1 #define _XOPEN_SOURCE /* glibc2 needs this */
2 #define __EXTENSIONS__ /* solaris needs this */
8 unsigned char old_sha1[20];
9 unsigned char new_sha1[20];
13 #define MAXOBJECTS (1000000)
15 static struct entry *convert[MAXOBJECTS];
16 static int nr_convert;
18 static struct entry * convert_entry(unsigned char *sha1);
20 static struct entry *insert_new(unsigned char *sha1, int pos)
22 struct entry *new = xmalloc(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))
64 die("expected sha1, got '%s'", (char*) buffer);
65 entry = convert_entry(sha1);
66 memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
69 static unsigned int convert_mode(unsigned int mode)
73 newmode = mode & S_IFMT;
75 newmode |= (mode & 0100) ? 0755 : 0644;
79 static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
81 char *new = xmalloc(size);
82 unsigned long newlen = 0;
87 int len = 21 + strlen(buffer);
88 char *path = strchr(buffer, ' ');
91 char *slash, *origpath;
93 if (!path || sscanf(buffer, "%o", &mode) != 1)
94 die("bad tree conversion");
95 mode = convert_mode(mode);
97 if (memcmp(path, base, baselen))
101 slash = strchr(path, '/');
103 newlen += sprintf(new + newlen, "%o %s", mode, path);
104 new[newlen++] = '\0';
105 memcpy(new + newlen, buffer + len - 20, 20);
114 newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
116 sha1 = (unsigned char *)(new + newlen);
119 len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1);
126 write_sha1_file(new, newlen, "tree", result_sha1);
131 static void convert_tree(void *buffer, unsigned long size, unsigned char *result_sha1)
133 void *orig_buffer = buffer;
134 unsigned long orig_size = size;
137 int len = 1+strlen(buffer);
139 convert_binary_sha1(buffer + len);
143 die("corrupt tree object");
148 write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
151 static unsigned long parse_oldstyle_date(const char *buf)
156 const char *formats[] = {
164 /* We only ever did two timezones in the bad old format .. */
165 const char *timezones[] = {
166 "PDT", "PST", "CEST", NULL
168 const char **fmt = formats;
171 while (isspace(c = *buf))
173 while ((c = *buf++) != '\n')
177 memset(&tm, 0, sizeof(tm));
179 const char *next = strptime(buf, *fmt, &tm);
185 const char **p = timezones;
186 while (isspace(*buf))
189 if (!memcmp(buf, *p, strlen(*p))) {
197 } while (*buf && *fmt);
198 printf("left: %s\n", buf);
202 static int convert_date_line(char *dst, void **buf, unsigned long *sp)
204 unsigned long size = *sp;
206 char *next = strchr(line, '\n');
207 char *date = strchr(line, '>');
211 die("missing or bad author/committer line %s", line);
215 *sp = size - (next - line);
218 memcpy(dst, line, len);
221 /* Is it already in new format? */
222 if (isdigit(*date)) {
223 int datelen = next - date;
224 memcpy(dst, date, datelen);
225 return len + datelen;
229 * Hacky hacky: one of the sparse old-style commits does not have
230 * any date at all, but we can fake it by using the committer date.
232 if (*date == '\n' && strchr(next, '>'))
233 date = strchr(next, '>')+2;
235 return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
238 static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
240 char *new = xmalloc(size + 100);
241 unsigned long newlen = 0;
244 memcpy(new + newlen, buffer, 46);
250 while (!memcmp(buffer, "parent ", 7)) {
251 memcpy(new + newlen, buffer, 48);
257 // "author xyz <xyz> date"
258 newlen += convert_date_line(new + newlen, &buffer, &size);
259 // "committer xyz <xyz> date"
260 newlen += convert_date_line(new + newlen, &buffer, &size);
263 memcpy(new + newlen, buffer, size);
266 write_sha1_file(new, newlen, "commit", result_sha1);
270 static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1)
272 void *orig_buffer = buffer;
273 unsigned long orig_size = size;
275 if (memcmp(buffer, "tree ", 5))
276 die("Bad commit '%s'", (char*) buffer);
277 convert_ascii_sha1(buffer+5);
278 buffer += 46; /* "tree " + "hex sha1" + "\n" */
279 while (!memcmp(buffer, "parent ", 7)) {
280 convert_ascii_sha1(buffer+7);
283 convert_date(orig_buffer, orig_size, result_sha1);
286 static struct entry * convert_entry(unsigned char *sha1)
288 struct entry *entry = lookup_entry(sha1);
293 if (entry->converted)
295 data = read_sha1_file(sha1, type, &size);
297 die("unable to read object %s", sha1_to_hex(sha1));
299 buffer = xmalloc(size);
300 memcpy(buffer, data, size);
302 if (!strcmp(type, "blob")) {
303 write_sha1_file(buffer, size, "blob", entry->new_sha1);
304 } else if (!strcmp(type, "tree"))
305 convert_tree(buffer, size, entry->new_sha1);
306 else if (!strcmp(type, "commit"))
307 convert_commit(buffer, size, entry->new_sha1);
309 die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
310 entry->converted = 1;
316 int main(int argc, char **argv)
318 unsigned char sha1[20];
321 if (argc != 2 || get_sha1(argv[1], sha1))
322 usage("git-convert-objects <sha1>");
324 entry = convert_entry(sha1);
325 printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));