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