views:

236

answers:

1

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?

+2  A: 

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...

Dan McGrath
Dan, thanks for the suggestion. While not as straight forward a solution as I would like, it worked very well. I just have to check the changed info for 'A' and see that the addition ends in a "/" which is the only indicator that it is a directory and not a file.
jaywon
Glad it helped you out.
Dan McGrath