Skip to content

Commit 0b493d2

Browse files
committed
Merge branch 'ds/stat-name-width-configuration'
"git diff" learned diff.statNameWidth configuration variable, to give the default width for the name part in the "--stat" output. * ds/stat-name-width-configuration: diff --stat: add config option to limit filename width
2 parents 2affeb3 + bd48adc commit 0b493d2

File tree

9 files changed

+60
-18
lines changed

9 files changed

+60
-18
lines changed

Documentation/config/diff.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ directories with less than 10% of the total amount of changed files,
5252
and accumulating child directory counts in the parent directories:
5353
`files,10,cumulative`.
5454

55+
diff.statNameWidth::
56+
Limit the width of the filename part in --stat output. If set, applies
57+
to all commands generating --stat output except format-patch.
58+
5559
diff.statGraphWidth::
5660
Limit the width of the graph part in --stat output. If set, applies
5761
to all commands generating --stat output except format-patch.

Documentation/diff-options.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,15 @@ have to use `--diff-algorithm=default` option.
204204
part. Maximum width defaults to terminal width, or 80 columns
205205
if not connected to a terminal, and can be overridden by
206206
`<width>`. The width of the filename part can be limited by
207-
giving another width `<name-width>` after a comma. The width
208-
of the graph part can be limited by using
209-
`--stat-graph-width=<width>` (affects all commands generating
210-
a stat graph) or by setting `diff.statGraphWidth=<width>`
211-
(does not affect `git format-patch`).
212-
By giving a third parameter `<count>`, you can limit the
213-
output to the first `<count>` lines, followed by `...` if
214-
there are more.
207+
giving another width `<name-width>` after a comma or by setting
208+
`diff.statNameWidth=<width>`. The width of the graph part can be
209+
limited by using `--stat-graph-width=<width>` or by setting
210+
`diff.statGraphWidth=<width>`. Using `--stat` or
211+
`--stat-graph-width` affects all commands generating a stat graph,
212+
while setting `diff.statNameWidth` or `diff.statGraphWidth`
213+
does not affect `git format-patch`.
214+
By giving a third parameter `<count>`, you can limit the output to
215+
the first `<count>` lines, followed by `...` if there are more.
215216
+
216217
These parameters can also be set individually with `--stat-width=<width>`,
217218
`--stat-name-width=<name-width>` and `--stat-count=<count>`.

builtin/diff.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
475475

476476
/* Set up defaults that will apply to both no-index and regular diffs. */
477477
rev.diffopt.stat_width = -1;
478+
rev.diffopt.stat_name_width = -1;
478479
rev.diffopt.stat_graph_width = -1;
479480
rev.diffopt.flags.allow_external = 1;
480481
rev.diffopt.flags.allow_textconv = 1;

builtin/log.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ static void cmd_log_init_defaults(struct rev_info *rev)
178178
rev->verbose_header = 1;
179179
rev->diffopt.flags.recursive = 1;
180180
rev->diffopt.stat_width = -1; /* use full terminal width */
181+
rev->diffopt.stat_name_width = -1; /* respect statNameWidth config */
181182
rev->diffopt.stat_graph_width = -1; /* respect statGraphWidth config */
182183
rev->abbrev_commit = default_abbrev_commit;
183184
rev->show_root_diff = default_show_root;

builtin/merge.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ static void finish(struct commit *head_commit,
467467
struct diff_options opts;
468468
repo_diff_setup(the_repository, &opts);
469469
opts.stat_width = -1; /* use full terminal width */
470+
opts.stat_name_width = -1; /* respect statNameWidth config */
470471
opts.stat_graph_width = -1; /* respect statGraphWidth config */
471472
opts.output_format |=
472473
DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;

builtin/rebase.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,6 +1804,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
18041804
/* We want color (if set), but no pager */
18051805
repo_diff_setup(the_repository, &opts);
18061806
opts.stat_width = -1; /* use full terminal width */
1807+
opts.stat_name_width = -1; /* respect statNameWidth config */
18071808
opts.stat_graph_width = -1; /* respect statGraphWidth config */
18081809
opts.output_format |=
18091810
DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;

diff.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ int diff_auto_refresh_index = 1;
6565
static int diff_mnemonic_prefix;
6666
static int diff_no_prefix;
6767
static int diff_relative;
68+
static int diff_stat_name_width;
6869
static int diff_stat_graph_width;
6970
static int diff_dirstat_permille_default = 30;
7071
static struct diff_options default_diff_options;
@@ -410,6 +411,10 @@ int git_diff_ui_config(const char *var, const char *value,
410411
diff_relative = git_config_bool(var, value);
411412
return 0;
412413
}
414+
if (!strcmp(var, "diff.statnamewidth")) {
415+
diff_stat_name_width = git_config_int(var, value, ctx->kvi);
416+
return 0;
417+
}
413418
if (!strcmp(var, "diff.statgraphwidth")) {
414419
diff_stat_graph_width = git_config_int(var, value, ctx->kvi);
415420
return 0;
@@ -2704,12 +2709,14 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
27042709
number_width = decimal_width(max_change) > number_width ?
27052710
decimal_width(max_change) : number_width;
27062711

2712+
if (options->stat_name_width == -1)
2713+
options->stat_name_width = diff_stat_name_width;
27072714
if (options->stat_graph_width == -1)
27082715
options->stat_graph_width = diff_stat_graph_width;
27092716

27102717
/*
2711-
* Guarantee 3/8*16==6 for the graph part
2712-
* and 5/8*16==10 for the filename part
2718+
* Guarantee 3/8*16 == 6 for the graph part
2719+
* and 5/8*16 == 10 for the filename part
27132720
*/
27142721
if (width < 16 + 6 + number_width)
27152722
width = 16 + 6 + number_width;

graph.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ void graph_setup_line_prefix(struct diff_options *diffopt)
339339
diffopt->output_prefix = diff_output_prefix_callback;
340340
}
341341

342-
343342
struct git_graph *graph_init(struct rev_info *opt)
344343
{
345344
struct git_graph *graph = xmalloc(sizeof(struct git_graph));

t/t4052-stat-output.sh

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,41 @@ log -1 --stat
4949
EOF
5050

5151
cat >expect.60 <<-'EOF'
52-
...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
52+
...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
5353
EOF
5454
cat >expect.6030 <<-'EOF'
5555
...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
5656
EOF
57-
cat >expect2.60 <<-'EOF'
57+
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" '
62+
git -c diff.statNameWidth=30 $cmd $args >output &&
63+
grep " | " output >actual &&
64+
test_cmp $expect actual
65+
';;
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" '
70+
git -c diff.statNameWidth=30 $cmd $args --stat=60 >output &&
71+
grep " | " output >actual &&
72+
test_cmp $expect actual
73+
';;
74+
esac
75+
done <<\EOF
76+
ignores expect72 format-patch -1 --stdout
77+
ignores expect.60 format-patch -1 --stdout
78+
respects expect.6030 diff HEAD^ HEAD --stat
79+
respects expect.6030 show --stat
80+
respects expect.6030 log -1 --stat
81+
EOF
82+
83+
cat >expect.40 <<-'EOF'
84+
...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
85+
EOF
86+
cat >expect2.40 <<-'EOF'
5887
...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
5988
...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
6089
EOF
@@ -67,16 +96,16 @@ do
6796
test_expect_success "$cmd --stat=width: a long name is given more room when the bar is short" '
6897
git $cmd $args --stat=40 >output &&
6998
grep " | " output >actual &&
70-
test_cmp $expect.60 actual
99+
test_cmp $expect.40 actual
71100
'
72101

73102
test_expect_success "$cmd --stat-width=width with long name" '
74103
git $cmd $args --stat-width=40 >output &&
75104
grep " | " output >actual &&
76-
test_cmp $expect.60 actual
105+
test_cmp $expect.40 actual
77106
'
78107

79-
test_expect_success "$cmd --stat=...,name-width with long name" '
108+
test_expect_success "$cmd --stat=width,name-width with long name" '
80109
git $cmd $args --stat=60,30 >output &&
81110
grep " | " output >actual &&
82111
test_cmp $expect.6030 actual
@@ -94,7 +123,6 @@ expect show --stat
94123
expect log -1 --stat
95124
EOF
96125

97-
98126
test_expect_success 'preparation for big change tests' '
99127
>abcd &&
100128
git add abcd &&
@@ -207,7 +235,6 @@ respects expect40 show --stat
207235
respects expect40 log -1 --stat
208236
EOF
209237

210-
211238
cat >expect <<'EOF'
212239
abcd | 1000 ++++++++++++++++++++++++++
213240
EOF

0 commit comments

Comments
 (0)