loose: compatibilty short name support

Update loose_objects_cache when udpating the loose objects map. This oidtree is used to discover which oids are possibilities when resolving short names, and it can support a mixture of sha1 and sha256 oids. With this any oid recorded objects/loose-objects-idx is usable for resolving an oid to an object. To make this maintainable a helper insert_loose_map is factored out of load_one_loose_object_map and repo_add_loose_object_map, and then modified to also update the loose_objects_cache. Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric W. Biederman committed Oct 1, 2023 at 21:40 UTC a2d923fb0d07e2518c2d692c53f0e1d83baa1604
1 file changed +25 -12
loose.c
+25 -12
@@ -7,6 +7,7 @@
7 #include "gettext.h"
8 #include "loose.h"
9 #include "lockfile.h"
10 +#include "oidtree.h"
11
12 static const char *loose_object_header = "# loose-object-idx\n";
13
@@ -42,6 +43,21 @@ static int insert_oid_pair(kh_oid_map_t *map, const struct object_id *key, const
43 return 1;
44 }
45
46 +static int insert_loose_map(struct object_directory *odb,
47 + const struct object_id *oid,
48 + const struct object_id *compat_oid)
49 +{
50 + struct loose_object_map *map = odb->loose_map;
51 + int inserted = 0;
52 +
53 + inserted |= insert_oid_pair(map->to_compat, oid, compat_oid);
54 + inserted |= insert_oid_pair(map->to_storage, compat_oid, oid);
55 + if (inserted)
56 + oidtree_insert(odb->loose_objects_cache, compat_oid);
57 +
58 + return inserted;
59 +}
60 +
61 static int load_one_loose_object_map(struct repository *repo, struct object_directory *dir)
62 {
63 struct strbuf buf = STRBUF_INIT, path = STRBUF_INIT;
@@ -49,15 +65,14 @@ static int load_one_loose_object_map(struct repository *repo, struct object_dire
65
66 if (!dir->loose_map)
67 loose_object_map_init(&dir->loose_map);
68 + if (!dir->loose_objects_cache) {
69 + ALLOC_ARRAY(dir->loose_objects_cache, 1);
70 + oidtree_init(dir->loose_objects_cache);
71 + }
72
53 - insert_oid_pair(dir->loose_map->to_compat, repo->hash_algo->empty_tree, repo->compat_hash_algo->empty_tree);
54 - insert_oid_pair(dir->loose_map->to_storage, repo->compat_hash_algo->empty_tree, repo->hash_algo->empty_tree);
55 -
56 - insert_oid_pair(dir->loose_map->to_compat, repo->hash_algo->empty_blob, repo->compat_hash_algo->empty_blob);
57 - insert_oid_pair(dir->loose_map->to_storage, repo->compat_hash_algo->empty_blob, repo->hash_algo->empty_blob);
58 -
59 - insert_oid_pair(dir->loose_map->to_compat, repo->hash_algo->null_oid, repo->compat_hash_algo->null_oid);
60 - insert_oid_pair(dir->loose_map->to_storage, repo->compat_hash_algo->null_oid, repo->hash_algo->null_oid);
73 + insert_loose_map(dir, repo->hash_algo->empty_tree, repo->compat_hash_algo->empty_tree);
74 + insert_loose_map(dir, repo->hash_algo->empty_blob, repo->compat_hash_algo->empty_blob);
75 + insert_loose_map(dir, repo->hash_algo->null_oid, repo->compat_hash_algo->null_oid);
76
77 strbuf_git_common_path(&path, repo, "objects/loose-object-idx");
78 fp = fopen(path.buf, "rb");
@@ -77,8 +92,7 @@ static int load_one_loose_object_map(struct repository *repo, struct object_dire
92 parse_oid_hex_algop(p, &compat_oid, &p, repo->compat_hash_algo) ||
93 p != buf.buf + buf.len)
94 goto err;
80 - insert_oid_pair(dir->loose_map->to_compat, &oid, &compat_oid);
81 - insert_oid_pair(dir->loose_map->to_storage, &compat_oid, &oid);
95 + insert_loose_map(dir, &oid, &compat_oid);
96 }
97
98 strbuf_release(&buf);
@@ -197,8 +211,7 @@ int repo_add_loose_object_map(struct repository *repo, const struct object_id *o
211 if (!should_use_loose_object_map(repo))
212 return 0;
213
200 - inserted |= insert_oid_pair(repo->objects->odb->loose_map->to_compat, oid, compat_oid);
201 - inserted |= insert_oid_pair(repo->objects->odb->loose_map->to_storage, compat_oid, oid);
214 + inserted = insert_loose_map(repo->objects->odb, oid, compat_oid);
215 if (inserted)
216 return write_one_object(repo, oid, compat_oid);
217 return 0;