Merge branch 'ah/doc-empty-string-is-false'
[git] / remote-testsvn.c
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"
11
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; };
18
19 static int cmd_capabilities(const char *line);
20 static int cmd_import(const char *line);
21 static int cmd_list(const char *line);
22
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 */
28 };
29
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 }
35 };
36
37 static int cmd_capabilities(const char *line)
38 {
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;
44 }
45
46 static void terminate_batch(void)
47 {
48         /* terminate a current batch's fast-import stream */
49         printf("done\n");
50         fflush(stdout);
51 }
52
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)
55 {
56         const struct object_id *note_oid;
57         char *msg = NULL;
58         unsigned long msglen;
59         enum object_type type;
60
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);
70         }
71         free_notes(NULL);
72         return msg;
73 }
74
75 static 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 = strchrnul(msg, '\n');
82                 len = end - msg;
83
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;
94                 }
95                 msg += len + 1;
96         }
97         /* didn't find it */
98         return -1;
99 }
100
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)
104 {
105         FILE *file = (FILE *)cb_data;
106         char *msg;
107         unsigned long msglen;
108         enum object_type type;
109         struct rev_note note;
110
111         if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen)) ||
112                         !msglen || type != OBJ_BLOB) {
113                 free(msg);
114                 return 1;
115         }
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;
121 }
122
123 static void regenerate_marks(void)
124 {
125         int ret;
126         FILE *marksfile = xfopen(marksfilename, "w+");
127
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);
132 }
133
134 static void check_or_regenerate_marks(int latestrev)
135 {
136         FILE *marksfile;
137         struct strbuf sb = STRBUF_INIT;
138         struct strbuf line = STRBUF_INIT;
139         int found = 0;
140
141         if (latestrev < 1)
142                 return;
143
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;
156                         }
157                 }
158                 fclose(marksfile);
159                 if (!found)
160                         regenerate_marks();
161         }
162         free_notes(NULL);
163         strbuf_release(&sb);
164         strbuf_release(&line);
165 }
166
167 static int cmd_import(const char *line)
168 {
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";
176
177         if (read_ref(private_ref, head_oid.hash))
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);
190                 }
191         }
192         check_or_regenerate_marks(startrev - 1);
193
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);
204
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;
209         }
210         /* setup marks file import/export */
211         printf("feature import-marks-if-exists=%s\n"
212                         "feature export-marks=%s\n", marksfilename, marksfilename);
213
214         svndump_init_fd(dumpin_fd, STDIN_FILENO);
215         svndump_read(url, private_ref, notes_ref);
216         svndump_deinit();
217         svndump_reset();
218
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);
224         }
225
226         return 0;
227 }
228
229 static int cmd_list(const char *line)
230 {
231         printf("? %s\n\n", remote_ref);
232         fflush(stdout);
233         return 0;
234 }
235
236 static int do_command(struct strbuf *line)
237 {
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;
241         /*
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.
246          */
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. */
256                 }
257                 return 1;       /* end of command stream, quit */
258         }
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;
265         }
266
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;
274                         }
275                         return p->fn(line->buf);
276                 }
277         }
278         die("Unknown command '%s'\n", line->buf);
279         return 0;
280 }
281
282 int cmd_main(int argc, const char **argv)
283 {
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;
289
290         setup_git_directory();
291         if (argc < 2 || argc > 3) {
292                 usage("git-remote-svn <remote-name> [<url>]");
293                 return 1;
294         }
295
296         remote = remote_get(argv[1]);
297         url_in = (argc == 3) ? argv[2] : remote->url[0];
298
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;
306         }
307
308         strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
309         private_ref = private_ref_sb.buf;
310
311         strbuf_addf(&notes_ref_sb, "refs/notes/%s/revs", remote->name);
312         notes_ref = notes_ref_sb.buf;
313
314         strbuf_addf(&marksfilename_sb, "%s/info/fast-import/remote-svn/%s.marks",
315                 get_git_dir(), remote->name);
316         marksfilename = marksfilename_sb.buf;
317
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");
324                 }
325                 if (do_command(&buf))
326                         break;
327                 strbuf_reset(&buf);
328         }
329
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;
336 }