[PATCH v2 00/18] setup: drop uses of `the_repository`
- From
Patrick Steinhardt <ps@pks.im>
- Date
- May 18, 2026, 09:30 UTC
- Message-ID
- <20260518-pks-setup-wo-the-repository-v2-0-6933c0f1d568@pks.im>
- In-Reply-To
- <20260420-pks-setup-wo-the-repository-v1-0-f4a81c4988e8@pks.im>
Hi,
I've had enough of "setup.c" and its complexities, so I finally decided to take the bullet and start refactoring this subsystem. This here is the first out of the following three steps:
1. Drop all uses of `the_repository`. This doesn't yet allow us to get
rid of `USE_THE_REPOSITORY_VARIABLE`. 2. Convert a couple of global variables and drop
`is_bare_repository_cfg`, which then allows us to drop
`USE_THE_REPOSITORY_VARIABLE`. 3. Refactor the subsystem a bit so that we stop intermixing repository
discovery and repository initialization. This is my original
motivation as I want to get rid of `odb_prepare_alternates()`, but
due to the way we initialize the repository it has proven to be
extremely tedious.Most of the patches in this series here are rather mechanical. There's only a handful of patches that warrant more attention:
- 2/18: setup: stop using `the_repository` in `is_inside_worktree()` - 3/18: setup: stop using `the_repository` in `is_inside_git_dir()` - 9/18: setup: stop using `the_repository` in `setup_work_tree()` - 10/18: setup: stop using `the_repository` in `set_git_work_tree()`
Those patches don't only mechanical move stuff around, but also change some logic to make it work.
Changes in v2:
- Drop some static variables.
- Rebase on top of origin/master at 59ff4886a5 (Merge branch
'ps/clang-w-glibc-2.43-and-_Generic', 2026-05-13). This resolves a
single conflict, but more importantly the rebase fixes CI.
- Link to v1: https://patch.msgid.link/20260420-pks-setup-wo-the-repository-v1-0-f4a81c4988e8@pks.imChanges in v1 (relative to the botched-up [1]): - Remove static `initialized` variable in `setup_work_tree()`. - Use enum values to initialize fields. - Fix up a comment. - Link to v1: https://lore.kernel.org/all/20260330-pks-setup-wo-the-repository-v1-0-0d2e822837aa@pks.im/
Thanks!
Patrick
[1]: <20260330-pks-setup-wo-the-repository-v1-0-0d2e822837aa@pks.im>
---
Patrick Steinhardt (18):
setup: replace use of `the_repository` in static functions
setup: stop using `the_repository` in `is_inside_worktree()`
setup: stop using `the_repository` in `is_inside_git_dir()`
setup: stop using `the_repository` in `prefix_path()`
setup: stop using `the_repository` in `path_inside_repo()`
setup: stop using `the_repository` in `verify_filename()`
setup: stop using `the_repository` in `verify_non_filename()`
setup: stop using `the_repository` in `enter_repo()`
setup: stop using `the_repository` in `setup_work_tree()`
setup: stop using `the_repository` in `set_git_work_tree()`
setup: stop using `the_repository` in `setup_git_env()`
setup: stop using `the_repository` in `setup_git_directory_gently()`
setup: stop using `the_repository` in `setup_git_directory()`
setup: stop using `the_repository` in `upgrade_repository_format()`
setup: stop using `the_repository` in `check_repository_format()`
setup: stop using `the_repository` in `initialize_repository_version()`
setup: stop using `the_repository` in `create_reference_database()`
setup: stop using `the_repository` in `init_db()`archive.c | 2 +- blame.c | 2 +- builtin/blame.c | 2 +- builtin/check-attr.c | 4 +- builtin/check-ref-format.c | 5 +- builtin/checkout-index.c | 4 +- builtin/checkout.c | 2 +- builtin/clone.c | 12 +- builtin/describe.c | 2 +- builtin/diff-index.c | 2 +- builtin/diff.c | 10 +- builtin/difftool.c | 2 +- builtin/grep.c | 8 +- builtin/hash-object.c | 4 +- builtin/help.c | 2 +- builtin/init-db.c | 8 +- builtin/ls-files.c | 4 +- builtin/merge-file.c | 2 +- builtin/mv.c | 5 +- builtin/read-tree.c | 2 +- builtin/receive-pack.c | 2 +- builtin/reset.c | 6 +- builtin/rev-parse.c | 14 +- builtin/rm.c | 2 +- builtin/sparse-checkout.c | 19 +- builtin/stripspace.c | 2 +- builtin/submodule--helper.c | 2 +- builtin/update-index.c | 16 +- builtin/upload-archive.c | 2 +- builtin/upload-pack.c | 2 +- daemon.c | 4 +- environment.h | 2 - git.c | 10 +- http-backend.c | 2 +- http-fetch.c | 2 +- http-push.c | 2 +- imap-send.c | 2 +- line-log.c | 2 +- list-objects-filter-options.c | 2 +- object-name.c | 4 +- pathspec.c | 2 +- refs.c | 2 +- remote-curl.c | 4 +- repository.h | 4 +- revision.c | 6 +- scalar.c | 4 +- setup.c | 451 ++++++++++++++------------- setup.h | 43 ++- submodule.c | 2 +- t/helper/test-advise.c | 2 +- t/helper/test-bitmap.c | 2 +- t/helper/test-bloom.c | 2 +- t/helper/test-cache-tree.c | 2 +- t/helper/test-config.c | 2 +- t/helper/test-dump-cache-tree.c | 2 +- t/helper/test-dump-fsmonitor.c | 2 +- t/helper/test-dump-split-index.c | 2 +- t/helper/test-dump-untracked-cache.c | 2 +- t/helper/test-find-pack.c | 2 +- t/helper/test-fsmonitor-client.c | 2 +- t/helper/test-lazy-init-name-hash.c | 2 +- t/helper/test-match-trees.c | 2 +- t/helper/test-pack-deltas.c | 2 +- t/helper/test-pack-mtimes.c | 2 +- t/helper/test-partial-clone.c | 4 +- t/helper/test-path-utils.c | 4 +- t/helper/test-path-walk.c | 2 +- t/helper/test-reach.c | 2 +- t/helper/test-read-cache.c | 2 +- t/helper/test-read-graph.c | 2 +- t/helper/test-read-midx.c | 2 +- t/helper/test-ref-store.c | 2 +- t/helper/test-revision-walking.c | 2 +- t/helper/test-scrap-cache-tree.c | 2 +- t/helper/test-serve-v2.c | 2 +- t/helper/test-submodule-config.c | 2 +- t/helper/test-submodule-nested-repo-config.c | 2 +- t/helper/test-submodule.c | 10 +- t/helper/test-subprocess.c | 6 +- t/helper/test-userdiff.c | 2 +- t/helper/test-write-cache.c | 2 +- worktree.c | 2 +- wt-status.c | 2 +- 83 files changed, 402 insertions(+), 383 deletions(-)
Range-diff versus v1:
1: 2bdd6dfead = 1: 275a0e45a3 setup: replace use of `the_repository` in static functions
2: e38c29bd25 ! 2: d748d28abe setup: stop using `the_repository` in `is_inside_worktree()`
@@ setup.c: int is_inside_git_dir(void)
- if (inside_work_tree < 0)
- inside_work_tree = is_inside_dir(repo_get_work_tree(the_repository));
- return inside_work_tree;
-+ static struct strbuf buf = STRBUF_INIT;
-+ const char *worktree = repo_get_work_tree(repo);
++ struct strbuf buf = STRBUF_INIT;
++ const char *worktree;
++ int ret;
++
++ worktree = repo_get_work_tree(repo);
+ if (!worktree)
+ return 0;
-+ return is_inside_dir(strbuf_realpath(&buf, worktree, 1));
++
++ ret = is_inside_dir(strbuf_realpath(&buf, worktree, 1));
++
++ strbuf_release(&buf);
++ return ret;
}
void setup_work_tree(void)
3: 184b1f2331 ! 3: 3f1da8a163 setup: stop using `the_repository` in `is_inside_git_dir()`
@@ setup.c: int is_nonbare_repository_dir(struct strbuf *path)
- if (inside_git_dir < 0)
- inside_git_dir = is_inside_dir(repo_get_git_dir(the_repository));
- return inside_git_dir;
-+ static struct strbuf buf = STRBUF_INIT;
-+ return is_inside_dir(strbuf_realpath(&buf, repo_get_git_dir(repo), 1));
++ struct strbuf buf = STRBUF_INIT;
++ int ret = is_inside_dir(strbuf_realpath(&buf, repo_get_git_dir(repo), 1));
++ strbuf_release(&buf);
++ return ret;
}
int is_inside_work_tree(struct repository *repo)
4: c178fb7f3d = 4: 59426f6dbc setup: stop using `the_repository` in `prefix_path()`
5: e408ba2332 = 5: aae9034d31 setup: stop using `the_repository` in `path_inside_repo()`
6: b43251adcb = 6: b8d6abd41f setup: stop using `the_repository` in `verify_filename()`
7: e489b6f84e ! 7: e6ecd9ea0d setup: stop using `the_repository` in `verify_non_filename()`
@@ builtin/reset.c: static void parse_args(struct pathspec *pathspec,
/* Otherwise we treat this as a filename */
## revision.c ##
-@@ revision.c: static int handle_dotdot_1(const char *arg, char *dotdot,
+@@ revision.c: static int handle_dotdot_1(const char *a_name, const char *b_name,
+ return -1;
if (!cant_be_filename) {
- *dotdot = '.';
-- verify_non_filename(revs->prefix, arg);
-+ verify_non_filename(the_repository, revs->prefix, arg);
- *dotdot = '\0';
+- verify_non_filename(revs->prefix, full_name);
++ verify_non_filename(the_repository, revs->prefix, full_name);
}
+ a_obj = parse_object(revs->repo, &a_oid);
@@ revision.c: static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int fl
goto out;
}
8: 54192c579e = 8: 87fe43bf7c setup: stop using `the_repository` in `enter_repo()`
9: 78ae1d0845 ! 9: 897a0ac4a6 setup: stop using `the_repository` in `setup_work_tree()`
@@ Commit message
the repository as a parameter. The injection of `the_repository` is thus
bumped one level higher, where callers now pass it in explicitly.
- Note that the function tracks bogus worktree configuration via a global
- variable. If we have bogus configuration, and if later on some caller
- tries to setup a worktree, then we'll die instead.
+ Note that the function tracks two bits of information via global
+ variables. This of course doesn't make much sense anymore now that we
+ can set up worktrees for arbitrary repositories:
- Of course, tracking this as a global variable doesn't make sense anymore
- now that we can set up worktrees for arbitrary repositories. Move the
- variable into `struct repository` instead.
+ - We track whether the worktree has already been initialized and, if
+ so, we skip the call to `chdir_notify()` and setenv(3p). It does not
+ make much sense to store this info in the repository, as we _would_
+ want to update the environment when switching between worktrees back
+ and forth.
+
+ So instead of storing this info in the repository, we drop this
+ state entirely and live with the fact that we may execute the logic
+ twice. It should ultimately be idempotent though and thus not be
+ much of a problem.
+
+ - We track whether the worktree configuration is bogus. If so, and if
+ later on some caller tries to setup the worktree, then we'll die
+ instead. This is indeed information that we can move into the
+ repository itself.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
@@ setup.c
ALLOWED_BARE_REPO_EXPLICIT = 0,
ALLOWED_BARE_REPO_ALL,
@@ setup.c: int is_inside_work_tree(struct repository *repo)
- return is_inside_dir(strbuf_realpath(&buf, worktree, 1));
+ return ret;
}
-void setup_work_tree(void)
+void setup_work_tree(struct repository *repo)
{
const char *work_tree;
- static int initialized = 0;
-@@ setup.c: void setup_work_tree(void)
- if (initialized)
- return;
+- static int initialized = 0;
+- if (initialized)
+- return;
+-
- if (work_tree_config_is_bogus)
+ if (repo->worktree_config_is_bogus)
die(_("unable to set up work tree using invalid config"));
@@ setup.c: void setup_work_tree(void)
if (!work_tree || chdir_notify(work_tree))
die(_("this operation must be run in a work tree"));
+@@ setup.c: void setup_work_tree(void)
+ */
+ if (getenv(GIT_WORK_TREE_ENVIRONMENT))
+ setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
+-
+- initialized = 1;
+ }
+
+ static void setup_original_cwd(struct repository *repo)
@@ setup.c: static const char *setup_explicit_git_dir(struct repository *repo,
if (git_work_tree_cfg) {
/* #22.2, #30 */
10: f2f0c80991 ! 10: f1ceac2491 setup: stop using `the_repository` in `set_git_work_tree()`
@@ setup.c: const char *enter_repo(struct repository *repo, const char *path, unsig
}
- git_work_tree_initialized = 1;
- repo_set_worktree(the_repository, new_work_tree);
-+ repo->worktree_initialized = 1;
++ repo->worktree_initialized = true;
+ repo_set_worktree(repo, new_work_tree);
}
11: 0c72972ba4 = 11: f7de435b86 setup: stop using `the_repository` in `setup_git_env()`
12: 7a758cf4de = 12: aea92d5f0a setup: stop using `the_repository` in `setup_git_directory_gently()`
13: 7485a9e031 = 13: 4981d7d67d setup: stop using `the_repository` in `setup_git_directory()`
14: 5397aac8e7 = 14: 046de3ad97 setup: stop using `the_repository` in `upgrade_repository_format()`
15: f713bdb472 ! 15: 905b7dbb89 setup: stop using `the_repository` in `check_repository_format()`
@@ setup.c: enum discovery_result discover_git_directory_reason(struct strbuf *comm
}
+/*
-+ * Check the repository format version in the path found in repo_get_git_dir(the_repository),
++ * Check the repository format version in the path found in repo_get_git_dir(repo),
+ * and die if it is a version we don't understand. Generally one would
+ * set_git_dir() before calling this, and use it only for "are we in a valid
+ * repo?".
16: 0e1931e40f = 16: bdf0a02c59 setup: stop using `the_repository` in `initialize_repository_version()`
17: 9cddc161a6 = 17: 74bd848268 setup: stop using `the_repository` in `create_reference_database()`
18: 996771094d = 18: 6fa2476a01 setup: stop using `the_repository` in `init_db()`--- base-commit: 59ff4886a579f4bc91e976fe18590b9ae02c7a08 change-id: 20260330-pks-setup-wo-the-repository-81e51bc55b91