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