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
;
89 /* Find the first name in the input. */
95 if (o
->merge
&& o
->pos
< active_nr
) {
96 /* This is a bit tricky: */
97 /* If the index has a subdirectory (with
98 * contents) as the first name, it'll get a
99 * filename like "foo/bar". But that's after
100 * "foo", so the entry in trees will get
101 * handled first, at which point we'll go into
102 * "foo", and deal with "bar" from the index,
103 * because the base will be "foo/". The only
104 * way we can actually have "foo/bar" first of
105 * all the things is if the trees don't
106 * contain "foo" at all, in which case we'll
107 * handle "foo/bar" without going into the
108 * directory, but that's fine (and will return
109 * an error anyway, with the added unknown
113 cache_name
= active_cache
[o
->pos
]->name
;
114 if (strlen(cache_name
) > baselen
&&
115 !memcmp(cache_name
, base
, baselen
)) {
116 cache_name
+= baselen
;
125 fprintf(stderr
, "index %s\n", first
);
127 for (i
= 0; i
< len
; i
++) {
128 if (!posns
[i
] || posns
[i
] == df_conflict_list
)
131 fprintf(stderr
, "%d %s\n", i
+ 1, posns
[i
]->name
);
133 if (!first
|| entcmp(first
, firstdir
,
135 S_ISDIR(posns
[i
]->mode
)) > 0) {
136 first
= posns
[i
]->name
;
137 firstdir
= S_ISDIR(posns
[i
]->mode
);
140 /* No name means we're done */
142 goto leave_directory
;
144 pathlen
= strlen(first
);
145 ce_size
= cache_entry_size(baselen
+ pathlen
);
147 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
149 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
152 if (cache_name
&& !strcmp(cache_name
, first
)) {
154 src
[0] = active_cache
[o
->pos
];
158 for (i
= 0; i
< len
; i
++) {
159 struct cache_entry
*ce
;
162 (posns
[i
] != df_conflict_list
&&
163 strcmp(first
, posns
[i
]->name
))) {
167 if (posns
[i
] == df_conflict_list
) {
168 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
172 if (S_ISDIR(posns
[i
]->mode
)) {
173 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
177 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
178 subposns
[i
] = create_tree_entry_list(&t
);
179 posns
[i
] = posns
[i
]->next
;
180 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
186 else if (i
+ 1 < o
->head_idx
)
188 else if (i
+ 1 > o
->head_idx
)
193 ce
= xcalloc(1, ce_size
);
194 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
195 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
197 memcpy(ce
->name
, base
, baselen
);
198 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
202 hashcpy(ce
->sha1
, posns
[i
]->sha1
);
203 src
[i
+ o
->merge
] = ce
;
204 subposns
[i
] = df_conflict_list
;
205 posns
[i
] = posns
[i
]->next
;
212 fprintf(stderr
, "%s:\n", first
);
213 for (i
= 0; i
< src_size
; i
++) {
214 fprintf(stderr
, " %d ", i
);
216 fprintf(stderr
, "%06x %s\n", src
[i
]->ce_mode
, sha1_to_hex(src
[i
]->sha1
));
218 fprintf(stderr
, "\n");
221 ret
= o
->fn(src
, o
, remove
);
226 fprintf(stderr
, "Added %d entries\n", ret
);
230 remove_entry(remove
);
231 for (i
= 0; i
< src_size
; i
++) {
233 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
239 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
240 memcpy(newbase
, base
, baselen
);
241 memcpy(newbase
+ baselen
, first
, pathlen
);
242 newbase
[baselen
+ pathlen
] = '/';
243 newbase
[baselen
+ pathlen
+ 1] = '\0';
244 if (unpack_trees_rec(subposns
, len
, newbase
, o
,
247 goto leave_directory
;
259 /* Unlink the last component and attempt to remove leading
260 * directories, in case this unlink is the removal of the
261 * last entry in the directory -- empty directories are removed.
263 static void unlink_entry(char *name
, char *last_symlink
)
267 if (has_symlink_leading_path(name
, last_symlink
))
274 cp
= strrchr(name
, '/');
281 status
= rmdir(name
);
290 static struct checkout state
;
291 static void check_updates(struct unpack_trees_options
*o
)
293 unsigned cnt
= 0, total
= 0;
294 struct progress
*progress
= NULL
;
295 char last_symlink
[PATH_MAX
];
298 if (o
->update
&& o
->verbose_update
) {
299 for (total
= cnt
= 0; cnt
< active_nr
; cnt
++) {
300 struct cache_entry
*ce
= active_cache
[cnt
];
301 if (ce
->ce_flags
& (CE_UPDATE
| CE_REMOVE
))
305 progress
= start_progress_delay("Checking out files",
310 *last_symlink
= '\0';
311 for (i
= 0; i
< active_nr
; i
++) {
312 struct cache_entry
*ce
= active_cache
[i
];
314 if (ce
->ce_flags
& (CE_UPDATE
| CE_REMOVE
))
315 display_progress(progress
, ++cnt
);
316 if (ce
->ce_flags
& CE_REMOVE
) {
318 unlink_entry(ce
->name
, last_symlink
);
319 remove_cache_entry_at(i
);
323 if (ce
->ce_flags
& CE_UPDATE
) {
324 ce
->ce_flags
&= ~CE_UPDATE
;
326 checkout_entry(ce
, &state
, NULL
);
327 *last_symlink
= '\0';
331 stop_progress(&progress
);
334 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
336 struct tree_entry_list
**posns
;
338 struct tree_entry_list df_conflict_list
;
339 static struct cache_entry
*dfc
;
341 memset(&df_conflict_list
, 0, sizeof(df_conflict_list
));
342 df_conflict_list
.next
= &df_conflict_list
;
343 memset(&state
, 0, sizeof(state
));
347 state
.refresh_cache
= 1;
352 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
353 o
->df_conflict_entry
= dfc
;
356 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
357 for (i
= 0; i
< len
; i
++)
358 posns
[i
] = create_tree_entry_list(t
+i
);
360 if (unpack_trees_rec(posns
, len
, o
->prefix
? o
->prefix
: "",
361 o
, &df_conflict_list
)) {
370 if (o
->trivial_merges_only
&& o
->nontrivial_merge
) {
375 return o
->gently
? -1 :
376 error("Merge requires file-level merging");
383 /* Here come the merge functions */
385 static int reject_merge(struct cache_entry
*ce
)
387 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
391 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
397 return a
->ce_mode
== b
->ce_mode
&&
398 !hashcmp(a
->sha1
, b
->sha1
);
403 * When a CE gets turned into an unmerged entry, we
404 * want it to be up-to-date
406 static int verify_uptodate(struct cache_entry
*ce
,
407 struct unpack_trees_options
*o
)
411 if (o
->index_only
|| o
->reset
)
414 if (!lstat(ce
->name
, &st
)) {
415 unsigned changed
= ce_match_stat(ce
, &st
, CE_MATCH_IGNORE_VALID
);
419 * NEEDSWORK: the current default policy is to allow
420 * submodule to be out of sync wrt the supermodule
421 * index. This needs to be tightened later for
422 * submodules that are marked to be automatically
425 if (S_ISGITLINK(ce
->ce_mode
))
431 return o
->gently
? -1 :
432 error("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
435 static void invalidate_ce_path(struct cache_entry
*ce
)
438 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
442 * Check that checking out ce->sha1 in subdir ce->name is not
443 * going to overwrite any working files.
445 * Currently, git does not checkout subprojects during a superproject
446 * checkout, so it is not going to overwrite anything.
448 static int verify_clean_submodule(struct cache_entry
*ce
, const char *action
,
449 struct unpack_trees_options
*o
)
454 static int verify_clean_subdirectory(struct cache_entry
*ce
, const char *action
,
455 struct unpack_trees_options
*o
)
458 * we are about to extract "ce->name"; we would not want to lose
459 * anything in the existing directory there.
466 unsigned char sha1
[20];
468 if (S_ISGITLINK(ce
->ce_mode
) &&
469 resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) == 0) {
470 /* If we are not going to update the submodule, then
473 if (!hashcmp(sha1
, ce
->sha1
))
475 return verify_clean_submodule(ce
, action
, o
);
479 * First let's make sure we do not have a local modification
482 namelen
= strlen(ce
->name
);
483 pos
= cache_name_pos(ce
->name
, namelen
);
485 return cnt
; /* we have it as nondirectory */
487 for (i
= pos
; i
< active_nr
; i
++) {
488 struct cache_entry
*ce
= active_cache
[i
];
489 int len
= ce_namelen(ce
);
491 strncmp(ce
->name
, ce
->name
, namelen
) ||
492 ce
->name
[namelen
] != '/')
495 * ce->name is an entry in the subdirectory.
498 if (verify_uptodate(ce
, o
))
500 ce
->ce_flags
|= CE_REMOVE
;
506 * Then we need to make sure that we do not lose a locally
507 * present file that is not ignored.
509 pathbuf
= xmalloc(namelen
+ 2);
510 memcpy(pathbuf
, ce
->name
, namelen
);
511 strcpy(pathbuf
+namelen
, "/");
513 memset(&d
, 0, sizeof(d
));
515 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
516 i
= read_directory(&d
, ce
->name
, pathbuf
, namelen
+1, NULL
);
518 return o
->gently
? -1 :
519 error("Updating '%s' would lose untracked files in it",
526 * We do not want to remove or overwrite a working tree file that
527 * is not tracked, unless it is ignored.
529 static int verify_absent(struct cache_entry
*ce
, const char *action
,
530 struct unpack_trees_options
*o
)
534 if (o
->index_only
|| o
->reset
|| !o
->update
)
537 if (has_symlink_leading_path(ce
->name
, NULL
))
540 if (!lstat(ce
->name
, &st
)) {
543 if (o
->dir
&& excluded(o
->dir
, ce
->name
))
545 * ce->name is explicitly excluded, so it is Ok to
549 if (S_ISDIR(st
.st_mode
)) {
551 * We are checking out path "foo" and
552 * found "foo/." in the working tree.
553 * This is tricky -- if we have modified
554 * files that are in "foo/" we would lose
557 cnt
= verify_clean_subdirectory(ce
, action
, o
);
560 * If this removed entries from the index,
561 * what that means is:
563 * (1) the caller unpack_trees_rec() saw path/foo
564 * in the index, and it has not removed it because
565 * it thinks it is handling 'path' as blob with
567 * (2) we will return "ok, we placed a merged entry
568 * in the index" which would cause o->pos to be
569 * incremented by one;
570 * (3) however, original o->pos now has 'path/foo'
571 * marked with "to be removed".
573 * We need to increment it by the number of
574 * deleted entries here.
581 * The previous round may already have decided to
582 * delete this path, which is in a subdirectory that
583 * is being replaced with a blob.
585 cnt
= cache_name_pos(ce
->name
, strlen(ce
->name
));
587 struct cache_entry
*ce
= active_cache
[cnt
];
588 if (ce
->ce_flags
& CE_REMOVE
)
592 return o
->gently
? -1 :
593 error("Untracked working tree file '%s' "
594 "would be %s by merge.", ce
->name
, action
);
599 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
600 struct unpack_trees_options
*o
)
602 merge
->ce_flags
|= CE_UPDATE
;
605 * See if we can re-use the old CE directly?
606 * That way we get the uptodate stat info.
608 * This also removes the UPDATE flag on
611 if (same(old
, merge
)) {
612 memcpy(merge
, old
, offsetof(struct cache_entry
, name
));
614 if (verify_uptodate(old
, o
))
616 invalidate_ce_path(old
);
620 if (verify_absent(merge
, "overwritten", o
))
622 invalidate_ce_path(merge
);
625 merge
->ce_flags
&= ~CE_STAGEMASK
;
626 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
630 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
631 struct unpack_trees_options
*o
)
634 if (verify_uptodate(old
, o
))
637 if (verify_absent(ce
, "removed", o
))
639 ce
->ce_flags
|= CE_REMOVE
;
640 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
641 invalidate_ce_path(ce
);
645 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
647 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
652 static void show_stage_entry(FILE *o
,
653 const char *label
, const struct cache_entry
*ce
)
656 fprintf(o
, "%s (missing)\n", label
);
658 fprintf(o
, "%s%06o %s %d\t%s\n",
661 sha1_to_hex(ce
->sha1
),
667 int threeway_merge(struct cache_entry
**stages
,
668 struct unpack_trees_options
*o
,
671 struct cache_entry
*index
;
672 struct cache_entry
*head
;
673 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
676 int remote_match
= 0;
678 int df_conflict_head
= 0;
679 int df_conflict_remote
= 0;
681 int any_anc_missing
= 0;
682 int no_anc_exists
= 1;
685 for (i
= 1; i
< o
->head_idx
; i
++) {
686 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
693 head
= stages
[o
->head_idx
];
695 if (head
== o
->df_conflict_entry
) {
696 df_conflict_head
= 1;
700 if (remote
== o
->df_conflict_entry
) {
701 df_conflict_remote
= 1;
705 /* First, if there's a #16 situation, note that to prevent #13
708 if (!same(remote
, head
)) {
709 for (i
= 1; i
< o
->head_idx
; i
++) {
710 if (same(stages
[i
], head
)) {
713 if (same(stages
[i
], remote
)) {
719 /* We start with cases where the index is allowed to match
720 * something other than the head: #14(ALT) and #2ALT, where it
721 * is permitted to match the result instead.
723 /* #14, #14ALT, #2ALT */
724 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
725 if (index
&& !same(index
, remote
) && !same(index
, head
))
726 return o
->gently
? -1 : reject_merge(index
);
727 return merged_entry(remote
, index
, o
);
730 * If we have an entry in the index cache, then we want to
731 * make sure that it matches head.
733 if (index
&& !same(index
, head
)) {
734 return o
->gently
? -1 : reject_merge(index
);
739 if (same(head
, remote
))
740 return merged_entry(head
, index
, o
);
742 if (!df_conflict_remote
&& remote_match
&& !head_match
)
743 return merged_entry(head
, index
, o
);
747 if (!head
&& !remote
&& any_anc_missing
) {
748 remove_entry(remove
);
752 /* Under the new "aggressive" rule, we resolve mostly trivial
753 * cases that we historically had git-merge-one-file resolve.
756 int head_deleted
= !head
&& !df_conflict_head
;
757 int remote_deleted
= !remote
&& !df_conflict_remote
;
758 struct cache_entry
*ce
= NULL
;
767 for (i
= 1; i
< o
->head_idx
; i
++) {
768 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
777 * Deleted in one and unchanged in the other.
779 if ((head_deleted
&& remote_deleted
) ||
780 (head_deleted
&& remote
&& remote_match
) ||
781 (remote_deleted
&& head
&& head_match
)) {
782 remove_entry(remove
);
784 return deleted_entry(index
, index
, o
);
785 else if (ce
&& !head_deleted
) {
786 if (verify_absent(ce
, "removed", o
))
792 * Added in both, identically.
794 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
795 return merged_entry(head
, index
, o
);
799 /* Below are "no merge" cases, which require that the index be
800 * up-to-date to avoid the files getting overwritten with
801 * conflict resolution files.
804 if (verify_uptodate(index
, o
))
808 remove_entry(remove
);
809 o
->nontrivial_merge
= 1;
811 /* #2, #3, #4, #6, #7, #9, #10, #11. */
813 if (!head_match
|| !remote_match
) {
814 for (i
= 1; i
< o
->head_idx
; i
++) {
815 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
816 keep_entry(stages
[i
], o
);
824 fprintf(stderr
, "read-tree: warning #16 detected\n");
825 show_stage_entry(stderr
, "head ", stages
[head_match
]);
826 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
829 if (head
) { count
+= keep_entry(head
, o
); }
830 if (remote
) { count
+= keep_entry(remote
, o
); }
837 * The rule is to "carry forward" what is in the index without losing
838 * information across a "fast forward", favoring a successful merge
839 * over a merge failure when it makes sense. For details of the
840 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
843 int twoway_merge(struct cache_entry
**src
,
844 struct unpack_trees_options
*o
,
847 struct cache_entry
*current
= src
[0];
848 struct cache_entry
*oldtree
= src
[1];
849 struct cache_entry
*newtree
= src
[2];
851 if (o
->merge_size
!= 2)
852 return error("Cannot do a twoway merge of %d trees",
855 if (oldtree
== o
->df_conflict_entry
)
857 if (newtree
== o
->df_conflict_entry
)
861 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
862 (!oldtree
&& newtree
&&
863 same(current
, newtree
)) || /* 6 and 7 */
864 (oldtree
&& newtree
&&
865 same(oldtree
, newtree
)) || /* 14 and 15 */
866 (oldtree
&& newtree
&&
867 !same(oldtree
, newtree
) && /* 18 and 19 */
868 same(current
, newtree
))) {
869 return keep_entry(current
, o
);
871 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
873 remove_entry(remove
);
874 return deleted_entry(oldtree
, current
, o
);
876 else if (oldtree
&& newtree
&&
877 same(current
, oldtree
) && !same(current
, newtree
)) {
879 return merged_entry(newtree
, current
, o
);
882 /* all other failures */
883 remove_entry(remove
);
885 return o
->gently
? -1 : reject_merge(oldtree
);
887 return o
->gently
? -1 : reject_merge(current
);
889 return o
->gently
? -1 : reject_merge(newtree
);
894 return merged_entry(newtree
, current
, o
);
895 remove_entry(remove
);
896 return deleted_entry(oldtree
, current
, o
);
902 * Keep the index entries at stage0, collapse stage1 but make sure
903 * stage0 does not have anything there.
905 int bind_merge(struct cache_entry
**src
,
906 struct unpack_trees_options
*o
,
909 struct cache_entry
*old
= src
[0];
910 struct cache_entry
*a
= src
[1];
912 if (o
->merge_size
!= 1)
913 return error("Cannot do a bind merge of %d trees\n",
916 return o
->gently
? -1 :
917 error("Entry '%s' overlaps. Cannot bind.", a
->name
);
919 return keep_entry(old
, o
);
921 return merged_entry(a
, NULL
, o
);
928 * - take the stat information from stage0, take the data from stage1
930 int oneway_merge(struct cache_entry
**src
,
931 struct unpack_trees_options
*o
,
934 struct cache_entry
*old
= src
[0];
935 struct cache_entry
*a
= src
[1];
937 if (o
->merge_size
!= 1)
938 return error("Cannot do a oneway merge of %d trees",
942 remove_entry(remove
);
943 return deleted_entry(old
, old
, o
);
945 if (old
&& same(old
, a
)) {
948 if (lstat(old
->name
, &st
) ||
949 ce_match_stat(old
, &st
, CE_MATCH_IGNORE_VALID
))
950 old
->ce_flags
|= CE_UPDATE
;
952 return keep_entry(old
, o
);
954 return merged_entry(a
, old
, o
);