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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 24/08/15 10:00AM, Patrick Steinhardt wrote:
> 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.

> 
> Signed-off-by: Patrick Steinhardt <ps@xxxxxx>
> ---
>  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,
> +	};

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

>  	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




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux