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