@matheusbsbs / matheus-bagliano / commits / da31b358fb
Fork 0

diff --check: minor fixups

There is no reason --exit-code and --check-diff must be mutually exclusive, so assign different bits to different results and allow them to be returned from the command. Introduce diff_result_code() to factor out the common code to decide final status code based on diffopt settings and use it everywhere. Update tests to match the above fix. Turning pager off when "diff --check" is used is a regression. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 13, 2007 at 23:40 UTC da31b358fb39b32622c14343ffe157a765f3948b
7 files changed +26 -28
builtin-diff-files.c
+1 -5
@@ -31,9 +31,5 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
31 if (!rev.diffopt.output_format)
32 rev.diffopt.output_format = DIFF_FORMAT_RAW;
33 result = run_diff_files_cmd(&rev, argc, argv);
34 - if (DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
35 - return DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0;
36 - if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
37 - return DIFF_OPT_TST(&rev.diffopt, CHECK_FAILED) != 0;
38 - return result;
34 + return diff_result_code(&rev.diffopt, result);
35 }
builtin-diff-index.c
+1 -5
@@ -44,9 +44,5 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
44 return -1;
45 }
46 result = run_diff_index(&rev, cached);
47 - if (DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
48 - return DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0;
49 - if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
50 - return DIFF_OPT_TST(&rev.diffopt, CHECK_FAILED) != 0;
51 - return result;
47 + return diff_result_code(&rev.diffopt, result);
48 }
builtin-diff-tree.c
+2 -4
@@ -132,8 +132,6 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
132 diff_tree_stdin(line);
133 }
134 }
135 - if (opt->diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
136 - return DIFF_OPT_TST(&opt->diffopt, CHECK_FAILED) != 0;
137 - return DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS)
138 - && DIFF_OPT_TST(&opt->diffopt, HAS_CHANGES);
135 +
136 + return diff_result_code(&opt->diffopt, 0);
137 }
builtin-diff.c
+4 -7
@@ -244,11 +244,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
244 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
245 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
246
247 - /* If the user asked for our exit code then don't start a
247 + /*
248 + * If the user asked for our exit code then don't start a
249 * pager or we would end up reporting its exit code instead.
250 */
250 - if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS) &&
251 - (!(rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)))
251 + if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
252 setup_pager();
253
254 /* Do we have --cached and not have a pending object, then
@@ -352,10 +352,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
352 else
353 result = builtin_diff_combined(&rev, argc, argv,
354 ent, ents);
355 - if (DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
356 - result = DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0;
357 - if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
358 - return DIFF_OPT_TST(&rev.diffopt, CHECK_FAILED) != 0;
355 + result = diff_result_code(&rev.diffopt, result);
356 if (1 < rev.diffopt.skip_stat_unmatch)
357 refresh_index_quietly();
358 return result;
diff.c
+14 -5
@@ -2125,12 +2125,7 @@ int diff_setup_done(struct diff_options *options)
2125 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
2126 count++;
2127 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
2128 - {
2128 count++;
2130 - if (DIFF_OPT_TST(options, QUIET) ||
2131 - DIFF_OPT_TST(options, EXIT_WITH_STATUS))
2132 - die("--check may not be used with --quiet or --exit-code");
2133 - }
2129 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
2130 count++;
2131 if (count > 1)
@@ -3180,6 +3175,20 @@ void diffcore_std(struct diff_options *options)
3175 DIFF_OPT_CLR(options, HAS_CHANGES);
3176 }
3177
3178 +int diff_result_code(struct diff_options *opt, int status)
3179 +{
3180 + int result = 0;
3181 + if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
3182 + !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
3183 + return status;
3184 + if (DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
3185 + DIFF_OPT_TST(opt, HAS_CHANGES))
3186 + result |= 01;
3187 + if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
3188 + DIFF_OPT_TST(opt, CHECK_FAILED))
3189 + result |= 02;
3190 + return result;
3191 +}
3192
3193 void diff_addremove(struct diff_options *options,
3194 int addremove, unsigned mode,
diff.h
+2
@@ -247,4 +247,6 @@ extern int run_diff_index(struct rev_info *revs, int cached);
247 extern int do_diff_cache(const unsigned char *, struct diff_options *);
248 extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
249
250 +extern int diff_result_code(struct diff_options *, int);
251 +
252 #endif /* DIFF_H */
t/t4015-diff-whitespace.sh
+2 -2
@@ -148,14 +148,14 @@ test_expect_failure 'check with space before tab in indent' '
148
149 '
150
151 -test_expect_failure '--check and --exit-code are exclusive' '
151 +test_expect_success '--check and --exit-code are not exclusive' '
152
153 git checkout x &&
154 git diff --check --exit-code
155
156 '
157
158 -test_expect_failure '--check and --quiet are exclusive' '
158 +test_expect_success '--check and --quiet are not exclusive' '
159
160 git diff --check --quiet
161