Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 29c2a3d

Browse files
committed
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
2 parents a5f9ba9 + dc801e7 commit 29c2a3d

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() - options->output_prefix_length;
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;
@@ -1587,7 +1614,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
15871614
if (data->files[i]->is_binary) {
15881615
fprintf(options->file, "%s", line_prefix);
15891616
show_name(options->file, prefix, name, len);
1590-
fprintf(options->file, " Bin ");
1617+
fprintf(options->file, " %*s ", number_width, "Bin");
15911618
fprintf(options->file, "%s%"PRIuMAX"%s",
15921619
del_c, deleted, reset);
15931620
fprintf(options->file, " -> ");
@@ -1600,7 +1627,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
16001627
else if (data->files[i]->is_unmerged) {
16011628
fprintf(options->file, "%s", line_prefix);
16021629
show_name(options->file, prefix, name, len);
1603-
fprintf(options->file, " Unmerged\n");
1630+
fprintf(options->file, " Unmerged\n");
16041631
continue;
16051632
}
16061633

@@ -1629,8 +1656,9 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
16291656
}
16301657
fprintf(options->file, "%s", line_prefix);
16311658
show_name(options->file, prefix, name, len);
1632-
fprintf(options->file, "%5"PRIuMAX"%s", added + deleted,
1633-
added + deleted ? " " : "");
1659+
fprintf(options->file, " %*"PRIuMAX"%s",
1660+
number_width, added + deleted,
1661+
added + deleted ? " " : "");
16341662
show_graph(options->file, '+', add, add_c, reset);
16351663
show_graph(options->file, '-', del, del_c, reset);
16361664
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

0 commit comments

Comments
 (0)