t4046: avoid continue in &&-chain for zsh
[alt-git.git] / range-diff.c
blobc45b6d849cbf4f97ec3d6eb893259837d25d9052
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 "repository.h"
17 #include "userdiff.h"
18 #include "apply.h"
19 #include "revision.h"
21 struct patch_util {
22 /* For the search for an exact match */
23 struct hashmap_entry e;
24 const char *diff, *patch;
26 int i, shown;
27 int diffsize;
28 size_t diff_offset;
29 /* the index of the matching item in the other branch, or -1 */
30 int matching;
31 struct object_id oid;
35 * Reads the patches into a string list, with the `util` field being populated
36 * as struct object_id (will need to be free()d).
38 static int read_patches(const char *range, struct string_list *list,
39 const struct strvec *other_arg)
41 struct child_process cp = CHILD_PROCESS_INIT;
42 struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
43 struct patch_util *util = NULL;
44 int in_header = 1;
45 char *line, *current_filename = NULL;
46 ssize_t len;
47 size_t size;
48 int ret = -1;
50 strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
51 "--reverse", "--date-order", "--decorate=no",
52 "--no-prefix", "--submodule=short",
54 * Choose indicators that are not used anywhere
55 * else in diffs, but still look reasonable
56 * (e.g. will not be confusing when debugging)
58 "--output-indicator-new=>",
59 "--output-indicator-old=<",
60 "--output-indicator-context=#",
61 "--no-abbrev-commit",
62 "--pretty=medium",
63 "--show-notes-by-default",
64 NULL);
65 strvec_push(&cp.args, range);
66 if (other_arg)
67 strvec_pushv(&cp.args, other_arg->v);
68 cp.out = -1;
69 cp.no_stdin = 1;
70 cp.git_cmd = 1;
72 if (start_command(&cp))
73 return error_errno(_("could not start `log`"));
74 if (strbuf_read(&contents, cp.out, 0) < 0) {
75 error_errno(_("could not read `log` output"));
76 finish_command(&cp);
77 goto cleanup;
79 if (finish_command(&cp))
80 goto cleanup;
82 line = contents.buf;
83 size = contents.len;
84 for (; size > 0; size -= len, line += len) {
85 const char *p;
86 char *eol;
88 eol = memchr(line, '\n', size);
89 if (eol) {
90 *eol = '\0';
91 len = eol + 1 - line;
92 } else {
93 len = size;
96 if (skip_prefix(line, "commit ", &p)) {
97 if (util) {
98 string_list_append(list, buf.buf)->util = util;
99 strbuf_reset(&buf);
101 CALLOC_ARRAY(util, 1);
102 if (repo_get_oid(the_repository, p, &util->oid)) {
103 error(_("could not parse commit '%s'"), p);
104 FREE_AND_NULL(util);
105 string_list_clear(list, 1);
106 goto cleanup;
108 util->matching = -1;
109 in_header = 1;
110 continue;
113 if (!util) {
114 error(_("could not parse first line of `log` output: "
115 "did not start with 'commit ': '%s'"),
116 line);
117 string_list_clear(list, 1);
118 goto cleanup;
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_AND_NULL(util);
140 string_list_clear(list, 1);
141 goto cleanup;
143 strbuf_addstr(&buf, " ## ");
144 if (patch.is_new > 0)
145 strbuf_addf(&buf, "%s (new)", patch.new_name);
146 else if (patch.is_delete > 0)
147 strbuf_addf(&buf, "%s (deleted)", patch.old_name);
148 else if (patch.is_rename)
149 strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
150 else
151 strbuf_addstr(&buf, patch.new_name);
153 free(current_filename);
154 if (patch.is_delete > 0)
155 current_filename = xstrdup(patch.old_name);
156 else
157 current_filename = xstrdup(patch.new_name);
159 if (patch.new_mode && patch.old_mode &&
160 patch.old_mode != patch.new_mode)
161 strbuf_addf(&buf, " (mode change %06o => %06o)",
162 patch.old_mode, patch.new_mode);
164 strbuf_addstr(&buf, " ##");
165 release_patch(&patch);
166 } else if (in_header) {
167 if (starts_with(line, "Author: ")) {
168 strbuf_addstr(&buf, " ## Metadata ##\n");
169 strbuf_addstr(&buf, line);
170 strbuf_addstr(&buf, "\n\n");
171 strbuf_addstr(&buf, " ## Commit message ##\n");
172 } else if (starts_with(line, "Notes") &&
173 line[strlen(line) - 1] == ':') {
174 strbuf_addstr(&buf, "\n\n");
175 /* strip the trailing colon */
176 strbuf_addf(&buf, " ## %.*s ##\n",
177 (int)(strlen(line) - 1), line);
178 } else if (starts_with(line, " ")) {
179 p = line + len - 2;
180 while (isspace(*p) && p >= line)
181 p--;
182 strbuf_add(&buf, line, p - line + 1);
183 strbuf_addch(&buf, '\n');
185 continue;
186 } else if (skip_prefix(line, "@@ ", &p)) {
187 p = strstr(p, "@@");
188 strbuf_addstr(&buf, "@@");
189 if (current_filename && p[2])
190 strbuf_addf(&buf, " %s:", current_filename);
191 if (p)
192 strbuf_addstr(&buf, p + 2);
193 } else if (!line[0])
195 * A completely blank (not ' \n', which is context)
196 * line is not valid in a diff. We skip it
197 * silently, because this neatly handles the blank
198 * separator line between commits in git-log
199 * output.
201 continue;
202 else if (line[0] == '>') {
203 strbuf_addch(&buf, '+');
204 strbuf_addstr(&buf, line + 1);
205 } else if (line[0] == '<') {
206 strbuf_addch(&buf, '-');
207 strbuf_addstr(&buf, line + 1);
208 } else if (line[0] == '#') {
209 strbuf_addch(&buf, ' ');
210 strbuf_addstr(&buf, line + 1);
211 } else {
212 strbuf_addch(&buf, ' ');
213 strbuf_addstr(&buf, line);
216 strbuf_addch(&buf, '\n');
217 util->diffsize++;
220 ret = 0;
221 cleanup:
222 strbuf_release(&contents);
224 if (util)
225 string_list_append(list, buf.buf)->util = util;
226 strbuf_release(&buf);
227 free(current_filename);
229 return ret;
232 static int patch_util_cmp(const void *cmp_data UNUSED,
233 const struct hashmap_entry *ha,
234 const struct hashmap_entry *hb,
235 const void *keydata)
237 const struct patch_util
238 *a = container_of(ha, const struct patch_util, e),
239 *b = container_of(hb, const struct patch_util, e);
240 return strcmp(a->diff, keydata ? keydata : b->diff);
243 static void find_exact_matches(struct string_list *a, struct string_list *b)
245 struct hashmap map = HASHMAP_INIT(patch_util_cmp, NULL);
246 int i;
248 /* First, add the patches of a to a hash map */
249 for (i = 0; i < a->nr; i++) {
250 struct patch_util *util = a->items[i].util;
252 util->i = i;
253 util->patch = a->items[i].string;
254 util->diff = util->patch + util->diff_offset;
255 hashmap_entry_init(&util->e, strhash(util->diff));
256 hashmap_add(&map, &util->e);
259 /* Now try to find exact matches in b */
260 for (i = 0; i < b->nr; i++) {
261 struct patch_util *util = b->items[i].util, *other;
263 util->i = i;
264 util->patch = b->items[i].string;
265 util->diff = util->patch + util->diff_offset;
266 hashmap_entry_init(&util->e, strhash(util->diff));
267 other = hashmap_remove_entry(&map, util, e, NULL);
268 if (other) {
269 if (other->matching >= 0)
270 BUG("already assigned!");
272 other->matching = i;
273 util->matching = other->i;
277 hashmap_clear(&map);
280 static int diffsize_consume(void *data,
281 char *line UNUSED,
282 unsigned long len UNUSED)
284 (*(int *)data)++;
285 return 0;
288 static void diffsize_hunk(void *data,
289 long ob UNUSED, long on UNUSED,
290 long nb UNUSED, long nn UNUSED,
291 const char *func UNUSED, long funclen UNUSED)
293 diffsize_consume(data, NULL, 0);
296 static int diffsize(const char *a, const char *b)
298 xpparam_t pp = { 0 };
299 xdemitconf_t cfg = { 0 };
300 mmfile_t mf1, mf2;
301 int count = 0;
303 mf1.ptr = (char *)a;
304 mf1.size = strlen(a);
305 mf2.ptr = (char *)b;
306 mf2.size = strlen(b);
308 cfg.ctxlen = 3;
309 if (!xdi_diff_outf(&mf1, &mf2,
310 diffsize_hunk, diffsize_consume, &count,
311 &pp, &cfg))
312 return count;
314 error(_("failed to generate diff"));
315 return COST_MAX;
318 static void get_correspondences(struct string_list *a, struct string_list *b,
319 int creation_factor)
321 int n = a->nr + b->nr;
322 int *cost, c, *a2b, *b2a;
323 int i, j;
325 ALLOC_ARRAY(cost, st_mult(n, n));
326 ALLOC_ARRAY(a2b, n);
327 ALLOC_ARRAY(b2a, n);
329 for (i = 0; i < a->nr; i++) {
330 struct patch_util *a_util = a->items[i].util;
332 for (j = 0; j < b->nr; j++) {
333 struct patch_util *b_util = b->items[j].util;
335 if (a_util->matching == j)
336 c = 0;
337 else if (a_util->matching < 0 && b_util->matching < 0)
338 c = diffsize(a_util->diff, b_util->diff);
339 else
340 c = COST_MAX;
341 cost[i + n * j] = c;
344 c = a_util->matching < 0 ?
345 a_util->diffsize * creation_factor / 100 : COST_MAX;
346 for (j = b->nr; j < n; j++)
347 cost[i + n * j] = c;
350 for (j = 0; j < b->nr; j++) {
351 struct patch_util *util = b->items[j].util;
353 c = util->matching < 0 ?
354 util->diffsize * creation_factor / 100 : COST_MAX;
355 for (i = a->nr; i < n; i++)
356 cost[i + n * j] = c;
359 for (i = a->nr; i < n; i++)
360 for (j = b->nr; j < n; j++)
361 cost[i + n * j] = 0;
363 compute_assignment(n, n, cost, a2b, b2a);
365 for (i = 0; i < a->nr; i++)
366 if (a2b[i] >= 0 && a2b[i] < b->nr) {
367 struct patch_util *a_util = a->items[i].util;
368 struct patch_util *b_util = b->items[a2b[i]].util;
370 a_util->matching = a2b[i];
371 b_util->matching = i;
374 free(cost);
375 free(a2b);
376 free(b2a);
379 static void output_pair_header(struct diff_options *diffopt,
380 int patch_no_width,
381 struct strbuf *buf,
382 struct strbuf *dashes,
383 struct patch_util *a_util,
384 struct patch_util *b_util)
386 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
387 struct commit *commit;
388 char status;
389 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
390 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
391 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
392 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
393 const char *color;
394 int abbrev = diffopt->abbrev;
396 if (abbrev < 0)
397 abbrev = DEFAULT_ABBREV;
399 if (!dashes->len)
400 strbuf_addchars(dashes, '-',
401 strlen(repo_find_unique_abbrev(the_repository, oid, abbrev)));
403 if (!b_util) {
404 color = color_old;
405 status = '<';
406 } else if (!a_util) {
407 color = color_new;
408 status = '>';
409 } else if (strcmp(a_util->patch, b_util->patch)) {
410 color = color_commit;
411 status = '!';
412 } else {
413 color = color_commit;
414 status = '=';
417 strbuf_reset(buf);
418 strbuf_addstr(buf, status == '!' ? color_old : color);
419 if (!a_util)
420 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
421 else
422 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
423 repo_find_unique_abbrev(the_repository, &a_util->oid, abbrev));
425 if (status == '!')
426 strbuf_addf(buf, "%s%s", color_reset, color);
427 strbuf_addch(buf, status);
428 if (status == '!')
429 strbuf_addf(buf, "%s%s", color_reset, color_new);
431 if (!b_util)
432 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
433 else
434 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
435 repo_find_unique_abbrev(the_repository, &b_util->oid, abbrev));
437 commit = lookup_commit_reference(the_repository, oid);
438 if (commit) {
439 if (status == '!')
440 strbuf_addf(buf, "%s%s", color_reset, color);
442 strbuf_addch(buf, ' ');
443 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
445 strbuf_addf(buf, "%s\n", color_reset);
447 fwrite(buf->buf, buf->len, 1, diffopt->file);
450 static struct userdiff_driver section_headers = {
451 .funcname = { "^ ## (.*) ##$\n"
452 "^.?@@ (.*)$", REG_EXTENDED }
455 static struct diff_filespec *get_filespec(const char *name, const char *p)
457 struct diff_filespec *spec = alloc_filespec(name);
459 fill_filespec(spec, null_oid(), 0, 0100644);
460 spec->data = (char *)p;
461 spec->size = strlen(p);
462 spec->should_munmap = 0;
463 spec->is_stdin = 1;
464 spec->driver = &section_headers;
466 return spec;
469 static void patch_diff(const char *a, const char *b,
470 struct diff_options *diffopt)
472 diff_queue(&diff_queued_diff,
473 get_filespec("a", a), get_filespec("b", b));
475 diffcore_std(diffopt);
476 diff_flush(diffopt);
479 static struct strbuf *output_prefix_cb(struct diff_options *opt UNUSED, void *data)
481 return data;
484 static void output(struct string_list *a, struct string_list *b,
485 struct range_diff_options *range_diff_opts)
487 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
488 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
489 int i = 0, j = 0;
490 struct diff_options opts;
491 struct strbuf indent = STRBUF_INIT;
493 if (range_diff_opts->diffopt)
494 memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
495 else
496 repo_diff_setup(the_repository, &opts);
498 opts.no_free = 1;
499 if (!opts.output_format)
500 opts.output_format = DIFF_FORMAT_PATCH;
501 opts.flags.suppress_diff_headers = 1;
502 opts.flags.dual_color_diffed_diffs =
503 range_diff_opts->dual_color;
504 opts.flags.suppress_hunk_header_line_count = 1;
505 opts.output_prefix = output_prefix_cb;
506 strbuf_addstr(&indent, " ");
507 opts.output_prefix_data = &indent;
508 diff_setup_done(&opts);
511 * We assume the user is really more interested in the second argument
512 * ("newer" version). To that end, we print the output in the order of
513 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
514 * commits that are no longer in the RHS into a good place, we place
515 * them once we have shown all of their predecessors in the LHS.
518 while (i < a->nr || j < b->nr) {
519 struct patch_util *a_util, *b_util;
520 a_util = i < a->nr ? a->items[i].util : NULL;
521 b_util = j < b->nr ? b->items[j].util : NULL;
523 /* Skip all the already-shown commits from the LHS. */
524 while (i < a->nr && a_util->shown)
525 a_util = ++i < a->nr ? a->items[i].util : NULL;
527 /* Show unmatched LHS commit whose predecessors were shown. */
528 if (i < a->nr && a_util->matching < 0) {
529 if (!range_diff_opts->right_only)
530 output_pair_header(&opts, patch_no_width,
531 &buf, &dashes, a_util, NULL);
532 i++;
533 continue;
536 /* Show unmatched RHS commits. */
537 while (j < b->nr && b_util->matching < 0) {
538 if (!range_diff_opts->left_only)
539 output_pair_header(&opts, patch_no_width,
540 &buf, &dashes, NULL, b_util);
541 b_util = ++j < b->nr ? b->items[j].util : NULL;
544 /* Show matching LHS/RHS pair. */
545 if (j < b->nr) {
546 a_util = a->items[b_util->matching].util;
547 output_pair_header(&opts, patch_no_width,
548 &buf, &dashes, a_util, b_util);
549 if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
550 patch_diff(a->items[b_util->matching].string,
551 b->items[j].string, &opts);
552 a_util->shown = 1;
553 j++;
556 strbuf_release(&buf);
557 strbuf_release(&dashes);
558 strbuf_release(&indent);
559 opts.no_free = 0;
560 diff_free(&opts);
563 int show_range_diff(const char *range1, const char *range2,
564 struct range_diff_options *range_diff_opts)
566 int res = 0;
568 struct string_list branch1 = STRING_LIST_INIT_DUP;
569 struct string_list branch2 = STRING_LIST_INIT_DUP;
571 if (range_diff_opts->left_only && range_diff_opts->right_only)
572 res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
574 if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
575 res = error(_("could not parse log for '%s'"), range1);
576 if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
577 res = error(_("could not parse log for '%s'"), range2);
579 if (!res) {
580 find_exact_matches(&branch1, &branch2);
581 get_correspondences(&branch1, &branch2,
582 range_diff_opts->creation_factor);
583 output(&branch1, &branch2, range_diff_opts);
586 string_list_clear(&branch1, 1);
587 string_list_clear(&branch2, 1);
589 return res;
592 int is_range_diff_range(const char *arg)
594 char *copy = xstrdup(arg); /* setup_revisions() modifies it */
595 const char *argv[] = { "", copy, "--", NULL };
596 int i, positive = 0, negative = 0;
597 struct rev_info revs;
599 repo_init_revisions(the_repository, &revs, NULL);
600 if (setup_revisions(3, argv, &revs, NULL) == 1) {
601 for (i = 0; i < revs.pending.nr; i++)
602 if (revs.pending.objects[i].item->flags & UNINTERESTING)
603 negative++;
604 else
605 positive++;
606 for (i = 0; i < revs.pending.nr; i++) {
607 struct object *obj = revs.pending.objects[i].item;
609 if (obj->type == OBJ_COMMIT)
610 clear_commit_marks((struct commit *)obj,
611 ALL_REV_FLAGS);
615 free(copy);
616 release_revisions(&revs);
617 return negative > 0 && positive > 0;