git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: How to check repository/working tree status from a script

From
SBSanti Béjar <santi@agolina.net>
Date
May 20, 2009, 11:19 UTC
Message-ID
<adf1fd3d0905200419s33d5c73ahba240b354aca87a3@mail.gmail.com>
In-Reply-To
<20090520111028.GA15453@torres.zugschlus.de>
2009/5/20 Marc Haber <mh+git@zugschlus.de>:
Show 30 quoted lines
> On Tue, May 19, 2009 at 09:18:12AM -0700, Linus Torvalds wrote:
>> On Tue, 19 May 2009, Marc Haber wrote:
>> > On Tue, May 19, 2009 at 04:34:47PM +0100, Tony Finch wrote:
>> > > On Tue, 19 May 2009, Marc Haber wrote:
>> > > > I would like to check in a script whether there (a) are uncommitted
>> > > > changes (as in "working tree differs from local repository")
>> > >
>> > > # check working tree is not different from the index
>> > > git diff --quiet
>> > > # check that the index is not different from the head
>> > > git diff --quiet --cached
>> >
>> > $ git diff --quiet; echo $?
>> > 0
>> > $ git diff --quiet --cached; echo $?
>> > 0
>> > $ touch keks
>> > $ git diff --quiet; echo $?
>> > 0
>> > $ git diff --quiet --cached; echo $?
>> > 0
>> > $
>> >
>> > Am I missing something?
>>
>> If "keks" is already something you know about, then 'touch' wouldn't have
>> changed it, so diff won't show it.
>
> keks didn't exist previously, so the touch generated a new empty 0 byte
> file.
And 'git diff' alone only cares about files it knows.
Show 12 quoted lines
>
>> And if what you want to know about is whether there are _new_ files you
>> might want to check, then you need a third check: 'git ls-files'. You
>> won't see it in the error code, but you can do
>>
>>       others=$(git ls-files -o --exclude-standard)
>>
>> and then check it 'others' is empty or not.
>
> So parsing the output is both the canonical and only way to do so
> since there is no meaningful exit code? Can I assume that the
> formatting of git output is not subject to change?

Git output for the plumbing comands do not change, it is designed for script use. OTOH git output for porcelain commands can change. 'git ls-files' is a plumbing commands, see git's manpage for the complete list.

HTH, Santi

Previous: Marc HaberNext: Johannes Sixt
Message 10 of 15 in “How to check repository/working tree status from a script”
  1. Marc HaberMay 19, 2009
  2. Tony FinchMay 19, 2009
  3. Marc HaberMay 19, 2009
  4. Tony FinchMay 19, 2009
  5. Marc HaberMay 20, 2009
  6. Linus TorvaldsMay 19, 2009
  7. Linus TorvaldsMay 19, 2009
  8. Marc HaberMay 20, 2009
  9. Marc HaberMay 20, 2009
  10. Santi BéjarMay 20, 2009
  11. Johannes SixtMay 20, 2009
  12. Marc HaberMay 20, 2009
  13. Marc HaberMay 21, 2009
  14. Jeff KingMay 21, 2009
  15. Marc HaberMay 23, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.