Makefile: add a DEVOPTS to suppress -Werror under DEVELOPER
[git/debian.git] / remote-testsvn.c
blobbcebb4c789567eb4017a3a0132ba55c59c427991
1 #include "cache.h"
2 #include "refs.h"
3 #include "remote.h"
4 #include "strbuf.h"
5 #include "url.h"
6 #include "exec_cmd.h"
7 #include "run-command.h"
8 #include "vcs-svn/svndump.h"
9 #include "notes.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 {
25 const char *name;
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 },
34 { NULL, NULL }
37 static int cmd_capabilities(const char *line)
39 printf("import\n");
40 printf("bidi-import\n");
41 printf("refspec %s:%s\n\n", remote_ref, private_ref);
42 fflush(stdout);
43 return 0;
46 static void terminate_batch(void)
48 /* terminate a current batch's fast-import stream */
49 printf("done\n");
50 fflush(stdout);
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;
57 char *msg = NULL;
58 unsigned long msglen;
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_sha1_file(note_oid->hash, &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);
69 FREE_AND_NULL(msg);
71 free_notes(NULL);
72 return msg;
75 static int parse_rev_note(const char *msg, struct rev_note *res)
77 const char *key, *value, *end;
78 size_t len;
80 while (*msg) {
81 end = strchrnul(msg, '\n');
82 len = end - msg;
84 key = "Revision-number: ";
85 if (starts_with(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 return 0;
95 msg += len + 1;
97 /* didn't find it */
98 return -1;
101 static int note2mark_cb(const struct object_id *object_oid,
102 const struct object_id *note_oid, char *note_path,
103 void *cb_data)
105 FILE *file = (FILE *)cb_data;
106 char *msg;
107 unsigned long msglen;
108 enum object_type type;
109 struct rev_note note;
111 if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen)) ||
112 !msglen || type != OBJ_BLOB) {
113 free(msg);
114 return 1;
116 if (parse_rev_note(msg, &note))
117 return 2;
118 if (fprintf(file, ":%d %s\n", note.rev_nr, oid_to_hex(object_oid)) < 1)
119 return 3;
120 return 0;
123 static void regenerate_marks(void)
125 int ret;
126 FILE *marksfile = xfopen(marksfilename, "w+");
128 ret = for_each_note(NULL, 0, note2mark_cb, marksfile);
129 if (ret)
130 die("Regeneration of marks failed, returned %d.", ret);
131 fclose(marksfile);
134 static void check_or_regenerate_marks(int latestrev)
136 FILE *marksfile;
137 struct strbuf sb = STRBUF_INIT;
138 struct strbuf line = STRBUF_INIT;
139 int found = 0;
141 if (latestrev < 1)
142 return;
144 init_notes(NULL, notes_ref, NULL, 0);
145 marksfile = fopen(marksfilename, "r");
146 if (!marksfile) {
147 regenerate_marks();
148 marksfile = xfopen(marksfilename, "r");
149 fclose(marksfile);
150 } else {
151 strbuf_addf(&sb, ":%d ", latestrev);
152 while (strbuf_getline_lf(&line, marksfile) != EOF) {
153 if (starts_with(line.buf, sb.buf)) {
154 found++;
155 break;
158 fclose(marksfile);
159 if (!found)
160 regenerate_marks();
162 free_notes(NULL);
163 strbuf_release(&sb);
164 strbuf_release(&line);
167 static int cmd_import(const char *line)
169 int code;
170 int dumpin_fd;
171 char *note_msg;
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))
178 startrev = 0;
179 else {
180 note_msg = read_ref_note(&head_oid);
181 if(note_msg == NULL) {
182 warning("No note found for %s.", private_ref);
183 startrev = 0;
184 } else {
185 struct rev_note note = { 0 };
186 if (parse_rev_note(note_msg, &note))
187 die("Revision number couldn't be parsed from note.");
188 startrev = note.rev_nr + 1;
189 free(note_msg);
192 check_or_regenerate_marks(startrev - 1);
194 if (dump_from_file) {
195 dumpin_fd = open(url, O_RDONLY);
196 if(dumpin_fd < 0)
197 die_errno("Couldn't open svn dump file %s.", url);
198 } else {
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);
206 if (code)
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);
216 svndump_deinit();
217 svndump_reset();
219 close(dumpin_fd);
220 if (!dump_from_file) {
221 code = finish_command(&svndump_proc);
222 if (code)
223 warning("%s, returned %d", command, code);
226 return 0;
229 static int cmd_list(const char *line)
231 printf("? %s\n\n", remote_ref);
232 fflush(stdout);
233 return 0;
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) {
248 if (batch_cmd) {
249 struct string_list_item *item;
250 for_each_string_list_item(item, &batchlines)
251 batch_cmd->fn(item->string);
252 terminate_batch();
253 batch_cmd = NULL;
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 */
259 if (batch_cmd) {
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);
264 return 0;
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)] == ' ')) {
270 if (p->batchable) {
271 batch_cmd = p;
272 string_list_append(&batchlines, line->buf);
273 return 0;
275 return p->fn(line->buf);
278 die("Unknown command '%s'\n", line->buf);
279 return 0;
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;
288 const char *url_in;
290 setup_git_directory();
291 if (argc < 2 || argc > 3) {
292 usage("git-remote-svn <remote-name> [<url>]");
293 return 1;
296 remote = remote_get(argv[1]);
297 url_in = (argc == 3) ? argv[2] : remote->url[0];
299 if (starts_with(url_in, "file://")) {
300 dump_from_file = 1;
301 url = url_decode(url_in + sizeof("file://")-1);
302 } else {
303 dump_from_file = 0;
304 end_url_with_slash(&url_sb, url_in);
305 url = url_sb.buf;
308 strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
309 private_ref = private_ref_sb.buf;
311 strbuf_addf(&notes_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;
318 while (1) {
319 if (strbuf_getline_lf(&buf, stdin) == EOF) {
320 if (ferror(stdin))
321 die("Error reading command stream");
322 else
323 die("Unexpected end of command stream");
325 if (do_command(&buf))
326 break;
327 strbuf_reset(&buf);
330 strbuf_release(&buf);
331 strbuf_release(&url_sb);
332 strbuf_release(&private_ref_sb);
333 strbuf_release(&notes_ref_sb);
334 strbuf_release(&marksfilename_sb);
335 return 0;