blob: 631678b08a7cb83b0349f1a683d3cd7fb797fb7a [file] [log] [blame]
Jason Riedya6da9392005-12-06 14:20:16 -08001#define _XOPEN_SOURCE 500 /* glibc2 and AIX 5.3L need this */
2#define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
Ramsay Allan Jonesda7bad52006-07-30 16:52:09 +01003#define _GNU_SOURCE
Linus Torvaldsbfac5d92005-04-23 16:37:31 -07004#include <time.h>
Linus Torvaldsd98b46f2005-04-20 01:10:46 -07005#include "cache.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02006#include "blob.h"
7#include "commit.h"
8#include "tree.h"
Linus Torvaldsd98b46f2005-04-20 01:10:46 -07009
10struct entry {
11 unsigned char old_sha1[20];
12 unsigned char new_sha1[20];
13 int converted;
14};
15
16#define MAXOBJECTS (1000000)
17
18static struct entry *convert[MAXOBJECTS];
19static int nr_convert;
20
21static struct entry * convert_entry(unsigned char *sha1);
22
23static struct entry *insert_new(unsigned char *sha1, int pos)
24{
Peter Eriksen90321c12006-04-03 19:30:46 +010025 struct entry *new = xcalloc(1, sizeof(struct entry));
Shawn Pearcee7024962006-08-23 02:49:00 -040026 hashcpy(new->old_sha1, sha1);
Linus Torvaldsd98b46f2005-04-20 01:10:46 -070027 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
35static 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];
David Rientjesa89fccd2006-08-17 11:54:57 -070042 int cmp = hashcmp(sha1, n->old_sha1);
Linus Torvaldsd98b46f2005-04-20 01:10:46 -070043 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
Linus Torvaldsd98b46f2005-04-20 01:10:46 -070054static void convert_binary_sha1(void *buffer)
55{
56 struct entry *entry = convert_entry(buffer);
Shawn Pearcee7024962006-08-23 02:49:00 -040057 hashcpy(buffer, entry->new_sha1);
Linus Torvaldsd98b46f2005-04-20 01:10:46 -070058}
59
60static void convert_ascii_sha1(void *buffer)
61{
62 unsigned char sha1[20];
63 struct entry *entry;
64
65 if (get_sha1_hex(buffer, sha1))
Junio C Hamano79db12e2005-08-09 21:25:46 -070066 die("expected sha1, got '%s'", (char*) buffer);
Linus Torvaldsd98b46f2005-04-20 01:10:46 -070067 entry = convert_entry(sha1);
68 memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
69}
70
Linus Torvalds4e813042005-07-27 15:29:38 -070071static 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
Linus Torvaldsbfac5d92005-04-23 16:37:31 -070081static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
82{
Christopher Li812666c2005-04-26 12:00:58 -070083 char *new = xmalloc(size);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -070084 unsigned long newlen = 0;
Linus Torvaldsbfac5d92005-04-23 16:37:31 -070085 unsigned long used;
Linus Torvaldsbfac5d92005-04-23 16:37:31 -070086
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");
Linus Torvalds4e813042005-07-27 15:29:38 -070097 mode = convert_mode(mode);
Linus Torvaldsbfac5d92005-04-23 16:37:31 -070098 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';
Shawn Pearcee7024962006-08-23 02:49:00 -0400107 hashcpy((unsigned char*)new + newlen, (unsigned char *) buffer + len - 20);
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700108 newlen += 20;
109
110 used += len;
111 size -= len;
Florian Forster1d7f1712006-06-18 17:18:09 +0200112 buffer = (char *) buffer + len;
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700113 continue;
114 }
115
tony.luck@intel.comf220fb62005-05-02 10:57:02 -0700116 newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700117 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;
Florian Forster1d7f1712006-06-18 17:18:09 +0200125 buffer = (char *) buffer + len;
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700126 }
127
Peter Eriksen8e440252006-04-02 14:44:09 +0200128 write_sha1_file(new, newlen, tree_type, result_sha1);
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700129 free(new);
130 return used;
131}
132
133static 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
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700138 while (size) {
139 int len = 1+strlen(buffer);
140
Florian Forster1d7f1712006-06-18 17:18:09 +0200141 convert_binary_sha1((char *) buffer + len);
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700142
143 len += 20;
144 if (len > size)
145 die("corrupt tree object");
146 size -= len;
Florian Forster1d7f1712006-06-18 17:18:09 +0200147 buffer = (char *) buffer + len;
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700148 }
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700149
150 write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700151}
152
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700153static unsigned long parse_oldstyle_date(const char *buf)
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700154{
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700155 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[] = {
Linus Torvalds3f053892005-04-24 15:49:09 -0700168 "PDT", "PST", "CEST", NULL
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700169 };
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
204static 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);
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700227 return len + datelen;
228 }
229
Linus Torvalds93256312005-04-23 16:48:32 -0700230 /*
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
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700237 return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
238}
239
240static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
241{
Christopher Li812666c2005-04-26 12:00:58 -0700242 char *new = xmalloc(size + 100);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -0700243 unsigned long newlen = 0;
Pavel Roskina9486b02006-07-10 02:57:51 -0400244
245 /* "tree <sha1>\n" */
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700246 memcpy(new + newlen, buffer, 46);
247 newlen += 46;
Florian Forster1d7f1712006-06-18 17:18:09 +0200248 buffer = (char *) buffer + 46;
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700249 size -= 46;
250
Pavel Roskina9486b02006-07-10 02:57:51 -0400251 /* "parent <sha1>\n" */
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700252 while (!memcmp(buffer, "parent ", 7)) {
253 memcpy(new + newlen, buffer, 48);
254 newlen += 48;
Florian Forster1d7f1712006-06-18 17:18:09 +0200255 buffer = (char *) buffer + 48;
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700256 size -= 48;
257 }
258
Pavel Roskina9486b02006-07-10 02:57:51 -0400259 /* "author xyz <xyz> date" */
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700260 newlen += convert_date_line(new + newlen, &buffer, &size);
Pavel Roskina9486b02006-07-10 02:57:51 -0400261 /* "committer xyz <xyz> date" */
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700262 newlen += convert_date_line(new + newlen, &buffer, &size);
263
Pavel Roskina9486b02006-07-10 02:57:51 -0400264 /* Rest */
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700265 memcpy(new + newlen, buffer, size);
266 newlen += size;
267
Peter Eriksen8e440252006-04-02 14:44:09 +0200268 write_sha1_file(new, newlen, commit_type, result_sha1);
269 free(new);
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700270}
271
272static 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
Linus Torvalds4e813042005-07-27 15:29:38 -0700277 if (memcmp(buffer, "tree ", 5))
Junio C Hamano79db12e2005-08-09 21:25:46 -0700278 die("Bad commit '%s'", (char*) buffer);
Florian Forster1d7f1712006-06-18 17:18:09 +0200279 convert_ascii_sha1((char *) buffer + 5);
280 buffer = (char *) buffer + 46; /* "tree " + "hex sha1" + "\n" */
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700281 while (!memcmp(buffer, "parent ", 7)) {
Florian Forster1d7f1712006-06-18 17:18:09 +0200282 convert_ascii_sha1((char *) buffer + 7);
283 buffer = (char *) buffer + 48;
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700284 }
Linus Torvaldsbfac5d92005-04-23 16:37:31 -0700285 convert_date(orig_buffer, orig_size, result_sha1);
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700286}
287
288static struct entry * convert_entry(unsigned char *sha1)
289{
290 struct entry *entry = lookup_entry(sha1);
291 char type[20];
292 void *buffer, *data;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -0700293 unsigned long size;
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700294
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
Christopher Li812666c2005-04-26 12:00:58 -0700301 buffer = xmalloc(size);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -0700302 memcpy(buffer, data, size);
Peter Eriksen8e440252006-04-02 14:44:09 +0200303
304 if (!strcmp(type, blob_type)) {
305 write_sha1_file(buffer, size, blob_type, entry->new_sha1);
306 } else if (!strcmp(type, tree_type))
Linus Torvaldsa44c9a52005-04-25 10:19:53 -0700307 convert_tree(buffer, size, entry->new_sha1);
Peter Eriksen8e440252006-04-02 14:44:09 +0200308 else if (!strcmp(type, commit_type))
Linus Torvaldsa44c9a52005-04-25 10:19:53 -0700309 convert_commit(buffer, size, entry->new_sha1);
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700310 else
311 die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700312 entry->converted = 1;
313 free(buffer);
Linus Torvalds4e813042005-07-27 15:29:38 -0700314 free(data);
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700315 return entry;
316}
317
318int main(int argc, char **argv)
319{
320 unsigned char sha1[20];
321 struct entry *entry;
322
Junio C Hamano53228a52005-11-26 00:50:02 -0800323 setup_git_directory();
324
Dmitry V. Levin31fff302006-05-09 01:43:38 +0400325 if (argc != 2)
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700326 usage("git-convert-objects <sha1>");
Dmitry V. Levin31fff302006-05-09 01:43:38 +0400327 if (get_sha1(argv[1], sha1))
328 die("Not a valid object name %s", argv[1]);
Linus Torvaldsd98b46f2005-04-20 01:10:46 -0700329
330 entry = convert_entry(sha1);
331 printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));
332 return 0;
333}