{"thread":{"id":"62075","subject":"Behavior change for git diff --cached --exit-code in git 2.46.0","startedAt":"2024-09-06T23:04:34Z","lastAt":"2024-09-06T23:04:34Z","messageCount":1,"participants":["Marco P."],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"502376","messageId":"646EB2F8-53B3-4B24-90E0-09A71DD4445F@mpri.me","threadId":"62075","inReplyTo":null,"subject":"Behavior change for git diff --cached --exit-code in git 2.46.0","fromName":"Marco P.","fromEmail":"marco@mpri.me","sentAt":"2024-09-06T23:04:19Z","receivedAt":"2024-09-06T23:04:34Z","isPatch":false,"sender":{"key":"marco@mpri.me","avatar":null},"body":"I noticed a change of behavior when upgrading from 2.45.2 to 2.46.0.\nYou can decide whether this is a regression or an improvement.\nI thought I’d say something just in case. Don’t shoot the messenger!\n\n\n\nOverview:\nThe --summary option to `git diff --cached` seems to override the --exit-code option in 2.46.0 such that this is no longer true:\n\n>  --exit-code\n>    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.\n\nYou can reproduce yourself by staging something, then trying the 4 combinations of:\n - With and without --summary\n -  2.45.2 and 2.46.0\n\nYou will see that with 2.46.0 + --summary flag =>  exit code is always 0.\nThis is different from 2.45.3 where --exit-code influences sets the exit code regardless of --summary\n\n\nMore context:\n\nI have a scheduled script that daily updates submodules.\nThe logic is:\n\n```bash\ngit submodule update --init --remote --checkout\ngit add path/to/submodule\nif git diff --cached --exit-code --summary;\nthen\n  # Nothing to commit\nelse\n  # Create commit and push \nfi\n```\n\nThis has been working for a while, most recently with git 2.45.2.\nAfter updating to 2.46.0, it stopped working.\nIt would _always_ think there’s \"nothing to commit\", even when the submodule did have new commits.\n\nAfter investigating, I discovered that the --summary option seems to override the --exit-code option.\ni.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)\n\nThis is not limited to submodules, the behavior is the same for regular staged files, e.g.:\n\n```\n# Stage a file:\n$ git add debug.md\n\n# Diff with 2.45.2 prints summary and exits with 1:\n$ git diff --cached --exit-code --summary ; echo $?\n create mode 100644 debug.md\n1\n\n# Diff with 2.46.0 prints summary and exits with 0:\n$ git-2.46.0 diff --cached --exit-code --summary ; echo $?\n create mode 100644 debug.md\n0\n```\n\nI fixed my script by dropping the --summary option.\nBut maybe this behavior change was unintended and not covered by tests, in which case you may want to treat this as a bug report.\n\nTaking this chance to thank you for reading and for maintaining git. I don’t know what I’d do without it!\n\nCheers,\nM’\n\n\n\n"}]}