Merge branch 'maint-1.6.1' into maint-1.6.2
[gitweb.git] / builtin-read-tree.c
index 5a09e17f1ad4fe19cf4e7cc29235d14d35405168..391d709704320dba7a918fdd79b68c6c604d33b2 100644 (file)
@@ -29,66 +29,7 @@ static int list_tree(unsigned char *sha1)
        return 0;
 }
 
-static int read_cache_unmerged(void)
-{
-       int i;
-       struct cache_entry **dst;
-       struct cache_entry *last = NULL;
-
-       read_cache();
-       dst = active_cache;
-       for (i = 0; i < active_nr; i++) {
-               struct cache_entry *ce = active_cache[i];
-               if (ce_stage(ce)) {
-                       remove_name_hash(ce);
-                       if (last && !strcmp(ce->name, last->name))
-                               continue;
-                       cache_tree_invalidate_path(active_cache_tree, ce->name);
-                       last = ce;
-                       continue;
-               }
-               *dst++ = ce;
-       }
-       active_nr = dst - active_cache;
-       return !!last;
-}
-
-static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
-{
-       struct tree_desc desc;
-       struct name_entry entry;
-       int cnt;
-
-       hashcpy(it->sha1, tree->object.sha1);
-       init_tree_desc(&desc, tree->buffer, tree->size);
-       cnt = 0;
-       while (tree_entry(&desc, &entry)) {
-               if (!S_ISDIR(entry.mode))
-                       cnt++;
-               else {
-                       struct cache_tree_sub *sub;
-                       struct tree *subtree = lookup_tree(entry.sha1);
-                       if (!subtree->object.parsed)
-                               parse_tree(subtree);
-                       sub = cache_tree_sub(it, entry.path);
-                       sub->cache_tree = cache_tree();
-                       prime_cache_tree_rec(sub->cache_tree, subtree);
-                       cnt += sub->cache_tree->entry_count;
-               }
-       }
-       it->entry_count = cnt;
-}
-
-static void prime_cache_tree(void)
-{
-       if (!nr_trees)
-               return;
-       active_cache_tree = cache_tree();
-       prime_cache_tree_rec(active_cache_tree, trees[0]);
-
-}
-
-static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <sha1> [<sha2> [<sha3>]])";
+static const char read_tree_usage[] = "git read-tree (<sha> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <sha1> [<sha2> [<sha3>]])";
 
 static struct lock_file lock_file;
 
@@ -218,6 +159,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
                usage(read_tree_usage);
        if ((opts.dir && !opts.update))
                die("--exclude-per-directory is meaningless unless -u");
+       if (opts.merge && !opts.index_only)
+               setup_work_tree();
 
        if (opts.merge) {
                if (stage < 2)
@@ -228,11 +171,11 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
                        break;
                case 2:
                        opts.fn = twoway_merge;
+                       opts.initial_checkout = is_cache_unborn();
                        break;
                case 3:
                default:
                        opts.fn = threeway_merge;
-                       cache_tree_free(&active_cache_tree);
                        break;
                }
 
@@ -242,6 +185,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
                        opts.head_idx = 1;
        }
 
+       cache_tree_free(&active_cache_tree);
        for (i = 0; i < nr_trees; i++) {
                struct tree *tree = trees[i];
                parse_tree(tree);
@@ -255,11 +199,14 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
         * "-m ent" or "--reset ent" form), we can obtain a fully
         * valid cache-tree because the index must match exactly
         * what came from the tree.
+        *
+        * The same holds true if we are switching between two trees
+        * using read-tree -m A B.  The index must match B after that.
         */
-       if (nr_trees && !opts.prefix && (!opts.merge || (stage == 2))) {
-               cache_tree_free(&active_cache_tree);
-               prime_cache_tree();
-       }
+       if (nr_trees == 1 && !opts.prefix)
+               prime_cache_tree(&active_cache_tree, trees[0]);
+       else if (nr_trees == 2 && opts.merge)
+               prime_cache_tree(&active_cache_tree, trees[1]);
 
        if (write_cache(newfd, active_cache, active_nr) ||
            commit_locked_index(&lock_file))