]> git.ipfire.org Git - thirdparty/git.git/blame - remote-testsvn.c
initial_ref_transaction_commit(): check for ref D/F conflicts
[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) {
2c5495f7
RM
81 end = strchrnul(msg, '\n');
82 len = end - msg;
8e43a1d0
FA
83
84 key = "Revision-number: ";
59556548 85 if (starts_with(msg, key)) {
8e43a1d0
FA
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;
bfae342c 93 return 0;
8e43a1d0
FA
94 }
95 msg += len + 1;
96 }
bfae342c
JK
97 /* didn't find it */
98 return -1;
8e43a1d0
FA
99}
100
5bfc76b5
FA
101static int note2mark_cb(const unsigned char *object_sha1,
102 const unsigned char *note_sha1, 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_sha1, &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, sha1_to_hex(object_sha1)) < 1)
119 return 3;
120 return 0;
121}
122
123static void regenerate_marks(void)
124{
125 int ret;
126 FILE *marksfile = fopen(marksfilename, "w+");
127
128 if (!marksfile)
129 die_errno("Couldn't create mark file %s.", marksfilename);
130 ret = for_each_note(NULL, 0, note2mark_cb, marksfile);
131 if (ret)
132 die("Regeneration of marks failed, returned %d.", ret);
133 fclose(marksfile);
134}
135
136static void check_or_regenerate_marks(int latestrev)
137{
138 FILE *marksfile;
139 struct strbuf sb = STRBUF_INIT;
140 struct strbuf line = STRBUF_INIT;
141 int found = 0;
142
143 if (latestrev < 1)
144 return;
145
146 init_notes(NULL, notes_ref, NULL, 0);
147 marksfile = fopen(marksfilename, "r");
148 if (!marksfile) {
149 regenerate_marks();
150 marksfile = fopen(marksfilename, "r");
151 if (!marksfile)
152 die_errno("cannot read marks file %s!", marksfilename);
153 fclose(marksfile);
154 } else {
155 strbuf_addf(&sb, ":%d ", latestrev);
156 while (strbuf_getline(&line, marksfile, '\n') != EOF) {
59556548 157 if (starts_with(line.buf, sb.buf)) {
5bfc76b5
FA
158 found++;
159 break;
160 }
161 }
162 fclose(marksfile);
163 if (!found)
164 regenerate_marks();
165 }
166 free_notes(NULL);
167 strbuf_release(&sb);
168 strbuf_release(&line);
169}
170
68f64ff8
FA
171static int cmd_import(const char *line)
172{
173 int code;
174 int dumpin_fd;
8e43a1d0
FA
175 char *note_msg;
176 unsigned char head_sha1[20];
177 unsigned int startrev;
d3180279 178 struct child_process svndump_proc = CHILD_PROCESS_INIT;
1cefa143 179 const char *command = "svnrdump";
68f64ff8 180
8e43a1d0
FA
181 if (read_ref(private_ref, head_sha1))
182 startrev = 0;
183 else {
184 note_msg = read_ref_note(head_sha1);
185 if(note_msg == NULL) {
186 warning("No note found for %s.", private_ref);
187 startrev = 0;
188 } else {
189 struct rev_note note = { 0 };
190 if (parse_rev_note(note_msg, &note))
191 die("Revision number couldn't be parsed from note.");
192 startrev = note.rev_nr + 1;
193 free(note_msg);
194 }
195 }
5bfc76b5 196 check_or_regenerate_marks(startrev - 1);
8e43a1d0 197
f6529de9
FA
198 if (dump_from_file) {
199 dumpin_fd = open(url, O_RDONLY);
200 if(dumpin_fd < 0)
201 die_errno("Couldn't open svn dump file %s.", url);
202 } else {
f6529de9 203 svndump_proc.out = -1;
1cefa143
RS
204 argv_array_push(&svndump_proc.args, command);
205 argv_array_push(&svndump_proc.args, "dump");
206 argv_array_push(&svndump_proc.args, url);
207 argv_array_pushf(&svndump_proc.args, "-r%u:HEAD", startrev);
f6529de9
FA
208
209 code = start_command(&svndump_proc);
210 if (code)
1cefa143 211 die("Unable to start %s, code %d", command, code);
f6529de9
FA
212 dumpin_fd = svndump_proc.out;
213 }
8d7cd8eb
FA
214 /* setup marks file import/export */
215 printf("feature import-marks-if-exists=%s\n"
216 "feature export-marks=%s\n", marksfilename, marksfilename);
217
68f64ff8 218 svndump_init_fd(dumpin_fd, STDIN_FILENO);
8e43a1d0 219 svndump_read(url, private_ref, notes_ref);
68f64ff8
FA
220 svndump_deinit();
221 svndump_reset();
222
223 close(dumpin_fd);
f6529de9
FA
224 if (!dump_from_file) {
225 code = finish_command(&svndump_proc);
226 if (code)
1cefa143 227 warning("%s, returned %d", command, code);
f6529de9 228 }
68f64ff8
FA
229
230 return 0;
231}
232
233static int cmd_list(const char *line)
234{
235 printf("? %s\n\n", remote_ref);
236 fflush(stdout);
237 return 0;
238}
239
240static int do_command(struct strbuf *line)
241{
242 const struct input_command_entry *p = input_command_list;
243 static struct string_list batchlines = STRING_LIST_INIT_DUP;
244 static const struct input_command_entry *batch_cmd;
245 /*
246 * commands can be grouped together in a batch.
247 * Batches are ended by \n. If no batch is active the program ends.
248 * During a batch all lines are buffered and passed to the handler function
249 * when the batch is terminated.
250 */
251 if (line->len == 0) {
252 if (batch_cmd) {
253 struct string_list_item *item;
254 for_each_string_list_item(item, &batchlines)
255 batch_cmd->fn(item->string);
256 terminate_batch();
257 batch_cmd = NULL;
258 string_list_clear(&batchlines, 0);
259 return 0; /* end of the batch, continue reading other commands. */
260 }
261 return 1; /* end of command stream, quit */
262 }
263 if (batch_cmd) {
59556548 264 if (!starts_with(batch_cmd->name, line->buf))
68f64ff8
FA
265 die("Active %s batch interrupted by %s", batch_cmd->name, line->buf);
266 /* buffer batch lines */
267 string_list_append(&batchlines, line->buf);
268 return 0;
269 }
270
271 for (p = input_command_list; p->name; p++) {
59556548 272 if (starts_with(line->buf, p->name) && (strlen(p->name) == line->len ||
68f64ff8
FA
273 line->buf[strlen(p->name)] == ' ')) {
274 if (p->batchable) {
275 batch_cmd = p;
276 string_list_append(&batchlines, line->buf);
277 return 0;
278 }
279 return p->fn(line->buf);
280 }
281 }
282 die("Unknown command '%s'\n", line->buf);
283 return 0;
284}
285
84d32bf7 286int main(int argc, char **argv)
68f64ff8
FA
287{
288 struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
8e43a1d0
FA
289 private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT,
290 notes_ref_sb = STRBUF_INIT;
68f64ff8
FA
291 static struct remote *remote;
292 const char *url_in;
293
294 git_extract_argv0_path(argv[0]);
295 setup_git_directory();
296 if (argc < 2 || argc > 3) {
297 usage("git-remote-svn <remote-name> [<url>]");
298 return 1;
299 }
300
301 remote = remote_get(argv[1]);
302 url_in = (argc == 3) ? argv[2] : remote->url[0];
303
59556548 304 if (starts_with(url_in, "file://")) {
f6529de9
FA
305 dump_from_file = 1;
306 url = url_decode(url_in + sizeof("file://")-1);
307 } else {
308 dump_from_file = 0;
309 end_url_with_slash(&url_sb, url_in);
310 url = url_sb.buf;
311 }
68f64ff8
FA
312
313 strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
314 private_ref = private_ref_sb.buf;
315
8e43a1d0
FA
316 strbuf_addf(&notes_ref_sb, "refs/notes/%s/revs", remote->name);
317 notes_ref = notes_ref_sb.buf;
318
8d7cd8eb
FA
319 strbuf_addf(&marksfilename_sb, "%s/info/fast-import/remote-svn/%s.marks",
320 get_git_dir(), remote->name);
321 marksfilename = marksfilename_sb.buf;
322
68f64ff8
FA
323 while (1) {
324 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
325 if (ferror(stdin))
326 die("Error reading command stream");
327 else
328 die("Unexpected end of command stream");
329 }
330 if (do_command(&buf))
331 break;
332 strbuf_reset(&buf);
333 }
334
335 strbuf_release(&buf);
336 strbuf_release(&url_sb);
337 strbuf_release(&private_ref_sb);
8e43a1d0 338 strbuf_release(&notes_ref_sb);
8d7cd8eb 339 strbuf_release(&marksfilename_sb);
68f64ff8
FA
340 return 0;
341}