Introduction to Subversion

Presented by David Brownlee

BELUG March 13th, 2007

Software Configuration Management uses tools and procedures to reproduce software in a consistent state at different points in time. A key tool for doing this is a version control system. Version control systems can also be used outside of software development. You might want to use a version control system to track changes in system configuration files or an experimental chili recipe. Subversion is a version control system. It allows you to track changes to and keep multiple versions of files and directories. In Software Configuration Management (SCM) terminology, a collection of one or more files and directories at a point in time is a configuration. You save your configurations in a central repository which is like a database. When you want to make a change to a configuration, you must check it out of the repository. Checking out a configuration creates a copy of the configuration's files and directories in your workspace. The workspace is a directory where you make all your changes. When you're finished making changes, you check the new files back into the repository. Subversion allows multiple people to work on the same files either locally or over a TCP/IP network. This is a (very) brief introduction to installing and using Subversion and I only cover a single user accessing a local (on the same machine) repository. The Subversion web site is http://subversion.tigris.org/. The documentation can be found at http://svnbook.red-bean.com/.

Installing Subversion

Install Subversion as you would any other package on your Linux distribution. Here is an example using yum on CentOS Linux:

$ sudo yum install subversion

[ ...snip... ]

Installed: subversion.i386 0:1.1.4-2.ent
Dependency Installed: apr.i386 0:0.9.4-24.5.c4.2 apr-util.i386 0:0.9.4-21 guile.i386 5:1.6.4-14 neon.i386 0:0.24.7-4 swig.i386 0:1.3.21-6 umb-scheme.i386 0:3.2-36.EL4
Complete!

Configuring Subversion

First, you need to configure the repository to store your file revisions. In this case, I'm putting the repository in my home directory. If you are setting up a repository for multiple users, you might want to put the repository in /var/svn or a similar location. You will also want to create an svn group and set filesystem permissions on the repository to control access. I'm also specifying that the fsfs (as opposed to the bdb) backend storage be used. You can read about the the differences in the Subversion documentation.

$ svnadmin create –fs-type fsfs /home/davidb/svn

This creates the repository in /home/davidb/svn. You will NOT edit the files in the repository directly.

All configurations in the repository share the same revision number (but not every file is changed every revision). You may want to have multiple repositories or organize all you projects in a single repository. Again, reading the Subversion documentation will help you plan for a larger installation than presented here.

Importing A Project Into The Repository

Version control systems are commonly used to store software projects. However, you can use Subversion for managing changes to other text files such as host configuration files or chili recipes. In our example, we will track changes to our Linux firewall configuration. First, make sure your EDITOR environment variable is set to your editor of choice:

$ export EDITOR=vim

Then, we make a temporary directory in our home directory and collect the files:

$ mkdir -p /home/davidb/tmp/trunk/etc/sysconfig
$ mkdir /home/davidb/tmp/tags
$ mkdir /home/davidb/tmp/branches
$ cd /home/davidb/tmp/trunk/etc/sysconfig
$ vi iptables

[ edit your file ]

$ cd ../../..

The trunk, tags, and branches directories are a convention explaned in the Subversion documentNow, import your project into the repository using the file URL notation:

$ svn import -m "Initial Import" file:///home/davidb/svn/firewall
Adding etc
Adding etc/sysconfig
Adding etc/sysconfig/iptables
Committed revision 1.

Do NOT delete your tmp directory yet! You must first verify that you can check out your work.

Create Your Workspace

Now you need to create your workspace, the directory where you will edit files:

$ mkdir /home/davidb/workspace
$ svn checkout file:///home/davidb/svn/firewall
A firewall/etc
A firewall/etc/sysconfig
A firewall/etc/sysconfig/iptables
Checked out revision 1.

Since the checkout worked, you can delete the temporary directory:

$ cd /home/davidb
$ rm -rf tmp

Editing Files

You can edit the files in your workspace normally. After you are satisfied with your changes, check your changes back into the repository:

$ cd /home/davidb/workspace/firewall/etc/sysconfig
$ vi iptables

[ make changes ]

svn commit iptables
Sending iptables
Transmitting file data .
Committed revision 2.

When you commit a change, the editor is started so you can add a note about what you have changed:

Another change
--This line, and those below, will be ignored--

M iptables
~
~
~

Adding New Files

To add a file, create or copy the file in your workspace, mark the file for addition to the repository, and then commit the file to the repository:

$ cd /home/davidb/workspace/firewall/etc
$ vi sysctl.conf
$ svn add sysctl.conf
A sysctl.conf
$ svn commit sysctl.conf
Adding sysctl.conf
Transmitting file data .
Committed revision 4.

Again, the editor will start so you can leave a note about what you did.

Showing History

To see how the files in your repository have changed, you can list their history:

$ svn log iptables
------------------------------------------------------------------------
r3 | davidb | 2007-02-06 18:20:57 -0800 (Tue, 06 Feb 2007) | 2 lines


Another change

------------------------------------------------------------------------
r2 | davidb | 2007-02-06 17:24:26 -0800 (Tue, 06 Feb 2007) | 2 lines


Added a line to allow inbound DNS queries

------------------------------------------------------------------------
r1 | davidb | 2007-02-06 17:16:57 -0800 (Tue, 06 Feb 2007) | 1 line


Initial Import
------------------------------------------------------------------------

Check The State Of Your Workspace

So you had fun playing with Subversion a couple weeks ago before life interrupted. Now you have some free time again but cannot remember which files in your workspace have been changed or still need to be added to the repository. Use svn status

$ svn status
? named
M iptables

The 'M' shows that the iptables file has been modified since you last checked it out. The '?' shows that Subversion doesn't know anything about the named file in your workspace. Either delete named or use svn add to add it to the repository.

See What Has Changed

So you made edits to the iptables file but cannot remember what you did. Use svn diff to see the differences between what is in your workspace and what is in the repository.

$ svn diff iptables
Index: iptables
===================================================================
--- iptables (revision 5)
+++ iptables (working copy)

[ diff output ]

Recover Your Repository

Your backups are not finished until you have proved you can recover from them. Backup your repository using the svnadmin tool:

$ svnadmin dump /home/davidb/svn > svn-backup-r17

Using svnadmin dump guarantees your backups are in a consistent state even if other users are checking files into the repository while your backing it up. The dump command also has –incremental and –revision options for making incremental backups.

Now create a new, empty repository and restore the repository using the dumpfile you created in the step above and the svnadmin load command:

$ svnadmin create –fs-type fsfs /home/davidb/svn-recovery-test
$ svnadmin load /home/davidb/svn-recovery-test < svn-backup-r17

End Note

This barely scratches the surface of what Subversion can do. In particular, you will want to read about update and diff since you will use them often. Read the documentation and experiment. Also, you can ask Subversion for help on the command line:

$ svn help
usage: svn <subcommand> [options] [args]
Subversion command-line client, version 1.4.0.
Type 'svn help <subcommand>' for help on a specific subcommand.
Type 'svn --version' to see the program version and RA modules
or 'svn --version --quiet' to see just the version number.


Most subcommands take file and/or directory arguments, recursing
on the directories. If no arguments are supplied to such a
command, it recurses on the current directory (inclusive) by default.


Available subcommands:
add
blame (praise, annotate, ann)
cat
checkout (co)
cleanup
commit (ci)
copy (cp)
delete (del, remove, rm)
diff (di)
export
help (?, h)
import
info
list (ls)
lock
log
merge

[ and more... ]