remote-testsvn.c 8.22 KB
Newer Older
1
#include "cache.h"
2
#include "refs.h"
3
#include "remote.h"
4
#include "object-store.h"
5 6
#include "strbuf.h"
#include "url.h"
7
#include "exec-cmd.h"
8 9 10 11 12 13
#include "run-command.h"
#include "vcs-svn/svndump.h"
#include "notes.h"
#include "argv-array.h"

static const char *url;
14
static int dump_from_file;
15 16
static const char *private_ref;
static const char *remote_ref = "refs/heads/master";
17 18
static const char *marksfilename, *notes_ref;
struct rev_note { unsigned int rev_nr; };
19 20 21 22 23 24 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

static int cmd_capabilities(const char *line);
static int cmd_import(const char *line);
static int cmd_list(const char *line);

typedef int (*input_command_handler)(const char *);
struct input_command_entry {
	const char *name;
	input_command_handler fn;
	unsigned char batchable;	/* whether the command starts or is part of a batch */
};

static const struct input_command_entry input_command_list[] = {
	{ "capabilities", cmd_capabilities, 0 },
	{ "import", cmd_import, 1 },
	{ "list", cmd_list, 0 },
	{ NULL, NULL }
};

static int cmd_capabilities(const char *line)
{
	printf("import\n");
	printf("bidi-import\n");
	printf("refspec %s:%s\n\n", remote_ref, private_ref);
	fflush(stdout);
	return 0;
}

static void terminate_batch(void)
{
	/* terminate a current batch's fast-import stream */
	printf("done\n");
	fflush(stdout);
}

54
/* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */
55
static char *read_ref_note(const struct object_id *oid)
56
{
57
	const struct object_id *note_oid;
58 59 60 61 62
	char *msg = NULL;
	unsigned long msglen;
	enum object_type type;

	init_notes(NULL, notes_ref, NULL, 0);
63
	if (!(note_oid = get_note(NULL, oid)))
64
		return NULL;	/* note tree not found */
65
	if (!(msg = read_object_file(note_oid, &type, &msglen)))
66 67 68 69
		error("Empty notes tree. %s", notes_ref);
	else if (!msglen || type != OBJ_BLOB) {
		error("Note contains unusable content. "
			"Is something else using this notes tree? %s", notes_ref);
70
		FREE_AND_NULL(msg);
71 72 73 74 75 76 77 78 79 80 81
	}
	free_notes(NULL);
	return msg;
}

static int parse_rev_note(const char *msg, struct rev_note *res)
{
	const char *key, *value, *end;
	size_t len;

	while (*msg) {
82 83
		end = strchrnul(msg, '\n');
		len = end - msg;
84 85

		key = "Revision-number: ";
86
		if (starts_with(msg, key)) {
87 88 89 90 91 92 93
			long i;
			char *end;
			value = msg + strlen(key);
			i = strtol(value, &end, 0);
			if (end == value || i < 0 || i > UINT32_MAX)
				return -1;
			res->rev_nr = i;
94
			return 0;
95 96 97
		}
		msg += len + 1;
	}
98 99
	/* didn't find it */
	return -1;
100 101
}

102 103
static int note2mark_cb(const struct object_id *object_oid,
		const struct object_id *note_oid, char *note_path,
104 105 106 107 108 109 110 111
		void *cb_data)
{
	FILE *file = (FILE *)cb_data;
	char *msg;
	unsigned long msglen;
	enum object_type type;
	struct rev_note note;

112
	if (!(msg = read_object_file(note_oid, &type, &msglen)) ||
113 114 115 116 117 118
			!msglen || type != OBJ_BLOB) {
		free(msg);
		return 1;
	}
	if (parse_rev_note(msg, &note))
		return 2;
119
	if (fprintf(file, ":%d %s\n", note.rev_nr, oid_to_hex(object_oid)) < 1)
120 121 122 123 124 125 126
		return 3;
	return 0;
}

static void regenerate_marks(void)
{
	int ret;
Duy Nguyen's avatar
Duy Nguyen committed
127
	FILE *marksfile = xfopen(marksfilename, "w+");
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

	ret = for_each_note(NULL, 0, note2mark_cb, marksfile);
	if (ret)
		die("Regeneration of marks failed, returned %d.", ret);
	fclose(marksfile);
}

static void check_or_regenerate_marks(int latestrev)
{
	FILE *marksfile;
	struct strbuf sb = STRBUF_INIT;
	struct strbuf line = STRBUF_INIT;
	int found = 0;

	if (latestrev < 1)
		return;

	init_notes(NULL, notes_ref, NULL, 0);
	marksfile = fopen(marksfilename, "r");
	if (!marksfile) {
		regenerate_marks();
Duy Nguyen's avatar
Duy Nguyen committed
149
		marksfile = xfopen(marksfilename, "r");
150 151 152
		fclose(marksfile);
	} else {
		strbuf_addf(&sb, ":%d ", latestrev);
153
		while (strbuf_getline_lf(&line, marksfile) != EOF) {
154
			if (starts_with(line.buf, sb.buf)) {
155 156 157 158 159 160 161 162 163 164 165 166 167
				found++;
				break;
			}
		}
		fclose(marksfile);
		if (!found)
			regenerate_marks();
	}
	free_notes(NULL);
	strbuf_release(&sb);
	strbuf_release(&line);
}

168 169 170 171
static int cmd_import(const char *line)
{
	int code;
	int dumpin_fd;
172
	char *note_msg;
173
	struct object_id head_oid;
174
	unsigned int startrev;
175
	struct child_process svndump_proc = CHILD_PROCESS_INIT;
176
	const char *command = "svnrdump";
177

178
	if (read_ref(private_ref, &head_oid))
179 180
		startrev = 0;
	else {
181
		note_msg = read_ref_note(&head_oid);
182 183 184 185 186 187 188 189 190 191 192
		if(note_msg == NULL) {
			warning("No note found for %s.", private_ref);
			startrev = 0;
		} else {
			struct rev_note note = { 0 };
			if (parse_rev_note(note_msg, &note))
				die("Revision number couldn't be parsed from note.");
			startrev = note.rev_nr + 1;
			free(note_msg);
		}
	}
193
	check_or_regenerate_marks(startrev - 1);
194

195 196 197 198 199 200
	if (dump_from_file) {
		dumpin_fd = open(url, O_RDONLY);
		if(dumpin_fd < 0)
			die_errno("Couldn't open svn dump file %s.", url);
	} else {
		svndump_proc.out = -1;
201 202 203 204
		argv_array_push(&svndump_proc.args, command);
		argv_array_push(&svndump_proc.args, "dump");
		argv_array_push(&svndump_proc.args, url);
		argv_array_pushf(&svndump_proc.args, "-r%u:HEAD", startrev);
205 206 207

		code = start_command(&svndump_proc);
		if (code)
208
			die("Unable to start %s, code %d", command, code);
209 210
		dumpin_fd = svndump_proc.out;
	}
211 212 213 214
	/* setup marks file import/export */
	printf("feature import-marks-if-exists=%s\n"
			"feature export-marks=%s\n", marksfilename, marksfilename);

215
	svndump_init_fd(dumpin_fd, STDIN_FILENO);
216
	svndump_read(url, private_ref, notes_ref);
217 218 219 220
	svndump_deinit();
	svndump_reset();

	close(dumpin_fd);
221 222 223
	if (!dump_from_file) {
		code = finish_command(&svndump_proc);
		if (code)
224
			warning("%s, returned %d", command, code);
225
	}
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

	return 0;
}

static int cmd_list(const char *line)
{
	printf("? %s\n\n", remote_ref);
	fflush(stdout);
	return 0;
}

static int do_command(struct strbuf *line)
{
	const struct input_command_entry *p = input_command_list;
	static struct string_list batchlines = STRING_LIST_INIT_DUP;
	static const struct input_command_entry *batch_cmd;
	/*
	 * commands can be grouped together in a batch.
	 * Batches are ended by \n. If no batch is active the program ends.
	 * During a batch all lines are buffered and passed to the handler function
	 * when the batch is terminated.
	 */
	if (line->len == 0) {
		if (batch_cmd) {
			struct string_list_item *item;
			for_each_string_list_item(item, &batchlines)
				batch_cmd->fn(item->string);
			terminate_batch();
			batch_cmd = NULL;
			string_list_clear(&batchlines, 0);
			return 0;	/* end of the batch, continue reading other commands. */
		}
		return 1;	/* end of command stream, quit */
	}
	if (batch_cmd) {
261
		if (!starts_with(batch_cmd->name, line->buf))
262 263 264 265 266 267 268
			die("Active %s batch interrupted by %s", batch_cmd->name, line->buf);
		/* buffer batch lines */
		string_list_append(&batchlines, line->buf);
		return 0;
	}

	for (p = input_command_list; p->name; p++) {
269
		if (starts_with(line->buf, p->name) && (strlen(p->name) == line->len ||
270 271 272 273 274 275 276 277 278 279 280 281 282
				line->buf[strlen(p->name)] == ' ')) {
			if (p->batchable) {
				batch_cmd = p;
				string_list_append(&batchlines, line->buf);
				return 0;
			}
			return p->fn(line->buf);
		}
	}
	die("Unknown command '%s'\n", line->buf);
	return 0;
}

283
int cmd_main(int argc, const char **argv)
284 285
{
	struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
286 287
			private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT,
			notes_ref_sb = STRBUF_INIT;
288 289 290 291 292 293 294 295 296 297 298 299
	static struct remote *remote;
	const char *url_in;

	setup_git_directory();
	if (argc < 2 || argc > 3) {
		usage("git-remote-svn <remote-name> [<url>]");
		return 1;
	}

	remote = remote_get(argv[1]);
	url_in = (argc == 3) ? argv[2] : remote->url[0];

300
	if (starts_with(url_in, "file://")) {
301 302 303 304 305 306 307
		dump_from_file = 1;
		url = url_decode(url_in + sizeof("file://")-1);
	} else {
		dump_from_file = 0;
		end_url_with_slash(&url_sb, url_in);
		url = url_sb.buf;
	}
308 309 310 311

	strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
	private_ref = private_ref_sb.buf;

312 313 314
	strbuf_addf(&notes_ref_sb, "refs/notes/%s/revs", remote->name);
	notes_ref = notes_ref_sb.buf;

315 316 317 318
	strbuf_addf(&marksfilename_sb, "%s/info/fast-import/remote-svn/%s.marks",
		get_git_dir(), remote->name);
	marksfilename = marksfilename_sb.buf;

319
	while (1) {
320
		if (strbuf_getline_lf(&buf, stdin) == EOF) {
321 322 323 324 325 326 327 328 329 330 331 332 333
			if (ferror(stdin))
				die("Error reading command stream");
			else
				die("Unexpected end of command stream");
		}
		if (do_command(&buf))
			break;
		strbuf_reset(&buf);
	}

	strbuf_release(&buf);
	strbuf_release(&url_sb);
	strbuf_release(&private_ref_sb);
334
	strbuf_release(&notes_ref_sb);
335
	strbuf_release(&marksfilename_sb);
336 337
	return 0;
}