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