784fac301bb6cc432d7e7fb69de066125c6aa594
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
14struct patch_util {
15 /* For the search for an exact match */
16 struct hashmap_entry e;
17 const char *diff, *patch;
18
19 int i, shown;
20 int diffsize;
21 size_t diff_offset;
22 /* the index of the matching item in the other branch, or -1 */
23 int matching;
24 struct object_id oid;
25};
26
27static size_t find_end_of_line(char *buffer, unsigned long size)
28{
29 char *eol = memchr(buffer, '\n', size);
30
31 if (!eol)
32 return size;
33
34 *eol = '\0';
35 return eol + 1 - buffer;
36}
37
38/*
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).
41 */
42static int read_patches(const char *range, struct string_list *list)
43{
44 struct child_process cp = CHILD_PROCESS_INIT;
45 struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
46 struct patch_util *util = NULL;
47 int in_header = 1;
48 char *line;
49 int offset, len;
50 size_t size;
51
52 argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
53 "--reverse", "--date-order", "--decorate=no",
54 /*
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)
58 */
59 "--output-indicator-new=>",
60 "--output-indicator-old=<",
61 "--output-indicator-context=#",
62 "--no-abbrev-commit", range,
63 NULL);
64 cp.out = -1;
65 cp.no_stdin = 1;
66 cp.git_cmd = 1;
67
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"));
72 finish_command(&cp);
73 return -1;
74 }
75
76 line = contents.buf;
77 size = contents.len;
78 for (offset = 0; size > 0; offset += len, size -= len, line += len) {
79 const char *p;
80
81 len = find_end_of_line(line, size);
82 line[len - 1] = '\0';
83 if (skip_prefix(line, "commit ", &p)) {
84 if (util) {
85 string_list_append(list, buf.buf)->util = util;
86 strbuf_reset(&buf);
87 }
88 util = xcalloc(sizeof(*util), 1);
89 if (get_oid(p, &util->oid)) {
90 error(_("could not parse commit '%s'"), p);
91 free(util);
92 string_list_clear(list, 1);
93 strbuf_release(&buf);
94 strbuf_release(&contents);
95 finish_command(&cp);
96 return -1;
97 }
98 util->matching = -1;
99 in_header = 1;
100 continue;
101 }
102
103 if (starts_with(line, "diff --git")) {
104 in_header = 0;
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, " ")) {
115 p = line + len - 2;
116 while (isspace(*p) && p >= line)
117 p--;
118 strbuf_add(&buf, line, p - line + 1);
119 strbuf_addch(&buf, '\n');
120 }
121 continue;
122 } else if (starts_with(line, "@@ "))
123 strbuf_addstr(&buf, "@@");
124 else if (!line[0] || starts_with(line, "index "))
125 /*
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
130 * output.
131 *
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.
135 */
136 continue;
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);
146 } else {
147 strbuf_addch(&buf, ' ');
148 strbuf_addstr(&buf, line);
149 }
150
151 strbuf_addch(&buf, '\n');
152 util->diffsize++;
153 }
154 strbuf_release(&contents);
155
156 if (util)
157 string_list_append(list, buf.buf)->util = util;
158 strbuf_release(&buf);
159
160 if (finish_command(&cp))
161 return -1;
162
163 return 0;
164}
165
166static int patch_util_cmp(const void *dummy, const struct patch_util *a,
167 const struct patch_util *b, const char *keydata)
168{
169 return strcmp(a->diff, keydata ? keydata : b->diff);
170}
171
172static void find_exact_matches(struct string_list *a, struct string_list *b)
173{
174 struct hashmap map;
175 int i;
176
177 hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
178
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;
182
183 util->i = i;
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);
188 }
189
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;
193
194 util->i = i;
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);
199 if (other) {
200 if (other->matching >= 0)
201 BUG("already assigned!");
202
203 other->matching = i;
204 util->matching = other->i;
205 }
206 }
207
208 hashmap_free(&map, 0);
209}
210
211static void diffsize_consume(void *data, char *line, unsigned long len)
212{
213 (*(int *)data)++;
214}
215
216static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
217 const char *funcline, long funclen)
218{
219 diffsize_consume(data, NULL, 0);
220}
221
222static int diffsize(const char *a, const char *b)
223{
224 xpparam_t pp = { 0 };
225 xdemitconf_t cfg = { 0 };
226 mmfile_t mf1, mf2;
227 int count = 0;
228
229 mf1.ptr = (char *)a;
230 mf1.size = strlen(a);
231 mf2.ptr = (char *)b;
232 mf2.size = strlen(b);
233
234 cfg.ctxlen = 3;
235 if (!xdi_diff_outf(&mf1, &mf2,
236 diffsize_hunk, diffsize_consume, &count,
237 &pp, &cfg))
238 return count;
239
240 error(_("failed to generate diff"));
241 return COST_MAX;
242}
243
244static void get_correspondences(struct string_list *a, struct string_list *b,
245 int creation_factor)
246{
247 int n = a->nr + b->nr;
248 int *cost, c, *a2b, *b2a;
249 int i, j;
250
251 ALLOC_ARRAY(cost, st_mult(n, n));
252 ALLOC_ARRAY(a2b, n);
253 ALLOC_ARRAY(b2a, n);
254
255 for (i = 0; i < a->nr; i++) {
256 struct patch_util *a_util = a->items[i].util;
257
258 for (j = 0; j < b->nr; j++) {
259 struct patch_util *b_util = b->items[j].util;
260
261 if (a_util->matching == j)
262 c = 0;
263 else if (a_util->matching < 0 && b_util->matching < 0)
264 c = diffsize(a_util->diff, b_util->diff);
265 else
266 c = COST_MAX;
267 cost[i + n * j] = c;
268 }
269
270 c = a_util->matching < 0 ?
271 a_util->diffsize * creation_factor / 100 : COST_MAX;
272 for (j = b->nr; j < n; j++)
273 cost[i + n * j] = c;
274 }
275
276 for (j = 0; j < b->nr; j++) {
277 struct patch_util *util = b->items[j].util;
278
279 c = util->matching < 0 ?
280 util->diffsize * creation_factor / 100 : COST_MAX;
281 for (i = a->nr; i < n; i++)
282 cost[i + n * j] = c;
283 }
284
285 for (i = a->nr; i < n; i++)
286 for (j = b->nr; j < n; j++)
287 cost[i + n * j] = 0;
288
289 compute_assignment(n, n, cost, a2b, b2a);
290
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;
295
296 a_util->matching = a2b[i];
297 b_util->matching = i;
298 }
299
300 free(cost);
301 free(a2b);
302 free(b2a);
303}
304
305static void output_pair_header(struct diff_options *diffopt,
306 int patch_no_width,
307 struct strbuf *buf,
308 struct strbuf *dashes,
309 struct patch_util *a_util,
310 struct patch_util *b_util)
311{
312 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
313 struct commit *commit;
314 char status;
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);
319 const char *color;
320
321 if (!dashes->len)
322 strbuf_addchars(dashes, '-',
323 strlen(find_unique_abbrev(oid,
324 DEFAULT_ABBREV)));
325
326 if (!b_util) {
327 color = color_old;
328 status = '<';
329 } else if (!a_util) {
330 color = color_new;
331 status = '>';
332 } else if (strcmp(a_util->patch, b_util->patch)) {
333 color = color_commit;
334 status = '!';
335 } else {
336 color = color_commit;
337 status = '=';
338 }
339
340 strbuf_reset(buf);
341 strbuf_addstr(buf, status == '!' ? color_old : color);
342 if (!a_util)
343 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
344 else
345 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
346 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
347
348 if (status == '!')
349 strbuf_addf(buf, "%s%s", color_reset, color);
350 strbuf_addch(buf, status);
351 if (status == '!')
352 strbuf_addf(buf, "%s%s", color_reset, color_new);
353
354 if (!b_util)
355 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
356 else
357 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
358 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
359
360 commit = lookup_commit_reference(the_repository, oid);
361 if (commit) {
362 if (status == '!')
363 strbuf_addf(buf, "%s%s", color_reset, color);
364
365 strbuf_addch(buf, ' ');
366 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
367 }
368 strbuf_addf(buf, "%s\n", color_reset);
369
370 fwrite(buf->buf, buf->len, 1, diffopt->file);
371}
372
373static struct userdiff_driver no_func_name = {
374 .funcname = { "$^", 0 }
375};
376
377static struct diff_filespec *get_filespec(const char *name, const char *p)
378{
379 struct diff_filespec *spec = alloc_filespec(name);
380
381 fill_filespec(spec, &null_oid, 0, 0100644);
382 spec->data = (char *)p;
383 spec->size = strlen(p);
384 spec->should_munmap = 0;
385 spec->is_stdin = 1;
386 spec->driver = &no_func_name;
387
388 return spec;
389}
390
391static void patch_diff(const char *a, const char *b,
392 struct diff_options *diffopt)
393{
394 diff_queue(&diff_queued_diff,
395 get_filespec("a", a), get_filespec("b", b));
396
397 diffcore_std(diffopt);
398 diff_flush(diffopt);
399}
400
401static void output(struct string_list *a, struct string_list *b,
402 struct diff_options *diffopt)
403{
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));
406 int i = 0, j = 0;
407
408 /*
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.
414 */
415
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;
420
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;
424
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);
429 i++;
430 continue;
431 }
432
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;
438 }
439
440 /* Show matching LHS/RHS pair. */
441 if (j < b->nr) {
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);
448 a_util->shown = 1;
449 j++;
450 }
451 }
452 strbuf_release(&buf);
453 strbuf_release(&dashes);
454}
455
456static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
457{
458 return data;
459}
460
461int show_range_diff(const char *range1, const char *range2,
462 int creation_factor, int dual_color,
463 struct diff_options *diffopt)
464{
465 int res = 0;
466
467 struct string_list branch1 = STRING_LIST_INIT_DUP;
468 struct string_list branch2 = STRING_LIST_INIT_DUP;
469
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);
474
475 if (!res) {
476 struct diff_options opts;
477 struct strbuf indent = STRBUF_INIT;
478
479 if (diffopt)
480 memcpy(&opts, diffopt, sizeof(opts));
481 else
482 diff_setup(&opts);
483
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);
492
493 find_exact_matches(&branch1, &branch2);
494 get_correspondences(&branch1, &branch2, creation_factor);
495 output(&branch1, &branch2, &opts);
496
497 strbuf_release(&indent);
498 }
499
500 string_list_clear(&branch1, 1);
501 string_list_clear(&branch2, 1);
502
503 return res;
504}