git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC 01/16] Implement a remote helper for svn in C.

From
Florian Achleitner <florian.achleitner.2.6.31@gmail.com>
Date
Jul 26, 2012, 07:32 UTC
Message-ID
<1343287957-22040-2-git-send-email-florian.achleitner.2.6.31@gmail.com>
In-Reply-To
<1343287957-22040-1-git-send-email-florian.achleitner.2.6.31@gmail.com>

Enables basic fetching from subversion repositories. When processing Remote URLs starting with svn::, git invokes this remote-helper. It starts svnrdump to extract revisions from the subversion repository in the 'dump file format', and converts them to a git-fast-import stream using the functions of vcs-svn/.

Imported refs are created in a private namespace at refs/svn/<remote-name/master. The revision history is imported linearly (no branch detection) and completely, i.e. from revision 0 to HEAD.

Signed-off-by: Florian Achleitner <florian.achleitner.2.6.31@gmail.com>
---
 contrib/svn-fe/remote-svn.c |  219 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 219 insertions(+)
 create mode 100644 contrib/svn-fe/remote-svn.c
diff --git a/contrib/svn-fe/remote-svn.c b/contrib/svn-fe/remote-svn.c
new file mode 100644
index 0000000..29fc371
--- /dev/null
+++ b/contrib/svn-fe/remote-svn.c
@@ -0,0 +1,219 @@
+
+#include "cache.h"
+#include "remote.h"
+#include "strbuf.h"
+#include "url.h"
+#include "exec_cmd.h"
+#include "run-command.h"
+#include "svndump.h"
+#include "argv-array.h"
+
+static int debug;
+
+static inline void printd(const char *fmt, ...)
+{
+	if (debug) {
+		va_list vargs;
+		va_start(vargs, fmt);
+		fprintf(stderr, "rhsvn debug: ");
+		vfprintf(stderr, fmt, vargs);
+		fprintf(stderr, "\n");
+		va_end(vargs);
+	}
+}
+
+static const char *url;
+static const char *private_ref;
+static const char *remote_ref = "refs/heads/master";
+
+/*
+ * remote-helpers receive commands on their stdin. Command handler functions
+ * can return cmd_result, on fatal errors they die().
+ * NOT_HANDLED: the command was not handled by this function, name doesn't match.
+ * TERMINATE: Requests normal termination of the remote-helper.
+ */
+enum cmd_result { SUCCESS, NOT_HANDLED, TERMINATE };
+enum cmd_result cmd_capabilities(struct strbuf *line);
+enum cmd_result cmd_import(struct strbuf *line);
+enum cmd_result cmd_list(struct strbuf *line);
+enum cmd_result cmd_terminate(struct strbuf *line);
+
+typedef enum cmd_result (*input_command_handler)(struct strbuf *);
+
+const input_command_handler input_command_list[] = {
+		cmd_capabilities, cmd_import, cmd_list, cmd_terminate, NULL
+};
+
+enum cmd_result cmd_capabilities(struct strbuf *line)
+{
+	if (strcmp(line->buf, "capabilities"))
+		return NOT_HANDLED;
+
+	printf("import\n");
+	printf("refspec %s:%s\n\n", remote_ref, private_ref);
+	fflush(stdout);
+	return SUCCESS;
+}
+
+enum cmd_result cmd_import(struct strbuf *line)
+{
+	static int batch_active;
+	int code, report_fd;
+	char *back_pipe_env;
+	int dumpin_fd;
+	unsigned int startrev = 0;
+	struct argv_array svndump_argv = ARGV_ARRAY_INIT;
+	struct child_process svndump_proc;
+
+	/* terminate a current batch's fast-import stream */
+	if (line->len == 0 && batch_active) {
+		printf("done\n");
+		fflush(stdout);
+		batch_active = 0;
+		printd("import-batch finished.");
+		/*
+		 * should the remote helper terminate after a batch?
+		 * It seems that it should.
+		 */
+		return TERMINATE;
+	}
+	if (prefixcmp(line->buf, "import"))
+		return NOT_HANDLED;
+
+	/* import commands can be grouped together in a batch. Batches are ended by \n */
+	batch_active = 1;
+
+	/*
+	 * When the remote-helper is invoked by transport-helper.c it passes the
+	 * filename of this pipe in the env-var.
+	 */
+	back_pipe_env = getenv("GIT_REPORT_FIFO");
+	if (!back_pipe_env) {
+		die("Cannot get cat-blob-pipe from environment! GIT_REPORT_FIFO has to"
+				"be set by the caller.");
+	}
+
+	/*
+	 * Opening a fifo for reading usually blocks until a writer has opened it too.
+	 * Opening a fifo for writing usually blocks until a reader has opened it too.
+	 * Therefore, we open with RDWR on both sides to avoid deadlocks.
+	 * Especially if there's nothing to do and one pipe end is closed immediately.
+	 */
+	report_fd = open(back_pipe_env, O_RDWR);
+	if (report_fd < 0) {
+		die("Unable to open fast-import back-pipe! %s", strerror(errno));
+	}
+	printd("Opened fast-import back-pipe %s for reading.", back_pipe_env);
+
+	memset(&svndump_proc, 0, sizeof (struct child_process));
+	svndump_proc.out = -1;
+	argv_array_push(&svndump_argv, "svnrdump");
+	argv_array_push(&svndump_argv, "dump");
+	argv_array_push(&svndump_argv, url);
+	argv_array_pushf(&svndump_argv, "-r%u:HEAD", startrev);
+	svndump_proc.argv = svndump_argv.argv;
+
+	code = start_command(&svndump_proc);
+	if (code)
+		die("Unable to start %s, code %d", svndump_proc.argv[0], code);
+	dumpin_fd = svndump_proc.out;
+
+	svndump_init_fd(dumpin_fd, report_fd);
+	svndump_read(url, private_ref);
+	svndump_deinit();
+	svndump_reset();
+
+	close(dumpin_fd);
+	close(report_fd);
+	code = finish_command(&svndump_proc);
+	if (code)
+		warning("%s, returned %d", svndump_proc.argv[0], code);
+	argv_array_clear(&svndump_argv);
+
+	return SUCCESS;
+}
+
+enum cmd_result cmd_list(struct strbuf *line)
+{
+	if (strcmp(line->buf, "list"))
+		return NOT_HANDLED;
+
+	printf("? HEAD\n");
+	printf("? %s\n\n", remote_ref);
+	fflush(stdout);
+	return SUCCESS;
+}
+
+enum cmd_result cmd_terminate(struct strbuf *line)
+{
+	/* an empty line terminates the program, if not in a batch sequence */
+	if (line->len == 0)
+		return TERMINATE;
+	else
+		return NOT_HANDLED;
+}
+enum cmd_result do_command(struct strbuf *line)
+{
+	const input_command_handler *p = input_command_list;
+	enum cmd_result ret;
+	printd("command line '%s'", line->buf);
+	while(*p) {
+		ret = (*p)(line);
+		if (ret != NOT_HANDLED)
+			return ret;
+		p++;
+	}
+	warning("Unknown command '%s'\n", line->buf);
+	return ret;
+}
+
+int main(int argc, const char **argv)
+{
+	struct strbuf buf = STRBUF_INIT;
+	int nongit;
+	static struct remote *remote;
+
+	if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
+		debug = 1;
+
+	git_extract_argv0_path(argv[0]);
+	setup_git_directory_gently(&nongit);
+	if (argc < 2) {
+		usage("git-remote-svn <remote-name> [<url>]");
+		return 1;
+	}
+
+	remote = remote_get(argv[1]);
+	if (argc == 3)
+		url = argv[2];
+	else if (argc == 2)
+		url = remote->url[0];
+	else
+		warning("Excess arguments!");
+
+	end_url_with_slash(&buf, url);
+	url = strbuf_detach(&buf, NULL);
+
+	printd("remote-svn starting with url %s", url);
+
+	strbuf_init(&buf, 0);
+	strbuf_addf(&buf, "refs/svn/%s/master", remote->name);
+	private_ref = strbuf_detach(&buf, NULL);
+
+	while(1) {
+		if (strbuf_getline(&buf, stdin, '\n') == EOF) {
+			if (ferror(stdin))
+				die_errno("Error reading command stream");
+			else
+				die_errno("Unexpected end of command stream");
+		}
+		if (do_command(&buf) == TERMINATE)
+			break;
+		strbuf_reset(&buf);
+	}
+
+	strbuf_release(&buf);
+	free((void*)url);
+	free((void*)private_ref);
+	return 0;
+}
-- 
1.7.9.5
Previous: Florian AchleitnerNext: Florian Achleitner
Message 2 of 47 in “[RFC 00/16] GSOC remote-svn, rewritten patch series”
  1. Florian AchleitnerJul 26, 2012
  2. 01/16 Implement a remote helper for svn in C.Florian Achleitner, Jul 26, 2012
  3. 02/16 Integrate remote-svn into svn-fe/Makefile.Florian Achleitner, Jul 26, 2012
  4. 03/16 Add svndump_init_fd to allow reading dumps from arbitrary FDs.Florian Achleitner, Jul 26, 2012
  5. 04/16 Add cat-blob report fifo from fast-import to remote-helper.Florian Achleitner, Jul 26, 2012
  6. 05/16 remote-svn, vcs-svn: Enable fetching to private refs.Florian Achleitner, Jul 26, 2012
  7. 06/16 Add a symlink 'git-remote-svn' in base dir.Florian Achleitner, Jul 26, 2012
  8. 07/16 Allow reading svn dumps from files via file:// urls.Florian Achleitner, Jul 26, 2012
  9. 08/16 vcs-svn: add fast_export_note to create notesFlorian Achleitner, Jul 26, 2012
  10. 09/16 Create a note for every imported commit containing svn metadata.Florian Achleitner, Jul 26, 2012
  11. 10/16 When debug==1, start fast-import with "--stats" instead of "--quiet".Florian Achleitner, Jul 26, 2012
  12. 11/16 Add explanatory comment for transport-helpers refs mapping.Florian Achleitner, Jul 26, 2012
  13. 12/16 remote-svn: add incremental import.Florian Achleitner, Jul 26, 2012
  14. 13/16 Add a svnrdump-simulator replaying a dump file for testing.Florian Achleitner, Jul 26, 2012
  15. 14/16 transport-helper: add import|export-marks to fast-import command line.Florian Achleitner, Jul 26, 2012
  16. 15/16 remote-svn: add marks-file regeneration.Florian Achleitner, Jul 26, 2012
  17. 16/16 Add a test script for remote-svn.Florian Achleitner, Jul 26, 2012
  18. Florian AchleitnerJul 26, 2012
  19. Jonathan NiederJul 28, 2012
  20. Jonathan NiederJul 26, 2012
  21. Florian AchleitnerJul 26, 2012
  22. Jonathan NiederJul 26, 2012
  23. Florian AchleitnerJul 26, 2012
  24. 00/16 GSOC remote-svn, rewritten patch seriesFlorian Achleitner, Jul 30, 2012
  25. 01/16 Implement a remote helper for svn in C.Florian Achleitner, Jul 30, 2012
  26. 02/16 Integrate remote-svn into svn-fe/Makefile.Florian Achleitner, Jul 30, 2012
  27. 03/16 Add svndump_init_fd to allow reading dumps from arbitrary FDs.Florian Achleitner, Jul 30, 2012
  28. 04/16 Add cat-blob report fifo from fast-import to remote-helper.Florian Achleitner, Jul 30, 2012
  29. 05/16 remote-svn, vcs-svn: Enable fetching to private refs.Florian Achleitner, Jul 30, 2012
  30. 06/16 Add a symlink 'git-remote-svn' in base dir.Florian Achleitner, Jul 30, 2012
  31. 07/16 Allow reading svn dumps from files via file:// urls.Florian Achleitner, Jul 30, 2012
  32. 08/16 vcs-svn: add fast_export_note to create notesFlorian Achleitner, Jul 30, 2012
  33. 09/16 Create a note for every imported commit containing svn metadata.Florian Achleitner, Jul 30, 2012
  34. 10/16 When debug==1, start fast-import with "--stats" instead of "--quiet".Florian Achleitner, Jul 30, 2012
  35. 11/16 Add explanatory comment for transport-helpers refs mapping.Florian Achleitner, Jul 30, 2012
  36. 12/16 remote-svn: add incremental import.Florian Achleitner, Jul 30, 2012
  37. 13/16 Add a svnrdump-simulator replaying a dump file for testing.Florian Achleitner, Jul 30, 2012
  38. 14/16 transport-helper: add import|export-marks to fast-import command line.Florian Achleitner, Jul 30, 2012
  39. 15/16 remote-svn: add marks-file regeneration.Florian Achleitner, Jul 30, 2012
  40. 16/16 Add a test script for remote-svn.Florian Achleitner, Jul 30, 2012
  41. Jonathan NiederJul 30, 2012
  42. Junio C HamanoJul 30, 2012
  43. Junio C HamanoJul 30, 2012
  44. Jonathan NiederJul 30, 2012
  45. Florian AchleitnerJul 30, 2012
  46. Junio C HamanoJul 30, 2012
  47. Florian AchleitnerJul 31, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.