For your first question, you can use git diff --quiet
(or git diff --exit-code
, but generally when you're using it for its exit code you want it not to print output anyhow, and git diff --quiet
implies --exit-code
) to determine if there have been any changes. That will give you a 1 value if there are changes, and a 0 if there are not. So if you want to have code that will run only if there are changes:
if ! git --git-dir="/dir/.git" diff --quiet
then
# do stuff...
fi
For your second question, I'd recommend a while read ...
loop to read lines from git diff-tree
:
git --git-dir="/dir/.git" diff-tree ORIG_HEAD.. |
while read srcmode dstmode srcsha dstsha status srcfile dstfile
do
# do something with $srcfile and $dstfile
done
Note that $srcmode
will have an extra :
at the beginning, and $dstfile
will only have a value if the file was renamed. If you don't want to worry about renames, pass in --no-renames
, and instead of seeing renames you'll see just the adds and deletes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…