1 #define _XOPEN_SOURCE /* glibc2 needs this */
7 unsigned char old_sha1
[20];
8 unsigned char new_sha1
[20];
12 #define MAXOBJECTS (1000000)
14 static struct entry
*convert
[MAXOBJECTS
];
15 static int nr_convert
;
17 static struct entry
* convert_entry(unsigned char *sha1
);
19 static struct entry
*insert_new(unsigned char *sha1
, int pos
)
21 struct entry
*new = malloc(sizeof(struct entry
));
23 memset(new, 0, sizeof(*new));
24 memcpy(new->old_sha1
, sha1
, 20);
25 memmove(convert
+ pos
+ 1, convert
+ pos
, (nr_convert
- pos
) * sizeof(struct entry
*));
28 if (nr_convert
== MAXOBJECTS
)
29 die("you're kidding me - hit maximum object limit");
33 static struct entry
*lookup_entry(unsigned char *sha1
)
35 int low
= 0, high
= nr_convert
;
38 int next
= (low
+ high
) / 2;
39 struct entry
*n
= convert
[next
];
40 int cmp
= memcmp(sha1
, n
->old_sha1
, 20);
49 return insert_new(sha1
, low
);
52 static void convert_binary_sha1(void *buffer
)
54 struct entry
*entry
= convert_entry(buffer
);
55 memcpy(buffer
, entry
->new_sha1
, 20);
58 static void convert_ascii_sha1(void *buffer
)
60 unsigned char sha1
[20];
63 if (get_sha1_hex(buffer
, sha1
))
65 entry
= convert_entry(sha1
);
66 memcpy(buffer
, sha1_to_hex(entry
->new_sha1
), 40);
69 static int write_subdirectory(void *buffer
, unsigned long size
, const char *base
, int baselen
, unsigned char *result_sha1
)
71 char *new = malloc(size
);
72 unsigned long newlen
= 0;
77 int len
= 21 + strlen(buffer
);
78 char *path
= strchr(buffer
, ' ');
81 char *slash
, *origpath
;
83 if (!path
|| sscanf(buffer
, "%o", &mode
) != 1)
84 die("bad tree conversion");
86 if (memcmp(path
, base
, baselen
))
90 slash
= strchr(path
, '/');
92 newlen
+= sprintf(new + newlen
, "%o %s", mode
, path
);
94 memcpy(new + newlen
, buffer
+ len
- 20, 20);
103 newlen
+= sprintf(new + newlen
, "%o %.*s", S_IFDIR
, slash
- path
, path
);
105 sha1
= (unsigned char *)(new + newlen
);
108 len
= write_subdirectory(buffer
, size
, origpath
, slash
-origpath
+1, sha1
);
115 write_sha1_file(new, newlen
, "tree", result_sha1
);
120 static void convert_tree(void *buffer
, unsigned long size
, unsigned char *result_sha1
)
122 void *orig_buffer
= buffer
;
123 unsigned long orig_size
= size
;
126 int len
= 1+strlen(buffer
);
128 convert_binary_sha1(buffer
+ len
);
132 die("corrupt tree object");
137 write_subdirectory(orig_buffer
, orig_size
, "", 0, result_sha1
);
140 static unsigned long parse_oldstyle_date(const char *buf
)
145 const char *formats
[] = {
153 /* We only ever did two timezones in the bad old format .. */
154 const char *timezones
[] = {
155 "PDT", "PST", "CEST", NULL
157 const char **fmt
= formats
;
160 while (isspace(c
= *buf
))
162 while ((c
= *buf
++) != '\n')
166 memset(&tm
, 0, sizeof(tm
));
168 const char *next
= strptime(buf
, *fmt
, &tm
);
174 const char **p
= timezones
;
175 while (isspace(*buf
))
178 if (!memcmp(buf
, *p
, strlen(*p
))) {
186 } while (*buf
&& *fmt
);
187 printf("left: %s\n", buf
);
191 static int convert_date_line(char *dst
, void **buf
, unsigned long *sp
)
193 unsigned long size
= *sp
;
195 char *next
= strchr(line
, '\n');
196 char *date
= strchr(line
, '>');
200 die("missing or bad author/committer line %s", line
);
204 *sp
= size
- (next
- line
);
207 memcpy(dst
, line
, len
);
210 /* Is it already in new format? */
211 if (isdigit(*date
)) {
212 int datelen
= next
- date
;
213 memcpy(dst
, date
, datelen
);
214 return len
+ datelen
;
218 * Hacky hacky: one of the sparse old-style commits does not have
219 * any date at all, but we can fake it by using the committer date.
221 if (*date
== '\n' && strchr(next
, '>'))
222 date
= strchr(next
, '>')+2;
224 return len
+ sprintf(dst
, "%lu -0700\n", parse_oldstyle_date(date
));
227 static void convert_date(void *buffer
, unsigned long size
, unsigned char *result_sha1
)
229 char *new = malloc(size
+ 100);
230 unsigned long newlen
= 0;
233 memcpy(new + newlen
, buffer
, 46);
239 while (!memcmp(buffer
, "parent ", 7)) {
240 memcpy(new + newlen
, buffer
, 48);
246 // "author xyz <xyz> date"
247 newlen
+= convert_date_line(new + newlen
, &buffer
, &size
);
248 // "committer xyz <xyz> date"
249 newlen
+= convert_date_line(new + newlen
, &buffer
, &size
);
252 memcpy(new + newlen
, buffer
, size
);
255 write_sha1_file(new, newlen
, "commit", result_sha1
);
259 static void convert_commit(void *buffer
, unsigned long size
, unsigned char *result_sha1
)
261 void *orig_buffer
= buffer
;
262 unsigned long orig_size
= size
;
264 convert_ascii_sha1(buffer
+5);
265 buffer
+= 46; /* "tree " + "hex sha1" + "\n" */
266 while (!memcmp(buffer
, "parent ", 7)) {
267 convert_ascii_sha1(buffer
+7);
270 convert_date(orig_buffer
, orig_size
, result_sha1
);
273 static struct entry
* convert_entry(unsigned char *sha1
)
275 struct entry
*entry
= lookup_entry(sha1
);
280 if (entry
->converted
)
282 data
= read_sha1_file(sha1
, type
, &size
);
284 die("unable to read object %s", sha1_to_hex(sha1
));
286 buffer
= malloc(size
);
287 memcpy(buffer
, data
, size
);
289 if (!strcmp(type
, "blob")) {
290 write_sha1_file(buffer
, size
, "blob", entry
->new_sha1
);
291 } else if (!strcmp(type
, "tree"))
292 convert_tree(buffer
, size
, entry
->new_sha1
);
293 else if (!strcmp(type
, "commit"))
294 convert_commit(buffer
, size
, entry
->new_sha1
);
296 die("unknown object type '%s' in %s", type
, sha1_to_hex(sha1
));
297 entry
->converted
= 1;
302 int main(int argc
, char **argv
)
304 unsigned char sha1
[20];
307 if (argc
!= 2 || get_sha1_hex(argv
[1], sha1
))
308 usage("convert-cache <sha1>");
310 entry
= convert_entry(sha1
);
311 printf("new sha1: %s\n", sha1_to_hex(entry
->new_sha1
));