Merge branch 'ab/submodule-helper-prep'

Code clean-up of "git submodule--helper".

* ab/submodule-helper-prep: (33 commits)
  submodule--helper: fix bad config API usage
  submodule--helper: libify even more "die" paths for module_update()
  submodule--helper: libify more "die" paths for module_update()
  submodule--helper: check repo{_submodule,}_init() return values
  submodule--helper: libify "must_die_on_failure" code paths (for die)
  submodule--helper update: don't override 'checkout' exit code
  submodule--helper: libify "must_die_on_failure" code paths
  submodule--helper: libify determine_submodule_update_strategy()
  submodule--helper: don't exit() on failure, return
  submodule--helper: use "code" in run_update_command()
  submodule API: don't handle SM_..{UNSPECIFIED,COMMAND} in to_string()
  submodule--helper: don't call submodule_strategy_to_string() in BUG()
  submodule--helper: add missing braces to "else" arm
  submodule--helper: return "ret", not "1" from update_submodule()
  submodule--helper: rename "int res" to "int ret"
  submodule--helper: don't redundantly check "else if (res)"
  submodule--helper: refactor "errmsg_str" to be a "struct strbuf"
  submodule--helper: add "const" to passed "struct update_data"
  submodule--helper: add "const" to copy of "update_data"
  submodule--helper: add "const" to passed "module_clone_data"
  ...
This commit is contained in:
Junio C Hamano 2022-09-13 11:38:23 -07:00
Родитель 0479138645 d4a492f4ad
Коммит f322e9f51b
16 изменённых файлов: 467 добавлений и 371 удалений

Просмотреть файл

@ -783,6 +783,7 @@ TEST_BUILTINS_OBJS += test-strcmp-offset.o
TEST_BUILTINS_OBJS += test-string-list.o
TEST_BUILTINS_OBJS += test-submodule-config.o
TEST_BUILTINS_OBJS += test-submodule-nested-repo-config.o
TEST_BUILTINS_OBJS += test-submodule.o
TEST_BUILTINS_OBJS += test-subprocess.o
TEST_BUILTINS_OBJS += test-trace2.o
TEST_BUILTINS_OBJS += test-urlmatch-normalization.o

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -569,8 +569,11 @@ static inline int git_has_dir_sep(const char *path)
/* The sentinel attribute is valid from gcc version 4.0 */
#if defined(__GNUC__) && (__GNUC__ >= 4)
#define LAST_ARG_MUST_BE_NULL __attribute__((sentinel))
/* warn_unused_result exists as of gcc 3.4.0, but be lazy and check 4.0 */
#define RESULT_MUST_BE_USED __attribute__ ((warn_unused_result))
#else
#define LAST_ARG_MUST_BE_NULL
#define RESULT_MUST_BE_USED
#endif
#define MAYBE_UNUSED __attribute__((__unused__))

Просмотреть файл

@ -1,6 +1,7 @@
#ifndef REPOSITORY_H
#define REPOSITORY_H
#include "git-compat-util.h"
#include "path.h"
struct config_set;
@ -186,6 +187,7 @@ void repo_set_gitdir(struct repository *repo, const char *root,
void repo_set_worktree(struct repository *repo, const char *path);
void repo_set_hash_algo(struct repository *repo, int algo);
void initialize_the_repository(void);
RESULT_MUST_BE_USED
int repo_init(struct repository *r, const char *gitdir, const char *worktree);
/*
@ -197,6 +199,7 @@ int repo_init(struct repository *r, const char *gitdir, const char *worktree);
* Return 0 upon success and a non-zero value upon failure.
*/
struct object_id;
RESULT_MUST_BE_USED
int repo_submodule_init(struct repository *subrepo,
struct repository *superproject,
const char *path,

Просмотреть файл

@ -415,10 +415,9 @@ int parse_submodule_update_strategy(const char *value,
return 0;
}
const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
const char *submodule_update_type_to_string(enum submodule_update_type type)
{
struct strbuf sb = STRBUF_INIT;
switch (s->type) {
switch (type) {
case SM_UPDATE_CHECKOUT:
return "checkout";
case SM_UPDATE_MERGE:
@ -428,12 +427,11 @@ const char *submodule_strategy_to_string(const struct submodule_update_strategy
case SM_UPDATE_NONE:
return "none";
case SM_UPDATE_UNSPECIFIED:
return NULL;
case SM_UPDATE_COMMAND:
strbuf_addf(&sb, "!%s", s->command);
return strbuf_detach(&sb, NULL);
BUG("init_submodule() should handle type %d", type);
default:
BUG("unexpected update strategy type: %d", type);
}
return NULL;
}
void handle_ignore_submodules_arg(struct diff_options *diffopt,

Просмотреть файл

@ -72,7 +72,7 @@ void die_path_inside_submodule(struct index_state *istate,
enum submodule_update_type parse_submodule_update_type(const char *value);
int parse_submodule_update_strategy(const char *value,
struct submodule_update_strategy *dst);
const char *submodule_strategy_to_string(const struct submodule_update_strategy *s);
const char *submodule_update_type_to_string(enum submodule_update_type type);
void handle_ignore_submodules_arg(struct diff_options *, const char *);
void show_submodule_diff_summary(struct diff_options *o, const char *path,
struct object_id *one, struct object_id *two,

Просмотреть файл

@ -15,14 +15,11 @@ int cmd__submodule_config(int argc, const char **argv)
{
const char **arg = argv;
int my_argc = argc;
int output_url = 0;
int lookup_name = 0;
arg++;
my_argc--;
while (arg[0] && starts_with(arg[0], "--")) {
if (!strcmp(arg[0], "--url"))
output_url = 1;
if (!strcmp(arg[0], "--name"))
lookup_name = 1;
arg++;
@ -57,12 +54,8 @@ int cmd__submodule_config(int argc, const char **argv)
if (!submodule)
die_usage(argc, argv, "Submodule not found.");
if (output_url)
printf("Submodule url: '%s' for path '%s'\n",
submodule->url, submodule->path);
else
printf("Submodule name: '%s' for path '%s'\n",
submodule->name, submodule->path);
printf("Submodule name: '%s' for path '%s'\n", submodule->name,
submodule->path);
arg += 2;
}

146
t/helper/test-submodule.c Normal file
Просмотреть файл

@ -0,0 +1,146 @@
#include "test-tool.h"
#include "test-tool-utils.h"
#include "cache.h"
#include "parse-options.h"
#include "remote.h"
#include "submodule-config.h"
#include "submodule.h"
#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
};
#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
};
#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,
};
static const char *submodule_usage[] = {
TEST_TOOL_CHECK_NAME_USAGE,
TEST_TOOL_IS_ACTIVE_USAGE,
TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
NULL
};
/*
* 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);
}
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]);
}
static int resolve_relative_url(int argc, const char **argv)
{
char *remoteurl, *res;
const char *up_path, *url;
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;
}
static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
{
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);
return resolve_relative_url(argc, argv);
}
static struct test_cmd cmds[] = {
{ "check-name", cmd__submodule_check_name },
{ "is-active", cmd__submodule_is_active },
{ "resolve-relative-url", cmd__submodule_resolve_relative_url},
};
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;
}

Просмотреть файл

@ -0,0 +1,9 @@
#ifndef TEST_TOOL_UTILS_H
#define TEST_TOOL_UTILS_H
struct test_cmd {
const char *name;
int (*fn)(int argc, const char **argv);
};
#endif

Просмотреть файл

@ -1,5 +1,6 @@
#include "git-compat-util.h"
#include "test-tool.h"
#include "test-tool-utils.h"
#include "trace2.h"
#include "parse-options.h"
@ -8,11 +9,6 @@ static const char * const test_tool_usage[] = {
NULL
};
struct test_cmd {
const char *name;
int (*fn)(int argc, const char **argv);
};
static struct test_cmd cmds[] = {
{ "advise", cmd__advise_if_enabled },
{ "bitmap", cmd__bitmap },
@ -81,6 +77,7 @@ static struct test_cmd cmds[] = {
{ "simple-ipc", cmd__simple_ipc },
{ "strcmp-offset", cmd__strcmp_offset },
{ "string-list", cmd__string_list },
{ "submodule", cmd__submodule },
{ "submodule-config", cmd__submodule_config },
{ "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
{ "subprocess", cmd__subprocess },

Просмотреть файл

@ -71,6 +71,7 @@ int cmd__sigchain(int argc, const char **argv);
int cmd__simple_ipc(int argc, const char **argv);
int cmd__strcmp_offset(int argc, const char **argv);
int cmd__string_list(int argc, const char **argv);
int cmd__submodule(int argc, const char **argv);
int cmd__submodule_config(int argc, const char **argv);
int cmd__submodule_nested_repo_config(int argc, const char **argv);
int cmd__subprocess(int argc, const char **argv);

Просмотреть файл

@ -22,7 +22,7 @@ relative_path() {
test_submodule_relative_url() {
test_expect_success "test_submodule_relative_url: $1 $2 $3 => $4" "
actual=\$(git submodule--helper resolve-relative-url-test '$1' '$2' '$3') &&
actual=\$(test-tool submodule resolve-relative-url '$1' '$2' '$3') &&
test \"\$actual\" = '$4'
"
}

Просмотреть файл

@ -14,6 +14,32 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh
test_expect_success 'submodule usage: -h' '
git submodule -h >out 2>err &&
grep "^usage: git submodule" out &&
test_must_be_empty err
'
test_expect_success 'submodule usage: --recursive' '
test_expect_code 1 git submodule --recursive >out 2>err &&
grep "^usage: git submodule" err &&
test_must_be_empty out
'
test_expect_success 'submodule usage: status --' '
test_expect_code 1 git submodule -- &&
test_expect_code 1 git submodule --end-of-options
'
for opt in '--quiet' '--cached'
do
test_expect_success "submodule usage: status $opt" '
git submodule $opt &&
git submodule status $opt &&
git submodule $opt status
'
done
test_expect_success 'submodule deinit works on empty repository' '
git submodule deinit --all
'
@ -152,6 +178,11 @@ test_expect_success 'submodule add' '
test_must_be_empty untracked
'
test_expect_success !WINDOWS 'submodule add (absolute path)' '
test_when_finished "git reset --hard" &&
git submodule add "$submodurl" "$submodurl/add-abs"
'
test_expect_success 'setup parent and one repository' '
test_create_repo parent &&
test_commit -C parent one
@ -1224,31 +1255,6 @@ test_expect_success 'submodule add clone shallow submodule' '
)
'
test_expect_success 'submodule helper list is not confused by common prefixes' '
mkdir -p dir1/b &&
(
cd dir1/b &&
git init &&
echo hi >testfile2 &&
git add . &&
git commit -m "test1"
) &&
mkdir -p dir2/b &&
(
cd dir2/b &&
git init &&
echo hello >testfile1 &&
git add . &&
git commit -m "test2"
) &&
git submodule add /dir1/b dir1/b &&
git submodule add /dir2/b dir2/b &&
git commit -m "first submodule commit" &&
git submodule--helper list dir1/b | cut -f 2 >actual &&
echo "dir1/b" >expect &&
test_cmp expect actual
'
test_expect_success 'setup superproject with submodules' '
git init sub1 &&
test_commit -C sub1 test &&

Просмотреть файл

@ -769,7 +769,7 @@ test_expect_success 'submodule update continues after recursive checkout error'
echo "" > file
)
) &&
test_must_fail git submodule update --recursive &&
test_expect_code 1 git submodule update --recursive &&
(cd submodule2 &&
git rev-parse --verify HEAD >../actual
) &&

Просмотреть файл

@ -1,9 +1,12 @@
#!/bin/sh
test_description='Test submodule--helper is-active
test_description='Test with test-tool submodule is-active
This test verifies that `git submodue--helper is-active` correctly identifies
This test verifies that `test-tool submodule is-active` correctly identifies
submodules which are "active" and interesting to the user.
This is a unit test of the submodule.c is_submodule_active() function,
which is also indirectly tested elsewhere.
'
. ./test-lib.sh
@ -25,13 +28,13 @@ test_expect_success 'setup' '
'
test_expect_success 'is-active works with urls' '
git -C super submodule--helper is-active sub1 &&
git -C super submodule--helper is-active sub2 &&
test-tool -C super submodule is-active sub1 &&
test-tool -C super submodule is-active sub2 &&
git -C super config --unset submodule.sub1.URL &&
test_must_fail git -C super submodule--helper is-active sub1 &&
test_must_fail test-tool -C super submodule is-active sub1 &&
git -C super config submodule.sub1.URL ../sub &&
git -C super submodule--helper is-active sub1
test-tool -C super submodule is-active sub1
'
test_expect_success 'is-active works with submodule.<name>.active config' '
@ -39,11 +42,11 @@ test_expect_success 'is-active works with submodule.<name>.active config' '
test_when_finished "git -C super config submodule.sub1.URL ../sub" &&
git -C super config --bool submodule.sub1.active "false" &&
test_must_fail git -C super submodule--helper is-active sub1 &&
test_must_fail test-tool -C super submodule is-active sub1 &&
git -C super config --bool submodule.sub1.active "true" &&
git -C super config --unset submodule.sub1.URL &&
git -C super submodule--helper is-active sub1
test-tool -C super submodule is-active sub1
'
test_expect_success 'is-active works with basic submodule.active config' '
@ -53,17 +56,17 @@ test_expect_success 'is-active works with basic submodule.active config' '
git -C super config --add submodule.active "." &&
git -C super config --unset submodule.sub1.URL &&
git -C super submodule--helper is-active sub1 &&
git -C super submodule--helper is-active sub2
test-tool -C super submodule is-active sub1 &&
test-tool -C super submodule is-active sub2
'
test_expect_success 'is-active correctly works with paths that are not submodules' '
test_when_finished "git -C super config --unset-all submodule.active" &&
test_must_fail git -C super submodule--helper is-active not-a-submodule &&
test_must_fail test-tool -C super submodule is-active not-a-submodule &&
git -C super config --add submodule.active "." &&
test_must_fail git -C super submodule--helper is-active not-a-submodule
test_must_fail test-tool -C super submodule is-active not-a-submodule
'
test_expect_success 'is-active works with exclusions in submodule.active config' '
@ -72,8 +75,8 @@ test_expect_success 'is-active works with exclusions in submodule.active config'
git -C super config --add submodule.active "." &&
git -C super config --add submodule.active ":(exclude)sub1" &&
test_must_fail git -C super submodule--helper is-active sub1 &&
git -C super submodule--helper is-active sub2
test_must_fail test-tool -C super submodule is-active sub1 &&
test-tool -C super submodule is-active sub2
'
test_expect_success 'is-active with submodule.active and submodule.<name>.active' '
@ -85,8 +88,8 @@ test_expect_success 'is-active with submodule.active and submodule.<name>.active
git -C super config --bool submodule.sub1.active "false" &&
git -C super config --bool submodule.sub2.active "true" &&
test_must_fail git -C super submodule--helper is-active sub1 &&
git -C super submodule--helper is-active sub2
test_must_fail test-tool -C super submodule is-active sub1 &&
test-tool -C super submodule is-active sub2
'
test_expect_success 'is-active, submodule.active and submodule add' '

Просмотреть файл

@ -21,7 +21,7 @@ test_expect_success 'check names' '
valid/with/paths
EOF
git submodule--helper check-name >actual <<-\EOF &&
test-tool submodule check-name >actual <<-\EOF &&
valid
valid/with/paths