treewide: remove cache.h inclusion due to setup.h changes
[alt-git.git] / t / helper / test-submodule.c
blob0e34581b209e8aeab1d1db8e948fff4ee40075dd
1 #include "test-tool.h"
2 #include "test-tool-utils.h"
3 #include "parse-options.h"
4 #include "remote.h"
5 #include "setup.h"
6 #include "submodule-config.h"
7 #include "submodule.h"
9 #define TEST_TOOL_CHECK_NAME_USAGE \
10 "test-tool submodule check-name <name>"
11 static const char *submodule_check_name_usage[] = {
12 TEST_TOOL_CHECK_NAME_USAGE,
13 NULL
16 #define TEST_TOOL_IS_ACTIVE_USAGE \
17 "test-tool submodule is-active <name>"
18 static const char *submodule_is_active_usage[] = {
19 TEST_TOOL_IS_ACTIVE_USAGE,
20 NULL
23 #define TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE \
24 "test-tool submodule resolve-relative-url <up_path> <remoteurl> <url>"
25 static const char *submodule_resolve_relative_url_usage[] = {
26 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
27 NULL,
30 static const char *submodule_usage[] = {
31 TEST_TOOL_CHECK_NAME_USAGE,
32 TEST_TOOL_IS_ACTIVE_USAGE,
33 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
34 NULL
38 * Exit non-zero if any of the submodule names given on the command line is
39 * invalid. If no names are given, filter stdin to print only valid names
40 * (which is primarily intended for testing).
42 static int check_name(int argc, const char **argv)
44 if (argc > 1) {
45 while (*++argv) {
46 if (check_submodule_name(*argv) < 0)
47 return 1;
49 } else {
50 struct strbuf buf = STRBUF_INIT;
51 while (strbuf_getline(&buf, stdin) != EOF) {
52 if (!check_submodule_name(buf.buf))
53 printf("%s\n", buf.buf);
55 strbuf_release(&buf);
57 return 0;
60 static int cmd__submodule_check_name(int argc, const char **argv)
62 struct option options[] = {
63 OPT_END()
65 argc = parse_options(argc, argv, "test-tools", options,
66 submodule_check_name_usage, 0);
67 if (argc)
68 usage_with_options(submodule_check_name_usage, options);
70 return check_name(argc, argv);
73 static int cmd__submodule_is_active(int argc, const char **argv)
75 struct option options[] = {
76 OPT_END()
78 argc = parse_options(argc, argv, "test-tools", options,
79 submodule_is_active_usage, 0);
80 if (argc != 1)
81 usage_with_options(submodule_is_active_usage, options);
83 setup_git_directory();
85 return !is_submodule_active(the_repository, argv[0]);
88 static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
90 char *remoteurl, *res;
91 const char *up_path, *url;
92 struct option options[] = {
93 OPT_END()
95 argc = parse_options(argc, argv, "test-tools", options,
96 submodule_resolve_relative_url_usage, 0);
97 if (argc != 3)
98 usage_with_options(submodule_resolve_relative_url_usage, options);
100 up_path = argv[0];
101 remoteurl = xstrdup(argv[1]);
102 url = argv[2];
104 if (!strcmp(up_path, "(null)"))
105 up_path = NULL;
107 res = relative_url(remoteurl, url, up_path);
108 puts(res);
109 free(res);
110 free(remoteurl);
111 return 0;
114 static int cmd__submodule_config_list(int argc, const char **argv)
116 struct option options[] = {
117 OPT_END()
119 const char *const usage[] = {
120 "test-tool submodule config-list <key>",
121 NULL
123 argc = parse_options(argc, argv, "test-tools", options, usage,
124 PARSE_OPT_KEEP_ARGV0);
126 setup_git_directory();
128 if (argc == 2)
129 return print_config_from_gitmodules(the_repository, argv[1]);
130 usage_with_options(usage, options);
133 static int cmd__submodule_config_set(int argc, const char **argv)
135 struct option options[] = {
136 OPT_END()
138 const char *const usage[] = {
139 "test-tool submodule config-set <key> <value>",
140 NULL
142 argc = parse_options(argc, argv, "test-tools", options, usage,
143 PARSE_OPT_KEEP_ARGV0);
145 setup_git_directory();
147 /* Equivalent to ACTION_SET in builtin/config.c */
148 if (argc == 3) {
149 if (!is_writing_gitmodules_ok())
150 die("please make sure that the .gitmodules file is in the working tree");
152 return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
154 usage_with_options(usage, options);
157 static int cmd__submodule_config_unset(int argc, const char **argv)
159 struct option options[] = {
160 OPT_END()
162 const char *const usage[] = {
163 "test-tool submodule config-unset <key>",
164 NULL
167 setup_git_directory();
169 if (argc == 2) {
170 if (!is_writing_gitmodules_ok())
171 die("please make sure that the .gitmodules file is in the working tree");
172 return config_set_in_gitmodules_file_gently(argv[1], NULL);
174 usage_with_options(usage, options);
177 static int cmd__submodule_config_writeable(int argc, const char **argv)
179 struct option options[] = {
180 OPT_END()
182 const char *const usage[] = {
183 "test-tool submodule config-writeable",
184 NULL
186 setup_git_directory();
188 if (argc == 1)
189 return is_writing_gitmodules_ok() ? 0 : -1;
191 usage_with_options(usage, options);
194 static struct test_cmd cmds[] = {
195 { "check-name", cmd__submodule_check_name },
196 { "is-active", cmd__submodule_is_active },
197 { "resolve-relative-url", cmd__submodule_resolve_relative_url},
198 { "config-list", cmd__submodule_config_list },
199 { "config-set", cmd__submodule_config_set },
200 { "config-unset", cmd__submodule_config_unset },
201 { "config-writeable", cmd__submodule_config_writeable },
204 int cmd__submodule(int argc, const char **argv)
206 struct option options[] = {
207 OPT_END()
209 size_t i;
211 argc = parse_options(argc, argv, "test-tools", options, submodule_usage,
212 PARSE_OPT_STOP_AT_NON_OPTION);
213 if (argc < 1)
214 usage_with_options(submodule_usage, options);
216 for (i = 0; i < ARRAY_SIZE(cmds); i++)
217 if (!strcmp(cmds[i].name, argv[0]))
218 return cmds[i].fn(argc, argv);
220 usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
221 argv[0]);
223 return 0;