Merge branch 'kn/for-all-refs'
[git/gitster.git] / t / helper / test-submodule.c
blob7197969a08149699a14c758ceef1223e494b7c35
1 #include "test-tool.h"
2 #include "test-tool-utils.h"
3 #include "parse-options.h"
4 #include "remote.h"
5 #include "repository.h"
6 #include "setup.h"
7 #include "strbuf.h"
8 #include "submodule-config.h"
9 #include "submodule.h"
11 #define TEST_TOOL_CHECK_NAME_USAGE \
12 "test-tool submodule check-name"
13 static const char *submodule_check_name_usage[] = {
14 TEST_TOOL_CHECK_NAME_USAGE,
15 NULL
18 #define TEST_TOOL_CHECK_URL_USAGE \
19 "test-tool submodule check-url"
20 static const char *submodule_check_url_usage[] = {
21 TEST_TOOL_CHECK_URL_USAGE,
22 NULL
25 #define TEST_TOOL_IS_ACTIVE_USAGE \
26 "test-tool submodule is-active <name>"
27 static const char *submodule_is_active_usage[] = {
28 TEST_TOOL_IS_ACTIVE_USAGE,
29 NULL
32 #define TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE \
33 "test-tool submodule resolve-relative-url <up_path> <remoteurl> <url>"
34 static const char *submodule_resolve_relative_url_usage[] = {
35 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
36 NULL,
39 static const char *submodule_usage[] = {
40 TEST_TOOL_CHECK_NAME_USAGE,
41 TEST_TOOL_CHECK_URL_USAGE,
42 TEST_TOOL_IS_ACTIVE_USAGE,
43 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
44 NULL
47 typedef int (*check_fn_t)(const char *);
50 * Apply 'check_fn' to each line of stdin, printing values that pass the check
51 * to stdout.
53 static int check_submodule(check_fn_t check_fn)
55 struct strbuf buf = STRBUF_INIT;
56 while (strbuf_getline(&buf, stdin) != EOF) {
57 if (!check_fn(buf.buf))
58 printf("%s\n", buf.buf);
60 strbuf_release(&buf);
61 return 0;
64 static int cmd__submodule_check_name(int argc, const char **argv)
66 struct option options[] = {
67 OPT_END()
69 argc = parse_options(argc, argv, "test-tools", options,
70 submodule_check_name_usage, 0);
71 if (argc)
72 usage_with_options(submodule_check_name_usage, options);
74 return check_submodule(check_submodule_name);
77 static int cmd__submodule_check_url(int argc, const char **argv)
79 struct option options[] = {
80 OPT_END()
82 argc = parse_options(argc, argv, "test-tools", options,
83 submodule_check_url_usage, 0);
84 if (argc)
85 usage_with_options(submodule_check_url_usage, options);
87 return check_submodule(check_submodule_url);
90 static int cmd__submodule_is_active(int argc, const char **argv)
92 struct option options[] = {
93 OPT_END()
95 argc = parse_options(argc, argv, "test-tools", options,
96 submodule_is_active_usage, 0);
97 if (argc != 1)
98 usage_with_options(submodule_is_active_usage, options);
100 setup_git_directory();
102 return !is_submodule_active(the_repository, argv[0]);
105 static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
107 char *remoteurl, *res;
108 const char *up_path, *url;
109 struct option options[] = {
110 OPT_END()
112 argc = parse_options(argc, argv, "test-tools", options,
113 submodule_resolve_relative_url_usage, 0);
114 if (argc != 3)
115 usage_with_options(submodule_resolve_relative_url_usage, options);
117 up_path = argv[0];
118 remoteurl = xstrdup(argv[1]);
119 url = argv[2];
121 if (!strcmp(up_path, "(null)"))
122 up_path = NULL;
124 res = relative_url(remoteurl, url, up_path);
125 puts(res);
126 free(res);
127 free(remoteurl);
128 return 0;
131 static int cmd__submodule_config_list(int argc, const char **argv)
133 struct option options[] = {
134 OPT_END()
136 const char *const usage[] = {
137 "test-tool submodule config-list <key>",
138 NULL
140 argc = parse_options(argc, argv, "test-tools", options, usage,
141 PARSE_OPT_KEEP_ARGV0);
143 setup_git_directory();
145 if (argc == 2)
146 return print_config_from_gitmodules(the_repository, argv[1]);
147 usage_with_options(usage, options);
150 static int cmd__submodule_config_set(int argc, const char **argv)
152 struct option options[] = {
153 OPT_END()
155 const char *const usage[] = {
156 "test-tool submodule config-set <key> <value>",
157 NULL
159 argc = parse_options(argc, argv, "test-tools", options, usage,
160 PARSE_OPT_KEEP_ARGV0);
162 setup_git_directory();
164 /* Equivalent to ACTION_SET in builtin/config.c */
165 if (argc == 3) {
166 if (!is_writing_gitmodules_ok())
167 die("please make sure that the .gitmodules file is in the working tree");
169 return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
171 usage_with_options(usage, options);
174 static int cmd__submodule_config_unset(int argc, const char **argv)
176 struct option options[] = {
177 OPT_END()
179 const char *const usage[] = {
180 "test-tool submodule config-unset <key>",
181 NULL
184 setup_git_directory();
186 if (argc == 2) {
187 if (!is_writing_gitmodules_ok())
188 die("please make sure that the .gitmodules file is in the working tree");
189 return config_set_in_gitmodules_file_gently(argv[1], NULL);
191 usage_with_options(usage, options);
194 static int cmd__submodule_config_writeable(int argc, const char **argv UNUSED)
196 struct option options[] = {
197 OPT_END()
199 const char *const usage[] = {
200 "test-tool submodule config-writeable",
201 NULL
203 setup_git_directory();
205 if (argc == 1)
206 return is_writing_gitmodules_ok() ? 0 : -1;
208 usage_with_options(usage, options);
211 static struct test_cmd cmds[] = {
212 { "check-name", cmd__submodule_check_name },
213 { "check-url", cmd__submodule_check_url },
214 { "is-active", cmd__submodule_is_active },
215 { "resolve-relative-url", cmd__submodule_resolve_relative_url},
216 { "config-list", cmd__submodule_config_list },
217 { "config-set", cmd__submodule_config_set },
218 { "config-unset", cmd__submodule_config_unset },
219 { "config-writeable", cmd__submodule_config_writeable },
222 int cmd__submodule(int argc, const char **argv)
224 struct option options[] = {
225 OPT_END()
227 size_t i;
229 argc = parse_options(argc, argv, "test-tools", options, submodule_usage,
230 PARSE_OPT_STOP_AT_NON_OPTION);
231 if (argc < 1)
232 usage_with_options(submodule_usage, options);
234 for (i = 0; i < ARRAY_SIZE(cmds); i++)
235 if (!strcmp(cmds[i].name, argv[0]))
236 return cmds[i].fn(argc, argv);
238 usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
239 argv[0]);
241 return 0;