Oct 28, 2009

Default settings of svn:keywords property

Keyword expansion is not the default for files added to an SVN repository.  And it’s done on a per file basis (with CVS we could configure an entire repository to turn on keyword expansion — .)  Some svn tools and plugins allow for defaulting properties when adding new files.  I’ve configured the two I use most often (eclipse and smartsvn) to the svn:keywords property for all new files:

svn:keywords : Date Revision Author URL Id

You can see a file’s properties from the command line with

svn pl -v filename

Some tool (like netbeans) don’t provide for defaulting properties when adding new files.  And there are probably some people (not gonna name names :^) who don’t configure their tools to default the svn:keywords property anyway.  So below is a little script that’ll do it for you.  The script is in ruby ’cause Brian (who first sketched it out a few years ago for a project) wanted to do some ruby scripting.

You can run it from time to time from the command line or better yet, set up a cron job on the build server to do it once or twice a day.  Our cron job does something like this:

cd ~/tmp/scratch-dir
svn checkout https://opi.svn.cvsdude.com/xyzzy
add_svn_keywords.rb
svn commit -m "Added svn:keywords property"

It’s up to you if you want to do a full checkout each time (an update would be faster.)

Here’s the script…

#!/usr/bin/ruby
#
# Adds svn:keywords property to source files.  Default to process the
# current directory.  Note: this does not commit the changes!
#
# Usage:
#   ruby add_svn_keywords.rb [src_dir]
#
# where
#   src_dir (Optional) The directory to process.
#
# for example
#   ruby add_svn_keywords.rb c:/mnscu/hr/code
#
require 'find'
require 'fileutils'
include FileUtils::Verbose
require 'ftools'
require 'optparse'

# -- functions --------------------------------------------------------------

def do_system(command)
    puts command
    system(command) or raise($?)
end

# -- vars -------------------------------------------------------------------

working_dir = "."
keywords = "Date Revision Author URL Id"
usage = "Usage: ruby add_svn_keywords.rb [src_dir]"

# -- options and svn checkout/update ----------------------------------------

opts = OptionParser.new
opts.parse(ARGV) rescue usage

unless ARGV[0].nil?
    working_dir = ARGV[0]
end

# -- svn properties ---------------------------------------------------------

num = 0
Find.find(working_dir) do |path|

    if FileTest.directory?(path)
        if File.basename(path) == '.svn'
            # allways skip the .svn dirs
            Find.prune
        else
            # don't bother setting properties on dirs
            next
        end
    else # must be a file...
        # quote spaces on paths that contain them (so svn wont fail)
        path.gsub!(/ /, ' ')

        # add keywords property to some files...
        if File.file?(path) and path =~ /.(ddl|bat|sh|css|dtd|el|groovy|gsp|html|java|js|jsp|properties|sql|txt|xml)$/
            keywordprop = `svn proplist --verbose #{path}`.to_a.find_all {|line| line.include? keywords  }
            if keywordprop.size == 0
                do_system("svn propset svn:keywords "#{keywords}" #{path}")
                num += 1
            end
        end
    end
end # find

# -- commit and cleanup -----------------------------------------------------

if num > 0
    #do_system("svn commit -m "Added svn:keywords property" #{working_dir}");
    puts "Added svn:keywords property to: #{num} files"
else
    puts "No changes"
end

# -- end of file ------------------------------------------------------------

About the Author

Object Partners profile.
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Feature Flags in Terraform
Feature flagging any code can be useful to developers but many don’t know how to or even that you can do it in Terraform. Some benefits of Feature Flagging your code You can enable different […]
Infrastructure as Code – The Wrong Way
You are probably familiar with the term “infrastructure as code”. It’s a great concept, and it’s gaining steam in the industry. Unfortunately, just as we had a lot to learn about how to write clean […]
Snowflake CI/CD using Jenkins and Schemachange
CI/CD and Management of Data Warehouses can be a serious challenge. In this blog you will learn how to setup CI/CD for Snowflake using Schemachange, Github, and Jenkins. For access to the code check out […]
How to get your pull requests approved more quickly
TL;DR The fewer reviews necessary, the quicker your PR gets approved. Code reviews serve an essential function on any software codebase. Done right, they help ensure correctness, reliability, and maintainability of code. On many teams, […]