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();
/**
* @var GitDriver
* @var VcsDriver
*/
protected $sshDriver;
protected $fallbackDriver;
/**
* {@inheritDoc}
*/
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->repository = $match[2];
$this->originUrl = 'bitbucket.org';
@ -49,8 +49,8 @@ abstract class BitbucketDriver extends VcsDriver
*/
public function getComposerInformation($identifier)
{
if ($this->sshDriver) {
return $this->sshDriver->getComposerInformation($identifier);
if ($this->fallbackDriver) {
return $this->fallbackDriver->getComposerInformation($identifier);
}
if (!isset($this->infoCache[$identifier])) {
@ -110,8 +110,8 @@ abstract class BitbucketDriver extends VcsDriver
*/
public function getFileContent($file, $identifier)
{
if ($this->sshDriver) {
return $this->sshDriver->getFileContent($file, $identifier);
if ($this->fallbackDriver) {
return $this->fallbackDriver->getFileContent($file, $identifier);
}
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)
{
if ($this->sshDriver) {
return $this->sshDriver->getChangeDate($identifier);
if ($this->fallbackDriver) {
return $this->fallbackDriver->getChangeDate($identifier);
}
$resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'
@ -163,22 +163,19 @@ abstract class BitbucketDriver extends VcsDriver
} catch (TransportException $e) {
$bitbucketUtil = new Bitbucket($this->io, $this->config, $this->process, $this->remoteFilesystem);
switch ($e->getCode()) {
case 403:
if (!$this->io->hasAuthentication($this->originUrl)
&& $bitbucketUtil->authorizeOAuth($this->originUrl)) {
return parent::getContents($url);
}
if (403 === $e->getCode()) {
if (!$this->io->hasAuthentication($this->originUrl)
&& $bitbucketUtil->authorizeOAuth($this->originUrl)
) {
return parent::getContents($url);
}
if (!$this->io->isInteractive() && $fetchingRepoData) {
return $this->attemptCloneFallback();
}
throw $e;
default:
throw $e;
if (!$this->io->isInteractive() && $fetchingRepoData) {
return $this->attemptCloneFallback();
}
}
throw $e;
}
}
@ -187,19 +184,14 @@ abstract class BitbucketDriver extends VcsDriver
*
* @return string
*/
protected function generateSshUrl()
{
return 'git@' . $this->originUrl . ':' . $this->owner.'/'.$this->repository.'.git';
}
abstract protected function generateSshUrl();
protected function attemptCloneFallback()
{
try {
$this->setupSshDriver($this->generateSshUrl());
return;
$this->setupFallbackDriver($this->generateSshUrl());
} catch (\RuntimeException $e) {
$this->sshDriver = null;
$this->fallbackDriver = null;
$this->io->writeError(
'<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()
{
if ($this->sshDriver) {
return $this->sshDriver->getRootIdentifier();
if ($this->fallbackDriver) {
return $this->fallbackDriver->getRootIdentifier();
}
if (null === $this->rootIdentifier) {
@ -51,8 +51,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
*/
public function getUrl()
{
if ($this->sshDriver) {
return $this->sshDriver->getUrl();
if ($this->fallbackDriver) {
return $this->fallbackDriver->getUrl();
}
return 'https://' . $this->originUrl . '/'.$this->owner.'/'.$this->repository.'.git';
@ -63,8 +63,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
*/
public function getSource($identifier)
{
if ($this->sshDriver) {
return $this->sshDriver->getSource($identifier);
if ($this->fallbackDriver) {
return $this->fallbackDriver->getSource($identifier);
}
return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
@ -86,8 +86,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
*/
public function getTags()
{
if ($this->sshDriver) {
return $this->sshDriver->getTags();
if ($this->fallbackDriver) {
return $this->fallbackDriver->getTags();
}
if (null === $this->tags) {
@ -107,8 +107,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
*/
public function getBranches()
{
if ($this->sshDriver) {
return $this->sshDriver->getBranches();
if ($this->fallbackDriver) {
return $this->fallbackDriver->getBranches();
}
if (null === $this->branches) {
@ -144,15 +144,23 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
/**
* @param string $url
*/
protected function setupSshDriver($url)
protected function setupFallbackDriver($url)
{
$this->sshDriver = new GitDriver(
$this->fallbackDriver = new GitDriver(
array('url' => $url),
$this->io,
$this->config,
$this->process,
$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;
}
@ -170,7 +170,7 @@ class GitLabDriver extends VcsDriver
return null;
}
if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
if ($isHash) {
$this->cache->write($identifier . ':' . $file, $content);
}

View File

@ -120,15 +120,23 @@ class HgBitbucketDriver extends BitbucketDriver
return true;
}
protected function setupSshDriver($url)
protected function setupFallbackDriver($url)
{
$this->sshDriver = new HgDriver(
$this->fallbackDriver = new HgDriver(
array('url' => $url),
$this->io,
$this->config,
$this->process,
$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)
{
return new \DateTime();
return null;
}
/**

View File

@ -224,6 +224,8 @@ class SvnDriver extends VcsDriver
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');
if (empty($composer['time'])) {
$composer['time'] = $this->getChangeDate($identifier)->format('Y-m-d H:i:s');
if (empty($composer['time']) && $changeDate = $this->getChangeDate($identifier)) {
$composer['time'] = $changeDate->format('Y-m-d H:i:s');
}
$this->infoCache[$identifier] = $composer;

View File

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