4 unsigned char old_sha1
[20];
5 unsigned char new_sha1
[20];
9 #define MAXOBJECTS (1000000)
11 static struct entry
*convert
[MAXOBJECTS
];
12 static int nr_convert
;
14 static struct entry
* convert_entry(unsigned char *sha1
);
16 static struct entry
*insert_new(unsigned char *sha1
, int pos
)
18 struct entry
*new = malloc(sizeof(struct entry
));
20 memset(new, 0, sizeof(*new));
21 memcpy(new->old_sha1
, sha1
, 20);
22 memmove(convert
+ pos
+ 1, convert
+ pos
, (nr_convert
- pos
) * sizeof(struct entry
*));
25 if (nr_convert
== MAXOBJECTS
)
26 die("you're kidding me - hit maximum object limit");
30 static struct entry
*lookup_entry(unsigned char *sha1
)
32 int low
= 0, high
= nr_convert
;
35 int next
= (low
+ high
) / 2;
36 struct entry
*n
= convert
[next
];
37 int cmp
= memcmp(sha1
, n
->old_sha1
, 20);
46 return insert_new(sha1
, low
);
49 static void convert_blob(void *buffer
, unsigned long size
)
54 static void convert_binary_sha1(void *buffer
)
56 struct entry
*entry
= convert_entry(buffer
);
57 memcpy(buffer
, entry
->new_sha1
, 20);
60 static void convert_ascii_sha1(void *buffer
)
62 unsigned char sha1
[20];
65 if (get_sha1_hex(buffer
, sha1
))
67 entry
= convert_entry(sha1
);
68 memcpy(buffer
, sha1_to_hex(entry
->new_sha1
), 40);
71 static void convert_tree(void *buffer
, unsigned long size
)
74 int len
= 1+strlen(buffer
);
76 convert_binary_sha1(buffer
+ len
);
80 die("corrupt tree object");
86 static void convert_commit(void *buffer
, unsigned long size
)
88 convert_ascii_sha1(buffer
+5);
89 buffer
+= 46; /* "tree " + "hex sha1" + "\n" */
90 while (!memcmp(buffer
, "parent ", 7)) {
91 convert_ascii_sha1(buffer
+7);
96 static struct entry
* convert_entry(unsigned char *sha1
)
98 struct entry
*entry
= lookup_entry(sha1
);
101 unsigned long size
, offset
;
103 if (entry
->converted
)
105 data
= read_sha1_file(sha1
, type
, &size
);
107 die("unable to read object %s", sha1_to_hex(sha1
));
109 buffer
= malloc(size
+ 100);
110 offset
= sprintf(buffer
, "%s %lu", type
, size
)+1;
111 memcpy(buffer
+ offset
, data
, size
);
113 if (!strcmp(type
, "blob"))
114 convert_blob(buffer
+ offset
, size
);
115 else if (!strcmp(type
, "tree"))
116 convert_tree(buffer
+ offset
, size
);
117 else if (!strcmp(type
, "commit"))
118 convert_commit(buffer
+ offset
, size
);
120 die("unknown object type '%s' in %s", type
, sha1_to_hex(sha1
));
121 write_sha1_file(buffer
, size
+ offset
, entry
->new_sha1
);
122 entry
->converted
= 1;
127 int main(int argc
, char **argv
)
129 unsigned char sha1
[20];
132 if (argc
!= 2 || get_sha1_hex(argv
[1], sha1
))
133 usage("convert-cache <sha1>");
135 entry
= convert_entry(sha1
);
136 printf("new sha1: %s\n", sha1_to_hex(entry
->new_sha1
));