* Re: [PATCH] setup: default hash algo to GIT_HASH_DEFAULT
2026-11-08 1:16 ` [PATCH] setup: default hash algo to GIT_HASH_DEFAULT brian m. carlson
@ 2026-11-08 4:42 ` Junio C Hamano
2026-11-08 14:38 ` brian m. carlson
2026-11-08 18:22 ` Junio C Hamano
` (2 subsequent siblings)
3 siblings, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2026-11-08 4:42 UTC (permalink / raw)
To: brian m. carlson; +Cc: git, Adam Dinwoodie
"brian m. carlson" <sandals@crustytoothpaste.net> writes:
> When we have no other hash algorithm to choose, we set no hash
> algorithm, which ends up with us defaulting to SHA-1. Instead, default
> to GIT_HASH_DEFAULT. This means that we will choose SHA-256 in breaking
> changes mode and in Git 3.0, which is what we want.
>
> Reported-by: Adam Dinwoodie <adam@dinwoodie.org>
> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
> ---
> setup.c | 2 ++
> t/t0001-init.sh | 16 ++++++++++++++++
> 2 files changed, 18 insertions(+)
With Git 3.0, we promised that SHA-1/SHA-256 interop will not have
to be ready, and that SHA-256 would become the default. And this
patch would be a good thing to have for the second promise.
But didn't we also discuss "empty/initial repository whose hash
algorithm is undecided can take a push of either SHA-1 or SHA-256
payload and that first push can decide and freeze the hash algorithm
used in the repository" or something along that line?
I am wondering if we want to reserve 'HASH_UNKNOWN' for such a "was
just created, not quite finished initializing yet" state.
The patch looks good for the goal stated in the log message.
Thanks.
> diff --git a/setup.c b/setup.c
> index 61bcdafe82..4a0dfc8908 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -2830,6 +2830,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,
> repo_fmt->hash_algo = env_algo;
> } else if (cfg.hash != GIT_HASH_UNKNOWN) {
> repo_fmt->hash_algo = cfg.hash;
> + } else {
> + repo_fmt->hash_algo = GIT_HASH_DEFAULT;
> }
>
> /*
> diff --git a/t/t0001-init.sh b/t/t0001-init.sh
> index f1d227f271..98c7562f32 100755
> --- a/t/t0001-init.sh
> +++ b/t/t0001-init.sh
> @@ -516,6 +516,22 @@ test_expect_success 're-init from a linked worktree' '
> )
> '
>
> +test_expect_success 'git init uses default object format' '
> + test_when_finished "rm -rf repo" &&
> + (
> + sane_unset GIT_DEFAULT_HASH &&
> + git init repo &&
> + if test_have_prereq WITH_BREAKING_CHANGES
> + then
> + echo sha256
> + else
> + echo sha1
> + fi >expect &&
> + git -C repo rev-parse --show-object-format >actual &&
> + test_cmp expect actual
> + )
> +'
> +
> test_expect_success 'init honors GIT_DEFAULT_HASH' '
> test_when_finished "rm -rf sha1 sha256" &&
> GIT_DEFAULT_HASH=sha1 git init sha1 &&
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [PATCH] setup: default hash algo to GIT_HASH_DEFAULT
2026-11-08 4:42 ` Junio C Hamano
@ 2026-11-08 14:38 ` brian m. carlson
0 siblings, 0 replies; 16+ messages in thread
From: brian m. carlson @ 2026-11-08 14:38 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git, Adam Dinwoodie, Jeff King
[-- Attachment #1: Type: text/plain, Size: 2037 bytes --]
On 2026-11-08 at 04:42:56, Junio C Hamano wrote:
> "brian m. carlson" <sandals@crustytoothpaste.net> writes:
>
> > When we have no other hash algorithm to choose, we set no hash
> > algorithm, which ends up with us defaulting to SHA-1. Instead, default
> > to GIT_HASH_DEFAULT. This means that we will choose SHA-256 in breaking
> > changes mode and in Git 3.0, which is what we want.
> >
> > Reported-by: Adam Dinwoodie <adam@dinwoodie.org>
> > Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
> > ---
> > setup.c | 2 ++
> > t/t0001-init.sh | 16 ++++++++++++++++
> > 2 files changed, 18 insertions(+)
>
> With Git 3.0, we promised that SHA-1/SHA-256 interop will not have
> to be ready, and that SHA-256 would become the default. And this
> patch would be a good thing to have for the second promise.
>
> But didn't we also discuss "empty/initial repository whose hash
> algorithm is undecided can take a push of either SHA-1 or SHA-256
> payload and that first push can decide and freeze the hash algorithm
> used in the repository" or something along that line?
We discussed that as a possibility, but it requires protocol changes and
nobody has stepped up to implement that. I think Peff was thinking
about maybe implementing that, but I don't think anyone came to a
decision.
> I am wondering if we want to reserve 'HASH_UNKNOWN' for such a "was
> just created, not quite finished initializing yet" state.
I think we concluded that we'd need to initialize a repository to _some_
hash algorithm even if we had the limbo state because otherwise people
could `git init` and then work locally and we need to have some existing
hash algorithm for that case. That would be true even with bare
repositories.
So I think the limbo functionality, if we implemented it, would be a
separate option (maybe `extensions.potentialObjectFormat`?) and this
patch should go in either way.
--
brian m. carlson (they/them)
Toronto, Ontario, CA
Speaking for myself only
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 325 bytes --]
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH] setup: default hash algo to GIT_HASH_DEFAULT
2026-11-08 1:16 ` [PATCH] setup: default hash algo to GIT_HASH_DEFAULT brian m. carlson
2026-11-08 4:42 ` Junio C Hamano
@ 2026-11-08 18:22 ` Junio C Hamano
2026-11-09 2:21 ` Junio C Hamano
2026-11-13 22:17 ` [PATCH v2] setup: fix hash algorithm detection brian m. carlson
2026-11-19 1:06 ` [PATCH v3] " brian m. carlson
3 siblings, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2026-11-08 18:22 UTC (permalink / raw)
To: brian m. carlson; +Cc: git, Adam Dinwoodie, Patrick Steinhardt
"brian m. carlson" <sandals@crustytoothpaste.net> writes:
> When we have no other hash algorithm to choose, we set no hash
> algorithm, which ends up with us defaulting to SHA-1. Instead, default
> to GIT_HASH_DEFAULT. This means that we will choose SHA-256 in breaking
> changes mode and in Git 3.0, which is what we want.
>
> Reported-by: Adam Dinwoodie <adam@dinwoodie.org>
> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
> ---
> setup.c | 2 ++
> t/t0001-init.sh | 16 ++++++++++++++++
> 2 files changed, 18 insertions(+)
>
> diff --git a/setup.c b/setup.c
> index 61bcdafe82..4a0dfc8908 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -2830,6 +2830,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,
> repo_fmt->hash_algo = env_algo;
> } else if (cfg.hash != GIT_HASH_UNKNOWN) {
> repo_fmt->hash_algo = cfg.hash;
> + } else {
> + repo_fmt->hash_algo = GIT_HASH_DEFAULT;
> }
Hmph, maybe I am not reading the code flow correctly, but does this
not break running "git init" to reinitialize an already initialized
repository? I mimicked the new test in your patch and wrote the
following just immediately after it, which
* figures out our hash and the other hash
* create a new repository with the "other" hash
* run "git init" without specifying any hash
It is debatable what should happen when you run "git init" from Git
3.0 without any specialization in an existing SHA-1 repository.
Possibilities include:
1. turn an existing SHA-1 repository into SHA-256 including format
conversion.
2. notice that the repository is already set up to use the SHA-1
format, and that the user did not explicitly say what format to
use, and behave as if --object-format=sha1 were given with a
notice or warning.
3. notice that the repository is already set up to use the SHA-1
format, but our default format is SHA-256. Because the user did
not countermand via an explicit --object-format=sha1 on the
command line, we should fail loudly, "cannot convert this SHA-1
repository to SHA-256".
I think #1 is impractical, #2 is probably the best, and #3 would
also be reasonable.
But the behaviour I am seeing with
$ make WITH_BREAKING_CHANGES=YesPlease
$ cd t && WITH_BREAKING_CHANGES=YesPlease sh t0001-init.sh -i -v
with the additional patch is different from any of the three (well,
technically, because the repository is empty, we _could_ argue that
we did #1, but ...).
I wonder what the "git init" command does wrt the reference storage
format (Patrick CC'ed for this)? I am sure we do not convert from
reffiles to reftable on the fly at "git init" time.
diff --git i/t/t0001-init.sh w/t/t0001-init.sh
index bc30cf853b..1d48a712ae 100755
--- i/t/t0001-init.sh
+++ w/t/t0001-init.sh
@@ -532,6 +532,31 @@ test_expect_success 'git init uses default object format' '
)
'
+test_expect_success 'git (re)init does not clobber object format' '
+ test_when_finished "rm -rf repo" &&
+ (
+ sane_unset GIT_DEFAULT_HASH &&
+ if test_have_prereq WITH_BREAKING_CHANGES
+ then
+ ours=sha256 opposite=sha1
+ else
+ ours=sha1 opposite=sha256
+ fi &&
+
+ # initialize to non-default format
+ git init --object-format=$opposite repo &&
+ git -C repo rev-parse --show-object-format >actual &&
+ echo "$opposite" >expect &&
+ test_cmp expect actual &&
+
+ # reinitialize without configuration
+ git init repo &&
+ git -C repo rev-parse --show-object-format >actual &&
+ echo "$opposite" >expect &&
+ test_cmp expect actual
+ )
+'
+
test_expect_success 'init honors GIT_DEFAULT_HASH' '
test_when_finished "rm -rf sha1 sha256" &&
GIT_DEFAULT_HASH=sha1 git init sha1 &&
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH] setup: default hash algo to GIT_HASH_DEFAULT
2026-11-08 18:22 ` Junio C Hamano
@ 2026-11-09 2:21 ` Junio C Hamano
2026-11-09 9:54 ` Patrick Steinhardt
0 siblings, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2026-11-09 2:21 UTC (permalink / raw)
To: brian m. carlson; +Cc: git, Adam Dinwoodie, Patrick Steinhardt
Junio C Hamano <gitster@pobox.com> writes:
> I wonder what the "git init" command does wrt the reference storage
> format (Patrick CC'ed for this)? I am sure we do not convert from
> reffiles to reftable on the fly at "git init" time.
I looked at setup.c:repository_format_configure() and found why
your patch regresses, as well as a latent bug in the existing code.
First, REPOSITORY_FORMAT_INIT sets .hash_algo = GIT_HASH_DEFAULT,
but read_repository_format() forces it to GIT_HASH_SHA1_LEGACY
when version == -1 (no config yet), which is why new repositories
kept getting SHA-1.
Second, unlike the ref-storage block right below it, which checks
"else if (repo_fmt->version >= 0)" once between the explicit
overrides and all default sources, the hash-algo block guards only
the GIT_DEFAULT_HASH branch (repo_fmt->version < 0), leaving "else
if (cfg.hash != GIT_HASH_UNKNOWN)" unguarded.
So init.defaultObjectFormat in ~/.gitconfig already corrupts an
existing repository on re-init, and an unguarded "else" for
GIT_HASH_DEFAULT falls into the same trap even without config.
Instead of starting the if/else if cascade like this:
if (repo_fmt->version >= 0 &&
hash != GIT_HASH_UNKNOWN &&
hash != repo_fmt->hash_algo)
die(_("attempt to reinitialize repository with different hash"));
we should have started it like so:
if (repo_fmt->version >= 0) {
if (hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
die(_("attempt to reinitialize repository with different hash"));
/* otherwise happy */
} else if ...
so that all the "set a new value from different sources like command
line, environment, ~/.gitconfig, or the built-in default" would
happen only when (repo_fmt->version < 0), probably?
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [PATCH] setup: default hash algo to GIT_HASH_DEFAULT
2026-11-09 2:21 ` Junio C Hamano
@ 2026-11-09 9:54 ` Patrick Steinhardt
0 siblings, 0 replies; 16+ messages in thread
From: Patrick Steinhardt @ 2026-11-09 9:54 UTC (permalink / raw)
To: Junio C Hamano; +Cc: brian m. carlson, git, Adam Dinwoodie
On Sun, Nov 08, 2026 at 06:21:43PM -0800, Junio C Hamano wrote:
> Junio C Hamano <gitster@pobox.com> writes:
>
> > I wonder what the "git init" command does wrt the reference storage
> > format (Patrick CC'ed for this)? I am sure we do not convert from
> > reffiles to reftable on the fly at "git init" time.
Yeah, we don't, and I don't think that would be reasonable. I would very
much be surprised if git-init(1) migrated a repository's hash format,
ref storage format or object storage format. Those are long-running
operations and in the past, git-init(1) used to be somewhat idempotent
on an already-initialized repository. So I'd much rather have us migrate
explicitly, like we do with `git refs migrate`.
> I looked at setup.c:repository_format_configure() and found why
> your patch regresses, as well as a latent bug in the existing code.
>
> First, REPOSITORY_FORMAT_INIT sets .hash_algo = GIT_HASH_DEFAULT,
> but read_repository_format() forces it to GIT_HASH_SHA1_LEGACY
> when version == -1 (no config yet), which is why new repositories
> kept getting SHA-1.
>
> Second, unlike the ref-storage block right below it, which checks
> "else if (repo_fmt->version >= 0)" once between the explicit
> overrides and all default sources, the hash-algo block guards only
> the GIT_DEFAULT_HASH branch (repo_fmt->version < 0), leaving "else
> if (cfg.hash != GIT_HASH_UNKNOWN)" unguarded.
>
> So init.defaultObjectFormat in ~/.gitconfig already corrupts an
> existing repository on re-init, and an unguarded "else" for
> GIT_HASH_DEFAULT falls into the same trap even without config.
>
> Instead of starting the if/else if cascade like this:
>
> if (repo_fmt->version >= 0 &&
> hash != GIT_HASH_UNKNOWN &&
> hash != repo_fmt->hash_algo)
> die(_("attempt to reinitialize repository with different hash"));
>
> we should have started it like so:
>
> if (repo_fmt->version >= 0) {
> if (hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
> die(_("attempt to reinitialize repository with different hash"));
> /* otherwise happy */
> } else if ...
>
> so that all the "set a new value from different sources like command
> line, environment, ~/.gitconfig, or the built-in default" would
> happen only when (repo_fmt->version < 0), probably?
Yeah, these blocks are overall quite hard to read. That's also the
reason why I re-did these checks for both the ref storage format and
then also used the new style for the object storage format. So my take
is that we should adapt the object format in a similar fashion so that
all of them use the same style, where we first detect the desired format
and then check whether we're trying to re-initialize with a different
object format towards the end of this function.
So something like this:
diff --git a/setup.c b/setup.c
index 61bcdafe82a..bf75abafeeb 100644
--- a/setup.c
+++ b/setup.c
@@ -2816,20 +2816,19 @@ static void repository_format_configure(struct repository_format *repo_fmt,
* specify a different algorithm, as that could cause corruption.
* Otherwise, if the user has specified one on the command line, use it.
*/
- env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);
- if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
- die(_("attempt to reinitialize repository with different hash"));
- else if (hash != GIT_HASH_UNKNOWN)
- repo_fmt->hash_algo = hash;
- else if (env) {
+ if (hash != GIT_HASH_UNKNOWN) {
+ /* nothing to do */
+ } else if ((env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT))) {
int env_algo = hash_algo_by_name(env);
if (env_algo == GIT_HASH_UNKNOWN)
die(_("unknown hash algorithm '%s'"), env);
if (repo_fmt->version < 0 ||
repo_fmt->hash_algo == GIT_HASH_UNKNOWN)
repo_fmt->hash_algo = env_algo;
- } else if (cfg.hash != GIT_HASH_UNKNOWN) {
- repo_fmt->hash_algo = cfg.hash;
+ } else if (repo_fmt->version >= 0) {
+ hash = repo_fmt->hash_algo;
+ } else {
+ hash = GIT_HASH_DEFAULT;
}
/*
@@ -2899,6 +2898,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,
* current ref storage format does not change.
*/
if (repo_fmt->version >= 0) {
+ if (hash != repo_fmt->hash_algo)
+ die(_("attempt to reinitialize repository with different hash"));
if (ref_storage_format != repo_fmt->ref_storage_format)
die(_("attempt to reinitialize repository with different reference storage format"));
if ((ref_storage_payload || repo_fmt->ref_storage_payload) &&
Patrick
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v2] setup: fix hash algorithm detection
2026-11-08 1:16 ` [PATCH] setup: default hash algo to GIT_HASH_DEFAULT brian m. carlson
2026-11-08 4:42 ` Junio C Hamano
2026-11-08 18:22 ` Junio C Hamano
@ 2026-11-13 22:17 ` brian m. carlson
2026-11-14 0:16 ` Junio C Hamano
2026-11-16 6:22 ` Patrick Steinhardt
2026-11-19 1:06 ` [PATCH v3] " brian m. carlson
3 siblings, 2 replies; 16+ messages in thread
From: brian m. carlson @ 2026-11-13 22:17 UTC (permalink / raw)
To: git; +Cc: Junio C Hamano, Patrick Steinhardt, Adam Dinwoodie
Our current hash algorithm selection code has a few different problems.
First, we do not actually default to GIT_HASH_DEFAULT. Second, if we
have an algorithm chosen by init.defaultObjectFormat and we re-init a
repository, we will change the hash algorithm, which is very much not
desired.
Let's restructure this code slightly. First, if an algorithm was
selected on the command line, assume that's what the user wanted.
Otherwise, the other options should only be chosen if our repository
doesn't exist (that is, repo_fmt->version is -1) or it's specifically
detected as having no hash algorithm. In that case, first use the
environment, then the config, and finally, the default algorithm.
Then, if the algorithm does not match the repository, complain loudly
and refuse to reinitialize the repository with a different hash
algorithm.
With this change, we will choose SHA-256 in breaking changes mode and in
Git 3.0, which is what we want. Add several tests to cover this case
and several others discovered along the way.
Reported-by: Adam Dinwoodie <adam@dinwoodie.org>
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
setup.c | 22 +++++++++++--------
t/t0001-init.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 71 insertions(+), 9 deletions(-)
diff --git a/setup.c b/setup.c
index 61bcdafe82..14c6e1a67d 100644
--- a/setup.c
+++ b/setup.c
@@ -2817,19 +2817,20 @@ static void repository_format_configure(struct repository_format *repo_fmt,
* Otherwise, if the user has specified one on the command line, use it.
*/
env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);
- if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
- die(_("attempt to reinitialize repository with different hash"));
- else if (hash != GIT_HASH_UNKNOWN)
- repo_fmt->hash_algo = hash;
- else if (env) {
+ if (hash != GIT_HASH_UNKNOWN) {
+ /* nothing to do */
+ } else if (repo_fmt->version >= 0 &&
+ repo_fmt->hash_algo != GIT_HASH_UNKNOWN) {
+ hash = repo_fmt->hash_algo;
+ } else if (env) {
int env_algo = hash_algo_by_name(env);
if (env_algo == GIT_HASH_UNKNOWN)
die(_("unknown hash algorithm '%s'"), env);
- if (repo_fmt->version < 0 ||
- repo_fmt->hash_algo == GIT_HASH_UNKNOWN)
- repo_fmt->hash_algo = env_algo;
+ hash = env_algo;
} else if (cfg.hash != GIT_HASH_UNKNOWN) {
- repo_fmt->hash_algo = cfg.hash;
+ hash = cfg.hash;
+ } else {
+ hash = GIT_HASH_DEFAULT;
}
/*
@@ -2899,6 +2900,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,
* current ref storage format does not change.
*/
if (repo_fmt->version >= 0) {
+ if (hash != repo_fmt->hash_algo)
+ die(_("attempt to reinitialize repository with different hash"));
if (ref_storage_format != repo_fmt->ref_storage_format)
die(_("attempt to reinitialize repository with different reference storage format"));
if ((ref_storage_payload || repo_fmt->ref_storage_payload) &&
@@ -2908,6 +2911,7 @@ static void repository_format_configure(struct repository_format *repo_fmt,
}
free(repo_fmt->ref_storage_payload);
+ repo_fmt->hash_algo = hash;
repo_fmt->ref_storage_format = ref_storage_format;
repo_fmt->ref_storage_payload = ref_storage_payload;
}
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index f1d227f271..d8244a2dec 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -516,6 +516,22 @@ test_expect_success 're-init from a linked worktree' '
)
'
+test_expect_success 'git init uses default object format' '
+ test_when_finished "rm -rf repo" &&
+ (
+ sane_unset GIT_DEFAULT_HASH &&
+ git init repo &&
+ if test_have_prereq WITH_BREAKING_CHANGES
+ then
+ echo sha256
+ else
+ echo sha1
+ fi >expect &&
+ git -C repo rev-parse --show-object-format >actual &&
+ test_cmp expect actual
+ )
+'
+
test_expect_success 'init honors GIT_DEFAULT_HASH' '
test_when_finished "rm -rf sha1 sha256" &&
GIT_DEFAULT_HASH=sha1 git init sha1 &&
@@ -602,6 +618,48 @@ do
'
done
+test_expect_success 'init refuses to reinitialize with different hash' '
+ test_when_finished "rm -rf repo" &&
+ sane_unset GIT_DEFAULT_HASH &&
+ for algo in sha1 sha256
+ do
+ if test "$algo" = sha1
+ then
+ other=sha256
+ else
+ other=sha1
+ fi &&
+ git init --object-format="$algo" repo &&
+ cat >expect <<-EOF &&
+ fatal: attempt to reinitialize repository with different hash
+ EOF
+ test_must_fail git init --object-format="$other" repo 2>err &&
+ test_cmp expect err &&
+ rm -fr repo || return 1
+ done
+'
+
+test_expect_success 're-init does not modify hash from config' '
+ test_when_finished "rm -rf repo" &&
+ sane_unset GIT_DEFAULT_HASH &&
+ for algo in sha1 sha256
+ do
+ if test "$algo" = sha1
+ then
+ other=sha256
+ else
+ other=sha1
+ fi &&
+ git init --object-format="$algo" repo &&
+ test_config_global init.defaultObjectFormat "$other" &&
+ git init repo &&
+ echo "$algo" >expect &&
+ git -C repo rev-parse --show-object-format >actual &&
+ test_cmp expect actual &&
+ rm -fr repo || return 1
+ done
+'
+
test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
test_when_finished "rm -rf explicit-v0" &&
git init --object-format=sha256 explicit-v0 &&
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH v2] setup: fix hash algorithm detection
2026-11-13 22:17 ` [PATCH v2] setup: fix hash algorithm detection brian m. carlson
@ 2026-11-14 0:16 ` Junio C Hamano
2026-11-16 6:22 ` Patrick Steinhardt
1 sibling, 0 replies; 16+ messages in thread
From: Junio C Hamano @ 2026-11-14 0:16 UTC (permalink / raw)
To: brian m. carlson; +Cc: git, Patrick Steinhardt, Adam Dinwoodie
"brian m. carlson" <sandals@crustytoothpaste.net> writes:
> Our current hash algorithm selection code has a few different problems.
> First, we do not actually default to GIT_HASH_DEFAULT. Second, if we
> have an algorithm chosen by init.defaultObjectFormat and we re-init a
> repository, we will change the hash algorithm, which is very much not
> desired.
>
> Let's restructure this code slightly. First, if an algorithm was
> selected on the command line, assume that's what the user wanted.
> Otherwise, the other options should only be chosen if our repository
> doesn't exist (that is, repo_fmt->version is -1) or it's specifically
> detected as having no hash algorithm. In that case, first use the
> environment, then the config, and finally, the default algorithm.
>
> Then, if the algorithm does not match the repository, complain loudly
> and refuse to reinitialize the repository with a different hash
> algorithm.
>
> With this change, we will choose SHA-256 in breaking changes mode and in
> Git 3.0, which is what we want. Add several tests to cover this case
> and several others discovered along the way.
>
> Reported-by: Adam Dinwoodie <adam@dinwoodie.org>
> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
> ---
> setup.c | 22 +++++++++++--------
> t/t0001-init.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 71 insertions(+), 9 deletions(-)
I was somehow expecting a patch that would apply cleanly both to the
maintenance track and to the 2.98 but that is totally unnecessary,
as it does not make any sense to build and use the maintenance track
with the WITH_BREAKING_CHANGES option ;-) The point of the
WITH_BREAKING_CHANGES option is to make sure we have a test vehicle
to try out something that is close enough to "Git 3.0" long before
we release one.
And the updated code structure looks much simpler and more
straightforward.
Will replace.
Thanks.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2] setup: fix hash algorithm detection
2026-11-13 22:17 ` [PATCH v2] setup: fix hash algorithm detection brian m. carlson
2026-11-14 0:16 ` Junio C Hamano
@ 2026-11-16 6:22 ` Patrick Steinhardt
1 sibling, 0 replies; 16+ messages in thread
From: Patrick Steinhardt @ 2026-11-16 6:22 UTC (permalink / raw)
To: brian m. carlson; +Cc: git, Junio C Hamano, Adam Dinwoodie
On Fri, Nov 13, 2026 at 10:17:53PM +0000, brian m. carlson wrote:
> diff --git a/setup.c b/setup.c
> index 61bcdafe82..14c6e1a67d 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -2817,19 +2817,20 @@ static void repository_format_configure(struct repository_format *repo_fmt,
> * Otherwise, if the user has specified one on the command line, use it.
> */
This comment is now a bit stale, as it talks about re-initializing the
repository with an incompatible hash algorithm. But that only gets
detected further down now. Do we maybe want to update it to have the
same bulleted list of precedence that we also have for the ref and
object storage formats?
The code changes look as expected to me and resemble what we have for
the two storage formats.
> diff --git a/t/t0001-init.sh b/t/t0001-init.sh
> index f1d227f271..d8244a2dec 100755
> --- a/t/t0001-init.sh
> +++ b/t/t0001-init.sh
> @@ -516,6 +516,22 @@ test_expect_success 're-init from a linked worktree' '
> )
> '
>
> +test_expect_success 'git init uses default object format' '
> + test_when_finished "rm -rf repo" &&
> + (
> + sane_unset GIT_DEFAULT_HASH &&
> + git init repo &&
> + if test_have_prereq WITH_BREAKING_CHANGES
> + then
> + echo sha256
> + else
> + echo sha1
> + fi >expect &&
I think we can just use GIT_TEST_BUILTIN_HASH for this, which detects
the hash via `git verson --build-options`.
Thanks!
Patrick
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v3] setup: fix hash algorithm detection
2026-11-08 1:16 ` [PATCH] setup: default hash algo to GIT_HASH_DEFAULT brian m. carlson
` (2 preceding siblings ...)
2026-11-13 22:17 ` [PATCH v2] setup: fix hash algorithm detection brian m. carlson
@ 2026-11-19 1:06 ` brian m. carlson
2026-11-19 9:52 ` Patrick Steinhardt
` (2 more replies)
3 siblings, 3 replies; 16+ messages in thread
From: brian m. carlson @ 2026-11-19 1:06 UTC (permalink / raw)
To: git; +Cc: Junio C Hamano, Patrick Steinhardt, Adam Dinwoodie
Our current hash algorithm selection code has a few different problems.
First, we do not actually default to GIT_HASH_DEFAULT. Second, if we
have an algorithm chosen by init.defaultObjectFormat and we re-init a
repository, we will change the hash algorithm, which is very much not
desired.
Let's restructure this code slightly. First, if an algorithm was
selected on the command line, assume that's what the user wanted.
Otherwise, the other options should only be chosen if our repository
doesn't exist (that is, repo_fmt->version is -1) or it's specifically
detected as having no hash algorithm. In that case, first use the
environment, then the config, and finally, the default algorithm.
Then, if the algorithm does not match the repository, complain loudly
and refuse to reinitialize the repository with a different hash
algorithm.
With this change, we will choose SHA-256 in breaking changes mode and in
Git 3.0, which is what we want. Add several tests to cover this case
and several others discovered along the way. Remove the now-inaccurate
comment.
Reported-by: Adam Dinwoodie <adam@dinwoodie.org>
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
Changes since v2:
* Remove comment.
* Use `$GIT_TEST_BUILTIN_HASH`.
setup.c | 27 ++++++++++++-------------
t/t0001-init.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 66 insertions(+), 14 deletions(-)
diff --git a/setup.c b/setup.c
index cedc031c54..5d7e8d590e 100644
--- a/setup.c
+++ b/setup.c
@@ -2887,25 +2887,21 @@ static void repository_format_configure(struct repository_format *repo_fmt,
config_with_options(read_default_format_config, &cfg, NULL, NULL, &opts);
- /*
- * If we already have an initialized repo, don't allow the user to
- * specify a different algorithm, as that could cause corruption.
- * Otherwise, if the user has specified one on the command line, use it.
- */
env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);
- if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
- die(_("attempt to reinitialize repository with different hash"));
- else if (hash != GIT_HASH_UNKNOWN)
- repo_fmt->hash_algo = hash;
- else if (env) {
+ if (hash != GIT_HASH_UNKNOWN) {
+ /* nothing to do */
+ } else if (repo_fmt->version >= 0 &&
+ repo_fmt->hash_algo != GIT_HASH_UNKNOWN) {
+ hash = repo_fmt->hash_algo;
+ } else if (env) {
int env_algo = hash_algo_by_name(env);
if (env_algo == GIT_HASH_UNKNOWN)
die(_("unknown hash algorithm '%s'"), env);
- if (repo_fmt->version < 0 ||
- repo_fmt->hash_algo == GIT_HASH_UNKNOWN)
- repo_fmt->hash_algo = env_algo;
+ hash = env_algo;
} else if (cfg.hash != GIT_HASH_UNKNOWN) {
- repo_fmt->hash_algo = cfg.hash;
+ hash = cfg.hash;
+ } else {
+ hash = GIT_HASH_DEFAULT;
}
/*
@@ -3022,6 +3018,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,
* current storage formats do not change.
*/
if (repo_fmt->version >= 0) {
+ if (hash != repo_fmt->hash_algo)
+ die(_("attempt to reinitialize repository with different hash"));
if (ref_storage_format != repo_fmt->ref_storage_format)
die(_("attempt to reinitialize repository with different reference storage format"));
if ((ref_storage_payload || repo_fmt->ref_storage_payload) &&
@@ -3038,6 +3036,7 @@ static void repository_format_configure(struct repository_format *repo_fmt,
free(cfg.ref_storage_payload);
free(repo_fmt->ref_storage_payload);
+ repo_fmt->hash_algo = hash;
repo_fmt->ref_storage_format = ref_storage_format;
repo_fmt->ref_storage_payload = ref_storage_payload;
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 9e57a06cea..d89f60bab9 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -516,6 +516,17 @@ test_expect_success 're-init from a linked worktree' '
)
'
+test_expect_success 'git init uses default object format' '
+ test_when_finished "rm -rf repo" &&
+ (
+ sane_unset GIT_DEFAULT_HASH &&
+ git init repo &&
+ echo "$GIT_TEST_BUILTIN_HASH" >expect &&
+ git -C repo rev-parse --show-object-format >actual &&
+ test_cmp expect actual
+ )
+'
+
test_expect_success 'init honors GIT_DEFAULT_HASH' '
test_when_finished "rm -rf sha1 sha256" &&
GIT_DEFAULT_HASH=sha1 git init sha1 &&
@@ -602,6 +613,48 @@ do
'
done
+test_expect_success 'init refuses to reinitialize with different hash' '
+ test_when_finished "rm -rf repo" &&
+ sane_unset GIT_DEFAULT_HASH &&
+ for algo in sha1 sha256
+ do
+ if test "$algo" = sha1
+ then
+ other=sha256
+ else
+ other=sha1
+ fi &&
+ git init --object-format="$algo" repo &&
+ cat >expect <<-EOF &&
+ fatal: attempt to reinitialize repository with different hash
+ EOF
+ test_must_fail git init --object-format="$other" repo 2>err &&
+ test_cmp expect err &&
+ rm -fr repo || return 1
+ done
+'
+
+test_expect_success 're-init does not modify hash from config' '
+ test_when_finished "rm -rf repo" &&
+ sane_unset GIT_DEFAULT_HASH &&
+ for algo in sha1 sha256
+ do
+ if test "$algo" = sha1
+ then
+ other=sha256
+ else
+ other=sha1
+ fi &&
+ git init --object-format="$algo" repo &&
+ test_config_global init.defaultObjectFormat "$other" &&
+ git init repo &&
+ echo "$algo" >expect &&
+ git -C repo rev-parse --show-object-format >actual &&
+ test_cmp expect actual &&
+ rm -fr repo || return 1
+ done
+'
+
test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
test_when_finished "rm -rf explicit-v0" &&
git init --object-format=sha256 explicit-v0 &&
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH v3] setup: fix hash algorithm detection
2026-11-19 1:06 ` [PATCH v3] " brian m. carlson
@ 2026-11-19 9:52 ` Patrick Steinhardt
2026-11-19 16:26 ` Junio C Hamano
2026-11-19 16:38 ` Junio C Hamano
2 siblings, 0 replies; 16+ messages in thread
From: Patrick Steinhardt @ 2026-11-19 9:52 UTC (permalink / raw)
To: brian m. carlson; +Cc: git, Junio C Hamano, Adam Dinwoodie
On Thu, Nov 19, 2026 at 01:06:12AM +0000, brian m. carlson wrote:
> Changes since v2:
> * Remove comment.
> * Use `$GIT_TEST_BUILTIN_HASH`.
Thanks, I'm happy with this version.
Patrick
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3] setup: fix hash algorithm detection
2026-11-19 1:06 ` [PATCH v3] " brian m. carlson
2026-11-19 9:52 ` Patrick Steinhardt
@ 2026-11-19 16:26 ` Junio C Hamano
2026-11-19 16:38 ` Junio C Hamano
2 siblings, 0 replies; 16+ messages in thread
From: Junio C Hamano @ 2026-11-19 16:26 UTC (permalink / raw)
To: brian m. carlson; +Cc: git, Patrick Steinhardt, Adam Dinwoodie
"brian m. carlson" <sandals@crustytoothpaste.net> writes:
> diff --git a/t/t0001-init.sh b/t/t0001-init.sh
> index 9e57a06cea..d89f60bab9 100755
> --- a/t/t0001-init.sh
> +++ b/t/t0001-init.sh
> @@ -516,6 +516,17 @@ test_expect_success 're-init from a linked worktree' '
> )
> '
>
> +test_expect_success 'git init uses default object format' '
> + test_when_finished "rm -rf repo" &&
> + (
> + sane_unset GIT_DEFAULT_HASH &&
> + git init repo &&
In this subshell, we discard GIT_DEFAULT_HASH that was set up by
t/test-lib.sh (either from the GIT_TEST_DEFAULT_HASH exported by
the CI framework or from the GIT_TEST_BUILTIN_HASH to match the
running binary). "git init repo" will create a repository using
the binary built-in default.
> + echo "$GIT_TEST_BUILTIN_HASH" >expect &&
> + git -C repo rev-parse --show-object-format >actual &&
And we have to see the same TEST_BUILTIN_HASH as the result.
> + test_cmp expect actual
> + )
> +'
Very good.
> +test_expect_success 'init refuses to reinitialize with different hash' '
> + test_when_finished "rm -rf repo" &&
> + sane_unset GIT_DEFAULT_HASH &&
Here, we permanently discard GIT_DEFAULT_HASH from t/test-lib.sh
without saving what it was. Would it cause issues to later tests
that we do not see in post context of this patch?
> + for algo in sha1 sha256
> + do
> + if test "$algo" = sha1
> + then
> + other=sha256
> + else
> + other=sha1
> + fi &&
> + git init --object-format="$algo" repo &&
We create in one format and then ...
> + cat >expect <<-EOF &&
> + fatal: attempt to reinitialize repository with different hash
> + EOF
> + test_must_fail git init --object-format="$other" repo 2>err &&
... try reinitializing to the other format. Expected is a failure
with a message.
> + test_cmp expect err &&
> + rm -fr repo || return 1
> + done
> +'
OK.
> +test_expect_success 're-init does not modify hash from config' '
> + test_when_finished "rm -rf repo" &&
> + sane_unset GIT_DEFAULT_HASH &&
Here, we discard GIT_DEFAULT_HASH again, correctly assumig that the
previous one can be skipped.
> + for algo in sha1 sha256
> + do
> + if test "$algo" = sha1
> + then
> + other=sha256
> + else
> + other=sha1
> + fi &&
> + git init --object-format="$algo" repo &&
Again, we create in one format and then ...
> + test_config_global init.defaultObjectFormat "$other" &&
> + git init repo &&
> + echo "$algo" >expect &&
... we do not want the configuration to cause reinitialization to
touch the format.
> + git -C repo rev-parse --show-object-format >actual &&
> + test_cmp expect actual &&
> + rm -fr repo || return 1
> + done
> +'
Looks good.
> test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
> test_when_finished "rm -rf explicit-v0" &&
> git init --object-format=sha256 explicit-v0 &&
My earlier question was about this one. A format is explicitly
given to this one, so the environment variables would not affect
this particular one, but if we have "git init" in later tests that
used to rely on the GIT_DEFAULT_HASH setting left by t/test-lib.sh
may we get in a trobule?
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3] setup: fix hash algorithm detection
2026-11-19 1:06 ` [PATCH v3] " brian m. carlson
2026-11-19 9:52 ` Patrick Steinhardt
2026-11-19 16:26 ` Junio C Hamano
@ 2026-11-19 16:38 ` Junio C Hamano
2026-11-19 22:20 ` brian m. carlson
2 siblings, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2026-11-19 16:38 UTC (permalink / raw)
To: brian m. carlson; +Cc: git, Patrick Steinhardt, Adam Dinwoodie
"brian m. carlson" <sandals@crustytoothpaste.net> writes:
> Our current hash algorithm selection code has a few different problems.
> First, we do not actually default to GIT_HASH_DEFAULT. Second, if we
> have an algorithm chosen by init.defaultObjectFormat and we re-init a
> repository, we will change the hash algorithm, which is very much not
> desired.
Can we end the "first" sentence with a reason? We do not actually
default to GIT_HASH_DEFAULT because we initialize a variable to
SHA1_LEGACY and we do not consider it as "nowhere the user specifies
which algorithm they want", or something?
> Let's restructure this code slightly. First, if an algorithm was
> selected on the command line, assume that's what the user wanted.
> Otherwise, the other options should only be chosen if our repository
> doesn't exist (that is, repo_fmt->version is -1) or it's specifically
> detected as having no hash algorithm. In that case, first use the
> environment, then the config, and finally, the default algorithm.
In the current code, does "it's specifically having no hash
algorithm" ever happen? Or is it our wish in near future to have
such a state to allow "the first use determines the hash algorithm
used" mode?
> Then, if the algorithm does not match the repository, complain loudly
> and refuse to reinitialize the repository with a different hash
> algorithm.
>
> With this change, we will choose SHA-256 in breaking changes mode and in
> Git 3.0, which is what we want. Add several tests to cover this case
> and several others discovered along the way. Remove the now-inaccurate
> comment.
Is the comment "now inaccurate"?
> - /*
> - * If we already have an initialized repo, don't allow the user to
> - * specify a different algorithm, as that could cause corruption.
> - * Otherwise, if the user has specified one on the command line, use it.
> - */
I think the first two lines is still relevant and serves as a
caution to whoever will be mucking with this function in the future.
The last sentence is not quite right in that following the user's
wish is not limited to the command line instruction (if they tell us
via an environment variable, we would honor it).
Oh, it just occured to me while I was writing the above
parenthesized comment. If I have ~/.gitconfig that was modified via
"git config set --global init.defaultObjectFormat sha256", would
that count as "the user has specified"?
Thanks.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3] setup: fix hash algorithm detection
2026-11-19 16:38 ` Junio C Hamano
@ 2026-11-19 22:20 ` brian m. carlson
0 siblings, 0 replies; 16+ messages in thread
From: brian m. carlson @ 2026-11-19 22:20 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git, Patrick Steinhardt, Adam Dinwoodie
[-- Attachment #1: Type: text/plain, Size: 2808 bytes --]
On 2026-11-19 at 16:38:13, Junio C Hamano wrote:
> "brian m. carlson" <sandals@crustytoothpaste.net> writes:
>
> > Our current hash algorithm selection code has a few different problems.
> > First, we do not actually default to GIT_HASH_DEFAULT. Second, if we
> > have an algorithm chosen by init.defaultObjectFormat and we re-init a
> > repository, we will change the hash algorithm, which is very much not
> > desired.
>
> Can we end the "first" sentence with a reason? We do not actually
> default to GIT_HASH_DEFAULT because we initialize a variable to
> SHA1_LEGACY and we do not consider it as "nowhere the user specifies
> which algorithm they want", or something?
I can expand on that.
> > Let's restructure this code slightly. First, if an algorithm was
> > selected on the command line, assume that's what the user wanted.
> > Otherwise, the other options should only be chosen if our repository
> > doesn't exist (that is, repo_fmt->version is -1) or it's specifically
> > detected as having no hash algorithm. In that case, first use the
> > environment, then the config, and finally, the default algorithm.
>
> In the current code, does "it's specifically having no hash
> algorithm" ever happen? Or is it our wish in near future to have
> such a state to allow "the first use determines the hash algorithm
> used" mode?
It should not as far as I can tell, but we have a bunch of different
cases, including the clone case, and I'm not 100% sure if that actually
ever happens.
> > - /*
> > - * If we already have an initialized repo, don't allow the user to
> > - * specify a different algorithm, as that could cause corruption.
> > - * Otherwise, if the user has specified one on the command line, use it.
> > - */
>
> I think the first two lines is still relevant and serves as a
> caution to whoever will be mucking with this function in the future.
> The last sentence is not quite right in that following the user's
> wish is not limited to the command line instruction (if they tell us
> via an environment variable, we would honor it).
>
> Oh, it just occured to me while I was writing the above
> parenthesized comment. If I have ~/.gitconfig that was modified via
> "git config set --global init.defaultObjectFormat sha256", would
> that count as "the user has specified"?
I'll rewrite the comment. The "user has specified one on the command
line" means with `--object-format`, but the user could specify one with
`init.defaultObjectFormat`. The difference is we scream if the former
mismatches with the repository, whereas if the repository is already
initialized, we ignore `GIT_DEFAULT_HASH` and
`init.defaultObjectFormat`.
--
brian m. carlson (they/them)
Toronto, Ontario, CA
Speaking for myself only
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 325 bytes --]
^ permalink raw reply [flat|nested] 16+ messages in thread