builtin/unpack-objects: convert to struct object_id

Convert struct delta_info and struct object_info, as well as the various functions, to use struct object_id. Convert several hard-coded 20 values to GIT_SHA1_RAWSZ. Among the functions converted is a caller of lookup_blob, which we will convert shortly. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed May 6, 2017 at 22:10 UTC 834bc47b42926dac7bb4d9df43bd1ea58ac794ce
1 file changed +32 -32
builtin/unpack-objects.c
+32 -32
@@ -127,7 +127,7 @@ static void *get_data(unsigned long size)
127 }
128
129 struct delta_info {
130 - unsigned char base_sha1[20];
130 + struct object_id base_oid;
131 unsigned nr;
132 off_t base_offset;
133 unsigned long size;
@@ -137,13 +137,13 @@ struct delta_info {
137
138 static struct delta_info *delta_list;
139
140 -static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
140 +static void add_delta_to_list(unsigned nr, const struct object_id *base_oid,
141 off_t base_offset,
142 void *delta, unsigned long size)
143 {
144 struct delta_info *info = xmalloc(sizeof(*info));
145
146 - hashcpy(info->base_sha1, base_sha1);
146 + oidcpy(&info->base_oid, base_oid);
147 info->base_offset = base_offset;
148 info->size = size;
149 info->delta = delta;
@@ -154,7 +154,7 @@ static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
154
155 struct obj_info {
156 off_t offset;
157 - unsigned char sha1[20];
157 + struct object_id oid;
158 struct object *obj;
159 };
160
@@ -170,9 +170,9 @@ static unsigned nr_objects;
170 */
171 static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
172 {
173 - unsigned char sha1[20];
173 + struct object_id oid;
174
175 - if (write_sha1_file(obj_buf->buffer, obj_buf->size, typename(obj->type), sha1) < 0)
175 + if (write_sha1_file(obj_buf->buffer, obj_buf->size, typename(obj->type), oid.hash) < 0)
176 die("failed to write object %s", oid_to_hex(&obj->oid));
177 obj->flags |= FLAG_WRITTEN;
178 }
@@ -237,19 +237,19 @@ static void write_object(unsigned nr, enum object_type type,
237 void *buf, unsigned long size)
238 {
239 if (!strict) {
240 - if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
240 + if (write_sha1_file(buf, size, typename(type), obj_list[nr].oid.hash) < 0)
241 die("failed to write object");
242 added_object(nr, type, buf, size);
243 free(buf);
244 obj_list[nr].obj = NULL;
245 } else if (type == OBJ_BLOB) {
246 struct blob *blob;
247 - if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
247 + if (write_sha1_file(buf, size, typename(type), obj_list[nr].oid.hash) < 0)
248 die("failed to write object");
249 added_object(nr, type, buf, size);
250 free(buf);
251
252 - blob = lookup_blob(obj_list[nr].sha1);
252 + blob = lookup_blob(obj_list[nr].oid.hash);
253 if (blob)
254 blob->object.flags |= FLAG_WRITTEN;
255 else
@@ -258,9 +258,9 @@ static void write_object(unsigned nr, enum object_type type,
258 } else {
259 struct object *obj;
260 int eaten;
261 - hash_sha1_file(buf, size, typename(type), obj_list[nr].sha1);
261 + hash_sha1_file(buf, size, typename(type), obj_list[nr].oid.hash);
262 added_object(nr, type, buf, size);
263 - obj = parse_object_buffer(obj_list[nr].sha1, type, size, buf, &eaten);
263 + obj = parse_object_buffer(obj_list[nr].oid.hash, type, size, buf, &eaten);
264 if (!obj)
265 die("invalid %s", typename(type));
266 add_object_buffer(obj, buf, size);
@@ -296,7 +296,7 @@ static void added_object(unsigned nr, enum object_type type,
296 struct delta_info *info;
297
298 while ((info = *p) != NULL) {
299 - if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
299 + if (!oidcmp(&info->base_oid, &obj_list[nr].oid) ||
300 info->base_offset == obj_list[nr].offset) {
301 *p = info->next;
302 p = &delta_list;
@@ -320,12 +320,12 @@ static void unpack_non_delta_entry(enum object_type type, unsigned long size,
320 free(buf);
321 }
322
323 -static int resolve_against_held(unsigned nr, const unsigned char *base,
323 +static int resolve_against_held(unsigned nr, const struct object_id *base,
324 void *delta_data, unsigned long delta_size)
325 {
326 struct object *obj;
327 struct obj_buffer *obj_buffer;
328 - obj = lookup_object(base);
328 + obj = lookup_object(base->hash);
329 if (!obj)
330 return 0;
331 obj_buffer = lookup_object_buffer(obj);
@@ -341,25 +341,25 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
341 {
342 void *delta_data, *base;
343 unsigned long base_size;
344 - unsigned char base_sha1[20];
344 + struct object_id base_oid;
345
346 if (type == OBJ_REF_DELTA) {
347 - hashcpy(base_sha1, fill(20));
348 - use(20);
347 + hashcpy(base_oid.hash, fill(GIT_SHA1_RAWSZ));
348 + use(GIT_SHA1_RAWSZ);
349 delta_data = get_data(delta_size);
350 if (dry_run || !delta_data) {
351 free(delta_data);
352 return;
353 }
354 - if (has_sha1_file(base_sha1))
354 + if (has_object_file(&base_oid))
355 ; /* Ok we have this one */
356 - else if (resolve_against_held(nr, base_sha1,
356 + else if (resolve_against_held(nr, &base_oid,
357 delta_data, delta_size))
358 return; /* we are done */
359 else {
360 /* cannot resolve yet --- queue it */
361 - hashclr(obj_list[nr].sha1);
362 - add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
361 + oidclr(&obj_list[nr].oid);
362 + add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size);
363 return;
364 }
365 } else {
@@ -399,8 +399,8 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
399 } else if (base_offset > obj_list[mid].offset) {
400 lo = mid + 1;
401 } else {
402 - hashcpy(base_sha1, obj_list[mid].sha1);
403 - base_found = !is_null_sha1(base_sha1);
402 + oidcpy(&base_oid, &obj_list[mid].oid);
403 + base_found = !is_null_oid(&base_oid);
404 break;
405 }
406 }
@@ -409,19 +409,19 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
409 * The delta base object is itself a delta that
410 * has not been resolved yet.
411 */
412 - hashclr(obj_list[nr].sha1);
413 - add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
412 + oidclr(&obj_list[nr].oid);
413 + add_delta_to_list(nr, &null_oid, base_offset, delta_data, delta_size);
414 return;
415 }
416 }
417
418 - if (resolve_against_held(nr, base_sha1, delta_data, delta_size))
418 + if (resolve_against_held(nr, &base_oid, delta_data, delta_size))
419 return;
420
421 - base = read_sha1_file(base_sha1, &type, &base_size);
421 + base = read_sha1_file(base_oid.hash, &type, &base_size);
422 if (!base) {
423 error("failed to read delta-pack base object %s",
424 - sha1_to_hex(base_sha1));
424 + oid_to_hex(&base_oid));
425 if (!recover)
426 exit(1);
427 has_errors = 1;
@@ -505,7 +505,7 @@ static void unpack_all(void)
505 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
506 {
507 int i;
508 - unsigned char sha1[20];
508 + struct object_id oid;
509
510 check_replace_refs = 0;
511
@@ -566,12 +566,12 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
566 git_SHA1_Init(&ctx);
567 unpack_all();
568 git_SHA1_Update(&ctx, buffer, offset);
569 - git_SHA1_Final(sha1, &ctx);
569 + git_SHA1_Final(oid.hash, &ctx);
570 if (strict)
571 write_rest();
572 - if (hashcmp(fill(20), sha1))
572 + if (hashcmp(fill(GIT_SHA1_RAWSZ), oid.hash))
573 die("final sha1 did not match");
574 - use(20);
574 + use(GIT_SHA1_RAWSZ);
575
576 /* Write the last part of the buffer to stdout */
577 while (len) {