Skip to content

Commit 8c60fcb

Browse files
committed
Merge branch 'jc/diff-stat-scaler'
* jc/diff-stat-scaler: diff --stat: show bars of same length for paths with same amount of changes
2 parents 4d9e079 + 2eeeef2 commit 8c60fcb

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

diff.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,13 +1273,15 @@ const char mime_boundary_leader[] = "------------";
12731273

12741274
static int scale_linear(int it, int width, int max_change)
12751275
{
1276+
if (!it)
1277+
return 0;
12761278
/*
1277-
* make sure that at least one '-' is printed if there were deletions,
1278-
* and likewise for '+'.
1279+
* make sure that at least one '-' or '+' is printed if
1280+
* there is any change to this path. The easiest way is to
1281+
* scale linearly as if the alloted width is one column shorter
1282+
* than it is, and then add 1 to the result.
12791283
*/
1280-
if (max_change < 2)
1281-
return it;
1282-
return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
1284+
return 1 + (it * (width - 1) / max_change);
12831285
}
12841286

12851287
static void show_name(FILE *file,
@@ -1495,8 +1497,19 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
14951497
dels += del;
14961498

14971499
if (width <= max_change) {
1498-
add = scale_linear(add, width, max_change);
1499-
del = scale_linear(del, width, max_change);
1500+
int total = add + del;
1501+
1502+
total = scale_linear(add + del, width, max_change);
1503+
if (total < 2 && add && del)
1504+
/* width >= 2 due to the sanity check */
1505+
total = 2;
1506+
if (add < del) {
1507+
add = scale_linear(add, width, max_change);
1508+
del = total - add;
1509+
} else {
1510+
del = scale_linear(del, width, max_change);
1511+
add = total - del;
1512+
}
15001513
}
15011514
fprintf(options->file, "%s", line_prefix);
15021515
show_name(options->file, prefix, name, len);

0 commit comments

Comments
 (0)