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.
8 * Manage the current working directory as a stack.
13 function __construct($dirs = array()) {
18 $this->dirs
[] = getcwd();
20 throw new Exception("Failed to chdir($dir)");
25 $oldDir = array_pop($this->dirs
);
33 * @var string 'checkout-all', 'begin', 'help', etc
40 protected $baseBranch;
43 * @var array ($repoName => $gitRef)
50 protected $civiRoot = '.';
55 protected $drupalVersion = 7;
60 protected $dryRun = FALSE;
65 protected $fetch = FALSE;
70 protected $rebase = FALSE;
73 * @var array ($repoName => $relPath)
78 * @var array, non-hyphenated arguments after the basedir
83 * @var string, the name of this program
92 function __construct() {
93 $this->dirStack
= new DirStack();
96 'packages' => 'packages',
99 'wordpress' => 'WordPress',
103 function main($args) {
104 if (!$this->parseOptions($args)) {
105 printf("Error parsing arguments\n");
110 // All operations relative to civiRoot
111 $this->dirStack
->push($this->civiRoot
);
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]);
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");
124 switch ($this->action
) {
126 call_user_func_array(array($this, 'doCheckoutAll'), $this->arguments
);
129 call_user_func_array(array($this, 'doFetchAll'), $this->arguments
);
132 call_user_func_array(array($this, 'doStatusAll'), $this->arguments
);
135 call_user_func_array(array($this, 'doBegin'), $this->arguments
);
138 call_user_func_array(array($this, 'doResume'), $this->arguments
);
145 return $this->returnError("unrecognized action: {$this->action}\n");
148 $this->dirStack
->pop();
155 function parseOptions($args) {
156 $this->branches
= array();
157 $this->arguments
= array();
159 foreach ($args as $arg) {
160 if ($arg == '--fetch') {
163 elseif ($arg == '--rebase') {
164 $this->rebase
= TRUE;
166 elseif ($arg == '--dry-run' ||
$arg == '-n') {
167 $this->dryRun
= TRUE;
169 elseif (preg_match('/^--d([678])/', $arg, $matches)) {
170 $this->drupalVersion
= $matches[1];
172 elseif (preg_match('/^--root=(.*)/', $arg, $matches)) {
173 $this->civiRoot
= $matches[1];
175 elseif (preg_match('/^--(core|packages|joomla|drupal|wordpress)=(.*)/', $arg, $matches)) {
176 $this->branches
[$matches[1]] = $matches[2];
178 elseif (preg_match('/^-/', $arg)) {
179 printf("unrecognized argument: %s\n", $arg);
183 $this->arguments
[] = $arg;
187 $this->program
= @array_shift
($this->arguments
);
188 $this->action
= @array_shift
($this->arguments
);
193 $program = basename($this->program
);
194 echo "Givi - Coordinate git checkouts across CiviCRM repositories\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";
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";
223 function doCheckoutAll($baseBranch = NULL) {
225 return $this->returnError("Missing <branch>\n");
227 $branches = $this->resolveBranches($baseBranch, $this->branches
);
232 foreach ($this->repos
as $repo => $relPath) {
233 $filteredBranch = $this->filterBranchName($repo, $branches[$repo]);
234 $this->run($relPath, 'git', 'checkout', $filteredBranch);
239 function doStatusAll() {
240 foreach ($this->repos
as $repo => $relPath) {
241 $this->run($relPath, 'git', 'status');
246 function doBegin($baseBranch = NULL) {
248 return $this->returnError("Missing <base-branch>\n");
250 if (empty($this->branches
)) {
251 return $this->returnError("Must specify a custom branch for at least one repository.\n");
253 $branches = $this->resolveBranches($baseBranch, $this->branches
);
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);
265 $this->run($relPath, 'git', 'checkout', '-b', $filteredBranch, $filteredBaseBranch);
270 function doResume($baseBranch = NULL) {
272 return $this->returnError("Missing <base-branch>\n");
274 if (empty($this->branches
)) {
275 return $this->returnError("Must specify a custom branch for at least one repository.\n");
277 $branches = $this->resolveBranches($baseBranch, $this->branches
);
282 foreach ($this->repos
as $repo => $relPath) {
283 $this->run($relPath, 'git', 'checkout', $branches[$repo]);
284 if ($branches[$repo] != $baseBranch && $this->rebase
) {
286 list ($baseRemoteRepo, $baseRemoteBranch) = $this->parseBranchRepo($baseBranch);
287 $this->run($relPath, 'git', 'pull', '--rebase', $baseRemoteRepo, $baseRemoteBranch);
293 * Given a ref name, determine the repo and branch
295 * FIXME: only supports $refs like "foo" (implicit origin) or "myremote/foo"
300 function parseBranchRepo($ref, $defaultRemote = 'origin') {
301 $parts = explode('/', $ref);
302 if (count($parts) == 1) {
303 return array($defaultRemote, $parts[1]);
305 elseif (count($parts) == 2) {
309 throw new Exception("Failed to parse branch name ($ref)");
316 * Any items after $command will be escaped and added to $command
318 * @param string $runDir
319 * @param string $command
322 function run($runDir, $command) {
323 $this->dirStack
->push($runDir);
325 $args = func_get_args();
328 foreach ($args as $arg) {
329 $command .= ' ' . escapeshellarg($arg);
331 printf("\nRUN [%s]: %s\n", $runDir, $command);
335 $r = system($command);
338 $this->dirStack
->pop();
342 function doFetchAll() {
343 foreach ($this->repos
as $repo => $relPath) {
344 $this->run($relPath, 'git', 'fetch', '--all');
349 * @param string $default branch to use by default
350 * @return array ($repoName => $gitRef)
352 function resolveBranches($default, $overrides) {
353 $branches = $overrides;
354 foreach ($this->repos
as $repo => $relPath) {
355 if (!isset($branches[$repo])) {
356 $branches[$repo] = $default;
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);
372 function returnError($message) {
373 echo "ERROR: ", $message, "\n";