Skip to content

Commit

Permalink
diff: allow --patch & cie to override -s/--no-patch
Browse files Browse the repository at this point in the history
All options that trigger a patch output now override --no-patch.

The case of --binary deserves extra attention: the name may suggest that
it turns a normal patch into a binary patch, but it actually already
enables patch output when normally disabled (e.g. "git log --binary"
displays a patch), hence it makes sense for "git show --no-patch
--binary" to display the binary patch.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
moy authored and gitster committed Jul 18, 2013
1 parent d09cd15 commit 71482d3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
28 changes: 17 additions & 11 deletions diff.c
Expand Up @@ -3497,22 +3497,27 @@ static int parse_submodule_opt(struct diff_options *options, const char *value)
return 1;
}

static void enable_patch_output(int *fmt) {
*fmt &= ~DIFF_FORMAT_NO_OUTPUT;
*fmt |= DIFF_FORMAT_PATCH;
}

int diff_opt_parse(struct diff_options *options, const char **av, int ac)
{
const char *arg = av[0];
const char *optarg;
int argcount;

/* Output format options */
if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch"))
options->output_format |= DIFF_FORMAT_PATCH;
else if (opt_arg(arg, 'U', "unified", &options->context))
options->output_format |= DIFF_FORMAT_PATCH;
if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
|| opt_arg(arg, 'U', "unified", &options->context))
enable_patch_output(&options->output_format);
else if (!strcmp(arg, "--raw"))
options->output_format |= DIFF_FORMAT_RAW;
else if (!strcmp(arg, "--patch-with-raw"))
options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
else if (!strcmp(arg, "--numstat"))
else if (!strcmp(arg, "--patch-with-raw")) {
enable_patch_output(&options->output_format);
options->output_format |= DIFF_FORMAT_RAW;
} else if (!strcmp(arg, "--numstat"))
options->output_format |= DIFF_FORMAT_NUMSTAT;
else if (!strcmp(arg, "--shortstat"))
options->output_format |= DIFF_FORMAT_SHORTSTAT;
Expand All @@ -3534,9 +3539,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
options->output_format |= DIFF_FORMAT_CHECKDIFF;
else if (!strcmp(arg, "--summary"))
options->output_format |= DIFF_FORMAT_SUMMARY;
else if (!strcmp(arg, "--patch-with-stat"))
options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
else if (!strcmp(arg, "--name-only"))
else if (!strcmp(arg, "--patch-with-stat")) {
enable_patch_output(&options->output_format);
options->output_format |= DIFF_FORMAT_DIFFSTAT;
} else if (!strcmp(arg, "--name-only"))
options->output_format |= DIFF_FORMAT_NAME;
else if (!strcmp(arg, "--name-status"))
options->output_format |= DIFF_FORMAT_NAME_STATUS;
Expand Down Expand Up @@ -3611,7 +3617,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)

/* flags options */
else if (!strcmp(arg, "--binary")) {
options->output_format |= DIFF_FORMAT_PATCH;
enable_patch_output(&options->output_format);
DIFF_OPT_SET(options, BINARY);
}
else if (!strcmp(arg, "--full-index"))
Expand Down
18 changes: 18 additions & 0 deletions t/t4000-diff-format.sh
Expand Up @@ -71,4 +71,22 @@ test_expect_success 'git diff-files --no-patch as synonym for -s' '
test_must_be_empty err
'

test_expect_success 'git diff-files --no-patch --patch shows the patch' '
git diff-files --no-patch --patch >actual &&
compare_diff_patch expected actual
'

test_expect_success 'git diff-files --no-patch --patch-with-raw shows the patch and raw data' '
git diff-files --no-patch --patch-with-raw >actual &&
grep -q "^:100644 100755 .* 0000000000000000000000000000000000000000 M path0\$" actual &&
tail -n +4 actual >actual-patch &&
compare_diff_patch expected actual-patch
'

test_expect_success 'git diff-files --patch --no-patch does not show the patch' '
git diff-files --patch --no-patch >actual 2>err &&
test_must_be_empty actual &&
test_must_be_empty err
'

test_done

0 comments on commit 71482d3

Please sign in to comment.