1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
6 #include "cache-tree.h"
7 #include "unpack-trees.h"
13 * Error messages expected by scripts out of plumbing commands such as
14 * read-tree. Non-scripted Porcelain is not required to use these messages
15 * and in fact are encouraged to reword them to better suit their particular
16 * situation better. See how "git checkout" replaces not_uptodate_file to
17 * explain why it does not allow switching between branches when you have
18 * local changes, for example.
20 static struct unpack_trees_error_msgs unpack_plumbing_errors
= {
22 "Entry '%s' would be overwritten by merge. Cannot merge.",
24 /* not_uptodate_file */
25 "Entry '%s' not uptodate. Cannot merge.",
27 /* not_uptodate_dir */
28 "Updating '%s' would lose untracked files in it",
30 /* would_lose_untracked */
31 "Untracked working tree file '%s' would be %s by merge.",
34 "Entry '%s' overlaps with '%s'. Cannot bind.",
37 #define ERRORMSG(o,fld) \
38 ( ((o) && (o)->msgs.fld) \
40 : (unpack_plumbing_errors.fld) )
42 static void add_entry(struct unpack_trees_options
*o
, struct cache_entry
*ce
,
43 unsigned int set
, unsigned int clear
)
45 unsigned int size
= ce_size(ce
);
46 struct cache_entry
*new = xmalloc(size
);
48 clear
|= CE_HASHED
| CE_UNHASHED
;
50 memcpy(new, ce
, size
);
52 new->ce_flags
= (new->ce_flags
& ~clear
) | set
;
53 add_index_entry(&o
->result
, new, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
57 * Unlink the last component and schedule the leading directories for
58 * removal, such that empty directories get removed.
60 static void unlink_entry(struct cache_entry
*ce
)
62 if (has_symlink_or_noent_leading_path(ce
->name
, ce_namelen(ce
)))
64 if (unlink_or_warn(ce
->name
))
66 schedule_dir_for_removal(ce
->name
, ce_namelen(ce
));
69 static struct checkout state
;
70 static int check_updates(struct unpack_trees_options
*o
)
72 unsigned cnt
= 0, total
= 0;
73 struct progress
*progress
= NULL
;
74 struct index_state
*index
= &o
->result
;
78 if (o
->update
&& o
->verbose_update
) {
79 for (total
= cnt
= 0; cnt
< index
->cache_nr
; cnt
++) {
80 struct cache_entry
*ce
= index
->cache
[cnt
];
81 if (ce
->ce_flags
& (CE_UPDATE
| CE_REMOVE
| CE_WT_REMOVE
))
85 progress
= start_progress_delay("Checking out files",
91 git_attr_set_direction(GIT_ATTR_CHECKOUT
, &o
->result
);
92 for (i
= 0; i
< index
->cache_nr
; i
++) {
93 struct cache_entry
*ce
= index
->cache
[i
];
95 if (ce
->ce_flags
& CE_WT_REMOVE
) {
96 display_progress(progress
, ++cnt
);
102 if (ce
->ce_flags
& CE_REMOVE
) {
103 display_progress(progress
, ++cnt
);
108 remove_marked_cache_entries(&o
->result
);
109 remove_scheduled_dirs();
111 for (i
= 0; i
< index
->cache_nr
; i
++) {
112 struct cache_entry
*ce
= index
->cache
[i
];
114 if (ce
->ce_flags
& CE_UPDATE
) {
115 display_progress(progress
, ++cnt
);
116 ce
->ce_flags
&= ~CE_UPDATE
;
118 errs
|= checkout_entry(ce
, &state
, NULL
);
122 stop_progress(&progress
);
124 git_attr_set_direction(GIT_ATTR_CHECKIN
, NULL
);
128 static inline int call_unpack_fn(struct cache_entry
**src
, struct unpack_trees_options
*o
)
130 int ret
= o
->fn(src
, o
);
136 static int unpack_index_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
138 struct cache_entry
*src
[5] = { ce
, NULL
, };
142 if (o
->skip_unmerged
) {
143 add_entry(o
, ce
, 0, 0);
147 return call_unpack_fn(src
, o
);
150 static int traverse_trees_recursive(int n
, unsigned long dirmask
, unsigned long df_conflicts
, struct name_entry
*names
, struct traverse_info
*info
)
153 struct tree_desc t
[MAX_UNPACK_TREES
];
154 struct traverse_info newinfo
;
155 struct name_entry
*p
;
164 newinfo
.pathlen
+= tree_entry_len(p
->path
, p
->sha1
) + 1;
165 newinfo
.conflicts
|= df_conflicts
;
167 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
168 const unsigned char *sha1
= NULL
;
170 sha1
= names
[i
].sha1
;
171 fill_tree_descriptor(t
+i
, sha1
);
173 return traverse_trees(n
, t
, &newinfo
);
177 * Compare the traverse-path to the cache entry without actually
178 * having to generate the textual representation of the traverse
181 * NOTE! This *only* compares up to the size of the traverse path
182 * itself - the caller needs to do the final check for the cache
183 * entry having more data at the end!
185 static int do_compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
187 int len
, pathlen
, ce_len
;
191 int cmp
= do_compare_entry(ce
, info
->prev
, &info
->name
);
195 pathlen
= info
->pathlen
;
196 ce_len
= ce_namelen(ce
);
198 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
199 if (ce_len
< pathlen
)
203 ce_name
= ce
->name
+ pathlen
;
205 len
= tree_entry_len(n
->path
, n
->sha1
);
206 return df_name_compare(ce_name
, ce_len
, S_IFREG
, n
->path
, len
, n
->mode
);
209 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
211 int cmp
= do_compare_entry(ce
, info
, n
);
216 * Even if the beginning compared identically, the ce should
217 * compare as bigger than a directory leading up to it!
219 return ce_namelen(ce
) > traverse_path_len(info
, n
);
222 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
, const struct name_entry
*n
, int stage
)
224 int len
= traverse_path_len(info
, n
);
225 struct cache_entry
*ce
= xcalloc(1, cache_entry_size(len
));
227 ce
->ce_mode
= create_ce_mode(n
->mode
);
228 ce
->ce_flags
= create_ce_flags(len
, stage
);
229 hashcpy(ce
->sha1
, n
->sha1
);
230 make_traverse_path(ce
->name
, info
, n
);
235 static int unpack_nondirectories(int n
, unsigned long mask
,
236 unsigned long dirmask
,
237 struct cache_entry
**src
,
238 const struct name_entry
*names
,
239 const struct traverse_info
*info
)
242 struct unpack_trees_options
*o
= info
->data
;
243 unsigned long conflicts
;
245 /* Do we have *only* directories? Nothing to do */
246 if (mask
== dirmask
&& !src
[0])
249 conflicts
= info
->conflicts
;
252 conflicts
|= dirmask
;
255 * Ok, we've filled in up to any potential index entry in src[0],
258 for (i
= 0; i
< n
; i
++) {
260 unsigned int bit
= 1ul << i
;
261 if (conflicts
& bit
) {
262 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
269 else if (i
+ 1 < o
->head_idx
)
271 else if (i
+ 1 > o
->head_idx
)
275 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
);
279 return call_unpack_fn(src
, o
);
281 for (i
= 0; i
< n
; i
++)
282 if (src
[i
] && src
[i
] != o
->df_conflict_entry
)
283 add_entry(o
, src
[i
], 0, 0);
287 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
289 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
290 struct unpack_trees_options
*o
= info
->data
;
291 const struct name_entry
*p
= names
;
293 /* Find first entry with a real name (we could use "mask" too) */
297 /* Are we supposed to look at the index too? */
299 while (o
->pos
< o
->src_index
->cache_nr
) {
300 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
301 int cmp
= compare_entry(ce
, info
, p
);
303 if (unpack_index_entry(ce
, o
) < 0)
311 * If we skip unmerged index entries, we'll skip this
312 * entry *and* the tree entries associated with it!
314 if (o
->skip_unmerged
) {
315 add_entry(o
, ce
, 0, 0);
325 if (unpack_nondirectories(n
, mask
, dirmask
, src
, names
, info
) < 0)
328 /* Now handle any directories.. */
330 unsigned long conflicts
= mask
& ~dirmask
;
337 /* special case: "diff-index --cached" looking at a tree */
338 if (o
->diff_index_cached
&&
339 n
== 1 && dirmask
== 1 && S_ISDIR(names
->mode
)) {
341 matches
= cache_tree_matches_traversal(o
->src_index
->cache_tree
,
344 * Everything under the name matches. Adjust o->pos to
345 * skip the entire hierarchy.
353 if (traverse_trees_recursive(n
, dirmask
, conflicts
,
362 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
364 discard_index(&o
->result
);
367 return error("%s", message
);
374 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
375 * resulting index, -2 on failure to reflect the changes to the work tree.
377 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
380 static struct cache_entry
*dfc
;
381 struct exclude_list el
;
383 if (len
> MAX_UNPACK_TREES
)
384 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES
);
385 memset(&state
, 0, sizeof(state
));
389 state
.refresh_cache
= 1;
391 memset(&el
, 0, sizeof(el
));
392 if (!core_apply_sparse_checkout
|| !o
->update
)
393 o
->skip_sparse_checkout
= 1;
394 if (!o
->skip_sparse_checkout
) {
395 if (add_excludes_from_file_to_list(git_path("info/sparse-checkout"), "", 0, NULL
, &el
, 0) < 0)
396 o
->skip_sparse_checkout
= 1;
401 memset(&o
->result
, 0, sizeof(o
->result
));
402 o
->result
.initialized
= 1;
404 o
->result
.timestamp
.sec
= o
->src_index
->timestamp
.sec
;
405 o
->result
.timestamp
.nsec
= o
->src_index
->timestamp
.nsec
;
410 dfc
= xcalloc(1, cache_entry_size(0));
411 o
->df_conflict_entry
= dfc
;
414 const char *prefix
= o
->prefix
? o
->prefix
: "";
415 struct traverse_info info
;
417 setup_traverse_info(&info
, prefix
);
418 info
.fn
= unpack_callback
;
421 if (traverse_trees(len
, t
, &info
) < 0) {
422 ret
= unpack_failed(o
, NULL
);
427 /* Any left-over entries in the index? */
429 while (o
->pos
< o
->src_index
->cache_nr
) {
430 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
431 if (unpack_index_entry(ce
, o
) < 0) {
432 ret
= unpack_failed(o
, NULL
);
438 if (o
->trivial_merges_only
&& o
->nontrivial_merge
) {
439 ret
= unpack_failed(o
, "Merge requires file-level merging");
444 ret
= check_updates(o
) ? (-2) : 0;
446 *o
->dst_index
= o
->result
;
449 for (i
= 0;i
< el
.nr
;i
++)
450 free(el
.excludes
[i
]);
457 /* Here come the merge functions */
459 static int reject_merge(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
461 return error(ERRORMSG(o
, would_overwrite
), ce
->name
);
464 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
470 return a
->ce_mode
== b
->ce_mode
&&
471 !hashcmp(a
->sha1
, b
->sha1
);
476 * When a CE gets turned into an unmerged entry, we
477 * want it to be up-to-date
479 static int verify_uptodate_1(struct cache_entry
*ce
,
480 struct unpack_trees_options
*o
,
481 const char *error_msg
)
485 if (o
->index_only
|| (!ce_skip_worktree(ce
) && (o
->reset
|| ce_uptodate(ce
))))
488 if (!lstat(ce
->name
, &st
)) {
489 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, CE_MATCH_IGNORE_VALID
);
493 * NEEDSWORK: the current default policy is to allow
494 * submodule to be out of sync wrt the supermodule
495 * index. This needs to be tightened later for
496 * submodules that are marked to be automatically
499 if (S_ISGITLINK(ce
->ce_mode
))
505 return o
->gently
? -1 :
506 error(error_msg
, ce
->name
);
509 static int verify_uptodate(struct cache_entry
*ce
,
510 struct unpack_trees_options
*o
)
512 return verify_uptodate_1(ce
, o
, ERRORMSG(o
, not_uptodate_file
));
515 static void invalidate_ce_path(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
518 cache_tree_invalidate_path(o
->src_index
->cache_tree
, ce
->name
);
522 * Check that checking out ce->sha1 in subdir ce->name is not
523 * going to overwrite any working files.
525 * Currently, git does not checkout subprojects during a superproject
526 * checkout, so it is not going to overwrite anything.
528 static int verify_clean_submodule(struct cache_entry
*ce
, const char *action
,
529 struct unpack_trees_options
*o
)
534 static int verify_clean_subdirectory(struct cache_entry
*ce
, const char *action
,
535 struct unpack_trees_options
*o
)
538 * we are about to extract "ce->name"; we would not want to lose
539 * anything in the existing directory there.
546 unsigned char sha1
[20];
548 if (S_ISGITLINK(ce
->ce_mode
) &&
549 resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) == 0) {
550 /* If we are not going to update the submodule, then
553 if (!hashcmp(sha1
, ce
->sha1
))
555 return verify_clean_submodule(ce
, action
, o
);
559 * First let's make sure we do not have a local modification
562 namelen
= strlen(ce
->name
);
563 for (i
= o
->pos
; i
< o
->src_index
->cache_nr
; i
++) {
564 struct cache_entry
*ce2
= o
->src_index
->cache
[i
];
565 int len
= ce_namelen(ce2
);
567 strncmp(ce
->name
, ce2
->name
, namelen
) ||
568 ce2
->name
[namelen
] != '/')
571 * ce2->name is an entry in the subdirectory.
573 if (!ce_stage(ce2
)) {
574 if (verify_uptodate(ce2
, o
))
576 add_entry(o
, ce2
, CE_REMOVE
, 0);
582 * Then we need to make sure that we do not lose a locally
583 * present file that is not ignored.
585 pathbuf
= xmalloc(namelen
+ 2);
586 memcpy(pathbuf
, ce
->name
, namelen
);
587 strcpy(pathbuf
+namelen
, "/");
589 memset(&d
, 0, sizeof(d
));
591 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
592 i
= read_directory(&d
, pathbuf
, namelen
+1, NULL
);
594 return o
->gently
? -1 :
595 error(ERRORMSG(o
, not_uptodate_dir
), ce
->name
);
601 * This gets called when there was no index entry for the tree entry 'dst',
602 * but we found a file in the working tree that 'lstat()' said was fine,
603 * and we're on a case-insensitive filesystem.
605 * See if we can find a case-insensitive match in the index that also
606 * matches the stat information, and assume it's that other file!
608 static int icase_exists(struct unpack_trees_options
*o
, struct cache_entry
*dst
, struct stat
*st
)
610 struct cache_entry
*src
;
612 src
= index_name_exists(o
->src_index
, dst
->name
, ce_namelen(dst
), 1);
613 return src
&& !ie_match_stat(o
->src_index
, src
, st
, CE_MATCH_IGNORE_VALID
);
617 * We do not want to remove or overwrite a working tree file that
618 * is not tracked, unless it is ignored.
620 static int verify_absent_1(struct cache_entry
*ce
, const char *action
,
621 struct unpack_trees_options
*o
,
622 const char *error_msg
)
626 if (o
->index_only
|| o
->reset
|| !o
->update
)
629 if (has_symlink_or_noent_leading_path(ce
->name
, ce_namelen(ce
)))
632 if (!lstat(ce
->name
, &st
)) {
634 int dtype
= ce_to_dtype(ce
);
635 struct cache_entry
*result
;
638 * It may be that the 'lstat()' succeeded even though
639 * target 'ce' was absent, because there is an old
640 * entry that is different only in case..
642 * Ignore that lstat() if it matches.
644 if (ignore_case
&& icase_exists(o
, ce
, &st
))
647 if (o
->dir
&& excluded(o
->dir
, ce
->name
, &dtype
))
649 * ce->name is explicitly excluded, so it is Ok to
653 if (S_ISDIR(st
.st_mode
)) {
655 * We are checking out path "foo" and
656 * found "foo/." in the working tree.
657 * This is tricky -- if we have modified
658 * files that are in "foo/" we would lose
661 ret
= verify_clean_subdirectory(ce
, action
, o
);
666 * If this removed entries from the index,
667 * what that means is:
669 * (1) the caller unpack_callback() saw path/foo
670 * in the index, and it has not removed it because
671 * it thinks it is handling 'path' as blob with
673 * (2) we will return "ok, we placed a merged entry
674 * in the index" which would cause o->pos to be
675 * incremented by one;
676 * (3) however, original o->pos now has 'path/foo'
677 * marked with "to be removed".
679 * We need to increment it by the number of
680 * deleted entries here.
687 * The previous round may already have decided to
688 * delete this path, which is in a subdirectory that
689 * is being replaced with a blob.
691 result
= index_name_exists(&o
->result
, ce
->name
, ce_namelen(ce
), 0);
693 if (result
->ce_flags
& CE_REMOVE
)
697 return o
->gently
? -1 :
698 error(ERRORMSG(o
, would_lose_untracked
), ce
->name
, action
);
702 static int verify_absent(struct cache_entry
*ce
, const char *action
,
703 struct unpack_trees_options
*o
)
705 return verify_absent_1(ce
, action
, o
, ERRORMSG(o
, would_lose_untracked
));
708 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
709 struct unpack_trees_options
*o
)
711 int update
= CE_UPDATE
;
715 * See if we can re-use the old CE directly?
716 * That way we get the uptodate stat info.
718 * This also removes the UPDATE flag on a match; otherwise
719 * we will end up overwriting local changes in the work tree.
721 if (same(old
, merge
)) {
722 copy_cache_entry(merge
, old
);
725 if (verify_uptodate(old
, o
))
727 if (ce_skip_worktree(old
))
728 update
|= CE_SKIP_WORKTREE
;
729 invalidate_ce_path(old
, o
);
733 if (verify_absent(merge
, "overwritten", o
))
735 invalidate_ce_path(merge
, o
);
738 add_entry(o
, merge
, update
, CE_STAGEMASK
);
742 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
743 struct unpack_trees_options
*o
)
745 /* Did it exist in the index? */
747 if (verify_absent(ce
, "removed", o
))
751 if (verify_uptodate(old
, o
))
753 add_entry(o
, ce
, CE_REMOVE
, 0);
754 invalidate_ce_path(ce
, o
);
758 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
760 add_entry(o
, ce
, 0, 0);
765 static void show_stage_entry(FILE *o
,
766 const char *label
, const struct cache_entry
*ce
)
769 fprintf(o
, "%s (missing)\n", label
);
771 fprintf(o
, "%s%06o %s %d\t%s\n",
774 sha1_to_hex(ce
->sha1
),
780 int threeway_merge(struct cache_entry
**stages
, struct unpack_trees_options
*o
)
782 struct cache_entry
*index
;
783 struct cache_entry
*head
;
784 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
787 int remote_match
= 0;
789 int df_conflict_head
= 0;
790 int df_conflict_remote
= 0;
792 int any_anc_missing
= 0;
793 int no_anc_exists
= 1;
796 for (i
= 1; i
< o
->head_idx
; i
++) {
797 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
804 head
= stages
[o
->head_idx
];
806 if (head
== o
->df_conflict_entry
) {
807 df_conflict_head
= 1;
811 if (remote
== o
->df_conflict_entry
) {
812 df_conflict_remote
= 1;
816 /* First, if there's a #16 situation, note that to prevent #13
819 if (!same(remote
, head
)) {
820 for (i
= 1; i
< o
->head_idx
; i
++) {
821 if (same(stages
[i
], head
)) {
824 if (same(stages
[i
], remote
)) {
830 /* We start with cases where the index is allowed to match
831 * something other than the head: #14(ALT) and #2ALT, where it
832 * is permitted to match the result instead.
834 /* #14, #14ALT, #2ALT */
835 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
836 if (index
&& !same(index
, remote
) && !same(index
, head
))
837 return o
->gently
? -1 : reject_merge(index
, o
);
838 return merged_entry(remote
, index
, o
);
841 * If we have an entry in the index cache, then we want to
842 * make sure that it matches head.
844 if (index
&& !same(index
, head
))
845 return o
->gently
? -1 : reject_merge(index
, o
);
849 if (same(head
, remote
))
850 return merged_entry(head
, index
, o
);
852 if (!df_conflict_remote
&& remote_match
&& !head_match
)
853 return merged_entry(head
, index
, o
);
857 if (!head
&& !remote
&& any_anc_missing
)
860 /* Under the new "aggressive" rule, we resolve mostly trivial
861 * cases that we historically had git-merge-one-file resolve.
864 int head_deleted
= !head
&& !df_conflict_head
;
865 int remote_deleted
= !remote
&& !df_conflict_remote
;
866 struct cache_entry
*ce
= NULL
;
875 for (i
= 1; i
< o
->head_idx
; i
++) {
876 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
885 * Deleted in one and unchanged in the other.
887 if ((head_deleted
&& remote_deleted
) ||
888 (head_deleted
&& remote
&& remote_match
) ||
889 (remote_deleted
&& head
&& head_match
)) {
891 return deleted_entry(index
, index
, o
);
892 if (ce
&& !head_deleted
) {
893 if (verify_absent(ce
, "removed", o
))
899 * Added in both, identically.
901 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
902 return merged_entry(head
, index
, o
);
906 /* Below are "no merge" cases, which require that the index be
907 * up-to-date to avoid the files getting overwritten with
908 * conflict resolution files.
911 if (verify_uptodate(index
, o
))
915 o
->nontrivial_merge
= 1;
917 /* #2, #3, #4, #6, #7, #9, #10, #11. */
919 if (!head_match
|| !remote_match
) {
920 for (i
= 1; i
< o
->head_idx
; i
++) {
921 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
922 keep_entry(stages
[i
], o
);
930 fprintf(stderr
, "read-tree: warning #16 detected\n");
931 show_stage_entry(stderr
, "head ", stages
[head_match
]);
932 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
935 if (head
) { count
+= keep_entry(head
, o
); }
936 if (remote
) { count
+= keep_entry(remote
, o
); }
943 * The rule is to "carry forward" what is in the index without losing
944 * information across a "fast forward", favoring a successful merge
945 * over a merge failure when it makes sense. For details of the
946 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
949 int twoway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
951 struct cache_entry
*current
= src
[0];
952 struct cache_entry
*oldtree
= src
[1];
953 struct cache_entry
*newtree
= src
[2];
955 if (o
->merge_size
!= 2)
956 return error("Cannot do a twoway merge of %d trees",
959 if (oldtree
== o
->df_conflict_entry
)
961 if (newtree
== o
->df_conflict_entry
)
965 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
966 (!oldtree
&& newtree
&&
967 same(current
, newtree
)) || /* 6 and 7 */
968 (oldtree
&& newtree
&&
969 same(oldtree
, newtree
)) || /* 14 and 15 */
970 (oldtree
&& newtree
&&
971 !same(oldtree
, newtree
) && /* 18 and 19 */
972 same(current
, newtree
))) {
973 return keep_entry(current
, o
);
975 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
977 return deleted_entry(oldtree
, current
, o
);
979 else if (oldtree
&& newtree
&&
980 same(current
, oldtree
) && !same(current
, newtree
)) {
982 return merged_entry(newtree
, current
, o
);
985 /* all other failures */
987 return o
->gently
? -1 : reject_merge(oldtree
, o
);
989 return o
->gently
? -1 : reject_merge(current
, o
);
991 return o
->gently
? -1 : reject_merge(newtree
, o
);
996 if (oldtree
&& !o
->initial_checkout
) {
998 * deletion of the path was staged;
1000 if (same(oldtree
, newtree
))
1002 return reject_merge(oldtree
, o
);
1004 return merged_entry(newtree
, current
, o
);
1006 return deleted_entry(oldtree
, current
, o
);
1012 * Keep the index entries at stage0, collapse stage1 but make sure
1013 * stage0 does not have anything there.
1015 int bind_merge(struct cache_entry
**src
,
1016 struct unpack_trees_options
*o
)
1018 struct cache_entry
*old
= src
[0];
1019 struct cache_entry
*a
= src
[1];
1021 if (o
->merge_size
!= 1)
1022 return error("Cannot do a bind merge of %d trees\n",
1025 return o
->gently
? -1 :
1026 error(ERRORMSG(o
, bind_overlap
), a
->name
, old
->name
);
1028 return keep_entry(old
, o
);
1030 return merged_entry(a
, NULL
, o
);
1037 * - take the stat information from stage0, take the data from stage1
1039 int oneway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
1041 struct cache_entry
*old
= src
[0];
1042 struct cache_entry
*a
= src
[1];
1044 if (o
->merge_size
!= 1)
1045 return error("Cannot do a oneway merge of %d trees",
1048 if (!a
|| a
== o
->df_conflict_entry
)
1049 return deleted_entry(old
, old
, o
);
1051 if (old
&& same(old
, a
)) {
1053 if (o
->reset
&& !ce_uptodate(old
) && !ce_skip_worktree(old
)) {
1055 if (lstat(old
->name
, &st
) ||
1056 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
))
1057 update
|= CE_UPDATE
;
1059 add_entry(o
, old
, update
, 0);
1062 return merged_entry(a
, old
, o
);