]> git.ipfire.org Git - thirdparty/git.git/commit
setup: fix reinit of repos with incompatible GIT_DEFAULT_HASH
authorPatrick Steinhardt <ps@pks.im>
Thu, 30 Jan 2025 16:24:19 +0000 (17:24 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Jan 2025 22:36:41 +0000 (14:36 -0800)
commit7e88640cd1801b6fe0288f744da7310b4749c0c8
tree1246200c94853f0e3e0ac16ed80a863ae00f882c
parent796fda3f786b3cd5518462b46895244dfecad63c
setup: fix reinit of repos with incompatible GIT_DEFAULT_HASH

The exact same issue as described in the preceding commit also exists
for GIT_DEFAULT_HASH. Thus, reinitializing a repository that e.g. uses
SHA1 with `GIT_DEFAULT_HASH=sha256 git init` will cause the object
format of that repository to change to SHA256. This is of course bogus
as any existing objects and refs will not be converted, thus causing
repository corruption:

    $ git init repo
    Initialized empty Git repository in /tmp/repo/.git/
    $ cd repo/
    $ git commit --allow-empty -m message
    [main (root-commit) 35a7344] message
    $ GIT_DEFAULT_HASH=sha256 git init
    Reinitialized existing Git repository in /tmp/repo/.git/
    $ git show
    fatal: your current branch appears to be broken

Fix the issue by ignoring the environment variable in case the repo has
already been initialized with an object hash.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
setup.c
t/t0001-init.sh