Skip to content

Commit ef0f30b

Browse files
author
Nick Harper
committed
Use PhutilArgumentParser in reparse.php
Summary: Update how arguments get parsed in reparse.php and add two new options --force-local and --min-date (both to be used with --all). This diff also fixes a bug in destroy_revision.php Test Plan: ran the script Reviewers: epriestley, vrana Reviewed By: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D3275
1 parent f748a47 commit ef0f30b

File tree

2 files changed

+100
-112
lines changed

2 files changed

+100
-112
lines changed

scripts/differential/destroy_revision.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
$id = trim(strtolower(head($revisions)), 'd ');
5454
$revision = id(new DifferentialRevision())->load($id);
5555

56-
if (!$id) {
56+
if (!$revision) {
5757
throw new Exception("No revision '{$id}' exists!");
5858
}
5959

scripts/repository/reparse.php

+99-111
Original file line numberDiff line numberDiff line change
@@ -20,60 +20,88 @@
2020
$root = dirname(dirname(dirname(__FILE__)));
2121
require_once $root.'/scripts/__init_script__.php';
2222

23-
$is_all = false;
24-
$reparse_message = false;
25-
$reparse_change = false;
26-
$reparse_herald = false;
27-
$reparse_owners = false;
28-
$reparse_what = false;
29-
$force = false;
30-
31-
$args = array_slice($argv, 1);
32-
foreach ($args as $arg) {
33-
if (!strncmp($arg, '--', 2)) {
34-
$flag = substr($arg, 2);
35-
switch ($flag) {
36-
case 'all':
37-
$is_all = true;
38-
break;
39-
case 'message':
40-
case 'messages':
41-
$reparse_message = true;
42-
break;
43-
case 'change':
44-
case 'changes':
45-
$reparse_change = true;
46-
break;
47-
case 'herald':
48-
$reparse_herald = true;
49-
break;
50-
case 'owners':
51-
$reparse_owners = true;
52-
break;
53-
case 'force':
54-
$force = true;
55-
break;
56-
case 'trace':
57-
PhutilServiceProfiler::installEchoListener();
58-
break;
59-
case 'help':
60-
help();
61-
break;
62-
default:
63-
usage("Unknown flag '{$arg}'.");
64-
break;
65-
}
66-
} else {
67-
if ($reparse_what) {
68-
usage("Specify exactly one thing to reparse.");
69-
}
70-
$reparse_what = $arg;
71-
}
23+
$args = new PhutilArgumentParser($argv);
24+
$args->setSynopsis(<<<EOHELP
25+
**reparse.php** __what__ __which_parts__ [--trace] [--force]
26+
27+
Rerun the Diffusion parser on specific commits and repositories. Mostly
28+
useful for debugging changes to Diffusion.
29+
EOHELP
30+
);
31+
32+
$args->parseStandardArguments();
33+
$args->parse(
34+
array(
35+
// what
36+
array(
37+
'name' => 'revision',
38+
'wildcard' => true,
39+
),
40+
array(
41+
'name' => 'all',
42+
'param' => 'callsign or phid',
43+
'help' => 'Reparse all commits in the specified repository. This '.
44+
'mode queues parsers into the task queue; you must run '.
45+
'taskmasters to actually do the parses. Use with '.
46+
'__--force-local__ to run the tasks locally instead of '.
47+
'with taskmasters.',
48+
),
49+
array(
50+
'name' => 'min-date',
51+
'param' => 'date',
52+
'help' => 'When used with __--all__, this will restrict to '.
53+
'reparsing only the commits that are newer than __date__.',
54+
),
55+
// which parts
56+
array(
57+
'name' => 'message',
58+
'help' => 'Reparse commit messages.',
59+
),
60+
array(
61+
'name' => 'change',
62+
'help' => 'Reparse changes.',
63+
),
64+
array(
65+
'name' => 'herald',
66+
'help' => 'Reevaluate Herald rules (may send huge amounts of email!)',
67+
),
68+
array(
69+
'name' => 'owners',
70+
'help' => 'Reevaluate related commits for owners packages (may '.
71+
'delete existing relationship entries between your '.
72+
'package and some old commits!)',
73+
),
74+
// misc options
75+
array(
76+
'name' => 'force',
77+
'short' => 'f',
78+
'help' => 'Act noninteractively, without prompting.',
79+
),
80+
array(
81+
'name' => 'force-local',
82+
'help' => 'Only used with __--all__, use this to run the tasks '.
83+
'locally instead of deferring them to taskmaster daemons.',
84+
),
85+
));
86+
87+
$all_from_repo = $args->getArg('all');
88+
$reparse_message = $args->getArg('message');
89+
$reparse_change = $args->getArg('change');
90+
$reparse_herald = $args->getArg('herald');
91+
$reparse_owners = $args->getArg('owners');
92+
$reparse_what = $args->getArg('revision');
93+
$force = $args->getArg('force');
94+
$force_local = $args->getArg('force-local');
95+
$min_date = $args->getArg('min-date');
96+
97+
if (count($reparse_what) > 1 || !($all_from_repo xor count($reparse_what))) {
98+
usage("Specify a commit or repository to reparse.");
7299
}
73100

74-
if (!$reparse_what) {
75-
usage("Specify a commit or repository to reparse.");
101+
if ($args->getArg('trace')) {
102+
PhutilServiceProfiler::installEchoListener();
76103
}
104+
77105
if (!$reparse_message && !$reparse_change && !$reparse_herald &&
78106
!$reparse_owners) {
79107
usage("Specify what information to reparse with --message, --change, ".
@@ -92,17 +120,27 @@
92120
}
93121

94122
$commits = array();
95-
if ($is_all) {
123+
if ($all_from_repo) {
96124
$repository = id(new PhabricatorRepository())->loadOneWhere(
97125
'callsign = %s OR phid = %s',
98-
$reparse_what,
99-
$reparse_what);
126+
$all_from_repo,
127+
$all_from_repo);
100128
if (!$repository) {
101-
throw new Exception("Unknown repository '{$reparse_what}'!");
129+
throw new Exception("Unknown repository {$all_from_repo}!");
130+
}
131+
$constraint = '';
132+
if ($min_date) {
133+
$table = new PhabricatorRepositoryCommit();
134+
$conn_r = $table->establishConnection('r');
135+
$constraint = qsprintf(
136+
$conn_r,
137+
'AND epoch > unix_timestamp(%s)',
138+
$min_date);
102139
}
103140
$commits = id(new PhabricatorRepositoryCommit())->loadAllWhere(
104-
'repositoryID = %d',
105-
$repository->getID());
141+
'repositoryID = %d %Q',
142+
$repository->getID(),
143+
$constraint);
106144
if (!$commits) {
107145
throw new Exception("No commits have been discovered in that repository!");
108146
}
@@ -133,7 +171,7 @@
133171
$commits = array($commit);
134172
}
135173

136-
if ($is_all) {
174+
if ($all_from_repo && !$force_local) {
137175
echo phutil_console_format(
138176
'**NOTE**: This script will queue tasks to reparse the data. Once the '.
139177
'tasks have been queued, you need to run Taskmaster daemons to execute '.
@@ -185,7 +223,7 @@
185223
'only' => true,
186224
);
187225

188-
if ($is_all) {
226+
if ($all_from_repo && !$force_local) {
189227
foreach ($classes as $class) {
190228
$task = new PhabricatorWorkerTask();
191229
$task->setTaskClass($class);
@@ -207,57 +245,7 @@
207245
echo "\nDone.\n";
208246

209247
function usage($message) {
210-
echo "Usage Error: {$message}";
211-
echo "\n\n";
212-
echo "Run 'reparse.php --help' for detailed help.\n";
213-
exit(1);
214-
}
215-
216-
function help() {
217-
$help = <<<EOHELP
218-
**SUMMARY**
219-
220-
**reparse.php** __what__ __which_parts__ [--trace] [--force]
221-
222-
Rerun the Diffusion parser on specific commits and repositories. Mostly
223-
useful for debugging changes to Diffusion.
224-
225-
__what__: what to reparse
226-
227-
__commit__
228-
Reparse one commit. This mode will reparse the commit in-process.
229-
230-
--all __repository_callsign__
231-
--all __repository_phid__
232-
Reparse all commits in the specified repository. These modes queue
233-
parsers into the task queue, you must run taskmasters to actually
234-
do the parses.
235-
236-
__which_parts__: which parts of the thing to reparse
237-
238-
__--message__
239-
Reparse commit messages.
240-
241-
__--change__
242-
Reparse changes.
243-
244-
__--herald__
245-
Reevaluate Herald rules (may send huge amounts of email!)
246-
247-
__--owners__
248-
Reevaluate related commits for owners packages (may delete existing
249-
relationship entries between your package and some old commits!)
250-
251-
__--force__: act noninteractively, without prompting
252-
__--trace__: run with debug tracing
253-
__--help__: show this help
254-
255-
**EXAMPLES**
256-
257-
reparse.php rX123 --change # Reparse change for "rX123".
258-
reparse.php --all E --message # Reparse all messages in "E" repository.
259-
260-
EOHELP;
261-
echo phutil_console_format($help);
248+
echo phutil_console_format(
249+
'**Usage Exception:** '.$message."\n");
262250
exit(1);
263251
}

0 commit comments

Comments
 (0)