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;
28 static int head_idx
= -1;
29 static int merge_size
= 0;
31 static struct object_list
*trees
= NULL
;
33 static struct cache_entry df_conflict_entry
= {
36 struct tree_entry_list
{
37 struct tree_entry_list
*next
;
38 unsigned directory
: 1;
39 unsigned executable
: 1;
43 const unsigned char *sha1
;
46 static struct tree_entry_list df_conflict_list
= {
48 .next
= &df_conflict_list
51 typedef int (*merge_fn_t
)(struct cache_entry
**src
);
53 static struct tree_entry_list
*create_tree_entry_list(struct tree
*tree
)
55 struct tree_desc desc
;
56 struct name_entry one
;
57 struct tree_entry_list
*ret
= NULL
;
58 struct tree_entry_list
**list_p
= &ret
;
60 desc
.buf
= tree
->buffer
;
61 desc
.size
= tree
->size
;
63 while (tree_entry(&desc
, &one
)) {
64 struct tree_entry_list
*entry
;
66 entry
= xmalloc(sizeof(struct tree_entry_list
));
67 entry
->name
= one
.path
;
68 entry
->sha1
= one
.sha1
;
69 entry
->mode
= one
.mode
;
70 entry
->directory
= S_ISDIR(one
.mode
) != 0;
71 entry
->executable
= (one
.mode
& S_IXUSR
) != 0;
72 entry
->symlink
= S_ISLNK(one
.mode
) != 0;
76 list_p
= &entry
->next
;
81 static int entcmp(const char *name1
, int dir1
, const char *name2
, int dir2
)
83 int len1
= strlen(name1
);
84 int len2
= strlen(name2
);
85 int len
= len1
< len2
? len1
: len2
;
86 int ret
= memcmp(name1
, name2
, len
);
96 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
102 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
103 const char *base
, merge_fn_t fn
, int *indpos
)
105 int baselen
= strlen(base
);
106 int src_size
= len
+ 1;
113 struct tree_entry_list
**subposns
;
114 struct cache_entry
**src
;
120 /* Find the first name in the input. */
125 /* Check the cache */
126 if (merge
&& *indpos
< active_nr
) {
127 /* This is a bit tricky: */
128 /* If the index has a subdirectory (with
129 * contents) as the first name, it'll get a
130 * filename like "foo/bar". But that's after
131 * "foo", so the entry in trees will get
132 * handled first, at which point we'll go into
133 * "foo", and deal with "bar" from the index,
134 * because the base will be "foo/". The only
135 * way we can actually have "foo/bar" first of
136 * all the things is if the trees don't
137 * contain "foo" at all, in which case we'll
138 * handle "foo/bar" without going into the
139 * directory, but that's fine (and will return
140 * an error anyway, with the added unknown
144 cache_name
= active_cache
[*indpos
]->name
;
145 if (strlen(cache_name
) > baselen
&&
146 !memcmp(cache_name
, base
, baselen
)) {
147 cache_name
+= baselen
;
156 printf("index %s\n", first
);
158 for (i
= 0; i
< len
; i
++) {
159 if (!posns
[i
] || posns
[i
] == &df_conflict_list
)
162 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
164 if (!first
|| entcmp(first
, firstdir
,
166 posns
[i
]->directory
) > 0) {
167 first
= posns
[i
]->name
;
168 firstdir
= posns
[i
]->directory
;
171 /* No name means we're done */
175 pathlen
= strlen(first
);
176 ce_size
= cache_entry_size(baselen
+ pathlen
);
178 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
180 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
182 if (cache_name
&& !strcmp(cache_name
, first
)) {
184 src
[0] = active_cache
[*indpos
];
185 remove_cache_entry_at(*indpos
);
188 for (i
= 0; i
< len
; i
++) {
189 struct cache_entry
*ce
;
192 (posns
[i
] != &df_conflict_list
&&
193 strcmp(first
, posns
[i
]->name
))) {
197 if (posns
[i
] == &df_conflict_list
) {
198 src
[i
+ merge
] = &df_conflict_entry
;
202 if (posns
[i
]->directory
) {
203 struct tree
*tree
= lookup_tree(posns
[i
]->sha1
);
206 subposns
[i
] = create_tree_entry_list(tree
);
207 posns
[i
] = posns
[i
]->next
;
208 src
[i
+ merge
] = &df_conflict_entry
;
214 else if (i
+ 1 < head_idx
)
216 else if (i
+ 1 > head_idx
)
221 ce
= xcalloc(1, ce_size
);
222 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
223 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
225 memcpy(ce
->name
, base
, baselen
);
226 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
230 memcpy(ce
->sha1
, posns
[i
]->sha1
, 20);
232 subposns
[i
] = &df_conflict_list
;
233 posns
[i
] = posns
[i
]->next
;
240 printf("%s:\n", first
);
241 for (i
= 0; i
< src_size
; i
++) {
244 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
252 printf("Added %d entries\n", ret
);
256 for (i
= 0; i
< src_size
; i
++) {
258 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
264 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
265 memcpy(newbase
, base
, baselen
);
266 memcpy(newbase
+ baselen
, first
, pathlen
);
267 newbase
[baselen
+ pathlen
] = '/';
268 newbase
[baselen
+ pathlen
+ 1] = '\0';
269 if (unpack_trees_rec(subposns
, len
, newbase
, fn
,
279 static void reject_merge(struct cache_entry
*ce
)
281 die("Entry '%s' would be overwritten by merge. Cannot merge.",
285 /* Unlink the last component and attempt to remove leading
286 * directories, in case this unlink is the removal of the
287 * last entry in the directory -- empty directories are removed.
289 static void unlink_entry(char *name
)
298 cp
= strrchr(name
, '/');
305 status
= rmdir(name
);
314 static void progress_interval(int signum
)
319 static void setup_progress_signal(void)
324 memset(&sa
, 0, sizeof(sa
));
325 sa
.sa_handler
= progress_interval
;
326 sigemptyset(&sa
.sa_mask
);
327 sa
.sa_flags
= SA_RESTART
;
328 sigaction(SIGALRM
, &sa
, NULL
);
330 v
.it_interval
.tv_sec
= 1;
331 v
.it_interval
.tv_usec
= 0;
332 v
.it_value
= v
.it_interval
;
333 setitimer(ITIMER_REAL
, &v
, NULL
);
336 static void check_updates(struct cache_entry
**src
, int nr
)
338 static struct checkout state
= {
344 unsigned short mask
= htons(CE_UPDATE
);
345 unsigned last_percent
= 200, cnt
= 0, total
= 0;
347 if (update
&& verbose_update
) {
348 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
349 struct cache_entry
*ce
= src
[cnt
];
350 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
354 /* Don't bother doing this for very small updates */
359 fprintf(stderr
, "Checking files out...\n");
360 setup_progress_signal();
367 struct cache_entry
*ce
= *src
++;
370 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
) {
373 percent
= (cnt
* 100) / total
;
374 if (percent
!= last_percent
||
376 fprintf(stderr
, "%4u%% (%u/%u) done\r",
377 percent
, cnt
, total
);
378 last_percent
= percent
;
384 unlink_entry(ce
->name
);
387 if (ce
->ce_flags
& mask
) {
388 ce
->ce_flags
&= ~mask
;
390 checkout_entry(ce
, &state
, NULL
);
394 signal(SIGALRM
, SIG_IGN
);
399 static int unpack_trees(merge_fn_t fn
)
402 unsigned len
= object_list_length(trees
);
403 struct tree_entry_list
**posns
;
405 struct object_list
*posn
= trees
;
409 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
410 for (i
= 0; i
< len
; i
++) {
411 posns
[i
] = create_tree_entry_list((struct tree
*) posn
->item
);
414 if (unpack_trees_rec(posns
, len
, "", fn
, &indpos
))
418 if (trivial_merges_only
&& nontrivial_merge
)
419 die("Merge requires file-level merging");
421 check_updates(active_cache
, active_nr
);
425 static int list_tree(unsigned char *sha1
)
427 struct tree
*tree
= parse_tree_indirect(sha1
);
430 object_list_append(&tree
->object
, &trees
);
434 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
440 return a
->ce_mode
== b
->ce_mode
&&
441 !memcmp(a
->sha1
, b
->sha1
, 20);
446 * When a CE gets turned into an unmerged entry, we
447 * want it to be up-to-date
449 static void verify_uptodate(struct cache_entry
*ce
)
453 if (index_only
|| reset
)
456 if (!lstat(ce
->name
, &st
)) {
457 unsigned changed
= ce_match_stat(ce
, &st
, 1);
463 ce
->ce_flags
|= htons(CE_UPDATE
);
468 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
471 static void invalidate_ce_path(struct cache_entry
*ce
)
474 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
478 * We do not want to remove or overwrite a working tree file that
481 static void verify_absent(const char *path
, const char *action
)
485 if (index_only
|| reset
|| !update
)
487 if (!lstat(path
, &st
))
488 die("Untracked working tree file '%s' "
489 "would be %s by merge.", path
, action
);
492 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
)
494 merge
->ce_flags
|= htons(CE_UPDATE
);
497 * See if we can re-use the old CE directly?
498 * That way we get the uptodate stat info.
500 * This also removes the UPDATE flag on
503 if (same(old
, merge
)) {
506 verify_uptodate(old
);
507 invalidate_ce_path(old
);
511 verify_absent(merge
->name
, "overwritten");
512 invalidate_ce_path(merge
);
515 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
516 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
);
520 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
)
523 verify_uptodate(old
);
525 verify_absent(ce
->name
, "removed");
527 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
528 invalidate_ce_path(ce
);
532 static int keep_entry(struct cache_entry
*ce
)
534 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
539 static void show_stage_entry(FILE *o
,
540 const char *label
, const struct cache_entry
*ce
)
543 fprintf(o
, "%s (missing)\n", label
);
545 fprintf(o
, "%s%06o %s %d\t%s\n",
548 sha1_to_hex(ce
->sha1
),
554 static int threeway_merge(struct cache_entry
**stages
)
556 struct cache_entry
*index
;
557 struct cache_entry
*head
;
558 struct cache_entry
*remote
= stages
[head_idx
+ 1];
561 int remote_match
= 0;
562 const char *path
= NULL
;
564 int df_conflict_head
= 0;
565 int df_conflict_remote
= 0;
567 int any_anc_missing
= 0;
568 int no_anc_exists
= 1;
571 for (i
= 1; i
< head_idx
; i
++) {
576 path
= stages
[i
]->name
;
582 head
= stages
[head_idx
];
584 if (head
== &df_conflict_entry
) {
585 df_conflict_head
= 1;
589 if (remote
== &df_conflict_entry
) {
590 df_conflict_remote
= 1;
601 /* First, if there's a #16 situation, note that to prevent #13
604 if (!same(remote
, head
)) {
605 for (i
= 1; i
< head_idx
; i
++) {
606 if (same(stages
[i
], head
)) {
609 if (same(stages
[i
], remote
)) {
615 /* We start with cases where the index is allowed to match
616 * something other than the head: #14(ALT) and #2ALT, where it
617 * is permitted to match the result instead.
619 /* #14, #14ALT, #2ALT */
620 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
621 if (index
&& !same(index
, remote
) && !same(index
, head
))
623 return merged_entry(remote
, index
);
626 * If we have an entry in the index cache, then we want to
627 * make sure that it matches head.
629 if (index
&& !same(index
, head
)) {
635 if (same(head
, remote
))
636 return merged_entry(head
, index
);
638 if (!df_conflict_remote
&& remote_match
&& !head_match
)
639 return merged_entry(head
, index
);
643 if (!head
&& !remote
&& any_anc_missing
)
646 /* Under the new "aggressive" rule, we resolve mostly trivial
647 * cases that we historically had git-merge-one-file resolve.
650 int head_deleted
= !head
&& !df_conflict_head
;
651 int remote_deleted
= !remote
&& !df_conflict_remote
;
654 * Deleted in one and unchanged in the other.
656 if ((head_deleted
&& remote_deleted
) ||
657 (head_deleted
&& remote
&& remote_match
) ||
658 (remote_deleted
&& head
&& head_match
)) {
660 return deleted_entry(index
, index
);
662 verify_absent(path
, "removed");
666 * Added in both, identically.
668 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
669 return merged_entry(head
, index
);
673 /* Below are "no merge" cases, which require that the index be
674 * up-to-date to avoid the files getting overwritten with
675 * conflict resolution files.
678 verify_uptodate(index
);
681 verify_absent(path
, "overwritten");
683 nontrivial_merge
= 1;
685 /* #2, #3, #4, #6, #7, #9, #11. */
687 if (!head_match
|| !remote_match
) {
688 for (i
= 1; i
< head_idx
; i
++) {
690 keep_entry(stages
[i
]);
698 fprintf(stderr
, "read-tree: warning #16 detected\n");
699 show_stage_entry(stderr
, "head ", stages
[head_match
]);
700 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
703 if (head
) { count
+= keep_entry(head
); }
704 if (remote
) { count
+= keep_entry(remote
); }
711 * The rule is to "carry forward" what is in the index without losing
712 * information across a "fast forward", favoring a successful merge
713 * over a merge failure when it makes sense. For details of the
714 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
717 static int twoway_merge(struct cache_entry
**src
)
719 struct cache_entry
*current
= src
[0];
720 struct cache_entry
*oldtree
= src
[1], *newtree
= src
[2];
723 return error("Cannot do a twoway merge of %d trees",
727 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
728 (!oldtree
&& newtree
&&
729 same(current
, newtree
)) || /* 6 and 7 */
730 (oldtree
&& newtree
&&
731 same(oldtree
, newtree
)) || /* 14 and 15 */
732 (oldtree
&& newtree
&&
733 !same(oldtree
, newtree
) && /* 18 and 19*/
734 same(current
, newtree
))) {
735 return keep_entry(current
);
737 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
739 return deleted_entry(oldtree
, current
);
741 else if (oldtree
&& newtree
&&
742 same(current
, oldtree
) && !same(current
, newtree
)) {
744 return merged_entry(newtree
, current
);
747 /* all other failures */
749 reject_merge(oldtree
);
751 reject_merge(current
);
753 reject_merge(newtree
);
758 return merged_entry(newtree
, current
);
760 return deleted_entry(oldtree
, current
);
767 * - take the stat information from stage0, take the data from stage1
769 static int oneway_merge(struct cache_entry
**src
)
771 struct cache_entry
*old
= src
[0];
772 struct cache_entry
*a
= src
[1];
775 return error("Cannot do a oneway merge of %d trees",
779 return deleted_entry(old
, old
);
780 if (old
&& same(old
, a
)) {
783 if (lstat(old
->name
, &st
) ||
784 ce_match_stat(old
, &st
, 1))
785 old
->ce_flags
|= htons(CE_UPDATE
);
787 return keep_entry(old
);
789 return merged_entry(a
, old
);
792 static int read_cache_unmerged(void)
795 struct cache_entry
**dst
;
796 struct cache_entry
*last
= NULL
;
800 for (i
= 0; i
< active_nr
; i
++) {
801 struct cache_entry
*ce
= active_cache
[i
];
803 if (last
&& !strcmp(ce
->name
, last
->name
))
805 invalidate_ce_path(ce
);
808 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
812 active_nr
= dst
- active_cache
;
816 static void prime_cache_tree_rec(struct cache_tree
*it
, struct tree
*tree
)
818 struct tree_desc desc
;
819 struct name_entry entry
;
822 memcpy(it
->sha1
, tree
->object
.sha1
, 20);
823 desc
.buf
= tree
->buffer
;
824 desc
.size
= tree
->size
;
826 while (tree_entry(&desc
, &entry
)) {
827 if (!S_ISDIR(entry
.mode
))
830 struct cache_tree_sub
*sub
;
831 struct tree
*subtree
= lookup_tree(entry
.sha1
);
832 if (!subtree
->object
.parsed
)
834 sub
= cache_tree_sub(it
, entry
.path
);
835 sub
->cache_tree
= cache_tree();
836 prime_cache_tree_rec(sub
->cache_tree
, subtree
);
837 cnt
+= sub
->cache_tree
->entry_count
;
840 it
->entry_count
= cnt
;
843 static void prime_cache_tree(void)
845 struct tree
*tree
= (struct tree
*)trees
->item
;
848 active_cache_tree
= cache_tree();
849 prime_cache_tree_rec(active_cache_tree
, tree
);
853 static const char read_tree_usage
[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
855 static struct cache_file cache_file
;
857 int cmd_read_tree(int argc
, const char **argv
, char **envp
)
859 int i
, newfd
, stage
= 0;
860 unsigned char sha1
[20];
861 merge_fn_t fn
= NULL
;
863 setup_git_directory();
864 git_config(git_default_config
);
866 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
868 die("unable to create new cachefile");
870 git_config(git_default_config
);
874 for (i
= 1; i
< argc
; i
++) {
875 const char *arg
= argv
[i
];
877 /* "-u" means "update", meaning that a merge will update
880 if (!strcmp(arg
, "-u")) {
885 if (!strcmp(arg
, "-v")) {
890 /* "-i" means "index only", meaning that a merge will
891 * not even look at the working tree.
893 if (!strcmp(arg
, "-i")) {
898 /* This differs from "-m" in that we'll silently ignore
899 * unmerged entries and overwrite working tree files that
900 * correspond to them.
902 if (!strcmp(arg
, "--reset")) {
904 usage(read_tree_usage
);
908 read_cache_unmerged();
912 if (!strcmp(arg
, "--trivial")) {
913 trivial_merges_only
= 1;
917 if (!strcmp(arg
, "--aggressive")) {
922 /* "-m" stands for "merge", meaning we start in stage 1 */
923 if (!strcmp(arg
, "-m")) {
925 usage(read_tree_usage
);
926 if (read_cache_unmerged())
927 die("you need to resolve your current index first");
933 /* using -u and -i at the same time makes no sense */
934 if (1 < index_only
+ update
)
935 usage(read_tree_usage
);
937 if (get_sha1(arg
, sha1
))
938 die("Not a valid object name %s", arg
);
939 if (list_tree(sha1
) < 0)
940 die("failed to unpack tree object %s", arg
);
943 if ((update
||index_only
) && !merge
)
944 usage(read_tree_usage
);
948 die("just how do you expect me to merge %d trees?", stage
-1);
959 cache_tree_free(&active_cache_tree
);
964 head_idx
= stage
- 2;
972 * When reading only one tree (either the most basic form,
973 * "-m ent" or "--reset ent" form), we can obtain a fully
974 * valid cache-tree because the index must match exactly
975 * what came from the tree.
977 if (trees
&& trees
->item
&& (!merge
|| (stage
== 2))) {
978 cache_tree_free(&active_cache_tree
);
982 if (write_cache(newfd
, active_cache
, active_nr
) ||
983 commit_index_file(&cache_file
))
984 die("unable to write new index file");