color: add the meta color GIT_COLOR_REVERSE
[git.git] / range-diff.c
blobb1663da7cd30688f8fd4d7efa41efb16eb130e96
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"
14 struct patch_util {
15 /* For the search for an exact match */
16 struct hashmap_entry e;
17 const char *diff, *patch;
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;
28 * Reads the patches into a string list, with the `util` field being populated
29 * as struct object_id (will need to be free()d).
31 static int read_patches(const char *range, struct string_list *list)
33 struct child_process cp = CHILD_PROCESS_INIT;
34 FILE *in;
35 struct strbuf buf = STRBUF_INIT, line = STRBUF_INIT;
36 struct patch_util *util = NULL;
37 int in_header = 1;
39 argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
40 "--reverse", "--date-order", "--decorate=no",
41 "--no-abbrev-commit", range,
42 NULL);
43 cp.out = -1;
44 cp.no_stdin = 1;
45 cp.git_cmd = 1;
47 if (start_command(&cp))
48 return error_errno(_("could not start `log`"));
49 in = fdopen(cp.out, "r");
50 if (!in) {
51 error_errno(_("could not read `log` output"));
52 finish_command(&cp);
53 return -1;
56 while (strbuf_getline(&line, in) != EOF) {
57 const char *p;
59 if (skip_prefix(line.buf, "commit ", &p)) {
60 if (util) {
61 string_list_append(list, buf.buf)->util = util;
62 strbuf_reset(&buf);
64 util = xcalloc(sizeof(*util), 1);
65 if (get_oid(p, &util->oid)) {
66 error(_("could not parse commit '%s'"), p);
67 free(util);
68 string_list_clear(list, 1);
69 strbuf_release(&buf);
70 strbuf_release(&line);
71 fclose(in);
72 finish_command(&cp);
73 return -1;
75 util->matching = -1;
76 in_header = 1;
77 continue;
80 if (starts_with(line.buf, "diff --git")) {
81 in_header = 0;
82 strbuf_addch(&buf, '\n');
83 if (!util->diff_offset)
84 util->diff_offset = buf.len;
85 strbuf_addbuf(&buf, &line);
86 } else if (in_header) {
87 if (starts_with(line.buf, "Author: ")) {
88 strbuf_addbuf(&buf, &line);
89 strbuf_addstr(&buf, "\n\n");
90 } else if (starts_with(line.buf, " ")) {
91 strbuf_rtrim(&line);
92 strbuf_addbuf(&buf, &line);
93 strbuf_addch(&buf, '\n');
95 continue;
96 } else if (starts_with(line.buf, "@@ "))
97 strbuf_addstr(&buf, "@@");
98 else if (!line.buf[0] || starts_with(line.buf, "index "))
100 * A completely blank (not ' \n', which is context)
101 * line is not valid in a diff. We skip it
102 * silently, because this neatly handles the blank
103 * separator line between commits in git-log
104 * output.
106 * We also want to ignore the diff's `index` lines
107 * because they contain exact blob hashes in which
108 * we are not interested.
110 continue;
111 else
112 strbuf_addbuf(&buf, &line);
114 strbuf_addch(&buf, '\n');
115 util->diffsize++;
117 fclose(in);
118 strbuf_release(&line);
120 if (util)
121 string_list_append(list, buf.buf)->util = util;
122 strbuf_release(&buf);
124 if (finish_command(&cp))
125 return -1;
127 return 0;
130 static int patch_util_cmp(const void *dummy, const struct patch_util *a,
131 const struct patch_util *b, const char *keydata)
133 return strcmp(a->diff, keydata ? keydata : b->diff);
136 static void find_exact_matches(struct string_list *a, struct string_list *b)
138 struct hashmap map;
139 int i;
141 hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
143 /* First, add the patches of a to a hash map */
144 for (i = 0; i < a->nr; i++) {
145 struct patch_util *util = a->items[i].util;
147 util->i = i;
148 util->patch = a->items[i].string;
149 util->diff = util->patch + util->diff_offset;
150 hashmap_entry_init(util, strhash(util->diff));
151 hashmap_add(&map, util);
154 /* Now try to find exact matches in b */
155 for (i = 0; i < b->nr; i++) {
156 struct patch_util *util = b->items[i].util, *other;
158 util->i = i;
159 util->patch = b->items[i].string;
160 util->diff = util->patch + util->diff_offset;
161 hashmap_entry_init(util, strhash(util->diff));
162 other = hashmap_remove(&map, util, NULL);
163 if (other) {
164 if (other->matching >= 0)
165 BUG("already assigned!");
167 other->matching = i;
168 util->matching = other->i;
172 hashmap_free(&map, 0);
175 static void diffsize_consume(void *data, char *line, unsigned long len)
177 (*(int *)data)++;
180 static int diffsize(const char *a, const char *b)
182 xpparam_t pp = { 0 };
183 xdemitconf_t cfg = { 0 };
184 mmfile_t mf1, mf2;
185 int count = 0;
187 mf1.ptr = (char *)a;
188 mf1.size = strlen(a);
189 mf2.ptr = (char *)b;
190 mf2.size = strlen(b);
192 cfg.ctxlen = 3;
193 if (!xdi_diff_outf(&mf1, &mf2, diffsize_consume, &count, &pp, &cfg))
194 return count;
196 error(_("failed to generate diff"));
197 return COST_MAX;
200 static void get_correspondences(struct string_list *a, struct string_list *b,
201 int creation_factor)
203 int n = a->nr + b->nr;
204 int *cost, c, *a2b, *b2a;
205 int i, j;
207 ALLOC_ARRAY(cost, st_mult(n, n));
208 ALLOC_ARRAY(a2b, n);
209 ALLOC_ARRAY(b2a, n);
211 for (i = 0; i < a->nr; i++) {
212 struct patch_util *a_util = a->items[i].util;
214 for (j = 0; j < b->nr; j++) {
215 struct patch_util *b_util = b->items[j].util;
217 if (a_util->matching == j)
218 c = 0;
219 else if (a_util->matching < 0 && b_util->matching < 0)
220 c = diffsize(a_util->diff, b_util->diff);
221 else
222 c = COST_MAX;
223 cost[i + n * j] = c;
226 c = a_util->matching < 0 ?
227 a_util->diffsize * creation_factor / 100 : COST_MAX;
228 for (j = b->nr; j < n; j++)
229 cost[i + n * j] = c;
232 for (j = 0; j < b->nr; j++) {
233 struct patch_util *util = b->items[j].util;
235 c = util->matching < 0 ?
236 util->diffsize * creation_factor / 100 : COST_MAX;
237 for (i = a->nr; i < n; i++)
238 cost[i + n * j] = c;
241 for (i = a->nr; i < n; i++)
242 for (j = b->nr; j < n; j++)
243 cost[i + n * j] = 0;
245 compute_assignment(n, n, cost, a2b, b2a);
247 for (i = 0; i < a->nr; i++)
248 if (a2b[i] >= 0 && a2b[i] < b->nr) {
249 struct patch_util *a_util = a->items[i].util;
250 struct patch_util *b_util = b->items[a2b[i]].util;
252 a_util->matching = a2b[i];
253 b_util->matching = i;
256 free(cost);
257 free(a2b);
258 free(b2a);
261 static void output_pair_header(struct diff_options *diffopt,
262 struct strbuf *buf,
263 struct strbuf *dashes,
264 struct patch_util *a_util,
265 struct patch_util *b_util)
267 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
268 struct commit *commit;
269 char status;
270 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
271 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
272 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
273 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
274 const char *color;
276 if (!dashes->len)
277 strbuf_addchars(dashes, '-',
278 strlen(find_unique_abbrev(oid,
279 DEFAULT_ABBREV)));
281 if (!b_util) {
282 color = color_old;
283 status = '<';
284 } else if (!a_util) {
285 color = color_new;
286 status = '>';
287 } else if (strcmp(a_util->patch, b_util->patch)) {
288 color = color_commit;
289 status = '!';
290 } else {
291 color = color_commit;
292 status = '=';
295 strbuf_reset(buf);
296 strbuf_addstr(buf, status == '!' ? color_old : color);
297 if (!a_util)
298 strbuf_addf(buf, "-: %s ", dashes->buf);
299 else
300 strbuf_addf(buf, "%d: %s ", a_util->i + 1,
301 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
303 if (status == '!')
304 strbuf_addf(buf, "%s%s", color_reset, color);
305 strbuf_addch(buf, status);
306 if (status == '!')
307 strbuf_addf(buf, "%s%s", color_reset, color_new);
309 if (!b_util)
310 strbuf_addf(buf, " -: %s", dashes->buf);
311 else
312 strbuf_addf(buf, " %d: %s", b_util->i + 1,
313 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
315 commit = lookup_commit_reference(the_repository, oid);
316 if (commit) {
317 if (status == '!')
318 strbuf_addf(buf, "%s%s", color_reset, color);
320 strbuf_addch(buf, ' ');
321 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
323 strbuf_addf(buf, "%s\n", color_reset);
325 fwrite(buf->buf, buf->len, 1, stdout);
328 static struct userdiff_driver no_func_name = {
329 .funcname = { "$^", 0 }
332 static struct diff_filespec *get_filespec(const char *name, const char *p)
334 struct diff_filespec *spec = alloc_filespec(name);
336 fill_filespec(spec, &null_oid, 0, 0644);
337 spec->data = (char *)p;
338 spec->size = strlen(p);
339 spec->should_munmap = 0;
340 spec->is_stdin = 1;
341 spec->driver = &no_func_name;
343 return spec;
346 static void patch_diff(const char *a, const char *b,
347 struct diff_options *diffopt)
349 diff_queue(&diff_queued_diff,
350 get_filespec("a", a), get_filespec("b", b));
352 diffcore_std(diffopt);
353 diff_flush(diffopt);
356 static void output(struct string_list *a, struct string_list *b,
357 struct diff_options *diffopt)
359 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
360 int i = 0, j = 0;
363 * We assume the user is really more interested in the second argument
364 * ("newer" version). To that end, we print the output in the order of
365 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
366 * commits that are no longer in the RHS into a good place, we place
367 * them once we have shown all of their predecessors in the LHS.
370 while (i < a->nr || j < b->nr) {
371 struct patch_util *a_util, *b_util;
372 a_util = i < a->nr ? a->items[i].util : NULL;
373 b_util = j < b->nr ? b->items[j].util : NULL;
375 /* Skip all the already-shown commits from the LHS. */
376 while (i < a->nr && a_util->shown)
377 a_util = ++i < a->nr ? a->items[i].util : NULL;
379 /* Show unmatched LHS commit whose predecessors were shown. */
380 if (i < a->nr && a_util->matching < 0) {
381 output_pair_header(diffopt,
382 &buf, &dashes, a_util, NULL);
383 i++;
384 continue;
387 /* Show unmatched RHS commits. */
388 while (j < b->nr && b_util->matching < 0) {
389 output_pair_header(diffopt,
390 &buf, &dashes, NULL, b_util);
391 b_util = ++j < b->nr ? b->items[j].util : NULL;
394 /* Show matching LHS/RHS pair. */
395 if (j < b->nr) {
396 a_util = a->items[b_util->matching].util;
397 output_pair_header(diffopt,
398 &buf, &dashes, a_util, b_util);
399 if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
400 patch_diff(a->items[b_util->matching].string,
401 b->items[j].string, diffopt);
402 a_util->shown = 1;
403 j++;
406 strbuf_release(&buf);
407 strbuf_release(&dashes);
410 int show_range_diff(const char *range1, const char *range2,
411 int creation_factor, struct diff_options *diffopt)
413 int res = 0;
415 struct string_list branch1 = STRING_LIST_INIT_DUP;
416 struct string_list branch2 = STRING_LIST_INIT_DUP;
418 if (read_patches(range1, &branch1))
419 res = error(_("could not parse log for '%s'"), range1);
420 if (!res && read_patches(range2, &branch2))
421 res = error(_("could not parse log for '%s'"), range2);
423 if (!res) {
424 find_exact_matches(&branch1, &branch2);
425 get_correspondences(&branch1, &branch2, creation_factor);
426 output(&branch1, &branch2, diffopt);
429 string_list_clear(&branch1, 1);
430 string_list_clear(&branch2, 1);
432 return res;