Code

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