[PATCH] Remove "delta" object representation.
[git.git] / pack-objects.c
index bc8bb95..102af30 100644 (file)
@@ -2,6 +2,7 @@
 #include "cache.h"
 #include "object.h"
 #include "delta.h"
+#include "csum-file.h"
 
 static const char pack_usage[] = "git-pack-objects [--window=N] [--depth=N] base-name < object-list";
 
@@ -10,7 +11,7 @@ enum object_type {
        OBJ_COMMIT,
        OBJ_TREE,
        OBJ_BLOB,
-       OBJ_DELTA       // NOTE! This is _not_ the same as a "delta" object in the filesystem
+       OBJ_DELTA
 };
 
 struct object_entry {
@@ -28,51 +29,7 @@ static struct object_entry **sorted_by_sha, **sorted_by_type;
 static struct object_entry *objects = NULL;
 static int nr_objects = 0, nr_alloc = 0;
 static const char *base_name;
-
-struct myfile {
-       int fd;
-       unsigned long chars;
-       unsigned char buffer[8192];
-};
-
-static FILE *create_file(const char *suffix)
-{
-       static char filename[PATH_MAX];
-       unsigned len;
-
-       len = snprintf(filename, PATH_MAX, "%s.%s", base_name, suffix);
-       if (len >= PATH_MAX)
-               die("you wascally wabbit, you");
-       return fopen(filename, "w");
-}
-
-static unsigned long fwrite_compressed(void *in, unsigned long size, FILE *f)
-{
-       z_stream stream;
-       unsigned long maxsize;
-       void *out;
-
-       memset(&stream, 0, sizeof(stream));
-       deflateInit(&stream, Z_DEFAULT_COMPRESSION);
-       maxsize = deflateBound(&stream, size);
-       out = xmalloc(maxsize);
-
-       /* Compress it */
-       stream.next_in = in;
-       stream.avail_in = size;
-
-       stream.next_out = out;
-       stream.avail_out = maxsize;
-
-       while (deflate(&stream, Z_FINISH) == Z_OK)
-               /* nothing */;
-       deflateEnd(&stream);
-
-       size = stream.total_out;
-       fwrite(out, size, 1, f);
-       free(out);
-       return size;
-}
+static unsigned char pack_file_sha1[20];
 
 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
 {
@@ -92,7 +49,7 @@ static void *delta_against(void *buf, unsigned long size, struct object_entry *e
        return delta_buf;
 }
 
-static unsigned long write_object(FILE *f, struct object_entry *entry)
+static unsigned long write_object(struct sha1file *f, struct object_entry *entry)
 {
        unsigned long size;
        char type[10];
@@ -121,8 +78,8 @@ static unsigned long write_object(FILE *f, struct object_entry *entry)
        }
        datalen = htonl(size);
        memcpy(header+1, &datalen, 4);
-       fwrite(header, hdrlen, 1, f);
-       datalen = fwrite_compressed(buf, size, f);
+       sha1write(f, header, hdrlen);
+       datalen = sha1write_compressed(f, buf, size);
        free(buf);
        return hdrlen + datalen;
 }
@@ -130,7 +87,7 @@ static unsigned long write_object(FILE *f, struct object_entry *entry)
 static void write_pack_file(void)
 {
        int i;
-       FILE *f = create_file("pack");
+       struct sha1file *f = sha1create("%s.%s", base_name, "pack");
        unsigned long offset = 0;
        unsigned long mb;
 
@@ -139,7 +96,7 @@ static void write_pack_file(void)
                entry->offset = offset;
                offset += write_object(f, entry);
        }
-       fclose(f);
+       sha1close(f, pack_file_sha1, 1);
        mb = offset >> 20;
        offset &= 0xfffff;
 }
@@ -147,7 +104,7 @@ static void write_pack_file(void)
 static void write_index_file(void)
 {
        int i;
-       FILE *f = create_file("idx");
+       struct sha1file *f = sha1create("%s.%s", base_name, "idx");
        struct object_entry **list = sorted_by_sha;
        struct object_entry **last = list + nr_objects;
        unsigned int array[256];
@@ -155,7 +112,7 @@ static void write_index_file(void)
        /*
         * Write the first-level table (the list is sorted,
         * but we use a 256-entry lookup to be able to avoid
-        * having to do eight extra binary search iterations)
+        * having to do eight extra binary search iterations).
         */
        for (i = 0; i < 256; i++) {
                struct object_entry **next = list;
@@ -168,7 +125,7 @@ static void write_index_file(void)
                array[i] = htonl(next - sorted_by_sha);
                list = next;
        }
-       fwrite(array, 256, sizeof(int), f);
+       sha1write(f, array, 256 * sizeof(int));
 
        /*
         * Write the actual SHA1 entries..
@@ -177,10 +134,11 @@ static void write_index_file(void)
        for (i = 0; i < nr_objects; i++) {
                struct object_entry *entry = *list++;
                unsigned int offset = htonl(entry->offset);
-               fwrite(&offset, 4, 1, f);
-               fwrite(entry->sha1, 20, 1, f);
+               sha1write(f, &offset, 4);
+               sha1write(f, entry->sha1, 20);
        }
-       fclose(f);
+       sha1write(f, pack_file_sha1, 20);
+       sha1close(f, NULL, 1);
 }
 
 static void add_object_entry(unsigned char *sha1, unsigned int hash)