5 #include "cache-tree.h"
6 #include "unpack-trees.h"
10 struct tree_entry_list
{
11 struct tree_entry_list
*next
;
12 unsigned directory
: 1;
13 unsigned executable
: 1;
17 const unsigned char *sha1
;
20 static struct tree_entry_list
*create_tree_entry_list(struct tree
*tree
)
22 struct tree_desc desc
;
23 struct name_entry one
;
24 struct tree_entry_list
*ret
= NULL
;
25 struct tree_entry_list
**list_p
= &ret
;
27 if (!tree
->object
.parsed
)
30 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
32 while (tree_entry(&desc
, &one
)) {
33 struct tree_entry_list
*entry
;
35 entry
= xmalloc(sizeof(struct tree_entry_list
));
36 entry
->name
= one
.path
;
37 entry
->sha1
= one
.sha1
;
38 entry
->mode
= one
.mode
;
39 entry
->directory
= S_ISDIR(one
.mode
) != 0;
40 entry
->executable
= (one
.mode
& S_IXUSR
) != 0;
41 entry
->symlink
= S_ISLNK(one
.mode
) != 0;
45 list_p
= &entry
->next
;
50 static int entcmp(const char *name1
, int dir1
, const char *name2
, int dir2
)
52 int len1
= strlen(name1
);
53 int len2
= strlen(name2
);
54 int len
= len1
< len2
? len1
: len2
;
55 int ret
= memcmp(name1
, name2
, len
);
65 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
71 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
72 const char *base
, struct unpack_trees_options
*o
,
73 struct tree_entry_list
*df_conflict_list
)
75 int baselen
= strlen(base
);
76 int src_size
= len
+ 1;
81 i_stk
= push_exclude_per_directory(o
->dir
, base
, strlen(base
));
89 struct tree_entry_list
**subposns
;
90 struct cache_entry
**src
;
96 /* Find the first name in the input. */
101 /* Check the cache */
102 if (o
->merge
&& o
->pos
< active_nr
) {
103 /* This is a bit tricky: */
104 /* If the index has a subdirectory (with
105 * contents) as the first name, it'll get a
106 * filename like "foo/bar". But that's after
107 * "foo", so the entry in trees will get
108 * handled first, at which point we'll go into
109 * "foo", and deal with "bar" from the index,
110 * because the base will be "foo/". The only
111 * way we can actually have "foo/bar" first of
112 * all the things is if the trees don't
113 * contain "foo" at all, in which case we'll
114 * handle "foo/bar" without going into the
115 * directory, but that's fine (and will return
116 * an error anyway, with the added unknown
120 cache_name
= active_cache
[o
->pos
]->name
;
121 if (strlen(cache_name
) > baselen
&&
122 !memcmp(cache_name
, base
, baselen
)) {
123 cache_name
+= baselen
;
132 printf("index %s\n", first
);
134 for (i
= 0; i
< len
; i
++) {
135 if (!posns
[i
] || posns
[i
] == df_conflict_list
)
138 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
140 if (!first
|| entcmp(first
, firstdir
,
142 posns
[i
]->directory
) > 0) {
143 first
= posns
[i
]->name
;
144 firstdir
= posns
[i
]->directory
;
147 /* No name means we're done */
149 goto leave_directory
;
151 pathlen
= strlen(first
);
152 ce_size
= cache_entry_size(baselen
+ pathlen
);
154 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
156 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
158 if (cache_name
&& !strcmp(cache_name
, first
)) {
160 src
[0] = active_cache
[o
->pos
];
161 remove_cache_entry_at(o
->pos
);
164 for (i
= 0; i
< len
; i
++) {
165 struct cache_entry
*ce
;
168 (posns
[i
] != df_conflict_list
&&
169 strcmp(first
, posns
[i
]->name
))) {
173 if (posns
[i
] == df_conflict_list
) {
174 src
[i
+ o
->merge
] = o
->df_conflict_entry
;
178 if (posns
[i
]->directory
) {
179 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
182 subposns
[i
] = create_tree_entry_list(tree
);
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
));
228 printf("Added %d entries\n", ret
);
232 for (i
= 0; i
< src_size
; i
++) {
234 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
240 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
241 memcpy(newbase
, base
, baselen
);
242 memcpy(newbase
+ baselen
, first
, pathlen
);
243 newbase
[baselen
+ pathlen
] = '/';
244 newbase
[baselen
+ pathlen
+ 1] = '\0';
245 if (unpack_trees_rec(subposns
, len
, newbase
, o
,
248 goto leave_directory
;
258 pop_exclude_per_directory(o
->dir
, i_stk
);
262 /* Unlink the last component and attempt to remove leading
263 * directories, in case this unlink is the removal of the
264 * last entry in the directory -- empty directories are removed.
266 static void unlink_entry(char *name
)
275 cp
= strrchr(name
, '/');
282 status
= rmdir(name
);
291 static volatile sig_atomic_t progress_update
;
293 static void progress_interval(int signum
)
298 static void setup_progress_signal(void)
303 memset(&sa
, 0, sizeof(sa
));
304 sa
.sa_handler
= progress_interval
;
305 sigemptyset(&sa
.sa_mask
);
306 sa
.sa_flags
= SA_RESTART
;
307 sigaction(SIGALRM
, &sa
, NULL
);
309 v
.it_interval
.tv_sec
= 1;
310 v
.it_interval
.tv_usec
= 0;
311 v
.it_value
= v
.it_interval
;
312 setitimer(ITIMER_REAL
, &v
, NULL
);
315 static struct checkout state
;
316 static void check_updates(struct cache_entry
**src
, int nr
,
317 struct unpack_trees_options
*o
)
319 unsigned short mask
= htons(CE_UPDATE
);
320 unsigned last_percent
= 200, cnt
= 0, total
= 0;
322 if (o
->update
&& o
->verbose_update
) {
323 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
324 struct cache_entry
*ce
= src
[cnt
];
325 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
329 /* Don't bother doing this for very small updates */
334 fprintf(stderr
, "Checking files out...\n");
335 setup_progress_signal();
342 struct cache_entry
*ce
= *src
++;
345 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
) {
348 percent
= (cnt
* 100) / total
;
349 if (percent
!= last_percent
||
351 fprintf(stderr
, "%4u%% (%u/%u) done\r",
352 percent
, cnt
, total
);
353 last_percent
= percent
;
360 unlink_entry(ce
->name
);
363 if (ce
->ce_flags
& mask
) {
364 ce
->ce_flags
&= ~mask
;
366 checkout_entry(ce
, &state
, NULL
);
370 signal(SIGALRM
, SIG_IGN
);
375 int unpack_trees(struct object_list
*trees
, struct unpack_trees_options
*o
)
377 unsigned len
= object_list_length(trees
);
378 struct tree_entry_list
**posns
;
380 struct object_list
*posn
= trees
;
381 struct tree_entry_list df_conflict_list
;
382 static struct cache_entry
*dfc
;
384 memset(&df_conflict_list
, 0, sizeof(df_conflict_list
));
385 df_conflict_list
.next
= &df_conflict_list
;
386 memset(&state
, 0, sizeof(state
));
390 state
.refresh_cache
= 1;
395 dfc
= xcalloc(1, sizeof(struct cache_entry
) + 1);
396 o
->df_conflict_entry
= dfc
;
399 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
400 for (i
= 0; i
< len
; i
++) {
401 posns
[i
] = create_tree_entry_list((struct tree
*) posn
->item
);
404 if (unpack_trees_rec(posns
, len
, o
->prefix
? o
->prefix
: "",
405 o
, &df_conflict_list
))
409 if (o
->trivial_merges_only
&& o
->nontrivial_merge
)
410 die("Merge requires file-level merging");
412 check_updates(active_cache
, active_nr
, o
);
416 /* Here come the merge functions */
418 static void reject_merge(struct cache_entry
*ce
)
420 die("Entry '%s' would be overwritten by merge. Cannot merge.",
424 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
430 return a
->ce_mode
== b
->ce_mode
&&
431 !hashcmp(a
->sha1
, b
->sha1
);
436 * When a CE gets turned into an unmerged entry, we
437 * want it to be up-to-date
439 static void verify_uptodate(struct cache_entry
*ce
,
440 struct unpack_trees_options
*o
)
444 if (o
->index_only
|| o
->reset
)
447 if (!lstat(ce
->name
, &st
)) {
448 unsigned changed
= ce_match_stat(ce
, &st
, 1);
454 ce
->ce_flags
|= htons(CE_UPDATE
);
459 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
462 static void invalidate_ce_path(struct cache_entry
*ce
)
465 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
468 static int verify_clean_subdirectory(const char *path
, const char *action
,
469 struct unpack_trees_options
*o
)
472 * we are about to extract "path"; we would not want to lose
473 * anything in the existing directory there.
482 * First let's make sure we do not have a local modification
485 namelen
= strlen(path
);
486 pos
= cache_name_pos(path
, namelen
);
488 return cnt
; /* we have it as nondirectory */
490 for (i
= pos
; i
< active_nr
; i
++) {
491 struct cache_entry
*ce
= active_cache
[i
];
492 int len
= ce_namelen(ce
);
494 strncmp(path
, ce
->name
, namelen
) ||
495 ce
->name
[namelen
] != '/')
498 * ce->name is an entry in the subdirectory.
501 verify_uptodate(ce
, o
);
508 * Then we need to make sure that we do not lose a locally
509 * present file that is not ignored.
511 pathbuf
= xmalloc(namelen
+ 2);
512 memcpy(pathbuf
, path
, namelen
);
513 strcpy(pathbuf
+namelen
, "/");
515 memset(&d
, 0, sizeof(d
));
517 d
.exclude_per_dir
= o
->dir
->exclude_per_dir
;
518 i
= read_directory(&d
, path
, pathbuf
, namelen
+1, NULL
);
520 die("Updating '%s' would lose untracked files in it",
527 * We do not want to remove or overwrite a working tree file that
528 * is not tracked, unless it is ignored.
530 static void verify_absent(const char *path
, const char *action
,
531 struct unpack_trees_options
*o
)
535 if (o
->index_only
|| o
->reset
|| !o
->update
)
538 if (!lstat(path
, &st
)) {
541 if (o
->dir
&& excluded(o
->dir
, path
))
543 * path is explicitly excluded, so it is Ok to
547 if (S_ISDIR(st
.st_mode
)) {
549 * We are checking out path "foo" and
550 * found "foo/." in the working tree.
551 * This is tricky -- if we have modified
552 * files that are in "foo/" we would lose
555 cnt
= verify_clean_subdirectory(path
, action
, o
);
558 * If this removed entries from the index,
559 * what that means is:
561 * (1) the caller unpack_trees_rec() saw path/foo
562 * in the index, and it has not removed it because
563 * it thinks it is handling 'path' as blob with
565 * (2) we will return "ok, we placed a merged entry
566 * in the index" which would cause o->pos to be
567 * incremented by one;
568 * (3) however, original o->pos now has 'path/foo'
569 * marked with "to be removed".
571 * We need to increment it by the number of
572 * deleted entries here.
579 * The previous round may already have decided to
580 * delete this path, which is in a subdirectory that
581 * is being replaced with a blob.
583 cnt
= cache_name_pos(path
, strlen(path
));
585 struct cache_entry
*ce
= active_cache
[cnt
];
586 if (!ce_stage(ce
) && !ce
->ce_mode
)
590 die("Untracked working tree file '%s' "
591 "would be %s by merge.", path
, action
);
595 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
,
596 struct unpack_trees_options
*o
)
598 merge
->ce_flags
|= htons(CE_UPDATE
);
601 * See if we can re-use the old CE directly?
602 * That way we get the uptodate stat info.
604 * This also removes the UPDATE flag on
607 if (same(old
, merge
)) {
610 verify_uptodate(old
, o
);
611 invalidate_ce_path(old
);
615 verify_absent(merge
->name
, "overwritten", o
);
616 invalidate_ce_path(merge
);
619 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
620 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
624 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
,
625 struct unpack_trees_options
*o
)
628 verify_uptodate(old
, o
);
630 verify_absent(ce
->name
, "removed", o
);
632 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
633 invalidate_ce_path(ce
);
637 static int keep_entry(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
639 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
644 static void show_stage_entry(FILE *o
,
645 const char *label
, const struct cache_entry
*ce
)
648 fprintf(o
, "%s (missing)\n", label
);
650 fprintf(o
, "%s%06o %s %d\t%s\n",
653 sha1_to_hex(ce
->sha1
),
659 int threeway_merge(struct cache_entry
**stages
,
660 struct unpack_trees_options
*o
)
662 struct cache_entry
*index
;
663 struct cache_entry
*head
;
664 struct cache_entry
*remote
= stages
[o
->head_idx
+ 1];
667 int remote_match
= 0;
668 const char *path
= NULL
;
670 int df_conflict_head
= 0;
671 int df_conflict_remote
= 0;
673 int any_anc_missing
= 0;
674 int no_anc_exists
= 1;
677 for (i
= 1; i
< o
->head_idx
; i
++) {
682 path
= stages
[i
]->name
;
688 head
= stages
[o
->head_idx
];
690 if (head
== o
->df_conflict_entry
) {
691 df_conflict_head
= 1;
695 if (remote
== o
->df_conflict_entry
) {
696 df_conflict_remote
= 1;
707 /* First, if there's a #16 situation, note that to prevent #13
710 if (!same(remote
, head
)) {
711 for (i
= 1; i
< o
->head_idx
; i
++) {
712 if (same(stages
[i
], head
)) {
715 if (same(stages
[i
], remote
)) {
721 /* We start with cases where the index is allowed to match
722 * something other than the head: #14(ALT) and #2ALT, where it
723 * is permitted to match the result instead.
725 /* #14, #14ALT, #2ALT */
726 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
727 if (index
&& !same(index
, remote
) && !same(index
, head
))
729 return merged_entry(remote
, index
, o
);
732 * If we have an entry in the index cache, then we want to
733 * make sure that it matches head.
735 if (index
&& !same(index
, head
)) {
741 if (same(head
, remote
))
742 return merged_entry(head
, index
, o
);
744 if (!df_conflict_remote
&& remote_match
&& !head_match
)
745 return merged_entry(head
, index
, o
);
749 if (!head
&& !remote
&& any_anc_missing
)
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
;
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
)) {
766 return deleted_entry(index
, index
, o
);
767 else if (path
&& !head_deleted
)
768 verify_absent(path
, "removed", o
);
772 * Added in both, identically.
774 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
775 return merged_entry(head
, index
, o
);
779 /* Below are "no merge" cases, which require that the index be
780 * up-to-date to avoid the files getting overwritten with
781 * conflict resolution files.
784 verify_uptodate(index
, o
);
787 o
->nontrivial_merge
= 1;
789 /* #2, #3, #4, #6, #7, #9, #11. */
791 if (!head_match
|| !remote_match
) {
792 for (i
= 1; i
< o
->head_idx
; i
++) {
794 keep_entry(stages
[i
], o
);
802 fprintf(stderr
, "read-tree: warning #16 detected\n");
803 show_stage_entry(stderr
, "head ", stages
[head_match
]);
804 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
807 if (head
) { count
+= keep_entry(head
, o
); }
808 if (remote
) { count
+= keep_entry(remote
, o
); }
815 * The rule is to "carry forward" what is in the index without losing
816 * information across a "fast forward", favoring a successful merge
817 * over a merge failure when it makes sense. For details of the
818 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
821 int twoway_merge(struct cache_entry
**src
,
822 struct unpack_trees_options
*o
)
824 struct cache_entry
*current
= src
[0];
825 struct cache_entry
*oldtree
= src
[1];
826 struct cache_entry
*newtree
= src
[2];
828 if (o
->merge_size
!= 2)
829 return error("Cannot do a twoway merge of %d trees",
832 if (oldtree
== o
->df_conflict_entry
)
834 if (newtree
== o
->df_conflict_entry
)
838 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
839 (!oldtree
&& newtree
&&
840 same(current
, newtree
)) || /* 6 and 7 */
841 (oldtree
&& newtree
&&
842 same(oldtree
, newtree
)) || /* 14 and 15 */
843 (oldtree
&& newtree
&&
844 !same(oldtree
, newtree
) && /* 18 and 19 */
845 same(current
, newtree
))) {
846 return keep_entry(current
, o
);
848 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
850 return deleted_entry(oldtree
, current
, o
);
852 else if (oldtree
&& newtree
&&
853 same(current
, oldtree
) && !same(current
, newtree
)) {
855 return merged_entry(newtree
, current
, o
);
858 /* all other failures */
860 reject_merge(oldtree
);
862 reject_merge(current
);
864 reject_merge(newtree
);
869 return merged_entry(newtree
, current
, o
);
871 return deleted_entry(oldtree
, current
, o
);
877 * Keep the index entries at stage0, collapse stage1 but make sure
878 * stage0 does not have anything there.
880 int bind_merge(struct cache_entry
**src
,
881 struct unpack_trees_options
*o
)
883 struct cache_entry
*old
= src
[0];
884 struct cache_entry
*a
= src
[1];
886 if (o
->merge_size
!= 1)
887 return error("Cannot do a bind merge of %d trees\n",
890 die("Entry '%s' overlaps. Cannot bind.", a
->name
);
892 return keep_entry(old
, o
);
894 return merged_entry(a
, NULL
, o
);
901 * - take the stat information from stage0, take the data from stage1
903 int oneway_merge(struct cache_entry
**src
,
904 struct unpack_trees_options
*o
)
906 struct cache_entry
*old
= src
[0];
907 struct cache_entry
*a
= src
[1];
909 if (o
->merge_size
!= 1)
910 return error("Cannot do a oneway merge of %d trees",
914 return deleted_entry(old
, old
, o
);
915 if (old
&& same(old
, a
)) {
918 if (lstat(old
->name
, &st
) ||
919 ce_match_stat(old
, &st
, 1))
920 old
->ce_flags
|= htons(CE_UPDATE
);
922 return keep_entry(old
, o
);
924 return merged_entry(a
, old
, o
);