HOWTO: Auto add revision number to script with Subversion

As I have gotten increasingly comfortable with subversion and have started to develop a work flow, I ran into a problem. Most of the scripts I have added to my repository thus far have been self-contained scripts that’d I’d typically run straight from the repository folder.

But I recently ran into a use case where this doesn’t work. For the PowerGUI addon that I wrote, the production files must reside in %AppData%. This is of course not my repository folder so I needed a way to centrally manage my script in subversion and deploy to this new folder when I was ready.
The copy part was simple enough. I wrote a powershell script and placed it in the same SVN folder. It looks like this:

# Force copies files from repository to destination
# Copy; Deploy;

$SourcePath “c:\SVNScripts\Development\addon-scriptbrowser\”
$SourceFiles = @(“scriptbrowser.ps1”, “backuprepo.ps1”)
$DestPath “$Env:APPDATA\Quest Software\PowerGUI\Add-ons\Add-on.ScriptBrowser”
ForEach($File in $SourceFiles{ Copy-Item $SourcePath$File $DestPath -Force }

 

That’s easy enough. But for the centrally managed part, I realized that I could quickly run into confusion wondering if the version in the %AppData% folder was the most recent or not. Wouldn’t it be great if I could embed the version number directly into the file?  It turns out you can! Although it’s worth noting that it will be a little different than what you may be expecting at first blush.

Subversion does not support or maintain per file revision history numbers. Instead, it only keeps track of changes at the repository level. In other words, consider this:

  • First Commit: Add File1.txt and File2.txt (Repo Version 1)
  • Second Commit: Edit File1.txt (Repo Version 2)
  • Third Commit: Edit File2.txt (Repo Version 3)
  • Fourth Commit: Edit File1.txt again (Repo Version 4)

Do you see what’s happening there? File1 now has a revision number of 4 despite it only having had to commits on it. Put another way, since I’m keeping all of my scripts in one repository, this could mean that if I don’t touch a file for a long time and then later make a change, its version could go from version 4 to version 217 in one commit. That’s the bad news. The good news is, the revision number on each file is the last time they were changed. This is the property we are going to take advantage of.

  • From Windows Explorer, select the file or files you intend to deploy to folders outside of Subversion but wish to track with a revision number
  • Right click on the file and choose Properties (just the regular windows properties)
  • Notice the Subversion tab. Go to that and press Properties (Note: The file must already be added to your repo for the tab to appear)

  • Select New / Keywords

     

  • Check Revision and press OK

  • You’ll see a key value pair has been added. Press OK

  • Now, edit your script
  • Add a line # $Revision$ (The # is needed so it’s known as a comment and the double dollar signs are needed so that SVN knows it’s a keyword)

  • Save the file and commit it. Everything will look the same except… go back and look at your file gain
  • You’ll see that it expanded the variable to fill in the current version number!

You can now use the copy script to deploy this to your production folder. Then later if you want to know if you are running the most current version, compare this line in your file to the latest version for that file from Show Log

The more I use of this source control stuff, the more I love it. I’m having way more fun than I should!

  • Robbie

 

Leave a Reply

Your email address will not be published.