Linux/Unix – Using Version Control Systems

If you’re going to be programming, especially in collaboration with other people, nothing beats a version control system for keeping track of the codebase and the changes you’re making. Don’t think you have to be involved with some big project either using C or some other heavy duty language. You can use a version control system for anything that involves saving frequently changed files on a computer. It could be a simple website or even that novel that you’re writing.

CVS

The most widely used version control system for Linux is CVS. CVS comes with all major distributions and is easily installed as part of the standard installation procedure.

Setting up a CVS server

If you’re thinking of working on a project, even if you’re going to be the only developer, it’s worth your while to set up a version control system like CVS. First, you’ll need to choose where you’ll be putting your files. This is going to be your repository. Though a repository can be part of an existing home user’s directory, I think it’s a good idea to create a new, distinct user for a CVS repository. For this example, we’ll create the user ‘devel’. Once ‘devel’ exists and has its own home directory, you can now create the repository. Log in as the user ‘devel’ and issue the following command:

 

cvs -d /home/devel/ init

You will now see a directory in /home/devel called ‘cvsroot’. Inside this directory, create a directory. For this example, we’ll call it ‘projects’. Inside the directory projects, we’ll create our first file. Using you favorite text editor, just create a file with something in it. If you want to type actual source code, then fine. But anything will do. Now that you’ve got your first directory and your first source code file, it’s time to import this directory into your repository. From now on, it will be your central repository for source code. Issue the following command:

 

cvs import -m “first file” projects devel start

The -m option is for leaving a message. This will appear in our history logs. As we’ll see further along, it’s good to leave messages when changes are made. This will aid in understanding what’s going on during the development process. The rest corresponds to our first directory, the user name and you must finish with start.

Before we do anything else, we should add users to devel’s group in /etc/group. This will permit us to connect to use the CVS repository. Your entry for devel should look like this:

devel:x:1003:mike

That would allow the user ‘mike’ to access the repository. To actually use the repository, we need to do one more thing. You need to add this to the end of your .bashrc file:

 

CVSROOT=:ext:mike@server-name:/home/devel/cvsroot

CVS_RSH=ssh

CVSEDITOR=/usr/bin/emacsclient

export CVSROOT CVS_RSH CVSEDITOR

First, change the cvsroot server-name to the actual server you’re using. You’ll notice the CVSEDITOR variable. What this does is to use emacs as the default editor for messages. For example, instead of typing: -m “message”, you can omit the message part and it will automatically fire up emacs. Now, I happen to have a preference for emacs as an editor. You can use the editor that you like.

Now you’re ready to use the CVS repository on your server. First, let’s get the project. Type this into a terminal:

 

cvs -q get projects

You will be asked for your password. The project will be whisked over via SSH. Now you’re ready to add to it.

 

Here is a list of the most frequently used commands that you’ll be using in your day to day work with CVS

 

cvs add file

This is used to add a file. If you’re adding a binary file, like an executable or an image, then you should use:

cvs add -kb file

 

cvs update file

This sends your changes to the repository.

 

cvs commit -m “message” file

This is for committing the file to the repository. As I mentioned above, if you’d like, you can omit the “message” part and your preferred text editor will be used to write the message.This needs to be used after ‘add’ or ‘update’.

 

cvs status file

This will show you the status of the file. If it’s locally modified, up-to-date with the repository or if it doesn’t exist – this command will show you.

You might also want to look into a graphical tool like cervisia that works quite well and has a lot of options. If you’re not good at religiously checking in your changes to the repository, cervisia will help you track down the modified files more easily.

 

Subversion is an alternative to CVS that is growing in popularity.

Installing Subversion

Users of Debian and Debian-based distributions (like Ubuntu) just need to issue the following command:

 

apt-get install subversion subversion-tools

 Subversion packages are also included in Red Hat’s Fedora Core distribution. If you have the ‘yum’ package management system installed, you can install subversion by issuing the command:

 

yum install subversion

There, of course, also exists the possibility of installing from source.

Creating a Repository

First, I created a directory called ‘dev’ in /home/mike where I am going to keep the repositories for projects I work on. Essentially, what happens is that you have a directory where you’re working and a directory where the repository stores that work for version control purposes. These are, of course, separate.

Let’s say you’ve been offered a project to design a website for a restaurant called Larry’s Clam Shack. If you want to use subversion for version control on the project, you need to first create a repository. To do this, issue the following command:

 

svnadmin create /home/mike/dev/clamshack

Once created, the directory structure will look like this:

 

drwxr-xr-x  2 mike mike 4096 2005-09-14 17:29 conf

drwxr-xr-x  2 mike mike 4096 2005-09-14 17:29 dav

drwxr-sr-x  2 mike mike 4096 2005-09-14 17:29 db

-r–r–r–  1 mike mike    2 2005-09-14 17:29 format

drwxr-xr-x  2 mike mike 4096 2005-09-14 17:29 hooks

drwxr-xr-x  2 mike mike 4096 2005-09-14 17:29 locks

-rw-r–r–  1 mike mike  379 2005-09-14 17:29 README.txt

According to the developers of, it’s a good idea to set up your directory structure for optimum use of subversion. First, create a temporary directory (call it ‘tmp’ if you want) Then create this directory structure inside of ‘tmp’:

 

mkdir larrycs

mkdir larrycs/trunk

mkdir larrycs/branches

mkdir larrycs/tags

Now what we’ll do is import this layout tree into the subversion repository ‘dev’:

 

svn import . file:///home/mike/dev/clamshack –message ‘creating clam shack repository’

You’ll see a message like this:

 

Adding         larrycs

Adding         larrycs/trunk

Adding         larrycs/branches

Adding         larrycs/tags

Committed revision 1.

 

 You can verify that it got created by issuing the command:

 

svn list –verbose file:///home/mike/dev/clamshack

 

Feel free to remove ‘tmp’. I did, anyway, because I get confused easily. I’m actually going to do my work in a directory which will get me the prize for originality: ‘work’ Now that you’re ready to start using the repository. You need to go to the directory that corresponds to ‘work’ and “checkout” the repository. Once you’re in your work directory, issue the following command:

 

svn checkout file:///home/mike/dev/clamshack 

 

You should see the following:

 

A  clamshack/larrycs

A  clamshack/larrycs/trunk

A  clamshack/larrycs/branches

A  clamshack/larrycs/tags

Checked out revision 1.

Working With Subversion

 Now that you’ve got your work area set up, you are free to add files. If you look inside the directory that the checkout created, you’ll find the layout we set up for subversion:

 

drwxr-xr-x  3 mike mike 4096 2005-09-13 18:51 branches

drwxr-xr-x  3 mike mike 4096 2005-09-13 18:51 tags

drwxr-xr-x  4 mike mike 4096 2005-09-13 18:59 trunk

 It’s in the ‘trunk’ directory where we’ll be storing out files. For example, I first created a directory to save the documentation for the project then I added it to the repository:

 

svn add docs/

 

Then we would commit it to the repository. Doing a ‘commit’ is like saying: “Yes, I really want this in there”.

svn commit -m “docs dir” docs/

 In the docs/ directory, I created a file called ‘notes’ where I will jot down things that I have to remember about the project. After creating the first entry, I added it as well:

 

svn add notes

 And then I committed it:

 

svn commit -m “notes file” notes

 I assume you’re getting the idea here.

 

 

 

You can leave off the -m “message here” part and your default text editor will pop up and ask you to write the message there. I prefer to save a step, so I don’t do it. If you do have some lengthy message to write, it might be a better idea to leave it off and use the text editor.

 As you’re working on your project, and you want to see what changes you’ve been making, you can issue the following command to see that:

 

svn log notes

If you’re hard at work on your work copy and you want to look at the differences between what you’ve got and what you started with, you can issue a diff command:

 

svn diff notes

 If you find that you’re changes aren’t what you really wanted, you can convert (or revert) back to the repository’s copy with:

 

svn revert notes

Advantages to Using Subversion

 Since subversion was meant to be a substitute for CVS, its advantages are essentially fixes of what was lacking in CVS. For example, aborted commit actions in CVS sometimes result in the corruption of the repository. This doesn’t happen with subversion. There is also better support for retaining version and history information of files when they are moved or renamed. It has also better built-in binary file support than CVS.

 At any rate, whether you use CVS or subversion, there’s no debate about the importance of some version control system for a programmer.

 

Advertisements

About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 16, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: