2 #include "test-tool-utils.h"
3 #include "parse-options.h"
5 #include "repository.h"
8 #include "submodule-config.h"
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
,
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
,
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
,
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
,
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
,
47 typedef int (*check_fn_t
)(const char *);
50 * Apply 'check_fn' to each line of stdin, printing values that pass the check
53 static int check_submodule(check_fn_t check_fn
)
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
);
64 static int cmd__submodule_check_name(int argc
, const char **argv
)
66 struct option options
[] = {
69 argc
= parse_options(argc
, argv
, "test-tools", options
,
70 submodule_check_name_usage
, 0);
72 usage_with_options(submodule_check_name_usage
, options
);
74 return check_submodule(check_submodule_name
);
77 static int cmd__submodule_check_url(int argc
, const char **argv
)
79 struct option options
[] = {
82 argc
= parse_options(argc
, argv
, "test-tools", options
,
83 submodule_check_url_usage
, 0);
85 usage_with_options(submodule_check_url_usage
, options
);
87 return check_submodule(check_submodule_url
);
90 static int cmd__submodule_is_active(int argc
, const char **argv
)
92 struct option options
[] = {
95 argc
= parse_options(argc
, argv
, "test-tools", options
,
96 submodule_is_active_usage
, 0);
98 usage_with_options(submodule_is_active_usage
, options
);
100 setup_git_directory();
102 return !is_submodule_active(the_repository
, argv
[0]);
105 static int cmd__submodule_resolve_relative_url(int argc
, const char **argv
)
107 char *remoteurl
, *res
;
108 const char *up_path
, *url
;
109 struct option options
[] = {
112 argc
= parse_options(argc
, argv
, "test-tools", options
,
113 submodule_resolve_relative_url_usage
, 0);
115 usage_with_options(submodule_resolve_relative_url_usage
, options
);
118 remoteurl
= xstrdup(argv
[1]);
121 if (!strcmp(up_path
, "(null)"))
124 res
= relative_url(remoteurl
, url
, up_path
);
131 static int cmd__submodule_config_list(int argc
, const char **argv
)
133 struct option options
[] = {
136 const char *const usage
[] = {
137 "test-tool submodule config-list <key>",
140 argc
= parse_options(argc
, argv
, "test-tools", options
, usage
,
141 PARSE_OPT_KEEP_ARGV0
);
143 setup_git_directory();
146 return print_config_from_gitmodules(the_repository
, argv
[1]);
147 usage_with_options(usage
, options
);
150 static int cmd__submodule_config_set(int argc
, const char **argv
)
152 struct option options
[] = {
155 const char *const usage
[] = {
156 "test-tool submodule config-set <key> <value>",
159 argc
= parse_options(argc
, argv
, "test-tools", options
, usage
,
160 PARSE_OPT_KEEP_ARGV0
);
162 setup_git_directory();
164 /* Equivalent to ACTION_SET in builtin/config.c */
166 if (!is_writing_gitmodules_ok())
167 die("please make sure that the .gitmodules file is in the working tree");
169 return config_set_in_gitmodules_file_gently(argv
[1], argv
[2]);
171 usage_with_options(usage
, options
);
174 static int cmd__submodule_config_unset(int argc
, const char **argv
)
176 struct option options
[] = {
179 const char *const usage
[] = {
180 "test-tool submodule config-unset <key>",
184 setup_git_directory();
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
);
191 usage_with_options(usage
, options
);
194 static int cmd__submodule_config_writeable(int argc
, const char **argv UNUSED
)
196 struct option options
[] = {
199 const char *const usage
[] = {
200 "test-tool submodule config-writeable",
203 setup_git_directory();
206 return is_writing_gitmodules_ok() ? 0 : -1;
208 usage_with_options(usage
, options
);
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
},
222 int cmd__submodule(int argc
, const char **argv
)
224 struct option options
[] = {
229 argc
= parse_options(argc
, argv
, "test-tools", options
, submodule_usage
,
230 PARSE_OPT_STOP_AT_NON_OPTION
);
232 usage_with_options(submodule_usage
, options
);
234 for (i
= 0; i
< ARRAY_SIZE(cmds
); i
++)
235 if (!strcmp(cmds
[i
].name
, argv
[0]))
236 return cmds
[i
].fn(argc
, argv
);
238 usage_msg_optf("unknown subcommand '%s'", submodule_usage
, options
,