21f82f33ef6f038eb45a7f9a3b0c6decc1c924d0
[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   <h2>Recovering Old Versions</h2>
1221
1222   <div class="understand">
1223     <h3>Learning Objectives</h3>
1224     <ul>
1225       <li>Discard changes made to a working copy.</li>
1226       <li>Recover an old version of a file.</li>
1227       <li>Explain what branches are and when they are used.</li>
1228     </ul>
1229   </div>
1230
1231   <p>
1232     Now that we have seen how to merge files and resolve conflicts,
1233     we can look at how to use version control as an "infinite undo".
1234     Suppose that when Wolfman starts work late one night,
1235     his copy of <code>explore</code> is in sync with the head at revision 12.
1236     He decides to edit the file <code>moons.txt</code>;
1237     unfortunately, he forgot that there was a full moon,
1238     so his changes don't make a lot of sense:
1239   </p>
1240
1241 <pre src="svn/poetry.txt">
1242 Just one moon can make me growl
1243 Four would make me want to howl
1244 ...
1245 </pre>
1246
1247   <p>
1248     When he's back in human form the next day,
1249     he wants to undo his changes.
1250     Without version control, his choices would be grim:
1251     he could try to edit them back into their original state by hand
1252     (which for some reason hardly ever seems to work),
1253     or ask his colleagues to send him their copies of the files
1254     (which is almost as embarrassing as chasing the neighbor's cat when in wolf form).
1255   </p>
1256
1257   <p>
1258     Since he's using Subversion, though,
1259     and hasn't committed his work to the repository,
1260     all he has to do is <a href="glossary.html#revert">revert</a> his local changes.
1261     <code>svn revert</code> simply throws away local changes to files
1262     and puts things back the way they were before those changes were made.
1263     This is a purely local operation:
1264     since Subversion stores the history of the project inside every working copy,
1265     Wolfman doesn't need to be connected to the network to do this.
1266   </p>
1267
1268   <p>
1269     To start,
1270     Wolfman uses <code>svn diff</code> <em>without</em> the <code>-r HEAD</code> flag
1271     to take a look at the differences between his file
1272     and the master copy in the repository.
1273     Since he doesn't want to keep his changes,
1274     his next command is <code>svn revert moons.txt</code>.
1275   </p>
1276
1277 <pre>
1278 $ <span class="in">cd jupiter</span>
1279 $ <span class="in">svn revert moons.txt</span>
1280 <span class="out">Reverted   moons.txt</span>
1281 </pre>
1282
1283   <p>
1284     What if someone <em>has</em> committed their changes,
1285     but still wants to undo them?
1286     For example,
1287     suppose Dracula decides that the numbers in <code>moons.txt</code> would look better with commas.
1288     He edits the file to put them in:
1289   </p>
1290
1291 <pre src="svn/moons_commas.txt">
1292 Name            Orbital Radius  Orbital Period  Mass            Radius
1293                 (10^3 km)       (days)          (10^20 kg)      (km)
1294 Amalthea        181.4           0.498179          0.075      131 x 73 x 67
1295 Io              421.6           1.769138        893.2          1<span class="highlight">,</span>821.6
1296 Europa          670.9           3.551181        480.0          1<span class="highlight">,</span>560.8
1297 Ganymede      1<span class="highlight">,</span>070.4           7.154553      1<span class="highlight">,</span>481.9          2<span class="highlight">,</span>631.2
1298 Callisto      1<span class="highlight">,</span>882.7          16.689018      1<span class="highlight">,</span>075.9          2<span class="highlight">,</span>410.3
1299 Himalia      11<span class="highlight">,</span>460           250.5662            0.095           85.0
1300 Elara        11<span class="highlight">,</span>740           259.6528            0.008           40.0
1301 </pre>
1302
1303   <p class="continue">
1304     then commits his changes to create revision 13.
1305     A little while later,
1306     the Mummy sees the change and orders Dracula to put things back the way they were.
1307     What should Dracula do?
1308   </p>
1309
1310   <p>
1311     We can draw the sequence of events leading up to revision 13
1312     as shown in <a href="#f:before_undoing">Fixture 12</a>:
1313   </p>
1314
1315   <figure id="f:before_undoing">
1316     <img src="svn/before_undoing.png" alt="Before Undoing" />
1317     <figcaption>Figure 12: Before Undoing</figcaption>
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 13</a>).
1328   </p>
1329
1330   <figure id="f:merging_history">
1331     <img src="svn/merging_history.png" alt="Merging History" />
1332     <figcaption>Figure 13: Merging History</figcaption>
1333   </figure>
1334
1335   <p class="continue">
1336     This is exactly like merging changes made by two different people;
1337     the only difference is that the "other person" is his past self.
1338   </p>
1339
1340   <p>
1341     To undo his commas,
1342     Dracula must merge revision 12 (the one before his change)
1343     with revision 13 (the current head revision)
1344     using <code>svn merge</code>:
1345   </p>
1346
1347 <pre>
1348 $ <span class="in">svn merge -r HEAD:12 moons.txt</span>
1349 <span class="out">-- Reverse-merging r13 into 'moons.txt'
1350 U  moons.txt</span>
1351 </pre>
1352
1353   <p class="continue">
1354     The <code>-r</code> flag specifies the range of revisions to merge:
1355     to undo the changes from revision 12 to revision 13,
1356     he uses either <code>13:12</code> or <code>HEAD:12</code>
1357     (since he is going backward in time from the most recent revision to revision 12).
1358     This is called a <a href="glossary.html#reverse-merge">reverse</a> merge
1359     because he's going backward in time.
1360   </p>
1361
1362   <p>
1363     After he runs this command,
1364     he must run <code>svn commit</code> to save the changes to the repository.
1365     This creates a new revision, number 14,
1366     rather than erasing revision 13.
1367     That way,
1368     the changes he made to create revision 13 are still there
1369     if he can ever convince the Mummy that numbers should have commas.
1370   </p>
1371
1372   <p>
1373     Merging can be used to recover older revisions of files,
1374     not just the most recent,
1375     and to recover many files or directories at a time.
1376     The most frequent use, though,
1377     is to manage parallel streams of development in large projects.
1378     This is outside the scope of this chapter,
1379     but the basic idea is simple.
1380   </p>
1381
1382   <p>
1383     Suppose that Universal Missions has just released a new program
1384     for designing interplanetary voyages.
1385     Dracula and Wolfman are supposed to add some features
1386     that were left out of the first release because time ran short.
1387     At the same time,
1388     Frankenstein and the Mummy are doing technical support:
1389     their job is to fix any bugs that users find.
1390   </p>
1391
1392   <p>
1393     All sorts of things could go wrong
1394     if both teams tried to work on the same code at the same time.
1395     In particular,
1396     Dracula and Wolfman might want to make large changes
1397     to the structure of the code
1398     in order to make it easier to add new features,
1399     while Frankenstein and the Mummy want to make as few changes as possible
1400     so as not to introduce new bugs while fixing old ones.
1401   </p>
1402
1403   <p>
1404     The usual way to handle this situation is
1405     to create a <a href="glossary.html#branch">branch</a>
1406     in the repository for each major sub-project
1407     (<a href="#f:branch_merge">Figure 14</a>).
1408     While Wolfman and Dracula work on
1409     the <a href="glossary.html#main-line">main line</a>,
1410     Frankenstein and the Mummy create a branch,
1411     which is just another copy of the repository's files and directories
1412     that is also under version control.
1413     They can work in their branch without disturbing Wolfman and Dracula and vice versa:
1414   </p>
1415
1416   <figure id="f:branch_merge">
1417     <img src="svn/branch_merge.png" alt="Branching and Merging" />
1418     <figcaption>Figure 14: Branching and Merging</figcaption>
1419   </figure>
1420
1421   <p>
1422     Branches in version control repositories are often described as "parallel universes".
1423     Each branch starts off as a clone of the project at some moment in time
1424     (typically each time the software is released,
1425     or whenever work starts on a major new feature).
1426     Changes made to a branch only affect that branch,
1427     just as changes made to the files in one directory don't affect files in other directories.
1428     However,
1429     the branch and the main line are both stored in the same repository,
1430     so their revision numbers are always in step.
1431   </p>
1432
1433   <p>
1434     If someone decides that a bug fix in one branch should also be made in another,
1435     all they have to do is merge the files in question.
1436     This is exactly like merging an old version of a file with the current one,
1437     but instead of going backward in time,
1438     the change is brought sideways from one branch to another.
1439   </p>
1440
1441   <p>
1442     Branching helps projects scale up by letting sub-teams work independently,
1443     but too many branches can cause as many problems as they solve.
1444     Karl Fogel's excellent book
1445     <a href="bib.html#fogel-producing-oss"><cite>Producing Open Source Software</cite></a>,
1446     and Laura Wingerd and Christopher Seiwald's paper
1447     "<a href="bib.html#wingerd-seiwald-scm">High-level Best Practices in Software Configuration Management</a>",
1448     talk about branches in much more detail.
1449     Projects usually don't need to do this until they have a dozen or more developers,
1450     or until several versions of their software are in simultaneous use,
1451     but using branches is a key part of switching from software carpentry to software engineering.
1452   </p>
1453
1454   <div class="keypoints">
1455     <h3>Summary</h3>
1456     <ul>
1457       <li>Old versions of files can be recovered by merging their old state with their current state.</li>
1458       <li>Recovering an old version of a file does not erase the intervening changes.</li>
1459       <li>Use branches to support parallel independent development.</li>
1460       <li><code>svn merge</code> merges two revisions of a file.</li>
1461       <li><code>svn revert</code> undoes local changes to files.</li>
1462     </ul>
1463   </div>
1464
1465   <div class="challenges">
1466     <h3>Challenges</h3>
1467
1468     <p class="fixme">write some</p>
1469   </div>
1470
1471 </section>
1472
1473     <section id="s:setup">
1474
1475       <h2>Setting up a Repository</h2>
1476
1477       <div class="understand" id="u:setup">
1478         <h3>Understand:</h3>
1479         <ul>
1480           <li>How to create a repository.</li>
1481         </ul>
1482       </div>
1483
1484       <p>
1485         It is finally time to see how to create a repository.
1486         As a quick recap,
1487         we will keep the master copy of our work in a repository
1488         on a server that we can access from other machines on the internet.
1489         That master copy consists of files and directories that no-one ever edits directly.
1490         Instead, a copy of Subversion running on that machine
1491         manages updates for us and watches for conflicts.
1492         Our working copy is a mirror image of the master sitting on our computer.
1493         When our Subversion client needs to communicate with the master,
1494         it exchanges data with the copy of Subversion running on the server.
1495       </p>
1496
1497       <figure id="f:repo_four_things">
1498         <img src="svn/repo_four_things.png" alt="What's Needed for a Repository" />
1499       </figure>
1500
1501       <p>
1502         To make this to work, we need four things
1503         (<a href="#f:repo_four_things">Figure XXX</a>):
1504       </p>
1505
1506       <ol>
1507
1508         <li>
1509           The repository itself.
1510           It's not enough to create an empty directory and start filling it with files:
1511           Subversion needs to create a lot of other structure
1512           in order to keep track of old revisions, who made what changes, and so on.
1513         </li>
1514
1515         <li>
1516           The full URL of the repository.
1517           This includes the URL of the server
1518           and the path to the repository on that machine.
1519           (The second part is needed because a single server can,
1520           and usually will,
1521           host many repositories.)
1522         </li>
1523
1524         <li>
1525           Permission to read or write the master copy.
1526           Many open source projects give the whole world permission to read from their repository,
1527           but very few allow strangers to write to it:
1528           there are just too many possibilities for abuse.
1529           Somehow, we have to set up a password or something like it
1530           so that users can prove who they are.
1531         </li>
1532
1533         <li>
1534           A working copy of the repository on our computer.
1535           Once the first three things are in place,
1536           this just means running the <code>checkout</code> command.
1537         </li>
1538
1539       </ol>
1540
1541       <p>
1542         To keep things simple,
1543         we will start by creating a repository on the machine that we're working on.
1544         This won't let us share our work with other people,
1545         but it <em>will</em> allow us to save the history of our work as we go along.
1546       </p>
1547
1548       <p>
1549         The command to create a repository is <code>svnadmin create</code>,
1550         followed by the path to the repository.
1551         If we want to create a repository called <code>lair_repo</code>
1552         directly under our home directory,
1553         we just <code>cd</code> to get home
1554         and run <code>svnadmin create lair_repo</code>.
1555         This command creates a directory called <code>lair_repo</code> to hold our repository,
1556         and fills it with various files that Subversion uses
1557         to keep track of the project's history:
1558       </p>
1559
1560 <pre>
1561 $ <span class="in">cd</span>
1562 $ <span class="in">svnadmin create lair_repo</span>
1563 $ <span class="in">ls -F lair_repo</span>
1564 <span class="out">README.txt    conf/    db/    format    hooks/    locks/</span>
1565 </pre>
1566
1567       <p class="continue">
1568         We should <em>never</em> edit anything in this repository directly.
1569         Doing so probably won't shred our sanity and leave us gibbering in mindless horror,
1570         but it will almost certainly make the repository unusable.
1571       </p>
1572
1573       <p>
1574         To get a working copy of this repository,
1575         we use Subversion's <code>checkout</code> command.
1576         If our home directory is <code>/users/mummy</code>,
1577         then the full path to the repository we just created is <code>/users/mummy/lair_repo</code>,
1578         so we run <code>svn checkout file:///users/mummy/lair lair_working</code>.
1579       </p>
1580
1581       <p>
1582         Working backward,
1583         the second argument,
1584         <code>lair_working</code>,
1585         specifies where the working copy is to be put.
1586         The first argument is the URL of our repository,
1587         and it has two parts.
1588         <code>/users/mummy/lair_repo</code> is the path to repository directory.
1589         <code>file://</code> specifies the <a href="glossary.html#protocol">protocol</a>
1590         that Subversion will use to communicate with the repository&mdash;in this case,
1591         it says that the repository is part of the local machine's filesystem.
1592         Notice that the protocol ends in two slashes,
1593         while the absolute path to the repository starts with a slash,
1594         making three in total.
1595         A very common mistake is to type only two, since that's what web URLs normally have.
1596       </p>
1597
1598       <p>
1599         When we're doing a checkout,
1600         it is <em>very</em> important that we provide the second argument,
1601         which specifies the name of the directory we want the working copy to be put in.
1602         Without it,
1603         Subversion will try to use the name of the repository,
1604         <code>lair_repo</code>,
1605         as the name of the working copy.
1606         Since we're in the directory that contains the repository,
1607         this means that Subversion will try to overwrite the repository with a working copy.
1608         Again,
1609         there isn't much risk of our sanity being torn to shreds,
1610         but this could ruin our repository.
1611       </p>
1612
1613       <p>
1614         To avoid this problem,
1615         most people create a sub-directory in their account called something like <code>repos</code>,
1616         and then create their repositories in that.
1617         For example,
1618         we could create our repository in <code>/users/mummy/repos/lair</code>,
1619         then check out a working copy as <code>/users/mummy/lair</code>.
1620         This practice makes both names easier to read.
1621       </p>
1622
1623       <p>
1624         The obvious next steps are
1625         to put our repository on a server,
1626         rather than on our personal machine,
1627         and to give other people access to the repository we have just created
1628         so that they can work with us.
1629         We'll discuss the first in <a href="web.html#s:svn">a later chapter</a>,
1630         but unfortunately,
1631         the second really does require things that we are not going to cover in this course.
1632         If you want to do this, you can:
1633       </p>
1634
1635       <ul>
1636
1637         <li>
1638           ask your system administrator to set it up for you;
1639         </li>
1640
1641         <li>
1642           use an open source hosting service like <a href="http://www.sf.net">SourceForge</a>,
1643           <a href="http://code.google.com">Google Code</a>,
1644           <a href="https://github.com/">GitHub</a>,
1645           or <a href="https://bitbucket.org/">BitBucket</a>; or
1646         </li>
1647
1648         <li>
1649           spend a few dollars a month on a commercial hosting service like <a href="http://dreamhost.com">DreamHost</a>
1650           that provides web-based GUIs for creating and managing repositories.
1651         </li>
1652
1653       </ul>
1654
1655       <p>
1656         If you choose the second or third option,
1657         please check with whoever handles intellectual property at your institution
1658         to make sure that putting your work on a commercially-operated machine
1659         that is probably in some other legal jurisdiction
1660         isn't going to cause trouble.
1661         Many people assume that it's "just OK",
1662         while others act as if not having asked will be an acceptable defence later on.
1663         Unfortunately,
1664         neither is true&hellip;
1665       </p>
1666
1667       <div class="keypoints" id="k:setup">
1668         <h3>Summary</h3>
1669         <ul>
1670           <li>Repositories can be hosted locally, on local (departmental) servers, on hosting services, or on their owners' own domains.</li>
1671           <li><code>svnadmin create <em>name</em></code> creates a new repository.</li>
1672         </ul>
1673       </div>
1674
1675     </section>
1676
1677     <section id="s:provenance">
1678
1679       <h2>Provenance</h2>
1680
1681       <div class="understand" id="u:provenance">
1682         <h3>Understand:</h3>
1683         <ul>
1684           <li>What data provenance is.</li>
1685           <li>How to embed version numbers and other information in files managed by version control.</li>
1686           <li>How to record version information about a program in its output.</li>
1687         </ul>
1688       </div>
1689
1690       <p>
1691         In art,
1692         the <a href="glossary.html#provenance">provenance</a> of a work
1693         is the history of who owned it, when, and where.
1694         In science,
1695         it's the record of how a particular result came to be:
1696         what raw data was processed by what version of what program to create which intermediate files,
1697         what was used to turn those files into which figures of which papers,
1698         and so on.
1699       </p>
1700
1701       <p>
1702         One of the central ideas of this course is that
1703         wen can automatically track the provenance of scientific data.
1704         To start,
1705         suppose we have a text file <code>combustion.dat</code> in a Subversion repository.
1706         Run the following two commands:
1707       </p>
1708
1709 <pre>
1710 $ svn propset svn:keywords Revision combustion.dat
1711 $ svn commit -m "Turning on the 'Revision' keyword" combustion.dat
1712 </pre>
1713
1714       <p>
1715         Now open the file in an editor
1716         and add the following line somewhere near the top:
1717       </p>
1718
1719 <pre>
1720 # $Revision:$
1721 </pre>
1722
1723       <p>
1724         The '#' sign isn't important:
1725         it's just what <code>.dat</code> files use to show comments.
1726         The <code>$Revision:$</code> string,
1727         on the other hand,
1728         means something special to Subversion.
1729         Save the file, and commit the change:
1730       </p>
1731
1732 <pre>
1733 $ svn commit -m "Inserting the 'Revision' keyword" combustion.dat
1734 </pre>
1735
1736       <p>
1737         When we open the file again,
1738         we'll see that Subversion has changed that line to something like:
1739       </p>
1740
1741 <pre>
1742 # $Revision: 143$
1743 </pre>
1744
1745       <p class="continue">
1746         i.e., Subversion has inserted the version number
1747         after the colon and before the closing <code>$</code>.
1748       </p>
1749
1750       <p>
1751         Here's what just happened.
1752         First, Subversion allows you to set
1753         <a href="glossary.html#property-subversion">properties</a>
1754         for files and and directories.
1755         These properties aren't in the files or directories themselves,
1756         but live in Subversion's database.
1757         One of those properties,
1758         <code>svn:keywords</code>,
1759         tells Subversion to look in files that are being changed
1760         for strings of the form <code>$propertyname: &hellip;$</code>,
1761         where <code>propertyname</code> is a string like <code>Revision</code> or <code>Author</code>.
1762         (About half a dozen such strings are supported.)
1763       </p>
1764
1765       <p>
1766         If it sees such a string,
1767         Subversion rewrites it as the commit is taking place to replace <code>&hellip;</code>
1768         with the current version number,
1769         the name of the person making the change,
1770         or whatever else the property's name tells it to do.
1771         You only have to add the string to the file once;
1772         after that,
1773         Subversion updates it for you every time the file changes.
1774       </p>
1775
1776       <p>
1777         Putting the version number in the file this way can be pretty handy.
1778         If you copy the file to another machine,
1779         for example,
1780         it carries its version number with it,
1781         so you can tell which version you have even if it's outside version control.
1782         We'll see some more useful things we can do with this information in
1783         <a href="python.html">the next chapter</a>.
1784       </p>
1785
1786       <div class="box">
1787
1788         <h3>When <em>Not</em> to Use Version Control</h3>
1789
1790         <p>
1791           Despite the rapidly decreasing cost of storage,
1792           it is still possible to run out of disk space.
1793           In some labs,
1794           people can easy go through 2 TB/month if they're not careful.
1795           Since version control tools usually store revisions in terms of lines,
1796           with binary data files,
1797           they end up essentially storing every revision separately.
1798           This isn't that bad
1799           (it's what we'd be doing anyway),
1800           but it means version control isn't doing what it likes to do,
1801           and the repository can get very large very quickly.
1802           Another concern is that if very old data will no longer be used,
1803           it can be nice to archive or delete old data files.
1804           This is not possible if our data is version controlled:
1805           information can only be added to a repository,
1806           so it can only ever increase in size.
1807         </p>
1808
1809       </div>
1810
1811       <p>
1812         We can use this trick with shell scripts too,
1813         or with almost any other kind of program.
1814         Going back to Nelle Nemo's data processing from the previous chapter,
1815         for example,
1816         suppose she writes a shell script that uses <code>gooclean</code>
1817         to tidy up data files.
1818         Her first version looks like this:
1819       </p>
1820
1821 <pre>
1822 for filename in $*
1823 do
1824     gooclean -b 0 100 &lt; $filename &gt; cleaned-$filename
1825 done
1826 </pre>
1827
1828       <p class="continue">
1829         i.e., it runs <code>gooclean</code> with bounding values of 0 and 100
1830         for each specified file,
1831         putting the result in a temporary file with a well-defined name.
1832         Assuming that '#' is the comment character for those kinds of data files,
1833         she could instead write:
1834       </p>
1835
1836 <pre>
1837 for filename in $*
1838 do
1839     <span class="highlight">echo "gooclean $Revision: 901$ -b 0 100" &gt; $filename</span>
1840     gooclean -b 0 100 &lt; $filename <span class="highlight">&gt;&gt;</span> cleaned-$filename
1841 done
1842 </pre>
1843
1844       <p>
1845         The first change puts a line in the output file
1846         that describes how that file was created.
1847         The second change is to use <code>&gt;&gt;</code> instead of <code>&gt;</code>
1848         to redirect <code>gooclean</code>'s output to the file.
1849         <code>&gt;&gt;</code> means "append to":
1850         instead of overwriting whatever is in the file,
1851         it adds more content to it.
1852         This ensures that the first line of the file is the provenance record,
1853         with the actual output of <code>gooclean</code> after it.
1854       </p>
1855
1856       <div class="keypoints" id="k:provenance">
1857         <h3>Summary</h3>
1858         <ul>
1859           <li><code>$Keyword:$</code> in a file can be filled in with a property value each time the file is committed.</li>
1860           <li idea="paranoia">Put version numbers in programs' output to establish provenance for data.</li>
1861           <li><code>svn propset svn:keywords <em>property</em> <em>files</em></code> tells Subversion to start filling in property values.</li>
1862         </ul>
1863       </div>
1864
1865     </section>
1866
1867 <section id="s:summary">
1868   <h2>Summing Up</h2>
1869
1870   <p>
1871     Correlation does not imply causality,
1872     but there is a very strong correlation between
1873     using version control
1874     and doing good computational science.
1875     There's an equally strong correlation
1876     between <em>not</em> using it and either wasting effort or getting things wrong.
1877     Today (the middle of 2013),
1878     I will not review a paper if the software used in it
1879     is not under version control.
1880     The work it reports might be interesting,
1881     but without the kind of record-keeping that version control provides,
1882     there's no way to know exactly what its authors did.
1883     Just as importantly,
1884     if someone doesn't know enough about computing to use version control,
1885     the odds are good that they don't know enough
1886     to do the programming right either.
1887   </p>
1888
1889 </section>
1890 {% endblock content %}