vcs-svn: avoid using ls command twice
[git/mingw.git] / vcs-svn / svndump.c
blob99a5ba0d1008083a2a0c868d1984f44c03ba4232
1 /*
2 * Parse and rearrange a svnadmin dump.
3 * Create the dump with:
4 * svnadmin dump --incremental -r<startrev>:<endrev> <repository> >outfile
6 * Licensed under a two-clause BSD-style license.
7 * See LICENSE for details.
8 */
10 #include "cache.h"
11 #include "repo_tree.h"
12 #include "fast_export.h"
13 #include "line_buffer.h"
14 #include "obj_pool.h"
15 #include "string_pool.h"
17 #define REPORT_FILENO 3
19 #define NODEACT_REPLACE 4
20 #define NODEACT_DELETE 3
21 #define NODEACT_ADD 2
22 #define NODEACT_CHANGE 1
23 #define NODEACT_UNKNOWN 0
25 /* States: */
26 #define DUMP_CTX 0 /* dump metadata */
27 #define REV_CTX 1 /* revision metadata */
28 #define NODE_CTX 2 /* node metadata */
29 #define INTERNODE_CTX 3 /* between nodes */
31 #define LENGTH_UNKNOWN (~0)
32 #define DATE_RFC2822_LEN 31
34 /* Create memory pool for log messages */
35 obj_pool_gen(log, char, 4096)
37 static struct line_buffer input = LINE_BUFFER_INIT;
39 #define REPORT_FILENO 3
41 static char *log_copy(uint32_t length, const char *log)
43 char *buffer;
44 log_free(log_pool.size);
45 buffer = log_pointer(log_alloc(length));
46 strncpy(buffer, log, length);
47 return buffer;
50 static struct {
51 uint32_t action, propLength, textLength, srcRev, type;
52 uint32_t src[REPO_MAX_PATH_DEPTH], dst[REPO_MAX_PATH_DEPTH];
53 uint32_t text_delta, prop_delta;
54 } node_ctx;
56 static struct {
57 uint32_t revision, author;
58 unsigned long timestamp;
59 char *log;
60 } rev_ctx;
62 static struct {
63 uint32_t version, uuid, url;
64 } dump_ctx;
66 static struct {
67 uint32_t svn_log, svn_author, svn_date, svn_executable, svn_special, uuid,
68 revision_number, node_path, node_kind, node_action,
69 node_copyfrom_path, node_copyfrom_rev, text_content_length,
70 prop_content_length, content_length, svn_fs_dump_format_version,
71 /* version 3 format */
72 text_delta, prop_delta;
73 } keys;
75 static void reset_node_ctx(char *fname)
77 node_ctx.type = 0;
78 node_ctx.action = NODEACT_UNKNOWN;
79 node_ctx.propLength = LENGTH_UNKNOWN;
80 node_ctx.textLength = LENGTH_UNKNOWN;
81 node_ctx.src[0] = ~0;
82 node_ctx.srcRev = 0;
83 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.dst, "/", fname);
84 node_ctx.text_delta = 0;
85 node_ctx.prop_delta = 0;
88 static void reset_rev_ctx(uint32_t revision)
90 rev_ctx.revision = revision;
91 rev_ctx.timestamp = 0;
92 rev_ctx.log = NULL;
93 rev_ctx.author = ~0;
96 static void reset_dump_ctx(uint32_t url)
98 dump_ctx.url = url;
99 dump_ctx.version = 1;
100 dump_ctx.uuid = ~0;
103 static void init_keys(void)
105 keys.svn_log = pool_intern("svn:log");
106 keys.svn_author = pool_intern("svn:author");
107 keys.svn_date = pool_intern("svn:date");
108 keys.svn_executable = pool_intern("svn:executable");
109 keys.svn_special = pool_intern("svn:special");
110 keys.uuid = pool_intern("UUID");
111 keys.revision_number = pool_intern("Revision-number");
112 keys.node_path = pool_intern("Node-path");
113 keys.node_kind = pool_intern("Node-kind");
114 keys.node_action = pool_intern("Node-action");
115 keys.node_copyfrom_path = pool_intern("Node-copyfrom-path");
116 keys.node_copyfrom_rev = pool_intern("Node-copyfrom-rev");
117 keys.text_content_length = pool_intern("Text-content-length");
118 keys.prop_content_length = pool_intern("Prop-content-length");
119 keys.content_length = pool_intern("Content-length");
120 keys.svn_fs_dump_format_version = pool_intern("SVN-fs-dump-format-version");
121 /* version 3 format (Subversion 1.1.0) */
122 keys.text_delta = pool_intern("Text-delta");
123 keys.prop_delta = pool_intern("Prop-delta");
126 static void handle_property(uint32_t key, const char *val, uint32_t len,
127 uint32_t *type_set)
129 if (key == keys.svn_log) {
130 if (!val)
131 die("invalid dump: unsets svn:log");
132 /* Value length excludes terminating nul. */
133 rev_ctx.log = log_copy(len + 1, val);
134 } else if (key == keys.svn_author) {
135 rev_ctx.author = pool_intern(val);
136 } else if (key == keys.svn_date) {
137 if (!val)
138 die("invalid dump: unsets svn:date");
139 if (parse_date_basic(val, &rev_ctx.timestamp, NULL))
140 warning("invalid timestamp: %s", val);
141 } else if (key == keys.svn_executable || key == keys.svn_special) {
142 if (*type_set) {
143 if (!val)
144 return;
145 die("invalid dump: sets type twice");
147 if (!val) {
148 node_ctx.type = REPO_MODE_BLB;
149 return;
151 *type_set = 1;
152 node_ctx.type = key == keys.svn_executable ?
153 REPO_MODE_EXE :
154 REPO_MODE_LNK;
158 static void read_props(void)
160 uint32_t key = ~0;
161 const char *t;
163 * NEEDSWORK: to support simple mode changes like
164 * K 11
165 * svn:special
166 * V 1
168 * D 14
169 * svn:executable
170 * we keep track of whether a mode has been set and reset to
171 * plain file only if not. We should be keeping track of the
172 * symlink and executable bits separately instead.
174 uint32_t type_set = 0;
175 while ((t = buffer_read_line(&input)) && strcmp(t, "PROPS-END")) {
176 uint32_t len;
177 const char *val;
178 const char type = t[0];
180 if (!type || t[1] != ' ')
181 die("invalid property line: %s\n", t);
182 len = atoi(&t[2]);
183 val = buffer_read_string(&input, len);
184 buffer_skip_bytes(&input, 1); /* Discard trailing newline. */
186 switch (type) {
187 case 'K':
188 key = pool_intern(val);
189 continue;
190 case 'D':
191 key = pool_intern(val);
192 val = NULL;
193 len = 0;
194 /* fall through */
195 case 'V':
196 handle_property(key, val, len, &type_set);
197 key = ~0;
198 continue;
199 default:
200 die("invalid property line: %s\n", t);
205 static void handle_node(void)
207 const uint32_t type = node_ctx.type;
208 const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
209 const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
211 * Old text for this node:
212 * NULL - directory or bug
213 * empty_blob - empty
214 * "<dataref>" - data retrievable from fast-import
216 static const char *const empty_blob = "::empty::";
217 const char *old_data = NULL;
219 if (node_ctx.text_delta)
220 die("text deltas not supported");
222 if (node_ctx.action == NODEACT_DELETE) {
223 if (have_text || have_props || node_ctx.srcRev)
224 die("invalid dump: deletion node has "
225 "copyfrom info, text, or properties");
226 return repo_delete(node_ctx.dst);
228 if (node_ctx.action == NODEACT_REPLACE) {
229 repo_delete(node_ctx.dst);
230 node_ctx.action = NODEACT_ADD;
232 if (node_ctx.srcRev) {
233 repo_copy(node_ctx.srcRev, node_ctx.src, node_ctx.dst);
234 if (node_ctx.action == NODEACT_ADD)
235 node_ctx.action = NODEACT_CHANGE;
237 if (have_text && type == REPO_MODE_DIR)
238 die("invalid dump: directories cannot have text attached");
241 * Find old content (old_data) and decide on the new mode.
243 if (node_ctx.action == NODEACT_CHANGE && !~*node_ctx.dst) {
244 if (type != REPO_MODE_DIR)
245 die("invalid dump: root of tree is not a regular file");
246 old_data = NULL;
247 } else if (node_ctx.action == NODEACT_CHANGE) {
248 uint32_t mode;
249 old_data = repo_read_path(node_ctx.dst, &mode);
250 if (mode == REPO_MODE_DIR && type != REPO_MODE_DIR)
251 die("invalid dump: cannot modify a directory into a file");
252 if (mode != REPO_MODE_DIR && type == REPO_MODE_DIR)
253 die("invalid dump: cannot modify a file into a directory");
254 node_ctx.type = mode;
255 } else if (node_ctx.action == NODEACT_ADD) {
256 if (type == REPO_MODE_DIR)
257 old_data = NULL;
258 else if (have_text)
259 old_data = empty_blob;
260 else
261 die("invalid dump: adds node without text");
262 } else {
263 die("invalid dump: Node-path block lacks Node-action");
267 * Adjust mode to reflect properties.
269 if (have_props) {
270 if (!node_ctx.prop_delta)
271 node_ctx.type = type;
272 if (node_ctx.propLength)
273 read_props();
277 * Save the result.
279 if (type == REPO_MODE_DIR) /* directories are not tracked. */
280 return;
281 assert(old_data);
282 if (old_data == empty_blob)
283 /* For the fast_export_* functions, NULL means empty. */
284 old_data = NULL;
285 if (!have_text) {
286 fast_export_modify(REPO_MAX_PATH_DEPTH, node_ctx.dst,
287 node_ctx.type, old_data);
288 return;
290 fast_export_modify(REPO_MAX_PATH_DEPTH, node_ctx.dst,
291 node_ctx.type, "inline");
292 fast_export_data(node_ctx.type, node_ctx.textLength, &input);
295 static void begin_revision(void)
297 if (!rev_ctx.revision) /* revision 0 gets no git commit. */
298 return;
299 fast_export_begin_commit(rev_ctx.revision, rev_ctx.author, rev_ctx.log,
300 dump_ctx.uuid, dump_ctx.url, rev_ctx.timestamp);
303 static void end_revision(void)
305 if (rev_ctx.revision)
306 fast_export_end_commit(rev_ctx.revision);
309 void svndump_read(const char *url)
311 char *val;
312 char *t;
313 uint32_t active_ctx = DUMP_CTX;
314 uint32_t len;
315 uint32_t key;
317 reset_dump_ctx(pool_intern(url));
318 while ((t = buffer_read_line(&input))) {
319 val = strstr(t, ": ");
320 if (!val)
321 continue;
322 *val++ = '\0';
323 *val++ = '\0';
324 key = pool_intern(t);
326 if (key == keys.svn_fs_dump_format_version) {
327 dump_ctx.version = atoi(val);
328 if (dump_ctx.version > 3)
329 die("expected svn dump format version <= 3, found %"PRIu32,
330 dump_ctx.version);
331 } else if (key == keys.uuid) {
332 dump_ctx.uuid = pool_intern(val);
333 } else if (key == keys.revision_number) {
334 if (active_ctx == NODE_CTX)
335 handle_node();
336 if (active_ctx == REV_CTX)
337 begin_revision();
338 if (active_ctx != DUMP_CTX)
339 end_revision();
340 active_ctx = REV_CTX;
341 reset_rev_ctx(atoi(val));
342 } else if (key == keys.node_path) {
343 if (active_ctx == NODE_CTX)
344 handle_node();
345 if (active_ctx == REV_CTX)
346 begin_revision();
347 active_ctx = NODE_CTX;
348 reset_node_ctx(val);
349 } else if (key == keys.node_kind) {
350 if (!strcmp(val, "dir"))
351 node_ctx.type = REPO_MODE_DIR;
352 else if (!strcmp(val, "file"))
353 node_ctx.type = REPO_MODE_BLB;
354 else
355 fprintf(stderr, "Unknown node-kind: %s\n", val);
356 } else if (key == keys.node_action) {
357 if (!strcmp(val, "delete")) {
358 node_ctx.action = NODEACT_DELETE;
359 } else if (!strcmp(val, "add")) {
360 node_ctx.action = NODEACT_ADD;
361 } else if (!strcmp(val, "change")) {
362 node_ctx.action = NODEACT_CHANGE;
363 } else if (!strcmp(val, "replace")) {
364 node_ctx.action = NODEACT_REPLACE;
365 } else {
366 fprintf(stderr, "Unknown node-action: %s\n", val);
367 node_ctx.action = NODEACT_UNKNOWN;
369 } else if (key == keys.node_copyfrom_path) {
370 pool_tok_seq(REPO_MAX_PATH_DEPTH, node_ctx.src, "/", val);
371 } else if (key == keys.node_copyfrom_rev) {
372 node_ctx.srcRev = atoi(val);
373 } else if (key == keys.text_content_length) {
374 node_ctx.textLength = atoi(val);
375 } else if (key == keys.prop_content_length) {
376 node_ctx.propLength = atoi(val);
377 } else if (key == keys.text_delta) {
378 node_ctx.text_delta = !strcmp(val, "true");
379 } else if (key == keys.prop_delta) {
380 node_ctx.prop_delta = !strcmp(val, "true");
381 } else if (key == keys.content_length) {
382 len = atoi(val);
383 buffer_read_line(&input);
384 if (active_ctx == REV_CTX) {
385 read_props();
386 } else if (active_ctx == NODE_CTX) {
387 handle_node();
388 active_ctx = INTERNODE_CTX;
389 } else {
390 fprintf(stderr, "Unexpected content length header: %"PRIu32"\n", len);
391 buffer_skip_bytes(&input, len);
395 if (active_ctx == NODE_CTX)
396 handle_node();
397 if (active_ctx == REV_CTX)
398 begin_revision();
399 if (active_ctx != DUMP_CTX)
400 end_revision();
403 int svndump_init(const char *filename)
405 if (buffer_init(&input, filename))
406 return error("cannot open %s: %s", filename, strerror(errno));
407 fast_export_init(REPORT_FILENO);
408 reset_dump_ctx(~0);
409 reset_rev_ctx(0);
410 reset_node_ctx(NULL);
411 init_keys();
412 return 0;
415 void svndump_deinit(void)
417 log_reset();
418 fast_export_deinit();
419 reset_dump_ctx(~0);
420 reset_rev_ctx(0);
421 reset_node_ctx(NULL);
422 if (buffer_deinit(&input))
423 fprintf(stderr, "Input error\n");
424 if (ferror(stdout))
425 fprintf(stderr, "Output error\n");
428 void svndump_reset(void)
430 log_reset();
431 fast_export_reset();
432 buffer_reset(&input);
433 reset_dump_ctx(~0);
434 reset_rev_ctx(0);
435 reset_node_ctx(NULL);