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