[PATCH] Split external diff command interface to a separate file.
[git/gitweb.git] / convert-cache.c
blob7102e455163563894448fd7090d2409e379ed314
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 static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
71 char *new = malloc(size);
72 unsigned long newlen = 0;
73 unsigned long used;
75 used = 0;
76 while (size) {
77 int len = 21 + strlen(buffer);
78 char *path = strchr(buffer, ' ');
79 unsigned char *sha1;
80 unsigned int mode;
81 char *slash, *origpath;
83 if (!path || sscanf(buffer, "%o", &mode) != 1)
84 die("bad tree conversion");
85 path++;
86 if (memcmp(path, base, baselen))
87 break;
88 origpath = path;
89 path += baselen;
90 slash = strchr(path, '/');
91 if (!slash) {
92 newlen += sprintf(new + newlen, "%o %s", mode, path);
93 new[newlen++] = '\0';
94 memcpy(new + newlen, buffer + len - 20, 20);
95 newlen += 20;
97 used += len;
98 size -= len;
99 buffer += len;
100 continue;
103 newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, slash - path, path);
104 new[newlen++] = 0;
105 sha1 = (unsigned char *)(new + newlen);
106 newlen += 20;
108 len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1);
110 used += len;
111 size -= len;
112 buffer += len;
115 write_sha1_file(new, newlen, "tree", result_sha1);
116 free(new);
117 return used;
120 static void convert_tree(void *buffer, unsigned long size, unsigned char *result_sha1)
122 void *orig_buffer = buffer;
123 unsigned long orig_size = size;
125 while (size) {
126 int len = 1+strlen(buffer);
128 convert_binary_sha1(buffer + len);
130 len += 20;
131 if (len > size)
132 die("corrupt tree object");
133 size -= len;
134 buffer += len;
137 write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
140 static unsigned long parse_oldstyle_date(const char *buf)
142 char c, *p;
143 char buffer[100];
144 struct tm tm;
145 const char *formats[] = {
146 "%c",
147 "%a %b %d %T",
148 "%Z",
149 "%Y",
150 " %Y",
151 NULL
153 /* We only ever did two timezones in the bad old format .. */
154 const char *timezones[] = {
155 "PDT", "PST", "CEST", NULL
157 const char **fmt = formats;
159 p = buffer;
160 while (isspace(c = *buf))
161 buf++;
162 while ((c = *buf++) != '\n')
163 *p++ = c;
164 *p++ = 0;
165 buf = buffer;
166 memset(&tm, 0, sizeof(tm));
167 do {
168 const char *next = strptime(buf, *fmt, &tm);
169 if (next) {
170 if (!*next)
171 return mktime(&tm);
172 buf = next;
173 } else {
174 const char **p = timezones;
175 while (isspace(*buf))
176 buf++;
177 while (*p) {
178 if (!memcmp(buf, *p, strlen(*p))) {
179 buf += strlen(*p);
180 break;
182 p++;
185 fmt++;
186 } while (*buf && *fmt);
187 printf("left: %s\n", buf);
188 return mktime(&tm);
191 static int convert_date_line(char *dst, void **buf, unsigned long *sp)
193 unsigned long size = *sp;
194 char *line = *buf;
195 char *next = strchr(line, '\n');
196 char *date = strchr(line, '>');
197 int len;
199 if (!next || !date)
200 die("missing or bad author/committer line %s", line);
201 next++; date += 2;
203 *buf = next;
204 *sp = size - (next - line);
206 len = date - line;
207 memcpy(dst, line, len);
208 dst += len;
210 /* Is it already in new format? */
211 if (isdigit(*date)) {
212 int datelen = next - date;
213 memcpy(dst, date, datelen);
214 return len + datelen;
218 * Hacky hacky: one of the sparse old-style commits does not have
219 * any date at all, but we can fake it by using the committer date.
221 if (*date == '\n' && strchr(next, '>'))
222 date = strchr(next, '>')+2;
224 return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
227 static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
229 char *new = malloc(size + 100);
230 unsigned long newlen = 0;
232 // "tree <sha1>\n"
233 memcpy(new + newlen, buffer, 46);
234 newlen += 46;
235 buffer += 46;
236 size -= 46;
238 // "parent <sha1>\n"
239 while (!memcmp(buffer, "parent ", 7)) {
240 memcpy(new + newlen, buffer, 48);
241 newlen += 48;
242 buffer += 48;
243 size -= 48;
246 // "author xyz <xyz> date"
247 newlen += convert_date_line(new + newlen, &buffer, &size);
248 // "committer xyz <xyz> date"
249 newlen += convert_date_line(new + newlen, &buffer, &size);
251 // Rest
252 memcpy(new + newlen, buffer, size);
253 newlen += size;
255 write_sha1_file(new, newlen, "commit", result_sha1);
256 free(new);
259 static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1)
261 void *orig_buffer = buffer;
262 unsigned long orig_size = size;
264 convert_ascii_sha1(buffer+5);
265 buffer += 46; /* "tree " + "hex sha1" + "\n" */
266 while (!memcmp(buffer, "parent ", 7)) {
267 convert_ascii_sha1(buffer+7);
268 buffer += 48;
270 convert_date(orig_buffer, orig_size, result_sha1);
273 static struct entry * convert_entry(unsigned char *sha1)
275 struct entry *entry = lookup_entry(sha1);
276 char type[20];
277 void *buffer, *data;
278 unsigned long size;
280 if (entry->converted)
281 return entry;
282 data = read_sha1_file(sha1, type, &size);
283 if (!data)
284 die("unable to read object %s", sha1_to_hex(sha1));
286 buffer = malloc(size);
287 memcpy(buffer, data, size);
289 if (!strcmp(type, "blob")) {
290 write_sha1_file(buffer, size, "blob", entry->new_sha1);
291 } else if (!strcmp(type, "tree"))
292 convert_tree(buffer, size, entry->new_sha1);
293 else if (!strcmp(type, "commit"))
294 convert_commit(buffer, size, entry->new_sha1);
295 else
296 die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
297 entry->converted = 1;
298 free(buffer);
299 return entry;
302 int main(int argc, char **argv)
304 unsigned char sha1[20];
305 struct entry *entry;
307 if (argc != 2 || get_sha1_hex(argv[1], sha1))
308 usage("convert-cache <sha1>");
310 entry = convert_entry(sha1);
311 printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));
312 return 0;