After yesterday’s post about source code control systems, here are some notes about getting started with it.
I’ll focus on Subversion, because that’s what I’m using.
Update, 2013 – Subversion is no longer in use, everything is now on GitHub.
There are two sides to a version control system: the server, where the source code “repository” is kept, and the client, which is your own workstation / PC. In distributed systems (DVCS), for example Git, there is no such asymmetry – client and server can perform the same tasks. But Subversion uses a centralized model: one repository, any number of clients.
You’ll need at least the client software to be able to use Subversion:
On Mac OSX it’s built-in (starting with 10.5, Leopard, I think) – although you probably have to install the Xcode that came with your Mac. You can check by typing “svn” in the terminal. If you get “Type ‘svn help’ for usage.”, then it’s ready for business.
On Linux, it’s a package called “subversion”. Install via your favorite package manager, depending on which flavor of Linux you’re using (e.g. Ubuntu).
Here’s the big picture:
Assuming you’re trying this out with existing source code (from JeeLabs, for example), then the server is somewhere on the internet, so you don’t have to deal with its setup. A little terminology and some conventions:
- the server has all the source code and all the changes in a “repository”
- you can get a local “working copy” by “checking it out” from the server
- you can do anything you like on your copy, but…
- don’t rename files or move anything around, without telling subversion
- you cand send changes back by “checking them in” (a.k.a. “commit”)
- you can adjust your copy to the latest version by “updating” your copy
So the basic idea is to check out a working copy (once), and then doing an update whenever you want to make sure you have the latest and the greatest. This update is totally safe: if you made changes on your working copy, and you do an update, subversion will tell you that there are changes, and occasionally maybe even a “conflict”, i.e. if your changes interfere with changes made by others.
Note that conflicts can occur in two ways: when updating your modified copy, or when committing your changes back to the server. The latter can lead to a conflict when someone made changes which you didn’t pick up yet.
If you don’t intend to make changes, or only minimally, then conflicts are a non-issue. In that case, subversion is simply a convenient (and very quick/efficient) way to keep up to date with all the latest changes.
There is another convenience, in that you can check to see what has changed, before updating. Perhaps you want to first examine the changes – and only the changes – to decide whether you want to adopt that latest version. This can be done with the subversion “status” and “diff” commands, but with the new Redmine setup at JeeLabs, it’s actually much simpler to use the web interface for this. Simply browse the repository from the web, starting here, and review the comments shown under the “Latest revisions” header (last ones are at the top). By zooming in as described yesterday, you can quickly see whether the changes are of interest to you. You could even decide to opdate only a single source file. Subversion will track all this, even with some parts updated and others not.
Let me give an example how to use subversion for getting the Ports library. I’ll use the command-line interface for this, assuming that it maps relatively simply to the GUI-based TortoiseSVN on Windows.
Step 1 – Initial check-out
You need to be in the proper place to check out. In this case, we want to get a “Ports” folder inside a “libraries” folder, which in turn needs to be placed inside the Arduino IDE’s “sketches” directory. On Mac OSX, this is “~/Documents/Arduino”. So the first thing to do, is make sure that the libraries folder exists:
% mkdir -p ~/Documents/Arduino/libraries
Then go to that directory:
% cd ~/Documents/Arduino/libraries
Now, we’ll check out the Ports library from JeeLabs:
% svn checkout svn://svn.jeelabs.org/jeelabs/trunk/libraries/Ports
A new directory is created with the same name, i.e. “Ports”. It will contain all the source files. So we’re essentially done.
One thing to note, is that subversion needs to track a bit of information (such as where this Ports thing came from and when it was checked out). It does this in sub-folders called “.svn” – the leading dot is a convention to keep this area hidden. So it’s there all right, but you often won’t see it. That’s fine. Never go in there and make any changes, or you’ll completely mess up your working copy, as far as subversion is concerned.
Step 2 – Using the source code
Nothing much to say here. Just use it as if you had created the Ports folder yourself, perhaps by unpacking a ZIP archive. Apart from the “.svn” hidden areas, it’s the same thing. You should check out a copy of the RF12 library as well, since that’s always needed when using the Ports library:
% cd ~/Documents/Arduino/libraries % svn checkout svn://svn.jeelabs.org/jeelabs/trunk/libraries/RF12
You can make changes to the source files if you want to. Subversion will know what you did, when asked, because it keeps an extra copy of the original files in the “.svn” folders, and besides, it could also use the server to check.
The one thing you can’t do with subversion (some other systems are more lenient), is to delete, rename, or move files and folders as you were used to. Don’t do things like these, or use other commands or programs to the same effect:
% mv foo.c bar.c % rm foo.h
Instead, ask subversion to do it for you, and all will be well:
% svn mv foo.c bar.c % svn rm foo.h
Step 3 – Tracking updates
Updating your copy to the latest version in the repository is trivial, and can be done at any time with:
% svn update
You will get a concise summary of which files were changed. Changes may also include adding new files and removing obsolete ones. After the update, your working copy will be in sync with the repository again.
If you only want to find out what would be changed, without actually updating anything, use the status command:
% svn status -u
But usually it’s simpler to use the web interface for this. For the Ports library, just go to http://jeelabs.net/projects/cafe/repository/show/Ports.
Step 4 – Submitting changes
If you have write access to the repository, then you can also submit the changes you made in your working copy. All you need is a reason… :)
Seriously, the only thing that needs some thought is what to put in the comment describing this change. Because Subversion insists on some comment. I usually check in after every little change and add a very brief note describing it, such as:
% svn ci -m "fixed bug X so Y works again"
Note that with all these commands, you don’t have to mention the respository at all. Subversion knows which repository is being referenced by looking at the information in the “.svn” folder in the current directory you’re in.
I won’t go into conflict resolution or any of the numerous more advanced commands in subversion. Check the excellent manual online to find out more.
The only comment I’d like to add to conclude this weblog post, is that the above was written specifically for subversion. Many other systems exists, but once you are used to the terminology and conventions of one, using another version control system is usually no big deal. Lots of people seem to be using Git these days. No doubt due to the free GitHub source repository server, available to anyone who wants to maintain their source code in a public place. It comes with a nice and powerful set of features, see the help section for an overview.
Have fun with version control – and say goodbye to version mixups once and for all!