Skip to contentSkip to conversation

builtin/clone: fix --revision with SHA-256 commits

PATCH In review =?UTF-8?q?Andrei=20=C8=98ova?= 2 participants 4 messages Nov 20 – Nov 21

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 builtin/clone: fix --revision with SHA-256 commits

The refspec item is created before the destination repository's hash algorithm is set up, so when cloning repositories using the SHA-256 object format parse_refspec incorrectly parses the hash.

---
Hello Git developers,

I was messing around with a repository using the SHA-256 object format and I noticed that --revision doesn't work if you give it a SHA-256 commit hash, so I put together this fix and a test case.

Thanks,
Andrei Șova

 builtin/clone.c           | 15 ++++++++++++++-
 t/t5621-clone-revision.sh | 15 +++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
builtin/clone.c +14 -1
index 4b9929b980..69513a14c2 100644
@@ -963,6 +963,7 @@ int cmd_clone(int argc,
 	struct string_list server_options = STRING_LIST_INIT_NODUP;
 	const char *bundle_uri = NULL;
 	char *option_rev = NULL;
+	int rev_refspec_idx = 0;
 
 	struct clone_opts opts = CLONE_OPTS_INIT;
 
@@ -1484,6 +1485,7 @@ int cmd_clone(int argc,
 		opts.wants_head = 0;
 		opts.detach = 1;
 
+		rev_refspec_idx = remote->fetch.nr;
 		refspec_append(&remote->fetch, option_rev);
 	}
 
@@ -1525,6 +1527,17 @@ int cmd_clone(int argc,
 	create_reference_database(the_repository, NULL, 1);
 	startup_info->have_repository = 1;
 
+	if (option_rev) {
+		/*
+		 * Re-initialize the refspec since potential hashes might have been
+		 * parsed under the wrong algorithm.
+		 */
+		struct refspec_item *r = &remote->fetch.items[rev_refspec_idx];
+		refspec_item_clear(r);
+		if (!refspec_item_init_fetch(r, option_rev, the_repository->hash_algo))
+			die(_("invalid refspec '%s'"), option_rev);
+	}
+
 	/*
 	 * Before fetching from the remote, download and install bundle
 	 * data from the --bundle-uri option.
@@ -1588,7 +1601,7 @@ int cmd_clone(int argc,
 		}
 	}
 
-	if (refs)
+	if (refs || (option_rev && remote->fetch.items[rev_refspec_idx].exact_sha1))
 		mapped_refs = wanted_peer_refs(&opts, refs, &remote->fetch);
 
 	if (mapped_refs) {
t/t5621-clone-revision.sh +15 -0
index 54789423f8..1afac8abaa 100755
@@ -78,6 +78,21 @@ test_expect_success 'clone with --revision being a raw commit hash' '
 	test_must_fail git -C dst config remote.origin.fetch
 '
 
+test_expect_success 'clone with --revision being a raw SHA-256 commit hash' '
+	test_when_finished "rm -rf src256 dst256" &&
+	git init --object-format=sha256 src256 &&
+	test_commit -C src256 --no-tag "initial commit" &&
+	oid=$(git -C src256 rev-parse HEAD) &&
+	GIT_DEFAULT_HASH=sha1 git clone --no-local --revision=$oid src256 dst256 &&
+	echo $oid >expect &&
+	git -C dst256 rev-parse HEAD >actual &&
+	test_must_fail git -C dst256 symbolic-ref -q HEAD >/dev/null &&
+	test_cmp expect actual &&
+	git -C dst256 for-each-ref refs >actual &&
+	test_must_be_empty actual &&
+	test_must_fail git -C dst256 config remote.origin.fetch
+'
+
 test_expect_success 'clone with --revision and --bare' '
 	test_when_finished "rm -rf dst" &&
 	git clone --revision=refs/heads/main --bare . dst &&
Signature · 1 line
2.55.0
#1 lore raw
Quoted 3 lines · "=?UTF-8?q?Andrei=20=C8=98ova?=" <[email protected]> writes:

The refspec item is created before the destination repository's hash algorithm is set up, so when cloning repositories using the SHA-256 object format parse_refspec incorrectly parses the hash.

A few improvements would make this much clearer for git log readers.

* Clearly state what fails for the user when cloning with "--revision=<sha256-oid>" while defaulting to SHA-1. Along the lines of

"git clone" fails with such and such error message (when talking to a server over protocol v2, it dies with this other message with protocol v0/v1).

* The issue triggers in the opposite direction too. I.e., when the default hash algorithm differs from the remote repository's hash algorithm discovered during the handshake, so the title can use some polishing ("with SHA-256" -> "when cloning from repo with different hash", or somesuch). parse_refspec() fails when the length does not exactly match (i.e., it won't be happy when it sees a SHA-1 object name even though it is shorter than the pre-handshake SHA-256 default, as it requires the exact length).

* Explain why if (refs) had to change. The log message does not mention the second hunk's guard change (if (refs || (option_rev && ...))), which is needed because ...?

Structure & line wrapping: Wrap lines to ~72 columns and end with an imperative command to tell somebody to "make the code like so".

Having said all that, if the problem is "the refspec item is created before we know what algorithm to use", wouldn't that suggest that the right conclusion from the observation should be more like "so, swap the sequence of initialization, keep the command line argument as an array of strings, do the handshake with the other side first and *then* finally create the refspec"?

Undoing what was done in an earlier step and recomputing may have been possible without losing information in this particular case, but that does not look like a good design, just sheer chance.

t/t5621-clone-revision.sh test_expect_success 'clone with --revision being a raw commit hash' ' · line 82

Quoted 10 lines
t/t5621-clone-revision.sh +2 -0
index 54789423f8..1afac8abaa 100755
@@ -78,6 +78,21 @@ test_expect_success 'clone with --revision being a raw commit hash' '
 	test_must_fail git -C dst config remote.origin.fetch
 '
 
+test_expect_success 'clone with --revision being a raw SHA-256 commit hash' '
+	test_when_finished "rm -rf src256 dst256" &&

Clean-up preparation.

t/t5621-clone-revision.sh test_expect_success 'clone with --revision being a raw commit hash' ' · line 85

Quoted 3 lines
t/t5621-clone-revision.sh +3 -0
+	git init --object-format=sha256 src256 &&
+	test_commit -C src256 --no-tag "initial commit" &&
+	oid=$(git -C src256 rev-parse HEAD) &&

Test set-up.

t/t5621-clone-revision.sh test_expect_success 'clone with --revision being a raw commit hash' ' · line 86

Quoted 1 line
t/t5621-clone-revision.sh +1 -0
+	GIT_DEFAULT_HASH=sha1 git clone --no-local --revision=$oid src256 dst256 &&

The command to be tested. You expect this not to fail.

After a group of commands that makes one logical test, it would help readers to have a blank line here. A truly tricky expectation may even want a comment in addition to such a paragraph break, but I think this one and the various expectations we see below may be reasonably easy to guess without comments.

t/t5621-clone-revision.sh test_expect_success 'clone with --revision being a raw commit hash' ' · line 90

Quoted 4 lines
t/t5621-clone-revision.sh +4 -0
+	echo $oid >expect &&
+	git -C dst256 rev-parse HEAD >actual &&
+	test_must_fail git -C dst256 symbolic-ref -q HEAD >/dev/null &&
+	test_cmp expect actual &&

This is an additional follow-up test. You want not just the clone to succeed, but HEAD to record the specified object name. Remove the third command that makes sure the HEAD is detached, so that setting up the expect and the actual files and their use come as a three-tuple. "The HEAD must be detached because we gave an exact object name" is a seprate fact that you want to verify.

t/t5621-clone-revision.sh test_expect_success 'clone with --revision being a raw commit hash' ' · line 92

Quoted 2 lines
t/t5621-clone-revision.sh +2 -0
+	git -C dst256 for-each-ref refs >actual &&
+	test_must_be_empty actual &&

So is "there should be no branches copied from them". For this test to be truly effective, wouldn't you want to have a few unrelted refs in the source repository (e.g., you can omit --no-tag when creating an initial commit)?

t/t5621-clone-revision.sh test_expect_success 'clone with --revision being a raw commit hash' ' · line 98

Quoted 6 lines
t/t5621-clone-revision.sh +3 -0
+	test_must_fail git -C dst256 config remote.origin.fetch
+'
+
 test_expect_success 'clone with --revision and --bare' '
 	test_when_finished "rm -rf dst" &&
 	git clone --revision=refs/heads/main --bare . dst &&

Thanks.

#2 lore raw

Hi! Thanks for the review.

Quoted 6 lines

Having said all that, if the problem is "the refspec item is created before we know what algorithm to use", wouldn't that suggest that the right conclusion from the observation should be more like "so, swap the sequence of initialization, keep the command line argument as an array of strings, do the handshake with the other side first and *then* finally create the refspec"?

Yea that was what I tried doing initially. Unfortunately it does not work because the ref prefixes have to be initialized before we can fetch the remote refs (and thus find out the remote hash algo), and we need the refspec item to exist before that, so there is a kind of circular dependency.

Quoted 3 lines

Undoing what was done in an earlier step and recomputing may have been possible without losing information in this particular case, but that does not look like a good design, just sheer chance.

I agree. I originally did not quite understand the purpose of refspec_ref_prefixes but I looked into it some more and I think it is OK to not initialize the ref prefixes when using --revision (IIUC the prefixes are used just as an optimization?). This also allows to get rid of the extra case in the if (refs) as it should be non-NULL now (previously it could be NULL since transport_get_remote_refs would falsely return nothing because of the incorrectly parsed refspec item).

Signature · 1 line
Andrei Șova
#3 lore raw

PATCH v2 1/1 builtin/clone: fix --revision when remote / local hash algos differ

In reply to =?UTF-8?q?Andrei=20=C8=98ova?=

Under protocol v2, "git clone --revision" fails with "fatal: Remote revision ... not found in upstream origin" when it is passed a valid commit hash with an algorithm that does not match the default algorithm (under protocol v1, this error message is: "fatal: couldn't find remote ref ..."). However, the hash points to a valid commit object on the remote.

The refspec item is created before the destination repository's hash algorithm is set up, so when cloning repositories using a different object format parse_refspec incorrectly parses the hash.

Delay the initialization of the refspec item until after we have gathered the remote repository's hash algorithm, so that it parses correctly under the expected algorithm. Consequently, leave the prefix list empty when --revision is used to get all the advertised refs.

---
 builtin/clone.c           | 38 +++++++++++++++++++++++---------------
 t/t5621-clone-revision.sh | 19 +++++++++++++++++++
 2 files changed, 42 insertions(+), 15 deletions(-)
builtin/clone.c +23 -15
index 4b9929b980..07a4c2db98 100644
@@ -485,7 +485,8 @@ static struct ref *find_remote_branch(const struct ref *refs, const char *branch
 
 static struct ref *wanted_peer_refs(struct clone_opts *opts,
 				    const struct ref *refs,
-				    struct refspec *refspec)
+				    struct refspec *refspec,
+				    int rev_refspec_idx)
 {
 	struct ref *local_refs = NULL;
 	struct ref **tail = &local_refs;
@@ -506,7 +507,7 @@ static struct ref *wanted_peer_refs(struct clone_opts *opts,
 	}
 
 	for (size_t i = 0; i < refspec->nr; i++)
-		get_fetch_map(refs, &refspec->items[i], &tail, 0);
+		get_fetch_map(refs, &refspec->items[i], &tail, i == rev_refspec_idx);
 
 	free_one_ref(to_free);
 
@@ -963,6 +964,7 @@ int cmd_clone(int argc,
 	struct string_list server_options = STRING_LIST_INIT_NODUP;
 	const char *bundle_uri = NULL;
 	char *option_rev = NULL;
+	int rev_refspec_idx = -1;
 
 	struct clone_opts opts = CLONE_OPTS_INIT;
 
@@ -1478,24 +1480,24 @@ int cmd_clone(int argc,
 	if (transport->smart_options && !deepen && !filter_options.choice)
 		transport->smart_options->check_self_contained_and_connected = 1;
 
+	if ((option_tags || option_branch) && !option_rev)
+		/*
+		 * Add tags refspec when user asked for tags (implicitly) or
+		 * specified --branch, whose argument might be a tag.
+		 */
+		refspec_append(&remote->fetch, TAG_REFSPEC);
+
+	if (!option_rev)
+		refspec_ref_prefixes(&remote->fetch,
+				     &transport_ls_refs_options.ref_prefixes);
+
 	if (option_rev) {
 		option_tags = 0;
 		option_single_branch = 0;
 		opts.wants_head = 0;
 		opts.detach = 1;
-
-		refspec_append(&remote->fetch, option_rev);
 	}
 
-	if (option_tags || option_branch)
-		/*
-		 * Add tags refspec when user asked for tags (implicitly) or
-		 * specified --branch, whose argument might be a tag.
-		 */
-		refspec_append(&remote->fetch, TAG_REFSPEC);
-
-	refspec_ref_prefixes(&remote->fetch,
-			     &transport_ls_refs_options.ref_prefixes);
 	if (option_branch)
 		expand_ref_prefix(&transport_ls_refs_options.ref_prefixes,
 				  option_branch);
@@ -1508,7 +1510,7 @@ int cmd_clone(int argc,
 	 * ref_prefixes will be empty so we fall back to asking about HEAD to
 	 * reduce traffic from the server.
 	 */
-	if (opts.wants_head || transport_ls_refs_options.ref_prefixes.nr == 0)
+	if ((opts.wants_head || transport_ls_refs_options.ref_prefixes.nr == 0) && !option_rev)
 		strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
 
 	refs = transport_get_remote_refs(transport, &transport_ls_refs_options);
@@ -1525,6 +1527,12 @@ int cmd_clone(int argc,
 	create_reference_database(the_repository, NULL, 1);
 	startup_info->have_repository = 1;
 
+	if (option_rev) {
+		remote->fetch.hash_algo = the_repository->hash_algo;
+		rev_refspec_idx = remote->fetch.nr;
+		refspec_append(&remote->fetch, option_rev);
+	}
+
 	/*
 	 * Before fetching from the remote, download and install bundle
 	 * data from the --bundle-uri option.
@@ -1589,7 +1597,7 @@ int cmd_clone(int argc,
 	}
 
 	if (refs)
-		mapped_refs = wanted_peer_refs(&opts, refs, &remote->fetch);
+		mapped_refs = wanted_peer_refs(&opts, refs, &remote->fetch, rev_refspec_idx);
 
 	if (mapped_refs) {
 		/*
t/t5621-clone-revision.sh +19 -0
index 54789423f8..a52c0a1d0e 100755
@@ -78,6 +78,25 @@ test_expect_success 'clone with --revision being a raw commit hash' '
 	test_must_fail git -C dst config remote.origin.fetch
 '
 
+test_expect_success 'clone with --revision being a raw SHA-256 commit hash' '
+	test_when_finished "rm -rf src256 dst256" &&
+
+	git init --object-format=sha256 src256 &&
+	test_commit -C src256 "test" &&
+	oid=$(git -C src256 rev-parse HEAD) &&
+
+	GIT_DEFAULT_HASH=sha1 git clone --no-local --revision=$oid src256 dst256 &&
+
+	echo $oid >expect &&
+	git -C dst256 rev-parse HEAD >actual &&
+	test_cmp expect actual &&
+
+	git -C dst256 for-each-ref refs >actual &&
+	test_must_be_empty actual &&
+
+	test_must_fail git -C dst256 config remote.origin.fetch
+'
+
 test_expect_success 'clone with --revision and --bare' '
 	test_when_finished "rm -rf dst" &&
 	git clone --revision=refs/heads/main --bare . dst &&
Signature · 1 line
2.55.0