Skip to content

Add PHP syntax check #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PreCommitManager.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function processChecks(){
try {
require_once $this->getCheckDirectory().DIRECTORY_SEPARATOR.$checkName.'Check.class.php';
$className = $checkName.'Check';
$check = new $className($mess);
$check = new $className($mess, $this->repoName, $this->trxNum);
$check->runCheck($fileChanges);
if ($check->fail()){
$this->checksWithError[] = $check;
Expand Down
6 changes: 5 additions & 1 deletion checks/BasePreCommitCheck.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ abstract class BasePreCommitCheck {
protected $globalError = array();
protected $codeError = array();
protected $options = array();
protected $repoName = null;
protected $trxNum = null;

public function __construct($svnComment=''){
public function __construct($svnComment='', $repoName = null, $trxNum = null){
$this->svnComment = $svnComment;
$this->repoName = $repoName;
$this->trxNum = $trxNum;
$this->parseOptions();
}

Expand Down
55 changes: 55 additions & 0 deletions checks/SyntaxCheck.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'BasePreCommitCheck.class.php';

class SyntaxCheck extends BasePreCommitCheck
{
static private $has_php_binary = null;

public function getTitle()
{
return "Reject files with syntax error";
}

public function renderErrorSummary()
{
return "Syntax error found";
}

public function checkFullFile($lines, $filename)
{
if ( $this->hasOption('no-syntax') ){
return;
}

$this->hasPhpBinary();

if ($this->getExtension($filename) !== 'php') {
return;
}

passthru('svnlook cat '.escapeshellarg($this->repoName).' '.escapeshellarg($filename).' -t '.escapeshellarg($this->trxNum).' | php -l', $return);

if ($return !== 0) {
return 'Syntax error in '.$filename;
}
}


private function hasPhpBinary()
{
if (self::$has_php_binary === null) {
passthru('php --version', $return);

if ($return !== 0) {
throw new Exception('Impossible to find PHP binary');
}

self::$has_php_binary = true;
}
}

public function renderInstructions()
{
return "If you want to force commit with error syntax, add the parameter --no-syntax in your comment";
}
}
2 changes: 1 addition & 1 deletion pre-commit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
# * --exculde=XX:ZZZ To list the checks that must be ignore for that repo
#

php /{PATH_TO_PHP_SVN_HOOKS_DIR}/svn_pre_commit_hook.php $1 $2 --include=EmptyComment:NoTabs
php /home/svn/hooks/php-svn-hook/svn_pre_commit_hook.php $1 $2 --include=EmptyComment:Syntax