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
;
85 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
86 * we did not change it).
87 * bit N is used for "interesting" lines, including context.
88 * bit (N+1) is used for "do not show deletion before this".
94 static char *grab_blob(const unsigned char *sha1
, unsigned int mode
, unsigned long *size
)
97 enum object_type type
;
99 if (S_ISGITLINK(mode
)) {
101 *size
= snprintf(blob
, 100,
102 "Subproject commit %s\n", sha1_to_hex(sha1
));
103 } else if (is_null_sha1(sha1
)) {
106 return xcalloc(1, 1);
108 blob
= read_sha1_file(sha1
, &type
, size
);
109 if (type
!= OBJ_BLOB
)
110 die("object '%s' is not a blob!", sha1_to_hex(sha1
));
115 static void append_lost(struct sline
*sline
, int n
, const char *line
, int len
)
118 unsigned long this_mask
= (1UL<<n
);
119 if (line
[len
-1] == '\n')
122 /* Check to see if we can squash things */
123 if (sline
->lost_head
) {
124 struct lline
*last_one
= NULL
;
125 /* We cannot squash it with earlier one */
126 for (lline
= sline
->lost_head
;
129 if (lline
->parent_map
& this_mask
)
131 lline
= last_one
? last_one
->next
: sline
->lost_head
;
133 if (lline
->len
== len
&&
134 !memcmp(lline
->line
, line
, len
)) {
135 lline
->parent_map
|= this_mask
;
142 lline
= xmalloc(sizeof(*lline
) + len
+ 1);
145 lline
->parent_map
= this_mask
;
146 memcpy(lline
->line
, line
, len
);
147 lline
->line
[len
] = 0;
148 *sline
->lost_tail
= lline
;
149 sline
->lost_tail
= &lline
->next
;
152 struct combine_diff_state
{
159 struct sline
*lost_bucket
;
162 static void consume_line(void *state_
, char *line
, unsigned long len
)
164 struct combine_diff_state
*state
= state_
;
165 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
166 if (parse_hunk_header(line
, len
,
167 &state
->ob
, &state
->on
,
168 &state
->nb
, &state
->nn
))
170 state
->lno
= state
->nb
;
172 /* @@ -1,2 +0,0 @@ to remove the
177 /* @@ -X,Y +N,0 @@ removed Y lines
178 * that would have come *after* line N
179 * in the result. Our lost buckets hang
180 * to the line after the removed lines,
182 state
->lost_bucket
= &state
->sline
[state
->nb
];
184 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
185 if (!state
->sline
[state
->nb
-1].p_lno
)
186 state
->sline
[state
->nb
-1].p_lno
=
187 xcalloc(state
->num_parent
,
188 sizeof(unsigned long));
189 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
192 if (!state
->lost_bucket
)
193 return; /* not in any hunk yet */
196 append_lost(state
->lost_bucket
, state
->n
, line
+1, len
-1);
199 state
->sline
[state
->lno
-1].flag
|= state
->nmask
;
205 static void combine_diff(const unsigned char *parent
, unsigned int mode
,
206 mmfile_t
*result_file
,
207 struct sline
*sline
, unsigned int cnt
, int n
,
210 unsigned int p_lno
, lno
;
211 unsigned long nmask
= (1UL << n
);
214 mmfile_t parent_file
;
216 struct combine_diff_state state
;
220 return; /* result deleted */
222 parent_file
.ptr
= grab_blob(parent
, mode
, &sz
);
223 parent_file
.size
= sz
;
224 memset(&xpp
, 0, sizeof(xpp
));
225 xpp
.flags
= XDF_NEED_MINIMAL
;
226 memset(&xecfg
, 0, sizeof(xecfg
));
227 memset(&state
, 0, sizeof(state
));
231 state
.num_parent
= num_parent
;
234 xdi_diff_outf(&parent_file
, result_file
, consume_line
, &state
,
236 free(parent_file
.ptr
);
238 /* Assign line numbers for this parent.
240 * sline[lno].p_lno[n] records the first line number
241 * (counting from 1) for parent N if the final hunk display
242 * started by showing sline[lno] (possibly showing the lost
243 * lines attached to it first).
245 for (lno
= 0, p_lno
= 1; lno
<= cnt
; lno
++) {
247 sline
[lno
].p_lno
[n
] = p_lno
;
249 /* How many lines would this sline advance the p_lno? */
250 ll
= sline
[lno
].lost_head
;
252 if (ll
->parent_map
& nmask
)
253 p_lno
++; /* '-' means parent had it */
256 if (lno
< cnt
&& !(sline
[lno
].flag
& nmask
))
257 p_lno
++; /* no '+' means parent had it */
259 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
262 static unsigned long context
= 3;
263 static char combine_marker
= '@';
265 static int interesting(struct sline
*sline
, unsigned long all_mask
)
267 /* If some parents lost lines here, or if we have added to
268 * some parent, it is interesting.
270 return ((sline
->flag
& all_mask
) || sline
->lost_head
);
273 static unsigned long adjust_hunk_tail(struct sline
*sline
,
274 unsigned long all_mask
,
275 unsigned long hunk_begin
,
278 /* i points at the first uninteresting line. If the last line
279 * of the hunk was interesting only because it has some
280 * deletion, then it is not all that interesting for the
281 * purpose of giving trailing context lines. This is because
282 * we output '-' line and then unmodified sline[i-1] itself in
283 * that case which gives us one extra context line.
285 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
290 static unsigned long find_next(struct sline
*sline
,
294 int look_for_uninteresting
)
296 /* We have examined up to i-1 and are about to look at i.
297 * Find next interesting or uninteresting line. Here,
298 * "interesting" does not mean interesting(), but marked by
299 * the give_context() function below (i.e. it includes context
300 * lines that are not interesting to interesting() function
301 * that are surrounded by interesting() ones.
304 if (look_for_uninteresting
305 ? !(sline
[i
].flag
& mark
)
306 : (sline
[i
].flag
& mark
))
313 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
315 unsigned long all_mask
= (1UL<<num_parent
) - 1;
316 unsigned long mark
= (1UL<<num_parent
);
317 unsigned long no_pre_delete
= (2UL<<num_parent
);
320 /* Two groups of interesting lines may have a short gap of
321 * uninteresting lines. Connect such groups to give them a
324 * We first start from what the interesting() function says,
325 * and mark them with "mark", and paint context lines with the
326 * mark. So interesting() would still say false for such context
327 * lines but they are treated as "interesting" in the end.
329 i
= find_next(sline
, mark
, 0, cnt
, 0);
334 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
337 /* Paint a few lines before the first interesting line. */
339 sline
[j
++].flag
|= mark
| no_pre_delete
;
342 /* we know up to i is to be included. where does the
343 * next uninteresting one start?
345 j
= find_next(sline
, mark
, i
, cnt
, 1);
347 break; /* the rest are all interesting */
349 /* lookahead context lines */
350 k
= find_next(sline
, mark
, j
, cnt
, 0);
351 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
353 if (k
< j
+ context
) {
354 /* k is interesting and [j,k) are not, but
355 * paint them interesting because the gap is small.
358 sline
[j
++].flag
|= mark
;
363 /* j is the first uninteresting line and there is
364 * no overlap beyond it within context lines. Paint
365 * the trailing edge a bit.
368 k
= (j
+ context
< cnt
+1) ? j
+ context
: cnt
+1;
370 sline
[j
++].flag
|= mark
;
375 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
376 int num_parent
, int dense
)
378 unsigned long all_mask
= (1UL<<num_parent
) - 1;
379 unsigned long mark
= (1UL<<num_parent
);
381 int has_interesting
= 0;
383 for (i
= 0; i
<= cnt
; i
++) {
384 if (interesting(&sline
[i
], all_mask
))
385 sline
[i
].flag
|= mark
;
387 sline
[i
].flag
&= ~mark
;
390 return give_context(sline
, cnt
, num_parent
);
392 /* Look at each hunk, and if we have changes from only one
393 * parent, or the changes are the same from all but one
394 * parent, mark that uninteresting.
398 unsigned long j
, hunk_begin
, hunk_end
;
399 unsigned long same_diff
;
400 while (i
<= cnt
&& !(sline
[i
].flag
& mark
))
403 break; /* No more interesting hunks */
405 for (j
= i
+ 1; j
<= cnt
; j
++) {
406 if (!(sline
[j
].flag
& mark
)) {
407 /* Look beyond the end to see if there
408 * is an interesting line after this
409 * hunk within context span.
411 unsigned long la
; /* lookahead */
413 la
= adjust_hunk_tail(sline
, all_mask
,
415 la
= (la
+ context
< cnt
+ 1) ?
416 (la
+ context
) : cnt
+ 1;
418 if (sline
[la
].flag
& mark
) {
430 /* [i..hunk_end) are interesting. Now is it really
431 * interesting? We check if there are only two versions
432 * and the result matches one of them. That is, we look
434 * (+) line, which records lines added to which parents;
435 * this line appears in the result.
436 * (-) line, which records from what parents the line
437 * was removed; this line does not appear in the result.
438 * then check the set of parents the result has difference
439 * from, from all lines. If there are lines that has
440 * different set of parents that the result has differences
441 * from, that means we have more than two versions.
443 * Even when we have only two versions, if the result does
444 * not match any of the parents, the it should be considered
445 * interesting. In such a case, we would have all '+' line.
446 * After passing the above "two versions" test, that would
447 * appear as "the same set of parents" to be "all parents".
451 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
452 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
453 struct lline
*ll
= sline
[j
].lost_head
;
455 /* This has some changes. Is it the
459 same_diff
= this_diff
;
460 else if (same_diff
!= this_diff
) {
465 while (ll
&& !has_interesting
) {
466 /* Lost this line from these parents;
467 * who are they? Are they the same?
469 this_diff
= ll
->parent_map
;
471 same_diff
= this_diff
;
472 else if (same_diff
!= this_diff
) {
479 if (!has_interesting
&& same_diff
!= all_mask
) {
480 /* This hunk is not that interesting after all */
481 for (j
= hunk_begin
; j
< hunk_end
; j
++)
482 sline
[j
].flag
&= ~mark
;
487 has_interesting
= give_context(sline
, cnt
, num_parent
);
488 return has_interesting
;
491 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, int n
, unsigned long null_context
)
493 l0
= sline
[l0
].p_lno
[n
];
494 l1
= sline
[l1
].p_lno
[n
];
495 printf(" -%lu,%lu", l0
, l1
-l0
-null_context
);
498 static int hunk_comment_line(const char *bol
)
505 return (isalpha(ch
) || ch
== '_' || ch
== '$');
508 static void show_line_to_eol(const char *line
, int len
, const char *reset
)
510 int saw_cr_at_eol
= 0;
513 saw_cr_at_eol
= (len
&& line
[len
-1] == '\r');
515 printf("%.*s%s%s\n", len
- saw_cr_at_eol
, line
,
517 saw_cr_at_eol
? "\r" : "");
520 static void dump_sline(struct sline
*sline
, unsigned long cnt
, int num_parent
,
523 unsigned long mark
= (1UL<<num_parent
);
524 unsigned long no_pre_delete
= (2UL<<num_parent
);
526 unsigned long lno
= 0;
527 const char *c_frag
= diff_get_color(use_color
, DIFF_FRAGINFO
);
528 const char *c_new
= diff_get_color(use_color
, DIFF_FILE_NEW
);
529 const char *c_old
= diff_get_color(use_color
, DIFF_FILE_OLD
);
530 const char *c_plain
= diff_get_color(use_color
, DIFF_PLAIN
);
531 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
534 return; /* result deleted */
537 unsigned long hunk_end
;
538 unsigned long rlines
;
539 const char *hunk_comment
= NULL
;
540 unsigned long null_context
= 0;
542 while (lno
<= cnt
&& !(sline
[lno
].flag
& mark
)) {
543 if (hunk_comment_line(sline
[lno
].bol
))
544 hunk_comment
= sline
[lno
].bol
;
550 for (hunk_end
= lno
+ 1; hunk_end
<= cnt
; hunk_end
++)
551 if (!(sline
[hunk_end
].flag
& mark
))
554 rlines
= hunk_end
- lno
;
556 rlines
--; /* pointing at the last delete hunk */
560 * Even when running with --unified=0, all
561 * lines in the hunk needs to be processed in
562 * the loop below in order to show the
563 * deletion recorded in lost_head. However,
564 * we do not want to show the resulting line
565 * with all blank context markers in such a
569 for (j
= lno
; j
< hunk_end
; j
++)
570 if (!(sline
[j
].flag
& (mark
-1)))
572 rlines
-= null_context
;
575 fputs(c_frag
, stdout
);
576 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
577 for (i
= 0; i
< num_parent
; i
++)
578 show_parent_lno(sline
, lno
, hunk_end
, i
, null_context
);
579 printf(" +%lu,%lu ", lno
+1, rlines
);
580 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
584 for (i
= 0; i
< 40; i
++) {
585 int ch
= hunk_comment
[i
] & 0xff;
586 if (!ch
|| ch
== '\n')
593 for (i
= 0; i
< comment_end
; i
++)
594 putchar(hunk_comment
[i
]);
597 printf("%s\n", c_reset
);
598 while (lno
< hunk_end
) {
601 unsigned long p_mask
;
602 struct sline
*sl
= &sline
[lno
++];
603 ll
= (sl
->flag
& no_pre_delete
) ? NULL
: sl
->lost_head
;
605 fputs(c_old
, stdout
);
606 for (j
= 0; j
< num_parent
; j
++) {
607 if (ll
->parent_map
& (1UL<<j
))
612 show_line_to_eol(ll
->line
, -1, c_reset
);
618 if (!(sl
->flag
& (mark
-1))) {
620 * This sline was here to hang the
621 * lost lines in front of it.
625 fputs(c_plain
, stdout
);
628 fputs(c_new
, stdout
);
629 for (j
= 0; j
< num_parent
; j
++) {
630 if (p_mask
& sl
->flag
)
636 show_line_to_eol(sl
->bol
, sl
->len
, c_reset
);
641 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
644 /* We have already examined parent j and we know parent i
645 * and parent j are the same, so reuse the combined result
646 * of parent j for parent i.
648 unsigned long lno
, imask
, jmask
;
652 for (lno
= 0; lno
<= cnt
; lno
++) {
653 struct lline
*ll
= sline
->lost_head
;
654 sline
->p_lno
[i
] = sline
->p_lno
[j
];
656 if (ll
->parent_map
& jmask
)
657 ll
->parent_map
|= imask
;
660 if (sline
->flag
& jmask
)
661 sline
->flag
|= imask
;
664 /* the overall size of the file (sline[cnt]) */
665 sline
->p_lno
[i
] = sline
->p_lno
[j
];
668 static void dump_quoted_path(const char *head
,
671 const char *c_meta
, const char *c_reset
)
673 static struct strbuf buf
= STRBUF_INIT
;
676 strbuf_addstr(&buf
, c_meta
);
677 strbuf_addstr(&buf
, head
);
678 quote_two_c_style(&buf
, prefix
, path
, 0);
679 strbuf_addstr(&buf
, c_reset
);
683 static void show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
684 int dense
, struct rev_info
*rev
)
686 struct diff_options
*opt
= &rev
->diffopt
;
687 unsigned long result_size
, cnt
, lno
;
689 struct sline
*sline
; /* survived lines */
690 int mode_differs
= 0;
692 int working_tree_file
= is_null_sha1(elem
->sha1
);
693 int abbrev
= DIFF_OPT_TST(opt
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
694 const char *a_prefix
, *b_prefix
;
695 mmfile_t result_file
;
697 context
= opt
->context
;
698 a_prefix
= opt
->a_prefix
? opt
->a_prefix
: "a/";
699 b_prefix
= opt
->b_prefix
? opt
->b_prefix
: "b/";
701 /* Read the result of merge first */
702 if (!working_tree_file
)
703 result
= grab_blob(elem
->sha1
, elem
->mode
, &result_size
);
705 /* Used by diff-tree to read from the working tree */
709 if (lstat(elem
->path
, &st
) < 0)
712 if (S_ISLNK(st
.st_mode
)) {
713 struct strbuf buf
= STRBUF_INIT
;
715 if (strbuf_readlink(&buf
, elem
->path
, st
.st_size
) < 0) {
716 error("readlink(%s): %s", elem
->path
,
720 result_size
= buf
.len
;
721 result
= strbuf_detach(&buf
, NULL
);
722 elem
->mode
= canon_mode(st
.st_mode
);
723 } else if (S_ISDIR(st
.st_mode
)) {
724 unsigned char sha1
[20];
725 if (resolve_gitlink_ref(elem
->path
, "HEAD", sha1
) < 0)
726 result
= grab_blob(elem
->sha1
, elem
->mode
, &result_size
);
728 result
= grab_blob(sha1
, elem
->mode
, &result_size
);
729 } else if (0 <= (fd
= open(elem
->path
, O_RDONLY
))) {
730 size_t len
= xsize_t(st
.st_size
);
734 elem
->mode
= canon_mode(st
.st_mode
);
735 /* if symlinks don't work, assume symlink if all parents
738 is_file
= has_symlinks
;
739 for (i
= 0; !is_file
&& i
< num_parent
; i
++)
740 is_file
= !S_ISLNK(elem
->parent
[i
].mode
);
742 elem
->mode
= canon_mode(S_IFLNK
);
745 result
= xmalloc(len
+ 1);
747 done
= read_in_full(fd
, result
, len
);
749 die("read error '%s'", elem
->path
);
751 die("early EOF '%s'", elem
->path
);
755 /* If not a fake symlink, apply filters, e.g. autocrlf */
757 struct strbuf buf
= STRBUF_INIT
;
759 if (convert_to_git(elem
->path
, result
, len
, &buf
, safe_crlf
)) {
761 result
= strbuf_detach(&buf
, &len
);
770 result
= xcalloc(1, 1);
777 for (cnt
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
781 if (result_size
&& result
[result_size
-1] != '\n')
782 cnt
++; /* incomplete line */
784 sline
= xcalloc(cnt
+2, sizeof(*sline
));
785 sline
[0].bol
= result
;
786 for (lno
= 0; lno
<= cnt
+ 1; lno
++) {
787 sline
[lno
].lost_tail
= &sline
[lno
].lost_head
;
790 for (lno
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
792 sline
[lno
].len
= cp
- sline
[lno
].bol
;
795 sline
[lno
].bol
= cp
+ 1;
798 if (result_size
&& result
[result_size
-1] != '\n')
799 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
801 result_file
.ptr
= result
;
802 result_file
.size
= result_size
;
804 /* Even p_lno[cnt+1] is valid -- that is for the end line number
805 * for deletion hunk at the end.
807 sline
[0].p_lno
= xcalloc((cnt
+2) * num_parent
, sizeof(unsigned long));
808 for (lno
= 0; lno
<= cnt
; lno
++)
809 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
811 for (i
= 0; i
< num_parent
; i
++) {
813 for (j
= 0; j
< i
; j
++) {
814 if (!hashcmp(elem
->parent
[i
].sha1
,
815 elem
->parent
[j
].sha1
)) {
816 reuse_combine_diff(sline
, cnt
, i
, j
);
821 combine_diff(elem
->parent
[i
].sha1
,
822 elem
->parent
[i
].mode
,
825 if (elem
->parent
[i
].mode
!= elem
->mode
)
829 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
831 if (show_hunks
|| mode_differs
|| working_tree_file
) {
833 int use_color
= DIFF_OPT_TST(opt
, COLOR_DIFF
);
834 const char *c_meta
= diff_get_color(use_color
, DIFF_METAINFO
);
835 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
839 if (rev
->loginfo
&& !rev
->no_commit_id
)
841 dump_quoted_path(dense
? "diff --cc " : "diff --combined ",
842 "", elem
->path
, c_meta
, c_reset
);
843 printf("%sindex ", c_meta
);
844 for (i
= 0; i
< num_parent
; i
++) {
845 abb
= find_unique_abbrev(elem
->parent
[i
].sha1
,
847 printf("%s%s", i
? "," : "", abb
);
849 abb
= find_unique_abbrev(elem
->sha1
, abbrev
);
850 printf("..%s%s\n", abb
, c_reset
);
853 deleted
= !elem
->mode
;
855 /* We say it was added if nobody had it */
857 for (i
= 0; added
&& i
< num_parent
; i
++)
858 if (elem
->parent
[i
].status
!=
862 printf("%snew file mode %06o",
866 printf("%sdeleted file ", c_meta
);
868 for (i
= 0; i
< num_parent
; i
++) {
869 printf("%s%06o", i
? "," : "",
870 elem
->parent
[i
].mode
);
873 printf("..%06o", elem
->mode
);
875 printf("%s\n", c_reset
);
878 dump_quoted_path("--- ", "", "/dev/null",
881 dump_quoted_path("--- ", a_prefix
, elem
->path
,
884 dump_quoted_path("+++ ", "", "/dev/null",
887 dump_quoted_path("+++ ", b_prefix
, elem
->path
,
889 dump_sline(sline
, cnt
, num_parent
,
890 DIFF_OPT_TST(opt
, COLOR_DIFF
));
894 for (lno
= 0; lno
< cnt
; lno
++) {
895 if (sline
[lno
].lost_head
) {
896 struct lline
*ll
= sline
[lno
].lost_head
;
898 struct lline
*tmp
= ll
;
904 free(sline
[0].p_lno
);
908 #define COLONS "::::::::::::::::::::::::::::::::"
910 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, struct rev_info
*rev
)
912 struct diff_options
*opt
= &rev
->diffopt
;
915 int line_termination
, inter_name_termination
;
917 line_termination
= opt
->line_termination
;
918 inter_name_termination
= '\t';
919 if (!line_termination
)
920 inter_name_termination
= 0;
922 if (rev
->loginfo
&& !rev
->no_commit_id
)
925 if (opt
->output_format
& DIFF_FORMAT_RAW
) {
926 offset
= strlen(COLONS
) - num_parent
;
929 prefix
= COLONS
+ offset
;
932 for (i
= 0; i
< num_parent
; i
++) {
933 printf("%s%06o", prefix
, p
->parent
[i
].mode
);
936 printf("%s%06o", prefix
, p
->mode
);
939 for (i
= 0; i
< num_parent
; i
++)
940 printf(" %s", diff_unique_abbrev(p
->parent
[i
].sha1
,
942 printf(" %s ", diff_unique_abbrev(p
->sha1
, opt
->abbrev
));
945 if (opt
->output_format
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
)) {
946 for (i
= 0; i
< num_parent
; i
++)
947 putchar(p
->parent
[i
].status
);
948 putchar(inter_name_termination
);
951 write_name_quoted(p
->path
, stdout
, line_termination
);
954 void show_combined_diff(struct combine_diff_path
*p
,
957 struct rev_info
*rev
)
959 struct diff_options
*opt
= &rev
->diffopt
;
962 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
964 DIFF_FORMAT_NAME_STATUS
))
965 show_raw_diff(p
, num_parent
, rev
);
966 else if (opt
->output_format
& DIFF_FORMAT_PATCH
)
967 show_patch_diff(p
, num_parent
, dense
, rev
);
970 void diff_tree_combined(const unsigned char *sha1
,
971 const unsigned char parent
[][20],
974 struct rev_info
*rev
)
976 struct diff_options
*opt
= &rev
->diffopt
;
977 struct diff_options diffopts
;
978 struct combine_diff_path
*p
, *paths
= NULL
;
979 int i
, num_paths
, needsep
, show_log_first
;
982 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
983 DIFF_OPT_SET(&diffopts
, RECURSIVE
);
984 DIFF_OPT_CLR(&diffopts
, ALLOW_EXTERNAL
);
986 show_log_first
= !!rev
->loginfo
&& !rev
->no_commit_id
;
988 /* find set of paths that everybody touches */
989 for (i
= 0; i
< num_parent
; i
++) {
990 /* show stat against the first parent even
991 * when doing combined diff.
993 int stat_opt
= (opt
->output_format
&
994 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
));
995 if (i
== 0 && stat_opt
)
996 diffopts
.output_format
= stat_opt
;
998 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
999 diff_tree_sha1(parent
[i
], sha1
, "", &diffopts
);
1000 diffcore_std(&diffopts
);
1001 paths
= intersect_paths(paths
, i
, num_parent
);
1003 if (show_log_first
&& i
== 0) {
1005 if (rev
->verbose_header
&& opt
->output_format
)
1006 putchar(opt
->line_termination
);
1008 diff_flush(&diffopts
);
1011 /* find out surviving paths */
1012 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
1017 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1019 DIFF_FORMAT_NAME_STATUS
)) {
1020 for (p
= paths
; p
; p
= p
->next
) {
1022 show_raw_diff(p
, num_parent
, rev
);
1026 else if (opt
->output_format
&
1027 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
))
1029 if (opt
->output_format
& DIFF_FORMAT_PATCH
) {
1031 putchar(opt
->line_termination
);
1032 for (p
= paths
; p
; p
= p
->next
) {
1034 show_patch_diff(p
, num_parent
, dense
,
1040 /* Clean things up */
1042 struct combine_diff_path
*tmp
= paths
;
1043 paths
= paths
->next
;
1048 void diff_tree_combined_merge(const unsigned char *sha1
,
1049 int dense
, struct rev_info
*rev
)
1052 const unsigned char (*parent
)[20];
1053 struct commit
*commit
= lookup_commit(sha1
);
1054 struct commit_list
*parents
;
1057 for (parents
= commit
->parents
, num_parent
= 0;
1059 parents
= parents
->next
, num_parent
++)
1062 parent
= xmalloc(num_parent
* sizeof(*parent
));
1063 for (parents
= commit
->parents
, num_parent
= 0;
1065 parents
= parents
->next
, num_parent
++)
1066 hashcpy((unsigned char*)(parent
+ num_parent
),
1067 parents
->item
->object
.sha1
);
1068 diff_tree_combined(sha1
, parent
, num_parent
, dense
, rev
);