blob: 77d7872aafe659045e9ec228de97e87c9cea00a1 [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"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080013
Junio C Hamanoea726d02006-01-28 00:03:38 -080014static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080015{
16 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamanoea726d02006-01-28 00:03:38 -080017 struct combine_diff_path *p;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080018 int i;
19
20 if (!n) {
Junio C Hamanoea726d02006-01-28 00:03:38 -080021 struct combine_diff_path *list = NULL, **tail = &list;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080022 for (i = 0; i < q->nr; i++) {
23 int len;
24 const char *path;
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070025 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080026 continue;
27 path = q->queue[i]->two->path;
28 len = strlen(path);
Junio C Hamano2454c962006-02-06 12:53:07 -080029 p = xmalloc(combine_diff_path_size(num_parent, len));
Felipe Contreras4b25d092009-05-01 12:06:36 +030030 p->path = (char *) &(p->parent[num_parent]);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080031 memcpy(p->path, path, len);
32 p->path[len] = 0;
33 p->len = len;
34 p->next = NULL;
Junio C Hamano2454c962006-02-06 12:53:07 -080035 memset(p->parent, 0,
36 sizeof(p->parent[0]) * num_parent);
37
Shawn Pearcee7024962006-08-23 02:49:00 -040038 hashcpy(p->sha1, q->queue[i]->two->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080039 p->mode = q->queue[i]->two->mode;
Shawn Pearcee7024962006-08-23 02:49:00 -040040 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080041 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080042 p->parent[n].status = q->queue[i]->status;
Junio C Hamano5290a0f2006-01-25 03:34:10 -080043 *tail = p;
44 tail = &p->next;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080045 }
46 return list;
47 }
48
49 for (p = curr; p; p = p->next) {
50 int found = 0;
51 if (!p->len)
52 continue;
53 for (i = 0; i < q->nr; i++) {
54 const char *path;
55 int len;
56
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070057 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080058 continue;
59 path = q->queue[i]->two->path;
60 len = strlen(path);
61 if (len == p->len && !memcmp(path, p->path, len)) {
62 found = 1;
Shawn Pearcee7024962006-08-23 02:49:00 -040063 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080064 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080065 p->parent[n].status = q->queue[i]->status;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080066 break;
67 }
68 }
69 if (!found)
70 p->len = 0;
71 }
72 return curr;
73}
74
Junio C Hamanob469d8b2006-01-30 16:34:29 -080075/* Lines lost from parent */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080076struct lline {
Antoine Pelisse99d32062013-03-23 18:23:28 +010077 struct lline *next, *prev;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080078 int len;
79 unsigned long parent_map;
80 char line[FLEX_ARRAY];
81};
82
Antoine Pelisse99d32062013-03-23 18:23:28 +010083/* Lines lost from current parent (before coalescing) */
84struct plost {
85 struct lline *lost_head, *lost_tail;
86 int len;
87};
88
Junio C Hamanob469d8b2006-01-30 16:34:29 -080089/* Lines surviving in the merge result */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080090struct sline {
Antoine Pelisse99d32062013-03-23 18:23:28 +010091 /* Accumulated and coalesced lost lines */
92 struct lline *lost;
93 int lenlost;
94 struct plost plost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080095 char *bol;
96 int len;
Junio C Hamano46dc9412006-02-02 15:17:42 -080097 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
98 * we did not change it).
Junio C Hamanob469d8b2006-01-30 16:34:29 -080099 * bit N is used for "interesting" lines, including context.
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700100 * bit (N+1) is used for "do not show deletion before this".
Junio C Hamanob469d8b2006-01-30 16:34:29 -0800101 */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800102 unsigned long flag;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800103 unsigned long *p_lno;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800104};
105
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100106static int match_string_spaces(const char *line1, int len1,
107 const char *line2, int len2,
108 long flags)
109{
110 if (flags & XDF_WHITESPACE_FLAGS) {
111 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
112 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
113 }
114
115 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
116 return (len1 == len2 && !memcmp(line1, line2, len1));
117
118 while (len1 > 0 && len2 > 0) {
119 len1--;
120 len2--;
121 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
122 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
123 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
124 return 0;
125
126 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
127 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
128 }
129 if (line1[len1] != line2[len2])
130 return 0;
131 }
132
133 if (flags & XDF_IGNORE_WHITESPACE) {
134 /* Consume remaining spaces */
135 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
136 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
137 }
138
139 /* We matched full line1 and line2 */
140 if (!len1 && !len2)
141 return 1;
142
143 return 0;
144}
145
Antoine Pelisse99d32062013-03-23 18:23:28 +0100146enum coalesce_direction { MATCH, BASE, NEW };
147
148/* Coalesce new lines into base by finding LCS */
149static struct lline *coalesce_lines(struct lline *base, int *lenbase,
150 struct lline *new, int lennew,
151 unsigned long parent, long flags)
152{
153 int **lcs;
154 enum coalesce_direction **directions;
155 struct lline *baseend, *newend = NULL;
156 int i, j, origbaselen = *lenbase;
157
158 if (new == NULL)
159 return base;
160
161 if (base == NULL) {
162 *lenbase = lennew;
163 return new;
164 }
165
166 /*
167 * Coalesce new lines into base by finding the LCS
168 * - Create the table to run dynamic programing
169 * - Compute the LCS
170 * - Then reverse read the direction structure:
171 * - If we have MATCH, assign parent to base flag, and consume
172 * both baseend and newend
173 * - Else if we have BASE, consume baseend
174 * - Else if we have NEW, insert newend lline into base and
175 * consume newend
176 */
177 lcs = xcalloc(origbaselen + 1, sizeof(int*));
178 directions = xcalloc(origbaselen + 1, sizeof(enum coalesce_direction*));
179 for (i = 0; i < origbaselen + 1; i++) {
180 lcs[i] = xcalloc(lennew + 1, sizeof(int));
181 directions[i] = xcalloc(lennew + 1, sizeof(enum coalesce_direction));
182 directions[i][0] = BASE;
183 }
184 for (j = 1; j < lennew + 1; j++)
185 directions[0][j] = NEW;
186
187 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
188 for (j = 1, newend = new; j < lennew + 1; j++) {
189 if (match_string_spaces(baseend->line, baseend->len,
190 newend->line, newend->len, flags)) {
191 lcs[i][j] = lcs[i - 1][j - 1] + 1;
192 directions[i][j] = MATCH;
193 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
194 lcs[i][j] = lcs[i][j - 1];
195 directions[i][j] = NEW;
196 } else {
197 lcs[i][j] = lcs[i - 1][j];
198 directions[i][j] = BASE;
199 }
200 if (newend->next)
201 newend = newend->next;
202 }
203 if (baseend->next)
204 baseend = baseend->next;
205 }
206
207 for (i = 0; i < origbaselen + 1; i++)
208 free(lcs[i]);
209 free(lcs);
210
211 /* At this point, baseend and newend point to the end of each lists */
212 i--;
213 j--;
214 while (i != 0 || j != 0) {
215 if (directions[i][j] == MATCH) {
216 baseend->parent_map |= 1<<parent;
217 baseend = baseend->prev;
218 newend = newend->prev;
219 i--;
220 j--;
221 } else if (directions[i][j] == NEW) {
222 struct lline *lline;
223
224 lline = newend;
225 /* Remove lline from new list and update newend */
226 if (lline->prev)
227 lline->prev->next = lline->next;
228 else
229 new = lline->next;
230 if (lline->next)
231 lline->next->prev = lline->prev;
232
233 newend = lline->prev;
234 j--;
235
236 /* Add lline to base list */
237 if (baseend) {
238 lline->next = baseend->next;
239 lline->prev = baseend;
240 if (lline->prev)
241 lline->prev->next = lline;
242 }
243 else {
244 lline->next = base;
245 base = lline;
246 }
247 (*lenbase)++;
248
249 if (lline->next)
250 lline->next->prev = lline;
251
252 } else {
253 baseend = baseend->prev;
254 i--;
255 }
256 }
257
258 newend = new;
259 while (newend) {
260 struct lline *lline = newend;
261 newend = newend->next;
262 free(lline);
263 }
264
265 for (i = 0; i < origbaselen + 1; i++)
266 free(directions[i]);
267 free(directions);
268
269 return base;
270}
271
272static char *grab_blob(const unsigned char *sha1, unsigned int mode,
273 unsigned long *size, struct userdiff_driver *textconv,
274 const char *path)
275{
276 char *blob;
277 enum object_type type;
278
279 if (S_ISGITLINK(mode)) {
280 blob = xmalloc(100);
281 *size = snprintf(blob, 100,
282 "Subproject commit %s\n", sha1_to_hex(sha1));
283 } else if (is_null_sha1(sha1)) {
284 /* deleted blob */
285 *size = 0;
286 return xcalloc(1, 1);
287 } else if (textconv) {
288 struct diff_filespec *df = alloc_filespec(path);
289 fill_filespec(df, sha1, 1, mode);
290 *size = fill_textconv(textconv, df, &blob);
291 free_filespec(df);
292 } else {
293 blob = read_sha1_file(sha1, &type, size);
294 if (type != OBJ_BLOB)
295 die("object '%s' is not a blob!", sha1_to_hex(sha1));
296 }
297 return blob;
298}
299
300static void append_lost(struct sline *sline, int n, const char *line, int len)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800301{
302 struct lline *lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800303 unsigned long this_mask = (1UL<<n);
304 if (line[len-1] == '\n')
305 len--;
306
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800307 lline = xmalloc(sizeof(*lline) + len + 1);
308 lline->len = len;
309 lline->next = NULL;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100310 lline->prev = sline->plost.lost_tail;
311 if (lline->prev)
312 lline->prev->next = lline;
313 else
314 sline->plost.lost_head = lline;
315 sline->plost.lost_tail = lline;
316 sline->plost.len++;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800317 lline->parent_map = this_mask;
318 memcpy(lline->line, line, len);
319 lline->line[len] = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800320}
321
Junio C Hamanof23fc772006-04-03 18:53:15 -0700322struct combine_diff_state {
Junio C Hamanoa0fd3142006-04-06 22:29:55 -0700323 unsigned int lno;
324 int ob, on, nb, nn;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700325 unsigned long nmask;
326 int num_parent;
327 int n;
328 struct sline *sline;
329 struct sline *lost_bucket;
330};
331
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700332static void consume_line(void *state_, char *line, unsigned long len)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700333{
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700334 struct combine_diff_state *state = state_;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700335 if (5 < len && !memcmp("@@ -", line, 4)) {
336 if (parse_hunk_header(line, len,
337 &state->ob, &state->on,
338 &state->nb, &state->nn))
339 return;
340 state->lno = state->nb;
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700341 if (state->nn == 0) {
Junio C Hamanof23fc772006-04-03 18:53:15 -0700342 /* @@ -X,Y +N,0 @@ removed Y lines
343 * that would have come *after* line N
344 * in the result. Our lost buckets hang
345 * to the line after the removed lines,
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700346 *
347 * Note that this is correct even when N == 0,
348 * in which case the hunk removes the first
349 * line in the file.
Junio C Hamanof23fc772006-04-03 18:53:15 -0700350 */
351 state->lost_bucket = &state->sline[state->nb];
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700352 if (!state->nb)
353 state->nb = 1;
354 } else {
Junio C Hamanof23fc772006-04-03 18:53:15 -0700355 state->lost_bucket = &state->sline[state->nb-1];
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700356 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700357 if (!state->sline[state->nb-1].p_lno)
358 state->sline[state->nb-1].p_lno =
359 xcalloc(state->num_parent,
360 sizeof(unsigned long));
361 state->sline[state->nb-1].p_lno[state->n] = state->ob;
362 return;
363 }
364 if (!state->lost_bucket)
365 return; /* not in any hunk yet */
366 switch (line[0]) {
367 case '-':
Antoine Pelisse99d32062013-03-23 18:23:28 +0100368 append_lost(state->lost_bucket, state->n, line+1, len-1);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700369 break;
370 case '+':
371 state->sline[state->lno-1].flag |= state->nmask;
372 state->lno++;
373 break;
374 }
375}
376
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700377static void combine_diff(const unsigned char *parent, unsigned int mode,
378 mmfile_t *result_file,
Junio C Hamano2386c292006-06-28 01:38:19 -0700379 struct sline *sline, unsigned int cnt, int n,
Jeff King0508fe52011-05-23 16:31:05 -0400380 int num_parent, int result_deleted,
381 struct userdiff_driver *textconv,
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100382 const char *path, long flags)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800383{
Junio C Hamanof23fc772006-04-03 18:53:15 -0700384 unsigned int p_lno, lno;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800385 unsigned long nmask = (1UL << n);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700386 xpparam_t xpp;
387 xdemitconf_t xecfg;
388 mmfile_t parent_file;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700389 struct combine_diff_state state;
390 unsigned long sz;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800391
Thomas Rast21798702010-04-15 14:59:37 +0200392 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800393 return; /* result deleted */
394
Jeff King0508fe52011-05-23 16:31:05 -0400395 parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700396 parent_file.size = sz;
Brian Downing9ccd0a82008-10-25 15:30:37 +0200397 memset(&xpp, 0, sizeof(xpp));
Antoine Pelissefa04ae02013-03-14 22:03:14 +0100398 xpp.flags = flags;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100399 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700400 memset(&state, 0, sizeof(state));
401 state.nmask = nmask;
402 state.sline = sline;
403 state.lno = 1;
404 state.num_parent = num_parent;
405 state.n = n;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800406
Junio C Hamano8a3f5242008-08-13 23:18:22 -0700407 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
René Scharfedfea7902010-05-04 22:41:34 +0200408 &xpp, &xecfg);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700409 free(parent_file.ptr);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800410
411 /* Assign line numbers for this parent.
412 *
413 * sline[lno].p_lno[n] records the first line number
414 * (counting from 1) for parent N if the final hunk display
415 * started by showing sline[lno] (possibly showing the lost
416 * lines attached to it first).
417 */
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700418 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
Junio C Hamanof16706c2006-01-30 22:33:15 -0800419 struct lline *ll;
420 sline[lno].p_lno[n] = p_lno;
421
Antoine Pelisse99d32062013-03-23 18:23:28 +0100422 /* Coalesce new lines */
423 if (sline[lno].plost.lost_head) {
424 struct sline *sl = &sline[lno];
425 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
426 sl->plost.lost_head,
427 sl->plost.len, n, flags);
428 sl->plost.lost_head = sl->plost.lost_tail = NULL;
429 sl->plost.len = 0;
430 }
431
Junio C Hamanof16706c2006-01-30 22:33:15 -0800432 /* How many lines would this sline advance the p_lno? */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100433 ll = sline[lno].lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800434 while (ll) {
435 if (ll->parent_map & nmask)
436 p_lno++; /* '-' means parent had it */
437 ll = ll->next;
438 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700439 if (lno < cnt && !(sline[lno].flag & nmask))
Junio C Hamanof16706c2006-01-30 22:33:15 -0800440 p_lno++; /* no '+' means parent had it */
441 }
442 sline[lno].p_lno[n] = p_lno; /* trailer */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800443}
444
445static unsigned long context = 3;
446static char combine_marker = '@';
447
448static int interesting(struct sline *sline, unsigned long all_mask)
449{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800450 /* If some parents lost lines here, or if we have added to
451 * some parent, it is interesting.
452 */
Antoine Pelisse99d32062013-03-23 18:23:28 +0100453 return ((sline->flag & all_mask) || sline->lost);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800454}
455
Junio C Hamano3ec19092006-01-26 03:53:01 -0800456static unsigned long adjust_hunk_tail(struct sline *sline,
457 unsigned long all_mask,
458 unsigned long hunk_begin,
459 unsigned long i)
460{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800461 /* i points at the first uninteresting line. If the last line
462 * of the hunk was interesting only because it has some
463 * deletion, then it is not all that interesting for the
464 * purpose of giving trailing context lines. This is because
465 * we output '-' line and then unmodified sline[i-1] itself in
466 * that case which gives us one extra context line.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800467 */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800468 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800469 i--;
470 return i;
471}
472
Junio C Hamano46dc9412006-02-02 15:17:42 -0800473static unsigned long find_next(struct sline *sline,
474 unsigned long mark,
475 unsigned long i,
476 unsigned long cnt,
Junio C Hamano2386c292006-06-28 01:38:19 -0700477 int look_for_uninteresting)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800478{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800479 /* We have examined up to i-1 and are about to look at i.
480 * Find next interesting or uninteresting line. Here,
481 * "interesting" does not mean interesting(), but marked by
482 * the give_context() function below (i.e. it includes context
483 * lines that are not interesting to interesting() function
484 * that are surrounded by interesting() ones.
485 */
Junio C Hamano74065952006-04-11 14:31:31 -0700486 while (i <= cnt)
Junio C Hamano2386c292006-06-28 01:38:19 -0700487 if (look_for_uninteresting
Junio C Hamano46dc9412006-02-02 15:17:42 -0800488 ? !(sline[i].flag & mark)
489 : (sline[i].flag & mark))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800490 return i;
491 else
492 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700493 return i;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800494}
495
496static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
497{
498 unsigned long all_mask = (1UL<<num_parent) - 1;
499 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700500 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800501 unsigned long i;
502
Junio C Hamano46dc9412006-02-02 15:17:42 -0800503 /* Two groups of interesting lines may have a short gap of
Pavel Roskin82e5a822006-07-10 01:50:18 -0400504 * uninteresting lines. Connect such groups to give them a
Junio C Hamano46dc9412006-02-02 15:17:42 -0800505 * bit of context.
506 *
507 * We first start from what the interesting() function says,
508 * and mark them with "mark", and paint context lines with the
509 * mark. So interesting() would still say false for such context
510 * lines but they are treated as "interesting" in the end.
511 */
512 i = find_next(sline, mark, 0, cnt, 0);
Junio C Hamano74065952006-04-11 14:31:31 -0700513 if (cnt < i)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800514 return 0;
515
Junio C Hamano74065952006-04-11 14:31:31 -0700516 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800517 unsigned long j = (context < i) ? (i - context) : 0;
518 unsigned long k;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800519
520 /* Paint a few lines before the first interesting line. */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800521 while (j < i)
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700522 sline[j++].flag |= mark | no_pre_delete;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800523
524 again:
Junio C Hamano46dc9412006-02-02 15:17:42 -0800525 /* we know up to i is to be included. where does the
526 * next uninteresting one start?
527 */
528 j = find_next(sline, mark, i, cnt, 1);
Junio C Hamano74065952006-04-11 14:31:31 -0700529 if (cnt < j)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800530 break; /* the rest are all interesting */
531
532 /* lookahead context lines */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800533 k = find_next(sline, mark, j, cnt, 0);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800534 j = adjust_hunk_tail(sline, all_mask, i, j);
535
536 if (k < j + context) {
537 /* k is interesting and [j,k) are not, but
538 * paint them interesting because the gap is small.
539 */
540 while (j < k)
541 sline[j++].flag |= mark;
542 i = k;
543 goto again;
544 }
545
546 /* j is the first uninteresting line and there is
Junio C Hamano46dc9412006-02-02 15:17:42 -0800547 * no overlap beyond it within context lines. Paint
548 * the trailing edge a bit.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800549 */
550 i = k;
Junio C Hamano74065952006-04-11 14:31:31 -0700551 k = (j + context < cnt+1) ? j + context : cnt+1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800552 while (j < k)
553 sline[j++].flag |= mark;
554 }
555 return 1;
556}
557
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800558static int make_hunks(struct sline *sline, unsigned long cnt,
Junio C Hamanod8f47902006-01-24 01:22:04 -0800559 int num_parent, int dense)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800560{
561 unsigned long all_mask = (1UL<<num_parent) - 1;
562 unsigned long mark = (1UL<<num_parent);
563 unsigned long i;
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800564 int has_interesting = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800565
Junio C Hamano74065952006-04-11 14:31:31 -0700566 for (i = 0; i <= cnt; i++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800567 if (interesting(&sline[i], all_mask))
568 sline[i].flag |= mark;
569 else
570 sline[i].flag &= ~mark;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800571 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800572 if (!dense)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800573 return give_context(sline, cnt, num_parent);
Junio C Hamanod8f47902006-01-24 01:22:04 -0800574
Junio C Hamano263eee22006-01-25 13:11:38 -0800575 /* Look at each hunk, and if we have changes from only one
576 * parent, or the changes are the same from all but one
577 * parent, mark that uninteresting.
Junio C Hamanod8f47902006-01-24 01:22:04 -0800578 */
579 i = 0;
Junio C Hamano74065952006-04-11 14:31:31 -0700580 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800581 unsigned long j, hunk_begin, hunk_end;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800582 unsigned long same_diff;
Junio C Hamano74065952006-04-11 14:31:31 -0700583 while (i <= cnt && !(sline[i].flag & mark))
Junio C Hamanod8f47902006-01-24 01:22:04 -0800584 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700585 if (cnt < i)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800586 break; /* No more interesting hunks */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800587 hunk_begin = i;
Junio C Hamano74065952006-04-11 14:31:31 -0700588 for (j = i + 1; j <= cnt; j++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800589 if (!(sline[j].flag & mark)) {
590 /* Look beyond the end to see if there
591 * is an interesting line after this
592 * hunk within context span.
593 */
594 unsigned long la; /* lookahead */
595 int contin = 0;
596 la = adjust_hunk_tail(sline, all_mask,
597 hunk_begin, j);
Junio C Hamano74065952006-04-11 14:31:31 -0700598 la = (la + context < cnt + 1) ?
599 (la + context) : cnt + 1;
René Scharfee5e9b562012-03-24 16:18:46 +0100600 while (la && j <= --la) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800601 if (sline[la].flag & mark) {
602 contin = 1;
603 break;
604 }
605 }
606 if (!contin)
607 break;
608 j = la;
609 }
610 }
611 hunk_end = j;
612
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800613 /* [i..hunk_end) are interesting. Now is it really
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800614 * interesting? We check if there are only two versions
615 * and the result matches one of them. That is, we look
616 * at:
617 * (+) line, which records lines added to which parents;
618 * this line appears in the result.
619 * (-) line, which records from what parents the line
620 * was removed; this line does not appear in the result.
621 * then check the set of parents the result has difference
622 * from, from all lines. If there are lines that has
623 * different set of parents that the result has differences
624 * from, that means we have more than two versions.
625 *
626 * Even when we have only two versions, if the result does
627 * not match any of the parents, the it should be considered
628 * interesting. In such a case, we would have all '+' line.
629 * After passing the above "two versions" test, that would
630 * appear as "the same set of parents" to be "all parents".
Junio C Hamanod8f47902006-01-24 01:22:04 -0800631 */
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800632 same_diff = 0;
633 has_interesting = 0;
634 for (j = i; j < hunk_end && !has_interesting; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800635 unsigned long this_diff = sline[j].flag & all_mask;
Antoine Pelisse99d32062013-03-23 18:23:28 +0100636 struct lline *ll = sline[j].lost;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800637 if (this_diff) {
638 /* This has some changes. Is it the
639 * same as others?
640 */
641 if (!same_diff)
642 same_diff = this_diff;
643 else if (same_diff != this_diff) {
644 has_interesting = 1;
645 break;
646 }
647 }
648 while (ll && !has_interesting) {
649 /* Lost this line from these parents;
650 * who are they? Are they the same?
651 */
652 this_diff = ll->parent_map;
653 if (!same_diff)
654 same_diff = this_diff;
655 else if (same_diff != this_diff) {
656 has_interesting = 1;
657 }
658 ll = ll->next;
659 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800660 }
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800661
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800662 if (!has_interesting && same_diff != all_mask) {
Junio C Hamanod8f47902006-01-24 01:22:04 -0800663 /* This hunk is not that interesting after all */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800664 for (j = hunk_begin; j < hunk_end; j++)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800665 sline[j].flag &= ~mark;
666 }
667 i = hunk_end;
668 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800669
670 has_interesting = give_context(sline, cnt, num_parent);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800671 return has_interesting;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800672}
673
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800674static 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 -0800675{
676 l0 = sline[l0].p_lno[n];
677 l1 = sline[l1].p_lno[n];
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800678 printf(" -%lu,%lu", l0, l1-l0-null_context);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800679}
680
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700681static int hunk_comment_line(const char *bol)
682{
Junio C Hamano7a8ac592006-10-26 02:05:05 -0700683 int ch;
684
685 if (!bol)
686 return 0;
687 ch = *bol & 0xff;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700688 return (isalpha(ch) || ch == '_' || ch == '$');
689}
690
Junio C Hamano39280972008-08-27 19:48:01 -0700691static void show_line_to_eol(const char *line, int len, const char *reset)
692{
693 int saw_cr_at_eol = 0;
694 if (len < 0)
695 len = strlen(line);
696 saw_cr_at_eol = (len && line[len-1] == '\r');
697
698 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
699 reset,
700 saw_cr_at_eol ? "\r" : "");
701}
702
John Keeping41ee2ad2013-02-07 20:15:28 +0000703static void dump_sline(struct sline *sline, const char *line_prefix,
704 unsigned long cnt, int num_parent,
Thomas Rast21798702010-04-15 14:59:37 +0200705 int use_color, int result_deleted)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800706{
707 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700708 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800709 int i;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800710 unsigned long lno = 0;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700711 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100712 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700713 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
714 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
715 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
716 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800717
Thomas Rast21798702010-04-15 14:59:37 +0200718 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800719 return; /* result deleted */
720
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800721 while (1) {
Junio C Hamano8bc75742006-04-12 13:23:50 -0700722 unsigned long hunk_end;
723 unsigned long rlines;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700724 const char *hunk_comment = NULL;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800725 unsigned long null_context = 0;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700726
727 while (lno <= cnt && !(sline[lno].flag & mark)) {
728 if (hunk_comment_line(sline[lno].bol))
729 hunk_comment = sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800730 lno++;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700731 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700732 if (cnt < lno)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800733 break;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700734 else {
Junio C Hamano74065952006-04-11 14:31:31 -0700735 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700736 if (!(sline[hunk_end].flag & mark))
737 break;
738 }
Junio C Hamano74065952006-04-11 14:31:31 -0700739 rlines = hunk_end - lno;
740 if (cnt < hunk_end)
741 rlines--; /* pointing at the last delete hunk */
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800742
743 if (!context) {
744 /*
745 * Even when running with --unified=0, all
746 * lines in the hunk needs to be processed in
747 * the loop below in order to show the
748 * deletion recorded in lost_head. However,
749 * we do not want to show the resulting line
750 * with all blank context markers in such a
751 * case. Compensate.
752 */
753 unsigned long j;
754 for (j = lno; j < hunk_end; j++)
755 if (!(sline[j].flag & (mark-1)))
756 null_context++;
757 rlines -= null_context;
758 }
759
John Keeping41ee2ad2013-02-07 20:15:28 +0000760 printf("%s%s", line_prefix, c_frag);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800761 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800762 for (i = 0; i < num_parent; i++)
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800763 show_parent_lno(sline, lno, hunk_end, i, null_context);
Junio C Hamano74065952006-04-11 14:31:31 -0700764 printf(" +%lu,%lu ", lno+1, rlines);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800765 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700766
767 if (hunk_comment) {
768 int comment_end = 0;
769 for (i = 0; i < 40; i++) {
770 int ch = hunk_comment[i] & 0xff;
771 if (!ch || ch == '\n')
772 break;
773 if (!isspace(ch))
774 comment_end = i;
775 }
776 if (comment_end)
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100777 printf("%s%s %s%s", c_reset,
778 c_plain, c_reset,
779 c_func);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700780 for (i = 0; i < comment_end; i++)
781 putchar(hunk_comment[i]);
782 }
783
Junio C Hamano567a03d2006-08-10 00:30:33 -0700784 printf("%s\n", c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800785 while (lno < hunk_end) {
786 struct lline *ll;
787 int j;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800788 unsigned long p_mask;
Benjamin Kramerfd13b212009-03-07 21:02:26 +0100789 struct sline *sl = &sline[lno++];
Antoine Pelisse99d32062013-03-23 18:23:28 +0100790 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800791 while (ll) {
John Keeping41ee2ad2013-02-07 20:15:28 +0000792 printf("%s%s", line_prefix, c_old);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800793 for (j = 0; j < num_parent; j++) {
794 if (ll->parent_map & (1UL<<j))
795 putchar('-');
796 else
797 putchar(' ');
798 }
Junio C Hamano39280972008-08-27 19:48:01 -0700799 show_line_to_eol(ll->line, -1, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800800 ll = ll->next;
801 }
Junio C Hamano74065952006-04-11 14:31:31 -0700802 if (cnt < lno)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700803 break;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800804 p_mask = 1;
John Keeping41ee2ad2013-02-07 20:15:28 +0000805 fputs(line_prefix, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800806 if (!(sl->flag & (mark-1))) {
807 /*
808 * This sline was here to hang the
809 * lost lines in front of it.
810 */
811 if (!context)
812 continue;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700813 fputs(c_plain, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800814 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700815 else
816 fputs(c_new, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800817 for (j = 0; j < num_parent; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800818 if (p_mask & sl->flag)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800819 putchar('+');
Junio C Hamano46dc9412006-02-02 15:17:42 -0800820 else
821 putchar(' ');
822 p_mask <<= 1;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800823 }
Junio C Hamano39280972008-08-27 19:48:01 -0700824 show_line_to_eol(sl->bol, sl->len, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800825 }
826 }
827}
828
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800829static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
830 int i, int j)
831{
832 /* We have already examined parent j and we know parent i
833 * and parent j are the same, so reuse the combined result
834 * of parent j for parent i.
835 */
836 unsigned long lno, imask, jmask;
837 imask = (1UL<<i);
838 jmask = (1UL<<j);
839
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700840 for (lno = 0; lno <= cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +0100841 struct lline *ll = sline->lost;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800842 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800843 while (ll) {
844 if (ll->parent_map & jmask)
845 ll->parent_map |= imask;
846 ll = ll->next;
847 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800848 if (sline->flag & jmask)
849 sline->flag |= imask;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800850 sline++;
851 }
Junio C Hamano44627312006-02-06 18:54:08 -0800852 /* the overall size of the file (sline[cnt]) */
853 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800854}
855
Junio C Hamano462a15b2007-12-26 16:51:19 -0800856static void dump_quoted_path(const char *head,
857 const char *prefix,
858 const char *path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000859 const char *line_prefix,
Junio C Hamano567a03d2006-08-10 00:30:33 -0700860 const char *c_meta, const char *c_reset)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800861{
Junio C Hamano462a15b2007-12-26 16:51:19 -0800862 static struct strbuf buf = STRBUF_INIT;
863
864 strbuf_reset(&buf);
John Keeping41ee2ad2013-02-07 20:15:28 +0000865 strbuf_addstr(&buf, line_prefix);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800866 strbuf_addstr(&buf, c_meta);
867 strbuf_addstr(&buf, head);
Junio C Hamanod5625092007-12-26 17:13:36 -0800868 quote_two_c_style(&buf, prefix, path, 0);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800869 strbuf_addstr(&buf, c_reset);
870 puts(buf.buf);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700871}
872
Jeff King7c978a02011-05-23 16:16:41 -0400873static void show_combined_header(struct combine_diff_path *elem,
874 int num_parent,
875 int dense,
876 struct rev_info *rev,
John Keeping41ee2ad2013-02-07 20:15:28 +0000877 const char *line_prefix,
Jeff King4d5f3472011-05-23 16:27:34 -0400878 int mode_differs,
879 int show_file_header)
Jeff King7c978a02011-05-23 16:16:41 -0400880{
881 struct diff_options *opt = &rev->diffopt;
882 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
883 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
884 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
Jeff Kingf1c96262011-08-17 22:03:12 -0700885 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
886 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
Jeff King7c978a02011-05-23 16:16:41 -0400887 const char *abb;
888 int added = 0;
889 int deleted = 0;
890 int i;
891
892 if (rev->loginfo && !rev->no_commit_id)
893 show_log(rev);
894
895 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
John Keeping41ee2ad2013-02-07 20:15:28 +0000896 "", elem->path, line_prefix, c_meta, c_reset);
897 printf("%s%sindex ", line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400898 for (i = 0; i < num_parent; i++) {
899 abb = find_unique_abbrev(elem->parent[i].sha1,
900 abbrev);
901 printf("%s%s", i ? "," : "", abb);
902 }
903 abb = find_unique_abbrev(elem->sha1, abbrev);
904 printf("..%s%s\n", abb, c_reset);
905
906 if (mode_differs) {
907 deleted = !elem->mode;
908
909 /* We say it was added if nobody had it */
910 added = !deleted;
911 for (i = 0; added && i < num_parent; i++)
912 if (elem->parent[i].status !=
913 DIFF_STATUS_ADDED)
914 added = 0;
915 if (added)
John Keeping41ee2ad2013-02-07 20:15:28 +0000916 printf("%s%snew file mode %06o",
917 line_prefix, c_meta, elem->mode);
Jeff King7c978a02011-05-23 16:16:41 -0400918 else {
919 if (deleted)
John Keeping41ee2ad2013-02-07 20:15:28 +0000920 printf("%s%sdeleted file ",
921 line_prefix, c_meta);
Jeff King7c978a02011-05-23 16:16:41 -0400922 printf("mode ");
923 for (i = 0; i < num_parent; i++) {
924 printf("%s%06o", i ? "," : "",
925 elem->parent[i].mode);
926 }
927 if (elem->mode)
928 printf("..%06o", elem->mode);
929 }
930 printf("%s\n", c_reset);
931 }
932
Jeff King4d5f3472011-05-23 16:27:34 -0400933 if (!show_file_header)
934 return;
935
Jeff King7c978a02011-05-23 16:16:41 -0400936 if (added)
937 dump_quoted_path("--- ", "", "/dev/null",
John Keeping41ee2ad2013-02-07 20:15:28 +0000938 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400939 else
940 dump_quoted_path("--- ", a_prefix, elem->path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000941 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400942 if (deleted)
943 dump_quoted_path("+++ ", "", "/dev/null",
John Keeping41ee2ad2013-02-07 20:15:28 +0000944 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400945 else
946 dump_quoted_path("+++ ", b_prefix, elem->path,
John Keeping41ee2ad2013-02-07 20:15:28 +0000947 line_prefix, c_meta, c_reset);
Jeff King7c978a02011-05-23 16:16:41 -0400948}
949
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700950static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
Junio C Hamano99694542011-08-04 11:39:10 -0700951 int dense, int working_tree_file,
952 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800953{
Linus Torvalds91539832006-04-17 11:59:32 -0700954 struct diff_options *opt = &rev->diffopt;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700955 unsigned long result_size, cnt, lno;
Thomas Rast21798702010-04-15 14:59:37 +0200956 int result_deleted = 0;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -0500957 char *result, *cp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800958 struct sline *sline; /* survived lines */
Junio C Hamano2454c962006-02-06 12:53:07 -0800959 int mode_differs = 0;
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700960 int i, show_hunks;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700961 mmfile_t result_file;
Jeff King4d5f3472011-05-23 16:27:34 -0400962 struct userdiff_driver *userdiff;
Jeff King0508fe52011-05-23 16:31:05 -0400963 struct userdiff_driver *textconv = NULL;
Jeff King4d5f3472011-05-23 16:27:34 -0400964 int is_binary;
John Keeping41ee2ad2013-02-07 20:15:28 +0000965 const char *line_prefix = diff_line_prefix(opt);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800966
Linus Torvaldsee1e5412006-05-13 13:23:48 -0700967 context = opt->context;
Jeff King4d5f3472011-05-23 16:27:34 -0400968 userdiff = userdiff_find_by_path(elem->path);
969 if (!userdiff)
970 userdiff = userdiff_find_by_name("default");
Jeff King0508fe52011-05-23 16:31:05 -0400971 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV))
972 textconv = userdiff_get_textconv(userdiff);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700973
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800974 /* Read the result of merge first */
Junio C Hamanof23fc772006-04-03 18:53:15 -0700975 if (!working_tree_file)
Jeff King0508fe52011-05-23 16:31:05 -0400976 result = grab_blob(elem->sha1, elem->mode, &result_size,
977 textconv, elem->path);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800978 else {
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800979 /* Used by diff-tree to read from the working tree */
Junio C Hamanoea726d02006-01-28 00:03:38 -0800980 struct stat st;
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800981 int fd = -1;
982
983 if (lstat(elem->path, &st) < 0)
984 goto deleted_file;
985
986 if (S_ISLNK(st.st_mode)) {
Junio C Hamano912342d2008-12-17 12:37:58 -0800987 struct strbuf buf = STRBUF_INIT;
988
989 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800990 error("readlink(%s): %s", elem->path,
991 strerror(errno));
992 return;
993 }
Junio C Hamano912342d2008-12-17 12:37:58 -0800994 result_size = buf.len;
995 result = strbuf_detach(&buf, NULL);
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800996 elem->mode = canon_mode(st.st_mode);
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700997 } else if (S_ISDIR(st.st_mode)) {
998 unsigned char sha1[20];
999 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
Jeff King0508fe52011-05-23 16:31:05 -04001000 result = grab_blob(elem->sha1, elem->mode,
1001 &result_size, NULL, NULL);
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001002 else
Jeff King0508fe52011-05-23 16:31:05 -04001003 result = grab_blob(sha1, elem->mode,
1004 &result_size, NULL, NULL);
1005 } else if (textconv) {
1006 struct diff_filespec *df = alloc_filespec(elem->path);
Jeff Kinge5450102012-07-28 11:03:01 -04001007 fill_filespec(df, null_sha1, 0, st.st_mode);
Jeff King0508fe52011-05-23 16:31:05 -04001008 result_size = fill_textconv(textconv, df, &result);
1009 free_filespec(df);
Kjetil Barvik91fcbcb2009-02-09 21:54:52 +01001010 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001011 size_t len = xsize_t(st.st_size);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001012 ssize_t done;
Johannes Sixta249a9b2007-03-03 20:38:00 +01001013 int is_file, i;
Junio C Hamanoea726d02006-01-28 00:03:38 -08001014
Junio C Hamano1b0c7172006-03-29 22:55:43 -08001015 elem->mode = canon_mode(st.st_mode);
Johannes Sixta249a9b2007-03-03 20:38:00 +01001016 /* if symlinks don't work, assume symlink if all parents
1017 * are symlinks
1018 */
1019 is_file = has_symlinks;
1020 for (i = 0; !is_file && i < num_parent; i++)
1021 is_file = !S_ISLNK(elem->parent[i].mode);
1022 if (!is_file)
1023 elem->mode = canon_mode(S_IFLNK);
1024
Junio C Hamanof23fc772006-04-03 18:53:15 -07001025 result_size = len;
Junio C Hamanoea726d02006-01-28 00:03:38 -08001026 result = xmalloc(len + 1);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001027
1028 done = read_in_full(fd, result, len);
1029 if (done < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02001030 die_errno("read error '%s'", elem->path);
Heikki Orsilac697ad12008-05-03 16:27:26 +03001031 else if (done < len)
1032 die("early EOF '%s'", elem->path);
1033
Junio C Hamanoea726d02006-01-28 00:03:38 -08001034 result[len] = 0;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001035
1036 /* If not a fake symlink, apply filters, e.g. autocrlf */
1037 if (is_file) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001038 struct strbuf buf = STRBUF_INIT;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001039
Alexander Gavrilov5e568f92008-08-23 23:21:21 +04001040 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
1041 free(result);
1042 result = strbuf_detach(&buf, &len);
1043 result_size = len;
1044 }
1045 }
Junio C Hamanoea726d02006-01-28 00:03:38 -08001046 }
1047 else {
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001048 deleted_file:
Thomas Rast21798702010-04-15 14:59:37 +02001049 result_deleted = 1;
Junio C Hamanof23fc772006-04-03 18:53:15 -07001050 result_size = 0;
Junio C Hamano713a11f2006-02-13 23:07:04 -08001051 elem->mode = 0;
Peter Eriksen28f75812006-07-25 09:30:18 +02001052 result = xcalloc(1, 1);
Junio C Hamanoea726d02006-01-28 00:03:38 -08001053 }
Junio C Hamano4fc970c2007-02-25 22:24:47 -08001054
Junio C Hamanoea726d02006-01-28 00:03:38 -08001055 if (0 <= fd)
1056 close(fd);
1057 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001058
Jeff Kingc95b99b2011-05-23 16:16:59 -04001059 for (i = 0; i < num_parent; i++) {
1060 if (elem->parent[i].mode != elem->mode) {
1061 mode_differs = 1;
1062 break;
1063 }
1064 }
1065
Jeff King0508fe52011-05-23 16:31:05 -04001066 if (textconv)
1067 is_binary = 0;
1068 else if (userdiff->binary != -1)
Jeff King4d5f3472011-05-23 16:27:34 -04001069 is_binary = userdiff->binary;
1070 else {
1071 is_binary = buffer_is_binary(result, result_size);
1072 for (i = 0; !is_binary && i < num_parent; i++) {
1073 char *buf;
1074 unsigned long size;
1075 buf = grab_blob(elem->parent[i].sha1,
1076 elem->parent[i].mode,
Jeff King0508fe52011-05-23 16:31:05 -04001077 &size, NULL, NULL);
Jeff King4d5f3472011-05-23 16:27:34 -04001078 if (buffer_is_binary(buf, size))
1079 is_binary = 1;
1080 free(buf);
1081 }
1082 }
1083 if (is_binary) {
1084 show_combined_header(elem, num_parent, dense, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001085 line_prefix, mode_differs, 0);
Jeff King4d5f3472011-05-23 16:27:34 -04001086 printf("Binary files differ\n");
1087 free(result);
1088 return;
1089 }
1090
Junio C Hamano2386c292006-06-28 01:38:19 -07001091 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001092 if (*cp == '\n')
1093 cnt++;
1094 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001095 if (result_size && result[result_size-1] != '\n')
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001096 cnt++; /* incomplete line */
1097
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001098 sline = xcalloc(cnt+2, sizeof(*sline));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001099 sline[0].bol = result;
Junio C Hamano2386c292006-06-28 01:38:19 -07001100 for (lno = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001101 if (*cp == '\n') {
1102 sline[lno].len = cp - sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001103 lno++;
1104 if (lno < cnt)
1105 sline[lno].bol = cp + 1;
1106 }
1107 }
Junio C Hamanof23fc772006-04-03 18:53:15 -07001108 if (result_size && result[result_size-1] != '\n')
1109 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1110
1111 result_file.ptr = result;
1112 result_file.size = result_size;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001113
Junio C Hamano8a470eb2006-04-11 03:13:29 -07001114 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1115 * for deletion hunk at the end.
1116 */
1117 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
1118 for (lno = 0; lno <= cnt; lno++)
Junio C Hamanof16706c2006-01-30 22:33:15 -08001119 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1120
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001121 for (i = 0; i < num_parent; i++) {
1122 int j;
1123 for (j = 0; j < i; j++) {
David Rientjesa89fccd2006-08-17 11:54:57 -07001124 if (!hashcmp(elem->parent[i].sha1,
1125 elem->parent[j].sha1)) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001126 reuse_combine_diff(sline, cnt, i, j);
1127 break;
1128 }
1129 }
1130 if (i <= j)
Junio C Hamano7dae8b22009-04-29 12:49:52 -07001131 combine_diff(elem->parent[i].sha1,
1132 elem->parent[i].mode,
1133 &result_file, sline,
Jeff King0508fe52011-05-23 16:31:05 -04001134 cnt, i, num_parent, result_deleted,
Antoine Pelissefa04ae02013-03-14 22:03:14 +01001135 textconv, elem->path, opt->xdl_opts);
Junio C Hamano3c39e9b2006-02-01 23:29:03 -08001136 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001137
Junio C Hamano8828cdc2006-01-25 14:26:22 -08001138 show_hunks = make_hunks(sline, cnt, num_parent, dense);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001139
Junio C Hamano713a11f2006-02-13 23:07:04 -08001140 if (show_hunks || mode_differs || working_tree_file) {
Jeff King7c978a02011-05-23 16:16:41 -04001141 show_combined_header(elem, num_parent, dense, rev,
John Keeping41ee2ad2013-02-07 20:15:28 +00001142 line_prefix, mode_differs, 1);
1143 dump_sline(sline, line_prefix, cnt, num_parent,
Jeff Kingf1c96262011-08-17 22:03:12 -07001144 opt->use_color, result_deleted);
Junio C Hamano8828cdc2006-01-25 14:26:22 -08001145 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001146 free(result);
1147
Junio C Hamano2386c292006-06-28 01:38:19 -07001148 for (lno = 0; lno < cnt; lno++) {
Antoine Pelisse99d32062013-03-23 18:23:28 +01001149 if (sline[lno].lost) {
1150 struct lline *ll = sline[lno].lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001151 while (ll) {
1152 struct lline *tmp = ll;
1153 ll = ll->next;
1154 free(tmp);
1155 }
1156 }
1157 }
Junio C Hamano46dc9412006-02-02 15:17:42 -08001158 free(sline[0].p_lno);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001159 free(sline);
1160}
1161
Linus Torvalds91539832006-04-17 11:59:32 -07001162static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
Linus Torvaldsee638022006-02-09 10:30:28 -08001163{
Linus Torvalds91539832006-04-17 11:59:32 -07001164 struct diff_options *opt = &rev->diffopt;
Junio C Hamano77667052013-02-03 13:08:18 -08001165 int line_termination, inter_name_termination, i;
John Keeping41ee2ad2013-02-07 20:15:28 +00001166 const char *line_prefix = diff_line_prefix(opt);
Linus Torvaldsee638022006-02-09 10:30:28 -08001167
1168 line_termination = opt->line_termination;
1169 inter_name_termination = '\t';
1170 if (!line_termination)
1171 inter_name_termination = 0;
1172
Junio C Hamano44152782006-10-26 02:05:59 -07001173 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -07001174 show_log(rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001175
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001176
Timo Hirvonenc6744342006-06-24 20:21:53 +03001177 if (opt->output_format & DIFF_FORMAT_RAW) {
John Keeping41ee2ad2013-02-07 20:15:28 +00001178 printf("%s", line_prefix);
Junio C Hamanoa1d68be2013-02-14 10:29:59 -08001179
Junio C Hamano77667052013-02-03 13:08:18 -08001180 /* As many colons as there are parents */
1181 for (i = 0; i < num_parent; i++)
1182 putchar(':');
Linus Torvaldsee638022006-02-09 10:30:28 -08001183
Junio C Hamano0a798072006-02-09 15:23:06 -08001184 /* Show the modes */
Junio C Hamano77667052013-02-03 13:08:18 -08001185 for (i = 0; i < num_parent; i++)
1186 printf("%06o ", p->parent[i].mode);
1187 printf("%06o", p->mode);
Junio C Hamano0a798072006-02-09 15:23:06 -08001188
1189 /* Show sha1's */
1190 for (i = 0; i < num_parent; i++)
1191 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
1192 opt->abbrev));
1193 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
1194 }
1195
Timo Hirvonenc6744342006-06-24 20:21:53 +03001196 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamanod416df82006-02-10 02:30:52 -08001197 for (i = 0; i < num_parent; i++)
1198 putchar(p->parent[i].status);
1199 putchar(inter_name_termination);
1200 }
Junio C Hamano0a798072006-02-09 15:23:06 -08001201
Pierre Habouzit663af342007-09-20 00:42:15 +02001202 write_name_quoted(p->path, stdout, line_termination);
Junio C Hamano0a798072006-02-09 15:23:06 -08001203}
1204
Junio C Hamano99694542011-08-04 11:39:10 -07001205/*
1206 * The result (p->elem) is from the working tree and their
1207 * parents are typically from multiple stages during a merge
1208 * (i.e. diff-files) or the state in HEAD and in the index
1209 * (i.e. diff-index).
1210 */
Linus Torvalds91539832006-04-17 11:59:32 -07001211void show_combined_diff(struct combine_diff_path *p,
Junio C Hamano0a798072006-02-09 15:23:06 -08001212 int num_parent,
1213 int dense,
Linus Torvalds91539832006-04-17 11:59:32 -07001214 struct rev_info *rev)
Junio C Hamano0a798072006-02-09 15:23:06 -08001215{
Linus Torvalds91539832006-04-17 11:59:32 -07001216 struct diff_options *opt = &rev->diffopt;
John Keeping41ee2ad2013-02-07 20:15:28 +00001217
Junio C Hamano0a798072006-02-09 15:23:06 -08001218 if (!p->len)
Linus Torvalds91539832006-04-17 11:59:32 -07001219 return;
Timo Hirvonenc6744342006-06-24 20:21:53 +03001220 if (opt->output_format & (DIFF_FORMAT_RAW |
1221 DIFF_FORMAT_NAME |
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001222 DIFF_FORMAT_NAME_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -07001223 show_raw_diff(p, num_parent, rev);
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001224 else if (opt->output_format & DIFF_FORMAT_PATCH)
Junio C Hamano99694542011-08-04 11:39:10 -07001225 show_patch_diff(p, num_parent, dense, 1, rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001226}
1227
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001228static void free_combined_pair(struct diff_filepair *pair)
1229{
1230 free(pair->two);
1231 free(pair);
1232}
1233
1234/*
1235 * A combine_diff_path expresses N parents on the LHS against 1 merge
1236 * result. Synthesize a diff_filepair that has N entries on the "one"
1237 * side and 1 entry on the "two" side.
1238 *
1239 * In the future, we might want to add more data to combine_diff_path
1240 * so that we can fill fields we are ignoring (most notably, size) here,
1241 * but currently nobody uses it, so this should suffice for now.
1242 */
1243static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1244 int num_parent)
1245{
1246 int i;
1247 struct diff_filepair *pair;
1248 struct diff_filespec *pool;
1249
1250 pair = xmalloc(sizeof(*pair));
1251 pool = xcalloc(num_parent + 1, sizeof(struct diff_filespec));
1252 pair->one = pool + 1;
1253 pair->two = pool;
1254
1255 for (i = 0; i < num_parent; i++) {
1256 pair->one[i].path = p->path;
1257 pair->one[i].mode = p->parent[i].mode;
1258 hashcpy(pair->one[i].sha1, p->parent[i].sha1);
1259 pair->one[i].sha1_valid = !is_null_sha1(p->parent[i].sha1);
1260 pair->one[i].has_more_entries = 1;
1261 }
1262 pair->one[num_parent - 1].has_more_entries = 0;
1263
1264 pair->two->path = p->path;
1265 pair->two->mode = p->mode;
1266 hashcpy(pair->two->sha1, p->sha1);
1267 pair->two->sha1_valid = !is_null_sha1(p->sha1);
1268 return pair;
1269}
1270
1271static void handle_combined_callback(struct diff_options *opt,
1272 struct combine_diff_path *paths,
1273 int num_parent,
1274 int num_paths)
1275{
1276 struct combine_diff_path *p;
1277 struct diff_queue_struct q;
1278 int i;
1279
1280 q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
1281 q.alloc = num_paths;
1282 q.nr = num_paths;
1283 for (i = 0, p = paths; p; p = p->next) {
1284 if (!p->len)
1285 continue;
1286 q.queue[i++] = combined_pair(p, num_parent);
1287 }
1288 opt->format_callback(&q, opt, opt->format_callback_data);
1289 for (i = 0; i < num_paths; i++)
1290 free_combined_pair(q.queue[i]);
1291 free(q.queue);
1292}
1293
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001294void diff_tree_combined(const unsigned char *sha1,
René Scharfe0041f092011-12-17 11:15:48 +01001295 const struct sha1_array *parents,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001296 int dense,
1297 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001298{
Linus Torvalds91539832006-04-17 11:59:32 -07001299 struct diff_options *opt = &rev->diffopt;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001300 struct diff_options diffopts;
Junio C Hamanoea726d02006-01-28 00:03:38 -08001301 struct combine_diff_path *p, *paths = NULL;
René Scharfe0041f092011-12-17 11:15:48 +01001302 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001303
Junio C Hamano5b236832006-02-09 14:35:19 -08001304 diffopts = *opt;
Junio C Hamano3969cf72006-06-27 15:08:19 -07001305 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01001306 DIFF_OPT_SET(&diffopts, RECURSIVE);
1307 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001308
Junio C Hamano44152782006-10-26 02:05:59 -07001309 show_log_first = !!rev->loginfo && !rev->no_commit_id;
Junio C Hamano3969cf72006-06-27 15:08:19 -07001310 needsep = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001311 /* find set of paths that everybody touches */
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001312 for (i = 0; i < num_parent; i++) {
Junio C Hamano965f8032006-04-17 22:53:03 -07001313 /* show stat against the first parent even
1314 * when doing combined diff.
1315 */
Junio C Hamano74e2abe2006-10-12 03:01:00 -07001316 int stat_opt = (opt->output_format &
1317 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1318 if (i == 0 && stat_opt)
1319 diffopts.output_format = stat_opt;
Junio C Hamano965f8032006-04-17 22:53:03 -07001320 else
1321 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
René Scharfe0041f092011-12-17 11:15:48 +01001322 diff_tree_sha1(parents->sha1[i], sha1, "", &diffopts);
Junio C Hamano5b236832006-02-09 14:35:19 -08001323 diffcore_std(&diffopts);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001324 paths = intersect_paths(paths, i, num_parent);
Linus Torvaldseab144a2006-04-17 16:59:42 -07001325
Junio C Hamano3969cf72006-06-27 15:08:19 -07001326 if (show_log_first && i == 0) {
Adam Simpkins02865652008-04-29 01:32:59 -07001327 show_log(rev);
John Keeping41ee2ad2013-02-07 20:15:28 +00001328
Junio C Hamano3969cf72006-06-27 15:08:19 -07001329 if (rev->verbose_header && opt->output_format)
John Keeping41ee2ad2013-02-07 20:15:28 +00001330 printf("%s%c", diff_line_prefix(opt),
1331 opt->line_termination);
Junio C Hamano3969cf72006-06-27 15:08:19 -07001332 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001333 diff_flush(&diffopts);
1334 }
1335
1336 /* find out surviving paths */
1337 for (num_paths = 0, p = paths; p; p = p->next) {
1338 if (p->len)
1339 num_paths++;
1340 }
Junio C Hamanoe3c3a552006-02-05 22:25:00 -08001341 if (num_paths) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001342 if (opt->output_format & (DIFF_FORMAT_RAW |
1343 DIFF_FORMAT_NAME |
1344 DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001345 for (p = paths; p; p = p->next) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001346 if (p->len)
1347 show_raw_diff(p, num_parent, rev);
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001348 }
Junio C Hamano3969cf72006-06-27 15:08:19 -07001349 needsep = 1;
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001350 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07001351 else if (opt->output_format &
1352 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
Junio C Hamano3969cf72006-06-27 15:08:19 -07001353 needsep = 1;
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001354 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1355 handle_combined_callback(opt, paths, num_parent, num_paths);
1356
Timo Hirvonenc6744342006-06-24 20:21:53 +03001357 if (opt->output_format & DIFF_FORMAT_PATCH) {
Junio C Hamano3969cf72006-06-27 15:08:19 -07001358 if (needsep)
John Keeping41ee2ad2013-02-07 20:15:28 +00001359 printf("%s%c", diff_line_prefix(opt),
1360 opt->line_termination);
Timo Hirvonenc6744342006-06-24 20:21:53 +03001361 for (p = paths; p; p = p->next) {
1362 if (p->len)
1363 show_patch_diff(p, num_parent, dense,
Junio C Hamano99694542011-08-04 11:39:10 -07001364 0, rev);
Timo Hirvonenc6744342006-06-24 20:21:53 +03001365 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001366 }
1367 }
1368
1369 /* Clean things up */
1370 while (paths) {
Junio C Hamanoea726d02006-01-28 00:03:38 -08001371 struct combine_diff_path *tmp = paths;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001372 paths = paths->next;
1373 free(tmp);
1374 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001375}
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001376
René Scharfe82889292011-12-17 11:20:07 +01001377void diff_tree_combined_merge(const struct commit *commit, int dense,
1378 struct rev_info *rev)
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001379{
René Scharfe0041f092011-12-17 11:15:48 +01001380 struct commit_list *parent = commit->parents;
1381 struct sha1_array parents = SHA1_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001382
René Scharfe0041f092011-12-17 11:15:48 +01001383 while (parent) {
1384 sha1_array_append(&parents, parent->item->object.sha1);
1385 parent = parent->next;
1386 }
René Scharfe82889292011-12-17 11:20:07 +01001387 diff_tree_combined(commit->object.sha1, &parents, dense, rev);
René Scharfe0041f092011-12-17 11:15:48 +01001388 sha1_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001389}