Git 2.46-rc2
[alt-git.git] / range-diff.c
blob5f01605550e57206accbf166a782681db4087eef
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "range-diff.h"
7 #include "object-name.h"
8 #include "string-list.h"
9 #include "run-command.h"
10 #include "strvec.h"
11 #include "hashmap.h"
12 #include "xdiff-interface.h"
13 #include "linear-assignment.h"
14 #include "diffcore.h"
15 #include "commit.h"
16 #include "pager.h"
17 #include "pretty.h"
18 #include "repository.h"
19 #include "userdiff.h"
20 #include "apply.h"
21 #include "revision.h"
23 struct patch_util {
24 /* For the search for an exact match */
25 struct hashmap_entry e;
26 const char *diff, *patch;
28 int i, shown;
29 int diffsize;
30 size_t diff_offset;
31 /* the index of the matching item in the other branch, or -1 */
32 int matching;
33 struct object_id oid;
37 * Reads the patches into a string list, with the `util` field being populated
38 * as struct object_id (will need to be free()d).
40 static int read_patches(const char *range, struct string_list *list,
41 const struct strvec *other_arg)
43 struct child_process cp = CHILD_PROCESS_INIT;
44 struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
45 struct patch_util *util = NULL;
46 int in_header = 1;
47 char *line, *current_filename = NULL;
48 ssize_t len;
49 size_t size;
50 int ret = -1;
52 strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
53 "--reverse", "--date-order", "--decorate=no",
54 "--no-prefix", "--submodule=short",
56 * Choose indicators that are not used anywhere
57 * else in diffs, but still look reasonable
58 * (e.g. will not be confusing when debugging)
60 "--output-indicator-new=>",
61 "--output-indicator-old=<",
62 "--output-indicator-context=#",
63 "--no-abbrev-commit",
64 "--pretty=medium",
65 "--show-notes-by-default",
66 NULL);
67 strvec_push(&cp.args, range);
68 if (other_arg)
69 strvec_pushv(&cp.args, other_arg->v);
70 cp.out = -1;
71 cp.no_stdin = 1;
72 cp.git_cmd = 1;
74 if (start_command(&cp))
75 return error_errno(_("could not start `log`"));
76 if (strbuf_read(&contents, cp.out, 0) < 0) {
77 error_errno(_("could not read `log` output"));
78 finish_command(&cp);
79 goto cleanup;
81 if (finish_command(&cp))
82 goto cleanup;
84 line = contents.buf;
85 size = contents.len;
86 for (; size > 0; size -= len, line += len) {
87 const char *p;
88 char *eol;
90 eol = memchr(line, '\n', size);
91 if (eol) {
92 *eol = '\0';
93 len = eol + 1 - line;
94 } else {
95 len = size;
98 if (skip_prefix(line, "commit ", &p)) {
99 if (util) {
100 string_list_append(list, buf.buf)->util = util;
101 strbuf_reset(&buf);
103 CALLOC_ARRAY(util, 1);
104 if (repo_get_oid(the_repository, p, &util->oid)) {
105 error(_("could not parse commit '%s'"), p);
106 FREE_AND_NULL(util);
107 string_list_clear(list, 1);
108 goto cleanup;
110 util->matching = -1;
111 in_header = 1;
112 continue;
115 if (!util) {
116 error(_("could not parse first line of `log` output: "
117 "did not start with 'commit ': '%s'"),
118 line);
119 string_list_clear(list, 1);
120 goto cleanup;
123 if (starts_with(line, "diff --git")) {
124 struct patch patch = { 0 };
125 struct strbuf root = STRBUF_INIT;
126 int linenr = 0;
127 int orig_len;
129 in_header = 0;
130 strbuf_addch(&buf, '\n');
131 if (!util->diff_offset)
132 util->diff_offset = buf.len;
133 if (eol)
134 *eol = '\n';
135 orig_len = len;
136 len = parse_git_diff_header(&root, &linenr, 0, line,
137 len, size, &patch);
138 if (len < 0) {
139 error(_("could not parse git header '%.*s'"),
140 orig_len, line);
141 FREE_AND_NULL(util);
142 string_list_clear(list, 1);
143 goto cleanup;
145 strbuf_addstr(&buf, " ## ");
146 if (patch.is_new > 0)
147 strbuf_addf(&buf, "%s (new)", patch.new_name);
148 else if (patch.is_delete > 0)
149 strbuf_addf(&buf, "%s (deleted)", patch.old_name);
150 else if (patch.is_rename)
151 strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
152 else
153 strbuf_addstr(&buf, patch.new_name);
155 free(current_filename);
156 if (patch.is_delete > 0)
157 current_filename = xstrdup(patch.old_name);
158 else
159 current_filename = xstrdup(patch.new_name);
161 if (patch.new_mode && patch.old_mode &&
162 patch.old_mode != patch.new_mode)
163 strbuf_addf(&buf, " (mode change %06o => %06o)",
164 patch.old_mode, patch.new_mode);
166 strbuf_addstr(&buf, " ##");
167 release_patch(&patch);
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++;
222 ret = 0;
223 cleanup:
224 strbuf_release(&contents);
226 if (util)
227 string_list_append(list, buf.buf)->util = util;
228 strbuf_release(&buf);
229 free(current_filename);
231 return ret;
234 static int patch_util_cmp(const void *cmp_data UNUSED,
235 const struct hashmap_entry *ha,
236 const struct hashmap_entry *hb,
237 const void *keydata)
239 const struct patch_util
240 *a = container_of(ha, const struct patch_util, e),
241 *b = container_of(hb, const struct patch_util, e);
242 return strcmp(a->diff, keydata ? keydata : b->diff);
245 static void find_exact_matches(struct string_list *a, struct string_list *b)
247 struct hashmap map = HASHMAP_INIT(patch_util_cmp, NULL);
248 int i;
250 /* First, add the patches of a to a hash map */
251 for (i = 0; i < a->nr; i++) {
252 struct patch_util *util = a->items[i].util;
254 util->i = i;
255 util->patch = a->items[i].string;
256 util->diff = util->patch + util->diff_offset;
257 hashmap_entry_init(&util->e, strhash(util->diff));
258 hashmap_add(&map, &util->e);
261 /* Now try to find exact matches in b */
262 for (i = 0; i < b->nr; i++) {
263 struct patch_util *util = b->items[i].util, *other;
265 util->i = i;
266 util->patch = b->items[i].string;
267 util->diff = util->patch + util->diff_offset;
268 hashmap_entry_init(&util->e, strhash(util->diff));
269 other = hashmap_remove_entry(&map, util, e, NULL);
270 if (other) {
271 if (other->matching >= 0)
272 BUG("already assigned!");
274 other->matching = i;
275 util->matching = other->i;
279 hashmap_clear(&map);
282 static int diffsize_consume(void *data,
283 char *line UNUSED,
284 unsigned long len UNUSED)
286 (*(int *)data)++;
287 return 0;
290 static void diffsize_hunk(void *data,
291 long ob UNUSED, long on UNUSED,
292 long nb UNUSED, long nn UNUSED,
293 const char *func UNUSED, long funclen UNUSED)
295 diffsize_consume(data, NULL, 0);
298 static int diffsize(const char *a, const char *b)
300 xpparam_t pp = { 0 };
301 xdemitconf_t cfg = { 0 };
302 mmfile_t mf1, mf2;
303 int count = 0;
305 mf1.ptr = (char *)a;
306 mf1.size = strlen(a);
307 mf2.ptr = (char *)b;
308 mf2.size = strlen(b);
310 cfg.ctxlen = 3;
311 if (!xdi_diff_outf(&mf1, &mf2,
312 diffsize_hunk, diffsize_consume, &count,
313 &pp, &cfg))
314 return count;
316 error(_("failed to generate diff"));
317 return COST_MAX;
320 static void get_correspondences(struct string_list *a, struct string_list *b,
321 int creation_factor)
323 int n = a->nr + b->nr;
324 int *cost, c, *a2b, *b2a;
325 int i, j;
327 ALLOC_ARRAY(cost, st_mult(n, n));
328 ALLOC_ARRAY(a2b, n);
329 ALLOC_ARRAY(b2a, n);
331 for (i = 0; i < a->nr; i++) {
332 struct patch_util *a_util = a->items[i].util;
334 for (j = 0; j < b->nr; j++) {
335 struct patch_util *b_util = b->items[j].util;
337 if (a_util->matching == j)
338 c = 0;
339 else if (a_util->matching < 0 && b_util->matching < 0)
340 c = diffsize(a_util->diff, b_util->diff);
341 else
342 c = COST_MAX;
343 cost[i + n * j] = c;
346 c = a_util->matching < 0 ?
347 a_util->diffsize * creation_factor / 100 : COST_MAX;
348 for (j = b->nr; j < n; j++)
349 cost[i + n * j] = c;
352 for (j = 0; j < b->nr; j++) {
353 struct patch_util *util = b->items[j].util;
355 c = util->matching < 0 ?
356 util->diffsize * creation_factor / 100 : COST_MAX;
357 for (i = a->nr; i < n; i++)
358 cost[i + n * j] = c;
361 for (i = a->nr; i < n; i++)
362 for (j = b->nr; j < n; j++)
363 cost[i + n * j] = 0;
365 compute_assignment(n, n, cost, a2b, b2a);
367 for (i = 0; i < a->nr; i++)
368 if (a2b[i] >= 0 && a2b[i] < b->nr) {
369 struct patch_util *a_util = a->items[i].util;
370 struct patch_util *b_util = b->items[a2b[i]].util;
372 a_util->matching = a2b[i];
373 b_util->matching = i;
376 free(cost);
377 free(a2b);
378 free(b2a);
381 static void output_pair_header(struct diff_options *diffopt,
382 int patch_no_width,
383 struct strbuf *buf,
384 struct strbuf *dashes,
385 struct patch_util *a_util,
386 struct patch_util *b_util)
388 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
389 struct commit *commit;
390 char status;
391 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
392 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
393 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
394 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
395 const char *color;
396 int abbrev = diffopt->abbrev;
398 if (abbrev < 0)
399 abbrev = DEFAULT_ABBREV;
401 if (!dashes->len)
402 strbuf_addchars(dashes, '-',
403 strlen(repo_find_unique_abbrev(the_repository, oid, abbrev)));
405 if (!b_util) {
406 color = color_old;
407 status = '<';
408 } else if (!a_util) {
409 color = color_new;
410 status = '>';
411 } else if (strcmp(a_util->patch, b_util->patch)) {
412 color = color_commit;
413 status = '!';
414 } else {
415 color = color_commit;
416 status = '=';
419 strbuf_reset(buf);
420 strbuf_addstr(buf, status == '!' ? color_old : color);
421 if (!a_util)
422 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
423 else
424 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
425 repo_find_unique_abbrev(the_repository, &a_util->oid, abbrev));
427 if (status == '!')
428 strbuf_addf(buf, "%s%s", color_reset, color);
429 strbuf_addch(buf, status);
430 if (status == '!')
431 strbuf_addf(buf, "%s%s", color_reset, color_new);
433 if (!b_util)
434 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
435 else
436 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
437 repo_find_unique_abbrev(the_repository, &b_util->oid, abbrev));
439 commit = lookup_commit_reference(the_repository, oid);
440 if (commit) {
441 if (status == '!')
442 strbuf_addf(buf, "%s%s", color_reset, color);
444 strbuf_addch(buf, ' ');
445 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
447 strbuf_addf(buf, "%s\n", color_reset);
449 fwrite(buf->buf, buf->len, 1, diffopt->file);
452 static struct userdiff_driver section_headers = {
453 .funcname = { "^ ## (.*) ##$\n"
454 "^.?@@ (.*)$", REG_EXTENDED }
457 static struct diff_filespec *get_filespec(const char *name, const char *p)
459 struct diff_filespec *spec = alloc_filespec(name);
461 fill_filespec(spec, null_oid(), 0, 0100644);
462 spec->data = (char *)p;
463 spec->size = strlen(p);
464 spec->should_munmap = 0;
465 spec->is_stdin = 1;
466 spec->driver = &section_headers;
468 return spec;
471 static void patch_diff(const char *a, const char *b,
472 struct diff_options *diffopt)
474 diff_queue(&diff_queued_diff,
475 get_filespec("a", a), get_filespec("b", b));
477 diffcore_std(diffopt);
478 diff_flush(diffopt);
481 static struct strbuf *output_prefix_cb(struct diff_options *opt UNUSED, void *data)
483 return data;
486 static void output(struct string_list *a, struct string_list *b,
487 struct range_diff_options *range_diff_opts)
489 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
490 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
491 int i = 0, j = 0;
492 struct diff_options opts;
493 struct strbuf indent = STRBUF_INIT;
495 if (range_diff_opts->diffopt)
496 memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
497 else
498 repo_diff_setup(the_repository, &opts);
500 opts.no_free = 1;
501 if (!opts.output_format)
502 opts.output_format = DIFF_FORMAT_PATCH;
503 opts.flags.suppress_diff_headers = 1;
504 opts.flags.dual_color_diffed_diffs =
505 range_diff_opts->dual_color;
506 opts.flags.suppress_hunk_header_line_count = 1;
507 opts.output_prefix = output_prefix_cb;
508 strbuf_addstr(&indent, " ");
509 opts.output_prefix_data = &indent;
510 diff_setup_done(&opts);
513 * We assume the user is really more interested in the second argument
514 * ("newer" version). To that end, we print the output in the order of
515 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
516 * commits that are no longer in the RHS into a good place, we place
517 * them once we have shown all of their predecessors in the LHS.
520 while (i < a->nr || j < b->nr) {
521 struct patch_util *a_util, *b_util;
522 a_util = i < a->nr ? a->items[i].util : NULL;
523 b_util = j < b->nr ? b->items[j].util : NULL;
525 /* Skip all the already-shown commits from the LHS. */
526 while (i < a->nr && a_util->shown)
527 a_util = ++i < a->nr ? a->items[i].util : NULL;
529 /* Show unmatched LHS commit whose predecessors were shown. */
530 if (i < a->nr && a_util->matching < 0) {
531 if (!range_diff_opts->right_only)
532 output_pair_header(&opts, patch_no_width,
533 &buf, &dashes, a_util, NULL);
534 i++;
535 continue;
538 /* Show unmatched RHS commits. */
539 while (j < b->nr && b_util->matching < 0) {
540 if (!range_diff_opts->left_only)
541 output_pair_header(&opts, patch_no_width,
542 &buf, &dashes, NULL, b_util);
543 b_util = ++j < b->nr ? b->items[j].util : NULL;
546 /* Show matching LHS/RHS pair. */
547 if (j < b->nr) {
548 a_util = a->items[b_util->matching].util;
549 output_pair_header(&opts, patch_no_width,
550 &buf, &dashes, a_util, b_util);
551 if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
552 patch_diff(a->items[b_util->matching].string,
553 b->items[j].string, &opts);
554 a_util->shown = 1;
555 j++;
558 strbuf_release(&buf);
559 strbuf_release(&dashes);
560 strbuf_release(&indent);
561 opts.no_free = 0;
562 diff_free(&opts);
565 int show_range_diff(const char *range1, const char *range2,
566 struct range_diff_options *range_diff_opts)
568 int res = 0;
570 struct string_list branch1 = STRING_LIST_INIT_DUP;
571 struct string_list branch2 = STRING_LIST_INIT_DUP;
573 if (range_diff_opts->left_only && range_diff_opts->right_only)
574 res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
576 if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
577 res = error(_("could not parse log for '%s'"), range1);
578 if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
579 res = error(_("could not parse log for '%s'"), range2);
581 if (!res) {
582 find_exact_matches(&branch1, &branch2);
583 get_correspondences(&branch1, &branch2,
584 range_diff_opts->creation_factor);
585 output(&branch1, &branch2, range_diff_opts);
588 string_list_clear(&branch1, 1);
589 string_list_clear(&branch2, 1);
591 return res;
594 int is_range_diff_range(const char *arg)
596 char *copy = xstrdup(arg); /* setup_revisions() modifies it */
597 const char *argv[] = { "", copy, "--", NULL };
598 int i, positive = 0, negative = 0;
599 struct rev_info revs;
601 repo_init_revisions(the_repository, &revs, NULL);
602 if (setup_revisions(3, argv, &revs, NULL) == 1) {
603 for (i = 0; i < revs.pending.nr; i++)
604 if (revs.pending.objects[i].item->flags & UNINTERESTING)
605 negative++;
606 else
607 positive++;
608 for (i = 0; i < revs.pending.nr; i++) {
609 struct object *obj = revs.pending.objects[i].item;
611 if (obj->type == OBJ_COMMIT)
612 clear_commit_marks((struct commit *)obj,
613 ALL_REV_FLAGS);
617 free(copy);
618 release_revisions(&revs);
619 return negative > 0 && positive > 0;