remote-svn, vcs-svn: Enable fetching to private refs
[git/git.git] / remote-testsvn.c
CommitLineData
68f64ff8
FA
1#include "cache.h"
2#include "remote.h"
3#include "strbuf.h"
4#include "url.h"
5#include "exec_cmd.h"
6#include "run-command.h"
7#include "vcs-svn/svndump.h"
8#include "notes.h"
9#include "argv-array.h"
10
11static const char *url;
12static const char *private_ref;
13static const char *remote_ref = "refs/heads/master";
14
15static int cmd_capabilities(const char *line);
16static int cmd_import(const char *line);
17static int cmd_list(const char *line);
18
19typedef int (*input_command_handler)(const char *);
20struct input_command_entry {
21 const char *name;
22 input_command_handler fn;
23 unsigned char batchable; /* whether the command starts or is part of a batch */
24};
25
26static const struct input_command_entry input_command_list[] = {
27 { "capabilities", cmd_capabilities, 0 },
28 { "import", cmd_import, 1 },
29 { "list", cmd_list, 0 },
30 { NULL, NULL }
31};
32
33static int cmd_capabilities(const char *line)
34{
35 printf("import\n");
36 printf("bidi-import\n");
37 printf("refspec %s:%s\n\n", remote_ref, private_ref);
38 fflush(stdout);
39 return 0;
40}
41
42static void terminate_batch(void)
43{
44 /* terminate a current batch's fast-import stream */
45 printf("done\n");
46 fflush(stdout);
47}
48
49static int cmd_import(const char *line)
50{
51 int code;
52 int dumpin_fd;
53 unsigned int startrev = 0;
54 struct argv_array svndump_argv = ARGV_ARRAY_INIT;
55 struct child_process svndump_proc;
56
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;
64
65 code = start_command(&svndump_proc);
66 if (code)
67 die("Unable to start %s, code %d", svndump_proc.argv[0], code);
68 dumpin_fd = svndump_proc.out;
69
70 svndump_init_fd(dumpin_fd, STDIN_FILENO);
71 svndump_read(url, private_ref);
72 svndump_deinit();
73 svndump_reset();
74
75 close(dumpin_fd);
76 code = finish_command(&svndump_proc);
77 if (code)
78 warning("%s, returned %d", svndump_proc.argv[0], code);
79 argv_array_clear(&svndump_argv);
80
81 return 0;
82}
83
84static int cmd_list(const char *line)
85{
86 printf("? %s\n\n", remote_ref);
87 fflush(stdout);
88 return 0;
89}
90
91static int do_command(struct strbuf *line)
92{
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;
96 /*
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.
101 */
102 if (line->len == 0) {
103 if (batch_cmd) {
104 struct string_list_item *item;
105 for_each_string_list_item(item, &batchlines)
106 batch_cmd->fn(item->string);
107 terminate_batch();
108 batch_cmd = NULL;
109 string_list_clear(&batchlines, 0);
110 return 0; /* end of the batch, continue reading other commands. */
111 }
112 return 1; /* end of command stream, quit */
113 }
114 if (batch_cmd) {
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);
119 return 0;
120 }
121
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)] == ' ')) {
125 if (p->batchable) {
126 batch_cmd = p;
127 string_list_append(&batchlines, line->buf);
128 return 0;
129 }
130 return p->fn(line->buf);
131 }
132 }
133 die("Unknown command '%s'\n", line->buf);
134 return 0;
135}
136
137int main(int argc, const char **argv)
138{
139 struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
140 private_ref_sb = STRBUF_INIT;
141 static struct remote *remote;
142 const char *url_in;
143
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>]");
148 return 1;
149 }
150
151 remote = remote_get(argv[1]);
152 url_in = (argc == 3) ? argv[2] : remote->url[0];
153
154 end_url_with_slash(&url_sb, url_in);
155 url = url_sb.buf;
156
157 strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
158 private_ref = private_ref_sb.buf;
159
160 while (1) {
161 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
162 if (ferror(stdin))
163 die("Error reading command stream");
164 else
165 die("Unexpected end of command stream");
166 }
167 if (do_command(&buf))
168 break;
169 strbuf_reset(&buf);
170 }
171
172 strbuf_release(&buf);
173 strbuf_release(&url_sb);
174 strbuf_release(&private_ref_sb);
175 return 0;
176}