Saturday, January 06, 2007

Version Control with subversion

Introduction

Version control is the art of managing changes to information. It has long been a critical tool for programmers, who typically spend their time making small changes to software and then undoing those changes the next day. But the usefulness of version control software extends far beyond the bounds of the software development world. Anywhere you can find people using computers to manage information that changes often, there is room for version control. And that's where Subversion comes into play.

About Subversion

Subversion is a free/open-source version control system. That is, Subversion manages files and directories over time. A tree of files is placed into a central repository. The repository is much like an ordinary file server, except that it remembers every change ever made to your files and directories. This allows you to recover older versions of your data, or examine the history of how your data changed.

Subversion can access its repository across networks, which allows it to be used by people on different computers. At some level, the ability for various people to modify and manage the same set of data from their respective locations fosters collaboration. Progress can occur more quickly without a single conduit through which all modifications must occur. And because the work is versioned, you need not fear that quality is the trade-off for losing that conduit—if some incorrect change is made to the data, just undo that change.

Installing SVN Client

In Linux

The easiest way to get Subversion is to download a binary package built for your operating system. Subversion's website (http://subversion.tigris.org/project_packages.html) often has these packages available for download. The site usually contains graphical installer packages for users of Microsoft operating systems. If you run a Unix-like operating system, you can use your system's native package distribution system (RPMs, DEBs, the ports tree, etc.) to get Subversion.

In Windows

Download Tortoise-svn from the site http://tortoisesvn.net/downloads and install in your windows machine.This can also be downloaded from IIITM-K sagar machine using "user" and "user" as username and password respectively

SVN Main Concepts

Repositories
Working Copies
Import/Checkout

Update/Commit
branch/Tag
Switch/Merge

Preliminaries:

(This is a typical demonstration of SVN usage cycle reproduced from IIITMK Web Techology class)

Login to the file server using ssh (or any other tool on windows). You must have an account on the machine The machine's IP is

192.168.0.3


1. Accessing the svn repository:

Students repo:

svn list svn://godavari.iiitmk.ac.in/users/students/

You will need a password (which will be given to you in the lab)

svn co svn://202.88.239.61/users/students/

Faculty repo:

svn co svn://godavari.iiitmk.ac.in/users/faculty/ (internal) svn co svn://202.88.239.61/users/faculty/ (external)

Staff repo:

svn co scn://godavari.iiitmk.ac.in/users/staff (internal) svn co svn://202.88.239.61/users/staff (external)

2. Creating a local directory


Create a directory on your machine:

 trunk
-- hw0
-- a.txt
-- b.txt
tags
--
 branches
--
 release
--

3. Importing to SVN


svn import test SVN-LOC

4. Checking out a working copy into new location


svn co SVN-LOC/trunk/hw0

5. Remove old local directory


6. Try out svn commands:


   -- status
-- log
-- revisions

7. Modify your working copy


   -- svn status
-- svn mkdir
-- svn add
-- svn rm
-- svn rmdir
-- svn move
-- svn copy


8. Commit your changes to the repository


   -- svn commit

9. Update your working copy


   -- svn update

10. Reverting


   -- svn revert
-- svn co -r version#


11. Troubleshooting


    -- svn cleanup
-- svn --force ...


Try this in Your Machine

Before we import files into the central repository, we need to create the directory structure in the local folder. The following command illustrate this.

    $mkdir /home/user/repos/trunk
$mkdir /home/user/repos/branches
$mkdir /home/user/repos/tags

then check the directory

    $ ls /home/user/repos/

it will print

    branches tags trunk

The above mentioned directory structure in the local machine can be imported to the central repository as shown in the command below

    $ svn import --message ”to the server repository” repos <>

Files from central repository can be exported to client machines using following command.

    $ mkdir ~/work
$ cd ~/work
$ svn checkout svn://server-hostname/path-to-your-svn-space/trunk

In order to check the working copy,

    $ cd ~/work
$ cd trunk
$ ls -al

You should be seeing a directory .svn.

Let us create a directory under trunk, say homeworks;

    $ cd ~/work
$ cd trunk
$ svn mkdir homeworks
$ cd homeworks
$ svn mkdir hw1
$ cd hw1

Let us create a couple of files in hw1 say test.txt and a.txt

    $ touch test.txt
$ touch a.txt

Checking the status of working copy will yeild following results

    $ svn status
? test.txt
? a.txt

The question mark means that the files are not versioned

Now let us make the files versioned by svn

    $ svn add test.txt

If we check the status

    $ svn status
A test.txt
? a.txt

The A test.txt means that the file is scheduled to be added to the repository.

Let us commit the changes made to the local copy to the repository

    $ cd ~/work/trunk
$ svn commit --message "Added homework directories"

It is always recommended to do the commit from the root directory of working copy here (~/work/trunk).
It is a good practice to give a reasonable message during a commit to help the people working with you to understand what happend.

Now to illustrate commit , merge and conflict resolution let us consider small example code shown below. The code mentioned below is contained in a file called TestDemo.java

   public class TestDemo {
public static void main(String args[]){
System.out.println(“Subversion Test”);
}
}


Put the file TestDemo.java ,just created in your trunk directory in the local machine, and to add this file to your central repository use the command

   svn add TestDemo.java

Now you can use the commit to reflect the changes in server.

   svn commit -m=”added a File” TestDemo.java


Now once again commit your trunk directory as mentioned earlier.

Now the file is stored in the trunk folder in the central repository and can be checkout to the local machine's folder. Suppose you checkout your trunk folder and it already contains the file TestDemo.java , if you made any change to this file ie. alter the code , and if you want to see the difference between your local repository and central repository do the following command.

   $ svn diff TestDemo.java

References

1)SVN Online Book - http://svnbook.red-bean.com
2)SUBVERSION Project - http://subversion.tigris.org



The practical command-set explained here are reproduced from Web-Technology classes from IIITMK,Park Center,Trivandrum

No comments: