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 <Matthieu.Moy@imag.fr> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Matthieu Moy committed
Jul 16, 2013 at 10:05 UTC
71482d389d0b214d2cfc2adf788ba1011a7dcb18
2 files changed
+35
-11
diff.c
+17
-11
@@ -3497,6 +3497,11 @@ static int parse_submodule_opt(struct diff_options *options, const char *value)
3497
return 1;
3498
}
3499
3500
+static void enable_patch_output(int *fmt) {
3501
+ *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
3502
+ *fmt |= DIFF_FORMAT_PATCH;
3503
+}
3504
+
3505
int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3506
{
3507
const char *arg = av[0];
@@ -3504,15 +3509,15 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3509
int argcount;
3510
3511
/* 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);
3515
else if (!strcmp(arg, "--raw"))
3516
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"))
3521
options->output_format |= DIFF_FORMAT_NUMSTAT;
3522
else if (!strcmp(arg, "--shortstat"))
3523
options->output_format |= DIFF_FORMAT_SHORTSTAT;
@@ -3534,9 +3539,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3539
options->output_format |= DIFF_FORMAT_CHECKDIFF;
3540
else if (!strcmp(arg, "--summary"))
3541
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"))
3546
options->output_format |= DIFF_FORMAT_NAME;
3547
else if (!strcmp(arg, "--name-status"))
3548
options->output_format |= DIFF_FORMAT_NAME_STATUS;
@@ -3611,7 +3617,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3617
3618
/* flags options */
3619
else if (!strcmp(arg, "--binary")) {
3614
- options->output_format |= DIFF_FORMAT_PATCH;
3620
+ enable_patch_output(&options->output_format);
3621
DIFF_OPT_SET(options, BINARY);
3622
}
3623
else if (!strcmp(arg, "--full-index"))
t/t4000-diff-format.sh
+18
@@ -71,4 +71,22 @@ test_expect_success 'git diff-files --no-patch as synonym for -s' '
71
test_must_be_empty err
72
'
73
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
+
92
test_done