1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "object-store.h"
10 #include "environment.h"
12 #include "object-name.h"
14 #include "xdiff-interface.h"
15 #include "xdiff/xmacros.h"
20 #include "oid-array.h"
23 static int compare_paths(const struct combine_diff_path
*one
,
24 const struct diff_filespec
*two
)
26 if (!S_ISDIR(one
->mode
) && !S_ISDIR(two
->mode
))
27 return strcmp(one
->path
, two
->path
);
29 return base_name_compare(one
->path
, strlen(one
->path
), one
->mode
,
30 two
->path
, strlen(two
->path
), two
->mode
);
33 static int filename_changed(char status
)
35 return status
== 'R' || status
== 'C';
38 static struct combine_diff_path
*intersect_paths(
39 struct combine_diff_path
*curr
,
42 int combined_all_paths
)
44 struct diff_queue_struct
*q
= &diff_queued_diff
;
45 struct combine_diff_path
*p
, **tail
= &curr
;
49 for (i
= 0; i
< q
->nr
; i
++) {
50 if (diff_unmodified_pair(q
->queue
[i
]))
52 p
= combine_diff_path_new(q
->queue
[i
]->two
->path
,
53 strlen(q
->queue
[i
]->two
->path
),
54 q
->queue
[i
]->two
->mode
,
55 &q
->queue
[i
]->two
->oid
,
57 oidcpy(&p
->parent
[n
].oid
, &q
->queue
[i
]->one
->oid
);
58 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
59 p
->parent
[n
].status
= q
->queue
[i
]->status
;
61 if (combined_all_paths
&&
62 filename_changed(p
->parent
[n
].status
)) {
63 p
->parent
[n
].path
= xstrdup(q
->queue
[i
]->one
->path
);
72 * paths in curr (linked list) and q->queue[] (array) are
73 * both sorted in the tree order.
76 while ((p
= *tail
) != NULL
) {
78 ? -1 : compare_paths(p
, q
->queue
[i
]->two
));
81 /* p->path not in q->queue[]; drop it */
83 for (j
= 0; j
< num_parent
; j
++)
84 free(p
->parent
[j
].path
);
90 /* q->queue[i] not in p->path; skip it */
95 oidcpy(&p
->parent
[n
].oid
, &q
->queue
[i
]->one
->oid
);
96 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
97 p
->parent
[n
].status
= q
->queue
[i
]->status
;
98 if (combined_all_paths
&&
99 filename_changed(p
->parent
[n
].status
))
100 p
->parent
[n
].path
= xstrdup(q
->queue
[i
]->one
->path
);
108 /* Lines lost from parent */
110 struct lline
*next
, *prev
;
112 unsigned long parent_map
;
113 char line
[FLEX_ARRAY
];
116 /* Lines lost from current parent (before coalescing) */
118 struct lline
*lost_head
, *lost_tail
;
122 /* Lines surviving in the merge result */
124 /* Accumulated and coalesced lost lines */
130 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
131 * we did not change it).
132 * bit N is used for "interesting" lines, including context.
133 * bit (N+1) is used for "do not show deletion before this".
136 unsigned long *p_lno
;
139 static int match_string_spaces(const char *line1
, int len1
,
140 const char *line2
, int len2
,
143 if (flags
& XDF_WHITESPACE_FLAGS
) {
144 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
- 1]); len1
--);
145 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
- 1]); len2
--);
148 if (!(flags
& (XDF_IGNORE_WHITESPACE
| XDF_IGNORE_WHITESPACE_CHANGE
)))
149 return (len1
== len2
&& !memcmp(line1
, line2
, len1
));
151 while (len1
> 0 && len2
> 0) {
154 if (XDL_ISSPACE(line1
[len1
]) || XDL_ISSPACE(line2
[len2
])) {
155 if ((flags
& XDF_IGNORE_WHITESPACE_CHANGE
) &&
156 (!XDL_ISSPACE(line1
[len1
]) || !XDL_ISSPACE(line2
[len2
])))
159 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
]); len1
--);
160 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
]); len2
--);
162 if (line1
[len1
] != line2
[len2
])
166 if (flags
& XDF_IGNORE_WHITESPACE
) {
167 /* Consume remaining spaces */
168 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
- 1]); len1
--);
169 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
- 1]); len2
--);
172 /* We matched full line1 and line2 */
179 enum coalesce_direction
{ MATCH
, BASE
, NEW
};
181 /* Coalesce new lines into base by finding LCS */
182 static struct lline
*coalesce_lines(struct lline
*base
, int *lenbase
,
183 struct lline
*newline
, int lennew
,
184 unsigned long parent
, long flags
)
187 enum coalesce_direction
**directions
;
188 struct lline
*baseend
, *newend
= NULL
;
189 int i
, j
, origbaselen
= *lenbase
;
200 * Coalesce new lines into base by finding the LCS
201 * - Create the table to run dynamic programming
203 * - Then reverse read the direction structure:
204 * - If we have MATCH, assign parent to base flag, and consume
205 * both baseend and newend
206 * - Else if we have BASE, consume baseend
207 * - Else if we have NEW, insert newend lline into base and
210 CALLOC_ARRAY(lcs
, st_add(origbaselen
, 1));
211 CALLOC_ARRAY(directions
, st_add(origbaselen
, 1));
212 for (i
= 0; i
< origbaselen
+ 1; i
++) {
213 CALLOC_ARRAY(lcs
[i
], st_add(lennew
, 1));
214 CALLOC_ARRAY(directions
[i
], st_add(lennew
, 1));
215 directions
[i
][0] = BASE
;
217 for (j
= 1; j
< lennew
+ 1; j
++)
218 directions
[0][j
] = NEW
;
220 for (i
= 1, baseend
= base
; i
< origbaselen
+ 1; i
++) {
221 for (j
= 1, newend
= newline
; j
< lennew
+ 1; j
++) {
222 if (match_string_spaces(baseend
->line
, baseend
->len
,
223 newend
->line
, newend
->len
, flags
)) {
224 lcs
[i
][j
] = lcs
[i
- 1][j
- 1] + 1;
225 directions
[i
][j
] = MATCH
;
226 } else if (lcs
[i
][j
- 1] >= lcs
[i
- 1][j
]) {
227 lcs
[i
][j
] = lcs
[i
][j
- 1];
228 directions
[i
][j
] = NEW
;
230 lcs
[i
][j
] = lcs
[i
- 1][j
];
231 directions
[i
][j
] = BASE
;
234 newend
= newend
->next
;
237 baseend
= baseend
->next
;
240 for (i
= 0; i
< origbaselen
+ 1; i
++)
244 /* At this point, baseend and newend point to the end of each lists */
247 while (i
!= 0 || j
!= 0) {
248 if (directions
[i
][j
] == MATCH
) {
249 baseend
->parent_map
|= 1<<parent
;
250 baseend
= baseend
->prev
;
251 newend
= newend
->prev
;
254 } else if (directions
[i
][j
] == NEW
) {
258 /* Remove lline from new list and update newend */
260 lline
->prev
->next
= lline
->next
;
262 newline
= lline
->next
;
264 lline
->next
->prev
= lline
->prev
;
266 newend
= lline
->prev
;
269 /* Add lline to base list */
271 lline
->next
= baseend
->next
;
272 lline
->prev
= baseend
;
274 lline
->prev
->next
= lline
;
283 lline
->next
->prev
= lline
;
286 baseend
= baseend
->prev
;
293 struct lline
*lline
= newend
;
294 newend
= newend
->next
;
298 for (i
= 0; i
< origbaselen
+ 1; i
++)
305 static char *grab_blob(struct repository
*r
,
306 const struct object_id
*oid
, unsigned int mode
,
307 unsigned long *size
, struct userdiff_driver
*textconv
,
311 enum object_type type
;
313 if (S_ISGITLINK(mode
)) {
314 struct strbuf buf
= STRBUF_INIT
;
315 strbuf_addf(&buf
, "Subproject commit %s\n", oid_to_hex(oid
));
317 blob
= strbuf_detach(&buf
, NULL
);
318 } else if (is_null_oid(oid
)) {
321 return xcalloc(1, 1);
322 } else if (textconv
) {
323 struct diff_filespec
*df
= alloc_filespec(path
);
324 fill_filespec(df
, oid
, 1, mode
);
325 *size
= fill_textconv(r
, textconv
, df
, &blob
);
328 blob
= repo_read_object_file(r
, oid
, &type
, size
);
330 die(_("unable to read %s"), oid_to_hex(oid
));
331 if (type
!= OBJ_BLOB
)
332 die("object '%s' is not a blob!", oid_to_hex(oid
));
337 static void append_lost(struct sline
*sline
, int n
, const char *line
, int len
)
340 unsigned long this_mask
= (1UL<<n
);
341 if (line
[len
-1] == '\n')
344 FLEX_ALLOC_MEM(lline
, line
, line
, len
);
347 lline
->prev
= sline
->plost
.lost_tail
;
349 lline
->prev
->next
= lline
;
351 sline
->plost
.lost_head
= lline
;
352 sline
->plost
.lost_tail
= lline
;
354 lline
->parent_map
= this_mask
;
357 struct combine_diff_state
{
364 struct sline
*lost_bucket
;
367 static void consume_hunk(void *state_
,
370 const char *func UNUSED
, long funclen UNUSED
)
372 struct combine_diff_state
*state
= state_
;
378 state
->lno
= state
->nb
;
379 if (state
->nn
== 0) {
380 /* @@ -X,Y +N,0 @@ removed Y lines
381 * that would have come *after* line N
382 * in the result. Our lost buckets hang
383 * to the line after the removed lines,
385 * Note that this is correct even when N == 0,
386 * in which case the hunk removes the first
389 state
->lost_bucket
= &state
->sline
[state
->nb
];
393 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
395 if (!state
->sline
[state
->nb
-1].p_lno
)
396 CALLOC_ARRAY(state
->sline
[state
->nb
- 1].p_lno
,
398 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
401 static int consume_line(void *state_
, char *line
, unsigned long len
)
403 struct combine_diff_state
*state
= state_
;
404 if (!state
->lost_bucket
)
405 return 0; /* not in any hunk yet */
408 append_lost(state
->lost_bucket
, state
->n
, line
+1, len
-1);
411 state
->sline
[state
->lno
-1].flag
|= state
->nmask
;
418 static void combine_diff(struct repository
*r
,
419 const struct object_id
*parent
, unsigned int mode
,
420 mmfile_t
*result_file
,
421 struct sline
*sline
, unsigned int cnt
, int n
,
422 int num_parent
, int result_deleted
,
423 struct userdiff_driver
*textconv
,
424 const char *path
, long flags
)
426 unsigned int p_lno
, lno
;
427 unsigned long nmask
= (1UL << n
);
430 mmfile_t parent_file
;
431 struct combine_diff_state state
;
435 return; /* result deleted */
437 parent_file
.ptr
= grab_blob(r
, parent
, mode
, &sz
, textconv
, path
);
438 parent_file
.size
= sz
;
439 memset(&xpp
, 0, sizeof(xpp
));
441 memset(&xecfg
, 0, sizeof(xecfg
));
442 memset(&state
, 0, sizeof(state
));
446 state
.num_parent
= num_parent
;
449 if (xdi_diff_outf(&parent_file
, result_file
, consume_hunk
,
450 consume_line
, &state
, &xpp
, &xecfg
))
451 die("unable to generate combined diff for %s",
453 free(parent_file
.ptr
);
455 /* Assign line numbers for this parent.
457 * sline[lno].p_lno[n] records the first line number
458 * (counting from 1) for parent N if the final hunk display
459 * started by showing sline[lno] (possibly showing the lost
460 * lines attached to it first).
462 for (lno
= 0, p_lno
= 1; lno
<= cnt
; lno
++) {
464 sline
[lno
].p_lno
[n
] = p_lno
;
466 /* Coalesce new lines */
467 if (sline
[lno
].plost
.lost_head
) {
468 struct sline
*sl
= &sline
[lno
];
469 sl
->lost
= coalesce_lines(sl
->lost
, &sl
->lenlost
,
471 sl
->plost
.len
, n
, flags
);
472 sl
->plost
.lost_head
= sl
->plost
.lost_tail
= NULL
;
476 /* How many lines would this sline advance the p_lno? */
477 ll
= sline
[lno
].lost
;
479 if (ll
->parent_map
& nmask
)
480 p_lno
++; /* '-' means parent had it */
483 if (lno
< cnt
&& !(sline
[lno
].flag
& nmask
))
484 p_lno
++; /* no '+' means parent had it */
486 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
489 static unsigned long context
= 3;
490 static char combine_marker
= '@';
492 static int interesting(struct sline
*sline
, unsigned long all_mask
)
494 /* If some parents lost lines here, or if we have added to
495 * some parent, it is interesting.
497 return ((sline
->flag
& all_mask
) || sline
->lost
);
500 static unsigned long adjust_hunk_tail(struct sline
*sline
,
501 unsigned long all_mask
,
502 unsigned long hunk_begin
,
505 /* i points at the first uninteresting line. If the last line
506 * of the hunk was interesting only because it has some
507 * deletion, then it is not all that interesting for the
508 * purpose of giving trailing context lines. This is because
509 * we output '-' line and then unmodified sline[i-1] itself in
510 * that case which gives us one extra context line.
512 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
517 static unsigned long find_next(struct sline
*sline
,
521 int look_for_uninteresting
)
523 /* We have examined up to i-1 and are about to look at i.
524 * Find next interesting or uninteresting line. Here,
525 * "interesting" does not mean interesting(), but marked by
526 * the give_context() function below (i.e. it includes context
527 * lines that are not interesting to interesting() function
528 * that are surrounded by interesting() ones.
531 if (look_for_uninteresting
532 ? !(sline
[i
].flag
& mark
)
533 : (sline
[i
].flag
& mark
))
540 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
542 unsigned long all_mask
= (1UL<<num_parent
) - 1;
543 unsigned long mark
= (1UL<<num_parent
);
544 unsigned long no_pre_delete
= (2UL<<num_parent
);
547 /* Two groups of interesting lines may have a short gap of
548 * uninteresting lines. Connect such groups to give them a
551 * We first start from what the interesting() function says,
552 * and mark them with "mark", and paint context lines with the
553 * mark. So interesting() would still say false for such context
554 * lines but they are treated as "interesting" in the end.
556 i
= find_next(sline
, mark
, 0, cnt
, 0);
561 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
564 /* Paint a few lines before the first interesting line. */
566 if (!(sline
[j
].flag
& mark
))
567 sline
[j
].flag
|= no_pre_delete
;
568 sline
[j
++].flag
|= mark
;
572 /* we know up to i is to be included. where does the
573 * next uninteresting one start?
575 j
= find_next(sline
, mark
, i
, cnt
, 1);
577 break; /* the rest are all interesting */
579 /* lookahead context lines */
580 k
= find_next(sline
, mark
, j
, cnt
, 0);
581 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
583 if (k
< j
+ context
) {
584 /* k is interesting and [j,k) are not, but
585 * paint them interesting because the gap is small.
588 sline
[j
++].flag
|= mark
;
593 /* j is the first uninteresting line and there is
594 * no overlap beyond it within context lines. Paint
595 * the trailing edge a bit.
598 k
= (j
+ context
< cnt
+1) ? j
+ context
: cnt
+1;
600 sline
[j
++].flag
|= mark
;
605 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
606 int num_parent
, int dense
)
608 unsigned long all_mask
= (1UL<<num_parent
) - 1;
609 unsigned long mark
= (1UL<<num_parent
);
611 int has_interesting
= 0;
613 for (i
= 0; i
<= cnt
; i
++) {
614 if (interesting(&sline
[i
], all_mask
))
615 sline
[i
].flag
|= mark
;
617 sline
[i
].flag
&= ~mark
;
620 return give_context(sline
, cnt
, num_parent
);
622 /* Look at each hunk, and if we have changes from only one
623 * parent, or the changes are the same from all but one
624 * parent, mark that uninteresting.
628 unsigned long j
, hunk_begin
, hunk_end
;
629 unsigned long same_diff
;
630 while (i
<= cnt
&& !(sline
[i
].flag
& mark
))
633 break; /* No more interesting hunks */
635 for (j
= i
+ 1; j
<= cnt
; j
++) {
636 if (!(sline
[j
].flag
& mark
)) {
637 /* Look beyond the end to see if there
638 * is an interesting line after this
639 * hunk within context span.
641 unsigned long la
; /* lookahead */
643 la
= adjust_hunk_tail(sline
, all_mask
,
645 la
= (la
+ context
< cnt
+ 1) ?
646 (la
+ context
) : cnt
+ 1;
647 while (la
&& j
<= --la
) {
648 if (sline
[la
].flag
& mark
) {
660 /* [i..hunk_end) are interesting. Now is it really
661 * interesting? We check if there are only two versions
662 * and the result matches one of them. That is, we look
664 * (+) line, which records lines added to which parents;
665 * this line appears in the result.
666 * (-) line, which records from what parents the line
667 * was removed; this line does not appear in the result.
668 * then check the set of parents the result has difference
669 * from, from all lines. If there are lines that has
670 * different set of parents that the result has differences
671 * from, that means we have more than two versions.
673 * Even when we have only two versions, if the result does
674 * not match any of the parents, the it should be considered
675 * interesting. In such a case, we would have all '+' line.
676 * After passing the above "two versions" test, that would
677 * appear as "the same set of parents" to be "all parents".
681 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
682 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
683 struct lline
*ll
= sline
[j
].lost
;
685 /* This has some changes. Is it the
689 same_diff
= this_diff
;
690 else if (same_diff
!= this_diff
) {
695 while (ll
&& !has_interesting
) {
696 /* Lost this line from these parents;
697 * who are they? Are they the same?
699 this_diff
= ll
->parent_map
;
701 same_diff
= this_diff
;
702 else if (same_diff
!= this_diff
) {
709 if (!has_interesting
&& same_diff
!= all_mask
) {
710 /* This hunk is not that interesting after all */
711 for (j
= hunk_begin
; j
< hunk_end
; j
++)
712 sline
[j
].flag
&= ~mark
;
717 has_interesting
= give_context(sline
, cnt
, num_parent
);
718 return has_interesting
;
721 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, int n
, unsigned long null_context
)
723 l0
= sline
[l0
].p_lno
[n
];
724 l1
= sline
[l1
].p_lno
[n
];
725 printf(" -%lu,%lu", l0
, l1
-l0
-null_context
);
728 static int hunk_comment_line(const char *bol
)
735 return (isalpha(ch
) || ch
== '_' || ch
== '$');
738 static void show_line_to_eol(const char *line
, int len
, const char *reset
)
740 int saw_cr_at_eol
= 0;
743 saw_cr_at_eol
= (len
&& line
[len
-1] == '\r');
745 printf("%.*s%s%s\n", len
- saw_cr_at_eol
, line
,
747 saw_cr_at_eol
? "\r" : "");
750 static void dump_sline(struct sline
*sline
, const char *line_prefix
,
751 unsigned long cnt
, int num_parent
,
752 int use_color
, int result_deleted
)
754 unsigned long mark
= (1UL<<num_parent
);
755 unsigned long no_pre_delete
= (2UL<<num_parent
);
757 unsigned long lno
= 0;
758 const char *c_frag
= diff_get_color(use_color
, DIFF_FRAGINFO
);
759 const char *c_func
= diff_get_color(use_color
, DIFF_FUNCINFO
);
760 const char *c_new
= diff_get_color(use_color
, DIFF_FILE_NEW
);
761 const char *c_old
= diff_get_color(use_color
, DIFF_FILE_OLD
);
762 const char *c_context
= diff_get_color(use_color
, DIFF_CONTEXT
);
763 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
766 return; /* result deleted */
769 unsigned long hunk_end
;
770 unsigned long rlines
;
771 const char *hunk_comment
= NULL
;
772 unsigned long null_context
= 0;
774 while (lno
<= cnt
&& !(sline
[lno
].flag
& mark
)) {
775 if (hunk_comment_line(sline
[lno
].bol
))
776 hunk_comment
= sline
[lno
].bol
;
782 for (hunk_end
= lno
+ 1; hunk_end
<= cnt
; hunk_end
++)
783 if (!(sline
[hunk_end
].flag
& mark
))
786 rlines
= hunk_end
- lno
;
788 rlines
--; /* pointing at the last delete hunk */
792 * Even when running with --unified=0, all
793 * lines in the hunk needs to be processed in
794 * the loop below in order to show the
795 * deletion recorded in lost_head. However,
796 * we do not want to show the resulting line
797 * with all blank context markers in such a
801 for (j
= lno
; j
< hunk_end
; j
++)
802 if (!(sline
[j
].flag
& (mark
-1)))
804 rlines
-= null_context
;
807 printf("%s%s", line_prefix
, c_frag
);
808 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
809 for (i
= 0; i
< num_parent
; i
++)
810 show_parent_lno(sline
, lno
, hunk_end
, i
, null_context
);
811 printf(" +%lu,%lu ", lno
+1, rlines
);
812 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
816 for (i
= 0; i
< 40; i
++) {
817 int ch
= hunk_comment
[i
] & 0xff;
818 if (!ch
|| ch
== '\n')
824 printf("%s%s %s%s", c_reset
,
827 for (i
= 0; i
< comment_end
; i
++)
828 putchar(hunk_comment
[i
]);
831 printf("%s\n", c_reset
);
832 while (lno
< hunk_end
) {
835 unsigned long p_mask
;
836 struct sline
*sl
= &sline
[lno
++];
837 ll
= (sl
->flag
& no_pre_delete
) ? NULL
: sl
->lost
;
839 printf("%s%s", line_prefix
, c_old
);
840 for (j
= 0; j
< num_parent
; j
++) {
841 if (ll
->parent_map
& (1UL<<j
))
846 show_line_to_eol(ll
->line
, -1, c_reset
);
852 fputs(line_prefix
, stdout
);
853 if (!(sl
->flag
& (mark
-1))) {
855 * This sline was here to hang the
856 * lost lines in front of it.
860 fputs(c_context
, stdout
);
863 fputs(c_new
, stdout
);
864 for (j
= 0; j
< num_parent
; j
++) {
865 if (p_mask
& sl
->flag
)
871 show_line_to_eol(sl
->bol
, sl
->len
, c_reset
);
876 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
879 /* We have already examined parent j and we know parent i
880 * and parent j are the same, so reuse the combined result
881 * of parent j for parent i.
883 unsigned long lno
, imask
, jmask
;
887 for (lno
= 0; lno
<= cnt
; lno
++) {
888 struct lline
*ll
= sline
->lost
;
889 sline
->p_lno
[i
] = sline
->p_lno
[j
];
891 if (ll
->parent_map
& jmask
)
892 ll
->parent_map
|= imask
;
895 if (sline
->flag
& jmask
)
896 sline
->flag
|= imask
;
899 /* the overall size of the file (sline[cnt]) */
900 sline
->p_lno
[i
] = sline
->p_lno
[j
];
903 static void dump_quoted_path(const char *head
,
906 const char *line_prefix
,
907 const char *c_meta
, const char *c_reset
)
909 static struct strbuf buf
= STRBUF_INIT
;
912 strbuf_addstr(&buf
, line_prefix
);
913 strbuf_addstr(&buf
, c_meta
);
914 strbuf_addstr(&buf
, head
);
915 quote_two_c_style(&buf
, prefix
, path
, 0);
916 strbuf_addstr(&buf
, c_reset
);
920 static void show_combined_header(struct combine_diff_path
*elem
,
922 struct rev_info
*rev
,
923 const char *line_prefix
,
925 int show_file_header
)
927 struct diff_options
*opt
= &rev
->diffopt
;
928 int abbrev
= opt
->flags
.full_index
? the_hash_algo
->hexsz
: DEFAULT_ABBREV
;
929 const char *a_prefix
= opt
->a_prefix
? opt
->a_prefix
: "a/";
930 const char *b_prefix
= opt
->b_prefix
? opt
->b_prefix
: "b/";
931 const char *c_meta
= diff_get_color_opt(opt
, DIFF_METAINFO
);
932 const char *c_reset
= diff_get_color_opt(opt
, DIFF_RESET
);
937 int dense
= rev
->dense_combined_merges
;
939 if (rev
->loginfo
&& !rev
->no_commit_id
)
942 dump_quoted_path(dense
? "diff --cc " : "diff --combined ",
943 "", elem
->path
, line_prefix
, c_meta
, c_reset
);
944 printf("%s%sindex ", line_prefix
, c_meta
);
945 for (i
= 0; i
< num_parent
; i
++) {
946 abb
= repo_find_unique_abbrev(the_repository
,
947 &elem
->parent
[i
].oid
, abbrev
);
948 printf("%s%s", i
? "," : "", abb
);
950 abb
= repo_find_unique_abbrev(the_repository
, &elem
->oid
, abbrev
);
951 printf("..%s%s\n", abb
, c_reset
);
954 deleted
= !elem
->mode
;
956 /* We say it was added if nobody had it */
958 for (i
= 0; added
&& i
< num_parent
; i
++)
959 if (elem
->parent
[i
].status
!=
963 printf("%s%snew file mode %06o",
964 line_prefix
, c_meta
, elem
->mode
);
967 printf("%s%sdeleted file ",
968 line_prefix
, c_meta
);
970 for (i
= 0; i
< num_parent
; i
++) {
971 printf("%s%06o", i
? "," : "",
972 elem
->parent
[i
].mode
);
975 printf("..%06o", elem
->mode
);
977 printf("%s\n", c_reset
);
980 if (!show_file_header
)
983 if (rev
->combined_all_paths
) {
984 for (i
= 0; i
< num_parent
; i
++) {
985 const char *path
= elem
->parent
[i
].path
?
986 elem
->parent
[i
].path
:
988 if (elem
->parent
[i
].status
== DIFF_STATUS_ADDED
)
989 dump_quoted_path("--- ", "", "/dev/null",
990 line_prefix
, c_meta
, c_reset
);
992 dump_quoted_path("--- ", a_prefix
, path
,
993 line_prefix
, c_meta
, c_reset
);
997 dump_quoted_path("--- ", "", "/dev/null",
998 line_prefix
, c_meta
, c_reset
);
1000 dump_quoted_path("--- ", a_prefix
, elem
->path
,
1001 line_prefix
, c_meta
, c_reset
);
1004 dump_quoted_path("+++ ", "", "/dev/null",
1005 line_prefix
, c_meta
, c_reset
);
1007 dump_quoted_path("+++ ", b_prefix
, elem
->path
,
1008 line_prefix
, c_meta
, c_reset
);
1011 static void show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
1012 int working_tree_file
,
1013 struct rev_info
*rev
)
1015 struct diff_options
*opt
= &rev
->diffopt
;
1016 unsigned long result_size
, cnt
, lno
;
1017 int result_deleted
= 0;
1019 struct sline
*sline
; /* survived lines */
1020 int mode_differs
= 0;
1022 mmfile_t result_file
;
1023 struct userdiff_driver
*userdiff
;
1024 struct userdiff_driver
*textconv
= NULL
;
1026 const char *line_prefix
= diff_line_prefix(opt
);
1028 context
= opt
->context
;
1029 userdiff
= userdiff_find_by_path(opt
->repo
->index
, elem
->path
);
1031 userdiff
= userdiff_find_by_name("default");
1032 if (opt
->flags
.allow_textconv
)
1033 textconv
= userdiff_get_textconv(opt
->repo
, userdiff
);
1035 /* Read the result of merge first */
1036 if (!working_tree_file
)
1037 result
= grab_blob(opt
->repo
, &elem
->oid
, elem
->mode
, &result_size
,
1038 textconv
, elem
->path
);
1040 /* Used by diff-tree to read from the working tree */
1044 if (lstat(elem
->path
, &st
) < 0)
1047 if (S_ISLNK(st
.st_mode
)) {
1048 struct strbuf buf
= STRBUF_INIT
;
1050 if (strbuf_readlink(&buf
, elem
->path
, st
.st_size
) < 0) {
1051 error_errno("readlink(%s)", elem
->path
);
1054 result_size
= buf
.len
;
1055 result
= strbuf_detach(&buf
, NULL
);
1056 elem
->mode
= canon_mode(st
.st_mode
);
1057 } else if (S_ISDIR(st
.st_mode
)) {
1058 struct object_id oid
;
1059 if (repo_resolve_gitlink_ref(the_repository
, elem
->path
,
1061 result
= grab_blob(opt
->repo
, &elem
->oid
,
1062 elem
->mode
, &result_size
,
1065 result
= grab_blob(opt
->repo
, &oid
, elem
->mode
,
1066 &result_size
, NULL
, NULL
);
1067 } else if (textconv
) {
1068 struct diff_filespec
*df
= alloc_filespec(elem
->path
);
1069 fill_filespec(df
, null_oid(the_hash_algo
), 0, st
.st_mode
);
1070 result_size
= fill_textconv(opt
->repo
, textconv
, df
, &result
);
1072 } else if (0 <= (fd
= open(elem
->path
, O_RDONLY
))) {
1073 size_t len
= xsize_t(st
.st_size
);
1077 elem
->mode
= canon_mode(st
.st_mode
);
1078 /* if symlinks don't work, assume symlink if all parents
1081 is_file
= has_symlinks
;
1082 for (i
= 0; !is_file
&& i
< num_parent
; i
++)
1083 is_file
= !S_ISLNK(elem
->parent
[i
].mode
);
1085 elem
->mode
= canon_mode(S_IFLNK
);
1088 result
= xmallocz(len
);
1090 done
= read_in_full(fd
, result
, len
);
1092 die_errno("read error '%s'", elem
->path
);
1093 else if (done
< len
)
1094 die("early EOF '%s'", elem
->path
);
1096 /* If not a fake symlink, apply filters, e.g. autocrlf */
1098 struct strbuf buf
= STRBUF_INIT
;
1100 if (convert_to_git(rev
->diffopt
.repo
->index
,
1101 elem
->path
, result
, len
, &buf
, global_conv_flags_eol
)) {
1103 result
= strbuf_detach(&buf
, &len
);
1113 result
= xcalloc(1, 1);
1120 for (i
= 0; i
< num_parent
; i
++) {
1121 if (elem
->parent
[i
].mode
!= elem
->mode
) {
1129 else if (userdiff
->binary
!= -1)
1130 is_binary
= userdiff
->binary
;
1132 is_binary
= buffer_is_binary(result
, result_size
);
1133 for (i
= 0; !is_binary
&& i
< num_parent
; i
++) {
1136 buf
= grab_blob(opt
->repo
,
1137 &elem
->parent
[i
].oid
,
1138 elem
->parent
[i
].mode
,
1140 if (buffer_is_binary(buf
, size
))
1146 show_combined_header(elem
, num_parent
, rev
,
1147 line_prefix
, mode_differs
, 0);
1148 printf("Binary files differ\n");
1153 for (cnt
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
1157 if (result_size
&& result
[result_size
-1] != '\n')
1158 cnt
++; /* incomplete line */
1160 CALLOC_ARRAY(sline
, st_add(cnt
, 2));
1161 sline
[0].bol
= result
;
1162 for (lno
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
1164 sline
[lno
].len
= cp
- sline
[lno
].bol
;
1167 sline
[lno
].bol
= cp
+ 1;
1170 if (result_size
&& result
[result_size
-1] != '\n')
1171 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
1173 result_file
.ptr
= result
;
1174 result_file
.size
= result_size
;
1177 * Even p_lno[cnt+1] is valid -- that is for the end line number
1178 * for deletion hunk at the end.
1180 CALLOC_ARRAY(sline
[0].p_lno
, st_mult(st_add(cnt
, 2), num_parent
));
1181 for (lno
= 0; lno
<= cnt
; lno
++)
1182 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
1184 for (i
= 0; i
< num_parent
; i
++) {
1186 for (j
= 0; j
< i
; j
++) {
1187 if (oideq(&elem
->parent
[i
].oid
,
1188 &elem
->parent
[j
].oid
)) {
1189 reuse_combine_diff(sline
, cnt
, i
, j
);
1194 combine_diff(opt
->repo
,
1195 &elem
->parent
[i
].oid
,
1196 elem
->parent
[i
].mode
,
1197 &result_file
, sline
,
1198 cnt
, i
, num_parent
, result_deleted
,
1199 textconv
, elem
->path
, opt
->xdl_opts
);
1202 show_hunks
= make_hunks(sline
, cnt
, num_parent
, rev
->dense_combined_merges
);
1204 if (show_hunks
|| mode_differs
|| working_tree_file
) {
1205 show_combined_header(elem
, num_parent
, rev
,
1206 line_prefix
, mode_differs
, 1);
1207 dump_sline(sline
, line_prefix
, cnt
, num_parent
,
1208 opt
->use_color
, result_deleted
);
1212 for (lno
= 0; lno
< cnt
+ 2; lno
++) {
1213 if (sline
[lno
].lost
) {
1214 struct lline
*ll
= sline
[lno
].lost
;
1216 struct lline
*tmp
= ll
;
1222 free(sline
[0].p_lno
);
1226 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, struct rev_info
*rev
)
1228 struct diff_options
*opt
= &rev
->diffopt
;
1229 int line_termination
, inter_name_termination
, i
;
1230 const char *line_prefix
= diff_line_prefix(opt
);
1232 line_termination
= opt
->line_termination
;
1233 inter_name_termination
= '\t';
1234 if (!line_termination
)
1235 inter_name_termination
= 0;
1237 if (rev
->loginfo
&& !rev
->no_commit_id
)
1241 if (opt
->output_format
& DIFF_FORMAT_RAW
) {
1242 printf("%s", line_prefix
);
1244 /* As many colons as there are parents */
1245 for (i
= 0; i
< num_parent
; i
++)
1248 /* Show the modes */
1249 for (i
= 0; i
< num_parent
; i
++)
1250 printf("%06o ", p
->parent
[i
].mode
);
1251 printf("%06o", p
->mode
);
1254 for (i
= 0; i
< num_parent
; i
++)
1255 printf(" %s", diff_aligned_abbrev(&p
->parent
[i
].oid
,
1257 printf(" %s ", diff_aligned_abbrev(&p
->oid
, opt
->abbrev
));
1260 if (opt
->output_format
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
)) {
1261 for (i
= 0; i
< num_parent
; i
++)
1262 putchar(p
->parent
[i
].status
);
1263 putchar(inter_name_termination
);
1266 for (i
= 0; i
< num_parent
; i
++)
1267 if (rev
->combined_all_paths
) {
1268 const char *path
= p
->parent
[i
].path
?
1271 write_name_quoted(path
, stdout
, inter_name_termination
);
1273 write_name_quoted(p
->path
, stdout
, line_termination
);
1277 * The result (p->elem) is from the working tree and their
1278 * parents are typically from multiple stages during a merge
1279 * (i.e. diff-files) or the state in HEAD and in the index
1280 * (i.e. diff-index).
1282 void show_combined_diff(struct combine_diff_path
*p
,
1284 struct rev_info
*rev
)
1286 struct diff_options
*opt
= &rev
->diffopt
;
1288 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1290 DIFF_FORMAT_NAME_STATUS
))
1291 show_raw_diff(p
, num_parent
, rev
);
1292 else if (opt
->output_format
& DIFF_FORMAT_PATCH
)
1293 show_patch_diff(p
, num_parent
, 1, rev
);
1296 static void free_combined_pair(struct diff_filepair
*pair
)
1303 * A combine_diff_path expresses N parents on the LHS against 1 merge
1304 * result. Synthesize a diff_filepair that has N entries on the "one"
1305 * side and 1 entry on the "two" side.
1307 * In the future, we might want to add more data to combine_diff_path
1308 * so that we can fill fields we are ignoring (most notably, size) here,
1309 * but currently nobody uses it, so this should suffice for now.
1311 static struct diff_filepair
*combined_pair(struct combine_diff_path
*p
,
1315 struct diff_filepair
*pair
;
1316 struct diff_filespec
*pool
;
1318 pair
= xmalloc(sizeof(*pair
));
1319 CALLOC_ARRAY(pool
, st_add(num_parent
, 1));
1320 pair
->one
= pool
+ 1;
1323 for (i
= 0; i
< num_parent
; i
++) {
1324 pair
->one
[i
].path
= p
->path
;
1325 pair
->one
[i
].mode
= p
->parent
[i
].mode
;
1326 oidcpy(&pair
->one
[i
].oid
, &p
->parent
[i
].oid
);
1327 pair
->one
[i
].oid_valid
= !is_null_oid(&p
->parent
[i
].oid
);
1328 pair
->one
[i
].has_more_entries
= 1;
1330 pair
->one
[num_parent
- 1].has_more_entries
= 0;
1332 pair
->two
->path
= p
->path
;
1333 pair
->two
->mode
= p
->mode
;
1334 oidcpy(&pair
->two
->oid
, &p
->oid
);
1335 pair
->two
->oid_valid
= !is_null_oid(&p
->oid
);
1339 static void handle_combined_callback(struct diff_options
*opt
,
1340 struct combine_diff_path
*paths
,
1344 struct combine_diff_path
*p
;
1345 struct diff_queue_struct q
;
1348 CALLOC_ARRAY(q
.queue
, num_paths
);
1349 q
.alloc
= num_paths
;
1351 for (i
= 0, p
= paths
; p
; p
= p
->next
)
1352 q
.queue
[i
++] = combined_pair(p
, num_parent
);
1353 opt
->format_callback(&q
, opt
, opt
->format_callback_data
);
1354 for (i
= 0; i
< num_paths
; i
++)
1355 free_combined_pair(q
.queue
[i
]);
1359 static const char *path_path(void *obj
)
1361 struct combine_diff_path
*path
= (struct combine_diff_path
*)obj
;
1367 * Diff stat formats which we always compute solely against the first parent.
1369 #define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
1370 | DIFF_FORMAT_SHORTSTAT \
1371 | DIFF_FORMAT_SUMMARY \
1372 | DIFF_FORMAT_DIRSTAT \
1373 | DIFF_FORMAT_DIFFSTAT)
1375 /* find set of paths that every parent touches */
1376 static struct combine_diff_path
*find_paths_generic(const struct object_id
*oid
,
1377 const struct oid_array
*parents
,
1378 struct diff_options
*opt
,
1379 int combined_all_paths
)
1381 struct combine_diff_path
*paths
= NULL
;
1382 int i
, num_parent
= parents
->nr
;
1383 int output_format
= opt
->output_format
;
1384 char *orderfile
= opt
->orderfile
;
1386 opt
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1387 /* tell diff_tree to emit paths in sorted (=tree) order */
1388 opt
->orderfile
= NULL
;
1390 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
1391 for (i
= 0; i
< num_parent
; i
++) {
1393 * show stat against the first parent even when doing
1396 int stat_opt
= output_format
& STAT_FORMAT_MASK
;
1397 if (i
== 0 && stat_opt
)
1398 opt
->output_format
= stat_opt
;
1400 opt
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1401 diff_tree_oid(&parents
->oid
[i
], oid
, "", opt
);
1403 paths
= intersect_paths(paths
, i
, num_parent
,
1404 combined_all_paths
);
1406 /* if showing diff, show it in requested order */
1407 if (opt
->output_format
!= DIFF_FORMAT_NO_OUTPUT
&&
1409 diffcore_order(orderfile
);
1415 opt
->output_format
= output_format
;
1416 opt
->orderfile
= orderfile
;
1422 * find set of paths that everybody touches, assuming diff is run without
1423 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1425 static struct combine_diff_path
*find_paths_multitree(
1426 const struct object_id
*oid
, const struct oid_array
*parents
,
1427 struct diff_options
*opt
)
1429 int i
, nparent
= parents
->nr
;
1430 const struct object_id
**parents_oid
;
1431 struct combine_diff_path
*paths
;
1434 ALLOC_ARRAY(parents_oid
, nparent
);
1435 for (i
= 0; i
< nparent
; i
++)
1436 parents_oid
[i
] = &parents
->oid
[i
];
1438 strbuf_init(&base
, PATH_MAX
);
1439 paths
= diff_tree_paths(oid
, parents_oid
, nparent
, &base
, opt
);
1441 strbuf_release(&base
);
1446 static int match_objfind(struct combine_diff_path
*path
,
1448 const struct oidset
*set
)
1451 if (oidset_contains(set
, &path
->oid
))
1453 for (i
= 0; i
< num_parent
; i
++) {
1454 if (oidset_contains(set
, &path
->parent
[i
].oid
))
1460 static struct combine_diff_path
*combined_objfind(struct diff_options
*opt
,
1461 struct combine_diff_path
*paths
,
1464 struct combine_diff_path
*ret
= NULL
, **tail
= &ret
;
1465 struct combine_diff_path
*p
= paths
;
1468 struct combine_diff_path
*next
= p
->next
;
1470 if (match_objfind(p
, num_parent
, opt
->objfind
)) {
1483 void diff_tree_combined(const struct object_id
*oid
,
1484 const struct oid_array
*parents
,
1485 struct rev_info
*rev
)
1487 struct diff_options
*opt
= &rev
->diffopt
;
1488 struct diff_options diffopts
;
1489 struct combine_diff_path
*p
, *paths
;
1490 int i
, num_paths
, needsep
, show_log_first
, num_parent
= parents
->nr
;
1491 int need_generic_pathscan
;
1493 if (opt
->ignore_regex_nr
)
1494 die("combined diff and '%s' cannot be used together",
1495 "--ignore-matching-lines");
1496 if (opt
->close_file
)
1497 die("combined diff and '%s' cannot be used together",
1500 /* nothing to do, if no parents */
1504 show_log_first
= !!rev
->loginfo
&& !rev
->no_commit_id
;
1506 if (show_log_first
) {
1509 if (rev
->verbose_header
&& opt
->output_format
&&
1510 opt
->output_format
!= DIFF_FORMAT_NO_OUTPUT
&&
1511 !commit_format_is_empty(rev
->commit_format
))
1512 printf("%s%c", diff_line_prefix(opt
),
1513 opt
->line_termination
);
1517 copy_pathspec(&diffopts
.pathspec
, &opt
->pathspec
);
1518 diffopts
.flags
.recursive
= 1;
1519 diffopts
.flags
.allow_external
= 0;
1521 /* find set of paths that everybody touches
1525 * Diffcore transformations are bound to diff_filespec and logic
1526 * comparing two entries - i.e. they do not apply directly to combine
1529 * If some of such transformations is requested - we launch generic
1530 * path scanning, which works significantly slower compared to
1531 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1533 * TODO some of the filters could be ported to work on
1534 * combine_diff_paths - i.e. all functionality that skips paths, so in
1535 * theory, we could end up having only multitree path scanning.
1537 * NOTE please keep this semantically in sync with diffcore_std()
1539 need_generic_pathscan
= opt
->skip_stat_unmatch
||
1540 opt
->flags
.follow_renames
||
1541 opt
->break_opt
!= -1 ||
1542 opt
->detect_rename
||
1543 (opt
->pickaxe_opts
&
1544 (DIFF_PICKAXE_KINDS_MASK
& ~DIFF_PICKAXE_KIND_OBJFIND
)) ||
1547 if (need_generic_pathscan
) {
1549 * NOTE generic case also handles --stat, as it computes
1550 * diff(sha1,parent_i) for all i to do the job, specifically
1553 paths
= find_paths_generic(oid
, parents
, &diffopts
,
1554 rev
->combined_all_paths
);
1558 paths
= find_paths_multitree(oid
, parents
, &diffopts
);
1560 if (opt
->pickaxe_opts
& DIFF_PICKAXE_KIND_OBJFIND
)
1561 paths
= combined_objfind(opt
, paths
, num_parent
);
1564 * show stat against the first parent even
1565 * when doing combined diff.
1567 stat_opt
= opt
->output_format
& STAT_FORMAT_MASK
;
1569 diffopts
.output_format
= stat_opt
;
1571 diff_tree_oid(&parents
->oid
[0], oid
, "", &diffopts
);
1572 diffcore_std(&diffopts
);
1574 diffcore_order(opt
->orderfile
);
1575 diff_flush(&diffopts
);
1579 /* find out number of surviving paths */
1580 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
)
1583 /* order paths according to diffcore_order */
1584 if (opt
->orderfile
&& num_paths
) {
1585 struct obj_order
*o
;
1587 ALLOC_ARRAY(o
, num_paths
);
1588 for (i
= 0, p
= paths
; p
; p
= p
->next
, i
++)
1590 order_objects(opt
->orderfile
, path_path
, o
, num_paths
);
1591 for (i
= 0; i
< num_paths
- 1; i
++) {
1593 p
->next
= o
[i
+1].obj
;
1596 p
= o
[num_paths
-1].obj
;
1604 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1606 DIFF_FORMAT_NAME_STATUS
)) {
1607 for (p
= paths
; p
; p
= p
->next
)
1608 show_raw_diff(p
, num_parent
, rev
);
1611 else if (opt
->output_format
& STAT_FORMAT_MASK
)
1613 else if (opt
->output_format
& DIFF_FORMAT_CALLBACK
)
1614 handle_combined_callback(opt
, paths
, num_parent
, num_paths
);
1616 if (opt
->output_format
& DIFF_FORMAT_PATCH
) {
1618 printf("%s%c", diff_line_prefix(opt
),
1619 opt
->line_termination
);
1620 for (p
= paths
; p
; p
= p
->next
)
1621 show_patch_diff(p
, num_parent
, 0, rev
);
1625 /* Clean things up */
1627 struct combine_diff_path
*tmp
= paths
;
1628 paths
= paths
->next
;
1629 for (i
= 0; i
< num_parent
; i
++)
1630 free(tmp
->parent
[i
].path
);
1634 clear_pathspec(&diffopts
.pathspec
);
1637 void diff_tree_combined_merge(const struct commit
*commit
,
1638 struct rev_info
*rev
)
1640 struct commit_list
*parent
= get_saved_parents(rev
, commit
);
1641 struct oid_array parents
= OID_ARRAY_INIT
;
1644 oid_array_append(&parents
, &parent
->item
->object
.oid
);
1645 parent
= parent
->next
;
1647 diff_tree_combined(&commit
->object
.oid
, &parents
, rev
);
1648 oid_array_clear(&parents
);
1651 struct combine_diff_path
*combine_diff_path_new(const char *path
,
1654 const struct object_id
*oid
,
1657 struct combine_diff_path
*p
;
1658 size_t parent_len
= st_mult(sizeof(p
->parent
[0]), num_parents
);
1660 p
= xmalloc(st_add4(sizeof(*p
), path_len
, 1, parent_len
));
1661 p
->path
= (char *)&(p
->parent
[num_parents
]);
1662 memcpy(p
->path
, path
, path_len
);
1663 p
->path
[path_len
] = 0;
1666 oidcpy(&p
->oid
, oid
);
1668 memset(p
->parent
, 0, parent_len
);