以编程方式打印git修订版并检查未提交的更改 [英] Programmatically printing git revision and checking for uncommitted changes
问题描述
例如,如果有未提交的更改,它应该输出
警告:未提交的更改。该输出可能不可重现。
另外,产生
当前提交:d27ec73cf2f1df89cbccd41494f579e066bad6fe
理想情况下,它应该使用管道瓷
Git探测的需要的位是
diff-index
,
rev-parse --verify
,也许 rev-parse --show-cdup
with LS-文件 - 其他
。
以下shell程序使用这些Git管道命令,可调节未跟踪/忽略的哈希ndling,并且对所有可能的错误情况都非常小心。
#!/ bin / sh
#warn-unclean:如果工作树和索引不干净,则输出警告
#为了最严格,请设置check_untracked = yes和check_ignored = yes。
#当两者都是'是'时,验证工作树和索引是否与HEAD相同。
#当只有check_untracked为yes时,允许额外忽略的文件。
#如果都不是,则允许额外的未跟踪文件和被忽略的文件。
check_untracked =是
check_ignored =是
警告(){
回波警告: ' $ * \
' '
}
#将HEAD与被跟踪文件的索引和/或工作树版本进行比较
git diff-index --quiet HEAD
案例$?在
0)
如果测试$ check_untracked!= yes;那么
clean = yes
else
#仍然需要检查未跟踪文件
or_ignored =''
exclude = - exclude-standard
如果测试$ check_ignored= yes;然后
or_ignored ='或忽略'
exclude =''
fi
(
#移动到工作树顶层
if up =$(git rev-parse --show-cdup);然后
test -n$ up&& cd$ up
else
echo'error运行 混帐REV-解析 - 显示,CDUP
出口129
科幻
#检查未跟踪文件
git的LS-文件 - 其他$排除--error-unmatch。> / dev / null 2>& 1
case $?in
0)#有一些untracked / ignored文件存在
warn'未跟踪'$ or_ignored'文件存在。'
exit 1
;;
1)#没有未跟踪的文件
exit 0
;;
*)
echo'error runninggit diff-index!'
exit 129
;;
esac
)
case $? in
0)clean = yes ;;
1)clean = no ;;
*)退出$? ;;
esac
fi
test$ clean= yes&&
如果c =$(git rev-parse --verify HEAD);然后
echo'当前提交:'$ c
else
echo'运行错误'git rev-parse --verify!'
fi
;;
1)
警告'某个被跟踪的文件有一个未提交的更改。'
;;
*)
echo'error runninggit diff-index!'
exit 129
;;
esac
您可能会在 exit
s如果你希望它的退出代码在所有情况下都是有意义的。
如果你不关心所有错误处理或未跟踪/忽略的处理,那么简短的东西就足够了:
if git diff-index --quiet;然后
printf'当前提交:%s\\\
'$(git rev-parse --verify HEAD)
else
echo'Warning:...'
fi
您也可以以简洁的方式检查未跟踪的文件(可以对其进行调整以适应忽略等),而不会出错处理:
git ls-files --others --exclude-standard --error-unmatch \
./$(git rev-parse --show-cdup)> / dev / null 2>& 1
To ensure that my scientific analysis is reproducible, I'd like to programmatically check if there are any modifications to the code base that aren't checked in, and if not, print out what commit is being used.
For example, if there are uncommitted changes, it should output
Warning: uncommitted changes made. This output may not be reproducible.
Else, produce
Current commit: d27ec73cf2f1df89cbccd41494f579e066bad6fe
Ideally, it should use "plumbing", not "porcelain".
The bits of Git plumbing you need are
diff-index
,
rev-parse --verify
and maybe rev-parse --show-cdup
with ls-files --others
.
The following shell program uses those Git plumbing commands, has adjustable untracked/ignored handling, and is fairly careful about all the possible error cases.
#!/bin/sh
# warn-unclean: print a warning if the working tree and index are not clean
# For utmost strictness, set check_untracked=yes and check_ignored=yes.
# When both are 'yes', verify that working tree and index are identical to HEAD.
# When only check_untracked is yes, extra ignored files are allowed.
# When neither is yes, extra untracked files and ignored files are allowed.
check_untracked=yes
check_ignored=yes
warn() {
echo 'Warning: '"$*" \
'This output may not be reproducible.'
}
# Compare HEAD to index and/or working tree versions of tracked files
git diff-index --quiet HEAD
case $? in
0)
if test "$check_untracked" != yes; then
clean=yes
else
# Still need to check for untracked files
or_ignored=''
exclude=--exclude-standard
if test "$check_ignored" = yes; then
or_ignored=' or ignored'
exclude=''
fi
(
# Move to top level of working tree
if up="$(git rev-parse --show-cdup)"; then
test -n "$up" && cd "$up"
else
echo 'error running "git rev-parse --show-cdup"'
exit 129
fi
# Check for untracked files
git ls-files --others $exclude --error-unmatch . >/dev/null 2>&1
case $? in
0) # some untracked/ignored file is present
warn 'some untracked'"$or_ignored"' file is present.'
exit 1
;;
1) # no untracked files
exit 0
;;
*)
echo 'error running "git diff-index"!'
exit 129
;;
esac
)
case $? in
0) clean=yes ;;
1) clean=no ;;
*) exit $? ;;
esac
fi
test "$clean" = yes &&
if c="$(git rev-parse --verify HEAD)"; then
echo 'Current commit: '"$c"
else
echo 'error running "git rev-parse --verify"!'
fi
;;
1)
warn 'some tracked file has an uncommitted change.'
;;
*)
echo 'error running "git diff-index"!'
exit 129
;;
esac
You might sprinkle in some more exit
s if you want its exit code to be meaningful in all cases.
If you do not care for all the error handling or the untracked/ignored handling, then something short might suffice:
if git diff-index --quiet; then
printf 'Current commit: %s\n' "$(git rev-parse --verify HEAD)
else
echo 'Warning: …'
fi
You can also check for untracked files (which could be tweaked for ignored etc) in a terse way, without error handling:
git ls-files --others --exclude-standard --error-unmatch \
"./$(git rev-parse --show-cdup)" >/dev/null 2>&1
这篇关于以编程方式打印git修订版并检查未提交的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!