Code

5a0f0382b8fac910f71eddfc77a42058c67da576
[git.git] / unpack-trees.c
1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
2 #include "cache.h"
3 #include "dir.h"
4 #include "tree.h"
5 #include "tree-walk.h"
6 #include "cache-tree.h"
7 #include "unpack-trees.h"
8 #include "progress.h"
9 #include "refs.h"
11 static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
12         unsigned int set, unsigned int clear)
13 {
14         unsigned int size = ce_size(ce);
15         struct cache_entry *new = xmalloc(size);
17         clear |= CE_HASHED | CE_UNHASHED;
19         memcpy(new, ce, size);
20         new->next = NULL;
21         new->ce_flags = (new->ce_flags & ~clear) | set;
22         add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|ADD_CACHE_SKIP_DFCHECK);
23 }
25 /* Unlink the last component and attempt to remove leading
26  * directories, in case this unlink is the removal of the
27  * last entry in the directory -- empty directories are removed.
28  */
29 static void unlink_entry(char *name, char *last_symlink)
30 {
31         char *cp, *prev;
33         if (has_symlink_leading_path(name, last_symlink))
34                 return;
35         if (unlink(name))
36                 return;
37         prev = NULL;
38         while (1) {
39                 int status;
40                 cp = strrchr(name, '/');
41                 if (prev)
42                         *prev = '/';
43                 if (!cp)
44                         break;
46                 *cp = 0;
47                 status = rmdir(name);
48                 if (status) {
49                         *cp = '/';
50                         break;
51                 }
52                 prev = cp;
53         }
54 }
56 static struct checkout state;
57 static void check_updates(struct unpack_trees_options *o)
58 {
59         unsigned cnt = 0, total = 0;
60         struct progress *progress = NULL;
61         char last_symlink[PATH_MAX];
62         struct index_state *index = &o->result;
63         int i;
65         if (o->update && o->verbose_update) {
66                 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
67                         struct cache_entry *ce = index->cache[cnt];
68                         if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
69                                 total++;
70                 }
72                 progress = start_progress_delay("Checking out files",
73                                                 total, 50, 1);
74                 cnt = 0;
75         }
77         *last_symlink = '\0';
78         for (i = 0; i < index->cache_nr; i++) {
79                 struct cache_entry *ce = index->cache[i];
81                 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
82                         display_progress(progress, ++cnt);
83                 if (ce->ce_flags & CE_REMOVE) {
84                         if (o->update)
85                                 unlink_entry(ce->name, last_symlink);
86                         remove_index_entry_at(&o->result, i);
87                         i--;
88                         continue;
89                 }
90                 if (ce->ce_flags & CE_UPDATE) {
91                         ce->ce_flags &= ~CE_UPDATE;
92                         if (o->update) {
93                                 checkout_entry(ce, &state, NULL);
94                                 *last_symlink = '\0';
95                         }
96                 }
97         }
98         stop_progress(&progress);
99 }
101 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
103         int ret = o->fn(src, o);
104         if (ret > 0)
105                 ret = 0;
106         return ret;
109 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
111         struct cache_entry *src[5] = { ce, };
113         o->pos++;
114         if (ce_stage(ce)) {
115                 if (o->skip_unmerged) {
116                         add_entry(o, ce, 0, 0);
117                         return 0;
118                 }
119                 return 0;
120         }
121         return call_unpack_fn(src, o);
124 int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
126         int i;
127         struct tree_desc t[3];
128         struct traverse_info newinfo;
129         struct name_entry *p;
131         p = names;
132         while (!p->mode)
133                 p++;
135         newinfo = *info;
136         newinfo.prev = info;
137         newinfo.name = *p;
138         newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
139         newinfo.conflicts |= df_conflicts;
141         for (i = 0; i < n; i++, dirmask >>= 1) {
142                 const unsigned char *sha1 = NULL;
143                 if (dirmask & 1)
144                         sha1 = names[i].sha1;
145                 fill_tree_descriptor(t+i, sha1);
146         }
147         return traverse_trees(n, t, &newinfo);
150 /*
151  * Compare the traverse-path to the cache entry without actually
152  * having to generate the textual representation of the traverse
153  * path.
154  *
155  * NOTE! This *only* compares up to the size of the traverse path
156  * itself - the caller needs to do the final check for the cache
157  * entry having more data at the end!
158  */
159 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
161         int len, pathlen, ce_len;
162         const char *ce_name;
164         if (info->prev) {
165                 int cmp = do_compare_entry(ce, info->prev, &info->name);
166                 if (cmp)
167                         return cmp;
168         }
169         pathlen = info->pathlen;
170         ce_len = ce_namelen(ce);
172         /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
173         if (ce_len < pathlen)
174                 return -1;
176         ce_len -= pathlen;
177         ce_name = ce->name + pathlen;
179         len = tree_entry_len(n->path, n->sha1);
180         return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
183 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
185         int cmp = do_compare_entry(ce, info, n);
186         if (cmp)
187                 return cmp;
189         /*
190          * Even if the beginning compared identically, the ce should
191          * compare as bigger than a directory leading up to it!
192          */
193         return ce_namelen(ce) > traverse_path_len(info, n);
196 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
198         int len = traverse_path_len(info, n);
199         struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
201         ce->ce_mode = create_ce_mode(n->mode);
202         ce->ce_flags = create_ce_flags(len, stage);
203         hashcpy(ce->sha1, n->sha1);
204         make_traverse_path(ce->name, info, n);
206         return ce;
209 static int unpack_nondirectories(int n, unsigned long mask, unsigned long dirmask, struct cache_entry *src[5],
210         const struct name_entry *names, const struct traverse_info *info)
212         int i;
213         struct unpack_trees_options *o = info->data;
214         unsigned long conflicts;
216         /* Do we have *only* directories? Nothing to do */
217         if (mask == dirmask && !src[0])
218                 return 0;
220         conflicts = info->conflicts;
221         if (o->merge)
222                 conflicts >>= 1;
223         conflicts |= dirmask;
225         /*
226          * Ok, we've filled in up to any potential index entry in src[0],
227          * now do the rest.
228          */
229         for (i = 0; i < n; i++) {
230                 int stage;
231                 unsigned int bit = 1ul << i;
232                 if (conflicts & bit) {
233                         src[i + o->merge] = o->df_conflict_entry;
234                         continue;
235                 }
236                 if (!(mask & bit))
237                         continue;
238                 if (!o->merge)
239                         stage = 0;
240                 else if (i + 1 < o->head_idx)
241                         stage = 1;
242                 else if (i + 1 > o->head_idx)
243                         stage = 3;
244                 else
245                         stage = 2;
246                 src[i + o->merge] = create_ce_entry(info, names + i, stage);
247         }
249         if (o->merge)
250                 return call_unpack_fn(src, o);
252         n += o->merge;
253         for (i = 0; i < n; i++)
254                 add_entry(o, src[i], 0, 0);
255         return 0;
258 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
260         struct cache_entry *src[5] = { NULL, };
261         struct unpack_trees_options *o = info->data;
262         const struct name_entry *p = names;
264         /* Find first entry with a real name (we could use "mask" too) */
265         while (!p->mode)
266                 p++;
268         /* Are we supposed to look at the index too? */
269         if (o->merge) {
270                 while (o->pos < o->src_index->cache_nr) {
271                         struct cache_entry *ce = o->src_index->cache[o->pos];
272                         int cmp = compare_entry(ce, info, p);
273                         if (cmp < 0) {
274                                 if (unpack_index_entry(ce, o) < 0)
275                                         return -1;
276                                 continue;
277                         }
278                         if (!cmp) {
279                                 o->pos++;
280                                 if (ce_stage(ce)) {
281                                         /*
282                                          * If we skip unmerged index entries, we'll skip this
283                                          * entry *and* the tree entries associated with it!
284                                          */
285                                         if (o->skip_unmerged) {
286                                                 add_entry(o, ce, 0, 0);
287                                                 return mask;
288                                         }
289                                         continue;
290                                 }
291                                 src[0] = ce;
292                         }
293                         break;
294                 }
295         }
297         if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
298                 return -1;
300         /* Now handle any directories.. */
301         if (dirmask) {
302                 unsigned long conflicts = mask & ~dirmask;
303                 if (o->merge) {
304                         conflicts <<= 1;
305                         if (src[0])
306                                 conflicts |= 1;
307                 }
308                 if (traverse_trees_recursive(n, dirmask, conflicts,
309                                              names, info) < 0)
310                         return -1;
311                 return mask;
312         }
314         return mask;
317 static int unpack_failed(struct unpack_trees_options *o, const char *message)
319         discard_index(&o->result);
320         if (!o->gently) {
321                 if (message)
322                         return error(message);
323                 return -1;
324         }
325         return -1;
328 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
330         static struct cache_entry *dfc;
332         if (len > 4)
333                 die("unpack_trees takes at most four trees");
334         memset(&state, 0, sizeof(state));
335         state.base_dir = "";
336         state.force = 1;
337         state.quiet = 1;
338         state.refresh_cache = 1;
340         memset(&o->result, 0, sizeof(o->result));
341         o->merge_size = len;
343         if (!dfc)
344                 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
345         o->df_conflict_entry = dfc;
347         if (len) {
348                 const char *prefix = o->prefix ? o->prefix : "";
349                 struct traverse_info info;
351                 setup_traverse_info(&info, prefix);
352                 info.fn = unpack_callback;
353                 info.data = o;
355                 if (traverse_trees(len, t, &info) < 0)
356                         return unpack_failed(o, NULL);
357         }
359         /* Any left-over entries in the index? */
360         if (o->merge) {
361                 while (o->pos < o->src_index->cache_nr) {
362                         struct cache_entry *ce = o->src_index->cache[o->pos];
363                         if (unpack_index_entry(ce, o) < 0)
364                                 return unpack_failed(o, NULL);
365                 }
366         }
368         if (o->trivial_merges_only && o->nontrivial_merge)
369                 return unpack_failed(o, "Merge requires file-level merging");
371         o->src_index = NULL;
372         check_updates(o);
373         if (o->dst_index)
374                 *o->dst_index = o->result;
375         return 0;
378 /* Here come the merge functions */
380 static int reject_merge(struct cache_entry *ce)
382         return error("Entry '%s' would be overwritten by merge. Cannot merge.",
383                      ce->name);
386 static int same(struct cache_entry *a, struct cache_entry *b)
388         if (!!a != !!b)
389                 return 0;
390         if (!a && !b)
391                 return 1;
392         return a->ce_mode == b->ce_mode &&
393                !hashcmp(a->sha1, b->sha1);
397 /*
398  * When a CE gets turned into an unmerged entry, we
399  * want it to be up-to-date
400  */
401 static int verify_uptodate(struct cache_entry *ce,
402                 struct unpack_trees_options *o)
404         struct stat st;
406         if (o->index_only || o->reset)
407                 return 0;
409         if (!lstat(ce->name, &st)) {
410                 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
411                 if (!changed)
412                         return 0;
413                 /*
414                  * NEEDSWORK: the current default policy is to allow
415                  * submodule to be out of sync wrt the supermodule
416                  * index.  This needs to be tightened later for
417                  * submodules that are marked to be automatically
418                  * checked out.
419                  */
420                 if (S_ISGITLINK(ce->ce_mode))
421                         return 0;
422                 errno = 0;
423         }
424         if (errno == ENOENT)
425                 return 0;
426         return o->gently ? -1 :
427                 error("Entry '%s' not uptodate. Cannot merge.", ce->name);
430 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
432         if (ce)
433                 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
436 /*
437  * Check that checking out ce->sha1 in subdir ce->name is not
438  * going to overwrite any working files.
439  *
440  * Currently, git does not checkout subprojects during a superproject
441  * checkout, so it is not going to overwrite anything.
442  */
443 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
444                                       struct unpack_trees_options *o)
446         return 0;
449 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
450                                       struct unpack_trees_options *o)
452         /*
453          * we are about to extract "ce->name"; we would not want to lose
454          * anything in the existing directory there.
455          */
456         int namelen;
457         int pos, i;
458         struct dir_struct d;
459         char *pathbuf;
460         int cnt = 0;
461         unsigned char sha1[20];
463         if (S_ISGITLINK(ce->ce_mode) &&
464             resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
465                 /* If we are not going to update the submodule, then
466                  * we don't care.
467                  */
468                 if (!hashcmp(sha1, ce->sha1))
469                         return 0;
470                 return verify_clean_submodule(ce, action, o);
471         }
473         /*
474          * First let's make sure we do not have a local modification
475          * in that directory.
476          */
477         namelen = strlen(ce->name);
478         pos = index_name_pos(o->src_index, ce->name, namelen);
479         if (0 <= pos)
480                 return cnt; /* we have it as nondirectory */
481         pos = -pos - 1;
482         for (i = pos; i < o->src_index->cache_nr; i++) {
483                 struct cache_entry *ce = o->src_index->cache[i];
484                 int len = ce_namelen(ce);
485                 if (len < namelen ||
486                     strncmp(ce->name, ce->name, namelen) ||
487                     ce->name[namelen] != '/')
488                         break;
489                 /*
490                  * ce->name is an entry in the subdirectory.
491                  */
492                 if (!ce_stage(ce)) {
493                         if (verify_uptodate(ce, o))
494                                 return -1;
495                         add_entry(o, ce, CE_REMOVE, 0);
496                 }
497                 cnt++;
498         }
500         /*
501          * Then we need to make sure that we do not lose a locally
502          * present file that is not ignored.
503          */
504         pathbuf = xmalloc(namelen + 2);
505         memcpy(pathbuf, ce->name, namelen);
506         strcpy(pathbuf+namelen, "/");
508         memset(&d, 0, sizeof(d));
509         if (o->dir)
510                 d.exclude_per_dir = o->dir->exclude_per_dir;
511         i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
512         if (i)
513                 return o->gently ? -1 :
514                         error("Updating '%s' would lose untracked files in it",
515                               ce->name);
516         free(pathbuf);
517         return cnt;
520 /*
521  * We do not want to remove or overwrite a working tree file that
522  * is not tracked, unless it is ignored.
523  */
524 static int verify_absent(struct cache_entry *ce, const char *action,
525                          struct unpack_trees_options *o)
527         struct stat st;
529         if (o->index_only || o->reset || !o->update)
530                 return 0;
532         if (has_symlink_leading_path(ce->name, NULL))
533                 return 0;
535         if (!lstat(ce->name, &st)) {
536                 int cnt;
537                 int dtype = ce_to_dtype(ce);
539                 if (o->dir && excluded(o->dir, ce->name, &dtype))
540                         /*
541                          * ce->name is explicitly excluded, so it is Ok to
542                          * overwrite it.
543                          */
544                         return 0;
545                 if (S_ISDIR(st.st_mode)) {
546                         /*
547                          * We are checking out path "foo" and
548                          * found "foo/." in the working tree.
549                          * This is tricky -- if we have modified
550                          * files that are in "foo/" we would lose
551                          * it.
552                          */
553                         cnt = verify_clean_subdirectory(ce, action, o);
555                         /*
556                          * If this removed entries from the index,
557                          * what that means is:
558                          *
559                          * (1) the caller unpack_trees_rec() saw path/foo
560                          * in the index, and it has not removed it because
561                          * it thinks it is handling 'path' as blob with
562                          * D/F conflict;
563                          * (2) we will return "ok, we placed a merged entry
564                          * in the index" which would cause o->pos to be
565                          * incremented by one;
566                          * (3) however, original o->pos now has 'path/foo'
567                          * marked with "to be removed".
568                          *
569                          * We need to increment it by the number of
570                          * deleted entries here.
571                          */
572                         o->pos += cnt;
573                         return 0;
574                 }
576                 /*
577                  * The previous round may already have decided to
578                  * delete this path, which is in a subdirectory that
579                  * is being replaced with a blob.
580                  */
581                 cnt = index_name_pos(&o->result, ce->name, strlen(ce->name));
582                 if (0 <= cnt) {
583                         struct cache_entry *ce = o->result.cache[cnt];
584                         if (ce->ce_flags & CE_REMOVE)
585                                 return 0;
586                 }
588                 return o->gently ? -1 :
589                         error("Untracked working tree file '%s' "
590                               "would be %s by merge.", ce->name, action);
591         }
592         return 0;
595 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
596                 struct unpack_trees_options *o)
598         if (old) {
599                 /*
600                  * See if we can re-use the old CE directly?
601                  * That way we get the uptodate stat info.
602                  *
603                  * This also removes the UPDATE flag on
604                  * a match.
605                  */
606                 if (same(old, merge)) {
607                         copy_cache_entry(merge, old);
608                 } else {
609                         if (verify_uptodate(old, o))
610                                 return -1;
611                         invalidate_ce_path(old, o);
612                 }
613         }
614         else {
615                 if (verify_absent(merge, "overwritten", o))
616                         return -1;
617                 invalidate_ce_path(merge, o);
618         }
620         add_entry(o, merge, CE_UPDATE, CE_STAGEMASK);
621         return 1;
624 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
625                 struct unpack_trees_options *o)
627         /* Did it exist in the index? */
628         if (!old) {
629                 if (verify_absent(ce, "removed", o))
630                         return -1;
631                 return 0;
632         }
633         if (verify_uptodate(old, o))
634                 return -1;
635         add_entry(o, ce, CE_REMOVE, 0);
636         invalidate_ce_path(ce, o);
637         return 1;
640 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
642         add_entry(o, ce, 0, 0);
643         return 1;
646 #if DBRT_DEBUG
647 static void show_stage_entry(FILE *o,
648                              const char *label, const struct cache_entry *ce)
650         if (!ce)
651                 fprintf(o, "%s (missing)\n", label);
652         else
653                 fprintf(o, "%s%06o %s %d\t%s\n",
654                         label,
655                         ce->ce_mode,
656                         sha1_to_hex(ce->sha1),
657                         ce_stage(ce),
658                         ce->name);
660 #endif
662 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
664         struct cache_entry *index;
665         struct cache_entry *head;
666         struct cache_entry *remote = stages[o->head_idx + 1];
667         int count;
668         int head_match = 0;
669         int remote_match = 0;
671         int df_conflict_head = 0;
672         int df_conflict_remote = 0;
674         int any_anc_missing = 0;
675         int no_anc_exists = 1;
676         int i;
678         for (i = 1; i < o->head_idx; i++) {
679                 if (!stages[i] || stages[i] == o->df_conflict_entry)
680                         any_anc_missing = 1;
681                 else
682                         no_anc_exists = 0;
683         }
685         index = stages[0];
686         head = stages[o->head_idx];
688         if (head == o->df_conflict_entry) {
689                 df_conflict_head = 1;
690                 head = NULL;
691         }
693         if (remote == o->df_conflict_entry) {
694                 df_conflict_remote = 1;
695                 remote = NULL;
696         }
698         /* First, if there's a #16 situation, note that to prevent #13
699          * and #14.
700          */
701         if (!same(remote, head)) {
702                 for (i = 1; i < o->head_idx; i++) {
703                         if (same(stages[i], head)) {
704                                 head_match = i;
705                         }
706                         if (same(stages[i], remote)) {
707                                 remote_match = i;
708                         }
709                 }
710         }
712         /* We start with cases where the index is allowed to match
713          * something other than the head: #14(ALT) and #2ALT, where it
714          * is permitted to match the result instead.
715          */
716         /* #14, #14ALT, #2ALT */
717         if (remote && !df_conflict_head && head_match && !remote_match) {
718                 if (index && !same(index, remote) && !same(index, head))
719                         return o->gently ? -1 : reject_merge(index);
720                 return merged_entry(remote, index, o);
721         }
722         /*
723          * If we have an entry in the index cache, then we want to
724          * make sure that it matches head.
725          */
726         if (index && !same(index, head))
727                 return o->gently ? -1 : reject_merge(index);
729         if (head) {
730                 /* #5ALT, #15 */
731                 if (same(head, remote))
732                         return merged_entry(head, index, o);
733                 /* #13, #3ALT */
734                 if (!df_conflict_remote && remote_match && !head_match)
735                         return merged_entry(head, index, o);
736         }
738         /* #1 */
739         if (!head && !remote && any_anc_missing)
740                 return 0;
742         /* Under the new "aggressive" rule, we resolve mostly trivial
743          * cases that we historically had git-merge-one-file resolve.
744          */
745         if (o->aggressive) {
746                 int head_deleted = !head && !df_conflict_head;
747                 int remote_deleted = !remote && !df_conflict_remote;
748                 struct cache_entry *ce = NULL;
750                 if (index)
751                         ce = index;
752                 else if (head)
753                         ce = head;
754                 else if (remote)
755                         ce = remote;
756                 else {
757                         for (i = 1; i < o->head_idx; i++) {
758                                 if (stages[i] && stages[i] != o->df_conflict_entry) {
759                                         ce = stages[i];
760                                         break;
761                                 }
762                         }
763                 }
765                 /*
766                  * Deleted in both.
767                  * Deleted in one and unchanged in the other.
768                  */
769                 if ((head_deleted && remote_deleted) ||
770                     (head_deleted && remote && remote_match) ||
771                     (remote_deleted && head && head_match)) {
772                         if (index)
773                                 return deleted_entry(index, index, o);
774                         if (ce && !head_deleted) {
775                                 if (verify_absent(ce, "removed", o))
776                                         return -1;
777                         }
778                         return 0;
779                 }
780                 /*
781                  * Added in both, identically.
782                  */
783                 if (no_anc_exists && head && remote && same(head, remote))
784                         return merged_entry(head, index, o);
786         }
788         /* Below are "no merge" cases, which require that the index be
789          * up-to-date to avoid the files getting overwritten with
790          * conflict resolution files.
791          */
792         if (index) {
793                 if (verify_uptodate(index, o))
794                         return -1;
795         }
797         o->nontrivial_merge = 1;
799         /* #2, #3, #4, #6, #7, #9, #10, #11. */
800         count = 0;
801         if (!head_match || !remote_match) {
802                 for (i = 1; i < o->head_idx; i++) {
803                         if (stages[i] && stages[i] != o->df_conflict_entry) {
804                                 keep_entry(stages[i], o);
805                                 count++;
806                                 break;
807                         }
808                 }
809         }
810 #if DBRT_DEBUG
811         else {
812                 fprintf(stderr, "read-tree: warning #16 detected\n");
813                 show_stage_entry(stderr, "head   ", stages[head_match]);
814                 show_stage_entry(stderr, "remote ", stages[remote_match]);
815         }
816 #endif
817         if (head) { count += keep_entry(head, o); }
818         if (remote) { count += keep_entry(remote, o); }
819         return count;
822 /*
823  * Two-way merge.
824  *
825  * The rule is to "carry forward" what is in the index without losing
826  * information across a "fast forward", favoring a successful merge
827  * over a merge failure when it makes sense.  For details of the
828  * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
829  *
830  */
831 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
833         struct cache_entry *current = src[0];
834         struct cache_entry *oldtree = src[1];
835         struct cache_entry *newtree = src[2];
837         if (o->merge_size != 2)
838                 return error("Cannot do a twoway merge of %d trees",
839                              o->merge_size);
841         if (oldtree == o->df_conflict_entry)
842                 oldtree = NULL;
843         if (newtree == o->df_conflict_entry)
844                 newtree = NULL;
846         if (current) {
847                 if ((!oldtree && !newtree) || /* 4 and 5 */
848                     (!oldtree && newtree &&
849                      same(current, newtree)) || /* 6 and 7 */
850                     (oldtree && newtree &&
851                      same(oldtree, newtree)) || /* 14 and 15 */
852                     (oldtree && newtree &&
853                      !same(oldtree, newtree) && /* 18 and 19 */
854                      same(current, newtree))) {
855                         return keep_entry(current, o);
856                 }
857                 else if (oldtree && !newtree && same(current, oldtree)) {
858                         /* 10 or 11 */
859                         return deleted_entry(oldtree, current, o);
860                 }
861                 else if (oldtree && newtree &&
862                          same(current, oldtree) && !same(current, newtree)) {
863                         /* 20 or 21 */
864                         return merged_entry(newtree, current, o);
865                 }
866                 else {
867                         /* all other failures */
868                         if (oldtree)
869                                 return o->gently ? -1 : reject_merge(oldtree);
870                         if (current)
871                                 return o->gently ? -1 : reject_merge(current);
872                         if (newtree)
873                                 return o->gently ? -1 : reject_merge(newtree);
874                         return -1;
875                 }
876         }
877         else if (newtree)
878                 return merged_entry(newtree, current, o);
879         return deleted_entry(oldtree, current, o);
882 /*
883  * Bind merge.
884  *
885  * Keep the index entries at stage0, collapse stage1 but make sure
886  * stage0 does not have anything there.
887  */
888 int bind_merge(struct cache_entry **src,
889                 struct unpack_trees_options *o)
891         struct cache_entry *old = src[0];
892         struct cache_entry *a = src[1];
894         if (o->merge_size != 1)
895                 return error("Cannot do a bind merge of %d trees\n",
896                              o->merge_size);
897         if (a && old)
898                 return o->gently ? -1 :
899                         error("Entry '%s' overlaps with '%s'.  Cannot bind.", a->name, old->name);
900         if (!a)
901                 return keep_entry(old, o);
902         else
903                 return merged_entry(a, NULL, o);
906 /*
907  * One-way merge.
908  *
909  * The rule is:
910  * - take the stat information from stage0, take the data from stage1
911  */
912 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
914         struct cache_entry *old = src[0];
915         struct cache_entry *a = src[1];
917         if (o->merge_size != 1)
918                 return error("Cannot do a oneway merge of %d trees",
919                              o->merge_size);
921         if (!a)
922                 return deleted_entry(old, old, o);
924         if (old && same(old, a)) {
925                 int update = 0;
926                 if (o->reset) {
927                         struct stat st;
928                         if (lstat(old->name, &st) ||
929                             ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
930                                 update |= CE_UPDATE;
931                 }
932                 add_entry(o, old, update, 0);
933                 return 0;
934         }
935         return merged_entry(a, old, o);