git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] submodule: port subcommand 'set-url' from shell to C

From
Shourya Shukla <shouryashukla.oo@gmail.com>
Date
Apr 16, 2020, 21:04 UTC
Message-ID
<20200416210456.19122-3-shouryashukla.oo@gmail.com>
In-Reply-To
<20200416210456.19122-1-shouryashukla.oo@gmail.com>

This aims to convert submodule subcommand 'set-url' to a builtin. 'set-url' is ported to 'submodule--helper.c' and the latter is called via 'git-submodule.sh'

Signed-off-by: Shourya Shukla <shouryashukla.oo@gmail.com>
---
 builtin/submodule--helper.c | 38 +++++++++++++++++++++++++++++++++++++
 git-submodule.sh            | 23 ++--------------------
 2 files changed, 40 insertions(+), 21 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 1a4b391c88..10e774a5c9 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2246,6 +2246,43 @@ static int module_config(int argc, const char **argv, const char *prefix)
 	usage_with_options(git_submodule_helper_usage, module_config_options);
 }
 
+static int module_set_url(int argc, const char **argv, const char *prefix)
+{
+	const char *newurl = NULL;
+	const char *path = NULL;
+
+	struct pathspec pathspec;
+	struct module_list list;
+
+	int quiet = 0;
+
+	struct option module_set_url_options[] = {
+		OPT__QUIET(&quiet, N_("Suppress output for setting url of a submodule")),
+		OPT_END()
+	};
+
+	const char *const git_submodule_helper_usage[] = {
+		N_("git submodule [--quiet] set-url [--] <path> <newurl>"),
+		NULL
+	};
+
+	argc = parse_options(argc, argv, NULL, module_set_url_options,
+			     git_submodule_helper_usage, 0);
+
+	path = argv[1];
+	newurl = argv[2];
+
+	if(!path || !newurl){
+		usage_with_options(git_submodule_helper_usage,module_set_url_options);
+		return 1;
+	}
+
+	if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
+		return 1;
+
+	return update_url_in_gitmodules(path, newurl);
+}
+
 #define SUPPORT_SUPER_PREFIX (1<<0)
 
 struct cmd_struct {
@@ -2276,6 +2313,7 @@ static struct cmd_struct commands[] = {
 	{"is-active", is_active, 0},
 	{"check-name", check_name, 0},
 	{"config", module_config, 0},
+	{"set-url", module_set_url, 0},
 };
 
 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
diff --git a/git-submodule.sh b/git-submodule.sh
index 89f915cae9..f0304c3e19 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -783,6 +783,7 @@ cmd_set_branch() {
 # $@ = requested path, requested url
 #
 cmd_set_url() {
+
 	while test $# -ne 0
 	do
 		case "$1" in
@@ -803,27 +804,7 @@ cmd_set_url() {
 		shift
 	done
 
-	if test $# -ne 2
-	then
-		usage
-	fi
-
-	# we can't use `git submodule--helper name` here because internally, it
-	# hashes the path so a trailing slash could lead to an unintentional no match
-	name="$(git submodule--helper list "$1" | cut -f2)"
-	if test -z "$name"
-	then
-		exit 1
-	fi
-
-	url="$2"
-	if test -z "$url"
-	then
-		exit 1
-	fi
-
-	git submodule--helper config submodule."$name".url "$url"
-	git submodule--helper sync ${GIT_QUIET:+--quiet} "$name"
+	git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper set-url ${GIT_QUIET:+--quiet} -- "$@"
 }
 
 #
-- 
2.20.1
Previous: Shourya ShuklaNext: Christian Couder
Message 3 of 8 in “submodule: port 'set-url' from shell to C”
  1. Shourya ShuklaApr 16, 2020
  2. 1/2 submodule.c: update URL in .gitmodules using update_url_in_gitmodulesShourya Shukla, Apr 16, 2020
  3. 2/2 submodule: port subcommand 'set-url' from shell to CShourya Shukla, Apr 16, 2020
  4. Christian CouderApr 17, 2020
  5. Christian CouderApr 17, 2020
  6. Denton LiuApr 20, 2020
  7. Shourya ShuklaApr 27, 2020
  8. Denton LiuApr 27, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.