我希望有一个预提交git钩子来检查(如果可能的话,自动删除)尾随空格。
在使git在提交之前自动删除尾随空格。中,我找到了一个链接到github页面,其中实现了这样一个钩子。这很好,但正如@VonC在该页面中提到的那样
由于该钩子获取每个文件的文件名,我建议您注意某些类型的文件:您不希望删除.md (标记)文件中的尾随空格!- VonC!
再往下走
我更愿意让钩子能够检测.md文件,而不是删除空白空间,而不是要求最终用户在git上添加一个-不验证选项。- VonC
据我所见,没有提到解决这一问题的办法。
由于我在我的项目中使用.md文件时有意识地尾随空格,这对我来说是个问题。
解决方案可能是微不足道的,但我对脚本所用的语言没有经验(目前也对学习它不感兴趣)。
这是脚本(github的副本):
#!/bin/bash
#
# A git hook script to find and fix trailing whitespace
# in your commits. Bypass it with the --no-verify option
# to git-commit
#
# usage: make a soft link to this file, e.g., ln -s ~/config/pre-commit.git.sh ~/some_project/.git/hooks/pre-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="
"
# autoremove trailing whitespace
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]+: .*//'`"
else
file="`echo $line | sed -r 's/:[0-9]+: .*//'`"
fi
# display tips
echo -e "auto remove trailing whitespace in \033[31m$file\033[0m!"
# since $file in working directory isn't always equal to $file in index, so we backup it
mv -f "$file" "${file}.save"
# discard changes in working directory
git checkout -- "$file"
# remove trailing whitespace
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 's/[[:space:]]*$//' "$file" > "${file}.bak"
mv -f "${file}.bak" "$file"
elif [ "$platform" == "mac" ]; then
sed -i "" 's/[[:space:]]*$//' "$file"
else
sed -i 's/[[:space:]]*$//' "$file"
fi
git add "$file"
# restore the $file
sed 's/[[:space:]]*$//' "${file}.save" > "$file"
rm "${file}.save"
done
if [ "x`git status -s | grep '^[A|D|M]'`" = "x" ]; then
# empty commit
echo
echo -e "\033[31mNO CHANGES ADDED, ABORT COMMIT!\033[0m"
exit 1
fi
# Now we can commit
exit如何对此进行修改,以便(例如)在检查中排除.md文件?另外,如果有可能排除多个文件类型,那就太好了。
发布于 2014-11-24 15:28:38
试试这个:
#!/bin/bash
#
# A git hook script to find and fix trailing whitespace
# in your commits. Bypass it with the --no-verify option
# to git-commit
#
# usage: make a soft link to this file, e.g., ln -s ~/config/pre-commit.git.sh ~/some_project/.git/hooks/pre-commit
LIST="md txt c cpp"
lookup() {
IFS=" "
for i in $LIST
do
if [ "$i" = "$1" ]
then
return 1
break
fi
done
return 0
}
# 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="
"
# autoremove trailing whitespace
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]+: .*//'`"
else
file="`echo $line | sed -r 's/:[0-9]+: .*//'`"
fi
lookup $(echo "$file" | awk -F . '{print $NF}')
if [ $? -eq 1 ]
then
echo Omitting "$file"
continue
fi
# display tips
echo -e "auto remove trailing whitespace in \033[31m$file\033[0m!"
# since $file in working directory isn't always equal to $file in index, so we backup it
mv -f "$file" "${file}.save"
# discard changes in working directory
git checkout -- "$file"
# remove trailing whitespace
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 's/[[:space:]]*$//' "$file" > "${file}.bak"
mv -f "${file}.bak" "$file"
elif [ "$platform" == "mac" ]; then
sed -i "" 's/[[:space:]]*$//' "$file"
else
sed -i 's/[[:space:]]*$//' "$file"
fi
git add "$file"
# restore the $file
sed 's/[[:space:]]*$//' "${file}.save" > "$file"
rm "${file}.save"
done
if [ "x`git status -s | grep '^[A|D|M]'`" = "x" ]; then
# empty commit
echo
echo -e "\033[31mNO CHANGES ADDED, ABORT COMMIT!\033[0m"
exit 1
fi
# Now we can commit
exit应该排除的文件扩展名列表在脚本开头的LIST中:
LIST="md txt c cpp"https://stackoverflow.com/questions/27107118
复制相似问题