确定最近 Git 提交目录中是否有任何文件更改
Determine If Any File Changed In Directory For Latest Git Commit
我正在为 git
写一个 post-commit
钩子。我想知道最新的提交是否更改了特定目录中的任何文件。如果有任何更改,我可以继续并调用一些昂贵的代码,否则我可以跳过它。
所以,far I'm getting the short hash like so:
# get last commit hash prepended with @ (i.e. @8a323d0)
function parse_git_hash() {
git rev-parse --short HEAD 2> /dev/null | sed "s/\(.*\)/@/"
}
现在,我需要确定指定目录是否有任何更改。但我不确定该怎么做。我看过并玩过 git-log
和 git-show
但到目前为止没有成功。
所以,我需要做这样的事情。
if [directory-changed()] {
echo "start expensive operation"
}
这实际上让我走到了那里:实际上它抓住了最后一次提交而不是指定的提交。
git log -U a79851b -1 my/directory/path
提前致谢。
您可以通过以下方式获取最新提交中添加、修改、删除和重命名的文件:
git diff --name-only --diff-filter=AMDR --cached @~..@
要获取影响特定目录的更改,请使用 grep
过滤输出。例如:
changes() {
git diff --name-only --diff-filter=AMDR --cached @~..@
}
if changes | grep -q dirname {
echo "start expensive operation"
}
带参数的脚本修改版本为:
#!/bin/bash
git diff --name-only --diff-filter=ADMR @~..@ | grep -q
retVal=$?
echo "git command retVal : ${retVal}"
if [ $retVal -eq 0 ]; then
echo "folder/file : changed"
else
echo "no match found for the folder/file : "
exit $retVal
fi
我正在为 git
写一个 post-commit
钩子。我想知道最新的提交是否更改了特定目录中的任何文件。如果有任何更改,我可以继续并调用一些昂贵的代码,否则我可以跳过它。
所以,far I'm getting the short hash like so:
# get last commit hash prepended with @ (i.e. @8a323d0)
function parse_git_hash() {
git rev-parse --short HEAD 2> /dev/null | sed "s/\(.*\)/@/"
}
现在,我需要确定指定目录是否有任何更改。但我不确定该怎么做。我看过并玩过 git-log
和 git-show
但到目前为止没有成功。
所以,我需要做这样的事情。
if [directory-changed()] {
echo "start expensive operation"
}
这实际上让我走到了那里:实际上它抓住了最后一次提交而不是指定的提交。
git log -U a79851b -1 my/directory/path
提前致谢。
您可以通过以下方式获取最新提交中添加、修改、删除和重命名的文件:
git diff --name-only --diff-filter=AMDR --cached @~..@
要获取影响特定目录的更改,请使用 grep
过滤输出。例如:
changes() {
git diff --name-only --diff-filter=AMDR --cached @~..@
}
if changes | grep -q dirname {
echo "start expensive operation"
}
带参数的脚本修改版本为:
#!/bin/bash
git diff --name-only --diff-filter=ADMR @~..@ | grep -q
retVal=$?
echo "git command retVal : ${retVal}"
if [ $retVal -eq 0 ]; then
echo "folder/file : changed"
else
echo "no match found for the folder/file : "
exit $retVal
fi