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