'git-merge': Documentation.
[alt-git.git] / read-tree.c
blob9ace5bb0ab65cd2c0abb98e333972429e4de0dd0
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define DBRT_DEBUG 1
8 #include "cache.h"
10 #include "object.h"
11 #include "tree.h"
13 static int merge = 0;
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 = {
25 .name = NULL,
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);
37 unsigned char c1, c2;
38 if (ret)
39 return ret;
40 c1 = name1[len];
41 c2 = name2[len];
42 if (!c1 && dir1)
43 c1 = '/';
44 if (!c2 && dir2)
45 c2 = '/';
46 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
47 if (c1 && c2 && !ret)
48 ret = len1 - len2;
49 return ret;
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;
57 do {
58 int i;
59 char *first;
60 int firstdir = 0;
61 int pathlen;
62 unsigned ce_size;
63 struct tree_entry_list **subposns;
64 struct cache_entry **src;
65 int any_files = 0;
66 int any_dirs = 0;
67 char *cache_name;
68 int ce_stage;
70 /* Find the first name in the input. */
72 first = NULL;
73 cache_name = NULL;
75 /* Check the cache */
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
91 * file case.
94 cache_name = active_cache[*indpos]->name;
95 if (strlen(cache_name) > baselen &&
96 !memcmp(cache_name, base, baselen)) {
97 cache_name += baselen;
98 first = cache_name;
99 } else {
100 cache_name = NULL;
104 #if DBRT_DEBUG > 1
105 if (first)
106 printf("index %s\n", first);
107 #endif
108 for (i = 0; i < len; i++) {
109 if (!posns[i] || posns[i] == &df_conflict_list)
110 continue;
111 #if DBRT_DEBUG > 1
112 printf("%d %s\n", i + 1, posns[i]->name);
113 #endif
114 if (!first || entcmp(first, firstdir,
115 posns[i]->name,
116 posns[i]->directory) > 0) {
117 first = posns[i]->name;
118 firstdir = posns[i]->directory;
121 /* No name means we're done */
122 if (!first)
123 return 0;
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)) {
135 any_files = 1;
136 src[0] = active_cache[*indpos];
137 remove_cache_entry_at(*indpos);
140 for (i = 0; i < len; i++) {
141 struct cache_entry *ce;
143 if (!posns[i] ||
144 (posns[i] != &df_conflict_list &&
145 strcmp(first, posns[i]->name))) {
146 continue;
149 if (posns[i] == &df_conflict_list) {
150 src[i + merge] = &df_conflict_entry;
151 continue;
154 if (posns[i]->directory) {
155 any_dirs = 1;
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;
160 continue;
163 if (!merge)
164 ce_stage = 0;
165 else if (i + 1 < head_idx)
166 ce_stage = 1;
167 else if (i + 1 > head_idx)
168 ce_stage = 3;
169 else
170 ce_stage = 2;
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,
176 ce_stage);
177 memcpy(ce->name, base, baselen);
178 memcpy(ce->name + baselen, first, pathlen + 1);
180 any_files = 1;
182 memcpy(ce->sha1, posns[i]->item.any->sha1, 20);
183 src[i + merge] = ce;
184 subposns[i] = &df_conflict_list;
185 posns[i] = posns[i]->next;
187 if (any_files) {
188 if (merge) {
189 int ret;
191 #if DBRT_DEBUG > 1
192 printf("%s:\n", first);
193 for (i = 0; i < src_size; i++) {
194 printf(" %d ", i);
195 if (src[i])
196 printf("%s\n", sha1_to_hex(src[i]->sha1));
197 else
198 printf("\n");
200 #endif
201 ret = fn(src);
203 #if DBRT_DEBUG > 1
204 printf("Added %d entries\n", ret);
205 #endif
206 *indpos += ret;
207 } else {
208 for (i = 0; i < src_size; i++) {
209 if (src[i]) {
210 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
215 if (any_dirs) {
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,
222 indpos))
223 return -1;
224 free(newbase);
226 free(subposns);
227 free(src);
228 } while (1);
231 static void reject_merge(struct cache_entry *ce)
233 die("Entry '%s' would be overwritten by merge. Cannot merge.",
234 ce->name);
237 static void check_updates(struct cache_entry **src, int nr)
239 static struct checkout state = {
240 .base_dir = "",
241 .force = 1,
242 .quiet = 1,
243 .refresh_cache = 1,
245 unsigned short mask = htons(CE_UPDATE);
246 while (nr--) {
247 struct cache_entry *ce = *src++;
248 if (!ce->ce_mode) {
249 if (update)
250 unlink(ce->name);
251 continue;
253 if (ce->ce_flags & mask) {
254 ce->ce_flags &= ~mask;
255 if (update)
256 checkout_entry(ce, &state);
261 static int unpack_trees(merge_fn_t fn)
263 int indpos = 0;
264 unsigned len = object_list_length(trees);
265 struct tree_entry_list **posns =
266 xmalloc(len * sizeof(struct tree_entry_list *));
267 int i;
268 struct object_list *posn = trees;
269 merge_size = len;
270 for (i = 0; i < len; i++) {
271 posns[i] = ((struct tree *) posn->item)->entries;
272 posn = posn->next;
274 if (unpack_trees_rec(posns, len, "", fn, &indpos))
275 return -1;
277 check_updates(active_cache, active_nr);
278 return 0;
281 static int list_tree(unsigned char *sha1)
283 struct tree *tree = parse_tree_indirect(sha1);
284 if (!tree)
285 return -1;
286 object_list_append(&tree->object, &trees);
287 return 0;
290 static int same(struct cache_entry *a, struct cache_entry *b)
292 if (!!a != !!b)
293 return 0;
294 if (!a && !b)
295 return 1;
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)
307 struct stat st;
309 if (!lstat(ce->name, &st)) {
310 unsigned changed = ce_match_stat(ce, &st);
311 if (!changed)
312 return;
313 errno = 0;
315 if (errno == ENOENT)
316 return;
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);
323 if (old) {
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
329 * a match.
331 if (same(old, merge)) {
332 *merge = *old;
333 } else {
334 verify_uptodate(old);
337 merge->ce_flags &= ~htons(CE_STAGEMASK);
338 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
339 return 1;
342 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
344 if (old)
345 verify_uptodate(old);
346 ce->ce_mode = 0;
347 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
348 return 1;
351 static int keep_entry(struct cache_entry *ce)
353 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
354 return 1;
357 #if DBRT_DEBUG
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",
362 label,
363 ntohl(ce->ce_mode),
364 sha1_to_hex(ce->sha1),
365 ce_stage(ce),
366 ce->name);
368 #endif
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];
375 int count;
376 int head_match = 0;
377 int remote_match = 0;
379 int df_conflict_head = 0;
380 int df_conflict_remote = 0;
382 int any_anc_missing = 0;
383 int i;
385 for (i = 1; i < head_idx; i++) {
386 if (!stages[i])
387 any_anc_missing = 1;
390 index = stages[0];
391 head = stages[head_idx];
393 if (head == &df_conflict_entry) {
394 df_conflict_head = 1;
395 head = NULL;
398 if (remote == &df_conflict_entry) {
399 df_conflict_remote = 1;
400 remote = NULL;
403 /* First, if there's a #16 situation, note that to prevent #13
404 * and #14.
406 if (!same(remote, head)) {
407 for (i = 1; i < head_idx; i++) {
408 if (same(stages[i], head)) {
409 head_match = i;
411 if (same(stages[i], remote)) {
412 remote_match = i;
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))
424 reject_merge(index);
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)) {
432 reject_merge(index);
435 if (head) {
436 /* #5ALT, #15 */
437 if (same(head, remote))
438 return merged_entry(head, index);
439 /* #13, #3ALT */
440 if (!df_conflict_remote && remote_match && !head_match)
441 return merged_entry(head, index);
444 /* #1 */
445 if (!head && !remote && any_anc_missing)
446 return 0;
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.
452 if (index) {
453 verify_uptodate(index);
456 /* #2, #3, #4, #6, #7, #9, #11. */
457 count = 0;
458 if (!head_match || !remote_match) {
459 for (i = 1; i < head_idx; i++) {
460 if (stages[i]) {
461 keep_entry(stages[i]);
462 count++;
463 break;
467 #if DBRT_DEBUG
468 else {
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]);
473 #endif
474 if (head) { count += keep_entry(head); }
475 if (remote) { count += keep_entry(remote); }
476 return count;
480 * Two-way merge.
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];
493 if (merge_size != 2)
494 return error("Cannot do a twoway merge of %d trees\n",
495 merge_size);
497 if (current) {
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)) {
509 /* 10 or 11 */
510 return deleted_entry(oldtree, current);
512 else if (oldtree && newtree &&
513 same(current, oldtree) && !same(current, newtree)) {
514 /* 20 or 21 */
515 return merged_entry(newtree, current);
517 else {
518 /* all other failures */
519 if (oldtree)
520 reject_merge(oldtree);
521 if (current)
522 reject_merge(current);
523 if (newtree)
524 reject_merge(newtree);
525 return -1;
528 else if (newtree)
529 return merged_entry(newtree, current);
530 else
531 return deleted_entry(oldtree, current);
535 * One-way merge.
537 * The rule is:
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];
545 if (merge_size != 1)
546 return error("Cannot do a oneway merge of %d trees\n",
547 merge_size);
549 if (!a)
550 return 0;
551 if (old && same(old, a)) {
552 return keep_entry(old);
554 return merged_entry(a, NULL);
557 static int read_cache_unmerged(void)
559 int i, deleted;
560 struct cache_entry **dst;
562 read_cache();
563 dst = active_cache;
564 deleted = 0;
565 for (i = 0; i < active_nr; i++) {
566 struct cache_entry *ce = active_cache[i];
567 if (ce_stage(ce)) {
568 deleted++;
569 continue;
571 if (deleted)
572 *dst = ce;
573 dst++;
575 active_nr -= deleted;
576 return 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());
590 if (newfd < 0)
591 die("unable to create new cachefile");
593 merge = 0;
594 reset = 0;
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")) {
600 update = 1;
601 continue;
604 /* This differs from "-m" in that we'll silently ignore unmerged entries */
605 if (!strcmp(arg, "--reset")) {
606 if (stage || merge)
607 usage(read_tree_usage);
608 reset = 1;
609 merge = 1;
610 stage = 1;
611 read_cache_unmerged();
612 continue;
615 if (!strcmp(arg, "--head")) {
616 head_idx = stage - 1;
617 fn = threeway_merge;
620 /* "-m" stands for "merge", meaning we start in stage 1 */
621 if (!strcmp(arg, "-m")) {
622 if (stage || merge)
623 usage(read_tree_usage);
624 if (read_cache_unmerged())
625 die("you need to resolve your current index first");
626 stage = 1;
627 merge = 1;
628 continue;
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);
635 stage++;
637 if (update && !merge)
638 usage(read_tree_usage);
639 if (merge && !fn) {
640 if (stage < 2)
641 die("just how do you expect me to merge %d trees?", stage-1);
642 switch (stage - 1) {
643 case 1:
644 fn = oneway_merge;
645 break;
646 case 2:
647 fn = twoway_merge;
648 break;
649 case 3:
650 fn = threeway_merge;
651 break;
652 default:
653 fn = threeway_merge;
654 break;
658 if (head_idx < 0) {
659 if (stage - 1 >= 3)
660 head_idx = stage - 2;
661 else
662 head_idx = 1;
665 unpack_trees(fn);
666 if (write_cache(newfd, active_cache, active_nr) ||
667 commit_index_file(&cache_file))
668 die("unable to write new index file");
669 return 0;