diff --stat: set the width defaults in a helper function

Extract the commonly used initialization of the --stat-width=<width>, --stat-name-width=<width> and --stat-graph-with=<width> parameters to their internal default values into a helper function, to avoid repeating the same initialization code in a few places. Add a couple of tests to additionally cover existing configuration options diff.statNameWidth=<width> and diff.statGraphWidth=<width> when used by git-merge to generate --stat outputs. This closes the gap that existed previously in the --stat tests, and reduces the chances for having any regressions introduced by this commit. While there, perform a small bunch of minor wording tweaks in the improved unit test, to improve its test-level consistency a bit. Signed-off-by: Dragan Simic <dsimic@manjaro.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Dragan Simic committed Sep 23, 2023 at 06:01 UTC 4ca7a3fd26d0c6214f46f2413c5ba8ed346fea1a
7 files changed +51 -35
builtin/diff.c
+1 -3
@@ -474,9 +474,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
474 repo_init_revisions(the_repository, &rev, prefix);
475
476 /* Set up defaults that will apply to both no-index and regular diffs. */
477 - rev.diffopt.stat_width = -1;
478 - rev.diffopt.stat_name_width = -1;
479 - rev.diffopt.stat_graph_width = -1;
477 + init_diffstat_widths(&rev.diffopt);
478 rev.diffopt.flags.allow_external = 1;
479 rev.diffopt.flags.allow_textconv = 1;
480
builtin/log.c
+2 -4
@@ -176,17 +176,15 @@ static void cmd_log_init_defaults(struct rev_info *rev)
176 if (default_follow)
177 rev->diffopt.flags.default_follow_renames = 1;
178 rev->verbose_header = 1;
179 + init_diffstat_widths(&rev->diffopt);
180 rev->diffopt.flags.recursive = 1;
180 - rev->diffopt.stat_width = -1; /* use full terminal width */
181 - rev->diffopt.stat_name_width = -1; /* respect statNameWidth config */
182 - rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */
181 + rev->diffopt.flags.allow_textconv = 1;
182 rev->abbrev_commit = default_abbrev_commit;
183 rev->show_root_diff = default_show_root;
184 rev->subject_prefix = fmt_patch_subject_prefix;
185 rev->patch_name_max = fmt_patch_name_max;
186 rev->show_signature = default_show_signature;
187 rev->encode_email_headers = default_encode_email_headers;
189 - rev->diffopt.flags.allow_textconv = 1;
188
189 if (default_date_mode)
190 parse_date_format(default_date_mode, &rev->date_mode);
builtin/merge.c
+1 -3
@@ -466,9 +466,7 @@ static void finish(struct commit *head_commit,
466 if (new_head && show_diffstat) {
467 struct diff_options opts;
468 repo_diff_setup(the_repository, &opts);
469 - opts.stat_width = -1; /* use full terminal width */
470 - opts.stat_name_width = -1; /* respect statNameWidth config */
471 - opts.stat_graph_width = -1; /* respect statGraphWidth config */
469 + init_diffstat_widths(&opts);
470 opts.output_format |=
471 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
472 opts.detect_rename = DIFF_DETECT_RENAME;
builtin/rebase.c
+1 -3
@@ -1803,9 +1803,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1803
1804 /* We want color (if set), but no pager */
1805 repo_diff_setup(the_repository, &opts);
1806 - opts.stat_width = -1; /* use full terminal width */
1807 - opts.stat_name_width = -1; /* respect statNameWidth config */
1808 - opts.stat_graph_width = -1; /* respect statGraphWidth config */
1806 + init_diffstat_widths(&opts);
1807 opts.output_format |=
1808 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1809 opts.detect_rename = DIFF_DETECT_RENAME;
diff.c
+7
@@ -6936,6 +6936,13 @@ void diff_queued_diff_prefetch(void *repository)
6936 oid_array_clear(&to_fetch);
6937 }
6938
6939 +void init_diffstat_widths(struct diff_options *options)
6940 +{
6941 + options->stat_width = -1; /* use full terminal width */
6942 + options->stat_name_width = -1; /* respect diff.statNameWidth config */
6943 + options->stat_graph_width = -1; /* respect diff.statGraphWidth config */
6944 +}
6945 +
6946 void diffcore_std(struct diff_options *options)
6947 {
6948 int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT |
diff.h
+1
@@ -573,6 +573,7 @@ int git_config_rename(const char *var, const char *value);
573
574 #define DIFF_PICKAXE_IGNORE_CASE 32
575
576 +void init_diffstat_widths(struct diff_options *);
577 void diffcore_std(struct diff_options *);
578 void diffcore_fix_diff_index(void);
579
t/t4052-stat-output.sh
+38 -22
@@ -12,7 +12,7 @@ TEST_PASSES_SANITIZE_LEAK=true
12 . ./test-lib.sh
13 . "$TEST_DIRECTORY"/lib-terminal.sh
14
15 -# 120 character name
15 +# 120-character name
16 name=aaaaaaaaaa
17 name=$name$name$name$name$name$name$name$name$name$name$name$name
18 test_expect_success 'preparation' '
@@ -58,7 +58,7 @@ while read verb expect cmd args
58 do
59 # No width limit applied when statNameWidth is ignored
60 case "$expect" in expect72|expect.6030)
61 - test_expect_success "$cmd $verb statNameWidth config with long name" '
61 + test_expect_success "$cmd $verb diff.statNameWidth with long name" '
62 git -c diff.statNameWidth=30 $cmd $args >output &&
63 grep " | " output >actual &&
64 test_cmp $expect actual
@@ -66,7 +66,7 @@ do
66 esac
67 # Maximum width limit still applied when statNameWidth is ignored
68 case "$expect" in expect.60|expect.6030)
69 - test_expect_success "$cmd --stat=width $verb statNameWidth config with long name" '
69 + test_expect_success "$cmd --stat=width $verb diff.statNameWidth with long name" '
70 git -c diff.statNameWidth=30 $cmd $args --stat=60 >output &&
71 grep " | " output >actual &&
72 test_cmp $expect actual
@@ -111,7 +111,7 @@ do
111 test_cmp $expect.6030 actual
112 '
113
114 - test_expect_success "$cmd --stat-name-width with long name" '
114 + test_expect_success "$cmd --stat-name-width=width with long name" '
115 git $cmd $args --stat-name-width=30 >output &&
116 grep " | " output >actual &&
117 test_cmp $expect.6030 actual
@@ -123,7 +123,7 @@ expect show --stat
123 expect log -1 --stat
124 EOF
125
126 -test_expect_success 'preparation for big change tests' '
126 +test_expect_success 'preparation for big-change tests' '
127 >abcd &&
128 git add abcd &&
129 git commit -m message &&
@@ -139,7 +139,7 @@ cat >expect72 <<'EOF'
139 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
140 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
141 EOF
142 -test_expect_success "format-patch --cover-letter ignores COLUMNS (big change)" '
142 +test_expect_success "format-patch --cover-letter ignores COLUMNS with big change" '
143 COLUMNS=200 git format-patch -1 --stdout --cover-letter >output &&
144 grep " | " output >actual &&
145 test_cmp expect72 actual
@@ -159,7 +159,7 @@ cat >expect200-graph <<'EOF'
159 EOF
160 while read verb expect cmd args
161 do
162 - test_expect_success "$cmd $verb COLUMNS (big change)" '
162 + test_expect_success "$cmd $verb COLUMNS with big change" '
163 COLUMNS=200 git $cmd $args >output &&
164 grep " | " output >actual &&
165 test_cmp "$expect" actual
@@ -167,7 +167,7 @@ do
167
168 case "$cmd" in diff|show) continue;; esac
169
170 - test_expect_success "$cmd --graph $verb COLUMNS (big change)" '
170 + test_expect_success "$cmd --graph $verb COLUMNS with big change" '
171 COLUMNS=200 git $cmd $args --graph >output &&
172 grep " | " output >actual &&
173 test_cmp "$expect-graph" actual
@@ -187,7 +187,7 @@ cat >expect40-graph <<'EOF'
187 EOF
188 while read verb expect cmd args
189 do
190 - test_expect_success "$cmd $verb not enough COLUMNS (big change)" '
190 + test_expect_success "$cmd $verb not enough COLUMNS with big change" '
191 COLUMNS=40 git $cmd $args >output &&
192 grep " | " output >actual &&
193 test_cmp "$expect" actual
@@ -195,7 +195,7 @@ do
195
196 case "$cmd" in diff|show) continue;; esac
197
198 - test_expect_success "$cmd --graph $verb not enough COLUMNS (big change)" '
198 + test_expect_success "$cmd --graph $verb not enough COLUMNS with big change" '
199 COLUMNS=40 git $cmd $args --graph >output &&
200 grep " | " output >actual &&
201 test_cmp "$expect-graph" actual
@@ -215,7 +215,7 @@ cat >expect40-graph <<'EOF'
215 EOF
216 while read verb expect cmd args
217 do
218 - test_expect_success "$cmd $verb statGraphWidth config" '
218 + test_expect_success "$cmd $verb diff.statGraphWidth" '
219 git -c diff.statGraphWidth=26 $cmd $args >output &&
220 grep " | " output >actual &&
221 test_cmp "$expect" actual
@@ -223,7 +223,7 @@ do
223
224 case "$cmd" in diff|show) continue;; esac
225
226 - test_expect_success "$cmd --graph $verb statGraphWidth config" '
226 + test_expect_success "$cmd --graph $verb diff.statGraphWidth" '
227 git -c diff.statGraphWidth=26 $cmd $args --graph >output &&
228 grep " | " output >actual &&
229 test_cmp "$expect-graph" actual
@@ -255,7 +255,7 @@ do
255 test_cmp expect actual
256 '
257
258 - test_expect_success "$cmd --stat-graph-width with big change" '
258 + test_expect_success "$cmd --stat-graph-width=width with big change" '
259 git $cmd $args --stat-graph-width=26 >output &&
260 grep " | " output >actual &&
261 test_cmp expect actual
@@ -269,7 +269,7 @@ do
269 test_cmp expect-graph actual
270 '
271
272 - test_expect_success "$cmd --stat-graph-width --graph with big change" '
272 + test_expect_success "$cmd --stat-graph-width=width --graph with big change" '
273 git $cmd $args --stat-graph-width=26 --graph >output &&
274 grep " | " output >actual &&
275 test_cmp expect-graph actual
@@ -281,7 +281,7 @@ show --stat
281 log -1 --stat
282 EOF
283
284 -test_expect_success 'preparation for long filename tests' '
284 +test_expect_success 'preparation for long-name tests' '
285 cp abcd aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
286 git add aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
287 git commit -m message
@@ -329,7 +329,7 @@ cat >expect200-graph <<'EOF'
329 EOF
330 while read verb expect cmd args
331 do
332 - test_expect_success "$cmd $verb COLUMNS (long filename)" '
332 + test_expect_success "$cmd $verb COLUMNS with long name" '
333 COLUMNS=200 git $cmd $args >output &&
334 grep " | " output >actual &&
335 test_cmp "$expect" actual
@@ -337,7 +337,7 @@ do
337
338 case "$cmd" in diff|show) continue;; esac
339
340 - test_expect_success "$cmd --graph $verb COLUMNS (long filename)" '
340 + test_expect_success "$cmd --graph $verb COLUMNS with long name" '
341 COLUMNS=200 git $cmd $args --graph >output &&
342 grep " | " output >actual &&
343 test_cmp "$expect-graph" actual
@@ -358,7 +358,7 @@ EOF
358 while read verb expect cmd args
359 do
360 test_expect_success COLUMNS_CAN_BE_1 \
361 - "$cmd $verb prefix greater than COLUMNS (big change)" '
361 + "$cmd $verb prefix greater than COLUMNS with big change" '
362 COLUMNS=1 git $cmd $args >output &&
363 grep " | " output >actual &&
364 test_cmp "$expect" actual
@@ -367,7 +367,7 @@ do
367 case "$cmd" in diff|show) continue;; esac
368
369 test_expect_success COLUMNS_CAN_BE_1 \
370 - "$cmd --graph $verb prefix greater than COLUMNS (big change)" '
370 + "$cmd --graph $verb prefix greater than COLUMNS with big change" '
371 COLUMNS=1 git $cmd $args --graph >output &&
372 grep " | " output >actual &&
373 test_cmp "$expect-graph" actual
@@ -382,8 +382,14 @@ EOF
382 cat >expect <<'EOF'
383 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
384 EOF
385 -test_expect_success 'merge --stat respects COLUMNS (big change)' '
386 - git checkout -b branch HEAD^^ &&
385 +test_expect_success 'merge --stat respects diff.statGraphWidth with big change' '
386 + git checkout -b branch1 HEAD^^ &&
387 + git -c diff.statGraphWidth=26 merge --stat --no-ff main^ >output &&
388 + grep " | " output >actual &&
389 + test_cmp expect40 actual
390 +'
391 +test_expect_success 'merge --stat respects COLUMNS with big change' '
392 + git checkout -b branch2 HEAD^^ &&
393 COLUMNS=100 git merge --stat --no-ff main^ >output &&
394 grep " | " output >actual &&
395 test_cmp expect actual
@@ -392,7 +398,17 @@ test_expect_success 'merge --stat respects COLUMNS (big change)' '
398 cat >expect <<'EOF'
399 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++
400 EOF
395 -test_expect_success 'merge --stat respects COLUMNS (long filename)' '
401 +cat >expect.30 <<'EOF'
402 + ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++++++++++++++++++++++++++++++
403 +EOF
404 +test_expect_success 'merge --stat respects diff.statNameWidth with long name' '
405 + git switch branch1 &&
406 + git -c diff.statNameWidth=30 merge --stat --no-ff main >output &&
407 + grep " | " output >actual &&
408 + test_cmp expect.30 actual
409 +'
410 +test_expect_success 'merge --stat respects COLUMNS with long name' '
411 + git switch branch2 &&
412 COLUMNS=100 git merge --stat --no-ff main >output &&
413 grep " | " output >actual &&
414 test_cmp expect actual