Skip to content

Commit

Permalink
* added multiple vcs support.
Browse files Browse the repository at this point in the history
* now works with subversion.
* fixed some strict coding warnings.
  • Loading branch information
larukedi committed Mar 29, 2013
1 parent cae7ce1 commit 54cdcb6
Show file tree
Hide file tree
Showing 18 changed files with 334 additions and 57 deletions.
1 change: 1 addition & 0 deletions git-hooks/pre-commit
Expand Up @@ -28,5 +28,6 @@ if ($exitCode != 0 || !file_exists("$bootstrapFile")) {
}

$GLOBALS['dir'] = realpath(__DIR__ ."/../../");
$GLOBALS['vcs'] = 'git';
$GLOBALS['hookType'] = "pre-commit";
include_once($bootstrapFile);
1 change: 1 addition & 0 deletions git-hooks/pre-receive
Expand Up @@ -31,6 +31,7 @@ if ($exitCode != 0 || !file_exists("$bootstrapFile")) {
list($rev1, $rev2, $ref) = array_map("trim", explode(' ', file_get_contents('php://stdin')));

$GLOBALS['dir'] = realpath(__DIR__ ."/../../");
$GLOBALS['vcs'] = 'git';
$GLOBALS['hookType'] = "pre-receive";
$GLOBALS['rev1'] = $rev1;
$GLOBALS['rev2'] = $rev2;
Expand Down
20 changes: 20 additions & 0 deletions src/Exceptions/NotImplemented.php
@@ -0,0 +1,20 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Muhafiz\Exceptions;

class NotImplemented extends \Exception
{
}
41 changes: 28 additions & 13 deletions src/Muhafiz.php
Expand Up @@ -16,35 +16,50 @@

namespace Muhafiz;
use Muhafiz\Utils\System as Sys;
use Muhafiz\Utils\Git as Git;
use Muhafiz\Vcs\Git as Git;
use Muhafiz\Vcs\Subversion as Subversion;
use Muhafiz\Runners as Runners;


/**
* Muhafiz, code guard!
*
* Check codebase for given coding standards
* and prevent git commits if necessary
*/
* Check codebase for given coding standards
* and prevent vcs commits if necessary
*/
class Muhafiz
{
private $_vcs;

/**
* Init Muhafiz and run required methods by required parameters
* @param array $configParams $GLOBALS passed from git hooks
* @param array $configParams $GLOBALS passed from vcs hooks
*/
public function init($configParams)
{
switch ($configParams['vcs']) {
case 'subversion':
$repository = $_SERVER['argv'][1];
$txn = $_SERVER['argv'][2];

$this->_vcs = new Subversion($configParams, $repository, $txn);
break;
case 'git':
$this->_vcs = new Git($configParams);
break;
}

if ($configParams['hookType'] == "pre-commit") {

chdir($configParams['dir']); //change current directory to git repository
$stagedFiles = Git::getStagedFiles();
chdir($configParams['dir']); //change current directory to vcs repository
$stagedFiles = $this->_vcs->getStagedFiles();
$files = $stagedFiles['output'];
$this->run($files);

} elseif ($configParams['hookType'] == "pre-receive") {
try {
$this->_checkIfPushDisabled($configParams['ref']);
$files = Git::getFilesAfterCommit($configParams['rev1'], $configParams['rev2']);
$files = $this->_vcs->getFilesAfterCommit($configParams['rev1'], $configParams['rev2']);
$this->run($files);
}
catch(Exception $e){
Expand All @@ -62,12 +77,12 @@ public function init($configParams)
/**
* Check if pushing to branch is disabled
*
* @param string $ref git ref name
* @param string $ref vcs ref name
*/
private function _checkIfPushDisabled($ref)
{
$branchName = str_replace("refs/heads/", "", $ref);
if ($config = Git::getConfig("muhafiz.disabled-branches", "")) {
if ($config = $this->_vcs->getConfig("muhafiz.disabled-branches", "")) {
$branches = array_map("trim", explode(" ", $config));
foreach ($branches as $branch) {
if ($branch == $branchName) {
Expand All @@ -79,22 +94,22 @@ private function _checkIfPushDisabled($ref)


/**
* check code by runners specified in 'muhafiz.active-runners' git config
* check code by runners specified in 'muhafiz.active-runners' vcs config
*
* @param array $files list of files to be checked
*/
public function run($files)
{
$activeRunnersList = "php, phpcs, jshint, lineend, bom, forbiddenfile";
$activeRunnersConfig = Git::getConfig("muhafiz.active-runners", $activeRunnersList);
$activeRunnersConfig = $this->_vcs->getConfig("muhafiz.active-runners", $activeRunnersList);
$activeRunners = explode(",", $activeRunnersConfig);

foreach ($activeRunners as $activeRunner) {
$activeRunner = str_replace('-', '', trim($activeRunner));
$className = "Muhafiz\\Runners\\" . ucfirst($activeRunner);

if (class_exists($className) && is_subclass_of($className, "\\Muhafiz\\Runners\\RunnersAbstract")) {
$runner = new $className();
$runner = new $className($this->_vcs);
echo "running $activeRunner ... ";
$runner->init($files);
echo "DONE \n";
Expand Down
2 changes: 1 addition & 1 deletion src/Runners/Bom.php
Expand Up @@ -32,7 +32,7 @@ function run(array $files)
foreach ($files as $file) {
//cat file with --show-nonprinting option
//and see if it contains BOM
$out = Sys::runCommand("cat --show-nonprinting ${file} | grep -iq '^M-oM-;M-?'");
$out = Sys::runCommand($this->_vcs->catCommand($file) . " | cat --show-nonprinting | grep -iq '^M-oM-;M-?'");

if ($out['exitCode'] == 0) { // it means we have BOM in this file
$this->_onRuleFailed($out);
Expand Down
3 changes: 1 addition & 2 deletions src/Runners/Consolefoo.php
Expand Up @@ -16,7 +16,6 @@

namespace Muhafiz\Runners;
use Muhafiz\Utils\System as Sys;
use Muhafiz\Utils\Git as Git;
use Muhafiz\Runners\RunnersAbstract as RunnersAbstract;

/**
Expand All @@ -34,7 +33,7 @@ function run(array $files)
foreach ($files as $file) {
//check if files have console.*() statements
//but dont check files which are commented with //
$out = Sys::runCommand("grep -vE '^\s*\/\/' ${file} | grep -iqE 'console\.[a-zA-Z]{1,20}\s*\(' 2>&1");
$out = Sys::runCommand($this->_vcs->catCommand($file) . " | grep -vE '^\s*\/\/' | grep -iqE 'console\.[a-zA-Z]{1,20}\s*\(' 2>&1");

if ($out['exitCode'] == 0) {
$out['output'][] = "'${file}' file contains console.*() statement";
Expand Down
5 changes: 2 additions & 3 deletions src/Runners/Jshint.php
Expand Up @@ -15,7 +15,6 @@

namespace Muhafiz\Runners;
use Muhafiz\Utils\System as Sys;
use Muhafiz\Utils\Git as Git;
use Muhafiz\Runners\RunnersAbstract as RunnersAbstract;


Expand All @@ -32,10 +31,10 @@ class Jshint extends RunnersAbstract
function run(array $files)
{
//get required config params
$configFile = Git::getConfig("muhafiz.runners.jshint.config", ".jshintrc");
$configFile = $this->_vcs->getConfig("muhafiz.runners.jshint.config", ".jshintrc");

foreach ($files as $file) {
$out = Sys::runCommand("jshint ${file} --config=${configFile}");
$out = Sys::runCommand($this->_vcs->catCommand($file) . " | jshint --config=${configFile}");

if ($out['exitCode'] != 0) {
$this->_onRuleFailed($out);
Expand Down
6 changes: 3 additions & 3 deletions src/Runners/Lineend.php
Expand Up @@ -15,7 +15,6 @@

namespace Muhafiz\Runners;
use Muhafiz\Utils\System as Sys;
use Muhafiz\Utils\Git as Git;
use Muhafiz\Runners\RunnersAbstract as RunnersAbstract;

/**
Expand All @@ -33,10 +32,11 @@ function run(array $files)

//get required config params
//TODO: check for valid parameters
$allowedLineEnd = Git::getConfig("muhafiz.runners.lineend.allowed", "unix");
$allowedLineEnd = $this->_vcs->getConfig("muhafiz.runners.lineend.allowed", "unix");

foreach ($files as $file) {
$out = Sys::runCommand("echo `cat -e ${file} | wc -l`,`cat -e ${file} | grep '\^M\$$' | wc -l`");
$cmd = $this->_vcs->catCommand($file);
$out = Sys::runCommand("echo `" . $cmd . " | cat -e | wc -l`,`" . $cmd . " | cat -e | grep '\^M\$$' | wc -l`");
list($total, $windows) = explode(",", $out['output'][0]);

$unix = $total - $windows;
Expand Down
3 changes: 1 addition & 2 deletions src/Runners/Php.php
Expand Up @@ -16,7 +16,6 @@

namespace Muhafiz\Runners;
use Muhafiz\Utils\System as Sys;
use Muhafiz\Utils\Git as Git;
use Muhafiz\Runners\RunnersAbstract as RunnersAbstract;

/**
Expand All @@ -33,7 +32,7 @@ function run(array $files)
{
foreach ($files as $file) {
//force php to display_errors and run php linter, also redirect stderr to stdout
$out = Sys::runCommand("php -l ${file} -d display_errors=1 2>&1");
$out = Sys::runCommand($this->_vcs->catCommand($file) . " | php -l -d display_errors=1 2>&1");

if ($out['exitCode'] != 0) {
$this->_onRuleFailed($out);
Expand Down
7 changes: 3 additions & 4 deletions src/Runners/Phpcs.php
Expand Up @@ -16,7 +16,6 @@

namespace Muhafiz\Runners;
use Muhafiz\Utils\System as Sys;
use Muhafiz\Utils\Git as Git;
use Muhafiz\Runners\RunnersAbstract as RunnersAbstract;


Expand All @@ -33,11 +32,11 @@ class Phpcs extends RunnersAbstract
function run(array $files)
{
//get required config params
$standard = Git::getConfig("muhafiz.runners.phpcs.standard", "PEAR");
$report = Git::getConfig("muhafiz.runners.phpcs.report", "emacs");
$standard = $this->_vcs->getConfig("muhafiz.runners.phpcs.standard", "PEAR");
$report = $this->_vcs->getConfig("muhafiz.runners.phpcs.report", "emacs");

foreach ($files as $file) {
$out = Sys::runCommand("phpcs ${file} --standard=${standard} --report=${report}");
$out = Sys::runCommand($this->_vcs->catCommand($file) . " | phpcs --standard=${standard} --report=${report}");

if ($out['exitCode'] != 0) {
$this->_onRuleFailed($out);
Expand Down
5 changes: 2 additions & 3 deletions src/Runners/Phpcsfixer.php
Expand Up @@ -16,7 +16,6 @@

namespace Muhafiz\Runners;
use Muhafiz\Utils\System as Sys;
use Muhafiz\Utils\Git as Git;
use Muhafiz\Runners\RunnersAbstract as RunnersAbstract;


Expand All @@ -33,10 +32,10 @@ class Phpcsfixer extends RunnersAbstract
public function run(array $files)
{
//get required config params
$standard = Git::getConfig("muhafiz.runners.php-cs-fixer.standard", "psr2");
$standard = $this->_vcs->getConfig("muhafiz.runners.php-cs-fixer.standard", "psr2");

foreach ($files as $file) {
$out = Sys::runCommand("php-cs-fixer fix --dry-run --level=${standard} ${file}");
$out = Sys::runCommand($this->_vcs->catCommand($file) . " | php-cs-fixer fix --dry-run --level=${standard}");

This comment has been minimized.

Copy link
@volkan

volkan May 9, 2013

Contributor

Bu satırı ekleyerek php-cs-fixer'i çalışmaz hale getirmişsiniz. Projede testlerin yazılması elzem bir durum olmuş. Bilginize.

This comment has been minimized.

Copy link
@eser

eser May 16, 2013

Contributor

Bununla ilgili bir issue acilmis durumda, yapisal bir degisiklik gerekiyor gibi Git'de oldugu gibi local erisemiyorsunuz Subversion'da eger dosya henuz commit edilmediyse. Bir oneriniz varsa uygulayabiliriz.

#27


if (count($out['output']) > 0) {
$this->_onRuleFailed($out);
Expand Down
11 changes: 7 additions & 4 deletions src/Runners/RunnersAbstract.php
Expand Up @@ -16,21 +16,23 @@
namespace Muhafiz\Runners;

use Muhafiz\Utils\System as Sys;
use Muhafiz\Utils\Git as Git;

/**
* All runners should have 'run()' method for given files array
*/
abstract class RunnersAbstract
{
private $_excludePattern;
protected $_vcs;

/**
* If any other tool needed for this runner
* check availilibility of this by executing $_toolCheckCommand
*/
public final function __construct()
public final function __construct($vcs)
{
$this->_vcs = $vcs;

if ($this->_toolCheckCommand) {
$cmdOut = Sys::runCommand($this->_toolCheckCommand);
if ($cmdOut['exitCode'] != 0) {
Expand Down Expand Up @@ -75,8 +77,9 @@ protected function _onRuleFailed(array $out)
*/
private function _filterExcludePaths($files)
{
$runnerName = strtolower(end(explode('\\', get_called_class())));
$excludePattern = Git::getConfig("muhafiz.runners." . $runnerName .".exclude-pattern", "");
$className = explode('\\', get_called_class());
$runnerName = strtolower(end($className));
$excludePattern = $this->_vcs->getConfig("muhafiz.runners." . $runnerName .".exclude-pattern", "");

if ($this->_excludePattern = $excludePattern) {
$files = array_filter($files, array($this, "_excludeByRegexp"));
Expand Down
3 changes: 1 addition & 2 deletions src/Runners/Vardump.php
Expand Up @@ -16,7 +16,6 @@

namespace Muhafiz\Runners;
use Muhafiz\Utils\System as Sys;
use Muhafiz\Utils\Git as Git;
use Muhafiz\Runners\RunnersAbstract as RunnersAbstract;

/**
Expand All @@ -34,7 +33,7 @@ function run(array $files)
foreach ($files as $file) {
//check if files have var_dump or print_r statement
//but dont check files which are commented with //
$out = Sys::runCommand("grep -vE '^\s*\/\/' ${file} | grep -iqE 'var_dump|print_r' 2>&1");
$out = Sys::runCommand($this->_vcs->catCommand($file) . " | grep -vE '^\s*\/\/' | grep -iqE 'var_dump|print_r' 2>&1");

if ($out['exitCode'] == 0) {
$out['output'][] = "'${file}' file contains var_dump or print_r statement";
Expand Down

0 comments on commit 54cdcb6

Please sign in to comment.