Skip to content

Commit eb0be38

Browse files
dschogitster
authored andcommitted
range-diff: adjust the output of the commit pairs
This not only uses "dashed stand-ins" for "pairs" where one side is missing (i.e. unmatched commits that are present only in one of the two commit ranges), but also adds onelines for the reader's pleasure. This change brings `git range-diff` yet another step closer to feature parity with tbdiff: it now shows the oneline, too, and indicates with `=` when the commits have identical diffs. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1cdde29 commit eb0be38

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

range-diff.c

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "xdiff-interface.h"
88
#include "linear-assignment.h"
99
#include "diffcore.h"
10+
#include "commit.h"
11+
#include "pretty.h"
1012

1113
struct patch_util {
1214
/* For the search for an exact match */
@@ -255,9 +257,49 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
255257
free(b2a);
256258
}
257259

258-
static const char *short_oid(struct patch_util *util)
260+
static void output_pair_header(struct strbuf *buf,
261+
struct strbuf *dashes,
262+
struct patch_util *a_util,
263+
struct patch_util *b_util)
259264
{
260-
return find_unique_abbrev(&util->oid, DEFAULT_ABBREV);
265+
struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
266+
struct commit *commit;
267+
268+
if (!dashes->len)
269+
strbuf_addchars(dashes, '-',
270+
strlen(find_unique_abbrev(oid,
271+
DEFAULT_ABBREV)));
272+
273+
strbuf_reset(buf);
274+
if (!a_util)
275+
strbuf_addf(buf, "-: %s ", dashes->buf);
276+
else
277+
strbuf_addf(buf, "%d: %s ", a_util->i + 1,
278+
find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
279+
280+
if (!a_util)
281+
strbuf_addch(buf, '>');
282+
else if (!b_util)
283+
strbuf_addch(buf, '<');
284+
else if (strcmp(a_util->patch, b_util->patch))
285+
strbuf_addch(buf, '!');
286+
else
287+
strbuf_addch(buf, '=');
288+
289+
if (!b_util)
290+
strbuf_addf(buf, " -: %s", dashes->buf);
291+
else
292+
strbuf_addf(buf, " %d: %s", b_util->i + 1,
293+
find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
294+
295+
commit = lookup_commit_reference(the_repository, oid);
296+
if (commit) {
297+
strbuf_addch(buf, ' ');
298+
pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
299+
}
300+
strbuf_addch(buf, '\n');
301+
302+
fwrite(buf->buf, buf->len, 1, stdout);
261303
}
262304

263305
static struct diff_filespec *get_filespec(const char *name, const char *p)
@@ -286,6 +328,7 @@ static void patch_diff(const char *a, const char *b,
286328
static void output(struct string_list *a, struct string_list *b,
287329
struct diff_options *diffopt)
288330
{
331+
struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
289332
int i = 0, j = 0;
290333

291334
/*
@@ -307,32 +350,30 @@ static void output(struct string_list *a, struct string_list *b,
307350

308351
/* Show unmatched LHS commit whose predecessors were shown. */
309352
if (i < a->nr && a_util->matching < 0) {
310-
printf("%d: %s < -: --------\n",
311-
i + 1, short_oid(a_util));
353+
output_pair_header(&buf, &dashes, a_util, NULL);
312354
i++;
313355
continue;
314356
}
315357

316358
/* Show unmatched RHS commits. */
317359
while (j < b->nr && b_util->matching < 0) {
318-
printf("-: -------- > %d: %s\n",
319-
j + 1, short_oid(b_util));
360+
output_pair_header(&buf, &dashes, NULL, b_util);
320361
b_util = ++j < b->nr ? b->items[j].util : NULL;
321362
}
322363

323364
/* Show matching LHS/RHS pair. */
324365
if (j < b->nr) {
325366
a_util = a->items[b_util->matching].util;
326-
printf("%d: %s ! %d: %s\n",
327-
b_util->matching + 1, short_oid(a_util),
328-
j + 1, short_oid(b_util));
367+
output_pair_header(&buf, &dashes, a_util, b_util);
329368
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
330369
patch_diff(a->items[b_util->matching].string,
331370
b->items[j].string, diffopt);
332371
a_util->shown = 1;
333372
j++;
334373
}
335374
}
375+
strbuf_release(&buf);
376+
strbuf_release(&dashes);
336377
}
337378

338379
int show_range_diff(const char *range1, const char *range2,

0 commit comments

Comments
 (0)