t/helper: stop using `the_index`
[git.git] / t / helper / test-dump-cache-tree.c
blob02b0b46c3f93fae47949ac299bfaa9c1423eff2c
1 #include "test-tool.h"
2 #include "hash.h"
3 #include "hex.h"
4 #include "tree.h"
5 #include "cache-tree.h"
6 #include "read-cache-ll.h"
7 #include "repository.h"
8 #include "setup.h"
10 static void dump_one(struct cache_tree *it, const char *pfx, const char *x)
12 if (it->entry_count < 0)
13 printf("%-40s %s%s (%d subtrees)\n",
14 "invalid", x, pfx, it->subtree_nr);
15 else
16 printf("%s %s%s (%d entries, %d subtrees)\n",
17 oid_to_hex(&it->oid), x, pfx,
18 it->entry_count, it->subtree_nr);
21 static int dump_cache_tree(struct cache_tree *it,
22 struct cache_tree *ref,
23 const char *pfx)
25 int i;
26 int errs = 0;
28 if (!it || !ref)
29 /* missing in either */
30 return 0;
32 if (it->entry_count < 0) {
33 /* invalid */
34 dump_one(it, pfx, "");
35 dump_one(ref, pfx, "#(ref) ");
37 else {
38 dump_one(it, pfx, "");
39 if (!oideq(&it->oid, &ref->oid) ||
40 ref->entry_count != it->entry_count ||
41 ref->subtree_nr != it->subtree_nr) {
42 /* claims to be valid but is lying */
43 dump_one(ref, pfx, "#(ref) ");
44 errs = 1;
48 for (i = 0; i < it->subtree_nr; i++) {
49 char path[PATH_MAX];
50 struct cache_tree_sub *down = it->down[i];
51 struct cache_tree_sub *rdwn;
53 rdwn = cache_tree_sub(ref, down->name);
54 xsnprintf(path, sizeof(path), "%s%.*s/", pfx, down->namelen, down->name);
55 if (dump_cache_tree(down->cache_tree, rdwn->cache_tree, path))
56 errs = 1;
58 return errs;
61 int cmd__dump_cache_tree(int ac UNUSED, const char **av UNUSED)
63 struct index_state istate;
64 struct cache_tree *another = cache_tree();
65 int ret;
67 setup_git_directory();
68 if (repo_read_index(the_repository) < 0)
69 die("unable to read index file");
70 istate = *the_repository->index;
71 istate.cache_tree = another;
72 cache_tree_update(&istate, WRITE_TREE_DRY_RUN);
73 ret = dump_cache_tree(the_repository->index->cache_tree, another, "");
74 cache_tree_free(&another);
76 return ret;