git
/
range-diff.c
622 строки · 16.1 Кб
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "environment.h"
5#include "gettext.h"
6#include "range-diff.h"
7#include "object-name.h"
8#include "string-list.h"
9#include "run-command.h"
10#include "strvec.h"
11#include "hashmap.h"
12#include "xdiff-interface.h"
13#include "linear-assignment.h"
14#include "diffcore.h"
15#include "commit.h"
16#include "pager.h"
17#include "pretty.h"
18#include "repository.h"
19#include "userdiff.h"
20#include "apply.h"
21#include "revision.h"
22
23struct patch_util {
24/* For the search for an exact match */
25struct hashmap_entry e;
26const char *diff, *patch;
27
28int i, shown;
29int diffsize;
30size_t diff_offset;
31/* the index of the matching item in the other branch, or -1 */
32int matching;
33struct object_id oid;
34};
35
36/*
37* Reads the patches into a string list, with the `util` field being populated
38* as struct object_id (will need to be free()d).
39*/
40static int read_patches(const char *range, struct string_list *list,
41const struct strvec *other_arg)
42{
43struct child_process cp = CHILD_PROCESS_INIT;
44struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
45struct patch_util *util = NULL;
46int in_header = 1;
47char *line, *current_filename = NULL;
48ssize_t len;
49size_t size;
50int ret = -1;
51
52strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
53"--reverse", "--date-order", "--decorate=no",
54"--no-prefix", "--submodule=short",
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=#",
63"--no-abbrev-commit",
64"--pretty=medium",
65"--show-notes-by-default",
66NULL);
67strvec_push(&cp.args, range);
68if (other_arg)
69strvec_pushv(&cp.args, other_arg->v);
70cp.out = -1;
71cp.no_stdin = 1;
72cp.git_cmd = 1;
73
74if (start_command(&cp))
75return error_errno(_("could not start `log`"));
76if (strbuf_read(&contents, cp.out, 0) < 0) {
77error_errno(_("could not read `log` output"));
78finish_command(&cp);
79goto cleanup;
80}
81if (finish_command(&cp))
82goto cleanup;
83
84line = contents.buf;
85size = contents.len;
86for (; size > 0; size -= len, line += len) {
87const char *p;
88char *eol;
89
90eol = memchr(line, '\n', size);
91if (eol) {
92*eol = '\0';
93len = eol + 1 - line;
94} else {
95len = size;
96}
97
98if (skip_prefix(line, "commit ", &p)) {
99if (util) {
100string_list_append(list, buf.buf)->util = util;
101strbuf_reset(&buf);
102}
103CALLOC_ARRAY(util, 1);
104if (repo_get_oid(the_repository, p, &util->oid)) {
105error(_("could not parse commit '%s'"), p);
106FREE_AND_NULL(util);
107string_list_clear(list, 1);
108goto cleanup;
109}
110util->matching = -1;
111in_header = 1;
112continue;
113}
114
115if (!util) {
116error(_("could not parse first line of `log` output: "
117"did not start with 'commit ': '%s'"),
118line);
119string_list_clear(list, 1);
120goto cleanup;
121}
122
123if (starts_with(line, "diff --git")) {
124struct patch patch = { 0 };
125struct strbuf root = STRBUF_INIT;
126int linenr = 0;
127int orig_len;
128
129in_header = 0;
130strbuf_addch(&buf, '\n');
131if (!util->diff_offset)
132util->diff_offset = buf.len;
133if (eol)
134*eol = '\n';
135orig_len = len;
136len = parse_git_diff_header(&root, &linenr, 0, line,
137len, size, &patch);
138if (len < 0) {
139error(_("could not parse git header '%.*s'"),
140orig_len, line);
141FREE_AND_NULL(util);
142string_list_clear(list, 1);
143goto cleanup;
144}
145strbuf_addstr(&buf, " ## ");
146if (patch.is_new > 0)
147strbuf_addf(&buf, "%s (new)", patch.new_name);
148else if (patch.is_delete > 0)
149strbuf_addf(&buf, "%s (deleted)", patch.old_name);
150else if (patch.is_rename)
151strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
152else
153strbuf_addstr(&buf, patch.new_name);
154
155free(current_filename);
156if (patch.is_delete > 0)
157current_filename = xstrdup(patch.old_name);
158else
159current_filename = xstrdup(patch.new_name);
160
161if (patch.new_mode && patch.old_mode &&
162patch.old_mode != patch.new_mode)
163strbuf_addf(&buf, " (mode change %06o => %06o)",
164patch.old_mode, patch.new_mode);
165
166strbuf_addstr(&buf, " ##");
167release_patch(&patch);
168} else if (in_header) {
169if (starts_with(line, "Author: ")) {
170strbuf_addstr(&buf, " ## Metadata ##\n");
171strbuf_addstr(&buf, line);
172strbuf_addstr(&buf, "\n\n");
173strbuf_addstr(&buf, " ## Commit message ##\n");
174} else if (starts_with(line, "Notes") &&
175line[strlen(line) - 1] == ':') {
176strbuf_addstr(&buf, "\n\n");
177/* strip the trailing colon */
178strbuf_addf(&buf, " ## %.*s ##\n",
179(int)(strlen(line) - 1), line);
180} else if (starts_with(line, " ")) {
181p = line + len - 2;
182while (isspace(*p) && p >= line)
183p--;
184strbuf_add(&buf, line, p - line + 1);
185strbuf_addch(&buf, '\n');
186}
187continue;
188} else if (skip_prefix(line, "@@ ", &p)) {
189p = strstr(p, "@@");
190strbuf_addstr(&buf, "@@");
191if (current_filename && p[2])
192strbuf_addf(&buf, " %s:", current_filename);
193if (p)
194strbuf_addstr(&buf, p + 2);
195} else if (!line[0])
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.
202*/
203continue;
204else if (line[0] == '>') {
205strbuf_addch(&buf, '+');
206strbuf_addstr(&buf, line + 1);
207} else if (line[0] == '<') {
208strbuf_addch(&buf, '-');
209strbuf_addstr(&buf, line + 1);
210} else if (line[0] == '#') {
211strbuf_addch(&buf, ' ');
212strbuf_addstr(&buf, line + 1);
213} else {
214strbuf_addch(&buf, ' ');
215strbuf_addstr(&buf, line);
216}
217
218strbuf_addch(&buf, '\n');
219util->diffsize++;
220}
221
222ret = 0;
223cleanup:
224strbuf_release(&contents);
225
226if (util)
227string_list_append(list, buf.buf)->util = util;
228strbuf_release(&buf);
229free(current_filename);
230
231return ret;
232}
233
234static int patch_util_cmp(const void *cmp_data UNUSED,
235const struct hashmap_entry *ha,
236const struct hashmap_entry *hb,
237const void *keydata)
238{
239const struct patch_util
240*a = container_of(ha, const struct patch_util, e),
241*b = container_of(hb, const struct patch_util, e);
242return strcmp(a->diff, keydata ? keydata : b->diff);
243}
244
245static void find_exact_matches(struct string_list *a, struct string_list *b)
246{
247struct hashmap map = HASHMAP_INIT(patch_util_cmp, NULL);
248int i;
249
250/* First, add the patches of a to a hash map */
251for (i = 0; i < a->nr; i++) {
252struct patch_util *util = a->items[i].util;
253
254util->i = i;
255util->patch = a->items[i].string;
256util->diff = util->patch + util->diff_offset;
257hashmap_entry_init(&util->e, strhash(util->diff));
258hashmap_add(&map, &util->e);
259}
260
261/* Now try to find exact matches in b */
262for (i = 0; i < b->nr; i++) {
263struct patch_util *util = b->items[i].util, *other;
264
265util->i = i;
266util->patch = b->items[i].string;
267util->diff = util->patch + util->diff_offset;
268hashmap_entry_init(&util->e, strhash(util->diff));
269other = hashmap_remove_entry(&map, util, e, NULL);
270if (other) {
271if (other->matching >= 0)
272BUG("already assigned!");
273
274other->matching = i;
275util->matching = other->i;
276}
277}
278
279hashmap_clear(&map);
280}
281
282static int diffsize_consume(void *data,
283char *line UNUSED,
284unsigned long len UNUSED)
285{
286(*(int *)data)++;
287return 0;
288}
289
290static void diffsize_hunk(void *data,
291long ob UNUSED, long on UNUSED,
292long nb UNUSED, long nn UNUSED,
293const char *func UNUSED, long funclen UNUSED)
294{
295diffsize_consume(data, NULL, 0);
296}
297
298static int diffsize(const char *a, const char *b)
299{
300xpparam_t pp = { 0 };
301xdemitconf_t cfg = { 0 };
302mmfile_t mf1, mf2;
303int count = 0;
304
305mf1.ptr = (char *)a;
306mf1.size = strlen(a);
307mf2.ptr = (char *)b;
308mf2.size = strlen(b);
309
310cfg.ctxlen = 3;
311if (!xdi_diff_outf(&mf1, &mf2,
312diffsize_hunk, diffsize_consume, &count,
313&pp, &cfg))
314return count;
315
316error(_("failed to generate diff"));
317return COST_MAX;
318}
319
320static void get_correspondences(struct string_list *a, struct string_list *b,
321int creation_factor)
322{
323int n = a->nr + b->nr;
324int *cost, c, *a2b, *b2a;
325int i, j;
326
327ALLOC_ARRAY(cost, st_mult(n, n));
328ALLOC_ARRAY(a2b, n);
329ALLOC_ARRAY(b2a, n);
330
331for (i = 0; i < a->nr; i++) {
332struct patch_util *a_util = a->items[i].util;
333
334for (j = 0; j < b->nr; j++) {
335struct patch_util *b_util = b->items[j].util;
336
337if (a_util->matching == j)
338c = 0;
339else if (a_util->matching < 0 && b_util->matching < 0)
340c = diffsize(a_util->diff, b_util->diff);
341else
342c = COST_MAX;
343cost[i + n * j] = c;
344}
345
346c = a_util->matching < 0 ?
347a_util->diffsize * creation_factor / 100 : COST_MAX;
348for (j = b->nr; j < n; j++)
349cost[i + n * j] = c;
350}
351
352for (j = 0; j < b->nr; j++) {
353struct patch_util *util = b->items[j].util;
354
355c = util->matching < 0 ?
356util->diffsize * creation_factor / 100 : COST_MAX;
357for (i = a->nr; i < n; i++)
358cost[i + n * j] = c;
359}
360
361for (i = a->nr; i < n; i++)
362for (j = b->nr; j < n; j++)
363cost[i + n * j] = 0;
364
365compute_assignment(n, n, cost, a2b, b2a);
366
367for (i = 0; i < a->nr; i++)
368if (a2b[i] >= 0 && a2b[i] < b->nr) {
369struct patch_util *a_util = a->items[i].util;
370struct patch_util *b_util = b->items[a2b[i]].util;
371
372a_util->matching = a2b[i];
373b_util->matching = i;
374}
375
376free(cost);
377free(a2b);
378free(b2a);
379}
380
381static void output_pair_header(struct diff_options *diffopt,
382int patch_no_width,
383struct strbuf *buf,
384struct strbuf *dashes,
385struct patch_util *a_util,
386struct patch_util *b_util)
387{
388struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
389struct commit *commit;
390char status;
391const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
392const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
393const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
394const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
395const char *color;
396int abbrev = diffopt->abbrev;
397
398if (abbrev < 0)
399abbrev = DEFAULT_ABBREV;
400
401if (!dashes->len)
402strbuf_addchars(dashes, '-',
403strlen(repo_find_unique_abbrev(the_repository, oid, abbrev)));
404
405if (!b_util) {
406color = color_old;
407status = '<';
408} else if (!a_util) {
409color = color_new;
410status = '>';
411} else if (strcmp(a_util->patch, b_util->patch)) {
412color = color_commit;
413status = '!';
414} else {
415color = color_commit;
416status = '=';
417}
418
419strbuf_reset(buf);
420strbuf_addstr(buf, status == '!' ? color_old : color);
421if (!a_util)
422strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
423else
424strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
425repo_find_unique_abbrev(the_repository, &a_util->oid, abbrev));
426
427if (status == '!')
428strbuf_addf(buf, "%s%s", color_reset, color);
429strbuf_addch(buf, status);
430if (status == '!')
431strbuf_addf(buf, "%s%s", color_reset, color_new);
432
433if (!b_util)
434strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
435else
436strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
437repo_find_unique_abbrev(the_repository, &b_util->oid, abbrev));
438
439commit = lookup_commit_reference(the_repository, oid);
440if (commit) {
441if (status == '!')
442strbuf_addf(buf, "%s%s", color_reset, color);
443
444strbuf_addch(buf, ' ');
445pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
446}
447strbuf_addf(buf, "%s\n", color_reset);
448
449fwrite(buf->buf, buf->len, 1, diffopt->file);
450}
451
452static struct userdiff_driver section_headers = {
453.funcname = {
454.pattern = "^ ## (.*) ##$\n^.?@@ (.*)$",
455.cflags = REG_EXTENDED,
456},
457};
458
459static struct diff_filespec *get_filespec(const char *name, const char *p)
460{
461struct diff_filespec *spec = alloc_filespec(name);
462
463fill_filespec(spec, null_oid(), 0, 0100644);
464spec->data = (char *)p;
465spec->size = strlen(p);
466spec->should_munmap = 0;
467spec->is_stdin = 1;
468spec->driver = §ion_headers;
469
470return spec;
471}
472
473static void patch_diff(const char *a, const char *b,
474struct diff_options *diffopt)
475{
476diff_queue(&diff_queued_diff,
477get_filespec("a", a), get_filespec("b", b));
478
479diffcore_std(diffopt);
480diff_flush(diffopt);
481}
482
483static struct strbuf *output_prefix_cb(struct diff_options *opt UNUSED, void *data)
484{
485return data;
486}
487
488static void output(struct string_list *a, struct string_list *b,
489struct range_diff_options *range_diff_opts)
490{
491struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
492int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
493int i = 0, j = 0;
494struct diff_options opts;
495struct strbuf indent = STRBUF_INIT;
496
497if (range_diff_opts->diffopt)
498memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
499else
500repo_diff_setup(the_repository, &opts);
501
502opts.no_free = 1;
503if (!opts.output_format)
504opts.output_format = DIFF_FORMAT_PATCH;
505opts.flags.suppress_diff_headers = 1;
506opts.flags.dual_color_diffed_diffs =
507range_diff_opts->dual_color;
508opts.flags.suppress_hunk_header_line_count = 1;
509opts.output_prefix = output_prefix_cb;
510strbuf_addstr(&indent, " ");
511opts.output_prefix_data = &indent;
512diff_setup_done(&opts);
513
514/*
515* We assume the user is really more interested in the second argument
516* ("newer" version). To that end, we print the output in the order of
517* the RHS (the `b` parameter). To put the LHS (the `a` parameter)
518* commits that are no longer in the RHS into a good place, we place
519* them once we have shown all of their predecessors in the LHS.
520*/
521
522while (i < a->nr || j < b->nr) {
523struct patch_util *a_util, *b_util;
524a_util = i < a->nr ? a->items[i].util : NULL;
525b_util = j < b->nr ? b->items[j].util : NULL;
526
527/* Skip all the already-shown commits from the LHS. */
528while (i < a->nr && a_util->shown)
529a_util = ++i < a->nr ? a->items[i].util : NULL;
530
531/* Show unmatched LHS commit whose predecessors were shown. */
532if (i < a->nr && a_util->matching < 0) {
533if (!range_diff_opts->right_only)
534output_pair_header(&opts, patch_no_width,
535&buf, &dashes, a_util, NULL);
536i++;
537continue;
538}
539
540/* Show unmatched RHS commits. */
541while (j < b->nr && b_util->matching < 0) {
542if (!range_diff_opts->left_only)
543output_pair_header(&opts, patch_no_width,
544&buf, &dashes, NULL, b_util);
545b_util = ++j < b->nr ? b->items[j].util : NULL;
546}
547
548/* Show matching LHS/RHS pair. */
549if (j < b->nr) {
550a_util = a->items[b_util->matching].util;
551output_pair_header(&opts, patch_no_width,
552&buf, &dashes, a_util, b_util);
553if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
554patch_diff(a->items[b_util->matching].string,
555b->items[j].string, &opts);
556a_util->shown = 1;
557j++;
558}
559}
560strbuf_release(&buf);
561strbuf_release(&dashes);
562strbuf_release(&indent);
563opts.no_free = 0;
564diff_free(&opts);
565}
566
567int show_range_diff(const char *range1, const char *range2,
568struct range_diff_options *range_diff_opts)
569{
570int res = 0;
571
572struct string_list branch1 = STRING_LIST_INIT_DUP;
573struct string_list branch2 = STRING_LIST_INIT_DUP;
574
575if (range_diff_opts->left_only && range_diff_opts->right_only)
576res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
577
578if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
579res = error(_("could not parse log for '%s'"), range1);
580if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
581res = error(_("could not parse log for '%s'"), range2);
582
583if (!res) {
584find_exact_matches(&branch1, &branch2);
585get_correspondences(&branch1, &branch2,
586range_diff_opts->creation_factor);
587output(&branch1, &branch2, range_diff_opts);
588}
589
590string_list_clear(&branch1, 1);
591string_list_clear(&branch2, 1);
592
593return res;
594}
595
596int is_range_diff_range(const char *arg)
597{
598char *copy = xstrdup(arg); /* setup_revisions() modifies it */
599const char *argv[] = { "", copy, "--", NULL };
600int i, positive = 0, negative = 0;
601struct rev_info revs;
602
603repo_init_revisions(the_repository, &revs, NULL);
604if (setup_revisions(3, argv, &revs, NULL) == 1) {
605for (i = 0; i < revs.pending.nr; i++)
606if (revs.pending.objects[i].item->flags & UNINTERESTING)
607negative++;
608else
609positive++;
610for (i = 0; i < revs.pending.nr; i++) {
611struct object *obj = revs.pending.objects[i].item;
612
613if (obj->type == OBJ_COMMIT)
614clear_commit_marks((struct commit *)obj,
615ALL_REV_FLAGS);
616}
617}
618
619free(copy);
620release_revisions(&revs);
621return negative > 0 && positive > 0;
622}
623