Code

Add flag to make unpack_trees() not print errors.
[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"
10 #define DBRT_DEBUG 1
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 };
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;
25         while (tree_entry(desc, &one)) {
26                 struct tree_entry_list *entry;
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;
34                 *list_p = entry;
35                 list_p = &entry->next;
36         }
37         return ret;
38 }
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 }
61 static inline void remove_entry(int remove)
62 {
63         if (remove >= 0)
64                 remove_cache_entry_at(remove);
65 }
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;
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;
89                 /* Find the first name in the input. */
91                 first = NULL;
92                 cache_name = NULL;
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                          */
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                 }
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;
144                 pathlen = strlen(first);
145                 ce_size = cache_entry_size(baselen + pathlen);
147                 src = xcalloc(src_size, sizeof(struct cache_entry *));
149                 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
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                 }
158                 for (i = 0; i < len; i++) {
159                         struct cache_entry *ce;
161                         if (!posns[i] ||
162                             (posns[i] != df_conflict_list &&
163                              strcmp(first, posns[i]->name))) {
164                                 continue;
165                         }
167                         if (posns[i] == df_conflict_list) {
168                                 src[i + o->merge] = o->df_conflict_entry;
169                                 continue;
170                         }
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                         }
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;
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);
200                         any_files = 1;
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;
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;
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);
255  leave_directory:
256         return retval;
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)
265         char *cp, *prev;
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;
280                 *cp = 0;
281                 status = rmdir(name);
282                 if (status) {
283                         *cp = '/';
284                         break;
285                 }
286                 prev = cp;
287         }
290 static struct checkout state;
291 static void check_updates(struct cache_entry **src, int nr,
292                         struct unpack_trees_options *o)
294         unsigned cnt = 0, total = 0;
295         struct progress *progress = NULL;
296         char last_symlink[PATH_MAX];
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                 }
305                 progress = start_progress_delay("Checking out files",
306                                                 total, 50, 2);
307                 cnt = 0;
308         }
310         *last_symlink = '\0';
311         while (nr--) {
312                 struct cache_entry *ce = *src++;
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);
332 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
334         struct tree_entry_list **posns;
335         int i;
336         struct tree_entry_list df_conflict_list;
337         static struct cache_entry *dfc;
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;
347         o->merge_size = len;
349         if (!dfc)
350                 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
351         o->df_conflict_entry = dfc;
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);
358                 if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
359                                      o, &df_conflict_list)) {
360                         if (o->gently) {
361                                 discard_cache();
362                                 read_cache();
363                         }
364                         return -1;
365                 }
366         }
368         if (o->trivial_merges_only && o->nontrivial_merge) {
369                 if (o->gently) {
370                         discard_cache();
371                         read_cache();
372                 }
373                 return o->gently ? -1 :
374                         error("Merge requires file-level merging");
375         }
377         check_updates(active_cache, active_nr, o);
378         return 0;
381 /* Here come the merge functions */
383 static int reject_merge(struct cache_entry *ce)
385         return error("Entry '%s' would be overwritten by merge. Cannot merge.",
386                      ce->name);
389 static int same(struct cache_entry *a, struct cache_entry *b)
391         if (!!a != !!b)
392                 return 0;
393         if (!a && !b)
394                 return 1;
395         return a->ce_mode == b->ce_mode &&
396                !hashcmp(a->sha1, b->sha1);
400 /*
401  * When a CE gets turned into an unmerged entry, we
402  * want it to be up-to-date
403  */
404 static int verify_uptodate(struct cache_entry *ce,
405                 struct unpack_trees_options *o)
407         struct stat st;
409         if (o->index_only || o->reset)
410                 return 0;
412         if (!lstat(ce->name, &st)) {
413                 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
414                 if (!changed)
415                         return 0;
416                 /*
417                  * NEEDSWORK: the current default policy is to allow
418                  * submodule to be out of sync wrt the supermodule
419                  * index.  This needs to be tightened later for
420                  * submodules that are marked to be automatically
421                  * checked out.
422                  */
423                 if (S_ISGITLINK(ce->ce_mode))
424                         return 0;
425                 errno = 0;
426         }
427         if (errno == ENOENT)
428                 return 0;
429         return o->gently ? -1 :
430                 error("Entry '%s' not uptodate. Cannot merge.", ce->name);
433 static void invalidate_ce_path(struct cache_entry *ce)
435         if (ce)
436                 cache_tree_invalidate_path(active_cache_tree, ce->name);
439 /*
440  * Check that checking out ce->sha1 in subdir ce->name is not
441  * going to overwrite any working files.
442  *
443  * Currently, git does not checkout subprojects during a superproject
444  * checkout, so it is not going to overwrite anything.
445  */
446 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
447                                       struct unpack_trees_options *o)
449         return 0;
452 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
453                                       struct unpack_trees_options *o)
455         /*
456          * we are about to extract "ce->name"; we would not want to lose
457          * anything in the existing directory there.
458          */
459         int namelen;
460         int pos, i;
461         struct dir_struct d;
462         char *pathbuf;
463         int cnt = 0;
464         unsigned char sha1[20];
466         if (S_ISGITLINK(ce->ce_mode) &&
467             resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
468                 /* If we are not going to update the submodule, then
469                  * we don't care.
470                  */
471                 if (!hashcmp(sha1, ce->sha1))
472                         return 0;
473                 return verify_clean_submodule(ce, action, o);
474         }
476         /*
477          * First let's make sure we do not have a local modification
478          * in that directory.
479          */
480         namelen = strlen(ce->name);
481         pos = cache_name_pos(ce->name, namelen);
482         if (0 <= pos)
483                 return cnt; /* we have it as nondirectory */
484         pos = -pos - 1;
485         for (i = pos; i < active_nr; i++) {
486                 struct cache_entry *ce = active_cache[i];
487                 int len = ce_namelen(ce);
488                 if (len < namelen ||
489                     strncmp(ce->name, ce->name, namelen) ||
490                     ce->name[namelen] != '/')
491                         break;
492                 /*
493                  * ce->name is an entry in the subdirectory.
494                  */
495                 if (!ce_stage(ce)) {
496                         if (verify_uptodate(ce, o))
497                                 return -1;
498                         ce->ce_flags |= CE_REMOVE;
499                 }
500                 cnt++;
501         }
503         /*
504          * Then we need to make sure that we do not lose a locally
505          * present file that is not ignored.
506          */
507         pathbuf = xmalloc(namelen + 2);
508         memcpy(pathbuf, ce->name, namelen);
509         strcpy(pathbuf+namelen, "/");
511         memset(&d, 0, sizeof(d));
512         if (o->dir)
513                 d.exclude_per_dir = o->dir->exclude_per_dir;
514         i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
515         if (i)
516                 return o->gently ? -1 :
517                         error("Updating '%s' would lose untracked files in it",
518                               ce->name);
519         free(pathbuf);
520         return cnt;
523 /*
524  * We do not want to remove or overwrite a working tree file that
525  * is not tracked, unless it is ignored.
526  */
527 static int verify_absent(struct cache_entry *ce, const char *action,
528                          struct unpack_trees_options *o)
530         struct stat st;
532         if (o->index_only || o->reset || !o->update)
533                 return 0;
535         if (has_symlink_leading_path(ce->name, NULL))
536                 return 0;
538         if (!lstat(ce->name, &st)) {
539                 int cnt;
541                 if (o->dir && excluded(o->dir, ce->name))
542                         /*
543                          * ce->name is explicitly excluded, so it is Ok to
544                          * overwrite it.
545                          */
546                         return 0;
547                 if (S_ISDIR(st.st_mode)) {
548                         /*
549                          * We are checking out path "foo" and
550                          * found "foo/." in the working tree.
551                          * This is tricky -- if we have modified
552                          * files that are in "foo/" we would lose
553                          * it.
554                          */
555                         cnt = verify_clean_subdirectory(ce, action, o);
557                         /*
558                          * If this removed entries from the index,
559                          * what that means is:
560                          *
561                          * (1) the caller unpack_trees_rec() saw path/foo
562                          * in the index, and it has not removed it because
563                          * it thinks it is handling 'path' as blob with
564                          * D/F conflict;
565                          * (2) we will return "ok, we placed a merged entry
566                          * in the index" which would cause o->pos to be
567                          * incremented by one;
568                          * (3) however, original o->pos now has 'path/foo'
569                          * marked with "to be removed".
570                          *
571                          * We need to increment it by the number of
572                          * deleted entries here.
573                          */
574                         o->pos += cnt;
575                         return 0;
576                 }
578                 /*
579                  * The previous round may already have decided to
580                  * delete this path, which is in a subdirectory that
581                  * is being replaced with a blob.
582                  */
583                 cnt = cache_name_pos(ce->name, strlen(ce->name));
584                 if (0 <= cnt) {
585                         struct cache_entry *ce = active_cache[cnt];
586                         if (ce->ce_flags & CE_REMOVE)
587                                 return 0;
588                 }
590                 return o->gently ? -1 :
591                         error("Untracked working tree file '%s' "
592                               "would be %s by merge.", ce->name, action);
593         }
594         return 0;
597 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
598                 struct unpack_trees_options *o)
600         merge->ce_flags |= CE_UPDATE;
601         if (old) {
602                 /*
603                  * See if we can re-use the old CE directly?
604                  * That way we get the uptodate stat info.
605                  *
606                  * This also removes the UPDATE flag on
607                  * a match.
608                  */
609                 if (same(old, merge)) {
610                         memcpy(merge, old, offsetof(struct cache_entry, name));
611                 } else {
612                         if (verify_uptodate(old, o))
613                                 return -1;
614                         invalidate_ce_path(old);
615                 }
616         }
617         else {
618                 if (verify_absent(merge, "overwritten", o))
619                         return -1;
620                 invalidate_ce_path(merge);
621         }
623         merge->ce_flags &= ~CE_STAGEMASK;
624         add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
625         return 1;
628 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
629                 struct unpack_trees_options *o)
631         if (old) {
632                 if (verify_uptodate(old, o))
633                         return -1;
634         } else
635                 if (verify_absent(ce, "removed", o))
636                         return -1;
637         ce->ce_flags |= CE_REMOVE;
638         add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
639         invalidate_ce_path(ce);
640         return 1;
643 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
645         add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
646         return 1;
649 #if DBRT_DEBUG
650 static void show_stage_entry(FILE *o,
651                              const char *label, const struct cache_entry *ce)
653         if (!ce)
654                 fprintf(o, "%s (missing)\n", label);
655         else
656                 fprintf(o, "%s%06o %s %d\t%s\n",
657                         label,
658                         ce->ce_mode,
659                         sha1_to_hex(ce->sha1),
660                         ce_stage(ce),
661                         ce->name);
663 #endif
665 int threeway_merge(struct cache_entry **stages,
666                 struct unpack_trees_options *o,
667                 int remove)
669         struct cache_entry *index;
670         struct cache_entry *head;
671         struct cache_entry *remote = stages[o->head_idx + 1];
672         int count;
673         int head_match = 0;
674         int remote_match = 0;
676         int df_conflict_head = 0;
677         int df_conflict_remote = 0;
679         int any_anc_missing = 0;
680         int no_anc_exists = 1;
681         int i;
683         for (i = 1; i < o->head_idx; i++) {
684                 if (!stages[i] || stages[i] == o->df_conflict_entry)
685                         any_anc_missing = 1;
686                 else
687                         no_anc_exists = 0;
688         }
690         index = stages[0];
691         head = stages[o->head_idx];
693         if (head == o->df_conflict_entry) {
694                 df_conflict_head = 1;
695                 head = NULL;
696         }
698         if (remote == o->df_conflict_entry) {
699                 df_conflict_remote = 1;
700                 remote = NULL;
701         }
703         /* First, if there's a #16 situation, note that to prevent #13
704          * and #14.
705          */
706         if (!same(remote, head)) {
707                 for (i = 1; i < o->head_idx; i++) {
708                         if (same(stages[i], head)) {
709                                 head_match = i;
710                         }
711                         if (same(stages[i], remote)) {
712                                 remote_match = i;
713                         }
714                 }
715         }
717         /* We start with cases where the index is allowed to match
718          * something other than the head: #14(ALT) and #2ALT, where it
719          * is permitted to match the result instead.
720          */
721         /* #14, #14ALT, #2ALT */
722         if (remote && !df_conflict_head && head_match && !remote_match) {
723                 if (index && !same(index, remote) && !same(index, head))
724                         return o->gently ? -1 : reject_merge(index);
725                 return merged_entry(remote, index, o);
726         }
727         /*
728          * If we have an entry in the index cache, then we want to
729          * make sure that it matches head.
730          */
731         if (index && !same(index, head)) {
732                 return o->gently ? -1 : reject_merge(index);
733         }
735         if (head) {
736                 /* #5ALT, #15 */
737                 if (same(head, remote))
738                         return merged_entry(head, index, o);
739                 /* #13, #3ALT */
740                 if (!df_conflict_remote && remote_match && !head_match)
741                         return merged_entry(head, index, o);
742         }
744         /* #1 */
745         if (!head && !remote && any_anc_missing) {
746                 remove_entry(remove);
747                 return 0;
748         }
750         /* Under the new "aggressive" rule, we resolve mostly trivial
751          * cases that we historically had git-merge-one-file resolve.
752          */
753         if (o->aggressive) {
754                 int head_deleted = !head && !df_conflict_head;
755                 int remote_deleted = !remote && !df_conflict_remote;
756                 struct cache_entry *ce = NULL;
758                 if (index)
759                         ce = index;
760                 else if (head)
761                         ce = head;
762                 else if (remote)
763                         ce = remote;
764                 else {
765                         for (i = 1; i < o->head_idx; i++) {
766                                 if (stages[i] && stages[i] != o->df_conflict_entry) {
767                                         ce = stages[i];
768                                         break;
769                                 }
770                         }
771                 }
773                 /*
774                  * Deleted in both.
775                  * Deleted in one and unchanged in the other.
776                  */
777                 if ((head_deleted && remote_deleted) ||
778                     (head_deleted && remote && remote_match) ||
779                     (remote_deleted && head && head_match)) {
780                         remove_entry(remove);
781                         if (index)
782                                 return deleted_entry(index, index, o);
783                         else if (ce && !head_deleted) {
784                                 if (verify_absent(ce, "removed", o))
785                                         return -1;
786                         }
787                         return 0;
788                 }
789                 /*
790                  * Added in both, identically.
791                  */
792                 if (no_anc_exists && head && remote && same(head, remote))
793                         return merged_entry(head, index, o);
795         }
797         /* Below are "no merge" cases, which require that the index be
798          * up-to-date to avoid the files getting overwritten with
799          * conflict resolution files.
800          */
801         if (index) {
802                 if (verify_uptodate(index, o))
803                         return -1;
804         }
806         remove_entry(remove);
807         o->nontrivial_merge = 1;
809         /* #2, #3, #4, #6, #7, #9, #10, #11. */
810         count = 0;
811         if (!head_match || !remote_match) {
812                 for (i = 1; i < o->head_idx; i++) {
813                         if (stages[i] && stages[i] != o->df_conflict_entry) {
814                                 keep_entry(stages[i], o);
815                                 count++;
816                                 break;
817                         }
818                 }
819         }
820 #if DBRT_DEBUG
821         else {
822                 fprintf(stderr, "read-tree: warning #16 detected\n");
823                 show_stage_entry(stderr, "head   ", stages[head_match]);
824                 show_stage_entry(stderr, "remote ", stages[remote_match]);
825         }
826 #endif
827         if (head) { count += keep_entry(head, o); }
828         if (remote) { count += keep_entry(remote, o); }
829         return count;
832 /*
833  * Two-way merge.
834  *
835  * The rule is to "carry forward" what is in the index without losing
836  * information across a "fast forward", favoring a successful merge
837  * over a merge failure when it makes sense.  For details of the
838  * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
839  *
840  */
841 int twoway_merge(struct cache_entry **src,
842                 struct unpack_trees_options *o,
843                 int remove)
845         struct cache_entry *current = src[0];
846         struct cache_entry *oldtree = src[1];
847         struct cache_entry *newtree = src[2];
849         if (o->merge_size != 2)
850                 return error("Cannot do a twoway merge of %d trees",
851                              o->merge_size);
853         if (oldtree == o->df_conflict_entry)
854                 oldtree = NULL;
855         if (newtree == o->df_conflict_entry)
856                 newtree = NULL;
858         if (current) {
859                 if ((!oldtree && !newtree) || /* 4 and 5 */
860                     (!oldtree && newtree &&
861                      same(current, newtree)) || /* 6 and 7 */
862                     (oldtree && newtree &&
863                      same(oldtree, newtree)) || /* 14 and 15 */
864                     (oldtree && newtree &&
865                      !same(oldtree, newtree) && /* 18 and 19 */
866                      same(current, newtree))) {
867                         return keep_entry(current, o);
868                 }
869                 else if (oldtree && !newtree && same(current, oldtree)) {
870                         /* 10 or 11 */
871                         remove_entry(remove);
872                         return deleted_entry(oldtree, current, o);
873                 }
874                 else if (oldtree && newtree &&
875                          same(current, oldtree) && !same(current, newtree)) {
876                         /* 20 or 21 */
877                         return merged_entry(newtree, current, o);
878                 }
879                 else {
880                         /* all other failures */
881                         remove_entry(remove);
882                         if (oldtree)
883                                 return o->gently ? -1 : reject_merge(oldtree);
884                         if (current)
885                                 return o->gently ? -1 : reject_merge(current);
886                         if (newtree)
887                                 return o->gently ? -1 : reject_merge(newtree);
888                         return -1;
889                 }
890         }
891         else if (newtree)
892                 return merged_entry(newtree, current, o);
893         remove_entry(remove);
894         return deleted_entry(oldtree, current, o);
897 /*
898  * Bind merge.
899  *
900  * Keep the index entries at stage0, collapse stage1 but make sure
901  * stage0 does not have anything there.
902  */
903 int bind_merge(struct cache_entry **src,
904                 struct unpack_trees_options *o,
905                 int remove)
907         struct cache_entry *old = src[0];
908         struct cache_entry *a = src[1];
910         if (o->merge_size != 1)
911                 return error("Cannot do a bind merge of %d trees\n",
912                              o->merge_size);
913         if (a && old)
914                 return o->gently ? -1 :
915                         error("Entry '%s' overlaps.  Cannot bind.", a->name);
916         if (!a)
917                 return keep_entry(old, o);
918         else
919                 return merged_entry(a, NULL, o);
922 /*
923  * One-way merge.
924  *
925  * The rule is:
926  * - take the stat information from stage0, take the data from stage1
927  */
928 int oneway_merge(struct cache_entry **src,
929                 struct unpack_trees_options *o,
930                 int remove)
932         struct cache_entry *old = src[0];
933         struct cache_entry *a = src[1];
935         if (o->merge_size != 1)
936                 return error("Cannot do a oneway merge of %d trees",
937                              o->merge_size);
939         if (!a) {
940                 remove_entry(remove);
941                 return deleted_entry(old, old, o);
942         }
943         if (old && same(old, a)) {
944                 if (o->reset) {
945                         struct stat st;
946                         if (lstat(old->name, &st) ||
947                             ce_match_stat(old, &st, CE_MATCH_IGNORE_VALID))
948                                 old->ce_flags |= CE_UPDATE;
949                 }
950                 return keep_entry(old, o);
951         }
952         return merged_entry(a, old, o);