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>
This commit is contained in:
Karthik Nayak
2024-12-03 15:43:55 +01:00
committed by Junio C Hamano
parent 8f8d6eee53
commit 2cf3fe63f6
10 changed files with 30 additions and 16 deletions

View File

@ -765,6 +765,7 @@ static void start_packfile(void)
p->pack_fd = pack_fd; p->pack_fd = pack_fd;
p->do_not_close = 1; p->do_not_close = 1;
p->repo = the_repository;
pack_file = hashfd(pack_fd, p->pack_name); pack_file = hashfd(pack_fd, p->pack_name);
pack_data = p; pack_data = p;
@ -888,7 +889,7 @@ static void end_packfile(void)
idx_name = keep_pack(create_index()); idx_name = keep_pack(create_index());
/* Register the packfile with core git's machinery. */ /* Register the packfile with core git's machinery. */
new_p = add_packed_git(idx_name, strlen(idx_name), 1); new_p = add_packed_git(pack_data->repo, idx_name, strlen(idx_name), 1);
if (!new_p) if (!new_p)
die("core git rejected index %s", idx_name); die("core git rejected index %s", idx_name);
all_packs[pack_id] = new_p; all_packs[pack_id] = new_p;

View File

@ -1552,7 +1552,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
if (do_fsck_object) { if (do_fsck_object) {
struct packed_git *p; struct packed_git *p;
p = add_packed_git(final_index_name, strlen(final_index_name), 0); p = add_packed_git(the_repository, final_index_name,
strlen(final_index_name), 0);
if (p) if (p)
install_packed_git(the_repository, p); install_packed_git(the_repository, p);
} }
@ -1650,7 +1651,8 @@ static void read_v2_anomalous_offsets(struct packed_git *p,
static void read_idx_option(struct pack_idx_option *opts, const char *pack_name) static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
{ {
struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1); struct packed_git *p = add_packed_git(the_repository, pack_name,
strlen(pack_name), 1);
if (!p) if (!p)
die(_("Cannot open existing pack file '%s'"), pack_name); die(_("Cannot open existing pack file '%s'"), pack_name);

View File

@ -1914,7 +1914,7 @@ static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
struct packed_git *p; struct packed_git *p;
strbuf_setlen(&packname, dirlen); strbuf_setlen(&packname, dirlen);
strbuf_addstr(&packname, pack_indexes->items[i].string); strbuf_addstr(&packname, pack_indexes->items[i].string);
p = add_packed_git(packname.buf, packname.len, 1); p = add_packed_git(ctx->r, packname.buf, packname.len, 1);
if (!p) { if (!p) {
ret = error(_("error adding pack %s"), packname.buf); ret = error(_("error adding pack %s"), packname.buf);
goto cleanup; goto cleanup;

View File

@ -54,7 +54,8 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string, strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
base_len); base_len);
strbuf_addstr(&idx_file, ".idx"); strbuf_addstr(&idx_file, ".idx");
new_pack = add_packed_git(idx_file.buf, idx_file.len, 1); new_pack = add_packed_git(the_repository, idx_file.buf,
idx_file.len, 1);
strbuf_release(&idx_file); strbuf_release(&idx_file);
} }

2
http.c
View File

@ -2439,7 +2439,7 @@ static int fetch_and_setup_pack_index(struct packed_git **packs_head,
if (!tmp_idx) if (!tmp_idx)
return -1; return -1;
new_pack = parse_pack_index(sha1, tmp_idx); new_pack = parse_pack_index(the_repository, sha1, tmp_idx);
if (!new_pack) { if (!new_pack) {
unlink(tmp_idx); unlink(tmp_idx);
free(tmp_idx); free(tmp_idx);

View File

@ -154,7 +154,7 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
return; return;
ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc); ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
p = add_packed_git(full_path, full_path_len, 0); p = add_packed_git(the_repository, full_path, full_path_len, 0);
if (!p) { if (!p) {
warning(_("failed to add packfile '%s'"), warning(_("failed to add packfile '%s'"),
full_path); full_path);

2
midx.c
View File

@ -464,7 +464,7 @@ int prepare_midx_pack(struct repository *r, struct multi_pack_index *m,
strhash(key.buf), key.buf, strhash(key.buf), key.buf,
struct packed_git, packmap_ent); struct packed_git, packmap_ent);
if (!p) { if (!p) {
p = add_packed_git(pack_name.buf, pack_name.len, m->local); p = add_packed_git(r, pack_name.buf, pack_name.len, m->local);
if (p) { if (p) {
install_packed_git(r, p); install_packed_git(r, p);
list_add_tail(&p->mru, &r->objects->packed_git_mru); list_add_tail(&p->mru, &r->objects->packed_git_mru);

View File

@ -10,6 +10,7 @@
struct oidmap; struct oidmap;
struct oidtree; struct oidtree;
struct strbuf; struct strbuf;
struct repository;
struct object_directory { struct object_directory {
struct object_directory *next; struct object_directory *next;
@ -135,6 +136,10 @@ struct packed_git {
*/ */
const uint32_t *mtimes_map; const uint32_t *mtimes_map;
size_t mtimes_size; size_t mtimes_size;
/* repo denotes the repository this packfile belongs to */
struct repository *repo;
/* something like ".git/objects/pack/xxxxx.pack" */ /* something like ".git/objects/pack/xxxxx.pack" */
char pack_name[FLEX_ARRAY]; /* more */ char pack_name[FLEX_ARRAY]; /* more */
}; };

View File

@ -217,11 +217,12 @@ uint32_t get_pack_fanout(struct packed_git *p, uint32_t value)
return ntohl(level1_ofs[value]); return ntohl(level1_ofs[value]);
} }
static struct packed_git *alloc_packed_git(int extra) static struct packed_git *alloc_packed_git(struct repository *r, int extra)
{ {
struct packed_git *p = xmalloc(st_add(sizeof(*p), extra)); struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
memset(p, 0, sizeof(*p)); memset(p, 0, sizeof(*p));
p->pack_fd = -1; p->pack_fd = -1;
p->repo = r;
return p; return p;
} }
@ -233,11 +234,12 @@ static char *pack_path_from_idx(const char *idx_path)
return xstrfmt("%.*s.pack", (int)len, idx_path); return xstrfmt("%.*s.pack", (int)len, idx_path);
} }
struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path) struct packed_git *parse_pack_index(struct repository *r, unsigned char *sha1,
const char *idx_path)
{ {
char *path = pack_path_from_idx(idx_path); char *path = pack_path_from_idx(idx_path);
size_t alloc = st_add(strlen(path), 1); size_t alloc = st_add(strlen(path), 1);
struct packed_git *p = alloc_packed_git(alloc); struct packed_git *p = alloc_packed_git(r, alloc);
memcpy(p->pack_name, path, alloc); /* includes NUL */ memcpy(p->pack_name, path, alloc); /* includes NUL */
free(path); free(path);
@ -703,7 +705,8 @@ void unuse_pack(struct pack_window **w_cursor)
} }
} }
struct packed_git *add_packed_git(const char *path, size_t path_len, int local) struct packed_git *add_packed_git(struct repository *r, const char *path,
size_t path_len, int local)
{ {
struct stat st; struct stat st;
size_t alloc; size_t alloc;
@ -721,7 +724,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
* the use xsnprintf double-checks that) * the use xsnprintf double-checks that)
*/ */
alloc = st_add3(path_len, strlen(".promisor"), 1); alloc = st_add3(path_len, strlen(".promisor"), 1);
p = alloc_packed_git(alloc); p = alloc_packed_git(r, alloc);
memcpy(p->pack_name, path, path_len); memcpy(p->pack_name, path, path_len);
xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep"); 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,
/* Don't reopen a pack we already have. */ /* Don't reopen a pack we already have. */
if (!hashmap_get(&data->r->objects->pack_map, &hent, pack_name)) { if (!hashmap_get(&data->r->objects->pack_map, &hent, pack_name)) {
p = add_packed_git(full_name, full_name_len, data->local); p = add_packed_git(data->r, full_name, full_name_len, data->local);
if (p) if (p)
install_packed_git(data->r, p); install_packed_git(data->r, p);
} }

View File

@ -46,7 +46,8 @@ const char *pack_basename(struct packed_git *p);
* and does not add the resulting packed_git struct to the internal list of * and does not add the resulting packed_git struct to the internal list of
* packs. You probably want add_packed_git() instead. * packs. You probably want add_packed_git() instead.
*/ */
struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path); struct packed_git *parse_pack_index(struct repository *r, unsigned char *sha1,
const char *idx_path);
typedef void each_file_in_pack_dir_fn(const char *full_path, size_t full_path_len, typedef void each_file_in_pack_dir_fn(const char *full_path, size_t full_path_len,
const char *file_name, void *data); const char *file_name, void *data);
@ -113,7 +114,8 @@ void close_pack(struct packed_git *);
void close_object_store(struct raw_object_store *o); void close_object_store(struct raw_object_store *o);
void unuse_pack(struct pack_window **); void unuse_pack(struct pack_window **);
void clear_delta_base_cache(void); void clear_delta_base_cache(void);
struct packed_git *add_packed_git(const char *path, size_t path_len, int local); struct packed_git *add_packed_git(struct repository *r, const char *path,
size_t path_len, int local);
/* /*
* Unlink the .pack and associated extension files. * Unlink the .pack and associated extension files.