Turn `git serve` into a test helper
[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 { "serve-v2", cmd__serve_v2 },
52 { "sha1", cmd__sha1 },
53 { "sha1-array", cmd__sha1_array },
54 { "sha256", cmd__sha256 },
55 { "sigchain", cmd__sigchain },
56 { "strcmp-offset", cmd__strcmp_offset },
57 { "string-list", cmd__string_list },
58 { "submodule-config", cmd__submodule_config },
59 { "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
60 { "subprocess", cmd__subprocess },
61 { "urlmatch-normalization", cmd__urlmatch_normalization },
62 { "xml-encode", cmd__xml_encode },
63 { "wildmatch", cmd__wildmatch },
64 #ifdef GIT_WINDOWS_NATIVE
65 { "windows-named-pipe", cmd__windows_named_pipe },
66 #endif
67 { "write-cache", cmd__write_cache },
68 };
69
70 static NORETURN void die_usage(void)
71 {
72 size_t i;
73
74 fprintf(stderr, "usage: test-tool <toolname> [args]\n");
75 for (i = 0; i < ARRAY_SIZE(cmds); i++)
76 fprintf(stderr, " %s\n", cmds[i].name);
77 exit(128);
78 }
79
80 int cmd_main(int argc, const char **argv)
81 {
82 int i;
83 const char *working_directory = NULL;
84 struct option options[] = {
85 OPT_STRING('C', NULL, &working_directory, "directory",
86 "change the working directory"),
87 OPT_END()
88 };
89
90 BUG_exit_code = 99;
91 argc = parse_options(argc, argv, NULL, options, test_tool_usage,
92 PARSE_OPT_STOP_AT_NON_OPTION |
93 PARSE_OPT_KEEP_ARGV0);
94
95 if (argc < 2)
96 die_usage();
97
98 if (working_directory && chdir(working_directory) < 0)
99 die("Could not cd to '%s'", working_directory);
100
101 for (i = 0; i < ARRAY_SIZE(cmds); i++) {
102 if (!strcmp(cmds[i].name, argv[1])) {
103 argv++;
104 argc--;
105 return cmds[i].fn(argc, argv);
106 }
107 }
108 error("there is no tool named '%s'", argv[1]);
109 die_usage();
110 }