blob: f8d619657767134ee56aaf57eab841e76ede00d0 [file] [log] [blame]
Elijah Newren5e3f94d2023-04-22 20:17:23 +00001#include "git-compat-util.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07002#include "object-store.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08003#include "commit.h"
Elijah Newren73359a92023-04-11 03:00:40 +00004#include "convert.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02005#include "blob.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08006#include "diff.h"
7#include "diffcore.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00008#include "environment.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00009#include "hex.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070010#include "object-name.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080011#include "quote.h"
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070012#include "xdiff-interface.h"
Antoine Pelissefa04ae02013-03-14 22:03:14 +010013#include "xdiff/xmacros.h"
Linus Torvalds91539832006-04-17 11:59:32 -070014#include "log-tree.h"
Junio C Hamano7dae8b22009-04-29 12:49:52 -070015#include "refs.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"
Elijah Newren65156bb2023-04-11 00:42:02 -070019#include "wrapper.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080020
Jeff Kinge09867f2014-08-19 22:14:30 -040021static int compare_paths(const struct combine_diff_path *one,
22 const struct diff_filespec *two)
23{
24 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
25 return strcmp(one->path, two->path);
26
27 return base_name_compare(one->path, strlen(one->path), one->mode,
28 two->path, strlen(two->path), two->mode);
29}
30
Elijah Newrend76ce4f2019-02-07 17:12:46 -080031static int filename_changed(char status)
32{
33 return status == 'R' || status == 'C';
34}
35
36static struct combine_diff_path *intersect_paths(
37 struct combine_diff_path *curr,
38 int n,
39 int num_parent,
40 int combined_all_paths)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080041{
42 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano7b1004b2014-01-28 13:55:59 -080043 struct combine_diff_path *p, **tail = &curr;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080044 int i, j, cmp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080045
46 if (!n) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080047 for (i = 0; i < q->nr; i++) {
48 int len;
49 const char *path;
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070050 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080051 continue;
52 path = q->queue[i]->two->path;
53 len = strlen(path);
Junio C Hamano2454c962006-02-06 12:53:07 -080054 p = xmalloc(combine_diff_path_size(num_parent, len));
Felipe Contreras4b25d092009-05-01 12:06:36 +030055 p->path = (char *) &(p->parent[num_parent]);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080056 memcpy(p->path, path, len);
57 p->path[len] = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080058 p->next = NULL;
Junio C Hamano2454c962006-02-06 12:53:07 -080059 memset(p->parent, 0,
60 sizeof(p->parent[0]) * num_parent);
61
brian m. carlsona0d12c42016-06-24 23:09:23 +000062 oidcpy(&p->oid, &q->queue[i]->two->oid);
Junio C Hamano2454c962006-02-06 12:53:07 -080063 p->mode = q->queue[i]->two->mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +000064 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
Junio C Hamano2454c962006-02-06 12:53:07 -080065 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080066 p->parent[n].status = q->queue[i]->status;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080067
68 if (combined_all_paths &&
69 filename_changed(p->parent[n].status)) {
70 strbuf_init(&p->parent[n].path, 0);
71 strbuf_addstr(&p->parent[n].path,
72 q->queue[i]->one->path);
73 }
Junio C Hamano5290a0f2006-01-25 03:34:10 -080074 *tail = p;
75 tail = &p->next;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080076 }
Junio C Hamano7b1004b2014-01-28 13:55:59 -080077 return curr;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080078 }
79
Kirill Smelkov8518ff82014-01-20 20:20:40 +040080 /*
Junio C Hamano7b1004b2014-01-28 13:55:59 -080081 * paths in curr (linked list) and q->queue[] (array) are
82 * both sorted in the tree order.
Kirill Smelkov8518ff82014-01-20 20:20:40 +040083 */
Kirill Smelkov8518ff82014-01-20 20:20:40 +040084 i = 0;
Junio C Hamano7b1004b2014-01-28 13:55:59 -080085 while ((p = *tail) != NULL) {
86 cmp = ((i >= q->nr)
Jeff Kinge09867f2014-08-19 22:14:30 -040087 ? -1 : compare_paths(p, q->queue[i]->two));
Kirill Smelkov8518ff82014-01-20 20:20:40 +040088
Kirill Smelkov8518ff82014-01-20 20:20:40 +040089 if (cmp < 0) {
Junio C Hamano7b1004b2014-01-28 13:55:59 -080090 /* p->path not in q->queue[]; drop it */
91 *tail = p->next;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080092 for (j = 0; j < num_parent; j++)
93 if (combined_all_paths &&
94 filename_changed(p->parent[j].status))
95 strbuf_release(&p->parent[j].path);
Junio C Hamano7b1004b2014-01-28 13:55:59 -080096 free(p);
Kirill Smelkov8518ff82014-01-20 20:20:40 +040097 continue;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080098 }
Kirill Smelkov8518ff82014-01-20 20:20:40 +040099
100 if (cmp > 0) {
Junio C Hamano7b1004b2014-01-28 13:55:59 -0800101 /* q->queue[i] not in p->path; skip it */
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400102 i++;
103 continue;
104 }
105
brian m. carlsona0d12c42016-06-24 23:09:23 +0000106 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400107 p->parent[n].mode = q->queue[i]->one->mode;
108 p->parent[n].status = q->queue[i]->status;
Elijah Newrend76ce4f2019-02-07 17:12:46 -0800109 if (combined_all_paths &&
110 filename_changed(p->parent[n].status))
111 strbuf_addstr(&p->parent[n].path,
112 q->queue[i]->one->path);
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400113
Junio C Hamano7b1004b2014-01-28 13:55:59 -0800114 tail = &p->next;
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400115 i++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800116 }
117 return curr;
118}
119
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800120/* Lines lost from parent */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800121struct lline {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100122 struct lline *next, *prev;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800123 int len;
124 unsigned long parent_map;
125 char line[FLEX_ARRAY];
126};
127
Antoine Pelisse99d32062013-03-23 18:23:28 +0100128/* Lines lost from current parent (before coalescing) */
129struct plost {
130 struct lline *lost_head, *lost_tail;
131 int len;
132};
133
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800134/* Lines surviving in the merge result */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800135struct sline {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100136 /* Accumulated and coalesced lost lines */
137 struct lline *lost;
138 int lenlost;
139 struct plost plost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800140 char *bol;
141 int len;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800142 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
143 * we did not change it).
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800144 * bit N is used for "interesting" lines, including context.
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700145 * bit (N+1) is used for "do not show deletion before this".
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800146 */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800147 unsigned long flag;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800148 unsigned long *p_lno;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800149};
150
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100151static int match_string_spaces(const char *line1, int len1,
152 const char *line2, int len2,
153 long flags)
154{
155 if (flags & XDF_WHITESPACE_FLAGS) {
156 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
157 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
158 }
159
160 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
161 return (len1 == len2 && !memcmp(line1, line2, len1));
162
163 while (len1 > 0 && len2 > 0) {
164 len1--;
165 len2--;
166 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
167 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
168 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
169 return 0;
170
171 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
172 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
173 }
174 if (line1[len1] != line2[len2])
175 return 0;
176 }
177
178 if (flags & XDF_IGNORE_WHITESPACE) {
179 /* Consume remaining spaces */
180 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
181 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
182 }
183
184 /* We matched full line1 and line2 */
185 if (!len1 && !len2)
186 return 1;
187
188 return 0;
189}
190
Antoine Pelisse99d32062013-03-23 18:23:28 +0100191enum coalesce_direction { MATCH, BASE, NEW };
192
193/* Coalesce new lines into base by finding LCS */
194static struct lline *coalesce_lines(struct lline *base, int *lenbase,
Brandon Williams06fffa02018-02-14 10:59:36 -0800195 struct lline *newline, int lennew,
Antoine Pelisse99d32062013-03-23 18:23:28 +0100196 unsigned long parent, long flags)
197{
198 int **lcs;
199 enum coalesce_direction **directions;
200 struct lline *baseend, *newend = NULL;
201 int i, j, origbaselen = *lenbase;
202
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700203 if (!newline)
Antoine Pelisse99d32062013-03-23 18:23:28 +0100204 return base;
205
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700206 if (!base) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100207 *lenbase = lennew;
Brandon Williams06fffa02018-02-14 10:59:36 -0800208 return newline;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100209 }
210
211 /*
212 * Coalesce new lines into base by finding the LCS
Ondřej Bílka98e023d2013-07-29 10:18:21 +0200213 * - Create the table to run dynamic programming
Antoine Pelisse99d32062013-03-23 18:23:28 +0100214 * - Compute the LCS
215 * - Then reverse read the direction structure:
216 * - If we have MATCH, assign parent to base flag, and consume
217 * both baseend and newend
218 * - Else if we have BASE, consume baseend
219 * - Else if we have NEW, insert newend lline into base and
220 * consume newend
221 */
René Scharfeca56dad2021-03-13 17:17:22 +0100222 CALLOC_ARRAY(lcs, st_add(origbaselen, 1));
223 CALLOC_ARRAY(directions, st_add(origbaselen, 1));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100224 for (i = 0; i < origbaselen + 1; i++) {
René Scharfeca56dad2021-03-13 17:17:22 +0100225 CALLOC_ARRAY(lcs[i], st_add(lennew, 1));
226 CALLOC_ARRAY(directions[i], st_add(lennew, 1));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100227 directions[i][0] = BASE;
228 }
229 for (j = 1; j < lennew + 1; j++)
230 directions[0][j] = NEW;
231
232 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
Brandon Williams06fffa02018-02-14 10:59:36 -0800233 for (j = 1, newend = newline; j < lennew + 1; j++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100234 if (match_string_spaces(baseend->line, baseend->len,
235 newend->line, newend->len, flags)) {
236 lcs[i][j] = lcs[i - 1][j - 1] + 1;
237 directions[i][j] = MATCH;
238 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
239 lcs[i][j] = lcs[i][j - 1];
240 directions[i][j] = NEW;
241 } else {
242 lcs[i][j] = lcs[i - 1][j];
243 directions[i][j] = BASE;
244 }
245 if (newend->next)
246 newend = newend->next;
247 }
248 if (baseend->next)
249 baseend = baseend->next;
250 }
251
252 for (i = 0; i < origbaselen + 1; i++)
253 free(lcs[i]);
254 free(lcs);
255
256 /* At this point, baseend and newend point to the end of each lists */
257 i--;
258 j--;
259 while (i != 0 || j != 0) {
260 if (directions[i][j] == MATCH) {
261 baseend->parent_map |= 1<<parent;
262 baseend = baseend->prev;
263 newend = newend->prev;
264 i--;
265 j--;
266 } else if (directions[i][j] == NEW) {
267 struct lline *lline;
268
269 lline = newend;
270 /* Remove lline from new list and update newend */
271 if (lline->prev)
272 lline->prev->next = lline->next;
273 else
Brandon Williams06fffa02018-02-14 10:59:36 -0800274 newline = lline->next;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100275 if (lline->next)
276 lline->next->prev = lline->prev;
277
278 newend = lline->prev;
279 j--;
280
281 /* Add lline to base list */
282 if (baseend) {
283 lline->next = baseend->next;
284 lline->prev = baseend;
285 if (lline->prev)
286 lline->prev->next = lline;
287 }
288 else {
289 lline->next = base;
290 base = lline;
291 }
292 (*lenbase)++;
293
294 if (lline->next)
295 lline->next->prev = lline;
296
297 } else {
298 baseend = baseend->prev;
299 i--;
300 }
301 }
302
Brandon Williams06fffa02018-02-14 10:59:36 -0800303 newend = newline;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100304 while (newend) {
305 struct lline *lline = newend;
306 newend = newend->next;
307 free(lline);
308 }
309
310 for (i = 0; i < origbaselen + 1; i++)
311 free(directions[i]);
312 free(directions);
313
314 return base;
315}
316
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200317static char *grab_blob(struct repository *r,
318 const struct object_id *oid, unsigned int mode,
Antoine Pelisse99d32062013-03-23 18:23:28 +0100319 unsigned long *size, struct userdiff_driver *textconv,
320 const char *path)
321{
322 char *blob;
323 enum object_type type;
324
325 if (S_ISGITLINK(mode)) {
Jeff King0dc3b032017-03-28 15:46:53 -0400326 struct strbuf buf = STRBUF_INIT;
327 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
328 *size = buf.len;
329 blob = strbuf_detach(&buf, NULL);
brian m. carlson1ff57c12015-03-13 23:39:33 +0000330 } else if (is_null_oid(oid)) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100331 /* deleted blob */
332 *size = 0;
333 return xcalloc(1, 1);
334 } else if (textconv) {
335 struct diff_filespec *df = alloc_filespec(path);
Brandon Williamsf9704c22017-05-30 10:30:50 -0700336 fill_filespec(df, oid, 1, mode);
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200337 *size = fill_textconv(r, textconv, df, &blob);
Antoine Pelisse99d32062013-03-23 18:23:28 +0100338 free_filespec(df);
339 } else {
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +0200340 blob = repo_read_object_file(r, oid, &type, size);
Antoine Pelisse99d32062013-03-23 18:23:28 +0100341 if (type != OBJ_BLOB)
brian m. carlson1ff57c12015-03-13 23:39:33 +0000342 die("object '%s' is not a blob!", oid_to_hex(oid));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100343 }
344 return blob;
345}
346
347static void append_lost(struct sline *sline, int n, const char *line, int len)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800348{
349 struct lline *lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800350 unsigned long this_mask = (1UL<<n);
351 if (line[len-1] == '\n')
352 len--;
353
Jeff King96ffc062016-02-22 17:44:32 -0500354 FLEX_ALLOC_MEM(lline, line, line, len);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800355 lline->len = len;
356 lline->next = NULL;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100357 lline->prev = sline->plost.lost_tail;
358 if (lline->prev)
359 lline->prev->next = lline;
360 else
361 sline->plost.lost_head = lline;
362 sline->plost.lost_tail = lline;
363 sline->plost.len++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800364 lline->parent_map = this_mask;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800365}
366
Junio C Hamanof23fc772006-04-03 18:53:15 -0700367struct combine_diff_state {
Junio C Hamanoa0fd3142006-04-06 22:29:55 -0700368 unsigned int lno;
369 int ob, on, nb, nn;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700370 unsigned long nmask;
371 int num_parent;
372 int n;
373 struct sline *sline;
374 struct sline *lost_bucket;
375};
376
Jeff King0074c912018-11-02 02:38:20 -0400377static void consume_hunk(void *state_,
378 long ob, long on,
379 long nb, long nn,
Jeff King61bdc7c2022-12-13 06:13:48 -0500380 const char *func UNUSED, long funclen UNUSED)
Jeff King0074c912018-11-02 02:38:20 -0400381{
382 struct combine_diff_state *state = state_;
383
384 state->ob = ob;
385 state->on = on;
386 state->nb = nb;
387 state->nn = nn;
388 state->lno = state->nb;
389 if (state->nn == 0) {
390 /* @@ -X,Y +N,0 @@ removed Y lines
391 * that would have come *after* line N
392 * in the result. Our lost buckets hang
393 * to the line after the removed lines,
394 *
395 * Note that this is correct even when N == 0,
396 * in which case the hunk removes the first
397 * line in the file.
398 */
399 state->lost_bucket = &state->sline[state->nb];
400 if (!state->nb)
401 state->nb = 1;
402 } else {
403 state->lost_bucket = &state->sline[state->nb-1];
404 }
405 if (!state->sline[state->nb-1].p_lno)
René Scharfeca56dad2021-03-13 17:17:22 +0100406 CALLOC_ARRAY(state->sline[state->nb - 1].p_lno,
407 state->num_parent);
Jeff King0074c912018-11-02 02:38:20 -0400408 state->sline[state->nb-1].p_lno[state->n] = state->ob;
409}
410
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200411static int consume_line(void *state_, char *line, unsigned long len)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700412{
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700413 struct combine_diff_state *state = state_;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700414 if (!state->lost_bucket)
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200415 return 0; /* not in any hunk yet */
Junio C Hamanof23fc772006-04-03 18:53:15 -0700416 switch (line[0]) {
417 case '-':
Antoine Pelisse99d32062013-03-23 18:23:28 +0100418 append_lost(state->lost_bucket, state->n, line+1, len-1);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700419 break;
420 case '+':
421 state->sline[state->lno-1].flag |= state->nmask;
422 state->lno++;
423 break;
424 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200425 return 0;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700426}
427
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200428static void combine_diff(struct repository *r,
429 const struct object_id *parent, unsigned int mode,
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700430 mmfile_t *result_file,
Junio C Hamano2386c292006-06-28 01:38:19 -0700431 struct sline *sline, unsigned int cnt, int n,
Jeff King0508fe52011-05-23 16:31:05 -0400432 int num_parent, int result_deleted,
433 struct userdiff_driver *textconv,
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100434 const char *path, long flags)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800435{
Junio C Hamanof23fc772006-04-03 18:53:15 -0700436 unsigned int p_lno, lno;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800437 unsigned long nmask = (1UL << n);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700438 xpparam_t xpp;
439 xdemitconf_t xecfg;
440 mmfile_t parent_file;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700441 struct combine_diff_state state;
442 unsigned long sz;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800443
Thomas Rast21798702010-04-15 14:59:37 +0200444 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800445 return; /* result deleted */
446
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200447 parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700448 parent_file.size = sz;
Brian Downing9ccd0a82008-10-25 15:30:37 +0200449 memset(&xpp, 0, sizeof(xpp));
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100450 xpp.flags = flags;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100451 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700452 memset(&state, 0, sizeof(state));
453 state.nmask = nmask;
454 state.sline = sline;
455 state.lno = 1;
456 state.num_parent = num_parent;
457 state.n = n;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800458
Jeff King0074c912018-11-02 02:38:20 -0400459 if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
460 consume_line, &state, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -0400461 die("unable to generate combined diff for %s",
Junio C Hamano11a458b2015-09-28 15:33:56 -0700462 oid_to_hex(parent));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700463 free(parent_file.ptr);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800464
465 /* Assign line numbers for this parent.
466 *
467 * sline[lno].p_lno[n] records the first line number
468 * (counting from 1) for parent N if the final hunk display
469 * started by showing sline[lno] (possibly showing the lost
470 * lines attached to it first).
471 */
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700472 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
Junio C Hamanof16706c2006-01-30 22:33:15 -0800473 struct lline *ll;
474 sline[lno].p_lno[n] = p_lno;
475
Antoine Pelisse99d32062013-03-23 18:23:28 +0100476 /* Coalesce new lines */
477 if (sline[lno].plost.lost_head) {
478 struct sline *sl = &sline[lno];
479 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
480 sl->plost.lost_head,
481 sl->plost.len, n, flags);
482 sl->plost.lost_head = sl->plost.lost_tail = NULL;
483 sl->plost.len = 0;
484 }
485
Junio C Hamanof16706c2006-01-30 22:33:15 -0800486 /* How many lines would this sline advance the p_lno? */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100487 ll = sline[lno].lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800488 while (ll) {
489 if (ll->parent_map & nmask)
490 p_lno++; /* '-' means parent had it */
491 ll = ll->next;
492 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700493 if (lno < cnt && !(sline[lno].flag & nmask))
Junio C Hamanof16706c2006-01-30 22:33:15 -0800494 p_lno++; /* no '+' means parent had it */
495 }
496 sline[lno].p_lno[n] = p_lno; /* trailer */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800497}
498
499static unsigned long context = 3;
500static char combine_marker = '@';
501
502static int interesting(struct sline *sline, unsigned long all_mask)
503{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800504 /* If some parents lost lines here, or if we have added to
505 * some parent, it is interesting.
506 */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100507 return ((sline->flag & all_mask) || sline->lost);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800508}
509
Junio C Hamano3ec19092006-01-26 03:53:01 -0800510static unsigned long adjust_hunk_tail(struct sline *sline,
511 unsigned long all_mask,
512 unsigned long hunk_begin,
513 unsigned long i)
514{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800515 /* i points at the first uninteresting line. If the last line
516 * of the hunk was interesting only because it has some
517 * deletion, then it is not all that interesting for the
518 * purpose of giving trailing context lines. This is because
519 * we output '-' line and then unmodified sline[i-1] itself in
520 * that case which gives us one extra context line.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800521 */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800522 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800523 i--;
524 return i;
525}
526
Junio C Hamano46dc9412006-02-02 15:17:42 -0800527static unsigned long find_next(struct sline *sline,
528 unsigned long mark,
529 unsigned long i,
530 unsigned long cnt,
Junio C Hamano2386c292006-06-28 01:38:19 -0700531 int look_for_uninteresting)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800532{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800533 /* We have examined up to i-1 and are about to look at i.
534 * Find next interesting or uninteresting line. Here,
535 * "interesting" does not mean interesting(), but marked by
536 * the give_context() function below (i.e. it includes context
537 * lines that are not interesting to interesting() function
538 * that are surrounded by interesting() ones.
539 */
Junio C Hamano74065952006-04-11 14:31:31 -0700540 while (i <= cnt)
Junio C Hamano2386c292006-06-28 01:38:19 -0700541 if (look_for_uninteresting
Junio C Hamano46dc9412006-02-02 15:17:42 -0800542 ? !(sline[i].flag & mark)
543 : (sline[i].flag & mark))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800544 return i;
545 else
546 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700547 return i;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800548}
549
550static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
551{
552 unsigned long all_mask = (1UL<<num_parent) - 1;
553 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700554 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800555 unsigned long i;
556
Junio C Hamano46dc9412006-02-02 15:17:42 -0800557 /* Two groups of interesting lines may have a short gap of
Pavel Roskin82e5a822006-07-10 01:50:18 -0400558 * uninteresting lines. Connect such groups to give them a
Junio C Hamano46dc9412006-02-02 15:17:42 -0800559 * bit of context.
560 *
561 * We first start from what the interesting() function says,
562 * and mark them with "mark", and paint context lines with the
563 * mark. So interesting() would still say false for such context
564 * lines but they are treated as "interesting" in the end.
565 */
566 i = find_next(sline, mark, 0, cnt, 0);
Junio C Hamano74065952006-04-11 14:31:31 -0700567 if (cnt < i)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800568 return 0;
569
Junio C Hamano74065952006-04-11 14:31:31 -0700570 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800571 unsigned long j = (context < i) ? (i - context) : 0;
572 unsigned long k;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800573
574 /* Paint a few lines before the first interesting line. */
Matthijs Kooijmanaac38572013-05-15 19:42:14 +0200575 while (j < i) {
576 if (!(sline[j].flag & mark))
577 sline[j].flag |= no_pre_delete;
578 sline[j++].flag |= mark;
579 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800580
581 again:
Junio C Hamano46dc9412006-02-02 15:17:42 -0800582 /* we know up to i is to be included. where does the
583 * next uninteresting one start?
584 */
585 j = find_next(sline, mark, i, cnt, 1);
Junio C Hamano74065952006-04-11 14:31:31 -0700586 if (cnt < j)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800587 break; /* the rest are all interesting */
588
589 /* lookahead context lines */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800590 k = find_next(sline, mark, j, cnt, 0);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800591 j = adjust_hunk_tail(sline, all_mask, i, j);
592
593 if (k < j + context) {
594 /* k is interesting and [j,k) are not, but
595 * paint them interesting because the gap is small.
596 */
597 while (j < k)
598 sline[j++].flag |= mark;
599 i = k;
600 goto again;
601 }
602
603 /* j is the first uninteresting line and there is
Junio C Hamano46dc9412006-02-02 15:17:42 -0800604 * no overlap beyond it within context lines. Paint
605 * the trailing edge a bit.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800606 */
607 i = k;
Junio C Hamano74065952006-04-11 14:31:31 -0700608 k = (j + context < cnt+1) ? j + context : cnt+1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800609 while (j < k)
610 sline[j++].flag |= mark;
611 }
612 return 1;
613}
614
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800615static int make_hunks(struct sline *sline, unsigned long cnt,
Junio C Hamanod8f47902006-01-24 01:22:04 -0800616 int num_parent, int dense)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800617{
618 unsigned long all_mask = (1UL<<num_parent) - 1;
619 unsigned long mark = (1UL<<num_parent);
620 unsigned long i;
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800621 int has_interesting = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800622
Junio C Hamano74065952006-04-11 14:31:31 -0700623 for (i = 0; i <= cnt; i++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800624 if (interesting(&sline[i], all_mask))
625 sline[i].flag |= mark;
626 else
627 sline[i].flag &= ~mark;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800628 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800629 if (!dense)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800630 return give_context(sline, cnt, num_parent);
Junio C Hamanod8f47902006-01-24 01:22:04 -0800631
Junio C Hamano263eee22006-01-25 13:11:38 -0800632 /* Look at each hunk, and if we have changes from only one
633 * parent, or the changes are the same from all but one
634 * parent, mark that uninteresting.
Junio C Hamanod8f47902006-01-24 01:22:04 -0800635 */
636 i = 0;
Junio C Hamano74065952006-04-11 14:31:31 -0700637 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800638 unsigned long j, hunk_begin, hunk_end;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800639 unsigned long same_diff;
Junio C Hamano74065952006-04-11 14:31:31 -0700640 while (i <= cnt && !(sline[i].flag & mark))
Junio C Hamanod8f47902006-01-24 01:22:04 -0800641 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700642 if (cnt < i)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800643 break; /* No more interesting hunks */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800644 hunk_begin = i;
Junio C Hamano74065952006-04-11 14:31:31 -0700645 for (j = i + 1; j <= cnt; j++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800646 if (!(sline[j].flag & mark)) {
647 /* Look beyond the end to see if there
648 * is an interesting line after this
649 * hunk within context span.
650 */
651 unsigned long la; /* lookahead */
652 int contin = 0;
653 la = adjust_hunk_tail(sline, all_mask,
654 hunk_begin, j);
Junio C Hamano74065952006-04-11 14:31:31 -0700655 la = (la + context < cnt + 1) ?
656 (la + context) : cnt + 1;
René Scharfee5e9b562012-03-24 16:18:46 +0100657 while (la && j <= --la) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800658 if (sline[la].flag & mark) {
659 contin = 1;
660 break;
661 }
662 }
663 if (!contin)
664 break;
665 j = la;
666 }
667 }
668 hunk_end = j;
669
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800670 /* [i..hunk_end) are interesting. Now is it really
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800671 * interesting? We check if there are only two versions
672 * and the result matches one of them. That is, we look
673 * at:
674 * (+) line, which records lines added to which parents;
675 * this line appears in the result.
676 * (-) line, which records from what parents the line
677 * was removed; this line does not appear in the result.
678 * then check the set of parents the result has difference
679 * from, from all lines. If there are lines that has
680 * different set of parents that the result has differences
681 * from, that means we have more than two versions.
682 *
683 * Even when we have only two versions, if the result does
684 * not match any of the parents, the it should be considered
685 * interesting. In such a case, we would have all '+' line.
686 * After passing the above "two versions" test, that would
687 * appear as "the same set of parents" to be "all parents".
Junio C Hamanod8f47902006-01-24 01:22:04 -0800688 */
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800689 same_diff = 0;
690 has_interesting = 0;
691 for (j = i; j < hunk_end && !has_interesting; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800692 unsigned long this_diff = sline[j].flag & all_mask;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100693 struct lline *ll = sline[j].lost;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800694 if (this_diff) {
695 /* This has some changes. Is it the
696 * same as others?
697 */
698 if (!same_diff)
699 same_diff = this_diff;
700 else if (same_diff != this_diff) {
701 has_interesting = 1;
702 break;
703 }
704 }
705 while (ll && !has_interesting) {
706 /* Lost this line from these parents;
707 * who are they? Are they the same?
708 */
709 this_diff = ll->parent_map;
710 if (!same_diff)
711 same_diff = this_diff;
712 else if (same_diff != this_diff) {
713 has_interesting = 1;
714 }
715 ll = ll->next;
716 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800717 }
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800718
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800719 if (!has_interesting && same_diff != all_mask) {
Junio C Hamanod8f47902006-01-24 01:22:04 -0800720 /* This hunk is not that interesting after all */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800721 for (j = hunk_begin; j < hunk_end; j++)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800722 sline[j].flag &= ~mark;
723 }
724 i = hunk_end;
725 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800726
727 has_interesting = give_context(sline, cnt, num_parent);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800728 return has_interesting;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800729}
730
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800731static 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 -0800732{
733 l0 = sline[l0].p_lno[n];
734 l1 = sline[l1].p_lno[n];
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800735 printf(" -%lu,%lu", l0, l1-l0-null_context);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800736}
737
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700738static int hunk_comment_line(const char *bol)
739{
Junio C Hamano7a8ac592006-10-26 02:05:05 -0700740 int ch;
741
742 if (!bol)
743 return 0;
744 ch = *bol & 0xff;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700745 return (isalpha(ch) || ch == '_' || ch == '$');
746}
747
Junio C Hamano39280972008-08-27 19:48:01 -0700748static void show_line_to_eol(const char *line, int len, const char *reset)
749{
750 int saw_cr_at_eol = 0;
751 if (len < 0)
752 len = strlen(line);
753 saw_cr_at_eol = (len && line[len-1] == '\r');
754
755 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
756 reset,
757 saw_cr_at_eol ? "\r" : "");
758}
759
John Keeping41ee2ad2013-02-07 20:15:28 +0000760static void dump_sline(struct sline *sline, const char *line_prefix,
761 unsigned long cnt, int num_parent,
Thomas Rast21798702010-04-15 14:59:37 +0200762 int use_color, int result_deleted)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800763{
764 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700765 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800766 int i;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800767 unsigned long lno = 0;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700768 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100769 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700770 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
771 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
Jeff King8dbf3eb2015-05-27 16:48:46 -0400772 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700773 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800774
Thomas Rast21798702010-04-15 14:59:37 +0200775 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800776 return; /* result deleted */
777
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800778 while (1) {
Junio C Hamano8bc75742006-04-12 13:23:50 -0700779 unsigned long hunk_end;
780 unsigned long rlines;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700781 const char *hunk_comment = NULL;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800782 unsigned long null_context = 0;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700783
784 while (lno <= cnt && !(sline[lno].flag & mark)) {
785 if (hunk_comment_line(sline[lno].bol))
786 hunk_comment = sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800787 lno++;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700788 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700789 if (cnt < lno)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800790 break;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700791 else {
Junio C Hamano74065952006-04-11 14:31:31 -0700792 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700793 if (!(sline[hunk_end].flag & mark))
794 break;
795 }
Junio C Hamano74065952006-04-11 14:31:31 -0700796 rlines = hunk_end - lno;
797 if (cnt < hunk_end)
798 rlines--; /* pointing at the last delete hunk */
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800799
800 if (!context) {
801 /*
802 * Even when running with --unified=0, all
803 * lines in the hunk needs to be processed in
804 * the loop below in order to show the
805 * deletion recorded in lost_head. However,
806 * we do not want to show the resulting line
807 * with all blank context markers in such a
808 * case. Compensate.
809 */
810 unsigned long j;
811 for (j = lno; j < hunk_end; j++)
812 if (!(sline[j].flag & (mark-1)))
813 null_context++;
814 rlines -= null_context;
815 }
816
John Keeping41ee2ad2013-02-07 20:15:28 +0000817 printf("%s%s", line_prefix, c_frag);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800818 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800819 for (i = 0; i < num_parent; i++)
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800820 show_parent_lno(sline, lno, hunk_end, i, null_context);
Junio C Hamano74065952006-04-11 14:31:31 -0700821 printf(" +%lu,%lu ", lno+1, rlines);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800822 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700823
824 if (hunk_comment) {
825 int comment_end = 0;
826 for (i = 0; i < 40; i++) {
827 int ch = hunk_comment[i] & 0xff;
828 if (!ch || ch == '\n')
829 break;
830 if (!isspace(ch))
831 comment_end = i;
832 }
833 if (comment_end)
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100834 printf("%s%s %s%s", c_reset,
Jeff King8dbf3eb2015-05-27 16:48:46 -0400835 c_context, c_reset,
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100836 c_func);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700837 for (i = 0; i < comment_end; i++)
838 putchar(hunk_comment[i]);
839 }
840
Junio C Hamano567a03d2006-08-10 00:30:33 -0700841 printf("%s\n", c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800842 while (lno < hunk_end) {
843 struct lline *ll;
844 int j;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800845 unsigned long p_mask;
Benjamin Kramerfd13b212009-03-07 21:02:26 +0100846 struct sline *sl = &sline[lno++];
Antoine Pelisse99d32062013-03-23 18:23:28 +0100847 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800848 while (ll) {
John Keeping41ee2ad2013-02-07 20:15:28 +0000849 printf("%s%s", line_prefix, c_old);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800850 for (j = 0; j < num_parent; j++) {
851 if (ll->parent_map & (1UL<<j))
852 putchar('-');
853 else
854 putchar(' ');
855 }
Junio C Hamano39280972008-08-27 19:48:01 -0700856 show_line_to_eol(ll->line, -1, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800857 ll = ll->next;
858 }
Junio C Hamano74065952006-04-11 14:31:31 -0700859 if (cnt < lno)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700860 break;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800861 p_mask = 1;
John Keeping41ee2ad2013-02-07 20:15:28 +0000862 fputs(line_prefix, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800863 if (!(sl->flag & (mark-1))) {
864 /*
865 * This sline was here to hang the
866 * lost lines in front of it.
867 */
868 if (!context)
869 continue;
Jeff King8dbf3eb2015-05-27 16:48:46 -0400870 fputs(c_context, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800871 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700872 else
873 fputs(c_new, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800874 for (j = 0; j < num_parent; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800875 if (p_mask & sl->flag)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800876 putchar('+');
Junio C Hamano46dc9412006-02-02 15:17:42 -0800877 else
878 putchar(' ');
879 p_mask <<= 1;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800880 }
Junio C Hamano39280972008-08-27 19:48:01 -0700881 show_line_to_eol(sl->bol, sl->len, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800882 }
883 }
884}
885
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800886static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
887 int i, int j)
888{
889 /* We have already examined parent j and we know parent i
890 * and parent j are the same, so reuse the combined result
891 * of parent j for parent i.
892 */
893 unsigned long lno, imask, jmask;
894 imask = (1UL<<i);
895 jmask = (1UL<<j);
896
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700897 for (lno = 0; lno <= cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100898 struct lline *ll = sline->lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800899 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800900 while (ll) {
901 if (ll->parent_map & jmask)
902 ll->parent_map |= imask;
903 ll = ll->next;
904 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800905 if (sline->flag & jmask)
906 sline->flag |= imask;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800907 sline++;
908 }
Junio C Hamano44627312006-02-06 18:54:08 -0800909 /* the overall size of the file (sline[cnt]) */
910 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800911}
912
Junio C Hamano462a15b2007-12-26 16:51:19 -0800913static void dump_quoted_path(const char *head,
914 const char *prefix,
915 const char *path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000916 const char *line_prefix,
Junio C Hamano567a03d2006-08-10 00:30:33 -0700917 const char *c_meta, const char *c_reset)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800918{
Junio C Hamano462a15b2007-12-26 16:51:19 -0800919 static struct strbuf buf = STRBUF_INIT;
920
921 strbuf_reset(&buf);
John Keeping41ee2ad2013-02-07 20:15:28 +0000922 strbuf_addstr(&buf, line_prefix);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800923 strbuf_addstr(&buf, c_meta);
924 strbuf_addstr(&buf, head);
Junio C Hamanod5625092007-12-26 17:13:36 -0800925 quote_two_c_style(&buf, prefix, path, 0);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800926 strbuf_addstr(&buf, c_reset);
927 puts(buf.buf);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700928}
929
Jeff King7c978a02011-05-23 16:16:41 -0400930static void show_combined_header(struct combine_diff_path *elem,
931 int num_parent,
Jeff King7c978a02011-05-23 16:16:41 -0400932 struct rev_info *rev,
John Keeping41ee2ad2013-02-07 20:15:28 +0000933 const char *line_prefix,
Jeff King4d5f3472011-05-23 16:27:34 -0400934 int mode_differs,
935 int show_file_header)
Jeff King7c978a02011-05-23 16:16:41 -0400936{
937 struct diff_options *opt = &rev->diffopt;
brian m. carlson976ff7e2019-08-18 20:04:12 +0000938 int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV;
Jeff King7c978a02011-05-23 16:16:41 -0400939 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
940 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
Jeff Kingf1c96262011-08-17 22:03:12 -0700941 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
942 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
Jeff King7c978a02011-05-23 16:16:41 -0400943 const char *abb;
944 int added = 0;
945 int deleted = 0;
946 int i;
Sergey Organovd01141d2020-09-29 14:31:22 +0300947 int dense = rev->dense_combined_merges;
Jeff King7c978a02011-05-23 16:16:41 -0400948
949 if (rev->loginfo && !rev->no_commit_id)
950 show_log(rev);
951
952 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
John Keeping41ee2ad2013-02-07 20:15:28 +0000953 "", elem->path, line_prefix, c_meta, c_reset);
954 printf("%s%sindex ", line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400955 for (i = 0; i < num_parent; i++) {
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200956 abb = repo_find_unique_abbrev(the_repository,
957 &elem->parent[i].oid, abbrev);
Jeff King7c978a02011-05-23 16:16:41 -0400958 printf("%s%s", i ? "," : "", abb);
959 }
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200960 abb = repo_find_unique_abbrev(the_repository, &elem->oid, abbrev);
Jeff King7c978a02011-05-23 16:16:41 -0400961 printf("..%s%s\n", abb, c_reset);
962
963 if (mode_differs) {
964 deleted = !elem->mode;
965
966 /* We say it was added if nobody had it */
967 added = !deleted;
968 for (i = 0; added && i < num_parent; i++)
969 if (elem->parent[i].status !=
970 DIFF_STATUS_ADDED)
971 added = 0;
972 if (added)
John Keeping41ee2ad2013-02-07 20:15:28 +0000973 printf("%s%snew file mode %06o",
974 line_prefix, c_meta, elem->mode);
Jeff King7c978a02011-05-23 16:16:41 -0400975 else {
976 if (deleted)
John Keeping41ee2ad2013-02-07 20:15:28 +0000977 printf("%s%sdeleted file ",
978 line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400979 printf("mode ");
980 for (i = 0; i < num_parent; i++) {
981 printf("%s%06o", i ? "," : "",
982 elem->parent[i].mode);
983 }
984 if (elem->mode)
985 printf("..%06o", elem->mode);
986 }
987 printf("%s\n", c_reset);
988 }
989
Jeff King4d5f3472011-05-23 16:27:34 -0400990 if (!show_file_header)
991 return;
992
Elijah Newrend76ce4f2019-02-07 17:12:46 -0800993 if (rev->combined_all_paths) {
994 for (i = 0; i < num_parent; i++) {
995 char *path = filename_changed(elem->parent[i].status)
996 ? elem->parent[i].path.buf : elem->path;
997 if (elem->parent[i].status == DIFF_STATUS_ADDED)
998 dump_quoted_path("--- ", "", "/dev/null",
999 line_prefix, c_meta, c_reset);
1000 else
1001 dump_quoted_path("--- ", a_prefix, path,
1002 line_prefix, c_meta, c_reset);
1003 }
1004 } else {
1005 if (added)
1006 dump_quoted_path("--- ", "", "/dev/null",
1007 line_prefix, c_meta, c_reset);
1008 else
1009 dump_quoted_path("--- ", a_prefix, elem->path,
1010 line_prefix, c_meta, c_reset);
1011 }
Jeff King7c978a02011-05-23 16:16:41 -04001012 if (deleted)
1013 dump_quoted_path("+++ ", "", "/dev/null",
John Keeping41ee2ad2013-02-07 20:15:28 +00001014 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -04001015 else
1016 dump_quoted_path("+++ ", b_prefix, elem->path,
John Keeping41ee2ad2013-02-07 20:15:28 +00001017 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -04001018}
1019
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001020static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
Sergey Organovd01141d2020-09-29 14:31:22 +03001021 int working_tree_file,
Junio C Hamano99694542011-08-04 11:39:10 -07001022 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001023{
Linus Torvalds91539832006-04-17 11:59:32 -07001024 struct diff_options *opt = &rev->diffopt;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001025 unsigned long result_size, cnt, lno;
Thomas Rast21798702010-04-15 14:59:37 +02001026 int result_deleted = 0;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -05001027 char *result, *cp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001028 struct sline *sline; /* survived lines */
Junio C Hamano2454c962006-02-06 12:53:07 -08001029 int mode_differs = 0;
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001030 int i, show_hunks;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001031 mmfile_t result_file;
Jeff King4d5f3472011-05-23 16:27:34 -04001032 struct userdiff_driver *userdiff;
Jeff King0508fe52011-05-23 16:31:05 -04001033 struct userdiff_driver *textconv = NULL;
Jeff King4d5f3472011-05-23 16:27:34 -04001034 int is_binary;
John Keeping41ee2ad2013-02-07 20:15:28 +00001035 const char *line_prefix = diff_line_prefix(opt);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001036
Linus Torvaldsee1e5412006-05-13 13:23:48 -07001037 context = opt->context;
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02001038 userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
Jeff King4d5f3472011-05-23 16:27:34 -04001039 if (!userdiff)
1040 userdiff = userdiff_find_by_name("default");
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001041 if (opt->flags.allow_textconv)
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01001042 textconv = userdiff_get_textconv(opt->repo, userdiff);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001043
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001044 /* Read the result of merge first */
Junio C Hamanof23fc772006-04-03 18:53:15 -07001045 if (!working_tree_file)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001046 result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size,
Jeff King0508fe52011-05-23 16:31:05 -04001047 textconv, elem->path);
Junio C Hamanoea726d02006-01-28 00:03:38 -08001048 else {
Junio C Hamano9843a1f2006-02-06 12:30:00 -08001049 /* Used by diff-tree to read from the working tree */
Junio C Hamanoea726d02006-01-28 00:03:38 -08001050 struct stat st;
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001051 int fd = -1;
1052
1053 if (lstat(elem->path, &st) < 0)
1054 goto deleted_file;
1055
1056 if (S_ISLNK(st.st_mode)) {
Junio C Hamano912342d2008-12-17 12:37:58 -08001057 struct strbuf buf = STRBUF_INIT;
1058
1059 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
Nguyễn Thái Ngọc Duy4b94ec92016-05-08 16:47:36 +07001060 error_errno("readlink(%s)", elem->path);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001061 return;
1062 }
Junio C Hamano912342d2008-12-17 12:37:58 -08001063 result_size = buf.len;
1064 result = strbuf_detach(&buf, NULL);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001065 elem->mode = canon_mode(st.st_mode);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001066 } else if (S_ISDIR(st.st_mode)) {
brian m. carlson1ff57c12015-03-13 23:39:33 +00001067 struct object_id oid;
brian m. carlsona98e6102017-10-15 22:07:07 +00001068 if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001069 result = grab_blob(opt->repo, &elem->oid,
1070 elem->mode, &result_size,
1071 NULL, NULL);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001072 else
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001073 result = grab_blob(opt->repo, &oid, elem->mode,
Jeff King0508fe52011-05-23 16:31:05 -04001074 &result_size, NULL, NULL);
1075 } else if (textconv) {
1076 struct diff_filespec *df = alloc_filespec(elem->path);
brian m. carlson14228442021-04-26 01:02:56 +00001077 fill_filespec(df, null_oid(), 0, st.st_mode);
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001078 result_size = fill_textconv(opt->repo, textconv, df, &result);
Jeff King0508fe52011-05-23 16:31:05 -04001079 free_filespec(df);
Kjetil Barvik91fcbcb2009-02-09 21:54:52 +01001080 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001081 size_t len = xsize_t(st.st_size);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001082 ssize_t done;
Johannes Sixta249a9b2007-03-03 20:38:00 +01001083 int is_file, i;
Junio C Hamanoea726d02006-01-28 00:03:38 -08001084
Junio C Hamano1b0c7172006-03-29 22:55:43 -08001085 elem->mode = canon_mode(st.st_mode);
Johannes Sixta249a9b2007-03-03 20:38:00 +01001086 /* if symlinks don't work, assume symlink if all parents
1087 * are symlinks
1088 */
1089 is_file = has_symlinks;
1090 for (i = 0; !is_file && i < num_parent; i++)
1091 is_file = !S_ISLNK(elem->parent[i].mode);
1092 if (!is_file)
1093 elem->mode = canon_mode(S_IFLNK);
1094
Junio C Hamanof23fc772006-04-03 18:53:15 -07001095 result_size = len;
Jeff King3733e692016-02-22 17:44:28 -05001096 result = xmallocz(len);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001097
1098 done = read_in_full(fd, result, len);
1099 if (done < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02001100 die_errno("read error '%s'", elem->path);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001101 else if (done < len)
1102 die("early EOF '%s'", elem->path);
1103
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001104 /* If not a fake symlink, apply filters, e.g. autocrlf */
1105 if (is_file) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001106 struct strbuf buf = STRBUF_INIT;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001107
Nguyễn Thái Ngọc Duy0734f202018-09-21 17:57:20 +02001108 if (convert_to_git(rev->diffopt.repo->index,
1109 elem->path, result, len, &buf, global_conv_flags_eol)) {
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001110 free(result);
1111 result = strbuf_detach(&buf, &len);
1112 result_size = len;
1113 }
1114 }
Junio C Hamanoea726d02006-01-28 00:03:38 -08001115 }
1116 else {
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001117 deleted_file:
Thomas Rast21798702010-04-15 14:59:37 +02001118 result_deleted = 1;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001119 result_size = 0;
Junio C Hamano713a11f2006-02-13 23:07:04 -08001120 elem->mode = 0;
Peter Eriksen28f75812006-07-25 09:30:18 +02001121 result = xcalloc(1, 1);
Junio C Hamanoea726d02006-01-28 00:03:38 -08001122 }
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001123
Junio C Hamanoea726d02006-01-28 00:03:38 -08001124 if (0 <= fd)
1125 close(fd);
1126 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001127
Jeff Kingc95b99b2011-05-23 16:16:59 -04001128 for (i = 0; i < num_parent; i++) {
1129 if (elem->parent[i].mode != elem->mode) {
1130 mode_differs = 1;
1131 break;
1132 }
1133 }
1134
Jeff King0508fe52011-05-23 16:31:05 -04001135 if (textconv)
1136 is_binary = 0;
1137 else if (userdiff->binary != -1)
Jeff King4d5f3472011-05-23 16:27:34 -04001138 is_binary = userdiff->binary;
1139 else {
1140 is_binary = buffer_is_binary(result, result_size);
1141 for (i = 0; !is_binary && i < num_parent; i++) {
1142 char *buf;
1143 unsigned long size;
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001144 buf = grab_blob(opt->repo,
1145 &elem->parent[i].oid,
Jeff King4d5f3472011-05-23 16:27:34 -04001146 elem->parent[i].mode,
Jeff King0508fe52011-05-23 16:31:05 -04001147 &size, NULL, NULL);
Jeff King4d5f3472011-05-23 16:27:34 -04001148 if (buffer_is_binary(buf, size))
1149 is_binary = 1;
1150 free(buf);
1151 }
1152 }
1153 if (is_binary) {
Sergey Organovd01141d2020-09-29 14:31:22 +03001154 show_combined_header(elem, num_parent, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001155 line_prefix, mode_differs, 0);
Jeff King4d5f3472011-05-23 16:27:34 -04001156 printf("Binary files differ\n");
1157 free(result);
1158 return;
1159 }
1160
Junio C Hamano2386c292006-06-28 01:38:19 -07001161 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001162 if (*cp == '\n')
1163 cnt++;
1164 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001165 if (result_size && result[result_size-1] != '\n')
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001166 cnt++; /* incomplete line */
1167
René Scharfeca56dad2021-03-13 17:17:22 +01001168 CALLOC_ARRAY(sline, st_add(cnt, 2));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001169 sline[0].bol = result;
Junio C Hamano2386c292006-06-28 01:38:19 -07001170 for (lno = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001171 if (*cp == '\n') {
1172 sline[lno].len = cp - sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001173 lno++;
1174 if (lno < cnt)
1175 sline[lno].bol = cp + 1;
1176 }
1177 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001178 if (result_size && result[result_size-1] != '\n')
1179 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1180
1181 result_file.ptr = result;
1182 result_file.size = result_size;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001183
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001184 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1185 * for deletion hunk at the end.
1186 */
René Scharfeca56dad2021-03-13 17:17:22 +01001187 CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001188 for (lno = 0; lno <= cnt; lno++)
Junio C Hamanof16706c2006-01-30 22:33:15 -08001189 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1190
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001191 for (i = 0; i < num_parent; i++) {
1192 int j;
1193 for (j = 0; j < i; j++) {
Jeff King4a7e27e2018-08-28 17:22:40 -04001194 if (oideq(&elem->parent[i].oid,
1195 &elem->parent[j].oid)) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001196 reuse_combine_diff(sline, cnt, i, j);
1197 break;
1198 }
1199 }
1200 if (i <= j)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001201 combine_diff(opt->repo,
1202 &elem->parent[i].oid,
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001203 elem->parent[i].mode,
1204 &result_file, sline,
Jeff King0508fe52011-05-23 16:31:05 -04001205 cnt, i, num_parent, result_deleted,
Antoine Pelissefa04ae02013-03-14 22:03:14 +01001206 textconv, elem->path, opt->xdl_opts);
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001207 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001208
Sergey Organovd01141d2020-09-29 14:31:22 +03001209 show_hunks = make_hunks(sline, cnt, num_parent, rev->dense_combined_merges);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001210
Junio C Hamano713a11f2006-02-13 23:07:04 -08001211 if (show_hunks || mode_differs || working_tree_file) {
Sergey Organovd01141d2020-09-29 14:31:22 +03001212 show_combined_header(elem, num_parent, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001213 line_prefix, mode_differs, 1);
1214 dump_sline(sline, line_prefix, cnt, num_parent,
Jeff Kingf1c96262011-08-17 22:03:12 -07001215 opt->use_color, result_deleted);
Junio C Hamano8828cdc2006-01-25 14:26:22 -08001216 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001217 free(result);
1218
Junio C Hamano2386c292006-06-28 01:38:19 -07001219 for (lno = 0; lno < cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +01001220 if (sline[lno].lost) {
1221 struct lline *ll = sline[lno].lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001222 while (ll) {
1223 struct lline *tmp = ll;
1224 ll = ll->next;
1225 free(tmp);
1226 }
1227 }
1228 }
Junio C Hamano46dc9412006-02-02 15:17:42 -08001229 free(sline[0].p_lno);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001230 free(sline);
1231}
1232
Linus Torvalds91539832006-04-17 11:59:32 -07001233static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
Linus Torvaldsee638022006-02-09 10:30:28 -08001234{
Linus Torvalds91539832006-04-17 11:59:32 -07001235 struct diff_options *opt = &rev->diffopt;
Junio C Hamano77667052013-02-03 13:08:18 -08001236 int line_termination, inter_name_termination, i;
John Keeping41ee2ad2013-02-07 20:15:28 +00001237 const char *line_prefix = diff_line_prefix(opt);
Linus Torvaldsee638022006-02-09 10:30:28 -08001238
1239 line_termination = opt->line_termination;
1240 inter_name_termination = '\t';
1241 if (!line_termination)
1242 inter_name_termination = 0;
1243
Junio C Hamano44152782006-10-26 02:05:59 -07001244 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -07001245 show_log(rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001246
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001247
Timo Hirvonenc6744342006-06-24 20:21:53 +03001248 if (opt->output_format & DIFF_FORMAT_RAW) {
John Keeping41ee2ad2013-02-07 20:15:28 +00001249 printf("%s", line_prefix);
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001250
Junio C Hamano77667052013-02-03 13:08:18 -08001251 /* As many colons as there are parents */
1252 for (i = 0; i < num_parent; i++)
1253 putchar(':');
Linus Torvaldsee638022006-02-09 10:30:28 -08001254
Junio C Hamano0a798072006-02-09 15:23:06 -08001255 /* Show the modes */
Junio C Hamano77667052013-02-03 13:08:18 -08001256 for (i = 0; i < num_parent; i++)
1257 printf("%06o ", p->parent[i].mode);
1258 printf("%06o", p->mode);
Junio C Hamano0a798072006-02-09 15:23:06 -08001259
1260 /* Show sha1's */
1261 for (i = 0; i < num_parent; i++)
Jeff Kingd6cece52016-10-20 02:20:07 -04001262 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
Jeff Kingd5e3b012016-10-20 02:19:43 -04001263 opt->abbrev));
Jeff Kingd6cece52016-10-20 02:20:07 -04001264 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
Junio C Hamano0a798072006-02-09 15:23:06 -08001265 }
1266
Timo Hirvonenc6744342006-06-24 20:21:53 +03001267 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamanod416df82006-02-10 02:30:52 -08001268 for (i = 0; i < num_parent; i++)
1269 putchar(p->parent[i].status);
1270 putchar(inter_name_termination);
1271 }
Junio C Hamano0a798072006-02-09 15:23:06 -08001272
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001273 for (i = 0; i < num_parent; i++)
1274 if (rev->combined_all_paths) {
1275 if (filename_changed(p->parent[i].status))
1276 write_name_quoted(p->parent[i].path.buf, stdout,
1277 inter_name_termination);
1278 else
1279 write_name_quoted(p->path, stdout,
1280 inter_name_termination);
1281 }
Pierre Habouzit663af342007-09-20 00:42:15 +02001282 write_name_quoted(p->path, stdout, line_termination);
Junio C Hamano0a798072006-02-09 15:23:06 -08001283}
1284
Junio C Hamano99694542011-08-04 11:39:10 -07001285/*
1286 * The result (p->elem) is from the working tree and their
1287 * parents are typically from multiple stages during a merge
1288 * (i.e. diff-files) or the state in HEAD and in the index
1289 * (i.e. diff-index).
1290 */
Linus Torvalds91539832006-04-17 11:59:32 -07001291void show_combined_diff(struct combine_diff_path *p,
Junio C Hamano0a798072006-02-09 15:23:06 -08001292 int num_parent,
Linus Torvalds91539832006-04-17 11:59:32 -07001293 struct rev_info *rev)
Junio C Hamano0a798072006-02-09 15:23:06 -08001294{
Linus Torvalds91539832006-04-17 11:59:32 -07001295 struct diff_options *opt = &rev->diffopt;
John Keeping41ee2ad2013-02-07 20:15:28 +00001296
Timo Hirvonenc6744342006-06-24 20:21:53 +03001297 if (opt->output_format & (DIFF_FORMAT_RAW |
1298 DIFF_FORMAT_NAME |
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001299 DIFF_FORMAT_NAME_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -07001300 show_raw_diff(p, num_parent, rev);
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001301 else if (opt->output_format & DIFF_FORMAT_PATCH)
Sergey Organovd01141d2020-09-29 14:31:22 +03001302 show_patch_diff(p, num_parent, 1, rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001303}
1304
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001305static void free_combined_pair(struct diff_filepair *pair)
1306{
1307 free(pair->two);
1308 free(pair);
1309}
1310
1311/*
1312 * A combine_diff_path expresses N parents on the LHS against 1 merge
1313 * result. Synthesize a diff_filepair that has N entries on the "one"
1314 * side and 1 entry on the "two" side.
1315 *
1316 * In the future, we might want to add more data to combine_diff_path
1317 * so that we can fill fields we are ignoring (most notably, size) here,
1318 * but currently nobody uses it, so this should suffice for now.
1319 */
1320static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1321 int num_parent)
1322{
1323 int i;
1324 struct diff_filepair *pair;
1325 struct diff_filespec *pool;
1326
1327 pair = xmalloc(sizeof(*pair));
René Scharfeca56dad2021-03-13 17:17:22 +01001328 CALLOC_ARRAY(pool, st_add(num_parent, 1));
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001329 pair->one = pool + 1;
1330 pair->two = pool;
1331
1332 for (i = 0; i < num_parent; i++) {
1333 pair->one[i].path = p->path;
1334 pair->one[i].mode = p->parent[i].mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +00001335 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
brian m. carlson41c95602016-06-24 23:09:24 +00001336 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001337 pair->one[i].has_more_entries = 1;
1338 }
1339 pair->one[num_parent - 1].has_more_entries = 0;
1340
1341 pair->two->path = p->path;
1342 pair->two->mode = p->mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +00001343 oidcpy(&pair->two->oid, &p->oid);
brian m. carlson41c95602016-06-24 23:09:24 +00001344 pair->two->oid_valid = !is_null_oid(&p->oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001345 return pair;
1346}
1347
1348static void handle_combined_callback(struct diff_options *opt,
1349 struct combine_diff_path *paths,
1350 int num_parent,
1351 int num_paths)
1352{
1353 struct combine_diff_path *p;
1354 struct diff_queue_struct q;
1355 int i;
1356
René Scharfeca56dad2021-03-13 17:17:22 +01001357 CALLOC_ARRAY(q.queue, num_paths);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001358 q.alloc = num_paths;
1359 q.nr = num_paths;
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001360 for (i = 0, p = paths; p; p = p->next)
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001361 q.queue[i++] = combined_pair(p, num_parent);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001362 opt->format_callback(&q, opt, opt->format_callback_data);
1363 for (i = 0; i < num_paths; i++)
1364 free_combined_pair(q.queue[i]);
1365 free(q.queue);
1366}
1367
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001368static const char *path_path(void *obj)
1369{
1370 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1371
1372 return path->path;
1373}
1374
Jeff King8817f0c2019-01-24 07:33:51 -05001375/*
1376 * Diff stat formats which we always compute solely against the first parent.
1377 */
1378#define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
Jeff King8290faa2019-01-24 07:34:51 -05001379 | DIFF_FORMAT_SHORTSTAT \
Jeff King04b19fc2019-01-24 07:35:40 -05001380 | DIFF_FORMAT_SUMMARY \
Jeff Kingdac03b52019-01-24 07:36:47 -05001381 | DIFF_FORMAT_DIRSTAT \
Jeff King8817f0c2019-01-24 07:33:51 -05001382 | DIFF_FORMAT_DIFFSTAT)
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001383
1384/* find set of paths that every parent touches */
Brandon Williams09fae192017-05-30 10:30:56 -07001385static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001386 const struct oid_array *parents,
1387 struct diff_options *opt,
1388 int combined_all_paths)
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001389{
1390 struct combine_diff_path *paths = NULL;
1391 int i, num_parent = parents->nr;
1392
1393 int output_format = opt->output_format;
1394 const char *orderfile = opt->orderfile;
1395
1396 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1397 /* tell diff_tree to emit paths in sorted (=tree) order */
1398 opt->orderfile = NULL;
1399
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001400 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001401 for (i = 0; i < num_parent; i++) {
1402 /*
1403 * show stat against the first parent even when doing
1404 * combined diff.
1405 */
Jeff King8817f0c2019-01-24 07:33:51 -05001406 int stat_opt = output_format & STAT_FORMAT_MASK;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001407 if (i == 0 && stat_opt)
1408 opt->output_format = stat_opt;
1409 else
1410 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams66f414f2017-05-30 10:31:03 -07001411 diff_tree_oid(&parents->oid[i], oid, "", opt);
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001412 diffcore_std(opt);
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001413 paths = intersect_paths(paths, i, num_parent,
1414 combined_all_paths);
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001415
1416 /* if showing diff, show it in requested order */
1417 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1418 orderfile) {
1419 diffcore_order(orderfile);
1420 }
1421
1422 diff_flush(opt);
1423 }
1424
1425 opt->output_format = output_format;
1426 opt->orderfile = orderfile;
1427 return paths;
1428}
1429
1430
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001431/*
1432 * find set of paths that everybody touches, assuming diff is run without
1433 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1434 */
1435static struct combine_diff_path *find_paths_multitree(
Brandon Williams09fae192017-05-30 10:30:56 -07001436 const struct object_id *oid, const struct oid_array *parents,
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001437 struct diff_options *opt)
1438{
1439 int i, nparent = parents->nr;
Brandon Williamsfda94b42017-05-30 10:31:06 -07001440 const struct object_id **parents_oid;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001441 struct combine_diff_path paths_head;
1442 struct strbuf base;
1443
Brandon Williamsfda94b42017-05-30 10:31:06 -07001444 ALLOC_ARRAY(parents_oid, nparent);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001445 for (i = 0; i < nparent; i++)
Brandon Williamsfda94b42017-05-30 10:31:06 -07001446 parents_oid[i] = &parents->oid[i];
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001447
1448 /* fake list head, so worker can assume it is non-NULL */
1449 paths_head.next = NULL;
1450
1451 strbuf_init(&base, PATH_MAX);
Brandon Williamsfda94b42017-05-30 10:31:06 -07001452 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001453
1454 strbuf_release(&base);
Brandon Williamsfda94b42017-05-30 10:31:06 -07001455 free(parents_oid);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001456 return paths_head.next;
1457}
1458
Jeff King957876f2020-09-30 07:52:40 -04001459static int match_objfind(struct combine_diff_path *path,
1460 int num_parent,
1461 const struct oidset *set)
1462{
1463 int i;
1464 if (oidset_contains(set, &path->oid))
1465 return 1;
1466 for (i = 0; i < num_parent; i++) {
1467 if (oidset_contains(set, &path->parent[i].oid))
1468 return 1;
1469 }
1470 return 0;
1471}
1472
1473static struct combine_diff_path *combined_objfind(struct diff_options *opt,
1474 struct combine_diff_path *paths,
1475 int num_parent)
1476{
1477 struct combine_diff_path *ret = NULL, **tail = &ret;
1478 struct combine_diff_path *p = paths;
1479
1480 while (p) {
1481 struct combine_diff_path *next = p->next;
1482
1483 if (match_objfind(p, num_parent, opt->objfind)) {
1484 p->next = NULL;
1485 *tail = p;
1486 tail = &p->next;
1487 } else {
1488 free(p);
1489 }
1490 p = next;
1491 }
1492
1493 return ret;
1494}
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001495
Brandon Williamsb9acf542017-05-30 10:30:55 -07001496void diff_tree_combined(const struct object_id *oid,
brian m. carlson910650d2017-03-31 01:40:00 +00001497 const struct oid_array *parents,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001498 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001499{
Linus Torvalds91539832006-04-17 11:59:32 -07001500 struct diff_options *opt = &rev->diffopt;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001501 struct diff_options diffopts;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001502 struct combine_diff_path *p, *paths;
René Scharfe0041f092011-12-17 11:15:48 +01001503 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001504 int need_generic_pathscan;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001505
René Scharfee3d1be42022-06-18 13:12:28 +02001506 if (opt->ignore_regex_nr)
1507 die("combined diff and '%s' cannot be used together",
1508 "--ignore-matching-lines");
René Scharfecfb19ae2022-06-18 13:12:34 +02001509 if (opt->close_file)
1510 die("combined diff and '%s' cannot be used together",
1511 "--output");
René Scharfee3d1be42022-06-18 13:12:28 +02001512
Kirill Smelkov51af1882014-02-03 16:47:19 +04001513 /* nothing to do, if no parents */
1514 if (!num_parent)
1515 return;
1516
1517 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1518 needsep = 0;
1519 if (show_log_first) {
1520 show_log(rev);
1521
Junio C Hamanoe7cc0ed2014-06-06 11:35:01 -07001522 if (rev->verbose_header && opt->output_format &&
Jeff Kingb9c7d6e2014-07-29 13:56:48 -04001523 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1524 !commit_format_is_empty(rev->commit_format))
Kirill Smelkov51af1882014-02-03 16:47:19 +04001525 printf("%s%c", diff_line_prefix(opt),
1526 opt->line_termination);
1527 }
1528
Junio C Hamano5b236832006-02-09 14:35:19 -08001529 diffopts = *opt;
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +07001530 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001531 diffopts.flags.recursive = 1;
1532 diffopts.flags.allow_external = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001533
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001534 /* find set of paths that everybody touches
1535 *
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001536 * NOTE
1537 *
1538 * Diffcore transformations are bound to diff_filespec and logic
1539 * comparing two entries - i.e. they do not apply directly to combine
1540 * diff.
1541 *
1542 * If some of such transformations is requested - we launch generic
1543 * path scanning, which works significantly slower compared to
1544 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1545 *
1546 * TODO some of the filters could be ported to work on
1547 * combine_diff_paths - i.e. all functionality that skips paths, so in
1548 * theory, we could end up having only multitree path scanning.
1549 *
1550 * NOTE please keep this semantically in sync with diffcore_std()
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001551 */
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001552 need_generic_pathscan = opt->skip_stat_unmatch ||
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001553 opt->flags.follow_renames ||
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001554 opt->break_opt != -1 ||
1555 opt->detect_rename ||
Jeff King957876f2020-09-30 07:52:40 -04001556 (opt->pickaxe_opts &
1557 (DIFF_PICKAXE_KINDS_MASK & ~DIFF_PICKAXE_KIND_OBJFIND)) ||
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001558 opt->filter;
1559
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001560 if (need_generic_pathscan) {
1561 /*
1562 * NOTE generic case also handles --stat, as it computes
1563 * diff(sha1,parent_i) for all i to do the job, specifically
1564 * for parent0.
1565 */
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001566 paths = find_paths_generic(oid, parents, &diffopts,
1567 rev->combined_all_paths);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001568 }
1569 else {
1570 int stat_opt;
Brandon Williams09fae192017-05-30 10:30:56 -07001571 paths = find_paths_multitree(oid, parents, &diffopts);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001572
Jeff King957876f2020-09-30 07:52:40 -04001573 if (opt->pickaxe_opts & DIFF_PICKAXE_KIND_OBJFIND)
1574 paths = combined_objfind(opt, paths, num_parent);
1575
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001576 /*
1577 * show stat against the first parent even
1578 * when doing combined diff.
1579 */
Jeff King8817f0c2019-01-24 07:33:51 -05001580 stat_opt = opt->output_format & STAT_FORMAT_MASK;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001581 if (stat_opt) {
1582 diffopts.output_format = stat_opt;
1583
Brandon Williams66f414f2017-05-30 10:31:03 -07001584 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001585 diffcore_std(&diffopts);
1586 if (opt->orderfile)
1587 diffcore_order(opt->orderfile);
1588 diff_flush(&diffopts);
1589 }
1590 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001591
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001592 /* find out number of surviving paths */
1593 for (num_paths = 0, p = paths; p; p = p->next)
1594 num_paths++;
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001595
1596 /* order paths according to diffcore_order */
1597 if (opt->orderfile && num_paths) {
1598 struct obj_order *o;
1599
Jeff Kingb32fa952016-02-22 17:44:25 -05001600 ALLOC_ARRAY(o, num_paths);
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001601 for (i = 0, p = paths; p; p = p->next, i++)
1602 o[i].obj = p;
1603 order_objects(opt->orderfile, path_path, o, num_paths);
1604 for (i = 0; i < num_paths - 1; i++) {
1605 p = o[i].obj;
1606 p->next = o[i+1].obj;
1607 }
1608
1609 p = o[num_paths-1].obj;
1610 p->next = NULL;
1611 paths = o[0].obj;
1612 free(o);
1613 }
1614
1615
Junio C Hamanoe3c3a552006-02-05 22:25:00 -08001616 if (num_paths) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001617 if (opt->output_format & (DIFF_FORMAT_RAW |
1618 DIFF_FORMAT_NAME |
1619 DIFF_FORMAT_NAME_STATUS)) {
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001620 for (p = paths; p; p = p->next)
1621 show_raw_diff(p, num_parent, rev);
Junio C Hamano3969cf72006-06-27 15:08:19 -07001622 needsep = 1;
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001623 }
Jeff King8817f0c2019-01-24 07:33:51 -05001624 else if (opt->output_format & STAT_FORMAT_MASK)
Junio C Hamano3969cf72006-06-27 15:08:19 -07001625 needsep = 1;
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001626 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1627 handle_combined_callback(opt, paths, num_parent, num_paths);
1628
Timo Hirvonenc6744342006-06-24 20:21:53 +03001629 if (opt->output_format & DIFF_FORMAT_PATCH) {
Junio C Hamano3969cf72006-06-27 15:08:19 -07001630 if (needsep)
John Keeping41ee2ad2013-02-07 20:15:28 +00001631 printf("%s%c", diff_line_prefix(opt),
1632 opt->line_termination);
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001633 for (p = paths; p; p = p->next)
Sergey Organovd01141d2020-09-29 14:31:22 +03001634 show_patch_diff(p, num_parent, 0, rev);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001635 }
1636 }
1637
1638 /* Clean things up */
1639 while (paths) {
Junio C Hamanoea726d02006-01-28 00:03:38 -08001640 struct combine_diff_path *tmp = paths;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001641 paths = paths->next;
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001642 for (i = 0; i < num_parent; i++)
1643 if (rev->combined_all_paths &&
1644 filename_changed(tmp->parent[i].status))
1645 strbuf_release(&tmp->parent[i].path);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001646 free(tmp);
1647 }
Clemens Buchacher46ec5102013-05-28 00:49:57 +02001648
Junio C Hamanoed6e8032016-06-02 14:09:22 -07001649 clear_pathspec(&diffopts.pathspec);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001650}
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001651
Sergey Organovd01141d2020-09-29 14:31:22 +03001652void diff_tree_combined_merge(const struct commit *commit,
René Scharfe82889292011-12-17 11:20:07 +01001653 struct rev_info *rev)
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001654{
Thomas Rast53d00b32013-07-31 22:13:20 +02001655 struct commit_list *parent = get_saved_parents(rev, commit);
brian m. carlson910650d2017-03-31 01:40:00 +00001656 struct oid_array parents = OID_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001657
René Scharfe0041f092011-12-17 11:15:48 +01001658 while (parent) {
brian m. carlson910650d2017-03-31 01:40:00 +00001659 oid_array_append(&parents, &parent->item->object.oid);
René Scharfe0041f092011-12-17 11:15:48 +01001660 parent = parent->next;
1661 }
Sergey Organovd01141d2020-09-29 14:31:22 +03001662 diff_tree_combined(&commit->object.oid, &parents, rev);
brian m. carlson910650d2017-03-31 01:40:00 +00001663 oid_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001664}