|
| 1 | +<?php |
| 2 | + |
| 3 | +final class DiffusionLintSaveRunner { |
| 4 | + private $arc = 'arc'; |
| 5 | + private $severity = ArcanistLintSeverity::SEVERITY_ADVICE; |
| 6 | + private $all = false; |
| 7 | + private $chunkSize = 256; |
| 8 | + |
| 9 | + private $svnRoot; |
| 10 | + private $lintCommit; |
| 11 | + private $branch; |
| 12 | + private $conn; |
| 13 | + private $deletes = array(); |
| 14 | + private $inserts = array(); |
| 15 | + |
| 16 | + |
| 17 | + public function setArc($path) { |
| 18 | + $this->arc = $path; |
| 19 | + return $this; |
| 20 | + } |
| 21 | + |
| 22 | + public function setSeverity($string) { |
| 23 | + $this->severity = $string; |
| 24 | + return $this; |
| 25 | + } |
| 26 | + |
| 27 | + public function setAll($bool) { |
| 28 | + $this->all = $bool; |
| 29 | + return $this; |
| 30 | + } |
| 31 | + |
| 32 | + public function setChunkSize($number) { |
| 33 | + $this->chunkSize = $number; |
| 34 | + return $this; |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + public function run($dir) { |
| 39 | + $working_copy = ArcanistWorkingCopyIdentity::newFromPath($dir); |
| 40 | + $api = ArcanistRepositoryAPI::newAPIFromWorkingCopyIdentity($working_copy); |
| 41 | + $this->svnRoot = id(new PhutilURI($api->getSourceControlPath()))->getPath(); |
| 42 | + |
| 43 | + $project_id = $working_copy->getProjectID(); |
| 44 | + $project = id(new PhabricatorRepositoryArcanistProject()) |
| 45 | + ->loadOneWhere('name = %s', $project_id); |
| 46 | + if (!$project || !$project->getRepositoryID()) { |
| 47 | + throw new Exception("Couldn't find repository for {$project_id}."); |
| 48 | + } |
| 49 | + |
| 50 | + $branch_name = $api->getBranchName(); |
| 51 | + $this->branch = new PhabricatorRepositoryBranch(); |
| 52 | + $this->conn = $this->branch->establishConnection('w'); |
| 53 | + $this->branch = $this->branch->loadOneWhere( |
| 54 | + 'repositoryID = %d AND name = %s', |
| 55 | + $project->getRepositoryID(), |
| 56 | + $branch_name); |
| 57 | + |
| 58 | + $this->lintCommit = null; |
| 59 | + if (!$this->branch) { |
| 60 | + $this->branch = id(new PhabricatorRepositoryBranch()) |
| 61 | + ->setRepositoryID($project->getRepositoryID()) |
| 62 | + ->setName($branch_name) |
| 63 | + ->save(); |
| 64 | + } else if (!$this->all) { |
| 65 | + $this->lintCommit = $this->branch->getLintCommit(); |
| 66 | + } |
| 67 | + |
| 68 | + if ($this->lintCommit) { |
| 69 | + try { |
| 70 | + $all_files = $api->getChangedFiles($this->lintCommit); |
| 71 | + } catch (ArcanistCapabilityNotSupportedException $ex) { |
| 72 | + $this->lintCommit = null; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (!$this->lintCommit) { |
| 77 | + $where = ($this->svnRoot |
| 78 | + ? qsprintf($this->conn, 'AND path LIKE %>', $this->svnRoot.'/') |
| 79 | + : ''); |
| 80 | + queryfx( |
| 81 | + $this->conn, |
| 82 | + 'DELETE FROM %T WHERE branchID = %d %Q', |
| 83 | + PhabricatorRepository::TABLE_LINTMESSAGE, |
| 84 | + $this->branch->getID(), |
| 85 | + $where); |
| 86 | + $all_files = $api->getAllFiles(); |
| 87 | + } |
| 88 | + |
| 89 | + $this->deletes = array(); |
| 90 | + $this->inserts = array(); |
| 91 | + $count = 0; |
| 92 | + |
| 93 | + $files = array(); |
| 94 | + foreach ($all_files as $file => $val) { |
| 95 | + $count++; |
| 96 | + if (!$this->lintCommit) { |
| 97 | + $file = $val; |
| 98 | + } else { |
| 99 | + $this->deletes[] = $this->svnRoot.'/'.$file; |
| 100 | + if ($val & ArcanistRepositoryAPI::FLAG_DELETED) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + } |
| 104 | + $files[$file] = $file; |
| 105 | + |
| 106 | + if (count($files) >= $this->chunkSize) { |
| 107 | + $this->runArcLint($files); |
| 108 | + $files = array(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + $this->runArcLint($files); |
| 113 | + $this->saveLintMessages(); |
| 114 | + $this->branch->setLintCommit($api->getWorkingCopyRevision()); |
| 115 | + $this->branch->save(); |
| 116 | + |
| 117 | + return $count; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + private function runArcLint(array $files) { |
| 122 | + if (!$files) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + echo '.'; |
| 127 | + try { |
| 128 | + $future = new ExecFuture( |
| 129 | + '%C lint --severity %s --output json %Ls', |
| 130 | + $this->arc, |
| 131 | + $this->severity, |
| 132 | + $files); |
| 133 | + |
| 134 | + foreach (new LinesOfALargeExecFuture($future) as $json) { |
| 135 | + $paths = json_decode($json, true); |
| 136 | + if (!is_array($paths)) { |
| 137 | + fprintf(STDERR, "Invalid JSON: {$json}\n"); |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + foreach ($paths as $path => $messages) { |
| 142 | + if (!isset($files[$path])) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + foreach ($messages as $message) { |
| 147 | + $this->inserts[] = qsprintf( |
| 148 | + $this->conn, |
| 149 | + '(%d, %s, %d, %s, %s, %s, %s)', |
| 150 | + $this->branch->getID(), |
| 151 | + $this->svnRoot.'/'.$path, |
| 152 | + idx($message, 'line', 0), |
| 153 | + idx($message, 'code', ''), |
| 154 | + idx($message, 'severity', ''), |
| 155 | + idx($message, 'name', ''), |
| 156 | + idx($message, 'description', '')); |
| 157 | + } |
| 158 | + |
| 159 | + if (count($this->deletes) >= 1024 || count($this->inserts) >= 256) { |
| 160 | + $this->saveLintMessages($this->branch); |
| 161 | + $this->deletes = array(); |
| 162 | + $this->inserts = array(); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + } catch (Exception $ex) { |
| 168 | + fprintf(STDERR, $ex->getMessage()."\n"); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + private function saveLintMessages() { |
| 174 | + $this->conn->openTransaction(); |
| 175 | + |
| 176 | + foreach (array_chunk($this->deletes, 1024) as $paths) { |
| 177 | + queryfx( |
| 178 | + $this->conn, |
| 179 | + 'DELETE FROM %T WHERE branchID = %d AND path IN (%Ls)', |
| 180 | + PhabricatorRepository::TABLE_LINTMESSAGE, |
| 181 | + $this->branch->getID(), |
| 182 | + $paths); |
| 183 | + } |
| 184 | + |
| 185 | + foreach (array_chunk($this->inserts, 256) as $values) { |
| 186 | + queryfx( |
| 187 | + $this->conn, |
| 188 | + 'INSERT INTO %T |
| 189 | + (branchID, path, line, code, severity, name, description) |
| 190 | + VALUES %Q', |
| 191 | + PhabricatorRepository::TABLE_LINTMESSAGE, |
| 192 | + implode(', ', $values)); |
| 193 | + } |
| 194 | + |
| 195 | + $this->conn->saveTransaction(); |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments