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

Behavior change for git diff --cached --exit-code in git 2.46.0

From
MPMarco P. <marco@mpri.me>
Date
Sep 6, 2024, 23:04 UTC
Message-ID
<646EB2F8-53B3-4B24-90E0-09A71DD4445F@mpri.me>

I noticed a change of behavior when upgrading from 2.45.2 to 2.46.0. You can decide whether this is a regression or an improvement. I thought I’d say something just in case. Don’t shoot the messenger!

Overview: The --summary option to `git diff --cached` seems to override the --exit-code option in 2.46.0 such that this is no longer true:

>  --exit-code
>    Make the program exit with codes similar to diff(1). That is, it exits with 1 if there were differences and 0 means no differences.
You can reproduce yourself by staging something, then trying the 4 combinations of:
 - With and without --summary
 -  2.45.2 and 2.46.0

You will see that with 2.46.0 + --summary flag => exit code is always 0. This is different from 2.45.3 where --exit-code influences sets the exit code regardless of --summary

More context:

I have a scheduled script that daily updates submodules. The logic is:

```bash
git submodule update --init --remote --checkout
git add path/to/submodule
if git diff --cached --exit-code --summary;
then
  # Nothing to commit
else
  # Create commit and push 
fi
```

This has been working for a while, most recently with git 2.45.2. After updating to 2.46.0, it stopped working. It would _always_ think there’s "nothing to commit", even when the submodule did have new commits.

After investigating, I discovered that the --summary option seems to override the --exit-code option. i.e. if --summary is present, then the exit code is always 0, and not 1 if there are staged changed (as I’d expect from --exit-code manual entry)

This is not limited to submodules, the behavior is the same for regular staged files, e.g.:

``` # Stage a file: $ git add debug.md

# Diff with 2.45.2 prints summary and exits with 1:
$ git diff --cached --exit-code --summary ; echo $?
 create mode 100644 debug.md
1
# Diff with 2.46.0 prints summary and exits with 0:
$ git-2.46.0 diff --cached --exit-code --summary ; echo $?
 create mode 100644 debug.md
0
```

I fixed my script by dropping the --summary option. But maybe this behavior change was unintended and not covered by tests, in which case you may want to treat this as a bug report.

Taking this chance to thank you for reading and for maintaining git. I don’t know what I’d do without it!

Cheers, M’

Message 1 of 1 in “Behavior change for git diff --cached --exit-code in git 2.46.0”
  1. Marco P.Sep 6, 2024

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.