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