--- /dev/null
+#include "cache.h"
+
+struct entry {
+ unsigned char old_sha1[20];
+ unsigned char new_sha1[20];
+ int converted;
+};
+
+#define MAXOBJECTS (1000000)
+
+static struct entry *convert[MAXOBJECTS];
+static int nr_convert;
+
+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));
+
+ memset(new, 0, sizeof(*new));
+ memcpy(new->old_sha1, sha1, 20);
+ memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
+ convert[pos] = new;
+ nr_convert++;
+ if (nr_convert == MAXOBJECTS)
+ die("you're kidding me - hit maximum object limit");
+ return new;
+}
+
+static struct entry *lookup_entry(unsigned char *sha1)
+{
+ int low = 0, high = nr_convert;
+
+ while (low < high) {
+ int next = (low + high) / 2;
+ struct entry *n = convert[next];
+ int cmp = memcmp(sha1, n->old_sha1, 20);
+ if (!cmp)
+ return n;
+ if (cmp < 0) {
+ high = next;
+ continue;
+ }
+ low = next+1;
+ }
+ return insert_new(sha1, low);
+}
+
+static void convert_blob(void *buffer, unsigned long size)
+{
+ /* Nothing to do */
+}
+
+static void convert_binary_sha1(void *buffer)
+{
+ struct entry *entry = convert_entry(buffer);
+ memcpy(buffer, entry->new_sha1, 20);
+}
+
+static void convert_ascii_sha1(void *buffer)
+{
+ unsigned char sha1[20];
+ struct entry *entry;
+
+ if (get_sha1_hex(buffer, sha1))
+ die("bad sha1");
+ entry = convert_entry(sha1);
+ memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
+}
+
+static void convert_tree(void *buffer, unsigned long size)
+{
+ while (size) {
+ int len = 1+strlen(buffer);
+
+ convert_binary_sha1(buffer + len);
+
+ len += 20;
+ if (len > size)
+ die("corrupt tree object");
+ size -= len;
+ buffer += len;
+ }
+}
+
+static void convert_commit(void *buffer, unsigned long size)
+{
+ convert_ascii_sha1(buffer+5);
+ buffer += 46; /* "tree " + "hex sha1" + "\n" */
+ while (!memcmp(buffer, "parent ", 7)) {
+ convert_ascii_sha1(buffer+7);
+ buffer += 48;
+ }
+}
+
+static struct entry * convert_entry(unsigned char *sha1)
+{
+ struct entry *entry = lookup_entry(sha1);
+ char type[20];
+ void *buffer, *data;
+ unsigned long size, offset;
+
+ if (entry->converted)
+ return entry;
+ data = read_sha1_file(sha1, type, &size);
+ 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);
+
+ if (!strcmp(type, "blob"))
+ convert_blob(buffer + offset, size);
+ else if (!strcmp(type, "tree"))
+ convert_tree(buffer + offset, size);
+ else if (!strcmp(type, "commit"))
+ convert_commit(buffer + offset, size);
+ else
+ die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
+ write_sha1_file(buffer, size + offset, entry->new_sha1);
+ entry->converted = 1;
+ free(buffer);
+ return entry;
+}
+
+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>");
+
+ entry = convert_entry(sha1);
+ printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));
+ return 0;
+}
if (map) {
char type[100];
unsigned long size;
- void *buffer = NULL;
- if (!check_sha1_signature(sha1, map, mapsize))
- buffer = unpack_sha1_file(map, mapsize, type,
- &size);
+ void *buffer = unpack_sha1_file(map, mapsize, type, &size);
+ if (!buffer)
+ return -1;
+ if (check_sha1_signature(sha1, buffer, size, type) < 0)
+ printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
munmap(map, mapsize);
- if (buffer && !fsck_entry(sha1, type, buffer, size))
+ if (!fsck_entry(sha1, type, buffer, size))
return 0;
}
}
return base;
}
-int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size)
+int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
{
+ char header[100];
unsigned char real_sha1[20];
SHA_CTX c;
SHA1_Init(&c);
+ SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
SHA1_Update(&c, map, size);
SHA1_Final(real_sha1, &c);
return memcmp(sha1, real_sha1, 20) ? -1 : 0;
unsigned char sha1[20];
SHA_CTX c;
+ /* Sha1.. */
+ SHA1_Init(&c);
+ SHA1_Update(&c, buf, len);
+ SHA1_Final(sha1, &c);
+
/* Set it up */
memset(&stream, 0, sizeof(stream));
deflateInit(&stream, Z_BEST_COMPRESSION);
deflateEnd(&stream);
size = stream.total_out;
- /* Sha1.. */
- SHA1_Init(&c);
- SHA1_Update(&c, compressed, size);
- SHA1_Final(sha1, &c);
-
if (write_sha1_buffer(sha1, compressed, size) < 0)
return -1;
if (returnsha1)