Code

Introduce an unlink(2) wrapper which gives warning if unlink failed
[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"
10 #include "attr.h"
12 /*
13  * Error messages expected by scripts out of plumbing commands such as
14  * read-tree.  Non-scripted Porcelain is not required to use these messages
15  * and in fact are encouraged to reword them to better suit their particular
16  * situation better.  See how "git checkout" replaces not_uptodate_file to
17  * explain why it does not allow switching between branches when you have
18  * local changes, for example.
19  */
20 static struct unpack_trees_error_msgs unpack_plumbing_errors = {
21         /* would_overwrite */
22         "Entry '%s' would be overwritten by merge. Cannot merge.",
24         /* not_uptodate_file */
25         "Entry '%s' not uptodate. Cannot merge.",
27         /* not_uptodate_dir */
28         "Updating '%s' would lose untracked files in it",
30         /* would_lose_untracked */
31         "Untracked working tree file '%s' would be %s by merge.",
33         /* bind_overlap */
34         "Entry '%s' overlaps with '%s'.  Cannot bind.",
35 };
37 #define ERRORMSG(o,fld) \
38         ( ((o) && (o)->msgs.fld) \
39         ? ((o)->msgs.fld) \
40         : (unpack_plumbing_errors.fld) )
42 static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
43         unsigned int set, unsigned int clear)
44 {
45         unsigned int size = ce_size(ce);
46         struct cache_entry *new = xmalloc(size);
48         clear |= CE_HASHED | CE_UNHASHED;
50         memcpy(new, ce, size);
51         new->next = NULL;
52         new->ce_flags = (new->ce_flags & ~clear) | set;
53         add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
54 }
56 /*
57  * Unlink the last component and schedule the leading directories for
58  * removal, such that empty directories get removed.
59  */
60 static void unlink_entry(struct cache_entry *ce)
61 {
62         if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
63                 return;
64         if (unlink(ce->name))
65                 return;
66         schedule_dir_for_removal(ce->name, ce_namelen(ce));
67 }
69 static struct checkout state;
70 static int check_updates(struct unpack_trees_options *o)
71 {
72         unsigned cnt = 0, total = 0;
73         struct progress *progress = NULL;
74         struct index_state *index = &o->result;
75         int i;
76         int errs = 0;
78         if (o->update && o->verbose_update) {
79                 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
80                         struct cache_entry *ce = index->cache[cnt];
81                         if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
82                                 total++;
83                 }
85                 progress = start_progress_delay("Checking out files",
86                                                 total, 50, 1);
87                 cnt = 0;
88         }
90         if (o->update)
91                 git_attr_set_direction(GIT_ATTR_CHECKOUT, &o->result);
92         for (i = 0; i < index->cache_nr; i++) {
93                 struct cache_entry *ce = index->cache[i];
95                 if (ce->ce_flags & CE_REMOVE) {
96                         display_progress(progress, ++cnt);
97                         if (o->update)
98                                 unlink_entry(ce);
99                 }
100         }
101         remove_marked_cache_entries(&o->result);
102         remove_scheduled_dirs();
104         for (i = 0; i < index->cache_nr; i++) {
105                 struct cache_entry *ce = index->cache[i];
107                 if (ce->ce_flags & CE_UPDATE) {
108                         display_progress(progress, ++cnt);
109                         ce->ce_flags &= ~CE_UPDATE;
110                         if (o->update) {
111                                 errs |= checkout_entry(ce, &state, NULL);
112                         }
113                 }
114         }
115         stop_progress(&progress);
116         if (o->update)
117                 git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
118         return errs != 0;
121 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
123         int ret = o->fn(src, o);
124         if (ret > 0)
125                 ret = 0;
126         return ret;
129 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
131         struct cache_entry *src[5] = { ce, };
133         o->pos++;
134         if (ce_stage(ce)) {
135                 if (o->skip_unmerged) {
136                         add_entry(o, ce, 0, 0);
137                         return 0;
138                 }
139         }
140         return call_unpack_fn(src, o);
143 int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
145         int i;
146         struct tree_desc t[MAX_UNPACK_TREES];
147         struct traverse_info newinfo;
148         struct name_entry *p;
150         p = names;
151         while (!p->mode)
152                 p++;
154         newinfo = *info;
155         newinfo.prev = info;
156         newinfo.name = *p;
157         newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
158         newinfo.conflicts |= df_conflicts;
160         for (i = 0; i < n; i++, dirmask >>= 1) {
161                 const unsigned char *sha1 = NULL;
162                 if (dirmask & 1)
163                         sha1 = names[i].sha1;
164                 fill_tree_descriptor(t+i, sha1);
165         }
166         return traverse_trees(n, t, &newinfo);
169 /*
170  * Compare the traverse-path to the cache entry without actually
171  * having to generate the textual representation of the traverse
172  * path.
173  *
174  * NOTE! This *only* compares up to the size of the traverse path
175  * itself - the caller needs to do the final check for the cache
176  * entry having more data at the end!
177  */
178 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
180         int len, pathlen, ce_len;
181         const char *ce_name;
183         if (info->prev) {
184                 int cmp = do_compare_entry(ce, info->prev, &info->name);
185                 if (cmp)
186                         return cmp;
187         }
188         pathlen = info->pathlen;
189         ce_len = ce_namelen(ce);
191         /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
192         if (ce_len < pathlen)
193                 return -1;
195         ce_len -= pathlen;
196         ce_name = ce->name + pathlen;
198         len = tree_entry_len(n->path, n->sha1);
199         return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
202 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
204         int cmp = do_compare_entry(ce, info, n);
205         if (cmp)
206                 return cmp;
208         /*
209          * Even if the beginning compared identically, the ce should
210          * compare as bigger than a directory leading up to it!
211          */
212         return ce_namelen(ce) > traverse_path_len(info, n);
215 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
217         int len = traverse_path_len(info, n);
218         struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
220         ce->ce_mode = create_ce_mode(n->mode);
221         ce->ce_flags = create_ce_flags(len, stage);
222         hashcpy(ce->sha1, n->sha1);
223         make_traverse_path(ce->name, info, n);
225         return ce;
228 static int unpack_nondirectories(int n, unsigned long mask,
229                                  unsigned long dirmask,
230                                  struct cache_entry **src,
231                                  const struct name_entry *names,
232                                  const struct traverse_info *info)
234         int i;
235         struct unpack_trees_options *o = info->data;
236         unsigned long conflicts;
238         /* Do we have *only* directories? Nothing to do */
239         if (mask == dirmask && !src[0])
240                 return 0;
242         conflicts = info->conflicts;
243         if (o->merge)
244                 conflicts >>= 1;
245         conflicts |= dirmask;
247         /*
248          * Ok, we've filled in up to any potential index entry in src[0],
249          * now do the rest.
250          */
251         for (i = 0; i < n; i++) {
252                 int stage;
253                 unsigned int bit = 1ul << i;
254                 if (conflicts & bit) {
255                         src[i + o->merge] = o->df_conflict_entry;
256                         continue;
257                 }
258                 if (!(mask & bit))
259                         continue;
260                 if (!o->merge)
261                         stage = 0;
262                 else if (i + 1 < o->head_idx)
263                         stage = 1;
264                 else if (i + 1 > o->head_idx)
265                         stage = 3;
266                 else
267                         stage = 2;
268                 src[i + o->merge] = create_ce_entry(info, names + i, stage);
269         }
271         if (o->merge)
272                 return call_unpack_fn(src, o);
274         for (i = 0; i < n; i++)
275                 if (src[i] && src[i] != o->df_conflict_entry)
276                         add_entry(o, src[i], 0, 0);
277         return 0;
280 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
282         struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
283         struct unpack_trees_options *o = info->data;
284         const struct name_entry *p = names;
286         /* Find first entry with a real name (we could use "mask" too) */
287         while (!p->mode)
288                 p++;
290         /* Are we supposed to look at the index too? */
291         if (o->merge) {
292                 while (o->pos < o->src_index->cache_nr) {
293                         struct cache_entry *ce = o->src_index->cache[o->pos];
294                         int cmp = compare_entry(ce, info, p);
295                         if (cmp < 0) {
296                                 if (unpack_index_entry(ce, o) < 0)
297                                         return -1;
298                                 continue;
299                         }
300                         if (!cmp) {
301                                 o->pos++;
302                                 if (ce_stage(ce)) {
303                                         /*
304                                          * If we skip unmerged index entries, we'll skip this
305                                          * entry *and* the tree entries associated with it!
306                                          */
307                                         if (o->skip_unmerged) {
308                                                 add_entry(o, ce, 0, 0);
309                                                 return mask;
310                                         }
311                                 }
312                                 src[0] = ce;
313                         }
314                         break;
315                 }
316         }
318         if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
319                 return -1;
321         /* Now handle any directories.. */
322         if (dirmask) {
323                 unsigned long conflicts = mask & ~dirmask;
324                 if (o->merge) {
325                         conflicts <<= 1;
326                         if (src[0])
327                                 conflicts |= 1;
328                 }
329                 if (traverse_trees_recursive(n, dirmask, conflicts,
330                                              names, info) < 0)
331                         return -1;
332                 return mask;
333         }
335         return mask;
338 static int unpack_failed(struct unpack_trees_options *o, const char *message)
340         discard_index(&o->result);
341         if (!o->gently) {
342                 if (message)
343                         return error("%s", message);
344                 return -1;
345         }
346         return -1;
349 /*
350  * N-way merge "len" trees.  Returns 0 on success, -1 on failure to manipulate the
351  * resulting index, -2 on failure to reflect the changes to the work tree.
352  */
353 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
355         int ret;
356         static struct cache_entry *dfc;
358         if (len > MAX_UNPACK_TREES)
359                 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
360         memset(&state, 0, sizeof(state));
361         state.base_dir = "";
362         state.force = 1;
363         state.quiet = 1;
364         state.refresh_cache = 1;
366         memset(&o->result, 0, sizeof(o->result));
367         o->result.initialized = 1;
368         if (o->src_index) {
369                 o->result.timestamp.sec = o->src_index->timestamp.sec;
370                 o->result.timestamp.nsec = o->src_index->timestamp.nsec;
371         }
372         o->merge_size = len;
374         if (!dfc)
375                 dfc = xcalloc(1, cache_entry_size(0));
376         o->df_conflict_entry = dfc;
378         if (len) {
379                 const char *prefix = o->prefix ? o->prefix : "";
380                 struct traverse_info info;
382                 setup_traverse_info(&info, prefix);
383                 info.fn = unpack_callback;
384                 info.data = o;
386                 if (traverse_trees(len, t, &info) < 0)
387                         return unpack_failed(o, NULL);
388         }
390         /* Any left-over entries in the index? */
391         if (o->merge) {
392                 while (o->pos < o->src_index->cache_nr) {
393                         struct cache_entry *ce = o->src_index->cache[o->pos];
394                         if (unpack_index_entry(ce, o) < 0)
395                                 return unpack_failed(o, NULL);
396                 }
397         }
399         if (o->trivial_merges_only && o->nontrivial_merge)
400                 return unpack_failed(o, "Merge requires file-level merging");
402         o->src_index = NULL;
403         ret = check_updates(o) ? (-2) : 0;
404         if (o->dst_index)
405                 *o->dst_index = o->result;
406         return ret;
409 /* Here come the merge functions */
411 static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
413         return error(ERRORMSG(o, would_overwrite), ce->name);
416 static int same(struct cache_entry *a, struct cache_entry *b)
418         if (!!a != !!b)
419                 return 0;
420         if (!a && !b)
421                 return 1;
422         return a->ce_mode == b->ce_mode &&
423                !hashcmp(a->sha1, b->sha1);
427 /*
428  * When a CE gets turned into an unmerged entry, we
429  * want it to be up-to-date
430  */
431 static int verify_uptodate(struct cache_entry *ce,
432                 struct unpack_trees_options *o)
434         struct stat st;
436         if (o->index_only || o->reset || ce_uptodate(ce))
437                 return 0;
439         if (!lstat(ce->name, &st)) {
440                 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
441                 if (!changed)
442                         return 0;
443                 /*
444                  * NEEDSWORK: the current default policy is to allow
445                  * submodule to be out of sync wrt the supermodule
446                  * index.  This needs to be tightened later for
447                  * submodules that are marked to be automatically
448                  * checked out.
449                  */
450                 if (S_ISGITLINK(ce->ce_mode))
451                         return 0;
452                 errno = 0;
453         }
454         if (errno == ENOENT)
455                 return 0;
456         return o->gently ? -1 :
457                 error(ERRORMSG(o, not_uptodate_file), ce->name);
460 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
462         if (ce)
463                 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
466 /*
467  * Check that checking out ce->sha1 in subdir ce->name is not
468  * going to overwrite any working files.
469  *
470  * Currently, git does not checkout subprojects during a superproject
471  * checkout, so it is not going to overwrite anything.
472  */
473 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
474                                       struct unpack_trees_options *o)
476         return 0;
479 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
480                                       struct unpack_trees_options *o)
482         /*
483          * we are about to extract "ce->name"; we would not want to lose
484          * anything in the existing directory there.
485          */
486         int namelen;
487         int i;
488         struct dir_struct d;
489         char *pathbuf;
490         int cnt = 0;
491         unsigned char sha1[20];
493         if (S_ISGITLINK(ce->ce_mode) &&
494             resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
495                 /* If we are not going to update the submodule, then
496                  * we don't care.
497                  */
498                 if (!hashcmp(sha1, ce->sha1))
499                         return 0;
500                 return verify_clean_submodule(ce, action, o);
501         }
503         /*
504          * First let's make sure we do not have a local modification
505          * in that directory.
506          */
507         namelen = strlen(ce->name);
508         for (i = o->pos; i < o->src_index->cache_nr; i++) {
509                 struct cache_entry *ce2 = o->src_index->cache[i];
510                 int len = ce_namelen(ce2);
511                 if (len < namelen ||
512                     strncmp(ce->name, ce2->name, namelen) ||
513                     ce2->name[namelen] != '/')
514                         break;
515                 /*
516                  * ce2->name is an entry in the subdirectory.
517                  */
518                 if (!ce_stage(ce2)) {
519                         if (verify_uptodate(ce2, o))
520                                 return -1;
521                         add_entry(o, ce2, CE_REMOVE, 0);
522                 }
523                 cnt++;
524         }
526         /*
527          * Then we need to make sure that we do not lose a locally
528          * present file that is not ignored.
529          */
530         pathbuf = xmalloc(namelen + 2);
531         memcpy(pathbuf, ce->name, namelen);
532         strcpy(pathbuf+namelen, "/");
534         memset(&d, 0, sizeof(d));
535         if (o->dir)
536                 d.exclude_per_dir = o->dir->exclude_per_dir;
537         i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
538         if (i)
539                 return o->gently ? -1 :
540                         error(ERRORMSG(o, not_uptodate_dir), ce->name);
541         free(pathbuf);
542         return cnt;
545 /*
546  * This gets called when there was no index entry for the tree entry 'dst',
547  * but we found a file in the working tree that 'lstat()' said was fine,
548  * and we're on a case-insensitive filesystem.
549  *
550  * See if we can find a case-insensitive match in the index that also
551  * matches the stat information, and assume it's that other file!
552  */
553 static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
555         struct cache_entry *src;
557         src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
558         return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID);
561 /*
562  * We do not want to remove or overwrite a working tree file that
563  * is not tracked, unless it is ignored.
564  */
565 static int verify_absent(struct cache_entry *ce, const char *action,
566                          struct unpack_trees_options *o)
568         struct stat st;
570         if (o->index_only || o->reset || !o->update)
571                 return 0;
573         if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
574                 return 0;
576         if (!lstat(ce->name, &st)) {
577                 int ret;
578                 int dtype = ce_to_dtype(ce);
579                 struct cache_entry *result;
581                 /*
582                  * It may be that the 'lstat()' succeeded even though
583                  * target 'ce' was absent, because there is an old
584                  * entry that is different only in case..
585                  *
586                  * Ignore that lstat() if it matches.
587                  */
588                 if (ignore_case && icase_exists(o, ce, &st))
589                         return 0;
591                 if (o->dir && excluded(o->dir, ce->name, &dtype))
592                         /*
593                          * ce->name is explicitly excluded, so it is Ok to
594                          * overwrite it.
595                          */
596                         return 0;
597                 if (S_ISDIR(st.st_mode)) {
598                         /*
599                          * We are checking out path "foo" and
600                          * found "foo/." in the working tree.
601                          * This is tricky -- if we have modified
602                          * files that are in "foo/" we would lose
603                          * it.
604                          */
605                         ret = verify_clean_subdirectory(ce, action, o);
606                         if (ret < 0)
607                                 return ret;
609                         /*
610                          * If this removed entries from the index,
611                          * what that means is:
612                          *
613                          * (1) the caller unpack_callback() saw path/foo
614                          * in the index, and it has not removed it because
615                          * it thinks it is handling 'path' as blob with
616                          * D/F conflict;
617                          * (2) we will return "ok, we placed a merged entry
618                          * in the index" which would cause o->pos to be
619                          * incremented by one;
620                          * (3) however, original o->pos now has 'path/foo'
621                          * marked with "to be removed".
622                          *
623                          * We need to increment it by the number of
624                          * deleted entries here.
625                          */
626                         o->pos += ret;
627                         return 0;
628                 }
630                 /*
631                  * The previous round may already have decided to
632                  * delete this path, which is in a subdirectory that
633                  * is being replaced with a blob.
634                  */
635                 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
636                 if (result) {
637                         if (result->ce_flags & CE_REMOVE)
638                                 return 0;
639                 }
641                 return o->gently ? -1 :
642                         error(ERRORMSG(o, would_lose_untracked), ce->name, action);
643         }
644         return 0;
647 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
648                 struct unpack_trees_options *o)
650         int update = CE_UPDATE;
652         if (old) {
653                 /*
654                  * See if we can re-use the old CE directly?
655                  * That way we get the uptodate stat info.
656                  *
657                  * This also removes the UPDATE flag on a match; otherwise
658                  * we will end up overwriting local changes in the work tree.
659                  */
660                 if (same(old, merge)) {
661                         copy_cache_entry(merge, old);
662                         update = 0;
663                 } else {
664                         if (verify_uptodate(old, o))
665                                 return -1;
666                         invalidate_ce_path(old, o);
667                 }
668         }
669         else {
670                 if (verify_absent(merge, "overwritten", o))
671                         return -1;
672                 invalidate_ce_path(merge, o);
673         }
675         add_entry(o, merge, update, CE_STAGEMASK);
676         return 1;
679 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
680                 struct unpack_trees_options *o)
682         /* Did it exist in the index? */
683         if (!old) {
684                 if (verify_absent(ce, "removed", o))
685                         return -1;
686                 return 0;
687         }
688         if (verify_uptodate(old, o))
689                 return -1;
690         add_entry(o, ce, CE_REMOVE, 0);
691         invalidate_ce_path(ce, o);
692         return 1;
695 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
697         add_entry(o, ce, 0, 0);
698         return 1;
701 #if DBRT_DEBUG
702 static void show_stage_entry(FILE *o,
703                              const char *label, const struct cache_entry *ce)
705         if (!ce)
706                 fprintf(o, "%s (missing)\n", label);
707         else
708                 fprintf(o, "%s%06o %s %d\t%s\n",
709                         label,
710                         ce->ce_mode,
711                         sha1_to_hex(ce->sha1),
712                         ce_stage(ce),
713                         ce->name);
715 #endif
717 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
719         struct cache_entry *index;
720         struct cache_entry *head;
721         struct cache_entry *remote = stages[o->head_idx + 1];
722         int count;
723         int head_match = 0;
724         int remote_match = 0;
726         int df_conflict_head = 0;
727         int df_conflict_remote = 0;
729         int any_anc_missing = 0;
730         int no_anc_exists = 1;
731         int i;
733         for (i = 1; i < o->head_idx; i++) {
734                 if (!stages[i] || stages[i] == o->df_conflict_entry)
735                         any_anc_missing = 1;
736                 else
737                         no_anc_exists = 0;
738         }
740         index = stages[0];
741         head = stages[o->head_idx];
743         if (head == o->df_conflict_entry) {
744                 df_conflict_head = 1;
745                 head = NULL;
746         }
748         if (remote == o->df_conflict_entry) {
749                 df_conflict_remote = 1;
750                 remote = NULL;
751         }
753         /* First, if there's a #16 situation, note that to prevent #13
754          * and #14.
755          */
756         if (!same(remote, head)) {
757                 for (i = 1; i < o->head_idx; i++) {
758                         if (same(stages[i], head)) {
759                                 head_match = i;
760                         }
761                         if (same(stages[i], remote)) {
762                                 remote_match = i;
763                         }
764                 }
765         }
767         /* We start with cases where the index is allowed to match
768          * something other than the head: #14(ALT) and #2ALT, where it
769          * is permitted to match the result instead.
770          */
771         /* #14, #14ALT, #2ALT */
772         if (remote && !df_conflict_head && head_match && !remote_match) {
773                 if (index && !same(index, remote) && !same(index, head))
774                         return o->gently ? -1 : reject_merge(index, o);
775                 return merged_entry(remote, index, o);
776         }
777         /*
778          * If we have an entry in the index cache, then we want to
779          * make sure that it matches head.
780          */
781         if (index && !same(index, head))
782                 return o->gently ? -1 : reject_merge(index, o);
784         if (head) {
785                 /* #5ALT, #15 */
786                 if (same(head, remote))
787                         return merged_entry(head, index, o);
788                 /* #13, #3ALT */
789                 if (!df_conflict_remote && remote_match && !head_match)
790                         return merged_entry(head, index, o);
791         }
793         /* #1 */
794         if (!head && !remote && any_anc_missing)
795                 return 0;
797         /* Under the new "aggressive" rule, we resolve mostly trivial
798          * cases that we historically had git-merge-one-file resolve.
799          */
800         if (o->aggressive) {
801                 int head_deleted = !head && !df_conflict_head;
802                 int remote_deleted = !remote && !df_conflict_remote;
803                 struct cache_entry *ce = NULL;
805                 if (index)
806                         ce = index;
807                 else if (head)
808                         ce = head;
809                 else if (remote)
810                         ce = remote;
811                 else {
812                         for (i = 1; i < o->head_idx; i++) {
813                                 if (stages[i] && stages[i] != o->df_conflict_entry) {
814                                         ce = stages[i];
815                                         break;
816                                 }
817                         }
818                 }
820                 /*
821                  * Deleted in both.
822                  * Deleted in one and unchanged in the other.
823                  */
824                 if ((head_deleted && remote_deleted) ||
825                     (head_deleted && remote && remote_match) ||
826                     (remote_deleted && head && head_match)) {
827                         if (index)
828                                 return deleted_entry(index, index, o);
829                         if (ce && !head_deleted) {
830                                 if (verify_absent(ce, "removed", o))
831                                         return -1;
832                         }
833                         return 0;
834                 }
835                 /*
836                  * Added in both, identically.
837                  */
838                 if (no_anc_exists && head && remote && same(head, remote))
839                         return merged_entry(head, index, o);
841         }
843         /* Below are "no merge" cases, which require that the index be
844          * up-to-date to avoid the files getting overwritten with
845          * conflict resolution files.
846          */
847         if (index) {
848                 if (verify_uptodate(index, o))
849                         return -1;
850         }
852         o->nontrivial_merge = 1;
854         /* #2, #3, #4, #6, #7, #9, #10, #11. */
855         count = 0;
856         if (!head_match || !remote_match) {
857                 for (i = 1; i < o->head_idx; i++) {
858                         if (stages[i] && stages[i] != o->df_conflict_entry) {
859                                 keep_entry(stages[i], o);
860                                 count++;
861                                 break;
862                         }
863                 }
864         }
865 #if DBRT_DEBUG
866         else {
867                 fprintf(stderr, "read-tree: warning #16 detected\n");
868                 show_stage_entry(stderr, "head   ", stages[head_match]);
869                 show_stage_entry(stderr, "remote ", stages[remote_match]);
870         }
871 #endif
872         if (head) { count += keep_entry(head, o); }
873         if (remote) { count += keep_entry(remote, o); }
874         return count;
877 /*
878  * Two-way merge.
879  *
880  * The rule is to "carry forward" what is in the index without losing
881  * information across a "fast forward", favoring a successful merge
882  * over a merge failure when it makes sense.  For details of the
883  * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
884  *
885  */
886 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
888         struct cache_entry *current = src[0];
889         struct cache_entry *oldtree = src[1];
890         struct cache_entry *newtree = src[2];
892         if (o->merge_size != 2)
893                 return error("Cannot do a twoway merge of %d trees",
894                              o->merge_size);
896         if (oldtree == o->df_conflict_entry)
897                 oldtree = NULL;
898         if (newtree == o->df_conflict_entry)
899                 newtree = NULL;
901         if (current) {
902                 if ((!oldtree && !newtree) || /* 4 and 5 */
903                     (!oldtree && newtree &&
904                      same(current, newtree)) || /* 6 and 7 */
905                     (oldtree && newtree &&
906                      same(oldtree, newtree)) || /* 14 and 15 */
907                     (oldtree && newtree &&
908                      !same(oldtree, newtree) && /* 18 and 19 */
909                      same(current, newtree))) {
910                         return keep_entry(current, o);
911                 }
912                 else if (oldtree && !newtree && same(current, oldtree)) {
913                         /* 10 or 11 */
914                         return deleted_entry(oldtree, current, o);
915                 }
916                 else if (oldtree && newtree &&
917                          same(current, oldtree) && !same(current, newtree)) {
918                         /* 20 or 21 */
919                         return merged_entry(newtree, current, o);
920                 }
921                 else {
922                         /* all other failures */
923                         if (oldtree)
924                                 return o->gently ? -1 : reject_merge(oldtree, o);
925                         if (current)
926                                 return o->gently ? -1 : reject_merge(current, o);
927                         if (newtree)
928                                 return o->gently ? -1 : reject_merge(newtree, o);
929                         return -1;
930                 }
931         }
932         else if (newtree) {
933                 if (oldtree && !o->initial_checkout) {
934                         /*
935                          * deletion of the path was staged;
936                          */
937                         if (same(oldtree, newtree))
938                                 return 1;
939                         return reject_merge(oldtree, o);
940                 }
941                 return merged_entry(newtree, current, o);
942         }
943         return deleted_entry(oldtree, current, o);
946 /*
947  * Bind merge.
948  *
949  * Keep the index entries at stage0, collapse stage1 but make sure
950  * stage0 does not have anything there.
951  */
952 int bind_merge(struct cache_entry **src,
953                 struct unpack_trees_options *o)
955         struct cache_entry *old = src[0];
956         struct cache_entry *a = src[1];
958         if (o->merge_size != 1)
959                 return error("Cannot do a bind merge of %d trees\n",
960                              o->merge_size);
961         if (a && old)
962                 return o->gently ? -1 :
963                         error(ERRORMSG(o, bind_overlap), a->name, old->name);
964         if (!a)
965                 return keep_entry(old, o);
966         else
967                 return merged_entry(a, NULL, o);
970 /*
971  * One-way merge.
972  *
973  * The rule is:
974  * - take the stat information from stage0, take the data from stage1
975  */
976 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
978         struct cache_entry *old = src[0];
979         struct cache_entry *a = src[1];
981         if (o->merge_size != 1)
982                 return error("Cannot do a oneway merge of %d trees",
983                              o->merge_size);
985         if (!a)
986                 return deleted_entry(old, old, o);
988         if (old && same(old, a)) {
989                 int update = 0;
990                 if (o->reset) {
991                         struct stat st;
992                         if (lstat(old->name, &st) ||
993                             ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
994                                 update |= CE_UPDATE;
995                 }
996                 add_entry(o, old, update, 0);
997                 return 0;
998         }
999         return merged_entry(a, old, o);