Skip to content

Commit be26c6a

Browse files
author
epriestley
committed
Refactor repository reparse scripts to be more useful
Summary: Splitting up D960 a bit, see that for context. We currently have two scripts, "parse_one_commit.php" and "reparse_all_commit_messages.php", but they're sort of silly and you can't do certain things with them. Replace them with one script which is more flexible and can do specific reparse steps on individual commits or entire repos. I left the old scripts as stubs since I think there are some FB wiki docs and stuff that mention them. I'll delete them in a month or whenever I remember or something. Test Plan: Ran "reparse.php" with various arguments, including vs-one-commit, vs-repository, with --trace, and against different types of repos. Reviewers: Makinde, jungejason, nh, tuomaspelkonen, aran Reviewed By: jungejason CC: aran, jungejason Differential Revision: 964
1 parent 7b8b469 commit be26c6a

File tree

4 files changed

+247
-142
lines changed

4 files changed

+247
-142
lines changed

scripts/repository/parse_one_commit.php

+4-66
Original file line numberDiff line numberDiff line change
@@ -17,69 +17,7 @@
1717
* limitations under the License.
1818
*/
1919

20-
$root = dirname(dirname(dirname(__FILE__)));
21-
require_once $root.'/scripts/__init_script__.php';
22-
require_once $root.'/scripts/__init_env__.php';
23-
24-
if (empty($argv[1])) {
25-
echo "usage: parse_one_commit.php <commit_name> [--herald]\n";
26-
die(1);
27-
}
28-
29-
$commit = isset($argv[1]) ? $argv[1] : null;
30-
if (!$commit) {
31-
throw new Exception("Provide a commit to parse!");
32-
}
33-
$matches = null;
34-
if (!preg_match('/r([A-Z]+)([a-z0-9]+)/', $commit, $matches)) {
35-
throw new Exception("Can't parse commit identifier!");
36-
}
37-
$repo = id(new PhabricatorRepository())->loadOneWhere(
38-
'callsign = %s',
39-
$matches[1]);
40-
if (!$repo) {
41-
throw new Exception("Unknown repository!");
42-
}
43-
$commit = id(new PhabricatorRepositoryCommit())->loadOneWhere(
44-
'repositoryID = %d AND commitIdentifier = %s',
45-
$repo->getID(),
46-
$matches[2]);
47-
if (!$commit) {
48-
throw new Exception('Unknown commit.');
49-
}
50-
51-
$workers = array();
52-
53-
$spec = array(
54-
'commitID' => $commit->getID(),
55-
'only' => true,
56-
);
57-
58-
switch ($repo->getVersionControlSystem()) {
59-
case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
60-
$workers[] = new PhabricatorRepositoryGitCommitMessageParserWorker(
61-
$spec);
62-
$workers[] = new PhabricatorRepositoryGitCommitChangeParserWorker(
63-
$spec);
64-
break;
65-
case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
66-
$workers[] = new PhabricatorRepositorySvnCommitMessageParserWorker(
67-
$spec);
68-
$workers[] = new PhabricatorRepositorySvnCommitChangeParserWorker(
69-
$spec);
70-
break;
71-
default:
72-
throw new Exception("Unknown repository type!");
73-
}
74-
75-
if (isset($argv[2]) && $argv[2] == '--herald') {
76-
$workers[] = new PhabricatorRepositoryCommitHeraldWorker($spec);
77-
}
78-
79-
foreach ($workers as $worker) {
80-
echo "Running ".get_class($worker)."...\n";
81-
$worker->doWork();
82-
}
83-
84-
echo "Done.\n";
85-
20+
echo "This script is obsolete. Instead, use:\n\n".
21+
" $ reparse.php <commit_name> --message --change\n\n".
22+
"See that script for more options.\n";
23+
exit(1);

scripts/repository/reparse.php

+236
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
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+
}

scripts/repository/reparse_all_commit_messages.php

+4-75
Original file line numberDiff line numberDiff line change
@@ -17,79 +17,8 @@
1717
* limitations under the License.
1818
*/
1919

20-
$root = dirname(dirname(dirname(__FILE__)));
21-
require_once $root.'/scripts/__init_script__.php';
22-
require_once $root.'/scripts/__init_env__.php';
20+
echo "This script is obsolete. Instead, use:\n\n".
21+
" $ reparse.php --all <repository_name> --message\n\n".
22+
"See that script for more options.\n";
23+
exit(1);
2324

24-
phutil_require_module('phutil', 'console');
25-
26-
if (empty($argv[1])) {
27-
echo "usage: reparse_all_commit_messages.php all\n".
28-
" reparse_all_commit_messages.php <repository_callsign>\n";
29-
exit(1);
30-
}
31-
32-
33-
echo phutil_console_format(
34-
'This script will queue tasks to reparse every commit message known to '.
35-
'Diffusion. Once the tasks have been inserted, you need to start '.
36-
'Taskmaster daemons to execute them.');
37-
38-
$ok = phutil_console_confirm('Do you want to continue?');
39-
if (!$ok) {
40-
die(1);
41-
}
42-
43-
if ($argv[1] == 'all') {
44-
echo "Loading all repositories...\n";
45-
$repositories = id(new PhabricatorRepository())->loadAll();
46-
echo "Loading all commits...\n";
47-
$commits = id(new PhabricatorRepositoryCommit())->loadAll();
48-
} else {
49-
$callsign = $argv[1];
50-
echo "Loading '{$callsign}' repository...\n";
51-
$repository = id(new PhabricatorRepository())->loadOneWhere(
52-
'callsign = %s',
53-
$argv[1]);
54-
if (!$repository) {
55-
throw new Exception("No such repository exists!");
56-
}
57-
$repositories = array(
58-
$repository->getID() => $repository,
59-
);
60-
echo "Loading commits in '{$callsign}' repository...\n";
61-
$commits = id(new PhabricatorRepositoryCommit())->loadAllWhere(
62-
'repositoryID = %d',
63-
$repository->getID());
64-
}
65-
66-
echo "Inserting tasks for ".count($commits)." commits";
67-
foreach ($commits as $commit) {
68-
echo ".";
69-
$id = $commit->getID();
70-
$repo = idx($repositories, $commit->getRepositoryID());
71-
if (!$repo) {
72-
echo "\nWarning: Commit #{$id} has an invalid repository ID.\n";
73-
}
74-
75-
switch ($repo->getVersionControlSystem()) {
76-
case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
77-
$task_class = 'PhabricatorRepositoryGitCommitMessageParserWorker';
78-
break;
79-
case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
80-
$task_class = 'PhabricatorRepositorySvnCommitMessageParserWorker';
81-
break;
82-
default:
83-
throw new Exception("Unknown repository type!");
84-
}
85-
86-
$task = new PhabricatorWorkerTask();
87-
$task->setTaskClass($task_class);
88-
$task->setData(
89-
array(
90-
'commitID' => $commit->getID(),
91-
'only' => true,
92-
));
93-
$task->save();
94-
}
95-
echo "\nDone.\n";

0 commit comments

Comments
 (0)