From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Stefan Beller" <sbeller@google.com>,
tsniatowski@vewd.com, "Jonathan Nieder" <jrnieder@gmail.com>,
marcnarc@xiplink.com, "Junio C Hamano" <gitster@pobox.com>,
"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 08/10] submodule clone: use repo_config_set()
Date: Wed, 16 Jan 2019 17:31:57 +0700 [thread overview]
Message-ID: <20190116103159.9305-9-pclouds@gmail.com> (raw)
In-Reply-To: <20190116103159.9305-1-pclouds@gmail.com>
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().
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
builtin/submodule--helper.c | 24 +++++++-----------------
config.c | 10 ++++++++++
config.h | 1 +
repository.c | 21 ++++++++++++++-------
repository.h | 4 ++++
5 files changed, 36 insertions(+), 24 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 8943127ae7..b5d74cd415 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1366,11 +1366,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,
@@ -1443,27 +1443,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_by_name(&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;
}
diff --git a/config.c b/config.c
index 151d28664e..007436b382 100644
--- a/config.c
+++ b/config.c
@@ -2157,6 +2157,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)
{
diff --git a/config.h b/config.h
index 62204dc252..22edd96716 100644
--- a/config.h
+++ b/config.h
@@ -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 *);
diff --git a/repository.c b/repository.c
index 5dd1486718..f997bd1629 100644
--- a/repository.c
+++ b/repository.c
@@ -176,16 +176,23 @@ int repo_submodule_init(struct repository *submodule,
const char *path)
{
const struct submodule *sub;
+
+ sub = submodule_from_path(superproject, &null_oid, path);
+ if (!sub)
+ return -1;
+ return repo_submodule_init_by_name(submodule, superproject,
+ path, sub->name);
+}
+
+int repo_submodule_init_by_name(struct repository *submodule,
+ struct repository *superproject,
+ const char *path,
+ const char *name)
+{
struct strbuf gitdir = STRBUF_INIT;
struct strbuf worktree = STRBUF_INIT;
int ret = 0;
- sub = submodule_from_path(superproject, &null_oid, path);
- if (!sub) {
- ret = -1;
- goto out;
- }
-
strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
@@ -199,7 +206,7 @@ int repo_submodule_init(struct repository *submodule,
*/
strbuf_reset(&gitdir);
strbuf_repo_git_path(&gitdir, superproject,
- "modules/%s", sub->name);
+ "modules/%s", name);
if (repo_init(submodule, gitdir.buf, NULL)) {
ret = -1;
diff --git a/repository.h b/repository.h
index 9f16c42c1e..d3f0592471 100644
--- a/repository.h
+++ b/repository.h
@@ -119,6 +119,10 @@ int repo_init(struct repository *r, const char *gitdir, const char *worktree);
int repo_submodule_init(struct repository *submodule,
struct repository *superproject,
const char *path);
+int repo_submodule_init_by_name(struct repository *submodule,
+ struct repository *superproject,
+ const char *path,
+ const char *name);
void repo_clear(struct repository *repo);
/*
--
2.20.0.482.g66447595a7
next prev parent reply other threads:[~2019-01-16 10:32 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-16 10:31 [RFC/PATCH 00/10] Support using submodules with worktrees Nguyễn Thái Ngọc Duy
2019-01-16 10:31 ` [PATCH 01/10] doc: about submodule support with multiple worktrees Nguyễn Thái Ngọc Duy
2019-01-16 22:06 ` Stefan Beller
2019-01-17 10:22 ` Duy Nguyen
2019-01-16 10:31 ` [PATCH 02/10] submodule--helper: add missing \n Nguyễn Thái Ngọc Duy
2019-01-16 10:31 ` [PATCH 03/10] submodule add: support multiple worktrees Nguyễn Thái Ngọc Duy
2019-01-16 22:27 ` Stefan Beller
2019-01-16 10:31 ` [PATCH 04/10] submodule init: " Nguyễn Thái Ngọc Duy
2019-01-16 10:31 ` [PATCH 05/10] submodule update: add tests for " Nguyễn Thái Ngọc Duy
2019-01-16 10:31 ` [PATCH 06/10] submodule sync: support " Nguyễn Thái Ngọc Duy
2019-01-16 10:31 ` [PATCH 07/10] submodule deinit: " Nguyễn Thái Ngọc Duy
2019-01-16 10:31 ` Nguyễn Thái Ngọc Duy [this message]
2019-01-16 10:31 ` [PATCH 09/10] submodule clone: propagate extensions.worktreeConfig Nguyễn Thái Ngọc Duy
2019-01-16 10:31 ` [PATCH 10/10] submodule ensure-core-worktree: write to config.worktree Nguyễn Thái Ngọc Duy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190116103159.9305-9-pclouds@gmail.com \
--to=pclouds@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jrnieder@gmail.com \
--cc=marcnarc@xiplink.com \
--cc=sbeller@google.com \
--cc=tsniatowski@vewd.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).