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>
This commit is contained in:
brian m. carlson
2017-05-06 22:10:12 +00:00
committed by Junio C Hamano
parent e6a492b7be
commit 834bc47b42

View File

@ -127,7 +127,7 @@ static void *get_data(unsigned long size)
} }
struct delta_info { struct delta_info {
unsigned char base_sha1[20]; struct object_id base_oid;
unsigned nr; unsigned nr;
off_t base_offset; off_t base_offset;
unsigned long size; unsigned long size;
@ -137,13 +137,13 @@ struct delta_info {
static struct delta_info *delta_list; static struct delta_info *delta_list;
static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1, static void add_delta_to_list(unsigned nr, const struct object_id *base_oid,
off_t base_offset, off_t base_offset,
void *delta, unsigned long size) void *delta, unsigned long size)
{ {
struct delta_info *info = xmalloc(sizeof(*info)); struct delta_info *info = xmalloc(sizeof(*info));
hashcpy(info->base_sha1, base_sha1); oidcpy(&info->base_oid, base_oid);
info->base_offset = base_offset; info->base_offset = base_offset;
info->size = size; info->size = size;
info->delta = delta; info->delta = delta;
@ -154,7 +154,7 @@ static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
struct obj_info { struct obj_info {
off_t offset; off_t offset;
unsigned char sha1[20]; struct object_id oid;
struct object *obj; struct object *obj;
}; };
@ -170,9 +170,9 @@ static unsigned nr_objects;
*/ */
static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf) static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
{ {
unsigned char sha1[20]; struct object_id oid;
if (write_sha1_file(obj_buf->buffer, obj_buf->size, typename(obj->type), sha1) < 0) if (write_sha1_file(obj_buf->buffer, obj_buf->size, typename(obj->type), oid.hash) < 0)
die("failed to write object %s", oid_to_hex(&obj->oid)); die("failed to write object %s", oid_to_hex(&obj->oid));
obj->flags |= FLAG_WRITTEN; obj->flags |= FLAG_WRITTEN;
} }
@ -237,19 +237,19 @@ static void write_object(unsigned nr, enum object_type type,
void *buf, unsigned long size) void *buf, unsigned long size)
{ {
if (!strict) { if (!strict) {
if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0) if (write_sha1_file(buf, size, typename(type), obj_list[nr].oid.hash) < 0)
die("failed to write object"); die("failed to write object");
added_object(nr, type, buf, size); added_object(nr, type, buf, size);
free(buf); free(buf);
obj_list[nr].obj = NULL; obj_list[nr].obj = NULL;
} else if (type == OBJ_BLOB) { } else if (type == OBJ_BLOB) {
struct blob *blob; struct blob *blob;
if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0) if (write_sha1_file(buf, size, typename(type), obj_list[nr].oid.hash) < 0)
die("failed to write object"); die("failed to write object");
added_object(nr, type, buf, size); added_object(nr, type, buf, size);
free(buf); free(buf);
blob = lookup_blob(obj_list[nr].sha1); blob = lookup_blob(obj_list[nr].oid.hash);
if (blob) if (blob)
blob->object.flags |= FLAG_WRITTEN; blob->object.flags |= FLAG_WRITTEN;
else else
@ -258,9 +258,9 @@ static void write_object(unsigned nr, enum object_type type,
} else { } else {
struct object *obj; struct object *obj;
int eaten; int eaten;
hash_sha1_file(buf, size, typename(type), obj_list[nr].sha1); hash_sha1_file(buf, size, typename(type), obj_list[nr].oid.hash);
added_object(nr, type, buf, size); added_object(nr, type, buf, size);
obj = parse_object_buffer(obj_list[nr].sha1, type, size, buf, &eaten); obj = parse_object_buffer(obj_list[nr].oid.hash, type, size, buf, &eaten);
if (!obj) if (!obj)
die("invalid %s", typename(type)); die("invalid %s", typename(type));
add_object_buffer(obj, buf, size); add_object_buffer(obj, buf, size);
@ -296,7 +296,7 @@ static void added_object(unsigned nr, enum object_type type,
struct delta_info *info; struct delta_info *info;
while ((info = *p) != NULL) { while ((info = *p) != NULL) {
if (!hashcmp(info->base_sha1, obj_list[nr].sha1) || if (!oidcmp(&info->base_oid, &obj_list[nr].oid) ||
info->base_offset == obj_list[nr].offset) { info->base_offset == obj_list[nr].offset) {
*p = info->next; *p = info->next;
p = &delta_list; p = &delta_list;
@ -320,12 +320,12 @@ static void unpack_non_delta_entry(enum object_type type, unsigned long size,
free(buf); free(buf);
} }
static int resolve_against_held(unsigned nr, const unsigned char *base, static int resolve_against_held(unsigned nr, const struct object_id *base,
void *delta_data, unsigned long delta_size) void *delta_data, unsigned long delta_size)
{ {
struct object *obj; struct object *obj;
struct obj_buffer *obj_buffer; struct obj_buffer *obj_buffer;
obj = lookup_object(base); obj = lookup_object(base->hash);
if (!obj) if (!obj)
return 0; return 0;
obj_buffer = lookup_object_buffer(obj); obj_buffer = lookup_object_buffer(obj);
@ -341,25 +341,25 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
{ {
void *delta_data, *base; void *delta_data, *base;
unsigned long base_size; unsigned long base_size;
unsigned char base_sha1[20]; struct object_id base_oid;
if (type == OBJ_REF_DELTA) { if (type == OBJ_REF_DELTA) {
hashcpy(base_sha1, fill(20)); hashcpy(base_oid.hash, fill(GIT_SHA1_RAWSZ));
use(20); use(GIT_SHA1_RAWSZ);
delta_data = get_data(delta_size); delta_data = get_data(delta_size);
if (dry_run || !delta_data) { if (dry_run || !delta_data) {
free(delta_data); free(delta_data);
return; return;
} }
if (has_sha1_file(base_sha1)) if (has_object_file(&base_oid))
; /* Ok we have this one */ ; /* Ok we have this one */
else if (resolve_against_held(nr, base_sha1, else if (resolve_against_held(nr, &base_oid,
delta_data, delta_size)) delta_data, delta_size))
return; /* we are done */ return; /* we are done */
else { else {
/* cannot resolve yet --- queue it */ /* cannot resolve yet --- queue it */
hashclr(obj_list[nr].sha1); oidclr(&obj_list[nr].oid);
add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size); add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size);
return; return;
} }
} else { } else {
@ -399,8 +399,8 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
} else if (base_offset > obj_list[mid].offset) { } else if (base_offset > obj_list[mid].offset) {
lo = mid + 1; lo = mid + 1;
} else { } else {
hashcpy(base_sha1, obj_list[mid].sha1); oidcpy(&base_oid, &obj_list[mid].oid);
base_found = !is_null_sha1(base_sha1); base_found = !is_null_oid(&base_oid);
break; break;
} }
} }
@ -409,19 +409,19 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
* The delta base object is itself a delta that * The delta base object is itself a delta that
* has not been resolved yet. * has not been resolved yet.
*/ */
hashclr(obj_list[nr].sha1); oidclr(&obj_list[nr].oid);
add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size); add_delta_to_list(nr, &null_oid, base_offset, delta_data, delta_size);
return; return;
} }
} }
if (resolve_against_held(nr, base_sha1, delta_data, delta_size)) if (resolve_against_held(nr, &base_oid, delta_data, delta_size))
return; return;
base = read_sha1_file(base_sha1, &type, &base_size); base = read_sha1_file(base_oid.hash, &type, &base_size);
if (!base) { if (!base) {
error("failed to read delta-pack base object %s", error("failed to read delta-pack base object %s",
sha1_to_hex(base_sha1)); oid_to_hex(&base_oid));
if (!recover) if (!recover)
exit(1); exit(1);
has_errors = 1; has_errors = 1;
@ -505,7 +505,7 @@ static void unpack_all(void)
int cmd_unpack_objects(int argc, const char **argv, const char *prefix) int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
{ {
int i; int i;
unsigned char sha1[20]; struct object_id oid;
check_replace_refs = 0; check_replace_refs = 0;
@ -566,12 +566,12 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
git_SHA1_Init(&ctx); git_SHA1_Init(&ctx);
unpack_all(); unpack_all();
git_SHA1_Update(&ctx, buffer, offset); git_SHA1_Update(&ctx, buffer, offset);
git_SHA1_Final(sha1, &ctx); git_SHA1_Final(oid.hash, &ctx);
if (strict) if (strict)
write_rest(); write_rest();
if (hashcmp(fill(20), sha1)) if (hashcmp(fill(GIT_SHA1_RAWSZ), oid.hash))
die("final sha1 did not match"); die("final sha1 did not match");
use(20); use(GIT_SHA1_RAWSZ);
/* Write the last part of the buffer to stdout */ /* Write the last part of the buffer to stdout */
while (len) { while (len) {