All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Andrei Șova" <andreisva2023@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] builtin/clone: fix --revision with SHA-256 commits
Date: Fri, 20 Nov 2026 09:36:04 -0800	[thread overview]
Message-ID: <xmqqv75rqubf.fsf@gitster.g> (raw)
In-Reply-To: <87mrr486sg.fsf@gmail.com> (UTF's message of "Thu, 19 Nov 2026 21:00:52 -0700")

"=?UTF-8?q?Andrei=20=C8=98ova?=" <andreisva2023@gmail.com> 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.

> 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" &&

Clean-up preparation.

> +	git init --object-format=sha256 src256 &&
> +	test_commit -C src256 --no-tag "initial commit" &&
> +	oid=$(git -C src256 rev-parse HEAD) &&

Test set-up.

> +	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.

> +	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.

> +	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)?

> +	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.

      reply	other threads:[~2026-11-20 17:36 UTC|newest]

Thread overview: 2+ 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 17:36 ` Junio C Hamano [this message]

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=xmqqv75rqubf.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=andreisva2023@gmail.com \
    --cc=git@vger.kernel.org \
    /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.