Merge branch 'zj/diff-stat-smaller-num-columns'

Spend only minimum number of columns necessary to show the number of lines
in the output from "diff --stat", instead of always allocating 4 columns
even when showing changes that are much smaller than 1000 lines.

By Zbigniew Jędrzejewski-Szmek
* zj/diff-stat-smaller-num-columns:
  diff --stat: use less columns for change counts
This commit is contained in:
Junio C Hamano 2012-05-02 13:53:28 -07:00
commit 29c2a3dbad
77 changed files with 400 additions and 353 deletions

44
diff.c
View file

@ -1443,8 +1443,8 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
{
int i, len, add, del, adds = 0, dels = 0;
uintmax_t max_change = 0, max_len = 0;
int total_files = data->nr;
int width, name_width, graph_width, number_width = 4, count;
int total_files = data->nr, count;
int width, name_width, graph_width, number_width = 0, bin_width = 0;
const char *reset, *add_c, *del_c;
const char *line_prefix = "";
int extra_shown = 0;
@ -1480,8 +1480,21 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
if (max_len < len)
max_len = len;
if (file->is_binary || file->is_unmerged)
if (file->is_unmerged) {
/* "Unmerged" is 8 characters */
bin_width = bin_width < 8 ? 8 : bin_width;
continue;
}
if (file->is_binary) {
/* "Bin XXX -> YYY bytes" */
int w = 14 + decimal_width(file->added)
+ decimal_width(file->deleted);
bin_width = bin_width < w ? w : bin_width;
/* Display change counts aligned with "Bin" */
number_width = 3;
continue;
}
if (max_change < change)
max_change = change;
}
@ -1506,12 +1519,22 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
* stat_name_width fixes the maximum width of the filename,
* and is also used to divide available columns if there
* aren't enough.
*
* Binary files are displayed with "Bin XXX -> YYY bytes"
* instead of the change count and graph. This part is treated
* similarly to the graph part, except that it is not
* "scaled". If total width is too small to accomodate the
* guaranteed minimum width of the filename part and the
* separators and this message, this message will "overflow"
* making the line longer than the maximum width.
*/
if (options->stat_width == -1)
width = term_columns() - options->output_prefix_length;
else
width = options->stat_width ? options->stat_width : 80;
number_width = decimal_width(max_change) > number_width ?
decimal_width(max_change) : number_width;
if (options->stat_graph_width == -1)
options->stat_graph_width = diff_stat_graph_width;
@ -1525,10 +1548,14 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
/*
* First assign sizes that are wanted, ignoring available width.
* strlen("Bin XXX -> YYY bytes") == bin_width, and the part
* starting from "XXX" should fit in graph_width.
*/
graph_width = (options->stat_graph_width &&
options->stat_graph_width < max_change) ?
options->stat_graph_width : max_change;
graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
if (options->stat_graph_width &&
options->stat_graph_width < graph_width)
graph_width = options->stat_graph_width;
name_width = (options->stat_name_width > 0 &&
options->stat_name_width < max_len) ?
options->stat_name_width : max_len;
@ -1587,7 +1614,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
if (data->files[i]->is_binary) {
fprintf(options->file, "%s", line_prefix);
show_name(options->file, prefix, name, len);
fprintf(options->file, " Bin ");
fprintf(options->file, " %*s ", number_width, "Bin");
fprintf(options->file, "%s%"PRIuMAX"%s",
del_c, deleted, reset);
fprintf(options->file, " -> ");
@ -1629,7 +1656,8 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
}
fprintf(options->file, "%s", line_prefix);
show_name(options->file, prefix, name, len);
fprintf(options->file, "%5"PRIuMAX"%s", added + deleted,
fprintf(options->file, " %*"PRIuMAX"%s",
number_width, added + deleted,
added + deleted ? " " : "");
show_graph(options->file, '+', add, add_c, reset);
show_graph(options->file, '-', del, del_c, reset);

View file

@ -107,4 +107,23 @@ test_expect_success 'diff --no-index with binary creation' '
test_cmp expected actual
'
cat >expect <<EOF
binfile | Bin 0 -> 1026 bytes
textfile | 10000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOF
test_expect_success 'diff --stat with binary files and big change count' '
echo X | dd of=binfile bs=1k seek=1 &&
git add binfile &&
i=0 &&
while test $i -lt 10000; do
echo $i &&
i=$(($i + 1))
done >textfile &&
git add textfile &&
git diff --cached --stat binfile textfile >output &&
grep " | " output >actual &&
test_cmp expect actual
'
test_done

View file

@ -22,7 +22,7 @@ test_expect_success 'preparation' '
while read cmd args
do
cat >expect <<-'EOF'
...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
EOF
test_expect_success "$cmd: small change with long name gives more space to the name" '
git $cmd $args >output &&
@ -31,7 +31,7 @@ do
'
cat >expect <<-'EOF'
...aaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
EOF
test_expect_success "$cmd --stat=width: a long name is given more room when the bar is short" '
git $cmd $args --stat=40 >output &&