]> git.ipfire.org Git - thirdparty/git.git/blame - remote-testsvn.c
Add a svnrdump-simulator replaying a dump file for testing
[thirdparty/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;
f6529de9 12static int dump_from_file;
68f64ff8
FA
13static const char *private_ref;
14static const char *remote_ref = "refs/heads/master";
8e43a1d0
FA
15static const char *marksfilename, *notes_ref;
16struct rev_note { unsigned int rev_nr; };
68f64ff8
FA
17
18static int cmd_capabilities(const char *line);
19static int cmd_import(const char *line);
20static int cmd_list(const char *line);
21
22typedef int (*input_command_handler)(const char *);
23struct input_command_entry {
24 const char *name;
25 input_command_handler fn;
26 unsigned char batchable; /* whether the command starts or is part of a batch */
27};
28
29static const struct input_command_entry input_command_list[] = {
30 { "capabilities", cmd_capabilities, 0 },
31 { "import", cmd_import, 1 },
32 { "list", cmd_list, 0 },
33 { NULL, NULL }
34};
35
36static int cmd_capabilities(const char *line)
37{
38 printf("import\n");
39 printf("bidi-import\n");
40 printf("refspec %s:%s\n\n", remote_ref, private_ref);
41 fflush(stdout);
42 return 0;
43}
44
45static void terminate_batch(void)
46{
47 /* terminate a current batch's fast-import stream */
48 printf("done\n");
49 fflush(stdout);
50}
51
8e43a1d0
FA
52/* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */
53static char *read_ref_note(const unsigned char sha1[20])
54{
55 const unsigned char *note_sha1;
56 char *msg = NULL;
57 unsigned long msglen;
58 enum object_type type;
59
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);
68 free(msg);
69 msg = NULL;
70 }
71 free_notes(NULL);
72 return msg;
73}
74
75static int parse_rev_note(const char *msg, struct rev_note *res)
76{
77 const char *key, *value, *end;
78 size_t len;
79
80 while (*msg) {
81 end = strchr(msg, '\n');
82 len = end ? end - msg : strlen(msg);
83
84 key = "Revision-number: ";
85 if (!prefixcmp(msg, key)) {
86 long i;
87 char *end;
88 value = msg + strlen(key);
89 i = strtol(value, &end, 0);
90 if (end == value || i < 0 || i > UINT32_MAX)
91 return -1;
92 res->rev_nr = i;
93 }
94 msg += len + 1;
95 }
96 return 0;
97}
98
68f64ff8
FA
99static int cmd_import(const char *line)
100{
101 int code;
102 int dumpin_fd;
8e43a1d0
FA
103 char *note_msg;
104 unsigned char head_sha1[20];
105 unsigned int startrev;
68f64ff8
FA
106 struct argv_array svndump_argv = ARGV_ARRAY_INIT;
107 struct child_process svndump_proc;
108
8e43a1d0
FA
109 if (read_ref(private_ref, head_sha1))
110 startrev = 0;
111 else {
112 note_msg = read_ref_note(head_sha1);
113 if(note_msg == NULL) {
114 warning("No note found for %s.", private_ref);
115 startrev = 0;
116 } else {
117 struct rev_note note = { 0 };
118 if (parse_rev_note(note_msg, &note))
119 die("Revision number couldn't be parsed from note.");
120 startrev = note.rev_nr + 1;
121 free(note_msg);
122 }
123 }
124
f6529de9
FA
125 if (dump_from_file) {
126 dumpin_fd = open(url, O_RDONLY);
127 if(dumpin_fd < 0)
128 die_errno("Couldn't open svn dump file %s.", url);
129 } else {
130 memset(&svndump_proc, 0, sizeof(struct child_process));
131 svndump_proc.out = -1;
132 argv_array_push(&svndump_argv, "svnrdump");
133 argv_array_push(&svndump_argv, "dump");
134 argv_array_push(&svndump_argv, url);
135 argv_array_pushf(&svndump_argv, "-r%u:HEAD", startrev);
136 svndump_proc.argv = svndump_argv.argv;
137
138 code = start_command(&svndump_proc);
139 if (code)
140 die("Unable to start %s, code %d", svndump_proc.argv[0], code);
141 dumpin_fd = svndump_proc.out;
142 }
8d7cd8eb
FA
143 /* setup marks file import/export */
144 printf("feature import-marks-if-exists=%s\n"
145 "feature export-marks=%s\n", marksfilename, marksfilename);
146
68f64ff8 147 svndump_init_fd(dumpin_fd, STDIN_FILENO);
8e43a1d0 148 svndump_read(url, private_ref, notes_ref);
68f64ff8
FA
149 svndump_deinit();
150 svndump_reset();
151
152 close(dumpin_fd);
f6529de9
FA
153 if (!dump_from_file) {
154 code = finish_command(&svndump_proc);
155 if (code)
156 warning("%s, returned %d", svndump_proc.argv[0], code);
157 argv_array_clear(&svndump_argv);
158 }
68f64ff8
FA
159
160 return 0;
161}
162
163static int cmd_list(const char *line)
164{
165 printf("? %s\n\n", remote_ref);
166 fflush(stdout);
167 return 0;
168}
169
170static int do_command(struct strbuf *line)
171{
172 const struct input_command_entry *p = input_command_list;
173 static struct string_list batchlines = STRING_LIST_INIT_DUP;
174 static const struct input_command_entry *batch_cmd;
175 /*
176 * commands can be grouped together in a batch.
177 * Batches are ended by \n. If no batch is active the program ends.
178 * During a batch all lines are buffered and passed to the handler function
179 * when the batch is terminated.
180 */
181 if (line->len == 0) {
182 if (batch_cmd) {
183 struct string_list_item *item;
184 for_each_string_list_item(item, &batchlines)
185 batch_cmd->fn(item->string);
186 terminate_batch();
187 batch_cmd = NULL;
188 string_list_clear(&batchlines, 0);
189 return 0; /* end of the batch, continue reading other commands. */
190 }
191 return 1; /* end of command stream, quit */
192 }
193 if (batch_cmd) {
194 if (prefixcmp(batch_cmd->name, line->buf))
195 die("Active %s batch interrupted by %s", batch_cmd->name, line->buf);
196 /* buffer batch lines */
197 string_list_append(&batchlines, line->buf);
198 return 0;
199 }
200
201 for (p = input_command_list; p->name; p++) {
202 if (!prefixcmp(line->buf, p->name) && (strlen(p->name) == line->len ||
203 line->buf[strlen(p->name)] == ' ')) {
204 if (p->batchable) {
205 batch_cmd = p;
206 string_list_append(&batchlines, line->buf);
207 return 0;
208 }
209 return p->fn(line->buf);
210 }
211 }
212 die("Unknown command '%s'\n", line->buf);
213 return 0;
214}
215
216int main(int argc, const char **argv)
217{
218 struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
8e43a1d0
FA
219 private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT,
220 notes_ref_sb = STRBUF_INIT;
68f64ff8
FA
221 static struct remote *remote;
222 const char *url_in;
223
224 git_extract_argv0_path(argv[0]);
225 setup_git_directory();
226 if (argc < 2 || argc > 3) {
227 usage("git-remote-svn <remote-name> [<url>]");
228 return 1;
229 }
230
231 remote = remote_get(argv[1]);
232 url_in = (argc == 3) ? argv[2] : remote->url[0];
233
f6529de9
FA
234 if (!prefixcmp(url_in, "file://")) {
235 dump_from_file = 1;
236 url = url_decode(url_in + sizeof("file://")-1);
237 } else {
238 dump_from_file = 0;
239 end_url_with_slash(&url_sb, url_in);
240 url = url_sb.buf;
241 }
68f64ff8
FA
242
243 strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
244 private_ref = private_ref_sb.buf;
245
8e43a1d0
FA
246 strbuf_addf(&notes_ref_sb, "refs/notes/%s/revs", remote->name);
247 notes_ref = notes_ref_sb.buf;
248
8d7cd8eb
FA
249 strbuf_addf(&marksfilename_sb, "%s/info/fast-import/remote-svn/%s.marks",
250 get_git_dir(), remote->name);
251 marksfilename = marksfilename_sb.buf;
252
68f64ff8
FA
253 while (1) {
254 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
255 if (ferror(stdin))
256 die("Error reading command stream");
257 else
258 die("Unexpected end of command stream");
259 }
260 if (do_command(&buf))
261 break;
262 strbuf_reset(&buf);
263 }
264
265 strbuf_release(&buf);
266 strbuf_release(&url_sb);
267 strbuf_release(&private_ref_sb);
8e43a1d0 268 strbuf_release(&notes_ref_sb);
8d7cd8eb 269 strbuf_release(&marksfilename_sb);
68f64ff8
FA
270 return 0;
271}