git-diff -B output fix.
[git/mingw/4msysgit/kblees.git] / convert-objects.c
blob168771ed857dd9e95b1e14da40f1e39d18f7b2cf
1 #define _XOPEN_SOURCE 500 /* glibc2 and AIX 5.3L need this */
2 #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
3 #define _GNU_SOURCE
4 #include <time.h>
5 #include "cache.h"
6 #include "blob.h"
7 #include "commit.h"
8 #include "tree.h"
10 struct entry {
11 unsigned char old_sha1[20];
12 unsigned char new_sha1[20];
13 int converted;
16 #define MAXOBJECTS (1000000)
18 static struct entry *convert[MAXOBJECTS];
19 static int nr_convert;
21 static struct entry * convert_entry(unsigned char *sha1);
23 static struct entry *insert_new(unsigned char *sha1, int pos)
25 struct entry *new = xcalloc(1, sizeof(struct entry));
26 memcpy(new->old_sha1, sha1, 20);
27 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;
35 static struct entry *lookup_entry(unsigned char *sha1)
37 int low = 0, high = nr_convert;
39 while (low < high) {
40 int next = (low + high) / 2;
41 struct entry *n = convert[next];
42 int cmp = memcmp(sha1, n->old_sha1, 20);
43 if (!cmp)
44 return n;
45 if (cmp < 0) {
46 high = next;
47 continue;
49 low = next+1;
51 return insert_new(sha1, low);
54 static void convert_binary_sha1(void *buffer)
56 struct entry *entry = convert_entry(buffer);
57 memcpy(buffer, entry->new_sha1, 20);
60 static void convert_ascii_sha1(void *buffer)
62 unsigned char sha1[20];
63 struct entry *entry;
65 if (get_sha1_hex(buffer, sha1))
66 die("expected sha1, got '%s'", (char*) buffer);
67 entry = convert_entry(sha1);
68 memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
71 static unsigned int convert_mode(unsigned int mode)
73 unsigned int newmode;
75 newmode = mode & S_IFMT;
76 if (S_ISREG(mode))
77 newmode |= (mode & 0100) ? 0755 : 0644;
78 return newmode;
81 static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
83 char *new = xmalloc(size);
84 unsigned long newlen = 0;
85 unsigned long used;
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;
95 if (!path || sscanf(buffer, "%o", &mode) != 1)
96 die("bad tree conversion");
97 mode = convert_mode(mode);
98 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';
107 memcpy(new + newlen, (char *) buffer + len - 20, 20);
108 newlen += 20;
110 used += len;
111 size -= len;
112 buffer = (char *) buffer + len;
113 continue;
116 newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
117 new[newlen++] = 0;
118 sha1 = (unsigned char *)(new + newlen);
119 newlen += 20;
121 len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1);
123 used += len;
124 size -= len;
125 buffer = (char *) buffer + len;
128 write_sha1_file(new, newlen, tree_type, result_sha1);
129 free(new);
130 return used;
133 static void convert_tree(void *buffer, unsigned long size, unsigned char *result_sha1)
135 void *orig_buffer = buffer;
136 unsigned long orig_size = size;
138 while (size) {
139 int len = 1+strlen(buffer);
141 convert_binary_sha1((char *) buffer + len);
143 len += 20;
144 if (len > size)
145 die("corrupt tree object");
146 size -= len;
147 buffer = (char *) buffer + len;
150 write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
153 static unsigned long parse_oldstyle_date(const char *buf)
155 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
166 /* We only ever did two timezones in the bad old format .. */
167 const char *timezones[] = {
168 "PDT", "PST", "CEST", NULL
170 const char **fmt = formats;
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;
195 p++;
198 fmt++;
199 } while (*buf && *fmt);
200 printf("left: %s\n", buf);
201 return mktime(&tm);
204 static int convert_date_line(char *dst, void **buf, unsigned long *sp)
206 unsigned long size = *sp;
207 char *line = *buf;
208 char *next = strchr(line, '\n');
209 char *date = strchr(line, '>');
210 int len;
212 if (!next || !date)
213 die("missing or bad author/committer line %s", line);
214 next++; date += 2;
216 *buf = next;
217 *sp = size - (next - line);
219 len = date - line;
220 memcpy(dst, line, len);
221 dst += len;
223 /* Is it already in new format? */
224 if (isdigit(*date)) {
225 int datelen = next - date;
226 memcpy(dst, date, datelen);
227 return len + datelen;
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.
234 if (*date == '\n' && strchr(next, '>'))
235 date = strchr(next, '>')+2;
237 return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
240 static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
242 char *new = xmalloc(size + 100);
243 unsigned long newlen = 0;
245 /* "tree <sha1>\n" */
246 memcpy(new + newlen, buffer, 46);
247 newlen += 46;
248 buffer = (char *) buffer + 46;
249 size -= 46;
251 /* "parent <sha1>\n" */
252 while (!memcmp(buffer, "parent ", 7)) {
253 memcpy(new + newlen, buffer, 48);
254 newlen += 48;
255 buffer = (char *) buffer + 48;
256 size -= 48;
259 /* "author xyz <xyz> date" */
260 newlen += convert_date_line(new + newlen, &buffer, &size);
261 /* "committer xyz <xyz> date" */
262 newlen += convert_date_line(new + newlen, &buffer, &size);
264 /* Rest */
265 memcpy(new + newlen, buffer, size);
266 newlen += size;
268 write_sha1_file(new, newlen, commit_type, result_sha1);
269 free(new);
272 static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1)
274 void *orig_buffer = buffer;
275 unsigned long orig_size = size;
277 if (memcmp(buffer, "tree ", 5))
278 die("Bad commit '%s'", (char*) buffer);
279 convert_ascii_sha1((char *) buffer + 5);
280 buffer = (char *) buffer + 46; /* "tree " + "hex sha1" + "\n" */
281 while (!memcmp(buffer, "parent ", 7)) {
282 convert_ascii_sha1((char *) buffer + 7);
283 buffer = (char *) buffer + 48;
285 convert_date(orig_buffer, orig_size, result_sha1);
288 static struct entry * convert_entry(unsigned char *sha1)
290 struct entry *entry = lookup_entry(sha1);
291 char type[20];
292 void *buffer, *data;
293 unsigned long size;
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));
301 buffer = xmalloc(size);
302 memcpy(buffer, data, size);
304 if (!strcmp(type, blob_type)) {
305 write_sha1_file(buffer, size, blob_type, entry->new_sha1);
306 } else if (!strcmp(type, tree_type))
307 convert_tree(buffer, size, entry->new_sha1);
308 else if (!strcmp(type, commit_type))
309 convert_commit(buffer, size, entry->new_sha1);
310 else
311 die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
312 entry->converted = 1;
313 free(buffer);
314 free(data);
315 return entry;
318 int main(int argc, char **argv)
320 unsigned char sha1[20];
321 struct entry *entry;
323 setup_git_directory();
325 if (argc != 2)
326 usage("git-convert-objects <sha1>");
327 if (get_sha1(argv[1], sha1))
328 die("Not a valid object name %s", argv[1]);
330 entry = convert_entry(sha1);
331 printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));
332 return 0;