Skip to content

Instantly share code, notes, and snippets.

@Gisleburt
Last active May 10, 2016 14:53
Show Gist options
  • Save Gisleburt/5d02fcc4fada140e9d4b to your computer and use it in GitHub Desktop.
Save Gisleburt/5d02fcc4fada140e9d4b to your computer and use it in GitHub Desktop.
A pre-commit script for my PHP project for Git
#!/usr/bin/php
<?php
// Based on code by raphaelstolt and buddhamagnet https://gist.github.com/raphaelstolt/588697
class PreCommitHook {
protected $green = "\033[0;32m";
protected $red = "\033[0;31m";
protected $reset = "\033[0m";
protected $binDir = 'bin';
protected function red($string) {
return $this->red . $string . $this->reset;
}
protected function green($string) {
return $this->green . $string . $this->reset;
}
protected function echoLine($string, $endLine = true) {
echo $string . ($endLine ? PHP_EOL : '');
}
public function phpUnit() {
$this->echoLine('Running PHPUnit... ', false);
exec($this->binDir.'/phpunit', $output, $returnCode);
if ($returnCode !== 0) {
$this->echoLine($this->red('Failed'));
$this->echoLine('PhpUnit Output: ');
$this->echoLine(implode(PHP_EOL, $output));
$this->echoLine($this->red('COMMIT FAILED'));
exit(1);
}
$this->echoLine($this->green('Success'));
}
public function phpBehat() {
$this->echoLine('Running Behat... ', false);
exec($this->binDir.'/behat -p daniel', $output, $returnCode);
if ($returnCode !== 0) {
$this->echoLine($this->red('Failed'));
$this->echoLine('Behat Output: ');
$this->echoLine(implode(PHP_EOL, $output));
$this->echoLine($this->red('COMMIT FAILED'));
exit(1);
}
$this->echoLine($this->green('Success'));
}
}
$hook = new PreCommitHook();
$hook->phpUnit();
$hook->phpBehat();
exit(0);
@Gisleburt
Copy link
Author

Update #2:

  • Added credit to original code even though there's now little in common 👅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment