Skip to content

Instantly share code, notes, and snippets.

@omnicolor
Created February 6, 2012 16:23
Show Gist options
  • Save omnicolor/1753090 to your computer and use it in GitHub Desktop.
Save omnicolor/1753090 to your computer and use it in GitHub Desktop.
SVN pre-commit hook that catches conflict markers
#!/usr/local/bin/php
<?php
/**
* Pre-commit Subversion script.
* @author Omni Adams <[email protected]>
*/
/**
* Path to the awk binary.
*/
define('AWK', '/usr/bin/awk');
/**
* Path to the grep binary.
*/
define('GREP', '/bin/grep');
/**
* Path to svnlook binary.
*/
define('SVNLOOK', '/usr/bin/svnlook');
/**
* Divider to inject into error messages.
*/
define('DIVIDER',
'********************************************************************************');
/**
* Scan changed files for SVN conflict markers.
*
* If any of he changed files contain >>>>>>, =======, or
* <<<<<<, writes an error message to standard error and
* returns true.
* @param string $transaction SVN transaction ID.
* @param string $repository Full path to the repository.
* @return boolean True if there was an error.
*/
function catchSvnArtifacts($transaction, $repository) {
$errors = false;
$changedCommand = SVNLOOK . ' changed -t "'
. $transaction . '" "'
. $repository . '" '
. '| ' . GREP . ' "^[UA]" '
. '| ' . AWK . ' \'{print $2}\'';
$changedOutput = array();
exec($changedCommand, $changedOutput);
foreach ($changedOutput as $file) {
$conflicts = '';
$conflictCommand = SVNLOOK . ' cat -t "'
. $transaction . '" "'
. $repository . '" "' . $file . '" '
. '| ' . GREP . ' -E '
. '"(<<<<<<)|(=======)|(>>>>>>)"';
exec($conflictCommand, $conflicts);
if (array() == $conflicts) {
continue;
}
$message = PHP_EOL . DIVIDER . PHP_EOL . PHP_EOL
. $file . ' contains unresolved Subversion '
. 'conflict markers.' . PHP_EOL;
file_put_contents('php://stderr', $message);
$errors = true;
}
return $errors;
}
$repository = $_SERVER['argv'][1];
$transaction = $_SERVER['argv'][2];
if (catchSvnArtifacts($transaction, $repository)) {
exit(1);
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment