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', '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();
98 '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 " You have cloned and forked the CiviCRM repos. Each of the repos has two\n";
197 echo " remotes (origin + upstream). When working on a new PR, you generally want\n";
198 echo " to checkout official code (eg upstream/master) in all repos, but 1-2 repos\n";
199 echo " should use a custom branch (which tracks upstream/master).\n";
201 echo " $program [options] checkout <branch>\n";
202 echo " $program [options] fetch\n";
203 echo " $program [options] status\n";
204 echo " $program [options] begin <base-branch> [--core=<new-branch>|--drupal=<new-branch>|...] \n";
205 echo " $program [options] resume [--rebase] <base-branch> [--core=<custom-branch>|--drupal=<custom-branch>|...] \n";
207 echo " checkout: Checkout same branch name on all repos\n";
208 echo " fetch: Fetch remote changes on all repos\n";
209 echo " status: Display status on all repos\n";
210 echo " begin: Begin work on a new branch on some repo (and use base-branch for all others)\n";
211 echo " resume: Resume work on an existing branch on some repo (and use base-branch for all others)\n";
212 echo "Common options:\n";
213 echo " --dry-run: Don't do anything; only print commands that would be run\n";
214 echo " --d6: Specify that Drupal branches should use 6.x-* prefixes\n";
215 echo " --d7: Specify that Drupal branches should use 7.x-* prefixes (default)\n";
216 echo " --fetch: Fetch the latest code before creating, updating, or checking-out anything\n";
217 echo " --root=X: Specify CiviCRM root directory (default: .)\n";
218 echo "Special options:\n";
219 echo " --core=X: Specify the branch to use on the core repository\n";
220 echo " --packages=X: Specify the branch to use on the packages repository\n";
221 echo " --drupal=X: Specify the branch to use on the drupal repository\n";
222 echo " --joomla=X: Specify the branch to use on the joomla repository\n";
223 echo " --wordpress=X: Specify the branch to use on the wordpress repository\n";
224 echo " --rebase: Perform a rebase before starting work\n";
225 echo "Known repositories:\n";
226 foreach ($this->repos
as $repo => $relPath) {
227 printf(" %-12s: %s\n", $repo, realpath($this->civiRoot
. DIRECTORY_SEPARATOR
. $relPath));
229 echo "When using 'begin' or 'resume' with a remote base-branch, most repositories\n";
230 echo "will have a detached HEAD. Only repos with an explicit branch will be real,\n";
231 echo "local branches.\n";
234 function doCheckoutAll($baseBranch = NULL) {
236 return $this->returnError("Missing <branch>\n");
238 $branches = $this->resolveBranches($baseBranch, $this->branches
);
243 foreach ($this->repos
as $repo => $relPath) {
244 $filteredBranch = $this->filterBranchName($repo, $branches[$repo]);
245 $this->run($relPath, 'git', 'checkout', $filteredBranch);
250 function doStatusAll() {
251 foreach ($this->repos
as $repo => $relPath) {
252 $this->run($relPath, 'git', 'status');
257 function doBegin($baseBranch = NULL) {
259 return $this->returnError("Missing <base-branch>\n");
261 if (empty($this->branches
)) {
262 return $this->returnError("Must specify a custom branch for at least one repository.\n");
264 $branches = $this->resolveBranches($baseBranch, $this->branches
);
269 foreach ($this->repos
as $repo => $relPath) {
270 $filteredBranch = $this->filterBranchName($repo, $branches[$repo]);
271 $filteredBaseBranch = $this->filterBranchName($repo, $baseBranch);
273 if ($filteredBranch == $filteredBaseBranch) {
274 $this->run($relPath, 'git', 'checkout', $filteredBranch);
277 $this->run($relPath, 'git', 'checkout', '-b', $filteredBranch, $filteredBaseBranch);
282 function doResume($baseBranch = NULL) {
284 return $this->returnError("Missing <base-branch>\n");
286 if (empty($this->branches
)) {
287 return $this->returnError("Must specify a custom branch for at least one repository.\n");
289 $branches = $this->resolveBranches($baseBranch, $this->branches
);
294 foreach ($this->repos
as $repo => $relPath) {
295 $filteredBranch = $this->filterBranchName($repo, $branches[$repo]);
296 $filteredBaseBranch = $this->filterBranchName($repo, $baseBranch);
298 $this->run($relPath, 'git', 'checkout', $filteredBranch);
299 if ($filteredBranch != $filteredBaseBranch && $this->rebase
) {
300 list ($baseRemoteRepo, $baseRemoteBranch) = $this->parseBranchRepo($filteredBaseBranch);
301 $this->run($relPath, 'git', 'pull', '--rebase', $baseRemoteRepo, $baseRemoteBranch);
307 * Given a ref name, determine the repo and branch
309 * FIXME: only supports $refs like "foo" (implicit origin) or "myremote/foo"
314 function parseBranchRepo($ref, $defaultRemote = 'origin') {
315 $parts = explode('/', $ref);
316 if (count($parts) == 1) {
317 return array($defaultRemote, $parts[1]);
319 elseif (count($parts) == 2) {
323 throw new Exception("Failed to parse branch name ($ref)");
330 * Any items after $command will be escaped and added to $command
332 * @param string $runDir
333 * @param string $command
336 function run($runDir, $command) {
337 $this->dirStack
->push($runDir);
339 $args = func_get_args();
342 foreach ($args as $arg) {
343 $command .= ' ' . escapeshellarg($arg);
345 printf("\nRUN [%s]: %s\n", $runDir, $command);
349 $r = system($command);
352 $this->dirStack
->pop();
356 function doFetchAll() {
357 foreach ($this->repos
as $repo => $relPath) {
358 $this->run($relPath, 'git', 'fetch', '--all');
363 * @param string $default branch to use by default
364 * @return array ($repoName => $gitRef)
366 function resolveBranches($default, $overrides) {
367 $branches = $overrides;
368 foreach ($this->repos
as $repo => $relPath) {
369 if (!isset($branches[$repo])) {
370 $branches[$repo] = $default;
376 function filterBranchName($repoName, $branchName) {
377 if ($repoName == 'drupal') {
378 $parts = explode('/', $branchName);
379 $last = $this->drupalVersion
. '.x-' . array_pop($parts);
380 array_push($parts, $last);
381 return implode('/', $parts);
386 function returnError($message) {
387 echo "ERROR: ", $message, "\n";