{"thread":{"id":"66793","subject":"Default object format for new repositories in Git v3","startedAt":"2026-11-06T16:50:03Z","lastAt":"2026-11-19T23:06:14Z","messageCount":17,"participants":["Adam Dinwoodie","brian m. carlson","Junio C Hamano","Patrick Steinhardt"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"557713","messageId":"CA+kUOampvjU3=VK3VGhYw2L5-E+LBsk=Q0UrAxhhR7hC5SUdJg@mail.gmail.com","threadId":"66793","inReplyTo":null,"subject":"Default object format for new repositories in Git v3","fromName":"Adam Dinwoodie","fromEmail":"adam@dinwoodie.org","sentAt":"2026-11-06T16:50:03Z","receivedAt":"2026-11-06T16:50:03Z","isPatch":false,"sender":{"key":"adam@dinwoodie.org","avatar":"https://avatars.githubusercontent.com/u/1397507?v=4"},"body":"Hi folks,\n\nAs part of preparing for Git v3, in particular for Nixpkgs, I've been\nrunning a bunch of tests using Git built with\n`WITH_BREAKING_CHANGES=1`. Somewhat to my surprise, `git init` seems\nto still be creating new repositories using sha1 rather than sha256.\nIs this expected?\n\nFor reference, I've attached the test script I've produced as a repro\nthat can be run from the root of a Git checkout, although it does\nrequire Nix. I've confirmed the behaviour at v2.56.0, next\n(972d6e3ab651d838b30a8d5d70d8ef6ced05a986) and main\n(0ffa3e35c7c82ab8d0de226149fb03b3f7674d05).\n\nFor a simpler repro that doesn't require Nix, build Git with\n`WITH_BREAKING_CHANGES=1`, then run:\n\n```\ngit init tmpdir\ngit -C tmpdir rev-parse --show-object-format\n```\n\nThis will print \"sha1\", when I'd expect \"sha256\".\n\n\n#!/usr/bin/env nix\n#!nix shell\n#!nix --override-flake nixpkgs https://releases.nixos.org/nixos/unstable/nixos-26.11pre1105037.1c89083e451e/nixexprs.tar.zst\n#!nix --ignore-env\n#!nix nixpkgs#bash nixpkgs#coreutils nixpkgs#nix nixpkgs#findutils nixpkgs#gnused nixpkgs#autoconf nixpkgs#gnumake nixpkgs#gcc nixpkgs#cargo\n#!nix --command bash\n\nset -euo pipefail\n\ntmpdir=\"$(mktemp --dir)\"\ntrap 'rm -rf -- \"$tmpdir\"' EXIT\n\n# Get the paths we need for zlib-ng\nexport NIX_PATH=nixpkgs=https://releases.nixos.org/nixos/unstable/nixos-26.11pre1105037.1c89083e451e/nixexprs.tar.zst\nnix-build '<nixpkgs>' -A zlib-ng -A zlib-ng.dev -o \"$tmpdir\"/zlib-ng\nzlib_lib=\"$(realpath \"$tmpdir\"/zlib-ng)\"\nzlib_dev=\"$(realpath \"$tmpdir\"/zlib-ng-dev)\"\n\n# Work out how much to parallelise the builds\njobs=\"$(nproc)\"\n\n# Make sure the build process can find zlib-ng\nexport CPPFLAGS=\"-isystem $zlib_dev/include\" LDFLAGS=\"-L$zlib_lib/lib -Wl,-rpath,$zlib_lib\"\n\n# Set options: disable features we don't need for speed of building, use\n# zlib-ng, enable breaking changes.\nexport ZLIB_NG=1 WITH_BREAKING_CHANGES=1 NO_TCLTK=1 NO_GETTEXT=1\n\n# Build git\nmake -j\"$jobs\" configure\n./configure\nmake -j\"$jobs\"\n\n# Make sure Git config doesn't have anything unexpected\nexport GIT_CONFIG_GLOBAL=/dev/null\nexport GIT_CONFIG_SYSTEM=/dev/null\nexport GIT_CONFIG=/dev/null\nexport GIT_CONFIG_COUNT=2\nexport GIT_CONFIG_KEY_0=user.name GIT_CONFIG_VALUE_0=Adam\nexport GIT_CONFIG_KEY_1=user.email GIT_CONFIG_VALUE_1=adam@localhost\nexport GIT_PAGER=cat\n\n# Make sure calling `git` gets the right executable\nexport PATH=\"$PWD\":\"$PATH\"\n\n# Set up a simple repository with a single commit\ngit init \"$tmpdir\"/repo\ncd \"$tmpdir\"/repo\ntouch test-file\ngit add test-file\ngit commit -m 'demo commit'\n\n# Show everything's sha1 rather than sha256\ngit log --format=raw\ngit rev-parse --show-object-format\n"},{"id":"557736","messageId":"au48oHXom4FGzwb9@fruit.crustytoothpaste.net","threadId":"66793","inReplyTo":"CA+kUOampvjU3=VK3VGhYw2L5-E+LBsk=Q0UrAxhhR7hC5SUdJg@mail.gmail.com","subject":"Re: Default object format for new repositories in Git v3","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2026-11-06T20:46:24Z","receivedAt":"2026-11-06T20:46:24Z","isPatch":false,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2026-11-06 at 16:50:03, Adam Dinwoodie wrote:\n> Hi folks,\n> \n> As part of preparing for Git v3, in particular for Nixpkgs, I've been\n> running a bunch of tests using Git built with\n> `WITH_BREAKING_CHANGES=1`. Somewhat to my surprise, `git init` seems\n> to still be creating new repositories using sha1 rather than sha256.\n> Is this expected?\n> \n> For reference, I've attached the test script I've produced as a repro\n> that can be run from the root of a Git checkout, although it does\n> require Nix. I've confirmed the behaviour at v2.56.0, next\n> (972d6e3ab651d838b30a8d5d70d8ef6ced05a986) and main\n> (0ffa3e35c7c82ab8d0de226149fb03b3f7674d05).\n> \n> For a simpler repro that doesn't require Nix, build Git with\n> `WITH_BREAKING_CHANGES=1`, then run:\n> \n> ```\n> git init tmpdir\n> git -C tmpdir rev-parse --show-object-format\n> ```\n> \n> This will print \"sha1\", when I'd expect \"sha256\".\n\nThis does appear to be broken.  I have a patch that should fix it that's\nrunning through CI right now.  I'll send it once it's green, either\nlater tonight or tomorrow.\n-- \nbrian m. carlson (they/them)\nToronto, Ontario, CA\nSpeaking for myself only\n"},{"id":"557824","messageId":"20261108011633.1772000-1-sandals@crustytoothpaste.net","threadId":"66793","inReplyTo":"CA+kUOampvjU3=VK3VGhYw2L5-E+LBsk=Q0UrAxhhR7hC5SUdJg@mail.gmail.com","subject":"[PATCH] setup: default hash algo to GIT_HASH_DEFAULT","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2026-11-08T01:16:33Z","receivedAt":"2026-11-08T01:16:33Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"When we have no other hash algorithm to choose, we set no hash\nalgorithm, which ends up with us defaulting to SHA-1.  Instead, default\nto GIT_HASH_DEFAULT.  This means that we will choose SHA-256 in breaking\nchanges mode and in Git 3.0, which is what we want.\n\nReported-by: Adam Dinwoodie <adam@dinwoodie.org>\nSigned-off-by: brian m. carlson <sandals@crustytoothpaste.net>\n---\n setup.c         |  2 ++\n t/t0001-init.sh | 16 ++++++++++++++++\n 2 files changed, 18 insertions(+)\n\ndiff --git a/setup.c b/setup.c\nindex 61bcdafe82..4a0dfc8908 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -2830,6 +2830,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n \t\t\trepo_fmt->hash_algo = env_algo;\n \t} else if (cfg.hash != GIT_HASH_UNKNOWN) {\n \t\trepo_fmt->hash_algo = cfg.hash;\n+\t} else {\n+\t\trepo_fmt->hash_algo = GIT_HASH_DEFAULT;\n \t}\n \n \t/*\ndiff --git a/t/t0001-init.sh b/t/t0001-init.sh\nindex f1d227f271..98c7562f32 100755\n--- a/t/t0001-init.sh\n+++ b/t/t0001-init.sh\n@@ -516,6 +516,22 @@ test_expect_success 're-init from a linked worktree' '\n \t)\n '\n \n+test_expect_success 'git init uses default object format' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\t(\n+\t\tsane_unset GIT_DEFAULT_HASH &&\n+\t\tgit init repo &&\n+\t\tif test_have_prereq WITH_BREAKING_CHANGES\n+\t\tthen\n+\t\t\techo sha256\n+\t\telse\n+\t\t\techo sha1\n+\t\tfi >expect &&\n+\t\tgit -C repo rev-parse --show-object-format >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success 'init honors GIT_DEFAULT_HASH' '\n \ttest_when_finished \"rm -rf sha1 sha256\" &&\n \tGIT_DEFAULT_HASH=sha1 git init sha1 &&\n\n"},{"id":"557831","messageId":"xmqqtsls53xr.fsf@gitster.g","threadId":"66793","inReplyTo":"20261108011633.1772000-1-sandals@crustytoothpaste.net","subject":"Re: [PATCH] setup: default hash algo to GIT_HASH_DEFAULT","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-08T04:42:56Z","receivedAt":"2026-11-08T04:42:56Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"brian m. carlson\" <sandals@crustytoothpaste.net> writes:\n\n> When we have no other hash algorithm to choose, we set no hash\n> algorithm, which ends up with us defaulting to SHA-1.  Instead, default\n> to GIT_HASH_DEFAULT.  This means that we will choose SHA-256 in breaking\n> changes mode and in Git 3.0, which is what we want.\n>\n> Reported-by: Adam Dinwoodie <adam@dinwoodie.org>\n> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>\n> ---\n>  setup.c         |  2 ++\n>  t/t0001-init.sh | 16 ++++++++++++++++\n>  2 files changed, 18 insertions(+)\n\nWith Git 3.0, we promised that SHA-1/SHA-256 interop will not have\nto be ready, and that SHA-256 would become the default.  And this\npatch would be a good thing to have for the second promise.\n\nBut didn't we also discuss \"empty/initial repository whose hash\nalgorithm is undecided can take a push of either SHA-1 or SHA-256\npayload and that first push can decide and freeze the hash algorithm\nused in the repository\" or something along that line?\n\nI am wondering if we want to reserve 'HASH_UNKNOWN' for such a \"was\njust created, not quite finished initializing yet\" state.\n\nThe patch looks good for the goal stated in the log message.\n\nThanks.\n\n\n\n\n> diff --git a/setup.c b/setup.c\n> index 61bcdafe82..4a0dfc8908 100644\n> --- a/setup.c\n> +++ b/setup.c\n> @@ -2830,6 +2830,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n>  \t\t\trepo_fmt->hash_algo = env_algo;\n>  \t} else if (cfg.hash != GIT_HASH_UNKNOWN) {\n>  \t\trepo_fmt->hash_algo = cfg.hash;\n> +\t} else {\n> +\t\trepo_fmt->hash_algo = GIT_HASH_DEFAULT;\n>  \t}\n>  \n>  \t/*\n> diff --git a/t/t0001-init.sh b/t/t0001-init.sh\n> index f1d227f271..98c7562f32 100755\n> --- a/t/t0001-init.sh\n> +++ b/t/t0001-init.sh\n> @@ -516,6 +516,22 @@ test_expect_success 're-init from a linked worktree' '\n>  \t)\n>  '\n>  \n> +test_expect_success 'git init uses default object format' '\n> +\ttest_when_finished \"rm -rf repo\" &&\n> +\t(\n> +\t\tsane_unset GIT_DEFAULT_HASH &&\n> +\t\tgit init repo &&\n> +\t\tif test_have_prereq WITH_BREAKING_CHANGES\n> +\t\tthen\n> +\t\t\techo sha256\n> +\t\telse\n> +\t\t\techo sha1\n> +\t\tfi >expect &&\n> +\t\tgit -C repo rev-parse --show-object-format >actual &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n>  test_expect_success 'init honors GIT_DEFAULT_HASH' '\n>  \ttest_when_finished \"rm -rf sha1 sha256\" &&\n>  \tGIT_DEFAULT_HASH=sha1 git init sha1 &&\n\n"},{"id":"557843","messageId":"avCJbJE7mx_Z2zLY@fruit.crustytoothpaste.net","threadId":"66793","inReplyTo":"xmqqtsls53xr.fsf@gitster.g","subject":"Re: [PATCH] setup: default hash algo to GIT_HASH_DEFAULT","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2026-11-08T14:38:37Z","receivedAt":"2026-11-08T14:38:37Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2026-11-08 at 04:42:56, Junio C Hamano wrote:\n> \"brian m. carlson\" <sandals@crustytoothpaste.net> writes:\n> \n> > When we have no other hash algorithm to choose, we set no hash\n> > algorithm, which ends up with us defaulting to SHA-1.  Instead, default\n> > to GIT_HASH_DEFAULT.  This means that we will choose SHA-256 in breaking\n> > changes mode and in Git 3.0, which is what we want.\n> >\n> > Reported-by: Adam Dinwoodie <adam@dinwoodie.org>\n> > Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>\n> > ---\n> >  setup.c         |  2 ++\n> >  t/t0001-init.sh | 16 ++++++++++++++++\n> >  2 files changed, 18 insertions(+)\n> \n> With Git 3.0, we promised that SHA-1/SHA-256 interop will not have\n> to be ready, and that SHA-256 would become the default.  And this\n> patch would be a good thing to have for the second promise.\n> \n> But didn't we also discuss \"empty/initial repository whose hash\n> algorithm is undecided can take a push of either SHA-1 or SHA-256\n> payload and that first push can decide and freeze the hash algorithm\n> used in the repository\" or something along that line?\n\nWe discussed that as a possibility, but it requires protocol changes and\nnobody has stepped up to implement that.  I think Peff was thinking\nabout maybe implementing that, but I don't think anyone came to a\ndecision.\n\n> I am wondering if we want to reserve 'HASH_UNKNOWN' for such a \"was\n> just created, not quite finished initializing yet\" state.\n\nI think we concluded that we'd need to initialize a repository to _some_\nhash algorithm even if we had the limbo state because otherwise people\ncould `git init` and then work locally and we need to have some existing\nhash algorithm for that case.  That would be true even with bare\nrepositories.\n\nSo I think the limbo functionality, if we implemented it, would be a\nseparate option (maybe `extensions.potentialObjectFormat`?) and this\npatch should go in either way.\n-- \nbrian m. carlson (they/them)\nToronto, Ontario, CA\nSpeaking for myself only\n"},{"id":"557847","messageId":"xmqqik274201.fsf@gitster.g","threadId":"66793","inReplyTo":"20261108011633.1772000-1-sandals@crustytoothpaste.net","subject":"Re: [PATCH] setup: default hash algo to GIT_HASH_DEFAULT","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-08T18:22:22Z","receivedAt":"2026-11-08T18:22:22Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"brian m. carlson\" <sandals@crustytoothpaste.net> writes:\n\n> When we have no other hash algorithm to choose, we set no hash\n> algorithm, which ends up with us defaulting to SHA-1.  Instead, default\n> to GIT_HASH_DEFAULT.  This means that we will choose SHA-256 in breaking\n> changes mode and in Git 3.0, which is what we want.\n>\n> Reported-by: Adam Dinwoodie <adam@dinwoodie.org>\n> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>\n> ---\n>  setup.c         |  2 ++\n>  t/t0001-init.sh | 16 ++++++++++++++++\n>  2 files changed, 18 insertions(+)\n>\n> diff --git a/setup.c b/setup.c\n> index 61bcdafe82..4a0dfc8908 100644\n> --- a/setup.c\n> +++ b/setup.c\n> @@ -2830,6 +2830,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n>  \t\t\trepo_fmt->hash_algo = env_algo;\n>  \t} else if (cfg.hash != GIT_HASH_UNKNOWN) {\n>  \t\trepo_fmt->hash_algo = cfg.hash;\n> +\t} else {\n> +\t\trepo_fmt->hash_algo = GIT_HASH_DEFAULT;\n>  \t}\n\nHmph, maybe I am not reading the code flow correctly, but does this\nnot break running \"git init\" to reinitialize an already initialized\nrepository?  I mimicked the new test in your patch and wrote the\nfollowing just immediately after it, which\n\n * figures out our hash and the other hash\n * create a new repository with the \"other\" hash\n * run \"git init\" without specifying any hash\n\nIt is debatable what should happen when you run \"git init\" from Git\n3.0 without any specialization in an existing SHA-1 repository.\n\nPossibilities include:\n\n 1. turn an existing SHA-1 repository into SHA-256 including format\n    conversion.\n\n 2. notice that the repository is already set up to use the SHA-1\n    format, and that the user did not explicitly say what format to\n    use, and behave as if --object-format=sha1 were given with a\n    notice or warning.\n\n 3. notice that the repository is already set up to use the SHA-1\n    format, but our default format is SHA-256.  Because the user did\n    not countermand via an explicit --object-format=sha1 on the\n    command line, we should fail loudly, \"cannot convert this SHA-1\n    repository to SHA-256\".\n\nI think #1 is impractical, #2 is probably the best, and #3 would\nalso be reasonable.\n\nBut the behaviour I am seeing with\n\n    $ make WITH_BREAKING_CHANGES=YesPlease\n    $ cd t && WITH_BREAKING_CHANGES=YesPlease sh t0001-init.sh -i -v\n\nwith the additional patch is different from any of the three (well,\ntechnically, because the repository is empty, we _could_ argue that\nwe did #1, but ...).\n\nI wonder what the \"git init\" command does wrt the reference storage\nformat (Patrick CC'ed for this)?  I am sure we do not convert from\nreffiles to reftable on the fly at \"git init\" time.\n\n\ndiff --git i/t/t0001-init.sh w/t/t0001-init.sh\nindex bc30cf853b..1d48a712ae 100755\n--- i/t/t0001-init.sh\n+++ w/t/t0001-init.sh\n@@ -532,6 +532,31 @@ test_expect_success 'git init uses default object format' '\n \t)\n '\n \n+test_expect_success 'git (re)init does not clobber object format' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\t(\n+\t\tsane_unset GIT_DEFAULT_HASH &&\n+\t\tif test_have_prereq WITH_BREAKING_CHANGES\n+\t\tthen\n+\t\t\tours=sha256 opposite=sha1\n+\t\telse\n+\t\t\tours=sha1 opposite=sha256\n+\t\tfi &&\n+\n+\t\t# initialize to non-default format\n+\t\tgit init --object-format=$opposite repo &&\n+\t\tgit -C repo rev-parse --show-object-format >actual &&\n+\t\techo \"$opposite\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\t# reinitialize without configuration\n+\t\tgit init repo &&\n+\t\tgit -C repo rev-parse --show-object-format >actual &&\n+\t\techo \"$opposite\" >expect &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success 'init honors GIT_DEFAULT_HASH' '\n \ttest_when_finished \"rm -rf sha1 sha256\" &&\n \tGIT_DEFAULT_HASH=sha1 git init sha1 &&\n\n"},{"id":"557869","messageId":"xmqqzevi218o.fsf@gitster.g","threadId":"66793","inReplyTo":"xmqqik274201.fsf@gitster.g","subject":"Re: [PATCH] setup: default hash algo to GIT_HASH_DEFAULT","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-09T02:21:43Z","receivedAt":"2026-11-09T02:21:43Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> I wonder what the \"git init\" command does wrt the reference storage\n> format (Patrick CC'ed for this)?  I am sure we do not convert from\n> reffiles to reftable on the fly at \"git init\" time.\n\nI looked at setup.c:repository_format_configure() and found why\nyour patch regresses, as well as a latent bug in the existing code.\n\nFirst, REPOSITORY_FORMAT_INIT sets .hash_algo = GIT_HASH_DEFAULT,\nbut read_repository_format() forces it to GIT_HASH_SHA1_LEGACY\nwhen version == -1 (no config yet), which is why new repositories\nkept getting SHA-1.\n\nSecond, unlike the ref-storage block right below it, which checks\n\"else if (repo_fmt->version >= 0)\" once between the explicit\noverrides and all default sources, the hash-algo block guards only\nthe GIT_DEFAULT_HASH branch (repo_fmt->version < 0), leaving \"else\nif (cfg.hash != GIT_HASH_UNKNOWN)\" unguarded.\n\nSo init.defaultObjectFormat in ~/.gitconfig already corrupts an\nexisting repository on re-init, and an unguarded \"else\" for\nGIT_HASH_DEFAULT falls into the same trap even without config.\n\nInstead of starting the if/else if cascade like this:\n\n\tif (repo_fmt->version >= 0 &&\n\t    hash != GIT_HASH_UNKNOWN &&\n\t    hash != repo_fmt->hash_algo)\n\t\tdie(_(\"attempt to reinitialize repository with different hash\"));\n\nwe should have started it like so:\n\n\tif (repo_fmt->version >= 0) {\n\t\tif (hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)\n\t\t\tdie(_(\"attempt to reinitialize repository with different hash\"));\n\t\t/* otherwise happy */\n\t} else if ...\n\nso that all the \"set a new value from different sources like command\nline, environment, ~/.gitconfig, or the built-in default\" would\nhappen only when (repo_fmt->version < 0), probably?\n\n\n"},{"id":"557906","messageId":"avGYZIFAmeSEP6ZS@pks.im","threadId":"66793","inReplyTo":"xmqqzevi218o.fsf@gitster.g","subject":"Re: [PATCH] setup: default hash algo to GIT_HASH_DEFAULT","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-11-09T09:54:44Z","receivedAt":"2026-11-09T09:54:44Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Sun, Nov 08, 2026 at 06:21:43PM -0800, Junio C Hamano wrote:\n> Junio C Hamano <gitster@pobox.com> writes:\n> \n> > I wonder what the \"git init\" command does wrt the reference storage\n> > format (Patrick CC'ed for this)?  I am sure we do not convert from\n> > reffiles to reftable on the fly at \"git init\" time.\n\nYeah, we don't, and I don't think that would be reasonable. I would very\nmuch be surprised if git-init(1) migrated a repository's hash format,\nref storage format or object storage format. Those are long-running\noperations and in the past, git-init(1) used to be somewhat idempotent\non an already-initialized repository. So I'd much rather have us migrate\nexplicitly, like we do with `git refs migrate`.\n\n> I looked at setup.c:repository_format_configure() and found why\n> your patch regresses, as well as a latent bug in the existing code.\n> \n> First, REPOSITORY_FORMAT_INIT sets .hash_algo = GIT_HASH_DEFAULT,\n> but read_repository_format() forces it to GIT_HASH_SHA1_LEGACY\n> when version == -1 (no config yet), which is why new repositories\n> kept getting SHA-1.\n> \n> Second, unlike the ref-storage block right below it, which checks\n> \"else if (repo_fmt->version >= 0)\" once between the explicit\n> overrides and all default sources, the hash-algo block guards only\n> the GIT_DEFAULT_HASH branch (repo_fmt->version < 0), leaving \"else\n> if (cfg.hash != GIT_HASH_UNKNOWN)\" unguarded.\n> \n> So init.defaultObjectFormat in ~/.gitconfig already corrupts an\n> existing repository on re-init, and an unguarded \"else\" for\n> GIT_HASH_DEFAULT falls into the same trap even without config.\n> \n> Instead of starting the if/else if cascade like this:\n> \n> \tif (repo_fmt->version >= 0 &&\n> \t    hash != GIT_HASH_UNKNOWN &&\n> \t    hash != repo_fmt->hash_algo)\n> \t\tdie(_(\"attempt to reinitialize repository with different hash\"));\n> \n> we should have started it like so:\n> \n> \tif (repo_fmt->version >= 0) {\n> \t\tif (hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)\n> \t\t\tdie(_(\"attempt to reinitialize repository with different hash\"));\n> \t\t/* otherwise happy */\n> \t} else if ...\n> \n> so that all the \"set a new value from different sources like command\n> line, environment, ~/.gitconfig, or the built-in default\" would\n> happen only when (repo_fmt->version < 0), probably?\n\nYeah, these blocks are overall quite hard to read. That's also the\nreason why I re-did these checks for both the ref storage format and\nthen also used the new style for the object storage format. So my take\nis that we should adapt the object format in a similar fashion so that\nall of them use the same style, where we first detect the desired format\nand then check whether we're trying to re-initialize with a different\nobject format towards the end of this function.\n\nSo something like this:\n\ndiff --git a/setup.c b/setup.c\nindex 61bcdafe82a..bf75abafeeb 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -2816,20 +2816,19 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n \t * specify a different algorithm, as that could cause corruption.\n \t * Otherwise, if the user has specified one on the command line, use it.\n \t */\n-\tenv = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);\n-\tif (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)\n-\t\tdie(_(\"attempt to reinitialize repository with different hash\"));\n-\telse if (hash != GIT_HASH_UNKNOWN)\n-\t\trepo_fmt->hash_algo = hash;\n-\telse if (env) {\n+\tif (hash != GIT_HASH_UNKNOWN) {\n+\t\t/* nothing to do */\n+\t} else if ((env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT))) {\n \t\tint env_algo = hash_algo_by_name(env);\n \t\tif (env_algo == GIT_HASH_UNKNOWN)\n \t\t\tdie(_(\"unknown hash algorithm '%s'\"), env);\n \t\tif (repo_fmt->version < 0 ||\n \t\t    repo_fmt->hash_algo == GIT_HASH_UNKNOWN)\n \t\t\trepo_fmt->hash_algo = env_algo;\n-\t} else if (cfg.hash != GIT_HASH_UNKNOWN) {\n-\t\trepo_fmt->hash_algo = cfg.hash;\n+\t} else if (repo_fmt->version >= 0) {\n+\t\thash = repo_fmt->hash_algo;\n+\t} else {\n+\t\thash = GIT_HASH_DEFAULT;\n \t}\n \n \t/*\n@@ -2899,6 +2898,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n \t * current ref storage format does not change.\n \t */\n \tif (repo_fmt->version >= 0) {\n+\t\tif (hash != repo_fmt->hash_algo)\n+\t\t\tdie(_(\"attempt to reinitialize repository with different hash\"));\n \t\tif (ref_storage_format != repo_fmt->ref_storage_format)\n \t\t\tdie(_(\"attempt to reinitialize repository with different reference storage format\"));\n \t\tif ((ref_storage_payload || repo_fmt->ref_storage_payload) &&\n\nPatrick\n\n"},{"id":"558432","messageId":"20261113221753.3408862-1-sandals@crustytoothpaste.net","threadId":"66793","inReplyTo":"20261108011633.1772000-1-sandals@crustytoothpaste.net","subject":"[PATCH v2] setup: fix hash algorithm detection","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2026-11-13T22:17:53Z","receivedAt":"2026-11-13T22:17:53Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"Our current hash algorithm selection code has a few different problems.\nFirst, we do not actually default to GIT_HASH_DEFAULT.  Second, if we\nhave an algorithm chosen by init.defaultObjectFormat and we re-init a\nrepository, we will change the hash algorithm, which is very much not\ndesired.\n\nLet's restructure this code slightly.  First, if an algorithm was\nselected on the command line, assume that's what the user wanted.\nOtherwise, the other options should only be chosen if our repository\ndoesn't exist (that is, repo_fmt->version is -1) or it's specifically\ndetected as having no hash algorithm.  In that case, first use the\nenvironment, then the config, and finally, the default algorithm.\n\nThen, if the algorithm does not match the repository, complain loudly\nand refuse to reinitialize the repository with a different hash\nalgorithm.\n\nWith this change, we will choose SHA-256 in breaking changes mode and in\nGit 3.0, which is what we want.  Add several tests to cover this case\nand several others discovered along the way.\n\nReported-by: Adam Dinwoodie <adam@dinwoodie.org>\nSigned-off-by: brian m. carlson <sandals@crustytoothpaste.net>\n---\n setup.c         | 22 +++++++++++--------\n t/t0001-init.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 71 insertions(+), 9 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 61bcdafe82..14c6e1a67d 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -2817,19 +2817,20 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n \t * Otherwise, if the user has specified one on the command line, use it.\n \t */\n \tenv = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);\n-\tif (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)\n-\t\tdie(_(\"attempt to reinitialize repository with different hash\"));\n-\telse if (hash != GIT_HASH_UNKNOWN)\n-\t\trepo_fmt->hash_algo = hash;\n-\telse if (env) {\n+\tif (hash != GIT_HASH_UNKNOWN) {\n+\t\t/* nothing to do */\n+\t} else if (repo_fmt->version >= 0 &&\n+\t\t   repo_fmt->hash_algo != GIT_HASH_UNKNOWN) {\n+\t\thash = repo_fmt->hash_algo;\n+\t} else if (env) {\n \t\tint env_algo = hash_algo_by_name(env);\n \t\tif (env_algo == GIT_HASH_UNKNOWN)\n \t\t\tdie(_(\"unknown hash algorithm '%s'\"), env);\n-\t\tif (repo_fmt->version < 0 ||\n-\t\t    repo_fmt->hash_algo == GIT_HASH_UNKNOWN)\n-\t\t\trepo_fmt->hash_algo = env_algo;\n+\t\thash = env_algo;\n \t} else if (cfg.hash != GIT_HASH_UNKNOWN) {\n-\t\trepo_fmt->hash_algo = cfg.hash;\n+\t\thash = cfg.hash;\n+\t} else {\n+\t\thash = GIT_HASH_DEFAULT;\n \t}\n \n \t/*\n@@ -2899,6 +2900,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n \t * current ref storage format does not change.\n \t */\n \tif (repo_fmt->version >= 0) {\n+\t\tif (hash != repo_fmt->hash_algo)\n+\t\t\tdie(_(\"attempt to reinitialize repository with different hash\"));\n \t\tif (ref_storage_format != repo_fmt->ref_storage_format)\n \t\t\tdie(_(\"attempt to reinitialize repository with different reference storage format\"));\n \t\tif ((ref_storage_payload || repo_fmt->ref_storage_payload) &&\n@@ -2908,6 +2911,7 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n \t}\n \n \tfree(repo_fmt->ref_storage_payload);\n+\trepo_fmt->hash_algo = hash;\n \trepo_fmt->ref_storage_format = ref_storage_format;\n \trepo_fmt->ref_storage_payload = ref_storage_payload;\n }\ndiff --git a/t/t0001-init.sh b/t/t0001-init.sh\nindex f1d227f271..d8244a2dec 100755\n--- a/t/t0001-init.sh\n+++ b/t/t0001-init.sh\n@@ -516,6 +516,22 @@ test_expect_success 're-init from a linked worktree' '\n \t)\n '\n \n+test_expect_success 'git init uses default object format' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\t(\n+\t\tsane_unset GIT_DEFAULT_HASH &&\n+\t\tgit init repo &&\n+\t\tif test_have_prereq WITH_BREAKING_CHANGES\n+\t\tthen\n+\t\t\techo sha256\n+\t\telse\n+\t\t\techo sha1\n+\t\tfi >expect &&\n+\t\tgit -C repo rev-parse --show-object-format >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success 'init honors GIT_DEFAULT_HASH' '\n \ttest_when_finished \"rm -rf sha1 sha256\" &&\n \tGIT_DEFAULT_HASH=sha1 git init sha1 &&\n@@ -602,6 +618,48 @@ do\n \t'\n done\n \n+test_expect_success 'init refuses to reinitialize with different hash' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsane_unset GIT_DEFAULT_HASH &&\n+\tfor algo in sha1 sha256\n+\tdo\n+\t\tif test \"$algo\" = sha1\n+\t\tthen\n+\t\t\tother=sha256\n+\t\telse\n+\t\t\tother=sha1\n+\t\tfi &&\n+\t\tgit init --object-format=\"$algo\" repo &&\n+\t\tcat >expect <<-EOF &&\n+\t\tfatal: attempt to reinitialize repository with different hash\n+\t\tEOF\n+\t\ttest_must_fail git init --object-format=\"$other\" repo 2>err &&\n+\t\ttest_cmp expect err &&\n+\t\trm -fr repo || return 1\n+\tdone\n+'\n+\n+test_expect_success 're-init does not modify hash from config' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsane_unset GIT_DEFAULT_HASH &&\n+\tfor algo in sha1 sha256\n+\tdo\n+\t\tif test \"$algo\" = sha1\n+\t\tthen\n+\t\t\tother=sha256\n+\t\telse\n+\t\t\tother=sha1\n+\t\tfi &&\n+\t\tgit init --object-format=\"$algo\" repo &&\n+\t\ttest_config_global init.defaultObjectFormat \"$other\" &&\n+\t\tgit init repo &&\n+\t\techo \"$algo\" >expect &&\n+\t\tgit -C repo rev-parse --show-object-format >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\trm -fr repo || return 1\n+\tdone\n+'\n+\n test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '\n \ttest_when_finished \"rm -rf explicit-v0\" &&\n \tgit init --object-format=sha256 explicit-v0 &&\n\n"},{"id":"558438","messageId":"xmqqh5hk8dyv.fsf@gitster.g","threadId":"66793","inReplyTo":"20261113221753.3408862-1-sandals@crustytoothpaste.net","subject":"Re: [PATCH v2] setup: fix hash algorithm detection","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-14T00:16:08Z","receivedAt":"2026-11-14T00:16:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"brian m. carlson\" <sandals@crustytoothpaste.net> writes:\n\n> Our current hash algorithm selection code has a few different problems.\n> First, we do not actually default to GIT_HASH_DEFAULT.  Second, if we\n> have an algorithm chosen by init.defaultObjectFormat and we re-init a\n> repository, we will change the hash algorithm, which is very much not\n> desired.\n>\n> Let's restructure this code slightly.  First, if an algorithm was\n> selected on the command line, assume that's what the user wanted.\n> Otherwise, the other options should only be chosen if our repository\n> doesn't exist (that is, repo_fmt->version is -1) or it's specifically\n> detected as having no hash algorithm.  In that case, first use the\n> environment, then the config, and finally, the default algorithm.\n>\n> Then, if the algorithm does not match the repository, complain loudly\n> and refuse to reinitialize the repository with a different hash\n> algorithm.\n>\n> With this change, we will choose SHA-256 in breaking changes mode and in\n> Git 3.0, which is what we want.  Add several tests to cover this case\n> and several others discovered along the way.\n>\n> Reported-by: Adam Dinwoodie <adam@dinwoodie.org>\n> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>\n> ---\n>  setup.c         | 22 +++++++++++--------\n>  t/t0001-init.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++\n>  2 files changed, 71 insertions(+), 9 deletions(-)\n\nI was somehow expecting a patch that would apply cleanly both to the\nmaintenance track and to the 2.98 but that is totally unnecessary,\nas it does not make any sense to build and use the maintenance track\nwith the WITH_BREAKING_CHANGES option ;-) The point of the\nWITH_BREAKING_CHANGES option is to make sure we have a test vehicle\nto try out something that is close enough to \"Git 3.0\" long before\nwe release one.\n\nAnd the updated code structure looks much simpler and more\nstraightforward.\n\nWill replace.\n\nThanks.\n\n"},{"id":"558510","messageId":"avqhPfANDZqeWGmo@pks.im","threadId":"66793","inReplyTo":"20261113221753.3408862-1-sandals@crustytoothpaste.net","subject":"Re: [PATCH v2] setup: fix hash algorithm detection","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-11-16T06:22:53Z","receivedAt":"2026-11-16T06:22:53Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Fri, Nov 13, 2026 at 10:17:53PM +0000, brian m. carlson wrote:\n> diff --git a/setup.c b/setup.c\n> index 61bcdafe82..14c6e1a67d 100644\n> --- a/setup.c\n> +++ b/setup.c\n> @@ -2817,19 +2817,20 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n>  \t * Otherwise, if the user has specified one on the command line, use it.\n>  \t */\n\nThis comment is now a bit stale, as it talks about re-initializing the\nrepository with an incompatible hash algorithm. But that only gets\ndetected further down now. Do we maybe want to update it to have the\nsame bulleted list of precedence that we also have for the ref and\nobject storage formats?\n\nThe code changes look as expected to me and resemble what we have for\nthe two storage formats.\n\n> diff --git a/t/t0001-init.sh b/t/t0001-init.sh\n> index f1d227f271..d8244a2dec 100755\n> --- a/t/t0001-init.sh\n> +++ b/t/t0001-init.sh\n> @@ -516,6 +516,22 @@ test_expect_success 're-init from a linked worktree' '\n>  \t)\n>  '\n>  \n> +test_expect_success 'git init uses default object format' '\n> +\ttest_when_finished \"rm -rf repo\" &&\n> +\t(\n> +\t\tsane_unset GIT_DEFAULT_HASH &&\n> +\t\tgit init repo &&\n> +\t\tif test_have_prereq WITH_BREAKING_CHANGES\n> +\t\tthen\n> +\t\t\techo sha256\n> +\t\telse\n> +\t\t\techo sha1\n> +\t\tfi >expect &&\n\nI think we can just use GIT_TEST_BUILTIN_HASH for this, which detects\nthe hash via `git verson --build-options`.\n\nThanks!\n\nPatrick\n\n"},{"id":"558779","messageId":"20261119010612.687733-1-sandals@crustytoothpaste.net","threadId":"66793","inReplyTo":"20261108011633.1772000-1-sandals@crustytoothpaste.net","subject":"[PATCH v3] setup: fix hash algorithm detection","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2026-11-19T01:06:12Z","receivedAt":"2026-11-19T01:06:12Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"Our current hash algorithm selection code has a few different problems.\nFirst, we do not actually default to GIT_HASH_DEFAULT.  Second, if we\nhave an algorithm chosen by init.defaultObjectFormat and we re-init a\nrepository, we will change the hash algorithm, which is very much not\ndesired.\n\nLet's restructure this code slightly.  First, if an algorithm was\nselected on the command line, assume that's what the user wanted.\nOtherwise, the other options should only be chosen if our repository\ndoesn't exist (that is, repo_fmt->version is -1) or it's specifically\ndetected as having no hash algorithm.  In that case, first use the\nenvironment, then the config, and finally, the default algorithm.\n\nThen, if the algorithm does not match the repository, complain loudly\nand refuse to reinitialize the repository with a different hash\nalgorithm.\n\nWith this change, we will choose SHA-256 in breaking changes mode and in\nGit 3.0, which is what we want.  Add several tests to cover this case\nand several others discovered along the way.  Remove the now-inaccurate\ncomment.\n\nReported-by: Adam Dinwoodie <adam@dinwoodie.org>\nSigned-off-by: brian m. carlson <sandals@crustytoothpaste.net>\n---\nChanges since v2:\n* Remove comment.\n* Use `$GIT_TEST_BUILTIN_HASH`.\n\n setup.c         | 27 ++++++++++++-------------\n t/t0001-init.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 66 insertions(+), 14 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex cedc031c54..5d7e8d590e 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -2887,25 +2887,21 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n \n \tconfig_with_options(read_default_format_config, &cfg, NULL, NULL, &opts);\n \n-\t/*\n-\t * If we already have an initialized repo, don't allow the user to\n-\t * specify a different algorithm, as that could cause corruption.\n-\t * Otherwise, if the user has specified one on the command line, use it.\n-\t */\n \tenv = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);\n-\tif (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)\n-\t\tdie(_(\"attempt to reinitialize repository with different hash\"));\n-\telse if (hash != GIT_HASH_UNKNOWN)\n-\t\trepo_fmt->hash_algo = hash;\n-\telse if (env) {\n+\tif (hash != GIT_HASH_UNKNOWN) {\n+\t\t/* nothing to do */\n+\t} else if (repo_fmt->version >= 0 &&\n+\t\t   repo_fmt->hash_algo != GIT_HASH_UNKNOWN) {\n+\t\thash = repo_fmt->hash_algo;\n+\t} else if (env) {\n \t\tint env_algo = hash_algo_by_name(env);\n \t\tif (env_algo == GIT_HASH_UNKNOWN)\n \t\t\tdie(_(\"unknown hash algorithm '%s'\"), env);\n-\t\tif (repo_fmt->version < 0 ||\n-\t\t    repo_fmt->hash_algo == GIT_HASH_UNKNOWN)\n-\t\t\trepo_fmt->hash_algo = env_algo;\n+\t\thash = env_algo;\n \t} else if (cfg.hash != GIT_HASH_UNKNOWN) {\n-\t\trepo_fmt->hash_algo = cfg.hash;\n+\t\thash = cfg.hash;\n+\t} else {\n+\t\thash = GIT_HASH_DEFAULT;\n \t}\n \n \t/*\n@@ -3022,6 +3018,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n \t * current storage formats do not change.\n \t */\n \tif (repo_fmt->version >= 0) {\n+\t\tif (hash != repo_fmt->hash_algo)\n+\t\t\tdie(_(\"attempt to reinitialize repository with different hash\"));\n \t\tif (ref_storage_format != repo_fmt->ref_storage_format)\n \t\t\tdie(_(\"attempt to reinitialize repository with different reference storage format\"));\n \t\tif ((ref_storage_payload || repo_fmt->ref_storage_payload) &&\n@@ -3038,6 +3036,7 @@ static void repository_format_configure(struct repository_format *repo_fmt,\n \n \tfree(cfg.ref_storage_payload);\n \tfree(repo_fmt->ref_storage_payload);\n+\trepo_fmt->hash_algo = hash;\n \trepo_fmt->ref_storage_format = ref_storage_format;\n \trepo_fmt->ref_storage_payload = ref_storage_payload;\n \ndiff --git a/t/t0001-init.sh b/t/t0001-init.sh\nindex 9e57a06cea..d89f60bab9 100755\n--- a/t/t0001-init.sh\n+++ b/t/t0001-init.sh\n@@ -516,6 +516,17 @@ test_expect_success 're-init from a linked worktree' '\n \t)\n '\n \n+test_expect_success 'git init uses default object format' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\t(\n+\t\tsane_unset GIT_DEFAULT_HASH &&\n+\t\tgit init repo &&\n+\t\techo \"$GIT_TEST_BUILTIN_HASH\" >expect &&\n+\t\tgit -C repo rev-parse --show-object-format >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success 'init honors GIT_DEFAULT_HASH' '\n \ttest_when_finished \"rm -rf sha1 sha256\" &&\n \tGIT_DEFAULT_HASH=sha1 git init sha1 &&\n@@ -602,6 +613,48 @@ do\n \t'\n done\n \n+test_expect_success 'init refuses to reinitialize with different hash' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsane_unset GIT_DEFAULT_HASH &&\n+\tfor algo in sha1 sha256\n+\tdo\n+\t\tif test \"$algo\" = sha1\n+\t\tthen\n+\t\t\tother=sha256\n+\t\telse\n+\t\t\tother=sha1\n+\t\tfi &&\n+\t\tgit init --object-format=\"$algo\" repo &&\n+\t\tcat >expect <<-EOF &&\n+\t\tfatal: attempt to reinitialize repository with different hash\n+\t\tEOF\n+\t\ttest_must_fail git init --object-format=\"$other\" repo 2>err &&\n+\t\ttest_cmp expect err &&\n+\t\trm -fr repo || return 1\n+\tdone\n+'\n+\n+test_expect_success 're-init does not modify hash from config' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsane_unset GIT_DEFAULT_HASH &&\n+\tfor algo in sha1 sha256\n+\tdo\n+\t\tif test \"$algo\" = sha1\n+\t\tthen\n+\t\t\tother=sha256\n+\t\telse\n+\t\t\tother=sha1\n+\t\tfi &&\n+\t\tgit init --object-format=\"$algo\" repo &&\n+\t\ttest_config_global init.defaultObjectFormat \"$other\" &&\n+\t\tgit init repo &&\n+\t\techo \"$algo\" >expect &&\n+\t\tgit -C repo rev-parse --show-object-format >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\trm -fr repo || return 1\n+\tdone\n+'\n+\n test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '\n \ttest_when_finished \"rm -rf explicit-v0\" &&\n \tgit init --object-format=sha256 explicit-v0 &&\n\n"},{"id":"558808","messageId":"av7GuJC5gMVsTaQL@pks.im","threadId":"66793","inReplyTo":"20261119010612.687733-1-sandals@crustytoothpaste.net","subject":"Re: [PATCH v3] setup: fix hash algorithm detection","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-11-19T09:52:09Z","receivedAt":"2026-11-19T09:52:09Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Nov 19, 2026 at 01:06:12AM +0000, brian m. carlson wrote:\n> Changes since v2:\n> * Remove comment.\n> * Use `$GIT_TEST_BUILTIN_HASH`.\n\nThanks, I'm happy with this version.\n\nPatrick\n\n"},{"id":"558861","messageId":"xmqqse0wzt1t.fsf@gitster.g","threadId":"66793","inReplyTo":"20261119010612.687733-1-sandals@crustytoothpaste.net","subject":"Re: [PATCH v3] setup: fix hash algorithm detection","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-19T16:26:22Z","receivedAt":"2026-11-19T16:26:22Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"brian m. carlson\" <sandals@crustytoothpaste.net> writes:\n\n> diff --git a/t/t0001-init.sh b/t/t0001-init.sh\n> index 9e57a06cea..d89f60bab9 100755\n> --- a/t/t0001-init.sh\n> +++ b/t/t0001-init.sh\n> @@ -516,6 +516,17 @@ test_expect_success 're-init from a linked worktree' '\n>  \t)\n>  '\n>  \n> +test_expect_success 'git init uses default object format' '\n> +\ttest_when_finished \"rm -rf repo\" &&\n> +\t(\n> +\t\tsane_unset GIT_DEFAULT_HASH &&\n> +\t\tgit init repo &&\n\nIn this subshell, we discard GIT_DEFAULT_HASH that was set up by\nt/test-lib.sh (either from the GIT_TEST_DEFAULT_HASH exported by\nthe CI framework or from the GIT_TEST_BUILTIN_HASH to match the\nrunning binary).  \"git init repo\" will create a repository using\nthe binary built-in default.\n\n> +\t\techo \"$GIT_TEST_BUILTIN_HASH\" >expect &&\n> +\t\tgit -C repo rev-parse --show-object-format >actual &&\n\nAnd we have to see the same TEST_BUILTIN_HASH as the result.\n\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n\nVery good.\n\n> +test_expect_success 'init refuses to reinitialize with different hash' '\n> +\ttest_when_finished \"rm -rf repo\" &&\n> +\tsane_unset GIT_DEFAULT_HASH &&\n\nHere, we permanently discard GIT_DEFAULT_HASH from t/test-lib.sh\nwithout saving what it was.  Would it cause issues to later tests\nthat we do not see in post context of this patch?\n\n> +\tfor algo in sha1 sha256\n> +\tdo\n> +\t\tif test \"$algo\" = sha1\n> +\t\tthen\n> +\t\t\tother=sha256\n> +\t\telse\n> +\t\t\tother=sha1\n> +\t\tfi &&\n> +\t\tgit init --object-format=\"$algo\" repo &&\n\nWe create in one format and then ...\n\n> +\t\tcat >expect <<-EOF &&\n> +\t\tfatal: attempt to reinitialize repository with different hash\n> +\t\tEOF\n> +\t\ttest_must_fail git init --object-format=\"$other\" repo 2>err &&\n\n... try reinitializing to the other format.  Expected is a failure\nwith a message.\n\n> +\t\ttest_cmp expect err &&\n> +\t\trm -fr repo || return 1\n> +\tdone\n> +'\n\nOK.\n\n> +test_expect_success 're-init does not modify hash from config' '\n> +\ttest_when_finished \"rm -rf repo\" &&\n> +\tsane_unset GIT_DEFAULT_HASH &&\n\nHere, we discard GIT_DEFAULT_HASH again, correctly assumig that the\nprevious one can be skipped.\n\n> +\tfor algo in sha1 sha256\n> +\tdo\n> +\t\tif test \"$algo\" = sha1\n> +\t\tthen\n> +\t\t\tother=sha256\n> +\t\telse\n> +\t\t\tother=sha1\n> +\t\tfi &&\n> +\t\tgit init --object-format=\"$algo\" repo &&\n\nAgain, we create in one format and then ...\n\n> +\t\ttest_config_global init.defaultObjectFormat \"$other\" &&\n> +\t\tgit init repo &&\n> +\t\techo \"$algo\" >expect &&\n\n... we do not want the configuration to cause reinitialization to\ntouch the format.\n\n> +\t\tgit -C repo rev-parse --show-object-format >actual &&\n> +\t\ttest_cmp expect actual &&\n> +\t\trm -fr repo || return 1\n> +\tdone\n> +'\n\nLooks good.\n\n>  test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '\n>  \ttest_when_finished \"rm -rf explicit-v0\" &&\n>  \tgit init --object-format=sha256 explicit-v0 &&\n\nMy earlier question was about this one.  A format is explicitly\ngiven to this one, so the environment variables would not affect\nthis particular one, but if we have \"git init\" in later tests that\nused to rely on the GIT_DEFAULT_HASH setting left by t/test-lib.sh\nmay we get in a trobule?\n\n"},{"id":"558862","messageId":"xmqqmrr4zsi2.fsf@gitster.g","threadId":"66793","inReplyTo":"20261119010612.687733-1-sandals@crustytoothpaste.net","subject":"Re: [PATCH v3] setup: fix hash algorithm detection","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-19T16:38:13Z","receivedAt":"2026-11-19T16:38:13Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"brian m. carlson\" <sandals@crustytoothpaste.net> writes:\n\n> Our current hash algorithm selection code has a few different problems.\n> First, we do not actually default to GIT_HASH_DEFAULT.  Second, if we\n> have an algorithm chosen by init.defaultObjectFormat and we re-init a\n> repository, we will change the hash algorithm, which is very much not\n> desired.\n\nCan we end the \"first\" sentence with a reason?  We do not actually\ndefault to GIT_HASH_DEFAULT because we initialize a variable to\nSHA1_LEGACY and we do not consider it as \"nowhere the user specifies\nwhich algorithm they want\", or something?\n\n> Let's restructure this code slightly.  First, if an algorithm was\n> selected on the command line, assume that's what the user wanted.\n> Otherwise, the other options should only be chosen if our repository\n> doesn't exist (that is, repo_fmt->version is -1) or it's specifically\n> detected as having no hash algorithm.  In that case, first use the\n> environment, then the config, and finally, the default algorithm.\n\nIn the current code, does \"it's specifically having no hash\nalgorithm\" ever happen?  Or is it our wish in near future to have\nsuch a state to allow \"the first use determines the hash algorithm\nused\" mode?\n\n> Then, if the algorithm does not match the repository, complain loudly\n> and refuse to reinitialize the repository with a different hash\n> algorithm.\n>\n> With this change, we will choose SHA-256 in breaking changes mode and in\n> Git 3.0, which is what we want.  Add several tests to cover this case\n> and several others discovered along the way.  Remove the now-inaccurate\n> comment.\n\nIs the comment \"now inaccurate\"?  \n\n> -\t/*\n> -\t * If we already have an initialized repo, don't allow the user to\n> -\t * specify a different algorithm, as that could cause corruption.\n> -\t * Otherwise, if the user has specified one on the command line, use it.\n> -\t */\n\nI think the first two lines is still relevant and serves as a\ncaution to whoever will be mucking with this function in the future.\nThe last sentence is not quite right in that following the user's\nwish is not limited to the command line instruction (if they tell us\nvia an environment variable, we would honor it).\n\nOh, it just occured to me while I was writing the above\nparenthesized comment.  If I have ~/.gitconfig that was modified via\n\"git config set --global init.defaultObjectFormat sha256\", would\nthat count as \"the user has specified\"?\n\nThanks.\n\n"},{"id":"558895","messageId":"av92KlePF0GKyQDh@fruit.crustytoothpaste.net","threadId":"66793","inReplyTo":"xmqqmrr4zsi2.fsf@gitster.g","subject":"Re: [PATCH v3] setup: fix hash algorithm detection","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2026-11-19T22:20:26Z","receivedAt":"2026-11-19T22:20:26Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2026-11-19 at 16:38:13, Junio C Hamano wrote:\n> \"brian m. carlson\" <sandals@crustytoothpaste.net> writes:\n> \n> > Our current hash algorithm selection code has a few different problems.\n> > First, we do not actually default to GIT_HASH_DEFAULT.  Second, if we\n> > have an algorithm chosen by init.defaultObjectFormat and we re-init a\n> > repository, we will change the hash algorithm, which is very much not\n> > desired.\n> \n> Can we end the \"first\" sentence with a reason?  We do not actually\n> default to GIT_HASH_DEFAULT because we initialize a variable to\n> SHA1_LEGACY and we do not consider it as \"nowhere the user specifies\n> which algorithm they want\", or something?\n\nI can expand on that.\n\n> > Let's restructure this code slightly.  First, if an algorithm was\n> > selected on the command line, assume that's what the user wanted.\n> > Otherwise, the other options should only be chosen if our repository\n> > doesn't exist (that is, repo_fmt->version is -1) or it's specifically\n> > detected as having no hash algorithm.  In that case, first use the\n> > environment, then the config, and finally, the default algorithm.\n> \n> In the current code, does \"it's specifically having no hash\n> algorithm\" ever happen?  Or is it our wish in near future to have\n> such a state to allow \"the first use determines the hash algorithm\n> used\" mode?\n\nIt should not as far as I can tell, but we have a bunch of different\ncases, including the clone case, and I'm not 100% sure if that actually\never happens.\n\n> > -\t/*\n> > -\t * If we already have an initialized repo, don't allow the user to\n> > -\t * specify a different algorithm, as that could cause corruption.\n> > -\t * Otherwise, if the user has specified one on the command line, use it.\n> > -\t */\n> \n> I think the first two lines is still relevant and serves as a\n> caution to whoever will be mucking with this function in the future.\n> The last sentence is not quite right in that following the user's\n> wish is not limited to the command line instruction (if they tell us\n> via an environment variable, we would honor it).\n> \n> Oh, it just occured to me while I was writing the above\n> parenthesized comment.  If I have ~/.gitconfig that was modified via\n> \"git config set --global init.defaultObjectFormat sha256\", would\n> that count as \"the user has specified\"?\n\nI'll rewrite the comment.  The \"user has specified one on the command\nline\" means with `--object-format`, but the user could specify one with\n`init.defaultObjectFormat`.  The difference is we scream if the former\nmismatches with the repository, whereas if the repository is already\ninitialized, we ignore `GIT_DEFAULT_HASH` and\n`init.defaultObjectFormat`.\n-- \nbrian m. carlson (they/them)\nToronto, Ontario, CA\nSpeaking for myself only\n"},{"id":"558901","messageId":"xmqqse0wto9l.fsf@gitster.g","threadId":"66793","inReplyTo":"av92KlePF0GKyQDh@fruit.crustytoothpaste.net","subject":"Re: [PATCH v3] setup: fix hash algorithm detection","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-19T23:06:14Z","receivedAt":"2026-11-19T23:06:14Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"brian m. carlson\" <sandals@crustytoothpaste.net> writes:\n\n> ...  The difference is we scream if the former\n> mismatches with the repository, whereas if the repository is already\n> initialized, we ignore `GIT_DEFAULT_HASH` and\n> `init.defaultObjectFormat`.\n\nThat makes perfect sense.  Thanks.\n\n"}]}