Code

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