]> Pileus Git - ~andy/git/blob - remote-testsvn.c
remote-svn: Activate import/export-marks for fast-import
[~andy/git] / remote-testsvn.c
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
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;
16
17 static int cmd_capabilities(const char *line);
18 static int cmd_import(const char *line);
19 static int cmd_list(const char *line);
20
21 typedef int (*input_command_handler)(const char *);
22 struct input_command_entry {
23         const char *name;
24         input_command_handler fn;
25         unsigned char batchable;        /* whether the command starts or is part of a batch */
26 };
27
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 },
32         { NULL, NULL }
33 };
34
35 static int cmd_capabilities(const char *line)
36 {
37         printf("import\n");
38         printf("bidi-import\n");
39         printf("refspec %s:%s\n\n", remote_ref, private_ref);
40         fflush(stdout);
41         return 0;
42 }
43
44 static void terminate_batch(void)
45 {
46         /* terminate a current batch's fast-import stream */
47         printf("done\n");
48         fflush(stdout);
49 }
50
51 static int cmd_import(const char *line)
52 {
53         int code;
54         int dumpin_fd;
55         unsigned int startrev = 0;
56         struct argv_array svndump_argv = ARGV_ARRAY_INIT;
57         struct child_process svndump_proc;
58
59         if (dump_from_file) {
60                 dumpin_fd = open(url, O_RDONLY);
61                 if(dumpin_fd < 0)
62                         die_errno("Couldn't open svn dump file %s.", url);
63         } else {
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;
71
72                 code = start_command(&svndump_proc);
73                 if (code)
74                         die("Unable to start %s, code %d", svndump_proc.argv[0], code);
75                 dumpin_fd = svndump_proc.out;
76         }
77         /* setup marks file import/export */
78         printf("feature import-marks-if-exists=%s\n"
79                         "feature export-marks=%s\n", marksfilename, marksfilename);
80
81         svndump_init_fd(dumpin_fd, STDIN_FILENO);
82         svndump_read(url, private_ref);
83         svndump_deinit();
84         svndump_reset();
85
86         close(dumpin_fd);
87         if (!dump_from_file) {
88                 code = finish_command(&svndump_proc);
89                 if (code)
90                         warning("%s, returned %d", svndump_proc.argv[0], code);
91                 argv_array_clear(&svndump_argv);
92         }
93
94         return 0;
95 }
96
97 static int cmd_list(const char *line)
98 {
99         printf("? %s\n\n", remote_ref);
100         fflush(stdout);
101         return 0;
102 }
103
104 static int do_command(struct strbuf *line)
105 {
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;
109         /*
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.
114          */
115         if (line->len == 0) {
116                 if (batch_cmd) {
117                         struct string_list_item *item;
118                         for_each_string_list_item(item, &batchlines)
119                                 batch_cmd->fn(item->string);
120                         terminate_batch();
121                         batch_cmd = NULL;
122                         string_list_clear(&batchlines, 0);
123                         return 0;       /* end of the batch, continue reading other commands. */
124                 }
125                 return 1;       /* end of command stream, quit */
126         }
127         if (batch_cmd) {
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);
132                 return 0;
133         }
134
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)] == ' ')) {
138                         if (p->batchable) {
139                                 batch_cmd = p;
140                                 string_list_append(&batchlines, line->buf);
141                                 return 0;
142                         }
143                         return p->fn(line->buf);
144                 }
145         }
146         die("Unknown command '%s'\n", line->buf);
147         return 0;
148 }
149
150 int main(int argc, const char **argv)
151 {
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;
155         const char *url_in;
156
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>]");
161                 return 1;
162         }
163
164         remote = remote_get(argv[1]);
165         url_in = (argc == 3) ? argv[2] : remote->url[0];
166
167         if (!prefixcmp(url_in, "file://")) {
168                 dump_from_file = 1;
169                 url = url_decode(url_in + sizeof("file://")-1);
170         } else {
171                 dump_from_file = 0;
172                 end_url_with_slash(&url_sb, url_in);
173                 url = url_sb.buf;
174         }
175
176         strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
177         private_ref = private_ref_sb.buf;
178
179         strbuf_addf(&marksfilename_sb, "%s/info/fast-import/remote-svn/%s.marks",
180                 get_git_dir(), remote->name);
181         marksfilename = marksfilename_sb.buf;
182
183         while (1) {
184                 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
185                         if (ferror(stdin))
186                                 die("Error reading command stream");
187                         else
188                                 die("Unexpected end of command stream");
189                 }
190                 if (do_command(&buf))
191                         break;
192                 strbuf_reset(&buf);
193         }
194
195         strbuf_release(&buf);
196         strbuf_release(&url_sb);
197         strbuf_release(&private_ref_sb);
198         strbuf_release(&marksfilename_sb);
199         return 0;
200 }