1
0
Fork 0

code improvements as suggested

Thanks for your input!
pull/5862/head
Thomas Flori 2016-11-12 11:10:13 +01:00
parent d70dfd2df3
commit ec27777341
8 changed files with 65 additions and 58 deletions

View File

@ -20,16 +20,16 @@ abstract class BitbucketDriver extends VcsDriver
protected $infoCache = array(); protected $infoCache = array();
/** /**
* @var GitDriver * @var VcsDriver
*/ */
protected $sshDriver; protected $fallbackDriver;
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function initialize() public function initialize()
{ {
preg_match('#^https?://bitbucket\.org/([^/]+)/(.+?)(\.git|/?)$#', $this->url, $match); preg_match('#^https?://bitbucket\.org/([^/]+)/([^/]+?)(\.git|/?)$#', $this->url, $match);
$this->owner = $match[1]; $this->owner = $match[1];
$this->repository = $match[2]; $this->repository = $match[2];
$this->originUrl = 'bitbucket.org'; $this->originUrl = 'bitbucket.org';
@ -49,8 +49,8 @@ abstract class BitbucketDriver extends VcsDriver
*/ */
public function getComposerInformation($identifier) public function getComposerInformation($identifier)
{ {
if ($this->sshDriver) { if ($this->fallbackDriver) {
return $this->sshDriver->getComposerInformation($identifier); return $this->fallbackDriver->getComposerInformation($identifier);
} }
if (!isset($this->infoCache[$identifier])) { if (!isset($this->infoCache[$identifier])) {
@ -110,8 +110,8 @@ abstract class BitbucketDriver extends VcsDriver
*/ */
public function getFileContent($file, $identifier) public function getFileContent($file, $identifier)
{ {
if ($this->sshDriver) { if ($this->fallbackDriver) {
return $this->sshDriver->getFileContent($file, $identifier); return $this->fallbackDriver->getFileContent($file, $identifier);
} }
if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier . ':' . $file)) { if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier . ':' . $file)) {
@ -137,8 +137,8 @@ abstract class BitbucketDriver extends VcsDriver
*/ */
public function getChangeDate($identifier) public function getChangeDate($identifier)
{ {
if ($this->sshDriver) { if ($this->fallbackDriver) {
return $this->sshDriver->getChangeDate($identifier); return $this->fallbackDriver->getChangeDate($identifier);
} }
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/' $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'
@ -163,22 +163,19 @@ abstract class BitbucketDriver extends VcsDriver
} catch (TransportException $e) { } catch (TransportException $e) {
$bitbucketUtil = new Bitbucket($this->io, $this->config, $this->process, $this->remoteFilesystem); $bitbucketUtil = new Bitbucket($this->io, $this->config, $this->process, $this->remoteFilesystem);
switch ($e->getCode()) { if (403 === $e->getCode()) {
case 403:
if (!$this->io->hasAuthentication($this->originUrl) if (!$this->io->hasAuthentication($this->originUrl)
&& $bitbucketUtil->authorizeOAuth($this->originUrl)) { && $bitbucketUtil->authorizeOAuth($this->originUrl)
) {
return parent::getContents($url); return parent::getContents($url);
} }
if (!$this->io->isInteractive() && $fetchingRepoData) { if (!$this->io->isInteractive() && $fetchingRepoData) {
return $this->attemptCloneFallback(); return $this->attemptCloneFallback();
} }
throw $e;
default:
throw $e;
} }
throw $e;
} }
} }
@ -187,19 +184,14 @@ abstract class BitbucketDriver extends VcsDriver
* *
* @return string * @return string
*/ */
protected function generateSshUrl() abstract protected function generateSshUrl();
{
return 'git@' . $this->originUrl . ':' . $this->owner.'/'.$this->repository.'.git';
}
protected function attemptCloneFallback() protected function attemptCloneFallback()
{ {
try { try {
$this->setupSshDriver($this->generateSshUrl()); $this->setupFallbackDriver($this->generateSshUrl());
return;
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
$this->sshDriver = null; $this->fallbackDriver = null;
$this->io->writeError( $this->io->writeError(
'<error>Failed to clone the ' . $this->generateSshUrl() . ' repository, try running in interactive mode' '<error>Failed to clone the ' . $this->generateSshUrl() . ' repository, try running in interactive mode'
@ -209,5 +201,5 @@ abstract class BitbucketDriver extends VcsDriver
} }
} }
abstract protected function setupSshDriver($url); abstract protected function setupFallbackDriver($url);
} }

View File

@ -32,8 +32,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
*/ */
public function getRootIdentifier() public function getRootIdentifier()
{ {
if ($this->sshDriver) { if ($this->fallbackDriver) {
return $this->sshDriver->getRootIdentifier(); return $this->fallbackDriver->getRootIdentifier();
} }
if (null === $this->rootIdentifier) { if (null === $this->rootIdentifier) {
@ -51,8 +51,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
*/ */
public function getUrl() public function getUrl()
{ {
if ($this->sshDriver) { if ($this->fallbackDriver) {
return $this->sshDriver->getUrl(); return $this->fallbackDriver->getUrl();
} }
return 'https://' . $this->originUrl . '/'.$this->owner.'/'.$this->repository.'.git'; return 'https://' . $this->originUrl . '/'.$this->owner.'/'.$this->repository.'.git';
@ -63,8 +63,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
*/ */
public function getSource($identifier) public function getSource($identifier)
{ {
if ($this->sshDriver) { if ($this->fallbackDriver) {
return $this->sshDriver->getSource($identifier); return $this->fallbackDriver->getSource($identifier);
} }
return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier); return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
@ -86,8 +86,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
*/ */
public function getTags() public function getTags()
{ {
if ($this->sshDriver) { if ($this->fallbackDriver) {
return $this->sshDriver->getTags(); return $this->fallbackDriver->getTags();
} }
if (null === $this->tags) { if (null === $this->tags) {
@ -107,8 +107,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
*/ */
public function getBranches() public function getBranches()
{ {
if ($this->sshDriver) { if ($this->fallbackDriver) {
return $this->sshDriver->getBranches(); return $this->fallbackDriver->getBranches();
} }
if (null === $this->branches) { if (null === $this->branches) {
@ -144,15 +144,23 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
/** /**
* @param string $url * @param string $url
*/ */
protected function setupSshDriver($url) protected function setupFallbackDriver($url)
{ {
$this->sshDriver = new GitDriver( $this->fallbackDriver = new GitDriver(
array('url' => $url), array('url' => $url),
$this->io, $this->io,
$this->config, $this->config,
$this->process, $this->process,
$this->remoteFilesystem $this->remoteFilesystem
); );
$this->sshDriver->initialize(); $this->fallbackDriver->initialize();
}
/**
* {@inheritdoc}
*/
protected function generateSshUrl()
{
return 'git@' . $this->originUrl . ':' . $this->owner.'/'.$this->repository.'.git';
} }
} }

View File

@ -155,7 +155,7 @@ class GitLabDriver extends VcsDriver
} }
} }
if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier . ':' . $file)) { if ($isHash = preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier . ':' . $file)) {
return $res; return $res;
} }
@ -170,7 +170,7 @@ class GitLabDriver extends VcsDriver
return null; return null;
} }
if (preg_match('{[a-f0-9]{40}}i', $identifier)) { if ($isHash) {
$this->cache->write($identifier . ':' . $file, $content); $this->cache->write($identifier . ':' . $file, $content);
} }

View File

@ -120,15 +120,23 @@ class HgBitbucketDriver extends BitbucketDriver
return true; return true;
} }
protected function setupSshDriver($url) protected function setupFallbackDriver($url)
{ {
$this->sshDriver = new HgDriver( $this->fallbackDriver = new HgDriver(
array('url' => $url), array('url' => $url),
$this->io, $this->io,
$this->config, $this->config,
$this->process, $this->process,
$this->remoteFilesystem $this->remoteFilesystem
); );
$this->sshDriver->initialize(); $this->fallbackDriver->initialize();
}
/**
* {@inheritdoc}
*/
protected function generateSshUrl()
{
return 'hg@' . $this->originUrl . '/' . $this->owner.'/'.$this->repository;
} }
} }

View File

@ -88,7 +88,7 @@ class PerforceDriver extends VcsDriver
*/ */
public function getChangeDate($identifier) public function getChangeDate($identifier)
{ {
return new \DateTime(); return null;
} }
/** /**

View File

@ -224,6 +224,8 @@ class SvnDriver extends VcsDriver
return new \DateTime($match[1], new \DateTimeZone('UTC')); return new \DateTime($match[1], new \DateTimeZone('UTC'));
} }
} }
return null;
} }
/** /**

View File

@ -83,8 +83,8 @@ abstract class VcsDriver implements VcsDriverInterface
$composer = JsonFile::parseJson($composerFileContent, $identifier . ':composer.json'); $composer = JsonFile::parseJson($composerFileContent, $identifier . ':composer.json');
if (empty($composer['time'])) { if (empty($composer['time']) && $changeDate = $this->getChangeDate($identifier)) {
$composer['time'] = $this->getChangeDate($identifier)->format('Y-m-d H:i:s'); $composer['time'] = $changeDate->format('Y-m-d H:i:s');
} }
$this->infoCache[$identifier] = $composer; $this->infoCache[$identifier] = $composer;

View File

@ -439,13 +439,10 @@ class Perforce
$index2 = strpos($result, 'no such file(s).'); $index2 = strpos($result, 'no such file(s).');
if ($index2 === false) { if ($index2 === false) {
$index3 = strpos($result, 'change'); $index3 = strpos($result, 'change');
if (!($index3 === false)) { if ($index3 !== false) {
$phrase = trim(substr($result, $index3)); $phrase = trim(substr($result, $index3));
$fields = explode(' ', $phrase); $fields = explode(' ', $phrase);
$id = $fields[1]; return substr($identifier, 0, $index) . '/' . $file . '@' . $fields[1];
$path = substr($identifier, 0, $index) . '/' . $file . '@' . $id;
return $path;
} }
} }
} }