7 #include "xdiff-interface.h"
12 static struct combine_diff_path
*intersect_paths(struct combine_diff_path
*curr
, int n
, int num_parent
)
14 struct diff_queue_struct
*q
= &diff_queued_diff
;
15 struct combine_diff_path
*p
;
19 struct combine_diff_path
*list
= NULL
, **tail
= &list
;
20 for (i
= 0; i
< q
->nr
; i
++) {
23 if (diff_unmodified_pair(q
->queue
[i
]))
25 path
= q
->queue
[i
]->two
->path
;
27 p
= xmalloc(combine_diff_path_size(num_parent
, len
));
28 p
->path
= (char *) &(p
->parent
[num_parent
]);
29 memcpy(p
->path
, path
, len
);
34 sizeof(p
->parent
[0]) * num_parent
);
36 hashcpy(p
->sha1
, q
->queue
[i
]->two
->sha1
);
37 p
->mode
= q
->queue
[i
]->two
->mode
;
38 hashcpy(p
->parent
[n
].sha1
, q
->queue
[i
]->one
->sha1
);
39 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
40 p
->parent
[n
].status
= q
->queue
[i
]->status
;
47 for (p
= curr
; p
; p
= p
->next
) {
51 for (i
= 0; i
< q
->nr
; i
++) {
55 if (diff_unmodified_pair(q
->queue
[i
]))
57 path
= q
->queue
[i
]->two
->path
;
59 if (len
== p
->len
&& !memcmp(path
, p
->path
, len
)) {
61 hashcpy(p
->parent
[n
].sha1
, q
->queue
[i
]->one
->sha1
);
62 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
63 p
->parent
[n
].status
= q
->queue
[i
]->status
;
73 /* Lines lost from parent */
77 unsigned long parent_map
;
78 char line
[FLEX_ARRAY
];
81 /* Lines surviving in the merge result */
83 struct lline
*lost_head
, **lost_tail
;
84 struct lline
*next_lost
;
87 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
88 * we did not change it).
89 * bit N is used for "interesting" lines, including context.
90 * bit (N+1) is used for "do not show deletion before this".
96 static char *grab_blob(const unsigned char *sha1
, unsigned int mode
, unsigned long *size
)
99 enum object_type type
;
101 if (S_ISGITLINK(mode
)) {
103 *size
= snprintf(blob
, 100,
104 "Subproject commit %s\n", sha1_to_hex(sha1
));
105 } else if (is_null_sha1(sha1
)) {
108 return xcalloc(1, 1);
110 blob
= read_sha1_file(sha1
, &type
, size
);
111 if (type
!= OBJ_BLOB
)
112 die("object '%s' is not a blob!", sha1_to_hex(sha1
));
117 static void append_lost(struct sline
*sline
, int n
, const char *line
, int len
)
120 unsigned long this_mask
= (1UL<<n
);
121 if (line
[len
-1] == '\n')
124 /* Check to see if we can squash things */
125 if (sline
->lost_head
) {
126 lline
= sline
->next_lost
;
128 if (lline
->len
== len
&&
129 !memcmp(lline
->line
, line
, len
)) {
130 lline
->parent_map
|= this_mask
;
131 sline
->next_lost
= lline
->next
;
138 lline
= xmalloc(sizeof(*lline
) + len
+ 1);
141 lline
->parent_map
= this_mask
;
142 memcpy(lline
->line
, line
, len
);
143 lline
->line
[len
] = 0;
144 *sline
->lost_tail
= lline
;
145 sline
->lost_tail
= &lline
->next
;
146 sline
->next_lost
= NULL
;
149 struct combine_diff_state
{
156 struct sline
*lost_bucket
;
159 static void consume_line(void *state_
, char *line
, unsigned long len
)
161 struct combine_diff_state
*state
= state_
;
162 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
163 if (parse_hunk_header(line
, len
,
164 &state
->ob
, &state
->on
,
165 &state
->nb
, &state
->nn
))
167 state
->lno
= state
->nb
;
168 if (state
->nn
== 0) {
169 /* @@ -X,Y +N,0 @@ removed Y lines
170 * that would have come *after* line N
171 * in the result. Our lost buckets hang
172 * to the line after the removed lines,
174 * Note that this is correct even when N == 0,
175 * in which case the hunk removes the first
178 state
->lost_bucket
= &state
->sline
[state
->nb
];
182 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
184 if (!state
->sline
[state
->nb
-1].p_lno
)
185 state
->sline
[state
->nb
-1].p_lno
=
186 xcalloc(state
->num_parent
,
187 sizeof(unsigned long));
188 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
189 state
->lost_bucket
->next_lost
= state
->lost_bucket
->lost_head
;
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
,
208 int num_parent
, int result_deleted
)
210 unsigned int p_lno
, lno
;
211 unsigned long nmask
= (1UL << n
);
214 mmfile_t parent_file
;
215 struct combine_diff_state state
;
219 return; /* result deleted */
221 parent_file
.ptr
= grab_blob(parent
, mode
, &sz
);
222 parent_file
.size
= sz
;
223 memset(&xpp
, 0, sizeof(xpp
));
225 memset(&xecfg
, 0, sizeof(xecfg
));
226 memset(&state
, 0, sizeof(state
));
230 state
.num_parent
= num_parent
;
233 xdi_diff_outf(&parent_file
, result_file
, consume_line
, &state
,
235 free(parent_file
.ptr
);
237 /* Assign line numbers for this parent.
239 * sline[lno].p_lno[n] records the first line number
240 * (counting from 1) for parent N if the final hunk display
241 * started by showing sline[lno] (possibly showing the lost
242 * lines attached to it first).
244 for (lno
= 0, p_lno
= 1; lno
<= cnt
; lno
++) {
246 sline
[lno
].p_lno
[n
] = p_lno
;
248 /* How many lines would this sline advance the p_lno? */
249 ll
= sline
[lno
].lost_head
;
251 if (ll
->parent_map
& nmask
)
252 p_lno
++; /* '-' means parent had it */
255 if (lno
< cnt
&& !(sline
[lno
].flag
& nmask
))
256 p_lno
++; /* no '+' means parent had it */
258 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
261 static unsigned long context
= 3;
262 static char combine_marker
= '@';
264 static int interesting(struct sline
*sline
, unsigned long all_mask
)
266 /* If some parents lost lines here, or if we have added to
267 * some parent, it is interesting.
269 return ((sline
->flag
& all_mask
) || sline
->lost_head
);
272 static unsigned long adjust_hunk_tail(struct sline
*sline
,
273 unsigned long all_mask
,
274 unsigned long hunk_begin
,
277 /* i points at the first uninteresting line. If the last line
278 * of the hunk was interesting only because it has some
279 * deletion, then it is not all that interesting for the
280 * purpose of giving trailing context lines. This is because
281 * we output '-' line and then unmodified sline[i-1] itself in
282 * that case which gives us one extra context line.
284 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
289 static unsigned long find_next(struct sline
*sline
,
293 int look_for_uninteresting
)
295 /* We have examined up to i-1 and are about to look at i.
296 * Find next interesting or uninteresting line. Here,
297 * "interesting" does not mean interesting(), but marked by
298 * the give_context() function below (i.e. it includes context
299 * lines that are not interesting to interesting() function
300 * that are surrounded by interesting() ones.
303 if (look_for_uninteresting
304 ? !(sline
[i
].flag
& mark
)
305 : (sline
[i
].flag
& mark
))
312 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
314 unsigned long all_mask
= (1UL<<num_parent
) - 1;
315 unsigned long mark
= (1UL<<num_parent
);
316 unsigned long no_pre_delete
= (2UL<<num_parent
);
319 /* Two groups of interesting lines may have a short gap of
320 * uninteresting lines. Connect such groups to give them a
323 * We first start from what the interesting() function says,
324 * and mark them with "mark", and paint context lines with the
325 * mark. So interesting() would still say false for such context
326 * lines but they are treated as "interesting" in the end.
328 i
= find_next(sline
, mark
, 0, cnt
, 0);
333 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
336 /* Paint a few lines before the first interesting line. */
338 sline
[j
++].flag
|= mark
| no_pre_delete
;
341 /* we know up to i is to be included. where does the
342 * next uninteresting one start?
344 j
= find_next(sline
, mark
, i
, cnt
, 1);
346 break; /* the rest are all interesting */
348 /* lookahead context lines */
349 k
= find_next(sline
, mark
, j
, cnt
, 0);
350 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
352 if (k
< j
+ context
) {
353 /* k is interesting and [j,k) are not, but
354 * paint them interesting because the gap is small.
357 sline
[j
++].flag
|= mark
;
362 /* j is the first uninteresting line and there is
363 * no overlap beyond it within context lines. Paint
364 * the trailing edge a bit.
367 k
= (j
+ context
< cnt
+1) ? j
+ context
: cnt
+1;
369 sline
[j
++].flag
|= mark
;
374 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
375 int num_parent
, int dense
)
377 unsigned long all_mask
= (1UL<<num_parent
) - 1;
378 unsigned long mark
= (1UL<<num_parent
);
380 int has_interesting
= 0;
382 for (i
= 0; i
<= cnt
; i
++) {
383 if (interesting(&sline
[i
], all_mask
))
384 sline
[i
].flag
|= mark
;
386 sline
[i
].flag
&= ~mark
;
389 return give_context(sline
, cnt
, num_parent
);
391 /* Look at each hunk, and if we have changes from only one
392 * parent, or the changes are the same from all but one
393 * parent, mark that uninteresting.
397 unsigned long j
, hunk_begin
, hunk_end
;
398 unsigned long same_diff
;
399 while (i
<= cnt
&& !(sline
[i
].flag
& mark
))
402 break; /* No more interesting hunks */
404 for (j
= i
+ 1; j
<= cnt
; j
++) {
405 if (!(sline
[j
].flag
& mark
)) {
406 /* Look beyond the end to see if there
407 * is an interesting line after this
408 * hunk within context span.
410 unsigned long la
; /* lookahead */
412 la
= adjust_hunk_tail(sline
, all_mask
,
414 la
= (la
+ context
< cnt
+ 1) ?
415 (la
+ context
) : cnt
+ 1;
417 if (sline
[la
].flag
& mark
) {
429 /* [i..hunk_end) are interesting. Now is it really
430 * interesting? We check if there are only two versions
431 * and the result matches one of them. That is, we look
433 * (+) line, which records lines added to which parents;
434 * this line appears in the result.
435 * (-) line, which records from what parents the line
436 * was removed; this line does not appear in the result.
437 * then check the set of parents the result has difference
438 * from, from all lines. If there are lines that has
439 * different set of parents that the result has differences
440 * from, that means we have more than two versions.
442 * Even when we have only two versions, if the result does
443 * not match any of the parents, the it should be considered
444 * interesting. In such a case, we would have all '+' line.
445 * After passing the above "two versions" test, that would
446 * appear as "the same set of parents" to be "all parents".
450 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
451 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
452 struct lline
*ll
= sline
[j
].lost_head
;
454 /* This has some changes. Is it the
458 same_diff
= this_diff
;
459 else if (same_diff
!= this_diff
) {
464 while (ll
&& !has_interesting
) {
465 /* Lost this line from these parents;
466 * who are they? Are they the same?
468 this_diff
= ll
->parent_map
;
470 same_diff
= this_diff
;
471 else if (same_diff
!= this_diff
) {
478 if (!has_interesting
&& same_diff
!= all_mask
) {
479 /* This hunk is not that interesting after all */
480 for (j
= hunk_begin
; j
< hunk_end
; j
++)
481 sline
[j
].flag
&= ~mark
;
486 has_interesting
= give_context(sline
, cnt
, num_parent
);
487 return has_interesting
;
490 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, int n
, unsigned long null_context
)
492 l0
= sline
[l0
].p_lno
[n
];
493 l1
= sline
[l1
].p_lno
[n
];
494 printf(" -%lu,%lu", l0
, l1
-l0
-null_context
);
497 static int hunk_comment_line(const char *bol
)
504 return (isalpha(ch
) || ch
== '_' || ch
== '$');
507 static void show_line_to_eol(const char *line
, int len
, const char *reset
)
509 int saw_cr_at_eol
= 0;
512 saw_cr_at_eol
= (len
&& line
[len
-1] == '\r');
514 printf("%.*s%s%s\n", len
- saw_cr_at_eol
, line
,
516 saw_cr_at_eol
? "\r" : "");
519 static void dump_sline(struct sline
*sline
, unsigned long cnt
, int num_parent
,
520 int use_color
, int result_deleted
)
522 unsigned long mark
= (1UL<<num_parent
);
523 unsigned long no_pre_delete
= (2UL<<num_parent
);
525 unsigned long lno
= 0;
526 const char *c_frag
= diff_get_color(use_color
, DIFF_FRAGINFO
);
527 const char *c_func
= diff_get_color(use_color
, DIFF_FUNCINFO
);
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')
592 printf("%s%s %s%s", c_reset
,
595 for (i
= 0; i
< comment_end
; i
++)
596 putchar(hunk_comment
[i
]);
599 printf("%s\n", c_reset
);
600 while (lno
< hunk_end
) {
603 unsigned long p_mask
;
604 struct sline
*sl
= &sline
[lno
++];
605 ll
= (sl
->flag
& no_pre_delete
) ? NULL
: sl
->lost_head
;
607 fputs(c_old
, stdout
);
608 for (j
= 0; j
< num_parent
; j
++) {
609 if (ll
->parent_map
& (1UL<<j
))
614 show_line_to_eol(ll
->line
, -1, c_reset
);
620 if (!(sl
->flag
& (mark
-1))) {
622 * This sline was here to hang the
623 * lost lines in front of it.
627 fputs(c_plain
, stdout
);
630 fputs(c_new
, stdout
);
631 for (j
= 0; j
< num_parent
; j
++) {
632 if (p_mask
& sl
->flag
)
638 show_line_to_eol(sl
->bol
, sl
->len
, c_reset
);
643 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
646 /* We have already examined parent j and we know parent i
647 * and parent j are the same, so reuse the combined result
648 * of parent j for parent i.
650 unsigned long lno
, imask
, jmask
;
654 for (lno
= 0; lno
<= cnt
; lno
++) {
655 struct lline
*ll
= sline
->lost_head
;
656 sline
->p_lno
[i
] = sline
->p_lno
[j
];
658 if (ll
->parent_map
& jmask
)
659 ll
->parent_map
|= imask
;
662 if (sline
->flag
& jmask
)
663 sline
->flag
|= imask
;
666 /* the overall size of the file (sline[cnt]) */
667 sline
->p_lno
[i
] = sline
->p_lno
[j
];
670 static void dump_quoted_path(const char *head
,
673 const char *c_meta
, const char *c_reset
)
675 static struct strbuf buf
= STRBUF_INIT
;
678 strbuf_addstr(&buf
, c_meta
);
679 strbuf_addstr(&buf
, head
);
680 quote_two_c_style(&buf
, prefix
, path
, 0);
681 strbuf_addstr(&buf
, c_reset
);
685 static void show_combined_header(struct combine_diff_path
*elem
,
688 struct rev_info
*rev
,
690 int show_file_header
)
692 struct diff_options
*opt
= &rev
->diffopt
;
693 int abbrev
= DIFF_OPT_TST(opt
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
694 const char *a_prefix
= opt
->a_prefix
? opt
->a_prefix
: "a/";
695 const char *b_prefix
= opt
->b_prefix
? opt
->b_prefix
: "b/";
696 int use_color
= DIFF_OPT_TST(opt
, COLOR_DIFF
);
697 const char *c_meta
= diff_get_color(use_color
, DIFF_METAINFO
);
698 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
704 if (rev
->loginfo
&& !rev
->no_commit_id
)
707 dump_quoted_path(dense
? "diff --cc " : "diff --combined ",
708 "", elem
->path
, c_meta
, c_reset
);
709 printf("%sindex ", c_meta
);
710 for (i
= 0; i
< num_parent
; i
++) {
711 abb
= find_unique_abbrev(elem
->parent
[i
].sha1
,
713 printf("%s%s", i
? "," : "", abb
);
715 abb
= find_unique_abbrev(elem
->sha1
, abbrev
);
716 printf("..%s%s\n", abb
, c_reset
);
719 deleted
= !elem
->mode
;
721 /* We say it was added if nobody had it */
723 for (i
= 0; added
&& i
< num_parent
; i
++)
724 if (elem
->parent
[i
].status
!=
728 printf("%snew file mode %06o",
732 printf("%sdeleted file ", c_meta
);
734 for (i
= 0; i
< num_parent
; i
++) {
735 printf("%s%06o", i
? "," : "",
736 elem
->parent
[i
].mode
);
739 printf("..%06o", elem
->mode
);
741 printf("%s\n", c_reset
);
744 if (!show_file_header
)
748 dump_quoted_path("--- ", "", "/dev/null",
751 dump_quoted_path("--- ", a_prefix
, elem
->path
,
754 dump_quoted_path("+++ ", "", "/dev/null",
757 dump_quoted_path("+++ ", b_prefix
, elem
->path
,
761 static void show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
762 int dense
, struct rev_info
*rev
)
764 struct diff_options
*opt
= &rev
->diffopt
;
765 unsigned long result_size
, cnt
, lno
;
766 int result_deleted
= 0;
768 struct sline
*sline
; /* survived lines */
769 int mode_differs
= 0;
771 int working_tree_file
= is_null_sha1(elem
->sha1
);
772 mmfile_t result_file
;
773 struct userdiff_driver
*userdiff
;
776 context
= opt
->context
;
777 userdiff
= userdiff_find_by_path(elem
->path
);
779 userdiff
= userdiff_find_by_name("default");
781 /* Read the result of merge first */
782 if (!working_tree_file
)
783 result
= grab_blob(elem
->sha1
, elem
->mode
, &result_size
);
785 /* Used by diff-tree to read from the working tree */
789 if (lstat(elem
->path
, &st
) < 0)
792 if (S_ISLNK(st
.st_mode
)) {
793 struct strbuf buf
= STRBUF_INIT
;
795 if (strbuf_readlink(&buf
, elem
->path
, st
.st_size
) < 0) {
796 error("readlink(%s): %s", elem
->path
,
800 result_size
= buf
.len
;
801 result
= strbuf_detach(&buf
, NULL
);
802 elem
->mode
= canon_mode(st
.st_mode
);
803 } else if (S_ISDIR(st
.st_mode
)) {
804 unsigned char sha1
[20];
805 if (resolve_gitlink_ref(elem
->path
, "HEAD", sha1
) < 0)
806 result
= grab_blob(elem
->sha1
, elem
->mode
, &result_size
);
808 result
= grab_blob(sha1
, elem
->mode
, &result_size
);
809 } else if (0 <= (fd
= open(elem
->path
, O_RDONLY
))) {
810 size_t len
= xsize_t(st
.st_size
);
814 elem
->mode
= canon_mode(st
.st_mode
);
815 /* if symlinks don't work, assume symlink if all parents
818 is_file
= has_symlinks
;
819 for (i
= 0; !is_file
&& i
< num_parent
; i
++)
820 is_file
= !S_ISLNK(elem
->parent
[i
].mode
);
822 elem
->mode
= canon_mode(S_IFLNK
);
825 result
= xmalloc(len
+ 1);
827 done
= read_in_full(fd
, result
, len
);
829 die_errno("read error '%s'", elem
->path
);
831 die("early EOF '%s'", elem
->path
);
835 /* If not a fake symlink, apply filters, e.g. autocrlf */
837 struct strbuf buf
= STRBUF_INIT
;
839 if (convert_to_git(elem
->path
, result
, len
, &buf
, safe_crlf
)) {
841 result
= strbuf_detach(&buf
, &len
);
851 result
= xcalloc(1, 1);
858 for (i
= 0; i
< num_parent
; i
++) {
859 if (elem
->parent
[i
].mode
!= elem
->mode
) {
865 if (userdiff
->binary
!= -1)
866 is_binary
= userdiff
->binary
;
868 is_binary
= buffer_is_binary(result
, result_size
);
869 for (i
= 0; !is_binary
&& i
< num_parent
; i
++) {
872 buf
= grab_blob(elem
->parent
[i
].sha1
,
873 elem
->parent
[i
].mode
,
875 if (buffer_is_binary(buf
, size
))
881 show_combined_header(elem
, num_parent
, dense
, rev
,
883 printf("Binary files differ\n");
888 for (cnt
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
892 if (result_size
&& result
[result_size
-1] != '\n')
893 cnt
++; /* incomplete line */
895 sline
= xcalloc(cnt
+2, sizeof(*sline
));
896 sline
[0].bol
= result
;
897 for (lno
= 0; lno
<= cnt
+ 1; lno
++) {
898 sline
[lno
].lost_tail
= &sline
[lno
].lost_head
;
901 for (lno
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
903 sline
[lno
].len
= cp
- sline
[lno
].bol
;
906 sline
[lno
].bol
= cp
+ 1;
909 if (result_size
&& result
[result_size
-1] != '\n')
910 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
912 result_file
.ptr
= result
;
913 result_file
.size
= result_size
;
915 /* Even p_lno[cnt+1] is valid -- that is for the end line number
916 * for deletion hunk at the end.
918 sline
[0].p_lno
= xcalloc((cnt
+2) * num_parent
, sizeof(unsigned long));
919 for (lno
= 0; lno
<= cnt
; lno
++)
920 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
922 for (i
= 0; i
< num_parent
; i
++) {
924 for (j
= 0; j
< i
; j
++) {
925 if (!hashcmp(elem
->parent
[i
].sha1
,
926 elem
->parent
[j
].sha1
)) {
927 reuse_combine_diff(sline
, cnt
, i
, j
);
932 combine_diff(elem
->parent
[i
].sha1
,
933 elem
->parent
[i
].mode
,
935 cnt
, i
, num_parent
, result_deleted
);
938 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
940 if (show_hunks
|| mode_differs
|| working_tree_file
) {
941 show_combined_header(elem
, num_parent
, dense
, rev
,
943 dump_sline(sline
, cnt
, num_parent
,
944 DIFF_OPT_TST(opt
, COLOR_DIFF
), result_deleted
);
948 for (lno
= 0; lno
< cnt
; lno
++) {
949 if (sline
[lno
].lost_head
) {
950 struct lline
*ll
= sline
[lno
].lost_head
;
952 struct lline
*tmp
= ll
;
958 free(sline
[0].p_lno
);
962 #define COLONS "::::::::::::::::::::::::::::::::"
964 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, struct rev_info
*rev
)
966 struct diff_options
*opt
= &rev
->diffopt
;
969 int line_termination
, inter_name_termination
;
971 line_termination
= opt
->line_termination
;
972 inter_name_termination
= '\t';
973 if (!line_termination
)
974 inter_name_termination
= 0;
976 if (rev
->loginfo
&& !rev
->no_commit_id
)
979 if (opt
->output_format
& DIFF_FORMAT_RAW
) {
980 offset
= strlen(COLONS
) - num_parent
;
983 prefix
= COLONS
+ offset
;
986 for (i
= 0; i
< num_parent
; i
++) {
987 printf("%s%06o", prefix
, p
->parent
[i
].mode
);
990 printf("%s%06o", prefix
, p
->mode
);
993 for (i
= 0; i
< num_parent
; i
++)
994 printf(" %s", diff_unique_abbrev(p
->parent
[i
].sha1
,
996 printf(" %s ", diff_unique_abbrev(p
->sha1
, opt
->abbrev
));
999 if (opt
->output_format
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
)) {
1000 for (i
= 0; i
< num_parent
; i
++)
1001 putchar(p
->parent
[i
].status
);
1002 putchar(inter_name_termination
);
1005 write_name_quoted(p
->path
, stdout
, line_termination
);
1008 void show_combined_diff(struct combine_diff_path
*p
,
1011 struct rev_info
*rev
)
1013 struct diff_options
*opt
= &rev
->diffopt
;
1016 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1018 DIFF_FORMAT_NAME_STATUS
))
1019 show_raw_diff(p
, num_parent
, rev
);
1020 else if (opt
->output_format
& DIFF_FORMAT_PATCH
)
1021 show_patch_diff(p
, num_parent
, dense
, rev
);
1024 void diff_tree_combined(const unsigned char *sha1
,
1025 const unsigned char parent
[][20],
1028 struct rev_info
*rev
)
1030 struct diff_options
*opt
= &rev
->diffopt
;
1031 struct diff_options diffopts
;
1032 struct combine_diff_path
*p
, *paths
= NULL
;
1033 int i
, num_paths
, needsep
, show_log_first
;
1036 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1037 DIFF_OPT_SET(&diffopts
, RECURSIVE
);
1038 DIFF_OPT_CLR(&diffopts
, ALLOW_EXTERNAL
);
1040 show_log_first
= !!rev
->loginfo
&& !rev
->no_commit_id
;
1042 /* find set of paths that everybody touches */
1043 for (i
= 0; i
< num_parent
; i
++) {
1044 /* show stat against the first parent even
1045 * when doing combined diff.
1047 int stat_opt
= (opt
->output_format
&
1048 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
));
1049 if (i
== 0 && stat_opt
)
1050 diffopts
.output_format
= stat_opt
;
1052 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1053 diff_tree_sha1(parent
[i
], sha1
, "", &diffopts
);
1054 diffcore_std(&diffopts
);
1055 paths
= intersect_paths(paths
, i
, num_parent
);
1057 if (show_log_first
&& i
== 0) {
1059 if (rev
->verbose_header
&& opt
->output_format
)
1060 putchar(opt
->line_termination
);
1062 diff_flush(&diffopts
);
1065 /* find out surviving paths */
1066 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
1071 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1073 DIFF_FORMAT_NAME_STATUS
)) {
1074 for (p
= paths
; p
; p
= p
->next
) {
1076 show_raw_diff(p
, num_parent
, rev
);
1080 else if (opt
->output_format
&
1081 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
))
1083 if (opt
->output_format
& DIFF_FORMAT_PATCH
) {
1085 putchar(opt
->line_termination
);
1086 for (p
= paths
; p
; p
= p
->next
) {
1088 show_patch_diff(p
, num_parent
, dense
,
1094 /* Clean things up */
1096 struct combine_diff_path
*tmp
= paths
;
1097 paths
= paths
->next
;
1102 void diff_tree_combined_merge(const unsigned char *sha1
,
1103 int dense
, struct rev_info
*rev
)
1106 const unsigned char (*parent
)[20];
1107 struct commit
*commit
= lookup_commit(sha1
);
1108 struct commit_list
*parents
;
1111 for (parents
= commit
->parents
, num_parent
= 0;
1113 parents
= parents
->next
, num_parent
++)
1116 parent
= xmalloc(num_parent
* sizeof(*parent
));
1117 for (parents
= commit
->parents
, num_parent
= 0;
1119 parents
= parents
->next
, num_parent
++)
1120 hashcpy((unsigned char *)(parent
+ num_parent
),
1121 parents
->item
->object
.sha1
);
1122 diff_tree_combined(sha1
, parent
, num_parent
, dense
, rev
);