Skip to content

Commit a67c235

Browse files
committed
Merge branch 'jc/diff-stat-scaler' into maint
* jc/diff-stat-scaler: diff --stat: show bars of same length for paths with same amount of changes
2 parents c17ff2a + 2eeeef2 commit a67c235

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
@@ -1276,13 +1276,15 @@ const char mime_boundary_leader[] = "------------";
12761276

12771277
static int scale_linear(int it, int width, int max_change)
12781278
{
1279+
if (!it)
1280+
return 0;
12791281
/*
1280-
* make sure that at least one '-' is printed if there were deletions,
1281-
* and likewise for '+'.
1282+
* make sure that at least one '-' or '+' is printed if
1283+
* there is any change to this path. The easiest way is to
1284+
* scale linearly as if the alloted width is one column shorter
1285+
* than it is, and then add 1 to the result.
12821286
*/
1283-
if (max_change < 2)
1284-
return it;
1285-
return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
1287+
return 1 + (it * (width - 1) / max_change);
12861288
}
12871289

12881290
static void show_name(FILE *file,
@@ -1498,8 +1500,19 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
14981500
dels += del;
14991501

15001502
if (width <= max_change) {
1501-
add = scale_linear(add, width, max_change);
1502-
del = scale_linear(del, width, max_change);
1503+
int total = add + del;
1504+
1505+
total = scale_linear(add + del, width, max_change);
1506+
if (total < 2 && add && del)
1507+
/* width >= 2 due to the sanity check */
1508+
total = 2;
1509+
if (add < del) {
1510+
add = scale_linear(add, width, max_change);
1511+
del = total - add;
1512+
} else {
1513+
del = scale_linear(del, width, max_change);
1514+
add = total - del;
1515+
}
15031516
}
15041517
fprintf(options->file, "%s", line_prefix);
15051518
show_name(options->file, prefix, name, len);

0 commit comments

Comments
 (0)