All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Andrei Șova" <andreisva2023@gmail.com>
To: gitster@pobox.com
Cc: git@vger.kernel.org
Subject: [PATCH v2] builtin/clone: fix --revision when remote / local hash algos differ
Date: Thu, 19 Nov 2026 21:00:52 -0700	[thread overview]
Message-ID: <87a4n3ufqw.fsf@gmail.com> (raw)
In-Reply-To: <87mrr486sg.fsf@gmail.com>

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.

Signed-off-by: Andrei Șova <andreisva2023@gmail.com>
---
 builtin/clone.c           | 38 +++++++++++++++++++++++---------------
 t/t5621-clone-revision.sh | 19 +++++++++++++++++++
 2 files changed, 42 insertions(+), 15 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index 4b9929b980..07a4c2db98 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -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) {
 		/*
diff --git a/t/t5621-clone-revision.sh b/t/t5621-clone-revision.sh
index 54789423f8..a52c0a1d0e 100755
--- a/t/t5621-clone-revision.sh
+++ b/t/t5621-clone-revision.sh
@@ -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 &&
-- 
2.55.0

  reply	other threads:[~2026-11-21  1:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  4:00 [PATCH] builtin/clone: fix --revision with SHA-256 commits =?UTF-8?q?Andrei=20=C8=98ova?=
2026-11-20  4:00 ` Andrei Șova [this message]
2026-11-20 17:36 ` Junio C Hamano
2026-11-20 20:36   ` Andrei Șova

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87a4n3ufqw.fsf@gmail.com \
    --to=andreisva2023@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.