6 #include "run-command.h"
7 #include "vcs-svn/svndump.h"
9 #include "argv-array.h"
11 static const char *url;
12 static const char *private_ref;
13 static const char *remote_ref = "refs/heads/master";
15 static int cmd_capabilities(const char *line);
16 static int cmd_import(const char *line);
17 static int cmd_list(const char *line);
19 typedef int (*input_command_handler)(const char *);
20 struct input_command_entry {
22 input_command_handler fn;
23 unsigned char batchable; /* whether the command starts or is part of a batch */
26 static const struct input_command_entry input_command_list[] = {
27 { "capabilities", cmd_capabilities, 0 },
28 { "import", cmd_import, 1 },
29 { "list", cmd_list, 0 },
33 static int cmd_capabilities(const char *line)
36 printf("bidi-import\n");
37 printf("refspec %s:%s\n\n", remote_ref, private_ref);
42 static void terminate_batch(void)
44 /* terminate a current batch's fast-import stream */
49 static int cmd_import(const char *line)
53 unsigned int startrev = 0;
54 struct argv_array svndump_argv = ARGV_ARRAY_INIT;
55 struct child_process svndump_proc;
57 memset(&svndump_proc, 0, sizeof(struct child_process));
58 svndump_proc.out = -1;
59 argv_array_push(&svndump_argv, "svnrdump");
60 argv_array_push(&svndump_argv, "dump");
61 argv_array_push(&svndump_argv, url);
62 argv_array_pushf(&svndump_argv, "-r%u:HEAD", startrev);
63 svndump_proc.argv = svndump_argv.argv;
65 code = start_command(&svndump_proc);
67 die("Unable to start %s, code %d", svndump_proc.argv[0], code);
68 dumpin_fd = svndump_proc.out;
70 svndump_init_fd(dumpin_fd, STDIN_FILENO);
71 svndump_read(url, private_ref);
76 code = finish_command(&svndump_proc);
78 warning("%s, returned %d", svndump_proc.argv[0], code);
79 argv_array_clear(&svndump_argv);
84 static int cmd_list(const char *line)
86 printf("? %s\n\n", remote_ref);
91 static int do_command(struct strbuf *line)
93 const struct input_command_entry *p = input_command_list;
94 static struct string_list batchlines = STRING_LIST_INIT_DUP;
95 static const struct input_command_entry *batch_cmd;
97 * commands can be grouped together in a batch.
98 * Batches are ended by \n. If no batch is active the program ends.
99 * During a batch all lines are buffered and passed to the handler function
100 * when the batch is terminated.
102 if (line->len == 0) {
104 struct string_list_item *item;
105 for_each_string_list_item(item, &batchlines)
106 batch_cmd->fn(item->string);
109 string_list_clear(&batchlines, 0);
110 return 0; /* end of the batch, continue reading other commands. */
112 return 1; /* end of command stream, quit */
115 if (prefixcmp(batch_cmd->name, line->buf))
116 die("Active %s batch interrupted by %s", batch_cmd->name, line->buf);
117 /* buffer batch lines */
118 string_list_append(&batchlines, line->buf);
122 for (p = input_command_list; p->name; p++) {
123 if (!prefixcmp(line->buf, p->name) && (strlen(p->name) == line->len ||
124 line->buf[strlen(p->name)] == ' ')) {
127 string_list_append(&batchlines, line->buf);
130 return p->fn(line->buf);
133 die("Unknown command '%s'\n", line->buf);
137 int main(int argc, const char **argv)
139 struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
140 private_ref_sb = STRBUF_INIT;
141 static struct remote *remote;
144 git_extract_argv0_path(argv[0]);
145 setup_git_directory();
146 if (argc < 2 || argc > 3) {
147 usage("git-remote-svn <remote-name> [<url>]");
151 remote = remote_get(argv[1]);
152 url_in = (argc == 3) ? argv[2] : remote->url[0];
154 end_url_with_slash(&url_sb, url_in);
157 strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
158 private_ref = private_ref_sb.buf;
161 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
163 die("Error reading command stream");
165 die("Unexpected end of command stream");
167 if (do_command(&buf))
172 strbuf_release(&buf);
173 strbuf_release(&url_sb);
174 strbuf_release(&private_ref_sb);