Skip to content

Commit

Permalink
diff --stat: use less columns for change counts
Browse files Browse the repository at this point in the history
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 <zbyszek@in.waw.pl>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Zbigniew Jędrzejewski-Szmek authored and Junio C Hamano committed Apr 30, 2012
1 parent 10d4332 commit dc801e7
Show file tree
Hide file tree
Showing 77 changed files with 400 additions and 353 deletions.
4 changes: 2 additions & 2 deletions Documentation/gitcore-tutorial.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,8 @@ would be different)
----------------
Updating from ae3a2da... to a80b4aa....
Fast-forward (no commit created; -m option ignored)
example | 1 +
hello | 1 +
example | 1 +
hello | 1 +
2 files changed, 2 insertions(+)
----------------

Expand Down
48 changes: 38 additions & 10 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
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;
Expand All @@ -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;
Expand Down Expand Up @@ -1583,7 +1610,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, " -> ");
Expand All @@ -1596,7 +1623,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
else if (data->files[i]->is_unmerged) {
fprintf(options->file, "%s", line_prefix);
show_name(options->file, prefix, name, len);
fprintf(options->file, " Unmerged\n");
fprintf(options->file, " Unmerged\n");
continue;
}

Expand Down Expand Up @@ -1625,8 +1652,9 @@ 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,
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);
fprintf(options->file, "\n");
Expand Down
2 changes: 1 addition & 1 deletion t/t0023-crlf-am.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Date: Thu, 23 Aug 2007 13:00:00 +0200
Subject: test1
---
foo | 1 +
foo | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 foo
Expand Down
4 changes: 2 additions & 2 deletions t/t1200-tutorial.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ test_expect_success 'git show-branch' '
cat > resolve.expect << EOF
Updating VARIABLE..VARIABLE
FASTFORWARD (no commit created; -m option ignored)
example | 1 +
hello | 1 +
example | 1 +
hello | 1 +
2 files changed, 2 insertions(+)
EOF

Expand Down
2 changes: 1 addition & 1 deletion t/t3404-rebase-interactive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ test_expect_success 'verbose flag is heeded, even after --continue' '
echo resolved > file1 &&
git add file1 &&
git rebase --continue > output &&
grep "^ file1 | 2 +-$" output
grep "^ file1 | 2 +-$" output
'

test_expect_success 'multi-squash only fires up editor once' '
Expand Down
2 changes: 1 addition & 1 deletion t/t3903-stash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ test_expect_success 'stash show format defaults to --stat' '
STASH_ID=$(git stash create) &&
git reset --hard &&
cat >expected <<-EOF &&
file | 1 +
file | 1 +
1 file changed, 1 insertion(+)
EOF
git stash show ${STASH_ID} >actual &&
Expand Down
19 changes: 19 additions & 0 deletions t/t4012-diff-binary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$ git diff-tree --cc --patch-with-stat --summary master
59d314ad6f356dd08601a4cd5e530381da3e3c64
dir/sub | 2 ++
file0 | 3 +++
dir/sub | 2 ++
file0 | 3 +++
2 files changed, 5 insertions(+)

diff --cc dir/sub
Expand Down
6 changes: 3 additions & 3 deletions t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_side
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
$ git diff-tree --cc --patch-with-stat --summary side
c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
3 files changed, 9 insertions(+)
create mode 100644 file3

Expand Down
4 changes: 2 additions & 2 deletions t/t4013/diff.diff-tree_--cc_--patch-with-stat_master
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$ git diff-tree --cc --patch-with-stat master
59d314ad6f356dd08601a4cd5e530381da3e3c64
dir/sub | 2 ++
file0 | 3 +++
dir/sub | 2 ++
file0 | 3 +++
2 files changed, 5 insertions(+)

diff --cc dir/sub
Expand Down
4 changes: 2 additions & 2 deletions t/t4013/diff.diff-tree_--cc_--stat_--summary_master
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$ git diff-tree --cc --stat --summary master
59d314ad6f356dd08601a4cd5e530381da3e3c64
dir/sub | 2 ++
file0 | 3 +++
dir/sub | 2 ++
file0 | 3 +++
2 files changed, 5 insertions(+)
$
6 changes: 3 additions & 3 deletions t/t4013/diff.diff-tree_--cc_--stat_--summary_side
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
$ git diff-tree --cc --stat --summary side
c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
3 files changed, 9 insertions(+)
create mode 100644 file3
$
4 changes: 2 additions & 2 deletions t/t4013/diff.diff-tree_--cc_--stat_master
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$ git diff-tree --cc --stat master
59d314ad6f356dd08601a4cd5e530381da3e3c64
dir/sub | 2 ++
file0 | 3 +++
dir/sub | 2 ++
file0 | 3 +++
2 files changed, 5 insertions(+)
$
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
$ git diff-tree --pretty=oneline --root --patch-with-stat initial
444ac553ac7612cc88969031b02b3767fb8a353a Initial
dir/sub | 2 ++
file0 | 3 +++
file2 | 3 +++
dir/sub | 2 ++
file0 | 3 +++
file2 | 3 +++
3 files changed, 8 insertions(+)

diff --git a/dir/sub b/dir/sub
Expand Down
6 changes: 3 additions & 3 deletions t/t4013/diff.diff-tree_--pretty_--patch-with-stat_side
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Date: Mon Jun 26 00:03:00 2006 +0000

Side
---
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
3 files changed, 9 insertions(+)

diff --git a/dir/sub b/dir/sub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Date: Mon Jun 26 00:00:00 2006 +0000

Initial
---
dir/sub | 2 ++
file0 | 3 +++
file2 | 3 +++
dir/sub | 2 ++
file0 | 3 +++
file2 | 3 +++
3 files changed, 8 insertions(+)

diff --git a/dir/sub b/dir/sub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Date: Mon Jun 26 00:00:00 2006 +0000

Initial

dir/sub | 2 ++
file0 | 3 +++
file2 | 3 +++
dir/sub | 2 ++
file0 | 3 +++
file2 | 3 +++
3 files changed, 8 insertions(+)
create mode 100644 dir/sub
create mode 100644 file0
Expand Down
6 changes: 3 additions & 3 deletions t/t4013/diff.diff-tree_--pretty_--root_--stat_initial
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Date: Mon Jun 26 00:00:00 2006 +0000

Initial

dir/sub | 2 ++
file0 | 3 +++
file2 | 3 +++
dir/sub | 2 ++
file0 | 3 +++
file2 | 3 +++
3 files changed, 8 insertions(+)
$
6 changes: 3 additions & 3 deletions t/t4013/diff.diff-tree_--root_--patch-with-stat_initial
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
$ git diff-tree --root --patch-with-stat initial
444ac553ac7612cc88969031b02b3767fb8a353a
dir/sub | 2 ++
file0 | 3 +++
file2 | 3 +++
dir/sub | 2 ++
file0 | 3 +++
file2 | 3 +++
3 files changed, 8 insertions(+)

diff --git a/dir/sub b/dir/sub
Expand Down
4 changes: 2 additions & 2 deletions t/t4013/diff.diff-tree_-c_--stat_--summary_master
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$ git diff-tree -c --stat --summary master
59d314ad6f356dd08601a4cd5e530381da3e3c64
dir/sub | 2 ++
file0 | 3 +++
dir/sub | 2 ++
file0 | 3 +++
2 files changed, 5 insertions(+)
$
6 changes: 3 additions & 3 deletions t/t4013/diff.diff-tree_-c_--stat_--summary_side
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
$ git diff-tree -c --stat --summary side
c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
3 files changed, 9 insertions(+)
create mode 100644 file3
$
4 changes: 2 additions & 2 deletions t/t4013/diff.diff-tree_-c_--stat_master
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$ git diff-tree -c --stat master
59d314ad6f356dd08601a4cd5e530381da3e3c64
dir/sub | 2 ++
file0 | 3 +++
dir/sub | 2 ++
file0 | 3 +++
2 files changed, 5 insertions(+)
$
6 changes: 3 additions & 3 deletions t/t4013/diff.diff_--patch-with-stat_-r_initial..side
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$ git diff --patch-with-stat -r initial..side
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
3 files changed, 9 insertions(+)

diff --git a/dir/sub b/dir/sub
Expand Down
6 changes: 3 additions & 3 deletions t/t4013/diff.diff_--patch-with-stat_initial..side
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$ git diff --patch-with-stat initial..side
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
3 files changed, 9 insertions(+)

diff --git a/dir/sub b/dir/sub
Expand Down
6 changes: 3 additions & 3 deletions t/t4013/diff.diff_--stat_initial..side
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$ git diff --stat initial..side
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
3 files changed, 9 insertions(+)
$
6 changes: 3 additions & 3 deletions t/t4013/diff.diff_-r_--stat_initial..side
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$ git diff -r --stat initial..side
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
3 files changed, 9 insertions(+)
$
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit

---
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
dir/sub | 2 ++
file0 | 3 +++
file3 | 4 ++++
3 files changed, 9 insertions(+)
create mode 100644 file3

Expand Down
Loading

0 comments on commit dc801e7

Please sign in to comment.