如何限制 git 预提交挂钩仅作用于具有特定扩展名的文件?
How do I limit a git pre-commit hook to only act on files with certain extensions?
我在 ~/.git-templates/hooks/pre-commit 有以下 Git 预提交挂钩。它旨在从文件中的每一行的末尾删除不必要的白色 space ...
# 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]+).*//'`"
else
file="`echo $line | sed -r 's/:[0-9]+: .*//'`"
line_number="`echo $line | sed -r 's/.*:([0-9]+).*//'`"
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 3[31m$file3[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
问题是,我只想将预提交挂钩应用于某些扩展名的文件。我需要做什么才能将操作限制为我选择的扩展程序?
A pre-commit hook 没有参数。
但是,如 this gist, you can list files about to be committed with git diff-index
.
具体来说,使用 --diff-filter
option:
git diff-index --name-only --cached --diff-filter = ACMRTX
示例:
# Show files about to be commited that do not match filters in .jshintignore and are js files
for FILE in ` git diff-index --name-only --cached --diff-filter = ACMRTX $ {against} - | grep -v -f .jshintignore | grep \. js $ -i ` ; do
jshint $ {FILE} >> / dev / null
EXIT_CODE = ` expr $ {EXIT_CODE} + $? `
done
if [[ $ {EXIT_CODE} -ne 0]] ; then
echo " "
echo " ----------------------------------------------- "
echo " | * * * EPICFAIL * * * | "
...
fi
exit $ (( $ {EXIT_CODE} ))
我在 ~/.git-templates/hooks/pre-commit 有以下 Git 预提交挂钩。它旨在从文件中的每一行的末尾删除不必要的白色 space ...
# 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]+).*//'`"
else
file="`echo $line | sed -r 's/:[0-9]+: .*//'`"
line_number="`echo $line | sed -r 's/.*:([0-9]+).*//'`"
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 3[31m$file3[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
问题是,我只想将预提交挂钩应用于某些扩展名的文件。我需要做什么才能将操作限制为我选择的扩展程序?
A pre-commit hook 没有参数。
但是,如 this gist, you can list files about to be committed with git diff-index
.
具体来说,使用 --diff-filter
option:
git diff-index --name-only --cached --diff-filter = ACMRTX
示例:
# Show files about to be commited that do not match filters in .jshintignore and are js files
for FILE in ` git diff-index --name-only --cached --diff-filter = ACMRTX $ {against} - | grep -v -f .jshintignore | grep \. js $ -i ` ; do
jshint $ {FILE} >> / dev / null
EXIT_CODE = ` expr $ {EXIT_CODE} + $? `
done
if [[ $ {EXIT_CODE} -ne 0]] ; then
echo " "
echo " ----------------------------------------------- "
echo " | * * * EPICFAIL * * * | "
...
fi
exit $ (( $ {EXIT_CODE} ))