gpt4 book ai didi

git - 为什么我的 git 预提交钩子(Hook)不修剪行尾的空白?

转载 作者:行者123 更新时间:2023-12-02 21:02:08 26 4
gpt4 key购买 nike

我使用的是 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

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

最佳答案

关于您的脚本:在退出前的最后一条指令中,您可能想调用 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

关于git - 为什么我的 git 预提交钩子(Hook)不修剪行尾的空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59147108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com