{"thread":{"id":"66892","subject":"[PATCH] builtin/clone: fix --revision with SHA-256 commits","startedAt":"2026-11-20T04:00:52Z","lastAt":"2026-11-20T04:00:52Z","messageCount":1,"participants":["=?UTF-8?q?Andrei=20=C8=98ova?="],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558906","messageId":"87mrr486sg.fsf@gmail.com","threadId":"66892","inReplyTo":null,"subject":"[PATCH] builtin/clone: fix --revision with SHA-256 commits","fromName":"=?UTF-8?q?Andrei=20=C8=98ova?=","fromEmail":"andreisva2023@gmail.com","sentAt":"2026-11-20T04:00:52Z","receivedAt":"2026-11-20T04:00:52Z","isPatch":true,"sender":{"key":"andreisva2023@gmail.com","avatar":null},"body":"The refspec item is created before the destination repository's hash algorithm\nis set up, so when cloning repositories using the SHA-256 object format\nparse_refspec incorrectly parses the hash.\n\nSigned-off-by: Andrei Șova <andreisva2023@gmail.com>\n---\nHello Git developers,\n\nI was messing around with a repository using the SHA-256 object format\nand I noticed that --revision doesn't work if you give it a SHA-256\ncommit hash, so I put together this fix and a test case.\n\nThanks,\nAndrei Șova\n\n\n builtin/clone.c           | 15 ++++++++++++++-\n t/t5621-clone-revision.sh | 15 +++++++++++++++\n 2 files changed, 29 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/clone.c b/builtin/clone.c\nindex 4b9929b980..69513a14c2 100644\n--- a/builtin/clone.c\n+++ b/builtin/clone.c\n@@ -963,6 +963,7 @@ int cmd_clone(int argc,\n \tstruct string_list server_options = STRING_LIST_INIT_NODUP;\n \tconst char *bundle_uri = NULL;\n \tchar *option_rev = NULL;\n+\tint rev_refspec_idx = 0;\n \n \tstruct clone_opts opts = CLONE_OPTS_INIT;\n \n@@ -1484,6 +1485,7 @@ int cmd_clone(int argc,\n \t\topts.wants_head = 0;\n \t\topts.detach = 1;\n \n+\t\trev_refspec_idx = remote->fetch.nr;\n \t\trefspec_append(&remote->fetch, option_rev);\n \t}\n \n@@ -1525,6 +1527,17 @@ int cmd_clone(int argc,\n \tcreate_reference_database(the_repository, NULL, 1);\n \tstartup_info->have_repository = 1;\n \n+\tif (option_rev) {\n+\t\t/*\n+\t\t * Re-initialize the refspec since potential hashes might have been\n+\t\t * parsed under the wrong algorithm.\n+\t\t */\n+\t\tstruct refspec_item *r = &remote->fetch.items[rev_refspec_idx];\n+\t\trefspec_item_clear(r);\n+\t\tif (!refspec_item_init_fetch(r, option_rev, the_repository->hash_algo))\n+\t\t\tdie(_(\"invalid refspec '%s'\"), option_rev);\n+\t}\n+\n \t/*\n \t * Before fetching from the remote, download and install bundle\n \t * data from the --bundle-uri option.\n@@ -1588,7 +1601,7 @@ int cmd_clone(int argc,\n \t\t}\n \t}\n \n-\tif (refs)\n+\tif (refs || (option_rev && remote->fetch.items[rev_refspec_idx].exact_sha1))\n \t\tmapped_refs = wanted_peer_refs(&opts, refs, &remote->fetch);\n \n \tif (mapped_refs) {\ndiff --git a/t/t5621-clone-revision.sh b/t/t5621-clone-revision.sh\nindex 54789423f8..1afac8abaa 100755\n--- a/t/t5621-clone-revision.sh\n+++ b/t/t5621-clone-revision.sh\n@@ -78,6 +78,21 @@ test_expect_success 'clone with --revision being a raw commit hash' '\n \ttest_must_fail git -C dst config remote.origin.fetch\n '\n \n+test_expect_success 'clone with --revision being a raw SHA-256 commit hash' '\n+\ttest_when_finished \"rm -rf src256 dst256\" &&\n+\tgit init --object-format=sha256 src256 &&\n+\ttest_commit -C src256 --no-tag \"initial commit\" &&\n+\toid=$(git -C src256 rev-parse HEAD) &&\n+\tGIT_DEFAULT_HASH=sha1 git clone --no-local --revision=$oid src256 dst256 &&\n+\techo $oid >expect &&\n+\tgit -C dst256 rev-parse HEAD >actual &&\n+\ttest_must_fail git -C dst256 symbolic-ref -q HEAD >/dev/null &&\n+\ttest_cmp expect actual &&\n+\tgit -C dst256 for-each-ref refs >actual &&\n+\ttest_must_be_empty actual &&\n+\ttest_must_fail git -C dst256 config remote.origin.fetch\n+'\n+\n test_expect_success 'clone with --revision and --bare' '\n \ttest_when_finished \"rm -rf dst\" &&\n \tgit clone --revision=refs/heads/main --bare . dst &&\n-- \n2.55.0\n\n"}]}