Browse Source

Merge remote-tracking branch 'GromNaN/git-cache'

Jordi Boggiano 11 years ago
parent
commit
7c83e6a04a

+ 13 - 0
src/Composer/Repository/Vcs/GitDriver.php

@@ -17,12 +17,14 @@ use Composer\Util\ProcessExecutor;
 use Composer\Util\Filesystem;
 use Composer\Util\Git as GitUtil;
 use Composer\IO\IOInterface;
+use Composer\Cache;
 
 /**
  * @author Jordi Boggiano <j.boggiano@seld.be>
  */
 class GitDriver extends VcsDriver
 {
+    protected $cache;
     protected $tags;
     protected $branches;
     protected $rootIdentifier;
@@ -77,6 +79,8 @@ class GitDriver extends VcsDriver
 
         $this->getTags();
         $this->getBranches();
+
+        $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url));
     }
 
     /**
@@ -132,6 +136,10 @@ class GitDriver extends VcsDriver
      */
     public function getComposerInformation($identifier)
     {
+        if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier)) {
+            $this->infoCache[$identifier] = JsonFile::parseJson($res);
+        }
+
         if (!isset($this->infoCache[$identifier])) {
             $resource = sprintf('%s:composer.json', escapeshellarg($identifier));
             $this->process->execute(sprintf('git show %s', $resource), $composer, $this->repoDir);
@@ -147,6 +155,11 @@ class GitDriver extends VcsDriver
                 $date = new \DateTime('@'.trim($output), new \DateTimeZone('UTC'));
                 $composer['time'] = $date->format('Y-m-d H:i:s');
             }
+
+            if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
+                $this->cache->write($identifier, json_encode($composer));
+            }
+
             $this->infoCache[$identifier] = $composer;
         }
 

+ 10 - 1
tests/Composer/Test/Repository/VcsRepositoryTest.php

@@ -25,12 +25,14 @@ use Composer\Config;
  */
 class VcsRepositoryTest extends \PHPUnit_Framework_TestCase
 {
+    private static $composerHome;
     private static $gitRepo;
     private $skipped;
 
     protected function initialize()
     {
         $oldCwd = getcwd();
+        self::$composerHome = sys_get_temp_dir() . '/composer-home-'.mt_rand().'/';
         self::$gitRepo = sys_get_temp_dir() . '/composer-git-'.mt_rand().'/';
 
         $locator = new ExecutableFinder();
@@ -125,6 +127,7 @@ class VcsRepositoryTest extends \PHPUnit_Framework_TestCase
     public static function tearDownAfterClass()
     {
         $fs = new Filesystem;
+        $fs->removeDirectory(self::$composerHome);
         $fs->removeDirectory(self::$gitRepo);
     }
 
@@ -140,7 +143,13 @@ class VcsRepositoryTest extends \PHPUnit_Framework_TestCase
             'dev-master' => true,
         );
 
-        $repo = new VcsRepository(array('url' => self::$gitRepo, 'type' => 'vcs'), new NullIO, new Config());
+        $config = new Config();
+        $config->merge(array(
+            'config' => array(
+                'home' => self::$composerHome,
+            ),
+        ));
+        $repo = new VcsRepository(array('url' => self::$gitRepo, 'type' => 'vcs'), new NullIO, $config);
         $packages = $repo->getPackages();
         $dumper = new ArrayDumper();