Skip to content

Commit a3673f4

Browse files
pks-tgitster
authored andcommitted
environment: make get_object_directory() accept a repository
The `get_object_directory()` function retrieves the path to the object 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 661624a commit a3673f4

20 files changed

+43
-39
lines changed

builtin/commit-graph.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "builtin.h"
22
#include "commit.h"
33
#include "config.h"
4-
#include "environment.h"
54
#include "gettext.h"
65
#include "hex.h"
76
#include "parse-options.h"
@@ -95,7 +94,7 @@ static int graph_verify(int argc, const char **argv, const char *prefix)
9594
usage_with_options(builtin_commit_graph_verify_usage, options);
9695

9796
if (!opts.obj_dir)
98-
opts.obj_dir = get_object_directory();
97+
opts.obj_dir = repo_get_object_directory(the_repository);
9998
if (opts.shallow)
10099
flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
101100
if (opts.progress)
@@ -275,7 +274,7 @@ static int graph_write(int argc, const char **argv, const char *prefix)
275274
if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
276275
die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
277276
if (!opts.obj_dir)
278-
opts.obj_dir = get_object_directory();
277+
opts.obj_dir = repo_get_object_directory(the_repository);
279278
if (opts.append)
280279
flags |= COMMIT_GRAPH_WRITE_APPEND;
281280
if (opts.split)

builtin/count-objects.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "builtin.h"
88
#include "config.h"
99
#include "dir.h"
10-
#include "environment.h"
1110
#include "gettext.h"
1211
#include "path.h"
1312
#include "repository.h"
@@ -116,7 +115,7 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
116115
report_linked_checkout_garbage(the_repository);
117116
}
118117

119-
for_each_loose_file_in_objdir(get_object_directory(),
118+
for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
120119
count_loose, count_cruft, NULL, NULL);
121120

122121
if (verbose) {

builtin/multi-pack-index.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include "builtin.h"
22
#include "abspath.h"
33
#include "config.h"
4-
#include "environment.h"
54
#include "gettext.h"
65
#include "parse-options.h"
76
#include "midx.h"
87
#include "strbuf.h"
98
#include "trace2.h"
109
#include "object-store-ll.h"
1110
#include "replace-object.h"
11+
#include "repository.h"
1212

1313
#define BUILTIN_MIDX_WRITE_USAGE \
1414
N_("git multi-pack-index [<options>] write [--preferred-pack=<pack>]" \
@@ -63,7 +63,7 @@ static int parse_object_dir(const struct option *opt, const char *arg,
6363
char **value = opt->value;
6464
free(*value);
6565
if (unset)
66-
*value = xstrdup(get_object_directory());
66+
*value = xstrdup(repo_get_object_directory(the_repository));
6767
else
6868
*value = real_pathdup(arg, 1);
6969
return 0;

builtin/pack-objects.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3940,7 +3940,7 @@ static int add_loose_object(const struct object_id *oid, const char *path,
39403940
*/
39413941
static void add_unreachable_loose_objects(void)
39423942
{
3943-
for_each_loose_file_in_objdir(get_object_directory(),
3943+
for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
39443944
add_loose_object,
39453945
NULL, NULL, NULL);
39463946
}

builtin/prune.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
193193
revs.exclude_promisor_objects = 1;
194194
}
195195

196-
for_each_loose_file_in_objdir(get_object_directory(), prune_object,
197-
prune_cruft, prune_subdir, &revs);
196+
for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
197+
prune_object, prune_cruft, prune_subdir, &revs);
198198

199199
prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
200-
remove_temporary_files(get_object_directory());
201-
s = mkpathdup("%s/pack", get_object_directory());
200+
remove_temporary_files(repo_get_object_directory(the_repository));
201+
s = mkpathdup("%s/pack", repo_get_object_directory(the_repository));
202202
remove_temporary_files(s);
203203
free(s);
204204

builtin/repack.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
12401240
if (write_midx && write_bitmaps) {
12411241
struct strbuf path = STRBUF_INIT;
12421242

1243-
strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
1243+
strbuf_addf(&path, "%s/%s_XXXXXX", repo_get_object_directory(the_repository),
12441244
"bitmap-ref-tips");
12451245

12461246
refs_snapshot = xmks_tempfile(path.buf);
@@ -1249,7 +1249,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
12491249
strbuf_release(&path);
12501250
}
12511251

1252-
packdir = mkpathdup("%s/pack", get_object_directory());
1252+
packdir = mkpathdup("%s/pack", repo_get_object_directory(the_repository));
12531253
packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
12541254
packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
12551255

@@ -1519,7 +1519,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
15191519
unsigned flags = 0;
15201520
if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
15211521
flags |= MIDX_WRITE_INCREMENTAL;
1522-
write_midx_file(get_object_directory(), NULL, NULL, flags);
1522+
write_midx_file(repo_get_object_directory(the_repository),
1523+
NULL, NULL, flags);
15231524
}
15241525

15251526
cleanup:

bulk-checkin.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile *state)
7575
close(fd);
7676
}
7777

78-
strbuf_addf(&packname, "%s/pack/pack-%s.", get_object_directory(),
78+
strbuf_addf(&packname, "%s/pack/pack-%s.", repo_get_object_directory(the_repository),
7979
hash_to_hex(hash));
8080
finish_tmp_packfile(&packname, state->pack_tmp_name,
8181
state->written, state->nr_written,
@@ -113,7 +113,7 @@ static void flush_batch_fsync(void)
113113
* to ensure that the data in each new object file is durable before
114114
* the final name is visible.
115115
*/
116-
strbuf_addf(&temp_path, "%s/bulk_fsync_XXXXXX", get_object_directory());
116+
strbuf_addf(&temp_path, "%s/bulk_fsync_XXXXXX", repo_get_object_directory(the_repository));
117117
temp = xmks_tempfile(temp_path.buf);
118118
fsync_or_die(get_tempfile_fd(temp), get_tempfile_path(temp));
119119
delete_tempfile(&temp);

environment.c

-7
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,6 @@ const char *get_git_work_tree(void)
273273
return the_repository->worktree;
274274
}
275275

276-
const char *get_object_directory(void)
277-
{
278-
if (!the_repository->objects->odb)
279-
BUG("git environment hasn't been setup");
280-
return the_repository->objects->odb->path;
281-
}
282-
283276
int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
284277
{
285278
int fd;

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_object_directory(void);
110109
char *get_index_file(void);
111110
char *get_graft_file(struct repository *r);
112111
void set_git_dir(const char *path, int make_realpath);

fetch-pack.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
18391839

18401840
string_list_append_nodup(pack_lockfiles,
18411841
xstrfmt("%s/pack/pack-%s.keep",
1842-
get_object_directory(),
1842+
repo_get_object_directory(the_repository),
18431843
packname));
18441844
}
18451845
string_list_clear(&packfile_uris, 0);

http-backend.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ static void get_head(struct strbuf *hdr, char *arg UNUSED)
601601

602602
static void get_info_packs(struct strbuf *hdr, char *arg UNUSED)
603603
{
604-
size_t objdirlen = strlen(get_object_directory());
604+
size_t objdirlen = strlen(repo_get_object_directory(the_repository));
605605
struct strbuf buf = STRBUF_INIT;
606606
struct packed_git *p;
607607
size_t cnt = 0;

object-file.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ static int start_loose_object_common(struct strbuf *tmp_file,
20532053
else if (errno == EACCES)
20542054
return error(_("insufficient permission for adding "
20552055
"an object to repository database %s"),
2056-
get_object_directory());
2056+
repo_get_object_directory(the_repository));
20572057
else
20582058
return error_errno(
20592059
_("unable to create temporary file"));
@@ -2228,7 +2228,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
22282228
prepare_loose_object_bulk_checkin();
22292229

22302230
/* Since oid is not determined, save tmp file to odb path. */
2231-
strbuf_addf(&filename, "%s/", get_object_directory());
2231+
strbuf_addf(&filename, "%s/", repo_get_object_directory(the_repository));
22322232
hdrlen = format_object_header(hdr, sizeof(hdr), OBJ_BLOB, len);
22332233

22342234
/*

pack-write.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "pack-objects.h"
1313
#include "pack-revindex.h"
1414
#include "path.h"
15+
#include "repository.h"
1516
#include "strbuf.h"
1617

1718
void reset_pack_idx_option(struct pack_idx_option *opts)
@@ -473,7 +474,7 @@ char *index_pack_lockfile(int ip_out, int *is_well_formed)
473474
packname[len-1] = 0;
474475
if (skip_prefix(packname, "keep\t", &name))
475476
return xstrfmt("%s/pack/pack-%s.keep",
476-
get_object_directory(), name);
477+
repo_get_object_directory(the_repository), name);
477478
return NULL;
478479
}
479480
if (is_well_formed)

packfile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ char *odb_pack_name(struct strbuf *buf,
3030
const char *ext)
3131
{
3232
strbuf_reset(buf);
33-
strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
33+
strbuf_addf(buf, "%s/pack/pack-%s.%s", repo_get_object_directory(the_repository),
3434
hash_to_hex(hash), ext);
3535
return buf->buf;
3636
}

prune-packed.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
13
#include "git-compat-util.h"
2-
#include "environment.h"
34
#include "gettext.h"
45
#include "object-store-ll.h"
56
#include "packfile.h"
67
#include "progress.h"
78
#include "prune-packed.h"
9+
#include "repository.h"
810

911
static struct progress *progress;
1012

@@ -37,7 +39,7 @@ void prune_packed_objects(int opts)
3739
if (opts & PRUNE_PACKED_VERBOSE)
3840
progress = start_delayed_progress(_("Removing duplicate objects"), 256);
3941

40-
for_each_loose_file_in_objdir(get_object_directory(),
42+
for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
4143
prune_object, NULL, prune_subdir, &opts);
4244

4345
/* Ensure we show 100% before finishing progress */

repository.c

+7
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ const char *repo_get_common_dir(struct repository *repo)
105105
return repo->commondir;
106106
}
107107

108+
const char *repo_get_object_directory(struct repository *repo)
109+
{
110+
if (!repo->objects->odb)
111+
BUG("repository hasn't been set up");
112+
return repo->objects->odb->path;
113+
}
114+
108115
static void repo_set_commondir(struct repository *repo,
109116
const char *commondir)
110117
{

repository.h

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ extern struct repository *the_repository;
208208

209209
const char *repo_get_git_dir(struct repository *repo);
210210
const char *repo_get_common_dir(struct repository *repo);
211+
const char *repo_get_object_directory(struct repository *repo);
211212

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

server-info.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "git-compat-util.h"
44
#include "dir.h"
5-
#include "environment.h"
65
#include "hex.h"
76
#include "repository.h"
87
#include "refs.h"
@@ -342,7 +341,8 @@ static int write_pack_info_file(struct update_info_ctx *uic)
342341

343342
static int update_info_packs(int force)
344343
{
345-
char *infofile = mkpathdup("%s/info/packs", get_object_directory());
344+
char *infofile = mkpathdup("%s/info/packs",
345+
repo_get_object_directory(the_repository));
346346
int ret;
347347

348348
init_pack_info(infofile, force);

setup.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2282,7 +2282,7 @@ static void create_object_directory(void)
22822282
struct strbuf path = STRBUF_INIT;
22832283
size_t baselen;
22842284

2285-
strbuf_addstr(&path, get_object_directory());
2285+
strbuf_addstr(&path, repo_get_object_directory(the_repository));
22862286
baselen = path.len;
22872287

22882288
safe_create_dir(path.buf, 1);

tmp-objdir.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "strvec.h"
1414
#include "quote.h"
1515
#include "object-store-ll.h"
16+
#include "repository.h"
1617

1718
struct tmp_objdir {
1819
struct strbuf path;
@@ -132,7 +133,8 @@ struct tmp_objdir *tmp_objdir_create(const char *prefix)
132133
* can recognize any stale objdirs left behind by a crash and delete
133134
* them.
134135
*/
135-
strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX", get_object_directory(), prefix);
136+
strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX",
137+
repo_get_object_directory(the_repository), prefix);
136138

137139
if (!mkdtemp(t->path.buf)) {
138140
/* free, not destroy, as we never touched the filesystem */
@@ -152,7 +154,7 @@ struct tmp_objdir *tmp_objdir_create(const char *prefix)
152154
}
153155

154156
env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
155-
absolute_path(get_object_directory()));
157+
absolute_path(repo_get_object_directory(the_repository)));
156158
env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf));
157159
env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
158160
absolute_path(t->path.buf));
@@ -267,7 +269,7 @@ int tmp_objdir_migrate(struct tmp_objdir *t)
267269
}
268270

269271
strbuf_addbuf(&src, &t->path);
270-
strbuf_addstr(&dst, get_object_directory());
272+
strbuf_addstr(&dst, repo_get_object_directory(the_repository));
271273

272274
ret = migrate_paths(&src, &dst);
273275

0 commit comments

Comments
 (0)