Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / diffusion / query / lowlevel / DiffusionLowLevelParentsQuery.php
blob9e6c2425cb3beaf714f1c283061f94d99e586c7f
1 <?php
3 final class DiffusionLowLevelParentsQuery
4 extends DiffusionLowLevelQuery {
6 private $identifier;
8 public function withIdentifier($identifier) {
9 $this->identifier = $identifier;
10 return $this;
13 protected function executeQuery() {
14 if (!strlen($this->identifier)) {
15 throw new PhutilInvalidStateException('withIdentifier');
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));
33 return $result;
36 private function loadGitParents() {
37 $repository = $this->getRepository();
39 list($stdout) = $repository->execxLocalCommand(
40 'log -n 1 %s %s --',
41 '--format=%P',
42 gitsprintf('%s', $this->identifier));
44 return preg_split('/\s+/', trim($stdout));
47 private function loadMercurialParents() {
48 $repository = $this->getRepository();
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}';
57 list($stdout) = $repository->execxLocalCommand(
58 'log --limit 1 --template %s --rev %s',
59 $hg_log_template,
60 $this->identifier);
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]);
71 return $hashes;
74 private function loadSubversionParents() {
75 $repository = $this->getRepository();
76 $identifier = $this->identifier;
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));
89 $n = (int)$identifier;
90 if ($n > 1) {
91 $ids = array($n - 1);
92 } else {
93 $ids = array();
96 return $ids;