]> git.ipfire.org Git - thirdparty/git.git/blame - range-diff.c
lockfile API users: simplify and don't leak "path"
[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"
359f0d75 14#include "revision.h"
d9c66f0b
JS
15
16struct patch_util {
17 /* For the search for an exact match */
18 struct hashmap_entry e;
19 const char *diff, *patch;
20
9dc46e02 21 int i, shown;
d9c66f0b
JS
22 int diffsize;
23 size_t diff_offset;
24 /* the index of the matching item in the other branch, or -1 */
25 int matching;
26 struct object_id oid;
27};
28
29/*
30 * Reads the patches into a string list, with the `util` field being populated
31 * as struct object_id (will need to be free()d).
32 */
bd361918 33static int read_patches(const char *range, struct string_list *list,
c972bf4c 34 const struct strvec *other_arg)
d9c66f0b
JS
35{
36 struct child_process cp = CHILD_PROCESS_INIT;
44b67cb6 37 struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
d9c66f0b
JS
38 struct patch_util *util = NULL;
39 int in_header = 1;
444e0969 40 char *line, *current_filename = NULL;
c4d59073 41 ssize_t len;
44b67cb6 42 size_t size;
d9c66f0b 43
c972bf4c 44 strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
f6d8942b
JK
45 "--reverse", "--date-order", "--decorate=no",
46 "--no-prefix",
47 /*
48 * Choose indicators that are not used anywhere
49 * else in diffs, but still look reasonable
50 * (e.g. will not be confusing when debugging)
51 */
52 "--output-indicator-new=>",
53 "--output-indicator-old=<",
54 "--output-indicator-context=#",
55 "--no-abbrev-commit",
56 "--pretty=medium",
57 "--notes",
58 NULL);
bd361918 59 if (other_arg)
d70a9eb6 60 strvec_pushv(&cp.args, other_arg->v);
c972bf4c 61 strvec_push(&cp.args, range);
d9c66f0b
JS
62 cp.out = -1;
63 cp.no_stdin = 1;
64 cp.git_cmd = 1;
65
66 if (start_command(&cp))
67 return error_errno(_("could not start `log`"));
44b67cb6 68 if (strbuf_read(&contents, cp.out, 0) < 0) {
d9c66f0b
JS
69 error_errno(_("could not read `log` output"));
70 finish_command(&cp);
71 return -1;
72 }
5189bb87
JS
73 if (finish_command(&cp))
74 return -1;
d9c66f0b 75
44b67cb6
TG
76 line = contents.buf;
77 size = contents.len;
47ac23d3 78 for (; size > 0; size -= len, line += len) {
d9c66f0b 79 const char *p;
7c86d365
JK
80 char *eol;
81
82 eol = memchr(line, '\n', size);
83 if (eol) {
84 *eol = '\0';
85 len = eol + 1 - line;
86 } else {
87 len = size;
88 }
d9c66f0b 89
44b67cb6 90 if (skip_prefix(line, "commit ", &p)) {
d9c66f0b
JS
91 if (util) {
92 string_list_append(list, buf.buf)->util = util;
93 strbuf_reset(&buf);
94 }
ca56dadb 95 CALLOC_ARRAY(util, 1);
d9c66f0b
JS
96 if (get_oid(p, &util->oid)) {
97 error(_("could not parse commit '%s'"), p);
98 free(util);
8c29b497 99 free(current_filename);
d9c66f0b
JS
100 string_list_clear(list, 1);
101 strbuf_release(&buf);
44b67cb6 102 strbuf_release(&contents);
d9c66f0b
JS
103 return -1;
104 }
105 util->matching = -1;
106 in_header = 1;
107 continue;
108 }
109
8cf51561
VD
110 if (!util) {
111 error(_("could not parse first line of `log` output: "
112 "did not start with 'commit ': '%s'"),
113 line);
8c29b497 114 free(current_filename);
8cf51561
VD
115 string_list_clear(list, 1);
116 strbuf_release(&buf);
117 strbuf_release(&contents);
8cf51561
VD
118 return -1;
119 }
120
44b67cb6 121 if (starts_with(line, "diff --git")) {
b66885a3
TG
122 struct patch patch = { 0 };
123 struct strbuf root = STRBUF_INIT;
124 int linenr = 0;
8d1675eb 125 int orig_len;
b66885a3 126
d9c66f0b
JS
127 in_header = 0;
128 strbuf_addch(&buf, '\n');
129 if (!util->diff_offset)
130 util->diff_offset = buf.len;
7c86d365
JK
131 if (eol)
132 *eol = '\n';
8d1675eb 133 orig_len = len;
937b76ed 134 len = parse_git_diff_header(&root, &linenr, 0, line,
b66885a3 135 len, size, &patch);
a2d474ad
JS
136 if (len < 0) {
137 error(_("could not parse git header '%.*s'"),
138 orig_len, line);
139 free(util);
140 free(current_filename);
141 string_list_clear(list, 1);
142 strbuf_release(&buf);
143 strbuf_release(&contents);
144 return -1;
145 }
b66885a3
TG
146 strbuf_addstr(&buf, " ## ");
147 if (patch.is_new > 0)
148 strbuf_addf(&buf, "%s (new)", patch.new_name);
149 else if (patch.is_delete > 0)
150 strbuf_addf(&buf, "%s (deleted)", patch.old_name);
151 else if (patch.is_rename)
152 strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
153 else
154 strbuf_addstr(&buf, patch.new_name);
155
444e0969
TG
156 free(current_filename);
157 if (patch.is_delete > 0)
158 current_filename = xstrdup(patch.old_name);
159 else
160 current_filename = xstrdup(patch.new_name);
161
b66885a3
TG
162 if (patch.new_mode && patch.old_mode &&
163 patch.old_mode != patch.new_mode)
164 strbuf_addf(&buf, " (mode change %06o => %06o)",
165 patch.old_mode, patch.new_mode);
166
167 strbuf_addstr(&buf, " ##");
d9c66f0b 168 } else if (in_header) {
44b67cb6 169 if (starts_with(line, "Author: ")) {
499352c2 170 strbuf_addstr(&buf, " ## Metadata ##\n");
44b67cb6 171 strbuf_addstr(&buf, line);
d9c66f0b 172 strbuf_addstr(&buf, "\n\n");
499352c2 173 strbuf_addstr(&buf, " ## Commit message ##\n");
9f726e1b
DL
174 } else if (starts_with(line, "Notes") &&
175 line[strlen(line) - 1] == ':') {
176 strbuf_addstr(&buf, "\n\n");
177 /* strip the trailing colon */
178 strbuf_addf(&buf, " ## %.*s ##\n",
179 (int)(strlen(line) - 1), line);
44b67cb6
TG
180 } else if (starts_with(line, " ")) {
181 p = line + len - 2;
182 while (isspace(*p) && p >= line)
183 p--;
184 strbuf_add(&buf, line, p - line + 1);
d9c66f0b
JS
185 strbuf_addch(&buf, '\n');
186 }
187 continue;
e1db2630
TG
188 } else if (skip_prefix(line, "@@ ", &p)) {
189 p = strstr(p, "@@");
444e0969
TG
190 strbuf_addstr(&buf, "@@");
191 if (current_filename && p[2])
192 strbuf_addf(&buf, " %s:", current_filename);
193 if (p)
194 strbuf_addstr(&buf, p + 2);
b66885a3 195 } else if (!line[0])
d9c66f0b
JS
196 /*
197 * A completely blank (not ' \n', which is context)
198 * line is not valid in a diff. We skip it
199 * silently, because this neatly handles the blank
200 * separator line between commits in git-log
201 * output.
d9c66f0b
JS
202 */
203 continue;
44b67cb6 204 else if (line[0] == '>') {
8d5ccb59 205 strbuf_addch(&buf, '+');
44b67cb6
TG
206 strbuf_addstr(&buf, line + 1);
207 } else if (line[0] == '<') {
8d5ccb59 208 strbuf_addch(&buf, '-');
44b67cb6
TG
209 strbuf_addstr(&buf, line + 1);
210 } else if (line[0] == '#') {
8d5ccb59 211 strbuf_addch(&buf, ' ');
44b67cb6 212 strbuf_addstr(&buf, line + 1);
8d5ccb59 213 } else {
2543a641 214 strbuf_addch(&buf, ' ');
44b67cb6 215 strbuf_addstr(&buf, line);
8d5ccb59 216 }
d9c66f0b
JS
217
218 strbuf_addch(&buf, '\n');
219 util->diffsize++;
220 }
44b67cb6 221 strbuf_release(&contents);
d9c66f0b
JS
222
223 if (util)
224 string_list_append(list, buf.buf)->util = util;
225 strbuf_release(&buf);
444e0969 226 free(current_filename);
d9c66f0b 227
d9c66f0b
JS
228 return 0;
229}
230
231static int patch_util_cmp(const void *dummy, const struct patch_util *a,
1ca69225 232 const struct patch_util *b, const char *keydata)
d9c66f0b
JS
233{
234 return strcmp(a->diff, keydata ? keydata : b->diff);
235}
236
237static void find_exact_matches(struct string_list *a, struct string_list *b)
238{
b19315d8 239 struct hashmap map = HASHMAP_INIT((hashmap_cmp_fn)patch_util_cmp, NULL);
d9c66f0b
JS
240 int i;
241
d9c66f0b
JS
242 /* First, add the patches of a to a hash map */
243 for (i = 0; i < a->nr; i++) {
244 struct patch_util *util = a->items[i].util;
245
246 util->i = i;
247 util->patch = a->items[i].string;
248 util->diff = util->patch + util->diff_offset;
d22245a2 249 hashmap_entry_init(&util->e, strhash(util->diff));
b94e5c1d 250 hashmap_add(&map, &util->e);
d9c66f0b
JS
251 }
252
253 /* Now try to find exact matches in b */
254 for (i = 0; i < b->nr; i++) {
255 struct patch_util *util = b->items[i].util, *other;
256
257 util->i = i;
258 util->patch = b->items[i].string;
259 util->diff = util->patch + util->diff_offset;
d22245a2 260 hashmap_entry_init(&util->e, strhash(util->diff));
404ab78e 261 other = hashmap_remove_entry(&map, util, e, NULL);
d9c66f0b
JS
262 if (other) {
263 if (other->matching >= 0)
264 BUG("already assigned!");
265
266 other->matching = i;
267 util->matching = other->i;
268 }
269 }
270
6da1a258 271 hashmap_clear(&map);
d9c66f0b
JS
272}
273
a8d5eb6d 274static int diffsize_consume(void *data, char *line, unsigned long len)
d9c66f0b
JS
275{
276 (*(int *)data)++;
a8d5eb6d 277 return 0;
d9c66f0b
JS
278}
279
d2eb8093
JK
280static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
281 const char *funcline, long funclen)
282{
283 diffsize_consume(data, NULL, 0);
284}
285
d9c66f0b
JS
286static int diffsize(const char *a, const char *b)
287{
288 xpparam_t pp = { 0 };
289 xdemitconf_t cfg = { 0 };
290 mmfile_t mf1, mf2;
291 int count = 0;
292
293 mf1.ptr = (char *)a;
294 mf1.size = strlen(a);
295 mf2.ptr = (char *)b;
296 mf2.size = strlen(b);
297
298 cfg.ctxlen = 3;
d2eb8093
JK
299 if (!xdi_diff_outf(&mf1, &mf2,
300 diffsize_hunk, diffsize_consume, &count,
301 &pp, &cfg))
d9c66f0b
JS
302 return count;
303
304 error(_("failed to generate diff"));
305 return COST_MAX;
306}
307
308static void get_correspondences(struct string_list *a, struct string_list *b,
309 int creation_factor)
310{
311 int n = a->nr + b->nr;
312 int *cost, c, *a2b, *b2a;
313 int i, j;
314
315 ALLOC_ARRAY(cost, st_mult(n, n));
316 ALLOC_ARRAY(a2b, n);
317 ALLOC_ARRAY(b2a, n);
318
319 for (i = 0; i < a->nr; i++) {
320 struct patch_util *a_util = a->items[i].util;
321
322 for (j = 0; j < b->nr; j++) {
323 struct patch_util *b_util = b->items[j].util;
324
325 if (a_util->matching == j)
326 c = 0;
327 else if (a_util->matching < 0 && b_util->matching < 0)
328 c = diffsize(a_util->diff, b_util->diff);
329 else
330 c = COST_MAX;
331 cost[i + n * j] = c;
332 }
333
334 c = a_util->matching < 0 ?
335 a_util->diffsize * creation_factor / 100 : COST_MAX;
336 for (j = b->nr; j < n; j++)
337 cost[i + n * j] = c;
338 }
339
340 for (j = 0; j < b->nr; j++) {
341 struct patch_util *util = b->items[j].util;
342
343 c = util->matching < 0 ?
344 util->diffsize * creation_factor / 100 : COST_MAX;
345 for (i = a->nr; i < n; i++)
346 cost[i + n * j] = c;
347 }
348
349 for (i = a->nr; i < n; i++)
350 for (j = b->nr; j < n; j++)
351 cost[i + n * j] = 0;
352
353 compute_assignment(n, n, cost, a2b, b2a);
354
355 for (i = 0; i < a->nr; i++)
356 if (a2b[i] >= 0 && a2b[i] < b->nr) {
357 struct patch_util *a_util = a->items[i].util;
358 struct patch_util *b_util = b->items[a2b[i]].util;
359
360 a_util->matching = a2b[i];
361 b_util->matching = i;
362 }
363
364 free(cost);
365 free(a2b);
366 free(b2a);
367}
368
faa1df86 369static void output_pair_header(struct diff_options *diffopt,
d1f87a2d 370 int patch_no_width,
faa1df86 371 struct strbuf *buf,
eb0be38c
JS
372 struct strbuf *dashes,
373 struct patch_util *a_util,
374 struct patch_util *b_util)
d9c66f0b 375{
eb0be38c
JS
376 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
377 struct commit *commit;
faa1df86
JS
378 char status;
379 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
380 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
381 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
382 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
383 const char *color;
eb0be38c
JS
384
385 if (!dashes->len)
386 strbuf_addchars(dashes, '-',
387 strlen(find_unique_abbrev(oid,
388 DEFAULT_ABBREV)));
389
faa1df86
JS
390 if (!b_util) {
391 color = color_old;
392 status = '<';
393 } else if (!a_util) {
394 color = color_new;
395 status = '>';
396 } else if (strcmp(a_util->patch, b_util->patch)) {
397 color = color_commit;
398 status = '!';
399 } else {
400 color = color_commit;
401 status = '=';
402 }
403
eb0be38c 404 strbuf_reset(buf);
faa1df86 405 strbuf_addstr(buf, status == '!' ? color_old : color);
eb0be38c 406 if (!a_util)
d1f87a2d 407 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
eb0be38c 408 else
d1f87a2d 409 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
eb0be38c
JS
410 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
411
faa1df86
JS
412 if (status == '!')
413 strbuf_addf(buf, "%s%s", color_reset, color);
414 strbuf_addch(buf, status);
415 if (status == '!')
416 strbuf_addf(buf, "%s%s", color_reset, color_new);
eb0be38c
JS
417
418 if (!b_util)
d1f87a2d 419 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
eb0be38c 420 else
d1f87a2d 421 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
eb0be38c
JS
422 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
423
424 commit = lookup_commit_reference(the_repository, oid);
425 if (commit) {
faa1df86
JS
426 if (status == '!')
427 strbuf_addf(buf, "%s%s", color_reset, color);
428
eb0be38c
JS
429 strbuf_addch(buf, ' ');
430 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
431 }
faa1df86 432 strbuf_addf(buf, "%s\n", color_reset);
eb0be38c 433
87f1b2d4 434 fwrite(buf->buf, buf->len, 1, diffopt->file);
d9c66f0b
JS
435}
436
499352c2
TG
437static struct userdiff_driver section_headers = {
438 .funcname = { "^ ## (.*) ##$\n"
439 "^.?@@ (.*)$", REG_EXTENDED }
4eba1fe6
JS
440};
441
c8c5e43a
JS
442static struct diff_filespec *get_filespec(const char *name, const char *p)
443{
444 struct diff_filespec *spec = alloc_filespec(name);
445
14228447 446 fill_filespec(spec, null_oid(), 0, 0100644);
c8c5e43a
JS
447 spec->data = (char *)p;
448 spec->size = strlen(p);
449 spec->should_munmap = 0;
450 spec->is_stdin = 1;
499352c2 451 spec->driver = &section_headers;
c8c5e43a
JS
452
453 return spec;
454}
455
456static void patch_diff(const char *a, const char *b,
1ca69225 457 struct diff_options *diffopt)
c8c5e43a
JS
458{
459 diff_queue(&diff_queued_diff,
460 get_filespec("a", a), get_filespec("b", b));
461
462 diffcore_std(diffopt);
463 diff_flush(diffopt);
464}
465
3e6046ed
JS
466static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
467{
468 return data;
469}
470
c8c5e43a 471static void output(struct string_list *a, struct string_list *b,
3e6046ed 472 struct range_diff_options *range_diff_opts)
d9c66f0b 473{
eb0be38c 474 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
d1f87a2d 475 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
9dc46e02 476 int i = 0, j = 0;
3e6046ed
JS
477 struct diff_options opts;
478 struct strbuf indent = STRBUF_INIT;
479
480 if (range_diff_opts->diffopt)
481 memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
482 else
483 diff_setup(&opts);
484
709b3f32 485 opts.no_free = 1;
3e6046ed
JS
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) {
1e79f973
JS
516 if (!range_diff_opts->right_only)
517 output_pair_header(&opts, patch_no_width,
faa1df86 518 &buf, &dashes, a_util, NULL);
9dc46e02
JS
519 i++;
520 continue;
521 }
d9c66f0b 522
9dc46e02
JS
523 /* Show unmatched RHS commits. */
524 while (j < b->nr && b_util->matching < 0) {
1e79f973
JS
525 if (!range_diff_opts->left_only)
526 output_pair_header(&opts, patch_no_width,
faa1df86 527 &buf, &dashes, NULL, b_util);
9dc46e02 528 b_util = ++j < b->nr ? b->items[j].util : NULL;
d9c66f0b 529 }
d9c66f0b 530
9dc46e02
JS
531 /* Show matching LHS/RHS pair. */
532 if (j < b->nr) {
533 a_util = a->items[b_util->matching].util;
3e6046ed 534 output_pair_header(&opts, patch_no_width,
faa1df86 535 &buf, &dashes, a_util, b_util);
3e6046ed 536 if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
c8c5e43a 537 patch_diff(a->items[b_util->matching].string,
3e6046ed 538 b->items[j].string, &opts);
9dc46e02
JS
539 a_util->shown = 1;
540 j++;
541 }
d9c66f0b 542 }
eb0be38c
JS
543 strbuf_release(&buf);
544 strbuf_release(&dashes);
3e6046ed 545 strbuf_release(&indent);
709b3f32
RS
546 opts.no_free = 0;
547 diff_free(&opts);
73a834e9
ES
548}
549
d9c66f0b 550int show_range_diff(const char *range1, const char *range2,
f1ce6c19 551 struct range_diff_options *range_diff_opts)
d9c66f0b
JS
552{
553 int res = 0;
554
555 struct string_list branch1 = STRING_LIST_INIT_DUP;
556 struct string_list branch2 = STRING_LIST_INIT_DUP;
557
1e79f973 558 if (range_diff_opts->left_only && range_diff_opts->right_only)
43ea635c 559 res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
1e79f973
JS
560
561 if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
d9c66f0b 562 res = error(_("could not parse log for '%s'"), range1);
f1ce6c19 563 if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
d9c66f0b
JS
564 res = error(_("could not parse log for '%s'"), range2);
565
566 if (!res) {
567 find_exact_matches(&branch1, &branch2);
f1ce6c19
JS
568 get_correspondences(&branch1, &branch2,
569 range_diff_opts->creation_factor);
3e6046ed 570 output(&branch1, &branch2, range_diff_opts);
d9c66f0b
JS
571 }
572
573 string_list_clear(&branch1, 1);
574 string_list_clear(&branch2, 1);
575
576 return res;
577}
679b5916
JS
578
579int is_range_diff_range(const char *arg)
580{
359f0d75
JS
581 char *copy = xstrdup(arg); /* setup_revisions() modifies it */
582 const char *argv[] = { "", copy, "--", NULL };
583 int i, positive = 0, negative = 0;
584 struct rev_info revs;
585
586 init_revisions(&revs, NULL);
587 if (setup_revisions(3, argv, &revs, NULL) == 1) {
588 for (i = 0; i < revs.pending.nr; i++)
589 if (revs.pending.objects[i].item->flags & UNINTERESTING)
590 negative++;
591 else
592 positive++;
593 for (i = 0; i < revs.pending.nr; i++) {
594 struct object *obj = revs.pending.objects[i].item;
595
596 if (obj->type == OBJ_COMMIT)
597 clear_commit_marks((struct commit *)obj,
598 ALL_REV_FLAGS);
599 }
600 }
601
602 free(copy);
603 object_array_clear(&revs.pending);
604 return negative > 0 && positive > 0;
679b5916 605}