dir: remove stray quote character in comment
[alt-git.git] / range-diff.c
blob7fed5a3b4bb54a53f434cf23fb96151e9a8f824d
1 #include "cache.h"
2 #include "range-diff.h"
3 #include "string-list.h"
4 #include "run-command.h"
5 #include "argv-array.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"
15 struct patch_util {
16 /* For the search for an exact match */
17 struct hashmap_entry e;
18 const char *diff, *patch;
20 int i, shown;
21 int diffsize;
22 size_t diff_offset;
23 /* the index of the matching item in the other branch, or -1 */
24 int matching;
25 struct object_id oid;
28 static size_t find_end_of_line(char *buffer, unsigned long size)
30 char *eol = memchr(buffer, '\n', size);
32 if (!eol)
33 return size;
35 *eol = '\0';
36 return eol + 1 - buffer;
40 * Reads the patches into a string list, with the `util` field being populated
41 * as struct object_id (will need to be free()d).
43 static int read_patches(const char *range, struct string_list *list)
45 struct child_process cp = CHILD_PROCESS_INIT;
46 struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
47 struct patch_util *util = NULL;
48 int in_header = 1;
49 char *line, *current_filename = NULL;
50 int offset, len;
51 size_t size;
53 argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
54 "--reverse", "--date-order", "--decorate=no",
55 "--no-prefix",
57 * Choose indicators that are not used anywhere
58 * else in diffs, but still look reasonable
59 * (e.g. will not be confusing when debugging)
61 "--output-indicator-new=>",
62 "--output-indicator-old=<",
63 "--output-indicator-context=#",
64 "--no-abbrev-commit", range,
65 NULL);
66 cp.out = -1;
67 cp.no_stdin = 1;
68 cp.git_cmd = 1;
70 if (start_command(&cp))
71 return error_errno(_("could not start `log`"));
72 if (strbuf_read(&contents, cp.out, 0) < 0) {
73 error_errno(_("could not read `log` output"));
74 finish_command(&cp);
75 return -1;
78 line = contents.buf;
79 size = contents.len;
80 for (offset = 0; size > 0; offset += len, size -= len, line += len) {
81 const char *p;
83 len = find_end_of_line(line, size);
84 line[len - 1] = '\0';
85 if (skip_prefix(line, "commit ", &p)) {
86 if (util) {
87 string_list_append(list, buf.buf)->util = util;
88 strbuf_reset(&buf);
90 util = xcalloc(sizeof(*util), 1);
91 if (get_oid(p, &util->oid)) {
92 error(_("could not parse commit '%s'"), p);
93 free(util);
94 string_list_clear(list, 1);
95 strbuf_release(&buf);
96 strbuf_release(&contents);
97 finish_command(&cp);
98 return -1;
100 util->matching = -1;
101 in_header = 1;
102 continue;
105 if (starts_with(line, "diff --git")) {
106 struct patch patch = { 0 };
107 struct strbuf root = STRBUF_INIT;
108 int linenr = 0;
110 in_header = 0;
111 strbuf_addch(&buf, '\n');
112 if (!util->diff_offset)
113 util->diff_offset = buf.len;
114 line[len - 1] = '\n';
115 len = parse_git_diff_header(&root, &linenr, 0, line,
116 len, size, &patch);
117 if (len < 0)
118 die(_("could not parse git header '%.*s'"), (int)len, line);
119 strbuf_addstr(&buf, " ## ");
120 if (patch.is_new > 0)
121 strbuf_addf(&buf, "%s (new)", patch.new_name);
122 else if (patch.is_delete > 0)
123 strbuf_addf(&buf, "%s (deleted)", patch.old_name);
124 else if (patch.is_rename)
125 strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
126 else
127 strbuf_addstr(&buf, patch.new_name);
129 free(current_filename);
130 if (patch.is_delete > 0)
131 current_filename = xstrdup(patch.old_name);
132 else
133 current_filename = xstrdup(patch.new_name);
135 if (patch.new_mode && patch.old_mode &&
136 patch.old_mode != patch.new_mode)
137 strbuf_addf(&buf, " (mode change %06o => %06o)",
138 patch.old_mode, patch.new_mode);
140 strbuf_addstr(&buf, " ##");
141 } else if (in_header) {
142 if (starts_with(line, "Author: ")) {
143 strbuf_addstr(&buf, " ## Metadata ##\n");
144 strbuf_addstr(&buf, line);
145 strbuf_addstr(&buf, "\n\n");
146 strbuf_addstr(&buf, " ## Commit message ##\n");
147 } else if (starts_with(line, " ")) {
148 p = line + len - 2;
149 while (isspace(*p) && p >= line)
150 p--;
151 strbuf_add(&buf, line, p - line + 1);
152 strbuf_addch(&buf, '\n');
154 continue;
155 } else if (skip_prefix(line, "@@ ", &p)) {
156 p = strstr(p, "@@");
157 strbuf_addstr(&buf, "@@");
158 if (current_filename && p[2])
159 strbuf_addf(&buf, " %s:", current_filename);
160 if (p)
161 strbuf_addstr(&buf, p + 2);
162 } else if (!line[0])
164 * A completely blank (not ' \n', which is context)
165 * line is not valid in a diff. We skip it
166 * silently, because this neatly handles the blank
167 * separator line between commits in git-log
168 * output.
170 continue;
171 else if (line[0] == '>') {
172 strbuf_addch(&buf, '+');
173 strbuf_addstr(&buf, line + 1);
174 } else if (line[0] == '<') {
175 strbuf_addch(&buf, '-');
176 strbuf_addstr(&buf, line + 1);
177 } else if (line[0] == '#') {
178 strbuf_addch(&buf, ' ');
179 strbuf_addstr(&buf, line + 1);
180 } else {
181 strbuf_addch(&buf, ' ');
182 strbuf_addstr(&buf, line);
185 strbuf_addch(&buf, '\n');
186 util->diffsize++;
188 strbuf_release(&contents);
190 if (util)
191 string_list_append(list, buf.buf)->util = util;
192 strbuf_release(&buf);
193 free(current_filename);
195 if (finish_command(&cp))
196 return -1;
198 return 0;
201 static int patch_util_cmp(const void *dummy, const struct patch_util *a,
202 const struct patch_util *b, const char *keydata)
204 return strcmp(a->diff, keydata ? keydata : b->diff);
207 static void find_exact_matches(struct string_list *a, struct string_list *b)
209 struct hashmap map;
210 int i;
212 hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
214 /* First, add the patches of a to a hash map */
215 for (i = 0; i < a->nr; i++) {
216 struct patch_util *util = a->items[i].util;
218 util->i = i;
219 util->patch = a->items[i].string;
220 util->diff = util->patch + util->diff_offset;
221 hashmap_entry_init(&util->e, strhash(util->diff));
222 hashmap_add(&map, &util->e);
225 /* Now try to find exact matches in b */
226 for (i = 0; i < b->nr; i++) {
227 struct patch_util *util = b->items[i].util, *other;
229 util->i = i;
230 util->patch = b->items[i].string;
231 util->diff = util->patch + util->diff_offset;
232 hashmap_entry_init(&util->e, strhash(util->diff));
233 other = hashmap_remove_entry(&map, util, e, NULL);
234 if (other) {
235 if (other->matching >= 0)
236 BUG("already assigned!");
238 other->matching = i;
239 util->matching = other->i;
243 hashmap_free(&map);
246 static void diffsize_consume(void *data, char *line, unsigned long len)
248 (*(int *)data)++;
251 static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
252 const char *funcline, long funclen)
254 diffsize_consume(data, NULL, 0);
257 static int diffsize(const char *a, const char *b)
259 xpparam_t pp = { 0 };
260 xdemitconf_t cfg = { 0 };
261 mmfile_t mf1, mf2;
262 int count = 0;
264 mf1.ptr = (char *)a;
265 mf1.size = strlen(a);
266 mf2.ptr = (char *)b;
267 mf2.size = strlen(b);
269 cfg.ctxlen = 3;
270 if (!xdi_diff_outf(&mf1, &mf2,
271 diffsize_hunk, diffsize_consume, &count,
272 &pp, &cfg))
273 return count;
275 error(_("failed to generate diff"));
276 return COST_MAX;
279 static void get_correspondences(struct string_list *a, struct string_list *b,
280 int creation_factor)
282 int n = a->nr + b->nr;
283 int *cost, c, *a2b, *b2a;
284 int i, j;
286 ALLOC_ARRAY(cost, st_mult(n, n));
287 ALLOC_ARRAY(a2b, n);
288 ALLOC_ARRAY(b2a, n);
290 for (i = 0; i < a->nr; i++) {
291 struct patch_util *a_util = a->items[i].util;
293 for (j = 0; j < b->nr; j++) {
294 struct patch_util *b_util = b->items[j].util;
296 if (a_util->matching == j)
297 c = 0;
298 else if (a_util->matching < 0 && b_util->matching < 0)
299 c = diffsize(a_util->diff, b_util->diff);
300 else
301 c = COST_MAX;
302 cost[i + n * j] = c;
305 c = a_util->matching < 0 ?
306 a_util->diffsize * creation_factor / 100 : COST_MAX;
307 for (j = b->nr; j < n; j++)
308 cost[i + n * j] = c;
311 for (j = 0; j < b->nr; j++) {
312 struct patch_util *util = b->items[j].util;
314 c = util->matching < 0 ?
315 util->diffsize * creation_factor / 100 : COST_MAX;
316 for (i = a->nr; i < n; i++)
317 cost[i + n * j] = c;
320 for (i = a->nr; i < n; i++)
321 for (j = b->nr; j < n; j++)
322 cost[i + n * j] = 0;
324 compute_assignment(n, n, cost, a2b, b2a);
326 for (i = 0; i < a->nr; i++)
327 if (a2b[i] >= 0 && a2b[i] < b->nr) {
328 struct patch_util *a_util = a->items[i].util;
329 struct patch_util *b_util = b->items[a2b[i]].util;
331 a_util->matching = a2b[i];
332 b_util->matching = i;
335 free(cost);
336 free(a2b);
337 free(b2a);
340 static void output_pair_header(struct diff_options *diffopt,
341 int patch_no_width,
342 struct strbuf *buf,
343 struct strbuf *dashes,
344 struct patch_util *a_util,
345 struct patch_util *b_util)
347 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
348 struct commit *commit;
349 char status;
350 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
351 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
352 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
353 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
354 const char *color;
356 if (!dashes->len)
357 strbuf_addchars(dashes, '-',
358 strlen(find_unique_abbrev(oid,
359 DEFAULT_ABBREV)));
361 if (!b_util) {
362 color = color_old;
363 status = '<';
364 } else if (!a_util) {
365 color = color_new;
366 status = '>';
367 } else if (strcmp(a_util->patch, b_util->patch)) {
368 color = color_commit;
369 status = '!';
370 } else {
371 color = color_commit;
372 status = '=';
375 strbuf_reset(buf);
376 strbuf_addstr(buf, status == '!' ? color_old : color);
377 if (!a_util)
378 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
379 else
380 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
381 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
383 if (status == '!')
384 strbuf_addf(buf, "%s%s", color_reset, color);
385 strbuf_addch(buf, status);
386 if (status == '!')
387 strbuf_addf(buf, "%s%s", color_reset, color_new);
389 if (!b_util)
390 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
391 else
392 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
393 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
395 commit = lookup_commit_reference(the_repository, oid);
396 if (commit) {
397 if (status == '!')
398 strbuf_addf(buf, "%s%s", color_reset, color);
400 strbuf_addch(buf, ' ');
401 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
403 strbuf_addf(buf, "%s\n", color_reset);
405 fwrite(buf->buf, buf->len, 1, diffopt->file);
408 static struct userdiff_driver section_headers = {
409 .funcname = { "^ ## (.*) ##$\n"
410 "^.?@@ (.*)$", REG_EXTENDED }
413 static struct diff_filespec *get_filespec(const char *name, const char *p)
415 struct diff_filespec *spec = alloc_filespec(name);
417 fill_filespec(spec, &null_oid, 0, 0100644);
418 spec->data = (char *)p;
419 spec->size = strlen(p);
420 spec->should_munmap = 0;
421 spec->is_stdin = 1;
422 spec->driver = &section_headers;
424 return spec;
427 static void patch_diff(const char *a, const char *b,
428 struct diff_options *diffopt)
430 diff_queue(&diff_queued_diff,
431 get_filespec("a", a), get_filespec("b", b));
433 diffcore_std(diffopt);
434 diff_flush(diffopt);
437 static void output(struct string_list *a, struct string_list *b,
438 struct diff_options *diffopt)
440 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
441 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
442 int i = 0, j = 0;
445 * We assume the user is really more interested in the second argument
446 * ("newer" version). To that end, we print the output in the order of
447 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
448 * commits that are no longer in the RHS into a good place, we place
449 * them once we have shown all of their predecessors in the LHS.
452 while (i < a->nr || j < b->nr) {
453 struct patch_util *a_util, *b_util;
454 a_util = i < a->nr ? a->items[i].util : NULL;
455 b_util = j < b->nr ? b->items[j].util : NULL;
457 /* Skip all the already-shown commits from the LHS. */
458 while (i < a->nr && a_util->shown)
459 a_util = ++i < a->nr ? a->items[i].util : NULL;
461 /* Show unmatched LHS commit whose predecessors were shown. */
462 if (i < a->nr && a_util->matching < 0) {
463 output_pair_header(diffopt, patch_no_width,
464 &buf, &dashes, a_util, NULL);
465 i++;
466 continue;
469 /* Show unmatched RHS commits. */
470 while (j < b->nr && b_util->matching < 0) {
471 output_pair_header(diffopt, patch_no_width,
472 &buf, &dashes, NULL, b_util);
473 b_util = ++j < b->nr ? b->items[j].util : NULL;
476 /* Show matching LHS/RHS pair. */
477 if (j < b->nr) {
478 a_util = a->items[b_util->matching].util;
479 output_pair_header(diffopt, patch_no_width,
480 &buf, &dashes, a_util, b_util);
481 if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
482 patch_diff(a->items[b_util->matching].string,
483 b->items[j].string, diffopt);
484 a_util->shown = 1;
485 j++;
488 strbuf_release(&buf);
489 strbuf_release(&dashes);
492 static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
494 return data;
497 int show_range_diff(const char *range1, const char *range2,
498 int creation_factor, int dual_color,
499 struct diff_options *diffopt)
501 int res = 0;
503 struct string_list branch1 = STRING_LIST_INIT_DUP;
504 struct string_list branch2 = STRING_LIST_INIT_DUP;
506 if (read_patches(range1, &branch1))
507 res = error(_("could not parse log for '%s'"), range1);
508 if (!res && read_patches(range2, &branch2))
509 res = error(_("could not parse log for '%s'"), range2);
511 if (!res) {
512 struct diff_options opts;
513 struct strbuf indent = STRBUF_INIT;
515 if (diffopt)
516 memcpy(&opts, diffopt, sizeof(opts));
517 else
518 diff_setup(&opts);
520 if (!opts.output_format)
521 opts.output_format = DIFF_FORMAT_PATCH;
522 opts.flags.suppress_diff_headers = 1;
523 opts.flags.dual_color_diffed_diffs = dual_color;
524 opts.flags.suppress_hunk_header_line_count = 1;
525 opts.output_prefix = output_prefix_cb;
526 strbuf_addstr(&indent, " ");
527 opts.output_prefix_data = &indent;
528 diff_setup_done(&opts);
530 find_exact_matches(&branch1, &branch2);
531 get_correspondences(&branch1, &branch2, creation_factor);
532 output(&branch1, &branch2, &opts);
534 strbuf_release(&indent);
537 string_list_clear(&branch1, 1);
538 string_list_clear(&branch2, 1);
540 return res;