fetch-pack: convert to struct object_id

Convert all uses of unsigned char [20] to struct object_id. Switch one use of get_sha1_hex to parse_oid_hex to avoid the need for a constant. This change is necessary in order to convert parse_object. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed May 1, 2017 at 02:28 UTC 1b283377b18bc1e363005729b025a88b36084c4e
1 file changed +45 -44
fetch-pack.c
+45 -44
@@ -118,9 +118,9 @@ static void rev_list_push(struct commit *commit, int mark)
118 }
119 }
120
121 -static int rev_list_insert_ref(const char *refname, const unsigned char *sha1)
121 +static int rev_list_insert_ref(const char *refname, const struct object_id *oid)
122 {
123 - struct object *o = deref_tag(parse_object(sha1), refname, 0);
123 + struct object *o = deref_tag(parse_object(oid->hash), refname, 0);
124
125 if (o && o->type == OBJ_COMMIT)
126 rev_list_push((struct commit *)o, SEEN);
@@ -131,7 +131,7 @@ static int rev_list_insert_ref(const char *refname, const unsigned char *sha1)
131 static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
132 int flag, void *cb_data)
133 {
134 - return rev_list_insert_ref(refname, oid->hash);
134 + return rev_list_insert_ref(refname, oid);
135 }
136
137 static int clear_marks(const char *refname, const struct object_id *oid,
@@ -183,7 +183,7 @@ static void mark_common(struct commit *commit,
183 Get the next rev to send, ignoring the common.
184 */
185
186 -static const unsigned char *get_rev(void)
186 +static const struct object_id *get_rev(void)
187 {
188 struct commit *commit = NULL;
189
@@ -222,7 +222,7 @@ static const unsigned char *get_rev(void)
222 }
223 }
224
225 - return commit->object.oid.hash;
225 + return &commit->object.oid;
226 }
227
228 enum ack_type {
@@ -251,7 +251,7 @@ static void consume_shallow_list(struct fetch_pack_args *args, int fd)
251 }
252 }
253
254 -static enum ack_type get_ack(int fd, unsigned char *result_sha1)
254 +static enum ack_type get_ack(int fd, struct object_id *result_oid)
255 {
256 int len;
257 char *line = packet_read_line(fd, &len);
@@ -262,7 +262,7 @@ static enum ack_type get_ack(int fd, unsigned char *result_sha1)
262 if (!strcmp(line, "NAK"))
263 return NAK;
264 if (skip_prefix(line, "ACK ", &arg)) {
265 - if (!get_sha1_hex(arg, result_sha1)) {
265 + if (!get_oid_hex(arg, result_oid)) {
266 arg += 40;
267 len -= arg - line;
268 if (len < 1)
@@ -293,7 +293,7 @@ static void send_request(struct fetch_pack_args *args,
293
294 static void insert_one_alternate_object(struct object *obj)
295 {
296 - rev_list_insert_ref(NULL, obj->oid.hash);
296 + rev_list_insert_ref(NULL, &obj->oid);
297 }
298
299 #define INITIAL_FLUSH 16
@@ -317,12 +317,12 @@ static int next_flush(struct fetch_pack_args *args, int count)
317 }
318
319 static int find_common(struct fetch_pack_args *args,
320 - int fd[2], unsigned char *result_sha1,
320 + int fd[2], struct object_id *result_oid,
321 struct ref *refs)
322 {
323 int fetching;
324 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
325 - const unsigned char *sha1;
325 + const struct object_id *oid;
326 unsigned in_vain = 0;
327 int got_continue = 0;
328 int got_ready = 0;
@@ -340,7 +340,7 @@ static int find_common(struct fetch_pack_args *args,
340
341 fetching = 0;
342 for ( ; refs ; refs = refs->next) {
343 - unsigned char *remote = refs->old_oid.hash;
343 + struct object_id *remote = &refs->old_oid;
344 const char *remote_hex;
345 struct object *o;
346
@@ -354,12 +354,12 @@ static int find_common(struct fetch_pack_args *args,
354 * interested in the case we *know* the object is
355 * reachable and we have already scanned it.
356 */
357 - if (((o = lookup_object(remote)) != NULL) &&
357 + if (((o = lookup_object(remote->hash)) != NULL) &&
358 (o->flags & COMPLETE)) {
359 continue;
360 }
361
362 - remote_hex = sha1_to_hex(remote);
362 + remote_hex = oid_to_hex(remote);
363 if (!fetching) {
364 struct strbuf c = STRBUF_INIT;
365 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
@@ -410,25 +410,25 @@ static int find_common(struct fetch_pack_args *args,
410 if (args->deepen) {
411 char *line;
412 const char *arg;
413 - unsigned char sha1[20];
413 + struct object_id oid;
414
415 send_request(args, fd[1], &req_buf);
416 while ((line = packet_read_line(fd[0], NULL))) {
417 if (skip_prefix(line, "shallow ", &arg)) {
418 - if (get_sha1_hex(arg, sha1))
418 + if (get_oid_hex(arg, &oid))
419 die(_("invalid shallow line: %s"), line);
420 - register_shallow(sha1);
420 + register_shallow(oid.hash);
421 continue;
422 }
423 if (skip_prefix(line, "unshallow ", &arg)) {
424 - if (get_sha1_hex(arg, sha1))
424 + if (get_oid_hex(arg, &oid))
425 die(_("invalid unshallow line: %s"), line);
426 - if (!lookup_object(sha1))
426 + if (!lookup_object(oid.hash))
427 die(_("object not found: %s"), line);
428 /* make sure that it is parsed as shallow */
429 - if (!parse_object(sha1))
429 + if (!parse_object(oid.hash))
430 die(_("error in object: %s"), line);
431 - if (unregister_shallow(sha1))
431 + if (unregister_shallow(oid.hash))
432 die(_("no shallow found: %s"), line);
433 continue;
434 }
@@ -447,9 +447,9 @@ static int find_common(struct fetch_pack_args *args,
447
448 flushes = 0;
449 retval = -1;
450 - while ((sha1 = get_rev())) {
451 - packet_buf_write(&req_buf, "have %s\n", sha1_to_hex(sha1));
452 - print_verbose(args, "have %s", sha1_to_hex(sha1));
450 + while ((oid = get_rev())) {
451 + packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
452 + print_verbose(args, "have %s", oid_to_hex(oid));
453 in_vain++;
454 if (flush_at <= ++count) {
455 int ack;
@@ -469,10 +469,10 @@ static int find_common(struct fetch_pack_args *args,
469
470 consume_shallow_list(args, fd[0]);
471 do {
472 - ack = get_ack(fd[0], result_sha1);
472 + ack = get_ack(fd[0], result_oid);
473 if (ack)
474 print_verbose(args, _("got %s %d %s"), "ack",
475 - ack, sha1_to_hex(result_sha1));
475 + ack, oid_to_hex(result_oid));
476 switch (ack) {
477 case ACK:
478 flushes = 0;
@@ -483,9 +483,9 @@ static int find_common(struct fetch_pack_args *args,
483 case ACK_ready:
484 case ACK_continue: {
485 struct commit *commit =
486 - lookup_commit(result_sha1);
486 + lookup_commit(result_oid->hash);
487 if (!commit)
488 - die(_("invalid commit %s"), sha1_to_hex(result_sha1));
488 + die(_("invalid commit %s"), oid_to_hex(result_oid));
489 if (args->stateless_rpc
490 && ack == ACK_common
491 && !(commit->object.flags & COMMON)) {
@@ -493,7 +493,7 @@ static int find_common(struct fetch_pack_args *args,
493 * on the next RPC request so the peer knows
494 * it is in common with us.
495 */
496 - const char *hex = sha1_to_hex(result_sha1);
496 + const char *hex = oid_to_hex(result_oid);
497 packet_buf_write(&req_buf, "have %s\n", hex);
498 state_len = req_buf.len;
499 /*
@@ -538,10 +538,10 @@ done:
538 if (!got_ready || !no_done)
539 consume_shallow_list(args, fd[0]);
540 while (flushes || multi_ack) {
541 - int ack = get_ack(fd[0], result_sha1);
541 + int ack = get_ack(fd[0], result_oid);
542 if (ack) {
543 print_verbose(args, _("got %s (%d) %s"), "ack",
544 - ack, sha1_to_hex(result_sha1));
544 + ack, oid_to_hex(result_oid));
545 if (ack == ACK)
546 return 0;
547 multi_ack = 1;
@@ -555,9 +555,9 @@ done:
555
556 static struct commit_list *complete;
557
558 -static int mark_complete(const unsigned char *sha1)
558 +static int mark_complete(const struct object_id *oid)
559 {
560 - struct object *o = parse_object(sha1);
560 + struct object *o = parse_object(oid->hash);
561
562 while (o && o->type == OBJ_TAG) {
563 struct tag *t = (struct tag *) o;
@@ -579,7 +579,7 @@ static int mark_complete(const unsigned char *sha1)
579 static int mark_complete_oid(const char *refname, const struct object_id *oid,
580 int flag, void *cb_data)
581 {
582 - return mark_complete(oid->hash);
582 + return mark_complete(oid);
583 }
584
585 static void mark_recent_complete_commits(struct fetch_pack_args *args,
@@ -637,14 +637,15 @@ static void filter_refs(struct fetch_pack_args *args,
637
638 /* Append unmatched requests to the list */
639 for (i = 0; i < nr_sought; i++) {
640 - unsigned char sha1[20];
640 + struct object_id oid;
641 + const char *p;
642
643 ref = sought[i];
644 if (ref->match_status != REF_NOT_MATCHED)
645 continue;
645 - if (get_sha1_hex(ref->name, sha1) ||
646 - ref->name[40] != '\0' ||
647 - hashcmp(sha1, ref->old_oid.hash))
646 + if (parse_oid_hex(ref->name, &oid, &p) ||
647 + *p != '\0' ||
648 + oidcmp(&oid, &ref->old_oid))
649 continue;
650
651 if ((allow_unadvertised_object_request &
@@ -661,7 +662,7 @@ static void filter_refs(struct fetch_pack_args *args,
662
663 static void mark_alternate_complete(struct object *obj)
664 {
664 - mark_complete(obj->oid.hash);
665 + mark_complete(&obj->oid);
666 }
667
668 static int everything_local(struct fetch_pack_args *args,
@@ -724,17 +725,17 @@ static int everything_local(struct fetch_pack_args *args,
725 filter_refs(args, refs, sought, nr_sought);
726
727 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
727 - const unsigned char *remote = ref->old_oid.hash;
728 + const struct object_id *remote = &ref->old_oid;
729 struct object *o;
730
730 - o = lookup_object(remote);
731 + o = lookup_object(remote->hash);
732 if (!o || !(o->flags & COMPLETE)) {
733 retval = 0;
733 - print_verbose(args, "want %s (%s)", sha1_to_hex(remote),
734 + print_verbose(args, "want %s (%s)", oid_to_hex(remote),
735 ref->name);
736 continue;
737 }
737 - print_verbose(args, _("already have %s (%s)"), sha1_to_hex(remote),
738 + print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
739 ref->name);
740 }
741 return retval;
@@ -873,7 +874,7 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
874 char **pack_lockfile)
875 {
876 struct ref *ref = copy_ref_list(orig_ref);
876 - unsigned char sha1[20];
877 + struct object_id oid;
878 const char *agent_feature;
879 int agent_len;
880
@@ -945,7 +946,7 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
946 packet_flush(fd[1]);
947 goto all_done;
948 }
948 - if (find_common(args, fd, sha1, ref) < 0)
949 + if (find_common(args, fd, &oid, ref) < 0)
950 if (!args->keep_pack)
951 /* When cloning, it is not unusual to have
952 * no common commit.