2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
16 static int update
= 0;
17 static int index_only
= 0;
18 static int nontrivial_merge
= 0;
19 static int trivial_merges_only
= 0;
20 static int aggressive
= 0;
21 static int verbose_update
= 0;
22 static volatile int progress_update
= 0;
24 static int head_idx
= -1;
25 static int merge_size
= 0;
27 static struct object_list
*trees
= NULL
;
29 static struct cache_entry df_conflict_entry
= {
32 static struct tree_entry_list df_conflict_list
= {
34 .next
= &df_conflict_list
37 typedef int (*merge_fn_t
)(struct cache_entry
**src
);
39 static int entcmp(char *name1
, int dir1
, char *name2
, int dir2
)
41 int len1
= strlen(name1
);
42 int len2
= strlen(name2
);
43 int len
= len1
< len2
? len1
: len2
;
44 int ret
= memcmp(name1
, name2
, len
);
54 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
60 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
61 const char *base
, merge_fn_t fn
, int *indpos
)
63 int baselen
= strlen(base
);
64 int src_size
= len
+ 1;
71 struct tree_entry_list
**subposns
;
72 struct cache_entry
**src
;
78 /* Find the first name in the input. */
84 if (merge
&& *indpos
< active_nr
) {
85 /* This is a bit tricky: */
86 /* If the index has a subdirectory (with
87 * contents) as the first name, it'll get a
88 * filename like "foo/bar". But that's after
89 * "foo", so the entry in trees will get
90 * handled first, at which point we'll go into
91 * "foo", and deal with "bar" from the index,
92 * because the base will be "foo/". The only
93 * way we can actually have "foo/bar" first of
94 * all the things is if the trees don't
95 * contain "foo" at all, in which case we'll
96 * handle "foo/bar" without going into the
97 * directory, but that's fine (and will return
98 * an error anyway, with the added unknown
102 cache_name
= active_cache
[*indpos
]->name
;
103 if (strlen(cache_name
) > baselen
&&
104 !memcmp(cache_name
, base
, baselen
)) {
105 cache_name
+= baselen
;
114 printf("index %s\n", first
);
116 for (i
= 0; i
< len
; i
++) {
117 if (!posns
[i
] || posns
[i
] == &df_conflict_list
)
120 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
122 if (!first
|| entcmp(first
, firstdir
,
124 posns
[i
]->directory
) > 0) {
125 first
= posns
[i
]->name
;
126 firstdir
= posns
[i
]->directory
;
129 /* No name means we're done */
133 pathlen
= strlen(first
);
134 ce_size
= cache_entry_size(baselen
+ pathlen
);
136 src
= xcalloc(src_size
, sizeof(struct cache_entry
*));
138 subposns
= xcalloc(len
, sizeof(struct tree_list_entry
*));
140 if (cache_name
&& !strcmp(cache_name
, first
)) {
142 src
[0] = active_cache
[*indpos
];
143 remove_cache_entry_at(*indpos
);
146 for (i
= 0; i
< len
; i
++) {
147 struct cache_entry
*ce
;
150 (posns
[i
] != &df_conflict_list
&&
151 strcmp(first
, posns
[i
]->name
))) {
155 if (posns
[i
] == &df_conflict_list
) {
156 src
[i
+ merge
] = &df_conflict_entry
;
160 if (posns
[i
]->directory
) {
162 parse_tree(posns
[i
]->item
.tree
);
163 subposns
[i
] = posns
[i
]->item
.tree
->entries
;
164 posns
[i
] = posns
[i
]->next
;
165 src
[i
+ merge
] = &df_conflict_entry
;
171 else if (i
+ 1 < head_idx
)
173 else if (i
+ 1 > head_idx
)
178 ce
= xcalloc(1, ce_size
);
179 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
180 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
182 memcpy(ce
->name
, base
, baselen
);
183 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
187 memcpy(ce
->sha1
, posns
[i
]->item
.any
->sha1
, 20);
189 subposns
[i
] = &df_conflict_list
;
190 posns
[i
] = posns
[i
]->next
;
197 printf("%s:\n", first
);
198 for (i
= 0; i
< src_size
; i
++) {
201 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
209 printf("Added %d entries\n", ret
);
213 for (i
= 0; i
< src_size
; i
++) {
215 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
221 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
222 memcpy(newbase
, base
, baselen
);
223 memcpy(newbase
+ baselen
, first
, pathlen
);
224 newbase
[baselen
+ pathlen
] = '/';
225 newbase
[baselen
+ pathlen
+ 1] = '\0';
226 if (unpack_trees_rec(subposns
, len
, newbase
, fn
,
236 static void reject_merge(struct cache_entry
*ce
)
238 die("Entry '%s' would be overwritten by merge. Cannot merge.",
242 /* Unlink the last component and attempt to remove leading
243 * directories, in case this unlink is the removal of the
244 * last entry in the directory -- empty directories are removed.
246 static void unlink_entry(char *name
)
255 cp
= strrchr(name
, '/');
262 status
= rmdir(name
);
271 static void progress_interval(int signum
)
276 static void setup_progress_signal(void)
281 memset(&sa
, 0, sizeof(sa
));
282 sa
.sa_handler
= progress_interval
;
283 sigemptyset(&sa
.sa_mask
);
284 sa
.sa_flags
= SA_RESTART
;
285 sigaction(SIGALRM
, &sa
, NULL
);
287 v
.it_interval
.tv_sec
= 1;
288 v
.it_interval
.tv_usec
= 0;
289 v
.it_value
= v
.it_interval
;
290 setitimer(ITIMER_REAL
, &v
, NULL
);
293 static void check_updates(struct cache_entry
**src
, int nr
)
295 static struct checkout state
= {
301 unsigned short mask
= htons(CE_UPDATE
);
302 unsigned last_percent
= 200, cnt
= 0, total
= 0;
304 if (update
&& verbose_update
) {
305 for (total
= cnt
= 0; cnt
< nr
; cnt
++) {
306 struct cache_entry
*ce
= src
[cnt
];
307 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
)
311 /* Don't bother doing this for very small updates */
316 fprintf(stderr
, "Checking files out...\n");
317 setup_progress_signal();
324 struct cache_entry
*ce
= *src
++;
327 if (!ce
->ce_mode
|| ce
->ce_flags
& mask
) {
330 percent
= (cnt
* 100) / total
;
331 if (percent
!= last_percent
||
333 fprintf(stderr
, "%4u%% (%u/%u) done\r",
334 percent
, cnt
, total
);
335 last_percent
= percent
;
341 unlink_entry(ce
->name
);
344 if (ce
->ce_flags
& mask
) {
345 ce
->ce_flags
&= ~mask
;
347 checkout_entry(ce
, &state
, NULL
);
351 signal(SIGALRM
, SIG_IGN
);
356 static int unpack_trees(merge_fn_t fn
)
359 unsigned len
= object_list_length(trees
);
360 struct tree_entry_list
**posns
;
362 struct object_list
*posn
= trees
;
366 posns
= xmalloc(len
* sizeof(struct tree_entry_list
*));
367 for (i
= 0; i
< len
; i
++) {
368 posns
[i
] = ((struct tree
*) posn
->item
)->entries
;
371 if (unpack_trees_rec(posns
, len
, "", fn
, &indpos
))
375 if (trivial_merges_only
&& nontrivial_merge
)
376 die("Merge requires file-level merging");
378 check_updates(active_cache
, active_nr
);
382 static int list_tree(unsigned char *sha1
)
384 struct tree
*tree
= parse_tree_indirect(sha1
);
387 object_list_append(&tree
->object
, &trees
);
391 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
397 return a
->ce_mode
== b
->ce_mode
&&
398 !memcmp(a
->sha1
, b
->sha1
, 20);
403 * When a CE gets turned into an unmerged entry, we
404 * want it to be up-to-date
406 static void verify_uptodate(struct cache_entry
*ce
)
413 if (!lstat(ce
->name
, &st
)) {
414 unsigned changed
= ce_match_stat(ce
, &st
, 1);
421 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
424 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
)
426 merge
->ce_flags
|= htons(CE_UPDATE
);
429 * See if we can re-use the old CE directly?
430 * That way we get the uptodate stat info.
432 * This also removes the UPDATE flag on
435 if (same(old
, merge
)) {
438 verify_uptodate(old
);
441 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
442 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
);
446 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
)
449 verify_uptodate(old
);
451 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
455 static int keep_entry(struct cache_entry
*ce
)
457 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
462 static void show_stage_entry(FILE *o
,
463 const char *label
, const struct cache_entry
*ce
)
466 fprintf(o
, "%s (missing)\n", label
);
468 fprintf(o
, "%s%06o %s %d\t%s\n",
471 sha1_to_hex(ce
->sha1
),
477 static int threeway_merge(struct cache_entry
**stages
)
479 struct cache_entry
*index
;
480 struct cache_entry
*head
;
481 struct cache_entry
*remote
= stages
[head_idx
+ 1];
484 int remote_match
= 0;
486 int df_conflict_head
= 0;
487 int df_conflict_remote
= 0;
489 int any_anc_missing
= 0;
490 int no_anc_exists
= 1;
493 for (i
= 1; i
< head_idx
; i
++) {
501 head
= stages
[head_idx
];
503 if (head
== &df_conflict_entry
) {
504 df_conflict_head
= 1;
508 if (remote
== &df_conflict_entry
) {
509 df_conflict_remote
= 1;
513 /* First, if there's a #16 situation, note that to prevent #13
516 if (!same(remote
, head
)) {
517 for (i
= 1; i
< head_idx
; i
++) {
518 if (same(stages
[i
], head
)) {
521 if (same(stages
[i
], remote
)) {
527 /* We start with cases where the index is allowed to match
528 * something other than the head: #14(ALT) and #2ALT, where it
529 * is permitted to match the result instead.
531 /* #14, #14ALT, #2ALT */
532 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
533 if (index
&& !same(index
, remote
) && !same(index
, head
))
535 return merged_entry(remote
, index
);
538 * If we have an entry in the index cache, then we want to
539 * make sure that it matches head.
541 if (index
&& !same(index
, head
)) {
547 if (same(head
, remote
))
548 return merged_entry(head
, index
);
550 if (!df_conflict_remote
&& remote_match
&& !head_match
)
551 return merged_entry(head
, index
);
555 if (!head
&& !remote
&& any_anc_missing
)
558 /* Under the new "aggressive" rule, we resolve mostly trivial
559 * cases that we historically had git-merge-one-file resolve.
562 int head_deleted
= !head
&& !df_conflict_head
;
563 int remote_deleted
= !remote
&& !df_conflict_remote
;
566 * Deleted in one and unchanged in the other.
568 if ((head_deleted
&& remote_deleted
) ||
569 (head_deleted
&& remote
&& remote_match
) ||
570 (remote_deleted
&& head
&& head_match
)) {
572 return deleted_entry(index
, index
);
576 * Added in both, identically.
578 if (no_anc_exists
&& head
&& remote
&& same(head
, remote
))
579 return merged_entry(head
, index
);
583 /* Below are "no merge" cases, which require that the index be
584 * up-to-date to avoid the files getting overwritten with
585 * conflict resolution files.
588 verify_uptodate(index
);
591 nontrivial_merge
= 1;
593 /* #2, #3, #4, #6, #7, #9, #11. */
595 if (!head_match
|| !remote_match
) {
596 for (i
= 1; i
< head_idx
; i
++) {
598 keep_entry(stages
[i
]);
606 fprintf(stderr
, "read-tree: warning #16 detected\n");
607 show_stage_entry(stderr
, "head ", stages
[head_match
]);
608 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
611 if (head
) { count
+= keep_entry(head
); }
612 if (remote
) { count
+= keep_entry(remote
); }
619 * The rule is to "carry forward" what is in the index without losing
620 * information across a "fast forward", favoring a successful merge
621 * over a merge failure when it makes sense. For details of the
622 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
625 static int twoway_merge(struct cache_entry
**src
)
627 struct cache_entry
*current
= src
[0];
628 struct cache_entry
*oldtree
= src
[1], *newtree
= src
[2];
631 return error("Cannot do a twoway merge of %d trees",
635 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
636 (!oldtree
&& newtree
&&
637 same(current
, newtree
)) || /* 6 and 7 */
638 (oldtree
&& newtree
&&
639 same(oldtree
, newtree
)) || /* 14 and 15 */
640 (oldtree
&& newtree
&&
641 !same(oldtree
, newtree
) && /* 18 and 19*/
642 same(current
, newtree
))) {
643 return keep_entry(current
);
645 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
647 return deleted_entry(oldtree
, current
);
649 else if (oldtree
&& newtree
&&
650 same(current
, oldtree
) && !same(current
, newtree
)) {
652 return merged_entry(newtree
, current
);
655 /* all other failures */
657 reject_merge(oldtree
);
659 reject_merge(current
);
661 reject_merge(newtree
);
666 return merged_entry(newtree
, current
);
668 return deleted_entry(oldtree
, current
);
675 * - take the stat information from stage0, take the data from stage1
677 static int oneway_merge(struct cache_entry
**src
)
679 struct cache_entry
*old
= src
[0];
680 struct cache_entry
*a
= src
[1];
683 return error("Cannot do a oneway merge of %d trees",
687 return deleted_entry(old
, NULL
);
688 if (old
&& same(old
, a
)) {
689 return keep_entry(old
);
691 return merged_entry(a
, NULL
);
694 static int read_cache_unmerged(void)
697 struct cache_entry
**dst
;
702 for (i
= 0; i
< active_nr
; i
++) {
703 struct cache_entry
*ce
= active_cache
[i
];
712 active_nr
-= deleted
;
716 static const char read_tree_usage
[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
718 static struct cache_file cache_file
;
720 int main(int argc
, char **argv
)
722 int i
, newfd
, reset
, stage
= 0;
723 unsigned char sha1
[20];
724 merge_fn_t fn
= NULL
;
726 setup_git_directory();
727 git_config(git_default_config
);
729 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
731 die("unable to create new cachefile");
733 git_config(git_default_config
);
737 for (i
= 1; i
< argc
; i
++) {
738 const char *arg
= argv
[i
];
740 /* "-u" means "update", meaning that a merge will update
743 if (!strcmp(arg
, "-u")) {
748 if (!strcmp(arg
, "-v")) {
753 /* "-i" means "index only", meaning that a merge will
754 * not even look at the working tree.
756 if (!strcmp(arg
, "-i")) {
761 /* This differs from "-m" in that we'll silently ignore unmerged entries */
762 if (!strcmp(arg
, "--reset")) {
764 usage(read_tree_usage
);
768 read_cache_unmerged();
772 if (!strcmp(arg
, "--trivial")) {
773 trivial_merges_only
= 1;
777 if (!strcmp(arg
, "--aggressive")) {
782 /* "-m" stands for "merge", meaning we start in stage 1 */
783 if (!strcmp(arg
, "-m")) {
785 usage(read_tree_usage
);
786 if (read_cache_unmerged())
787 die("you need to resolve your current index first");
793 /* using -u and -i at the same time makes no sense */
794 if (1 < index_only
+ update
)
795 usage(read_tree_usage
);
797 if (get_sha1(arg
, sha1
))
798 die("Not a valid object name %s", arg
);
799 if (list_tree(sha1
) < 0)
800 die("failed to unpack tree object %s", arg
);
803 if ((update
||index_only
) && !merge
)
804 usage(read_tree_usage
);
808 die("just how do you expect me to merge %d trees?", stage
-1);
825 head_idx
= stage
- 2;
831 if (write_cache(newfd
, active_cache
, active_nr
) ||
832 commit_index_file(&cache_file
))
833 die("unable to write new index file");