+ else { /* #3ALT */
+ if (!causes_df_conflict(b, 3, dst, next, tail) &&
+ (!old || same(b, old)))
+ return merged_entry_allow_dirty(b, old, dst);
+ }
+ /* otherwise we will apply the original rule */
+ }
+ /* #14ALT */
+ if (a && b && c && same(a, b) && !same(a, c)) {
+ if (old && same(old, c))
+ return merged_entry_allow_dirty(c, old, dst);
+ /* otherwise the regular rule applies */
+ }
+ /*
+ * If we have an entry in the index cache ("old"), then we want
+ * to make sure that it matches any entries in stage 2 ("first
+ * branch", aka "b").
+ */
+ if (old) {
+ if (!b || !same(old, b))
+ return -1;
+ }
+ merge = merge_entries(a, b, c);
+ if (merge)
+ return merged_entry(merge, old, dst);
+ if (old)
+ verify_uptodate(old);
+ count = 0;
+ if (a) { *dst++ = a; count++; }
+ if (b) { *dst++ = b; count++; }
+ if (c) { *dst++ = c; count++; }
+ return count;
+}
+
+/*
+ * Two-way merge.
+ *
+ * The rule is to "carry forward" what is in the index without losing
+ * information across a "fast forward", favoring a successful merge
+ * over a merge failure when it makes sense. For details of the
+ * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
+ *
+ */
+static int twoway_merge(struct cache_entry **src, struct cache_entry **dst,
+ struct cache_entry **next, int tail)
+{
+ struct cache_entry *current = src[0];
+ struct cache_entry *oldtree = src[1], *newtree = src[2];
+
+ if (src[3])
+ return -1;
+
+ if (current) {
+ if ((!oldtree && !newtree) || /* 4 and 5 */
+ (!oldtree && newtree &&
+ same(current, newtree)) || /* 6 and 7 */
+ (oldtree && newtree &&
+ same(oldtree, newtree)) || /* 14 and 15 */
+ (oldtree && newtree &&
+ !same(oldtree, newtree) && /* 18 and 19*/
+ same(current, newtree))) {
+ *dst++ = current;
+ return 1;
+ }
+ else if (oldtree && !newtree && same(current, oldtree)) {
+ /* 10 or 11 */
+ return deleted_entry(oldtree, current, dst);
+ }
+ else if (oldtree && newtree &&
+ same(current, oldtree) && !same(current, newtree)) {
+ /* 20 or 21 */
+ return merged_entry(newtree, current, dst);
+ }
+ else
+ /* all other failures */
+ return -1;
+ }
+ else if (newtree)
+ return merged_entry(newtree, current, dst);
+ else
+ return deleted_entry(oldtree, current, dst);
+}
+
+/*
+ * Two-way merge emulated with three-way merge.
+ *
+ * This treats "read-tree -m H M" by transforming it internally
+ * into "read-tree -m H I+H M", where I+H is a tree that would
+ * contain the contents of the current index file, overlayed on
+ * top of H. Unlike the traditional two-way merge, this leaves
+ * the stages in the resulting index file and lets the user resolve
+ * the merge conflicts using standard tools for three-way merge.
+ *
+ * This function is just to set-up such an arrangement, and the
+ * actual merge uses threeway_merge() function.
+ */
+static void setup_emu23(void)
+{
+ /* stage0 contains I, stage1 H, stage2 M.
+ * move stage2 to stage3, and create stage2 entries
+ * by scanning stage0 and stage1 entries.
+ */
+ int i, namelen, size;
+ struct cache_entry *ce, *stage2;
+
+ for (i = 0; i < active_nr; i++) {
+ ce = active_cache[i];
+ if (ce_stage(ce) != 2)
+ continue;
+ /* hoist them up to stage 3 */
+ namelen = ce_namelen(ce);
+ ce->ce_flags = create_ce_flags(namelen, 3);
+ }
+
+ for (i = 0; i < active_nr; i++) {
+ ce = active_cache[i];
+ if (ce_stage(ce) > 1)
+ continue;
+ namelen = ce_namelen(ce);
+ size = cache_entry_size(namelen);
+ stage2 = xmalloc(size);
+ memcpy(stage2, ce, size);
+ stage2->ce_flags = create_ce_flags(namelen, 2);
+ if (add_cache_entry(stage2, ADD_CACHE_OK_TO_ADD) < 0)
+ die("cannot merge index and our head tree");
+
+ /* We are done with this name, so skip to next name */
+ while (i < active_nr &&
+ ce_namelen(active_cache[i]) == namelen &&
+ !memcmp(active_cache[i]->name, ce->name, namelen))
+ i++;
+ i--; /* compensate for the loop control */
+ }
+}
+
+/*
+ * One-way merge.
+ *
+ * The rule is:
+ * - take the stat information from stage0, take the data from stage1
+ */
+static int oneway_merge(struct cache_entry **src, struct cache_entry **dst,
+ struct cache_entry **next, int tail)
+{
+ struct cache_entry *old = src[0];
+ struct cache_entry *a = src[1];
+
+ if (src[2] || src[3])
+ return -1;
+
+ if (!a)
+ return 0;
+ if (old && same(old, a)) {
+ *dst++ = old;
+ return 1;
+ }
+ return merged_entry(a, NULL, dst);
+}
+
+static void check_updates(struct cache_entry **src, int nr)
+{
+ static struct checkout state = {
+ .base_dir = "",
+ .force = 1,
+ .quiet = 1,
+ .refresh_cache = 1,
+ };
+ unsigned short mask = htons(CE_UPDATE);
+ while (nr--) {
+ struct cache_entry *ce = *src++;
+ if (!ce->ce_mode) {
+ if (update)
+ unlink(ce->name);
+ continue;
+ }
+ if (ce->ce_flags & mask) {
+ ce->ce_flags &= ~mask;
+ if (update)
+ checkout_entry(ce, &state);