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

[RFC PATCH v2] submodule: correct remote name with fetch

From
Daniel Black <daniel@mariadb.org>
Date
Oct 8, 2024, 01:49 UTC
Message-ID
<20241008015835.41678-2-daniel@mariadb.org>
In-Reply-To
<20241008015835.41678-1-daniel@mariadb.org>

The code fetches the submodules remote based on the superproject remote name instead of the submodule remote name[1].

Instead of grabbing the default remote of the superproject repository, ask the default remote of the submodule we are going to run 'git fetch' in.

1. https://lore.kernel.org/git/ZJR5SPDj4Wt_gmRO@pweza/
Signed-off-by: Daniel Black <daniel@mariadb.org>
---
 builtin/submodule--helper.c |  9 ++++-
 t/t5568-fetch-submodule.sh  | 65 +++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100755 t/t5568-fetch-submodule.sh
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index a1ada86952..567d21d330 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2322,7 +2322,14 @@ static int fetch_in_submodule(const char *module_path, int depth, int quiet,
 		strvec_pushf(&cp.args, "--depth=%d", depth);
 	if (oid) {
 		char *hex = oid_to_hex(oid);
-		char *remote = get_default_remote();
+		char *remote;
+		int code;
+
+		code = get_default_remote_submodule(module_path, &remote);
+		if (code) {
+			child_process_clear(&cp);
+			return code;
+		}
 
 		strvec_pushl(&cp.args, remote, hex, NULL);
 		free(remote);
diff --git a/t/t5568-fetch-submodule.sh b/t/t5568-fetch-submodule.sh
new file mode 100755
index 0000000000..56978bcfd7
--- /dev/null
+++ b/t/t5568-fetch-submodule.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+test_description='fetch can handle submodules origin names'
+
+GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
+export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+SUPER="$HTTPD_DOCUMENT_ROOT_PATH/super"
+SUB="$HTTPD_DOCUMENT_ROOT_PATH/sub"
+SUPER_URI="$HTTPD_URL/smart/super"
+SUB_URI="$HTTPD_URL/smart/sub"
+
+setup() {
+	SERVER="$1"
+	git init "$SERVER" &&
+	test_when_finished 'rm -rf "$SERVER"' &&
+	test_config -C "$SERVER" http.receivepack true
+}
+
+test_expect_success 'fetch submodule remote of different name from superproject' '
+	setup "$SUPER" &&
+	test_create_repo super &&
+	test_commit -C super bar &&
+	git -C super remote add upstream "$SUPER_URI" &&
+	test_config -C super push.default upstream &&
+	git -C super push --set-upstream upstream master:main &&
+
+	setup "$SUB" &&
+	test_create_repo sub &&
+	test_commit -C sub foo &&
+	git -C sub branch newmain &&
+	test_commit -C sub morefoo &&
+	test_commit -C sub moremorefoo &&
+	git -C sub remote add upstream "$SUB_URI" &&
+	test_config -C sub push.default upstream &&
+	git -C sub push --set-upstream upstream master:main &&
+
+	git -C super submodule add --branch main -- "$SUB_URI" sub &&
+	git -C super commit -am "add submodule" &&
+	git -C super push &&
+
+	# Needs to create unreachable commit from current master branch.
+	git -C sub checkout newmain &&
+	test_commit -C sub echo &&
+	test_commit -C sub moreecho &&
+	git -C sub push  --set-upstream upstream newmain:newmain &&
+
+	git clone --origin o1 --branch main -- "$SUPER_URI" superproject &&
+	git -C superproject submodule update --init &&
+
+	git -C super/sub fetch &&
+	git -C super/sub checkout origin/newmain &&
+	git -C super commit -m "update submodule" sub &&
+	git -C super push && 
+
+	git -C superproject pull --no-recurse-submodules &&
+	git -C superproject submodule update
+'
+
+test_done
-- 
2.46.2
Previous: Daniel BlackNext: Junio C Hamano
Message 9 of 12 in “bug: submodule update fails to fetch”
  1. Sergei GolubchikJun 22, 2023
  2. Taylor BlauJun 22, 2023
  3. Sergei GolubchikJun 22, 2023
  4. Jacob KellerJun 23, 2023
  5. submodule: correct remote name with fetchDaniel Black, Oct 1, 2024
  6. Junio C HamanoOct 1, 2024
  7. Junio C HamanoOct 1, 2024
  8. Daniel Black, Oct 8, 2024
  9. submodule: correct remote name with fetchDaniel Black, Oct 8, 2024
  10. Junio C HamanoOct 8, 2024
  11. submodule: correct remote name with fetchDaniel Black, Oct 9, 2024
  12. Junio C HamanoOct 9, 2024

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.