Challenges for SVN intro
[swc-version-control-svn.git] / svn.html
1 {% extends "templates/_base.html" %}
2
3 {% block file_metadata %}
4   <meta name="title" content="Version Control With Subversion" />
5 {% endblock file_metadata %}
6
7 {% block content %}
8   <ol class="toc">
9     <li><a href="#s:basics">Basic Use</a></li>
10     <li><a href="#s:merge">Merging Conflicts</a></li>
11     <li><a href="#s:rollback">Recovering Old Versions</a></li>
12     <li><a href="#s:setup">Setting up a Repository</a></li>
13     <li><a href="#s:provenance">Provenance</a></li>
14     <li><a href="#s:summary">Summing Up</a></li>
15   </ol>
16
17 <p>
18   Wolfman and Dracula have been hired by Universal Missions
19   (a space services spinoff from Euphoric State University)
20   to figure out where the company should send its next planetary lander.
21   They want to be able to work on the plans at the same time,
22   but they have run into problems doing this in the past.
23   If they take turns,
24   each one will spend a lot of time waiting for the other to finish.
25   On the other hand,
26   if they work on their own copies and email changes back and forth
27   they know that things will be lost, overwritten, or duplicated.
28 </p>
29
30 <p>
31   The right solution is to use a
32   <a href="glossary.html#version-control-system">version control system</a>
33   to manage their work.
34   Version control is better than mailing files back and forth because:
35 </p>
36
37 <ol>
38
39   <li>
40     It's hard (but not impossible) to accidentally overlook or overwrite someone's changes,
41     because the version control system highlights them automatically.
42   </li>
43
44   <li>
45     It keeps a record of who made what changes when,
46     so that if people have questions later on,
47     they know who to ask
48     (or blame).
49   </li>
50
51   <li>
52     Nothing that is committed to version control is ever lost.
53     This means it can be used like the "undo" feature in an editor,
54     and since all old versions of files are saved
55     it's always possible to go back in time to see exactly who wrote what on a particular day,
56     or what version of a program was used to generate a particular set of results.
57   </li>
58
59 </ol>
60
61 <div class="box">
62   <h3>Nothing's Perfekt</h3>
63
64   <p>
65     Version control systems do have one important shortcoming.
66     While it is easy for them to find, display, and merge differences in text files,
67     images, MP3s, PDFs, or Microsoft Word or Excel files aren't stored as text&mdash;they
68     use specialized binary data formats.
69     Most version control systems don't know how to deal with these formats,
70     so all they can say is, "These files differ."
71     Reconciling those differences will probably require use of an auxiliary tool,
72     such as an audio editor
73     or Microsoft Word's "Compare and Merge" utility.
74   </p>
75 </div>
76
77 <p>
78   The rest of this chapter will explore how to use
79   a popular open source version control system called Subversion.
80 </p>
81
82 <div class="guide">
83   <h2>For Instructors</h2>
84
85   <p class="fixme">explain</p>
86
87   <div class="prereq">
88     <h3>Prerequisites</h3>
89     <p class="fixme">prereq</p>
90   </div>
91
92   <div class="notes">
93     <h3>Teaching Notes</h3>
94     <ul>
95     </ul>
96   </div>
97
98 </div>
99
100 <section id="s:basics">
101   <h2>Basic Use</h2>
102
103   <div class="understand">
104     <h3>Learning Objectives:</h3>
105     <ul>
106       <li>Draw a diagram showing the places version control stores information.</li>
107       <li>Check out a working copy of a repository.</li>
108       <li>View the history of changes to a project.</li>
109       <li>Explain why working copies of different projects should not overlap.</li>
110       <li>Add files to a project.</li>
111       <li>Commit changes made to a working copy to a repository.</li>
112       <li>Update a working copy to get changes from the repository.</li>
113       <li>Compare the current state of a working copy to the last update from the repository, and to the current state of the repository.</li>
114       <li>Explain what "version 123 of <code>xyz.txt</code>" actually means.</li>
115     </ul>
116   </div>
117
118   <p>
119     A version control system keeps the master copy of a file
120     in a <a href="glossary.html#repository">repository</a>
121     located on a <a href="glossary.html#server">server</a>&mdash;a computer
122     that is never used directly by people,
123     but only by their programs
124     (<a href="#f:repository">Figure 1</a>).
125     No-one ever edits the master copy directly.
126     Instead,
127     Wolfman and Dracula each have a <a href="glossary.html#working-copy">working copy</a>
128     on their own machines.
129     They can each edit their working copies whenever and however they want.
130   </p>
131
132   <figure id="f:repository">
133     <img src="svn/repository.png" alt="Repositories and Working Copies" />
134     <figcaption>Figure 1: Repositories and Working Copies</figcaption>
135   </figure>
136
137   <p id="a:commit">
138     When Wolfman is ready to share his changes with Dracula,
139     he <a href="glossary.html#commit">commits</a> his work to the repository
140     (<a href="#f:workflow">Figure 2</a>).
141     Dracula can then <a href="glossary.html#update">update</a> his working copy
142     to get those changes when he's ready for them.
143     And of course,
144     when Dracula finishes working on something,
145     he can commit and so that Wolfman can update.
146   </p>
147
148   <figure id="f:workflow">
149     <img src="svn/workflow.png" alt="Sharing Files Through Version Control" />
150     <figcaption>Figure 2: Sharing Files Through Version Control</figcaption>
151   </figure>
152
153   <p>
154     If this is all there was to version control,
155     it would be no better than FTP or Dropbox.
156     But what if Dracula and Wolfman change their working copies at the same time?
157     If Wolfman commits first,
158     his changes are simply copied to the repository
159     (<a href="#f:merge_first_commit">Figure 3</a>):
160   </p>
161
162   <figure id="f:merge_first_commit">
163     <img src="svn/merge_first_commit.png" alt="Wolfman Commits First" />
164     <figcaption>Figure 3: Wolfman Commits First</figcaption>
165   </figure>
166
167   <p class="continue">
168     If Dracula now tries to commit something that would overwrite Wolfman's changes
169     the version control system detects the <a href="glossary.html#conflict">conflict</a>,
170     halts the commit,
171     and tells Dracula that there's a problem
172     (<a href="#f:merge_second_commit">Figure 4</a>):
173   </p>
174
175   <figure id="f:merge_second_commit">
176     <img src="svn/merge_second_commit.png" alt="Dracula Has a Conflict" />
177     <figcaption>Figure 4: Dracula Has a Conflict</figcaption>
178   </figure>
179
180   <p class="continue">
181     Dracula must <a href="glossary.html#resolve">resolve</a> that conflict
182     before the version control system will allow him to commit his work.
183     He can accept what Wolfman did,
184     replace it with what he has done,
185     or write something new that combines the two&mdash;that's up to him
186     (<a href="#f:merge_resolve">Figure 5</a>).
187     Once he has cleaned things up, he can go ahead and try committing again.
188     If all of the conflicts have been resolved,
189     the version control will accept it this time.
190   </p>
191
192   <figure id="f:merge_resolve">
193     <img src="svn/merge_resolve.png" alt="Resolving the Conflict" />
194     <figcaption>Figure 5: Resolving the Conflict</figcaption>
195   </figure>
196
197   <div class="box">
198     <h3>Forgiveness vs. Permission</h3>
199
200     <p>
201       Old-fashioned version control systems prevented conflicts from happening
202       by <a href="glossary.html#lock">locking</a> the master copy
203       whenever someone was working on it.
204       This <a href="glossary.html#pessimistic-concurrency">pessimistic</a> strategy
205       guaranteed that a second person (or monster)
206       could never make changes to the same file at the same time,
207       but it also meant that people had to take turns editing files.
208     </p>
209
210     <p>
211       Most of today's version control systems use
212       an <a href="glossary.html#optimistic-concurrency">optimistic</a> strategy instead:
213       people are always allowed to edit their working copies,
214       and if a conflict occurs,
215       the version control system helps them sort it out after the fact.
216     </p>
217   </div>
218
219   <p>
220     To see how this actually works,
221     let's assume that the Mummy
222     (Dracula and Wolfman's boss)
223     has already put some notes in a version control repository
224     whose URL is <code>https://universal.software-carpentry.org/monsters</code>.
225     Every repository has an address like this that uniquely identifies the location of the master copy.
226   </p>
227
228   <div class="box">
229     <h3>There's More Than One Way To Do It</h3>
230
231     <p>
232       We will drive Subversion from the command line in our examples,
233       but if you prefer using a GUI,
234       there are many for you to choose from.
235       Please see the <a href="ref.html#s:svn:gui">reference</a> for links.
236     </p>
237   </div>
238
239   <p>
240     It's Monday morning,
241     and Dracula has just joined the project.
242     In order to get a working copy on his computer,
243     Dracula has to <a href="glossary.html#check-out">check out</a> a copy of the repository.
244     He only has to do this once per project:
245     once he has a working copy,
246     he can update it over and over again to get other people's work.
247   </p>
248
249   <p>
250     While in his home directory,
251     Dracula types the command:
252   </p>
253
254 <pre>
255 $ <span class="in">svn checkout https://universal.software-carpentry.org/monsters</span>
256 </pre>
257
258   <p class="continue">
259     This creates a new directory called <code>monsters</code>
260     and fills it with a copy of the repository's contents
261     (<a href="#f:example_repo">Figure 6</a>).
262   </p>
263
264 <pre>
265 <span class="out">A    monsters/jupiter
266 A    monsters/mars
267 A    monsters/mars/mons-olympus.txt
268 A    monsters/mars/cydonia.txt
269 A    monsters/earth
270 A    monsters/earth/himalayas.txt
271 A    monsters/earth/antarctica.txt
272 A    monsters/earth/carlsbad.txt
273 Checked out revision 6.</span>
274 </pre>
275
276   <figure id="f:example_repo">
277     <img src="svn/example_repo.png" alt="Example Repository" />
278     <figcaption>Figure 6: Example Repository</figcaption>
279   </figure>
280
281   <p class="continue">
282     Dracula can then go into this directory
283     and use regular shell commands to view the files:
284   </p>
285
286 <pre>
287 $ <span class="in">cd monsters</span>
288 $ <span class="in">ls</span>
289 <span class="out">earth   jupiter mars</span>
290 $ <span class="in">ls *</span>
291 <span class="out">earth:
292 antarctica.txt  carlsbad.txt  himalayas.txt
293
294 jupiter:
295
296 mars:
297 cydonia.txt  mons-olympus.txt</span>
298 </pre>
299
300     <div class="box">
301       <h3>Don't Let the Working Copies Overlap</h3>
302
303       <p>
304         It's very important that the working copies of different project do not overlap;
305         in particular,
306         we should never try to check out one project inside a working copy of another project.
307         The reason is that Subversion stories information about
308         the current state of a working copy
309         in special sub-directories called <code>.svn</code>:
310       </p>
311
312 <pre>
313 $ <span class="in">pwd</span>
314 <span class="out">/home/vlad/monsters</span>
315 $ <span class="in">ls -a</span>
316 <span class="out">.    ..    .svn    earth    jupiter    mars</span>
317 $ <span class="in">ls -F .svn</span>
318 <span class="out">entries    prop-base/    props/    text-base/    tmp/</span>
319 </pre>
320
321       <p class="continue">
322         If two working copies overlap,
323         the files in the <code>.svn</code> directories for one repository
324         will be clobbered by the other repository's <code>.svn</code> files,
325         and Subversion will become hopelessly confused.
326       </p>
327     </div>
328
329   <p>
330     Dracula can find out more about the history of the project
331     using Subversion's <code>log</code> command:
332   </p>
333
334 <pre>
335 $ <span class="in">svn log</span>
336 <span class="out">------------------------------------------------------------------------
337 r6 | mummy | 2010-07-26 09:21:10 -0400 (Mon, 26 Jul 2010) | 1 line
338
339 Damn the budget---the Jovian moons would be a _perfect_ place to explore.
340 ------------------------------------------------------------------------
341 r5 | mummy | 2010-07-26 09:19:39 -0400 (Mon, 26 Jul 2010) | 1 line
342
343 The budget might not even stretch to the Arctic :-(
344 ------------------------------------------------------------------------
345 r4 | mummy | 2010-07-26 09:17:46 -0400 (Mon, 26 Jul 2010) | 1 line
346
347 Budget cuts may force us to do another dry run in the Arctic.
348 ------------------------------------------------------------------------
349 r3 | mummy | 2010-07-26 09:14:14 -0400 (Mon, 26 Jul 2010) | 1 line
350
351 Converting document to wiki-formatted text.
352 ------------------------------------------------------------------------
353 r2 | mummy | 2010-07-26 09:11:55 -0400 (Mon, 26 Jul 2010) | 1 line
354
355 Or put it down near the Face of Cydonia?
356 ------------------------------------------------------------------------
357 r1 | mummy | 2010-07-26 09:08:23 -0400 (Mon, 26 Jul 2010) | 1 line
358
359 Send the probe to Mons Olympus?
360 ------------------------------------------------------------------------</span>
361 </pre>
362
363   <p class="continue">
364     Subversion displays a summary of all the changes made to the project so far.
365     This list includes the
366     <a href="glossary.html#revision-number">revision number</a>,
367     the name of the person who made the change,
368     the date the change was made,
369     and whatever comment the user provided when the change was submitted.
370     As we can see,
371     the <code>monsters</code> project is currently at revision 6,
372     and all changes so far have been made by the Mummy.
373   </p>
374
375   <p>
376     Notice how detailed the comments on the updates are.
377     Good comments are as important in version control as they are in coding.
378     Without them, it can be very difficult to figure out who did what, when, and why.
379     We can use comments like "Changed things" and "Fixed it" if we want,
380     or even no comments at all,
381     but we'll only be making more work for our future selves.
382   </p>
383
384   <div class="box">
385     <h3>Numbering Versions</h3>
386
387     <p>
388       Another thing to notice is that the revision number applies to the whole repository,
389       not to a particular file.
390       When we talk about "version 61" we mean
391       "the state of all files and directories at that point."
392       Older version control systems like CVS gave each file a new version number when it was updated,
393       which meant that version 38 of one file could correspond in time to version 17 of another
394       (<a href="#f:version_numbering">Figure 7</a>).
395       Experience shows that
396       global version numbers that apply to everything in the repository
397       are easier to manage than
398       per-file version numbers,
399       so that's what Subversion uses.
400     </p>
401
402     <figure id="f:version_numbering">
403       <img src="svn/version_numbering.png" alt="Version Numbering Schemes" />
404       <figcaption>Figure 7: Version Numbering Schemes</figcaption>
405     </figure>
406   </div>
407
408   <p>
409     A couple of cubicles away,
410     Wolfman also runs <code>svn checkout</code>
411     to get a working copy of the repository.
412     He also gets version 6,
413     so the files on his machine are the same as the files on Dracula's.
414     While he is looking through the files,
415     Dracula decides to add some information to the repository about Jupiter's moons.
416     Using his favorite editor,
417     he creates a file in the <code>jupiter</code> directory called <code>moons.txt</code>,
418     and fills it with information about Io, Europa, Ganymede, and Callisto:
419   </p>
420
421 <pre src="svn/moons_initial.txt">
422 Name            Orbital Radius  Orbital Period  Mass            Radius
423 Io              421.6           1.769138        893.2           1821.6
424 Europa          670.9           3.551181        480.0           1560.8
425 Ganymede        1070.4          7.154553        1481.9          2631.2
426 Calisto         1882.7          16.689018       1075.9          2410.3
427 </pre>
428
429   <p>
430     After double-checking his data,
431     he wants to commit the file to the repository so that everyone else on the project can see it.
432     The first step is to add the file to his working copy using <code>svn add</code>:
433   </p>
434
435 <pre>
436 $ <span class="in">svn add jupiter/moons.txt</span>
437 <span class="out">A         jupiter/moons.txt</span>
438 </pre>
439
440   <p>
441     Adding a file is not the same as creating it&mdash;he has already done that.
442     Instead,
443     the <code>svn add</code> command tells Subversion to add the file to
444     the list of things it's supposed to manage.
445     It's quite common,
446     particularly in programming projects,
447     to have backup files or intermediate files in a directory
448     that aren't worth storing in the repository.
449     This is why version control requires us to explicitly tell it which files are to be managed.
450   </p>
451
452   <p>
453     Once he has told Subversion to add the file,
454     Dracula can go ahead and commit his changes to the repository.
455     He uses the <code>-m</code> flag to provide a one-line message explaining what he's doing;
456     if he didn't,
457     Subversion would open his default editor
458     so that he could type in something longer.
459   </p>
460
461 <pre>
462 $ <span class="in">svn commit -m "Some basic facts about the Galilean moons of Jupiter." jupiter/moons.txt</span>
463 <span class="out">Adding         jupiter/moons.txt
464 Transmitting file data .
465 Committed revision 7.</span>
466 </pre>
467
468   <p>
469     When Dracula runs the <code>svn commit</code> command,
470     Subversion establishes a connection to the server,
471     copies over his changes,
472     and updates the revision number from 6 to 7
473     (<a href="#f:updated_repo">Figure 8</a>).
474   </p>
475
476   <figure id="f:updated_repo">
477     <img src="svn/updated_repo.png" alt="Updated Repository" />
478     <figcaption>Figure 8: Updated Repository</figcaption>
479   </figure>
480
481   <p id="a:define-head">
482     Back in his cubicle,
483     Wolfman uses <code>svn update</code> to update his working copy.
484     It tells him that a new file has been added
485     and brings his working copy up to date with version 7 of the repository,
486     because this is now the most recent revision
487     (also called the <a href="glossary.html#head">head</a>).
488     <code>svn update</code> updates an existing working copy,
489     rather than checking out a new one.
490     While <code>svn checkout</code> is usually only run once per project per machine,
491     <code>svn update</code> may be run many times a day.
492   </p>
493
494   <p>
495     Looking in the new file <code>jupiter/moons.txt</code>,
496     Wolfman notices that Dracula has misspelled "Callisto"
497     (it is supposed to have two L's.)
498     Wolfman edits that line of the file:
499   </p>
500
501 <pre src="svn/moons_spelling.txt">
502 Name            Orbital Radius  Orbital Period  Mass            Radius
503 Io              421.6           1.769138        893.2           1821.6
504 Europa          670.9           3.551181        480.0           1560.8
505 Ganymede        1070.4          7.154553        1481.9          2631.2
506 <span class="highlight">Callisto        1882.7          16.689018       1075.9          2410.3</span>
507 </pre>
508
509   <p class="continue">
510     He also adds a line about Amalthea,
511     which he thinks might be an interesting place to send a probe
512     despite its small size:
513   </p>
514
515 <pre src="svn/moons_amalthea.txt">
516 Name            Orbital Radius  Orbital Period  Mass            Radius
517 <span class="highlight">Amalthea        181.4           0.498179        0.075           125.0</span>
518 Io              421.6           1.769138        893.2           1821.6
519 Europa          670.9           3.551181        480.0           1560.8
520 Ganymede        1070.4          7.154553        1481.9          2631.2
521 Callisto        1882.7          16.689018       1075.9          2410.3
522 </pre>
523
524   <p>
525     Next,
526     he uses the <code>svn status</code> command to check that he hasn't accidentally changed anything else:
527   </p>
528
529 <pre>
530 $ <span class="in">svn status</span>
531 <span class="out">M       jupiter/moons.txt</span>
532 </pre>
533
534   <p class="continue">
535     and then runs <code>svn commit</code>.
536     Since has hasn't used the <code>-m</code> flag to provide a message on the command line,
537     Subversion launches his default editor and shows him:
538   </p>
539
540 <pre>
541
542 --This line, and those below, will be ignored--
543
544 M    jupiter/moons.txt
545 </pre>
546
547   <p>
548     He changes this to be
549   </p>
550
551 <pre>
552 1. Fixed typo in moon's name: 'Calisto' -> 'Callisto'.
553 2. Added information about Amalthea.
554 --This line, and those below, will be ignored--
555
556 M    jupiter/moons.txt
557 </pre>
558
559   <p class="continue">
560     When he saves this temporary file and exits the editor,
561     Subversion commits his changes:
562   </p>
563
564 <pre>
565 <span class="out">Sending        jupiter/moons.txt
566 Transmitting file data .
567 Committed revision 8.</span>
568 </pre>
569
570   <p class="continue">
571     Note that since Wolfman didn't specify a particular file to commit,
572     Subversion commits <em>all</em> of his changes.
573     This is why he ran the <code>svn status</code> command first.
574   </p>
575
576   <div class="box">
577     <h3>Which Editor?</h3>
578     <p>
579       If you don't have a default editor set up,
580       Subversion will probably open an editor called Vi.
581       If this happens,
582       type escape-colon-w-q-! to exit
583       and hope it never happens again.
584     </p>
585   </div>
586
587   <div class="box" id="b:basics:transaction">
588     <h3>Working With Multiple Files</h3>
589
590     <p>
591       Our example only includes one file,
592       but version control can work on any number of files at once.
593       For example,
594       if Wolfman noticed that a dozen data files had the same incorrect header,
595       he could change it in all 12 files,
596       then commit all those changes at once.
597       This is actually the best way to work:
598       every logical change to the project should be a single commit,
599       and every commit should include everything involved in one logical change.
600     </p>
601
602   </div>
603
604   <p>
605     That night,
606     Dracula wants to synchronize with Wolfman's work.
607     Before updating his working copy with <code>svn update</code>,
608     though,
609     he checks to see if he has made any changes locally
610     by running <code>svn diff</code>.
611     Without arguments,
612     it compares what's in his working copy to what he got the last time he updated.
613     There are no differences,
614     so there's no output:
615   </p>
616
617 <pre>
618 $ <span class="in">svn diff</span>
619 $
620 </pre>
621
622   <p class="continue">
623     To compare his working copy to the master,
624     Dracula uses <code>svn diff -r HEAD</code>.
625     The <code>-r</code> flag is used to specify a revision,
626     while <code>HEAD</code> means
627     "<a href="#a:define-head">the latest version of the master</a>".
628   </p>
629
630 <pre>
631 $ <span class="in">svn diff -r HEAD</span>
632 <span class="out">--- moons.txt(revision 8)
633 +++ moons.txt(working copy)
634 @@ -1,5 +1,6 @@
635  Name            Orbital Radius  Orbital Period  Mass            Radius
636 +Amalthea        181.4           0.498179        0.075           125.0
637  Io              421.6           1.769138        893.2           1821.6
638  Europa          670.9           3.551181        480.0           1560.8
639  Ganymede        1070.4          7.154553        1481.9          2631.2
640 -Calisto         1882.7          16.689018       1075.9          2410.3
641 +Callisto        1882.7          16.689018       1075.9          2410.3
642 </span>
643 </pre>
644
645   <p class="continue">
646     After looking over the changes,
647     Dracula goes ahead and does the update.
648   </p>
649
650   <div class="box">
651     <h3>Reading a Diff</h3>
652
653     <p>
654       The output of <code>diff</code> is cryptic even by Unix standards.
655       The first two lines:
656     </p>
657
658 <pre>
659 --- moons.txt(revision 9)
660 +++ moons.txt(working copy)
661 </pre>
662
663     <p class="continue">
664       signal that '-' will be used to show content from revision 9
665       and '+' to show content from the user's working copy.
666       The next line, with the '@' markers,
667       indicates where lines were inserted or removed.
668       This isn't really intended for human consumption:
669       editors and other tools can use this information
670       to replay a series of edits against a file.
671     </p>
672
673     <p>
674       The most important parts of what follows are the lines marked with '+' and '-',
675       which show insertions and deletions respectively.
676       Here,
677       we can see that the line for Amalthea was inserted,
678       and that the line for Callisto was changed
679       (which is indicated by an add and a delete right next to one another).
680       Many editors and other tools can display diffs like this in a two-column display,
681       highlighting changes.
682     </p>
683
684   </div>
685
686   <div class="box">
687     <h3>Diffing Other Files</h3>
688
689     <p>
690       <code>svn diff</code> mimics the behavior of
691       the Unix <code>diff</code> command,
692       which can be used to compare any two files.
693       Given these two files:
694     </p>
695
696     <table>
697       <tr>
698         <th><code>left.txt</code></th>
699         <th><code>right.txt</code></th>
700       </tr>
701       <tr>
702         <td valign="top">
703 <pre>hydrogen
704 lithium
705 sodium
706 magnesium
707 rubidium</pre>
708         </td>
709         <td valign="top">
710 <pre>hydrogen
711 lithium
712 beryllium
713 sodium
714 potassium
715 strontium</pre>
716         </td>
717       </tr>
718     </table>
719
720     <p class="continue">
721       <code>diff</code>'s output is:
722     </p>
723 <pre>
724 $ <span class="in">diff left.txt right.txt</span>
725 <span class="out">2a3
726 &gt; beryllium
727 4,5c5,6
728 &lt; magnesium
729 &lt; rubidium
730 ---
731 &gt; potassium
732 &gt; strontium</span>
733 </pre>
734   </div>
735
736   <p>
737     This is a very common workflow,
738     and is the basic heartbeat of most developers' days.
739     The steps are:
740   </p>
741
742   <ol>
743
744     <li>
745       Update our working copy
746       so that we have any changes other people have committed.
747     </li>
748
749     <li>
750       Do our own work.
751     </li>
752
753     <li>
754       Commit our changes to the repository
755       so that other people can get them.
756     </li>
757
758   </ol>
759
760   <p>
761     It's worth noticing here how important Wolfman's comments about his changes were.
762     It's hard to see the difference between "Calisto" with one 'L' and "Callisto" with two,
763     even if the line containing the difference has been highlighted.
764     Without Wolfman's comments,
765     Dracula might have wasted time wondering what the difference was.
766   </p>
767
768   <p>
769     In fact,
770     Wolfman should probably have committed his two changes separately,
771     since there's no logical connection between
772     fixing a typo in Callisto's name
773     and adding information about Amalthea to the same file.
774     Just as a function or program should do one job and one job only,
775     a single commit to version control should have a single logical purpose so that it's easier to find,
776     understand,
777     and if necessary undo later on.
778   </p>
779
780   <div class="keypoints">
781     <h3>Summary</h3>
782     <ul>
783       <li>Version control is a better way to manage shared files than email or shared folders.</li>
784       <li>The master copy is stored in a repository.</li>
785       <li>Nobody ever edits the master directory: instead, each person edits a local working copy.</li>
786       <li>People share changes by committing them to the master or updating their local copy from the master.</li>
787       <li>The version control system prevents people from overwriting each other's work by forcing them to merge concurrent changes before committing.</li>
788       <li>It also keeps a complete history of changes made to the master so that old versions can be recovered reliably.</li>
789       <li>Version control systems work best with text files, but can also handle binary files such as images and Word documents.</li>
790       <li>Every repository is identified by a URL.</li>
791       <li>Working copies of different repositories may not overlap.</li>
792       <li>Each changed to the master copy is identified by a unique revision number.</li>
793       <li>Revisions identify snapshots of the entire repository, not changes to individual files.</li>
794       <li>Each change should be commented to make the history more readable.</li>
795       <li>Commits are transactions: either all changes are successfully committed, or none are.</li>
796       <li>The basic workflow for version control is update-change-commit.</li>
797       <li><code>svn add <em>things</em></code> tells Subversion to start managing particular files or directories.</li>
798       <li><code>svn checkout <em>url</em></code> checks out a working copy of a repository.</li>
799       <li><code>svn commit -m "<em>message</em>" <em>things</em></code> sends changes to the repository.</li>
800       <li><code>svn diff</code> compares the current state of a working copy to the state after the most recent update.</li>
801       <li><code>svn diff -r HEAD</code> compares the current state of a working copy to the state of the master copy.</li>
802       <li><code>svn history</code> shows the history of a working copy.</li>
803       <li><code>svn status</code> shows the status of a working copy.</li>
804       <li><code>svn update</code> updates a working copy from the repository.</li>
805     </ul>
806   </div>
807
808   <div class="challenges">
809     <h3>Challenges</h3>
810
811     <ol>
812
813       <li>
814         Using the repository URL, user ID, and password provided by the instructor,
815         perform the following actions:
816         <ol>
817           <li>
818             Check out a working copy of the repository.
819           </li>
820           <li>
821             Create a text file called <em>your_id</em>.txt
822             (using your user ID instead of <em>your_id</em>)
823             and write a three-line biography of yourself in it.
824           </li>
825           <li>
826             Add this file to your working copy.
827           </li>
828           <li>
829             Commit your changes to the repository.
830           </li>
831           <li>
832             Update your working copy to get other people's biographies.
833           </li>
834           <li>
835             Examine the change log to see
836             the order in which people added their biographies
837             to the repository.
838           </li>
839         </ol>
840       </li>
841
842       <li>
843         What does the command <code>svn diff -r 14</code> do?
844         What does it do if there have only been 10 changes to the repository?
845       </li>
846
847       <li>
848         By default,
849         Unix <code>diff</code> and <code>svn diff</code> compare files line by line.
850         Why doesn't this work for MP3 audio files?
851       </li>
852
853     </ol>
854   </div>
855
856 </section>
857
858     <section id="s:merge">
859
860       <h2>Merging Conflicts</h2>
861
862       <div class="understand" id="u:merge">
863         <h3>Understand:</h3>
864         <ul>
865           <li>What a conflict in an update is.</li>
866           <li>How to resolve conflicts when updating.</li>
867         </ul>
868       </div>
869
870       <p>
871         Dracula and Wolfman have both synchronized their working copies of <code>monsters</code>
872         with version 8 of the repository.
873         Dracula now edits his copy to change Amalthea's radius
874         from a single number to a triple to reflect its irregular shape:
875       </p>
876
877 <pre src="svn/moons_dracula_triple.txt">
878 Name            Orbital Radius  Orbital Period  Mass            Radius
879 <span class="highlight">Amalthea        181.4           0.498179        0.075           131 x 73 x 67</span>
880 Io              421.6           1.769138        893.2           1821.6
881 Europa          670.9           3.551181        480.0           1560.8
882 Ganymede        1070.4          7.154553        1481.9          2631.2
883 Callisto        1882.7          16.689018       1075.9          2410.3
884 </pre>
885
886       <p class="continue">
887         He then commits his work,
888         creating revision 9 of the repository
889         (<a href="#f:after_dracula_commits">Figure XXX</a>).
890       </p>
891
892       <figure id="f:after_dracula_commits">
893         <img src="svn/after_dracula_commits.png" alt="After Dracula Commits" />
894       </figure>
895
896       <p>
897         But while he is doing this,
898         Wolfman is editing <em>his</em> copy
899         to add information about two other minor moons,
900         Himalia and Elara:
901       </p>
902
903 <pre src="svn/moons_wolfman_extras.txt">
904 Name            Orbital Radius  Orbital Period  Mass            Radius
905 Amalthea        181.4           0.498179        0.075           131
906 Io              421.6           1.769138        893.2           1821.6
907 Europa          670.9           3.551181        480.0           1560.8
908 Ganymede        1070.4          7.154553        1481.9          2631.2
909 Callisto        1882.7          16.689018       1075.9          2410.3
910 <span class="highlight">Himalia         11460           250.5662        0.095           85.0
911 Elara           11740           259.6528        0.008           40.0</span>
912 </pre>
913
914       <p>
915         When Wolfman tries to commit his changes to the repository,
916         Subversion won't let him:
917       </p>
918
919 <pre>
920 $ <span class="in">svn commit -m "Added data for Himalia, Elara"</span>
921 <span class="out">Sending        jupiter/moons.txt
922 svn: Commit failed (details follow):
923 svn: File or directory 'moons.txt' is out of date; try updating
924 svn: resource out of date; try updating</span>
925 </pre>
926
927       <p class="continue">
928         The reason is that
929         Wolfman's changes were based on revision 8,
930         but the repository is now at revision 9,
931         and the file that Wolfman is trying to overwrite
932         is different in the later revision.
933         (Remember,
934         one of version control's main jobs is to make sure that
935         people don't trample on each other's work.)
936         Wolfman has to update his working copy to get Dracula's changes before he can commit.
937         Luckily,
938         Dracula edited a line that Wolfman didn't change,
939         so Subversion can merge the differences automatically.
940       </p>
941
942       <p>
943         This does <em>not</em> mean that Wolfman's changes have been committed to the repository:
944         Subversion only does that when it's ordered to.
945         Wolfman's changes are still in his working copy,
946         and <em>only</em> in his working copy.
947         But since Wolfman's version of the file now includes
948         the lines that Dracula added,
949         Wolfman can go ahead and commit them as usual to create revision 10.
950       </p>
951
952       <p>
953         Wolfman's working copy is now in sync with the master,
954         but Dracula's is one behind at revision 9.
955         At this point,
956         they independently decide to add measurement units
957         to the columns in <code>moons.txt</code>.
958         Wolfman is quicker off the mark this time;
959         he adds a line to the file:
960       </p>
961
962 <pre src="svn/moons_wolfman_units.txt">
963 Name            Orbital Radius  Orbital Period  Mass            Radius
964 <span class="highlight">                (10**3 km)      (days)          (10**20 kg)     (km)</span>
965 Amalthea        181.4           0.498179        0.075           131 x 73 x 67
966 Io              421.6           1.769138        893.2           1821.6
967 Europa          670.9           3.551181        480.0           1560.8
968 Ganymede        1070.4          7.154553        1481.9          2631.2
969 Callisto        1882.7          16.689018       1075.9          2410.3
970 Himalia         11460           250.5662        0.095           85.0
971 Elara           11740           259.6528        0.008           40.0
972 </pre>
973
974       <p class="continue">
975         and commits it to create revision 11.
976         While he is doing this,
977         though,
978         Dracula inserts a different line at the top of the file:
979       </p>
980
981 <pre src="svn/moons_dracula_units.txt">
982 Name            Orbital Radius  Orbital Period  Mass            Radius
983 <span class="highlight">                * 10^3 km       * days          * 10^20 kg      * km</span>
984 Amalthea        181.4           0.498179        0.075           131 x 73 x 67
985 Io              421.6           1.769138        893.2           1821.6
986 Europa          670.9           3.551181        480.0           1560.8
987 Ganymede        1070.4          7.154553        1481.9          2631.2
988 Callisto        1882.7          16.689018       1075.9          2410.3
989 Himalia         11460           250.5662        0.095           85.0
990 Elara           11740           259.6528        0.008           40.0
991 </pre>
992
993       <p>
994         Once again,
995         when Dracula tries to commit,
996         Subversion tells him he can't.
997         But this time,
998         when Dracula does updates his working copy,
999         he doesn't just get the line Wolfman added to create revision 11.
1000         There is an actual conflict in the file,
1001         so Subversion asks Dracula what he wants to do:
1002       </p>
1003
1004 <pre src="svn/moons_dracula_conflict.txt">
1005 $ <span class="in">svn update</span>
1006 <span class="out">Conflict discovered in 'jupiter/moons.txt'.
1007 Select: (p) postpone, (df) diff-full, (e) edit,
1008         (mc) mine-conflict, (tc) theirs-conflict,
1009         (s) show all options:</span>
1010 </pre>
1011
1012       <p>
1013         Dracula choose <code>p</code> for "postpone",
1014         which tells Subversion that he'll deal with the problem later.
1015         Once the update is finished,
1016         he opens <code>moons.txt</code> in his editor and sees:
1017       </p>
1018
1019 <pre>
1020  Name            Orbital Radius  Orbital Period  Mass
1021 +&lt;&lt;&lt;&lt;&lt;&lt;&lt; .mine
1022          +                * 10^3 km       * days         * 10^20 kg
1023 +=======
1024 +                (10**3 km)      (days)         (10**20 kg)
1025 +&gt;&gt;&gt;&gt;&gt;&gt;&gt; .r11
1026  Amalthea        181.4           0.498179        0.074
1027  Io              421.6           1.769138        893.2
1028  Europa          670.9           3.551181        480.0
1029  Ganymede        1070.4          7.154553        1481.9
1030  Callisto        1882.7          16.689018       1075.9
1031 </pre>
1032
1033       <p class="continue">
1034         As we can see,
1035         Subversion has inserted
1036         <a href="glossary.html#conflict-marker">conflict markers</a>
1037         in <code>moons.txt</code>
1038         wherever there is a conflict.
1039         The line <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt; .mine</code> shows the start of the conflict,
1040         and is followed by the lines from the local copy of the file.
1041         The separator <code>=======</code> is then
1042         followed by the lines from the repository's file that are in conflict with that section,
1043         while <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt; .r11</code> marks the end of the conflict.
1044       </p>
1045
1046       <p>
1047         Before he can commit,
1048         Dracula has to edit his copy of the file to get rid of those markers.
1049         He changes it to:
1050       </p>
1051
1052 <pre src="svn/moons_dracula_resolved.txt">
1053 Name            Orbital Radius  Orbital Period  Mass            Radius
1054 <span class="highlight">                (10^3 km)       (days)          (10^20 kg)      (km)</span>
1055 Amalthea        181.4           0.498179        0.075           131 x 73 x 67
1056 Io              421.6           1.769138        893.2           1821.6
1057 Europa          670.9           3.551181        480.0           1560.8
1058 Ganymede        1070.4          7.154553        1481.9          2631.2
1059 Callisto        1882.7          16.689018       1075.9          2410.3
1060 Himalia         11460           250.5662        0.095           85.0
1061 Elara           11740           259.6528        0.008           40.0
1062 </pre>
1063
1064       <p class="continue">
1065         then uses the <code>svn resolved</code> command to tell Subversion that
1066         he has fixed the problem.
1067         Subversion will now let him commit to create revision 12.
1068       </p>
1069
1070       <div class="box">
1071
1072         <h3>Auxiliary Files</h3>
1073
1074         <p>
1075           When Dracula did his update and Subversion detected the conflict in <code>moons.txt</code>,
1076           it created three temporary files to help Dracula resolve it.
1077           The first is called <code>moons.txt.r9</code>;
1078           it is the file as it was in Dracula's local copy
1079           before he started making changes,
1080           i.e., the common ancestor for his work
1081           and whatever he is in conflict with.
1082         </p>
1083
1084         <p>
1085           The second file is <code>moons.txt.r11</code>.
1086           This is the most up-to-date revision from the repository&mdash;the
1087           file as it is including Wolfman's changes.
1088           The third temporary file, <code>moons.txt.mine</code>,
1089           is the file as it was in Dracula's working copy before he did the Subversion update.
1090         </p>
1091
1092         <p>
1093           Subversion creates these auxiliary files primarily
1094           to help people merge conflicts in binary files.
1095           It wouldn't make sense to insert <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;</code>
1096           and <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt;</code> characters into an image file
1097           (it would almost certainly result in a corrupted image).
1098           The <code>svn resolved</code> command deletes these three extra files
1099           as well as telling Subversion that the conflict has been taken care of.
1100         </p>
1101
1102       </div>
1103
1104       <p>
1105         Some power users prefer to work with interpolated conflict markers directly,
1106         but for the rest of us,
1107         there are several tools for displaying differences and helping to merge them,
1108         including <a href="http://diffuse.sourceforge.net/">Diffuse</a> and <a href="http://winmerge.org/">WinMerge</a>.
1109         If Dracula launches Diffuse,
1110         it displays his file,
1111         the common base that he and Wolfman were working from,
1112         and Wolfman's file in a three-pane view
1113         (<a href="#f:diff_viewer">Figure XXX</a>):
1114       </p>
1115
1116       <figure id="f:diff_viewer">
1117         <img src="svn/diff_viewer.png" alt="A Difference Viewer" />
1118       </figure>
1119
1120       <p class="continue">
1121         Dracula can use the buttons to merge changes from either of the edited versions
1122         into the common ancestor,
1123         or edit the central pane directly.
1124         Again,
1125         once he is done,
1126         he uses <code>svn resolved</code> and <code>svn commit</code>
1127         to create revision 12 of the repository.
1128       </p>
1129
1130       <p>
1131         In this case, the conflict was small and easy to fix.
1132         However, if two or more people on a team are repeatedly creating conflicts for one another,
1133         it's usually a signal of deeper communication problems:
1134         either they aren't talking as often as they should, or their responsibilities overlap.
1135         If used properly,
1136         the version control system can help the team find and fix these issues
1137         so that it will be more productive in future.
1138       </p>
1139
1140       <div class="box">
1141
1142         <h3>Working With Multiple Files</h3>
1143
1144         <p>
1145           As mentioned <a href="#a:transaction">earlier</a>,
1146           every logical change to a project should result in a single commit,
1147           and every commit should represent one logical change.
1148           This is especially true when resolving conflicts:
1149           the work done to reconcile one person's changes with another are often complicated,
1150           so it should be a single entry in the project's history,
1151           with other, later, changes coming after it.
1152         </p>
1153
1154       </div>
1155
1156       <div class="keypoints" id="k:merge">
1157         <h3>Summary</h3>
1158         <ul>
1159           <li>Conflicts must be resolved before a commit can be completed.</li>
1160           <li>Subversion puts markers in text files to show regions of conflict.</li>
1161           <li>For each conflicted file, Subversion creates auxiliary files containing the common parent, the master version, and the local version.</li>
1162           <li><code>svn resolve <em>files</em></code> tells Subversion that conflicts have been resolved.</li>
1163         </ul>
1164       </div>
1165
1166     </section>
1167
1168     <section id="s:rollback">
1169
1170       <h2>Recovering Old Versions</h2>
1171
1172       <div class="understand" id="u:rollback">
1173         <h3>Understand:</h3>
1174         <ul>
1175           <li>How to undo changes to a working copy.</li>
1176           <li>How to recover old versions of files.</li>
1177           <li>What a branch is.</li>
1178         </ul>
1179       </div>
1180
1181       <p>
1182         Now that we have seen how to merge files and resolve conflicts,
1183         we can look at how to use version control as an "infinite undo".
1184         Suppose that when Wolfman starts work late one night,
1185         his copy of <code>monsters</code> is in sync with the head at revision 12.
1186         He decides to edit the file <code>moons.txt</code>;
1187         unfortunately, he forgot that there was a full moon,
1188         so his changes don't make a lot of sense:
1189       </p>
1190
1191 <pre src="svn/poetry.txt">
1192 Just one moon can make me growl
1193 Four would make me want to howl
1194 ...
1195 </pre>
1196
1197       <p>
1198         When he's back in human form the next day,
1199         he wants to undo his changes.
1200         Without version control, his choices would be grim:
1201         he could try to edit them back into their original state by hand
1202         (which for some reason hardly ever seems to work),
1203         or ask his colleagues to send him their copies of the files
1204         (which is almost as embarrassing as chasing the neighbor's cat when in wolf form).
1205       </p>
1206
1207       <p>
1208         Since he's using Subversion, though,
1209         and hasn't committed his work to the repository,
1210         all he has to do is <a href="glossary.html#revert">revert</a> his local changes.
1211         <code>svn revert</code> simply throws away local changes to files
1212         and puts things back the way they were before those changes were made.
1213         This is a purely local operation:
1214         since Subversion stores the history of the project inside every working copy,
1215         Wolfman doesn't need to be connected to the network to do this.
1216       </p>
1217
1218       <p>
1219         To start,
1220         Wolfman uses <code>svn diff</code> <em>without</em> the <code>-r HEAD</code> flag
1221         to take a look at the differences between his file
1222         and the master copy in the repository.
1223         Since he doesn't want to keep his changes,
1224         his next command is <code>svn revert moons.txt</code>.
1225       </p>
1226
1227 <pre>
1228 $ <span class="in">cd jupiter</span>
1229 $ <span class="in">svn revert moons.txt</span>
1230 <span class="out">Reverted   moons.txt</span>
1231 </pre>
1232
1233       <p>
1234         What if someone <em>has</em> committed their changes,
1235         but still wants to undo them?
1236         For example,
1237         suppose Dracula decides that the numbers in <code>moons.txt</code> would look better with commas.
1238         He edits the file to put them in:
1239       </p>
1240
1241 <pre src="svn/moons_commas.txt">
1242 Name            Orbital Radius  Orbital Period  Mass            Radius
1243                 (10^3 km)       (days)          (10^20 kg)      (km)
1244 Amalthea        181.4           0.498179          0.075      131 x 73 x 67
1245 Io              421.6           1.769138        893.2          1<span class="highlight">,</span>821.6
1246 Europa          670.9           3.551181        480.0          1<span class="highlight">,</span>560.8
1247 Ganymede      1<span class="highlight">,</span>070.4           7.154553      1<span class="highlight">,</span>481.9          2<span class="highlight">,</span>631.2
1248 Callisto      1<span class="highlight">,</span>882.7          16.689018      1<span class="highlight">,</span>075.9          2<span class="highlight">,</span>410.3
1249 Himalia      11<span class="highlight">,</span>460           250.5662            0.095           85.0
1250 Elara        11<span class="highlight">,</span>740           259.6528            0.008           40.0
1251 </pre>
1252
1253       <p class="continue">
1254         then commits his changes to create revision 13.
1255         A little while later,
1256         the Mummy sees the change and orders Dracula to put things back the way they were.
1257         What should Dracula do?
1258       </p>
1259
1260       <p>
1261         We can draw the sequence of events leading up to revision 13
1262         as shown in <a href="#f:before_undoing">Fixture XXX</a>:
1263       </p>
1264
1265       <figure id="f:before_undoing">
1266         <img src="svn/before_undoing.png" alt="Before Undoing" />
1267       </figure>
1268
1269       <p class="continue">
1270         Dracula wants to erase revision 13 from the repository,
1271         but he can't actually do that:
1272         once a change is in the repository,
1273         it's there forever.
1274         What he can do instead is merge the old revision with the current revision
1275         to create a new revision
1276         (<a href="#f:merging_history">Fixture XXX</a>).
1277       </p>
1278
1279       <figure id="f:merging_history">
1280         <img src="svn/merging_history.png" alt="Merging History" />
1281       </figure>
1282
1283       <p class="continue">
1284         This is exactly like merging changes made by two different people;
1285         the only difference is that the "other person" is his past self.
1286       </p>
1287
1288       <p>
1289         To undo his commas,
1290         Dracula must merge revision 12 (the one before his change)
1291         with revision 13 (the current head revision)
1292         using <code>svn merge</code>:
1293       </p>
1294
1295 <pre>
1296 $ <span class="in">svn merge -r HEAD:12 moons.txt</span>
1297 <span class="out">-- Reverse-merging r13 into 'moons.txt'
1298 U  moons.txt</span>
1299 </pre>
1300
1301       <p class="continue">
1302         The <code>-r</code> flag specifies the range of revisions to merge:
1303         to undo the changes from revision 12 to revision 13,
1304         he uses either <code>13:12</code> or <code>HEAD:12</code>
1305         (since he is going backward in time from the most recent revision to revision 12).
1306         This is called a <a href="glossary.html#reverse-merge">reverse</a> merge
1307         because he's going backward in time.
1308       </p>
1309
1310       <p>
1311         After he runs this command,
1312         he must run <code>svn commit</code> to save the changes to the repository.
1313         This creates a new revision, number 14,
1314         rather than erasing revision 13.
1315         That way,
1316         the changes he made to create revision 13 are still there
1317         if he can ever convince the Mummy that numbers should have commas.
1318       </p>
1319
1320       <p>
1321         Merging can be used to recover older revisions of files,
1322         not just the most recent,
1323         and to recover many files or directories at a time.
1324         The most frequent use, though,
1325         is to manage parallel streams of development in large projects.
1326         This is outside the scope of this chapter,
1327         but the basic idea is simple.
1328       </p>
1329
1330       <p>
1331         Suppose that Universal Monsters has just released a new program for designing secret lairs.
1332         Dracula and Wolfman are supposed to start adding a few features
1333         that had to be left out of the first release because time ran short.
1334         At the same time,
1335         Frankenstein and the Mummy are doing technical support:
1336         their job is to fix any bugs that users find.
1337         All sorts of things could go wrong if both teams tried to work on the same code at the same time.
1338         For example,
1339         if Frankenstein fixed a bug and sent a new copy of the program to a user in Greenland,
1340         it would be all too easy for him to accidentally include
1341         the half-completed shark tank control feature that Wolfman was working on.
1342       </p>
1343
1344       <p>
1345         The usual way to handle this situation is
1346         to create a <a href="glossary.html#branch">branch</a>
1347         in the repository for each major sub-project
1348         (<a href="#f:branch_merge">Figure XXX</a>).
1349         While Wolfman and Dracula work on
1350         the <a href="glossary.html#main-line">main line</a>,
1351         Frankenstein and the Mummy create a branch,
1352         which is just another copy of the repository's files and directories
1353         that is also under version control.
1354         They can work in their branch without disturbing Wolfman and Dracula and vice versa:
1355       </p>
1356
1357       <figure id="f:branch_merge">
1358         <img src="svn/branch_merge.png" alt="Branching and Merging" />
1359       </figure>
1360
1361       <p>
1362         Branches in version control repositories are often described as "parallel universes".
1363         Each branch starts off as a clone of the project at some moment in time
1364         (typically each time the software is released,
1365         or whenever work starts on a major new feature).
1366         Changes made to a branch only affect that branch,
1367         just as changes made to the files in one directory don't affect files in other directories.
1368         However,
1369         the branch and the main line are both stored in the same repository,
1370         so their revision numbers are always in step.
1371       </p>
1372
1373       <p>
1374         If someone decides that a bug fix in one branch should also be made in another,
1375         all they have to do is merge the files in question.
1376         This is exactly like merging an old version of a file with the current one,
1377         but instead of going backward in time,
1378         the change is brought sideways from one branch to another.
1379       </p>
1380
1381       <p>
1382         Branching helps projects scale up by letting sub-teams work independently,
1383         but too many branches can cause as many problems as they solve.
1384         Karl Fogel's excellent book
1385         <a href="bib.html#fogel-producing-oss"><cite>Producing Open Source Software</cite></a>,
1386         and Laura Wingerd and Christopher Seiwald's paper
1387         "<a href="bib.html#wingerd-seiwald-scm">High-level Best Practices in Software Configuration Management</a>",
1388         talk about branches in much more detail.
1389         Projects usually don't need to do this until they have a dozen or more developers,
1390         or until several versions of their software are in simultaneous use,
1391         but using branches is a key part of switching from software carpentry to software engineering.
1392       </p>
1393
1394       <div class="keypoints" id="k:rollback">
1395         <h3>Summary</h3>
1396         <ul>
1397           <li>Old versions of files can be recovered by merging their old state with their current state.</li>
1398           <li>Recovering an old version of a file does not erase the intervening changes.</li>
1399           <li>Use branches to support parallel independent development.</li>
1400           <li><code>svn merge</code> merges two revisions of a file.</li>
1401           <li><code>svn revert</code> undoes local changes to files.</li>
1402         </ul>
1403       </div>
1404
1405     </section>
1406
1407     <section id="s:setup">
1408
1409       <h2>Setting up a Repository</h2>
1410
1411       <div class="understand" id="u:setup">
1412         <h3>Understand:</h3>
1413         <ul>
1414           <li>How to create a repository.</li>
1415         </ul>
1416       </div>
1417
1418       <p>
1419         It is finally time to see how to create a repository.
1420         As a quick recap,
1421         we will keep the master copy of our work in a repository
1422         on a server that we can access from other machines on the internet.
1423         That master copy consists of files and directories that no-one ever edits directly.
1424         Instead, a copy of Subversion running on that machine
1425         manages updates for us and watches for conflicts.
1426         Our working copy is a mirror image of the master sitting on our computer.
1427         When our Subversion client needs to communicate with the master,
1428         it exchanges data with the copy of Subversion running on the server.
1429       </p>
1430
1431       <figure id="f:repo_four_things">
1432         <img src="svn/repo_four_things.png" alt="What's Needed for a Repository" />
1433       </figure>
1434
1435       <p>
1436         To make this to work, we need four things
1437         (<a href="#f:repo_four_things">Figure XXX</a>):
1438       </p>
1439
1440       <ol>
1441
1442         <li>
1443           The repository itself.
1444           It's not enough to create an empty directory and start filling it with files:
1445           Subversion needs to create a lot of other structure
1446           in order to keep track of old revisions, who made what changes, and so on.
1447         </li>
1448
1449         <li>
1450           The full URL of the repository.
1451           This includes the URL of the server
1452           and the path to the repository on that machine.
1453           (The second part is needed because a single server can,
1454           and usually will,
1455           host many repositories.)
1456         </li>
1457
1458         <li>
1459           Permission to read or write the master copy.
1460           Many open source projects give the whole world permission to read from their repository,
1461           but very few allow strangers to write to it:
1462           there are just too many possibilities for abuse.
1463           Somehow, we have to set up a password or something like it
1464           so that users can prove who they are.
1465         </li>
1466
1467         <li>
1468           A working copy of the repository on our computer.
1469           Once the first three things are in place,
1470           this just means running the <code>checkout</code> command.
1471         </li>
1472
1473       </ol>
1474
1475       <p>
1476         To keep things simple,
1477         we will start by creating a repository on the machine that we're working on.
1478         This won't let us share our work with other people,
1479         but it <em>will</em> allow us to save the history of our work as we go along.
1480       </p>
1481
1482       <p>
1483         The command to create a repository is <code>svnadmin create</code>,
1484         followed by the path to the repository.
1485         If we want to create a repository called <code>lair_repo</code>
1486         directly under our home directory,
1487         we just <code>cd</code> to get home
1488         and run <code>svnadmin create lair_repo</code>.
1489         This command creates a directory called <code>lair_repo</code> to hold our repository,
1490         and fills it with various files that Subversion uses
1491         to keep track of the project's history:
1492       </p>
1493
1494 <pre>
1495 $ <span class="in">cd</span>
1496 $ <span class="in">svnadmin create lair_repo</span>
1497 $ <span class="in">ls -F lair_repo</span>
1498 <span class="out">README.txt    conf/    db/    format    hooks/    locks/</span>
1499 </pre>
1500
1501       <p class="continue">
1502         We should <em>never</em> edit anything in this repository directly.
1503         Doing so probably won't shred our sanity and leave us gibbering in mindless horror,
1504         but it will almost certainly make the repository unusable.
1505       </p>
1506
1507       <p>
1508         To get a working copy of this repository,
1509         we use Subversion's <code>checkout</code> command.
1510         If our home directory is <code>/users/mummy</code>,
1511         then the full path to the repository we just created is <code>/users/mummy/lair_repo</code>,
1512         so we run <code>svn checkout file:///users/mummy/lair lair_working</code>.
1513       </p>
1514
1515       <p>
1516         Working backward,
1517         the second argument,
1518         <code>lair_working</code>,
1519         specifies where the working copy is to be put.
1520         The first argument is the URL of our repository,
1521         and it has two parts.
1522         <code>/users/mummy/lair_repo</code> is the path to repository directory.
1523         <code>file://</code> specifies the <a href="glossary.html#protocol">protocol</a>
1524         that Subversion will use to communicate with the repository&mdash;in this case,
1525         it says that the repository is part of the local machine's filesystem.
1526         Notice that the protocol ends in two slashes,
1527         while the absolute path to the repository starts with a slash,
1528         making three in total.
1529         A very common mistake is to type only two, since that's what web URLs normally have.
1530       </p>
1531
1532       <p>
1533         When we're doing a checkout,
1534         it is <em>very</em> important that we provide the second argument,
1535         which specifies the name of the directory we want the working copy to be put in.
1536         Without it,
1537         Subversion will try to use the name of the repository,
1538         <code>lair_repo</code>,
1539         as the name of the working copy.
1540         Since we're in the directory that contains the repository,
1541         this means that Subversion will try to overwrite the repository with a working copy.
1542         Again,
1543         there isn't much risk of our sanity being torn to shreds,
1544         but this could ruin our repository.
1545       </p>
1546
1547       <p>
1548         To avoid this problem,
1549         most people create a sub-directory in their account called something like <code>repos</code>,
1550         and then create their repositories in that.
1551         For example,
1552         we could create our repository in <code>/users/mummy/repos/lair</code>,
1553         then check out a working copy as <code>/users/mummy/lair</code>.
1554         This practice makes both names easier to read.
1555       </p>
1556
1557       <p>
1558         The obvious next steps are
1559         to put our repository on a server,
1560         rather than on our personal machine,
1561         and to give other people access to the repository we have just created
1562         so that they can work with us.
1563         We'll discuss the first in <a href="web.html#s:svn">a later chapter</a>,
1564         but unfortunately,
1565         the second really does require things that we are not going to cover in this course.
1566         If you want to do this, you can:
1567       </p>
1568
1569       <ul>
1570
1571         <li>
1572           ask your system administrator to set it up for you;
1573         </li>
1574
1575         <li>
1576           use an open source hosting service like <a href="http://www.sf.net">SourceForge</a>,
1577           <a href="http://code.google.com">Google Code</a>,
1578           <a href="https://github.com/">GitHub</a>,
1579           or <a href="https://bitbucket.org/">BitBucket</a>; or
1580         </li>
1581
1582         <li>
1583           spend a few dollars a month on a commercial hosting service like <a href="http://dreamhost.com">DreamHost</a>
1584           that provides web-based GUIs for creating and managing repositories.
1585         </li>
1586
1587       </ul>
1588
1589       <p>
1590         If you choose the second or third option,
1591         please check with whoever handles intellectual property at your institution
1592         to make sure that putting your work on a commercially-operated machine
1593         that is probably in some other legal jurisdiction
1594         isn't going to cause trouble.
1595         Many people assume that it's "just OK",
1596         while others act as if not having asked will be an acceptable defence later on.
1597         Unfortunately,
1598         neither is true&hellip;
1599       </p>
1600
1601       <div class="keypoints" id="k:setup">
1602         <h3>Summary</h3>
1603         <ul>
1604           <li>Repositories can be hosted locally, on local (departmental) servers, on hosting services, or on their owners' own domains.</li>
1605           <li><code>svnadmin create <em>name</em></code> creates a new repository.</li>
1606         </ul>
1607       </div>
1608
1609     </section>
1610
1611     <section id="s:provenance">
1612
1613       <h2>Provenance</h2>
1614
1615       <div class="understand" id="u:provenance">
1616         <h3>Understand:</h3>
1617         <ul>
1618           <li>What data provenance is.</li>
1619           <li>How to embed version numbers and other information in files managed by version control.</li>
1620           <li>How to record version information about a program in its output.</li>
1621         </ul>
1622       </div>
1623
1624       <p>
1625         In art,
1626         the <a href="glossary.html#provenance">provenance</a> of a work
1627         is the history of who owned it, when, and where.
1628         In science,
1629         it's the record of how a particular result came to be:
1630         what raw data was processed by what version of what program to create which intermediate files,
1631         what was used to turn those files into which figures of which papers,
1632         and so on.
1633       </p>
1634
1635       <p>
1636         One of the central ideas of this course is that
1637         wen can automatically track the provenance of scientific data.
1638         To start,
1639         suppose we have a text file <code>combustion.dat</code> in a Subversion repository.
1640         Run the following two commands:
1641       </p>
1642
1643 <pre>
1644 $ svn propset svn:keywords Revision combustion.dat
1645 $ svn commit -m "Turning on the 'Revision' keyword" combustion.dat
1646 </pre>
1647
1648       <p>
1649         Now open the file in an editor
1650         and add the following line somewhere near the top:
1651       </p>
1652
1653 <pre>
1654 # $Revision:$
1655 </pre>
1656
1657       <p>
1658         The '#' sign isn't important:
1659         it's just what <code>.dat</code> files use to show comments.
1660         The <code>$Revision:$</code> string,
1661         on the other hand,
1662         means something special to Subversion.
1663         Save the file, and commit the change:
1664       </p>
1665
1666 <pre>
1667 $ svn commit -m "Inserting the 'Revision' keyword" combustion.dat
1668 </pre>
1669
1670       <p>
1671         When we open the file again,
1672         we'll see that Subversion has changed that line to something like:
1673       </p>
1674
1675 <pre>
1676 # $Revision: 143$
1677 </pre>
1678
1679       <p class="continue">
1680         i.e., Subversion has inserted the version number
1681         after the colon and before the closing <code>$</code>.
1682       </p>
1683
1684       <p>
1685         Here's what just happened.
1686         First, Subversion allows you to set
1687         <a href="glossary.html#property-subversion">properties</a>
1688         for files and and directories.
1689         These properties aren't in the files or directories themselves,
1690         but live in Subversion's database.
1691         One of those properties,
1692         <code>svn:keywords</code>,
1693         tells Subversion to look in files that are being changed
1694         for strings of the form <code>$propertyname: &hellip;$</code>,
1695         where <code>propertyname</code> is a string like <code>Revision</code> or <code>Author</code>.
1696         (About half a dozen such strings are supported.)
1697       </p>
1698
1699       <p>
1700         If it sees such a string,
1701         Subversion rewrites it as the commit is taking place to replace <code>&hellip;</code>
1702         with the current version number,
1703         the name of the person making the change,
1704         or whatever else the property's name tells it to do.
1705         You only have to add the string to the file once;
1706         after that,
1707         Subversion updates it for you every time the file changes.
1708       </p>
1709
1710       <p>
1711         Putting the version number in the file this way can be pretty handy.
1712         If you copy the file to another machine,
1713         for example,
1714         it carries its version number with it,
1715         so you can tell which version you have even if it's outside version control.
1716         We'll see some more useful things we can do with this information in
1717         <a href="python.html">the next chapter</a>.
1718       </p>
1719
1720       <div class="box">
1721
1722         <h3>When <em>Not</em> to Use Version Control</h3>
1723
1724         <p>
1725           Despite the rapidly decreasing cost of storage,
1726           it is still possible to run out of disk space.
1727           In some labs,
1728           people can easy go through 2 TB/month if they're not careful.
1729           Since version control tools usually store revisions in terms of lines,
1730           with binary data files,
1731           they end up essentially storing every revision separately.
1732           This isn't that bad
1733           (it's what we'd be doing anyway),
1734           but it means version control isn't doing what it likes to do,
1735           and the repository can get very large very quickly.
1736           Another concern is that if very old data will no longer be used,
1737           it can be nice to archive or delete old data files.
1738           This is not possible if our data is version controlled:
1739           information can only be added to a repository,
1740           so it can only ever increase in size.
1741         </p>
1742
1743       </div>
1744
1745       <p>
1746         We can use this trick with shell scripts too,
1747         or with almost any other kind of program.
1748         Going back to Nelle Nemo's data processing from the previous chapter,
1749         for example,
1750         suppose she writes a shell script that uses <code>gooclean</code>
1751         to tidy up data files.
1752         Her first version looks like this:
1753       </p>
1754
1755 <pre>
1756 for filename in $*
1757 do
1758     gooclean -b 0 100 &lt; $filename &gt; cleaned-$filename
1759 done
1760 </pre>
1761
1762       <p class="continue">
1763         i.e., it runs <code>gooclean</code> with bounding values of 0 and 100
1764         for each specified file,
1765         putting the result in a temporary file with a well-defined name.
1766         Assuming that '#' is the comment character for those kinds of data files,
1767         she could instead write:
1768       </p>
1769
1770 <pre>
1771 for filename in $*
1772 do
1773     <span class="highlight">echo "gooclean $Revision: 901$ -b 0 100" &gt; $filename</span>
1774     gooclean -b 0 100 &lt; $filename <span class="highlight">&gt;&gt;</span> cleaned-$filename
1775 done
1776 </pre>
1777
1778       <p>
1779         The first change puts a line in the output file
1780         that describes how that file was created.
1781         The second change is to use <code>&gt;&gt;</code> instead of <code>&gt;</code>
1782         to redirect <code>gooclean</code>'s output to the file.
1783         <code>&gt;&gt;</code> means "append to":
1784         instead of overwriting whatever is in the file,
1785         it adds more content to it.
1786         This ensures that the first line of the file is the provenance record,
1787         with the actual output of <code>gooclean</code> after it.
1788       </p>
1789
1790       <div class="keypoints" id="k:provenance">
1791         <h3>Summary</h3>
1792         <ul>
1793           <li><code>$Keyword:$</code> in a file can be filled in with a property value each time the file is committed.</li>
1794           <li idea="paranoia">Put version numbers in programs' output to establish provenance for data.</li>
1795           <li><code>svn propset svn:keywords <em>property</em> <em>files</em></code> tells Subversion to start filling in property values.</li>
1796         </ul>
1797       </div>
1798
1799     </section>
1800
1801 <section id="s:summary">
1802   <h2>Summing Up</h2>
1803
1804   <p>
1805     Correlation does not imply causality,
1806     but there is a very strong correlation between
1807     using version control
1808     and doing good computational science.
1809     There's an equally strong correlation
1810     between <em>not</em> using it and either wasting effort or getting things wrong.
1811     Today (the middle of 2013),
1812     I will not review a paper if the software used in it
1813     is not under version control.
1814     The work it reports might be interesting,
1815     but without the kind of record-keeping that version control provides,
1816     there's no way to know exactly what its authors did.
1817     Just as importantly,
1818     if someone doesn't know enough about computing to use version control,
1819     the odds are good that they don't know enough
1820     to do the programming right either.
1821   </p>
1822
1823 </section>
1824 {% endblock content %}