Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / diffusion / query / lowlevel / DiffusionLowLevelMercurialBranchesQuery.php
blob1e040e21e9982e7522efb53ce66b6d3235bd98e1
1 <?php
3 /**
4 * Execute and parse a low-level Mercurial branches query using `hg branches`.
5 */
6 final class DiffusionLowLevelMercurialBranchesQuery
7 extends DiffusionLowLevelQuery {
9 private $contains;
11 public function withContainsCommit($commit) {
12 $this->contains = $commit;
13 return $this;
16 protected function executeQuery() {
17 $repository = $this->getRepository();
19 $specs = array();
20 if ($this->contains !== null) {
21 $specs['all'] = hgsprintf(
22 '(descendants(%s) and head())',
23 $this->contains);
24 $specs['open'] = hgsprintf(
25 '(descendants(%s) and head() and not closed())',
26 $this->contains);
27 } else {
28 $specs['all'] = hgsprintf('head()');
29 $specs['open'] = hgsprintf('head() and not closed()');
32 $futures = array();
33 foreach ($specs as $key => $spec) {
34 $futures[$key] = $repository->getLocalCommandFuture(
35 'log --template %s --rev %s',
36 '{node}\1{branch}\2',
37 $spec);
40 $branches = array();
41 $open = array();
42 foreach (new FutureIterator($futures) as $key => $future) {
43 list($stdout) = $future->resolvex();
45 $lines = explode("\2", $stdout);
46 $lines = array_filter($lines);
47 foreach ($lines as $line) {
48 list($node, $branch) = explode("\1", $line);
49 $id = $node.'/'.$branch;
50 if (empty($branches[$id])) {
51 $branches[$id] = id(new DiffusionRepositoryRef())
52 ->setShortName($branch)
53 ->setCommitIdentifier($node);
56 if ($key == 'open') {
57 $open[$id] = true;
62 foreach ($branches as $id => $branch) {
63 $branch->setRawFields(
64 array(
65 'closed' => (empty($open[$id])),
66 ));
69 return array_values($branches);