1
0
Fork 0

Merge pull request #487 from mheleniak/fix_git_driver

correct working directory for cli commands, fixes #482
pull/489/merge
Jordi Boggiano 2012-03-22 01:42:44 -07:00
commit b11f72eed7
2 changed files with 15 additions and 16 deletions

View File

@ -35,11 +35,10 @@ class GitDriver extends VcsDriver
$this->repoDir = $this->url; $this->repoDir = $this->url;
} else { } else {
$this->repoDir = sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9]}i', '-', $url) . '/'; $this->repoDir = sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9]}i', '-', $url) . '/';
$repoDir = escapeshellarg($this->repoDir);
if (is_dir($this->repoDir)) { if (is_dir($this->repoDir)) {
$this->process->execute(sprintf('cd %s && git fetch origin', $repoDir), $output); $this->process->execute('git fetch origin', $output, $this->repoDir);
} else { } else {
$this->process->execute(sprintf('git clone %s %s', $url, $repoDir), $output); $this->process->execute(sprintf('git clone %s %s', $url, escapeshellarg($this->repoDir)), $output);
} }
} }
@ -57,7 +56,7 @@ class GitDriver extends VcsDriver
if ($this->isLocal) { if ($this->isLocal) {
// select currently checked out branch if master is not available // select currently checked out branch if master is not available
$this->process->execute(sprintf('cd %s && git branch --no-color', escapeshellarg($this->repoDir)), $output); $this->process->execute('git branch --no-color', $output, $this->repoDir);
$branches = $this->process->splitLines($output); $branches = $this->process->splitLines($output);
if (!in_array('* master', $branches)) { if (!in_array('* master', $branches)) {
foreach ($branches as $branch) { foreach ($branches as $branch) {
@ -69,7 +68,7 @@ class GitDriver extends VcsDriver
} }
} else { } else {
// try to find a non-master remote HEAD branch // try to find a non-master remote HEAD branch
$this->process->execute(sprintf('cd %s && git branch --no-color -r', escapeshellarg($this->repoDir)), $output); $this->process->execute('git branch --no-color -r', $output, $this->repoDir);
foreach ($this->process->splitLines($output) as $branch) { foreach ($this->process->splitLines($output) as $branch) {
if ($branch && preg_match('{/HEAD +-> +[^/]+/(\S+)}', $branch, $match)) { if ($branch && preg_match('{/HEAD +-> +[^/]+/(\S+)}', $branch, $match)) {
$this->rootIdentifier = $match[1]; $this->rootIdentifier = $match[1];
@ -114,7 +113,7 @@ class GitDriver extends VcsDriver
public function getComposerInformation($identifier) public function getComposerInformation($identifier)
{ {
if (!isset($this->infoCache[$identifier])) { if (!isset($this->infoCache[$identifier])) {
$this->process->execute(sprintf('cd %s && git show %s:composer.json', escapeshellarg($this->repoDir), escapeshellarg($identifier)), $composer); $this->process->execute(sprintf('git show %s:composer.json', escapeshellarg($identifier)), $composer, $this->repoDir);
if (!trim($composer)) { if (!trim($composer)) {
return; return;
@ -123,7 +122,7 @@ class GitDriver extends VcsDriver
$composer = JsonFile::parseJson($composer); $composer = JsonFile::parseJson($composer);
if (!isset($composer['time'])) { if (!isset($composer['time'])) {
$this->process->execute(sprintf('cd %s && git log -1 --format=%%at %s', escapeshellarg($this->repoDir), escapeshellarg($identifier)), $output); $this->process->execute(sprintf('git log -1 --format=%%at %s', escapeshellarg($identifier)), $output, $this->repoDir);
$date = new \DateTime('@'.trim($output)); $date = new \DateTime('@'.trim($output));
$composer['time'] = $date->format('Y-m-d H:i:s'); $composer['time'] = $date->format('Y-m-d H:i:s');
} }
@ -139,7 +138,7 @@ class GitDriver extends VcsDriver
public function getTags() public function getTags()
{ {
if (null === $this->tags) { if (null === $this->tags) {
$this->process->execute(sprintf('cd %s && git tag', escapeshellarg($this->repoDir)), $output); $this->process->execute('git tag', $output, $this->repoDir);
$output = $this->process->splitLines($output); $output = $this->process->splitLines($output);
$this->tags = $output ? array_combine($output, $output) : array(); $this->tags = $output ? array_combine($output, $output) : array();
} }
@ -156,10 +155,9 @@ class GitDriver extends VcsDriver
$branches = array(); $branches = array();
$this->process->execute(sprintf( $this->process->execute(sprintf(
'cd %s && git branch --no-color --no-abbrev -v %s', 'git branch --no-color --no-abbrev -v %s',
escapeshellarg($this->repoDir),
$this->isLocal ? '' : '-r' $this->isLocal ? '' : '-r'
), $output); ), $output, $this->repoDir);
foreach ($this->process->splitLines($output) as $branch) { foreach ($this->process->splitLines($output) as $branch) {
if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) { if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
preg_match('{^(?:\* )? *(?:[^/]+/)?(\S+) *([a-f0-9]+) .*$}', $branch, $match); preg_match('{^(?:\* )? *(?:[^/]+/)?(\S+) *([a-f0-9]+) .*$}', $branch, $match);
@ -186,7 +184,7 @@ class GitDriver extends VcsDriver
if (static::isLocalUrl($url)) { if (static::isLocalUrl($url)) {
$process = new ProcessExecutor(); $process = new ProcessExecutor();
// check whether there is a git repo in that path // check whether there is a git repo in that path
if ($process->execute(sprintf('cd %s && git tag', escapeshellarg($url)), $output) === 0) { if ($process->execute('git tag', $output, $url) === 0) {
return true; return true;
} }
} }

View File

@ -26,15 +26,16 @@ class ProcessExecutor
/** /**
* runs a process on the commandline * runs a process on the commandline
* *
* @param $command the command to execute * @param string $command the command to execute
* @param null $output the output will be written into this var if passed * @param null $output the output will be written into this var if passed
* @param string $cwd the working directory
* @return int statuscode * @return int statuscode
*/ */
public function execute($command, &$output = null) public function execute($command, &$output = null, $cwd = null)
{ {
$captureOutput = count(func_get_args()) > 1; $captureOutput = count(func_get_args()) > 1;
$this->errorOutput = null; $this->errorOutput = null;
$process = new Process($command, null, null, null, static::getTimeout()); $process = new Process($command, $cwd, null, null, static::getTimeout());
$process->run(function($type, $buffer) use ($captureOutput) { $process->run(function($type, $buffer) use ($captureOutput) {
if ($captureOutput) { if ($captureOutput) {
return; return;