Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 2 | #include "blob.h" |
| 3 | #include "commit.h" |
| 4 | #include "tree.h" |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 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 | { |
Peter Eriksen | 90321c1 | 2006-04-03 19:30:46 +0100 | [diff] [blame] | 21 | struct entry *new = xcalloc(1, sizeof(struct entry)); |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 22 | hashcpy(new->old_sha1, sha1); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 23 | memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *)); |
| 24 | convert[pos] = new; |
| 25 | nr_convert++; |
| 26 | if (nr_convert == MAXOBJECTS) |
| 27 | die("you're kidding me - hit maximum object limit"); |
| 28 | return new; |
| 29 | } |
| 30 | |
| 31 | static struct entry *lookup_entry(unsigned char *sha1) |
| 32 | { |
| 33 | int low = 0, high = nr_convert; |
| 34 | |
| 35 | while (low < high) { |
| 36 | int next = (low + high) / 2; |
| 37 | struct entry *n = convert[next]; |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 38 | int cmp = hashcmp(sha1, n->old_sha1); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 39 | if (!cmp) |
| 40 | return n; |
| 41 | if (cmp < 0) { |
| 42 | high = next; |
| 43 | continue; |
| 44 | } |
| 45 | low = next+1; |
| 46 | } |
| 47 | return insert_new(sha1, low); |
| 48 | } |
| 49 | |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 50 | static void convert_binary_sha1(void *buffer) |
| 51 | { |
| 52 | struct entry *entry = convert_entry(buffer); |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 53 | hashcpy(buffer, entry->new_sha1); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void convert_ascii_sha1(void *buffer) |
| 57 | { |
| 58 | unsigned char sha1[20]; |
| 59 | struct entry *entry; |
| 60 | |
| 61 | if (get_sha1_hex(buffer, sha1)) |
Junio C Hamano | 79db12e | 2005-08-09 21:25:46 -0700 | [diff] [blame] | 62 | die("expected sha1, got '%s'", (char*) buffer); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 63 | entry = convert_entry(sha1); |
| 64 | memcpy(buffer, sha1_to_hex(entry->new_sha1), 40); |
| 65 | } |
| 66 | |
Linus Torvalds | 4e81304 | 2005-07-27 15:29:38 -0700 | [diff] [blame] | 67 | static unsigned int convert_mode(unsigned int mode) |
| 68 | { |
| 69 | unsigned int newmode; |
| 70 | |
| 71 | newmode = mode & S_IFMT; |
| 72 | if (S_ISREG(mode)) |
| 73 | newmode |= (mode & 0100) ? 0755 : 0644; |
| 74 | return newmode; |
| 75 | } |
| 76 | |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 77 | static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1) |
| 78 | { |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 79 | char *new = xmalloc(size); |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 80 | unsigned long newlen = 0; |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 81 | unsigned long used; |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 82 | |
| 83 | used = 0; |
| 84 | while (size) { |
| 85 | int len = 21 + strlen(buffer); |
| 86 | char *path = strchr(buffer, ' '); |
| 87 | unsigned char *sha1; |
| 88 | unsigned int mode; |
| 89 | char *slash, *origpath; |
| 90 | |
Andy Whitcroft | 6e6db39 | 2007-04-19 03:08:15 +0100 | [diff] [blame] | 91 | if (!path || strtoul_ui(buffer, 8, &mode)) |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 92 | die("bad tree conversion"); |
Linus Torvalds | 4e81304 | 2005-07-27 15:29:38 -0700 | [diff] [blame] | 93 | mode = convert_mode(mode); |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 94 | path++; |
| 95 | if (memcmp(path, base, baselen)) |
| 96 | break; |
| 97 | origpath = path; |
| 98 | path += baselen; |
| 99 | slash = strchr(path, '/'); |
| 100 | if (!slash) { |
| 101 | newlen += sprintf(new + newlen, "%o %s", mode, path); |
| 102 | new[newlen++] = '\0'; |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 103 | hashcpy((unsigned char*)new + newlen, (unsigned char *) buffer + len - 20); |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 104 | newlen += 20; |
| 105 | |
| 106 | used += len; |
| 107 | size -= len; |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 108 | buffer = (char *) buffer + len; |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 109 | continue; |
| 110 | } |
| 111 | |
tony.luck@intel.com | f220fb6 | 2005-05-02 10:57:02 -0700 | [diff] [blame] | 112 | newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path); |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 113 | new[newlen++] = 0; |
| 114 | sha1 = (unsigned char *)(new + newlen); |
| 115 | newlen += 20; |
| 116 | |
| 117 | len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1); |
| 118 | |
| 119 | used += len; |
| 120 | size -= len; |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 121 | buffer = (char *) buffer + len; |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 124 | write_sha1_file(new, newlen, tree_type, result_sha1); |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 125 | free(new); |
| 126 | return used; |
| 127 | } |
| 128 | |
| 129 | static void convert_tree(void *buffer, unsigned long size, unsigned char *result_sha1) |
| 130 | { |
| 131 | void *orig_buffer = buffer; |
| 132 | unsigned long orig_size = size; |
| 133 | |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 134 | while (size) { |
Shawn O. Pearce | 3a55602 | 2007-03-06 20:44:17 -0500 | [diff] [blame] | 135 | size_t len = 1+strlen(buffer); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 136 | |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 137 | convert_binary_sha1((char *) buffer + len); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 138 | |
| 139 | len += 20; |
| 140 | if (len > size) |
| 141 | die("corrupt tree object"); |
| 142 | size -= len; |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 143 | buffer = (char *) buffer + len; |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 144 | } |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 145 | |
| 146 | write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 149 | static unsigned long parse_oldstyle_date(const char *buf) |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 150 | { |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 151 | char c, *p; |
| 152 | char buffer[100]; |
| 153 | struct tm tm; |
| 154 | const char *formats[] = { |
| 155 | "%c", |
| 156 | "%a %b %d %T", |
| 157 | "%Z", |
| 158 | "%Y", |
| 159 | " %Y", |
| 160 | NULL |
| 161 | }; |
| 162 | /* We only ever did two timezones in the bad old format .. */ |
| 163 | const char *timezones[] = { |
Linus Torvalds | 3f05389 | 2005-04-24 15:49:09 -0700 | [diff] [blame] | 164 | "PDT", "PST", "CEST", NULL |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 165 | }; |
| 166 | const char **fmt = formats; |
| 167 | |
| 168 | p = buffer; |
| 169 | while (isspace(c = *buf)) |
| 170 | buf++; |
| 171 | while ((c = *buf++) != '\n') |
| 172 | *p++ = c; |
| 173 | *p++ = 0; |
| 174 | buf = buffer; |
| 175 | memset(&tm, 0, sizeof(tm)); |
| 176 | do { |
| 177 | const char *next = strptime(buf, *fmt, &tm); |
| 178 | if (next) { |
| 179 | if (!*next) |
| 180 | return mktime(&tm); |
| 181 | buf = next; |
| 182 | } else { |
| 183 | const char **p = timezones; |
| 184 | while (isspace(*buf)) |
| 185 | buf++; |
| 186 | while (*p) { |
| 187 | if (!memcmp(buf, *p, strlen(*p))) { |
| 188 | buf += strlen(*p); |
| 189 | break; |
| 190 | } |
| 191 | p++; |
| 192 | } |
| 193 | } |
| 194 | fmt++; |
| 195 | } while (*buf && *fmt); |
| 196 | printf("left: %s\n", buf); |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 197 | return mktime(&tm); |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static int convert_date_line(char *dst, void **buf, unsigned long *sp) |
| 201 | { |
| 202 | unsigned long size = *sp; |
| 203 | char *line = *buf; |
| 204 | char *next = strchr(line, '\n'); |
| 205 | char *date = strchr(line, '>'); |
| 206 | int len; |
| 207 | |
| 208 | if (!next || !date) |
| 209 | die("missing or bad author/committer line %s", line); |
| 210 | next++; date += 2; |
| 211 | |
| 212 | *buf = next; |
| 213 | *sp = size - (next - line); |
| 214 | |
| 215 | len = date - line; |
| 216 | memcpy(dst, line, len); |
| 217 | dst += len; |
| 218 | |
| 219 | /* Is it already in new format? */ |
| 220 | if (isdigit(*date)) { |
| 221 | int datelen = next - date; |
| 222 | memcpy(dst, date, datelen); |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 223 | return len + datelen; |
| 224 | } |
| 225 | |
Linus Torvalds | 9325631 | 2005-04-23 16:48:32 -0700 | [diff] [blame] | 226 | /* |
| 227 | * Hacky hacky: one of the sparse old-style commits does not have |
| 228 | * any date at all, but we can fake it by using the committer date. |
| 229 | */ |
| 230 | if (*date == '\n' && strchr(next, '>')) |
| 231 | date = strchr(next, '>')+2; |
| 232 | |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 233 | return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date)); |
| 234 | } |
| 235 | |
| 236 | static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1) |
| 237 | { |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 238 | char *new = xmalloc(size + 100); |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 239 | unsigned long newlen = 0; |
Pavel Roskin | a9486b0 | 2006-07-10 02:57:51 -0400 | [diff] [blame] | 240 | |
| 241 | /* "tree <sha1>\n" */ |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 242 | memcpy(new + newlen, buffer, 46); |
| 243 | newlen += 46; |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 244 | buffer = (char *) buffer + 46; |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 245 | size -= 46; |
| 246 | |
Pavel Roskin | a9486b0 | 2006-07-10 02:57:51 -0400 | [diff] [blame] | 247 | /* "parent <sha1>\n" */ |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 248 | while (!memcmp(buffer, "parent ", 7)) { |
| 249 | memcpy(new + newlen, buffer, 48); |
| 250 | newlen += 48; |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 251 | buffer = (char *) buffer + 48; |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 252 | size -= 48; |
| 253 | } |
| 254 | |
Pavel Roskin | a9486b0 | 2006-07-10 02:57:51 -0400 | [diff] [blame] | 255 | /* "author xyz <xyz> date" */ |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 256 | newlen += convert_date_line(new + newlen, &buffer, &size); |
Pavel Roskin | a9486b0 | 2006-07-10 02:57:51 -0400 | [diff] [blame] | 257 | /* "committer xyz <xyz> date" */ |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 258 | newlen += convert_date_line(new + newlen, &buffer, &size); |
| 259 | |
Pavel Roskin | a9486b0 | 2006-07-10 02:57:51 -0400 | [diff] [blame] | 260 | /* Rest */ |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 261 | memcpy(new + newlen, buffer, size); |
| 262 | newlen += size; |
| 263 | |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 264 | write_sha1_file(new, newlen, commit_type, result_sha1); |
| 265 | free(new); |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1) |
| 269 | { |
| 270 | void *orig_buffer = buffer; |
| 271 | unsigned long orig_size = size; |
| 272 | |
Linus Torvalds | 4e81304 | 2005-07-27 15:29:38 -0700 | [diff] [blame] | 273 | if (memcmp(buffer, "tree ", 5)) |
Junio C Hamano | 79db12e | 2005-08-09 21:25:46 -0700 | [diff] [blame] | 274 | die("Bad commit '%s'", (char*) buffer); |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 275 | convert_ascii_sha1((char *) buffer + 5); |
| 276 | buffer = (char *) buffer + 46; /* "tree " + "hex sha1" + "\n" */ |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 277 | while (!memcmp(buffer, "parent ", 7)) { |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 278 | convert_ascii_sha1((char *) buffer + 7); |
| 279 | buffer = (char *) buffer + 48; |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 280 | } |
Linus Torvalds | bfac5d9 | 2005-04-23 16:37:31 -0700 | [diff] [blame] | 281 | convert_date(orig_buffer, orig_size, result_sha1); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static struct entry * convert_entry(unsigned char *sha1) |
| 285 | { |
| 286 | struct entry *entry = lookup_entry(sha1); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 287 | enum object_type type; |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 288 | void *buffer, *data; |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 289 | unsigned long size; |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 290 | |
| 291 | if (entry->converted) |
| 292 | return entry; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 293 | data = read_sha1_file(sha1, &type, &size); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 294 | if (!data) |
| 295 | die("unable to read object %s", sha1_to_hex(sha1)); |
| 296 | |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 297 | buffer = xmalloc(size); |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 298 | memcpy(buffer, data, size); |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 299 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 300 | if (type == OBJ_BLOB) { |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 301 | write_sha1_file(buffer, size, blob_type, entry->new_sha1); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 302 | } else if (type == OBJ_TREE) |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 303 | convert_tree(buffer, size, entry->new_sha1); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 304 | else if (type == OBJ_COMMIT) |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 305 | convert_commit(buffer, size, entry->new_sha1); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 306 | else |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 307 | die("unknown object type %d in %s", type, sha1_to_hex(sha1)); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 308 | entry->converted = 1; |
| 309 | free(buffer); |
Linus Torvalds | 4e81304 | 2005-07-27 15:29:38 -0700 | [diff] [blame] | 310 | free(data); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 311 | return entry; |
| 312 | } |
| 313 | |
| 314 | int main(int argc, char **argv) |
| 315 | { |
| 316 | unsigned char sha1[20]; |
| 317 | struct entry *entry; |
| 318 | |
Junio C Hamano | 53228a5 | 2005-11-26 00:50:02 -0800 | [diff] [blame] | 319 | setup_git_directory(); |
| 320 | |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 321 | if (argc != 2) |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 322 | usage("git-convert-objects <sha1>"); |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 323 | if (get_sha1(argv[1], sha1)) |
| 324 | die("Not a valid object name %s", argv[1]); |
Linus Torvalds | d98b46f | 2005-04-20 01:10:46 -0700 | [diff] [blame] | 325 | |
| 326 | entry = convert_entry(sha1); |
| 327 | printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1)); |
| 328 | return 0; |
| 329 | } |