range-diff: don't remove funcname from inner diff
[git] / range-diff.c
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"
9 #include "diffcore.h"
10 #include "commit.h"
11 #include "pretty.h"
12 #include "userdiff.h"
13
14 struct patch_util {
15         /* For the search for an exact match */
16         struct hashmap_entry e;
17         const char *diff, *patch;
18
19         int i, shown;
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
27 static 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
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  */
42 static int read_patches(const char *range, struct string_list *list)
43 {
44         struct child_process cp = CHILD_PROCESS_INIT;
45         struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
46         struct patch_util *util = NULL;
47         int in_header = 1;
48         char *line;
49         int offset, len;
50         size_t size;
51
52         argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
53                         "--reverse", "--date-order", "--decorate=no",
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=#",
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`"));
70         if (strbuf_read(&contents, cp.out, 0) < 0) {
71                 error_errno(_("could not read `log` output"));
72                 finish_command(&cp);
73                 return -1;
74         }
75
76         line = contents.buf;
77         size = contents.len;
78         for (offset = 0; size > 0; offset += len, size -= len, line += len) {
79                 const char *p;
80
81                 len = find_end_of_line(line, size);
82                 line[len - 1] = '\0';
83                 if (skip_prefix(line, "commit ", &p)) {
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);
94                                 strbuf_release(&contents);
95                                 finish_command(&cp);
96                                 return -1;
97                         }
98                         util->matching = -1;
99                         in_header = 1;
100                         continue;
101                 }
102
103                 if (starts_with(line, "diff --git")) {
104                         in_header = 0;
105                         strbuf_addch(&buf, '\n');
106                         if (!util->diff_offset)
107                                 util->diff_offset = buf.len;
108                         strbuf_addch(&buf, ' ');
109                         strbuf_addstr(&buf, line);
110                 } else if (in_header) {
111                         if (starts_with(line, "Author: ")) {
112                                 strbuf_addstr(&buf, line);
113                                 strbuf_addstr(&buf, "\n\n");
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);
119                                 strbuf_addch(&buf, '\n');
120                         }
121                         continue;
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 "))
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;
138                 else if (line[0] == '>') {
139                         strbuf_addch(&buf, '+');
140                         strbuf_addstr(&buf, line + 1);
141                 } else if (line[0] == '<') {
142                         strbuf_addch(&buf, '-');
143                         strbuf_addstr(&buf, line + 1);
144                 } else if (line[0] == '#') {
145                         strbuf_addch(&buf, ' ');
146                         strbuf_addstr(&buf, line + 1);
147                 } else {
148                         strbuf_addch(&buf, ' ');
149                         strbuf_addstr(&buf, line);
150                 }
151
152                 strbuf_addch(&buf, '\n');
153                 util->diffsize++;
154         }
155         strbuf_release(&contents);
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
167 static int patch_util_cmp(const void *dummy, const struct patch_util *a,
168                           const struct patch_util *b, const char *keydata)
169 {
170         return strcmp(a->diff, keydata ? keydata : b->diff);
171 }
172
173 static 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
212 static void diffsize_consume(void *data, char *line, unsigned long len)
213 {
214         (*(int *)data)++;
215 }
216
217 static 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
223 static 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;
236         if (!xdi_diff_outf(&mf1, &mf2,
237                            diffsize_hunk, diffsize_consume, &count,
238                            &pp, &cfg))
239                 return count;
240
241         error(_("failed to generate diff"));
242         return COST_MAX;
243 }
244
245 static 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
306 static void output_pair_header(struct diff_options *diffopt,
307                                int patch_no_width,
308                                struct strbuf *buf,
309                                struct strbuf *dashes,
310                                struct patch_util *a_util,
311                                struct patch_util *b_util)
312 {
313         struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
314         struct commit *commit;
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;
321
322         if (!dashes->len)
323                 strbuf_addchars(dashes, '-',
324                                 strlen(find_unique_abbrev(oid,
325                                                           DEFAULT_ABBREV)));
326
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
341         strbuf_reset(buf);
342         strbuf_addstr(buf, status == '!' ? color_old : color);
343         if (!a_util)
344                 strbuf_addf(buf, "%*s:  %s ", patch_no_width, "-", dashes->buf);
345         else
346                 strbuf_addf(buf, "%*d:  %s ", patch_no_width, a_util->i + 1,
347                             find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
348
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);
354
355         if (!b_util)
356                 strbuf_addf(buf, " %*s:  %s", patch_no_width, "-", dashes->buf);
357         else
358                 strbuf_addf(buf, " %*d:  %s", patch_no_width, b_util->i + 1,
359                             find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
360
361         commit = lookup_commit_reference(the_repository, oid);
362         if (commit) {
363                 if (status == '!')
364                         strbuf_addf(buf, "%s%s", color_reset, color);
365
366                 strbuf_addch(buf, ' ');
367                 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
368         }
369         strbuf_addf(buf, "%s\n", color_reset);
370
371         fwrite(buf->buf, buf->len, 1, diffopt->file);
372 }
373
374 static struct userdiff_driver no_func_name = {
375         .funcname = { "$^", 0 }
376 };
377
378 static struct diff_filespec *get_filespec(const char *name, const char *p)
379 {
380         struct diff_filespec *spec = alloc_filespec(name);
381
382         fill_filespec(spec, &null_oid, 0, 0100644);
383         spec->data = (char *)p;
384         spec->size = strlen(p);
385         spec->should_munmap = 0;
386         spec->is_stdin = 1;
387         spec->driver = &no_func_name;
388
389         return spec;
390 }
391
392 static void patch_diff(const char *a, const char *b,
393                        struct diff_options *diffopt)
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
402 static void output(struct string_list *a, struct string_list *b,
403                    struct diff_options *diffopt)
404 {
405         struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
406         int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
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) {
428                         output_pair_header(diffopt, patch_no_width,
429                                            &buf, &dashes, a_util, NULL);
430                         i++;
431                         continue;
432                 }
433
434                 /* Show unmatched RHS commits. */
435                 while (j < b->nr && b_util->matching < 0) {
436                         output_pair_header(diffopt, patch_no_width,
437                                            &buf, &dashes, NULL, b_util);
438                         b_util = ++j < b->nr ? b->items[j].util : NULL;
439                 }
440
441                 /* Show matching LHS/RHS pair. */
442                 if (j < b->nr) {
443                         a_util = a->items[b_util->matching].util;
444                         output_pair_header(diffopt, patch_no_width,
445                                            &buf, &dashes, a_util, b_util);
446                         if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
447                                 patch_diff(a->items[b_util->matching].string,
448                                            b->items[j].string, diffopt);
449                         a_util->shown = 1;
450                         j++;
451                 }
452         }
453         strbuf_release(&buf);
454         strbuf_release(&dashes);
455 }
456
457 static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
458 {
459         return data;
460 }
461
462 int show_range_diff(const char *range1, const char *range2,
463                     int creation_factor, int dual_color,
464                     struct diff_options *diffopt)
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) {
477                 struct diff_options opts;
478                 struct strbuf indent = STRBUF_INIT;
479
480                 if (diffopt)
481                         memcpy(&opts, diffopt, sizeof(opts));
482                 else
483                         diff_setup(&opts);
484
485                 if (!opts.output_format)
486                         opts.output_format = DIFF_FORMAT_PATCH;
487                 opts.flags.suppress_diff_headers = 1;
488                 opts.flags.dual_color_diffed_diffs = dual_color;
489                 opts.output_prefix = output_prefix_cb;
490                 strbuf_addstr(&indent, "    ");
491                 opts.output_prefix_data = &indent;
492                 diff_setup_done(&opts);
493
494                 find_exact_matches(&branch1, &branch2);
495                 get_correspondences(&branch1, &branch2, creation_factor);
496                 output(&branch1, &branch2, &opts);
497
498                 strbuf_release(&indent);
499         }
500
501         string_list_clear(&branch1, 1);
502         string_list_clear(&branch2, 1);
503
504         return res;
505 }