]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-submodule.c
The fifth batch
[thirdparty/git.git] / t / helper / test-submodule.c
CommitLineData
9fb2a970
ÆAB
1#include "test-tool.h"
2#include "test-tool-utils.h"
9fb2a970 3#include "parse-options.h"
96a28a9b 4#include "remote.h"
d1cbe1e6 5#include "repository.h"
e38da487 6#include "setup.h"
e9bb1664 7#include "strbuf.h"
85321a34 8#include "submodule-config.h"
9fb2a970
ÆAB
9#include "submodule.h"
10
85321a34 11#define TEST_TOOL_CHECK_NAME_USAGE \
6af2c4ad 12 "test-tool submodule check-name"
85321a34
ÆAB
13static const char *submodule_check_name_usage[] = {
14 TEST_TOOL_CHECK_NAME_USAGE,
15 NULL
16};
17
7e2fc39d
VD
18#define TEST_TOOL_CHECK_URL_USAGE \
19 "test-tool submodule check-url"
20static const char *submodule_check_url_usage[] = {
21 TEST_TOOL_CHECK_URL_USAGE,
22 NULL
23};
24
9fb2a970
ÆAB
25#define TEST_TOOL_IS_ACTIVE_USAGE \
26 "test-tool submodule is-active <name>"
27static const char *submodule_is_active_usage[] = {
28 TEST_TOOL_IS_ACTIVE_USAGE,
29 NULL
30};
31
96a28a9b
ÆAB
32#define TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE \
33 "test-tool submodule resolve-relative-url <up_path> <remoteurl> <url>"
34static const char *submodule_resolve_relative_url_usage[] = {
35 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
36 NULL,
37};
38
9fb2a970 39static const char *submodule_usage[] = {
85321a34 40 TEST_TOOL_CHECK_NAME_USAGE,
7e2fc39d 41 TEST_TOOL_CHECK_URL_USAGE,
9fb2a970 42 TEST_TOOL_IS_ACTIVE_USAGE,
96a28a9b 43 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
9fb2a970
ÆAB
44 NULL
45};
46
7e2fc39d
VD
47typedef int (*check_fn_t)(const char *);
48
85321a34 49/*
7e2fc39d
VD
50 * Apply 'check_fn' to each line of stdin, printing values that pass the check
51 * to stdout.
85321a34 52 */
7e2fc39d 53static int check_submodule(check_fn_t check_fn)
85321a34 54{
6af2c4ad
VD
55 struct strbuf buf = STRBUF_INIT;
56 while (strbuf_getline(&buf, stdin) != EOF) {
7e2fc39d 57 if (!check_fn(buf.buf))
6af2c4ad 58 printf("%s\n", buf.buf);
85321a34 59 }
6af2c4ad 60 strbuf_release(&buf);
85321a34
ÆAB
61 return 0;
62}
63
64static 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
7e2fc39d
VD
74 return check_submodule(check_submodule_name);
75}
76
77static 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);
85321a34
ÆAB
88}
89
9fb2a970
ÆAB
90static 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
6823c198 105static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
96a28a9b
ÆAB
106{
107 char *remoteurl, *res;
108 const char *up_path, *url;
6823c198
JK
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);
96a28a9b
ÆAB
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
cc74a4ac
ÆAB
131static 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
150static 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
174static 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
126e3b3d 194static int cmd__submodule_config_writeable(int argc, const char **argv UNUSED)
cc74a4ac
ÆAB
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
9fb2a970 211static struct test_cmd cmds[] = {
85321a34 212 { "check-name", cmd__submodule_check_name },
7e2fc39d 213 { "check-url", cmd__submodule_check_url },
9fb2a970 214 { "is-active", cmd__submodule_is_active },
96a28a9b 215 { "resolve-relative-url", cmd__submodule_resolve_relative_url},
cc74a4ac
ÆAB
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 },
9fb2a970
ÆAB
220};
221
222int 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}