From: Stefan Beller <sbeller@google.com>
To: git@vger.kernel.org, gitster@pobox.com
Cc: j6t@kdbg.org, sunshine@sunshineco.com, Jens.Lehmann@web.de,
Stefan Beller <sbeller@google.com>
Subject: [PATCHv4 2/2] submodule: port init from shell to C
Date: Thu, 21 Jan 2016 15:18:43 -0800 [thread overview]
Message-ID: <1453418323-29587-1-git-send-email-sbeller@google.com> (raw)
In-Reply-To: <1453260274-31005-1-git-send-email-sbeller@google.com>
By having the `init` functionality in C, we can reference it easier
from other parts in the code.
Signed-off-by: Stefan Beller <sbeller@google.com>
---
Hi Junio,
please replace the top-most patch of sb/submodule-init with this patch.
This will print to stdout instead of stderr, as it's the most
faithful shell -> translation. The s/printf/fprintf(stderr, / patch
will come with the series that needs the behavior, i.e. as one of the
first patches in the groups series (that is under heavy mental
construction currently).
Thanks,
Stefan
Interdiff to the top of sb/submodule-init (6a514f75c25, submodule: port
init from shell to C, Committed: 2016-01-20 12:55:30):
# diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
# index 4684f16..6323048 100644
# --- a/builtin/submodule--helper.c
# +++ b/builtin/submodule--helper.c
# @@ -232,7 +232,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
# struct strbuf sb = STRBUF_INIT;
# char *url = NULL;
# const char *upd = NULL;
# - const char *displaypath = relative_path(xgetcwd(), prefix, &sb);;
# + const char *displaypath = relative_path(xgetcwd(), prefix, &sb);
#
# /* Only loads from .gitmodules, no overlay with .git/config */
# gitmodules_config();
# @@ -276,7 +276,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
# die(_("Failed to register url for submodule path '%s'"),
# displaypath);
# if (!quiet)
# - fprintf(stderr, _("Submodule '%s' (%s) registered for path '%s'\n"),
# + printf(_("Submodule '%s' (%s) registered for path '%s'\n"),
# sub->name, url, displaypath);
# free(url);
# }
builtin/submodule--helper.c | 115 ++++++++++++++++++++++++++++++++++++++++++--
git-submodule.sh | 39 +--------------
2 files changed, 111 insertions(+), 43 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 1484b36..6323048 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -221,6 +221,115 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *pr
return 0;
}
+static int git_submodule_config(const char *var, const char *value, void *cb)
+{
+ return parse_submodule_config_option(var, value);
+}
+
+static void init_submodule(const char *path, const char *prefix, int quiet)
+{
+ const struct submodule *sub;
+ struct strbuf sb = STRBUF_INIT;
+ char *url = NULL;
+ const char *upd = NULL;
+ const char *displaypath = relative_path(xgetcwd(), prefix, &sb);
+
+ /* Only loads from .gitmodules, no overlay with .git/config */
+ gitmodules_config();
+
+ sub = submodule_from_path(null_sha1, path);
+
+ /*
+ * Copy url setting when it is not set yet.
+ * To look up the url in .git/config, we must not fall back to
+ * .gitmodules, so look it up directly.
+ */
+ strbuf_reset(&sb);
+ strbuf_addf(&sb, "submodule.%s.url", sub->name);
+ if (git_config_get_string(sb.buf, &url)) {
+ url = xstrdup(sub->url);
+
+ if (!url)
+ die(_("No url found for submodule path '%s' in .gitmodules"),
+ displaypath);
+
+ /* Possibly a url relative to parent */
+ if (starts_with_dot_dot_slash(url) ||
+ starts_with_dot_slash(url)) {
+ char *remoteurl;
+ char *remote = get_default_remote();
+ struct strbuf remotesb = STRBUF_INIT;
+ strbuf_addf(&remotesb, "remote.%s.url", remote);
+ free(remote);
+
+ if (git_config_get_string(remotesb.buf, &remoteurl))
+ /*
+ * The repository is its own
+ * authoritative upstream
+ */
+ remoteurl = xgetcwd();
+ url = relative_url(remoteurl, url, NULL);
+ strbuf_release(&remotesb);
+ }
+
+ if (git_config_set(sb.buf, url))
+ die(_("Failed to register url for submodule path '%s'"),
+ displaypath);
+ if (!quiet)
+ printf(_("Submodule '%s' (%s) registered for path '%s'\n"),
+ sub->name, url, displaypath);
+ free(url);
+ }
+
+ /* Copy "update" setting when it is not set yet */
+ strbuf_reset(&sb);
+ strbuf_addf(&sb, "submodule.%s.update", sub->name);
+ if (git_config_get_string_const(sb.buf, &upd) && sub->update) {
+ upd = sub->update;
+ if (strcmp(sub->update, "checkout") &&
+ strcmp(sub->update, "rebase") &&
+ strcmp(sub->update, "merge") &&
+ strcmp(sub->update, "none")) {
+ fprintf(stderr, _("warning: unknown update mode '%s' suggested for submodule '%s'\n"),
+ upd, sub->name);
+ upd = "none";
+ }
+ if (git_config_set(sb.buf, upd))
+ die(_("Failed to register update mode for submodule path '%s'"), displaypath);
+ }
+ strbuf_release(&sb);
+}
+
+static int module_init(int argc, const char **argv, const char *prefix)
+{
+ int quiet = 0;
+ int i;
+
+ struct option module_init_options[] = {
+ OPT_STRING(0, "prefix", &prefix,
+ N_("path"),
+ N_("alternative anchor for relative paths")),
+ OPT__QUIET(&quiet, "Suppress output for initialzing a submodule"),
+ OPT_END()
+ };
+
+ const char *const git_submodule_helper_usage[] = {
+ N_("git submodule--helper init [<path>]"),
+ NULL
+ };
+
+ argc = parse_options(argc, argv, prefix, module_init_options,
+ git_submodule_helper_usage, 0);
+
+ if (argc == 0)
+ die(_("Pass at least one submodule"));
+
+ for (i = 0; i < argc; i++)
+ init_submodule(argv[i], prefix, quiet);
+
+ return 0;
+}
+
struct module_list {
const struct cache_entry **entries;
int alloc, nr;
@@ -466,11 +575,6 @@ static int module_clone(int argc, const char **argv, const char *prefix)
return 0;
}
-static int git_submodule_config(const char *var, const char *value, void *cb)
-{
- return parse_submodule_config_option(var, value);
-}
-
struct submodule_update_clone {
/* states */
int count;
@@ -716,6 +820,7 @@ static struct cmd_struct commands[] = {
{"update-clone", update_clone},
{"resolve-relative-url", resolve_relative_url},
{"resolve-relative-url-test", resolve_relative_url_test},
+ {"init", module_init}
};
int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
diff --git a/git-submodule.sh b/git-submodule.sh
index 615ef9b..6fce0dc 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -398,45 +398,8 @@ cmd_init()
while read mode sha1 stage sm_path
do
die_if_unmatched "$mode"
- name=$(git submodule--helper name "$sm_path") || exit
-
- displaypath=$(relative_path "$sm_path")
-
- # Copy url setting when it is not set yet
- if test -z "$(git config "submodule.$name.url")"
- then
- url=$(git config -f .gitmodules submodule."$name".url)
- test -z "$url" &&
- die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")"
-
- # Possibly a url relative to parent
- case "$url" in
- ./*|../*)
- url=$(git submodule--helper resolve-relative-url "$url") || exit
- ;;
- esac
- git config submodule."$name".url "$url" ||
- die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")"
- say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")"
- fi
-
- # Copy "update" setting when it is not set yet
- if upd="$(git config -f .gitmodules submodule."$name".update)" &&
- test -n "$upd" &&
- test -z "$(git config submodule."$name".update)"
- then
- case "$upd" in
- checkout | rebase | merge | none)
- ;; # known modes of updating
- *)
- echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'"
- upd=none
- ;;
- esac
- git config submodule."$name".update "$upd" ||
- die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")"
- fi
+ git submodule--helper init ${GIT_QUIET:+--quiet} "$sm_path" || exit
done
}
--
2.7.0.rc0.41.g89994f2.dirty
next prev parent reply other threads:[~2016-01-21 23:19 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-01-15 23:37 [PATCH 0/2] Port `git submodule init` from shell to C Stefan Beller
2016-01-15 23:37 ` [PATCH 1/2] submodule: Port resolve_relative_url " Stefan Beller
2016-01-15 23:37 ` [PATCH 2/2] submodule: Port init " Stefan Beller
2016-01-19 23:17 ` [PATCH 0/2] Port `git submodule init` " Junio C Hamano
2016-01-20 2:03 ` [PATCHv2 " Stefan Beller
2016-01-20 2:03 ` [PATCHv2 1/2] submodule: port resolve_relative_url " Stefan Beller
2016-01-22 19:03 ` Johannes Sixt
2016-01-22 20:23 ` Stefan Beller
2016-01-20 2:03 ` [PATCHv2 2/2] submodule: port init " Stefan Beller
2016-01-20 3:24 ` [PATCHv3 " Stefan Beller
2016-01-20 21:01 ` Junio C Hamano
2016-01-20 22:33 ` Stefan Beller
2016-01-20 23:04 ` Junio C Hamano
2016-01-21 23:18 ` Stefan Beller [this message]
2016-01-22 22:30 ` [PATCHv4 " Junio C Hamano
2016-01-22 22:32 ` Stefan Beller
2016-01-22 23:32 ` [PATCHv3 0/2] Port `git submodule init` " Stefan Beller
2016-01-25 22:46 ` Junio C Hamano
2016-01-28 2:30 ` [PATCHv4 " Stefan Beller
2016-01-28 2:30 ` [PATCHv4 1/2] submodule: port resolve_relative_url " Stefan Beller
2016-01-28 22:03 ` Junio C Hamano
2016-01-28 22:11 ` Stefan Beller
2016-01-28 2:30 ` [PATCHv4 2/2] submodule: port init " Stefan Beller
2016-02-27 8:30 ` Duy Nguyen
2016-01-22 23:32 ` [PATCHv3 1/2] submodule: port resolve_relative_url " Stefan Beller
2016-01-22 23:32 ` [PATCHv3 2/2] submodule: port init " Stefan Beller
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1453418323-29587-1-git-send-email-sbeller@google.com \
--to=sbeller@google.com \
--cc=Jens.Lehmann@web.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=j6t@kdbg.org \
--cc=sunshine@sunshineco.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.
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 NNTP newsgroup(s).