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