diff --stat: allow custom diffstat output width.
This adds two parameters to "diff --stat". . --stat-width=72 tells that the page should fit on 72-column output. . --stat-name-width=30 tells that the filename part is limited to 30 columns. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 26, 2006 at 18:53 UTC
a2540023dcf833ca02d87c28052e8b7135d56b60
2 files changed
+96
-38
diff.c
+93
-38
@@ -545,21 +545,64 @@ static void diffstat_consume(void *priv, char *line, unsigned long len)
545
x->deleted++;
546
}
547
548
-static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
549
-static const char minuses[]= "----------------------------------------------------------------------";
548
const char mime_boundary_leader[] = "------------";
549
552
-static void show_stats(struct diffstat_t* data)
550
+static int scale_linear(int it, int width, int max_change)
551
+{
552
+ /*
553
+ * round(width * it / max_change);
554
+ */
555
+ return (it * width * 2 + max_change) / (max_change * 2);
556
+}
557
+
558
+static void show_name(const char *prefix, const char *name, int len)
559
+{
560
+ printf(" %s%-*s |", prefix, len, name);
561
+}
562
+
563
+static void show_graph(char ch, int cnt)
564
+{
565
+ if (cnt <= 0)
566
+ return;
567
+ while (cnt--)
568
+ putchar(ch);
569
+}
570
+
571
+static void show_stats(struct diffstat_t* data, struct diff_options *options)
572
{
573
int i, len, add, del, total, adds = 0, dels = 0;
555
- int max, max_change = 0, max_len = 0;
574
+ int max_change = 0, max_len = 0;
575
int total_files = data->nr;
576
+ int width, name_width;
577
578
if (data->nr == 0)
579
return;
580
581
+ width = options->stat_width ? options->stat_width : 80;
582
+ name_width = options->stat_name_width ? options->stat_name_width : 50;
583
+
584
+ /* Sanity: give at least 5 columns to the graph,
585
+ * but leave at least 10 columns for the name.
586
+ */
587
+ if (width < name_width + 15) {
588
+ if (name_width <= 25)
589
+ width = name_width + 15;
590
+ else
591
+ name_width = width - 15;
592
+ }
593
+
594
+ /* Find the longest filename and max number of changes */
595
for (i = 0; i < data->nr; i++) {
596
struct diffstat_file *file = data->files[i];
597
+ int change = file->added + file->deleted;
598
+
599
+ len = quote_c_style(file->name, NULL, NULL, 0);
600
+ if (len) {
601
+ char *qname = xmalloc(len + 1);
602
+ quote_c_style(file->name, qname, NULL, 0);
603
+ free(file->name);
604
+ file->name = qname;
605
+ }
606
607
len = strlen(file->name);
608
if (max_len < len)
@@ -567,54 +610,53 @@ static void show_stats(struct diffstat_t* data)
610
611
if (file->is_binary || file->is_unmerged)
612
continue;
570
- if (max_change < file->added + file->deleted)
571
- max_change = file->added + file->deleted;
613
+ if (max_change < change)
614
+ max_change = change;
615
}
616
617
+ /* Compute the width of the graph part;
618
+ * 10 is for one blank at the beginning of the line plus
619
+ * " | count " between the name and the graph.
620
+ *
621
+ * From here on, name_width is the width of the name area,
622
+ * and width is the width of the graph area.
623
+ */
624
+ name_width = (name_width < max_len) ? name_width : max_len;
625
+ if (width < (name_width + 10) + max_change)
626
+ width = width - (name_width + 10);
627
+ else
628
+ width = max_change;
629
+
630
for (i = 0; i < data->nr; i++) {
631
const char *prefix = "";
632
char *name = data->files[i]->name;
633
int added = data->files[i]->added;
634
int deleted = data->files[i]->deleted;
579
-
580
- if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
581
- char *qname = xmalloc(len + 1);
582
- quote_c_style(name, qname, NULL, 0);
583
- free(name);
584
- data->files[i]->name = name = qname;
585
- }
635
+ int name_len;
636
637
/*
638
* "scale" the filename
639
*/
590
- len = strlen(name);
591
- max = max_len;
592
- if (max > 50)
593
- max = 50;
594
- if (len > max) {
640
+ len = name_width;
641
+ name_len = strlen(name);
642
+ if (name_width < name_len) {
643
char *slash;
644
prefix = "...";
597
- max -= 3;
598
- name += len - max;
645
+ len -= 3;
646
+ name += name_len - len;
647
slash = strchr(name, '/');
648
if (slash)
649
name = slash;
650
}
603
- len = max;
604
-
605
- /*
606
- * scale the add/delete
607
- */
608
- max = max_change;
609
- if (max + len > 70)
610
- max = 70 - len;
651
652
if (data->files[i]->is_binary) {
613
- printf(" %s%-*s | Bin\n", prefix, len, name);
653
+ show_name(prefix, name, len);
654
+ printf(" Bin\n");
655
goto free_diffstat_file;
656
}
657
else if (data->files[i]->is_unmerged) {
617
- printf(" %s%-*s | Unmerged\n", prefix, len, name);
658
+ show_name(prefix, name, len);
659
+ printf(" Unmerged\n");
660
goto free_diffstat_file;
661
}
662
else if (!data->files[i]->is_renamed &&
@@ -623,27 +665,32 @@ static void show_stats(struct diffstat_t* data)
665
goto free_diffstat_file;
666
}
667
668
+ /*
669
+ * scale the add/delete
670
+ */
671
add = added;
672
del = deleted;
673
total = add + del;
674
adds += add;
675
dels += del;
676
632
- if (max_change > 0) {
633
- total = (total * max + max_change / 2) / max_change;
634
- add = (add * max + max_change / 2) / max_change;
677
+ if (width <= max_change) {
678
+ total = scale_linear(total, width, max_change);
679
+ add = scale_linear(add, width, max_change);
680
del = total - add;
681
}
637
- printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
638
- len, name, added + deleted,
639
- add, pluses, del, minuses);
682
+ show_name(prefix, name, len);
683
+ printf("%5d ", added + deleted);
684
+ show_graph('+', add);
685
+ show_graph('-', del);
686
+ putchar('\n');
687
free_diffstat_file:
688
free(data->files[i]->name);
689
free(data->files[i]);
690
}
691
free(data->files);
692
printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
646
- total_files, adds, dels);
693
+ total_files, adds, dels);
694
}
695
696
struct checkdiff_t {
@@ -1681,6 +1728,14 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1728
}
1729
else if (!strcmp(arg, "--stat"))
1730
options->output_format |= DIFF_FORMAT_DIFFSTAT;
1731
+ else if (!strncmp(arg, "--stat-width=", 13)) {
1732
+ options->stat_width = strtoul(arg + 13, NULL, 10);
1733
+ options->output_format |= DIFF_FORMAT_DIFFSTAT;
1734
+ }
1735
+ else if (!strncmp(arg, "--stat-name-width=", 18)) {
1736
+ options->stat_name_width = strtoul(arg + 18, NULL, 10);
1737
+ options->output_format |= DIFF_FORMAT_DIFFSTAT;
1738
+ }
1739
else if (!strcmp(arg, "--check"))
1740
options->output_format |= DIFF_FORMAT_CHECKDIFF;
1741
else if (!strcmp(arg, "--summary"))
@@ -2438,7 +2493,7 @@ void diff_flush(struct diff_options *options)
2493
if (check_pair_status(p))
2494
diff_flush_stat(p, options, &diffstat);
2495
}
2441
- show_stats(&diffstat);
2496
+ show_stats(&diffstat, options);
2497
separator++;
2498
}
2499
diff.h
+3
@@ -69,6 +69,9 @@ struct diff_options {
69
const char *stat_sep;
70
long xdl_opts;
71
72
+ int stat_width;
73
+ int stat_name_width;
74
+
75
int nr_paths;
76
const char **paths;
77
int *pathlens;