blob: 8f2313d5022326dc244fa8eba4a8640299ebeac8 [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. carlson1ff57c12015-03-13 23:39:33 +000047 hashcpy(p->oid.hash, q->queue[i]->two->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080048 p->mode = q->queue[i]->two->mode;
brian m. carlson1ff57c12015-03-13 23:39:33 +000049 hashcpy(p->parent[n].oid.hash, q->queue[i]->one->sha1);
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. carlson1ff57c12015-03-13 23:39:33 +000080 hashcpy(p->parent[n].oid.hash, q->queue[i]->one->sha1);
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,
165 struct lline *new, int lennew,
166 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
173 if (new == NULL)
174 return base;
175
176 if (base == NULL) {
177 *lenbase = lennew;
178 return new;
179 }
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++) {
203 for (j = 1, newend = new; j < lennew + 1; j++) {
204 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
244 new = lline->next;
245 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
273 newend = new;
274 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)) {
295 blob = xmalloc(100);
296 *size = snprintf(blob, 100,
brian m. carlson1ff57c12015-03-13 23:39:33 +0000297 "Subproject commit %s\n", oid_to_hex(oid));
298 } else if (is_null_oid(oid)) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100299 /* deleted blob */
300 *size = 0;
301 return xcalloc(1, 1);
302 } else if (textconv) {
303 struct diff_filespec *df = alloc_filespec(path);
brian m. carlson1ff57c12015-03-13 23:39:33 +0000304 fill_filespec(df, oid->hash, 1, mode);
Antoine Pelisse99d32062013-03-23 18:23:28 +0100305 *size = fill_textconv(textconv, df, &blob);
306 free_filespec(df);
307 } else {
brian m. carlson1ff57c12015-03-13 23:39:33 +0000308 blob = read_sha1_file(oid->hash, &type, size);
Antoine Pelisse99d32062013-03-23 18:23:28 +0100309 if (type != OBJ_BLOB)
brian m. carlson1ff57c12015-03-13 23:39:33 +0000310 die("object '%s' is not a blob!", oid_to_hex(oid));
Antoine Pelisse99d32062013-03-23 18:23:28 +0100311 }
312 return blob;
313}
314
315static void append_lost(struct sline *sline, int n, const char *line, int len)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800316{
317 struct lline *lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800318 unsigned long this_mask = (1UL<<n);
319 if (line[len-1] == '\n')
320 len--;
321
Jeff King96ffc062016-02-22 17:44:32 -0500322 FLEX_ALLOC_MEM(lline, line, line, len);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800323 lline->len = len;
324 lline->next = NULL;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100325 lline->prev = sline->plost.lost_tail;
326 if (lline->prev)
327 lline->prev->next = lline;
328 else
329 sline->plost.lost_head = lline;
330 sline->plost.lost_tail = lline;
331 sline->plost.len++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800332 lline->parent_map = this_mask;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800333}
334
Junio C Hamanof23fc772006-04-03 18:53:15 -0700335struct combine_diff_state {
Junio C Hamanoa0fd3142006-04-06 22:29:55 -0700336 unsigned int lno;
337 int ob, on, nb, nn;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700338 unsigned long nmask;
339 int num_parent;
340 int n;
341 struct sline *sline;
342 struct sline *lost_bucket;
343};
344
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700345static void consume_line(void *state_, char *line, unsigned long len)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700346{
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700347 struct combine_diff_state *state = state_;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700348 if (5 < len && !memcmp("@@ -", line, 4)) {
349 if (parse_hunk_header(line, len,
350 &state->ob, &state->on,
351 &state->nb, &state->nn))
352 return;
353 state->lno = state->nb;
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700354 if (state->nn == 0) {
Junio C Hamanof23fc772006-04-03 18:53:15 -0700355 /* @@ -X,Y +N,0 @@ removed Y lines
356 * that would have come *after* line N
357 * in the result. Our lost buckets hang
358 * to the line after the removed lines,
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700359 *
360 * Note that this is correct even when N == 0,
361 * in which case the hunk removes the first
362 * line in the file.
Junio C Hamanof23fc772006-04-03 18:53:15 -0700363 */
364 state->lost_bucket = &state->sline[state->nb];
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700365 if (!state->nb)
366 state->nb = 1;
367 } else {
Junio C Hamanof23fc772006-04-03 18:53:15 -0700368 state->lost_bucket = &state->sline[state->nb-1];
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700369 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700370 if (!state->sline[state->nb-1].p_lno)
371 state->sline[state->nb-1].p_lno =
372 xcalloc(state->num_parent,
373 sizeof(unsigned long));
374 state->sline[state->nb-1].p_lno[state->n] = state->ob;
375 return;
376 }
377 if (!state->lost_bucket)
378 return; /* not in any hunk yet */
379 switch (line[0]) {
380 case '-':
Antoine Pelisse99d32062013-03-23 18:23:28 +0100381 append_lost(state->lost_bucket, state->n, line+1, len-1);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700382 break;
383 case '+':
384 state->sline[state->lno-1].flag |= state->nmask;
385 state->lno++;
386 break;
387 }
388}
389
brian m. carlson1ff57c12015-03-13 23:39:33 +0000390static void combine_diff(const struct object_id *parent, unsigned int mode,
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700391 mmfile_t *result_file,
Junio C Hamano2386c292006-06-28 01:38:19 -0700392 struct sline *sline, unsigned int cnt, int n,
Jeff King0508fe52011-05-23 16:31:05 -0400393 int num_parent, int result_deleted,
394 struct userdiff_driver *textconv,
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100395 const char *path, long flags)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800396{
Junio C Hamanof23fc772006-04-03 18:53:15 -0700397 unsigned int p_lno, lno;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800398 unsigned long nmask = (1UL << n);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700399 xpparam_t xpp;
400 xdemitconf_t xecfg;
401 mmfile_t parent_file;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700402 struct combine_diff_state state;
403 unsigned long sz;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800404
Thomas Rast21798702010-04-15 14:59:37 +0200405 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800406 return; /* result deleted */
407
Jeff King0508fe52011-05-23 16:31:05 -0400408 parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700409 parent_file.size = sz;
Brian Downing9ccd0a82008-10-25 15:30:37 +0200410 memset(&xpp, 0, sizeof(xpp));
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100411 xpp.flags = flags;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100412 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700413 memset(&state, 0, sizeof(state));
414 state.nmask = nmask;
415 state.sline = sline;
416 state.lno = 1;
417 state.num_parent = num_parent;
418 state.n = n;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800419
Jeff King3efb9882015-09-24 19:12:23 -0400420 if (xdi_diff_outf(&parent_file, result_file, consume_line, &state,
421 &xpp, &xecfg))
422 die("unable to generate combined diff for %s",
Junio C Hamano11a458b2015-09-28 15:33:56 -0700423 oid_to_hex(parent));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700424 free(parent_file.ptr);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800425
426 /* Assign line numbers for this parent.
427 *
428 * sline[lno].p_lno[n] records the first line number
429 * (counting from 1) for parent N if the final hunk display
430 * started by showing sline[lno] (possibly showing the lost
431 * lines attached to it first).
432 */
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700433 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
Junio C Hamanof16706c2006-01-30 22:33:15 -0800434 struct lline *ll;
435 sline[lno].p_lno[n] = p_lno;
436
Antoine Pelisse99d32062013-03-23 18:23:28 +0100437 /* Coalesce new lines */
438 if (sline[lno].plost.lost_head) {
439 struct sline *sl = &sline[lno];
440 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
441 sl->plost.lost_head,
442 sl->plost.len, n, flags);
443 sl->plost.lost_head = sl->plost.lost_tail = NULL;
444 sl->plost.len = 0;
445 }
446
Junio C Hamanof16706c2006-01-30 22:33:15 -0800447 /* How many lines would this sline advance the p_lno? */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100448 ll = sline[lno].lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800449 while (ll) {
450 if (ll->parent_map & nmask)
451 p_lno++; /* '-' means parent had it */
452 ll = ll->next;
453 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700454 if (lno < cnt && !(sline[lno].flag & nmask))
Junio C Hamanof16706c2006-01-30 22:33:15 -0800455 p_lno++; /* no '+' means parent had it */
456 }
457 sline[lno].p_lno[n] = p_lno; /* trailer */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800458}
459
460static unsigned long context = 3;
461static char combine_marker = '@';
462
463static int interesting(struct sline *sline, unsigned long all_mask)
464{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800465 /* If some parents lost lines here, or if we have added to
466 * some parent, it is interesting.
467 */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100468 return ((sline->flag & all_mask) || sline->lost);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800469}
470
Junio C Hamano3ec19092006-01-26 03:53:01 -0800471static unsigned long adjust_hunk_tail(struct sline *sline,
472 unsigned long all_mask,
473 unsigned long hunk_begin,
474 unsigned long i)
475{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800476 /* i points at the first uninteresting line. If the last line
477 * of the hunk was interesting only because it has some
478 * deletion, then it is not all that interesting for the
479 * purpose of giving trailing context lines. This is because
480 * we output '-' line and then unmodified sline[i-1] itself in
481 * that case which gives us one extra context line.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800482 */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800483 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800484 i--;
485 return i;
486}
487
Junio C Hamano46dc9412006-02-02 15:17:42 -0800488static unsigned long find_next(struct sline *sline,
489 unsigned long mark,
490 unsigned long i,
491 unsigned long cnt,
Junio C Hamano2386c292006-06-28 01:38:19 -0700492 int look_for_uninteresting)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800493{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800494 /* We have examined up to i-1 and are about to look at i.
495 * Find next interesting or uninteresting line. Here,
496 * "interesting" does not mean interesting(), but marked by
497 * the give_context() function below (i.e. it includes context
498 * lines that are not interesting to interesting() function
499 * that are surrounded by interesting() ones.
500 */
Junio C Hamano74065952006-04-11 14:31:31 -0700501 while (i <= cnt)
Junio C Hamano2386c292006-06-28 01:38:19 -0700502 if (look_for_uninteresting
Junio C Hamano46dc9412006-02-02 15:17:42 -0800503 ? !(sline[i].flag & mark)
504 : (sline[i].flag & mark))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800505 return i;
506 else
507 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700508 return i;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800509}
510
511static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
512{
513 unsigned long all_mask = (1UL<<num_parent) - 1;
514 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700515 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800516 unsigned long i;
517
Junio C Hamano46dc9412006-02-02 15:17:42 -0800518 /* Two groups of interesting lines may have a short gap of
Pavel Roskin82e5a822006-07-10 01:50:18 -0400519 * uninteresting lines. Connect such groups to give them a
Junio C Hamano46dc9412006-02-02 15:17:42 -0800520 * bit of context.
521 *
522 * We first start from what the interesting() function says,
523 * and mark them with "mark", and paint context lines with the
524 * mark. So interesting() would still say false for such context
525 * lines but they are treated as "interesting" in the end.
526 */
527 i = find_next(sline, mark, 0, cnt, 0);
Junio C Hamano74065952006-04-11 14:31:31 -0700528 if (cnt < i)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800529 return 0;
530
Junio C Hamano74065952006-04-11 14:31:31 -0700531 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800532 unsigned long j = (context < i) ? (i - context) : 0;
533 unsigned long k;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800534
535 /* Paint a few lines before the first interesting line. */
Matthijs Kooijmanaac38572013-05-15 19:42:14 +0200536 while (j < i) {
537 if (!(sline[j].flag & mark))
538 sline[j].flag |= no_pre_delete;
539 sline[j++].flag |= mark;
540 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800541
542 again:
Junio C Hamano46dc9412006-02-02 15:17:42 -0800543 /* we know up to i is to be included. where does the
544 * next uninteresting one start?
545 */
546 j = find_next(sline, mark, i, cnt, 1);
Junio C Hamano74065952006-04-11 14:31:31 -0700547 if (cnt < j)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800548 break; /* the rest are all interesting */
549
550 /* lookahead context lines */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800551 k = find_next(sline, mark, j, cnt, 0);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800552 j = adjust_hunk_tail(sline, all_mask, i, j);
553
554 if (k < j + context) {
555 /* k is interesting and [j,k) are not, but
556 * paint them interesting because the gap is small.
557 */
558 while (j < k)
559 sline[j++].flag |= mark;
560 i = k;
561 goto again;
562 }
563
564 /* j is the first uninteresting line and there is
Junio C Hamano46dc9412006-02-02 15:17:42 -0800565 * no overlap beyond it within context lines. Paint
566 * the trailing edge a bit.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800567 */
568 i = k;
Junio C Hamano74065952006-04-11 14:31:31 -0700569 k = (j + context < cnt+1) ? j + context : cnt+1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800570 while (j < k)
571 sline[j++].flag |= mark;
572 }
573 return 1;
574}
575
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800576static int make_hunks(struct sline *sline, unsigned long cnt,
Junio C Hamanod8f47902006-01-24 01:22:04 -0800577 int num_parent, int dense)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800578{
579 unsigned long all_mask = (1UL<<num_parent) - 1;
580 unsigned long mark = (1UL<<num_parent);
581 unsigned long i;
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800582 int has_interesting = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800583
Junio C Hamano74065952006-04-11 14:31:31 -0700584 for (i = 0; i <= cnt; i++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800585 if (interesting(&sline[i], all_mask))
586 sline[i].flag |= mark;
587 else
588 sline[i].flag &= ~mark;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800589 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800590 if (!dense)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800591 return give_context(sline, cnt, num_parent);
Junio C Hamanod8f47902006-01-24 01:22:04 -0800592
Junio C Hamano263eee22006-01-25 13:11:38 -0800593 /* Look at each hunk, and if we have changes from only one
594 * parent, or the changes are the same from all but one
595 * parent, mark that uninteresting.
Junio C Hamanod8f47902006-01-24 01:22:04 -0800596 */
597 i = 0;
Junio C Hamano74065952006-04-11 14:31:31 -0700598 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800599 unsigned long j, hunk_begin, hunk_end;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800600 unsigned long same_diff;
Junio C Hamano74065952006-04-11 14:31:31 -0700601 while (i <= cnt && !(sline[i].flag & mark))
Junio C Hamanod8f47902006-01-24 01:22:04 -0800602 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700603 if (cnt < i)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800604 break; /* No more interesting hunks */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800605 hunk_begin = i;
Junio C Hamano74065952006-04-11 14:31:31 -0700606 for (j = i + 1; j <= cnt; j++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800607 if (!(sline[j].flag & mark)) {
608 /* Look beyond the end to see if there
609 * is an interesting line after this
610 * hunk within context span.
611 */
612 unsigned long la; /* lookahead */
613 int contin = 0;
614 la = adjust_hunk_tail(sline, all_mask,
615 hunk_begin, j);
Junio C Hamano74065952006-04-11 14:31:31 -0700616 la = (la + context < cnt + 1) ?
617 (la + context) : cnt + 1;
René Scharfee5e9b562012-03-24 16:18:46 +0100618 while (la && j <= --la) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800619 if (sline[la].flag & mark) {
620 contin = 1;
621 break;
622 }
623 }
624 if (!contin)
625 break;
626 j = la;
627 }
628 }
629 hunk_end = j;
630
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800631 /* [i..hunk_end) are interesting. Now is it really
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800632 * interesting? We check if there are only two versions
633 * and the result matches one of them. That is, we look
634 * at:
635 * (+) line, which records lines added to which parents;
636 * this line appears in the result.
637 * (-) line, which records from what parents the line
638 * was removed; this line does not appear in the result.
639 * then check the set of parents the result has difference
640 * from, from all lines. If there are lines that has
641 * different set of parents that the result has differences
642 * from, that means we have more than two versions.
643 *
644 * Even when we have only two versions, if the result does
645 * not match any of the parents, the it should be considered
646 * interesting. In such a case, we would have all '+' line.
647 * After passing the above "two versions" test, that would
648 * appear as "the same set of parents" to be "all parents".
Junio C Hamanod8f47902006-01-24 01:22:04 -0800649 */
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800650 same_diff = 0;
651 has_interesting = 0;
652 for (j = i; j < hunk_end && !has_interesting; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800653 unsigned long this_diff = sline[j].flag & all_mask;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100654 struct lline *ll = sline[j].lost;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800655 if (this_diff) {
656 /* This has some changes. Is it the
657 * same as others?
658 */
659 if (!same_diff)
660 same_diff = this_diff;
661 else if (same_diff != this_diff) {
662 has_interesting = 1;
663 break;
664 }
665 }
666 while (ll && !has_interesting) {
667 /* Lost this line from these parents;
668 * who are they? Are they the same?
669 */
670 this_diff = ll->parent_map;
671 if (!same_diff)
672 same_diff = this_diff;
673 else if (same_diff != this_diff) {
674 has_interesting = 1;
675 }
676 ll = ll->next;
677 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800678 }
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800679
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800680 if (!has_interesting && same_diff != all_mask) {
Junio C Hamanod8f47902006-01-24 01:22:04 -0800681 /* This hunk is not that interesting after all */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800682 for (j = hunk_begin; j < hunk_end; j++)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800683 sline[j].flag &= ~mark;
684 }
685 i = hunk_end;
686 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800687
688 has_interesting = give_context(sline, cnt, num_parent);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800689 return has_interesting;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800690}
691
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800692static 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 -0800693{
694 l0 = sline[l0].p_lno[n];
695 l1 = sline[l1].p_lno[n];
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800696 printf(" -%lu,%lu", l0, l1-l0-null_context);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800697}
698
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700699static int hunk_comment_line(const char *bol)
700{
Junio C Hamano7a8ac592006-10-26 02:05:05 -0700701 int ch;
702
703 if (!bol)
704 return 0;
705 ch = *bol & 0xff;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700706 return (isalpha(ch) || ch == '_' || ch == '$');
707}
708
Junio C Hamano39280972008-08-27 19:48:01 -0700709static void show_line_to_eol(const char *line, int len, const char *reset)
710{
711 int saw_cr_at_eol = 0;
712 if (len < 0)
713 len = strlen(line);
714 saw_cr_at_eol = (len && line[len-1] == '\r');
715
716 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
717 reset,
718 saw_cr_at_eol ? "\r" : "");
719}
720
John Keeping41ee2ad2013-02-07 20:15:28 +0000721static void dump_sline(struct sline *sline, const char *line_prefix,
722 unsigned long cnt, int num_parent,
Thomas Rast21798702010-04-15 14:59:37 +0200723 int use_color, int result_deleted)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800724{
725 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700726 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800727 int i;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800728 unsigned long lno = 0;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700729 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100730 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700731 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
732 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
Jeff King8dbf3eb2015-05-27 16:48:46 -0400733 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700734 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800735
Thomas Rast21798702010-04-15 14:59:37 +0200736 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800737 return; /* result deleted */
738
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800739 while (1) {
Junio C Hamano8bc75742006-04-12 13:23:50 -0700740 unsigned long hunk_end;
741 unsigned long rlines;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700742 const char *hunk_comment = NULL;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800743 unsigned long null_context = 0;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700744
745 while (lno <= cnt && !(sline[lno].flag & mark)) {
746 if (hunk_comment_line(sline[lno].bol))
747 hunk_comment = sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800748 lno++;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700749 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700750 if (cnt < lno)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800751 break;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700752 else {
Junio C Hamano74065952006-04-11 14:31:31 -0700753 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700754 if (!(sline[hunk_end].flag & mark))
755 break;
756 }
Junio C Hamano74065952006-04-11 14:31:31 -0700757 rlines = hunk_end - lno;
758 if (cnt < hunk_end)
759 rlines--; /* pointing at the last delete hunk */
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800760
761 if (!context) {
762 /*
763 * Even when running with --unified=0, all
764 * lines in the hunk needs to be processed in
765 * the loop below in order to show the
766 * deletion recorded in lost_head. However,
767 * we do not want to show the resulting line
768 * with all blank context markers in such a
769 * case. Compensate.
770 */
771 unsigned long j;
772 for (j = lno; j < hunk_end; j++)
773 if (!(sline[j].flag & (mark-1)))
774 null_context++;
775 rlines -= null_context;
776 }
777
John Keeping41ee2ad2013-02-07 20:15:28 +0000778 printf("%s%s", line_prefix, c_frag);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800779 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800780 for (i = 0; i < num_parent; i++)
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800781 show_parent_lno(sline, lno, hunk_end, i, null_context);
Junio C Hamano74065952006-04-11 14:31:31 -0700782 printf(" +%lu,%lu ", lno+1, rlines);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800783 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700784
785 if (hunk_comment) {
786 int comment_end = 0;
787 for (i = 0; i < 40; i++) {
788 int ch = hunk_comment[i] & 0xff;
789 if (!ch || ch == '\n')
790 break;
791 if (!isspace(ch))
792 comment_end = i;
793 }
794 if (comment_end)
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100795 printf("%s%s %s%s", c_reset,
Jeff King8dbf3eb2015-05-27 16:48:46 -0400796 c_context, c_reset,
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100797 c_func);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700798 for (i = 0; i < comment_end; i++)
799 putchar(hunk_comment[i]);
800 }
801
Junio C Hamano567a03d2006-08-10 00:30:33 -0700802 printf("%s\n", c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800803 while (lno < hunk_end) {
804 struct lline *ll;
805 int j;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800806 unsigned long p_mask;
Benjamin Kramerfd13b212009-03-07 21:02:26 +0100807 struct sline *sl = &sline[lno++];
Antoine Pelisse99d32062013-03-23 18:23:28 +0100808 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800809 while (ll) {
John Keeping41ee2ad2013-02-07 20:15:28 +0000810 printf("%s%s", line_prefix, c_old);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800811 for (j = 0; j < num_parent; j++) {
812 if (ll->parent_map & (1UL<<j))
813 putchar('-');
814 else
815 putchar(' ');
816 }
Junio C Hamano39280972008-08-27 19:48:01 -0700817 show_line_to_eol(ll->line, -1, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800818 ll = ll->next;
819 }
Junio C Hamano74065952006-04-11 14:31:31 -0700820 if (cnt < lno)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700821 break;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800822 p_mask = 1;
John Keeping41ee2ad2013-02-07 20:15:28 +0000823 fputs(line_prefix, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800824 if (!(sl->flag & (mark-1))) {
825 /*
826 * This sline was here to hang the
827 * lost lines in front of it.
828 */
829 if (!context)
830 continue;
Jeff King8dbf3eb2015-05-27 16:48:46 -0400831 fputs(c_context, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800832 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700833 else
834 fputs(c_new, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800835 for (j = 0; j < num_parent; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800836 if (p_mask & sl->flag)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800837 putchar('+');
Junio C Hamano46dc9412006-02-02 15:17:42 -0800838 else
839 putchar(' ');
840 p_mask <<= 1;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800841 }
Junio C Hamano39280972008-08-27 19:48:01 -0700842 show_line_to_eol(sl->bol, sl->len, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800843 }
844 }
845}
846
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800847static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
848 int i, int j)
849{
850 /* We have already examined parent j and we know parent i
851 * and parent j are the same, so reuse the combined result
852 * of parent j for parent i.
853 */
854 unsigned long lno, imask, jmask;
855 imask = (1UL<<i);
856 jmask = (1UL<<j);
857
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700858 for (lno = 0; lno <= cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100859 struct lline *ll = sline->lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800860 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800861 while (ll) {
862 if (ll->parent_map & jmask)
863 ll->parent_map |= imask;
864 ll = ll->next;
865 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800866 if (sline->flag & jmask)
867 sline->flag |= imask;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800868 sline++;
869 }
Junio C Hamano44627312006-02-06 18:54:08 -0800870 /* the overall size of the file (sline[cnt]) */
871 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800872}
873
Junio C Hamano462a15b2007-12-26 16:51:19 -0800874static void dump_quoted_path(const char *head,
875 const char *prefix,
876 const char *path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000877 const char *line_prefix,
Junio C Hamano567a03d2006-08-10 00:30:33 -0700878 const char *c_meta, const char *c_reset)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800879{
Junio C Hamano462a15b2007-12-26 16:51:19 -0800880 static struct strbuf buf = STRBUF_INIT;
881
882 strbuf_reset(&buf);
John Keeping41ee2ad2013-02-07 20:15:28 +0000883 strbuf_addstr(&buf, line_prefix);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800884 strbuf_addstr(&buf, c_meta);
885 strbuf_addstr(&buf, head);
Junio C Hamanod5625092007-12-26 17:13:36 -0800886 quote_two_c_style(&buf, prefix, path, 0);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800887 strbuf_addstr(&buf, c_reset);
888 puts(buf.buf);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700889}
890
Jeff King7c978a02011-05-23 16:16:41 -0400891static void show_combined_header(struct combine_diff_path *elem,
892 int num_parent,
893 int dense,
894 struct rev_info *rev,
John Keeping41ee2ad2013-02-07 20:15:28 +0000895 const char *line_prefix,
Jeff King4d5f3472011-05-23 16:27:34 -0400896 int mode_differs,
897 int show_file_header)
Jeff King7c978a02011-05-23 16:16:41 -0400898{
899 struct diff_options *opt = &rev->diffopt;
brian m. carlson1ff57c12015-03-13 23:39:33 +0000900 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? GIT_SHA1_HEXSZ : DEFAULT_ABBREV;
Jeff King7c978a02011-05-23 16:16:41 -0400901 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
902 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
Jeff Kingf1c96262011-08-17 22:03:12 -0700903 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
904 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
Jeff King7c978a02011-05-23 16:16:41 -0400905 const char *abb;
906 int added = 0;
907 int deleted = 0;
908 int i;
909
910 if (rev->loginfo && !rev->no_commit_id)
911 show_log(rev);
912
913 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
John Keeping41ee2ad2013-02-07 20:15:28 +0000914 "", elem->path, line_prefix, c_meta, c_reset);
915 printf("%s%sindex ", line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400916 for (i = 0; i < num_parent; i++) {
brian m. carlson1ff57c12015-03-13 23:39:33 +0000917 abb = find_unique_abbrev(elem->parent[i].oid.hash,
Jeff King7c978a02011-05-23 16:16:41 -0400918 abbrev);
919 printf("%s%s", i ? "," : "", abb);
920 }
brian m. carlson1ff57c12015-03-13 23:39:33 +0000921 abb = find_unique_abbrev(elem->oid.hash, abbrev);
Jeff King7c978a02011-05-23 16:16:41 -0400922 printf("..%s%s\n", abb, c_reset);
923
924 if (mode_differs) {
925 deleted = !elem->mode;
926
927 /* We say it was added if nobody had it */
928 added = !deleted;
929 for (i = 0; added && i < num_parent; i++)
930 if (elem->parent[i].status !=
931 DIFF_STATUS_ADDED)
932 added = 0;
933 if (added)
John Keeping41ee2ad2013-02-07 20:15:28 +0000934 printf("%s%snew file mode %06o",
935 line_prefix, c_meta, elem->mode);
Jeff King7c978a02011-05-23 16:16:41 -0400936 else {
937 if (deleted)
John Keeping41ee2ad2013-02-07 20:15:28 +0000938 printf("%s%sdeleted file ",
939 line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400940 printf("mode ");
941 for (i = 0; i < num_parent; i++) {
942 printf("%s%06o", i ? "," : "",
943 elem->parent[i].mode);
944 }
945 if (elem->mode)
946 printf("..%06o", elem->mode);
947 }
948 printf("%s\n", c_reset);
949 }
950
Jeff King4d5f3472011-05-23 16:27:34 -0400951 if (!show_file_header)
952 return;
953
Jeff King7c978a02011-05-23 16:16:41 -0400954 if (added)
955 dump_quoted_path("--- ", "", "/dev/null",
John Keeping41ee2ad2013-02-07 20:15:28 +0000956 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400957 else
958 dump_quoted_path("--- ", a_prefix, elem->path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000959 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400960 if (deleted)
961 dump_quoted_path("+++ ", "", "/dev/null",
John Keeping41ee2ad2013-02-07 20:15:28 +0000962 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400963 else
964 dump_quoted_path("+++ ", b_prefix, elem->path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000965 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400966}
967
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700968static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
Junio C Hamano99694542011-08-04 11:39:10 -0700969 int dense, int working_tree_file,
970 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800971{
Linus Torvalds91539832006-04-17 11:59:32 -0700972 struct diff_options *opt = &rev->diffopt;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700973 unsigned long result_size, cnt, lno;
Thomas Rast21798702010-04-15 14:59:37 +0200974 int result_deleted = 0;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -0500975 char *result, *cp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800976 struct sline *sline; /* survived lines */
Junio C Hamano2454c962006-02-06 12:53:07 -0800977 int mode_differs = 0;
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700978 int i, show_hunks;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700979 mmfile_t result_file;
Jeff King4d5f3472011-05-23 16:27:34 -0400980 struct userdiff_driver *userdiff;
Jeff King0508fe52011-05-23 16:31:05 -0400981 struct userdiff_driver *textconv = NULL;
Jeff King4d5f3472011-05-23 16:27:34 -0400982 int is_binary;
John Keeping41ee2ad2013-02-07 20:15:28 +0000983 const char *line_prefix = diff_line_prefix(opt);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800984
Linus Torvaldsee1e5412006-05-13 13:23:48 -0700985 context = opt->context;
Jeff King4d5f3472011-05-23 16:27:34 -0400986 userdiff = userdiff_find_by_path(elem->path);
987 if (!userdiff)
988 userdiff = userdiff_find_by_name("default");
Jeff King0508fe52011-05-23 16:31:05 -0400989 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV))
990 textconv = userdiff_get_textconv(userdiff);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700991
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800992 /* Read the result of merge first */
Junio C Hamanof23fc772006-04-03 18:53:15 -0700993 if (!working_tree_file)
brian m. carlson1ff57c12015-03-13 23:39:33 +0000994 result = grab_blob(&elem->oid, elem->mode, &result_size,
Jeff King0508fe52011-05-23 16:31:05 -0400995 textconv, elem->path);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800996 else {
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800997 /* Used by diff-tree to read from the working tree */
Junio C Hamanoea726d02006-01-28 00:03:38 -0800998 struct stat st;
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800999 int fd = -1;
1000
1001 if (lstat(elem->path, &st) < 0)
1002 goto deleted_file;
1003
1004 if (S_ISLNK(st.st_mode)) {
Junio C Hamano912342d2008-12-17 12:37:58 -08001005 struct strbuf buf = STRBUF_INIT;
1006
1007 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
Nguyễn Thái Ngọc Duy4b94ec92016-05-08 16:47:36 +07001008 error_errno("readlink(%s)", elem->path);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001009 return;
1010 }
Junio C Hamano912342d2008-12-17 12:37:58 -08001011 result_size = buf.len;
1012 result = strbuf_detach(&buf, NULL);
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001013 elem->mode = canon_mode(st.st_mode);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001014 } else if (S_ISDIR(st.st_mode)) {
brian m. carlson1ff57c12015-03-13 23:39:33 +00001015 struct object_id oid;
1016 if (resolve_gitlink_ref(elem->path, "HEAD", oid.hash) < 0)
1017 result = grab_blob(&elem->oid, elem->mode,
Jeff King0508fe52011-05-23 16:31:05 -04001018 &result_size, NULL, NULL);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001019 else
brian m. carlson1ff57c12015-03-13 23:39:33 +00001020 result = grab_blob(&oid, elem->mode,
Jeff King0508fe52011-05-23 16:31:05 -04001021 &result_size, NULL, NULL);
1022 } else if (textconv) {
1023 struct diff_filespec *df = alloc_filespec(elem->path);
Jeff Kinge5450102012-07-28 11:03:01 -04001024 fill_filespec(df, null_sha1, 0, st.st_mode);
Jeff King0508fe52011-05-23 16:31:05 -04001025 result_size = fill_textconv(textconv, df, &result);
1026 free_filespec(df);
Kjetil Barvik91fcbcb2009-02-09 21:54:52 +01001027 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001028 size_t len = xsize_t(st.st_size);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001029 ssize_t done;
Johannes Sixta249a9b2007-03-03 20:38:00 +01001030 int is_file, i;
Junio C Hamanoea726d02006-01-28 00:03:38 -08001031
Junio C Hamano1b0c7172006-03-29 22:55:43 -08001032 elem->mode = canon_mode(st.st_mode);
Johannes Sixta249a9b2007-03-03 20:38:00 +01001033 /* if symlinks don't work, assume symlink if all parents
1034 * are symlinks
1035 */
1036 is_file = has_symlinks;
1037 for (i = 0; !is_file && i < num_parent; i++)
1038 is_file = !S_ISLNK(elem->parent[i].mode);
1039 if (!is_file)
1040 elem->mode = canon_mode(S_IFLNK);
1041
Junio C Hamanof23fc772006-04-03 18:53:15 -07001042 result_size = len;
Jeff King3733e692016-02-22 17:44:28 -05001043 result = xmallocz(len);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001044
1045 done = read_in_full(fd, result, len);
1046 if (done < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02001047 die_errno("read error '%s'", elem->path);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001048 else if (done < len)
1049 die("early EOF '%s'", elem->path);
1050
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001051 /* If not a fake symlink, apply filters, e.g. autocrlf */
1052 if (is_file) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001053 struct strbuf buf = STRBUF_INIT;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001054
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001055 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
1056 free(result);
1057 result = strbuf_detach(&buf, &len);
1058 result_size = len;
1059 }
1060 }
Junio C Hamanoea726d02006-01-28 00:03:38 -08001061 }
1062 else {
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001063 deleted_file:
Thomas Rast21798702010-04-15 14:59:37 +02001064 result_deleted = 1;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001065 result_size = 0;
Junio C Hamano713a11f2006-02-13 23:07:04 -08001066 elem->mode = 0;
Peter Eriksen28f75812006-07-25 09:30:18 +02001067 result = xcalloc(1, 1);
Junio C Hamanoea726d02006-01-28 00:03:38 -08001068 }
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001069
Junio C Hamanoea726d02006-01-28 00:03:38 -08001070 if (0 <= fd)
1071 close(fd);
1072 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001073
Jeff Kingc95b99b2011-05-23 16:16:59 -04001074 for (i = 0; i < num_parent; i++) {
1075 if (elem->parent[i].mode != elem->mode) {
1076 mode_differs = 1;
1077 break;
1078 }
1079 }
1080
Jeff King0508fe52011-05-23 16:31:05 -04001081 if (textconv)
1082 is_binary = 0;
1083 else if (userdiff->binary != -1)
Jeff King4d5f3472011-05-23 16:27:34 -04001084 is_binary = userdiff->binary;
1085 else {
1086 is_binary = buffer_is_binary(result, result_size);
1087 for (i = 0; !is_binary && i < num_parent; i++) {
1088 char *buf;
1089 unsigned long size;
brian m. carlson1ff57c12015-03-13 23:39:33 +00001090 buf = grab_blob(&elem->parent[i].oid,
Jeff King4d5f3472011-05-23 16:27:34 -04001091 elem->parent[i].mode,
Jeff King0508fe52011-05-23 16:31:05 -04001092 &size, NULL, NULL);
Jeff King4d5f3472011-05-23 16:27:34 -04001093 if (buffer_is_binary(buf, size))
1094 is_binary = 1;
1095 free(buf);
1096 }
1097 }
1098 if (is_binary) {
1099 show_combined_header(elem, num_parent, dense, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001100 line_prefix, mode_differs, 0);
Jeff King4d5f3472011-05-23 16:27:34 -04001101 printf("Binary files differ\n");
1102 free(result);
1103 return;
1104 }
1105
Junio C Hamano2386c292006-06-28 01:38:19 -07001106 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001107 if (*cp == '\n')
1108 cnt++;
1109 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001110 if (result_size && result[result_size-1] != '\n')
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001111 cnt++; /* incomplete line */
1112
Jeff King50a6c8e2016-02-22 17:44:35 -05001113 sline = xcalloc(st_add(cnt, 2), sizeof(*sline));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001114 sline[0].bol = result;
Junio C Hamano2386c292006-06-28 01:38:19 -07001115 for (lno = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001116 if (*cp == '\n') {
1117 sline[lno].len = cp - sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001118 lno++;
1119 if (lno < cnt)
1120 sline[lno].bol = cp + 1;
1121 }
1122 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001123 if (result_size && result[result_size-1] != '\n')
1124 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1125
1126 result_file.ptr = result;
1127 result_file.size = result_size;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001128
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001129 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1130 * for deletion hunk at the end.
1131 */
Jeff King50a6c8e2016-02-22 17:44:35 -05001132 sline[0].p_lno = xcalloc(st_mult(st_add(cnt, 2), num_parent), sizeof(unsigned long));
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001133 for (lno = 0; lno <= cnt; lno++)
Junio C Hamanof16706c2006-01-30 22:33:15 -08001134 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1135
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001136 for (i = 0; i < num_parent; i++) {
1137 int j;
1138 for (j = 0; j < i; j++) {
brian m. carlson1ff57c12015-03-13 23:39:33 +00001139 if (!oidcmp(&elem->parent[i].oid,
1140 &elem->parent[j].oid)) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001141 reuse_combine_diff(sline, cnt, i, j);
1142 break;
1143 }
1144 }
1145 if (i <= j)
brian m. carlson1ff57c12015-03-13 23:39:33 +00001146 combine_diff(&elem->parent[i].oid,
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001147 elem->parent[i].mode,
1148 &result_file, sline,
Jeff King0508fe52011-05-23 16:31:05 -04001149 cnt, i, num_parent, result_deleted,
Antoine Pelissefa04ae02013-03-14 22:03:14 +01001150 textconv, elem->path, opt->xdl_opts);
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001151 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001152
Junio C Hamano8828cdc2006-01-25 14:26:22 -08001153 show_hunks = make_hunks(sline, cnt, num_parent, dense);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001154
Junio C Hamano713a11f2006-02-13 23:07:04 -08001155 if (show_hunks || mode_differs || working_tree_file) {
Jeff King7c978a02011-05-23 16:16:41 -04001156 show_combined_header(elem, num_parent, dense, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001157 line_prefix, mode_differs, 1);
1158 dump_sline(sline, line_prefix, cnt, num_parent,
Jeff Kingf1c96262011-08-17 22:03:12 -07001159 opt->use_color, result_deleted);
Junio C Hamano8828cdc2006-01-25 14:26:22 -08001160 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001161 free(result);
1162
Junio C Hamano2386c292006-06-28 01:38:19 -07001163 for (lno = 0; lno < cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +01001164 if (sline[lno].lost) {
1165 struct lline *ll = sline[lno].lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001166 while (ll) {
1167 struct lline *tmp = ll;
1168 ll = ll->next;
1169 free(tmp);
1170 }
1171 }
1172 }
Junio C Hamano46dc9412006-02-02 15:17:42 -08001173 free(sline[0].p_lno);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001174 free(sline);
1175}
1176
Linus Torvalds91539832006-04-17 11:59:32 -07001177static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
Linus Torvaldsee638022006-02-09 10:30:28 -08001178{
Linus Torvalds91539832006-04-17 11:59:32 -07001179 struct diff_options *opt = &rev->diffopt;
Junio C Hamano77667052013-02-03 13:08:18 -08001180 int line_termination, inter_name_termination, i;
John Keeping41ee2ad2013-02-07 20:15:28 +00001181 const char *line_prefix = diff_line_prefix(opt);
Linus Torvaldsee638022006-02-09 10:30:28 -08001182
1183 line_termination = opt->line_termination;
1184 inter_name_termination = '\t';
1185 if (!line_termination)
1186 inter_name_termination = 0;
1187
Junio C Hamano44152782006-10-26 02:05:59 -07001188 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -07001189 show_log(rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001190
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001191
Timo Hirvonenc6744342006-06-24 20:21:53 +03001192 if (opt->output_format & DIFF_FORMAT_RAW) {
John Keeping41ee2ad2013-02-07 20:15:28 +00001193 printf("%s", line_prefix);
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001194
Junio C Hamano77667052013-02-03 13:08:18 -08001195 /* As many colons as there are parents */
1196 for (i = 0; i < num_parent; i++)
1197 putchar(':');
Linus Torvaldsee638022006-02-09 10:30:28 -08001198
Junio C Hamano0a798072006-02-09 15:23:06 -08001199 /* Show the modes */
Junio C Hamano77667052013-02-03 13:08:18 -08001200 for (i = 0; i < num_parent; i++)
1201 printf("%06o ", p->parent[i].mode);
1202 printf("%06o", p->mode);
Junio C Hamano0a798072006-02-09 15:23:06 -08001203
1204 /* Show sha1's */
1205 for (i = 0; i < num_parent; i++)
brian m. carlson1ff57c12015-03-13 23:39:33 +00001206 printf(" %s", diff_unique_abbrev(p->parent[i].oid.hash,
Junio C Hamano0a798072006-02-09 15:23:06 -08001207 opt->abbrev));
brian m. carlson1ff57c12015-03-13 23:39:33 +00001208 printf(" %s ", diff_unique_abbrev(p->oid.hash, opt->abbrev));
Junio C Hamano0a798072006-02-09 15:23:06 -08001209 }
1210
Timo Hirvonenc6744342006-06-24 20:21:53 +03001211 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamanod416df82006-02-10 02:30:52 -08001212 for (i = 0; i < num_parent; i++)
1213 putchar(p->parent[i].status);
1214 putchar(inter_name_termination);
1215 }
Junio C Hamano0a798072006-02-09 15:23:06 -08001216
Pierre Habouzit663af342007-09-20 00:42:15 +02001217 write_name_quoted(p->path, stdout, line_termination);
Junio C Hamano0a798072006-02-09 15:23:06 -08001218}
1219
Junio C Hamano99694542011-08-04 11:39:10 -07001220/*
1221 * The result (p->elem) is from the working tree and their
1222 * parents are typically from multiple stages during a merge
1223 * (i.e. diff-files) or the state in HEAD and in the index
1224 * (i.e. diff-index).
1225 */
Linus Torvalds91539832006-04-17 11:59:32 -07001226void show_combined_diff(struct combine_diff_path *p,
Junio C Hamano0a798072006-02-09 15:23:06 -08001227 int num_parent,
1228 int dense,
Linus Torvalds91539832006-04-17 11:59:32 -07001229 struct rev_info *rev)
Junio C Hamano0a798072006-02-09 15:23:06 -08001230{
Linus Torvalds91539832006-04-17 11:59:32 -07001231 struct diff_options *opt = &rev->diffopt;
John Keeping41ee2ad2013-02-07 20:15:28 +00001232
Timo Hirvonenc6744342006-06-24 20:21:53 +03001233 if (opt->output_format & (DIFF_FORMAT_RAW |
1234 DIFF_FORMAT_NAME |
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001235 DIFF_FORMAT_NAME_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -07001236 show_raw_diff(p, num_parent, rev);
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001237 else if (opt->output_format & DIFF_FORMAT_PATCH)
Junio C Hamano99694542011-08-04 11:39:10 -07001238 show_patch_diff(p, num_parent, dense, 1, rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001239}
1240
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001241static void free_combined_pair(struct diff_filepair *pair)
1242{
1243 free(pair->two);
1244 free(pair);
1245}
1246
1247/*
1248 * A combine_diff_path expresses N parents on the LHS against 1 merge
1249 * result. Synthesize a diff_filepair that has N entries on the "one"
1250 * side and 1 entry on the "two" side.
1251 *
1252 * In the future, we might want to add more data to combine_diff_path
1253 * so that we can fill fields we are ignoring (most notably, size) here,
1254 * but currently nobody uses it, so this should suffice for now.
1255 */
1256static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1257 int num_parent)
1258{
1259 int i;
1260 struct diff_filepair *pair;
1261 struct diff_filespec *pool;
1262
1263 pair = xmalloc(sizeof(*pair));
Jeff King50a6c8e2016-02-22 17:44:35 -05001264 pool = xcalloc(st_add(num_parent, 1), sizeof(struct diff_filespec));
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001265 pair->one = pool + 1;
1266 pair->two = pool;
1267
1268 for (i = 0; i < num_parent; i++) {
1269 pair->one[i].path = p->path;
1270 pair->one[i].mode = p->parent[i].mode;
brian m. carlson1ff57c12015-03-13 23:39:33 +00001271 hashcpy(pair->one[i].sha1, p->parent[i].oid.hash);
1272 pair->one[i].sha1_valid = !is_null_oid(&p->parent[i].oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001273 pair->one[i].has_more_entries = 1;
1274 }
1275 pair->one[num_parent - 1].has_more_entries = 0;
1276
1277 pair->two->path = p->path;
1278 pair->two->mode = p->mode;
brian m. carlson1ff57c12015-03-13 23:39:33 +00001279 hashcpy(pair->two->sha1, p->oid.hash);
1280 pair->two->sha1_valid = !is_null_oid(&p->oid);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001281 return pair;
1282}
1283
1284static void handle_combined_callback(struct diff_options *opt,
1285 struct combine_diff_path *paths,
1286 int num_parent,
1287 int num_paths)
1288{
1289 struct combine_diff_path *p;
1290 struct diff_queue_struct q;
1291 int i;
1292
1293 q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
1294 q.alloc = num_paths;
1295 q.nr = num_paths;
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001296 for (i = 0, p = paths; p; p = p->next)
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001297 q.queue[i++] = combined_pair(p, num_parent);
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001298 opt->format_callback(&q, opt, opt->format_callback_data);
1299 for (i = 0; i < num_paths; i++)
1300 free_combined_pair(q.queue[i]);
1301 free(q.queue);
1302}
1303
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001304static const char *path_path(void *obj)
1305{
1306 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1307
1308 return path->path;
1309}
1310
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001311
1312/* find set of paths that every parent touches */
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001313static struct combine_diff_path *find_paths_generic(const unsigned char *sha1,
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001314 const struct sha1_array *parents, struct diff_options *opt)
1315{
1316 struct combine_diff_path *paths = NULL;
1317 int i, num_parent = parents->nr;
1318
1319 int output_format = opt->output_format;
1320 const char *orderfile = opt->orderfile;
1321
1322 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1323 /* tell diff_tree to emit paths in sorted (=tree) order */
1324 opt->orderfile = NULL;
1325
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001326 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001327 for (i = 0; i < num_parent; i++) {
1328 /*
1329 * show stat against the first parent even when doing
1330 * combined diff.
1331 */
1332 int stat_opt = (output_format &
1333 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1334 if (i == 0 && stat_opt)
1335 opt->output_format = stat_opt;
1336 else
1337 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1338 diff_tree_sha1(parents->sha1[i], sha1, "", opt);
1339 diffcore_std(opt);
1340 paths = intersect_paths(paths, i, num_parent);
1341
1342 /* if showing diff, show it in requested order */
1343 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1344 orderfile) {
1345 diffcore_order(orderfile);
1346 }
1347
1348 diff_flush(opt);
1349 }
1350
1351 opt->output_format = output_format;
1352 opt->orderfile = orderfile;
1353 return paths;
1354}
1355
1356
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001357/*
1358 * find set of paths that everybody touches, assuming diff is run without
1359 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1360 */
1361static struct combine_diff_path *find_paths_multitree(
1362 const unsigned char *sha1, const struct sha1_array *parents,
1363 struct diff_options *opt)
1364{
1365 int i, nparent = parents->nr;
1366 const unsigned char **parents_sha1;
1367 struct combine_diff_path paths_head;
1368 struct strbuf base;
1369
Jeff Kingb32fa952016-02-22 17:44:25 -05001370 ALLOC_ARRAY(parents_sha1, nparent);
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001371 for (i = 0; i < nparent; i++)
1372 parents_sha1[i] = parents->sha1[i];
1373
1374 /* fake list head, so worker can assume it is non-NULL */
1375 paths_head.next = NULL;
1376
1377 strbuf_init(&base, PATH_MAX);
1378 diff_tree_paths(&paths_head, sha1, parents_sha1, nparent, &base, opt);
1379
1380 strbuf_release(&base);
1381 free(parents_sha1);
1382 return paths_head.next;
1383}
1384
1385
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001386void diff_tree_combined(const unsigned char *sha1,
René Scharfe0041f092011-12-17 11:15:48 +01001387 const struct sha1_array *parents,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001388 int dense,
1389 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001390{
Linus Torvalds91539832006-04-17 11:59:32 -07001391 struct diff_options *opt = &rev->diffopt;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001392 struct diff_options diffopts;
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001393 struct combine_diff_path *p, *paths;
René Scharfe0041f092011-12-17 11:15:48 +01001394 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001395 int need_generic_pathscan;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001396
Kirill Smelkov51af1882014-02-03 16:47:19 +04001397 /* nothing to do, if no parents */
1398 if (!num_parent)
1399 return;
1400
1401 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1402 needsep = 0;
1403 if (show_log_first) {
1404 show_log(rev);
1405
Junio C Hamanoe7cc0ed2014-06-06 11:35:01 -07001406 if (rev->verbose_header && opt->output_format &&
Jeff Kingb9c7d6e2014-07-29 13:56:48 -04001407 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1408 !commit_format_is_empty(rev->commit_format))
Kirill Smelkov51af1882014-02-03 16:47:19 +04001409 printf("%s%c", diff_line_prefix(opt),
1410 opt->line_termination);
1411 }
1412
Junio C Hamano5b236832006-02-09 14:35:19 -08001413 diffopts = *opt;
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +07001414 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01001415 DIFF_OPT_SET(&diffopts, RECURSIVE);
1416 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001417
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001418 /* find set of paths that everybody touches
1419 *
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001420 * NOTE
1421 *
1422 * Diffcore transformations are bound to diff_filespec and logic
1423 * comparing two entries - i.e. they do not apply directly to combine
1424 * diff.
1425 *
1426 * If some of such transformations is requested - we launch generic
1427 * path scanning, which works significantly slower compared to
1428 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1429 *
1430 * TODO some of the filters could be ported to work on
1431 * combine_diff_paths - i.e. all functionality that skips paths, so in
1432 * theory, we could end up having only multitree path scanning.
1433 *
1434 * NOTE please keep this semantically in sync with diffcore_std()
Kirill Smelkoveeb3f322014-02-03 16:47:20 +04001435 */
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04001436 need_generic_pathscan = opt->skip_stat_unmatch ||
1437 DIFF_OPT_TST(opt, FOLLOW_RENAMES) ||
1438 opt->break_opt != -1 ||
1439 opt->detect_rename ||
1440 opt->pickaxe ||
1441 opt->filter;
1442
1443
1444 if (need_generic_pathscan) {
1445 /*
1446 * NOTE generic case also handles --stat, as it computes
1447 * diff(sha1,parent_i) for all i to do the job, specifically
1448 * for parent0.
1449 */
1450 paths = find_paths_generic(sha1, parents, &diffopts);
1451 }
1452 else {
1453 int stat_opt;
1454 paths = find_paths_multitree(sha1, parents, &diffopts);
1455
1456 /*
1457 * show stat against the first parent even
1458 * when doing combined diff.
1459 */
1460 stat_opt = (opt->output_format &
1461 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1462 if (stat_opt) {
1463 diffopts.output_format = stat_opt;
1464
1465 diff_tree_sha1(parents->sha1[0], sha1, "", &diffopts);
1466 diffcore_std(&diffopts);
1467 if (opt->orderfile)
1468 diffcore_order(opt->orderfile);
1469 diff_flush(&diffopts);
1470 }
1471 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001472
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001473 /* find out number of surviving paths */
1474 for (num_paths = 0, p = paths; p; p = p->next)
1475 num_paths++;
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001476
1477 /* order paths according to diffcore_order */
1478 if (opt->orderfile && num_paths) {
1479 struct obj_order *o;
1480
Jeff Kingb32fa952016-02-22 17:44:25 -05001481 ALLOC_ARRAY(o, num_paths);
Kirill Smelkov8518ff82014-01-20 20:20:40 +04001482 for (i = 0, p = paths; p; p = p->next, i++)
1483 o[i].obj = p;
1484 order_objects(opt->orderfile, path_path, o, num_paths);
1485 for (i = 0; i < num_paths - 1; i++) {
1486 p = o[i].obj;
1487 p->next = o[i+1].obj;
1488 }
1489
1490 p = o[num_paths-1].obj;
1491 p->next = NULL;
1492 paths = o[0].obj;
1493 free(o);
1494 }
1495
1496
Junio C Hamanoe3c3a552006-02-05 22:25:00 -08001497 if (num_paths) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001498 if (opt->output_format & (DIFF_FORMAT_RAW |
1499 DIFF_FORMAT_NAME |
1500 DIFF_FORMAT_NAME_STATUS)) {
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001501 for (p = paths; p; p = p->next)
1502 show_raw_diff(p, num_parent, rev);
Junio C Hamano3969cf72006-06-27 15:08:19 -07001503 needsep = 1;
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001504 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07001505 else if (opt->output_format &
1506 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
Junio C Hamano3969cf72006-06-27 15:08:19 -07001507 needsep = 1;
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001508 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1509 handle_combined_callback(opt, paths, num_parent, num_paths);
1510
Timo Hirvonenc6744342006-06-24 20:21:53 +03001511 if (opt->output_format & DIFF_FORMAT_PATCH) {
Junio C Hamano3969cf72006-06-27 15:08:19 -07001512 if (needsep)
John Keeping41ee2ad2013-02-07 20:15:28 +00001513 printf("%s%c", diff_line_prefix(opt),
1514 opt->line_termination);
Kirill Smelkovaf82c782014-01-20 20:20:41 +04001515 for (p = paths; p; p = p->next)
1516 show_patch_diff(p, num_parent, dense,
1517 0, rev);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001518 }
1519 }
1520
1521 /* Clean things up */
1522 while (paths) {
Junio C Hamanoea726d02006-01-28 00:03:38 -08001523 struct combine_diff_path *tmp = paths;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001524 paths = paths->next;
1525 free(tmp);
1526 }
Clemens Buchacher46ec5102013-05-28 00:49:57 +02001527
Nguyễn Thái Ngọc Duybd1928d2013-07-14 15:35:58 +07001528 free_pathspec(&diffopts.pathspec);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001529}
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001530
René Scharfe82889292011-12-17 11:20:07 +01001531void diff_tree_combined_merge(const struct commit *commit, int dense,
1532 struct rev_info *rev)
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001533{
Thomas Rast53d00b32013-07-31 22:13:20 +02001534 struct commit_list *parent = get_saved_parents(rev, commit);
René Scharfe0041f092011-12-17 11:15:48 +01001535 struct sha1_array parents = SHA1_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001536
René Scharfe0041f092011-12-17 11:15:48 +01001537 while (parent) {
brian m. carlsoned1c9972015-11-10 02:22:29 +00001538 sha1_array_append(&parents, parent->item->object.oid.hash);
René Scharfe0041f092011-12-17 11:15:48 +01001539 parent = parent->next;
1540 }
brian m. carlsoned1c9972015-11-10 02:22:29 +00001541 diff_tree_combined(commit->object.oid.hash, &parents, dense, rev);
René Scharfe0041f092011-12-17 11:15:48 +01001542 sha1_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001543}