http-walker: use object_id instead of bare hash

We long ago switched most code to using object_id structs instead of bare "unsigned char *" hashes. This gives us more type safety from the compiler, and generally makes it easier to understand what we expect in each parameter. But the dumb-http code has lagged behind. And indeed, the whole "walker" subsystem interface has the same problem, though http-walker is the only user left. So let's update the walker interface to pass object_id structs (which we already have anyway at all call sites!), and likewise use those within the http-walker methods that it calls. This cleans up the dumb-http code a bit, but will also let us fix a few more commonly used helper functions. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Taylor Blau <me@ttaylorr.com>

Jeff King committed Oct 25, 2024 at 03:03 UTC 0af861e0c8d7b7c6135bfc688be13370ac2c9468
3 files changed +17 -16
http-walker.c
+13 -12
@@ -147,14 +147,14 @@ static int fill_active_slot(void *data UNUSED)
147 return 0;
148 }
149
150 -static void prefetch(struct walker *walker, unsigned char *sha1)
150 +static void prefetch(struct walker *walker, const struct object_id *oid)
151 {
152 struct object_request *newreq;
153 struct walker_data *data = walker->data;
154
155 newreq = xmalloc(sizeof(*newreq));
156 newreq->walker = walker;
157 - oidread(&newreq->oid, sha1, the_repository->hash_algo);
157 + oidcpy(&newreq->oid, oid);
158 newreq->repo = data->alt;
159 newreq->state = WAITING;
160 newreq->req = NULL;
@@ -422,7 +422,8 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo)
422 return ret;
423 }
424
425 -static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
425 +static int http_fetch_pack(struct walker *walker, struct alt_base *repo,
426 + const struct object_id *oid)
427 {
428 struct packed_git *target;
429 int ret;
@@ -431,7 +432,7 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
432
433 if (fetch_indices(walker, repo))
434 return -1;
434 - target = find_sha1_pack(sha1, repo->packs);
435 + target = find_sha1_pack(oid->hash, repo->packs);
436 if (!target)
437 return -1;
438 close_pack_index(target);
@@ -440,7 +441,7 @@ static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigne
441 fprintf(stderr, "Getting pack %s\n",
442 hash_to_hex(target->hash));
443 fprintf(stderr, " which contains %s\n",
443 - hash_to_hex(sha1));
444 + oid_to_hex(oid));
445 }
446
447 preq = new_http_pack_request(target->hash, repo->base);
@@ -477,9 +478,9 @@ static void abort_object_request(struct object_request *obj_req)
478 release_object_request(obj_req);
479 }
480
480 -static int fetch_object(struct walker *walker, unsigned char *hash)
481 +static int fetch_object(struct walker *walker, const struct object_id *oid)
482 {
482 - char *hex = hash_to_hex(hash);
483 + char *hex = oid_to_hex(oid);
484 int ret = 0;
485 struct object_request *obj_req = NULL;
486 struct http_object_request *req;
@@ -487,7 +488,7 @@ static int fetch_object(struct walker *walker, unsigned char *hash)
488
489 list_for_each(pos, head) {
490 obj_req = list_entry(pos, struct object_request, node);
490 - if (hasheq(obj_req->oid.hash, hash, the_repository->hash_algo))
491 + if (oideq(&obj_req->oid, oid))
492 break;
493 }
494 if (!obj_req)
@@ -548,20 +549,20 @@ static int fetch_object(struct walker *walker, unsigned char *hash)
549 return ret;
550 }
551
551 -static int fetch(struct walker *walker, unsigned char *hash)
552 +static int fetch(struct walker *walker, const struct object_id *oid)
553 {
554 struct walker_data *data = walker->data;
555 struct alt_base *altbase = data->alt;
556
556 - if (!fetch_object(walker, hash))
557 + if (!fetch_object(walker, oid))
558 return 0;
559 while (altbase) {
559 - if (!http_fetch_pack(walker, altbase, hash))
560 + if (!http_fetch_pack(walker, altbase, oid))
561 return 0;
562 fetch_alternates(walker, data->alt->base);
563 altbase = altbase->next;
564 }
564 - return error("Unable to find %s under %s", hash_to_hex(hash),
565 + return error("Unable to find %s under %s", oid_to_hex(oid),
566 data->alt->base);
567 }
568
walker.c
+2 -2
@@ -157,7 +157,7 @@ static int process(struct walker *walker, struct object *obj)
157 else {
158 if (obj->flags & COMPLETE)
159 return 0;
160 - walker->prefetch(walker, obj->oid.hash);
160 + walker->prefetch(walker, &obj->oid);
161 }
162
163 object_list_insert(obj, process_queue_end);
@@ -186,7 +186,7 @@ static int loop(struct walker *walker)
186 * the queue because we needed to fetch it first.
187 */
188 if (! (obj->flags & TO_SCAN)) {
189 - if (walker->fetch(walker, obj->oid.hash)) {
189 + if (walker->fetch(walker, &obj->oid)) {
190 stop_progress(&progress);
191 report_missing(obj);
192 return -1;
walker.h
+2 -2
@@ -6,8 +6,8 @@
6 struct walker {
7 void *data;
8 int (*fetch_ref)(struct walker *, struct ref *ref);
9 - void (*prefetch)(struct walker *, unsigned char *sha1);
10 - int (*fetch)(struct walker *, unsigned char *sha1);
9 + void (*prefetch)(struct walker *, const struct object_id *oid);
10 + int (*fetch)(struct walker *, const struct object_id *oid);
11 void (*cleanup)(struct walker *);
12 int get_verbosely;
13 int get_progress;