Adding SVN Information to File Headers Revisited

You may already be familiar with my previous post on Adding SVN Information to File Headers. After further review, I have decided to update the content to be more explicit. I want to better define how to establish the keywords and their usage in subversion repositories.

If you want to apply a standardized header to the files you are committing to your svn repository, you must first define the keyword list that you want to use. For this explanation and ease of use, I will use the same set of keywords I used in the previous post. Here is the sample header you can add to the top of your file. I use this specifically with php.

/* SVN FILE: $Id$ */
/**
* Project Name :  Project Description
*
* @package        className
* @subpackage     subclassName
* @author         $Author$
* @copyright      $Copyright$
* @version        $Rev$
* @lastrevision   $Date$
* @license        $License$
* @filesource     $URL$
*/

It’s important to note two things. First, the keyword is case sensitive. Second, the keyword is enclosed by to dollar signs. This tells svn that it is in fact the keyword it is looking for to replace.

There are two ways to establish the keyword list with svn. I will first describe the simplest method for establishing the svn keyword set. It is to set the keyword properties in your ~/.subversion/config file. To do that, follow these steps:

  1. Open a terminal screen and make sure you are in your home directory: cd ~/
  2. Open the .subversion/config file for editing: vi ~/.subversion/config
  3. Find the [miscellany] section of the file by using the command: /[misc and hit Enter
  4. Down arrow to enable-auto-props and make sure it is set as: enable-auto-props = yes
  5. Next, find the [auto-props] secction of the file by using the command: /[auto and hit Enter
  6. Scroll to the bottom of the section (most likely the end of the file) and add: *.php = svn:keywords:Date Author Id Copyright Revision License URL

The keywords for subversion have been established on your machine for your user. This is not a global setting that is applied to the repository itself, only to your box and the user name you are running the updates from. Also, if you prefer to set the keywords specifically for only certain files, you can do so from withing the checkedout repository by running:

svn propset svn:keywords “list of keywords here” name_of_file.ext

The name_of_file.ext is the file within the repository you want to establish the list of keywords for.

Now that we have the keywords established, we need to understand that svn only recognizes some of the keywords by default. You can find the list at the subversion site. The list is as follows:

  • Date (may also be specified as LastChangeDate)
  • Revision (may also be specified as LastChangedRevision or Rev)
  • Author (may also be specified as LastChangedBy)
  • HeadURL (may be abbreviated as URL)
  • ID

The other 4 keywords for use with svn will need to be set differently. Since the className and subClass name will not change for the given file, you can just manually add that once and it will stay in the actual file. There is no need to update it.

The other two keywords need to be defined using the propset command on the subversion command line. This will define the values for the keywords for all files within your checked out version. Test set the keyword value for copyright, run the following command from the root directory of your checked out repository:

svn propset Copyright “(c)2009 My Company Name. All Rights Reserved” ./*

I want to bring your attention to two things. First, you will need to change the information between the quotes to make sure the copyright reads the way you want it to. Second, I want you to notice the ./* on the end. This basically tells subversion that this will apply to all of the svn specific files for the repository you are in.

The next keyword is set in a similar fashion. The only difference is, you define the license by pointing to a specific file within the repository. So if your license file is in the main subversion directory, you will run the following command:

svn propset License -F ./path/to/license ./*

Again, make sure the ./path/to/license is an actual path and file name from within the repository. Also note the same usage of the directory application setting of ./* to which this keyword will be applied.

A word of warning. The keyword fields are measured in bytes, so there is some risk of corruption if using multi-byte characters. Be sure to read up on the subversion docs if you have any concerns as this is outside the scope of this topic.

That should do it. You now have what it takes to set the headers in all of your repository files. If you use this, please feel free to post your comments and/or feedback.

Like it? Post to your favorite location and share.
  • Digg
  • del.icio.us
  • Facebook
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Twitter

8 Comments

surbhi  on November 13th, 2009

I could not make custom svn keyword substitution for Copyright” and “License”. Could you please help me?
I am using svn, version 1.5.0 (r31699)
Do we need to do something special in order to make custom keyword substitution to work?

Chuck Burgess  on January 14th, 2010

Will you indicate what you have done so far? There are a few step required in order to get the substitution to work.

Chuck Burgess  on January 19th, 2010

One thing you can try is to update the global settings of subversion: /etc/subversion/config with the settings indicated above. Then be sure that you use recursive if you have files in directories below the directory you are running the propset command on. It will look like this:

svn propset svn:keywords “{KEYWORDS}” ./* -R

Please let me know if you have had any success. Good luck.

alan75  on June 8th, 2010

Hi Chuck
Thanks for so detailed description, but it does not work for me completely. I mean substitution for custom keyword “Copyright”. SVN updates my source file with “system” information (e.g. Author, Rev, ID), but ignores custom (e.g. Copyright, owner).
I am pretty sure that all required steps were done well because I can check it with “propget” command:
>svn propget svn:keywords file.java
Date Author Id Rev URL Copyright owner

>svn propget Copyright file.java
MyCompany

Result of modification header for my file:
/**
* Class javadoc.
* @author $Author: alan75 $
* @copyright $Copyright$
*/

As you can see SVN inserted my nick as author, but ignored Copyright at all.

I will really appreciate you for any comments and ideas why substitution in header may not work properly with “custom” keywords…

alan75  on June 8th, 2010

I hope there is some hint for enforce SVN substitute custom property (just I really need it for my project), but in svnbook I see following information:
Subversion defines the list of keywords available for substitution. That list contains the following five keywords, some of which have aliases that you can also use:…

“five keywords” – only five… hmmm

Chuck Burgess  on June 8th, 2010

Yes. There are only five main keywords it automatically lists for substitution when enabled. However, you can define custom substitutions.

I have been able to get this working for my repositories. Even using Google Code. Have you tried submitting and then checking the code out again (or doing an update) to see if the variables are modified?

alan75  on June 9th, 2010

Yes, I did both operation (commit and update). Changes for “standard” svn keywords are in place after “commit”. I did tests with 2 svn repositories:
- Collab.net (homeland for svn)
- xp-dev.com (free)

Do you remember which svn repository and client did you use exactly?

Chuck Burgess  on June 9th, 2010

I use Cornerstone for Mac and I post to the Google Code repository.

Leave a Comment

You must be to post a comment.