Skip to content

Commit

Permalink
Split complex methods
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Sep 7, 2017
1 parent 584a164 commit dd559b0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 35 deletions.
32 changes: 23 additions & 9 deletions Converter/PackageUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,17 @@ public static function checkAliasVersion(AssetTypeInterface $assetType, $depende
*/
public static function convertDependencyVersion(AssetTypeInterface $assetType, $dependency, $version)
{
$containsHash = strpos($version, '#') !== false;
$containsHash = false !== strpos($version, '#');
$version = str_replace('#', '', $version);
$version = empty($version) ? '*' : $version;
$version = trim($version);
$version = empty($version) ? '*' : trim($version);
$searchVersion = str_replace(array(' ', '<', '>', '=', '^', '~'), '', $version);

// sha version or branch version
// sha size: 4-40. See https://git-scm.com/book/tr/v2/Git-Tools-Revision-Selection#_short_sha_1
if ($containsHash && preg_match('{^[0-9a-f]{4,40}$}', $version)) {
$version = 'dev-default#'.$version;
} elseif ('*' !== $version && !Validator::validateTag($searchVersion, $assetType) && !static::depIsRange($version)) {
$oldVersion = $version;
$version = 'dev-'.$assetType->getVersionConverter()->convertVersion($version);

if (!Validator::validateBranch($oldVersion)) {
$version .= ' || '.$oldVersion;
}
$version = static::convertBrachVersion($assetType, $version);
}

return array($dependency, $version);
Expand Down Expand Up @@ -304,4 +298,24 @@ protected static function depIsRange($version)

return (bool) preg_match('/[\<\>\=\^\~\ ]/', $version);
}

/**
* Convert the dependency branch version.
*
* @param AssetTypeInterface $assetType The asset type
* @param string $version The version
*
* @return string
*/
protected static function convertBrachVersion(AssetTypeInterface $assetType, $version)
{
$oldVersion = $version;
$version = 'dev-'.$assetType->getVersionConverter()->convertVersion($version);

if (!Validator::validateBranch($oldVersion)) {
$version .= ' || '.$oldVersion;
}

return $version;
}
}
70 changes: 44 additions & 26 deletions Repository/Vcs/GitDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Composer\Repository\Vcs\GitDriver as BaseGitDriver;
use Composer\Util\Filesystem;
use Composer\Util\Git as GitUtil;
use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager;

/**
* Git vcs driver.
Expand Down Expand Up @@ -46,8 +47,8 @@ public function initialize()
{
/* @var AssetRepositoryManager $arm */
$arm = $this->repoConfig['asset-repository-manager'];

$skipSync = false;

if (null !== ($skip = $arm->getConfig()->get('git-skip-update'))) {
$localUrl = $this->config->get('cache-vcs-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url).'/';
// check if local copy exists and if it is a git repository and that modification time is within threshold
Expand All @@ -57,39 +58,56 @@ public function initialize()
}
}

// copied from parent::initialize()
if (Filesystem::isLocalPath($this->url)) {
$this->url = preg_replace('{[\\/]\.git/?$}', '', $this->url);
$this->repoDir = $this->url;
$cacheUrl = realpath($this->url);
} else {
$this->repoDir = $this->config->get('cache-vcs-dir') . '/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/';
$cacheUrl = Filesystem::isLocalPath($this->url)
? $this->initializeLocalPath() : $this->initializeRemotePath($skipSync);
$this->getTags();
$this->getBranches();
$this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $cacheUrl));
}

GitUtil::cleanEnv();
/**
* Initialize the local path.
*
* @return string
*/
private function initializeLocalPath()
{
$this->url = preg_replace('{[\\/]\.git/?$}', '', $this->url);
$this->repoDir = $this->url;

$fs = new Filesystem();
$fs->ensureDirectoryExists(dirname($this->repoDir));
return realpath($this->url);
}

if (!is_writable(dirname($this->repoDir))) {
throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.dirname($this->repoDir).'" directory is not writable by the current user.');
}
/**
* Initialize the remote path.
*
* @param bool $skipSync Check if sync must be skipped
*
* @return string
*/
private function initializeRemotePath($skipSync)
{
$this->repoDir = $this->config->get('cache-vcs-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url).'/';

if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $this->url)) {
throw new \InvalidArgumentException('The source URL '.$this->url.' is invalid, ssh URLs should have a port number after ":".'."\n".'Use ssh://[email protected]:22/path or just [email protected]:path if you do not want to provide a password or custom port.');
}
GitUtil::cleanEnv();

$gitUtil = new GitUtil($this->io, $this->config, $this->process, $fs);
// patched line, sync from local dir without modifying url
if (!$skipSync && !$gitUtil->syncMirror($this->url, $this->repoDir)) {
$this->io->writeError('<error>Failed to update '.$this->url.', package information from this repository may be outdated</error>');
}
$fs = new Filesystem();
$fs->ensureDirectoryExists(dirname($this->repoDir));

$cacheUrl = $this->url;
if (!is_writable(dirname($this->repoDir))) {
throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.dirname($this->repoDir).'" directory is not writable by the current user.');
}

$this->getTags();
$this->getBranches();
if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $this->url)) {
throw new \InvalidArgumentException('The source URL '.$this->url.' is invalid, ssh URLs should have a port number after ":".'."\n".'Use ssh://[email protected]:22/path or just [email protected]:path if you do not want to provide a password or custom port.');
}

$this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $cacheUrl));
$gitUtil = new GitUtil($this->io, $this->config, $this->process, $fs);
// patched line, sync from local dir without modifying url
if (!$skipSync && !$gitUtil->syncMirror($this->url, $this->repoDir)) {
$this->io->writeError('<error>Failed to update '.$this->url.', package information from this repository may be outdated</error>');
}

return $this->url;
}
}

0 comments on commit dd559b0

Please sign in to comment.