Introduction to Subversion for .NET Developers

I presented a 15 minute introduction to Subversion during the Alt.NET Community on Alternative .NET tools evening last week. The video should appear online soon at the Skills Matter site. Here is the talk in a more readable form meanwhile.

Subversion is my favourite version control system. I’ve been using it for about two years now (I first wrote about it first in april ’07) and so far I am very happy with it. It was built as a replacement for CVS, which was the standard version control system in the Unix/Linux world and more or less de facto standard for opensource projects at the time when Subversion was started (sometime in 2000). Subversion took the best ideas from CVS, added some very interesting concepts and solved most of the problems that people had with CVS (but not all of them). It is now the typical choice for any new Java projects and replaced CVS as the standard version control system in most of the opensource projects. Over the last few years, it is becoming more and more popular for Windows .NET projects as well.

Lots of tools for Subversion

CVS was never really popular among developers working on Microsoft Windows because of poor tool support. SVN, on the other hand, has very good support for Windows. In fact, good tool support is one of the best things about Subversion. Since it is used on so many operating systems and for so many development platforms, Subversion has quite a few tools that you can use. The default one that comes with the basic installation is the command line tool, which gives you full control of the system but it is not really what is normally considered user friendly. Even if you are not used to command line tools, I suggest looking into it because it will help you automate tasks. A very interesting Windows tool is TortoiseSVN, an opensource plugin for Windows explorer that allows you to perform all operations with Subversion from the normal Windows file window using a right-click context menu. TortoiseSVN is a great tool for people that do not want to use an IDE or learn another tool, such as business analysts that just want to check-in and check-out acceptance tests. It is also useful for database developers that use IDEs such as TOAD which have no concept of version control.

Two another very important tools for .NET developers are AnkhSVN and VisualSVN. AnkhSVN is an opensource plugin for Visual Studio which enables you to perform version control operations from the IDE. I use the command line SVN tool for everything, so I’m not really the best person to tell you about ins and outs of AnkhSVN. I evaluated it the last time more than a year ago, and it was not really functionally complete but it could do the basic operations. Apparently it has been significantly improved over the last few months. If AnkhSVN does not do the job for you, try VisualSVN, a commercial Visual Studio plugin that integrates SVN as a source code control system into Visual Studio. It’s not very expensive, and should have all the functions that you might need. Again, this is just from what I’ve heard, I do not use it myself.

A tool that is especially interesting for me is ViewVC. ViewVC is a web front-end to Subversion, running on Apache. It will give you proper read-only access to a SVN repository using a browser, so it is very useful to look at the changes or code from a machine that does not have SVN installed. In the past, I used this to give the support team access to the code and deployment history. Subversion itself can provide HTTP access using Apache, without additional tools, but you need to remember a lot of cryptic URL commands for that. ViewVC gives you a point-and-click interface to browse revision history, see colour-coded differences between versions and browse through the repository. For a live demo, see the dbfit project repository on Sourceforge.

In general, you’ll find that opensource tools have great support for Subversion. Cruise Control .NET, for example, supports Subversion out of the box even for initial checkout and labeling. Team System support is still incomplete. So if you want to use lot of .NET opensource tools in your process, putting the code on Subversion makes a lot of sense.

Subversion is atomic

The best feature of Subversion is atomic commit. That means that the whole change set is either going to succeed completely or fail, regardless of how many files there are in that set. For example, if you did a big refactoring of the source code and want to commit 50 files, but the 47th commit fails because someone else modified that file meanwhile, changes to the first 46 files will be rolled back on the server and you’ll be asked to resolve the problem before committing again. If the commit went through partially, the build would fail on the server and you would have to check what was committed and what failed. So the atomic commit solves a lot of consistency issues that happen with some other version control systems.

There are two more important things to know about those atomic versions. First is that the whole repository has a unique version sequence, incremented on every commit. Second is that Subversion stores only differences between versions for text files in the change log. These two features have some interesting implications. For start, you don’t have to remember to tag important versions. Version completed by each commit is assigned a unique number, and you can easily use that number to download the snapshot of how files looked like at that point in time. On .NET projects, I use this as the DLL version. When you need to troubleshoot, right-click on the DLL, see the version number and then you can easily checkout the exact source of that code version. Another important thing coming out from storing only the differences in files is that tagging and branching effectively does very little — it just creates a new revision without modifying any files. So you can easily tag or branch 50 MB of files in a millisecond.

Subversion works smart

Subversion has many smart functions that make it a very good choice for a development version control system. It really helps to automate some of dull tasks.

Storing differences in text files and automatic versions allow Subversion to provide one very interesting auditing function. Called “blame”, this function lists a file with every line marked with the last change version and the person who changed it. (This is available on the web frontend viewvc as well — see a live demo again on Sourceforge).

Since versions are stored as differences, we can easily roll back any particular version by reversing the differences, without affecting any other changes. Similar to that, we can take a particular set of differences (eg changes to a branch) and then apply it to different files (eg main branch). So Subversion makes merging branches relatively easy.

Subversion is also smart enough to understand that it is rarely the only tool applied to a project, so it does not require you to check-out large branches in order for tools that don’t know about Subversion to work correctly. It will find new or missing files and folders and appropriately suggest adding or removing those from the repository. Subversion works in edit-and-merge mode which means that two people can work on the same file at the same time, as long as they do not change the same line, and it will be able to merge the changes correctly.

The only real problem with Subversion, also inherited from CVS, is that it is a folder-based system, which means that it will store meta-data in a hidden .svn folder inside your folders. If you rename or copy a folder that was committed to the version control, you will also copy the meta-data files but they are no longer correct. So changing something and committing back will actually change the old location on the version control server. Renaming and moving individual files is not a problem, but with folders it is much more complicated. That’s where TortoiseSVN can help again, since you get the subversion rename, copy and move functions basically in the same menu as the normal ones, so you just have to remember to click on the correct option.

Links for the end

Here are some links to download tools or look for more information:

Image credits: Locomoco, Derek Farr, Pierre J.