Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make "git describe" mention the presence or absence of local changes?

How can I check, in a script, if local changes are present? Perhaps in combination with git describe?

like image 876
Adrian Panasiuk Avatar asked Dec 31 '09 12:12

Adrian Panasiuk


2 Answers

Since git 1.6.6, git-describe has accepted a --dirty option. If you have uncommitted changes in your working tree then you'll get output like this:

$ git describe --dirty
1.0.2-2-g215081f-dirty
like image 157
Sam Morris Avatar answered Nov 15 '22 09:11

Sam Morris


You will need to make sure that both the two following properties are met:

  1. That there are no differences between HEAD and the index cache

    git diff-index --cached HEAD

  2. That there are no differences between the index and the working tree:

    git diff-files

Both commands take a --quiet parameter which will set the exit code according to whether there are differences or not (starting some time after git 1.4). If you need to make it work on git 1.4, you need to run the commands without --quiet and check whether they produce any output.

Note: git diff is a porcelain command, and thus should not be used in scripts. Use above plumbing commands instead.

Example shell code, taken from my git_version.sh script:

git_dirty=yes
# git-1.4 does not understand "git-diff-files --quiet"
# git-1.4 does not understand "git-diff-index --cached --quiet HEAD"
if [ "x$($GIT diff-files)" = "x" ] && [ "x$($GIT diff-index --cached HEAD)" = "x" ]; then
    git_dirty=no
fi

If you can require a git version >= 1.5, if git diff-files --quiet && git diff-index --quiet --cached HEAD; then can replace above comparison.

Note: This solution (exactly like Antony's interactive git diff HEAD --quiet) only discovers local changes relative to HEAD. However, local commits can arguable also be considered local changes, and naturally will not show up in any diff against HEAD. You will need to check the SHA1 value git describe uses to detect whether HEAD is from a set of commits you consider not to be local changes.

like image 28
ndim Avatar answered Nov 15 '22 07:11

ndim