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 int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
62 const char *base
, struct unpack_trees_options
*o
,
63 struct tree_entry_list
*df_conflict_list
)
65 int baselen
= strlen(base
);
66 int src_size
= len
+ 1;
71 i_stk
= push_exclude_per_directory(o
->dir
, base
, strlen(base
));
79 struct tree_entry_list
**subposns
;
80 struct cache_entry
**src
;
86 /* Find the first name in the input. */
92 if (o
->merge
&& o
->pos
< active_nr
) {
93 /* This is a bit tricky: */
94 /* If the index has a subdirectory (with
95 * contents) as the first name, it'll get a
96 * filename like "foo/bar". But that's after
97 * "foo", so the entry in trees will get
98 * handled first, at which point we'll go into
99 * "foo", and deal with "bar" from the index,
100 * because the base will be "foo/". The only
101 * way we can actually have "foo/bar" first of
102 * all the things is if the trees don't
103 * contain "foo" at all, in which case we'll
104 * handle "foo/bar" without going into the
105 * directory, but that's fine (and will return
106 * an error anyway, with the added unknown
110 cache_name
= active_cache
[o
->pos
]->name
;
111 if (strlen(cache_name
) > baselen
&&
112 !memcmp(cache_name
, base
, baselen
)) {
113 cache_name
+= baselen
;
122 printf("index %s\n", first
);
124 for (i
= 0; i
< len
; i
++) {
125 if (!posns
[i
] || posns
[i
] == df_conflict_list
)
128 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
130 if (!first
|| entcmp(first
, firstdir
,
132 S_ISDIR(posns
[i
]->mode
)) > 0) {
133 first
= posns
[i
]->name
;
134 firstdir
= S_ISDIR(posns
[i
]->mode
);
137 /* No name means we're done */
139 goto leave_directory
;
141 pathlen
= strlen(first
);
142 ce_size
= cache_entry_size(baselen
+ pathlen
);
144 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
146 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
148 if (cache_name
&& !strcmp(cache_name
, first
)) {
150 src
[0] = active_cache
[o
->pos
];
151 remove_cache_entry_at(o
->pos
);
154 for (i
= 0; i
< len
; i
++) {
155 struct cache_entry
*ce
;
158 (posns
[i
] != df_conflict_list
&&
159 strcmp(first
, posns
[i
]->name
))) {
163 if (posns
[i
] == df_conflict_list
) {
164 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
168 if (S_ISDIR(posns
[i
]->mode
)) {
169 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
173 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
174 subposns
[i
] = create_tree_entry_list(&t
);
175 posns
[i
] = posns
[i
]->next
;
176 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
182 else if (i
+ 1 < o
->head_idx
)
184 else if (i
+ 1 > o
->head_idx
)
189 ce
= xcalloc(1, ce_size
);
190 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
191 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
193 memcpy(ce
->name
, base
, baselen
);
194 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
198 hashcpy(ce
->sha1
, posns
[i
]->sha1
);
199 src
[i
+ o
->merge
] = ce
;
200 subposns
[i
] = df_conflict_list
;
201 posns
[i
] = posns
[i
]->next
;
208 printf("%s:\n", first
);
209 for (i
= 0; i
< src_size
; i
++) {
212 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
220 printf("Added %d entries\n", ret
);
224 for (i
= 0; i
< src_size
; i
++) {
226 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
232 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
233 memcpy(newbase
, base
, baselen
);
234 memcpy(newbase
+ baselen
, first
, pathlen
);
235 newbase
[baselen
+ pathlen
] = '/';
236 newbase
[baselen
+ pathlen
+ 1] = '\0';
237 if (unpack_trees_rec(subposns
, len
, newbase
, o
,
240 goto leave_directory
;
250 pop_exclude_per_directory(o
->dir
, i_stk
);
254 /* Unlink the last component and attempt to remove leading
255 * directories, in case this unlink is the removal of the
256 * last entry in the directory -- empty directories are removed.
258 static void unlink_entry(char *name
, char *last_symlink
)
262 if (has_symlink_leading_path(name
, last_symlink
))
269 cp
= strrchr(name
, '/');
276 status
= rmdir(name
);
285 static struct checkout state
;
286 static void check_updates(struct cache_entry
**src
, int nr
,
287 struct unpack_trees_options
*o
)
289 unsigned short mask
= htons(CE_UPDATE
);
290 unsigned cnt
= 0, total
= 0;
291 struct progress progress
;
292 char last_symlink
[PATH_MAX
];
294 if (o
->update
&& o
->verbose_update
) {
295 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
296 struct cache_entry
*ce
= src
[cnt
];
297 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
301 start_progress_delay(&progress
, "Checking %u files out...",
306 *last_symlink
= '\0';
308 struct cache_entry
*ce
= *src
++;
311 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
312 display_progress(&progress
, ++cnt
);
315 unlink_entry(ce
->name
, last_symlink
);
318 if (ce
->ce_flags
& mask
) {
319 ce
->ce_flags
&= ~mask
;
321 checkout_entry(ce
, &state
, NULL
);
322 *last_symlink
= '\0';
327 stop_progress(&progress
);;
330 int unpack_trees(unsigned len
, struct tree_desc
*t
, struct unpack_trees_options
*o
)
332 struct tree_entry_list
**posns
;
334 struct tree_entry_list df_conflict_list
;
335 static struct cache_entry
*dfc
;
337 memset(&df_conflict_list
, 0, sizeof(df_conflict_list
));
338 df_conflict_list
.next
= &df_conflict_list
;
339 memset(&state
, 0, sizeof(state
));
343 state
.refresh_cache
= 1;
348 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
349 o
->df_conflict_entry
= dfc
;
352 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
353 for (i
= 0; i
< len
; i
++)
354 posns
[i
] = create_tree_entry_list(t
+i
);
356 if (unpack_trees_rec(posns
, len
, o
->prefix
? o
->prefix
: "",
357 o
, &df_conflict_list
))
361 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
362 die("Merge requires file-level merging");
364 check_updates(active_cache
, active_nr
, o
);
368 /* Here come the merge functions */
370 static void reject_merge(struct cache_entry
*ce
)
372 die("Entry '%s' would be overwritten by merge. Cannot merge.",
376 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
382 return a
->ce_mode
== b
->ce_mode
&&
383 !hashcmp(a
->sha1
, b
->sha1
);
388 * When a CE gets turned into an unmerged entry, we
389 * want it to be up-to-date
391 static void verify_uptodate(struct cache_entry
*ce
,
392 struct unpack_trees_options
*o
)
396 if (o
->index_only
|| o
->reset
)
399 if (!lstat(ce
->name
, &st
)) {
400 unsigned changed
= ce_match_stat(ce
, &st
, 1);
404 * NEEDSWORK: the current default policy is to allow
405 * submodule to be out of sync wrt the supermodule
406 * index. This needs to be tightened later for
407 * submodules that are marked to be automatically
410 if (S_ISGITLINK(ntohl(ce
->ce_mode
)))
416 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
419 static void invalidate_ce_path(struct cache_entry
*ce
)
422 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
426 * Check that checking out ce->sha1 in subdir ce->name is not
427 * going to overwrite any working files.
429 * Currently, git does not checkout subprojects during a superproject
430 * checkout, so it is not going to overwrite anything.
432 static int verify_clean_submodule(struct cache_entry
*ce
, const char *action
,
433 struct unpack_trees_options
*o
)
438 static int verify_clean_subdirectory(struct cache_entry
*ce
, const char *action
,
439 struct unpack_trees_options
*o
)
442 * we are about to extract "ce->name"; we would not want to lose
443 * anything in the existing directory there.
450 unsigned char sha1
[20];
452 if (S_ISGITLINK(ntohl(ce
->ce_mode
)) &&
453 resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) == 0) {
454 /* If we are not going to update the submodule, then
457 if (!hashcmp(sha1
, ce
->sha1
))
459 return verify_clean_submodule(ce
, action
, o
);
463 * First let's make sure we do not have a local modification
466 namelen
= strlen(ce
->name
);
467 pos
= cache_name_pos(ce
->name
, namelen
);
469 return cnt
; /* we have it as nondirectory */
471 for (i
= pos
; i
< active_nr
; i
++) {
472 struct cache_entry
*ce
= active_cache
[i
];
473 int len
= ce_namelen(ce
);
475 strncmp(ce
->name
, ce
->name
, namelen
) ||
476 ce
->name
[namelen
] != '/')
479 * ce->name is an entry in the subdirectory.
482 verify_uptodate(ce
, o
);
489 * Then we need to make sure that we do not lose a locally
490 * present file that is not ignored.
492 pathbuf
= xmalloc(namelen
+ 2);
493 memcpy(pathbuf
, ce
->name
, namelen
);
494 strcpy(pathbuf
+namelen
, "/");
496 memset(&d
, 0, sizeof(d
));
498 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
499 i
= read_directory(&d
, ce
->name
, pathbuf
, namelen
+1, NULL
);
501 die("Updating '%s' would lose untracked files in it",
508 * We do not want to remove or overwrite a working tree file that
509 * is not tracked, unless it is ignored.
511 static void verify_absent(struct cache_entry
*ce
, const char *action
,
512 struct unpack_trees_options
*o
)
516 if (o
->index_only
|| o
->reset
|| !o
->update
)
519 if (has_symlink_leading_path(ce
->name
, NULL
))
522 if (!lstat(ce
->name
, &st
)) {
525 if (o
->dir
&& excluded(o
->dir
, ce
->name
))
527 * ce->name is explicitly excluded, so it is Ok to
531 if (S_ISDIR(st
.st_mode
)) {
533 * We are checking out path "foo" and
534 * found "foo/." in the working tree.
535 * This is tricky -- if we have modified
536 * files that are in "foo/" we would lose
539 cnt
= verify_clean_subdirectory(ce
, action
, o
);
542 * If this removed entries from the index,
543 * what that means is:
545 * (1) the caller unpack_trees_rec() saw path/foo
546 * in the index, and it has not removed it because
547 * it thinks it is handling 'path' as blob with
549 * (2) we will return "ok, we placed a merged entry
550 * in the index" which would cause o->pos to be
551 * incremented by one;
552 * (3) however, original o->pos now has 'path/foo'
553 * marked with "to be removed".
555 * We need to increment it by the number of
556 * deleted entries here.
563 * The previous round may already have decided to
564 * delete this path, which is in a subdirectory that
565 * is being replaced with a blob.
567 cnt
= cache_name_pos(ce
->name
, strlen(ce
->name
));
569 struct cache_entry
*ce
= active_cache
[cnt
];
570 if (!ce_stage(ce
) && !ce
->ce_mode
)
574 die("Untracked working tree file '%s' "
575 "would be %s by merge.", ce
->name
, action
);
579 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
580 struct unpack_trees_options
*o
)
582 merge
->ce_flags
|= htons(CE_UPDATE
);
585 * See if we can re-use the old CE directly?
586 * That way we get the uptodate stat info.
588 * This also removes the UPDATE flag on
591 if (same(old
, merge
)) {
594 verify_uptodate(old
, o
);
595 invalidate_ce_path(old
);
599 verify_absent(merge
, "overwritten", o
);
600 invalidate_ce_path(merge
);
603 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
604 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
608 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
609 struct unpack_trees_options
*o
)
612 verify_uptodate(old
, o
);
614 verify_absent(ce
, "removed", o
);
616 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
617 invalidate_ce_path(ce
);
621 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
623 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
628 static void show_stage_entry(FILE *o
,
629 const char *label
, const struct cache_entry
*ce
)
632 fprintf(o
, "%s (missing)\n", label
);
634 fprintf(o
, "%s%06o %s %d\t%s\n",
637 sha1_to_hex(ce
->sha1
),
643 int threeway_merge(struct cache_entry
**stages
,
644 struct unpack_trees_options
*o
)
646 struct cache_entry
*index
;
647 struct cache_entry
*head
;
648 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
651 int remote_match
= 0;
653 int df_conflict_head
= 0;
654 int df_conflict_remote
= 0;
656 int any_anc_missing
= 0;
657 int no_anc_exists
= 1;
660 for (i
= 1; i
< o
->head_idx
; i
++) {
661 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
668 head
= stages
[o
->head_idx
];
670 if (head
== o
->df_conflict_entry
) {
671 df_conflict_head
= 1;
675 if (remote
== o
->df_conflict_entry
) {
676 df_conflict_remote
= 1;
680 /* First, if there's a #16 situation, note that to prevent #13
683 if (!same(remote
, head
)) {
684 for (i
= 1; i
< o
->head_idx
; i
++) {
685 if (same(stages
[i
], head
)) {
688 if (same(stages
[i
], remote
)) {
694 /* We start with cases where the index is allowed to match
695 * something other than the head: #14(ALT) and #2ALT, where it
696 * is permitted to match the result instead.
698 /* #14, #14ALT, #2ALT */
699 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
700 if (index
&& !same(index
, remote
) && !same(index
, head
))
702 return merged_entry(remote
, index
, o
);
705 * If we have an entry in the index cache, then we want to
706 * make sure that it matches head.
708 if (index
&& !same(index
, head
)) {
714 if (same(head
, remote
))
715 return merged_entry(head
, index
, o
);
717 if (!df_conflict_remote
&& remote_match
&& !head_match
)
718 return merged_entry(head
, index
, o
);
722 if (!head
&& !remote
&& any_anc_missing
)
725 /* Under the new "aggressive" rule, we resolve mostly trivial
726 * cases that we historically had git-merge-one-file resolve.
729 int head_deleted
= !head
&& !df_conflict_head
;
730 int remote_deleted
= !remote
&& !df_conflict_remote
;
731 struct cache_entry
*ce
= NULL
;
740 for (i
= 1; i
< o
->head_idx
; i
++) {
741 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
750 * Deleted in one and unchanged in the other.
752 if ((head_deleted
&& remote_deleted
) ||
753 (head_deleted
&& remote
&& remote_match
) ||
754 (remote_deleted
&& head
&& head_match
)) {
756 return deleted_entry(index
, index
, o
);
757 else if (ce
&& !head_deleted
)
758 verify_absent(ce
, "removed", o
);
762 * Added in both, identically.
764 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
765 return merged_entry(head
, index
, o
);
769 /* Below are "no merge" cases, which require that the index be
770 * up-to-date to avoid the files getting overwritten with
771 * conflict resolution files.
774 verify_uptodate(index
, o
);
777 o
->nontrivial_merge
= 1;
779 /* #2, #3, #4, #6, #7, #9, #10, #11. */
781 if (!head_match
|| !remote_match
) {
782 for (i
= 1; i
< o
->head_idx
; i
++) {
783 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
784 keep_entry(stages
[i
], o
);
792 fprintf(stderr
, "read-tree: warning #16 detected\n");
793 show_stage_entry(stderr
, "head ", stages
[head_match
]);
794 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
797 if (head
) { count
+= keep_entry(head
, o
); }
798 if (remote
) { count
+= keep_entry(remote
, o
); }
805 * The rule is to "carry forward" what is in the index without losing
806 * information across a "fast forward", favoring a successful merge
807 * over a merge failure when it makes sense. For details of the
808 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
811 int twoway_merge(struct cache_entry
**src
,
812 struct unpack_trees_options
*o
)
814 struct cache_entry
*current
= src
[0];
815 struct cache_entry
*oldtree
= src
[1];
816 struct cache_entry
*newtree
= src
[2];
818 if (o
->merge_size
!= 2)
819 return error("Cannot do a twoway merge of %d trees",
822 if (oldtree
== o
->df_conflict_entry
)
824 if (newtree
== o
->df_conflict_entry
)
828 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
829 (!oldtree
&& newtree
&&
830 same(current
, newtree
)) || /* 6 and 7 */
831 (oldtree
&& newtree
&&
832 same(oldtree
, newtree
)) || /* 14 and 15 */
833 (oldtree
&& newtree
&&
834 !same(oldtree
, newtree
) && /* 18 and 19 */
835 same(current
, newtree
))) {
836 return keep_entry(current
, o
);
838 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
840 return deleted_entry(oldtree
, current
, o
);
842 else if (oldtree
&& newtree
&&
843 same(current
, oldtree
) && !same(current
, newtree
)) {
845 return merged_entry(newtree
, current
, o
);
848 /* all other failures */
850 reject_merge(oldtree
);
852 reject_merge(current
);
854 reject_merge(newtree
);
859 return merged_entry(newtree
, current
, o
);
861 return deleted_entry(oldtree
, current
, o
);
867 * Keep the index entries at stage0, collapse stage1 but make sure
868 * stage0 does not have anything there.
870 int bind_merge(struct cache_entry
**src
,
871 struct unpack_trees_options
*o
)
873 struct cache_entry
*old
= src
[0];
874 struct cache_entry
*a
= src
[1];
876 if (o
->merge_size
!= 1)
877 return error("Cannot do a bind merge of %d trees\n",
880 die("Entry '%s' overlaps. Cannot bind.", a
->name
);
882 return keep_entry(old
, o
);
884 return merged_entry(a
, NULL
, o
);
891 * - take the stat information from stage0, take the data from stage1
893 int oneway_merge(struct cache_entry
**src
,
894 struct unpack_trees_options
*o
)
896 struct cache_entry
*old
= src
[0];
897 struct cache_entry
*a
= src
[1];
899 if (o
->merge_size
!= 1)
900 return error("Cannot do a oneway merge of %d trees",
904 return deleted_entry(old
, old
, o
);
905 if (old
&& same(old
, a
)) {
908 if (lstat(old
->name
, &st
) ||
909 ce_match_stat(old
, &st
, 1))
910 old
->ce_flags
|= htons(CE_UPDATE
);
912 return keep_entry(old
, o
);
914 return merged_entry(a
, old
, o
);