aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2026-01-15 05:50:16 -0800
committerJunio C Hamano <gitster@pobox.com>2026-01-15 05:50:16 -0800
commitec16dde5c8c08fe6f26a2183a038e02ea7b2b25f (patch)
treefef31f79ec514c9429614e2518ea48874f2d0801
parentc8e1706e8dbe7a511c1fd85d5147a1722b4080f1 (diff)
parenta282a8f163fa70f9eacc880e6188141cef917058 (diff)
downloadgit-ec16dde5c8c08fe6f26a2183a038e02ea7b2b25f.tar.xz
Merge branch 'ps/packfile-store-in-odb-source' into ps/odb-for-each-object
* ps/packfile-store-in-odb-source: packfile: move MIDX into packfile store packfile: refactor `find_pack_entry()` to work on the packfile store packfile: inline `find_kept_pack_entry()` packfile: only prepare owning store in `packfile_store_prepare()` packfile: only prepare owning store in `packfile_store_get_packs()` packfile: move packfile store into object source packfile: refactor misleading code when unusing pack windows packfile: refactor kept-pack cache to work with packfile stores packfile: pass source to `prepare_pack()` packfile: create store via its owning source odb: properly close sources before freeing them builtin/gc: fix condition for whether to write commit graphs
-rw-r--r--builtin/fast-import.c37
-rw-r--r--builtin/gc.c8
-rw-r--r--builtin/grep.c10
-rw-r--r--builtin/index-pack.c2
-rw-r--r--builtin/pack-objects.c104
-rw-r--r--http.c2
-rw-r--r--midx.c19
-rw-r--r--odb.c46
-rw-r--r--odb.h11
-rw-r--r--odb/streaming.c9
-rw-r--r--packfile.c227
-rw-r--r--packfile.h102
-rw-r--r--reachable.c2
-rw-r--r--revision.c8
-rwxr-xr-xt/t7900-maintenance.sh25
15 files changed, 359 insertions, 253 deletions
diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index 7849005ccb..b8a7757cfd 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -900,7 +900,7 @@ static void end_packfile(void)
idx_name = keep_pack(create_index());
/* Register the packfile with core git's machinery. */
- new_p = packfile_store_load_pack(pack_data->repo->objects->packfiles,
+ new_p = packfile_store_load_pack(pack_data->repo->objects->sources->packfiles,
idx_name, 1);
if (!new_p)
die(_("core Git rejected index %s"), idx_name);
@@ -955,7 +955,7 @@ static int store_object(
struct object_id *oidout,
uintmax_t mark)
{
- struct packfile_store *packs = the_repository->objects->packfiles;
+ struct odb_source *source;
void *out, *delta;
struct object_entry *e;
unsigned char hdr[96];
@@ -979,7 +979,11 @@ static int store_object(
if (e->idx.offset) {
duplicate_count_by_type[type]++;
return 1;
- } else if (packfile_list_find_oid(packfile_store_get_packs(packs), &oid)) {
+ }
+
+ for (source = the_repository->objects->sources; source; source = source->next) {
+ if (!packfile_list_find_oid(packfile_store_get_packs(source->packfiles), &oid))
+ continue;
e->type = type;
e->pack_id = MAX_PACK_ID;
e->idx.offset = 1; /* just not zero! */
@@ -1096,10 +1100,10 @@ static void truncate_pack(struct hashfile_checkpoint *checkpoint)
static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
{
- struct packfile_store *packs = the_repository->objects->packfiles;
size_t in_sz = 64 * 1024, out_sz = 64 * 1024;
unsigned char *in_buf = xmalloc(in_sz);
unsigned char *out_buf = xmalloc(out_sz);
+ struct odb_source *source;
struct object_entry *e;
struct object_id oid;
unsigned long hdrlen;
@@ -1179,24 +1183,29 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
if (e->idx.offset) {
duplicate_count_by_type[OBJ_BLOB]++;
truncate_pack(&checkpoint);
+ goto out;
+ }
- } else if (packfile_list_find_oid(packfile_store_get_packs(packs), &oid)) {
+ for (source = the_repository->objects->sources; source; source = source->next) {
+ if (!packfile_list_find_oid(packfile_store_get_packs(source->packfiles), &oid))
+ continue;
e->type = OBJ_BLOB;
e->pack_id = MAX_PACK_ID;
e->idx.offset = 1; /* just not zero! */
duplicate_count_by_type[OBJ_BLOB]++;
truncate_pack(&checkpoint);
-
- } else {
- e->depth = 0;
- e->type = OBJ_BLOB;
- e->pack_id = pack_id;
- e->idx.offset = offset;
- e->idx.crc32 = crc32_end(pack_file);
- object_count++;
- object_count_by_type[OBJ_BLOB]++;
+ goto out;
}
+ e->depth = 0;
+ e->type = OBJ_BLOB;
+ e->pack_id = pack_id;
+ e->idx.offset = offset;
+ e->idx.crc32 = crc32_end(pack_file);
+ object_count++;
+ object_count_by_type[OBJ_BLOB]++;
+
+out:
free(in_buf);
free(out_buf);
}
diff --git a/builtin/gc.c b/builtin/gc.c
index 92c6e7b954..17ff68cbd9 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -1130,8 +1130,10 @@ static int dfs_on_ref(const struct reference *ref, void *cb_data)
return 0;
commit = lookup_commit(the_repository, maybe_peeled);
- if (!commit)
+ if (!commit || commit->object.flags & SEEN)
return 0;
+ commit->object.flags |= SEEN;
+
if (repo_parse_commit(the_repository, commit) ||
commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
return 0;
@@ -1141,7 +1143,7 @@ static int dfs_on_ref(const struct reference *ref, void *cb_data)
if (data->num_not_in_graph >= data->limit)
return 1;
- commit_list_append(commit, &stack);
+ commit_list_insert(commit, &stack);
while (!result && stack) {
struct commit_list *parent;
@@ -1162,7 +1164,7 @@ static int dfs_on_ref(const struct reference *ref, void *cb_data)
break;
}
- commit_list_append(parent->item, &stack);
+ commit_list_insert(parent->item, &stack);
}
}
diff --git a/builtin/grep.c b/builtin/grep.c
index 53cccf2d25..5b8b87b1ac 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -1213,8 +1213,14 @@ int cmd_grep(int argc,
*/
if (recurse_submodules)
repo_read_gitmodules(the_repository, 1);
- if (startup_info->have_repository)
- packfile_store_prepare(the_repository->objects->packfiles);
+
+ if (startup_info->have_repository) {
+ struct odb_source *source;
+
+ odb_prepare_alternates(the_repository->objects);
+ for (source = the_repository->objects->sources; source; source = source->next)
+ packfile_store_prepare(source->packfiles);
+ }
start_threads(&opt);
} else {
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index a7e901e49c..b67fb0256c 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1638,7 +1638,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
hash, "idx", 1);
if (do_fsck_object && startup_info->have_repository)
- packfile_store_load_pack(the_repository->objects->packfiles,
+ packfile_store_load_pack(the_repository->objects->sources->packfiles,
final_index_name, 0);
if (!from_stdin) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index c261b48284..6ee31d48c9 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1529,49 +1529,53 @@ static int want_cruft_object_mtime(struct repository *r,
const struct object_id *oid,
unsigned flags, uint32_t mtime)
{
- struct packed_git **cache;
+ struct odb_source *source;
- for (cache = kept_pack_cache(r, flags); *cache; cache++) {
- struct packed_git *p = *cache;
- off_t ofs;
- uint32_t candidate_mtime;
+ for (source = r->objects->sources; source; source = source->next) {
+ struct packed_git **cache = packfile_store_get_kept_pack_cache(source->packfiles, flags);
- ofs = find_pack_entry_one(oid, p);
- if (!ofs)
- continue;
+ for (; *cache; cache++) {
+ struct packed_git *p = *cache;
+ off_t ofs;
+ uint32_t candidate_mtime;
- /*
- * We have a copy of the object 'oid' in a non-cruft
- * pack. We can avoid packing an additional copy
- * regardless of what the existing copy's mtime is since
- * it is outside of a cruft pack.
- */
- if (!p->is_cruft)
- return 0;
-
- /*
- * If we have a copy of the object 'oid' in a cruft
- * pack, then either read the cruft pack's mtime for
- * that object, or, if that can't be loaded, assume the
- * pack's mtime itself.
- */
- if (!load_pack_mtimes(p)) {
- uint32_t pos;
- if (offset_to_pack_pos(p, ofs, &pos) < 0)
+ ofs = find_pack_entry_one(oid, p);
+ if (!ofs)
continue;
- candidate_mtime = nth_packed_mtime(p, pos);
- } else {
- candidate_mtime = p->mtime;
- }
- /*
- * We have a surviving copy of the object in a cruft
- * pack whose mtime is greater than or equal to the one
- * we are considering. We can thus avoid packing an
- * additional copy of that object.
- */
- if (mtime <= candidate_mtime)
- return 0;
+ /*
+ * We have a copy of the object 'oid' in a non-cruft
+ * pack. We can avoid packing an additional copy
+ * regardless of what the existing copy's mtime is since
+ * it is outside of a cruft pack.
+ */
+ if (!p->is_cruft)
+ return 0;
+
+ /*
+ * If we have a copy of the object 'oid' in a cruft
+ * pack, then either read the cruft pack's mtime for
+ * that object, or, if that can't be loaded, assume the
+ * pack's mtime itself.
+ */
+ if (!load_pack_mtimes(p)) {
+ uint32_t pos;
+ if (offset_to_pack_pos(p, ofs, &pos) < 0)
+ continue;
+ candidate_mtime = nth_packed_mtime(p, pos);
+ } else {
+ candidate_mtime = p->mtime;
+ }
+
+ /*
+ * We have a surviving copy of the object in a cruft
+ * pack whose mtime is greater than or equal to the one
+ * we are considering. We can thus avoid packing an
+ * additional copy of that object.
+ */
+ if (mtime <= candidate_mtime)
+ return 0;
+ }
}
return -1;
@@ -1624,9 +1628,9 @@ static int want_found_object(const struct object_id *oid, int exclude,
*/
unsigned flags = 0;
if (ignore_packed_keep_on_disk)
- flags |= ON_DISK_KEEP_PACKS;
+ flags |= KEPT_PACK_ON_DISK;
if (ignore_packed_keep_in_core)
- flags |= IN_CORE_KEEP_PACKS;
+ flags |= KEPT_PACK_IN_CORE;
/*
* If the object is in a pack that we want to ignore, *and* we
@@ -1749,13 +1753,15 @@ static int want_object_in_pack_mtime(const struct object_id *oid,
}
}
- for (e = the_repository->objects->packfiles->packs.head; e; e = e->next) {
- struct packed_git *p = e->pack;
- want = want_object_in_pack_one(p, oid, exclude, found_pack, found_offset, found_mtime);
- if (!exclude && want > 0)
- packfile_list_prepend(&the_repository->objects->packfiles->packs, p);
- if (want != -1)
- return want;
+ for (source = the_repository->objects->sources; source; source = source->next) {
+ for (e = source->packfiles->packs.head; e; e = e->next) {
+ struct packed_git *p = e->pack;
+ want = want_object_in_pack_one(p, oid, exclude, found_pack, found_offset, found_mtime);
+ if (!exclude && want > 0)
+ packfile_list_prepend(&source->packfiles->packs, p);
+ if (want != -1)
+ return want;
+ }
}
if (uri_protocols.nr) {
@@ -3931,7 +3937,7 @@ static void read_stdin_packs(enum stdin_packs_mode mode, int rev_list_unpacked)
* an optimization during delta selection.
*/
revs.no_kept_objects = 1;
- revs.keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
+ revs.keep_pack_cache_flags |= KEPT_PACK_IN_CORE;
revs.blob_objects = 1;
revs.tree_objects = 1;
revs.tag_objects = 1;
@@ -4030,7 +4036,7 @@ static void show_cruft_commit(struct commit *commit, void *data)
static int cruft_include_check_obj(struct object *obj, void *data UNUSED)
{
- return !has_object_kept_pack(to_pack.repo, &obj->oid, IN_CORE_KEEP_PACKS);
+ return !has_object_kept_pack(to_pack.repo, &obj->oid, KEPT_PACK_IN_CORE);
}
static int cruft_include_check(struct commit *commit, void *data)
diff --git a/http.c b/http.c
index 41f850db16..7815f144de 100644
--- a/http.c
+++ b/http.c
@@ -2544,7 +2544,7 @@ void http_install_packfile(struct packed_git *p,
struct packfile_list *list_to_remove_from)
{
packfile_list_remove(list_to_remove_from, p);
- packfile_store_add_pack(the_repository->objects->packfiles, p);
+ packfile_store_add_pack(the_repository->objects->sources->packfiles, p);
}
struct http_pack_request *new_http_pack_request(
diff --git a/midx.c b/midx.c
index b681b18fc1..a75ea99a0d 100644
--- a/midx.c
+++ b/midx.c
@@ -95,8 +95,8 @@ static int midx_read_object_offsets(const unsigned char *chunk_start,
struct multi_pack_index *get_multi_pack_index(struct odb_source *source)
{
- packfile_store_prepare(source->odb->packfiles);
- return source->midx;
+ packfile_store_prepare(source->packfiles);
+ return source->packfiles->midx;
}
static struct multi_pack_index *load_multi_pack_index_one(struct odb_source *source,
@@ -447,7 +447,6 @@ static uint32_t midx_for_pack(struct multi_pack_index **_m,
int prepare_midx_pack(struct multi_pack_index *m,
uint32_t pack_int_id)
{
- struct repository *r = m->source->odb->repo;
struct strbuf pack_name = STRBUF_INIT;
struct packed_git *p;
@@ -460,7 +459,7 @@ int prepare_midx_pack(struct multi_pack_index *m,
strbuf_addf(&pack_name, "%s/pack/%s", m->source->path,
m->pack_names[pack_int_id]);
- p = packfile_store_load_pack(r->objects->packfiles,
+ p = packfile_store_load_pack(m->source->packfiles,
pack_name.buf, m->source->local);
strbuf_release(&pack_name);
@@ -710,12 +709,12 @@ int prepare_multi_pack_index_one(struct odb_source *source)
if (!r->settings.core_multi_pack_index)
return 0;
- if (source->midx)
+ if (source->packfiles->midx)
return 1;
- source->midx = load_multi_pack_index(source);
+ source->packfiles->midx = load_multi_pack_index(source);
- return !!source->midx;
+ return !!source->packfiles->midx;
}
int midx_checksum_valid(struct multi_pack_index *m)
@@ -804,9 +803,9 @@ void clear_midx_file(struct repository *r)
struct odb_source *source;
for (source = r->objects->sources; source; source = source->next) {
- if (source->midx)
- close_midx(source->midx);
- source->midx = NULL;
+ if (source->packfiles->midx)
+ close_midx(source->packfiles->midx);
+ source->packfiles->midx = NULL;
}
}
diff --git a/odb.c b/odb.c
index ffd78e1c46..ac70b6a099 100644
--- a/odb.c
+++ b/odb.c
@@ -229,6 +229,7 @@ static struct odb_source *odb_source_new(struct object_database *odb,
source->local = local;
source->path = xstrdup(path);
source->loose = odb_source_loose_new(source);
+ source->packfiles = packfile_store_new(source);
return source;
}
@@ -376,6 +377,7 @@ static void odb_source_free(struct odb_source *source)
{
free(source->path);
odb_source_loose_free(source->loose);
+ packfile_store_free(source->packfiles);
free(source);
}
@@ -710,19 +712,19 @@ static int do_oid_object_info_extended(struct object_database *odb,
while (1) {
struct odb_source *source;
- if (!packfile_store_read_object_info(odb->packfiles, real, oi, flags))
- return 0;
-
/* Most likely it's a loose object. */
- for (source = odb->sources; source; source = source->next)
- if (!odb_source_loose_read_object_info(source, real, oi, flags))
+ for (source = odb->sources; source; source = source->next) {
+ if (!packfile_store_read_object_info(source->packfiles, real, oi, flags) ||
+ !odb_source_loose_read_object_info(source, real, oi, flags))
return 0;
+ }
/* Not a loose object; someone else may have just packed it. */
if (!(flags & OBJECT_INFO_QUICK)) {
odb_reprepare(odb->repo->objects);
- if (!packfile_store_read_object_info(odb->packfiles, real, oi, flags))
- return 0;
+ for (source = odb->sources; source; source = source->next)
+ if (!packfile_store_read_object_info(source->packfiles, real, oi, flags))
+ return 0;
}
/*
@@ -981,13 +983,14 @@ int odb_freshen_object(struct object_database *odb,
{
struct odb_source *source;
- if (packfile_store_freshen_object(odb->packfiles, oid))
- return 1;
-
odb_prepare_alternates(odb);
- for (source = odb->sources; source; source = source->next)
+ for (source = odb->sources; source; source = source->next) {
+ if (packfile_store_freshen_object(source->packfiles, oid))
+ return 1;
+
if (odb_source_loose_freshen_object(source, oid))
return 1;
+ }
return 0;
}
@@ -1062,7 +1065,6 @@ struct object_database *odb_new(struct repository *repo,
memset(o, 0, sizeof(*o));
o->repo = repo;
- o->packfiles = packfile_store_new(o);
pthread_mutex_init(&o->replace_mutex, NULL);
string_list_init_dup(&o->submodule_source_paths);
@@ -1082,15 +1084,8 @@ struct object_database *odb_new(struct repository *repo,
void odb_close(struct object_database *o)
{
struct odb_source *source;
-
- packfile_store_close(o->packfiles);
-
- for (source = o->sources; source; source = source->next) {
- if (source->midx)
- close_midx(source->midx);
- source->midx = NULL;
- }
-
+ for (source = o->sources; source; source = source->next)
+ packfile_store_close(source->packfiles);
close_commit_graph(o);
}
@@ -1117,14 +1112,13 @@ void odb_free(struct object_database *o)
oidmap_clear(&o->replace_map, 1);
pthread_mutex_destroy(&o->replace_mutex);
+ odb_close(o);
odb_free_sources(o);
for (size_t i = 0; i < o->cached_object_nr; i++)
free((char *) o->cached_objects[i].value.buf);
free(o->cached_objects);
- odb_close(o);
- packfile_store_free(o->packfiles);
string_list_clear(&o->submodule_source_paths, 0);
chdir_notify_unregister(NULL, odb_update_commondir, o);
@@ -1147,13 +1141,13 @@ void odb_reprepare(struct object_database *o)
o->loaded_alternates = 0;
odb_prepare_alternates(o);
- for (source = o->sources; source; source = source->next)
+ for (source = o->sources; source; source = source->next) {
odb_source_loose_reprepare(source);
+ packfile_store_reprepare(source->packfiles);
+ }
o->approximate_object_count_valid = 0;
- packfile_store_reprepare(o->packfiles);
-
obj_read_unlock();
}
diff --git a/odb.h b/odb.h
index afae5e5c01..bab07755f4 100644
--- a/odb.h
+++ b/odb.h
@@ -51,12 +51,8 @@ struct odb_source {
/* Private state for loose objects. */
struct odb_source_loose *loose;
- /*
- * private data
- *
- * should only be accessed directly by packfile.c and midx.c
- */
- struct multi_pack_index *midx;
+ /* Should only be accessed directly by packfile.c and midx.c. */
+ struct packfile_store *packfiles;
/*
* Figure out whether this is the local source of the owning
@@ -128,9 +124,6 @@ struct object_database {
struct commit_graph *commit_graph;
unsigned commit_graph_attempted : 1; /* if loading has been attempted */
- /* Should only be accessed directly by packfile.c and midx.c. */
- struct packfile_store *packfiles;
-
/*
* This is meant to hold a *small* number of objects that you would
* want odb_read_object() to be able to return, but yet you do not want
diff --git a/odb/streaming.c b/odb/streaming.c
index 745cd486fb..4a4474f891 100644
--- a/odb/streaming.c
+++ b/odb/streaming.c
@@ -185,13 +185,12 @@ static int istream_source(struct odb_read_stream **out,
{
struct odb_source *source;
- if (!packfile_store_read_object_stream(out, odb->packfiles, oid))
- return 0;
-
odb_prepare_alternates(odb);
- for (source = odb->sources; source; source = source->next)
- if (!odb_source_loose_read_object_stream(out, source, oid))
+ for (source = odb->sources; source; source = source->next) {
+ if (!packfile_store_read_object_stream(out, source->packfiles, oid) ||
+ !odb_source_loose_read_object_stream(out, source, oid))
return 0;
+ }
return open_istream_incore(out, odb, oid);
}
diff --git a/packfile.c b/packfile.c
index 79cea97f35..402c3b5dc7 100644
--- a/packfile.c
+++ b/packfile.c
@@ -355,16 +355,17 @@ static void scan_windows(struct packed_git *p,
}
}
-static int unuse_one_window(struct packed_git *current)
+static int unuse_one_window(struct object_database *odb)
{
+ struct odb_source *source;
struct packfile_list_entry *e;
struct packed_git *lru_p = NULL;
struct pack_window *lru_w = NULL, *lru_l = NULL;
- if (current)
- scan_windows(current, &lru_p, &lru_w, &lru_l);
- for (e = current->repo->objects->packfiles->packs.head; e; e = e->next)
- scan_windows(e->pack, &lru_p, &lru_w, &lru_l);
+ for (source = odb->sources; source; source = source->next)
+ for (e = source->packfiles->packs.head; e; e = e->next)
+ scan_windows(e->pack, &lru_p, &lru_w, &lru_l);
+
if (lru_p) {
munmap(lru_w->base, lru_w->len);
pack_mapped -= lru_w->len;
@@ -529,15 +530,18 @@ static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struc
static int close_one_pack(struct repository *r)
{
+ struct odb_source *source;
struct packfile_list_entry *e;
struct packed_git *lru_p = NULL;
struct pack_window *mru_w = NULL;
int accept_windows_inuse = 1;
- for (e = r->objects->packfiles->packs.head; e; e = e->next) {
- if (e->pack->pack_fd == -1)
- continue;
- find_lru_pack(e->pack, &lru_p, &mru_w, &accept_windows_inuse);
+ for (source = r->objects->sources; source; source = source->next) {
+ for (e = source->packfiles->packs.head; e; e = e->next) {
+ if (e->pack->pack_fd == -1)
+ continue;
+ find_lru_pack(e->pack, &lru_p, &mru_w, &accept_windows_inuse);
+ }
}
if (lru_p)
@@ -740,8 +744,8 @@ unsigned char *use_pack(struct packed_git *p,
win->len = (size_t)len;
pack_mapped += win->len;
- while (settings->packed_git_limit < pack_mapped
- && unuse_one_window(p))
+ while (settings->packed_git_limit < pack_mapped &&
+ unuse_one_window(p->repo->objects))
; /* nothing */
win->base = xmmap_gently(NULL, win->len,
PROT_READ, MAP_PRIVATE,
@@ -876,7 +880,7 @@ struct packed_git *packfile_store_load_pack(struct packfile_store *store,
p = strmap_get(&store->packs_by_path, key.buf);
if (!p) {
- p = add_packed_git(store->odb->repo, idx_path,
+ p = add_packed_git(store->source->odb->repo, idx_path,
strlen(idx_path), local);
if (p)
packfile_store_add_pack(store, p);
@@ -975,10 +979,8 @@ void for_each_file_in_pack_dir(const char *objdir,
}
struct prepare_pack_data {
- struct repository *r;
+ struct odb_source *source;
struct string_list *garbage;
- int local;
- struct multi_pack_index *m;
};
static void prepare_pack(const char *full_name, size_t full_name_len,
@@ -988,10 +990,11 @@ static void prepare_pack(const char *full_name, size_t full_name_len,
size_t base_len = full_name_len;
if (strip_suffix_mem(full_name, &base_len, ".idx") &&
- !(data->m && midx_contains_pack(data->m, file_name))) {
+ !(data->source->packfiles->midx &&
+ midx_contains_pack(data->source->packfiles->midx, file_name))) {
char *trimmed_path = xstrndup(full_name, full_name_len);
- packfile_store_load_pack(data->r->objects->packfiles,
- trimmed_path, data->local);
+ packfile_store_load_pack(data->source->packfiles,
+ trimmed_path, data->source->local);
free(trimmed_path);
}
@@ -1020,10 +1023,8 @@ static void prepare_packed_git_one(struct odb_source *source)
{
struct string_list garbage = STRING_LIST_INIT_DUP;
struct prepare_pack_data data = {
- .m = source->midx,
- .r = source->odb->repo,
+ .source = source,
.garbage = &garbage,
- .local = source->local,
};
for_each_file_in_pack_dir(source->path, prepare_pack, &data);
@@ -1063,16 +1064,11 @@ static int sort_pack(const struct packfile_list_entry *a,
void packfile_store_prepare(struct packfile_store *store)
{
- struct odb_source *source;
-
if (store->initialized)
return;
- odb_prepare_alternates(store->odb);
- for (source = store->odb->sources; source; source = source->next) {
- prepare_multi_pack_index_one(source);
- prepare_packed_git_one(source);
- }
+ prepare_multi_pack_index_one(store->source);
+ prepare_packed_git_one(store->source);
sort_packs(&store->packs.head, sort_pack);
for (struct packfile_list_entry *e = store->packs.head; e; e = e->next)
@@ -1092,10 +1088,8 @@ struct packfile_list_entry *packfile_store_get_packs(struct packfile_store *stor
{
packfile_store_prepare(store);
- for (struct odb_source *source = store->odb->sources; source; source = source->next) {
- struct multi_pack_index *m = source->midx;
- if (!m)
- continue;
+ if (store->midx) {
+ struct multi_pack_index *m = store->midx;
for (uint32_t i = 0; i < m->num_packs + m->num_packs_in_base; i++)
prepare_midx_pack(m, i);
}
@@ -1250,11 +1244,15 @@ void mark_bad_packed_object(struct packed_git *p, const struct object_id *oid)
const struct packed_git *has_packed_and_bad(struct repository *r,
const struct object_id *oid)
{
- struct packfile_list_entry *e;
+ struct odb_source *source;
+
+ for (source = r->objects->sources; source; source = source->next) {
+ struct packfile_list_entry *e;
+ for (e = source->packfiles->packs.head; e; e = e->next)
+ if (oidset_contains(&e->pack->bad_objects, oid))
+ return e->pack;
+ }
- for (e = r->objects->packfiles->packs.head; e; e = e->next)
- if (oidset_contains(&e->pack->bad_objects, oid))
- return e->pack;
return NULL;
}
@@ -2109,30 +2107,26 @@ static int fill_pack_entry(const struct object_id *oid,
return 1;
}
-static int find_pack_entry(struct repository *r,
+static int find_pack_entry(struct packfile_store *store,
const struct object_id *oid,
struct pack_entry *e)
{
struct packfile_list_entry *l;
- packfile_store_prepare(r->objects->packfiles);
-
- for (struct odb_source *source = r->objects->sources; source; source = source->next)
- if (source->midx && fill_midx_entry(source->midx, oid, e))
- return 1;
-
- if (!r->objects->packfiles->packs.head)
- return 0;
+ packfile_store_prepare(store);
+ if (store->midx && fill_midx_entry(store->midx, oid, e))
+ return 1;
- for (l = r->objects->packfiles->packs.head; l; l = l->next) {
+ for (l = store->packs.head; l; l = l->next) {
struct packed_git *p = l->pack;
if (!p->multi_pack_index && fill_pack_entry(oid, e, p)) {
- if (!r->objects->packfiles->skip_mru_updates)
- packfile_list_prepend(&r->objects->packfiles->packs, p);
+ if (!store->skip_mru_updates)
+ packfile_list_prepend(&store->packs, p);
return 1;
}
}
+
return 0;
}
@@ -2140,7 +2134,7 @@ int packfile_store_freshen_object(struct packfile_store *store,
const struct object_id *oid)
{
struct pack_entry e;
- if (!find_pack_entry(store->odb->repo, oid, &e))
+ if (!find_pack_entry(store, oid, &e))
return 0;
if (e.p->is_cruft)
return 0;
@@ -2160,7 +2154,7 @@ int packfile_store_read_object_info(struct packfile_store *store,
struct pack_entry e;
int ret;
- if (!find_pack_entry(store->odb->repo, oid, &e))
+ if (!find_pack_entry(store, oid, &e))
return 1;
/*
@@ -2179,25 +2173,26 @@ int packfile_store_read_object_info(struct packfile_store *store,
return 0;
}
-static void maybe_invalidate_kept_pack_cache(struct repository *r,
+static void maybe_invalidate_kept_pack_cache(struct packfile_store *store,
unsigned flags)
{
- if (!r->objects->packfiles->kept_cache.packs)
+ if (!store->kept_cache.packs)
return;
- if (r->objects->packfiles->kept_cache.flags == flags)
+ if (store->kept_cache.flags == flags)
return;
- FREE_AND_NULL(r->objects->packfiles->kept_cache.packs);
- r->objects->packfiles->kept_cache.flags = 0;
+ FREE_AND_NULL(store->kept_cache.packs);
+ store->kept_cache.flags = 0;
}
-struct packed_git **kept_pack_cache(struct repository *r, unsigned flags)
+struct packed_git **packfile_store_get_kept_pack_cache(struct packfile_store *store,
+ unsigned flags)
{
- maybe_invalidate_kept_pack_cache(r, flags);
+ maybe_invalidate_kept_pack_cache(store, flags);
- if (!r->objects->packfiles->kept_cache.packs) {
+ if (!store->kept_cache.packs) {
struct packed_git **packs = NULL;
+ struct packfile_list_entry *e;
size_t nr = 0, alloc = 0;
- struct packed_git *p;
/*
* We want "all" packs here, because we need to cover ones that
@@ -2207,9 +2202,11 @@ struct packed_git **kept_pack_cache(struct repository *r, unsigned flags)
* covers, one kept and one not kept, but the midx returns only
* the non-kept version.
*/
- repo_for_each_pack(r, p) {
- if ((p->pack_keep && (flags & ON_DISK_KEEP_PACKS)) ||
- (p->pack_keep_in_core && (flags & IN_CORE_KEEP_PACKS))) {
+ for (e = packfile_store_get_packs(store); e; e = e->next) {
+ struct packed_git *p = e->pack;
+
+ if ((p->pack_keep && (flags & KEPT_PACK_ON_DISK)) ||
+ (p->pack_keep_in_core && (flags & KEPT_PACK_IN_CORE))) {
ALLOC_GROW(packs, nr + 1, alloc);
packs[nr++] = p;
}
@@ -2217,40 +2214,47 @@ struct packed_git **kept_pack_cache(struct repository *r, unsigned flags)
ALLOC_GROW(packs, nr + 1, alloc);
packs[nr] = NULL;
- r->objects->packfiles->kept_cache.packs = packs;
- r->objects->packfiles->kept_cache.flags = flags;
+ store->kept_cache.packs = packs;
+ store->kept_cache.flags = flags;
}
- return r->objects->packfiles->kept_cache.packs;
+ return store->kept_cache.packs;
}
-int find_kept_pack_entry(struct repository *r,
- const struct object_id *oid,
- unsigned flags,
- struct pack_entry *e)
+int has_object_pack(struct repository *r, const struct object_id *oid)
{
- struct packed_git **cache;
+ struct odb_source *source;
+ struct pack_entry e;
- for (cache = kept_pack_cache(r, flags); *cache; cache++) {
- struct packed_git *p = *cache;
- if (fill_pack_entry(oid, e, p))
- return 1;
+ odb_prepare_alternates(r->objects);
+ for (source = r->objects->sources; source; source = source->next) {
+ int ret = find_pack_entry(source->packfiles, oid, &e);
+ if (ret)
+ return ret;
}
return 0;
}
-int has_object_pack(struct repository *r, const struct object_id *oid)
-{
- struct pack_entry e;
- return find_pack_entry(r, oid, &e);
-}
-
int has_object_kept_pack(struct repository *r, const struct object_id *oid,
unsigned flags)
{
+ struct odb_source *source;
struct pack_entry e;
- return find_kept_pack_entry(r, oid, flags, &e);
+
+ for (source = r->objects->sources; source; source = source->next) {
+ struct packed_git **cache;
+
+ cache = packfile_store_get_kept_pack_cache(source->packfiles, flags);
+
+ for (; *cache; cache++) {
+ struct packed_git *p = *cache;
+ if (fill_pack_entry(oid, &e, p))
+ return 1;
+ }
+ }
+
+ return 0;
}
int for_each_object_in_pack(struct packed_git *p,
@@ -2300,32 +2304,46 @@ int for_each_object_in_pack(struct packed_git *p,
int for_each_packed_object(struct repository *repo, each_packed_object_fn cb,
void *data, enum for_each_object_flags flags)
{
- struct packed_git *p;
+ struct odb_source *source;
int r = 0;
int pack_errors = 0;
- repo->objects->packfiles->skip_mru_updates = true;
- repo_for_each_pack(repo, p) {
- if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
- continue;
- if ((flags & FOR_EACH_OBJECT_PROMISOR_ONLY) &&
- !p->pack_promisor)
- continue;
- if ((flags & FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS) &&
- p->pack_keep_in_core)
- continue;
- if ((flags & FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS) &&
- p->pack_keep)
- continue;
- if (open_pack_index(p)) {
- pack_errors = 1;
- continue;
+ odb_prepare_alternates(repo->objects);
+
+ for (source = repo->objects->sources; source; source = source->next) {
+ struct packfile_list_entry *e;
+
+ source->packfiles->skip_mru_updates = true;
+
+ for (e = packfile_store_get_packs(source->packfiles); e; e = e->next) {
+ struct packed_git *p = e->pack;
+
+ if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
+ continue;
+ if ((flags & FOR_EACH_OBJECT_PROMISOR_ONLY) &&
+ !p->pack_promisor)
+ continue;
+ if ((flags & FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS) &&
+ p->pack_keep_in_core)
+ continue;
+ if ((flags & FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS) &&
+ p->pack_keep)
+ continue;
+ if (open_pack_index(p)) {
+ pack_errors = 1;
+ continue;
+ }
+
+ r = for_each_object_in_pack(p, cb, data, flags);
+ if (r)
+ break;
}
- r = for_each_object_in_pack(p, cb, data, flags);
+
+ source->packfiles->skip_mru_updates = false;
+
if (r)
break;
}
- repo->objects->packfiles->skip_mru_updates = false;
return r ? r : pack_errors;
}
@@ -2423,11 +2441,11 @@ int parse_pack_header_option(const char *in, unsigned char *out, unsigned int *l
return 0;
}
-struct packfile_store *packfile_store_new(struct object_database *odb)
+struct packfile_store *packfile_store_new(struct odb_source *source)
{
struct packfile_store *store;
CALLOC_ARRAY(store, 1);
- store->odb = odb;
+ store->source = source;
strmap_init(&store->packs_by_path);
return store;
}
@@ -2449,6 +2467,9 @@ void packfile_store_close(struct packfile_store *store)
BUG("want to close pack marked 'do-not-close'");
close_pack(e->pack);
}
+ if (store->midx)
+ close_midx(store->midx);
+ store->midx = NULL;
}
struct odb_packed_read_stream {
@@ -2547,7 +2568,7 @@ int packfile_store_read_object_stream(struct odb_read_stream **out,
if (packfile_store_read_object_info(store, oid, &oi, 0) ||
oi.u.packed.type == PACKED_OBJECT_TYPE_REF_DELTA ||
oi.u.packed.type == PACKED_OBJECT_TYPE_OFS_DELTA ||
- repo_settings_get_big_file_threshold(store->odb->repo) >= size)
+ repo_settings_get_big_file_threshold(store->source->odb->repo) >= size)
return -1;
in_pack_type = unpack_object_header(oi.u.packed.pack,
diff --git a/packfile.h b/packfile.h
index 33fed26362..acc5c55ad5 100644
--- a/packfile.h
+++ b/packfile.h
@@ -5,6 +5,7 @@
#include "object.h"
#include "odb.h"
#include "oidset.h"
+#include "repository.h"
#include "strmap.h"
/* in odb.h */
@@ -77,7 +78,7 @@ struct packed_git *packfile_list_find_oid(struct packfile_list_entry *packs,
* A store that manages packfiles for a given object database.
*/
struct packfile_store {
- struct object_database *odb;
+ struct odb_source *source;
/*
* The list of packfiles in the order in which they have been most
@@ -90,15 +91,19 @@ struct packfile_store {
* is an on-disk ".keep" file or because they are marked as "kept" in
* memory.
*
- * Should not be accessed directly, but via `kept_pack_cache()`. The
- * list of packs gets invalidated when the stored flags and the flags
- * passed to `kept_pack_cache()` mismatch.
+ * Should not be accessed directly, but via
+ * `packfile_store_get_kept_pack_cache()`. The list of packs gets
+ * invalidated when the stored flags and the flags passed to
+ * `packfile_store_get_kept_pack_cache()` mismatch.
*/
struct {
struct packed_git **packs;
unsigned flags;
} kept_cache;
+ /* The multi-pack index that belongs to this specific packfile store. */
+ struct multi_pack_index *midx;
+
/*
* A map of packfile names to packed_git structs for tracking which
* packs have been loaded already.
@@ -129,9 +134,9 @@ struct packfile_store {
/*
* Allocate and initialize a new empty packfile store for the given object
- * database.
+ * database source.
*/
-struct packfile_store *packfile_store_new(struct object_database *odb);
+struct packfile_store *packfile_store_new(struct odb_source *source);
/*
* Free the packfile store and all its associated state. All packfiles
@@ -170,13 +175,64 @@ void packfile_store_add_pack(struct packfile_store *store,
struct packed_git *pack);
/*
+ * Get all packs managed by the given store, including packfiles that are
+ * referenced by multi-pack indices.
+ */
+struct packfile_list_entry *packfile_store_get_packs(struct packfile_store *store);
+
+struct repo_for_each_pack_data {
+ struct odb_source *source;
+ struct packfile_list_entry *entry;
+};
+
+static inline struct repo_for_each_pack_data repo_for_eack_pack_data_init(struct repository *repo)
+{
+ struct repo_for_each_pack_data data = { 0 };
+
+ odb_prepare_alternates(repo->objects);
+
+ for (struct odb_source *source = repo->objects->sources; source; source = source->next) {
+ struct packfile_list_entry *entry = packfile_store_get_packs(source->packfiles);
+ if (!entry)
+ continue;
+ data.source = source;
+ data.entry = entry;
+ break;
+ }
+
+ return data;
+}
+
+static inline void repo_for_each_pack_data_next(struct repo_for_each_pack_data *data)
+{
+ struct odb_source *source;
+
+ data->entry = data->entry->next;
+ if (data->entry)
+ return;
+
+ for (source = data->source->next; source; source = source->next) {
+ struct packfile_list_entry *entry = packfile_store_get_packs(source->packfiles);
+ if (!entry)
+ continue;
+ data->source = source;
+ data->entry = entry;
+ return;
+ }
+
+ data->source = NULL;
+ data->entry = NULL;
+}
+
+/*
* Load and iterate through all packs of the given repository. This helper
* function will yield packfiles from all object sources connected to the
* repository.
*/
#define repo_for_each_pack(repo, p) \
- for (struct packfile_list_entry *e = packfile_store_get_packs(repo->objects->packfiles); \
- ((p) = (e ? e->pack : NULL)); e = e->next)
+ for (struct repo_for_each_pack_data eack_pack_data = repo_for_eack_pack_data_init(repo); \
+ ((p) = (eack_pack_data.entry ? eack_pack_data.entry->pack : NULL)); \
+ repo_for_each_pack_data_next(&eack_pack_data))
int packfile_store_read_object_stream(struct odb_read_stream **out,
struct packfile_store *store,
@@ -194,12 +250,6 @@ int packfile_store_read_object_info(struct packfile_store *store,
unsigned flags);
/*
- * Get all packs managed by the given store, including packfiles that are
- * referenced by multi-pack indices.
- */
-struct packfile_list_entry *packfile_store_get_packs(struct packfile_store *store);
-
-/*
* Open the packfile and add it to the store if it isn't yet known. Returns
* either the newly opened packfile or the preexisting packfile. Returns a
* `NULL` pointer in case the packfile could not be opened.
@@ -210,6 +260,19 @@ struct packed_git *packfile_store_load_pack(struct packfile_store *store,
int packfile_store_freshen_object(struct packfile_store *store,
const struct object_id *oid);
+enum kept_pack_type {
+ KEPT_PACK_ON_DISK = (1 << 0),
+ KEPT_PACK_IN_CORE = (1 << 1),
+};
+
+/*
+ * Retrieve the cache of kept packs from the given packfile store. Accepts a
+ * combination of `kept_pack_type` flags. The cache is computed on demand and
+ * will be recomputed whenever the flags change.
+ */
+struct packed_git **packfile_store_get_kept_pack_cache(struct packfile_store *store,
+ unsigned flags);
+
struct pack_window {
struct pack_window *next;
unsigned char *base;
@@ -388,21 +451,10 @@ int packed_object_info(struct packed_git *pack,
void mark_bad_packed_object(struct packed_git *, const struct object_id *);
const struct packed_git *has_packed_and_bad(struct repository *, const struct object_id *);
-#define ON_DISK_KEEP_PACKS 1
-#define IN_CORE_KEEP_PACKS 2
-
-/*
- * Iff a pack file in the given repository contains the object named by sha1,
- * return true and store its location to e.
- */
-int find_kept_pack_entry(struct repository *r, const struct object_id *oid, unsigned flags, struct pack_entry *e);
-
int has_object_pack(struct repository *r, const struct object_id *oid);
int has_object_kept_pack(struct repository *r, const struct object_id *oid,
unsigned flags);
-struct packed_git **kept_pack_cache(struct repository *r, unsigned flags);
-
/*
* Return 1 if an object in a promisor packfile is or refers to the given
* object, 0 otherwise.
diff --git a/reachable.c b/reachable.c
index b753c39553..4b532039d5 100644
--- a/reachable.c
+++ b/reachable.c
@@ -242,7 +242,7 @@ static int want_recent_object(struct recent_data *data,
const struct object_id *oid)
{
if (data->ignore_in_core_kept_packs &&
- has_object_kept_pack(data->revs->repo, oid, IN_CORE_KEEP_PACKS))
+ has_object_kept_pack(data->revs->repo, oid, KEPT_PACK_IN_CORE))
return 0;
return 1;
}
diff --git a/revision.c b/revision.c
index 1858e093ee..b65a763770 100644
--- a/revision.c
+++ b/revision.c
@@ -2518,14 +2518,14 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
die(_("--unpacked=<packfile> no longer supported"));
} else if (!strcmp(arg, "--no-kept-objects")) {
revs->no_kept_objects = 1;
- revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
- revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
+ revs->keep_pack_cache_flags |= KEPT_PACK_IN_CORE;
+ revs->keep_pack_cache_flags |= KEPT_PACK_ON_DISK;
} else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
revs->no_kept_objects = 1;
if (!strcmp(optarg, "in-core"))
- revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
+ revs->keep_pack_cache_flags |= KEPT_PACK_IN_CORE;
if (!strcmp(optarg, "on-disk"))
- revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
+ revs->keep_pack_cache_flags |= KEPT_PACK_ON_DISK;
} else if (!strcmp(arg, "-r")) {
revs->diff = 1;
revs->diffopt.flags.recursive = 1;
diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh
index 6b36f52df7..7cc0ce57f8 100755
--- a/t/t7900-maintenance.sh
+++ b/t/t7900-maintenance.sh
@@ -206,6 +206,31 @@ test_expect_success 'commit-graph auto condition' '
test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
'
+test_expect_success 'commit-graph auto condition with merges' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ (
+ cd repo &&
+ git config set maintenance.auto false &&
+ test_commit initial &&
+ git switch --create feature &&
+ test_commit feature-1 &&
+ test_commit feature-2 &&
+ git switch - &&
+ test_commit main-1 &&
+ test_commit main-2 &&
+ git merge feature &&
+
+ # We have 6 commits, none of which are covered by a commit
+ # graph. So this must be the boundary at which we start to
+ # perform maintenance.
+ test_must_fail git -c maintenance.commit-graph.auto=7 \
+ maintenance is-needed --auto --task=commit-graph &&
+ git -c maintenance.commit-graph.auto=6 \
+ maintenance is-needed --auto --task=commit-graph
+ )
+'
+
test_expect_success 'run --task=bogus' '
test_must_fail git maintenance run --task=bogus 2>err &&
test_grep "is not a valid task" err