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