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