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