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>
This commit is contained in:
Junio C Hamano 2007-12-13 23:40:27 -08:00
parent 62c64895cf
commit da31b358fb
7 changed files with 26 additions and 28 deletions

View File

@ -31,9 +31,5 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
if (!rev.diffopt.output_format) if (!rev.diffopt.output_format)
rev.diffopt.output_format = DIFF_FORMAT_RAW; rev.diffopt.output_format = DIFF_FORMAT_RAW;
result = run_diff_files_cmd(&rev, argc, argv); result = run_diff_files_cmd(&rev, argc, argv);
if (DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS)) return diff_result_code(&rev.diffopt, result);
return DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0;
if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
return DIFF_OPT_TST(&rev.diffopt, CHECK_FAILED) != 0;
return result;
} }

View File

@ -44,9 +44,5 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
return -1; return -1;
} }
result = run_diff_index(&rev, cached); result = run_diff_index(&rev, cached);
if (DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS)) return diff_result_code(&rev.diffopt, result);
return DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0;
if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
return DIFF_OPT_TST(&rev.diffopt, CHECK_FAILED) != 0;
return result;
} }

View File

@ -132,8 +132,6 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
diff_tree_stdin(line); diff_tree_stdin(line);
} }
} }
if (opt->diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
return DIFF_OPT_TST(&opt->diffopt, CHECK_FAILED) != 0; return diff_result_code(&opt->diffopt, 0);
return DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS)
&& DIFF_OPT_TST(&opt->diffopt, HAS_CHANGES);
} }

View File

@ -244,11 +244,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL); DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
DIFF_OPT_SET(&rev.diffopt, RECURSIVE); DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
/* If the user asked for our exit code then don't start a /*
* If the user asked for our exit code then don't start a
* pager or we would end up reporting its exit code instead. * pager or we would end up reporting its exit code instead.
*/ */
if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS) && if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
(!(rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)))
setup_pager(); setup_pager();
/* Do we have --cached and not have a pending object, then /* 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)
else else
result = builtin_diff_combined(&rev, argc, argv, result = builtin_diff_combined(&rev, argc, argv,
ent, ents); ent, ents);
if (DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS)) result = diff_result_code(&rev.diffopt, result);
result = DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0;
if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
return DIFF_OPT_TST(&rev.diffopt, CHECK_FAILED) != 0;
if (1 < rev.diffopt.skip_stat_unmatch) if (1 < rev.diffopt.skip_stat_unmatch)
refresh_index_quietly(); refresh_index_quietly();
return result; return result;

19
diff.c
View File

@ -2125,12 +2125,7 @@ int diff_setup_done(struct diff_options *options)
if (options->output_format & DIFF_FORMAT_NAME_STATUS) if (options->output_format & DIFF_FORMAT_NAME_STATUS)
count++; count++;
if (options->output_format & DIFF_FORMAT_CHECKDIFF) if (options->output_format & DIFF_FORMAT_CHECKDIFF)
{
count++; count++;
if (DIFF_OPT_TST(options, QUIET) ||
DIFF_OPT_TST(options, EXIT_WITH_STATUS))
die("--check may not be used with --quiet or --exit-code");
}
if (options->output_format & DIFF_FORMAT_NO_OUTPUT) if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
count++; count++;
if (count > 1) if (count > 1)
@ -3180,6 +3175,20 @@ void diffcore_std(struct diff_options *options)
DIFF_OPT_CLR(options, HAS_CHANGES); DIFF_OPT_CLR(options, HAS_CHANGES);
} }
int diff_result_code(struct diff_options *opt, int status)
{
int result = 0;
if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
!(opt->output_format & DIFF_FORMAT_CHECKDIFF))
return status;
if (DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
DIFF_OPT_TST(opt, HAS_CHANGES))
result |= 01;
if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
DIFF_OPT_TST(opt, CHECK_FAILED))
result |= 02;
return result;
}
void diff_addremove(struct diff_options *options, void diff_addremove(struct diff_options *options,
int addremove, unsigned mode, int addremove, unsigned mode,

2
diff.h
View File

@ -247,4 +247,6 @@ extern int run_diff_index(struct rev_info *revs, int cached);
extern int do_diff_cache(const unsigned char *, struct diff_options *); extern int do_diff_cache(const unsigned char *, struct diff_options *);
extern int diff_flush_patch_id(struct diff_options *, unsigned char *); extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
extern int diff_result_code(struct diff_options *, int);
#endif /* DIFF_H */ #endif /* DIFF_H */

View File

@ -148,14 +148,14 @@ test_expect_failure 'check with space before tab in indent' '
' '
test_expect_failure '--check and --exit-code are exclusive' ' test_expect_success '--check and --exit-code are not exclusive' '
git checkout x && git checkout x &&
git diff --check --exit-code git diff --check --exit-code
' '
test_expect_failure '--check and --quiet are exclusive' ' test_expect_success '--check and --quiet are not exclusive' '
git diff --check --quiet git diff --check --quiet