|
Introduction
Whether you call it source control, version control, or source code management,
if you're in the business of software development, providing a safe repository
for your code is critical.
Most of us know that we should be using some sort of source control software.
Developers who work on team projects usually don't have a choice... after all,
someone needs to act as a traffic cop to keep order. But source control has
benefits to offer the lone developer as well. While products like Microsoft's
Visual Source Safe and Visual Studio Team Foundation Server are often out of the price
range for most individual developers, that doesn't mean you should forego source
control altogether.
This article will offer a quick introduction to
Subversion, an open-source version control system that you can get for free and
have up and running in just a few minutes.
Installation
You can find links to download the Subversion from the Subversion Project's
Getting Subversion page.
Actually the most likely problem you'll have is that you'll find too many links
and not know which one to use. Tending to want to get things straight from the source,
I'd recommend you download the release directly from Tigris.org. You'll find
the link in the Windows binaries section.
For those of you having trouble finding it, the link I used is
subversion > Releases > Windows.
Once there, you'll see a number of files. I'd recommend you choose the
highest version number available as a Windows MSI installer. As I write this that
is Setup-Subversion-1.5.3.msi.
Initial setup is a breeze:
You can simply use the defaults for most of the options. The one screen
that might confuse you some is the one labeled "Select Apache Binding".
Subversion can be configured to use Apache as its server. For the purposes
of this article, we'll be using Subversion's built-in server: Svnserve.
Unless you plan on using Apache with Subversion, this setting doesn't matter.
If you are planning on using Apache, choose the setting that corresponds to
the version of Apache you have installed.
Creating a Repository
A repository is where subversion stores all your files. It's basically a database designed
to store files and information about them. Before we can do much of anything we need to
create a repository. I'm going to place it on the root of my computer's C: drive and call it "svn_data".
-
Open a command prompt window.
-
Type the following command:
svnadmin create C:\svn_data
If you decide to keep your repository in a different location simply replace
"C:\svn_data" with the full path to the location where you want
Subversion to create your repository.
Setting Up a Server
If you work alone and you'll only be accessing your Subversion repository from
the local machine it is possible to start using it immediately after installation
using the file:// protocol. Personally I prefer using the server-based method
and recommend that you take the few seconds it takes to set up a server
even if you do fit the criteria above.
As I mentioned earlier, I'll be using Svnserve. According to the documentation:
Svnserve - a lightweight stand-alone server which uses a custom protocol over an ordinary TCP/IP connection.
In most cases svnserve is easier to setup and runs faster than the Apache based server.
And now that SASL support is included it is easy to secure as well.
To install Svnserve as a Windows service and set its root to the repository we just created,
enter the following command at a command prompt. If it's still open, you can use the same command
window you used to create your repository.
sc create svnserve binpath= "\"C:\Program Files\Subversion\bin\svnserve.exe\" --service --root C:\svn_data" displayname= "Subversion Server" depend= tcpip start= auto
It's a long command so it probably line wrapped in your browser, but that should all be on one line.
If you installed Subversion to a different folder or chose a different location for your repository,
you'll need to modify the command to reflect those differences.
Once installed you'll need to start the service which can be done by simply rebooting or
typing the following command line:
net start svnserve
Once you've installed your Svnserve-based server, you'll access it via the svn:// protocol.
Seting Up User Accounts
Next you'll need to setup usernames and passwords to track the users that are able to access your repository.
You can do this by editing the "C:\svn_data\conf\svnserve.conf" and "C:\svn_data\conf\passwd"
files.
In "C:\svn_data\conf\svnserve.conf", just uncomment the line that says "password-db = passwd".
That tells it to look to the "passwd" file to get a list of usernames and passwords. Obviously the next step is to edit
"C:\svn_data\conf\passwd" and add a line with your desired username and password. It's pretty simple and
the files are commented quite well.
Importing a Project Into the Repository
What you store in your repository and how you store it is totally up to you.
For illustration, I'm going to work with a very simple folder called "C:\MyWebSite"
that only contains two files "default.htm" and "products.htm".
I'm going to ignore the whole trunk, branches, tags structure for the time being.
I do recommend that you do a search and read up on the concept before you decide
on a structure for your own repository. A good place to start is
Chapter 4. Branching and Merging
from O'Reilly Media's "Version Control with Subversion".
To get your project into Subversion's repository, you use the "svn import" command.
Here's the sample command you should use if you've been following along with the example:
svn import C:\MyWebSite svn://localhost/MyWebSite -m "Import MyWebSite"
Notice that we were able to omit the path to the repository since we specified the root folder
when we installed the svnserve service.
You'll be prompted for your username and password. Once you enter them you should
get a result similar to the following:
C:\>svn import C:\MyWebSite svn://localhost/MyWebSite -m "Import MyWebSite"
Authentication realm: <svn://localhost:3690>
Password for 'username': ********
Adding C:\MyWebSite\products.htm
Adding C:\MyWebSite\default.htm
Committed revision 1.
Granted our sample project is quite small, but its now stored in our Subversion repository
and has been given a revision number.
Checking Out a Project
Now that our sample project lives in the Subversion repository, if we need to make any changes
we first how do we get a working copy. This is done via the "checkout" or "co" command.
I'm going to checkout the whole folder.
-
Open a command prompt window.
-
Change to the folder in which I want to create my working copy.
CD \Temp
-
Type the following command:
svn checkout svn://localhost/MyWebSite
-
You should see a result similar to the following:
C:\Temp>svn checkout svn://localhost/MyWebSite
A MyWebSite\products.htm
A MyWebSite\default.htm
Checked out revision 1.
And you should have a new folder named "C:\Temp\MyWebSite" that contains the two files.
Committing Changes
Now that I've checked out my sample project. It's time to make some changes.
I'm going to add some additional HTML to my working copy of the "products.htm" file.
Once the changes are made, it's time to check and see what those changes were and add them back to the
repository.
-
If you aren't still there, open a command prompt window and change to
your working folder.
CD \Temp\MyWebSite
-
To see the differences between your working version of a file and the version in the
repository, use the "diff" or "di" command:
svn diff products.htm
Notice we don't need to specify the full path (since we're in the working folder)
or the repository (since Subversion manages this information on checked out files for us).
You should see something like the following depending on the changes you made to the file in question:
C:\Temp\MyWebSite>svn diff products.htm
Index: products.htm
===================================================================
--- products.htm (revision 1)
+++ products.htm (working copy)
@@ -6,5 +6,11 @@
<h1>Products</h1>
+<ul>
+<li>Product 1</li>
+<li>Product 2</li>
+</ul>
+
+
</body>
</html>
-
Now that we've seen the changes and we're sure we want to commit them, we use the
"commit" or "ci" command.
svn commit products.htm -m "Added Product1 and Product2"
Unlike most of the other commands, this one requires that you include a comment.
This should briefly but accurately describe the changes that you're checking in.
C:\Temp\MyWebSite>svn commit products.htm -m "Added Product1 and Product2"
Sending products.htm
Transmitting file data .
Committed revision 2.
Updating the Working Copy
Now that we've checked in our changes, you might think that our local copy is back in sync with the
server, but that's not the case. If you take a look at the output from the "svn info" command,
you'll notice that it still says we're using revision 1. In order to get our working copy up to date we
need to use the "svn update" command. Running update will get the latest versions from the
server and if it succeeds will display a status message that looks like this:
At revision 2.
or whatever version your repository happens to be up to.
Conclusion
I hope this article has shown you just how simple version control can be. I showed you how
in just a few minutes you can set up and start using your very own Subversion repository and server.
In this article we focused on the server-side of things and some very basic command-line based
client-side operations. Next time we'll examine some of the graphical tools that help
make using Subversion even easier and can even integrate it into Visual Studio.
In the mean time, you might find the following links useful:
|