Nick Hodges

Delphi Development Pretty Good Practices #2 – Source Control

23 Apr

Okay, so for this first installment, I’ll be illustrating one of my core principles for developing applications:  All my code of any importance at all goes under source control.  All of it.

I’m all by myself – why should I use source control?

We ask about source control use on the annual survey, and a surprisingly large percentage of you are not using source control at all.    That as a bit of a surprise.  If you are working on a team of any size, using source control is a no brainer.  But even if you are working alone, using source control is a really good idea. 

Why, you might ask?  Well, there are a number of good reasons:

  1. It’s good to be in the habit.  Sure, you may be working alone.  But in the future you may not be.  Or your “weekend hobby project” might turn into a popular project with many developers.  If anything like that happens, being in the habit of using source code control will stand you in good stead.
  2. It protects your code.  Since your code is stored in on a server apart from your development machine, you have a backup. And then, you can even backup the code on the server.  Sure, you can zip it all up any time you want, but you don’t get all the other benefits I’m listing here.
  3. It can save your butt.  Sometimes, you might accidently delete something.  You might make mistakes and change code that you didn’t want changed.  You might start off on some crazy idea when you are feeling a bit saucy, and then regret it.  Source control can save you from all of these by making it a piece of cake to revert to any previous state.  It’s like a really powerful “undo” feature.
  4. It allows you to “turn back time”. Say you are a shareware author. You like to release updates and new versions.  And say you get a support request from a customer that has a bug while using a version that is two major releases old. Source control lets you easily recreate the code base for that exact release and debug the problem that the user is seeing.
  5. It makes you think about your process.  Even if you work alone, you should be deliberate and organized in how you write code. If you are in the habit of checking your code into a source control system, you’ll end up thinking more about what you are doing, how you are doing things, and you’ll end up being more organized and deliberate. 
  6. It gives you the freedom to experiment.  Somewhat the mirror image of the previous reason, source control gives you the freedom to say “What the heck, I’ll try that wacky way of doing things!”  Since you know that you can always get back to a known good state, you can be free to experiment and try something that might otherwise hesitate to do. And that experiment might just prove to be a brilliant way to do it. 
  7. It lets you backtrack.  Even when we work alone, we can’t remember every single thing we do and every single change we make.  And I bet at least once in your life you’ve looked at some code and said “Huh? When the heck did that happen?”  With a source control system, you can answer that question very easily.  You can track where a specific change came from and when it was made and maybe even the comment you made when you checked the change in.
  8. It lets you see what changed.  Sometimes, things start acting up. Maybe a section of your application that you haven’t used in a while is behaving differently than you expected.  Maybe it is totally broken and you have no idea why.  Source control can let you track the process and peer into the history of a specific chunk of code to see what changes were made and how those changes affected the project as a whole.

I’m sure you all can think of more reasons.  Bottom line is that if you aren’t using source control, then you should start, no matter what your development situation is.  ZIP files just aren’t going to cut it.  Seriously.

Okay, I’m convinced. What now?

I’m convinced that my points above are so compelling that you are in violent agreement with me, so I’m going to make you use the Subversion client to get the code for this series’ demo project, TextScrubber. TextScrubber is only available from SourceForge under Subversion.   If you don’t use Subversion for source control, you should at least have the command line client on your machine, because Subversion is everywhere, and you should at least know how to get code from code repositories that use it.  I know that there are other well known source control management systems out there.  Git and Mercurial are growing in popularity, but Subversion is probably the most widely used source control system out there.  We use Subversion internally here on the RAD Studio team.

Getting the Subversion Command Line Client

So the first thing you’ll need to do is to get the Subversion client.  (If you already have the client, you can skip this whole section) A whole lot of projects out there — whether on SourceForge or GoogleCode or CodePlex — use Subversion, so having the client is pretty useful.  It’s also small and easy to use, so here’s how you get it:

  1. I’d recommend getting the binary package from Collabnet:  In order to do that, you’ll have to get an account with them. You can sign up here:  http://www.open.collab.net/servlets/Join  If you don’t want to do that, you can get the binaries here as well:  http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91
  2. Go to:  http://www.collab.net/downloads/subversion/ and choose the second download entitled:  “CollabNet Subversion Command-Line Client v1.6.9 (for Windows)”
  3. There is a big orange button there that says “Download”.  Note again that this is the second button on that page as we are downloading the client only.
  4. Press the button and download the file. 
  5. Execute the installer that you downloaded. 

There, you just installed the Subversion client.  The path to the client should now be on your DOS PATH (if it isn’t, you can put it there) and you should be all ready to go on the command line. 

Note: Subversion has only very recently been brought under the umbrella of the Apache Project. As such, it is licensed under the very friendly Apache License, Version 2.0.   It was originally founded by Collabnet, but moved over to be a part of the Apache set of tools just this past February.  It is open source, so if you are really hard core, you can download the source and compile it all yourself.  Me?   I’m not that hardcore. I use the convenient binaries provided by the good folks at Collabnet. 

Grabbing the Code for TextScrubber

Once you have the command line Subversion client installed (it’s called svn.exe, by the way), you can easily download the code for TextScrubber.  Seriously, it’s like falling off a log.  Just do the following:

  1. Open a command window and go to the parent directory where you want the code to go.  For instance, if you want the code to go into c:\code\textscrubber, you want to start with your command prompt at c:\code
  2. Issue the following command: 

svn co https://textscrubber.svn.sourceforge.net/svnroot/textscrubber/trunk textscrubber

co’ stands for ‘checkout’. The second parameter is the URL for the ‘tip’ of the Subversion repository. This command will pull from SourceForge the most recent version of the code into the \textscrubber\trunk directory. That last parameter there is the name of the subdirectory that will be created and filled with the code from SourceForge. 

That’s it. You now have the most recent, up to date code for TextScrubber on your machine.  You are all ready to start straightening and unformatting text!

Using Subversion

In the future, if I make updates to the project, then all you need to do is to navigate to the \textscrubber\trunk directory and do a “7up” or "svn up" command, and it will get the latest and greatest code for you in a jiffy.  Can’t be much simpler than that. That’s the command you’ll end up using the most, probably.

Since it is so popular and commonly used, there is a lot of information about using Subversion out on the web.  I’ve found this tutorial to be very useful, as well as the book (written by the same guys that wrote the tutorial..): Version Control with Subversion

Another useful tool for using Subversion is TortoiseSVN.  TortoiseSVN is a Windows shell plug-in that provides all the functionality of the Subversion client in an easy to use GUI.  Below is a screen shot of the TortoiseSVN Log Window for the TSmiley project on SourceForge.

TortoiseSVN is also an open source project, so you can get it for free.  If TortoiseSVN proves to be valuable to you, I’d also encourage you make a generous donation to the project.  I did.

Getting and Setting Up the Subversion Server

If you make the wise and sagely decision to go ahead and use source control for all of your code, you can easily set up the Subversion server.  You can, of course, download the Collabnet binaries and install them, but if you want to go the pathetically easy route (which I recommend), you should download and install Visual SVN Server.  I’m not even going to go through the steps to installing the server, because they are not much more than download, run the install, and you are done.  That’s it.  It takes like two minutes.  Seriously. They even provide you with a nice Management Console application for administering the server, making that really easy as well. 

(And while you are looking at VisualSVN Server, you can consider upgrading to their Enterprise edition.) 

Now, I myself run the full server (Apache, etc. – the whole ball ‘o’ wax….) on my local machine and use it to manage my code.  This enables me to browse my code in a browser and do other things that are supported by a full blown server   But that might be a bit of overkill for some of you (I’m weird that way – I just want the “full” experience and like to be in learning mode…) and you might not want to do that.  Instead, you can simply create local repositories on your local disk.   The Subversion tutorial above can show you how to do that. 

I have been using the server locally since I use a laptop exclusively and move it between work and home.  But I think I’ll set up the server on a machine at home and check in there. That way, my code will be stored on a separate machine.

More About Source Control

As you can probably tell, this post isn’t meant to be a primer on Subversion or source control in general.  The tutorial I listed above is a good place to learn about Subversion.  If you want to learn more about source control in general, I recommend reading Source Control HOWTO by Erik Sink.  Eric is a really interesting guy who has a great blog and also runs SourceGear, a commercial vendor of developer tools including Vault, a source control management tool that I recommend considering.  You might be interested to note that Vault is free for single users.

That’s it.

And so that is the first “Pretty Good Practice” for developing with Delphi: Manage your code with a source control system.

So, what do you think?  Are you ready to make the move to source control management?

32 Responses to “Delphi Development Pretty Good Practices #2 – Source Control”

  1. 1
    Stamp Says:

    Once I began using Source Control (for both at the office, and my home projects), I never looked back. Your post did Source Control justice! Looking forward to more great posts!

  2. 2
    Rob McDonell Says:

    I’m one of those part-time lone programmers who’s been programming without source control my whole career. I just started a month ago with Visual SVN Server and TortoiseSVN (and I’m also looking at Git). I see all the benefits that Nick talked about. I dunno why I didn’t do this years ago.

  3. 3
    Xepol Says:

    Considering D1 was supposed to come with source control integration, it is perhaps a little embarrasing that D2010 still has no real source control integration today. It apparently has stubs, but it would appear nothing has really been down with it.

    Most disappointing is that the Delphi development team itself, which created those stubs AND has used SVN for years now apparently has made zero attempt to create a plugin which integrates SVN with the IDE.

    Nick, might I suggest that the fact that such a tool/plugin does not exist is actually the strongest endorsement any of us need about source control in general and SVN in particular? (and perhaps shows how lacking the opentools api is itself when it comes to source control - perhaps explaining why no one else appears to have picked up the ball!)

    I’ll grant that source control is perhaps a required evil in larger projects, but with the current state of things - a single developer is better off developing in a VM and making snapshots at regular intervals and major deploy points (the IDE itself keeps __history files to deal with minor issues along the way).

    At least, that continues to be a brief summary of my feeling on the whole subject.

    There is a WHOLE lot more, including how -ALL- source control projects fall short on the higher level project of projects scenario, and how even the IDE’s history features fails to manage related files (.dfm&.pas) as a tightly integrated group - but that is a much longer discussion on the inherent value of source control itself.

  4. 4
    Daniel Says:

    Is anyone using the Version Control from http://www.teamcoherence.com/ ? It seems to be a lot easier to handle as it doesn’t need any 3rd party software. It also has a Free Single-User license. I’ll be experimenting with this VCS.

  5. 5
    Stuart Says:

    Does anyone know of a free/low cost online repository service for non-opensource software? I’d like to use svn client (or other), but would really like a - and I hate myself for using this word - "cloud" server - this gets the code off my site too, without having to swap external HD’s with my wife’s shop every week?

    Tx

    Stuart

  6. 6
    Michael Says:

    @Daniel: I still use it … it is still very handy.

    Turtle for SVN access. What I do not need is in the end IDE integration …

    Frankly I use version control especially for protecting my code against HIT driven restructuring attacks:-) …

  7. 7
    Luigi D. Sandon Says:

    Besides lacking VCS integration hooks easily accessible, some Delphi files are VCS unfriendly.
    1) .dfm, .dpr and .dproj can esily lead to conflicts while merging. A .dfm diff/merge tool would be welcome, and for xml based .proj files as well. Project units should be removed from the .dpr, that in its uses clause should list only the relevant units called in it it, mamage project data in the .dproj files.
    2) Delphi still saves some informations in binary .res files. Those data should be moved to .rc files which are easier to hande in a VCS, and compiled when needed.

  8. 8
    Clóvis Valadares Jr Says:

    I can not understand. So we use a software because it is the most popular? The criterion should not be because is it better? We need to be consistent here.

    Git is far superior to subversion. Delphi language and environment is superior too. We shoud use both.

    If popularity is the sole criterion, then we will all use java.

  9. 9
    Clóvis Valadares Jr Says:

    @Stuart

    Try unfuddle.com/

  10. 10
    Thomas Mueller Says:

    I think your example for checking out the code is wrong. You should never check out the whole tree but only the trunk, so the command should be:

    svn co https://textscrubber.svn.sourceforge.net/svnroot/textscrubber/trunk textscrubber

    If you omit the "trunk", you will alway get all the tags and branches as well as the trunk which is usually not what you want.

  11. 11
    Andreano Lanusse Says:

    Nick, great post!!!

  12. 12
    Boro Says:

    We always used VSS at work (2005) but recently started moving to Subversion/Turtle,…

    Apart VSS becoming obsolete there are some cute things about, like rich gui client with handy project management not really available for Subversion,…

    Also I was wondering whether anyone had experience with migrating some GBs of source code from VSS to Subversion as well?

    Kind Regards,
    B.

  13. 13
    john Says:

    This feels so dated! A lengthy Subversion post when lots of people are moving to DVCS… Come on!!

    Subversion may make sense in some circumstances but you not mentioning git/hg at all, imho, discredits the whole post. Sorry.

    Delphi is so behind not only concerning 64 bit and crossplatform but also in spirit (fortunately the compiler-guys make an exception :-)

  14. 14
    Bruce McGee Says:

    I started using source control full time about 10 years ago, and I wouldn’t dream of doing development without it. Even for solo projects.

    I’m looking forward to more posts like this, and lots of (constructive) feedback on what tools people like to use.

  15. 15
    Uwe Schuster Says:

    @Xepol:
    The Open Tools API has a lot of possibilities and projects like DelphiSVN or JEDI VCS do use them to integrate SVN or JVCS into the IDE.
    Which are exactly the lacks of the Open Tools API related to source control?
    (Are the any QC reports for it?)

    @john: Nick’s post is mainly about using source control and he did mention Git and Mercurial in it! I am quite sure that he would have used Git or Mercurial when he would be an expert in one of them. I guess anyone looking for a Git or Mercurial tutorial can find them very easily. For exam. hginit.com is a tutorial for Mercurial and IIRC it was Nick who told me about it.

    BTW, I hoped anyone from EMBT would blog about their Subversion integration (*), but if one only gets bad feedback, because Git or Mercurial are supposed to be state of the art, it’s not worth to do that.

    How easy is it to install a secure central Git or Mercurial repository in comparison to install VisualSVN server?
    (There can be found enough information for exam. about Git by using different SSH solutions, Gitosis or whatever and that is definitely not easy)

    *=
    see "Delphi 2010 Subversion Support?" in .delphi.non-technical
    https://forums.embarcadero.com/thread.jspa?messageID=144885&tstart=0#144885
    see "Delphi 2010 and Subversion" in .delphi.ide
    https://forums.embarcadero.com/thread.jspa?messageID=210174&tstart=0#210174

  16. 16
    joe Says:

    @Bruce, Uwe

    When I searched for git/hg I didn’t find it, but you are correct, it is - in a good way - mentioned in the post. My apologies.

    PS: and as I am on subscription I am looking forward to crossplatform…

  17. 17
    Luigi D. Sandon Says:

    OpenTools is a powerful API, but is complex too. A simpler API (built over OpenTools) aimed at VCS integration only would be useful, it will ease integration a lot.

    I still don’t understand why IT is often driven by fashion. DVCS are a good example. They were born to solve some issues in large open source projects where development is really distributed, but propaganda says you they are the Solution To All Problems. Oh well, Subversion may deserve it because it was presented the same way, while time has shown it has pros and cons like any tool - and still some of the issues it was designed to solve as well.
    People should first asses what are their needs, and then choose the tool that fits them most. VCS are not an exception, centralized VCS fit some development models while DVCS others. Use them in the wrong way, and they backfire. For example DVCS may lead to developers working in isolation for a long time. It may be very good for some projects, and a big danger for others. Some tools have full ACLs, while others have limited security or no security at all - which may not be an issue in open source projects, but could be very important in other sectors.
    A VCS is a tool that holds a software company most precious asset - code - and should not be choosen just because of actual fashion.

  18. 18
    David Moorhouse Says:

    Meh - got the boys here weaned off their 10 year old addiction to VSS. A little resistance initially but now they’re into SVN :)

  19. 19
    Delphi Development Pretty Good Practices – Source Control « Wagenheimer's Game Development Blog Says:

    [...] Delphi Development Pretty Good Practices #2 – Source Control [...]

  20. 20
    Ken Knopfli Says:

    Sounds like yet another thing to install and configure when I reformat my hard drive.

    I just copy the entire project to an external hard drive in a directory named by date. Easy, esp. with today’s Terrabytes. And I have no problems accessing stuff I did way back in D1 - heck, even in Turbo Pascal! And there are compare utilities if you need them.

    Life is too short.

  21. 21
    Luigi D. Sandon Says:

    @Knopfli: do you make copies when you change a single file? Do you ever mantained an older version and needing to port fixes to a newer one, or viceversa? VCS are much more efficient to handle and track changes in many ways. Installing most VCS if often a matter of minutes - you will take much more time setting up the OS and Delphi. Anyway if you have to reformat your hard disk often you have other issues than installing a VCS…

  22. 22
    Nick Hodges Says:

    Thomas — you are right about the checkout command. I’ve changed the article.

    Nick

  23. 23
    Robert Love Says:

    The only thing I would add is make sure your 3rd Party Libraries are under version control as well, and that you are building and installing them from there instead of there local installs. This insures that all developers are using the same version, and if you rollback you can rollback to the correct version of 3rd Party components as well.

  24. 24
    Nick Hodges Says:

    "John Doe" –

    I did mention git and mercurial.

    Thomas –

    Do you have DUnit installed?

  25. 25
    Ken Knopfli Says:

    @Luigi D. Sandon: Have you ever needed something from years back and found it packed in a format to which you no longer have the program?

    I also work in a team so I do use a VCE. But there we have a department that maintains it. I cannot justify the overhead for single-developer projects.

    As a rule, I prefer self-discipline to myriad utilities.

    To each his own, I guess.

    Reformatting happens when too much clutter begins to slow your PC down, or you install a program that does hidden things, like installing adware or unwanted runtimes.

  26. 26
    Ken Knopfli Says:

    @Luigi: "Do you ever mantained an older version and needing to port fixes to a newer one"

    This happened in my team project, yes. And we had to call a meeting to work out how to do it in our VCS.

  27. 27
    Luigi D. Sandon Says:

    VCS are not easy to learn, that’s true. Each have its own way of doing things, and to use them properly may require time and training. I always used a VCS on my own for its inherent advantages, but also to really learn how to use it. I won’t call it a "utility", it’s my source code database - do you call a database a "utility" and prefer to work with plain files to store structured data changing over time? :) VCS are a kind of specialized database, the fact that the storage could be files or a full database doesn’t change the user perspective - you use the VCS commands to select, insert, update and delete data.

  28. 28
    Ken Knopfli Says:

    "delete data"… We tried out different solutions to the code fork problem. Then, when we wanted to delete the wrong ideas, we discovered the VCS does not allow a true delete! Not a big deal, but annoying to see them everytime we log in, forever.

    It is what I call a "sweaty palms" application. You are so afraid of making a mistake, you get sweaty palms every time you dare do anything unfamiliar.

    So you understand where I am coming from. Fine, if you have a support team. But an additional worry for solo developers.

  29. 29
    Leif Says:

    @Robert Love: Can you or someone describe how to deal with 3rd party libraries. Most of them have visual components and some requires settings in the registry.

    A quick peek into a previous version should not take more than a couple of minutes, otherwise I would prefer to work on virtual platforms (and put the whole platform into a version handler).

  30. 30
    Nick Hodges | Setting up Hudson to compile Delphi Projects Says:

    [...] Delphi Projects by Nick on 5 November 2010 21:37 Just like you should be using Source Control (even if you are a one man operation), you should be doing continuous integration.  We currently use CruiseControl.net, but we are [...]

  31. 31
    Using Version Control for Home Development? | Everyday I'm coding Says:

    [...] Credits also to http://blogs.embarcadero.com/nickhodges/2010/04/23/39416: [...]

  32. 32
    gaming laptops under 500 Says:

    gaming laptops under 500…

    Nick Hodges » Blog Archive » Delphi Development Pretty Good Practices #2 – Source Control…

© 2014 Nick Hodges | Entries (RSS) and Comments (RSS)

Your Index Web Directorywordpress logo
Close