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 printf("index %s\n", first
);
127 for (i
= 0; i
< len
; i
++) {
128 if (!posns
[i
] || posns
[i
] == df_conflict_list
)
131 printf("%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 printf("%s:\n", first
);
213 for (i
= 0; i
< src_size
; i
++) {
216 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
221 ret
= o
->fn(src
, o
, remove
);
226 printf("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 cache_entry
**src
, int nr
,
292 struct unpack_trees_options
*o
)
294 unsigned cnt
= 0, total
= 0;
295 struct progress
*progress
= NULL
;
296 char last_symlink
[PATH_MAX
];
298 if (o
->update
&& o
->verbose_update
) {
299 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
300 struct cache_entry
*ce
= src
[cnt
];
301 if (ce
->ce_flags
& (CE_UPDATE
| CE_REMOVE
))
305 progress
= start_progress_delay("Checking out files",
310 *last_symlink
= '\0';
312 struct cache_entry
*ce
= *src
++;
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
);
321 if (ce
->ce_flags
& CE_UPDATE
) {
322 ce
->ce_flags
&= ~CE_UPDATE
;
324 checkout_entry(ce
, &state
, NULL
);
325 *last_symlink
= '\0';
329 stop_progress(&progress
);
332 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
334 struct tree_entry_list
**posns
;
336 struct tree_entry_list df_conflict_list
;
337 static struct cache_entry
*dfc
;
339 memset(&df_conflict_list
, 0, sizeof(df_conflict_list
));
340 df_conflict_list
.next
= &df_conflict_list
;
341 memset(&state
, 0, sizeof(state
));
345 state
.refresh_cache
= 1;
350 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
351 o
->df_conflict_entry
= dfc
;
354 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
355 for (i
= 0; i
< len
; i
++)
356 posns
[i
] = create_tree_entry_list(t
+i
);
358 if (unpack_trees_rec(posns
, len
, o
->prefix
? o
->prefix
: "",
359 o
, &df_conflict_list
))
363 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
364 return error("Merge requires file-level merging");
366 check_updates(active_cache
, active_nr
, o
);
370 /* Here come the merge functions */
372 static int reject_merge(struct cache_entry
*ce
)
374 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
378 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
384 return a
->ce_mode
== b
->ce_mode
&&
385 !hashcmp(a
->sha1
, b
->sha1
);
390 * When a CE gets turned into an unmerged entry, we
391 * want it to be up-to-date
393 static int verify_uptodate(struct cache_entry
*ce
,
394 struct unpack_trees_options
*o
)
398 if (o
->index_only
|| o
->reset
)
401 if (!lstat(ce
->name
, &st
)) {
402 unsigned changed
= ce_match_stat(ce
, &st
, CE_MATCH_IGNORE_VALID
);
406 * NEEDSWORK: the current default policy is to allow
407 * submodule to be out of sync wrt the supermodule
408 * index. This needs to be tightened later for
409 * submodules that are marked to be automatically
412 if (S_ISGITLINK(ce
->ce_mode
))
418 return error("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
421 static void invalidate_ce_path(struct cache_entry
*ce
)
424 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
428 * Check that checking out ce->sha1 in subdir ce->name is not
429 * going to overwrite any working files.
431 * Currently, git does not checkout subprojects during a superproject
432 * checkout, so it is not going to overwrite anything.
434 static int verify_clean_submodule(struct cache_entry
*ce
, const char *action
,
435 struct unpack_trees_options
*o
)
440 static int verify_clean_subdirectory(struct cache_entry
*ce
, const char *action
,
441 struct unpack_trees_options
*o
)
444 * we are about to extract "ce->name"; we would not want to lose
445 * anything in the existing directory there.
452 unsigned char sha1
[20];
454 if (S_ISGITLINK(ce
->ce_mode
) &&
455 resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) == 0) {
456 /* If we are not going to update the submodule, then
459 if (!hashcmp(sha1
, ce
->sha1
))
461 return verify_clean_submodule(ce
, action
, o
);
465 * First let's make sure we do not have a local modification
468 namelen
= strlen(ce
->name
);
469 pos
= cache_name_pos(ce
->name
, namelen
);
471 return cnt
; /* we have it as nondirectory */
473 for (i
= pos
; i
< active_nr
; i
++) {
474 struct cache_entry
*ce
= active_cache
[i
];
475 int len
= ce_namelen(ce
);
477 strncmp(ce
->name
, ce
->name
, namelen
) ||
478 ce
->name
[namelen
] != '/')
481 * ce->name is an entry in the subdirectory.
484 if (verify_uptodate(ce
, o
))
486 ce
->ce_flags
|= CE_REMOVE
;
492 * Then we need to make sure that we do not lose a locally
493 * present file that is not ignored.
495 pathbuf
= xmalloc(namelen
+ 2);
496 memcpy(pathbuf
, ce
->name
, namelen
);
497 strcpy(pathbuf
+namelen
, "/");
499 memset(&d
, 0, sizeof(d
));
501 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
502 i
= read_directory(&d
, ce
->name
, pathbuf
, namelen
+1, NULL
);
504 return error("Updating '%s' would lose untracked files in it",
511 * We do not want to remove or overwrite a working tree file that
512 * is not tracked, unless it is ignored.
514 static int verify_absent(struct cache_entry
*ce
, const char *action
,
515 struct unpack_trees_options
*o
)
519 if (o
->index_only
|| o
->reset
|| !o
->update
)
522 if (has_symlink_leading_path(ce
->name
, NULL
))
525 if (!lstat(ce
->name
, &st
)) {
528 if (o
->dir
&& excluded(o
->dir
, ce
->name
))
530 * ce->name is explicitly excluded, so it is Ok to
534 if (S_ISDIR(st
.st_mode
)) {
536 * We are checking out path "foo" and
537 * found "foo/." in the working tree.
538 * This is tricky -- if we have modified
539 * files that are in "foo/" we would lose
542 cnt
= verify_clean_subdirectory(ce
, action
, o
);
545 * If this removed entries from the index,
546 * what that means is:
548 * (1) the caller unpack_trees_rec() saw path/foo
549 * in the index, and it has not removed it because
550 * it thinks it is handling 'path' as blob with
552 * (2) we will return "ok, we placed a merged entry
553 * in the index" which would cause o->pos to be
554 * incremented by one;
555 * (3) however, original o->pos now has 'path/foo'
556 * marked with "to be removed".
558 * We need to increment it by the number of
559 * deleted entries here.
566 * The previous round may already have decided to
567 * delete this path, which is in a subdirectory that
568 * is being replaced with a blob.
570 cnt
= cache_name_pos(ce
->name
, strlen(ce
->name
));
572 struct cache_entry
*ce
= active_cache
[cnt
];
573 if (ce
->ce_flags
& CE_REMOVE
)
577 return error("Untracked working tree file '%s' "
578 "would be %s by merge.", ce
->name
, action
);
583 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
584 struct unpack_trees_options
*o
)
586 merge
->ce_flags
|= CE_UPDATE
;
589 * See if we can re-use the old CE directly?
590 * That way we get the uptodate stat info.
592 * This also removes the UPDATE flag on
595 if (same(old
, merge
)) {
596 memcpy(merge
, old
, offsetof(struct cache_entry
, name
));
598 if (verify_uptodate(old
, o
))
600 invalidate_ce_path(old
);
604 if (verify_absent(merge
, "overwritten", o
))
606 invalidate_ce_path(merge
);
609 merge
->ce_flags
&= ~CE_STAGEMASK
;
610 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
614 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
615 struct unpack_trees_options
*o
)
618 if (verify_uptodate(old
, o
))
621 if (verify_absent(ce
, "removed", o
))
623 ce
->ce_flags
|= CE_REMOVE
;
624 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
625 invalidate_ce_path(ce
);
629 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
631 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
636 static void show_stage_entry(FILE *o
,
637 const char *label
, const struct cache_entry
*ce
)
640 fprintf(o
, "%s (missing)\n", label
);
642 fprintf(o
, "%s%06o %s %d\t%s\n",
645 sha1_to_hex(ce
->sha1
),
651 int threeway_merge(struct cache_entry
**stages
,
652 struct unpack_trees_options
*o
,
655 struct cache_entry
*index
;
656 struct cache_entry
*head
;
657 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
660 int remote_match
= 0;
662 int df_conflict_head
= 0;
663 int df_conflict_remote
= 0;
665 int any_anc_missing
= 0;
666 int no_anc_exists
= 1;
669 for (i
= 1; i
< o
->head_idx
; i
++) {
670 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
677 head
= stages
[o
->head_idx
];
679 if (head
== o
->df_conflict_entry
) {
680 df_conflict_head
= 1;
684 if (remote
== o
->df_conflict_entry
) {
685 df_conflict_remote
= 1;
689 /* First, if there's a #16 situation, note that to prevent #13
692 if (!same(remote
, head
)) {
693 for (i
= 1; i
< o
->head_idx
; i
++) {
694 if (same(stages
[i
], head
)) {
697 if (same(stages
[i
], remote
)) {
703 /* We start with cases where the index is allowed to match
704 * something other than the head: #14(ALT) and #2ALT, where it
705 * is permitted to match the result instead.
707 /* #14, #14ALT, #2ALT */
708 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
709 if (index
&& !same(index
, remote
) && !same(index
, head
))
710 return reject_merge(index
);
711 return merged_entry(remote
, index
, o
);
714 * If we have an entry in the index cache, then we want to
715 * make sure that it matches head.
717 if (index
&& !same(index
, head
)) {
718 return reject_merge(index
);
723 if (same(head
, remote
))
724 return merged_entry(head
, index
, o
);
726 if (!df_conflict_remote
&& remote_match
&& !head_match
)
727 return merged_entry(head
, index
, o
);
731 if (!head
&& !remote
&& any_anc_missing
) {
732 remove_entry(remove
);
736 /* Under the new "aggressive" rule, we resolve mostly trivial
737 * cases that we historically had git-merge-one-file resolve.
740 int head_deleted
= !head
&& !df_conflict_head
;
741 int remote_deleted
= !remote
&& !df_conflict_remote
;
742 struct cache_entry
*ce
= NULL
;
751 for (i
= 1; i
< o
->head_idx
; i
++) {
752 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
761 * Deleted in one and unchanged in the other.
763 if ((head_deleted
&& remote_deleted
) ||
764 (head_deleted
&& remote
&& remote_match
) ||
765 (remote_deleted
&& head
&& head_match
)) {
766 remove_entry(remove
);
768 return deleted_entry(index
, index
, o
);
769 else if (ce
&& !head_deleted
) {
770 if (verify_absent(ce
, "removed", o
))
776 * Added in both, identically.
778 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
779 return merged_entry(head
, index
, o
);
783 /* Below are "no merge" cases, which require that the index be
784 * up-to-date to avoid the files getting overwritten with
785 * conflict resolution files.
788 if (verify_uptodate(index
, o
))
792 remove_entry(remove
);
793 o
->nontrivial_merge
= 1;
795 /* #2, #3, #4, #6, #7, #9, #10, #11. */
797 if (!head_match
|| !remote_match
) {
798 for (i
= 1; i
< o
->head_idx
; i
++) {
799 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
800 keep_entry(stages
[i
], o
);
808 fprintf(stderr
, "read-tree: warning #16 detected\n");
809 show_stage_entry(stderr
, "head ", stages
[head_match
]);
810 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
813 if (head
) { count
+= keep_entry(head
, o
); }
814 if (remote
) { count
+= keep_entry(remote
, o
); }
821 * The rule is to "carry forward" what is in the index without losing
822 * information across a "fast forward", favoring a successful merge
823 * over a merge failure when it makes sense. For details of the
824 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
827 int twoway_merge(struct cache_entry
**src
,
828 struct unpack_trees_options
*o
,
831 struct cache_entry
*current
= src
[0];
832 struct cache_entry
*oldtree
= src
[1];
833 struct cache_entry
*newtree
= src
[2];
835 if (o
->merge_size
!= 2)
836 return error("Cannot do a twoway merge of %d trees",
839 if (oldtree
== o
->df_conflict_entry
)
841 if (newtree
== o
->df_conflict_entry
)
845 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
846 (!oldtree
&& newtree
&&
847 same(current
, newtree
)) || /* 6 and 7 */
848 (oldtree
&& newtree
&&
849 same(oldtree
, newtree
)) || /* 14 and 15 */
850 (oldtree
&& newtree
&&
851 !same(oldtree
, newtree
) && /* 18 and 19 */
852 same(current
, newtree
))) {
853 return keep_entry(current
, o
);
855 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
857 remove_entry(remove
);
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 */
867 remove_entry(remove
);
869 return reject_merge(oldtree
);
871 return reject_merge(current
);
873 return reject_merge(newtree
);
878 return merged_entry(newtree
, current
, o
);
879 remove_entry(remove
);
880 return deleted_entry(oldtree
, current
, o
);
886 * Keep the index entries at stage0, collapse stage1 but make sure
887 * stage0 does not have anything there.
889 int bind_merge(struct cache_entry
**src
,
890 struct unpack_trees_options
*o
,
893 struct cache_entry
*old
= src
[0];
894 struct cache_entry
*a
= src
[1];
896 if (o
->merge_size
!= 1)
897 return error("Cannot do a bind merge of %d trees\n",
900 return error("Entry '%s' overlaps. Cannot bind.", a
->name
);
902 return keep_entry(old
, o
);
904 return merged_entry(a
, NULL
, o
);
911 * - take the stat information from stage0, take the data from stage1
913 int oneway_merge(struct cache_entry
**src
,
914 struct unpack_trees_options
*o
,
917 struct cache_entry
*old
= src
[0];
918 struct cache_entry
*a
= src
[1];
920 if (o
->merge_size
!= 1)
921 return error("Cannot do a oneway merge of %d trees",
925 remove_entry(remove
);
926 return deleted_entry(old
, old
, o
);
928 if (old
&& same(old
, a
)) {
931 if (lstat(old
->name
, &st
) ||
932 ce_match_stat(old
, &st
, CE_MATCH_IGNORE_VALID
))
933 old
->ce_flags
|= CE_UPDATE
;
935 return keep_entry(old
, o
);
937 return merged_entry(a
, old
, o
);