git-p4: fix typos
[git/gitster.git] / t / helper / test-submodule.c
blob22e518d22905261fbc735383fd443e06ce7c203b
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "test-tool.h"
4 #include "test-tool-utils.h"
5 #include "parse-options.h"
6 #include "remote.h"
7 #include "repository.h"
8 #include "setup.h"
9 #include "strbuf.h"
10 #include "submodule-config.h"
11 #include "submodule.h"
13 #define TEST_TOOL_CHECK_NAME_USAGE \
14 "test-tool submodule check-name"
15 static const char *submodule_check_name_usage[] = {
16 TEST_TOOL_CHECK_NAME_USAGE,
17 NULL
20 #define TEST_TOOL_CHECK_URL_USAGE \
21 "test-tool submodule check-url"
22 static const char *submodule_check_url_usage[] = {
23 TEST_TOOL_CHECK_URL_USAGE,
24 NULL
27 #define TEST_TOOL_IS_ACTIVE_USAGE \
28 "test-tool submodule is-active <name>"
29 static const char *submodule_is_active_usage[] = {
30 TEST_TOOL_IS_ACTIVE_USAGE,
31 NULL
34 #define TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE \
35 "test-tool submodule resolve-relative-url <up_path> <remoteurl> <url>"
36 static const char *submodule_resolve_relative_url_usage[] = {
37 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
38 NULL,
41 static const char *submodule_usage[] = {
42 TEST_TOOL_CHECK_NAME_USAGE,
43 TEST_TOOL_CHECK_URL_USAGE,
44 TEST_TOOL_IS_ACTIVE_USAGE,
45 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
46 NULL
49 typedef int (*check_fn_t)(const char *);
52 * Apply 'check_fn' to each line of stdin, printing values that pass the check
53 * to stdout.
55 static int check_submodule(check_fn_t check_fn)
57 struct strbuf buf = STRBUF_INIT;
58 while (strbuf_getline(&buf, stdin) != EOF) {
59 if (!check_fn(buf.buf))
60 printf("%s\n", buf.buf);
62 strbuf_release(&buf);
63 return 0;
66 static int cmd__submodule_check_name(int argc, const char **argv)
68 struct option options[] = {
69 OPT_END()
71 argc = parse_options(argc, argv, "test-tools", options,
72 submodule_check_name_usage, 0);
73 if (argc)
74 usage_with_options(submodule_check_name_usage, options);
76 return check_submodule(check_submodule_name);
79 static int cmd__submodule_check_url(int argc, const char **argv)
81 struct option options[] = {
82 OPT_END()
84 argc = parse_options(argc, argv, "test-tools", options,
85 submodule_check_url_usage, 0);
86 if (argc)
87 usage_with_options(submodule_check_url_usage, options);
89 return check_submodule(check_submodule_url);
92 static int cmd__submodule_is_active(int argc, const char **argv)
94 struct option options[] = {
95 OPT_END()
97 argc = parse_options(argc, argv, "test-tools", options,
98 submodule_is_active_usage, 0);
99 if (argc != 1)
100 usage_with_options(submodule_is_active_usage, options);
102 setup_git_directory();
104 return !is_submodule_active(the_repository, argv[0]);
107 static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
109 char *remoteurl, *res;
110 const char *up_path, *url;
111 struct option options[] = {
112 OPT_END()
114 argc = parse_options(argc, argv, "test-tools", options,
115 submodule_resolve_relative_url_usage, 0);
116 if (argc != 3)
117 usage_with_options(submodule_resolve_relative_url_usage, options);
119 up_path = argv[0];
120 remoteurl = xstrdup(argv[1]);
121 url = argv[2];
123 if (!strcmp(up_path, "(null)"))
124 up_path = NULL;
126 res = relative_url(remoteurl, url, up_path);
127 puts(res);
128 free(res);
129 free(remoteurl);
130 return 0;
133 static int cmd__submodule_config_list(int argc, const char **argv)
135 struct option options[] = {
136 OPT_END()
138 const char *const usage[] = {
139 "test-tool submodule config-list <key>",
140 NULL
142 argc = parse_options(argc, argv, "test-tools", options, usage,
143 PARSE_OPT_KEEP_ARGV0);
145 setup_git_directory();
147 if (argc == 2)
148 return print_config_from_gitmodules(the_repository, argv[1]);
149 usage_with_options(usage, options);
152 static int cmd__submodule_config_set(int argc, const char **argv)
154 struct option options[] = {
155 OPT_END()
157 const char *const usage[] = {
158 "test-tool submodule config-set <key> <value>",
159 NULL
161 argc = parse_options(argc, argv, "test-tools", options, usage,
162 PARSE_OPT_KEEP_ARGV0);
164 setup_git_directory();
166 /* Equivalent to ACTION_SET in builtin/config.c */
167 if (argc == 3) {
168 if (!is_writing_gitmodules_ok())
169 die("please make sure that the .gitmodules file is in the working tree");
171 return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
173 usage_with_options(usage, options);
176 static int cmd__submodule_config_unset(int argc, const char **argv)
178 struct option options[] = {
179 OPT_END()
181 const char *const usage[] = {
182 "test-tool submodule config-unset <key>",
183 NULL
186 setup_git_directory();
188 if (argc == 2) {
189 if (!is_writing_gitmodules_ok())
190 die("please make sure that the .gitmodules file is in the working tree");
191 return config_set_in_gitmodules_file_gently(argv[1], NULL);
193 usage_with_options(usage, options);
196 static int cmd__submodule_config_writeable(int argc, const char **argv UNUSED)
198 struct option options[] = {
199 OPT_END()
201 const char *const usage[] = {
202 "test-tool submodule config-writeable",
203 NULL
205 setup_git_directory();
207 if (argc == 1)
208 return is_writing_gitmodules_ok() ? 0 : -1;
210 usage_with_options(usage, options);
213 static struct test_cmd cmds[] = {
214 { "check-name", cmd__submodule_check_name },
215 { "check-url", cmd__submodule_check_url },
216 { "is-active", cmd__submodule_is_active },
217 { "resolve-relative-url", cmd__submodule_resolve_relative_url},
218 { "config-list", cmd__submodule_config_list },
219 { "config-set", cmd__submodule_config_set },
220 { "config-unset", cmd__submodule_config_unset },
221 { "config-writeable", cmd__submodule_config_writeable },
224 int cmd__submodule(int argc, const char **argv)
226 struct option options[] = {
227 OPT_END()
229 size_t i;
231 argc = parse_options(argc, argv, "test-tools", options, submodule_usage,
232 PARSE_OPT_STOP_AT_NON_OPTION);
233 if (argc < 1)
234 usage_with_options(submodule_usage, options);
236 for (i = 0; i < ARRAY_SIZE(cmds); i++)
237 if (!strcmp(cmds[i].name, argv[0]))
238 return cmds[i].fn(argc, argv);
240 usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
241 argv[0]);
243 return 0;