git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] builtin/clone: fix --revision with SHA-256 commits
@ 2026-11-20  4:00 =?UTF-8?q?Andrei=20=C8=98ova?=
  2026-11-20 17:36 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: =?UTF-8?q?Andrei=20=C8=98ova?= @ 2026-11-20  4:00 UTC (permalink / raw)
  To: git

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.

Signed-off-by: Andrei Șova <andreisva2023@gmail.com>
---
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(-)

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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-20 17:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  4:00 [PATCH] builtin/clone: fix --revision with SHA-256 commits =?UTF-8?q?Andrei=20=C8=98ova?=
2026-11-20 17:36 ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).