@matheusbsbs / matheus-bagliano / commits / 3ed74e608a
Fork 0

diff --stat: ensure at least one '-' for deletions, and one '+' for additions

The number of '-' and '+' is still linear. The idea is that scaled-length := floor(a * length + b) with the following constraints: if length == 1, scaled-length == 1, and the combined length of plusses and minusses should not be larger than the width by a small margin. Thus, a + b == 1 and a * max_plusses + b + a * max_minusses + b = width + 1 The solution is a * x + b = ((width - 1) * (x - 1) + max_change - 1) / (max_change - 1) Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Sep 28, 2006 at 17:37 UTC 3ed74e608a69ce0f10bfad2e4ef6cf99eec04613
1 file changed +7 -4
diff.c
+7 -4
@@ -550,9 +550,12 @@ const char mime_boundary_leader[] = "------------";
550 static int scale_linear(int it, int width, int max_change)
551 {
552 /*
553 - * round(width * it / max_change);
553 + * make sure that at least one '-' is printed if there were deletions,
554 + * and likewise for '+'.
555 */
555 - return (it * width * 2 + max_change) / (max_change * 2);
556 + if (max_change < 2)
557 + return it;
558 + return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
559 }
560
561 static void show_name(const char *prefix, const char *name, int len,
@@ -684,9 +687,9 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
687 dels += del;
688
689 if (width <= max_change) {
687 - total = scale_linear(total, width, max_change);
690 add = scale_linear(add, width, max_change);
689 - del = total - add;
691 + del = scale_linear(del, width, max_change);
692 + total = add + del;
693 }
694 show_name(prefix, name, len, reset, set);
695 printf("%5d ", added + deleted);