convert-objects.c 7.4 KB
Newer Older
1 2
#define _XOPEN_SOURCE 500 /* glibc2 and AIX 5.3L need this */
#define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
3
#include <time.h>
4
#include "cache.h"
5 6 7
#include "blob.h"
#include "commit.h"
#include "tree.h"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

struct entry {
	unsigned char old_sha1[20];
	unsigned char new_sha1[20];
	int converted;
};

#define MAXOBJECTS (1000000)

static struct entry *convert[MAXOBJECTS];
static int nr_convert;

static struct entry * convert_entry(unsigned char *sha1);

static struct entry *insert_new(unsigned char *sha1, int pos)
{
24
	struct entry *new = xcalloc(1, sizeof(struct entry));
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	memcpy(new->old_sha1, sha1, 20);
	memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
	convert[pos] = new;
	nr_convert++;
	if (nr_convert == MAXOBJECTS)
		die("you're kidding me - hit maximum object limit");
	return new;
}

static struct entry *lookup_entry(unsigned char *sha1)
{
	int low = 0, high = nr_convert;

	while (low < high) {
		int next = (low + high) / 2;
		struct entry *n = convert[next];
		int cmp = memcmp(sha1, n->old_sha1, 20);
		if (!cmp)
			return n;
		if (cmp < 0) {
			high = next;
			continue;
		}
		low = next+1;
	}
	return insert_new(sha1, low);
}

static void convert_binary_sha1(void *buffer)
{
	struct entry *entry = convert_entry(buffer);
	memcpy(buffer, entry->new_sha1, 20);
}

static void convert_ascii_sha1(void *buffer)
{
	unsigned char sha1[20];
	struct entry *entry;

	if (get_sha1_hex(buffer, sha1))
65
		die("expected sha1, got '%s'", (char*) buffer);
66 67 68 69
	entry = convert_entry(sha1);
	memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
}

70 71 72 73 74 75 76 77 78 79
static unsigned int convert_mode(unsigned int mode)
{
	unsigned int newmode;

	newmode = mode & S_IFMT;
	if (S_ISREG(mode))
		newmode |= (mode & 0100) ? 0755 : 0644;
	return newmode;
}

80
static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
81
{
82
	char *new = xmalloc(size);
83
	unsigned long newlen = 0;
84 85 86 87 88 89 90 91 92 93 94 95
	unsigned long used;

	used = 0;
	while (size) {
		int len = 21 + strlen(buffer);
		char *path = strchr(buffer, ' ');
		unsigned char *sha1;
		unsigned int mode;
		char *slash, *origpath;

		if (!path || sscanf(buffer, "%o", &mode) != 1)
			die("bad tree conversion");
96
		mode = convert_mode(mode);
97 98 99 100 101 102 103 104 105
		path++;
		if (memcmp(path, base, baselen))
			break;
		origpath = path;
		path += baselen;
		slash = strchr(path, '/');
		if (!slash) {
			newlen += sprintf(new + newlen, "%o %s", mode, path);
			new[newlen++] = '\0';
106
			memcpy(new + newlen, (char *) buffer + len - 20, 20);
107 108 109 110
			newlen += 20;

			used += len;
			size -= len;
111
			buffer = (char *) buffer + len;
112 113 114
			continue;
		}

115
		newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
116 117 118 119 120 121 122 123
		new[newlen++] = 0;
		sha1 = (unsigned char *)(new + newlen);
		newlen += 20;

		len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1);

		used += len;
		size -= len;
124
		buffer = (char *) buffer + len;
125 126
	}

127
	write_sha1_file(new, newlen, tree_type, result_sha1);
128 129 130 131 132 133 134 135 136
	free(new);
	return used;
}

static void convert_tree(void *buffer, unsigned long size, unsigned char *result_sha1)
{
	void *orig_buffer = buffer;
	unsigned long orig_size = size;

137 138 139
	while (size) {
		int len = 1+strlen(buffer);

140
		convert_binary_sha1((char *) buffer + len);
141 142 143 144 145

		len += 20;
		if (len > size)
			die("corrupt tree object");
		size -= len;
146
		buffer = (char *) buffer + len;
147
	}
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

	write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
}

static unsigned long parse_oldstyle_date(const char *buf)
{
	char c, *p;
	char buffer[100];
	struct tm tm;
	const char *formats[] = {
		"%c",
		"%a %b %d %T",
		"%Z",
		"%Y",
		" %Y",
		NULL
	};
	/* We only ever did two timezones in the bad old format .. */
	const char *timezones[] = {
167
		"PDT", "PST", "CEST", NULL
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	};
	const char **fmt = formats;

	p = buffer;
	while (isspace(c = *buf))
		buf++;
	while ((c = *buf++) != '\n')
		*p++ = c;
	*p++ = 0;
	buf = buffer;
	memset(&tm, 0, sizeof(tm));
	do {
		const char *next = strptime(buf, *fmt, &tm);
		if (next) {
			if (!*next)
				return mktime(&tm);
			buf = next;
		} else {
			const char **p = timezones;
			while (isspace(*buf))
				buf++;
			while (*p) {
				if (!memcmp(buf, *p, strlen(*p))) {
					buf += strlen(*p);
					break;
				}
				p++;
			}
		}
		fmt++;
	} while (*buf && *fmt);
	printf("left: %s\n", buf);
	return mktime(&tm);				
}

static int convert_date_line(char *dst, void **buf, unsigned long *sp)
{
	unsigned long size = *sp;
	char *line = *buf;
	char *next = strchr(line, '\n');
	char *date = strchr(line, '>');
	int len;

	if (!next || !date)
		die("missing or bad author/committer line %s", line);
	next++; date += 2;

	*buf = next;
	*sp = size - (next - line);

	len = date - line;
	memcpy(dst, line, len);
	dst += len;

	/* Is it already in new format? */
	if (isdigit(*date)) {
		int datelen = next - date;
		memcpy(dst, date, datelen);
		return len + datelen;
	}

229 230 231 232 233 234 235
	/*
	 * Hacky hacky: one of the sparse old-style commits does not have
	 * any date at all, but we can fake it by using the committer date.
	 */
	if (*date == '\n' && strchr(next, '>'))
		date = strchr(next, '>')+2;

236
	return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
237 238
}

239
static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
240
{
241
	char *new = xmalloc(size + 100);
242
	unsigned long newlen = 0;
243
	
244 245 246
	// "tree <sha1>\n"
	memcpy(new + newlen, buffer, 46);
	newlen += 46;
247
	buffer = (char *) buffer + 46;
248 249 250 251 252 253
	size -= 46;

	// "parent <sha1>\n"
	while (!memcmp(buffer, "parent ", 7)) {
		memcpy(new + newlen, buffer, 48);
		newlen += 48;
254
		buffer = (char *) buffer + 48;
255 256 257 258 259 260 261 262 263 264 265 266
		size -= 48;
	}

	// "author xyz <xyz> date"
	newlen += convert_date_line(new + newlen, &buffer, &size);
	// "committer xyz <xyz> date"
	newlen += convert_date_line(new + newlen, &buffer, &size);

	// Rest
	memcpy(new + newlen, buffer, size);
	newlen += size;

267 268
	write_sha1_file(new, newlen, commit_type, result_sha1);
	free(new);
269 270 271 272 273 274 275
}

static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1)
{
	void *orig_buffer = buffer;
	unsigned long orig_size = size;

276
	if (memcmp(buffer, "tree ", 5))
277
		die("Bad commit '%s'", (char*) buffer);
278 279
	convert_ascii_sha1((char *) buffer + 5);
	buffer = (char *) buffer + 46;    /* "tree " + "hex sha1" + "\n" */
280
	while (!memcmp(buffer, "parent ", 7)) {
281 282
		convert_ascii_sha1((char *) buffer + 7);
		buffer = (char *) buffer + 48;
283
	}
284
	convert_date(orig_buffer, orig_size, result_sha1);
285 286 287 288 289 290 291
}

static struct entry * convert_entry(unsigned char *sha1)
{
	struct entry *entry = lookup_entry(sha1);
	char type[20];
	void *buffer, *data;
292
	unsigned long size;
293 294 295 296 297 298 299

	if (entry->converted)
		return entry;
	data = read_sha1_file(sha1, type, &size);
	if (!data)
		die("unable to read object %s", sha1_to_hex(sha1));

300
	buffer = xmalloc(size);
301
	memcpy(buffer, data, size);
302 303 304 305

	if (!strcmp(type, blob_type)) {
		write_sha1_file(buffer, size, blob_type, entry->new_sha1);
	} 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 310 311 312
	else
		die("unknown object type '%s' in %s", type, sha1_to_hex(sha1));
	entry->converted = 1;
	free(buffer);
313
	free(data);
314 315 316 317 318 319 320 321
	return entry;
}

int main(int argc, char **argv)
{
	unsigned char sha1[20];
	struct entry *entry;

322 323
	setup_git_directory();

324
	if (argc != 2)
J
Junio C Hamano 已提交
325
		usage("git-convert-objects <sha1>");
326 327
	if (get_sha1(argv[1], sha1))
		die("Not a valid object name %s", argv[1]);
328 329 330 331 332

	entry = convert_entry(sha1);
	printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));
	return 0;
}