packfile: add repository to struct `packed_git`

The struct `packed_git` holds information regarding a packed object file. Let's add the repository variable to this object, to represent the repository that this packfile belongs to. This helps remove dependency on the global `the_repository` object in `packfile.c` by simply using repository information now readily available in the struct. We do need to consider that a packfile could be part of the alternates of a repository, but considering that we only have one repository struct and also that we currently anyways use 'the_repository', we should be OK with this change. We also modify `alloc_packed_git` to ensure that the repository is added to newly created `packed_git` structs. This requires modifying the function and all its callee to pass the repository object down the levels. Helped-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Dec 3, 2024 at 15:43 UTC 2cf3fe63f6eedd6d132c530b897595345a05088b
10 files changed +30 -16
builtin/fast-import.c
+2 -1
@@ -765,6 +765,7 @@ static void start_packfile(void)
765
766 p->pack_fd = pack_fd;
767 p->do_not_close = 1;
768 + p->repo = the_repository;
769 pack_file = hashfd(pack_fd, p->pack_name);
770
771 pack_data = p;
@@ -888,7 +889,7 @@ static void end_packfile(void)
889 idx_name = keep_pack(create_index());
890
891 /* Register the packfile with core git's machinery. */
891 - new_p = add_packed_git(idx_name, strlen(idx_name), 1);
892 + new_p = add_packed_git(pack_data->repo, idx_name, strlen(idx_name), 1);
893 if (!new_p)
894 die("core git rejected index %s", idx_name);
895 all_packs[pack_id] = new_p;
builtin/index-pack.c
+4 -2
@@ -1552,7 +1552,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1552
1553 if (do_fsck_object) {
1554 struct packed_git *p;
1555 - p = add_packed_git(final_index_name, strlen(final_index_name), 0);
1555 + p = add_packed_git(the_repository, final_index_name,
1556 + strlen(final_index_name), 0);
1557 if (p)
1558 install_packed_git(the_repository, p);
1559 }
@@ -1650,7 +1651,8 @@ static void read_v2_anomalous_offsets(struct packed_git *p,
1651
1652 static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
1653 {
1653 - struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1);
1654 + struct packed_git *p = add_packed_git(the_repository, pack_name,
1655 + strlen(pack_name), 1);
1656
1657 if (!p)
1658 die(_("Cannot open existing pack file '%s'"), pack_name);
commit-graph.c
+1 -1
@@ -1914,7 +1914,7 @@ static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1914 struct packed_git *p;
1915 strbuf_setlen(&packname, dirlen);
1916 strbuf_addstr(&packname, pack_indexes->items[i].string);
1917 - p = add_packed_git(packname.buf, packname.len, 1);
1917 + p = add_packed_git(ctx->r, packname.buf, packname.len, 1);
1918 if (!p) {
1919 ret = error(_("error adding pack %s"), packname.buf);
1920 goto cleanup;
connected.c
+2 -1
@@ -54,7 +54,8 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
54 strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
55 base_len);
56 strbuf_addstr(&idx_file, ".idx");
57 - new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
57 + new_pack = add_packed_git(the_repository, idx_file.buf,
58 + idx_file.len, 1);
59 strbuf_release(&idx_file);
60 }
61
http.c
+1 -1
@@ -2439,7 +2439,7 @@ static int fetch_and_setup_pack_index(struct packed_git **packs_head,
2439 if (!tmp_idx)
2440 return -1;
2441
2442 - new_pack = parse_pack_index(sha1, tmp_idx);
2442 + new_pack = parse_pack_index(the_repository, sha1, tmp_idx);
2443 if (!new_pack) {
2444 unlink(tmp_idx);
2445 free(tmp_idx);
midx-write.c
+1 -1
@@ -154,7 +154,7 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
154 return;
155
156 ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
157 - p = add_packed_git(full_path, full_path_len, 0);
157 + p = add_packed_git(the_repository, full_path, full_path_len, 0);
158 if (!p) {
159 warning(_("failed to add packfile '%s'"),
160 full_path);
midx.c
+1 -1
@@ -464,7 +464,7 @@ int prepare_midx_pack(struct repository *r, struct multi_pack_index *m,
464 strhash(key.buf), key.buf,
465 struct packed_git, packmap_ent);
466 if (!p) {
467 - p = add_packed_git(pack_name.buf, pack_name.len, m->local);
467 + p = add_packed_git(r, pack_name.buf, pack_name.len, m->local);
468 if (p) {
469 install_packed_git(r, p);
470 list_add_tail(&p->mru, &r->objects->packed_git_mru);
object-store-ll.h
+5
@@ -10,6 +10,7 @@
10 struct oidmap;
11 struct oidtree;
12 struct strbuf;
13 +struct repository;
14
15 struct object_directory {
16 struct object_directory *next;
@@ -135,6 +136,10 @@ struct packed_git {
136 */
137 const uint32_t *mtimes_map;
138 size_t mtimes_size;
139 +
140 + /* repo denotes the repository this packfile belongs to */
141 + struct repository *repo;
142 +
143 /* something like ".git/objects/pack/xxxxx.pack" */
144 char pack_name[FLEX_ARRAY]; /* more */
145 };
packfile.c
+9 -6
@@ -217,11 +217,12 @@ uint32_t get_pack_fanout(struct packed_git *p, uint32_t value)
217 return ntohl(level1_ofs[value]);
218 }
219
220 -static struct packed_git *alloc_packed_git(int extra)
220 +static struct packed_git *alloc_packed_git(struct repository *r, int extra)
221 {
222 struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
223 memset(p, 0, sizeof(*p));
224 p->pack_fd = -1;
225 + p->repo = r;
226 return p;
227 }
228
@@ -233,11 +234,12 @@ static char *pack_path_from_idx(const char *idx_path)
234 return xstrfmt("%.*s.pack", (int)len, idx_path);
235 }
236
236 -struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
237 +struct packed_git *parse_pack_index(struct repository *r, unsigned char *sha1,
238 + const char *idx_path)
239 {
240 char *path = pack_path_from_idx(idx_path);
241 size_t alloc = st_add(strlen(path), 1);
240 - struct packed_git *p = alloc_packed_git(alloc);
242 + struct packed_git *p = alloc_packed_git(r, alloc);
243
244 memcpy(p->pack_name, path, alloc); /* includes NUL */
245 free(path);
@@ -703,7 +705,8 @@ void unuse_pack(struct pack_window **w_cursor)
705 }
706 }
707
706 -struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
708 +struct packed_git *add_packed_git(struct repository *r, const char *path,
709 + size_t path_len, int local)
710 {
711 struct stat st;
712 size_t alloc;
@@ -721,7 +724,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
724 * the use xsnprintf double-checks that)
725 */
726 alloc = st_add3(path_len, strlen(".promisor"), 1);
724 - p = alloc_packed_git(alloc);
727 + p = alloc_packed_git(r, alloc);
728 memcpy(p->pack_name, path, path_len);
729
730 xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
@@ -877,7 +880,7 @@ static void prepare_pack(const char *full_name, size_t full_name_len,
880
881 /* Don't reopen a pack we already have. */
882 if (!hashmap_get(&data->r->objects->pack_map, &hent, pack_name)) {
880 - p = add_packed_git(full_name, full_name_len, data->local);
883 + p = add_packed_git(data->r, full_name, full_name_len, data->local);
884 if (p)
885 install_packed_git(data->r, p);
886 }
packfile.h
+4 -2
@@ -46,7 +46,8 @@ const char *pack_basename(struct packed_git *p);
46 * and does not add the resulting packed_git struct to the internal list of
47 * packs. You probably want add_packed_git() instead.
48 */
49 -struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path);
49 +struct packed_git *parse_pack_index(struct repository *r, unsigned char *sha1,
50 + const char *idx_path);
51
52 typedef void each_file_in_pack_dir_fn(const char *full_path, size_t full_path_len,
53 const char *file_name, void *data);
@@ -113,7 +114,8 @@ void close_pack(struct packed_git *);
114 void close_object_store(struct raw_object_store *o);
115 void unuse_pack(struct pack_window **);
116 void clear_delta_base_cache(void);
116 -struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
117 +struct packed_git *add_packed_git(struct repository *r, const char *path,
118 + size_t path_len, int local);
119
120 /*
121 * Unlink the .pack and associated extension files.