3

I am trying to perform Git diff in two branches with the same file. I want to get the difference of two files.

  • If there is a fatal error
  • If there is a difference/ not matched
  • If there is NO difference/ matched

I tried saving the command in a variable to get the result. Example: GIT_DIFF=$(git diff RB_202005:/test.txt RB_202006:/test.txt) and then printing the variable (Example: echo $GIT_DIFF) but nothing is being returned.

Roan
  • 63
  • 2
  • 7

2 Answers2

4

Since you are only interested in the cases "no diff", "diff", "error", I would run a

git diff --exit-code --quiet .....

--exit-code sets the exit code in the way the normal diff would do.

--quiet suppresses the output.

If the exit code is 0, you don't have differences.

If the exit code is 1, you have differences.

If the exit code is 2 or 128, you have fatal errors.

UPDATED As the OP pointed out in the comment, git-diff --exit-code produces status code 128, if the file to be compared does not exist. According to the man-page, it should produce the same exit code as the standard diff, which would be 2. Hence it is best to treat any exit code larger than 1 as standard error. This would also catch the case that git itself is not found (in which case the shell would likely report exit code 127).

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Yes, but be careful and save it to a different variable immediately after running `git diff, because every command afterwards (including, say, any `echo`) will overwrite `$?`. – user1934428 May 12 '20 at 13:18
  • In my case, Exit Code 128 is when there are fatal errors. Thank you very much. – Roan May 12 '20 at 13:46
  • @Roan: Indeed, I could reproduce it. To me it seems to be a bug, because it contradicts the description given in the man-page. I suggest that you catch any exit code larger than 1 as fatal error, in case the git developers fix this one day. – user1934428 May 13 '20 at 06:03
  • 1
    Just `git diff --quiet` works, because it [implies --exit-code](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---quiet). – Gamma032 Jul 16 '21 at 05:42
  • @Roan : Exit code 128 really looks like a bug. A process is not supposed to set this explicitly. "Normal" exit codes are below 126 - see [here](https://tldp.org/LDP/abs/html/exitcodes.html). – user1934428 Jul 20 '21 at 10:19
0

This would not always work with the new (Git 2.30, Q1 2021) git diff -I, should you want to ignore a diff pattern.

"git diff -I<pattern> -exit-code(man)" should exit with 0 status when all the changes match the ignored pattern, but it didn't.

See commit 50f0439 (16 Dec 2020) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 59fcf74, 18 Dec 2020)

diff: correct interaction between --exit-code and -I<pattern>

Just like "git diff -w --exit-code"(man) should exit with 0 when ignoring whitespace differences results in no changes shown, if ignoring certain changes with "git diff -I<pattern> --exit-code(man)" result in an empty patch, we should exit with 0.

The test suite did not cover the interaction between "--exit-code" and "-w"; add one while adding a new test for "--exit-code" + "-I".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250