SVN (by Professor Cappello for CS 50, Winter 2007)What is version control?Controlling changes to information is version control: A version control system enables collaborative editing and sharing of information.Why is it useful?For a teamIt allows a set of people to concurrently use and change the information in a set of related files.For a single personIt allows that person to start making a set of complex changes to his files without having to worry about whether or not he can restore the files to their prior state, if desired.What kinds of files is version control good for?
Must I use version control for my project?Yes. It enables multiple team members and programming pairs to work concurrently on the project. Use of a version control tool, such as Subversion, results in fewer errors and a freer style of project development.Versioning Models(Based on the material in the SVN book)
The Problem of File-SharingAll version control systems address the same problem: how does the system enable users to share information, but prevent them from destructively interfering with each other? It's all too easy for users to accidentally overwrite each other's changes in the repository. Consider the scenario shown in the problem to avoid (below). Suppose we have two co-workers, Harry and Sally. They each decide to edit the same repository file at the same time. If Harry saves his changes to the repository first, then it's possible that (a few moments later) Sally could accidentally overwrite them with her own new version of the file. While Harry's version of the file won't be lost forever (because the system remembers every change), any changes Harry made won't be present in Sally's newer version of the file, because she never saw Harry's changes to begin with. Harry's work is still effectively lost—or at least missing from the latest version of the file—and probably by accident. This is definitely a situation we want to avoid! Figure 2.2. The problem to avoid ![]() The Lock-Modify-Unlock SolutionMany version control systems use a lock-modify-unlock model to address this problem. In such a system, the repository allows only one person to change a file at a time. Harry must “lock” the file before he can change it. If Sally tries to lock the file, the repository will deny the request. All she can do is read the file, and wait for Harry to finish his changes and release his lock. After Harry unlocks the file, Sally can lock and edit it. Figure 2.3. The lock-modify-unlock solution ![]() The lock-modify-unlock model is restrictive, and often becomes a development bottleneck:
The Copy-Modify-Merge SolutionSubversion, CVS, and other version control systems use a copy-modify-merge model. In this model, each user's client contacts the project repository and creates a personal working copy—a local reflection of the repository's files and directories. Developers concurrently modify their private copies. The private copies are merged into a new version. The version control system assists with the merging, but ultimately a human being is responsible for making it happen correctly. Say that Harry and Sally each create working copies of the same project, copied from the repository. They concurrently make changes to the same file A within their copies. Sally saves her changes to the repository first. When Harry attempts to save his changes later, the repository informs him that his file A is out-of-date. In other words, that file A in the repository has somehow changed since he last copied it. Harry asks his client to merge any new changes from the repository into his working copy of file A. Chances are that Sally's changes don't overlap with his own; so once he has both sets of changes integrated, he saves his working copy back to the repository. Figure 2.4. The copy-modify-merge solution ![]() Figure 2.5. The copy-modify-merge solution (continued) ![]() What if Sally's changes overlap Harry's changes? This is called a conflict. When Harry asks his client to merge the latest repository changes into his working copy, his copy of file A is flagged as being in conflict: He can see both sets of conflicting changes, and manually choose between them. The software does not automatically resolve conflicts. This responsibility is delegated to humans. Once Harry has resolved the overlapping changes—perhaps after a discussion with Sally—he can save the merged file back to the repository. The copy-modify-merge model may sound chaotic, but in practice, it runs extremely smoothly. Developers work concurrently. When they work on the same files, it turns out that most of their concurrent changes don't overlap; conflicts are infrequent. The amount of time it takes to resolve conflicts generally is far less than the time lost by a locking system. No system can force users to communicate perfectly, and no system can detect semantic conflicts. So there's no point in being lulled into a false promise that a locking system will somehow prevent conflicts; in practice, locking seems to inhibit productivity more than anything else. What am I expected to know about version control for this course?
Subversions basic commandsCreating your work space:
The typical work cycle looks like this:
Update Your Working Copy$ svn update In this case, someone else checked in modifications to both foo.c and bar.c since the last time you updated, and Subversion has updated your working copy to include those changes. When the server sends changes to your working copy, a letter code is displayed next to each item to let you know what actions Subversion performed to bring your working copy up-to-date:
Make Changes to Your Working Copysvn statusM bar.c # the content in bar.c has local modifications svn diff$ svn diff svn revert$ svn revert README Subversion reverts the file to its pre-modified state. svn revert can undo any scheduled operation. $ svn status foo Resolve Conflicts (Merging Others' Changes)$ svn update The C stands for conflict. You have to manually choose between your changes to bar.c and those in the repository. 3 things typically occur to assist you in noticing and resolving that conflict:
For example, Sally changes the file sandwich.txt in the repository. Harry changes the file in his working copy and commits in. Sally updates her working copy before checking in and she gets a conflict: $ svn update Subversion will not allow you to commit the file sandwich.txt until the 3 temporary files are removed. $ svn commit --message "Add a few more things" You need to either:
Once you've resolved the conflict, you let Subversion know by running svn resolved. This removes the 3 temporary files. Subversion no longer considers the file to be in conflict. $ svn resolved sandwich.txt Merging Conflicts by HandYou and Sally, both edit the file sandwich.txt at the same time. Sally commits her changes. When you update your working copy, you get a conflict. Edit sandwich.txt to resolve the conflicts. $ cat sandwich.txt The <<, ==, and >> are conflict markers: They are not part of the actual data in conflict. Ensure that those are removed from the file before your next commit. The text between the first 2 sets of markers is composed of the changes you made in the conflicting area: <<<<<<< .mine The text between the 2nd and 3rd sets of conflict markers is the text from Sally's commit: ======= You talk to Sally, and do the right thing (discard Sally's edits): Top piece of bread Run svn resolved $ svn resolved sandwich.txt Copying a File Onto Your Working FileIf you get a conflict and decide that you want to throw out your changes, you can merely copy one of the temporary files created by Subversion over the file in your working copy: $ svn update Using svn revertIf you get a conflict and want to throw out your changes, revert: $ svn revert sandwich.txt When you revert a conflicted file, you don't have to run svn resolved. svn resolved requires an argument. Only run svn resolved when you're certain that you've fixed the conflict in your file—once the temporary files are removed, Subversion lets you commit the file even if it still contains conflict markers. Commit Your ChangesThe commit operation requires a log message (describing your change). Your log message is attached to the new revision. If your log message is brief, you may supply it on the command line using the --message (or -m) option: $ svn commit --message "Corrected number of cheese slices." If you are composing your log message in a separate file as you work, tell Subversion to get the message from the file: $ svn commit --file logmsg If you fail to specify either the --message or --file switch, then Subversion will automatically launch your favorite editor for composing a log message. If you get: $ svn commit --message "Add another rule" Run svn update, deal with any merges or conflicts that result, and re-commit. To learn morePlease look at the Subversion part of the course Resources page. |