git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] setup: default hash algo to GIT_HASH_DEFAULT

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 9, 2026, 02:21 UTC
Message-ID
<xmqqzevi218o.fsf@gitster.g>
In-Reply-To
<xmqqik274201.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> writes:
> I wonder what the "git init" command does wrt the reference storage
> format (Patrick CC'ed for this)?  I am sure we do not convert from
> reffiles to reftable on the fly at "git init" time.

I looked at setup.c:repository_format_configure() and found why your patch regresses, as well as a latent bug in the existing code.

First, REPOSITORY_FORMAT_INIT sets .hash_algo = GIT_HASH_DEFAULT, but read_repository_format() forces it to GIT_HASH_SHA1_LEGACY when version == -1 (no config yet), which is why new repositories kept getting SHA-1.

Second, unlike the ref-storage block right below it, which checks "else if (repo_fmt->version >= 0)" once between the explicit overrides and all default sources, the hash-algo block guards only the GIT_DEFAULT_HASH branch (repo_fmt->version < 0), leaving "else if (cfg.hash != GIT_HASH_UNKNOWN)" unguarded.

So init.defaultObjectFormat in ~/.gitconfig already corrupts an existing repository on re-init, and an unguarded "else" for GIT_HASH_DEFAULT falls into the same trap even without config.

Instead of starting the if/else if cascade like this:
	if (repo_fmt->version >= 0 &&
	    hash != GIT_HASH_UNKNOWN &&
	    hash != repo_fmt->hash_algo)
		die(_("attempt to reinitialize repository with different hash"));
we should have started it like so:
	if (repo_fmt->version >= 0) {
		if (hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
			die(_("attempt to reinitialize repository with different hash"));
		/* otherwise happy */
	} else if ...

so that all the "set a new value from different sources like command line, environment, ~/.gitconfig, or the built-in default" would happen only when (repo_fmt->version < 0), probably?

Previous: Junio C HamanoNext: Patrick Steinhardt
Message 7 of 11 in “Default object format for new repositories in Git v3”
  1. Adam DinwoodieNov 6, 2026
  2. brian m. carlsonNov 6, 2026
  3. setup: default hash algo to GIT_HASH_DEFAULTbrian m. carlson, Nov 8, 2026
  4. Junio C HamanoNov 8, 2026
  5. brian m. carlsonNov 8, 2026
  6. Junio C HamanoNov 8, 2026
  7. Junio C HamanoNov 9, 2026
  8. Patrick SteinhardtNov 9, 2026
  9. setup: fix hash algorithm detectionbrian m. carlson, Nov 13, 2026
  10. Junio C HamanoNov 14, 2026
  11. Patrick SteinhardtNov 16, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.