2
votes

I wrote the following pre-commit script for SVN to validate that a user has submitted the minimum amount of information on a file commit. However, when trying to add/delete a directory, now it is failing. I know why it's failing obviously, I just didn't realize the pre-commit script was called on every action. How do I filter for the action being performed? Can I do that with svn log or do I need to use something else?

<?php
define('SVNLOOK', "\"C:\Program Files\CollabNet\Subversion Server\svnlook.exe\"");
define('NOTEXT', 1000);
define('NOPATTERNMATCH', 1001);
define('ERRORPROCESSING', 9999);

$repo_path = $argv[1];
$transaction = $argv[2];
$tracking_regex_pattern = "/\b(?:bug|issue|ter)\s*[#]{0,1}(\d+)\b/i";

exec(SVNLOOK . " log $repo_path -t$transaction", $revisions);

//loop through transaction message line by line for validation
$validation_passed = (bool) false;
$has_text = (bool) false;
foreach($revisions as $change_line){
  $change_line = trim($change_line);
  if (!empty($change_line)){
    $has_text = true;
    if (preg_match($tracking_regex_pattern, $change_line)) {
        $validation_passed = true;
    }
  }
}

if(!$validation_passed){
  switch($has_text) {
    case true:
      throwError(NOPATTERNMATCH, $revisions);
      break;
    case false:
      throwError(NOTEXT, $revisions);      
      break;
    default:
      throwError(ERRORPROCESSING, $revisions);
      break;
  }   
}else{
  exit(0);
}

function throwError($error_code, $revisions){
  $fp = fopen('php://stderr', 'w');
  fwrite($fp, "********** (Error Code: $error_code) **********\n");
  foreach($revisions as $change_line) {
    fwrite($fp, $change_line."\n"); 
  }
  fclose($fp); 
  exit($error_code);  
}
?>

How do I test for what svn command is being exectued in the current transaction? Basically I only want the validation to run on a svn commit and not a copy, mkdir, etc...

Thanks in advance!!!

EDIT: I probably should have clarified with the original post, I am using TortoiseSVN's Create Folder... to do this. Maybe TortoiseSVN is doing some kind of commit operation?

1
Call me crazy, but if it's a pre-commit hook, shouldn't it always be a commit operation ... ?Noon Silk
that was my first thought too... Although, I can see why it would be called on a svn copy too. I'm not in a location I can actually check this however. :(. The doc does say it is ONLY called just before a commit transaction is evaluated to a real revision.Dan McGrath
@silky & Dan - those were my exact thoughts when developing the pre-commit script, but it is in fact happening :(jaywon
hey guys, just to clarify this is the closest description of why post-commit fires "The pre-commit hook is invoked before a Subversion txn is # committed". Since a mkdir command is a transaction like anything else, it fires the pre-commit script.jaywon

1 Answers

2
votes

I don't have a great answer for you, but...

Maybe you can use svnlook changed --copy-info to help a bit. Using that, you can tell if an item was copied from somewhere. If all items are copied, then it may be safe to assume this from an svn copy instruction. Likewise, if you checking the items returned, you should be able to determine if it was from adding or deleting a directory as well (since it will just be a single directory item being added or deleted.)

Hope it helps somewhat...