Source Control

What is Source Control ?

Source Control Systems are software that are used to track changes to source code files and also to safely store source code.

When you start a new project, where do you keep the source code? If you are a beginner programmer, you may simply create a folder in your computer and start creating and modifying files.

After you spend lot of time on one project, you will realise the need for a backup. So, you may make a copy of your project and save into another location in your computer.

Over a period of time, you may find that something is not working which used to work with an old version of your code. That is the time you realise the need for maintaining various versions of your code. So, you may end up making copies of your project and save into some location with different folder names, probably using the current date as the folder name. This will help you locate any old version of your project based on the date.

Ok, now, what if the hard drive crash all of a sudden? This is the time you will appreciate the idea of taking backup to another external hard drive or CD ROMs.

The real problem starts when more than one programmer has to work on the same file in the same project. It is not an easy job to share files between 2 or more programmers. Each programmer may end up changing the other person's files and may forget to notify him resulting in messing up the changes.

You can resolve most of these issues by using an appropriate source control system.

Purpose of Source Control Systems ?

Source control systems can be used to solve all of the issues discussed in the previous chapter.

1. Keep backup of your source code or documents.

2. Maintain all versions of your files so that you can retrieve any of the old versions of the file easily without searching all over your hard drive.

3. Track which person changed what and when.

4. Control who changes the file to avoid more than one person changing the file at the same time.

5. At any point of time, any of the team members can get the latest version of all files from one single location, without the need to ask each member for the latest files.

6. Track progress - project manager or team leader can monitor the file change activity in source control systems to find out how much work each person has done on each day.

Popular source control systems ?

1. Visual Source Safe (VSS)

VSS is one of the widely used source control systems. This is a Microsoft product and is available as part of the Visual Studio products. Most of the .NET developers use this tool for the source control.

2. Visual Studio Team System (VSTS)

Visual Studio Team System is a new product from Microsoft. This product has several other features buil into it in addition to the source control system. In next few years, most of the .NET developers might switch to this product and abandon VSS!

3. PVCS

PVCS® Version Manager™, leveraging version control technology, organizes, manages and protects software assets across distributed development teams. Version Manager is fast and easy to install, use and administer, yet it provides rich, mature capabilities such as enhanced security, web-based remote access, support for multiple platforms and IDEs, and centrally managed database archives. You can read more about PVCS here - http://www.serena.com/Products/professional/vm/home.asp

4. Surround SCM

Surround SCM is a full-featured version manager that controls who changes your files and tracks what was changed and when. Advanced branching and private branches facilitate concurrent development of products and versions. You can find more information about this product from http://www.seapine.com/surroundscm.html

How Source Control Systems work ?

A Source Control System is a software used to track file changes. Below are the steps required to use Source Control Systems:

1. Install a source control server software in one of the secure servers in your office.

2. Create a new database in the source control server. This database will keep all files for your project.

3. Create users in source control for each member in your team.

4. Install source control client software in each member's machines.

5. Connect the client software to the server software.

6. Start using the source control system by adding files to the server using the client software.

Typically, you will create one source control database for each project in your company. In small companies, each team may install a source control server and create a source control database for their team. But in large organizations, there will be one dedicated, secure server computer in which the source control server is installed. No team members/managers will have access to this system. Only the networking department will access this machine. They will create a separate source control database for each project in the company and create users for that team.

Source Control systems provide a user interface very similar to the windows explorer. You can create folders and add files. The folder structure should be same the folder structure in your project. You will create the same set of folders and add files to the corresponding folders.

When any person create a new file in the project, he will add it to the Source Control System in the correspoding to folder. From that point onwards, the file is "controlled" by source control system. If anybody want to change the file (including the person who created the file), he has to "checkout" the file from Source control. You will learn more about the Checkin and Checkout process in coming chapters.

Source Control Database

Do not get confused with databases like MS Access or SQL Server when we talk about the Source Control database. This is totally different. While MS Access or SQL Server is used to create tables and store data, Source control databases are used to store the files. Each source control system has their own proprietary format and method to store their data(files).

One of the popular source control systems called 'Visual Source Safe' uses text files to store it's data. When you create a new 'database' in VSS, it creates a bunch of files in it's folder. Whenever you checkin a file to VSS, it will break that file into some custom format and store into one or more of the database files it created. However, as a user of source control system, you will not bother about the source control database. You will always use the source control client software to checkin/checkout files. You will not even see where the database is located. The Source Control Administrator may be the only person who need to worry about the source control. He will be responsible to take backup of the database files so that the entire source control system and files stored within it can be restored in case of a system crash.

Source Control Checkin process

When any person create a new file in the project, he will add it to the Source Control System in the correspoding to folder. This process is called "Checkin".

Most of the source control systems provide a windows explorer like user interface. You can checkin files in different ways:

1. Drag and drop files from windows explorer to appropriate folder in source control explorer.

2. Go to appropriate folder in source control system, right click on the folder name and select 'Add Files'. This will launch a file browser which will allow you to select files.

3. Integrate with Visual Studio - most source control systems are integrated with Visual Studio when you install their client software. This is the easiest way to work with source control systems. When you right click on any file in the Solution Explorer in Visual Studio, it will give you the option to checkin or checkout files.

After you add (checkin) a file to source control , the file is "controlled" by source control system. If anybody want to change the file (including the person who created the file), he has to "checkout" the file from Source control.

When you checkin a file to source control, it will make the file in your computer 'Read only' so that you cannot edit it. This is to remind you that the file is controlled by source control system.

You can add new files to source control, but you cannot checkin an existing file unless you have checked out that file.

Source Control Checkout process

If you want to edit a file that is checked in to source control, you must go to source control system and "checkout" the file. All you have to do is, right click on appropriate file in the source control explorer and select "Checkout File". The source control system will remember the folder structure you specified and copy the latest version of the file to the appropriate folder.

When you checkout a file from Source Control, it will copy the latest version of that file into your computer. This will overwrite your copy of that file. So, if you have made any changes to the file in your computer without checking out, those changes will be lost during this process. It is very important that you should not change any file before checking out the file from source control.

When a person checkout a file, the source control system will mark that file as 'locked'. This means no other user can checkout the same file until you checkin that file. This will ensure that the same file will not be edited by different people at the same time.

How to cheat source control ?

There is no way a source control system can stop you from editing a file that is in your computer. Only thing the source control can do is, make the file readonly to remind you to checked out the file

You can always go to the windows explorer and change the 'Readonly' attribute of the file. This will allow you to edit the file!

But not that by doing this, you are cheating the source control. You cannot checkin the file you changed (since you have not checked out the file). If you somehow manage to checkin the file to source control, you may end up messing up the file. Some other members may have changed the same file and you may overwrite his changes by checking in your version of the file. (If you had checked out the file before you changed, then nobody else can checkout that file and change anything which will ensure that you will not lose other's changes on the same file). It is easy for someone to look at the 'history' of the file in source control and find who did what and when. So never attempt to cheat source control systems by changing the 'Readonly' attribute.

Version control and file history

One of the primary advantages of using a source control system is maintaining versions nd tracking changes

Everytime you checkin a file to the source control, it will create a new version of the file. If you right click on anyfile and select 'View History', it will display all versions numbers of the file and also show details like who checked in that version and when was it checked in.

You can select any of the versions in the history and view that version of that file. This will help you to easily revert to an old version of the file if you find that the old version was working better than the new version!

Also, you can select any 2 versions in the history and 'compare'. This will display the difference between those 2 versions. Depending on the source control software, it will display line by line comparison results. This will help you to easily find which member of the team made what change to the file in each version. So you cannot escape saying 'I am not the person who made that stupid change" !