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