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 (starts_with(line, "@@ "))
123 strbuf_addstr(&buf, "@@");
124 else if (!line[0] || starts_with(line, "index "))
126 * A completely blank (not ' \n', which is context)
127 * line is not valid in a diff. We skip it
128 * silently, because this neatly handles the blank
129 * separator line between commits in git-log
132 * We also want to ignore the diff's `index` lines
133 * because they contain exact blob hashes in which
134 * we are not interested.
137 else if (line[0] == '>') {
138 strbuf_addch(&buf, '+');
139 strbuf_addstr(&buf, line + 1);
140 } else if (line[0] == '<') {
141 strbuf_addch(&buf, '-');
142 strbuf_addstr(&buf, line + 1);
143 } else if (line[0] == '#') {
144 strbuf_addch(&buf, ' ');
145 strbuf_addstr(&buf, line + 1);
147 strbuf_addch(&buf, ' ');
148 strbuf_addstr(&buf, line);
151 strbuf_addch(&buf, '\n');
154 strbuf_release(&contents);
157 string_list_append(list, buf.buf)->util = util;
158 strbuf_release(&buf);
160 if (finish_command(&cp))
166 static int patch_util_cmp(const void *dummy, const struct patch_util *a,
167 const struct patch_util *b, const char *keydata)
169 return strcmp(a->diff, keydata ? keydata : b->diff);
172 static void find_exact_matches(struct string_list *a, struct string_list *b)
177 hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
179 /* First, add the patches of a to a hash map */
180 for (i = 0; i < a->nr; i++) {
181 struct patch_util *util = a->items[i].util;
184 util->patch = a->items[i].string;
185 util->diff = util->patch + util->diff_offset;
186 hashmap_entry_init(util, strhash(util->diff));
187 hashmap_add(&map, util);
190 /* Now try to find exact matches in b */
191 for (i = 0; i < b->nr; i++) {
192 struct patch_util *util = b->items[i].util, *other;
195 util->patch = b->items[i].string;
196 util->diff = util->patch + util->diff_offset;
197 hashmap_entry_init(util, strhash(util->diff));
198 other = hashmap_remove(&map, util, NULL);
200 if (other->matching >= 0)
201 BUG("already assigned!");
204 util->matching = other->i;
208 hashmap_free(&map, 0);
211 static void diffsize_consume(void *data, char *line, unsigned long len)
216 static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
217 const char *funcline, long funclen)
219 diffsize_consume(data, NULL, 0);
222 static int diffsize(const char *a, const char *b)
224 xpparam_t pp = { 0 };
225 xdemitconf_t cfg = { 0 };
230 mf1.size = strlen(a);
232 mf2.size = strlen(b);
235 if (!xdi_diff_outf(&mf1, &mf2,
236 diffsize_hunk, diffsize_consume, &count,
240 error(_("failed to generate diff"));
244 static void get_correspondences(struct string_list *a, struct string_list *b,
247 int n = a->nr + b->nr;
248 int *cost, c, *a2b, *b2a;
251 ALLOC_ARRAY(cost, st_mult(n, n));
255 for (i = 0; i < a->nr; i++) {
256 struct patch_util *a_util = a->items[i].util;
258 for (j = 0; j < b->nr; j++) {
259 struct patch_util *b_util = b->items[j].util;
261 if (a_util->matching == j)
263 else if (a_util->matching < 0 && b_util->matching < 0)
264 c = diffsize(a_util->diff, b_util->diff);
270 c = a_util->matching < 0 ?
271 a_util->diffsize * creation_factor / 100 : COST_MAX;
272 for (j = b->nr; j < n; j++)
276 for (j = 0; j < b->nr; j++) {
277 struct patch_util *util = b->items[j].util;
279 c = util->matching < 0 ?
280 util->diffsize * creation_factor / 100 : COST_MAX;
281 for (i = a->nr; i < n; i++)
285 for (i = a->nr; i < n; i++)
286 for (j = b->nr; j < n; j++)
289 compute_assignment(n, n, cost, a2b, b2a);
291 for (i = 0; i < a->nr; i++)
292 if (a2b[i] >= 0 && a2b[i] < b->nr) {
293 struct patch_util *a_util = a->items[i].util;
294 struct patch_util *b_util = b->items[a2b[i]].util;
296 a_util->matching = a2b[i];
297 b_util->matching = i;
305 static void output_pair_header(struct diff_options *diffopt,
308 struct strbuf *dashes,
309 struct patch_util *a_util,
310 struct patch_util *b_util)
312 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
313 struct commit *commit;
315 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
316 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
317 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
318 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
322 strbuf_addchars(dashes, '-',
323 strlen(find_unique_abbrev(oid,
329 } else if (!a_util) {
332 } else if (strcmp(a_util->patch, b_util->patch)) {
333 color = color_commit;
336 color = color_commit;
341 strbuf_addstr(buf, status == '!' ? color_old : color);
343 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
345 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
346 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
349 strbuf_addf(buf, "%s%s", color_reset, color);
350 strbuf_addch(buf, status);
352 strbuf_addf(buf, "%s%s", color_reset, color_new);
355 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
357 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
358 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
360 commit = lookup_commit_reference(the_repository, oid);
363 strbuf_addf(buf, "%s%s", color_reset, color);
365 strbuf_addch(buf, ' ');
366 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
368 strbuf_addf(buf, "%s\n", color_reset);
370 fwrite(buf->buf, buf->len, 1, diffopt->file);
373 static struct userdiff_driver no_func_name = {
374 .funcname = { "$^", 0 }
377 static struct diff_filespec *get_filespec(const char *name, const char *p)
379 struct diff_filespec *spec = alloc_filespec(name);
381 fill_filespec(spec, &null_oid, 0, 0100644);
382 spec->data = (char *)p;
383 spec->size = strlen(p);
384 spec->should_munmap = 0;
386 spec->driver = &no_func_name;
391 static void patch_diff(const char *a, const char *b,
392 struct diff_options *diffopt)
394 diff_queue(&diff_queued_diff,
395 get_filespec("a", a), get_filespec("b", b));
397 diffcore_std(diffopt);
401 static void output(struct string_list *a, struct string_list *b,
402 struct diff_options *diffopt)
404 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
405 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
409 * We assume the user is really more interested in the second argument
410 * ("newer" version). To that end, we print the output in the order of
411 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
412 * commits that are no longer in the RHS into a good place, we place
413 * them once we have shown all of their predecessors in the LHS.
416 while (i < a->nr || j < b->nr) {
417 struct patch_util *a_util, *b_util;
418 a_util = i < a->nr ? a->items[i].util : NULL;
419 b_util = j < b->nr ? b->items[j].util : NULL;
421 /* Skip all the already-shown commits from the LHS. */
422 while (i < a->nr && a_util->shown)
423 a_util = ++i < a->nr ? a->items[i].util : NULL;
425 /* Show unmatched LHS commit whose predecessors were shown. */
426 if (i < a->nr && a_util->matching < 0) {
427 output_pair_header(diffopt, patch_no_width,
428 &buf, &dashes, a_util, NULL);
433 /* Show unmatched RHS commits. */
434 while (j < b->nr && b_util->matching < 0) {
435 output_pair_header(diffopt, patch_no_width,
436 &buf, &dashes, NULL, b_util);
437 b_util = ++j < b->nr ? b->items[j].util : NULL;
440 /* Show matching LHS/RHS pair. */
442 a_util = a->items[b_util->matching].util;
443 output_pair_header(diffopt, patch_no_width,
444 &buf, &dashes, a_util, b_util);
445 if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
446 patch_diff(a->items[b_util->matching].string,
447 b->items[j].string, diffopt);
452 strbuf_release(&buf);
453 strbuf_release(&dashes);
456 static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
461 int show_range_diff(const char *range1, const char *range2,
462 int creation_factor, int dual_color,
463 struct diff_options *diffopt)
467 struct string_list branch1 = STRING_LIST_INIT_DUP;
468 struct string_list branch2 = STRING_LIST_INIT_DUP;
470 if (read_patches(range1, &branch1))
471 res = error(_("could not parse log for '%s'"), range1);
472 if (!res && read_patches(range2, &branch2))
473 res = error(_("could not parse log for '%s'"), range2);
476 struct diff_options opts;
477 struct strbuf indent = STRBUF_INIT;
480 memcpy(&opts, diffopt, sizeof(opts));
484 if (!opts.output_format)
485 opts.output_format = DIFF_FORMAT_PATCH;
486 opts.flags.suppress_diff_headers = 1;
487 opts.flags.dual_color_diffed_diffs = dual_color;
488 opts.output_prefix = output_prefix_cb;
489 strbuf_addstr(&indent, " ");
490 opts.output_prefix_data = &indent;
491 diff_setup_done(&opts);
493 find_exact_matches(&branch1, &branch2);
494 get_correspondences(&branch1, &branch2, creation_factor);
495 output(&branch1, &branch2, &opts);
497 strbuf_release(&indent);
500 string_list_clear(&branch1, 1);
501 string_list_clear(&branch2, 1);