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