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"
14
15struct patch_util {
16 /* For the search for an exact match */
17 struct hashmap_entry e;
18 const char *diff, *patch;
19
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;
26};
27
28static size_t find_end_of_line(char *buffer, unsigned long size)
29{
30 char *eol = memchr(buffer, '\n', size);
31
32 if (!eol)
33 return size;
34
35 *eol = '\0';
36 return eol + 1 - buffer;
37}
38
39/*
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).
42 */
43static int read_patches(const char *range, struct string_list *list)
44{
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;
50 int offset, len;
51 size_t size;
52
53 argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
54 "--reverse", "--date-order", "--decorate=no",
55 /*
56 * Choose indicators that are not used anywhere
57 * else in diffs, but still look reasonable
58 * (e.g. will not be confusing when debugging)
59 */
60 "--output-indicator-new=>",
61 "--output-indicator-old=<",
62 "--output-indicator-context=#",
63 "--no-abbrev-commit", range,
64 NULL);
65 cp.out = -1;
66 cp.no_stdin = 1;
67 cp.git_cmd = 1;
68
69 if (start_command(&cp))
70 return error_errno(_("could not start `log`"));
71 if (strbuf_read(&contents, cp.out, 0) < 0) {
72 error_errno(_("could not read `log` output"));
73 finish_command(&cp);
74 return -1;
75 }
76
77 line = contents.buf;
78 size = contents.len;
79 for (offset = 0; size > 0; offset += len, size -= len, line += len) {
80 const char *p;
81
82 len = find_end_of_line(line, size);
83 line[len - 1] = '\0';
84 if (skip_prefix(line, "commit ", &p)) {
85 if (util) {
86 string_list_append(list, buf.buf)->util = util;
87 strbuf_reset(&buf);
88 }
89 util = xcalloc(sizeof(*util), 1);
90 if (get_oid(p, &util->oid)) {
91 error(_("could not parse commit '%s'"), p);
92 free(util);
93 string_list_clear(list, 1);
94 strbuf_release(&buf);
95 strbuf_release(&contents);
96 finish_command(&cp);
97 return -1;
98 }
99 util->matching = -1;
100 in_header = 1;
101 continue;
102 }
103
104 if (starts_with(line, "diff --git")) {
105 struct patch patch = { 0 };
106 struct strbuf root = STRBUF_INIT;
107 int linenr = 0;
108
109 in_header = 0;
110 strbuf_addch(&buf, '\n');
111 if (!util->diff_offset)
112 util->diff_offset = buf.len;
113 line[len - 1] = '\n';
114 len = parse_git_diff_header(&root, &linenr, 1, line,
115 len, size, &patch);
116 if (len < 0)
117 die(_("could not parse git header '%.*s'"), (int)len, line);
118 strbuf_addstr(&buf, " ## ");
119 if (patch.is_new > 0)
120 strbuf_addf(&buf, "%s (new)", patch.new_name);
121 else if (patch.is_delete > 0)
122 strbuf_addf(&buf, "%s (deleted)", patch.old_name);
123 else if (patch.is_rename)
124 strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
125 else
126 strbuf_addstr(&buf, patch.new_name);
127
128 if (patch.new_mode && patch.old_mode &&
129 patch.old_mode != patch.new_mode)
130 strbuf_addf(&buf, " (mode change %06o => %06o)",
131 patch.old_mode, patch.new_mode);
132
133 strbuf_addstr(&buf, " ##");
134 } else if (in_header) {
135 if (starts_with(line, "Author: ")) {
136 strbuf_addstr(&buf, line);
137 strbuf_addstr(&buf, "\n\n");
138 } else if (starts_with(line, " ")) {
139 p = line + len - 2;
140 while (isspace(*p) && p >= line)
141 p--;
142 strbuf_add(&buf, line, p - line + 1);
143 strbuf_addch(&buf, '\n');
144 }
145 continue;
146 } else if (skip_prefix(line, "@@ ", &p)) {
147 p = strstr(p, "@@");
148 strbuf_addstr(&buf, p ? p : "@@");
149 } else if (!line[0])
150 /*
151 * A completely blank (not ' \n', which is context)
152 * line is not valid in a diff. We skip it
153 * silently, because this neatly handles the blank
154 * separator line between commits in git-log
155 * output.
156 */
157 continue;
158 else if (line[0] == '>') {
159 strbuf_addch(&buf, '+');
160 strbuf_addstr(&buf, line + 1);
161 } else if (line[0] == '<') {
162 strbuf_addch(&buf, '-');
163 strbuf_addstr(&buf, line + 1);
164 } else if (line[0] == '#') {
165 strbuf_addch(&buf, ' ');
166 strbuf_addstr(&buf, line + 1);
167 } else {
168 strbuf_addch(&buf, ' ');
169 strbuf_addstr(&buf, line);
170 }
171
172 strbuf_addch(&buf, '\n');
173 util->diffsize++;
174 }
175 strbuf_release(&contents);
176
177 if (util)
178 string_list_append(list, buf.buf)->util = util;
179 strbuf_release(&buf);
180
181 if (finish_command(&cp))
182 return -1;
183
184 return 0;
185}
186
187static int patch_util_cmp(const void *dummy, const struct patch_util *a,
188 const struct patch_util *b, const char *keydata)
189{
190 return strcmp(a->diff, keydata ? keydata : b->diff);
191}
192
193static void find_exact_matches(struct string_list *a, struct string_list *b)
194{
195 struct hashmap map;
196 int i;
197
198 hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
199
200 /* First, add the patches of a to a hash map */
201 for (i = 0; i < a->nr; i++) {
202 struct patch_util *util = a->items[i].util;
203
204 util->i = i;
205 util->patch = a->items[i].string;
206 util->diff = util->patch + util->diff_offset;
207 hashmap_entry_init(util, strhash(util->diff));
208 hashmap_add(&map, util);
209 }
210
211 /* Now try to find exact matches in b */
212 for (i = 0; i < b->nr; i++) {
213 struct patch_util *util = b->items[i].util, *other;
214
215 util->i = i;
216 util->patch = b->items[i].string;
217 util->diff = util->patch + util->diff_offset;
218 hashmap_entry_init(util, strhash(util->diff));
219 other = hashmap_remove(&map, util, NULL);
220 if (other) {
221 if (other->matching >= 0)
222 BUG("already assigned!");
223
224 other->matching = i;
225 util->matching = other->i;
226 }
227 }
228
229 hashmap_free(&map, 0);
230}
231
232static void diffsize_consume(void *data, char *line, unsigned long len)
233{
234 (*(int *)data)++;
235}
236
237static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
238 const char *funcline, long funclen)
239{
240 diffsize_consume(data, NULL, 0);
241}
242
243static int diffsize(const char *a, const char *b)
244{
245 xpparam_t pp = { 0 };
246 xdemitconf_t cfg = { 0 };
247 mmfile_t mf1, mf2;
248 int count = 0;
249
250 mf1.ptr = (char *)a;
251 mf1.size = strlen(a);
252 mf2.ptr = (char *)b;
253 mf2.size = strlen(b);
254
255 cfg.ctxlen = 3;
256 if (!xdi_diff_outf(&mf1, &mf2,
257 diffsize_hunk, diffsize_consume, &count,
258 &pp, &cfg))
259 return count;
260
261 error(_("failed to generate diff"));
262 return COST_MAX;
263}
264
265static void get_correspondences(struct string_list *a, struct string_list *b,
266 int creation_factor)
267{
268 int n = a->nr + b->nr;
269 int *cost, c, *a2b, *b2a;
270 int i, j;
271
272 ALLOC_ARRAY(cost, st_mult(n, n));
273 ALLOC_ARRAY(a2b, n);
274 ALLOC_ARRAY(b2a, n);
275
276 for (i = 0; i < a->nr; i++) {
277 struct patch_util *a_util = a->items[i].util;
278
279 for (j = 0; j < b->nr; j++) {
280 struct patch_util *b_util = b->items[j].util;
281
282 if (a_util->matching == j)
283 c = 0;
284 else if (a_util->matching < 0 && b_util->matching < 0)
285 c = diffsize(a_util->diff, b_util->diff);
286 else
287 c = COST_MAX;
288 cost[i + n * j] = c;
289 }
290
291 c = a_util->matching < 0 ?
292 a_util->diffsize * creation_factor / 100 : COST_MAX;
293 for (j = b->nr; j < n; j++)
294 cost[i + n * j] = c;
295 }
296
297 for (j = 0; j < b->nr; j++) {
298 struct patch_util *util = b->items[j].util;
299
300 c = util->matching < 0 ?
301 util->diffsize * creation_factor / 100 : COST_MAX;
302 for (i = a->nr; i < n; i++)
303 cost[i + n * j] = c;
304 }
305
306 for (i = a->nr; i < n; i++)
307 for (j = b->nr; j < n; j++)
308 cost[i + n * j] = 0;
309
310 compute_assignment(n, n, cost, a2b, b2a);
311
312 for (i = 0; i < a->nr; i++)
313 if (a2b[i] >= 0 && a2b[i] < b->nr) {
314 struct patch_util *a_util = a->items[i].util;
315 struct patch_util *b_util = b->items[a2b[i]].util;
316
317 a_util->matching = a2b[i];
318 b_util->matching = i;
319 }
320
321 free(cost);
322 free(a2b);
323 free(b2a);
324}
325
326static void output_pair_header(struct diff_options *diffopt,
327 int patch_no_width,
328 struct strbuf *buf,
329 struct strbuf *dashes,
330 struct patch_util *a_util,
331 struct patch_util *b_util)
332{
333 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
334 struct commit *commit;
335 char status;
336 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
337 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
338 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
339 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
340 const char *color;
341
342 if (!dashes->len)
343 strbuf_addchars(dashes, '-',
344 strlen(find_unique_abbrev(oid,
345 DEFAULT_ABBREV)));
346
347 if (!b_util) {
348 color = color_old;
349 status = '<';
350 } else if (!a_util) {
351 color = color_new;
352 status = '>';
353 } else if (strcmp(a_util->patch, b_util->patch)) {
354 color = color_commit;
355 status = '!';
356 } else {
357 color = color_commit;
358 status = '=';
359 }
360
361 strbuf_reset(buf);
362 strbuf_addstr(buf, status == '!' ? color_old : color);
363 if (!a_util)
364 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
365 else
366 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
367 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
368
369 if (status == '!')
370 strbuf_addf(buf, "%s%s", color_reset, color);
371 strbuf_addch(buf, status);
372 if (status == '!')
373 strbuf_addf(buf, "%s%s", color_reset, color_new);
374
375 if (!b_util)
376 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
377 else
378 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
379 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
380
381 commit = lookup_commit_reference(the_repository, oid);
382 if (commit) {
383 if (status == '!')
384 strbuf_addf(buf, "%s%s", color_reset, color);
385
386 strbuf_addch(buf, ' ');
387 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
388 }
389 strbuf_addf(buf, "%s\n", color_reset);
390
391 fwrite(buf->buf, buf->len, 1, diffopt->file);
392}
393
394static struct userdiff_driver no_func_name = {
395 .funcname = { "$^", 0 }
396};
397
398static struct diff_filespec *get_filespec(const char *name, const char *p)
399{
400 struct diff_filespec *spec = alloc_filespec(name);
401
402 fill_filespec(spec, &null_oid, 0, 0100644);
403 spec->data = (char *)p;
404 spec->size = strlen(p);
405 spec->should_munmap = 0;
406 spec->is_stdin = 1;
407 spec->driver = &no_func_name;
408
409 return spec;
410}
411
412static void patch_diff(const char *a, const char *b,
413 struct diff_options *diffopt)
414{
415 diff_queue(&diff_queued_diff,
416 get_filespec("a", a), get_filespec("b", b));
417
418 diffcore_std(diffopt);
419 diff_flush(diffopt);
420}
421
422static void output(struct string_list *a, struct string_list *b,
423 struct diff_options *diffopt)
424{
425 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
426 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
427 int i = 0, j = 0;
428
429 /*
430 * We assume the user is really more interested in the second argument
431 * ("newer" version). To that end, we print the output in the order of
432 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
433 * commits that are no longer in the RHS into a good place, we place
434 * them once we have shown all of their predecessors in the LHS.
435 */
436
437 while (i < a->nr || j < b->nr) {
438 struct patch_util *a_util, *b_util;
439 a_util = i < a->nr ? a->items[i].util : NULL;
440 b_util = j < b->nr ? b->items[j].util : NULL;
441
442 /* Skip all the already-shown commits from the LHS. */
443 while (i < a->nr && a_util->shown)
444 a_util = ++i < a->nr ? a->items[i].util : NULL;
445
446 /* Show unmatched LHS commit whose predecessors were shown. */
447 if (i < a->nr && a_util->matching < 0) {
448 output_pair_header(diffopt, patch_no_width,
449 &buf, &dashes, a_util, NULL);
450 i++;
451 continue;
452 }
453
454 /* Show unmatched RHS commits. */
455 while (j < b->nr && b_util->matching < 0) {
456 output_pair_header(diffopt, patch_no_width,
457 &buf, &dashes, NULL, b_util);
458 b_util = ++j < b->nr ? b->items[j].util : NULL;
459 }
460
461 /* Show matching LHS/RHS pair. */
462 if (j < b->nr) {
463 a_util = a->items[b_util->matching].util;
464 output_pair_header(diffopt, patch_no_width,
465 &buf, &dashes, a_util, b_util);
466 if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
467 patch_diff(a->items[b_util->matching].string,
468 b->items[j].string, diffopt);
469 a_util->shown = 1;
470 j++;
471 }
472 }
473 strbuf_release(&buf);
474 strbuf_release(&dashes);
475}
476
477static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
478{
479 return data;
480}
481
482int show_range_diff(const char *range1, const char *range2,
483 int creation_factor, int dual_color,
484 struct diff_options *diffopt)
485{
486 int res = 0;
487
488 struct string_list branch1 = STRING_LIST_INIT_DUP;
489 struct string_list branch2 = STRING_LIST_INIT_DUP;
490
491 if (read_patches(range1, &branch1))
492 res = error(_("could not parse log for '%s'"), range1);
493 if (!res && read_patches(range2, &branch2))
494 res = error(_("could not parse log for '%s'"), range2);
495
496 if (!res) {
497 struct diff_options opts;
498 struct strbuf indent = STRBUF_INIT;
499
500 if (diffopt)
501 memcpy(&opts, diffopt, sizeof(opts));
502 else
503 diff_setup(&opts);
504
505 if (!opts.output_format)
506 opts.output_format = DIFF_FORMAT_PATCH;
507 opts.flags.suppress_diff_headers = 1;
508 opts.flags.dual_color_diffed_diffs = dual_color;
509 opts.flags.suppress_hunk_header_line_count = 1;
510 opts.output_prefix = output_prefix_cb;
511 strbuf_addstr(&indent, " ");
512 opts.output_prefix_data = &indent;
513 diff_setup_done(&opts);
514
515 find_exact_matches(&branch1, &branch2);
516 get_correspondences(&branch1, &branch2, creation_factor);
517 output(&branch1, &branch2, &opts);
518
519 strbuf_release(&indent);
520 }
521
522 string_list_clear(&branch1, 1);
523 string_list_clear(&branch2, 1);
524
525 return res;
526}