Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / repository / management / PhabricatorRepositoryManagementParentsWorkflow.php
blobe201ef40fcc4ec464596d27d9a80ff67f70fe7b1
1 <?php
3 final class PhabricatorRepositoryManagementParentsWorkflow
4 extends PhabricatorRepositoryManagementWorkflow {
6 protected function didConstruct() {
7 $this
8 ->setName('parents')
9 ->setExamples('**parents** [options] [__repository__] ...')
10 ->setSynopsis(
11 pht(
12 'Build parent caches in repositories that are missing the data, '.
13 'or rebuild them in a specific __repository__.'))
14 ->setArguments(
15 array(
16 array(
17 'name' => 'repos',
18 'wildcard' => true,
20 ));
23 public function execute(PhutilArgumentParser $args) {
24 $repos = $this->loadRepositories($args, 'repos');
25 if (!$repos) {
26 $repos = id(new PhabricatorRepositoryQuery())
27 ->setViewer($this->getViewer())
28 ->execute();
31 $console = PhutilConsole::getConsole();
32 foreach ($repos as $repo) {
33 $monogram = $repo->getMonogram();
34 if ($repo->isSVN()) {
35 $console->writeOut(
36 "%s\n",
37 pht(
38 'Skipping "%s": Subversion repositories do not require this '.
39 'cache to be built.',
40 $monogram));
41 continue;
43 $this->rebuildRepository($repo);
46 return 0;
49 private function rebuildRepository(PhabricatorRepository $repo) {
50 $console = PhutilConsole::getConsole();
51 $console->writeOut("%s\n", pht('Rebuilding "%s"...', $repo->getMonogram()));
53 $refs = id(new PhabricatorRepositoryRefCursorQuery())
54 ->setViewer($this->getViewer())
55 ->withRefTypes(array(PhabricatorRepositoryRefCursor::TYPE_BRANCH))
56 ->withRepositoryPHIDs(array($repo->getPHID()))
57 ->needPositions(true)
58 ->execute();
60 $graph = array();
61 foreach ($refs as $ref) {
62 if (!$repo->shouldTrackBranch($ref->getRefName())) {
63 continue;
66 $console->writeOut(
67 "%s\n",
68 pht('Rebuilding branch "%s"...', $ref->getRefName()));
70 foreach ($ref->getPositionIdentifiers() as $commit) {
71 if ($repo->isGit()) {
72 $stream = new PhabricatorGitGraphStream($repo, $commit);
73 } else {
74 $stream = new PhabricatorMercurialGraphStream($repo, $commit);
77 $discover = array($commit);
78 while ($discover) {
79 $target = array_pop($discover);
80 if (isset($graph[$target])) {
81 continue;
83 $graph[$target] = $stream->getParents($target);
84 foreach ($graph[$target] as $parent) {
85 $discover[] = $parent;
91 $console->writeOut(
92 "%s\n",
93 pht(
94 'Found %s total commit(s); updating...',
95 phutil_count($graph)));
97 $commit_table = id(new PhabricatorRepositoryCommit());
98 $commit_table_name = $commit_table->getTableName();
99 $conn_w = $commit_table->establishConnection('w');
101 $bar = id(new PhutilConsoleProgressBar())
102 ->setTotal(count($graph));
104 $need = array();
105 foreach ($graph as $child => $parents) {
106 foreach ($parents as $parent) {
107 $need[$parent] = $parent;
109 $need[$child] = $child;
112 $map = array();
113 foreach (array_chunk($need, 2048) as $chunk) {
114 $rows = queryfx_all(
115 $conn_w,
116 'SELECT id, commitIdentifier FROM %T
117 WHERE commitIdentifier IN (%Ls) AND repositoryID = %d',
118 $commit_table_name,
119 $chunk,
120 $repo->getID());
121 foreach ($rows as $row) {
122 $map[$row['commitIdentifier']] = $row['id'];
126 $insert_sql = array();
127 $delete_sql = array();
129 foreach ($graph as $child => $parents) {
130 $names = $parents;
131 $names[] = $child;
133 foreach ($names as $name) {
134 if (empty($map[$name])) {
135 throw new Exception(pht('Unknown commit "%s"!', $name));
139 if (!$parents) {
140 // Write an explicit 0 to indicate "no parents" instead of "no data".
141 $insert_sql[] = qsprintf(
142 $conn_w,
143 '(%d, 0)',
144 $map[$child]);
145 } else {
146 foreach ($parents as $parent) {
147 $insert_sql[] = qsprintf(
148 $conn_w,
149 '(%d, %d)',
150 $map[$child],
151 $map[$parent]);
155 $delete_sql[] = $map[$child];
157 $bar->update(1);
160 $commit_table->openTransaction();
161 foreach (PhabricatorLiskDAO::chunkSQL($delete_sql) as $chunk) {
162 queryfx(
163 $conn_w,
164 'DELETE FROM %T WHERE childCommitID IN (%LQ)',
165 PhabricatorRepository::TABLE_PARENTS,
166 $chunk);
169 foreach (PhabricatorLiskDAO::chunkSQL($insert_sql) as $chunk) {
170 queryfx(
171 $conn_w,
172 'INSERT INTO %T (childCommitID, parentCommitID) VALUES %LQ',
173 PhabricatorRepository::TABLE_PARENTS,
174 $chunk);
176 $commit_table->saveTransaction();
178 $bar->done();