Merge branch 'jc/test-3402'
[git/jrn.git] / convert-objects.c
blob0fabd8981c6047e5144cfb3776b4f4a5588f24e5
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"
9 struct entry {
10 unsigned char old_sha1[20];
11 unsigned char new_sha1[20];
12 int converted;
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 *));
27 convert[pos] = new;
28 nr_convert++;
29 if (nr_convert == MAXOBJECTS)
30 die("you're kidding me - hit maximum object limit");
31 return new;
34 static struct entry *lookup_entry(unsigned char *sha1)
36 int low = 0, high = nr_convert;
38 while (low < high) {
39 int next = (low + high) / 2;
40 struct entry *n = convert[next];
41 int cmp = memcmp(sha1, n->old_sha1, 20);
42 if (!cmp)
43 return n;
44 if (cmp < 0) {
45 high = next;
46 continue;
48 low = next+1;
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];
62 struct entry *entry;
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)
72 unsigned int newmode;
74 newmode = mode & S_IFMT;
75 if (S_ISREG(mode))
76 newmode |= (mode & 0100) ? 0755 : 0644;
77 return newmode;
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;
84 unsigned long used;
86 used = 0;
87 while (size) {
88 int len = 21 + strlen(buffer);
89 char *path = strchr(buffer, ' ');
90 unsigned char *sha1;
91 unsigned int mode;
92 char *slash, *origpath;
94 if (!path || sscanf(buffer, "%o", &mode) != 1)
95 die("bad tree conversion");
96 mode = convert_mode(mode);
97 path++;
98 if (memcmp(path, base, baselen))
99 break;
100 origpath = path;
101 path += baselen;
102 slash = strchr(path, '/');
103 if (!slash) {
104 newlen += sprintf(new + newlen, "%o %s", mode, path);
105 new[newlen++] = '\0';
106 memcpy(new + newlen, (char *) buffer + len - 20, 20);
107 newlen += 20;
109 used += len;
110 size -= len;
111 buffer = (char *) buffer + len;
112 continue;
115 newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
116 new[newlen++] = 0;
117 sha1 = (unsigned char *)(new + newlen);
118 newlen += 20;
120 len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1);
122 used += len;
123 size -= len;
124 buffer = (char *) buffer + len;
127 write_sha1_file(new, newlen, tree_type, result_sha1);
128 free(new);
129 return used;
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;
137 while (size) {
138 int len = 1+strlen(buffer);
140 convert_binary_sha1((char *) buffer + len);
142 len += 20;
143 if (len > size)
144 die("corrupt tree object");
145 size -= len;
146 buffer = (char *) buffer + len;
149 write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
152 static unsigned long parse_oldstyle_date(const char *buf)
154 char c, *p;
155 char buffer[100];
156 struct tm tm;
157 const char *formats[] = {
158 "%c",
159 "%a %b %d %T",
160 "%Z",
161 "%Y",
162 " %Y",
163 NULL
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;
171 p = buffer;
172 while (isspace(c = *buf))
173 buf++;
174 while ((c = *buf++) != '\n')
175 *p++ = c;
176 *p++ = 0;
177 buf = buffer;
178 memset(&tm, 0, sizeof(tm));
179 do {
180 const char *next = strptime(buf, *fmt, &tm);
181 if (next) {
182 if (!*next)
183 return mktime(&tm);
184 buf = next;
185 } else {
186 const char **p = timezones;
187 while (isspace(*buf))
188 buf++;
189 while (*p) {
190 if (!memcmp(buf, *p, strlen(*p))) {
191 buf += strlen(*p);
192 break;
194 p++;
197 fmt++;
198 } while (*buf && *fmt);
199 printf("left: %s\n", buf);
200 return mktime(&tm);
203 static int convert_date_line(char *dst, void **buf, unsigned long *sp)
205 unsigned long size = *sp;
206 char *line = *buf;
207 char *next = strchr(line, '\n');
208 char *date = strchr(line, '>');
209 int len;
211 if (!next || !date)
212 die("missing or bad author/committer line %s", line);
213 next++; date += 2;
215 *buf = next;
216 *sp = size - (next - line);
218 len = date - line;
219 memcpy(dst, line, len);
220 dst += 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;
244 // "tree <sha1>\n"
245 memcpy(new + newlen, buffer, 46);
246 newlen += 46;
247 buffer = (char *) buffer + 46;
248 size -= 46;
250 // "parent <sha1>\n"
251 while (!memcmp(buffer, "parent ", 7)) {
252 memcpy(new + newlen, buffer, 48);
253 newlen += 48;
254 buffer = (char *) buffer + 48;
255 size -= 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);
263 // Rest
264 memcpy(new + newlen, buffer, size);
265 newlen += size;
267 write_sha1_file(new, newlen, commit_type, result_sha1);
268 free(new);
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((char *) buffer + 5);
279 buffer = (char *) buffer + 46; /* "tree " + "hex sha1" + "\n" */
280 while (!memcmp(buffer, "parent ", 7)) {
281 convert_ascii_sha1((char *) buffer + 7);
282 buffer = (char *) buffer + 48;
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);
290 char type[20];
291 void *buffer, *data;
292 unsigned long size;
294 if (entry->converted)
295 return entry;
296 data = read_sha1_file(sha1, type, &size);
297 if (!data)
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);
309 else
310 die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
311 entry->converted = 1;
312 free(buffer);
313 free(data);
314 return entry;
317 int main(int argc, char **argv)
319 unsigned char sha1[20];
320 struct entry *entry;
322 setup_git_directory();
324 if (argc != 2)
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));
331 return 0;