blob: a2e8dcf8553ff15d7cfed8f8ec4735185ec162bb [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"
Linus Torvalds91539832006-04-17 11:59:32 -07008#include "log-tree.h"
Junio C Hamano7dae8b22009-04-29 12:49:52 -07009#include "refs.h"
Jeff King4d5f3472011-05-23 16:27:34 -040010#include "userdiff.h"
René Scharfe0041f092011-12-17 11:15:48 +010011#include "sha1-array.h"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080012
Junio C Hamanoea726d02006-01-28 00:03:38 -080013static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080014{
15 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamanoea726d02006-01-28 00:03:38 -080016 struct combine_diff_path *p;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080017 int i;
18
19 if (!n) {
Junio C Hamanoea726d02006-01-28 00:03:38 -080020 struct combine_diff_path *list = NULL, **tail = &list;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080021 for (i = 0; i < q->nr; i++) {
22 int len;
23 const char *path;
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070024 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080025 continue;
26 path = q->queue[i]->two->path;
27 len = strlen(path);
Junio C Hamano2454c962006-02-06 12:53:07 -080028 p = xmalloc(combine_diff_path_size(num_parent, len));
Felipe Contreras4b25d092009-05-01 12:06:36 +030029 p->path = (char *) &(p->parent[num_parent]);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080030 memcpy(p->path, path, len);
31 p->path[len] = 0;
32 p->len = len;
33 p->next = NULL;
Junio C Hamano2454c962006-02-06 12:53:07 -080034 memset(p->parent, 0,
35 sizeof(p->parent[0]) * num_parent);
36
Shawn Pearcee7024962006-08-23 02:49:00 -040037 hashcpy(p->sha1, q->queue[i]->two->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080038 p->mode = q->queue[i]->two->mode;
Shawn Pearcee7024962006-08-23 02:49:00 -040039 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080040 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080041 p->parent[n].status = q->queue[i]->status;
Junio C Hamano5290a0f2006-01-25 03:34:10 -080042 *tail = p;
43 tail = &p->next;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080044 }
45 return list;
46 }
47
48 for (p = curr; p; p = p->next) {
49 int found = 0;
50 if (!p->len)
51 continue;
52 for (i = 0; i < q->nr; i++) {
53 const char *path;
54 int len;
55
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070056 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080057 continue;
58 path = q->queue[i]->two->path;
59 len = strlen(path);
60 if (len == p->len && !memcmp(path, p->path, len)) {
61 found = 1;
Shawn Pearcee7024962006-08-23 02:49:00 -040062 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080063 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080064 p->parent[n].status = q->queue[i]->status;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080065 break;
66 }
67 }
68 if (!found)
69 p->len = 0;
70 }
71 return curr;
72}
73
Junio C Hamanob469d8b2006-01-30 16:34:29 -080074/* Lines lost from parent */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080075struct lline {
76 struct lline *next;
77 int len;
78 unsigned long parent_map;
79 char line[FLEX_ARRAY];
80};
81
Junio C Hamanob469d8b2006-01-30 16:34:29 -080082/* Lines surviving in the merge result */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080083struct sline {
84 struct lline *lost_head, **lost_tail;
Junio C Hamano55d5d5b2009-07-22 14:48:28 -070085 struct lline *next_lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080086 char *bol;
87 int len;
Junio C Hamano46dc9412006-02-02 15:17:42 -080088 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
89 * we did not change it).
Junio C Hamanob469d8b2006-01-30 16:34:29 -080090 * bit N is used for "interesting" lines, including context.
Junio C Hamanoc86fbe52008-06-18 23:59:41 -070091 * bit (N+1) is used for "do not show deletion before this".
Junio C Hamanob469d8b2006-01-30 16:34:29 -080092 */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080093 unsigned long flag;
Junio C Hamanof16706c2006-01-30 22:33:15 -080094 unsigned long *p_lno;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080095};
96
Jeff King0508fe52011-05-23 16:31:05 -040097static char *grab_blob(const unsigned char *sha1, unsigned int mode,
98 unsigned long *size, struct userdiff_driver *textconv,
99 const char *path)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800100{
101 char *blob;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500102 enum object_type type;
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700103
104 if (S_ISGITLINK(mode)) {
105 blob = xmalloc(100);
106 *size = snprintf(blob, 100,
107 "Subproject commit %s\n", sha1_to_hex(sha1));
108 } else if (is_null_sha1(sha1)) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800109 /* deleted blob */
110 *size = 0;
111 return xcalloc(1, 1);
Jeff King0508fe52011-05-23 16:31:05 -0400112 } else if (textconv) {
113 struct diff_filespec *df = alloc_filespec(path);
114 fill_filespec(df, sha1, mode);
115 *size = fill_textconv(textconv, df, &blob);
116 free_filespec(df);
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700117 } else {
118 blob = read_sha1_file(sha1, &type, size);
119 if (type != OBJ_BLOB)
120 die("object '%s' is not a blob!", sha1_to_hex(sha1));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800121 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800122 return blob;
123}
124
Junio C Hamanof23fc772006-04-03 18:53:15 -0700125static void append_lost(struct sline *sline, int n, const char *line, int len)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800126{
127 struct lline *lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800128 unsigned long this_mask = (1UL<<n);
129 if (line[len-1] == '\n')
130 len--;
131
132 /* Check to see if we can squash things */
133 if (sline->lost_head) {
Junio C Hamano55d5d5b2009-07-22 14:48:28 -0700134 lline = sline->next_lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800135 while (lline) {
136 if (lline->len == len &&
137 !memcmp(lline->line, line, len)) {
138 lline->parent_map |= this_mask;
Junio C Hamano55d5d5b2009-07-22 14:48:28 -0700139 sline->next_lost = lline->next;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800140 return;
141 }
142 lline = lline->next;
143 }
144 }
145
146 lline = xmalloc(sizeof(*lline) + len + 1);
147 lline->len = len;
148 lline->next = NULL;
149 lline->parent_map = this_mask;
150 memcpy(lline->line, line, len);
151 lline->line[len] = 0;
Junio C Hamano5290a0f2006-01-25 03:34:10 -0800152 *sline->lost_tail = lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800153 sline->lost_tail = &lline->next;
Junio C Hamano55d5d5b2009-07-22 14:48:28 -0700154 sline->next_lost = NULL;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800155}
156
Junio C Hamanof23fc772006-04-03 18:53:15 -0700157struct combine_diff_state {
Junio C Hamanoa0fd3142006-04-06 22:29:55 -0700158 unsigned int lno;
159 int ob, on, nb, nn;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700160 unsigned long nmask;
161 int num_parent;
162 int n;
163 struct sline *sline;
164 struct sline *lost_bucket;
165};
166
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700167static void consume_line(void *state_, char *line, unsigned long len)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700168{
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700169 struct combine_diff_state *state = state_;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700170 if (5 < len && !memcmp("@@ -", line, 4)) {
171 if (parse_hunk_header(line, len,
172 &state->ob, &state->on,
173 &state->nb, &state->nn))
174 return;
175 state->lno = state->nb;
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700176 if (state->nn == 0) {
Junio C Hamanof23fc772006-04-03 18:53:15 -0700177 /* @@ -X,Y +N,0 @@ removed Y lines
178 * that would have come *after* line N
179 * in the result. Our lost buckets hang
180 * to the line after the removed lines,
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700181 *
182 * Note that this is correct even when N == 0,
183 * in which case the hunk removes the first
184 * line in the file.
Junio C Hamanof23fc772006-04-03 18:53:15 -0700185 */
186 state->lost_bucket = &state->sline[state->nb];
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700187 if (!state->nb)
188 state->nb = 1;
189 } else {
Junio C Hamanof23fc772006-04-03 18:53:15 -0700190 state->lost_bucket = &state->sline[state->nb-1];
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700191 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700192 if (!state->sline[state->nb-1].p_lno)
193 state->sline[state->nb-1].p_lno =
194 xcalloc(state->num_parent,
195 sizeof(unsigned long));
196 state->sline[state->nb-1].p_lno[state->n] = state->ob;
Junio C Hamano55d5d5b2009-07-22 14:48:28 -0700197 state->lost_bucket->next_lost = state->lost_bucket->lost_head;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700198 return;
199 }
200 if (!state->lost_bucket)
201 return; /* not in any hunk yet */
202 switch (line[0]) {
203 case '-':
204 append_lost(state->lost_bucket, state->n, line+1, len-1);
205 break;
206 case '+':
207 state->sline[state->lno-1].flag |= state->nmask;
208 state->lno++;
209 break;
210 }
211}
212
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700213static void combine_diff(const unsigned char *parent, unsigned int mode,
214 mmfile_t *result_file,
Junio C Hamano2386c292006-06-28 01:38:19 -0700215 struct sline *sline, unsigned int cnt, int n,
Jeff King0508fe52011-05-23 16:31:05 -0400216 int num_parent, int result_deleted,
217 struct userdiff_driver *textconv,
218 const char *path)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800219{
Junio C Hamanof23fc772006-04-03 18:53:15 -0700220 unsigned int p_lno, lno;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800221 unsigned long nmask = (1UL << n);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700222 xpparam_t xpp;
223 xdemitconf_t xecfg;
224 mmfile_t parent_file;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700225 struct combine_diff_state state;
226 unsigned long sz;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800227
Thomas Rast21798702010-04-15 14:59:37 +0200228 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800229 return; /* result deleted */
230
Jeff King0508fe52011-05-23 16:31:05 -0400231 parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700232 parent_file.size = sz;
Brian Downing9ccd0a82008-10-25 15:30:37 +0200233 memset(&xpp, 0, sizeof(xpp));
René Scharfe582aa002010-05-02 15:04:41 +0200234 xpp.flags = 0;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100235 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700236 memset(&state, 0, sizeof(state));
237 state.nmask = nmask;
238 state.sline = sline;
239 state.lno = 1;
240 state.num_parent = num_parent;
241 state.n = n;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800242
Junio C Hamano8a3f5242008-08-13 23:18:22 -0700243 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
René Scharfedfea7902010-05-04 22:41:34 +0200244 &xpp, &xecfg);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700245 free(parent_file.ptr);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800246
247 /* Assign line numbers for this parent.
248 *
249 * sline[lno].p_lno[n] records the first line number
250 * (counting from 1) for parent N if the final hunk display
251 * started by showing sline[lno] (possibly showing the lost
252 * lines attached to it first).
253 */
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700254 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
Junio C Hamanof16706c2006-01-30 22:33:15 -0800255 struct lline *ll;
256 sline[lno].p_lno[n] = p_lno;
257
258 /* How many lines would this sline advance the p_lno? */
259 ll = sline[lno].lost_head;
260 while (ll) {
261 if (ll->parent_map & nmask)
262 p_lno++; /* '-' means parent had it */
263 ll = ll->next;
264 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700265 if (lno < cnt && !(sline[lno].flag & nmask))
Junio C Hamanof16706c2006-01-30 22:33:15 -0800266 p_lno++; /* no '+' means parent had it */
267 }
268 sline[lno].p_lno[n] = p_lno; /* trailer */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800269}
270
271static unsigned long context = 3;
272static char combine_marker = '@';
273
274static int interesting(struct sline *sline, unsigned long all_mask)
275{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800276 /* If some parents lost lines here, or if we have added to
277 * some parent, it is interesting.
278 */
279 return ((sline->flag & all_mask) || sline->lost_head);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800280}
281
Junio C Hamano3ec19092006-01-26 03:53:01 -0800282static unsigned long adjust_hunk_tail(struct sline *sline,
283 unsigned long all_mask,
284 unsigned long hunk_begin,
285 unsigned long i)
286{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800287 /* i points at the first uninteresting line. If the last line
288 * of the hunk was interesting only because it has some
289 * deletion, then it is not all that interesting for the
290 * purpose of giving trailing context lines. This is because
291 * we output '-' line and then unmodified sline[i-1] itself in
292 * that case which gives us one extra context line.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800293 */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800294 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800295 i--;
296 return i;
297}
298
Junio C Hamano46dc9412006-02-02 15:17:42 -0800299static unsigned long find_next(struct sline *sline,
300 unsigned long mark,
301 unsigned long i,
302 unsigned long cnt,
Junio C Hamano2386c292006-06-28 01:38:19 -0700303 int look_for_uninteresting)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800304{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800305 /* We have examined up to i-1 and are about to look at i.
306 * Find next interesting or uninteresting line. Here,
307 * "interesting" does not mean interesting(), but marked by
308 * the give_context() function below (i.e. it includes context
309 * lines that are not interesting to interesting() function
310 * that are surrounded by interesting() ones.
311 */
Junio C Hamano74065952006-04-11 14:31:31 -0700312 while (i <= cnt)
Junio C Hamano2386c292006-06-28 01:38:19 -0700313 if (look_for_uninteresting
Junio C Hamano46dc9412006-02-02 15:17:42 -0800314 ? !(sline[i].flag & mark)
315 : (sline[i].flag & mark))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800316 return i;
317 else
318 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700319 return i;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800320}
321
322static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
323{
324 unsigned long all_mask = (1UL<<num_parent) - 1;
325 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700326 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800327 unsigned long i;
328
Junio C Hamano46dc9412006-02-02 15:17:42 -0800329 /* Two groups of interesting lines may have a short gap of
Pavel Roskin82e5a822006-07-10 01:50:18 -0400330 * uninteresting lines. Connect such groups to give them a
Junio C Hamano46dc9412006-02-02 15:17:42 -0800331 * bit of context.
332 *
333 * We first start from what the interesting() function says,
334 * and mark them with "mark", and paint context lines with the
335 * mark. So interesting() would still say false for such context
336 * lines but they are treated as "interesting" in the end.
337 */
338 i = find_next(sline, mark, 0, cnt, 0);
Junio C Hamano74065952006-04-11 14:31:31 -0700339 if (cnt < i)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800340 return 0;
341
Junio C Hamano74065952006-04-11 14:31:31 -0700342 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800343 unsigned long j = (context < i) ? (i - context) : 0;
344 unsigned long k;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800345
346 /* Paint a few lines before the first interesting line. */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800347 while (j < i)
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700348 sline[j++].flag |= mark | no_pre_delete;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800349
350 again:
Junio C Hamano46dc9412006-02-02 15:17:42 -0800351 /* we know up to i is to be included. where does the
352 * next uninteresting one start?
353 */
354 j = find_next(sline, mark, i, cnt, 1);
Junio C Hamano74065952006-04-11 14:31:31 -0700355 if (cnt < j)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800356 break; /* the rest are all interesting */
357
358 /* lookahead context lines */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800359 k = find_next(sline, mark, j, cnt, 0);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800360 j = adjust_hunk_tail(sline, all_mask, i, j);
361
362 if (k < j + context) {
363 /* k is interesting and [j,k) are not, but
364 * paint them interesting because the gap is small.
365 */
366 while (j < k)
367 sline[j++].flag |= mark;
368 i = k;
369 goto again;
370 }
371
372 /* j is the first uninteresting line and there is
Junio C Hamano46dc9412006-02-02 15:17:42 -0800373 * no overlap beyond it within context lines. Paint
374 * the trailing edge a bit.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800375 */
376 i = k;
Junio C Hamano74065952006-04-11 14:31:31 -0700377 k = (j + context < cnt+1) ? j + context : cnt+1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800378 while (j < k)
379 sline[j++].flag |= mark;
380 }
381 return 1;
382}
383
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800384static int make_hunks(struct sline *sline, unsigned long cnt,
Junio C Hamanod8f47902006-01-24 01:22:04 -0800385 int num_parent, int dense)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800386{
387 unsigned long all_mask = (1UL<<num_parent) - 1;
388 unsigned long mark = (1UL<<num_parent);
389 unsigned long i;
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800390 int has_interesting = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800391
Junio C Hamano74065952006-04-11 14:31:31 -0700392 for (i = 0; i <= cnt; i++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800393 if (interesting(&sline[i], all_mask))
394 sline[i].flag |= mark;
395 else
396 sline[i].flag &= ~mark;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800397 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800398 if (!dense)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800399 return give_context(sline, cnt, num_parent);
Junio C Hamanod8f47902006-01-24 01:22:04 -0800400
Junio C Hamano263eee22006-01-25 13:11:38 -0800401 /* Look at each hunk, and if we have changes from only one
402 * parent, or the changes are the same from all but one
403 * parent, mark that uninteresting.
Junio C Hamanod8f47902006-01-24 01:22:04 -0800404 */
405 i = 0;
Junio C Hamano74065952006-04-11 14:31:31 -0700406 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800407 unsigned long j, hunk_begin, hunk_end;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800408 unsigned long same_diff;
Junio C Hamano74065952006-04-11 14:31:31 -0700409 while (i <= cnt && !(sline[i].flag & mark))
Junio C Hamanod8f47902006-01-24 01:22:04 -0800410 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700411 if (cnt < i)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800412 break; /* No more interesting hunks */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800413 hunk_begin = i;
Junio C Hamano74065952006-04-11 14:31:31 -0700414 for (j = i + 1; j <= cnt; j++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800415 if (!(sline[j].flag & mark)) {
416 /* Look beyond the end to see if there
417 * is an interesting line after this
418 * hunk within context span.
419 */
420 unsigned long la; /* lookahead */
421 int contin = 0;
422 la = adjust_hunk_tail(sline, all_mask,
423 hunk_begin, j);
Junio C Hamano74065952006-04-11 14:31:31 -0700424 la = (la + context < cnt + 1) ?
425 (la + context) : cnt + 1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800426 while (j <= --la) {
427 if (sline[la].flag & mark) {
428 contin = 1;
429 break;
430 }
431 }
432 if (!contin)
433 break;
434 j = la;
435 }
436 }
437 hunk_end = j;
438
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800439 /* [i..hunk_end) are interesting. Now is it really
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800440 * interesting? We check if there are only two versions
441 * and the result matches one of them. That is, we look
442 * at:
443 * (+) line, which records lines added to which parents;
444 * this line appears in the result.
445 * (-) line, which records from what parents the line
446 * was removed; this line does not appear in the result.
447 * then check the set of parents the result has difference
448 * from, from all lines. If there are lines that has
449 * different set of parents that the result has differences
450 * from, that means we have more than two versions.
451 *
452 * Even when we have only two versions, if the result does
453 * not match any of the parents, the it should be considered
454 * interesting. In such a case, we would have all '+' line.
455 * After passing the above "two versions" test, that would
456 * appear as "the same set of parents" to be "all parents".
Junio C Hamanod8f47902006-01-24 01:22:04 -0800457 */
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800458 same_diff = 0;
459 has_interesting = 0;
460 for (j = i; j < hunk_end && !has_interesting; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800461 unsigned long this_diff = sline[j].flag & all_mask;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800462 struct lline *ll = sline[j].lost_head;
463 if (this_diff) {
464 /* This has some changes. Is it the
465 * same as others?
466 */
467 if (!same_diff)
468 same_diff = this_diff;
469 else if (same_diff != this_diff) {
470 has_interesting = 1;
471 break;
472 }
473 }
474 while (ll && !has_interesting) {
475 /* Lost this line from these parents;
476 * who are they? Are they the same?
477 */
478 this_diff = ll->parent_map;
479 if (!same_diff)
480 same_diff = this_diff;
481 else if (same_diff != this_diff) {
482 has_interesting = 1;
483 }
484 ll = ll->next;
485 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800486 }
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800487
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800488 if (!has_interesting && same_diff != all_mask) {
Junio C Hamanod8f47902006-01-24 01:22:04 -0800489 /* This hunk is not that interesting after all */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800490 for (j = hunk_begin; j < hunk_end; j++)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800491 sline[j].flag &= ~mark;
492 }
493 i = hunk_end;
494 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800495
496 has_interesting = give_context(sline, cnt, num_parent);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800497 return has_interesting;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800498}
499
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800500static 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 -0800501{
502 l0 = sline[l0].p_lno[n];
503 l1 = sline[l1].p_lno[n];
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800504 printf(" -%lu,%lu", l0, l1-l0-null_context);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800505}
506
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700507static int hunk_comment_line(const char *bol)
508{
Junio C Hamano7a8ac592006-10-26 02:05:05 -0700509 int ch;
510
511 if (!bol)
512 return 0;
513 ch = *bol & 0xff;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700514 return (isalpha(ch) || ch == '_' || ch == '$');
515}
516
Junio C Hamano39280972008-08-27 19:48:01 -0700517static void show_line_to_eol(const char *line, int len, const char *reset)
518{
519 int saw_cr_at_eol = 0;
520 if (len < 0)
521 len = strlen(line);
522 saw_cr_at_eol = (len && line[len-1] == '\r');
523
524 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
525 reset,
526 saw_cr_at_eol ? "\r" : "");
527}
528
Junio C Hamano567a03d2006-08-10 00:30:33 -0700529static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
Thomas Rast21798702010-04-15 14:59:37 +0200530 int use_color, int result_deleted)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800531{
532 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700533 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800534 int i;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800535 unsigned long lno = 0;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700536 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100537 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700538 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
539 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
540 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
541 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800542
Thomas Rast21798702010-04-15 14:59:37 +0200543 if (result_deleted)
Junio C Hamano44627312006-02-06 18:54:08 -0800544 return; /* result deleted */
545
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800546 while (1) {
Junio C Hamano8bc75742006-04-12 13:23:50 -0700547 unsigned long hunk_end;
548 unsigned long rlines;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700549 const char *hunk_comment = NULL;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800550 unsigned long null_context = 0;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700551
552 while (lno <= cnt && !(sline[lno].flag & mark)) {
553 if (hunk_comment_line(sline[lno].bol))
554 hunk_comment = sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800555 lno++;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700556 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700557 if (cnt < lno)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800558 break;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700559 else {
Junio C Hamano74065952006-04-11 14:31:31 -0700560 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700561 if (!(sline[hunk_end].flag & mark))
562 break;
563 }
Junio C Hamano74065952006-04-11 14:31:31 -0700564 rlines = hunk_end - lno;
565 if (cnt < hunk_end)
566 rlines--; /* pointing at the last delete hunk */
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800567
568 if (!context) {
569 /*
570 * Even when running with --unified=0, all
571 * lines in the hunk needs to be processed in
572 * the loop below in order to show the
573 * deletion recorded in lost_head. However,
574 * we do not want to show the resulting line
575 * with all blank context markers in such a
576 * case. Compensate.
577 */
578 unsigned long j;
579 for (j = lno; j < hunk_end; j++)
580 if (!(sline[j].flag & (mark-1)))
581 null_context++;
582 rlines -= null_context;
583 }
584
Junio C Hamano567a03d2006-08-10 00:30:33 -0700585 fputs(c_frag, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800586 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800587 for (i = 0; i < num_parent; i++)
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800588 show_parent_lno(sline, lno, hunk_end, i, null_context);
Junio C Hamano74065952006-04-11 14:31:31 -0700589 printf(" +%lu,%lu ", lno+1, rlines);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800590 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700591
592 if (hunk_comment) {
593 int comment_end = 0;
594 for (i = 0; i < 40; i++) {
595 int ch = hunk_comment[i] & 0xff;
596 if (!ch || ch == '\n')
597 break;
598 if (!isspace(ch))
599 comment_end = i;
600 }
601 if (comment_end)
Bert Wesarg89cb73a2009-11-27 07:55:18 +0100602 printf("%s%s %s%s", c_reset,
603 c_plain, c_reset,
604 c_func);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700605 for (i = 0; i < comment_end; i++)
606 putchar(hunk_comment[i]);
607 }
608
Junio C Hamano567a03d2006-08-10 00:30:33 -0700609 printf("%s\n", c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800610 while (lno < hunk_end) {
611 struct lline *ll;
612 int j;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800613 unsigned long p_mask;
Benjamin Kramerfd13b212009-03-07 21:02:26 +0100614 struct sline *sl = &sline[lno++];
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700615 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost_head;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800616 while (ll) {
Junio C Hamano567a03d2006-08-10 00:30:33 -0700617 fputs(c_old, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800618 for (j = 0; j < num_parent; j++) {
619 if (ll->parent_map & (1UL<<j))
620 putchar('-');
621 else
622 putchar(' ');
623 }
Junio C Hamano39280972008-08-27 19:48:01 -0700624 show_line_to_eol(ll->line, -1, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800625 ll = ll->next;
626 }
Junio C Hamano74065952006-04-11 14:31:31 -0700627 if (cnt < lno)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700628 break;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800629 p_mask = 1;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800630 if (!(sl->flag & (mark-1))) {
631 /*
632 * This sline was here to hang the
633 * lost lines in front of it.
634 */
635 if (!context)
636 continue;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700637 fputs(c_plain, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800638 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700639 else
640 fputs(c_new, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800641 for (j = 0; j < num_parent; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800642 if (p_mask & sl->flag)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800643 putchar('+');
Junio C Hamano46dc9412006-02-02 15:17:42 -0800644 else
645 putchar(' ');
646 p_mask <<= 1;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800647 }
Junio C Hamano39280972008-08-27 19:48:01 -0700648 show_line_to_eol(sl->bol, sl->len, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800649 }
650 }
651}
652
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800653static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
654 int i, int j)
655{
656 /* We have already examined parent j and we know parent i
657 * and parent j are the same, so reuse the combined result
658 * of parent j for parent i.
659 */
660 unsigned long lno, imask, jmask;
661 imask = (1UL<<i);
662 jmask = (1UL<<j);
663
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700664 for (lno = 0; lno <= cnt; lno++) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800665 struct lline *ll = sline->lost_head;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800666 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800667 while (ll) {
668 if (ll->parent_map & jmask)
669 ll->parent_map |= imask;
670 ll = ll->next;
671 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800672 if (sline->flag & jmask)
673 sline->flag |= imask;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800674 sline++;
675 }
Junio C Hamano44627312006-02-06 18:54:08 -0800676 /* the overall size of the file (sline[cnt]) */
677 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800678}
679
Junio C Hamano462a15b2007-12-26 16:51:19 -0800680static void dump_quoted_path(const char *head,
681 const char *prefix,
682 const char *path,
Junio C Hamano567a03d2006-08-10 00:30:33 -0700683 const char *c_meta, const char *c_reset)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800684{
Junio C Hamano462a15b2007-12-26 16:51:19 -0800685 static struct strbuf buf = STRBUF_INIT;
686
687 strbuf_reset(&buf);
688 strbuf_addstr(&buf, c_meta);
689 strbuf_addstr(&buf, head);
Junio C Hamanod5625092007-12-26 17:13:36 -0800690 quote_two_c_style(&buf, prefix, path, 0);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800691 strbuf_addstr(&buf, c_reset);
692 puts(buf.buf);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700693}
694
Jeff King7c978a02011-05-23 16:16:41 -0400695static void show_combined_header(struct combine_diff_path *elem,
696 int num_parent,
697 int dense,
698 struct rev_info *rev,
Jeff King4d5f3472011-05-23 16:27:34 -0400699 int mode_differs,
700 int show_file_header)
Jeff King7c978a02011-05-23 16:16:41 -0400701{
702 struct diff_options *opt = &rev->diffopt;
703 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
704 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
705 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
Jeff Kingf1c96262011-08-17 22:03:12 -0700706 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
707 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
Jeff King7c978a02011-05-23 16:16:41 -0400708 const char *abb;
709 int added = 0;
710 int deleted = 0;
711 int i;
712
713 if (rev->loginfo && !rev->no_commit_id)
714 show_log(rev);
715
716 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
717 "", elem->path, c_meta, c_reset);
718 printf("%sindex ", c_meta);
719 for (i = 0; i < num_parent; i++) {
720 abb = find_unique_abbrev(elem->parent[i].sha1,
721 abbrev);
722 printf("%s%s", i ? "," : "", abb);
723 }
724 abb = find_unique_abbrev(elem->sha1, abbrev);
725 printf("..%s%s\n", abb, c_reset);
726
727 if (mode_differs) {
728 deleted = !elem->mode;
729
730 /* We say it was added if nobody had it */
731 added = !deleted;
732 for (i = 0; added && i < num_parent; i++)
733 if (elem->parent[i].status !=
734 DIFF_STATUS_ADDED)
735 added = 0;
736 if (added)
737 printf("%snew file mode %06o",
738 c_meta, elem->mode);
739 else {
740 if (deleted)
741 printf("%sdeleted file ", c_meta);
742 printf("mode ");
743 for (i = 0; i < num_parent; i++) {
744 printf("%s%06o", i ? "," : "",
745 elem->parent[i].mode);
746 }
747 if (elem->mode)
748 printf("..%06o", elem->mode);
749 }
750 printf("%s\n", c_reset);
751 }
752
Jeff King4d5f3472011-05-23 16:27:34 -0400753 if (!show_file_header)
754 return;
755
Jeff King7c978a02011-05-23 16:16:41 -0400756 if (added)
757 dump_quoted_path("--- ", "", "/dev/null",
758 c_meta, c_reset);
759 else
760 dump_quoted_path("--- ", a_prefix, elem->path,
761 c_meta, c_reset);
762 if (deleted)
763 dump_quoted_path("+++ ", "", "/dev/null",
764 c_meta, c_reset);
765 else
766 dump_quoted_path("+++ ", b_prefix, elem->path,
767 c_meta, c_reset);
768}
769
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700770static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
Junio C Hamano99694542011-08-04 11:39:10 -0700771 int dense, int working_tree_file,
772 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800773{
Linus Torvalds91539832006-04-17 11:59:32 -0700774 struct diff_options *opt = &rev->diffopt;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700775 unsigned long result_size, cnt, lno;
Thomas Rast21798702010-04-15 14:59:37 +0200776 int result_deleted = 0;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -0500777 char *result, *cp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800778 struct sline *sline; /* survived lines */
Junio C Hamano2454c962006-02-06 12:53:07 -0800779 int mode_differs = 0;
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700780 int i, show_hunks;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700781 mmfile_t result_file;
Jeff King4d5f3472011-05-23 16:27:34 -0400782 struct userdiff_driver *userdiff;
Jeff King0508fe52011-05-23 16:31:05 -0400783 struct userdiff_driver *textconv = NULL;
Jeff King4d5f3472011-05-23 16:27:34 -0400784 int is_binary;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800785
Linus Torvaldsee1e5412006-05-13 13:23:48 -0700786 context = opt->context;
Jeff King4d5f3472011-05-23 16:27:34 -0400787 userdiff = userdiff_find_by_path(elem->path);
788 if (!userdiff)
789 userdiff = userdiff_find_by_name("default");
Jeff King0508fe52011-05-23 16:31:05 -0400790 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV))
791 textconv = userdiff_get_textconv(userdiff);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700792
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800793 /* Read the result of merge first */
Junio C Hamanof23fc772006-04-03 18:53:15 -0700794 if (!working_tree_file)
Jeff King0508fe52011-05-23 16:31:05 -0400795 result = grab_blob(elem->sha1, elem->mode, &result_size,
796 textconv, elem->path);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800797 else {
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800798 /* Used by diff-tree to read from the working tree */
Junio C Hamanoea726d02006-01-28 00:03:38 -0800799 struct stat st;
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800800 int fd = -1;
801
802 if (lstat(elem->path, &st) < 0)
803 goto deleted_file;
804
805 if (S_ISLNK(st.st_mode)) {
Junio C Hamano912342d2008-12-17 12:37:58 -0800806 struct strbuf buf = STRBUF_INIT;
807
808 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800809 error("readlink(%s): %s", elem->path,
810 strerror(errno));
811 return;
812 }
Junio C Hamano912342d2008-12-17 12:37:58 -0800813 result_size = buf.len;
814 result = strbuf_detach(&buf, NULL);
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800815 elem->mode = canon_mode(st.st_mode);
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700816 } else if (S_ISDIR(st.st_mode)) {
817 unsigned char sha1[20];
818 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
Jeff King0508fe52011-05-23 16:31:05 -0400819 result = grab_blob(elem->sha1, elem->mode,
820 &result_size, NULL, NULL);
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700821 else
Jeff King0508fe52011-05-23 16:31:05 -0400822 result = grab_blob(sha1, elem->mode,
823 &result_size, NULL, NULL);
824 } else if (textconv) {
825 struct diff_filespec *df = alloc_filespec(elem->path);
826 fill_filespec(df, null_sha1, st.st_mode);
827 result_size = fill_textconv(textconv, df, &result);
828 free_filespec(df);
Kjetil Barvik91fcbcb2009-02-09 21:54:52 +0100829 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500830 size_t len = xsize_t(st.st_size);
Heikki Orsilac697ad12008-05-03 16:27:26 +0300831 ssize_t done;
Johannes Sixta249a9b2007-03-03 20:38:00 +0100832 int is_file, i;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800833
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800834 elem->mode = canon_mode(st.st_mode);
Johannes Sixta249a9b2007-03-03 20:38:00 +0100835 /* if symlinks don't work, assume symlink if all parents
836 * are symlinks
837 */
838 is_file = has_symlinks;
839 for (i = 0; !is_file && i < num_parent; i++)
840 is_file = !S_ISLNK(elem->parent[i].mode);
841 if (!is_file)
842 elem->mode = canon_mode(S_IFLNK);
843
Junio C Hamanof23fc772006-04-03 18:53:15 -0700844 result_size = len;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800845 result = xmalloc(len + 1);
Heikki Orsilac697ad12008-05-03 16:27:26 +0300846
847 done = read_in_full(fd, result, len);
848 if (done < 0)
Thomas Rast0721c312009-06-27 17:58:47 +0200849 die_errno("read error '%s'", elem->path);
Heikki Orsilac697ad12008-05-03 16:27:26 +0300850 else if (done < len)
851 die("early EOF '%s'", elem->path);
852
Junio C Hamanoea726d02006-01-28 00:03:38 -0800853 result[len] = 0;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +0400854
855 /* If not a fake symlink, apply filters, e.g. autocrlf */
856 if (is_file) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500857 struct strbuf buf = STRBUF_INIT;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +0400858
Alexander Gavrilov5e568f92008-08-23 23:21:21 +0400859 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
860 free(result);
861 result = strbuf_detach(&buf, &len);
862 result_size = len;
863 }
864 }
Junio C Hamanoea726d02006-01-28 00:03:38 -0800865 }
866 else {
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800867 deleted_file:
Thomas Rast21798702010-04-15 14:59:37 +0200868 result_deleted = 1;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700869 result_size = 0;
Junio C Hamano713a11f2006-02-13 23:07:04 -0800870 elem->mode = 0;
Peter Eriksen28f75812006-07-25 09:30:18 +0200871 result = xcalloc(1, 1);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800872 }
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800873
Junio C Hamanoea726d02006-01-28 00:03:38 -0800874 if (0 <= fd)
875 close(fd);
876 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800877
Jeff Kingc95b99b2011-05-23 16:16:59 -0400878 for (i = 0; i < num_parent; i++) {
879 if (elem->parent[i].mode != elem->mode) {
880 mode_differs = 1;
881 break;
882 }
883 }
884
Jeff King0508fe52011-05-23 16:31:05 -0400885 if (textconv)
886 is_binary = 0;
887 else if (userdiff->binary != -1)
Jeff King4d5f3472011-05-23 16:27:34 -0400888 is_binary = userdiff->binary;
889 else {
890 is_binary = buffer_is_binary(result, result_size);
891 for (i = 0; !is_binary && i < num_parent; i++) {
892 char *buf;
893 unsigned long size;
894 buf = grab_blob(elem->parent[i].sha1,
895 elem->parent[i].mode,
Jeff King0508fe52011-05-23 16:31:05 -0400896 &size, NULL, NULL);
Jeff King4d5f3472011-05-23 16:27:34 -0400897 if (buffer_is_binary(buf, size))
898 is_binary = 1;
899 free(buf);
900 }
901 }
902 if (is_binary) {
903 show_combined_header(elem, num_parent, dense, rev,
904 mode_differs, 0);
905 printf("Binary files differ\n");
906 free(result);
907 return;
908 }
909
Junio C Hamano2386c292006-06-28 01:38:19 -0700910 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800911 if (*cp == '\n')
912 cnt++;
913 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700914 if (result_size && result[result_size-1] != '\n')
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800915 cnt++; /* incomplete line */
916
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700917 sline = xcalloc(cnt+2, sizeof(*sline));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800918 sline[0].bol = result;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700919 for (lno = 0; lno <= cnt + 1; lno++) {
Junio C Hamano44627312006-02-06 18:54:08 -0800920 sline[lno].lost_tail = &sline[lno].lost_head;
921 sline[lno].flag = 0;
922 }
Junio C Hamano2386c292006-06-28 01:38:19 -0700923 for (lno = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800924 if (*cp == '\n') {
925 sline[lno].len = cp - sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800926 lno++;
927 if (lno < cnt)
928 sline[lno].bol = cp + 1;
929 }
930 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700931 if (result_size && result[result_size-1] != '\n')
932 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
933
934 result_file.ptr = result;
935 result_file.size = result_size;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800936
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700937 /* Even p_lno[cnt+1] is valid -- that is for the end line number
938 * for deletion hunk at the end.
939 */
940 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
941 for (lno = 0; lno <= cnt; lno++)
Junio C Hamanof16706c2006-01-30 22:33:15 -0800942 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
943
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800944 for (i = 0; i < num_parent; i++) {
945 int j;
946 for (j = 0; j < i; j++) {
David Rientjesa89fccd2006-08-17 11:54:57 -0700947 if (!hashcmp(elem->parent[i].sha1,
948 elem->parent[j].sha1)) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800949 reuse_combine_diff(sline, cnt, i, j);
950 break;
951 }
952 }
953 if (i <= j)
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700954 combine_diff(elem->parent[i].sha1,
955 elem->parent[i].mode,
956 &result_file, sline,
Jeff King0508fe52011-05-23 16:31:05 -0400957 cnt, i, num_parent, result_deleted,
958 textconv, elem->path);
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800959 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800960
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800961 show_hunks = make_hunks(sline, cnt, num_parent, dense);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800962
Junio C Hamano713a11f2006-02-13 23:07:04 -0800963 if (show_hunks || mode_differs || working_tree_file) {
Jeff King7c978a02011-05-23 16:16:41 -0400964 show_combined_header(elem, num_parent, dense, rev,
Jeff King4d5f3472011-05-23 16:27:34 -0400965 mode_differs, 1);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800966 dump_sline(sline, cnt, num_parent,
Jeff Kingf1c96262011-08-17 22:03:12 -0700967 opt->use_color, result_deleted);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800968 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800969 free(result);
970
Junio C Hamano2386c292006-06-28 01:38:19 -0700971 for (lno = 0; lno < cnt; lno++) {
972 if (sline[lno].lost_head) {
973 struct lline *ll = sline[lno].lost_head;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800974 while (ll) {
975 struct lline *tmp = ll;
976 ll = ll->next;
977 free(tmp);
978 }
979 }
980 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800981 free(sline[0].p_lno);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800982 free(sline);
983}
984
Linus Torvaldsee638022006-02-09 10:30:28 -0800985#define COLONS "::::::::::::::::::::::::::::::::"
986
Linus Torvalds91539832006-04-17 11:59:32 -0700987static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
Linus Torvaldsee638022006-02-09 10:30:28 -0800988{
Linus Torvalds91539832006-04-17 11:59:32 -0700989 struct diff_options *opt = &rev->diffopt;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -0500990 int i, offset;
Linus Torvaldsee638022006-02-09 10:30:28 -0800991 const char *prefix;
992 int line_termination, inter_name_termination;
993
994 line_termination = opt->line_termination;
995 inter_name_termination = '\t';
996 if (!line_termination)
997 inter_name_termination = 0;
998
Junio C Hamano44152782006-10-26 02:05:59 -0700999 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -07001000 show_log(rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001001
Timo Hirvonenc6744342006-06-24 20:21:53 +03001002 if (opt->output_format & DIFF_FORMAT_RAW) {
Junio C Hamano0a798072006-02-09 15:23:06 -08001003 offset = strlen(COLONS) - num_parent;
1004 if (offset < 0)
1005 offset = 0;
1006 prefix = COLONS + offset;
Linus Torvaldsee638022006-02-09 10:30:28 -08001007
Junio C Hamano0a798072006-02-09 15:23:06 -08001008 /* Show the modes */
1009 for (i = 0; i < num_parent; i++) {
1010 printf("%s%06o", prefix, p->parent[i].mode);
1011 prefix = " ";
1012 }
1013 printf("%s%06o", prefix, p->mode);
1014
1015 /* Show sha1's */
1016 for (i = 0; i < num_parent; i++)
1017 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
1018 opt->abbrev));
1019 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
1020 }
1021
Timo Hirvonenc6744342006-06-24 20:21:53 +03001022 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamanod416df82006-02-10 02:30:52 -08001023 for (i = 0; i < num_parent; i++)
1024 putchar(p->parent[i].status);
1025 putchar(inter_name_termination);
1026 }
Junio C Hamano0a798072006-02-09 15:23:06 -08001027
Pierre Habouzit663af342007-09-20 00:42:15 +02001028 write_name_quoted(p->path, stdout, line_termination);
Junio C Hamano0a798072006-02-09 15:23:06 -08001029}
1030
Junio C Hamano99694542011-08-04 11:39:10 -07001031/*
1032 * The result (p->elem) is from the working tree and their
1033 * parents are typically from multiple stages during a merge
1034 * (i.e. diff-files) or the state in HEAD and in the index
1035 * (i.e. diff-index).
1036 */
Linus Torvalds91539832006-04-17 11:59:32 -07001037void show_combined_diff(struct combine_diff_path *p,
Junio C Hamano0a798072006-02-09 15:23:06 -08001038 int num_parent,
1039 int dense,
Linus Torvalds91539832006-04-17 11:59:32 -07001040 struct rev_info *rev)
Junio C Hamano0a798072006-02-09 15:23:06 -08001041{
Linus Torvalds91539832006-04-17 11:59:32 -07001042 struct diff_options *opt = &rev->diffopt;
Junio C Hamano0a798072006-02-09 15:23:06 -08001043 if (!p->len)
Linus Torvalds91539832006-04-17 11:59:32 -07001044 return;
Timo Hirvonenc6744342006-06-24 20:21:53 +03001045 if (opt->output_format & (DIFF_FORMAT_RAW |
1046 DIFF_FORMAT_NAME |
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001047 DIFF_FORMAT_NAME_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -07001048 show_raw_diff(p, num_parent, rev);
Junio C Hamano89b0c4b2006-08-13 19:19:34 -07001049 else if (opt->output_format & DIFF_FORMAT_PATCH)
Junio C Hamano99694542011-08-04 11:39:10 -07001050 show_patch_diff(p, num_parent, dense, 1, rev);
Linus Torvaldsee638022006-02-09 10:30:28 -08001051}
1052
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001053static void free_combined_pair(struct diff_filepair *pair)
1054{
1055 free(pair->two);
1056 free(pair);
1057}
1058
1059/*
1060 * A combine_diff_path expresses N parents on the LHS against 1 merge
1061 * result. Synthesize a diff_filepair that has N entries on the "one"
1062 * side and 1 entry on the "two" side.
1063 *
1064 * In the future, we might want to add more data to combine_diff_path
1065 * so that we can fill fields we are ignoring (most notably, size) here,
1066 * but currently nobody uses it, so this should suffice for now.
1067 */
1068static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1069 int num_parent)
1070{
1071 int i;
1072 struct diff_filepair *pair;
1073 struct diff_filespec *pool;
1074
1075 pair = xmalloc(sizeof(*pair));
1076 pool = xcalloc(num_parent + 1, sizeof(struct diff_filespec));
1077 pair->one = pool + 1;
1078 pair->two = pool;
1079
1080 for (i = 0; i < num_parent; i++) {
1081 pair->one[i].path = p->path;
1082 pair->one[i].mode = p->parent[i].mode;
1083 hashcpy(pair->one[i].sha1, p->parent[i].sha1);
1084 pair->one[i].sha1_valid = !is_null_sha1(p->parent[i].sha1);
1085 pair->one[i].has_more_entries = 1;
1086 }
1087 pair->one[num_parent - 1].has_more_entries = 0;
1088
1089 pair->two->path = p->path;
1090 pair->two->mode = p->mode;
1091 hashcpy(pair->two->sha1, p->sha1);
1092 pair->two->sha1_valid = !is_null_sha1(p->sha1);
1093 return pair;
1094}
1095
1096static void handle_combined_callback(struct diff_options *opt,
1097 struct combine_diff_path *paths,
1098 int num_parent,
1099 int num_paths)
1100{
1101 struct combine_diff_path *p;
1102 struct diff_queue_struct q;
1103 int i;
1104
1105 q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
1106 q.alloc = num_paths;
1107 q.nr = num_paths;
1108 for (i = 0, p = paths; p; p = p->next) {
1109 if (!p->len)
1110 continue;
1111 q.queue[i++] = combined_pair(p, num_parent);
1112 }
1113 opt->format_callback(&q, opt, opt->format_callback_data);
1114 for (i = 0; i < num_paths; i++)
1115 free_combined_pair(q.queue[i]);
1116 free(q.queue);
1117}
1118
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001119void diff_tree_combined(const unsigned char *sha1,
René Scharfe0041f092011-12-17 11:15:48 +01001120 const struct sha1_array *parents,
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001121 int dense,
1122 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001123{
Linus Torvalds91539832006-04-17 11:59:32 -07001124 struct diff_options *opt = &rev->diffopt;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001125 struct diff_options diffopts;
Junio C Hamanoea726d02006-01-28 00:03:38 -08001126 struct combine_diff_path *p, *paths = NULL;
René Scharfe0041f092011-12-17 11:15:48 +01001127 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001128
Junio C Hamano5b236832006-02-09 14:35:19 -08001129 diffopts = *opt;
Junio C Hamano3969cf72006-06-27 15:08:19 -07001130 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01001131 DIFF_OPT_SET(&diffopts, RECURSIVE);
1132 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001133
Junio C Hamano44152782006-10-26 02:05:59 -07001134 show_log_first = !!rev->loginfo && !rev->no_commit_id;
Junio C Hamano3969cf72006-06-27 15:08:19 -07001135 needsep = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001136 /* find set of paths that everybody touches */
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001137 for (i = 0; i < num_parent; i++) {
Junio C Hamano965f8032006-04-17 22:53:03 -07001138 /* show stat against the first parent even
1139 * when doing combined diff.
1140 */
Junio C Hamano74e2abe2006-10-12 03:01:00 -07001141 int stat_opt = (opt->output_format &
1142 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1143 if (i == 0 && stat_opt)
1144 diffopts.output_format = stat_opt;
Junio C Hamano965f8032006-04-17 22:53:03 -07001145 else
1146 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
René Scharfe0041f092011-12-17 11:15:48 +01001147 diff_tree_sha1(parents->sha1[i], sha1, "", &diffopts);
Junio C Hamano5b236832006-02-09 14:35:19 -08001148 diffcore_std(&diffopts);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001149 paths = intersect_paths(paths, i, num_parent);
Linus Torvaldseab144a2006-04-17 16:59:42 -07001150
Junio C Hamano3969cf72006-06-27 15:08:19 -07001151 if (show_log_first && i == 0) {
Adam Simpkins02865652008-04-29 01:32:59 -07001152 show_log(rev);
Junio C Hamano3969cf72006-06-27 15:08:19 -07001153 if (rev->verbose_header && opt->output_format)
1154 putchar(opt->line_termination);
1155 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001156 diff_flush(&diffopts);
1157 }
1158
1159 /* find out surviving paths */
1160 for (num_paths = 0, p = paths; p; p = p->next) {
1161 if (p->len)
1162 num_paths++;
1163 }
Junio C Hamanoe3c3a552006-02-05 22:25:00 -08001164 if (num_paths) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001165 if (opt->output_format & (DIFF_FORMAT_RAW |
1166 DIFF_FORMAT_NAME |
1167 DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001168 for (p = paths; p; p = p->next) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001169 if (p->len)
1170 show_raw_diff(p, num_parent, rev);
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001171 }
Junio C Hamano3969cf72006-06-27 15:08:19 -07001172 needsep = 1;
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001173 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07001174 else if (opt->output_format &
1175 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
Junio C Hamano3969cf72006-06-27 15:08:19 -07001176 needsep = 1;
Junio C Hamano25e5e2b2011-08-19 23:32:51 -07001177 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1178 handle_combined_callback(opt, paths, num_parent, num_paths);
1179
Timo Hirvonenc6744342006-06-24 20:21:53 +03001180 if (opt->output_format & DIFF_FORMAT_PATCH) {
Junio C Hamano3969cf72006-06-27 15:08:19 -07001181 if (needsep)
1182 putchar(opt->line_termination);
Timo Hirvonenc6744342006-06-24 20:21:53 +03001183 for (p = paths; p; p = p->next) {
1184 if (p->len)
1185 show_patch_diff(p, num_parent, dense,
Junio C Hamano99694542011-08-04 11:39:10 -07001186 0, rev);
Timo Hirvonenc6744342006-06-24 20:21:53 +03001187 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001188 }
1189 }
1190
1191 /* Clean things up */
1192 while (paths) {
Junio C Hamanoea726d02006-01-28 00:03:38 -08001193 struct combine_diff_path *tmp = paths;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001194 paths = paths->next;
1195 free(tmp);
1196 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001197}
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001198
René Scharfe82889292011-12-17 11:20:07 +01001199void diff_tree_combined_merge(const struct commit *commit, int dense,
1200 struct rev_info *rev)
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001201{
René Scharfe0041f092011-12-17 11:15:48 +01001202 struct commit_list *parent = commit->parents;
1203 struct sha1_array parents = SHA1_ARRAY_INIT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001204
René Scharfe0041f092011-12-17 11:15:48 +01001205 while (parent) {
1206 sha1_array_append(&parents, parent->item->object.sha1);
1207 parent = parent->next;
1208 }
René Scharfe82889292011-12-17 11:20:07 +01001209 diff_tree_combined(commit->object.sha1, &parents, dense, rev);
René Scharfe0041f092011-12-17 11:15:48 +01001210 sha1_array_clear(&parents);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001211}