]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-submodule.c
Merge branch 'jk/clone-allow-bare-and-o-together'
[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 resolve_relative_url(int argc, const char **argv)
89 {
90 char *remoteurl, *res;
91 const char *up_path, *url;
92
93 up_path = argv[0];
94 remoteurl = xstrdup(argv[1]);
95 url = argv[2];
96
97 if (!strcmp(up_path, "(null)"))
98 up_path = NULL;
99
100 res = relative_url(remoteurl, url, up_path);
101 puts(res);
102 free(res);
103 free(remoteurl);
104 return 0;
105 }
106
107 static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
108 {
109 struct option options[] = {
110 OPT_END()
111 };
112 argc = parse_options(argc, argv, "test-tools", options,
113 submodule_resolve_relative_url_usage, 0);
114 if (argc != 3)
115 usage_with_options(submodule_resolve_relative_url_usage, options);
116
117 return resolve_relative_url(argc, argv);
118 }
119
120 static struct test_cmd cmds[] = {
121 { "check-name", cmd__submodule_check_name },
122 { "is-active", cmd__submodule_is_active },
123 { "resolve-relative-url", cmd__submodule_resolve_relative_url},
124 };
125
126 int cmd__submodule(int argc, const char **argv)
127 {
128 struct option options[] = {
129 OPT_END()
130 };
131 size_t i;
132
133 argc = parse_options(argc, argv, "test-tools", options, submodule_usage,
134 PARSE_OPT_STOP_AT_NON_OPTION);
135 if (argc < 1)
136 usage_with_options(submodule_usage, options);
137
138 for (i = 0; i < ARRAY_SIZE(cmds); i++)
139 if (!strcmp(cmds[i].name, argv[0]))
140 return cmds[i].fn(argc, argv);
141
142 usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
143 argv[0]);
144
145 return 0;
146 }