Skip to contentSkip to conversation

Submodule harden fetches (WAS: submodule: Fetch the direct sha1 first)

PATCH In review Stefan Beller 2 participants 10 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover Submodule harden fetches (WAS: submodule: Fetch the direct sha1 first)

A simple patch evolves into a series!

First we'll fix a bug by adding the --objects switch to rev-list to have a stricter check for the desired commit to be there. IIUC this is not 100% right yet, but it improves the situation.

The second patch introduces an extra fetch in case the first fetch did not yield the expected commit, using the pattern

    # first 4 lines unchanged:
    if $sha1's history and objects are incomplete:
        fetch ;# normally just like we have done before
    else 
        die ...
    # new code:     
    if $sha1's history and objects are still incomplete:
        fetch $sha1
    else
        die ... 

Thanks,
Stefan

Stefan Beller (2):
submodule: Include check for objects when fetching
submodule: Try harder to fetch needed sha1 by direct fetching sha1

 git-submodule.sh | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
Signature · 1 line
2.7.0.rc0.34.ga06e0b3.dirty
#1 lore raw

PATCH 1/2 submodule: Include check for objects when fetching

Quoted 5 lines · Junio

To be complete, the rev-list command line should also run with "--objects"; after all, a commit walker fetch may have downloaded commit chain completely but haven't fetched necessary trees and blobs when it was killed, and "rev-list $sha1 --not --all" would not catch such a breakage without "--objects".

By adding the --objects switch to rev-list we make sure to do a complete check.

---
 git-submodule.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git-submodule.sh +1 -1
index 9bc5c5f..f5d6675 100755
@@ -746,7 +746,7 @@ Maybe you want to use 'update --init'?")"
 				# 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) &&
+					( (rev=$(git rev-list --objects -n 1 $sha1 --not --all 2>/dev/null) &&
 					 test -z "$rev") || git-fetch)) ||
 				die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
 			fi
Signature · 1 line
2.7.0.rc0.34.ga06e0b3.dirty
#5 lore raw

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

In reply to Stefan Beller

When reviewing a change in Gerrit, which also updates a submodule, a common review practice is to download and cherry-pick the patch locally to test it. However when testing it locally, the 'git submodule update' may fail fetching the correct submodule sha1 as the corresponding commit in the submodule is not yet part of the project history, but also just a proposed change.

If $sha1 was not part of the default fetch, we try to fetch the $sha1 directly. Some servers however do not support direct fetch by sha1, which leads git-fetch to fail quickly. We can fail ourselves here as the still missing sha1 would lead to a failure later in the checkout stage anyway, so failing here is as good as we can get.

---

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?

Thanks,
Stefan

 git-submodule.sh | 7 +++++++
 1 file changed, 7 insertions(+)
git-submodule.sh +7 -0
index f5d6675..f021fe3 100755
@@ -749,6 +749,13 @@ Maybe you want to use 'update --init'?")"
 					( (rev=$(git rev-list --objects -n 1 $sha1 --not --all 2>/dev/null) &&
 					 test -z "$rev") || git-fetch)) ||
 				die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
+
+				# In case the sha1 is still missing, try harder
+				# by fetching the sha1 directly.
+				(clear_local_git_env; cd "$sm_path" &&
+					( (rev=$(git rev-list --objects -n 1 $sha1 --not --all 2>/dev/null) &&
+					 test -z "$rev") || git-fetch $(get_default_remote) $sha1 )) ||
+				die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
 			fi
 
 			# Is this something we just cloned?
Signature · 1 line
2.7.0.rc0.34.ga06e0b3.dirty
#2 lore raw

Re: [PATCH 1/2] submodule: Include check for objects when fetching

In reply to Stefan Beller
Quoted 9 lines · Stefan Beller
Quoted 5 lines · Junio

To be complete, the rev-list command line should also run with "--objects"; after all, a commit walker fetch may have downloaded commit chain completely but haven't fetched necessary trees and blobs when it was killed, and "rev-list $sha1 --not --all" would not catch such a breakage without "--objects".

By adding the --objects switch to rev-list we make sure to do a complete check.

You also need to drop "-n1" for the command to be equivalent to quickfetch, I think.

Quoted 19 lines
---
 git-submodule.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git-submodule.sh +1 -1
index 9bc5c5f..f5d6675 100755
@@ -746,7 +746,7 @@ Maybe you want to use 'update --init'?")"
 				# 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) &&
+					( (rev=$(git rev-list --objects -n 1 $sha1 --not --all 2>/dev/null) &&
 					 test -z "$rev") || git-fetch)) ||
 				die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
 			fi
#3 lore raw

Re: [PATCH 1/2] submodule: Include check for objects when fetching

Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Stefan Beller
Quoted 5 lines · Junio

To be complete, the rev-list command line should also run with "--objects"; after all, a commit walker fetch may have downloaded commit chain completely but haven't fetched necessary trees and blobs when it was killed, and "rev-list $sha1 --not --all" would not catch such a breakage without "--objects".

Quoted 5 lines
Quoted 2 lines

By adding the --objects switch to rev-list we make sure to do a complete check.

You also need to drop "-n1" for the command to be equivalent to quickfetch, I think.

Ahh, I think I need to take it back.

As the existing code relies not just on the exit status of rev-list but also checks if anything comes out of it, having "-n1" would not hurt.

The case I was worried about was that we have all commits to connect $sha1 to what are reachable from refs, but some tree/blob objects referenced by these commits were missing. By cutting the displaying with "-n1", rev-list may happily exit successfully after showing $sha1, without even realizing that some trees/blobs are missing and the history leading to $sha1 cannot be considered complete.

But because the code checks the empty-ness of $rev, this case is diagnosed as "$sha1 is not reachable from any of the refs" correctly anyway, so it should be OK.

Quoted 18 lines
Quoted 18 lines
---
 git-submodule.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git-submodule.sh +1 -1
index 9bc5c5f..f5d6675 100755
@@ -746,7 +746,7 @@ Maybe you want to use 'update --init'?")"
 				# 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) &&
+					( (rev=$(git rev-list --objects -n 1 $sha1 --not --all 2>/dev/null) &&
 					 test -z "$rev") || git-fetch)) ||
 				die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
 			fi
#4 lore raw

Re: [PATCH 1/2] submodule: Include check for objects when fetching

Quoted 30 lines · Junio C Hamano
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Stefan Beller
Quoted 5 lines · Junio

To be complete, the rev-list command line should also run with "--objects"; after all, a commit walker fetch may have downloaded commit chain completely but haven't fetched necessary trees and blobs when it was killed, and "rev-list $sha1 --not --all" would not catch such a breakage without "--objects".

Quoted 5 lines
Quoted 2 lines

By adding the --objects switch to rev-list we make sure to do a complete check.

You also need to drop "-n1" for the command to be equivalent to quickfetch, I think.

Ahh, I think I need to take it back.

As the existing code relies not just on the exit status of rev-list but also checks if anything comes out of it, having "-n1" would not hurt.

The case I was worried about was that we have all commits to connect $sha1 to what are reachable from refs, but some tree/blob objects referenced by these commits were missing. By cutting the displaying with "-n1", rev-list may happily exit successfully after showing $sha1, without even realizing that some trees/blobs are missing and the history leading to $sha1 cannot be considered complete.

Quoted 3 lines

But because the code checks the empty-ness of $rev, this case is diagnosed as "$sha1 is not reachable from any of the refs" correctly anyway, so it should be OK.

... this is embarrassing, but for exactly the same reason, I do not think we need "--objects" either. If $sha1 is not something that is already reachable from any of the refs, "rev-list -n1" would show something, making $rev non-empty, and it would result in a fetch, whether the objects that are reachable from the commits between existing refs and $sha1 are complete or not. So it is pointless to use "--objects" to test their presense.

Sorry about the noise ;-)

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
---
 git-submodule.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git-submodule.sh +1 -1
index 9bc5c5f..f5d6675 100755
@@ -746,7 +746,7 @@ Maybe you want to use 'update --init'?")"
 				# 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) &&
+					( (rev=$(git rev-list --objects -n 1 $sha1 --not --all 2>/dev/null) &&
 					 test -z "$rev") || git-fetch)) ||
 				die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
 			fi
#6 lore raw

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

In reply to Stefan Beller
Quoted 8 lines · Stefan Beller

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(-)
git-submodule.sh +26 -3
index 9bc5c5f..836348f 100755
@@ -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
 
#7 lore raw

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

Quoted 10 lines · Junio C Hamano
Quoted 7 lines · Stefan Beller

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: ....")

Quoted 7 lines
Quoted 1 line

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?

This looks very readable.

Feel free to drop both my patches and just introduce this patch as yours!

Quoted 9 lines
 git-submodule.sh | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
git-submodule.sh +0 -0
index 9bc5c5f..836348f 100755

git-submodule.sh cmd_deinit() · line 598

Quoted 8 lines
git-submodule.sh +5 -0
@@ -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"

While we're talking about this code anyway, I wondered if we can make it more cryptic again (just kidding!) and shorten it a bit by using the

    is_tip_reachable () (
        clear_local_git_env
        test -z $(git -C $1 rev-list -n 1 "$2" --not --all 2>/dev/null)
    )
Quoted 21 lines
git-submodule.sh +13 -1
+)
+
+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" &&

git-submodule.sh Maybe you want to use 'update --init'?")" · line 773

Quoted 10 lines
git-submodule.sh +8 -2
-                                       ( (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" ||

For another split second I wondered about the return code of is_tip_reachable, if the result is actually negated, but reading the chaining here, makes sense.

Thanks,
Stefan

Quoted 3 lines
git-submodule.sh +0 -0
                                die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
                        fi
 
#8 lore raw

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

Quoted 17 lines · Stefan Beller
Quoted 16 lines · Junio C Hamano
Quoted 8 lines · Stefan Beller

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.

Quoted 5 lines
Quoted 1 line

Perhaps something along this line?

This looks very readable.

Feel free to drop both my patches and just introduce this patch as yours!

Nah, this is not my itch but yours ;-)

Besides, have we agreed what error message we want to give to the users yet?

Quoted 67 lines
Quoted 9 lines
 git-submodule.sh | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
git-submodule.sh +0 -0
index 9bc5c5f..836348f 100755

git-submodule.sh cmd_deinit() · line 598

Quoted 8 lines
git-submodule.sh +5 -0
@@ -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"

While we're talking about this code anyway, I wondered if we can make it more cryptic again (just kidding!) and shorten it a bit by using the

    is_tip_reachable () (
        clear_local_git_env
        test -z $(git -C $1 rev-list -n 1 "$2" --not --all 2>/dev/null)
    )
Quoted 21 lines
git-submodule.sh +13 -1
+)
+
+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" &&

git-submodule.sh Maybe you want to use 'update --init'?")" · line 773

Quoted 10 lines
git-submodule.sh +8 -2
-                                       ( (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" ||

For another split second I wondered about the return code of is_tip_reachable, if the result is actually negated, but reading the chaining here, makes sense.

Thanks,
Stefan

Quoted 3 lines
git-submodule.sh +0 -0
                                die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
                        fi
 
#9 lore raw

PATCH 1/1 submodule: Try harder to fetch needed sha1 by direct fetching sha1

When reviewing a change in Gerrit, which also updates a submodule, a common review practice is to download and cherry-pick the patch locally to test it. However when testing it locally, the 'git submodule update' may fail fetching the correct submodule sha1 as the corresponding commit in the submodule is not yet part of the project history, but also just a proposed change.

If $sha1 was not part of the default fetch, we try to fetch the $sha1 directly. Some servers however do not support direct fetch by sha1, which leads git-fetch to fail quickly. We can fail ourselves here as the still missing sha1 would lead to a failure later in the checkout stage anyway, so failing here is as good as we can get.

---

Junio, I took your patch and reworded the error message, so 25 / 26 lines of the code are still originally authored by you. But as "it is my itch not yours" I am resending it. If you changed your mind, take authorship of this.

Thanks,
Stefan

 git-submodule.sh | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
git-submodule.sh +26 -3
index 9bc5c5f..babfc64 100755
@@ -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,10 +763,15 @@ 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 "Fetched in submodule path '\$displaypath', but it did not contain $sha1. Direct fetching of that commit failed.")"
 			fi
 
 			# Is this something we just cloned?
Signature · 1 line
2.7.0.rc0.34.ga06e0b3.dirty