6 #include "run-command.h"
7 #include "vcs-svn/svndump.h"
9 #include "argv-array.h"
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
, *notes_ref
;
16 struct rev_note
{ unsigned int rev_nr
; };
18 static int cmd_capabilities(const char *line
);
19 static int cmd_import(const char *line
);
20 static int cmd_list(const char *line
);
22 typedef int (*input_command_handler
)(const char *);
23 struct input_command_entry
{
25 input_command_handler fn
;
26 unsigned char batchable
; /* whether the command starts or is part of a batch */
29 static const struct input_command_entry input_command_list
[] = {
30 { "capabilities", cmd_capabilities
, 0 },
31 { "import", cmd_import
, 1 },
32 { "list", cmd_list
, 0 },
36 static int cmd_capabilities(const char *line
)
39 printf("bidi-import\n");
40 printf("refspec %s:%s\n\n", remote_ref
, private_ref
);
45 static void terminate_batch(void)
47 /* terminate a current batch's fast-import stream */
52 /* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */
53 static char *read_ref_note(const unsigned char sha1
[20])
55 const unsigned char *note_sha1
;
58 enum object_type type
;
60 init_notes(NULL
, notes_ref
, NULL
, 0);
61 if (!(note_sha1
= get_note(NULL
, sha1
)))
62 return NULL
; /* note tree not found */
63 if (!(msg
= read_sha1_file(note_sha1
, &type
, &msglen
)))
64 error("Empty notes tree. %s", notes_ref
);
65 else if (!msglen
|| type
!= OBJ_BLOB
) {
66 error("Note contains unusable content. "
67 "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
= strchr(msg
, '\n');
82 len
= end
? end
- msg
: strlen(msg
);
84 key
= "Revision-number: ";
85 if (!prefixcmp(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 unsigned char *object_sha1
,
102 const unsigned char *note_sha1
, 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_sha1_file(note_sha1
, &type
, &msglen
)) ||
112 !msglen
|| type
!= OBJ_BLOB
) {
116 if (parse_rev_note(msg
, ¬e
))
118 if (fprintf(file
, ":%d %s\n", note
.rev_nr
, sha1_to_hex(object_sha1
)) < 1)
123 static void regenerate_marks(void)
126 FILE *marksfile
= fopen(marksfilename
, "w+");
129 die_errno("Couldn't create mark file %s.", marksfilename
);
130 ret
= for_each_note(NULL
, 0, note2mark_cb
, marksfile
);
132 die("Regeneration of marks failed, returned %d.", ret
);
136 static void check_or_regenerate_marks(int latestrev
)
139 struct strbuf sb
= STRBUF_INIT
;
140 struct strbuf line
= STRBUF_INIT
;
146 init_notes(NULL
, notes_ref
, NULL
, 0);
147 marksfile
= fopen(marksfilename
, "r");
150 marksfile
= fopen(marksfilename
, "r");
152 die_errno("cannot read marks file %s!", marksfilename
);
155 strbuf_addf(&sb
, ":%d ", latestrev
);
156 while (strbuf_getline(&line
, marksfile
, '\n') != EOF
) {
157 if (!prefixcmp(line
.buf
, sb
.buf
)) {
168 strbuf_release(&line
);
171 static int cmd_import(const char *line
)
176 unsigned char head_sha1
[20];
177 unsigned int startrev
;
178 struct argv_array svndump_argv
= ARGV_ARRAY_INIT
;
179 struct child_process svndump_proc
;
181 if (read_ref(private_ref
, head_sha1
))
184 note_msg
= read_ref_note(head_sha1
);
185 if(note_msg
== NULL
) {
186 warning("No note found for %s.", private_ref
);
189 struct rev_note note
= { 0 };
190 if (parse_rev_note(note_msg
, ¬e
))
191 die("Revision number couldn't be parsed from note.");
192 startrev
= note
.rev_nr
+ 1;
196 check_or_regenerate_marks(startrev
- 1);
198 if (dump_from_file
) {
199 dumpin_fd
= open(url
, O_RDONLY
);
201 die_errno("Couldn't open svn dump file %s.", url
);
203 memset(&svndump_proc
, 0, sizeof(struct child_process
));
204 svndump_proc
.out
= -1;
205 argv_array_push(&svndump_argv
, "svnrdump");
206 argv_array_push(&svndump_argv
, "dump");
207 argv_array_push(&svndump_argv
, url
);
208 argv_array_pushf(&svndump_argv
, "-r%u:HEAD", startrev
);
209 svndump_proc
.argv
= svndump_argv
.argv
;
211 code
= start_command(&svndump_proc
);
213 die("Unable to start %s, code %d", svndump_proc
.argv
[0], code
);
214 dumpin_fd
= svndump_proc
.out
;
216 /* setup marks file import/export */
217 printf("feature import-marks-if-exists=%s\n"
218 "feature export-marks=%s\n", marksfilename
, marksfilename
);
220 svndump_init_fd(dumpin_fd
, STDIN_FILENO
);
221 svndump_read(url
, private_ref
, notes_ref
);
226 if (!dump_from_file
) {
227 code
= finish_command(&svndump_proc
);
229 warning("%s, returned %d", svndump_proc
.argv
[0], code
);
230 argv_array_clear(&svndump_argv
);
236 static int cmd_list(const char *line
)
238 printf("? %s\n\n", remote_ref
);
243 static int do_command(struct strbuf
*line
)
245 const struct input_command_entry
*p
= input_command_list
;
246 static struct string_list batchlines
= STRING_LIST_INIT_DUP
;
247 static const struct input_command_entry
*batch_cmd
;
249 * commands can be grouped together in a batch.
250 * Batches are ended by \n. If no batch is active the program ends.
251 * During a batch all lines are buffered and passed to the handler function
252 * when the batch is terminated.
254 if (line
->len
== 0) {
256 struct string_list_item
*item
;
257 for_each_string_list_item(item
, &batchlines
)
258 batch_cmd
->fn(item
->string
);
261 string_list_clear(&batchlines
, 0);
262 return 0; /* end of the batch, continue reading other commands. */
264 return 1; /* end of command stream, quit */
267 if (prefixcmp(batch_cmd
->name
, line
->buf
))
268 die("Active %s batch interrupted by %s", batch_cmd
->name
, line
->buf
);
269 /* buffer batch lines */
270 string_list_append(&batchlines
, line
->buf
);
274 for (p
= input_command_list
; p
->name
; p
++) {
275 if (!prefixcmp(line
->buf
, p
->name
) && (strlen(p
->name
) == line
->len
||
276 line
->buf
[strlen(p
->name
)] == ' ')) {
279 string_list_append(&batchlines
, line
->buf
);
282 return p
->fn(line
->buf
);
285 die("Unknown command '%s'\n", line
->buf
);
289 int main(int argc
, char **argv
)
291 struct strbuf buf
= STRBUF_INIT
, url_sb
= STRBUF_INIT
,
292 private_ref_sb
= STRBUF_INIT
, marksfilename_sb
= STRBUF_INIT
,
293 notes_ref_sb
= STRBUF_INIT
;
294 static struct remote
*remote
;
297 git_extract_argv0_path(argv
[0]);
298 setup_git_directory();
299 if (argc
< 2 || argc
> 3) {
300 usage("git-remote-svn <remote-name> [<url>]");
304 remote
= remote_get(argv
[1]);
305 url_in
= (argc
== 3) ? argv
[2] : remote
->url
[0];
307 if (!prefixcmp(url_in
, "file://")) {
309 url
= url_decode(url_in
+ sizeof("file://")-1);
312 end_url_with_slash(&url_sb
, url_in
);
316 strbuf_addf(&private_ref_sb
, "refs/svn/%s/master", remote
->name
);
317 private_ref
= private_ref_sb
.buf
;
319 strbuf_addf(¬es_ref_sb
, "refs/notes/%s/revs", remote
->name
);
320 notes_ref
= notes_ref_sb
.buf
;
322 strbuf_addf(&marksfilename_sb
, "%s/info/fast-import/remote-svn/%s.marks",
323 get_git_dir(), remote
->name
);
324 marksfilename
= marksfilename_sb
.buf
;
327 if (strbuf_getline(&buf
, stdin
, '\n') == EOF
) {
329 die("Error reading command stream");
331 die("Unexpected end of command stream");
333 if (do_command(&buf
))
338 strbuf_release(&buf
);
339 strbuf_release(&url_sb
);
340 strbuf_release(&private_ref_sb
);
341 strbuf_release(¬es_ref_sb
);
342 strbuf_release(&marksfilename_sb
);