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