checkout branch: prime cache-tree fully

When switching to another branch, the earlier code relied on incremental
invalidation of cache-tree entries to degrade it.  While it is not wrong
per-se, we know that the resulting index must fully match the branch we
are switching to unless the -m (merge) option is used.

We should simply fully re-prime the cache-tree using the new tree object
in such a case.  And for safety, invalidate the cache-tree as a whole in
other cases.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff --git a/builtin-checkout.c b/builtin-checkout.c
index ffdb33a..efa1ebf 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -368,14 +368,17 @@
 	int ret;
 	struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
 	int newfd = hold_locked_index(lock_file, 1);
+	int reprime_cache_tree = 0;
 
 	if (read_cache() < 0)
 		return error("corrupt index file");
 
+	cache_tree_free(&active_cache_tree);
 	if (opts->force) {
 		ret = reset_tree(new->commit->tree, opts, 1);
 		if (ret)
 			return ret;
+		reprime_cache_tree = 1;
 	} else {
 		struct tree_desc trees[2];
 		struct tree *tree;
@@ -411,7 +414,9 @@
 		init_tree_desc(&trees[1], tree->buffer, tree->size);
 
 		ret = unpack_trees(2, trees, &topts);
-		if (ret == -1) {
+		if (ret != -1) {
+			reprime_cache_tree = 1;
+		} else {
 			/*
 			 * Unpack couldn't do a trivial merge; either
 			 * give up or do a real merge, depending on
@@ -455,6 +460,8 @@
 		}
 	}
 
+	if (reprime_cache_tree)
+		prime_cache_tree(&active_cache_tree, new->commit->tree);
 	if (write_cache(newfd, active_cache, active_nr) ||
 	    commit_locked_index(lock_file))
 		die("unable to write new index file");