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
,
97 unsigned long *size
, struct userdiff_driver
*textconv
,
101 enum object_type type
;
103 if (S_ISGITLINK(mode
)) {
105 *size
= snprintf(blob
, 100,
106 "Subproject commit %s\n", sha1_to_hex(sha1
));
107 } else if (is_null_sha1(sha1
)) {
110 return xcalloc(1, 1);
111 } else if (textconv
) {
112 struct diff_filespec
*df
= alloc_filespec(path
);
113 fill_filespec(df
, sha1
, mode
);
114 *size
= fill_textconv(textconv
, df
, &blob
);
117 blob
= read_sha1_file(sha1
, &type
, size
);
118 if (type
!= OBJ_BLOB
)
119 die("object '%s' is not a blob!", sha1_to_hex(sha1
));
124 static void append_lost(struct sline
*sline
, int n
, const char *line
, int len
)
127 unsigned long this_mask
= (1UL<<n
);
128 if (line
[len
-1] == '\n')
131 /* Check to see if we can squash things */
132 if (sline
->lost_head
) {
133 lline
= sline
->next_lost
;
135 if (lline
->len
== len
&&
136 !memcmp(lline
->line
, line
, len
)) {
137 lline
->parent_map
|= this_mask
;
138 sline
->next_lost
= lline
->next
;
145 lline
= xmalloc(sizeof(*lline
) + len
+ 1);
148 lline
->parent_map
= this_mask
;
149 memcpy(lline
->line
, line
, len
);
150 lline
->line
[len
] = 0;
151 *sline
->lost_tail
= lline
;
152 sline
->lost_tail
= &lline
->next
;
153 sline
->next_lost
= NULL
;
156 struct combine_diff_state
{
163 struct sline
*lost_bucket
;
166 static void consume_line(void *state_
, char *line
, unsigned long len
)
168 struct combine_diff_state
*state
= state_
;
169 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
170 if (parse_hunk_header(line
, len
,
171 &state
->ob
, &state
->on
,
172 &state
->nb
, &state
->nn
))
174 state
->lno
= state
->nb
;
175 if (state
->nn
== 0) {
176 /* @@ -X,Y +N,0 @@ removed Y lines
177 * that would have come *after* line N
178 * in the result. Our lost buckets hang
179 * to the line after the removed lines,
181 * Note that this is correct even when N == 0,
182 * in which case the hunk removes the first
185 state
->lost_bucket
= &state
->sline
[state
->nb
];
189 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
191 if (!state
->sline
[state
->nb
-1].p_lno
)
192 state
->sline
[state
->nb
-1].p_lno
=
193 xcalloc(state
->num_parent
,
194 sizeof(unsigned long));
195 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
196 state
->lost_bucket
->next_lost
= state
->lost_bucket
->lost_head
;
199 if (!state
->lost_bucket
)
200 return; /* not in any hunk yet */
203 append_lost(state
->lost_bucket
, state
->n
, line
+1, len
-1);
206 state
->sline
[state
->lno
-1].flag
|= state
->nmask
;
212 static void combine_diff(const unsigned char *parent
, unsigned int mode
,
213 mmfile_t
*result_file
,
214 struct sline
*sline
, unsigned int cnt
, int n
,
215 int num_parent
, int result_deleted
,
216 struct userdiff_driver
*textconv
,
219 unsigned int p_lno
, lno
;
220 unsigned long nmask
= (1UL << n
);
223 mmfile_t parent_file
;
224 struct combine_diff_state state
;
228 return; /* result deleted */
230 parent_file
.ptr
= grab_blob(parent
, mode
, &sz
, textconv
, path
);
231 parent_file
.size
= sz
;
232 memset(&xpp
, 0, sizeof(xpp
));
234 memset(&xecfg
, 0, sizeof(xecfg
));
235 memset(&state
, 0, sizeof(state
));
239 state
.num_parent
= num_parent
;
242 xdi_diff_outf(&parent_file
, result_file
, consume_line
, &state
,
244 free(parent_file
.ptr
);
246 /* Assign line numbers for this parent.
248 * sline[lno].p_lno[n] records the first line number
249 * (counting from 1) for parent N if the final hunk display
250 * started by showing sline[lno] (possibly showing the lost
251 * lines attached to it first).
253 for (lno
= 0, p_lno
= 1; lno
<= cnt
; lno
++) {
255 sline
[lno
].p_lno
[n
] = p_lno
;
257 /* How many lines would this sline advance the p_lno? */
258 ll
= sline
[lno
].lost_head
;
260 if (ll
->parent_map
& nmask
)
261 p_lno
++; /* '-' means parent had it */
264 if (lno
< cnt
&& !(sline
[lno
].flag
& nmask
))
265 p_lno
++; /* no '+' means parent had it */
267 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
270 static unsigned long context
= 3;
271 static char combine_marker
= '@';
273 static int interesting(struct sline
*sline
, unsigned long all_mask
)
275 /* If some parents lost lines here, or if we have added to
276 * some parent, it is interesting.
278 return ((sline
->flag
& all_mask
) || sline
->lost_head
);
281 static unsigned long adjust_hunk_tail(struct sline
*sline
,
282 unsigned long all_mask
,
283 unsigned long hunk_begin
,
286 /* i points at the first uninteresting line. If the last line
287 * of the hunk was interesting only because it has some
288 * deletion, then it is not all that interesting for the
289 * purpose of giving trailing context lines. This is because
290 * we output '-' line and then unmodified sline[i-1] itself in
291 * that case which gives us one extra context line.
293 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
298 static unsigned long find_next(struct sline
*sline
,
302 int look_for_uninteresting
)
304 /* We have examined up to i-1 and are about to look at i.
305 * Find next interesting or uninteresting line. Here,
306 * "interesting" does not mean interesting(), but marked by
307 * the give_context() function below (i.e. it includes context
308 * lines that are not interesting to interesting() function
309 * that are surrounded by interesting() ones.
312 if (look_for_uninteresting
313 ? !(sline
[i
].flag
& mark
)
314 : (sline
[i
].flag
& mark
))
321 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
323 unsigned long all_mask
= (1UL<<num_parent
) - 1;
324 unsigned long mark
= (1UL<<num_parent
);
325 unsigned long no_pre_delete
= (2UL<<num_parent
);
328 /* Two groups of interesting lines may have a short gap of
329 * uninteresting lines. Connect such groups to give them a
332 * We first start from what the interesting() function says,
333 * and mark them with "mark", and paint context lines with the
334 * mark. So interesting() would still say false for such context
335 * lines but they are treated as "interesting" in the end.
337 i
= find_next(sline
, mark
, 0, cnt
, 0);
342 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
345 /* Paint a few lines before the first interesting line. */
347 sline
[j
++].flag
|= mark
| no_pre_delete
;
350 /* we know up to i is to be included. where does the
351 * next uninteresting one start?
353 j
= find_next(sline
, mark
, i
, cnt
, 1);
355 break; /* the rest are all interesting */
357 /* lookahead context lines */
358 k
= find_next(sline
, mark
, j
, cnt
, 0);
359 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
361 if (k
< j
+ context
) {
362 /* k is interesting and [j,k) are not, but
363 * paint them interesting because the gap is small.
366 sline
[j
++].flag
|= mark
;
371 /* j is the first uninteresting line and there is
372 * no overlap beyond it within context lines. Paint
373 * the trailing edge a bit.
376 k
= (j
+ context
< cnt
+1) ? j
+ context
: cnt
+1;
378 sline
[j
++].flag
|= mark
;
383 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
384 int num_parent
, int dense
)
386 unsigned long all_mask
= (1UL<<num_parent
) - 1;
387 unsigned long mark
= (1UL<<num_parent
);
389 int has_interesting
= 0;
391 for (i
= 0; i
<= cnt
; i
++) {
392 if (interesting(&sline
[i
], all_mask
))
393 sline
[i
].flag
|= mark
;
395 sline
[i
].flag
&= ~mark
;
398 return give_context(sline
, cnt
, num_parent
);
400 /* Look at each hunk, and if we have changes from only one
401 * parent, or the changes are the same from all but one
402 * parent, mark that uninteresting.
406 unsigned long j
, hunk_begin
, hunk_end
;
407 unsigned long same_diff
;
408 while (i
<= cnt
&& !(sline
[i
].flag
& mark
))
411 break; /* No more interesting hunks */
413 for (j
= i
+ 1; j
<= cnt
; j
++) {
414 if (!(sline
[j
].flag
& mark
)) {
415 /* Look beyond the end to see if there
416 * is an interesting line after this
417 * hunk within context span.
419 unsigned long la
; /* lookahead */
421 la
= adjust_hunk_tail(sline
, all_mask
,
423 la
= (la
+ context
< cnt
+ 1) ?
424 (la
+ context
) : cnt
+ 1;
426 if (sline
[la
].flag
& mark
) {
438 /* [i..hunk_end) are interesting. Now is it really
439 * interesting? We check if there are only two versions
440 * and the result matches one of them. That is, we look
442 * (+) line, which records lines added to which parents;
443 * this line appears in the result.
444 * (-) line, which records from what parents the line
445 * was removed; this line does not appear in the result.
446 * then check the set of parents the result has difference
447 * from, from all lines. If there are lines that has
448 * different set of parents that the result has differences
449 * from, that means we have more than two versions.
451 * Even when we have only two versions, if the result does
452 * not match any of the parents, the it should be considered
453 * interesting. In such a case, we would have all '+' line.
454 * After passing the above "two versions" test, that would
455 * appear as "the same set of parents" to be "all parents".
459 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
460 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
461 struct lline
*ll
= sline
[j
].lost_head
;
463 /* This has some changes. Is it the
467 same_diff
= this_diff
;
468 else if (same_diff
!= this_diff
) {
473 while (ll
&& !has_interesting
) {
474 /* Lost this line from these parents;
475 * who are they? Are they the same?
477 this_diff
= ll
->parent_map
;
479 same_diff
= this_diff
;
480 else if (same_diff
!= this_diff
) {
487 if (!has_interesting
&& same_diff
!= all_mask
) {
488 /* This hunk is not that interesting after all */
489 for (j
= hunk_begin
; j
< hunk_end
; j
++)
490 sline
[j
].flag
&= ~mark
;
495 has_interesting
= give_context(sline
, cnt
, num_parent
);
496 return has_interesting
;
499 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, int n
, unsigned long null_context
)
501 l0
= sline
[l0
].p_lno
[n
];
502 l1
= sline
[l1
].p_lno
[n
];
503 printf(" -%lu,%lu", l0
, l1
-l0
-null_context
);
506 static int hunk_comment_line(const char *bol
)
513 return (isalpha(ch
) || ch
== '_' || ch
== '$');
516 static void show_line_to_eol(const char *line
, int len
, const char *reset
)
518 int saw_cr_at_eol
= 0;
521 saw_cr_at_eol
= (len
&& line
[len
-1] == '\r');
523 printf("%.*s%s%s\n", len
- saw_cr_at_eol
, line
,
525 saw_cr_at_eol
? "\r" : "");
528 static void dump_sline(struct sline
*sline
, unsigned long cnt
, int num_parent
,
529 int use_color
, int result_deleted
)
531 unsigned long mark
= (1UL<<num_parent
);
532 unsigned long no_pre_delete
= (2UL<<num_parent
);
534 unsigned long lno
= 0;
535 const char *c_frag
= diff_get_color(use_color
, DIFF_FRAGINFO
);
536 const char *c_func
= diff_get_color(use_color
, DIFF_FUNCINFO
);
537 const char *c_new
= diff_get_color(use_color
, DIFF_FILE_NEW
);
538 const char *c_old
= diff_get_color(use_color
, DIFF_FILE_OLD
);
539 const char *c_plain
= diff_get_color(use_color
, DIFF_PLAIN
);
540 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
543 return; /* result deleted */
546 unsigned long hunk_end
;
547 unsigned long rlines
;
548 const char *hunk_comment
= NULL
;
549 unsigned long null_context
= 0;
551 while (lno
<= cnt
&& !(sline
[lno
].flag
& mark
)) {
552 if (hunk_comment_line(sline
[lno
].bol
))
553 hunk_comment
= sline
[lno
].bol
;
559 for (hunk_end
= lno
+ 1; hunk_end
<= cnt
; hunk_end
++)
560 if (!(sline
[hunk_end
].flag
& mark
))
563 rlines
= hunk_end
- lno
;
565 rlines
--; /* pointing at the last delete hunk */
569 * Even when running with --unified=0, all
570 * lines in the hunk needs to be processed in
571 * the loop below in order to show the
572 * deletion recorded in lost_head. However,
573 * we do not want to show the resulting line
574 * with all blank context markers in such a
578 for (j
= lno
; j
< hunk_end
; j
++)
579 if (!(sline
[j
].flag
& (mark
-1)))
581 rlines
-= null_context
;
584 fputs(c_frag
, stdout
);
585 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
586 for (i
= 0; i
< num_parent
; i
++)
587 show_parent_lno(sline
, lno
, hunk_end
, i
, null_context
);
588 printf(" +%lu,%lu ", lno
+1, rlines
);
589 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
593 for (i
= 0; i
< 40; i
++) {
594 int ch
= hunk_comment
[i
] & 0xff;
595 if (!ch
|| ch
== '\n')
601 printf("%s%s %s%s", c_reset
,
604 for (i
= 0; i
< comment_end
; i
++)
605 putchar(hunk_comment
[i
]);
608 printf("%s\n", c_reset
);
609 while (lno
< hunk_end
) {
612 unsigned long p_mask
;
613 struct sline
*sl
= &sline
[lno
++];
614 ll
= (sl
->flag
& no_pre_delete
) ? NULL
: sl
->lost_head
;
616 fputs(c_old
, stdout
);
617 for (j
= 0; j
< num_parent
; j
++) {
618 if (ll
->parent_map
& (1UL<<j
))
623 show_line_to_eol(ll
->line
, -1, c_reset
);
629 if (!(sl
->flag
& (mark
-1))) {
631 * This sline was here to hang the
632 * lost lines in front of it.
636 fputs(c_plain
, stdout
);
639 fputs(c_new
, stdout
);
640 for (j
= 0; j
< num_parent
; j
++) {
641 if (p_mask
& sl
->flag
)
647 show_line_to_eol(sl
->bol
, sl
->len
, c_reset
);
652 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
655 /* We have already examined parent j and we know parent i
656 * and parent j are the same, so reuse the combined result
657 * of parent j for parent i.
659 unsigned long lno
, imask
, jmask
;
663 for (lno
= 0; lno
<= cnt
; lno
++) {
664 struct lline
*ll
= sline
->lost_head
;
665 sline
->p_lno
[i
] = sline
->p_lno
[j
];
667 if (ll
->parent_map
& jmask
)
668 ll
->parent_map
|= imask
;
671 if (sline
->flag
& jmask
)
672 sline
->flag
|= imask
;
675 /* the overall size of the file (sline[cnt]) */
676 sline
->p_lno
[i
] = sline
->p_lno
[j
];
679 static void dump_quoted_path(const char *head
,
682 const char *c_meta
, const char *c_reset
)
684 static struct strbuf buf
= STRBUF_INIT
;
687 strbuf_addstr(&buf
, c_meta
);
688 strbuf_addstr(&buf
, head
);
689 quote_two_c_style(&buf
, prefix
, path
, 0);
690 strbuf_addstr(&buf
, c_reset
);
694 static void show_combined_header(struct combine_diff_path
*elem
,
697 struct rev_info
*rev
,
699 int show_file_header
)
701 struct diff_options
*opt
= &rev
->diffopt
;
702 int abbrev
= DIFF_OPT_TST(opt
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
703 const char *a_prefix
= opt
->a_prefix
? opt
->a_prefix
: "a/";
704 const char *b_prefix
= opt
->b_prefix
? opt
->b_prefix
: "b/";
705 int use_color
= DIFF_OPT_TST(opt
, COLOR_DIFF
);
706 const char *c_meta
= diff_get_color(use_color
, DIFF_METAINFO
);
707 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
713 if (rev
->loginfo
&& !rev
->no_commit_id
)
716 dump_quoted_path(dense
? "diff --cc " : "diff --combined ",
717 "", elem
->path
, c_meta
, c_reset
);
718 printf("%sindex ", c_meta
);
719 for (i
= 0; i
< num_parent
; i
++) {
720 abb
= find_unique_abbrev(elem
->parent
[i
].sha1
,
722 printf("%s%s", i
? "," : "", abb
);
724 abb
= find_unique_abbrev(elem
->sha1
, abbrev
);
725 printf("..%s%s\n", abb
, c_reset
);
728 deleted
= !elem
->mode
;
730 /* We say it was added if nobody had it */
732 for (i
= 0; added
&& i
< num_parent
; i
++)
733 if (elem
->parent
[i
].status
!=
737 printf("%snew file mode %06o",
741 printf("%sdeleted file ", c_meta
);
743 for (i
= 0; i
< num_parent
; i
++) {
744 printf("%s%06o", i
? "," : "",
745 elem
->parent
[i
].mode
);
748 printf("..%06o", elem
->mode
);
750 printf("%s\n", c_reset
);
753 if (!show_file_header
)
757 dump_quoted_path("--- ", "", "/dev/null",
760 dump_quoted_path("--- ", a_prefix
, elem
->path
,
763 dump_quoted_path("+++ ", "", "/dev/null",
766 dump_quoted_path("+++ ", b_prefix
, elem
->path
,
770 static void show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
771 int dense
, int working_tree_file
,
772 struct rev_info
*rev
)
774 struct diff_options
*opt
= &rev
->diffopt
;
775 unsigned long result_size
, cnt
, lno
;
776 int result_deleted
= 0;
778 struct sline
*sline
; /* survived lines */
779 int mode_differs
= 0;
781 mmfile_t result_file
;
782 struct userdiff_driver
*userdiff
;
783 struct userdiff_driver
*textconv
= NULL
;
786 context
= opt
->context
;
787 userdiff
= userdiff_find_by_path(elem
->path
);
789 userdiff
= userdiff_find_by_name("default");
790 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
))
791 textconv
= userdiff_get_textconv(userdiff
);
793 /* Read the result of merge first */
794 if (!working_tree_file
)
795 result
= grab_blob(elem
->sha1
, elem
->mode
, &result_size
,
796 textconv
, elem
->path
);
798 /* Used by diff-tree to read from the working tree */
802 if (lstat(elem
->path
, &st
) < 0)
805 if (S_ISLNK(st
.st_mode
)) {
806 struct strbuf buf
= STRBUF_INIT
;
808 if (strbuf_readlink(&buf
, elem
->path
, st
.st_size
) < 0) {
809 error("readlink(%s): %s", elem
->path
,
813 result_size
= buf
.len
;
814 result
= strbuf_detach(&buf
, NULL
);
815 elem
->mode
= canon_mode(st
.st_mode
);
816 } else if (S_ISDIR(st
.st_mode
)) {
817 unsigned char sha1
[20];
818 if (resolve_gitlink_ref(elem
->path
, "HEAD", sha1
) < 0)
819 result
= grab_blob(elem
->sha1
, elem
->mode
,
820 &result_size
, NULL
, NULL
);
822 result
= grab_blob(sha1
, elem
->mode
,
823 &result_size
, NULL
, NULL
);
824 } else if (textconv
) {
825 struct diff_filespec
*df
= alloc_filespec(elem
->path
);
826 fill_filespec(df
, null_sha1
, st
.st_mode
);
827 result_size
= fill_textconv(textconv
, df
, &result
);
829 } else if (0 <= (fd
= open(elem
->path
, O_RDONLY
))) {
830 size_t len
= xsize_t(st
.st_size
);
834 elem
->mode
= canon_mode(st
.st_mode
);
835 /* if symlinks don't work, assume symlink if all parents
838 is_file
= has_symlinks
;
839 for (i
= 0; !is_file
&& i
< num_parent
; i
++)
840 is_file
= !S_ISLNK(elem
->parent
[i
].mode
);
842 elem
->mode
= canon_mode(S_IFLNK
);
845 result
= xmalloc(len
+ 1);
847 done
= read_in_full(fd
, result
, len
);
849 die_errno("read error '%s'", elem
->path
);
851 die("early EOF '%s'", elem
->path
);
855 /* If not a fake symlink, apply filters, e.g. autocrlf */
857 struct strbuf buf
= STRBUF_INIT
;
859 if (convert_to_git(elem
->path
, result
, len
, &buf
, safe_crlf
)) {
861 result
= strbuf_detach(&buf
, &len
);
871 result
= xcalloc(1, 1);
878 for (i
= 0; i
< num_parent
; i
++) {
879 if (elem
->parent
[i
].mode
!= elem
->mode
) {
887 else if (userdiff
->binary
!= -1)
888 is_binary
= userdiff
->binary
;
890 is_binary
= buffer_is_binary(result
, result_size
);
891 for (i
= 0; !is_binary
&& i
< num_parent
; i
++) {
894 buf
= grab_blob(elem
->parent
[i
].sha1
,
895 elem
->parent
[i
].mode
,
897 if (buffer_is_binary(buf
, size
))
903 show_combined_header(elem
, num_parent
, dense
, rev
,
905 printf("Binary files differ\n");
910 for (cnt
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
914 if (result_size
&& result
[result_size
-1] != '\n')
915 cnt
++; /* incomplete line */
917 sline
= xcalloc(cnt
+2, sizeof(*sline
));
918 sline
[0].bol
= result
;
919 for (lno
= 0; lno
<= cnt
+ 1; lno
++) {
920 sline
[lno
].lost_tail
= &sline
[lno
].lost_head
;
923 for (lno
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
925 sline
[lno
].len
= cp
- sline
[lno
].bol
;
928 sline
[lno
].bol
= cp
+ 1;
931 if (result_size
&& result
[result_size
-1] != '\n')
932 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
934 result_file
.ptr
= result
;
935 result_file
.size
= result_size
;
937 /* Even p_lno[cnt+1] is valid -- that is for the end line number
938 * for deletion hunk at the end.
940 sline
[0].p_lno
= xcalloc((cnt
+2) * num_parent
, sizeof(unsigned long));
941 for (lno
= 0; lno
<= cnt
; lno
++)
942 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
944 for (i
= 0; i
< num_parent
; i
++) {
946 for (j
= 0; j
< i
; j
++) {
947 if (!hashcmp(elem
->parent
[i
].sha1
,
948 elem
->parent
[j
].sha1
)) {
949 reuse_combine_diff(sline
, cnt
, i
, j
);
954 combine_diff(elem
->parent
[i
].sha1
,
955 elem
->parent
[i
].mode
,
957 cnt
, i
, num_parent
, result_deleted
,
958 textconv
, elem
->path
);
961 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
963 if (show_hunks
|| mode_differs
|| working_tree_file
) {
964 show_combined_header(elem
, num_parent
, dense
, rev
,
966 dump_sline(sline
, cnt
, num_parent
,
967 DIFF_OPT_TST(opt
, COLOR_DIFF
), result_deleted
);
971 for (lno
= 0; lno
< cnt
; lno
++) {
972 if (sline
[lno
].lost_head
) {
973 struct lline
*ll
= sline
[lno
].lost_head
;
975 struct lline
*tmp
= ll
;
981 free(sline
[0].p_lno
);
985 #define COLONS "::::::::::::::::::::::::::::::::"
987 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, struct rev_info
*rev
)
989 struct diff_options
*opt
= &rev
->diffopt
;
992 int line_termination
, inter_name_termination
;
994 line_termination
= opt
->line_termination
;
995 inter_name_termination
= '\t';
996 if (!line_termination
)
997 inter_name_termination
= 0;
999 if (rev
->loginfo
&& !rev
->no_commit_id
)
1002 if (opt
->output_format
& DIFF_FORMAT_RAW
) {
1003 offset
= strlen(COLONS
) - num_parent
;
1006 prefix
= COLONS
+ offset
;
1008 /* Show the modes */
1009 for (i
= 0; i
< num_parent
; i
++) {
1010 printf("%s%06o", prefix
, p
->parent
[i
].mode
);
1013 printf("%s%06o", prefix
, p
->mode
);
1016 for (i
= 0; i
< num_parent
; i
++)
1017 printf(" %s", diff_unique_abbrev(p
->parent
[i
].sha1
,
1019 printf(" %s ", diff_unique_abbrev(p
->sha1
, opt
->abbrev
));
1022 if (opt
->output_format
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
)) {
1023 for (i
= 0; i
< num_parent
; i
++)
1024 putchar(p
->parent
[i
].status
);
1025 putchar(inter_name_termination
);
1028 write_name_quoted(p
->path
, stdout
, line_termination
);
1032 * The result (p->elem) is from the working tree and their
1033 * parents are typically from multiple stages during a merge
1034 * (i.e. diff-files) or the state in HEAD and in the index
1035 * (i.e. diff-index).
1037 void show_combined_diff(struct combine_diff_path
*p
,
1040 struct rev_info
*rev
)
1042 struct diff_options
*opt
= &rev
->diffopt
;
1045 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1047 DIFF_FORMAT_NAME_STATUS
))
1048 show_raw_diff(p
, num_parent
, rev
);
1049 else if (opt
->output_format
& DIFF_FORMAT_PATCH
)
1050 show_patch_diff(p
, num_parent
, dense
, 1, rev
);
1053 void diff_tree_combined(const unsigned char *sha1
,
1054 const unsigned char parent
[][20],
1057 struct rev_info
*rev
)
1059 struct diff_options
*opt
= &rev
->diffopt
;
1060 struct diff_options diffopts
;
1061 struct combine_diff_path
*p
, *paths
= NULL
;
1062 int i
, num_paths
, needsep
, show_log_first
;
1065 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1066 DIFF_OPT_SET(&diffopts
, RECURSIVE
);
1067 DIFF_OPT_CLR(&diffopts
, ALLOW_EXTERNAL
);
1069 show_log_first
= !!rev
->loginfo
&& !rev
->no_commit_id
;
1071 /* find set of paths that everybody touches */
1072 for (i
= 0; i
< num_parent
; i
++) {
1073 /* show stat against the first parent even
1074 * when doing combined diff.
1076 int stat_opt
= (opt
->output_format
&
1077 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
));
1078 if (i
== 0 && stat_opt
)
1079 diffopts
.output_format
= stat_opt
;
1081 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1082 diff_tree_sha1(parent
[i
], sha1
, "", &diffopts
);
1083 diffcore_std(&diffopts
);
1084 paths
= intersect_paths(paths
, i
, num_parent
);
1086 if (show_log_first
&& i
== 0) {
1088 if (rev
->verbose_header
&& opt
->output_format
)
1089 putchar(opt
->line_termination
);
1091 diff_flush(&diffopts
);
1094 /* find out surviving paths */
1095 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
1100 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1102 DIFF_FORMAT_NAME_STATUS
)) {
1103 for (p
= paths
; p
; p
= p
->next
) {
1105 show_raw_diff(p
, num_parent
, rev
);
1109 else if (opt
->output_format
&
1110 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
))
1112 if (opt
->output_format
& DIFF_FORMAT_PATCH
) {
1114 putchar(opt
->line_termination
);
1115 for (p
= paths
; p
; p
= p
->next
) {
1117 show_patch_diff(p
, num_parent
, dense
,
1123 /* Clean things up */
1125 struct combine_diff_path
*tmp
= paths
;
1126 paths
= paths
->next
;
1131 void diff_tree_combined_merge(const unsigned char *sha1
,
1132 int dense
, struct rev_info
*rev
)
1135 const unsigned char (*parent
)[20];
1136 struct commit
*commit
= lookup_commit(sha1
);
1137 struct commit_list
*parents
;
1140 for (parents
= commit
->parents
, num_parent
= 0;
1142 parents
= parents
->next
, num_parent
++)
1145 parent
= xmalloc(num_parent
* sizeof(*parent
));
1146 for (parents
= commit
->parents
, num_parent
= 0;
1148 parents
= parents
->next
, num_parent
++)
1149 hashcpy((unsigned char *)(parent
+ num_parent
),
1150 parents
->item
->object
.sha1
);
1151 diff_tree_combined(sha1
, parent
, num_parent
, dense
, rev
);