trace2: add stopwatch timers
[alt-git.git] / t / helper / test-submodule.c
blobe0e0c53d38619ded93c916b5f11685cf616a1de2
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"
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
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
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,
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
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).
42 static int check_name(int argc, const char **argv)
44 if (argc > 1) {
45 while (*++argv) {
46 if (check_submodule_name(*argv) < 0)
47 return 1;
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);
55 strbuf_release(&buf);
57 return 0;
60 static int cmd__submodule_check_name(int argc, const char **argv)
62 struct option options[] = {
63 OPT_END()
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);
70 return check_name(argc, argv);
73 static int cmd__submodule_is_active(int argc, const char **argv)
75 struct option options[] = {
76 OPT_END()
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);
83 setup_git_directory();
85 return !is_submodule_active(the_repository, argv[0]);
88 static int resolve_relative_url(int argc, const char **argv)
90 char *remoteurl, *res;
91 const char *up_path, *url;
93 up_path = argv[0];
94 remoteurl = xstrdup(argv[1]);
95 url = argv[2];
97 if (!strcmp(up_path, "(null)"))
98 up_path = NULL;
100 res = relative_url(remoteurl, url, up_path);
101 puts(res);
102 free(res);
103 free(remoteurl);
104 return 0;
107 static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
109 struct option options[] = {
110 OPT_END()
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);
117 return resolve_relative_url(argc, argv);
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},
126 int cmd__submodule(int argc, const char **argv)
128 struct option options[] = {
129 OPT_END()
131 size_t i;
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);
138 for (i = 0; i < ARRAY_SIZE(cmds); i++)
139 if (!strcmp(cmds[i].name, argv[0]))
140 return cmds[i].fn(argc, argv);
142 usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
143 argv[0]);
145 return 0;