Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

71 lines
1.7 KiB

<?php
class PHPGit_Command
{
/**
* @var string Real filesystem path of the repository
*/
protected $dir;
/**
* @var string Git command to run
*/
protected $commandString;
/**
* @var boolean Whether to enable debug mode or not
* When debug mode is on, commands and their output are displayed
*/
protected $debug;
/**
* Instanciate a new Git command
*
* @param string $dir real filesystem path of the repository
* @param array $options
*/
public function __construct($dir, $commandString, $debug)
{
$commandString = trim($commandString);
$this->dir = $dir;
$this->commandString = $commandString;
$this->debug = $debug;
}
public function run()
{
$commandToRun = sprintf('cd %s && %s', escapeshellarg($this->dir), $this->commandString);
if($this->debug) {
print $commandToRun."\n";
}
ob_start();
passthru($commandToRun, $returnVar);
$output = ob_get_clean();
if($this->debug) {
print $output."\n";
}
if(0 !== $returnVar) {
// Git 1.5.x returns 1 when running "git status"
if(1 === $returnVar && 0 === strncmp($this->commandString, 'git status', 10)) {
// it's ok
}
else {
throw new GitRuntimeException(sprintf(
'Command %s failed with code %s: %s',
$commandToRun,
$returnVar,
$output
), $returnVar);
}
}
return trim($output);
}
}
class GitRuntimeException extends RuntimeException {}