#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";
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 {
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)
{
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];
}
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;
}
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;
entry->offset = offset;
offset += write_object(f, entry);
}
- fclose(f);
+ sha1close(f, pack_file_sha1, 1);
mb = offset >> 20;
offset &= 0xfffff;
}
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];
/*
* 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;
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..
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)
static void check_object(struct object_entry *entry)
{
- char buffer[128];
- char type[10];
- unsigned long mapsize;
- z_stream stream;
- void *map;
-
- map = map_sha1_file(entry->sha1, &mapsize);
- if (!map)
- die("unable to map %s", sha1_to_hex(entry->sha1));
- if (unpack_sha1_header(&stream, map, mapsize, buffer, sizeof(buffer)) < 0)
- die("unable to unpack %s header", sha1_to_hex(entry->sha1));
- munmap(map, mapsize);
- if (parse_sha1_header(buffer, type, &entry->size) < 0)
- die("unable to parse %s header", sha1_to_hex(entry->sha1));
- if (!strcmp(type, "commit")) {
- entry->type = OBJ_COMMIT;
- } else if (!strcmp(type, "tree")) {
- entry->type = OBJ_TREE;
- } else if (!strcmp(type, "blob")) {
- entry->type = OBJ_BLOB;
- } else
- die("unable to pack object %s of type %s", sha1_to_hex(entry->sha1), type);
+ char type[20];
+
+ if (!sha1_object_info(entry->sha1, type, &entry->size)) {
+ if (!strcmp(type, "commit")) {
+ entry->type = OBJ_COMMIT;
+ } else if (!strcmp(type, "tree")) {
+ entry->type = OBJ_TREE;
+ } else if (!strcmp(type, "blob")) {
+ entry->type = OBJ_BLOB;
+ } else
+ die("unable to pack object %s of type %s",
+ sha1_to_hex(entry->sha1), type);
+ }
+ else
+ die("unable to get type of object %s",
+ sha1_to_hex(entry->sha1));
}
static void get_object_details(void)