From 05d20915bc2658371989a47ff1214e2eb6506de4 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 16 Aug 2024 10:56:53 +0200 Subject: [PATCH 1/5] t0001: exercise initialization with ref formats more thoroughly While our object format tests for git-init(1) exercise tests with all known formats in t0001, the tests for the ref format don't. This leads to some missing test coverage for interesting cases, like whether or not a non-default ref storage format causes us to bump the repository format version. We also don't test for the precedence of the `--ref-format=` and the `GIT_DEFAULT_REF_FORMAT=` environment variable. Extend the test suite to cover more scenarios related to the ref format. Signed-off-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- t/t0001-init.sh | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 49e9bf77c6..2093f5c1ee 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -558,15 +558,6 @@ test_expect_success DEFAULT_REPO_FORMAT 'extensions.refStorage with unknown back grep "invalid value for ${SQ}extensions.refstorage${SQ}: ${SQ}garbage${SQ}" err ' -test_expect_success DEFAULT_REPO_FORMAT 'init with GIT_DEFAULT_REF_FORMAT=files' ' - test_when_finished "rm -rf refformat" && - GIT_DEFAULT_REF_FORMAT=files git init refformat && - echo 0 >expect && - git -C refformat config core.repositoryformatversion >actual && - test_cmp expect actual && - test_must_fail git -C refformat config extensions.refstorage -' - test_expect_success 'init with GIT_DEFAULT_REF_FORMAT=garbage' ' test_when_finished "rm -rf refformat" && cat >expect <<-EOF && @@ -576,15 +567,46 @@ test_expect_success 'init with GIT_DEFAULT_REF_FORMAT=garbage' ' test_cmp expect err ' -test_expect_success 'init with --ref-format=files' ' +backends="files reftable" +for format in $backends +do + test_expect_success DEFAULT_REPO_FORMAT "init with GIT_DEFAULT_REF_FORMAT=$format" ' + test_when_finished "rm -rf refformat" && + GIT_DEFAULT_REF_FORMAT=$format git init refformat && + + if test $format = files + then + test_must_fail git -C refformat config extensions.refstorage && + echo 0 >expect + else + git -C refformat config extensions.refstorage && + echo 1 >expect + fi && + git -C refformat config core.repositoryformatversion >actual && + test_cmp expect actual && + + echo $format >expect && + git -C refformat rev-parse --show-ref-format >actual && + test_cmp expect actual + ' + + test_expect_success "init with --ref-format=$format" ' + test_when_finished "rm -rf refformat" && + git init --ref-format=$format refformat && + echo $format >expect && + git -C refformat rev-parse --show-ref-format >actual && + test_cmp expect actual + ' +done + +test_expect_success "--ref-format= overrides GIT_DEFAULT_REF_FORMAT" ' test_when_finished "rm -rf refformat" && - git init --ref-format=files refformat && - echo files >expect && + GIT_DEFAULT_REF_FORMAT=files git init --ref-format=reftable refformat && + echo reftable >expect && git -C refformat rev-parse --show-ref-format >actual && test_cmp expect actual ' -backends="files reftable" for from_format in $backends do test_expect_success "re-init with same format ($from_format)" ' From 7689f6cbd11cbfd0828a77a5fb070d5a2f06edc7 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 16 Aug 2024 10:56:55 +0200 Subject: [PATCH 2/5] t0001: delete repositories when object format tests finish The object format tests create one-shot repositories that are only used by the respective test, but never delete them. This makes it hard to pick a proper repository name in subsequent tests, as more and more names are taken already. Delete these repositories via `test_when_finished`. Signed-off-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- t/t0001-init.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 2093f5c1ee..795408e16c 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -500,6 +500,7 @@ test_expect_success 're-init from a linked worktree' ' ' test_expect_success 'init honors GIT_DEFAULT_HASH' ' + test_when_finished "rm -rf sha1 sha256" && GIT_DEFAULT_HASH=sha1 git init sha1 && git -C sha1 rev-parse --show-object-format >actual && echo sha1 >expected && @@ -511,6 +512,7 @@ test_expect_success 'init honors GIT_DEFAULT_HASH' ' ' test_expect_success 'init honors --object-format' ' + test_when_finished "rm -rf explicit-sha1 explicit-sha256" && git init --object-format=sha1 explicit-sha1 && git -C explicit-sha1 rev-parse --show-object-format >actual && echo sha1 >expected && @@ -522,6 +524,7 @@ test_expect_success 'init honors --object-format' ' ' 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 && git -C explicit-v0 config core.repositoryformatversion 0 && test_must_fail git -C explicit-v0 rev-parse --show-object-format From 39e15b789ad3c3a192f4c8abde3daff83a053aaa Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 16 Aug 2024 10:56:58 +0200 Subject: [PATCH 3/5] setup: merge configuration of repository formats The configuration of repository formats is split up across two functions `validate_hash_algorithm()` and `validate_ref_storage_format()`. This is fine as-is, but we are about to extend the logic to also read default values from the config. With the logic split across two functions, we would either have to pass in additional parameters read from the config, or read the config multiple times. Both of these options feel a bit unwieldy. Merge the code into a new function `repository_format_configure()` that is responsible for configuring the whole repository's format. Like this, we can easily read the config in a single place, only. Furthermore, move the calls to `repo_set_ref_storage_format()` and `repo_set_hash_algo()` into this new function as well, such that all the logic to configure the repository format is self-contained here. Signed-off-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- setup.c | 47 ++++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/setup.c b/setup.c index d458edcc02..5dfcdc99dd 100644 --- a/setup.c +++ b/setup.c @@ -2284,14 +2284,17 @@ static void separate_git_dir(const char *git_dir, const char *git_link) write_file(git_link, "gitdir: %s", git_dir); } -static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash) +static void repository_format_configure(struct repository_format *repo_fmt, + int hash, enum ref_storage_format ref_format) { - const char *env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT); + const char *env; + /* * 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) @@ -2302,25 +2305,22 @@ static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash die(_("unknown hash algorithm '%s'"), env); repo_fmt->hash_algo = env_algo; } -} - -static void validate_ref_storage_format(struct repository_format *repo_fmt, - enum ref_storage_format format) -{ - const char *name = getenv("GIT_DEFAULT_REF_FORMAT"); + repo_set_hash_algo(the_repository, repo_fmt->hash_algo); + env = getenv("GIT_DEFAULT_REF_FORMAT"); if (repo_fmt->version >= 0 && - format != REF_STORAGE_FORMAT_UNKNOWN && - format != repo_fmt->ref_storage_format) { + ref_format != REF_STORAGE_FORMAT_UNKNOWN && + ref_format != repo_fmt->ref_storage_format) { die(_("attempt to reinitialize repository with different reference storage format")); - } else if (format != REF_STORAGE_FORMAT_UNKNOWN) { - repo_fmt->ref_storage_format = format; - } else if (name) { - format = ref_storage_format_by_name(name); - if (format == REF_STORAGE_FORMAT_UNKNOWN) - die(_("unknown ref storage format '%s'"), name); - repo_fmt->ref_storage_format = format; + } else if (ref_format != REF_STORAGE_FORMAT_UNKNOWN) { + repo_fmt->ref_storage_format = ref_format; + } else if (env) { + ref_format = ref_storage_format_by_name(env); + if (ref_format == REF_STORAGE_FORMAT_UNKNOWN) + die(_("unknown ref storage format '%s'"), env); + repo_fmt->ref_storage_format = ref_format; } + repo_set_ref_storage_format(the_repository, repo_fmt->ref_storage_format); } int init_db(const char *git_dir, const char *real_git_dir, @@ -2353,22 +2353,15 @@ int init_db(const char *git_dir, const char *real_git_dir, } startup_info->have_repository = 1; - /* Check to see if the repository version is right. + /* + * Check to see if the repository version is right. * Note that a newly created repository does not have * config file, so this will not fail. What we are catching * is an attempt to reinitialize new repository with an old tool. */ check_repository_format(&repo_fmt); - validate_hash_algorithm(&repo_fmt, hash); - validate_ref_storage_format(&repo_fmt, ref_storage_format); - - /* - * Now that we have set up both the hash algorithm and the ref storage - * format we can update the repository's settings accordingly. - */ - repo_set_hash_algo(the_repository, repo_fmt.hash_algo); - repo_set_ref_storage_format(the_repository, repo_fmt.ref_storage_format); + repository_format_configure(&repo_fmt, hash, ref_storage_format); /* * Ensure `core.hidedotfiles` is processed. This must happen after we From 0c22e09b7376ce9c010e5901108bab08e5ec2b2c Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 16 Aug 2024 10:57:03 +0200 Subject: [PATCH 4/5] 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 Signed-off-by: Junio C Hamano --- Documentation/config/init.txt | 5 ++++ setup.c | 40 ++++++++++++++++++++++++++++ t/t0001-init.sh | 50 +++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) diff --git a/Documentation/config/init.txt b/Documentation/config/init.txt index af03acdbcb..d6f8b6e61b 100644 --- a/Documentation/config/init.txt +++ b/Documentation/config/init.txt @@ -8,3 +8,8 @@ endif::[] `init.defaultBranch`:: Allows overriding the default branch name e.g. when initializing a new repository. +`init.defaultObjectFormat`:: + Allows overriding the default object format for new repositories. See + `--object-format=` in linkgit:git-init[1]. Both the command line option + and the `GIT_DEFAULT_HASH` environment variable take precedence over + this config. diff --git a/setup.c b/setup.c index 5dfcdc99dd..770ad1393f 100644 --- a/setup.c +++ b/setup.c @@ -2284,11 +2284,49 @@ static void separate_git_dir(const char *git_dir, const char *git_link) write_file(git_link, "gitdir: %s", git_dir); } +struct default_format_config { + int hash; +}; + +static int read_default_format_config(const char *key, const char *value, + const struct config_context *ctx UNUSED, + void *payload) +{ + struct default_format_config *cfg = payload; + char *str = NULL; + int ret; + + if (!strcmp(key, "init.defaultobjectformat")) { + ret = git_config_string(&str, key, value); + if (ret) + goto out; + cfg->hash = hash_algo_by_name(str); + if (cfg->hash == GIT_HASH_UNKNOWN) + warning(_("unknown hash algorithm '%s'"), str); + goto out; + } + + ret = 0; +out: + free(str); + return ret; +} + static void repository_format_configure(struct repository_format *repo_fmt, int hash, enum ref_storage_format ref_format) { + struct default_format_config cfg = { + .hash = GIT_HASH_UNKNOWN, + }; + struct config_options opts = { + .respect_includes = 1, + .ignore_repo = 1, + .ignore_worktree = 1, + }; const char *env; + 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. @@ -2304,6 +2342,8 @@ static void repository_format_configure(struct repository_format *repo_fmt, if (env_algo == GIT_HASH_UNKNOWN) die(_("unknown hash algorithm '%s'"), env); repo_fmt->hash_algo = env_algo; + } else if (cfg.hash != GIT_HASH_UNKNOWN) { + repo_fmt->hash_algo = cfg.hash; } repo_set_hash_algo(the_repository, repo_fmt->hash_algo); diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 795408e16c..cd34710f32 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -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 && From d2511eeae5fc679cf1b1591b3604e6abf5c056c2 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 16 Aug 2024 10:57:12 +0200 Subject: [PATCH 5/5] setup: make ref storage format configurable via config Similar to the preceding commit, introduce a new "init.defaultRefFormat" config that allows the user to globally set the ref storage format used by newly created repositories. Signed-off-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- Documentation/config/init.txt | 5 ++++ setup.c | 14 +++++++++++ t/t0001-init.sh | 44 +++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/Documentation/config/init.txt b/Documentation/config/init.txt index d6f8b6e61b..e45b2a8121 100644 --- a/Documentation/config/init.txt +++ b/Documentation/config/init.txt @@ -13,3 +13,8 @@ endif::[] `--object-format=` in linkgit:git-init[1]. Both the command line option and the `GIT_DEFAULT_HASH` environment variable take precedence over this config. +`init.defaultRefFormat`:: + Allows overriding the default ref storage format for new repositories. + See `--ref-format=` in linkgit:git-init[1]. Both the command line + option and the `GIT_DEFAULT_REF_FORMAT` environment variable take + precedence over this config. diff --git a/setup.c b/setup.c index 770ad1393f..dd2251f655 100644 --- a/setup.c +++ b/setup.c @@ -2286,6 +2286,7 @@ static void separate_git_dir(const char *git_dir, const char *git_link) struct default_format_config { int hash; + enum ref_storage_format ref_format; }; static int read_default_format_config(const char *key, const char *value, @@ -2306,6 +2307,16 @@ static int read_default_format_config(const char *key, const char *value, goto out; } + if (!strcmp(key, "init.defaultrefformat")) { + ret = git_config_string(&str, key, value); + if (ret) + goto out; + cfg->ref_format = ref_storage_format_by_name(str); + if (cfg->ref_format == REF_STORAGE_FORMAT_UNKNOWN) + warning(_("unknown ref storage format '%s'"), str); + goto out; + } + ret = 0; out: free(str); @@ -2317,6 +2328,7 @@ static void repository_format_configure(struct repository_format *repo_fmt, { struct default_format_config cfg = { .hash = GIT_HASH_UNKNOWN, + .ref_format = REF_STORAGE_FORMAT_UNKNOWN, }; struct config_options opts = { .respect_includes = 1, @@ -2359,6 +2371,8 @@ static void repository_format_configure(struct repository_format *repo_fmt, if (ref_format == REF_STORAGE_FORMAT_UNKNOWN) die(_("unknown ref storage format '%s'"), env); repo_fmt->ref_storage_format = ref_format; + } else if (cfg.ref_format != REF_STORAGE_FORMAT_UNKNOWN) { + repo_fmt->ref_storage_format = cfg.ref_format; } repo_set_ref_storage_format(the_repository, repo_fmt->ref_storage_format); } diff --git a/t/t0001-init.sh b/t/t0001-init.sh index cd34710f32..0178aa62a4 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -620,6 +620,19 @@ test_expect_success 'init with GIT_DEFAULT_REF_FORMAT=garbage' ' test_cmp expect err ' +test_expect_success 'init warns about invalid init.defaultRefFormat' ' + test_when_finished "rm -rf repo" && + test_config_global init.defaultRefFormat garbage && + + echo "warning: unknown ref storage format ${SQ}garbage${SQ}" >expect && + git init repo 2>err && + test_cmp expect err && + + git -C repo rev-parse --show-ref-format >actual && + echo $GIT_DEFAULT_REF_FORMAT >expected && + test_cmp expected actual +' + backends="files reftable" for format in $backends do @@ -650,6 +663,27 @@ do git -C refformat rev-parse --show-ref-format >actual && test_cmp expect actual ' + + test_expect_success "init with init.defaultRefFormat=$format" ' + test_when_finished "rm -rf refformat" && + test_config_global init.defaultRefFormat $format && + ( + sane_unset GIT_DEFAULT_REF_FORMAT && + git init refformat + ) && + + echo $format >expect && + git -C refformat rev-parse --show-ref-format >actual && + test_cmp expect actual + ' + + test_expect_success "--ref-format=$format overrides GIT_DEFAULT_REF_FORMAT" ' + test_when_finished "rm -rf refformat" && + GIT_DEFAULT_REF_FORMAT=garbage git init --ref-format=$format refformat && + echo $format >expect && + git -C refformat rev-parse --show-ref-format >actual && + test_cmp expect actual + ' done test_expect_success "--ref-format= overrides GIT_DEFAULT_REF_FORMAT" ' @@ -660,6 +694,16 @@ test_expect_success "--ref-format= overrides GIT_DEFAULT_REF_FORMAT" ' test_cmp expect actual ' +test_expect_success "GIT_DEFAULT_REF_FORMAT= overrides init.defaultRefFormat" ' + test_when_finished "rm -rf refformat" && + test_config_global init.defaultRefFormat files && + + GIT_DEFAULT_REF_FORMAT=reftable git init refformat && + echo reftable >expect && + git -C refformat rev-parse --show-ref-format >actual && + test_cmp expect actual +' + for from_format in $backends do test_expect_success "re-init with same format ($from_format)" '