Thursday, October 22, 2009

How to add version info to subversion files automatically

Adding version info to subversion files
You can configure Subversion and TortoiseSVN to update the version information on the files checked in to subversion by adding subversion keywords to your file and setting the subversion properties. To automatically add the version number, the user who changed the file and the date in which the file was changed, follow these steps:

Add the subversion keywords to the file. These properties and keywords are case sensitive. To ensure that you are using the correct value of the keyword, I suggest copy the following text to your file and make the changes necessary to make it a valid comment:
/**
**  Last Changed by    : $LastChangedBy$
**  Last Changed Rev   : $Rev$
**  Last Changed Date  : $LastChangedDate$
**/


The next step is to add the subversion properties to the file. It can be done by one file at a time or at a folder level to add the properties to all the files in the folder and its sub folders. To do this, right click on the file/folder, select TortoiseSVN->Properties. Click “New” and in the properties window select “svn:keywords” and enter “LastChangedBy Rev LastChangedDate” as the value as shown below:

To avoid making errors, I would suggest that you use the import/export functionality to add these properties. Select a file that already has this property and you know it is working and click on export as shown below. Save the property to a file and close the properties window. Now go to the TortoiseSVN properties window of the new file and select import and select the properties file you saved before. This will ensure that you have set the properties correctly for that file.


Subversion automatic property setting
You can configure Subversion and TortoiseSVN to set properties automatically on files and folders when they are added to the repository. There are two ways of doing this.
You can edit the subversion configuration file to enable this feature on your client. The General page of TortoiseSVN's settings dialog has an edit button to take you there directly. (Right click anywhere within the right pane of windows explorer, Highlight the TortoiseSVN menu and click on the settings tab to bring up the General settings page of Tortoise SVN )
The config file is a simple text file which controls some of subversion's workings. You need to change two things: First, in the section headed miscellany uncomment the line enable-auto-props = yes. Secondly you need to edit the section below to define which properties you want added to which file types. This method is a standard subversion feature and works with any subversion client. However it has to be defined on each client individually - there is no way to propagate these settings from the repository. Table 1 shows a sample snippet of the config file with the changes that adds the properties for the file types .txt, .java, .jsp, .htm*, *.properties, *.xml and *.xml. You can add more lines as needed to add more file types.
An alternative method is to set the tsvn:autoprops property on folders. I recommend that we use this method as all of us are using TortoiseSVN client. This method only works for TortoiseSVN clients, but it does get propagated to all working copies on update. The advantage is if even if one user did not have the correct settings on their PC, these properties will get propagated as long as the top folder was created from a pc with the right setting. To do this right click on a top level folder for the project, select tortisSVN properties and select add property and select the tsvn:autoprops property name and add the values as shown below:


Whichever method you choose, you should note that auto-props are only applied to files at the time they are added to the repository. Auto-props will never change the properties of files which are already versioned. If you want to add the properties to files that are already committed to subversion, you may want to select the top level folder and add the subversion property.

Table 1: Partial Text of the Subversion config file
### Section for configuring miscelleneous Subversion options.
[miscellany]
.
.
.
### Automatic properties are defined in the section 'auto-props'.
enable-auto-props = yes
### Set interactive-conflicts to 'no' to disable interactive
### conflict resolution prompting.  It defaults to 'yes'.
# interactive-conflicts = no

### Section for configuring automatic properties.
[auto-props]
### The format of the entries is:
###   file-name-pattern = propname[=value][;propname[=value]...]
### The file-name-pattern can contain wildcards (such as '*' and
### '?').  All entries which match (case-insensitively) will be
### applied to the file.  Note that auto-props functionality
### must be enabled, which is typically done by setting the
### 'enable-auto-props' option.
*.txt = svn:eol-style=native;svn:keywords=LastChangedBy Rev LastChangedDate
*.java = svn:eol-style=native;svn:keywords=LastChangedBy Rev LastChangedDate
*.jsp = svn:eol-style=native;svn:keywords=LastChangedBy Rev LastChangedDate
*.htm* = svn:eol-style=native;svn:keywords=LastChangedBy Rev LastChangedDate
*.properties* = svn:eol-style=native;svn:keywords=LastChangedBy Rev LastChangedDate
*.xml = svn:eol-style=native;svn:keywords=LastChangedBy Rev LastChangedDate
*.sql = svn:eol-style=native;svn:keywords=LastChangedBy Rev LastChangedDate
# *.png = svn:mime-type=image/png
# *.jpg = svn:mime-type=image/jpeg
# Makefile = svn:eol-style=native

No comments: