threads / discuss / 21467

determine if the tree is dirty

Subject: determine if the tree is dirty

## tl;dr

5 messages between Nov 3, 2009 and Nov 3, 2009.

replies: 4people: 4as markdown or json

Adam Mercer· Nov 3, 2009, 18:57 UTC · lore
Hi

As part of a python script I need to determine if a tree has any uncommitted changes, so far I am been using:

  # determine tree status
  status_cmd = 'git status'
  status_output = run_external_command(status_cmd,
honour_ret_code=False)[1].strip()
  if status_output.endswith('no changes added to commit (use "git add"
and/or "git commit -a")'):
    git_status = 'UNCLEAN: Some modifications not committed'
  else:
    git_status = 'CLEAN: All modifications committed'

but I feel that this relies to heavily on the porcelain and that there should be a better way to accomplish this without relying on parsing the output of git-status.

Does anyone know of a better way to accomplish this?
Cheers
Adam
Jeff Epler· Nov 3, 2009, 19:06 UTC · re: Adam Mercer · lore

Re: determine if the tree is dirty

On Tue, Nov 03, 2009 at 12:57:37PM -0600, Adam Mercer wrote:
> As part of a python script I need to determine if a tree has any
> uncommitted changes
GIT-VERSION-GEN uses something like this (shell script):
        git update-index -q --refresh
        test -z "$(git diff-index --name-only HEAD --)" || VN="$VN-dirty" ;;
Jeff
Junio C Hamano· Nov 3, 2009, 19:19 UTC · re: Adam Mercer · lore

Re: determine if the tree is dirty

Adam Mercer <ramercer@gmail.com> writes:
Show 18 quoted lines
> As part of a python script I need to determine if a tree has any
> uncommitted changes, so far I am been using:
>
>   # determine tree status
>   status_cmd = 'git status'
>   status_output = run_external_command(status_cmd,
> honour_ret_code=False)[1].strip()
>   if status_output.endswith('no changes added to commit (use "git add"
> and/or "git commit -a")'):
>     git_status = 'UNCLEAN: Some modifications not committed'
>   else:
>     git_status = 'CLEAN: All modifications committed'
>
> but I feel that this relies to heavily on the porcelain and that there
> should be a better way to accomplish this without relying on parsing
> the output of git-status.
>
> Does anyone know of a better way to accomplish this?
Use plumbing commands.

"git diff-files" will show changes you have in the work tree compared to the index.

"git diff-index HEAD" will show changes you have in the work tree compared to the HEAD.

"git diff-index --cached HEAD" will show changes you have in the index compared to the HEAD.

Adam Mercer· Nov 3, 2009, 19:30 UTC · re: Junio C Hamano · lore

Re: determine if the tree is dirty

On Tue, Nov 3, 2009 at 13:06, Jeff Epler <jepler@unpythonic.net> wrote:
> GIT-VERSION-GEN uses something like this (shell script):
>        git update-index -q --refresh
>        test -z "$(git diff-index --name-only HEAD --)" || VN="$VN-dirty" ;;
On Tue, Nov 3, 2009 at 13:19, Junio C Hamano <gitster@pobox.com> wrote:
Show 10 quoted lines
> Use plumbing commands.
>
> "git diff-files" will show changes you have in the work tree compared to
> the index.
>
> "git diff-index HEAD" will show changes you have in the work tree compared
> to the HEAD.
>
> "git diff-index --cached HEAD" will show changes you have in the index
> compared to the HEAD.
Fantastic, this is exactly what I was after! Thanks!
Cheers
Adam
Markus Hitter· Nov 3, 2009, 21:07 UTC · re: Adam Mercer · lore

Re: determine if the tree is dirty

Am 03.11.2009 um 19:57 schrieb Adam Mercer:
> As part of a python script I need to determine if a tree has any
> uncommitted changes, so far I am been using:
> [...]
> Does anyone know of a better way to accomplish this?
I'm using
git ls-files -m -o -d --exclude-standard
If it returns anything, your repo is dirty.
Markus
- - - - - - - - - - - - - - - - - - -
Dipl. Ing. Markus Hitter
http://www.jump-ing.de/

← back to recent threads