1 #define _XOPEN_SOURCE 500 /* glibc2 and AIX 5.3L need this */
2 #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
10 unsigned char old_sha1[20];
11 unsigned char new_sha1[20];
15 #define MAXOBJECTS (1000000)
17 static struct entry *convert[MAXOBJECTS];
18 static int nr_convert;
20 static struct entry * convert_entry(unsigned char *sha1);
22 static struct entry *insert_new(unsigned char *sha1, int pos)
24 struct entry *new = xcalloc(1, sizeof(struct entry));
25 memcpy(new->old_sha1, sha1, 20);
26 memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
29 if (nr_convert == MAXOBJECTS)
30 die("you're kidding me - hit maximum object limit");
34 static struct entry *lookup_entry(unsigned char *sha1)
36 int low = 0, high = nr_convert;
39 int next = (low + high) / 2;
40 struct entry *n = convert[next];
41 int cmp = memcmp(sha1, n->old_sha1, 20);
50 return insert_new(sha1, low);
53 static void convert_binary_sha1(void *buffer)
55 struct entry *entry = convert_entry(buffer);
56 memcpy(buffer, entry->new_sha1, 20);
59 static void convert_ascii_sha1(void *buffer)
61 unsigned char sha1[20];
64 if (get_sha1_hex(buffer, sha1))
65 die("expected sha1, got '%s'", (char*) buffer);
66 entry = convert_entry(sha1);
67 memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
70 static unsigned int convert_mode(unsigned int mode)
74 newmode = mode & S_IFMT;
76 newmode |= (mode & 0100) ? 0755 : 0644;
80 static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
82 char *new = xmalloc(size);
83 unsigned long newlen = 0;
88 int len = 21 + strlen(buffer);
89 char *path = strchr(buffer, ' ');
92 char *slash, *origpath;
94 if (!path || sscanf(buffer, "%o", &mode) != 1)
95 die("bad tree conversion");
96 mode = convert_mode(mode);
98 if (memcmp(path, base, baselen))
102 slash = strchr(path, '/');
104 newlen += sprintf(new + newlen, "%o %s", mode, path);
105 new[newlen++] = '\0';
106 memcpy(new + newlen, buffer + len - 20, 20);
115 newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
117 sha1 = (unsigned char *)(new + newlen);
120 len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1);
127 write_sha1_file(new, newlen, tree_type, result_sha1);
132 static void convert_tree(void *buffer, unsigned long size, unsigned char *result_sha1)
134 void *orig_buffer = buffer;
135 unsigned long orig_size = size;
138 int len = 1+strlen(buffer);
140 convert_binary_sha1(buffer + len);
144 die("corrupt tree object");
149 write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
152 static unsigned long parse_oldstyle_date(const char *buf)
157 const char *formats[] = {
165 /* We only ever did two timezones in the bad old format .. */
166 const char *timezones[] = {
167 "PDT", "PST", "CEST", NULL
169 const char **fmt = formats;
172 while (isspace(c = *buf))
174 while ((c = *buf++) != '\n')
178 memset(&tm, 0, sizeof(tm));
180 const char *next = strptime(buf, *fmt, &tm);
186 const char **p = timezones;
187 while (isspace(*buf))
190 if (!memcmp(buf, *p, strlen(*p))) {
198 } while (*buf && *fmt);
199 printf("left: %s\n", buf);
203 static int convert_date_line(char *dst, void **buf, unsigned long *sp)
205 unsigned long size = *sp;
207 char *next = strchr(line, '\n');
208 char *date = strchr(line, '>');
212 die("missing or bad author/committer line %s", line);
216 *sp = size - (next - line);
219 memcpy(dst, line, len);
222 /* Is it already in new format? */
223 if (isdigit(*date)) {
224 int datelen = next - date;
225 memcpy(dst, date, datelen);
226 return len + datelen;
230 * Hacky hacky: one of the sparse old-style commits does not have
231 * any date at all, but we can fake it by using the committer date.
233 if (*date == '\n' && strchr(next, '>'))
234 date = strchr(next, '>')+2;
236 return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
239 static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
241 char *new = xmalloc(size + 100);
242 unsigned long newlen = 0;
245 memcpy(new + newlen, buffer, 46);
251 while (!memcmp(buffer, "parent ", 7)) {
252 memcpy(new + newlen, buffer, 48);
258 // "author xyz <xyz> date"
259 newlen += convert_date_line(new + newlen, &buffer, &size);
260 // "committer xyz <xyz> date"
261 newlen += convert_date_line(new + newlen, &buffer, &size);
264 memcpy(new + newlen, buffer, size);
267 write_sha1_file(new, newlen, commit_type, result_sha1);
271 static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1)
273 void *orig_buffer = buffer;
274 unsigned long orig_size = size;
276 if (memcmp(buffer, "tree ", 5))
277 die("Bad commit '%s'", (char*) buffer);
278 convert_ascii_sha1(buffer+5);
279 buffer += 46; /* "tree " + "hex sha1" + "\n" */
280 while (!memcmp(buffer, "parent ", 7)) {
281 convert_ascii_sha1(buffer+7);
284 convert_date(orig_buffer, orig_size, result_sha1);
287 static struct entry * convert_entry(unsigned char *sha1)
289 struct entry *entry = lookup_entry(sha1);
294 if (entry->converted)
296 data = read_sha1_file(sha1, type, &size);
298 die("unable to read object %s", sha1_to_hex(sha1));
300 buffer = xmalloc(size);
301 memcpy(buffer, data, size);
303 if (!strcmp(type, blob_type)) {
304 write_sha1_file(buffer, size, blob_type, entry->new_sha1);
305 } else if (!strcmp(type, tree_type))
306 convert_tree(buffer, size, entry->new_sha1);
307 else if (!strcmp(type, commit_type))
308 convert_commit(buffer, size, entry->new_sha1);
310 die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
311 entry->converted = 1;
317 int main(int argc, char **argv)
319 unsigned char sha1[20];
322 setup_git_directory();
325 usage("git-convert-objects <sha1>");
326 if (get_sha1(argv[1], sha1))
327 die("Not a valid object name %s", argv[1]);
329 entry = convert_entry(sha1);
330 printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));