2 #include "range-diff.h"
3 #include "string-list.h"
4 #include "run-command.h"
5 #include "argv-array.h"
7 #include "xdiff-interface.h"
8 #include "linear-assignment.h"
15 /* For the search for an exact match */
16 struct hashmap_entry e;
17 const char *diff, *patch;
22 /* the index of the matching item in the other branch, or -1 */
27 static size_t find_end_of_line(char *buffer, unsigned long size)
29 char *eol = memchr(buffer, '\n', size);
35 return eol + 1 - buffer;
39 * Reads the patches into a string list, with the `util` field being populated
40 * as struct object_id (will need to be free()d).
42 static int read_patches(const char *range, struct string_list *list)
44 struct child_process cp = CHILD_PROCESS_INIT;
45 struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
46 struct patch_util *util = NULL;
52 argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
53 "--reverse", "--date-order", "--decorate=no",
55 * Choose indicators that are not used anywhere
56 * else in diffs, but still look reasonable
57 * (e.g. will not be confusing when debugging)
59 "--output-indicator-new=>",
60 "--output-indicator-old=<",
61 "--output-indicator-context=#",
62 "--no-abbrev-commit", range,
68 if (start_command(&cp))
69 return error_errno(_("could not start `log`"));
70 if (strbuf_read(&contents, cp.out, 0) < 0) {
71 error_errno(_("could not read `log` output"));
78 for (offset = 0; size > 0; offset += len, size -= len, line += len) {
81 len = find_end_of_line(line, size);
83 if (skip_prefix(line, "commit ", &p)) {
85 string_list_append(list, buf.buf)->util = util;
88 util = xcalloc(sizeof(*util), 1);
89 if (get_oid(p, &util->oid)) {
90 error(_("could not parse commit '%s'"), p);
92 string_list_clear(list, 1);
94 strbuf_release(&contents);
103 if (starts_with(line, "diff --git")) {
105 strbuf_addch(&buf, '\n');
106 if (!util->diff_offset)
107 util->diff_offset = buf.len;
108 strbuf_addch(&buf, ' ');
109 strbuf_addstr(&buf, line);
110 } else if (in_header) {
111 if (starts_with(line, "Author: ")) {
112 strbuf_addstr(&buf, line);
113 strbuf_addstr(&buf, "\n\n");
114 } else if (starts_with(line, " ")) {
116 while (isspace(*p) && p >= line)
118 strbuf_add(&buf, line, p - line + 1);
119 strbuf_addch(&buf, '\n');
122 } else if (skip_prefix(line, "@@ ", &p)) {
124 strbuf_addstr(&buf, p ? p : "@@");
125 } else if (!line[0] || starts_with(line, "index "))
127 * A completely blank (not ' \n', which is context)
128 * line is not valid in a diff. We skip it
129 * silently, because this neatly handles the blank
130 * separator line between commits in git-log
133 * We also want to ignore the diff's `index` lines
134 * because they contain exact blob hashes in which
135 * we are not interested.
138 else if (line[0] == '>') {
139 strbuf_addch(&buf, '+');
140 strbuf_addstr(&buf, line + 1);
141 } else if (line[0] == '<') {
142 strbuf_addch(&buf, '-');
143 strbuf_addstr(&buf, line + 1);
144 } else if (line[0] == '#') {
145 strbuf_addch(&buf, ' ');
146 strbuf_addstr(&buf, line + 1);
148 strbuf_addch(&buf, ' ');
149 strbuf_addstr(&buf, line);
152 strbuf_addch(&buf, '\n');
155 strbuf_release(&contents);
158 string_list_append(list, buf.buf)->util = util;
159 strbuf_release(&buf);
161 if (finish_command(&cp))
167 static int patch_util_cmp(const void *dummy, const struct patch_util *a,
168 const struct patch_util *b, const char *keydata)
170 return strcmp(a->diff, keydata ? keydata : b->diff);
173 static void find_exact_matches(struct string_list *a, struct string_list *b)
178 hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
180 /* First, add the patches of a to a hash map */
181 for (i = 0; i < a->nr; i++) {
182 struct patch_util *util = a->items[i].util;
185 util->patch = a->items[i].string;
186 util->diff = util->patch + util->diff_offset;
187 hashmap_entry_init(util, strhash(util->diff));
188 hashmap_add(&map, util);
191 /* Now try to find exact matches in b */
192 for (i = 0; i < b->nr; i++) {
193 struct patch_util *util = b->items[i].util, *other;
196 util->patch = b->items[i].string;
197 util->diff = util->patch + util->diff_offset;
198 hashmap_entry_init(util, strhash(util->diff));
199 other = hashmap_remove(&map, util, NULL);
201 if (other->matching >= 0)
202 BUG("already assigned!");
205 util->matching = other->i;
209 hashmap_free(&map, 0);
212 static void diffsize_consume(void *data, char *line, unsigned long len)
217 static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
218 const char *funcline, long funclen)
220 diffsize_consume(data, NULL, 0);
223 static int diffsize(const char *a, const char *b)
225 xpparam_t pp = { 0 };
226 xdemitconf_t cfg = { 0 };
231 mf1.size = strlen(a);
233 mf2.size = strlen(b);
236 if (!xdi_diff_outf(&mf1, &mf2,
237 diffsize_hunk, diffsize_consume, &count,
241 error(_("failed to generate diff"));
245 static void get_correspondences(struct string_list *a, struct string_list *b,
248 int n = a->nr + b->nr;
249 int *cost, c, *a2b, *b2a;
252 ALLOC_ARRAY(cost, st_mult(n, n));
256 for (i = 0; i < a->nr; i++) {
257 struct patch_util *a_util = a->items[i].util;
259 for (j = 0; j < b->nr; j++) {
260 struct patch_util *b_util = b->items[j].util;
262 if (a_util->matching == j)
264 else if (a_util->matching < 0 && b_util->matching < 0)
265 c = diffsize(a_util->diff, b_util->diff);
271 c = a_util->matching < 0 ?
272 a_util->diffsize * creation_factor / 100 : COST_MAX;
273 for (j = b->nr; j < n; j++)
277 for (j = 0; j < b->nr; j++) {
278 struct patch_util *util = b->items[j].util;
280 c = util->matching < 0 ?
281 util->diffsize * creation_factor / 100 : COST_MAX;
282 for (i = a->nr; i < n; i++)
286 for (i = a->nr; i < n; i++)
287 for (j = b->nr; j < n; j++)
290 compute_assignment(n, n, cost, a2b, b2a);
292 for (i = 0; i < a->nr; i++)
293 if (a2b[i] >= 0 && a2b[i] < b->nr) {
294 struct patch_util *a_util = a->items[i].util;
295 struct patch_util *b_util = b->items[a2b[i]].util;
297 a_util->matching = a2b[i];
298 b_util->matching = i;
306 static void output_pair_header(struct diff_options *diffopt,
309 struct strbuf *dashes,
310 struct patch_util *a_util,
311 struct patch_util *b_util)
313 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
314 struct commit *commit;
316 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
317 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
318 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
319 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
323 strbuf_addchars(dashes, '-',
324 strlen(find_unique_abbrev(oid,
330 } else if (!a_util) {
333 } else if (strcmp(a_util->patch, b_util->patch)) {
334 color = color_commit;
337 color = color_commit;
342 strbuf_addstr(buf, status == '!' ? color_old : color);
344 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
346 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
347 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
350 strbuf_addf(buf, "%s%s", color_reset, color);
351 strbuf_addch(buf, status);
353 strbuf_addf(buf, "%s%s", color_reset, color_new);
356 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
358 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
359 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
361 commit = lookup_commit_reference(the_repository, oid);
364 strbuf_addf(buf, "%s%s", color_reset, color);
366 strbuf_addch(buf, ' ');
367 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
369 strbuf_addf(buf, "%s\n", color_reset);
371 fwrite(buf->buf, buf->len, 1, diffopt->file);
374 static struct userdiff_driver no_func_name = {
375 .funcname = { "$^", 0 }
378 static struct diff_filespec *get_filespec(const char *name, const char *p)
380 struct diff_filespec *spec = alloc_filespec(name);
382 fill_filespec(spec, &null_oid, 0, 0100644);
383 spec->data = (char *)p;
384 spec->size = strlen(p);
385 spec->should_munmap = 0;
387 spec->driver = &no_func_name;
392 static void patch_diff(const char *a, const char *b,
393 struct diff_options *diffopt)
395 diff_queue(&diff_queued_diff,
396 get_filespec("a", a), get_filespec("b", b));
398 diffcore_std(diffopt);
402 static void output(struct string_list *a, struct string_list *b,
403 struct diff_options *diffopt)
405 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
406 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
410 * We assume the user is really more interested in the second argument
411 * ("newer" version). To that end, we print the output in the order of
412 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
413 * commits that are no longer in the RHS into a good place, we place
414 * them once we have shown all of their predecessors in the LHS.
417 while (i < a->nr || j < b->nr) {
418 struct patch_util *a_util, *b_util;
419 a_util = i < a->nr ? a->items[i].util : NULL;
420 b_util = j < b->nr ? b->items[j].util : NULL;
422 /* Skip all the already-shown commits from the LHS. */
423 while (i < a->nr && a_util->shown)
424 a_util = ++i < a->nr ? a->items[i].util : NULL;
426 /* Show unmatched LHS commit whose predecessors were shown. */
427 if (i < a->nr && a_util->matching < 0) {
428 output_pair_header(diffopt, patch_no_width,
429 &buf, &dashes, a_util, NULL);
434 /* Show unmatched RHS commits. */
435 while (j < b->nr && b_util->matching < 0) {
436 output_pair_header(diffopt, patch_no_width,
437 &buf, &dashes, NULL, b_util);
438 b_util = ++j < b->nr ? b->items[j].util : NULL;
441 /* Show matching LHS/RHS pair. */
443 a_util = a->items[b_util->matching].util;
444 output_pair_header(diffopt, patch_no_width,
445 &buf, &dashes, a_util, b_util);
446 if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
447 patch_diff(a->items[b_util->matching].string,
448 b->items[j].string, diffopt);
453 strbuf_release(&buf);
454 strbuf_release(&dashes);
457 static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
462 int show_range_diff(const char *range1, const char *range2,
463 int creation_factor, int dual_color,
464 struct diff_options *diffopt)
468 struct string_list branch1 = STRING_LIST_INIT_DUP;
469 struct string_list branch2 = STRING_LIST_INIT_DUP;
471 if (read_patches(range1, &branch1))
472 res = error(_("could not parse log for '%s'"), range1);
473 if (!res && read_patches(range2, &branch2))
474 res = error(_("could not parse log for '%s'"), range2);
477 struct diff_options opts;
478 struct strbuf indent = STRBUF_INIT;
481 memcpy(&opts, diffopt, sizeof(opts));
485 if (!opts.output_format)
486 opts.output_format = DIFF_FORMAT_PATCH;
487 opts.flags.suppress_diff_headers = 1;
488 opts.flags.dual_color_diffed_diffs = dual_color;
489 opts.output_prefix = output_prefix_cb;
490 strbuf_addstr(&indent, " ");
491 opts.output_prefix_data = &indent;
492 diff_setup_done(&opts);
494 find_exact_matches(&branch1, &branch2);
495 get_correspondences(&branch1, &branch2, creation_factor);
496 output(&branch1, &branch2, &opts);
498 strbuf_release(&indent);
501 string_list_clear(&branch1, 1);
502 string_list_clear(&branch2, 1);