blob: c45b6d849cbf4f97ec3d6eb893259837d25d9052 [file] [log] [blame]
Elijah Newren5e3f94d2023-04-22 20:17:23 +00001#include "git-compat-util.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00002#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00003#include "gettext.h"
Johannes Schindelind9c66f02018-08-13 04:33:04 -07004#include "range-diff.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -07005#include "object-name.h"
Johannes Schindelind9c66f02018-08-13 04:33:04 -07006#include "string-list.h"
7#include "run-command.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -04008#include "strvec.h"
Johannes Schindelind9c66f02018-08-13 04:33:04 -07009#include "hashmap.h"
10#include "xdiff-interface.h"
11#include "linear-assignment.h"
Johannes Schindelinc8c5e432018-08-13 04:33:07 -070012#include "diffcore.h"
Johannes Schindelineb0be382018-08-13 04:33:13 -070013#include "commit.h"
Elijah Newrenca4eed72023-04-11 00:41:59 -070014#include "pager.h"
Johannes Schindelineb0be382018-08-13 04:33:13 -070015#include "pretty.h"
Elijah Newrendf6e8742023-05-16 06:34:00 +000016#include "repository.h"
Johannes Schindelin4eba1fe2018-08-13 04:33:14 -070017#include "userdiff.h"
Thomas Gummererb66885a2019-07-11 17:08:49 +010018#include "apply.h"
Johannes Schindelin359f0d72021-02-05 14:44:48 +000019#include "revision.h"
Johannes Schindelind9c66f02018-08-13 04:33:04 -070020
21struct patch_util {
22 /* For the search for an exact match */
23 struct hashmap_entry e;
24 const char *diff, *patch;
25
Johannes Schindelin9dc46e02018-08-13 04:33:05 -070026 int i, shown;
Johannes Schindelind9c66f02018-08-13 04:33:04 -070027 int diffsize;
28 size_t diff_offset;
29 /* the index of the matching item in the other branch, or -1 */
30 int matching;
31 struct object_id oid;
32};
33
34/*
35 * Reads the patches into a string list, with the `util` field being populated
36 * as struct object_id (will need to be free()d).
37 */
Denton Liubd361912019-11-20 13:18:45 -080038static int read_patches(const char *range, struct string_list *list,
Jeff Kingc972bf42020-07-28 16:25:12 -040039 const struct strvec *other_arg)
Johannes Schindelind9c66f02018-08-13 04:33:04 -070040{
41 struct child_process cp = CHILD_PROCESS_INIT;
Thomas Gummerer44b67cb2019-07-11 17:08:46 +010042 struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
Johannes Schindelind9c66f02018-08-13 04:33:04 -070043 struct patch_util *util = NULL;
44 int in_header = 1;
Thomas Gummerer444e0962019-07-11 17:08:50 +010045 char *line, *current_filename = NULL;
Jeff Kingc4d59072021-08-09 18:48:48 -040046 ssize_t len;
Thomas Gummerer44b67cb2019-07-11 17:08:46 +010047 size_t size;
Ævar Arnfjörð Bjarmason2d102c22022-03-04 19:32:16 +010048 int ret = -1;
Johannes Schindelind9c66f02018-08-13 04:33:04 -070049
Jeff Kingc972bf42020-07-28 16:25:12 -040050 strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
Jeff Kingf6d89422020-07-28 16:26:31 -040051 "--reverse", "--date-order", "--decorate=no",
Philippe Blain04b1f1f2022-06-06 20:59:13 +000052 "--no-prefix", "--submodule=short",
Jeff Kingf6d89422020-07-28 16:26:31 -040053 /*
54 * Choose indicators that are not used anywhere
55 * else in diffs, but still look reasonable
56 * (e.g. will not be confusing when debugging)
57 */
58 "--output-indicator-new=>",
59 "--output-indicator-old=<",
60 "--output-indicator-context=#",
61 "--no-abbrev-commit",
62 "--pretty=medium",
Kristoffer Haugsbakk2e0d30d2023-09-19 22:26:50 +020063 "--show-notes-by-default",
Jeff Kingf6d89422020-07-28 16:26:31 -040064 NULL);
Johannes Schindelinb7574782022-08-26 09:39:30 +000065 strvec_push(&cp.args, range);
Denton Liubd361912019-11-20 13:18:45 -080066 if (other_arg)
Jeff Kingd70a9eb2020-07-28 20:37:20 -040067 strvec_pushv(&cp.args, other_arg->v);
Johannes Schindelind9c66f02018-08-13 04:33:04 -070068 cp.out = -1;
69 cp.no_stdin = 1;
70 cp.git_cmd = 1;
71
72 if (start_command(&cp))
73 return error_errno(_("could not start `log`"));
Thomas Gummerer44b67cb2019-07-11 17:08:46 +010074 if (strbuf_read(&contents, cp.out, 0) < 0) {
Johannes Schindelind9c66f02018-08-13 04:33:04 -070075 error_errno(_("could not read `log` output"));
76 finish_command(&cp);
Ævar Arnfjörð Bjarmason2d102c22022-03-04 19:32:16 +010077 goto cleanup;
Johannes Schindelind9c66f02018-08-13 04:33:04 -070078 }
Johannes Schindelin5189bb82021-02-04 20:07:51 +000079 if (finish_command(&cp))
Ævar Arnfjörð Bjarmason2d102c22022-03-04 19:32:16 +010080 goto cleanup;
Johannes Schindelind9c66f02018-08-13 04:33:04 -070081
Thomas Gummerer44b67cb2019-07-11 17:08:46 +010082 line = contents.buf;
83 size = contents.len;
Jeff King47ac23d2021-08-09 18:47:42 -040084 for (; size > 0; size -= len, line += len) {
Johannes Schindelind9c66f02018-08-13 04:33:04 -070085 const char *p;
Jeff King7c86d362021-08-09 18:48:39 -040086 char *eol;
Johannes Schindelind9c66f02018-08-13 04:33:04 -070087
Jeff King7c86d362021-08-09 18:48:39 -040088 eol = memchr(line, '\n', size);
89 if (eol) {
90 *eol = '\0';
91 len = eol + 1 - line;
92 } else {
93 len = size;
94 }
95
Thomas Gummerer44b67cb2019-07-11 17:08:46 +010096 if (skip_prefix(line, "commit ", &p)) {
Johannes Schindelind9c66f02018-08-13 04:33:04 -070097 if (util) {
98 string_list_append(list, buf.buf)->util = util;
99 strbuf_reset(&buf);
100 }
René Scharfeca56dad2021-03-13 17:17:22 +0100101 CALLOC_ARRAY(util, 1);
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200102 if (repo_get_oid(the_repository, p, &util->oid)) {
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700103 error(_("could not parse commit '%s'"), p);
Ævar Arnfjörð Bjarmason2d102c22022-03-04 19:32:16 +0100104 FREE_AND_NULL(util);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700105 string_list_clear(list, 1);
Ævar Arnfjörð Bjarmason2d102c22022-03-04 19:32:16 +0100106 goto cleanup;
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700107 }
108 util->matching = -1;
109 in_header = 1;
110 continue;
111 }
112
Vasil Dimov8cf51562020-04-15 20:32:24 +0000113 if (!util) {
114 error(_("could not parse first line of `log` output: "
115 "did not start with 'commit ': '%s'"),
116 line);
117 string_list_clear(list, 1);
Ævar Arnfjörð Bjarmason2d102c22022-03-04 19:32:16 +0100118 goto cleanup;
Vasil Dimov8cf51562020-04-15 20:32:24 +0000119 }
120
Thomas Gummerer44b67cb2019-07-11 17:08:46 +0100121 if (starts_with(line, "diff --git")) {
Thomas Gummererb66885a2019-07-11 17:08:49 +0100122 struct patch patch = { 0 };
123 struct strbuf root = STRBUF_INIT;
124 int linenr = 0;
Vasil Dimov8d1675e2020-04-15 20:32:25 +0000125 int orig_len;
Thomas Gummererb66885a2019-07-11 17:08:49 +0100126
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700127 in_header = 0;
128 strbuf_addch(&buf, '\n');
129 if (!util->diff_offset)
130 util->diff_offset = buf.len;
Jeff King7c86d362021-08-09 18:48:39 -0400131 if (eol)
132 *eol = '\n';
Vasil Dimov8d1675e2020-04-15 20:32:25 +0000133 orig_len = len;
Johannes Schindelin937b76e2019-10-02 14:10:47 -0700134 len = parse_git_diff_header(&root, &linenr, 0, line,
Thomas Gummererb66885a2019-07-11 17:08:49 +0100135 len, size, &patch);
Johannes Schindelina2d474a2021-02-04 20:07:50 +0000136 if (len < 0) {
137 error(_("could not parse git header '%.*s'"),
138 orig_len, line);
Ævar Arnfjörð Bjarmason2d102c22022-03-04 19:32:16 +0100139 FREE_AND_NULL(util);
Johannes Schindelina2d474a2021-02-04 20:07:50 +0000140 string_list_clear(list, 1);
Ævar Arnfjörð Bjarmason2d102c22022-03-04 19:32:16 +0100141 goto cleanup;
Johannes Schindelina2d474a2021-02-04 20:07:50 +0000142 }
Thomas Gummererb66885a2019-07-11 17:08:49 +0100143 strbuf_addstr(&buf, " ## ");
144 if (patch.is_new > 0)
145 strbuf_addf(&buf, "%s (new)", patch.new_name);
146 else if (patch.is_delete > 0)
147 strbuf_addf(&buf, "%s (deleted)", patch.old_name);
148 else if (patch.is_rename)
149 strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
150 else
151 strbuf_addstr(&buf, patch.new_name);
152
Thomas Gummerer444e0962019-07-11 17:08:50 +0100153 free(current_filename);
154 if (patch.is_delete > 0)
155 current_filename = xstrdup(patch.old_name);
156 else
157 current_filename = xstrdup(patch.new_name);
158
Thomas Gummererb66885a2019-07-11 17:08:49 +0100159 if (patch.new_mode && patch.old_mode &&
160 patch.old_mode != patch.new_mode)
161 strbuf_addf(&buf, " (mode change %06o => %06o)",
162 patch.old_mode, patch.new_mode);
163
164 strbuf_addstr(&buf, " ##");
Ævar Arnfjörð Bjarmason4998e932022-03-04 19:32:15 +0100165 release_patch(&patch);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700166 } else if (in_header) {
Thomas Gummerer44b67cb2019-07-11 17:08:46 +0100167 if (starts_with(line, "Author: ")) {
Thomas Gummerer499352c2019-07-11 17:08:51 +0100168 strbuf_addstr(&buf, " ## Metadata ##\n");
Thomas Gummerer44b67cb2019-07-11 17:08:46 +0100169 strbuf_addstr(&buf, line);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700170 strbuf_addstr(&buf, "\n\n");
Thomas Gummerer499352c2019-07-11 17:08:51 +0100171 strbuf_addstr(&buf, " ## Commit message ##\n");
Denton Liu9f726e12019-11-20 13:18:43 -0800172 } else if (starts_with(line, "Notes") &&
173 line[strlen(line) - 1] == ':') {
174 strbuf_addstr(&buf, "\n\n");
175 /* strip the trailing colon */
176 strbuf_addf(&buf, " ## %.*s ##\n",
177 (int)(strlen(line) - 1), line);
Thomas Gummerer44b67cb2019-07-11 17:08:46 +0100178 } else if (starts_with(line, " ")) {
179 p = line + len - 2;
180 while (isspace(*p) && p >= line)
181 p--;
182 strbuf_add(&buf, line, p - line + 1);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700183 strbuf_addch(&buf, '\n');
184 }
185 continue;
Thomas Gummerere1db2632019-07-11 17:08:47 +0100186 } else if (skip_prefix(line, "@@ ", &p)) {
187 p = strstr(p, "@@");
Thomas Gummerer444e0962019-07-11 17:08:50 +0100188 strbuf_addstr(&buf, "@@");
189 if (current_filename && p[2])
190 strbuf_addf(&buf, " %s:", current_filename);
191 if (p)
192 strbuf_addstr(&buf, p + 2);
Thomas Gummererb66885a2019-07-11 17:08:49 +0100193 } else if (!line[0])
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700194 /*
195 * A completely blank (not ' \n', which is context)
196 * line is not valid in a diff. We skip it
197 * silently, because this neatly handles the blank
198 * separator line between commits in git-log
199 * output.
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700200 */
201 continue;
Thomas Gummerer44b67cb2019-07-11 17:08:46 +0100202 else if (line[0] == '>') {
Stefan Beller8d5ccb52018-08-17 13:43:53 -0700203 strbuf_addch(&buf, '+');
Thomas Gummerer44b67cb2019-07-11 17:08:46 +0100204 strbuf_addstr(&buf, line + 1);
205 } else if (line[0] == '<') {
Stefan Beller8d5ccb52018-08-17 13:43:53 -0700206 strbuf_addch(&buf, '-');
Thomas Gummerer44b67cb2019-07-11 17:08:46 +0100207 strbuf_addstr(&buf, line + 1);
208 } else if (line[0] == '#') {
Stefan Beller8d5ccb52018-08-17 13:43:53 -0700209 strbuf_addch(&buf, ' ');
Thomas Gummerer44b67cb2019-07-11 17:08:46 +0100210 strbuf_addstr(&buf, line + 1);
Stefan Beller8d5ccb52018-08-17 13:43:53 -0700211 } else {
Stefan Beller2543a642018-08-17 13:43:54 -0700212 strbuf_addch(&buf, ' ');
Thomas Gummerer44b67cb2019-07-11 17:08:46 +0100213 strbuf_addstr(&buf, line);
Stefan Beller8d5ccb52018-08-17 13:43:53 -0700214 }
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700215
216 strbuf_addch(&buf, '\n');
217 util->diffsize++;
218 }
Ævar Arnfjörð Bjarmason2d102c22022-03-04 19:32:16 +0100219
220 ret = 0;
221cleanup:
Thomas Gummerer44b67cb2019-07-11 17:08:46 +0100222 strbuf_release(&contents);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700223
224 if (util)
225 string_list_append(list, buf.buf)->util = util;
226 strbuf_release(&buf);
Thomas Gummerer444e0962019-07-11 17:08:50 +0100227 free(current_filename);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700228
Ævar Arnfjörð Bjarmason2d102c22022-03-04 19:32:16 +0100229 return ret;
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700230}
231
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200232static int patch_util_cmp(const void *cmp_data UNUSED,
Jeff Kingbeaa1d92023-08-19 19:55:30 -0400233 const struct hashmap_entry *ha,
234 const struct hashmap_entry *hb,
235 const void *keydata)
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700236{
Jeff Kingbeaa1d92023-08-19 19:55:30 -0400237 const struct patch_util
238 *a = container_of(ha, const struct patch_util, e),
239 *b = container_of(hb, const struct patch_util, e);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700240 return strcmp(a->diff, keydata ? keydata : b->diff);
241}
242
243static void find_exact_matches(struct string_list *a, struct string_list *b)
244{
Jeff Kingbeaa1d92023-08-19 19:55:30 -0400245 struct hashmap map = HASHMAP_INIT(patch_util_cmp, NULL);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700246 int i;
247
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700248 /* First, add the patches of a to a hash map */
249 for (i = 0; i < a->nr; i++) {
250 struct patch_util *util = a->items[i].util;
251
252 util->i = i;
253 util->patch = a->items[i].string;
254 util->diff = util->patch + util->diff_offset;
Eric Wongd22245a2019-10-06 23:30:27 +0000255 hashmap_entry_init(&util->e, strhash(util->diff));
Eric Wongb94e5c12019-10-06 23:30:29 +0000256 hashmap_add(&map, &util->e);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700257 }
258
259 /* Now try to find exact matches in b */
260 for (i = 0; i < b->nr; i++) {
261 struct patch_util *util = b->items[i].util, *other;
262
263 util->i = i;
264 util->patch = b->items[i].string;
265 util->diff = util->patch + util->diff_offset;
Eric Wongd22245a2019-10-06 23:30:27 +0000266 hashmap_entry_init(&util->e, strhash(util->diff));
Eric Wong404ab782019-10-06 23:30:42 +0000267 other = hashmap_remove_entry(&map, util, e, NULL);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700268 if (other) {
269 if (other->matching >= 0)
270 BUG("already assigned!");
271
272 other->matching = i;
273 util->matching = other->i;
274 }
275 }
276
Elijah Newren6da1a252020-11-02 18:55:05 +0000277 hashmap_clear(&map);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700278}
279
Jeff King61bdc7c2022-12-13 06:13:48 -0500280static int diffsize_consume(void *data,
281 char *line UNUSED,
282 unsigned long len UNUSED)
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700283{
284 (*(int *)data)++;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200285 return 0;
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700286}
287
Jeff King61bdc7c2022-12-13 06:13:48 -0500288static void diffsize_hunk(void *data,
289 long ob UNUSED, long on UNUSED,
290 long nb UNUSED, long nn UNUSED,
291 const char *func UNUSED, long funclen UNUSED)
Jeff Kingd2eb8092018-11-02 02:39:40 -0400292{
293 diffsize_consume(data, NULL, 0);
294}
295
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700296static int diffsize(const char *a, const char *b)
297{
298 xpparam_t pp = { 0 };
299 xdemitconf_t cfg = { 0 };
300 mmfile_t mf1, mf2;
301 int count = 0;
302
303 mf1.ptr = (char *)a;
304 mf1.size = strlen(a);
305 mf2.ptr = (char *)b;
306 mf2.size = strlen(b);
307
308 cfg.ctxlen = 3;
Jeff Kingd2eb8092018-11-02 02:39:40 -0400309 if (!xdi_diff_outf(&mf1, &mf2,
310 diffsize_hunk, diffsize_consume, &count,
311 &pp, &cfg))
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700312 return count;
313
314 error(_("failed to generate diff"));
315 return COST_MAX;
316}
317
318static void get_correspondences(struct string_list *a, struct string_list *b,
319 int creation_factor)
320{
321 int n = a->nr + b->nr;
322 int *cost, c, *a2b, *b2a;
323 int i, j;
324
325 ALLOC_ARRAY(cost, st_mult(n, n));
326 ALLOC_ARRAY(a2b, n);
327 ALLOC_ARRAY(b2a, n);
328
329 for (i = 0; i < a->nr; i++) {
330 struct patch_util *a_util = a->items[i].util;
331
332 for (j = 0; j < b->nr; j++) {
333 struct patch_util *b_util = b->items[j].util;
334
335 if (a_util->matching == j)
336 c = 0;
337 else if (a_util->matching < 0 && b_util->matching < 0)
338 c = diffsize(a_util->diff, b_util->diff);
339 else
340 c = COST_MAX;
341 cost[i + n * j] = c;
342 }
343
344 c = a_util->matching < 0 ?
345 a_util->diffsize * creation_factor / 100 : COST_MAX;
346 for (j = b->nr; j < n; j++)
347 cost[i + n * j] = c;
348 }
349
350 for (j = 0; j < b->nr; j++) {
351 struct patch_util *util = b->items[j].util;
352
353 c = util->matching < 0 ?
354 util->diffsize * creation_factor / 100 : COST_MAX;
355 for (i = a->nr; i < n; i++)
356 cost[i + n * j] = c;
357 }
358
359 for (i = a->nr; i < n; i++)
360 for (j = b->nr; j < n; j++)
361 cost[i + n * j] = 0;
362
363 compute_assignment(n, n, cost, a2b, b2a);
364
365 for (i = 0; i < a->nr; i++)
366 if (a2b[i] >= 0 && a2b[i] < b->nr) {
367 struct patch_util *a_util = a->items[i].util;
368 struct patch_util *b_util = b->items[a2b[i]].util;
369
370 a_util->matching = a2b[i];
371 b_util->matching = i;
372 }
373
374 free(cost);
375 free(a2b);
376 free(b2a);
377}
378
Johannes Schindelinfaa1df82018-08-13 04:33:18 -0700379static void output_pair_header(struct diff_options *diffopt,
Johannes Schindelind1f87a22018-08-13 04:33:28 -0700380 int patch_no_width,
Johannes Schindelinfaa1df82018-08-13 04:33:18 -0700381 struct strbuf *buf,
Johannes Schindelineb0be382018-08-13 04:33:13 -0700382 struct strbuf *dashes,
383 struct patch_util *a_util,
384 struct patch_util *b_util)
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700385{
Johannes Schindelineb0be382018-08-13 04:33:13 -0700386 struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
387 struct commit *commit;
Johannes Schindelinfaa1df82018-08-13 04:33:18 -0700388 char status;
389 const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
390 const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
391 const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
392 const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
393 const char *color;
René Scharfed9165be2023-02-28 17:13:27 +0100394 int abbrev = diffopt->abbrev;
Teng Long2b159692023-02-20 22:24:44 +0800395
396 if (abbrev < 0)
397 abbrev = DEFAULT_ABBREV;
Johannes Schindelineb0be382018-08-13 04:33:13 -0700398
399 if (!dashes->len)
400 strbuf_addchars(dashes, '-',
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200401 strlen(repo_find_unique_abbrev(the_repository, oid, abbrev)));
Johannes Schindelineb0be382018-08-13 04:33:13 -0700402
Johannes Schindelinfaa1df82018-08-13 04:33:18 -0700403 if (!b_util) {
404 color = color_old;
405 status = '<';
406 } else if (!a_util) {
407 color = color_new;
408 status = '>';
409 } else if (strcmp(a_util->patch, b_util->patch)) {
410 color = color_commit;
411 status = '!';
412 } else {
413 color = color_commit;
414 status = '=';
415 }
416
Johannes Schindelineb0be382018-08-13 04:33:13 -0700417 strbuf_reset(buf);
Johannes Schindelinfaa1df82018-08-13 04:33:18 -0700418 strbuf_addstr(buf, status == '!' ? color_old : color);
Johannes Schindelineb0be382018-08-13 04:33:13 -0700419 if (!a_util)
Johannes Schindelind1f87a22018-08-13 04:33:28 -0700420 strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
Johannes Schindelineb0be382018-08-13 04:33:13 -0700421 else
Johannes Schindelind1f87a22018-08-13 04:33:28 -0700422 strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200423 repo_find_unique_abbrev(the_repository, &a_util->oid, abbrev));
Johannes Schindelineb0be382018-08-13 04:33:13 -0700424
Johannes Schindelinfaa1df82018-08-13 04:33:18 -0700425 if (status == '!')
426 strbuf_addf(buf, "%s%s", color_reset, color);
427 strbuf_addch(buf, status);
428 if (status == '!')
429 strbuf_addf(buf, "%s%s", color_reset, color_new);
Johannes Schindelineb0be382018-08-13 04:33:13 -0700430
431 if (!b_util)
Johannes Schindelind1f87a22018-08-13 04:33:28 -0700432 strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
Johannes Schindelineb0be382018-08-13 04:33:13 -0700433 else
Johannes Schindelind1f87a22018-08-13 04:33:28 -0700434 strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200435 repo_find_unique_abbrev(the_repository, &b_util->oid, abbrev));
Johannes Schindelineb0be382018-08-13 04:33:13 -0700436
437 commit = lookup_commit_reference(the_repository, oid);
438 if (commit) {
Johannes Schindelinfaa1df82018-08-13 04:33:18 -0700439 if (status == '!')
440 strbuf_addf(buf, "%s%s", color_reset, color);
441
Johannes Schindelineb0be382018-08-13 04:33:13 -0700442 strbuf_addch(buf, ' ');
443 pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
444 }
Johannes Schindelinfaa1df82018-08-13 04:33:18 -0700445 strbuf_addf(buf, "%s\n", color_reset);
Johannes Schindelineb0be382018-08-13 04:33:13 -0700446
Eric Sunshine87f1b2d2018-07-22 05:57:10 -0400447 fwrite(buf->buf, buf->len, 1, diffopt->file);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700448}
449
Thomas Gummerer499352c2019-07-11 17:08:51 +0100450static struct userdiff_driver section_headers = {
451 .funcname = { "^ ## (.*) ##$\n"
452 "^.?@@ (.*)$", REG_EXTENDED }
Johannes Schindelin4eba1fe2018-08-13 04:33:14 -0700453};
454
Johannes Schindelinc8c5e432018-08-13 04:33:07 -0700455static struct diff_filespec *get_filespec(const char *name, const char *p)
456{
457 struct diff_filespec *spec = alloc_filespec(name);
458
brian m. carlson14228442021-04-26 01:02:56 +0000459 fill_filespec(spec, null_oid(), 0, 0100644);
Johannes Schindelinc8c5e432018-08-13 04:33:07 -0700460 spec->data = (char *)p;
461 spec->size = strlen(p);
462 spec->should_munmap = 0;
463 spec->is_stdin = 1;
Thomas Gummerer499352c2019-07-11 17:08:51 +0100464 spec->driver = &section_headers;
Johannes Schindelinc8c5e432018-08-13 04:33:07 -0700465
466 return spec;
467}
468
469static void patch_diff(const char *a, const char *b,
Thomas Gummerer1ca69222019-07-11 17:08:45 +0100470 struct diff_options *diffopt)
Johannes Schindelinc8c5e432018-08-13 04:33:07 -0700471{
472 diff_queue(&diff_queued_diff,
473 get_filespec("a", a), get_filespec("b", b));
474
475 diffcore_std(diffopt);
476 diff_flush(diffopt);
477}
478
Jeff King61bdc7c2022-12-13 06:13:48 -0500479static struct strbuf *output_prefix_cb(struct diff_options *opt UNUSED, void *data)
Johannes Schindelin3e6046e2021-02-05 14:46:12 +0000480{
481 return data;
482}
483
Johannes Schindelinc8c5e432018-08-13 04:33:07 -0700484static void output(struct string_list *a, struct string_list *b,
Johannes Schindelin3e6046e2021-02-05 14:46:12 +0000485 struct range_diff_options *range_diff_opts)
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700486{
Johannes Schindelineb0be382018-08-13 04:33:13 -0700487 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
Johannes Schindelind1f87a22018-08-13 04:33:28 -0700488 int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
Johannes Schindelin9dc46e02018-08-13 04:33:05 -0700489 int i = 0, j = 0;
Johannes Schindelin3e6046e2021-02-05 14:46:12 +0000490 struct diff_options opts;
491 struct strbuf indent = STRBUF_INIT;
492
493 if (range_diff_opts->diffopt)
494 memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
495 else
Ævar Arnfjörð Bjarmason08539032023-03-28 15:58:49 +0200496 repo_diff_setup(the_repository, &opts);
Johannes Schindelin3e6046e2021-02-05 14:46:12 +0000497
René Scharfe709b3f32021-09-04 09:50:58 +0200498 opts.no_free = 1;
Johannes Schindelin3e6046e2021-02-05 14:46:12 +0000499 if (!opts.output_format)
500 opts.output_format = DIFF_FORMAT_PATCH;
501 opts.flags.suppress_diff_headers = 1;
502 opts.flags.dual_color_diffed_diffs =
503 range_diff_opts->dual_color;
504 opts.flags.suppress_hunk_header_line_count = 1;
505 opts.output_prefix = output_prefix_cb;
506 strbuf_addstr(&indent, " ");
507 opts.output_prefix_data = &indent;
508 diff_setup_done(&opts);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700509
Johannes Schindelin9dc46e02018-08-13 04:33:05 -0700510 /*
511 * We assume the user is really more interested in the second argument
512 * ("newer" version). To that end, we print the output in the order of
513 * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
514 * commits that are no longer in the RHS into a good place, we place
515 * them once we have shown all of their predecessors in the LHS.
516 */
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700517
Johannes Schindelin9dc46e02018-08-13 04:33:05 -0700518 while (i < a->nr || j < b->nr) {
519 struct patch_util *a_util, *b_util;
520 a_util = i < a->nr ? a->items[i].util : NULL;
521 b_util = j < b->nr ? b->items[j].util : NULL;
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700522
Johannes Schindelin9dc46e02018-08-13 04:33:05 -0700523 /* Skip all the already-shown commits from the LHS. */
524 while (i < a->nr && a_util->shown)
525 a_util = ++i < a->nr ? a->items[i].util : NULL;
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700526
Johannes Schindelin9dc46e02018-08-13 04:33:05 -0700527 /* Show unmatched LHS commit whose predecessors were shown. */
528 if (i < a->nr && a_util->matching < 0) {
Johannes Schindelin1e79f972021-02-05 14:46:13 +0000529 if (!range_diff_opts->right_only)
530 output_pair_header(&opts, patch_no_width,
Johannes Schindelinfaa1df82018-08-13 04:33:18 -0700531 &buf, &dashes, a_util, NULL);
Johannes Schindelin9dc46e02018-08-13 04:33:05 -0700532 i++;
533 continue;
534 }
535
536 /* Show unmatched RHS commits. */
537 while (j < b->nr && b_util->matching < 0) {
Johannes Schindelin1e79f972021-02-05 14:46:13 +0000538 if (!range_diff_opts->left_only)
539 output_pair_header(&opts, patch_no_width,
Johannes Schindelinfaa1df82018-08-13 04:33:18 -0700540 &buf, &dashes, NULL, b_util);
Johannes Schindelin9dc46e02018-08-13 04:33:05 -0700541 b_util = ++j < b->nr ? b->items[j].util : NULL;
542 }
543
544 /* Show matching LHS/RHS pair. */
545 if (j < b->nr) {
546 a_util = a->items[b_util->matching].util;
Johannes Schindelin3e6046e2021-02-05 14:46:12 +0000547 output_pair_header(&opts, patch_no_width,
Johannes Schindelinfaa1df82018-08-13 04:33:18 -0700548 &buf, &dashes, a_util, b_util);
Johannes Schindelin3e6046e2021-02-05 14:46:12 +0000549 if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
Johannes Schindelinc8c5e432018-08-13 04:33:07 -0700550 patch_diff(a->items[b_util->matching].string,
Johannes Schindelin3e6046e2021-02-05 14:46:12 +0000551 b->items[j].string, &opts);
Johannes Schindelin9dc46e02018-08-13 04:33:05 -0700552 a_util->shown = 1;
553 j++;
554 }
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700555 }
Johannes Schindelineb0be382018-08-13 04:33:13 -0700556 strbuf_release(&buf);
557 strbuf_release(&dashes);
Johannes Schindelin3e6046e2021-02-05 14:46:12 +0000558 strbuf_release(&indent);
René Scharfe709b3f32021-09-04 09:50:58 +0200559 opts.no_free = 0;
560 diff_free(&opts);
Eric Sunshine73a834e2018-07-22 05:57:12 -0400561}
562
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700563int show_range_diff(const char *range1, const char *range2,
Johannes Schindelinf1ce6c12021-02-05 14:46:11 +0000564 struct range_diff_options *range_diff_opts)
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700565{
566 int res = 0;
567
568 struct string_list branch1 = STRING_LIST_INIT_DUP;
569 struct string_list branch2 = STRING_LIST_INIT_DUP;
570
Johannes Schindelin1e79f972021-02-05 14:46:13 +0000571 if (range_diff_opts->left_only && range_diff_opts->right_only)
Jean-Noël Avila43ea6352022-01-05 20:02:14 +0000572 res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only");
Johannes Schindelin1e79f972021-02-05 14:46:13 +0000573
574 if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700575 res = error(_("could not parse log for '%s'"), range1);
Johannes Schindelinf1ce6c12021-02-05 14:46:11 +0000576 if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700577 res = error(_("could not parse log for '%s'"), range2);
578
579 if (!res) {
580 find_exact_matches(&branch1, &branch2);
Johannes Schindelinf1ce6c12021-02-05 14:46:11 +0000581 get_correspondences(&branch1, &branch2,
582 range_diff_opts->creation_factor);
Johannes Schindelin3e6046e2021-02-05 14:46:12 +0000583 output(&branch1, &branch2, range_diff_opts);
Johannes Schindelind9c66f02018-08-13 04:33:04 -0700584 }
585
586 string_list_clear(&branch1, 1);
587 string_list_clear(&branch2, 1);
588
589 return res;
590}
Johannes Schindelin679b5912021-01-27 16:37:22 +0000591
592int is_range_diff_range(const char *arg)
593{
Johannes Schindelin359f0d72021-02-05 14:44:48 +0000594 char *copy = xstrdup(arg); /* setup_revisions() modifies it */
595 const char *argv[] = { "", copy, "--", NULL };
596 int i, positive = 0, negative = 0;
597 struct rev_info revs;
598
Ævar Arnfjörð Bjarmason035c7de2023-03-28 15:58:56 +0200599 repo_init_revisions(the_repository, &revs, NULL);
Johannes Schindelin359f0d72021-02-05 14:44:48 +0000600 if (setup_revisions(3, argv, &revs, NULL) == 1) {
601 for (i = 0; i < revs.pending.nr; i++)
602 if (revs.pending.objects[i].item->flags & UNINTERESTING)
603 negative++;
604 else
605 positive++;
606 for (i = 0; i < revs.pending.nr; i++) {
607 struct object *obj = revs.pending.objects[i].item;
608
609 if (obj->type == OBJ_COMMIT)
610 clear_commit_marks((struct commit *)obj,
611 ALL_REV_FLAGS);
612 }
613 }
614
615 free(copy);
Ævar Arnfjörð Bjarmason1878b5e2022-04-13 22:01:35 +0200616 release_revisions(&revs);
Johannes Schindelin359f0d72021-02-05 14:44:48 +0000617 return negative > 0 && positive > 0;
Johannes Schindelin679b5912021-01-27 16:37:22 +0000618}