blob: ea3ca5f950561a92fdd3be1a4ee4bbd726656118 [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.
87 */
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080088 unsigned long flag;
Junio C Hamanof16706c2006-01-30 22:33:15 -080089 unsigned long *p_lno;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080090};
91
92static char *grab_blob(const unsigned char *sha1, unsigned long *size)
93{
94 char *blob;
Nicolas Pitre21666f12007-02-26 14:55:59 -050095 enum object_type type;
David Rientjes0bef57e2006-08-15 13:37:19 -070096 if (is_null_sha1(sha1)) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -080097 /* deleted blob */
98 *size = 0;
99 return xcalloc(1, 1);
100 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500101 blob = read_sha1_file(sha1, &type, size);
102 if (type != OBJ_BLOB)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800103 die("object '%s' is not a blob!", sha1_to_hex(sha1));
104 return blob;
105}
106
Junio C Hamanof23fc772006-04-03 18:53:15 -0700107static void append_lost(struct sline *sline, int n, const char *line, int len)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800108{
109 struct lline *lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800110 unsigned long this_mask = (1UL<<n);
111 if (line[len-1] == '\n')
112 len--;
113
114 /* Check to see if we can squash things */
115 if (sline->lost_head) {
116 struct lline *last_one = NULL;
117 /* We cannot squash it with earlier one */
118 for (lline = sline->lost_head;
119 lline;
120 lline = lline->next)
121 if (lline->parent_map & this_mask)
122 last_one = lline;
123 lline = last_one ? last_one->next : sline->lost_head;
124 while (lline) {
125 if (lline->len == len &&
126 !memcmp(lline->line, line, len)) {
127 lline->parent_map |= this_mask;
128 return;
129 }
130 lline = lline->next;
131 }
132 }
133
134 lline = xmalloc(sizeof(*lline) + len + 1);
135 lline->len = len;
136 lline->next = NULL;
137 lline->parent_map = this_mask;
138 memcpy(lline->line, line, len);
139 lline->line[len] = 0;
Junio C Hamano5290a0f2006-01-25 03:34:10 -0800140 *sline->lost_tail = lline;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800141 sline->lost_tail = &lline->next;
142}
143
Junio C Hamanof23fc772006-04-03 18:53:15 -0700144struct combine_diff_state {
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700145 struct xdiff_emit_state xm;
146
Junio C Hamanoa0fd3142006-04-06 22:29:55 -0700147 unsigned int lno;
148 int ob, on, nb, nn;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700149 unsigned long nmask;
150 int num_parent;
151 int n;
152 struct sline *sline;
153 struct sline *lost_bucket;
154};
155
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700156static void consume_line(void *state_, char *line, unsigned long len)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700157{
Junio C Hamanod9ea73e2006-04-05 02:03:58 -0700158 struct combine_diff_state *state = state_;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700159 if (5 < len && !memcmp("@@ -", line, 4)) {
160 if (parse_hunk_header(line, len,
161 &state->ob, &state->on,
162 &state->nb, &state->nn))
163 return;
164 state->lno = state->nb;
165 if (!state->nb)
166 /* @@ -1,2 +0,0 @@ to remove the
167 * first two lines...
168 */
169 state->nb = 1;
170 if (state->nn == 0)
171 /* @@ -X,Y +N,0 @@ removed Y lines
172 * that would have come *after* line N
173 * in the result. Our lost buckets hang
174 * to the line after the removed lines,
175 */
176 state->lost_bucket = &state->sline[state->nb];
177 else
178 state->lost_bucket = &state->sline[state->nb-1];
179 if (!state->sline[state->nb-1].p_lno)
180 state->sline[state->nb-1].p_lno =
181 xcalloc(state->num_parent,
182 sizeof(unsigned long));
183 state->sline[state->nb-1].p_lno[state->n] = state->ob;
184 return;
185 }
186 if (!state->lost_bucket)
187 return; /* not in any hunk yet */
188 switch (line[0]) {
189 case '-':
190 append_lost(state->lost_bucket, state->n, line+1, len-1);
191 break;
192 case '+':
193 state->sline[state->lno-1].flag |= state->nmask;
194 state->lno++;
195 break;
196 }
197}
198
Junio C Hamanof23fc772006-04-03 18:53:15 -0700199static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
Junio C Hamano2386c292006-06-28 01:38:19 -0700200 struct sline *sline, unsigned int cnt, int n,
201 int num_parent)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800202{
Junio C Hamanof23fc772006-04-03 18:53:15 -0700203 unsigned int p_lno, lno;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800204 unsigned long nmask = (1UL << n);
Junio C Hamanof23fc772006-04-03 18:53:15 -0700205 xpparam_t xpp;
206 xdemitconf_t xecfg;
207 mmfile_t parent_file;
208 xdemitcb_t ecb;
209 struct combine_diff_state state;
210 unsigned long sz;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800211
Junio C Hamano44627312006-02-06 18:54:08 -0800212 if (!cnt)
213 return; /* result deleted */
214
Junio C Hamanof23fc772006-04-03 18:53:15 -0700215 parent_file.ptr = grab_blob(parent, &sz);
216 parent_file.size = sz;
217 xpp.flags = XDF_NEED_MINIMAL;
218 xecfg.ctxlen = 0;
219 xecfg.flags = 0;
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 Hamanof23fc772006-04-03 18:53:15 -0700230 xdl_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);
312 unsigned long i;
313
Junio C Hamano46dc9412006-02-02 15:17:42 -0800314 /* Two groups of interesting lines may have a short gap of
Pavel Roskin82e5a822006-07-10 01:50:18 -0400315 * uninteresting lines. Connect such groups to give them a
Junio C Hamano46dc9412006-02-02 15:17:42 -0800316 * bit of context.
317 *
318 * We first start from what the interesting() function says,
319 * and mark them with "mark", and paint context lines with the
320 * mark. So interesting() would still say false for such context
321 * lines but they are treated as "interesting" in the end.
322 */
323 i = find_next(sline, mark, 0, cnt, 0);
Junio C Hamano74065952006-04-11 14:31:31 -0700324 if (cnt < i)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800325 return 0;
326
Junio C Hamano74065952006-04-11 14:31:31 -0700327 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800328 unsigned long j = (context < i) ? (i - context) : 0;
329 unsigned long k;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800330
331 /* Paint a few lines before the first interesting line. */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800332 while (j < i)
333 sline[j++].flag |= mark;
334
335 again:
Junio C Hamano46dc9412006-02-02 15:17:42 -0800336 /* we know up to i is to be included. where does the
337 * next uninteresting one start?
338 */
339 j = find_next(sline, mark, i, cnt, 1);
Junio C Hamano74065952006-04-11 14:31:31 -0700340 if (cnt < j)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800341 break; /* the rest are all interesting */
342
343 /* lookahead context lines */
Junio C Hamano46dc9412006-02-02 15:17:42 -0800344 k = find_next(sline, mark, j, cnt, 0);
Junio C Hamano3ec19092006-01-26 03:53:01 -0800345 j = adjust_hunk_tail(sline, all_mask, i, j);
346
347 if (k < j + context) {
348 /* k is interesting and [j,k) are not, but
349 * paint them interesting because the gap is small.
350 */
351 while (j < k)
352 sline[j++].flag |= mark;
353 i = k;
354 goto again;
355 }
356
357 /* j is the first uninteresting line and there is
Junio C Hamano46dc9412006-02-02 15:17:42 -0800358 * no overlap beyond it within context lines. Paint
359 * the trailing edge a bit.
Junio C Hamano3ec19092006-01-26 03:53:01 -0800360 */
361 i = k;
Junio C Hamano74065952006-04-11 14:31:31 -0700362 k = (j + context < cnt+1) ? j + context : cnt+1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800363 while (j < k)
364 sline[j++].flag |= mark;
365 }
366 return 1;
367}
368
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800369static int make_hunks(struct sline *sline, unsigned long cnt,
Junio C Hamanod8f47902006-01-24 01:22:04 -0800370 int num_parent, int dense)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800371{
372 unsigned long all_mask = (1UL<<num_parent) - 1;
373 unsigned long mark = (1UL<<num_parent);
374 unsigned long i;
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800375 int has_interesting = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800376
Junio C Hamano74065952006-04-11 14:31:31 -0700377 for (i = 0; i <= cnt; i++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800378 if (interesting(&sline[i], all_mask))
379 sline[i].flag |= mark;
380 else
381 sline[i].flag &= ~mark;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800382 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800383 if (!dense)
Junio C Hamano3ec19092006-01-26 03:53:01 -0800384 return give_context(sline, cnt, num_parent);
Junio C Hamanod8f47902006-01-24 01:22:04 -0800385
Junio C Hamano263eee22006-01-25 13:11:38 -0800386 /* Look at each hunk, and if we have changes from only one
387 * parent, or the changes are the same from all but one
388 * parent, mark that uninteresting.
Junio C Hamanod8f47902006-01-24 01:22:04 -0800389 */
390 i = 0;
Junio C Hamano74065952006-04-11 14:31:31 -0700391 while (i <= cnt) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800392 unsigned long j, hunk_begin, hunk_end;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800393 unsigned long same_diff;
Junio C Hamano74065952006-04-11 14:31:31 -0700394 while (i <= cnt && !(sline[i].flag & mark))
Junio C Hamanod8f47902006-01-24 01:22:04 -0800395 i++;
Junio C Hamano74065952006-04-11 14:31:31 -0700396 if (cnt < i)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800397 break; /* No more interesting hunks */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800398 hunk_begin = i;
Junio C Hamano74065952006-04-11 14:31:31 -0700399 for (j = i + 1; j <= cnt; j++) {
Junio C Hamano3ec19092006-01-26 03:53:01 -0800400 if (!(sline[j].flag & mark)) {
401 /* Look beyond the end to see if there
402 * is an interesting line after this
403 * hunk within context span.
404 */
405 unsigned long la; /* lookahead */
406 int contin = 0;
407 la = adjust_hunk_tail(sline, all_mask,
408 hunk_begin, j);
Junio C Hamano74065952006-04-11 14:31:31 -0700409 la = (la + context < cnt + 1) ?
410 (la + context) : cnt + 1;
Junio C Hamano3ec19092006-01-26 03:53:01 -0800411 while (j <= --la) {
412 if (sline[la].flag & mark) {
413 contin = 1;
414 break;
415 }
416 }
417 if (!contin)
418 break;
419 j = la;
420 }
421 }
422 hunk_end = j;
423
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800424 /* [i..hunk_end) are interesting. Now is it really
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800425 * interesting? We check if there are only two versions
426 * and the result matches one of them. That is, we look
427 * at:
428 * (+) line, which records lines added to which parents;
429 * this line appears in the result.
430 * (-) line, which records from what parents the line
431 * was removed; this line does not appear in the result.
432 * then check the set of parents the result has difference
433 * from, from all lines. If there are lines that has
434 * different set of parents that the result has differences
435 * from, that means we have more than two versions.
436 *
437 * Even when we have only two versions, if the result does
438 * not match any of the parents, the it should be considered
439 * interesting. In such a case, we would have all '+' line.
440 * After passing the above "two versions" test, that would
441 * appear as "the same set of parents" to be "all parents".
Junio C Hamanod8f47902006-01-24 01:22:04 -0800442 */
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800443 same_diff = 0;
444 has_interesting = 0;
445 for (j = i; j < hunk_end && !has_interesting; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800446 unsigned long this_diff = sline[j].flag & all_mask;
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800447 struct lline *ll = sline[j].lost_head;
448 if (this_diff) {
449 /* This has some changes. Is it the
450 * same as others?
451 */
452 if (!same_diff)
453 same_diff = this_diff;
454 else if (same_diff != this_diff) {
455 has_interesting = 1;
456 break;
457 }
458 }
459 while (ll && !has_interesting) {
460 /* Lost this line from these parents;
461 * who are they? Are they the same?
462 */
463 this_diff = ll->parent_map;
464 if (!same_diff)
465 same_diff = this_diff;
466 else if (same_diff != this_diff) {
467 has_interesting = 1;
468 }
469 ll = ll->next;
470 }
Junio C Hamanod8f47902006-01-24 01:22:04 -0800471 }
Junio C Hamanobf1c32b2006-02-02 00:12:55 -0800472
Junio C Hamanofd4b1d22006-02-02 01:28:08 -0800473 if (!has_interesting && same_diff != all_mask) {
Junio C Hamanod8f47902006-01-24 01:22:04 -0800474 /* This hunk is not that interesting after all */
Junio C Hamano3ec19092006-01-26 03:53:01 -0800475 for (j = hunk_begin; j < hunk_end; j++)
Junio C Hamanod8f47902006-01-24 01:22:04 -0800476 sline[j].flag &= ~mark;
477 }
478 i = hunk_end;
479 }
Junio C Hamano3ec19092006-01-26 03:53:01 -0800480
481 has_interesting = give_context(sline, cnt, num_parent);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800482 return has_interesting;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800483}
484
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800485static 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 -0800486{
487 l0 = sline[l0].p_lno[n];
488 l1 = sline[l1].p_lno[n];
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800489 printf(" -%lu,%lu", l0, l1-l0-null_context);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800490}
491
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700492static int hunk_comment_line(const char *bol)
493{
Junio C Hamano7a8ac592006-10-26 02:05:05 -0700494 int ch;
495
496 if (!bol)
497 return 0;
498 ch = *bol & 0xff;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700499 return (isalpha(ch) || ch == '_' || ch == '$');
500}
501
Junio C Hamano567a03d2006-08-10 00:30:33 -0700502static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
503 int use_color)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800504{
505 unsigned long mark = (1UL<<num_parent);
506 int i;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800507 unsigned long lno = 0;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700508 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
509 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
510 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
511 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
512 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800513
Junio C Hamano44627312006-02-06 18:54:08 -0800514 if (!cnt)
515 return; /* result deleted */
516
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800517 while (1) {
518 struct sline *sl = &sline[lno];
Junio C Hamano8bc75742006-04-12 13:23:50 -0700519 unsigned long hunk_end;
520 unsigned long rlines;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700521 const char *hunk_comment = NULL;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800522 unsigned long null_context = 0;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700523
524 while (lno <= cnt && !(sline[lno].flag & mark)) {
525 if (hunk_comment_line(sline[lno].bol))
526 hunk_comment = sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800527 lno++;
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700528 }
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700529 if (cnt < lno)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800530 break;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700531 else {
Junio C Hamano74065952006-04-11 14:31:31 -0700532 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700533 if (!(sline[hunk_end].flag & mark))
534 break;
535 }
Junio C Hamano74065952006-04-11 14:31:31 -0700536 rlines = hunk_end - lno;
537 if (cnt < hunk_end)
538 rlines--; /* pointing at the last delete hunk */
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800539
540 if (!context) {
541 /*
542 * Even when running with --unified=0, all
543 * lines in the hunk needs to be processed in
544 * the loop below in order to show the
545 * deletion recorded in lost_head. However,
546 * we do not want to show the resulting line
547 * with all blank context markers in such a
548 * case. Compensate.
549 */
550 unsigned long j;
551 for (j = lno; j < hunk_end; j++)
552 if (!(sline[j].flag & (mark-1)))
553 null_context++;
554 rlines -= null_context;
555 }
556
Junio C Hamano567a03d2006-08-10 00:30:33 -0700557 fputs(c_frag, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800558 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanof16706c2006-01-30 22:33:15 -0800559 for (i = 0; i < num_parent; i++)
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800560 show_parent_lno(sline, lno, hunk_end, i, null_context);
Junio C Hamano74065952006-04-11 14:31:31 -0700561 printf(" +%lu,%lu ", lno+1, rlines);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800562 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700563
564 if (hunk_comment) {
565 int comment_end = 0;
566 for (i = 0; i < 40; i++) {
567 int ch = hunk_comment[i] & 0xff;
568 if (!ch || ch == '\n')
569 break;
570 if (!isspace(ch))
571 comment_end = i;
572 }
573 if (comment_end)
574 putchar(' ');
575 for (i = 0; i < comment_end; i++)
576 putchar(hunk_comment[i]);
577 }
578
Junio C Hamano567a03d2006-08-10 00:30:33 -0700579 printf("%s\n", c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800580 while (lno < hunk_end) {
581 struct lline *ll;
582 int j;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800583 unsigned long p_mask;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800584 sl = &sline[lno++];
585 ll = sl->lost_head;
586 while (ll) {
Junio C Hamano567a03d2006-08-10 00:30:33 -0700587 fputs(c_old, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800588 for (j = 0; j < num_parent; j++) {
589 if (ll->parent_map & (1UL<<j))
590 putchar('-');
591 else
592 putchar(' ');
593 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700594 printf("%s%s\n", ll->line, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800595 ll = ll->next;
596 }
Junio C Hamano74065952006-04-11 14:31:31 -0700597 if (cnt < lno)
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700598 break;
Junio C Hamano46dc9412006-02-02 15:17:42 -0800599 p_mask = 1;
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800600 if (!(sl->flag & (mark-1))) {
601 /*
602 * This sline was here to hang the
603 * lost lines in front of it.
604 */
605 if (!context)
606 continue;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700607 fputs(c_plain, stdout);
Junio C Hamano3b0f5e82007-02-03 12:37:54 -0800608 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700609 else
610 fputs(c_new, stdout);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800611 for (j = 0; j < num_parent; j++) {
Junio C Hamano46dc9412006-02-02 15:17:42 -0800612 if (p_mask & sl->flag)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800613 putchar('+');
Junio C Hamano46dc9412006-02-02 15:17:42 -0800614 else
615 putchar(' ');
616 p_mask <<= 1;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800617 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700618 printf("%.*s%s\n", sl->len, sl->bol, c_reset);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800619 }
620 }
621}
622
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800623static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
624 int i, int j)
625{
626 /* We have already examined parent j and we know parent i
627 * and parent j are the same, so reuse the combined result
628 * of parent j for parent i.
629 */
630 unsigned long lno, imask, jmask;
631 imask = (1UL<<i);
632 jmask = (1UL<<j);
633
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700634 for (lno = 0; lno <= cnt; lno++) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800635 struct lline *ll = sline->lost_head;
Junio C Hamanof16706c2006-01-30 22:33:15 -0800636 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800637 while (ll) {
638 if (ll->parent_map & jmask)
639 ll->parent_map |= imask;
640 ll = ll->next;
641 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800642 if (sline->flag & jmask)
643 sline->flag |= imask;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800644 sline++;
645 }
Junio C Hamano44627312006-02-06 18:54:08 -0800646 /* the overall size of the file (sline[cnt]) */
647 sline->p_lno[i] = sline->p_lno[j];
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800648}
649
Junio C Hamano567a03d2006-08-10 00:30:33 -0700650static void dump_quoted_path(const char *prefix, const char *path,
651 const char *c_meta, const char *c_reset)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800652{
Junio C Hamano567a03d2006-08-10 00:30:33 -0700653 printf("%s%s", c_meta, prefix);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700654 if (quote_c_style(path, NULL, NULL, 0))
655 quote_c_style(path, NULL, stdout, 0);
656 else
657 printf("%s", path);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700658 printf("%s\n", c_reset);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700659}
660
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700661static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
662 int dense, struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800663{
Linus Torvalds91539832006-04-17 11:59:32 -0700664 struct diff_options *opt = &rev->diffopt;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700665 unsigned long result_size, cnt, lno;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -0500666 char *result, *cp;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800667 struct sline *sline; /* survived lines */
Junio C Hamano2454c962006-02-06 12:53:07 -0800668 int mode_differs = 0;
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700669 int i, show_hunks;
David Rientjes0bef57e2006-08-15 13:37:19 -0700670 int working_tree_file = is_null_sha1(elem->sha1);
Mark Woodinge70c6b32006-02-27 12:52:50 +0000671 int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
Junio C Hamanof23fc772006-04-03 18:53:15 -0700672 mmfile_t result_file;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800673
Linus Torvaldsee1e5412006-05-13 13:23:48 -0700674 context = opt->context;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800675 /* Read the result of merge first */
Junio C Hamanof23fc772006-04-03 18:53:15 -0700676 if (!working_tree_file)
677 result = grab_blob(elem->sha1, &result_size);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800678 else {
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800679 /* Used by diff-tree to read from the working tree */
Junio C Hamanoea726d02006-01-28 00:03:38 -0800680 struct stat st;
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800681 int fd = -1;
682
683 if (lstat(elem->path, &st) < 0)
684 goto deleted_file;
685
686 if (S_ISLNK(st.st_mode)) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500687 size_t len = xsize_t(st.st_size);
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800688 result_size = len;
689 result = xmalloc(len + 1);
690 if (result_size != readlink(elem->path, result, len)) {
691 error("readlink(%s): %s", elem->path,
692 strerror(errno));
693 return;
694 }
695 result[len] = 0;
696 elem->mode = canon_mode(st.st_mode);
697 }
698 else if (0 <= (fd = open(elem->path, O_RDONLY)) &&
699 !fstat(fd, &st)) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500700 size_t len = xsize_t(st.st_size);
Jim Meyeringee24ee52007-02-27 00:11:35 +0100701 size_t sz = 0;
Johannes Sixta249a9b2007-03-03 20:38:00 +0100702 int is_file, i;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800703
Junio C Hamano1b0c7172006-03-29 22:55:43 -0800704 elem->mode = canon_mode(st.st_mode);
Johannes Sixta249a9b2007-03-03 20:38:00 +0100705 /* if symlinks don't work, assume symlink if all parents
706 * are symlinks
707 */
708 is_file = has_symlinks;
709 for (i = 0; !is_file && i < num_parent; i++)
710 is_file = !S_ISLNK(elem->parent[i].mode);
711 if (!is_file)
712 elem->mode = canon_mode(S_IFLNK);
713
Junio C Hamanof23fc772006-04-03 18:53:15 -0700714 result_size = len;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800715 result = xmalloc(len + 1);
Junio C Hamano2386c292006-06-28 01:38:19 -0700716 while (sz < len) {
Johan Herland8a912bc2007-05-15 14:49:22 +0200717 ssize_t done = xread(fd, result+sz, len-sz);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800718 if (done == 0)
719 break;
720 if (done < 0)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700721 die("read error '%s'", elem->path);
Junio C Hamano2386c292006-06-28 01:38:19 -0700722 sz += done;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800723 }
724 result[len] = 0;
725 }
726 else {
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800727 deleted_file:
Junio C Hamanof23fc772006-04-03 18:53:15 -0700728 result_size = 0;
Junio C Hamano713a11f2006-02-13 23:07:04 -0800729 elem->mode = 0;
Peter Eriksen28f75812006-07-25 09:30:18 +0200730 result = xcalloc(1, 1);
Junio C Hamanoea726d02006-01-28 00:03:38 -0800731 }
Junio C Hamano4fc970c2007-02-25 22:24:47 -0800732
Junio C Hamanoea726d02006-01-28 00:03:38 -0800733 if (0 <= fd)
734 close(fd);
735 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800736
Junio C Hamano2386c292006-06-28 01:38:19 -0700737 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800738 if (*cp == '\n')
739 cnt++;
740 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700741 if (result_size && result[result_size-1] != '\n')
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800742 cnt++; /* incomplete line */
743
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700744 sline = xcalloc(cnt+2, sizeof(*sline));
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800745 sline[0].bol = result;
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700746 for (lno = 0; lno <= cnt + 1; lno++) {
Junio C Hamano44627312006-02-06 18:54:08 -0800747 sline[lno].lost_tail = &sline[lno].lost_head;
748 sline[lno].flag = 0;
749 }
Junio C Hamano2386c292006-06-28 01:38:19 -0700750 for (lno = 0, cp = result; cp < result + result_size; cp++) {
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800751 if (*cp == '\n') {
752 sline[lno].len = cp - sline[lno].bol;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800753 lno++;
754 if (lno < cnt)
755 sline[lno].bol = cp + 1;
756 }
757 }
Junio C Hamanof23fc772006-04-03 18:53:15 -0700758 if (result_size && result[result_size-1] != '\n')
759 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
760
761 result_file.ptr = result;
762 result_file.size = result_size;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800763
Junio C Hamano8a470eb2006-04-11 03:13:29 -0700764 /* Even p_lno[cnt+1] is valid -- that is for the end line number
765 * for deletion hunk at the end.
766 */
767 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
768 for (lno = 0; lno <= cnt; lno++)
Junio C Hamanof16706c2006-01-30 22:33:15 -0800769 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
770
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800771 for (i = 0; i < num_parent; i++) {
772 int j;
773 for (j = 0; j < i; j++) {
David Rientjesa89fccd2006-08-17 11:54:57 -0700774 if (!hashcmp(elem->parent[i].sha1,
775 elem->parent[j].sha1)) {
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800776 reuse_combine_diff(sline, cnt, i, j);
777 break;
778 }
779 }
780 if (i <= j)
Junio C Hamanof23fc772006-04-03 18:53:15 -0700781 combine_diff(elem->parent[i].sha1, &result_file, sline,
Junio C Hamanof16706c2006-01-30 22:33:15 -0800782 cnt, i, num_parent);
Junio C Hamano2454c962006-02-06 12:53:07 -0800783 if (elem->parent[i].mode != elem->mode)
784 mode_differs = 1;
Junio C Hamano3c39e9b2006-02-01 23:29:03 -0800785 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800786
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800787 show_hunks = make_hunks(sline, cnt, num_parent, dense);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800788
Junio C Hamano713a11f2006-02-13 23:07:04 -0800789 if (show_hunks || mode_differs || working_tree_file) {
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800790 const char *abb;
Junio C Hamano567a03d2006-08-10 00:30:33 -0700791 int use_color = opt->color_diff;
792 const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
793 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700794 int added = 0;
795 int deleted = 0;
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800796
Junio C Hamano44152782006-10-26 02:05:59 -0700797 if (rev->loginfo && !rev->no_commit_id)
Timo Hirvonen39bc9a62006-06-25 13:54:14 +0300798 show_log(rev, opt->msg_sep);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700799 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
800 elem->path, c_meta, c_reset);
801 printf("%sindex ", c_meta);
Junio C Hamano823bcd62006-02-02 05:21:14 -0800802 for (i = 0; i < num_parent; i++) {
Junio C Hamano297a1aa2006-02-10 01:51:12 -0800803 abb = find_unique_abbrev(elem->parent[i].sha1,
Mark Woodinge70c6b32006-02-27 12:52:50 +0000804 abbrev);
Junio C Hamano9843a1f2006-02-06 12:30:00 -0800805 printf("%s%s", i ? "," : "", abb);
Junio C Hamano823bcd62006-02-02 05:21:14 -0800806 }
Mark Woodinge70c6b32006-02-27 12:52:50 +0000807 abb = find_unique_abbrev(elem->sha1, abbrev);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700808 printf("..%s%s\n", abb, c_reset);
Junio C Hamano2454c962006-02-06 12:53:07 -0800809
810 if (mode_differs) {
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700811 deleted = !elem->mode;
812
813 /* We say it was added if nobody had it */
814 added = !deleted;
Junio C Hamanod416df82006-02-10 02:30:52 -0800815 for (i = 0; added && i < num_parent; i++)
816 if (elem->parent[i].status !=
817 DIFF_STATUS_ADDED)
818 added = 0;
819 if (added)
Junio C Hamano567a03d2006-08-10 00:30:33 -0700820 printf("%snew file mode %06o",
821 c_meta, elem->mode);
Junio C Hamanod416df82006-02-10 02:30:52 -0800822 else {
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700823 if (deleted)
Junio C Hamano567a03d2006-08-10 00:30:33 -0700824 printf("%sdeleted file ", c_meta);
Junio C Hamanod416df82006-02-10 02:30:52 -0800825 printf("mode ");
826 for (i = 0; i < num_parent; i++) {
827 printf("%s%06o", i ? "," : "",
828 elem->parent[i].mode);
829 }
830 if (elem->mode)
831 printf("..%06o", elem->mode);
Junio C Hamano2454c962006-02-06 12:53:07 -0800832 }
Junio C Hamano567a03d2006-08-10 00:30:33 -0700833 printf("%s\n", c_reset);
Junio C Hamano2454c962006-02-06 12:53:07 -0800834 }
Junio C Hamanod5f6a012006-10-26 00:05:04 -0700835 if (added)
836 dump_quoted_path("--- /dev/", "null", c_meta, c_reset);
837 else
838 dump_quoted_path("--- a/", elem->path, c_meta, c_reset);
839 if (deleted)
840 dump_quoted_path("+++ /dev/", "null", c_meta, c_reset);
841 else
842 dump_quoted_path("+++ b/", elem->path, c_meta, c_reset);
Junio C Hamano567a03d2006-08-10 00:30:33 -0700843 dump_sline(sline, cnt, num_parent, opt->color_diff);
Junio C Hamano8828cdc2006-01-25 14:26:22 -0800844 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800845 free(result);
846
Junio C Hamano2386c292006-06-28 01:38:19 -0700847 for (lno = 0; lno < cnt; lno++) {
848 if (sline[lno].lost_head) {
849 struct lline *ll = sline[lno].lost_head;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800850 while (ll) {
851 struct lline *tmp = ll;
852 ll = ll->next;
853 free(tmp);
854 }
855 }
856 }
Junio C Hamano46dc9412006-02-02 15:17:42 -0800857 free(sline[0].p_lno);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800858 free(sline);
859}
860
Linus Torvaldsee638022006-02-09 10:30:28 -0800861#define COLONS "::::::::::::::::::::::::::::::::"
862
Linus Torvalds91539832006-04-17 11:59:32 -0700863static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
Linus Torvaldsee638022006-02-09 10:30:28 -0800864{
Linus Torvalds91539832006-04-17 11:59:32 -0700865 struct diff_options *opt = &rev->diffopt;
Serge E. Hallyn310f8b52006-04-17 10:14:47 -0500866 int i, offset;
Linus Torvaldsee638022006-02-09 10:30:28 -0800867 const char *prefix;
868 int line_termination, inter_name_termination;
869
870 line_termination = opt->line_termination;
871 inter_name_termination = '\t';
872 if (!line_termination)
873 inter_name_termination = 0;
874
Junio C Hamano44152782006-10-26 02:05:59 -0700875 if (rev->loginfo && !rev->no_commit_id)
Timo Hirvonen39bc9a62006-06-25 13:54:14 +0300876 show_log(rev, opt->msg_sep);
Linus Torvaldsee638022006-02-09 10:30:28 -0800877
Timo Hirvonenc6744342006-06-24 20:21:53 +0300878 if (opt->output_format & DIFF_FORMAT_RAW) {
Junio C Hamano0a798072006-02-09 15:23:06 -0800879 offset = strlen(COLONS) - num_parent;
880 if (offset < 0)
881 offset = 0;
882 prefix = COLONS + offset;
Linus Torvaldsee638022006-02-09 10:30:28 -0800883
Junio C Hamano0a798072006-02-09 15:23:06 -0800884 /* Show the modes */
885 for (i = 0; i < num_parent; i++) {
886 printf("%s%06o", prefix, p->parent[i].mode);
887 prefix = " ";
888 }
889 printf("%s%06o", prefix, p->mode);
890
891 /* Show sha1's */
892 for (i = 0; i < num_parent; i++)
893 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
894 opt->abbrev));
895 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
896 }
897
Timo Hirvonenc6744342006-06-24 20:21:53 +0300898 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamanod416df82006-02-10 02:30:52 -0800899 for (i = 0; i < num_parent; i++)
900 putchar(p->parent[i].status);
901 putchar(inter_name_termination);
902 }
Junio C Hamano0a798072006-02-09 15:23:06 -0800903
904 if (line_termination) {
905 if (quote_c_style(p->path, NULL, NULL, 0))
906 quote_c_style(p->path, NULL, stdout, 0);
907 else
908 printf("%s", p->path);
909 putchar(line_termination);
910 }
911 else {
912 printf("%s%c", p->path, line_termination);
913 }
914}
915
Linus Torvalds91539832006-04-17 11:59:32 -0700916void show_combined_diff(struct combine_diff_path *p,
Junio C Hamano0a798072006-02-09 15:23:06 -0800917 int num_parent,
918 int dense,
Linus Torvalds91539832006-04-17 11:59:32 -0700919 struct rev_info *rev)
Junio C Hamano0a798072006-02-09 15:23:06 -0800920{
Linus Torvalds91539832006-04-17 11:59:32 -0700921 struct diff_options *opt = &rev->diffopt;
Junio C Hamano0a798072006-02-09 15:23:06 -0800922 if (!p->len)
Linus Torvalds91539832006-04-17 11:59:32 -0700923 return;
Timo Hirvonenc6744342006-06-24 20:21:53 +0300924 if (opt->output_format & (DIFF_FORMAT_RAW |
925 DIFF_FORMAT_NAME |
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700926 DIFF_FORMAT_NAME_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -0700927 show_raw_diff(p, num_parent, rev);
Junio C Hamano89b0c4b2006-08-13 19:19:34 -0700928 else if (opt->output_format & DIFF_FORMAT_PATCH)
Linus Torvalds91539832006-04-17 11:59:32 -0700929 show_patch_diff(p, num_parent, dense, rev);
Linus Torvaldsee638022006-02-09 10:30:28 -0800930}
931
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700932void diff_tree_combined(const unsigned char *sha1,
933 const unsigned char parent[][20],
934 int num_parent,
935 int dense,
936 struct rev_info *rev)
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800937{
Linus Torvalds91539832006-04-17 11:59:32 -0700938 struct diff_options *opt = &rev->diffopt;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800939 struct diff_options diffopts;
Junio C Hamanoea726d02006-01-28 00:03:38 -0800940 struct combine_diff_path *p, *paths = NULL;
Junio C Hamano3969cf72006-06-27 15:08:19 -0700941 int i, num_paths, needsep, show_log_first;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800942
Junio C Hamano5b236832006-02-09 14:35:19 -0800943 diffopts = *opt;
Junio C Hamano3969cf72006-06-27 15:08:19 -0700944 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800945 diffopts.recursive = 1;
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700946 diffopts.allow_external = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800947
Junio C Hamano44152782006-10-26 02:05:59 -0700948 show_log_first = !!rev->loginfo && !rev->no_commit_id;
Junio C Hamano3969cf72006-06-27 15:08:19 -0700949 needsep = 0;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800950 /* find set of paths that everybody touches */
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700951 for (i = 0; i < num_parent; i++) {
Junio C Hamano965f8032006-04-17 22:53:03 -0700952 /* show stat against the first parent even
953 * when doing combined diff.
954 */
Junio C Hamano74e2abe2006-10-12 03:01:00 -0700955 int stat_opt = (opt->output_format &
956 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
957 if (i == 0 && stat_opt)
958 diffopts.output_format = stat_opt;
Junio C Hamano965f8032006-04-17 22:53:03 -0700959 else
960 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -0700961 diff_tree_sha1(parent[i], sha1, "", &diffopts);
Junio C Hamano5b236832006-02-09 14:35:19 -0800962 diffcore_std(&diffopts);
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800963 paths = intersect_paths(paths, i, num_parent);
Linus Torvaldseab144a2006-04-17 16:59:42 -0700964
Junio C Hamano3969cf72006-06-27 15:08:19 -0700965 if (show_log_first && i == 0) {
Timo Hirvonen39bc9a62006-06-25 13:54:14 +0300966 show_log(rev, opt->msg_sep);
Junio C Hamano3969cf72006-06-27 15:08:19 -0700967 if (rev->verbose_header && opt->output_format)
968 putchar(opt->line_termination);
969 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800970 diff_flush(&diffopts);
971 }
972
973 /* find out surviving paths */
974 for (num_paths = 0, p = paths; p; p = p->next) {
975 if (p->len)
976 num_paths++;
977 }
Junio C Hamanoe3c3a552006-02-05 22:25:00 -0800978 if (num_paths) {
Timo Hirvonenc6744342006-06-24 20:21:53 +0300979 if (opt->output_format & (DIFF_FORMAT_RAW |
980 DIFF_FORMAT_NAME |
981 DIFF_FORMAT_NAME_STATUS)) {
Junio C Hamano86ff1d22006-04-10 17:36:53 -0700982 for (p = paths; p; p = p->next) {
Timo Hirvonenc6744342006-06-24 20:21:53 +0300983 if (p->len)
984 show_raw_diff(p, num_parent, rev);
Junio C Hamano86ff1d22006-04-10 17:36:53 -0700985 }
Junio C Hamano3969cf72006-06-27 15:08:19 -0700986 needsep = 1;
Junio C Hamano86ff1d22006-04-10 17:36:53 -0700987 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -0700988 else if (opt->output_format &
989 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
Junio C Hamano3969cf72006-06-27 15:08:19 -0700990 needsep = 1;
Timo Hirvonenc6744342006-06-24 20:21:53 +0300991 if (opt->output_format & DIFF_FORMAT_PATCH) {
Junio C Hamano3969cf72006-06-27 15:08:19 -0700992 if (needsep)
993 putchar(opt->line_termination);
Timo Hirvonenc6744342006-06-24 20:21:53 +0300994 for (p = paths; p; p = p->next) {
995 if (p->len)
996 show_patch_diff(p, num_parent, dense,
997 rev);
998 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -0800999 }
1000 }
1001
1002 /* Clean things up */
1003 while (paths) {
Junio C Hamanoea726d02006-01-28 00:03:38 -08001004 struct combine_diff_path *tmp = paths;
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001005 paths = paths->next;
1006 free(tmp);
1007 }
Junio C Hamanoaf3feef2006-01-24 01:22:04 -08001008}
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001009
1010void diff_tree_combined_merge(const unsigned char *sha1,
1011 int dense, struct rev_info *rev)
1012{
1013 int num_parent;
1014 const unsigned char (*parent)[20];
1015 struct commit *commit = lookup_commit(sha1);
1016 struct commit_list *parents;
1017
1018 /* count parents */
1019 for (parents = commit->parents, num_parent = 0;
1020 parents;
1021 parents = parents->next, num_parent++)
1022 ; /* nothing */
1023
1024 parent = xmalloc(num_parent * sizeof(*parent));
1025 for (parents = commit->parents, num_parent = 0;
1026 parents;
1027 parents = parents->next, num_parent++)
Shawn Pearcee7024962006-08-23 02:49:00 -04001028 hashcpy((unsigned char*)(parent + num_parent),
1029 parents->item->object.sha1);
Junio C Hamano0fe7c1d2006-04-29 01:24:49 -07001030 diff_tree_combined(sha1, parent, num_parent, dense, rev);
1031}