我如何检查bash脚本,如果我的本地git回购有变化

有一些脚本在检查更改时无法正常工作。

我试过这样的:

VN=$(git describe --abbrev=7 HEAD 2>/dev/null) git update-index -q --refresh CHANGED=$(git diff-index --name-only HEAD --) if [ ! -z $CHANGED ]; then VN="$VN-mod" fi 

是否有某种布尔检查,如果自上次提交以来发生了更改,或者如何才能真正testing是否对本地存储库进行了新更改

我正在做这一切的版本创build脚本(我在这里find的地方)

你正在做的事情几乎可以奏效:如果它是空的,你应该引用$CHANGED-ztesting为空,这意味着没有变化。 你的意思是:

 if [ -n "$CHANGED" ]; then VN="$VN-mod" fi 

git的GIT-VERSION-GEN

 git update-index -q --refresh test -z "$(git diff-index --name-only HEAD --)" || VN="$VN-dirty" 

看起来你是在抄袭它,但你忘记了引用的细节。

当然,你也可以这样做:

 if git diff-index --quiet HEAD --; then # no changes else # changes fi 

或者如果你只关心“事情已经改变”的情况下:

 if ! git diff-index --quiet HEAD --; then VN="$VN-mod" fi 

使用--quiet有一个好处,即git可以在遇到单个diff时立即停止处理,因此可能不需要检查整个工作树。

使用git状态

 cd /git/directory if [[ `git status --porcelain` ]]; then # changes else # no changes fi 

虽然Jefromi的回答很好,但我只是为了参考。

从git源代码有一个sh脚本,其中包括以下内容。

 require_clean_work_tree () { git rev-parse --verify HEAD >/dev/null || exit 1 git update-index -q --ignore-submodules --refresh err=0 if ! git diff-files --quiet --ignore-submodules then echo >&2 "Cannot $1: You have unstaged changes." err=1 fi if ! git diff-index --cached --quiet --ignore-submodules HEAD -- then if [ $err = 0 ] then echo >&2 "Cannot $1: Your index contains uncommitted changes." else echo >&2 "Additionally, your index contains uncommitted changes." fi err=1 fi if [ $err = 1 ] then test -n "$2" && echo >&2 "$2" exit 1 fi } 

有类似的问题,但我不得不检查添加的文件也。 所以我做了以下几点:

 cd /local/repo RUN=0 git diff --no-ext-diff --quiet --exit-code || RUN=1 if [ $RUN = 0 ]; then RUN=`git ls-files --exclude-standard --others| wc -l` fi if [ $RUN = 0 ]; then exit 0 fi 

这里有一个很好的bash脚本函数,用于检查是否有差异,将其打印到用户,并在用户提交之前提示用户是否想要部署。 为Heroku和Python应用程序而构build,但对于其他任何应用程序都不需要更改。

 commit(){ echo "Please enter a commit message..." read msg git add . --all git commit -am $msg } check_commit(){ echo ========== CHECKING FOR CHANGES ======== changes=$(git diff) if [ -n "$changes" ]; then echo "" echo "*** CHANGES FOUND ***" echo "$changes" echo "" echo "You have uncomitted changes." echo "Would you like to commit them (y/n)?" read n case $n in "y") commit;; "n") echo "Changes will not be included...";; *) echo "invalid option";; esac else echo "... No changes found" fi } deploy(){ check_commit echo ========== DEPLOYING TO HEROKU ======== git push heroku master heroku run python manage.py syncdb } 

你可以从Gist复制: https ://gist.github.com/sshadmand/f33afe7c9071bb725105

这是我怎么做的…

 CHANGES=`git status | grep "working directory clean"` if [ ! CHANGES -eq "" ] then # do stuff here else echo "You have uncommitted changes..." fi 

git身份是你的朋友

更改为git目录以使git状态正常工作

 cd c:/path/to/.git 

设置一个variables来设置工作树,所以你不会得到'这个操作必须运行在工作树'的错误

 WORKTREE=c:/path/to/worktree 

捕获bashvariables中的git状态输出

使用 – 保证在标准格式和可parsing的瓷器

 CHANGED=$(git --work-tree=${WORKTREE} status --porcelain) 

如果-n(不为空),我们有变化。

 if [ -n "${CHANGED}" ]; then echo 'changed'; else echo 'not changed'; fi 

这很好。 它还将列出受影响的文件:

 if git diff-index --name-status --exit-code HEAD; then echo Git working copy is clean...; else echo ; echo ERROR: Git working copy is dirty!; echo Commit your changes and try again.; fi;