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