为什么我的git不从行尾预提交钩子来修剪空白?

时间:2019-12-02 21:45:09

标签: git whitespace pre-commit-hook

我在Mac Mojave上。我已经在〜/ .git-templates / hooks / pre-commit中创建了一个文件,我想从我提交的文件的行尾删除空白。我希望在我所有的项目中都能做到这一点。

# A git hook script to find and fix trailing whitespace in your commits. Bypass
# it with the --no-verify option to git-commit.

# detect platform
platform="win"
uname_result=`uname`
if [[ "$uname_result" == "Linux" ]]; then
  platform="linux"
elif [[ "$uname_result" == "Darwin" ]]; then
  platform="mac"
fi

# change IFS to ignore filename's space in |for|
IFS="
"

# remove trailing whitespace in modified lines
for line in `git diff --check --cached | sed '/^[+-]/d'` ; do
  # get file name
  if [[ "$platform" == "mac" ]]; then
    file="`echo $line | sed -E 's/:[0-9]+: .*//'`"
    line_number="`echo $line | sed -E 's/.*:([0-9]+).*/\1/'`"
  else
    file="`echo $line | sed -r 's/:[0-9]+: .*//'`"
    line_number="`echo $line | sed -r 's/.*:([0-9]+).*/\1/'`"
  fi

  # since $file in working directory isn't always equal to $file in index,
  # we backup it; thereby we can add our whitespace fixes without accidently
  # adding unstaged changes
  backup_file="${file}.working_directory_backup"
  cat "$file" > "$backup_file"
  git checkout -- "$file" # discard unstaged changes in working directory

  # remove trailing whitespace in $file (modified lines only)
  if [[ "$platform" == "win" ]]; then
    # in windows, `sed -i` adds ready-only attribute to $file (I don't kown why), so we use temp file instead
    sed "${line_number}s/[[:space:]]*$//" "$file" > "${file}.bak"
    mv -f "${file}.bak" "$file"
  elif [[ "$platform" == "mac" ]]; then
    sed -i "" "${line_number}s/[[:space:]]*$//" "$file"
  else
    sed -i "${line_number}s/[[:space:]]*$//" "$file"
  fi
  git add "$file" # to index, so our whitespace changes will be committed

  # restore unstaged changes in $file from its working directory backup, fixing
  # whitespace that we fixed above
  sed "${line_number}s/[[:space:]]*$//" "$backup_file" > "$file"
  rm "$backup_file"

  [[ "$platform" == "mac" ]] || e_option="-e" # mac does not understand -e
  echo $e_option "Removed trailing whitespace in \033[31m$file\033[0m:$line_number"
done

echo

# credits:
# https://github.com/philz/snippets/blob/master/pre-commit-remove-trailing-whitespace.sh
# https://github.com/imoldman/config/blob/master/pre-commit.git.sh

# If there still are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

# Now we can commit
exit

因此,问题在于它没有修剪行尾的空白。提交后打开文件时,仍然看到空白。所以我的问题是如何解决这个问题?我是否将钩子放在错误的位置,或者我的文件中还需要做其他事情?

4 个答案:

答案 0 :(得分:5)

关于脚本:退出前的最后一次操作,您可能想调用git diff而不是git diff-index,对吗?


关于“在提交之前从文件中删除尾随空格”的操作:

  • 大多数编辑器允许在保存文件时执行此操作,这可能是从自己编辑的文件中删除尾随空格的最简单方法

  • 使用特定于git的触发器:一种更适合的方法是在git属性中使用干净的过滤器(请参见git书的Keyword Expansion部分):

clean filter

这将在您“ git add”每个文件时而不是在提交时应用更改:

# choose a name for your filter (e.g : 'trimspace'), and write
# the two 'clean' and 'smudge' action :
$ git config filter.trimspace.clean 'sed -e "s/[[:space:]]*$//g"'
$ git config filter.trimspace.smudge cat

# edit the `.gitattributes` file at the root of your repo,
# and target all the files you may want to trim :
$ cat .gitattributes
*.txt filter=trimspace
*.c filter=trimspace
*.py filter=trimspace
...

# from now on : running `git add` will auto trim targeted files when
# they are added to the index

答案 1 :(得分:3)

首先,确保挂钩位于全局挂钩路径(available since Git 2.9)所引用的文件夹中

git config --global core.hooksPath /path/to/my/centralized/hooks

然后,检查pre-commit钩是否可执行,并实际运行:在开始处至少添加一个回显,以验证其在提交时的执行。

答案 2 :(得分:3)

变量$against似乎在这里引起问题。 $against变量未在此处初始化,这似乎是失败的原因。

正如您提到的那样,您的代码在该行死亡。如果我删除行exec git diff-index --check --cached $against --,您的代码将正常执行(无错误情况)。

转到该特定行::如果仍然保留空格,则该行用于打印出任何行。但是,我发现代码是部分编写的。

完整代码必须为:

#!/bin/sh
#
# This hook script verifies that there are no whitespace errors in the files to be committed

if git rev-parse --verify HEAD >/dev/null 2>&1
then
 against=HEAD
else
 # Initial commit: diff against an empty tree object
 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to stderr.
exec 1>&2

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

可以在this网站上找到该代码。

您需要在最后一行进行比较,但是$against未定义。如果不存在HEAD,则此代码会将其正确定义为HEAD或某些虚拟树对象。

添加此代码后,它可以在我的系统中正常运行(Ubuntu 16.04LTS,Git版本2.7.4)

尝试添加完整的代码,让我知道它是否适合您。

谢谢

答案 3 :(得分:1)

$against未定义。从官方pre-commit.sample添加以下内容:

if git rev-parse --verify HEAD >/dev/null 2>&1
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=$(git hash-object -t tree /dev/null)
fi