threads / discuss / 62075

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

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

## tl;dr

One message between Sep 6, 2024 and Sep 6, 2024.

replies: 0people: 1as markdown or json

Marco P.· Sep 6, 2024, 23:04 UTC · lore

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’

← back to recent threads