[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/diffusion/query/lowlevel/ -> DiffusionLowLevelCommitQuery.php (source)

   1  <?php
   2  
   3  /**
   4   * Populate a @{class:DiffusionCommitRef} with information about a specific
   5   * commit in a repository. This is a low-level query which talks directly to
   6   * the underlying VCS.
   7   */
   8  final class DiffusionLowLevelCommitQuery
   9    extends DiffusionLowLevelQuery {
  10  
  11    private $identifier;
  12  
  13    public function withIdentifier($identifier) {
  14      $this->identifier = $identifier;
  15      return $this;
  16    }
  17  
  18    public function executeQuery() {
  19      if (!strlen($this->identifier)) {
  20        throw new Exception(
  21          pht('You must provide an identifier with withIdentifier()!'));
  22      }
  23  
  24      $type = $this->getRepository()->getVersionControlSystem();
  25      switch ($type) {
  26        case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
  27          $result = $this->loadGitCommitRef();
  28          break;
  29        case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL:
  30          $result = $this->loadMercurialCommitRef();
  31          break;
  32        case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
  33          $result = $this->loadSubversionCommitRef();
  34          break;
  35        default:
  36          throw new Exception(pht('Unsupported repository type "%s"!', $type));
  37      }
  38  
  39      return $result;
  40    }
  41  
  42    private function loadGitCommitRef() {
  43      $repository = $this->getRepository();
  44  
  45      // NOTE: %B was introduced somewhat recently in git's history, so pull
  46      // commit message information with %s and %b instead.
  47  
  48      // Even though we pass --encoding here, git doesn't always succeed, so
  49      // we try a little harder, since git *does* tell us what the actual encoding
  50      // is correctly (unless it doesn't; encoding is sometimes empty).
  51      list($info) = $repository->execxLocalCommand(
  52        'log -n 1 --encoding=%s --format=%s %s --',
  53        'UTF-8',
  54        implode(
  55          '%x00',
  56          array('%e', '%cn', '%ce', '%an', '%ae', '%T', '%s%n%n%b')),
  57        $this->identifier);
  58  
  59      $parts = explode("\0", $info);
  60      $encoding = array_shift($parts);
  61  
  62      foreach ($parts as $key => $part) {
  63        if ($encoding) {
  64          $part = phutil_utf8_convert($part, 'UTF-8', $encoding);
  65        }
  66        $parts[$key] = phutil_utf8ize($part);
  67        if (!strlen($parts[$key])) {
  68          $parts[$key] = null;
  69        }
  70      }
  71  
  72      $hashes = array(
  73        id(new DiffusionCommitHash())
  74          ->setHashType(ArcanistDifferentialRevisionHash::HASH_GIT_COMMIT)
  75          ->setHashValue($this->identifier),
  76        id(new DiffusionCommitHash())
  77          ->setHashType(ArcanistDifferentialRevisionHash::HASH_GIT_TREE)
  78          ->setHashValue($parts[4]),
  79      );
  80  
  81      return id(new DiffusionCommitRef())
  82        ->setCommitterName($parts[0])
  83        ->setCommitterEmail($parts[1])
  84        ->setAuthorName($parts[2])
  85        ->setAuthorEmail($parts[3])
  86        ->setHashes($hashes)
  87        ->setMessage($parts[5]);
  88    }
  89  
  90    private function loadMercurialCommitRef() {
  91      $repository = $this->getRepository();
  92  
  93      list($stdout) = $repository->execxLocalCommand(
  94        'log --template %s --rev %s',
  95        '{author}\\n{desc}',
  96        hgsprintf('%s', $this->identifier));
  97  
  98      list($author, $message) = explode("\n", $stdout, 2);
  99  
 100      $author = phutil_utf8ize($author);
 101      $message = phutil_utf8ize($message);
 102  
 103      list($author_name, $author_email) = $this->splitUserIdentifier($author);
 104  
 105      $hashes = array(
 106        id(new DiffusionCommitHash())
 107          ->setHashType(ArcanistDifferentialRevisionHash::HASH_MERCURIAL_COMMIT)
 108          ->setHashValue($this->identifier),
 109      );
 110  
 111      return id(new DiffusionCommitRef())
 112        ->setAuthorName($author_name)
 113        ->setAuthorEmail($author_email)
 114        ->setMessage($message)
 115        ->setHashes($hashes);
 116    }
 117  
 118    private function loadSubversionCommitRef() {
 119      $repository = $this->getRepository();
 120  
 121      list($xml) = $repository->execxRemoteCommand(
 122        'log --xml --limit 1 %s',
 123        $repository->getSubversionPathURI(null, $this->identifier));
 124  
 125      // Subversion may send us back commit messages which won't parse because
 126      // they have non UTF-8 garbage in them. Slam them into valid UTF-8.
 127      $xml = phutil_utf8ize($xml);
 128      $log = new SimpleXMLElement($xml);
 129      $entry = $log->logentry[0];
 130  
 131      $author = (string)$entry->author;
 132      $message = (string)$entry->msg;
 133  
 134      list($author_name, $author_email) = $this->splitUserIdentifier($author);
 135  
 136      // No hashes in Subversion.
 137      $hashes = array();
 138  
 139      return id(new DiffusionCommitRef())
 140        ->setAuthorName($author_name)
 141        ->setAuthorEmail($author_email)
 142        ->setMessage($message)
 143        ->setHashes($hashes);
 144    }
 145  
 146    private function splitUserIdentifier($user) {
 147      $email = new PhutilEmailAddress($user);
 148  
 149      if ($email->getDisplayName() || $email->getDomainName()) {
 150        $user_name = $email->getDisplayName();
 151        $user_email = $email->getAddress();
 152      } else {
 153        $user_name = $email->getAddress();
 154        $user_email = null;
 155      }
 156  
 157      return array($user_name, $user_email);
 158    }
 159  
 160  }


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