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