Browse Source

Add Fossil support to Composer

bohwaz 9 years ago
parent
commit
abcbef4a67

+ 1 - 1
src/Composer/Command/CreateProjectCommand.php

@@ -200,7 +200,7 @@ EOT
         ) {
             $finder = new Finder();
             $finder->depth(0)->directories()->in(getcwd())->ignoreVCS(false)->ignoreDotFiles(false);
-            foreach (array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg') as $vcsName) {
+            foreach (array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg', '.fslckout', '_FOSSIL_') as $vcsName) {
                 $finder->name($vcsName);
             }
 

+ 117 - 0
src/Composer/Downloader/FossilDownloader.php

@@ -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_');
+    }
+}

+ 1 - 0
src/Composer/Factory.php

@@ -460,6 +460,7 @@ class Factory
 
         $dm->setDownloader('git', new Downloader\GitDownloader($io, $config, $executor, $fs));
         $dm->setDownloader('svn', new Downloader\SvnDownloader($io, $config, $executor, $fs));
+        $dm->setDownloader('fossil', new Downloader\FossilDownloader($io, $config, $executor, $fs));
         $dm->setDownloader('hg', new Downloader\HgDownloader($io, $config, $executor, $fs));
         $dm->setDownloader('perforce', new Downloader\PerforceDownloader($io, $config));
         $dm->setDownloader('zip', new Downloader\ZipDownloader($io, $config, $eventDispatcher, $cache, $executor, $rfs));

+ 30 - 0
src/Composer/Package/Version/VersionGuesser.php

@@ -74,6 +74,11 @@ class VersionGuesser
                 return $versionData;
             }
 
+            $versionData = $this->guessFossilVersion($packageConfig, $path);
+            if (null !== $versionData) {
+                return $versionData;
+            }
+
             return $this->guessSvnVersion($packageConfig, $path);
         }
     }
@@ -212,6 +217,31 @@ class VersionGuesser
         return $version;
     }
 
+    private function guessFossilVersion(array $packageConfig, $path)
+    {
+        $version = null;
+
+        // try to fetch current version from fossil
+        if (0 === $this->process->execute('fossil branch list', $output, $path)) {
+            $branch = trim($output);
+            $version = $this->versionParser->normalizeBranch($branch);
+
+            if ('9999999-dev' === $version) {
+                $version = 'dev-' . $branch;
+            }
+        }
+
+        // try to fetch current version from fossil tags
+        if (0 === $this->process->execute('fossil tag list', $output, $path)) {
+            try {
+                return $this->versionParser->normalize(trim($output));
+            } catch (\Exception $e) {
+            }
+        }
+
+        return array('version' => $version, 'commit' => '');
+    }
+
     private function guessSvnVersion(array $packageConfig, $path)
     {
         SvnUtil::cleanEnv();

+ 1 - 0
src/Composer/Repository/RepositoryFactory.php

@@ -121,6 +121,7 @@ class RepositoryFactory
         $rm->setRepositoryClass('git', 'Composer\Repository\VcsRepository');
         $rm->setRepositoryClass('gitlab', 'Composer\Repository\VcsRepository');
         $rm->setRepositoryClass('svn', 'Composer\Repository\VcsRepository');
+        $rm->setRepositoryClass('fossil', 'Composer\Repository\FossilRepository');
         $rm->setRepositoryClass('perforce', 'Composer\Repository\VcsRepository');
         $rm->setRepositoryClass('hg', 'Composer\Repository\VcsRepository');
         $rm->setRepositoryClass('artifact', 'Composer\Repository\ArtifactRepository');

+ 214 - 0
src/Composer/Repository/Vcs/FossilDriver.php

@@ -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;
+   }
+}

+ 1 - 0
src/Composer/Repository/VcsRepository.php

@@ -53,6 +53,7 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
             'hg-bitbucket'  => 'Composer\Repository\Vcs\HgBitbucketDriver',
             'hg'            => 'Composer\Repository\Vcs\HgDriver',
             'perforce'      => 'Composer\Repository\Vcs\PerforceDriver',
+            'fossil'        => 'Composer\Repository\Vcs\Fossil',
             // svn must be last because identifying a subversion server for sure is practically impossible
             'svn'           => 'Composer\Repository\Vcs\SvnDriver',
         );