]> git.ipfire.org Git - thirdparty/git.git/blame - convert-objects.c
shortlog: fix segfault on empty authorname
[thirdparty/git.git] / convert-objects.c
CommitLineData
73fbd33c 1#define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
a6da9395 2#define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
da7bad50 3#define _GNU_SOURCE
bfac5d94 4#include <time.h>
d98b46f8 5#include "cache.h"
8e440259
PE
6#include "blob.h"
7#include "commit.h"
8#include "tree.h"
d98b46f8
LT
9
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{
90321c10 25 struct entry *new = xcalloc(1, sizeof(struct entry));
e702496e 26 hashcpy(new->old_sha1, sha1);
d98b46f8
LT
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;
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];
a89fccd2 42 int cmp = hashcmp(sha1, n->old_sha1);
d98b46f8
LT
43 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
d98b46f8
LT
54static void convert_binary_sha1(void *buffer)
55{
56 struct entry *entry = convert_entry(buffer);
e702496e 57 hashcpy(buffer, entry->new_sha1);
d98b46f8
LT
58}
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))
79db12e8 66 die("expected sha1, got '%s'", (char*) buffer);
d98b46f8
LT
67 entry = convert_entry(sha1);
68 memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
69}
70
4e81304a
LT
71static 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
bfac5d94 81static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
d98b46f8 82{
812666c8 83 char *new = xmalloc(size);
a44c9a5e 84 unsigned long newlen = 0;
bfac5d94 85 unsigned long used;
bfac5d94
LT
86
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");
4e81304a 97 mode = convert_mode(mode);
bfac5d94
LT
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';
e702496e 107 hashcpy((unsigned char*)new + newlen, (unsigned char *) buffer + len - 20);
bfac5d94
LT
108 newlen += 20;
109
110 used += len;
111 size -= len;
1d7f171c 112 buffer = (char *) buffer + len;
bfac5d94
LT
113 continue;
114 }
115
f220fb6b 116 newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
bfac5d94
LT
117 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;
1d7f171c 125 buffer = (char *) buffer + len;
bfac5d94
LT
126 }
127
8e440259 128 write_sha1_file(new, newlen, tree_type, result_sha1);
bfac5d94
LT
129 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
d98b46f8
LT
138 while (size) {
139 int len = 1+strlen(buffer);
140
1d7f171c 141 convert_binary_sha1((char *) buffer + len);
d98b46f8
LT
142
143 len += 20;
144 if (len > size)
145 die("corrupt tree object");
146 size -= len;
1d7f171c 147 buffer = (char *) buffer + len;
d98b46f8 148 }
bfac5d94
LT
149
150 write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
151}
152
153static unsigned long parse_oldstyle_date(const char *buf)
154{
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
165 };
166 /* We only ever did two timezones in the bad old format .. */
167 const char *timezones[] = {
3f053897 168 "PDT", "PST", "CEST", NULL
bfac5d94
LT
169 };
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);
bfac5d94
LT
227 return len + datelen;
228 }
229
93256315
LT
230 /*
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
bfac5d94 237 return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
d98b46f8
LT
238}
239
bfac5d94 240static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
d98b46f8 241{
812666c8 242 char *new = xmalloc(size + 100);
a44c9a5e 243 unsigned long newlen = 0;
a9486b02
PR
244
245 /* "tree <sha1>\n" */
bfac5d94
LT
246 memcpy(new + newlen, buffer, 46);
247 newlen += 46;
1d7f171c 248 buffer = (char *) buffer + 46;
bfac5d94
LT
249 size -= 46;
250
a9486b02 251 /* "parent <sha1>\n" */
bfac5d94
LT
252 while (!memcmp(buffer, "parent ", 7)) {
253 memcpy(new + newlen, buffer, 48);
254 newlen += 48;
1d7f171c 255 buffer = (char *) buffer + 48;
bfac5d94
LT
256 size -= 48;
257 }
258
a9486b02 259 /* "author xyz <xyz> date" */
bfac5d94 260 newlen += convert_date_line(new + newlen, &buffer, &size);
a9486b02 261 /* "committer xyz <xyz> date" */
bfac5d94
LT
262 newlen += convert_date_line(new + newlen, &buffer, &size);
263
a9486b02 264 /* Rest */
bfac5d94
LT
265 memcpy(new + newlen, buffer, size);
266 newlen += size;
267
8e440259
PE
268 write_sha1_file(new, newlen, commit_type, result_sha1);
269 free(new);
bfac5d94
LT
270}
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
4e81304a 277 if (memcmp(buffer, "tree ", 5))
79db12e8 278 die("Bad commit '%s'", (char*) buffer);
1d7f171c
FF
279 convert_ascii_sha1((char *) buffer + 5);
280 buffer = (char *) buffer + 46; /* "tree " + "hex sha1" + "\n" */
d98b46f8 281 while (!memcmp(buffer, "parent ", 7)) {
1d7f171c
FF
282 convert_ascii_sha1((char *) buffer + 7);
283 buffer = (char *) buffer + 48;
d98b46f8 284 }
bfac5d94 285 convert_date(orig_buffer, orig_size, result_sha1);
d98b46f8
LT
286}
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;
a44c9a5e 293 unsigned long size;
d98b46f8
LT
294
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
812666c8 301 buffer = xmalloc(size);
a44c9a5e 302 memcpy(buffer, data, size);
8e440259
PE
303
304 if (!strcmp(type, blob_type)) {
305 write_sha1_file(buffer, size, blob_type, entry->new_sha1);
306 } else if (!strcmp(type, tree_type))
a44c9a5e 307 convert_tree(buffer, size, entry->new_sha1);
8e440259 308 else if (!strcmp(type, commit_type))
a44c9a5e 309 convert_commit(buffer, size, entry->new_sha1);
d98b46f8
LT
310 else
311 die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
d98b46f8
LT
312 entry->converted = 1;
313 free(buffer);
4e81304a 314 free(data);
d98b46f8
LT
315 return entry;
316}
317
318int main(int argc, char **argv)
319{
320 unsigned char sha1[20];
321 struct entry *entry;
322
53228a5f
JH
323 setup_git_directory();
324
31fff305 325 if (argc != 2)
215a7ad1 326 usage("git-convert-objects <sha1>");
31fff305
DL
327 if (get_sha1(argv[1], sha1))
328 die("Not a valid object name %s", argv[1]);
d98b46f8
LT
329
330 entry = convert_entry(sha1);
331 printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));
332 return 0;
333}