Skip to content

Commit 661624a

Browse files
pks-tgitster
authored andcommitted
environment: make get_git_common_dir() accept a repository
The `get_git_common_dir()` function retrieves the path to the common directory for `the_repository`. Make it accept a `struct repository` such that it can work on arbitrary repositories and make it part of the repository subsystem. This reduces our reliance on `the_repository` and clarifies scope. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 246deea commit 661624a

13 files changed

+22
-21
lines changed

builtin/config.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ static void location_options_init(struct config_location_options *opts,
807807
else
808808
opts->options.respect_includes = opts->respect_includes_opt;
809809
if (startup_info->have_repository) {
810-
opts->options.commondir = get_git_common_dir();
810+
opts->options.commondir = repo_get_common_dir(the_repository);
811811
opts->options.git_dir = repo_get_git_dir(the_repository);
812812
}
813813
}

builtin/gc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2132,7 +2132,7 @@ static int schtasks_schedule_task(const char *exec_path, enum schedule_priority
21322132
get_schedule_cmd(&cmd, NULL);
21332133

21342134
strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
2135-
get_git_common_dir(), frequency);
2135+
repo_get_common_dir(the_repository), frequency);
21362136
tfile = xmks_tempfile(tfilename.buf);
21372137
strbuf_release(&tfilename);
21382138

builtin/rev-parse.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "path.h"
2020
#include "diff.h"
2121
#include "read-cache-ll.h"
22+
#include "repository.h"
2223
#include "revision.h"
2324
#include "setup.h"
2425
#include "split-index.h"
@@ -1042,7 +1043,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
10421043
continue;
10431044
}
10441045
if (!strcmp(arg, "--git-common-dir")) {
1045-
print_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
1046+
print_path(repo_get_common_dir(the_repository), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
10461047
continue;
10471048
}
10481049
if (!strcmp(arg, "--is-inside-git-dir")) {

builtin/worktree.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static void prune_worktrees(void)
219219
}
220220
closedir(dir);
221221

222-
strbuf_add_absolute_path(&main_path, get_git_common_dir());
222+
strbuf_add_absolute_path(&main_path, repo_get_common_dir(the_repository));
223223
/* massage main worktree absolute path to match 'gitdir' content */
224224
strbuf_strip_suffix(&main_path, "/.");
225225
string_list_append_nodup(&kept, strbuf_detach(&main_path, NULL));
@@ -492,7 +492,7 @@ static int add_worktree(const char *path, const char *refname,
492492
strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
493493
strbuf_realpath(&realpath, sb_git.buf, 1);
494494
write_file(sb.buf, "%s", realpath.buf);
495-
strbuf_realpath(&realpath, get_git_common_dir(), 1);
495+
strbuf_realpath(&realpath, repo_get_common_dir(the_repository), 1);
496496
write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
497497
realpath.buf, name);
498498
strbuf_reset(&sb);

config.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,7 @@ void read_early_config(config_fn_t cb, void *data)
22132213
opts.respect_includes = 1;
22142214

22152215
if (have_git_dir()) {
2216-
opts.commondir = get_git_common_dir();
2216+
opts.commondir = repo_get_common_dir(the_repository);
22172217
opts.git_dir = repo_get_git_dir(the_repository);
22182218
/*
22192219
* When setup_git_directory() was not yet asked to discover the

environment.c

-7
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,6 @@ int have_git_dir(void)
228228
|| the_repository->gitdir;
229229
}
230230

231-
const char *get_git_common_dir(void)
232-
{
233-
if (!the_repository->commondir)
234-
BUG("git environment hasn't been setup");
235-
return the_repository->commondir;
236-
}
237-
238231
const char *get_git_namespace(void)
239232
{
240233
if (!git_namespace)

environment.h

-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ int have_git_dir(void);
106106
extern int is_bare_repository_cfg;
107107
int is_bare_repository(void);
108108
extern char *git_work_tree_cfg;
109-
const char *get_git_common_dir(void);
110109
const char *get_object_directory(void);
111110
char *get_index_file(void);
112111
char *get_graft_file(struct repository *r);

repository.c

+7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ const char *repo_get_git_dir(struct repository *repo)
9898
return repo->gitdir;
9999
}
100100

101+
const char *repo_get_common_dir(struct repository *repo)
102+
{
103+
if (!repo->commondir)
104+
BUG("repository hasn't been set up");
105+
return repo->commondir;
106+
}
107+
101108
static void repo_set_commondir(struct repository *repo,
102109
const char *commondir)
103110
{

repository.h

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ extern struct repository *the_repository;
207207
#endif
208208

209209
const char *repo_get_git_dir(struct repository *repo);
210+
const char *repo_get_common_dir(struct repository *repo);
210211

211212
/*
212213
* Define a custom repository layout. Any field can be NULL, which

setup.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2068,7 +2068,7 @@ static void copy_templates(const char *option_template)
20682068
goto close_free_return;
20692069
}
20702070

2071-
strbuf_addstr(&path, get_git_common_dir());
2071+
strbuf_addstr(&path, repo_get_common_dir(the_repository));
20722072
strbuf_complete(&path, '/');
20732073
copy_templates_1(&path, &template_path, dir);
20742074
close_free_return:

submodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2462,7 +2462,7 @@ void absorb_git_dir_into_superproject(const char *path,
24622462
} else {
24632463
/* Is it already absorbed into the superprojects git dir? */
24642464
char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
2465-
char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
2465+
char *real_common_git_dir = real_pathdup(repo_get_common_dir(the_repository), 1);
24662466

24672467
if (!starts_with(real_sub_git_dir, real_common_git_dir))
24682468
relocate_single_git_dir_into_superproject(path, super_prefix);

trace.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ void trace_repo_setup(void)
315315
prefix = "(null)";
316316

317317
trace_printf_key(&trace_setup_key, "setup: git_dir: %s\n", quote_crnl(repo_get_git_dir(the_repository)));
318-
trace_printf_key(&trace_setup_key, "setup: git_common_dir: %s\n", quote_crnl(get_git_common_dir()));
318+
trace_printf_key(&trace_setup_key, "setup: git_common_dir: %s\n", quote_crnl(repo_get_common_dir(the_repository)));
319319
trace_printf_key(&trace_setup_key, "setup: worktree: %s\n", quote_crnl(git_work_tree));
320320
trace_printf_key(&trace_setup_key, "setup: cwd: %s\n", quote_crnl(cwd));
321321
trace_printf_key(&trace_setup_key, "setup: prefix: %s\n", quote_crnl(prefix));

worktree.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static struct worktree *get_main_worktree(int skip_reading_head)
7272
struct worktree *worktree = NULL;
7373
struct strbuf worktree_path = STRBUF_INIT;
7474

75-
strbuf_add_real_path(&worktree_path, get_git_common_dir());
75+
strbuf_add_real_path(&worktree_path, repo_get_common_dir(the_repository));
7676
strbuf_strip_suffix(&worktree_path, "/.git");
7777

7878
CALLOC_ARRAY(worktree, 1);
@@ -143,7 +143,7 @@ static struct worktree **get_worktrees_internal(int skip_reading_head)
143143

144144
list[counter++] = get_main_worktree(skip_reading_head);
145145

146-
strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
146+
strbuf_addf(&path, "%s/worktrees", repo_get_common_dir(the_repository));
147147
dir = opendir(path.buf);
148148
strbuf_release(&path);
149149
if (dir) {
@@ -173,7 +173,7 @@ const char *get_worktree_git_dir(const struct worktree *wt)
173173
if (!wt)
174174
return repo_get_git_dir(the_repository);
175175
else if (!wt->id)
176-
return get_git_common_dir();
176+
return repo_get_common_dir(the_repository);
177177
else
178178
return git_common_path("worktrees/%s", wt->id);
179179
}
@@ -626,7 +626,7 @@ static int is_main_worktree_path(const char *path)
626626

627627
strbuf_add_real_path(&target, path);
628628
strbuf_strip_suffix(&target, "/.git");
629-
strbuf_add_real_path(&maindir, get_git_common_dir());
629+
strbuf_add_real_path(&maindir, repo_get_common_dir(the_repository));
630630
strbuf_strip_suffix(&maindir, "/.git");
631631
cmp = fspathcmp(maindir.buf, target.buf);
632632

0 commit comments

Comments
 (0)