Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 71482d3

Browse files
moygitster
authored andcommitted
diff: allow --patch & cie to override -s/--no-patch
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 <[email protected]> Reviewed-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d09cd15 commit 71482d3

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

diff.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3497,22 +3497,27 @@ static int parse_submodule_opt(struct diff_options *options, const char *value)
34973497
return 1;
34983498
}
34993499

3500+
static void enable_patch_output(int *fmt) {
3501+
*fmt &= ~DIFF_FORMAT_NO_OUTPUT;
3502+
*fmt |= DIFF_FORMAT_PATCH;
3503+
}
3504+
35003505
int diff_opt_parse(struct diff_options *options, const char **av, int ac)
35013506
{
35023507
const char *arg = av[0];
35033508
const char *optarg;
35043509
int argcount;
35053510

35063511
/* Output format options */
3507-
if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch"))
3508-
options->output_format |= DIFF_FORMAT_PATCH;
3509-
else if (opt_arg(arg, 'U', "unified", &options->context))
3510-
options->output_format |= DIFF_FORMAT_PATCH;
3512+
if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
3513+
|| opt_arg(arg, 'U', "unified", &options->context))
3514+
enable_patch_output(&options->output_format);
35113515
else if (!strcmp(arg, "--raw"))
35123516
options->output_format |= DIFF_FORMAT_RAW;
3513-
else if (!strcmp(arg, "--patch-with-raw"))
3514-
options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
3515-
else if (!strcmp(arg, "--numstat"))
3517+
else if (!strcmp(arg, "--patch-with-raw")) {
3518+
enable_patch_output(&options->output_format);
3519+
options->output_format |= DIFF_FORMAT_RAW;
3520+
} else if (!strcmp(arg, "--numstat"))
35163521
options->output_format |= DIFF_FORMAT_NUMSTAT;
35173522
else if (!strcmp(arg, "--shortstat"))
35183523
options->output_format |= DIFF_FORMAT_SHORTSTAT;
@@ -3534,9 +3539,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
35343539
options->output_format |= DIFF_FORMAT_CHECKDIFF;
35353540
else if (!strcmp(arg, "--summary"))
35363541
options->output_format |= DIFF_FORMAT_SUMMARY;
3537-
else if (!strcmp(arg, "--patch-with-stat"))
3538-
options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
3539-
else if (!strcmp(arg, "--name-only"))
3542+
else if (!strcmp(arg, "--patch-with-stat")) {
3543+
enable_patch_output(&options->output_format);
3544+
options->output_format |= DIFF_FORMAT_DIFFSTAT;
3545+
} else if (!strcmp(arg, "--name-only"))
35403546
options->output_format |= DIFF_FORMAT_NAME;
35413547
else if (!strcmp(arg, "--name-status"))
35423548
options->output_format |= DIFF_FORMAT_NAME_STATUS;
@@ -3611,7 +3617,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
36113617

36123618
/* flags options */
36133619
else if (!strcmp(arg, "--binary")) {
3614-
options->output_format |= DIFF_FORMAT_PATCH;
3620+
enable_patch_output(&options->output_format);
36153621
DIFF_OPT_SET(options, BINARY);
36163622
}
36173623
else if (!strcmp(arg, "--full-index"))

t/t4000-diff-format.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,22 @@ test_expect_success 'git diff-files --no-patch as synonym for -s' '
7171
test_must_be_empty err
7272
'
7373

74+
test_expect_success 'git diff-files --no-patch --patch shows the patch' '
75+
git diff-files --no-patch --patch >actual &&
76+
compare_diff_patch expected actual
77+
'
78+
79+
test_expect_success 'git diff-files --no-patch --patch-with-raw shows the patch and raw data' '
80+
git diff-files --no-patch --patch-with-raw >actual &&
81+
grep -q "^:100644 100755 .* 0000000000000000000000000000000000000000 M path0\$" actual &&
82+
tail -n +4 actual >actual-patch &&
83+
compare_diff_patch expected actual-patch
84+
'
85+
86+
test_expect_success 'git diff-files --patch --no-patch does not show the patch' '
87+
git diff-files --patch --no-patch >actual 2>err &&
88+
test_must_be_empty actual &&
89+
test_must_be_empty err
90+
'
91+
7492
test_done

0 commit comments

Comments
 (0)