Version control: Automatic SVN propsets

Propset in SVN, or property setting, is the process of adding versioned metadata to your files. In Joomla development it is common to set the magic svn:keyword property to php-files, to put useful and dynamic information about a file into the file itself. In particular you will very often see the Id keyword being used in the docblock heading of the file. In this tutorial we will look at how you can set this property in your php-files, and how you can configure subversion to do it automatically for you.

Setting Id as a keyword, the manual propset

The standard, command line way, of setting the Id keyword on a file, is as follows:

To begin with, you’ll need a php-file that contains the keyword. Let’s say you have index.php in your repository with a docblock that maybe looks like this:
/**
* @version   $Id$
* @package   My Component
* @copyright Copyright (C) 2009 John Doe. All rights reserved.
* @license   GNU GPLv2
*/

Notice the $Id$ on the second line.

Next thing we need to do is to set this keyword as a property of the file. We do this by running the following command:
svn propset svn:keywords "Id" index.php

After that, you can commit the file to your repository, and $Id$ will magically be replaced by something like this:
$Id: index.php 7 2009-11-05 19:53:58Z john $

This means that the file index.php was last changed in revision 7 on the evening of November 5th by the user john.

Easy, huh?

Let subversion fix propsets automatically

When you are adding multiple files to your project, it becomes tiresome to set this property manually each and every time you create a new file; First you will have to add the file to your repository, and they you can set the property and commit a second time to actually see the property change. Instead, you can configure subversion to automatically add Id as a keyword to all your PHP-files. To do this you need to change a couple of lines in your subversion configuration.

The default config file is on Windows located in %APPDATA%\Subversion and on *nix (that means you too, OS X users) it is ~/.subversion, ~ being your user’s home folder. As you can see for *nix, the folder name starts with a period, meaning it is hidden. In other words you won’t be able to for see this folder in the OS X Finder usually. So if you are an OS X user, you can do one of the following to show the hidden folders:

  • Either tell OSX to display the hidden files permanently, by running this command in Terminal:
    defaults write com.apple.Finder AppleShowAllFiles YES
    After running the command, you need to restart Finder. Do this by running the following command:
    killall Finder
  • Or, you can start your text editor, choose Open to open a file for editing, and then in the dialog window hit CMD-Shift-. (period). This will display hidden files and folders. (OS 10.6 Snow Leopard required)
  • Or, you can use Terminal, and edit the file using the CLI.

Inside the config file, you add this:

[miscellany]
enable-auto-props = yes

[auto-props]
*.php = svn:keywords=Id

This will make Id a keyword of any PHP file, and it will automatically be set for you when you add a new PHP-file to your repository.

If you are editing the default config file, everything but the last line should be in there already. Just make sure you remove any # signs in front of the lines, as this comments out the functionality.

Further reading:

SVN Book, Example usages of property setting in SVN
SVN Book, chapter 7, Advanced topics: Configuration

SVN Book, chapter 7, Advanced topics: Properties

Related tips:

Enable auto-properties and set a custom subversion config file for Eclipse

Sources:

Show hidden files in OS X Finder
Show and hide hidden files in Open/Save dialog in OS X (10.6 and up)