Skip to content

Commit b9d37a5

Browse files
committed
Move prime_cache_tree() to cache-tree.c
The interface to build cache-tree belongs there. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8cc21ce commit b9d37a5

File tree

4 files changed

+40
-36
lines changed

4 files changed

+40
-36
lines changed

builtin-checkout.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "commit.h"
66
#include "tree.h"
77
#include "tree-walk.h"
8+
#include "cache-tree.h"
89
#include "unpack-trees.h"
910
#include "dir.h"
1011
#include "run-command.h"

builtin-read-tree.c

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,6 @@ static int list_tree(unsigned char *sha1)
2929
return 0;
3030
}
3131

32-
static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
33-
{
34-
struct tree_desc desc;
35-
struct name_entry entry;
36-
int cnt;
37-
38-
hashcpy(it->sha1, tree->object.sha1);
39-
init_tree_desc(&desc, tree->buffer, tree->size);
40-
cnt = 0;
41-
while (tree_entry(&desc, &entry)) {
42-
if (!S_ISDIR(entry.mode))
43-
cnt++;
44-
else {
45-
struct cache_tree_sub *sub;
46-
struct tree *subtree = lookup_tree(entry.sha1);
47-
if (!subtree->object.parsed)
48-
parse_tree(subtree);
49-
sub = cache_tree_sub(it, entry.path);
50-
sub->cache_tree = cache_tree();
51-
prime_cache_tree_rec(sub->cache_tree, subtree);
52-
cnt += sub->cache_tree->entry_count;
53-
}
54-
}
55-
it->entry_count = cnt;
56-
}
57-
58-
static void prime_cache_tree(void)
59-
{
60-
if (!nr_trees)
61-
return;
62-
active_cache_tree = cache_tree();
63-
prime_cache_tree_rec(active_cache_tree, trees[0]);
64-
65-
}
66-
6732
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>]])";
6833

6934
static struct lock_file lock_file;
@@ -236,7 +201,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
236201
* what came from the tree.
237202
*/
238203
if (nr_trees == 1 && !opts.prefix)
239-
prime_cache_tree();
204+
prime_cache_tree(&active_cache_tree, trees[0]);
240205

241206
if (write_cache(newfd, active_cache, active_nr) ||
242207
commit_locked_index(&lock_file))

cache-tree.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "tree.h"
3+
#include "tree-walk.h"
34
#include "cache-tree.h"
45

56
#ifndef DEBUG
@@ -591,3 +592,36 @@ int write_cache_as_tree(unsigned char *sha1, int missing_ok, const char *prefix)
591592

592593
return 0;
593594
}
595+
596+
static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
597+
{
598+
struct tree_desc desc;
599+
struct name_entry entry;
600+
int cnt;
601+
602+
hashcpy(it->sha1, tree->object.sha1);
603+
init_tree_desc(&desc, tree->buffer, tree->size);
604+
cnt = 0;
605+
while (tree_entry(&desc, &entry)) {
606+
if (!S_ISDIR(entry.mode))
607+
cnt++;
608+
else {
609+
struct cache_tree_sub *sub;
610+
struct tree *subtree = lookup_tree(entry.sha1);
611+
if (!subtree->object.parsed)
612+
parse_tree(subtree);
613+
sub = cache_tree_sub(it, entry.path);
614+
sub->cache_tree = cache_tree();
615+
prime_cache_tree_rec(sub->cache_tree, subtree);
616+
cnt += sub->cache_tree->entry_count;
617+
}
618+
}
619+
it->entry_count = cnt;
620+
}
621+
622+
void prime_cache_tree(struct cache_tree **it, struct tree *tree)
623+
{
624+
cache_tree_free(it);
625+
*it = cache_tree();
626+
prime_cache_tree_rec(*it, tree);
627+
}

cache-tree.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef CACHE_TREE_H
22
#define CACHE_TREE_H
33

4+
#include "tree.h"
5+
46
struct cache_tree;
57
struct cache_tree_sub {
68
struct cache_tree *cache_tree;
@@ -33,4 +35,6 @@ int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int, int)
3335
#define WRITE_TREE_PREFIX_ERROR (-3)
3436

3537
int write_cache_as_tree(unsigned char *sha1, int missing_ok, const char *prefix);
38+
void prime_cache_tree(struct cache_tree **, struct tree *);
39+
3640
#endif

0 commit comments

Comments
 (0)