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