]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-submodule.c
commit -a -m: allow the top-level tree to become empty again
[thirdparty/git.git] / t / helper / test-submodule.c
1 #include "test-tool.h"
2 #include "test-tool-utils.h"
3 #include "cache.h"
4 #include "parse-options.h"
5 #include "remote.h"
6 #include "submodule-config.h"
7 #include "submodule.h"
8
9 #define TEST_TOOL_CHECK_NAME_USAGE \
10 "test-tool submodule check-name <name>"
11 static const char *submodule_check_name_usage[] = {
12 TEST_TOOL_CHECK_NAME_USAGE,
13 NULL
14 };
15
16 #define TEST_TOOL_IS_ACTIVE_USAGE \
17 "test-tool submodule is-active <name>"
18 static const char *submodule_is_active_usage[] = {
19 TEST_TOOL_IS_ACTIVE_USAGE,
20 NULL
21 };
22
23 #define TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE \
24 "test-tool submodule resolve-relative-url <up_path> <remoteurl> <url>"
25 static const char *submodule_resolve_relative_url_usage[] = {
26 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
27 NULL,
28 };
29
30 static const char *submodule_usage[] = {
31 TEST_TOOL_CHECK_NAME_USAGE,
32 TEST_TOOL_IS_ACTIVE_USAGE,
33 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
34 NULL
35 };
36
37 /*
38 * Exit non-zero if any of the submodule names given on the command line is
39 * invalid. If no names are given, filter stdin to print only valid names
40 * (which is primarily intended for testing).
41 */
42 static int check_name(int argc, const char **argv)
43 {
44 if (argc > 1) {
45 while (*++argv) {
46 if (check_submodule_name(*argv) < 0)
47 return 1;
48 }
49 } else {
50 struct strbuf buf = STRBUF_INIT;
51 while (strbuf_getline(&buf, stdin) != EOF) {
52 if (!check_submodule_name(buf.buf))
53 printf("%s\n", buf.buf);
54 }
55 strbuf_release(&buf);
56 }
57 return 0;
58 }
59
60 static int cmd__submodule_check_name(int argc, const char **argv)
61 {
62 struct option options[] = {
63 OPT_END()
64 };
65 argc = parse_options(argc, argv, "test-tools", options,
66 submodule_check_name_usage, 0);
67 if (argc)
68 usage_with_options(submodule_check_name_usage, options);
69
70 return check_name(argc, argv);
71 }
72
73 static int cmd__submodule_is_active(int argc, const char **argv)
74 {
75 struct option options[] = {
76 OPT_END()
77 };
78 argc = parse_options(argc, argv, "test-tools", options,
79 submodule_is_active_usage, 0);
80 if (argc != 1)
81 usage_with_options(submodule_is_active_usage, options);
82
83 setup_git_directory();
84
85 return !is_submodule_active(the_repository, argv[0]);
86 }
87
88 static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
89 {
90 char *remoteurl, *res;
91 const char *up_path, *url;
92 struct option options[] = {
93 OPT_END()
94 };
95 argc = parse_options(argc, argv, "test-tools", options,
96 submodule_resolve_relative_url_usage, 0);
97 if (argc != 3)
98 usage_with_options(submodule_resolve_relative_url_usage, options);
99
100 up_path = argv[0];
101 remoteurl = xstrdup(argv[1]);
102 url = argv[2];
103
104 if (!strcmp(up_path, "(null)"))
105 up_path = NULL;
106
107 res = relative_url(remoteurl, url, up_path);
108 puts(res);
109 free(res);
110 free(remoteurl);
111 return 0;
112 }
113
114 static int cmd__submodule_config_list(int argc, const char **argv)
115 {
116 struct option options[] = {
117 OPT_END()
118 };
119 const char *const usage[] = {
120 "test-tool submodule config-list <key>",
121 NULL
122 };
123 argc = parse_options(argc, argv, "test-tools", options, usage,
124 PARSE_OPT_KEEP_ARGV0);
125
126 setup_git_directory();
127
128 if (argc == 2)
129 return print_config_from_gitmodules(the_repository, argv[1]);
130 usage_with_options(usage, options);
131 }
132
133 static int cmd__submodule_config_set(int argc, const char **argv)
134 {
135 struct option options[] = {
136 OPT_END()
137 };
138 const char *const usage[] = {
139 "test-tool submodule config-set <key> <value>",
140 NULL
141 };
142 argc = parse_options(argc, argv, "test-tools", options, usage,
143 PARSE_OPT_KEEP_ARGV0);
144
145 setup_git_directory();
146
147 /* Equivalent to ACTION_SET in builtin/config.c */
148 if (argc == 3) {
149 if (!is_writing_gitmodules_ok())
150 die("please make sure that the .gitmodules file is in the working tree");
151
152 return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
153 }
154 usage_with_options(usage, options);
155 }
156
157 static int cmd__submodule_config_unset(int argc, const char **argv)
158 {
159 struct option options[] = {
160 OPT_END()
161 };
162 const char *const usage[] = {
163 "test-tool submodule config-unset <key>",
164 NULL
165 };
166
167 setup_git_directory();
168
169 if (argc == 2) {
170 if (!is_writing_gitmodules_ok())
171 die("please make sure that the .gitmodules file is in the working tree");
172 return config_set_in_gitmodules_file_gently(argv[1], NULL);
173 }
174 usage_with_options(usage, options);
175 }
176
177 static int cmd__submodule_config_writeable(int argc, const char **argv)
178 {
179 struct option options[] = {
180 OPT_END()
181 };
182 const char *const usage[] = {
183 "test-tool submodule config-writeable",
184 NULL
185 };
186 setup_git_directory();
187
188 if (argc == 1)
189 return is_writing_gitmodules_ok() ? 0 : -1;
190
191 usage_with_options(usage, options);
192 }
193
194 static struct test_cmd cmds[] = {
195 { "check-name", cmd__submodule_check_name },
196 { "is-active", cmd__submodule_is_active },
197 { "resolve-relative-url", cmd__submodule_resolve_relative_url},
198 { "config-list", cmd__submodule_config_list },
199 { "config-set", cmd__submodule_config_set },
200 { "config-unset", cmd__submodule_config_unset },
201 { "config-writeable", cmd__submodule_config_writeable },
202 };
203
204 int cmd__submodule(int argc, const char **argv)
205 {
206 struct option options[] = {
207 OPT_END()
208 };
209 size_t i;
210
211 argc = parse_options(argc, argv, "test-tools", options, submodule_usage,
212 PARSE_OPT_STOP_AT_NON_OPTION);
213 if (argc < 1)
214 usage_with_options(submodule_usage, options);
215
216 for (i = 0; i < ARRAY_SIZE(cmds); i++)
217 if (!strcmp(cmds[i].name, argv[0]))
218 return cmds[i].fn(argc, argv);
219
220 usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
221 argv[0]);
222
223 return 0;
224 }