Merge branch 'ab/config-based-hooks-base' into seen
[git] / t / helper / test-tool.c
1 #include "git-compat-util.h"
2 #include "test-tool.h"
3 #include "trace2.h"
4 #include "parse-options.h"
5
6 static const char * const test_tool_usage[] = {
7         "test-tool [-C <directory>] <command [<arguments>...]]",
8         NULL
9 };
10
11 struct test_cmd {
12         const char *name;
13         int (*fn)(int argc, const char **argv);
14 };
15
16 static struct test_cmd cmds[] = {
17         { "advise", cmd__advise_if_enabled },
18         { "bitmap", cmd__bitmap },
19         { "bloom", cmd__bloom },
20         { "chmtime", cmd__chmtime },
21         { "config", cmd__config },
22         { "crontab", cmd__crontab },
23         { "ctype", cmd__ctype },
24         { "date", cmd__date },
25         { "delta", cmd__delta },
26         { "dir-iterator", cmd__dir_iterator },
27         { "drop-caches", cmd__drop_caches },
28         { "dump-cache-tree", cmd__dump_cache_tree },
29         { "dump-fsmonitor", cmd__dump_fsmonitor },
30         { "dump-split-index", cmd__dump_split_index },
31         { "dump-untracked-cache", cmd__dump_untracked_cache },
32         { "example-decorate", cmd__example_decorate },
33         { "fast-rebase", cmd__fast_rebase },
34         { "fsmonitor-client", cmd__fsmonitor_client },
35         { "genrandom", cmd__genrandom },
36         { "genzeros", cmd__genzeros },
37         { "hashmap", cmd__hashmap },
38         { "hash-speed", cmd__hash_speed },
39         { "index-version", cmd__index_version },
40         { "json-writer", cmd__json_writer },
41         { "lazy-init-name-hash", cmd__lazy_init_name_hash },
42         { "match-trees", cmd__match_trees },
43         { "mergesort", cmd__mergesort },
44         { "mktemp", cmd__mktemp },
45         { "oid-array", cmd__oid_array },
46         { "oidmap", cmd__oidmap },
47         { "online-cpus", cmd__online_cpus },
48         { "parse-options", cmd__parse_options },
49         { "parse-pathspec-file", cmd__parse_pathspec_file },
50         { "partial-clone", cmd__partial_clone },
51         { "path-utils", cmd__path_utils },
52         { "pcre2-config", cmd__pcre2_config },
53         { "pkt-line", cmd__pkt_line },
54         { "prio-queue", cmd__prio_queue },
55         { "proc-receive", cmd__proc_receive},
56         { "progress", cmd__progress },
57         { "reach", cmd__reach },
58         { "read-cache", cmd__read_cache },
59         { "read-cache-again", cmd__read_cache_again },
60         { "read-cache-perf", cmd__read_cache_perf },
61         { "read-graph", cmd__read_graph },
62         { "read-midx", cmd__read_midx },
63         { "ref-store", cmd__ref_store },
64         { "regex", cmd__regex },
65         { "repository", cmd__repository },
66         { "revision-walking", cmd__revision_walking },
67         { "run-command", cmd__run_command },
68         { "scrap-cache-tree", cmd__scrap_cache_tree },
69         { "serve-v2", cmd__serve_v2 },
70         { "sha1", cmd__sha1 },
71         { "sha256", cmd__sha256 },
72         { "sigchain", cmd__sigchain },
73         { "simple-ipc", cmd__simple_ipc },
74         { "strcmp-offset", cmd__strcmp_offset },
75         { "string-list", cmd__string_list },
76         { "submodule-config", cmd__submodule_config },
77         { "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
78         { "subprocess", cmd__subprocess },
79         { "trace2", cmd__trace2 },
80         { "userdiff", cmd__userdiff },
81         { "urlmatch-normalization", cmd__urlmatch_normalization },
82         { "xml-encode", cmd__xml_encode },
83         { "wildmatch", cmd__wildmatch },
84 #ifdef GIT_WINDOWS_NATIVE
85         { "windows-named-pipe", cmd__windows_named_pipe },
86 #endif
87         { "write-cache", cmd__write_cache },
88 };
89
90 static NORETURN void die_usage(void)
91 {
92         size_t i;
93
94         fprintf(stderr, "usage: test-tool <toolname> [args]\n");
95         for (i = 0; i < ARRAY_SIZE(cmds); i++)
96                 fprintf(stderr, "  %s\n", cmds[i].name);
97         exit(128);
98 }
99
100 int cmd_main(int argc, const char **argv)
101 {
102         int i;
103         const char *working_directory = NULL;
104         struct option options[] = {
105                 OPT_STRING('C', NULL, &working_directory, "directory",
106                            "change the working directory"),
107                 OPT_END()
108         };
109
110         BUG_exit_code = 99;
111         argc = parse_options(argc, argv, NULL, options, test_tool_usage,
112                              PARSE_OPT_STOP_AT_NON_OPTION |
113                              PARSE_OPT_KEEP_ARGV0);
114
115         if (argc < 2)
116                 die_usage();
117
118         if (working_directory && chdir(working_directory) < 0)
119                 die("Could not cd to '%s'", working_directory);
120
121         for (i = 0; i < ARRAY_SIZE(cmds); i++) {
122                 if (!strcmp(cmds[i].name, argv[1])) {
123                         argv++;
124                         argc--;
125                         trace2_cmd_name(cmds[i].name);
126                         trace2_cmd_list_config();
127                         trace2_cmd_list_env_vars();
128                         return cmds[i].fn(argc, argv);
129                 }
130         }
131         error("there is no tool named '%s'", argv[1]);
132         die_usage();
133 }