fsck-cache: warn about missing commit dates
[git/gitweb.git] / convert-cache.c
blob431b11798816a88c78daa846fdebf41ed57f1378
1 #define _XOPEN_SOURCE /* glibc2 needs this */
2 #include <time.h>
3 #include <ctype.h>
4 #include "cache.h"
6 struct entry {
7 unsigned char old_sha1[20];
8 unsigned char new_sha1[20];
9 int converted;
12 #define MAXOBJECTS (1000000)
14 static struct entry *convert[MAXOBJECTS];
15 static int nr_convert;
17 static struct entry * convert_entry(unsigned char *sha1);
19 static struct entry *insert_new(unsigned char *sha1, int pos)
21 struct entry *new = malloc(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 *));
26 convert[pos] = new;
27 nr_convert++;
28 if (nr_convert == MAXOBJECTS)
29 die("you're kidding me - hit maximum object limit");
30 return new;
33 static struct entry *lookup_entry(unsigned char *sha1)
35 int low = 0, high = nr_convert;
37 while (low < high) {
38 int next = (low + high) / 2;
39 struct entry *n = convert[next];
40 int cmp = memcmp(sha1, n->old_sha1, 20);
41 if (!cmp)
42 return n;
43 if (cmp < 0) {
44 high = next;
45 continue;
47 low = next+1;
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];
61 struct entry *entry;
63 if (get_sha1_hex(buffer, sha1))
64 die("bad sha1");
65 entry = convert_entry(sha1);
66 memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
69 #define ORIG_OFFSET (40)
71 static int prepend_integer(char *buffer, unsigned val, int i)
73 buffer[--i] = '\0';
74 do {
75 buffer[--i] = '0' + (val % 10);
76 val /= 10;
77 } while (val);
78 return i;
82 static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
84 char *new = malloc(size + ORIG_OFFSET);
85 unsigned long newlen = ORIG_OFFSET;
86 unsigned long used;
87 int i;
89 used = 0;
90 while (size) {
91 int len = 21 + strlen(buffer);
92 char *path = strchr(buffer, ' ');
93 unsigned char *sha1;
94 unsigned int mode;
95 char *slash, *origpath;
97 if (!path || sscanf(buffer, "%o", &mode) != 1)
98 die("bad tree conversion");
99 path++;
100 if (memcmp(path, base, baselen))
101 break;
102 origpath = path;
103 path += baselen;
104 slash = strchr(path, '/');
105 if (!slash) {
106 newlen += sprintf(new + newlen, "%o %s", mode, path);
107 new[newlen++] = '\0';
108 memcpy(new + newlen, buffer + len - 20, 20);
109 newlen += 20;
111 used += len;
112 size -= len;
113 buffer += len;
114 continue;
117 newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, slash - path, path);
118 new[newlen++] = 0;
119 sha1 = (unsigned char *)(new + newlen);
120 newlen += 20;
122 len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1);
124 used += len;
125 size -= len;
126 buffer += len;
129 i = prepend_integer(new, newlen - ORIG_OFFSET, ORIG_OFFSET);
130 i -= 5;
131 memcpy(new + i, "tree ", 5);
133 write_sha1_file(new + i, newlen - i, result_sha1);
134 free(new);
135 return used;
138 static void convert_tree(void *buffer, unsigned long size, unsigned char *result_sha1)
140 void *orig_buffer = buffer;
141 unsigned long orig_size = size;
143 while (size) {
144 int len = 1+strlen(buffer);
146 convert_binary_sha1(buffer + len);
148 len += 20;
149 if (len > size)
150 die("corrupt tree object");
151 size -= len;
152 buffer += len;
155 write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
158 static unsigned long parse_oldstyle_date(const char *buf)
160 char c, *p;
161 char buffer[100];
162 struct tm tm;
163 const char *formats[] = {
164 "%c",
165 "%a %b %d %T",
166 "%Z",
167 "%Y",
168 " %Y",
169 NULL
171 /* We only ever did two timezones in the bad old format .. */
172 const char *timezones[] = {
173 "PDT", "PST", "CEST", NULL
175 const char **fmt = formats;
177 p = buffer;
178 while (isspace(c = *buf))
179 buf++;
180 while ((c = *buf++) != '\n')
181 *p++ = c;
182 *p++ = 0;
183 buf = buffer;
184 memset(&tm, 0, sizeof(tm));
185 do {
186 const char *next = strptime(buf, *fmt, &tm);
187 if (next) {
188 if (!*next)
189 return mktime(&tm);
190 buf = next;
191 } else {
192 const char **p = timezones;
193 while (isspace(*buf))
194 buf++;
195 while (*p) {
196 if (!memcmp(buf, *p, strlen(*p))) {
197 buf += strlen(*p);
198 break;
200 p++;
203 fmt++;
204 } while (*buf && *fmt);
205 printf("left: %s\n", buf);
206 return mktime(&tm);
209 static int convert_date_line(char *dst, void **buf, unsigned long *sp)
211 unsigned long size = *sp;
212 char *line = *buf;
213 char *next = strchr(line, '\n');
214 char *date = strchr(line, '>');
215 int len;
217 if (!next || !date)
218 die("missing or bad author/committer line %s", line);
219 next++; date += 2;
221 *buf = next;
222 *sp = size - (next - line);
224 len = date - line;
225 memcpy(dst, line, len);
226 dst += len;
228 /* Is it already in new format? */
229 if (isdigit(*date)) {
230 int datelen = next - date;
231 memcpy(dst, date, datelen);
232 return len + datelen;
236 * Hacky hacky: one of the sparse old-style commits does not have
237 * any date at all, but we can fake it by using the committer date.
239 if (*date == '\n' && strchr(next, '>'))
240 date = strchr(next, '>')+2;
242 return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
245 static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
247 char *new = malloc(size + ORIG_OFFSET + 100);
248 unsigned long newlen = ORIG_OFFSET;
249 int i;
251 // "tree <sha1>\n"
252 memcpy(new + newlen, buffer, 46);
253 newlen += 46;
254 buffer += 46;
255 size -= 46;
257 // "parent <sha1>\n"
258 while (!memcmp(buffer, "parent ", 7)) {
259 memcpy(new + newlen, buffer, 48);
260 newlen += 48;
261 buffer += 48;
262 size -= 48;
265 // "author xyz <xyz> date"
266 newlen += convert_date_line(new + newlen, &buffer, &size);
267 // "committer xyz <xyz> date"
268 newlen += convert_date_line(new + newlen, &buffer, &size);
270 // Rest
271 memcpy(new + newlen, buffer, size);
272 newlen += size;
274 i = prepend_integer(new, newlen - ORIG_OFFSET, ORIG_OFFSET);
275 i -= 7;
276 memcpy(new + i, "commit ", 7);
278 write_sha1_file(new + i, newlen - i, result_sha1);
279 free(new);
282 static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1)
284 void *orig_buffer = buffer;
285 unsigned long orig_size = size;
287 convert_ascii_sha1(buffer+5);
288 buffer += 46; /* "tree " + "hex sha1" + "\n" */
289 while (!memcmp(buffer, "parent ", 7)) {
290 convert_ascii_sha1(buffer+7);
291 buffer += 48;
293 convert_date(orig_buffer, orig_size, result_sha1);
296 static struct entry * convert_entry(unsigned char *sha1)
298 struct entry *entry = lookup_entry(sha1);
299 char type[20];
300 void *buffer, *data;
301 unsigned long size, offset;
303 if (entry->converted)
304 return entry;
305 data = read_sha1_file(sha1, type, &size);
306 if (!data)
307 die("unable to read object %s", sha1_to_hex(sha1));
309 buffer = malloc(size + 100);
310 offset = sprintf(buffer, "%s %lu", type, size)+1;
311 memcpy(buffer + offset, data, size);
313 if (!strcmp(type, "blob")) {
314 write_sha1_file(buffer, size + offset, entry->new_sha1);
315 } else if (!strcmp(type, "tree"))
316 convert_tree(buffer + offset, size, entry->new_sha1);
317 else if (!strcmp(type, "commit"))
318 convert_commit(buffer + offset, size, entry->new_sha1);
319 else
320 die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
321 entry->converted = 1;
322 free(buffer);
323 return entry;
326 int main(int argc, char **argv)
328 unsigned char sha1[20];
329 struct entry *entry;
331 if (argc != 2 || get_sha1_hex(argv[1], sha1))
332 usage("convert-cache <sha1>");
334 entry = convert_entry(sha1);
335 printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));
336 return 0;