submodule-config.h: remove unnecessary include
[git.git] / t / helper / test-submodule.c
blob50c154d0370bfbdd91b70747133977a752f2b504
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 <name>"
13 static const char *submodule_check_name_usage[] = {
14 TEST_TOOL_CHECK_NAME_USAGE,
15 NULL
18 #define TEST_TOOL_IS_ACTIVE_USAGE \
19 "test-tool submodule is-active <name>"
20 static const char *submodule_is_active_usage[] = {
21 TEST_TOOL_IS_ACTIVE_USAGE,
22 NULL
25 #define TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE \
26 "test-tool submodule resolve-relative-url <up_path> <remoteurl> <url>"
27 static const char *submodule_resolve_relative_url_usage[] = {
28 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
29 NULL,
32 static const char *submodule_usage[] = {
33 TEST_TOOL_CHECK_NAME_USAGE,
34 TEST_TOOL_IS_ACTIVE_USAGE,
35 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
36 NULL
40 * Exit non-zero if any of the submodule names given on the command line is
41 * invalid. If no names are given, filter stdin to print only valid names
42 * (which is primarily intended for testing).
44 static int check_name(int argc, const char **argv)
46 if (argc > 1) {
47 while (*++argv) {
48 if (check_submodule_name(*argv) < 0)
49 return 1;
51 } else {
52 struct strbuf buf = STRBUF_INIT;
53 while (strbuf_getline(&buf, stdin) != EOF) {
54 if (!check_submodule_name(buf.buf))
55 printf("%s\n", buf.buf);
57 strbuf_release(&buf);
59 return 0;
62 static int cmd__submodule_check_name(int argc, const char **argv)
64 struct option options[] = {
65 OPT_END()
67 argc = parse_options(argc, argv, "test-tools", options,
68 submodule_check_name_usage, 0);
69 if (argc)
70 usage_with_options(submodule_check_name_usage, options);
72 return check_name(argc, argv);
75 static int cmd__submodule_is_active(int argc, const char **argv)
77 struct option options[] = {
78 OPT_END()
80 argc = parse_options(argc, argv, "test-tools", options,
81 submodule_is_active_usage, 0);
82 if (argc != 1)
83 usage_with_options(submodule_is_active_usage, options);
85 setup_git_directory();
87 return !is_submodule_active(the_repository, argv[0]);
90 static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
92 char *remoteurl, *res;
93 const char *up_path, *url;
94 struct option options[] = {
95 OPT_END()
97 argc = parse_options(argc, argv, "test-tools", options,
98 submodule_resolve_relative_url_usage, 0);
99 if (argc != 3)
100 usage_with_options(submodule_resolve_relative_url_usage, options);
102 up_path = argv[0];
103 remoteurl = xstrdup(argv[1]);
104 url = argv[2];
106 if (!strcmp(up_path, "(null)"))
107 up_path = NULL;
109 res = relative_url(remoteurl, url, up_path);
110 puts(res);
111 free(res);
112 free(remoteurl);
113 return 0;
116 static int cmd__submodule_config_list(int argc, const char **argv)
118 struct option options[] = {
119 OPT_END()
121 const char *const usage[] = {
122 "test-tool submodule config-list <key>",
123 NULL
125 argc = parse_options(argc, argv, "test-tools", options, usage,
126 PARSE_OPT_KEEP_ARGV0);
128 setup_git_directory();
130 if (argc == 2)
131 return print_config_from_gitmodules(the_repository, argv[1]);
132 usage_with_options(usage, options);
135 static int cmd__submodule_config_set(int argc, const char **argv)
137 struct option options[] = {
138 OPT_END()
140 const char *const usage[] = {
141 "test-tool submodule config-set <key> <value>",
142 NULL
144 argc = parse_options(argc, argv, "test-tools", options, usage,
145 PARSE_OPT_KEEP_ARGV0);
147 setup_git_directory();
149 /* Equivalent to ACTION_SET in builtin/config.c */
150 if (argc == 3) {
151 if (!is_writing_gitmodules_ok())
152 die("please make sure that the .gitmodules file is in the working tree");
154 return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
156 usage_with_options(usage, options);
159 static int cmd__submodule_config_unset(int argc, const char **argv)
161 struct option options[] = {
162 OPT_END()
164 const char *const usage[] = {
165 "test-tool submodule config-unset <key>",
166 NULL
169 setup_git_directory();
171 if (argc == 2) {
172 if (!is_writing_gitmodules_ok())
173 die("please make sure that the .gitmodules file is in the working tree");
174 return config_set_in_gitmodules_file_gently(argv[1], NULL);
176 usage_with_options(usage, options);
179 static int cmd__submodule_config_writeable(int argc, const char **argv UNUSED)
181 struct option options[] = {
182 OPT_END()
184 const char *const usage[] = {
185 "test-tool submodule config-writeable",
186 NULL
188 setup_git_directory();
190 if (argc == 1)
191 return is_writing_gitmodules_ok() ? 0 : -1;
193 usage_with_options(usage, options);
196 static struct test_cmd cmds[] = {
197 { "check-name", cmd__submodule_check_name },
198 { "is-active", cmd__submodule_is_active },
199 { "resolve-relative-url", cmd__submodule_resolve_relative_url},
200 { "config-list", cmd__submodule_config_list },
201 { "config-set", cmd__submodule_config_set },
202 { "config-unset", cmd__submodule_config_unset },
203 { "config-writeable", cmd__submodule_config_writeable },
206 int cmd__submodule(int argc, const char **argv)
208 struct option options[] = {
209 OPT_END()
211 size_t i;
213 argc = parse_options(argc, argv, "test-tools", options, submodule_usage,
214 PARSE_OPT_STOP_AT_NON_OPTION);
215 if (argc < 1)
216 usage_with_options(submodule_usage, options);
218 for (i = 0; i < ARRAY_SIZE(cmds); i++)
219 if (!strcmp(cmds[i].name, argv[0]))
220 return cmds[i].fn(argc, argv);
222 usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
223 argv[0]);
225 return 0;