2 * Parse and rearrange a svnadmin dump.
3 * Create the dump with:
4 * svnadmin dump --incremental -r<startrev>:<endrev> <repository> >outfile
15 #include "repo_tree.h"
16 #include "fast_export.h"
17 #include "line_buffer.h"
19 #include "string_pool.h"
21 #define NODEACT_REPLACE 4
22 #define NODEACT_DELETE 3
24 #define NODEACT_CHANGE 1
25 #define NODEACT_UNKNOWN 0
31 #define LENGTH_UNKNOWN (~0)
33 #define BLOB_MARK_OFFSET 1000000000
35 /* Create memory pool for log messages */
36 obj_pool_gen(log
, char, 4096);
38 static char* log_copy(uint32_t length
, char *log
)
41 log_free(log_pool
.size
);
42 buffer
= log_pointer(log_alloc(length
));
43 strncpy(buffer
, log
, length
);
48 uint32_t action
, propLength
, textLength
, srcRev
, srcMode
, mark
, type
;
49 uint32_t src
[REPO_MAX_PATH_DEPTH
], dst
[REPO_MAX_PATH_DEPTH
];
53 uint32_t revision
, author
;
63 uint32_t svn_log
, svn_author
, svn_date
, svn_executable
, svn_special
, uuid
,
64 revision_number
, node_path
, node_kind
, node_action
,
65 node_copyfrom_path
, node_copyfrom_rev
, text_content_length
,
66 prop_content_length
, content_length
;
69 static void reset_node_ctx(char *fname
)
72 node_ctx
.action
= NODEACT_UNKNOWN
;
73 node_ctx
.propLength
= LENGTH_UNKNOWN
;
74 node_ctx
.textLength
= LENGTH_UNKNOWN
;
78 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.dst
, "/", fname
);
82 static void reset_rev_ctx(uint32_t revision
)
84 rev_ctx
.revision
= revision
;
85 rev_ctx
.timestamp
= 0;
90 static void reset_dump_ctx(uint32_t url
)
96 static void init_keys(void)
98 keys
.svn_log
= pool_intern("svn:log");
99 keys
.svn_author
= pool_intern("svn:author");
100 keys
.svn_date
= pool_intern("svn:date");
101 keys
.svn_executable
= pool_intern("svn:executable");
102 keys
.svn_special
= pool_intern("svn:special");
103 keys
.uuid
= pool_intern("UUID");
104 keys
.revision_number
= pool_intern("Revision-number");
105 keys
.node_path
= pool_intern("Node-path");
106 keys
.node_kind
= pool_intern("Node-kind");
107 keys
.node_action
= pool_intern("Node-action");
108 keys
.node_copyfrom_path
= pool_intern("Node-copyfrom-path");
109 keys
.node_copyfrom_rev
= pool_intern("Node-copyfrom-rev");
110 keys
.text_content_length
= pool_intern("Text-content-length");
111 keys
.prop_content_length
= pool_intern("Prop-content-length");
112 keys
.content_length
= pool_intern("Content-length");
115 static uint32_t next_blob_mark(void)
117 static uint32_t mark
= BLOB_MARK_OFFSET
;
121 static void read_props(void)
128 while ((t
= buffer_read_line()) && strcmp(t
, "PROPS-END")) {
129 if (!strncmp(t
, "K ", 2)) {
131 key
= pool_intern(buffer_read_string(len
));
133 } else if (!strncmp(t
, "V ", 2)) {
135 val
= buffer_read_string(len
);
136 if (key
== keys
.svn_log
) {
137 /* Value length excludes terminating nul. */
138 rev_ctx
.log
= log_copy(len
+ 1, val
);
139 } else if (key
== keys
.svn_author
) {
140 rev_ctx
.author
= pool_intern(val
);
141 } else if (key
== keys
.svn_date
) {
142 strptime(val
, "%FT%T", &tm
);
143 rev_ctx
.timestamp
= mkgmtime(&tm
);
144 } else if (key
== keys
.svn_executable
) {
145 node_ctx
.type
= REPO_MODE_EXE
;
146 } else if (key
== keys
.svn_special
) {
147 node_ctx
.type
= REPO_MODE_LNK
;
155 static void handle_node(void)
157 if (node_ctx
.propLength
!= LENGTH_UNKNOWN
&& node_ctx
.propLength
) {
161 if (node_ctx
.srcRev
) {
162 node_ctx
.srcMode
= repo_copy(node_ctx
.srcRev
, node_ctx
.src
, node_ctx
.dst
);
165 if (node_ctx
.textLength
!= LENGTH_UNKNOWN
&&
166 node_ctx
.type
!= REPO_MODE_DIR
) {
167 node_ctx
.mark
= next_blob_mark();
170 if (node_ctx
.action
== NODEACT_DELETE
) {
171 repo_delete(node_ctx
.dst
);
172 } else if (node_ctx
.action
== NODEACT_CHANGE
||
173 node_ctx
.action
== NODEACT_REPLACE
) {
174 if (node_ctx
.action
== NODEACT_REPLACE
&&
175 node_ctx
.type
== REPO_MODE_DIR
) {
176 repo_replace(node_ctx
.dst
, node_ctx
.mark
);
177 } else if (node_ctx
.propLength
!= LENGTH_UNKNOWN
) {
178 repo_modify(node_ctx
.dst
, node_ctx
.type
, node_ctx
.mark
);
179 } else if (node_ctx
.textLength
!= LENGTH_UNKNOWN
) {
180 node_ctx
.srcMode
= repo_replace(node_ctx
.dst
, node_ctx
.mark
);
182 } else if (node_ctx
.action
== NODEACT_ADD
) {
183 if (node_ctx
.srcRev
&&
184 node_ctx
.propLength
== LENGTH_UNKNOWN
&&
185 node_ctx
.textLength
!= LENGTH_UNKNOWN
) {
186 node_ctx
.srcMode
= repo_replace(node_ctx
.dst
, node_ctx
.mark
);
187 } else if ((node_ctx
.type
== REPO_MODE_DIR
&& !node_ctx
.srcRev
) ||
188 node_ctx
.textLength
!= LENGTH_UNKNOWN
){
189 repo_add(node_ctx
.dst
, node_ctx
.type
, node_ctx
.mark
);
193 if (node_ctx
.propLength
== LENGTH_UNKNOWN
&& node_ctx
.srcMode
) {
194 node_ctx
.type
= node_ctx
.srcMode
;
198 fast_export_blob(node_ctx
.type
, node_ctx
.mark
, node_ctx
.textLength
);
199 } else if (node_ctx
.textLength
!= LENGTH_UNKNOWN
) {
200 buffer_skip_bytes(node_ctx
.textLength
);
204 static void handle_revision(void)
206 repo_commit(rev_ctx
.revision
, rev_ctx
.author
, rev_ctx
.log
, dump_ctx
.uuid
,
207 dump_ctx
.url
, rev_ctx
.timestamp
);
210 static void svndump_read(uint32_t url
)
214 uint32_t active_ctx
= DUMP_CTX
;
219 while ((t
= buffer_read_line())) {
220 val
= strstr(t
, ": ");
224 key
= pool_intern(t
);
226 if(key
== keys
.uuid
) {
227 dump_ctx
.uuid
= pool_intern(val
);
228 } else if (key
== keys
.revision_number
) {
229 if (active_ctx
== NODE_CTX
) handle_node();
230 if (active_ctx
!= DUMP_CTX
) handle_revision();
231 active_ctx
= REV_CTX
;
232 reset_rev_ctx(atoi(val
));
233 } else if (key
== keys
.node_path
) {
234 if (active_ctx
== NODE_CTX
)
236 active_ctx
= NODE_CTX
;
238 } else if (key
== keys
.node_kind
) {
239 if (!strcmp(val
, "dir")) {
240 node_ctx
.type
= REPO_MODE_DIR
;
241 } else if (!strcmp(val
, "file")) {
242 node_ctx
.type
= REPO_MODE_BLB
;
244 fprintf(stderr
, "Unknown node-kind: %s\n", val
);
246 } else if (key
== keys
.node_action
) {
247 if (!strcmp(val
, "delete")) {
248 node_ctx
.action
= NODEACT_DELETE
;
249 } else if (!strcmp(val
, "add")) {
250 node_ctx
.action
= NODEACT_ADD
;
251 } else if (!strcmp(val
, "change")) {
252 node_ctx
.action
= NODEACT_CHANGE
;
253 } else if (!strcmp(val
, "replace")) {
254 node_ctx
.action
= NODEACT_REPLACE
;
256 fprintf(stderr
, "Unknown node-action: %s\n", val
);
257 node_ctx
.action
= NODEACT_UNKNOWN
;
259 } else if (key
== keys
.node_copyfrom_path
) {
260 pool_tok_seq(REPO_MAX_PATH_DEPTH
, node_ctx
.src
, "/", val
);
261 } else if (key
== keys
.node_copyfrom_rev
) {
262 node_ctx
.srcRev
= atoi(val
);
263 } else if (key
== keys
.text_content_length
) {
264 node_ctx
.textLength
= atoi(val
);
265 } else if (key
== keys
.prop_content_length
) {
266 node_ctx
.propLength
= atoi(val
);
267 } else if (key
== keys
.content_length
) {
270 if (active_ctx
== REV_CTX
) {
272 } else if (active_ctx
== NODE_CTX
) {
274 active_ctx
= REV_CTX
;
276 fprintf(stderr
, "Unexpected content length header: %d\n", len
);
277 buffer_skip_bytes(len
);
281 if (active_ctx
== NODE_CTX
) handle_node();
282 if (active_ctx
!= DUMP_CTX
) handle_revision();
285 static void svndump_reset(void)
292 reset_node_ctx(NULL
);
295 int main(int argc
, char **argv
)
298 svndump_read((argc
> 1) ? pool_intern(argv
[1]) : ~0);