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