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