]> asedeno.scripts.mit.edu Git - git.git/blob - unpack-trees.c
Allow callers of unpack_trees() to handle failure
[git.git] / unpack-trees.c
1 #include "cache.h"
2 #include "dir.h"
3 #include "tree.h"
4 #include "tree-walk.h"
5 #include "cache-tree.h"
6 #include "unpack-trees.h"
7 #include "progress.h"
8 #include "refs.h"
9
10 #define DBRT_DEBUG 1
11
12 struct tree_entry_list {
13         struct tree_entry_list *next;
14         unsigned int mode;
15         const char *name;
16         const unsigned char *sha1;
17 };
18
19 static struct tree_entry_list *create_tree_entry_list(struct tree_desc *desc)
20 {
21         struct name_entry one;
22         struct tree_entry_list *ret = NULL;
23         struct tree_entry_list **list_p = &ret;
24
25         while (tree_entry(desc, &one)) {
26                 struct tree_entry_list *entry;
27
28                 entry = xmalloc(sizeof(struct tree_entry_list));
29                 entry->name = one.path;
30                 entry->sha1 = one.sha1;
31                 entry->mode = one.mode;
32                 entry->next = NULL;
33
34                 *list_p = entry;
35                 list_p = &entry->next;
36         }
37         return ret;
38 }
39
40 static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
41 {
42         int len1 = strlen(name1);
43         int len2 = strlen(name2);
44         int len = len1 < len2 ? len1 : len2;
45         int ret = memcmp(name1, name2, len);
46         unsigned char c1, c2;
47         if (ret)
48                 return ret;
49         c1 = name1[len];
50         c2 = name2[len];
51         if (!c1 && dir1)
52                 c1 = '/';
53         if (!c2 && dir2)
54                 c2 = '/';
55         ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
56         if (c1 && c2 && !ret)
57                 ret = len1 - len2;
58         return ret;
59 }
60
61 static inline void remove_entry(int remove)
62 {
63         if (remove >= 0)
64                 remove_cache_entry_at(remove);
65 }
66
67 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
68                             const char *base, struct unpack_trees_options *o,
69                             struct tree_entry_list *df_conflict_list)
70 {
71         int remove;
72         int baselen = strlen(base);
73         int src_size = len + 1;
74         int retval = 0;
75
76         do {
77                 int i;
78                 const char *first;
79                 int firstdir = 0;
80                 int pathlen;
81                 unsigned ce_size;
82                 struct tree_entry_list **subposns;
83                 struct cache_entry **src;
84                 int any_files = 0;
85                 int any_dirs = 0;
86                 char *cache_name;
87                 int ce_stage;
88
89                 /* Find the first name in the input. */
90
91                 first = NULL;
92                 cache_name = NULL;
93
94                 /* Check the cache */
95                 if (o->merge && o->pos < active_nr) {
96                         /* This is a bit tricky: */
97                         /* If the index has a subdirectory (with
98                          * contents) as the first name, it'll get a
99                          * filename like "foo/bar". But that's after
100                          * "foo", so the entry in trees will get
101                          * handled first, at which point we'll go into
102                          * "foo", and deal with "bar" from the index,
103                          * because the base will be "foo/". The only
104                          * way we can actually have "foo/bar" first of
105                          * all the things is if the trees don't
106                          * contain "foo" at all, in which case we'll
107                          * handle "foo/bar" without going into the
108                          * directory, but that's fine (and will return
109                          * an error anyway, with the added unknown
110                          * file case.
111                          */
112
113                         cache_name = active_cache[o->pos]->name;
114                         if (strlen(cache_name) > baselen &&
115                             !memcmp(cache_name, base, baselen)) {
116                                 cache_name += baselen;
117                                 first = cache_name;
118                         } else {
119                                 cache_name = NULL;
120                         }
121                 }
122
123 #if DBRT_DEBUG > 1
124                 if (first)
125                         printf("index %s\n", first);
126 #endif
127                 for (i = 0; i < len; i++) {
128                         if (!posns[i] || posns[i] == df_conflict_list)
129                                 continue;
130 #if DBRT_DEBUG > 1
131                         printf("%d %s\n", i + 1, posns[i]->name);
132 #endif
133                         if (!first || entcmp(first, firstdir,
134                                              posns[i]->name,
135                                              S_ISDIR(posns[i]->mode)) > 0) {
136                                 first = posns[i]->name;
137                                 firstdir = S_ISDIR(posns[i]->mode);
138                         }
139                 }
140                 /* No name means we're done */
141                 if (!first)
142                         goto leave_directory;
143
144                 pathlen = strlen(first);
145                 ce_size = cache_entry_size(baselen + pathlen);
146
147                 src = xcalloc(src_size, sizeof(struct cache_entry *));
148
149                 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
150
151                 remove = -1;
152                 if (cache_name && !strcmp(cache_name, first)) {
153                         any_files = 1;
154                         src[0] = active_cache[o->pos];
155                         remove = o->pos;
156                 }
157
158                 for (i = 0; i < len; i++) {
159                         struct cache_entry *ce;
160
161                         if (!posns[i] ||
162                             (posns[i] != df_conflict_list &&
163                              strcmp(first, posns[i]->name))) {
164                                 continue;
165                         }
166
167                         if (posns[i] == df_conflict_list) {
168                                 src[i + o->merge] = o->df_conflict_entry;
169                                 continue;
170                         }
171
172                         if (S_ISDIR(posns[i]->mode)) {
173                                 struct tree *tree = lookup_tree(posns[i]->sha1);
174                                 struct tree_desc t;
175                                 any_dirs = 1;
176                                 parse_tree(tree);
177                                 init_tree_desc(&t, tree->buffer, tree->size);
178                                 subposns[i] = create_tree_entry_list(&t);
179                                 posns[i] = posns[i]->next;
180                                 src[i + o->merge] = o->df_conflict_entry;
181                                 continue;
182                         }
183
184                         if (!o->merge)
185                                 ce_stage = 0;
186                         else if (i + 1 < o->head_idx)
187                                 ce_stage = 1;
188                         else if (i + 1 > o->head_idx)
189                                 ce_stage = 3;
190                         else
191                                 ce_stage = 2;
192
193                         ce = xcalloc(1, ce_size);
194                         ce->ce_mode = create_ce_mode(posns[i]->mode);
195                         ce->ce_flags = create_ce_flags(baselen + pathlen,
196                                                        ce_stage);
197                         memcpy(ce->name, base, baselen);
198                         memcpy(ce->name + baselen, first, pathlen + 1);
199
200                         any_files = 1;
201
202                         hashcpy(ce->sha1, posns[i]->sha1);
203                         src[i + o->merge] = ce;
204                         subposns[i] = df_conflict_list;
205                         posns[i] = posns[i]->next;
206                 }
207                 if (any_files) {
208                         if (o->merge) {
209                                 int ret;
210
211 #if DBRT_DEBUG > 1
212                                 printf("%s:\n", first);
213                                 for (i = 0; i < src_size; i++) {
214                                         printf(" %d ", i);
215                                         if (src[i])
216                                                 printf("%s\n", sha1_to_hex(src[i]->sha1));
217                                         else
218                                                 printf("\n");
219                                 }
220 #endif
221                                 ret = o->fn(src, o, remove);
222                                 if (ret < 0)
223                                         return ret;
224
225 #if DBRT_DEBUG > 1
226                                 printf("Added %d entries\n", ret);
227 #endif
228                                 o->pos += ret;
229                         } else {
230                                 remove_entry(remove);
231                                 for (i = 0; i < src_size; i++) {
232                                         if (src[i]) {
233                                                 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
234                                         }
235                                 }
236                         }
237                 }
238                 if (any_dirs) {
239                         char *newbase = xmalloc(baselen + 2 + pathlen);
240                         memcpy(newbase, base, baselen);
241                         memcpy(newbase + baselen, first, pathlen);
242                         newbase[baselen + pathlen] = '/';
243                         newbase[baselen + pathlen + 1] = '\0';
244                         if (unpack_trees_rec(subposns, len, newbase, o,
245                                              df_conflict_list)) {
246                                 retval = -1;
247                                 goto leave_directory;
248                         }
249                         free(newbase);
250                 }
251                 free(subposns);
252                 free(src);
253         } while (1);
254
255  leave_directory:
256         return retval;
257 }
258
259 /* Unlink the last component and attempt to remove leading
260  * directories, in case this unlink is the removal of the
261  * last entry in the directory -- empty directories are removed.
262  */
263 static void unlink_entry(char *name, char *last_symlink)
264 {
265         char *cp, *prev;
266
267         if (has_symlink_leading_path(name, last_symlink))
268                 return;
269         if (unlink(name))
270                 return;
271         prev = NULL;
272         while (1) {
273                 int status;
274                 cp = strrchr(name, '/');
275                 if (prev)
276                         *prev = '/';
277                 if (!cp)
278                         break;
279
280                 *cp = 0;
281                 status = rmdir(name);
282                 if (status) {
283                         *cp = '/';
284                         break;
285                 }
286                 prev = cp;
287         }
288 }
289
290 static struct checkout state;
291 static void check_updates(struct cache_entry **src, int nr,
292                         struct unpack_trees_options *o)
293 {
294         unsigned cnt = 0, total = 0;
295         struct progress *progress = NULL;
296         char last_symlink[PATH_MAX];
297
298         if (o->update && o->verbose_update) {
299                 for (total = cnt = 0; cnt < nr; cnt++) {
300                         struct cache_entry *ce = src[cnt];
301                         if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
302                                 total++;
303                 }
304
305                 progress = start_progress_delay("Checking out files",
306                                                 total, 50, 2);
307                 cnt = 0;
308         }
309
310         *last_symlink = '\0';
311         while (nr--) {
312                 struct cache_entry *ce = *src++;
313
314                 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
315                         display_progress(progress, ++cnt);
316                 if (ce->ce_flags & CE_REMOVE) {
317                         if (o->update)
318                                 unlink_entry(ce->name, last_symlink);
319                         continue;
320                 }
321                 if (ce->ce_flags & CE_UPDATE) {
322                         ce->ce_flags &= ~CE_UPDATE;
323                         if (o->update) {
324                                 checkout_entry(ce, &state, NULL);
325                                 *last_symlink = '\0';
326                         }
327                 }
328         }
329         stop_progress(&progress);
330 }
331
332 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
333 {
334         struct tree_entry_list **posns;
335         int i;
336         struct tree_entry_list df_conflict_list;
337         static struct cache_entry *dfc;
338
339         memset(&df_conflict_list, 0, sizeof(df_conflict_list));
340         df_conflict_list.next = &df_conflict_list;
341         memset(&state, 0, sizeof(state));
342         state.base_dir = "";
343         state.force = 1;
344         state.quiet = 1;
345         state.refresh_cache = 1;
346
347         o->merge_size = len;
348
349         if (!dfc)
350                 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
351         o->df_conflict_entry = dfc;
352
353         if (len) {
354                 posns = xmalloc(len * sizeof(struct tree_entry_list *));
355                 for (i = 0; i < len; i++)
356                         posns[i] = create_tree_entry_list(t+i);
357
358                 if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
359                                      o, &df_conflict_list))
360                         return -1;
361         }
362
363         if (o->trivial_merges_only && o->nontrivial_merge)
364                 return error("Merge requires file-level merging");
365
366         check_updates(active_cache, active_nr, o);
367         return 0;
368 }
369
370 /* Here come the merge functions */
371
372 static int reject_merge(struct cache_entry *ce)
373 {
374         return error("Entry '%s' would be overwritten by merge. Cannot merge.",
375                      ce->name);
376 }
377
378 static int same(struct cache_entry *a, struct cache_entry *b)
379 {
380         if (!!a != !!b)
381                 return 0;
382         if (!a && !b)
383                 return 1;
384         return a->ce_mode == b->ce_mode &&
385                !hashcmp(a->sha1, b->sha1);
386 }
387
388
389 /*
390  * When a CE gets turned into an unmerged entry, we
391  * want it to be up-to-date
392  */
393 static int verify_uptodate(struct cache_entry *ce,
394                 struct unpack_trees_options *o)
395 {
396         struct stat st;
397
398         if (o->index_only || o->reset)
399                 return 0;
400
401         if (!lstat(ce->name, &st)) {
402                 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
403                 if (!changed)
404                         return 0;
405                 /*
406                  * NEEDSWORK: the current default policy is to allow
407                  * submodule to be out of sync wrt the supermodule
408                  * index.  This needs to be tightened later for
409                  * submodules that are marked to be automatically
410                  * checked out.
411                  */
412                 if (S_ISGITLINK(ce->ce_mode))
413                         return 0;
414                 errno = 0;
415         }
416         if (errno == ENOENT)
417                 return 0;
418         return error("Entry '%s' not uptodate. Cannot merge.", ce->name);
419 }
420
421 static void invalidate_ce_path(struct cache_entry *ce)
422 {
423         if (ce)
424                 cache_tree_invalidate_path(active_cache_tree, ce->name);
425 }
426
427 /*
428  * Check that checking out ce->sha1 in subdir ce->name is not
429  * going to overwrite any working files.
430  *
431  * Currently, git does not checkout subprojects during a superproject
432  * checkout, so it is not going to overwrite anything.
433  */
434 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
435                                       struct unpack_trees_options *o)
436 {
437         return 0;
438 }
439
440 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
441                                       struct unpack_trees_options *o)
442 {
443         /*
444          * we are about to extract "ce->name"; we would not want to lose
445          * anything in the existing directory there.
446          */
447         int namelen;
448         int pos, i;
449         struct dir_struct d;
450         char *pathbuf;
451         int cnt = 0;
452         unsigned char sha1[20];
453
454         if (S_ISGITLINK(ce->ce_mode) &&
455             resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
456                 /* If we are not going to update the submodule, then
457                  * we don't care.
458                  */
459                 if (!hashcmp(sha1, ce->sha1))
460                         return 0;
461                 return verify_clean_submodule(ce, action, o);
462         }
463
464         /*
465          * First let's make sure we do not have a local modification
466          * in that directory.
467          */
468         namelen = strlen(ce->name);
469         pos = cache_name_pos(ce->name, namelen);
470         if (0 <= pos)
471                 return cnt; /* we have it as nondirectory */
472         pos = -pos - 1;
473         for (i = pos; i < active_nr; i++) {
474                 struct cache_entry *ce = active_cache[i];
475                 int len = ce_namelen(ce);
476                 if (len < namelen ||
477                     strncmp(ce->name, ce->name, namelen) ||
478                     ce->name[namelen] != '/')
479                         break;
480                 /*
481                  * ce->name is an entry in the subdirectory.
482                  */
483                 if (!ce_stage(ce)) {
484                         if (verify_uptodate(ce, o))
485                                 return -1;
486                         ce->ce_flags |= CE_REMOVE;
487                 }
488                 cnt++;
489         }
490
491         /*
492          * Then we need to make sure that we do not lose a locally
493          * present file that is not ignored.
494          */
495         pathbuf = xmalloc(namelen + 2);
496         memcpy(pathbuf, ce->name, namelen);
497         strcpy(pathbuf+namelen, "/");
498
499         memset(&d, 0, sizeof(d));
500         if (o->dir)
501                 d.exclude_per_dir = o->dir->exclude_per_dir;
502         i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
503         if (i)
504                 return error("Updating '%s' would lose untracked files in it",
505                              ce->name);
506         free(pathbuf);
507         return cnt;
508 }
509
510 /*
511  * We do not want to remove or overwrite a working tree file that
512  * is not tracked, unless it is ignored.
513  */
514 static int verify_absent(struct cache_entry *ce, const char *action,
515                          struct unpack_trees_options *o)
516 {
517         struct stat st;
518
519         if (o->index_only || o->reset || !o->update)
520                 return 0;
521
522         if (has_symlink_leading_path(ce->name, NULL))
523                 return 0;
524
525         if (!lstat(ce->name, &st)) {
526                 int cnt;
527
528                 if (o->dir && excluded(o->dir, ce->name))
529                         /*
530                          * ce->name is explicitly excluded, so it is Ok to
531                          * overwrite it.
532                          */
533                         return 0;
534                 if (S_ISDIR(st.st_mode)) {
535                         /*
536                          * We are checking out path "foo" and
537                          * found "foo/." in the working tree.
538                          * This is tricky -- if we have modified
539                          * files that are in "foo/" we would lose
540                          * it.
541                          */
542                         cnt = verify_clean_subdirectory(ce, action, o);
543
544                         /*
545                          * If this removed entries from the index,
546                          * what that means is:
547                          *
548                          * (1) the caller unpack_trees_rec() saw path/foo
549                          * in the index, and it has not removed it because
550                          * it thinks it is handling 'path' as blob with
551                          * D/F conflict;
552                          * (2) we will return "ok, we placed a merged entry
553                          * in the index" which would cause o->pos to be
554                          * incremented by one;
555                          * (3) however, original o->pos now has 'path/foo'
556                          * marked with "to be removed".
557                          *
558                          * We need to increment it by the number of
559                          * deleted entries here.
560                          */
561                         o->pos += cnt;
562                         return 0;
563                 }
564
565                 /*
566                  * The previous round may already have decided to
567                  * delete this path, which is in a subdirectory that
568                  * is being replaced with a blob.
569                  */
570                 cnt = cache_name_pos(ce->name, strlen(ce->name));
571                 if (0 <= cnt) {
572                         struct cache_entry *ce = active_cache[cnt];
573                         if (ce->ce_flags & CE_REMOVE)
574                                 return 0;
575                 }
576
577                 return error("Untracked working tree file '%s' "
578                              "would be %s by merge.", ce->name, action);
579         }
580         return 0;
581 }
582
583 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
584                 struct unpack_trees_options *o)
585 {
586         merge->ce_flags |= CE_UPDATE;
587         if (old) {
588                 /*
589                  * See if we can re-use the old CE directly?
590                  * That way we get the uptodate stat info.
591                  *
592                  * This also removes the UPDATE flag on
593                  * a match.
594                  */
595                 if (same(old, merge)) {
596                         memcpy(merge, old, offsetof(struct cache_entry, name));
597                 } else {
598                         if (verify_uptodate(old, o))
599                                 return -1;
600                         invalidate_ce_path(old);
601                 }
602         }
603         else {
604                 if (verify_absent(merge, "overwritten", o))
605                         return -1;
606                 invalidate_ce_path(merge);
607         }
608
609         merge->ce_flags &= ~CE_STAGEMASK;
610         add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
611         return 1;
612 }
613
614 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
615                 struct unpack_trees_options *o)
616 {
617         if (old) {
618                 if (verify_uptodate(old, o))
619                         return -1;
620         } else
621                 if (verify_absent(ce, "removed", o))
622                         return -1;
623         ce->ce_flags |= CE_REMOVE;
624         add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
625         invalidate_ce_path(ce);
626         return 1;
627 }
628
629 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
630 {
631         add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
632         return 1;
633 }
634
635 #if DBRT_DEBUG
636 static void show_stage_entry(FILE *o,
637                              const char *label, const struct cache_entry *ce)
638 {
639         if (!ce)
640                 fprintf(o, "%s (missing)\n", label);
641         else
642                 fprintf(o, "%s%06o %s %d\t%s\n",
643                         label,
644                         ce->ce_mode,
645                         sha1_to_hex(ce->sha1),
646                         ce_stage(ce),
647                         ce->name);
648 }
649 #endif
650
651 int threeway_merge(struct cache_entry **stages,
652                 struct unpack_trees_options *o,
653                 int remove)
654 {
655         struct cache_entry *index;
656         struct cache_entry *head;
657         struct cache_entry *remote = stages[o->head_idx + 1];
658         int count;
659         int head_match = 0;
660         int remote_match = 0;
661
662         int df_conflict_head = 0;
663         int df_conflict_remote = 0;
664
665         int any_anc_missing = 0;
666         int no_anc_exists = 1;
667         int i;
668
669         for (i = 1; i < o->head_idx; i++) {
670                 if (!stages[i] || stages[i] == o->df_conflict_entry)
671                         any_anc_missing = 1;
672                 else
673                         no_anc_exists = 0;
674         }
675
676         index = stages[0];
677         head = stages[o->head_idx];
678
679         if (head == o->df_conflict_entry) {
680                 df_conflict_head = 1;
681                 head = NULL;
682         }
683
684         if (remote == o->df_conflict_entry) {
685                 df_conflict_remote = 1;
686                 remote = NULL;
687         }
688
689         /* First, if there's a #16 situation, note that to prevent #13
690          * and #14.
691          */
692         if (!same(remote, head)) {
693                 for (i = 1; i < o->head_idx; i++) {
694                         if (same(stages[i], head)) {
695                                 head_match = i;
696                         }
697                         if (same(stages[i], remote)) {
698                                 remote_match = i;
699                         }
700                 }
701         }
702
703         /* We start with cases where the index is allowed to match
704          * something other than the head: #14(ALT) and #2ALT, where it
705          * is permitted to match the result instead.
706          */
707         /* #14, #14ALT, #2ALT */
708         if (remote && !df_conflict_head && head_match && !remote_match) {
709                 if (index && !same(index, remote) && !same(index, head))
710                         return reject_merge(index);
711                 return merged_entry(remote, index, o);
712         }
713         /*
714          * If we have an entry in the index cache, then we want to
715          * make sure that it matches head.
716          */
717         if (index && !same(index, head)) {
718                 return reject_merge(index);
719         }
720
721         if (head) {
722                 /* #5ALT, #15 */
723                 if (same(head, remote))
724                         return merged_entry(head, index, o);
725                 /* #13, #3ALT */
726                 if (!df_conflict_remote && remote_match && !head_match)
727                         return merged_entry(head, index, o);
728         }
729
730         /* #1 */
731         if (!head && !remote && any_anc_missing) {
732                 remove_entry(remove);
733                 return 0;
734         }
735
736         /* Under the new "aggressive" rule, we resolve mostly trivial
737          * cases that we historically had git-merge-one-file resolve.
738          */
739         if (o->aggressive) {
740                 int head_deleted = !head && !df_conflict_head;
741                 int remote_deleted = !remote && !df_conflict_remote;
742                 struct cache_entry *ce = NULL;
743
744                 if (index)
745                         ce = index;
746                 else if (head)
747                         ce = head;
748                 else if (remote)
749                         ce = remote;
750                 else {
751                         for (i = 1; i < o->head_idx; i++) {
752                                 if (stages[i] && stages[i] != o->df_conflict_entry) {
753                                         ce = stages[i];
754                                         break;
755                                 }
756                         }
757                 }
758
759                 /*
760                  * Deleted in both.
761                  * Deleted in one and unchanged in the other.
762                  */
763                 if ((head_deleted && remote_deleted) ||
764                     (head_deleted && remote && remote_match) ||
765                     (remote_deleted && head && head_match)) {
766                         remove_entry(remove);
767                         if (index)
768                                 return deleted_entry(index, index, o);
769                         else if (ce && !head_deleted) {
770                                 if (verify_absent(ce, "removed", o))
771                                         return -1;
772                         }
773                         return 0;
774                 }
775                 /*
776                  * Added in both, identically.
777                  */
778                 if (no_anc_exists && head && remote && same(head, remote))
779                         return merged_entry(head, index, o);
780
781         }
782
783         /* Below are "no merge" cases, which require that the index be
784          * up-to-date to avoid the files getting overwritten with
785          * conflict resolution files.
786          */
787         if (index) {
788                 if (verify_uptodate(index, o))
789                         return -1;
790         }
791
792         remove_entry(remove);
793         o->nontrivial_merge = 1;
794
795         /* #2, #3, #4, #6, #7, #9, #10, #11. */
796         count = 0;
797         if (!head_match || !remote_match) {
798                 for (i = 1; i < o->head_idx; i++) {
799                         if (stages[i] && stages[i] != o->df_conflict_entry) {
800                                 keep_entry(stages[i], o);
801                                 count++;
802                                 break;
803                         }
804                 }
805         }
806 #if DBRT_DEBUG
807         else {
808                 fprintf(stderr, "read-tree: warning #16 detected\n");
809                 show_stage_entry(stderr, "head   ", stages[head_match]);
810                 show_stage_entry(stderr, "remote ", stages[remote_match]);
811         }
812 #endif
813         if (head) { count += keep_entry(head, o); }
814         if (remote) { count += keep_entry(remote, o); }
815         return count;
816 }
817
818 /*
819  * Two-way merge.
820  *
821  * The rule is to "carry forward" what is in the index without losing
822  * information across a "fast forward", favoring a successful merge
823  * over a merge failure when it makes sense.  For details of the
824  * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
825  *
826  */
827 int twoway_merge(struct cache_entry **src,
828                 struct unpack_trees_options *o,
829                 int remove)
830 {
831         struct cache_entry *current = src[0];
832         struct cache_entry *oldtree = src[1];
833         struct cache_entry *newtree = src[2];
834
835         if (o->merge_size != 2)
836                 return error("Cannot do a twoway merge of %d trees",
837                              o->merge_size);
838
839         if (oldtree == o->df_conflict_entry)
840                 oldtree = NULL;
841         if (newtree == o->df_conflict_entry)
842                 newtree = NULL;
843
844         if (current) {
845                 if ((!oldtree && !newtree) || /* 4 and 5 */
846                     (!oldtree && newtree &&
847                      same(current, newtree)) || /* 6 and 7 */
848                     (oldtree && newtree &&
849                      same(oldtree, newtree)) || /* 14 and 15 */
850                     (oldtree && newtree &&
851                      !same(oldtree, newtree) && /* 18 and 19 */
852                      same(current, newtree))) {
853                         return keep_entry(current, o);
854                 }
855                 else if (oldtree && !newtree && same(current, oldtree)) {
856                         /* 10 or 11 */
857                         remove_entry(remove);
858                         return deleted_entry(oldtree, current, o);
859                 }
860                 else if (oldtree && newtree &&
861                          same(current, oldtree) && !same(current, newtree)) {
862                         /* 20 or 21 */
863                         return merged_entry(newtree, current, o);
864                 }
865                 else {
866                         /* all other failures */
867                         remove_entry(remove);
868                         if (oldtree)
869                                 return reject_merge(oldtree);
870                         if (current)
871                                 return reject_merge(current);
872                         if (newtree)
873                                 return reject_merge(newtree);
874                         return -1;
875                 }
876         }
877         else if (newtree)
878                 return merged_entry(newtree, current, o);
879         remove_entry(remove);
880         return deleted_entry(oldtree, current, o);
881 }
882
883 /*
884  * Bind merge.
885  *
886  * Keep the index entries at stage0, collapse stage1 but make sure
887  * stage0 does not have anything there.
888  */
889 int bind_merge(struct cache_entry **src,
890                 struct unpack_trees_options *o,
891                 int remove)
892 {
893         struct cache_entry *old = src[0];
894         struct cache_entry *a = src[1];
895
896         if (o->merge_size != 1)
897                 return error("Cannot do a bind merge of %d trees\n",
898                              o->merge_size);
899         if (a && old)
900                 return error("Entry '%s' overlaps.  Cannot bind.", a->name);
901         if (!a)
902                 return keep_entry(old, o);
903         else
904                 return merged_entry(a, NULL, o);
905 }
906
907 /*
908  * One-way merge.
909  *
910  * The rule is:
911  * - take the stat information from stage0, take the data from stage1
912  */
913 int oneway_merge(struct cache_entry **src,
914                 struct unpack_trees_options *o,
915                 int remove)
916 {
917         struct cache_entry *old = src[0];
918         struct cache_entry *a = src[1];
919
920         if (o->merge_size != 1)
921                 return error("Cannot do a oneway merge of %d trees",
922                              o->merge_size);
923
924         if (!a) {
925                 remove_entry(remove);
926                 return deleted_entry(old, old, o);
927         }
928         if (old && same(old, a)) {
929                 if (o->reset) {
930                         struct stat st;
931                         if (lstat(old->name, &st) ||
932                             ce_match_stat(old, &st, CE_MATCH_IGNORE_VALID))
933                                 old->ce_flags |= CE_UPDATE;
934                 }
935                 return keep_entry(old, o);
936         }
937         return merged_entry(a, old, o);
938 }