setup: make object format configurable via config

The object format for repositories can either be configured explicitly
by passing the `--object-format=` option to git-init(1) or git-clone(1),
or globally by setting the `GIT_DEFAULT_HASH` environment variable.
While the former makes sense, setting random environment variables is
not really a good user experience in case someone decides to only use
SHA256 repositories.

It is only natural to expect for a user that things like this can also
be configured via their config. As such, introduce a new config
"init.defaultObjectFormat", similar to "init.defaultBranch", that allows
the user to configure the default object format when creating new repos.

The precedence order now is the following, where the first one wins:

  1. The `--object-format=` switch.

  2. The `GIT_DEFAULT_HASH` environment variable.

  3. The `init.defaultObjectFormat` config variable.

This matches the typical precedence order we use in Git. We typically
let the environment override the config such that the latter can easily
be overridden on an ephemeral basis, for example by scripts.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2024-08-16 10:57:03 +02:00
committed by Junio C Hamano
parent 39e15b789a
commit 0c22e09b73
3 changed files with 95 additions and 0 deletions

View File

@ -523,6 +523,56 @@ test_expect_success 'init honors --object-format' '
test_cmp expected actual
'
test_expect_success 'init honors init.defaultObjectFormat' '
test_when_finished "rm -rf sha1 sha256" &&
test_config_global init.defaultObjectFormat sha1 &&
(
sane_unset GIT_DEFAULT_HASH &&
git init sha1 &&
git -C sha1 rev-parse --show-object-format >actual &&
echo sha1 >expected &&
test_cmp expected actual
) &&
test_config_global init.defaultObjectFormat sha256 &&
(
sane_unset GIT_DEFAULT_HASH &&
git init sha256 &&
git -C sha256 rev-parse --show-object-format >actual &&
echo sha256 >expected &&
test_cmp expected actual
)
'
test_expect_success 'init warns about invalid init.defaultObjectFormat' '
test_when_finished "rm -rf repo" &&
test_config_global init.defaultObjectFormat garbage &&
echo "warning: unknown hash algorithm ${SQ}garbage${SQ}" >expect &&
git init repo 2>err &&
test_cmp expect err &&
git -C repo rev-parse --show-object-format >actual &&
echo $GIT_DEFAULT_HASH >expected &&
test_cmp expected actual
'
test_expect_success '--object-format overrides GIT_DEFAULT_HASH' '
test_when_finished "rm -rf repo" &&
GIT_DEFAULT_HASH=sha1 git init --object-format=sha256 repo &&
git -C repo rev-parse --show-object-format >actual &&
echo sha256 >expected
'
test_expect_success 'GIT_DEFAULT_HASH overrides init.defaultObjectFormat' '
test_when_finished "rm -rf repo" &&
test_config_global init.defaultObjectFormat sha1 &&
GIT_DEFAULT_HASH=sha256 git init repo &&
git -C repo rev-parse --show-object-format >actual &&
echo sha256 >expected
'
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 &&