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>

Patrick Steinhardt committed Aug 16, 2024 at 10:57 UTC 0c22e09b7376ce9c010e5901108bab08e5ec2b2c
3 files changed +95
Documentation/config/init.txt
+5
@@ -8,3 +8,8 @@ endif::[]
8 `init.defaultBranch`::
9 Allows overriding the default branch name e.g. when initializing
10 a new repository.
11 +`init.defaultObjectFormat`::
12 + Allows overriding the default object format for new repositories. See
13 + `--object-format=` in linkgit:git-init[1]. Both the command line option
14 + and the `GIT_DEFAULT_HASH` environment variable take precedence over
15 + this config.
setup.c
+40
@@ -2284,11 +2284,49 @@ static void separate_git_dir(const char *git_dir, const char *git_link)
2284 write_file(git_link, "gitdir: %s", git_dir);
2285 }
2286
2287 +struct default_format_config {
2288 + int hash;
2289 +};
2290 +
2291 +static int read_default_format_config(const char *key, const char *value,
2292 + const struct config_context *ctx UNUSED,
2293 + void *payload)
2294 +{
2295 + struct default_format_config *cfg = payload;
2296 + char *str = NULL;
2297 + int ret;
2298 +
2299 + if (!strcmp(key, "init.defaultobjectformat")) {
2300 + ret = git_config_string(&str, key, value);
2301 + if (ret)
2302 + goto out;
2303 + cfg->hash = hash_algo_by_name(str);
2304 + if (cfg->hash == GIT_HASH_UNKNOWN)
2305 + warning(_("unknown hash algorithm '%s'"), str);
2306 + goto out;
2307 + }
2308 +
2309 + ret = 0;
2310 +out:
2311 + free(str);
2312 + return ret;
2313 +}
2314 +
2315 static void repository_format_configure(struct repository_format *repo_fmt,
2316 int hash, enum ref_storage_format ref_format)
2317 {
2318 + struct default_format_config cfg = {
2319 + .hash = GIT_HASH_UNKNOWN,
2320 + };
2321 + struct config_options opts = {
2322 + .respect_includes = 1,
2323 + .ignore_repo = 1,
2324 + .ignore_worktree = 1,
2325 + };
2326 const char *env;
2327
2328 + config_with_options(read_default_format_config, &cfg, NULL, NULL, &opts);
2329 +
2330 /*
2331 * If we already have an initialized repo, don't allow the user to
2332 * specify a different algorithm, as that could cause corruption.
@@ -2304,6 +2342,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,
2342 if (env_algo == GIT_HASH_UNKNOWN)
2343 die(_("unknown hash algorithm '%s'"), env);
2344 repo_fmt->hash_algo = env_algo;
2345 + } else if (cfg.hash != GIT_HASH_UNKNOWN) {
2346 + repo_fmt->hash_algo = cfg.hash;
2347 }
2348 repo_set_hash_algo(the_repository, repo_fmt->hash_algo);
2349
t/t0001-init.sh
+50
@@ -523,6 +523,56 @@ test_expect_success 'init honors --object-format' '
523 test_cmp expected actual
524 '
525
526 +test_expect_success 'init honors init.defaultObjectFormat' '
527 + test_when_finished "rm -rf sha1 sha256" &&
528 +
529 + test_config_global init.defaultObjectFormat sha1 &&
530 + (
531 + sane_unset GIT_DEFAULT_HASH &&
532 + git init sha1 &&
533 + git -C sha1 rev-parse --show-object-format >actual &&
534 + echo sha1 >expected &&
535 + test_cmp expected actual
536 + ) &&
537 +
538 + test_config_global init.defaultObjectFormat sha256 &&
539 + (
540 + sane_unset GIT_DEFAULT_HASH &&
541 + git init sha256 &&
542 + git -C sha256 rev-parse --show-object-format >actual &&
543 + echo sha256 >expected &&
544 + test_cmp expected actual
545 + )
546 +'
547 +
548 +test_expect_success 'init warns about invalid init.defaultObjectFormat' '
549 + test_when_finished "rm -rf repo" &&
550 + test_config_global init.defaultObjectFormat garbage &&
551 +
552 + echo "warning: unknown hash algorithm ${SQ}garbage${SQ}" >expect &&
553 + git init repo 2>err &&
554 + test_cmp expect err &&
555 +
556 + git -C repo rev-parse --show-object-format >actual &&
557 + echo $GIT_DEFAULT_HASH >expected &&
558 + test_cmp expected actual
559 +'
560 +
561 +test_expect_success '--object-format overrides GIT_DEFAULT_HASH' '
562 + test_when_finished "rm -rf repo" &&
563 + GIT_DEFAULT_HASH=sha1 git init --object-format=sha256 repo &&
564 + git -C repo rev-parse --show-object-format >actual &&
565 + echo sha256 >expected
566 +'
567 +
568 +test_expect_success 'GIT_DEFAULT_HASH overrides init.defaultObjectFormat' '
569 + test_when_finished "rm -rf repo" &&
570 + test_config_global init.defaultObjectFormat sha1 &&
571 + GIT_DEFAULT_HASH=sha256 git init repo &&
572 + git -C repo rev-parse --show-object-format >actual &&
573 + echo sha256 >expected
574 +'
575 +
576 test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
577 test_when_finished "rm -rf explicit-v0" &&
578 git init --object-format=sha256 explicit-v0 &&