blob: 3af708c5b671920cff3d1888eb9c8ae724e0d648 [file] [log] [blame]
Florian Achleitner68f64ff2012-09-19 17:21:15 +02001#include "cache.h"
Michael Haggertyfb58c8d2015-06-22 16:03:05 +02002#include "refs.h"
Florian Achleitner68f64ff2012-09-19 17:21:15 +02003#include "remote.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07004#include "object-store.h"
Florian Achleitner68f64ff2012-09-19 17:21:15 +02005#include "strbuf.h"
6#include "url.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07007#include "exec-cmd.h"
Florian Achleitner68f64ff2012-09-19 17:21:15 +02008#include "run-command.h"
9#include "vcs-svn/svndump.h"
10#include "notes.h"
11#include "argv-array.h"
12
13static const char *url;
Florian Achleitnerf6529de2012-09-19 17:21:23 +020014static int dump_from_file;
Florian Achleitner68f64ff2012-09-19 17:21:15 +020015static const char *private_ref;
16static const char *remote_ref = "refs/heads/master";
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020017static const char *marksfilename, *notes_ref;
18struct rev_note { unsigned int rev_nr; };
Florian Achleitner68f64ff2012-09-19 17:21:15 +020019
20static int cmd_capabilities(const char *line);
21static int cmd_import(const char *line);
22static int cmd_list(const char *line);
23
24typedef int (*input_command_handler)(const char *);
25struct input_command_entry {
26 const char *name;
27 input_command_handler fn;
28 unsigned char batchable; /* whether the command starts or is part of a batch */
29};
30
31static const struct input_command_entry input_command_list[] = {
32 { "capabilities", cmd_capabilities, 0 },
33 { "import", cmd_import, 1 },
34 { "list", cmd_list, 0 },
35 { NULL, NULL }
36};
37
38static int cmd_capabilities(const char *line)
39{
40 printf("import\n");
41 printf("bidi-import\n");
42 printf("refspec %s:%s\n\n", remote_ref, private_ref);
43 fflush(stdout);
44 return 0;
45}
46
47static void terminate_batch(void)
48{
49 /* terminate a current batch's fast-import stream */
50 printf("done\n");
51 fflush(stdout);
52}
53
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020054/* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */
brian m. carlson5ee8a952017-05-30 10:30:43 -070055static char *read_ref_note(const struct object_id *oid)
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020056{
brian m. carlson9ef72232017-05-30 10:30:40 -070057 const struct object_id *note_oid;
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020058 char *msg = NULL;
59 unsigned long msglen;
60 enum object_type type;
61
62 init_notes(NULL, notes_ref, NULL, 0);
brian m. carlson5ee8a952017-05-30 10:30:43 -070063 if (!(note_oid = get_note(NULL, oid)))
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020064 return NULL; /* note tree not found */
brian m. carlsonb4f5aca2018-03-12 02:27:53 +000065 if (!(msg = read_object_file(note_oid, &type, &msglen)))
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020066 error("Empty notes tree. %s", notes_ref);
67 else if (!msglen || type != OBJ_BLOB) {
68 error("Note contains unusable content. "
69 "Is something else using this notes tree? %s", notes_ref);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +000070 FREE_AND_NULL(msg);
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020071 }
72 free_notes(NULL);
73 return msg;
74}
75
76static int parse_rev_note(const char *msg, struct rev_note *res)
77{
78 const char *key, *value, *end;
79 size_t len;
80
81 while (*msg) {
Rohit Mani2c5495f2014-03-07 22:48:31 -080082 end = strchrnul(msg, '\n');
83 len = end - msg;
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020084
85 key = "Revision-number: ";
Christian Couder59556542013-11-30 21:55:40 +010086 if (starts_with(msg, key)) {
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020087 long i;
88 char *end;
89 value = msg + strlen(key);
90 i = strtol(value, &end, 0);
91 if (end == value || i < 0 || i > UINT32_MAX)
92 return -1;
93 res->rev_nr = i;
Jeff Kingbfae3422012-12-14 17:11:44 -050094 return 0;
Florian Achleitner8e43a1d2012-09-19 17:21:27 +020095 }
96 msg += len + 1;
97 }
Jeff Kingbfae3422012-12-14 17:11:44 -050098 /* didn't find it */
99 return -1;
Florian Achleitner8e43a1d2012-09-19 17:21:27 +0200100}
101
brian m. carlson490bc832017-05-30 10:30:39 -0700102static int note2mark_cb(const struct object_id *object_oid,
103 const struct object_id *note_oid, char *note_path,
Florian Achleitner5bfc76b2012-09-19 17:21:29 +0200104 void *cb_data)
105{
106 FILE *file = (FILE *)cb_data;
107 char *msg;
108 unsigned long msglen;
109 enum object_type type;
110 struct rev_note note;
111
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000112 if (!(msg = read_object_file(note_oid, &type, &msglen)) ||
Florian Achleitner5bfc76b2012-09-19 17:21:29 +0200113 !msglen || type != OBJ_BLOB) {
114 free(msg);
115 return 1;
116 }
117 if (parse_rev_note(msg, &note))
118 return 2;
brian m. carlson490bc832017-05-30 10:30:39 -0700119 if (fprintf(file, ":%d %s\n", note.rev_nr, oid_to_hex(object_oid)) < 1)
Florian Achleitner5bfc76b2012-09-19 17:21:29 +0200120 return 3;
121 return 0;
122}
123
124static void regenerate_marks(void)
125{
126 int ret;
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +0700127 FILE *marksfile = xfopen(marksfilename, "w+");
Florian Achleitner5bfc76b2012-09-19 17:21:29 +0200128
Florian Achleitner5bfc76b2012-09-19 17:21:29 +0200129 ret = for_each_note(NULL, 0, note2mark_cb, marksfile);
130 if (ret)
131 die("Regeneration of marks failed, returned %d.", ret);
132 fclose(marksfile);
133}
134
135static void check_or_regenerate_marks(int latestrev)
136{
137 FILE *marksfile;
138 struct strbuf sb = STRBUF_INIT;
139 struct strbuf line = STRBUF_INIT;
140 int found = 0;
141
142 if (latestrev < 1)
143 return;
144
145 init_notes(NULL, notes_ref, NULL, 0);
146 marksfile = fopen(marksfilename, "r");
147 if (!marksfile) {
148 regenerate_marks();
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +0700149 marksfile = xfopen(marksfilename, "r");
Florian Achleitner5bfc76b2012-09-19 17:21:29 +0200150 fclose(marksfile);
151 } else {
152 strbuf_addf(&sb, ":%d ", latestrev);
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800153 while (strbuf_getline_lf(&line, marksfile) != EOF) {
Christian Couder59556542013-11-30 21:55:40 +0100154 if (starts_with(line.buf, sb.buf)) {
Florian Achleitner5bfc76b2012-09-19 17:21:29 +0200155 found++;
156 break;
157 }
158 }
159 fclose(marksfile);
160 if (!found)
161 regenerate_marks();
162 }
163 free_notes(NULL);
164 strbuf_release(&sb);
165 strbuf_release(&line);
166}
167
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200168static int cmd_import(const char *line)
169{
170 int code;
171 int dumpin_fd;
Florian Achleitner8e43a1d2012-09-19 17:21:27 +0200172 char *note_msg;
brian m. carlson5ee8a952017-05-30 10:30:43 -0700173 struct object_id head_oid;
Florian Achleitner8e43a1d2012-09-19 17:21:27 +0200174 unsigned int startrev;
René Scharfed3180272014-08-19 21:09:35 +0200175 struct child_process svndump_proc = CHILD_PROCESS_INIT;
René Scharfe1cefa142014-07-18 17:20:19 +0200176 const char *command = "svnrdump";
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200177
brian m. carlson34c290a2017-10-15 22:06:56 +0000178 if (read_ref(private_ref, &head_oid))
Florian Achleitner8e43a1d2012-09-19 17:21:27 +0200179 startrev = 0;
180 else {
brian m. carlson5ee8a952017-05-30 10:30:43 -0700181 note_msg = read_ref_note(&head_oid);
Florian Achleitner8e43a1d2012-09-19 17:21:27 +0200182 if(note_msg == NULL) {
183 warning("No note found for %s.", private_ref);
184 startrev = 0;
185 } else {
186 struct rev_note note = { 0 };
187 if (parse_rev_note(note_msg, &note))
188 die("Revision number couldn't be parsed from note.");
189 startrev = note.rev_nr + 1;
190 free(note_msg);
191 }
192 }
Florian Achleitner5bfc76b2012-09-19 17:21:29 +0200193 check_or_regenerate_marks(startrev - 1);
Florian Achleitner8e43a1d2012-09-19 17:21:27 +0200194
Florian Achleitnerf6529de2012-09-19 17:21:23 +0200195 if (dump_from_file) {
196 dumpin_fd = open(url, O_RDONLY);
197 if(dumpin_fd < 0)
198 die_errno("Couldn't open svn dump file %s.", url);
199 } else {
Florian Achleitnerf6529de2012-09-19 17:21:23 +0200200 svndump_proc.out = -1;
René Scharfe1cefa142014-07-18 17:20:19 +0200201 argv_array_push(&svndump_proc.args, command);
202 argv_array_push(&svndump_proc.args, "dump");
203 argv_array_push(&svndump_proc.args, url);
204 argv_array_pushf(&svndump_proc.args, "-r%u:HEAD", startrev);
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200205
Florian Achleitnerf6529de2012-09-19 17:21:23 +0200206 code = start_command(&svndump_proc);
207 if (code)
René Scharfe1cefa142014-07-18 17:20:19 +0200208 die("Unable to start %s, code %d", command, code);
Florian Achleitnerf6529de2012-09-19 17:21:23 +0200209 dumpin_fd = svndump_proc.out;
210 }
Florian Achleitner8d7cd8e2012-09-19 17:21:26 +0200211 /* setup marks file import/export */
212 printf("feature import-marks-if-exists=%s\n"
213 "feature export-marks=%s\n", marksfilename, marksfilename);
214
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200215 svndump_init_fd(dumpin_fd, STDIN_FILENO);
Florian Achleitner8e43a1d2012-09-19 17:21:27 +0200216 svndump_read(url, private_ref, notes_ref);
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200217 svndump_deinit();
218 svndump_reset();
219
220 close(dumpin_fd);
Florian Achleitnerf6529de2012-09-19 17:21:23 +0200221 if (!dump_from_file) {
222 code = finish_command(&svndump_proc);
223 if (code)
René Scharfe1cefa142014-07-18 17:20:19 +0200224 warning("%s, returned %d", command, code);
Florian Achleitnerf6529de2012-09-19 17:21:23 +0200225 }
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200226
227 return 0;
228}
229
230static int cmd_list(const char *line)
231{
232 printf("? %s\n\n", remote_ref);
233 fflush(stdout);
234 return 0;
235}
236
237static int do_command(struct strbuf *line)
238{
239 const struct input_command_entry *p = input_command_list;
240 static struct string_list batchlines = STRING_LIST_INIT_DUP;
241 static const struct input_command_entry *batch_cmd;
242 /*
243 * commands can be grouped together in a batch.
244 * Batches are ended by \n. If no batch is active the program ends.
245 * During a batch all lines are buffered and passed to the handler function
246 * when the batch is terminated.
247 */
248 if (line->len == 0) {
249 if (batch_cmd) {
250 struct string_list_item *item;
251 for_each_string_list_item(item, &batchlines)
252 batch_cmd->fn(item->string);
253 terminate_batch();
254 batch_cmd = NULL;
255 string_list_clear(&batchlines, 0);
256 return 0; /* end of the batch, continue reading other commands. */
257 }
258 return 1; /* end of command stream, quit */
259 }
260 if (batch_cmd) {
Christian Couder59556542013-11-30 21:55:40 +0100261 if (!starts_with(batch_cmd->name, line->buf))
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200262 die("Active %s batch interrupted by %s", batch_cmd->name, line->buf);
263 /* buffer batch lines */
264 string_list_append(&batchlines, line->buf);
265 return 0;
266 }
267
268 for (p = input_command_list; p->name; p++) {
Christian Couder59556542013-11-30 21:55:40 +0100269 if (starts_with(line->buf, p->name) && (strlen(p->name) == line->len ||
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200270 line->buf[strlen(p->name)] == ' ')) {
271 if (p->batchable) {
272 batch_cmd = p;
273 string_list_append(&batchlines, line->buf);
274 return 0;
275 }
276 return p->fn(line->buf);
277 }
278 }
279 die("Unknown command '%s'\n", line->buf);
280 return 0;
281}
282
Jeff King3f2e2292016-07-01 01:58:58 -0400283int cmd_main(int argc, const char **argv)
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200284{
285 struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
Florian Achleitner8e43a1d2012-09-19 17:21:27 +0200286 private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT,
287 notes_ref_sb = STRBUF_INIT;
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200288 static struct remote *remote;
289 const char *url_in;
290
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200291 setup_git_directory();
292 if (argc < 2 || argc > 3) {
293 usage("git-remote-svn <remote-name> [<url>]");
294 return 1;
295 }
296
297 remote = remote_get(argv[1]);
298 url_in = (argc == 3) ? argv[2] : remote->url[0];
299
Christian Couder59556542013-11-30 21:55:40 +0100300 if (starts_with(url_in, "file://")) {
Florian Achleitnerf6529de2012-09-19 17:21:23 +0200301 dump_from_file = 1;
302 url = url_decode(url_in + sizeof("file://")-1);
303 } else {
304 dump_from_file = 0;
305 end_url_with_slash(&url_sb, url_in);
306 url = url_sb.buf;
307 }
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200308
309 strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
310 private_ref = private_ref_sb.buf;
311
Florian Achleitner8e43a1d2012-09-19 17:21:27 +0200312 strbuf_addf(&notes_ref_sb, "refs/notes/%s/revs", remote->name);
313 notes_ref = notes_ref_sb.buf;
314
Florian Achleitner8d7cd8e2012-09-19 17:21:26 +0200315 strbuf_addf(&marksfilename_sb, "%s/info/fast-import/remote-svn/%s.marks",
316 get_git_dir(), remote->name);
317 marksfilename = marksfilename_sb.buf;
318
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200319 while (1) {
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800320 if (strbuf_getline_lf(&buf, stdin) == EOF) {
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200321 if (ferror(stdin))
322 die("Error reading command stream");
323 else
324 die("Unexpected end of command stream");
325 }
326 if (do_command(&buf))
327 break;
328 strbuf_reset(&buf);
329 }
330
331 strbuf_release(&buf);
332 strbuf_release(&url_sb);
333 strbuf_release(&private_ref_sb);
Florian Achleitner8e43a1d2012-09-19 17:21:27 +0200334 strbuf_release(&notes_ref_sb);
Florian Achleitner8d7cd8e2012-09-19 17:21:26 +0200335 strbuf_release(&marksfilename_sb);
Florian Achleitner68f64ff2012-09-19 17:21:15 +0200336 return 0;
337}