Skip to content

Instantly share code, notes, and snippets.

@jdennes
Created November 18, 2009 21:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdennes/238300 to your computer and use it in GitHub Desktop.
Save jdennes/238300 to your computer and use it in GitHub Desktop.
pre-revprop-change svn hook in python
# pre-revprop-change.py:
#
# Performs the following:
# - Makes sure that only the log message may be modified
# - Makes sure that the action is either addition or modification, but not deletion
# - Makes sure that an empty log message isn't entered
#
# usage: pre-revprop-change.py -t REPOS REV USERNAME PROPNAME ACTION
# N.B. The new value of the property is passed via standard input
# e.g. in pre-revprop-change.cmd (under Windows)
#
# PATH=C:\Python26;C:\Python26\Scripts
# python.exe {common_hooks_dir}\pre-revprop-change.py %1 %2 %3 %4 %5
import os
import sys
import getopt
try:
my_getopt = getopt.gnu_getopt
except AttributeError:
my_getopt = getopt.getopt
import re
import svn
import svn.fs
import svn.repos
import svn.core
def check_prop_name(prop):
if prop != 'svn:log':
sys.stderr.write("\nYou may only edit the log message of a revision.")
return False
return True
def check_action(action):
if action == 'D':
sys.stderr.write("\nYou may only add or modify the log message of a revision, but not delete the log message.")
return False
return True
def check_log_message():
msg = sys.stdin.read()
if msg == None or msg.strip() == "":
sys.stderr.write("\nYou have engaged in the obscene act of attempting to enter an empty log message.\n\nPlease make another attempt, preferably including the Jira issue number in the log message.")
return False
return True
def usage_and_exit(error_msg=None):
import os.path
stream = error_msg and sys.stderr or sys.stdout
if error_msg:
stream.write("ERROR: %s\n\n" % error_msg)
stream.write("USAGE: %s REPOS REV USERNAME PROPNAME ACTION\n"
% (os.path.basename(sys.argv[0])))
sys.exit(error_msg and 1 or 0)
def main(ignored_pool, argv):
repos_path = None
revision = None
username = None
prop_name = None
action = None
try:
opts, args = my_getopt(argv[1:], 't:h?', ["help"])
except:
usage_and_exit("problem processing arguments / options.")
for opt, value in opts:
if opt == '--help' or opt == '-h' or opt == '-?':
usage_and_exit()
elif opt == '-t':
txn_name = value
else:
usage_and_exit("unknown option '%s'." % opt)
if len(args) > 5:
usage_and_exit("maximum of five arguments allowed.")
repos_path = svn.core.svn_path_canonicalize(args[0])
revision = args[1]
username = args[2]
prop_name = args[3]
action = args[4]
fs = svn.repos.svn_repos_fs(svn.repos.svn_repos_open(repos_path))
if check_prop_name(prop_name) and check_action(action) and check_log_message():
sys.exit(0)
else:
sys.exit(1)
if __name__ == '__main__':
sys.exit(svn.core.run_app(main, sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment