Monday, July 12, 2010

SVN pre-revprop-change hook script, to allow log edits

SVN hook scripts are little scripts which SVN attempts to call automatically during various operations. The svn pre-revprop-change hook script is one that is called when you try to modify a property of a revision. The most common use of this script is to allow the editing of svn log comments.

Hook script templates should already exist, in the /hooks directory inside your repository director. For me, I have several repos in /var/svn. Say I want to set up comment editing for a particular repo named 'svnrepo1'. My hook templates would be in /var/svn/svnrepo1/hooks.

To activate one of the template hooks, simply rename it to match whatever hook it should be with. For example, my pre-revprop-change template was named 'pre-revprop-change.tmpl' - to activate it, I copied (better idea than just moving it) it to 'pre-revprop-change'. Here's the code inside the pre-revprop-change hook:


# 5 ordered arguments are sent to pre-revprop-change:
# [1] path to repository
# [2] the revision being modified
# [3] username of the person editing the revision
# [4] the property being modified
# [5] the action; 'A'dded, 'M'odified, 'D'eleted


# assigning the arguments to named variables
REPO="$1"
REV="$2"
USER="$3"
PROPNAME="$4"
ACTION="$5"


# we only want to allow 'M'odifiying of svn:log.
# No adding or deleting,
# and no changing of other properties
if[ "$ACTION" = "M" -a "$PROPNAME" = "svn:log"]; then exit 0; fi

exit 1



That's all. All the pre-revprop-change hook needs to do, is make sure people aren't doing anything they shouldn't do, and then exit 0 if they're being good little kids, and exit 1 if they're breaking the rules. This script can also be easily modified to only allow the original comment's author to change the comment. To do this, just change the "if" statement to look like this:

if [ "$ACTION = "M" -a "$PROPNAME" = "svn:log" -a `svnlook author /path/to/your/repo -r $REV` = "$USER" ]; then exit 0; fi


This uses the svnlook utility to check if the username associated with the revision ($REV) matches the person editing it ($USER)

Wednesday, June 9, 2010

Multiple Inheritance with PHP

Right now at work, I'm implementing classes in PHP for an Interactive Design Tool which will allow us to select fields from multiple tables and multiple sources, and then automatically generate the CloverETL XML graphs used to execute the data tranformations to create a new Datamart. I came across a case where I wanted one class to inherit methods and members from more than one other class. This type of behaviour is not supported by PHP, but I came across some hack-ish ways to facilitate it.


class b {
public function thisisB($e) {
print "This is B $e\n";
}
}

class c {
public function thisisC($e) {
print "This is C $e";
}
}

class a extends b {
function __construct() {
$this->c = new c();
}

public function __call($method,$args) {
$this->c->$method($args[0]);
}
}

$a = new a();
$a->thisisB("from A");
$a->thisisC("from A");

The output will be:


This is B from A
This is C from A

The __call method will capture any function call that doesn't already have a function definition in the class (or it's inherited classes) and the arguments. From here, we've made a member object of class c inside class a, so we can pass any unregocnized calls onto $this->c, and we'll have access to the methods of class c. It's not perfect, and it's a little ugly, but it works and does what you would expect.