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