1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
6 #include "cache-tree.h"
7 #include "unpack-trees.h"
11 static void add_entry(struct unpack_trees_options
*o
, struct cache_entry
*ce
,
12 unsigned int set
, unsigned int clear
)
14 unsigned int size
= ce_size(ce
);
15 struct cache_entry
*new = xmalloc(size
);
17 clear
|= CE_HASHED
| CE_UNHASHED
;
19 memcpy(new, ce
, size
);
21 new->ce_flags
= (new->ce_flags
& ~clear
) | set
;
22 add_index_entry(&o
->result
, new, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
|ADD_CACHE_SKIP_DFCHECK
);
25 /* Unlink the last component and attempt to remove leading
26 * directories, in case this unlink is the removal of the
27 * last entry in the directory -- empty directories are removed.
29 static void unlink_entry(char *name
, char *last_symlink
)
33 if (has_symlink_leading_path(name
, last_symlink
))
40 cp
= strrchr(name
, '/');
56 static struct checkout state
;
57 static void check_updates(struct unpack_trees_options
*o
)
59 unsigned cnt
= 0, total
= 0;
60 struct progress
*progress
= NULL
;
61 char last_symlink
[PATH_MAX
];
62 struct index_state
*index
= &o
->result
;
65 if (o
->update
&& o
->verbose_update
) {
66 for (total
= cnt
= 0; cnt
< index
->cache_nr
; cnt
++) {
67 struct cache_entry
*ce
= index
->cache
[cnt
];
68 if (ce
->ce_flags
& (CE_UPDATE
| CE_REMOVE
))
72 progress
= start_progress_delay("Checking out files",
78 for (i
= 0; i
< index
->cache_nr
; i
++) {
79 struct cache_entry
*ce
= index
->cache
[i
];
81 if (ce
->ce_flags
& (CE_UPDATE
| CE_REMOVE
))
82 display_progress(progress
, ++cnt
);
83 if (ce
->ce_flags
& CE_REMOVE
) {
85 unlink_entry(ce
->name
, last_symlink
);
86 remove_index_entry_at(&o
->result
, i
);
90 if (ce
->ce_flags
& CE_UPDATE
) {
91 ce
->ce_flags
&= ~CE_UPDATE
;
93 checkout_entry(ce
, &state
, NULL
);
98 stop_progress(&progress
);
101 static inline int call_unpack_fn(struct cache_entry
**src
, struct unpack_trees_options
*o
)
103 int ret
= o
->fn(src
, o
);
109 static int unpack_index_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
111 struct cache_entry
*src
[5] = { ce
, };
115 if (o
->skip_unmerged
) {
116 add_entry(o
, ce
, 0, 0);
120 return call_unpack_fn(src
, o
);
123 int traverse_trees_recursive(int n
, unsigned long dirmask
, unsigned long df_conflicts
, struct name_entry
*names
, struct traverse_info
*info
)
126 struct tree_desc t
[3];
127 struct traverse_info newinfo
;
128 struct name_entry
*p
;
137 newinfo
.pathlen
+= tree_entry_len(p
->path
, p
->sha1
) + 1;
138 newinfo
.conflicts
|= df_conflicts
;
140 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
141 const unsigned char *sha1
= NULL
;
143 sha1
= names
[i
].sha1
;
144 fill_tree_descriptor(t
+i
, sha1
);
146 return traverse_trees(n
, t
, &newinfo
);
150 * Compare the traverse-path to the cache entry without actually
151 * having to generate the textual representation of the traverse
154 * NOTE! This *only* compares up to the size of the traverse path
155 * itself - the caller needs to do the final check for the cache
156 * entry having more data at the end!
158 static int do_compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
160 int len
, pathlen
, ce_len
;
164 int cmp
= do_compare_entry(ce
, info
->prev
, &info
->name
);
168 pathlen
= info
->pathlen
;
169 ce_len
= ce_namelen(ce
);
171 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
172 if (ce_len
< pathlen
)
176 ce_name
= ce
->name
+ pathlen
;
178 len
= tree_entry_len(n
->path
, n
->sha1
);
179 return df_name_compare(ce_name
, ce_len
, S_IFREG
, n
->path
, len
, n
->mode
);
182 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
184 int cmp
= do_compare_entry(ce
, info
, n
);
189 * Even if the beginning compared identically, the ce should
190 * compare as bigger than a directory leading up to it!
192 return ce_namelen(ce
) > traverse_path_len(info
, n
);
195 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
, const struct name_entry
*n
, int stage
)
197 int len
= traverse_path_len(info
, n
);
198 struct cache_entry
*ce
= xcalloc(1, cache_entry_size(len
));
200 ce
->ce_mode
= create_ce_mode(n
->mode
);
201 ce
->ce_flags
= create_ce_flags(len
, stage
);
202 hashcpy(ce
->sha1
, n
->sha1
);
203 make_traverse_path(ce
->name
, info
, n
);
208 static int unpack_nondirectories(int n
, unsigned long mask
, unsigned long dirmask
, struct cache_entry
*src
[5],
209 const struct name_entry
*names
, const struct traverse_info
*info
)
212 struct unpack_trees_options
*o
= info
->data
;
213 unsigned long conflicts
;
215 /* Do we have *only* directories? Nothing to do */
216 if (mask
== dirmask
&& !src
[0])
219 conflicts
= info
->conflicts
;
222 conflicts
|= dirmask
;
225 * Ok, we've filled in up to any potential index entry in src[0],
228 for (i
= 0; i
< n
; i
++) {
230 unsigned int bit
= 1ul << i
;
231 if (conflicts
& bit
) {
232 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
239 else if (i
+ 1 < o
->head_idx
)
241 else if (i
+ 1 > o
->head_idx
)
245 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
);
249 return call_unpack_fn(src
, o
);
252 for (i
= 0; i
< n
; i
++)
253 add_entry(o
, src
[i
], 0, 0);
257 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
259 struct cache_entry
*src
[5] = { NULL
, };
260 struct unpack_trees_options
*o
= info
->data
;
261 const struct name_entry
*p
= names
;
263 /* Find first entry with a real name (we could use "mask" too) */
267 /* Are we supposed to look at the index too? */
269 while (o
->pos
< o
->src_index
->cache_nr
) {
270 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
271 int cmp
= compare_entry(ce
, info
, p
);
273 if (unpack_index_entry(ce
, o
) < 0)
281 * If we skip unmerged index entries, we'll skip this
282 * entry *and* the tree entries associated with it!
284 if (o
->skip_unmerged
) {
285 add_entry(o
, ce
, 0, 0);
295 if (unpack_nondirectories(n
, mask
, dirmask
, src
, names
, info
) < 0)
298 /* Now handle any directories.. */
300 unsigned long conflicts
= mask
& ~dirmask
;
306 if (traverse_trees_recursive(n
, dirmask
, conflicts
,
315 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
317 discard_index(&o
->result
);
320 return error(message
);
326 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
328 static struct cache_entry
*dfc
;
331 die("unpack_trees takes at most four trees");
332 memset(&state
, 0, sizeof(state
));
336 state
.refresh_cache
= 1;
338 memset(&o
->result
, 0, sizeof(o
->result
));
342 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
343 o
->df_conflict_entry
= dfc
;
346 const char *prefix
= o
->prefix
? o
->prefix
: "";
347 struct traverse_info info
;
349 setup_traverse_info(&info
, prefix
);
350 info
.fn
= unpack_callback
;
353 if (traverse_trees(len
, t
, &info
) < 0)
354 return unpack_failed(o
, NULL
);
357 /* Any left-over entries in the index? */
359 while (o
->pos
< o
->src_index
->cache_nr
) {
360 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
361 if (unpack_index_entry(ce
, o
) < 0)
362 return unpack_failed(o
, NULL
);
366 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
367 return unpack_failed(o
, "Merge requires file-level merging");
372 *o
->dst_index
= o
->result
;
376 /* Here come the merge functions */
378 static int reject_merge(struct cache_entry
*ce
)
380 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
384 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
390 return a
->ce_mode
== b
->ce_mode
&&
391 !hashcmp(a
->sha1
, b
->sha1
);
396 * When a CE gets turned into an unmerged entry, we
397 * want it to be up-to-date
399 static int verify_uptodate(struct cache_entry
*ce
,
400 struct unpack_trees_options
*o
)
404 if (o
->index_only
|| o
->reset
)
407 if (!lstat(ce
->name
, &st
)) {
408 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, CE_MATCH_IGNORE_VALID
);
412 * NEEDSWORK: the current default policy is to allow
413 * submodule to be out of sync wrt the supermodule
414 * index. This needs to be tightened later for
415 * submodules that are marked to be automatically
418 if (S_ISGITLINK(ce
->ce_mode
))
424 return o
->gently
? -1 :
425 error("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
428 static void invalidate_ce_path(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
431 cache_tree_invalidate_path(o
->src_index
->cache_tree
, ce
->name
);
435 * Check that checking out ce->sha1 in subdir ce->name is not
436 * going to overwrite any working files.
438 * Currently, git does not checkout subprojects during a superproject
439 * checkout, so it is not going to overwrite anything.
441 static int verify_clean_submodule(struct cache_entry
*ce
, const char *action
,
442 struct unpack_trees_options
*o
)
447 static int verify_clean_subdirectory(struct cache_entry
*ce
, const char *action
,
448 struct unpack_trees_options
*o
)
451 * we are about to extract "ce->name"; we would not want to lose
452 * anything in the existing directory there.
459 unsigned char sha1
[20];
461 if (S_ISGITLINK(ce
->ce_mode
) &&
462 resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) == 0) {
463 /* If we are not going to update the submodule, then
466 if (!hashcmp(sha1
, ce
->sha1
))
468 return verify_clean_submodule(ce
, action
, o
);
472 * First let's make sure we do not have a local modification
475 namelen
= strlen(ce
->name
);
476 pos
= index_name_pos(o
->src_index
, ce
->name
, namelen
);
478 return cnt
; /* we have it as nondirectory */
480 for (i
= pos
; i
< o
->src_index
->cache_nr
; i
++) {
481 struct cache_entry
*ce
= o
->src_index
->cache
[i
];
482 int len
= ce_namelen(ce
);
484 strncmp(ce
->name
, ce
->name
, namelen
) ||
485 ce
->name
[namelen
] != '/')
488 * ce->name is an entry in the subdirectory.
491 if (verify_uptodate(ce
, o
))
493 add_entry(o
, ce
, CE_REMOVE
, 0);
499 * Then we need to make sure that we do not lose a locally
500 * present file that is not ignored.
502 pathbuf
= xmalloc(namelen
+ 2);
503 memcpy(pathbuf
, ce
->name
, namelen
);
504 strcpy(pathbuf
+namelen
, "/");
506 memset(&d
, 0, sizeof(d
));
508 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
509 i
= read_directory(&d
, ce
->name
, pathbuf
, namelen
+1, NULL
);
511 return o
->gently
? -1 :
512 error("Updating '%s' would lose untracked files in it",
519 * We do not want to remove or overwrite a working tree file that
520 * is not tracked, unless it is ignored.
522 static int verify_absent(struct cache_entry
*ce
, const char *action
,
523 struct unpack_trees_options
*o
)
527 if (o
->index_only
|| o
->reset
|| !o
->update
)
530 if (has_symlink_leading_path(ce
->name
, NULL
))
533 if (!lstat(ce
->name
, &st
)) {
535 int dtype
= ce_to_dtype(ce
);
537 if (o
->dir
&& excluded(o
->dir
, ce
->name
, &dtype
))
539 * ce->name is explicitly excluded, so it is Ok to
543 if (S_ISDIR(st
.st_mode
)) {
545 * We are checking out path "foo" and
546 * found "foo/." in the working tree.
547 * This is tricky -- if we have modified
548 * files that are in "foo/" we would lose
551 cnt
= verify_clean_subdirectory(ce
, action
, o
);
554 * If this removed entries from the index,
555 * what that means is:
557 * (1) the caller unpack_trees_rec() saw path/foo
558 * in the index, and it has not removed it because
559 * it thinks it is handling 'path' as blob with
561 * (2) we will return "ok, we placed a merged entry
562 * in the index" which would cause o->pos to be
563 * incremented by one;
564 * (3) however, original o->pos now has 'path/foo'
565 * marked with "to be removed".
567 * We need to increment it by the number of
568 * deleted entries here.
575 * The previous round may already have decided to
576 * delete this path, which is in a subdirectory that
577 * is being replaced with a blob.
579 cnt
= index_name_pos(&o
->result
, ce
->name
, strlen(ce
->name
));
581 struct cache_entry
*ce
= o
->result
.cache
[cnt
];
582 if (ce
->ce_flags
& CE_REMOVE
)
586 return o
->gently
? -1 :
587 error("Untracked working tree file '%s' "
588 "would be %s by merge.", ce
->name
, action
);
593 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
594 struct unpack_trees_options
*o
)
598 * See if we can re-use the old CE directly?
599 * That way we get the uptodate stat info.
601 * This also removes the UPDATE flag on
604 if (same(old
, merge
)) {
605 copy_cache_entry(merge
, old
);
607 if (verify_uptodate(old
, o
))
609 invalidate_ce_path(old
, o
);
613 if (verify_absent(merge
, "overwritten", o
))
615 invalidate_ce_path(merge
, o
);
618 add_entry(o
, merge
, CE_UPDATE
, CE_STAGEMASK
);
622 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
623 struct unpack_trees_options
*o
)
625 /* Did it exist in the index? */
627 if (verify_absent(ce
, "removed", o
))
631 if (verify_uptodate(old
, o
))
633 add_entry(o
, ce
, CE_REMOVE
, 0);
634 invalidate_ce_path(ce
, o
);
638 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
640 add_entry(o
, ce
, 0, 0);
645 static void show_stage_entry(FILE *o
,
646 const char *label
, const struct cache_entry
*ce
)
649 fprintf(o
, "%s (missing)\n", label
);
651 fprintf(o
, "%s%06o %s %d\t%s\n",
654 sha1_to_hex(ce
->sha1
),
660 int threeway_merge(struct cache_entry
**stages
, struct unpack_trees_options
*o
)
662 struct cache_entry
*index
;
663 struct cache_entry
*head
;
664 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
667 int remote_match
= 0;
669 int df_conflict_head
= 0;
670 int df_conflict_remote
= 0;
672 int any_anc_missing
= 0;
673 int no_anc_exists
= 1;
676 for (i
= 1; i
< o
->head_idx
; i
++) {
677 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
684 head
= stages
[o
->head_idx
];
686 if (head
== o
->df_conflict_entry
) {
687 df_conflict_head
= 1;
691 if (remote
== o
->df_conflict_entry
) {
692 df_conflict_remote
= 1;
696 /* First, if there's a #16 situation, note that to prevent #13
699 if (!same(remote
, head
)) {
700 for (i
= 1; i
< o
->head_idx
; i
++) {
701 if (same(stages
[i
], head
)) {
704 if (same(stages
[i
], remote
)) {
710 /* We start with cases where the index is allowed to match
711 * something other than the head: #14(ALT) and #2ALT, where it
712 * is permitted to match the result instead.
714 /* #14, #14ALT, #2ALT */
715 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
716 if (index
&& !same(index
, remote
) && !same(index
, head
))
717 return o
->gently
? -1 : reject_merge(index
);
718 return merged_entry(remote
, index
, o
);
721 * If we have an entry in the index cache, then we want to
722 * make sure that it matches head.
724 if (index
&& !same(index
, head
))
725 return o
->gently
? -1 : reject_merge(index
);
729 if (same(head
, remote
))
730 return merged_entry(head
, index
, o
);
732 if (!df_conflict_remote
&& remote_match
&& !head_match
)
733 return merged_entry(head
, index
, o
);
737 if (!head
&& !remote
&& any_anc_missing
)
740 /* Under the new "aggressive" rule, we resolve mostly trivial
741 * cases that we historically had git-merge-one-file resolve.
744 int head_deleted
= !head
&& !df_conflict_head
;
745 int remote_deleted
= !remote
&& !df_conflict_remote
;
746 struct cache_entry
*ce
= NULL
;
755 for (i
= 1; i
< o
->head_idx
; i
++) {
756 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
765 * Deleted in one and unchanged in the other.
767 if ((head_deleted
&& remote_deleted
) ||
768 (head_deleted
&& remote
&& remote_match
) ||
769 (remote_deleted
&& head
&& head_match
)) {
771 return deleted_entry(index
, index
, o
);
772 if (ce
&& !head_deleted
) {
773 if (verify_absent(ce
, "removed", o
))
779 * Added in both, identically.
781 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
782 return merged_entry(head
, index
, o
);
786 /* Below are "no merge" cases, which require that the index be
787 * up-to-date to avoid the files getting overwritten with
788 * conflict resolution files.
791 if (verify_uptodate(index
, o
))
795 o
->nontrivial_merge
= 1;
797 /* #2, #3, #4, #6, #7, #9, #10, #11. */
799 if (!head_match
|| !remote_match
) {
800 for (i
= 1; i
< o
->head_idx
; i
++) {
801 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
802 keep_entry(stages
[i
], o
);
810 fprintf(stderr
, "read-tree: warning #16 detected\n");
811 show_stage_entry(stderr
, "head ", stages
[head_match
]);
812 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
815 if (head
) { count
+= keep_entry(head
, o
); }
816 if (remote
) { count
+= keep_entry(remote
, o
); }
823 * The rule is to "carry forward" what is in the index without losing
824 * information across a "fast forward", favoring a successful merge
825 * over a merge failure when it makes sense. For details of the
826 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
829 int twoway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
831 struct cache_entry
*current
= src
[0];
832 struct cache_entry
*oldtree
= src
[1];
833 struct cache_entry
*newtree
= src
[2];
835 if (o
->merge_size
!= 2)
836 return error("Cannot do a twoway merge of %d trees",
839 if (oldtree
== o
->df_conflict_entry
)
841 if (newtree
== o
->df_conflict_entry
)
845 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
846 (!oldtree
&& newtree
&&
847 same(current
, newtree
)) || /* 6 and 7 */
848 (oldtree
&& newtree
&&
849 same(oldtree
, newtree
)) || /* 14 and 15 */
850 (oldtree
&& newtree
&&
851 !same(oldtree
, newtree
) && /* 18 and 19 */
852 same(current
, newtree
))) {
853 return keep_entry(current
, o
);
855 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
857 return deleted_entry(oldtree
, current
, o
);
859 else if (oldtree
&& newtree
&&
860 same(current
, oldtree
) && !same(current
, newtree
)) {
862 return merged_entry(newtree
, current
, o
);
865 /* all other failures */
867 return o
->gently
? -1 : reject_merge(oldtree
);
869 return o
->gently
? -1 : reject_merge(current
);
871 return o
->gently
? -1 : reject_merge(newtree
);
876 return merged_entry(newtree
, current
, o
);
877 return deleted_entry(oldtree
, current
, o
);
883 * Keep the index entries at stage0, collapse stage1 but make sure
884 * stage0 does not have anything there.
886 int bind_merge(struct cache_entry
**src
,
887 struct unpack_trees_options
*o
)
889 struct cache_entry
*old
= src
[0];
890 struct cache_entry
*a
= src
[1];
892 if (o
->merge_size
!= 1)
893 return error("Cannot do a bind merge of %d trees\n",
896 return o
->gently
? -1 :
897 error("Entry '%s' overlaps with '%s'. Cannot bind.", a
->name
, old
->name
);
899 return keep_entry(old
, o
);
901 return merged_entry(a
, NULL
, o
);
908 * - take the stat information from stage0, take the data from stage1
910 int oneway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
912 struct cache_entry
*old
= src
[0];
913 struct cache_entry
*a
= src
[1];
915 if (o
->merge_size
!= 1)
916 return error("Cannot do a oneway merge of %d trees",
920 return deleted_entry(old
, old
, o
);
922 if (old
&& same(old
, a
)) {
926 if (lstat(old
->name
, &st
) ||
927 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
))
930 add_entry(o
, old
, update
, 0);
933 return merged_entry(a
, old
, o
);