object: stop depending on `the_repository`

There are a couple of functions exposed by "object.c" that implicitly depend on `the_repository`. Remove this dependency by injecting the repository via a parameter. Adapt callers accordingly by simply using `the_repository`, except in cases where the subsystem is already free of the repository. In that case, we instead pass the repository provided by the caller's context. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 10, 2025 at 08:13 UTC 74d414c9f14a91a3b7bd04972bf3eb9bbe6fd81b
15 files changed +48 -44
builtin/fsck.c
+2 -2
@@ -399,12 +399,12 @@ static void check_connectivity(void)
399 }
400
401 /* Look up all the requirements, warn about missing objects.. */
402 - max = get_max_object_index();
402 + max = get_max_object_index(the_repository);
403 if (verbose)
404 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
405
406 for (i = 0; i < max; i++) {
407 - struct object *obj = get_indexed_object(i);
407 + struct object *obj = get_indexed_object(the_repository, i);
408
409 if (obj)
410 check_object(obj);
builtin/grep.c
+1 -1
@@ -1144,7 +1144,7 @@ int cmd_grep(int argc,
1144 break;
1145 }
1146
1147 - object = parse_object_or_die(&oid, arg);
1147 + object = parse_object_or_die(the_repository, &oid, arg);
1148 if (!seen_dashdash)
1149 verify_non_filename(prefix, arg);
1150 add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
builtin/index-pack.c
+2 -2
@@ -279,14 +279,14 @@ static unsigned check_objects(void)
279 {
280 unsigned i, max, foreign_nr = 0;
281
282 - max = get_max_object_index();
282 + max = get_max_object_index(the_repository);
283
284 if (verbose)
285 progress = start_delayed_progress(the_repository,
286 _("Checking objects"), max);
287
288 for (i = 0; i < max; i++) {
289 - foreign_nr += check_object(get_indexed_object(i));
289 + foreign_nr += check_object(get_indexed_object(the_repository, i));
290 display_progress(progress, i + 1);
291 }
292
builtin/log.c
+1 -1
@@ -2468,7 +2468,7 @@ int cmd_format_patch(int argc,
2468 base = get_base_commit(&cfg, list, nr);
2469 if (base) {
2470 reset_revision_walk();
2471 - clear_object_flags(UNINTERESTING);
2471 + clear_object_flags(the_repository, UNINTERESTING);
2472 prepare_bases(&bases, base, list, nr);
2473 }
2474
builtin/name-rev.c
+2 -2
@@ -667,9 +667,9 @@ int cmd_name_rev(int argc,
667 } else if (all) {
668 int i, max;
669
670 - max = get_max_object_index();
670 + max = get_max_object_index(the_repository);
671 for (i = 0; i < max; i++) {
672 - struct object *obj = get_indexed_object(i);
672 + struct object *obj = get_indexed_object(the_repository, i);
673 if (!obj || obj->type != OBJ_COMMIT)
674 continue;
675 show_name(obj, NULL,
builtin/pack-objects.c
+1 -1
@@ -4161,7 +4161,7 @@ static int mark_bitmap_preferred_tip(const char *refname,
4161 if (!peel_iterated_oid(the_repository, oid, &peeled))
4162 oid = &peeled;
4163
4164 - object = parse_object_or_die(oid, refname);
4164 + object = parse_object_or_die(the_repository, oid, refname);
4165 if (object->type == OBJ_COMMIT)
4166 object->flags |= NEEDS_BITMAP;
4167
builtin/prune.c
+1 -1
@@ -185,7 +185,7 @@ int cmd_prune(int argc,
185 const char *name = *argv++;
186
187 if (!repo_get_oid(the_repository, name, &oid)) {
188 - struct object *object = parse_object_or_die(&oid,
188 + struct object *object = parse_object_or_die(the_repository, &oid,
189 name);
190 add_pending_object(&revs, object, "");
191 }
midx-write.c
+2 -2
@@ -708,7 +708,7 @@ static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
708 if (!peel_iterated_oid(revs->repo, oid, &peeled))
709 oid = &peeled;
710
711 - object = parse_object_or_die(oid, refname);
711 + object = parse_object_or_die(revs->repo, oid, refname);
712 if (object->type != OBJ_COMMIT)
713 return 0;
714
@@ -768,7 +768,7 @@ static int read_refs_snapshot(const char *refs_snapshot,
768 if (*end)
769 die(_("malformed line: %s"), buf.buf);
770
771 - object = parse_object_or_die(&oid, NULL);
771 + object = parse_object_or_die(revs->repo, &oid, NULL);
772 if (preferred)
773 object->flags |= NEEDS_BITMAP;
774
object.c
+11 -10
@@ -1,4 +1,3 @@
1 -#define USE_THE_REPOSITORY_VARIABLE
1 #define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "git-compat-util.h"
@@ -18,14 +17,15 @@
17 #include "commit-graph.h"
18 #include "loose.h"
19
21 -unsigned int get_max_object_index(void)
20 +unsigned int get_max_object_index(const struct repository *repo)
21 {
23 - return the_repository->parsed_objects->obj_hash_size;
22 + return repo->parsed_objects->obj_hash_size;
23 }
24
26 -struct object *get_indexed_object(unsigned int idx)
25 +struct object *get_indexed_object(const struct repository *repo,
26 + unsigned int idx)
27 {
28 - return the_repository->parsed_objects->obj_hash[idx];
28 + return repo->parsed_objects->obj_hash[idx];
29 }
30
31 static const char *object_type_strings[] = {
@@ -283,10 +283,11 @@ struct object *parse_object_buffer(struct repository *r, const struct object_id
283 return obj;
284 }
285
286 -struct object *parse_object_or_die(const struct object_id *oid,
286 +struct object *parse_object_or_die(struct repository *repo,
287 + const struct object_id *oid,
288 const char *name)
289 {
289 - struct object *o = parse_object(the_repository, oid);
290 + struct object *o = parse_object(repo, oid);
291 if (o)
292 return o;
293
@@ -524,12 +525,12 @@ void object_array_remove_duplicates(struct object_array *array)
525 }
526 }
527
527 -void clear_object_flags(unsigned flags)
528 +void clear_object_flags(struct repository *repo, unsigned flags)
529 {
530 int i;
531
531 - for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) {
532 - struct object *obj = the_repository->parsed_objects->obj_hash[i];
532 + for (i=0; i < repo->parsed_objects->obj_hash_size; i++) {
533 + struct object *obj = repo->parsed_objects->obj_hash[i];
534 if (obj)
535 obj->flags &= ~flags;
536 }
object.h
+6 -4
@@ -169,12 +169,13 @@ int type_from_string_gently(const char *str, ssize_t, int gentle);
169 /*
170 * Return the current number of buckets in the object hashmap.
171 */
172 -unsigned int get_max_object_index(void);
172 +unsigned int get_max_object_index(const struct repository *repo);
173
174 /*
175 * Return the object from the specified bucket in the object hashmap.
176 */
177 -struct object *get_indexed_object(unsigned int);
177 +struct object *get_indexed_object(const struct repository *repo,
178 + unsigned int);
179
180 /*
181 * This can be used to see if we have heard of the object before, but
@@ -231,7 +232,8 @@ struct object *parse_object_with_flags(struct repository *r,
232 * "name" parameter is not NULL, it is included in the error message
233 * (otherwise, the hex object ID is given).
234 */
234 -struct object *parse_object_or_die(const struct object_id *oid, const char *name);
235 +struct object *parse_object_or_die(struct repository *repo, const struct object_id *oid,
236 + const char *name);
237
238 /* Given the result of read_sha1_file(), returns the object after
239 * parsing it. eaten_p indicates if the object has a borrowed copy
@@ -336,7 +338,7 @@ void object_array_remove_duplicates(struct object_array *array);
338 */
339 void object_array_clear(struct object_array *array);
340
339 -void clear_object_flags(unsigned flags);
341 +void clear_object_flags(struct repository *repo, unsigned flags);
342
343 /*
344 * Clear the specified object flags from all in-core commit objects from
pack-bitmap.c
+3 -3
@@ -1301,7 +1301,7 @@ static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1301 revs->tag_objects = tmp_tags;
1302
1303 reset_revision_walk();
1304 - clear_object_flags(UNINTERESTING);
1304 + clear_object_flags(repo, UNINTERESTING);
1305
1306 /*
1307 * Then add the boundary commit(s) as fill-in traversal tips.
@@ -1935,7 +1935,7 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1935 struct object *object = revs->pending.objects[i].item;
1936
1937 if (object->type == OBJ_NONE)
1938 - parse_object_or_die(&object->oid, NULL);
1938 + parse_object_or_die(revs->repo, &object->oid, NULL);
1939
1940 while (object->type == OBJ_TAG) {
1941 struct tag *tag = (struct tag *) object;
@@ -1945,7 +1945,7 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1945 else
1946 object_list_insert(object, &wants);
1947
1948 - object = parse_object_or_die(get_tagged_oid(tag), NULL);
1948 + object = parse_object_or_die(revs->repo, get_tagged_oid(tag), NULL);
1949 object->flags |= (tag->object.flags & UNINTERESTING);
1950 }
1951
reachable.c
+3 -3
@@ -45,7 +45,7 @@ static void add_one_file(const char *path, struct rev_info *revs)
45 }
46 strbuf_trim(&buf);
47 if (!get_oid_hex(buf.buf, &oid)) {
48 - object = parse_object_or_die(&oid, buf.buf);
48 + object = parse_object_or_die(the_repository, &oid, buf.buf);
49 add_pending_object(revs, object, "");
50 }
51 strbuf_release(&buf);
@@ -94,7 +94,7 @@ static int add_one_ref(const char *path, const char *referent UNUSED, const stru
94 return 0;
95 }
96
97 - object = parse_object_or_die(oid, path);
97 + object = parse_object_or_die(the_repository, oid, path);
98 add_pending_object(revs, object, "");
99
100 return 0;
@@ -218,7 +218,7 @@ static void add_recent_object(const struct object_id *oid,
218 switch (type) {
219 case OBJ_TAG:
220 case OBJ_COMMIT:
221 - obj = parse_object_or_die(oid, NULL);
221 + obj = parse_object_or_die(the_repository, oid, NULL);
222 break;
223 case OBJ_TREE:
224 obj = (struct object *)lookup_tree(the_repository, oid);
revision.c
+2 -1
@@ -3612,7 +3612,8 @@ static void set_children(struct rev_info *revs)
3612
3613 void reset_revision_walk(void)
3614 {
3615 - clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3615 + clear_object_flags(the_repository,
3616 + SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3617 }
3618
3619 static int mark_uninteresting(const struct object_id *oid,
shallow.c
+5 -5
@@ -226,7 +226,7 @@ struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
226 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
227 * set at this point. But better be safe than sorry.
228 */
229 - clear_object_flags(both_flags);
229 + clear_object_flags(the_repository, both_flags);
230
231 is_repository_shallow(the_repository); /* make sure shallows are read */
232
@@ -613,9 +613,9 @@ static void paint_down(struct paint_info *info, const struct object_id *oid,
613 }
614 }
615
616 - nr = get_max_object_index();
616 + nr = get_max_object_index(the_repository);
617 for (i = 0; i < nr; i++) {
618 - struct object *o = get_indexed_object(i);
618 + struct object *o = get_indexed_object(the_repository, i);
619 if (o && o->type == OBJ_COMMIT)
620 o->flags &= ~SEEN;
621 }
@@ -675,9 +675,9 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
675 * Prepare the commit graph to track what refs can reach what
676 * (new) shallow commits.
677 */
678 - nr = get_max_object_index();
678 + nr = get_max_object_index(the_repository);
679 for (i = 0; i < nr; i++) {
680 - struct object *o = get_indexed_object(i);
680 + struct object *o = get_indexed_object(the_repository, i);
681 if (!o || o->type != OBJ_COMMIT)
682 continue;
683
upload-pack.c
+6 -6
@@ -665,8 +665,8 @@ static int do_reachable_revlist(struct child_process *cmd,
665
666 cmd_in = xfdopen(cmd->in, "w");
667
668 - for (i = get_max_object_index(); 0 < i; ) {
669 - o = get_indexed_object(--i);
668 + for (i = get_max_object_index(the_repository); 0 < i; ) {
669 + o = get_indexed_object(the_repository, --i);
670 if (!o)
671 continue;
672 if (reachable && o->type == OBJ_COMMIT)
@@ -734,8 +734,8 @@ static int get_reachable_list(struct upload_pack_data *data,
734 o->flags &= ~TMP_MARK;
735 }
736 }
737 - for (i = get_max_object_index(); 0 < i; i--) {
738 - o = get_indexed_object(i - 1);
737 + for (i = get_max_object_index(the_repository); 0 < i; i--) {
738 + o = get_indexed_object(the_repository, i - 1);
739 if (o && o->type == OBJ_COMMIT &&
740 (o->flags & TMP_MARK)) {
741 add_object_array(o, NULL, reachable);
@@ -1557,7 +1557,7 @@ static int parse_want_ref(struct packet_writer *writer, const char *line,
1557 }
1558
1559 if (!o)
1560 - o = parse_object_or_die(&oid, refname_nons);
1560 + o = parse_object_or_die(the_repository, &oid, refname_nons);
1561
1562 if (!(o->flags & WANTED)) {
1563 o->flags |= WANTED;
@@ -1793,7 +1793,7 @@ int upload_pack_v2(struct repository *r, struct packet_reader *request)
1793 enum fetch_state state = FETCH_PROCESS_ARGS;
1794 struct upload_pack_data data;
1795
1796 - clear_object_flags(ALL_FLAGS);
1796 + clear_object_flags(the_repository, ALL_FLAGS);
1797
1798 upload_pack_data_init(&data);
1799 data.use_sideband = LARGE_PACKET_MAX;