5 #include "cache-tree.h"
6 #include "unpack-trees.h"
12 struct tree_entry_list
{
13 struct tree_entry_list
*next
;
16 const unsigned char *sha1
;
19 static struct tree_entry_list
*create_tree_entry_list(struct tree_desc
*desc
)
21 struct name_entry one
;
22 struct tree_entry_list
*ret
= NULL
;
23 struct tree_entry_list
**list_p
= &ret
;
25 while (tree_entry(desc
, &one
)) {
26 struct tree_entry_list
*entry
;
28 entry
= xmalloc(sizeof(struct tree_entry_list
));
29 entry
->name
= one
.path
;
30 entry
->sha1
= one
.sha1
;
31 entry
->mode
= one
.mode
;
35 list_p
= &entry
->next
;
40 static int entcmp(const char *name1
, int dir1
, const char *name2
, int dir2
)
42 int len1
= strlen(name1
);
43 int len2
= strlen(name2
);
44 int len
= len1
< len2
? len1
: len2
;
45 int ret
= memcmp(name1
, name2
, len
);
55 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
61 static inline void remove_entry(int remove
)
64 remove_cache_entry_at(remove
);
67 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
68 const char *base
, struct unpack_trees_options
*o
,
69 struct tree_entry_list
*df_conflict_list
)
72 int baselen
= strlen(base
);
73 int src_size
= len
+ 1;
78 i_stk
= push_exclude_per_directory(o
->dir
, base
, strlen(base
));
86 struct tree_entry_list
**subposns
;
87 struct cache_entry
**src
;
93 /* Find the first name in the input. */
99 if (o
->merge
&& o
->pos
< active_nr
) {
100 /* This is a bit tricky: */
101 /* If the index has a subdirectory (with
102 * contents) as the first name, it'll get a
103 * filename like "foo/bar". But that's after
104 * "foo", so the entry in trees will get
105 * handled first, at which point we'll go into
106 * "foo", and deal with "bar" from the index,
107 * because the base will be "foo/". The only
108 * way we can actually have "foo/bar" first of
109 * all the things is if the trees don't
110 * contain "foo" at all, in which case we'll
111 * handle "foo/bar" without going into the
112 * directory, but that's fine (and will return
113 * an error anyway, with the added unknown
117 cache_name
= active_cache
[o
->pos
]->name
;
118 if (strlen(cache_name
) > baselen
&&
119 !memcmp(cache_name
, base
, baselen
)) {
120 cache_name
+= baselen
;
129 printf("index %s\n", first
);
131 for (i
= 0; i
< len
; i
++) {
132 if (!posns
[i
] || posns
[i
] == df_conflict_list
)
135 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
137 if (!first
|| entcmp(first
, firstdir
,
139 S_ISDIR(posns
[i
]->mode
)) > 0) {
140 first
= posns
[i
]->name
;
141 firstdir
= S_ISDIR(posns
[i
]->mode
);
144 /* No name means we're done */
146 goto leave_directory
;
148 pathlen
= strlen(first
);
149 ce_size
= cache_entry_size(baselen
+ pathlen
);
151 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
153 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
156 if (cache_name
&& !strcmp(cache_name
, first
)) {
158 src
[0] = active_cache
[o
->pos
];
162 for (i
= 0; i
< len
; i
++) {
163 struct cache_entry
*ce
;
166 (posns
[i
] != df_conflict_list
&&
167 strcmp(first
, posns
[i
]->name
))) {
171 if (posns
[i
] == df_conflict_list
) {
172 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
176 if (S_ISDIR(posns
[i
]->mode
)) {
177 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
181 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
182 subposns
[i
] = create_tree_entry_list(&t
);
183 posns
[i
] = posns
[i
]->next
;
184 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
190 else if (i
+ 1 < o
->head_idx
)
192 else if (i
+ 1 > o
->head_idx
)
197 ce
= xcalloc(1, ce_size
);
198 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
199 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
201 memcpy(ce
->name
, base
, baselen
);
202 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
206 hashcpy(ce
->sha1
, posns
[i
]->sha1
);
207 src
[i
+ o
->merge
] = ce
;
208 subposns
[i
] = df_conflict_list
;
209 posns
[i
] = posns
[i
]->next
;
216 printf("%s:\n", first
);
217 for (i
= 0; i
< src_size
; i
++) {
220 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
225 ret
= o
->fn(src
, o
, remove
);
228 printf("Added %d entries\n", ret
);
232 remove_entry(remove
);
233 for (i
= 0; i
< src_size
; i
++) {
235 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
241 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
242 memcpy(newbase
, base
, baselen
);
243 memcpy(newbase
+ baselen
, first
, pathlen
);
244 newbase
[baselen
+ pathlen
] = '/';
245 newbase
[baselen
+ pathlen
+ 1] = '\0';
246 if (unpack_trees_rec(subposns
, len
, newbase
, o
,
249 goto leave_directory
;
259 pop_exclude_per_directory(o
->dir
, i_stk
);
263 /* Unlink the last component and attempt to remove leading
264 * directories, in case this unlink is the removal of the
265 * last entry in the directory -- empty directories are removed.
267 static void unlink_entry(char *name
, char *last_symlink
)
271 if (has_symlink_leading_path(name
, last_symlink
))
278 cp
= strrchr(name
, '/');
285 status
= rmdir(name
);
294 static struct checkout state
;
295 static void check_updates(struct cache_entry
**src
, int nr
,
296 struct unpack_trees_options
*o
)
298 unsigned short mask
= htons(CE_UPDATE
);
299 unsigned cnt
= 0, total
= 0;
300 struct progress
*progress
= NULL
;
301 char last_symlink
[PATH_MAX
];
303 if (o
->update
&& o
->verbose_update
) {
304 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
305 struct cache_entry
*ce
= src
[cnt
];
306 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
310 progress
= start_progress_delay("Checking out files",
315 *last_symlink
= '\0';
317 struct cache_entry
*ce
= *src
++;
319 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
320 display_progress(progress
, ++cnt
);
323 unlink_entry(ce
->name
, last_symlink
);
326 if (ce
->ce_flags
& mask
) {
327 ce
->ce_flags
&= ~mask
;
329 checkout_entry(ce
, &state
, NULL
);
330 *last_symlink
= '\0';
334 stop_progress(&progress
);
337 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
339 struct tree_entry_list
**posns
;
341 struct tree_entry_list df_conflict_list
;
342 static struct cache_entry
*dfc
;
344 memset(&df_conflict_list
, 0, sizeof(df_conflict_list
));
345 df_conflict_list
.next
= &df_conflict_list
;
346 memset(&state
, 0, sizeof(state
));
350 state
.refresh_cache
= 1;
355 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
356 o
->df_conflict_entry
= dfc
;
359 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
360 for (i
= 0; i
< len
; i
++)
361 posns
[i
] = create_tree_entry_list(t
+i
);
363 if (unpack_trees_rec(posns
, len
, o
->prefix
? o
->prefix
: "",
364 o
, &df_conflict_list
))
368 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
369 die("Merge requires file-level merging");
371 check_updates(active_cache
, active_nr
, o
);
375 /* Here come the merge functions */
377 static void reject_merge(struct cache_entry
*ce
)
379 die("Entry '%s' would be overwritten by merge. Cannot merge.",
383 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
389 return a
->ce_mode
== b
->ce_mode
&&
390 !hashcmp(a
->sha1
, b
->sha1
);
395 * When a CE gets turned into an unmerged entry, we
396 * want it to be up-to-date
398 static void verify_uptodate(struct cache_entry
*ce
,
399 struct unpack_trees_options
*o
)
403 if (o
->index_only
|| o
->reset
)
406 if (!lstat(ce
->name
, &st
)) {
407 unsigned changed
= ce_match_stat(ce
, &st
, CE_MATCH_IGNORE_VALID
);
411 * NEEDSWORK: the current default policy is to allow
412 * submodule to be out of sync wrt the supermodule
413 * index. This needs to be tightened later for
414 * submodules that are marked to be automatically
417 if (S_ISGITLINK(ntohl(ce
->ce_mode
)))
423 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
426 static void invalidate_ce_path(struct cache_entry
*ce
)
429 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
433 * Check that checking out ce->sha1 in subdir ce->name is not
434 * going to overwrite any working files.
436 * Currently, git does not checkout subprojects during a superproject
437 * checkout, so it is not going to overwrite anything.
439 static int verify_clean_submodule(struct cache_entry
*ce
, const char *action
,
440 struct unpack_trees_options
*o
)
445 static int verify_clean_subdirectory(struct cache_entry
*ce
, const char *action
,
446 struct unpack_trees_options
*o
)
449 * we are about to extract "ce->name"; we would not want to lose
450 * anything in the existing directory there.
457 unsigned char sha1
[20];
459 if (S_ISGITLINK(ntohl(ce
->ce_mode
)) &&
460 resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) == 0) {
461 /* If we are not going to update the submodule, then
464 if (!hashcmp(sha1
, ce
->sha1
))
466 return verify_clean_submodule(ce
, action
, o
);
470 * First let's make sure we do not have a local modification
473 namelen
= strlen(ce
->name
);
474 pos
= cache_name_pos(ce
->name
, namelen
);
476 return cnt
; /* we have it as nondirectory */
478 for (i
= pos
; i
< active_nr
; i
++) {
479 struct cache_entry
*ce
= active_cache
[i
];
480 int len
= ce_namelen(ce
);
482 strncmp(ce
->name
, ce
->name
, namelen
) ||
483 ce
->name
[namelen
] != '/')
486 * ce->name is an entry in the subdirectory.
489 verify_uptodate(ce
, o
);
496 * Then we need to make sure that we do not lose a locally
497 * present file that is not ignored.
499 pathbuf
= xmalloc(namelen
+ 2);
500 memcpy(pathbuf
, ce
->name
, namelen
);
501 strcpy(pathbuf
+namelen
, "/");
503 memset(&d
, 0, sizeof(d
));
505 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
506 i
= read_directory(&d
, ce
->name
, pathbuf
, namelen
+1, NULL
);
508 die("Updating '%s' would lose untracked files in it",
515 * We do not want to remove or overwrite a working tree file that
516 * is not tracked, unless it is ignored.
518 static void verify_absent(struct cache_entry
*ce
, const char *action
,
519 struct unpack_trees_options
*o
)
523 if (o
->index_only
|| o
->reset
|| !o
->update
)
526 if (has_symlink_leading_path(ce
->name
, NULL
))
529 if (!lstat(ce
->name
, &st
)) {
532 if (o
->dir
&& excluded(o
->dir
, ce
->name
))
534 * ce->name is explicitly excluded, so it is Ok to
538 if (S_ISDIR(st
.st_mode
)) {
540 * We are checking out path "foo" and
541 * found "foo/." in the working tree.
542 * This is tricky -- if we have modified
543 * files that are in "foo/" we would lose
546 cnt
= verify_clean_subdirectory(ce
, action
, o
);
549 * If this removed entries from the index,
550 * what that means is:
552 * (1) the caller unpack_trees_rec() saw path/foo
553 * in the index, and it has not removed it because
554 * it thinks it is handling 'path' as blob with
556 * (2) we will return "ok, we placed a merged entry
557 * in the index" which would cause o->pos to be
558 * incremented by one;
559 * (3) however, original o->pos now has 'path/foo'
560 * marked with "to be removed".
562 * We need to increment it by the number of
563 * deleted entries here.
570 * The previous round may already have decided to
571 * delete this path, which is in a subdirectory that
572 * is being replaced with a blob.
574 cnt
= cache_name_pos(ce
->name
, strlen(ce
->name
));
576 struct cache_entry
*ce
= active_cache
[cnt
];
577 if (!ce_stage(ce
) && !ce
->ce_mode
)
581 die("Untracked working tree file '%s' "
582 "would be %s by merge.", ce
->name
, action
);
586 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
587 struct unpack_trees_options
*o
)
589 merge
->ce_flags
|= htons(CE_UPDATE
);
592 * See if we can re-use the old CE directly?
593 * That way we get the uptodate stat info.
595 * This also removes the UPDATE flag on
598 if (same(old
, merge
)) {
601 verify_uptodate(old
, o
);
602 invalidate_ce_path(old
);
606 verify_absent(merge
, "overwritten", o
);
607 invalidate_ce_path(merge
);
610 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
611 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
615 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
616 struct unpack_trees_options
*o
)
619 verify_uptodate(old
, o
);
621 verify_absent(ce
, "removed", o
);
623 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
624 invalidate_ce_path(ce
);
628 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
630 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
635 static void show_stage_entry(FILE *o
,
636 const char *label
, const struct cache_entry
*ce
)
639 fprintf(o
, "%s (missing)\n", label
);
641 fprintf(o
, "%s%06o %s %d\t%s\n",
644 sha1_to_hex(ce
->sha1
),
650 int threeway_merge(struct cache_entry
**stages
,
651 struct unpack_trees_options
*o
,
654 struct cache_entry
*index
;
655 struct cache_entry
*head
;
656 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
659 int remote_match
= 0;
661 int df_conflict_head
= 0;
662 int df_conflict_remote
= 0;
664 int any_anc_missing
= 0;
665 int no_anc_exists
= 1;
668 for (i
= 1; i
< o
->head_idx
; i
++) {
669 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
676 head
= stages
[o
->head_idx
];
678 if (head
== o
->df_conflict_entry
) {
679 df_conflict_head
= 1;
683 if (remote
== o
->df_conflict_entry
) {
684 df_conflict_remote
= 1;
688 /* First, if there's a #16 situation, note that to prevent #13
691 if (!same(remote
, head
)) {
692 for (i
= 1; i
< o
->head_idx
; i
++) {
693 if (same(stages
[i
], head
)) {
696 if (same(stages
[i
], remote
)) {
702 /* We start with cases where the index is allowed to match
703 * something other than the head: #14(ALT) and #2ALT, where it
704 * is permitted to match the result instead.
706 /* #14, #14ALT, #2ALT */
707 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
708 if (index
&& !same(index
, remote
) && !same(index
, head
))
710 return merged_entry(remote
, index
, o
);
713 * If we have an entry in the index cache, then we want to
714 * make sure that it matches head.
716 if (index
&& !same(index
, head
)) {
722 if (same(head
, remote
))
723 return merged_entry(head
, index
, o
);
725 if (!df_conflict_remote
&& remote_match
&& !head_match
)
726 return merged_entry(head
, index
, o
);
730 if (!head
&& !remote
&& any_anc_missing
) {
731 remove_entry(remove
);
735 /* Under the new "aggressive" rule, we resolve mostly trivial
736 * cases that we historically had git-merge-one-file resolve.
739 int head_deleted
= !head
&& !df_conflict_head
;
740 int remote_deleted
= !remote
&& !df_conflict_remote
;
741 struct cache_entry
*ce
= NULL
;
750 for (i
= 1; i
< o
->head_idx
; i
++) {
751 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
760 * Deleted in one and unchanged in the other.
762 if ((head_deleted
&& remote_deleted
) ||
763 (head_deleted
&& remote
&& remote_match
) ||
764 (remote_deleted
&& head
&& head_match
)) {
765 remove_entry(remove
);
767 return deleted_entry(index
, index
, o
);
768 else if (ce
&& !head_deleted
)
769 verify_absent(ce
, "removed", o
);
773 * Added in both, identically.
775 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
776 return merged_entry(head
, index
, o
);
780 /* Below are "no merge" cases, which require that the index be
781 * up-to-date to avoid the files getting overwritten with
782 * conflict resolution files.
785 verify_uptodate(index
, o
);
788 remove_entry(remove
);
789 o
->nontrivial_merge
= 1;
791 /* #2, #3, #4, #6, #7, #9, #10, #11. */
793 if (!head_match
|| !remote_match
) {
794 for (i
= 1; i
< o
->head_idx
; i
++) {
795 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
796 keep_entry(stages
[i
], o
);
804 fprintf(stderr
, "read-tree: warning #16 detected\n");
805 show_stage_entry(stderr
, "head ", stages
[head_match
]);
806 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
809 if (head
) { count
+= keep_entry(head
, o
); }
810 if (remote
) { count
+= keep_entry(remote
, o
); }
817 * The rule is to "carry forward" what is in the index without losing
818 * information across a "fast forward", favoring a successful merge
819 * over a merge failure when it makes sense. For details of the
820 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
823 int twoway_merge(struct cache_entry
**src
,
824 struct unpack_trees_options
*o
,
827 struct cache_entry
*current
= src
[0];
828 struct cache_entry
*oldtree
= src
[1];
829 struct cache_entry
*newtree
= src
[2];
831 if (o
->merge_size
!= 2)
832 return error("Cannot do a twoway merge of %d trees",
835 if (oldtree
== o
->df_conflict_entry
)
837 if (newtree
== o
->df_conflict_entry
)
841 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
842 (!oldtree
&& newtree
&&
843 same(current
, newtree
)) || /* 6 and 7 */
844 (oldtree
&& newtree
&&
845 same(oldtree
, newtree
)) || /* 14 and 15 */
846 (oldtree
&& newtree
&&
847 !same(oldtree
, newtree
) && /* 18 and 19 */
848 same(current
, newtree
))) {
849 return keep_entry(current
, o
);
851 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
853 remove_entry(remove
);
854 return deleted_entry(oldtree
, current
, o
);
856 else if (oldtree
&& newtree
&&
857 same(current
, oldtree
) && !same(current
, newtree
)) {
859 return merged_entry(newtree
, current
, o
);
862 /* all other failures */
863 remove_entry(remove
);
865 reject_merge(oldtree
);
867 reject_merge(current
);
869 reject_merge(newtree
);
874 return merged_entry(newtree
, current
, o
);
875 remove_entry(remove
);
876 return deleted_entry(oldtree
, current
, o
);
882 * Keep the index entries at stage0, collapse stage1 but make sure
883 * stage0 does not have anything there.
885 int bind_merge(struct cache_entry
**src
,
886 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 die("Entry '%s' overlaps. Cannot bind.", a
->name
);
898 return keep_entry(old
, o
);
900 return merged_entry(a
, NULL
, o
);
907 * - take the stat information from stage0, take the data from stage1
909 int oneway_merge(struct cache_entry
**src
,
910 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 remove_entry(remove
);
922 return deleted_entry(old
, old
, o
);
924 if (old
&& same(old
, a
)) {
927 if (lstat(old
->name
, &st
) ||
928 ce_match_stat(old
, &st
, CE_MATCH_IGNORE_VALID
))
929 old
->ce_flags
|= htons(CE_UPDATE
);
931 return keep_entry(old
, o
);
933 return merged_entry(a
, old
, o
);