Skip to content

Commit 6901ffe

Browse files
committed
Merge branch 'jc/diff-s-with-other-options'
The "-s" (silent, squelch) option of the "diff" family of commands did not interact with other options that specify the output format well. This has been cleaned up so that it will clear all the formatting options given before. * jc/diff-s-with-other-options: diff: fix interaction between the "-s" option and other options
2 parents 6d2a88c + 9d484b9 commit 6901ffe

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

Documentation/diff-options.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ endif::git-diff[]
3535

3636
-s::
3737
--no-patch::
38-
Suppress diff output. Useful for commands like `git show` that
39-
show the patch by default, or to cancel the effect of `--patch`.
38+
Suppress all output from the diff machinery. Useful for
39+
commands like `git show` that show the patch by default to
40+
squelch their output, or to cancel the effect of options like
41+
`--patch`, `--stat` earlier on the command line in an alias.
42+
4043
endif::git-format-patch[]
4144

4245
ifdef::git-log[]

diff.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4936,6 +4936,7 @@ static int diff_opt_stat(const struct option *opt, const char *value, int unset)
49364936
} else
49374937
BUG("%s should not get here", opt->long_name);
49384938

4939+
options->output_format &= ~DIFF_FORMAT_NO_OUTPUT;
49394940
options->output_format |= DIFF_FORMAT_DIFFSTAT;
49404941
options->stat_name_width = name_width;
49414942
options->stat_graph_width = graph_width;
@@ -4955,6 +4956,7 @@ static int parse_dirstat_opt(struct diff_options *options, const char *params)
49554956
* The caller knows a dirstat-related option is given from the command
49564957
* line; allow it to say "return this_function();"
49574958
*/
4959+
options->output_format &= ~DIFF_FORMAT_NO_OUTPUT;
49584960
options->output_format |= DIFF_FORMAT_DIRSTAT;
49594961
return 1;
49604962
}
@@ -5154,6 +5156,7 @@ static int diff_opt_compact_summary(const struct option *opt,
51545156
options->flags.stat_with_summary = 0;
51555157
} else {
51565158
options->flags.stat_with_summary = 1;
5159+
options->output_format &= ~DIFF_FORMAT_NO_OUTPUT;
51575160
options->output_format |= DIFF_FORMAT_DIFFSTAT;
51585161
}
51595162
return 0;
@@ -5499,9 +5502,8 @@ struct option *add_diff_options(const struct option *opts,
54995502
OPT_BITOP('p', "patch", &options->output_format,
55005503
N_("generate patch"),
55015504
DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
5502-
OPT_BIT_F('s', "no-patch", &options->output_format,
5503-
N_("suppress diff output"),
5504-
DIFF_FORMAT_NO_OUTPUT, PARSE_OPT_NONEG),
5505+
OPT_SET_INT('s', "no-patch", &options->output_format,
5506+
N_("suppress diff output"), DIFF_FORMAT_NO_OUTPUT),
55055507
OPT_BITOP('u', NULL, &options->output_format,
55065508
N_("generate patch"),
55075509
DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
@@ -5510,9 +5512,9 @@ struct option *add_diff_options(const struct option *opts,
55105512
PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified),
55115513
OPT_BOOL('W', "function-context", &options->flags.funccontext,
55125514
N_("generate diffs with <n> lines context")),
5513-
OPT_BIT_F(0, "raw", &options->output_format,
5515+
OPT_BITOP(0, "raw", &options->output_format,
55145516
N_("generate the diff in raw format"),
5515-
DIFF_FORMAT_RAW, PARSE_OPT_NONEG),
5517+
DIFF_FORMAT_RAW, DIFF_FORMAT_NO_OUTPUT),
55165518
OPT_BITOP(0, "patch-with-raw", &options->output_format,
55175519
N_("synonym for '-p --raw'"),
55185520
DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW,
@@ -5521,12 +5523,12 @@ struct option *add_diff_options(const struct option *opts,
55215523
N_("synonym for '-p --stat'"),
55225524
DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT,
55235525
DIFF_FORMAT_NO_OUTPUT),
5524-
OPT_BIT_F(0, "numstat", &options->output_format,
5526+
OPT_BITOP(0, "numstat", &options->output_format,
55255527
N_("machine friendly --stat"),
5526-
DIFF_FORMAT_NUMSTAT, PARSE_OPT_NONEG),
5527-
OPT_BIT_F(0, "shortstat", &options->output_format,
5528+
DIFF_FORMAT_NUMSTAT, DIFF_FORMAT_NO_OUTPUT),
5529+
OPT_BITOP(0, "shortstat", &options->output_format,
55285530
N_("output only the last line of --stat"),
5529-
DIFF_FORMAT_SHORTSTAT, PARSE_OPT_NONEG),
5531+
DIFF_FORMAT_SHORTSTAT, DIFF_FORMAT_NO_OUTPUT),
55305532
OPT_CALLBACK_F('X', "dirstat", options, N_("<param1,param2>..."),
55315533
N_("output the distribution of relative amount of changes for each sub-directory"),
55325534
PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
@@ -5542,9 +5544,9 @@ struct option *add_diff_options(const struct option *opts,
55425544
OPT_BIT_F(0, "check", &options->output_format,
55435545
N_("warn if changes introduce conflict markers or whitespace errors"),
55445546
DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG),
5545-
OPT_BIT_F(0, "summary", &options->output_format,
5547+
OPT_BITOP(0, "summary", &options->output_format,
55465548
N_("condensed summary such as creations, renames and mode changes"),
5547-
DIFF_FORMAT_SUMMARY, PARSE_OPT_NONEG),
5549+
DIFF_FORMAT_SUMMARY, DIFF_FORMAT_NO_OUTPUT),
55485550
OPT_BIT_F(0, "name-only", &options->output_format,
55495551
N_("show only names of changed files"),
55505552
DIFF_FORMAT_NAME, PARSE_OPT_NONEG),

t/t4000-diff-format.sh

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
test_description='Test built-in diff output engine.
77
8+
We happen to know that all diff plumbing and diff Porcelain share the
9+
same command line parser, so testing one should be sufficient; pick
10+
diff-files as a representative.
811
'
912

1013
TEST_PASSES_SANITIZE_LEAK=true
@@ -16,9 +19,11 @@ Line 2
1619
line 3'
1720
cat path0 >path1
1821
chmod +x path1
22+
mkdir path2
23+
>path2/path3
1924

2025
test_expect_success 'update-index --add two files with and without +x.' '
21-
git update-index --add path0 path1
26+
git update-index --add path0 path1 path2/path3
2227
'
2328

2429
mv path0 path0-
@@ -91,4 +96,31 @@ test_expect_success 'git diff-files --patch --no-patch does not show the patch'
9196
test_must_be_empty err
9297
'
9398

99+
100+
# Smudge path2/path3 so that dirstat has something to show
101+
date >path2/path3
102+
103+
for format in stat raw numstat shortstat summary \
104+
dirstat cumulative dirstat-by-file \
105+
patch-with-raw patch-with-stat compact-summary
106+
do
107+
test_expect_success "--no-patch in 'git diff-files --no-patch --$format' is a no-op" '
108+
git diff-files --no-patch "--$format" >actual &&
109+
git diff-files "--$format" >expect &&
110+
test_cmp expect actual
111+
'
112+
113+
test_expect_success "--no-patch clears all previous ones" '
114+
git diff-files --$format -s -p >actual &&
115+
git diff-files -p >expect &&
116+
test_cmp expect actual
117+
'
118+
119+
test_expect_success "--no-patch in 'git diff --no-patch --$format' is a no-op" '
120+
git diff --no-patch "--$format" >actual &&
121+
git diff "--$format" >expect &&
122+
test_cmp expect actual
123+
'
124+
done
125+
94126
test_done

0 commit comments

Comments
 (0)