Skip to content

Commit 808e1db

Browse files
Michael J Grubergitster
authored andcommitted
diff: introduce --stat-lines to limit the stat lines
Often one is interested in the full --stat output only for commits which change a few files, but not others, because larger restructuring gives a --stat which fills a few screens. Introduce a new option --stat-count=<count> which limits the --stat output to the first <count> lines, followed by a "..." line. It can also be given as the third parameter in --stat=<width>,<name-width>,<count>. Also, the unstuck form is supported analogous to the other two stat parameters. Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 358e460 commit 808e1db

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

Documentation/diff-options.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ 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.
5659

5760
--numstat::
5861
Similar to `\--stat`, but shows number of added and

diff.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
12441244
int i, len, add, del, adds = 0, dels = 0;
12451245
uintmax_t max_change = 0, max_len = 0;
12461246
int total_files = data->nr;
1247-
int width, name_width;
1247+
int width, name_width, count;
12481248
const char *reset, *add_c, *del_c;
12491249
const char *line_prefix = "";
12501250
struct strbuf *msg = NULL;
@@ -1259,6 +1259,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
12591259

12601260
width = options->stat_width ? options->stat_width : 80;
12611261
name_width = options->stat_name_width ? options->stat_name_width : 50;
1262+
count = options->stat_count ? options->stat_count : data->nr;
12621263

12631264
/* Sanity: give at least 5 columns to the graph,
12641265
* but leave at least 10 columns for the name.
@@ -1275,11 +1276,12 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
12751276
add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
12761277
del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
12771278

1278-
for (i = 0; i < data->nr; i++) {
1279+
for (i = 0; (i < count) && (i < data->nr); i++) {
12791280
struct diffstat_file *file = data->files[i];
12801281
uintmax_t change = file->added + file->deleted;
12811282
if (!data->files[i]->is_renamed &&
12821283
(change == 0)) {
1284+
count++; /* not shown == room for one more */
12831285
continue;
12841286
}
12851287
fill_print_name(file);
@@ -1292,6 +1294,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
12921294
if (max_change < change)
12931295
max_change = change;
12941296
}
1297+
count = i; /* min(count, data->nr) */
12951298

12961299
/* Compute the width of the graph part;
12971300
* 10 is for one blank at the beginning of the line plus
@@ -1306,7 +1309,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
13061309
else
13071310
width = max_change;
13081311

1309-
for (i = 0; i < data->nr; i++) {
1312+
for (i = 0; i < count; i++) {
13101313
const char *prefix = "";
13111314
char *name = data->files[i]->print_name;
13121315
uintmax_t added = data->files[i]->added;
@@ -1373,6 +1376,19 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
13731376
show_graph(options->file, '-', del, del_c, reset);
13741377
fprintf(options->file, "\n");
13751378
}
1379+
if (count < data->nr)
1380+
fprintf(options->file, "%s ...\n", line_prefix);
1381+
for (i = count; i < data->nr; i++) {
1382+
uintmax_t added = data->files[i]->added;
1383+
uintmax_t deleted = data->files[i]->deleted;
1384+
if (!data->files[i]->is_renamed &&
1385+
(added + deleted == 0)) {
1386+
total_files--;
1387+
continue;
1388+
}
1389+
adds += added;
1390+
dels += deleted;
1391+
}
13761392
fprintf(options->file, "%s", line_prefix);
13771393
fprintf(options->file,
13781394
" %d files changed, %d insertions(+), %d deletions(-)\n",
@@ -3109,6 +3125,7 @@ static int stat_opt(struct diff_options *options, const char **av)
31093125
char *end;
31103126
int width = options->stat_width;
31113127
int name_width = options->stat_name_width;
3128+
int count = options->stat_count;
31123129
int argcount = 1;
31133130

31143131
arg += strlen("--stat");
@@ -3136,12 +3153,24 @@ static int stat_opt(struct diff_options *options, const char **av)
31363153
name_width = strtoul(av[1], &end, 10);
31373154
argcount = 2;
31383155
}
3156+
} else if (!prefixcmp(arg, "-count")) {
3157+
arg += strlen("-count");
3158+
if (*arg == '=')
3159+
count = strtoul(arg + 1, &end, 10);
3160+
else if (!*arg && !av[1])
3161+
die("Option '--stat-count' requires a value");
3162+
else if (!*arg) {
3163+
count = strtoul(av[1], &end, 10);
3164+
argcount = 2;
3165+
}
31393166
}
31403167
break;
31413168
case '=':
31423169
width = strtoul(arg+1, &end, 10);
31433170
if (*end == ',')
31443171
name_width = strtoul(end+1, &end, 10);
3172+
if (*end == ',')
3173+
count = strtoul(end+1, &end, 10);
31453174
}
31463175

31473176
/* Important! This checks all the error cases! */
@@ -3150,6 +3179,7 @@ static int stat_opt(struct diff_options *options, const char **av)
31503179
options->output_format |= DIFF_FORMAT_DIFFSTAT;
31513180
options->stat_name_width = name_width;
31523181
options->stat_width = width;
3182+
options->stat_count = count;
31533183
return argcount;
31543184
}
31553185

@@ -3195,7 +3225,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
31953225
else if (!strcmp(arg, "-s"))
31963226
options->output_format |= DIFF_FORMAT_NO_OUTPUT;
31973227
else if (!prefixcmp(arg, "--stat"))
3198-
/* --stat, --stat-width, or --stat-name-width */
3228+
/* --stat, --stat-width, --stat-name-width, or --stat-count */
31993229
return stat_opt(options, av);
32003230

32013231
/* renames options */

diff.h

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

125125
int stat_width;
126126
int stat_name_width;
127+
int stat_count;
127128
const char *word_regex;
128129
enum diff_words_type word_diff;
129130

0 commit comments

Comments
 (0)