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>

Michael J Gruber committed May 27, 2011 at 14:36 UTC 808e1db231195af31075d1e0f9e4f6026a96d06a
3 files changed +39 -5
Documentation/diff-options.txt
+4 -1
@@ -48,11 +48,14 @@ endif::git-format-patch[]
48 --patience::
49 Generate a diff using the "patience diff" algorithm.
50
51 ---stat[=<width>[,<name-width>]]::
51 +--stat[=<width>[,<name-width>[,<count>]]]::
52 Generate a diffstat. You can override the default
53 output width for 80-column terminal by `--stat=<width>`.
54 The width of the filename part can be controlled by
55 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 --numstat::
61 Similar to `\--stat`, but shows number of added and
diff.c
+34 -4
@@ -1244,7 +1244,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1244 int i, len, add, del, adds = 0, dels = 0;
1245 uintmax_t max_change = 0, max_len = 0;
1246 int total_files = data->nr;
1247 - int width, name_width;
1247 + int width, name_width, count;
1248 const char *reset, *add_c, *del_c;
1249 const char *line_prefix = "";
1250 struct strbuf *msg = NULL;
@@ -1259,6 +1259,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1259
1260 width = options->stat_width ? options->stat_width : 80;
1261 name_width = options->stat_name_width ? options->stat_name_width : 50;
1262 + count = options->stat_count ? options->stat_count : data->nr;
1263
1264 /* Sanity: give at least 5 columns to the graph,
1265 * 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)
1276 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
1277 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
1278
1278 - for (i = 0; i < data->nr; i++) {
1279 + for (i = 0; (i < count) && (i < data->nr); i++) {
1280 struct diffstat_file *file = data->files[i];
1281 uintmax_t change = file->added + file->deleted;
1282 if (!data->files[i]->is_renamed &&
1283 (change == 0)) {
1284 + count++; /* not shown == room for one more */
1285 continue;
1286 }
1287 fill_print_name(file);
@@ -1292,6 +1294,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1294 if (max_change < change)
1295 max_change = change;
1296 }
1297 + count = i; /* min(count, data->nr) */
1298
1299 /* Compute the width of the graph part;
1300 * 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)
1309 else
1310 width = max_change;
1311
1309 - for (i = 0; i < data->nr; i++) {
1312 + for (i = 0; i < count; i++) {
1313 const char *prefix = "";
1314 char *name = data->files[i]->print_name;
1315 uintmax_t added = data->files[i]->added;
@@ -1373,6 +1376,19 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
1376 show_graph(options->file, '-', del, del_c, reset);
1377 fprintf(options->file, "\n");
1378 }
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 + }
1392 fprintf(options->file, "%s", line_prefix);
1393 fprintf(options->file,
1394 " %d files changed, %d insertions(+), %d deletions(-)\n",
@@ -3109,6 +3125,7 @@ static int stat_opt(struct diff_options *options, const char **av)
3125 char *end;
3126 int width = options->stat_width;
3127 int name_width = options->stat_name_width;
3128 + int count = options->stat_count;
3129 int argcount = 1;
3130
3131 arg += strlen("--stat");
@@ -3136,12 +3153,24 @@ static int stat_opt(struct diff_options *options, const char **av)
3153 name_width = strtoul(av[1], &end, 10);
3154 argcount = 2;
3155 }
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 + }
3166 }
3167 break;
3168 case '=':
3169 width = strtoul(arg+1, &end, 10);
3170 if (*end == ',')
3171 name_width = strtoul(end+1, &end, 10);
3172 + if (*end == ',')
3173 + count = strtoul(end+1, &end, 10);
3174 }
3175
3176 /* Important! This checks all the error cases! */
@@ -3150,6 +3179,7 @@ static int stat_opt(struct diff_options *options, const char **av)
3179 options->output_format |= DIFF_FORMAT_DIFFSTAT;
3180 options->stat_name_width = name_width;
3181 options->stat_width = width;
3182 + options->stat_count = count;
3183 return argcount;
3184 }
3185
@@ -3195,7 +3225,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3225 else if (!strcmp(arg, "-s"))
3226 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
3227 else if (!prefixcmp(arg, "--stat"))
3198 - /* --stat, --stat-width, or --stat-name-width */
3228 + /* --stat, --stat-width, --stat-name-width, or --stat-count */
3229 return stat_opt(options, av);
3230
3231 /* renames options */
diff.h
+1
@@ -124,6 +124,7 @@ struct diff_options {
124
125 int stat_width;
126 int stat_name_width;
127 + int stat_count;
128 const char *word_regex;
129 enum diff_words_type word_diff;
130