]>
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", | |
2e0d30d9 | 63 | "--show-notes-by-default", |
f6d8942b | 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, |
beaa1d95 JK |
233 | const struct hashmap_entry *ha, |
234 | const struct hashmap_entry *hb, | |
235 | const void *keydata) | |
d9c66f0b | 236 | { |
beaa1d95 JK |
237 | const struct patch_util |
238 | *a = container_of(ha, const struct patch_util, e), | |
239 | *b = container_of(hb, const struct patch_util, e); | |
d9c66f0b JS |
240 | return strcmp(a->diff, keydata ? keydata : b->diff); |
241 | } | |
242 | ||
243 | static void find_exact_matches(struct string_list *a, struct string_list *b) | |
244 | { | |
beaa1d95 | 245 | struct hashmap map = HASHMAP_INIT(patch_util_cmp, NULL); |
d9c66f0b JS |
246 | int i; |
247 | ||
d9c66f0b JS |
248 | /* First, add the patches of a to a hash map */ |
249 | for (i = 0; i < a->nr; i++) { | |
250 | struct patch_util *util = a->items[i].util; | |
251 | ||
252 | util->i = i; | |
253 | util->patch = a->items[i].string; | |
254 | util->diff = util->patch + util->diff_offset; | |
d22245a2 | 255 | hashmap_entry_init(&util->e, strhash(util->diff)); |
b94e5c1d | 256 | hashmap_add(&map, &util->e); |
d9c66f0b JS |
257 | } |
258 | ||
259 | /* Now try to find exact matches in b */ | |
260 | for (i = 0; i < b->nr; i++) { | |
261 | struct patch_util *util = b->items[i].util, *other; | |
262 | ||
263 | util->i = i; | |
264 | util->patch = b->items[i].string; | |
265 | util->diff = util->patch + util->diff_offset; | |
d22245a2 | 266 | hashmap_entry_init(&util->e, strhash(util->diff)); |
404ab78e | 267 | other = hashmap_remove_entry(&map, util, e, NULL); |
d9c66f0b JS |
268 | if (other) { |
269 | if (other->matching >= 0) | |
270 | BUG("already assigned!"); | |
271 | ||
272 | other->matching = i; | |
273 | util->matching = other->i; | |
274 | } | |
275 | } | |
276 | ||
6da1a258 | 277 | hashmap_clear(&map); |
d9c66f0b JS |
278 | } |
279 | ||
61bdc7c5 JK |
280 | static int diffsize_consume(void *data, |
281 | char *line UNUSED, | |
282 | unsigned long len UNUSED) | |
d9c66f0b JS |
283 | { |
284 | (*(int *)data)++; | |
a8d5eb6d | 285 | return 0; |
d9c66f0b JS |
286 | } |
287 | ||
61bdc7c5 JK |
288 | static void diffsize_hunk(void *data, |
289 | long ob UNUSED, long on UNUSED, | |
290 | long nb UNUSED, long nn UNUSED, | |
291 | const char *func UNUSED, long funclen UNUSED) | |
d2eb8093 JK |
292 | { |
293 | diffsize_consume(data, NULL, 0); | |
294 | } | |
295 | ||
d9c66f0b JS |
296 | static int diffsize(const char *a, const char *b) |
297 | { | |
298 | xpparam_t pp = { 0 }; | |
299 | xdemitconf_t cfg = { 0 }; | |
300 | mmfile_t mf1, mf2; | |
301 | int count = 0; | |
302 | ||
303 | mf1.ptr = (char *)a; | |
304 | mf1.size = strlen(a); | |
305 | mf2.ptr = (char *)b; | |
306 | mf2.size = strlen(b); | |
307 | ||
308 | cfg.ctxlen = 3; | |
d2eb8093 JK |
309 | if (!xdi_diff_outf(&mf1, &mf2, |
310 | diffsize_hunk, diffsize_consume, &count, | |
311 | &pp, &cfg)) | |
d9c66f0b JS |
312 | return count; |
313 | ||
314 | error(_("failed to generate diff")); | |
315 | return COST_MAX; | |
316 | } | |
317 | ||
318 | static void get_correspondences(struct string_list *a, struct string_list *b, | |
319 | int creation_factor) | |
320 | { | |
321 | int n = a->nr + b->nr; | |
322 | int *cost, c, *a2b, *b2a; | |
323 | int i, j; | |
324 | ||
325 | ALLOC_ARRAY(cost, st_mult(n, n)); | |
326 | ALLOC_ARRAY(a2b, n); | |
327 | ALLOC_ARRAY(b2a, n); | |
328 | ||
329 | for (i = 0; i < a->nr; i++) { | |
330 | struct patch_util *a_util = a->items[i].util; | |
331 | ||
332 | for (j = 0; j < b->nr; j++) { | |
333 | struct patch_util *b_util = b->items[j].util; | |
334 | ||
335 | if (a_util->matching == j) | |
336 | c = 0; | |
337 | else if (a_util->matching < 0 && b_util->matching < 0) | |
338 | c = diffsize(a_util->diff, b_util->diff); | |
339 | else | |
340 | c = COST_MAX; | |
341 | cost[i + n * j] = c; | |
342 | } | |
343 | ||
344 | c = a_util->matching < 0 ? | |
345 | a_util->diffsize * creation_factor / 100 : COST_MAX; | |
346 | for (j = b->nr; j < n; j++) | |
347 | cost[i + n * j] = c; | |
348 | } | |
349 | ||
350 | for (j = 0; j < b->nr; j++) { | |
351 | struct patch_util *util = b->items[j].util; | |
352 | ||
353 | c = util->matching < 0 ? | |
354 | util->diffsize * creation_factor / 100 : COST_MAX; | |
355 | for (i = a->nr; i < n; i++) | |
356 | cost[i + n * j] = c; | |
357 | } | |
358 | ||
359 | for (i = a->nr; i < n; i++) | |
360 | for (j = b->nr; j < n; j++) | |
361 | cost[i + n * j] = 0; | |
362 | ||
363 | compute_assignment(n, n, cost, a2b, b2a); | |
364 | ||
365 | for (i = 0; i < a->nr; i++) | |
366 | if (a2b[i] >= 0 && a2b[i] < b->nr) { | |
367 | struct patch_util *a_util = a->items[i].util; | |
368 | struct patch_util *b_util = b->items[a2b[i]].util; | |
369 | ||
370 | a_util->matching = a2b[i]; | |
371 | b_util->matching = i; | |
372 | } | |
373 | ||
374 | free(cost); | |
375 | free(a2b); | |
376 | free(b2a); | |
377 | } | |
378 | ||
faa1df86 | 379 | static void output_pair_header(struct diff_options *diffopt, |
d1f87a2d | 380 | int patch_no_width, |
faa1df86 | 381 | struct strbuf *buf, |
eb0be38c JS |
382 | struct strbuf *dashes, |
383 | struct patch_util *a_util, | |
384 | struct patch_util *b_util) | |
d9c66f0b | 385 | { |
eb0be38c JS |
386 | struct object_id *oid = a_util ? &a_util->oid : &b_util->oid; |
387 | struct commit *commit; | |
faa1df86 JS |
388 | char status; |
389 | const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET); | |
390 | const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD); | |
391 | const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW); | |
392 | const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT); | |
393 | const char *color; | |
d9165bef | 394 | int abbrev = diffopt->abbrev; |
2b15969f TL |
395 | |
396 | if (abbrev < 0) | |
397 | abbrev = DEFAULT_ABBREV; | |
eb0be38c JS |
398 | |
399 | if (!dashes->len) | |
400 | strbuf_addchars(dashes, '-', | |
d850b7a5 | 401 | strlen(repo_find_unique_abbrev(the_repository, oid, abbrev))); |
eb0be38c | 402 | |
faa1df86 JS |
403 | if (!b_util) { |
404 | color = color_old; | |
405 | status = '<'; | |
406 | } else if (!a_util) { | |
407 | color = color_new; | |
408 | status = '>'; | |
409 | } else if (strcmp(a_util->patch, b_util->patch)) { | |
410 | color = color_commit; | |
411 | status = '!'; | |
412 | } else { | |
413 | color = color_commit; | |
414 | status = '='; | |
415 | } | |
416 | ||
eb0be38c | 417 | strbuf_reset(buf); |
faa1df86 | 418 | strbuf_addstr(buf, status == '!' ? color_old : color); |
eb0be38c | 419 | if (!a_util) |
d1f87a2d | 420 | strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf); |
eb0be38c | 421 | else |
d1f87a2d | 422 | strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1, |
d850b7a5 | 423 | repo_find_unique_abbrev(the_repository, &a_util->oid, abbrev)); |
eb0be38c | 424 | |
faa1df86 JS |
425 | if (status == '!') |
426 | strbuf_addf(buf, "%s%s", color_reset, color); | |
427 | strbuf_addch(buf, status); | |
428 | if (status == '!') | |
429 | strbuf_addf(buf, "%s%s", color_reset, color_new); | |
eb0be38c JS |
430 | |
431 | if (!b_util) | |
d1f87a2d | 432 | strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf); |
eb0be38c | 433 | else |
d1f87a2d | 434 | strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1, |
d850b7a5 | 435 | repo_find_unique_abbrev(the_repository, &b_util->oid, abbrev)); |
eb0be38c JS |
436 | |
437 | commit = lookup_commit_reference(the_repository, oid); | |
438 | if (commit) { | |
faa1df86 JS |
439 | if (status == '!') |
440 | strbuf_addf(buf, "%s%s", color_reset, color); | |
441 | ||
eb0be38c JS |
442 | strbuf_addch(buf, ' '); |
443 | pp_commit_easy(CMIT_FMT_ONELINE, commit, buf); | |
444 | } | |
faa1df86 | 445 | strbuf_addf(buf, "%s\n", color_reset); |
eb0be38c | 446 | |
87f1b2d4 | 447 | fwrite(buf->buf, buf->len, 1, diffopt->file); |
d9c66f0b JS |
448 | } |
449 | ||
499352c2 TG |
450 | static struct userdiff_driver section_headers = { |
451 | .funcname = { "^ ## (.*) ##$\n" | |
452 | "^.?@@ (.*)$", REG_EXTENDED } | |
4eba1fe6 JS |
453 | }; |
454 | ||
c8c5e43a JS |
455 | static struct diff_filespec *get_filespec(const char *name, const char *p) |
456 | { | |
457 | struct diff_filespec *spec = alloc_filespec(name); | |
458 | ||
14228447 | 459 | fill_filespec(spec, null_oid(), 0, 0100644); |
c8c5e43a JS |
460 | spec->data = (char *)p; |
461 | spec->size = strlen(p); | |
462 | spec->should_munmap = 0; | |
463 | spec->is_stdin = 1; | |
499352c2 | 464 | spec->driver = §ion_headers; |
c8c5e43a JS |
465 | |
466 | return spec; | |
467 | } | |
468 | ||
469 | static void patch_diff(const char *a, const char *b, | |
1ca69225 | 470 | struct diff_options *diffopt) |
c8c5e43a JS |
471 | { |
472 | diff_queue(&diff_queued_diff, | |
473 | get_filespec("a", a), get_filespec("b", b)); | |
474 | ||
475 | diffcore_std(diffopt); | |
476 | diff_flush(diffopt); | |
477 | } | |
478 | ||
61bdc7c5 | 479 | static struct strbuf *output_prefix_cb(struct diff_options *opt UNUSED, void *data) |
3e6046ed JS |
480 | { |
481 | return data; | |
482 | } | |
483 | ||
c8c5e43a | 484 | static void output(struct string_list *a, struct string_list *b, |
3e6046ed | 485 | struct range_diff_options *range_diff_opts) |
d9c66f0b | 486 | { |
eb0be38c | 487 | struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT; |
d1f87a2d | 488 | int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr)); |
9dc46e02 | 489 | int i = 0, j = 0; |
3e6046ed JS |
490 | struct diff_options opts; |
491 | struct strbuf indent = STRBUF_INIT; | |
492 | ||
493 | if (range_diff_opts->diffopt) | |
494 | memcpy(&opts, range_diff_opts->diffopt, sizeof(opts)); | |
495 | else | |
08539032 | 496 | repo_diff_setup(the_repository, &opts); |
3e6046ed | 497 | |
709b3f32 | 498 | opts.no_free = 1; |
3e6046ed JS |
499 | if (!opts.output_format) |
500 | opts.output_format = DIFF_FORMAT_PATCH; | |
501 | opts.flags.suppress_diff_headers = 1; | |
502 | opts.flags.dual_color_diffed_diffs = | |
503 | range_diff_opts->dual_color; | |
504 | opts.flags.suppress_hunk_header_line_count = 1; | |
505 | opts.output_prefix = output_prefix_cb; | |
506 | strbuf_addstr(&indent, " "); | |
507 | opts.output_prefix_data = &indent; | |
508 | diff_setup_done(&opts); | |
9dc46e02 JS |
509 | |
510 | /* | |
511 | * We assume the user is really more interested in the second argument | |
512 | * ("newer" version). To that end, we print the output in the order of | |
513 | * the RHS (the `b` parameter). To put the LHS (the `a` parameter) | |
514 | * commits that are no longer in the RHS into a good place, we place | |
515 | * them once we have shown all of their predecessors in the LHS. | |
516 | */ | |
517 | ||
518 | while (i < a->nr || j < b->nr) { | |
519 | struct patch_util *a_util, *b_util; | |
520 | a_util = i < a->nr ? a->items[i].util : NULL; | |
521 | b_util = j < b->nr ? b->items[j].util : NULL; | |
522 | ||
523 | /* Skip all the already-shown commits from the LHS. */ | |
524 | while (i < a->nr && a_util->shown) | |
525 | a_util = ++i < a->nr ? a->items[i].util : NULL; | |
526 | ||
527 | /* Show unmatched LHS commit whose predecessors were shown. */ | |
528 | if (i < a->nr && a_util->matching < 0) { | |
1e79f973 JS |
529 | if (!range_diff_opts->right_only) |
530 | output_pair_header(&opts, patch_no_width, | |
faa1df86 | 531 | &buf, &dashes, a_util, NULL); |
9dc46e02 JS |
532 | i++; |
533 | continue; | |
534 | } | |
d9c66f0b | 535 | |
9dc46e02 JS |
536 | /* Show unmatched RHS commits. */ |
537 | while (j < b->nr && b_util->matching < 0) { | |
1e79f973 JS |
538 | if (!range_diff_opts->left_only) |
539 | output_pair_header(&opts, patch_no_width, | |
faa1df86 | 540 | &buf, &dashes, NULL, b_util); |
9dc46e02 | 541 | b_util = ++j < b->nr ? b->items[j].util : NULL; |
d9c66f0b | 542 | } |
d9c66f0b | 543 | |
9dc46e02 JS |
544 | /* Show matching LHS/RHS pair. */ |
545 | if (j < b->nr) { | |
546 | a_util = a->items[b_util->matching].util; | |
3e6046ed | 547 | output_pair_header(&opts, patch_no_width, |
faa1df86 | 548 | &buf, &dashes, a_util, b_util); |
3e6046ed | 549 | if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT)) |
c8c5e43a | 550 | patch_diff(a->items[b_util->matching].string, |
3e6046ed | 551 | b->items[j].string, &opts); |
9dc46e02 JS |
552 | a_util->shown = 1; |
553 | j++; | |
554 | } | |
d9c66f0b | 555 | } |
eb0be38c JS |
556 | strbuf_release(&buf); |
557 | strbuf_release(&dashes); | |
3e6046ed | 558 | strbuf_release(&indent); |
709b3f32 RS |
559 | opts.no_free = 0; |
560 | diff_free(&opts); | |
73a834e9 ES |
561 | } |
562 | ||
d9c66f0b | 563 | int show_range_diff(const char *range1, const char *range2, |
f1ce6c19 | 564 | struct range_diff_options *range_diff_opts) |
d9c66f0b JS |
565 | { |
566 | int res = 0; | |
567 | ||
568 | struct string_list branch1 = STRING_LIST_INIT_DUP; | |
569 | struct string_list branch2 = STRING_LIST_INIT_DUP; | |
570 | ||
1e79f973 | 571 | if (range_diff_opts->left_only && range_diff_opts->right_only) |
43ea635c | 572 | res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only"); |
1e79f973 JS |
573 | |
574 | if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg)) | |
d9c66f0b | 575 | res = error(_("could not parse log for '%s'"), range1); |
f1ce6c19 | 576 | if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg)) |
d9c66f0b JS |
577 | res = error(_("could not parse log for '%s'"), range2); |
578 | ||
579 | if (!res) { | |
580 | find_exact_matches(&branch1, &branch2); | |
f1ce6c19 JS |
581 | get_correspondences(&branch1, &branch2, |
582 | range_diff_opts->creation_factor); | |
3e6046ed | 583 | output(&branch1, &branch2, range_diff_opts); |
d9c66f0b JS |
584 | } |
585 | ||
586 | string_list_clear(&branch1, 1); | |
587 | string_list_clear(&branch2, 1); | |
588 | ||
589 | return res; | |
590 | } | |
679b5916 JS |
591 | |
592 | int is_range_diff_range(const char *arg) | |
593 | { | |
359f0d75 JS |
594 | char *copy = xstrdup(arg); /* setup_revisions() modifies it */ |
595 | const char *argv[] = { "", copy, "--", NULL }; | |
596 | int i, positive = 0, negative = 0; | |
597 | struct rev_info revs; | |
598 | ||
035c7de9 | 599 | repo_init_revisions(the_repository, &revs, NULL); |
359f0d75 JS |
600 | if (setup_revisions(3, argv, &revs, NULL) == 1) { |
601 | for (i = 0; i < revs.pending.nr; i++) | |
602 | if (revs.pending.objects[i].item->flags & UNINTERESTING) | |
603 | negative++; | |
604 | else | |
605 | positive++; | |
606 | for (i = 0; i < revs.pending.nr; i++) { | |
607 | struct object *obj = revs.pending.objects[i].item; | |
608 | ||
609 | if (obj->type == OBJ_COMMIT) | |
610 | clear_commit_marks((struct commit *)obj, | |
611 | ALL_REV_FLAGS); | |
612 | } | |
613 | } | |
614 | ||
615 | free(copy); | |
1878b5ed | 616 | release_revisions(&revs); |
359f0d75 | 617 | return negative > 0 && positive > 0; |
679b5916 | 618 | } |