[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/repository/engine/ -> PhabricatorRepositoryMirrorEngine.php (source)

   1  <?php
   2  
   3  /**
   4   * Pushes a repository to its mirrors.
   5   */
   6  final class PhabricatorRepositoryMirrorEngine
   7    extends PhabricatorRepositoryEngine {
   8  
   9    public function pushToMirrors() {
  10      $repository = $this->getRepository();
  11  
  12      if (!$repository->canMirror()) {
  13        return;
  14      }
  15  
  16      $mirrors = id(new PhabricatorRepositoryMirrorQuery())
  17        ->setViewer($this->getViewer())
  18        ->withRepositoryPHIDs(array($repository->getPHID()))
  19        ->execute();
  20  
  21      $exceptions = array();
  22      foreach ($mirrors as $mirror) {
  23        try {
  24          $this->pushRepositoryToMirror($repository, $mirror);
  25        } catch (Exception $ex) {
  26          $exceptions[] = $ex;
  27        }
  28      }
  29  
  30      if ($exceptions) {
  31        throw new PhutilAggregateException(
  32          pht(
  33            'Exceptions occurred while mirroring the "%s" repository.',
  34            $repository->getCallsign()),
  35          $exceptions);
  36      }
  37    }
  38  
  39    private function pushRepositoryToMirror(
  40      PhabricatorRepository $repository,
  41      PhabricatorRepositoryMirror $mirror) {
  42  
  43      // TODO: This is a little bit janky, but we don't have first-class
  44      // infrastructure for running remote commands against an arbitrary remote
  45      // right now. Just make an emphemeral copy of the repository and muck with
  46      // it a little bit. In the medium term, we should pull this command stuff
  47      // out and use it here and for "Land to ...".
  48  
  49      $proxy = clone $repository;
  50      $proxy->makeEphemeral();
  51  
  52      $proxy->setDetail('hosting-enabled', false);
  53      $proxy->setDetail('remote-uri', $mirror->getRemoteURI());
  54      $proxy->setCredentialPHID($mirror->getCredentialPHID());
  55  
  56      $this->log(pht('Pushing to remote "%s"...', $mirror->getRemoteURI()));
  57  
  58      if ($proxy->isGit()) {
  59        $this->pushToGitRepository($proxy);
  60      } else if ($proxy->isHg()) {
  61        $this->pushToHgRepository($proxy);
  62      } else {
  63        throw new Exception(pht('Unsupported VCS!'));
  64      }
  65    }
  66  
  67    private function pushToGitRepository(
  68      PhabricatorRepository $proxy) {
  69  
  70      $future = $proxy->getRemoteCommandFuture(
  71        'push --verbose --mirror -- %P',
  72        $proxy->getRemoteURIEnvelope());
  73  
  74      $future
  75        ->setCWD($proxy->getLocalPath())
  76        ->resolvex();
  77    }
  78  
  79    private function pushToHgRepository(
  80      PhabricatorRepository $proxy) {
  81  
  82      $future = $proxy->getRemoteCommandFuture(
  83        'push --verbose --rev tip -- %P',
  84        $proxy->getRemoteURIEnvelope());
  85  
  86      try {
  87        $future
  88          ->setCWD($proxy->getLocalPath())
  89          ->resolvex();
  90      } catch (CommandException $ex) {
  91        if (preg_match('/no changes found/', $ex->getStdOut())) {
  92          // mercurial says nothing changed, but that's good
  93        } else {
  94          throw $ex;
  95        }
  96      }
  97    }
  98  
  99  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1