1 #define _XOPEN_SOURCE /* glibc2 needs this */
6 unsigned char old_sha1[20];
7 unsigned char new_sha1[20];
11 #define MAXOBJECTS (1000000)
13 static struct entry *convert[MAXOBJECTS];
14 static int nr_convert;
16 static struct entry * convert_entry(unsigned char *sha1);
18 static struct entry *insert_new(unsigned char *sha1, int pos)
20 struct entry *new = xmalloc(sizeof(struct entry));
21 memset(new, 0, sizeof(*new));
22 memcpy(new->old_sha1, sha1, 20);
23 memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
26 if (nr_convert == MAXOBJECTS)
27 die("you're kidding me - hit maximum object limit");
31 static struct entry *lookup_entry(unsigned char *sha1)
33 int low = 0, high = nr_convert;
36 int next = (low + high) / 2;
37 struct entry *n = convert[next];
38 int cmp = memcmp(sha1, n->old_sha1, 20);
47 return insert_new(sha1, low);
50 static void convert_binary_sha1(void *buffer)
52 struct entry *entry = convert_entry(buffer);
53 memcpy(buffer, entry->new_sha1, 20);
56 static void convert_ascii_sha1(void *buffer)
58 unsigned char sha1[20];
61 if (get_sha1_hex(buffer, sha1))
62 die("expected sha1, got '%s'", (char*) buffer);
63 entry = convert_entry(sha1);
64 memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
67 static unsigned int convert_mode(unsigned int mode)
71 newmode = mode & S_IFMT;
73 newmode |= (mode & 0100) ? 0755 : 0644;
77 static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
79 char *new = xmalloc(size);
80 unsigned long newlen = 0;
85 int len = 21 + strlen(buffer);
86 char *path = strchr(buffer, ' ');
89 char *slash, *origpath;
91 if (!path || sscanf(buffer, "%o", &mode) != 1)
92 die("bad tree conversion");
93 mode = convert_mode(mode);
95 if (memcmp(path, base, baselen))
99 slash = strchr(path, '/');
101 newlen += sprintf(new + newlen, "%o %s", mode, path);
102 new[newlen++] = '\0';
103 memcpy(new + newlen, buffer + len - 20, 20);
112 newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
114 sha1 = (unsigned char *)(new + newlen);
117 len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1);
124 write_sha1_file(new, newlen, "tree", result_sha1);
129 static void convert_tree(void *buffer, unsigned long size, unsigned char *result_sha1)
131 void *orig_buffer = buffer;
132 unsigned long orig_size = size;
135 int len = 1+strlen(buffer);
137 convert_binary_sha1(buffer + len);
141 die("corrupt tree object");
146 write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
149 static unsigned long parse_oldstyle_date(const char *buf)
154 const char *formats[] = {
162 /* We only ever did two timezones in the bad old format .. */
163 const char *timezones[] = {
164 "PDT", "PST", "CEST", NULL
166 const char **fmt = formats;
169 while (isspace(c = *buf))
171 while ((c = *buf++) != '\n')
175 memset(&tm, 0, sizeof(tm));
177 const char *next = strptime(buf, *fmt, &tm);
183 const char **p = timezones;
184 while (isspace(*buf))
187 if (!memcmp(buf, *p, strlen(*p))) {
195 } while (*buf && *fmt);
196 printf("left: %s\n", buf);
200 static int convert_date_line(char *dst, void **buf, unsigned long *sp)
202 unsigned long size = *sp;
204 char *next = strchr(line, '\n');
205 char *date = strchr(line, '>');
209 die("missing or bad author/committer line %s", line);
213 *sp = size - (next - line);
216 memcpy(dst, line, len);
219 /* Is it already in new format? */
220 if (isdigit(*date)) {
221 int datelen = next - date;
222 memcpy(dst, date, datelen);
223 return len + datelen;
227 * Hacky hacky: one of the sparse old-style commits does not have
228 * any date at all, but we can fake it by using the committer date.
230 if (*date == '\n' && strchr(next, '>'))
231 date = strchr(next, '>')+2;
233 return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
236 static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
238 char *new = xmalloc(size + 100);
239 unsigned long newlen = 0;
242 memcpy(new + newlen, buffer, 46);
248 while (!memcmp(buffer, "parent ", 7)) {
249 memcpy(new + newlen, buffer, 48);
255 // "author xyz <xyz> date"
256 newlen += convert_date_line(new + newlen, &buffer, &size);
257 // "committer xyz <xyz> date"
258 newlen += convert_date_line(new + newlen, &buffer, &size);
261 memcpy(new + newlen, buffer, size);
264 write_sha1_file(new, newlen, "commit", result_sha1);
268 static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1)
270 void *orig_buffer = buffer;
271 unsigned long orig_size = size;
273 if (memcmp(buffer, "tree ", 5))
274 die("Bad commit '%s'", (char*) buffer);
275 convert_ascii_sha1(buffer+5);
276 buffer += 46; /* "tree " + "hex sha1" + "\n" */
277 while (!memcmp(buffer, "parent ", 7)) {
278 convert_ascii_sha1(buffer+7);
281 convert_date(orig_buffer, orig_size, result_sha1);
284 static struct entry * convert_entry(unsigned char *sha1)
286 struct entry *entry = lookup_entry(sha1);
291 if (entry->converted)
293 data = read_sha1_file(sha1, type, &size);
295 die("unable to read object %s", sha1_to_hex(sha1));
297 buffer = xmalloc(size);
298 memcpy(buffer, data, size);
300 if (!strcmp(type, "blob")) {
301 write_sha1_file(buffer, size, "blob", entry->new_sha1);
302 } else if (!strcmp(type, "tree"))
303 convert_tree(buffer, size, entry->new_sha1);
304 else if (!strcmp(type, "commit"))
305 convert_commit(buffer, size, entry->new_sha1);
307 die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
308 entry->converted = 1;
314 int main(int argc, char **argv)
316 unsigned char sha1[20];
319 if (argc != 2 || get_sha1(argv[1], sha1))
320 usage("git-convert-objects <sha1>");
322 entry = convert_entry(sha1);
323 printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));