Skip to content

Commit dc801e7

Browse files
keszybzgitster
authored andcommitted
diff --stat: use less columns for change counts
Number of columns required for change counts is now computed based on the maximum number of changed lines instead of being fixed. This means that usually a few more columns will be available for the filenames and the graph. The graph width logic is also modified to include enough space for "Bin XXX -> YYY bytes". If changes to binary files are mixed with changes to text files, change counts are padded to take at least three columns. And the other way around, if change counts require more than three columns, then "Bin"s are padded to align with the change count. This way, the +- part starts in the same column as "XXX -> YYY" part for binary files. This makes the graph easier to parse visually thanks to the empty column. This mimics the layout of diff --stat before this change. Tests and the tutorial are updated to reflect the new --stat output. This means either the removal of extra padding and/or the addition of up to three extra characters to truncated filenames. One test is added to check the graph alignment when a binary file change and text file change of more than 999 lines are committed together. Signed-off-by: Zbigniew Jędrzejewski-Szmek <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 10d4332 commit dc801e7

File tree

77 files changed

+400
-353
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+400
-353
lines changed

Documentation/gitcore-tutorial.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,8 @@ would be different)
10021002
----------------
10031003
Updating from ae3a2da... to a80b4aa....
10041004
Fast-forward (no commit created; -m option ignored)
1005-
example | 1 +
1006-
hello | 1 +
1005+
example | 1 +
1006+
hello | 1 +
10071007
2 files changed, 2 insertions(+)
10081008
----------------
10091009

diff.c

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,8 +1443,8 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
14431443
{
14441444
int i, len, add, del, adds = 0, dels = 0;
14451445
uintmax_t max_change = 0, max_len = 0;
1446-
int total_files = data->nr;
1447-
int width, name_width, graph_width, number_width = 4, count;
1446+
int total_files = data->nr, count;
1447+
int width, name_width, graph_width, number_width = 0, bin_width = 0;
14481448
const char *reset, *add_c, *del_c;
14491449
const char *line_prefix = "";
14501450
int extra_shown = 0;
@@ -1480,8 +1480,21 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
14801480
if (max_len < len)
14811481
max_len = len;
14821482

1483-
if (file->is_binary || file->is_unmerged)
1483+
if (file->is_unmerged) {
1484+
/* "Unmerged" is 8 characters */
1485+
bin_width = bin_width < 8 ? 8 : bin_width;
14841486
continue;
1487+
}
1488+
if (file->is_binary) {
1489+
/* "Bin XXX -> YYY bytes" */
1490+
int w = 14 + decimal_width(file->added)
1491+
+ decimal_width(file->deleted);
1492+
bin_width = bin_width < w ? w : bin_width;
1493+
/* Display change counts aligned with "Bin" */
1494+
number_width = 3;
1495+
continue;
1496+
}
1497+
14851498
if (max_change < change)
14861499
max_change = change;
14871500
}
@@ -1506,12 +1519,22 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
15061519
* stat_name_width fixes the maximum width of the filename,
15071520
* and is also used to divide available columns if there
15081521
* aren't enough.
1522+
*
1523+
* Binary files are displayed with "Bin XXX -> YYY bytes"
1524+
* instead of the change count and graph. This part is treated
1525+
* similarly to the graph part, except that it is not
1526+
* "scaled". If total width is too small to accomodate the
1527+
* guaranteed minimum width of the filename part and the
1528+
* separators and this message, this message will "overflow"
1529+
* making the line longer than the maximum width.
15091530
*/
15101531

15111532
if (options->stat_width == -1)
15121533
width = term_columns();
15131534
else
15141535
width = options->stat_width ? options->stat_width : 80;
1536+
number_width = decimal_width(max_change) > number_width ?
1537+
decimal_width(max_change) : number_width;
15151538

15161539
if (options->stat_graph_width == -1)
15171540
options->stat_graph_width = diff_stat_graph_width;
@@ -1525,10 +1548,14 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
15251548

15261549
/*
15271550
* First assign sizes that are wanted, ignoring available width.
1551+
* strlen("Bin XXX -> YYY bytes") == bin_width, and the part
1552+
* starting from "XXX" should fit in graph_width.
15281553
*/
1529-
graph_width = (options->stat_graph_width &&
1530-
options->stat_graph_width < max_change) ?
1531-
options->stat_graph_width : max_change;
1554+
graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
1555+
if (options->stat_graph_width &&
1556+
options->stat_graph_width < graph_width)
1557+
graph_width = options->stat_graph_width;
1558+
15321559
name_width = (options->stat_name_width > 0 &&
15331560
options->stat_name_width < max_len) ?
15341561
options->stat_name_width : max_len;
@@ -1583,7 +1610,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
15831610
if (data->files[i]->is_binary) {
15841611
fprintf(options->file, "%s", line_prefix);
15851612
show_name(options->file, prefix, name, len);
1586-
fprintf(options->file, " Bin ");
1613+
fprintf(options->file, " %*s ", number_width, "Bin");
15871614
fprintf(options->file, "%s%"PRIuMAX"%s",
15881615
del_c, deleted, reset);
15891616
fprintf(options->file, " -> ");
@@ -1596,7 +1623,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
15961623
else if (data->files[i]->is_unmerged) {
15971624
fprintf(options->file, "%s", line_prefix);
15981625
show_name(options->file, prefix, name, len);
1599-
fprintf(options->file, " Unmerged\n");
1626+
fprintf(options->file, " Unmerged\n");
16001627
continue;
16011628
}
16021629

@@ -1625,8 +1652,9 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
16251652
}
16261653
fprintf(options->file, "%s", line_prefix);
16271654
show_name(options->file, prefix, name, len);
1628-
fprintf(options->file, "%5"PRIuMAX"%s", added + deleted,
1629-
added + deleted ? " " : "");
1655+
fprintf(options->file, " %*"PRIuMAX"%s",
1656+
number_width, added + deleted,
1657+
added + deleted ? " " : "");
16301658
show_graph(options->file, '+', add, add_c, reset);
16311659
show_graph(options->file, '-', del, del_c, reset);
16321660
fprintf(options->file, "\n");

t/t0023-crlf-am.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Date: Thu, 23 Aug 2007 13:00:00 +0200
1111
Subject: test1
1212
1313
---
14-
foo | 1 +
14+
foo | 1 +
1515
1 files changed, 1 insertions(+), 0 deletions(-)
1616
create mode 100644 foo
1717

t/t1200-tutorial.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ test_expect_success 'git show-branch' '
154154
cat > resolve.expect << EOF
155155
Updating VARIABLE..VARIABLE
156156
FASTFORWARD (no commit created; -m option ignored)
157-
example | 1 +
158-
hello | 1 +
157+
example | 1 +
158+
hello | 1 +
159159
2 files changed, 2 insertions(+)
160160
EOF
161161

t/t3404-rebase-interactive.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ test_expect_success 'verbose flag is heeded, even after --continue' '
323323
echo resolved > file1 &&
324324
git add file1 &&
325325
git rebase --continue > output &&
326-
grep "^ file1 | 2 +-$" output
326+
grep "^ file1 | 2 +-$" output
327327
'
328328

329329
test_expect_success 'multi-squash only fires up editor once' '

t/t3903-stash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ test_expect_success 'stash show format defaults to --stat' '
443443
STASH_ID=$(git stash create) &&
444444
git reset --hard &&
445445
cat >expected <<-EOF &&
446-
file | 1 +
446+
file | 1 +
447447
1 file changed, 1 insertion(+)
448448
EOF
449449
git stash show ${STASH_ID} >actual &&

t/t4012-diff-binary.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,23 @@ test_expect_success 'diff --no-index with binary creation' '
107107
test_cmp expected actual
108108
'
109109

110+
cat >expect <<EOF
111+
binfile | Bin 0 -> 1026 bytes
112+
textfile | 10000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
113+
EOF
114+
115+
test_expect_success 'diff --stat with binary files and big change count' '
116+
echo X | dd of=binfile bs=1k seek=1 &&
117+
git add binfile &&
118+
i=0 &&
119+
while test $i -lt 10000; do
120+
echo $i &&
121+
i=$(($i + 1))
122+
done >textfile &&
123+
git add textfile &&
124+
git diff --cached --stat binfile textfile >output &&
125+
grep " | " output >actual &&
126+
test_cmp expect actual
127+
'
128+
110129
test_done

t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_master

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
$ git diff-tree --cc --patch-with-stat --summary master
22
59d314ad6f356dd08601a4cd5e530381da3e3c64
3-
dir/sub | 2 ++
4-
file0 | 3 +++
3+
dir/sub | 2 ++
4+
file0 | 3 +++
55
2 files changed, 5 insertions(+)
66

77
diff --cc dir/sub

t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_side

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
$ git diff-tree --cc --patch-with-stat --summary side
22
c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
3-
dir/sub | 2 ++
4-
file0 | 3 +++
5-
file3 | 4 ++++
3+
dir/sub | 2 ++
4+
file0 | 3 +++
5+
file3 | 4 ++++
66
3 files changed, 9 insertions(+)
77
create mode 100644 file3
88

t/t4013/diff.diff-tree_--cc_--patch-with-stat_master

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
$ git diff-tree --cc --patch-with-stat master
22
59d314ad6f356dd08601a4cd5e530381da3e3c64
3-
dir/sub | 2 ++
4-
file0 | 3 +++
3+
dir/sub | 2 ++
4+
file0 | 3 +++
55
2 files changed, 5 insertions(+)
66

77
diff --cc dir/sub
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$ git diff-tree --cc --stat --summary master
22
59d314ad6f356dd08601a4cd5e530381da3e3c64
3-
dir/sub | 2 ++
4-
file0 | 3 +++
3+
dir/sub | 2 ++
4+
file0 | 3 +++
55
2 files changed, 5 insertions(+)
66
$
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
$ git diff-tree --cc --stat --summary side
22
c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
3-
dir/sub | 2 ++
4-
file0 | 3 +++
5-
file3 | 4 ++++
3+
dir/sub | 2 ++
4+
file0 | 3 +++
5+
file3 | 4 ++++
66
3 files changed, 9 insertions(+)
77
create mode 100644 file3
88
$
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$ git diff-tree --cc --stat master
22
59d314ad6f356dd08601a4cd5e530381da3e3c64
3-
dir/sub | 2 ++
4-
file0 | 3 +++
3+
dir/sub | 2 ++
4+
file0 | 3 +++
55
2 files changed, 5 insertions(+)
66
$

t/t4013/diff.diff-tree_--pretty=oneline_--root_--patch-with-stat_initial

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
$ git diff-tree --pretty=oneline --root --patch-with-stat initial
22
444ac553ac7612cc88969031b02b3767fb8a353a Initial
3-
dir/sub | 2 ++
4-
file0 | 3 +++
5-
file2 | 3 +++
3+
dir/sub | 2 ++
4+
file0 | 3 +++
5+
file2 | 3 +++
66
3 files changed, 8 insertions(+)
77

88
diff --git a/dir/sub b/dir/sub

t/t4013/diff.diff-tree_--pretty_--patch-with-stat_side

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Date: Mon Jun 26 00:03:00 2006 +0000
55

66
Side
77
---
8-
dir/sub | 2 ++
9-
file0 | 3 +++
10-
file3 | 4 ++++
8+
dir/sub | 2 ++
9+
file0 | 3 +++
10+
file3 | 4 ++++
1111
3 files changed, 9 insertions(+)
1212

1313
diff --git a/dir/sub b/dir/sub

t/t4013/diff.diff-tree_--pretty_--root_--patch-with-stat_initial

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Date: Mon Jun 26 00:00:00 2006 +0000
55

66
Initial
77
---
8-
dir/sub | 2 ++
9-
file0 | 3 +++
10-
file2 | 3 +++
8+
dir/sub | 2 ++
9+
file0 | 3 +++
10+
file2 | 3 +++
1111
3 files changed, 8 insertions(+)
1212

1313
diff --git a/dir/sub b/dir/sub

t/t4013/diff.diff-tree_--pretty_--root_--stat_--summary_initial

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Date: Mon Jun 26 00:00:00 2006 +0000
55

66
Initial
77

8-
dir/sub | 2 ++
9-
file0 | 3 +++
10-
file2 | 3 +++
8+
dir/sub | 2 ++
9+
file0 | 3 +++
10+
file2 | 3 +++
1111
3 files changed, 8 insertions(+)
1212
create mode 100644 dir/sub
1313
create mode 100644 file0

t/t4013/diff.diff-tree_--pretty_--root_--stat_initial

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Date: Mon Jun 26 00:00:00 2006 +0000
55

66
Initial
77

8-
dir/sub | 2 ++
9-
file0 | 3 +++
10-
file2 | 3 +++
8+
dir/sub | 2 ++
9+
file0 | 3 +++
10+
file2 | 3 +++
1111
3 files changed, 8 insertions(+)
1212
$

t/t4013/diff.diff-tree_--root_--patch-with-stat_initial

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
$ git diff-tree --root --patch-with-stat initial
22
444ac553ac7612cc88969031b02b3767fb8a353a
3-
dir/sub | 2 ++
4-
file0 | 3 +++
5-
file2 | 3 +++
3+
dir/sub | 2 ++
4+
file0 | 3 +++
5+
file2 | 3 +++
66
3 files changed, 8 insertions(+)
77

88
diff --git a/dir/sub b/dir/sub
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$ git diff-tree -c --stat --summary master
22
59d314ad6f356dd08601a4cd5e530381da3e3c64
3-
dir/sub | 2 ++
4-
file0 | 3 +++
3+
dir/sub | 2 ++
4+
file0 | 3 +++
55
2 files changed, 5 insertions(+)
66
$
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
$ git diff-tree -c --stat --summary side
22
c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
3-
dir/sub | 2 ++
4-
file0 | 3 +++
5-
file3 | 4 ++++
3+
dir/sub | 2 ++
4+
file0 | 3 +++
5+
file3 | 4 ++++
66
3 files changed, 9 insertions(+)
77
create mode 100644 file3
88
$
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$ git diff-tree -c --stat master
22
59d314ad6f356dd08601a4cd5e530381da3e3c64
3-
dir/sub | 2 ++
4-
file0 | 3 +++
3+
dir/sub | 2 ++
4+
file0 | 3 +++
55
2 files changed, 5 insertions(+)
66
$

t/t4013/diff.diff_--patch-with-stat_-r_initial..side

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
$ git diff --patch-with-stat -r initial..side
2-
dir/sub | 2 ++
3-
file0 | 3 +++
4-
file3 | 4 ++++
2+
dir/sub | 2 ++
3+
file0 | 3 +++
4+
file3 | 4 ++++
55
3 files changed, 9 insertions(+)
66

77
diff --git a/dir/sub b/dir/sub

t/t4013/diff.diff_--patch-with-stat_initial..side

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
$ git diff --patch-with-stat initial..side
2-
dir/sub | 2 ++
3-
file0 | 3 +++
4-
file3 | 4 ++++
2+
dir/sub | 2 ++
3+
file0 | 3 +++
4+
file3 | 4 ++++
55
3 files changed, 9 insertions(+)
66

77
diff --git a/dir/sub b/dir/sub
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$ git diff --stat initial..side
2-
dir/sub | 2 ++
3-
file0 | 3 +++
4-
file3 | 4 ++++
2+
dir/sub | 2 ++
3+
file0 | 3 +++
4+
file3 | 4 ++++
55
3 files changed, 9 insertions(+)
66
$
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$ git diff -r --stat initial..side
2-
dir/sub | 2 ++
3-
file0 | 3 +++
4-
file3 | 4 ++++
2+
dir/sub | 2 ++
3+
file0 | 3 +++
4+
file3 | 4 ++++
55
3 files changed, 9 insertions(+)
66
$

t/t4013/diff.format-patch_--attach_--stdout_--suffix=.diff_initial..side

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Content-Type: text/plain; charset=UTF-8; format=fixed
1212
Content-Transfer-Encoding: 8bit
1313

1414
---
15-
dir/sub | 2 ++
16-
file0 | 3 +++
17-
file3 | 4 ++++
15+
dir/sub | 2 ++
16+
file0 | 3 +++
17+
file3 | 4 ++++
1818
3 files changed, 9 insertions(+)
1919
create mode 100644 file3
2020

0 commit comments

Comments
 (0)