Code

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