What is Source Control and Why Should I Use It?

By: Jon Lebensold

So you’ve started developing this application for a client and naturally, he or she is really pleased with your development. Six months pass. You’re called in again to add a couple features to your existing application. Unfortunately, as you begin development, changes in the source code begin to occur.

Your first inclination is to keep a “beta version” but as the changes start rolling in and the client starts to shift on what they want, you end up doing a bunch of things to keep your head on straight:

  • Start commenting out whole blocks of code in case the client changes their mind
  • Start putting comments like “//this is repeated on line ….” because you know a refactoring would take 10 hours and the client wants something tomorrow at nine AM.

If you’ve just partnered with another developer, collaborating on a work without source control becomes even more difficult since changes in one part of your application may cascade in an unexpected manner.

Source control is part of a larger strategy to take one-off, hodge-podge code and make it well documented and ready for larger scale development. With source control, keeping track of your code doesn’t curtail development. Furthermore, you allow yourself the freedom to experiment, remove or make drastic changes to your code base knowing full well that you can rewind in time and compare what was changed or even revert back if need be.

If in those last six months you’ve gone from a one man shop to a team of developers, source control goes from being a convenience to a business necessity.

Using subversion to manage your PHP code

As projects start to get a little larger, it becomes crucial that you manage your code in an easy and organized manner. Using subversion is a breeze with tools like TortoiseSVN on windows and SCPlugin on the mac.

This article is an attempt at introducing the concept of SVN. There are many great tutorials out there that outline the steps for installing SVN on windows, mac and linux:

Why use SVN at all?

Want to try out that fancy new library from your favorite rogue PHP developer? Looking to clean out all those comments for deprecated code that your client might change his mind on? Subversion is great for handling text and small binary files when changes are frequent and you’ll likely want to step back in time.

… Especially when you introduce unit testing to your code, its always helpful to go back if a future revision of your web application doesn’t pass existing unit tests.

SVN becomes a handy companion when debugging the present leads you astray and the past will save you from future development in developing something you’ve already written.


Architectural View: How SVN can speed up testing.

Triggers are used in SVN to allow developers to create pre and post actions on the commit. This way, you can develop locally, push to a test server for review and then release in an organized and scheduled manner.

SAMPLE PHP WORKFLOW WITH SUBVERSION

SVN Chart

How SVN enters into your existing workflow.

If the goal is to take your PHP application to another level of robustness, where development, testing and production are part of your overall development strategy, SVN will make sure you’re not copying files back and forth in an ad-hoc manner. Naturally, copying files arbitrarily as a project hits above the 10,000 line code mark becomes unwieldy, let alone trying to co-ordinate with multiple developers who are all doing the same thing.

SVN supports post-triggers and ignore lists so that you can keep config files out of your source tree. With triggers, a commit could automatically trigger a re-deployment or “svn update” or “svn export” onto an existing testing server. This way, the testing server becomes a distant twin to your development machine.

What’s the difference between SVN Export and SVN Checkout?

An svn export doesn’t bring the hidden files for tracking source control (either .svn or _svn folders, depending on your configuration). SVN Export is perfect for generating packages or doing a production deployment of your web application since the SVN meta data won’t be pulled from the SVN server.

How SVN manages your source code.

If you’ve used source control in the past, the days of using Microsoft SourceSafe or CVS might still be haunting you. These tools, while great in their heyday, have some serious limitations.

CVS isn’t atomic, meaning that in the event of network failure, CVS checkouts can come in incomplete, essentially leaving your commits half done. While I can’t speak about the current version of SourceSafe, I still remember spending many hours checking and re-checking that all the source files had been properly unlocked. Especially when dealing with classes that were several thousand’s of lines, group development became slow and arduous.

How SVN manages conflicts.

What if two people check out a file and try and do a commit? The second person who’s made changes to the file will have a conflict reported. SVN will insist that the person run “svn update”, an action that can be performed by right-clicking inside the folder checked out by SVN.

SCPlugin integrates with Finder on the Mac:

SVN on Mac

TortoiseSVN under Windows:

SVN Windows

SVN is outlining where the code that was modified begins and where your changes end. It’s also expecting that you’ll resolve the change and commit it back into the tree. TortoiseSVN also comes with a diff tool built in so you can easily track where your source code has changed. If you use a tool like Eclipse or BBEdit on the mac, SVN can easily be integrated into your existing development.

How to replace code in SVN:

Don’t try and overwrite files under source control! SVN is clever enough to know what you’re up to and your commit will fail. If a total rewrite of a file was done, or you need to merge in changes, just select all and paste into the old file. That way the document history isn’t lost.

Merging code can also be done, but large merges might leave you picking up the pieces so committing often (at least once a day) and documenting your commits with messages will keep things organized. The plus side to this is that as a developer, writing a changelog or a document outlining the feature enhancements and bugfixes becomes a simple process of dumping your commit log to a text file.

What About Binary Files?

SVN doesn’t try and do difference comparisons on binary files, so replacing images and other binary application components (like swf’s, compiled libraries or system files) will need extra care. In such cases, using SVN’s locking features might protect binary commit collisions.


This entry was posted on Monday, November 5th, 2007 at 7:41 pm and is filed under Advanced PHP. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

One Response to “What is Source Control and Why Should I Use It?”

  1. KillerPHP.com: What is Source Control and Why Should I Use It?…

Created By Stefan Mischook  |  Top of page  go to top of page