Code

Make 'unpack_trees()' take the index to work on as an argument
[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 inline void remove_entry(int remove, struct unpack_trees_options *o)
12 {
13         if (remove >= 0)
14                 remove_index_entry_at(o->index, remove);
15 }
17 /* Unlink the last component and attempt to remove leading
18  * directories, in case this unlink is the removal of the
19  * last entry in the directory -- empty directories are removed.
20  */
21 static void unlink_entry(char *name, char *last_symlink)
22 {
23         char *cp, *prev;
25         if (has_symlink_leading_path(name, last_symlink))
26                 return;
27         if (unlink(name))
28                 return;
29         prev = NULL;
30         while (1) {
31                 int status;
32                 cp = strrchr(name, '/');
33                 if (prev)
34                         *prev = '/';
35                 if (!cp)
36                         break;
38                 *cp = 0;
39                 status = rmdir(name);
40                 if (status) {
41                         *cp = '/';
42                         break;
43                 }
44                 prev = cp;
45         }
46 }
48 static struct checkout state;
49 static void check_updates(struct unpack_trees_options *o)
50 {
51         unsigned cnt = 0, total = 0;
52         struct progress *progress = NULL;
53         char last_symlink[PATH_MAX];
54         int i;
56         if (o->update && o->verbose_update) {
57                 for (total = cnt = 0; cnt < o->index->cache_nr; cnt++) {
58                         struct cache_entry *ce = o->index->cache[cnt];
59                         if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
60                                 total++;
61                 }
63                 progress = start_progress_delay("Checking out files",
64                                                 total, 50, 1);
65                 cnt = 0;
66         }
68         *last_symlink = '\0';
69         for (i = 0; i < o->index->cache_nr; i++) {
70                 struct cache_entry *ce = o->index->cache[i];
72                 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
73                         display_progress(progress, ++cnt);
74                 if (ce->ce_flags & CE_REMOVE) {
75                         if (o->update)
76                                 unlink_entry(ce->name, last_symlink);
77                         remove_index_entry_at(o->index, i);
78                         i--;
79                         continue;
80                 }
81                 if (ce->ce_flags & CE_UPDATE) {
82                         ce->ce_flags &= ~CE_UPDATE;
83                         if (o->update) {
84                                 checkout_entry(ce, &state, NULL);
85                                 *last_symlink = '\0';
86                         }
87                 }
88         }
89         stop_progress(&progress);
90 }
92 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o, int remove)
93 {
94         int ret = o->fn(src, o, remove);
95         if (ret > 0) {
96                 o->pos += ret;
97                 ret = 0;
98         }
99         return ret;
102 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
104         struct cache_entry *src[5] = { ce, };
105         if (ce_stage(ce)) {
106                 if (o->skip_unmerged) {
107                         o->pos++;
108                 } else {
109                         remove_entry(o->pos, o);
110                 }
111                 return 0;
112         }
113         return call_unpack_fn(src, o, o->pos);
116 int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
118         int i;
119         struct tree_desc t[3];
120         struct traverse_info newinfo;
121         struct name_entry *p;
123         p = names;
124         while (!p->mode)
125                 p++;
127         newinfo = *info;
128         newinfo.prev = info;
129         newinfo.name = *p;
130         newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
131         newinfo.conflicts |= df_conflicts;
133         for (i = 0; i < n; i++, dirmask >>= 1) {
134                 const unsigned char *sha1 = NULL;
135                 if (dirmask & 1)
136                         sha1 = names[i].sha1;
137                 fill_tree_descriptor(t+i, sha1);
138         }
139         traverse_trees(n, t, &newinfo);
140         return 0;
143 /*
144  * Compare the traverse-path to the cache entry without actually
145  * having to generate the textual representation of the traverse
146  * path.
147  *
148  * NOTE! This *only* compares up to the size of the traverse path
149  * itself - the caller needs to do the final check for the cache
150  * entry having more data at the end!
151  */
152 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
154         int len, pathlen, ce_len;
155         const char *ce_name;
157         if (info->prev) {
158                 int cmp = do_compare_entry(ce, info->prev, &info->name);
159                 if (cmp)
160                         return cmp;
161         }
162         pathlen = info->pathlen;
163         ce_len = ce_namelen(ce);
165         /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
166         if (ce_len < pathlen)
167                 return -1;
169         ce_len -= pathlen;
170         ce_name = ce->name + pathlen;
172         len = tree_entry_len(n->path, n->sha1);
173         return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
176 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
178         int cmp = do_compare_entry(ce, info, n);
179         if (cmp)
180                 return cmp;
182         /*
183          * Even if the beginning compared identically, the ce should
184          * compare as bigger than a directory leading up to it!
185          */
186         return ce_namelen(ce) > traverse_path_len(info, n);
189 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
191         int len = traverse_path_len(info, n);
192         struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
194         ce->ce_mode = create_ce_mode(n->mode);
195         ce->ce_flags = create_ce_flags(len, stage);
196         hashcpy(ce->sha1, n->sha1);
197         make_traverse_path(ce->name, info, n);
199         return ce;
202 static int unpack_nondirectories(int n, unsigned long mask, unsigned long dirmask, struct cache_entry *src[5],
203         const struct name_entry *names, const struct traverse_info *info, int remove)
205         int i;
206         struct unpack_trees_options *o = info->data;
207         unsigned long conflicts;
209         /* Do we have *only* directories? Nothing to do */
210         if (mask == dirmask && !src[0])
211                 return 0;
213         conflicts = info->conflicts;
214         if (o->merge)
215                 conflicts >>= 1;
216         conflicts |= dirmask;
218         /*
219          * Ok, we've filled in up to any potential index entry in src[0],
220          * now do the rest.
221          */
222         for (i = 0; i < n; i++) {
223                 int stage;
224                 unsigned int bit = 1ul << i;
225                 if (conflicts & bit) {
226                         src[i + o->merge] = o->df_conflict_entry;
227                         continue;
228                 }
229                 if (!(mask & bit))
230                         continue;
231                 if (!o->merge)
232                         stage = 0;
233                 else if (i + 1 < o->head_idx)
234                         stage = 1;
235                 else if (i + 1 > o->head_idx)
236                         stage = 3;
237                 else
238                         stage = 2;
239                 src[i + o->merge] = create_ce_entry(info, names + i, stage);
240         }
242         if (o->merge)
243                 return call_unpack_fn(src, o, remove);
245         n += o->merge;
246         remove_entry(remove, o);
247         for (i = 0; i < n; i++)
248                 add_index_entry(o->index, src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
249         return 0;
252 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
254         struct cache_entry *src[5] = { NULL, };
255         struct unpack_trees_options *o = info->data;
256         int remove = -1;
257         const struct name_entry *p = names;
259         /* Find first entry with a real name (we could use "mask" too) */
260         while (!p->mode)
261                 p++;
263         /* Are we supposed to look at the index too? */
264         if (o->merge) {
265                 while (o->pos < o->index->cache_nr) {
266                         struct cache_entry *ce = o->index->cache[o->pos];
267                         int cmp = compare_entry(ce, info, p);
268                         if (cmp < 0) {
269                                 if (unpack_index_entry(ce, o) < 0)
270                                         return -1;
271                                 continue;
272                         }
273                         if (!cmp) {
274                                 if (ce_stage(ce)) {
275                                         /*
276                                          * If we skip unmerged index entries, we'll skip this
277                                          * entry *and* the tree entries associated with it!
278                                          */
279                                         if (o->skip_unmerged)
280                                                 return mask;
281                                         remove_entry(o->pos, o);
282                                         continue;
283                                 }
284                                 src[0] = ce;
285                                 remove = o->pos;
286                         }
287                         break;
288                 }
289         }
291         if (unpack_nondirectories(n, mask, dirmask, src, names, info, remove) < 0)
292                 return -1;
294         /* Now handle any directories.. */
295         if (dirmask) {
296                 unsigned long conflicts = mask & ~dirmask;
297                 if (o->merge) {
298                         conflicts <<= 1;
299                         if (src[0])
300                                 conflicts |= 1;
301                 }
302                 traverse_trees_recursive(n, dirmask, conflicts, names, info);
303                 return mask;
304         }
306         return mask;
309 static int unpack_failed(struct unpack_trees_options *o, const char *message)
311         if (!o->gently) {
312                 if (message)
313                         return error(message);
314                 return -1;
315         }
316         discard_index(o->index);
317         read_index(o->index);
318         return -1;
321 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
323         static struct cache_entry *dfc;
325         if (len > 4)
326                 die("unpack_trees takes at most four trees");
327         memset(&state, 0, sizeof(state));
328         state.base_dir = "";
329         state.force = 1;
330         state.quiet = 1;
331         state.refresh_cache = 1;
333         o->merge_size = len;
335         if (!dfc)
336                 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
337         o->df_conflict_entry = dfc;
339         if (len) {
340                 const char *prefix = o->prefix ? o->prefix : "";
341                 struct traverse_info info;
343                 setup_traverse_info(&info, prefix);
344                 info.fn = unpack_callback;
345                 info.data = o;
347                 if (traverse_trees(len, t, &info) < 0)
348                         return unpack_failed(o, NULL);
349         }
351         /* Any left-over entries in the index? */
352         if (o->merge) {
353                 while (o->pos < o->index->cache_nr) {
354                         struct cache_entry *ce = o->index->cache[o->pos];
355                         if (unpack_index_entry(ce, o) < 0)
356                                 return unpack_failed(o, NULL);
357                 }
358         }
360         if (o->trivial_merges_only && o->nontrivial_merge)
361                 return unpack_failed(o, "Merge requires file-level merging");
363         check_updates(o);
364         return 0;
367 /* Here come the merge functions */
369 static int reject_merge(struct cache_entry *ce)
371         return error("Entry '%s' would be overwritten by merge. Cannot merge.",
372                      ce->name);
375 static int same(struct cache_entry *a, struct cache_entry *b)
377         if (!!a != !!b)
378                 return 0;
379         if (!a && !b)
380                 return 1;
381         return a->ce_mode == b->ce_mode &&
382                !hashcmp(a->sha1, b->sha1);
386 /*
387  * When a CE gets turned into an unmerged entry, we
388  * want it to be up-to-date
389  */
390 static int verify_uptodate(struct cache_entry *ce,
391                 struct unpack_trees_options *o)
393         struct stat st;
395         if (o->index_only || o->reset)
396                 return 0;
398         if (!lstat(ce->name, &st)) {
399                 unsigned changed = ie_match_stat(o->index, ce, &st, CE_MATCH_IGNORE_VALID);
400                 if (!changed)
401                         return 0;
402                 /*
403                  * NEEDSWORK: the current default policy is to allow
404                  * submodule to be out of sync wrt the supermodule
405                  * index.  This needs to be tightened later for
406                  * submodules that are marked to be automatically
407                  * checked out.
408                  */
409                 if (S_ISGITLINK(ce->ce_mode))
410                         return 0;
411                 errno = 0;
412         }
413         if (errno == ENOENT)
414                 return 0;
415         return o->gently ? -1 :
416                 error("Entry '%s' not uptodate. Cannot merge.", ce->name);
419 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
421         if (ce)
422                 cache_tree_invalidate_path(o->index->cache_tree, ce->name);
425 /*
426  * Check that checking out ce->sha1 in subdir ce->name is not
427  * going to overwrite any working files.
428  *
429  * Currently, git does not checkout subprojects during a superproject
430  * checkout, so it is not going to overwrite anything.
431  */
432 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
433                                       struct unpack_trees_options *o)
435         return 0;
438 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
439                                       struct unpack_trees_options *o)
441         /*
442          * we are about to extract "ce->name"; we would not want to lose
443          * anything in the existing directory there.
444          */
445         int namelen;
446         int pos, i;
447         struct dir_struct d;
448         char *pathbuf;
449         int cnt = 0;
450         unsigned char sha1[20];
452         if (S_ISGITLINK(ce->ce_mode) &&
453             resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
454                 /* If we are not going to update the submodule, then
455                  * we don't care.
456                  */
457                 if (!hashcmp(sha1, ce->sha1))
458                         return 0;
459                 return verify_clean_submodule(ce, action, o);
460         }
462         /*
463          * First let's make sure we do not have a local modification
464          * in that directory.
465          */
466         namelen = strlen(ce->name);
467         pos = index_name_pos(o->index, ce->name, namelen);
468         if (0 <= pos)
469                 return cnt; /* we have it as nondirectory */
470         pos = -pos - 1;
471         for (i = pos; i < o->index->cache_nr; i++) {
472                 struct cache_entry *ce = o->index->cache[i];
473                 int len = ce_namelen(ce);
474                 if (len < namelen ||
475                     strncmp(ce->name, ce->name, namelen) ||
476                     ce->name[namelen] != '/')
477                         break;
478                 /*
479                  * ce->name is an entry in the subdirectory.
480                  */
481                 if (!ce_stage(ce)) {
482                         if (verify_uptodate(ce, o))
483                                 return -1;
484                         ce->ce_flags |= CE_REMOVE;
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                 return o->gently ? -1 :
503                         error("Updating '%s' would lose untracked files in it",
504                               ce->name);
505         free(pathbuf);
506         return cnt;
509 /*
510  * We do not want to remove or overwrite a working tree file that
511  * is not tracked, unless it is ignored.
512  */
513 static int verify_absent(struct cache_entry *ce, const char *action,
514                          struct unpack_trees_options *o)
516         struct stat st;
518         if (o->index_only || o->reset || !o->update)
519                 return 0;
521         if (has_symlink_leading_path(ce->name, NULL))
522                 return 0;
524         if (!lstat(ce->name, &st)) {
525                 int cnt;
526                 int dtype = ce_to_dtype(ce);
528                 if (o->dir && excluded(o->dir, ce->name, &dtype))
529                         /*
530                          * ce->name is explicitly excluded, so it is Ok to
531                          * overwrite it.
532                          */
533                         return 0;
534                 if (S_ISDIR(st.st_mode)) {
535                         /*
536                          * We are checking out path "foo" and
537                          * found "foo/." in the working tree.
538                          * This is tricky -- if we have modified
539                          * files that are in "foo/" we would lose
540                          * it.
541                          */
542                         cnt = verify_clean_subdirectory(ce, action, o);
544                         /*
545                          * If this removed entries from the index,
546                          * what that means is:
547                          *
548                          * (1) the caller unpack_trees_rec() saw path/foo
549                          * in the index, and it has not removed it because
550                          * it thinks it is handling 'path' as blob with
551                          * D/F conflict;
552                          * (2) we will return "ok, we placed a merged entry
553                          * in the index" which would cause o->pos to be
554                          * incremented by one;
555                          * (3) however, original o->pos now has 'path/foo'
556                          * marked with "to be removed".
557                          *
558                          * We need to increment it by the number of
559                          * deleted entries here.
560                          */
561                         o->pos += cnt;
562                         return 0;
563                 }
565                 /*
566                  * The previous round may already have decided to
567                  * delete this path, which is in a subdirectory that
568                  * is being replaced with a blob.
569                  */
570                 cnt = index_name_pos(o->index, ce->name, strlen(ce->name));
571                 if (0 <= cnt) {
572                         struct cache_entry *ce = o->index->cache[cnt];
573                         if (ce->ce_flags & CE_REMOVE)
574                                 return 0;
575                 }
577                 return o->gently ? -1 :
578                         error("Untracked working tree file '%s' "
579                               "would be %s by merge.", ce->name, action);
580         }
581         return 0;
584 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
585                 struct unpack_trees_options *o)
587         merge->ce_flags |= CE_UPDATE;
588         if (old) {
589                 /*
590                  * See if we can re-use the old CE directly?
591                  * That way we get the uptodate stat info.
592                  *
593                  * This also removes the UPDATE flag on
594                  * a match.
595                  */
596                 if (same(old, merge)) {
597                         copy_cache_entry(merge, old);
598                 } else {
599                         if (verify_uptodate(old, o))
600                                 return -1;
601                         invalidate_ce_path(old, o);
602                 }
603         }
604         else {
605                 if (verify_absent(merge, "overwritten", o))
606                         return -1;
607                 invalidate_ce_path(merge, o);
608         }
610         merge->ce_flags &= ~CE_STAGEMASK;
611         add_index_entry(o->index, 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                 if (verify_uptodate(old, o))
620                         return -1;
621         } else
622                 if (verify_absent(ce, "removed", o))
623                         return -1;
624         ce->ce_flags |= CE_REMOVE;
625         add_index_entry(o->index, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
626         invalidate_ce_path(ce, o);
627         return 1;
630 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
632         add_index_entry(o->index, ce, ADD_CACHE_OK_TO_ADD);
633         return 1;
636 #if DBRT_DEBUG
637 static void show_stage_entry(FILE *o,
638                              const char *label, const struct cache_entry *ce)
640         if (!ce)
641                 fprintf(o, "%s (missing)\n", label);
642         else
643                 fprintf(o, "%s%06o %s %d\t%s\n",
644                         label,
645                         ce->ce_mode,
646                         sha1_to_hex(ce->sha1),
647                         ce_stage(ce),
648                         ce->name);
650 #endif
652 int threeway_merge(struct cache_entry **stages,
653                 struct unpack_trees_options *o,
654                 int remove)
656         struct cache_entry *index;
657         struct cache_entry *head;
658         struct cache_entry *remote = stages[o->head_idx + 1];
659         int count;
660         int head_match = 0;
661         int remote_match = 0;
663         int df_conflict_head = 0;
664         int df_conflict_remote = 0;
666         int any_anc_missing = 0;
667         int no_anc_exists = 1;
668         int i;
670         for (i = 1; i < o->head_idx; i++) {
671                 if (!stages[i] || stages[i] == o->df_conflict_entry)
672                         any_anc_missing = 1;
673                 else
674                         no_anc_exists = 0;
675         }
677         index = stages[0];
678         head = stages[o->head_idx];
680         if (head == o->df_conflict_entry) {
681                 df_conflict_head = 1;
682                 head = NULL;
683         }
685         if (remote == o->df_conflict_entry) {
686                 df_conflict_remote = 1;
687                 remote = NULL;
688         }
690         /* First, if there's a #16 situation, note that to prevent #13
691          * and #14.
692          */
693         if (!same(remote, head)) {
694                 for (i = 1; i < o->head_idx; i++) {
695                         if (same(stages[i], head)) {
696                                 head_match = i;
697                         }
698                         if (same(stages[i], remote)) {
699                                 remote_match = i;
700                         }
701                 }
702         }
704         /* We start with cases where the index is allowed to match
705          * something other than the head: #14(ALT) and #2ALT, where it
706          * is permitted to match the result instead.
707          */
708         /* #14, #14ALT, #2ALT */
709         if (remote && !df_conflict_head && head_match && !remote_match) {
710                 if (index && !same(index, remote) && !same(index, head))
711                         return o->gently ? -1 : reject_merge(index);
712                 return merged_entry(remote, index, o);
713         }
714         /*
715          * If we have an entry in the index cache, then we want to
716          * make sure that it matches head.
717          */
718         if (index && !same(index, head))
719                 return o->gently ? -1 : reject_merge(index);
721         if (head) {
722                 /* #5ALT, #15 */
723                 if (same(head, remote))
724                         return merged_entry(head, index, o);
725                 /* #13, #3ALT */
726                 if (!df_conflict_remote && remote_match && !head_match)
727                         return merged_entry(head, index, o);
728         }
730         /* #1 */
731         if (!head && !remote && any_anc_missing) {
732                 remove_entry(remove, o);
733                 return 0;
734         }
736         /* Under the new "aggressive" rule, we resolve mostly trivial
737          * cases that we historically had git-merge-one-file resolve.
738          */
739         if (o->aggressive) {
740                 int head_deleted = !head && !df_conflict_head;
741                 int remote_deleted = !remote && !df_conflict_remote;
742                 struct cache_entry *ce = NULL;
744                 if (index)
745                         ce = index;
746                 else if (head)
747                         ce = head;
748                 else if (remote)
749                         ce = remote;
750                 else {
751                         for (i = 1; i < o->head_idx; i++) {
752                                 if (stages[i] && stages[i] != o->df_conflict_entry) {
753                                         ce = stages[i];
754                                         break;
755                                 }
756                         }
757                 }
759                 /*
760                  * Deleted in both.
761                  * Deleted in one and unchanged in the other.
762                  */
763                 if ((head_deleted && remote_deleted) ||
764                     (head_deleted && remote && remote_match) ||
765                     (remote_deleted && head && head_match)) {
766                         remove_entry(remove, o);
767                         if (index)
768                                 return deleted_entry(index, index, o);
769                         else if (ce && !head_deleted) {
770                                 if (verify_absent(ce, "removed", o))
771                                         return -1;
772                         }
773                         return 0;
774                 }
775                 /*
776                  * Added in both, identically.
777                  */
778                 if (no_anc_exists && head && remote && same(head, remote))
779                         return merged_entry(head, index, o);
781         }
783         /* Below are "no merge" cases, which require that the index be
784          * up-to-date to avoid the files getting overwritten with
785          * conflict resolution files.
786          */
787         if (index) {
788                 if (verify_uptodate(index, o))
789                         return -1;
790         }
792         remove_entry(remove, o);
793         o->nontrivial_merge = 1;
795         /* #2, #3, #4, #6, #7, #9, #10, #11. */
796         count = 0;
797         if (!head_match || !remote_match) {
798                 for (i = 1; i < o->head_idx; i++) {
799                         if (stages[i] && stages[i] != o->df_conflict_entry) {
800                                 keep_entry(stages[i], o);
801                                 count++;
802                                 break;
803                         }
804                 }
805         }
806 #if DBRT_DEBUG
807         else {
808                 fprintf(stderr, "read-tree: warning #16 detected\n");
809                 show_stage_entry(stderr, "head   ", stages[head_match]);
810                 show_stage_entry(stderr, "remote ", stages[remote_match]);
811         }
812 #endif
813         if (head) { count += keep_entry(head, o); }
814         if (remote) { count += keep_entry(remote, o); }
815         return count;
818 /*
819  * Two-way merge.
820  *
821  * The rule is to "carry forward" what is in the index without losing
822  * information across a "fast forward", favoring a successful merge
823  * over a merge failure when it makes sense.  For details of the
824  * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
825  *
826  */
827 int twoway_merge(struct cache_entry **src,
828                 struct unpack_trees_options *o,
829                 int remove)
831         struct cache_entry *current = src[0];
832         struct cache_entry *oldtree = src[1];
833         struct cache_entry *newtree = src[2];
835         if (o->merge_size != 2)
836                 return error("Cannot do a twoway merge of %d trees",
837                              o->merge_size);
839         if (oldtree == o->df_conflict_entry)
840                 oldtree = NULL;
841         if (newtree == o->df_conflict_entry)
842                 newtree = NULL;
844         if (current) {
845                 if ((!oldtree && !newtree) || /* 4 and 5 */
846                     (!oldtree && newtree &&
847                      same(current, newtree)) || /* 6 and 7 */
848                     (oldtree && newtree &&
849                      same(oldtree, newtree)) || /* 14 and 15 */
850                     (oldtree && newtree &&
851                      !same(oldtree, newtree) && /* 18 and 19 */
852                      same(current, newtree))) {
853                         return keep_entry(current, o);
854                 }
855                 else if (oldtree && !newtree && same(current, oldtree)) {
856                         /* 10 or 11 */
857                         remove_entry(remove, o);
858                         return deleted_entry(oldtree, current, o);
859                 }
860                 else if (oldtree && newtree &&
861                          same(current, oldtree) && !same(current, newtree)) {
862                         /* 20 or 21 */
863                         return merged_entry(newtree, current, o);
864                 }
865                 else {
866                         /* all other failures */
867                         remove_entry(remove, o);
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         remove_entry(remove, o);
880         return deleted_entry(oldtree, current, o);
883 /*
884  * Bind merge.
885  *
886  * Keep the index entries at stage0, collapse stage1 but make sure
887  * stage0 does not have anything there.
888  */
889 int bind_merge(struct cache_entry **src,
890                 struct unpack_trees_options *o,
891                 int remove)
893         struct cache_entry *old = src[0];
894         struct cache_entry *a = src[1];
896         if (o->merge_size != 1)
897                 return error("Cannot do a bind merge of %d trees\n",
898                              o->merge_size);
899         if (a && old)
900                 return o->gently ? -1 :
901                         error("Entry '%s' overlaps.  Cannot bind.", a->name);
902         if (!a)
903                 return keep_entry(old, o);
904         else
905                 return merged_entry(a, NULL, o);
908 /*
909  * One-way merge.
910  *
911  * The rule is:
912  * - take the stat information from stage0, take the data from stage1
913  */
914 int oneway_merge(struct cache_entry **src,
915                 struct unpack_trees_options *o,
916                 int remove)
918         struct cache_entry *old = src[0];
919         struct cache_entry *a = src[1];
921         if (o->merge_size != 1)
922                 return error("Cannot do a oneway merge of %d trees",
923                              o->merge_size);
925         if (!a) {
926                 remove_entry(remove, o);
927                 return deleted_entry(old, old, o);
928         }
929         if (old && same(old, a)) {
930                 if (o->reset) {
931                         struct stat st;
932                         if (lstat(old->name, &st) ||
933                             ie_match_stat(o->index, old, &st, CE_MATCH_IGNORE_VALID))
934                                 old->ce_flags |= CE_UPDATE;
935                 }
936                 return keep_entry(old, o);
937         }
938         return merged_entry(a, old, o);