On 02/22/2017 03:04 PM, Nguyễn Thái Ngọc Duy wrote:
> 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 <pclo...@gmail.com>
> ---
>  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;

I didn't read this patch too carefully, but where the old code used the
constant `SUBMODULE_PATH_ERR_NOT_CONFIGURED`, the new code returns -1.
In fact, now the constant is totally unused. It looks like there's some
more cleanup that could happen.

> -                     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 3b98766a6..36b8d1d11 100644
> --- a/submodule.c
> +++ b/submodule.c
> @@ -1514,3 +1514,34 @@ void absorb_git_dir_into_superproject(const char 
> *prefix,
>               strbuf_release(&sb);
>       }
>  }
> +
> +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 05ab674f0..fc3d0303e 100644
> --- a/submodule.h
> +++ b/submodule.h
> @@ -81,6 +81,7 @@ extern int push_unpushed_submodules(struct sha1_array 
> *commits,
>                                   int dry_run);
>  extern void connect_work_tree_and_git_dir(const char *work_tree, const char 
> *git_dir);
>  extern int parallel_submodules(void);
> +int submodule_to_gitdir(struct strbuf *buf, const char *submodule);

A docstring is always nice :-)

>  
>  /*
>   * Prepare the "env_array" parameter of a "struct child_process" for 
> executing
> 

Michael

Reply via email to