Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from niels-nijens/svn-adapter
Browse files Browse the repository at this point in the history
Added Subversion repository adapter
  • Loading branch information
niels-nijens committed Nov 24, 2015
2 parents 399e734 + 493751a commit 59cb181
Show file tree
Hide file tree
Showing 3 changed files with 562 additions and 0 deletions.
154 changes: 154 additions & 0 deletions src/Adapter/SubversionAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace Accompli\Chrono\Adapter;

use Symfony\Component\Process\ProcessUtils;

/**
* SubversionAdapter.
*
* @author Niels Nijens <[email protected]>
*/
class SubversionAdapter extends AbstractAdapter
{
/**
* The path in the repository representing 'trunk' or 'master'.
*
* @var string
*/
private $trunkPath = 'trunk';

/**
* The path in the repository representing 'branches'.
*
* @var string
*/
private $branchesPath = 'branches';

/**
* The path in the repository representing 'tags'.
*
* @var string
*/
private $tagsPath = 'tags';

/**
* {@inheritdoc}
*/
public function supportsRepository()
{
$result = $this->processExecutor->execute('svn --version');
if ($result->isSuccessful() === false) {
return false;
}

if (preg_match('#(^svn://|^svn\+ssh://|svn\.)#i', $this->repositoryUrl)) {
return true;
}

$result = $this->processExecutor->execute(sprintf('svn info --non-interactive %s', ProcessUtils::escapeArgument($this->repositoryUrl)));
if ($result->isSuccessful()) {
return true;
}

return false;
}

/**
* {@inheritdoc}
*/
public function getBranches()
{
$branches = array();

$result = $this->processExecutor->execute(sprintf('svn ls --non-interactive %s', ProcessUtils::escapeArgument($this->repositoryUrl.'/'.$this->trunkPath)));
if ($result->isSuccessful()) {
foreach ($result->getOutputAsArray() as $branch) {
$matches = array();
if (preg_match('#^\s*(\S+).*?(\S+)\s*$#', $branch, $matches) && $matches[2] === './') {
$branches[$matches[1]] = 'master';
}
}
}

$result = $this->processExecutor->execute(sprintf('svn ls --non-interactive %s', ProcessUtils::escapeArgument($this->repositoryUrl.'/'.$this->branchesPath)));
if ($result->isSuccessful()) {
foreach ($result->getOutputAsArray() as $branch) {
$matches = array();
if (preg_match('#^\s*(\S+).*?(\S+)\s*$#', $branch, $matches)) {
$branches[$matches[1]] = $matches[2];
}
}
}

return $branches;
}

/**
* {@inheritdoc}
*/
public function getTags()
{
$tags = array();

$result = $this->processExecutor->execute(sprintf('svn ls --non-interactive %s', ProcessUtils::escapeArgument($this->repositoryUrl.'/'.$this->tagsPath)));
if ($result->isSuccessful()) {
foreach ($result->getOutputAsArray() as $tag) {
$matches = array();
if (preg_match('#^\s*(\S+).*?(\S+)\s*$#', $tag, $matches)) {
$tags[$matches[1]] = $matches[2];
}
}
}

return $tags;
}

/**
* {@inheritdoc}
*/
public function checkout($version)
{
$checkoutSuccesful = false;

$escapedRepositoryUrlWithVersionPath = $this->getRepositoryUrlWithVersionPath($version);
if ($escapedRepositoryUrlWithVersionPath === false) {
return false;
}

if ($this->processExecutor->isDirectory($this->repositoryDirectory) && $this->processExecutor->execute('svn info --non-interactive', $this->repositoryDirectory)->isSuccessful()) {
$checkoutSuccesful = $this->processExecutor->execute(sprintf('svn switch --non-interactive %s', $escapedRepositoryUrlWithVersionPath), $this->repositoryDirectory)->isSuccessful();
} else {
$escapedRepositoryDirectory = ProcessUtils::escapeArgument($this->repositoryDirectory);

$result = $this->processExecutor->execute(sprintf('svn checkout --non-interactive %s %s', $escapedRepositoryUrlWithVersionPath, $escapedRepositoryDirectory));
$checkoutSuccesful = $result->isSuccessful();
}

return $checkoutSuccesful;
}

/**
* Returns the escaped repository URL with version path.
* Returns false when the repository URL for a version cannot be found.
*
* @param string $version
*
* @return string|bool
*/
private function getRepositoryUrlWithVersionPath($version)
{
$repositoryUrl = $this->repositoryUrl;
if (in_array($version, array('master', $this->trunkPath))) {
$repositoryUrl .= '/'.$this->trunkPath;
} elseif (in_array($version, $this->getBranches())) {
$repositoryUrl .= '/'.$this->branchesPath.'/'.$version;
} elseif (in_array($version, $this->getTags())) {
$repositoryUrl .= '/'.$this->tagsPath.'/'.$version;
} else {
return false;
}

return ProcessUtils::escapeArgument($repositoryUrl);
}
}
1 change: 1 addition & 0 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Repository implements RepositoryInterface
*/
private $adapters = array(
'Accompli\Chrono\Adapter\GitAdapter',
'Accompli\Chrono\Adapter\SubversionAdapter',
);

/**
Expand Down
Loading

0 comments on commit 59cb181

Please sign in to comment.