blob: 5b63af1eeb71907f23117bc552fa7a4019f09722 [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"
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080010
Junio C Hamanoea726d02006-01-28 00:03:38 -080011static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080012{
13 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamanoea726d02006-01-28 00:03:38 -080014 struct combine_diff_path *p;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080015 int i;
16
17 if (!n) {
Junio C Hamanoea726d02006-01-28 00:03:38 -080018 struct combine_diff_path *list = NULL, **tail = &list;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080019 for (i = 0; i < q->nr; i++) {
20 int len;
21 const char *path;
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070022 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080023 continue;
24 path = q->queue[i]->two->path;
25 len = strlen(path);
Junio C Hamano2454c962006-02-06 12:53:07 -080026 p = xmalloc(combine_diff_path_size(num_parent, len));
Felipe Contreras4b25d092009-05-01 12:06:36 +030027 p->path = (char *) &(p->parent[num_parent]);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080028 memcpy(p->path, path, len);
29 p->path[len] = 0;
30 p->len = len;
31 p->next = NULL;
Junio C Hamano2454c962006-02-06 12:53:07 -080032 memset(p->parent, 0,
33 sizeof(p->parent[0]) * num_parent);
34
Shawn Pearcee7024962006-08-23 02:49:00 -040035 hashcpy(p->sha1, q->queue[i]->two->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080036 p->mode = q->queue[i]->two->mode;
Shawn Pearcee7024962006-08-23 02:49:00 -040037 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080038 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080039 p->parent[n].status = q->queue[i]->status;
Junio C Hamano5290a0f2006-01-25 03:34:10 -080040 *tail = p;
41 tail = &p->next;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080042 }
43 return list;
44 }
45
46 for (p = curr; p; p = p->next) {
47 int found = 0;
48 if (!p->len)
49 continue;
50 for (i = 0; i < q->nr; i++) {
51 const char *path;
52 int len;
53
Junio C Hamanoa976b0a2006-08-14 18:36:00 -070054 if (diff_unmodified_pair(q->queue[i]))
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080055 continue;
56 path = q->queue[i]->two->path;
57 len = strlen(path);
58 if (len == p->len && !memcmp(path, p->path, len)) {
59 found = 1;
Shawn Pearcee7024962006-08-23 02:49:00 -040060 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
Junio C Hamano2454c962006-02-06 12:53:07 -080061 p->parent[n].mode = q->queue[i]->one->mode;
Junio C Hamanod416df82006-02-10 02:30:52 -080062 p->parent[n].status = q->queue[i]->status;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080063 break;
64 }
65 }
66 if (!found)
67 p->len = 0;
68 }
69 return curr;
70}
71
Junio C Hamanob469d8b2006-01-30 16:34:29 -080072/* Lines lost from parent */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080073struct lline {
74 struct lline *next;
75 int len;
76 unsigned long parent_map;
77 char line[FLEX_ARRAY];
78};
79
Junio C Hamanob469d8b2006-01-30 16:34:29 -080080/* Lines surviving in the merge result */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080081struct sline {
82 struct lline *lost_head, **lost_tail;
Junio C Hamano55d5d5b2009-07-22 14:48:28 -070083 struct lline *next_lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080084 char *bol;
85 int len;
Junio C Hamano46dc9412006-02-02 15:17:42 -080086 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
87 * we did not change it).
Junio C Hamanob469d8b2006-01-30 16:34:29 -080088 * bit N is used for "interesting" lines, including context.
Junio C Hamanoc86fbe52008-06-18 23:59:41 -070089 * bit (N+1) is used for "do not show deletion before this".
Junio C Hamanob469d8b2006-01-30 16:34:29 -080090 */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080091 unsigned long flag;
Junio C Hamanof16706c2006-01-30 22:33:15 -080092 unsigned long *p_lno;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080093};
94
Junio C Hamano7dae8b22009-04-29 12:49:52 -070095static char *grab_blob(const unsigned char *sha1, unsigned int mode, unsigned long *size)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080096{
97 char *blob;
Nicolas Pitre21666f12007-02-26 14:55:59 -050098 enum object_type type;
Junio C Hamano7dae8b22009-04-29 12:49:52 -070099
100 if (S_ISGITLINK(mode)) {
101 blob = xmalloc(100);
102 *size = snprintf(blob, 100,
103 "Subproject commit %s\n", sha1_to_hex(sha1));
104 } else if (is_null_sha1(sha1)) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800105 /* deleted blob */
106 *size = 0;
107 return xcalloc(1, 1);
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700108 } else {
109 blob = read_sha1_file(sha1, &type, size);
110 if (type != OBJ_BLOB)
111 die("object '%s' is not a blob!", sha1_to_hex(sha1));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800112 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800113 return blob;
114}
115
Junio C Hamanof23fc772006-04-03 18:53:15 -0700116static void append_lost(struct sline *sline, int n, const char *line, int len)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800117{
118 struct lline *lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800119 unsigned long this_mask = (1UL<<n);
120 if (line[len-1] == '\n')
121 len--;
122
123 /* Check to see if we can squash things */
124 if (sline->lost_head) {
Junio C Hamano55d5d5b2009-07-22 14:48:28 -0700125 lline = sline->next_lost;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800126 while (lline) {
127 if (lline->len == len &&
128 !memcmp(lline->line, line, len)) {
129 lline->parent_map |= this_mask;
Junio C Hamano55d5d5b2009-07-22 14:48:28 -0700130 sline->next_lost = lline->next;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800131 return;
132 }
133 lline = lline->next;
134 }
135 }
136
137 lline = xmalloc(sizeof(*lline) + len + 1);
138 lline->len = len;
139 lline->next = NULL;
140 lline->parent_map = this_mask;
141 memcpy(lline->line, line, len);
142 lline->line[len] = 0;
Junio C Hamano5290a0f2006-01-25 03:34:10 -0800143 *sline->lost_tail = lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800144 sline->lost_tail = &lline->next;
Junio C Hamano55d5d5b2009-07-22 14:48:28 -0700145 sline->next_lost = NULL;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800146}
147
Junio C Hamanof23fc772006-04-03 18:53:15 -0700148struct combine_diff_state {
Junio C Hamanoa0fd3142006-04-06 22:29:55 -0700149 unsigned int lno;
150 int ob, on, nb, nn;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700151 unsigned long nmask;
152 int num_parent;
153 int n;
154 struct sline *sline;
155 struct sline *lost_bucket;
156};
157
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700158static void consume_line(void *state_, char *line, unsigned long len)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700159{
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700160 struct combine_diff_state *state = state_;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700161 if (5 < len && !memcmp("@@ -", line, 4)) {
162 if (parse_hunk_header(line, len,
163 &state->ob, &state->on,
164 &state->nb, &state->nn))
165 return;
166 state->lno = state->nb;
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700167 if (state->nn == 0) {
Junio C Hamanof23fc772006-04-03 18:53:15 -0700168 /* @@ -X,Y +N,0 @@ removed Y lines
169 * that would have come *after* line N
170 * in the result. Our lost buckets hang
171 * to the line after the removed lines,
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700172 *
173 * Note that this is correct even when N == 0,
174 * in which case the hunk removes the first
175 * line in the file.
Junio C Hamanof23fc772006-04-03 18:53:15 -0700176 */
177 state->lost_bucket = &state->sline[state->nb];
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700178 if (!state->nb)
179 state->nb = 1;
180 } else {
Junio C Hamanof23fc772006-04-03 18:53:15 -0700181 state->lost_bucket = &state->sline[state->nb-1];
Junio C Hamanob810cbb2009-07-22 14:48:29 -0700182 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700183 if (!state->sline[state->nb-1].p_lno)
184 state->sline[state->nb-1].p_lno =
185 xcalloc(state->num_parent,
186 sizeof(unsigned long));
187 state->sline[state->nb-1].p_lno[state->n] = state->ob;
Junio C Hamano55d5d5b2009-07-22 14:48:28 -0700188 state->lost_bucket->next_lost = state->lost_bucket->lost_head;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700189 return;
190 }
191 if (!state->lost_bucket)
192 return; /* not in any hunk yet */
193 switch (line[0]) {
194 case '-':
195 append_lost(state->lost_bucket, state->n, line+1, len-1);
196 break;
197 case '+':
198 state->sline[state->lno-1].flag |= state->nmask;
199 state->lno++;
200 break;
201 }
202}
203
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700204static void combine_diff(const unsigned char *parent, unsigned int mode,
205 mmfile_t *result_file,
Junio C Hamano2386c292006-06-28 01:38:19 -0700206 struct sline *sline, unsigned int cnt, int n,
207 int num_parent)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800208{
Junio C Hamanof23fc772006-04-03 18:53:15 -0700209 unsigned int p_lno, lno;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800210 unsigned long nmask = (1UL << n);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700211 xpparam_t xpp;
212 xdemitconf_t xecfg;
213 mmfile_t parent_file;
214 xdemitcb_t ecb;
215 struct combine_diff_state state;
216 unsigned long sz;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800217
Junio C Hamano44627312006-02-06 18:54:08 -0800218 if (!cnt)
219 return; /* result deleted */
220
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700221 parent_file.ptr = grab_blob(parent, mode, &sz);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700222 parent_file.size = sz;
Brian Downing9ccd0a82008-10-25 15:30:37 +0200223 memset(&xpp, 0, sizeof(xpp));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700224 xpp.flags = XDF_NEED_MINIMAL;
Johannes Schindelin30b25012007-07-04 19:05:46 +0100225 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanof23fc772006-04-03 18:53:15 -0700226 memset(&state, 0, sizeof(state));
227 state.nmask = nmask;
228 state.sline = sline;
229 state.lno = 1;
230 state.num_parent = num_parent;
231 state.n = n;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800232
Junio C Hamano8a3f5242008-08-13 23:18:22 -0700233 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
234 &xpp, &xecfg, &ecb);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700235 free(parent_file.ptr);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800236
237 /* Assign line numbers for this parent.
238 *
239 * sline[lno].p_lno[n] records the first line number
240 * (counting from 1) for parent N if the final hunk display
241 * started by showing sline[lno] (possibly showing the lost
242 * lines attached to it first).
243 */
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700244 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
Junio C Hamanof16706c2006-01-30 22:33:15 -0800245 struct lline *ll;
246 sline[lno].p_lno[n] = p_lno;
247
248 /* How many lines would this sline advance the p_lno? */
249 ll = sline[lno].lost_head;
250 while (ll) {
251 if (ll->parent_map & nmask)
252 p_lno++; /* '-' means parent had it */
253 ll = ll->next;
254 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700255 if (lno < cnt && !(sline[lno].flag & nmask))
Junio C Hamanof16706c2006-01-30 22:33:15 -0800256 p_lno++; /* no '+' means parent had it */
257 }
258 sline[lno].p_lno[n] = p_lno; /* trailer */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800259}
260
261static unsigned long context = 3;
262static char combine_marker = '@';
263
264static int interesting(struct sline *sline, unsigned long all_mask)
265{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800266 /* If some parents lost lines here, or if we have added to
267 * some parent, it is interesting.
268 */
269 return ((sline->flag & all_mask) || sline->lost_head);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800270}
271
Junio C Hamano3ec19092006-01-26 03:53:01 -0800272static unsigned long adjust_hunk_tail(struct sline *sline,
273 unsigned long all_mask,
274 unsigned long hunk_begin,
275 unsigned long i)
276{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800277 /* i points at the first uninteresting line. If the last line
278 * of the hunk was interesting only because it has some
279 * deletion, then it is not all that interesting for the
280 * purpose of giving trailing context lines. This is because
281 * we output '-' line and then unmodified sline[i-1] itself in
282 * that case which gives us one extra context line.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800283 */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800284 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800285 i--;
286 return i;
287}
288
Junio C Hamano46dc9412006-02-02 15:17:42 -0800289static unsigned long find_next(struct sline *sline,
290 unsigned long mark,
291 unsigned long i,
292 unsigned long cnt,
Junio C Hamano2386c292006-06-28 01:38:19 -0700293 int look_for_uninteresting)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800294{
Junio C Hamano46dc9412006-02-02 15:17:42 -0800295 /* We have examined up to i-1 and are about to look at i.
296 * Find next interesting or uninteresting line. Here,
297 * "interesting" does not mean interesting(), but marked by
298 * the give_context() function below (i.e. it includes context
299 * lines that are not interesting to interesting() function
300 * that are surrounded by interesting() ones.
301 */
Junio C Hamano74065952006-04-11 14:31:31 -0700302 while (i <= cnt)
Junio C Hamano2386c292006-06-28 01:38:19 -0700303 if (look_for_uninteresting
Junio C Hamano46dc9412006-02-02 15:17:42 -0800304 ? !(sline[i].flag & mark)
305 : (sline[i].flag & mark))
Junio C Hamano3ec19092006-01-26 03:53:01 -0800306 return i;
307 else
308 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700309 return i;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800310}
311
312static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
313{
314 unsigned long all_mask = (1UL<<num_parent) - 1;
315 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700316 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800317 unsigned long i;
318
Junio C Hamano46dc9412006-02-02 15:17:42 -0800319 /* Two groups of interesting lines may have a short gap of
Pavel Roskin82e5a822006-07-10 01:50:18 -0400320 * uninteresting lines. Connect such groups to give them a
Junio C Hamano46dc9412006-02-02 15:17:42 -0800321 * bit of context.
322 *
323 * We first start from what the interesting() function says,
324 * and mark them with "mark", and paint context lines with the
325 * mark. So interesting() would still say false for such context
326 * lines but they are treated as "interesting" in the end.
327 */
328 i = find_next(sline, mark, 0, cnt, 0);
Junio C Hamano74065952006-04-11 14:31:31 -0700329 if (cnt < i)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800330 return 0;
331
Junio C Hamano74065952006-04-11 14:31:31 -0700332 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800333 unsigned long j = (context < i) ? (i - context) : 0;
334 unsigned long k;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800335
336 /* Paint a few lines before the first interesting line. */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800337 while (j < i)
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700338 sline[j++].flag |= mark | no_pre_delete;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800339
340 again:
Junio C Hamano46dc9412006-02-02 15:17:42 -0800341 /* we know up to i is to be included. where does the
342 * next uninteresting one start?
343 */
344 j = find_next(sline, mark, i, cnt, 1);
Junio C Hamano74065952006-04-11 14:31:31 -0700345 if (cnt < j)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800346 break; /* the rest are all interesting */
347
348 /* lookahead context lines */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800349 k = find_next(sline, mark, j, cnt, 0);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800350 j = adjust_hunk_tail(sline, all_mask, i, j);
351
352 if (k < j + context) {
353 /* k is interesting and [j,k) are not, but
354 * paint them interesting because the gap is small.
355 */
356 while (j < k)
357 sline[j++].flag |= mark;
358 i = k;
359 goto again;
360 }
361
362 /* j is the first uninteresting line and there is
Junio C Hamano46dc9412006-02-02 15:17:42 -0800363 * no overlap beyond it within context lines. Paint
364 * the trailing edge a bit.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800365 */
366 i = k;
Junio C Hamano74065952006-04-11 14:31:31 -0700367 k = (j + context < cnt+1) ? j + context : cnt+1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800368 while (j < k)
369 sline[j++].flag |= mark;
370 }
371 return 1;
372}
373
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800374static int make_hunks(struct sline *sline, unsigned long cnt,
Junio C Hamanod8f47902006-01-24 01:22:04 -0800375 int num_parent, int dense)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800376{
377 unsigned long all_mask = (1UL<<num_parent) - 1;
378 unsigned long mark = (1UL<<num_parent);
379 unsigned long i;
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800380 int has_interesting = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800381
Junio C Hamano74065952006-04-11 14:31:31 -0700382 for (i = 0; i <= cnt; i++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800383 if (interesting(&sline[i], all_mask))
384 sline[i].flag |= mark;
385 else
386 sline[i].flag &= ~mark;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800387 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800388 if (!dense)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800389 return give_context(sline, cnt, num_parent);
Junio C Hamanod8f47902006-01-24 01:22:04 -0800390
Junio C Hamano263eee22006-01-25 13:11:38 -0800391 /* Look at each hunk, and if we have changes from only one
392 * parent, or the changes are the same from all but one
393 * parent, mark that uninteresting.
Junio C Hamanod8f47902006-01-24 01:22:04 -0800394 */
395 i = 0;
Junio C Hamano74065952006-04-11 14:31:31 -0700396 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800397 unsigned long j, hunk_begin, hunk_end;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800398 unsigned long same_diff;
Junio C Hamano74065952006-04-11 14:31:31 -0700399 while (i <= cnt && !(sline[i].flag & mark))
Junio C Hamanod8f47902006-01-24 01:22:04 -0800400 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700401 if (cnt < i)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800402 break; /* No more interesting hunks */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800403 hunk_begin = i;
Junio C Hamano74065952006-04-11 14:31:31 -0700404 for (j = i + 1; j <= cnt; j++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800405 if (!(sline[j].flag & mark)) {
406 /* Look beyond the end to see if there
407 * is an interesting line after this
408 * hunk within context span.
409 */
410 unsigned long la; /* lookahead */
411 int contin = 0;
412 la = adjust_hunk_tail(sline, all_mask,
413 hunk_begin, j);
Junio C Hamano74065952006-04-11 14:31:31 -0700414 la = (la + context < cnt + 1) ?
415 (la + context) : cnt + 1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800416 while (j <= --la) {
417 if (sline[la].flag & mark) {
418 contin = 1;
419 break;
420 }
421 }
422 if (!contin)
423 break;
424 j = la;
425 }
426 }
427 hunk_end = j;
428
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800429 /* [i..hunk_end) are interesting. Now is it really
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800430 * interesting? We check if there are only two versions
431 * and the result matches one of them. That is, we look
432 * at:
433 * (+) line, which records lines added to which parents;
434 * this line appears in the result.
435 * (-) line, which records from what parents the line
436 * was removed; this line does not appear in the result.
437 * then check the set of parents the result has difference
438 * from, from all lines. If there are lines that has
439 * different set of parents that the result has differences
440 * from, that means we have more than two versions.
441 *
442 * Even when we have only two versions, if the result does
443 * not match any of the parents, the it should be considered
444 * interesting. In such a case, we would have all '+' line.
445 * After passing the above "two versions" test, that would
446 * appear as "the same set of parents" to be "all parents".
Junio C Hamanod8f47902006-01-24 01:22:04 -0800447 */
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800448 same_diff = 0;
449 has_interesting = 0;
450 for (j = i; j < hunk_end && !has_interesting; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800451 unsigned long this_diff = sline[j].flag & all_mask;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800452 struct lline *ll = sline[j].lost_head;
453 if (this_diff) {
454 /* This has some changes. Is it the
455 * same as others?
456 */
457 if (!same_diff)
458 same_diff = this_diff;
459 else if (same_diff != this_diff) {
460 has_interesting = 1;
461 break;
462 }
463 }
464 while (ll && !has_interesting) {
465 /* Lost this line from these parents;
466 * who are they? Are they the same?
467 */
468 this_diff = ll->parent_map;
469 if (!same_diff)
470 same_diff = this_diff;
471 else if (same_diff != this_diff) {
472 has_interesting = 1;
473 }
474 ll = ll->next;
475 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800476 }
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800477
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800478 if (!has_interesting && same_diff != all_mask) {
Junio C Hamanod8f47902006-01-24 01:22:04 -0800479 /* This hunk is not that interesting after all */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800480 for (j = hunk_begin; j < hunk_end; j++)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800481 sline[j].flag &= ~mark;
482 }
483 i = hunk_end;
484 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800485
486 has_interesting = give_context(sline, cnt, num_parent);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800487 return has_interesting;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800488}
489
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800490static 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 -0800491{
492 l0 = sline[l0].p_lno[n];
493 l1 = sline[l1].p_lno[n];
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800494 printf(" -%lu,%lu", l0, l1-l0-null_context);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800495}
496
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700497static int hunk_comment_line(const char *bol)
498{
Junio C Hamano7a8ac592006-10-26 02:05:05 -0700499 int ch;
500
501 if (!bol)
502 return 0;
503 ch = *bol & 0xff;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700504 return (isalpha(ch) || ch == '_' || ch == '$');
505}
506
Junio C Hamano39280972008-08-27 19:48:01 -0700507static void show_line_to_eol(const char *line, int len, const char *reset)
508{
509 int saw_cr_at_eol = 0;
510 if (len < 0)
511 len = strlen(line);
512 saw_cr_at_eol = (len && line[len-1] == '\r');
513
514 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
515 reset,
516 saw_cr_at_eol ? "\r" : "");
517}
518
Junio C Hamano567a03d2006-08-10 00:30:33 -0700519static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
520 int use_color)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800521{
522 unsigned long mark = (1UL<<num_parent);
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700523 unsigned long no_pre_delete = (2UL<<num_parent);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800524 int i;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800525 unsigned long lno = 0;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700526 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
527 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
528 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
529 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
530 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800531
Junio C Hamano44627312006-02-06 18:54:08 -0800532 if (!cnt)
533 return; /* result deleted */
534
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800535 while (1) {
Junio C Hamano8bc75742006-04-12 13:23:50 -0700536 unsigned long hunk_end;
537 unsigned long rlines;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700538 const char *hunk_comment = NULL;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800539 unsigned long null_context = 0;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700540
541 while (lno <= cnt && !(sline[lno].flag & mark)) {
542 if (hunk_comment_line(sline[lno].bol))
543 hunk_comment = sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800544 lno++;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700545 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700546 if (cnt < lno)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800547 break;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700548 else {
Junio C Hamano74065952006-04-11 14:31:31 -0700549 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700550 if (!(sline[hunk_end].flag & mark))
551 break;
552 }
Junio C Hamano74065952006-04-11 14:31:31 -0700553 rlines = hunk_end - lno;
554 if (cnt < hunk_end)
555 rlines--; /* pointing at the last delete hunk */
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800556
557 if (!context) {
558 /*
559 * Even when running with --unified=0, all
560 * lines in the hunk needs to be processed in
561 * the loop below in order to show the
562 * deletion recorded in lost_head. However,
563 * we do not want to show the resulting line
564 * with all blank context markers in such a
565 * case. Compensate.
566 */
567 unsigned long j;
568 for (j = lno; j < hunk_end; j++)
569 if (!(sline[j].flag & (mark-1)))
570 null_context++;
571 rlines -= null_context;
572 }
573
Junio C Hamano567a03d2006-08-10 00:30:33 -0700574 fputs(c_frag, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800575 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800576 for (i = 0; i < num_parent; i++)
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800577 show_parent_lno(sline, lno, hunk_end, i, null_context);
Junio C Hamano74065952006-04-11 14:31:31 -0700578 printf(" +%lu,%lu ", lno+1, rlines);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800579 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700580
581 if (hunk_comment) {
582 int comment_end = 0;
583 for (i = 0; i < 40; i++) {
584 int ch = hunk_comment[i] & 0xff;
585 if (!ch || ch == '\n')
586 break;
587 if (!isspace(ch))
588 comment_end = i;
589 }
590 if (comment_end)
591 putchar(' ');
592 for (i = 0; i < comment_end; i++)
593 putchar(hunk_comment[i]);
594 }
595
Junio C Hamano567a03d2006-08-10 00:30:33 -0700596 printf("%s\n", c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800597 while (lno < hunk_end) {
598 struct lline *ll;
599 int j;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800600 unsigned long p_mask;
Benjamin Kramerfd13b212009-03-07 21:02:26 +0100601 struct sline *sl = &sline[lno++];
Junio C Hamanoc86fbe52008-06-18 23:59:41 -0700602 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost_head;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800603 while (ll) {
Junio C Hamano567a03d2006-08-10 00:30:33 -0700604 fputs(c_old, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800605 for (j = 0; j < num_parent; j++) {
606 if (ll->parent_map & (1UL<<j))
607 putchar('-');
608 else
609 putchar(' ');
610 }
Junio C Hamano39280972008-08-27 19:48:01 -0700611 show_line_to_eol(ll->line, -1, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800612 ll = ll->next;
613 }
Junio C Hamano74065952006-04-11 14:31:31 -0700614 if (cnt < lno)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700615 break;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800616 p_mask = 1;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800617 if (!(sl->flag & (mark-1))) {
618 /*
619 * This sline was here to hang the
620 * lost lines in front of it.
621 */
622 if (!context)
623 continue;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700624 fputs(c_plain, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800625 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700626 else
627 fputs(c_new, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800628 for (j = 0; j < num_parent; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800629 if (p_mask & sl->flag)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800630 putchar('+');
Junio C Hamano46dc9412006-02-02 15:17:42 -0800631 else
632 putchar(' ');
633 p_mask <<= 1;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800634 }
Junio C Hamano39280972008-08-27 19:48:01 -0700635 show_line_to_eol(sl->bol, sl->len, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800636 }
637 }
638}
639
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800640static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
641 int i, int j)
642{
643 /* We have already examined parent j and we know parent i
644 * and parent j are the same, so reuse the combined result
645 * of parent j for parent i.
646 */
647 unsigned long lno, imask, jmask;
648 imask = (1UL<<i);
649 jmask = (1UL<<j);
650
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700651 for (lno = 0; lno <= cnt; lno++) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800652 struct lline *ll = sline->lost_head;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800653 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800654 while (ll) {
655 if (ll->parent_map & jmask)
656 ll->parent_map |= imask;
657 ll = ll->next;
658 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800659 if (sline->flag & jmask)
660 sline->flag |= imask;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800661 sline++;
662 }
Junio C Hamano44627312006-02-06 18:54:08 -0800663 /* the overall size of the file (sline[cnt]) */
664 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800665}
666
Junio C Hamano462a15b2007-12-26 16:51:19 -0800667static void dump_quoted_path(const char *head,
668 const char *prefix,
669 const char *path,
Junio C Hamano567a03d2006-08-10 00:30:33 -0700670 const char *c_meta, const char *c_reset)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800671{
Junio C Hamano462a15b2007-12-26 16:51:19 -0800672 static struct strbuf buf = STRBUF_INIT;
673
674 strbuf_reset(&buf);
675 strbuf_addstr(&buf, c_meta);
676 strbuf_addstr(&buf, head);
Junio C Hamanod5625092007-12-26 17:13:36 -0800677 quote_two_c_style(&buf, prefix, path, 0);
Junio C Hamano462a15b2007-12-26 16:51:19 -0800678 strbuf_addstr(&buf, c_reset);
679 puts(buf.buf);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700680}
681
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700682static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
683 int dense, struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800684{
Linus Torvalds91539832006-04-17 11:59:32 -0700685 struct diff_options *opt = &rev->diffopt;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700686 unsigned long result_size, cnt, lno;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -0500687 char *result, *cp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800688 struct sline *sline; /* survived lines */
Junio C Hamano2454c962006-02-06 12:53:07 -0800689 int mode_differs = 0;
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700690 int i, show_hunks;
David Rientjes0bef57e2006-08-15 13:37:19 -0700691 int working_tree_file = is_null_sha1(elem->sha1);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100692 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700693 const char *a_prefix, *b_prefix;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700694 mmfile_t result_file;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800695
Linus Torvaldsee1e5412006-05-13 13:23:48 -0700696 context = opt->context;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700697 a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
698 b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
699
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800700 /* Read the result of merge first */
Junio C Hamanof23fc772006-04-03 18:53:15 -0700701 if (!working_tree_file)
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700702 result = grab_blob(elem->sha1, elem->mode, &result_size);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800703 else {
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800704 /* Used by diff-tree to read from the working tree */
Junio C Hamanoea726d02006-01-28 00:03:38 -0800705 struct stat st;
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800706 int fd = -1;
707
708 if (lstat(elem->path, &st) < 0)
709 goto deleted_file;
710
711 if (S_ISLNK(st.st_mode)) {
Junio C Hamano912342d2008-12-17 12:37:58 -0800712 struct strbuf buf = STRBUF_INIT;
713
714 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800715 error("readlink(%s): %s", elem->path,
716 strerror(errno));
717 return;
718 }
Junio C Hamano912342d2008-12-17 12:37:58 -0800719 result_size = buf.len;
720 result = strbuf_detach(&buf, NULL);
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800721 elem->mode = canon_mode(st.st_mode);
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700722 } else if (S_ISDIR(st.st_mode)) {
723 unsigned char sha1[20];
724 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
725 result = grab_blob(elem->sha1, elem->mode, &result_size);
726 else
727 result = grab_blob(sha1, elem->mode, &result_size);
Kjetil Barvik91fcbcb2009-02-09 21:54:52 +0100728 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500729 size_t len = xsize_t(st.st_size);
Heikki Orsilac697ad12008-05-03 16:27:26 +0300730 ssize_t done;
Johannes Sixta249a9b2007-03-03 20:38:00 +0100731 int is_file, i;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800732
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800733 elem->mode = canon_mode(st.st_mode);
Johannes Sixta249a9b2007-03-03 20:38:00 +0100734 /* if symlinks don't work, assume symlink if all parents
735 * are symlinks
736 */
737 is_file = has_symlinks;
738 for (i = 0; !is_file && i < num_parent; i++)
739 is_file = !S_ISLNK(elem->parent[i].mode);
740 if (!is_file)
741 elem->mode = canon_mode(S_IFLNK);
742
Junio C Hamanof23fc772006-04-03 18:53:15 -0700743 result_size = len;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800744 result = xmalloc(len + 1);
Heikki Orsilac697ad12008-05-03 16:27:26 +0300745
746 done = read_in_full(fd, result, len);
747 if (done < 0)
Thomas Rast0721c312009-06-27 17:58:47 +0200748 die_errno("read error '%s'", elem->path);
Heikki Orsilac697ad12008-05-03 16:27:26 +0300749 else if (done < len)
750 die("early EOF '%s'", elem->path);
751
Junio C Hamanoea726d02006-01-28 00:03:38 -0800752 result[len] = 0;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +0400753
754 /* If not a fake symlink, apply filters, e.g. autocrlf */
755 if (is_file) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500756 struct strbuf buf = STRBUF_INIT;
Alexander Gavrilov5e568f92008-08-23 23:21:21 +0400757
Alexander Gavrilov5e568f92008-08-23 23:21:21 +0400758 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
759 free(result);
760 result = strbuf_detach(&buf, &len);
761 result_size = len;
762 }
763 }
Junio C Hamanoea726d02006-01-28 00:03:38 -0800764 }
765 else {
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800766 deleted_file:
Junio C Hamanof23fc772006-04-03 18:53:15 -0700767 result_size = 0;
Junio C Hamano713a11f2006-02-13 23:07:04 -0800768 elem->mode = 0;
Peter Eriksen28f75812006-07-25 09:30:18 +0200769 result = xcalloc(1, 1);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800770 }
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800771
Junio C Hamanoea726d02006-01-28 00:03:38 -0800772 if (0 <= fd)
773 close(fd);
774 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800775
Junio C Hamano2386c292006-06-28 01:38:19 -0700776 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800777 if (*cp == '\n')
778 cnt++;
779 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700780 if (result_size && result[result_size-1] != '\n')
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800781 cnt++; /* incomplete line */
782
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700783 sline = xcalloc(cnt+2, sizeof(*sline));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800784 sline[0].bol = result;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700785 for (lno = 0; lno <= cnt + 1; lno++) {
Junio C Hamano44627312006-02-06 18:54:08 -0800786 sline[lno].lost_tail = &sline[lno].lost_head;
787 sline[lno].flag = 0;
788 }
Junio C Hamano2386c292006-06-28 01:38:19 -0700789 for (lno = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800790 if (*cp == '\n') {
791 sline[lno].len = cp - sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800792 lno++;
793 if (lno < cnt)
794 sline[lno].bol = cp + 1;
795 }
796 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700797 if (result_size && result[result_size-1] != '\n')
798 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
799
800 result_file.ptr = result;
801 result_file.size = result_size;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800802
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700803 /* Even p_lno[cnt+1] is valid -- that is for the end line number
804 * for deletion hunk at the end.
805 */
806 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
807 for (lno = 0; lno <= cnt; lno++)
Junio C Hamanof16706c2006-01-30 22:33:15 -0800808 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
809
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800810 for (i = 0; i < num_parent; i++) {
811 int j;
812 for (j = 0; j < i; j++) {
David Rientjesa89fccd2006-08-17 11:54:57 -0700813 if (!hashcmp(elem->parent[i].sha1,
814 elem->parent[j].sha1)) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800815 reuse_combine_diff(sline, cnt, i, j);
816 break;
817 }
818 }
819 if (i <= j)
Junio C Hamano7dae8b22009-04-29 12:49:52 -0700820 combine_diff(elem->parent[i].sha1,
821 elem->parent[i].mode,
822 &result_file, sline,
Junio C Hamanof16706c2006-01-30 22:33:15 -0800823 cnt, i, num_parent);
Junio C Hamano2454c962006-02-06 12:53:07 -0800824 if (elem->parent[i].mode != elem->mode)
825 mode_differs = 1;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800826 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800827
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800828 show_hunks = make_hunks(sline, cnt, num_parent, dense);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800829
Junio C Hamano713a11f2006-02-13 23:07:04 -0800830 if (show_hunks || mode_differs || working_tree_file) {
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800831 const char *abb;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100832 int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700833 const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
834 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700835 int added = 0;
836 int deleted = 0;
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800837
Junio C Hamano44152782006-10-26 02:05:59 -0700838 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -0700839 show_log(rev);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700840 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
Junio C Hamano462a15b2007-12-26 16:51:19 -0800841 "", elem->path, c_meta, c_reset);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700842 printf("%sindex ", c_meta);
Junio C Hamano823bcd62006-02-02 05:21:14 -0800843 for (i = 0; i < num_parent; i++) {
Junio C Hamano297a1aa2006-02-10 01:51:12 -0800844 abb = find_unique_abbrev(elem->parent[i].sha1,
Mark Woodinge70c6b32006-02-27 12:52:50 +0000845 abbrev);
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800846 printf("%s%s", i ? "," : "", abb);
Junio C Hamano823bcd62006-02-02 05:21:14 -0800847 }
Mark Woodinge70c6b32006-02-27 12:52:50 +0000848 abb = find_unique_abbrev(elem->sha1, abbrev);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700849 printf("..%s%s\n", abb, c_reset);
Junio C Hamano2454c962006-02-06 12:53:07 -0800850
851 if (mode_differs) {
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700852 deleted = !elem->mode;
853
854 /* We say it was added if nobody had it */
855 added = !deleted;
Junio C Hamanod416df82006-02-10 02:30:52 -0800856 for (i = 0; added && i < num_parent; i++)
857 if (elem->parent[i].status !=
858 DIFF_STATUS_ADDED)
859 added = 0;
860 if (added)
Junio C Hamano567a03d2006-08-10 00:30:33 -0700861 printf("%snew file mode %06o",
862 c_meta, elem->mode);
Junio C Hamanod416df82006-02-10 02:30:52 -0800863 else {
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700864 if (deleted)
Junio C Hamano567a03d2006-08-10 00:30:33 -0700865 printf("%sdeleted file ", c_meta);
Junio C Hamanod416df82006-02-10 02:30:52 -0800866 printf("mode ");
867 for (i = 0; i < num_parent; i++) {
868 printf("%s%06o", i ? "," : "",
869 elem->parent[i].mode);
870 }
871 if (elem->mode)
872 printf("..%06o", elem->mode);
Junio C Hamano2454c962006-02-06 12:53:07 -0800873 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700874 printf("%s\n", c_reset);
Junio C Hamano2454c962006-02-06 12:53:07 -0800875 }
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700876 if (added)
Junio C Hamano462a15b2007-12-26 16:51:19 -0800877 dump_quoted_path("--- ", "", "/dev/null",
878 c_meta, c_reset);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700879 else
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700880 dump_quoted_path("--- ", a_prefix, elem->path,
Junio C Hamano462a15b2007-12-26 16:51:19 -0800881 c_meta, c_reset);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700882 if (deleted)
Junio C Hamano462a15b2007-12-26 16:51:19 -0800883 dump_quoted_path("+++ ", "", "/dev/null",
884 c_meta, c_reset);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700885 else
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700886 dump_quoted_path("+++ ", b_prefix, elem->path,
Junio C Hamano462a15b2007-12-26 16:51:19 -0800887 c_meta, c_reset);
888 dump_sline(sline, cnt, num_parent,
889 DIFF_OPT_TST(opt, COLOR_DIFF));
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800890 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800891 free(result);
892
Junio C Hamano2386c292006-06-28 01:38:19 -0700893 for (lno = 0; lno < cnt; lno++) {
894 if (sline[lno].lost_head) {
895 struct lline *ll = sline[lno].lost_head;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800896 while (ll) {
897 struct lline *tmp = ll;
898 ll = ll->next;
899 free(tmp);
900 }
901 }
902 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800903 free(sline[0].p_lno);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800904 free(sline);
905}
906
Linus Torvaldsee638022006-02-09 10:30:28 -0800907#define COLONS "::::::::::::::::::::::::::::::::"
908
Linus Torvalds91539832006-04-17 11:59:32 -0700909static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
Linus Torvaldsee638022006-02-09 10:30:28 -0800910{
Linus Torvalds91539832006-04-17 11:59:32 -0700911 struct diff_options *opt = &rev->diffopt;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -0500912 int i, offset;
Linus Torvaldsee638022006-02-09 10:30:28 -0800913 const char *prefix;
914 int line_termination, inter_name_termination;
915
916 line_termination = opt->line_termination;
917 inter_name_termination = '\t';
918 if (!line_termination)
919 inter_name_termination = 0;
920
Junio C Hamano44152782006-10-26 02:05:59 -0700921 if (rev->loginfo && !rev->no_commit_id)
Adam Simpkins02865652008-04-29 01:32:59 -0700922 show_log(rev);
Linus Torvaldsee638022006-02-09 10:30:28 -0800923
Timo Hirvonenc6744342006-06-24 20:21:53 +0300924 if (opt->output_format & DIFF_FORMAT_RAW) {
Junio C Hamano0a798072006-02-09 15:23:06 -0800925 offset = strlen(COLONS) - num_parent;
926 if (offset < 0)
927 offset = 0;
928 prefix = COLONS + offset;
Linus Torvaldsee638022006-02-09 10:30:28 -0800929
Junio C Hamano0a798072006-02-09 15:23:06 -0800930 /* Show the modes */
931 for (i = 0; i < num_parent; i++) {
932 printf("%s%06o", prefix, p->parent[i].mode);
933 prefix = " ";
934 }
935 printf("%s%06o", prefix, p->mode);
936
937 /* Show sha1's */
938 for (i = 0; i < num_parent; i++)
939 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
940 opt->abbrev));
941 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
942 }
943
Timo Hirvonenc6744342006-06-24 20:21:53 +0300944 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamanod416df82006-02-10 02:30:52 -0800945 for (i = 0; i < num_parent; i++)
946 putchar(p->parent[i].status);
947 putchar(inter_name_termination);
948 }
Junio C Hamano0a798072006-02-09 15:23:06 -0800949
Pierre Habouzit663af342007-09-20 00:42:15 +0200950 write_name_quoted(p->path, stdout, line_termination);
Junio C Hamano0a798072006-02-09 15:23:06 -0800951}
952
Linus Torvalds91539832006-04-17 11:59:32 -0700953void show_combined_diff(struct combine_diff_path *p,
Junio C Hamano0a798072006-02-09 15:23:06 -0800954 int num_parent,
955 int dense,
Linus Torvalds91539832006-04-17 11:59:32 -0700956 struct rev_info *rev)
Junio C Hamano0a798072006-02-09 15:23:06 -0800957{
Linus Torvalds91539832006-04-17 11:59:32 -0700958 struct diff_options *opt = &rev->diffopt;
Junio C Hamano0a798072006-02-09 15:23:06 -0800959 if (!p->len)
Linus Torvalds91539832006-04-17 11:59:32 -0700960 return;
Timo Hirvonenc6744342006-06-24 20:21:53 +0300961 if (opt->output_format & (DIFF_FORMAT_RAW |
962 DIFF_FORMAT_NAME |
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700963 DIFF_FORMAT_NAME_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -0700964 show_raw_diff(p, num_parent, rev);
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700965 else if (opt->output_format & DIFF_FORMAT_PATCH)
Linus Torvalds91539832006-04-17 11:59:32 -0700966 show_patch_diff(p, num_parent, dense, rev);
Linus Torvaldsee638022006-02-09 10:30:28 -0800967}
968
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700969void diff_tree_combined(const unsigned char *sha1,
970 const unsigned char parent[][20],
971 int num_parent,
972 int dense,
973 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800974{
Linus Torvalds91539832006-04-17 11:59:32 -0700975 struct diff_options *opt = &rev->diffopt;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800976 struct diff_options diffopts;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800977 struct combine_diff_path *p, *paths = NULL;
Junio C Hamano3969cf72006-06-27 15:08:19 -0700978 int i, num_paths, needsep, show_log_first;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800979
Junio C Hamano5b236832006-02-09 14:35:19 -0800980 diffopts = *opt;
Junio C Hamano3969cf72006-06-27 15:08:19 -0700981 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +0100982 DIFF_OPT_SET(&diffopts, RECURSIVE);
983 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800984
Junio C Hamano44152782006-10-26 02:05:59 -0700985 show_log_first = !!rev->loginfo && !rev->no_commit_id;
Junio C Hamano3969cf72006-06-27 15:08:19 -0700986 needsep = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800987 /* find set of paths that everybody touches */
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700988 for (i = 0; i < num_parent; i++) {
Junio C Hamano965f8032006-04-17 22:53:03 -0700989 /* show stat against the first parent even
990 * when doing combined diff.
991 */
Junio C Hamano74e2abe2006-10-12 03:01:00 -0700992 int stat_opt = (opt->output_format &
993 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
994 if (i == 0 && stat_opt)
995 diffopts.output_format = stat_opt;
Junio C Hamano965f8032006-04-17 22:53:03 -0700996 else
997 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700998 diff_tree_sha1(parent[i], sha1, "", &diffopts);
Junio C Hamano5b236832006-02-09 14:35:19 -0800999 diffcore_std(&diffopts);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001000 paths = intersect_paths(paths, i, num_parent);
Linus Torvaldseab144a2006-04-17 16:59:42 -07001001
Junio C Hamano3969cf72006-06-27 15:08:19 -07001002 if (show_log_first && i == 0) {
Adam Simpkins02865652008-04-29 01:32:59 -07001003 show_log(rev);
Junio C Hamano3969cf72006-06-27 15:08:19 -07001004 if (rev->verbose_header && opt->output_format)
1005 putchar(opt->line_termination);
1006 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001007 diff_flush(&diffopts);
1008 }
1009
1010 /* find out surviving paths */
1011 for (num_paths = 0, p = paths; p; p = p->next) {
1012 if (p->len)
1013 num_paths++;
1014 }
Junio C Hamanoe3c3a552006-02-05 22:25:00 -08001015 if (num_paths) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001016 if (opt->output_format & (DIFF_FORMAT_RAW |
1017 DIFF_FORMAT_NAME |
1018 DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001019 for (p = paths; p; p = p->next) {
Timo Hirvonenc6744342006-06-24 20:21:53 +03001020 if (p->len)
1021 show_raw_diff(p, num_parent, rev);
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001022 }
Junio C Hamano3969cf72006-06-27 15:08:19 -07001023 needsep = 1;
Junio C Hamano86ff1d22006-04-10 17:36:53 -07001024 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07001025 else if (opt->output_format &
1026 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
Junio C Hamano3969cf72006-06-27 15:08:19 -07001027 needsep = 1;
Timo Hirvonenc6744342006-06-24 20:21:53 +03001028 if (opt->output_format & DIFF_FORMAT_PATCH) {
Junio C Hamano3969cf72006-06-27 15:08:19 -07001029 if (needsep)
1030 putchar(opt->line_termination);
Timo Hirvonenc6744342006-06-24 20:21:53 +03001031 for (p = paths; p; p = p->next) {
1032 if (p->len)
1033 show_patch_diff(p, num_parent, dense,
1034 rev);
1035 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001036 }
1037 }
1038
1039 /* Clean things up */
1040 while (paths) {
Junio C Hamanoea726d02006-01-28 00:03:38 -08001041 struct combine_diff_path *tmp = paths;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001042 paths = paths->next;
1043 free(tmp);
1044 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001045}
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001046
1047void diff_tree_combined_merge(const unsigned char *sha1,
1048 int dense, struct rev_info *rev)
1049{
1050 int num_parent;
1051 const unsigned char (*parent)[20];
1052 struct commit *commit = lookup_commit(sha1);
1053 struct commit_list *parents;
1054
1055 /* count parents */
1056 for (parents = commit->parents, num_parent = 0;
1057 parents;
1058 parents = parents->next, num_parent++)
1059 ; /* nothing */
1060
1061 parent = xmalloc(num_parent * sizeof(*parent));
1062 for (parents = commit->parents, num_parent = 0;
1063 parents;
1064 parents = parents->next, num_parent++)
Felipe Contreras4b25d092009-05-01 12:06:36 +03001065 hashcpy((unsigned char *)(parent + num_parent),
Shawn Pearcee7024962006-08-23 02:49:00 -04001066 parents->item->object.sha1);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001067 diff_tree_combined(sha1, parent, num_parent, dense, rev);
1068}