1<?php
2
3abstract class ArcanistRepositoryMarkerQuery
4  extends ArcanistRepositoryQuery {
5
6  private $isActive;
7  private $markerTypes;
8  private $names;
9  private $commitHashes;
10  private $ancestorCommitHashes;
11  private $remotes;
12  private $isRemoteCache = false;
13
14  final public function withMarkerTypes(array $types) {
15    $this->markerTypes = array_fuse($types);
16    return $this;
17  }
18
19  final public function withNames(array $names) {
20    $this->names = array_fuse($names);
21    return $this;
22  }
23
24  final public function withRemotes(array $remotes) {
25    assert_instances_of($remotes, 'ArcanistRemoteRef');
26    $this->remotes = $remotes;
27    return $this;
28  }
29
30  final public function withIsRemoteCache($is_cache) {
31    $this->isRemoteCache = $is_cache;
32    return $this;
33  }
34
35  final public function withIsActive($active) {
36    $this->isActive = $active;
37    return $this;
38  }
39
40  final public function execute() {
41    $remotes = $this->remotes;
42    if ($remotes !== null) {
43      $marker_lists = array();
44      foreach ($remotes as $remote) {
45        $marker_list = $this->newRemoteRefMarkers($remote);
46        foreach ($marker_list as $marker) {
47          $marker->attachRemoteRef($remote);
48        }
49        $marker_lists[] = $marker_list;
50      }
51      $markers = array_mergev($marker_lists);
52    } else {
53      $markers = $this->newLocalRefMarkers();
54      foreach ($markers as $marker) {
55        $marker->attachRemoteRef(null);
56      }
57    }
58
59    $api = $this->getRepositoryAPI();
60    foreach ($markers as $marker) {
61      $state_ref = id(new ArcanistWorkingCopyStateRef())
62        ->setCommitRef($marker->getCommitRef());
63
64      $marker->attachWorkingCopyStateRef($state_ref);
65
66      $hash = $marker->getCommitHash();
67      $hash = $api->getDisplayHash($hash);
68      $marker->setDisplayHash($hash);
69    }
70
71    $types = $this->markerTypes;
72    if ($types !== null) {
73      foreach ($markers as $key => $marker) {
74        if (!isset($types[$marker->getMarkerType()])) {
75          unset($markers[$key]);
76        }
77      }
78    }
79
80    $names = $this->names;
81    if ($names !== null) {
82      foreach ($markers as $key => $marker) {
83        if (!isset($names[$marker->getName()])) {
84          unset($markers[$key]);
85        }
86      }
87    }
88
89    if ($this->isActive !== null) {
90      foreach ($markers as $key => $marker) {
91        if ($marker->getIsActive() !== $this->isActive) {
92          unset($markers[$key]);
93        }
94      }
95    }
96
97    if ($this->isRemoteCache !== null) {
98      $want_cache = $this->isRemoteCache;
99      foreach ($markers as $key => $marker) {
100        $is_cache = ($marker->getRemoteName() !== null);
101        if ($is_cache !== $want_cache) {
102          unset($markers[$key]);
103        }
104      }
105    }
106
107    return $markers;
108  }
109
110  final protected function shouldQueryMarkerType($marker_type) {
111    if ($this->markerTypes === null) {
112      return true;
113    }
114
115    return isset($this->markerTypes[$marker_type]);
116  }
117
118  abstract protected function newLocalRefMarkers();
119  abstract protected function newRemoteRefMarkers(ArcanistRemoteRef $remote);
120
121}
122