sha1_file: convert read_object_with_reference to object_id
[git/git.git] / remote-testsvn.c
CommitLineData
68f64ff8 1#include "cache.h"
fb58c8d5 2#include "refs.h"
68f64ff8
FA
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
12static const char *url;
f6529de9 13static int dump_from_file;
68f64ff8
FA
14static const char *private_ref;
15static const char *remote_ref = "refs/heads/master";
8e43a1d0
FA
16static const char *marksfilename, *notes_ref;
17struct rev_note { unsigned int rev_nr; };
68f64ff8
FA
18
19static int cmd_capabilities(const char *line);
20static int cmd_import(const char *line);
21static int cmd_list(const char *line);
22
23typedef int (*input_command_handler)(const char *);
24struct 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
30static 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
37static 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
46static void terminate_batch(void)
47{
48 /* terminate a current batch's fast-import stream */
49 printf("done\n");
50 fflush(stdout);
51}
52
8e43a1d0 53/* NOTE: 'ref' refers to a git reference, while 'rev' refers to a svn revision. */
5ee8a954 54static char *read_ref_note(const struct object_id *oid)
8e43a1d0 55{
9ef72230 56 const struct object_id *note_oid;
8e43a1d0
FA
57 char *msg = NULL;
58 unsigned long msglen;
59 enum object_type type;
60
61 init_notes(NULL, notes_ref, NULL, 0);
5ee8a954 62 if (!(note_oid = get_note(NULL, oid)))
8e43a1d0 63 return NULL; /* note tree not found */
9ef72230 64 if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen)))
8e43a1d0
FA
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);
6a83d902 69 FREE_AND_NULL(msg);
8e43a1d0
FA
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
490bc83a 101static int note2mark_cb(const struct object_id *object_oid,
102 const struct object_id *note_oid, char *note_path,
5bfc76b5
FA
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
490bc83a 111 if (!(msg = read_sha1_file(note_oid->hash, &type, &msglen)) ||
5bfc76b5
FA
112 !msglen || type != OBJ_BLOB) {
113 free(msg);
114 return 1;
115 }
116 if (parse_rev_note(msg, &note))
117 return 2;
490bc83a 118 if (fprintf(file, ":%d %s\n", note.rev_nr, oid_to_hex(object_oid)) < 1)
5bfc76b5
FA
119 return 3;
120 return 0;
121}
122
123static void regenerate_marks(void)
124{
125 int ret;
23a9e071 126 FILE *marksfile = xfopen(marksfilename, "w+");
5bfc76b5 127
5bfc76b5
FA
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
134static 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();
23a9e071 148 marksfile = xfopen(marksfilename, "r");
5bfc76b5
FA
149 fclose(marksfile);
150 } else {
151 strbuf_addf(&sb, ":%d ", latestrev);
8f309aeb 152 while (strbuf_getline_lf(&line, marksfile) != EOF) {
59556548 153 if (starts_with(line.buf, sb.buf)) {
5bfc76b5
FA
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
68f64ff8
FA
167static int cmd_import(const char *line)
168{
169 int code;
170 int dumpin_fd;
8e43a1d0 171 char *note_msg;
5ee8a954 172 struct object_id head_oid;
8e43a1d0 173 unsigned int startrev;
d3180279 174 struct child_process svndump_proc = CHILD_PROCESS_INIT;
1cefa143 175 const char *command = "svnrdump";
68f64ff8 176
34c290a6 177 if (read_ref(private_ref, &head_oid))
8e43a1d0
FA
178 startrev = 0;
179 else {
5ee8a954 180 note_msg = read_ref_note(&head_oid);
8e43a1d0
FA
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 }
5bfc76b5 192 check_or_regenerate_marks(startrev - 1);
8e43a1d0 193
f6529de9
FA
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 {
f6529de9 199 svndump_proc.out = -1;
1cefa143
RS
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);
f6529de9
FA
204
205 code = start_command(&svndump_proc);
206 if (code)
1cefa143 207 die("Unable to start %s, code %d", command, code);
f6529de9
FA
208 dumpin_fd = svndump_proc.out;
209 }
8d7cd8eb
FA
210 /* setup marks file import/export */
211 printf("feature import-marks-if-exists=%s\n"
212 "feature export-marks=%s\n", marksfilename, marksfilename);
213
68f64ff8 214 svndump_init_fd(dumpin_fd, STDIN_FILENO);
8e43a1d0 215 svndump_read(url, private_ref, notes_ref);
68f64ff8
FA
216 svndump_deinit();
217 svndump_reset();
218
219 close(dumpin_fd);
f6529de9
FA
220 if (!dump_from_file) {
221 code = finish_command(&svndump_proc);
222 if (code)
1cefa143 223 warning("%s, returned %d", command, code);
f6529de9 224 }
68f64ff8
FA
225
226 return 0;
227}
228
229static int cmd_list(const char *line)
230{
231 printf("? %s\n\n", remote_ref);
232 fflush(stdout);
233 return 0;
234}
235
236static 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) {
59556548 260 if (!starts_with(batch_cmd->name, line->buf))
68f64ff8
FA
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++) {
59556548 268 if (starts_with(line->buf, p->name) && (strlen(p->name) == line->len ||
68f64ff8
FA
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
3f2e2297 282int cmd_main(int argc, const char **argv)
68f64ff8
FA
283{
284 struct strbuf buf = STRBUF_INIT, url_sb = STRBUF_INIT,
8e43a1d0
FA
285 private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT,
286 notes_ref_sb = STRBUF_INIT;
68f64ff8
FA
287 static struct remote *remote;
288 const char *url_in;
289
68f64ff8
FA
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
59556548 299 if (starts_with(url_in, "file://")) {
f6529de9
FA
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 }
68f64ff8
FA
307
308 strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
309 private_ref = private_ref_sb.buf;
310
8e43a1d0
FA
311 strbuf_addf(&notes_ref_sb, "refs/notes/%s/revs", remote->name);
312 notes_ref = notes_ref_sb.buf;
313
8d7cd8eb
FA
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
68f64ff8 318 while (1) {
8f309aeb 319 if (strbuf_getline_lf(&buf, stdin) == EOF) {
68f64ff8
FA
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);
8e43a1d0 333 strbuf_release(&notes_ref_sb);
8d7cd8eb 334 strbuf_release(&marksfilename_sb);
68f64ff8
FA
335 return 0;
336}