X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=convert-cache.c;h=8916a3692f89df0d96e8bbdf885f6b9eae6a1212;hb=1e8be59d14f36128e5eb12cc0457e676bb79690c;hp=431b11798816a88c78daa846fdebf41ed57f1378;hpb=3f053897b3445988309d0ae7378944783c34d152;p=git.git diff --git a/convert-cache.c b/convert-cache.c index 431b1179..8916a369 100644 --- a/convert-cache.c +++ b/convert-cache.c @@ -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,30 +60,26 @@ static void convert_ascii_sha1(void *buffer) struct entry *entry; if (get_sha1_hex(buffer, sha1)) - die("bad sha1"); + die("expected sha1, got '%s'", buffer); entry = convert_entry(sha1); memcpy(buffer, sha1_to_hex(entry->new_sha1), 40); } -#define ORIG_OFFSET (40) - -static int prepend_integer(char *buffer, unsigned val, int i) +static unsigned int convert_mode(unsigned int mode) { - buffer[--i] = '\0'; - do { - buffer[--i] = '0' + (val % 10); - val /= 10; - } while (val); - return i; -} + 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 + ORIG_OFFSET); - unsigned long newlen = ORIG_OFFSET; + char *new = xmalloc(size); + unsigned long newlen = 0; unsigned long used; - int i; used = 0; while (size) { @@ -96,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; @@ -114,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; @@ -126,11 +122,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,10 +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 + ORIG_OFFSET + 100); - unsigned long newlen = ORIG_OFFSET; - int i; - + char *new = xmalloc(size + 100); + unsigned long newlen = 0; + // "tree \n" memcpy(new + newlen, buffer, 46); newlen += 46; @@ -271,11 +262,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); } @@ -284,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'", buffer); convert_ascii_sha1(buffer+5); buffer += 46; /* "tree " + "hex sha1" + "\n" */ while (!memcmp(buffer, "parent ", 7)) { @@ -298,7 +287,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,20 +295,20 @@ 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 = xmalloc(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; free(buffer); + free(data); return entry; } @@ -328,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 "); + if (argc != 2 || get_sha1(argv[1], sha1)) + usage("git-convert-cache "); entry = convert_entry(sha1); printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));