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

Re: [PATCH v2 3/8] setup: deduplicate logic to apply repository format

From
Karthik Nayak <karthik.188@gmail.com>
Date
Jun 3, 2026, 12:43 UTC
Message-ID
<CAOLa=ZSnDz1+C8y7ozFDdv68vqLFk-E+FsXhAnhwbm2D6a1Fng@mail.gmail.com>
In-Reply-To
<20260526-b4-pks-setup-centralize-odb-creation-v2-3-2fa5b385c13e@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 29 quoted lines
> After having discovered the repository format we then apply it to the
> repository so that it knows to use the proper repository extensions. The
> logic to apply the format is duplicated across three callsites, which
> makes it rather painfull to add new extensions.
>
> Introduce a new function `apply_repository_format()` that takes a repo
> and applies a given format to it and adapt all callsites to use it.
> While at it, rename `check_repository_format()` to clarify that it
> doesn't only _check_ the format, but that it also applies it.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  repository.c | 31 +++++++-------------
>  setup.c      | 93 ++++++++++++++++++++++++++++++++----------------------------
>  setup.h      |  9 ++++++
>  3 files changed, 70 insertions(+), 63 deletions(-)
>
> diff --git a/repository.c b/repository.c
> index db57b8308b..58a13f7c4f 100644
> --- a/repository.c
> +++ b/repository.c
> @@ -262,8 +262,8 @@ void repo_set_worktree(struct repository *repo, const char *path)
>  	trace2_def_repo(repo);
>  }
>
> -static int read_and_verify_repository_format(struct repository_format *format,
> -					     const char *commondir)
> +static int read_repository_format_from_commondir(struct repository_format *format,
> +						 const char *commondir)
Nit: The commit explicitly calls out one rename, but this one wasn't.
Show 12 quoted lines
>  {
>  	int ret = 0;
>  	struct strbuf sb = STRBUF_INIT;
> @@ -272,11 +272,6 @@ static int read_and_verify_repository_format(struct repository_format *format,
>  	read_repository_format(format, sb.buf);
>  	strbuf_reset(&sb);
>
> -	if (verify_repository_format(format, &sb) < 0) {
> -		warning("%s", sb.buf);
> -		ret = -1;
> -	}
> -

So we remove this, so that the callee would independently verify the format I assume.

Edit: seems like we call verify_repository_format() within
apply_repository_format() and the latter is called by the callee.
Show 30 quoted lines
>  	strbuf_release(&sb);
>  	return ret;
>  }
> @@ -290,6 +285,8 @@ int repo_init(struct repository *repo,
>  	      const char *worktree)
>  {
>  	struct repository_format format = REPOSITORY_FORMAT_INIT;
> +	struct strbuf err = STRBUF_INIT;
> +
>  	memset(repo, 0, sizeof(*repo));
>
>  	initialize_repository(repo);
> @@ -297,21 +294,13 @@ int repo_init(struct repository *repo,
>  	if (repo_init_gitdir(repo, gitdir))
>  		goto error;
>
> -	if (read_and_verify_repository_format(&format, repo->commondir))
> +	if (read_repository_format_from_commondir(&format, repo->commondir))
>  		goto error;
>
> -	repo_set_hash_algo(repo, format.hash_algo);
> -	repo_set_compat_hash_algo(repo, format.compat_hash_algo);
> -	repo_set_ref_storage_format(repo, format.ref_storage_format,
> -				    format.ref_storage_payload);
> -	repo->repository_format_worktree_config = format.worktree_config;
> -	repo->repository_format_relative_worktrees = format.relative_worktrees;
> -	repo->repository_format_precious_objects = format.precious_objects;
> -	repo->repository_format_submodule_path_cfg = format.submodule_path_cfg;
> -
> -	/* take ownership of format.partial_clone */

I see that we now do an xstrdup for format.partial_clone, meaning we have our own memory segment to care about. Do we have to worry about format.partial_clone not being free'd?

Show 9 quoted lines
> -	repo->repository_format_partial_clone = format.partial_clone;
> -	format.partial_clone = NULL;
> +	if (apply_repository_format(repo, &format, &err) < 0) {
> +		warning("%s", err.buf);
> +		goto error;
> +	}
>
>  	if (worktree)
>  		repo_set_worktree(repo, worktree);
[snip]
Show 13 quoted lines
> diff --git a/setup.h b/setup.h
> index 9409326fe4..5ed92f53fa 100644
> --- a/setup.h
> +++ b/setup.h
> @@ -221,6 +221,15 @@ void clear_repository_format(struct repository_format *format);
>  int verify_repository_format(const struct repository_format *format,
>  			     struct strbuf *err);
>
> +/*
> + * Apply the given repository format to the repo. This initializes extensions
> + * and basic data structures required for normal operation. Returns 0 on
> + * success, a negative error code otherwise.
> + */
Nit: perhaps we should also mention that we verify the format?
Show 10 quoted lines
> +int apply_repository_format(struct repository *repo,
> +			    const struct repository_format *format,
> +			    struct strbuf *err);
> +
>  const char *get_template_dir(const char *option_template);
>
>  #define INIT_DB_QUIET      (1 << 0)
>
> --
> 2.54.0.926.g75ba10bac6.dirty
p
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 6 of 17 in “setup: centralize object database creation”
  1. 0/8 setup: centralize object database creationPatrick Steinhardt, May 26, 2026
  2. 1/8 t0001: plug test gaps for git-init(1) with GIT_OBJECT_DIRECTORYPatrick Steinhardt, May 26, 2026
  3. Karthik NayakJun 3, 2026
  4. 2/8 setup: drop `setup_git_env()`Patrick Steinhardt, May 26, 2026
  5. 3/8 setup: deduplicate logic to apply repository formatPatrick Steinhardt, May 26, 2026
  6. Karthik NayakJun 3, 2026
  7. Patrick SteinhardtJun 4, 2026
  8. 5/8 setup: stop creating the object database in `setup_git_env()`Patrick Steinhardt, May 26, 2026
  9. Karthik NayakJun 3, 2026
  10. 4/8 repository: stop initializing the object database in `repo_set_gitdir()`Patrick Steinhardt, May 26, 2026
  11. Karthik NayakJun 3, 2026
  12. Patrick SteinhardtJun 4, 2026
  13. 6/8 setup: stop initializing object database without repositoryPatrick Steinhardt, May 26, 2026
  14. 7/8 repository: stop reading loose object map twice on repo initPatrick Steinhardt, May 26, 2026
  15. 8/8 setup: construct object database in `apply_repository_format()`Patrick Steinhardt, May 26, 2026
  16. Karthik NayakJun 3, 2026
  17. Patrick SteinhardtJun 4, 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.