Dumpfile/Tutorial.pm untested

filter: Statementstmt n/a Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a SCAR -
1  
2 =pod
3  
4 =head1 SVN::Dumpfile Tutorial
5  
6 This tutorial describes the use of the SVN::Dumpfile package to access,
7 manipulate and create Subversion dumpfiles.
8  
9 =head2 Basics
10  
11 Subversion repositories can be exported in a file format independent from the
12 used database backend which is compatible to older and future versions. This
13 files are called 'dumpfiles' and can be generated by the C<svnadmin dump>
14 command and imported with C<svnadmin load>.
15  
16 The SVN::Dumpfile module represents one dumpfile and provides all methods to
17 access all of its contained elements but also to generate new dumpfiles out of
18 existing ones or from other sources.
19  
20  
21 =head3 Dumpfile format
22  
23 The dumpfile format as specified by the Subversion team in
24 L<http://svn.collab.net/repos/svn/trunk/notes/dump-load-format.txt> looks like
25 this:
26  
27 --------------------
28 Dumpfile Headers
29 [ Revision node 0 ]
30 Revision node
31 Node
32 Node
33 ...
34 Revision node
35 Node
36 Node
37 ...
38 --------------------
39  
40 The dumpfile starts with a few file headers specifing the used version and the
41 an unique ID. The rest is build from revision nodes followed by zero, one or
42 more normal nodes. Only revision nodes for revision #0 and placeholder for
43 deleted revisions are not followed by normal nodes, but normally all others are.
44  
45  
46 =head3 Subversion Nodes
47  
48 As mentioned before there are two different kinds of nodes in subversion
49 dumpfiles: normal nodes and revision nodes. Revision nodes start a new revision
50 and only hold the revision number and properties -- author, date and log entry.
51 Normal nodes hold the informations for one single change to a single file or
52 directory. This change can be to add, modify or delete the file or directory
53 and/or the properties of it.
54  
55 In general nodes contain of three different parts: headers, properties and
56 content, in this order. Only the header part is mandatory, the other two can be
57 missing in the case the node has no properties and/or content. The headers
58 provide information about the kind of the node and about the length of the
59 properties and content. Therefor if the both are changed some headers have to
60 be recalculated.
61  
62 The nodes returned by the SVN::Dumpfile method read_node() (aliases: get_node()
63 next_node()) are objects from the SVN::Dumpfile::Node class which holds one
64 object each from the classes SVN::Dumpfile::Node::Headers,
65 SVN::Dumpfile::Node::Properties and SVN::Dumpfile::Node::Content.
66 The node object provides many methods to access the underlying objects, so that
67 they don't have to be accessed directly most of the time.
68  
69  
70 =head3 Node Headers
71  
72 The following headers are known to exist in Subversion dumpfiles and should be
73 known to the user to get the most out of this package.
74  
75 Note that all length and checksum headers are automatically recalculated when
76 the node is marked as changed by using the changed() method.
77  
78 =over 4
79  
80 =item Revision-number: non-negative integer
81  
82 The first header in, and only in, a revision node gives the revision number. All
83 following normal nodes belong to this revision until the next revision node.
84  
85 =item Node-path: unix/style/path
86  
87 This header gives the path of the file, symlink or directory described by the
88 current normal node. Note that no leading or trailing (for directories) slash is
89 included.
90  
91 =item Node-kind: ( file | dir )
92  
93 This header specifies if the node describes a file or a directory. Unix symbolic
94 links are handled as files with special a special property set.
95  
96 =item Node-action: ( add | delete | change )
97  
98 The performed action on the described file or directory, which can be added,
99 deleted or changed. Note that any change in properties (add, delete or change)
100 of an already existing file or directory are marked as 'change' action.
101  
102 =item Node-copyfrom-rev: non-negative integer
103  
104 If a file or directory was copied from another the source revision number of the
105 original is given by this header. This header must be used in combination with
106 Node-copyfrom-path.
107  
108 =item Node-copyfrom-path: unix/style/path
109  
110 If a file or directory was copied from another the path of the
111 original is given by this header. This header must be used in combination with
112 Node-copyfrom-rev.
113  
114 =item Prop-delta: ( true | false )
115  
116 This header specifies if the properties are included as delta to the last
117 changed revision of the file or directory. If present it is normally set to
118 'true'. If not present the default value is 'false'.
119  
120 =item Text-delta: ( true | false )
121  
122 This header specifies if the content is included as delta to the last
123 changed revision of the file or directory. If present it is normally set to
124 'true'. If not present the default value is 'false'.
125  
126 =item Prop-content-length: non-negative integer
127  
128 This header gives the number of bytes of the properties block. If not present or
129 zero then the node has no properties or they did not got changed (if
130 'Prop-delta: true'). Because this length includes the "PROPS-END${NL}" marker a
131 value of 10 means that there is an empty properties block attached to the node.
132  
133 =item Text-content-length: non-negative integer
134  
135 This header gives the number of bytes of the content. If not present or zero
136 then the node has no content. This means normally that the node describes a
137 directory or an unmodified file copy.
138  
139 =item Text-content-md5: MD5 sum from content (32 hex-digits)
140  
141 This header specifies the MD5 (Message Digest 5) checksum of the content (in
142 binary, not ASCII form). If it is not correct the import of the dumpfile will be
143 aborted.
144  
145 =item Text-copy-source-md5: MD5 sum from copied content (32 hex-digits)
146  
147 If the node describes a copy process this header can be used to improve data
148 integrity. The MD5 is the same as given by the 'Text-content-md5' header of the
149 copied file.
150  
151 =item Content-length: non-negative integer
152  
153 This header gives the total number of bytes of the content, i.e. is the sum of
154 the 'Prop-content-length' and 'Text-content-length' headers.
155  
156 =back
157  
158  
159 =head3 Subversion Properties
160  
161 Properties can have any text as name and, like the content,
162 hold any kind of data either in text or binary form. They provide a handy way to
163 attach meta-data or other information to the files and directories.
164  
165 Subversion itself uses a special set of properties to store basic but important
166 meta-data. All of these have the prefix 'svn:' which should not be used for any
167 user defined properties. The following svn properties are known to exists and
168 described briefly because they can influence how the dumpfile nodes are to be
169 handled. For more informations see the Subversion book at
170 L<http://svnbook.red-bean.com/en/1.1/ch07s02.html>.
171  
172 =over 4
173  
174 =item svn:keywords = Revision Rev LastChangedRev Date LastChangedDate Author
175 LastChangedBy HeadURL URL Id (any combination)
176  
177 Specifies which keywords should be expanded inside the file. Note that the
178 dumped file content only includes the unexpanded file content. Some keywords
179 have multiple alternate names as listed above.
180  
181 =item svn:mime-type = group/type
182  
183 Provides the MIME type of the file. Binary files must have the correct type
184 set or 'application/octet-stream' if it isn't known. Text files like
185 source code doesn't have to have this property set, but if so it should be
186 a type from the 'text/' group if no specific type is defined.
187  
188 =item svn:eol-style = ( native | CRLF | LF | CR )
189  
190 Specifies which end-of-line style should be used for text files. It is
191 recommended by the author of this Perl package to use 'native' wherever you can.
192 This results that text files are checked-out in the native style of the used
193 operation system. However, some file formats need a specific fixed style which
194 should not be changed to not corrupt the file.
195  
196 =item svn:executable
197  
198 Marks the file as executable if exists, i.e. the executable flag is set when the
199 file is checked-out on a Unix-like OS. The value is meaningless and can be
200 empty.
201  
202  
203 =item svn:special = ?
204  
205 Set on 'file' nodes which actually describe symlinks or other special things.
206  
207  
208 =item svn:ignore = MULTI LINE LIST
209  
210 Only applicable to directories this property specifies the ignore patterns to be
211 used for Subversion status reports.
212  
213 =item svn:externals = MULTI LINE LIST
214  
215 Lists links to external Subversion repository URLs.
216  
217  
218  
219 =back
220  
221  
222 =head3 Basic methods
223  
224 =head4 Open existing Dumpfile
225  
226 use SVN::Dumpfile;
227 my $df = new SVN::Dumpfile;
228 $df->open("mydumpfile.dump");
229  
230  
231 =head4 Access Dumpfile Headers
232  
233 $df->version() # Dumpfile version, at the moment: 1, 2 or 3
234 $df->uuid() # Universal Unique IDentifier (for version >= 2)
235  
236  
237 =head4 Get Dumpfile Headers as printable String
238  
239 $df->as_string()
240  
241  
242 =head4 Read (next) Node from Dumpfile
243  
244 my $node = $df->read_node();
245 # aliases: next_node() get_node()
246  
247 =head4 Check if Revision Node or not
248  
249 if ($node->is_rev) {
250 # Revision
251 }
252 else {
253 # Normal node
254 }
255  
256  
257 =head4 Check Existence of Node Parts
258  
259 The existence of all three parts can, and should, be checked with the methods:
260  
261 $node->has_headers()
262 $node->has_properties()
263 $node->has_content()
264  
265 While headers are mandartory there might not exists in a new created node which
266 was not read from a dumpfile but created by the user.
267  
268  
269 =head4 Access to Headers
270  
271 To test if the node has a specific header use:
272  
273 $node->has_header('Some-Header')
274  
275 Single headers can be read with
276  
277 $node->header('Some-Header')
278  
279 and changed by providing a second argument or using the method as lvalue:
280  
281 $node->header('Some-Header', 'new value')
282 $node->header('Some-Header') = 'new value'
283  
284 =head4 Access to Properties
285  
286 To test if the node has a specific property use:
287  
288 $node->has_property('Some-Property')
289  
290 Single properties can be read with
291  
292 $node->property('Some-Property')
293  
294 and changed by providing a second argument or using the method as lvalue:
295  
296 $node->property('Some-Property', 'new value')
297 $node->property('Some-Property') = 'new value'
298  
299 SVN::Dumpfile tries to maintain the exact string representation of
300 unmodified nodes to support before/after comparison. Because of this the order
301 of the properties must also be saved. New properties can be added to any
302 position using:
303  
304 $node->properties->add('property name', 'value', $position)
305  
306 If $position is not given the property is added at the end.
307  
308 =head4 Access to Content
309  
310 The node content can be either text (ASCII, UTF-8, ...) or binary. In the
311 second case a 'svn:mime-type' property should (must?) be present and indicate
312 its type. In general the mime type 'application/octet-stream' is used, but all
313 other from the 'application' group should be considered binary. Actually only
314 types from the group 'text' can be safely considered text.
315  
316 To test the mime type use
317  
318 $node->property('svn:mime-type')
319  
320 The content is returned by
321  
322 $node->content()
323  
324 and can be changed by providing an argument or using the method as lvalue:
325  
326 $node->content('new content')
327 $node->content() = 'new content'
328  
329  
330 =head4 Marking the Node as changed
331  
332 After changing the content or any properties the coresponding headers must be
333 recalculated. To force this mark the node as changed using:
334  
335 $node->changed();
336  
337  
338 =head4 Creating new Dumpfiles
339  
340 New dumpfile can be created by the create() method. Because the file headers are
341 written immediately they must exist before the method is called. They can
342 be copied from another open dumpfile with copy():
343  
344 my $df2 = $df->copy(); # Copies file headers only
345 $df2->create("newfile.dump");
346  
347 =head4 Write Nodes to Dumpfile
348  
349 Nodes can be written to new created dumpfiles with:
350  
351 $df2->write_node($node);
352  
353 However they can also be printed to any file handle using:
354  
355 $node->as_string()
356  
357  
358 =head4 Close Dumpfile
359  
360 All dumpfiles, open()-ed or create()-d ones, should be closed using close().
361  
362 $df->close();
363 $df2->close();
364  
365 There are, however, closed automatically when the dumpfile object is going out
366 of scope.
367  
368  
369 =head4 Access to Underlying Objects
370  
371 If direct access to the underlying objects is needed they can be accessed by the
372 following methods which return blessed object references:
373  
374 $node->headers() # Returns SVN::Dumpfile::Node::Headers object
375 $node->properties() # Returns SVN::Dumpfile::Node::Properties object
376 $node->contents() # Returns SVN::Dumpfile::Node::Contents object
377  
378 =head2 Examples
379  
380 =head3 Read existing Dumpfile and print it
381  
382 my $df = new SVN::Dumpfile;
383 $df->open('file.dump');
384  
385 # Print dumpfile headers
386 print $df->as_string;
387  
388 while ( my $node = $df->read_node ) {
389 print $node->as_string;
390 }
391  
392 $df->close();
393  
394 =head3 Create new Dumpfile manually
395  
396 my $df = new SVN::Dumpfile ( version => 3 );
397 # UUID will be created automatically
398 $df->create('file.dump');
399  
400 my $rev;
401 my $node;
402  
403 # [...]
404  
405 # Compact way to create nodes:
406  
407 # Create revision node
408 $rev = SVN::Dumpfile::Node->newrev(
409 number => 123,
410 author => 'karlheinz',
411 date => '2006-05-10T13:31:40.486172Z', # Can be any format accepted
412 # by Date::Parse
413 log => 'Changed X to Y. Added Z.'
414 );
415  
416 $df->write_node($rev);
417  
418 # Create file node
419 $node = SVN::Dumpfile::Node->new(
420 headers => {
421 'Node-path' => 'test/path',
422 'Node-kind' => 'file',
423 'Node-action' => 'add',
424 },
425 properties => {
426 'svn:eol-style' => 'native',
427 'svn:keywords' => 'Id Rev Author',
428 'userprop' => "USER\n",
429 },
430 content => "Some ...\n...\n... content.\n",
431 );
432  
433 # Note that the node has been marked as changed and content and property
434 # specific headers will be recalculated when node is written.
435  
436 $df->write_node($node);
437  
438 # [...]
439  
440  
441 # Other way to create nodes
442  
443 # Create revision node
444 $rev = new SVN::Dumpfile::Node;
445  
446 $rev->header( 'Revision-number', 123 );
447 $rev->property( 'svn:author', 'karlheinz' );
448 $rev->property( 'date','2006-05-10T13:31:40.486172Z' ); # Must be correct
449 # format!
450 $rev->property( 'log', 'Changed X to Y. Added Z.' );
451  
452 $rev->changed();
453  
454 $df->write_node($rev);
455  
456  
457 # Create file node
458 $node = new SVN::Dumpfile::Node;
459 $node->header( 'Node-path', 'test/path' );
460 $node->header( 'Node-kind', 'file' );
461 $node->header( 'Node-action', 'add' );
462  
463 $node->property( 'svn:eol-style', 'native' );
464 $node->property( 'svn:eol-style', 'native' );
465 $node->property( 'svn:keywords', 'Id Rev Author' );
466 $node->property( 'userprop', "USER\n" );
467  
468 $node->content("Some ...\n...\n... content.\n");
469  
470 $node->changed();
471  
472 $df->write_node($node);
473  
474 # [...]
475  
476 $df->close();
477  
478  
479 =cut
480