]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-submodule.c
The thirteenth batch
[thirdparty/git.git] / t / helper / test-submodule.c
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"
10
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
16 };
17
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
23 };
24
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
30 };
31
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,
37 };
38
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
45 };
46
47 typedef int (*check_fn_t)(const char *);
48
49 /*
50 * Apply 'check_fn' to each line of stdin, printing values that pass the check
51 * to stdout.
52 */
53 static int check_submodule(check_fn_t check_fn)
54 {
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);
59 }
60 strbuf_release(&buf);
61 return 0;
62 }
63
64 static int cmd__submodule_check_name(int argc, const char **argv)
65 {
66 struct option options[] = {
67 OPT_END()
68 };
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);
73
74 return check_submodule(check_submodule_name);
75 }
76
77 static int cmd__submodule_check_url(int argc, const char **argv)
78 {
79 struct option options[] = {
80 OPT_END()
81 };
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);
86
87 return check_submodule(check_submodule_url);
88 }
89
90 static int cmd__submodule_is_active(int argc, const char **argv)
91 {
92 struct option options[] = {
93 OPT_END()
94 };
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);
99
100 setup_git_directory();
101
102 return !is_submodule_active(the_repository, argv[0]);
103 }
104
105 static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
106 {
107 char *remoteurl, *res;
108 const char *up_path, *url;
109 struct option options[] = {
110 OPT_END()
111 };
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);
116
117 up_path = argv[0];
118 remoteurl = xstrdup(argv[1]);
119 url = argv[2];
120
121 if (!strcmp(up_path, "(null)"))
122 up_path = NULL;
123
124 res = relative_url(remoteurl, url, up_path);
125 puts(res);
126 free(res);
127 free(remoteurl);
128 return 0;
129 }
130
131 static int cmd__submodule_config_list(int argc, const char **argv)
132 {
133 struct option options[] = {
134 OPT_END()
135 };
136 const char *const usage[] = {
137 "test-tool submodule config-list <key>",
138 NULL
139 };
140 argc = parse_options(argc, argv, "test-tools", options, usage,
141 PARSE_OPT_KEEP_ARGV0);
142
143 setup_git_directory();
144
145 if (argc == 2)
146 return print_config_from_gitmodules(the_repository, argv[1]);
147 usage_with_options(usage, options);
148 }
149
150 static int cmd__submodule_config_set(int argc, const char **argv)
151 {
152 struct option options[] = {
153 OPT_END()
154 };
155 const char *const usage[] = {
156 "test-tool submodule config-set <key> <value>",
157 NULL
158 };
159 argc = parse_options(argc, argv, "test-tools", options, usage,
160 PARSE_OPT_KEEP_ARGV0);
161
162 setup_git_directory();
163
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");
168
169 return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
170 }
171 usage_with_options(usage, options);
172 }
173
174 static int cmd__submodule_config_unset(int argc, const char **argv)
175 {
176 struct option options[] = {
177 OPT_END()
178 };
179 const char *const usage[] = {
180 "test-tool submodule config-unset <key>",
181 NULL
182 };
183
184 setup_git_directory();
185
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);
190 }
191 usage_with_options(usage, options);
192 }
193
194 static int cmd__submodule_config_writeable(int argc, const char **argv UNUSED)
195 {
196 struct option options[] = {
197 OPT_END()
198 };
199 const char *const usage[] = {
200 "test-tool submodule config-writeable",
201 NULL
202 };
203 setup_git_directory();
204
205 if (argc == 1)
206 return is_writing_gitmodules_ok() ? 0 : -1;
207
208 usage_with_options(usage, options);
209 }
210
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 },
220 };
221
222 int cmd__submodule(int argc, const char **argv)
223 {
224 struct option options[] = {
225 OPT_END()
226 };
227 size_t i;
228
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);
233
234 for (i = 0; i < ARRAY_SIZE(cmds); i++)
235 if (!strcmp(cmds[i].name, argv[0]))
236 return cmds[i].fn(argc, argv);
237
238 usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
239 argv[0]);
240
241 return 0;
242 }