PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/repository/reparse.php

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