]>
Commit | Line | Data |
---|---|---|
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 | |
14 | struct 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 | ||
27 | /* | |
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). | |
30 | */ | |
31 | static int read_patches(const char *range, struct string_list *list) | |
32 | { | |
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; | |
38 | ||
39 | argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges", | |
40 | "--reverse", "--date-order", "--decorate=no", | |
8d5ccb59 SB |
41 | /* |
42 | * Choose indicators that are not used anywhere | |
43 | * else in diffs, but still look reasonable | |
44 | * (e.g. will not be confusing when debugging) | |
45 | */ | |
46 | "--output-indicator-new=>", | |
47 | "--output-indicator-old=<", | |
48 | "--output-indicator-context=#", | |
d9c66f0b JS |
49 | "--no-abbrev-commit", range, |
50 | NULL); | |
51 | cp.out = -1; | |
52 | cp.no_stdin = 1; | |
53 | cp.git_cmd = 1; | |
54 | ||
55 | if (start_command(&cp)) | |
56 | return error_errno(_("could not start `log`")); | |
57 | in = fdopen(cp.out, "r"); | |
58 | if (!in) { | |
59 | error_errno(_("could not read `log` output")); | |
60 | finish_command(&cp); | |
61 | return -1; | |
62 | } | |
63 | ||
64 | while (strbuf_getline(&line, in) != EOF) { | |
65 | const char *p; | |
66 | ||
67 | if (skip_prefix(line.buf, "commit ", &p)) { | |
68 | if (util) { | |
69 | string_list_append(list, buf.buf)->util = util; | |
70 | strbuf_reset(&buf); | |
71 | } | |
72 | util = xcalloc(sizeof(*util), 1); | |
73 | if (get_oid(p, &util->oid)) { | |
74 | error(_("could not parse commit '%s'"), p); | |
75 | free(util); | |
76 | string_list_clear(list, 1); | |
77 | strbuf_release(&buf); | |
78 | strbuf_release(&line); | |
79 | fclose(in); | |
80 | finish_command(&cp); | |
81 | return -1; | |
82 | } | |
83 | util->matching = -1; | |
84 | in_header = 1; | |
85 | continue; | |
86 | } | |
87 | ||
88 | if (starts_with(line.buf, "diff --git")) { | |
89 | in_header = 0; | |
90 | strbuf_addch(&buf, '\n'); | |
91 | if (!util->diff_offset) | |
92 | util->diff_offset = buf.len; | |
93 | strbuf_addbuf(&buf, &line); | |
94 | } else if (in_header) { | |
95 | if (starts_with(line.buf, "Author: ")) { | |
96 | strbuf_addbuf(&buf, &line); | |
97 | strbuf_addstr(&buf, "\n\n"); | |
98 | } else if (starts_with(line.buf, " ")) { | |
a142f978 | 99 | strbuf_rtrim(&line); |
d9c66f0b JS |
100 | strbuf_addbuf(&buf, &line); |
101 | strbuf_addch(&buf, '\n'); | |
102 | } | |
103 | continue; | |
104 | } else if (starts_with(line.buf, "@@ ")) | |
105 | strbuf_addstr(&buf, "@@"); | |
106 | else if (!line.buf[0] || starts_with(line.buf, "index ")) | |
107 | /* | |
108 | * A completely blank (not ' \n', which is context) | |
109 | * line is not valid in a diff. We skip it | |
110 | * silently, because this neatly handles the blank | |
111 | * separator line between commits in git-log | |
112 | * output. | |
113 | * | |
114 | * We also want to ignore the diff's `index` lines | |
115 | * because they contain exact blob hashes in which | |
116 | * we are not interested. | |
117 | */ | |
118 | continue; | |
8d5ccb59 SB |
119 | else if (line.buf[0] == '>') { |
120 | strbuf_addch(&buf, '+'); | |
121 | strbuf_add(&buf, line.buf + 1, line.len - 1); | |
122 | } else if (line.buf[0] == '<') { | |
123 | strbuf_addch(&buf, '-'); | |
124 | strbuf_add(&buf, line.buf + 1, line.len - 1); | |
125 | } else if (line.buf[0] == '#') { | |
126 | strbuf_addch(&buf, ' '); | |
127 | strbuf_add(&buf, line.buf + 1, line.len - 1); | |
128 | } else { | |
d9c66f0b | 129 | strbuf_addbuf(&buf, &line); |
8d5ccb59 | 130 | } |
d9c66f0b JS |
131 | |
132 | strbuf_addch(&buf, '\n'); | |
133 | util->diffsize++; | |
134 | } | |
135 | fclose(in); | |
136 | strbuf_release(&line); | |
137 | ||
138 | if (util) | |
139 | string_list_append(list, buf.buf)->util = util; | |
140 | strbuf_release(&buf); | |
141 | ||
142 | if (finish_command(&cp)) | |
143 | return -1; | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
148 | static int patch_util_cmp(const void *dummy, const struct patch_util *a, | |
149 | const struct patch_util *b, const char *keydata) | |
150 | { | |
151 | return strcmp(a->diff, keydata ? keydata : b->diff); | |
152 | } | |
153 | ||
154 | static void find_exact_matches(struct string_list *a, struct string_list *b) | |
155 | { | |
156 | struct hashmap map; | |
157 | int i; | |
158 | ||
159 | hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0); | |
160 | ||
161 | /* First, add the patches of a to a hash map */ | |
162 | for (i = 0; i < a->nr; i++) { | |
163 | struct patch_util *util = a->items[i].util; | |
164 | ||
165 | util->i = i; | |
166 | util->patch = a->items[i].string; | |
167 | util->diff = util->patch + util->diff_offset; | |
168 | hashmap_entry_init(util, strhash(util->diff)); | |
169 | hashmap_add(&map, util); | |
170 | } | |
171 | ||
172 | /* Now try to find exact matches in b */ | |
173 | for (i = 0; i < b->nr; i++) { | |
174 | struct patch_util *util = b->items[i].util, *other; | |
175 | ||
176 | util->i = i; | |
177 | util->patch = b->items[i].string; | |
178 | util->diff = util->patch + util->diff_offset; | |
179 | hashmap_entry_init(util, strhash(util->diff)); | |
180 | other = hashmap_remove(&map, util, NULL); | |
181 | if (other) { | |
182 | if (other->matching >= 0) | |
183 | BUG("already assigned!"); | |
184 | ||
185 | other->matching = i; | |
186 | util->matching = other->i; | |
187 | } | |
188 | } | |
189 | ||
190 | hashmap_free(&map, 0); | |
191 | } | |
192 | ||
193 | static void diffsize_consume(void *data, char *line, unsigned long len) | |
194 | { | |
195 | (*(int *)data)++; | |
196 | } | |
197 | ||
198 | static int diffsize(const char *a, const char *b) | |
199 | { | |
200 | xpparam_t pp = { 0 }; | |
201 | xdemitconf_t cfg = { 0 }; | |
202 | mmfile_t mf1, mf2; | |
203 | int count = 0; | |
204 | ||
205 | mf1.ptr = (char *)a; | |
206 | mf1.size = strlen(a); | |
207 | mf2.ptr = (char *)b; | |
208 | mf2.size = strlen(b); | |
209 | ||
210 | cfg.ctxlen = 3; | |
211 | if (!xdi_diff_outf(&mf1, &mf2, diffsize_consume, &count, &pp, &cfg)) | |
212 | return count; | |
213 | ||
214 | error(_("failed to generate diff")); | |
215 | return COST_MAX; | |
216 | } | |
217 | ||
218 | static void get_correspondences(struct string_list *a, struct string_list *b, | |
219 | int creation_factor) | |
220 | { | |
221 | int n = a->nr + b->nr; | |
222 | int *cost, c, *a2b, *b2a; | |
223 | int i, j; | |
224 | ||
225 | ALLOC_ARRAY(cost, st_mult(n, n)); | |
226 | ALLOC_ARRAY(a2b, n); | |
227 | ALLOC_ARRAY(b2a, n); | |
228 | ||
229 | for (i = 0; i < a->nr; i++) { | |
230 | struct patch_util *a_util = a->items[i].util; | |
231 | ||
232 | for (j = 0; j < b->nr; j++) { | |
233 | struct patch_util *b_util = b->items[j].util; | |
234 | ||
235 | if (a_util->matching == j) | |
236 | c = 0; | |
237 | else if (a_util->matching < 0 && b_util->matching < 0) | |
238 | c = diffsize(a_util->diff, b_util->diff); | |
239 | else | |
240 | c = COST_MAX; | |
241 | cost[i + n * j] = c; | |
242 | } | |
243 | ||
244 | c = a_util->matching < 0 ? | |
245 | a_util->diffsize * creation_factor / 100 : COST_MAX; | |
246 | for (j = b->nr; j < n; j++) | |
247 | cost[i + n * j] = c; | |
248 | } | |
249 | ||
250 | for (j = 0; j < b->nr; j++) { | |
251 | struct patch_util *util = b->items[j].util; | |
252 | ||
253 | c = util->matching < 0 ? | |
254 | util->diffsize * creation_factor / 100 : COST_MAX; | |
255 | for (i = a->nr; i < n; i++) | |
256 | cost[i + n * j] = c; | |
257 | } | |
258 | ||
259 | for (i = a->nr; i < n; i++) | |
260 | for (j = b->nr; j < n; j++) | |
261 | cost[i + n * j] = 0; | |
262 | ||
263 | compute_assignment(n, n, cost, a2b, b2a); | |
264 | ||
265 | for (i = 0; i < a->nr; i++) | |
266 | if (a2b[i] >= 0 && a2b[i] < b->nr) { | |
267 | struct patch_util *a_util = a->items[i].util; | |
268 | struct patch_util *b_util = b->items[a2b[i]].util; | |
269 | ||
270 | a_util->matching = a2b[i]; | |
271 | b_util->matching = i; | |
272 | } | |
273 | ||
274 | free(cost); | |
275 | free(a2b); | |
276 | free(b2a); | |
277 | } | |
278 | ||
faa1df86 | 279 | static void output_pair_header(struct diff_options *diffopt, |
d1f87a2d | 280 | int patch_no_width, |
faa1df86 | 281 | struct strbuf *buf, |
eb0be38c JS |
282 | struct strbuf *dashes, |
283 | struct patch_util *a_util, | |
284 | struct patch_util *b_util) | |
d9c66f0b | 285 | { |
eb0be38c JS |
286 | struct object_id *oid = a_util ? &a_util->oid : &b_util->oid; |
287 | struct commit *commit; | |
faa1df86 JS |
288 | char status; |
289 | const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET); | |
290 | const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD); | |
291 | const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW); | |
292 | const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT); | |
293 | const char *color; | |
eb0be38c JS |
294 | |
295 | if (!dashes->len) | |
296 | strbuf_addchars(dashes, '-', | |
297 | strlen(find_unique_abbrev(oid, | |
298 | DEFAULT_ABBREV))); | |
299 | ||
faa1df86 JS |
300 | if (!b_util) { |
301 | color = color_old; | |
302 | status = '<'; | |
303 | } else if (!a_util) { | |
304 | color = color_new; | |
305 | status = '>'; | |
306 | } else if (strcmp(a_util->patch, b_util->patch)) { | |
307 | color = color_commit; | |
308 | status = '!'; | |
309 | } else { | |
310 | color = color_commit; | |
311 | status = '='; | |
312 | } | |
313 | ||
eb0be38c | 314 | strbuf_reset(buf); |
faa1df86 | 315 | strbuf_addstr(buf, status == '!' ? color_old : color); |
eb0be38c | 316 | if (!a_util) |
d1f87a2d | 317 | strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf); |
eb0be38c | 318 | else |
d1f87a2d | 319 | strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1, |
eb0be38c JS |
320 | find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV)); |
321 | ||
faa1df86 JS |
322 | if (status == '!') |
323 | strbuf_addf(buf, "%s%s", color_reset, color); | |
324 | strbuf_addch(buf, status); | |
325 | if (status == '!') | |
326 | strbuf_addf(buf, "%s%s", color_reset, color_new); | |
eb0be38c JS |
327 | |
328 | if (!b_util) | |
d1f87a2d | 329 | strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf); |
eb0be38c | 330 | else |
d1f87a2d | 331 | strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1, |
eb0be38c JS |
332 | find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV)); |
333 | ||
334 | commit = lookup_commit_reference(the_repository, oid); | |
335 | if (commit) { | |
faa1df86 JS |
336 | if (status == '!') |
337 | strbuf_addf(buf, "%s%s", color_reset, color); | |
338 | ||
eb0be38c JS |
339 | strbuf_addch(buf, ' '); |
340 | pp_commit_easy(CMIT_FMT_ONELINE, commit, buf); | |
341 | } | |
faa1df86 | 342 | strbuf_addf(buf, "%s\n", color_reset); |
eb0be38c JS |
343 | |
344 | fwrite(buf->buf, buf->len, 1, stdout); | |
d9c66f0b JS |
345 | } |
346 | ||
4eba1fe6 JS |
347 | static struct userdiff_driver no_func_name = { |
348 | .funcname = { "$^", 0 } | |
349 | }; | |
350 | ||
c8c5e43a JS |
351 | static struct diff_filespec *get_filespec(const char *name, const char *p) |
352 | { | |
353 | struct diff_filespec *spec = alloc_filespec(name); | |
354 | ||
355 | fill_filespec(spec, &null_oid, 0, 0644); | |
356 | spec->data = (char *)p; | |
357 | spec->size = strlen(p); | |
358 | spec->should_munmap = 0; | |
359 | spec->is_stdin = 1; | |
4eba1fe6 | 360 | spec->driver = &no_func_name; |
c8c5e43a JS |
361 | |
362 | return spec; | |
363 | } | |
364 | ||
365 | static void patch_diff(const char *a, const char *b, | |
366 | struct diff_options *diffopt) | |
367 | { | |
368 | diff_queue(&diff_queued_diff, | |
369 | get_filespec("a", a), get_filespec("b", b)); | |
370 | ||
371 | diffcore_std(diffopt); | |
372 | diff_flush(diffopt); | |
373 | } | |
374 | ||
375 | static void output(struct string_list *a, struct string_list *b, | |
376 | struct diff_options *diffopt) | |
d9c66f0b | 377 | { |
eb0be38c | 378 | struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT; |
d1f87a2d | 379 | int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr)); |
9dc46e02 JS |
380 | int i = 0, j = 0; |
381 | ||
382 | /* | |
383 | * We assume the user is really more interested in the second argument | |
384 | * ("newer" version). To that end, we print the output in the order of | |
385 | * the RHS (the `b` parameter). To put the LHS (the `a` parameter) | |
386 | * commits that are no longer in the RHS into a good place, we place | |
387 | * them once we have shown all of their predecessors in the LHS. | |
388 | */ | |
389 | ||
390 | while (i < a->nr || j < b->nr) { | |
391 | struct patch_util *a_util, *b_util; | |
392 | a_util = i < a->nr ? a->items[i].util : NULL; | |
393 | b_util = j < b->nr ? b->items[j].util : NULL; | |
394 | ||
395 | /* Skip all the already-shown commits from the LHS. */ | |
396 | while (i < a->nr && a_util->shown) | |
397 | a_util = ++i < a->nr ? a->items[i].util : NULL; | |
398 | ||
399 | /* Show unmatched LHS commit whose predecessors were shown. */ | |
400 | if (i < a->nr && a_util->matching < 0) { | |
d1f87a2d | 401 | output_pair_header(diffopt, patch_no_width, |
faa1df86 | 402 | &buf, &dashes, a_util, NULL); |
9dc46e02 JS |
403 | i++; |
404 | continue; | |
405 | } | |
d9c66f0b | 406 | |
9dc46e02 JS |
407 | /* Show unmatched RHS commits. */ |
408 | while (j < b->nr && b_util->matching < 0) { | |
d1f87a2d | 409 | output_pair_header(diffopt, patch_no_width, |
faa1df86 | 410 | &buf, &dashes, NULL, b_util); |
9dc46e02 | 411 | b_util = ++j < b->nr ? b->items[j].util : NULL; |
d9c66f0b | 412 | } |
d9c66f0b | 413 | |
9dc46e02 JS |
414 | /* Show matching LHS/RHS pair. */ |
415 | if (j < b->nr) { | |
416 | a_util = a->items[b_util->matching].util; | |
d1f87a2d | 417 | output_pair_header(diffopt, patch_no_width, |
faa1df86 | 418 | &buf, &dashes, a_util, b_util); |
c8c5e43a JS |
419 | if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT)) |
420 | patch_diff(a->items[b_util->matching].string, | |
421 | b->items[j].string, diffopt); | |
9dc46e02 JS |
422 | a_util->shown = 1; |
423 | j++; | |
424 | } | |
d9c66f0b | 425 | } |
eb0be38c JS |
426 | strbuf_release(&buf); |
427 | strbuf_release(&dashes); | |
d9c66f0b JS |
428 | } |
429 | ||
430 | int show_range_diff(const char *range1, const char *range2, | |
c8c5e43a | 431 | int creation_factor, struct diff_options *diffopt) |
d9c66f0b JS |
432 | { |
433 | int res = 0; | |
434 | ||
435 | struct string_list branch1 = STRING_LIST_INIT_DUP; | |
436 | struct string_list branch2 = STRING_LIST_INIT_DUP; | |
437 | ||
438 | if (read_patches(range1, &branch1)) | |
439 | res = error(_("could not parse log for '%s'"), range1); | |
440 | if (!res && read_patches(range2, &branch2)) | |
441 | res = error(_("could not parse log for '%s'"), range2); | |
442 | ||
443 | if (!res) { | |
444 | find_exact_matches(&branch1, &branch2); | |
445 | get_correspondences(&branch1, &branch2, creation_factor); | |
c8c5e43a | 446 | output(&branch1, &branch2, diffopt); |
d9c66f0b JS |
447 | } |
448 | ||
449 | string_list_clear(&branch1, 1); | |
450 | string_list_clear(&branch2, 1); | |
451 | ||
452 | return res; | |
453 | } |