]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-submodule.c
Merge branch 'bb/unicode-width-table-15'
[thirdparty/git.git] / t / helper / test-submodule.c
1 #include "test-tool.h"
2 #include "test-tool-utils.h"
3 #include "parse-options.h"
4 #include "remote.h"
5 #include "repository.h"
6 #include "setup.h"
7 #include "submodule-config.h"
8 #include "submodule.h"
9
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,
14 NULL
15 };
16
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,
21 NULL
22 };
23
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,
28 NULL,
29 };
30
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,
35 NULL
36 };
37
38 /*
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).
42 */
43 static int check_name(int argc, const char **argv)
44 {
45 if (argc > 1) {
46 while (*++argv) {
47 if (check_submodule_name(*argv) < 0)
48 return 1;
49 }
50 } else {
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);
55 }
56 strbuf_release(&buf);
57 }
58 return 0;
59 }
60
61 static int cmd__submodule_check_name(int argc, const char **argv)
62 {
63 struct option options[] = {
64 OPT_END()
65 };
66 argc = parse_options(argc, argv, "test-tools", options,
67 submodule_check_name_usage, 0);
68 if (argc)
69 usage_with_options(submodule_check_name_usage, options);
70
71 return check_name(argc, argv);
72 }
73
74 static int cmd__submodule_is_active(int argc, const char **argv)
75 {
76 struct option options[] = {
77 OPT_END()
78 };
79 argc = parse_options(argc, argv, "test-tools", options,
80 submodule_is_active_usage, 0);
81 if (argc != 1)
82 usage_with_options(submodule_is_active_usage, options);
83
84 setup_git_directory();
85
86 return !is_submodule_active(the_repository, argv[0]);
87 }
88
89 static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
90 {
91 char *remoteurl, *res;
92 const char *up_path, *url;
93 struct option options[] = {
94 OPT_END()
95 };
96 argc = parse_options(argc, argv, "test-tools", options,
97 submodule_resolve_relative_url_usage, 0);
98 if (argc != 3)
99 usage_with_options(submodule_resolve_relative_url_usage, options);
100
101 up_path = argv[0];
102 remoteurl = xstrdup(argv[1]);
103 url = argv[2];
104
105 if (!strcmp(up_path, "(null)"))
106 up_path = NULL;
107
108 res = relative_url(remoteurl, url, up_path);
109 puts(res);
110 free(res);
111 free(remoteurl);
112 return 0;
113 }
114
115 static int cmd__submodule_config_list(int argc, const char **argv)
116 {
117 struct option options[] = {
118 OPT_END()
119 };
120 const char *const usage[] = {
121 "test-tool submodule config-list <key>",
122 NULL
123 };
124 argc = parse_options(argc, argv, "test-tools", options, usage,
125 PARSE_OPT_KEEP_ARGV0);
126
127 setup_git_directory();
128
129 if (argc == 2)
130 return print_config_from_gitmodules(the_repository, argv[1]);
131 usage_with_options(usage, options);
132 }
133
134 static int cmd__submodule_config_set(int argc, const char **argv)
135 {
136 struct option options[] = {
137 OPT_END()
138 };
139 const char *const usage[] = {
140 "test-tool submodule config-set <key> <value>",
141 NULL
142 };
143 argc = parse_options(argc, argv, "test-tools", options, usage,
144 PARSE_OPT_KEEP_ARGV0);
145
146 setup_git_directory();
147
148 /* Equivalent to ACTION_SET in builtin/config.c */
149 if (argc == 3) {
150 if (!is_writing_gitmodules_ok())
151 die("please make sure that the .gitmodules file is in the working tree");
152
153 return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
154 }
155 usage_with_options(usage, options);
156 }
157
158 static int cmd__submodule_config_unset(int argc, const char **argv)
159 {
160 struct option options[] = {
161 OPT_END()
162 };
163 const char *const usage[] = {
164 "test-tool submodule config-unset <key>",
165 NULL
166 };
167
168 setup_git_directory();
169
170 if (argc == 2) {
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);
174 }
175 usage_with_options(usage, options);
176 }
177
178 static int cmd__submodule_config_writeable(int argc, const char **argv UNUSED)
179 {
180 struct option options[] = {
181 OPT_END()
182 };
183 const char *const usage[] = {
184 "test-tool submodule config-writeable",
185 NULL
186 };
187 setup_git_directory();
188
189 if (argc == 1)
190 return is_writing_gitmodules_ok() ? 0 : -1;
191
192 usage_with_options(usage, options);
193 }
194
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 },
203 };
204
205 int cmd__submodule(int argc, const char **argv)
206 {
207 struct option options[] = {
208 OPT_END()
209 };
210 size_t i;
211
212 argc = parse_options(argc, argv, "test-tools", options, submodule_usage,
213 PARSE_OPT_STOP_AT_NON_OPTION);
214 if (argc < 1)
215 usage_with_options(submodule_usage, options);
216
217 for (i = 0; i < ARRAY_SIZE(cmds); i++)
218 if (!strcmp(cmds[i].name, argv[0]))
219 return cmds[i].fn(argc, argv);
220
221 usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
222 argv[0]);
223
224 return 0;
225 }