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