1 #define USE_THE_REPOSITORY_VARIABLE
4 #include "test-tool-utils.h"
5 #include "parse-options.h"
7 #include "repository.h"
10 #include "submodule-config.h"
11 #include "submodule.h"
13 #define TEST_TOOL_CHECK_NAME_USAGE \
14 "test-tool submodule check-name"
15 static const char *submodule_check_name_usage
[] = {
16 TEST_TOOL_CHECK_NAME_USAGE
,
20 #define TEST_TOOL_CHECK_URL_USAGE \
21 "test-tool submodule check-url"
22 static const char *submodule_check_url_usage
[] = {
23 TEST_TOOL_CHECK_URL_USAGE
,
27 #define TEST_TOOL_IS_ACTIVE_USAGE \
28 "test-tool submodule is-active <name>"
29 static const char *submodule_is_active_usage
[] = {
30 TEST_TOOL_IS_ACTIVE_USAGE
,
34 #define TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE \
35 "test-tool submodule resolve-relative-url <up_path> <remoteurl> <url>"
36 static const char *submodule_resolve_relative_url_usage
[] = {
37 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE
,
41 static const char *submodule_usage
[] = {
42 TEST_TOOL_CHECK_NAME_USAGE
,
43 TEST_TOOL_CHECK_URL_USAGE
,
44 TEST_TOOL_IS_ACTIVE_USAGE
,
45 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE
,
49 typedef int (*check_fn_t
)(const char *);
52 * Apply 'check_fn' to each line of stdin, printing values that pass the check
55 static int check_submodule(check_fn_t check_fn
)
57 struct strbuf buf
= STRBUF_INIT
;
58 while (strbuf_getline(&buf
, stdin
) != EOF
) {
59 if (!check_fn(buf
.buf
))
60 printf("%s\n", buf
.buf
);
66 static int cmd__submodule_check_name(int argc
, const char **argv
)
68 struct option options
[] = {
71 argc
= parse_options(argc
, argv
, "test-tools", options
,
72 submodule_check_name_usage
, 0);
74 usage_with_options(submodule_check_name_usage
, options
);
76 return check_submodule(check_submodule_name
);
79 static int cmd__submodule_check_url(int argc
, const char **argv
)
81 struct option options
[] = {
84 argc
= parse_options(argc
, argv
, "test-tools", options
,
85 submodule_check_url_usage
, 0);
87 usage_with_options(submodule_check_url_usage
, options
);
89 return check_submodule(check_submodule_url
);
92 static int cmd__submodule_is_active(int argc
, const char **argv
)
94 struct option options
[] = {
97 argc
= parse_options(argc
, argv
, "test-tools", options
,
98 submodule_is_active_usage
, 0);
100 usage_with_options(submodule_is_active_usage
, options
);
102 setup_git_directory();
104 return !is_submodule_active(the_repository
, argv
[0]);
107 static int cmd__submodule_resolve_relative_url(int argc
, const char **argv
)
109 char *remoteurl
, *res
;
110 const char *up_path
, *url
;
111 struct option options
[] = {
114 argc
= parse_options(argc
, argv
, "test-tools", options
,
115 submodule_resolve_relative_url_usage
, 0);
117 usage_with_options(submodule_resolve_relative_url_usage
, options
);
120 remoteurl
= xstrdup(argv
[1]);
123 if (!strcmp(up_path
, "(null)"))
126 res
= relative_url(remoteurl
, url
, up_path
);
133 static int cmd__submodule_config_list(int argc
, const char **argv
)
135 struct option options
[] = {
138 const char *const usage
[] = {
139 "test-tool submodule config-list <key>",
142 argc
= parse_options(argc
, argv
, "test-tools", options
, usage
,
143 PARSE_OPT_KEEP_ARGV0
);
145 setup_git_directory();
148 return print_config_from_gitmodules(the_repository
, argv
[1]);
149 usage_with_options(usage
, options
);
152 static int cmd__submodule_config_set(int argc
, const char **argv
)
154 struct option options
[] = {
157 const char *const usage
[] = {
158 "test-tool submodule config-set <key> <value>",
161 argc
= parse_options(argc
, argv
, "test-tools", options
, usage
,
162 PARSE_OPT_KEEP_ARGV0
);
164 setup_git_directory();
166 /* Equivalent to ACTION_SET in builtin/config.c */
168 if (!is_writing_gitmodules_ok())
169 die("please make sure that the .gitmodules file is in the working tree");
171 return config_set_in_gitmodules_file_gently(argv
[1], argv
[2]);
173 usage_with_options(usage
, options
);
176 static int cmd__submodule_config_unset(int argc
, const char **argv
)
178 struct option options
[] = {
181 const char *const usage
[] = {
182 "test-tool submodule config-unset <key>",
186 setup_git_directory();
189 if (!is_writing_gitmodules_ok())
190 die("please make sure that the .gitmodules file is in the working tree");
191 return config_set_in_gitmodules_file_gently(argv
[1], NULL
);
193 usage_with_options(usage
, options
);
196 static int cmd__submodule_config_writeable(int argc
, const char **argv UNUSED
)
198 struct option options
[] = {
201 const char *const usage
[] = {
202 "test-tool submodule config-writeable",
205 setup_git_directory();
208 return is_writing_gitmodules_ok() ? 0 : -1;
210 usage_with_options(usage
, options
);
213 static struct test_cmd cmds
[] = {
214 { "check-name", cmd__submodule_check_name
},
215 { "check-url", cmd__submodule_check_url
},
216 { "is-active", cmd__submodule_is_active
},
217 { "resolve-relative-url", cmd__submodule_resolve_relative_url
},
218 { "config-list", cmd__submodule_config_list
},
219 { "config-set", cmd__submodule_config_set
},
220 { "config-unset", cmd__submodule_config_unset
},
221 { "config-writeable", cmd__submodule_config_writeable
},
224 int cmd__submodule(int argc
, const char **argv
)
226 struct option options
[] = {
231 argc
= parse_options(argc
, argv
, "test-tools", options
, submodule_usage
,
232 PARSE_OPT_STOP_AT_NON_OPTION
);
234 usage_with_options(submodule_usage
, options
);
236 for (i
= 0; i
< ARRAY_SIZE(cmds
); i
++)
237 if (!strcmp(cmds
[i
].name
, argv
[0]))
238 return cmds
[i
].fn(argc
, argv
);
240 usage_msg_optf("unknown subcommand '%s'", submodule_usage
, options
,