]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Simplify "write_sha1_file()" interfaces
authorLinus Torvalds <torvalds@ppc970.osdl.org>
Mon, 25 Apr 2005 17:19:53 +0000 (10:19 -0700)
committerLinus Torvalds <torvalds@ppc970.osdl.org>
Mon, 25 Apr 2005 17:19:53 +0000 (10:19 -0700)
The write function now adds the header to the file by itself, so there
is no reason to duplicate it among all the users any more.

cache.h
commit-tree.c
convert-cache.c
sha1_file.c
write-tree.c

diff --git a/cache.h b/cache.h
index 794d676a5cf5c9a03309c4b368840f8707cfcf46..226788a5bbe812ae00335560bce20d57899119b6 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -121,7 +121,7 @@ extern int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int
 extern void * map_sha1_file(const unsigned char *sha1, unsigned long *size);
 extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size);
 extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
-extern int write_sha1_file(char *buf, unsigned len, unsigned char *return_sha1);
+extern int write_sha1_file(char *buf, unsigned len, const char *type, unsigned char *return_sha1);
 
 extern int check_sha1_signature(unsigned char *sha1, void *buf, unsigned long size, const char *type);
 
index c0b07f89286c3f6cceae8122b4c3142c8efaf8e1..f6e408756042868b368cf403393c5b6c96343cbb 100644 (file)
 #include <time.h>
 
 #define BLOCKING (1ul << 14)
-#define ORIG_OFFSET (40)
 
 /*
- * Leave space at the beginning to insert the tag
- * once we know how big things are.
- *
  * FIXME! Share the code with "write-tree.c"
  */
 static void init_buffer(char **bufp, unsigned int *sizep)
 {
        char *buf = malloc(BLOCKING);
-       memset(buf, 0, ORIG_OFFSET);
-       *sizep = ORIG_OFFSET;
+       *sizep = 0;
        *bufp = buf;
 }
 
@@ -52,34 +47,6 @@ static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
        memcpy(buf + size, one_line, len);
 }
 
-static int prepend_integer(char *buffer, unsigned val, int i)
-{
-       buffer[--i] = '\0';
-       do {
-               buffer[--i] = '0' + (val % 10);
-               val /= 10;
-       } while (val);
-       return i;
-}
-
-static void finish_buffer(char *tag, char **bufp, unsigned int *sizep)
-{
-       int taglen;
-       int offset;
-       char *buf = *bufp;
-       unsigned int size = *sizep;
-
-       offset = prepend_integer(buf, size - ORIG_OFFSET, ORIG_OFFSET);
-       taglen = strlen(tag);
-       offset -= taglen;
-       buf += offset;
-       size -= offset;
-       memcpy(buf, tag, taglen);
-
-       *bufp = buf;
-       *sizep = size;
-}
-
 static void remove_special(char *p)
 {
        char c;
@@ -355,9 +322,7 @@ int main(int argc, char **argv)
        while (fgets(comment, sizeof(comment), stdin) != NULL)
                add_buffer(&buffer, &size, "%s", comment);
 
-       finish_buffer("commit ", &buffer, &size);
-
-       write_sha1_file(buffer, size, commit_sha1);
+       write_sha1_file(buffer, size, "commit", commit_sha1);
        printf("%s\n", sha1_to_hex(commit_sha1));
        return 0;
 }
index 431b11798816a88c78daa846fdebf41ed57f1378..7102e455163563894448fd7090d2409e379ed314 100644 (file)
@@ -66,25 +66,11 @@ static void convert_ascii_sha1(void *buffer)
        memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
 }
 
-#define ORIG_OFFSET (40)
-
-static int prepend_integer(char *buffer, unsigned val, int i)
-{
-       buffer[--i] = '\0';
-       do {
-               buffer[--i] = '0' + (val % 10);
-               val /= 10;
-       } while (val);
-       return i;
-}
-
-
 static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
 {
-       char *new = malloc(size + ORIG_OFFSET);
-       unsigned long newlen = ORIG_OFFSET;
+       char *new = malloc(size);
+       unsigned long newlen = 0;
        unsigned long used;
-       int i;
 
        used = 0;
        while (size) {
@@ -126,11 +112,7 @@ static int write_subdirectory(void *buffer, unsigned long size, const char *base
                buffer += len;
        }
 
-       i = prepend_integer(new, newlen - ORIG_OFFSET, ORIG_OFFSET);
-       i -= 5;
-       memcpy(new + i, "tree ", 5);
-
-       write_sha1_file(new + i, newlen - i, result_sha1);
+       write_sha1_file(new, newlen, "tree", result_sha1);
        free(new);
        return used;
 }
@@ -244,9 +226,8 @@ static int convert_date_line(char *dst, void **buf, unsigned long *sp)
 
 static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
 {
-       char *new = malloc(size + ORIG_OFFSET + 100);
-       unsigned long newlen = ORIG_OFFSET;
-       int i;
+       char *new = malloc(size + 100);
+       unsigned long newlen = 0;
 
        // "tree <sha1>\n"
        memcpy(new + newlen, buffer, 46);
@@ -271,11 +252,7 @@ static void convert_date(void *buffer, unsigned long size, unsigned char *result
        memcpy(new + newlen, buffer, size);
        newlen += size;
 
-       i = prepend_integer(new, newlen - ORIG_OFFSET, ORIG_OFFSET);
-       i -= 7;
-       memcpy(new + i, "commit ", 7);
-
-       write_sha1_file(new + i, newlen - i, result_sha1);
+       write_sha1_file(new, newlen, "commit", result_sha1);
        free(new);      
 }
 
@@ -298,7 +275,7 @@ static struct entry * convert_entry(unsigned char *sha1)
        struct entry *entry = lookup_entry(sha1);
        char type[20];
        void *buffer, *data;
-       unsigned long size, offset;
+       unsigned long size;
 
        if (entry->converted)
                return entry;
@@ -306,16 +283,15 @@ static struct entry * convert_entry(unsigned char *sha1)
        if (!data)
                die("unable to read object %s", sha1_to_hex(sha1));
 
-       buffer = malloc(size + 100);
-       offset = sprintf(buffer, "%s %lu", type, size)+1;
-       memcpy(buffer + offset, data, size);
+       buffer = malloc(size);
+       memcpy(buffer, data, size);
        
        if (!strcmp(type, "blob")) {
-               write_sha1_file(buffer, size + offset, entry->new_sha1);
+               write_sha1_file(buffer, size, "blob", entry->new_sha1);
        } else if (!strcmp(type, "tree"))
-               convert_tree(buffer + offset, size, entry->new_sha1);
+               convert_tree(buffer, size, entry->new_sha1);
        else if (!strcmp(type, "commit"))
-               convert_commit(buffer + offset, size, entry->new_sha1);
+               convert_commit(buffer, size, entry->new_sha1);
        else
                die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
        entry->converted = 1;
index 28ad1598e54200ca8ee1261ed7beb4e31e20b2f1..d2f38f0cdc78d81ffb5a601eb603a81775586368 100644 (file)
@@ -155,8 +155,8 @@ void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned l
 
        inflateInit(&stream);
        ret = inflate(&stream, 0);
-       if (ret < Z_OK)
-               return NULL;
+       if (ret < Z_OK)
+               return NULL;
        if (sscanf(buffer, "%10s %lu", type, size) != 2)
                return NULL;
 
@@ -231,7 +231,7 @@ void *read_tree_with_tree_or_commit_sha1(const unsigned char *sha1,
        return buffer;
 }
 
-int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1)
+int write_sha1_file(char *buf, unsigned len, const char *type, unsigned char *returnsha1)
 {
        int size;
        char *compressed;
@@ -239,10 +239,15 @@ int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1)
        unsigned char sha1[20];
        SHA_CTX c;
        char *filename;
-       int fd;
+       char hdr[50];
+       int fd, hdrlen;
+
+       /* Generate the header */
+       hdrlen = sprintf(hdr, "%s %d", type, len)+1;
 
        /* Sha1.. */
        SHA1_Init(&c);
+       SHA1_Update(&c, hdr, hdrlen);
        SHA1_Update(&c, buf, len);
        SHA1_Final(sha1, &c);
 
@@ -265,14 +270,22 @@ int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1)
        /* Set it up */
        memset(&stream, 0, sizeof(stream));
        deflateInit(&stream, Z_BEST_COMPRESSION);
-       size = deflateBound(&stream, len);
+       size = deflateBound(&stream, len+hdrlen);
        compressed = malloc(size);
 
        /* Compress it */
-       stream.next_in = buf;
-       stream.avail_in = len;
        stream.next_out = compressed;
        stream.avail_out = size;
+
+       /* First header.. */
+       stream.next_in = hdr;
+       stream.avail_in = hdrlen;
+       while (deflate(&stream, 0) == Z_OK)
+               /* nothing */
+
+       /* Then the data itself.. */
+       stream.next_in = buf;
+       stream.avail_in = len;
        while (deflate(&stream, Z_FINISH) == Z_OK)
                /* nothing */;
        deflateEnd(&stream);
index 827809dbddbff6dd8cf842641f6db5ad2f3ae07a..bb7ceedb8e2d670f02880e0626b350a5a636f472 100644 (file)
@@ -17,29 +17,17 @@ static int check_valid_sha1(unsigned char *sha1)
        return ret;
 }
 
-static int prepend_integer(char *buffer, unsigned val, int i)
-{
-       buffer[--i] = '\0';
-       do {
-               buffer[--i] = '0' + (val % 10);
-               val /= 10;
-       } while (val);
-       return i;
-}
-
-#define ORIG_OFFSET (40)       /* Enough space to add the header of "tree <size>\0" */
-
 static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1)
 {
        unsigned char subdir_sha1[20];
        unsigned long size, offset;
        char *buffer;
-       int i, nr;
+       int nr;
 
        /* Guess at some random initial size */
        size = 8192;
        buffer = malloc(size);
-       offset = ORIG_OFFSET;
+       offset = 0;
 
        nr = 0;
        do {
@@ -89,11 +77,7 @@ static int write_tree(struct cache_entry **cachep, int maxentries, const char *b
                nr++;
        } while (nr < maxentries);
 
-       i = prepend_integer(buffer, offset - ORIG_OFFSET, ORIG_OFFSET);
-       i -= 5;
-       memcpy(buffer+i, "tree ", 5);
-
-       write_sha1_file(buffer + i, offset - i, returnsha1);
+       write_sha1_file(buffer, offset, "tree", returnsha1);
        free(buffer);
        return nr;
 }