t0000: clear GIT_SKIP_TESTS before running sub-tests
[alt-git.git] / range-diff.c
blobe9479794b46a51fd08b8ed287a50f35b7c92a4d5
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;
29 static size_t find_end_of_line(char *buffer, unsigned long size)
31 char *eol = memchr(buffer, '\n', size);
33 if (!eol)
34 return size;
36 *eol = '\0';
37 return eol + 1 - buffer;
41 * Reads the patches into a string list, with the `util` field being populated
42 * as struct object_id (will need to be free()d).
44 static int read_patches(const char *range, struct string_list *list,
45 const struct strvec *other_arg)
47 struct child_process cp = CHILD_PROCESS_INIT;
48 struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
49 struct patch_util *util = NULL;
50 int in_header = 1;
51 char *line, *current_filename = NULL;
52 int offset, len;
53 size_t size;
55 strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
56 "--reverse", "--date-order", "--decorate=no",
57 "--no-prefix",
59 * Choose indicators that are not used anywhere
60 * else in diffs, but still look reasonable
61 * (e.g. will not be confusing when debugging)
63 "--output-indicator-new=>",
64 "--output-indicator-old=<",
65 "--output-indicator-context=#",
66 "--no-abbrev-commit",
67 "--pretty=medium",
68 "--notes",
69 NULL);
70 if (other_arg)
71 strvec_pushv(&cp.args, other_arg->v);
72 strvec_push(&cp.args, range);
73 cp.out = -1;
74 cp.no_stdin = 1;
75 cp.git_cmd = 1;
77 if (start_command(&cp))
78 return error_errno(_("could not start `log`"));
79 if (strbuf_read(&contents, cp.out, 0) < 0) {
80 error_errno(_("could not read `log` output"));
81 finish_command(&cp);
82 return -1;
84 if (finish_command(&cp))
85 return -1;
87 line = contents.buf;
88 size = contents.len;
89 for (offset = 0; size > 0; offset += len, size -= len, line += len) {
90 const char *p;
92 len = find_end_of_line(line, size);
93 line[len - 1] = '\0';
94 if (skip_prefix(line, "commit ", &p)) {
95 if (util) {
96 string_list_append(list, buf.buf)->util = util;
97 strbuf_reset(&buf);
99 CALLOC_ARRAY(util, 1);
100 if (get_oid(p, &util->oid)) {
101 error(_("could not parse commit '%s'"), p);
102 free(util);
103 free(current_filename);
104 string_list_clear(list, 1);
105 strbuf_release(&buf);
106 strbuf_release(&contents);
107 return -1;
109 util->matching = -1;
110 in_header = 1;
111 continue;
114 if (!util) {
115 error(_("could not parse first line of `log` output: "
116 "did not start with 'commit ': '%s'"),
117 line);
118 free(current_filename);
119 string_list_clear(list, 1);
120 strbuf_release(&buf);
121 strbuf_release(&contents);
122 return -1;
125 if (starts_with(line, "diff --git")) {
126 struct patch patch = { 0 };
127 struct strbuf root = STRBUF_INIT;
128 int linenr = 0;
129 int orig_len;
131 in_header = 0;
132 strbuf_addch(&buf, '\n');
133 if (!util->diff_offset)
134 util->diff_offset = buf.len;
135 line[len - 1] = '\n';
136 orig_len = len;
137 len = parse_git_diff_header(&root, &linenr, 0, line,
138 len, size, &patch);
139 if (len < 0) {
140 error(_("could not parse git header '%.*s'"),
141 orig_len, line);
142 free(util);
143 free(current_filename);
144 string_list_clear(list, 1);
145 strbuf_release(&buf);
146 strbuf_release(&contents);
147 return -1;
149 strbuf_addstr(&buf, " ## ");
150 if (patch.is_new > 0)
151 strbuf_addf(&buf, "%s (new)", patch.new_name);
152 else if (patch.is_delete > 0)
153 strbuf_addf(&buf, "%s (deleted)", patch.old_name);
154 else if (patch.is_rename)
155 strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
156 else
157 strbuf_addstr(&buf, patch.new_name);
159 free(current_filename);
160 if (patch.is_delete > 0)
161 current_filename = xstrdup(patch.old_name);
162 else
163 current_filename = xstrdup(patch.new_name);
165 if (patch.new_mode && patch.old_mode &&
166 patch.old_mode != patch.new_mode)
167 strbuf_addf(&buf, " (mode change %06o => %06o)",
168 patch.old_mode, patch.new_mode);
170 strbuf_addstr(&buf, " ##");
171 } else if (in_header) {
172 if (starts_with(line, "Author: ")) {
173 strbuf_addstr(&buf, " ## Metadata ##\n");
174 strbuf_addstr(&buf, line);
175 strbuf_addstr(&buf, "\n\n");
176 strbuf_addstr(&buf, " ## Commit message ##\n");
177 } else if (starts_with(line, "Notes") &&
178 line[strlen(line) - 1] == ':') {
179 strbuf_addstr(&buf, "\n\n");
180 /* strip the trailing colon */
181 strbuf_addf(&buf, " ## %.*s ##\n",
182 (int)(strlen(line) - 1), line);
183 } else if (starts_with(line, " ")) {
184 p = line + len - 2;
185 while (isspace(*p) && p >= line)
186 p--;
187 strbuf_add(&buf, line, p - line + 1);
188 strbuf_addch(&buf, '\n');
190 continue;
191 } else if (skip_prefix(line, "@@ ", &p)) {
192 p = strstr(p, "@@");
193 strbuf_addstr(&buf, "@@");
194 if (current_filename && p[2])
195 strbuf_addf(&buf, " %s:", current_filename);
196 if (p)
197 strbuf_addstr(&buf, p + 2);
198 } else if (!line[0])
200 * A completely blank (not ' \n', which is context)
201 * line is not valid in a diff. We skip it
202 * silently, because this neatly handles the blank
203 * separator line between commits in git-log
204 * output.
206 continue;
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 if (line[0] == '#') {
214 strbuf_addch(&buf, ' ');
215 strbuf_addstr(&buf, line + 1);
216 } else {
217 strbuf_addch(&buf, ' ');
218 strbuf_addstr(&buf, line);
221 strbuf_addch(&buf, '\n');
222 util->diffsize++;
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 0;
234 static int patch_util_cmp(const void *dummy, const struct patch_util *a,
235 const struct patch_util *b, const char *keydata)
237 return strcmp(a->diff, keydata ? keydata : b->diff);
240 static void find_exact_matches(struct string_list *a, struct string_list *b)
242 struct hashmap map = HASHMAP_INIT((hashmap_cmp_fn)patch_util_cmp, NULL);
243 int i;
245 /* First, add the patches of a to a hash map */
246 for (i = 0; i < a->nr; i++) {
247 struct patch_util *util = a->items[i].util;
249 util->i = i;
250 util->patch = a->items[i].string;
251 util->diff = util->patch + util->diff_offset;
252 hashmap_entry_init(&util->e, strhash(util->diff));
253 hashmap_add(&map, &util->e);
256 /* Now try to find exact matches in b */
257 for (i = 0; i < b->nr; i++) {
258 struct patch_util *util = b->items[i].util, *other;
260 util->i = i;
261 util->patch = b->items[i].string;
262 util->diff = util->patch + util->diff_offset;
263 hashmap_entry_init(&util->e, strhash(util->diff));
264 other = hashmap_remove_entry(&map, util, e, NULL);
265 if (other) {
266 if (other->matching >= 0)
267 BUG("already assigned!");
269 other->matching = i;
270 util->matching = other->i;
274 hashmap_clear(&map);
277 static int diffsize_consume(void *data, char *line, unsigned long len)
279 (*(int *)data)++;
280 return 0;
283 static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
284 const char *funcline, long funclen)
286 diffsize_consume(data, NULL, 0);
289 static int diffsize(const char *a, const char *b)
291 xpparam_t pp = { 0 };
292 xdemitconf_t cfg = { 0 };
293 mmfile_t mf1, mf2;
294 int count = 0;
296 mf1.ptr = (char *)a;
297 mf1.size = strlen(a);
298 mf2.ptr = (char *)b;
299 mf2.size = strlen(b);
301 cfg.ctxlen = 3;
302 if (!xdi_diff_outf(&mf1, &mf2,
303 diffsize_hunk, diffsize_consume, &count,
304 &pp, &cfg))
305 return count;
307 error(_("failed to generate diff"));
308 return COST_MAX;
311 static void get_correspondences(struct string_list *a, struct string_list *b,
312 int creation_factor)
314 int n = a->nr + b->nr;
315 int *cost, c, *a2b, *b2a;
316 int i, j;
318 ALLOC_ARRAY(cost, st_mult(n, n));
319 ALLOC_ARRAY(a2b, n);
320 ALLOC_ARRAY(b2a, n);
322 for (i = 0; i < a->nr; i++) {
323 struct patch_util *a_util = a->items[i].util;
325 for (j = 0; j < b->nr; j++) {
326 struct patch_util *b_util = b->items[j].util;
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;
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;
343 for (j = 0; j < b->nr; j++) {
344 struct patch_util *util = b->items[j].util;
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;
352 for (i = a->nr; i < n; i++)
353 for (j = b->nr; j < n; j++)
354 cost[i + n * j] = 0;
356 compute_assignment(n, n, cost, a2b, b2a);
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;
363 a_util->matching = a2b[i];
364 b_util->matching = i;
367 free(cost);
368 free(a2b);
369 free(b2a);
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)
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;
388 if (!dashes->len)
389 strbuf_addchars(dashes, '-',
390 strlen(find_unique_abbrev(oid,
391 DEFAULT_ABBREV)));
393 if (!b_util) {
394 color = color_old;
395 status = '<';
396 } else if (!a_util) {
397 color = color_new;
398 status = '>';
399 } else if (strcmp(a_util->patch, b_util->patch)) {
400 color = color_commit;
401 status = '!';
402 } else {
403 color = color_commit;
404 status = '=';
407 strbuf_reset(buf);
408 strbuf_addstr(buf, status == '!' ? color_old : color);
409 if (!a_util)
410 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
411 else
412 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
413 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
415 if (status == '!')
416 strbuf_addf(buf, "%s%s", color_reset, color);
417 strbuf_addch(buf, status);
418 if (status == '!')
419 strbuf_addf(buf, "%s%s", color_reset, color_new);
421 if (!b_util)
422 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
423 else
424 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
425 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
427 commit = lookup_commit_reference(the_repository, oid);
428 if (commit) {
429 if (status == '!')
430 strbuf_addf(buf, "%s%s", color_reset, color);
432 strbuf_addch(buf, ' ');
433 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
435 strbuf_addf(buf, "%s\n", color_reset);
437 fwrite(buf->buf, buf->len, 1, diffopt->file);
440 static struct userdiff_driver section_headers = {
441 .funcname = { "^ ## (.*) ##$\n"
442 "^.?@@ (.*)$", REG_EXTENDED }
445 static struct diff_filespec *get_filespec(const char *name, const char *p)
447 struct diff_filespec *spec = alloc_filespec(name);
449 fill_filespec(spec, null_oid(), 0, 0100644);
450 spec->data = (char *)p;
451 spec->size = strlen(p);
452 spec->should_munmap = 0;
453 spec->is_stdin = 1;
454 spec->driver = &section_headers;
456 return spec;
459 static void patch_diff(const char *a, const char *b,
460 struct diff_options *diffopt)
462 diff_queue(&diff_queued_diff,
463 get_filespec("a", a), get_filespec("b", b));
465 diffcore_std(diffopt);
466 diff_flush(diffopt);
469 static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
471 return data;
474 static void output(struct string_list *a, struct string_list *b,
475 struct range_diff_options *range_diff_opts)
477 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
478 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
479 int i = 0, j = 0;
480 struct diff_options opts;
481 struct strbuf indent = STRBUF_INIT;
483 if (range_diff_opts->diffopt)
484 memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
485 else
486 diff_setup(&opts);
488 if (!opts.output_format)
489 opts.output_format = DIFF_FORMAT_PATCH;
490 opts.flags.suppress_diff_headers = 1;
491 opts.flags.dual_color_diffed_diffs =
492 range_diff_opts->dual_color;
493 opts.flags.suppress_hunk_header_line_count = 1;
494 opts.output_prefix = output_prefix_cb;
495 strbuf_addstr(&indent, " ");
496 opts.output_prefix_data = &indent;
497 diff_setup_done(&opts);
500 * We assume the user is really more interested in the second argument
501 * ("newer" version). To that end, we print the output in the order of
502 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
503 * commits that are no longer in the RHS into a good place, we place
504 * them once we have shown all of their predecessors in the LHS.
507 while (i < a->nr || j < b->nr) {
508 struct patch_util *a_util, *b_util;
509 a_util = i < a->nr ? a->items[i].util : NULL;
510 b_util = j < b->nr ? b->items[j].util : NULL;
512 /* Skip all the already-shown commits from the LHS. */
513 while (i < a->nr && a_util->shown)
514 a_util = ++i < a->nr ? a->items[i].util : NULL;
516 /* Show unmatched LHS commit whose predecessors were shown. */
517 if (i < a->nr && a_util->matching < 0) {
518 if (!range_diff_opts->right_only)
519 output_pair_header(&opts, patch_no_width,
520 &buf, &dashes, a_util, NULL);
521 i++;
522 continue;
525 /* Show unmatched RHS commits. */
526 while (j < b->nr && b_util->matching < 0) {
527 if (!range_diff_opts->left_only)
528 output_pair_header(&opts, patch_no_width,
529 &buf, &dashes, NULL, b_util);
530 b_util = ++j < b->nr ? b->items[j].util : NULL;
533 /* Show matching LHS/RHS pair. */
534 if (j < b->nr) {
535 a_util = a->items[b_util->matching].util;
536 output_pair_header(&opts, patch_no_width,
537 &buf, &dashes, a_util, b_util);
538 if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
539 patch_diff(a->items[b_util->matching].string,
540 b->items[j].string, &opts);
541 a_util->shown = 1;
542 j++;
545 strbuf_release(&buf);
546 strbuf_release(&dashes);
547 strbuf_release(&indent);
550 int show_range_diff(const char *range1, const char *range2,
551 struct range_diff_options *range_diff_opts)
553 int res = 0;
555 struct string_list branch1 = STRING_LIST_INIT_DUP;
556 struct string_list branch2 = STRING_LIST_INIT_DUP;
558 if (range_diff_opts->left_only && range_diff_opts->right_only)
559 res = error(_("--left-only and --right-only are mutually exclusive"));
561 if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
562 res = error(_("could not parse log for '%s'"), range1);
563 if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
564 res = error(_("could not parse log for '%s'"), range2);
566 if (!res) {
567 find_exact_matches(&branch1, &branch2);
568 get_correspondences(&branch1, &branch2,
569 range_diff_opts->creation_factor);
570 output(&branch1, &branch2, range_diff_opts);
573 string_list_clear(&branch1, 1);
574 string_list_clear(&branch2, 1);
576 return res;
579 int is_range_diff_range(const char *arg)
581 char *copy = xstrdup(arg); /* setup_revisions() modifies it */
582 const char *argv[] = { "", copy, "--", NULL };
583 int i, positive = 0, negative = 0;
584 struct rev_info revs;
586 init_revisions(&revs, NULL);
587 if (setup_revisions(3, argv, &revs, NULL) == 1) {
588 for (i = 0; i < revs.pending.nr; i++)
589 if (revs.pending.objects[i].item->flags & UNINTERESTING)
590 negative++;
591 else
592 positive++;
593 for (i = 0; i < revs.pending.nr; i++) {
594 struct object *obj = revs.pending.objects[i].item;
596 if (obj->type == OBJ_COMMIT)
597 clear_commit_marks((struct commit *)obj,
598 ALL_REV_FLAGS);
602 free(copy);
603 object_array_clear(&revs.pending);
604 return negative > 0 && positive > 0;