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