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

File tree

13 files changed

+22
-21
lines changed

13 files changed

+22
-21
lines changed

builtin/config.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 7 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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:

0 commit comments

Comments
 (0)