]> git.ipfire.org Git - thirdparty/git.git/blame - vcs-svn/svndump.c
revision: add --grep-reflog to filter commits by reflog messages
[thirdparty/git.git] / vcs-svn / svndump.c
CommitLineData
21746aa3
DB
1/*
2 * Parse and rearrange a svnadmin dump.
3 * Create the dump with:
4 * svnadmin dump --incremental -r<startrev>:<endrev> <repository> >outfile
5 *
6 * Licensed under a two-clause BSD-style license.
7 * See LICENSE for details.
8 */
9
10#include "cache.h"
11#include "repo_tree.h"
12#include "fast_export.h"
13#include "line_buffer.h"
dce33c9c 14#include "strbuf.h"
c5147722 15#include "svndump.h"
21746aa3 16
044ad290
DB
17/*
18 * Compare start of string to literal of equal length;
19 * must be guarded by length test.
20 */
21#define constcmp(s, ref) memcmp(s, ref, sizeof(ref) - 1)
22
41529bbc
DB
23#define REPORT_FILENO 3
24
21746aa3
DB
25#define NODEACT_REPLACE 4
26#define NODEACT_DELETE 3
27#define NODEACT_ADD 2
28#define NODEACT_CHANGE 1
29#define NODEACT_UNKNOWN 0
30
7e11902c
JN
31/* States: */
32#define DUMP_CTX 0 /* dump metadata */
33#define REV_CTX 1 /* revision metadata */
34#define NODE_CTX 2 /* node metadata */
35#define INTERNODE_CTX 3 /* between nodes */
21746aa3 36
21746aa3
DB
37#define DATE_RFC2822_LEN 31
38
e5e45ca1
JN
39static struct line_buffer input = LINE_BUFFER_INIT;
40
21746aa3 41static struct {
e32b79cb
JN
42 uint32_t action, srcRev, type;
43 off_t prop_length, text_length;
03087971 44 struct strbuf src, dst;
1f05d07c 45 uint32_t text_delta, prop_delta;
21746aa3
DB
46} node_ctx;
47
48static struct {
7c5817d3 49 uint32_t revision;
21746aa3 50 unsigned long timestamp;
7c5817d3 51 struct strbuf log, author;
21746aa3
DB
52} rev_ctx;
53
54static struct {
7c5817d3
DB
55 uint32_t version;
56 struct strbuf uuid, url;
21746aa3
DB
57} dump_ctx;
58
21746aa3
DB
59static void reset_node_ctx(char *fname)
60{
61 node_ctx.type = 0;
62 node_ctx.action = NODEACT_UNKNOWN;
e32b79cb 63 node_ctx.prop_length = -1;
150f7546 64 node_ctx.text_length = -1;
03087971 65 strbuf_reset(&node_ctx.src);
21746aa3 66 node_ctx.srcRev = 0;
03087971
DB
67 strbuf_reset(&node_ctx.dst);
68 if (fname)
69 strbuf_addstr(&node_ctx.dst, fname);
1f05d07c
DB
70 node_ctx.text_delta = 0;
71 node_ctx.prop_delta = 0;
21746aa3
DB
72}
73
74static void reset_rev_ctx(uint32_t revision)
75{
76 rev_ctx.revision = revision;
77 rev_ctx.timestamp = 0;
dce33c9c 78 strbuf_reset(&rev_ctx.log);
7c5817d3 79 strbuf_reset(&rev_ctx.author);
21746aa3
DB
80}
81
7c5817d3 82static void reset_dump_ctx(const char *url)
21746aa3 83{
7c5817d3
DB
84 strbuf_reset(&dump_ctx.url);
85 if (url)
86 strbuf_addstr(&dump_ctx.url, url);
b3e5bce1 87 dump_ctx.version = 1;
7c5817d3 88 strbuf_reset(&dump_ctx.uuid);
21746aa3
DB
89}
90
044ad290 91static void handle_property(const struct strbuf *key_buf,
4c3169b0 92 struct strbuf *val,
6b01b676 93 uint32_t *type_set)
2a48afe1 94{
044ad290
DB
95 const char *key = key_buf->buf;
96 size_t keylen = key_buf->len;
97
98 switch (keylen + 1) {
99 case sizeof("svn:log"):
100 if (constcmp(key, "svn:log"))
101 break;
6b01b676
DB
102 if (!val)
103 die("invalid dump: unsets svn:log");
4c3169b0 104 strbuf_swap(&rev_ctx.log, val);
044ad290
DB
105 break;
106 case sizeof("svn:author"):
107 if (constcmp(key, "svn:author"))
108 break;
4c3169b0
JN
109 if (!val)
110 strbuf_reset(&rev_ctx.author);
111 else
112 strbuf_swap(&rev_ctx.author, val);
044ad290
DB
113 break;
114 case sizeof("svn:date"):
115 if (constcmp(key, "svn:date"))
116 break;
6b01b676
DB
117 if (!val)
118 die("invalid dump: unsets svn:date");
4c3169b0
JN
119 if (parse_date_basic(val->buf, &rev_ctx.timestamp, NULL))
120 warning("invalid timestamp: %s", val->buf);
044ad290
DB
121 break;
122 case sizeof("svn:executable"):
123 case sizeof("svn:special"):
124 if (keylen == strlen("svn:executable") &&
125 constcmp(key, "svn:executable"))
126 break;
127 if (keylen == strlen("svn:special") &&
128 constcmp(key, "svn:special"))
129 break;
6b01b676
DB
130 if (*type_set) {
131 if (!val)
132 return;
133 die("invalid dump: sets type twice");
134 }
135 if (!val) {
136 node_ctx.type = REPO_MODE_BLB;
137 return;
138 }
139 *type_set = 1;
044ad290 140 node_ctx.type = keylen == strlen("svn:executable") ?
6b01b676
DB
141 REPO_MODE_EXE :
142 REPO_MODE_LNK;
2a48afe1 143 }
21746aa3
DB
144}
145
c9d1c8ba
JN
146static void die_short_read(void)
147{
148 if (buffer_ferror(&input))
149 die_errno("error reading dump file");
150 die("invalid dump: unexpected end of file");
151}
152
21746aa3
DB
153static void read_props(void)
154{
044ad290 155 static struct strbuf key = STRBUF_INIT;
e7d04ee1 156 static struct strbuf val = STRBUF_INIT;
6263c06d 157 const char *t;
6b01b676
DB
158 /*
159 * NEEDSWORK: to support simple mode changes like
160 * K 11
161 * svn:special
162 * V 1
163 * *
164 * D 14
165 * svn:executable
166 * we keep track of whether a mode has been set and reset to
167 * plain file only if not. We should be keeping track of the
168 * symlink and executable bits separately instead.
169 */
170 uint32_t type_set = 0;
e5e45ca1 171 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
6263c06d 172 uint32_t len;
6263c06d 173 const char type = t[0];
c9d1c8ba 174 int ch;
6263c06d
JN
175
176 if (!type || t[1] != ' ')
82247e9b 177 die("invalid property line: %s", t);
6263c06d 178 len = atoi(&t[2]);
e7d04ee1
JN
179 strbuf_reset(&val);
180 buffer_read_binary(&input, &val, len);
181 if (val.len < len)
c9d1c8ba
JN
182 die_short_read();
183
184 /* Discard trailing newline. */
185 ch = buffer_read_char(&input);
186 if (ch == EOF)
187 die_short_read();
188 if (ch != '\n')
e7d04ee1 189 die("invalid dump: expected newline after %s", val.buf);
6263c06d
JN
190
191 switch (type) {
192 case 'K':
e7d04ee1
JN
193 strbuf_swap(&key, &val);
194 continue;
6b01b676 195 case 'D':
4c3169b0 196 handle_property(&val, NULL, &type_set);
e7d04ee1 197 continue;
6263c06d 198 case 'V':
4c3169b0 199 handle_property(&key, &val, &type_set);
044ad290 200 strbuf_reset(&key);
6263c06d
JN
201 continue;
202 default:
82247e9b 203 die("invalid property line: %s", t);
21746aa3
DB
204 }
205 }
206}
207
208static void handle_node(void)
209{
1c7bb316 210 const uint32_t type = node_ctx.type;
e32b79cb 211 const int have_props = node_ctx.prop_length != -1;
150f7546 212 const int have_text = node_ctx.text_length != -1;
723b7a27
JN
213 /*
214 * Old text for this node:
215 * NULL - directory or bug
216 * empty_blob - empty
217 * "<dataref>" - data retrievable from fast-import
218 */
219 static const char *const empty_blob = "::empty::";
220 const char *old_data = NULL;
7a75e661 221 uint32_t old_mode = REPO_MODE_BLB;
21746aa3
DB
222
223 if (node_ctx.action == NODEACT_DELETE) {
5a38b186 224 if (have_text || have_props || node_ctx.srcRev)
5af8fae2
JN
225 die("invalid dump: deletion node has "
226 "copyfrom info, text, or properties");
c19d653c 227 repo_delete(node_ctx.dst.buf);
9e113988 228 return;
5af8fae2 229 }
6ee4a9be 230 if (node_ctx.action == NODEACT_REPLACE) {
03087971 231 repo_delete(node_ctx.dst.buf);
6ee4a9be
JN
232 node_ctx.action = NODEACT_ADD;
233 }
1c7bb316 234 if (node_ctx.srcRev) {
03087971 235 repo_copy(node_ctx.srcRev, node_ctx.src.buf, node_ctx.dst.buf);
414e569e
JN
236 if (node_ctx.action == NODEACT_ADD)
237 node_ctx.action = NODEACT_CHANGE;
1c7bb316 238 }
5a38b186 239 if (have_text && type == REPO_MODE_DIR)
462e1f51 240 die("invalid dump: directories cannot have text attached");
5a38b186
JN
241
242 /*
723b7a27 243 * Find old content (old_data) and decide on the new mode.
5a38b186 244 */
03087971 245 if (node_ctx.action == NODEACT_CHANGE && !*node_ctx.dst.buf) {
9e8c5321
JN
246 if (type != REPO_MODE_DIR)
247 die("invalid dump: root of tree is not a regular file");
723b7a27 248 old_data = NULL;
9e8c5321 249 } else if (node_ctx.action == NODEACT_CHANGE) {
5a38b186 250 uint32_t mode;
c19d653c 251 old_data = repo_read_path(node_ctx.dst.buf, &mode);
c7dbf35e
JN
252 if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
253 die("invalid dump: cannot modify a directory into a file");
254 if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
255 die("invalid dump: cannot modify a file into a directory");
256 node_ctx.type = mode;
7a75e661 257 old_mode = mode;
21746aa3 258 } else if (node_ctx.action == NODEACT_ADD) {
723b7a27
JN
259 if (type == REPO_MODE_DIR)
260 old_data = NULL;
261 else if (have_text)
262 old_data = empty_blob;
263 else
c7dbf35e 264 die("invalid dump: adds node without text");
c7dbf35e 265 } else {
414e569e 266 die("invalid dump: Node-path block lacks Node-action");
21746aa3 267 }
5a38b186
JN
268
269 /*
270 * Adjust mode to reflect properties.
271 */
1c7bb316 272 if (have_props) {
6b01b676
DB
273 if (!node_ctx.prop_delta)
274 node_ctx.type = type;
e32b79cb 275 if (node_ctx.prop_length)
1c7bb316 276 read_props();
21746aa3 277 }
5a38b186
JN
278
279 /*
280 * Save the result.
281 */
723b7a27
JN
282 if (type == REPO_MODE_DIR) /* directories are not tracked. */
283 return;
284 assert(old_data);
285 if (old_data == empty_blob)
286 /* For the fast_export_* functions, NULL means empty. */
287 old_data = NULL;
288 if (!have_text) {
03087971 289 fast_export_modify(node_ctx.dst.buf, node_ctx.type, old_data);
723b7a27
JN
290 return;
291 }
7a75e661
DB
292 if (!node_ctx.text_delta) {
293 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
150f7546 294 fast_export_data(node_ctx.type, node_ctx.text_length, &input);
7a75e661
DB
295 return;
296 }
03087971 297 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
7a75e661 298 fast_export_blob_delta(node_ctx.type, old_mode, old_data,
150f7546 299 node_ctx.text_length, &input);
21746aa3
DB
300}
301
7e11902c
JN
302static void begin_revision(void)
303{
304 if (!rev_ctx.revision) /* revision 0 gets no git commit. */
305 return;
9ecfa8ae
JN
306 fast_export_begin_commit(rev_ctx.revision, rev_ctx.author.buf,
307 &rev_ctx.log, dump_ctx.uuid.buf, dump_ctx.url.buf,
308 rev_ctx.timestamp);
21746aa3
DB
309}
310
7e11902c 311static void end_revision(void)
21746aa3
DB
312{
313 if (rev_ctx.revision)
723b7a27 314 fast_export_end_commit(rev_ctx.revision);
21746aa3
DB
315}
316
317void svndump_read(const char *url)
318{
319 char *val;
320 char *t;
321 uint32_t active_ctx = DUMP_CTX;
322 uint32_t len;
21746aa3 323
7c5817d3 324 reset_dump_ctx(url);
e5e45ca1 325 while ((t = buffer_read_line(&input))) {
f1602054 326 val = strchr(t, ':');
21746aa3
DB
327 if (!val)
328 continue;
f1602054
DB
329 val++;
330 if (*val != ' ')
331 continue;
332 val++;
21746aa3 333
90c0a3cf
DB
334 /* strlen(key) + 1 */
335 switch (val - t - 1) {
336 case sizeof("SVN-fs-dump-format-version"):
337 if (constcmp(t, "SVN-fs-dump-format-version"))
338 continue;
b3e5bce1 339 dump_ctx.version = atoi(val);
1f05d07c 340 if (dump_ctx.version > 3)
a62bbf8f 341 die("expected svn dump format version <= 3, found %"PRIu32,
b3e5bce1 342 dump_ctx.version);
90c0a3cf
DB
343 break;
344 case sizeof("UUID"):
345 if (constcmp(t, "UUID"))
346 continue;
7c5817d3
DB
347 strbuf_reset(&dump_ctx.uuid);
348 strbuf_addstr(&dump_ctx.uuid, val);
90c0a3cf
DB
349 break;
350 case sizeof("Revision-number"):
351 if (constcmp(t, "Revision-number"))
352 continue;
21746aa3
DB
353 if (active_ctx == NODE_CTX)
354 handle_node();
7e11902c
JN
355 if (active_ctx == REV_CTX)
356 begin_revision();
21746aa3 357 if (active_ctx != DUMP_CTX)
7e11902c 358 end_revision();
21746aa3
DB
359 active_ctx = REV_CTX;
360 reset_rev_ctx(atoi(val));
90c0a3cf
DB
361 break;
362 case sizeof("Node-path"):
d8d8708b 363 if (constcmp(t, "Node-"))
90c0a3cf
DB
364 continue;
365 if (!constcmp(t + strlen("Node-"), "path")) {
366 if (active_ctx == NODE_CTX)
367 handle_node();
9ecfa8ae
JN
368 if (active_ctx == REV_CTX)
369 begin_revision();
90c0a3cf
DB
370 active_ctx = NODE_CTX;
371 reset_node_ctx(val);
372 break;
373 }
374 if (constcmp(t + strlen("Node-"), "kind"))
375 continue;
21746aa3
DB
376 if (!strcmp(val, "dir"))
377 node_ctx.type = REPO_MODE_DIR;
378 else if (!strcmp(val, "file"))
379 node_ctx.type = REPO_MODE_BLB;
380 else
381 fprintf(stderr, "Unknown node-kind: %s\n", val);
90c0a3cf
DB
382 break;
383 case sizeof("Node-action"):
384 if (constcmp(t, "Node-action"))
385 continue;
21746aa3
DB
386 if (!strcmp(val, "delete")) {
387 node_ctx.action = NODEACT_DELETE;
388 } else if (!strcmp(val, "add")) {
389 node_ctx.action = NODEACT_ADD;
390 } else if (!strcmp(val, "change")) {
391 node_ctx.action = NODEACT_CHANGE;
392 } else if (!strcmp(val, "replace")) {
393 node_ctx.action = NODEACT_REPLACE;
394 } else {
395 fprintf(stderr, "Unknown node-action: %s\n", val);
396 node_ctx.action = NODEACT_UNKNOWN;
397 }
90c0a3cf
DB
398 break;
399 case sizeof("Node-copyfrom-path"):
400 if (constcmp(t, "Node-copyfrom-path"))
401 continue;
03087971
DB
402 strbuf_reset(&node_ctx.src);
403 strbuf_addstr(&node_ctx.src, val);
90c0a3cf
DB
404 break;
405 case sizeof("Node-copyfrom-rev"):
406 if (constcmp(t, "Node-copyfrom-rev"))
407 continue;
21746aa3 408 node_ctx.srcRev = atoi(val);
90c0a3cf
DB
409 break;
410 case sizeof("Text-content-length"):
e32b79cb
JN
411 if (constcmp(t, "Text") && constcmp(t, "Prop"))
412 continue;
413 if (constcmp(t + 4, "-content-length"))
414 continue;
415 {
150f7546 416 char *end;
e32b79cb 417 uintmax_t len;
150f7546 418
e32b79cb 419 len = strtoumax(val, &end, 10);
150f7546
JN
420 if (!isdigit(*val) || *end)
421 die("invalid dump: non-numeric length %s", val);
e32b79cb 422 if (len > maximum_signed_value_of_type(off_t))
150f7546 423 die("unrepresentable length in dump: %s", val);
e32b79cb
JN
424
425 if (*t == 'T')
426 node_ctx.text_length = (off_t) len;
427 else
428 node_ctx.prop_length = (off_t) len;
90c0a3cf
DB
429 break;
430 }
90c0a3cf
DB
431 case sizeof("Text-delta"):
432 if (!constcmp(t, "Text-delta")) {
433 node_ctx.text_delta = !strcmp(val, "true");
434 break;
435 }
436 if (constcmp(t, "Prop-delta"))
437 continue;
1f05d07c 438 node_ctx.prop_delta = !strcmp(val, "true");
90c0a3cf
DB
439 break;
440 case sizeof("Content-length"):
441 if (constcmp(t, "Content-length"))
442 continue;
21746aa3 443 len = atoi(val);
c9d1c8ba
JN
444 t = buffer_read_line(&input);
445 if (!t)
446 die_short_read();
447 if (*t)
448 die("invalid dump: expected blank line after content length header");
21746aa3
DB
449 if (active_ctx == REV_CTX) {
450 read_props();
451 } else if (active_ctx == NODE_CTX) {
452 handle_node();
7e11902c 453 active_ctx = INTERNODE_CTX;
21746aa3 454 } else {
5418d96d 455 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
c9d1c8ba
JN
456 if (buffer_skip_bytes(&input, len) != len)
457 die_short_read();
21746aa3
DB
458 }
459 }
460 }
c9d1c8ba
JN
461 if (buffer_ferror(&input))
462 die_short_read();
21746aa3
DB
463 if (active_ctx == NODE_CTX)
464 handle_node();
7e11902c
JN
465 if (active_ctx == REV_CTX)
466 begin_revision();
21746aa3 467 if (active_ctx != DUMP_CTX)
7e11902c 468 end_revision();
21746aa3
DB
469}
470
5c28a8b0 471int svndump_init(const char *filename)
21746aa3 472{
a62bbf8f 473 if (buffer_init(&input, filename))
5c28a8b0 474 return error("cannot open %s: %s", filename, strerror(errno));
41529bbc 475 fast_export_init(REPORT_FILENO);
7c5817d3
DB
476 strbuf_init(&dump_ctx.uuid, 4096);
477 strbuf_init(&dump_ctx.url, 4096);
dce33c9c 478 strbuf_init(&rev_ctx.log, 4096);
7c5817d3 479 strbuf_init(&rev_ctx.author, 4096);
03087971
DB
480 strbuf_init(&node_ctx.src, 4096);
481 strbuf_init(&node_ctx.dst, 4096);
7c5817d3 482 reset_dump_ctx(NULL);
21746aa3
DB
483 reset_rev_ctx(0);
484 reset_node_ctx(NULL);
5c28a8b0 485 return 0;
21746aa3
DB
486}
487
488void svndump_deinit(void)
489{
41529bbc 490 fast_export_deinit();
7c5817d3 491 reset_dump_ctx(NULL);
21746aa3
DB
492 reset_rev_ctx(0);
493 reset_node_ctx(NULL);
dce33c9c 494 strbuf_release(&rev_ctx.log);
03087971
DB
495 strbuf_release(&node_ctx.src);
496 strbuf_release(&node_ctx.dst);
e5e45ca1 497 if (buffer_deinit(&input))
21746aa3
DB
498 fprintf(stderr, "Input error\n");
499 if (ferror(stdout))
500 fprintf(stderr, "Output error\n");
501}
502
503void svndump_reset(void)
504{
7c5817d3
DB
505 strbuf_release(&dump_ctx.uuid);
506 strbuf_release(&dump_ctx.url);
507 strbuf_release(&rev_ctx.log);
508 strbuf_release(&rev_ctx.author);
21746aa3 509}