blob: 9f80a1c5e3a461afd11966625589684d61187911 [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 Hamanoaf3feef2006-01-24 01:22:04 -08009
Junio C Hamanoea726d02006-01-28 00:03:38 -080010static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080011{
12 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamanoea726d02006-01-28 00:03:38 -080013 struct combine_diff_path *p;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080014 int i;
15
16 if (!n) {
Junio C Hamanoea726d02006-01-28 00:03:38 -080017 struct combine_diff_path *list = NULL, **tail = &list;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080018 for (i = 0; i < q->nr; i++) {
19 int len;
20 const char *path;
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070021 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080022 continue;
23 path = q->queue[i]->two->path;
24 len = strlen(path);
Junio C Hamano2454c962006-02-06 12:53:07 -080025 p = xmalloc(combine_diff_path_size(num_parent, len));
26 p->path = (char*) &(p->parent[num_parent]);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080027 memcpy(p->path, path, len);
28 p->path[len] = 0;
29 p->len = len;
30 p->next = NULL;
Junio C Hamano2454c962006-02-06 12:53:07 -080031 memset(p->parent, 0,
32 sizeof(p->parent[0]) * num_parent);
33
Shawn Pearcee7024962006-08-23 02:49:00 -040034 hashcpy(p->sha1, q->queue[i]->two->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080035 p->mode = q->queue[i]->two->mode;
Shawn Pearcee7024962006-08-23 02:49:00 -040036 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080037 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080038 p->parent[n].status = q->queue[i]->status;
Junio C Hamano5290a0f2006-01-25 03:34:10 -080039 *tail = p;
40 tail = &p->next;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080041 }
42 return list;
43 }
44
45 for (p = curr; p; p = p->next) {
46 int found = 0;
47 if (!p->len)
48 continue;
49 for (i = 0; i < q->nr; i++) {
50 const char *path;
51 int len;
52
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070053 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080054 continue;
55 path = q->queue[i]->two->path;
56 len = strlen(path);
57 if (len == p->len && !memcmp(path, p->path, len)) {
58 found = 1;
Shawn Pearcee7024962006-08-23 02:49:00 -040059 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080060 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080061 p->parent[n].status = q->queue[i]->status;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080062 break;
63 }
64 }
65 if (!found)
66 p->len = 0;
67 }
68 return curr;
69}
70
Junio C Hamanob469d8b2006-01-30 16:34:29 -080071/* Lines lost from parent */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080072struct lline {
73 struct lline *next;
74 int len;
75 unsigned long parent_map;
76 char line[FLEX_ARRAY];
77};
78
Junio C Hamanob469d8b2006-01-30 16:34:29 -080079/* Lines surviving in the merge result */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080080struct sline {
81 struct lline *lost_head, **lost_tail;
82 char *bol;
83 int len;
Junio C Hamano46dc9412006-02-02 15:17:42 -080084 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
85 * we did not change it).
Junio C Hamanob469d8b2006-01-30 16:34:29 -080086 * bit N is used for "interesting" lines, including context.
Junio C Hamanoc86fbe52008-06-18 23:59:41 -070087 * bit (N+1) is used for "do not show deletion before this".
Junio C Hamanob469d8b2006-01-30 16:34:29 -080088 */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080089 unsigned long flag;
Junio C Hamanof16706c2006-01-30 22:33:15 -080090 unsigned long *p_lno;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080091};
92
93static char *grab_blob(const unsigned char *sha1, unsigned long *size)
94{
95 char *blob;
Nicolas Pitre21666f12007-02-26 14:55:59 -050096 enum object_type type;
David Rientjes0bef57e2006-08-15 13:37:19 -070097 if (is_null_sha1(sha1)) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080098 /* deleted blob */
99 *size = 0;
100 return xcalloc(1, 1);
101 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500102 blob = read_sha1_file(sha1, &type, size);
103 if (type != OBJ_BLOB)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800104 die("object '%s' is not a blob!", sha1_to_hex(sha1));
105 return blob;
106}
107
Junio C Hamanof23fc772006-04-03 18:53:15 -0700108static void append_lost(struct sline *sline, int n, const char *line, int len)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800109{
110 struct lline *lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800111 unsigned long this_mask = (1UL<<n);
112 if (line[len-1] == '\n')
113 len--;
114
115 /* Check to see if we can squash things */
116 if (sline->lost_head) {
117 struct lline *last_one = NULL;
118 /* We cannot squash it with earlier one */
119 for (lline = sline->lost_head;
120 lline;
121 lline = lline->next)
122 if (lline->parent_map & this_mask)
123 last_one = lline;
124 lline = last_one ? last_one->next : sline->lost_head;
125 while (lline) {
126 if (lline->len == len &&
127 !memcmp(lline->line, line, len)) {
128 lline->parent_map |= this_mask;
129 return;
130 }
131 lline = lline->next;
132 }
133 }
134
135 lline = xmalloc(sizeof(*lline) + len + 1);
136 lline->len = len;
137 lline->next = NULL;
138 lline->parent_map = this_mask;
139 memcpy(lline->line, line, len);
140 lline->line[len] = 0;
Junio C Hamano5290a0f2006-01-25 03:34:10 -0800141 *sline->lost_tail = lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800142 sline->lost_tail = &lline->next;
143}
144
Junio C Hamanof23fc772006-04-03 18:53:15 -0700145struct combine_diff_state {
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700146 struct xdiff_emit_state xm;
147
Junio C Hamanoa0fd3142006-04-06 22:29:55 -0700148 unsigned int lno;
149 int ob, on, nb, nn;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700150 unsigned long nmask;
151 int num_parent;
152 int n;
153 struct sline *sline;
154 struct sline *lost_bucket;
155};
156
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700157static void consume_line(void *state_, char *line, unsigned long len)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700158{
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700159 struct combine_diff_state *state = state_;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700160 if (5 < len && !memcmp("@@ -", line, 4)) {
161 if (parse_hunk_header(line, len,
162 &state->ob, &state->on,
163 &state->nb, &state->nn))
164 return;
165 state->lno = state->nb;
166 if (!state->nb)
167 /* @@ -1,2 +0,0 @@ to remove the
168 * first two lines...
169 */
170 state->nb = 1;
171 if (state->nn == 0)
172 /* @@ -X,Y +N,0 @@ removed Y lines
173 * that would have come *after* line N
174 * in the result. Our lost buckets hang
175 * to the line after the removed lines,
176 */
177 state->lost_bucket = &state->sline[state->nb];
178 else
179 state->lost_bucket = &state->sline[state->nb-1];
180 if (!state->sline[state->nb-1].p_lno)
181 state->sline[state->nb-1].p_lno =
182 xcalloc(state->num_parent,
183 sizeof(unsigned long));
184 state->sline[state->nb-1].p_lno[state->n] = state->ob;
185 return;
186 }
187 if (!state->lost_bucket)
188 return; /* not in any hunk yet */
189 switch (line[0]) {
190 case '-':
191 append_lost(state->lost_bucket, state->n, line+1, len-1);
192 break;
193 case '+':
194 state->sline[state->lno-1].flag |= state->nmask;
195 state->lno++;
196 break;
197 }
198}
199
Junio C Hamanof23fc772006-04-03 18:53:15 -0700200static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
Junio C Hamano2386c292006-06-28 01:38:19 -0700201 struct sline *sline, unsigned int cnt, int n,
202 int num_parent)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800203{
Junio C Hamanof23fc772006-04-03 18:53:15 -0700204 unsigned int p_lno, lno;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800205 unsigned long nmask = (1UL << n);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700206 xpparam_t xpp;
207 xdemitconf_t xecfg;
208 mmfile_t parent_file;
209 xdemitcb_t ecb;
210 struct combine_diff_state state;
211 unsigned long sz;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800212
Junio C Hamano44627312006-02-06 18:54:08 -0800213 if (!cnt)
214 return; /* result deleted */
215
Junio C Hamanof23fc772006-04-03 18:53:15 -0700216 parent_file.ptr = grab_blob(parent, &sz);
217 parent_file.size = sz;
218 xpp.flags = XDF_NEED_MINIMAL;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100219 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700220 ecb.outf = xdiff_outf;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700221 ecb.priv = &state;
222 memset(&state, 0, sizeof(state));
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700223 state.xm.consume = consume_line;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700224 state.nmask = nmask;
225 state.sline = sline;
226 state.lno = 1;
227 state.num_parent = num_parent;
228 state.n = n;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800229
Junio C Hamanoc279d7e2007-12-13 13:25:07 -0800230 xdi_diff(&parent_file, result_file, &xpp, &xecfg, &ecb);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700231 free(parent_file.ptr);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800232
233 /* Assign line numbers for this parent.
234 *
235 * sline[lno].p_lno[n] records the first line number
236 * (counting from 1) for parent N if the final hunk display
237 * started by showing sline[lno] (possibly showing the lost
238 * lines attached to it first).
239 */
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700240 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
Junio C Hamanof16706c2006-01-30 22:33:15 -0800241 struct lline *ll;
242 sline[lno].p_lno[n] = p_lno;
243
244 /* How many lines would this sline advance the p_lno? */
245 ll = sline[lno].lost_head;
246 while (ll) {
247 if (ll->parent_map & nmask)
248 p_lno++; /* '-' means parent had it */
249 ll = ll->next;
250 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700251 if (lno < cnt && !(sline[lno].flag & nmask))
Junio C Hamanof16706c2006-01-30 22:33:15 -0800252 p_lno++; /* no '+' means parent had it */
253 }
254 sline[lno].p_lno[n] = p_lno; /* trailer */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800255}
256
257static unsigned long context = 3;
258static char combine_marker = '@';
259
260static int interesting(struct sline *sline, unsigned long all_mask)
261{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800262 /* If some parents lost lines here, or if we have added to
263 * some parent, it is interesting.
264 */
265 return ((sline->flag & all_mask) || sline->lost_head);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800266}
267
Junio C Hamano3ec19092006-01-26 03:53:01 -0800268static unsigned long adjust_hunk_tail(struct sline *sline,
269 unsigned long all_mask,
270 unsigned long hunk_begin,
271 unsigned long i)
272{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800273 /* i points at the first uninteresting line. If the last line
274 * of the hunk was interesting only because it has some
275 * deletion, then it is not all that interesting for the
276 * purpose of giving trailing context lines. This is because
277 * we output '-' line and then unmodified sline[i-1] itself in
278 * that case which gives us one extra context line.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800279 */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800280 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800281 i--;
282 return i;
283}
284
Junio C Hamano46dc9412006-02-02 15:17:42 -0800285static unsigned long find_next(struct sline *sline,
286 unsigned long mark,
287 unsigned long i,
288 unsigned long cnt,
Junio C Hamano2386c292006-06-28 01:38:19 -0700289 int look_for_uninteresting)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800290{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800291 /* We have examined up to i-1 and are about to look at i.
292 * Find next interesting or uninteresting line. Here,
293 * "interesting" does not mean interesting(), but marked by
294 * the give_context() function below (i.e. it includes context
295 * lines that are not interesting to interesting() function
296 * that are surrounded by interesting() ones.
297 */
Junio C Hamano74065952006-04-11 14:31:31 -0700298 while (i <= cnt)
Junio C Hamano2386c292006-06-28 01:38:19 -0700299 if (look_for_uninteresting
Junio C Hamano46dc9412006-02-02 15:17:42 -0800300 ? !(sline[i].flag & mark)
301 : (sline[i].flag & mark))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800302 return i;
303 else
304 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700305 return i;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800306}
307
308static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
309{
310 unsigned long all_mask = (1UL<<num_parent) - 1;
311 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700312 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800313 unsigned long i;
314
Junio C Hamano46dc9412006-02-02 15:17:42 -0800315 /* Two groups of interesting lines may have a short gap of
Pavel Roskin82e5a822006-07-10 01:50:18 -0400316 * uninteresting lines. Connect such groups to give them a
Junio C Hamano46dc9412006-02-02 15:17:42 -0800317 * bit of context.
318 *
319 * We first start from what the interesting() function says,
320 * and mark them with "mark", and paint context lines with the
321 * mark. So interesting() would still say false for such context
322 * lines but they are treated as "interesting" in the end.
323 */
324 i = find_next(sline, mark, 0, cnt, 0);
Junio C Hamano74065952006-04-11 14:31:31 -0700325 if (cnt < i)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800326 return 0;
327
Junio C Hamano74065952006-04-11 14:31:31 -0700328 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800329 unsigned long j = (context < i) ? (i - context) : 0;
330 unsigned long k;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800331
332 /* Paint a few lines before the first interesting line. */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800333 while (j < i)
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700334 sline[j++].flag |= mark | no_pre_delete;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800335
336 again:
Junio C Hamano46dc9412006-02-02 15:17:42 -0800337 /* we know up to i is to be included. where does the
338 * next uninteresting one start?
339 */
340 j = find_next(sline, mark, i, cnt, 1);
Junio C Hamano74065952006-04-11 14:31:31 -0700341 if (cnt < j)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800342 break; /* the rest are all interesting */
343
344 /* lookahead context lines */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800345 k = find_next(sline, mark, j, cnt, 0);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800346 j = adjust_hunk_tail(sline, all_mask, i, j);
347
348 if (k < j + context) {
349 /* k is interesting and [j,k) are not, but
350 * paint them interesting because the gap is small.
351 */
352 while (j < k)
353 sline[j++].flag |= mark;
354 i = k;
355 goto again;
356 }
357
358 /* j is the first uninteresting line and there is
Junio C Hamano46dc9412006-02-02 15:17:42 -0800359 * no overlap beyond it within context lines. Paint
360 * the trailing edge a bit.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800361 */
362 i = k;
Junio C Hamano74065952006-04-11 14:31:31 -0700363 k = (j + context < cnt+1) ? j + context : cnt+1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800364 while (j < k)
365 sline[j++].flag |= mark;
366 }
367 return 1;
368}
369
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800370static int make_hunks(struct sline *sline, unsigned long cnt,
Junio C Hamanod8f47902006-01-24 01:22:04 -0800371 int num_parent, int dense)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800372{
373 unsigned long all_mask = (1UL<<num_parent) - 1;
374 unsigned long mark = (1UL<<num_parent);
375 unsigned long i;
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800376 int has_interesting = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800377
Junio C Hamano74065952006-04-11 14:31:31 -0700378 for (i = 0; i <= cnt; i++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800379 if (interesting(&sline[i], all_mask))
380 sline[i].flag |= mark;
381 else
382 sline[i].flag &= ~mark;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800383 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800384 if (!dense)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800385 return give_context(sline, cnt, num_parent);
Junio C Hamanod8f47902006-01-24 01:22:04 -0800386
Junio C Hamano263eee22006-01-25 13:11:38 -0800387 /* Look at each hunk, and if we have changes from only one
388 * parent, or the changes are the same from all but one
389 * parent, mark that uninteresting.
Junio C Hamanod8f47902006-01-24 01:22:04 -0800390 */
391 i = 0;
Junio C Hamano74065952006-04-11 14:31:31 -0700392 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800393 unsigned long j, hunk_begin, hunk_end;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800394 unsigned long same_diff;
Junio C Hamano74065952006-04-11 14:31:31 -0700395 while (i <= cnt && !(sline[i].flag & mark))
Junio C Hamanod8f47902006-01-24 01:22:04 -0800396 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700397 if (cnt < i)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800398 break; /* No more interesting hunks */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800399 hunk_begin = i;
Junio C Hamano74065952006-04-11 14:31:31 -0700400 for (j = i + 1; j <= cnt; j++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800401 if (!(sline[j].flag & mark)) {
402 /* Look beyond the end to see if there
403 * is an interesting line after this
404 * hunk within context span.
405 */
406 unsigned long la; /* lookahead */
407 int contin = 0;
408 la = adjust_hunk_tail(sline, all_mask,
409 hunk_begin, j);
Junio C Hamano74065952006-04-11 14:31:31 -0700410 la = (la + context < cnt + 1) ?
411 (la + context) : cnt + 1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800412 while (j <= --la) {
413 if (sline[la].flag & mark) {
414 contin = 1;
415 break;
416 }
417 }
418 if (!contin)
419 break;
420 j = la;
421 }
422 }
423 hunk_end = j;
424
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800425 /* [i..hunk_end) are interesting. Now is it really
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800426 * interesting? We check if there are only two versions
427 * and the result matches one of them. That is, we look
428 * at:
429 * (+) line, which records lines added to which parents;
430 * this line appears in the result.
431 * (-) line, which records from what parents the line
432 * was removed; this line does not appear in the result.
433 * then check the set of parents the result has difference
434 * from, from all lines. If there are lines that has
435 * different set of parents that the result has differences
436 * from, that means we have more than two versions.
437 *
438 * Even when we have only two versions, if the result does
439 * not match any of the parents, the it should be considered
440 * interesting. In such a case, we would have all '+' line.
441 * After passing the above "two versions" test, that would
442 * appear as "the same set of parents" to be "all parents".
Junio C Hamanod8f47902006-01-24 01:22:04 -0800443 */
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800444 same_diff = 0;
445 has_interesting = 0;
446 for (j = i; j < hunk_end && !has_interesting; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800447 unsigned long this_diff = sline[j].flag & all_mask;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800448 struct lline *ll = sline[j].lost_head;
449 if (this_diff) {
450 /* This has some changes. Is it the
451 * same as others?
452 */
453 if (!same_diff)
454 same_diff = this_diff;
455 else if (same_diff != this_diff) {
456 has_interesting = 1;
457 break;
458 }
459 }
460 while (ll && !has_interesting) {
461 /* Lost this line from these parents;
462 * who are they? Are they the same?
463 */
464 this_diff = ll->parent_map;
465 if (!same_diff)
466 same_diff = this_diff;
467 else if (same_diff != this_diff) {
468 has_interesting = 1;
469 }
470 ll = ll->next;
471 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800472 }
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800473
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800474 if (!has_interesting && same_diff != all_mask) {
Junio C Hamanod8f47902006-01-24 01:22:04 -0800475 /* This hunk is not that interesting after all */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800476 for (j = hunk_begin; j < hunk_end; j++)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800477 sline[j].flag &= ~mark;
478 }
479 i = hunk_end;
480 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800481
482 has_interesting = give_context(sline, cnt, num_parent);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800483 return has_interesting;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800484}
485
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800486static 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 -0800487{
488 l0 = sline[l0].p_lno[n];
489 l1 = sline[l1].p_lno[n];
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800490 printf(" -%lu,%lu", l0, l1-l0-null_context);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800491}
492
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700493static int hunk_comment_line(const char *bol)
494{
Junio C Hamano7a8ac592006-10-26 02:05:05 -0700495 int ch;
496
497 if (!bol)
498 return 0;
499 ch = *bol & 0xff;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700500 return (isalpha(ch) || ch == '_' || ch == '$');
501}
502
Junio C Hamano567a03d2006-08-10 00:30:33 -0700503static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
504 int use_color)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800505{
506 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700507 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800508 int i;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800509 unsigned long lno = 0;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700510 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
511 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
512 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
513 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
514 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800515
Junio C Hamano44627312006-02-06 18:54:08 -0800516 if (!cnt)
517 return; /* result deleted */
518
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800519 while (1) {
520 struct sline *sl = &sline[lno];
Junio C Hamano8bc75742006-04-12 13:23:50 -0700521 unsigned long hunk_end;
522 unsigned long rlines;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700523 const char *hunk_comment = NULL;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800524 unsigned long null_context = 0;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700525
526 while (lno <= cnt && !(sline[lno].flag & mark)) {
527 if (hunk_comment_line(sline[lno].bol))
528 hunk_comment = sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800529 lno++;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700530 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700531 if (cnt < lno)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800532 break;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700533 else {
Junio C Hamano74065952006-04-11 14:31:31 -0700534 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700535 if (!(sline[hunk_end].flag & mark))
536 break;
537 }
Junio C Hamano74065952006-04-11 14:31:31 -0700538 rlines = hunk_end - lno;
539 if (cnt < hunk_end)
540 rlines--; /* pointing at the last delete hunk */
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800541
542 if (!context) {
543 /*
544 * Even when running with --unified=0, all
545 * lines in the hunk needs to be processed in
546 * the loop below in order to show the
547 * deletion recorded in lost_head. However,
548 * we do not want to show the resulting line
549 * with all blank context markers in such a
550 * case. Compensate.
551 */
552 unsigned long j;
553 for (j = lno; j < hunk_end; j++)
554 if (!(sline[j].flag & (mark-1)))
555 null_context++;
556 rlines -= null_context;
557 }
558
Junio C Hamano567a03d2006-08-10 00:30:33 -0700559 fputs(c_frag, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800560 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800561 for (i = 0; i < num_parent; i++)
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800562 show_parent_lno(sline, lno, hunk_end, i, null_context);
Junio C Hamano74065952006-04-11 14:31:31 -0700563 printf(" +%lu,%lu ", lno+1, rlines);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800564 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700565
566 if (hunk_comment) {
567 int comment_end = 0;
568 for (i = 0; i < 40; i++) {
569 int ch = hunk_comment[i] & 0xff;
570 if (!ch || ch == '\n')
571 break;
572 if (!isspace(ch))
573 comment_end = i;
574 }
575 if (comment_end)
576 putchar(' ');
577 for (i = 0; i < comment_end; i++)
578 putchar(hunk_comment[i]);
579 }
580
Junio C Hamano567a03d2006-08-10 00:30:33 -0700581 printf("%s\n", c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800582 while (lno < hunk_end) {
583 struct lline *ll;
584 int j;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800585 unsigned long p_mask;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800586 sl = &sline[lno++];
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700587 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost_head;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800588 while (ll) {
Junio C Hamano567a03d2006-08-10 00:30:33 -0700589 fputs(c_old, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800590 for (j = 0; j < num_parent; j++) {
591 if (ll->parent_map & (1UL<<j))
592 putchar('-');
593 else
594 putchar(' ');
595 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700596 printf("%s%s\n", ll->line, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800597 ll = ll->next;
598 }
Junio C Hamano74065952006-04-11 14:31:31 -0700599 if (cnt < lno)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700600 break;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800601 p_mask = 1;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800602 if (!(sl->flag & (mark-1))) {
603 /*
604 * This sline was here to hang the
605 * lost lines in front of it.
606 */
607 if (!context)
608 continue;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700609 fputs(c_plain, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800610 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700611 else
612 fputs(c_new, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800613 for (j = 0; j < num_parent; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800614 if (p_mask & sl->flag)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800615 putchar('+');
Junio C Hamano46dc9412006-02-02 15:17:42 -0800616 else
617 putchar(' ');
618 p_mask <<= 1;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800619 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700620 printf("%.*s%s\n", sl->len, sl->bol, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800621 }
622 }
623}
624
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800625static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
626 int i, int j)
627{
628 /* We have already examined parent j and we know parent i
629 * and parent j are the same, so reuse the combined result
630 * of parent j for parent i.
631 */
632 unsigned long lno, imask, jmask;
633 imask = (1UL<<i);
634 jmask = (1UL<<j);
635
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700636 for (lno = 0; lno <= cnt; lno++) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800637 struct lline *ll = sline->lost_head;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800638 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800639 while (ll) {
640 if (ll->parent_map & jmask)
641 ll->parent_map |= imask;
642 ll = ll->next;
643 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800644 if (sline->flag & jmask)
645 sline->flag |= imask;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800646 sline++;
647 }
Junio C Hamano44627312006-02-06 18:54:08 -0800648 /* the overall size of the file (sline[cnt]) */
649 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800650}
651
Junio C Hamano462a15b2007-12-26 16:51:19 -0800652static void dump_quoted_path(const char *head,
653 const char *prefix,
654 const char *path,
Junio C Hamano567a03d2006-08-10 00:30:33 -0700655 const char *c_meta, const char *c_reset)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800656{
Junio C Hamano462a15b2007-12-26 16:51:19 -0800657 static struct strbuf buf = STRBUF_INIT;
658
659 strbuf_reset(&buf);
660 strbuf_addstr(&buf, c_meta);
661 strbuf_addstr(&buf, head);
Junio C Hamanod5625092007-12-26 17:13:36 -0800662 quote_two_c_style(&buf, prefix, path, 0);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800663 strbuf_addstr(&buf, c_reset);
664 puts(buf.buf);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700665}
666
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700667static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
668 int dense, struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800669{
Linus Torvalds91539832006-04-17 11:59:32 -0700670 struct diff_options *opt = &rev->diffopt;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700671 unsigned long result_size, cnt, lno;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -0500672 char *result, *cp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800673 struct sline *sline; /* survived lines */
Junio C Hamano2454c962006-02-06 12:53:07 -0800674 int mode_differs = 0;
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700675 int i, show_hunks;
David Rientjes0bef57e2006-08-15 13:37:19 -0700676 int working_tree_file = is_null_sha1(elem->sha1);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100677 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700678 mmfile_t result_file;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800679
Linus Torvaldsee1e5412006-05-13 13:23:48 -0700680 context = opt->context;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800681 /* Read the result of merge first */
Junio C Hamanof23fc772006-04-03 18:53:15 -0700682 if (!working_tree_file)
683 result = grab_blob(elem->sha1, &result_size);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800684 else {
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800685 /* Used by diff-tree to read from the working tree */
Junio C Hamanoea726d02006-01-28 00:03:38 -0800686 struct stat st;
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800687 int fd = -1;
688
689 if (lstat(elem->path, &st) < 0)
690 goto deleted_file;
691
692 if (S_ISLNK(st.st_mode)) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500693 size_t len = xsize_t(st.st_size);
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800694 result_size = len;
695 result = xmalloc(len + 1);
696 if (result_size != readlink(elem->path, result, len)) {
697 error("readlink(%s): %s", elem->path,
698 strerror(errno));
699 return;
700 }
701 result[len] = 0;
702 elem->mode = canon_mode(st.st_mode);
703 }
704 else if (0 <= (fd = open(elem->path, O_RDONLY)) &&
705 !fstat(fd, &st)) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500706 size_t len = xsize_t(st.st_size);
Heikki Orsilac697ad12008-05-03 16:27:26 +0300707 ssize_t done;
Johannes Sixta249a9b2007-03-03 20:38:00 +0100708 int is_file, i;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800709
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800710 elem->mode = canon_mode(st.st_mode);
Johannes Sixta249a9b2007-03-03 20:38:00 +0100711 /* if symlinks don't work, assume symlink if all parents
712 * are symlinks
713 */
714 is_file = has_symlinks;
715 for (i = 0; !is_file && i < num_parent; i++)
716 is_file = !S_ISLNK(elem->parent[i].mode);
717 if (!is_file)
718 elem->mode = canon_mode(S_IFLNK);
719
Junio C Hamanof23fc772006-04-03 18:53:15 -0700720 result_size = len;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800721 result = xmalloc(len + 1);
Heikki Orsilac697ad12008-05-03 16:27:26 +0300722
723 done = read_in_full(fd, result, len);
724 if (done < 0)
725 die("read error '%s'", elem->path);
726 else if (done < len)
727 die("early EOF '%s'", elem->path);
728
Junio C Hamanoea726d02006-01-28 00:03:38 -0800729 result[len] = 0;
730 }
731 else {
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800732 deleted_file:
Junio C Hamanof23fc772006-04-03 18:53:15 -0700733 result_size = 0;
Junio C Hamano713a11f2006-02-13 23:07:04 -0800734 elem->mode = 0;
Peter Eriksen28f75812006-07-25 09:30:18 +0200735 result = xcalloc(1, 1);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800736 }
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800737
Junio C Hamanoea726d02006-01-28 00:03:38 -0800738 if (0 <= fd)
739 close(fd);
740 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800741
Junio C Hamano2386c292006-06-28 01:38:19 -0700742 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800743 if (*cp == '\n')
744 cnt++;
745 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700746 if (result_size && result[result_size-1] != '\n')
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800747 cnt++; /* incomplete line */
748
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700749 sline = xcalloc(cnt+2, sizeof(*sline));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800750 sline[0].bol = result;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700751 for (lno = 0; lno <= cnt + 1; lno++) {
Junio C Hamano44627312006-02-06 18:54:08 -0800752 sline[lno].lost_tail = &sline[lno].lost_head;
753 sline[lno].flag = 0;
754 }
Junio C Hamano2386c292006-06-28 01:38:19 -0700755 for (lno = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800756 if (*cp == '\n') {
757 sline[lno].len = cp - sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800758 lno++;
759 if (lno < cnt)
760 sline[lno].bol = cp + 1;
761 }
762 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700763 if (result_size && result[result_size-1] != '\n')
764 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
765
766 result_file.ptr = result;
767 result_file.size = result_size;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800768
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700769 /* Even p_lno[cnt+1] is valid -- that is for the end line number
770 * for deletion hunk at the end.
771 */
772 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
773 for (lno = 0; lno <= cnt; lno++)
Junio C Hamanof16706c2006-01-30 22:33:15 -0800774 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
775
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800776 for (i = 0; i < num_parent; i++) {
777 int j;
778 for (j = 0; j < i; j++) {
David Rientjesa89fccd2006-08-17 11:54:57 -0700779 if (!hashcmp(elem->parent[i].sha1,
780 elem->parent[j].sha1)) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800781 reuse_combine_diff(sline, cnt, i, j);
782 break;
783 }
784 }
785 if (i <= j)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700786 combine_diff(elem->parent[i].sha1, &result_file, sline,
Junio C Hamanof16706c2006-01-30 22:33:15 -0800787 cnt, i, num_parent);
Junio C Hamano2454c962006-02-06 12:53:07 -0800788 if (elem->parent[i].mode != elem->mode)
789 mode_differs = 1;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800790 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800791
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800792 show_hunks = make_hunks(sline, cnt, num_parent, dense);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800793
Junio C Hamano713a11f2006-02-13 23:07:04 -0800794 if (show_hunks || mode_differs || working_tree_file) {
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800795 const char *abb;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100796 int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700797 const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
798 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700799 int added = 0;
800 int deleted = 0;
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800801
Junio C Hamano44152782006-10-26 02:05:59 -0700802 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -0700803 show_log(rev);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700804 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
Junio C Hamano462a15b2007-12-26 16:51:19 -0800805 "", elem->path, c_meta, c_reset);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700806 printf("%sindex ", c_meta);
Junio C Hamano823bcd62006-02-02 05:21:14 -0800807 for (i = 0; i < num_parent; i++) {
Junio C Hamano297a1aa2006-02-10 01:51:12 -0800808 abb = find_unique_abbrev(elem->parent[i].sha1,
Mark Woodinge70c6b32006-02-27 12:52:50 +0000809 abbrev);
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800810 printf("%s%s", i ? "," : "", abb);
Junio C Hamano823bcd62006-02-02 05:21:14 -0800811 }
Mark Woodinge70c6b32006-02-27 12:52:50 +0000812 abb = find_unique_abbrev(elem->sha1, abbrev);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700813 printf("..%s%s\n", abb, c_reset);
Junio C Hamano2454c962006-02-06 12:53:07 -0800814
815 if (mode_differs) {
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700816 deleted = !elem->mode;
817
818 /* We say it was added if nobody had it */
819 added = !deleted;
Junio C Hamanod416df82006-02-10 02:30:52 -0800820 for (i = 0; added && i < num_parent; i++)
821 if (elem->parent[i].status !=
822 DIFF_STATUS_ADDED)
823 added = 0;
824 if (added)
Junio C Hamano567a03d2006-08-10 00:30:33 -0700825 printf("%snew file mode %06o",
826 c_meta, elem->mode);
Junio C Hamanod416df82006-02-10 02:30:52 -0800827 else {
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700828 if (deleted)
Junio C Hamano567a03d2006-08-10 00:30:33 -0700829 printf("%sdeleted file ", c_meta);
Junio C Hamanod416df82006-02-10 02:30:52 -0800830 printf("mode ");
831 for (i = 0; i < num_parent; i++) {
832 printf("%s%06o", i ? "," : "",
833 elem->parent[i].mode);
834 }
835 if (elem->mode)
836 printf("..%06o", elem->mode);
Junio C Hamano2454c962006-02-06 12:53:07 -0800837 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700838 printf("%s\n", c_reset);
Junio C Hamano2454c962006-02-06 12:53:07 -0800839 }
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700840 if (added)
Junio C Hamano462a15b2007-12-26 16:51:19 -0800841 dump_quoted_path("--- ", "", "/dev/null",
842 c_meta, c_reset);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700843 else
Junio C Hamano462a15b2007-12-26 16:51:19 -0800844 dump_quoted_path("--- ", opt->a_prefix, elem->path,
845 c_meta, c_reset);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700846 if (deleted)
Junio C Hamano462a15b2007-12-26 16:51:19 -0800847 dump_quoted_path("+++ ", "", "/dev/null",
848 c_meta, c_reset);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700849 else
Junio C Hamano462a15b2007-12-26 16:51:19 -0800850 dump_quoted_path("+++ ", opt->b_prefix, elem->path,
851 c_meta, c_reset);
852 dump_sline(sline, cnt, num_parent,
853 DIFF_OPT_TST(opt, COLOR_DIFF));
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800854 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800855 free(result);
856
Junio C Hamano2386c292006-06-28 01:38:19 -0700857 for (lno = 0; lno < cnt; lno++) {
858 if (sline[lno].lost_head) {
859 struct lline *ll = sline[lno].lost_head;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800860 while (ll) {
861 struct lline *tmp = ll;
862 ll = ll->next;
863 free(tmp);
864 }
865 }
866 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800867 free(sline[0].p_lno);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800868 free(sline);
869}
870
Linus Torvaldsee638022006-02-09 10:30:28 -0800871#define COLONS "::::::::::::::::::::::::::::::::"
872
Linus Torvalds91539832006-04-17 11:59:32 -0700873static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
Linus Torvaldsee638022006-02-09 10:30:28 -0800874{
Linus Torvalds91539832006-04-17 11:59:32 -0700875 struct diff_options *opt = &rev->diffopt;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -0500876 int i, offset;
Linus Torvaldsee638022006-02-09 10:30:28 -0800877 const char *prefix;
878 int line_termination, inter_name_termination;
879
880 line_termination = opt->line_termination;
881 inter_name_termination = '\t';
882 if (!line_termination)
883 inter_name_termination = 0;
884
Junio C Hamano44152782006-10-26 02:05:59 -0700885 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -0700886 show_log(rev);
Linus Torvaldsee638022006-02-09 10:30:28 -0800887
Timo Hirvonenc6744342006-06-24 20:21:53 +0300888 if (opt->output_format & DIFF_FORMAT_RAW) {
Junio C Hamano0a798072006-02-09 15:23:06 -0800889 offset = strlen(COLONS) - num_parent;
890 if (offset < 0)
891 offset = 0;
892 prefix = COLONS + offset;
Linus Torvaldsee638022006-02-09 10:30:28 -0800893
Junio C Hamano0a798072006-02-09 15:23:06 -0800894 /* Show the modes */
895 for (i = 0; i < num_parent; i++) {
896 printf("%s%06o", prefix, p->parent[i].mode);
897 prefix = " ";
898 }
899 printf("%s%06o", prefix, p->mode);
900
901 /* Show sha1's */
902 for (i = 0; i < num_parent; i++)
903 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
904 opt->abbrev));
905 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
906 }
907
Timo Hirvonenc6744342006-06-24 20:21:53 +0300908 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamanod416df82006-02-10 02:30:52 -0800909 for (i = 0; i < num_parent; i++)
910 putchar(p->parent[i].status);
911 putchar(inter_name_termination);
912 }
Junio C Hamano0a798072006-02-09 15:23:06 -0800913
Pierre Habouzit663af342007-09-20 00:42:15 +0200914 write_name_quoted(p->path, stdout, line_termination);
Junio C Hamano0a798072006-02-09 15:23:06 -0800915}
916
Linus Torvalds91539832006-04-17 11:59:32 -0700917void show_combined_diff(struct combine_diff_path *p,
Junio C Hamano0a798072006-02-09 15:23:06 -0800918 int num_parent,
919 int dense,
Linus Torvalds91539832006-04-17 11:59:32 -0700920 struct rev_info *rev)
Junio C Hamano0a798072006-02-09 15:23:06 -0800921{
Linus Torvalds91539832006-04-17 11:59:32 -0700922 struct diff_options *opt = &rev->diffopt;
Junio C Hamano0a798072006-02-09 15:23:06 -0800923 if (!p->len)
Linus Torvalds91539832006-04-17 11:59:32 -0700924 return;
Timo Hirvonenc6744342006-06-24 20:21:53 +0300925 if (opt->output_format & (DIFF_FORMAT_RAW |
926 DIFF_FORMAT_NAME |
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700927 DIFF_FORMAT_NAME_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -0700928 show_raw_diff(p, num_parent, rev);
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700929 else if (opt->output_format & DIFF_FORMAT_PATCH)
Linus Torvalds91539832006-04-17 11:59:32 -0700930 show_patch_diff(p, num_parent, dense, rev);
Linus Torvaldsee638022006-02-09 10:30:28 -0800931}
932
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700933void diff_tree_combined(const unsigned char *sha1,
934 const unsigned char parent[][20],
935 int num_parent,
936 int dense,
937 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800938{
Linus Torvalds91539832006-04-17 11:59:32 -0700939 struct diff_options *opt = &rev->diffopt;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800940 struct diff_options diffopts;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800941 struct combine_diff_path *p, *paths = NULL;
Junio C Hamano3969cf72006-06-27 15:08:19 -0700942 int i, num_paths, needsep, show_log_first;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800943
Junio C Hamano5b236832006-02-09 14:35:19 -0800944 diffopts = *opt;
Junio C Hamano3969cf72006-06-27 15:08:19 -0700945 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100946 DIFF_OPT_SET(&diffopts, RECURSIVE);
947 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800948
Junio C Hamano44152782006-10-26 02:05:59 -0700949 show_log_first = !!rev->loginfo && !rev->no_commit_id;
Junio C Hamano3969cf72006-06-27 15:08:19 -0700950 needsep = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800951 /* find set of paths that everybody touches */
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700952 for (i = 0; i < num_parent; i++) {
Junio C Hamano965f8032006-04-17 22:53:03 -0700953 /* show stat against the first parent even
954 * when doing combined diff.
955 */
Junio C Hamano74e2abe2006-10-12 03:01:00 -0700956 int stat_opt = (opt->output_format &
957 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
958 if (i == 0 && stat_opt)
959 diffopts.output_format = stat_opt;
Junio C Hamano965f8032006-04-17 22:53:03 -0700960 else
961 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700962 diff_tree_sha1(parent[i], sha1, "", &diffopts);
Junio C Hamano5b236832006-02-09 14:35:19 -0800963 diffcore_std(&diffopts);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800964 paths = intersect_paths(paths, i, num_parent);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700965
Junio C Hamano3969cf72006-06-27 15:08:19 -0700966 if (show_log_first && i == 0) {
Adam Simpkins02865652008-04-29 01:32:59 -0700967 show_log(rev);
Junio C Hamano3969cf72006-06-27 15:08:19 -0700968 if (rev->verbose_header && opt->output_format)
969 putchar(opt->line_termination);
970 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800971 diff_flush(&diffopts);
972 }
973
974 /* find out surviving paths */
975 for (num_paths = 0, p = paths; p; p = p->next) {
976 if (p->len)
977 num_paths++;
978 }
Junio C Hamanoe3c3a552006-02-05 22:25:00 -0800979 if (num_paths) {
Timo Hirvonenc6744342006-06-24 20:21:53 +0300980 if (opt->output_format & (DIFF_FORMAT_RAW |
981 DIFF_FORMAT_NAME |
982 DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamano86ff1d22006-04-10 17:36:53 -0700983 for (p = paths; p; p = p->next) {
Timo Hirvonenc6744342006-06-24 20:21:53 +0300984 if (p->len)
985 show_raw_diff(p, num_parent, rev);
Junio C Hamano86ff1d22006-04-10 17:36:53 -0700986 }
Junio C Hamano3969cf72006-06-27 15:08:19 -0700987 needsep = 1;
Junio C Hamano86ff1d22006-04-10 17:36:53 -0700988 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -0700989 else if (opt->output_format &
990 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
Junio C Hamano3969cf72006-06-27 15:08:19 -0700991 needsep = 1;
Timo Hirvonenc6744342006-06-24 20:21:53 +0300992 if (opt->output_format & DIFF_FORMAT_PATCH) {
Junio C Hamano3969cf72006-06-27 15:08:19 -0700993 if (needsep)
994 putchar(opt->line_termination);
Timo Hirvonenc6744342006-06-24 20:21:53 +0300995 for (p = paths; p; p = p->next) {
996 if (p->len)
997 show_patch_diff(p, num_parent, dense,
998 rev);
999 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001000 }
1001 }
1002
1003 /* Clean things up */
1004 while (paths) {
Junio C Hamanoea726d02006-01-28 00:03:38 -08001005 struct combine_diff_path *tmp = paths;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001006 paths = paths->next;
1007 free(tmp);
1008 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001009}
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001010
1011void diff_tree_combined_merge(const unsigned char *sha1,
1012 int dense, struct rev_info *rev)
1013{
1014 int num_parent;
1015 const unsigned char (*parent)[20];
1016 struct commit *commit = lookup_commit(sha1);
1017 struct commit_list *parents;
1018
1019 /* count parents */
1020 for (parents = commit->parents, num_parent = 0;
1021 parents;
1022 parents = parents->next, num_parent++)
1023 ; /* nothing */
1024
1025 parent = xmalloc(num_parent * sizeof(*parent));
1026 for (parents = commit->parents, num_parent = 0;
1027 parents;
1028 parents = parents->next, num_parent++)
Shawn Pearcee7024962006-08-23 02:49:00 -04001029 hashcpy((unsigned char*)(parent + num_parent),
1030 parents->item->object.sha1);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001031 diff_tree_combined(sha1, parent, num_parent, dense, rev);
1032}