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