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