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

Re: [PATCH 2/2] submodule: Try harder to fetch needed sha1 by direct fetching sha1

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 23, 2016, 00:01 UTC
Message-ID
<xmqqzius2t31.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<1456180548-20996-3-git-send-email-sbeller@google.com>
Stefan Beller <sbeller@google.com> writes:
Show 8 quoted lines
> We may want to discuss the error message. It is the same as in the case
> before (git-fetch <no args>), this is good for translation, but may be bad
> for the user as we may want to give extra information.
> ("We fetched for you and it worked, however the sha1 was not included,
> so we fetched again the server broke it, so we error out.  You used to
> see error message: ....")
>
> Although this may be too much information?

Now the "go to that submodule directory and see if $sha1 is reachable from any of the ref" check is written twice exactly the same way, it deserves to become a small helper function, I think.

Perhaps something along this line?
 git-submodule.sh | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
diff --git a/git-submodule.sh b/git-submodule.sh
index 9bc5c5f..836348f 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -591,6 +591,24 @@ cmd_deinit()
 	done
 }
 
+is_tip_reachable () (
+	clear_local_git_env
+	cd "$1" &&
+	rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
+	test -z "$rev"
+)
+
+fetch_in_submodule () (
+	clear_local_git_env
+	cd "$1" &&
+	case "$2" in
+	'')
+		git fetch ;;
+	*)
+		git fetch $(get_default_remote) "$2" ;;
+	esac
+)
+
 #
 # Update each submodule path to correct revision, using clone and checkout as needed
 #
@@ -745,9 +763,14 @@ Maybe you want to use 'update --init'?")"
 			then
 				# Run fetch only if $sha1 isn't present or it
 				# is not reachable from a ref.
-				(clear_local_git_env; cd "$sm_path" &&
-					( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
-					 test -z "$rev") || git-fetch)) ||
+				is_tip_reachable "$sm_path" "$sha1" ||
+				fetch_in_submodule "$sm_path" ||
+				die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
+
+				# Now we tried the usual fetch, but $sha1 may
+				# not be reachable from any of the refs
+				is_tip_reachable "$sm_path" "$sha1" ||
+				fetch_in_submodule "$sm_path" "$sha1" ||
 				die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
 			fi
 
Previous: Stefan BellerNext: Stefan Beller
Message 7 of 10 in “Submodule harden fetches (WAS: submodule: Fetch the direct sha1 first)”
  1. 0/2 Submodule harden fetches (WAS: submodule: Fetch the direct sha1 first)Stefan Beller, Feb 22, 2016
  2. 1/2 submodule: Include check for objects when fetchingStefan Beller, Feb 22, 2016
  3. Junio C HamanoFeb 22, 2016
  4. Junio C HamanoFeb 22, 2016
  5. Junio C HamanoFeb 22, 2016
  6. 2/2 submodule: Try harder to fetch needed sha1 by direct fetching sha1Stefan Beller, Feb 22, 2016
  7. Junio C HamanoFeb 23, 2016
  8. Stefan BellerFeb 23, 2016
  9. Junio C HamanoFeb 23, 2016
  10. submodule: Try harder to fetch needed sha1 by direct fetching sha1Stefan Beller, Feb 24, 2016

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.