git-p4: fix typos
[git/gitster.git] / t / helper / test-cache-tree.c
blob5cdef3ebefc6281499f80a01456b8cf6c6944d5c
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "test-tool.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 "read-cache-ll.h"
10 #include "repository.h"
11 #include "setup.h"
13 static char const * const test_cache_tree_usage[] = {
14 N_("test-tool cache-tree <options> (control|prime|update)"),
15 NULL
18 int cmd__cache_tree(int argc, const char **argv)
20 struct object_id oid;
21 struct tree *tree;
22 int empty = 0;
23 int invalidate_qty = 0;
24 int i;
26 struct option options[] = {
27 OPT_BOOL(0, "empty", &empty,
28 N_("clear the cache tree before each iteration")),
29 OPT_INTEGER_F(0, "invalidate", &invalidate_qty,
30 N_("number of entries in the cache tree to invalidate (default 0)"),
31 PARSE_OPT_NONEG),
32 OPT_END()
35 setup_git_directory();
37 argc = parse_options(argc, argv, NULL, options, test_cache_tree_usage, 0);
39 if (repo_read_index(the_repository) < 0)
40 die(_("unable to read index file"));
42 oidcpy(&oid, &the_repository->index->cache_tree->oid);
43 tree = parse_tree_indirect(&oid);
44 if (!tree)
45 die(_("not a tree object: %s"), oid_to_hex(&oid));
47 if (empty) {
48 /* clear the cache tree & allocate a new one */
49 cache_tree_free(&the_repository->index->cache_tree);
50 the_repository->index->cache_tree = cache_tree();
51 } else if (invalidate_qty) {
52 /* invalidate the specified number of unique paths */
53 float f_interval = (float)the_repository->index->cache_nr / invalidate_qty;
54 int interval = f_interval < 1.0 ? 1 : (int)f_interval;
55 for (i = 0; i < invalidate_qty && i * interval < the_repository->index->cache_nr; i++)
56 cache_tree_invalidate_path(the_repository->index, the_repository->index->cache[i * interval]->name);
59 if (argc != 1)
60 usage_with_options(test_cache_tree_usage, options);
61 else if (!strcmp(argv[0], "prime"))
62 prime_cache_tree(the_repository, the_repository->index, tree);
63 else if (!strcmp(argv[0], "update"))
64 cache_tree_update(the_repository->index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
65 /* use "control" subcommand to specify no-op */
66 else if (!!strcmp(argv[0], "control"))
67 die(_("Unhandled subcommand '%s'"), argv[0]);
69 return 0;