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

[PATCH 09/10] submodule: fetch in submodules git directory instead of in worktree

From
Stefan Beller <sbeller@google.com>
Date
Aug 8, 2018, 22:17 UTC
Message-ID
<20180808221752.195419-10-sbeller@google.com>
In-Reply-To
<20180808221752.195419-1-sbeller@google.com>

This patch started as a refactoring to make 'get_next_submodule' more readable, but upon doing so, I realized that git-fetch actually doesn't need to be run in the worktree. So let's run it in the git dir instead.

That should pave the way towards fetching submodules that are currently not checked out.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 submodule.c                 | 43 ++++++++++++++++++++++++++-----------
 t/t5526-fetch-submodules.sh |  7 +++++-
 2 files changed, 37 insertions(+), 13 deletions(-)
diff --git a/submodule.c b/submodule.c
index 21757e32908..ec7ea6f8c2d 100644
--- a/submodule.c
+++ b/submodule.c
@@ -481,6 +481,12 @@ void prepare_submodule_repo_env(struct argv_array *out)
 			 DEFAULT_GIT_DIR_ENVIRONMENT);
 }
 
+static void prepare_submodule_repo_env_in_gitdir(struct argv_array *out)
+{
+	prepare_submodule_repo_env_no_git_dir(out);
+	argv_array_pushf(out, "%s=.", GIT_DIR_ENVIRONMENT);
+}
+
 /* Helper function to display the submodule header line prior to the full
  * summary output. If it can locate the submodule objects directory it will
  * attempt to lookup both the left and right commits and put them into the
@@ -1227,6 +1233,27 @@ static int get_fetch_recurse_config(const struct submodule *submodule,
 	return spf->default_option;
 }
 
+static const char *get_submodule_git_dir(struct repository *r, const char *path)
+{
+	struct repository subrepo;
+	const char *ret;
+
+	if (repo_submodule_init(&subrepo, r, path)) {
+		/* no entry in .gitmodules? */
+		struct strbuf gitdir = STRBUF_INIT;
+		strbuf_repo_worktree_path(&gitdir, r, "%s/.git", path);
+		if (repo_init(&subrepo, gitdir.buf, NULL)) {
+			strbuf_release(&gitdir);
+			return NULL;
+		}
+	}
+
+	ret = xstrdup(subrepo.gitdir);
+	repo_clear(&subrepo);
+
+	return ret;
+}
+
 static int get_next_submodule(struct child_process *cp,
 			      struct strbuf *err, void *data, void **task_cb)
 {
@@ -1234,8 +1261,6 @@ static int get_next_submodule(struct child_process *cp,
 	struct submodule_parallel_fetch *spf = data;
 
 	for (; spf->count < spf->r->index->cache_nr; spf->count++) {
-		struct strbuf submodule_path = STRBUF_INIT;
-		struct strbuf submodule_git_dir = STRBUF_INIT;
 		struct strbuf submodule_prefix = STRBUF_INIT;
 		const struct cache_entry *ce = spf->r->index->cache[spf->count];
 		const char *git_dir, *default_argv;
@@ -1273,16 +1298,12 @@ static int get_next_submodule(struct child_process *cp,
 			continue;
 		}
 
-		strbuf_repo_worktree_path(&submodule_path, spf->r, "%s", ce->name);
-		strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
 		strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
-		git_dir = read_gitfile(submodule_git_dir.buf);
-		if (!git_dir)
-			git_dir = submodule_git_dir.buf;
-		if (is_directory(git_dir)) {
+		git_dir = get_submodule_git_dir(spf->r, ce->name);
+		if (git_dir) {
 			child_process_init(cp);
-			cp->dir = strbuf_detach(&submodule_path, NULL);
-			prepare_submodule_repo_env(&cp->env_array);
+			prepare_submodule_repo_env_in_gitdir(&cp->env_array);
+			cp->dir = git_dir;
 			cp->git_cmd = 1;
 			if (!spf->quiet)
 				strbuf_addf(err, "Fetching submodule %s%s\n",
@@ -1294,8 +1315,6 @@ static int get_next_submodule(struct child_process *cp,
 			argv_array_push(&cp->args, submodule_prefix.buf);
 			ret = 1;
 		}
-		strbuf_release(&submodule_path);
-		strbuf_release(&submodule_git_dir);
 		strbuf_release(&submodule_prefix);
 		if (ret) {
 			spf->count++;
diff --git a/t/t5526-fetch-submodules.sh b/t/t5526-fetch-submodules.sh
index 0f730d77815..4437cb17698 100755
--- a/t/t5526-fetch-submodules.sh
+++ b/t/t5526-fetch-submodules.sh
@@ -567,7 +567,12 @@ test_expect_success 'fetching submodule into a broken repository' '
 
 	test_must_fail git -C dst status &&
 	test_must_fail git -C dst diff &&
-	test_must_fail git -C dst fetch --recurse-submodules
+
+	# git-fetch cannot find the git directory of the submodule,
+	# so it will do nothing, successfully, as it cannot distinguish between
+	# this broken submodule and a submodule that was just set active but
+	# not cloned yet
+	git -C dst fetch --recurse-submodules
 '
 
 test_expect_success "fetch new commits when submodule got renamed" '
-- 
2.18.0.597.ga71716f1ad-goog
Previous: Stefan BellerNext: Stefan Beller
Message 24 of 27 in “fetch: make sure submodule oids are fetched”
  1. 00/10 fetch: make sure submodule oids are fetchedStefan Beller, Aug 8, 2018
  2. 01/10 string_list: print_string_list to use trace_printfStefan Beller, Aug 8, 2018
  3. Junio C HamanoAug 9, 2018
  4. Stefan BellerAug 9, 2018
  5. 02/10 string-list.h: add string_list_pop function.Stefan Beller, Aug 8, 2018
  6. Martin ÅgrenAug 9, 2018
  7. Junio C HamanoAug 9, 2018
  8. Jeff KingAug 9, 2018
  9. Stefan BellerAug 9, 2018
  10. Jeff KingAug 9, 2018
  11. Stefan BellerAug 9, 2018
  12. 03/10 sha1-array: provide oid_array_remove_ifStefan Beller, Aug 8, 2018
  13. Martin ÅgrenAug 9, 2018
  14. Stefan BellerAug 9, 2018
  15. Jeff KingAug 9, 2018
  16. Junio C HamanoAug 9, 2018
  17. Junio C HamanoAug 9, 2018
  18. 04/10 submodule.c: convert submodule_move_head new argument to object idStefan Beller, Aug 8, 2018
  19. Junio C HamanoAug 9, 2018
  20. 05/10 submodule.c: fix indentationStefan Beller, Aug 8, 2018
  21. 06/10 submodule.c: sort changed_submodule_names before searching itStefan Beller, Aug 8, 2018
  22. 07/10 submodule: move global changed_submodule_names into fetch submodule structStefan Beller, Aug 8, 2018
  23. 08/10 submodule.c: do not copy around submodule listStefan Beller, Aug 8, 2018
  24. 09/10 submodule: fetch in submodules git directory instead of in worktreeStefan Beller, Aug 8, 2018
  25. 10/10 fetch: retry fetching submodules if sha1 were not fetchedStefan Beller, Aug 8, 2018
  26. Martin ÅgrenAug 9, 2018
  27. Stefan BellerAug 9, 2018

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.