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 int 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
;
66 if (o
->update
&& o
->verbose_update
) {
67 for (total
= cnt
= 0; cnt
< index
->cache_nr
; cnt
++) {
68 struct cache_entry
*ce
= index
->cache
[cnt
];
69 if (ce
->ce_flags
& (CE_UPDATE
| CE_REMOVE
))
73 progress
= start_progress_delay("Checking out files",
79 for (i
= 0; i
< index
->cache_nr
; i
++) {
80 struct cache_entry
*ce
= index
->cache
[i
];
82 if (ce
->ce_flags
& CE_REMOVE
) {
83 display_progress(progress
, ++cnt
);
85 unlink_entry(ce
->name
, last_symlink
);
86 remove_index_entry_at(&o
->result
, i
);
92 for (i
= 0; i
< index
->cache_nr
; i
++) {
93 struct cache_entry
*ce
= index
->cache
[i
];
95 if (ce
->ce_flags
& CE_UPDATE
) {
96 display_progress(progress
, ++cnt
);
97 ce
->ce_flags
&= ~CE_UPDATE
;
99 errs
|= checkout_entry(ce
, &state
, NULL
);
100 *last_symlink
= '\0';
104 stop_progress(&progress
);
108 static inline int call_unpack_fn(struct cache_entry
**src
, struct unpack_trees_options
*o
)
110 int ret
= o
->fn(src
, o
);
116 static int unpack_index_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
118 struct cache_entry
*src
[5] = { ce
, };
122 if (o
->skip_unmerged
) {
123 add_entry(o
, ce
, 0, 0);
127 return call_unpack_fn(src
, o
);
130 int traverse_trees_recursive(int n
, unsigned long dirmask
, unsigned long df_conflicts
, struct name_entry
*names
, struct traverse_info
*info
)
133 struct tree_desc t
[MAX_UNPACK_TREES
];
134 struct traverse_info newinfo
;
135 struct name_entry
*p
;
144 newinfo
.pathlen
+= tree_entry_len(p
->path
, p
->sha1
) + 1;
145 newinfo
.conflicts
|= df_conflicts
;
147 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
148 const unsigned char *sha1
= NULL
;
150 sha1
= names
[i
].sha1
;
151 fill_tree_descriptor(t
+i
, sha1
);
153 return traverse_trees(n
, t
, &newinfo
);
157 * Compare the traverse-path to the cache entry without actually
158 * having to generate the textual representation of the traverse
161 * NOTE! This *only* compares up to the size of the traverse path
162 * itself - the caller needs to do the final check for the cache
163 * entry having more data at the end!
165 static int do_compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
167 int len
, pathlen
, ce_len
;
171 int cmp
= do_compare_entry(ce
, info
->prev
, &info
->name
);
175 pathlen
= info
->pathlen
;
176 ce_len
= ce_namelen(ce
);
178 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
179 if (ce_len
< pathlen
)
183 ce_name
= ce
->name
+ pathlen
;
185 len
= tree_entry_len(n
->path
, n
->sha1
);
186 return df_name_compare(ce_name
, ce_len
, S_IFREG
, n
->path
, len
, n
->mode
);
189 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
191 int cmp
= do_compare_entry(ce
, info
, n
);
196 * Even if the beginning compared identically, the ce should
197 * compare as bigger than a directory leading up to it!
199 return ce_namelen(ce
) > traverse_path_len(info
, n
);
202 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
, const struct name_entry
*n
, int stage
)
204 int len
= traverse_path_len(info
, n
);
205 struct cache_entry
*ce
= xcalloc(1, cache_entry_size(len
));
207 ce
->ce_mode
= create_ce_mode(n
->mode
);
208 ce
->ce_flags
= create_ce_flags(len
, stage
);
209 hashcpy(ce
->sha1
, n
->sha1
);
210 make_traverse_path(ce
->name
, info
, n
);
215 static int unpack_nondirectories(int n
, unsigned long mask
, unsigned long dirmask
, struct cache_entry
*src
[5],
216 const struct name_entry
*names
, const struct traverse_info
*info
)
219 struct unpack_trees_options
*o
= info
->data
;
220 unsigned long conflicts
;
222 /* Do we have *only* directories? Nothing to do */
223 if (mask
== dirmask
&& !src
[0])
226 conflicts
= info
->conflicts
;
229 conflicts
|= dirmask
;
232 * Ok, we've filled in up to any potential index entry in src[0],
235 for (i
= 0; i
< n
; i
++) {
237 unsigned int bit
= 1ul << i
;
238 if (conflicts
& bit
) {
239 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
246 else if (i
+ 1 < o
->head_idx
)
248 else if (i
+ 1 > o
->head_idx
)
252 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
);
256 return call_unpack_fn(src
, o
);
259 for (i
= 0; i
< n
; i
++)
260 add_entry(o
, src
[i
], 0, 0);
264 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
266 struct cache_entry
*src
[5] = { NULL
, };
267 struct unpack_trees_options
*o
= info
->data
;
268 const struct name_entry
*p
= names
;
270 /* Find first entry with a real name (we could use "mask" too) */
274 /* Are we supposed to look at the index too? */
276 while (o
->pos
< o
->src_index
->cache_nr
) {
277 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
278 int cmp
= compare_entry(ce
, info
, p
);
280 if (unpack_index_entry(ce
, o
) < 0)
288 * If we skip unmerged index entries, we'll skip this
289 * entry *and* the tree entries associated with it!
291 if (o
->skip_unmerged
) {
292 add_entry(o
, ce
, 0, 0);
302 if (unpack_nondirectories(n
, mask
, dirmask
, src
, names
, info
) < 0)
305 /* Now handle any directories.. */
307 unsigned long conflicts
= mask
& ~dirmask
;
313 if (traverse_trees_recursive(n
, dirmask
, conflicts
,
322 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
324 discard_index(&o
->result
);
327 return error(message
);
333 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
335 static struct cache_entry
*dfc
;
337 if (len
> MAX_UNPACK_TREES
)
338 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES
);
339 memset(&state
, 0, sizeof(state
));
343 state
.refresh_cache
= 1;
345 memset(&o
->result
, 0, sizeof(o
->result
));
347 o
->result
.timestamp
= o
->src_index
->timestamp
;
351 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
352 o
->df_conflict_entry
= dfc
;
355 const char *prefix
= o
->prefix
? o
->prefix
: "";
356 struct traverse_info info
;
358 setup_traverse_info(&info
, prefix
);
359 info
.fn
= unpack_callback
;
362 if (traverse_trees(len
, t
, &info
) < 0)
363 return unpack_failed(o
, NULL
);
366 /* Any left-over entries in the index? */
368 while (o
->pos
< o
->src_index
->cache_nr
) {
369 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
370 if (unpack_index_entry(ce
, o
) < 0)
371 return unpack_failed(o
, NULL
);
375 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
376 return unpack_failed(o
, "Merge requires file-level merging");
379 if (check_updates(o
))
382 *o
->dst_index
= o
->result
;
386 /* Here come the merge functions */
388 static int reject_merge(struct cache_entry
*ce
)
390 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
394 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
400 return a
->ce_mode
== b
->ce_mode
&&
401 !hashcmp(a
->sha1
, b
->sha1
);
406 * When a CE gets turned into an unmerged entry, we
407 * want it to be up-to-date
409 static int verify_uptodate(struct cache_entry
*ce
,
410 struct unpack_trees_options
*o
)
414 if (o
->index_only
|| o
->reset
)
417 if (!lstat(ce
->name
, &st
)) {
418 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, CE_MATCH_IGNORE_VALID
);
422 * NEEDSWORK: the current default policy is to allow
423 * submodule to be out of sync wrt the supermodule
424 * index. This needs to be tightened later for
425 * submodules that are marked to be automatically
428 if (S_ISGITLINK(ce
->ce_mode
))
434 return o
->gently
? -1 :
435 error("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
438 static void invalidate_ce_path(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
441 cache_tree_invalidate_path(o
->src_index
->cache_tree
, ce
->name
);
445 * Check that checking out ce->sha1 in subdir ce->name is not
446 * going to overwrite any working files.
448 * Currently, git does not checkout subprojects during a superproject
449 * checkout, so it is not going to overwrite anything.
451 static int verify_clean_submodule(struct cache_entry
*ce
, const char *action
,
452 struct unpack_trees_options
*o
)
457 static int verify_clean_subdirectory(struct cache_entry
*ce
, const char *action
,
458 struct unpack_trees_options
*o
)
461 * we are about to extract "ce->name"; we would not want to lose
462 * anything in the existing directory there.
469 unsigned char sha1
[20];
471 if (S_ISGITLINK(ce
->ce_mode
) &&
472 resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) == 0) {
473 /* If we are not going to update the submodule, then
476 if (!hashcmp(sha1
, ce
->sha1
))
478 return verify_clean_submodule(ce
, action
, o
);
482 * First let's make sure we do not have a local modification
485 namelen
= strlen(ce
->name
);
486 pos
= index_name_pos(o
->src_index
, ce
->name
, namelen
);
488 return cnt
; /* we have it as nondirectory */
490 for (i
= pos
; i
< o
->src_index
->cache_nr
; i
++) {
491 struct cache_entry
*ce
= o
->src_index
->cache
[i
];
492 int len
= ce_namelen(ce
);
494 strncmp(ce
->name
, ce
->name
, namelen
) ||
495 ce
->name
[namelen
] != '/')
498 * ce->name is an entry in the subdirectory.
501 if (verify_uptodate(ce
, o
))
503 add_entry(o
, ce
, CE_REMOVE
, 0);
509 * Then we need to make sure that we do not lose a locally
510 * present file that is not ignored.
512 pathbuf
= xmalloc(namelen
+ 2);
513 memcpy(pathbuf
, ce
->name
, namelen
);
514 strcpy(pathbuf
+namelen
, "/");
516 memset(&d
, 0, sizeof(d
));
518 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
519 i
= read_directory(&d
, ce
->name
, pathbuf
, namelen
+1, NULL
);
521 return o
->gently
? -1 :
522 error("Updating '%s' would lose untracked files in it",
529 * This gets called when there was no index entry for the tree entry 'dst',
530 * but we found a file in the working tree that 'lstat()' said was fine,
531 * and we're on a case-insensitive filesystem.
533 * See if we can find a case-insensitive match in the index that also
534 * matches the stat information, and assume it's that other file!
536 static int icase_exists(struct unpack_trees_options
*o
, struct cache_entry
*dst
, struct stat
*st
)
538 struct cache_entry
*src
;
540 src
= index_name_exists(o
->src_index
, dst
->name
, ce_namelen(dst
), 1);
541 return src
&& !ie_match_stat(o
->src_index
, src
, st
, CE_MATCH_IGNORE_VALID
);
545 * We do not want to remove or overwrite a working tree file that
546 * is not tracked, unless it is ignored.
548 static int verify_absent(struct cache_entry
*ce
, const char *action
,
549 struct unpack_trees_options
*o
)
553 if (o
->index_only
|| o
->reset
|| !o
->update
)
556 if (has_symlink_leading_path(ce
->name
, NULL
))
559 if (!lstat(ce
->name
, &st
)) {
561 int dtype
= ce_to_dtype(ce
);
562 struct cache_entry
*result
;
565 * It may be that the 'lstat()' succeeded even though
566 * target 'ce' was absent, because there is an old
567 * entry that is different only in case..
569 * Ignore that lstat() if it matches.
571 if (ignore_case
&& icase_exists(o
, ce
, &st
))
574 if (o
->dir
&& excluded(o
->dir
, ce
->name
, &dtype
))
576 * ce->name is explicitly excluded, so it is Ok to
580 if (S_ISDIR(st
.st_mode
)) {
582 * We are checking out path "foo" and
583 * found "foo/." in the working tree.
584 * This is tricky -- if we have modified
585 * files that are in "foo/" we would lose
588 cnt
= verify_clean_subdirectory(ce
, action
, o
);
591 * If this removed entries from the index,
592 * what that means is:
594 * (1) the caller unpack_trees_rec() saw path/foo
595 * in the index, and it has not removed it because
596 * it thinks it is handling 'path' as blob with
598 * (2) we will return "ok, we placed a merged entry
599 * in the index" which would cause o->pos to be
600 * incremented by one;
601 * (3) however, original o->pos now has 'path/foo'
602 * marked with "to be removed".
604 * We need to increment it by the number of
605 * deleted entries here.
612 * The previous round may already have decided to
613 * delete this path, which is in a subdirectory that
614 * is being replaced with a blob.
616 result
= index_name_exists(&o
->result
, ce
->name
, ce_namelen(ce
), 0);
618 if (result
->ce_flags
& CE_REMOVE
)
622 return o
->gently
? -1 :
623 error("Untracked working tree file '%s' "
624 "would be %s by merge.", ce
->name
, action
);
629 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
630 struct unpack_trees_options
*o
)
632 int update
= CE_UPDATE
;
636 * See if we can re-use the old CE directly?
637 * That way we get the uptodate stat info.
639 * This also removes the UPDATE flag on a match; otherwise
640 * we will end up overwriting local changes in the work tree.
642 if (same(old
, merge
)) {
643 copy_cache_entry(merge
, old
);
646 if (verify_uptodate(old
, o
))
648 invalidate_ce_path(old
, o
);
652 if (verify_absent(merge
, "overwritten", o
))
654 invalidate_ce_path(merge
, o
);
657 add_entry(o
, merge
, update
, CE_STAGEMASK
);
661 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
662 struct unpack_trees_options
*o
)
664 /* Did it exist in the index? */
666 if (verify_absent(ce
, "removed", o
))
670 if (verify_uptodate(old
, o
))
672 add_entry(o
, ce
, CE_REMOVE
, 0);
673 invalidate_ce_path(ce
, o
);
677 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
679 add_entry(o
, ce
, 0, 0);
684 static void show_stage_entry(FILE *o
,
685 const char *label
, const struct cache_entry
*ce
)
688 fprintf(o
, "%s (missing)\n", label
);
690 fprintf(o
, "%s%06o %s %d\t%s\n",
693 sha1_to_hex(ce
->sha1
),
699 int threeway_merge(struct cache_entry
**stages
, struct unpack_trees_options
*o
)
701 struct cache_entry
*index
;
702 struct cache_entry
*head
;
703 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
706 int remote_match
= 0;
708 int df_conflict_head
= 0;
709 int df_conflict_remote
= 0;
711 int any_anc_missing
= 0;
712 int no_anc_exists
= 1;
715 for (i
= 1; i
< o
->head_idx
; i
++) {
716 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
723 head
= stages
[o
->head_idx
];
725 if (head
== o
->df_conflict_entry
) {
726 df_conflict_head
= 1;
730 if (remote
== o
->df_conflict_entry
) {
731 df_conflict_remote
= 1;
735 /* First, if there's a #16 situation, note that to prevent #13
738 if (!same(remote
, head
)) {
739 for (i
= 1; i
< o
->head_idx
; i
++) {
740 if (same(stages
[i
], head
)) {
743 if (same(stages
[i
], remote
)) {
749 /* We start with cases where the index is allowed to match
750 * something other than the head: #14(ALT) and #2ALT, where it
751 * is permitted to match the result instead.
753 /* #14, #14ALT, #2ALT */
754 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
755 if (index
&& !same(index
, remote
) && !same(index
, head
))
756 return o
->gently
? -1 : reject_merge(index
);
757 return merged_entry(remote
, index
, o
);
760 * If we have an entry in the index cache, then we want to
761 * make sure that it matches head.
763 if (index
&& !same(index
, head
))
764 return o
->gently
? -1 : reject_merge(index
);
768 if (same(head
, remote
))
769 return merged_entry(head
, index
, o
);
771 if (!df_conflict_remote
&& remote_match
&& !head_match
)
772 return merged_entry(head
, index
, o
);
776 if (!head
&& !remote
&& any_anc_missing
)
779 /* Under the new "aggressive" rule, we resolve mostly trivial
780 * cases that we historically had git-merge-one-file resolve.
783 int head_deleted
= !head
&& !df_conflict_head
;
784 int remote_deleted
= !remote
&& !df_conflict_remote
;
785 struct cache_entry
*ce
= NULL
;
794 for (i
= 1; i
< o
->head_idx
; i
++) {
795 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
804 * Deleted in one and unchanged in the other.
806 if ((head_deleted
&& remote_deleted
) ||
807 (head_deleted
&& remote
&& remote_match
) ||
808 (remote_deleted
&& head
&& head_match
)) {
810 return deleted_entry(index
, index
, o
);
811 if (ce
&& !head_deleted
) {
812 if (verify_absent(ce
, "removed", o
))
818 * Added in both, identically.
820 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
821 return merged_entry(head
, index
, o
);
825 /* Below are "no merge" cases, which require that the index be
826 * up-to-date to avoid the files getting overwritten with
827 * conflict resolution files.
830 if (verify_uptodate(index
, o
))
834 o
->nontrivial_merge
= 1;
836 /* #2, #3, #4, #6, #7, #9, #10, #11. */
838 if (!head_match
|| !remote_match
) {
839 for (i
= 1; i
< o
->head_idx
; i
++) {
840 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
841 keep_entry(stages
[i
], o
);
849 fprintf(stderr
, "read-tree: warning #16 detected\n");
850 show_stage_entry(stderr
, "head ", stages
[head_match
]);
851 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
854 if (head
) { count
+= keep_entry(head
, o
); }
855 if (remote
) { count
+= keep_entry(remote
, o
); }
862 * The rule is to "carry forward" what is in the index without losing
863 * information across a "fast forward", favoring a successful merge
864 * over a merge failure when it makes sense. For details of the
865 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
868 int twoway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
870 struct cache_entry
*current
= src
[0];
871 struct cache_entry
*oldtree
= src
[1];
872 struct cache_entry
*newtree
= src
[2];
874 if (o
->merge_size
!= 2)
875 return error("Cannot do a twoway merge of %d trees",
878 if (oldtree
== o
->df_conflict_entry
)
880 if (newtree
== o
->df_conflict_entry
)
884 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
885 (!oldtree
&& newtree
&&
886 same(current
, newtree
)) || /* 6 and 7 */
887 (oldtree
&& newtree
&&
888 same(oldtree
, newtree
)) || /* 14 and 15 */
889 (oldtree
&& newtree
&&
890 !same(oldtree
, newtree
) && /* 18 and 19 */
891 same(current
, newtree
))) {
892 return keep_entry(current
, o
);
894 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
896 return deleted_entry(oldtree
, current
, o
);
898 else if (oldtree
&& newtree
&&
899 same(current
, oldtree
) && !same(current
, newtree
)) {
901 return merged_entry(newtree
, current
, o
);
904 /* all other failures */
906 return o
->gently
? -1 : reject_merge(oldtree
);
908 return o
->gently
? -1 : reject_merge(current
);
910 return o
->gently
? -1 : reject_merge(newtree
);
915 return merged_entry(newtree
, current
, o
);
916 return deleted_entry(oldtree
, current
, o
);
922 * Keep the index entries at stage0, collapse stage1 but make sure
923 * stage0 does not have anything there.
925 int bind_merge(struct cache_entry
**src
,
926 struct unpack_trees_options
*o
)
928 struct cache_entry
*old
= src
[0];
929 struct cache_entry
*a
= src
[1];
931 if (o
->merge_size
!= 1)
932 return error("Cannot do a bind merge of %d trees\n",
935 return o
->gently
? -1 :
936 error("Entry '%s' overlaps with '%s'. Cannot bind.", a
->name
, old
->name
);
938 return keep_entry(old
, o
);
940 return merged_entry(a
, NULL
, o
);
947 * - take the stat information from stage0, take the data from stage1
949 int oneway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
951 struct cache_entry
*old
= src
[0];
952 struct cache_entry
*a
= src
[1];
954 if (o
->merge_size
!= 1)
955 return error("Cannot do a oneway merge of %d trees",
959 return deleted_entry(old
, old
, o
);
961 if (old
&& same(old
, a
)) {
965 if (lstat(old
->name
, &st
) ||
966 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
))
969 add_entry(o
, old
, update
, 0);
972 return merged_entry(a
, old
, o
);