@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 99 lines 2.6 kB view raw
1<?php 2 3final class DiffusionLowLevelParentsQuery 4 extends DiffusionLowLevelQuery { 5 6 private $identifier; 7 8 public function withIdentifier($identifier) { 9 $this->identifier = $identifier; 10 return $this; 11 } 12 13 protected function executeQuery() { 14 if (!strlen($this->identifier)) { 15 throw new PhutilInvalidStateException('withIdentifier'); 16 } 17 18 $type = $this->getRepository()->getVersionControlSystem(); 19 switch ($type) { 20 case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT: 21 $result = $this->loadGitParents(); 22 break; 23 case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL: 24 $result = $this->loadMercurialParents(); 25 break; 26 case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN: 27 $result = $this->loadSubversionParents(); 28 break; 29 default: 30 throw new Exception(pht('Unsupported repository type "%s"!', $type)); 31 } 32 33 return $result; 34 } 35 36 private function loadGitParents() { 37 $repository = $this->getRepository(); 38 39 list($stdout) = $repository->execxLocalCommand( 40 'log -n 1 %s %s --', 41 '--format=%P', 42 gitsprintf('%s', $this->identifier)); 43 44 return preg_split('/\s+/', trim($stdout)); 45 } 46 47 private function loadMercurialParents() { 48 $repository = $this->getRepository(); 49 50 $hg_analyzer = PhutilBinaryAnalyzer::getForBinary('hg'); 51 if ($hg_analyzer->isMercurialTemplatePnodeAvailable()) { 52 $hg_log_template = '{p1.node} {p2.node}'; 53 } else { 54 $hg_log_template = '{p1node} {p2node}'; 55 } 56 57 list($stdout) = $repository->execxLocalCommand( 58 'log --limit 1 --template %s --rev %s', 59 $hg_log_template, 60 $this->identifier); 61 62 $hashes = preg_split('/\s+/', trim($stdout)); 63 foreach ($hashes as $key => $value) { 64 // We get 40-character hashes but also get the "000000..." hash for 65 // missing parents; ignore it. 66 if (preg_match('/^0+\z/', $value)) { 67 unset($hashes[$key]); 68 } 69 } 70 71 return $hashes; 72 } 73 74 private function loadSubversionParents() { 75 $repository = $this->getRepository(); 76 $identifier = $this->identifier; 77 78 $refs = id(new DiffusionCachedResolveRefsQuery()) 79 ->setRepository($repository) 80 ->withRefs(array($identifier)) 81 ->execute(); 82 if (!$refs) { 83 throw new Exception( 84 pht( 85 'No commit "%s" in this repository.', 86 $identifier)); 87 } 88 89 $n = (int)$identifier; 90 if ($n > 1) { 91 $ids = array($n - 1); 92 } else { 93 $ids = array(); 94 } 95 96 return $ids; 97 } 98 99}