[PATCH] Updates to glossary
[git.git] / convert-cache.c
index 7102e45..95f0302 100644 (file)
@@ -18,8 +18,7 @@ static struct entry * convert_entry(unsigned char *sha1);
 
 static struct entry *insert_new(unsigned char *sha1, int pos)
 {
-       struct entry *new = malloc(sizeof(struct entry));
-
+       struct entry *new = xmalloc(sizeof(struct entry));
        memset(new, 0, sizeof(*new));
        memcpy(new->old_sha1, sha1, 20);
        memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
@@ -61,14 +60,24 @@ static void convert_ascii_sha1(void *buffer)
        struct entry *entry;
 
        if (get_sha1_hex(buffer, sha1))
-               die("bad sha1");
+               die("expected sha1, got '%s'", (char*) buffer);
        entry = convert_entry(sha1);
        memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
 }
 
+static unsigned int convert_mode(unsigned int mode)
+{
+       unsigned int newmode;
+
+       newmode = mode & S_IFMT;
+       if (S_ISREG(mode))
+               newmode |= (mode & 0100) ? 0755 : 0644;
+       return newmode;
+}
+
 static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
 {
-       char *new = malloc(size);
+       char *new = xmalloc(size);
        unsigned long newlen = 0;
        unsigned long used;
 
@@ -82,6 +91,7 @@ static int write_subdirectory(void *buffer, unsigned long size, const char *base
 
                if (!path || sscanf(buffer, "%o", &mode) != 1)
                        die("bad tree conversion");
+               mode = convert_mode(mode);
                path++;
                if (memcmp(path, base, baselen))
                        break;
@@ -100,7 +110,7 @@ static int write_subdirectory(void *buffer, unsigned long size, const char *base
                        continue;
                }
 
-               newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, slash - path, path);
+               newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
                new[newlen++] = 0;
                sha1 = (unsigned char *)(new + newlen);
                newlen += 20;
@@ -226,9 +236,9 @@ 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 + 100);
+       char *new = xmalloc(size + 100);
        unsigned long newlen = 0;
-
+       
        // "tree <sha1>\n"
        memcpy(new + newlen, buffer, 46);
        newlen += 46;
@@ -261,6 +271,8 @@ static void convert_commit(void *buffer, unsigned long size, unsigned char *resu
        void *orig_buffer = buffer;
        unsigned long orig_size = size;
 
+       if (memcmp(buffer, "tree ", 5))
+               die("Bad commit '%s'", (char*) buffer);
        convert_ascii_sha1(buffer+5);
        buffer += 46;    /* "tree " + "hex sha1" + "\n" */
        while (!memcmp(buffer, "parent ", 7)) {
@@ -283,7 +295,7 @@ static struct entry * convert_entry(unsigned char *sha1)
        if (!data)
                die("unable to read object %s", sha1_to_hex(sha1));
 
-       buffer = malloc(size);
+       buffer = xmalloc(size);
        memcpy(buffer, data, size);
        
        if (!strcmp(type, "blob")) {
@@ -296,6 +308,7 @@ static struct entry * convert_entry(unsigned char *sha1)
                die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
        entry->converted = 1;
        free(buffer);
+       free(data);
        return entry;
 }
 
@@ -304,8 +317,8 @@ int main(int argc, char **argv)
        unsigned char sha1[20];
        struct entry *entry;
 
-       if (argc != 2 || get_sha1_hex(argv[1], sha1))
-               usage("convert-cache <sha1>");
+       if (argc != 2 || get_sha1(argv[1], sha1))
+               usage("git-convert-cache <sha1>");
 
        entry = convert_entry(sha1);
        printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));