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