forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorRepositoryManagementMovePathsWorkflow.php
150 lines (127 loc) · 3.39 KB
/
PhabricatorRepositoryManagementMovePathsWorkflow.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
final class PhabricatorRepositoryManagementMovePathsWorkflow
extends PhabricatorRepositoryManagementWorkflow {
protected function didConstruct() {
$this
->setName('move-paths')
->setSynopsis(pht('Move repository local paths.'))
->setArguments(
array(
array(
'name' => 'from',
'param' => 'prefix',
'help' => pht('Move paths with this prefix.'),
),
array(
'name' => 'to',
'param' => 'prefix',
'help' => pht('Replace matching prefixes with this string.'),
),
array(
'name' => 'force',
'help' => pht('Apply changes without prompting.'),
),
));
}
public function execute(PhutilArgumentParser $args) {
$console = PhutilConsole::getConsole();
$repos = id(new PhabricatorRepositoryQuery())
->setViewer($this->getViewer())
->execute();
if (!$repos) {
$console->writeErr("%s\n", pht('There are no repositories.'));
return 0;
}
$from = $args->getArg('from');
if (!strlen($from)) {
throw new Exception(
pht(
'You must specify a path prefix to move from with --from.'));
}
$to = $args->getArg('to');
if (!strlen($to)) {
throw new Exception(
pht(
'You must specify a path prefix to move to with --to.'));
}
$is_force = $args->getArg('force');
$rows = array();
$any_changes = false;
foreach ($repos as $repo) {
$src = $repo->getLocalPath();
$row = array(
'repository' => $repo,
'move' => false,
'monogram' => $repo->getMonogram(),
'src' => $src,
'dst' => '',
);
if (strncmp($src, $from, strlen($from))) {
$row['action'] = pht('Ignore');
} else {
$dst = $to.substr($src, strlen($from));
$row['action'] = tsprintf('**%s**', pht('Move'));
$row['dst'] = $dst;
$row['move'] = true;
$any_changes = true;
}
$rows[] = $row;
}
$table = id(new PhutilConsoleTable())
->addColumn(
'action',
array(
'title' => pht('Action'),
))
->addColumn(
'monogram',
array(
'title' => pht('Repository'),
))
->addColumn(
'src',
array(
'title' => pht('Src'),
))
->addColumn(
'dst',
array(
'title' => pht('Dst'),
))
->setBorders(true);
foreach ($rows as $row) {
$display = array_select_keys(
$row,
array(
'action',
'monogram',
'src',
'dst',
));
$table->addRow($display);
}
$table->draw();
if (!$any_changes) {
$console->writeOut(pht('No matching repositories.')."\n");
return 0;
}
$prompt = pht('Apply these changes?');
if (!$is_force && !phutil_console_confirm($prompt)) {
throw new Exception(pht('Declining to apply changes.'));
}
foreach ($rows as $row) {
if (empty($row['move'])) {
continue;
}
$repo = $row['repository'];
queryfx(
$repo->establishConnection('w'),
'UPDATE %T SET localPath = %s WHERE id = %d',
$repo->getTableName(),
$row['dst'],
$repo->getID());
}
$console->writeOut(pht('Applied changes.')."\n");
return 0;
}
}