[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @task config     Configuring Repository Engines
   5   * @task internal   Internals
   6   */
   7  abstract class PhabricatorRepositoryEngine {
   8  
   9    private $repository;
  10    private $verbose;
  11  
  12    /**
  13     * @task config
  14     */
  15    public function setRepository(PhabricatorRepository $repository) {
  16      $this->repository = $repository;
  17      return $this;
  18    }
  19  
  20  
  21    /**
  22     * @task config
  23     */
  24    protected function getRepository() {
  25      if ($this->repository === null) {
  26        throw new Exception('Call setRepository() to provide a repository!');
  27      }
  28  
  29      return $this->repository;
  30    }
  31  
  32  
  33    /**
  34     * @task config
  35     */
  36    public function setVerbose($verbose) {
  37      $this->verbose = $verbose;
  38      return $this;
  39    }
  40  
  41  
  42    /**
  43     * @task config
  44     */
  45    public function getVerbose() {
  46      return $this->verbose;
  47    }
  48  
  49  
  50    public function getViewer() {
  51      return PhabricatorUser::getOmnipotentUser();
  52    }
  53  
  54    /**
  55     * Verify that the "origin" remote exists, and points at the correct URI.
  56     *
  57     * This catches or corrects some types of misconfiguration, and also repairs
  58     * an issue where Git 1.7.1 does not create an "origin" for `--bare` clones.
  59     * See T4041.
  60     *
  61     * @param   PhabricatorRepository Repository to verify.
  62     * @return  void
  63     */
  64    protected function verifyGitOrigin(PhabricatorRepository $repository) {
  65      list($remotes) = $repository->execxLocalCommand(
  66        'remote show -n origin');
  67  
  68      $matches = null;
  69      if (!preg_match('/^\s*Fetch URL:\s*(.*?)\s*$/m', $remotes, $matches)) {
  70        throw new Exception(
  71          "Expected 'Fetch URL' in 'git remote show -n origin'.");
  72      }
  73  
  74      $remote_uri = $matches[1];
  75      $expect_remote = $repository->getRemoteURI();
  76  
  77      if ($remote_uri == 'origin') {
  78        // If a remote does not exist, git pretends it does and prints out a
  79        // made up remote where the URI is the same as the remote name. This is
  80        // definitely not correct.
  81  
  82        // Possibly, we should use `git remote --verbose` instead, which does not
  83        // suffer from this problem (but is a little more complicated to parse).
  84        $valid = false;
  85        $exists = false;
  86      } else {
  87        $normal_type_git = PhabricatorRepositoryURINormalizer::TYPE_GIT;
  88  
  89        $remote_normal = id(new PhabricatorRepositoryURINormalizer(
  90          $normal_type_git,
  91          $remote_uri))->getNormalizedPath();
  92  
  93        $expect_normal = id(new PhabricatorRepositoryURINormalizer(
  94          $normal_type_git,
  95          $expect_remote))->getNormalizedPath();
  96  
  97        $valid = ($remote_normal == $expect_normal);
  98        $exists = true;
  99      }
 100  
 101      if (!$valid) {
 102        if (!$exists) {
 103          // If there's no "origin" remote, just create it regardless of how
 104          // strongly we own the working copy. There is almost no conceivable
 105          // scenario in which this could do damage.
 106          $this->log(
 107            pht(
 108              'Remote "origin" does not exist. Creating "origin", with '.
 109              'URI "%s".',
 110              $expect_remote));
 111          $repository->execxLocalCommand(
 112            'remote add origin %P',
 113            $repository->getRemoteURIEnvelope());
 114  
 115          // NOTE: This doesn't fetch the origin (it just creates it), so we won't
 116          // know about origin branches until the next "pull" happens. That's fine
 117          // for our purposes, but might impact things in the future.
 118        } else {
 119          if ($repository->canDestroyWorkingCopy()) {
 120            // Bad remote, but we can try to repair it.
 121            $this->log(
 122              pht(
 123                'Remote "origin" exists, but is pointed at the wrong URI, "%s". '.
 124                'Resetting origin URI to "%s.',
 125                $remote_uri,
 126                $expect_remote));
 127            $repository->execxLocalCommand(
 128              'remote set-url origin %P',
 129              $repository->getRemoteURIEnvelope());
 130          } else {
 131            // Bad remote and we aren't comfortable repairing it.
 132            $message = pht(
 133              'Working copy at "%s" has a mismatched origin URI, "%s". '.
 134              'The expected origin URI is "%s". Fix your configuration, or '.
 135              'set the remote URI correctly. To avoid breaking anything, '.
 136              'Phabricator will not automatically fix this.',
 137              $repository->getLocalPath(),
 138              $remote_uri,
 139              $expect_remote);
 140            throw new Exception($message);
 141          }
 142        }
 143      }
 144    }
 145  
 146  
 147  
 148  
 149    /**
 150     * @task internal
 151     */
 152    protected function log($pattern /* ... */) {
 153      if ($this->getVerbose()) {
 154        $console = PhutilConsole::getConsole();
 155        $argv = func_get_args();
 156        array_unshift($argv, "%s\n");
 157        call_user_func_array(array($console, 'writeOut'), $argv);
 158      }
 159      return $this;
 160    }
 161  
 162  }


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