read-tree: teach 1-way merege and plain read to prime cache-tree.
This teaches read-tree to fully populate valid cache-tree when reading a tree from scratch, or reading a single tree into an existing index, reusing only the cached stat information (i.e. one-way merge). We have already taught update-index about cache-tree, so "git checkout" followed by updates to a few path followed by a "git commit" would become very efficient. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Apr 27, 2006 at 01:33 UTC
7927a55d5bde25702dca4fb1a7d6eb7ef61110ba
3 files changed
+54
-3
cache-tree.c
+8
-3
@@ -91,6 +91,12 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
91
return down;
92
}
93
94
+struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
95
+{
96
+ int pathlen = strlen(path);
97
+ return find_subtree(it, path, pathlen, 1);
98
+}
99
+
100
void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
101
{
102
/* a/b/c
@@ -476,12 +482,11 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
482
struct cache_tree *sub;
483
struct cache_tree_sub *subtree;
484
const char *name = buf;
479
- int namelen;
485
+
486
sub = read_one(&buf, &size);
487
if (!sub)
488
goto free_return;
483
- namelen = strlen(name);
484
- subtree = find_subtree(it, name, namelen, 1);
489
+ subtree = cache_tree_sub(it, name);
490
subtree->cache_tree = sub;
491
}
492
if (subtree_nr != it->subtree_nr)
cache-tree.h
+1
@@ -20,6 +20,7 @@ struct cache_tree {
20
struct cache_tree *cache_tree(void);
21
void cache_tree_free(struct cache_tree **);
22
void cache_tree_invalidate_path(struct cache_tree *, const char *);
23
+struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *);
24
25
void *cache_tree_write(struct cache_tree *root, unsigned long *size_p);
26
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
read-tree.c
+45
@@ -725,6 +725,39 @@ static int read_cache_unmerged(void)
725
return deleted;
726
}
727
728
+static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
729
+{
730
+ struct tree_entry_list *ent;
731
+ int cnt;
732
+
733
+ memcpy(it->sha1, tree->object.sha1, 20);
734
+ for (cnt = 0, ent = tree->entries; ent; ent = ent->next) {
735
+ if (!ent->directory)
736
+ cnt++;
737
+ else {
738
+ struct cache_tree_sub *sub;
739
+ struct tree *subtree = (struct tree *)ent->item.tree;
740
+ if (!subtree->object.parsed)
741
+ parse_tree(subtree);
742
+ sub = cache_tree_sub(it, ent->name);
743
+ sub->cache_tree = cache_tree();
744
+ prime_cache_tree_rec(sub->cache_tree, subtree);
745
+ cnt += sub->cache_tree->entry_count;
746
+ }
747
+ }
748
+ it->entry_count = cnt;
749
+}
750
+
751
+static void prime_cache_tree(void)
752
+{
753
+ struct tree *tree = (struct tree *)trees->item;
754
+ if (!tree)
755
+ return;
756
+ active_cache_tree = cache_tree();
757
+ prime_cache_tree_rec(active_cache_tree, tree);
758
+
759
+}
760
+
761
static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
762
763
static struct cache_file cache_file;
@@ -839,6 +872,18 @@ int main(int argc, char **argv)
872
}
873
874
unpack_trees(fn);
875
+
876
+ /*
877
+ * When reading only one tree (either the most basic form,
878
+ * "-m ent" or "--reset ent" form), we can obtain a fully
879
+ * valid cache-tree because the index must match exactly
880
+ * what came from the tree.
881
+ */
882
+ if (trees->item && (!merge || (stage == 2))) {
883
+ cache_tree_free(&active_cache_tree);
884
+ prime_cache_tree();
885
+ }
886
+
887
if (write_cache(newfd, active_cache, active_nr) ||
888
commit_index_file(&cache_file))
889
die("unable to write new index file");