|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/* |
| 5 | + * Copyright 2011 Facebook, Inc. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +$root = dirname(dirname(dirname(__FILE__))); |
| 21 | +require_once $root.'/scripts/__init_script__.php'; |
| 22 | +require_once $root.'/scripts/__init_env__.php'; |
| 23 | + |
| 24 | +phutil_require_module('phutil', 'console'); |
| 25 | + |
| 26 | +$is_all = false; |
| 27 | +$reparse_message = false; |
| 28 | +$reparse_change = false; |
| 29 | +$reparse_herald = false; |
| 30 | +$reparse_what = false; |
| 31 | + |
| 32 | +$args = array_slice($argv, 1); |
| 33 | +foreach ($args as $arg) { |
| 34 | + if (!strncmp($arg, '--', 2)) { |
| 35 | + $flag = substr($arg, 2); |
| 36 | + switch ($flag) { |
| 37 | + case 'all': |
| 38 | + $is_all = true; |
| 39 | + break; |
| 40 | + case 'message': |
| 41 | + case 'messages': |
| 42 | + $reparse_message = true; |
| 43 | + break; |
| 44 | + case 'change': |
| 45 | + case 'changes': |
| 46 | + $reparse_change = true; |
| 47 | + break; |
| 48 | + case 'herald': |
| 49 | + $reparse_herald = true; |
| 50 | + break; |
| 51 | + case 'trace': |
| 52 | + PhutilServiceProfiler::installEchoListener(); |
| 53 | + break; |
| 54 | + case 'help': |
| 55 | + help(); |
| 56 | + break; |
| 57 | + default: |
| 58 | + usage("Unknown flag '{$arg}'."); |
| 59 | + } |
| 60 | + } else { |
| 61 | + if ($reparse_what) { |
| 62 | + usage("Specify exactly one thing to reparse."); |
| 63 | + } |
| 64 | + $reparse_what = $arg; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +if (!$reparse_what) { |
| 69 | + usage("Specify a commit or repository to reparse."); |
| 70 | +} |
| 71 | +if (!$reparse_message && !$reparse_change && !$reparse_herald) { |
| 72 | + usage("Specify what information to reparse with --message, --change, and/or ". |
| 73 | + "--herald."); |
| 74 | +} |
| 75 | + |
| 76 | +$commits = array(); |
| 77 | +if ($is_all) { |
| 78 | + $repository = id(new PhabricatorRepository())->loadOneWhere( |
| 79 | + 'callsign = %s OR phid = %s', |
| 80 | + $reparse_what, |
| 81 | + $reparse_what); |
| 82 | + if (!$repository) { |
| 83 | + throw new Exception("Unknown repository '{$reparse_what}'!"); |
| 84 | + } |
| 85 | + $commits = id(new PhabricatorRepositoryCommit())->loadAllWhere( |
| 86 | + 'repositoryID = %d', |
| 87 | + $repository->getID()); |
| 88 | + if (!$commits) { |
| 89 | + throw new Exception("No commits have been discovered in that repository!"); |
| 90 | + } |
| 91 | + $callsign = $repository->getCallsign(); |
| 92 | +} else { |
| 93 | + $matches = null; |
| 94 | + if (!preg_match('/r([A-Z]+)([a-z0-9]+)/', $reparse_what, $matches)) { |
| 95 | + throw new Exception("Can't parse commit identifier!"); |
| 96 | + } |
| 97 | + $callsign = $matches[1]; |
| 98 | + $commit_identifier = $matches[2]; |
| 99 | + $repository = id(new PhabricatorRepository())->loadOneWhere( |
| 100 | + 'callsign = %s', |
| 101 | + $callsign); |
| 102 | + if (!$repository) { |
| 103 | + throw new Exception("No repository with callsign '{$callsign}'!"); |
| 104 | + } |
| 105 | + $commit = id(new PhabricatorRepositoryCommit())->loadOneWhere( |
| 106 | + 'repositoryID = %d AND commitIdentifier = %s', |
| 107 | + $repository->getID(), |
| 108 | + $commit_identifier); |
| 109 | + if (!$commit) { |
| 110 | + throw new Exception( |
| 111 | + "No matching commit '{$commit_identifier}' in repository '{$callsign}'. ". |
| 112 | + "(For git and mercurial repositories, you must specify the entire ". |
| 113 | + "commit hash.)"); |
| 114 | + } |
| 115 | + $commits = array($commit); |
| 116 | +} |
| 117 | + |
| 118 | +if ($is_all) { |
| 119 | + echo phutil_console_format( |
| 120 | + '**NOTE**: This script will queue tasks to reparse the data. Once the '. |
| 121 | + 'tasks have been queued, you need to run Taskmaster daemons to execute '. |
| 122 | + 'them.'); |
| 123 | + echo "\n\n"; |
| 124 | + echo "QUEUEING TASKS (".number_format(count($commits))." Commits):\n"; |
| 125 | +} |
| 126 | + |
| 127 | +$tasks = array(); |
| 128 | +foreach ($commits as $commit) { |
| 129 | + $classes = array(); |
| 130 | + switch ($repository->getVersionControlSystem()) { |
| 131 | + case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT: |
| 132 | + if ($reparse_message) { |
| 133 | + $classes[] = 'PhabricatorRepositoryGitCommitMessageParserWorker'; |
| 134 | + } |
| 135 | + if ($reparse_change) { |
| 136 | + $classes[] = 'PhabricatorRepositoryGitCommitChangeParserWorker'; |
| 137 | + } |
| 138 | + break; |
| 139 | + case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL: |
| 140 | + if ($reparse_message) { |
| 141 | + $classes[] = 'PhabricatorRepositoryMercurialCommitMessageParserWorker'; |
| 142 | + } |
| 143 | + if ($reparse_change) { |
| 144 | + $classes[] = 'PhabricatorRepositoryMercurialCommitChangeParserWorker'; |
| 145 | + } |
| 146 | + break; |
| 147 | + case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN: |
| 148 | + if ($reparse_message) { |
| 149 | + $classes[] = 'PhabricatorRepositorySvnCommitMessageParserWorker'; |
| 150 | + } |
| 151 | + if ($reparse_change) { |
| 152 | + $classes[] = 'PhabricatorRepositorySvnCommitChangeParserWorker'; |
| 153 | + } |
| 154 | + break; |
| 155 | + } |
| 156 | + |
| 157 | + if ($reparse_herald) { |
| 158 | + $classes[] = 'PhabricatorRepositoryCommitHeraldWorker'; |
| 159 | + } |
| 160 | + |
| 161 | + $spec = array( |
| 162 | + 'commitID' => $commit->getID(), |
| 163 | + 'only' => true, |
| 164 | + ); |
| 165 | + |
| 166 | + if ($is_all) { |
| 167 | + foreach ($classes as $class) { |
| 168 | + $task = new PhabricatorWorkerTask(); |
| 169 | + $task->setTaskClass($class); |
| 170 | + $task->setData($spec); |
| 171 | + $task->save(); |
| 172 | + |
| 173 | + $commit_name = 'r'.$callsign.$commit->getCommitIdentifier(); |
| 174 | + echo " Queued '{$class}' for commit '{$commit_name}'.\n"; |
| 175 | + } |
| 176 | + } else { |
| 177 | + foreach ($classes as $class) { |
| 178 | + $worker = newv($class, array($spec)); |
| 179 | + echo "Running '{$class}'...\n"; |
| 180 | + $worker->doWork(); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +echo "\nDone.\n"; |
| 186 | + |
| 187 | +function usage($message) { |
| 188 | + echo "Usage Error: {$message}"; |
| 189 | + echo "\n\n"; |
| 190 | + echo "Run 'reparse.php --help' for detailed help.\n"; |
| 191 | + exit(1); |
| 192 | +} |
| 193 | + |
| 194 | +function help() { |
| 195 | + $help = <<<EOHELP |
| 196 | +**SUMMARY** |
| 197 | +
|
| 198 | + **reparse.php** __what__ __which_parts__ [--trace] |
| 199 | +
|
| 200 | + Rerun the Diffusion parser on specific commits and repositories. Mostly |
| 201 | + useful for debugging changes to Diffusion. |
| 202 | +
|
| 203 | + __what__: what to reparse |
| 204 | +
|
| 205 | + __commit__ |
| 206 | + Reparse one commit. This mode will reparse the commit in-process. |
| 207 | +
|
| 208 | + --all __repository_callsign__ |
| 209 | + --all __repository_phid__ |
| 210 | + Reparse all commits in the specified repository. These modes queue |
| 211 | + parsers into the task queue, you must run taskmasters to actually |
| 212 | + do the parses them. |
| 213 | +
|
| 214 | + __which_parts__: which parts of the thing to reparse |
| 215 | +
|
| 216 | + __--message__ |
| 217 | + Reparse commit messages. |
| 218 | +
|
| 219 | + __--change__ |
| 220 | + Reparse changes. |
| 221 | +
|
| 222 | + __--herald__ |
| 223 | + Reevaluate Herald rules (may send huge amounts of email!) |
| 224 | +
|
| 225 | + __--trace__: run with debug tracing |
| 226 | + __--help__: show this help |
| 227 | +
|
| 228 | +**EXAMPLES** |
| 229 | +
|
| 230 | + reparse.php rX123 --change # Reparse change for "rX123". |
| 231 | + reparse.php --all E --message # Reparse all messages in "E" repository. |
| 232 | +
|
| 233 | +EOHELP; |
| 234 | + echo phutil_console_format($help); |
| 235 | + exit(1); |
| 236 | +} |
0 commit comments