2022-08-31 23:17:48 +00:00
|
|
|
#include "test-tool.h"
|
|
|
|
#include "test-tool-utils.h"
|
|
|
|
#include "parse-options.h"
|
2022-08-31 23:17:50 +00:00
|
|
|
#include "remote.h"
|
2023-04-22 20:17:20 +00:00
|
|
|
#include "repository.h"
|
2023-03-21 06:26:05 +00:00
|
|
|
#include "setup.h"
|
2022-08-31 23:17:49 +00:00
|
|
|
#include "submodule-config.h"
|
2022-08-31 23:17:48 +00:00
|
|
|
#include "submodule.h"
|
|
|
|
|
2022-08-31 23:17:49 +00:00
|
|
|
#define TEST_TOOL_CHECK_NAME_USAGE \
|
|
|
|
"test-tool submodule check-name <name>"
|
|
|
|
static const char *submodule_check_name_usage[] = {
|
|
|
|
TEST_TOOL_CHECK_NAME_USAGE,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2022-08-31 23:17:48 +00:00
|
|
|
#define TEST_TOOL_IS_ACTIVE_USAGE \
|
|
|
|
"test-tool submodule is-active <name>"
|
|
|
|
static const char *submodule_is_active_usage[] = {
|
|
|
|
TEST_TOOL_IS_ACTIVE_USAGE,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2022-08-31 23:17:50 +00:00
|
|
|
#define TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE \
|
|
|
|
"test-tool submodule resolve-relative-url <up_path> <remoteurl> <url>"
|
|
|
|
static const char *submodule_resolve_relative_url_usage[] = {
|
|
|
|
TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2022-08-31 23:17:48 +00:00
|
|
|
static const char *submodule_usage[] = {
|
2022-08-31 23:17:49 +00:00
|
|
|
TEST_TOOL_CHECK_NAME_USAGE,
|
2022-08-31 23:17:48 +00:00
|
|
|
TEST_TOOL_IS_ACTIVE_USAGE,
|
2022-08-31 23:17:50 +00:00
|
|
|
TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
|
2022-08-31 23:17:48 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2022-08-31 23:17:49 +00:00
|
|
|
/*
|
|
|
|
* Exit non-zero if any of the submodule names given on the command line is
|
|
|
|
* invalid. If no names are given, filter stdin to print only valid names
|
|
|
|
* (which is primarily intended for testing).
|
|
|
|
*/
|
|
|
|
static int check_name(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
if (argc > 1) {
|
|
|
|
while (*++argv) {
|
|
|
|
if (check_submodule_name(*argv) < 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct strbuf buf = STRBUF_INIT;
|
|
|
|
while (strbuf_getline(&buf, stdin) != EOF) {
|
|
|
|
if (!check_submodule_name(buf.buf))
|
|
|
|
printf("%s\n", buf.buf);
|
|
|
|
}
|
|
|
|
strbuf_release(&buf);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd__submodule_check_name(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
argc = parse_options(argc, argv, "test-tools", options,
|
|
|
|
submodule_check_name_usage, 0);
|
|
|
|
if (argc)
|
|
|
|
usage_with_options(submodule_check_name_usage, options);
|
|
|
|
|
|
|
|
return check_name(argc, argv);
|
|
|
|
}
|
|
|
|
|
2022-08-31 23:17:48 +00:00
|
|
|
static int cmd__submodule_is_active(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
argc = parse_options(argc, argv, "test-tools", options,
|
|
|
|
submodule_is_active_usage, 0);
|
|
|
|
if (argc != 1)
|
|
|
|
usage_with_options(submodule_is_active_usage, options);
|
|
|
|
|
|
|
|
setup_git_directory();
|
|
|
|
|
|
|
|
return !is_submodule_active(the_repository, argv[0]);
|
|
|
|
}
|
|
|
|
|
2022-10-06 13:10:15 +00:00
|
|
|
static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
|
2022-08-31 23:17:50 +00:00
|
|
|
{
|
|
|
|
char *remoteurl, *res;
|
|
|
|
const char *up_path, *url;
|
2022-10-06 13:10:15 +00:00
|
|
|
struct option options[] = {
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
argc = parse_options(argc, argv, "test-tools", options,
|
|
|
|
submodule_resolve_relative_url_usage, 0);
|
|
|
|
if (argc != 3)
|
|
|
|
usage_with_options(submodule_resolve_relative_url_usage, options);
|
2022-08-31 23:17:50 +00:00
|
|
|
|
|
|
|
up_path = argv[0];
|
|
|
|
remoteurl = xstrdup(argv[1]);
|
|
|
|
url = argv[2];
|
|
|
|
|
|
|
|
if (!strcmp(up_path, "(null)"))
|
|
|
|
up_path = NULL;
|
|
|
|
|
|
|
|
res = relative_url(remoteurl, url, up_path);
|
|
|
|
puts(res);
|
|
|
|
free(res);
|
|
|
|
free(remoteurl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-08 14:10:32 +00:00
|
|
|
static int cmd__submodule_config_list(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
const char *const usage[] = {
|
|
|
|
"test-tool submodule config-list <key>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
argc = parse_options(argc, argv, "test-tools", options, usage,
|
|
|
|
PARSE_OPT_KEEP_ARGV0);
|
|
|
|
|
|
|
|
setup_git_directory();
|
|
|
|
|
|
|
|
if (argc == 2)
|
|
|
|
return print_config_from_gitmodules(the_repository, argv[1]);
|
|
|
|
usage_with_options(usage, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd__submodule_config_set(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
const char *const usage[] = {
|
|
|
|
"test-tool submodule config-set <key> <value>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
argc = parse_options(argc, argv, "test-tools", options, usage,
|
|
|
|
PARSE_OPT_KEEP_ARGV0);
|
|
|
|
|
|
|
|
setup_git_directory();
|
|
|
|
|
|
|
|
/* Equivalent to ACTION_SET in builtin/config.c */
|
|
|
|
if (argc == 3) {
|
|
|
|
if (!is_writing_gitmodules_ok())
|
|
|
|
die("please make sure that the .gitmodules file is in the working tree");
|
|
|
|
|
|
|
|
return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
|
|
|
|
}
|
|
|
|
usage_with_options(usage, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd__submodule_config_unset(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
const char *const usage[] = {
|
|
|
|
"test-tool submodule config-unset <key>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
setup_git_directory();
|
|
|
|
|
|
|
|
if (argc == 2) {
|
|
|
|
if (!is_writing_gitmodules_ok())
|
|
|
|
die("please make sure that the .gitmodules file is in the working tree");
|
|
|
|
return config_set_in_gitmodules_file_gently(argv[1], NULL);
|
|
|
|
}
|
|
|
|
usage_with_options(usage, options);
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:57:25 +00:00
|
|
|
static int cmd__submodule_config_writeable(int argc, const char **argv UNUSED)
|
2022-11-08 14:10:32 +00:00
|
|
|
{
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
const char *const usage[] = {
|
|
|
|
"test-tool submodule config-writeable",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
setup_git_directory();
|
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
return is_writing_gitmodules_ok() ? 0 : -1;
|
|
|
|
|
|
|
|
usage_with_options(usage, options);
|
|
|
|
}
|
|
|
|
|
2022-08-31 23:17:48 +00:00
|
|
|
static struct test_cmd cmds[] = {
|
2022-08-31 23:17:49 +00:00
|
|
|
{ "check-name", cmd__submodule_check_name },
|
2022-08-31 23:17:48 +00:00
|
|
|
{ "is-active", cmd__submodule_is_active },
|
2022-08-31 23:17:50 +00:00
|
|
|
{ "resolve-relative-url", cmd__submodule_resolve_relative_url},
|
2022-11-08 14:10:32 +00:00
|
|
|
{ "config-list", cmd__submodule_config_list },
|
|
|
|
{ "config-set", cmd__submodule_config_set },
|
|
|
|
{ "config-unset", cmd__submodule_config_unset },
|
|
|
|
{ "config-writeable", cmd__submodule_config_writeable },
|
2022-08-31 23:17:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int cmd__submodule(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, "test-tools", options, submodule_usage,
|
|
|
|
PARSE_OPT_STOP_AT_NON_OPTION);
|
|
|
|
if (argc < 1)
|
|
|
|
usage_with_options(submodule_usage, options);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(cmds); i++)
|
|
|
|
if (!strcmp(cmds[i].name, argv[0]))
|
|
|
|
return cmds[i].fn(argc, argv);
|
|
|
|
|
|
|
|
usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
|
|
|
|
argv[0]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|