diff --stat: use the full terminal width

Default to the real terminal width for diff --stat output, instead of the hard-coded 80 columns. Some projects (especially in Java), have long filename paths, with nested directories or long individual filenames. When files are renamed, the filename part in stat output can be almost useless. If the middle part between { and } is long (because the file was moved to a completely different directory), then most of the path would be truncated. It makes sense to detect and use the full terminal width and display full filenames if possible. The are commands like diff, show, and log, which can adapt the output to the terminal width. There are also commands like format-patch, whose output should be independent of the terminal width. Since it is safer to use the 80-column default, the real terminal width is only used if requested by the calling code by setting diffopts.stat_width=-1. Normally this value is 0, and can be set by the user only to a non-negative value, so -1 is safe to use internally. This patch only changes the diff builtin to use the full terminal width. Signed-off-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Zbigniew Jędrzejewski-Szmek committed Mar 1, 2012 at 13:26 UTC af9fedc12873bf331019a502ed1fc944fa986713
3 files changed +16 -3
builtin/diff.c
+3
@@ -285,6 +285,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
285 /* Otherwise, we are doing the usual "git" diff */
286 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
287
288 + /* Scale to real terminal size */
289 + rev.diffopt.stat_width = -1;
290 +
291 /* Default to let external and textconv be used */
292 DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
293 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
diff.c
+4 -1
@@ -1343,7 +1343,10 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1343 line_prefix = msg->buf;
1344 }
1345
1346 - width = options->stat_width ? options->stat_width : 80;
1346 + if (options->stat_width == -1)
1347 + width = term_columns();
1348 + else
1349 + width = options->stat_width ? options->stat_width : 80;
1350 name_width = options->stat_name_width ? options->stat_name_width : 50;
1351 count = options->stat_count ? options->stat_count : data->nr;
1352
t/t4052-stat-output.sh
+9 -2
@@ -83,6 +83,10 @@ cat >expect80 <<'EOF'
83 abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
84 EOF
85
86 +cat >expect200 <<'EOF'
87 + abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
88 +EOF
89 +
90 while read verb expect cmd args
91 do
92 test_expect_success "$cmd $verb COLUMNS (big change)" '
@@ -92,7 +96,7 @@ do
96 '
97 done <<\EOF
98 ignores expect80 format-patch -1 --stdout
95 -ignores expect80 diff HEAD^ HEAD --stat
99 +respects expect200 diff HEAD^ HEAD --stat
100 ignores expect80 show --stat
101 ignores expect80 log -1 --stat
102 EOF
@@ -146,6 +150,9 @@ EOF
150 cat >expect80 <<'EOF'
151 ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++++++++++
152 EOF
153 +cat >expect200 <<'EOF'
154 + ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
155 +EOF
156 while read verb expect cmd args
157 do
158 test_expect_success "$cmd $verb COLUMNS (long filename)" '
@@ -155,7 +162,7 @@ do
162 '
163 done <<\EOF
164 ignores expect80 format-patch -1 --stdout
158 -ignores expect80 diff HEAD^ HEAD --stat
165 +respects expect200 diff HEAD^ HEAD --stat
166 ignores expect80 show --stat
167 ignores expect80 log -1 --stat
168 EOF