git для Windows: удалить завершающие пробелы при фиксации
Я написал
pre-commit
крючок для удаления конечных пробелов с помощью git для Windows 2.27.0.windows.1. Это похоже на хорошо изученную тему, но, к сожалению, я не могу заставить ее работать должным образом. Это мой
.git\hooks\pre-commit
файл:
#!/bin/sh
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
git diff --cached --name-only --diff-filter=ACM -z $against | xargs -0 | while read LINE
do
if [ -n "$LINE" ]; then
remove_whitespace "$LINE"
git add "$LINE"
fi
done
Предположим, у меня есть файл
test.txt
уже зафиксирован, и в нем нет конечных пробелов. Теперь я добавляю к нему конечные пробелы и хочу, чтобы
pre-commit
чтобы удалить этот пробел и ничего не фиксировать (поскольку изменений не было). Но вот что происходит:
C:\> git status
On branch test
Your branch is ahead of 'origin/test' by 17 commits.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
C:\> git commit test.txt -m test
test.txt
[test 30cb9c0] test
C:\> git status
On branch test
Your branch is ahead of 'origin/test' by 18 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test.txt
C:\> git add test.txt
C:\> git status
On branch test
Your branch is ahead of 'origin/test' by 18 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
Он действительно удалил конечные пробелы из
test.txt
, с тех пор как я
add
очищенный файл с
git add test.txt
он больше не показывает изменений. Но
pre-commit
Хук, похоже, приводит к тому, что файл с конечным пробелом находится в подготовленной области, а файл без конечного пробела еще не подготовлен для фиксации.
Как я могу обработать все файлы, которые собираются быть зафиксированы, зафиксировать результат указанной обработки, но только если это привело к изменению уже зафиксированной версии?