@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 140 lines 3.7 kB view raw
1<?php 2 3final class DiffusionBranchTableController extends DiffusionController { 4 5 public function shouldAllowPublic() { 6 return true; 7 } 8 9 public function handleRequest(AphrontRequest $request) { 10 $response = $this->loadDiffusionContext(); 11 if ($response) { 12 return $response; 13 } 14 15 $viewer = $this->getViewer(); 16 $drequest = $this->getDiffusionRequest(); 17 $repository = $drequest->getRepository(); 18 19 $pager = id(new PHUIPagerView()) 20 ->readFromRequest($request); 21 22 $params = array( 23 'offset' => $pager->getOffset(), 24 'limit' => $pager->getPageSize() + 1, 25 'branch' => null, 26 ); 27 28 $contains = $drequest->getSymbolicCommit(); 29 if (phutil_nonempty_string($contains)) { 30 $params['contains'] = $contains; 31 } 32 33 $branches = $this->callConduitWithDiffusionRequest( 34 'diffusion.branchquery', 35 $params); 36 $branches = $pager->sliceResults($branches); 37 38 $branches = DiffusionRepositoryRef::loadAllFromDictionaries($branches); 39 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); 44 } 45 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(); 57 58 $list = id(new DiffusionBranchListView()) 59 ->setUser($viewer) 60 ->setBranches($branches) 61 ->setCommits($commits) 62 ->setDiffusionRequest($drequest); 63 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); 70 } 71 72 $crumbs = $this->buildCrumbs( 73 array( 74 'branches' => true, 75 )); 76 $crumbs->setBorder(true); 77 78 $header = id(new PHUIHeaderView()) 79 ->setHeader(pht('Branches')) 80 ->setHeaderIcon('fa-code-fork'); 81 82 if (!$repository->isSVN()) { 83 $branch_tag = $this->renderBranchTag($drequest); 84 $header->addTag($branch_tag); 85 } 86 87 $tabs = $this->buildTabsView('branch'); 88 89 $view = id(new PHUITwoColumnView()) 90 ->setHeader($header) 91 ->setTabs($tabs) 92 ->setFooter(array( 93 $content, 94 )); 95 96 return $this->newPage() 97 ->setTitle( 98 array( 99 pht('Branches'), 100 $repository->getDisplayName(), 101 )) 102 ->setCrumbs($crumbs) 103 ->appendChild($view); 104 } 105 106 private function sortBranches( 107 PhabricatorRepository $repository, 108 array $branches) { 109 110 $publisher = $repository->newPublisher(); 111 $default_branch = $repository->getDefaultBranch(); 112 113 $vectors = array(); 114 foreach ($branches as $key => $branch) { 115 $short_name = $branch->getShortName(); 116 117 if ($short_name === $default_branch) { 118 $order_default = 0; 119 } else { 120 $order_default = 1; 121 } 122 123 if ($publisher->shouldPublishRef($branch)) { 124 $order_permanent = 0; 125 } else { 126 $order_permanent = 1; 127 } 128 129 $vectors[$key] = id(new PhutilSortVector()) 130 ->addInt($order_default) 131 ->addInt($order_permanent) 132 ->addString($short_name); 133 } 134 135 $vectors = msortv($vectors, 'getSelf'); 136 137 return array_select_keys($branches, array_keys($vectors)); 138 } 139 140}