path.c: move some code out of strbuf_git_path_submodule()

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_* functions. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Mar 26, 2017 at 09:42 UTC bbbb7de7ac69e66c9263bd0e220172fe717b3f54
3 files changed +44 -28
path.c
+7 -28
@@ -471,39 +471,19 @@ const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...)
471 }
472
473 /* Returns 0 on success, negative on failure. */
474 -#define SUBMODULE_PATH_ERR_NOT_CONFIGURED -1
474 static int do_submodule_path(struct strbuf *buf, const char *path,
475 const char *fmt, va_list args)
476 {
478 - const char *git_dir;
477 struct strbuf git_submodule_common_dir = STRBUF_INIT;
478 struct strbuf git_submodule_dir = STRBUF_INIT;
481 - const struct submodule *sub;
482 - int err = 0;
479 + int ret;
480
484 - strbuf_addstr(buf, path);
485 - strbuf_complete(buf, '/');
486 - strbuf_addstr(buf, ".git");
487 -
488 - git_dir = read_gitfile(buf->buf);
489 - if (git_dir) {
490 - strbuf_reset(buf);
491 - strbuf_addstr(buf, git_dir);
492 - }
493 - if (!is_git_directory(buf->buf)) {
494 - gitmodules_config();
495 - sub = submodule_from_path(null_sha1, path);
496 - if (!sub) {
497 - err = SUBMODULE_PATH_ERR_NOT_CONFIGURED;
498 - goto cleanup;
499 - }
500 - strbuf_reset(buf);
501 - strbuf_git_path(buf, "%s/%s", "modules", sub->name);
502 - }
503 -
504 - strbuf_addch(buf, '/');
505 - strbuf_addbuf(&git_submodule_dir, buf);
481 + ret = submodule_to_gitdir(&git_submodule_dir, path);
482 + if (ret)
483 + goto cleanup;
484
485 + strbuf_complete(&git_submodule_dir, '/');
486 + strbuf_addbuf(buf, &git_submodule_dir);
487 strbuf_vaddf(buf, fmt, args);
488
489 if (get_common_dir_noenv(&git_submodule_common_dir, git_submodule_dir.buf))
@@ -514,8 +494,7 @@ static int do_submodule_path(struct strbuf *buf, const char *path,
494 cleanup:
495 strbuf_release(&git_submodule_dir);
496 strbuf_release(&git_submodule_common_dir);
517 -
518 - return err;
497 + return ret;
498 }
499
500 char *git_pathdup_submodule(const char *path, const char *fmt, ...)
submodule.c
+31
@@ -1596,3 +1596,34 @@ const char *get_superproject_working_tree(void)
1596
1597 return ret;
1598 }
1599 +
1600 +int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
1601 +{
1602 + const struct submodule *sub;
1603 + const char *git_dir;
1604 + int ret = 0;
1605 +
1606 + strbuf_reset(buf);
1607 + strbuf_addstr(buf, submodule);
1608 + strbuf_complete(buf, '/');
1609 + strbuf_addstr(buf, ".git");
1610 +
1611 + git_dir = read_gitfile(buf->buf);
1612 + if (git_dir) {
1613 + strbuf_reset(buf);
1614 + strbuf_addstr(buf, git_dir);
1615 + }
1616 + if (!is_git_directory(buf->buf)) {
1617 + gitmodules_config();
1618 + sub = submodule_from_path(null_sha1, submodule);
1619 + if (!sub) {
1620 + ret = -1;
1621 + goto cleanup;
1622 + }
1623 + strbuf_reset(buf);
1624 + strbuf_git_path(buf, "%s/%s", "modules", sub->name);
1625 + }
1626 +
1627 +cleanup:
1628 + return ret;
1629 +}
submodule.h
+6
@@ -81,6 +81,12 @@ extern int push_unpushed_submodules(struct sha1_array *commits,
81 int dry_run);
82 extern void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);
83 extern int parallel_submodules(void);
84 +/*
85 + * Given a submodule path (as in the index), return the repository
86 + * path of that submodule in 'buf'. Return -1 on error or when the
87 + * submodule is not initialized.
88 + */
89 +int submodule_to_gitdir(struct strbuf *buf, const char *submodule);
90
91 /*
92 * Prepare the "env_array" parameter of a "struct child_process" for executing