givi - Print list of known repositories on help screen
[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 /**
749e432e 33 * @var string 'checkout', 'begin', 'help', etc
6130de47
TO
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) {
749e432e 125 case 'checkout':
6130de47
TO
126 call_user_func_array(array($this, 'doCheckoutAll'), $this->arguments);
127 break;
749e432e 128 case 'fetch':
6130de47
TO
129 call_user_func_array(array($this, 'doFetchAll'), $this->arguments);
130 break;
749e432e 131 case 'status':
6130de47
TO
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";
749e432e
TO
196 echo " $program [options] checkout <branch>\n";
197 echo " $program [options] fetch\n";
198 echo " $program [options] status\n";
6130de47
TO
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";
749e432e
TO
202 echo " checkout: Checkout same branch name on all repos\n";
203 echo " fetch: Fetch remote changes on all repos\n";
204 echo " status: Display status on all repos\n";
6130de47
TO
205 echo " begin: Begin work on a new branch on some repo (and use base-branch for all others)\n";
206 echo " resume: Resume work on an existing branch on some repo (and use base-branch for all others)\n";
207 echo "Common options:\n";
749e432e 208 echo " --dry-run: Don't do anything; only print commands that would be run\n";
6130de47
TO
209 echo " --d6: Specify that Drupal branches should use 6.x-* prefixes\n";
210 echo " --d7: Specify that Drupal branches should use 7.x-* prefixes (default)\n";
211 echo " --fetch: Fetch the latest code before creating, updating, or checking-out anything\n";
212 echo " --root=X: Specify CiviCRM root directory (default: .)\n";
213 echo "Special options:\n";
214 echo " --core=X: Specify the branch to use on the core repository\n";
215 echo " --packages=X: Specify the branch to use on the packages repository\n";
216 echo " --drupal=X: Specify the branch to use on the drupal repository\n";
217 echo " --joomla=X: Specify the branch to use on the joomla repository\n";
218 echo " --wordpress=X: Specify the branch to use on the wordpress repository\n";
219 echo " --rebase: Perform a rebase before starting work\n";
ab79d84c
TO
220 echo "Known repositories:\n";
221 foreach ($this->repos as $repo => $relPath) {
222 printf(" %-12s: %s\n", $repo, realpath($this->civiRoot . DIRECTORY_SEPARATOR . $relPath));
223 }
6130de47
TO
224 echo "When using 'begin' or 'resume' with a remote base-branch, most repositories\n";
225 echo "will have a detached HEAD. Only repos with an explicit branch will be real,\n";
226 echo "local branches.\n";
227 }
228
229 function doCheckoutAll($baseBranch = NULL) {
230 if (!$baseBranch) {
231 return $this->returnError("Missing <branch>\n");
232 }
233 $branches = $this->resolveBranches($baseBranch, $this->branches);
234 if ($this->fetch) {
235 $this->doFetchAll();
236 }
237
238 foreach ($this->repos as $repo => $relPath) {
239 $filteredBranch = $this->filterBranchName($repo, $branches[$repo]);
240 $this->run($relPath, 'git', 'checkout', $filteredBranch);
241 }
242 return TRUE;
243 }
244
245 function doStatusAll() {
246 foreach ($this->repos as $repo => $relPath) {
247 $this->run($relPath, 'git', 'status');
248 }
249 return TRUE;
250 }
251
252 function doBegin($baseBranch = NULL) {
253 if (!$baseBranch) {
254 return $this->returnError("Missing <base-branch>\n");
255 }
256 if (empty($this->branches)) {
257 return $this->returnError("Must specify a custom branch for at least one repository.\n");
258 }
259 $branches = $this->resolveBranches($baseBranch, $this->branches);
260 if ($this->fetch) {
261 $this->doFetchAll();
262 }
263
264 foreach ($this->repos as $repo => $relPath) {
265 $filteredBranch = $this->filterBranchName($repo, $branches[$repo]);
266 $filteredBaseBranch = $this->filterBranchName($repo, $baseBranch);
267 if ($filteredBranch == $filteredBaseBranch) {
268 $this->run($relPath, 'git', 'checkout', $filteredBranch);
269 }
270 else {
271 $this->run($relPath, 'git', 'checkout', '-b', $filteredBranch, $filteredBaseBranch);
272 }
273 }
274 }
275
276 function doResume($baseBranch = NULL) {
277 if (!$baseBranch) {
278 return $this->returnError("Missing <base-branch>\n");
279 }
280 if (empty($this->branches)) {
281 return $this->returnError("Must specify a custom branch for at least one repository.\n");
282 }
283 $branches = $this->resolveBranches($baseBranch, $this->branches);
284 if ($this->fetch) {
285 $this->doFetchAll();
286 }
287
288 foreach ($this->repos as $repo => $relPath) {
289 $this->run($relPath, 'git', 'checkout', $branches[$repo]);
290 if ($branches[$repo] != $baseBranch && $this->rebase) {
291 // FIXME: assumes
292 list ($baseRemoteRepo, $baseRemoteBranch) = $this->parseBranchRepo($baseBranch);
293 $this->run($relPath, 'git', 'pull', '--rebase', $baseRemoteRepo, $baseRemoteBranch);
294 }
295 }
296 }
297
298 /**
299 * Given a ref name, determine the repo and branch
300 *
301 * FIXME: only supports $refs like "foo" (implicit origin) or "myremote/foo"
302 *
303 * @param $ref
304 * @return array
305 */
306 function parseBranchRepo($ref, $defaultRemote = 'origin') {
307 $parts = explode('/', $ref);
308 if (count($parts) == 1) {
309 return array($defaultRemote, $parts[1]);
310 }
311 elseif (count($parts) == 2) {
312 return $parts;
313 }
314 else {
315 throw new Exception("Failed to parse branch name ($ref)");
316 }
317 }
318
319 /**
320 * Run a command
321 *
322 * Any items after $command will be escaped and added to $command
323 *
324 * @param string $runDir
325 * @param string $command
326 * @return string
327 */
328 function run($runDir, $command) {
329 $this->dirStack->push($runDir);
330
331 $args = func_get_args();
332 array_shift($args);
333 array_shift($args);
334 foreach ($args as $arg) {
335 $command .= ' ' . escapeshellarg($arg);
336 }
337 printf("\nRUN [%s]: %s\n", $runDir, $command);
338 if ($this->dryRun) {
339 $r = NULL;
340 } else {
341 $r = system($command);
342 }
343
344 $this->dirStack->pop();
345 return $r;
346 }
347
348 function doFetchAll() {
349 foreach ($this->repos as $repo => $relPath) {
350 $this->run($relPath, 'git', 'fetch', '--all');
351 }
352 }
353
354 /**
355 * @param string $default branch to use by default
356 * @return array ($repoName => $gitRef)
357 */
358 function resolveBranches($default, $overrides) {
359 $branches = $overrides;
360 foreach ($this->repos as $repo => $relPath) {
361 if (!isset($branches[$repo])) {
362 $branches[$repo] = $default;
363 }
364 }
365 return $branches;
366 }
367
368 function filterBranchName($repoName, $branchName) {
369 if ($repoName == 'drupal') {
370 $parts = explode('/', $branchName);
371 $last = $this->drupalVersion . '.x-' . array_pop($parts);
372 array_push($parts, $last);
373 return implode('/', $parts);
374 }
375 return $branchName;
376 }
377
378 function returnError($message) {
379 echo "ERROR: ", $message, "\n";
380 $this->doHelp();
381 return FALSE;
382 }
383}
384
385$givi = new Givi();
386$givi->main($argv);