Commit 347b232b authored by Duy Nguyen's avatar Duy Nguyen

submodule clone: use repo_config_set()

Avoid direct access to $SUBMODULE/config and do it through
repo_config_set() instead. Note that repo_submodule_init() cannot be
used because this early in the submodule initialization process, we
may fail to get and parse .gitmodules with submodule_from_path().
parent 17238436
......@@ -1370,11 +1370,11 @@ static int module_clone(int argc, const char **argv, const char *prefix)
const char *name = NULL, *url = NULL, *depth = NULL;
int quiet = 0;
int progress = 0;
char *p, *path = NULL, *sm_gitdir;
char *path = NULL, *sm_gitdir;
struct strbuf sb = STRBUF_INIT;
struct string_list reference = STRING_LIST_INIT_NODUP;
int dissociate = 0;
char *sm_alternate = NULL, *error_strategy = NULL;
struct repository subrepo;
struct option module_clone_options[] = {
OPT_STRING(0, "prefix", &prefix,
......@@ -1447,27 +1447,17 @@ static int module_clone(int argc, const char **argv, const char *prefix)
connect_work_tree_and_git_dir(path, sm_gitdir, 0);
p = git_pathdup_submodule(path, "config");
if (!p)
die(_("could not get submodule directory for '%s'"), path);
if (repo_submodule_init_early(&subrepo, the_repository, path, name))
die(_("could not get a repository handle for submodule '%s'"), path);
/* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
git_config_get_string("submodule.alternateLocation", &sm_alternate);
if (sm_alternate)
git_config_set_in_file(p, "submodule.alternateLocation",
sm_alternate);
git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
if (error_strategy)
git_config_set_in_file(p, "submodule.alternateErrorStrategy",
error_strategy);
free(sm_alternate);
free(error_strategy);
repo_config_copy(&subrepo, the_repository, "submodule.alternateLocation");
repo_config_copy(&subrepo, the_repository, "submodule.alternateErrorStrategy");
repo_clear(&subrepo);
strbuf_release(&sb);
free(sm_gitdir);
free(path);
free(p);
return 0;
}
......
......@@ -2159,6 +2159,16 @@ void repo_config_set(struct repository *r, const char *key, const char *value)
die(_("could not unset '%s'"), key);
}
void repo_config_copy(struct repository *dst, struct repository *src, const char *key)
{
char *value = NULL;
repo_config_get_string(src, key, &value);
if (value)
repo_config_set(dst, key, value);
free(value);
}
int repo_config_set_worktree_gently(struct repository *r,
const char *key, const char *value)
{
......
......@@ -105,6 +105,7 @@ extern void git_config_set_in_file(const char *, const char *, const char *);
extern int git_config_set_gently(const char *, const char *);
extern int repo_config_set_gently(struct repository *, const char *, const char *);
extern void repo_config_set(struct repository *, const char *, const char *);
extern void repo_config_copy(struct repository *dst, struct repository *src, const char *key);
extern int repo_config_set_worktree_gently(struct repository *, const char *, const char *);
extern void git_config_set(const char *, const char *);
extern int git_config_parse_key(const char *, char **, int *);
......
......@@ -185,18 +185,24 @@ int repo_init(struct repository *repo,
int repo_submodule_init(struct repository *subrepo,
struct repository *superproject,
const struct submodule *sub)
{
if (!sub)
return -1;
return repo_submodule_init_early(subrepo, superproject,
sub->path, sub->name);
}
int repo_submodule_init_early(struct repository *subrepo,
struct repository *superproject,
const char *path,
const char *name)
{
struct strbuf gitdir = STRBUF_INIT;
struct strbuf worktree = STRBUF_INIT;
int ret = 0;
if (!sub) {
ret = -1;
goto out;
}
strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", sub->path);
strbuf_repo_worktree_path(&worktree, superproject, "%s", sub->path);
strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
/*
......@@ -208,7 +214,7 @@ int repo_submodule_init(struct repository *subrepo,
*/
strbuf_reset(&gitdir);
strbuf_repo_git_path(&gitdir, superproject,
"modules/%s", sub->name);
"modules/%s", name);
if (repo_init(subrepo, gitdir.buf, NULL)) {
ret = -1;
......@@ -219,7 +225,7 @@ int repo_submodule_init(struct repository *subrepo,
subrepo->submodule_prefix = xstrfmt("%s%s/",
superproject->submodule_prefix ?
superproject->submodule_prefix :
"", sub->path);
"", path);
out:
strbuf_release(&gitdir);
......
......@@ -136,6 +136,14 @@ struct submodule;
int repo_submodule_init(struct repository *subrepo,
struct repository *superproject,
const struct submodule *sub);
/*
* Should only be used for early submodule initialization where the
* above function will not work
*/
int repo_submodule_init_early(struct repository *submodule,
struct repository *superproject,
const char *path,
const char *name);
void repo_clear(struct repository *repo);
/*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment