Rename givi.php to givi
[civicrm-core.git] / bin / givi
CommitLineData
6130de47
TO
1#!/usr/bin/env php
2<?php
3
4// This is the minimalist denialist implementation that doesn't check it's
5// pre-conditions and will screw up if you don't know what you're doing.
6
7/**
8 * Manage the current working directory as a stack.
9 */
10class DirStack {
11 protected $dirs;
12
13 function __construct($dirs = array()) {
14 $this->dirs = $dirs;
15 }
16
17 function push($dir) {
18 $this->dirs[] = getcwd();
19 if (!chdir($dir)) {
20 throw new Exception("Failed to chdir($dir)");
21 }
22 }
23
24 function pop() {
25 $oldDir = array_pop($this->dirs);
26 chdir($oldDir);
27 }
28}
29
30class Givi {
31
32 /**
33 * @var string 'checkout-all', 'begin', 'help', etc
34 */
35 protected $action;
36
37 /**
38 * @var string
39 */
40 protected $baseBranch;
41
42 /**
43 * @var array ($repoName => $gitRef)
44 */
45 protected $branches;
46
47 /**
48 * @var string
49 */
50 protected $civiRoot = '.';
51
52 /**
53 * @var int
54 */
55 protected $drupalVersion = 7;
56
57 /**
58 * @var bool
59 */
60 protected $dryRun = FALSE;
61
62 /**
63 * @var bool
64 */
65 protected $fetch = FALSE;
66
67 /**
68 * @var bool
69 */
70 protected $rebase = FALSE;
71
72 /**
73 * @var array ($repoName => $relPath)
74 */
75 protected $repos;
76
77 /**
78 * @var array, non-hyphenated arguments after the basedir
79 */
80 protected $arguments;
81
82 /**
83 * @var string, the name of this program
84 */
85 protected $program;
86
87 /**
88 * @var DirStack
89 */
90 protected $dirStack;
91
92 function __construct() {
93 $this->dirStack = new DirStack();
94 $this->repos = array(
95 'core' => '.',
96 'packages' => 'packages',
97 'joomla' => 'joomla',
98 'drupal' => 'drupal',
99 'wordpress' => 'WordPress',
100 );
101 }
102
103 function main($args) {
104 if (!$this->parseOptions($args)) {
105 printf("Error parsing arguments\n");
106 $this->doHelp();
107 return FALSE;
108 }
109
110 // All operations relative to civiRoot
111 $this->dirStack->push($this->civiRoot);
112
113 // Filter branch list based on what repos actually exist
114 foreach (array_keys($this->repos) as $repo) {
115 if (!is_dir($this->repos[$repo])) {
116 unset($this->repos[$repo]);
117 }
118 }
119 if (!isset($this->repos['core']) || !isset($this->repos['packages'])) {
120 return $this->returnError("Root appears to be invalid -- missing too many repos. Try --root=<dir>\n");
121 }
122
123 // Run the action
124 switch ($this->action) {
125 case 'checkout-all':
126 call_user_func_array(array($this, 'doCheckoutAll'), $this->arguments);
127 break;
128 case 'fetch-all':
129 call_user_func_array(array($this, 'doFetchAll'), $this->arguments);
130 break;
131 case 'status-all':
132 call_user_func_array(array($this, 'doStatusAll'), $this->arguments);
133 break;
134 case 'begin':
135 call_user_func_array(array($this, 'doBegin'), $this->arguments);
136 break;
137 case 'resume':
138 call_user_func_array(array($this, 'doResume'), $this->arguments);
139 break;
140 case 'help':
03da1773 141 case '':
6130de47
TO
142 $this->doHelp();
143 break;
144 default:
145 return $this->returnError("unrecognized action: {$this->action}\n");
146 }
147
148 $this->dirStack->pop();
149 }
150
151 /**
152 * @param $args
153 * @return bool
154 */
155 function parseOptions($args) {
156 $this->branches = array();
157 $this->arguments = array();
158
159 foreach ($args as $arg) {
160 if ($arg == '--fetch') {
161 $this->fetch = TRUE;
162 }
163 elseif ($arg == '--rebase') {
164 $this->rebase = TRUE;
165 }
166 elseif ($arg == '--dry-run' || $arg == '-n') {
167 $this->dryRun = TRUE;
168 }
169 elseif (preg_match('/^--d([678])/', $arg, $matches)) {
170 $this->drupalVersion = $matches[1];
171 }
172 elseif (preg_match('/^--root=(.*)/', $arg, $matches)) {
173 $this->civiRoot = $matches[1];
174 }
175 elseif (preg_match('/^--(core|packages|joomla|drupal|wordpress)=(.*)/', $arg, $matches)) {
176 $this->branches[$matches[1]] = $matches[2];
177 }
178 elseif (preg_match('/^-/', $arg)) {
179 printf("unrecognized argument: %s\n", $arg);
180 return FALSE;
181 }
182 else {
183 $this->arguments[] = $arg;
184 }
185 }
186
187 $this->program = @array_shift($this->arguments);
188 $this->action = @array_shift($this->arguments);
189 return TRUE;
190 }
191
192 function doHelp() {
193 $program = basename($this->program);
194 echo "Givi - Coordinate git checkouts across CiviCRM repositories\n";
195 echo "Usage:\n";
196 echo " $program [options] checkout-all <branch>\n";
197 echo " $program [options] fetch-all\n";
198 echo " $program [options] status-all\n";
199 echo " $program [options] begin <base-branch> [--core=<new-branch>|--drupal=<new-branch>|...] \n";
200 echo " $program [options] resume [--rebase] <base-branch> [--core=<custom-branch>|--drupal=<custom-branch>|...] \n";
201 echo "Actions:\n";
202 echo " checkout-all: Checkout same branch name on all repos\n";
203 echo " status-all: Display status on all repos\n";
204 echo " begin: Begin work on a new branch on some repo (and use base-branch for all others)\n";
205 echo " resume: Resume work on an existing branch on some repo (and use base-branch for all others)\n";
206 echo "Common options:\n";
207 echo " --d6: Specify that Drupal branches should use 6.x-* prefixes\n";
208 echo " --d7: Specify that Drupal branches should use 7.x-* prefixes (default)\n";
209 echo " --fetch: Fetch the latest code before creating, updating, or checking-out anything\n";
210 echo " --root=X: Specify CiviCRM root directory (default: .)\n";
211 echo "Special options:\n";
212 echo " --core=X: Specify the branch to use on the core repository\n";
213 echo " --packages=X: Specify the branch to use on the packages repository\n";
214 echo " --drupal=X: Specify the branch to use on the drupal repository\n";
215 echo " --joomla=X: Specify the branch to use on the joomla repository\n";
216 echo " --wordpress=X: Specify the branch to use on the wordpress repository\n";
217 echo " --rebase: Perform a rebase before starting work\n";
218 echo "When using 'begin' or 'resume' with a remote base-branch, most repositories\n";
219 echo "will have a detached HEAD. Only repos with an explicit branch will be real,\n";
220 echo "local branches.\n";
221 }
222
223 function doCheckoutAll($baseBranch = NULL) {
224 if (!$baseBranch) {
225 return $this->returnError("Missing <branch>\n");
226 }
227 $branches = $this->resolveBranches($baseBranch, $this->branches);
228 if ($this->fetch) {
229 $this->doFetchAll();
230 }
231
232 foreach ($this->repos as $repo => $relPath) {
233 $filteredBranch = $this->filterBranchName($repo, $branches[$repo]);
234 $this->run($relPath, 'git', 'checkout', $filteredBranch);
235 }
236 return TRUE;
237 }
238
239 function doStatusAll() {
240 foreach ($this->repos as $repo => $relPath) {
241 $this->run($relPath, 'git', 'status');
242 }
243 return TRUE;
244 }
245
246 function doBegin($baseBranch = NULL) {
247 if (!$baseBranch) {
248 return $this->returnError("Missing <base-branch>\n");
249 }
250 if (empty($this->branches)) {
251 return $this->returnError("Must specify a custom branch for at least one repository.\n");
252 }
253 $branches = $this->resolveBranches($baseBranch, $this->branches);
254 if ($this->fetch) {
255 $this->doFetchAll();
256 }
257
258 foreach ($this->repos as $repo => $relPath) {
259 $filteredBranch = $this->filterBranchName($repo, $branches[$repo]);
260 $filteredBaseBranch = $this->filterBranchName($repo, $baseBranch);
261 if ($filteredBranch == $filteredBaseBranch) {
262 $this->run($relPath, 'git', 'checkout', $filteredBranch);
263 }
264 else {
265 $this->run($relPath, 'git', 'checkout', '-b', $filteredBranch, $filteredBaseBranch);
266 }
267 }
268 }
269
270 function doResume($baseBranch = NULL) {
271 if (!$baseBranch) {
272 return $this->returnError("Missing <base-branch>\n");
273 }
274 if (empty($this->branches)) {
275 return $this->returnError("Must specify a custom branch for at least one repository.\n");
276 }
277 $branches = $this->resolveBranches($baseBranch, $this->branches);
278 if ($this->fetch) {
279 $this->doFetchAll();
280 }
281
282 foreach ($this->repos as $repo => $relPath) {
283 $this->run($relPath, 'git', 'checkout', $branches[$repo]);
284 if ($branches[$repo] != $baseBranch && $this->rebase) {
285 // FIXME: assumes
286 list ($baseRemoteRepo, $baseRemoteBranch) = $this->parseBranchRepo($baseBranch);
287 $this->run($relPath, 'git', 'pull', '--rebase', $baseRemoteRepo, $baseRemoteBranch);
288 }
289 }
290 }
291
292 /**
293 * Given a ref name, determine the repo and branch
294 *
295 * FIXME: only supports $refs like "foo" (implicit origin) or "myremote/foo"
296 *
297 * @param $ref
298 * @return array
299 */
300 function parseBranchRepo($ref, $defaultRemote = 'origin') {
301 $parts = explode('/', $ref);
302 if (count($parts) == 1) {
303 return array($defaultRemote, $parts[1]);
304 }
305 elseif (count($parts) == 2) {
306 return $parts;
307 }
308 else {
309 throw new Exception("Failed to parse branch name ($ref)");
310 }
311 }
312
313 /**
314 * Run a command
315 *
316 * Any items after $command will be escaped and added to $command
317 *
318 * @param string $runDir
319 * @param string $command
320 * @return string
321 */
322 function run($runDir, $command) {
323 $this->dirStack->push($runDir);
324
325 $args = func_get_args();
326 array_shift($args);
327 array_shift($args);
328 foreach ($args as $arg) {
329 $command .= ' ' . escapeshellarg($arg);
330 }
331 printf("\nRUN [%s]: %s\n", $runDir, $command);
332 if ($this->dryRun) {
333 $r = NULL;
334 } else {
335 $r = system($command);
336 }
337
338 $this->dirStack->pop();
339 return $r;
340 }
341
342 function doFetchAll() {
343 foreach ($this->repos as $repo => $relPath) {
344 $this->run($relPath, 'git', 'fetch', '--all');
345 }
346 }
347
348 /**
349 * @param string $default branch to use by default
350 * @return array ($repoName => $gitRef)
351 */
352 function resolveBranches($default, $overrides) {
353 $branches = $overrides;
354 foreach ($this->repos as $repo => $relPath) {
355 if (!isset($branches[$repo])) {
356 $branches[$repo] = $default;
357 }
358 }
359 return $branches;
360 }
361
362 function filterBranchName($repoName, $branchName) {
363 if ($repoName == 'drupal') {
364 $parts = explode('/', $branchName);
365 $last = $this->drupalVersion . '.x-' . array_pop($parts);
366 array_push($parts, $last);
367 return implode('/', $parts);
368 }
369 return $branchName;
370 }
371
372 function returnError($message) {
373 echo "ERROR: ", $message, "\n";
374 $this->doHelp();
375 return FALSE;
376 }
377}
378
379$givi = new Givi();
380$givi->main($argv);