6 #include "run-command.h"
7 #include "vcs-svn/svndump.h"
9 #include "argv-array.h"
11 static const char *url;
12 static int dump_from_file;
13 static const char *private_ref;
14 static const char *remote_ref = "refs/heads/master";
15 static const char *marksfilename;
17 static int cmd_capabilities(const char *line);
18 static int cmd_import(const char *line);
19 static int cmd_list(const char *line);
21 typedef int (*input_command_handler)(const char *);
22 struct input_command_entry {
24 input_command_handler fn;
25 unsigned char batchable; /* whether the command starts or is part of a batch */
28 static const struct input_command_entry input_command_list[] = {
29 { "capabilities", cmd_capabilities, 0 },
30 { "import", cmd_import, 1 },
31 { "list", cmd_list, 0 },
35 static int cmd_capabilities(const char *line)
38 printf("bidi-import\n");
39 printf("refspec %s:%s\n\n", remote_ref, private_ref);
44 static void terminate_batch(void)
46 /* terminate a current batch's fast-import stream */
51 static int cmd_import(const char *line)
55 unsigned int startrev = 0;
56 struct argv_array svndump_argv = ARGV_ARRAY_INIT;
57 struct child_process svndump_proc;
60 dumpin_fd = open(url, O_RDONLY);
62 die_errno("Couldn't open svn dump file %s.", url);
64 memset(&svndump_proc, 0, sizeof(struct child_process));
65 svndump_proc.out = -1;
66 argv_array_push(&svndump_argv, "svnrdump");
67 argv_array_push(&svndump_argv, "dump");
68 argv_array_push(&svndump_argv, url);
69 argv_array_pushf(&svndump_argv, "-r%u:HEAD", startrev);
70 svndump_proc.argv = svndump_argv.argv;
72 code = start_command(&svndump_proc);
74 die("Unable to start %s, code %d", svndump_proc.argv[0], code);
75 dumpin_fd = svndump_proc.out;
77 /* setup marks file import/export */
78 printf("feature import-marks-if-exists=%s\n"
79 "feature export-marks=%s\n", marksfilename, marksfilename);
81 svndump_init_fd(dumpin_fd, STDIN_FILENO);
82 svndump_read(url, private_ref);
87 if (!dump_from_file) {
88 code = finish_command(&svndump_proc);
90 warning("%s, returned %d", svndump_proc.argv[0], code);
91 argv_array_clear(&svndump_argv);
97 static int cmd_list(const char *line)
99 printf("? %s\n\n", remote_ref);
104 static int do_command(struct strbuf *line)
106 const struct input_command_entry *p = input_command_list;
107 static struct string_list batchlines = STRING_LIST_INIT_DUP;
108 static const struct input_command_entry *batch_cmd;
110 * commands can be grouped together in a batch.
111 * Batches are ended by \n. If no batch is active the program ends.
112 * During a batch all lines are buffered and passed to the handler function
113 * when the batch is terminated.
115 if (line->len == 0) {
117 struct string_list_item *item;
118 for_each_string_list_item(item, &batchlines)
119 batch_cmd->fn(item->string);
122 string_list_clear(&batchlines, 0);
123 return 0; /* end of the batch, continue reading other commands. */
125 return 1; /* end of command stream, quit */
128 if (prefixcmp(batch_cmd->name, line->buf))
129 die("Active %s batch interrupted by %s", batch_cmd->name, line->buf);
130 /* buffer batch lines */
131 string_list_append(&batchlines, line->buf);
135 for (p = input_command_list; p->name; p++) {
136 if (!prefixcmp(line->buf, p->name) && (strlen(p->name) == line->len ||
137 line->buf[strlen(p->name)] == ' ')) {
140 string_list_append(&batchlines, line->buf);
143 return p->fn(line->buf);
146 die("Unknown command '%s'\n", line->buf);
150 int main(int argc, const char **argv)
152 struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
153 private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT;
154 static struct remote *remote;
157 git_extract_argv0_path(argv[0]);
158 setup_git_directory();
159 if (argc < 2 || argc > 3) {
160 usage("git-remote-svn <remote-name> [<url>]");
164 remote = remote_get(argv[1]);
165 url_in = (argc == 3) ? argv[2] : remote->url[0];
167 if (!prefixcmp(url_in, "file://")) {
169 url = url_decode(url_in + sizeof("file://")-1);
172 end_url_with_slash(&url_sb, url_in);
176 strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
177 private_ref = private_ref_sb.buf;
179 strbuf_addf(&marksfilename_sb, "%s/info/fast-import/remote-svn/%s.marks",
180 get_git_dir(), remote->name);
181 marksfilename = marksfilename_sb.buf;
184 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
186 die("Error reading command stream");
188 die("Unexpected end of command stream");
190 if (do_command(&buf))
195 strbuf_release(&buf);
196 strbuf_release(&url_sb);
197 strbuf_release(&private_ref_sb);
198 strbuf_release(&marksfilename_sb);