Minor fixup to documentation of hooks in git-receive-pack.
[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
9 #define DBRT_DEBUG 1
10
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 };
20
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;
27
28         if (!tree->object.parsed)
29                 parse_tree(tree);
30
31         init_tree_desc(&desc, tree->buffer, tree->size);
32
33         while (tree_entry(&desc, &one)) {
34                 struct tree_entry_list *entry;
35
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;
44
45                 *list_p = entry;
46                 list_p = &entry->next;
47         }
48         return ret;
49 }
50
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 }
71
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;
80
81         if (o->dir)
82                 i_stk = push_exclude_per_directory(o->dir, base, strlen(base));
83
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;
96
97                 /* Find the first name in the input. */
98
99                 first = NULL;
100                 cache_name = NULL;
101
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                          */
120
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                 }
130
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;
151
152                 pathlen = strlen(first);
153                 ce_size = cache_entry_size(baselen + pathlen);
154
155                 src = xcalloc(src_size, sizeof(struct cache_entry *));
156
157                 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
158
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                 }
164
165                 for (i = 0; i < len; i++) {
166                         struct cache_entry *ce;
167
168                         if (!posns[i] ||
169                             (posns[i] != df_conflict_list &&
170                              strcmp(first, posns[i]->name))) {
171                                 continue;
172                         }
173
174                         if (posns[i] == df_conflict_list) {
175                                 src[i + o->merge] = o->df_conflict_entry;
176                                 continue;
177                         }
178
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                         }
188
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;
197
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);
204
205                         any_files = 1;
206
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;
215
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);
227
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);
256
257  leave_directory:
258         if (o->dir)
259                 pop_exclude_per_directory(o->dir, i_stk);
260         return retval;
261 }
262
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)
268 {
269         char *cp, *prev;
270
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;
283
284                 *cp = 0;
285                 status = rmdir(name);
286                 if (status) {
287                         *cp = '/';
288                         break;
289                 }
290                 prev = cp;
291         }
292 }
293
294 static struct checkout state;
295 static void check_updates(struct cache_entry **src, int nr,
296                         struct unpack_trees_options *o)
297 {
298         unsigned short mask = htons(CE_UPDATE);
299         unsigned cnt = 0, total = 0;
300         struct progress progress;
301         char last_symlink[PATH_MAX];
302
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                 }
309
310                 start_progress_delay(&progress, "Checking %u files out...",
311                                      "", total, 50, 2);
312                 cnt = 0;
313         }
314
315         *last_symlink = '\0';
316         while (nr--) {
317                 struct cache_entry *ce = *src++;
318
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);;
337 }
338
339 int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
340 {
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;
347
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;
355
356         o->merge_size = len;
357
358         if (!dfc)
359                 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
360         o->df_conflict_entry = dfc;
361
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         }
372
373         if (o->trivial_merges_only && o->nontrivial_merge)
374                 die("Merge requires file-level merging");
375
376         check_updates(active_cache, active_nr, o);
377         return 0;
378 }
379
380 /* Here come the merge functions */
381
382 static void reject_merge(struct cache_entry *ce)
383 {
384         die("Entry '%s' would be overwritten by merge. Cannot merge.",
385             ce->name);
386 }
387
388 static int same(struct cache_entry *a, struct cache_entry *b)
389 {
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);
396 }
397
398
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)
405 {
406         struct stat st;
407
408         if (o->index_only || o->reset)
409                 return;
410
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 (o->reset) {
418                 ce->ce_flags |= htons(CE_UPDATE);
419                 return;
420         }
421         if (errno == ENOENT)
422                 return;
423         die("Entry '%s' not uptodate. Cannot merge.", ce->name);
424 }
425
426 static void invalidate_ce_path(struct cache_entry *ce)
427 {
428         if (ce)
429                 cache_tree_invalidate_path(active_cache_tree, ce->name);
430 }
431
432 static int verify_clean_subdirectory(const char *path, const char *action,
433                                       struct unpack_trees_options *o)
434 {
435         /*
436          * we are about to extract "path"; we would not want to lose
437          * anything in the existing directory there.
438          */
439         int namelen;
440         int pos, i;
441         struct dir_struct d;
442         char *pathbuf;
443         int cnt = 0;
444
445         /*
446          * First let's make sure we do not have a local modification
447          * in that directory.
448          */
449         namelen = strlen(path);
450         pos = cache_name_pos(path, namelen);
451         if (0 <= pos)
452                 return cnt; /* we have it as nondirectory */
453         pos = -pos - 1;
454         for (i = pos; i < active_nr; i++) {
455                 struct cache_entry *ce = active_cache[i];
456                 int len = ce_namelen(ce);
457                 if (len < namelen ||
458                     strncmp(path, ce->name, namelen) ||
459                     ce->name[namelen] != '/')
460                         break;
461                 /*
462                  * ce->name is an entry in the subdirectory.
463                  */
464                 if (!ce_stage(ce)) {
465                         verify_uptodate(ce, o);
466                         ce->ce_mode = 0;
467                 }
468                 cnt++;
469         }
470
471         /*
472          * Then we need to make sure that we do not lose a locally
473          * present file that is not ignored.
474          */
475         pathbuf = xmalloc(namelen + 2);
476         memcpy(pathbuf, path, namelen);
477         strcpy(pathbuf+namelen, "/");
478
479         memset(&d, 0, sizeof(d));
480         if (o->dir)
481                 d.exclude_per_dir = o->dir->exclude_per_dir;
482         i = read_directory(&d, path, pathbuf, namelen+1, NULL);
483         if (i)
484                 die("Updating '%s' would lose untracked files in it",
485                     path);
486         free(pathbuf);
487         return cnt;
488 }
489
490 /*
491  * We do not want to remove or overwrite a working tree file that
492  * is not tracked, unless it is ignored.
493  */
494 static void verify_absent(const char *path, const char *action,
495                 struct unpack_trees_options *o)
496 {
497         struct stat st;
498
499         if (o->index_only || o->reset || !o->update)
500                 return;
501
502         if (!lstat(path, &st)) {
503                 int cnt;
504
505                 if (o->dir && excluded(o->dir, path))
506                         /*
507                          * path is explicitly excluded, so it is Ok to
508                          * overwrite it.
509                          */
510                         return;
511                 if (S_ISDIR(st.st_mode)) {
512                         /*
513                          * We are checking out path "foo" and
514                          * found "foo/." in the working tree.
515                          * This is tricky -- if we have modified
516                          * files that are in "foo/" we would lose
517                          * it.
518                          */
519                         cnt = verify_clean_subdirectory(path, action, o);
520
521                         /*
522                          * If this removed entries from the index,
523                          * what that means is:
524                          *
525                          * (1) the caller unpack_trees_rec() saw path/foo
526                          * in the index, and it has not removed it because
527                          * it thinks it is handling 'path' as blob with
528                          * D/F conflict;
529                          * (2) we will return "ok, we placed a merged entry
530                          * in the index" which would cause o->pos to be
531                          * incremented by one;
532                          * (3) however, original o->pos now has 'path/foo'
533                          * marked with "to be removed".
534                          *
535                          * We need to increment it by the number of
536                          * deleted entries here.
537                          */
538                         o->pos += cnt;
539                         return;
540                 }
541
542                 /*
543                  * The previous round may already have decided to
544                  * delete this path, which is in a subdirectory that
545                  * is being replaced with a blob.
546                  */
547                 cnt = cache_name_pos(path, strlen(path));
548                 if (0 <= cnt) {
549                         struct cache_entry *ce = active_cache[cnt];
550                         if (!ce_stage(ce) && !ce->ce_mode)
551                                 return;
552                 }
553
554                 die("Untracked working tree file '%s' "
555                     "would be %s by merge.", path, action);
556         }
557 }
558
559 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
560                 struct unpack_trees_options *o)
561 {
562         merge->ce_flags |= htons(CE_UPDATE);
563         if (old) {
564                 /*
565                  * See if we can re-use the old CE directly?
566                  * That way we get the uptodate stat info.
567                  *
568                  * This also removes the UPDATE flag on
569                  * a match.
570                  */
571                 if (same(old, merge)) {
572                         *merge = *old;
573                 } else {
574                         verify_uptodate(old, o);
575                         invalidate_ce_path(old);
576                 }
577         }
578         else {
579                 verify_absent(merge->name, "overwritten", o);
580                 invalidate_ce_path(merge);
581         }
582
583         merge->ce_flags &= ~htons(CE_STAGEMASK);
584         add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
585         return 1;
586 }
587
588 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
589                 struct unpack_trees_options *o)
590 {
591         if (old)
592                 verify_uptodate(old, o);
593         else
594                 verify_absent(ce->name, "removed", o);
595         ce->ce_mode = 0;
596         add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
597         invalidate_ce_path(ce);
598         return 1;
599 }
600
601 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
602 {
603         add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
604         return 1;
605 }
606
607 #if DBRT_DEBUG
608 static void show_stage_entry(FILE *o,
609                              const char *label, const struct cache_entry *ce)
610 {
611         if (!ce)
612                 fprintf(o, "%s (missing)\n", label);
613         else
614                 fprintf(o, "%s%06o %s %d\t%s\n",
615                         label,
616                         ntohl(ce->ce_mode),
617                         sha1_to_hex(ce->sha1),
618                         ce_stage(ce),
619                         ce->name);
620 }
621 #endif
622
623 int threeway_merge(struct cache_entry **stages,
624                 struct unpack_trees_options *o)
625 {
626         struct cache_entry *index;
627         struct cache_entry *head;
628         struct cache_entry *remote = stages[o->head_idx + 1];
629         int count;
630         int head_match = 0;
631         int remote_match = 0;
632
633         int df_conflict_head = 0;
634         int df_conflict_remote = 0;
635
636         int any_anc_missing = 0;
637         int no_anc_exists = 1;
638         int i;
639
640         for (i = 1; i < o->head_idx; i++) {
641                 if (!stages[i] || stages[i] == o->df_conflict_entry)
642                         any_anc_missing = 1;
643                 else
644                         no_anc_exists = 0;
645         }
646
647         index = stages[0];
648         head = stages[o->head_idx];
649
650         if (head == o->df_conflict_entry) {
651                 df_conflict_head = 1;
652                 head = NULL;
653         }
654
655         if (remote == o->df_conflict_entry) {
656                 df_conflict_remote = 1;
657                 remote = NULL;
658         }
659
660         /* First, if there's a #16 situation, note that to prevent #13
661          * and #14.
662          */
663         if (!same(remote, head)) {
664                 for (i = 1; i < o->head_idx; i++) {
665                         if (same(stages[i], head)) {
666                                 head_match = i;
667                         }
668                         if (same(stages[i], remote)) {
669                                 remote_match = i;
670                         }
671                 }
672         }
673
674         /* We start with cases where the index is allowed to match
675          * something other than the head: #14(ALT) and #2ALT, where it
676          * is permitted to match the result instead.
677          */
678         /* #14, #14ALT, #2ALT */
679         if (remote && !df_conflict_head && head_match && !remote_match) {
680                 if (index && !same(index, remote) && !same(index, head))
681                         reject_merge(index);
682                 return merged_entry(remote, index, o);
683         }
684         /*
685          * If we have an entry in the index cache, then we want to
686          * make sure that it matches head.
687          */
688         if (index && !same(index, head)) {
689                 reject_merge(index);
690         }
691
692         if (head) {
693                 /* #5ALT, #15 */
694                 if (same(head, remote))
695                         return merged_entry(head, index, o);
696                 /* #13, #3ALT */
697                 if (!df_conflict_remote && remote_match && !head_match)
698                         return merged_entry(head, index, o);
699         }
700
701         /* #1 */
702         if (!head && !remote && any_anc_missing)
703                 return 0;
704
705         /* Under the new "aggressive" rule, we resolve mostly trivial
706          * cases that we historically had git-merge-one-file resolve.
707          */
708         if (o->aggressive) {
709                 int head_deleted = !head && !df_conflict_head;
710                 int remote_deleted = !remote && !df_conflict_remote;
711                 const char *path = NULL;
712
713                 if (index)
714                         path = index->name;
715                 else if (head)
716                         path = head->name;
717                 else if (remote)
718                         path = remote->name;
719                 else {
720                         for (i = 1; i < o->head_idx; i++) {
721                                 if (stages[i] && stages[i] != o->df_conflict_entry) {
722                                         path = stages[i]->name;
723                                         break;
724                                 }
725                         }
726                 }
727
728                 /*
729                  * Deleted in both.
730                  * Deleted in one and unchanged in the other.
731                  */
732                 if ((head_deleted && remote_deleted) ||
733                     (head_deleted && remote && remote_match) ||
734                     (remote_deleted && head && head_match)) {
735                         if (index)
736                                 return deleted_entry(index, index, o);
737                         else if (path && !head_deleted)
738                                 verify_absent(path, "removed", o);
739                         return 0;
740                 }
741                 /*
742                  * Added in both, identically.
743                  */
744                 if (no_anc_exists && head && remote && same(head, remote))
745                         return merged_entry(head, index, o);
746
747         }
748
749         /* Below are "no merge" cases, which require that the index be
750          * up-to-date to avoid the files getting overwritten with
751          * conflict resolution files.
752          */
753         if (index) {
754                 verify_uptodate(index, o);
755         }
756
757         o->nontrivial_merge = 1;
758
759         /* #2, #3, #4, #6, #7, #9, #10, #11. */
760         count = 0;
761         if (!head_match || !remote_match) {
762                 for (i = 1; i < o->head_idx; i++) {
763                         if (stages[i] && stages[i] != o->df_conflict_entry) {
764                                 keep_entry(stages[i], o);
765                                 count++;
766                                 break;
767                         }
768                 }
769         }
770 #if DBRT_DEBUG
771         else {
772                 fprintf(stderr, "read-tree: warning #16 detected\n");
773                 show_stage_entry(stderr, "head   ", stages[head_match]);
774                 show_stage_entry(stderr, "remote ", stages[remote_match]);
775         }
776 #endif
777         if (head) { count += keep_entry(head, o); }
778         if (remote) { count += keep_entry(remote, o); }
779         return count;
780 }
781
782 /*
783  * Two-way merge.
784  *
785  * The rule is to "carry forward" what is in the index without losing
786  * information across a "fast forward", favoring a successful merge
787  * over a merge failure when it makes sense.  For details of the
788  * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
789  *
790  */
791 int twoway_merge(struct cache_entry **src,
792                 struct unpack_trees_options *o)
793 {
794         struct cache_entry *current = src[0];
795         struct cache_entry *oldtree = src[1];
796         struct cache_entry *newtree = src[2];
797
798         if (o->merge_size != 2)
799                 return error("Cannot do a twoway merge of %d trees",
800                              o->merge_size);
801
802         if (oldtree == o->df_conflict_entry)
803                 oldtree = NULL;
804         if (newtree == o->df_conflict_entry)
805                 newtree = NULL;
806
807         if (current) {
808                 if ((!oldtree && !newtree) || /* 4 and 5 */
809                     (!oldtree && newtree &&
810                      same(current, newtree)) || /* 6 and 7 */
811                     (oldtree && newtree &&
812                      same(oldtree, newtree)) || /* 14 and 15 */
813                     (oldtree && newtree &&
814                      !same(oldtree, newtree) && /* 18 and 19 */
815                      same(current, newtree))) {
816                         return keep_entry(current, o);
817                 }
818                 else if (oldtree && !newtree && same(current, oldtree)) {
819                         /* 10 or 11 */
820                         return deleted_entry(oldtree, current, o);
821                 }
822                 else if (oldtree && newtree &&
823                          same(current, oldtree) && !same(current, newtree)) {
824                         /* 20 or 21 */
825                         return merged_entry(newtree, current, o);
826                 }
827                 else {
828                         /* all other failures */
829                         if (oldtree)
830                                 reject_merge(oldtree);
831                         if (current)
832                                 reject_merge(current);
833                         if (newtree)
834                                 reject_merge(newtree);
835                         return -1;
836                 }
837         }
838         else if (newtree)
839                 return merged_entry(newtree, current, o);
840         else
841                 return deleted_entry(oldtree, current, o);
842 }
843
844 /*
845  * Bind merge.
846  *
847  * Keep the index entries at stage0, collapse stage1 but make sure
848  * stage0 does not have anything there.
849  */
850 int bind_merge(struct cache_entry **src,
851                 struct unpack_trees_options *o)
852 {
853         struct cache_entry *old = src[0];
854         struct cache_entry *a = src[1];
855
856         if (o->merge_size != 1)
857                 return error("Cannot do a bind merge of %d trees\n",
858                              o->merge_size);
859         if (a && old)
860                 die("Entry '%s' overlaps.  Cannot bind.", a->name);
861         if (!a)
862                 return keep_entry(old, o);
863         else
864                 return merged_entry(a, NULL, o);
865 }
866
867 /*
868  * One-way merge.
869  *
870  * The rule is:
871  * - take the stat information from stage0, take the data from stage1
872  */
873 int oneway_merge(struct cache_entry **src,
874                 struct unpack_trees_options *o)
875 {
876         struct cache_entry *old = src[0];
877         struct cache_entry *a = src[1];
878
879         if (o->merge_size != 1)
880                 return error("Cannot do a oneway merge of %d trees",
881                              o->merge_size);
882
883         if (!a)
884                 return deleted_entry(old, old, o);
885         if (old && same(old, a)) {
886                 if (o->reset) {
887                         struct stat st;
888                         if (lstat(old->name, &st) ||
889                             ce_match_stat(old, &st, 1))
890                                 old->ce_flags |= htons(CE_UPDATE);
891                 }
892                 return keep_entry(old, o);
893         }
894         return merged_entry(a, old, o);
895 }