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;
82 struct tree_entry_list
**subposns
;
83 struct cache_entry
**src
;
90 /* Find the first name in the input. */
96 if (o
->merge
&& o
->pos
< active_nr
) {
97 /* This is a bit tricky: */
98 /* If the index has a subdirectory (with
99 * contents) as the first name, it'll get a
100 * filename like "foo/bar". But that's after
101 * "foo", so the entry in trees will get
102 * handled first, at which point we'll go into
103 * "foo", and deal with "bar" from the index,
104 * because the base will be "foo/". The only
105 * way we can actually have "foo/bar" first of
106 * all the things is if the trees don't
107 * contain "foo" at all, in which case we'll
108 * handle "foo/bar" without going into the
109 * directory, but that's fine (and will return
110 * an error anyway, with the added unknown
114 cache_name
= active_cache
[o
->pos
]->name
;
115 if (strlen(cache_name
) > baselen
&&
116 !memcmp(cache_name
, base
, baselen
)) {
117 cache_name
+= baselen
;
126 fprintf(stderr
, "index %s\n", first
);
128 for (i
= 0; i
< len
; i
++) {
129 if (!posns
[i
] || posns
[i
] == df_conflict_list
)
132 fprintf(stderr
, "%d %s\n", i
+ 1, posns
[i
]->name
);
134 if (!first
|| entcmp(first
, firstdir
,
136 S_ISDIR(posns
[i
]->mode
)) > 0) {
137 first
= posns
[i
]->name
;
138 firstdir
= S_ISDIR(posns
[i
]->mode
);
141 /* No name means we're done */
143 goto leave_directory
;
145 pathlen
= strlen(first
);
146 ce_size
= cache_entry_size(baselen
+ pathlen
);
148 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
150 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
153 if (cache_name
&& !strcmp(cache_name
, first
)) {
155 src
[0] = active_cache
[o
->pos
];
157 if (o
->skip_unmerged
&& ce_stage(src
[0]))
161 for (i
= 0; i
< len
; i
++) {
162 struct cache_entry
*ce
;
165 (posns
[i
] != df_conflict_list
&&
166 strcmp(first
, posns
[i
]->name
))) {
170 if (posns
[i
] == df_conflict_list
) {
171 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
175 if (S_ISDIR(posns
[i
]->mode
)) {
176 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
180 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
181 subposns
[i
] = create_tree_entry_list(&t
);
182 posns
[i
] = posns
[i
]->next
;
183 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
188 subposns
[i
] = df_conflict_list
;
189 posns
[i
] = posns
[i
]->next
;
195 else if (i
+ 1 < o
->head_idx
)
197 else if (i
+ 1 > o
->head_idx
)
202 ce
= xcalloc(1, ce_size
);
203 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
204 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
206 memcpy(ce
->name
, base
, baselen
);
207 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
211 hashcpy(ce
->sha1
, posns
[i
]->sha1
);
212 src
[i
+ o
->merge
] = ce
;
213 subposns
[i
] = df_conflict_list
;
214 posns
[i
] = posns
[i
]->next
;
219 while (o
->pos
< active_nr
&&
220 !strcmp(active_cache
[o
->pos
]->name
,
223 } else if (o
->merge
) {
227 fprintf(stderr
, "%s:\n", first
);
228 for (i
= 0; i
< src_size
; i
++) {
229 fprintf(stderr
, " %d ", i
);
231 fprintf(stderr
, "%06x %s\n", src
[i
]->ce_mode
, sha1_to_hex(src
[i
]->sha1
));
233 fprintf(stderr
, "\n");
236 ret
= o
->fn(src
, o
, remove
);
241 fprintf(stderr
, "Added %d entries\n", ret
);
245 remove_entry(remove
);
246 for (i
= 0; i
< src_size
; i
++) {
248 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
254 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
255 memcpy(newbase
, base
, baselen
);
256 memcpy(newbase
+ baselen
, first
, pathlen
);
257 newbase
[baselen
+ pathlen
] = '/';
258 newbase
[baselen
+ pathlen
+ 1] = '\0';
259 if (unpack_trees_rec(subposns
, len
, newbase
, o
,
262 goto leave_directory
;
274 /* Unlink the last component and attempt to remove leading
275 * directories, in case this unlink is the removal of the
276 * last entry in the directory -- empty directories are removed.
278 static void unlink_entry(char *name
, char *last_symlink
)
282 if (has_symlink_leading_path(name
, last_symlink
))
289 cp
= strrchr(name
, '/');
296 status
= rmdir(name
);
305 static struct checkout state
;
306 static void check_updates(struct unpack_trees_options
*o
)
308 unsigned cnt
= 0, total
= 0;
309 struct progress
*progress
= NULL
;
310 char last_symlink
[PATH_MAX
];
313 if (o
->update
&& o
->verbose_update
) {
314 for (total
= cnt
= 0; cnt
< active_nr
; cnt
++) {
315 struct cache_entry
*ce
= active_cache
[cnt
];
316 if (ce
->ce_flags
& (CE_UPDATE
| CE_REMOVE
))
320 progress
= start_progress_delay("Checking out files",
325 *last_symlink
= '\0';
326 for (i
= 0; i
< active_nr
; i
++) {
327 struct cache_entry
*ce
= active_cache
[i
];
329 if (ce
->ce_flags
& (CE_UPDATE
| CE_REMOVE
))
330 display_progress(progress
, ++cnt
);
331 if (ce
->ce_flags
& CE_REMOVE
) {
333 unlink_entry(ce
->name
, last_symlink
);
334 remove_cache_entry_at(i
);
338 if (ce
->ce_flags
& CE_UPDATE
) {
339 ce
->ce_flags
&= ~CE_UPDATE
;
341 checkout_entry(ce
, &state
, NULL
);
342 *last_symlink
= '\0';
346 stop_progress(&progress
);
349 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
351 struct tree_entry_list
**posns
;
353 struct tree_entry_list df_conflict_list
;
354 static struct cache_entry
*dfc
;
356 memset(&df_conflict_list
, 0, sizeof(df_conflict_list
));
357 df_conflict_list
.next
= &df_conflict_list
;
358 memset(&state
, 0, sizeof(state
));
362 state
.refresh_cache
= 1;
367 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
368 o
->df_conflict_entry
= dfc
;
371 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
372 for (i
= 0; i
< len
; i
++)
373 posns
[i
] = create_tree_entry_list(t
+i
);
375 if (unpack_trees_rec(posns
, len
, o
->prefix
? o
->prefix
: "",
376 o
, &df_conflict_list
)) {
385 if (o
->trivial_merges_only
&& o
->nontrivial_merge
) {
390 return o
->gently
? -1 :
391 error("Merge requires file-level merging");
398 /* Here come the merge functions */
400 static int reject_merge(struct cache_entry
*ce
)
402 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
406 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
412 return a
->ce_mode
== b
->ce_mode
&&
413 !hashcmp(a
->sha1
, b
->sha1
);
418 * When a CE gets turned into an unmerged entry, we
419 * want it to be up-to-date
421 static int verify_uptodate(struct cache_entry
*ce
,
422 struct unpack_trees_options
*o
)
426 if (o
->index_only
|| o
->reset
)
429 if (!lstat(ce
->name
, &st
)) {
430 unsigned changed
= ce_match_stat(ce
, &st
, CE_MATCH_IGNORE_VALID
);
434 * NEEDSWORK: the current default policy is to allow
435 * submodule to be out of sync wrt the supermodule
436 * index. This needs to be tightened later for
437 * submodules that are marked to be automatically
440 if (S_ISGITLINK(ce
->ce_mode
))
446 return o
->gently
? -1 :
447 error("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
450 static void invalidate_ce_path(struct cache_entry
*ce
)
453 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
457 * Check that checking out ce->sha1 in subdir ce->name is not
458 * going to overwrite any working files.
460 * Currently, git does not checkout subprojects during a superproject
461 * checkout, so it is not going to overwrite anything.
463 static int verify_clean_submodule(struct cache_entry
*ce
, const char *action
,
464 struct unpack_trees_options
*o
)
469 static int verify_clean_subdirectory(struct cache_entry
*ce
, const char *action
,
470 struct unpack_trees_options
*o
)
473 * we are about to extract "ce->name"; we would not want to lose
474 * anything in the existing directory there.
481 unsigned char sha1
[20];
483 if (S_ISGITLINK(ce
->ce_mode
) &&
484 resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) == 0) {
485 /* If we are not going to update the submodule, then
488 if (!hashcmp(sha1
, ce
->sha1
))
490 return verify_clean_submodule(ce
, action
, o
);
494 * First let's make sure we do not have a local modification
497 namelen
= strlen(ce
->name
);
498 pos
= cache_name_pos(ce
->name
, namelen
);
500 return cnt
; /* we have it as nondirectory */
502 for (i
= pos
; i
< active_nr
; i
++) {
503 struct cache_entry
*ce
= active_cache
[i
];
504 int len
= ce_namelen(ce
);
506 strncmp(ce
->name
, ce
->name
, namelen
) ||
507 ce
->name
[namelen
] != '/')
510 * ce->name is an entry in the subdirectory.
513 if (verify_uptodate(ce
, o
))
515 ce
->ce_flags
|= CE_REMOVE
;
521 * Then we need to make sure that we do not lose a locally
522 * present file that is not ignored.
524 pathbuf
= xmalloc(namelen
+ 2);
525 memcpy(pathbuf
, ce
->name
, namelen
);
526 strcpy(pathbuf
+namelen
, "/");
528 memset(&d
, 0, sizeof(d
));
530 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
531 i
= read_directory(&d
, ce
->name
, pathbuf
, namelen
+1, NULL
);
533 return o
->gently
? -1 :
534 error("Updating '%s' would lose untracked files in it",
541 * We do not want to remove or overwrite a working tree file that
542 * is not tracked, unless it is ignored.
544 static int verify_absent(struct cache_entry
*ce
, const char *action
,
545 struct unpack_trees_options
*o
)
549 if (o
->index_only
|| o
->reset
|| !o
->update
)
552 if (has_symlink_leading_path(ce
->name
, NULL
))
555 if (!lstat(ce
->name
, &st
)) {
557 int dtype
= ce_to_dtype(ce
);
559 if (o
->dir
&& excluded(o
->dir
, ce
->name
, &dtype
))
561 * ce->name is explicitly excluded, so it is Ok to
565 if (S_ISDIR(st
.st_mode
)) {
567 * We are checking out path "foo" and
568 * found "foo/." in the working tree.
569 * This is tricky -- if we have modified
570 * files that are in "foo/" we would lose
573 cnt
= verify_clean_subdirectory(ce
, action
, o
);
576 * If this removed entries from the index,
577 * what that means is:
579 * (1) the caller unpack_trees_rec() saw path/foo
580 * in the index, and it has not removed it because
581 * it thinks it is handling 'path' as blob with
583 * (2) we will return "ok, we placed a merged entry
584 * in the index" which would cause o->pos to be
585 * incremented by one;
586 * (3) however, original o->pos now has 'path/foo'
587 * marked with "to be removed".
589 * We need to increment it by the number of
590 * deleted entries here.
597 * The previous round may already have decided to
598 * delete this path, which is in a subdirectory that
599 * is being replaced with a blob.
601 cnt
= cache_name_pos(ce
->name
, strlen(ce
->name
));
603 struct cache_entry
*ce
= active_cache
[cnt
];
604 if (ce
->ce_flags
& CE_REMOVE
)
608 return o
->gently
? -1 :
609 error("Untracked working tree file '%s' "
610 "would be %s by merge.", ce
->name
, action
);
615 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
616 struct unpack_trees_options
*o
)
618 merge
->ce_flags
|= CE_UPDATE
;
621 * See if we can re-use the old CE directly?
622 * That way we get the uptodate stat info.
624 * This also removes the UPDATE flag on
627 if (same(old
, merge
)) {
628 copy_cache_entry(merge
, old
);
630 if (verify_uptodate(old
, o
))
632 invalidate_ce_path(old
);
636 if (verify_absent(merge
, "overwritten", o
))
638 invalidate_ce_path(merge
);
641 merge
->ce_flags
&= ~CE_STAGEMASK
;
642 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
646 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
647 struct unpack_trees_options
*o
)
650 if (verify_uptodate(old
, o
))
653 if (verify_absent(ce
, "removed", o
))
655 ce
->ce_flags
|= CE_REMOVE
;
656 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
657 invalidate_ce_path(ce
);
661 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
663 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
668 static void show_stage_entry(FILE *o
,
669 const char *label
, const struct cache_entry
*ce
)
672 fprintf(o
, "%s (missing)\n", label
);
674 fprintf(o
, "%s%06o %s %d\t%s\n",
677 sha1_to_hex(ce
->sha1
),
683 int threeway_merge(struct cache_entry
**stages
,
684 struct unpack_trees_options
*o
,
687 struct cache_entry
*index
;
688 struct cache_entry
*head
;
689 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
692 int remote_match
= 0;
694 int df_conflict_head
= 0;
695 int df_conflict_remote
= 0;
697 int any_anc_missing
= 0;
698 int no_anc_exists
= 1;
701 for (i
= 1; i
< o
->head_idx
; i
++) {
702 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
709 head
= stages
[o
->head_idx
];
711 if (head
== o
->df_conflict_entry
) {
712 df_conflict_head
= 1;
716 if (remote
== o
->df_conflict_entry
) {
717 df_conflict_remote
= 1;
721 /* First, if there's a #16 situation, note that to prevent #13
724 if (!same(remote
, head
)) {
725 for (i
= 1; i
< o
->head_idx
; i
++) {
726 if (same(stages
[i
], head
)) {
729 if (same(stages
[i
], remote
)) {
735 /* We start with cases where the index is allowed to match
736 * something other than the head: #14(ALT) and #2ALT, where it
737 * is permitted to match the result instead.
739 /* #14, #14ALT, #2ALT */
740 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
741 if (index
&& !same(index
, remote
) && !same(index
, head
))
742 return o
->gently
? -1 : reject_merge(index
);
743 return merged_entry(remote
, index
, o
);
746 * If we have an entry in the index cache, then we want to
747 * make sure that it matches head.
749 if (index
&& !same(index
, head
))
750 return o
->gently
? -1 : reject_merge(index
);
754 if (same(head
, remote
))
755 return merged_entry(head
, index
, o
);
757 if (!df_conflict_remote
&& remote_match
&& !head_match
)
758 return merged_entry(head
, index
, o
);
762 if (!head
&& !remote
&& any_anc_missing
) {
763 remove_entry(remove
);
767 /* Under the new "aggressive" rule, we resolve mostly trivial
768 * cases that we historically had git-merge-one-file resolve.
771 int head_deleted
= !head
&& !df_conflict_head
;
772 int remote_deleted
= !remote
&& !df_conflict_remote
;
773 struct cache_entry
*ce
= NULL
;
782 for (i
= 1; i
< o
->head_idx
; i
++) {
783 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
792 * Deleted in one and unchanged in the other.
794 if ((head_deleted
&& remote_deleted
) ||
795 (head_deleted
&& remote
&& remote_match
) ||
796 (remote_deleted
&& head
&& head_match
)) {
797 remove_entry(remove
);
799 return deleted_entry(index
, index
, o
);
800 else if (ce
&& !head_deleted
) {
801 if (verify_absent(ce
, "removed", o
))
807 * Added in both, identically.
809 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
810 return merged_entry(head
, index
, o
);
814 /* Below are "no merge" cases, which require that the index be
815 * up-to-date to avoid the files getting overwritten with
816 * conflict resolution files.
819 if (verify_uptodate(index
, o
))
823 remove_entry(remove
);
824 o
->nontrivial_merge
= 1;
826 /* #2, #3, #4, #6, #7, #9, #10, #11. */
828 if (!head_match
|| !remote_match
) {
829 for (i
= 1; i
< o
->head_idx
; i
++) {
830 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
831 keep_entry(stages
[i
], o
);
839 fprintf(stderr
, "read-tree: warning #16 detected\n");
840 show_stage_entry(stderr
, "head ", stages
[head_match
]);
841 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
844 if (head
) { count
+= keep_entry(head
, o
); }
845 if (remote
) { count
+= keep_entry(remote
, o
); }
852 * The rule is to "carry forward" what is in the index without losing
853 * information across a "fast forward", favoring a successful merge
854 * over a merge failure when it makes sense. For details of the
855 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
858 int twoway_merge(struct cache_entry
**src
,
859 struct unpack_trees_options
*o
,
862 struct cache_entry
*current
= src
[0];
863 struct cache_entry
*oldtree
= src
[1];
864 struct cache_entry
*newtree
= src
[2];
866 if (o
->merge_size
!= 2)
867 return error("Cannot do a twoway merge of %d trees",
870 if (oldtree
== o
->df_conflict_entry
)
872 if (newtree
== o
->df_conflict_entry
)
876 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
877 (!oldtree
&& newtree
&&
878 same(current
, newtree
)) || /* 6 and 7 */
879 (oldtree
&& newtree
&&
880 same(oldtree
, newtree
)) || /* 14 and 15 */
881 (oldtree
&& newtree
&&
882 !same(oldtree
, newtree
) && /* 18 and 19 */
883 same(current
, newtree
))) {
884 return keep_entry(current
, o
);
886 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
888 remove_entry(remove
);
889 return deleted_entry(oldtree
, current
, o
);
891 else if (oldtree
&& newtree
&&
892 same(current
, oldtree
) && !same(current
, newtree
)) {
894 return merged_entry(newtree
, current
, o
);
897 /* all other failures */
898 remove_entry(remove
);
900 return o
->gently
? -1 : reject_merge(oldtree
);
902 return o
->gently
? -1 : reject_merge(current
);
904 return o
->gently
? -1 : reject_merge(newtree
);
909 return merged_entry(newtree
, current
, o
);
910 remove_entry(remove
);
911 return deleted_entry(oldtree
, current
, o
);
917 * Keep the index entries at stage0, collapse stage1 but make sure
918 * stage0 does not have anything there.
920 int bind_merge(struct cache_entry
**src
,
921 struct unpack_trees_options
*o
,
924 struct cache_entry
*old
= src
[0];
925 struct cache_entry
*a
= src
[1];
927 if (o
->merge_size
!= 1)
928 return error("Cannot do a bind merge of %d trees\n",
931 return o
->gently
? -1 :
932 error("Entry '%s' overlaps. Cannot bind.", a
->name
);
934 return keep_entry(old
, o
);
936 return merged_entry(a
, NULL
, o
);
943 * - take the stat information from stage0, take the data from stage1
945 int oneway_merge(struct cache_entry
**src
,
946 struct unpack_trees_options
*o
,
949 struct cache_entry
*old
= src
[0];
950 struct cache_entry
*a
= src
[1];
952 if (o
->merge_size
!= 1)
953 return error("Cannot do a oneway merge of %d trees",
957 remove_entry(remove
);
958 return deleted_entry(old
, old
, o
);
960 if (old
&& same(old
, a
)) {
963 if (lstat(old
->name
, &st
) ||
964 ce_match_stat(old
, &st
, CE_MATCH_IGNORE_VALID
))
965 old
->ce_flags
|= CE_UPDATE
;
967 return keep_entry(old
, o
);
969 return merged_entry(a
, old
, o
);