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 <name>"
13 static const char *submodule_check_name_usage
[] = {
14 TEST_TOOL_CHECK_NAME_USAGE
,
18 #define TEST_TOOL_IS_ACTIVE_USAGE \
19 "test-tool submodule is-active <name>"
20 static const char *submodule_is_active_usage
[] = {
21 TEST_TOOL_IS_ACTIVE_USAGE
,
25 #define TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE \
26 "test-tool submodule resolve-relative-url <up_path> <remoteurl> <url>"
27 static const char *submodule_resolve_relative_url_usage
[] = {
28 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE
,
32 static const char *submodule_usage
[] = {
33 TEST_TOOL_CHECK_NAME_USAGE
,
34 TEST_TOOL_IS_ACTIVE_USAGE
,
35 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE
,
40 * Exit non-zero if any of the submodule names given on the command line is
41 * invalid. If no names are given, filter stdin to print only valid names
42 * (which is primarily intended for testing).
44 static int check_name(int argc
, const char **argv
)
48 if (check_submodule_name(*argv
) < 0)
52 struct strbuf buf
= STRBUF_INIT
;
53 while (strbuf_getline(&buf
, stdin
) != EOF
) {
54 if (!check_submodule_name(buf
.buf
))
55 printf("%s\n", buf
.buf
);
62 static int cmd__submodule_check_name(int argc
, const char **argv
)
64 struct option options
[] = {
67 argc
= parse_options(argc
, argv
, "test-tools", options
,
68 submodule_check_name_usage
, 0);
70 usage_with_options(submodule_check_name_usage
, options
);
72 return check_name(argc
, argv
);
75 static int cmd__submodule_is_active(int argc
, const char **argv
)
77 struct option options
[] = {
80 argc
= parse_options(argc
, argv
, "test-tools", options
,
81 submodule_is_active_usage
, 0);
83 usage_with_options(submodule_is_active_usage
, options
);
85 setup_git_directory();
87 return !is_submodule_active(the_repository
, argv
[0]);
90 static int cmd__submodule_resolve_relative_url(int argc
, const char **argv
)
92 char *remoteurl
, *res
;
93 const char *up_path
, *url
;
94 struct option options
[] = {
97 argc
= parse_options(argc
, argv
, "test-tools", options
,
98 submodule_resolve_relative_url_usage
, 0);
100 usage_with_options(submodule_resolve_relative_url_usage
, options
);
103 remoteurl
= xstrdup(argv
[1]);
106 if (!strcmp(up_path
, "(null)"))
109 res
= relative_url(remoteurl
, url
, up_path
);
116 static int cmd__submodule_config_list(int argc
, const char **argv
)
118 struct option options
[] = {
121 const char *const usage
[] = {
122 "test-tool submodule config-list <key>",
125 argc
= parse_options(argc
, argv
, "test-tools", options
, usage
,
126 PARSE_OPT_KEEP_ARGV0
);
128 setup_git_directory();
131 return print_config_from_gitmodules(the_repository
, argv
[1]);
132 usage_with_options(usage
, options
);
135 static int cmd__submodule_config_set(int argc
, const char **argv
)
137 struct option options
[] = {
140 const char *const usage
[] = {
141 "test-tool submodule config-set <key> <value>",
144 argc
= parse_options(argc
, argv
, "test-tools", options
, usage
,
145 PARSE_OPT_KEEP_ARGV0
);
147 setup_git_directory();
149 /* Equivalent to ACTION_SET in builtin/config.c */
151 if (!is_writing_gitmodules_ok())
152 die("please make sure that the .gitmodules file is in the working tree");
154 return config_set_in_gitmodules_file_gently(argv
[1], argv
[2]);
156 usage_with_options(usage
, options
);
159 static int cmd__submodule_config_unset(int argc
, const char **argv
)
161 struct option options
[] = {
164 const char *const usage
[] = {
165 "test-tool submodule config-unset <key>",
169 setup_git_directory();
172 if (!is_writing_gitmodules_ok())
173 die("please make sure that the .gitmodules file is in the working tree");
174 return config_set_in_gitmodules_file_gently(argv
[1], NULL
);
176 usage_with_options(usage
, options
);
179 static int cmd__submodule_config_writeable(int argc
, const char **argv UNUSED
)
181 struct option options
[] = {
184 const char *const usage
[] = {
185 "test-tool submodule config-writeable",
188 setup_git_directory();
191 return is_writing_gitmodules_ok() ? 0 : -1;
193 usage_with_options(usage
, options
);
196 static struct test_cmd cmds
[] = {
197 { "check-name", cmd__submodule_check_name
},
198 { "is-active", cmd__submodule_is_active
},
199 { "resolve-relative-url", cmd__submodule_resolve_relative_url
},
200 { "config-list", cmd__submodule_config_list
},
201 { "config-set", cmd__submodule_config_set
},
202 { "config-unset", cmd__submodule_config_unset
},
203 { "config-writeable", cmd__submodule_config_writeable
},
206 int cmd__submodule(int argc
, const char **argv
)
208 struct option options
[] = {
213 argc
= parse_options(argc
, argv
, "test-tools", options
, submodule_usage
,
214 PARSE_OPT_STOP_AT_NON_OPTION
);
216 usage_with_options(submodule_usage
, options
);
218 for (i
= 0; i
< ARRAY_SIZE(cmds
); i
++)
219 if (!strcmp(cmds
[i
].name
, argv
[0]))
220 return cmds
[i
].fn(argc
, argv
);
222 usage_msg_optf("unknown subcommand '%s'", submodule_usage
, options
,