2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
14 static int update
= 0;
15 static int index_only
= 0;
17 static int head_idx
= -1;
18 static int merge_size
= 0;
20 static struct object_list
*trees
= NULL
;
22 static struct cache_entry df_conflict_entry
= {
25 static struct tree_entry_list df_conflict_list
= {
27 .next
= &df_conflict_list
30 typedef int (*merge_fn_t
)(struct cache_entry
**src
);
32 static int entcmp(char *name1
, int dir1
, char *name2
, int dir2
)
34 int len1
= strlen(name1
);
35 int len2
= strlen(name2
);
36 int len
= len1
< len2
? len1
: len2
;
37 int ret
= memcmp(name1
, name2
, len
);
47 ret
= (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
53 static int unpack_trees_rec(struct tree_entry_list
**posns
, int len
,
54 const char *base
, merge_fn_t fn
, int *indpos
)
56 int baselen
= strlen(base
);
57 int src_size
= len
+ 1;
64 struct tree_entry_list
**subposns
;
65 struct cache_entry
**src
;
71 /* Find the first name in the input. */
77 if (merge
&& *indpos
< active_nr
) {
78 /* This is a bit tricky: */
79 /* If the index has a subdirectory (with
80 * contents) as the first name, it'll get a
81 * filename like "foo/bar". But that's after
82 * "foo", so the entry in trees will get
83 * handled first, at which point we'll go into
84 * "foo", and deal with "bar" from the index,
85 * because the base will be "foo/". The only
86 * way we can actually have "foo/bar" first of
87 * all the things is if the trees don't
88 * contain "foo" at all, in which case we'll
89 * handle "foo/bar" without going into the
90 * directory, but that's fine (and will return
91 * an error anyway, with the added unknown
95 cache_name
= active_cache
[*indpos
]->name
;
96 if (strlen(cache_name
) > baselen
&&
97 !memcmp(cache_name
, base
, baselen
)) {
98 cache_name
+= baselen
;
107 printf("index %s\n", first
);
109 for (i
= 0; i
< len
; i
++) {
110 if (!posns
[i
] || posns
[i
] == &df_conflict_list
)
113 printf("%d %s\n", i
+ 1, posns
[i
]->name
);
115 if (!first
|| entcmp(first
, firstdir
,
117 posns
[i
]->directory
) > 0) {
118 first
= posns
[i
]->name
;
119 firstdir
= posns
[i
]->directory
;
122 /* No name means we're done */
126 pathlen
= strlen(first
);
127 ce_size
= cache_entry_size(baselen
+ pathlen
);
129 src
= xmalloc(sizeof(struct cache_entry
*) * src_size
);
130 memset(src
, 0, sizeof(struct cache_entry
*) * src_size
);
132 subposns
= xmalloc(sizeof(struct tree_list_entry
*) * len
);
133 memset(subposns
, 0, sizeof(struct tree_list_entry
*) * len
);
135 if (cache_name
&& !strcmp(cache_name
, first
)) {
137 src
[0] = active_cache
[*indpos
];
138 remove_cache_entry_at(*indpos
);
141 for (i
= 0; i
< len
; i
++) {
142 struct cache_entry
*ce
;
145 (posns
[i
] != &df_conflict_list
&&
146 strcmp(first
, posns
[i
]->name
))) {
150 if (posns
[i
] == &df_conflict_list
) {
151 src
[i
+ merge
] = &df_conflict_entry
;
155 if (posns
[i
]->directory
) {
157 parse_tree(posns
[i
]->item
.tree
);
158 subposns
[i
] = posns
[i
]->item
.tree
->entries
;
159 posns
[i
] = posns
[i
]->next
;
160 src
[i
+ merge
] = &df_conflict_entry
;
166 else if (i
+ 1 < head_idx
)
168 else if (i
+ 1 > head_idx
)
173 ce
= xmalloc(ce_size
);
174 memset(ce
, 0, ce_size
);
175 ce
->ce_mode
= create_ce_mode(posns
[i
]->mode
);
176 ce
->ce_flags
= create_ce_flags(baselen
+ pathlen
,
178 memcpy(ce
->name
, base
, baselen
);
179 memcpy(ce
->name
+ baselen
, first
, pathlen
+ 1);
183 memcpy(ce
->sha1
, posns
[i
]->item
.any
->sha1
, 20);
185 subposns
[i
] = &df_conflict_list
;
186 posns
[i
] = posns
[i
]->next
;
193 printf("%s:\n", first
);
194 for (i
= 0; i
< src_size
; i
++) {
197 printf("%s\n", sha1_to_hex(src
[i
]->sha1
));
205 printf("Added %d entries\n", ret
);
209 for (i
= 0; i
< src_size
; i
++) {
211 add_cache_entry(src
[i
], ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
217 char *newbase
= xmalloc(baselen
+ 2 + pathlen
);
218 memcpy(newbase
, base
, baselen
);
219 memcpy(newbase
+ baselen
, first
, pathlen
);
220 newbase
[baselen
+ pathlen
] = '/';
221 newbase
[baselen
+ pathlen
+ 1] = '\0';
222 if (unpack_trees_rec(subposns
, len
, newbase
, fn
,
232 static void reject_merge(struct cache_entry
*ce
)
234 die("Entry '%s' would be overwritten by merge. Cannot merge.",
238 static void check_updates(struct cache_entry
**src
, int nr
)
240 static struct checkout state
= {
246 unsigned short mask
= htons(CE_UPDATE
);
248 struct cache_entry
*ce
= *src
++;
254 if (ce
->ce_flags
& mask
) {
255 ce
->ce_flags
&= ~mask
;
257 checkout_entry(ce
, &state
);
262 static int unpack_trees(merge_fn_t fn
)
265 unsigned len
= object_list_length(trees
);
266 struct tree_entry_list
**posns
=
267 xmalloc(len
* sizeof(struct tree_entry_list
*));
269 struct object_list
*posn
= trees
;
271 for (i
= 0; i
< len
; i
++) {
272 posns
[i
] = ((struct tree
*) posn
->item
)->entries
;
275 if (unpack_trees_rec(posns
, len
, "", fn
, &indpos
))
278 check_updates(active_cache
, active_nr
);
282 static int list_tree(unsigned char *sha1
)
284 struct tree
*tree
= parse_tree_indirect(sha1
);
287 object_list_append(&tree
->object
, &trees
);
291 static int same(struct cache_entry
*a
, struct cache_entry
*b
)
297 return a
->ce_mode
== b
->ce_mode
&&
298 !memcmp(a
->sha1
, b
->sha1
, 20);
303 * When a CE gets turned into an unmerged entry, we
304 * want it to be up-to-date
306 static void verify_uptodate(struct cache_entry
*ce
)
313 if (!lstat(ce
->name
, &st
)) {
314 unsigned changed
= ce_match_stat(ce
, &st
);
321 die("Entry '%s' not uptodate. Cannot merge.", ce
->name
);
324 static int merged_entry(struct cache_entry
*merge
, struct cache_entry
*old
)
326 merge
->ce_flags
|= htons(CE_UPDATE
);
329 * See if we can re-use the old CE directly?
330 * That way we get the uptodate stat info.
332 * This also removes the UPDATE flag on
335 if (same(old
, merge
)) {
338 verify_uptodate(old
);
341 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
342 add_cache_entry(merge
, ADD_CACHE_OK_TO_ADD
);
346 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
)
349 verify_uptodate(old
);
351 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
355 static int keep_entry(struct cache_entry
*ce
)
357 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
);
362 static void show_stage_entry(FILE *o
,
363 const char *label
, const struct cache_entry
*ce
)
365 fprintf(stderr
, "%s%06o %s %d\t%s\n",
368 sha1_to_hex(ce
->sha1
),
374 static int threeway_merge(struct cache_entry
**stages
)
376 struct cache_entry
*index
;
377 struct cache_entry
*head
;
378 struct cache_entry
*remote
= stages
[head_idx
+ 1];
381 int remote_match
= 0;
383 int df_conflict_head
= 0;
384 int df_conflict_remote
= 0;
386 int any_anc_missing
= 0;
389 for (i
= 1; i
< head_idx
; i
++) {
395 head
= stages
[head_idx
];
397 if (head
== &df_conflict_entry
) {
398 df_conflict_head
= 1;
402 if (remote
== &df_conflict_entry
) {
403 df_conflict_remote
= 1;
407 /* First, if there's a #16 situation, note that to prevent #13
410 if (!same(remote
, head
)) {
411 for (i
= 1; i
< head_idx
; i
++) {
412 if (same(stages
[i
], head
)) {
415 if (same(stages
[i
], remote
)) {
421 /* We start with cases where the index is allowed to match
422 * something other than the head: #14(ALT) and #2ALT, where it
423 * is permitted to match the result instead.
425 /* #14, #14ALT, #2ALT */
426 if (remote
&& !df_conflict_head
&& head_match
&& !remote_match
) {
427 if (index
&& !same(index
, remote
) && !same(index
, head
))
429 return merged_entry(remote
, index
);
432 * If we have an entry in the index cache, then we want to
433 * make sure that it matches head.
435 if (index
&& !same(index
, head
)) {
441 if (same(head
, remote
))
442 return merged_entry(head
, index
);
444 if (!df_conflict_remote
&& remote_match
&& !head_match
)
445 return merged_entry(head
, index
);
449 if (!head
&& !remote
&& any_anc_missing
)
452 /* Below are "no merge" cases, which require that the index be
453 * up-to-date to avoid the files getting overwritten with
454 * conflict resolution files.
457 verify_uptodate(index
);
460 /* #2, #3, #4, #6, #7, #9, #11. */
462 if (!head_match
|| !remote_match
) {
463 for (i
= 1; i
< head_idx
; i
++) {
465 keep_entry(stages
[i
]);
473 fprintf(stderr
, "read-tree: warning #16 detected\n");
474 show_stage_entry(stderr
, "head ", stages
[head_match
]);
475 show_stage_entry(stderr
, "remote ", stages
[remote_match
]);
478 if (head
) { count
+= keep_entry(head
); }
479 if (remote
) { count
+= keep_entry(remote
); }
486 * The rule is to "carry forward" what is in the index without losing
487 * information across a "fast forward", favoring a successful merge
488 * over a merge failure when it makes sense. For details of the
489 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
492 static int twoway_merge(struct cache_entry
**src
)
494 struct cache_entry
*current
= src
[0];
495 struct cache_entry
*oldtree
= src
[1], *newtree
= src
[2];
498 return error("Cannot do a twoway merge of %d trees\n",
502 if ((!oldtree
&& !newtree
) || /* 4 and 5 */
503 (!oldtree
&& newtree
&&
504 same(current
, newtree
)) || /* 6 and 7 */
505 (oldtree
&& newtree
&&
506 same(oldtree
, newtree
)) || /* 14 and 15 */
507 (oldtree
&& newtree
&&
508 !same(oldtree
, newtree
) && /* 18 and 19*/
509 same(current
, newtree
))) {
510 return keep_entry(current
);
512 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
514 return deleted_entry(oldtree
, current
);
516 else if (oldtree
&& newtree
&&
517 same(current
, oldtree
) && !same(current
, newtree
)) {
519 return merged_entry(newtree
, current
);
522 /* all other failures */
524 reject_merge(oldtree
);
526 reject_merge(current
);
528 reject_merge(newtree
);
533 return merged_entry(newtree
, current
);
535 return deleted_entry(oldtree
, current
);
542 * - take the stat information from stage0, take the data from stage1
544 static int oneway_merge(struct cache_entry
**src
)
546 struct cache_entry
*old
= src
[0];
547 struct cache_entry
*a
= src
[1];
550 return error("Cannot do a oneway merge of %d trees\n",
555 if (old
&& same(old
, a
)) {
556 return keep_entry(old
);
558 return merged_entry(a
, NULL
);
561 static int read_cache_unmerged(void)
564 struct cache_entry
**dst
;
569 for (i
= 0; i
< active_nr
; i
++) {
570 struct cache_entry
*ce
= active_cache
[i
];
579 active_nr
-= deleted
;
583 static const char read_tree_usage
[] = "git-read-tree (<sha> | -m [-u | -i] <sha1> [<sha2> [<sha3>]])";
585 static struct cache_file cache_file
;
587 int main(int argc
, char **argv
)
589 int i
, newfd
, reset
, stage
= 0;
590 unsigned char sha1
[20];
591 merge_fn_t fn
= NULL
;
593 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
595 die("unable to create new cachefile");
599 for (i
= 1; i
< argc
; i
++) {
600 const char *arg
= argv
[i
];
602 /* "-u" means "update", meaning that a merge will update
605 if (!strcmp(arg
, "-u")) {
610 /* "-i" means "index only", meaning that a merge will
611 * not even look at the working tree.
613 if (!strcmp(arg
, "-i")) {
618 /* This differs from "-m" in that we'll silently ignore unmerged entries */
619 if (!strcmp(arg
, "--reset")) {
621 usage(read_tree_usage
);
625 read_cache_unmerged();
629 if (!strcmp(arg
, "--head")) {
630 head_idx
= stage
- 1;
634 /* "-m" stands for "merge", meaning we start in stage 1 */
635 if (!strcmp(arg
, "-m")) {
637 usage(read_tree_usage
);
638 if (read_cache_unmerged())
639 die("you need to resolve your current index first");
645 /* using -u and -i at the same time makes no sense */
646 if (1 < index_only
+ update
)
647 usage(read_tree_usage
);
649 if (get_sha1(arg
, sha1
) < 0)
650 usage(read_tree_usage
);
651 if (list_tree(sha1
) < 0)
652 die("failed to unpack tree object %s", arg
);
655 if (update
&& !merge
)
656 usage(read_tree_usage
);
659 die("just how do you expect me to merge %d trees?", stage
-1);
678 head_idx
= stage
- 2;
684 if (write_cache(newfd
, active_cache
, active_nr
) ||
685 commit_index_file(&cache_file
))
686 die("unable to write new index file");