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