Many Drupal developers use Subversion (SVN) internally within their organization, due to its many benefits over what the Drupal project's repository uses: the dated CVS.

One annoying aspect though is that almost all Drupal files use CVS style tags, such as the $Id$ one. Subversion does not recognize these by default.

However, there is a way to make Subversion recognize these tags. This post describes the steps needed to achieve this goal.

1. Edit your subversion configuration file

Depending on your distribution, subversion's configuration could be in /etc/subversion/config.

2. Set auto properties

Under the [miscellany] section, add the following line:

[miscellany]
enable-auto-props = yes 

3. Configure auto properties

We now list the Drupal file types, and enable Id and Revision tags on them.

[auto-props]
# enable keywords for Drupal files
*.inc = svn:keywords=Revision Id
*.install = svn:keywords=Revision Id
*.info = svn:keywords=Revision Id
*.module = svn:keywords=Revision Id
*.theme = svn:keywords=Revision Id
*.php = svn:keywords=Revision Id
# CSS and JS
*.css = svn:keywords=Revision Id
*.js = svn:keywords=Revision Id
# C files
*.c = svn:keywords=Revision Id
*.h = svn:keywords=Revision Id
# Scripts
*.pl = svn:keywords=Revision Id
*.sh = svn:keywords=Revision Id
*.txt = svn:keywords=Revision Id 

Wait! This only works for newly added files. What about files that are already in the repository? For these, we have to do the next step.

4. Change properties for existing files:

A command like the following enables the named properties to be replaced by SVN with the relevant info. Note that Rev is an alias for Revision.

svn propset svn:keywords "Id Rev" some_file.module

But we need this on all files in the repository, so you need to follow these steps:

# Change to your sandbox's main directory 
cd /your/sandbox
# Make sure that everything is up to date
svn up
# Get a list of all Drupal files
find . -type f | grep -v '/\.svn/' | egrep "\.(inc|install|info|module|theme|php|txt|sh)$" > list
# Add svn:keywords properties for all files
for FILE in `cat list`
do
svn propset svn:keywords "Id Rev" $FILE
done
# Commit the change
svn commit -m "Adding Id and Rev property to all files" 

Now, all the files will have the Id and Revision tag replaced, and you can trace what your client/site is using, vs. what is in the repository.

Enjoy ...

Comments

Mon, 2009/01/05 - 03:55

What does adding this achieve apart from version stamping the top of the file?

Surely if you wanted the version number you could use "svn info" which would give you far more useful information than a revision number...?

This is, however, a very useful tutorial! Thanks!

Mon, 2009/01/05 - 10:14

Many benefits. Think about files that you hand over to clients and they no longer sync with your repository.

One scenario, for example, if you enable only $Revision$, but not $Id$, then $Id$ will have the cvs.drupal.org that you checked it out from, and Revision will have the SVN version of your local repository.

Another example is that when you give the files to a client, and they report a problem later, you will know what version they are using at a glance, then you can use svn info for more digging, and even svn diff to see if they did modify it or not.

Wed, 2009/01/07 - 06:54

Good tip - you can also achieve the same end in one line. I often use the following in PHP projects:
find . \( -name "*.php" -o -name "*.js" \) -exec svn propset svn:keywords Id {} \; && commit -m "Added svn id keyword"

A slightly more complicated selection clause in the find command is needed for your exact example.

Fri, 2009/07/31 - 12:24

The above article is applicable if you have a repository that you manage yourself. The advantage is that you do the above only once, and then it applies to every file and every checkin/checkout in the future.

If you do not have administration rights on the repository, you can still do that but you have to change your SVN client configuration as per the instructions here.

Thu, 2010/10/28 - 05:14

Thank you once again for another brilliant 2bits blog post. It seems whenever I want to do something a little bit different, but somehow Drupal-related (in this case mimic Drupal CVS), you've been their before and posted a tutorial! =)

I searched all over the Interwebz for a tutorial on setting properties server-side and this is the only one I could find.

Is your Drupal or Backdrop CMS site slow?
Is it suffering from server resources shortages?
Is it experiencing outages?
Contact us for Drupal or Backdrop CMS Performance Optimization and Tuning Consulting