Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / diffusion / controller / DiffusionBranchTableController.php
blob13f566a57b9404d2a5c57dbdaf5800bf4d9817ee
1 <?php
3 final class DiffusionBranchTableController extends DiffusionController {
5 public function shouldAllowPublic() {
6 return true;
9 public function handleRequest(AphrontRequest $request) {
10 $response = $this->loadDiffusionContext();
11 if ($response) {
12 return $response;
15 $viewer = $this->getViewer();
16 $drequest = $this->getDiffusionRequest();
17 $repository = $drequest->getRepository();
19 $pager = id(new PHUIPagerView())
20 ->readFromRequest($request);
22 $params = array(
23 'offset' => $pager->getOffset(),
24 'limit' => $pager->getPageSize() + 1,
25 'branch' => null,
28 $contains = $drequest->getSymbolicCommit();
29 if (strlen($contains)) {
30 $params['contains'] = $contains;
33 $branches = $this->callConduitWithDiffusionRequest(
34 'diffusion.branchquery',
35 $params);
36 $branches = $pager->sliceResults($branches);
38 $branches = DiffusionRepositoryRef::loadAllFromDictionaries($branches);
40 // If there is one page of results or fewer, sort branches so the default
41 // branch is on top and permanent branches are below it.
42 if (!$pager->getOffset() && !$pager->getHasMorePages()) {
43 $branches = $this->sortBranches($repository, $branches);
46 $content = null;
47 if (!$branches) {
48 $content = $this->renderStatusMessage(
49 pht('No Branches'),
50 pht('This repository has no branches.'));
51 } else {
52 $commits = id(new DiffusionCommitQuery())
53 ->setViewer($viewer)
54 ->withIdentifiers(mpull($branches, 'getCommitIdentifier'))
55 ->withRepository($repository)
56 ->execute();
58 $list = id(new DiffusionBranchListView())
59 ->setUser($viewer)
60 ->setBranches($branches)
61 ->setCommits($commits)
62 ->setDiffusionRequest($drequest);
64 $content = id(new PHUIObjectBoxView())
65 ->setHeaderText($repository->getName())
66 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
67 ->addClass('diffusion-mobile-view')
68 ->setTable($list)
69 ->setPager($pager);
72 $crumbs = $this->buildCrumbs(
73 array(
74 'branches' => true,
75 ));
76 $crumbs->setBorder(true);
78 $header = id(new PHUIHeaderView())
79 ->setHeader(pht('Branches'))
80 ->setHeaderIcon('fa-code-fork');
82 if (!$repository->isSVN()) {
83 $branch_tag = $this->renderBranchTag($drequest);
84 $header->addTag($branch_tag);
87 $tabs = $this->buildTabsView('branch');
89 $view = id(new PHUITwoColumnView())
90 ->setHeader($header)
91 ->setTabs($tabs)
92 ->setFooter(array(
93 $content,
94 ));
96 return $this->newPage()
97 ->setTitle(
98 array(
99 pht('Branches'),
100 $repository->getDisplayName(),
102 ->setCrumbs($crumbs)
103 ->appendChild($view);
106 private function sortBranches(
107 PhabricatorRepository $repository,
108 array $branches) {
110 $publisher = $repository->newPublisher();
111 $default_branch = $repository->getDefaultBranch();
113 $vectors = array();
114 foreach ($branches as $key => $branch) {
115 $short_name = $branch->getShortName();
117 if ($short_name === $default_branch) {
118 $order_default = 0;
119 } else {
120 $order_default = 1;
123 if ($publisher->shouldPublishRef($branch)) {
124 $order_permanent = 0;
125 } else {
126 $order_permanent = 1;
129 $vectors[$key] = id(new PhutilSortVector())
130 ->addInt($order_default)
131 ->addInt($order_permanent)
132 ->addString($short_name);
135 $vectors = msortv($vectors, 'getSelf');
137 return array_select_keys($branches, array_keys($vectors));