All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Adrian Ratiu <adrian.ratiu@collabora.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
	"brian m . carlson" <sandals@crustytoothpaste.net>,
	Axel Rasmussen <axelrasmussen@google.com>,
	Emily Shaffer <emilyshaffer@google.com>,
	Josh Steadmon <steadmon@google.com>
Subject: Re: [PATCH] ls-remote: add --show-object-format
Date: Thu, 5 Nov 2026 15:13:02 +0100	[thread overview]
Message-ID: <auyO7lmLKD2OSwnD@pks.im> (raw)
In-Reply-To: <20261105104541.790881-1-adrian.ratiu@collabora.com>

On Thu, Nov 05, 2026 at 12:45:41PM +0200, Adrian Ratiu wrote:
> While working on JJ sha256 remote support (it uses Git for networking),
> I noticed there is no way to get the object format of a repo without
> cloning it. [1]
> 
> Git clone learns the remote format during the protocol handshake, but
> it is not shown anywhere else. For example, when creating an empty repo
> and running "git fetch":
> 
> 1. If the remote has references, fetch can fail with a mismatch error
> and "git ls-remote" shows the format indirectly through the length
> of the object IDs. Not ideal, but the it can be figured out eventually.

s/the it/then it/

Also, this is a somewhat imperfect way to detect the hash algorithm as
it assumes that we don't ever add a third hash that has the same length
as either of the existing ones. So it works now, but it may not work in
the future anymore.

> 2. If the remote is empty, "git ls-remote" prints nothing and fetch
> succeeds, so the new local repo can silently get the wrong format,
> which only shows up later, on the first push.
> 
> To fill this gap, add a ls-remote --show-object-format option which
> prints the object format learned from the remote on a line of its own,
> before the references.

Seems sensible.

> diff --git a/Documentation/git-ls-remote.adoc b/Documentation/git-ls-remote.adoc
> index d71c4ab3e2..1abf7b60b7 100644
> --- a/Documentation/git-ls-remote.adoc
> +++ b/Documentation/git-ls-remote.adoc
> @@ -64,6 +65,13 @@ OPTIONS
>  	upload-pack only shows the symref HEAD, so it will be the only
>  	one shown by ls-remote.
>  
> +--show-object-format::
> +	Show the object format (hash algorithm, e.g. `sha1` or `sha256`) used
> +	by the remote repository before printing the references, on a line of
> +	its own. This also works for a remote repo without any refs, which is
> +	useful to find out which object format to use for a local repository,

A bit curious that you first say "repo" and then "repository". I'd just
drop "repo".

> diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
> index fe77829557..6d961f9961 100644
> --- a/builtin/ls-remote.c
> +++ b/builtin/ls-remote.c
> @@ -150,6 +154,8 @@ int cmd_ls_remote(int argc,
>  
>  	if (!dest && !quiet)
>  		fprintf(stderr, "From %s\n", remote->url.v[0]);
> +	if (show_object_format)
> +		printf("%s\n", transport_get_hash_algo(transport)->name);

Should we maybe make this a tiny bit more extensible by not printing the
object format without any kind of prefix? We could for example instead
print "object-format: %s", which would allow us to add more fields in
the future without the need to make parsers be order-dependent.

Also, one more question I had is whether we know that the hash algo will
always be populated here. I would assume so, as we have this logic a
couple lines further up:

	ref = transport_get_remote_refs(transport, &transport_options);
	if (ref) {
		int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
		repo_set_hash_algo(the_repository, hash_algo);
	}

But in theory, this logic would also work alright if the hash was NULL.

> diff --git a/t/t5512-ls-remote.sh b/t/t5512-ls-remote.sh
> index 2e1d4f7586..7e82154fe4 100755
> --- a/t/t5512-ls-remote.sh
> +++ b/t/t5512-ls-remote.sh
> @@ -332,6 +332,52 @@ test_expect_success 'indicate no refs in v0 standards-compliant empty remote' '
>  	test_expect_code 2 git ls-remote --exit-code --upload-pack=./cat-input .
>  '
>  
> +# Run "ls-remote --show-object-format" with the given arguments over
> +# protocol v0 and v2, and compare its output with "expect".
> +test_show_object_format () {
> +	for version in 0 2
> +	do
> +		git -c protocol.version=$version \
> +			ls-remote --show-object-format "$@" >actual &&
> +		test_cmp expect actual || return 1

It's a bit weird that we expect the caller to already have written
`expect`. A better interface would be to have them call the expected
object format as another parameter.

> +	done
> +}
> +
> +test_expect_success 'ls-remote --show-object-format with empty sha1 remote' '
> +	git init --bare --object-format=sha1 empty-sha1.git &&
> +	echo sha1 >expect &&
> +	test_show_object_format empty-sha1.git
> +'
> +
> +test_expect_success 'ls-remote --show-object-format with empty sha256 remote' '
> +	git init --bare --object-format=sha256 empty-sha256.git &&
> +	echo sha256 >expect &&
> +	test_show_object_format empty-sha256.git
> +'

We could easily deduplicate these tests by looping around `for hash in
sha1 sha256`.

Thanks!

Patrick

  reply	other threads:[~2026-11-05 14:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05 10:45 [PATCH] ls-remote: add --show-object-format Adrian Ratiu
2026-11-05 14:13 ` Patrick Steinhardt [this message]
2026-11-05 16:35   ` Adrian Ratiu

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=auyO7lmLKD2OSwnD@pks.im \
    --to=ps@pks.im \
    --cc=adrian.ratiu@collabora.com \
    --cc=axelrasmussen@google.com \
    --cc=emilyshaffer@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=sandals@crustytoothpaste.net \
    --cc=steadmon@google.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.