git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] cache-tree: Create/update cache-tree on checkout

From
David Turner <dturner@twopensource.com>
Date
Jun 28, 2014, 00:20 UTC
Message-ID
<1403914856-3546-1-git-send-email-dturner@twitter.com>

When git checkout checks out a branch, create or update the cache-tree so that subsequent operations are faster.

Signed-off-by: David Turner <dturner@twitter.com>
---
 builtin/checkout.c    |  4 ++++
 cache-tree.c          | 22 ++++++++++++----------
 cache-tree.h          |  1 +
 t/t0090-cache-tree.sh | 15 ++++++++++++++-
 4 files changed, 31 insertions(+), 11 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 07cf555..df791e8 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -553,6 +553,10 @@ static int merge_working_tree(const struct checkout_opts *opts,
 		}
 	}
 
+	if (write_cache_as_tree(NULL, WRITE_TREE_DO_NOT_WRITE, "")) {
+		warn("Unable to write cache_tree");
+	}
+
 	if (write_cache(newfd, active_cache, active_nr) ||
 	    commit_locked_index(lock_file))
 		die(_("unable to write new index file"));
diff --git a/cache-tree.c b/cache-tree.c
index 7fa524a..fb203c6 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -568,17 +568,18 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
 
 int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
 {
-	int entries, was_valid, newfd;
+	int entries, was_valid, newfd = -1;
 	struct lock_file *lock_file;
 
-	/*
-	 * We can't free this memory, it becomes part of a linked list
-	 * parsed atexit()
-	 */
-	lock_file = xcalloc(1, sizeof(struct lock_file));
-
-	newfd = hold_locked_index(lock_file, 1);
+	if (!(flags & WRITE_TREE_DO_NOT_WRITE)) {
+		/*
+		 * We can't free this memory, it becomes part of a linked list
+		 * parsed atexit()
+		 */
+		lock_file = xcalloc(1, sizeof(struct lock_file));
 
+		newfd = hold_locked_index(lock_file, 1);
+	}
 	entries = read_cache();
 	if (entries < 0)
 		return WRITE_TREE_UNREADABLE_INDEX;
@@ -612,9 +613,10 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
 			cache_tree_find(active_cache_tree, prefix);
 		if (!subtree)
 			return WRITE_TREE_PREFIX_ERROR;
-		hashcpy(sha1, subtree->sha1);
+		if (sha1)
+			hashcpy(sha1, subtree->sha1);
 	}
-	else
+	else if (sha1)
 		hashcpy(sha1, active_cache_tree->sha1);
 
 	if (0 <= newfd)
diff --git a/cache-tree.h b/cache-tree.h
index f1923ad..daf6640 100644
--- a/cache-tree.h
+++ b/cache-tree.h
@@ -39,6 +39,7 @@ int update_main_cache_tree(int);
 #define WRITE_TREE_IGNORE_CACHE_TREE 2
 #define WRITE_TREE_DRY_RUN 4
 #define WRITE_TREE_SILENT 8
+#define WRITE_TREE_DO_NOT_WRITE 16
 
 /* error return codes */
 #define WRITE_TREE_UNREADABLE_INDEX (-1)
diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
index 6c33e28..7c60675 100755
--- a/t/t0090-cache-tree.sh
+++ b/t/t0090-cache-tree.sh
@@ -85,9 +85,22 @@ test_expect_success 'reset --hard without index gives cache-tree' '
 	test_shallow_cache_tree
 '
 
-test_expect_failure 'checkout gives cache-tree' '
+test_expect_success 'checkout gives cache-tree' '
+	git tag current
 	git checkout HEAD^ &&
 	test_shallow_cache_tree
 '
 
+test_expect_success 'checkout -b gives cache-tree' '
+	git checkout current &&
+	git checkout -b prev HEAD^ &&
+	test_shallow_cache_tree
+'
+
+test_expect_success 'checkout -B gives cache-tree' '
+	git checkout current &&
+	git checkout -B prev HEAD^ &&
+	test_shallow_cache_tree
+'
+
 test_done
-- 
2.0.0.390.gcb682f8
Next: David Turner
Message 1 of 7 in “cache-tree: Create/update cache-tree on checkout”
  1. 1/3 cache-tree: Create/update cache-tree on checkoutDavid Turner, Jun 28, 2014
  2. 2/3 test-dump-cache-tree: Improve output format and exit codeDavid Turner, Jun 28, 2014
  3. 3/3 cache-tree: Write index with updated cache-tree after commitDavid Turner, Jun 28, 2014
  4. Junio C HamanoJun 30, 2014
  5. Duy NguyenJun 29, 2014
  6. Junio C HamanoJun 30, 2014
  7. Junio C HamanoJun 30, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.