7 #include "xdiff-interface.h"
11 static struct combine_diff_path
*intersect_paths(struct combine_diff_path
*curr
, int n
, int num_parent
)
13 struct diff_queue_struct
*q
= &diff_queued_diff
;
14 struct combine_diff_path
*p
;
18 struct combine_diff_path
*list
= NULL
, **tail
= &list
;
19 for (i
= 0; i
< q
->nr
; i
++) {
22 if (diff_unmodified_pair(q
->queue
[i
]))
24 path
= q
->queue
[i
]->two
->path
;
26 p
= xmalloc(combine_diff_path_size(num_parent
, len
));
27 p
->path
= (char *) &(p
->parent
[num_parent
]);
28 memcpy(p
->path
, path
, len
);
33 sizeof(p
->parent
[0]) * num_parent
);
35 hashcpy(p
->sha1
, q
->queue
[i
]->two
->sha1
);
36 p
->mode
= q
->queue
[i
]->two
->mode
;
37 hashcpy(p
->parent
[n
].sha1
, q
->queue
[i
]->one
->sha1
);
38 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
39 p
->parent
[n
].status
= q
->queue
[i
]->status
;
46 for (p
= curr
; p
; p
= p
->next
) {
50 for (i
= 0; i
< q
->nr
; i
++) {
54 if (diff_unmodified_pair(q
->queue
[i
]))
56 path
= q
->queue
[i
]->two
->path
;
58 if (len
== p
->len
&& !memcmp(path
, p
->path
, len
)) {
60 hashcpy(p
->parent
[n
].sha1
, q
->queue
[i
]->one
->sha1
);
61 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
62 p
->parent
[n
].status
= q
->queue
[i
]->status
;
72 /* Lines lost from parent */
76 unsigned long parent_map
;
77 char line
[FLEX_ARRAY
];
80 /* Lines surviving in the merge result */
82 struct lline
*lost_head
, **lost_tail
;
83 struct lline
*next_lost
;
86 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
87 * we did not change it).
88 * bit N is used for "interesting" lines, including context.
89 * bit (N+1) is used for "do not show deletion before this".
95 static char *grab_blob(const unsigned char *sha1
, unsigned int mode
, unsigned long *size
)
98 enum object_type type
;
100 if (S_ISGITLINK(mode
)) {
102 *size
= snprintf(blob
, 100,
103 "Subproject commit %s\n", sha1_to_hex(sha1
));
104 } else if (is_null_sha1(sha1
)) {
107 return xcalloc(1, 1);
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
));
116 static void append_lost(struct sline
*sline
, int n
, const char *line
, int len
)
119 unsigned long this_mask
= (1UL<<n
);
120 if (line
[len
-1] == '\n')
123 /* Check to see if we can squash things */
124 if (sline
->lost_head
) {
125 lline
= sline
->next_lost
;
127 if (lline
->len
== len
&&
128 !memcmp(lline
->line
, line
, len
)) {
129 lline
->parent_map
|= this_mask
;
130 sline
->next_lost
= lline
->next
;
137 lline
= xmalloc(sizeof(*lline
) + len
+ 1);
140 lline
->parent_map
= this_mask
;
141 memcpy(lline
->line
, line
, len
);
142 lline
->line
[len
] = 0;
143 *sline
->lost_tail
= lline
;
144 sline
->lost_tail
= &lline
->next
;
145 sline
->next_lost
= NULL
;
148 struct combine_diff_state
{
155 struct sline
*lost_bucket
;
158 static void consume_line(void *state_
, char *line
, unsigned long len
)
160 struct combine_diff_state
*state
= state_
;
161 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
162 if (parse_hunk_header(line
, len
,
163 &state
->ob
, &state
->on
,
164 &state
->nb
, &state
->nn
))
166 state
->lno
= state
->nb
;
168 /* @@ -1,2 +0,0 @@ to remove the
173 /* @@ -X,Y +N,0 @@ removed Y lines
174 * that would have come *after* line N
175 * in the result. Our lost buckets hang
176 * to the line after the removed lines,
178 state
->lost_bucket
= &state
->sline
[state
->nb
];
180 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
181 if (!state
->sline
[state
->nb
-1].p_lno
)
182 state
->sline
[state
->nb
-1].p_lno
=
183 xcalloc(state
->num_parent
,
184 sizeof(unsigned long));
185 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
186 state
->lost_bucket
->next_lost
= state
->lost_bucket
->lost_head
;
189 if (!state
->lost_bucket
)
190 return; /* not in any hunk yet */
193 append_lost(state
->lost_bucket
, state
->n
, line
+1, len
-1);
196 state
->sline
[state
->lno
-1].flag
|= state
->nmask
;
202 static void combine_diff(const unsigned char *parent
, unsigned int mode
,
203 mmfile_t
*result_file
,
204 struct sline
*sline
, unsigned int cnt
, int n
,
207 unsigned int p_lno
, lno
;
208 unsigned long nmask
= (1UL << n
);
211 mmfile_t parent_file
;
213 struct combine_diff_state state
;
217 return; /* result deleted */
219 parent_file
.ptr
= grab_blob(parent
, mode
, &sz
);
220 parent_file
.size
= sz
;
221 memset(&xpp
, 0, sizeof(xpp
));
222 xpp
.flags
= XDF_NEED_MINIMAL
;
223 memset(&xecfg
, 0, sizeof(xecfg
));
224 memset(&state
, 0, sizeof(state
));
228 state
.num_parent
= num_parent
;
231 xdi_diff_outf(&parent_file
, result_file
, consume_line
, &state
,
233 free(parent_file
.ptr
);
235 /* Assign line numbers for this parent.
237 * sline[lno].p_lno[n] records the first line number
238 * (counting from 1) for parent N if the final hunk display
239 * started by showing sline[lno] (possibly showing the lost
240 * lines attached to it first).
242 for (lno
= 0, p_lno
= 1; lno
<= cnt
; lno
++) {
244 sline
[lno
].p_lno
[n
] = p_lno
;
246 /* How many lines would this sline advance the p_lno? */
247 ll
= sline
[lno
].lost_head
;
249 if (ll
->parent_map
& nmask
)
250 p_lno
++; /* '-' means parent had it */
253 if (lno
< cnt
&& !(sline
[lno
].flag
& nmask
))
254 p_lno
++; /* no '+' means parent had it */
256 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
259 static unsigned long context
= 3;
260 static char combine_marker
= '@';
262 static int interesting(struct sline
*sline
, unsigned long all_mask
)
264 /* If some parents lost lines here, or if we have added to
265 * some parent, it is interesting.
267 return ((sline
->flag
& all_mask
) || sline
->lost_head
);
270 static unsigned long adjust_hunk_tail(struct sline
*sline
,
271 unsigned long all_mask
,
272 unsigned long hunk_begin
,
275 /* i points at the first uninteresting line. If the last line
276 * of the hunk was interesting only because it has some
277 * deletion, then it is not all that interesting for the
278 * purpose of giving trailing context lines. This is because
279 * we output '-' line and then unmodified sline[i-1] itself in
280 * that case which gives us one extra context line.
282 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
287 static unsigned long find_next(struct sline
*sline
,
291 int look_for_uninteresting
)
293 /* We have examined up to i-1 and are about to look at i.
294 * Find next interesting or uninteresting line. Here,
295 * "interesting" does not mean interesting(), but marked by
296 * the give_context() function below (i.e. it includes context
297 * lines that are not interesting to interesting() function
298 * that are surrounded by interesting() ones.
301 if (look_for_uninteresting
302 ? !(sline
[i
].flag
& mark
)
303 : (sline
[i
].flag
& mark
))
310 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
312 unsigned long all_mask
= (1UL<<num_parent
) - 1;
313 unsigned long mark
= (1UL<<num_parent
);
314 unsigned long no_pre_delete
= (2UL<<num_parent
);
317 /* Two groups of interesting lines may have a short gap of
318 * uninteresting lines. Connect such groups to give them a
321 * We first start from what the interesting() function says,
322 * and mark them with "mark", and paint context lines with the
323 * mark. So interesting() would still say false for such context
324 * lines but they are treated as "interesting" in the end.
326 i
= find_next(sline
, mark
, 0, cnt
, 0);
331 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
334 /* Paint a few lines before the first interesting line. */
336 sline
[j
++].flag
|= mark
| no_pre_delete
;
339 /* we know up to i is to be included. where does the
340 * next uninteresting one start?
342 j
= find_next(sline
, mark
, i
, cnt
, 1);
344 break; /* the rest are all interesting */
346 /* lookahead context lines */
347 k
= find_next(sline
, mark
, j
, cnt
, 0);
348 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
350 if (k
< j
+ context
) {
351 /* k is interesting and [j,k) are not, but
352 * paint them interesting because the gap is small.
355 sline
[j
++].flag
|= mark
;
360 /* j is the first uninteresting line and there is
361 * no overlap beyond it within context lines. Paint
362 * the trailing edge a bit.
365 k
= (j
+ context
< cnt
+1) ? j
+ context
: cnt
+1;
367 sline
[j
++].flag
|= mark
;
372 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
373 int num_parent
, int dense
)
375 unsigned long all_mask
= (1UL<<num_parent
) - 1;
376 unsigned long mark
= (1UL<<num_parent
);
378 int has_interesting
= 0;
380 for (i
= 0; i
<= cnt
; i
++) {
381 if (interesting(&sline
[i
], all_mask
))
382 sline
[i
].flag
|= mark
;
384 sline
[i
].flag
&= ~mark
;
387 return give_context(sline
, cnt
, num_parent
);
389 /* Look at each hunk, and if we have changes from only one
390 * parent, or the changes are the same from all but one
391 * parent, mark that uninteresting.
395 unsigned long j
, hunk_begin
, hunk_end
;
396 unsigned long same_diff
;
397 while (i
<= cnt
&& !(sline
[i
].flag
& mark
))
400 break; /* No more interesting hunks */
402 for (j
= i
+ 1; j
<= cnt
; j
++) {
403 if (!(sline
[j
].flag
& mark
)) {
404 /* Look beyond the end to see if there
405 * is an interesting line after this
406 * hunk within context span.
408 unsigned long la
; /* lookahead */
410 la
= adjust_hunk_tail(sline
, all_mask
,
412 la
= (la
+ context
< cnt
+ 1) ?
413 (la
+ context
) : cnt
+ 1;
415 if (sline
[la
].flag
& mark
) {
427 /* [i..hunk_end) are interesting. Now is it really
428 * interesting? We check if there are only two versions
429 * and the result matches one of them. That is, we look
431 * (+) line, which records lines added to which parents;
432 * this line appears in the result.
433 * (-) line, which records from what parents the line
434 * was removed; this line does not appear in the result.
435 * then check the set of parents the result has difference
436 * from, from all lines. If there are lines that has
437 * different set of parents that the result has differences
438 * from, that means we have more than two versions.
440 * Even when we have only two versions, if the result does
441 * not match any of the parents, the it should be considered
442 * interesting. In such a case, we would have all '+' line.
443 * After passing the above "two versions" test, that would
444 * appear as "the same set of parents" to be "all parents".
448 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
449 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
450 struct lline
*ll
= sline
[j
].lost_head
;
452 /* This has some changes. Is it the
456 same_diff
= this_diff
;
457 else if (same_diff
!= this_diff
) {
462 while (ll
&& !has_interesting
) {
463 /* Lost this line from these parents;
464 * who are they? Are they the same?
466 this_diff
= ll
->parent_map
;
468 same_diff
= this_diff
;
469 else if (same_diff
!= this_diff
) {
476 if (!has_interesting
&& same_diff
!= all_mask
) {
477 /* This hunk is not that interesting after all */
478 for (j
= hunk_begin
; j
< hunk_end
; j
++)
479 sline
[j
].flag
&= ~mark
;
484 has_interesting
= give_context(sline
, cnt
, num_parent
);
485 return has_interesting
;
488 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, int n
, unsigned long null_context
)
490 l0
= sline
[l0
].p_lno
[n
];
491 l1
= sline
[l1
].p_lno
[n
];
492 printf(" -%lu,%lu", l0
, l1
-l0
-null_context
);
495 static int hunk_comment_line(const char *bol
)
502 return (isalpha(ch
) || ch
== '_' || ch
== '$');
505 static void show_line_to_eol(const char *line
, int len
, const char *reset
)
507 int saw_cr_at_eol
= 0;
510 saw_cr_at_eol
= (len
&& line
[len
-1] == '\r');
512 printf("%.*s%s%s\n", len
- saw_cr_at_eol
, line
,
514 saw_cr_at_eol
? "\r" : "");
517 static void dump_sline(struct sline
*sline
, unsigned long cnt
, int num_parent
,
520 unsigned long mark
= (1UL<<num_parent
);
521 unsigned long no_pre_delete
= (2UL<<num_parent
);
523 unsigned long lno
= 0;
524 const char *c_frag
= diff_get_color(use_color
, DIFF_FRAGINFO
);
525 const char *c_new
= diff_get_color(use_color
, DIFF_FILE_NEW
);
526 const char *c_old
= diff_get_color(use_color
, DIFF_FILE_OLD
);
527 const char *c_plain
= diff_get_color(use_color
, DIFF_PLAIN
);
528 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
531 return; /* result deleted */
534 unsigned long hunk_end
;
535 unsigned long rlines
;
536 const char *hunk_comment
= NULL
;
537 unsigned long null_context
= 0;
539 while (lno
<= cnt
&& !(sline
[lno
].flag
& mark
)) {
540 if (hunk_comment_line(sline
[lno
].bol
))
541 hunk_comment
= sline
[lno
].bol
;
547 for (hunk_end
= lno
+ 1; hunk_end
<= cnt
; hunk_end
++)
548 if (!(sline
[hunk_end
].flag
& mark
))
551 rlines
= hunk_end
- lno
;
553 rlines
--; /* pointing at the last delete hunk */
557 * Even when running with --unified=0, all
558 * lines in the hunk needs to be processed in
559 * the loop below in order to show the
560 * deletion recorded in lost_head. However,
561 * we do not want to show the resulting line
562 * with all blank context markers in such a
566 for (j
= lno
; j
< hunk_end
; j
++)
567 if (!(sline
[j
].flag
& (mark
-1)))
569 rlines
-= null_context
;
572 fputs(c_frag
, stdout
);
573 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
574 for (i
= 0; i
< num_parent
; i
++)
575 show_parent_lno(sline
, lno
, hunk_end
, i
, null_context
);
576 printf(" +%lu,%lu ", lno
+1, rlines
);
577 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
581 for (i
= 0; i
< 40; i
++) {
582 int ch
= hunk_comment
[i
] & 0xff;
583 if (!ch
|| ch
== '\n')
590 for (i
= 0; i
< comment_end
; i
++)
591 putchar(hunk_comment
[i
]);
594 printf("%s\n", c_reset
);
595 while (lno
< hunk_end
) {
598 unsigned long p_mask
;
599 struct sline
*sl
= &sline
[lno
++];
600 ll
= (sl
->flag
& no_pre_delete
) ? NULL
: sl
->lost_head
;
602 fputs(c_old
, stdout
);
603 for (j
= 0; j
< num_parent
; j
++) {
604 if (ll
->parent_map
& (1UL<<j
))
609 show_line_to_eol(ll
->line
, -1, c_reset
);
615 if (!(sl
->flag
& (mark
-1))) {
617 * This sline was here to hang the
618 * lost lines in front of it.
622 fputs(c_plain
, stdout
);
625 fputs(c_new
, stdout
);
626 for (j
= 0; j
< num_parent
; j
++) {
627 if (p_mask
& sl
->flag
)
633 show_line_to_eol(sl
->bol
, sl
->len
, c_reset
);
638 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
641 /* We have already examined parent j and we know parent i
642 * and parent j are the same, so reuse the combined result
643 * of parent j for parent i.
645 unsigned long lno
, imask
, jmask
;
649 for (lno
= 0; lno
<= cnt
; lno
++) {
650 struct lline
*ll
= sline
->lost_head
;
651 sline
->p_lno
[i
] = sline
->p_lno
[j
];
653 if (ll
->parent_map
& jmask
)
654 ll
->parent_map
|= imask
;
657 if (sline
->flag
& jmask
)
658 sline
->flag
|= imask
;
661 /* the overall size of the file (sline[cnt]) */
662 sline
->p_lno
[i
] = sline
->p_lno
[j
];
665 static void dump_quoted_path(const char *head
,
668 const char *c_meta
, const char *c_reset
)
670 static struct strbuf buf
= STRBUF_INIT
;
673 strbuf_addstr(&buf
, c_meta
);
674 strbuf_addstr(&buf
, head
);
675 quote_two_c_style(&buf
, prefix
, path
, 0);
676 strbuf_addstr(&buf
, c_reset
);
680 static void show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
681 int dense
, struct rev_info
*rev
)
683 struct diff_options
*opt
= &rev
->diffopt
;
684 unsigned long result_size
, cnt
, lno
;
686 struct sline
*sline
; /* survived lines */
687 int mode_differs
= 0;
689 int working_tree_file
= is_null_sha1(elem
->sha1
);
690 int abbrev
= DIFF_OPT_TST(opt
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
691 const char *a_prefix
, *b_prefix
;
692 mmfile_t result_file
;
694 context
= opt
->context
;
695 a_prefix
= opt
->a_prefix
? opt
->a_prefix
: "a/";
696 b_prefix
= opt
->b_prefix
? opt
->b_prefix
: "b/";
698 /* Read the result of merge first */
699 if (!working_tree_file
)
700 result
= grab_blob(elem
->sha1
, elem
->mode
, &result_size
);
702 /* Used by diff-tree to read from the working tree */
706 if (lstat(elem
->path
, &st
) < 0)
709 if (S_ISLNK(st
.st_mode
)) {
710 struct strbuf buf
= STRBUF_INIT
;
712 if (strbuf_readlink(&buf
, elem
->path
, st
.st_size
) < 0) {
713 error("readlink(%s): %s", elem
->path
,
717 result_size
= buf
.len
;
718 result
= strbuf_detach(&buf
, NULL
);
719 elem
->mode
= canon_mode(st
.st_mode
);
720 } else if (S_ISDIR(st
.st_mode
)) {
721 unsigned char sha1
[20];
722 if (resolve_gitlink_ref(elem
->path
, "HEAD", sha1
) < 0)
723 result
= grab_blob(elem
->sha1
, elem
->mode
, &result_size
);
725 result
= grab_blob(sha1
, elem
->mode
, &result_size
);
726 } else if (0 <= (fd
= open(elem
->path
, O_RDONLY
))) {
727 size_t len
= xsize_t(st
.st_size
);
731 elem
->mode
= canon_mode(st
.st_mode
);
732 /* if symlinks don't work, assume symlink if all parents
735 is_file
= has_symlinks
;
736 for (i
= 0; !is_file
&& i
< num_parent
; i
++)
737 is_file
= !S_ISLNK(elem
->parent
[i
].mode
);
739 elem
->mode
= canon_mode(S_IFLNK
);
742 result
= xmalloc(len
+ 1);
744 done
= read_in_full(fd
, result
, len
);
746 die("read error '%s'", elem
->path
);
748 die("early EOF '%s'", elem
->path
);
752 /* If not a fake symlink, apply filters, e.g. autocrlf */
754 struct strbuf buf
= STRBUF_INIT
;
756 if (convert_to_git(elem
->path
, result
, len
, &buf
, safe_crlf
)) {
758 result
= strbuf_detach(&buf
, &len
);
767 result
= xcalloc(1, 1);
774 for (cnt
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
778 if (result_size
&& result
[result_size
-1] != '\n')
779 cnt
++; /* incomplete line */
781 sline
= xcalloc(cnt
+2, sizeof(*sline
));
782 sline
[0].bol
= result
;
783 for (lno
= 0; lno
<= cnt
+ 1; lno
++) {
784 sline
[lno
].lost_tail
= &sline
[lno
].lost_head
;
787 for (lno
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
789 sline
[lno
].len
= cp
- sline
[lno
].bol
;
792 sline
[lno
].bol
= cp
+ 1;
795 if (result_size
&& result
[result_size
-1] != '\n')
796 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
798 result_file
.ptr
= result
;
799 result_file
.size
= result_size
;
801 /* Even p_lno[cnt+1] is valid -- that is for the end line number
802 * for deletion hunk at the end.
804 sline
[0].p_lno
= xcalloc((cnt
+2) * num_parent
, sizeof(unsigned long));
805 for (lno
= 0; lno
<= cnt
; lno
++)
806 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
808 for (i
= 0; i
< num_parent
; i
++) {
810 for (j
= 0; j
< i
; j
++) {
811 if (!hashcmp(elem
->parent
[i
].sha1
,
812 elem
->parent
[j
].sha1
)) {
813 reuse_combine_diff(sline
, cnt
, i
, j
);
818 combine_diff(elem
->parent
[i
].sha1
,
819 elem
->parent
[i
].mode
,
822 if (elem
->parent
[i
].mode
!= elem
->mode
)
826 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
828 if (show_hunks
|| mode_differs
|| working_tree_file
) {
830 int use_color
= DIFF_OPT_TST(opt
, COLOR_DIFF
);
831 const char *c_meta
= diff_get_color(use_color
, DIFF_METAINFO
);
832 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
836 if (rev
->loginfo
&& !rev
->no_commit_id
)
838 dump_quoted_path(dense
? "diff --cc " : "diff --combined ",
839 "", elem
->path
, c_meta
, c_reset
);
840 printf("%sindex ", c_meta
);
841 for (i
= 0; i
< num_parent
; i
++) {
842 abb
= find_unique_abbrev(elem
->parent
[i
].sha1
,
844 printf("%s%s", i
? "," : "", abb
);
846 abb
= find_unique_abbrev(elem
->sha1
, abbrev
);
847 printf("..%s%s\n", abb
, c_reset
);
850 deleted
= !elem
->mode
;
852 /* We say it was added if nobody had it */
854 for (i
= 0; added
&& i
< num_parent
; i
++)
855 if (elem
->parent
[i
].status
!=
859 printf("%snew file mode %06o",
863 printf("%sdeleted file ", c_meta
);
865 for (i
= 0; i
< num_parent
; i
++) {
866 printf("%s%06o", i
? "," : "",
867 elem
->parent
[i
].mode
);
870 printf("..%06o", elem
->mode
);
872 printf("%s\n", c_reset
);
875 dump_quoted_path("--- ", "", "/dev/null",
878 dump_quoted_path("--- ", a_prefix
, elem
->path
,
881 dump_quoted_path("+++ ", "", "/dev/null",
884 dump_quoted_path("+++ ", b_prefix
, elem
->path
,
886 dump_sline(sline
, cnt
, num_parent
,
887 DIFF_OPT_TST(opt
, COLOR_DIFF
));
891 for (lno
= 0; lno
< cnt
; lno
++) {
892 if (sline
[lno
].lost_head
) {
893 struct lline
*ll
= sline
[lno
].lost_head
;
895 struct lline
*tmp
= ll
;
901 free(sline
[0].p_lno
);
905 #define COLONS "::::::::::::::::::::::::::::::::"
907 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, struct rev_info
*rev
)
909 struct diff_options
*opt
= &rev
->diffopt
;
912 int line_termination
, inter_name_termination
;
914 line_termination
= opt
->line_termination
;
915 inter_name_termination
= '\t';
916 if (!line_termination
)
917 inter_name_termination
= 0;
919 if (rev
->loginfo
&& !rev
->no_commit_id
)
922 if (opt
->output_format
& DIFF_FORMAT_RAW
) {
923 offset
= strlen(COLONS
) - num_parent
;
926 prefix
= COLONS
+ offset
;
929 for (i
= 0; i
< num_parent
; i
++) {
930 printf("%s%06o", prefix
, p
->parent
[i
].mode
);
933 printf("%s%06o", prefix
, p
->mode
);
936 for (i
= 0; i
< num_parent
; i
++)
937 printf(" %s", diff_unique_abbrev(p
->parent
[i
].sha1
,
939 printf(" %s ", diff_unique_abbrev(p
->sha1
, opt
->abbrev
));
942 if (opt
->output_format
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
)) {
943 for (i
= 0; i
< num_parent
; i
++)
944 putchar(p
->parent
[i
].status
);
945 putchar(inter_name_termination
);
948 write_name_quoted(p
->path
, stdout
, line_termination
);
951 void show_combined_diff(struct combine_diff_path
*p
,
954 struct rev_info
*rev
)
956 struct diff_options
*opt
= &rev
->diffopt
;
959 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
961 DIFF_FORMAT_NAME_STATUS
))
962 show_raw_diff(p
, num_parent
, rev
);
963 else if (opt
->output_format
& DIFF_FORMAT_PATCH
)
964 show_patch_diff(p
, num_parent
, dense
, rev
);
967 void diff_tree_combined(const unsigned char *sha1
,
968 const unsigned char parent
[][20],
971 struct rev_info
*rev
)
973 struct diff_options
*opt
= &rev
->diffopt
;
974 struct diff_options diffopts
;
975 struct combine_diff_path
*p
, *paths
= NULL
;
976 int i
, num_paths
, needsep
, show_log_first
;
979 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
980 DIFF_OPT_SET(&diffopts
, RECURSIVE
);
981 DIFF_OPT_CLR(&diffopts
, ALLOW_EXTERNAL
);
983 show_log_first
= !!rev
->loginfo
&& !rev
->no_commit_id
;
985 /* find set of paths that everybody touches */
986 for (i
= 0; i
< num_parent
; i
++) {
987 /* show stat against the first parent even
988 * when doing combined diff.
990 int stat_opt
= (opt
->output_format
&
991 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
));
992 if (i
== 0 && stat_opt
)
993 diffopts
.output_format
= stat_opt
;
995 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
996 diff_tree_sha1(parent
[i
], sha1
, "", &diffopts
);
997 diffcore_std(&diffopts
);
998 paths
= intersect_paths(paths
, i
, num_parent
);
1000 if (show_log_first
&& i
== 0) {
1002 if (rev
->verbose_header
&& opt
->output_format
)
1003 putchar(opt
->line_termination
);
1005 diff_flush(&diffopts
);
1008 /* find out surviving paths */
1009 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
1014 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1016 DIFF_FORMAT_NAME_STATUS
)) {
1017 for (p
= paths
; p
; p
= p
->next
) {
1019 show_raw_diff(p
, num_parent
, rev
);
1023 else if (opt
->output_format
&
1024 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
))
1026 if (opt
->output_format
& DIFF_FORMAT_PATCH
) {
1028 putchar(opt
->line_termination
);
1029 for (p
= paths
; p
; p
= p
->next
) {
1031 show_patch_diff(p
, num_parent
, dense
,
1037 /* Clean things up */
1039 struct combine_diff_path
*tmp
= paths
;
1040 paths
= paths
->next
;
1045 void diff_tree_combined_merge(const unsigned char *sha1
,
1046 int dense
, struct rev_info
*rev
)
1049 const unsigned char (*parent
)[20];
1050 struct commit
*commit
= lookup_commit(sha1
);
1051 struct commit_list
*parents
;
1054 for (parents
= commit
->parents
, num_parent
= 0;
1056 parents
= parents
->next
, num_parent
++)
1059 parent
= xmalloc(num_parent
* sizeof(*parent
));
1060 for (parents
= commit
->parents
, num_parent
= 0;
1062 parents
= parents
->next
, num_parent
++)
1063 hashcpy((unsigned char *)(parent
+ num_parent
),
1064 parents
->item
->object
.sha1
);
1065 diff_tree_combined(sha1
, parent
, num_parent
, dense
, rev
);