57de37faa53df5fb84247e2fd7486a108039676e
[git.git] / convert-objects.c
1 #define _XOPEN_SOURCE 500 /* glibc2 and AIX 5.3L need this */
2 #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
3 #include <time.h>
4 #include "cache.h"
5 #include "blob.h"
6 #include "commit.h"
7 #include "tree.h"
8
9 struct entry {
10         unsigned char old_sha1[20];
11         unsigned char new_sha1[20];
12         int converted;
13 };
14
15 #define MAXOBJECTS (1000000)
16
17 static struct entry *convert[MAXOBJECTS];
18 static int nr_convert;
19
20 static struct entry * convert_entry(unsigned char *sha1);
21
22 static struct entry *insert_new(unsigned char *sha1, int pos)
23 {
24         struct entry *new = xmalloc(sizeof(struct entry));
25         memset(new, 0, sizeof(*new));
26         memcpy(new->old_sha1, sha1, 20);
27         memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
28         convert[pos] = new;
29         nr_convert++;
30         if (nr_convert == MAXOBJECTS)
31                 die("you're kidding me - hit maximum object limit");
32         return new;
33 }
34
35 static struct entry *lookup_entry(unsigned char *sha1)
36 {
37         int low = 0, high = nr_convert;
38
39         while (low < high) {
40                 int next = (low + high) / 2;
41                 struct entry *n = convert[next];
42                 int cmp = memcmp(sha1, n->old_sha1, 20);
43                 if (!cmp)
44                         return n;
45                 if (cmp < 0) {
46                         high = next;
47                         continue;
48                 }
49                 low = next+1;
50         }
51         return insert_new(sha1, low);
52 }
53
54 static void convert_binary_sha1(void *buffer)
55 {
56         struct entry *entry = convert_entry(buffer);
57         memcpy(buffer, entry->new_sha1, 20);
58 }
59
60 static void convert_ascii_sha1(void *buffer)
61 {
62         unsigned char sha1[20];
63         struct entry *entry;
64
65         if (get_sha1_hex(buffer, sha1))
66                 die("expected sha1, got '%s'", (char*) buffer);
67         entry = convert_entry(sha1);
68         memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
69 }
70
71 static unsigned int convert_mode(unsigned int mode)
72 {
73         unsigned int newmode;
74
75         newmode = mode & S_IFMT;
76         if (S_ISREG(mode))
77                 newmode |= (mode & 0100) ? 0755 : 0644;
78         return newmode;
79 }
80
81 static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
82 {
83         char *new = xmalloc(size);
84         unsigned long newlen = 0;
85         unsigned long used;
86
87         used = 0;
88         while (size) {
89                 int len = 21 + strlen(buffer);
90                 char *path = strchr(buffer, ' ');
91                 unsigned char *sha1;
92                 unsigned int mode;
93                 char *slash, *origpath;
94
95                 if (!path || sscanf(buffer, "%o", &mode) != 1)
96                         die("bad tree conversion");
97                 mode = convert_mode(mode);
98                 path++;
99                 if (memcmp(path, base, baselen))
100                         break;
101                 origpath = path;
102                 path += baselen;
103                 slash = strchr(path, '/');
104                 if (!slash) {
105                         newlen += sprintf(new + newlen, "%o %s", mode, path);
106                         new[newlen++] = '\0';
107                         memcpy(new + newlen, buffer + len - 20, 20);
108                         newlen += 20;
109
110                         used += len;
111                         size -= len;
112                         buffer += len;
113                         continue;
114                 }
115
116                 newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
117                 new[newlen++] = 0;
118                 sha1 = (unsigned char *)(new + newlen);
119                 newlen += 20;
120
121                 len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1);
122
123                 used += len;
124                 size -= len;
125                 buffer += len;
126         }
127
128         write_sha1_file(new, newlen, tree_type, result_sha1);
129         free(new);
130         return used;
131 }
132
133 static void convert_tree(void *buffer, unsigned long size, unsigned char *result_sha1)
134 {
135         void *orig_buffer = buffer;
136         unsigned long orig_size = size;
137
138         while (size) {
139                 int len = 1+strlen(buffer);
140
141                 convert_binary_sha1(buffer + len);
142
143                 len += 20;
144                 if (len > size)
145                         die("corrupt tree object");
146                 size -= len;
147                 buffer += len;
148         }
149
150         write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
151 }
152
153 static unsigned long parse_oldstyle_date(const char *buf)
154 {
155         char c, *p;
156         char buffer[100];
157         struct tm tm;
158         const char *formats[] = {
159                 "%c",
160                 "%a %b %d %T",
161                 "%Z",
162                 "%Y",
163                 " %Y",
164                 NULL
165         };
166         /* We only ever did two timezones in the bad old format .. */
167         const char *timezones[] = {
168                 "PDT", "PST", "CEST", NULL
169         };
170         const char **fmt = formats;
171
172         p = buffer;
173         while (isspace(c = *buf))
174                 buf++;
175         while ((c = *buf++) != '\n')
176                 *p++ = c;
177         *p++ = 0;
178         buf = buffer;
179         memset(&tm, 0, sizeof(tm));
180         do {
181                 const char *next = strptime(buf, *fmt, &tm);
182                 if (next) {
183                         if (!*next)
184                                 return mktime(&tm);
185                         buf = next;
186                 } else {
187                         const char **p = timezones;
188                         while (isspace(*buf))
189                                 buf++;
190                         while (*p) {
191                                 if (!memcmp(buf, *p, strlen(*p))) {
192                                         buf += strlen(*p);
193                                         break;
194                                 }
195                                 p++;
196                         }
197                 }
198                 fmt++;
199         } while (*buf && *fmt);
200         printf("left: %s\n", buf);
201         return mktime(&tm);                             
202 }
203
204 static int convert_date_line(char *dst, void **buf, unsigned long *sp)
205 {
206         unsigned long size = *sp;
207         char *line = *buf;
208         char *next = strchr(line, '\n');
209         char *date = strchr(line, '>');
210         int len;
211
212         if (!next || !date)
213                 die("missing or bad author/committer line %s", line);
214         next++; date += 2;
215
216         *buf = next;
217         *sp = size - (next - line);
218
219         len = date - line;
220         memcpy(dst, line, len);
221         dst += len;
222
223         /* Is it already in new format? */
224         if (isdigit(*date)) {
225                 int datelen = next - date;
226                 memcpy(dst, date, datelen);
227                 return len + datelen;
228         }
229
230         /*
231          * Hacky hacky: one of the sparse old-style commits does not have
232          * any date at all, but we can fake it by using the committer date.
233          */
234         if (*date == '\n' && strchr(next, '>'))
235                 date = strchr(next, '>')+2;
236
237         return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
238 }
239
240 static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
241 {
242         char *new = xmalloc(size + 100);
243         unsigned long newlen = 0;
244         
245         // "tree <sha1>\n"
246         memcpy(new + newlen, buffer, 46);
247         newlen += 46;
248         buffer += 46;
249         size -= 46;
250
251         // "parent <sha1>\n"
252         while (!memcmp(buffer, "parent ", 7)) {
253                 memcpy(new + newlen, buffer, 48);
254                 newlen += 48;
255                 buffer += 48;
256                 size -= 48;
257         }
258
259         // "author xyz <xyz> date"
260         newlen += convert_date_line(new + newlen, &buffer, &size);
261         // "committer xyz <xyz> date"
262         newlen += convert_date_line(new + newlen, &buffer, &size);
263
264         // Rest
265         memcpy(new + newlen, buffer, size);
266         newlen += size;
267
268         write_sha1_file(new, newlen, commit_type, result_sha1);
269         free(new);
270 }
271
272 static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1)
273 {
274         void *orig_buffer = buffer;
275         unsigned long orig_size = size;
276
277         if (memcmp(buffer, "tree ", 5))
278                 die("Bad commit '%s'", (char*) buffer);
279         convert_ascii_sha1(buffer+5);
280         buffer += 46;    /* "tree " + "hex sha1" + "\n" */
281         while (!memcmp(buffer, "parent ", 7)) {
282                 convert_ascii_sha1(buffer+7);
283                 buffer += 48;
284         }
285         convert_date(orig_buffer, orig_size, result_sha1);
286 }
287
288 static struct entry * convert_entry(unsigned char *sha1)
289 {
290         struct entry *entry = lookup_entry(sha1);
291         char type[20];
292         void *buffer, *data;
293         unsigned long size;
294
295         if (entry->converted)
296                 return entry;
297         data = read_sha1_file(sha1, type, &size);
298         if (!data)
299                 die("unable to read object %s", sha1_to_hex(sha1));
300
301         buffer = xmalloc(size);
302         memcpy(buffer, data, size);
303
304         if (!strcmp(type, blob_type)) {
305                 write_sha1_file(buffer, size, blob_type, entry->new_sha1);
306         } else if (!strcmp(type, tree_type))
307                 convert_tree(buffer, size, entry->new_sha1);
308         else if (!strcmp(type, commit_type))
309                 convert_commit(buffer, size, entry->new_sha1);
310         else
311                 die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
312         entry->converted = 1;
313         free(buffer);
314         free(data);
315         return entry;
316 }
317
318 int main(int argc, char **argv)
319 {
320         unsigned char sha1[20];
321         struct entry *entry;
322
323         setup_git_directory();
324
325         if (argc != 2 || get_sha1(argv[1], sha1))
326                 usage("git-convert-objects <sha1>");
327
328         entry = convert_entry(sha1);
329         printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));
330         return 0;
331 }