2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
14 static int update
= 0;
16 static int head_idx
= -1;
17 static int merge_size
= 0;
19 static struct object_list
*trees
= NULL
;
21 static struct cache_entry df_conflict_entry
= {
24 static struct tree_entry_list df_conflict_list
= {
26 .next
= &df_conflict_list
29 typedef int (*merge_fn_t
)(struct cache_entry
**src
);
31 static int entcmp(char *name1
, int dir1
, char *name2
, int dir2
)
33 int len1
= strlen(name1
);
34 int len2
= strlen(name2
);
35 int len
= len1
< len2
? len1
: len2
;
36 int ret
= memcmp(name1
, name2
, len
);
46 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
52 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
53 const char *base
, merge_fn_t fn
, int *indpos
)
55 int baselen
= strlen(base
);
56 int src_size
= len
+ 1;
63 struct tree_entry_list
**subposns
;
64 struct cache_entry
**src
;
70 /* Find the first name in the input. */
76 if (merge
&& *indpos
< active_nr
) {
77 /* This is a bit tricky: */
78 /* If the index has a subdirectory (with
79 * contents) as the first name, it'll get a
80 * filename like "foo/bar". But that's after
81 * "foo", so the entry in trees will get
82 * handled first, at which point we'll go into
83 * "foo", and deal with "bar" from the index,
84 * because the base will be "foo/". The only
85 * way we can actually have "foo/bar" first of
86 * all the things is if the trees don't
87 * contain "foo" at all, in which case we'll
88 * handle "foo/bar" without going into the
89 * directory, but that's fine (and will return
90 * an error anyway, with the added unknown
94 cache_name
= active_cache
[*indpos
]->name
;
95 if (strlen(cache_name
) > baselen
&&
96 !memcmp(cache_name
, base
, baselen
)) {
97 cache_name
+= baselen
;
106 printf("index %s\n", first
);
108 for (i
= 0; i
< len
; i
++) {
109 if (!posns
[i
] || posns
[i
] == &df_conflict_list
)
112 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
114 if (!first
|| entcmp(first
, firstdir
,
116 posns
[i
]->directory
) > 0) {
117 first
= posns
[i
]->name
;
118 firstdir
= posns
[i
]->directory
;
121 /* No name means we're done */
125 pathlen
= strlen(first
);
126 ce_size
= cache_entry_size(baselen
+ pathlen
);
128 src
= xmalloc(sizeof(struct cache_entry
*) * src_size
);
129 memset(src
, 0, sizeof(struct cache_entry
*) * src_size
);
131 subposns
= xmalloc(sizeof(struct tree_list_entry
*) * len
);
132 memset(subposns
, 0, sizeof(struct tree_list_entry
*) * len
);
134 if (cache_name
&& !strcmp(cache_name
, first
)) {
136 src
[0] = active_cache
[*indpos
];
137 remove_cache_entry_at(*indpos
);
140 for (i
= 0; i
< len
; i
++) {
141 struct cache_entry
*ce
;
144 (posns
[i
] != &df_conflict_list
&&
145 strcmp(first
, posns
[i
]->name
))) {
149 if (posns
[i
] == &df_conflict_list
) {
150 src
[i
+ merge
] = &df_conflict_entry
;
154 if (posns
[i
]->directory
) {
156 parse_tree(posns
[i
]->item
.tree
);
157 subposns
[i
] = posns
[i
]->item
.tree
->entries
;
158 posns
[i
] = posns
[i
]->next
;
159 src
[i
+ merge
] = &df_conflict_entry
;
165 else if (i
+ 1 < head_idx
)
167 else if (i
+ 1 > head_idx
)
172 ce
= xmalloc(ce_size
);
173 memset(ce
, 0, ce_size
);
174 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
175 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
177 memcpy(ce
->name
, base
, baselen
);
178 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
182 memcpy(ce
->sha1
, posns
[i
]->item
.any
->sha1
, 20);
184 subposns
[i
] = &df_conflict_list
;
185 posns
[i
] = posns
[i
]->next
;
192 printf("%s:\n", first
);
193 for (i
= 0; i
< src_size
; i
++) {
196 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
204 printf("Added %d entries\n", ret
);
208 for (i
= 0; i
< src_size
; i
++) {
210 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
216 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
217 memcpy(newbase
, base
, baselen
);
218 memcpy(newbase
+ baselen
, first
, pathlen
);
219 newbase
[baselen
+ pathlen
] = '/';
220 newbase
[baselen
+ pathlen
+ 1] = '\0';
221 if (unpack_trees_rec(subposns
, len
, newbase
, fn
,
231 static void reject_merge(struct cache_entry
*ce
)
233 die("Entry '%s' would be overwritten by merge. Cannot merge.",
237 static void check_updates(struct cache_entry
**src
, int nr
)
239 static struct checkout state
= {
245 unsigned short mask
= htons(CE_UPDATE
);
247 struct cache_entry
*ce
= *src
++;
253 if (ce
->ce_flags
& mask
) {
254 ce
->ce_flags
&= ~mask
;
256 checkout_entry(ce
, &state
);
261 static int unpack_trees(merge_fn_t fn
)
264 unsigned len
= object_list_length(trees
);
265 struct tree_entry_list
**posns
=
266 xmalloc(len
* sizeof(struct tree_entry_list
*));
268 struct object_list
*posn
= trees
;
270 for (i
= 0; i
< len
; i
++) {
271 posns
[i
] = ((struct tree
*) posn
->item
)->entries
;
274 if (unpack_trees_rec(posns
, len
, "", fn
, &indpos
))
277 check_updates(active_cache
, active_nr
);
281 static int list_tree(unsigned char *sha1
)
283 struct tree
*tree
= parse_tree_indirect(sha1
);
286 object_list_append(&tree
->object
, &trees
);
290 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
296 return a
->ce_mode
== b
->ce_mode
&&
297 !memcmp(a
->sha1
, b
->sha1
, 20);
302 * When a CE gets turned into an unmerged entry, we
303 * want it to be up-to-date
305 static void verify_uptodate(struct cache_entry
*ce
)
309 if (!lstat(ce
->name
, &st
)) {
310 unsigned changed
= ce_match_stat(ce
, &st
);
317 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
320 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
)
322 merge
->ce_flags
|= htons(CE_UPDATE
);
325 * See if we can re-use the old CE directly?
326 * That way we get the uptodate stat info.
328 * This also removes the UPDATE flag on
331 if (same(old
, merge
)) {
334 verify_uptodate(old
);
337 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
338 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
);
342 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
)
345 verify_uptodate(old
);
347 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
351 static int keep_entry(struct cache_entry
*ce
)
353 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
358 static void show_stage_entry(FILE *o
,
359 const char *label
, const struct cache_entry
*ce
)
361 fprintf(stderr
, "%s%06o %s %d\t%s\n",
364 sha1_to_hex(ce
->sha1
),
370 static int threeway_merge(struct cache_entry
**stages
)
372 struct cache_entry
*index
;
373 struct cache_entry
*head
;
374 struct cache_entry
*remote
= stages
[head_idx
+ 1];
377 int remote_match
= 0;
379 int df_conflict_head
= 0;
380 int df_conflict_remote
= 0;
382 int any_anc_missing
= 0;
385 for (i
= 1; i
< head_idx
; i
++) {
391 head
= stages
[head_idx
];
393 if (head
== &df_conflict_entry
) {
394 df_conflict_head
= 1;
398 if (remote
== &df_conflict_entry
) {
399 df_conflict_remote
= 1;
403 /* First, if there's a #16 situation, note that to prevent #13
406 if (!same(remote
, head
)) {
407 for (i
= 1; i
< head_idx
; i
++) {
408 if (same(stages
[i
], head
)) {
411 if (same(stages
[i
], remote
)) {
417 /* We start with cases where the index is allowed to match
418 * something other than the head: #14(ALT) and #2ALT, where it
419 * is permitted to match the result instead.
421 /* #14, #14ALT, #2ALT */
422 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
423 if (index
&& !same(index
, remote
) && !same(index
, head
))
425 return merged_entry(remote
, index
);
428 * If we have an entry in the index cache, then we want to
429 * make sure that it matches head.
431 if (index
&& !same(index
, head
)) {
437 if (same(head
, remote
))
438 return merged_entry(head
, index
);
440 if (!df_conflict_remote
&& remote_match
&& !head_match
)
441 return merged_entry(head
, index
);
445 if (!head
&& !remote
&& any_anc_missing
)
448 /* Below are "no merge" cases, which require that the index be
449 * up-to-date to avoid the files getting overwritten with
450 * conflict resolution files.
453 verify_uptodate(index
);
456 /* #2, #3, #4, #6, #7, #9, #11. */
458 if (!head_match
|| !remote_match
) {
459 for (i
= 1; i
< head_idx
; i
++) {
461 keep_entry(stages
[i
]);
469 fprintf(stderr
, "read-tree: warning #16 detected\n");
470 show_stage_entry(stderr
, "head ", stages
[head_match
]);
471 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
474 if (head
) { count
+= keep_entry(head
); }
475 if (remote
) { count
+= keep_entry(remote
); }
482 * The rule is to "carry forward" what is in the index without losing
483 * information across a "fast forward", favoring a successful merge
484 * over a merge failure when it makes sense. For details of the
485 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
488 static int twoway_merge(struct cache_entry
**src
)
490 struct cache_entry
*current
= src
[0];
491 struct cache_entry
*oldtree
= src
[1], *newtree
= src
[2];
494 return error("Cannot do a twoway merge of %d trees\n",
498 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
499 (!oldtree
&& newtree
&&
500 same(current
, newtree
)) || /* 6 and 7 */
501 (oldtree
&& newtree
&&
502 same(oldtree
, newtree
)) || /* 14 and 15 */
503 (oldtree
&& newtree
&&
504 !same(oldtree
, newtree
) && /* 18 and 19*/
505 same(current
, newtree
))) {
506 return keep_entry(current
);
508 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
510 return deleted_entry(oldtree
, current
);
512 else if (oldtree
&& newtree
&&
513 same(current
, oldtree
) && !same(current
, newtree
)) {
515 return merged_entry(newtree
, current
);
518 /* all other failures */
520 reject_merge(oldtree
);
522 reject_merge(current
);
524 reject_merge(newtree
);
529 return merged_entry(newtree
, current
);
531 return deleted_entry(oldtree
, current
);
538 * - take the stat information from stage0, take the data from stage1
540 static int oneway_merge(struct cache_entry
**src
)
542 struct cache_entry
*old
= src
[0];
543 struct cache_entry
*a
= src
[1];
546 return error("Cannot do a oneway merge of %d trees\n",
551 if (old
&& same(old
, a
)) {
552 return keep_entry(old
);
554 return merged_entry(a
, NULL
);
557 static int read_cache_unmerged(void)
560 struct cache_entry
**dst
;
565 for (i
= 0; i
< active_nr
; i
++) {
566 struct cache_entry
*ce
= active_cache
[i
];
575 active_nr
-= deleted
;
579 static const char read_tree_usage
[] = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])";
581 static struct cache_file cache_file
;
583 int main(int argc
, char **argv
)
585 int i
, newfd
, reset
, stage
= 0;
586 unsigned char sha1
[20];
587 merge_fn_t fn
= NULL
;
589 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
591 die("unable to create new cachefile");
595 for (i
= 1; i
< argc
; i
++) {
596 const char *arg
= argv
[i
];
598 /* "-u" means "update", meaning that a merge will update the working directory */
599 if (!strcmp(arg
, "-u")) {
604 /* This differs from "-m" in that we'll silently ignore unmerged entries */
605 if (!strcmp(arg
, "--reset")) {
607 usage(read_tree_usage
);
611 read_cache_unmerged();
615 if (!strcmp(arg
, "--head")) {
616 head_idx
= stage
- 1;
620 /* "-m" stands for "merge", meaning we start in stage 1 */
621 if (!strcmp(arg
, "-m")) {
623 usage(read_tree_usage
);
624 if (read_cache_unmerged())
625 die("you need to resolve your current index first");
631 if (get_sha1(arg
, sha1
) < 0)
632 usage(read_tree_usage
);
633 if (list_tree(sha1
) < 0)
634 die("failed to unpack tree object %s", arg
);
637 if (update
&& !merge
)
638 usage(read_tree_usage
);
641 die("just how do you expect me to merge %d trees?", stage
-1);
660 head_idx
= stage
- 2;
666 if (write_cache(newfd
, active_cache
, active_nr
) ||
667 commit_index_file(&cache_file
))
668 die("unable to write new index file");