Sync with 2.31.8
[git.git] / range-diff.c
blob012b4ea6d2cc9812f3f7089f9239d44cef58b88c
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"
16 struct patch_util {
17 /* For the search for an exact match */
18 struct hashmap_entry e;
19 const char *diff, *patch;
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;
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).
33 static int read_patches(const char *range, struct string_list *list,
34 const struct strvec *other_arg)
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;
44 strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
45 "--reverse", "--date-order", "--decorate=no",
46 "--no-prefix",
48 * Choose indicators that are not used anywhere
49 * else in diffs, but still look reasonable
50 * (e.g. will not be confusing when debugging)
52 "--output-indicator-new=>",
53 "--output-indicator-old=<",
54 "--output-indicator-context=#",
55 "--no-abbrev-commit",
56 "--pretty=medium",
57 "--notes",
58 NULL);
59 if (other_arg)
60 strvec_pushv(&cp.args, other_arg->v);
61 strvec_push(&cp.args, range);
62 cp.out = -1;
63 cp.no_stdin = 1;
64 cp.git_cmd = 1;
66 if (start_command(&cp))
67 return error_errno(_("could not start `log`"));
68 if (strbuf_read(&contents, cp.out, 0) < 0) {
69 error_errno(_("could not read `log` output"));
70 finish_command(&cp);
71 return -1;
73 if (finish_command(&cp))
74 return -1;
76 line = contents.buf;
77 size = contents.len;
78 for (; size > 0; size -= len, line += len) {
79 const char *p;
80 char *eol;
82 eol = memchr(line, '\n', size);
83 if (eol) {
84 *eol = '\0';
85 len = eol + 1 - line;
86 } else {
87 len = size;
90 if (skip_prefix(line, "commit ", &p)) {
91 if (util) {
92 string_list_append(list, buf.buf)->util = util;
93 strbuf_reset(&buf);
95 CALLOC_ARRAY(util, 1);
96 if (get_oid(p, &util->oid)) {
97 error(_("could not parse commit '%s'"), p);
98 free(util);
99 free(current_filename);
100 string_list_clear(list, 1);
101 strbuf_release(&buf);
102 strbuf_release(&contents);
103 return -1;
105 util->matching = -1;
106 in_header = 1;
107 continue;
110 if (!util) {
111 error(_("could not parse first line of `log` output: "
112 "did not start with 'commit ': '%s'"),
113 line);
114 free(current_filename);
115 string_list_clear(list, 1);
116 strbuf_release(&buf);
117 strbuf_release(&contents);
118 return -1;
121 if (starts_with(line, "diff --git")) {
122 struct patch patch = { 0 };
123 struct strbuf root = STRBUF_INIT;
124 int linenr = 0;
125 int orig_len;
127 in_header = 0;
128 strbuf_addch(&buf, '\n');
129 if (!util->diff_offset)
130 util->diff_offset = buf.len;
131 if (eol)
132 *eol = '\n';
133 orig_len = len;
134 len = parse_git_diff_header(&root, &linenr, 0, line,
135 len, size, &patch);
136 if (len < 0) {
137 error(_("could not parse git header '%.*s'"),
138 orig_len, line);
139 free(util);
140 free(current_filename);
141 string_list_clear(list, 1);
142 strbuf_release(&buf);
143 strbuf_release(&contents);
144 return -1;
146 strbuf_addstr(&buf, " ## ");
147 if (patch.is_new > 0)
148 strbuf_addf(&buf, "%s (new)", patch.new_name);
149 else if (patch.is_delete > 0)
150 strbuf_addf(&buf, "%s (deleted)", patch.old_name);
151 else if (patch.is_rename)
152 strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
153 else
154 strbuf_addstr(&buf, patch.new_name);
156 free(current_filename);
157 if (patch.is_delete > 0)
158 current_filename = xstrdup(patch.old_name);
159 else
160 current_filename = xstrdup(patch.new_name);
162 if (patch.new_mode && patch.old_mode &&
163 patch.old_mode != patch.new_mode)
164 strbuf_addf(&buf, " (mode change %06o => %06o)",
165 patch.old_mode, patch.new_mode);
167 strbuf_addstr(&buf, " ##");
168 } else if (in_header) {
169 if (starts_with(line, "Author: ")) {
170 strbuf_addstr(&buf, " ## Metadata ##\n");
171 strbuf_addstr(&buf, line);
172 strbuf_addstr(&buf, "\n\n");
173 strbuf_addstr(&buf, " ## Commit message ##\n");
174 } else if (starts_with(line, "Notes") &&
175 line[strlen(line) - 1] == ':') {
176 strbuf_addstr(&buf, "\n\n");
177 /* strip the trailing colon */
178 strbuf_addf(&buf, " ## %.*s ##\n",
179 (int)(strlen(line) - 1), line);
180 } else if (starts_with(line, " ")) {
181 p = line + len - 2;
182 while (isspace(*p) && p >= line)
183 p--;
184 strbuf_add(&buf, line, p - line + 1);
185 strbuf_addch(&buf, '\n');
187 continue;
188 } else if (skip_prefix(line, "@@ ", &p)) {
189 p = strstr(p, "@@");
190 strbuf_addstr(&buf, "@@");
191 if (current_filename && p[2])
192 strbuf_addf(&buf, " %s:", current_filename);
193 if (p)
194 strbuf_addstr(&buf, p + 2);
195 } else if (!line[0])
197 * A completely blank (not ' \n', which is context)
198 * line is not valid in a diff. We skip it
199 * silently, because this neatly handles the blank
200 * separator line between commits in git-log
201 * output.
203 continue;
204 else if (line[0] == '>') {
205 strbuf_addch(&buf, '+');
206 strbuf_addstr(&buf, line + 1);
207 } else if (line[0] == '<') {
208 strbuf_addch(&buf, '-');
209 strbuf_addstr(&buf, line + 1);
210 } else if (line[0] == '#') {
211 strbuf_addch(&buf, ' ');
212 strbuf_addstr(&buf, line + 1);
213 } else {
214 strbuf_addch(&buf, ' ');
215 strbuf_addstr(&buf, line);
218 strbuf_addch(&buf, '\n');
219 util->diffsize++;
221 strbuf_release(&contents);
223 if (util)
224 string_list_append(list, buf.buf)->util = util;
225 strbuf_release(&buf);
226 free(current_filename);
228 return 0;
231 static int patch_util_cmp(const void *dummy, const struct patch_util *a,
232 const struct patch_util *b, const char *keydata)
234 return strcmp(a->diff, keydata ? keydata : b->diff);
237 static void find_exact_matches(struct string_list *a, struct string_list *b)
239 struct hashmap map = HASHMAP_INIT((hashmap_cmp_fn)patch_util_cmp, NULL);
240 int i;
242 /* First, add the patches of a to a hash map */
243 for (i = 0; i < a->nr; i++) {
244 struct patch_util *util = a->items[i].util;
246 util->i = i;
247 util->patch = a->items[i].string;
248 util->diff = util->patch + util->diff_offset;
249 hashmap_entry_init(&util->e, strhash(util->diff));
250 hashmap_add(&map, &util->e);
253 /* Now try to find exact matches in b */
254 for (i = 0; i < b->nr; i++) {
255 struct patch_util *util = b->items[i].util, *other;
257 util->i = i;
258 util->patch = b->items[i].string;
259 util->diff = util->patch + util->diff_offset;
260 hashmap_entry_init(&util->e, strhash(util->diff));
261 other = hashmap_remove_entry(&map, util, e, NULL);
262 if (other) {
263 if (other->matching >= 0)
264 BUG("already assigned!");
266 other->matching = i;
267 util->matching = other->i;
271 hashmap_clear(&map);
274 static void diffsize_consume(void *data, char *line, unsigned long len)
276 (*(int *)data)++;
279 static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
280 const char *funcline, long funclen)
282 diffsize_consume(data, NULL, 0);
285 static int diffsize(const char *a, const char *b)
287 xpparam_t pp = { 0 };
288 xdemitconf_t cfg = { 0 };
289 mmfile_t mf1, mf2;
290 int count = 0;
292 mf1.ptr = (char *)a;
293 mf1.size = strlen(a);
294 mf2.ptr = (char *)b;
295 mf2.size = strlen(b);
297 cfg.ctxlen = 3;
298 if (!xdi_diff_outf(&mf1, &mf2,
299 diffsize_hunk, diffsize_consume, &count,
300 &pp, &cfg))
301 return count;
303 error(_("failed to generate diff"));
304 return COST_MAX;
307 static void get_correspondences(struct string_list *a, struct string_list *b,
308 int creation_factor)
310 int n = a->nr + b->nr;
311 int *cost, c, *a2b, *b2a;
312 int i, j;
314 ALLOC_ARRAY(cost, st_mult(n, n));
315 ALLOC_ARRAY(a2b, n);
316 ALLOC_ARRAY(b2a, n);
318 for (i = 0; i < a->nr; i++) {
319 struct patch_util *a_util = a->items[i].util;
321 for (j = 0; j < b->nr; j++) {
322 struct patch_util *b_util = b->items[j].util;
324 if (a_util->matching == j)
325 c = 0;
326 else if (a_util->matching < 0 && b_util->matching < 0)
327 c = diffsize(a_util->diff, b_util->diff);
328 else
329 c = COST_MAX;
330 cost[i + n * j] = c;
333 c = a_util->matching < 0 ?
334 a_util->diffsize * creation_factor / 100 : COST_MAX;
335 for (j = b->nr; j < n; j++)
336 cost[i + n * j] = c;
339 for (j = 0; j < b->nr; j++) {
340 struct patch_util *util = b->items[j].util;
342 c = util->matching < 0 ?
343 util->diffsize * creation_factor / 100 : COST_MAX;
344 for (i = a->nr; i < n; i++)
345 cost[i + n * j] = c;
348 for (i = a->nr; i < n; i++)
349 for (j = b->nr; j < n; j++)
350 cost[i + n * j] = 0;
352 compute_assignment(n, n, cost, a2b, b2a);
354 for (i = 0; i < a->nr; i++)
355 if (a2b[i] >= 0 && a2b[i] < b->nr) {
356 struct patch_util *a_util = a->items[i].util;
357 struct patch_util *b_util = b->items[a2b[i]].util;
359 a_util->matching = a2b[i];
360 b_util->matching = i;
363 free(cost);
364 free(a2b);
365 free(b2a);
368 static void output_pair_header(struct diff_options *diffopt,
369 int patch_no_width,
370 struct strbuf *buf,
371 struct strbuf *dashes,
372 struct patch_util *a_util,
373 struct patch_util *b_util)
375 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
376 struct commit *commit;
377 char status;
378 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
379 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
380 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
381 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
382 const char *color;
384 if (!dashes->len)
385 strbuf_addchars(dashes, '-',
386 strlen(find_unique_abbrev(oid,
387 DEFAULT_ABBREV)));
389 if (!b_util) {
390 color = color_old;
391 status = '<';
392 } else if (!a_util) {
393 color = color_new;
394 status = '>';
395 } else if (strcmp(a_util->patch, b_util->patch)) {
396 color = color_commit;
397 status = '!';
398 } else {
399 color = color_commit;
400 status = '=';
403 strbuf_reset(buf);
404 strbuf_addstr(buf, status == '!' ? color_old : color);
405 if (!a_util)
406 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
407 else
408 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
409 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
411 if (status == '!')
412 strbuf_addf(buf, "%s%s", color_reset, color);
413 strbuf_addch(buf, status);
414 if (status == '!')
415 strbuf_addf(buf, "%s%s", color_reset, color_new);
417 if (!b_util)
418 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
419 else
420 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
421 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
423 commit = lookup_commit_reference(the_repository, oid);
424 if (commit) {
425 if (status == '!')
426 strbuf_addf(buf, "%s%s", color_reset, color);
428 strbuf_addch(buf, ' ');
429 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
431 strbuf_addf(buf, "%s\n", color_reset);
433 fwrite(buf->buf, buf->len, 1, diffopt->file);
436 static struct userdiff_driver section_headers = {
437 .funcname = { "^ ## (.*) ##$\n"
438 "^.?@@ (.*)$", REG_EXTENDED }
441 static struct diff_filespec *get_filespec(const char *name, const char *p)
443 struct diff_filespec *spec = alloc_filespec(name);
445 fill_filespec(spec, null_oid(), 0, 0100644);
446 spec->data = (char *)p;
447 spec->size = strlen(p);
448 spec->should_munmap = 0;
449 spec->is_stdin = 1;
450 spec->driver = &section_headers;
452 return spec;
455 static void patch_diff(const char *a, const char *b,
456 struct diff_options *diffopt)
458 diff_queue(&diff_queued_diff,
459 get_filespec("a", a), get_filespec("b", b));
461 diffcore_std(diffopt);
462 diff_flush(diffopt);
465 static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
467 return data;
470 static void output(struct string_list *a, struct string_list *b,
471 struct range_diff_options *range_diff_opts)
473 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
474 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
475 int i = 0, j = 0;
476 struct diff_options opts;
477 struct strbuf indent = STRBUF_INIT;
479 if (range_diff_opts->diffopt)
480 memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
481 else
482 diff_setup(&opts);
484 if (!opts.output_format)
485 opts.output_format = DIFF_FORMAT_PATCH;
486 opts.flags.suppress_diff_headers = 1;
487 opts.flags.dual_color_diffed_diffs =
488 range_diff_opts->dual_color;
489 opts.flags.suppress_hunk_header_line_count = 1;
490 opts.output_prefix = output_prefix_cb;
491 strbuf_addstr(&indent, " ");
492 opts.output_prefix_data = &indent;
493 diff_setup_done(&opts);
496 * We assume the user is really more interested in the second argument
497 * ("newer" version). To that end, we print the output in the order of
498 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
499 * commits that are no longer in the RHS into a good place, we place
500 * them once we have shown all of their predecessors in the LHS.
503 while (i < a->nr || j < b->nr) {
504 struct patch_util *a_util, *b_util;
505 a_util = i < a->nr ? a->items[i].util : NULL;
506 b_util = j < b->nr ? b->items[j].util : NULL;
508 /* Skip all the already-shown commits from the LHS. */
509 while (i < a->nr && a_util->shown)
510 a_util = ++i < a->nr ? a->items[i].util : NULL;
512 /* Show unmatched LHS commit whose predecessors were shown. */
513 if (i < a->nr && a_util->matching < 0) {
514 if (!range_diff_opts->right_only)
515 output_pair_header(&opts, patch_no_width,
516 &buf, &dashes, a_util, NULL);
517 i++;
518 continue;
521 /* Show unmatched RHS commits. */
522 while (j < b->nr && b_util->matching < 0) {
523 if (!range_diff_opts->left_only)
524 output_pair_header(&opts, patch_no_width,
525 &buf, &dashes, NULL, b_util);
526 b_util = ++j < b->nr ? b->items[j].util : NULL;
529 /* Show matching LHS/RHS pair. */
530 if (j < b->nr) {
531 a_util = a->items[b_util->matching].util;
532 output_pair_header(&opts, patch_no_width,
533 &buf, &dashes, a_util, b_util);
534 if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
535 patch_diff(a->items[b_util->matching].string,
536 b->items[j].string, &opts);
537 a_util->shown = 1;
538 j++;
541 strbuf_release(&buf);
542 strbuf_release(&dashes);
543 strbuf_release(&indent);
546 int show_range_diff(const char *range1, const char *range2,
547 struct range_diff_options *range_diff_opts)
549 int res = 0;
551 struct string_list branch1 = STRING_LIST_INIT_DUP;
552 struct string_list branch2 = STRING_LIST_INIT_DUP;
554 if (range_diff_opts->left_only && range_diff_opts->right_only)
555 res = error(_("--left-only and --right-only are mutually exclusive"));
557 if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
558 res = error(_("could not parse log for '%s'"), range1);
559 if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
560 res = error(_("could not parse log for '%s'"), range2);
562 if (!res) {
563 find_exact_matches(&branch1, &branch2);
564 get_correspondences(&branch1, &branch2,
565 range_diff_opts->creation_factor);
566 output(&branch1, &branch2, range_diff_opts);
569 string_list_clear(&branch1, 1);
570 string_list_clear(&branch2, 1);
572 return res;
575 int is_range_diff_range(const char *arg)
577 char *copy = xstrdup(arg); /* setup_revisions() modifies it */
578 const char *argv[] = { "", copy, "--", NULL };
579 int i, positive = 0, negative = 0;
580 struct rev_info revs;
582 init_revisions(&revs, NULL);
583 if (setup_revisions(3, argv, &revs, NULL) == 1) {
584 for (i = 0; i < revs.pending.nr; i++)
585 if (revs.pending.objects[i].item->flags & UNINTERESTING)
586 negative++;
587 else
588 positive++;
589 for (i = 0; i < revs.pending.nr; i++) {
590 struct object *obj = revs.pending.objects[i].item;
592 if (obj->type == OBJ_COMMIT)
593 clear_commit_marks((struct commit *)obj,
594 ALL_REV_FLAGS);
598 free(copy);
599 object_array_clear(&revs.pending);
600 return negative > 0 && positive > 0;