2 #include "test-tool-utils.h"
3 #include "parse-options.h"
5 #include "repository.h"
7 #include "submodule-config.h"
10 #define TEST_TOOL_CHECK_NAME_USAGE \
11 "test-tool submodule check-name <name>"
12 static const char *submodule_check_name_usage
[] = {
13 TEST_TOOL_CHECK_NAME_USAGE
,
17 #define TEST_TOOL_IS_ACTIVE_USAGE \
18 "test-tool submodule is-active <name>"
19 static const char *submodule_is_active_usage
[] = {
20 TEST_TOOL_IS_ACTIVE_USAGE
,
24 #define TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE \
25 "test-tool submodule resolve-relative-url <up_path> <remoteurl> <url>"
26 static const char *submodule_resolve_relative_url_usage
[] = {
27 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE
,
31 static const char *submodule_usage
[] = {
32 TEST_TOOL_CHECK_NAME_USAGE
,
33 TEST_TOOL_IS_ACTIVE_USAGE
,
34 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE
,
39 * Exit non-zero if any of the submodule names given on the command line is
40 * invalid. If no names are given, filter stdin to print only valid names
41 * (which is primarily intended for testing).
43 static int check_name(int argc
, const char **argv
)
47 if (check_submodule_name(*argv
) < 0)
51 struct strbuf buf
= STRBUF_INIT
;
52 while (strbuf_getline(&buf
, stdin
) != EOF
) {
53 if (!check_submodule_name(buf
.buf
))
54 printf("%s\n", buf
.buf
);
61 static int cmd__submodule_check_name(int argc
, const char **argv
)
63 struct option options
[] = {
66 argc
= parse_options(argc
, argv
, "test-tools", options
,
67 submodule_check_name_usage
, 0);
69 usage_with_options(submodule_check_name_usage
, options
);
71 return check_name(argc
, argv
);
74 static int cmd__submodule_is_active(int argc
, const char **argv
)
76 struct option options
[] = {
79 argc
= parse_options(argc
, argv
, "test-tools", options
,
80 submodule_is_active_usage
, 0);
82 usage_with_options(submodule_is_active_usage
, options
);
84 setup_git_directory();
86 return !is_submodule_active(the_repository
, argv
[0]);
89 static int cmd__submodule_resolve_relative_url(int argc
, const char **argv
)
91 char *remoteurl
, *res
;
92 const char *up_path
, *url
;
93 struct option options
[] = {
96 argc
= parse_options(argc
, argv
, "test-tools", options
,
97 submodule_resolve_relative_url_usage
, 0);
99 usage_with_options(submodule_resolve_relative_url_usage
, options
);
102 remoteurl
= xstrdup(argv
[1]);
105 if (!strcmp(up_path
, "(null)"))
108 res
= relative_url(remoteurl
, url
, up_path
);
115 static int cmd__submodule_config_list(int argc
, const char **argv
)
117 struct option options
[] = {
120 const char *const usage
[] = {
121 "test-tool submodule config-list <key>",
124 argc
= parse_options(argc
, argv
, "test-tools", options
, usage
,
125 PARSE_OPT_KEEP_ARGV0
);
127 setup_git_directory();
130 return print_config_from_gitmodules(the_repository
, argv
[1]);
131 usage_with_options(usage
, options
);
134 static int cmd__submodule_config_set(int argc
, const char **argv
)
136 struct option options
[] = {
139 const char *const usage
[] = {
140 "test-tool submodule config-set <key> <value>",
143 argc
= parse_options(argc
, argv
, "test-tools", options
, usage
,
144 PARSE_OPT_KEEP_ARGV0
);
146 setup_git_directory();
148 /* Equivalent to ACTION_SET in builtin/config.c */
150 if (!is_writing_gitmodules_ok())
151 die("please make sure that the .gitmodules file is in the working tree");
153 return config_set_in_gitmodules_file_gently(argv
[1], argv
[2]);
155 usage_with_options(usage
, options
);
158 static int cmd__submodule_config_unset(int argc
, const char **argv
)
160 struct option options
[] = {
163 const char *const usage
[] = {
164 "test-tool submodule config-unset <key>",
168 setup_git_directory();
171 if (!is_writing_gitmodules_ok())
172 die("please make sure that the .gitmodules file is in the working tree");
173 return config_set_in_gitmodules_file_gently(argv
[1], NULL
);
175 usage_with_options(usage
, options
);
178 static int cmd__submodule_config_writeable(int argc
, const char **argv UNUSED
)
180 struct option options
[] = {
183 const char *const usage
[] = {
184 "test-tool submodule config-writeable",
187 setup_git_directory();
190 return is_writing_gitmodules_ok() ? 0 : -1;
192 usage_with_options(usage
, options
);
195 static struct test_cmd cmds
[] = {
196 { "check-name", cmd__submodule_check_name
},
197 { "is-active", cmd__submodule_is_active
},
198 { "resolve-relative-url", cmd__submodule_resolve_relative_url
},
199 { "config-list", cmd__submodule_config_list
},
200 { "config-set", cmd__submodule_config_set
},
201 { "config-unset", cmd__submodule_config_unset
},
202 { "config-writeable", cmd__submodule_config_writeable
},
205 int cmd__submodule(int argc
, const char **argv
)
207 struct option options
[] = {
212 argc
= parse_options(argc
, argv
, "test-tools", options
, submodule_usage
,
213 PARSE_OPT_STOP_AT_NON_OPTION
);
215 usage_with_options(submodule_usage
, options
);
217 for (i
= 0; i
< ARRAY_SIZE(cmds
); i
++)
218 if (!strcmp(cmds
[i
].name
, argv
[0]))
219 return cmds
[i
].fn(argc
, argv
);
221 usage_msg_optf("unknown subcommand '%s'", submodule_usage
, options
,