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