Skip to content

Commit a852aac

Browse files
committed
Merge branch 'mg/diff-stat-count'
* mg/diff-stat-count: diff --stat-count: finishing touches diff-options.txt: describe --stat-{width,name-width,count} diff: introduce --stat-lines to limit the stat lines diff.c: omit hidden entries from namelen calculation with --stat
2 parents 0faf247 + e5f85df commit a852aac

File tree

4 files changed

+78
-10
lines changed

4 files changed

+78
-10
lines changed

Documentation/diff-options.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ endif::git-format-patch[]
4848
--patience::
4949
Generate a diff using the "patience diff" algorithm.
5050

51-
--stat[=<width>[,<name-width>]]::
51+
--stat[=<width>[,<name-width>[,<count>]]]::
5252
Generate a diffstat. You can override the default
5353
output width for 80-column terminal by `--stat=<width>`.
5454
The width of the filename part can be controlled by
5555
giving another width to it separated by a comma.
56+
By giving a third parameter `<count>`, you can limit the
57+
output to the first `<count>` lines, followed by
58+
`...` if there are more.
59+
+
60+
These parameters can also be set individually with `--stat-width=<width>`,
61+
`--stat-name-width=<name-width>` and `--stat-count=<count>`.
5662

5763
--numstat::
5864
Similar to `\--stat`, but shows number of added and

diff.c

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,9 +1316,10 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
13161316
int i, len, add, del, adds = 0, dels = 0;
13171317
uintmax_t max_change = 0, max_len = 0;
13181318
int total_files = data->nr;
1319-
int width, name_width;
1319+
int width, name_width, count;
13201320
const char *reset, *add_c, *del_c;
13211321
const char *line_prefix = "";
1322+
int extra_shown = 0;
13221323
struct strbuf *msg = NULL;
13231324

13241325
if (data->nr == 0)
@@ -1331,6 +1332,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
13311332

13321333
width = options->stat_width ? options->stat_width : 80;
13331334
name_width = options->stat_name_width ? options->stat_name_width : 50;
1335+
count = options->stat_count ? options->stat_count : data->nr;
13341336

13351337
/* Sanity: give at least 5 columns to the graph,
13361338
* but leave at least 10 columns for the name.
@@ -1347,9 +1349,14 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
13471349
add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
13481350
del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
13491351

1350-
for (i = 0; i < data->nr; i++) {
1352+
for (i = 0; (i < count) && (i < data->nr); i++) {
13511353
struct diffstat_file *file = data->files[i];
13521354
uintmax_t change = file->added + file->deleted;
1355+
if (!data->files[i]->is_renamed &&
1356+
(change == 0)) {
1357+
count++; /* not shown == room for one more */
1358+
continue;
1359+
}
13531360
fill_print_name(file);
13541361
len = strlen(file->print_name);
13551362
if (max_len < len)
@@ -1360,6 +1367,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
13601367
if (max_change < change)
13611368
max_change = change;
13621369
}
1370+
count = i; /* min(count, data->nr) */
13631371

13641372
/* Compute the width of the graph part;
13651373
* 10 is for one blank at the beginning of the line plus
@@ -1374,13 +1382,18 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
13741382
else
13751383
width = max_change;
13761384

1377-
for (i = 0; i < data->nr; i++) {
1385+
for (i = 0; i < count; i++) {
13781386
const char *prefix = "";
13791387
char *name = data->files[i]->print_name;
13801388
uintmax_t added = data->files[i]->added;
13811389
uintmax_t deleted = data->files[i]->deleted;
13821390
int name_len;
13831391

1392+
if (!data->files[i]->is_renamed &&
1393+
(added + deleted == 0)) {
1394+
total_files--;
1395+
continue;
1396+
}
13841397
/*
13851398
* "scale" the filename
13861399
*/
@@ -1415,11 +1428,6 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
14151428
fprintf(options->file, " Unmerged\n");
14161429
continue;
14171430
}
1418-
else if (!data->files[i]->is_renamed &&
1419-
(added + deleted == 0)) {
1420-
total_files--;
1421-
continue;
1422-
}
14231431

14241432
/*
14251433
* scale the add/delete
@@ -1441,6 +1449,20 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
14411449
show_graph(options->file, '-', del, del_c, reset);
14421450
fprintf(options->file, "\n");
14431451
}
1452+
for (i = count; i < data->nr; i++) {
1453+
uintmax_t added = data->files[i]->added;
1454+
uintmax_t deleted = data->files[i]->deleted;
1455+
if (!data->files[i]->is_renamed &&
1456+
(added + deleted == 0)) {
1457+
total_files--;
1458+
continue;
1459+
}
1460+
adds += added;
1461+
dels += deleted;
1462+
if (!extra_shown)
1463+
fprintf(options->file, "%s ...\n", line_prefix);
1464+
extra_shown = 1;
1465+
}
14441466
fprintf(options->file, "%s", line_prefix);
14451467
fprintf(options->file,
14461468
" %d files changed, %d insertions(+), %d deletions(-)\n",
@@ -3208,6 +3230,7 @@ static int stat_opt(struct diff_options *options, const char **av)
32083230
char *end;
32093231
int width = options->stat_width;
32103232
int name_width = options->stat_name_width;
3233+
int count = options->stat_count;
32113234
int argcount = 1;
32123235

32133236
arg += strlen("--stat");
@@ -3235,12 +3258,24 @@ static int stat_opt(struct diff_options *options, const char **av)
32353258
name_width = strtoul(av[1], &end, 10);
32363259
argcount = 2;
32373260
}
3261+
} else if (!prefixcmp(arg, "-count")) {
3262+
arg += strlen("-count");
3263+
if (*arg == '=')
3264+
count = strtoul(arg + 1, &end, 10);
3265+
else if (!*arg && !av[1])
3266+
die("Option '--stat-count' requires a value");
3267+
else if (!*arg) {
3268+
count = strtoul(av[1], &end, 10);
3269+
argcount = 2;
3270+
}
32383271
}
32393272
break;
32403273
case '=':
32413274
width = strtoul(arg+1, &end, 10);
32423275
if (*end == ',')
32433276
name_width = strtoul(end+1, &end, 10);
3277+
if (*end == ',')
3278+
count = strtoul(end+1, &end, 10);
32443279
}
32453280

32463281
/* Important! This checks all the error cases! */
@@ -3249,6 +3284,7 @@ static int stat_opt(struct diff_options *options, const char **av)
32493284
options->output_format |= DIFF_FORMAT_DIFFSTAT;
32503285
options->stat_name_width = name_width;
32513286
options->stat_width = width;
3287+
options->stat_count = count;
32523288
return argcount;
32533289
}
32543290

@@ -3313,7 +3349,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
33133349
else if (!strcmp(arg, "-s"))
33143350
options->output_format |= DIFF_FORMAT_NO_OUTPUT;
33153351
else if (!prefixcmp(arg, "--stat"))
3316-
/* --stat, --stat-width, or --stat-name-width */
3352+
/* --stat, --stat-width, --stat-name-width, or --stat-count */
33173353
return stat_opt(options, av);
33183354

33193355
/* renames options */

diff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ struct diff_options {
125125

126126
int stat_width;
127127
int stat_name_width;
128+
int stat_count;
128129
const char *word_regex;
129130
enum diff_words_type word_diff;
130131

t/t4049-diff-stat-count.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/sh
2+
# Copyright (c) 2011, Google Inc.
3+
4+
test_description='diff --stat-count'
5+
. ./test-lib.sh
6+
7+
test_expect_success setup '
8+
>a &&
9+
>b &&
10+
>c &&
11+
>d &&
12+
git add a b c d &&
13+
chmod +x c d &&
14+
echo a >a &&
15+
echo b >b &&
16+
cat >expect <<-\EOF
17+
a | 1 +
18+
b | 1 +
19+
2 files changed, 2 insertions(+), 0 deletions(-)
20+
EOF
21+
git diff --stat --stat-count=2 >actual &&
22+
test_cmp expect actual
23+
'
24+
25+
test_done

0 commit comments

Comments
 (0)