diff-no-index: exit(1) if 'diff --quiet <repo file> <external file>' finds changes

When running 'git diff --quiet <file1> <file2>', if file1 or file2 is outside the repository, it will exit(0) even if the files differ. It should exit(1) when they differ. This happens because 'diff_no_index' looks at the 'found_changes' member from 'diff_options' to determine if changes were made. This is the wrong thing to do, since it is only set if xdiff is actually run and it finds a change (the diff machinery will optimize out the xdiff call when it is not necessary) and in that case HAS_CHANGED flag needs to be taken into account. Use diff_result_code() that knows all these details for the correct exit value instead. Signed-off-by: Tim Henigan <tim.henigan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tim Henigan committed Jun 21, 2012 at 14:09 UTC 304970dd5d9e542e70eca98932c1e9f26770678e
2 files changed +73 -2
diff-no-index.c
+1 -1
@@ -256,5 +256,5 @@ void diff_no_index(struct rev_info *revs,
256 * The return code for --no-index imitates diff(1):
257 * 0 = no changes, 1 = changes, else error
258 */
259 - exit(revs->diffopt.found_changes);
259 + exit(diff_result_code(&revs->diffopt, 0));
260 }
t/t4035-diff-quiet.sh
+72 -1
@@ -10,7 +10,22 @@ test_expect_success 'setup' '
10 git commit -m first &&
11 echo 2 >b &&
12 git add . &&
13 - git commit -a -m second
13 + git commit -a -m second &&
14 + mkdir -p test-outside/repo && (
15 + cd test-outside/repo &&
16 + git init &&
17 + echo "1 1" >a &&
18 + git add . &&
19 + git commit -m 1
20 + ) &&
21 + mkdir -p test-outside/non/git && (
22 + cd test-outside/non/git &&
23 + echo "1 1" >a &&
24 + echo "1 1" >matching-file &&
25 + echo "1 1 " >trailing-space &&
26 + echo "1 1" >extra-space &&
27 + echo "2" >never-match
28 + )
29 '
30
31 test_expect_success 'git diff-tree HEAD^ HEAD' '
@@ -77,4 +92,60 @@ test_expect_success 'git diff-index --cached HEAD' '
92 }
93 '
94
95 +test_expect_success 'git diff, one file outside repo' '
96 + (
97 + cd test-outside/repo &&
98 + test_expect_code 0 git diff --quiet a ../non/git/matching-file &&
99 + test_expect_code 1 git diff --quiet a ../non/git/extra-space
100 + )
101 +'
102 +
103 +test_expect_success 'git diff, both files outside repo' '
104 + (
105 + GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
106 + export GIT_CEILING_DIRECTORIES &&
107 + cd test-outside/non/git &&
108 + test_expect_code 0 git diff --quiet a matching-file &&
109 + test_expect_code 1 git diff --quiet a extra-space
110 + )
111 +'
112 +
113 +test_expect_success 'git diff --ignore-space-at-eol, one file outside repo' '
114 + (
115 + cd test-outside/repo &&
116 + test_expect_code 0 git diff --quiet --ignore-space-at-eol a ../non/git/trailing-space &&
117 + test_expect_code 1 git diff --quiet --ignore-space-at-eol a ../non/git/extra-space
118 + )
119 +'
120 +
121 +test_expect_success 'git diff --ignore-space-at-eol, both files outside repo' '
122 + (
123 + GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
124 + export GIT_CEILING_DIRECTORIES &&
125 + cd test-outside/non/git &&
126 + test_expect_code 0 git diff --quiet --ignore-space-at-eol a trailing-space &&
127 + test_expect_code 1 git diff --quiet --ignore-space-at-eol a extra-space
128 + )
129 +'
130 +
131 +test_expect_success 'git diff --ignore-all-space, one file outside repo' '
132 + (
133 + cd test-outside/repo &&
134 + test_expect_code 0 git diff --quiet --ignore-all-space a ../non/git/trailing-space &&
135 + test_expect_code 0 git diff --quiet --ignore-all-space a ../non/git/extra-space &&
136 + test_expect_code 1 git diff --quiet --ignore-all-space a ../non/git/never-match
137 + )
138 +'
139 +
140 +test_expect_success 'git diff --ignore-all-space, both files outside repo' '
141 + (
142 + GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
143 + export GIT_CEILING_DIRECTORIES &&
144 + cd test-outside/non/git &&
145 + test_expect_code 0 git diff --quiet --ignore-all-space a trailing-space &&
146 + test_expect_code 0 git diff --quiet --ignore-all-space a extra-space &&
147 + test_expect_code 1 git diff --quiet --ignore-all-space a never-match
148 + )
149 +'
150 +
151 test_done