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