Merge branch 'ks/branch-set-upstream' into next
[gitweb.git] / builtin / unpack-objects.c
index 4532aa083154ae7e9be07014d3761821fa6c4ddc..689a29fac1a50df6e5b8fc3cda617ef1c6d060b3 100644 (file)
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "object.h"
 #include "delta.h"
 #include "pack.h"
@@ -112,8 +113,7 @@ static void *get_data(unsigned long size)
                        break;
                if (ret != Z_OK) {
                        error("inflate returned %d", ret);
-                       free(buf);
-                       buf = NULL;
+                       FREE_AND_NULL(buf);
                        if (!recover)
                                exit(1);
                        has_errors = 1;
@@ -127,7 +127,7 @@ static void *get_data(unsigned long size)
 }
 
 struct delta_info {
-       unsigned char base_sha1[20];
+       struct object_id base_oid;
        unsigned nr;
        off_t base_offset;
        unsigned long size;
@@ -137,13 +137,13 @@ struct delta_info {
 
 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,
                              void *delta, unsigned long size)
 {
        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->size = size;
        info->delta = delta;
@@ -154,7 +154,7 @@ static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
 
 struct obj_info {
        off_t offset;
-       unsigned char sha1[20];
+       struct object_id oid;
        struct object *obj;
 };
 
@@ -170,9 +170,9 @@ static unsigned nr_objects;
  */
 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));
        obj->flags |= FLAG_WRITTEN;
 }
@@ -237,19 +237,19 @@ static void write_object(unsigned nr, enum object_type type,
                         void *buf, unsigned long size)
 {
        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");
                added_object(nr, type, buf, size);
                free(buf);
                obj_list[nr].obj = NULL;
        } else if (type == OBJ_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");
                added_object(nr, type, buf, size);
                free(buf);
 
-               blob = lookup_blob(obj_list[nr].sha1);
+               blob = lookup_blob(&obj_list[nr].oid);
                if (blob)
                        blob->object.flags |= FLAG_WRITTEN;
                else
@@ -258,9 +258,10 @@ static void write_object(unsigned nr, enum object_type type,
        } else {
                struct object *obj;
                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);
-               obj = parse_object_buffer(obj_list[nr].sha1, type, size, buf, &eaten);
+               obj = parse_object_buffer(&obj_list[nr].oid, type, size, buf,
+                                         &eaten);
                if (!obj)
                        die("invalid %s", typename(type));
                add_object_buffer(obj, buf, size);
@@ -296,7 +297,7 @@ static void added_object(unsigned nr, enum object_type type,
        struct delta_info *info;
 
        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) {
                        *p = info->next;
                        p = &delta_list;
@@ -320,12 +321,12 @@ static void unpack_non_delta_entry(enum object_type type, unsigned long size,
                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)
 {
        struct object *obj;
        struct obj_buffer *obj_buffer;
-       obj = lookup_object(base);
+       obj = lookup_object(base->hash);
        if (!obj)
                return 0;
        obj_buffer = lookup_object_buffer(obj);
@@ -341,25 +342,25 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
 {
        void *delta_data, *base;
        unsigned long base_size;
-       unsigned char base_sha1[20];
+       struct object_id base_oid;
 
        if (type == OBJ_REF_DELTA) {
-               hashcpy(base_sha1, fill(20));
-               use(20);
+               hashcpy(base_oid.hash, fill(GIT_SHA1_RAWSZ));
+               use(GIT_SHA1_RAWSZ);
                delta_data = get_data(delta_size);
                if (dry_run || !delta_data) {
                        free(delta_data);
                        return;
                }
-               if (has_sha1_file(base_sha1))
+               if (has_object_file(&base_oid))
                        ; /* 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))
                        return; /* we are done */
                else {
                        /* cannot resolve yet --- queue it */
-                       hashclr(obj_list[nr].sha1);
-                       add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
+                       oidclr(&obj_list[nr].oid);
+                       add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size);
                        return;
                }
        } else {
@@ -399,8 +400,8 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
                        } else if (base_offset > obj_list[mid].offset) {
                                lo = mid + 1;
                        } else {
-                               hashcpy(base_sha1, obj_list[mid].sha1);
-                               base_found = !is_null_sha1(base_sha1);
+                               oidcpy(&base_oid, &obj_list[mid].oid);
+                               base_found = !is_null_oid(&base_oid);
                                break;
                        }
                }
@@ -409,19 +410,19 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
                         * The delta base object is itself a delta that
                         * has not been resolved yet.
                         */
-                       hashclr(obj_list[nr].sha1);
-                       add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
+                       oidclr(&obj_list[nr].oid);
+                       add_delta_to_list(nr, &null_oid, base_offset, delta_data, delta_size);
                        return;
                }
        }
 
-       if (resolve_against_held(nr, base_sha1, delta_data, delta_size))
+       if (resolve_against_held(nr, &base_oid, delta_data, delta_size))
                return;
 
-       base = read_sha1_file(base_sha1, &type, &base_size);
+       base = read_sha1_file(base_oid.hash, &type, &base_size);
        if (!base) {
                error("failed to read delta-pack base object %s",
-                     sha1_to_hex(base_sha1));
+                     oid_to_hex(&base_oid));
                if (!recover)
                        exit(1);
                has_errors = 1;
@@ -505,7 +506,7 @@ static void unpack_all(void)
 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
 {
        int i;
-       unsigned char sha1[20];
+       struct object_id oid;
 
        check_replace_refs = 0;
 
@@ -566,12 +567,12 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
        git_SHA1_Init(&ctx);
        unpack_all();
        git_SHA1_Update(&ctx, buffer, offset);
-       git_SHA1_Final(sha1, &ctx);
+       git_SHA1_Final(oid.hash, &ctx);
        if (strict)
                write_rest();
-       if (hashcmp(fill(20), sha1))
+       if (hashcmp(fill(GIT_SHA1_RAWSZ), oid.hash))
                die("final sha1 did not match");
-       use(20);
+       use(GIT_SHA1_RAWSZ);
 
        /* Write the last part of the buffer to stdout */
        while (len) {