Working with Subversion

Filed under: Technical References

Posted: March 7, 2009

NOTE: As stated above, how you use Subversion depends on how many projects you will have in your repository. The following is currently written in a locally based uni-project centric way and is not very helpful to somebody who wants to manage many different projects using a remote server.

Importing Your Code

If you organize your files according to common convention, the actual tree of your code might be made like so:

mkdir -p ~/Documents/code/${PROJECT_1}/trunk/
touch ~/Documents/code/${PROJECT_1}/trunk/super-crunch.c
mkdir ~/Documents/code/${PROJECT_1}/tags/
mkdir ~/Documents/code/${PROJECT_1}/branches/
mkdir -p ~/Documents/code/${PROJECT_2}/trunk/
touch ~/Documents/code/${PROJECT_2}/trunk/index.php
mkdir ~/Documents/code/${PROJECT_2}/tags/
mkdir ~/Documents/code/${PROJECT_2}/branches/
mkdir -p ~/Documents/teX/${TEXDOC_1}/trunk/
mkdir ~/Documents/teX/${TEXDOC_1}/tags/
mkdir ~/Documents/teX/${TEXDOC_1}/branches/

Or, if you’re using a modern shell, you simply write:

mkdir -p ~/Documents/code/{${PROJECT_1},${PROJECT_2}}/{trunk,tags,branches}
mkdir -p ~/Documents/teX/${TEXDOC_1}/{trunk,tags,branches}
touch ~/Documents/code/${PROJECT_1}/trunk/super-crunch.c
touch ~/Documents/code/${PROJECT_2}/trunk/index.php

The most important thing about the directory listing above is the use of trunk, branches, and tags subdirectories off the main project listing. Don’t be confused by the ~/Documents root directory, as that is just to make it simple on us. You could just as easily do:

# Start our project workspace
mkdir ~/svn_projects/
# Lay out the directory structure for our first project. It is a small one.
mkdir ~/svn_projects/${PROJECT_1}/
mkdir ~/svn_projects/${PROJECT_1}/trunk/
mkdir ~/svn_projects/${PROJECT_1}/tags/
mkdir ~/svn_projects/${PROJECT_1}/branches/
# Add the entry for the first file of our first project.
touch ~/svn_projects/${PROJECT_1}/trunk/super-crunch.c
# Now we add our next projects directory structure to the workspace.
# This one will be a larger project so it uses a slightly different directory structure.
mkdir ~/svn_projects/${PROJECT_2}/
mkdir ~/svn_projects/${PROJECT_2}/trunk/src -p
mkdir ~/svn_projects/${PROJECT_2}/trunk/docs -p
mkdir ~/svn_projects/${PROJECT_2}/tags/
mkdir ~/svn_projects/${PROJECT_2}/branches/
# .. And add our first source code file to it.
touch ~/svn_projects/${PROJECT_2}/trunk/src/index.php
# And don’t forget the documentation!
touch ~/svn_projects/${PROJECT_2}/trunk/docs/readme.txt

Again, using shell globbing, this reduces to:

mkdir -p ~/svn_projects/{${PROJECT_1},${PROJECT_2}}/{trunk,tags,branches}
mkdir -p ~/svn_projects/${PROJECT_2}/trunk/{src,docs}
touch ~/svn_projects/${PROJECT_1}/trunk/super-crunch.c
touch ~/svn_projects/${PROJECT_2}/trunk/src/index.php
touch ~/svn_projects/${PROJECT_2}/trunk/docs/readme.txt

If you’re lazy or not worried about the giant mess it will make, you can add all of the projects in the ~/Documents folder (From the top directory listing above) to your svn repository very simply with the command:

svn import ~/Documents file:///${HOME}/.svn

However if you chose to go the more well designed route and used the second - and more shallow and thus faster to use - directory structure, you will have the option of using each subdirectory as its own sub-repository.

Using the second directory structure above as a perfect example of this, if you do the following you will be adding two different projects to your one repository.

svn import ~/svn_projects/${PROJECT_1} file:///${HOME}/.svn/${PROJECT_1}
svn import ~/svn_projects/${PROJECT_2} file:///${HOME}/.svn/${PROJECT_2}

The additions and changes in the last set of commands is very important. The addition of the extra last part of the third argument allows you to place different projects in different subdirectories of your repository. Don’t forget to add it if you’re using multiple projects in one repository!

NOTE: If you do forget it, all of the files in the project you are importing will be imported directly into the root directory of the repository you’re importing to and not the subdirectory for that project, and you will have a big mess to clean up. If you’re using a different repository per project, you don’t have to bother with this extra namespace but if you want multiple projects per repository this is necessary.

To list files in your repository

svn list –verbose file:///${HOME}/.svn

NOTE: If you wish to destroy your repository you can safely “rm -rf $HOME/.svn” and all of your hard work in ~/svn_projects/ will remain intact, however, you’d better make sure that you svn update if you want the latest revision that others may have submitted.

Once you have done the intial import, you can update the files under any of the project directories without modifying the repository.  To commit your changes to the repository, run:

svn commit

In the directory. It will ask you to give a comment about the changes you made.  If you don’t remember all the changes, you can get a quick reminder with:

svn diff -u

This will list all of your changes, with the repository copy lines starting with a “-’ sign and your local additions starting with a “+” sign.

If you want to work on your files from multiple locations, you can do:

svn checkout svn+ssh://paris/home/ugrad/username/.svn

In this case you will need to make sure each copy is up-to-date before working on it.  You can do this with:

svn update

If you are unsure about whether your files are up-to-date or not-committed, use:

svn status

More complete documentation can be found at: http://subversion.tigris.org/