Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "brian m. carlson" <sandals@crustytoothpaste.net>
Cc: <git@vger.kernel.org>,  Patrick Steinhardt <ps@pks.im>,
	 Adam Dinwoodie <adam@dinwoodie.org>
Subject: Re: [PATCH v4] setup: fix hash algorithm detection
Date: Mon, 23 Nov 2026 08:14:02 -0800	[thread overview]
Message-ID: <xmqqmrqzbk51.fsf@gitster.g> (raw)
In-Reply-To: <20261123011403.720209-1-sandals@crustytoothpaste.net> (brian m. carlson's message of "Mon, 23 Nov 2026 01:14:03 +0000")

"brian m. carlson" <sandals@crustytoothpaste.net> writes:

> Our current hash algorithm selection code has a few different problems.

> First, we do not actually default to GIT_HASH_DEFAULT because, due to a
> bug, our code has never considered GIT_HASH_DEFAULT as a default for git
> init and if an algorithm is not specified in the config file, we assume
> that the repository is SHA-1.

REPOSITORY_FORMAT_INIT does set .hash_algo to GIT_HASH_DEFAULT, but
create_repository() calls read_repository_format() early, and that
overwrites .hash_algo to GIT_HASH_SHA1_LEGACY when version is -1 (e.g.,
a new repository).  So indeed, DEFAULT is not honoured in the code path.

> diff --git a/setup.c b/setup.c
> index cedc031c54..c2a56b997a 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -2888,24 +2888,29 @@ static void repository_format_configure(struct repository_format *repo_fmt,
>  	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.
> -	 * Otherwise, if the user has specified one on the command line, use it.
> +	 * If the user specified a hash algorithm on the command line, then use
> +	 * that.  Otherwise, if the repository exists, use that algorithm; if
> +	 * not, choose the environment variable or config option in that order,
> +	 * and then fall back to the default.
> +	 *
> +	 * If the repository is already initialized and we pick a different
> +	 * algorithm than the current one, we will reject that below.
>  	 */
>  	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)
> -		repo_fmt->hash_algo = hash;
> -	else if (env) {
> +	if (hash != GIT_HASH_UNKNOWN) {
> +		/* nothing to do */
> +	} else if (repo_fmt->version >= 0 &&
> +		   repo_fmt->hash_algo != GIT_HASH_UNKNOWN) {
> +		hash = repo_fmt->hash_algo;

I was comparing what is done to hash (here), ref-format (below), and
obj-format (further down), and noticed that the latter two only check
"else if (repo_fmt->version >= 0)" at the corresponding place.  I was
confused by the extra check to ensure '.hash_algo' here is not
UNKNOWN.

I know the proposed commit log message did say "should not occur", but
the code makes readers wonder why the impossible case is sitting
there, and it will be even more so after the code becomes part of our
history and somebody is reading this code in a few months.  They do
not always go back to the log message.

Before control reaches here, create_repository() would have called
read_and_verify_repository_format(), which in turn would have called
read_repository_format(), which in turn uses check_repo_format().

check_repo_format() is the only place core.repositoryformatversion is
read and the '.version' member is set to a value to give us
"repo_fmt->version >= 0" (after REPOSITORY_FORMAT_INIT initializes the
member to -1).  The function also parses "extensions." and calls
handle_extension().  It rejects GIT_HASH_UNKNOWN, so the '.hash_algo'
member cannot be set to GIT_HASH_UNKNOWN by it.  Again, aside from the
initialization done in read_repository_format() to
GIT_HASH_SHA1_LEGACY, this is the only place the '.hash_algo' member
is assigned to.

Therefore, if the '.version' member is >= 0, the '.hash_algo' member
can never be GIT_HASH_UNKNOWN, so I think it is pointless (and
confusing) to have the check to ensure '.hash_algo' is not UNKNOWN
here.  Simplifying the condition to

     } else if (repo_fmt->version >= 0) {

may save readers from the confusion I had.  And that is what the
ref_storage_format and object_storage_format sides of the code do in
the same function, I think.

> +	} else if (env) {
>  		int env_algo = hash_algo_by_name(env);
>  		if (env_algo == GIT_HASH_UNKNOWN)
>  			die(_("unknown hash algorithm '%s'"), env);
> -		if (repo_fmt->version < 0 ||
> -		    repo_fmt->hash_algo == GIT_HASH_UNKNOWN)
> -			repo_fmt->hash_algo = env_algo;
> +		hash = env_algo;
>  	} else if (cfg.hash != GIT_HASH_UNKNOWN) {
> -		repo_fmt->hash_algo = cfg.hash;
> +		hash = cfg.hash;
> +	} else {
> +		hash = GIT_HASH_DEFAULT;
>  	}

  parent reply	other threads:[~2026-11-23 16:14 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06 16:50 Default object format for new repositories in Git v3 Adam Dinwoodie
2026-11-06 20:46 ` brian m. carlson
2026-11-08  1:16 ` [PATCH] setup: default hash algo to GIT_HASH_DEFAULT brian m. carlson
2026-11-08  4:42   ` Junio C Hamano
2026-11-08 14:38     ` brian m. carlson
2026-11-08 18:22   ` Junio C Hamano
2026-11-09  2:21     ` Junio C Hamano
2026-11-09  9:54       ` Patrick Steinhardt
2026-11-13 22:17   ` [PATCH v2] setup: fix hash algorithm detection brian m. carlson
2026-11-14  0:16     ` Junio C Hamano
2026-11-16  6:22     ` Patrick Steinhardt
2026-11-19  1:06   ` [PATCH v3] " brian m. carlson
2026-11-19  9:52     ` Patrick Steinhardt
2026-11-19 16:26     ` Junio C Hamano
2026-11-19 16:38     ` Junio C Hamano
2026-11-19 22:20       ` brian m. carlson
2026-11-19 23:06         ` Junio C Hamano
2026-11-23  1:14   ` [PATCH v4] " brian m. carlson
2026-11-23  2:29     ` Junio C Hamano
2026-11-23 16:14     ` Junio C Hamano [this message]
2026-11-23 16:25     ` Junio C Hamano
2026-11-23 22:13       ` brian m. carlson
2026-11-24  0:01         ` Junio C Hamano
2026-11-24 22:59   ` [PATCH v5] " brian m. carlson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqmrqzbk51.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=adam@dinwoodie.org \
    --cc=git@vger.kernel.org \
    --cc=ps@pks.im \
    --cc=sandals@crustytoothpaste.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox