]> git.ipfire.org Git - thirdparty/git.git/blame - range-diff.c
Merge branch 'ab/pager-exit-log'
[thirdparty/git.git] / range-diff.c
CommitLineData
d9c66f0b
JS
1#include "cache.h"
2#include "range-diff.h"
3#include "string-list.h"
4#include "run-command.h"
dbbcd44f 5#include "strvec.h"
d9c66f0b
JS
6#include "hashmap.h"
7#include "xdiff-interface.h"
8#include "linear-assignment.h"
c8c5e43a 9#include "diffcore.h"
eb0be38c
JS
10#include "commit.h"
11#include "pretty.h"
4eba1fe6 12#include "userdiff.h"
b66885a3 13#include "apply.h"
359f0d75 14#include "revision.h"
d9c66f0b
JS
15
16struct patch_util {
17 /* For the search for an exact match */
18 struct hashmap_entry e;
19 const char *diff, *patch;
20
9dc46e02 21 int i, shown;
d9c66f0b
JS
22 int diffsize;
23 size_t diff_offset;
24 /* the index of the matching item in the other branch, or -1 */
25 int matching;
26 struct object_id oid;
27};
28
44b67cb6
TG
29static size_t find_end_of_line(char *buffer, unsigned long size)
30{
31 char *eol = memchr(buffer, '\n', size);
32
33 if (!eol)
34 return size;
35
36 *eol = '\0';
37 return eol + 1 - buffer;
38}
39
d9c66f0b
JS
40/*
41 * Reads the patches into a string list, with the `util` field being populated
42 * as struct object_id (will need to be free()d).
43 */
bd361918 44static int read_patches(const char *range, struct string_list *list,
c972bf4c 45 const struct strvec *other_arg)
d9c66f0b
JS
46{
47 struct child_process cp = CHILD_PROCESS_INIT;
44b67cb6 48 struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
d9c66f0b
JS
49 struct patch_util *util = NULL;
50 int in_header = 1;
444e0969 51 char *line, *current_filename = NULL;
44b67cb6
TG
52 int offset, len;
53 size_t size;
d9c66f0b 54
c972bf4c 55 strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
f6d8942b
JK
56 "--reverse", "--date-order", "--decorate=no",
57 "--no-prefix",
58 /*
59 * Choose indicators that are not used anywhere
60 * else in diffs, but still look reasonable
61 * (e.g. will not be confusing when debugging)
62 */
63 "--output-indicator-new=>",
64 "--output-indicator-old=<",
65 "--output-indicator-context=#",
66 "--no-abbrev-commit",
67 "--pretty=medium",
68 "--notes",
69 NULL);
bd361918 70 if (other_arg)
d70a9eb6 71 strvec_pushv(&cp.args, other_arg->v);
c972bf4c 72 strvec_push(&cp.args, range);
d9c66f0b
JS
73 cp.out = -1;
74 cp.no_stdin = 1;
75 cp.git_cmd = 1;
76
77 if (start_command(&cp))
78 return error_errno(_("could not start `log`"));
44b67cb6 79 if (strbuf_read(&contents, cp.out, 0) < 0) {
d9c66f0b
JS
80 error_errno(_("could not read `log` output"));
81 finish_command(&cp);
82 return -1;
83 }
5189bb87
JS
84 if (finish_command(&cp))
85 return -1;
d9c66f0b 86
44b67cb6
TG
87 line = contents.buf;
88 size = contents.len;
89 for (offset = 0; size > 0; offset += len, size -= len, line += len) {
d9c66f0b
JS
90 const char *p;
91
44b67cb6
TG
92 len = find_end_of_line(line, size);
93 line[len - 1] = '\0';
94 if (skip_prefix(line, "commit ", &p)) {
d9c66f0b
JS
95 if (util) {
96 string_list_append(list, buf.buf)->util = util;
97 strbuf_reset(&buf);
98 }
99 util = xcalloc(sizeof(*util), 1);
100 if (get_oid(p, &util->oid)) {
101 error(_("could not parse commit '%s'"), p);
102 free(util);
8c29b497 103 free(current_filename);
d9c66f0b
JS
104 string_list_clear(list, 1);
105 strbuf_release(&buf);
44b67cb6 106 strbuf_release(&contents);
d9c66f0b
JS
107 return -1;
108 }
109 util->matching = -1;
110 in_header = 1;
111 continue;
112 }
113
8cf51561
VD
114 if (!util) {
115 error(_("could not parse first line of `log` output: "
116 "did not start with 'commit ': '%s'"),
117 line);
8c29b497 118 free(current_filename);
8cf51561
VD
119 string_list_clear(list, 1);
120 strbuf_release(&buf);
121 strbuf_release(&contents);
8cf51561
VD
122 return -1;
123 }
124
44b67cb6 125 if (starts_with(line, "diff --git")) {
b66885a3
TG
126 struct patch patch = { 0 };
127 struct strbuf root = STRBUF_INIT;
128 int linenr = 0;
8d1675eb 129 int orig_len;
b66885a3 130
d9c66f0b
JS
131 in_header = 0;
132 strbuf_addch(&buf, '\n');
133 if (!util->diff_offset)
134 util->diff_offset = buf.len;
b66885a3 135 line[len - 1] = '\n';
8d1675eb 136 orig_len = len;
937b76ed 137 len = parse_git_diff_header(&root, &linenr, 0, line,
b66885a3 138 len, size, &patch);
a2d474ad
JS
139 if (len < 0) {
140 error(_("could not parse git header '%.*s'"),
141 orig_len, line);
142 free(util);
143 free(current_filename);
144 string_list_clear(list, 1);
145 strbuf_release(&buf);
146 strbuf_release(&contents);
147 return -1;
148 }
b66885a3
TG
149 strbuf_addstr(&buf, " ## ");
150 if (patch.is_new > 0)
151 strbuf_addf(&buf, "%s (new)", patch.new_name);
152 else if (patch.is_delete > 0)
153 strbuf_addf(&buf, "%s (deleted)", patch.old_name);
154 else if (patch.is_rename)
155 strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
156 else
157 strbuf_addstr(&buf, patch.new_name);
158
444e0969
TG
159 free(current_filename);
160 if (patch.is_delete > 0)
161 current_filename = xstrdup(patch.old_name);
162 else
163 current_filename = xstrdup(patch.new_name);
164
b66885a3
TG
165 if (patch.new_mode && patch.old_mode &&
166 patch.old_mode != patch.new_mode)
167 strbuf_addf(&buf, " (mode change %06o => %06o)",
168 patch.old_mode, patch.new_mode);
169
170 strbuf_addstr(&buf, " ##");
d9c66f0b 171 } else if (in_header) {
44b67cb6 172 if (starts_with(line, "Author: ")) {
499352c2 173 strbuf_addstr(&buf, " ## Metadata ##\n");
44b67cb6 174 strbuf_addstr(&buf, line);
d9c66f0b 175 strbuf_addstr(&buf, "\n\n");
499352c2 176 strbuf_addstr(&buf, " ## Commit message ##\n");
9f726e1b
DL
177 } else if (starts_with(line, "Notes") &&
178 line[strlen(line) - 1] == ':') {
179 strbuf_addstr(&buf, "\n\n");
180 /* strip the trailing colon */
181 strbuf_addf(&buf, " ## %.*s ##\n",
182 (int)(strlen(line) - 1), line);
44b67cb6
TG
183 } else if (starts_with(line, " ")) {
184 p = line + len - 2;
185 while (isspace(*p) && p >= line)
186 p--;
187 strbuf_add(&buf, line, p - line + 1);
d9c66f0b
JS
188 strbuf_addch(&buf, '\n');
189 }
190 continue;
e1db2630
TG
191 } else if (skip_prefix(line, "@@ ", &p)) {
192 p = strstr(p, "@@");
444e0969
TG
193 strbuf_addstr(&buf, "@@");
194 if (current_filename && p[2])
195 strbuf_addf(&buf, " %s:", current_filename);
196 if (p)
197 strbuf_addstr(&buf, p + 2);
b66885a3 198 } else if (!line[0])
d9c66f0b
JS
199 /*
200 * A completely blank (not ' \n', which is context)
201 * line is not valid in a diff. We skip it
202 * silently, because this neatly handles the blank
203 * separator line between commits in git-log
204 * output.
d9c66f0b
JS
205 */
206 continue;
44b67cb6 207 else if (line[0] == '>') {
8d5ccb59 208 strbuf_addch(&buf, '+');
44b67cb6
TG
209 strbuf_addstr(&buf, line + 1);
210 } else if (line[0] == '<') {
8d5ccb59 211 strbuf_addch(&buf, '-');
44b67cb6
TG
212 strbuf_addstr(&buf, line + 1);
213 } else if (line[0] == '#') {
8d5ccb59 214 strbuf_addch(&buf, ' ');
44b67cb6 215 strbuf_addstr(&buf, line + 1);
8d5ccb59 216 } else {
2543a641 217 strbuf_addch(&buf, ' ');
44b67cb6 218 strbuf_addstr(&buf, line);
8d5ccb59 219 }
d9c66f0b
JS
220
221 strbuf_addch(&buf, '\n');
222 util->diffsize++;
223 }
44b67cb6 224 strbuf_release(&contents);
d9c66f0b
JS
225
226 if (util)
227 string_list_append(list, buf.buf)->util = util;
228 strbuf_release(&buf);
444e0969 229 free(current_filename);
d9c66f0b 230
d9c66f0b
JS
231 return 0;
232}
233
234static int patch_util_cmp(const void *dummy, const struct patch_util *a,
1ca69225 235 const struct patch_util *b, const char *keydata)
d9c66f0b
JS
236{
237 return strcmp(a->diff, keydata ? keydata : b->diff);
238}
239
240static void find_exact_matches(struct string_list *a, struct string_list *b)
241{
b19315d8 242 struct hashmap map = HASHMAP_INIT((hashmap_cmp_fn)patch_util_cmp, NULL);
d9c66f0b
JS
243 int i;
244
d9c66f0b
JS
245 /* First, add the patches of a to a hash map */
246 for (i = 0; i < a->nr; i++) {
247 struct patch_util *util = a->items[i].util;
248
249 util->i = i;
250 util->patch = a->items[i].string;
251 util->diff = util->patch + util->diff_offset;
d22245a2 252 hashmap_entry_init(&util->e, strhash(util->diff));
b94e5c1d 253 hashmap_add(&map, &util->e);
d9c66f0b
JS
254 }
255
256 /* Now try to find exact matches in b */
257 for (i = 0; i < b->nr; i++) {
258 struct patch_util *util = b->items[i].util, *other;
259
260 util->i = i;
261 util->patch = b->items[i].string;
262 util->diff = util->patch + util->diff_offset;
d22245a2 263 hashmap_entry_init(&util->e, strhash(util->diff));
404ab78e 264 other = hashmap_remove_entry(&map, util, e, NULL);
d9c66f0b
JS
265 if (other) {
266 if (other->matching >= 0)
267 BUG("already assigned!");
268
269 other->matching = i;
270 util->matching = other->i;
271 }
272 }
273
6da1a258 274 hashmap_clear(&map);
d9c66f0b
JS
275}
276
277static void diffsize_consume(void *data, char *line, unsigned long len)
278{
279 (*(int *)data)++;
280}
281
d2eb8093
JK
282static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
283 const char *funcline, long funclen)
284{
285 diffsize_consume(data, NULL, 0);
286}
287
d9c66f0b
JS
288static int diffsize(const char *a, const char *b)
289{
290 xpparam_t pp = { 0 };
291 xdemitconf_t cfg = { 0 };
292 mmfile_t mf1, mf2;
293 int count = 0;
294
295 mf1.ptr = (char *)a;
296 mf1.size = strlen(a);
297 mf2.ptr = (char *)b;
298 mf2.size = strlen(b);
299
300 cfg.ctxlen = 3;
d2eb8093
JK
301 if (!xdi_diff_outf(&mf1, &mf2,
302 diffsize_hunk, diffsize_consume, &count,
303 &pp, &cfg))
d9c66f0b
JS
304 return count;
305
306 error(_("failed to generate diff"));
307 return COST_MAX;
308}
309
310static void get_correspondences(struct string_list *a, struct string_list *b,
311 int creation_factor)
312{
313 int n = a->nr + b->nr;
314 int *cost, c, *a2b, *b2a;
315 int i, j;
316
317 ALLOC_ARRAY(cost, st_mult(n, n));
318 ALLOC_ARRAY(a2b, n);
319 ALLOC_ARRAY(b2a, n);
320
321 for (i = 0; i < a->nr; i++) {
322 struct patch_util *a_util = a->items[i].util;
323
324 for (j = 0; j < b->nr; j++) {
325 struct patch_util *b_util = b->items[j].util;
326
327 if (a_util->matching == j)
328 c = 0;
329 else if (a_util->matching < 0 && b_util->matching < 0)
330 c = diffsize(a_util->diff, b_util->diff);
331 else
332 c = COST_MAX;
333 cost[i + n * j] = c;
334 }
335
336 c = a_util->matching < 0 ?
337 a_util->diffsize * creation_factor / 100 : COST_MAX;
338 for (j = b->nr; j < n; j++)
339 cost[i + n * j] = c;
340 }
341
342 for (j = 0; j < b->nr; j++) {
343 struct patch_util *util = b->items[j].util;
344
345 c = util->matching < 0 ?
346 util->diffsize * creation_factor / 100 : COST_MAX;
347 for (i = a->nr; i < n; i++)
348 cost[i + n * j] = c;
349 }
350
351 for (i = a->nr; i < n; i++)
352 for (j = b->nr; j < n; j++)
353 cost[i + n * j] = 0;
354
355 compute_assignment(n, n, cost, a2b, b2a);
356
357 for (i = 0; i < a->nr; i++)
358 if (a2b[i] >= 0 && a2b[i] < b->nr) {
359 struct patch_util *a_util = a->items[i].util;
360 struct patch_util *b_util = b->items[a2b[i]].util;
361
362 a_util->matching = a2b[i];
363 b_util->matching = i;
364 }
365
366 free(cost);
367 free(a2b);
368 free(b2a);
369}
370
faa1df86 371static void output_pair_header(struct diff_options *diffopt,
d1f87a2d 372 int patch_no_width,
faa1df86 373 struct strbuf *buf,
eb0be38c
JS
374 struct strbuf *dashes,
375 struct patch_util *a_util,
376 struct patch_util *b_util)
d9c66f0b 377{
eb0be38c
JS
378 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
379 struct commit *commit;
faa1df86
JS
380 char status;
381 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
382 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
383 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
384 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
385 const char *color;
eb0be38c
JS
386
387 if (!dashes->len)
388 strbuf_addchars(dashes, '-',
389 strlen(find_unique_abbrev(oid,
390 DEFAULT_ABBREV)));
391
faa1df86
JS
392 if (!b_util) {
393 color = color_old;
394 status = '<';
395 } else if (!a_util) {
396 color = color_new;
397 status = '>';
398 } else if (strcmp(a_util->patch, b_util->patch)) {
399 color = color_commit;
400 status = '!';
401 } else {
402 color = color_commit;
403 status = '=';
404 }
405
eb0be38c 406 strbuf_reset(buf);
faa1df86 407 strbuf_addstr(buf, status == '!' ? color_old : color);
eb0be38c 408 if (!a_util)
d1f87a2d 409 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
eb0be38c 410 else
d1f87a2d 411 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
eb0be38c
JS
412 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
413
faa1df86
JS
414 if (status == '!')
415 strbuf_addf(buf, "%s%s", color_reset, color);
416 strbuf_addch(buf, status);
417 if (status == '!')
418 strbuf_addf(buf, "%s%s", color_reset, color_new);
eb0be38c
JS
419
420 if (!b_util)
d1f87a2d 421 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
eb0be38c 422 else
d1f87a2d 423 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
eb0be38c
JS
424 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
425
426 commit = lookup_commit_reference(the_repository, oid);
427 if (commit) {
faa1df86
JS
428 if (status == '!')
429 strbuf_addf(buf, "%s%s", color_reset, color);
430
eb0be38c
JS
431 strbuf_addch(buf, ' ');
432 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
433 }
faa1df86 434 strbuf_addf(buf, "%s\n", color_reset);
eb0be38c 435
87f1b2d4 436 fwrite(buf->buf, buf->len, 1, diffopt->file);
d9c66f0b
JS
437}
438
499352c2
TG
439static struct userdiff_driver section_headers = {
440 .funcname = { "^ ## (.*) ##$\n"
441 "^.?@@ (.*)$", REG_EXTENDED }
4eba1fe6
JS
442};
443
c8c5e43a
JS
444static struct diff_filespec *get_filespec(const char *name, const char *p)
445{
446 struct diff_filespec *spec = alloc_filespec(name);
447
0e573e8f 448 fill_filespec(spec, &null_oid, 0, 0100644);
c8c5e43a
JS
449 spec->data = (char *)p;
450 spec->size = strlen(p);
451 spec->should_munmap = 0;
452 spec->is_stdin = 1;
499352c2 453 spec->driver = &section_headers;
c8c5e43a
JS
454
455 return spec;
456}
457
458static void patch_diff(const char *a, const char *b,
1ca69225 459 struct diff_options *diffopt)
c8c5e43a
JS
460{
461 diff_queue(&diff_queued_diff,
462 get_filespec("a", a), get_filespec("b", b));
463
464 diffcore_std(diffopt);
465 diff_flush(diffopt);
466}
467
3e6046ed
JS
468static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
469{
470 return data;
471}
472
c8c5e43a 473static void output(struct string_list *a, struct string_list *b,
3e6046ed 474 struct range_diff_options *range_diff_opts)
d9c66f0b 475{
eb0be38c 476 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
d1f87a2d 477 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
9dc46e02 478 int i = 0, j = 0;
3e6046ed
JS
479 struct diff_options opts;
480 struct strbuf indent = STRBUF_INIT;
481
482 if (range_diff_opts->diffopt)
483 memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
484 else
485 diff_setup(&opts);
486
487 if (!opts.output_format)
488 opts.output_format = DIFF_FORMAT_PATCH;
489 opts.flags.suppress_diff_headers = 1;
490 opts.flags.dual_color_diffed_diffs =
491 range_diff_opts->dual_color;
492 opts.flags.suppress_hunk_header_line_count = 1;
493 opts.output_prefix = output_prefix_cb;
494 strbuf_addstr(&indent, " ");
495 opts.output_prefix_data = &indent;
496 diff_setup_done(&opts);
9dc46e02
JS
497
498 /*
499 * We assume the user is really more interested in the second argument
500 * ("newer" version). To that end, we print the output in the order of
501 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
502 * commits that are no longer in the RHS into a good place, we place
503 * them once we have shown all of their predecessors in the LHS.
504 */
505
506 while (i < a->nr || j < b->nr) {
507 struct patch_util *a_util, *b_util;
508 a_util = i < a->nr ? a->items[i].util : NULL;
509 b_util = j < b->nr ? b->items[j].util : NULL;
510
511 /* Skip all the already-shown commits from the LHS. */
512 while (i < a->nr && a_util->shown)
513 a_util = ++i < a->nr ? a->items[i].util : NULL;
514
515 /* Show unmatched LHS commit whose predecessors were shown. */
516 if (i < a->nr && a_util->matching < 0) {
1e79f973
JS
517 if (!range_diff_opts->right_only)
518 output_pair_header(&opts, patch_no_width,
faa1df86 519 &buf, &dashes, a_util, NULL);
9dc46e02
JS
520 i++;
521 continue;
522 }
d9c66f0b 523
9dc46e02
JS
524 /* Show unmatched RHS commits. */
525 while (j < b->nr && b_util->matching < 0) {
1e79f973
JS
526 if (!range_diff_opts->left_only)
527 output_pair_header(&opts, patch_no_width,
faa1df86 528 &buf, &dashes, NULL, b_util);
9dc46e02 529 b_util = ++j < b->nr ? b->items[j].util : NULL;
d9c66f0b 530 }
d9c66f0b 531
9dc46e02
JS
532 /* Show matching LHS/RHS pair. */
533 if (j < b->nr) {
534 a_util = a->items[b_util->matching].util;
3e6046ed 535 output_pair_header(&opts, patch_no_width,
faa1df86 536 &buf, &dashes, a_util, b_util);
3e6046ed 537 if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
c8c5e43a 538 patch_diff(a->items[b_util->matching].string,
3e6046ed 539 b->items[j].string, &opts);
9dc46e02
JS
540 a_util->shown = 1;
541 j++;
542 }
d9c66f0b 543 }
eb0be38c
JS
544 strbuf_release(&buf);
545 strbuf_release(&dashes);
3e6046ed 546 strbuf_release(&indent);
73a834e9
ES
547}
548
d9c66f0b 549int show_range_diff(const char *range1, const char *range2,
f1ce6c19 550 struct range_diff_options *range_diff_opts)
d9c66f0b
JS
551{
552 int res = 0;
553
554 struct string_list branch1 = STRING_LIST_INIT_DUP;
555 struct string_list branch2 = STRING_LIST_INIT_DUP;
556
1e79f973
JS
557 if (range_diff_opts->left_only && range_diff_opts->right_only)
558 res = error(_("--left-only and --right-only are mutually exclusive"));
559
560 if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
d9c66f0b 561 res = error(_("could not parse log for '%s'"), range1);
f1ce6c19 562 if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
d9c66f0b
JS
563 res = error(_("could not parse log for '%s'"), range2);
564
565 if (!res) {
566 find_exact_matches(&branch1, &branch2);
f1ce6c19
JS
567 get_correspondences(&branch1, &branch2,
568 range_diff_opts->creation_factor);
3e6046ed 569 output(&branch1, &branch2, range_diff_opts);
d9c66f0b
JS
570 }
571
572 string_list_clear(&branch1, 1);
573 string_list_clear(&branch2, 1);
574
575 return res;
576}
679b5916
JS
577
578int is_range_diff_range(const char *arg)
579{
359f0d75
JS
580 char *copy = xstrdup(arg); /* setup_revisions() modifies it */
581 const char *argv[] = { "", copy, "--", NULL };
582 int i, positive = 0, negative = 0;
583 struct rev_info revs;
584
585 init_revisions(&revs, NULL);
586 if (setup_revisions(3, argv, &revs, NULL) == 1) {
587 for (i = 0; i < revs.pending.nr; i++)
588 if (revs.pending.objects[i].item->flags & UNINTERESTING)
589 negative++;
590 else
591 positive++;
592 for (i = 0; i < revs.pending.nr; i++) {
593 struct object *obj = revs.pending.objects[i].item;
594
595 if (obj->type == OBJ_COMMIT)
596 clear_commit_marks((struct commit *)obj,
597 ALL_REV_FLAGS);
598 }
599 }
600
601 free(copy);
602 object_array_clear(&revs.pending);
603 return negative > 0 && positive > 0;
679b5916 604}