From: Eric Sunshine <sunshine@sunshineco.com>
To: Stefan Beller <sbeller@google.com>
Cc: Junio C Hamano <gitster@pobox.com>,
Git List <git@vger.kernel.org>,
Jonathan Nieder <jrnieder@gmail.com>,
Johannes Schindelin <johannes.schindelin@gmail.com>,
Jens Lehmann <Jens.Lehmann@web.de>, Jeff King <peff@peff.net>
Subject: Re: [PATCHv4 3/3] submodule: implement `clone` as a builtin helper
Date: Tue, 1 Sep 2015 14:52:54 -0400 [thread overview]
Message-ID: <CAPig+cRxQdquKZXdmpiZjj8JFB2wdCE4UcbW6qYUwvtZsVw=ug@mail.gmail.com> (raw)
In-Reply-To: <1441131869-29474-4-git-send-email-sbeller@google.com>
On Tue, Sep 1, 2015 at 2:24 PM, Stefan Beller <sbeller@google.com> wrote:
> This converts implements the helper `module_clone`. This functionality is
Mentioned previously[1]: "converts implements"?
[1]: http://article.gmane.org/gmane.comp.version-control.git/276966
> needed for converting `git submodule update` later on, which we want to
> add threading to.
>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index f5e408a..63f535a 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> +static int module_clone(int argc, const char **argv, const char *prefix)
> +{
> + const char *path = NULL, *name = NULL, *url = NULL;
> + const char *reference = NULL, *depth = NULL;
> + const char *alternative_path = NULL, *p;
> + int quiet = 0;
> + FILE *submodule_dot_git;
> + char *sm_gitdir;
> + struct strbuf rel_path = STRBUF_INIT;
> + struct strbuf sb = STRBUF_INIT;
> +
> + struct option module_clone_options[] = {
> + OPT_STRING(0, "prefix", &alternative_path,
> + N_("path"),
> + N_("alternative anchor for relative paths")),
> + OPT_STRING(0, "path", &path,
> + N_("path"),
> + N_("where the new submodule will be cloned to")),
> + OPT_STRING(0, "name", &name,
> + N_("string"),
> + N_("name of the new submodule")),
> + OPT_STRING(0, "url", &url,
> + N_("string"),
> + N_("url where to clone the submodule from")),
> + OPT_STRING(0, "reference", &reference,
> + N_("string"),
> + N_("reference repository")),
> + OPT_STRING(0, "depth", &depth,
> + N_("string"),
> + N_("depth for shallow clones")),
> + OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
> + OPT_END()
> + };
> +
> + const char * const git_submodule_helper_usage[] = {
Style: *const
> + N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
> + "[--reference <repository>] [--name <name>] [--url <url>]"
> + "[--depth <depth>] [--] [<path>...]"),
> + NULL
> + };
> +
> + argc = parse_options(argc, argv, prefix, module_clone_options,
> + git_submodule_helper_usage, 0);
> +
> + strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
> + sm_gitdir = strbuf_detach(&sb, NULL);
> +
> + if (!file_exists(sm_gitdir)) {
> + if (safe_create_leading_directories_const(sm_gitdir) < 0)
> + die(_("could not create directory '%s'"), sm_gitdir);
> + if (clone_submodule(path, sm_gitdir, url, depth, reference, quiet))
> + die(N_("clone of '%s' into submodule path '%s' failed"),
> + url, path);
s/N_/_/
> + } else {
> + if (safe_create_leading_directories_const(path) < 0)
> + die(_("could not create directory '%s'"), path);
> + if (unlink(sm_gitdir) < 0)
> + die_errno(_("failed to delete '%s'"), sm_gitdir);
> + }
> +
> + /* Write a .git file in the submodule to redirect to the superproject. */
> + if (alternative_path && *alternative_path)) {
> + p = relative_path(path, alternative_path, &sb);
> + strbuf_reset(&sb);
> + } else
> + p = path;
> +
> + if (safe_create_leading_directories_const(p) < 0)
> + die(_("could not create directory '%s'"), p);
> +
> + strbuf_addf(&sb, "%s/.git", p);
> +
> + if (safe_create_leading_directories_const(sb.buf) < 0)
> + die(_("could not create leading directories of '%s'"), sb.buf);
> + submodule_dot_git = fopen(sb.buf, "w");
> + if (!submodule_dot_git)
> + die (_("cannot open file '%s': %s"), sb.buf, strerror(errno));
Style: drop space before '('
Also, can't you use die_errno() here?
> + fprintf(submodule_dot_git, "gitdir: %s\n",
> + relative_path(sm_gitdir, path, &rel_path));
> + if (fclose(submodule_dot_git))
> + die(_("could not close file %s"), sb.buf);
> + strbuf_reset(&sb);
> +
> + /* Redirect the worktree of the submodule in the superproject's config */
> + if (strbuf_getcwd(&sb))
> + die_errno(_("unable to get current working directory"));
> +
> + if (!is_absolute_path(sm_gitdir)) {
> + if (strbuf_getcwd(&sb))
> + die_errno(_("unable to get current working directory"));
Why does this need to call getcwd() on 'sb' when it was called
immediately above the conditional and its value hasn't changed?
> + strbuf_addf(&sb, "/%s", sm_gitdir);
> + free(sm_gitdir);
> + sm_gitdir = strbuf_detach(&sb, NULL);
> + }
> +
> +
> + strbuf_addf(&sb, "/%s", path);
> +
> + p = git_pathdup_submodule(path, "config");
> + if (!p)
> + die(_("could not get submodule directory for '%s'"), path);
> + git_config_set_in_file(p, "core.worktree",
> + relative_path(sb.buf, sm_gitdir, &rel_path));
> + strbuf_release(&sb);
Is 'rel_path' being leaked here?
> + free(sm_gitdir);
> + return 0;
> +}
>
> struct cmd_struct {
> const char *cmd;
> @@ -132,6 +271,7 @@ struct cmd_struct {
> static struct cmd_struct commands[] = {
> {"list", module_list},
> {"name", module_name},
> + {"clone", module_clone},
> };
next prev parent reply other threads:[~2015-09-01 18:53 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-01 18:24 [PATCHv4 0/3] submodule--helper: Have some refactoring only patches first Stefan Beller
2015-09-01 18:24 ` [PATCHv4 1/3] submodule: implement `list` as a builtin helper Stefan Beller
2015-09-01 19:55 ` Junio C Hamano
2015-09-02 7:20 ` Junio C Hamano
2015-09-01 23:17 ` Eric Sunshine
2015-09-01 18:24 ` [PATCHv4 2/3] submodule: implement `name` " Stefan Beller
2015-09-01 19:01 ` Eric Sunshine
2015-09-01 18:24 ` [PATCHv4 3/3] submodule: implement `clone` " Stefan Beller
2015-09-01 18:52 ` Eric Sunshine [this message]
2015-09-01 19:05 ` Jeff King
2015-09-01 19:15 ` Eric Sunshine
2015-09-01 19:13 ` Junio C Hamano
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='CAPig+cRxQdquKZXdmpiZjj8JFB2wdCE4UcbW6qYUwvtZsVw=ug@mail.gmail.com' \
--to=sunshine@sunshineco.com \
--cc=Jens.Lehmann@web.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=johannes.schindelin@gmail.com \
--cc=jrnieder@gmail.com \
--cc=peff@peff.net \
--cc=sbeller@google.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).