Tutorials

SVN HowTo

How to use Version Control with Subversion (SVN)

Subversion is a free software that allows to manage files and directories on a server. Multiple users can work collaboratively with different versions of the files on the server.

First, start reading the SVN Book.

SVN Installation

To work with SVN, please install it depending on your operating system:

  • Ubuntu: sudo apt-get install subversion
  • OpenSuse: sudo zypper install subversion
  • Mac: The SVN command line tools are contained in the XCode installer.
  • Windows: Use TortoiseSVN!

SVN Usage

You can develop code and maintain it in the SVN repository in the following way:

Browse the repository online:

Check out a repo from the server to a local directory (via the so called check-out-line):

svn co url/repo directory

For example:

svn co http://svn.code.sf.net/p/libsquish/code/trunk checkout-dir

This is called an anonymous checkout, because local changes of the repository cannot be submitted to the server.

Change your working directory to the checkout directory:

cd checkout-dir

Examine the files in the directory:

ls -l

If the directory contains an open-source software, we can build the software, for example with the CMake build system:

cmake . && make

Or the Automake build system

./configure && make

This is what you basically can do with an anonymous checkout.

If you do not already have write access to the repo, approach the admin of the SVN server and ask for permission to do so or create your own repository. I recommend sourceforge.net for that purpose.

If you have permissions to modify and check out the repository with a username and password, we can do much more with it:

First we check out a local copy with:

svn co --username=username --password=password url/repo directory

Now we can modify the repository, for example by creating a file with a plain text editor and adding it to version control:

emacs filename.txt &
svn add filename.txt

This results in a modified local copy of the repository, but the repository on the server has not changed yet!

To see what actually has changed locally, we tell svn to show a status of the actual directory:

svn stat -v

Now, the local changes of repository are uploaded by committing the local changes to the server:

svn commit -m “changes”

This creates a new revision of your code on the server. Versions are numbered sequentially, starting with revision number 1.

To see the status both locally and on the server, we tell svn to show a status again:

svn stat -v

We may also modify the repository by creating a sub-directory and adding it to version control and committing the changes again:

mkdir subdir
svn add subdir

For each commit you need to provide a comment, the so called commit log or commit message. The best way to specify this message is to type it on the command line after the -m option:

svn commit -m “commit message”

Each new commit will result in a new version with increasing revision number. This time it’s revision number 2.

Now we modify “filename.txt” with a plain text editor (e.g. emacs) again and show the textual changes with:

emacs filename.txt &
svn diff

The changes shown by the above command are the differences of your local copy against the latest version on the server.

If you are done with your changes, commit those to the server:

svn commit -m “another commit message”

Side note: svn stores your password so that you do not need to retype the password arguments every time you commit.

Check and see the corresponding commit log:

svn log .

To update your local copy with changes others may have committed to the repository:

svn update

If you are working on a project alone, you do not need to do the latter. But to be able to track the evolution of your code in a reasonable way, you should adhere to the following rules:

When working with SVN, there are four main rules of thumb:

  • Commit your changes at least once a day!
  • Do not commit code that doesn’t compile!
  • If it doesn’t compile, fix it!
  • Before actually committing it, review your code with “svn diff” and fix what you have forgotten to fix!

That’s it. You are using (sub)version control!


SVN Usage example

To check out the existing repository hello, which contains the “hello, world!” C example, type those commands in the unix shell:

# check out repo
svn co svn://schorsch.efi.fh-nuernberg.de/hello hello

# change working directory to repo
cd hello

# show commit log of committed revisions
svn log .

# compile and run hello-world example
gcc hello.c -o hello && ./hello

# modify contents
echo “/* this is a comment */” >> hello.c

# show line by line modifications
svn diff

# show modified files
svn stat -v

…


SVN Hints

  • To set a default editor for commit logs use
    • setenv EDITOR vi
  • To set the “executable bit”
    • svn propset svn:executable on <file>
  • To remove the “executable bit”
    • svn propdel svn:executable <file>
  • To edit the externals use
    • svn propedit svn:externals .
    • each line contains: <local-dir> [-rXXX] <svn-url>
  • For UTF-8 file names use
    • setenv LC_CTYPE en_US.UTF-8
  • To set the “end-of-line style” to “native”
    • svn propset svn:eol-style native <file>
  • To enable svn “auto-props” append to ~/.subversion/config:
[miscellany]
enable-auto-props = yes
global-ignores = *~ *.o *.a Makefile CMakeCache.txt CMakeFiles cmake_install.cmake install_manifest.txt
[auto-props]
*.c = svn:eol-style=native
*.cpp = svn:eol-style=native
*.h = svn:eol-style=native
*.java = svn:eol-style=native
*Makefile = svn:eol-style=native
*.sh = svn:eol-style=native;svn:executable
*.txt = svn:eol-style=native
*.html = svn:eol-style=native
*.css = svn:eol-style=native
*.rc = svn:eol-style=CRLF
*.dsp = svn:eol-style=CRLF
*.dsw = svn:eol-style=CRLF 
*.txt = svn:mime-type=text/plain
*.html = svn:mime-type=text/html
*.css  = svn:mime-type=text/css
*.jpg  = svn:mime-type=image/jpeg
*.png  = svn:mime-type=image/png
*.svg  = svn:mime-type=image/svg
*.pgm = svn:mime-type=image/x-portable-graymap
*.ppm = svn:mime-type=image/x-portable-pixmap


SVN Serve

If you want to run your own SVN server, please have a look at the SVN Serve tutorial.

Options: