2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 #include "tree-walk.h"
13 #include "cache-tree.h"
20 static int update
= 0;
21 static int index_only
= 0;
22 static int nontrivial_merge
= 0;
23 static int trivial_merges_only
= 0;
24 static int aggressive
= 0;
25 static int verbose_update
= 0;
26 static volatile int progress_update
= 0;
27 static const char *prefix
= NULL
;
29 static int head_idx
= -1;
30 static int merge_size
= 0;
32 static struct object_list
*trees
= NULL
;
34 static struct cache_entry df_conflict_entry
= {
37 struct tree_entry_list
{
38 struct tree_entry_list
*next
;
39 unsigned directory
: 1;
40 unsigned executable
: 1;
44 const unsigned char *sha1
;
47 static struct tree_entry_list df_conflict_list
= {
49 .next
= &df_conflict_list
52 typedef int (*merge_fn_t
)(struct cache_entry
**src
);
54 static struct tree_entry_list
*create_tree_entry_list(struct tree
*tree
)
56 struct tree_desc desc
;
57 struct name_entry one
;
58 struct tree_entry_list
*ret
= NULL
;
59 struct tree_entry_list
**list_p
= &ret
;
61 desc
.buf
= tree
->buffer
;
62 desc
.size
= tree
->size
;
64 while (tree_entry(&desc
, &one
)) {
65 struct tree_entry_list
*entry
;
67 entry
= xmalloc(sizeof(struct tree_entry_list
));
68 entry
->name
= one
.path
;
69 entry
->sha1
= one
.sha1
;
70 entry
->mode
= one
.mode
;
71 entry
->directory
= S_ISDIR(one
.mode
) != 0;
72 entry
->executable
= (one
.mode
& S_IXUSR
) != 0;
73 entry
->symlink
= S_ISLNK(one
.mode
) != 0;
77 list_p
= &entry
->next
;
82 static int entcmp(const char *name1
, int dir1
, const char *name2
, int dir2
)
84 int len1
= strlen(name1
);
85 int len2
= strlen(name2
);
86 int len
= len1
< len2
? len1
: len2
;
87 int ret
= memcmp(name1
, name2
, len
);
97 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
103 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
104 const char *base
, merge_fn_t fn
, int *indpos
)
106 int baselen
= strlen(base
);
107 int src_size
= len
+ 1;
114 struct tree_entry_list
**subposns
;
115 struct cache_entry
**src
;
121 /* Find the first name in the input. */
126 /* Check the cache */
127 if (merge
&& *indpos
< active_nr
) {
128 /* This is a bit tricky: */
129 /* If the index has a subdirectory (with
130 * contents) as the first name, it'll get a
131 * filename like "foo/bar". But that's after
132 * "foo", so the entry in trees will get
133 * handled first, at which point we'll go into
134 * "foo", and deal with "bar" from the index,
135 * because the base will be "foo/". The only
136 * way we can actually have "foo/bar" first of
137 * all the things is if the trees don't
138 * contain "foo" at all, in which case we'll
139 * handle "foo/bar" without going into the
140 * directory, but that's fine (and will return
141 * an error anyway, with the added unknown
145 cache_name
= active_cache
[*indpos
]->name
;
146 if (strlen(cache_name
) > baselen
&&
147 !memcmp(cache_name
, base
, baselen
)) {
148 cache_name
+= baselen
;
157 printf("index %s\n", first
);
159 for (i
= 0; i
< len
; i
++) {
160 if (!posns
[i
] || posns
[i
] == &df_conflict_list
)
163 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
165 if (!first
|| entcmp(first
, firstdir
,
167 posns
[i
]->directory
) > 0) {
168 first
= posns
[i
]->name
;
169 firstdir
= posns
[i
]->directory
;
172 /* No name means we're done */
176 pathlen
= strlen(first
);
177 ce_size
= cache_entry_size(baselen
+ pathlen
);
179 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
181 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
183 if (cache_name
&& !strcmp(cache_name
, first
)) {
185 src
[0] = active_cache
[*indpos
];
186 remove_cache_entry_at(*indpos
);
189 for (i
= 0; i
< len
; i
++) {
190 struct cache_entry
*ce
;
193 (posns
[i
] != &df_conflict_list
&&
194 strcmp(first
, posns
[i
]->name
))) {
198 if (posns
[i
] == &df_conflict_list
) {
199 src
[i
+ merge
] = &df_conflict_entry
;
203 if (posns
[i
]->directory
) {
204 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
207 subposns
[i
] = create_tree_entry_list(tree
);
208 posns
[i
] = posns
[i
]->next
;
209 src
[i
+ merge
] = &df_conflict_entry
;
215 else if (i
+ 1 < head_idx
)
217 else if (i
+ 1 > head_idx
)
222 ce
= xcalloc(1, ce_size
);
223 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
224 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
226 memcpy(ce
->name
, base
, baselen
);
227 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
231 memcpy(ce
->sha1
, posns
[i
]->sha1
, 20);
233 subposns
[i
] = &df_conflict_list
;
234 posns
[i
] = posns
[i
]->next
;
241 printf("%s:\n", first
);
242 for (i
= 0; i
< src_size
; i
++) {
245 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
253 printf("Added %d entries\n", ret
);
257 for (i
= 0; i
< src_size
; i
++) {
259 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
265 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
266 memcpy(newbase
, base
, baselen
);
267 memcpy(newbase
+ baselen
, first
, pathlen
);
268 newbase
[baselen
+ pathlen
] = '/';
269 newbase
[baselen
+ pathlen
+ 1] = '\0';
270 if (unpack_trees_rec(subposns
, len
, newbase
, fn
,
280 static void reject_merge(struct cache_entry
*ce
)
282 die("Entry '%s' would be overwritten by merge. Cannot merge.",
286 /* Unlink the last component and attempt to remove leading
287 * directories, in case this unlink is the removal of the
288 * last entry in the directory -- empty directories are removed.
290 static void unlink_entry(char *name
)
299 cp
= strrchr(name
, '/');
306 status
= rmdir(name
);
315 static void progress_interval(int signum
)
320 static void setup_progress_signal(void)
325 memset(&sa
, 0, sizeof(sa
));
326 sa
.sa_handler
= progress_interval
;
327 sigemptyset(&sa
.sa_mask
);
328 sa
.sa_flags
= SA_RESTART
;
329 sigaction(SIGALRM
, &sa
, NULL
);
331 v
.it_interval
.tv_sec
= 1;
332 v
.it_interval
.tv_usec
= 0;
333 v
.it_value
= v
.it_interval
;
334 setitimer(ITIMER_REAL
, &v
, NULL
);
337 static void check_updates(struct cache_entry
**src
, int nr
)
339 static struct checkout state
= {
345 unsigned short mask
= htons(CE_UPDATE
);
346 unsigned last_percent
= 200, cnt
= 0, total
= 0;
348 if (update
&& verbose_update
) {
349 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
350 struct cache_entry
*ce
= src
[cnt
];
351 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
355 /* Don't bother doing this for very small updates */
360 fprintf(stderr
, "Checking files out...\n");
361 setup_progress_signal();
368 struct cache_entry
*ce
= *src
++;
371 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
) {
374 percent
= (cnt
* 100) / total
;
375 if (percent
!= last_percent
||
377 fprintf(stderr
, "%4u%% (%u/%u) done\r",
378 percent
, cnt
, total
);
379 last_percent
= percent
;
385 unlink_entry(ce
->name
);
388 if (ce
->ce_flags
& mask
) {
389 ce
->ce_flags
&= ~mask
;
391 checkout_entry(ce
, &state
, NULL
);
395 signal(SIGALRM
, SIG_IGN
);
400 static int unpack_trees(merge_fn_t fn
)
403 unsigned len
= object_list_length(trees
);
404 struct tree_entry_list
**posns
;
406 struct object_list
*posn
= trees
;
410 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
411 for (i
= 0; i
< len
; i
++) {
412 posns
[i
] = create_tree_entry_list((struct tree
*) posn
->item
);
415 if (unpack_trees_rec(posns
, len
, prefix
? prefix
: "",
420 if (trivial_merges_only
&& nontrivial_merge
)
421 die("Merge requires file-level merging");
423 check_updates(active_cache
, active_nr
);
427 static int list_tree(unsigned char *sha1
)
429 struct tree
*tree
= parse_tree_indirect(sha1
);
432 object_list_append(&tree
->object
, &trees
);
436 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
442 return a
->ce_mode
== b
->ce_mode
&&
443 !memcmp(a
->sha1
, b
->sha1
, 20);
448 * When a CE gets turned into an unmerged entry, we
449 * want it to be up-to-date
451 static void verify_uptodate(struct cache_entry
*ce
)
455 if (index_only
|| reset
)
458 if (!lstat(ce
->name
, &st
)) {
459 unsigned changed
= ce_match_stat(ce
, &st
, 1);
465 ce
->ce_flags
|= htons(CE_UPDATE
);
470 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
473 static void invalidate_ce_path(struct cache_entry
*ce
)
476 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
480 * We do not want to remove or overwrite a working tree file that
483 static void verify_absent(const char *path
, const char *action
)
487 if (index_only
|| reset
|| !update
)
489 if (!lstat(path
, &st
))
490 die("Untracked working tree file '%s' "
491 "would be %s by merge.", path
, action
);
494 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
)
496 merge
->ce_flags
|= htons(CE_UPDATE
);
499 * See if we can re-use the old CE directly?
500 * That way we get the uptodate stat info.
502 * This also removes the UPDATE flag on
505 if (same(old
, merge
)) {
508 verify_uptodate(old
);
509 invalidate_ce_path(old
);
513 verify_absent(merge
->name
, "overwritten");
514 invalidate_ce_path(merge
);
517 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
518 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
);
522 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
)
525 verify_uptodate(old
);
527 verify_absent(ce
->name
, "removed");
529 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
530 invalidate_ce_path(ce
);
534 static int keep_entry(struct cache_entry
*ce
)
536 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
541 static void show_stage_entry(FILE *o
,
542 const char *label
, const struct cache_entry
*ce
)
545 fprintf(o
, "%s (missing)\n", label
);
547 fprintf(o
, "%s%06o %s %d\t%s\n",
550 sha1_to_hex(ce
->sha1
),
556 static int threeway_merge(struct cache_entry
**stages
)
558 struct cache_entry
*index
;
559 struct cache_entry
*head
;
560 struct cache_entry
*remote
= stages
[head_idx
+ 1];
563 int remote_match
= 0;
564 const char *path
= NULL
;
566 int df_conflict_head
= 0;
567 int df_conflict_remote
= 0;
569 int any_anc_missing
= 0;
570 int no_anc_exists
= 1;
573 for (i
= 1; i
< head_idx
; i
++) {
578 path
= stages
[i
]->name
;
584 head
= stages
[head_idx
];
586 if (head
== &df_conflict_entry
) {
587 df_conflict_head
= 1;
591 if (remote
== &df_conflict_entry
) {
592 df_conflict_remote
= 1;
603 /* First, if there's a #16 situation, note that to prevent #13
606 if (!same(remote
, head
)) {
607 for (i
= 1; i
< head_idx
; i
++) {
608 if (same(stages
[i
], head
)) {
611 if (same(stages
[i
], remote
)) {
617 /* We start with cases where the index is allowed to match
618 * something other than the head: #14(ALT) and #2ALT, where it
619 * is permitted to match the result instead.
621 /* #14, #14ALT, #2ALT */
622 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
623 if (index
&& !same(index
, remote
) && !same(index
, head
))
625 return merged_entry(remote
, index
);
628 * If we have an entry in the index cache, then we want to
629 * make sure that it matches head.
631 if (index
&& !same(index
, head
)) {
637 if (same(head
, remote
))
638 return merged_entry(head
, index
);
640 if (!df_conflict_remote
&& remote_match
&& !head_match
)
641 return merged_entry(head
, index
);
645 if (!head
&& !remote
&& any_anc_missing
)
648 /* Under the new "aggressive" rule, we resolve mostly trivial
649 * cases that we historically had git-merge-one-file resolve.
652 int head_deleted
= !head
&& !df_conflict_head
;
653 int remote_deleted
= !remote
&& !df_conflict_remote
;
656 * Deleted in one and unchanged in the other.
658 if ((head_deleted
&& remote_deleted
) ||
659 (head_deleted
&& remote
&& remote_match
) ||
660 (remote_deleted
&& head
&& head_match
)) {
662 return deleted_entry(index
, index
);
664 verify_absent(path
, "removed");
668 * Added in both, identically.
670 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
671 return merged_entry(head
, index
);
675 /* Below are "no merge" cases, which require that the index be
676 * up-to-date to avoid the files getting overwritten with
677 * conflict resolution files.
680 verify_uptodate(index
);
683 verify_absent(path
, "overwritten");
685 nontrivial_merge
= 1;
687 /* #2, #3, #4, #6, #7, #9, #11. */
689 if (!head_match
|| !remote_match
) {
690 for (i
= 1; i
< head_idx
; i
++) {
692 keep_entry(stages
[i
]);
700 fprintf(stderr
, "read-tree: warning #16 detected\n");
701 show_stage_entry(stderr
, "head ", stages
[head_match
]);
702 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
705 if (head
) { count
+= keep_entry(head
); }
706 if (remote
) { count
+= keep_entry(remote
); }
713 * The rule is to "carry forward" what is in the index without losing
714 * information across a "fast forward", favoring a successful merge
715 * over a merge failure when it makes sense. For details of the
716 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
719 static int twoway_merge(struct cache_entry
**src
)
721 struct cache_entry
*current
= src
[0];
722 struct cache_entry
*oldtree
= src
[1], *newtree
= src
[2];
725 return error("Cannot do a twoway merge of %d trees",
729 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
730 (!oldtree
&& newtree
&&
731 same(current
, newtree
)) || /* 6 and 7 */
732 (oldtree
&& newtree
&&
733 same(oldtree
, newtree
)) || /* 14 and 15 */
734 (oldtree
&& newtree
&&
735 !same(oldtree
, newtree
) && /* 18 and 19*/
736 same(current
, newtree
))) {
737 return keep_entry(current
);
739 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
741 return deleted_entry(oldtree
, current
);
743 else if (oldtree
&& newtree
&&
744 same(current
, oldtree
) && !same(current
, newtree
)) {
746 return merged_entry(newtree
, current
);
749 /* all other failures */
751 reject_merge(oldtree
);
753 reject_merge(current
);
755 reject_merge(newtree
);
760 return merged_entry(newtree
, current
);
762 return deleted_entry(oldtree
, current
);
768 * Keep the index entries at stage0, collapse stage1 but make sure
769 * stage0 does not have anything there.
771 static int bind_merge(struct cache_entry
**src
)
773 struct cache_entry
*old
= src
[0];
774 struct cache_entry
*a
= src
[1];
777 return error("Cannot do a bind merge of %d trees\n",
780 die("Entry '%s' overlaps. Cannot bind.", a
->name
);
782 return keep_entry(old
);
784 return merged_entry(a
, NULL
);
791 * - take the stat information from stage0, take the data from stage1
793 static int oneway_merge(struct cache_entry
**src
)
795 struct cache_entry
*old
= src
[0];
796 struct cache_entry
*a
= src
[1];
799 return error("Cannot do a oneway merge of %d trees",
803 return deleted_entry(old
, old
);
804 if (old
&& same(old
, a
)) {
807 if (lstat(old
->name
, &st
) ||
808 ce_match_stat(old
, &st
, 1))
809 old
->ce_flags
|= htons(CE_UPDATE
);
811 return keep_entry(old
);
813 return merged_entry(a
, old
);
816 static int read_cache_unmerged(void)
819 struct cache_entry
**dst
;
820 struct cache_entry
*last
= NULL
;
824 for (i
= 0; i
< active_nr
; i
++) {
825 struct cache_entry
*ce
= active_cache
[i
];
827 if (last
&& !strcmp(ce
->name
, last
->name
))
829 invalidate_ce_path(ce
);
832 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
836 active_nr
= dst
- active_cache
;
840 static void prime_cache_tree_rec(struct cache_tree
*it
, struct tree
*tree
)
842 struct tree_desc desc
;
843 struct name_entry entry
;
846 memcpy(it
->sha1
, tree
->object
.sha1
, 20);
847 desc
.buf
= tree
->buffer
;
848 desc
.size
= tree
->size
;
850 while (tree_entry(&desc
, &entry
)) {
851 if (!S_ISDIR(entry
.mode
))
854 struct cache_tree_sub
*sub
;
855 struct tree
*subtree
= lookup_tree(entry
.sha1
);
856 if (!subtree
->object
.parsed
)
858 sub
= cache_tree_sub(it
, entry
.path
);
859 sub
->cache_tree
= cache_tree();
860 prime_cache_tree_rec(sub
->cache_tree
, subtree
);
861 cnt
+= sub
->cache_tree
->entry_count
;
864 it
->entry_count
= cnt
;
867 static void prime_cache_tree(void)
869 struct tree
*tree
= (struct tree
*)trees
->item
;
872 active_cache_tree
= cache_tree();
873 prime_cache_tree_rec(active_cache_tree
, tree
);
877 static const char read_tree_usage
[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])";
879 static struct cache_file cache_file
;
881 int cmd_read_tree(int argc
, const char **argv
, char **envp
)
883 int i
, newfd
, stage
= 0;
884 unsigned char sha1
[20];
885 merge_fn_t fn
= NULL
;
887 setup_git_directory();
888 git_config(git_default_config
);
890 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
892 die("unable to create new cachefile");
894 git_config(git_default_config
);
898 for (i
= 1; i
< argc
; i
++) {
899 const char *arg
= argv
[i
];
901 /* "-u" means "update", meaning that a merge will update
904 if (!strcmp(arg
, "-u")) {
909 if (!strcmp(arg
, "-v")) {
914 /* "-i" means "index only", meaning that a merge will
915 * not even look at the working tree.
917 if (!strcmp(arg
, "-i")) {
922 /* "--prefix=<subdirectory>/" means keep the current index
923 * entries and put the entries from the tree under the
924 * given subdirectory.
926 if (!strncmp(arg
, "--prefix=", 9)) {
927 if (stage
|| merge
|| prefix
)
928 usage(read_tree_usage
);
932 if (read_cache_unmerged())
933 die("you need to resolve your current index first");
937 /* This differs from "-m" in that we'll silently ignore
938 * unmerged entries and overwrite working tree files that
939 * correspond to them.
941 if (!strcmp(arg
, "--reset")) {
942 if (stage
|| merge
|| prefix
)
943 usage(read_tree_usage
);
947 read_cache_unmerged();
951 if (!strcmp(arg
, "--trivial")) {
952 trivial_merges_only
= 1;
956 if (!strcmp(arg
, "--aggressive")) {
961 /* "-m" stands for "merge", meaning we start in stage 1 */
962 if (!strcmp(arg
, "-m")) {
963 if (stage
|| merge
|| prefix
)
964 usage(read_tree_usage
);
965 if (read_cache_unmerged())
966 die("you need to resolve your current index first");
972 /* using -u and -i at the same time makes no sense */
973 if (1 < index_only
+ update
)
974 usage(read_tree_usage
);
976 if (get_sha1(arg
, sha1
))
977 die("Not a valid object name %s", arg
);
978 if (list_tree(sha1
) < 0)
979 die("failed to unpack tree object %s", arg
);
982 if ((update
||index_only
) && !merge
)
983 usage(read_tree_usage
);
986 int pfxlen
= strlen(prefix
);
988 if (prefix
[pfxlen
-1] != '/')
989 die("prefix must end with /");
991 die("binding merge takes only one tree");
992 pos
= cache_name_pos(prefix
, pfxlen
);
994 die("corrupt index file");
996 if (pos
< active_nr
&&
997 !strncmp(active_cache
[pos
]->name
, prefix
, pfxlen
))
998 die("subdirectory '%s' already exists.", prefix
);
999 pos
= cache_name_pos(prefix
, pfxlen
-1);
1001 die("file '%.*s' already exists.", pfxlen
-1, prefix
);
1006 die("just how do you expect me to merge %d trees?", stage
-1);
1007 switch (stage
- 1) {
1009 fn
= prefix
? bind_merge
: oneway_merge
;
1016 fn
= threeway_merge
;
1017 cache_tree_free(&active_cache_tree
);
1022 head_idx
= stage
- 2;
1030 * When reading only one tree (either the most basic form,
1031 * "-m ent" or "--reset ent" form), we can obtain a fully
1032 * valid cache-tree because the index must match exactly
1033 * what came from the tree.
1035 if (trees
&& trees
->item
&& !prefix
&& (!merge
|| (stage
== 2))) {
1036 cache_tree_free(&active_cache_tree
);
1040 if (write_cache(newfd
, active_cache
, active_nr
) ||
1041 commit_index_file(&cache_file
))
1042 die("unable to write new index file");