7 #include "run-command.h"
8 #include "vcs-svn/svndump.h"
10 #include "argv-array.h"
12 static const char *url
;
13 static int dump_from_file
;
14 static const char *private_ref
;
15 static const char *remote_ref
= "refs/heads/master";
16 static const char *marksfilename
, *notes_ref
;
17 struct rev_note
{ unsigned int rev_nr
; };
19 static int cmd_capabilities(const char *line
);
20 static int cmd_import(const char *line
);
21 static int cmd_list(const char *line
);
23 typedef int (*input_command_handler
)(const char *);
24 struct input_command_entry
{
26 input_command_handler fn
;
27 unsigned char batchable
; /* whether the command starts or is part of a batch */
30 static const struct input_command_entry input_command_list
[] = {
31 { "capabilities", cmd_capabilities
, 0 },
32 { "import", cmd_import
, 1 },
33 { "list", cmd_list
, 0 },
37 static int cmd_capabilities(const char *line
)
40 printf("bidi-import\n");
41 printf("refspec %s:%s\n\n", remote_ref
, private_ref
);
46 static void terminate_batch(void)
48 /* terminate a current batch's fast-import stream */
53 /* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */
54 static char *read_ref_note(const struct object_id
*oid
)
56 const struct object_id
*note_oid
;
59 enum object_type type
;
61 init_notes(NULL
, notes_ref
, NULL
, 0);
62 if (!(note_oid
= get_note(NULL
, oid
)))
63 return NULL
; /* note tree not found */
64 if (!(msg
= read_object_file(note_oid
, &type
, &msglen
)))
65 error("Empty notes tree. %s", notes_ref
);
66 else if (!msglen
|| type
!= OBJ_BLOB
) {
67 error("Note contains unusable content. "
68 "Is something else using this notes tree? %s", notes_ref
);
75 static int parse_rev_note(const char *msg
, struct rev_note
*res
)
77 const char *key
, *value
, *end
;
81 end
= strchrnul(msg
, '\n');
84 key
= "Revision-number: ";
85 if (starts_with(msg
, key
)) {
88 value
= msg
+ strlen(key
);
89 i
= strtol(value
, &end
, 0);
90 if (end
== value
|| i
< 0 || i
> UINT32_MAX
)
101 static int note2mark_cb(const struct object_id
*object_oid
,
102 const struct object_id
*note_oid
, char *note_path
,
105 FILE *file
= (FILE *)cb_data
;
107 unsigned long msglen
;
108 enum object_type type
;
109 struct rev_note note
;
111 if (!(msg
= read_object_file(note_oid
, &type
, &msglen
)) ||
112 !msglen
|| type
!= OBJ_BLOB
) {
116 if (parse_rev_note(msg
, ¬e
))
118 if (fprintf(file
, ":%d %s\n", note
.rev_nr
, oid_to_hex(object_oid
)) < 1)
123 static void regenerate_marks(void)
126 FILE *marksfile
= xfopen(marksfilename
, "w+");
128 ret
= for_each_note(NULL
, 0, note2mark_cb
, marksfile
);
130 die("Regeneration of marks failed, returned %d.", ret
);
134 static void check_or_regenerate_marks(int latestrev
)
137 struct strbuf sb
= STRBUF_INIT
;
138 struct strbuf line
= STRBUF_INIT
;
144 init_notes(NULL
, notes_ref
, NULL
, 0);
145 marksfile
= fopen(marksfilename
, "r");
148 marksfile
= xfopen(marksfilename
, "r");
151 strbuf_addf(&sb
, ":%d ", latestrev
);
152 while (strbuf_getline_lf(&line
, marksfile
) != EOF
) {
153 if (starts_with(line
.buf
, sb
.buf
)) {
164 strbuf_release(&line
);
167 static int cmd_import(const char *line
)
172 struct object_id head_oid
;
173 unsigned int startrev
;
174 struct child_process svndump_proc
= CHILD_PROCESS_INIT
;
175 const char *command
= "svnrdump";
177 if (read_ref(private_ref
, &head_oid
))
180 note_msg
= read_ref_note(&head_oid
);
181 if(note_msg
== NULL
) {
182 warning("No note found for %s.", private_ref
);
185 struct rev_note note
= { 0 };
186 if (parse_rev_note(note_msg
, ¬e
))
187 die("Revision number couldn't be parsed from note.");
188 startrev
= note
.rev_nr
+ 1;
192 check_or_regenerate_marks(startrev
- 1);
194 if (dump_from_file
) {
195 dumpin_fd
= open(url
, O_RDONLY
);
197 die_errno("Couldn't open svn dump file %s.", url
);
199 svndump_proc
.out
= -1;
200 argv_array_push(&svndump_proc
.args
, command
);
201 argv_array_push(&svndump_proc
.args
, "dump");
202 argv_array_push(&svndump_proc
.args
, url
);
203 argv_array_pushf(&svndump_proc
.args
, "-r%u:HEAD", startrev
);
205 code
= start_command(&svndump_proc
);
207 die("Unable to start %s, code %d", command
, code
);
208 dumpin_fd
= svndump_proc
.out
;
210 /* setup marks file import/export */
211 printf("feature import-marks-if-exists=%s\n"
212 "feature export-marks=%s\n", marksfilename
, marksfilename
);
214 svndump_init_fd(dumpin_fd
, STDIN_FILENO
);
215 svndump_read(url
, private_ref
, notes_ref
);
220 if (!dump_from_file
) {
221 code
= finish_command(&svndump_proc
);
223 warning("%s, returned %d", command
, code
);
229 static int cmd_list(const char *line
)
231 printf("? %s\n\n", remote_ref
);
236 static int do_command(struct strbuf
*line
)
238 const struct input_command_entry
*p
= input_command_list
;
239 static struct string_list batchlines
= STRING_LIST_INIT_DUP
;
240 static const struct input_command_entry
*batch_cmd
;
242 * commands can be grouped together in a batch.
243 * Batches are ended by \n. If no batch is active the program ends.
244 * During a batch all lines are buffered and passed to the handler function
245 * when the batch is terminated.
247 if (line
->len
== 0) {
249 struct string_list_item
*item
;
250 for_each_string_list_item(item
, &batchlines
)
251 batch_cmd
->fn(item
->string
);
254 string_list_clear(&batchlines
, 0);
255 return 0; /* end of the batch, continue reading other commands. */
257 return 1; /* end of command stream, quit */
260 if (!starts_with(batch_cmd
->name
, line
->buf
))
261 die("Active %s batch interrupted by %s", batch_cmd
->name
, line
->buf
);
262 /* buffer batch lines */
263 string_list_append(&batchlines
, line
->buf
);
267 for (p
= input_command_list
; p
->name
; p
++) {
268 if (starts_with(line
->buf
, p
->name
) && (strlen(p
->name
) == line
->len
||
269 line
->buf
[strlen(p
->name
)] == ' ')) {
272 string_list_append(&batchlines
, line
->buf
);
275 return p
->fn(line
->buf
);
278 die("Unknown command '%s'\n", line
->buf
);
282 int cmd_main(int argc
, const char **argv
)
284 struct strbuf buf
= STRBUF_INIT
, url_sb
= STRBUF_INIT
,
285 private_ref_sb
= STRBUF_INIT
, marksfilename_sb
= STRBUF_INIT
,
286 notes_ref_sb
= STRBUF_INIT
;
287 static struct remote
*remote
;
290 setup_git_directory();
291 if (argc
< 2 || argc
> 3) {
292 usage("git-remote-svn <remote-name> [<url>]");
296 remote
= remote_get(argv
[1]);
297 url_in
= (argc
== 3) ? argv
[2] : remote
->url
[0];
299 if (starts_with(url_in
, "file://")) {
301 url
= url_decode(url_in
+ sizeof("file://")-1);
304 end_url_with_slash(&url_sb
, url_in
);
308 strbuf_addf(&private_ref_sb
, "refs/svn/%s/master", remote
->name
);
309 private_ref
= private_ref_sb
.buf
;
311 strbuf_addf(¬es_ref_sb
, "refs/notes/%s/revs", remote
->name
);
312 notes_ref
= notes_ref_sb
.buf
;
314 strbuf_addf(&marksfilename_sb
, "%s/info/fast-import/remote-svn/%s.marks",
315 get_git_dir(), remote
->name
);
316 marksfilename
= marksfilename_sb
.buf
;
319 if (strbuf_getline_lf(&buf
, stdin
) == EOF
) {
321 die("Error reading command stream");
323 die("Unexpected end of command stream");
325 if (do_command(&buf
))
330 strbuf_release(&buf
);
331 strbuf_release(&url_sb
);
332 strbuf_release(&private_ref_sb
);
333 strbuf_release(¬es_ref_sb
);
334 strbuf_release(&marksfilename_sb
);