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