Add Fossil support to Composer
parent
894b53c115
commit
abcbef4a67
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Composer.
|
||||
*
|
||||
* (c) Nils Adermann <naderman@naderman.de>
|
||||
* Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Composer\Downloader;
|
||||
|
||||
use Composer\Package\PackageInterface;
|
||||
use Composer\Util\ProcessExecutor;
|
||||
|
||||
/**
|
||||
* @author BohwaZ <http://bohwaz.net/>
|
||||
*/
|
||||
class FossilDownloader extends VcsDownloader
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function doDownload(PackageInterface $package, $path, $url)
|
||||
{
|
||||
// Ensure we are allowed to use this URL by config
|
||||
$this->config->prohibitUrlByConfig($url, $this->io);
|
||||
|
||||
$url = ProcessExecutor::escape($url);
|
||||
$ref = ProcessExecutor::escape($package->getSourceReference());
|
||||
$repoFile = $path . '.fossil';
|
||||
$this->io->writeError(" Cloning ".$package->getSourceReference($repoFile));
|
||||
$command = sprintf('fossil clone %s %s', $url, ProcessExecutor::escape());
|
||||
if (0 !== $this->process->execute($command, $ignoredOutput)) {
|
||||
throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
|
||||
}
|
||||
$command = sprintf('fossil open %s', ProcessExecutor::escape($repoFile));
|
||||
if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) {
|
||||
throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
|
||||
}
|
||||
$command = sprintf('fossil update %s', $ref);
|
||||
if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) {
|
||||
throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url)
|
||||
{
|
||||
// Ensure we are allowed to use this URL by config
|
||||
$this->config->prohibitUrlByConfig($url, $this->io);
|
||||
|
||||
$url = ProcessExecutor::escape($url);
|
||||
$ref = ProcessExecutor::escape($target->getSourceReference());
|
||||
$this->io->writeError(" Updating to ".$target->getSourceReference());
|
||||
|
||||
if (!$this->hasMetadataRepository($path)) {
|
||||
throw new \RuntimeException('The .fslckout file is missing from '.$path.', see https://getcomposer.org/commit-deps for more information');
|
||||
}
|
||||
|
||||
$command = sprintf('fossil pull && fossil up', $url, $ref);
|
||||
if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) {
|
||||
throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getLocalChanges(PackageInterface $package, $path)
|
||||
{
|
||||
if (!$this->hasMetadataRepository($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->process->execute('fossil changes', $output, realpath($path));
|
||||
|
||||
return trim($output) ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getCommitLogs($fromReference, $toReference, $path)
|
||||
{
|
||||
$command = sprintf('fossil timeline -t ci -W 0 -n 0 before %s', $toReference);
|
||||
|
||||
if (0 !== $this->process->execute($command, $output, realpath($path))) {
|
||||
throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
|
||||
}
|
||||
|
||||
$log = '';
|
||||
$match = '/\d\d:\d\d:\d\d\s+\[' . $toReference . '\]/';
|
||||
|
||||
foreach ($this->process->splitLines($output) as $line) {
|
||||
if (preg_match($match, $line))
|
||||
{
|
||||
break;
|
||||
}
|
||||
$log .= $line;
|
||||
}
|
||||
|
||||
return $log;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function hasMetadataRepository($path)
|
||||
{
|
||||
return is_file($path . '/.fslckout') || is_file($path . '/_FOSSIL_');
|
||||
}
|
||||
}
|
@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Composer.
|
||||
*
|
||||
* (c) Nils Adermann <naderman@naderman.de>
|
||||
* Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Composer\Repository\Vcs;
|
||||
|
||||
use Composer\Config;
|
||||
use Composer\Json\JsonFile;
|
||||
use Composer\Util\ProcessExecutor;
|
||||
use Composer\Util\Filesystem;
|
||||
use Composer\IO\IOInterface;
|
||||
|
||||
/**
|
||||
* @author BohwaZ <http://bohwaz.net/>
|
||||
*/
|
||||
class FossilDriver extends VcsDriver
|
||||
{
|
||||
protected $tags;
|
||||
protected $branches;
|
||||
protected $rootIdentifier;
|
||||
protected $repoFile;
|
||||
protected $checkoutDir;
|
||||
protected $infoCache = array();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
if (Filesystem::isLocalPath($this->url)) {
|
||||
$this->checkoutDir = $this->url;
|
||||
} else {
|
||||
$cacheDir = $this->config->get('cache-vcs-dir');
|
||||
$this->repoFile = $cacheDir . '/' . preg_replace('{[^a-z0-9]}i', '-', $this->url) . '.fossil';
|
||||
$this->checkoutDir = $cacheDir . '/' . preg_replace('{[^a-z0-9]}i', '-', $this->url) . '/';
|
||||
|
||||
$fs = new Filesystem();
|
||||
$fs->ensureDirectoryExists($cacheDir);
|
||||
|
||||
if (!is_writable(dirname($this->checkoutDir))) {
|
||||
throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.$cacheDir.'" directory is not writable by the current user.');
|
||||
}
|
||||
|
||||
// Ensure we are allowed to use this URL by config
|
||||
$this->config->prohibitUrlByConfig($this->url, $this->io);
|
||||
|
||||
// update the repo if it is a valid fossil repository
|
||||
if (is_file($this->repoFile) && is_dir($this->checkoutDir) && 0 === $this->process->execute('fossil info', $output, $this->checkoutDir)) {
|
||||
if (0 !== $this->process->execute('fossil pull', $output, $this->checkoutDir)) {
|
||||
$this->io->writeError('<error>Failed to update '.$this->url.', package information from this repository may be outdated ('.$this->process->getErrorOutput().')</error>');
|
||||
}
|
||||
} else {
|
||||
// clean up directory and do a fresh clone into it
|
||||
$fs->removeDirectory($this->checkoutDir);
|
||||
$fs->remove($this->repoFile);
|
||||
|
||||
$fs->ensureDirectoryExists($this->checkoutDir);
|
||||
|
||||
if (0 !== $this->process->execute(sprintf('fossil clone %s %s', ProcessExecutor::escape($this->url), ProcessExecutor::escape($this->repoFile)), $output, $cacheDir)) {
|
||||
$output = $this->process->getErrorOutput();
|
||||
|
||||
if (0 !== $this->process->execute('fossil version', $ignoredOutput)) {
|
||||
throw new \RuntimeException('Failed to clone '.$this->url.', fossil was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput());
|
||||
}
|
||||
|
||||
throw new \RuntimeException('Failed to clone '.$this->url.' to repository ' . $this->repoFile . "\n\n" .$output);
|
||||
}
|
||||
|
||||
if (0 !== $this->process->execute(sprintf('fossil open %s', ProcessExecutor::escape($this->repoFile)), $output, $this->checkoutDir)) {
|
||||
$output = $this->process->getErrorOutput();
|
||||
|
||||
throw new \RuntimeException('Failed to open repository '.$this->repoFile.' in ' . $this->checkoutDir . "\n\n" .$output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->getTags();
|
||||
$this->getBranches();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getRootIdentifier()
|
||||
{
|
||||
if (null === $this->rootIdentifier) {
|
||||
$this->rootIdentifier = 'trunk';
|
||||
}
|
||||
|
||||
return $this->rootIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getSource($identifier)
|
||||
{
|
||||
return array('type' => 'fossil', 'url' => $this->getUrl(), 'reference' => $identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getDist($identifier)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getComposerInformation($identifier)
|
||||
{
|
||||
if (!isset($this->infoCache[$identifier])) {
|
||||
$this->process->execute(sprintf('fossil -r %s composer.json', ProcessExecutor::escape($identifier)), $composer, $this->checkoutDir);
|
||||
|
||||
if (!trim($composer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$composer = JsonFile::parseJson($composer, $identifier);
|
||||
|
||||
if (empty($composer['time'])) {
|
||||
$this->process->execute(sprintf('fossil finfo -r %s | head -n 2 | tail -n 1 | awk \'{print $1}\'', ProcessExecutor::escape($identifier)), $output, $this->checkoutDir);
|
||||
$date = new \DateTime(trim($output), new \DateTimeZone('UTC'));
|
||||
$composer['time'] = $date->format('Y-m-d H:i:s');
|
||||
}
|
||||
$this->infoCache[$identifier] = $composer;
|
||||
}
|
||||
|
||||
return $this->infoCache[$identifier];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getTags()
|
||||
{
|
||||
if (null === $this->tags) {
|
||||
$tags = array();
|
||||
|
||||
$this->process->execute('fossil tag list', $output, $this->checkoutDir);
|
||||
foreach ($this->process->splitLines($output) as $tag) {
|
||||
$tags[$tag] = $tag;
|
||||
}
|
||||
|
||||
$this->tags = $tags;
|
||||
}
|
||||
|
||||
return $this->tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getBranches()
|
||||
{
|
||||
if (null === $this->branches) {
|
||||
$branches = array();
|
||||
$bookmarks = array();
|
||||
|
||||
$this->process->execute('fossil branch list', $output, $this->checkoutDir);
|
||||
foreach ($this->process->splitLines($output) as $branch) {
|
||||
$branches[$branch] = $branch;
|
||||
}
|
||||
|
||||
$this->branches = $branches;
|
||||
}
|
||||
|
||||
return $this->branches;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public static function supports(IOInterface $io, Config $config, $url, $deep = false)
|
||||
{
|
||||
if (preg_match('#(^(?:https?|ssh)://(?:[^@]@)?chiselapp\.com)#i', $url)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// local filesystem
|
||||
if (Filesystem::isLocalPath($url)) {
|
||||
$url = Filesystem::getPlatformPath($url);
|
||||
if (!is_dir($url)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$process = new ProcessExecutor();
|
||||
// check whether there is a fossil repo in that path
|
||||
if ($process->execute('fossil info', $output, $url) === 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue