Skip to content

Commit a559f79

Browse files
committed
range-diff: improve the order of the shown commits
This patch lets `git range-diff` use the same order as tbdiff. The idea is simple: for left-to-right readers, it is natural to assume that the `git range-diff` is performed between an older vs a newer version of the branch. As such, the user is probably more interested in the question "where did this come from?" rather than "where did that one go?". To that end, we list the commits in the order of the second commit range ("the newer version"), inserting the unmatched commits of the first commit range as soon as all their predecessors have been shown. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 86bfce1 commit a559f79

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

range-diff.c

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct patch_util {
1212
struct hashmap_entry e;
1313
const char *diff, *patch;
1414

15-
int i;
15+
int i, shown;
1616
int diffsize;
1717
size_t diff_offset;
1818
/* the index of the matching item in the other branch, or -1 */
@@ -256,28 +256,49 @@ static const char *short_oid(struct patch_util *util)
256256

257257
static void output(struct string_list *a, struct string_list *b)
258258
{
259-
int i;
260-
261-
for (i = 0; i < b->nr; i++) {
262-
struct patch_util *util = b->items[i].util, *prev;
259+
int i = 0, j = 0;
260+
261+
/*
262+
* We assume the user is really more interested in the second argument
263+
* ("newer" version). To that end, we print the output in the order of
264+
* the RHS (the `b` parameter). To put the LHS (the `a` parameter)
265+
* commits that are no longer in the RHS into a good place, we place
266+
* them once we have shown all of their predecessors in the LHS.
267+
*/
268+
269+
while (i < a->nr || j < b->nr) {
270+
struct patch_util *a_util, *b_util;
271+
a_util = i < a->nr ? a->items[i].util : NULL;
272+
b_util = j < b->nr ? b->items[j].util : NULL;
273+
274+
/* Skip all the already-shown commits from the LHS. */
275+
while (i < a->nr && a_util->shown)
276+
a_util = ++i < a->nr ? a->items[i].util : NULL;
277+
278+
/* Show unmatched LHS commit whose predecessors were shown. */
279+
if (i < a->nr && a_util->matching < 0) {
280+
printf("%d: %s < -: --------\n",
281+
i + 1, short_oid(a_util));
282+
i++;
283+
continue;
284+
}
263285

264-
if (util->matching < 0)
286+
/* Show unmatched RHS commits. */
287+
while (j < b->nr && b_util->matching < 0) {
265288
printf("-: -------- > %d: %s\n",
266-
i + 1, short_oid(util));
267-
else {
268-
prev = a->items[util->matching].util;
269-
printf("%d: %s ! %d: %s\n",
270-
util->matching + 1, short_oid(prev),
271-
i + 1, short_oid(util));
289+
j + 1, short_oid(b_util));
290+
b_util = ++j < b->nr ? b->items[j].util : NULL;
272291
}
273-
}
274-
275-
for (i = 0; i < a->nr; i++) {
276-
struct patch_util *util = a->items[i].util;
277292

278-
if (util->matching < 0)
279-
printf("%d: %s < -: --------\n",
280-
i + 1, short_oid(util));
293+
/* Show matching LHS/RHS pair. */
294+
if (j < b->nr) {
295+
a_util = a->items[b_util->matching].util;
296+
printf("%d: %s ! %d: %s\n",
297+
b_util->matching + 1, short_oid(a_util),
298+
j + 1, short_oid(b_util));
299+
a_util->shown = 1;
300+
j++;
301+
}
281302
}
282303
}
283304

0 commit comments

Comments
 (0)