# determine if the tree is dirty

5 messages from 2009-11-03 to 2009-11-03. Participants: Adam Mercer, Jeff Epler, Junio C Hamano, Markus Hitter.
Thread: https://gitlist.dev/t/21467

## Adam Mercer, 2009-11-03 18:57

Subject: determine if the tree is dirty
Message-ID: <799406d60911031057l5dcb4d4fi3705cc66997ff7f7@mail.gmail.com>
URL: https://gitlist.dev/e/799406d60911031057l5dcb4d4fi3705cc66997ff7f7%40mail.gmail.com

```
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, 2009-11-03 19:06

Subject: Re: determine if the tree is dirty
Message-ID: <20091103190619.GC30171@unpythonic.net>
URL: https://gitlist.dev/e/20091103190619.GC30171%40unpythonic.net
In-Reply-To: <799406d60911031057l5dcb4d4fi3705cc66997ff7f7@mail.gmail.com>

```
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, 2009-11-03 19:19

Subject: Re: determine if the tree is dirty
Message-ID: <7v3a4vs9g3.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v3a4vs9g3.fsf%40alter.siamese.dyndns.org
In-Reply-To: <799406d60911031057l5dcb4d4fi3705cc66997ff7f7@mail.gmail.com>

```
Adam Mercer <ramercer@gmail.com> writes:

> 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, 2009-11-03 19:30

Subject: Re: determine if the tree is dirty
Message-ID: <799406d60911031130u3e37677fi1a8dfd6f7fdfdfe9@mail.gmail.com>
URL: https://gitlist.dev/e/799406d60911031130u3e37677fi1a8dfd6f7fdfdfe9%40mail.gmail.com
In-Reply-To: <7v3a4vs9g3.fsf@alter.siamese.dyndns.org>

```
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:

> 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, 2009-11-03 21:07

Subject: Re: determine if the tree is dirty
Message-ID: <83B75882-0005-4BB3-BA7C-60A9105BDD5A@jump-ing.de>
URL: https://gitlist.dev/e/83B75882-0005-4BB3-BA7C-60A9105BDD5A%40jump-ing.de
In-Reply-To: <799406d60911031057l5dcb4d4fi3705cc66997ff7f7@mail.gmail.com>

```

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/

```
