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