blob: f6b624dc288d72bde9c4947b892a2fca684ca824 [file] [log] [blame]
Patrick Steinhardte7da9382024-06-14 08:50:23 +02001#define USE_THE_REPOSITORY_VARIABLE
2
Elijah Newren5e3f94d2023-04-22 20:17:23 +00003#include "git-compat-util.h"
Elijah Newrena034e912023-05-16 06:34:06 +00004#include "object-store-ll.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08005#include "commit.h"
Elijah Newren73359a92023-04-11 03:00:40 +00006#include "convert.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08007#include "diff.h"
8#include "diffcore.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00009#include "environment.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000010#include "hex.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070011#include "object-name.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080012#include "quote.h"
Junio C Hamanod9ea73e2006-04-05 02:03:58 -070013#include "xdiff-interface.h"
Antoine Pelissefa04ae02013-03-14 22:03:14 +010014#include "xdiff/xmacros.h"
Linus Torvalds91539832006-04-17 11:59:32 -070015#include "log-tree.h"
Junio C Hamano7dae8b22009-04-29 12:49:52 -070016#include "refs.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +000017#include "tree.h"
Jeff King4d5f3472011-05-23 16:27:34 -040018#include "userdiff.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040019#include "oid-array.h"
Thomas Rast53d00b32013-07-31 22:13:20 +020020#include "revision.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080021
Jeff Kinge09867f2014-08-19 22:14:30 -040022static int compare_paths(const struct combine_diff_path *one,
23 const struct diff_filespec *two)
24{
25 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
26 return strcmp(one->path, two->path);
27
28 return base_name_compare(one->path, strlen(one->path), one->mode,
29 two->path, strlen(two->path), two->mode);
30}
31
Elijah Newrend76ce4f2019-02-07 17:12:46 -080032static int filename_changed(char status)
33{
34 return status == 'R' || status == 'C';
35}
36
37static struct combine_diff_path *intersect_paths(
38 struct combine_diff_path *curr,
39 int n,
40 int num_parent,
41 int combined_all_paths)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080042{
43 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano7b1004b2014-01-28 13:55:59 -080044 struct combine_diff_path *p, **tail = &curr;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080045 int i, j, cmp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080046
47 if (!n) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080048 for (i = 0; i < q->nr; i++) {
49 int len;
50 const char *path;
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070051 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080052 continue;
53 path = q->queue[i]->two->path;
54 len = strlen(path);
Junio C Hamano2454c962006-02-06 12:53:07 -080055 p = xmalloc(combine_diff_path_size(num_parent, len));
Felipe Contreras4b25d092009-05-01 12:06:36 +030056 p->path = (char *) &(p->parent[num_parent]);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080057 memcpy(p->path, path, len);
58 p->path[len] = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080059 p->next = NULL;
Junio C Hamano2454c962006-02-06 12:53:07 -080060 memset(p->parent, 0,
61 sizeof(p->parent[0]) * num_parent);
62
brian m. carlsona0d12c42016-06-24 23:09:23 +000063 oidcpy(&p->oid, &q->queue[i]->two->oid);
Junio C Hamano2454c962006-02-06 12:53:07 -080064 p->mode = q->queue[i]->two->mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +000065 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
Junio C Hamano2454c962006-02-06 12:53:07 -080066 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080067 p->parent[n].status = q->queue[i]->status;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080068
69 if (combined_all_paths &&
70 filename_changed(p->parent[n].status)) {
71 strbuf_init(&p->parent[n].path, 0);
72 strbuf_addstr(&p->parent[n].path,
73 q->queue[i]->one->path);
74 }
Junio C Hamano5290a0f2006-01-25 03:34:10 -080075 *tail = p;
76 tail = &p->next;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080077 }
Junio C Hamano7b1004b2014-01-28 13:55:59 -080078 return curr;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080079 }
80
Kirill Smelkov8518ff82014-01-20 20:20:40 +040081 /*
Junio C Hamano7b1004b2014-01-28 13:55:59 -080082 * paths in curr (linked list) and q->queue[] (array) are
83 * both sorted in the tree order.
Kirill Smelkov8518ff82014-01-20 20:20:40 +040084 */
Kirill Smelkov8518ff82014-01-20 20:20:40 +040085 i = 0;
Junio C Hamano7b1004b2014-01-28 13:55:59 -080086 while ((p = *tail) != NULL) {
87 cmp = ((i >= q->nr)
Jeff Kinge09867f2014-08-19 22:14:30 -040088 ? -1 : compare_paths(p, q->queue[i]->two));
Kirill Smelkov8518ff82014-01-20 20:20:40 +040089
Kirill Smelkov8518ff82014-01-20 20:20:40 +040090 if (cmp < 0) {
Junio C Hamano7b1004b2014-01-28 13:55:59 -080091 /* p->path not in q->queue[]; drop it */
92 *tail = p->next;
Elijah Newrend76ce4f2019-02-07 17:12:46 -080093 for (j = 0; j < num_parent; j++)
94 if (combined_all_paths &&
95 filename_changed(p->parent[j].status))
96 strbuf_release(&p->parent[j].path);
Junio C Hamano7b1004b2014-01-28 13:55:59 -080097 free(p);
Kirill Smelkov8518ff82014-01-20 20:20:40 +040098 continue;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080099 }
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400100
101 if (cmp > 0) {
Junio C Hamano7b1004b2014-01-28 13:55:59 -0800102 /* q->queue[i] not in p->path; skip it */
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400103 i++;
104 continue;
105 }
106
brian m. carlsona0d12c42016-06-24 23:09:23 +0000107 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400108 p->parent[n].mode = q->queue[i]->one->mode;
109 p->parent[n].status = q->queue[i]->status;
Elijah Newrend76ce4f2019-02-07 17:12:46 -0800110 if (combined_all_paths &&
111 filename_changed(p->parent[n].status))
112 strbuf_addstr(&p->parent[n].path,
113 q->queue[i]->one->path);
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400114
Junio C Hamano7b1004b2014-01-28 13:55:59 -0800115 tail = &p->next;
Kirill Smelkov8518ff82014-01-20 20:20:40 +0400116 i++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800117 }
118 return curr;
119}
120
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800121/* Lines lost from parent */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800122struct lline {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100123 struct lline *next, *prev;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800124 int len;
125 unsigned long parent_map;
126 char line[FLEX_ARRAY];
127};
128
Antoine Pelisse99d32062013-03-23 18:23:28 +0100129/* Lines lost from current parent (before coalescing) */
130struct plost {
131 struct lline *lost_head, *lost_tail;
132 int len;
133};
134
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800135/* Lines surviving in the merge result */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800136struct sline {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100137 /* Accumulated and coalesced lost lines */
138 struct lline *lost;
139 int lenlost;
140 struct plost plost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800141 char *bol;
142 int len;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800143 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
144 * we did not change it).
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800145 * bit N is used for "interesting" lines, including context.
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700146 * bit (N+1) is used for "do not show deletion before this".
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800147 */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800148 unsigned long flag;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800149 unsigned long *p_lno;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800150};
151
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100152static int match_string_spaces(const char *line1, int len1,
153 const char *line2, int len2,
154 long flags)
155{
156 if (flags & XDF_WHITESPACE_FLAGS) {
157 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
158 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
159 }
160
161 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
162 return (len1 == len2 && !memcmp(line1, line2, len1));
163
164 while (len1 > 0 && len2 > 0) {
165 len1--;
166 len2--;
167 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
168 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
169 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
170 return 0;
171
172 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
173 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
174 }
175 if (line1[len1] != line2[len2])
176 return 0;
177 }
178
179 if (flags & XDF_IGNORE_WHITESPACE) {
180 /* Consume remaining spaces */
181 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
182 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
183 }
184
185 /* We matched full line1 and line2 */
186 if (!len1 && !len2)
187 return 1;
188
189 return 0;
190}
191
Antoine Pelisse99d32062013-03-23 18:23:28 +0100192enum coalesce_direction { MATCH, BASE, NEW };
193
194/* Coalesce new lines into base by finding LCS */
195static struct lline *coalesce_lines(struct lline *base, int *lenbase,
Brandon Williams06fffa02018-02-14 10:59:36 -0800196 struct lline *newline, int lennew,
Antoine Pelisse99d32062013-03-23 18:23:28 +0100197 unsigned long parent, long flags)
198{
199 int **lcs;
200 enum coalesce_direction **directions;
201 struct lline *baseend, *newend = NULL;
202 int i, j, origbaselen = *lenbase;
203
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700204 if (!newline)
Antoine Pelisse99d32062013-03-23 18:23:28 +0100205 return base;
206
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700207 if (!base) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100208 *lenbase = lennew;
Brandon Williams06fffa02018-02-14 10:59:36 -0800209 return newline;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100210 }
211
212 /*
213 * Coalesce new lines into base by finding the LCS
Ondřej Bílka98e023d2013-07-29 10:18:21 +0200214 * - Create the table to run dynamic programming
Antoine Pelisse99d32062013-03-23 18:23:28 +0100215 * - Compute the LCS
216 * - Then reverse read the direction structure:
217 * - If we have MATCH, assign parent to base flag, and consume
218 * both baseend and newend
219 * - Else if we have BASE, consume baseend
220 * - Else if we have NEW, insert newend lline into base and
221 * consume newend
222 */
René Scharfeca56dad2021-03-13 17:17:22 +0100223 CALLOC_ARRAY(lcs, st_add(origbaselen, 1));
224 CALLOC_ARRAY(directions, st_add(origbaselen, 1));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100225 for (i = 0; i < origbaselen + 1; i++) {
René Scharfeca56dad2021-03-13 17:17:22 +0100226 CALLOC_ARRAY(lcs[i], st_add(lennew, 1));
227 CALLOC_ARRAY(directions[i], st_add(lennew, 1));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100228 directions[i][0] = BASE;
229 }
230 for (j = 1; j < lennew + 1; j++)
231 directions[0][j] = NEW;
232
233 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
Brandon Williams06fffa02018-02-14 10:59:36 -0800234 for (j = 1, newend = newline; j < lennew + 1; j++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100235 if (match_string_spaces(baseend->line, baseend->len,
236 newend->line, newend->len, flags)) {
237 lcs[i][j] = lcs[i - 1][j - 1] + 1;
238 directions[i][j] = MATCH;
239 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
240 lcs[i][j] = lcs[i][j - 1];
241 directions[i][j] = NEW;
242 } else {
243 lcs[i][j] = lcs[i - 1][j];
244 directions[i][j] = BASE;
245 }
246 if (newend->next)
247 newend = newend->next;
248 }
249 if (baseend->next)
250 baseend = baseend->next;
251 }
252
253 for (i = 0; i < origbaselen + 1; i++)
254 free(lcs[i]);
255 free(lcs);
256
257 /* At this point, baseend and newend point to the end of each lists */
258 i--;
259 j--;
260 while (i != 0 || j != 0) {
261 if (directions[i][j] == MATCH) {
262 baseend->parent_map |= 1<<parent;
263 baseend = baseend->prev;
264 newend = newend->prev;
265 i--;
266 j--;
267 } else if (directions[i][j] == NEW) {
268 struct lline *lline;
269
270 lline = newend;
271 /* Remove lline from new list and update newend */
272 if (lline->prev)
273 lline->prev->next = lline->next;
274 else
Brandon Williams06fffa02018-02-14 10:59:36 -0800275 newline = lline->next;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100276 if (lline->next)
277 lline->next->prev = lline->prev;
278
279 newend = lline->prev;
280 j--;
281
282 /* Add lline to base list */
283 if (baseend) {
284 lline->next = baseend->next;
285 lline->prev = baseend;
286 if (lline->prev)
287 lline->prev->next = lline;
288 }
289 else {
290 lline->next = base;
291 base = lline;
292 }
293 (*lenbase)++;
294
295 if (lline->next)
296 lline->next->prev = lline;
297
298 } else {
299 baseend = baseend->prev;
300 i--;
301 }
302 }
303
Brandon Williams06fffa02018-02-14 10:59:36 -0800304 newend = newline;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100305 while (newend) {
306 struct lline *lline = newend;
307 newend = newend->next;
308 free(lline);
309 }
310
311 for (i = 0; i < origbaselen + 1; i++)
312 free(directions[i]);
313 free(directions);
314
315 return base;
316}
317
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200318static char *grab_blob(struct repository *r,
319 const struct object_id *oid, unsigned int mode,
Antoine Pelisse99d32062013-03-23 18:23:28 +0100320 unsigned long *size, struct userdiff_driver *textconv,
321 const char *path)
322{
323 char *blob;
324 enum object_type type;
325
326 if (S_ISGITLINK(mode)) {
Jeff King0dc3b032017-03-28 15:46:53 -0400327 struct strbuf buf = STRBUF_INIT;
328 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
329 *size = buf.len;
330 blob = strbuf_detach(&buf, NULL);
brian m. carlson1ff57c12015-03-13 23:39:33 +0000331 } else if (is_null_oid(oid)) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100332 /* deleted blob */
333 *size = 0;
334 return xcalloc(1, 1);
335 } else if (textconv) {
336 struct diff_filespec *df = alloc_filespec(path);
Brandon Williamsf9704c22017-05-30 10:30:50 -0700337 fill_filespec(df, oid, 1, mode);
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200338 *size = fill_textconv(r, textconv, df, &blob);
Antoine Pelisse99d32062013-03-23 18:23:28 +0100339 free_filespec(df);
340 } else {
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +0200341 blob = repo_read_object_file(r, oid, &type, size);
Johannes Schindelin568459b2024-02-05 14:35:53 +0000342 if (!blob)
343 die(_("unable to read %s"), oid_to_hex(oid));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100344 if (type != OBJ_BLOB)
brian m. carlson1ff57c12015-03-13 23:39:33 +0000345 die("object '%s' is not a blob!", oid_to_hex(oid));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100346 }
347 return blob;
348}
349
350static void append_lost(struct sline *sline, int n, const char *line, int len)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800351{
352 struct lline *lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800353 unsigned long this_mask = (1UL<<n);
354 if (line[len-1] == '\n')
355 len--;
356
Jeff King96ffc062016-02-22 17:44:32 -0500357 FLEX_ALLOC_MEM(lline, line, line, len);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800358 lline->len = len;
359 lline->next = NULL;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100360 lline->prev = sline->plost.lost_tail;
361 if (lline->prev)
362 lline->prev->next = lline;
363 else
364 sline->plost.lost_head = lline;
365 sline->plost.lost_tail = lline;
366 sline->plost.len++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800367 lline->parent_map = this_mask;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800368}
369
Junio C Hamanof23fc772006-04-03 18:53:15 -0700370struct combine_diff_state {
Junio C Hamanoa0fd3142006-04-06 22:29:55 -0700371 unsigned int lno;
372 int ob, on, nb, nn;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700373 unsigned long nmask;
374 int num_parent;
375 int n;
376 struct sline *sline;
377 struct sline *lost_bucket;
378};
379
Jeff King0074c912018-11-02 02:38:20 -0400380static void consume_hunk(void *state_,
381 long ob, long on,
382 long nb, long nn,
Jeff King61bdc7c2022-12-13 06:13:48 -0500383 const char *func UNUSED, long funclen UNUSED)
Jeff King0074c912018-11-02 02:38:20 -0400384{
385 struct combine_diff_state *state = state_;
386
387 state->ob = ob;
388 state->on = on;
389 state->nb = nb;
390 state->nn = nn;
391 state->lno = state->nb;
392 if (state->nn == 0) {
393 /* @@ -X,Y +N,0 @@ removed Y lines
394 * that would have come *after* line N
395 * in the result. Our lost buckets hang
396 * to the line after the removed lines,
397 *
398 * Note that this is correct even when N == 0,
399 * in which case the hunk removes the first
400 * line in the file.
401 */
402 state->lost_bucket = &state->sline[state->nb];
403 if (!state->nb)
404 state->nb = 1;
405 } else {
406 state->lost_bucket = &state->sline[state->nb-1];
407 }
408 if (!state->sline[state->nb-1].p_lno)
René Scharfeca56dad2021-03-13 17:17:22 +0100409 CALLOC_ARRAY(state->sline[state->nb - 1].p_lno,
410 state->num_parent);
Jeff King0074c912018-11-02 02:38:20 -0400411 state->sline[state->nb-1].p_lno[state->n] = state->ob;
412}
413
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200414static int consume_line(void *state_, char *line, unsigned long len)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700415{
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700416 struct combine_diff_state *state = state_;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700417 if (!state->lost_bucket)
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200418 return 0; /* not in any hunk yet */
Junio C Hamanof23fc772006-04-03 18:53:15 -0700419 switch (line[0]) {
420 case '-':
Antoine Pelisse99d32062013-03-23 18:23:28 +0100421 append_lost(state->lost_bucket, state->n, line+1, len-1);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700422 break;
423 case '+':
424 state->sline[state->lno-1].flag |= state->nmask;
425 state->lno++;
426 break;
427 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +0200428 return 0;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700429}
430
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200431static void combine_diff(struct repository *r,
432 const struct object_id *parent, unsigned int mode,
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700433 mmfile_t *result_file,
Junio C Hamano2386c292006-06-28 01:38:19 -0700434 struct sline *sline, unsigned int cnt, int n,
Jeff King0508fe52011-05-23 16:31:05 -0400435 int num_parent, int result_deleted,
436 struct userdiff_driver *textconv,
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100437 const char *path, long flags)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800438{
Junio C Hamanof23fc772006-04-03 18:53:15 -0700439 unsigned int p_lno, lno;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800440 unsigned long nmask = (1UL << n);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700441 xpparam_t xpp;
442 xdemitconf_t xecfg;
443 mmfile_t parent_file;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700444 struct combine_diff_state state;
445 unsigned long sz;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800446
Thomas Rast21798702010-04-15 14:59:37 +0200447 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800448 return; /* result deleted */
449
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200450 parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700451 parent_file.size = sz;
Brian Downing9ccd0a82008-10-25 15:30:37 +0200452 memset(&xpp, 0, sizeof(xpp));
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100453 xpp.flags = flags;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100454 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700455 memset(&state, 0, sizeof(state));
456 state.nmask = nmask;
457 state.sline = sline;
458 state.lno = 1;
459 state.num_parent = num_parent;
460 state.n = n;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800461
Jeff King0074c912018-11-02 02:38:20 -0400462 if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
463 consume_line, &state, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -0400464 die("unable to generate combined diff for %s",
Junio C Hamano11a458b2015-09-28 15:33:56 -0700465 oid_to_hex(parent));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700466 free(parent_file.ptr);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800467
468 /* Assign line numbers for this parent.
469 *
470 * sline[lno].p_lno[n] records the first line number
471 * (counting from 1) for parent N if the final hunk display
472 * started by showing sline[lno] (possibly showing the lost
473 * lines attached to it first).
474 */
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700475 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
Junio C Hamanof16706c2006-01-30 22:33:15 -0800476 struct lline *ll;
477 sline[lno].p_lno[n] = p_lno;
478
Antoine Pelisse99d32062013-03-23 18:23:28 +0100479 /* Coalesce new lines */
480 if (sline[lno].plost.lost_head) {
481 struct sline *sl = &sline[lno];
482 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
483 sl->plost.lost_head,
484 sl->plost.len, n, flags);
485 sl->plost.lost_head = sl->plost.lost_tail = NULL;
486 sl->plost.len = 0;
487 }
488
Junio C Hamanof16706c2006-01-30 22:33:15 -0800489 /* How many lines would this sline advance the p_lno? */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100490 ll = sline[lno].lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800491 while (ll) {
492 if (ll->parent_map & nmask)
493 p_lno++; /* '-' means parent had it */
494 ll = ll->next;
495 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700496 if (lno < cnt && !(sline[lno].flag & nmask))
Junio C Hamanof16706c2006-01-30 22:33:15 -0800497 p_lno++; /* no '+' means parent had it */
498 }
499 sline[lno].p_lno[n] = p_lno; /* trailer */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800500}
501
502static unsigned long context = 3;
503static char combine_marker = '@';
504
505static int interesting(struct sline *sline, unsigned long all_mask)
506{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800507 /* If some parents lost lines here, or if we have added to
508 * some parent, it is interesting.
509 */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100510 return ((sline->flag & all_mask) || sline->lost);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800511}
512
Junio C Hamano3ec19092006-01-26 03:53:01 -0800513static unsigned long adjust_hunk_tail(struct sline *sline,
514 unsigned long all_mask,
515 unsigned long hunk_begin,
516 unsigned long i)
517{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800518 /* i points at the first uninteresting line. If the last line
519 * of the hunk was interesting only because it has some
520 * deletion, then it is not all that interesting for the
521 * purpose of giving trailing context lines. This is because
522 * we output '-' line and then unmodified sline[i-1] itself in
523 * that case which gives us one extra context line.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800524 */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800525 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800526 i--;
527 return i;
528}
529
Junio C Hamano46dc9412006-02-02 15:17:42 -0800530static unsigned long find_next(struct sline *sline,
531 unsigned long mark,
532 unsigned long i,
533 unsigned long cnt,
Junio C Hamano2386c292006-06-28 01:38:19 -0700534 int look_for_uninteresting)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800535{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800536 /* We have examined up to i-1 and are about to look at i.
537 * Find next interesting or uninteresting line. Here,
538 * "interesting" does not mean interesting(), but marked by
539 * the give_context() function below (i.e. it includes context
540 * lines that are not interesting to interesting() function
541 * that are surrounded by interesting() ones.
542 */
Junio C Hamano74065952006-04-11 14:31:31 -0700543 while (i <= cnt)
Junio C Hamano2386c292006-06-28 01:38:19 -0700544 if (look_for_uninteresting
Junio C Hamano46dc9412006-02-02 15:17:42 -0800545 ? !(sline[i].flag & mark)
546 : (sline[i].flag & mark))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800547 return i;
548 else
549 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700550 return i;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800551}
552
553static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
554{
555 unsigned long all_mask = (1UL<<num_parent) - 1;
556 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700557 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800558 unsigned long i;
559
Junio C Hamano46dc9412006-02-02 15:17:42 -0800560 /* Two groups of interesting lines may have a short gap of
Pavel Roskin82e5a822006-07-10 01:50:18 -0400561 * uninteresting lines. Connect such groups to give them a
Junio C Hamano46dc9412006-02-02 15:17:42 -0800562 * bit of context.
563 *
564 * We first start from what the interesting() function says,
565 * and mark them with "mark", and paint context lines with the
566 * mark. So interesting() would still say false for such context
567 * lines but they are treated as "interesting" in the end.
568 */
569 i = find_next(sline, mark, 0, cnt, 0);
Junio C Hamano74065952006-04-11 14:31:31 -0700570 if (cnt < i)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800571 return 0;
572
Junio C Hamano74065952006-04-11 14:31:31 -0700573 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800574 unsigned long j = (context < i) ? (i - context) : 0;
575 unsigned long k;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800576
577 /* Paint a few lines before the first interesting line. */
Matthijs Kooijmanaac38572013-05-15 19:42:14 +0200578 while (j < i) {
579 if (!(sline[j].flag & mark))
580 sline[j].flag |= no_pre_delete;
581 sline[j++].flag |= mark;
582 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800583
584 again:
Junio C Hamano46dc9412006-02-02 15:17:42 -0800585 /* we know up to i is to be included. where does the
586 * next uninteresting one start?
587 */
588 j = find_next(sline, mark, i, cnt, 1);
Junio C Hamano74065952006-04-11 14:31:31 -0700589 if (cnt < j)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800590 break; /* the rest are all interesting */
591
592 /* lookahead context lines */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800593 k = find_next(sline, mark, j, cnt, 0);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800594 j = adjust_hunk_tail(sline, all_mask, i, j);
595
596 if (k < j + context) {
597 /* k is interesting and [j,k) are not, but
598 * paint them interesting because the gap is small.
599 */
600 while (j < k)
601 sline[j++].flag |= mark;
602 i = k;
603 goto again;
604 }
605
606 /* j is the first uninteresting line and there is
Junio C Hamano46dc9412006-02-02 15:17:42 -0800607 * no overlap beyond it within context lines. Paint
608 * the trailing edge a bit.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800609 */
610 i = k;
Junio C Hamano74065952006-04-11 14:31:31 -0700611 k = (j + context < cnt+1) ? j + context : cnt+1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800612 while (j < k)
613 sline[j++].flag |= mark;
614 }
615 return 1;
616}
617
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800618static int make_hunks(struct sline *sline, unsigned long cnt,
Junio C Hamanod8f47902006-01-24 01:22:04 -0800619 int num_parent, int dense)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800620{
621 unsigned long all_mask = (1UL<<num_parent) - 1;
622 unsigned long mark = (1UL<<num_parent);
623 unsigned long i;
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800624 int has_interesting = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800625
Junio C Hamano74065952006-04-11 14:31:31 -0700626 for (i = 0; i <= cnt; i++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800627 if (interesting(&sline[i], all_mask))
628 sline[i].flag |= mark;
629 else
630 sline[i].flag &= ~mark;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800631 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800632 if (!dense)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800633 return give_context(sline, cnt, num_parent);
Junio C Hamanod8f47902006-01-24 01:22:04 -0800634
Junio C Hamano263eee22006-01-25 13:11:38 -0800635 /* Look at each hunk, and if we have changes from only one
636 * parent, or the changes are the same from all but one
637 * parent, mark that uninteresting.
Junio C Hamanod8f47902006-01-24 01:22:04 -0800638 */
639 i = 0;
Junio C Hamano74065952006-04-11 14:31:31 -0700640 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800641 unsigned long j, hunk_begin, hunk_end;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800642 unsigned long same_diff;
Junio C Hamano74065952006-04-11 14:31:31 -0700643 while (i <= cnt && !(sline[i].flag & mark))
Junio C Hamanod8f47902006-01-24 01:22:04 -0800644 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700645 if (cnt < i)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800646 break; /* No more interesting hunks */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800647 hunk_begin = i;
Junio C Hamano74065952006-04-11 14:31:31 -0700648 for (j = i + 1; j <= cnt; j++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800649 if (!(sline[j].flag & mark)) {
650 /* Look beyond the end to see if there
651 * is an interesting line after this
652 * hunk within context span.
653 */
654 unsigned long la; /* lookahead */
655 int contin = 0;
656 la = adjust_hunk_tail(sline, all_mask,
657 hunk_begin, j);
Junio C Hamano74065952006-04-11 14:31:31 -0700658 la = (la + context < cnt + 1) ?
659 (la + context) : cnt + 1;
René Scharfee5e9b562012-03-24 16:18:46 +0100660 while (la && j <= --la) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800661 if (sline[la].flag & mark) {
662 contin = 1;
663 break;
664 }
665 }
666 if (!contin)
667 break;
668 j = la;
669 }
670 }
671 hunk_end = j;
672
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800673 /* [i..hunk_end) are interesting. Now is it really
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800674 * interesting? We check if there are only two versions
675 * and the result matches one of them. That is, we look
676 * at:
677 * (+) line, which records lines added to which parents;
678 * this line appears in the result.
679 * (-) line, which records from what parents the line
680 * was removed; this line does not appear in the result.
681 * then check the set of parents the result has difference
682 * from, from all lines. If there are lines that has
683 * different set of parents that the result has differences
684 * from, that means we have more than two versions.
685 *
686 * Even when we have only two versions, if the result does
687 * not match any of the parents, the it should be considered
688 * interesting. In such a case, we would have all '+' line.
689 * After passing the above "two versions" test, that would
690 * appear as "the same set of parents" to be "all parents".
Junio C Hamanod8f47902006-01-24 01:22:04 -0800691 */
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800692 same_diff = 0;
693 has_interesting = 0;
694 for (j = i; j < hunk_end && !has_interesting; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800695 unsigned long this_diff = sline[j].flag & all_mask;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100696 struct lline *ll = sline[j].lost;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800697 if (this_diff) {
698 /* This has some changes. Is it the
699 * same as others?
700 */
701 if (!same_diff)
702 same_diff = this_diff;
703 else if (same_diff != this_diff) {
704 has_interesting = 1;
705 break;
706 }
707 }
708 while (ll && !has_interesting) {
709 /* Lost this line from these parents;
710 * who are they? Are they the same?
711 */
712 this_diff = ll->parent_map;
713 if (!same_diff)
714 same_diff = this_diff;
715 else if (same_diff != this_diff) {
716 has_interesting = 1;
717 }
718 ll = ll->next;
719 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800720 }
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800721
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800722 if (!has_interesting && same_diff != all_mask) {
Junio C Hamanod8f47902006-01-24 01:22:04 -0800723 /* This hunk is not that interesting after all */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800724 for (j = hunk_begin; j < hunk_end; j++)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800725 sline[j].flag &= ~mark;
726 }
727 i = hunk_end;
728 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800729
730 has_interesting = give_context(sline, cnt, num_parent);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800731 return has_interesting;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800732}
733
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800734static 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 -0800735{
736 l0 = sline[l0].p_lno[n];
737 l1 = sline[l1].p_lno[n];
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800738 printf(" -%lu,%lu", l0, l1-l0-null_context);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800739}
740
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700741static int hunk_comment_line(const char *bol)
742{
Junio C Hamano7a8ac592006-10-26 02:05:05 -0700743 int ch;
744
745 if (!bol)
746 return 0;
747 ch = *bol & 0xff;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700748 return (isalpha(ch) || ch == '_' || ch == '$');
749}
750
Junio C Hamano39280972008-08-27 19:48:01 -0700751static void show_line_to_eol(const char *line, int len, const char *reset)
752{
753 int saw_cr_at_eol = 0;
754 if (len < 0)
755 len = strlen(line);
756 saw_cr_at_eol = (len && line[len-1] == '\r');
757
758 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
759 reset,
760 saw_cr_at_eol ? "\r" : "");
761}
762
John Keeping41ee2ad2013-02-07 20:15:28 +0000763static void dump_sline(struct sline *sline, const char *line_prefix,
764 unsigned long cnt, int num_parent,
Thomas Rast21798702010-04-15 14:59:37 +0200765 int use_color, int result_deleted)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800766{
767 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700768 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800769 int i;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800770 unsigned long lno = 0;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700771 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100772 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700773 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
774 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
Jeff King8dbf3eb2015-05-27 16:48:46 -0400775 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700776 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800777
Thomas Rast21798702010-04-15 14:59:37 +0200778 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800779 return; /* result deleted */
780
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800781 while (1) {
Junio C Hamano8bc75742006-04-12 13:23:50 -0700782 unsigned long hunk_end;
783 unsigned long rlines;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700784 const char *hunk_comment = NULL;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800785 unsigned long null_context = 0;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700786
787 while (lno <= cnt && !(sline[lno].flag & mark)) {
788 if (hunk_comment_line(sline[lno].bol))
789 hunk_comment = sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800790 lno++;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700791 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700792 if (cnt < lno)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800793 break;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700794 else {
Junio C Hamano74065952006-04-11 14:31:31 -0700795 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700796 if (!(sline[hunk_end].flag & mark))
797 break;
798 }
Junio C Hamano74065952006-04-11 14:31:31 -0700799 rlines = hunk_end - lno;
800 if (cnt < hunk_end)
801 rlines--; /* pointing at the last delete hunk */
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800802
803 if (!context) {
804 /*
805 * Even when running with --unified=0, all
806 * lines in the hunk needs to be processed in
807 * the loop below in order to show the
808 * deletion recorded in lost_head. However,
809 * we do not want to show the resulting line
810 * with all blank context markers in such a
811 * case. Compensate.
812 */
813 unsigned long j;
814 for (j = lno; j < hunk_end; j++)
815 if (!(sline[j].flag & (mark-1)))
816 null_context++;
817 rlines -= null_context;
818 }
819
John Keeping41ee2ad2013-02-07 20:15:28 +0000820 printf("%s%s", line_prefix, c_frag);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800821 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800822 for (i = 0; i < num_parent; i++)
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800823 show_parent_lno(sline, lno, hunk_end, i, null_context);
Junio C Hamano74065952006-04-11 14:31:31 -0700824 printf(" +%lu,%lu ", lno+1, rlines);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800825 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700826
827 if (hunk_comment) {
828 int comment_end = 0;
829 for (i = 0; i < 40; i++) {
830 int ch = hunk_comment[i] & 0xff;
831 if (!ch || ch == '\n')
832 break;
833 if (!isspace(ch))
834 comment_end = i;
835 }
836 if (comment_end)
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100837 printf("%s%s %s%s", c_reset,
Jeff King8dbf3eb2015-05-27 16:48:46 -0400838 c_context, c_reset,
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100839 c_func);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700840 for (i = 0; i < comment_end; i++)
841 putchar(hunk_comment[i]);
842 }
843
Junio C Hamano567a03d2006-08-10 00:30:33 -0700844 printf("%s\n", c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800845 while (lno < hunk_end) {
846 struct lline *ll;
847 int j;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800848 unsigned long p_mask;
Benjamin Kramerfd13b212009-03-07 21:02:26 +0100849 struct sline *sl = &sline[lno++];
Antoine Pelisse99d32062013-03-23 18:23:28 +0100850 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800851 while (ll) {
John Keeping41ee2ad2013-02-07 20:15:28 +0000852 printf("%s%s", line_prefix, c_old);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800853 for (j = 0; j < num_parent; j++) {
854 if (ll->parent_map & (1UL<<j))
855 putchar('-');
856 else
857 putchar(' ');
858 }
Junio C Hamano39280972008-08-27 19:48:01 -0700859 show_line_to_eol(ll->line, -1, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800860 ll = ll->next;
861 }
Junio C Hamano74065952006-04-11 14:31:31 -0700862 if (cnt < lno)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700863 break;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800864 p_mask = 1;
John Keeping41ee2ad2013-02-07 20:15:28 +0000865 fputs(line_prefix, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800866 if (!(sl->flag & (mark-1))) {
867 /*
868 * This sline was here to hang the
869 * lost lines in front of it.
870 */
871 if (!context)
872 continue;
Jeff King8dbf3eb2015-05-27 16:48:46 -0400873 fputs(c_context, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800874 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700875 else
876 fputs(c_new, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800877 for (j = 0; j < num_parent; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800878 if (p_mask & sl->flag)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800879 putchar('+');
Junio C Hamano46dc9412006-02-02 15:17:42 -0800880 else
881 putchar(' ');
882 p_mask <<= 1;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800883 }
Junio C Hamano39280972008-08-27 19:48:01 -0700884 show_line_to_eol(sl->bol, sl->len, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800885 }
886 }
887}
888
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800889static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
890 int i, int j)
891{
892 /* We have already examined parent j and we know parent i
893 * and parent j are the same, so reuse the combined result
894 * of parent j for parent i.
895 */
896 unsigned long lno, imask, jmask;
897 imask = (1UL<<i);
898 jmask = (1UL<<j);
899
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700900 for (lno = 0; lno <= cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100901 struct lline *ll = sline->lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800902 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800903 while (ll) {
904 if (ll->parent_map & jmask)
905 ll->parent_map |= imask;
906 ll = ll->next;
907 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800908 if (sline->flag & jmask)
909 sline->flag |= imask;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800910 sline++;
911 }
Junio C Hamano44627312006-02-06 18:54:08 -0800912 /* the overall size of the file (sline[cnt]) */
913 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800914}
915
Junio C Hamano462a15b2007-12-26 16:51:19 -0800916static void dump_quoted_path(const char *head,
917 const char *prefix,
918 const char *path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000919 const char *line_prefix,
Junio C Hamano567a03d2006-08-10 00:30:33 -0700920 const char *c_meta, const char *c_reset)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800921{
Junio C Hamano462a15b2007-12-26 16:51:19 -0800922 static struct strbuf buf = STRBUF_INIT;
923
924 strbuf_reset(&buf);
John Keeping41ee2ad2013-02-07 20:15:28 +0000925 strbuf_addstr(&buf, line_prefix);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800926 strbuf_addstr(&buf, c_meta);
927 strbuf_addstr(&buf, head);
Junio C Hamanod5625092007-12-26 17:13:36 -0800928 quote_two_c_style(&buf, prefix, path, 0);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800929 strbuf_addstr(&buf, c_reset);
930 puts(buf.buf);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700931}
932
Jeff King7c978a02011-05-23 16:16:41 -0400933static void show_combined_header(struct combine_diff_path *elem,
934 int num_parent,
Jeff King7c978a02011-05-23 16:16:41 -0400935 struct rev_info *rev,
John Keeping41ee2ad2013-02-07 20:15:28 +0000936 const char *line_prefix,
Jeff King4d5f3472011-05-23 16:27:34 -0400937 int mode_differs,
938 int show_file_header)
Jeff King7c978a02011-05-23 16:16:41 -0400939{
940 struct diff_options *opt = &rev->diffopt;
brian m. carlson976ff7e2019-08-18 20:04:12 +0000941 int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV;
Jeff King7c978a02011-05-23 16:16:41 -0400942 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
943 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
Jeff Kingf1c96262011-08-17 22:03:12 -0700944 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
945 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
Jeff King7c978a02011-05-23 16:16:41 -0400946 const char *abb;
947 int added = 0;
948 int deleted = 0;
949 int i;
Sergey Organovd01141d2020-09-29 14:31:22 +0300950 int dense = rev->dense_combined_merges;
Jeff King7c978a02011-05-23 16:16:41 -0400951
952 if (rev->loginfo && !rev->no_commit_id)
953 show_log(rev);
954
955 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
John Keeping41ee2ad2013-02-07 20:15:28 +0000956 "", elem->path, line_prefix, c_meta, c_reset);
957 printf("%s%sindex ", line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400958 for (i = 0; i < num_parent; i++) {
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200959 abb = repo_find_unique_abbrev(the_repository,
960 &elem->parent[i].oid, abbrev);
Jeff King7c978a02011-05-23 16:16:41 -0400961 printf("%s%s", i ? "," : "", abb);
962 }
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200963 abb = repo_find_unique_abbrev(the_repository, &elem->oid, abbrev);
Jeff King7c978a02011-05-23 16:16:41 -0400964 printf("..%s%s\n", abb, c_reset);
965
966 if (mode_differs) {
967 deleted = !elem->mode;
968
969 /* We say it was added if nobody had it */
970 added = !deleted;
971 for (i = 0; added && i < num_parent; i++)
972 if (elem->parent[i].status !=
973 DIFF_STATUS_ADDED)
974 added = 0;
975 if (added)
John Keeping41ee2ad2013-02-07 20:15:28 +0000976 printf("%s%snew file mode %06o",
977 line_prefix, c_meta, elem->mode);
Jeff King7c978a02011-05-23 16:16:41 -0400978 else {
979 if (deleted)
John Keeping41ee2ad2013-02-07 20:15:28 +0000980 printf("%s%sdeleted file ",
981 line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400982 printf("mode ");
983 for (i = 0; i < num_parent; i++) {
984 printf("%s%06o", i ? "," : "",
985 elem->parent[i].mode);
986 }
987 if (elem->mode)
988 printf("..%06o", elem->mode);
989 }
990 printf("%s\n", c_reset);
991 }
992
Jeff King4d5f3472011-05-23 16:27:34 -0400993 if (!show_file_header)
994 return;
995
Elijah Newrend76ce4f2019-02-07 17:12:46 -0800996 if (rev->combined_all_paths) {
997 for (i = 0; i < num_parent; i++) {
998 char *path = filename_changed(elem->parent[i].status)
999 ? elem->parent[i].path.buf : elem->path;
1000 if (elem->parent[i].status == DIFF_STATUS_ADDED)
1001 dump_quoted_path("--- ", "", "/dev/null",
1002 line_prefix, c_meta, c_reset);
1003 else
1004 dump_quoted_path("--- ", a_prefix, path,
1005 line_prefix, c_meta, c_reset);
1006 }
1007 } else {
1008 if (added)
1009 dump_quoted_path("--- ", "", "/dev/null",
1010 line_prefix, c_meta, c_reset);
1011 else
1012 dump_quoted_path("--- ", a_prefix, elem->path,
1013 line_prefix, c_meta, c_reset);
1014 }
Jeff King7c978a02011-05-23 16:16:41 -04001015 if (deleted)
1016 dump_quoted_path("+++ ", "", "/dev/null",
John Keeping41ee2ad2013-02-07 20:15:28 +00001017 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -04001018 else
1019 dump_quoted_path("+++ ", b_prefix, elem->path,
John Keeping41ee2ad2013-02-07 20:15:28 +00001020 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -04001021}
1022
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001023static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
Sergey Organovd01141d2020-09-29 14:31:22 +03001024 int working_tree_file,
Junio C Hamano99694542011-08-04 11:39:10 -07001025 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001026{
Linus Torvalds91539832006-04-17 11:59:32 -07001027 struct diff_options *opt = &rev->diffopt;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001028 unsigned long result_size, cnt, lno;
Thomas Rast21798702010-04-15 14:59:37 +02001029 int result_deleted = 0;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -05001030 char *result, *cp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001031 struct sline *sline; /* survived lines */
Junio C Hamano2454c962006-02-06 12:53:07 -08001032 int mode_differs = 0;
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001033 int i, show_hunks;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001034 mmfile_t result_file;
Jeff King4d5f3472011-05-23 16:27:34 -04001035 struct userdiff_driver *userdiff;
Jeff King0508fe52011-05-23 16:31:05 -04001036 struct userdiff_driver *textconv = NULL;
Jeff King4d5f3472011-05-23 16:27:34 -04001037 int is_binary;
John Keeping41ee2ad2013-02-07 20:15:28 +00001038 const char *line_prefix = diff_line_prefix(opt);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001039
Linus Torvaldsee1e5412006-05-13 13:23:48 -07001040 context = opt->context;
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02001041 userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
Jeff King4d5f3472011-05-23 16:27:34 -04001042 if (!userdiff)
1043 userdiff = userdiff_find_by_name("default");
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001044 if (opt->flags.allow_textconv)
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01001045 textconv = userdiff_get_textconv(opt->repo, userdiff);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001046
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001047 /* Read the result of merge first */
Junio C Hamanof23fc772006-04-03 18:53:15 -07001048 if (!working_tree_file)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001049 result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size,
Jeff King0508fe52011-05-23 16:31:05 -04001050 textconv, elem->path);
Junio C Hamanoea726d02006-01-28 00:03:38 -08001051 else {
Junio C Hamano9843a1f2006-02-06 12:30:00 -08001052 /* Used by diff-tree to read from the working tree */
Junio C Hamanoea726d02006-01-28 00:03:38 -08001053 struct stat st;
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001054 int fd = -1;
1055
1056 if (lstat(elem->path, &st) < 0)
1057 goto deleted_file;
1058
1059 if (S_ISLNK(st.st_mode)) {
Junio C Hamano912342d2008-12-17 12:37:58 -08001060 struct strbuf buf = STRBUF_INIT;
1061
1062 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
Nguyễn Thái Ngọc Duy4b94ec92016-05-08 16:47:36 +07001063 error_errno("readlink(%s)", elem->path);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001064 return;
1065 }
Junio C Hamano912342d2008-12-17 12:37:58 -08001066 result_size = buf.len;
1067 result = strbuf_detach(&buf, NULL);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001068 elem->mode = canon_mode(st.st_mode);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001069 } else if (S_ISDIR(st.st_mode)) {
brian m. carlson1ff57c12015-03-13 23:39:33 +00001070 struct object_id oid;
Patrick Steinhardte19488a2024-05-17 10:18:39 +02001071 if (repo_resolve_gitlink_ref(the_repository, elem->path,
1072 "HEAD", &oid) < 0)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001073 result = grab_blob(opt->repo, &elem->oid,
1074 elem->mode, &result_size,
1075 NULL, NULL);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001076 else
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001077 result = grab_blob(opt->repo, &oid, elem->mode,
Jeff King0508fe52011-05-23 16:31:05 -04001078 &result_size, NULL, NULL);
1079 } else if (textconv) {
1080 struct diff_filespec *df = alloc_filespec(elem->path);
brian m. carlson14228442021-04-26 01:02:56 +00001081 fill_filespec(df, null_oid(), 0, st.st_mode);
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001082 result_size = fill_textconv(opt->repo, textconv, df, &result);
Jeff King0508fe52011-05-23 16:31:05 -04001083 free_filespec(df);
Kjetil Barvik91fcbcb2009-02-09 21:54:52 +01001084 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001085 size_t len = xsize_t(st.st_size);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001086 ssize_t done;
Johannes Sixta249a9b2007-03-03 20:38:00 +01001087 int is_file, i;
Junio C Hamanoea726d02006-01-28 00:03:38 -08001088
Junio C Hamano1b0c7172006-03-29 22:55:43 -08001089 elem->mode = canon_mode(st.st_mode);
Johannes Sixta249a9b2007-03-03 20:38:00 +01001090 /* if symlinks don't work, assume symlink if all parents
1091 * are symlinks
1092 */
1093 is_file = has_symlinks;
1094 for (i = 0; !is_file && i < num_parent; i++)
1095 is_file = !S_ISLNK(elem->parent[i].mode);
1096 if (!is_file)
1097 elem->mode = canon_mode(S_IFLNK);
1098
Junio C Hamanof23fc772006-04-03 18:53:15 -07001099 result_size = len;
Jeff King3733e692016-02-22 17:44:28 -05001100 result = xmallocz(len);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001101
1102 done = read_in_full(fd, result, len);
1103 if (done < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02001104 die_errno("read error '%s'", elem->path);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001105 else if (done < len)
1106 die("early EOF '%s'", elem->path);
1107
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001108 /* If not a fake symlink, apply filters, e.g. autocrlf */
1109 if (is_file) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001110 struct strbuf buf = STRBUF_INIT;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001111
Nguyễn Thái Ngọc Duy0734f202018-09-21 17:57:20 +02001112 if (convert_to_git(rev->diffopt.repo->index,
1113 elem->path, result, len, &buf, global_conv_flags_eol)) {
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001114 free(result);
1115 result = strbuf_detach(&buf, &len);
1116 result_size = len;
1117 }
1118 }
Junio C Hamanoea726d02006-01-28 00:03:38 -08001119 }
1120 else {
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001121 deleted_file:
Thomas Rast21798702010-04-15 14:59:37 +02001122 result_deleted = 1;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001123 result_size = 0;
Junio C Hamano713a11f2006-02-13 23:07:04 -08001124 elem->mode = 0;
Peter Eriksen28f75812006-07-25 09:30:18 +02001125 result = xcalloc(1, 1);
Junio C Hamanoea726d02006-01-28 00:03:38 -08001126 }
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001127
Junio C Hamanoea726d02006-01-28 00:03:38 -08001128 if (0 <= fd)
1129 close(fd);
1130 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001131
Jeff Kingc95b99b2011-05-23 16:16:59 -04001132 for (i = 0; i < num_parent; i++) {
1133 if (elem->parent[i].mode != elem->mode) {
1134 mode_differs = 1;
1135 break;
1136 }
1137 }
1138
Jeff King0508fe52011-05-23 16:31:05 -04001139 if (textconv)
1140 is_binary = 0;
1141 else if (userdiff->binary != -1)
Jeff King4d5f3472011-05-23 16:27:34 -04001142 is_binary = userdiff->binary;
1143 else {
1144 is_binary = buffer_is_binary(result, result_size);
1145 for (i = 0; !is_binary && i < num_parent; i++) {
1146 char *buf;
1147 unsigned long size;
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001148 buf = grab_blob(opt->repo,
1149 &elem->parent[i].oid,
Jeff King4d5f3472011-05-23 16:27:34 -04001150 elem->parent[i].mode,
Jeff King0508fe52011-05-23 16:31:05 -04001151 &size, NULL, NULL);
Jeff King4d5f3472011-05-23 16:27:34 -04001152 if (buffer_is_binary(buf, size))
1153 is_binary = 1;
1154 free(buf);
1155 }
1156 }
1157 if (is_binary) {
Sergey Organovd01141d2020-09-29 14:31:22 +03001158 show_combined_header(elem, num_parent, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001159 line_prefix, mode_differs, 0);
Jeff King4d5f3472011-05-23 16:27:34 -04001160 printf("Binary files differ\n");
1161 free(result);
1162 return;
1163 }
1164
Junio C Hamano2386c292006-06-28 01:38:19 -07001165 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001166 if (*cp == '\n')
1167 cnt++;
1168 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001169 if (result_size && result[result_size-1] != '\n')
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001170 cnt++; /* incomplete line */
1171
René Scharfeca56dad2021-03-13 17:17:22 +01001172 CALLOC_ARRAY(sline, st_add(cnt, 2));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001173 sline[0].bol = result;
Junio C Hamano2386c292006-06-28 01:38:19 -07001174 for (lno = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001175 if (*cp == '\n') {
1176 sline[lno].len = cp - sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001177 lno++;
1178 if (lno < cnt)
1179 sline[lno].bol = cp + 1;
1180 }
1181 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001182 if (result_size && result[result_size-1] != '\n')
1183 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1184
1185 result_file.ptr = result;
1186 result_file.size = result_size;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001187
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001188 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1189 * for deletion hunk at the end.
1190 */
René Scharfeca56dad2021-03-13 17:17:22 +01001191 CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001192 for (lno = 0; lno <= cnt; lno++)
Junio C Hamanof16706c2006-01-30 22:33:15 -08001193 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1194
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001195 for (i = 0; i < num_parent; i++) {
1196 int j;
1197 for (j = 0; j < i; j++) {
Jeff King4a7e27e2018-08-28 17:22:40 -04001198 if (oideq(&elem->parent[i].oid,
1199 &elem->parent[j].oid)) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001200 reuse_combine_diff(sline, cnt, i, j);
1201 break;
1202 }
1203 }
1204 if (i <= j)
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001205 combine_diff(opt->repo,
1206 &elem->parent[i].oid,
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001207 elem->parent[i].mode,
1208 &result_file, sline,
Jeff King0508fe52011-05-23 16:31:05 -04001209 cnt, i, num_parent, result_deleted,
Antoine Pelissefa04ae02013-03-14 22:03:14 +01001210 textconv, elem->path, opt->xdl_opts);
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001211 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001212
Sergey Organovd01141d2020-09-29 14:31:22 +03001213 show_hunks = make_hunks(sline, cnt, num_parent, rev->dense_combined_merges);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001214
Junio C Hamano713a11f2006-02-13 23:07:04 -08001215 if (show_hunks || mode_differs || working_tree_file) {
Sergey Organovd01141d2020-09-29 14:31:22 +03001216 show_combined_header(elem, num_parent, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001217 line_prefix, mode_differs, 1);
1218 dump_sline(sline, line_prefix, cnt, num_parent,
Jeff Kingf1c96262011-08-17 22:03:12 -07001219 opt->use_color, result_deleted);
Junio C Hamano8828cdc2006-01-25 14:26:22 -08001220 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001221 free(result);
1222
Junio C Hamano2386c292006-06-28 01:38:19 -07001223 for (lno = 0; lno < cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +01001224 if (sline[lno].lost) {
1225 struct lline *ll = sline[lno].lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001226 while (ll) {
1227 struct lline *tmp = ll;
1228 ll = ll->next;
1229 free(tmp);
1230 }
1231 }
1232 }
Junio C Hamano46dc9412006-02-02 15:17:42 -08001233 free(sline[0].p_lno);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001234 free(sline);
1235}
1236
Linus Torvalds91539832006-04-17 11:59:32 -07001237static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
Linus Torvaldsee638022006-02-09 10:30:28 -08001238{
Linus Torvalds91539832006-04-17 11:59:32 -07001239 struct diff_options *opt = &rev->diffopt;
Junio C Hamano77667052013-02-03 13:08:18 -08001240 int line_termination, inter_name_termination, i;
John Keeping41ee2ad2013-02-07 20:15:28 +00001241 const char *line_prefix = diff_line_prefix(opt);
Linus Torvaldsee638022006-02-09 10:30:28 -08001242
1243 line_termination = opt->line_termination;
1244 inter_name_termination = '\t';
1245 if (!line_termination)
1246 inter_name_termination = 0;
1247
Junio C Hamano44152782006-10-26 02:05:59 -07001248 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -07001249 show_log(rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001250
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001251
Timo Hirvonenc6744342006-06-24 20:21:53 +03001252 if (opt->output_format & DIFF_FORMAT_RAW) {
John Keeping41ee2ad2013-02-07 20:15:28 +00001253 printf("%s", line_prefix);
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001254
Junio C Hamano77667052013-02-03 13:08:18 -08001255 /* As many colons as there are parents */
1256 for (i = 0; i < num_parent; i++)
1257 putchar(':');
Linus Torvaldsee638022006-02-09 10:30:28 -08001258
Junio C Hamano0a798072006-02-09 15:23:06 -08001259 /* Show the modes */
Junio C Hamano77667052013-02-03 13:08:18 -08001260 for (i = 0; i < num_parent; i++)
1261 printf("%06o ", p->parent[i].mode);
1262 printf("%06o", p->mode);
Junio C Hamano0a798072006-02-09 15:23:06 -08001263
1264 /* Show sha1's */
1265 for (i = 0; i < num_parent; i++)
Jeff Kingd6cece52016-10-20 02:20:07 -04001266 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
Jeff Kingd5e3b012016-10-20 02:19:43 -04001267 opt->abbrev));
Jeff Kingd6cece52016-10-20 02:20:07 -04001268 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
Junio C Hamano0a798072006-02-09 15:23:06 -08001269 }
1270
Timo Hirvonenc6744342006-06-24 20:21:53 +03001271 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamanod416df82006-02-10 02:30:52 -08001272 for (i = 0; i < num_parent; i++)
1273 putchar(p->parent[i].status);
1274 putchar(inter_name_termination);
1275 }
Junio C Hamano0a798072006-02-09 15:23:06 -08001276
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001277 for (i = 0; i < num_parent; i++)
1278 if (rev->combined_all_paths) {
1279 if (filename_changed(p->parent[i].status))
1280 write_name_quoted(p->parent[i].path.buf, stdout,
1281 inter_name_termination);
1282 else
1283 write_name_quoted(p->path, stdout,
1284 inter_name_termination);
1285 }
Pierre Habouzit663af342007-09-20 00:42:15 +02001286 write_name_quoted(p->path, stdout, line_termination);
Junio C Hamano0a798072006-02-09 15:23:06 -08001287}
1288
Junio C Hamano99694542011-08-04 11:39:10 -07001289/*
1290 * The result (p->elem) is from the working tree and their
1291 * parents are typically from multiple stages during a merge
1292 * (i.e. diff-files) or the state in HEAD and in the index
1293 * (i.e. diff-index).
1294 */
Linus Torvalds91539832006-04-17 11:59:32 -07001295void show_combined_diff(struct combine_diff_path *p,
Junio C Hamano0a798072006-02-09 15:23:06 -08001296 int num_parent,
Linus Torvalds91539832006-04-17 11:59:32 -07001297 struct rev_info *rev)
Junio C Hamano0a798072006-02-09 15:23:06 -08001298{
Linus Torvalds91539832006-04-17 11:59:32 -07001299 struct diff_options *opt = &rev->diffopt;
John Keeping41ee2ad2013-02-07 20:15:28 +00001300
Timo Hirvonenc6744342006-06-24 20:21:53 +03001301 if (opt->output_format & (DIFF_FORMAT_RAW |
1302 DIFF_FORMAT_NAME |
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001303 DIFF_FORMAT_NAME_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -07001304 show_raw_diff(p, num_parent, rev);
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001305 else if (opt->output_format & DIFF_FORMAT_PATCH)
Sergey Organovd01141d2020-09-29 14:31:22 +03001306 show_patch_diff(p, num_parent, 1, rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001307}
1308
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001309static void free_combined_pair(struct diff_filepair *pair)
1310{
1311 free(pair->two);
1312 free(pair);
1313}
1314
1315/*
1316 * A combine_diff_path expresses N parents on the LHS against 1 merge
1317 * result. Synthesize a diff_filepair that has N entries on the "one"
1318 * side and 1 entry on the "two" side.
1319 *
1320 * In the future, we might want to add more data to combine_diff_path
1321 * so that we can fill fields we are ignoring (most notably, size) here,
1322 * but currently nobody uses it, so this should suffice for now.
1323 */
1324static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1325 int num_parent)
1326{
1327 int i;
1328 struct diff_filepair *pair;
1329 struct diff_filespec *pool;
1330
1331 pair = xmalloc(sizeof(*pair));
René Scharfeca56dad2021-03-13 17:17:22 +01001332 CALLOC_ARRAY(pool, st_add(num_parent, 1));
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001333 pair->one = pool + 1;
1334 pair->two = pool;
1335
1336 for (i = 0; i < num_parent; i++) {
1337 pair->one[i].path = p->path;
1338 pair->one[i].mode = p->parent[i].mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +00001339 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
brian m. carlson41c95602016-06-24 23:09:24 +00001340 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001341 pair->one[i].has_more_entries = 1;
1342 }
1343 pair->one[num_parent - 1].has_more_entries = 0;
1344
1345 pair->two->path = p->path;
1346 pair->two->mode = p->mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +00001347 oidcpy(&pair->two->oid, &p->oid);
brian m. carlson41c95602016-06-24 23:09:24 +00001348 pair->two->oid_valid = !is_null_oid(&p->oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001349 return pair;
1350}
1351
1352static void handle_combined_callback(struct diff_options *opt,
1353 struct combine_diff_path *paths,
1354 int num_parent,
1355 int num_paths)
1356{
1357 struct combine_diff_path *p;
1358 struct diff_queue_struct q;
1359 int i;
1360
René Scharfeca56dad2021-03-13 17:17:22 +01001361 CALLOC_ARRAY(q.queue, num_paths);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001362 q.alloc = num_paths;
1363 q.nr = num_paths;
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001364 for (i = 0, p = paths; p; p = p->next)
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001365 q.queue[i++] = combined_pair(p, num_parent);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001366 opt->format_callback(&q, opt, opt->format_callback_data);
1367 for (i = 0; i < num_paths; i++)
1368 free_combined_pair(q.queue[i]);
1369 free(q.queue);
1370}
1371
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001372static const char *path_path(void *obj)
1373{
1374 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1375
1376 return path->path;
1377}
1378
Jeff King8817f0c2019-01-24 07:33:51 -05001379/*
1380 * Diff stat formats which we always compute solely against the first parent.
1381 */
1382#define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
Jeff King8290faa2019-01-24 07:34:51 -05001383 | DIFF_FORMAT_SHORTSTAT \
Jeff King04b19fc2019-01-24 07:35:40 -05001384 | DIFF_FORMAT_SUMMARY \
Jeff Kingdac03b52019-01-24 07:36:47 -05001385 | DIFF_FORMAT_DIRSTAT \
Jeff King8817f0c2019-01-24 07:33:51 -05001386 | DIFF_FORMAT_DIFFSTAT)
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001387
1388/* find set of paths that every parent touches */
Brandon Williams09fae192017-05-30 10:30:56 -07001389static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001390 const struct oid_array *parents,
1391 struct diff_options *opt,
1392 int combined_all_paths)
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001393{
1394 struct combine_diff_path *paths = NULL;
1395 int i, num_parent = parents->nr;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001396 int output_format = opt->output_format;
Patrick Steinhardt76c7e702024-09-26 13:46:29 +02001397 char *orderfile = opt->orderfile;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001398
1399 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1400 /* tell diff_tree to emit paths in sorted (=tree) order */
1401 opt->orderfile = NULL;
1402
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001403 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001404 for (i = 0; i < num_parent; i++) {
1405 /*
1406 * show stat against the first parent even when doing
1407 * combined diff.
1408 */
Jeff King8817f0c2019-01-24 07:33:51 -05001409 int stat_opt = output_format & STAT_FORMAT_MASK;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001410 if (i == 0 && stat_opt)
1411 opt->output_format = stat_opt;
1412 else
1413 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams66f414f2017-05-30 10:31:03 -07001414 diff_tree_oid(&parents->oid[i], oid, "", opt);
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001415 diffcore_std(opt);
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001416 paths = intersect_paths(paths, i, num_parent,
1417 combined_all_paths);
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001418
1419 /* if showing diff, show it in requested order */
1420 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1421 orderfile) {
1422 diffcore_order(orderfile);
1423 }
1424
1425 diff_flush(opt);
1426 }
1427
1428 opt->output_format = output_format;
1429 opt->orderfile = orderfile;
1430 return paths;
1431}
1432
1433
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001434/*
1435 * find set of paths that everybody touches, assuming diff is run without
1436 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1437 */
1438static struct combine_diff_path *find_paths_multitree(
Brandon Williams09fae192017-05-30 10:30:56 -07001439 const struct object_id *oid, const struct oid_array *parents,
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001440 struct diff_options *opt)
1441{
1442 int i, nparent = parents->nr;
Brandon Williamsfda94b42017-05-30 10:31:06 -07001443 const struct object_id **parents_oid;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001444 struct combine_diff_path paths_head;
1445 struct strbuf base;
1446
Brandon Williamsfda94b42017-05-30 10:31:06 -07001447 ALLOC_ARRAY(parents_oid, nparent);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001448 for (i = 0; i < nparent; i++)
Brandon Williamsfda94b42017-05-30 10:31:06 -07001449 parents_oid[i] = &parents->oid[i];
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001450
1451 /* fake list head, so worker can assume it is non-NULL */
1452 paths_head.next = NULL;
1453
1454 strbuf_init(&base, PATH_MAX);
Brandon Williamsfda94b42017-05-30 10:31:06 -07001455 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001456
1457 strbuf_release(&base);
Brandon Williamsfda94b42017-05-30 10:31:06 -07001458 free(parents_oid);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001459 return paths_head.next;
1460}
1461
Jeff King957876f2020-09-30 07:52:40 -04001462static int match_objfind(struct combine_diff_path *path,
1463 int num_parent,
1464 const struct oidset *set)
1465{
1466 int i;
1467 if (oidset_contains(set, &path->oid))
1468 return 1;
1469 for (i = 0; i < num_parent; i++) {
1470 if (oidset_contains(set, &path->parent[i].oid))
1471 return 1;
1472 }
1473 return 0;
1474}
1475
1476static struct combine_diff_path *combined_objfind(struct diff_options *opt,
1477 struct combine_diff_path *paths,
1478 int num_parent)
1479{
1480 struct combine_diff_path *ret = NULL, **tail = &ret;
1481 struct combine_diff_path *p = paths;
1482
1483 while (p) {
1484 struct combine_diff_path *next = p->next;
1485
1486 if (match_objfind(p, num_parent, opt->objfind)) {
1487 p->next = NULL;
1488 *tail = p;
1489 tail = &p->next;
1490 } else {
1491 free(p);
1492 }
1493 p = next;
1494 }
1495
1496 return ret;
1497}
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001498
Brandon Williamsb9acf542017-05-30 10:30:55 -07001499void diff_tree_combined(const struct object_id *oid,
brian m. carlson910650d2017-03-31 01:40:00 +00001500 const struct oid_array *parents,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001501 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001502{
Linus Torvalds91539832006-04-17 11:59:32 -07001503 struct diff_options *opt = &rev->diffopt;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001504 struct diff_options diffopts;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001505 struct combine_diff_path *p, *paths;
René Scharfe0041f092011-12-17 11:15:48 +01001506 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001507 int need_generic_pathscan;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001508
René Scharfee3d1be42022-06-18 13:12:28 +02001509 if (opt->ignore_regex_nr)
1510 die("combined diff and '%s' cannot be used together",
1511 "--ignore-matching-lines");
René Scharfecfb19ae2022-06-18 13:12:34 +02001512 if (opt->close_file)
1513 die("combined diff and '%s' cannot be used together",
1514 "--output");
René Scharfee3d1be42022-06-18 13:12:28 +02001515
Kirill Smelkov51af1882014-02-03 16:47:19 +04001516 /* nothing to do, if no parents */
1517 if (!num_parent)
1518 return;
1519
1520 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1521 needsep = 0;
1522 if (show_log_first) {
1523 show_log(rev);
1524
Junio C Hamanoe7cc0ed2014-06-06 11:35:01 -07001525 if (rev->verbose_header && opt->output_format &&
Jeff Kingb9c7d6e2014-07-29 13:56:48 -04001526 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1527 !commit_format_is_empty(rev->commit_format))
Kirill Smelkov51af1882014-02-03 16:47:19 +04001528 printf("%s%c", diff_line_prefix(opt),
1529 opt->line_termination);
1530 }
1531
Junio C Hamano5b236832006-02-09 14:35:19 -08001532 diffopts = *opt;
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +07001533 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001534 diffopts.flags.recursive = 1;
1535 diffopts.flags.allow_external = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001536
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001537 /* find set of paths that everybody touches
1538 *
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001539 * NOTE
1540 *
1541 * Diffcore transformations are bound to diff_filespec and logic
1542 * comparing two entries - i.e. they do not apply directly to combine
1543 * diff.
1544 *
1545 * If some of such transformations is requested - we launch generic
1546 * path scanning, which works significantly slower compared to
1547 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1548 *
1549 * TODO some of the filters could be ported to work on
1550 * combine_diff_paths - i.e. all functionality that skips paths, so in
1551 * theory, we could end up having only multitree path scanning.
1552 *
1553 * NOTE please keep this semantically in sync with diffcore_std()
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001554 */
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001555 need_generic_pathscan = opt->skip_stat_unmatch ||
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001556 opt->flags.follow_renames ||
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001557 opt->break_opt != -1 ||
1558 opt->detect_rename ||
Jeff King957876f2020-09-30 07:52:40 -04001559 (opt->pickaxe_opts &
1560 (DIFF_PICKAXE_KINDS_MASK & ~DIFF_PICKAXE_KIND_OBJFIND)) ||
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001561 opt->filter;
1562
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001563 if (need_generic_pathscan) {
1564 /*
1565 * NOTE generic case also handles --stat, as it computes
1566 * diff(sha1,parent_i) for all i to do the job, specifically
1567 * for parent0.
1568 */
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001569 paths = find_paths_generic(oid, parents, &diffopts,
1570 rev->combined_all_paths);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001571 }
1572 else {
1573 int stat_opt;
Brandon Williams09fae192017-05-30 10:30:56 -07001574 paths = find_paths_multitree(oid, parents, &diffopts);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001575
Jeff King957876f2020-09-30 07:52:40 -04001576 if (opt->pickaxe_opts & DIFF_PICKAXE_KIND_OBJFIND)
1577 paths = combined_objfind(opt, paths, num_parent);
1578
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001579 /*
1580 * show stat against the first parent even
1581 * when doing combined diff.
1582 */
Jeff King8817f0c2019-01-24 07:33:51 -05001583 stat_opt = opt->output_format & STAT_FORMAT_MASK;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001584 if (stat_opt) {
1585 diffopts.output_format = stat_opt;
1586
Brandon Williams66f414f2017-05-30 10:31:03 -07001587 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001588 diffcore_std(&diffopts);
1589 if (opt->orderfile)
1590 diffcore_order(opt->orderfile);
1591 diff_flush(&diffopts);
1592 }
1593 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001594
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001595 /* find out number of surviving paths */
1596 for (num_paths = 0, p = paths; p; p = p->next)
1597 num_paths++;
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001598
1599 /* order paths according to diffcore_order */
1600 if (opt->orderfile && num_paths) {
1601 struct obj_order *o;
1602
Jeff Kingb32fa952016-02-22 17:44:25 -05001603 ALLOC_ARRAY(o, num_paths);
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001604 for (i = 0, p = paths; p; p = p->next, i++)
1605 o[i].obj = p;
1606 order_objects(opt->orderfile, path_path, o, num_paths);
1607 for (i = 0; i < num_paths - 1; i++) {
1608 p = o[i].obj;
1609 p->next = o[i+1].obj;
1610 }
1611
1612 p = o[num_paths-1].obj;
1613 p->next = NULL;
1614 paths = o[0].obj;
1615 free(o);
1616 }
1617
1618
Junio C Hamanoe3c3a552006-02-05 22:25:00 -08001619 if (num_paths) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001620 if (opt->output_format & (DIFF_FORMAT_RAW |
1621 DIFF_FORMAT_NAME |
1622 DIFF_FORMAT_NAME_STATUS)) {
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001623 for (p = paths; p; p = p->next)
1624 show_raw_diff(p, num_parent, rev);
Junio C Hamano3969cf72006-06-27 15:08:19 -07001625 needsep = 1;
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001626 }
Jeff King8817f0c2019-01-24 07:33:51 -05001627 else if (opt->output_format & STAT_FORMAT_MASK)
Junio C Hamano3969cf72006-06-27 15:08:19 -07001628 needsep = 1;
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001629 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1630 handle_combined_callback(opt, paths, num_parent, num_paths);
1631
Timo Hirvonenc6744342006-06-24 20:21:53 +03001632 if (opt->output_format & DIFF_FORMAT_PATCH) {
Junio C Hamano3969cf72006-06-27 15:08:19 -07001633 if (needsep)
John Keeping41ee2ad2013-02-07 20:15:28 +00001634 printf("%s%c", diff_line_prefix(opt),
1635 opt->line_termination);
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001636 for (p = paths; p; p = p->next)
Sergey Organovd01141d2020-09-29 14:31:22 +03001637 show_patch_diff(p, num_parent, 0, rev);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001638 }
1639 }
1640
1641 /* Clean things up */
1642 while (paths) {
Junio C Hamanoea726d02006-01-28 00:03:38 -08001643 struct combine_diff_path *tmp = paths;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001644 paths = paths->next;
Elijah Newrend76ce4f2019-02-07 17:12:46 -08001645 for (i = 0; i < num_parent; i++)
1646 if (rev->combined_all_paths &&
1647 filename_changed(tmp->parent[i].status))
1648 strbuf_release(&tmp->parent[i].path);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001649 free(tmp);
1650 }
Clemens Buchacher46ec5102013-05-28 00:49:57 +02001651
Junio C Hamanoed6e8032016-06-02 14:09:22 -07001652 clear_pathspec(&diffopts.pathspec);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001653}
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001654
Sergey Organovd01141d2020-09-29 14:31:22 +03001655void diff_tree_combined_merge(const struct commit *commit,
René Scharfe82889292011-12-17 11:20:07 +01001656 struct rev_info *rev)
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001657{
Thomas Rast53d00b32013-07-31 22:13:20 +02001658 struct commit_list *parent = get_saved_parents(rev, commit);
brian m. carlson910650d2017-03-31 01:40:00 +00001659 struct oid_array parents = OID_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001660
René Scharfe0041f092011-12-17 11:15:48 +01001661 while (parent) {
brian m. carlson910650d2017-03-31 01:40:00 +00001662 oid_array_append(&parents, &parent->item->object.oid);
René Scharfe0041f092011-12-17 11:15:48 +01001663 parent = parent->next;
1664 }
Sergey Organovd01141d2020-09-29 14:31:22 +03001665 diff_tree_combined(&commit->object.oid, &parents, rev);
brian m. carlson910650d2017-03-31 01:40:00 +00001666 oid_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001667}