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

[PATCH] Allow git-diff exit with codes similar to diff(1)

From
Alex Riesen <raa.lkml@gmail.com>
Date
Mar 14, 2007, 14:01 UTC
Message-ID
<81b0412b0703140701h60982fddw3ed8fa71288cb220@mail.gmail.com>
In-Reply-To
<7v6494dwms.fsf@assigned-by-dhcp.cox.net>

This introduces a new command-line option: --exit-code. The diff programs will return 1 for differences, return 0 for equality, and something else for errors.

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
Updated the patch:
- added more tests: path limiter and -S
- moved exit code into diff_options and used diffcore_std* for its evaluation
On 3/14/07, Junio C Hamano <junkio@cox.net> wrote:
Show 6 quoted lines
> > Challenging... Now, if someone just told me where to look for
> > differences in diff-tree case...
>
> As the resident git-diff expert I might hack on this myself as
> the --quiet options are useful, and --exit-code comes almost
> free when you properly do --quiet that implies --quick.
Thanks! That would _most_ welcome :)
Show 6 quoted lines
> > Can it eventually be wired to "-s" (DIFF_FORMAT_NO_OUTPUT)?
>
> I do not think so.  When we run diff internally to pick the set
> of paths (i.e. run diffcore and check contents of diff_queue,
> just like you did for diff-index/diff-files), we internally use
> NO_OUTPUT.  See merge-recursive.c for an example.
Right, is not that simple as it looks...
Show 15 quoted lines
> >> >       diffcore_std(&revs->diffopt);
> >> > +     ret = revs->diffopt.diff_exit_code && diff_queued_diff.nr ? 1: 0;
> >> >       diff_flush(&revs->diffopt);
> >> >       return ret;
> >> >  }
> >>
> >> This side looks correct, as you are counting queued_diff.nr after
> >> letting diffcore_std() to filter the results.
> >
> > And it will continue to work if the diffing is left early because of
> > no output needed. Err, will it?
>
> To implement --quick correctly, you need to know when it is safe
> to leave early.  Presence of -S (pickaxe) would most likely mean
> you shouldn't leave early.

Thanks, that got me thinking. Moved all exit code evaluation into diffcore_std, added a field for the code to diff_options, and use it if called with --exit-code.

 Documentation/diff-options.txt |    5 +++
 builtin-diff-files.c           |    4 ++-
 builtin-diff-index.c           |    4 ++-
 builtin-diff-tree.c            |    4 +-
 builtin-diff.c                 |   19 +++++++-----
 diff-lib.c                     |    5 ++-
 diff.c                         |    6 ++++
 diff.h                         |    5 ++-
 t/t4017-diff-retval.sh         |   64 ++++++++++++++++++++++++++++++++++++++++
 9 files changed, 102 insertions(+), 14 deletions(-)
 create mode 100755 t/t4017-diff-retval.sh
Previous: Junio C HamanoNext: Junio C Hamano
Message 25 of 32 in “Allow git-diff exit with codes similar to diff(1)”
  1. Allow git-diff exit with codes similar to diff(1)Alex Riesen, Mar 14, 2007
  2. Johannes SchindelinMar 14, 2007
  3. Linus TorvaldsMar 14, 2007
  4. Junio C HamanoMar 14, 2007
  5. Linus TorvaldsMar 14, 2007
  6. Johannes SchindelinMar 14, 2007
  7. Linus TorvaldsMar 14, 2007
  8. Johannes SchindelinMar 14, 2007
  9. Alex RiesenMar 14, 2007
  10. Johannes SchindelinMar 14, 2007
  11. Alex RiesenMar 14, 2007
  12. Johannes SchindelinMar 14, 2007
  13. Simon 'corecode' SchubertMar 15, 2007
  14. Alex RiesenMar 15, 2007
  15. Junio C HamanoMar 14, 2007
  16. Alex RiesenMar 14, 2007
  17. Junio C HamanoMar 14, 2007
  18. Junio C HamanoMar 14, 2007
  19. Alex RiesenMar 14, 2007
  20. Junio C HamanoMar 14, 2007
  21. Alex RiesenMar 14, 2007
  22. Junio C HamanoMar 14, 2007
  23. Alex RiesenMar 14, 2007
  24. Junio C HamanoMar 14, 2007
  25. Allow git-diff exit with codes similar to diff(1)Alex Riesen, Mar 14, 2007
  26. Junio C HamanoMar 14, 2007
  27. Alex RiesenMar 14, 2007
  28. Junio C HamanoMar 14, 2007
  29. Allow git-diff exit with codes similar to diff(1)Alex Riesen, Mar 14, 2007
  30. Junio C HamanoMar 14, 2007
  31. Junio C HamanoMar 14, 2007
  32. Alex RiesenMar 14, 2007

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.