Skip to contentSkip to conversation

Introduce configs for default repo format

PATCH Superseded Patrick Steinhardt 5 participants 24 messages Aug 15, 2024 – Aug 16, 2024 Documentation

lore
Share & tools
lore
#0 lore raw

cover Introduce configs for default repo format

Hi,

to set up the default object and ref storage formats, users have to set up some environment variables. This is somewhat unwieldy and not really in line with how a user typically expects to configure Git, namely by using the config system. It makes it harder than necessary to globally default to the different formats and requires the user to munge with files like .profile to persist that setting. Needless to say, this is a bit of an awkward user experience.

This patch series thus introduces two new configs to set the default object hash and ref storage format for newly created repositories. Like this, folks can simply use the global- or system-level config to adapt to their needs. This also has the advantage of giving them the ability to adapt the default formats via guarded includes, such that e.g. repos in some filesystem hierarchy use format A, whereas others use format B.

This comes from a discussion with Sebastian (Cc'd) at the Git User Group in Berlin yesterday.

Thanks!

Patrick

Patrick Steinhardt (5):
t0001: exercise initialization with ref formats more thoroughly
t0001: delete repositories when object format tests finish
setup: merge configuration of repository formats
setup: make object format configurable via config
setup: make ref storage format configurable via config

 Documentation/config/init.txt |  10 +++
 setup.c                       | 101 ++++++++++++++++-------
 t/t0001-init.sh               | 145 +++++++++++++++++++++++++++++++---
 3 files changed, 216 insertions(+), 40 deletions(-)
Signature · 1 line
2.46.0.46.g406f326d27.dirty
#1 lore raw

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.

---
 t/t0001-init.sh | 48 +++++++++++++++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 13 deletions(-)
t/t0001-init.sh +35 -13
index 49e9bf77c6..2093f5c1ee 100755
@@ -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)" '
Signature · 1 line
2.46.0.46.g406f326d27.dirty
#2 lore raw

Re: [PATCH 1/5] t0001: exercise initialization with ref formats more thoroughly

Quoted 6 lines · Patrick Steinhardt

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.

Makes sense to plug these test gaps.

Quoted 28 lines

Extend the test suite to cover more scenarios related to the ref format.

---
 t/t0001-init.sh | 48 +++++++++++++++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 13 deletions(-)
t/t0001-init.sh +0 -9
index 49e9bf77c6..2093f5c1ee 100755
@@ -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 &&

t/t0001-init.sh test_expect_success 'init with GIT_DEFAULT_REF_FORMAT=garbage' ' · line 573

Quoted 8 lines
t/t0001-init.sh +4 -1
@@ -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" '

The DEFAULT_REPO_FORMAT prereq is needed here because a non-default repo format also changes the repository format version.

Quoted 27 lines
t/t0001-init.sh +27 -0
+		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

t/t0001-init.sh test_expect_success 'init with GIT_DEFAULT_REF_FORMAT=garbage' ' · line 608

Quoted 10 lines
t/t0001-init.sh +4 -2
+
+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
 '

Nice that we now validate that the --ref-format option takes precedence over the GIT_DEFAULT_REF_FORMAT environment variable.

-Justin

#3 lore raw

PATCH 2/5 t0001: delete repositories when object format tests finish

In reply to Patrick Steinhardt

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.

---
 t/t0001-init.sh | 3 +++
 1 file changed, 3 insertions(+)
t/t0001-init.sh +3 -0
index 2093f5c1ee..795408e16c 100755
@@ -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
Signature · 1 line
2.46.0.46.g406f326d27.dirty
#4 lore raw

PATCH 3/5 setup: merge configuration of repository formats

In reply to Patrick Steinhardt

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 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.

---
 setup.c | 47 ++++++++++++++++++++---------------------------
 1 file changed, 20 insertions(+), 27 deletions(-)
setup.c +20 -27
index d458edcc02..5dfcdc99dd 100644
@@ -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
Signature · 1 line
2.46.0.46.g406f326d27.dirty
#5 lore raw

Re: [PATCH 3/5] setup: merge configuration of repository formats

Quoted 7 lines · Patrick Steinhardt

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.

Combining the repository format configuration logic into a single function seems like a good option. It looks like go we even further though since we also include setting the hash and ref format for the_repository. Might be nice to also mention this.

Quoted 1 line

Merge the code into a new a new function repository_format_configure()

s/new a new/new/

Quoted 5 lines

that is responsible for configuring the whole repository's format. Like this, we can easily read the config in a single place, only.

---

[snip]

setup.c int init_db(const char *git_dir, const char *real_git_dir, · line 2369

Quoted 4 lines
setup.c +0 -4
-	/*
-	 * Now that we have set up both the hash algorithm and the ref storage
-	 * format we can update the repository's settings accordingly.
-	 */

The above comment somewhat made it sound like the repository format had to be configured for both the hash algo and ref storage before updating the_repository, but that does not seem to be a requirement in actuality.

setup.c int init_db(const char *git_dir, const char *real_git_dir, · line 2364

Quoted 3 lines
setup.c +1 -2
-	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);

Overall, I like that this repostory format configuration is under repository_format_configure(). The validate_*() functions names confused me slightly initially because I assumed they were only validating, but they also configure the repo format. Looking good :)

-Justin

#6 lore raw

Re: [PATCH 3/5] setup: merge configuration of repository formats

Quoted 9 lines · Justin Tobler

setup.c int init_db(const char *git_dir, const char *real_git_dir, · line 2364

Quoted 3 lines · Patrick Steinhardt
setup.c +1 -2
-	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);

Overall, I like that this repostory format configuration is under repository_format_configure(). The validate_*() functions names confused me slightly initially because I assumed they were only validating, but they also configure the repo format. Looking good :)

Yeah, agreed. We _did_ validate whether the new configuration makes sense, but that wasn't really the main thing those functions did.

Patrick

#7 lore raw

PATCH 4/5 setup: make object format configurable via config

In reply to Patrick Steinhardt

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.

---
 Documentation/config/init.txt |  5 ++++
 setup.c                       | 40 ++++++++++++++++++++++++++++
 t/t0001-init.sh               | 50 +++++++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+)
Documentation/config/init.txt +5 -0
index af03acdbcb..d6f8b6e61b 100644
@@ -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.
setup.c +40 -0
index 5dfcdc99dd..770ad1393f 100644
@@ -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);
 
t/t0001-init.sh +50 -0

56 lines not on this page · raw message

Signature · 1 line
2.46.0.46.g406f326d27.dirty
#8 lore raw

Re: [PATCH 4/5] setup: make object format configurable via config

Quoted 11 lines · Patrick Steinhardt

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.

Adding an option to the Git config to allow the default hash algorithm and reference format to be configured makes sense. In my opinion it is much more ergonomic.

Quoted 58 lines
---
 Documentation/config/init.txt |  5 ++++
 setup.c                       | 40 ++++++++++++++++++++++++++++
 t/t0001-init.sh               | 50 +++++++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+)
Documentation/config/init.txt +5 -0
index af03acdbcb..d6f8b6e61b 100644
@@ -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.
setup.c +28 -0
index 5dfcdc99dd..770ad1393f 100644
@@ -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,

setup.c static void separate_git_dir(const char *git_dir, const char *git_link) · line 2325

Quoted 10 lines
setup.c +8 -0
 					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,
+	};

As these format configurations only make sense to be included in the global config, the search radius is limited accordingly.

setup.c static void repository_format_configure(struct repository_format *repo_fmt, · line 2346

Quoted 13 lines
setup.c +4 -0
 	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;

The environment variable takes higher precedence than the Git config. Makes sense to allow the env to override Git cofiguration.

The way this patch is setup should make it easy to extend for a default reference format config. Very nice.

-Justin

#9 lore raw

PATCH 5/5 setup: make ref storage format configurable via config

In reply to Patrick Steinhardt

Similar as 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.

---
 Documentation/config/init.txt |  5 ++++
 setup.c                       | 14 +++++++++++
 t/t0001-init.sh               | 44 +++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)
Documentation/config/init.txt +5 -0
index d6f8b6e61b..9be97bcac8 100644
@@ -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 object 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.
setup.c +14 -0
index 770ad1393f..dd2251f655 100644
@@ -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);
 }
t/t0001-init.sh +44 -0

62 lines not on this page · raw message

Signature · 1 line
2.46.0.46.g406f326d27.dirty
#10 lore raw

Re: [PATCH 5/5] setup: make ref storage format configurable via config

Quoted 1 line · Patrick Steinhardt

Similar as the preceding commit, introduce a new "init.defaultRefFormat"

s/as/to/

Quoted 15 lines

config that allows the user to globally set the ref storage format used by newly created repositories.

---
 Documentation/config/init.txt |  5 ++++
 setup.c                       | 14 +++++++++++
 t/t0001-init.sh               | 44 +++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)
Documentation/config/init.txt +0 -0
index d6f8b6e61b..9be97bcac8 100644

Documentation/config/init.txt endif::[] · line 17

Quoted 5 lines
Documentation/config/init.txt +2 -0
@@ -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 object format for new repositories. See

I think you meant to say "default reference format" here. :)

Documentation/config/init.txt endif::[] · line 20

Quoted 3 lines
Documentation/config/init.txt +3 -0
+	`--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.

[snip]

The remainder of this patch extends from the previous patch to add support for a configurable default reference format set in a Git config. Nicely done and looks good to me.

-Justin

#11 lore raw
In reply to Patrick Steinhardt
Quoted 17 lines · Patrick Steinhardt

Hi,

to set up the default object and ref storage formats, users have to set up some environment variables. This is somewhat unwieldy and not really in line with how a user typically expects to configure Git, namely by using the config system. It makes it harder than necessary to globally default to the different formats and requires the user to munge with files like .profile to persist that setting. Needless to say, this is a bit of an awkward user experience.

This patch series thus introduces two new configs to set the default object hash and ref storage format for newly created repositories. Like this, folks can simply use the global- or system-level config to adapt to their needs. This also has the advantage of giving them the ability to adapt the default formats via guarded includes, such that e.g. repos in some filesystem hierarchy use format A, whereas others use format B.

Quoted 7 lines

This comes from a discussion with Sebastian (Cc'd) at the Git User Group in Berlin yesterday.

Thanks!

Patrick

It's a good idea to make the user could set up the default object and ref storage formats by "git-config(1)". I have read all the patches, from my perspective, there is no major problems.

But I wanna ask a question here about the following code snippet:

  if (repo_fmt->version >= 0 && hash !=  GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
          die(_("..."));
  else if (hash != GIT_HASH_UNKNOWN)
          repo_fmt->hash_algo = hash;
  else if (env) {
          ...
          repo_fmt->hash_algo = env_algo;
  } else if (cfg.hash != GIT_HASH_UNKNOWN) {
          repo_fmt->hash_algo = cfg.hash;
  }

It's obvious that the precedence of "hash" is the top. We need to make sure when users execute "git init --object-format=sha256" command, this explicit info should be considered at the top. However, in the current design, the precedence of the environment variable is higher than the "git-config(1)".

If the user uses the following command:

  $ export GIT_DEFAULT_HASH_ENVIRONMENT=sha1
  $ git -c init.defaultObjectFormat=sha256 repo

The repo would be initialized with the sha1 algorithm. I think we should think carefully which precedence should be higher. I cannot give an answer here. I am not familiar with the whole database and do not the concern. But from my own perspective, I think the precedence of the config should be higher than the environment variable. This is a new feature, the people who would like to use it, they will never use environment variable and we should ignore the functionality of the environment variable. But for people who do not know this feature, they will continue to use the environment variable and they will never be influenced by the configs.

Thanks,
Jialuo

#12 lore raw
Quoted 5 lines · shejialuo

If the user uses the following command:

  $ export GIT_DEFAULT_HASH_ENVIRONMENT=sha1
  $ git -c init.defaultObjectFormat=sha256 repo
Quoted 10 lines

The repo would be initialized with the sha1 algorithm. I think we should think carefully which precedence should be higher. I cannot give an answer here. I am not familiar with the whole database and do not the concern. But from my own perspective, I think the precedence of the config should be higher than the environment variable. This is a new feature, the people who would like to use it, they will never use environment variable and we should ignore the functionality of the environment variable. But for people who do not know this feature, they will continue to use the environment variable and they will never be influenced by the configs.

The standard behaviour we have with other environment variables is that they override the config, such as with GIT_SSH_COMMAND and GIT_SSH_VARIANT. The reason is that the config in this case is usually per-user or per-system, but it's very common to override settings on an ephemeral basis with the environment.

Think of the case where you have a language package manager that's creating a repository or fetching from one. You can override on the command line with GIT_DEFAULT_HASH or GIT_SSH_VARIANT, but you can't control the actual git invocation, since it's baked into the package manager. Or if you're writing a test and need all of the Git commands to use a particular setting, then it's easy to simply set the environment once and forget about it. (This is what we do at $DAYJOB to implement SHA-256 test modes for our repos: the test script sets GIT_DEFAULT_HASH and all of our test repos magically use the right setting.)

Signature · 2 lines
brian m. carlson (they/them or he/him)
Toronto, Ontario, CA
#13 lore raw
Quoted 17 lines · brian m. carlson
Quoted 5 lines · shejialuo

If the user uses the following command:

  $ export GIT_DEFAULT_HASH_ENVIRONMENT=sha1
  $ git -c init.defaultObjectFormat=sha256 repo
Quoted 10 lines

The repo would be initialized with the sha1 algorithm. I think we should think carefully which precedence should be higher. I cannot give an answer here. I am not familiar with the whole database and do not the concern. But from my own perspective, I think the precedence of the config should be higher than the environment variable. This is a new feature, the people who would like to use it, they will never use environment variable and we should ignore the functionality of the environment variable. But for people who do not know this feature, they will continue to use the environment variable and they will never be influenced by the configs.

Quoted 5 lines

The standard behaviour we have with other environment variables is that they override the config, such as with GIT_SSH_COMMAND and GIT_SSH_VARIANT. The reason is that the config in this case is usually per-user or per-system, but it's very common to override settings on an ephemeral basis with the environment.

Right. It is good that somebody can give a clear answer when a new person says they cannot and then give an answer that contradicts with an established practice ;-).

#14 lore raw
Quoted 25 lines · Junio C Hamano
Quoted 17 lines · brian m. carlson
Quoted 5 lines · shejialuo

If the user uses the following command:

  $ export GIT_DEFAULT_HASH_ENVIRONMENT=sha1
  $ git -c init.defaultObjectFormat=sha256 repo
Quoted 10 lines

The repo would be initialized with the sha1 algorithm. I think we should think carefully which precedence should be higher. I cannot give an answer here. I am not familiar with the whole database and do not the concern. But from my own perspective, I think the precedence of the config should be higher than the environment variable. This is a new feature, the people who would like to use it, they will never use environment variable and we should ignore the functionality of the environment variable. But for people who do not know this feature, they will continue to use the environment variable and they will never be influenced by the configs.

Quoted 5 lines

The standard behaviour we have with other environment variables is that they override the config, such as with GIT_SSH_COMMAND and GIT_SSH_VARIANT. The reason is that the config in this case is usually per-user or per-system, but it's very common to override settings on an ephemeral basis with the environment.

Quoted 3 lines

Right. It is good that somebody can give a clear answer when a new person says they cannot and then give an answer that contradicts with an established practice ;-).

Yeah, thanks for putting it so clearly. I'll also put a variant of this into the commit message to clarify.

Patrick

#15 lore raw
In reply to Patrick Steinhardt
Quoted 10 lines · Patrick Steinhardt

Hi,

to set up the default object and ref storage formats, users have to set up some environment variables. This is somewhat unwieldy and not really in line with how a user typically expects to configure Git, namely by using the config system. It makes it harder than necessary to globally default to the different formats and requires the user to munge with files like .profile to persist that setting. Needless to say, this is a bit of an awkward user experience.

Quoted 6 lines

This patch series thus introduces two new configs to set the default object hash and ref storage format for newly created repositories. Like this, folks can simply use the global- or system-level config to adapt to their needs. This also has the advantage of giving them the ability to adapt the default formats via guarded includes, such that e.g. repos in some filesystem hierarchy use format A, whereas others use format B.

I like the idea of this series, which I think is a much nicer way to set these defaults and can also correctly apply to existing terminal windows. As you mentioned, this also allows different configurations per directory, and it makes it easier if you need to have a system-wide policy for whatever reason.

I've taken a look at the patches and they seem fine to me. Thanks for sending them.

Signature · 2 lines
brian m. carlson (they/them or he/him)
Toronto, Ontario, CA
#16 lore raw

v2 cover Introduce configs for default repo format

In reply to Patrick Steinhardt

Hi,

this is the second version of my patch series that introduces two new config settings init.defaultRefFormat and init.defaultObjectFormat to make the default formats configurable without environment variables.

Changes compared to v1:

- Extend commit message to mention that we also move repo_set_ref_storage_format() and repo_set_hash_algo() into repository_format_configure().

- Extend commit message to explain precedence.

- Fix a grammar issue.

- Fix a copy/paste error in the documentation of init.defaultRefFormat.

Thanks!

Patrick

Patrick Steinhardt (5):
t0001: exercise initialization with ref formats more thoroughly
t0001: delete repositories when object format tests finish
setup: merge configuration of repository formats
setup: make object format configurable via config
setup: make ref storage format configurable via config

 Documentation/config/init.txt |  10 +++
 setup.c                       | 101 ++++++++++++++++-------
 t/t0001-init.sh               | 145 +++++++++++++++++++++++++++++++---
 3 files changed, 216 insertions(+), 40 deletions(-)

Range-diff against v1:

1:  0d3844db32 = 1:  0d3844db32 t0001: exercise initialization with ref formats more thoroughly
2:  6b0cefef6a = 2:  6b0cefef6a t0001: delete repositories when object format tests finish
3:  16f52b75d8 ! 3:  ce0fad88bb setup: merge configuration of repository formats

@@ Commit message
or read the config multiple times. Both of these options feel a bit unwieldy.

- Merge the code into a new 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.
+ 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.

4: e1cdaf0f0e ! 4: 8508662062 setup: make object format configurable via config
@@ Commit message
"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.
+

## Documentation/config/init.txt ##
5: a0417b7d1a ! 5: b68a841450 setup: make ref storage format configurable via config
@@ Metadata
## Commit message ##
setup: make ref storage format configurable via config

- Similar as the preceding commit, introduce a new "init.defaultRefFormat"
+ 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.

@@ Documentation/config/init.txt: endif::[]
and the GIT_DEFAULT_HASH environment variable take precedence over this config.
+init.defaultRefFormat::
-+ Allows overriding the default object 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.
++ 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.

## setup.c ##
@@ setup.c: static void separate_git_dir(const char *git_dir, const char *git_link)

Signature · 1 line
2.46.0.46.g406f326d27.dirty
#17 lore raw

PATCH v2 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.

---
 t/t0001-init.sh | 48 +++++++++++++++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 13 deletions(-)
t/t0001-init.sh +35 -13
index 49e9bf77c6..2093f5c1ee 100755
@@ -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)" '
Signature · 1 line
2.46.0.46.g406f326d27.dirty
#18 lore raw

PATCH v2 2/5 t0001: delete repositories when object format tests finish

In reply to Patrick Steinhardt

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.

---
 t/t0001-init.sh | 3 +++
 1 file changed, 3 insertions(+)
t/t0001-init.sh +3 -0
index 2093f5c1ee..795408e16c 100755
@@ -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
Signature · 1 line
2.46.0.46.g406f326d27.dirty
#19 lore raw

PATCH v2 3/5 setup: merge configuration of repository formats

In reply to Patrick Steinhardt

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.

---
 setup.c | 47 ++++++++++++++++++++---------------------------
 1 file changed, 20 insertions(+), 27 deletions(-)
setup.c +20 -27
index d458edcc02..5dfcdc99dd 100644
@@ -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
Signature · 1 line
2.46.0.46.g406f326d27.dirty
#20 lore raw

PATCH v2 4/5 setup: make object format configurable via config

In reply to Patrick Steinhardt

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.

---
 Documentation/config/init.txt |  5 ++++
 setup.c                       | 40 ++++++++++++++++++++++++++++
 t/t0001-init.sh               | 50 +++++++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+)
Documentation/config/init.txt +5 -0
index af03acdbcb..d6f8b6e61b 100644
@@ -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.
setup.c +40 -0
index 5dfcdc99dd..770ad1393f 100644
@@ -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);
 
t/t0001-init.sh +50 -0

56 lines not on this page · raw message

Signature · 1 line
2.46.0.46.g406f326d27.dirty
#21 lore raw

Re: [PATCH v2 4/5] setup: make object format configurable via config

Quoted 11 lines · Patrick Steinhardt

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.

Thanks for documenting this, as the reason for deciding the precedence between environment and configuration is no longer as strong as it used to be.

Before the "git -c var=val" happened, the only sensible preference order was to allow environment to override a configured default by doing "VAR=val git cmd ...". In other words, the order used to be something you could safely forget and still infer the right order with logic alone.

But with "git -c var=val", even if a hypothetical Git allowed configuration to override environment, a user could still override them with "-c var=val", so "environment trumps configuration" is no longer "because there is no sensible workaround if it were the other way around", but merely "because that used to be the only logical way".

#22 lore raw

PATCH v2 5/5 setup: make ref storage format configurable via config

In reply to Patrick Steinhardt

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.

---
 Documentation/config/init.txt |  5 ++++
 setup.c                       | 14 +++++++++++
 t/t0001-init.sh               | 44 +++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)
Documentation/config/init.txt +5 -0
index d6f8b6e61b..e45b2a8121 100644
@@ -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.
setup.c +14 -0
index 770ad1393f..dd2251f655 100644
@@ -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);
 }
t/t0001-init.sh +44 -0

62 lines not on this page · raw message

Signature · 1 line
2.46.0.46.g406f326d27.dirty
#23 lore raw
In reply to Patrick Steinhardt
Quoted 14 lines · Patrick Steinhardt

Hi,

this is the second version of my patch series that introduces two new config settings init.defaultRefFormat and init.defaultObjectFormat to make the default formats configurable without environment variables.

Changes compared to v1:

- Extend commit message to mention that we also move repo_set_ref_storage_format() and repo_set_hash_algo() into repository_format_configure().

- Extend commit message to explain precedence.

Quoted 8 lines

- Fix a grammar issue.

- Fix a copy/paste error in the documentation of init.defaultRefFormat.

Thanks!

Patrick

All of my comments have been addressed with this version of the patch series. This version looks good to me. Thanks

-Justin