Permalink
Browse files

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
  • Loading branch information...
2 parents a5f9ba9 + dc801e7 commit 29c2a3dbadaa1c2d1128a4892241f82f5b34778c @gitster gitster committed May 2, 2012
Showing with 400 additions and 353 deletions.
  1. +2 −2 Documentation/gitcore-tutorial.txt
  2. +38 −10 diff.c
  3. +1 −1 t/t0023-crlf-am.sh
  4. +2 −2 t/t1200-tutorial.sh
  5. +1 −1 t/t3404-rebase-interactive.sh
  6. +1 −1 t/t3903-stash.sh
  7. +19 −0 t/t4012-diff-binary.sh
  8. +2 −2 t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_master
  9. +3 −3 t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_side
  10. +2 −2 t/t4013/diff.diff-tree_--cc_--patch-with-stat_master
  11. +2 −2 t/t4013/diff.diff-tree_--cc_--stat_--summary_master
  12. +3 −3 t/t4013/diff.diff-tree_--cc_--stat_--summary_side
  13. +2 −2 t/t4013/diff.diff-tree_--cc_--stat_master
  14. +3 −3 t/t4013/diff.diff-tree_--pretty=oneline_--root_--patch-with-stat_initial
  15. +3 −3 t/t4013/diff.diff-tree_--pretty_--patch-with-stat_side
  16. +3 −3 t/t4013/diff.diff-tree_--pretty_--root_--patch-with-stat_initial
  17. +3 −3 t/t4013/diff.diff-tree_--pretty_--root_--stat_--summary_initial
  18. +3 −3 t/t4013/diff.diff-tree_--pretty_--root_--stat_initial
  19. +3 −3 t/t4013/diff.diff-tree_--root_--patch-with-stat_initial
  20. +2 −2 t/t4013/diff.diff-tree_-c_--stat_--summary_master
  21. +3 −3 t/t4013/diff.diff-tree_-c_--stat_--summary_side
  22. +2 −2 t/t4013/diff.diff-tree_-c_--stat_master
  23. +3 −3 t/t4013/diff.diff_--patch-with-stat_-r_initial..side
  24. +3 −3 t/t4013/diff.diff_--patch-with-stat_initial..side
  25. +3 −3 t/t4013/diff.diff_--stat_initial..side
  26. +3 −3 t/t4013/diff.diff_-r_--stat_initial..side
  27. +3 −3 t/t4013/diff.format-patch_--attach_--stdout_--suffix=.diff_initial..side
  28. +8 −8 t/t4013/diff.format-patch_--attach_--stdout_initial..master
  29. +5 −5 t/t4013/diff.format-patch_--attach_--stdout_initial..master^
  30. +3 −3 t/t4013/diff.format-patch_--attach_--stdout_initial..side
  31. +8 −8 t/t4013/diff.format-patch_--inline_--stdout_--numbered-files_initial..master
  32. +8 −8 t/t4013/diff.format-patch_--inline_--stdout_--subject-prefix=TESTCASE_initial..master
  33. +8 −8 t/t4013/diff.format-patch_--inline_--stdout_initial..master
  34. +5 −5 t/t4013/diff.format-patch_--inline_--stdout_initial..master^
  35. +3 −3 t/t4013/diff.format-patch_--inline_--stdout_initial..master^^
  36. +3 −3 t/t4013/diff.format-patch_--inline_--stdout_initial..side
  37. +9 −9 t/t4013/diff.format-patch_--stdout_--cover-letter_-n_initial..master^
  38. +8 −8 t/t4013/diff.format-patch_--stdout_--no-numbered_initial..master
  39. +8 −8 t/t4013/diff.format-patch_--stdout_--numbered_initial..master
  40. +8 −8 t/t4013/diff.format-patch_--stdout_initial..master
  41. +5 −5 t/t4013/diff.format-patch_--stdout_initial..master^
  42. +3 −3 t/t4013/diff.format-patch_--stdout_initial..side
  43. +3 −3 t/t4013/diff.log_--patch-with-stat_--summary_master_--_dir_
  44. +8 −8 t/t4013/diff.log_--patch-with-stat_master
  45. +3 −3 t/t4013/diff.log_--patch-with-stat_master_--_dir_
  46. +13 −13 t/t4013/diff.log_--root_--cc_--patch-with-stat_--summary_master
  47. +11 −11 t/t4013/diff.log_--root_--patch-with-stat_--summary_master
  48. +11 −11 t/t4013/diff.log_--root_--patch-with-stat_master
  49. +13 −13 t/t4013/diff.log_--root_-c_--patch-with-stat_--summary_master
  50. +3 −3 t/t4013/diff.show_--patch-with-stat_--summary_side
  51. +3 −3 t/t4013/diff.show_--patch-with-stat_side
  52. +3 −3 t/t4013/diff.show_--stat_--summary_side
  53. +3 −3 t/t4013/diff.show_--stat_side
  54. +3 −3 t/t4013/diff.whatchanged_--patch-with-stat_--summary_master_--_dir_
  55. +8 −8 t/t4013/diff.whatchanged_--patch-with-stat_master
  56. +3 −3 t/t4013/diff.whatchanged_--patch-with-stat_master_--_dir_
  57. +13 −13 t/t4013/diff.whatchanged_--root_--cc_--patch-with-stat_--summary_master
  58. +11 −11 t/t4013/diff.whatchanged_--root_--patch-with-stat_--summary_master
  59. +11 −11 t/t4013/diff.whatchanged_--root_--patch-with-stat_master
  60. +13 −13 t/t4013/diff.whatchanged_--root_-c_--patch-with-stat_--summary_master
  61. +7 −7 t/t4016-diff-quote.sh
  62. +1 −1 t/t4030-diff-textconv.sh
  63. +1 −1 t/t4045-diff-relative.sh
  64. +2 −2 t/t4049-diff-stat-count.sh
  65. +3 −3 t/t4052-stat-output.sh
  66. +15 −15 t/t4202-log.sh
  67. +1 −1 t/t5100/patch0001
  68. +1 −1 t/t5100/patch0002
  69. +1 −1 t/t5100/patch0003
  70. +2 −2 t/t5100/patch0005
  71. +1 −1 t/t5100/patch0006
  72. +1 −1 t/t5100/patch0010
  73. +1 −1 t/t5100/patch0011
  74. +1 −1 t/t5100/patch0014
  75. +1 −1 t/t5100/patch0014--scissors
  76. +9 −9 t/t5100/sample.mbox
  77. +6 −6 t/t7602-merge-octopus-many.sh
@@ -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(+)
----------------
View
48 diff.c
@@ -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, " -> ");
@@ -1600,7 +1627,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;
}
@@ -1629,8 +1656,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");
View
@@ -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
View
@@ -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
@@ -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' '
View
@@ -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 &&
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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(+)
$
@@ -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
$
@@ -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(+)
$
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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(+)
$
@@ -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
@@ -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(+)
$
@@ -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
$
@@ -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(+)
$
@@ -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
@@ -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
@@ -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(+)
$
@@ -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(+)
$
@@ -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
Oops, something went wrong.

0 comments on commit 29c2a3d

Please sign in to comment.