Skip to content

Commit da31b35

Browse files
committed
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>
1 parent 62c6489 commit da31b35

File tree

7 files changed

+26
-28
lines changed

7 files changed

+26
-28
lines changed

builtin-diff-files.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,5 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
3131
if (!rev.diffopt.output_format)
3232
rev.diffopt.output_format = DIFF_FORMAT_RAW;
3333
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);
3935
}

builtin-diff-index.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,5 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
4444
return -1;
4545
}
4646
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);
5248
}

builtin-diff-tree.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
132132
diff_tree_stdin(line);
133133
}
134134
}
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);
139137
}

builtin-diff.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
244244
DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
245245
DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
246246

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
248249
* pager or we would end up reporting its exit code instead.
249250
*/
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))
252252
setup_pager();
253253

254254
/* 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)
352352
else
353353
result = builtin_diff_combined(&rev, argc, argv,
354354
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);
359356
if (1 < rev.diffopt.skip_stat_unmatch)
360357
refresh_index_quietly();
361358
return result;

diff.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,12 +2125,7 @@ int diff_setup_done(struct diff_options *options)
21252125
if (options->output_format & DIFF_FORMAT_NAME_STATUS)
21262126
count++;
21272127
if (options->output_format & DIFF_FORMAT_CHECKDIFF)
2128-
{
21292128
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-
}
21342129
if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
21352130
count++;
21362131
if (count > 1)
@@ -3180,6 +3175,20 @@ void diffcore_std(struct diff_options *options)
31803175
DIFF_OPT_CLR(options, HAS_CHANGES);
31813176
}
31823177

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+
}
31833192

31843193
void diff_addremove(struct diff_options *options,
31853194
int addremove, unsigned mode,

diff.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,6 @@ extern int run_diff_index(struct rev_info *revs, int cached);
247247
extern int do_diff_cache(const unsigned char *, struct diff_options *);
248248
extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
249249

250+
extern int diff_result_code(struct diff_options *, int);
251+
250252
#endif /* DIFF_H */

t/t4015-diff-whitespace.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ test_expect_failure 'check with space before tab in indent' '
148148
149149
'
150150

151-
test_expect_failure '--check and --exit-code are exclusive' '
151+
test_expect_success '--check and --exit-code are not exclusive' '
152152
153153
git checkout x &&
154154
git diff --check --exit-code
155155
156156
'
157157

158-
test_expect_failure '--check and --quiet are exclusive' '
158+
test_expect_success '--check and --quiet are not exclusive' '
159159
160160
git diff --check --quiet
161161

0 commit comments

Comments
 (0)