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.",
36 /* sparse_not_uptodate_file */
37 "Entry '%s' not uptodate. Cannot update sparse checkout.",
39 /* would_lose_orphaned */
40 "Working tree file '%s' would be %s by sparse checkout update.",
43 #define ERRORMSG(o,fld) \
44 ( ((o) && (o)->msgs.fld) \
46 : (unpack_plumbing_errors.fld) )
48 static void add_entry(struct unpack_trees_options
*o
, struct cache_entry
*ce
,
49 unsigned int set
, unsigned int clear
)
51 unsigned int size
= ce_size(ce
);
52 struct cache_entry
*new = xmalloc(size
);
54 clear
|= CE_HASHED
| CE_UNHASHED
;
56 memcpy(new, ce
, size
);
58 new->ce_flags
= (new->ce_flags
& ~clear
) | set
;
59 add_index_entry(&o
->result
, new, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
63 * Unlink the last component and schedule the leading directories for
64 * removal, such that empty directories get removed.
66 static void unlink_entry(struct cache_entry
*ce
)
68 if (has_symlink_or_noent_leading_path(ce
->name
, ce_namelen(ce
)))
70 if (unlink_or_warn(ce
->name
))
72 schedule_dir_for_removal(ce
->name
, ce_namelen(ce
));
75 static struct checkout state
;
76 static int check_updates(struct unpack_trees_options
*o
)
78 unsigned cnt
= 0, total
= 0;
79 struct progress
*progress
= NULL
;
80 struct index_state
*index
= &o
->result
;
84 if (o
->update
&& o
->verbose_update
) {
85 for (total
= cnt
= 0; cnt
< index
->cache_nr
; cnt
++) {
86 struct cache_entry
*ce
= index
->cache
[cnt
];
87 if (ce
->ce_flags
& (CE_UPDATE
| CE_REMOVE
| CE_WT_REMOVE
))
91 progress
= start_progress_delay("Checking out files",
97 git_attr_set_direction(GIT_ATTR_CHECKOUT
, &o
->result
);
98 for (i
= 0; i
< index
->cache_nr
; i
++) {
99 struct cache_entry
*ce
= index
->cache
[i
];
101 if (ce
->ce_flags
& CE_WT_REMOVE
) {
102 display_progress(progress
, ++cnt
);
108 if (ce
->ce_flags
& CE_REMOVE
) {
109 display_progress(progress
, ++cnt
);
114 remove_marked_cache_entries(&o
->result
);
115 remove_scheduled_dirs();
117 for (i
= 0; i
< index
->cache_nr
; i
++) {
118 struct cache_entry
*ce
= index
->cache
[i
];
120 if (ce
->ce_flags
& CE_UPDATE
) {
121 display_progress(progress
, ++cnt
);
122 ce
->ce_flags
&= ~CE_UPDATE
;
124 errs
|= checkout_entry(ce
, &state
, NULL
);
128 stop_progress(&progress
);
130 git_attr_set_direction(GIT_ATTR_CHECKIN
, NULL
);
134 static int verify_uptodate_sparse(struct cache_entry
*ce
, struct unpack_trees_options
*o
);
135 static int verify_absent_sparse(struct cache_entry
*ce
, const char *action
, struct unpack_trees_options
*o
);
137 static int will_have_skip_worktree(const struct cache_entry
*ce
, struct unpack_trees_options
*o
)
139 const char *basename
;
144 basename
= strrchr(ce
->name
, '/');
145 basename
= basename
? basename
+1 : ce
->name
;
146 return excluded_from_list(ce
->name
, ce_namelen(ce
), basename
, NULL
, o
->el
) <= 0;
149 static int apply_sparse_checkout(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
151 int was_skip_worktree
= ce_skip_worktree(ce
);
153 if (will_have_skip_worktree(ce
, o
))
154 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
156 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
159 * We only care about files getting into the checkout area
160 * If merge strategies want to remove some, go ahead, this
161 * flag will be removed eventually in unpack_trees() if it's
162 * outside checkout area.
164 if (ce
->ce_flags
& CE_REMOVE
)
167 if (!was_skip_worktree
&& ce_skip_worktree(ce
)) {
169 * If CE_UPDATE is set, verify_uptodate() must be called already
170 * also stat info may have lost after merged_entry() so calling
171 * verify_uptodate() again may fail
173 if (!(ce
->ce_flags
& CE_UPDATE
) && verify_uptodate_sparse(ce
, o
))
175 ce
->ce_flags
|= CE_WT_REMOVE
;
177 if (was_skip_worktree
&& !ce_skip_worktree(ce
)) {
178 if (verify_absent_sparse(ce
, "overwritten", o
))
180 ce
->ce_flags
|= CE_UPDATE
;
185 static inline int call_unpack_fn(struct cache_entry
**src
, struct unpack_trees_options
*o
)
187 int ret
= o
->fn(src
, o
);
193 static int unpack_index_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
195 struct cache_entry
*src
[5] = { ce
, NULL
, };
199 if (o
->skip_unmerged
) {
200 add_entry(o
, ce
, 0, 0);
204 return call_unpack_fn(src
, o
);
207 static int traverse_trees_recursive(int n
, unsigned long dirmask
, unsigned long df_conflicts
, struct name_entry
*names
, struct traverse_info
*info
)
210 struct tree_desc t
[MAX_UNPACK_TREES
];
211 struct traverse_info newinfo
;
212 struct name_entry
*p
;
221 newinfo
.pathlen
+= tree_entry_len(p
->path
, p
->sha1
) + 1;
222 newinfo
.conflicts
|= df_conflicts
;
224 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
225 const unsigned char *sha1
= NULL
;
227 sha1
= names
[i
].sha1
;
228 fill_tree_descriptor(t
+i
, sha1
);
230 return traverse_trees(n
, t
, &newinfo
);
234 * Compare the traverse-path to the cache entry without actually
235 * having to generate the textual representation of the traverse
238 * NOTE! This *only* compares up to the size of the traverse path
239 * itself - the caller needs to do the final check for the cache
240 * entry having more data at the end!
242 static int do_compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
244 int len
, pathlen
, ce_len
;
248 int cmp
= do_compare_entry(ce
, info
->prev
, &info
->name
);
252 pathlen
= info
->pathlen
;
253 ce_len
= ce_namelen(ce
);
255 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
256 if (ce_len
< pathlen
)
260 ce_name
= ce
->name
+ pathlen
;
262 len
= tree_entry_len(n
->path
, n
->sha1
);
263 return df_name_compare(ce_name
, ce_len
, S_IFREG
, n
->path
, len
, n
->mode
);
266 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
268 int cmp
= do_compare_entry(ce
, info
, n
);
273 * Even if the beginning compared identically, the ce should
274 * compare as bigger than a directory leading up to it!
276 return ce_namelen(ce
) > traverse_path_len(info
, n
);
279 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
, const struct name_entry
*n
, int stage
)
281 int len
= traverse_path_len(info
, n
);
282 struct cache_entry
*ce
= xcalloc(1, cache_entry_size(len
));
284 ce
->ce_mode
= create_ce_mode(n
->mode
);
285 ce
->ce_flags
= create_ce_flags(len
, stage
);
286 hashcpy(ce
->sha1
, n
->sha1
);
287 make_traverse_path(ce
->name
, info
, n
);
292 static int unpack_nondirectories(int n
, unsigned long mask
,
293 unsigned long dirmask
,
294 struct cache_entry
**src
,
295 const struct name_entry
*names
,
296 const struct traverse_info
*info
)
299 struct unpack_trees_options
*o
= info
->data
;
300 unsigned long conflicts
;
302 /* Do we have *only* directories? Nothing to do */
303 if (mask
== dirmask
&& !src
[0])
306 conflicts
= info
->conflicts
;
309 conflicts
|= dirmask
;
312 * Ok, we've filled in up to any potential index entry in src[0],
315 for (i
= 0; i
< n
; i
++) {
317 unsigned int bit
= 1ul << i
;
318 if (conflicts
& bit
) {
319 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
326 else if (i
+ 1 < o
->head_idx
)
328 else if (i
+ 1 > o
->head_idx
)
332 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
);
336 return call_unpack_fn(src
, o
);
338 for (i
= 0; i
< n
; i
++)
339 if (src
[i
] && src
[i
] != o
->df_conflict_entry
)
340 add_entry(o
, src
[i
], 0, 0);
344 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
346 discard_index(&o
->result
);
349 return error("%s", message
);
355 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
357 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
358 struct unpack_trees_options
*o
= info
->data
;
359 const struct name_entry
*p
= names
;
361 /* Find first entry with a real name (we could use "mask" too) */
365 /* Are we supposed to look at the index too? */
367 while (o
->pos
< o
->src_index
->cache_nr
) {
368 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
369 int cmp
= compare_entry(ce
, info
, p
);
371 if (unpack_index_entry(ce
, o
) < 0)
372 return unpack_failed(o
, NULL
);
379 * If we skip unmerged index entries, we'll skip this
380 * entry *and* the tree entries associated with it!
382 if (o
->skip_unmerged
) {
383 add_entry(o
, ce
, 0, 0);
393 if (unpack_nondirectories(n
, mask
, dirmask
, src
, names
, info
) < 0)
396 /* Now handle any directories.. */
398 unsigned long conflicts
= mask
& ~dirmask
;
405 /* special case: "diff-index --cached" looking at a tree */
406 if (o
->diff_index_cached
&&
407 n
== 1 && dirmask
== 1 && S_ISDIR(names
->mode
)) {
409 matches
= cache_tree_matches_traversal(o
->src_index
->cache_tree
,
412 * Everything under the name matches. Adjust o->pos to
413 * skip the entire hierarchy.
421 if (traverse_trees_recursive(n
, dirmask
, conflicts
,
431 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
432 * resulting index, -2 on failure to reflect the changes to the work tree.
434 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
437 static struct cache_entry
*dfc
;
438 struct exclude_list el
;
440 if (len
> MAX_UNPACK_TREES
)
441 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES
);
442 memset(&state
, 0, sizeof(state
));
446 state
.refresh_cache
= 1;
448 memset(&el
, 0, sizeof(el
));
449 if (!core_apply_sparse_checkout
|| !o
->update
)
450 o
->skip_sparse_checkout
= 1;
451 if (!o
->skip_sparse_checkout
) {
452 if (add_excludes_from_file_to_list(git_path("info/sparse-checkout"), "", 0, NULL
, &el
, 0) < 0)
453 o
->skip_sparse_checkout
= 1;
458 memset(&o
->result
, 0, sizeof(o
->result
));
459 o
->result
.initialized
= 1;
461 o
->result
.timestamp
.sec
= o
->src_index
->timestamp
.sec
;
462 o
->result
.timestamp
.nsec
= o
->src_index
->timestamp
.nsec
;
467 dfc
= xcalloc(1, cache_entry_size(0));
468 o
->df_conflict_entry
= dfc
;
471 const char *prefix
= o
->prefix
? o
->prefix
: "";
472 struct traverse_info info
;
474 setup_traverse_info(&info
, prefix
);
475 info
.fn
= unpack_callback
;
478 if (traverse_trees(len
, t
, &info
) < 0) {
479 ret
= unpack_failed(o
, NULL
);
484 /* Any left-over entries in the index? */
486 while (o
->pos
< o
->src_index
->cache_nr
) {
487 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
488 if (unpack_index_entry(ce
, o
) < 0) {
489 ret
= unpack_failed(o
, NULL
);
495 if (o
->trivial_merges_only
&& o
->nontrivial_merge
) {
496 ret
= unpack_failed(o
, "Merge requires file-level merging");
500 if (!o
->skip_sparse_checkout
) {
501 int empty_worktree
= 1;
502 for (i
= 0;i
< o
->result
.cache_nr
;i
++) {
503 struct cache_entry
*ce
= o
->result
.cache
[i
];
505 if (apply_sparse_checkout(ce
, o
)) {
510 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
511 * area as a result of ce_skip_worktree() shortcuts in
512 * verify_absent() and verify_uptodate(). Clear them.
514 if (ce_skip_worktree(ce
))
515 ce
->ce_flags
&= ~(CE_UPDATE
| CE_REMOVE
);
520 if (o
->result
.cache_nr
&& empty_worktree
) {
521 ret
= unpack_failed(o
, "Sparse checkout leaves no entry on working directory");
527 ret
= check_updates(o
) ? (-2) : 0;
529 *o
->dst_index
= o
->result
;
532 for (i
= 0;i
< el
.nr
;i
++)
533 free(el
.excludes
[i
]);
540 /* Here come the merge functions */
542 static int reject_merge(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
544 return error(ERRORMSG(o
, would_overwrite
), ce
->name
);
547 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
553 if ((a
->ce_flags
| b
->ce_flags
) & CE_CONFLICTED
)
555 return a
->ce_mode
== b
->ce_mode
&&
556 !hashcmp(a
->sha1
, b
->sha1
);
561 * When a CE gets turned into an unmerged entry, we
562 * want it to be up-to-date
564 static int verify_uptodate_1(struct cache_entry
*ce
,
565 struct unpack_trees_options
*o
,
566 const char *error_msg
)
570 if (o
->index_only
|| (!ce_skip_worktree(ce
) && (o
->reset
|| ce_uptodate(ce
))))
573 if (!lstat(ce
->name
, &st
)) {
574 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
578 * NEEDSWORK: the current default policy is to allow
579 * submodule to be out of sync wrt the supermodule
580 * index. This needs to be tightened later for
581 * submodules that are marked to be automatically
584 if (S_ISGITLINK(ce
->ce_mode
))
590 return o
->gently
? -1 :
591 error(error_msg
, ce
->name
);
594 static int verify_uptodate(struct cache_entry
*ce
,
595 struct unpack_trees_options
*o
)
597 if (!o
->skip_sparse_checkout
&& will_have_skip_worktree(ce
, o
))
599 return verify_uptodate_1(ce
, o
, ERRORMSG(o
, not_uptodate_file
));
602 static int verify_uptodate_sparse(struct cache_entry
*ce
,
603 struct unpack_trees_options
*o
)
605 return verify_uptodate_1(ce
, o
, ERRORMSG(o
, sparse_not_uptodate_file
));
608 static void invalidate_ce_path(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
611 cache_tree_invalidate_path(o
->src_index
->cache_tree
, ce
->name
);
615 * Check that checking out ce->sha1 in subdir ce->name is not
616 * going to overwrite any working files.
618 * Currently, git does not checkout subprojects during a superproject
619 * checkout, so it is not going to overwrite anything.
621 static int verify_clean_submodule(struct cache_entry
*ce
, const char *action
,
622 struct unpack_trees_options
*o
)
627 static int verify_clean_subdirectory(struct cache_entry
*ce
, const char *action
,
628 struct unpack_trees_options
*o
)
631 * we are about to extract "ce->name"; we would not want to lose
632 * anything in the existing directory there.
639 unsigned char sha1
[20];
641 if (S_ISGITLINK(ce
->ce_mode
) &&
642 resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) == 0) {
643 /* If we are not going to update the submodule, then
646 if (!hashcmp(sha1
, ce
->sha1
))
648 return verify_clean_submodule(ce
, action
, o
);
652 * First let's make sure we do not have a local modification
655 namelen
= strlen(ce
->name
);
656 for (i
= o
->pos
; i
< o
->src_index
->cache_nr
; i
++) {
657 struct cache_entry
*ce2
= o
->src_index
->cache
[i
];
658 int len
= ce_namelen(ce2
);
660 strncmp(ce
->name
, ce2
->name
, namelen
) ||
661 ce2
->name
[namelen
] != '/')
664 * ce2->name is an entry in the subdirectory.
666 if (!ce_stage(ce2
)) {
667 if (verify_uptodate(ce2
, o
))
669 add_entry(o
, ce2
, CE_REMOVE
, 0);
675 * Then we need to make sure that we do not lose a locally
676 * present file that is not ignored.
678 pathbuf
= xmalloc(namelen
+ 2);
679 memcpy(pathbuf
, ce
->name
, namelen
);
680 strcpy(pathbuf
+namelen
, "/");
682 memset(&d
, 0, sizeof(d
));
684 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
685 i
= read_directory(&d
, pathbuf
, namelen
+1, NULL
);
687 return o
->gently
? -1 :
688 error(ERRORMSG(o
, not_uptodate_dir
), ce
->name
);
694 * This gets called when there was no index entry for the tree entry 'dst',
695 * but we found a file in the working tree that 'lstat()' said was fine,
696 * and we're on a case-insensitive filesystem.
698 * See if we can find a case-insensitive match in the index that also
699 * matches the stat information, and assume it's that other file!
701 static int icase_exists(struct unpack_trees_options
*o
, struct cache_entry
*dst
, struct stat
*st
)
703 struct cache_entry
*src
;
705 src
= index_name_exists(o
->src_index
, dst
->name
, ce_namelen(dst
), 1);
706 return src
&& !ie_match_stat(o
->src_index
, src
, st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
710 * We do not want to remove or overwrite a working tree file that
711 * is not tracked, unless it is ignored.
713 static int verify_absent_1(struct cache_entry
*ce
, const char *action
,
714 struct unpack_trees_options
*o
,
715 const char *error_msg
)
719 if (o
->index_only
|| o
->reset
|| !o
->update
)
722 if (has_symlink_or_noent_leading_path(ce
->name
, ce_namelen(ce
)))
725 if (!lstat(ce
->name
, &st
)) {
727 int dtype
= ce_to_dtype(ce
);
728 struct cache_entry
*result
;
731 * It may be that the 'lstat()' succeeded even though
732 * target 'ce' was absent, because there is an old
733 * entry that is different only in case..
735 * Ignore that lstat() if it matches.
737 if (ignore_case
&& icase_exists(o
, ce
, &st
))
740 if (o
->dir
&& excluded(o
->dir
, ce
->name
, &dtype
))
742 * ce->name is explicitly excluded, so it is Ok to
746 if (S_ISDIR(st
.st_mode
)) {
748 * We are checking out path "foo" and
749 * found "foo/." in the working tree.
750 * This is tricky -- if we have modified
751 * files that are in "foo/" we would lose
754 ret
= verify_clean_subdirectory(ce
, action
, o
);
759 * If this removed entries from the index,
760 * what that means is:
762 * (1) the caller unpack_callback() saw path/foo
763 * in the index, and it has not removed it because
764 * it thinks it is handling 'path' as blob with
766 * (2) we will return "ok, we placed a merged entry
767 * in the index" which would cause o->pos to be
768 * incremented by one;
769 * (3) however, original o->pos now has 'path/foo'
770 * marked with "to be removed".
772 * We need to increment it by the number of
773 * deleted entries here.
780 * The previous round may already have decided to
781 * delete this path, which is in a subdirectory that
782 * is being replaced with a blob.
784 result
= index_name_exists(&o
->result
, ce
->name
, ce_namelen(ce
), 0);
786 if (result
->ce_flags
& CE_REMOVE
)
790 return o
->gently
? -1 :
791 error(ERRORMSG(o
, would_lose_untracked
), ce
->name
, action
);
795 static int verify_absent(struct cache_entry
*ce
, const char *action
,
796 struct unpack_trees_options
*o
)
798 if (!o
->skip_sparse_checkout
&& will_have_skip_worktree(ce
, o
))
800 return verify_absent_1(ce
, action
, o
, ERRORMSG(o
, would_lose_untracked
));
803 static int verify_absent_sparse(struct cache_entry
*ce
, const char *action
,
804 struct unpack_trees_options
*o
)
806 return verify_absent_1(ce
, action
, o
, ERRORMSG(o
, would_lose_orphaned
));
809 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
810 struct unpack_trees_options
*o
)
812 int update
= CE_UPDATE
;
815 if (verify_absent(merge
, "overwritten", o
))
817 invalidate_ce_path(merge
, o
);
818 } else if (!(old
->ce_flags
& CE_CONFLICTED
)) {
820 * See if we can re-use the old CE directly?
821 * That way we get the uptodate stat info.
823 * This also removes the UPDATE flag on a match; otherwise
824 * we will end up overwriting local changes in the work tree.
826 if (same(old
, merge
)) {
827 copy_cache_entry(merge
, old
);
830 if (verify_uptodate(old
, o
))
832 if (ce_skip_worktree(old
))
833 update
|= CE_SKIP_WORKTREE
;
834 invalidate_ce_path(old
, o
);
838 * Previously unmerged entry left as an existence
839 * marker by read_index_unmerged();
841 invalidate_ce_path(old
, o
);
844 add_entry(o
, merge
, update
, CE_STAGEMASK
);
848 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
849 struct unpack_trees_options
*o
)
851 /* Did it exist in the index? */
853 if (verify_absent(ce
, "removed", o
))
857 if (!(old
->ce_flags
& CE_CONFLICTED
) && verify_uptodate(old
, o
))
859 add_entry(o
, ce
, CE_REMOVE
, 0);
860 invalidate_ce_path(ce
, o
);
864 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
866 add_entry(o
, ce
, 0, 0);
871 static void show_stage_entry(FILE *o
,
872 const char *label
, const struct cache_entry
*ce
)
875 fprintf(o
, "%s (missing)\n", label
);
877 fprintf(o
, "%s%06o %s %d\t%s\n",
880 sha1_to_hex(ce
->sha1
),
886 int threeway_merge(struct cache_entry
**stages
, struct unpack_trees_options
*o
)
888 struct cache_entry
*index
;
889 struct cache_entry
*head
;
890 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
893 int remote_match
= 0;
895 int df_conflict_head
= 0;
896 int df_conflict_remote
= 0;
898 int any_anc_missing
= 0;
899 int no_anc_exists
= 1;
902 for (i
= 1; i
< o
->head_idx
; i
++) {
903 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
910 head
= stages
[o
->head_idx
];
912 if (head
== o
->df_conflict_entry
) {
913 df_conflict_head
= 1;
917 if (remote
== o
->df_conflict_entry
) {
918 df_conflict_remote
= 1;
922 /* First, if there's a #16 situation, note that to prevent #13
925 if (!same(remote
, head
)) {
926 for (i
= 1; i
< o
->head_idx
; i
++) {
927 if (same(stages
[i
], head
)) {
930 if (same(stages
[i
], remote
)) {
936 /* We start with cases where the index is allowed to match
937 * something other than the head: #14(ALT) and #2ALT, where it
938 * is permitted to match the result instead.
940 /* #14, #14ALT, #2ALT */
941 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
942 if (index
&& !same(index
, remote
) && !same(index
, head
))
943 return o
->gently
? -1 : reject_merge(index
, o
);
944 return merged_entry(remote
, index
, o
);
947 * If we have an entry in the index cache, then we want to
948 * make sure that it matches head.
950 if (index
&& !same(index
, head
))
951 return o
->gently
? -1 : reject_merge(index
, o
);
955 if (same(head
, remote
))
956 return merged_entry(head
, index
, o
);
958 if (!df_conflict_remote
&& remote_match
&& !head_match
)
959 return merged_entry(head
, index
, o
);
963 if (!head
&& !remote
&& any_anc_missing
)
966 /* Under the new "aggressive" rule, we resolve mostly trivial
967 * cases that we historically had git-merge-one-file resolve.
970 int head_deleted
= !head
&& !df_conflict_head
;
971 int remote_deleted
= !remote
&& !df_conflict_remote
;
972 struct cache_entry
*ce
= NULL
;
981 for (i
= 1; i
< o
->head_idx
; i
++) {
982 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
991 * Deleted in one and unchanged in the other.
993 if ((head_deleted
&& remote_deleted
) ||
994 (head_deleted
&& remote
&& remote_match
) ||
995 (remote_deleted
&& head
&& head_match
)) {
997 return deleted_entry(index
, index
, o
);
998 if (ce
&& !head_deleted
) {
999 if (verify_absent(ce
, "removed", o
))
1005 * Added in both, identically.
1007 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
1008 return merged_entry(head
, index
, o
);
1012 /* Below are "no merge" cases, which require that the index be
1013 * up-to-date to avoid the files getting overwritten with
1014 * conflict resolution files.
1017 if (verify_uptodate(index
, o
))
1021 o
->nontrivial_merge
= 1;
1023 /* #2, #3, #4, #6, #7, #9, #10, #11. */
1025 if (!head_match
|| !remote_match
) {
1026 for (i
= 1; i
< o
->head_idx
; i
++) {
1027 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
1028 keep_entry(stages
[i
], o
);
1036 fprintf(stderr
, "read-tree: warning #16 detected\n");
1037 show_stage_entry(stderr
, "head ", stages
[head_match
]);
1038 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
1041 if (head
) { count
+= keep_entry(head
, o
); }
1042 if (remote
) { count
+= keep_entry(remote
, o
); }
1049 * The rule is to "carry forward" what is in the index without losing
1050 * information across a "fast-forward", favoring a successful merge
1051 * over a merge failure when it makes sense. For details of the
1052 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
1055 int twoway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
1057 struct cache_entry
*current
= src
[0];
1058 struct cache_entry
*oldtree
= src
[1];
1059 struct cache_entry
*newtree
= src
[2];
1061 if (o
->merge_size
!= 2)
1062 return error("Cannot do a twoway merge of %d trees",
1065 if (oldtree
== o
->df_conflict_entry
)
1067 if (newtree
== o
->df_conflict_entry
)
1071 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
1072 (!oldtree
&& newtree
&&
1073 same(current
, newtree
)) || /* 6 and 7 */
1074 (oldtree
&& newtree
&&
1075 same(oldtree
, newtree
)) || /* 14 and 15 */
1076 (oldtree
&& newtree
&&
1077 !same(oldtree
, newtree
) && /* 18 and 19 */
1078 same(current
, newtree
))) {
1079 return keep_entry(current
, o
);
1081 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
1083 return deleted_entry(oldtree
, current
, o
);
1085 else if (oldtree
&& newtree
&&
1086 same(current
, oldtree
) && !same(current
, newtree
)) {
1088 return merged_entry(newtree
, current
, o
);
1091 /* all other failures */
1093 return o
->gently
? -1 : reject_merge(oldtree
, o
);
1095 return o
->gently
? -1 : reject_merge(current
, o
);
1097 return o
->gently
? -1 : reject_merge(newtree
, o
);
1102 if (oldtree
&& !o
->initial_checkout
) {
1104 * deletion of the path was staged;
1106 if (same(oldtree
, newtree
))
1108 return reject_merge(oldtree
, o
);
1110 return merged_entry(newtree
, current
, o
);
1112 return deleted_entry(oldtree
, current
, o
);
1118 * Keep the index entries at stage0, collapse stage1 but make sure
1119 * stage0 does not have anything there.
1121 int bind_merge(struct cache_entry
**src
,
1122 struct unpack_trees_options
*o
)
1124 struct cache_entry
*old
= src
[0];
1125 struct cache_entry
*a
= src
[1];
1127 if (o
->merge_size
!= 1)
1128 return error("Cannot do a bind merge of %d trees\n",
1131 return o
->gently
? -1 :
1132 error(ERRORMSG(o
, bind_overlap
), a
->name
, old
->name
);
1134 return keep_entry(old
, o
);
1136 return merged_entry(a
, NULL
, o
);
1143 * - take the stat information from stage0, take the data from stage1
1145 int oneway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
1147 struct cache_entry
*old
= src
[0];
1148 struct cache_entry
*a
= src
[1];
1150 if (o
->merge_size
!= 1)
1151 return error("Cannot do a oneway merge of %d trees",
1154 if (!a
|| a
== o
->df_conflict_entry
)
1155 return deleted_entry(old
, old
, o
);
1157 if (old
&& same(old
, a
)) {
1159 if (o
->reset
&& !ce_uptodate(old
) && !ce_skip_worktree(old
)) {
1161 if (lstat(old
->name
, &st
) ||
1162 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
))
1163 update
|= CE_UPDATE
;
1165 add_entry(o
, old
, update
, 0);
1168 return merged_entry(a
, old
, o
);