blob: 1ec9af1f813968830ee153e23a4670014124683f [file] [log] [blame]
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001#include "cache.h"
2#include "commit.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02003#include "blob.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08004#include "diff.h"
5#include "diffcore.h"
6#include "quote.h"
Junio C Hamanod9ea73e2006-04-05 02:03:58 -07007#include "xdiff-interface.h"
Antoine Pelissefa04ae02013-03-14 22:03:14 +01008#include "xdiff/xmacros.h"
Linus Torvalds91539832006-04-17 11:59:32 -07009#include "log-tree.h"
Junio C Hamano7dae8b22009-04-29 12:49:52 -070010#include "refs.h"
Jeff King4d5f3472011-05-23 16:27:34 -040011#include "userdiff.h"
René Scharfe0041f092011-12-17 11:15:48 +010012#include "sha1-array.h"
Thomas Rast53d00b32013-07-31 22:13:20 +020013#include "revision.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080014
Jeff Kinge09867f2014-08-19 22:14:30 -040015static int compare_paths(const struct combine_diff_path *one,
16 const struct diff_filespec *two)
17{
18 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
19 return strcmp(one->path, two->path);
20
21 return base_name_compare(one->path, strlen(one->path), one->mode,
22 two->path, strlen(two->path), two->mode);
23}
24
Junio C Hamanoea726d02006-01-28 00:03:38 -080025static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080026{
27 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano7b1004b2014-01-28 13:55:59 -080028 struct combine_diff_path *p, **tail = &curr;
Kirill Smelkov8518ff82014-01-20 20:20:40 +040029 int i, cmp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080030
31 if (!n) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080032 for (i = 0; i < q->nr; i++) {
33 int len;
34 const char *path;
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070035 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080036 continue;
37 path = q->queue[i]->two->path;
38 len = strlen(path);
Junio C Hamano2454c962006-02-06 12:53:07 -080039 p = xmalloc(combine_diff_path_size(num_parent, len));
Felipe Contreras4b25d092009-05-01 12:06:36 +030040 p->path = (char *) &(p->parent[num_parent]);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080041 memcpy(p->path, path, len);
42 p->path[len] = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080043 p->next = NULL;
Junio C Hamano2454c962006-02-06 12:53:07 -080044 memset(p->parent, 0,
45 sizeof(p->parent[0]) * num_parent);
46
brian m. carlsona0d12c42016-06-24 23:09:23 +000047 oidcpy(&p->oid, &q->queue[i]->two->oid);
Junio C Hamano2454c962006-02-06 12:53:07 -080048 p->mode = q->queue[i]->two->mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +000049 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
Junio C Hamano2454c962006-02-06 12:53:07 -080050 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080051 p->parent[n].status = q->queue[i]->status;
Junio C Hamano5290a0f2006-01-25 03:34:10 -080052 *tail = p;
53 tail = &p->next;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080054 }
Junio C Hamano7b1004b2014-01-28 13:55:59 -080055 return curr;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080056 }
57
Kirill Smelkov8518ff82014-01-20 20:20:40 +040058 /*
Junio C Hamano7b1004b2014-01-28 13:55:59 -080059 * paths in curr (linked list) and q->queue[] (array) are
60 * both sorted in the tree order.
Kirill Smelkov8518ff82014-01-20 20:20:40 +040061 */
Kirill Smelkov8518ff82014-01-20 20:20:40 +040062 i = 0;
Junio C Hamano7b1004b2014-01-28 13:55:59 -080063 while ((p = *tail) != NULL) {
64 cmp = ((i >= q->nr)
Jeff Kinge09867f2014-08-19 22:14:30 -040065 ? -1 : compare_paths(p, q->queue[i]->two));
Kirill Smelkov8518ff82014-01-20 20:20:40 +040066
Kirill Smelkov8518ff82014-01-20 20:20:40 +040067 if (cmp < 0) {
Junio C Hamano7b1004b2014-01-28 13:55:59 -080068 /* p->path not in q->queue[]; drop it */
69 *tail = p->next;
70 free(p);
Kirill Smelkov8518ff82014-01-20 20:20:40 +040071 continue;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080072 }
Kirill Smelkov8518ff82014-01-20 20:20:40 +040073
74 if (cmp > 0) {
Junio C Hamano7b1004b2014-01-28 13:55:59 -080075 /* q->queue[i] not in p->path; skip it */
Kirill Smelkov8518ff82014-01-20 20:20:40 +040076 i++;
77 continue;
78 }
79
brian m. carlsona0d12c42016-06-24 23:09:23 +000080 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
Kirill Smelkov8518ff82014-01-20 20:20:40 +040081 p->parent[n].mode = q->queue[i]->one->mode;
82 p->parent[n].status = q->queue[i]->status;
83
Junio C Hamano7b1004b2014-01-28 13:55:59 -080084 tail = &p->next;
Kirill Smelkov8518ff82014-01-20 20:20:40 +040085 i++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080086 }
87 return curr;
88}
89
Junio C Hamanob469d8b2006-01-30 16:34:29 -080090/* Lines lost from parent */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080091struct lline {
Antoine Pelisse99d32062013-03-23 18:23:28 +010092 struct lline *next, *prev;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080093 int len;
94 unsigned long parent_map;
95 char line[FLEX_ARRAY];
96};
97
Antoine Pelisse99d32062013-03-23 18:23:28 +010098/* Lines lost from current parent (before coalescing) */
99struct plost {
100 struct lline *lost_head, *lost_tail;
101 int len;
102};
103
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800104/* Lines surviving in the merge result */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800105struct sline {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100106 /* Accumulated and coalesced lost lines */
107 struct lline *lost;
108 int lenlost;
109 struct plost plost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800110 char *bol;
111 int len;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800112 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
113 * we did not change it).
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800114 * bit N is used for "interesting" lines, including context.
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700115 * bit (N+1) is used for "do not show deletion before this".
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800116 */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800117 unsigned long flag;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800118 unsigned long *p_lno;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800119};
120
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100121static int match_string_spaces(const char *line1, int len1,
122 const char *line2, int len2,
123 long flags)
124{
125 if (flags & XDF_WHITESPACE_FLAGS) {
126 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
127 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
128 }
129
130 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
131 return (len1 == len2 && !memcmp(line1, line2, len1));
132
133 while (len1 > 0 && len2 > 0) {
134 len1--;
135 len2--;
136 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
137 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
138 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
139 return 0;
140
141 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
142 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
143 }
144 if (line1[len1] != line2[len2])
145 return 0;
146 }
147
148 if (flags & XDF_IGNORE_WHITESPACE) {
149 /* Consume remaining spaces */
150 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
151 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
152 }
153
154 /* We matched full line1 and line2 */
155 if (!len1 && !len2)
156 return 1;
157
158 return 0;
159}
160
Antoine Pelisse99d32062013-03-23 18:23:28 +0100161enum coalesce_direction { MATCH, BASE, NEW };
162
163/* Coalesce new lines into base by finding LCS */
164static struct lline *coalesce_lines(struct lline *base, int *lenbase,
Brandon Williams06fffa02018-02-14 10:59:36 -0800165 struct lline *newline, int lennew,
Antoine Pelisse99d32062013-03-23 18:23:28 +0100166 unsigned long parent, long flags)
167{
168 int **lcs;
169 enum coalesce_direction **directions;
170 struct lline *baseend, *newend = NULL;
171 int i, j, origbaselen = *lenbase;
172
Brandon Williams06fffa02018-02-14 10:59:36 -0800173 if (newline == NULL)
Antoine Pelisse99d32062013-03-23 18:23:28 +0100174 return base;
175
176 if (base == NULL) {
177 *lenbase = lennew;
Brandon Williams06fffa02018-02-14 10:59:36 -0800178 return newline;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100179 }
180
181 /*
182 * Coalesce new lines into base by finding the LCS
Ondřej Bílka98e023d2013-07-29 10:18:21 +0200183 * - Create the table to run dynamic programming
Antoine Pelisse99d32062013-03-23 18:23:28 +0100184 * - Compute the LCS
185 * - Then reverse read the direction structure:
186 * - If we have MATCH, assign parent to base flag, and consume
187 * both baseend and newend
188 * - Else if we have BASE, consume baseend
189 * - Else if we have NEW, insert newend lline into base and
190 * consume newend
191 */
Jeff King50a6c8e2016-02-22 17:44:35 -0500192 lcs = xcalloc(st_add(origbaselen, 1), sizeof(int*));
193 directions = xcalloc(st_add(origbaselen, 1), sizeof(enum coalesce_direction*));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100194 for (i = 0; i < origbaselen + 1; i++) {
Jeff King50a6c8e2016-02-22 17:44:35 -0500195 lcs[i] = xcalloc(st_add(lennew, 1), sizeof(int));
196 directions[i] = xcalloc(st_add(lennew, 1), sizeof(enum coalesce_direction));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100197 directions[i][0] = BASE;
198 }
199 for (j = 1; j < lennew + 1; j++)
200 directions[0][j] = NEW;
201
202 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
Brandon Williams06fffa02018-02-14 10:59:36 -0800203 for (j = 1, newend = newline; j < lennew + 1; j++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100204 if (match_string_spaces(baseend->line, baseend->len,
205 newend->line, newend->len, flags)) {
206 lcs[i][j] = lcs[i - 1][j - 1] + 1;
207 directions[i][j] = MATCH;
208 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
209 lcs[i][j] = lcs[i][j - 1];
210 directions[i][j] = NEW;
211 } else {
212 lcs[i][j] = lcs[i - 1][j];
213 directions[i][j] = BASE;
214 }
215 if (newend->next)
216 newend = newend->next;
217 }
218 if (baseend->next)
219 baseend = baseend->next;
220 }
221
222 for (i = 0; i < origbaselen + 1; i++)
223 free(lcs[i]);
224 free(lcs);
225
226 /* At this point, baseend and newend point to the end of each lists */
227 i--;
228 j--;
229 while (i != 0 || j != 0) {
230 if (directions[i][j] == MATCH) {
231 baseend->parent_map |= 1<<parent;
232 baseend = baseend->prev;
233 newend = newend->prev;
234 i--;
235 j--;
236 } else if (directions[i][j] == NEW) {
237 struct lline *lline;
238
239 lline = newend;
240 /* Remove lline from new list and update newend */
241 if (lline->prev)
242 lline->prev->next = lline->next;
243 else
Brandon Williams06fffa02018-02-14 10:59:36 -0800244 newline = lline->next;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100245 if (lline->next)
246 lline->next->prev = lline->prev;
247
248 newend = lline->prev;
249 j--;
250
251 /* Add lline to base list */
252 if (baseend) {
253 lline->next = baseend->next;
254 lline->prev = baseend;
255 if (lline->prev)
256 lline->prev->next = lline;
257 }
258 else {
259 lline->next = base;
260 base = lline;
261 }
262 (*lenbase)++;
263
264 if (lline->next)
265 lline->next->prev = lline;
266
267 } else {
268 baseend = baseend->prev;
269 i--;
270 }
271 }
272
Brandon Williams06fffa02018-02-14 10:59:36 -0800273 newend = newline;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100274 while (newend) {
275 struct lline *lline = newend;
276 newend = newend->next;
277 free(lline);
278 }
279
280 for (i = 0; i < origbaselen + 1; i++)
281 free(directions[i]);
282 free(directions);
283
284 return base;
285}
286
brian m. carlson1ff57c12015-03-13 23:39:33 +0000287static char *grab_blob(const struct object_id *oid, unsigned int mode,
Antoine Pelisse99d32062013-03-23 18:23:28 +0100288 unsigned long *size, struct userdiff_driver *textconv,
289 const char *path)
290{
291 char *blob;
292 enum object_type type;
293
294 if (S_ISGITLINK(mode)) {
Jeff King0dc3b032017-03-28 15:46:53 -0400295 struct strbuf buf = STRBUF_INIT;
296 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
297 *size = buf.len;
298 blob = strbuf_detach(&buf, NULL);
brian m. carlson1ff57c12015-03-13 23:39:33 +0000299 } else if (is_null_oid(oid)) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100300 /* deleted blob */
301 *size = 0;
302 return xcalloc(1, 1);
303 } else if (textconv) {
304 struct diff_filespec *df = alloc_filespec(path);
Brandon Williamsf9704c22017-05-30 10:30:50 -0700305 fill_filespec(df, oid, 1, mode);
Antoine Pelisse99d32062013-03-23 18:23:28 +0100306 *size = fill_textconv(textconv, df, &blob);
307 free_filespec(df);
308 } else {
brian m. carlson1ff57c12015-03-13 23:39:33 +0000309 blob = read_sha1_file(oid->hash, &type, size);
Antoine Pelisse99d32062013-03-23 18:23:28 +0100310 if (type != OBJ_BLOB)
brian m. carlson1ff57c12015-03-13 23:39:33 +0000311 die("object '%s' is not a blob!", oid_to_hex(oid));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100312 }
313 return blob;
314}
315
316static void append_lost(struct sline *sline, int n, const char *line, int len)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800317{
318 struct lline *lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800319 unsigned long this_mask = (1UL<<n);
320 if (line[len-1] == '\n')
321 len--;
322
Jeff King96ffc062016-02-22 17:44:32 -0500323 FLEX_ALLOC_MEM(lline, line, line, len);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800324 lline->len = len;
325 lline->next = NULL;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100326 lline->prev = sline->plost.lost_tail;
327 if (lline->prev)
328 lline->prev->next = lline;
329 else
330 sline->plost.lost_head = lline;
331 sline->plost.lost_tail = lline;
332 sline->plost.len++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800333 lline->parent_map = this_mask;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800334}
335
Junio C Hamanof23fc772006-04-03 18:53:15 -0700336struct combine_diff_state {
Junio C Hamanoa0fd3142006-04-06 22:29:55 -0700337 unsigned int lno;
338 int ob, on, nb, nn;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700339 unsigned long nmask;
340 int num_parent;
341 int n;
342 struct sline *sline;
343 struct sline *lost_bucket;
344};
345
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700346static void consume_line(void *state_, char *line, unsigned long len)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700347{
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700348 struct combine_diff_state *state = state_;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700349 if (5 < len && !memcmp("@@ -", line, 4)) {
350 if (parse_hunk_header(line, len,
351 &state->ob, &state->on,
352 &state->nb, &state->nn))
353 return;
354 state->lno = state->nb;
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700355 if (state->nn == 0) {
Junio C Hamanof23fc772006-04-03 18:53:15 -0700356 /* @@ -X,Y +N,0 @@ removed Y lines
357 * that would have come *after* line N
358 * in the result. Our lost buckets hang
359 * to the line after the removed lines,
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700360 *
361 * Note that this is correct even when N == 0,
362 * in which case the hunk removes the first
363 * line in the file.
Junio C Hamanof23fc772006-04-03 18:53:15 -0700364 */
365 state->lost_bucket = &state->sline[state->nb];
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700366 if (!state->nb)
367 state->nb = 1;
368 } else {
Junio C Hamanof23fc772006-04-03 18:53:15 -0700369 state->lost_bucket = &state->sline[state->nb-1];
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700370 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700371 if (!state->sline[state->nb-1].p_lno)
372 state->sline[state->nb-1].p_lno =
373 xcalloc(state->num_parent,
374 sizeof(unsigned long));
375 state->sline[state->nb-1].p_lno[state->n] = state->ob;
376 return;
377 }
378 if (!state->lost_bucket)
379 return; /* not in any hunk yet */
380 switch (line[0]) {
381 case '-':
Antoine Pelisse99d32062013-03-23 18:23:28 +0100382 append_lost(state->lost_bucket, state->n, line+1, len-1);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700383 break;
384 case '+':
385 state->sline[state->lno-1].flag |= state->nmask;
386 state->lno++;
387 break;
388 }
389}
390
brian m. carlson1ff57c12015-03-13 23:39:33 +0000391static void combine_diff(const struct object_id *parent, unsigned int mode,
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700392 mmfile_t *result_file,
Junio C Hamano2386c292006-06-28 01:38:19 -0700393 struct sline *sline, unsigned int cnt, int n,
Jeff King0508fe52011-05-23 16:31:05 -0400394 int num_parent, int result_deleted,
395 struct userdiff_driver *textconv,
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100396 const char *path, long flags)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800397{
Junio C Hamanof23fc772006-04-03 18:53:15 -0700398 unsigned int p_lno, lno;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800399 unsigned long nmask = (1UL << n);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700400 xpparam_t xpp;
401 xdemitconf_t xecfg;
402 mmfile_t parent_file;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700403 struct combine_diff_state state;
404 unsigned long sz;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800405
Thomas Rast21798702010-04-15 14:59:37 +0200406 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800407 return; /* result deleted */
408
Jeff King0508fe52011-05-23 16:31:05 -0400409 parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700410 parent_file.size = sz;
Brian Downing9ccd0a82008-10-25 15:30:37 +0200411 memset(&xpp, 0, sizeof(xpp));
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100412 xpp.flags = flags;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100413 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700414 memset(&state, 0, sizeof(state));
415 state.nmask = nmask;
416 state.sline = sline;
417 state.lno = 1;
418 state.num_parent = num_parent;
419 state.n = n;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800420
Jeff King3efb9882015-09-24 19:12:23 -0400421 if (xdi_diff_outf(&parent_file, result_file, consume_line, &state,
422 &xpp, &xecfg))
423 die("unable to generate combined diff for %s",
Junio C Hamano11a458b2015-09-28 15:33:56 -0700424 oid_to_hex(parent));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700425 free(parent_file.ptr);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800426
427 /* Assign line numbers for this parent.
428 *
429 * sline[lno].p_lno[n] records the first line number
430 * (counting from 1) for parent N if the final hunk display
431 * started by showing sline[lno] (possibly showing the lost
432 * lines attached to it first).
433 */
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700434 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
Junio C Hamanof16706c2006-01-30 22:33:15 -0800435 struct lline *ll;
436 sline[lno].p_lno[n] = p_lno;
437
Antoine Pelisse99d32062013-03-23 18:23:28 +0100438 /* Coalesce new lines */
439 if (sline[lno].plost.lost_head) {
440 struct sline *sl = &sline[lno];
441 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
442 sl->plost.lost_head,
443 sl->plost.len, n, flags);
444 sl->plost.lost_head = sl->plost.lost_tail = NULL;
445 sl->plost.len = 0;
446 }
447
Junio C Hamanof16706c2006-01-30 22:33:15 -0800448 /* How many lines would this sline advance the p_lno? */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100449 ll = sline[lno].lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800450 while (ll) {
451 if (ll->parent_map & nmask)
452 p_lno++; /* '-' means parent had it */
453 ll = ll->next;
454 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700455 if (lno < cnt && !(sline[lno].flag & nmask))
Junio C Hamanof16706c2006-01-30 22:33:15 -0800456 p_lno++; /* no '+' means parent had it */
457 }
458 sline[lno].p_lno[n] = p_lno; /* trailer */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800459}
460
461static unsigned long context = 3;
462static char combine_marker = '@';
463
464static int interesting(struct sline *sline, unsigned long all_mask)
465{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800466 /* If some parents lost lines here, or if we have added to
467 * some parent, it is interesting.
468 */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100469 return ((sline->flag & all_mask) || sline->lost);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800470}
471
Junio C Hamano3ec19092006-01-26 03:53:01 -0800472static unsigned long adjust_hunk_tail(struct sline *sline,
473 unsigned long all_mask,
474 unsigned long hunk_begin,
475 unsigned long i)
476{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800477 /* i points at the first uninteresting line. If the last line
478 * of the hunk was interesting only because it has some
479 * deletion, then it is not all that interesting for the
480 * purpose of giving trailing context lines. This is because
481 * we output '-' line and then unmodified sline[i-1] itself in
482 * that case which gives us one extra context line.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800483 */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800484 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800485 i--;
486 return i;
487}
488
Junio C Hamano46dc9412006-02-02 15:17:42 -0800489static unsigned long find_next(struct sline *sline,
490 unsigned long mark,
491 unsigned long i,
492 unsigned long cnt,
Junio C Hamano2386c292006-06-28 01:38:19 -0700493 int look_for_uninteresting)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800494{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800495 /* We have examined up to i-1 and are about to look at i.
496 * Find next interesting or uninteresting line. Here,
497 * "interesting" does not mean interesting(), but marked by
498 * the give_context() function below (i.e. it includes context
499 * lines that are not interesting to interesting() function
500 * that are surrounded by interesting() ones.
501 */
Junio C Hamano74065952006-04-11 14:31:31 -0700502 while (i <= cnt)
Junio C Hamano2386c292006-06-28 01:38:19 -0700503 if (look_for_uninteresting
Junio C Hamano46dc9412006-02-02 15:17:42 -0800504 ? !(sline[i].flag & mark)
505 : (sline[i].flag & mark))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800506 return i;
507 else
508 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700509 return i;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800510}
511
512static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
513{
514 unsigned long all_mask = (1UL<<num_parent) - 1;
515 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700516 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800517 unsigned long i;
518
Junio C Hamano46dc9412006-02-02 15:17:42 -0800519 /* Two groups of interesting lines may have a short gap of
Pavel Roskin82e5a822006-07-10 01:50:18 -0400520 * uninteresting lines. Connect such groups to give them a
Junio C Hamano46dc9412006-02-02 15:17:42 -0800521 * bit of context.
522 *
523 * We first start from what the interesting() function says,
524 * and mark them with "mark", and paint context lines with the
525 * mark. So interesting() would still say false for such context
526 * lines but they are treated as "interesting" in the end.
527 */
528 i = find_next(sline, mark, 0, cnt, 0);
Junio C Hamano74065952006-04-11 14:31:31 -0700529 if (cnt < i)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800530 return 0;
531
Junio C Hamano74065952006-04-11 14:31:31 -0700532 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800533 unsigned long j = (context < i) ? (i - context) : 0;
534 unsigned long k;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800535
536 /* Paint a few lines before the first interesting line. */
Matthijs Kooijmanaac38572013-05-15 19:42:14 +0200537 while (j < i) {
538 if (!(sline[j].flag & mark))
539 sline[j].flag |= no_pre_delete;
540 sline[j++].flag |= mark;
541 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800542
543 again:
Junio C Hamano46dc9412006-02-02 15:17:42 -0800544 /* we know up to i is to be included. where does the
545 * next uninteresting one start?
546 */
547 j = find_next(sline, mark, i, cnt, 1);
Junio C Hamano74065952006-04-11 14:31:31 -0700548 if (cnt < j)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800549 break; /* the rest are all interesting */
550
551 /* lookahead context lines */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800552 k = find_next(sline, mark, j, cnt, 0);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800553 j = adjust_hunk_tail(sline, all_mask, i, j);
554
555 if (k < j + context) {
556 /* k is interesting and [j,k) are not, but
557 * paint them interesting because the gap is small.
558 */
559 while (j < k)
560 sline[j++].flag |= mark;
561 i = k;
562 goto again;
563 }
564
565 /* j is the first uninteresting line and there is
Junio C Hamano46dc9412006-02-02 15:17:42 -0800566 * no overlap beyond it within context lines. Paint
567 * the trailing edge a bit.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800568 */
569 i = k;
Junio C Hamano74065952006-04-11 14:31:31 -0700570 k = (j + context < cnt+1) ? j + context : cnt+1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800571 while (j < k)
572 sline[j++].flag |= mark;
573 }
574 return 1;
575}
576
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800577static int make_hunks(struct sline *sline, unsigned long cnt,
Junio C Hamanod8f47902006-01-24 01:22:04 -0800578 int num_parent, int dense)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800579{
580 unsigned long all_mask = (1UL<<num_parent) - 1;
581 unsigned long mark = (1UL<<num_parent);
582 unsigned long i;
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800583 int has_interesting = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800584
Junio C Hamano74065952006-04-11 14:31:31 -0700585 for (i = 0; i <= cnt; i++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800586 if (interesting(&sline[i], all_mask))
587 sline[i].flag |= mark;
588 else
589 sline[i].flag &= ~mark;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800590 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800591 if (!dense)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800592 return give_context(sline, cnt, num_parent);
Junio C Hamanod8f47902006-01-24 01:22:04 -0800593
Junio C Hamano263eee22006-01-25 13:11:38 -0800594 /* Look at each hunk, and if we have changes from only one
595 * parent, or the changes are the same from all but one
596 * parent, mark that uninteresting.
Junio C Hamanod8f47902006-01-24 01:22:04 -0800597 */
598 i = 0;
Junio C Hamano74065952006-04-11 14:31:31 -0700599 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800600 unsigned long j, hunk_begin, hunk_end;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800601 unsigned long same_diff;
Junio C Hamano74065952006-04-11 14:31:31 -0700602 while (i <= cnt && !(sline[i].flag & mark))
Junio C Hamanod8f47902006-01-24 01:22:04 -0800603 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700604 if (cnt < i)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800605 break; /* No more interesting hunks */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800606 hunk_begin = i;
Junio C Hamano74065952006-04-11 14:31:31 -0700607 for (j = i + 1; j <= cnt; j++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800608 if (!(sline[j].flag & mark)) {
609 /* Look beyond the end to see if there
610 * is an interesting line after this
611 * hunk within context span.
612 */
613 unsigned long la; /* lookahead */
614 int contin = 0;
615 la = adjust_hunk_tail(sline, all_mask,
616 hunk_begin, j);
Junio C Hamano74065952006-04-11 14:31:31 -0700617 la = (la + context < cnt + 1) ?
618 (la + context) : cnt + 1;
René Scharfee5e9b562012-03-24 16:18:46 +0100619 while (la && j <= --la) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800620 if (sline[la].flag & mark) {
621 contin = 1;
622 break;
623 }
624 }
625 if (!contin)
626 break;
627 j = la;
628 }
629 }
630 hunk_end = j;
631
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800632 /* [i..hunk_end) are interesting. Now is it really
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800633 * interesting? We check if there are only two versions
634 * and the result matches one of them. That is, we look
635 * at:
636 * (+) line, which records lines added to which parents;
637 * this line appears in the result.
638 * (-) line, which records from what parents the line
639 * was removed; this line does not appear in the result.
640 * then check the set of parents the result has difference
641 * from, from all lines. If there are lines that has
642 * different set of parents that the result has differences
643 * from, that means we have more than two versions.
644 *
645 * Even when we have only two versions, if the result does
646 * not match any of the parents, the it should be considered
647 * interesting. In such a case, we would have all '+' line.
648 * After passing the above "two versions" test, that would
649 * appear as "the same set of parents" to be "all parents".
Junio C Hamanod8f47902006-01-24 01:22:04 -0800650 */
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800651 same_diff = 0;
652 has_interesting = 0;
653 for (j = i; j < hunk_end && !has_interesting; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800654 unsigned long this_diff = sline[j].flag & all_mask;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100655 struct lline *ll = sline[j].lost;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800656 if (this_diff) {
657 /* This has some changes. Is it the
658 * same as others?
659 */
660 if (!same_diff)
661 same_diff = this_diff;
662 else if (same_diff != this_diff) {
663 has_interesting = 1;
664 break;
665 }
666 }
667 while (ll && !has_interesting) {
668 /* Lost this line from these parents;
669 * who are they? Are they the same?
670 */
671 this_diff = ll->parent_map;
672 if (!same_diff)
673 same_diff = this_diff;
674 else if (same_diff != this_diff) {
675 has_interesting = 1;
676 }
677 ll = ll->next;
678 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800679 }
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800680
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800681 if (!has_interesting && same_diff != all_mask) {
Junio C Hamanod8f47902006-01-24 01:22:04 -0800682 /* This hunk is not that interesting after all */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800683 for (j = hunk_begin; j < hunk_end; j++)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800684 sline[j].flag &= ~mark;
685 }
686 i = hunk_end;
687 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800688
689 has_interesting = give_context(sline, cnt, num_parent);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800690 return has_interesting;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800691}
692
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800693static 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 -0800694{
695 l0 = sline[l0].p_lno[n];
696 l1 = sline[l1].p_lno[n];
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800697 printf(" -%lu,%lu", l0, l1-l0-null_context);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800698}
699
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700700static int hunk_comment_line(const char *bol)
701{
Junio C Hamano7a8ac592006-10-26 02:05:05 -0700702 int ch;
703
704 if (!bol)
705 return 0;
706 ch = *bol & 0xff;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700707 return (isalpha(ch) || ch == '_' || ch == '$');
708}
709
Junio C Hamano39280972008-08-27 19:48:01 -0700710static void show_line_to_eol(const char *line, int len, const char *reset)
711{
712 int saw_cr_at_eol = 0;
713 if (len < 0)
714 len = strlen(line);
715 saw_cr_at_eol = (len && line[len-1] == '\r');
716
717 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
718 reset,
719 saw_cr_at_eol ? "\r" : "");
720}
721
John Keeping41ee2ad2013-02-07 20:15:28 +0000722static void dump_sline(struct sline *sline, const char *line_prefix,
723 unsigned long cnt, int num_parent,
Thomas Rast21798702010-04-15 14:59:37 +0200724 int use_color, int result_deleted)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800725{
726 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700727 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800728 int i;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800729 unsigned long lno = 0;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700730 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100731 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700732 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
733 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
Jeff King8dbf3eb2015-05-27 16:48:46 -0400734 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700735 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800736
Thomas Rast21798702010-04-15 14:59:37 +0200737 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800738 return; /* result deleted */
739
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800740 while (1) {
Junio C Hamano8bc75742006-04-12 13:23:50 -0700741 unsigned long hunk_end;
742 unsigned long rlines;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700743 const char *hunk_comment = NULL;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800744 unsigned long null_context = 0;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700745
746 while (lno <= cnt && !(sline[lno].flag & mark)) {
747 if (hunk_comment_line(sline[lno].bol))
748 hunk_comment = sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800749 lno++;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700750 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700751 if (cnt < lno)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800752 break;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700753 else {
Junio C Hamano74065952006-04-11 14:31:31 -0700754 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700755 if (!(sline[hunk_end].flag & mark))
756 break;
757 }
Junio C Hamano74065952006-04-11 14:31:31 -0700758 rlines = hunk_end - lno;
759 if (cnt < hunk_end)
760 rlines--; /* pointing at the last delete hunk */
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800761
762 if (!context) {
763 /*
764 * Even when running with --unified=0, all
765 * lines in the hunk needs to be processed in
766 * the loop below in order to show the
767 * deletion recorded in lost_head. However,
768 * we do not want to show the resulting line
769 * with all blank context markers in such a
770 * case. Compensate.
771 */
772 unsigned long j;
773 for (j = lno; j < hunk_end; j++)
774 if (!(sline[j].flag & (mark-1)))
775 null_context++;
776 rlines -= null_context;
777 }
778
John Keeping41ee2ad2013-02-07 20:15:28 +0000779 printf("%s%s", line_prefix, c_frag);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800780 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800781 for (i = 0; i < num_parent; i++)
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800782 show_parent_lno(sline, lno, hunk_end, i, null_context);
Junio C Hamano74065952006-04-11 14:31:31 -0700783 printf(" +%lu,%lu ", lno+1, rlines);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800784 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700785
786 if (hunk_comment) {
787 int comment_end = 0;
788 for (i = 0; i < 40; i++) {
789 int ch = hunk_comment[i] & 0xff;
790 if (!ch || ch == '\n')
791 break;
792 if (!isspace(ch))
793 comment_end = i;
794 }
795 if (comment_end)
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100796 printf("%s%s %s%s", c_reset,
Jeff King8dbf3eb2015-05-27 16:48:46 -0400797 c_context, c_reset,
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100798 c_func);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700799 for (i = 0; i < comment_end; i++)
800 putchar(hunk_comment[i]);
801 }
802
Junio C Hamano567a03d2006-08-10 00:30:33 -0700803 printf("%s\n", c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800804 while (lno < hunk_end) {
805 struct lline *ll;
806 int j;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800807 unsigned long p_mask;
Benjamin Kramerfd13b212009-03-07 21:02:26 +0100808 struct sline *sl = &sline[lno++];
Antoine Pelisse99d32062013-03-23 18:23:28 +0100809 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800810 while (ll) {
John Keeping41ee2ad2013-02-07 20:15:28 +0000811 printf("%s%s", line_prefix, c_old);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800812 for (j = 0; j < num_parent; j++) {
813 if (ll->parent_map & (1UL<<j))
814 putchar('-');
815 else
816 putchar(' ');
817 }
Junio C Hamano39280972008-08-27 19:48:01 -0700818 show_line_to_eol(ll->line, -1, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800819 ll = ll->next;
820 }
Junio C Hamano74065952006-04-11 14:31:31 -0700821 if (cnt < lno)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700822 break;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800823 p_mask = 1;
John Keeping41ee2ad2013-02-07 20:15:28 +0000824 fputs(line_prefix, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800825 if (!(sl->flag & (mark-1))) {
826 /*
827 * This sline was here to hang the
828 * lost lines in front of it.
829 */
830 if (!context)
831 continue;
Jeff King8dbf3eb2015-05-27 16:48:46 -0400832 fputs(c_context, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800833 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700834 else
835 fputs(c_new, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800836 for (j = 0; j < num_parent; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800837 if (p_mask & sl->flag)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800838 putchar('+');
Junio C Hamano46dc9412006-02-02 15:17:42 -0800839 else
840 putchar(' ');
841 p_mask <<= 1;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800842 }
Junio C Hamano39280972008-08-27 19:48:01 -0700843 show_line_to_eol(sl->bol, sl->len, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800844 }
845 }
846}
847
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800848static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
849 int i, int j)
850{
851 /* We have already examined parent j and we know parent i
852 * and parent j are the same, so reuse the combined result
853 * of parent j for parent i.
854 */
855 unsigned long lno, imask, jmask;
856 imask = (1UL<<i);
857 jmask = (1UL<<j);
858
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700859 for (lno = 0; lno <= cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100860 struct lline *ll = sline->lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800861 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800862 while (ll) {
863 if (ll->parent_map & jmask)
864 ll->parent_map |= imask;
865 ll = ll->next;
866 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800867 if (sline->flag & jmask)
868 sline->flag |= imask;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800869 sline++;
870 }
Junio C Hamano44627312006-02-06 18:54:08 -0800871 /* the overall size of the file (sline[cnt]) */
872 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800873}
874
Junio C Hamano462a15b2007-12-26 16:51:19 -0800875static void dump_quoted_path(const char *head,
876 const char *prefix,
877 const char *path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000878 const char *line_prefix,
Junio C Hamano567a03d2006-08-10 00:30:33 -0700879 const char *c_meta, const char *c_reset)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800880{
Junio C Hamano462a15b2007-12-26 16:51:19 -0800881 static struct strbuf buf = STRBUF_INIT;
882
883 strbuf_reset(&buf);
John Keeping41ee2ad2013-02-07 20:15:28 +0000884 strbuf_addstr(&buf, line_prefix);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800885 strbuf_addstr(&buf, c_meta);
886 strbuf_addstr(&buf, head);
Junio C Hamanod5625092007-12-26 17:13:36 -0800887 quote_two_c_style(&buf, prefix, path, 0);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800888 strbuf_addstr(&buf, c_reset);
889 puts(buf.buf);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700890}
891
Jeff King7c978a02011-05-23 16:16:41 -0400892static void show_combined_header(struct combine_diff_path *elem,
893 int num_parent,
894 int dense,
895 struct rev_info *rev,
John Keeping41ee2ad2013-02-07 20:15:28 +0000896 const char *line_prefix,
Jeff King4d5f3472011-05-23 16:27:34 -0400897 int mode_differs,
898 int show_file_header)
Jeff King7c978a02011-05-23 16:16:41 -0400899{
900 struct diff_options *opt = &rev->diffopt;
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700901 int abbrev = opt->flags.full_index ? GIT_SHA1_HEXSZ : DEFAULT_ABBREV;
Jeff King7c978a02011-05-23 16:16:41 -0400902 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
903 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
Jeff Kingf1c96262011-08-17 22:03:12 -0700904 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
905 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
Jeff King7c978a02011-05-23 16:16:41 -0400906 const char *abb;
907 int added = 0;
908 int deleted = 0;
909 int i;
910
911 if (rev->loginfo && !rev->no_commit_id)
912 show_log(rev);
913
914 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
John Keeping41ee2ad2013-02-07 20:15:28 +0000915 "", elem->path, line_prefix, c_meta, c_reset);
916 printf("%s%sindex ", line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400917 for (i = 0; i < num_parent; i++) {
brian m. carlson1ff57c12015-03-13 23:39:33 +0000918 abb = find_unique_abbrev(elem->parent[i].oid.hash,
Jeff King7c978a02011-05-23 16:16:41 -0400919 abbrev);
920 printf("%s%s", i ? "," : "", abb);
921 }
brian m. carlson1ff57c12015-03-13 23:39:33 +0000922 abb = find_unique_abbrev(elem->oid.hash, abbrev);
Jeff King7c978a02011-05-23 16:16:41 -0400923 printf("..%s%s\n", abb, c_reset);
924
925 if (mode_differs) {
926 deleted = !elem->mode;
927
928 /* We say it was added if nobody had it */
929 added = !deleted;
930 for (i = 0; added && i < num_parent; i++)
931 if (elem->parent[i].status !=
932 DIFF_STATUS_ADDED)
933 added = 0;
934 if (added)
John Keeping41ee2ad2013-02-07 20:15:28 +0000935 printf("%s%snew file mode %06o",
936 line_prefix, c_meta, elem->mode);
Jeff King7c978a02011-05-23 16:16:41 -0400937 else {
938 if (deleted)
John Keeping41ee2ad2013-02-07 20:15:28 +0000939 printf("%s%sdeleted file ",
940 line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400941 printf("mode ");
942 for (i = 0; i < num_parent; i++) {
943 printf("%s%06o", i ? "," : "",
944 elem->parent[i].mode);
945 }
946 if (elem->mode)
947 printf("..%06o", elem->mode);
948 }
949 printf("%s\n", c_reset);
950 }
951
Jeff King4d5f3472011-05-23 16:27:34 -0400952 if (!show_file_header)
953 return;
954
Jeff King7c978a02011-05-23 16:16:41 -0400955 if (added)
956 dump_quoted_path("--- ", "", "/dev/null",
John Keeping41ee2ad2013-02-07 20:15:28 +0000957 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400958 else
959 dump_quoted_path("--- ", a_prefix, elem->path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000960 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400961 if (deleted)
962 dump_quoted_path("+++ ", "", "/dev/null",
John Keeping41ee2ad2013-02-07 20:15:28 +0000963 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400964 else
965 dump_quoted_path("+++ ", b_prefix, elem->path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000966 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400967}
968
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700969static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
Junio C Hamano99694542011-08-04 11:39:10 -0700970 int dense, int working_tree_file,
971 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800972{
Linus Torvalds91539832006-04-17 11:59:32 -0700973 struct diff_options *opt = &rev->diffopt;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700974 unsigned long result_size, cnt, lno;
Thomas Rast21798702010-04-15 14:59:37 +0200975 int result_deleted = 0;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -0500976 char *result, *cp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800977 struct sline *sline; /* survived lines */
Junio C Hamano2454c962006-02-06 12:53:07 -0800978 int mode_differs = 0;
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700979 int i, show_hunks;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700980 mmfile_t result_file;
Jeff King4d5f3472011-05-23 16:27:34 -0400981 struct userdiff_driver *userdiff;
Jeff King0508fe52011-05-23 16:31:05 -0400982 struct userdiff_driver *textconv = NULL;
Jeff King4d5f3472011-05-23 16:27:34 -0400983 int is_binary;
John Keeping41ee2ad2013-02-07 20:15:28 +0000984 const char *line_prefix = diff_line_prefix(opt);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800985
Linus Torvaldsee1e5412006-05-13 13:23:48 -0700986 context = opt->context;
Jeff King4d5f3472011-05-23 16:27:34 -0400987 userdiff = userdiff_find_by_path(elem->path);
988 if (!userdiff)
989 userdiff = userdiff_find_by_name("default");
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700990 if (opt->flags.allow_textconv)
Jeff King0508fe52011-05-23 16:31:05 -0400991 textconv = userdiff_get_textconv(userdiff);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700992
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800993 /* Read the result of merge first */
Junio C Hamanof23fc772006-04-03 18:53:15 -0700994 if (!working_tree_file)
brian m. carlson1ff57c12015-03-13 23:39:33 +0000995 result = grab_blob(&elem->oid, elem->mode, &result_size,
Jeff King0508fe52011-05-23 16:31:05 -0400996 textconv, elem->path);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800997 else {
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800998 /* Used by diff-tree to read from the working tree */
Junio C Hamanoea726d02006-01-28 00:03:38 -0800999 struct stat st;
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001000 int fd = -1;
1001
1002 if (lstat(elem->path, &st) < 0)
1003 goto deleted_file;
1004
1005 if (S_ISLNK(st.st_mode)) {
Junio C Hamano912342d2008-12-17 12:37:58 -08001006 struct strbuf buf = STRBUF_INIT;
1007
1008 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
Nguyễn Thái Ngọc Duy4b94ec92016-05-08 16:47:36 +07001009 error_errno("readlink(%s)", elem->path);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001010 return;
1011 }
Junio C Hamano912342d2008-12-17 12:37:58 -08001012 result_size = buf.len;
1013 result = strbuf_detach(&buf, NULL);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001014 elem->mode = canon_mode(st.st_mode);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001015 } else if (S_ISDIR(st.st_mode)) {
brian m. carlson1ff57c12015-03-13 23:39:33 +00001016 struct object_id oid;
brian m. carlsona98e6102017-10-15 22:07:07 +00001017 if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
brian m. carlson1ff57c12015-03-13 23:39:33 +00001018 result = grab_blob(&elem->oid, elem->mode,
Jeff King0508fe52011-05-23 16:31:05 -04001019 &result_size, NULL, NULL);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001020 else
brian m. carlson1ff57c12015-03-13 23:39:33 +00001021 result = grab_blob(&oid, elem->mode,
Jeff King0508fe52011-05-23 16:31:05 -04001022 &result_size, NULL, NULL);
1023 } else if (textconv) {
1024 struct diff_filespec *df = alloc_filespec(elem->path);
Brandon Williamsf9704c22017-05-30 10:30:50 -07001025 fill_filespec(df, &null_oid, 0, st.st_mode);
Jeff King0508fe52011-05-23 16:31:05 -04001026 result_size = fill_textconv(textconv, df, &result);
1027 free_filespec(df);
Kjetil Barvik91fcbcb2009-02-09 21:54:52 +01001028 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001029 size_t len = xsize_t(st.st_size);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001030 ssize_t done;
Johannes Sixta249a9b2007-03-03 20:38:00 +01001031 int is_file, i;
Junio C Hamanoea726d02006-01-28 00:03:38 -08001032
Junio C Hamano1b0c7172006-03-29 22:55:43 -08001033 elem->mode = canon_mode(st.st_mode);
Johannes Sixta249a9b2007-03-03 20:38:00 +01001034 /* if symlinks don't work, assume symlink if all parents
1035 * are symlinks
1036 */
1037 is_file = has_symlinks;
1038 for (i = 0; !is_file && i < num_parent; i++)
1039 is_file = !S_ISLNK(elem->parent[i].mode);
1040 if (!is_file)
1041 elem->mode = canon_mode(S_IFLNK);
1042
Junio C Hamanof23fc772006-04-03 18:53:15 -07001043 result_size = len;
Jeff King3733e692016-02-22 17:44:28 -05001044 result = xmallocz(len);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001045
1046 done = read_in_full(fd, result, len);
1047 if (done < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02001048 die_errno("read error '%s'", elem->path);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001049 else if (done < len)
1050 die("early EOF '%s'", elem->path);
1051
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001052 /* If not a fake symlink, apply filters, e.g. autocrlf */
1053 if (is_file) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001054 struct strbuf buf = STRBUF_INIT;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001055
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01001056 if (convert_to_git(&the_index, elem->path, result, len, &buf, global_conv_flags_eol)) {
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001057 free(result);
1058 result = strbuf_detach(&buf, &len);
1059 result_size = len;
1060 }
1061 }
Junio C Hamanoea726d02006-01-28 00:03:38 -08001062 }
1063 else {
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001064 deleted_file:
Thomas Rast21798702010-04-15 14:59:37 +02001065 result_deleted = 1;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001066 result_size = 0;
Junio C Hamano713a11f2006-02-13 23:07:04 -08001067 elem->mode = 0;
Peter Eriksen28f75812006-07-25 09:30:18 +02001068 result = xcalloc(1, 1);
Junio C Hamanoea726d02006-01-28 00:03:38 -08001069 }
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001070
Junio C Hamanoea726d02006-01-28 00:03:38 -08001071 if (0 <= fd)
1072 close(fd);
1073 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001074
Jeff Kingc95b99b2011-05-23 16:16:59 -04001075 for (i = 0; i < num_parent; i++) {
1076 if (elem->parent[i].mode != elem->mode) {
1077 mode_differs = 1;
1078 break;
1079 }
1080 }
1081
Jeff King0508fe52011-05-23 16:31:05 -04001082 if (textconv)
1083 is_binary = 0;
1084 else if (userdiff->binary != -1)
Jeff King4d5f3472011-05-23 16:27:34 -04001085 is_binary = userdiff->binary;
1086 else {
1087 is_binary = buffer_is_binary(result, result_size);
1088 for (i = 0; !is_binary && i < num_parent; i++) {
1089 char *buf;
1090 unsigned long size;
brian m. carlson1ff57c12015-03-13 23:39:33 +00001091 buf = grab_blob(&elem->parent[i].oid,
Jeff King4d5f3472011-05-23 16:27:34 -04001092 elem->parent[i].mode,
Jeff King0508fe52011-05-23 16:31:05 -04001093 &size, NULL, NULL);
Jeff King4d5f3472011-05-23 16:27:34 -04001094 if (buffer_is_binary(buf, size))
1095 is_binary = 1;
1096 free(buf);
1097 }
1098 }
1099 if (is_binary) {
1100 show_combined_header(elem, num_parent, dense, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001101 line_prefix, mode_differs, 0);
Jeff King4d5f3472011-05-23 16:27:34 -04001102 printf("Binary files differ\n");
1103 free(result);
1104 return;
1105 }
1106
Junio C Hamano2386c292006-06-28 01:38:19 -07001107 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001108 if (*cp == '\n')
1109 cnt++;
1110 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001111 if (result_size && result[result_size-1] != '\n')
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001112 cnt++; /* incomplete line */
1113
Jeff King50a6c8e2016-02-22 17:44:35 -05001114 sline = xcalloc(st_add(cnt, 2), sizeof(*sline));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001115 sline[0].bol = result;
Junio C Hamano2386c292006-06-28 01:38:19 -07001116 for (lno = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001117 if (*cp == '\n') {
1118 sline[lno].len = cp - sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001119 lno++;
1120 if (lno < cnt)
1121 sline[lno].bol = cp + 1;
1122 }
1123 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001124 if (result_size && result[result_size-1] != '\n')
1125 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1126
1127 result_file.ptr = result;
1128 result_file.size = result_size;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001129
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001130 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1131 * for deletion hunk at the end.
1132 */
Jeff King50a6c8e2016-02-22 17:44:35 -05001133 sline[0].p_lno = xcalloc(st_mult(st_add(cnt, 2), num_parent), sizeof(unsigned long));
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001134 for (lno = 0; lno <= cnt; lno++)
Junio C Hamanof16706c2006-01-30 22:33:15 -08001135 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1136
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001137 for (i = 0; i < num_parent; i++) {
1138 int j;
1139 for (j = 0; j < i; j++) {
brian m. carlson1ff57c12015-03-13 23:39:33 +00001140 if (!oidcmp(&elem->parent[i].oid,
1141 &elem->parent[j].oid)) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001142 reuse_combine_diff(sline, cnt, i, j);
1143 break;
1144 }
1145 }
1146 if (i <= j)
brian m. carlson1ff57c12015-03-13 23:39:33 +00001147 combine_diff(&elem->parent[i].oid,
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001148 elem->parent[i].mode,
1149 &result_file, sline,
Jeff King0508fe52011-05-23 16:31:05 -04001150 cnt, i, num_parent, result_deleted,
Antoine Pelissefa04ae02013-03-14 22:03:14 +01001151 textconv, elem->path, opt->xdl_opts);
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001152 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001153
Junio C Hamano8828cdc2006-01-25 14:26:22 -08001154 show_hunks = make_hunks(sline, cnt, num_parent, dense);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001155
Junio C Hamano713a11f2006-02-13 23:07:04 -08001156 if (show_hunks || mode_differs || working_tree_file) {
Jeff King7c978a02011-05-23 16:16:41 -04001157 show_combined_header(elem, num_parent, dense, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001158 line_prefix, mode_differs, 1);
1159 dump_sline(sline, line_prefix, cnt, num_parent,
Jeff Kingf1c96262011-08-17 22:03:12 -07001160 opt->use_color, result_deleted);
Junio C Hamano8828cdc2006-01-25 14:26:22 -08001161 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001162 free(result);
1163
Junio C Hamano2386c292006-06-28 01:38:19 -07001164 for (lno = 0; lno < cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +01001165 if (sline[lno].lost) {
1166 struct lline *ll = sline[lno].lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001167 while (ll) {
1168 struct lline *tmp = ll;
1169 ll = ll->next;
1170 free(tmp);
1171 }
1172 }
1173 }
Junio C Hamano46dc9412006-02-02 15:17:42 -08001174 free(sline[0].p_lno);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001175 free(sline);
1176}
1177
Linus Torvalds91539832006-04-17 11:59:32 -07001178static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
Linus Torvaldsee638022006-02-09 10:30:28 -08001179{
Linus Torvalds91539832006-04-17 11:59:32 -07001180 struct diff_options *opt = &rev->diffopt;
Junio C Hamano77667052013-02-03 13:08:18 -08001181 int line_termination, inter_name_termination, i;
John Keeping41ee2ad2013-02-07 20:15:28 +00001182 const char *line_prefix = diff_line_prefix(opt);
Linus Torvaldsee638022006-02-09 10:30:28 -08001183
1184 line_termination = opt->line_termination;
1185 inter_name_termination = '\t';
1186 if (!line_termination)
1187 inter_name_termination = 0;
1188
Junio C Hamano44152782006-10-26 02:05:59 -07001189 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -07001190 show_log(rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001191
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001192
Timo Hirvonenc6744342006-06-24 20:21:53 +03001193 if (opt->output_format & DIFF_FORMAT_RAW) {
John Keeping41ee2ad2013-02-07 20:15:28 +00001194 printf("%s", line_prefix);
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001195
Junio C Hamano77667052013-02-03 13:08:18 -08001196 /* As many colons as there are parents */
1197 for (i = 0; i < num_parent; i++)
1198 putchar(':');
Linus Torvaldsee638022006-02-09 10:30:28 -08001199
Junio C Hamano0a798072006-02-09 15:23:06 -08001200 /* Show the modes */
Junio C Hamano77667052013-02-03 13:08:18 -08001201 for (i = 0; i < num_parent; i++)
1202 printf("%06o ", p->parent[i].mode);
1203 printf("%06o", p->mode);
Junio C Hamano0a798072006-02-09 15:23:06 -08001204
1205 /* Show sha1's */
1206 for (i = 0; i < num_parent; i++)
Jeff Kingd6cece52016-10-20 02:20:07 -04001207 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
Jeff Kingd5e3b012016-10-20 02:19:43 -04001208 opt->abbrev));
Jeff Kingd6cece52016-10-20 02:20:07 -04001209 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
Junio C Hamano0a798072006-02-09 15:23:06 -08001210 }
1211
Timo Hirvonenc6744342006-06-24 20:21:53 +03001212 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamanod416df82006-02-10 02:30:52 -08001213 for (i = 0; i < num_parent; i++)
1214 putchar(p->parent[i].status);
1215 putchar(inter_name_termination);
1216 }
Junio C Hamano0a798072006-02-09 15:23:06 -08001217
Pierre Habouzit663af342007-09-20 00:42:15 +02001218 write_name_quoted(p->path, stdout, line_termination);
Junio C Hamano0a798072006-02-09 15:23:06 -08001219}
1220
Junio C Hamano99694542011-08-04 11:39:10 -07001221/*
1222 * The result (p->elem) is from the working tree and their
1223 * parents are typically from multiple stages during a merge
1224 * (i.e. diff-files) or the state in HEAD and in the index
1225 * (i.e. diff-index).
1226 */
Linus Torvalds91539832006-04-17 11:59:32 -07001227void show_combined_diff(struct combine_diff_path *p,
Junio C Hamano0a798072006-02-09 15:23:06 -08001228 int num_parent,
1229 int dense,
Linus Torvalds91539832006-04-17 11:59:32 -07001230 struct rev_info *rev)
Junio C Hamano0a798072006-02-09 15:23:06 -08001231{
Linus Torvalds91539832006-04-17 11:59:32 -07001232 struct diff_options *opt = &rev->diffopt;
John Keeping41ee2ad2013-02-07 20:15:28 +00001233
Timo Hirvonenc6744342006-06-24 20:21:53 +03001234 if (opt->output_format & (DIFF_FORMAT_RAW |
1235 DIFF_FORMAT_NAME |
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001236 DIFF_FORMAT_NAME_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -07001237 show_raw_diff(p, num_parent, rev);
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001238 else if (opt->output_format & DIFF_FORMAT_PATCH)
Junio C Hamano99694542011-08-04 11:39:10 -07001239 show_patch_diff(p, num_parent, dense, 1, rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001240}
1241
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001242static void free_combined_pair(struct diff_filepair *pair)
1243{
1244 free(pair->two);
1245 free(pair);
1246}
1247
1248/*
1249 * A combine_diff_path expresses N parents on the LHS against 1 merge
1250 * result. Synthesize a diff_filepair that has N entries on the "one"
1251 * side and 1 entry on the "two" side.
1252 *
1253 * In the future, we might want to add more data to combine_diff_path
1254 * so that we can fill fields we are ignoring (most notably, size) here,
1255 * but currently nobody uses it, so this should suffice for now.
1256 */
1257static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1258 int num_parent)
1259{
1260 int i;
1261 struct diff_filepair *pair;
1262 struct diff_filespec *pool;
1263
1264 pair = xmalloc(sizeof(*pair));
Jeff King50a6c8e2016-02-22 17:44:35 -05001265 pool = xcalloc(st_add(num_parent, 1), sizeof(struct diff_filespec));
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001266 pair->one = pool + 1;
1267 pair->two = pool;
1268
1269 for (i = 0; i < num_parent; i++) {
1270 pair->one[i].path = p->path;
1271 pair->one[i].mode = p->parent[i].mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +00001272 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
brian m. carlson41c95602016-06-24 23:09:24 +00001273 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001274 pair->one[i].has_more_entries = 1;
1275 }
1276 pair->one[num_parent - 1].has_more_entries = 0;
1277
1278 pair->two->path = p->path;
1279 pair->two->mode = p->mode;
brian m. carlsona0d12c42016-06-24 23:09:23 +00001280 oidcpy(&pair->two->oid, &p->oid);
brian m. carlson41c95602016-06-24 23:09:24 +00001281 pair->two->oid_valid = !is_null_oid(&p->oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001282 return pair;
1283}
1284
1285static void handle_combined_callback(struct diff_options *opt,
1286 struct combine_diff_path *paths,
1287 int num_parent,
1288 int num_paths)
1289{
1290 struct combine_diff_path *p;
1291 struct diff_queue_struct q;
1292 int i;
1293
1294 q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
1295 q.alloc = num_paths;
1296 q.nr = num_paths;
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001297 for (i = 0, p = paths; p; p = p->next)
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001298 q.queue[i++] = combined_pair(p, num_parent);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001299 opt->format_callback(&q, opt, opt->format_callback_data);
1300 for (i = 0; i < num_paths; i++)
1301 free_combined_pair(q.queue[i]);
1302 free(q.queue);
1303}
1304
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001305static const char *path_path(void *obj)
1306{
1307 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1308
1309 return path->path;
1310}
1311
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001312
1313/* find set of paths that every parent touches */
Brandon Williams09fae192017-05-30 10:30:56 -07001314static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
brian m. carlson910650d2017-03-31 01:40:00 +00001315 const struct oid_array *parents, struct diff_options *opt)
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001316{
1317 struct combine_diff_path *paths = NULL;
1318 int i, num_parent = parents->nr;
1319
1320 int output_format = opt->output_format;
1321 const char *orderfile = opt->orderfile;
1322
1323 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1324 /* tell diff_tree to emit paths in sorted (=tree) order */
1325 opt->orderfile = NULL;
1326
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001327 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001328 for (i = 0; i < num_parent; i++) {
1329 /*
1330 * show stat against the first parent even when doing
1331 * combined diff.
1332 */
1333 int stat_opt = (output_format &
1334 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1335 if (i == 0 && stat_opt)
1336 opt->output_format = stat_opt;
1337 else
1338 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams66f414f2017-05-30 10:31:03 -07001339 diff_tree_oid(&parents->oid[i], oid, "", opt);
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001340 diffcore_std(opt);
1341 paths = intersect_paths(paths, i, num_parent);
1342
1343 /* if showing diff, show it in requested order */
1344 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1345 orderfile) {
1346 diffcore_order(orderfile);
1347 }
1348
1349 diff_flush(opt);
1350 }
1351
1352 opt->output_format = output_format;
1353 opt->orderfile = orderfile;
1354 return paths;
1355}
1356
1357
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001358/*
1359 * find set of paths that everybody touches, assuming diff is run without
1360 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1361 */
1362static struct combine_diff_path *find_paths_multitree(
Brandon Williams09fae192017-05-30 10:30:56 -07001363 const struct object_id *oid, const struct oid_array *parents,
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001364 struct diff_options *opt)
1365{
1366 int i, nparent = parents->nr;
Brandon Williamsfda94b42017-05-30 10:31:06 -07001367 const struct object_id **parents_oid;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001368 struct combine_diff_path paths_head;
1369 struct strbuf base;
1370
Brandon Williamsfda94b42017-05-30 10:31:06 -07001371 ALLOC_ARRAY(parents_oid, nparent);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001372 for (i = 0; i < nparent; i++)
Brandon Williamsfda94b42017-05-30 10:31:06 -07001373 parents_oid[i] = &parents->oid[i];
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001374
1375 /* fake list head, so worker can assume it is non-NULL */
1376 paths_head.next = NULL;
1377
1378 strbuf_init(&base, PATH_MAX);
Brandon Williamsfda94b42017-05-30 10:31:06 -07001379 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001380
1381 strbuf_release(&base);
Brandon Williamsfda94b42017-05-30 10:31:06 -07001382 free(parents_oid);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001383 return paths_head.next;
1384}
1385
1386
Brandon Williamsb9acf542017-05-30 10:30:55 -07001387void diff_tree_combined(const struct object_id *oid,
brian m. carlson910650d2017-03-31 01:40:00 +00001388 const struct oid_array *parents,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001389 int dense,
1390 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001391{
Linus Torvalds91539832006-04-17 11:59:32 -07001392 struct diff_options *opt = &rev->diffopt;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001393 struct diff_options diffopts;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001394 struct combine_diff_path *p, *paths;
René Scharfe0041f092011-12-17 11:15:48 +01001395 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001396 int need_generic_pathscan;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001397
Kirill Smelkov51af1882014-02-03 16:47:19 +04001398 /* nothing to do, if no parents */
1399 if (!num_parent)
1400 return;
1401
1402 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1403 needsep = 0;
1404 if (show_log_first) {
1405 show_log(rev);
1406
Junio C Hamanoe7cc0ed2014-06-06 11:35:01 -07001407 if (rev->verbose_header && opt->output_format &&
Jeff Kingb9c7d6e2014-07-29 13:56:48 -04001408 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1409 !commit_format_is_empty(rev->commit_format))
Kirill Smelkov51af1882014-02-03 16:47:19 +04001410 printf("%s%c", diff_line_prefix(opt),
1411 opt->line_termination);
1412 }
1413
Junio C Hamano5b236832006-02-09 14:35:19 -08001414 diffopts = *opt;
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +07001415 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001416 diffopts.flags.recursive = 1;
1417 diffopts.flags.allow_external = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001418
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001419 /* find set of paths that everybody touches
1420 *
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001421 * NOTE
1422 *
1423 * Diffcore transformations are bound to diff_filespec and logic
1424 * comparing two entries - i.e. they do not apply directly to combine
1425 * diff.
1426 *
1427 * If some of such transformations is requested - we launch generic
1428 * path scanning, which works significantly slower compared to
1429 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1430 *
1431 * TODO some of the filters could be ported to work on
1432 * combine_diff_paths - i.e. all functionality that skips paths, so in
1433 * theory, we could end up having only multitree path scanning.
1434 *
1435 * NOTE please keep this semantically in sync with diffcore_std()
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001436 */
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001437 need_generic_pathscan = opt->skip_stat_unmatch ||
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001438 opt->flags.follow_renames ||
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001439 opt->break_opt != -1 ||
1440 opt->detect_rename ||
Stefan Bellercf630512018-01-04 14:50:41 -08001441 (opt->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001442 opt->filter;
1443
1444
1445 if (need_generic_pathscan) {
1446 /*
1447 * NOTE generic case also handles --stat, as it computes
1448 * diff(sha1,parent_i) for all i to do the job, specifically
1449 * for parent0.
1450 */
Brandon Williams09fae192017-05-30 10:30:56 -07001451 paths = find_paths_generic(oid, parents, &diffopts);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001452 }
1453 else {
1454 int stat_opt;
Brandon Williams09fae192017-05-30 10:30:56 -07001455 paths = find_paths_multitree(oid, parents, &diffopts);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001456
1457 /*
1458 * show stat against the first parent even
1459 * when doing combined diff.
1460 */
1461 stat_opt = (opt->output_format &
1462 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1463 if (stat_opt) {
1464 diffopts.output_format = stat_opt;
1465
Brandon Williams66f414f2017-05-30 10:31:03 -07001466 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001467 diffcore_std(&diffopts);
1468 if (opt->orderfile)
1469 diffcore_order(opt->orderfile);
1470 diff_flush(&diffopts);
1471 }
1472 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001473
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001474 /* find out number of surviving paths */
1475 for (num_paths = 0, p = paths; p; p = p->next)
1476 num_paths++;
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001477
1478 /* order paths according to diffcore_order */
1479 if (opt->orderfile && num_paths) {
1480 struct obj_order *o;
1481
Jeff Kingb32fa952016-02-22 17:44:25 -05001482 ALLOC_ARRAY(o, num_paths);
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001483 for (i = 0, p = paths; p; p = p->next, i++)
1484 o[i].obj = p;
1485 order_objects(opt->orderfile, path_path, o, num_paths);
1486 for (i = 0; i < num_paths - 1; i++) {
1487 p = o[i].obj;
1488 p->next = o[i+1].obj;
1489 }
1490
1491 p = o[num_paths-1].obj;
1492 p->next = NULL;
1493 paths = o[0].obj;
1494 free(o);
1495 }
1496
1497
Junio C Hamanoe3c3a552006-02-05 22:25:00 -08001498 if (num_paths) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001499 if (opt->output_format & (DIFF_FORMAT_RAW |
1500 DIFF_FORMAT_NAME |
1501 DIFF_FORMAT_NAME_STATUS)) {
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001502 for (p = paths; p; p = p->next)
1503 show_raw_diff(p, num_parent, rev);
Junio C Hamano3969cf72006-06-27 15:08:19 -07001504 needsep = 1;
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001505 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07001506 else if (opt->output_format &
1507 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
Junio C Hamano3969cf72006-06-27 15:08:19 -07001508 needsep = 1;
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001509 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1510 handle_combined_callback(opt, paths, num_parent, num_paths);
1511
Timo Hirvonenc6744342006-06-24 20:21:53 +03001512 if (opt->output_format & DIFF_FORMAT_PATCH) {
Junio C Hamano3969cf72006-06-27 15:08:19 -07001513 if (needsep)
John Keeping41ee2ad2013-02-07 20:15:28 +00001514 printf("%s%c", diff_line_prefix(opt),
1515 opt->line_termination);
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001516 for (p = paths; p; p = p->next)
1517 show_patch_diff(p, num_parent, dense,
1518 0, rev);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001519 }
1520 }
1521
1522 /* Clean things up */
1523 while (paths) {
Junio C Hamanoea726d02006-01-28 00:03:38 -08001524 struct combine_diff_path *tmp = paths;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001525 paths = paths->next;
1526 free(tmp);
1527 }
Clemens Buchacher46ec5102013-05-28 00:49:57 +02001528
Junio C Hamanoed6e8032016-06-02 14:09:22 -07001529 clear_pathspec(&diffopts.pathspec);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001530}
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001531
René Scharfe82889292011-12-17 11:20:07 +01001532void diff_tree_combined_merge(const struct commit *commit, int dense,
1533 struct rev_info *rev)
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001534{
Thomas Rast53d00b32013-07-31 22:13:20 +02001535 struct commit_list *parent = get_saved_parents(rev, commit);
brian m. carlson910650d2017-03-31 01:40:00 +00001536 struct oid_array parents = OID_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001537
René Scharfe0041f092011-12-17 11:15:48 +01001538 while (parent) {
brian m. carlson910650d2017-03-31 01:40:00 +00001539 oid_array_append(&parents, &parent->item->object.oid);
René Scharfe0041f092011-12-17 11:15:48 +01001540 parent = parent->next;
1541 }
Brandon Williamsb9acf542017-05-30 10:30:55 -07001542 diff_tree_combined(&commit->object.oid, &parents, dense, rev);
brian m. carlson910650d2017-03-31 01:40:00 +00001543 oid_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001544}