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;
669 int df_conflict_head
= 0;
670 int df_conflict_remote
= 0;
672 int any_anc_missing
= 0;
673 int no_anc_exists
= 1;
676 for (i
= 1; i
< o
->head_idx
; i
++) {
677 if (!stages
[i
] || stages
[i
] == o
->df_conflict_entry
)
684 head
= stages
[o
->head_idx
];
686 if (head
== o
->df_conflict_entry
) {
687 df_conflict_head
= 1;
691 if (remote
== o
->df_conflict_entry
) {
692 df_conflict_remote
= 1;
696 /* First, if there's a #16 situation, note that to prevent #13
699 if (!same(remote
, head
)) {
700 for (i
= 1; i
< o
->head_idx
; i
++) {
701 if (same(stages
[i
], head
)) {
704 if (same(stages
[i
], remote
)) {
710 /* We start with cases where the index is allowed to match
711 * something other than the head: #14(ALT) and #2ALT, where it
712 * is permitted to match the result instead.
714 /* #14, #14ALT, #2ALT */
715 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
716 if (index
&& !same(index
, remote
) && !same(index
, head
))
718 return merged_entry(remote
, index
, o
);
721 * If we have an entry in the index cache, then we want to
722 * make sure that it matches head.
724 if (index
&& !same(index
, head
)) {
730 if (same(head
, remote
))
731 return merged_entry(head
, index
, o
);
733 if (!df_conflict_remote
&& remote_match
&& !head_match
)
734 return merged_entry(head
, index
, o
);
738 if (!head
&& !remote
&& any_anc_missing
)
741 /* Under the new "aggressive" rule, we resolve mostly trivial
742 * cases that we historically had git-merge-one-file resolve.
745 int head_deleted
= !head
&& !df_conflict_head
;
746 int remote_deleted
= !remote
&& !df_conflict_remote
;
747 const char *path
= NULL
;
756 for (i
= 1; i
< o
->head_idx
; i
++) {
757 if (stages
[i
] && stages
[i
] != o
->df_conflict_entry
) {
758 path
= stages
[i
]->name
;
766 * Deleted in one and unchanged in the other.
768 if ((head_deleted
&& remote_deleted
) ||
769 (head_deleted
&& remote
&& remote_match
) ||
770 (remote_deleted
&& head
&& head_match
)) {
772 return deleted_entry(index
, index
, o
);
773 else if (path
&& !head_deleted
)
774 verify_absent(path
, "removed", o
);
778 * Added in both, identically.
780 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
781 return merged_entry(head
, index
, o
);
785 /* Below are "no merge" cases, which require that the index be
786 * up-to-date to avoid the files getting overwritten with
787 * conflict resolution files.
790 verify_uptodate(index
, o
);
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
)
830 struct cache_entry
*current
= src
[0];
831 struct cache_entry
*oldtree
= src
[1];
832 struct cache_entry
*newtree
= src
[2];
834 if (o
->merge_size
!= 2)
835 return error("Cannot do a twoway merge of %d trees",
838 if (oldtree
== o
->df_conflict_entry
)
840 if (newtree
== o
->df_conflict_entry
)
844 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
845 (!oldtree
&& newtree
&&
846 same(current
, newtree
)) || /* 6 and 7 */
847 (oldtree
&& newtree
&&
848 same(oldtree
, newtree
)) || /* 14 and 15 */
849 (oldtree
&& newtree
&&
850 !same(oldtree
, newtree
) && /* 18 and 19 */
851 same(current
, newtree
))) {
852 return keep_entry(current
, o
);
854 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
856 return deleted_entry(oldtree
, current
, o
);
858 else if (oldtree
&& newtree
&&
859 same(current
, oldtree
) && !same(current
, newtree
)) {
861 return merged_entry(newtree
, current
, o
);
864 /* all other failures */
866 reject_merge(oldtree
);
868 reject_merge(current
);
870 reject_merge(newtree
);
875 return merged_entry(newtree
, current
, o
);
877 return deleted_entry(oldtree
, current
, o
);
883 * Keep the index entries at stage0, collapse stage1 but make sure
884 * stage0 does not have anything there.
886 int bind_merge(struct cache_entry
**src
,
887 struct unpack_trees_options
*o
)
889 struct cache_entry
*old
= src
[0];
890 struct cache_entry
*a
= src
[1];
892 if (o
->merge_size
!= 1)
893 return error("Cannot do a bind merge of %d trees\n",
896 die("Entry '%s' overlaps. Cannot bind.", a
->name
);
898 return keep_entry(old
, o
);
900 return merged_entry(a
, NULL
, o
);
907 * - take the stat information from stage0, take the data from stage1
909 int oneway_merge(struct cache_entry
**src
,
910 struct unpack_trees_options
*o
)
912 struct cache_entry
*old
= src
[0];
913 struct cache_entry
*a
= src
[1];
915 if (o
->merge_size
!= 1)
916 return error("Cannot do a oneway merge of %d trees",
920 return deleted_entry(old
, old
, o
);
921 if (old
&& same(old
, a
)) {
924 if (lstat(old
->name
, &st
) ||
925 ce_match_stat(old
, &st
, 1))
926 old
->ce_flags
|= htons(CE_UPDATE
);
928 return keep_entry(old
, o
);
930 return merged_entry(a
, old
, o
);