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 (S_ISGITLINK(ce
->ce_mode
)) {
71 if (rmdir(ce
->name
)) {
72 warning("unable to rmdir %s: %s",
73 ce
->name
, strerror(errno
));
78 if (unlink_or_warn(ce
->name
))
80 schedule_dir_for_removal(ce
->name
, ce_namelen(ce
));
83 static struct checkout state
;
84 static int check_updates(struct unpack_trees_options
*o
)
86 unsigned cnt
= 0, total
= 0;
87 struct progress
*progress
= NULL
;
88 struct index_state
*index
= &o
->result
;
92 if (o
->update
&& o
->verbose_update
) {
93 for (total
= cnt
= 0; cnt
< index
->cache_nr
; cnt
++) {
94 struct cache_entry
*ce
= index
->cache
[cnt
];
95 if (ce
->ce_flags
& (CE_UPDATE
| CE_REMOVE
| CE_WT_REMOVE
))
99 progress
= start_progress_delay("Checking out files",
105 git_attr_set_direction(GIT_ATTR_CHECKOUT
, &o
->result
);
106 for (i
= 0; i
< index
->cache_nr
; i
++) {
107 struct cache_entry
*ce
= index
->cache
[i
];
109 if (ce
->ce_flags
& CE_WT_REMOVE
) {
110 display_progress(progress
, ++cnt
);
116 if (ce
->ce_flags
& CE_REMOVE
) {
117 display_progress(progress
, ++cnt
);
122 remove_marked_cache_entries(&o
->result
);
123 remove_scheduled_dirs();
125 for (i
= 0; i
< index
->cache_nr
; i
++) {
126 struct cache_entry
*ce
= index
->cache
[i
];
128 if (ce
->ce_flags
& CE_UPDATE
) {
129 display_progress(progress
, ++cnt
);
130 ce
->ce_flags
&= ~CE_UPDATE
;
132 errs
|= checkout_entry(ce
, &state
, NULL
);
136 stop_progress(&progress
);
138 git_attr_set_direction(GIT_ATTR_CHECKIN
, NULL
);
142 static int verify_uptodate_sparse(struct cache_entry
*ce
, struct unpack_trees_options
*o
);
143 static int verify_absent_sparse(struct cache_entry
*ce
, const char *action
, struct unpack_trees_options
*o
);
145 static int will_have_skip_worktree(const struct cache_entry
*ce
, struct unpack_trees_options
*o
)
147 const char *basename
;
152 basename
= strrchr(ce
->name
, '/');
153 basename
= basename
? basename
+1 : ce
->name
;
154 return excluded_from_list(ce
->name
, ce_namelen(ce
), basename
, NULL
, o
->el
) <= 0;
157 static int apply_sparse_checkout(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
159 int was_skip_worktree
= ce_skip_worktree(ce
);
161 if (will_have_skip_worktree(ce
, o
))
162 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
164 ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
167 * We only care about files getting into the checkout area
168 * If merge strategies want to remove some, go ahead, this
169 * flag will be removed eventually in unpack_trees() if it's
170 * outside checkout area.
172 if (ce
->ce_flags
& CE_REMOVE
)
175 if (!was_skip_worktree
&& ce_skip_worktree(ce
)) {
177 * If CE_UPDATE is set, verify_uptodate() must be called already
178 * also stat info may have lost after merged_entry() so calling
179 * verify_uptodate() again may fail
181 if (!(ce
->ce_flags
& CE_UPDATE
) && verify_uptodate_sparse(ce
, o
))
183 ce
->ce_flags
|= CE_WT_REMOVE
;
185 if (was_skip_worktree
&& !ce_skip_worktree(ce
)) {
186 if (verify_absent_sparse(ce
, "overwritten", o
))
188 ce
->ce_flags
|= CE_UPDATE
;
193 static inline int call_unpack_fn(struct cache_entry
**src
, struct unpack_trees_options
*o
)
195 int ret
= o
->fn(src
, o
);
201 static int unpack_index_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
203 struct cache_entry
*src
[5] = { ce
, NULL
, };
207 if (o
->skip_unmerged
) {
208 add_entry(o
, ce
, 0, 0);
212 return call_unpack_fn(src
, o
);
215 static int traverse_trees_recursive(int n
, unsigned long dirmask
, unsigned long df_conflicts
, struct name_entry
*names
, struct traverse_info
*info
)
218 struct tree_desc t
[MAX_UNPACK_TREES
];
219 struct traverse_info newinfo
;
220 struct name_entry
*p
;
229 newinfo
.pathlen
+= tree_entry_len(p
->path
, p
->sha1
) + 1;
230 newinfo
.conflicts
|= df_conflicts
;
232 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
233 const unsigned char *sha1
= NULL
;
235 sha1
= names
[i
].sha1
;
236 fill_tree_descriptor(t
+i
, sha1
);
238 return traverse_trees(n
, t
, &newinfo
);
242 * Compare the traverse-path to the cache entry without actually
243 * having to generate the textual representation of the traverse
246 * NOTE! This *only* compares up to the size of the traverse path
247 * itself - the caller needs to do the final check for the cache
248 * entry having more data at the end!
250 static int do_compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
252 int len
, pathlen
, ce_len
;
256 int cmp
= do_compare_entry(ce
, info
->prev
, &info
->name
);
260 pathlen
= info
->pathlen
;
261 ce_len
= ce_namelen(ce
);
263 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
264 if (ce_len
< pathlen
)
268 ce_name
= ce
->name
+ pathlen
;
270 len
= tree_entry_len(n
->path
, n
->sha1
);
271 return df_name_compare(ce_name
, ce_len
, S_IFREG
, n
->path
, len
, n
->mode
);
274 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
276 int cmp
= do_compare_entry(ce
, info
, n
);
281 * Even if the beginning compared identically, the ce should
282 * compare as bigger than a directory leading up to it!
284 return ce_namelen(ce
) > traverse_path_len(info
, n
);
287 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
, const struct name_entry
*n
, int stage
)
289 int len
= traverse_path_len(info
, n
);
290 struct cache_entry
*ce
= xcalloc(1, cache_entry_size(len
));
292 ce
->ce_mode
= create_ce_mode(n
->mode
);
293 ce
->ce_flags
= create_ce_flags(len
, stage
);
294 hashcpy(ce
->sha1
, n
->sha1
);
295 make_traverse_path(ce
->name
, info
, n
);
300 static int unpack_nondirectories(int n
, unsigned long mask
,
301 unsigned long dirmask
,
302 struct cache_entry
**src
,
303 const struct name_entry
*names
,
304 const struct traverse_info
*info
)
307 struct unpack_trees_options
*o
= info
->data
;
308 unsigned long conflicts
;
310 /* Do we have *only* directories? Nothing to do */
311 if (mask
== dirmask
&& !src
[0])
314 conflicts
= info
->conflicts
;
317 conflicts
|= dirmask
;
320 * Ok, we've filled in up to any potential index entry in src[0],
323 for (i
= 0; i
< n
; i
++) {
325 unsigned int bit
= 1ul << i
;
326 if (conflicts
& bit
) {
327 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
334 else if (i
+ 1 < o
->head_idx
)
336 else if (i
+ 1 > o
->head_idx
)
340 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
);
344 return call_unpack_fn(src
, o
);
346 for (i
= 0; i
< n
; i
++)
347 if (src
[i
] && src
[i
] != o
->df_conflict_entry
)
348 add_entry(o
, src
[i
], 0, 0);
352 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
354 discard_index(&o
->result
);
357 return error("%s", message
);
363 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
365 struct cache_entry
*src
[MAX_UNPACK_TREES
+ 1] = { NULL
, };
366 struct unpack_trees_options
*o
= info
->data
;
367 const struct name_entry
*p
= names
;
369 /* Find first entry with a real name (we could use "mask" too) */
373 /* Are we supposed to look at the index too? */
375 while (o
->pos
< o
->src_index
->cache_nr
) {
376 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
377 int cmp
= compare_entry(ce
, info
, p
);
379 if (unpack_index_entry(ce
, o
) < 0)
380 return unpack_failed(o
, NULL
);
387 * If we skip unmerged index entries, we'll skip this
388 * entry *and* the tree entries associated with it!
390 if (o
->skip_unmerged
) {
391 add_entry(o
, ce
, 0, 0);
401 if (unpack_nondirectories(n
, mask
, dirmask
, src
, names
, info
) < 0)
404 /* Now handle any directories.. */
406 unsigned long conflicts
= mask
& ~dirmask
;
413 /* special case: "diff-index --cached" looking at a tree */
414 if (o
->diff_index_cached
&&
415 n
== 1 && dirmask
== 1 && S_ISDIR(names
->mode
)) {
417 matches
= cache_tree_matches_traversal(o
->src_index
->cache_tree
,
420 * Everything under the name matches. Adjust o->pos to
421 * skip the entire hierarchy.
429 if (traverse_trees_recursive(n
, dirmask
, conflicts
,
439 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
440 * resulting index, -2 on failure to reflect the changes to the work tree.
442 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
445 static struct cache_entry
*dfc
;
446 struct exclude_list el
;
448 if (len
> MAX_UNPACK_TREES
)
449 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES
);
450 memset(&state
, 0, sizeof(state
));
454 state
.refresh_cache
= 1;
456 memset(&el
, 0, sizeof(el
));
457 if (!core_apply_sparse_checkout
|| !o
->update
)
458 o
->skip_sparse_checkout
= 1;
459 if (!o
->skip_sparse_checkout
) {
460 if (add_excludes_from_file_to_list(git_path("info/sparse-checkout"), "", 0, NULL
, &el
, 0) < 0)
461 o
->skip_sparse_checkout
= 1;
466 memset(&o
->result
, 0, sizeof(o
->result
));
467 o
->result
.initialized
= 1;
469 o
->result
.timestamp
.sec
= o
->src_index
->timestamp
.sec
;
470 o
->result
.timestamp
.nsec
= o
->src_index
->timestamp
.nsec
;
475 dfc
= xcalloc(1, cache_entry_size(0));
476 o
->df_conflict_entry
= dfc
;
479 const char *prefix
= o
->prefix
? o
->prefix
: "";
480 struct traverse_info info
;
482 setup_traverse_info(&info
, prefix
);
483 info
.fn
= unpack_callback
;
486 if (traverse_trees(len
, t
, &info
) < 0) {
487 ret
= unpack_failed(o
, NULL
);
492 /* Any left-over entries in the index? */
494 while (o
->pos
< o
->src_index
->cache_nr
) {
495 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
496 if (unpack_index_entry(ce
, o
) < 0) {
497 ret
= unpack_failed(o
, NULL
);
503 if (o
->trivial_merges_only
&& o
->nontrivial_merge
) {
504 ret
= unpack_failed(o
, "Merge requires file-level merging");
508 if (!o
->skip_sparse_checkout
) {
509 int empty_worktree
= 1;
510 for (i
= 0;i
< o
->result
.cache_nr
;i
++) {
511 struct cache_entry
*ce
= o
->result
.cache
[i
];
513 if (apply_sparse_checkout(ce
, o
)) {
518 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
519 * area as a result of ce_skip_worktree() shortcuts in
520 * verify_absent() and verify_uptodate(). Clear them.
522 if (ce_skip_worktree(ce
))
523 ce
->ce_flags
&= ~(CE_UPDATE
| CE_REMOVE
);
528 if (o
->result
.cache_nr
&& empty_worktree
) {
529 ret
= unpack_failed(o
, "Sparse checkout leaves no entry on working directory");
535 ret
= check_updates(o
) ? (-2) : 0;
537 *o
->dst_index
= o
->result
;
540 for (i
= 0;i
< el
.nr
;i
++)
541 free(el
.excludes
[i
]);
548 /* Here come the merge functions */
550 static int reject_merge(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
552 return error(ERRORMSG(o
, would_overwrite
), ce
->name
);
555 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
561 if ((a
->ce_flags
| b
->ce_flags
) & CE_CONFLICTED
)
563 return a
->ce_mode
== b
->ce_mode
&&
564 !hashcmp(a
->sha1
, b
->sha1
);
569 * When a CE gets turned into an unmerged entry, we
570 * want it to be up-to-date
572 static int verify_uptodate_1(struct cache_entry
*ce
,
573 struct unpack_trees_options
*o
,
574 const char *error_msg
)
578 if (o
->index_only
|| (!ce_skip_worktree(ce
) && (o
->reset
|| ce_uptodate(ce
))))
581 if (!lstat(ce
->name
, &st
)) {
582 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
586 * NEEDSWORK: the current default policy is to allow
587 * submodule to be out of sync wrt the supermodule
588 * index. This needs to be tightened later for
589 * submodules that are marked to be automatically
592 if (S_ISGITLINK(ce
->ce_mode
))
598 return o
->gently
? -1 :
599 error(error_msg
, ce
->name
);
602 static int verify_uptodate(struct cache_entry
*ce
,
603 struct unpack_trees_options
*o
)
605 if (!o
->skip_sparse_checkout
&& will_have_skip_worktree(ce
, o
))
607 return verify_uptodate_1(ce
, o
, ERRORMSG(o
, not_uptodate_file
));
610 static int verify_uptodate_sparse(struct cache_entry
*ce
,
611 struct unpack_trees_options
*o
)
613 return verify_uptodate_1(ce
, o
, ERRORMSG(o
, sparse_not_uptodate_file
));
616 static void invalidate_ce_path(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
619 cache_tree_invalidate_path(o
->src_index
->cache_tree
, ce
->name
);
623 * Check that checking out ce->sha1 in subdir ce->name is not
624 * going to overwrite any working files.
626 * Currently, git does not checkout subprojects during a superproject
627 * checkout, so it is not going to overwrite anything.
629 static int verify_clean_submodule(struct cache_entry
*ce
, const char *action
,
630 struct unpack_trees_options
*o
)
635 static int verify_clean_subdirectory(struct cache_entry
*ce
, const char *action
,
636 struct unpack_trees_options
*o
)
639 * we are about to extract "ce->name"; we would not want to lose
640 * anything in the existing directory there.
647 unsigned char sha1
[20];
649 if (S_ISGITLINK(ce
->ce_mode
) &&
650 resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) == 0) {
651 /* If we are not going to update the submodule, then
654 if (!hashcmp(sha1
, ce
->sha1
))
656 return verify_clean_submodule(ce
, action
, o
);
660 * First let's make sure we do not have a local modification
663 namelen
= strlen(ce
->name
);
664 for (i
= o
->pos
; i
< o
->src_index
->cache_nr
; i
++) {
665 struct cache_entry
*ce2
= o
->src_index
->cache
[i
];
666 int len
= ce_namelen(ce2
);
668 strncmp(ce
->name
, ce2
->name
, namelen
) ||
669 ce2
->name
[namelen
] != '/')
672 * ce2->name is an entry in the subdirectory.
674 if (!ce_stage(ce2
)) {
675 if (verify_uptodate(ce2
, o
))
677 add_entry(o
, ce2
, CE_REMOVE
, 0);
683 * Then we need to make sure that we do not lose a locally
684 * present file that is not ignored.
686 pathbuf
= xmalloc(namelen
+ 2);
687 memcpy(pathbuf
, ce
->name
, namelen
);
688 strcpy(pathbuf
+namelen
, "/");
690 memset(&d
, 0, sizeof(d
));
692 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
693 i
= read_directory(&d
, pathbuf
, namelen
+1, NULL
);
695 return o
->gently
? -1 :
696 error(ERRORMSG(o
, not_uptodate_dir
), ce
->name
);
702 * This gets called when there was no index entry for the tree entry 'dst',
703 * but we found a file in the working tree that 'lstat()' said was fine,
704 * and we're on a case-insensitive filesystem.
706 * See if we can find a case-insensitive match in the index that also
707 * matches the stat information, and assume it's that other file!
709 static int icase_exists(struct unpack_trees_options
*o
, struct cache_entry
*dst
, struct stat
*st
)
711 struct cache_entry
*src
;
713 src
= index_name_exists(o
->src_index
, dst
->name
, ce_namelen(dst
), 1);
714 return src
&& !ie_match_stat(o
->src_index
, src
, st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
718 * We do not want to remove or overwrite a working tree file that
719 * is not tracked, unless it is ignored.
721 static int verify_absent_1(struct cache_entry
*ce
, const char *action
,
722 struct unpack_trees_options
*o
,
723 const char *error_msg
)
727 if (o
->index_only
|| o
->reset
|| !o
->update
)
730 if (has_symlink_or_noent_leading_path(ce
->name
, ce_namelen(ce
)))
733 if (!lstat(ce
->name
, &st
)) {
735 int dtype
= ce_to_dtype(ce
);
736 struct cache_entry
*result
;
739 * It may be that the 'lstat()' succeeded even though
740 * target 'ce' was absent, because there is an old
741 * entry that is different only in case..
743 * Ignore that lstat() if it matches.
745 if (ignore_case
&& icase_exists(o
, ce
, &st
))
748 if (o
->dir
&& excluded(o
->dir
, ce
->name
, &dtype
))
750 * ce->name is explicitly excluded, so it is Ok to
754 if (S_ISDIR(st
.st_mode
)) {
756 * We are checking out path "foo" and
757 * found "foo/." in the working tree.
758 * This is tricky -- if we have modified
759 * files that are in "foo/" we would lose
762 ret
= verify_clean_subdirectory(ce
, action
, o
);
767 * If this removed entries from the index,
768 * what that means is:
770 * (1) the caller unpack_callback() saw path/foo
771 * in the index, and it has not removed it because
772 * it thinks it is handling 'path' as blob with
774 * (2) we will return "ok, we placed a merged entry
775 * in the index" which would cause o->pos to be
776 * incremented by one;
777 * (3) however, original o->pos now has 'path/foo'
778 * marked with "to be removed".
780 * We need to increment it by the number of
781 * deleted entries here.
788 * The previous round may already have decided to
789 * delete this path, which is in a subdirectory that
790 * is being replaced with a blob.
792 result
= index_name_exists(&o
->result
, ce
->name
, ce_namelen(ce
), 0);
794 if (result
->ce_flags
& CE_REMOVE
)
798 return o
->gently
? -1 :
799 error(ERRORMSG(o
, would_lose_untracked
), ce
->name
, action
);
803 static int verify_absent(struct cache_entry
*ce
, const char *action
,
804 struct unpack_trees_options
*o
)
806 if (!o
->skip_sparse_checkout
&& will_have_skip_worktree(ce
, o
))
808 return verify_absent_1(ce
, action
, o
, ERRORMSG(o
, would_lose_untracked
));
811 static int verify_absent_sparse(struct cache_entry
*ce
, const char *action
,
812 struct unpack_trees_options
*o
)
814 return verify_absent_1(ce
, action
, o
, ERRORMSG(o
, would_lose_orphaned
));
817 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
818 struct unpack_trees_options
*o
)
820 int update
= CE_UPDATE
;
823 if (verify_absent(merge
, "overwritten", o
))
825 invalidate_ce_path(merge
, o
);
826 } else if (!(old
->ce_flags
& CE_CONFLICTED
)) {
828 * See if we can re-use the old CE directly?
829 * That way we get the uptodate stat info.
831 * This also removes the UPDATE flag on a match; otherwise
832 * we will end up overwriting local changes in the work tree.
834 if (same(old
, merge
)) {
835 copy_cache_entry(merge
, old
);
838 if (verify_uptodate(old
, o
))
840 if (ce_skip_worktree(old
))
841 update
|= CE_SKIP_WORKTREE
;
842 invalidate_ce_path(old
, o
);
846 * Previously unmerged entry left as an existence
847 * marker by read_index_unmerged();
849 invalidate_ce_path(old
, o
);
852 add_entry(o
, merge
, update
, CE_STAGEMASK
);
856 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
857 struct unpack_trees_options
*o
)
859 /* Did it exist in the index? */
861 if (verify_absent(ce
, "removed", o
))
865 if (!(old
->ce_flags
& CE_CONFLICTED
) && verify_uptodate(old
, o
))
867 add_entry(o
, ce
, CE_REMOVE
, 0);
868 invalidate_ce_path(ce
, o
);
872 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
874 add_entry(o
, ce
, 0, 0);
879 static void show_stage_entry(FILE *o
,
880 const char *label
, const struct cache_entry
*ce
)
883 fprintf(o
, "%s (missing)\n", label
);
885 fprintf(o
, "%s%06o %s %d\t%s\n",
888 sha1_to_hex(ce
->sha1
),
894 int threeway_merge(struct cache_entry
**stages
, struct unpack_trees_options
*o
)
896 struct cache_entry
*index
;
897 struct cache_entry
*head
;
898 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
901 int remote_match
= 0;
903 int df_conflict_head
= 0;
904 int df_conflict_remote
= 0;
906 int any_anc_missing
= 0;
907 int no_anc_exists
= 1;
910 for (i
= 1; i
< o
->head_idx
; i
++) {
911 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
918 head
= stages
[o
->head_idx
];
920 if (head
== o
->df_conflict_entry
) {
921 df_conflict_head
= 1;
925 if (remote
== o
->df_conflict_entry
) {
926 df_conflict_remote
= 1;
930 /* First, if there's a #16 situation, note that to prevent #13
933 if (!same(remote
, head
)) {
934 for (i
= 1; i
< o
->head_idx
; i
++) {
935 if (same(stages
[i
], head
)) {
938 if (same(stages
[i
], remote
)) {
944 /* We start with cases where the index is allowed to match
945 * something other than the head: #14(ALT) and #2ALT, where it
946 * is permitted to match the result instead.
948 /* #14, #14ALT, #2ALT */
949 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
950 if (index
&& !same(index
, remote
) && !same(index
, head
))
951 return o
->gently
? -1 : reject_merge(index
, o
);
952 return merged_entry(remote
, index
, o
);
955 * If we have an entry in the index cache, then we want to
956 * make sure that it matches head.
958 if (index
&& !same(index
, head
))
959 return o
->gently
? -1 : reject_merge(index
, o
);
963 if (same(head
, remote
))
964 return merged_entry(head
, index
, o
);
966 if (!df_conflict_remote
&& remote_match
&& !head_match
)
967 return merged_entry(head
, index
, o
);
971 if (!head
&& !remote
&& any_anc_missing
)
974 /* Under the new "aggressive" rule, we resolve mostly trivial
975 * cases that we historically had git-merge-one-file resolve.
978 int head_deleted
= !head
&& !df_conflict_head
;
979 int remote_deleted
= !remote
&& !df_conflict_remote
;
980 struct cache_entry
*ce
= NULL
;
989 for (i
= 1; i
< o
->head_idx
; i
++) {
990 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
999 * Deleted in one and unchanged in the other.
1001 if ((head_deleted
&& remote_deleted
) ||
1002 (head_deleted
&& remote
&& remote_match
) ||
1003 (remote_deleted
&& head
&& head_match
)) {
1005 return deleted_entry(index
, index
, o
);
1006 if (ce
&& !head_deleted
) {
1007 if (verify_absent(ce
, "removed", o
))
1013 * Added in both, identically.
1015 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
1016 return merged_entry(head
, index
, o
);
1020 /* Below are "no merge" cases, which require that the index be
1021 * up-to-date to avoid the files getting overwritten with
1022 * conflict resolution files.
1025 if (verify_uptodate(index
, o
))
1029 o
->nontrivial_merge
= 1;
1031 /* #2, #3, #4, #6, #7, #9, #10, #11. */
1033 if (!head_match
|| !remote_match
) {
1034 for (i
= 1; i
< o
->head_idx
; i
++) {
1035 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
1036 keep_entry(stages
[i
], o
);
1044 fprintf(stderr
, "read-tree: warning #16 detected\n");
1045 show_stage_entry(stderr
, "head ", stages
[head_match
]);
1046 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
1049 if (head
) { count
+= keep_entry(head
, o
); }
1050 if (remote
) { count
+= keep_entry(remote
, o
); }
1057 * The rule is to "carry forward" what is in the index without losing
1058 * information across a "fast-forward", favoring a successful merge
1059 * over a merge failure when it makes sense. For details of the
1060 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
1063 int twoway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
1065 struct cache_entry
*current
= src
[0];
1066 struct cache_entry
*oldtree
= src
[1];
1067 struct cache_entry
*newtree
= src
[2];
1069 if (o
->merge_size
!= 2)
1070 return error("Cannot do a twoway merge of %d trees",
1073 if (oldtree
== o
->df_conflict_entry
)
1075 if (newtree
== o
->df_conflict_entry
)
1079 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
1080 (!oldtree
&& newtree
&&
1081 same(current
, newtree
)) || /* 6 and 7 */
1082 (oldtree
&& newtree
&&
1083 same(oldtree
, newtree
)) || /* 14 and 15 */
1084 (oldtree
&& newtree
&&
1085 !same(oldtree
, newtree
) && /* 18 and 19 */
1086 same(current
, newtree
))) {
1087 return keep_entry(current
, o
);
1089 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
1091 return deleted_entry(oldtree
, current
, o
);
1093 else if (oldtree
&& newtree
&&
1094 same(current
, oldtree
) && !same(current
, newtree
)) {
1096 return merged_entry(newtree
, current
, o
);
1099 /* all other failures */
1101 return o
->gently
? -1 : reject_merge(oldtree
, o
);
1103 return o
->gently
? -1 : reject_merge(current
, o
);
1105 return o
->gently
? -1 : reject_merge(newtree
, o
);
1110 if (oldtree
&& !o
->initial_checkout
) {
1112 * deletion of the path was staged;
1114 if (same(oldtree
, newtree
))
1116 return reject_merge(oldtree
, o
);
1118 return merged_entry(newtree
, current
, o
);
1120 return deleted_entry(oldtree
, current
, o
);
1126 * Keep the index entries at stage0, collapse stage1 but make sure
1127 * stage0 does not have anything there.
1129 int bind_merge(struct cache_entry
**src
,
1130 struct unpack_trees_options
*o
)
1132 struct cache_entry
*old
= src
[0];
1133 struct cache_entry
*a
= src
[1];
1135 if (o
->merge_size
!= 1)
1136 return error("Cannot do a bind merge of %d trees\n",
1139 return o
->gently
? -1 :
1140 error(ERRORMSG(o
, bind_overlap
), a
->name
, old
->name
);
1142 return keep_entry(old
, o
);
1144 return merged_entry(a
, NULL
, o
);
1151 * - take the stat information from stage0, take the data from stage1
1153 int oneway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
1155 struct cache_entry
*old
= src
[0];
1156 struct cache_entry
*a
= src
[1];
1158 if (o
->merge_size
!= 1)
1159 return error("Cannot do a oneway merge of %d trees",
1162 if (!a
|| a
== o
->df_conflict_entry
)
1163 return deleted_entry(old
, old
, o
);
1165 if (old
&& same(old
, a
)) {
1167 if (o
->reset
&& !ce_uptodate(old
) && !ce_skip_worktree(old
)) {
1169 if (lstat(old
->name
, &st
) ||
1170 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
))
1171 update
|= CE_UPDATE
;
1173 add_entry(o
, old
, update
, 0);
1176 return merged_entry(a
, old
, o
);