Tidying up another section of the instructors' guide on SVN
[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/explore</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/explore</span>
256 </pre>
257
258   <p class="continue">
259     This creates a new directory called <code>explore</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    explore/jupiter
266 A    explore/mars
267 A    explore/mars/mons-olympus.txt
268 A    explore/mars/cydonia.txt
269 A    explore/earth
270 A    explore/earth/himalayas.txt
271 A    explore/earth/antarctica.txt
272 A    explore/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 explore</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/explore</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>explore</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   <h2>Merging Conflicts</h2>
860
861   <div class="understand">
862     <h3>Learning Objectives:</h3>
863     <ul>
864       <li>Explain what causes conflicts to occur and how to tell when one has occurred.</li>
865       <li>Resolve a conflict.</li>
866       <li>Identify the auxiliary files created when a conflict occurs.</li>
867     </ul>
868   </div>
869
870   <p>
871     Dracula and Wolfman have both synchronized their working copies of <code>explore</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 9</a>).
890   </p>
891
892   <figure id="f:after_dracula_commits">
893     <img src="svn/after_dracula_commits.png" alt="After Dracula Commits" />
894     <figcaption>Figure 9: After Dracula Commits</figcaption>
895   </figure>
896
897   <p>
898     But while he is doing this,
899     Wolfman is editing <em>his</em> copy
900     to add information about two other minor moons,
901     Himalia and Elara:
902   </p>
903
904 <pre src="svn/moons_wolfman_extras.txt">
905 Name            Orbital Radius  Orbital Period  Mass            Radius
906 Amalthea        181.4           0.498179        0.075           131
907 Io              421.6           1.769138        893.2           1821.6
908 Europa          670.9           3.551181        480.0           1560.8
909 Ganymede        1070.4          7.154553        1481.9          2631.2
910 Callisto        1882.7          16.689018       1075.9          2410.3
911 <span class="highlight">Himalia         11460           250.5662        0.095           85.0
912 Elara           11740           259.6528        0.008           40.0</span>
913 </pre>
914
915   <p>
916     When Wolfman tries to commit his changes to the repository,
917     Subversion won't let him:
918   </p>
919
920 <pre>
921 $ <span class="in">svn commit -m "Added data for Himalia, Elara"</span>
922 <span class="out">Sending        jupiter/moons.txt
923 svn: Commit failed (details follow):
924 svn: File or directory 'moons.txt' is out of date; try updating
925 svn: resource out of date; try updating</span>
926 </pre>
927
928   <p class="continue">
929     The reason is that
930     Wolfman's changes were based on revision 8,
931     but the repository is now at revision 9,
932     and the file that Wolfman is trying to overwrite
933     is different in the later revision.
934     (Remember,
935     one of version control's main jobs is to make sure that
936     people don't trample on each other's work.)
937     Wolfman has to update his working copy to get Dracula's changes before he can commit.
938     Luckily,
939     Dracula edited a line that Wolfman didn't change,
940     so Subversion can merge the differences automatically.
941   </p>
942
943   <p>
944     This does <em>not</em> mean that Wolfman's changes have been committed to the repository:
945     Subversion only does that when it's ordered to.
946     Wolfman's changes are still in his working copy,
947     and <em>only</em> in his working copy.
948     But since Wolfman's version of the file now includes
949     the lines that Dracula added,
950     Wolfman can go ahead and commit them as usual to create revision 10.
951   </p>
952
953   <p>
954     Wolfman's working copy is now in sync with the master,
955     but Dracula's is one behind at revision 9.
956     At this point,
957     they independently decide to add measurement units
958     to the columns in <code>moons.txt</code>.
959     Wolfman is quicker off the mark this time;
960     he adds a line to the file:
961   </p>
962
963 <pre src="svn/moons_wolfman_units.txt">
964 Name            Orbital Radius  Orbital Period  Mass            Radius
965 <span class="highlight">                (10**3 km)      (days)          (10**20 kg)     (km)</span>
966 Amalthea        181.4           0.498179        0.075           131 x 73 x 67
967 Io              421.6           1.769138        893.2           1821.6
968 Europa          670.9           3.551181        480.0           1560.8
969 Ganymede        1070.4          7.154553        1481.9          2631.2
970 Callisto        1882.7          16.689018       1075.9          2410.3
971 Himalia         11460           250.5662        0.095           85.0
972 Elara           11740           259.6528        0.008           40.0
973 </pre>
974
975   <p class="continue">
976     and commits it to create revision 11.
977     While he is doing this,
978     though,
979     Dracula inserts a different line at the top of the file:
980   </p>
981
982 <pre src="svn/moons_dracula_units.txt">
983 Name            Orbital Radius  Orbital Period  Mass            Radius
984 <span class="highlight">                * 10^3 km       * days          * 10^20 kg      * km</span>
985 Amalthea        181.4           0.498179        0.075           131 x 73 x 67
986 Io              421.6           1.769138        893.2           1821.6
987 Europa          670.9           3.551181        480.0           1560.8
988 Ganymede        1070.4          7.154553        1481.9          2631.2
989 Callisto        1882.7          16.689018       1075.9          2410.3
990 Himalia         11460           250.5662        0.095           85.0
991 Elara           11740           259.6528        0.008           40.0
992 </pre>
993
994   <p>
995     Once again,
996     when Dracula tries to commit,
997     Subversion tells him he can't.
998     But this time,
999     when Dracula does updates his working copy,
1000     he doesn't just get the line Wolfman added to create revision 11.
1001     There is an actual conflict in the file,
1002     so Subversion asks Dracula what he wants to do:
1003   </p>
1004
1005 <pre src="svn/moons_dracula_conflict.txt">
1006 $ <span class="in">svn update</span>
1007 <span class="out">Conflict discovered in 'jupiter/moons.txt'.
1008 Select: (p) postpone, (df) diff-full, (e) edit,
1009         (mc) mine-conflict, (tc) theirs-conflict,
1010         (s) show all options:</span>
1011 </pre>
1012
1013   <p>
1014     Dracula choose <code>p</code> for "postpone",
1015     which tells Subversion that he'll deal with the problem later.
1016     Once the update is finished,
1017     he opens <code>moons.txt</code> in his editor and sees:
1018   </p>
1019
1020 <pre>
1021  Name            Orbital Radius  Orbital Period  Mass
1022 +&lt;&lt;&lt;&lt;&lt;&lt;&lt; .mine
1023          +                * 10^3 km       * days         * 10^20 kg
1024 +=======
1025 +                (10**3 km)      (days)         (10**20 kg)
1026 +&gt;&gt;&gt;&gt;&gt;&gt;&gt; .r11
1027  Amalthea        181.4           0.498179        0.074
1028  Io              421.6           1.769138        893.2
1029  Europa          670.9           3.551181        480.0
1030  Ganymede        1070.4          7.154553        1481.9
1031  Callisto        1882.7          16.689018       1075.9
1032 </pre>
1033
1034   <p class="continue">
1035     As we can see,
1036     Subversion has inserted
1037     <a href="glossary.html#conflict-marker">conflict markers</a>
1038     in <code>moons.txt</code>
1039     wherever there is a conflict.
1040     The line <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt; .mine</code> shows the start of the conflict,
1041     and is followed by the lines from the local copy of the file.
1042     The separator <code>=======</code> is then
1043     followed by the lines from the repository's file that are in conflict with that section,
1044     while <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt; .r11</code> marks the end of the conflict.
1045   </p>
1046
1047   <p>
1048     Before he can commit,
1049     Dracula has to edit his copy of the file to get rid of those markers.
1050     He changes it to:
1051   </p>
1052
1053 <pre src="svn/moons_dracula_resolved.txt">
1054 Name            Orbital Radius  Orbital Period  Mass            Radius
1055 <span class="highlight">                (10^3 km)       (days)          (10^20 kg)      (km)</span>
1056 Amalthea        181.4           0.498179        0.075           131 x 73 x 67
1057 Io              421.6           1.769138        893.2           1821.6
1058 Europa          670.9           3.551181        480.0           1560.8
1059 Ganymede        1070.4          7.154553        1481.9          2631.2
1060 Callisto        1882.7          16.689018       1075.9          2410.3
1061 Himalia         11460           250.5662        0.095           85.0
1062 Elara           11740           259.6528        0.008           40.0
1063 </pre>
1064
1065   <p class="continue">
1066     then uses the <code>svn resolved</code> command to tell Subversion that
1067     he has fixed the problem.
1068     Subversion will now let him commit to create revision 12.
1069   </p>
1070
1071   <div class="box">
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 10</a>):
1114   </p>
1115
1116   <figure id="f:diff_viewer">
1117     <img src="svn/diff_viewer.png" alt="A Difference Viewer" />
1118     <figcaption>Figure 10: A Difference Viewer</figcaption>
1119   </figure>
1120
1121   <p class="continue">
1122     Dracula can use the buttons to merge changes from either of the edited versions
1123     into the common ancestor,
1124     or edit the central pane directly.
1125     Again,
1126     once he is done,
1127     he uses <code>svn resolved</code> and <code>svn commit</code>
1128     to create revision 12 of the repository.
1129   </p>
1130
1131   <p>
1132     In this case, the conflict was small and easy to fix.
1133     However, if two or more people on a team are repeatedly creating conflicts for one another,
1134     it's usually a signal of deeper communication problems:
1135     either they aren't talking as often as they should, or their responsibilities overlap.
1136     If used properly,
1137     the version control system can help the team find and fix these issues
1138     so that it will be more productive in future.
1139   </p>
1140
1141   <div class="box">
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">
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   <div class="challenges">
1167     <h3>Challenges</h3>
1168
1169     <p>
1170       If you are working in a group,
1171       partner with someone who has also wrote a biography for themselves
1172       for the previous section's challenges.
1173     </p>
1174
1175     <ol>
1176       <li>
1177         Both partners use <code>svn update</code>
1178         to make sure their working copies are up to date
1179         and that there are no local changes.
1180       </li>
1181       <li>
1182         The first partner edits her biography and commits the changes.
1183       </li>
1184       <li>
1185         The second partner edits her copy of the file
1186         (<em>without</em> having updated to get the first partner's changes),
1187         then tries to <code>svn commit</code>.
1188       </li>
1189       <li>
1190         Once the second partner has resolved the conflict,
1191         she commits her changes.
1192       </li>
1193       <li>
1194         Repeat these four steps with roles reversed.
1195       </li>
1196     </ol>
1197
1198     <p>
1199       If you are working on your own,
1200       you can simulate the steps above
1201       by checking out a second copy of the project into a new directory.
1202       (Remember,
1203       this cannot overlap any existing checked-out copies.)
1204       Edit your biography in one copy and commit those changes,
1205       then switch to the other copy and edit the same file
1206       before updating.
1207       <a href="#f:challenge_conflict">Figure 11</a> shows
1208       the differences between these two challenges.
1209     </p>
1210
1211     <figure id="f:challenge_conflict">
1212       <img src="svn/challenge_conflict.png" alt="Practicing Conflict Resolution" />
1213       <figcaption>Figure 11: Practicing Conflict Resolution</figcaption>
1214     </figure>
1215   </div>
1216
1217 </section>
1218
1219     <section id="s:rollback">
1220
1221       <h2>Recovering Old Versions</h2>
1222
1223       <div class="understand" id="u:rollback">
1224         <h3>Understand:</h3>
1225         <ul>
1226           <li>How to undo changes to a working copy.</li>
1227           <li>How to recover old versions of files.</li>
1228           <li>What a branch is.</li>
1229         </ul>
1230       </div>
1231
1232       <p>
1233         Now that we have seen how to merge files and resolve conflicts,
1234         we can look at how to use version control as an "infinite undo".
1235         Suppose that when Wolfman starts work late one night,
1236         his copy of <code>explore</code> is in sync with the head at revision 12.
1237         He decides to edit the file <code>moons.txt</code>;
1238         unfortunately, he forgot that there was a full moon,
1239         so his changes don't make a lot of sense:
1240       </p>
1241
1242 <pre src="svn/poetry.txt">
1243 Just one moon can make me growl
1244 Four would make me want to howl
1245 ...
1246 </pre>
1247
1248       <p>
1249         When he's back in human form the next day,
1250         he wants to undo his changes.
1251         Without version control, his choices would be grim:
1252         he could try to edit them back into their original state by hand
1253         (which for some reason hardly ever seems to work),
1254         or ask his colleagues to send him their copies of the files
1255         (which is almost as embarrassing as chasing the neighbor's cat when in wolf form).
1256       </p>
1257
1258       <p>
1259         Since he's using Subversion, though,
1260         and hasn't committed his work to the repository,
1261         all he has to do is <a href="glossary.html#revert">revert</a> his local changes.
1262         <code>svn revert</code> simply throws away local changes to files
1263         and puts things back the way they were before those changes were made.
1264         This is a purely local operation:
1265         since Subversion stores the history of the project inside every working copy,
1266         Wolfman doesn't need to be connected to the network to do this.
1267       </p>
1268
1269       <p>
1270         To start,
1271         Wolfman uses <code>svn diff</code> <em>without</em> the <code>-r HEAD</code> flag
1272         to take a look at the differences between his file
1273         and the master copy in the repository.
1274         Since he doesn't want to keep his changes,
1275         his next command is <code>svn revert moons.txt</code>.
1276       </p>
1277
1278 <pre>
1279 $ <span class="in">cd jupiter</span>
1280 $ <span class="in">svn revert moons.txt</span>
1281 <span class="out">Reverted   moons.txt</span>
1282 </pre>
1283
1284       <p>
1285         What if someone <em>has</em> committed their changes,
1286         but still wants to undo them?
1287         For example,
1288         suppose Dracula decides that the numbers in <code>moons.txt</code> would look better with commas.
1289         He edits the file to put them in:
1290       </p>
1291
1292 <pre src="svn/moons_commas.txt">
1293 Name            Orbital Radius  Orbital Period  Mass            Radius
1294                 (10^3 km)       (days)          (10^20 kg)      (km)
1295 Amalthea        181.4           0.498179          0.075      131 x 73 x 67
1296 Io              421.6           1.769138        893.2          1<span class="highlight">,</span>821.6
1297 Europa          670.9           3.551181        480.0          1<span class="highlight">,</span>560.8
1298 Ganymede      1<span class="highlight">,</span>070.4           7.154553      1<span class="highlight">,</span>481.9          2<span class="highlight">,</span>631.2
1299 Callisto      1<span class="highlight">,</span>882.7          16.689018      1<span class="highlight">,</span>075.9          2<span class="highlight">,</span>410.3
1300 Himalia      11<span class="highlight">,</span>460           250.5662            0.095           85.0
1301 Elara        11<span class="highlight">,</span>740           259.6528            0.008           40.0
1302 </pre>
1303
1304       <p class="continue">
1305         then commits his changes to create revision 13.
1306         A little while later,
1307         the Mummy sees the change and orders Dracula to put things back the way they were.
1308         What should Dracula do?
1309       </p>
1310
1311       <p>
1312         We can draw the sequence of events leading up to revision 13
1313         as shown in <a href="#f:before_undoing">Fixture XXX</a>:
1314       </p>
1315
1316       <figure id="f:before_undoing">
1317         <img src="svn/before_undoing.png" alt="Before Undoing" />
1318       </figure>
1319
1320       <p class="continue">
1321         Dracula wants to erase revision 13 from the repository,
1322         but he can't actually do that:
1323         once a change is in the repository,
1324         it's there forever.
1325         What he can do instead is merge the old revision with the current revision
1326         to create a new revision
1327         (<a href="#f:merging_history">Fixture XXX</a>).
1328       </p>
1329
1330       <figure id="f:merging_history">
1331         <img src="svn/merging_history.png" alt="Merging History" />
1332       </figure>
1333
1334       <p class="continue">
1335         This is exactly like merging changes made by two different people;
1336         the only difference is that the "other person" is his past self.
1337       </p>
1338
1339       <p>
1340         To undo his commas,
1341         Dracula must merge revision 12 (the one before his change)
1342         with revision 13 (the current head revision)
1343         using <code>svn merge</code>:
1344       </p>
1345
1346 <pre>
1347 $ <span class="in">svn merge -r HEAD:12 moons.txt</span>
1348 <span class="out">-- Reverse-merging r13 into 'moons.txt'
1349 U  moons.txt</span>
1350 </pre>
1351
1352       <p class="continue">
1353         The <code>-r</code> flag specifies the range of revisions to merge:
1354         to undo the changes from revision 12 to revision 13,
1355         he uses either <code>13:12</code> or <code>HEAD:12</code>
1356         (since he is going backward in time from the most recent revision to revision 12).
1357         This is called a <a href="glossary.html#reverse-merge">reverse</a> merge
1358         because he's going backward in time.
1359       </p>
1360
1361       <p>
1362         After he runs this command,
1363         he must run <code>svn commit</code> to save the changes to the repository.
1364         This creates a new revision, number 14,
1365         rather than erasing revision 13.
1366         That way,
1367         the changes he made to create revision 13 are still there
1368         if he can ever convince the Mummy that numbers should have commas.
1369       </p>
1370
1371       <p>
1372         Merging can be used to recover older revisions of files,
1373         not just the most recent,
1374         and to recover many files or directories at a time.
1375         The most frequent use, though,
1376         is to manage parallel streams of development in large projects.
1377         This is outside the scope of this chapter,
1378         but the basic idea is simple.
1379       </p>
1380
1381       <p>
1382         Suppose that Universal Monsters has just released a new program for designing secret lairs.
1383         Dracula and Wolfman are supposed to start adding a few features
1384         that had to be left out of the first release because time ran short.
1385         At the same time,
1386         Frankenstein and the Mummy are doing technical support:
1387         their job is to fix any bugs that users find.
1388         All sorts of things could go wrong if both teams tried to work on the same code at the same time.
1389         For example,
1390         if Frankenstein fixed a bug and sent a new copy of the program to a user in Greenland,
1391         it would be all too easy for him to accidentally include
1392         the half-completed shark tank control feature that Wolfman was working on.
1393       </p>
1394
1395       <p>
1396         The usual way to handle this situation is
1397         to create a <a href="glossary.html#branch">branch</a>
1398         in the repository for each major sub-project
1399         (<a href="#f:branch_merge">Figure XXX</a>).
1400         While Wolfman and Dracula work on
1401         the <a href="glossary.html#main-line">main line</a>,
1402         Frankenstein and the Mummy create a branch,
1403         which is just another copy of the repository's files and directories
1404         that is also under version control.
1405         They can work in their branch without disturbing Wolfman and Dracula and vice versa:
1406       </p>
1407
1408       <figure id="f:branch_merge">
1409         <img src="svn/branch_merge.png" alt="Branching and Merging" />
1410       </figure>
1411
1412       <p>
1413         Branches in version control repositories are often described as "parallel universes".
1414         Each branch starts off as a clone of the project at some moment in time
1415         (typically each time the software is released,
1416         or whenever work starts on a major new feature).
1417         Changes made to a branch only affect that branch,
1418         just as changes made to the files in one directory don't affect files in other directories.
1419         However,
1420         the branch and the main line are both stored in the same repository,
1421         so their revision numbers are always in step.
1422       </p>
1423
1424       <p>
1425         If someone decides that a bug fix in one branch should also be made in another,
1426         all they have to do is merge the files in question.
1427         This is exactly like merging an old version of a file with the current one,
1428         but instead of going backward in time,
1429         the change is brought sideways from one branch to another.
1430       </p>
1431
1432       <p>
1433         Branching helps projects scale up by letting sub-teams work independently,
1434         but too many branches can cause as many problems as they solve.
1435         Karl Fogel's excellent book
1436         <a href="bib.html#fogel-producing-oss"><cite>Producing Open Source Software</cite></a>,
1437         and Laura Wingerd and Christopher Seiwald's paper
1438         "<a href="bib.html#wingerd-seiwald-scm">High-level Best Practices in Software Configuration Management</a>",
1439         talk about branches in much more detail.
1440         Projects usually don't need to do this until they have a dozen or more developers,
1441         or until several versions of their software are in simultaneous use,
1442         but using branches is a key part of switching from software carpentry to software engineering.
1443       </p>
1444
1445       <div class="keypoints" id="k:rollback">
1446         <h3>Summary</h3>
1447         <ul>
1448           <li>Old versions of files can be recovered by merging their old state with their current state.</li>
1449           <li>Recovering an old version of a file does not erase the intervening changes.</li>
1450           <li>Use branches to support parallel independent development.</li>
1451           <li><code>svn merge</code> merges two revisions of a file.</li>
1452           <li><code>svn revert</code> undoes local changes to files.</li>
1453         </ul>
1454       </div>
1455
1456     </section>
1457
1458     <section id="s:setup">
1459
1460       <h2>Setting up a Repository</h2>
1461
1462       <div class="understand" id="u:setup">
1463         <h3>Understand:</h3>
1464         <ul>
1465           <li>How to create a repository.</li>
1466         </ul>
1467       </div>
1468
1469       <p>
1470         It is finally time to see how to create a repository.
1471         As a quick recap,
1472         we will keep the master copy of our work in a repository
1473         on a server that we can access from other machines on the internet.
1474         That master copy consists of files and directories that no-one ever edits directly.
1475         Instead, a copy of Subversion running on that machine
1476         manages updates for us and watches for conflicts.
1477         Our working copy is a mirror image of the master sitting on our computer.
1478         When our Subversion client needs to communicate with the master,
1479         it exchanges data with the copy of Subversion running on the server.
1480       </p>
1481
1482       <figure id="f:repo_four_things">
1483         <img src="svn/repo_four_things.png" alt="What's Needed for a Repository" />
1484       </figure>
1485
1486       <p>
1487         To make this to work, we need four things
1488         (<a href="#f:repo_four_things">Figure XXX</a>):
1489       </p>
1490
1491       <ol>
1492
1493         <li>
1494           The repository itself.
1495           It's not enough to create an empty directory and start filling it with files:
1496           Subversion needs to create a lot of other structure
1497           in order to keep track of old revisions, who made what changes, and so on.
1498         </li>
1499
1500         <li>
1501           The full URL of the repository.
1502           This includes the URL of the server
1503           and the path to the repository on that machine.
1504           (The second part is needed because a single server can,
1505           and usually will,
1506           host many repositories.)
1507         </li>
1508
1509         <li>
1510           Permission to read or write the master copy.
1511           Many open source projects give the whole world permission to read from their repository,
1512           but very few allow strangers to write to it:
1513           there are just too many possibilities for abuse.
1514           Somehow, we have to set up a password or something like it
1515           so that users can prove who they are.
1516         </li>
1517
1518         <li>
1519           A working copy of the repository on our computer.
1520           Once the first three things are in place,
1521           this just means running the <code>checkout</code> command.
1522         </li>
1523
1524       </ol>
1525
1526       <p>
1527         To keep things simple,
1528         we will start by creating a repository on the machine that we're working on.
1529         This won't let us share our work with other people,
1530         but it <em>will</em> allow us to save the history of our work as we go along.
1531       </p>
1532
1533       <p>
1534         The command to create a repository is <code>svnadmin create</code>,
1535         followed by the path to the repository.
1536         If we want to create a repository called <code>lair_repo</code>
1537         directly under our home directory,
1538         we just <code>cd</code> to get home
1539         and run <code>svnadmin create lair_repo</code>.
1540         This command creates a directory called <code>lair_repo</code> to hold our repository,
1541         and fills it with various files that Subversion uses
1542         to keep track of the project's history:
1543       </p>
1544
1545 <pre>
1546 $ <span class="in">cd</span>
1547 $ <span class="in">svnadmin create lair_repo</span>
1548 $ <span class="in">ls -F lair_repo</span>
1549 <span class="out">README.txt    conf/    db/    format    hooks/    locks/</span>
1550 </pre>
1551
1552       <p class="continue">
1553         We should <em>never</em> edit anything in this repository directly.
1554         Doing so probably won't shred our sanity and leave us gibbering in mindless horror,
1555         but it will almost certainly make the repository unusable.
1556       </p>
1557
1558       <p>
1559         To get a working copy of this repository,
1560         we use Subversion's <code>checkout</code> command.
1561         If our home directory is <code>/users/mummy</code>,
1562         then the full path to the repository we just created is <code>/users/mummy/lair_repo</code>,
1563         so we run <code>svn checkout file:///users/mummy/lair lair_working</code>.
1564       </p>
1565
1566       <p>
1567         Working backward,
1568         the second argument,
1569         <code>lair_working</code>,
1570         specifies where the working copy is to be put.
1571         The first argument is the URL of our repository,
1572         and it has two parts.
1573         <code>/users/mummy/lair_repo</code> is the path to repository directory.
1574         <code>file://</code> specifies the <a href="glossary.html#protocol">protocol</a>
1575         that Subversion will use to communicate with the repository&mdash;in this case,
1576         it says that the repository is part of the local machine's filesystem.
1577         Notice that the protocol ends in two slashes,
1578         while the absolute path to the repository starts with a slash,
1579         making three in total.
1580         A very common mistake is to type only two, since that's what web URLs normally have.
1581       </p>
1582
1583       <p>
1584         When we're doing a checkout,
1585         it is <em>very</em> important that we provide the second argument,
1586         which specifies the name of the directory we want the working copy to be put in.
1587         Without it,
1588         Subversion will try to use the name of the repository,
1589         <code>lair_repo</code>,
1590         as the name of the working copy.
1591         Since we're in the directory that contains the repository,
1592         this means that Subversion will try to overwrite the repository with a working copy.
1593         Again,
1594         there isn't much risk of our sanity being torn to shreds,
1595         but this could ruin our repository.
1596       </p>
1597
1598       <p>
1599         To avoid this problem,
1600         most people create a sub-directory in their account called something like <code>repos</code>,
1601         and then create their repositories in that.
1602         For example,
1603         we could create our repository in <code>/users/mummy/repos/lair</code>,
1604         then check out a working copy as <code>/users/mummy/lair</code>.
1605         This practice makes both names easier to read.
1606       </p>
1607
1608       <p>
1609         The obvious next steps are
1610         to put our repository on a server,
1611         rather than on our personal machine,
1612         and to give other people access to the repository we have just created
1613         so that they can work with us.
1614         We'll discuss the first in <a href="web.html#s:svn">a later chapter</a>,
1615         but unfortunately,
1616         the second really does require things that we are not going to cover in this course.
1617         If you want to do this, you can:
1618       </p>
1619
1620       <ul>
1621
1622         <li>
1623           ask your system administrator to set it up for you;
1624         </li>
1625
1626         <li>
1627           use an open source hosting service like <a href="http://www.sf.net">SourceForge</a>,
1628           <a href="http://code.google.com">Google Code</a>,
1629           <a href="https://github.com/">GitHub</a>,
1630           or <a href="https://bitbucket.org/">BitBucket</a>; or
1631         </li>
1632
1633         <li>
1634           spend a few dollars a month on a commercial hosting service like <a href="http://dreamhost.com">DreamHost</a>
1635           that provides web-based GUIs for creating and managing repositories.
1636         </li>
1637
1638       </ul>
1639
1640       <p>
1641         If you choose the second or third option,
1642         please check with whoever handles intellectual property at your institution
1643         to make sure that putting your work on a commercially-operated machine
1644         that is probably in some other legal jurisdiction
1645         isn't going to cause trouble.
1646         Many people assume that it's "just OK",
1647         while others act as if not having asked will be an acceptable defence later on.
1648         Unfortunately,
1649         neither is true&hellip;
1650       </p>
1651
1652       <div class="keypoints" id="k:setup">
1653         <h3>Summary</h3>
1654         <ul>
1655           <li>Repositories can be hosted locally, on local (departmental) servers, on hosting services, or on their owners' own domains.</li>
1656           <li><code>svnadmin create <em>name</em></code> creates a new repository.</li>
1657         </ul>
1658       </div>
1659
1660     </section>
1661
1662     <section id="s:provenance">
1663
1664       <h2>Provenance</h2>
1665
1666       <div class="understand" id="u:provenance">
1667         <h3>Understand:</h3>
1668         <ul>
1669           <li>What data provenance is.</li>
1670           <li>How to embed version numbers and other information in files managed by version control.</li>
1671           <li>How to record version information about a program in its output.</li>
1672         </ul>
1673       </div>
1674
1675       <p>
1676         In art,
1677         the <a href="glossary.html#provenance">provenance</a> of a work
1678         is the history of who owned it, when, and where.
1679         In science,
1680         it's the record of how a particular result came to be:
1681         what raw data was processed by what version of what program to create which intermediate files,
1682         what was used to turn those files into which figures of which papers,
1683         and so on.
1684       </p>
1685
1686       <p>
1687         One of the central ideas of this course is that
1688         wen can automatically track the provenance of scientific data.
1689         To start,
1690         suppose we have a text file <code>combustion.dat</code> in a Subversion repository.
1691         Run the following two commands:
1692       </p>
1693
1694 <pre>
1695 $ svn propset svn:keywords Revision combustion.dat
1696 $ svn commit -m "Turning on the 'Revision' keyword" combustion.dat
1697 </pre>
1698
1699       <p>
1700         Now open the file in an editor
1701         and add the following line somewhere near the top:
1702       </p>
1703
1704 <pre>
1705 # $Revision:$
1706 </pre>
1707
1708       <p>
1709         The '#' sign isn't important:
1710         it's just what <code>.dat</code> files use to show comments.
1711         The <code>$Revision:$</code> string,
1712         on the other hand,
1713         means something special to Subversion.
1714         Save the file, and commit the change:
1715       </p>
1716
1717 <pre>
1718 $ svn commit -m "Inserting the 'Revision' keyword" combustion.dat
1719 </pre>
1720
1721       <p>
1722         When we open the file again,
1723         we'll see that Subversion has changed that line to something like:
1724       </p>
1725
1726 <pre>
1727 # $Revision: 143$
1728 </pre>
1729
1730       <p class="continue">
1731         i.e., Subversion has inserted the version number
1732         after the colon and before the closing <code>$</code>.
1733       </p>
1734
1735       <p>
1736         Here's what just happened.
1737         First, Subversion allows you to set
1738         <a href="glossary.html#property-subversion">properties</a>
1739         for files and and directories.
1740         These properties aren't in the files or directories themselves,
1741         but live in Subversion's database.
1742         One of those properties,
1743         <code>svn:keywords</code>,
1744         tells Subversion to look in files that are being changed
1745         for strings of the form <code>$propertyname: &hellip;$</code>,
1746         where <code>propertyname</code> is a string like <code>Revision</code> or <code>Author</code>.
1747         (About half a dozen such strings are supported.)
1748       </p>
1749
1750       <p>
1751         If it sees such a string,
1752         Subversion rewrites it as the commit is taking place to replace <code>&hellip;</code>
1753         with the current version number,
1754         the name of the person making the change,
1755         or whatever else the property's name tells it to do.
1756         You only have to add the string to the file once;
1757         after that,
1758         Subversion updates it for you every time the file changes.
1759       </p>
1760
1761       <p>
1762         Putting the version number in the file this way can be pretty handy.
1763         If you copy the file to another machine,
1764         for example,
1765         it carries its version number with it,
1766         so you can tell which version you have even if it's outside version control.
1767         We'll see some more useful things we can do with this information in
1768         <a href="python.html">the next chapter</a>.
1769       </p>
1770
1771       <div class="box">
1772
1773         <h3>When <em>Not</em> to Use Version Control</h3>
1774
1775         <p>
1776           Despite the rapidly decreasing cost of storage,
1777           it is still possible to run out of disk space.
1778           In some labs,
1779           people can easy go through 2 TB/month if they're not careful.
1780           Since version control tools usually store revisions in terms of lines,
1781           with binary data files,
1782           they end up essentially storing every revision separately.
1783           This isn't that bad
1784           (it's what we'd be doing anyway),
1785           but it means version control isn't doing what it likes to do,
1786           and the repository can get very large very quickly.
1787           Another concern is that if very old data will no longer be used,
1788           it can be nice to archive or delete old data files.
1789           This is not possible if our data is version controlled:
1790           information can only be added to a repository,
1791           so it can only ever increase in size.
1792         </p>
1793
1794       </div>
1795
1796       <p>
1797         We can use this trick with shell scripts too,
1798         or with almost any other kind of program.
1799         Going back to Nelle Nemo's data processing from the previous chapter,
1800         for example,
1801         suppose she writes a shell script that uses <code>gooclean</code>
1802         to tidy up data files.
1803         Her first version looks like this:
1804       </p>
1805
1806 <pre>
1807 for filename in $*
1808 do
1809     gooclean -b 0 100 &lt; $filename &gt; cleaned-$filename
1810 done
1811 </pre>
1812
1813       <p class="continue">
1814         i.e., it runs <code>gooclean</code> with bounding values of 0 and 100
1815         for each specified file,
1816         putting the result in a temporary file with a well-defined name.
1817         Assuming that '#' is the comment character for those kinds of data files,
1818         she could instead write:
1819       </p>
1820
1821 <pre>
1822 for filename in $*
1823 do
1824     <span class="highlight">echo "gooclean $Revision: 901$ -b 0 100" &gt; $filename</span>
1825     gooclean -b 0 100 &lt; $filename <span class="highlight">&gt;&gt;</span> cleaned-$filename
1826 done
1827 </pre>
1828
1829       <p>
1830         The first change puts a line in the output file
1831         that describes how that file was created.
1832         The second change is to use <code>&gt;&gt;</code> instead of <code>&gt;</code>
1833         to redirect <code>gooclean</code>'s output to the file.
1834         <code>&gt;&gt;</code> means "append to":
1835         instead of overwriting whatever is in the file,
1836         it adds more content to it.
1837         This ensures that the first line of the file is the provenance record,
1838         with the actual output of <code>gooclean</code> after it.
1839       </p>
1840
1841       <div class="keypoints" id="k:provenance">
1842         <h3>Summary</h3>
1843         <ul>
1844           <li><code>$Keyword:$</code> in a file can be filled in with a property value each time the file is committed.</li>
1845           <li idea="paranoia">Put version numbers in programs' output to establish provenance for data.</li>
1846           <li><code>svn propset svn:keywords <em>property</em> <em>files</em></code> tells Subversion to start filling in property values.</li>
1847         </ul>
1848       </div>
1849
1850     </section>
1851
1852 <section id="s:summary">
1853   <h2>Summing Up</h2>
1854
1855   <p>
1856     Correlation does not imply causality,
1857     but there is a very strong correlation between
1858     using version control
1859     and doing good computational science.
1860     There's an equally strong correlation
1861     between <em>not</em> using it and either wasting effort or getting things wrong.
1862     Today (the middle of 2013),
1863     I will not review a paper if the software used in it
1864     is not under version control.
1865     The work it reports might be interesting,
1866     but without the kind of record-keeping that version control provides,
1867     there's no way to know exactly what its authors did.
1868     Just as importantly,
1869     if someone doesn't know enough about computing to use version control,
1870     the odds are good that they don't know enough
1871     to do the programming right either.
1872   </p>
1873
1874 </section>
1875 {% endblock content %}