1 Oh Most High and Fragrant Emacs, please be in -*- text -*- mode!
3 This is the library described in the section "The working copy
4 management library" of svn-design.texi. It performs local operations
5 in the working copy, tweaking administrative files and versioned data.
6 It does not communicate directly with a repository; instead, other
7 libraries that do talk to the repository call into this library to
8 make queries and changes in the working copy.
11 The Problem We're Solving
12 -------------------------
14 The working copy is arranged as a directory tree, which, at checkout,
15 mirrors a tree rooted at some node in the repository. Over time, the
16 working copy accumulates uncommitted changes, some of which may affect
17 its tree layout. By commit time, the working copy's layout could be
18 arbitrarily different from the repository tree on which it was based.
20 Furthermore, updates/commits do not always involve the entire tree, so
21 it is possible for the working copy to go a very long time without
22 being a perfect mirror of some tree in the repository.
25 One Way We're Not Solving It
26 ----------------------------
28 Updates and commits are about merging two trees that share a common
29 ancestor, but have diverged since that ancestor. In real life, one of
30 the trees comes from the working copy, the other from the repository.
31 But when thinking about how to merge two such trees, we can ignore the
32 question of which is the working copy and which is the repository,
33 because the principles involved are symmetrical.
35 Why do we say symmetrical?
37 It's tempting to think of a change as being either "from" the working
38 copy or "in" the repository. But the true source of a change is some
39 committer -- each change represents some developer's intention toward
40 a file or a tree, and a conflict is what happens when two intentions
41 are incompatible (or their compatibility cannot be automatically
44 It doesn't matter in what order the intentions were discovered --
45 which has already made it into the repository versus which exists only
46 in someone's working copy. Incompatibility is incompatibility,
47 independent of timing.
49 In fact, a working copy can be viewed as a "branch" off the
50 repository, and the changes committed in the repository *since* then
51 represent another, divergent branch. Thus, every update or commit is
52 a general branch-merge problem:
54 - An update is an attempt to merge the repository's branch into the
55 working copy's branch, and the attempt may fail wholly or
56 partially depending on the number of conflicts.
58 - A commit is an attempt to merge the working copy's branch into
59 the repository. The exact same algorithm is used as with
60 updates, the only difference being that a commit must succeed
61 completely or not at all. That last condition is merely a
62 usability decision: the repository tree is shared by many
63 people, so folding both sides of a conflict into it to aid
64 resolution would actually make it less usable, not more. On the
65 other hand, representing both sides of a conflict in a working
66 copy is often helpful to the person who owns that copy.
68 So below we consider the general problem of how to merge two trees
69 that have a common ancestor. The concrete tree layout discussed will
70 be that of the working copy, because this library needs to know
71 exactly how to massage a working copy from one state to another.
74 Structure of the Working Copy
75 -----------------------------
77 Working copy meta-information is stored in .svn/ subdirectories,
78 analogous to CVS/ subdirs. See the separate sections below for more details.
80 .svn/format /* Deprecated in post 1.3 working copies. */
81 entries /* Various adm info for each directory entry */
82 dir-props /* Working properties for this directory */
83 dir-prop-base /* Pristine properties for this directory */
84 dir-prop-revert /* Dir base-props for revert, if any */
85 lock /* If existent, tells others this dir is busy
87 log /* Operations log, if any (for rollback
89 log.N /* Additional ops logs (N is an integer
91 text-base/ /* Pristine repos revisions of the files... */
92 foo.c.svn-base /* Repos revision of foo.c. */
93 foo.c.svn-revert /* Text-base used when reverting, if any. */
94 props/ /* Working properties for files in this dir */
95 foo.c.svn-work /* Stores foo.c's working props */
96 prop-base/ /* Pristine properties for files in this dir */
97 foo.c.svn-base /* Stores foo.c's pristine props */
98 foo.c.svn-revert /* Props base when reverting, if any */
99 wcprops/ /* Obsolete in format 7 and beyond. */
101 dir-wcprops /* Obsolete in format 7 and beyond.
102 all-wcprops /* Special 'wc' props for files in this dir */
103 tmp/ /* Local tmp area */
104 ./ /* Adm files are written directly here */
105 text-base/ /* tmp area for base files */
106 prop-base/ /* tmp area for base props */
107 props/ /* tmp area for props */
108 empty-file /* Obsolete, no longer used, not present in
109 post-1.3 working copies */
112 Says what version of the working copy adm format this is (so future
113 clients can be backwards compatible easily).
115 This file is deprecated and present for backwards compatibility.
116 It may be removed in the future.
119 This file holds revision numbers and other information for this
120 directory and its files, and records the presence of subdirs (but
121 does not record much other information about them, as the subdirs
122 do that themselves). See below for more information.
124 Since format 7, this file also contains the format number of this
125 working copy directory.
127 Also, the presence of this file means that the entire process of
128 creating the adm area was completed, because this is always the
129 last file created. Of course, that's no guarantee that someone
130 didn't muck things up afterwards, but it's good enough for
134 Properties for this directory. These are the "working" properties
135 that may be changed by the user.
138 Same as `dir-props', except this is the pristine copy; analogous to
139 the "text-base" revisions of files. The last up-to-date copy of the
140 directory's properties live here.
143 In a schedule-replace situation for this directory, this holds the
144 base-props for the deleted version of the directory (i.e., the
145 version that is being replaced). If this file doesn't exist, the
146 `dir-prop-base' file is used.
149 Present if some client is using this .svn/ subdir for anything that
150 requires write access.
153 These files (XML fragments) hold a log of actions that are about to be
154 done, or are in the process of being done. Each action is of the
155 sort that, given a log entry for it, either it is okay to do the
156 action again (i.e., the action is idempotent), or else one can tell
157 unambiguously whether or not the action was successfully done.
158 Thus, in recovering from a crash or an interrupt, the wc library
159 reads over the log file, ignoring those actions that have already
160 been done, and doing the ones that have not. When all the actions
161 in log have been done, the log files are removed.
163 Some operations produce more than one log file. The first log file
164 is named `log', the next `log.1' and so on. Processing the log
165 files starts at `log' and stops after `log.N' when there is no `log.N+1'
166 (counting the first log file as `log.0'; it is named `log' for
169 Soon there will be a general explanation/algorithm for using the
170 log file; for now, this example gives the flavor:
172 To do a fresh checkout of `iota' in directory `.'
174 1. add_file() produces the new ./.svn/tmp/.svn/entries, which
175 probably is the same as the original `entries' file since
176 `iota' is likely to be the same revision as its parent
177 directory. (But not necessarily...)
179 2. apply_textdelta() hands window_handler() to its caller.
181 3. window_handler() is invoked N times, constructing
184 4. finish_file() is called. First, it creates `log' atomically,
185 with the following items,
187 <mv src=".svn/tmp/iota" dst=".svn/text-base/iota">
188 <mv src=".svn/tmp/.svn/entries" dst=".svn/entries">
189 <merge src=".svn/text-base/iota" dst="iota">
191 Then it does the operations in the log file one by one.
192 When it's done, it removes the log.
194 To recover from a crash:
196 1. Look for a log file.
198 A. If none, just "rm -r tmp/*".
200 B. Else, run over the log file from top to bottom,
201 attempting to do each action. If an action turns out to
202 have already been done, that's fine, just ignore it.
203 When done, remove the log file.
206 Note that foo/.svn/log always uses paths relative to foo/, for
209 <!-- THIS IS GOOD -->
210 <mv name=".svn/tmp/prop-base/name"
211 dest=".svn/prop-base/name">
215 <!-- THIS WOULD BE BAD -->
216 <mv name="/home/joe/project/.svn/tmp/prop-base/name"
217 dest="/home/joe/project/.svn/prop-base/name">
221 <!-- THIS WOULD ALSO BE BAD -->
222 <mv name="tmp/prop-base/name"
223 dest="prop-base/name">
225 The problem with the second way is that is violates the
226 separability of .svn subdirectories -- a subdir should be operable
227 independent of its location in the local filesystem.
229 The problem with the third way is that it can't conveniently refer
230 to the user's actual working files, only to files inside .svn/.
233 A shallow mirror of the working directory (i.e., the parent of the
234 .svn/ subdirectory), giving us reproducible tmp names.
236 When the working copy library needs a tmp file for something in the
237 .svn dir, it uses tmp/thing, for example .svn/tmp/entries, or
238 .svn/tmp/text-base/foo.c.svn-base. When temp file with a unique name
239 is needed, use the `.tmp' extension to distinguish it from temporary
240 admin files with well-known names.
242 See discussion of the `log' file for more details.
245 Each file in text-base/ is a pristine repository revision of that
246 file, corresponding to the revision indicated in `entries'. These
247 files are used for sending diffs back to the server, etc.
249 A file named `foo.c' in the working copy will be named `foo.c.svn-base'
252 For a file scheduled for replacement, the text-base of the deleted
253 entry may be stored in `foo.c.svn-revert'.
256 Pristine repos properties for those files, in hashdump format. Named
257 with the extension `.svn-base'.
259 For an entry scheduled for replacement, the text-base of the deleted
260 entry may be stored in `foo.c.svn-revert'.
264 The non-pristine (working copy) of each file's properties. These
265 are where local modifications to properties live. The files in this
266 directory are given `.svn-work' extensions.
268 Notice that right now, Subversion's ability to handle metadata
269 (properties) is a bit limited:
271 1. Properties are not "streamy" the same way a file's text is.
272 Properties are held entirely in memory.
274 2. Property *lists* are also held entirely in memory. Property
275 lists move back and forth between hashtables and our disk-based
276 `hashdump' format. Anytime a user wishes to read or write an
277 individual property, the *entire* property list is loaded from
278 disk into memory, and written back out again. Not exactly a
279 paradigm of efficiency!
281 In other words, for Subversion 1.0, properties work sufficiently,
282 but shouldn't be abused. They work fine for storing information
283 like ACLs, permissions, ownership, and notes; but users shouldn't
284 be trying to store 30 meg PNG files. :)
287 Some properties are never seen or set by the user, and are never
288 stored in the repository filesystem. They are created and used
289 exclusively by the networking layer (DAV right now) and need to
290 be secretly saved and retrieved, much like a web browser stores
291 "cookies". Special wc library routines allow the networking layer
292 to get and set these properties. By design, working copy metadata
293 used by libsvn_wc itself should always be stored in the entries file,
296 Note that because these properties aren't being versioned, we don't
297 bother to keep pristine forms of them in a 'base' area.
300 This file was added in format 4 and earlier. This was used to
301 create file diffs against the empty file (i.e. for adds and
305 This file was removed in format 5. It used to contain a short text
306 saying what this directory is for and warning users not to alter
312 This section describes the entries file as of format 7 and beyond. See below
313 for the older XML-based format.
315 The entries file is a text file. The character encoding of the file
316 is UTF-8 with no BOM (byte order mark) allowed at the beginning. All
317 whitespace is significant.
319 The file starts with a decimal number which is the format version of
320 this working copy directory, followed by a line feed (0x0a) character.
321 No whitespace (except for the terminating line feed) is allowed before
322 or after the number. The changes in each format are listed in wc.h.
324 The rest of the file contains one record for each directory entry.
325 Each record contains a number of ordered fields as described below.
326 The fields are terminated by a line feed (0x0a) character. Empty
327 fields are represented by just the terminator. Empty fields that are
328 only followed by empty fields may be omited from the record. Records
329 are terminated by a form feed (0x0c) and a cosmetic line feed (0x0a).
331 The bytes representing the characters "\" and ASCII control characters
332 (0x01 - 0x1f and 0x7f) must be escaped when they occur in a field.
333 The escaping uses the syntax "\xHH", where "HH" are the two
334 hexadecimal digits (either upper- or lowercase) representing the
335 escaped byte. No other bytes may be escaped. NUL bytes are not
338 A field may be boolean, in which case it can have either a value equal
339 to the field name, meaning true, or no value, meaning false. Timestamps
340 are stored in the format produced by svn_time_to_cstring(). Numbers
341 are stored in decimal.
343 The first entry has an empty name field and is the entry for this directory,
344 that is, the directory containing this administrative area. This is
345 known as the "this_dir" entry.
347 The following fields are allowed; they are present in the order in
348 which they are described. Except for boolean fields, the field names
349 are not present in the file.
352 The basename of this entry, or the empty string for the this_dir
353 entry. Required for all entries.
356 The kind of this entry: `file' or `dir'. Required for all entries.
359 The revision that the pristine text and properties of this entry
360 represent. Defaults to the revision of the this_dir entry, for
361 which it is required. Set to 0 for entries not yet in the
365 The URL of the corresponding entry in the repository. Required
366 for the this_dir entry; for all other entries, the default is to
367 append the URI-encoded entry name to the URL of the this_dir entry,
371 The prefix of the URL which represents the repository root of this
372 entry. Defaults to the repository root of the this_dir entry.
373 Optional for the this_dir entry, for compatibility.
376 The current scheduling for this entry: `add', `delete' or
377 `replace'. Defaults to normal scheduling.
380 For file entries, the timestamp of the working file when it was
381 last known to be identical to the text base file. Optional, no default.
384 For file entries, base-64-encoded MD5 checksum of the text-base
385 file. Optional, for backwards compatibility.
388 The date of the committed-rev if available. Optional, no default.
391 The last committed revision for this entry if this entry is in the
392 repository. Optional, no default.
395 The author of the `committed-rev' if available. Optional, no default.
398 A boolean: true if there are any working properties for this entry.
401 A boolean: true if this entry has any property modifications.
404 A space-separated list of property names whose presence is cached
405 in present-props. Defaults to the value of the this_dir entry.
406 For the this_dir entry, defaults to the empty list.
409 A space-separated list of property names. If a property name n is
410 in this list, then the working props of this entry contains this
411 property. If cachable-props contains a property name n' but n' is
412 absent from present-props, then the working props don't contain
413 this property. Defaults to the empty list.
415 conflict-old, conflict-new and conflict-wrk:
416 Present if there is a text conflict, in which case these three
417 fields specify the relative filenames of the three saved
421 In case of a property conflict, this field is present and
422 specifies the relative filename of the property reject file.
425 A boolean: true if this entry was added with history; only allowed
426 when schedule is add. (### Why aren't the copyfrom attributes
430 If this entry is added with history, the URL of the copy source.
431 Present iff copyfrom-rev is present.
434 If this entry is added with history, the revision of the copy
435 source. Present iff copyfrom-url is present.
438 A boolean: true if this entry is deleted in its revision but exists
439 in its parent's revision. This is necessary because updates are
440 not atomic: different bits of a working copy can be updated to
441 different revisions at different times, and it's possible that
442 this entry may be updated to a more recent revision (R) than its
443 parent's revision (P). If this entry is deleted in R, and the
444 parent is trying to report its own state (based on P) to the
445 repository, the parent cannot simply claim to be at P; the parent
446 must also indicate that this particular entry is deleted because it
450 A boolean: true if is an entry by this name in the repository but
451 we don't know anything about it except its kind.
454 A boolean: true if this entries file is not complete yet. Used
455 when updating. This is only allowed on the this_dir entry; it
456 allows update operations to be non-atomic, by marking the directory
457 as still in the process of being updated. If this update is
458 interrupted for some reason, a later update will see that this
459 directory is incomplete and Do The Right Thing.
462 The repository UUID of this entry. Defaults to the UUID of the
463 this_dir entry. Optional, even for the this_dir entry, for
464 backwards compatibility.
467 The lock token URL if this entry is locked in the repository;
471 The lock owner, iff there is a lock token.
474 The lock comment iff there is a lock token and the lock has a
478 The lock creation date iff there is a lock token.
481 Which changelist this entry is part of, or empty if none.
484 A boolean: true iff this entry should be kept after a scheduled
485 deletion is committed. This is only allowed on the this_dir entry,
486 and only when the schedule is 'delete'.
489 The number of bytes in the working file. This can differ from the
490 number of bytes in the text base; for example, the working file may
491 have undergone keyword substitution or eol translation.
492 The purpose of this field is to serve as a reference for the
493 change-detection heuristic.
496 The entry depth of this directory. `empty' means updates will not pull
497 in any files or subdirectories not already present. `files' means that
498 updates will pull in any files not already present. `immediates' means
499 updates will pull in any files or subdirectories not already
500 present, and those subdirectories' this_dir entries will have depth
501 `empty'. `' means infinite (normal) depth -- the directory has all its
502 entries and pulls new entries with depth infinity as well.
503 Default is infinite (`').
505 The only fields allowed in an entry for a directory (other than the
506 this_dir entry) are name, absent, deleted, schedule, copied,
507 copyfrom-url, copyfrom-rev and kind. The other fields are only stored
508 in the this_dir entry for the directory in question.
511 XML-based entries format
512 ------------------------
514 In format 6 and earlier, the entries file is stored in an XML based
515 format. The entries file is an XML document with a top-level element
516 named `wc-entries'. This element contains one or more `entries'
517 elements, one for each directory entry. All XML elements in the
518 entries file are in the XML namespace "svn:".
520 All `entry' elements are empty, and can have the attributes
521 corresponding to fields of the non-XML format.
523 An attribute may be boolean, in which case it can have one of the
524 values `true' or `false'. Boolean attributes default to `false' if
525 not present. Timestamps are stored in the same format as in the
526 non-XML format. Inheritence of values from the this_dir entry works
527 in the same way as in the non-XML format.
529 Fields added in format 7 and later are not allowed in the XML-based
530 format. The attributes `has-props', `has-prop-mods', `cachable-props',
531 and `present-props' are only valid in format 6.
533 In addition, the following attribute is allowed, which has no
534 corresponding field in the non-XML format:
537 Obsolete. In format 5 and earlier this was similar to `text-time',
538 but for the working props file.
545 For each entry, there may be one base and one working properties file.
546 For files, these are named .svn/prop-base/foo.svn-base and
547 .svn/props/foo.svn-work, respectively. For directories, these are
548 stored directly under .svn in .svn/dir-prop-base and .svn/dir-props,
549 respectively. Property files are in the hashdump format produced by
550 svn_hash_write(). If the file contains no properties, it is either
551 empty or contains just the "END\n" delimiter. The way properties are
552 stored changed in format 6; that way is described first.
554 In format 6 and later, the base-props file is present only if there
555 are any base properties. The working props file is present only if
556 the entry has property modifications (i.e. its has-prop-mods
557 field is true). Note that an existing, but empty working props
558 file means that there are property modifications, but no working
561 In formats 5 and earlier, base-props are handled the same, but a
562 non-existent working props file is equivalent to an empty file and the
563 working props file always contains the working properties. The
564 `prop-time' attribute can be used to optimize detection of property
567 In format 8 and beyond, wcprops are stored in a file called all-wcprops.
568 This file need not exist if no entry in the directory has any wcprops.
569 The file starts with all wcprops for the this_dir entry in hashdump format.
570 Then comes, for each entry that has wcprops, a line containing the basename
571 of the entry followed by the wcprops for that entry in hashdump format.
573 In format 7 and earlier, wcprops are stored in a similar fashion to
574 how base-props are stored, but they use .svn/dir-wcprops and
575 .svn/wcprops/foo.svn-work names for directory and file properties,
579 How the client applies an update delta
580 --------------------------------------
582 Updating is more than just bringing changes down from the repository;
583 it's also folding those changes into the working copy. Getting the
584 right changes is the easy part -- folding them in is hard.
586 Before we examine how Subversion handles this, let's look at what CVS
589 1. Unmodified portions of the working copy are simply brought
590 up-to-date. The server sends a forward diff, the client applies
593 2. Locally modified portions are "merged", where possible. That
594 is, the changes from the repository are incorporated into the
595 local changes in an intelligent way (if the diff application
596 succeeds, then no conflict, else go to 3...)
598 3. Where merging is not possible, a conflict is flagged, and *both*
599 sides of the conflict are folded into the local file in such a
600 way that it's easy for the developer to figure out what
601 happened. (And the old locally-modified file is saved under a
602 temp name, just in case.)
604 It would be nice for Subversion to do things this way too;
605 unfortunately, that's not possible in every case.
607 CVS has a wonderfully simplifying limitation: it doesn't version
608 directories, so never has tree-structure conflicts. Given that only
609 textual conflicts are possible, there is usually a natural way to
610 express both sides of a conflict -- just include the opposing texts
611 inside the file, delimited with conflict markers. (Or for binary
612 files, make both revisions available under temporary names.)
614 While Subversion can behave the same way for textual conflicts, the
615 situation is more complex for trees. There is sometimes no way for a
616 working copy to reflect both sides of a tree conflict without being
617 more confusing than helpful. How does one put "conflict markers" into
618 a directory, especially when what was a directory might now be a file,
621 Therefore, while Subversion does everything it can to fold conflicts
622 intelligently (doing at least as well as CVS does), in extreme cases
623 it is acceptable for the Subversion client to punt, saying in effect
624 "Your working copy is too out of whack; please move it aside, check
625 out a fresh one, redo your changes in the fresh copy, and commit from
626 that." (This response may also apply to subtrees of the working copy,
629 Usually it offers more detail than that, too. In addition to the
630 overall out-of-whackness message, it can say "Directory foo was
631 renamed to bar, conflicting with your new file bar; file blah was
632 deleted, conflicting with your local change to file blah, ..." and so
633 on. The important thing is that these are informational only -- they
634 tell the user what's wrong, but they don't try to fix it
637 All this is purely a matter of *client-side* intelligence. Nothing in
638 the repository logic or protocol affects the client's ability to fold
639 conflicts. So as we get smarter, and/or as there is demand for more
640 informative conflicting updates, the client's behavior can improve and
641 punting can become a rare event. We should start out with a _simple_
642 conflict-folding algorithm initially, though.
645 Text and Property Components
646 ----------------------------
648 A Subversion working copy keeps track of *two* forks per file, much
649 like the way MacOS files have "data" forks and "resource" forks. Each
650 file under revision control has its "text" and "properties" tracked
651 with different timestamps and different conflict (reject) files. In
652 this vein, each file's status-line has two columns which describe the
657 -- glub.c --> glub.c is completely up-to-date.
658 U- foo.c --> foo.c's textual component was updated.
659 -M bar.c --> bar.c's properties have been locally modified
660 UC baz.c --> baz.c has had both components patched, but a
661 local property change is creating a conflict.