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);
121 return call_unpack_fn(src
, o
);
124 int traverse_trees_recursive(int n
, unsigned long dirmask
, unsigned long df_conflicts
, struct name_entry
*names
, struct traverse_info
*info
)
127 struct tree_desc t
[3];
128 struct traverse_info newinfo
;
129 struct name_entry
*p
;
138 newinfo
.pathlen
+= tree_entry_len(p
->path
, p
->sha1
) + 1;
139 newinfo
.conflicts
|= df_conflicts
;
141 for (i
= 0; i
< n
; i
++, dirmask
>>= 1) {
142 const unsigned char *sha1
= NULL
;
144 sha1
= names
[i
].sha1
;
145 fill_tree_descriptor(t
+i
, sha1
);
147 traverse_trees(n
, t
, &newinfo
);
152 * Compare the traverse-path to the cache entry without actually
153 * having to generate the textual representation of the traverse
156 * NOTE! This *only* compares up to the size of the traverse path
157 * itself - the caller needs to do the final check for the cache
158 * entry having more data at the end!
160 static int do_compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
162 int len
, pathlen
, ce_len
;
166 int cmp
= do_compare_entry(ce
, info
->prev
, &info
->name
);
170 pathlen
= info
->pathlen
;
171 ce_len
= ce_namelen(ce
);
173 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
174 if (ce_len
< pathlen
)
178 ce_name
= ce
->name
+ pathlen
;
180 len
= tree_entry_len(n
->path
, n
->sha1
);
181 return df_name_compare(ce_name
, ce_len
, S_IFREG
, n
->path
, len
, n
->mode
);
184 static int compare_entry(const struct cache_entry
*ce
, const struct traverse_info
*info
, const struct name_entry
*n
)
186 int cmp
= do_compare_entry(ce
, info
, n
);
191 * Even if the beginning compared identically, the ce should
192 * compare as bigger than a directory leading up to it!
194 return ce_namelen(ce
) > traverse_path_len(info
, n
);
197 static struct cache_entry
*create_ce_entry(const struct traverse_info
*info
, const struct name_entry
*n
, int stage
)
199 int len
= traverse_path_len(info
, n
);
200 struct cache_entry
*ce
= xcalloc(1, cache_entry_size(len
));
202 ce
->ce_mode
= create_ce_mode(n
->mode
);
203 ce
->ce_flags
= create_ce_flags(len
, stage
);
204 hashcpy(ce
->sha1
, n
->sha1
);
205 make_traverse_path(ce
->name
, info
, n
);
210 static int unpack_nondirectories(int n
, unsigned long mask
, unsigned long dirmask
, struct cache_entry
*src
[5],
211 const struct name_entry
*names
, const struct traverse_info
*info
)
214 struct unpack_trees_options
*o
= info
->data
;
215 unsigned long conflicts
;
217 /* Do we have *only* directories? Nothing to do */
218 if (mask
== dirmask
&& !src
[0])
221 conflicts
= info
->conflicts
;
224 conflicts
|= dirmask
;
227 * Ok, we've filled in up to any potential index entry in src[0],
230 for (i
= 0; i
< n
; i
++) {
232 unsigned int bit
= 1ul << i
;
233 if (conflicts
& bit
) {
234 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
241 else if (i
+ 1 < o
->head_idx
)
243 else if (i
+ 1 > o
->head_idx
)
247 src
[i
+ o
->merge
] = create_ce_entry(info
, names
+ i
, stage
);
251 return call_unpack_fn(src
, o
);
254 for (i
= 0; i
< n
; i
++)
255 add_entry(o
, src
[i
], 0, 0);
259 static int unpack_callback(int n
, unsigned long mask
, unsigned long dirmask
, struct name_entry
*names
, struct traverse_info
*info
)
261 struct cache_entry
*src
[5] = { NULL
, };
262 struct unpack_trees_options
*o
= info
->data
;
263 const struct name_entry
*p
= names
;
265 /* Find first entry with a real name (we could use "mask" too) */
269 /* Are we supposed to look at the index too? */
271 while (o
->pos
< o
->src_index
->cache_nr
) {
272 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
273 int cmp
= compare_entry(ce
, info
, p
);
275 if (unpack_index_entry(ce
, o
) < 0)
283 * If we skip unmerged index entries, we'll skip this
284 * entry *and* the tree entries associated with it!
286 if (o
->skip_unmerged
) {
287 add_entry(o
, ce
, 0, 0);
298 if (unpack_nondirectories(n
, mask
, dirmask
, src
, names
, info
) < 0)
301 /* Now handle any directories.. */
303 unsigned long conflicts
= mask
& ~dirmask
;
309 traverse_trees_recursive(n
, dirmask
, conflicts
, names
, info
);
316 static int unpack_failed(struct unpack_trees_options
*o
, const char *message
)
318 discard_index(&o
->result
);
321 return error(message
);
327 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
329 static struct cache_entry
*dfc
;
332 die("unpack_trees takes at most four trees");
333 memset(&state
, 0, sizeof(state
));
337 state
.refresh_cache
= 1;
339 memset(&o
->result
, 0, sizeof(o
->result
));
343 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
344 o
->df_conflict_entry
= dfc
;
347 const char *prefix
= o
->prefix
? o
->prefix
: "";
348 struct traverse_info info
;
350 setup_traverse_info(&info
, prefix
);
351 info
.fn
= unpack_callback
;
354 if (traverse_trees(len
, t
, &info
) < 0)
355 return unpack_failed(o
, NULL
);
358 /* Any left-over entries in the index? */
360 while (o
->pos
< o
->src_index
->cache_nr
) {
361 struct cache_entry
*ce
= o
->src_index
->cache
[o
->pos
];
362 if (unpack_index_entry(ce
, o
) < 0)
363 return unpack_failed(o
, NULL
);
367 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
368 return unpack_failed(o
, "Merge requires file-level merging");
373 *o
->dst_index
= o
->result
;
377 /* Here come the merge functions */
379 static int reject_merge(struct cache_entry
*ce
)
381 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
385 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
391 return a
->ce_mode
== b
->ce_mode
&&
392 !hashcmp(a
->sha1
, b
->sha1
);
397 * When a CE gets turned into an unmerged entry, we
398 * want it to be up-to-date
400 static int verify_uptodate(struct cache_entry
*ce
,
401 struct unpack_trees_options
*o
)
405 if (o
->index_only
|| o
->reset
)
408 if (!lstat(ce
->name
, &st
)) {
409 unsigned changed
= ie_match_stat(o
->src_index
, ce
, &st
, CE_MATCH_IGNORE_VALID
);
413 * NEEDSWORK: the current default policy is to allow
414 * submodule to be out of sync wrt the supermodule
415 * index. This needs to be tightened later for
416 * submodules that are marked to be automatically
419 if (S_ISGITLINK(ce
->ce_mode
))
425 return o
->gently
? -1 :
426 error("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
429 static void invalidate_ce_path(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
432 cache_tree_invalidate_path(o
->src_index
->cache_tree
, ce
->name
);
436 * Check that checking out ce->sha1 in subdir ce->name is not
437 * going to overwrite any working files.
439 * Currently, git does not checkout subprojects during a superproject
440 * checkout, so it is not going to overwrite anything.
442 static int verify_clean_submodule(struct cache_entry
*ce
, const char *action
,
443 struct unpack_trees_options
*o
)
448 static int verify_clean_subdirectory(struct cache_entry
*ce
, const char *action
,
449 struct unpack_trees_options
*o
)
452 * we are about to extract "ce->name"; we would not want to lose
453 * anything in the existing directory there.
460 unsigned char sha1
[20];
462 if (S_ISGITLINK(ce
->ce_mode
) &&
463 resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) == 0) {
464 /* If we are not going to update the submodule, then
467 if (!hashcmp(sha1
, ce
->sha1
))
469 return verify_clean_submodule(ce
, action
, o
);
473 * First let's make sure we do not have a local modification
476 namelen
= strlen(ce
->name
);
477 pos
= index_name_pos(o
->src_index
, ce
->name
, namelen
);
479 return cnt
; /* we have it as nondirectory */
481 for (i
= pos
; i
< o
->src_index
->cache_nr
; i
++) {
482 struct cache_entry
*ce
= o
->src_index
->cache
[i
];
483 int len
= ce_namelen(ce
);
485 strncmp(ce
->name
, ce
->name
, namelen
) ||
486 ce
->name
[namelen
] != '/')
489 * ce->name is an entry in the subdirectory.
492 if (verify_uptodate(ce
, o
))
494 add_entry(o
, ce
, CE_REMOVE
, 0);
500 * Then we need to make sure that we do not lose a locally
501 * present file that is not ignored.
503 pathbuf
= xmalloc(namelen
+ 2);
504 memcpy(pathbuf
, ce
->name
, namelen
);
505 strcpy(pathbuf
+namelen
, "/");
507 memset(&d
, 0, sizeof(d
));
509 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
510 i
= read_directory(&d
, ce
->name
, pathbuf
, namelen
+1, NULL
);
512 return o
->gently
? -1 :
513 error("Updating '%s' would lose untracked files in it",
520 * We do not want to remove or overwrite a working tree file that
521 * is not tracked, unless it is ignored.
523 static int verify_absent(struct cache_entry
*ce
, const char *action
,
524 struct unpack_trees_options
*o
)
528 if (o
->index_only
|| o
->reset
|| !o
->update
)
531 if (has_symlink_leading_path(ce
->name
, NULL
))
534 if (!lstat(ce
->name
, &st
)) {
536 int dtype
= ce_to_dtype(ce
);
538 if (o
->dir
&& excluded(o
->dir
, ce
->name
, &dtype
))
540 * ce->name is explicitly excluded, so it is Ok to
544 if (S_ISDIR(st
.st_mode
)) {
546 * We are checking out path "foo" and
547 * found "foo/." in the working tree.
548 * This is tricky -- if we have modified
549 * files that are in "foo/" we would lose
552 cnt
= verify_clean_subdirectory(ce
, action
, o
);
555 * If this removed entries from the index,
556 * what that means is:
558 * (1) the caller unpack_trees_rec() saw path/foo
559 * in the index, and it has not removed it because
560 * it thinks it is handling 'path' as blob with
562 * (2) we will return "ok, we placed a merged entry
563 * in the index" which would cause o->pos to be
564 * incremented by one;
565 * (3) however, original o->pos now has 'path/foo'
566 * marked with "to be removed".
568 * We need to increment it by the number of
569 * deleted entries here.
576 * The previous round may already have decided to
577 * delete this path, which is in a subdirectory that
578 * is being replaced with a blob.
580 cnt
= index_name_pos(&o
->result
, ce
->name
, strlen(ce
->name
));
582 struct cache_entry
*ce
= o
->result
.cache
[cnt
];
583 if (ce
->ce_flags
& CE_REMOVE
)
587 return o
->gently
? -1 :
588 error("Untracked working tree file '%s' "
589 "would be %s by merge.", ce
->name
, action
);
594 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
595 struct unpack_trees_options
*o
)
599 * See if we can re-use the old CE directly?
600 * That way we get the uptodate stat info.
602 * This also removes the UPDATE flag on
605 if (same(old
, merge
)) {
606 copy_cache_entry(merge
, old
);
608 if (verify_uptodate(old
, o
))
610 invalidate_ce_path(old
, o
);
614 if (verify_absent(merge
, "overwritten", o
))
616 invalidate_ce_path(merge
, o
);
619 add_entry(o
, merge
, CE_UPDATE
, CE_STAGEMASK
);
623 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
624 struct unpack_trees_options
*o
)
626 /* Did it exist in the index? */
628 if (verify_absent(ce
, "removed", o
))
632 if (verify_uptodate(old
, o
))
634 add_entry(o
, ce
, CE_REMOVE
, 0);
635 invalidate_ce_path(ce
, o
);
639 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
641 add_entry(o
, ce
, 0, 0);
646 static void show_stage_entry(FILE *o
,
647 const char *label
, const struct cache_entry
*ce
)
650 fprintf(o
, "%s (missing)\n", label
);
652 fprintf(o
, "%s%06o %s %d\t%s\n",
655 sha1_to_hex(ce
->sha1
),
661 int threeway_merge(struct cache_entry
**stages
, struct unpack_trees_options
*o
)
663 struct cache_entry
*index
;
664 struct cache_entry
*head
;
665 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
668 int remote_match
= 0;
670 int df_conflict_head
= 0;
671 int df_conflict_remote
= 0;
673 int any_anc_missing
= 0;
674 int no_anc_exists
= 1;
677 for (i
= 1; i
< o
->head_idx
; i
++) {
678 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
685 head
= stages
[o
->head_idx
];
687 if (head
== o
->df_conflict_entry
) {
688 df_conflict_head
= 1;
692 if (remote
== o
->df_conflict_entry
) {
693 df_conflict_remote
= 1;
697 /* First, if there's a #16 situation, note that to prevent #13
700 if (!same(remote
, head
)) {
701 for (i
= 1; i
< o
->head_idx
; i
++) {
702 if (same(stages
[i
], head
)) {
705 if (same(stages
[i
], remote
)) {
711 /* We start with cases where the index is allowed to match
712 * something other than the head: #14(ALT) and #2ALT, where it
713 * is permitted to match the result instead.
715 /* #14, #14ALT, #2ALT */
716 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
717 if (index
&& !same(index
, remote
) && !same(index
, head
))
718 return o
->gently
? -1 : reject_merge(index
);
719 return merged_entry(remote
, index
, o
);
722 * If we have an entry in the index cache, then we want to
723 * make sure that it matches head.
725 if (index
&& !same(index
, head
))
726 return o
->gently
? -1 : reject_merge(index
);
730 if (same(head
, remote
))
731 return merged_entry(head
, index
, o
);
733 if (!df_conflict_remote
&& remote_match
&& !head_match
)
734 return merged_entry(head
, index
, o
);
738 if (!head
&& !remote
&& any_anc_missing
)
741 /* Under the new "aggressive" rule, we resolve mostly trivial
742 * cases that we historically had git-merge-one-file resolve.
745 int head_deleted
= !head
&& !df_conflict_head
;
746 int remote_deleted
= !remote
&& !df_conflict_remote
;
747 struct cache_entry
*ce
= NULL
;
756 for (i
= 1; i
< o
->head_idx
; i
++) {
757 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
766 * Deleted in one and unchanged in the other.
768 if ((head_deleted
&& remote_deleted
) ||
769 (head_deleted
&& remote
&& remote_match
) ||
770 (remote_deleted
&& head
&& head_match
)) {
772 return deleted_entry(index
, index
, o
);
773 if (ce
&& !head_deleted
) {
774 if (verify_absent(ce
, "removed", o
))
780 * Added in both, identically.
782 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
783 return merged_entry(head
, index
, o
);
787 /* Below are "no merge" cases, which require that the index be
788 * up-to-date to avoid the files getting overwritten with
789 * conflict resolution files.
792 if (verify_uptodate(index
, o
))
796 o
->nontrivial_merge
= 1;
798 /* #2, #3, #4, #6, #7, #9, #10, #11. */
800 if (!head_match
|| !remote_match
) {
801 for (i
= 1; i
< o
->head_idx
; i
++) {
802 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
803 keep_entry(stages
[i
], o
);
811 fprintf(stderr
, "read-tree: warning #16 detected\n");
812 show_stage_entry(stderr
, "head ", stages
[head_match
]);
813 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
816 if (head
) { count
+= keep_entry(head
, o
); }
817 if (remote
) { count
+= keep_entry(remote
, o
); }
824 * The rule is to "carry forward" what is in the index without losing
825 * information across a "fast forward", favoring a successful merge
826 * over a merge failure when it makes sense. For details of the
827 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
830 int twoway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
832 struct cache_entry
*current
= src
[0];
833 struct cache_entry
*oldtree
= src
[1];
834 struct cache_entry
*newtree
= src
[2];
836 if (o
->merge_size
!= 2)
837 return error("Cannot do a twoway merge of %d trees",
840 if (oldtree
== o
->df_conflict_entry
)
842 if (newtree
== o
->df_conflict_entry
)
846 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
847 (!oldtree
&& newtree
&&
848 same(current
, newtree
)) || /* 6 and 7 */
849 (oldtree
&& newtree
&&
850 same(oldtree
, newtree
)) || /* 14 and 15 */
851 (oldtree
&& newtree
&&
852 !same(oldtree
, newtree
) && /* 18 and 19 */
853 same(current
, newtree
))) {
854 return keep_entry(current
, o
);
856 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
858 return deleted_entry(oldtree
, current
, o
);
860 else if (oldtree
&& newtree
&&
861 same(current
, oldtree
) && !same(current
, newtree
)) {
863 return merged_entry(newtree
, current
, o
);
866 /* all other failures */
868 return o
->gently
? -1 : reject_merge(oldtree
);
870 return o
->gently
? -1 : reject_merge(current
);
872 return o
->gently
? -1 : reject_merge(newtree
);
877 return merged_entry(newtree
, current
, o
);
878 return deleted_entry(oldtree
, current
, o
);
884 * Keep the index entries at stage0, collapse stage1 but make sure
885 * stage0 does not have anything there.
887 int bind_merge(struct cache_entry
**src
,
888 struct unpack_trees_options
*o
)
890 struct cache_entry
*old
= src
[0];
891 struct cache_entry
*a
= src
[1];
893 if (o
->merge_size
!= 1)
894 return error("Cannot do a bind merge of %d trees\n",
897 return o
->gently
? -1 :
898 error("Entry '%s' overlaps with '%s'. Cannot bind.", a
->name
, old
->name
);
900 return keep_entry(old
, o
);
902 return merged_entry(a
, NULL
, o
);
909 * - take the stat information from stage0, take the data from stage1
911 int oneway_merge(struct cache_entry
**src
, struct unpack_trees_options
*o
)
913 struct cache_entry
*old
= src
[0];
914 struct cache_entry
*a
= src
[1];
916 if (o
->merge_size
!= 1)
917 return error("Cannot do a oneway merge of %d trees",
921 return deleted_entry(old
, old
, o
);
923 if (old
&& same(old
, a
)) {
927 if (lstat(old
->name
, &st
) ||
928 ie_match_stat(o
->src_index
, old
, &st
, CE_MATCH_IGNORE_VALID
))
931 add_entry(o
, old
, update
, 0);
934 return merged_entry(a
, old
, o
);