worktree: pass repository to file-local functions

We have a bunch of file-local functions that use `the_repository`. Adapt them so that the repository is instead passed as a parameter so that we can get rid of this dependency. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 16, 2026 at 07:33 UTC 2cec1c92e63dec350ab044170a08d610381349b1
1 file changed +26 -21
worktree.c
+26 -21
@@ -111,27 +111,28 @@ static int is_main_worktree_bare(struct repository *repo)
111 /**
112 * get the main worktree
113 */
114 -static struct worktree *get_main_worktree(int skip_reading_head)
114 +static struct worktree *get_main_worktree(struct repository *repo,
115 + int skip_reading_head)
116 {
117 struct worktree *worktree = NULL;
118 struct strbuf worktree_path = STRBUF_INIT;
119
119 - strbuf_add_real_path(&worktree_path, repo_get_common_dir(the_repository));
120 + strbuf_add_real_path(&worktree_path, repo_get_common_dir(repo));
121 strbuf_strip_suffix(&worktree_path, "/.git");
122
123 CALLOC_ARRAY(worktree, 1);
123 - worktree->repo = the_repository;
124 + worktree->repo = repo;
125 worktree->path = strbuf_detach(&worktree_path, NULL);
126 worktree->is_current = is_current_worktree(worktree);
126 - worktree->is_bare = (the_repository->bare_cfg == 1) ||
127 - is_bare_repository(the_repository) ||
127 + worktree->is_bare = (repo->bare_cfg == 1) ||
128 + is_bare_repository(repo) ||
129 /*
130 * When in a secondary worktree we have to also verify if the main
131 * worktree is bare in $commondir/config.worktree.
132 * This check is unnecessary if we're currently in the main worktree,
133 * as prior checks already consulted all configs of the current worktree.
134 */
134 - (!worktree->is_current && is_main_worktree_bare(the_repository));
135 + (!worktree->is_current && is_main_worktree_bare(repo));
136
137 if (!skip_reading_head)
138 add_head_info(worktree);
@@ -182,7 +183,8 @@ done:
183 * retrieving worktree metadata that could be used when the worktree is known
184 * to not be in a healthy state, e.g. when creating or repairing it.
185 */
185 -static struct worktree **get_worktrees_internal(int skip_reading_head)
186 +static struct worktree **get_worktrees_internal(struct repository *repo,
187 + int skip_reading_head)
188 {
189 struct worktree **list = NULL;
190 struct strbuf path = STRBUF_INIT;
@@ -192,9 +194,9 @@ static struct worktree **get_worktrees_internal(int skip_reading_head)
194
195 ALLOC_ARRAY(list, alloc);
196
195 - list[counter++] = get_main_worktree(skip_reading_head);
197 + list[counter++] = get_main_worktree(repo, skip_reading_head);
198
197 - strbuf_addf(&path, "%s/worktrees", repo_get_common_dir(the_repository));
199 + strbuf_addf(&path, "%s/worktrees", repo_get_common_dir(repo));
200 dir = opendir(path.buf);
201 strbuf_release(&path);
202 if (dir) {
@@ -216,12 +218,12 @@ static struct worktree **get_worktrees_internal(int skip_reading_head)
218
219 struct worktree **get_worktrees(void)
220 {
219 - return get_worktrees_internal(0);
221 + return get_worktrees_internal(the_repository, 0);
222 }
223
224 struct worktree **get_worktrees_without_reading_head(void)
225 {
224 - return get_worktrees_internal(1);
226 + return get_worktrees_internal(the_repository, 1);
227 }
228
229 char *get_worktree_git_dir(const struct worktree *wt)
@@ -707,7 +709,7 @@ static void repair_noop(int iserr UNUSED,
709
710 void repair_worktrees(worktree_repair_fn fn, void *cb_data, int use_relative_paths)
711 {
710 - struct worktree **worktrees = get_worktrees_internal(1);
712 + struct worktree **worktrees = get_worktrees_internal(the_repository, 1);
713 struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
714
715 if (!fn)
@@ -752,7 +754,7 @@ done:
754
755 void repair_worktrees_after_gitdir_move(const char *old_path)
756 {
755 - struct worktree **worktrees = get_worktrees_internal(1);
757 + struct worktree **worktrees = get_worktrees_internal(the_repository, 1);
758 struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
759
760 for (; *wt; wt++)
@@ -786,7 +788,9 @@ static int is_main_worktree_path(const char *path)
788 *
789 * Returns -1 on failure and strbuf.len on success.
790 */
789 -static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)
791 +static ssize_t infer_backlink(struct repository *repo,
792 + const char *gitfile,
793 + struct strbuf *inferred)
794 {
795 struct strbuf actual = STRBUF_INIT;
796 const char *id;
@@ -801,7 +805,7 @@ static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)
805 id++; /* advance past '/' to point at <id> */
806 if (!*id)
807 goto error;
804 - repo_common_path_replace(the_repository, inferred, "worktrees/%s", id);
808 + repo_common_path_replace(repo, inferred, "worktrees/%s", id);
809 if (!is_directory(inferred->buf))
810 goto error;
811
@@ -842,7 +846,7 @@ void repair_worktree_at_path(const char *path,
846 goto done;
847 }
848
845 - infer_backlink(dotgit.buf, &inferred_backlink);
849 + infer_backlink(the_repository, dotgit.buf, &inferred_backlink);
850 strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0);
851 dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
852 if (dotgit_contents) {
@@ -1017,12 +1021,13 @@ done:
1021 return rc;
1022 }
1023
1020 -static int move_config_setting(const char *key, const char *value,
1024 +static int move_config_setting(struct repository *repo,
1025 + const char *key, const char *value,
1026 const char *from_file, const char *to_file)
1027 {
1023 - if (repo_config_set_in_file_gently(the_repository, to_file, key, NULL, value))
1028 + if (repo_config_set_in_file_gently(repo, to_file, key, NULL, value))
1029 return error(_("unable to set %s in '%s'"), key, to_file);
1025 - if (repo_config_set_in_file_gently(the_repository, from_file, key, NULL, NULL))
1030 + if (repo_config_set_in_file_gently(repo, from_file, key, NULL, NULL))
1031 return error(_("unable to unset %s in '%s'"), key, from_file);
1032 return 0;
1033 }
@@ -1058,7 +1063,7 @@ int init_worktree_config(struct repository *r)
1063 * _could_ be negating a global core.bare=true.
1064 */
1065 if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) {
1061 - if ((res = move_config_setting("core.bare", "true",
1066 + if ((res = move_config_setting(r, "core.bare", "true",
1067 common_config_file,
1068 main_worktree_file)))
1069 goto cleanup;
@@ -1070,7 +1075,7 @@ int init_worktree_config(struct repository *r)
1075 * upgrade to worktree config.
1076 */
1077 if (!git_configset_get_value(&cs, "core.worktree", &core_worktree, NULL)) {
1073 - if ((res = move_config_setting("core.worktree", core_worktree,
1078 + if ((res = move_config_setting(r, "core.worktree", core_worktree,
1079 common_config_file,
1080 main_worktree_file)))
1081 goto cleanup;