git - How do I programmatically determine if there are uncommited changes? -
in makefile, i'd perform actions if there uncommited changes (either in working tree or index). what's cleanest , efficient way that? command exits return value of 0 in 1 case , non-zero in other suit purposes.
i can run git status
, pipe output through grep
, feel there must better way.
update: op daniel stutzbach points out in comments simple command git diff-index
worked him:
git diff-index --quiet head --
you can see "how check if command succeeded?" if using in bash script:
git diff-index --quiet head -- || echo "untracked"; //
note: commented anthony sottile
git diff-index head ...
fail on branch has no commits (such newly initialized repository).
1 workaround i've foundgit diff-index $(git write-tree) ...
"programmatically" means never ever rely on porcelain commands.
always rely on plumbing commands.
see "checking dirty index or untracked files git" alternatives (like git status --porcelain
)
you can take inspiration from new "require_clean_work_tree
function" written speak ;) (early october 2010)
require_clean_work_tree () { # update index git update-index -q --ignore-submodules --refresh err=0 # disallow unstaged changes in working tree if ! git diff-files --quiet --ignore-submodules -- echo >&2 "cannot $1: have unstaged changes." git diff-files --name-status -r --ignore-submodules -- >&2 err=1 fi # disallow uncommitted changes in index if ! git diff-index --cached --quiet head --ignore-submodules -- echo >&2 "cannot $1: index contains uncommitted changes." git diff-index --cached --name-status -r --ignore-submodules head -- >&2 err=1 fi if [ $err = 1 ] echo >&2 "please commit or stash them." exit 1 fi }
Comments
Post a Comment