blob: c38f546e4f096fbd3e9b6cb13ab96bb800f56504 [file] [log] [blame]
Ævar Arnfjörð Bjarmasonbdafeae2022-11-19 14:07:37 +01001#define USE_THE_INDEX_VARIABLE
Nguyễn Thái Ngọc Duy06ccb292018-03-24 08:44:39 +01002#include "test-tool.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +00003#include "hash.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00004#include "hex.h"
Junio C Hamano17448202006-04-23 20:20:25 -07005#include "tree.h"
6#include "cache-tree.h"
Elijah Newren08c46a42023-05-16 06:33:56 +00007#include "read-cache-ll.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +00008#include "repository.h"
Elijah Newrene38da482023-03-21 06:26:05 +00009#include "setup.h"
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070010
11static 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",
brian m. carlsone0a92802017-05-01 02:28:56 +000018 oid_to_hex(&it->oid), x, pfx,
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070019 it->entry_count, it->subtree_nr);
20}
21
22static int dump_cache_tree(struct cache_tree *it,
23 struct cache_tree *ref,
24 const char *pfx)
Junio C Hamano17448202006-04-23 20:20:25 -070025{
26 int i;
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070027 int errs = 0;
28
Junio C Hamanoa84faf72006-05-03 15:32:54 -070029 if (!it || !ref)
30 /* missing in either */
31 return 0;
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070032
33 if (it->entry_count < 0) {
David Turner969dd8c2014-07-07 17:33:43 -070034 /* invalid */
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070035 dump_one(it, pfx, "");
36 dump_one(ref, pfx, "#(ref) ");
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070037 }
38 else {
39 dump_one(it, pfx, "");
Jeff King9001dc22018-08-28 17:22:48 -040040 if (!oideq(&it->oid, &ref->oid) ||
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070041 ref->entry_count != it->entry_count ||
42 ref->subtree_nr != it->subtree_nr) {
David Turner969dd8c2014-07-07 17:33:43 -070043 /* claims to be valid but is lying */
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070044 dump_one(ref, pfx, "#(ref) ");
45 errs = 1;
46 }
47 }
48
Junio C Hamano17448202006-04-23 20:20:25 -070049 for (i = 0; i < it->subtree_nr; i++) {
50 char path[PATH_MAX];
51 struct cache_tree_sub *down = it->down[i];
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070052 struct cache_tree_sub *rdwn;
53
54 rdwn = cache_tree_sub(ref, down->name);
Jeff King04724222015-09-24 17:06:03 -040055 xsnprintf(path, sizeof(path), "%s%.*s/", pfx, down->namelen, down->name);
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070056 if (dump_cache_tree(down->cache_tree, rdwn->cache_tree, path))
57 errs = 1;
Junio C Hamano17448202006-04-23 20:20:25 -070058 }
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070059 return errs;
Junio C Hamano17448202006-04-23 20:20:25 -070060}
61
Jeff King126e3b32023-03-28 16:57:25 -040062int cmd__dump_cache_tree(int ac UNUSED, const char **av UNUSED)
Junio C Hamano17448202006-04-23 20:20:25 -070063{
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +070064 struct index_state istate;
Junio C Hamanod2cb7c62006-04-27 16:22:45 -070065 struct cache_tree *another = cache_tree();
Ævar Arnfjörð Bjarmason9afa46d2022-07-01 12:37:34 +020066 int ret;
67
Jeff King4ce742f2016-10-20 02:16:59 -040068 setup_git_directory();
Ævar Arnfjörð Bjarmason0ea414a2022-11-19 14:07:35 +010069 if (repo_read_index(the_repository) < 0)
Junio C Hamano17448202006-04-23 20:20:25 -070070 die("unable to read index file");
Nguyễn Thái Ngọc Duyd0cfc3e2014-06-13 19:19:32 +070071 istate = the_index;
72 istate.cache_tree = another;
73 cache_tree_update(&istate, WRITE_TREE_DRY_RUN);
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +010074 ret = dump_cache_tree(the_index.cache_tree, another, "");
Ævar Arnfjörð Bjarmason9afa46d2022-07-01 12:37:34 +020075 cache_tree_free(&another);
76
77 return ret;
Junio C Hamano17448202006-04-23 20:20:25 -070078}