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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 12, 2024 at 13:29 UTC a3673f48986cf990006e56a57e4ad3c7134161e7
20 files changed +43 -39
builtin/commit-graph.c
+2 -3
@@ -1,7 +1,6 @@
1 #include "builtin.h"
2 #include "commit.h"
3 #include "config.h"
4 -#include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "parse-options.h"
@@ -95,7 +94,7 @@ static int graph_verify(int argc, const char **argv, const char *prefix)
94 usage_with_options(builtin_commit_graph_verify_usage, options);
95
96 if (!opts.obj_dir)
98 - opts.obj_dir = get_object_directory();
97 + opts.obj_dir = repo_get_object_directory(the_repository);
98 if (opts.shallow)
99 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
100 if (opts.progress)
@@ -275,7 +274,7 @@ static int graph_write(int argc, const char **argv, const char *prefix)
274 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
275 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
276 if (!opts.obj_dir)
278 - opts.obj_dir = get_object_directory();
277 + opts.obj_dir = repo_get_object_directory(the_repository);
278 if (opts.append)
279 flags |= COMMIT_GRAPH_WRITE_APPEND;
280 if (opts.split)
builtin/count-objects.c
+1 -2
@@ -7,7 +7,6 @@
7 #include "builtin.h"
8 #include "config.h"
9 #include "dir.h"
10 -#include "environment.h"
10 #include "gettext.h"
11 #include "path.h"
12 #include "repository.h"
@@ -116,7 +115,7 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
115 report_linked_checkout_garbage(the_repository);
116 }
117
119 - for_each_loose_file_in_objdir(get_object_directory(),
118 + for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
119 count_loose, count_cruft, NULL, NULL);
120
121 if (verbose) {
builtin/multi-pack-index.c
+2 -2
@@ -1,7 +1,6 @@
1 #include "builtin.h"
2 #include "abspath.h"
3 #include "config.h"
4 -#include "environment.h"
4 #include "gettext.h"
5 #include "parse-options.h"
6 #include "midx.h"
@@ -9,6 +8,7 @@
8 #include "trace2.h"
9 #include "object-store-ll.h"
10 #include "replace-object.h"
11 +#include "repository.h"
12
13 #define BUILTIN_MIDX_WRITE_USAGE \
14 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,
63 char **value = opt->value;
64 free(*value);
65 if (unset)
66 - *value = xstrdup(get_object_directory());
66 + *value = xstrdup(repo_get_object_directory(the_repository));
67 else
68 *value = real_pathdup(arg, 1);
69 return 0;
builtin/pack-objects.c
+1 -1
@@ -3940,7 +3940,7 @@ static int add_loose_object(const struct object_id *oid, const char *path,
3940 */
3941 static void add_unreachable_loose_objects(void)
3942 {
3943 - for_each_loose_file_in_objdir(get_object_directory(),
3943 + for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
3944 add_loose_object,
3945 NULL, NULL, NULL);
3946 }
builtin/prune.c
+4 -4
@@ -193,12 +193,12 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
193 revs.exclude_promisor_objects = 1;
194 }
195
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);
198
199 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));
202 remove_temporary_files(s);
203 free(s);
204
builtin/repack.c
+4 -3
@@ -1240,7 +1240,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
1240 if (write_midx && write_bitmaps) {
1241 struct strbuf path = STRBUF_INIT;
1242
1243 - strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
1243 + strbuf_addf(&path, "%s/%s_XXXXXX", repo_get_object_directory(the_repository),
1244 "bitmap-ref-tips");
1245
1246 refs_snapshot = xmks_tempfile(path.buf);
@@ -1249,7 +1249,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
1249 strbuf_release(&path);
1250 }
1251
1252 - packdir = mkpathdup("%s/pack", get_object_directory());
1252 + packdir = mkpathdup("%s/pack", repo_get_object_directory(the_repository));
1253 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
1254 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
1255
@@ -1519,7 +1519,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
1519 unsigned flags = 0;
1520 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
1521 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);
1524 }
1525
1526 cleanup:
bulk-checkin.c
+2 -2
@@ -75,7 +75,7 @@ static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile *state)
75 close(fd);
76 }
77
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),
79 hash_to_hex(hash));
80 finish_tmp_packfile(&packname, state->pack_tmp_name,
81 state->written, state->nr_written,
@@ -113,7 +113,7 @@ static void flush_batch_fsync(void)
113 * to ensure that the data in each new object file is durable before
114 * the final name is visible.
115 */
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));
117 temp = xmks_tempfile(temp_path.buf);
118 fsync_or_die(get_tempfile_fd(temp), get_tempfile_path(temp));
119 delete_tempfile(&temp);
environment.c
-7
@@ -273,13 +273,6 @@ const char *get_git_work_tree(void)
273 return the_repository->worktree;
274 }
275
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 -
276 int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
277 {
278 int fd;
environment.h
-1
@@ -106,7 +106,6 @@ int have_git_dir(void);
106 extern int is_bare_repository_cfg;
107 int is_bare_repository(void);
108 extern char *git_work_tree_cfg;
109 -const char *get_object_directory(void);
109 char *get_index_file(void);
110 char *get_graft_file(struct repository *r);
111 void set_git_dir(const char *path, int make_realpath);
fetch-pack.c
+1 -1
@@ -1839,7 +1839,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1839
1840 string_list_append_nodup(pack_lockfiles,
1841 xstrfmt("%s/pack/pack-%s.keep",
1842 - get_object_directory(),
1842 + repo_get_object_directory(the_repository),
1843 packname));
1844 }
1845 string_list_clear(&packfile_uris, 0);
http-backend.c
+1 -1
@@ -601,7 +601,7 @@ static void get_head(struct strbuf *hdr, char *arg UNUSED)
601
602 static void get_info_packs(struct strbuf *hdr, char *arg UNUSED)
603 {
604 - size_t objdirlen = strlen(get_object_directory());
604 + size_t objdirlen = strlen(repo_get_object_directory(the_repository));
605 struct strbuf buf = STRBUF_INIT;
606 struct packed_git *p;
607 size_t cnt = 0;
object-file.c
+2 -2
@@ -2053,7 +2053,7 @@ static int start_loose_object_common(struct strbuf *tmp_file,
2053 else if (errno == EACCES)
2054 return error(_("insufficient permission for adding "
2055 "an object to repository database %s"),
2056 - get_object_directory());
2056 + repo_get_object_directory(the_repository));
2057 else
2058 return error_errno(
2059 _("unable to create temporary file"));
@@ -2228,7 +2228,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
2228 prepare_loose_object_bulk_checkin();
2229
2230 /* 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));
2232 hdrlen = format_object_header(hdr, sizeof(hdr), OBJ_BLOB, len);
2233
2234 /*
pack-write.c
+2 -1
@@ -12,6 +12,7 @@
12 #include "pack-objects.h"
13 #include "pack-revindex.h"
14 #include "path.h"
15 +#include "repository.h"
16 #include "strbuf.h"
17
18 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)
474 packname[len-1] = 0;
475 if (skip_prefix(packname, "keep\t", &name))
476 return xstrfmt("%s/pack/pack-%s.keep",
476 - get_object_directory(), name);
477 + repo_get_object_directory(the_repository), name);
478 return NULL;
479 }
480 if (is_well_formed)
packfile.c
+1 -1
@@ -30,7 +30,7 @@ char *odb_pack_name(struct strbuf *buf,
30 const char *ext)
31 {
32 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),
34 hash_to_hex(hash), ext);
35 return buf->buf;
36 }
prune-packed.c
+4 -2
@@ -1,10 +1,12 @@
1 +#define USE_THE_REPOSITORY_VARIABLE
2 +
3 #include "git-compat-util.h"
2 -#include "environment.h"
4 #include "gettext.h"
5 #include "object-store-ll.h"
6 #include "packfile.h"
7 #include "progress.h"
8 #include "prune-packed.h"
9 +#include "repository.h"
10
11 static struct progress *progress;
12
@@ -37,7 +39,7 @@ void prune_packed_objects(int opts)
39 if (opts & PRUNE_PACKED_VERBOSE)
40 progress = start_delayed_progress(_("Removing duplicate objects"), 256);
41
40 - for_each_loose_file_in_objdir(get_object_directory(),
42 + for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
43 prune_object, NULL, prune_subdir, &opts);
44
45 /* Ensure we show 100% before finishing progress */
repository.c
+7
@@ -105,6 +105,13 @@ const char *repo_get_common_dir(struct repository *repo)
105 return repo->commondir;
106 }
107
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 +
115 static void repo_set_commondir(struct repository *repo,
116 const char *commondir)
117 {
repository.h
+1
@@ -208,6 +208,7 @@ extern struct repository *the_repository;
208
209 const char *repo_get_git_dir(struct repository *repo);
210 const char *repo_get_common_dir(struct repository *repo);
211 +const char *repo_get_object_directory(struct repository *repo);
212
213 /*
214 * Define a custom repository layout. Any field can be NULL, which
server-info.c
+2 -2
@@ -2,7 +2,6 @@
2
3 #include "git-compat-util.h"
4 #include "dir.h"
5 -#include "environment.h"
5 #include "hex.h"
6 #include "repository.h"
7 #include "refs.h"
@@ -342,7 +341,8 @@ static int write_pack_info_file(struct update_info_ctx *uic)
341
342 static int update_info_packs(int force)
343 {
345 - char *infofile = mkpathdup("%s/info/packs", get_object_directory());
344 + char *infofile = mkpathdup("%s/info/packs",
345 + repo_get_object_directory(the_repository));
346 int ret;
347
348 init_pack_info(infofile, force);
setup.c
+1 -1
@@ -2282,7 +2282,7 @@ static void create_object_directory(void)
2282 struct strbuf path = STRBUF_INIT;
2283 size_t baselen;
2284
2285 - strbuf_addstr(&path, get_object_directory());
2285 + strbuf_addstr(&path, repo_get_object_directory(the_repository));
2286 baselen = path.len;
2287
2288 safe_create_dir(path.buf, 1);
tmp-objdir.c
+5 -3
@@ -13,6 +13,7 @@
13 #include "strvec.h"
14 #include "quote.h"
15 #include "object-store-ll.h"
16 +#include "repository.h"
17
18 struct tmp_objdir {
19 struct strbuf path;
@@ -132,7 +133,8 @@ struct tmp_objdir *tmp_objdir_create(const char *prefix)
133 * can recognize any stale objdirs left behind by a crash and delete
134 * them.
135 */
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);
138
139 if (!mkdtemp(t->path.buf)) {
140 /* free, not destroy, as we never touched the filesystem */
@@ -152,7 +154,7 @@ struct tmp_objdir *tmp_objdir_create(const char *prefix)
154 }
155
156 env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
155 - absolute_path(get_object_directory()));
157 + absolute_path(repo_get_object_directory(the_repository)));
158 env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf));
159 env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
160 absolute_path(t->path.buf));
@@ -267,7 +269,7 @@ int tmp_objdir_migrate(struct tmp_objdir *t)
269 }
270
271 strbuf_addbuf(&src, &t->path);
270 - strbuf_addstr(&dst, get_object_directory());
272 + strbuf_addstr(&dst, repo_get_object_directory(the_repository));
273
274 ret = migrate_paths(&src, &dst);
275