🌐 AI搜索 & 代理 主页
blob: 24dc435e482c0364ebc88e7ad55930f437d73ad2 [file] [log] [blame]
Johannes Schindelind9c66f02018-08-13 11:33:041#include "cache.h"
2#include "range-diff.h"
3#include "string-list.h"
4#include "run-command.h"
Jeff Kingdbbcd442020-07-28 20:23:395#include "strvec.h"
Johannes Schindelind9c66f02018-08-13 11:33:046#include "hashmap.h"
7#include "xdiff-interface.h"
8#include "linear-assignment.h"
Johannes Schindelinc8c5e432018-08-13 11:33:079#include "diffcore.h"
Johannes Schindelineb0be382018-08-13 11:33:1310#include "commit.h"
11#include "pretty.h"
Johannes Schindelin4eba1fe2018-08-13 11:33:1412#include "userdiff.h"
Thomas Gummererb66885a2019-07-11 16:08:4913#include "apply.h"
Johannes Schindelind9c66f02018-08-13 11:33:0414
15struct patch_util {
16 /* For the search for an exact match */
17 struct hashmap_entry e;
18 const char *diff, *patch;
19
Johannes Schindelin9dc46e02018-08-13 11:33:0520 int i, shown;
Johannes Schindelind9c66f02018-08-13 11:33:0421 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
Thomas Gummerer44b67cb2019-07-11 16:08:4628static 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
Johannes Schindelind9c66f02018-08-13 11:33:0439/*
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 */
Denton Liubd361912019-11-20 21:18:4543static int read_patches(const char *range, struct string_list *list,
Jeff Kingc972bf42020-07-28 20:25:1244 const struct strvec *other_arg)
Johannes Schindelind9c66f02018-08-13 11:33:0445{
46 struct child_process cp = CHILD_PROCESS_INIT;
Thomas Gummerer44b67cb2019-07-11 16:08:4647 struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
Johannes Schindelind9c66f02018-08-13 11:33:0448 struct patch_util *util = NULL;
49 int in_header = 1;
Thomas Gummerer444e0962019-07-11 16:08:5050 char *line, *current_filename = NULL;
Thomas Gummerer44b67cb2019-07-11 16:08:4651 int offset, len;
52 size_t size;
Johannes Schindelind9c66f02018-08-13 11:33:0453
Jeff Kingc972bf42020-07-28 20:25:1254 strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
Jeff Kingf6d89422020-07-28 20:26:3155 "--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);
Denton Liubd361912019-11-20 21:18:4569 if (other_arg)
Jeff Kingd70a9eb2020-07-29 00:37:2070 strvec_pushv(&cp.args, other_arg->v);
Jeff Kingc972bf42020-07-28 20:25:1271 strvec_push(&cp.args, range);
Johannes Schindelind9c66f02018-08-13 11:33:0472 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`"));
Thomas Gummerer44b67cb2019-07-11 16:08:4678 if (strbuf_read(&contents, cp.out, 0) < 0) {
Johannes Schindelind9c66f02018-08-13 11:33:0479 error_errno(_("could not read `log` output"));
80 finish_command(&cp);
81 return -1;
82 }
83
Thomas Gummerer44b67cb2019-07-11 16:08:4684 line = contents.buf;
85 size = contents.len;
86 for (offset = 0; size > 0; offset += len, size -= len, line += len) {
Johannes Schindelind9c66f02018-08-13 11:33:0487 const char *p;
88
Thomas Gummerer44b67cb2019-07-11 16:08:4689 len = find_end_of_line(line, size);
90 line[len - 1] = '\0';
91 if (skip_prefix(line, "commit ", &p)) {
Johannes Schindelind9c66f02018-08-13 11:33:0492 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);
Thomas Gummerer44b67cb2019-07-11 16:08:46102 strbuf_release(&contents);
Johannes Schindelind9c66f02018-08-13 11:33:04103 finish_command(&cp);
104 return -1;
105 }
106 util->matching = -1;
107 in_header = 1;
108 continue;
109 }
110
Vasil Dimov8cf51562020-04-15 20:32:24111 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
Thomas Gummerer44b67cb2019-07-11 16:08:46122 if (starts_with(line, "diff --git")) {
Thomas Gummererb66885a2019-07-11 16:08:49123 struct patch patch = { 0 };
124 struct strbuf root = STRBUF_INIT;
125 int linenr = 0;
Vasil Dimov8d1675e2020-04-15 20:32:25126 int orig_len;
Thomas Gummererb66885a2019-07-11 16:08:49127
Johannes Schindelind9c66f02018-08-13 11:33:04128 in_header = 0;
129 strbuf_addch(&buf, '\n');
130 if (!util->diff_offset)
131 util->diff_offset = buf.len;
Thomas Gummererb66885a2019-07-11 16:08:49132 line[len - 1] = '\n';
Vasil Dimov8d1675e2020-04-15 20:32:25133 orig_len = len;
Johannes Schindelin937b76e2019-10-02 21:10:47134 len = parse_git_diff_header(&root, &linenr, 0, line,
Thomas Gummererb66885a2019-07-11 16:08:49135 len, size, &patch);
136 if (len < 0)
Vasil Dimov8d1675e2020-04-15 20:32:25137 die(_("could not parse git header '%.*s'"),
138 orig_len, line);
Thomas Gummererb66885a2019-07-11 16:08:49139 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
Thomas Gummerer444e0962019-07-11 16:08:50149 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
Thomas Gummererb66885a2019-07-11 16:08:49155 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, " ##");
Johannes Schindelind9c66f02018-08-13 11:33:04161 } else if (in_header) {
Thomas Gummerer44b67cb2019-07-11 16:08:46162 if (starts_with(line, "Author: ")) {
Thomas Gummerer499352c2019-07-11 16:08:51163 strbuf_addstr(&buf, " ## Metadata ##\n");
Thomas Gummerer44b67cb2019-07-11 16:08:46164 strbuf_addstr(&buf, line);
Johannes Schindelind9c66f02018-08-13 11:33:04165 strbuf_addstr(&buf, "\n\n");
Thomas Gummerer499352c2019-07-11 16:08:51166 strbuf_addstr(&buf, " ## Commit message ##\n");
Denton Liu9f726e12019-11-20 21:18:43167 } 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);
Thomas Gummerer44b67cb2019-07-11 16:08:46173 } 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);
Johannes Schindelind9c66f02018-08-13 11:33:04178 strbuf_addch(&buf, '\n');
179 }
180 continue;
Thomas Gummerere1db2632019-07-11 16:08:47181 } else if (skip_prefix(line, "@@ ", &p)) {
182 p = strstr(p, "@@");
Thomas Gummerer444e0962019-07-11 16:08:50183 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);
Thomas Gummererb66885a2019-07-11 16:08:49188 } else if (!line[0])
Johannes Schindelind9c66f02018-08-13 11:33:04189 /*
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.
Johannes Schindelind9c66f02018-08-13 11:33:04195 */
196 continue;
Thomas Gummerer44b67cb2019-07-11 16:08:46197 else if (line[0] == '>') {
Stefan Beller8d5ccb52018-08-17 20:43:53198 strbuf_addch(&buf, '+');
Thomas Gummerer44b67cb2019-07-11 16:08:46199 strbuf_addstr(&buf, line + 1);
200 } else if (line[0] == '<') {
Stefan Beller8d5ccb52018-08-17 20:43:53201 strbuf_addch(&buf, '-');
Thomas Gummerer44b67cb2019-07-11 16:08:46202 strbuf_addstr(&buf, line + 1);
203 } else if (line[0] == '#') {
Stefan Beller8d5ccb52018-08-17 20:43:53204 strbuf_addch(&buf, ' ');
Thomas Gummerer44b67cb2019-07-11 16:08:46205 strbuf_addstr(&buf, line + 1);
Stefan Beller8d5ccb52018-08-17 20:43:53206 } else {
Stefan Beller2543a642018-08-17 20:43:54207 strbuf_addch(&buf, ' ');
Thomas Gummerer44b67cb2019-07-11 16:08:46208 strbuf_addstr(&buf, line);
Stefan Beller8d5ccb52018-08-17 20:43:53209 }
Johannes Schindelind9c66f02018-08-13 11:33:04210
211 strbuf_addch(&buf, '\n');
212 util->diffsize++;
213 }
Thomas Gummerer44b67cb2019-07-11 16:08:46214 strbuf_release(&contents);
Johannes Schindelind9c66f02018-08-13 11:33:04215
216 if (util)
217 string_list_append(list, buf.buf)->util = util;
218 strbuf_release(&buf);
Thomas Gummerer444e0962019-07-11 16:08:50219 free(current_filename);
Johannes Schindelind9c66f02018-08-13 11:33:04220
221 if (finish_command(&cp))
222 return -1;
223
224 return 0;
225}
226
227static int patch_util_cmp(const void *dummy, const struct patch_util *a,
Thomas Gummerer1ca69222019-07-11 16:08:45228 const struct patch_util *b, const char *keydata)
Johannes Schindelind9c66f02018-08-13 11:33:04229{
230 return strcmp(a->diff, keydata ? keydata : b->diff);
231}
232
233static void find_exact_matches(struct string_list *a, struct string_list *b)
234{
235 struct hashmap map;
236 int i;
237
238 hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
239
240 /* First, add the patches of a to a hash map */
241 for (i = 0; i < a->nr; i++) {
242 struct patch_util *util = a->items[i].util;
243
244 util->i = i;
245 util->patch = a->items[i].string;
246 util->diff = util->patch + util->diff_offset;
Eric Wongd22245a2019-10-06 23:30:27247 hashmap_entry_init(&util->e, strhash(util->diff));
Eric Wongb94e5c12019-10-06 23:30:29248 hashmap_add(&map, &util->e);
Johannes Schindelind9c66f02018-08-13 11:33:04249 }
250
251 /* Now try to find exact matches in b */
252 for (i = 0; i < b->nr; i++) {
253 struct patch_util *util = b->items[i].util, *other;
254
255 util->i = i;
256 util->patch = b->items[i].string;
257 util->diff = util->patch + util->diff_offset;
Eric Wongd22245a2019-10-06 23:30:27258 hashmap_entry_init(&util->e, strhash(util->diff));
Eric Wong404ab782019-10-06 23:30:42259 other = hashmap_remove_entry(&map, util, e, NULL);
Johannes Schindelind9c66f02018-08-13 11:33:04260 if (other) {
261 if (other->matching >= 0)
262 BUG("already assigned!");
263
264 other->matching = i;
265 util->matching = other->i;
266 }
267 }
268
Eric Wongc8e424c2019-10-06 23:30:40269 hashmap_free(&map);
Johannes Schindelind9c66f02018-08-13 11:33:04270}
271
272static void diffsize_consume(void *data, char *line, unsigned long len)
273{
274 (*(int *)data)++;
275}
276
Jeff Kingd2eb8092018-11-02 06:39:40277static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
278 const char *funcline, long funclen)
279{
280 diffsize_consume(data, NULL, 0);
281}
282
Johannes Schindelind9c66f02018-08-13 11:33:04283static int diffsize(const char *a, const char *b)
284{
285 xpparam_t pp = { 0 };
286 xdemitconf_t cfg = { 0 };
287 mmfile_t mf1, mf2;
288 int count = 0;
289
290 mf1.ptr = (char *)a;
291 mf1.size = strlen(a);
292 mf2.ptr = (char *)b;
293 mf2.size = strlen(b);
294
295 cfg.ctxlen = 3;
Jeff Kingd2eb8092018-11-02 06:39:40296 if (!xdi_diff_outf(&mf1, &mf2,
297 diffsize_hunk, diffsize_consume, &count,
298 &pp, &cfg))
Johannes Schindelind9c66f02018-08-13 11:33:04299 return count;
300
301 error(_("failed to generate diff"));
302 return COST_MAX;
303}
304
305static void get_correspondences(struct string_list *a, struct string_list *b,
306 int creation_factor)
307{
308 int n = a->nr + b->nr;
309 int *cost, c, *a2b, *b2a;
310 int i, j;
311
312 ALLOC_ARRAY(cost, st_mult(n, n));
313 ALLOC_ARRAY(a2b, n);
314 ALLOC_ARRAY(b2a, n);
315
316 for (i = 0; i < a->nr; i++) {
317 struct patch_util *a_util = a->items[i].util;
318
319 for (j = 0; j < b->nr; j++) {
320 struct patch_util *b_util = b->items[j].util;
321
322 if (a_util->matching == j)
323 c = 0;
324 else if (a_util->matching < 0 && b_util->matching < 0)
325 c = diffsize(a_util->diff, b_util->diff);
326 else
327 c = COST_MAX;
328 cost[i + n * j] = c;
329 }
330
331 c = a_util->matching < 0 ?
332 a_util->diffsize * creation_factor / 100 : COST_MAX;
333 for (j = b->nr; j < n; j++)
334 cost[i + n * j] = c;
335 }
336
337 for (j = 0; j < b->nr; j++) {
338 struct patch_util *util = b->items[j].util;
339
340 c = util->matching < 0 ?
341 util->diffsize * creation_factor / 100 : COST_MAX;
342 for (i = a->nr; i < n; i++)
343 cost[i + n * j] = c;
344 }
345
346 for (i = a->nr; i < n; i++)
347 for (j = b->nr; j < n; j++)
348 cost[i + n * j] = 0;
349
350 compute_assignment(n, n, cost, a2b, b2a);
351
352 for (i = 0; i < a->nr; i++)
353 if (a2b[i] >= 0 && a2b[i] < b->nr) {
354 struct patch_util *a_util = a->items[i].util;
355 struct patch_util *b_util = b->items[a2b[i]].util;
356
357 a_util->matching = a2b[i];
358 b_util->matching = i;
359 }
360
361 free(cost);
362 free(a2b);
363 free(b2a);
364}
365
Johannes Schindelinfaa1df82018-08-13 11:33:18366static void output_pair_header(struct diff_options *diffopt,
Johannes Schindelind1f87a22018-08-13 11:33:28367 int patch_no_width,
Johannes Schindelinfaa1df82018-08-13 11:33:18368 struct strbuf *buf,
Johannes Schindelineb0be382018-08-13 11:33:13369 struct strbuf *dashes,
370 struct patch_util *a_util,
371 struct patch_util *b_util)
Johannes Schindelind9c66f02018-08-13 11:33:04372{
Johannes Schindelineb0be382018-08-13 11:33:13373 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
374 struct commit *commit;
Johannes Schindelinfaa1df82018-08-13 11:33:18375 char status;
376 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
377 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
378 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
379 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
380 const char *color;
Johannes Schindelineb0be382018-08-13 11:33:13381
382 if (!dashes->len)
383 strbuf_addchars(dashes, '-',
384 strlen(find_unique_abbrev(oid,
385 DEFAULT_ABBREV)));
386
Johannes Schindelinfaa1df82018-08-13 11:33:18387 if (!b_util) {
388 color = color_old;
389 status = '<';
390 } else if (!a_util) {
391 color = color_new;
392 status = '>';
393 } else if (strcmp(a_util->patch, b_util->patch)) {
394 color = color_commit;
395 status = '!';
396 } else {
397 color = color_commit;
398 status = '=';
399 }
400
Johannes Schindelineb0be382018-08-13 11:33:13401 strbuf_reset(buf);
Johannes Schindelinfaa1df82018-08-13 11:33:18402 strbuf_addstr(buf, status == '!' ? color_old : color);
Johannes Schindelineb0be382018-08-13 11:33:13403 if (!a_util)
Johannes Schindelind1f87a22018-08-13 11:33:28404 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
Johannes Schindelineb0be382018-08-13 11:33:13405 else
Johannes Schindelind1f87a22018-08-13 11:33:28406 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
Johannes Schindelineb0be382018-08-13 11:33:13407 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
408
Johannes Schindelinfaa1df82018-08-13 11:33:18409 if (status == '!')
410 strbuf_addf(buf, "%s%s", color_reset, color);
411 strbuf_addch(buf, status);
412 if (status == '!')
413 strbuf_addf(buf, "%s%s", color_reset, color_new);
Johannes Schindelineb0be382018-08-13 11:33:13414
415 if (!b_util)
Johannes Schindelind1f87a22018-08-13 11:33:28416 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
Johannes Schindelineb0be382018-08-13 11:33:13417 else
Johannes Schindelind1f87a22018-08-13 11:33:28418 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
Johannes Schindelineb0be382018-08-13 11:33:13419 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
420
421 commit = lookup_commit_reference(the_repository, oid);
422 if (commit) {
Johannes Schindelinfaa1df82018-08-13 11:33:18423 if (status == '!')
424 strbuf_addf(buf, "%s%s", color_reset, color);
425
Johannes Schindelineb0be382018-08-13 11:33:13426 strbuf_addch(buf, ' ');
427 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
428 }
Johannes Schindelinfaa1df82018-08-13 11:33:18429 strbuf_addf(buf, "%s\n", color_reset);
Johannes Schindelineb0be382018-08-13 11:33:13430
Eric Sunshine87f1b2d2018-07-22 09:57:10431 fwrite(buf->buf, buf->len, 1, diffopt->file);
Johannes Schindelind9c66f02018-08-13 11:33:04432}
433
Thomas Gummerer499352c2019-07-11 16:08:51434static struct userdiff_driver section_headers = {
435 .funcname = { "^ ## (.*) ##$\n"
436 "^.?@@ (.*)$", REG_EXTENDED }
Johannes Schindelin4eba1fe2018-08-13 11:33:14437};
438
Johannes Schindelinc8c5e432018-08-13 11:33:07439static struct diff_filespec *get_filespec(const char *name, const char *p)
440{
441 struct diff_filespec *spec = alloc_filespec(name);
442
Lucas De Marchi0e573e82018-10-24 19:46:17443 fill_filespec(spec, &null_oid, 0, 0100644);
Johannes Schindelinc8c5e432018-08-13 11:33:07444 spec->data = (char *)p;
445 spec->size = strlen(p);
446 spec->should_munmap = 0;
447 spec->is_stdin = 1;
Thomas Gummerer499352c2019-07-11 16:08:51448 spec->driver = &section_headers;
Johannes Schindelinc8c5e432018-08-13 11:33:07449
450 return spec;
451}
452
453static void patch_diff(const char *a, const char *b,
Thomas Gummerer1ca69222019-07-11 16:08:45454 struct diff_options *diffopt)
Johannes Schindelinc8c5e432018-08-13 11:33:07455{
456 diff_queue(&diff_queued_diff,
457 get_filespec("a", a), get_filespec("b", b));
458
459 diffcore_std(diffopt);
460 diff_flush(diffopt);
461}
462
463static void output(struct string_list *a, struct string_list *b,
464 struct diff_options *diffopt)
Johannes Schindelind9c66f02018-08-13 11:33:04465{
Johannes Schindelineb0be382018-08-13 11:33:13466 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
Johannes Schindelind1f87a22018-08-13 11:33:28467 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
Johannes Schindelin9dc46e02018-08-13 11:33:05468 int i = 0, j = 0;
Johannes Schindelind9c66f02018-08-13 11:33:04469
Johannes Schindelin9dc46e02018-08-13 11:33:05470 /*
471 * We assume the user is really more interested in the second argument
472 * ("newer" version). To that end, we print the output in the order of
473 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
474 * commits that are no longer in the RHS into a good place, we place
475 * them once we have shown all of their predecessors in the LHS.
476 */
Johannes Schindelind9c66f02018-08-13 11:33:04477
Johannes Schindelin9dc46e02018-08-13 11:33:05478 while (i < a->nr || j < b->nr) {
479 struct patch_util *a_util, *b_util;
480 a_util = i < a->nr ? a->items[i].util : NULL;
481 b_util = j < b->nr ? b->items[j].util : NULL;
Johannes Schindelind9c66f02018-08-13 11:33:04482
Johannes Schindelin9dc46e02018-08-13 11:33:05483 /* Skip all the already-shown commits from the LHS. */
484 while (i < a->nr && a_util->shown)
485 a_util = ++i < a->nr ? a->items[i].util : NULL;
Johannes Schindelind9c66f02018-08-13 11:33:04486
Johannes Schindelin9dc46e02018-08-13 11:33:05487 /* Show unmatched LHS commit whose predecessors were shown. */
488 if (i < a->nr && a_util->matching < 0) {
Johannes Schindelind1f87a22018-08-13 11:33:28489 output_pair_header(diffopt, patch_no_width,
Johannes Schindelinfaa1df82018-08-13 11:33:18490 &buf, &dashes, a_util, NULL);
Johannes Schindelin9dc46e02018-08-13 11:33:05491 i++;
492 continue;
493 }
494
495 /* Show unmatched RHS commits. */
496 while (j < b->nr && b_util->matching < 0) {
Johannes Schindelind1f87a22018-08-13 11:33:28497 output_pair_header(diffopt, patch_no_width,
Johannes Schindelinfaa1df82018-08-13 11:33:18498 &buf, &dashes, NULL, b_util);
Johannes Schindelin9dc46e02018-08-13 11:33:05499 b_util = ++j < b->nr ? b->items[j].util : NULL;
500 }
501
502 /* Show matching LHS/RHS pair. */
503 if (j < b->nr) {
504 a_util = a->items[b_util->matching].util;
Johannes Schindelind1f87a22018-08-13 11:33:28505 output_pair_header(diffopt, patch_no_width,
Johannes Schindelinfaa1df82018-08-13 11:33:18506 &buf, &dashes, a_util, b_util);
Johannes Schindelinc8c5e432018-08-13 11:33:07507 if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
508 patch_diff(a->items[b_util->matching].string,
509 b->items[j].string, diffopt);
Johannes Schindelin9dc46e02018-08-13 11:33:05510 a_util->shown = 1;
511 j++;
512 }
Johannes Schindelind9c66f02018-08-13 11:33:04513 }
Johannes Schindelineb0be382018-08-13 11:33:13514 strbuf_release(&buf);
515 strbuf_release(&dashes);
Johannes Schindelind9c66f02018-08-13 11:33:04516}
517
Eric Sunshine73a834e2018-07-22 09:57:12518static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
519{
520 return data;
521}
522
Johannes Schindelind9c66f02018-08-13 11:33:04523int show_range_diff(const char *range1, const char *range2,
Eric Sunshine73a834e2018-07-22 09:57:12524 int creation_factor, int dual_color,
Denton Liuf8675342019-12-06 20:16:26525 const struct diff_options *diffopt,
Jeff Kingc972bf42020-07-28 20:25:12526 const struct strvec *other_arg)
Johannes Schindelind9c66f02018-08-13 11:33:04527{
528 int res = 0;
529
530 struct string_list branch1 = STRING_LIST_INIT_DUP;
531 struct string_list branch2 = STRING_LIST_INIT_DUP;
532
Denton Liubd361912019-11-20 21:18:45533 if (read_patches(range1, &branch1, other_arg))
Johannes Schindelind9c66f02018-08-13 11:33:04534 res = error(_("could not parse log for '%s'"), range1);
Denton Liubd361912019-11-20 21:18:45535 if (!res && read_patches(range2, &branch2, other_arg))
Johannes Schindelind9c66f02018-08-13 11:33:04536 res = error(_("could not parse log for '%s'"), range2);
537
538 if (!res) {
Eric Sunshine73a834e2018-07-22 09:57:12539 struct diff_options opts;
540 struct strbuf indent = STRBUF_INIT;
541
Junio C Hamanod8981c32018-11-30 04:27:11542 if (diffopt)
543 memcpy(&opts, diffopt, sizeof(opts));
544 else
545 diff_setup(&opts);
546
Ævar Arnfjörð Bjarmasona48e12e2018-11-13 18:55:58547 if (!opts.output_format)
548 opts.output_format = DIFF_FORMAT_PATCH;
Eric Sunshine73a834e2018-07-22 09:57:12549 opts.flags.suppress_diff_headers = 1;
550 opts.flags.dual_color_diffed_diffs = dual_color;
Thomas Gummerer430be362019-07-11 16:08:48551 opts.flags.suppress_hunk_header_line_count = 1;
Eric Sunshine73a834e2018-07-22 09:57:12552 opts.output_prefix = output_prefix_cb;
553 strbuf_addstr(&indent, " ");
554 opts.output_prefix_data = &indent;
555 diff_setup_done(&opts);
556
Johannes Schindelind9c66f02018-08-13 11:33:04557 find_exact_matches(&branch1, &branch2);
558 get_correspondences(&branch1, &branch2, creation_factor);
Eric Sunshine73a834e2018-07-22 09:57:12559 output(&branch1, &branch2, &opts);
560
561 strbuf_release(&indent);
Johannes Schindelind9c66f02018-08-13 11:33:04562 }
563
564 string_list_clear(&branch1, 1);
565 string_list_clear(&branch2, 1);
566
567 return res;
568}