blob: b0ece9548082c17510b6abd17427434bbf66cae9 [file] [log] [blame]
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001#include "cache.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"
Peter Eriksen8e440252006-04-02 14:44:09 +02004#include "blob.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08005#include "diff.h"
6#include "diffcore.h"
7#include "quote.h"
Junio C Hamanod9ea73e2006-04-05 02:03:58 -07008#include "xdiff-interface.h"
Antoine Pelissefa04ae02013-03-14 22:03:14 +01009#include "xdiff/xmacros.h"
Linus Torvalds91539832006-04-17 11:59:32 -070010#include "log-tree.h"
Junio C Hamano7dae8b22009-04-29 12:49:52 -070011#include "refs.h"
Jeff King4d5f3472011-05-23 16:27:34 -040012#include "userdiff.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040013#include "oid-array.h"
Thomas Rast53d00b32013-07-31 22:13:20 +020014#include "revision.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080015
Jeff Kinge09867f2014-08-19 22:14:30 -040016static int compare_paths(const struct combine_diff_path *one,
17 const struct diff_filespec *two)
18{
19 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
20 return strcmp(one->path, two->path);
21
22 return base_name_compare(one->path, strlen(one->path), one->mode,
23 two->path, strlen(two->path), two->mode);
24}
25
Elijah Newrend76ce4f2019-02-07 17:12:46 -080026static int filename_changed(char status)
27{
28 return status == 'R' || status == 'C';
29}
30
31static struct combine_diff_path *intersect_paths(
32 struct combine_diff_path *curr,
33 int n,
34 int num_parent,
35 int combined_all_paths)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080036{
37 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano7b1004b2014-01-28 13:55:59 -080038 struct combine_diff_path *p, **tail = &curr;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080039 int i, j, cmp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080040
41 if (!n) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080042 for (i = 0; i < q->nr; i++) {
43 int len;
44 const char *path;
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070045 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080046 continue;
47 path = q->queue[i]->two->path;
48 len = strlen(path);
Junio C Hamano2454c962006-02-06 12:53:07 -080049 p = xmalloc(combine_diff_path_size(num_parent, len));
Felipe Contreras4b25d092009-05-01 12:06:36 +030050 p->path = (char *) &(p->parent[num_parent]);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080051 memcpy(p->path, path, len);
52 p->path[len] = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080053 p->next = NULL;
Junio C Hamano2454c962006-02-06 12:53:07 -080054 memset(p->parent, 0,
55 sizeof(p->parent[0]) * num_parent);
56
brian m. carlsona0d12c42016-06-24 23:09:23 +000057 oidcpy(&p->oid, &q->queue[i]->two->oid);
Junio C Hamano2454c962006-02-06 12:53:07 -080058 p->mode = q->queue[i]->two->mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +000059 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
Junio C Hamano2454c962006-02-06 12:53:07 -080060 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080061 p->parent[n].status = q->queue[i]->status;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080062
63 if (combined_all_paths &&
64 filename_changed(p->parent[n].status)) {
65 strbuf_init(&p->parent[n].path, 0);
66 strbuf_addstr(&p->parent[n].path,
67 q->queue[i]->one->path);
68 }
Junio C Hamano5290a0f2006-01-25 03:34:10 -080069 *tail = p;
70 tail = &p->next;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080071 }
Junio C Hamano7b1004b2014-01-28 13:55:59 -080072 return curr;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080073 }
74
Kirill Smelkov8518ff82014-01-20 20:20:40 +040075 /*
Junio C Hamano7b1004b2014-01-28 13:55:59 -080076 * paths in curr (linked list) and q->queue[] (array) are
77 * both sorted in the tree order.
Kirill Smelkov8518ff82014-01-20 20:20:40 +040078 */
Kirill Smelkov8518ff82014-01-20 20:20:40 +040079 i = 0;
Junio C Hamano7b1004b2014-01-28 13:55:59 -080080 while ((p = *tail) != NULL) {
81 cmp = ((i >= q->nr)
Jeff Kinge09867f2014-08-19 22:14:30 -040082 ? -1 : compare_paths(p, q->queue[i]->two));
Kirill Smelkov8518ff82014-01-20 20:20:40 +040083
Kirill Smelkov8518ff82014-01-20 20:20:40 +040084 if (cmp < 0) {
Junio C Hamano7b1004b2014-01-28 13:55:59 -080085 /* p->path not in q->queue[]; drop it */
86 *tail = p->next;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080087 for (j = 0; j < num_parent; j++)
88 if (combined_all_paths &&
89 filename_changed(p->parent[j].status))
90 strbuf_release(&p->parent[j].path);
Junio C Hamano7b1004b2014-01-28 13:55:59 -080091 free(p);
Kirill Smelkov8518ff82014-01-20 20:20:40 +040092 continue;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080093 }
Kirill Smelkov8518ff82014-01-20 20:20:40 +040094
95 if (cmp > 0) {
Junio C Hamano7b1004b2014-01-28 13:55:59 -080096 /* q->queue[i] not in p->path; skip it */
Kirill Smelkov8518ff82014-01-20 20:20:40 +040097 i++;
98 continue;
99 }
100
brian m. carlsona0d12c42016-06-24 23:09:23 +0000101 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400102 p->parent[n].mode = q->queue[i]->one->mode;
103 p->parent[n].status = q->queue[i]->status;
Elijah Newrend76ce4f2019-02-07 17:12:46 -0800104 if (combined_all_paths &&
105 filename_changed(p->parent[n].status))
106 strbuf_addstr(&p->parent[n].path,
107 q->queue[i]->one->path);
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400108
Junio C Hamano7b1004b2014-01-28 13:55:59 -0800109 tail = &p->next;
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400110 i++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800111 }
112 return curr;
113}
114
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800115/* Lines lost from parent */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800116struct lline {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100117 struct lline *next, *prev;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800118 int len;
119 unsigned long parent_map;
120 char line[FLEX_ARRAY];
121};
122
Antoine Pelisse99d32062013-03-23 18:23:28 +0100123/* Lines lost from current parent (before coalescing) */
124struct plost {
125 struct lline *lost_head, *lost_tail;
126 int len;
127};
128
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800129/* Lines surviving in the merge result */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800130struct sline {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100131 /* Accumulated and coalesced lost lines */
132 struct lline *lost;
133 int lenlost;
134 struct plost plost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800135 char *bol;
136 int len;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800137 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
138 * we did not change it).
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800139 * bit N is used for "interesting" lines, including context.
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700140 * bit (N+1) is used for "do not show deletion before this".
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800141 */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800142 unsigned long flag;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800143 unsigned long *p_lno;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800144};
145
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100146static int match_string_spaces(const char *line1, int len1,
147 const char *line2, int len2,
148 long flags)
149{
150 if (flags & XDF_WHITESPACE_FLAGS) {
151 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
152 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
153 }
154
155 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
156 return (len1 == len2 && !memcmp(line1, line2, len1));
157
158 while (len1 > 0 && len2 > 0) {
159 len1--;
160 len2--;
161 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
162 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
163 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
164 return 0;
165
166 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
167 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
168 }
169 if (line1[len1] != line2[len2])
170 return 0;
171 }
172
173 if (flags & XDF_IGNORE_WHITESPACE) {
174 /* Consume remaining spaces */
175 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
176 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
177 }
178
179 /* We matched full line1 and line2 */
180 if (!len1 && !len2)
181 return 1;
182
183 return 0;
184}
185
Antoine Pelisse99d32062013-03-23 18:23:28 +0100186enum coalesce_direction { MATCH, BASE, NEW };
187
188/* Coalesce new lines into base by finding LCS */
189static struct lline *coalesce_lines(struct lline *base, int *lenbase,
Brandon Williams06fffa02018-02-14 10:59:36 -0800190 struct lline *newline, int lennew,
Antoine Pelisse99d32062013-03-23 18:23:28 +0100191 unsigned long parent, long flags)
192{
193 int **lcs;
194 enum coalesce_direction **directions;
195 struct lline *baseend, *newend = NULL;
196 int i, j, origbaselen = *lenbase;
197
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700198 if (!newline)
Antoine Pelisse99d32062013-03-23 18:23:28 +0100199 return base;
200
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700201 if (!base) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100202 *lenbase = lennew;
Brandon Williams06fffa02018-02-14 10:59:36 -0800203 return newline;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100204 }
205
206 /*
207 * Coalesce new lines into base by finding the LCS
Ondřej Bílka98e023d2013-07-29 10:18:21 +0200208 * - Create the table to run dynamic programming
Antoine Pelisse99d32062013-03-23 18:23:28 +0100209 * - Compute the LCS
210 * - Then reverse read the direction structure:
211 * - If we have MATCH, assign parent to base flag, and consume
212 * both baseend and newend
213 * - Else if we have BASE, consume baseend
214 * - Else if we have NEW, insert newend lline into base and
215 * consume newend
216 */
René Scharfeca56dad2021-03-13 17:17:22 +0100217 CALLOC_ARRAY(lcs, st_add(origbaselen, 1));
218 CALLOC_ARRAY(directions, st_add(origbaselen, 1));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100219 for (i = 0; i < origbaselen + 1; i++) {
René Scharfeca56dad2021-03-13 17:17:22 +0100220 CALLOC_ARRAY(lcs[i], st_add(lennew, 1));
221 CALLOC_ARRAY(directions[i], st_add(lennew, 1));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100222 directions[i][0] = BASE;
223 }
224 for (j = 1; j < lennew + 1; j++)
225 directions[0][j] = NEW;
226
227 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
Brandon Williams06fffa02018-02-14 10:59:36 -0800228 for (j = 1, newend = newline; j < lennew + 1; j++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100229 if (match_string_spaces(baseend->line, baseend->len,
230 newend->line, newend->len, flags)) {
231 lcs[i][j] = lcs[i - 1][j - 1] + 1;
232 directions[i][j] = MATCH;
233 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
234 lcs[i][j] = lcs[i][j - 1];
235 directions[i][j] = NEW;
236 } else {
237 lcs[i][j] = lcs[i - 1][j];
238 directions[i][j] = BASE;
239 }
240 if (newend->next)
241 newend = newend->next;
242 }
243 if (baseend->next)
244 baseend = baseend->next;
245 }
246
247 for (i = 0; i < origbaselen + 1; i++)
248 free(lcs[i]);
249 free(lcs);
250
251 /* At this point, baseend and newend point to the end of each lists */
252 i--;
253 j--;
254 while (i != 0 || j != 0) {
255 if (directions[i][j] == MATCH) {
256 baseend->parent_map |= 1<<parent;
257 baseend = baseend->prev;
258 newend = newend->prev;
259 i--;
260 j--;
261 } else if (directions[i][j] == NEW) {
262 struct lline *lline;
263
264 lline = newend;
265 /* Remove lline from new list and update newend */
266 if (lline->prev)
267 lline->prev->next = lline->next;
268 else
Brandon Williams06fffa02018-02-14 10:59:36 -0800269 newline = lline->next;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100270 if (lline->next)
271 lline->next->prev = lline->prev;
272
273 newend = lline->prev;
274 j--;
275
276 /* Add lline to base list */
277 if (baseend) {
278 lline->next = baseend->next;
279 lline->prev = baseend;
280 if (lline->prev)
281 lline->prev->next = lline;
282 }
283 else {
284 lline->next = base;
285 base = lline;
286 }
287 (*lenbase)++;
288
289 if (lline->next)
290 lline->next->prev = lline;
291
292 } else {
293 baseend = baseend->prev;
294 i--;
295 }
296 }
297
Brandon Williams06fffa02018-02-14 10:59:36 -0800298 newend = newline;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100299 while (newend) {
300 struct lline *lline = newend;
301 newend = newend->next;
302 free(lline);
303 }
304
305 for (i = 0; i < origbaselen + 1; i++)
306 free(directions[i]);
307 free(directions);
308
309 return base;
310}
311
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200312static char *grab_blob(struct repository *r,
313 const struct object_id *oid, unsigned int mode,
Antoine Pelisse99d32062013-03-23 18:23:28 +0100314 unsigned long *size, struct userdiff_driver *textconv,
315 const char *path)
316{
317 char *blob;
318 enum object_type type;
319
320 if (S_ISGITLINK(mode)) {
Jeff King0dc3b032017-03-28 15:46:53 -0400321 struct strbuf buf = STRBUF_INIT;
322 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
323 *size = buf.len;
324 blob = strbuf_detach(&buf, NULL);
brian m. carlson1ff57c12015-03-13 23:39:33 +0000325 } else if (is_null_oid(oid)) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100326 /* deleted blob */
327 *size = 0;
328 return xcalloc(1, 1);
329 } else if (textconv) {
330 struct diff_filespec *df = alloc_filespec(path);
Brandon Williamsf9704c22017-05-30 10:30:50 -0700331 fill_filespec(df, oid, 1, mode);
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200332 *size = fill_textconv(r, textconv, df, &blob);
Antoine Pelisse99d32062013-03-23 18:23:28 +0100333 free_filespec(df);
334 } else {
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000335 blob = read_object_file(oid, &type, size);
Antoine Pelisse99d32062013-03-23 18:23:28 +0100336 if (type != OBJ_BLOB)
brian m. carlson1ff57c12015-03-13 23:39:33 +0000337 die("object '%s' is not a blob!", oid_to_hex(oid));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100338 }
339 return blob;
340}
341
342static void append_lost(struct sline *sline, int n, const char *line, int len)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800343{
344 struct lline *lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800345 unsigned long this_mask = (1UL<<n);
346 if (line[len-1] == '\n')
347 len--;
348
Jeff King96ffc062016-02-22 17:44:32 -0500349 FLEX_ALLOC_MEM(lline, line, line, len);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800350 lline->len = len;
351 lline->next = NULL;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100352 lline->prev = sline->plost.lost_tail;
353 if (lline->prev)
354 lline->prev->next = lline;
355 else
356 sline->plost.lost_head = lline;
357 sline->plost.lost_tail = lline;
358 sline->plost.len++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800359 lline->parent_map = this_mask;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800360}
361
Junio C Hamanof23fc772006-04-03 18:53:15 -0700362struct combine_diff_state {
Junio C Hamanoa0fd3142006-04-06 22:29:55 -0700363 unsigned int lno;
364 int ob, on, nb, nn;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700365 unsigned long nmask;
366 int num_parent;
367 int n;
368 struct sline *sline;
369 struct sline *lost_bucket;
370};
371
Jeff King0074c912018-11-02 02:38:20 -0400372static void consume_hunk(void *state_,
373 long ob, long on,
374 long nb, long nn,
375 const char *funcline, long funclen)
376{
377 struct combine_diff_state *state = state_;
378
379 state->ob = ob;
380 state->on = on;
381 state->nb = nb;
382 state->nn = nn;
383 state->lno = state->nb;
384 if (state->nn == 0) {
385 /* @@ -X,Y +N,0 @@ removed Y lines
386 * that would have come *after* line N
387 * in the result. Our lost buckets hang
388 * to the line after the removed lines,
389 *
390 * Note that this is correct even when N == 0,
391 * in which case the hunk removes the first
392 * line in the file.
393 */
394 state->lost_bucket = &state->sline[state->nb];
395 if (!state->nb)
396 state->nb = 1;
397 } else {
398 state->lost_bucket = &state->sline[state->nb-1];
399 }
400 if (!state->sline[state->nb-1].p_lno)
René Scharfeca56dad2021-03-13 17:17:22 +0100401 CALLOC_ARRAY(state->sline[state->nb - 1].p_lno,
402 state->num_parent);
Jeff King0074c912018-11-02 02:38:20 -0400403 state->sline[state->nb-1].p_lno[state->n] = state->ob;
404}
405
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200406static int consume_line(void *state_, char *line, unsigned long len)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700407{
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700408 struct combine_diff_state *state = state_;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700409 if (!state->lost_bucket)
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200410 return 0; /* not in any hunk yet */
Junio C Hamanof23fc772006-04-03 18:53:15 -0700411 switch (line[0]) {
412 case '-':
Antoine Pelisse99d32062013-03-23 18:23:28 +0100413 append_lost(state->lost_bucket, state->n, line+1, len-1);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700414 break;
415 case '+':
416 state->sline[state->lno-1].flag |= state->nmask;
417 state->lno++;
418 break;
419 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200420 return 0;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700421}
422
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200423static void combine_diff(struct repository *r,
424 const struct object_id *parent, unsigned int mode,
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700425 mmfile_t *result_file,
Junio C Hamano2386c292006-06-28 01:38:19 -0700426 struct sline *sline, unsigned int cnt, int n,
Jeff King0508fe52011-05-23 16:31:05 -0400427 int num_parent, int result_deleted,
428 struct userdiff_driver *textconv,
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100429 const char *path, long flags)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800430{
Junio C Hamanof23fc772006-04-03 18:53:15 -0700431 unsigned int p_lno, lno;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800432 unsigned long nmask = (1UL << n);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700433 xpparam_t xpp;
434 xdemitconf_t xecfg;
435 mmfile_t parent_file;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700436 struct combine_diff_state state;
437 unsigned long sz;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800438
Thomas Rast21798702010-04-15 14:59:37 +0200439 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800440 return; /* result deleted */
441
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200442 parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700443 parent_file.size = sz;
Brian Downing9ccd0a82008-10-25 15:30:37 +0200444 memset(&xpp, 0, sizeof(xpp));
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100445 xpp.flags = flags;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100446 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700447 memset(&state, 0, sizeof(state));
448 state.nmask = nmask;
449 state.sline = sline;
450 state.lno = 1;
451 state.num_parent = num_parent;
452 state.n = n;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800453
Jeff King0074c912018-11-02 02:38:20 -0400454 if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
455 consume_line, &state, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -0400456 die("unable to generate combined diff for %s",
Junio C Hamano11a458b2015-09-28 15:33:56 -0700457 oid_to_hex(parent));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700458 free(parent_file.ptr);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800459
460 /* Assign line numbers for this parent.
461 *
462 * sline[lno].p_lno[n] records the first line number
463 * (counting from 1) for parent N if the final hunk display
464 * started by showing sline[lno] (possibly showing the lost
465 * lines attached to it first).
466 */
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700467 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
Junio C Hamanof16706c2006-01-30 22:33:15 -0800468 struct lline *ll;
469 sline[lno].p_lno[n] = p_lno;
470
Antoine Pelisse99d32062013-03-23 18:23:28 +0100471 /* Coalesce new lines */
472 if (sline[lno].plost.lost_head) {
473 struct sline *sl = &sline[lno];
474 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
475 sl->plost.lost_head,
476 sl->plost.len, n, flags);
477 sl->plost.lost_head = sl->plost.lost_tail = NULL;
478 sl->plost.len = 0;
479 }
480
Junio C Hamanof16706c2006-01-30 22:33:15 -0800481 /* How many lines would this sline advance the p_lno? */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100482 ll = sline[lno].lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800483 while (ll) {
484 if (ll->parent_map & nmask)
485 p_lno++; /* '-' means parent had it */
486 ll = ll->next;
487 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700488 if (lno < cnt && !(sline[lno].flag & nmask))
Junio C Hamanof16706c2006-01-30 22:33:15 -0800489 p_lno++; /* no '+' means parent had it */
490 }
491 sline[lno].p_lno[n] = p_lno; /* trailer */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800492}
493
494static unsigned long context = 3;
495static char combine_marker = '@';
496
497static int interesting(struct sline *sline, unsigned long all_mask)
498{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800499 /* If some parents lost lines here, or if we have added to
500 * some parent, it is interesting.
501 */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100502 return ((sline->flag & all_mask) || sline->lost);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800503}
504
Junio C Hamano3ec19092006-01-26 03:53:01 -0800505static unsigned long adjust_hunk_tail(struct sline *sline,
506 unsigned long all_mask,
507 unsigned long hunk_begin,
508 unsigned long i)
509{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800510 /* i points at the first uninteresting line. If the last line
511 * of the hunk was interesting only because it has some
512 * deletion, then it is not all that interesting for the
513 * purpose of giving trailing context lines. This is because
514 * we output '-' line and then unmodified sline[i-1] itself in
515 * that case which gives us one extra context line.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800516 */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800517 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800518 i--;
519 return i;
520}
521
Junio C Hamano46dc9412006-02-02 15:17:42 -0800522static unsigned long find_next(struct sline *sline,
523 unsigned long mark,
524 unsigned long i,
525 unsigned long cnt,
Junio C Hamano2386c292006-06-28 01:38:19 -0700526 int look_for_uninteresting)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800527{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800528 /* We have examined up to i-1 and are about to look at i.
529 * Find next interesting or uninteresting line. Here,
530 * "interesting" does not mean interesting(), but marked by
531 * the give_context() function below (i.e. it includes context
532 * lines that are not interesting to interesting() function
533 * that are surrounded by interesting() ones.
534 */
Junio C Hamano74065952006-04-11 14:31:31 -0700535 while (i <= cnt)
Junio C Hamano2386c292006-06-28 01:38:19 -0700536 if (look_for_uninteresting
Junio C Hamano46dc9412006-02-02 15:17:42 -0800537 ? !(sline[i].flag & mark)
538 : (sline[i].flag & mark))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800539 return i;
540 else
541 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700542 return i;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800543}
544
545static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
546{
547 unsigned long all_mask = (1UL<<num_parent) - 1;
548 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700549 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800550 unsigned long i;
551
Junio C Hamano46dc9412006-02-02 15:17:42 -0800552 /* Two groups of interesting lines may have a short gap of
Pavel Roskin82e5a822006-07-10 01:50:18 -0400553 * uninteresting lines. Connect such groups to give them a
Junio C Hamano46dc9412006-02-02 15:17:42 -0800554 * bit of context.
555 *
556 * We first start from what the interesting() function says,
557 * and mark them with "mark", and paint context lines with the
558 * mark. So interesting() would still say false for such context
559 * lines but they are treated as "interesting" in the end.
560 */
561 i = find_next(sline, mark, 0, cnt, 0);
Junio C Hamano74065952006-04-11 14:31:31 -0700562 if (cnt < i)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800563 return 0;
564
Junio C Hamano74065952006-04-11 14:31:31 -0700565 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800566 unsigned long j = (context < i) ? (i - context) : 0;
567 unsigned long k;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800568
569 /* Paint a few lines before the first interesting line. */
Matthijs Kooijmanaac38572013-05-15 19:42:14 +0200570 while (j < i) {
571 if (!(sline[j].flag & mark))
572 sline[j].flag |= no_pre_delete;
573 sline[j++].flag |= mark;
574 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800575
576 again:
Junio C Hamano46dc9412006-02-02 15:17:42 -0800577 /* we know up to i is to be included. where does the
578 * next uninteresting one start?
579 */
580 j = find_next(sline, mark, i, cnt, 1);
Junio C Hamano74065952006-04-11 14:31:31 -0700581 if (cnt < j)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800582 break; /* the rest are all interesting */
583
584 /* lookahead context lines */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800585 k = find_next(sline, mark, j, cnt, 0);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800586 j = adjust_hunk_tail(sline, all_mask, i, j);
587
588 if (k < j + context) {
589 /* k is interesting and [j,k) are not, but
590 * paint them interesting because the gap is small.
591 */
592 while (j < k)
593 sline[j++].flag |= mark;
594 i = k;
595 goto again;
596 }
597
598 /* j is the first uninteresting line and there is
Junio C Hamano46dc9412006-02-02 15:17:42 -0800599 * no overlap beyond it within context lines. Paint
600 * the trailing edge a bit.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800601 */
602 i = k;
Junio C Hamano74065952006-04-11 14:31:31 -0700603 k = (j + context < cnt+1) ? j + context : cnt+1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800604 while (j < k)
605 sline[j++].flag |= mark;
606 }
607 return 1;
608}
609
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800610static int make_hunks(struct sline *sline, unsigned long cnt,
Junio C Hamanod8f47902006-01-24 01:22:04 -0800611 int num_parent, int dense)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800612{
613 unsigned long all_mask = (1UL<<num_parent) - 1;
614 unsigned long mark = (1UL<<num_parent);
615 unsigned long i;
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800616 int has_interesting = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800617
Junio C Hamano74065952006-04-11 14:31:31 -0700618 for (i = 0; i <= cnt; i++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800619 if (interesting(&sline[i], all_mask))
620 sline[i].flag |= mark;
621 else
622 sline[i].flag &= ~mark;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800623 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800624 if (!dense)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800625 return give_context(sline, cnt, num_parent);
Junio C Hamanod8f47902006-01-24 01:22:04 -0800626
Junio C Hamano263eee22006-01-25 13:11:38 -0800627 /* Look at each hunk, and if we have changes from only one
628 * parent, or the changes are the same from all but one
629 * parent, mark that uninteresting.
Junio C Hamanod8f47902006-01-24 01:22:04 -0800630 */
631 i = 0;
Junio C Hamano74065952006-04-11 14:31:31 -0700632 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800633 unsigned long j, hunk_begin, hunk_end;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800634 unsigned long same_diff;
Junio C Hamano74065952006-04-11 14:31:31 -0700635 while (i <= cnt && !(sline[i].flag & mark))
Junio C Hamanod8f47902006-01-24 01:22:04 -0800636 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700637 if (cnt < i)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800638 break; /* No more interesting hunks */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800639 hunk_begin = i;
Junio C Hamano74065952006-04-11 14:31:31 -0700640 for (j = i + 1; j <= cnt; j++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800641 if (!(sline[j].flag & mark)) {
642 /* Look beyond the end to see if there
643 * is an interesting line after this
644 * hunk within context span.
645 */
646 unsigned long la; /* lookahead */
647 int contin = 0;
648 la = adjust_hunk_tail(sline, all_mask,
649 hunk_begin, j);
Junio C Hamano74065952006-04-11 14:31:31 -0700650 la = (la + context < cnt + 1) ?
651 (la + context) : cnt + 1;
René Scharfee5e9b562012-03-24 16:18:46 +0100652 while (la && j <= --la) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800653 if (sline[la].flag & mark) {
654 contin = 1;
655 break;
656 }
657 }
658 if (!contin)
659 break;
660 j = la;
661 }
662 }
663 hunk_end = j;
664
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800665 /* [i..hunk_end) are interesting. Now is it really
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800666 * interesting? We check if there are only two versions
667 * and the result matches one of them. That is, we look
668 * at:
669 * (+) line, which records lines added to which parents;
670 * this line appears in the result.
671 * (-) line, which records from what parents the line
672 * was removed; this line does not appear in the result.
673 * then check the set of parents the result has difference
674 * from, from all lines. If there are lines that has
675 * different set of parents that the result has differences
676 * from, that means we have more than two versions.
677 *
678 * Even when we have only two versions, if the result does
679 * not match any of the parents, the it should be considered
680 * interesting. In such a case, we would have all '+' line.
681 * After passing the above "two versions" test, that would
682 * appear as "the same set of parents" to be "all parents".
Junio C Hamanod8f47902006-01-24 01:22:04 -0800683 */
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800684 same_diff = 0;
685 has_interesting = 0;
686 for (j = i; j < hunk_end && !has_interesting; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800687 unsigned long this_diff = sline[j].flag & all_mask;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100688 struct lline *ll = sline[j].lost;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800689 if (this_diff) {
690 /* This has some changes. Is it the
691 * same as others?
692 */
693 if (!same_diff)
694 same_diff = this_diff;
695 else if (same_diff != this_diff) {
696 has_interesting = 1;
697 break;
698 }
699 }
700 while (ll && !has_interesting) {
701 /* Lost this line from these parents;
702 * who are they? Are they the same?
703 */
704 this_diff = ll->parent_map;
705 if (!same_diff)
706 same_diff = this_diff;
707 else if (same_diff != this_diff) {
708 has_interesting = 1;
709 }
710 ll = ll->next;
711 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800712 }
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800713
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800714 if (!has_interesting && same_diff != all_mask) {
Junio C Hamanod8f47902006-01-24 01:22:04 -0800715 /* This hunk is not that interesting after all */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800716 for (j = hunk_begin; j < hunk_end; j++)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800717 sline[j].flag &= ~mark;
718 }
719 i = hunk_end;
720 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800721
722 has_interesting = give_context(sline, cnt, num_parent);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800723 return has_interesting;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800724}
725
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800726static 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 -0800727{
728 l0 = sline[l0].p_lno[n];
729 l1 = sline[l1].p_lno[n];
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800730 printf(" -%lu,%lu", l0, l1-l0-null_context);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800731}
732
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700733static int hunk_comment_line(const char *bol)
734{
Junio C Hamano7a8ac592006-10-26 02:05:05 -0700735 int ch;
736
737 if (!bol)
738 return 0;
739 ch = *bol & 0xff;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700740 return (isalpha(ch) || ch == '_' || ch == '$');
741}
742
Junio C Hamano39280972008-08-27 19:48:01 -0700743static void show_line_to_eol(const char *line, int len, const char *reset)
744{
745 int saw_cr_at_eol = 0;
746 if (len < 0)
747 len = strlen(line);
748 saw_cr_at_eol = (len && line[len-1] == '\r');
749
750 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
751 reset,
752 saw_cr_at_eol ? "\r" : "");
753}
754
John Keeping41ee2ad2013-02-07 20:15:28 +0000755static void dump_sline(struct sline *sline, const char *line_prefix,
756 unsigned long cnt, int num_parent,
Thomas Rast21798702010-04-15 14:59:37 +0200757 int use_color, int result_deleted)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800758{
759 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700760 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800761 int i;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800762 unsigned long lno = 0;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700763 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100764 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700765 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
766 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
Jeff King8dbf3eb2015-05-27 16:48:46 -0400767 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700768 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800769
Thomas Rast21798702010-04-15 14:59:37 +0200770 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800771 return; /* result deleted */
772
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800773 while (1) {
Junio C Hamano8bc75742006-04-12 13:23:50 -0700774 unsigned long hunk_end;
775 unsigned long rlines;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700776 const char *hunk_comment = NULL;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800777 unsigned long null_context = 0;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700778
779 while (lno <= cnt && !(sline[lno].flag & mark)) {
780 if (hunk_comment_line(sline[lno].bol))
781 hunk_comment = sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800782 lno++;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700783 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700784 if (cnt < lno)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800785 break;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700786 else {
Junio C Hamano74065952006-04-11 14:31:31 -0700787 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700788 if (!(sline[hunk_end].flag & mark))
789 break;
790 }
Junio C Hamano74065952006-04-11 14:31:31 -0700791 rlines = hunk_end - lno;
792 if (cnt < hunk_end)
793 rlines--; /* pointing at the last delete hunk */
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800794
795 if (!context) {
796 /*
797 * Even when running with --unified=0, all
798 * lines in the hunk needs to be processed in
799 * the loop below in order to show the
800 * deletion recorded in lost_head. However,
801 * we do not want to show the resulting line
802 * with all blank context markers in such a
803 * case. Compensate.
804 */
805 unsigned long j;
806 for (j = lno; j < hunk_end; j++)
807 if (!(sline[j].flag & (mark-1)))
808 null_context++;
809 rlines -= null_context;
810 }
811
John Keeping41ee2ad2013-02-07 20:15:28 +0000812 printf("%s%s", line_prefix, c_frag);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800813 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800814 for (i = 0; i < num_parent; i++)
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800815 show_parent_lno(sline, lno, hunk_end, i, null_context);
Junio C Hamano74065952006-04-11 14:31:31 -0700816 printf(" +%lu,%lu ", lno+1, rlines);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800817 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700818
819 if (hunk_comment) {
820 int comment_end = 0;
821 for (i = 0; i < 40; i++) {
822 int ch = hunk_comment[i] & 0xff;
823 if (!ch || ch == '\n')
824 break;
825 if (!isspace(ch))
826 comment_end = i;
827 }
828 if (comment_end)
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100829 printf("%s%s %s%s", c_reset,
Jeff King8dbf3eb2015-05-27 16:48:46 -0400830 c_context, c_reset,
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100831 c_func);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700832 for (i = 0; i < comment_end; i++)
833 putchar(hunk_comment[i]);
834 }
835
Junio C Hamano567a03d2006-08-10 00:30:33 -0700836 printf("%s\n", c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800837 while (lno < hunk_end) {
838 struct lline *ll;
839 int j;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800840 unsigned long p_mask;
Benjamin Kramerfd13b212009-03-07 21:02:26 +0100841 struct sline *sl = &sline[lno++];
Antoine Pelisse99d32062013-03-23 18:23:28 +0100842 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800843 while (ll) {
John Keeping41ee2ad2013-02-07 20:15:28 +0000844 printf("%s%s", line_prefix, c_old);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800845 for (j = 0; j < num_parent; j++) {
846 if (ll->parent_map & (1UL<<j))
847 putchar('-');
848 else
849 putchar(' ');
850 }
Junio C Hamano39280972008-08-27 19:48:01 -0700851 show_line_to_eol(ll->line, -1, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800852 ll = ll->next;
853 }
Junio C Hamano74065952006-04-11 14:31:31 -0700854 if (cnt < lno)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700855 break;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800856 p_mask = 1;
John Keeping41ee2ad2013-02-07 20:15:28 +0000857 fputs(line_prefix, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800858 if (!(sl->flag & (mark-1))) {
859 /*
860 * This sline was here to hang the
861 * lost lines in front of it.
862 */
863 if (!context)
864 continue;
Jeff King8dbf3eb2015-05-27 16:48:46 -0400865 fputs(c_context, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800866 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700867 else
868 fputs(c_new, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800869 for (j = 0; j < num_parent; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800870 if (p_mask & sl->flag)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800871 putchar('+');
Junio C Hamano46dc9412006-02-02 15:17:42 -0800872 else
873 putchar(' ');
874 p_mask <<= 1;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800875 }
Junio C Hamano39280972008-08-27 19:48:01 -0700876 show_line_to_eol(sl->bol, sl->len, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800877 }
878 }
879}
880
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800881static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
882 int i, int j)
883{
884 /* We have already examined parent j and we know parent i
885 * and parent j are the same, so reuse the combined result
886 * of parent j for parent i.
887 */
888 unsigned long lno, imask, jmask;
889 imask = (1UL<<i);
890 jmask = (1UL<<j);
891
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700892 for (lno = 0; lno <= cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100893 struct lline *ll = sline->lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800894 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800895 while (ll) {
896 if (ll->parent_map & jmask)
897 ll->parent_map |= imask;
898 ll = ll->next;
899 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800900 if (sline->flag & jmask)
901 sline->flag |= imask;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800902 sline++;
903 }
Junio C Hamano44627312006-02-06 18:54:08 -0800904 /* the overall size of the file (sline[cnt]) */
905 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800906}
907
Junio C Hamano462a15b2007-12-26 16:51:19 -0800908static void dump_quoted_path(const char *head,
909 const char *prefix,
910 const char *path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000911 const char *line_prefix,
Junio C Hamano567a03d2006-08-10 00:30:33 -0700912 const char *c_meta, const char *c_reset)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800913{
Junio C Hamano462a15b2007-12-26 16:51:19 -0800914 static struct strbuf buf = STRBUF_INIT;
915
916 strbuf_reset(&buf);
John Keeping41ee2ad2013-02-07 20:15:28 +0000917 strbuf_addstr(&buf, line_prefix);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800918 strbuf_addstr(&buf, c_meta);
919 strbuf_addstr(&buf, head);
Junio C Hamanod5625092007-12-26 17:13:36 -0800920 quote_two_c_style(&buf, prefix, path, 0);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800921 strbuf_addstr(&buf, c_reset);
922 puts(buf.buf);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700923}
924
Jeff King7c978a02011-05-23 16:16:41 -0400925static void show_combined_header(struct combine_diff_path *elem,
926 int num_parent,
Jeff King7c978a02011-05-23 16:16:41 -0400927 struct rev_info *rev,
John Keeping41ee2ad2013-02-07 20:15:28 +0000928 const char *line_prefix,
Jeff King4d5f3472011-05-23 16:27:34 -0400929 int mode_differs,
930 int show_file_header)
Jeff King7c978a02011-05-23 16:16:41 -0400931{
932 struct diff_options *opt = &rev->diffopt;
brian m. carlson976ff7e2019-08-18 20:04:12 +0000933 int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV;
Jeff King7c978a02011-05-23 16:16:41 -0400934 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
935 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
Jeff Kingf1c96262011-08-17 22:03:12 -0700936 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
937 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
Jeff King7c978a02011-05-23 16:16:41 -0400938 const char *abb;
939 int added = 0;
940 int deleted = 0;
941 int i;
Sergey Organovd01141d2020-09-29 14:31:22 +0300942 int dense = rev->dense_combined_merges;
Jeff King7c978a02011-05-23 16:16:41 -0400943
944 if (rev->loginfo && !rev->no_commit_id)
945 show_log(rev);
946
947 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
John Keeping41ee2ad2013-02-07 20:15:28 +0000948 "", elem->path, line_prefix, c_meta, c_reset);
949 printf("%s%sindex ", line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400950 for (i = 0; i < num_parent; i++) {
brian m. carlsonaab95832018-03-12 02:27:30 +0000951 abb = find_unique_abbrev(&elem->parent[i].oid,
Jeff King7c978a02011-05-23 16:16:41 -0400952 abbrev);
953 printf("%s%s", i ? "," : "", abb);
954 }
brian m. carlsonaab95832018-03-12 02:27:30 +0000955 abb = find_unique_abbrev(&elem->oid, abbrev);
Jeff King7c978a02011-05-23 16:16:41 -0400956 printf("..%s%s\n", abb, c_reset);
957
958 if (mode_differs) {
959 deleted = !elem->mode;
960
961 /* We say it was added if nobody had it */
962 added = !deleted;
963 for (i = 0; added && i < num_parent; i++)
964 if (elem->parent[i].status !=
965 DIFF_STATUS_ADDED)
966 added = 0;
967 if (added)
John Keeping41ee2ad2013-02-07 20:15:28 +0000968 printf("%s%snew file mode %06o",
969 line_prefix, c_meta, elem->mode);
Jeff King7c978a02011-05-23 16:16:41 -0400970 else {
971 if (deleted)
John Keeping41ee2ad2013-02-07 20:15:28 +0000972 printf("%s%sdeleted file ",
973 line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400974 printf("mode ");
975 for (i = 0; i < num_parent; i++) {
976 printf("%s%06o", i ? "," : "",
977 elem->parent[i].mode);
978 }
979 if (elem->mode)
980 printf("..%06o", elem->mode);
981 }
982 printf("%s\n", c_reset);
983 }
984
Jeff King4d5f3472011-05-23 16:27:34 -0400985 if (!show_file_header)
986 return;
987
Elijah Newrend76ce4f2019-02-07 17:12:46 -0800988 if (rev->combined_all_paths) {
989 for (i = 0; i < num_parent; i++) {
990 char *path = filename_changed(elem->parent[i].status)
991 ? elem->parent[i].path.buf : elem->path;
992 if (elem->parent[i].status == DIFF_STATUS_ADDED)
993 dump_quoted_path("--- ", "", "/dev/null",
994 line_prefix, c_meta, c_reset);
995 else
996 dump_quoted_path("--- ", a_prefix, path,
997 line_prefix, c_meta, c_reset);
998 }
999 } else {
1000 if (added)
1001 dump_quoted_path("--- ", "", "/dev/null",
1002 line_prefix, c_meta, c_reset);
1003 else
1004 dump_quoted_path("--- ", a_prefix, elem->path,
1005 line_prefix, c_meta, c_reset);
1006 }
Jeff King7c978a02011-05-23 16:16:41 -04001007 if (deleted)
1008 dump_quoted_path("+++ ", "", "/dev/null",
John Keeping41ee2ad2013-02-07 20:15:28 +00001009 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -04001010 else
1011 dump_quoted_path("+++ ", b_prefix, elem->path,
John Keeping41ee2ad2013-02-07 20:15:28 +00001012 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -04001013}
1014
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001015static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
Sergey Organovd01141d2020-09-29 14:31:22 +03001016 int working_tree_file,
Junio C Hamano99694542011-08-04 11:39:10 -07001017 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001018{
Linus Torvalds91539832006-04-17 11:59:32 -07001019 struct diff_options *opt = &rev->diffopt;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001020 unsigned long result_size, cnt, lno;
Thomas Rast21798702010-04-15 14:59:37 +02001021 int result_deleted = 0;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -05001022 char *result, *cp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001023 struct sline *sline; /* survived lines */
Junio C Hamano2454c962006-02-06 12:53:07 -08001024 int mode_differs = 0;
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001025 int i, show_hunks;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001026 mmfile_t result_file;
Jeff King4d5f3472011-05-23 16:27:34 -04001027 struct userdiff_driver *userdiff;
Jeff King0508fe52011-05-23 16:31:05 -04001028 struct userdiff_driver *textconv = NULL;
Jeff King4d5f3472011-05-23 16:27:34 -04001029 int is_binary;
John Keeping41ee2ad2013-02-07 20:15:28 +00001030 const char *line_prefix = diff_line_prefix(opt);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001031
Linus Torvaldsee1e5412006-05-13 13:23:48 -07001032 context = opt->context;
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02001033 userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
Jeff King4d5f3472011-05-23 16:27:34 -04001034 if (!userdiff)
1035 userdiff = userdiff_find_by_name("default");
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001036 if (opt->flags.allow_textconv)
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01001037 textconv = userdiff_get_textconv(opt->repo, userdiff);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001038
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001039 /* Read the result of merge first */
Junio C Hamanof23fc772006-04-03 18:53:15 -07001040 if (!working_tree_file)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001041 result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size,
Jeff King0508fe52011-05-23 16:31:05 -04001042 textconv, elem->path);
Junio C Hamanoea726d02006-01-28 00:03:38 -08001043 else {
Junio C Hamano9843a1f2006-02-06 12:30:00 -08001044 /* Used by diff-tree to read from the working tree */
Junio C Hamanoea726d02006-01-28 00:03:38 -08001045 struct stat st;
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001046 int fd = -1;
1047
1048 if (lstat(elem->path, &st) < 0)
1049 goto deleted_file;
1050
1051 if (S_ISLNK(st.st_mode)) {
Junio C Hamano912342d2008-12-17 12:37:58 -08001052 struct strbuf buf = STRBUF_INIT;
1053
1054 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
Nguyễn Thái Ngọc Duy4b94ec92016-05-08 16:47:36 +07001055 error_errno("readlink(%s)", elem->path);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001056 return;
1057 }
Junio C Hamano912342d2008-12-17 12:37:58 -08001058 result_size = buf.len;
1059 result = strbuf_detach(&buf, NULL);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001060 elem->mode = canon_mode(st.st_mode);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001061 } else if (S_ISDIR(st.st_mode)) {
brian m. carlson1ff57c12015-03-13 23:39:33 +00001062 struct object_id oid;
brian m. carlsona98e6102017-10-15 22:07:07 +00001063 if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001064 result = grab_blob(opt->repo, &elem->oid,
1065 elem->mode, &result_size,
1066 NULL, NULL);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001067 else
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001068 result = grab_blob(opt->repo, &oid, elem->mode,
Jeff King0508fe52011-05-23 16:31:05 -04001069 &result_size, NULL, NULL);
1070 } else if (textconv) {
1071 struct diff_filespec *df = alloc_filespec(elem->path);
brian m. carlson14228442021-04-26 01:02:56 +00001072 fill_filespec(df, null_oid(), 0, st.st_mode);
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001073 result_size = fill_textconv(opt->repo, textconv, df, &result);
Jeff King0508fe52011-05-23 16:31:05 -04001074 free_filespec(df);
Kjetil Barvik91fcbcb2009-02-09 21:54:52 +01001075 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001076 size_t len = xsize_t(st.st_size);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001077 ssize_t done;
Johannes Sixta249a9b2007-03-03 20:38:00 +01001078 int is_file, i;
Junio C Hamanoea726d02006-01-28 00:03:38 -08001079
Junio C Hamano1b0c7172006-03-29 22:55:43 -08001080 elem->mode = canon_mode(st.st_mode);
Johannes Sixta249a9b2007-03-03 20:38:00 +01001081 /* if symlinks don't work, assume symlink if all parents
1082 * are symlinks
1083 */
1084 is_file = has_symlinks;
1085 for (i = 0; !is_file && i < num_parent; i++)
1086 is_file = !S_ISLNK(elem->parent[i].mode);
1087 if (!is_file)
1088 elem->mode = canon_mode(S_IFLNK);
1089
Junio C Hamanof23fc772006-04-03 18:53:15 -07001090 result_size = len;
Jeff King3733e692016-02-22 17:44:28 -05001091 result = xmallocz(len);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001092
1093 done = read_in_full(fd, result, len);
1094 if (done < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02001095 die_errno("read error '%s'", elem->path);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001096 else if (done < len)
1097 die("early EOF '%s'", elem->path);
1098
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001099 /* If not a fake symlink, apply filters, e.g. autocrlf */
1100 if (is_file) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001101 struct strbuf buf = STRBUF_INIT;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001102
Nguyễn Thái Ngọc Duy0734f202018-09-21 17:57:20 +02001103 if (convert_to_git(rev->diffopt.repo->index,
1104 elem->path, result, len, &buf, global_conv_flags_eol)) {
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001105 free(result);
1106 result = strbuf_detach(&buf, &len);
1107 result_size = len;
1108 }
1109 }
Junio C Hamanoea726d02006-01-28 00:03:38 -08001110 }
1111 else {
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001112 deleted_file:
Thomas Rast21798702010-04-15 14:59:37 +02001113 result_deleted = 1;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001114 result_size = 0;
Junio C Hamano713a11f2006-02-13 23:07:04 -08001115 elem->mode = 0;
Peter Eriksen28f75812006-07-25 09:30:18 +02001116 result = xcalloc(1, 1);
Junio C Hamanoea726d02006-01-28 00:03:38 -08001117 }
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001118
Junio C Hamanoea726d02006-01-28 00:03:38 -08001119 if (0 <= fd)
1120 close(fd);
1121 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001122
Jeff Kingc95b99b2011-05-23 16:16:59 -04001123 for (i = 0; i < num_parent; i++) {
1124 if (elem->parent[i].mode != elem->mode) {
1125 mode_differs = 1;
1126 break;
1127 }
1128 }
1129
Jeff King0508fe52011-05-23 16:31:05 -04001130 if (textconv)
1131 is_binary = 0;
1132 else if (userdiff->binary != -1)
Jeff King4d5f3472011-05-23 16:27:34 -04001133 is_binary = userdiff->binary;
1134 else {
1135 is_binary = buffer_is_binary(result, result_size);
1136 for (i = 0; !is_binary && i < num_parent; i++) {
1137 char *buf;
1138 unsigned long size;
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001139 buf = grab_blob(opt->repo,
1140 &elem->parent[i].oid,
Jeff King4d5f3472011-05-23 16:27:34 -04001141 elem->parent[i].mode,
Jeff King0508fe52011-05-23 16:31:05 -04001142 &size, NULL, NULL);
Jeff King4d5f3472011-05-23 16:27:34 -04001143 if (buffer_is_binary(buf, size))
1144 is_binary = 1;
1145 free(buf);
1146 }
1147 }
1148 if (is_binary) {
Sergey Organovd01141d2020-09-29 14:31:22 +03001149 show_combined_header(elem, num_parent, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001150 line_prefix, mode_differs, 0);
Jeff King4d5f3472011-05-23 16:27:34 -04001151 printf("Binary files differ\n");
1152 free(result);
1153 return;
1154 }
1155
Junio C Hamano2386c292006-06-28 01:38:19 -07001156 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001157 if (*cp == '\n')
1158 cnt++;
1159 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001160 if (result_size && result[result_size-1] != '\n')
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001161 cnt++; /* incomplete line */
1162
René Scharfeca56dad2021-03-13 17:17:22 +01001163 CALLOC_ARRAY(sline, st_add(cnt, 2));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001164 sline[0].bol = result;
Junio C Hamano2386c292006-06-28 01:38:19 -07001165 for (lno = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001166 if (*cp == '\n') {
1167 sline[lno].len = cp - sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001168 lno++;
1169 if (lno < cnt)
1170 sline[lno].bol = cp + 1;
1171 }
1172 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001173 if (result_size && result[result_size-1] != '\n')
1174 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1175
1176 result_file.ptr = result;
1177 result_file.size = result_size;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001178
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001179 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1180 * for deletion hunk at the end.
1181 */
René Scharfeca56dad2021-03-13 17:17:22 +01001182 CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001183 for (lno = 0; lno <= cnt; lno++)
Junio C Hamanof16706c2006-01-30 22:33:15 -08001184 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1185
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001186 for (i = 0; i < num_parent; i++) {
1187 int j;
1188 for (j = 0; j < i; j++) {
Jeff King4a7e27e2018-08-28 17:22:40 -04001189 if (oideq(&elem->parent[i].oid,
1190 &elem->parent[j].oid)) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001191 reuse_combine_diff(sline, cnt, i, j);
1192 break;
1193 }
1194 }
1195 if (i <= j)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001196 combine_diff(opt->repo,
1197 &elem->parent[i].oid,
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001198 elem->parent[i].mode,
1199 &result_file, sline,
Jeff King0508fe52011-05-23 16:31:05 -04001200 cnt, i, num_parent, result_deleted,
Antoine Pelissefa04ae02013-03-14 22:03:14 +01001201 textconv, elem->path, opt->xdl_opts);
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001202 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001203
Sergey Organovd01141d2020-09-29 14:31:22 +03001204 show_hunks = make_hunks(sline, cnt, num_parent, rev->dense_combined_merges);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001205
Junio C Hamano713a11f2006-02-13 23:07:04 -08001206 if (show_hunks || mode_differs || working_tree_file) {
Sergey Organovd01141d2020-09-29 14:31:22 +03001207 show_combined_header(elem, num_parent, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001208 line_prefix, mode_differs, 1);
1209 dump_sline(sline, line_prefix, cnt, num_parent,
Jeff Kingf1c96262011-08-17 22:03:12 -07001210 opt->use_color, result_deleted);
Junio C Hamano8828cdc2006-01-25 14:26:22 -08001211 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001212 free(result);
1213
Junio C Hamano2386c292006-06-28 01:38:19 -07001214 for (lno = 0; lno < cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +01001215 if (sline[lno].lost) {
1216 struct lline *ll = sline[lno].lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001217 while (ll) {
1218 struct lline *tmp = ll;
1219 ll = ll->next;
1220 free(tmp);
1221 }
1222 }
1223 }
Junio C Hamano46dc9412006-02-02 15:17:42 -08001224 free(sline[0].p_lno);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001225 free(sline);
1226}
1227
Linus Torvalds91539832006-04-17 11:59:32 -07001228static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
Linus Torvaldsee638022006-02-09 10:30:28 -08001229{
Linus Torvalds91539832006-04-17 11:59:32 -07001230 struct diff_options *opt = &rev->diffopt;
Junio C Hamano77667052013-02-03 13:08:18 -08001231 int line_termination, inter_name_termination, i;
John Keeping41ee2ad2013-02-07 20:15:28 +00001232 const char *line_prefix = diff_line_prefix(opt);
Linus Torvaldsee638022006-02-09 10:30:28 -08001233
1234 line_termination = opt->line_termination;
1235 inter_name_termination = '\t';
1236 if (!line_termination)
1237 inter_name_termination = 0;
1238
Junio C Hamano44152782006-10-26 02:05:59 -07001239 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -07001240 show_log(rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001241
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001242
Timo Hirvonenc6744342006-06-24 20:21:53 +03001243 if (opt->output_format & DIFF_FORMAT_RAW) {
John Keeping41ee2ad2013-02-07 20:15:28 +00001244 printf("%s", line_prefix);
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001245
Junio C Hamano77667052013-02-03 13:08:18 -08001246 /* As many colons as there are parents */
1247 for (i = 0; i < num_parent; i++)
1248 putchar(':');
Linus Torvaldsee638022006-02-09 10:30:28 -08001249
Junio C Hamano0a798072006-02-09 15:23:06 -08001250 /* Show the modes */
Junio C Hamano77667052013-02-03 13:08:18 -08001251 for (i = 0; i < num_parent; i++)
1252 printf("%06o ", p->parent[i].mode);
1253 printf("%06o", p->mode);
Junio C Hamano0a798072006-02-09 15:23:06 -08001254
1255 /* Show sha1's */
1256 for (i = 0; i < num_parent; i++)
Jeff Kingd6cece52016-10-20 02:20:07 -04001257 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
Jeff Kingd5e3b012016-10-20 02:19:43 -04001258 opt->abbrev));
Jeff Kingd6cece52016-10-20 02:20:07 -04001259 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
Junio C Hamano0a798072006-02-09 15:23:06 -08001260 }
1261
Timo Hirvonenc6744342006-06-24 20:21:53 +03001262 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamanod416df82006-02-10 02:30:52 -08001263 for (i = 0; i < num_parent; i++)
1264 putchar(p->parent[i].status);
1265 putchar(inter_name_termination);
1266 }
Junio C Hamano0a798072006-02-09 15:23:06 -08001267
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001268 for (i = 0; i < num_parent; i++)
1269 if (rev->combined_all_paths) {
1270 if (filename_changed(p->parent[i].status))
1271 write_name_quoted(p->parent[i].path.buf, stdout,
1272 inter_name_termination);
1273 else
1274 write_name_quoted(p->path, stdout,
1275 inter_name_termination);
1276 }
Pierre Habouzit663af342007-09-20 00:42:15 +02001277 write_name_quoted(p->path, stdout, line_termination);
Junio C Hamano0a798072006-02-09 15:23:06 -08001278}
1279
Junio C Hamano99694542011-08-04 11:39:10 -07001280/*
1281 * The result (p->elem) is from the working tree and their
1282 * parents are typically from multiple stages during a merge
1283 * (i.e. diff-files) or the state in HEAD and in the index
1284 * (i.e. diff-index).
1285 */
Linus Torvalds91539832006-04-17 11:59:32 -07001286void show_combined_diff(struct combine_diff_path *p,
Junio C Hamano0a798072006-02-09 15:23:06 -08001287 int num_parent,
Linus Torvalds91539832006-04-17 11:59:32 -07001288 struct rev_info *rev)
Junio C Hamano0a798072006-02-09 15:23:06 -08001289{
Linus Torvalds91539832006-04-17 11:59:32 -07001290 struct diff_options *opt = &rev->diffopt;
John Keeping41ee2ad2013-02-07 20:15:28 +00001291
Timo Hirvonenc6744342006-06-24 20:21:53 +03001292 if (opt->output_format & (DIFF_FORMAT_RAW |
1293 DIFF_FORMAT_NAME |
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001294 DIFF_FORMAT_NAME_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -07001295 show_raw_diff(p, num_parent, rev);
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001296 else if (opt->output_format & DIFF_FORMAT_PATCH)
Sergey Organovd01141d2020-09-29 14:31:22 +03001297 show_patch_diff(p, num_parent, 1, rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001298}
1299
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001300static void free_combined_pair(struct diff_filepair *pair)
1301{
1302 free(pair->two);
1303 free(pair);
1304}
1305
1306/*
1307 * A combine_diff_path expresses N parents on the LHS against 1 merge
1308 * result. Synthesize a diff_filepair that has N entries on the "one"
1309 * side and 1 entry on the "two" side.
1310 *
1311 * In the future, we might want to add more data to combine_diff_path
1312 * so that we can fill fields we are ignoring (most notably, size) here,
1313 * but currently nobody uses it, so this should suffice for now.
1314 */
1315static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1316 int num_parent)
1317{
1318 int i;
1319 struct diff_filepair *pair;
1320 struct diff_filespec *pool;
1321
1322 pair = xmalloc(sizeof(*pair));
René Scharfeca56dad2021-03-13 17:17:22 +01001323 CALLOC_ARRAY(pool, st_add(num_parent, 1));
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001324 pair->one = pool + 1;
1325 pair->two = pool;
1326
1327 for (i = 0; i < num_parent; i++) {
1328 pair->one[i].path = p->path;
1329 pair->one[i].mode = p->parent[i].mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +00001330 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
brian m. carlson41c95602016-06-24 23:09:24 +00001331 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001332 pair->one[i].has_more_entries = 1;
1333 }
1334 pair->one[num_parent - 1].has_more_entries = 0;
1335
1336 pair->two->path = p->path;
1337 pair->two->mode = p->mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +00001338 oidcpy(&pair->two->oid, &p->oid);
brian m. carlson41c95602016-06-24 23:09:24 +00001339 pair->two->oid_valid = !is_null_oid(&p->oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001340 return pair;
1341}
1342
1343static void handle_combined_callback(struct diff_options *opt,
1344 struct combine_diff_path *paths,
1345 int num_parent,
1346 int num_paths)
1347{
1348 struct combine_diff_path *p;
1349 struct diff_queue_struct q;
1350 int i;
1351
René Scharfeca56dad2021-03-13 17:17:22 +01001352 CALLOC_ARRAY(q.queue, num_paths);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001353 q.alloc = num_paths;
1354 q.nr = num_paths;
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001355 for (i = 0, p = paths; p; p = p->next)
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001356 q.queue[i++] = combined_pair(p, num_parent);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001357 opt->format_callback(&q, opt, opt->format_callback_data);
1358 for (i = 0; i < num_paths; i++)
1359 free_combined_pair(q.queue[i]);
1360 free(q.queue);
1361}
1362
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001363static const char *path_path(void *obj)
1364{
1365 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1366
1367 return path->path;
1368}
1369
Jeff King8817f0c2019-01-24 07:33:51 -05001370/*
1371 * Diff stat formats which we always compute solely against the first parent.
1372 */
1373#define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
Jeff King8290faa2019-01-24 07:34:51 -05001374 | DIFF_FORMAT_SHORTSTAT \
Jeff King04b19fc2019-01-24 07:35:40 -05001375 | DIFF_FORMAT_SUMMARY \
Jeff Kingdac03b52019-01-24 07:36:47 -05001376 | DIFF_FORMAT_DIRSTAT \
Jeff King8817f0c2019-01-24 07:33:51 -05001377 | DIFF_FORMAT_DIFFSTAT)
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001378
1379/* find set of paths that every parent touches */
Brandon Williams09fae192017-05-30 10:30:56 -07001380static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001381 const struct oid_array *parents,
1382 struct diff_options *opt,
1383 int combined_all_paths)
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001384{
1385 struct combine_diff_path *paths = NULL;
1386 int i, num_parent = parents->nr;
1387
1388 int output_format = opt->output_format;
1389 const char *orderfile = opt->orderfile;
1390
1391 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1392 /* tell diff_tree to emit paths in sorted (=tree) order */
1393 opt->orderfile = NULL;
1394
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001395 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001396 for (i = 0; i < num_parent; i++) {
1397 /*
1398 * show stat against the first parent even when doing
1399 * combined diff.
1400 */
Jeff King8817f0c2019-01-24 07:33:51 -05001401 int stat_opt = output_format & STAT_FORMAT_MASK;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001402 if (i == 0 && stat_opt)
1403 opt->output_format = stat_opt;
1404 else
1405 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams66f414f2017-05-30 10:31:03 -07001406 diff_tree_oid(&parents->oid[i], oid, "", opt);
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001407 diffcore_std(opt);
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001408 paths = intersect_paths(paths, i, num_parent,
1409 combined_all_paths);
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001410
1411 /* if showing diff, show it in requested order */
1412 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1413 orderfile) {
1414 diffcore_order(orderfile);
1415 }
1416
1417 diff_flush(opt);
1418 }
1419
1420 opt->output_format = output_format;
1421 opt->orderfile = orderfile;
1422 return paths;
1423}
1424
1425
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001426/*
1427 * find set of paths that everybody touches, assuming diff is run without
1428 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1429 */
1430static struct combine_diff_path *find_paths_multitree(
Brandon Williams09fae192017-05-30 10:30:56 -07001431 const struct object_id *oid, const struct oid_array *parents,
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001432 struct diff_options *opt)
1433{
1434 int i, nparent = parents->nr;
Brandon Williamsfda94b42017-05-30 10:31:06 -07001435 const struct object_id **parents_oid;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001436 struct combine_diff_path paths_head;
1437 struct strbuf base;
1438
Brandon Williamsfda94b42017-05-30 10:31:06 -07001439 ALLOC_ARRAY(parents_oid, nparent);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001440 for (i = 0; i < nparent; i++)
Brandon Williamsfda94b42017-05-30 10:31:06 -07001441 parents_oid[i] = &parents->oid[i];
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001442
1443 /* fake list head, so worker can assume it is non-NULL */
1444 paths_head.next = NULL;
1445
1446 strbuf_init(&base, PATH_MAX);
Brandon Williamsfda94b42017-05-30 10:31:06 -07001447 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001448
1449 strbuf_release(&base);
Brandon Williamsfda94b42017-05-30 10:31:06 -07001450 free(parents_oid);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001451 return paths_head.next;
1452}
1453
Jeff King957876f2020-09-30 07:52:40 -04001454static int match_objfind(struct combine_diff_path *path,
1455 int num_parent,
1456 const struct oidset *set)
1457{
1458 int i;
1459 if (oidset_contains(set, &path->oid))
1460 return 1;
1461 for (i = 0; i < num_parent; i++) {
1462 if (oidset_contains(set, &path->parent[i].oid))
1463 return 1;
1464 }
1465 return 0;
1466}
1467
1468static struct combine_diff_path *combined_objfind(struct diff_options *opt,
1469 struct combine_diff_path *paths,
1470 int num_parent)
1471{
1472 struct combine_diff_path *ret = NULL, **tail = &ret;
1473 struct combine_diff_path *p = paths;
1474
1475 while (p) {
1476 struct combine_diff_path *next = p->next;
1477
1478 if (match_objfind(p, num_parent, opt->objfind)) {
1479 p->next = NULL;
1480 *tail = p;
1481 tail = &p->next;
1482 } else {
1483 free(p);
1484 }
1485 p = next;
1486 }
1487
1488 return ret;
1489}
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001490
Brandon Williamsb9acf542017-05-30 10:30:55 -07001491void diff_tree_combined(const struct object_id *oid,
brian m. carlson910650d2017-03-31 01:40:00 +00001492 const struct oid_array *parents,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001493 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001494{
Linus Torvalds91539832006-04-17 11:59:32 -07001495 struct diff_options *opt = &rev->diffopt;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001496 struct diff_options diffopts;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001497 struct combine_diff_path *p, *paths;
René Scharfe0041f092011-12-17 11:15:48 +01001498 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001499 int need_generic_pathscan;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001500
René Scharfee3d1be42022-06-18 13:12:28 +02001501 if (opt->ignore_regex_nr)
1502 die("combined diff and '%s' cannot be used together",
1503 "--ignore-matching-lines");
René Scharfecfb19ae2022-06-18 13:12:34 +02001504 if (opt->close_file)
1505 die("combined diff and '%s' cannot be used together",
1506 "--output");
René Scharfee3d1be42022-06-18 13:12:28 +02001507
Kirill Smelkov51af1882014-02-03 16:47:19 +04001508 /* nothing to do, if no parents */
1509 if (!num_parent)
1510 return;
1511
1512 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1513 needsep = 0;
1514 if (show_log_first) {
1515 show_log(rev);
1516
Junio C Hamanoe7cc0ed2014-06-06 11:35:01 -07001517 if (rev->verbose_header && opt->output_format &&
Jeff Kingb9c7d6e2014-07-29 13:56:48 -04001518 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1519 !commit_format_is_empty(rev->commit_format))
Kirill Smelkov51af1882014-02-03 16:47:19 +04001520 printf("%s%c", diff_line_prefix(opt),
1521 opt->line_termination);
1522 }
1523
Junio C Hamano5b236832006-02-09 14:35:19 -08001524 diffopts = *opt;
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +07001525 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001526 diffopts.flags.recursive = 1;
1527 diffopts.flags.allow_external = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001528
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001529 /* find set of paths that everybody touches
1530 *
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001531 * NOTE
1532 *
1533 * Diffcore transformations are bound to diff_filespec and logic
1534 * comparing two entries - i.e. they do not apply directly to combine
1535 * diff.
1536 *
1537 * If some of such transformations is requested - we launch generic
1538 * path scanning, which works significantly slower compared to
1539 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1540 *
1541 * TODO some of the filters could be ported to work on
1542 * combine_diff_paths - i.e. all functionality that skips paths, so in
1543 * theory, we could end up having only multitree path scanning.
1544 *
1545 * NOTE please keep this semantically in sync with diffcore_std()
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001546 */
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001547 need_generic_pathscan = opt->skip_stat_unmatch ||
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001548 opt->flags.follow_renames ||
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001549 opt->break_opt != -1 ||
1550 opt->detect_rename ||
Jeff King957876f2020-09-30 07:52:40 -04001551 (opt->pickaxe_opts &
1552 (DIFF_PICKAXE_KINDS_MASK & ~DIFF_PICKAXE_KIND_OBJFIND)) ||
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001553 opt->filter;
1554
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001555 if (need_generic_pathscan) {
1556 /*
1557 * NOTE generic case also handles --stat, as it computes
1558 * diff(sha1,parent_i) for all i to do the job, specifically
1559 * for parent0.
1560 */
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001561 paths = find_paths_generic(oid, parents, &diffopts,
1562 rev->combined_all_paths);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001563 }
1564 else {
1565 int stat_opt;
Brandon Williams09fae192017-05-30 10:30:56 -07001566 paths = find_paths_multitree(oid, parents, &diffopts);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001567
Jeff King957876f2020-09-30 07:52:40 -04001568 if (opt->pickaxe_opts & DIFF_PICKAXE_KIND_OBJFIND)
1569 paths = combined_objfind(opt, paths, num_parent);
1570
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001571 /*
1572 * show stat against the first parent even
1573 * when doing combined diff.
1574 */
Jeff King8817f0c2019-01-24 07:33:51 -05001575 stat_opt = opt->output_format & STAT_FORMAT_MASK;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001576 if (stat_opt) {
1577 diffopts.output_format = stat_opt;
1578
Brandon Williams66f414f2017-05-30 10:31:03 -07001579 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001580 diffcore_std(&diffopts);
1581 if (opt->orderfile)
1582 diffcore_order(opt->orderfile);
1583 diff_flush(&diffopts);
1584 }
1585 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001586
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001587 /* find out number of surviving paths */
1588 for (num_paths = 0, p = paths; p; p = p->next)
1589 num_paths++;
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001590
1591 /* order paths according to diffcore_order */
1592 if (opt->orderfile && num_paths) {
1593 struct obj_order *o;
1594
Jeff Kingb32fa952016-02-22 17:44:25 -05001595 ALLOC_ARRAY(o, num_paths);
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001596 for (i = 0, p = paths; p; p = p->next, i++)
1597 o[i].obj = p;
1598 order_objects(opt->orderfile, path_path, o, num_paths);
1599 for (i = 0; i < num_paths - 1; i++) {
1600 p = o[i].obj;
1601 p->next = o[i+1].obj;
1602 }
1603
1604 p = o[num_paths-1].obj;
1605 p->next = NULL;
1606 paths = o[0].obj;
1607 free(o);
1608 }
1609
1610
Junio C Hamanoe3c3a552006-02-05 22:25:00 -08001611 if (num_paths) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001612 if (opt->output_format & (DIFF_FORMAT_RAW |
1613 DIFF_FORMAT_NAME |
1614 DIFF_FORMAT_NAME_STATUS)) {
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001615 for (p = paths; p; p = p->next)
1616 show_raw_diff(p, num_parent, rev);
Junio C Hamano3969cf72006-06-27 15:08:19 -07001617 needsep = 1;
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001618 }
Jeff King8817f0c2019-01-24 07:33:51 -05001619 else if (opt->output_format & STAT_FORMAT_MASK)
Junio C Hamano3969cf72006-06-27 15:08:19 -07001620 needsep = 1;
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001621 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1622 handle_combined_callback(opt, paths, num_parent, num_paths);
1623
Timo Hirvonenc6744342006-06-24 20:21:53 +03001624 if (opt->output_format & DIFF_FORMAT_PATCH) {
Junio C Hamano3969cf72006-06-27 15:08:19 -07001625 if (needsep)
John Keeping41ee2ad2013-02-07 20:15:28 +00001626 printf("%s%c", diff_line_prefix(opt),
1627 opt->line_termination);
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001628 for (p = paths; p; p = p->next)
Sergey Organovd01141d2020-09-29 14:31:22 +03001629 show_patch_diff(p, num_parent, 0, rev);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001630 }
1631 }
1632
1633 /* Clean things up */
1634 while (paths) {
Junio C Hamanoea726d02006-01-28 00:03:38 -08001635 struct combine_diff_path *tmp = paths;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001636 paths = paths->next;
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001637 for (i = 0; i < num_parent; i++)
1638 if (rev->combined_all_paths &&
1639 filename_changed(tmp->parent[i].status))
1640 strbuf_release(&tmp->parent[i].path);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001641 free(tmp);
1642 }
Clemens Buchacher46ec5102013-05-28 00:49:57 +02001643
Junio C Hamanoed6e8032016-06-02 14:09:22 -07001644 clear_pathspec(&diffopts.pathspec);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001645}
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001646
Sergey Organovd01141d2020-09-29 14:31:22 +03001647void diff_tree_combined_merge(const struct commit *commit,
René Scharfe82889292011-12-17 11:20:07 +01001648 struct rev_info *rev)
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001649{
Thomas Rast53d00b32013-07-31 22:13:20 +02001650 struct commit_list *parent = get_saved_parents(rev, commit);
brian m. carlson910650d2017-03-31 01:40:00 +00001651 struct oid_array parents = OID_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001652
René Scharfe0041f092011-12-17 11:15:48 +01001653 while (parent) {
brian m. carlson910650d2017-03-31 01:40:00 +00001654 oid_array_append(&parents, &parent->item->object.oid);
René Scharfe0041f092011-12-17 11:15:48 +01001655 parent = parent->next;
1656 }
Sergey Organovd01141d2020-09-29 14:31:22 +03001657 diff_tree_combined(&commit->object.oid, &parents, rev);
brian m. carlson910650d2017-03-31 01:40:00 +00001658 oid_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001659}