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