blob: d6d6fa168942053e756317d8b490435e4d1280b6 [file] [log] [blame]
Elijah Newren5e3f94d2023-04-22 20:17:23 +00001#include "git-compat-util.h"
Elijah Newrena034e912023-05-16 06:34:06 +00002#include "object-store-ll.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08003#include "commit.h"
Elijah Newren73359a92023-04-11 03:00:40 +00004#include "convert.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08005#include "diff.h"
6#include "diffcore.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00007#include "environment.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00008#include "hex.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -07009#include "object-name.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080010#include "quote.h"
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070011#include "xdiff-interface.h"
Antoine Pelissefa04ae02013-03-14 22:03:14 +010012#include "xdiff/xmacros.h"
Linus Torvalds91539832006-04-17 11:59:32 -070013#include "log-tree.h"
Junio C Hamano7dae8b22009-04-29 12:49:52 -070014#include "refs.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +000015#include "tree.h"
Jeff King4d5f3472011-05-23 16:27:34 -040016#include "userdiff.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040017#include "oid-array.h"
Thomas Rast53d00b32013-07-31 22:13:20 +020018#include "revision.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080019
Jeff Kinge09867f2014-08-19 22:14:30 -040020static int compare_paths(const struct combine_diff_path *one,
21 const struct diff_filespec *two)
22{
23 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
24 return strcmp(one->path, two->path);
25
26 return base_name_compare(one->path, strlen(one->path), one->mode,
27 two->path, strlen(two->path), two->mode);
28}
29
Elijah Newrend76ce4f2019-02-07 17:12:46 -080030static int filename_changed(char status)
31{
32 return status == 'R' || status == 'C';
33}
34
35static struct combine_diff_path *intersect_paths(
36 struct combine_diff_path *curr,
37 int n,
38 int num_parent,
39 int combined_all_paths)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080040{
41 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano7b1004b2014-01-28 13:55:59 -080042 struct combine_diff_path *p, **tail = &curr;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080043 int i, j, cmp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080044
45 if (!n) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080046 for (i = 0; i < q->nr; i++) {
47 int len;
48 const char *path;
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070049 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080050 continue;
51 path = q->queue[i]->two->path;
52 len = strlen(path);
Junio C Hamano2454c962006-02-06 12:53:07 -080053 p = xmalloc(combine_diff_path_size(num_parent, len));
Felipe Contreras4b25d092009-05-01 12:06:36 +030054 p->path = (char *) &(p->parent[num_parent]);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080055 memcpy(p->path, path, len);
56 p->path[len] = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080057 p->next = NULL;
Junio C Hamano2454c962006-02-06 12:53:07 -080058 memset(p->parent, 0,
59 sizeof(p->parent[0]) * num_parent);
60
brian m. carlsona0d12c42016-06-24 23:09:23 +000061 oidcpy(&p->oid, &q->queue[i]->two->oid);
Junio C Hamano2454c962006-02-06 12:53:07 -080062 p->mode = q->queue[i]->two->mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +000063 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
Junio C Hamano2454c962006-02-06 12:53:07 -080064 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080065 p->parent[n].status = q->queue[i]->status;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080066
67 if (combined_all_paths &&
68 filename_changed(p->parent[n].status)) {
69 strbuf_init(&p->parent[n].path, 0);
70 strbuf_addstr(&p->parent[n].path,
71 q->queue[i]->one->path);
72 }
Junio C Hamano5290a0f2006-01-25 03:34:10 -080073 *tail = p;
74 tail = &p->next;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080075 }
Junio C Hamano7b1004b2014-01-28 13:55:59 -080076 return curr;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080077 }
78
Kirill Smelkov8518ff82014-01-20 20:20:40 +040079 /*
Junio C Hamano7b1004b2014-01-28 13:55:59 -080080 * paths in curr (linked list) and q->queue[] (array) are
81 * both sorted in the tree order.
Kirill Smelkov8518ff82014-01-20 20:20:40 +040082 */
Kirill Smelkov8518ff82014-01-20 20:20:40 +040083 i = 0;
Junio C Hamano7b1004b2014-01-28 13:55:59 -080084 while ((p = *tail) != NULL) {
85 cmp = ((i >= q->nr)
Jeff Kinge09867f2014-08-19 22:14:30 -040086 ? -1 : compare_paths(p, q->queue[i]->two));
Kirill Smelkov8518ff82014-01-20 20:20:40 +040087
Kirill Smelkov8518ff82014-01-20 20:20:40 +040088 if (cmp < 0) {
Junio C Hamano7b1004b2014-01-28 13:55:59 -080089 /* p->path not in q->queue[]; drop it */
90 *tail = p->next;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080091 for (j = 0; j < num_parent; j++)
92 if (combined_all_paths &&
93 filename_changed(p->parent[j].status))
94 strbuf_release(&p->parent[j].path);
Junio C Hamano7b1004b2014-01-28 13:55:59 -080095 free(p);
Kirill Smelkov8518ff82014-01-20 20:20:40 +040096 continue;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080097 }
Kirill Smelkov8518ff82014-01-20 20:20:40 +040098
99 if (cmp > 0) {
Junio C Hamano7b1004b2014-01-28 13:55:59 -0800100 /* q->queue[i] not in p->path; skip it */
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400101 i++;
102 continue;
103 }
104
brian m. carlsona0d12c42016-06-24 23:09:23 +0000105 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400106 p->parent[n].mode = q->queue[i]->one->mode;
107 p->parent[n].status = q->queue[i]->status;
Elijah Newrend76ce4f2019-02-07 17:12:46 -0800108 if (combined_all_paths &&
109 filename_changed(p->parent[n].status))
110 strbuf_addstr(&p->parent[n].path,
111 q->queue[i]->one->path);
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400112
Junio C Hamano7b1004b2014-01-28 13:55:59 -0800113 tail = &p->next;
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400114 i++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800115 }
116 return curr;
117}
118
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800119/* Lines lost from parent */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800120struct lline {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100121 struct lline *next, *prev;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800122 int len;
123 unsigned long parent_map;
124 char line[FLEX_ARRAY];
125};
126
Antoine Pelisse99d32062013-03-23 18:23:28 +0100127/* Lines lost from current parent (before coalescing) */
128struct plost {
129 struct lline *lost_head, *lost_tail;
130 int len;
131};
132
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800133/* Lines surviving in the merge result */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800134struct sline {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100135 /* Accumulated and coalesced lost lines */
136 struct lline *lost;
137 int lenlost;
138 struct plost plost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800139 char *bol;
140 int len;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800141 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
142 * we did not change it).
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800143 * bit N is used for "interesting" lines, including context.
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700144 * bit (N+1) is used for "do not show deletion before this".
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800145 */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800146 unsigned long flag;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800147 unsigned long *p_lno;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800148};
149
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100150static int match_string_spaces(const char *line1, int len1,
151 const char *line2, int len2,
152 long flags)
153{
154 if (flags & XDF_WHITESPACE_FLAGS) {
155 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
156 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
157 }
158
159 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
160 return (len1 == len2 && !memcmp(line1, line2, len1));
161
162 while (len1 > 0 && len2 > 0) {
163 len1--;
164 len2--;
165 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
166 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
167 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
168 return 0;
169
170 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
171 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
172 }
173 if (line1[len1] != line2[len2])
174 return 0;
175 }
176
177 if (flags & XDF_IGNORE_WHITESPACE) {
178 /* Consume remaining spaces */
179 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
180 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
181 }
182
183 /* We matched full line1 and line2 */
184 if (!len1 && !len2)
185 return 1;
186
187 return 0;
188}
189
Antoine Pelisse99d32062013-03-23 18:23:28 +0100190enum coalesce_direction { MATCH, BASE, NEW };
191
192/* Coalesce new lines into base by finding LCS */
193static struct lline *coalesce_lines(struct lline *base, int *lenbase,
Brandon Williams06fffa02018-02-14 10:59:36 -0800194 struct lline *newline, int lennew,
Antoine Pelisse99d32062013-03-23 18:23:28 +0100195 unsigned long parent, long flags)
196{
197 int **lcs;
198 enum coalesce_direction **directions;
199 struct lline *baseend, *newend = NULL;
200 int i, j, origbaselen = *lenbase;
201
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700202 if (!newline)
Antoine Pelisse99d32062013-03-23 18:23:28 +0100203 return base;
204
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700205 if (!base) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100206 *lenbase = lennew;
Brandon Williams06fffa02018-02-14 10:59:36 -0800207 return newline;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100208 }
209
210 /*
211 * Coalesce new lines into base by finding the LCS
Ondřej Bílka98e023d2013-07-29 10:18:21 +0200212 * - Create the table to run dynamic programming
Antoine Pelisse99d32062013-03-23 18:23:28 +0100213 * - Compute the LCS
214 * - Then reverse read the direction structure:
215 * - If we have MATCH, assign parent to base flag, and consume
216 * both baseend and newend
217 * - Else if we have BASE, consume baseend
218 * - Else if we have NEW, insert newend lline into base and
219 * consume newend
220 */
René Scharfeca56dad2021-03-13 17:17:22 +0100221 CALLOC_ARRAY(lcs, st_add(origbaselen, 1));
222 CALLOC_ARRAY(directions, st_add(origbaselen, 1));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100223 for (i = 0; i < origbaselen + 1; i++) {
René Scharfeca56dad2021-03-13 17:17:22 +0100224 CALLOC_ARRAY(lcs[i], st_add(lennew, 1));
225 CALLOC_ARRAY(directions[i], st_add(lennew, 1));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100226 directions[i][0] = BASE;
227 }
228 for (j = 1; j < lennew + 1; j++)
229 directions[0][j] = NEW;
230
231 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
Brandon Williams06fffa02018-02-14 10:59:36 -0800232 for (j = 1, newend = newline; j < lennew + 1; j++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100233 if (match_string_spaces(baseend->line, baseend->len,
234 newend->line, newend->len, flags)) {
235 lcs[i][j] = lcs[i - 1][j - 1] + 1;
236 directions[i][j] = MATCH;
237 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
238 lcs[i][j] = lcs[i][j - 1];
239 directions[i][j] = NEW;
240 } else {
241 lcs[i][j] = lcs[i - 1][j];
242 directions[i][j] = BASE;
243 }
244 if (newend->next)
245 newend = newend->next;
246 }
247 if (baseend->next)
248 baseend = baseend->next;
249 }
250
251 for (i = 0; i < origbaselen + 1; i++)
252 free(lcs[i]);
253 free(lcs);
254
255 /* At this point, baseend and newend point to the end of each lists */
256 i--;
257 j--;
258 while (i != 0 || j != 0) {
259 if (directions[i][j] == MATCH) {
260 baseend->parent_map |= 1<<parent;
261 baseend = baseend->prev;
262 newend = newend->prev;
263 i--;
264 j--;
265 } else if (directions[i][j] == NEW) {
266 struct lline *lline;
267
268 lline = newend;
269 /* Remove lline from new list and update newend */
270 if (lline->prev)
271 lline->prev->next = lline->next;
272 else
Brandon Williams06fffa02018-02-14 10:59:36 -0800273 newline = lline->next;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100274 if (lline->next)
275 lline->next->prev = lline->prev;
276
277 newend = lline->prev;
278 j--;
279
280 /* Add lline to base list */
281 if (baseend) {
282 lline->next = baseend->next;
283 lline->prev = baseend;
284 if (lline->prev)
285 lline->prev->next = lline;
286 }
287 else {
288 lline->next = base;
289 base = lline;
290 }
291 (*lenbase)++;
292
293 if (lline->next)
294 lline->next->prev = lline;
295
296 } else {
297 baseend = baseend->prev;
298 i--;
299 }
300 }
301
Brandon Williams06fffa02018-02-14 10:59:36 -0800302 newend = newline;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100303 while (newend) {
304 struct lline *lline = newend;
305 newend = newend->next;
306 free(lline);
307 }
308
309 for (i = 0; i < origbaselen + 1; i++)
310 free(directions[i]);
311 free(directions);
312
313 return base;
314}
315
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200316static char *grab_blob(struct repository *r,
317 const struct object_id *oid, unsigned int mode,
Antoine Pelisse99d32062013-03-23 18:23:28 +0100318 unsigned long *size, struct userdiff_driver *textconv,
319 const char *path)
320{
321 char *blob;
322 enum object_type type;
323
324 if (S_ISGITLINK(mode)) {
Jeff King0dc3b032017-03-28 15:46:53 -0400325 struct strbuf buf = STRBUF_INIT;
326 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
327 *size = buf.len;
328 blob = strbuf_detach(&buf, NULL);
brian m. carlson1ff57c12015-03-13 23:39:33 +0000329 } else if (is_null_oid(oid)) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100330 /* deleted blob */
331 *size = 0;
332 return xcalloc(1, 1);
333 } else if (textconv) {
334 struct diff_filespec *df = alloc_filespec(path);
Brandon Williamsf9704c22017-05-30 10:30:50 -0700335 fill_filespec(df, oid, 1, mode);
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200336 *size = fill_textconv(r, textconv, df, &blob);
Antoine Pelisse99d32062013-03-23 18:23:28 +0100337 free_filespec(df);
338 } else {
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +0200339 blob = repo_read_object_file(r, oid, &type, size);
Johannes Schindelin568459b2024-02-05 14:35:53 +0000340 if (!blob)
341 die(_("unable to read %s"), oid_to_hex(oid));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100342 if (type != OBJ_BLOB)
brian m. carlson1ff57c12015-03-13 23:39:33 +0000343 die("object '%s' is not a blob!", oid_to_hex(oid));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100344 }
345 return blob;
346}
347
348static void append_lost(struct sline *sline, int n, const char *line, int len)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800349{
350 struct lline *lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800351 unsigned long this_mask = (1UL<<n);
352 if (line[len-1] == '\n')
353 len--;
354
Jeff King96ffc062016-02-22 17:44:32 -0500355 FLEX_ALLOC_MEM(lline, line, line, len);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800356 lline->len = len;
357 lline->next = NULL;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100358 lline->prev = sline->plost.lost_tail;
359 if (lline->prev)
360 lline->prev->next = lline;
361 else
362 sline->plost.lost_head = lline;
363 sline->plost.lost_tail = lline;
364 sline->plost.len++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800365 lline->parent_map = this_mask;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800366}
367
Junio C Hamanof23fc772006-04-03 18:53:15 -0700368struct combine_diff_state {
Junio C Hamanoa0fd3142006-04-06 22:29:55 -0700369 unsigned int lno;
370 int ob, on, nb, nn;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700371 unsigned long nmask;
372 int num_parent;
373 int n;
374 struct sline *sline;
375 struct sline *lost_bucket;
376};
377
Jeff King0074c912018-11-02 02:38:20 -0400378static void consume_hunk(void *state_,
379 long ob, long on,
380 long nb, long nn,
Jeff King61bdc7c2022-12-13 06:13:48 -0500381 const char *func UNUSED, long funclen UNUSED)
Jeff King0074c912018-11-02 02:38:20 -0400382{
383 struct combine_diff_state *state = state_;
384
385 state->ob = ob;
386 state->on = on;
387 state->nb = nb;
388 state->nn = nn;
389 state->lno = state->nb;
390 if (state->nn == 0) {
391 /* @@ -X,Y +N,0 @@ removed Y lines
392 * that would have come *after* line N
393 * in the result. Our lost buckets hang
394 * to the line after the removed lines,
395 *
396 * Note that this is correct even when N == 0,
397 * in which case the hunk removes the first
398 * line in the file.
399 */
400 state->lost_bucket = &state->sline[state->nb];
401 if (!state->nb)
402 state->nb = 1;
403 } else {
404 state->lost_bucket = &state->sline[state->nb-1];
405 }
406 if (!state->sline[state->nb-1].p_lno)
René Scharfeca56dad2021-03-13 17:17:22 +0100407 CALLOC_ARRAY(state->sline[state->nb - 1].p_lno,
408 state->num_parent);
Jeff King0074c912018-11-02 02:38:20 -0400409 state->sline[state->nb-1].p_lno[state->n] = state->ob;
410}
411
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200412static int consume_line(void *state_, char *line, unsigned long len)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700413{
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700414 struct combine_diff_state *state = state_;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700415 if (!state->lost_bucket)
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200416 return 0; /* not in any hunk yet */
Junio C Hamanof23fc772006-04-03 18:53:15 -0700417 switch (line[0]) {
418 case '-':
Antoine Pelisse99d32062013-03-23 18:23:28 +0100419 append_lost(state->lost_bucket, state->n, line+1, len-1);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700420 break;
421 case '+':
422 state->sline[state->lno-1].flag |= state->nmask;
423 state->lno++;
424 break;
425 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200426 return 0;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700427}
428
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200429static void combine_diff(struct repository *r,
430 const struct object_id *parent, unsigned int mode,
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700431 mmfile_t *result_file,
Junio C Hamano2386c292006-06-28 01:38:19 -0700432 struct sline *sline, unsigned int cnt, int n,
Jeff King0508fe52011-05-23 16:31:05 -0400433 int num_parent, int result_deleted,
434 struct userdiff_driver *textconv,
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100435 const char *path, long flags)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800436{
Junio C Hamanof23fc772006-04-03 18:53:15 -0700437 unsigned int p_lno, lno;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800438 unsigned long nmask = (1UL << n);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700439 xpparam_t xpp;
440 xdemitconf_t xecfg;
441 mmfile_t parent_file;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700442 struct combine_diff_state state;
443 unsigned long sz;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800444
Thomas Rast21798702010-04-15 14:59:37 +0200445 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800446 return; /* result deleted */
447
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200448 parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700449 parent_file.size = sz;
Brian Downing9ccd0a82008-10-25 15:30:37 +0200450 memset(&xpp, 0, sizeof(xpp));
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100451 xpp.flags = flags;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100452 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700453 memset(&state, 0, sizeof(state));
454 state.nmask = nmask;
455 state.sline = sline;
456 state.lno = 1;
457 state.num_parent = num_parent;
458 state.n = n;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800459
Jeff King0074c912018-11-02 02:38:20 -0400460 if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
461 consume_line, &state, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -0400462 die("unable to generate combined diff for %s",
Junio C Hamano11a458b2015-09-28 15:33:56 -0700463 oid_to_hex(parent));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700464 free(parent_file.ptr);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800465
466 /* Assign line numbers for this parent.
467 *
468 * sline[lno].p_lno[n] records the first line number
469 * (counting from 1) for parent N if the final hunk display
470 * started by showing sline[lno] (possibly showing the lost
471 * lines attached to it first).
472 */
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700473 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
Junio C Hamanof16706c2006-01-30 22:33:15 -0800474 struct lline *ll;
475 sline[lno].p_lno[n] = p_lno;
476
Antoine Pelisse99d32062013-03-23 18:23:28 +0100477 /* Coalesce new lines */
478 if (sline[lno].plost.lost_head) {
479 struct sline *sl = &sline[lno];
480 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
481 sl->plost.lost_head,
482 sl->plost.len, n, flags);
483 sl->plost.lost_head = sl->plost.lost_tail = NULL;
484 sl->plost.len = 0;
485 }
486
Junio C Hamanof16706c2006-01-30 22:33:15 -0800487 /* How many lines would this sline advance the p_lno? */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100488 ll = sline[lno].lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800489 while (ll) {
490 if (ll->parent_map & nmask)
491 p_lno++; /* '-' means parent had it */
492 ll = ll->next;
493 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700494 if (lno < cnt && !(sline[lno].flag & nmask))
Junio C Hamanof16706c2006-01-30 22:33:15 -0800495 p_lno++; /* no '+' means parent had it */
496 }
497 sline[lno].p_lno[n] = p_lno; /* trailer */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800498}
499
500static unsigned long context = 3;
501static char combine_marker = '@';
502
503static int interesting(struct sline *sline, unsigned long all_mask)
504{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800505 /* If some parents lost lines here, or if we have added to
506 * some parent, it is interesting.
507 */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100508 return ((sline->flag & all_mask) || sline->lost);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800509}
510
Junio C Hamano3ec19092006-01-26 03:53:01 -0800511static unsigned long adjust_hunk_tail(struct sline *sline,
512 unsigned long all_mask,
513 unsigned long hunk_begin,
514 unsigned long i)
515{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800516 /* i points at the first uninteresting line. If the last line
517 * of the hunk was interesting only because it has some
518 * deletion, then it is not all that interesting for the
519 * purpose of giving trailing context lines. This is because
520 * we output '-' line and then unmodified sline[i-1] itself in
521 * that case which gives us one extra context line.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800522 */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800523 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800524 i--;
525 return i;
526}
527
Junio C Hamano46dc9412006-02-02 15:17:42 -0800528static unsigned long find_next(struct sline *sline,
529 unsigned long mark,
530 unsigned long i,
531 unsigned long cnt,
Junio C Hamano2386c292006-06-28 01:38:19 -0700532 int look_for_uninteresting)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800533{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800534 /* We have examined up to i-1 and are about to look at i.
535 * Find next interesting or uninteresting line. Here,
536 * "interesting" does not mean interesting(), but marked by
537 * the give_context() function below (i.e. it includes context
538 * lines that are not interesting to interesting() function
539 * that are surrounded by interesting() ones.
540 */
Junio C Hamano74065952006-04-11 14:31:31 -0700541 while (i <= cnt)
Junio C Hamano2386c292006-06-28 01:38:19 -0700542 if (look_for_uninteresting
Junio C Hamano46dc9412006-02-02 15:17:42 -0800543 ? !(sline[i].flag & mark)
544 : (sline[i].flag & mark))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800545 return i;
546 else
547 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700548 return i;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800549}
550
551static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
552{
553 unsigned long all_mask = (1UL<<num_parent) - 1;
554 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700555 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800556 unsigned long i;
557
Junio C Hamano46dc9412006-02-02 15:17:42 -0800558 /* Two groups of interesting lines may have a short gap of
Pavel Roskin82e5a822006-07-10 01:50:18 -0400559 * uninteresting lines. Connect such groups to give them a
Junio C Hamano46dc9412006-02-02 15:17:42 -0800560 * bit of context.
561 *
562 * We first start from what the interesting() function says,
563 * and mark them with "mark", and paint context lines with the
564 * mark. So interesting() would still say false for such context
565 * lines but they are treated as "interesting" in the end.
566 */
567 i = find_next(sline, mark, 0, cnt, 0);
Junio C Hamano74065952006-04-11 14:31:31 -0700568 if (cnt < i)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800569 return 0;
570
Junio C Hamano74065952006-04-11 14:31:31 -0700571 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800572 unsigned long j = (context < i) ? (i - context) : 0;
573 unsigned long k;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800574
575 /* Paint a few lines before the first interesting line. */
Matthijs Kooijmanaac38572013-05-15 19:42:14 +0200576 while (j < i) {
577 if (!(sline[j].flag & mark))
578 sline[j].flag |= no_pre_delete;
579 sline[j++].flag |= mark;
580 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800581
582 again:
Junio C Hamano46dc9412006-02-02 15:17:42 -0800583 /* we know up to i is to be included. where does the
584 * next uninteresting one start?
585 */
586 j = find_next(sline, mark, i, cnt, 1);
Junio C Hamano74065952006-04-11 14:31:31 -0700587 if (cnt < j)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800588 break; /* the rest are all interesting */
589
590 /* lookahead context lines */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800591 k = find_next(sline, mark, j, cnt, 0);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800592 j = adjust_hunk_tail(sline, all_mask, i, j);
593
594 if (k < j + context) {
595 /* k is interesting and [j,k) are not, but
596 * paint them interesting because the gap is small.
597 */
598 while (j < k)
599 sline[j++].flag |= mark;
600 i = k;
601 goto again;
602 }
603
604 /* j is the first uninteresting line and there is
Junio C Hamano46dc9412006-02-02 15:17:42 -0800605 * no overlap beyond it within context lines. Paint
606 * the trailing edge a bit.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800607 */
608 i = k;
Junio C Hamano74065952006-04-11 14:31:31 -0700609 k = (j + context < cnt+1) ? j + context : cnt+1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800610 while (j < k)
611 sline[j++].flag |= mark;
612 }
613 return 1;
614}
615
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800616static int make_hunks(struct sline *sline, unsigned long cnt,
Junio C Hamanod8f47902006-01-24 01:22:04 -0800617 int num_parent, int dense)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800618{
619 unsigned long all_mask = (1UL<<num_parent) - 1;
620 unsigned long mark = (1UL<<num_parent);
621 unsigned long i;
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800622 int has_interesting = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800623
Junio C Hamano74065952006-04-11 14:31:31 -0700624 for (i = 0; i <= cnt; i++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800625 if (interesting(&sline[i], all_mask))
626 sline[i].flag |= mark;
627 else
628 sline[i].flag &= ~mark;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800629 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800630 if (!dense)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800631 return give_context(sline, cnt, num_parent);
Junio C Hamanod8f47902006-01-24 01:22:04 -0800632
Junio C Hamano263eee22006-01-25 13:11:38 -0800633 /* Look at each hunk, and if we have changes from only one
634 * parent, or the changes are the same from all but one
635 * parent, mark that uninteresting.
Junio C Hamanod8f47902006-01-24 01:22:04 -0800636 */
637 i = 0;
Junio C Hamano74065952006-04-11 14:31:31 -0700638 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800639 unsigned long j, hunk_begin, hunk_end;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800640 unsigned long same_diff;
Junio C Hamano74065952006-04-11 14:31:31 -0700641 while (i <= cnt && !(sline[i].flag & mark))
Junio C Hamanod8f47902006-01-24 01:22:04 -0800642 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700643 if (cnt < i)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800644 break; /* No more interesting hunks */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800645 hunk_begin = i;
Junio C Hamano74065952006-04-11 14:31:31 -0700646 for (j = i + 1; j <= cnt; j++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800647 if (!(sline[j].flag & mark)) {
648 /* Look beyond the end to see if there
649 * is an interesting line after this
650 * hunk within context span.
651 */
652 unsigned long la; /* lookahead */
653 int contin = 0;
654 la = adjust_hunk_tail(sline, all_mask,
655 hunk_begin, j);
Junio C Hamano74065952006-04-11 14:31:31 -0700656 la = (la + context < cnt + 1) ?
657 (la + context) : cnt + 1;
René Scharfee5e9b562012-03-24 16:18:46 +0100658 while (la && j <= --la) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800659 if (sline[la].flag & mark) {
660 contin = 1;
661 break;
662 }
663 }
664 if (!contin)
665 break;
666 j = la;
667 }
668 }
669 hunk_end = j;
670
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800671 /* [i..hunk_end) are interesting. Now is it really
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800672 * interesting? We check if there are only two versions
673 * and the result matches one of them. That is, we look
674 * at:
675 * (+) line, which records lines added to which parents;
676 * this line appears in the result.
677 * (-) line, which records from what parents the line
678 * was removed; this line does not appear in the result.
679 * then check the set of parents the result has difference
680 * from, from all lines. If there are lines that has
681 * different set of parents that the result has differences
682 * from, that means we have more than two versions.
683 *
684 * Even when we have only two versions, if the result does
685 * not match any of the parents, the it should be considered
686 * interesting. In such a case, we would have all '+' line.
687 * After passing the above "two versions" test, that would
688 * appear as "the same set of parents" to be "all parents".
Junio C Hamanod8f47902006-01-24 01:22:04 -0800689 */
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800690 same_diff = 0;
691 has_interesting = 0;
692 for (j = i; j < hunk_end && !has_interesting; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800693 unsigned long this_diff = sline[j].flag & all_mask;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100694 struct lline *ll = sline[j].lost;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800695 if (this_diff) {
696 /* This has some changes. Is it the
697 * same as others?
698 */
699 if (!same_diff)
700 same_diff = this_diff;
701 else if (same_diff != this_diff) {
702 has_interesting = 1;
703 break;
704 }
705 }
706 while (ll && !has_interesting) {
707 /* Lost this line from these parents;
708 * who are they? Are they the same?
709 */
710 this_diff = ll->parent_map;
711 if (!same_diff)
712 same_diff = this_diff;
713 else if (same_diff != this_diff) {
714 has_interesting = 1;
715 }
716 ll = ll->next;
717 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800718 }
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800719
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800720 if (!has_interesting && same_diff != all_mask) {
Junio C Hamanod8f47902006-01-24 01:22:04 -0800721 /* This hunk is not that interesting after all */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800722 for (j = hunk_begin; j < hunk_end; j++)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800723 sline[j].flag &= ~mark;
724 }
725 i = hunk_end;
726 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800727
728 has_interesting = give_context(sline, cnt, num_parent);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800729 return has_interesting;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800730}
731
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800732static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
Junio C Hamanof16706c2006-01-30 22:33:15 -0800733{
734 l0 = sline[l0].p_lno[n];
735 l1 = sline[l1].p_lno[n];
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800736 printf(" -%lu,%lu", l0, l1-l0-null_context);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800737}
738
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700739static int hunk_comment_line(const char *bol)
740{
Junio C Hamano7a8ac592006-10-26 02:05:05 -0700741 int ch;
742
743 if (!bol)
744 return 0;
745 ch = *bol & 0xff;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700746 return (isalpha(ch) || ch == '_' || ch == '$');
747}
748
Junio C Hamano39280972008-08-27 19:48:01 -0700749static void show_line_to_eol(const char *line, int len, const char *reset)
750{
751 int saw_cr_at_eol = 0;
752 if (len < 0)
753 len = strlen(line);
754 saw_cr_at_eol = (len && line[len-1] == '\r');
755
756 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
757 reset,
758 saw_cr_at_eol ? "\r" : "");
759}
760
John Keeping41ee2ad2013-02-07 20:15:28 +0000761static void dump_sline(struct sline *sline, const char *line_prefix,
762 unsigned long cnt, int num_parent,
Thomas Rast21798702010-04-15 14:59:37 +0200763 int use_color, int result_deleted)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800764{
765 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700766 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800767 int i;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800768 unsigned long lno = 0;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700769 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100770 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700771 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
772 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
Jeff King8dbf3eb2015-05-27 16:48:46 -0400773 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700774 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800775
Thomas Rast21798702010-04-15 14:59:37 +0200776 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800777 return; /* result deleted */
778
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800779 while (1) {
Junio C Hamano8bc75742006-04-12 13:23:50 -0700780 unsigned long hunk_end;
781 unsigned long rlines;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700782 const char *hunk_comment = NULL;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800783 unsigned long null_context = 0;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700784
785 while (lno <= cnt && !(sline[lno].flag & mark)) {
786 if (hunk_comment_line(sline[lno].bol))
787 hunk_comment = sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800788 lno++;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700789 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700790 if (cnt < lno)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800791 break;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700792 else {
Junio C Hamano74065952006-04-11 14:31:31 -0700793 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700794 if (!(sline[hunk_end].flag & mark))
795 break;
796 }
Junio C Hamano74065952006-04-11 14:31:31 -0700797 rlines = hunk_end - lno;
798 if (cnt < hunk_end)
799 rlines--; /* pointing at the last delete hunk */
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800800
801 if (!context) {
802 /*
803 * Even when running with --unified=0, all
804 * lines in the hunk needs to be processed in
805 * the loop below in order to show the
806 * deletion recorded in lost_head. However,
807 * we do not want to show the resulting line
808 * with all blank context markers in such a
809 * case. Compensate.
810 */
811 unsigned long j;
812 for (j = lno; j < hunk_end; j++)
813 if (!(sline[j].flag & (mark-1)))
814 null_context++;
815 rlines -= null_context;
816 }
817
John Keeping41ee2ad2013-02-07 20:15:28 +0000818 printf("%s%s", line_prefix, c_frag);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800819 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800820 for (i = 0; i < num_parent; i++)
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800821 show_parent_lno(sline, lno, hunk_end, i, null_context);
Junio C Hamano74065952006-04-11 14:31:31 -0700822 printf(" +%lu,%lu ", lno+1, rlines);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800823 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700824
825 if (hunk_comment) {
826 int comment_end = 0;
827 for (i = 0; i < 40; i++) {
828 int ch = hunk_comment[i] & 0xff;
829 if (!ch || ch == '\n')
830 break;
831 if (!isspace(ch))
832 comment_end = i;
833 }
834 if (comment_end)
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100835 printf("%s%s %s%s", c_reset,
Jeff King8dbf3eb2015-05-27 16:48:46 -0400836 c_context, c_reset,
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100837 c_func);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700838 for (i = 0; i < comment_end; i++)
839 putchar(hunk_comment[i]);
840 }
841
Junio C Hamano567a03d2006-08-10 00:30:33 -0700842 printf("%s\n", c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800843 while (lno < hunk_end) {
844 struct lline *ll;
845 int j;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800846 unsigned long p_mask;
Benjamin Kramerfd13b212009-03-07 21:02:26 +0100847 struct sline *sl = &sline[lno++];
Antoine Pelisse99d32062013-03-23 18:23:28 +0100848 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800849 while (ll) {
John Keeping41ee2ad2013-02-07 20:15:28 +0000850 printf("%s%s", line_prefix, c_old);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800851 for (j = 0; j < num_parent; j++) {
852 if (ll->parent_map & (1UL<<j))
853 putchar('-');
854 else
855 putchar(' ');
856 }
Junio C Hamano39280972008-08-27 19:48:01 -0700857 show_line_to_eol(ll->line, -1, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800858 ll = ll->next;
859 }
Junio C Hamano74065952006-04-11 14:31:31 -0700860 if (cnt < lno)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700861 break;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800862 p_mask = 1;
John Keeping41ee2ad2013-02-07 20:15:28 +0000863 fputs(line_prefix, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800864 if (!(sl->flag & (mark-1))) {
865 /*
866 * This sline was here to hang the
867 * lost lines in front of it.
868 */
869 if (!context)
870 continue;
Jeff King8dbf3eb2015-05-27 16:48:46 -0400871 fputs(c_context, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800872 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700873 else
874 fputs(c_new, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800875 for (j = 0; j < num_parent; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800876 if (p_mask & sl->flag)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800877 putchar('+');
Junio C Hamano46dc9412006-02-02 15:17:42 -0800878 else
879 putchar(' ');
880 p_mask <<= 1;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800881 }
Junio C Hamano39280972008-08-27 19:48:01 -0700882 show_line_to_eol(sl->bol, sl->len, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800883 }
884 }
885}
886
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800887static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
888 int i, int j)
889{
890 /* We have already examined parent j and we know parent i
891 * and parent j are the same, so reuse the combined result
892 * of parent j for parent i.
893 */
894 unsigned long lno, imask, jmask;
895 imask = (1UL<<i);
896 jmask = (1UL<<j);
897
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700898 for (lno = 0; lno <= cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100899 struct lline *ll = sline->lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800900 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800901 while (ll) {
902 if (ll->parent_map & jmask)
903 ll->parent_map |= imask;
904 ll = ll->next;
905 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800906 if (sline->flag & jmask)
907 sline->flag |= imask;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800908 sline++;
909 }
Junio C Hamano44627312006-02-06 18:54:08 -0800910 /* the overall size of the file (sline[cnt]) */
911 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800912}
913
Junio C Hamano462a15b2007-12-26 16:51:19 -0800914static void dump_quoted_path(const char *head,
915 const char *prefix,
916 const char *path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000917 const char *line_prefix,
Junio C Hamano567a03d2006-08-10 00:30:33 -0700918 const char *c_meta, const char *c_reset)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800919{
Junio C Hamano462a15b2007-12-26 16:51:19 -0800920 static struct strbuf buf = STRBUF_INIT;
921
922 strbuf_reset(&buf);
John Keeping41ee2ad2013-02-07 20:15:28 +0000923 strbuf_addstr(&buf, line_prefix);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800924 strbuf_addstr(&buf, c_meta);
925 strbuf_addstr(&buf, head);
Junio C Hamanod5625092007-12-26 17:13:36 -0800926 quote_two_c_style(&buf, prefix, path, 0);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800927 strbuf_addstr(&buf, c_reset);
928 puts(buf.buf);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700929}
930
Jeff King7c978a02011-05-23 16:16:41 -0400931static void show_combined_header(struct combine_diff_path *elem,
932 int num_parent,
Jeff King7c978a02011-05-23 16:16:41 -0400933 struct rev_info *rev,
John Keeping41ee2ad2013-02-07 20:15:28 +0000934 const char *line_prefix,
Jeff King4d5f3472011-05-23 16:27:34 -0400935 int mode_differs,
936 int show_file_header)
Jeff King7c978a02011-05-23 16:16:41 -0400937{
938 struct diff_options *opt = &rev->diffopt;
brian m. carlson976ff7e2019-08-18 20:04:12 +0000939 int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV;
Jeff King7c978a02011-05-23 16:16:41 -0400940 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
941 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
Jeff Kingf1c96262011-08-17 22:03:12 -0700942 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
943 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
Jeff King7c978a02011-05-23 16:16:41 -0400944 const char *abb;
945 int added = 0;
946 int deleted = 0;
947 int i;
Sergey Organovd01141d2020-09-29 14:31:22 +0300948 int dense = rev->dense_combined_merges;
Jeff King7c978a02011-05-23 16:16:41 -0400949
950 if (rev->loginfo && !rev->no_commit_id)
951 show_log(rev);
952
953 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
John Keeping41ee2ad2013-02-07 20:15:28 +0000954 "", elem->path, line_prefix, c_meta, c_reset);
955 printf("%s%sindex ", line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400956 for (i = 0; i < num_parent; i++) {
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200957 abb = repo_find_unique_abbrev(the_repository,
958 &elem->parent[i].oid, abbrev);
Jeff King7c978a02011-05-23 16:16:41 -0400959 printf("%s%s", i ? "," : "", abb);
960 }
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200961 abb = repo_find_unique_abbrev(the_repository, &elem->oid, abbrev);
Jeff King7c978a02011-05-23 16:16:41 -0400962 printf("..%s%s\n", abb, c_reset);
963
964 if (mode_differs) {
965 deleted = !elem->mode;
966
967 /* We say it was added if nobody had it */
968 added = !deleted;
969 for (i = 0; added && i < num_parent; i++)
970 if (elem->parent[i].status !=
971 DIFF_STATUS_ADDED)
972 added = 0;
973 if (added)
John Keeping41ee2ad2013-02-07 20:15:28 +0000974 printf("%s%snew file mode %06o",
975 line_prefix, c_meta, elem->mode);
Jeff King7c978a02011-05-23 16:16:41 -0400976 else {
977 if (deleted)
John Keeping41ee2ad2013-02-07 20:15:28 +0000978 printf("%s%sdeleted file ",
979 line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400980 printf("mode ");
981 for (i = 0; i < num_parent; i++) {
982 printf("%s%06o", i ? "," : "",
983 elem->parent[i].mode);
984 }
985 if (elem->mode)
986 printf("..%06o", elem->mode);
987 }
988 printf("%s\n", c_reset);
989 }
990
Jeff King4d5f3472011-05-23 16:27:34 -0400991 if (!show_file_header)
992 return;
993
Elijah Newrend76ce4f2019-02-07 17:12:46 -0800994 if (rev->combined_all_paths) {
995 for (i = 0; i < num_parent; i++) {
996 char *path = filename_changed(elem->parent[i].status)
997 ? elem->parent[i].path.buf : elem->path;
998 if (elem->parent[i].status == DIFF_STATUS_ADDED)
999 dump_quoted_path("--- ", "", "/dev/null",
1000 line_prefix, c_meta, c_reset);
1001 else
1002 dump_quoted_path("--- ", a_prefix, path,
1003 line_prefix, c_meta, c_reset);
1004 }
1005 } else {
1006 if (added)
1007 dump_quoted_path("--- ", "", "/dev/null",
1008 line_prefix, c_meta, c_reset);
1009 else
1010 dump_quoted_path("--- ", a_prefix, elem->path,
1011 line_prefix, c_meta, c_reset);
1012 }
Jeff King7c978a02011-05-23 16:16:41 -04001013 if (deleted)
1014 dump_quoted_path("+++ ", "", "/dev/null",
John Keeping41ee2ad2013-02-07 20:15:28 +00001015 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -04001016 else
1017 dump_quoted_path("+++ ", b_prefix, elem->path,
John Keeping41ee2ad2013-02-07 20:15:28 +00001018 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -04001019}
1020
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001021static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
Sergey Organovd01141d2020-09-29 14:31:22 +03001022 int working_tree_file,
Junio C Hamano99694542011-08-04 11:39:10 -07001023 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001024{
Linus Torvalds91539832006-04-17 11:59:32 -07001025 struct diff_options *opt = &rev->diffopt;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001026 unsigned long result_size, cnt, lno;
Thomas Rast21798702010-04-15 14:59:37 +02001027 int result_deleted = 0;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -05001028 char *result, *cp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001029 struct sline *sline; /* survived lines */
Junio C Hamano2454c962006-02-06 12:53:07 -08001030 int mode_differs = 0;
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001031 int i, show_hunks;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001032 mmfile_t result_file;
Jeff King4d5f3472011-05-23 16:27:34 -04001033 struct userdiff_driver *userdiff;
Jeff King0508fe52011-05-23 16:31:05 -04001034 struct userdiff_driver *textconv = NULL;
Jeff King4d5f3472011-05-23 16:27:34 -04001035 int is_binary;
John Keeping41ee2ad2013-02-07 20:15:28 +00001036 const char *line_prefix = diff_line_prefix(opt);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001037
Linus Torvaldsee1e5412006-05-13 13:23:48 -07001038 context = opt->context;
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02001039 userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
Jeff King4d5f3472011-05-23 16:27:34 -04001040 if (!userdiff)
1041 userdiff = userdiff_find_by_name("default");
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001042 if (opt->flags.allow_textconv)
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01001043 textconv = userdiff_get_textconv(opt->repo, userdiff);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001044
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001045 /* Read the result of merge first */
Junio C Hamanof23fc772006-04-03 18:53:15 -07001046 if (!working_tree_file)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001047 result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size,
Jeff King0508fe52011-05-23 16:31:05 -04001048 textconv, elem->path);
Junio C Hamanoea726d02006-01-28 00:03:38 -08001049 else {
Junio C Hamano9843a1f2006-02-06 12:30:00 -08001050 /* Used by diff-tree to read from the working tree */
Junio C Hamanoea726d02006-01-28 00:03:38 -08001051 struct stat st;
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001052 int fd = -1;
1053
1054 if (lstat(elem->path, &st) < 0)
1055 goto deleted_file;
1056
1057 if (S_ISLNK(st.st_mode)) {
Junio C Hamano912342d2008-12-17 12:37:58 -08001058 struct strbuf buf = STRBUF_INIT;
1059
1060 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
Nguyễn Thái Ngọc Duy4b94ec92016-05-08 16:47:36 +07001061 error_errno("readlink(%s)", elem->path);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001062 return;
1063 }
Junio C Hamano912342d2008-12-17 12:37:58 -08001064 result_size = buf.len;
1065 result = strbuf_detach(&buf, NULL);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001066 elem->mode = canon_mode(st.st_mode);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001067 } else if (S_ISDIR(st.st_mode)) {
brian m. carlson1ff57c12015-03-13 23:39:33 +00001068 struct object_id oid;
brian m. carlsona98e6102017-10-15 22:07:07 +00001069 if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001070 result = grab_blob(opt->repo, &elem->oid,
1071 elem->mode, &result_size,
1072 NULL, NULL);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001073 else
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001074 result = grab_blob(opt->repo, &oid, elem->mode,
Jeff King0508fe52011-05-23 16:31:05 -04001075 &result_size, NULL, NULL);
1076 } else if (textconv) {
1077 struct diff_filespec *df = alloc_filespec(elem->path);
brian m. carlson14228442021-04-26 01:02:56 +00001078 fill_filespec(df, null_oid(), 0, st.st_mode);
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001079 result_size = fill_textconv(opt->repo, textconv, df, &result);
Jeff King0508fe52011-05-23 16:31:05 -04001080 free_filespec(df);
Kjetil Barvik91fcbcb2009-02-09 21:54:52 +01001081 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001082 size_t len = xsize_t(st.st_size);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001083 ssize_t done;
Johannes Sixta249a9b2007-03-03 20:38:00 +01001084 int is_file, i;
Junio C Hamanoea726d02006-01-28 00:03:38 -08001085
Junio C Hamano1b0c7172006-03-29 22:55:43 -08001086 elem->mode = canon_mode(st.st_mode);
Johannes Sixta249a9b2007-03-03 20:38:00 +01001087 /* if symlinks don't work, assume symlink if all parents
1088 * are symlinks
1089 */
1090 is_file = has_symlinks;
1091 for (i = 0; !is_file && i < num_parent; i++)
1092 is_file = !S_ISLNK(elem->parent[i].mode);
1093 if (!is_file)
1094 elem->mode = canon_mode(S_IFLNK);
1095
Junio C Hamanof23fc772006-04-03 18:53:15 -07001096 result_size = len;
Jeff King3733e692016-02-22 17:44:28 -05001097 result = xmallocz(len);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001098
1099 done = read_in_full(fd, result, len);
1100 if (done < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02001101 die_errno("read error '%s'", elem->path);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001102 else if (done < len)
1103 die("early EOF '%s'", elem->path);
1104
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001105 /* If not a fake symlink, apply filters, e.g. autocrlf */
1106 if (is_file) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001107 struct strbuf buf = STRBUF_INIT;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001108
Nguyễn Thái Ngọc Duy0734f202018-09-21 17:57:20 +02001109 if (convert_to_git(rev->diffopt.repo->index,
1110 elem->path, result, len, &buf, global_conv_flags_eol)) {
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001111 free(result);
1112 result = strbuf_detach(&buf, &len);
1113 result_size = len;
1114 }
1115 }
Junio C Hamanoea726d02006-01-28 00:03:38 -08001116 }
1117 else {
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001118 deleted_file:
Thomas Rast21798702010-04-15 14:59:37 +02001119 result_deleted = 1;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001120 result_size = 0;
Junio C Hamano713a11f2006-02-13 23:07:04 -08001121 elem->mode = 0;
Peter Eriksen28f75812006-07-25 09:30:18 +02001122 result = xcalloc(1, 1);
Junio C Hamanoea726d02006-01-28 00:03:38 -08001123 }
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001124
Junio C Hamanoea726d02006-01-28 00:03:38 -08001125 if (0 <= fd)
1126 close(fd);
1127 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001128
Jeff Kingc95b99b2011-05-23 16:16:59 -04001129 for (i = 0; i < num_parent; i++) {
1130 if (elem->parent[i].mode != elem->mode) {
1131 mode_differs = 1;
1132 break;
1133 }
1134 }
1135
Jeff King0508fe52011-05-23 16:31:05 -04001136 if (textconv)
1137 is_binary = 0;
1138 else if (userdiff->binary != -1)
Jeff King4d5f3472011-05-23 16:27:34 -04001139 is_binary = userdiff->binary;
1140 else {
1141 is_binary = buffer_is_binary(result, result_size);
1142 for (i = 0; !is_binary && i < num_parent; i++) {
1143 char *buf;
1144 unsigned long size;
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001145 buf = grab_blob(opt->repo,
1146 &elem->parent[i].oid,
Jeff King4d5f3472011-05-23 16:27:34 -04001147 elem->parent[i].mode,
Jeff King0508fe52011-05-23 16:31:05 -04001148 &size, NULL, NULL);
Jeff King4d5f3472011-05-23 16:27:34 -04001149 if (buffer_is_binary(buf, size))
1150 is_binary = 1;
1151 free(buf);
1152 }
1153 }
1154 if (is_binary) {
Sergey Organovd01141d2020-09-29 14:31:22 +03001155 show_combined_header(elem, num_parent, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001156 line_prefix, mode_differs, 0);
Jeff King4d5f3472011-05-23 16:27:34 -04001157 printf("Binary files differ\n");
1158 free(result);
1159 return;
1160 }
1161
Junio C Hamano2386c292006-06-28 01:38:19 -07001162 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001163 if (*cp == '\n')
1164 cnt++;
1165 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001166 if (result_size && result[result_size-1] != '\n')
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001167 cnt++; /* incomplete line */
1168
René Scharfeca56dad2021-03-13 17:17:22 +01001169 CALLOC_ARRAY(sline, st_add(cnt, 2));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001170 sline[0].bol = result;
Junio C Hamano2386c292006-06-28 01:38:19 -07001171 for (lno = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001172 if (*cp == '\n') {
1173 sline[lno].len = cp - sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001174 lno++;
1175 if (lno < cnt)
1176 sline[lno].bol = cp + 1;
1177 }
1178 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001179 if (result_size && result[result_size-1] != '\n')
1180 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1181
1182 result_file.ptr = result;
1183 result_file.size = result_size;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001184
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001185 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1186 * for deletion hunk at the end.
1187 */
René Scharfeca56dad2021-03-13 17:17:22 +01001188 CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001189 for (lno = 0; lno <= cnt; lno++)
Junio C Hamanof16706c2006-01-30 22:33:15 -08001190 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1191
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001192 for (i = 0; i < num_parent; i++) {
1193 int j;
1194 for (j = 0; j < i; j++) {
Jeff King4a7e27e2018-08-28 17:22:40 -04001195 if (oideq(&elem->parent[i].oid,
1196 &elem->parent[j].oid)) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001197 reuse_combine_diff(sline, cnt, i, j);
1198 break;
1199 }
1200 }
1201 if (i <= j)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001202 combine_diff(opt->repo,
1203 &elem->parent[i].oid,
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001204 elem->parent[i].mode,
1205 &result_file, sline,
Jeff King0508fe52011-05-23 16:31:05 -04001206 cnt, i, num_parent, result_deleted,
Antoine Pelissefa04ae02013-03-14 22:03:14 +01001207 textconv, elem->path, opt->xdl_opts);
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001208 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001209
Sergey Organovd01141d2020-09-29 14:31:22 +03001210 show_hunks = make_hunks(sline, cnt, num_parent, rev->dense_combined_merges);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001211
Junio C Hamano713a11f2006-02-13 23:07:04 -08001212 if (show_hunks || mode_differs || working_tree_file) {
Sergey Organovd01141d2020-09-29 14:31:22 +03001213 show_combined_header(elem, num_parent, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001214 line_prefix, mode_differs, 1);
1215 dump_sline(sline, line_prefix, cnt, num_parent,
Jeff Kingf1c96262011-08-17 22:03:12 -07001216 opt->use_color, result_deleted);
Junio C Hamano8828cdc2006-01-25 14:26:22 -08001217 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001218 free(result);
1219
Junio C Hamano2386c292006-06-28 01:38:19 -07001220 for (lno = 0; lno < cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +01001221 if (sline[lno].lost) {
1222 struct lline *ll = sline[lno].lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001223 while (ll) {
1224 struct lline *tmp = ll;
1225 ll = ll->next;
1226 free(tmp);
1227 }
1228 }
1229 }
Junio C Hamano46dc9412006-02-02 15:17:42 -08001230 free(sline[0].p_lno);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001231 free(sline);
1232}
1233
Linus Torvalds91539832006-04-17 11:59:32 -07001234static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
Linus Torvaldsee638022006-02-09 10:30:28 -08001235{
Linus Torvalds91539832006-04-17 11:59:32 -07001236 struct diff_options *opt = &rev->diffopt;
Junio C Hamano77667052013-02-03 13:08:18 -08001237 int line_termination, inter_name_termination, i;
John Keeping41ee2ad2013-02-07 20:15:28 +00001238 const char *line_prefix = diff_line_prefix(opt);
Linus Torvaldsee638022006-02-09 10:30:28 -08001239
1240 line_termination = opt->line_termination;
1241 inter_name_termination = '\t';
1242 if (!line_termination)
1243 inter_name_termination = 0;
1244
Junio C Hamano44152782006-10-26 02:05:59 -07001245 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -07001246 show_log(rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001247
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001248
Timo Hirvonenc6744342006-06-24 20:21:53 +03001249 if (opt->output_format & DIFF_FORMAT_RAW) {
John Keeping41ee2ad2013-02-07 20:15:28 +00001250 printf("%s", line_prefix);
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001251
Junio C Hamano77667052013-02-03 13:08:18 -08001252 /* As many colons as there are parents */
1253 for (i = 0; i < num_parent; i++)
1254 putchar(':');
Linus Torvaldsee638022006-02-09 10:30:28 -08001255
Junio C Hamano0a798072006-02-09 15:23:06 -08001256 /* Show the modes */
Junio C Hamano77667052013-02-03 13:08:18 -08001257 for (i = 0; i < num_parent; i++)
1258 printf("%06o ", p->parent[i].mode);
1259 printf("%06o", p->mode);
Junio C Hamano0a798072006-02-09 15:23:06 -08001260
1261 /* Show sha1's */
1262 for (i = 0; i < num_parent; i++)
Jeff Kingd6cece52016-10-20 02:20:07 -04001263 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
Jeff Kingd5e3b012016-10-20 02:19:43 -04001264 opt->abbrev));
Jeff Kingd6cece52016-10-20 02:20:07 -04001265 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
Junio C Hamano0a798072006-02-09 15:23:06 -08001266 }
1267
Timo Hirvonenc6744342006-06-24 20:21:53 +03001268 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamanod416df82006-02-10 02:30:52 -08001269 for (i = 0; i < num_parent; i++)
1270 putchar(p->parent[i].status);
1271 putchar(inter_name_termination);
1272 }
Junio C Hamano0a798072006-02-09 15:23:06 -08001273
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001274 for (i = 0; i < num_parent; i++)
1275 if (rev->combined_all_paths) {
1276 if (filename_changed(p->parent[i].status))
1277 write_name_quoted(p->parent[i].path.buf, stdout,
1278 inter_name_termination);
1279 else
1280 write_name_quoted(p->path, stdout,
1281 inter_name_termination);
1282 }
Pierre Habouzit663af342007-09-20 00:42:15 +02001283 write_name_quoted(p->path, stdout, line_termination);
Junio C Hamano0a798072006-02-09 15:23:06 -08001284}
1285
Junio C Hamano99694542011-08-04 11:39:10 -07001286/*
1287 * The result (p->elem) is from the working tree and their
1288 * parents are typically from multiple stages during a merge
1289 * (i.e. diff-files) or the state in HEAD and in the index
1290 * (i.e. diff-index).
1291 */
Linus Torvalds91539832006-04-17 11:59:32 -07001292void show_combined_diff(struct combine_diff_path *p,
Junio C Hamano0a798072006-02-09 15:23:06 -08001293 int num_parent,
Linus Torvalds91539832006-04-17 11:59:32 -07001294 struct rev_info *rev)
Junio C Hamano0a798072006-02-09 15:23:06 -08001295{
Linus Torvalds91539832006-04-17 11:59:32 -07001296 struct diff_options *opt = &rev->diffopt;
John Keeping41ee2ad2013-02-07 20:15:28 +00001297
Timo Hirvonenc6744342006-06-24 20:21:53 +03001298 if (opt->output_format & (DIFF_FORMAT_RAW |
1299 DIFF_FORMAT_NAME |
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001300 DIFF_FORMAT_NAME_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -07001301 show_raw_diff(p, num_parent, rev);
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001302 else if (opt->output_format & DIFF_FORMAT_PATCH)
Sergey Organovd01141d2020-09-29 14:31:22 +03001303 show_patch_diff(p, num_parent, 1, rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001304}
1305
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001306static void free_combined_pair(struct diff_filepair *pair)
1307{
1308 free(pair->two);
1309 free(pair);
1310}
1311
1312/*
1313 * A combine_diff_path expresses N parents on the LHS against 1 merge
1314 * result. Synthesize a diff_filepair that has N entries on the "one"
1315 * side and 1 entry on the "two" side.
1316 *
1317 * In the future, we might want to add more data to combine_diff_path
1318 * so that we can fill fields we are ignoring (most notably, size) here,
1319 * but currently nobody uses it, so this should suffice for now.
1320 */
1321static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1322 int num_parent)
1323{
1324 int i;
1325 struct diff_filepair *pair;
1326 struct diff_filespec *pool;
1327
1328 pair = xmalloc(sizeof(*pair));
René Scharfeca56dad2021-03-13 17:17:22 +01001329 CALLOC_ARRAY(pool, st_add(num_parent, 1));
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001330 pair->one = pool + 1;
1331 pair->two = pool;
1332
1333 for (i = 0; i < num_parent; i++) {
1334 pair->one[i].path = p->path;
1335 pair->one[i].mode = p->parent[i].mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +00001336 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
brian m. carlson41c95602016-06-24 23:09:24 +00001337 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001338 pair->one[i].has_more_entries = 1;
1339 }
1340 pair->one[num_parent - 1].has_more_entries = 0;
1341
1342 pair->two->path = p->path;
1343 pair->two->mode = p->mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +00001344 oidcpy(&pair->two->oid, &p->oid);
brian m. carlson41c95602016-06-24 23:09:24 +00001345 pair->two->oid_valid = !is_null_oid(&p->oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001346 return pair;
1347}
1348
1349static void handle_combined_callback(struct diff_options *opt,
1350 struct combine_diff_path *paths,
1351 int num_parent,
1352 int num_paths)
1353{
1354 struct combine_diff_path *p;
1355 struct diff_queue_struct q;
1356 int i;
1357
René Scharfeca56dad2021-03-13 17:17:22 +01001358 CALLOC_ARRAY(q.queue, num_paths);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001359 q.alloc = num_paths;
1360 q.nr = num_paths;
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001361 for (i = 0, p = paths; p; p = p->next)
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001362 q.queue[i++] = combined_pair(p, num_parent);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001363 opt->format_callback(&q, opt, opt->format_callback_data);
1364 for (i = 0; i < num_paths; i++)
1365 free_combined_pair(q.queue[i]);
1366 free(q.queue);
1367}
1368
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001369static const char *path_path(void *obj)
1370{
1371 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1372
1373 return path->path;
1374}
1375
Jeff King8817f0c2019-01-24 07:33:51 -05001376/*
1377 * Diff stat formats which we always compute solely against the first parent.
1378 */
1379#define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
Jeff King8290faa2019-01-24 07:34:51 -05001380 | DIFF_FORMAT_SHORTSTAT \
Jeff King04b19fc2019-01-24 07:35:40 -05001381 | DIFF_FORMAT_SUMMARY \
Jeff Kingdac03b52019-01-24 07:36:47 -05001382 | DIFF_FORMAT_DIRSTAT \
Jeff King8817f0c2019-01-24 07:33:51 -05001383 | DIFF_FORMAT_DIFFSTAT)
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001384
1385/* find set of paths that every parent touches */
Brandon Williams09fae192017-05-30 10:30:56 -07001386static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001387 const struct oid_array *parents,
1388 struct diff_options *opt,
1389 int combined_all_paths)
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001390{
1391 struct combine_diff_path *paths = NULL;
1392 int i, num_parent = parents->nr;
1393
1394 int output_format = opt->output_format;
1395 const char *orderfile = opt->orderfile;
1396
1397 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1398 /* tell diff_tree to emit paths in sorted (=tree) order */
1399 opt->orderfile = NULL;
1400
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001401 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001402 for (i = 0; i < num_parent; i++) {
1403 /*
1404 * show stat against the first parent even when doing
1405 * combined diff.
1406 */
Jeff King8817f0c2019-01-24 07:33:51 -05001407 int stat_opt = output_format & STAT_FORMAT_MASK;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001408 if (i == 0 && stat_opt)
1409 opt->output_format = stat_opt;
1410 else
1411 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams66f414f2017-05-30 10:31:03 -07001412 diff_tree_oid(&parents->oid[i], oid, "", opt);
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001413 diffcore_std(opt);
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001414 paths = intersect_paths(paths, i, num_parent,
1415 combined_all_paths);
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001416
1417 /* if showing diff, show it in requested order */
1418 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1419 orderfile) {
1420 diffcore_order(orderfile);
1421 }
1422
1423 diff_flush(opt);
1424 }
1425
1426 opt->output_format = output_format;
1427 opt->orderfile = orderfile;
1428 return paths;
1429}
1430
1431
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001432/*
1433 * find set of paths that everybody touches, assuming diff is run without
1434 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1435 */
1436static struct combine_diff_path *find_paths_multitree(
Brandon Williams09fae192017-05-30 10:30:56 -07001437 const struct object_id *oid, const struct oid_array *parents,
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001438 struct diff_options *opt)
1439{
1440 int i, nparent = parents->nr;
Brandon Williamsfda94b42017-05-30 10:31:06 -07001441 const struct object_id **parents_oid;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001442 struct combine_diff_path paths_head;
1443 struct strbuf base;
1444
Brandon Williamsfda94b42017-05-30 10:31:06 -07001445 ALLOC_ARRAY(parents_oid, nparent);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001446 for (i = 0; i < nparent; i++)
Brandon Williamsfda94b42017-05-30 10:31:06 -07001447 parents_oid[i] = &parents->oid[i];
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001448
1449 /* fake list head, so worker can assume it is non-NULL */
1450 paths_head.next = NULL;
1451
1452 strbuf_init(&base, PATH_MAX);
Brandon Williamsfda94b42017-05-30 10:31:06 -07001453 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001454
1455 strbuf_release(&base);
Brandon Williamsfda94b42017-05-30 10:31:06 -07001456 free(parents_oid);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001457 return paths_head.next;
1458}
1459
Jeff King957876f2020-09-30 07:52:40 -04001460static int match_objfind(struct combine_diff_path *path,
1461 int num_parent,
1462 const struct oidset *set)
1463{
1464 int i;
1465 if (oidset_contains(set, &path->oid))
1466 return 1;
1467 for (i = 0; i < num_parent; i++) {
1468 if (oidset_contains(set, &path->parent[i].oid))
1469 return 1;
1470 }
1471 return 0;
1472}
1473
1474static struct combine_diff_path *combined_objfind(struct diff_options *opt,
1475 struct combine_diff_path *paths,
1476 int num_parent)
1477{
1478 struct combine_diff_path *ret = NULL, **tail = &ret;
1479 struct combine_diff_path *p = paths;
1480
1481 while (p) {
1482 struct combine_diff_path *next = p->next;
1483
1484 if (match_objfind(p, num_parent, opt->objfind)) {
1485 p->next = NULL;
1486 *tail = p;
1487 tail = &p->next;
1488 } else {
1489 free(p);
1490 }
1491 p = next;
1492 }
1493
1494 return ret;
1495}
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001496
Brandon Williamsb9acf542017-05-30 10:30:55 -07001497void diff_tree_combined(const struct object_id *oid,
brian m. carlson910650d2017-03-31 01:40:00 +00001498 const struct oid_array *parents,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001499 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001500{
Linus Torvalds91539832006-04-17 11:59:32 -07001501 struct diff_options *opt = &rev->diffopt;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001502 struct diff_options diffopts;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001503 struct combine_diff_path *p, *paths;
René Scharfe0041f092011-12-17 11:15:48 +01001504 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001505 int need_generic_pathscan;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001506
René Scharfee3d1be42022-06-18 13:12:28 +02001507 if (opt->ignore_regex_nr)
1508 die("combined diff and '%s' cannot be used together",
1509 "--ignore-matching-lines");
René Scharfecfb19ae2022-06-18 13:12:34 +02001510 if (opt->close_file)
1511 die("combined diff and '%s' cannot be used together",
1512 "--output");
René Scharfee3d1be42022-06-18 13:12:28 +02001513
Kirill Smelkov51af1882014-02-03 16:47:19 +04001514 /* nothing to do, if no parents */
1515 if (!num_parent)
1516 return;
1517
1518 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1519 needsep = 0;
1520 if (show_log_first) {
1521 show_log(rev);
1522
Junio C Hamanoe7cc0ed2014-06-06 11:35:01 -07001523 if (rev->verbose_header && opt->output_format &&
Jeff Kingb9c7d6e2014-07-29 13:56:48 -04001524 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1525 !commit_format_is_empty(rev->commit_format))
Kirill Smelkov51af1882014-02-03 16:47:19 +04001526 printf("%s%c", diff_line_prefix(opt),
1527 opt->line_termination);
1528 }
1529
Junio C Hamano5b236832006-02-09 14:35:19 -08001530 diffopts = *opt;
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +07001531 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001532 diffopts.flags.recursive = 1;
1533 diffopts.flags.allow_external = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001534
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001535 /* find set of paths that everybody touches
1536 *
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001537 * NOTE
1538 *
1539 * Diffcore transformations are bound to diff_filespec and logic
1540 * comparing two entries - i.e. they do not apply directly to combine
1541 * diff.
1542 *
1543 * If some of such transformations is requested - we launch generic
1544 * path scanning, which works significantly slower compared to
1545 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1546 *
1547 * TODO some of the filters could be ported to work on
1548 * combine_diff_paths - i.e. all functionality that skips paths, so in
1549 * theory, we could end up having only multitree path scanning.
1550 *
1551 * NOTE please keep this semantically in sync with diffcore_std()
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001552 */
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001553 need_generic_pathscan = opt->skip_stat_unmatch ||
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001554 opt->flags.follow_renames ||
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001555 opt->break_opt != -1 ||
1556 opt->detect_rename ||
Jeff King957876f2020-09-30 07:52:40 -04001557 (opt->pickaxe_opts &
1558 (DIFF_PICKAXE_KINDS_MASK & ~DIFF_PICKAXE_KIND_OBJFIND)) ||
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001559 opt->filter;
1560
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001561 if (need_generic_pathscan) {
1562 /*
1563 * NOTE generic case also handles --stat, as it computes
1564 * diff(sha1,parent_i) for all i to do the job, specifically
1565 * for parent0.
1566 */
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001567 paths = find_paths_generic(oid, parents, &diffopts,
1568 rev->combined_all_paths);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001569 }
1570 else {
1571 int stat_opt;
Brandon Williams09fae192017-05-30 10:30:56 -07001572 paths = find_paths_multitree(oid, parents, &diffopts);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001573
Jeff King957876f2020-09-30 07:52:40 -04001574 if (opt->pickaxe_opts & DIFF_PICKAXE_KIND_OBJFIND)
1575 paths = combined_objfind(opt, paths, num_parent);
1576
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001577 /*
1578 * show stat against the first parent even
1579 * when doing combined diff.
1580 */
Jeff King8817f0c2019-01-24 07:33:51 -05001581 stat_opt = opt->output_format & STAT_FORMAT_MASK;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001582 if (stat_opt) {
1583 diffopts.output_format = stat_opt;
1584
Brandon Williams66f414f2017-05-30 10:31:03 -07001585 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001586 diffcore_std(&diffopts);
1587 if (opt->orderfile)
1588 diffcore_order(opt->orderfile);
1589 diff_flush(&diffopts);
1590 }
1591 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001592
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001593 /* find out number of surviving paths */
1594 for (num_paths = 0, p = paths; p; p = p->next)
1595 num_paths++;
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001596
1597 /* order paths according to diffcore_order */
1598 if (opt->orderfile && num_paths) {
1599 struct obj_order *o;
1600
Jeff Kingb32fa952016-02-22 17:44:25 -05001601 ALLOC_ARRAY(o, num_paths);
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001602 for (i = 0, p = paths; p; p = p->next, i++)
1603 o[i].obj = p;
1604 order_objects(opt->orderfile, path_path, o, num_paths);
1605 for (i = 0; i < num_paths - 1; i++) {
1606 p = o[i].obj;
1607 p->next = o[i+1].obj;
1608 }
1609
1610 p = o[num_paths-1].obj;
1611 p->next = NULL;
1612 paths = o[0].obj;
1613 free(o);
1614 }
1615
1616
Junio C Hamanoe3c3a552006-02-05 22:25:00 -08001617 if (num_paths) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001618 if (opt->output_format & (DIFF_FORMAT_RAW |
1619 DIFF_FORMAT_NAME |
1620 DIFF_FORMAT_NAME_STATUS)) {
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001621 for (p = paths; p; p = p->next)
1622 show_raw_diff(p, num_parent, rev);
Junio C Hamano3969cf72006-06-27 15:08:19 -07001623 needsep = 1;
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001624 }
Jeff King8817f0c2019-01-24 07:33:51 -05001625 else if (opt->output_format & STAT_FORMAT_MASK)
Junio C Hamano3969cf72006-06-27 15:08:19 -07001626 needsep = 1;
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001627 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1628 handle_combined_callback(opt, paths, num_parent, num_paths);
1629
Timo Hirvonenc6744342006-06-24 20:21:53 +03001630 if (opt->output_format & DIFF_FORMAT_PATCH) {
Junio C Hamano3969cf72006-06-27 15:08:19 -07001631 if (needsep)
John Keeping41ee2ad2013-02-07 20:15:28 +00001632 printf("%s%c", diff_line_prefix(opt),
1633 opt->line_termination);
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001634 for (p = paths; p; p = p->next)
Sergey Organovd01141d2020-09-29 14:31:22 +03001635 show_patch_diff(p, num_parent, 0, rev);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001636 }
1637 }
1638
1639 /* Clean things up */
1640 while (paths) {
Junio C Hamanoea726d02006-01-28 00:03:38 -08001641 struct combine_diff_path *tmp = paths;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001642 paths = paths->next;
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001643 for (i = 0; i < num_parent; i++)
1644 if (rev->combined_all_paths &&
1645 filename_changed(tmp->parent[i].status))
1646 strbuf_release(&tmp->parent[i].path);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001647 free(tmp);
1648 }
Clemens Buchacher46ec5102013-05-28 00:49:57 +02001649
Junio C Hamanoed6e8032016-06-02 14:09:22 -07001650 clear_pathspec(&diffopts.pathspec);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001651}
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001652
Sergey Organovd01141d2020-09-29 14:31:22 +03001653void diff_tree_combined_merge(const struct commit *commit,
René Scharfe82889292011-12-17 11:20:07 +01001654 struct rev_info *rev)
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001655{
Thomas Rast53d00b32013-07-31 22:13:20 +02001656 struct commit_list *parent = get_saved_parents(rev, commit);
brian m. carlson910650d2017-03-31 01:40:00 +00001657 struct oid_array parents = OID_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001658
René Scharfe0041f092011-12-17 11:15:48 +01001659 while (parent) {
brian m. carlson910650d2017-03-31 01:40:00 +00001660 oid_array_append(&parents, &parent->item->object.oid);
René Scharfe0041f092011-12-17 11:15:48 +01001661 parent = parent->next;
1662 }
Sergey Organovd01141d2020-09-29 14:31:22 +03001663 diff_tree_combined(&commit->object.oid, &parents, rev);
brian m. carlson910650d2017-03-31 01:40:00 +00001664 oid_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001665}