]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-cache-tree.c
setup.h: move declarations for setup.c functions from cache.h
[thirdparty/git.git] / t / helper / test-cache-tree.c
1 #define USE_THE_INDEX_VARIABLE
2 #include "test-tool.h"
3 #include "cache.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "tree.h"
7 #include "cache-tree.h"
8 #include "parse-options.h"
9 #include "setup.h"
10
11 static char const * const test_cache_tree_usage[] = {
12 N_("test-tool cache-tree <options> (control|prime|update)"),
13 NULL
14 };
15
16 int cmd__cache_tree(int argc, const char **argv)
17 {
18 struct object_id oid;
19 struct tree *tree;
20 int empty = 0;
21 int invalidate_qty = 0;
22 int i;
23
24 struct option options[] = {
25 OPT_BOOL(0, "empty", &empty,
26 N_("clear the cache tree before each iteration")),
27 OPT_INTEGER_F(0, "invalidate", &invalidate_qty,
28 N_("number of entries in the cache tree to invalidate (default 0)"),
29 PARSE_OPT_NONEG),
30 OPT_END()
31 };
32
33 setup_git_directory();
34
35 argc = parse_options(argc, argv, NULL, options, test_cache_tree_usage, 0);
36
37 if (repo_read_index(the_repository) < 0)
38 die(_("unable to read index file"));
39
40 oidcpy(&oid, &the_index.cache_tree->oid);
41 tree = parse_tree_indirect(&oid);
42 if (!tree)
43 die(_("not a tree object: %s"), oid_to_hex(&oid));
44
45 if (empty) {
46 /* clear the cache tree & allocate a new one */
47 cache_tree_free(&the_index.cache_tree);
48 the_index.cache_tree = cache_tree();
49 } else if (invalidate_qty) {
50 /* invalidate the specified number of unique paths */
51 float f_interval = (float)the_index.cache_nr / invalidate_qty;
52 int interval = f_interval < 1.0 ? 1 : (int)f_interval;
53 for (i = 0; i < invalidate_qty && i * interval < the_index.cache_nr; i++)
54 cache_tree_invalidate_path(&the_index, the_index.cache[i * interval]->name);
55 }
56
57 if (argc != 1)
58 usage_with_options(test_cache_tree_usage, options);
59 else if (!strcmp(argv[0], "prime"))
60 prime_cache_tree(the_repository, &the_index, tree);
61 else if (!strcmp(argv[0], "update"))
62 cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
63 /* use "control" subcommand to specify no-op */
64 else if (!!strcmp(argv[0], "control"))
65 die(_("Unhandled subcommand '%s'"), argv[0]);
66
67 return 0;
68 }