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 = xmalloc(sizeof(struct entry
));
22 memset(new, 0, sizeof(*new));
23 memcpy(new->old_sha1
, sha1
, 20);
24 memmove(convert
+ pos
+ 1, convert
+ pos
, (nr_convert
- pos
) * sizeof(struct entry
*));
27 if (nr_convert
== MAXOBJECTS
)
28 die("you're kidding me - hit maximum object limit");
32 static struct entry
*lookup_entry(unsigned char *sha1
)
34 int low
= 0, high
= nr_convert
;
37 int next
= (low
+ high
) / 2;
38 struct entry
*n
= convert
[next
];
39 int cmp
= memcmp(sha1
, n
->old_sha1
, 20);
48 return insert_new(sha1
, low
);
51 static void convert_binary_sha1(void *buffer
)
53 struct entry
*entry
= convert_entry(buffer
);
54 memcpy(buffer
, entry
->new_sha1
, 20);
57 static void convert_ascii_sha1(void *buffer
)
59 unsigned char sha1
[20];
62 if (get_sha1_hex(buffer
, sha1
))
63 die("expected sha1, got '%s'", (char*) buffer
);
64 entry
= convert_entry(sha1
);
65 memcpy(buffer
, sha1_to_hex(entry
->new_sha1
), 40);
68 static unsigned int convert_mode(unsigned int mode
)
72 newmode
= mode
& S_IFMT
;
74 newmode
|= (mode
& 0100) ? 0755 : 0644;
78 static int write_subdirectory(void *buffer
, unsigned long size
, const char *base
, int baselen
, unsigned char *result_sha1
)
80 char *new = xmalloc(size
);
81 unsigned long newlen
= 0;
86 int len
= 21 + strlen(buffer
);
87 char *path
= strchr(buffer
, ' ');
90 char *slash
, *origpath
;
92 if (!path
|| sscanf(buffer
, "%o", &mode
) != 1)
93 die("bad tree conversion");
94 mode
= convert_mode(mode
);
96 if (memcmp(path
, base
, baselen
))
100 slash
= strchr(path
, '/');
102 newlen
+= sprintf(new + newlen
, "%o %s", mode
, path
);
103 new[newlen
++] = '\0';
104 memcpy(new + newlen
, buffer
+ len
- 20, 20);
113 newlen
+= sprintf(new + newlen
, "%o %.*s", S_IFDIR
, (int)(slash
- path
), path
);
115 sha1
= (unsigned char *)(new + newlen
);
118 len
= write_subdirectory(buffer
, size
, origpath
, slash
-origpath
+1, sha1
);
125 write_sha1_file(new, newlen
, "tree", result_sha1
);
130 static void convert_tree(void *buffer
, unsigned long size
, unsigned char *result_sha1
)
132 void *orig_buffer
= buffer
;
133 unsigned long orig_size
= size
;
136 int len
= 1+strlen(buffer
);
138 convert_binary_sha1(buffer
+ len
);
142 die("corrupt tree object");
147 write_subdirectory(orig_buffer
, orig_size
, "", 0, result_sha1
);
150 static unsigned long parse_oldstyle_date(const char *buf
)
155 const char *formats
[] = {
163 /* We only ever did two timezones in the bad old format .. */
164 const char *timezones
[] = {
165 "PDT", "PST", "CEST", NULL
167 const char **fmt
= formats
;
170 while (isspace(c
= *buf
))
172 while ((c
= *buf
++) != '\n')
176 memset(&tm
, 0, sizeof(tm
));
178 const char *next
= strptime(buf
, *fmt
, &tm
);
184 const char **p
= timezones
;
185 while (isspace(*buf
))
188 if (!memcmp(buf
, *p
, strlen(*p
))) {
196 } while (*buf
&& *fmt
);
197 printf("left: %s\n", buf
);
201 static int convert_date_line(char *dst
, void **buf
, unsigned long *sp
)
203 unsigned long size
= *sp
;
205 char *next
= strchr(line
, '\n');
206 char *date
= strchr(line
, '>');
210 die("missing or bad author/committer line %s", line
);
214 *sp
= size
- (next
- line
);
217 memcpy(dst
, line
, len
);
220 /* Is it already in new format? */
221 if (isdigit(*date
)) {
222 int datelen
= next
- date
;
223 memcpy(dst
, date
, datelen
);
224 return len
+ datelen
;
228 * Hacky hacky: one of the sparse old-style commits does not have
229 * any date at all, but we can fake it by using the committer date.
231 if (*date
== '\n' && strchr(next
, '>'))
232 date
= strchr(next
, '>')+2;
234 return len
+ sprintf(dst
, "%lu -0700\n", parse_oldstyle_date(date
));
237 static void convert_date(void *buffer
, unsigned long size
, unsigned char *result_sha1
)
239 char *new = xmalloc(size
+ 100);
240 unsigned long newlen
= 0;
243 memcpy(new + newlen
, buffer
, 46);
249 while (!memcmp(buffer
, "parent ", 7)) {
250 memcpy(new + newlen
, buffer
, 48);
256 // "author xyz <xyz> date"
257 newlen
+= convert_date_line(new + newlen
, &buffer
, &size
);
258 // "committer xyz <xyz> date"
259 newlen
+= convert_date_line(new + newlen
, &buffer
, &size
);
262 memcpy(new + newlen
, buffer
, size
);
265 write_sha1_file(new, newlen
, "commit", result_sha1
);
269 static void convert_commit(void *buffer
, unsigned long size
, unsigned char *result_sha1
)
271 void *orig_buffer
= buffer
;
272 unsigned long orig_size
= size
;
274 if (memcmp(buffer
, "tree ", 5))
275 die("Bad commit '%s'", (char*) buffer
);
276 convert_ascii_sha1(buffer
+5);
277 buffer
+= 46; /* "tree " + "hex sha1" + "\n" */
278 while (!memcmp(buffer
, "parent ", 7)) {
279 convert_ascii_sha1(buffer
+7);
282 convert_date(orig_buffer
, orig_size
, result_sha1
);
285 static struct entry
* convert_entry(unsigned char *sha1
)
287 struct entry
*entry
= lookup_entry(sha1
);
292 if (entry
->converted
)
294 data
= read_sha1_file(sha1
, type
, &size
);
296 die("unable to read object %s", sha1_to_hex(sha1
));
298 buffer
= xmalloc(size
);
299 memcpy(buffer
, data
, size
);
301 if (!strcmp(type
, "blob")) {
302 write_sha1_file(buffer
, size
, "blob", entry
->new_sha1
);
303 } else if (!strcmp(type
, "tree"))
304 convert_tree(buffer
, size
, entry
->new_sha1
);
305 else if (!strcmp(type
, "commit"))
306 convert_commit(buffer
, size
, entry
->new_sha1
);
308 die("unknown object type '%s' in %s", type
, sha1_to_hex(sha1
));
309 entry
->converted
= 1;
315 int main(int argc
, char **argv
)
317 unsigned char sha1
[20];
320 if (argc
!= 2 || get_sha1(argv
[1], sha1
))
321 usage("git-convert-objects <sha1>");
323 entry
= convert_entry(sha1
);
324 printf("new sha1: %s\n", sha1_to_hex(entry
->new_sha1
));