Skip to content

Commit 909a194

Browse files
committed
range-diff: adjust the output of the commit pairs
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]>
1 parent b2a5a04 commit 909a194

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

range-diff.c

Lines changed: 59 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 */
@@ -251,9 +253,59 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
251253
free(b2a);
252254
}
253255

254-
static const char *short_oid(struct patch_util *util)
256+
static void output_pair_header(struct strbuf *buf,
257+
struct patch_util *a_util,
258+
struct patch_util *b_util)
255259
{
256-
return find_unique_abbrev(util->oid.hash, DEFAULT_ABBREV);
260+
static char *dashes;
261+
struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
262+
struct commit *commit;
263+
264+
if (!dashes) {
265+
char *p;
266+
267+
dashes = xstrdup(find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
268+
for (p = dashes; *p; p++)
269+
*p = '-';
270+
}
271+
272+
strbuf_reset(buf);
273+
if (!a_util)
274+
strbuf_addf(buf, "-: %s ", dashes);
275+
else
276+
strbuf_addf(buf, "%d: %s ", a_util->i + 1,
277+
find_unique_abbrev(a_util->oid.hash,
278+
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);
291+
else
292+
strbuf_addf(buf, " %d: %s", b_util->i + 1,
293+
find_unique_abbrev(b_util->oid.hash,
294+
DEFAULT_ABBREV));
295+
296+
commit = lookup_commit_reference(oid);
297+
if (commit) {
298+
const char *commit_buffer = get_commit_buffer(commit, NULL);
299+
const char *subject;
300+
301+
find_commit_subject(commit_buffer, &subject);
302+
strbuf_addch(buf, ' ');
303+
format_subject(buf, subject, " ");
304+
unuse_commit_buffer(commit, commit_buffer);
305+
}
306+
strbuf_addch(buf, '\n');
307+
308+
fwrite(buf->buf, buf->len, 1, stdout);
257309
}
258310

259311
static struct diff_filespec *get_filespec(const char *name, const char *p)
@@ -282,6 +334,7 @@ static void patch_diff(const char *a, const char *b,
282334
static void output(struct string_list *a, struct string_list *b,
283335
struct diff_options *diffopt)
284336
{
337+
struct strbuf buf = STRBUF_INIT;
285338
int i = 0, j = 0;
286339

287340
/*
@@ -303,32 +356,29 @@ static void output(struct string_list *a, struct string_list *b,
303356

304357
/* Show unmatched LHS commit whose predecessors were shown. */
305358
if (i < a->nr && a_util->matching < 0) {
306-
printf("%d: %s < -: --------\n",
307-
i + 1, short_oid(a_util));
359+
output_pair_header(&buf, a_util, NULL);
308360
i++;
309361
continue;
310362
}
311363

312364
/* Show unmatched RHS commits. */
313365
while (j < b->nr && b_util->matching < 0) {
314-
printf("-: -------- > %d: %s\n",
315-
j + 1, short_oid(b_util));
366+
output_pair_header(&buf, NULL, b_util);
316367
b_util = ++j < b->nr ? b->items[j].util : NULL;
317368
}
318369

319370
/* Show matching LHS/RHS pair. */
320371
if (j < b->nr) {
321372
a_util = a->items[b_util->matching].util;
322-
printf("%d: %s ! %d: %s\n",
323-
b_util->matching + 1, short_oid(a_util),
324-
j + 1, short_oid(b_util));
373+
output_pair_header(&buf, a_util, b_util);
325374
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
326375
patch_diff(a->items[b_util->matching].string,
327376
b->items[j].string, diffopt);
328377
a_util->shown = 1;
329378
j++;
330379
}
331380
}
381+
strbuf_release(&buf);
332382
}
333383

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

0 commit comments

Comments
 (0)