bundle: convert to struct object_id
Convert the bundle code, plus the sole external user of struct ref_list_entry, to use struct object_id. Include cache.h from within bundle.h to provide the definition. Convert some of the hash parsing code to use parse_oid_hex to avoid needing to hard-code constant values. 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
b8607f35b180a00b3f3240ff7d26d034a83fb23a
3 files changed
+21
-18
bundle.c
+17
-16
@@ -12,11 +12,11 @@
12
13
static const char bundle_signature[] = "# v2 git bundle\n";
14
15
-static void add_to_ref_list(const unsigned char *sha1, const char *name,
15
+static void add_to_ref_list(const struct object_id *oid, const char *name,
16
struct ref_list *list)
17
{
18
ALLOC_GROW(list->list, list->nr + 1, list->alloc);
19
- hashcpy(list->list[list->nr].sha1, sha1);
19
+ oidcpy(&list->list[list->nr].oid, oid);
20
list->list[list->nr].name = xstrdup(name);
21
list->nr++;
22
}
@@ -40,8 +40,9 @@ static int parse_bundle_header(int fd, struct bundle_header *header,
40
/* The bundle header ends with an empty line */
41
while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
42
buf.len && buf.buf[0] != '\n') {
43
- unsigned char sha1[20];
43
+ struct object_id oid;
44
int is_prereq = 0;
45
+ const char *p;
46
47
if (*buf.buf == '-') {
48
is_prereq = 1;
@@ -54,9 +55,9 @@ static int parse_bundle_header(int fd, struct bundle_header *header,
55
* Prerequisites have object name that is optionally
56
* followed by SP and subject line.
57
*/
57
- if (get_sha1_hex(buf.buf, sha1) ||
58
- (buf.len > 40 && !isspace(buf.buf[40])) ||
59
- (!is_prereq && buf.len <= 40)) {
58
+ if (parse_oid_hex(buf.buf, &oid, &p) ||
59
+ (*p && !isspace(*p)) ||
60
+ (!is_prereq && !*p)) {
61
if (report_path)
62
error(_("unrecognized header: %s%s (%d)"),
63
(is_prereq ? "-" : ""), buf.buf, (int)buf.len);
@@ -64,9 +65,9 @@ static int parse_bundle_header(int fd, struct bundle_header *header,
65
break;
66
} else {
67
if (is_prereq)
67
- add_to_ref_list(sha1, "", &header->prerequisites);
68
+ add_to_ref_list(&oid, "", &header->prerequisites);
69
else
69
- add_to_ref_list(sha1, buf.buf + 41, &header->references);
70
+ add_to_ref_list(&oid, p + 1, &header->references);
71
}
72
}
73
@@ -115,7 +116,7 @@ static int list_refs(struct ref_list *r, int argc, const char **argv)
116
if (j == argc)
117
continue;
118
}
118
- printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
119
+ printf("%s %s\n", oid_to_hex(&r->list[i].oid),
120
r->list[i].name);
121
}
122
return 0;
@@ -141,7 +142,7 @@ int verify_bundle(struct bundle_header *header, int verbose)
142
init_revisions(&revs, NULL);
143
for (i = 0; i < p->nr; i++) {
144
struct ref_list_entry *e = p->list + i;
144
- struct object *o = parse_object(e->sha1);
145
+ struct object *o = parse_object(e->oid.hash);
146
if (o) {
147
o->flags |= PREREQ_MARK;
148
add_pending_object(&revs, o, e->name);
@@ -149,7 +150,7 @@ int verify_bundle(struct bundle_header *header, int verbose)
150
}
151
if (++ret == 1)
152
error("%s", message);
152
- error("%s %s", sha1_to_hex(e->sha1), e->name);
153
+ error("%s %s", oid_to_hex(&e->oid), e->name);
154
}
155
if (revs.pending.nr != p->nr)
156
return ret;
@@ -285,16 +286,16 @@ static int compute_and_write_prerequisites(int bundle_fd,
286
return -1;
287
rls_fout = xfdopen(rls.out, "r");
288
while (strbuf_getwholeline(&buf, rls_fout, '\n') != EOF) {
288
- unsigned char sha1[20];
289
+ struct object_id oid;
290
if (buf.len > 0 && buf.buf[0] == '-') {
291
write_or_die(bundle_fd, buf.buf, buf.len);
291
- if (!get_sha1_hex(buf.buf + 1, sha1)) {
292
- struct object *object = parse_object_or_die(sha1, buf.buf);
292
+ if (!get_oid_hex(buf.buf + 1, &oid)) {
293
+ struct object *object = parse_object_or_die(oid.hash, buf.buf);
294
object->flags |= UNINTERESTING;
295
add_pending_object(revs, object, buf.buf);
296
}
296
- } else if (!get_sha1_hex(buf.buf, sha1)) {
297
- struct object *object = parse_object_or_die(sha1, buf.buf);
297
+ } else if (!get_oid_hex(buf.buf, &oid)) {
298
+ struct object *object = parse_object_or_die(oid.hash, buf.buf);
299
object->flags |= SHOWN;
300
}
301
}
bundle.h
+3
-1
@@ -1,10 +1,12 @@
1
#ifndef BUNDLE_H
2
#define BUNDLE_H
3
4
+#include "cache.h"
5
+
6
struct ref_list {
7
unsigned int nr, alloc;
8
struct ref_list_entry {
7
- unsigned char sha1[20];
9
+ struct object_id oid;
10
char *name;
11
} *list;
12
};
transport.c
+1
-1
@@ -87,7 +87,7 @@ static struct ref *get_refs_from_bundle(struct transport *transport, int for_pus
87
for (i = 0; i < data->header.references.nr; i++) {
88
struct ref_list_entry *e = data->header.references.list + i;
89
struct ref *ref = alloc_ref(e->name);
90
- hashcpy(ref->old_oid.hash, e->sha1);
90
+ oidcpy(&ref->old_oid, &e->oid);
91
ref->next = result;
92
result = ref;
93
}