refs is learning to avoid path rewriting that is done by strbuf_git_path_submodule(). Factor out this code so it could be reused by refs* Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx> --- path.c | 34 +++++++--------------------------- submodule.c | 31 +++++++++++++++++++++++++++++++ submodule.h | 1 + 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/path.c b/path.c index efcedafba..3451d2916 100644 --- a/path.c +++ b/path.c @@ -475,35 +475,16 @@ const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...) static int do_submodule_path(struct strbuf *buf, const char *path, const char *fmt, va_list args) { - const char *git_dir; struct strbuf git_submodule_common_dir = STRBUF_INIT; struct strbuf git_submodule_dir = STRBUF_INIT; - const struct submodule *sub; - int err = 0; + int ret; - strbuf_addstr(buf, path); - strbuf_complete(buf, '/'); - strbuf_addstr(buf, ".git"); - - git_dir = read_gitfile(buf->buf); - if (git_dir) { - strbuf_reset(buf); - strbuf_addstr(buf, git_dir); - } - if (!is_git_directory(buf->buf)) { - gitmodules_config(); - sub = submodule_from_path(null_sha1, path); - if (!sub) { - err = SUBMODULE_PATH_ERR_NOT_CONFIGURED; - goto cleanup; - } - strbuf_reset(buf); - strbuf_git_path(buf, "%s/%s", "modules", sub->name); - } - - strbuf_addch(buf, '/'); - strbuf_addbuf(&git_submodule_dir, buf); + ret = submodule_to_gitdir(&git_submodule_dir, path); + if (ret) + goto cleanup; + strbuf_complete(&git_submodule_dir, '/'); + strbuf_addbuf(buf, &git_submodule_dir); strbuf_vaddf(buf, fmt, args); if (get_common_dir_noenv(&git_submodule_common_dir, git_submodule_dir.buf)) @@ -514,8 +495,7 @@ static int do_submodule_path(struct strbuf *buf, const char *path, cleanup: strbuf_release(&git_submodule_dir); strbuf_release(&git_submodule_common_dir); - - return err; + return ret; } char *git_pathdup_submodule(const char *path, const char *fmt, ...) diff --git a/submodule.c b/submodule.c index ece17315d..3ce589d55 100644 --- a/submodule.c +++ b/submodule.c @@ -1335,3 +1335,34 @@ void prepare_submodule_repo_env(struct argv_array *out) } argv_array_push(out, "GIT_DIR=.git"); } + +int submodule_to_gitdir(struct strbuf *buf, const char *submodule) +{ + const struct submodule *sub; + const char *git_dir; + int ret = 0; + + strbuf_reset(buf); + strbuf_addstr(buf, submodule); + strbuf_complete(buf, '/'); + strbuf_addstr(buf, ".git"); + + git_dir = read_gitfile(buf->buf); + if (git_dir) { + strbuf_reset(buf); + strbuf_addstr(buf, git_dir); + } + if (!is_git_directory(buf->buf)) { + gitmodules_config(); + sub = submodule_from_path(null_sha1, submodule); + if (!sub) { + ret = -1; + goto cleanup; + } + strbuf_reset(buf); + strbuf_git_path(buf, "%s/%s", "modules", sub->name); + } + +cleanup: + return ret; +} diff --git a/submodule.h b/submodule.h index 23d76682b..2728494ce 100644 --- a/submodule.h +++ b/submodule.h @@ -70,6 +70,7 @@ extern int push_unpushed_submodules(struct sha1_array *commits, int dry_run); void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir); int parallel_submodules(void); +int submodule_to_gitdir(struct strbuf *buf, const char *submodule); /* * Prepare the "env_array" parameter of a "struct child_process" for executing -- 2.11.0.157.gd943d85