private function _switchBranches($targetStory) { // Don't switch branches if we're not switching stories $currentStoryId = GitPlanbox_Util::currentStoryId(); if ($targetStory->id == $currentStoryId) { return; } // Confirm that they want to branch off the current branch $currentBranch = GitPlanbox_Util::currentGitBranchName(); print "Starting story: {$targetStory->name}\n"; print " (branching off {$currentBranch})\n"; // Ask the user what they'd like to call the branch $config = GitPlanbox_Config::get(); $branch = NULL; while ($branch === NULL) { $branch = GitPlanbox_Util::readline("What should we name the branch? (leave blank to stay on {$currentBranch}) "); if ($branch !== '' && !preg_match($config->branchregex(), $branch)) { print "Error: Branch names must match regex: '{$config->branchregex()}'.\n"; $branch = NULL; } } if ($branch != '') { // Build the branch name based on the branch-name-template in config $branchName = $this->_buildBranchName($branch, $targetStory, $currentBranch); // Create the branch $command = "git checkout -b {$branchName} {$currentBranch}"; exec($command, $output, $returnCode); if ($returnCode !== 0) { throw new Exception("Error creating new branch."); } } }
public function run($arguments, CLImaxController $cliController) { // Look for a story id on the command line $storyId = NULL; if (isset($arguments[0])) { $storyId = ltrim($arguments[0], "# "); } // Look for the story id in the current git branch name if (!$storyId) { $storyId = GitPlanbox_Util::currentStoryId(); } // Require a storyId to move forward if (!$storyId) { throw new Exception("I was unable to auto-detect which story id you would like to pause, please specify it like this: 'git planbox pause 12345'"); } // Fetch the story from Planbox $session = GitPlanbox_Session::create(); try { $story = $session->post('get_story', array('story_id' => $storyId)); } catch (GitPlanbox_ApplicationErrorException $e) { throw new Exception("Unable to fetch story {$storyId} from Planbox."); } // Arrange tasks in useful ways $tasksByStatus = array(); $tasksByTaskId = array(); foreach ($story->tasks as $task) { $tasksByStatus[$task->status][] = $task; $tasksByTaskId[$task->id] = $task; } // If there are no inprogress tasks, tell the user if (!isset($tasksByStatus['inprogress']) || count($tasksByStatus['inprogress']) < 1) { print "There are no tasks currently in-progress for story {$storyId}.\n"; return 0; } // If there's only one status:inprogress task, set it to status:pending if (count($tasksByStatus['inprogress']) == 1) { $task = array_shift($tasksByStatus['inprogress']); $this->_pauseTimerForTask($session, $storyId, $task->id); return 0; } // Otherwise ask the user which task they'd like to pause (allow pausing all too!) foreach ($tasksByStatus['inprogress'] as $task) { printf("%8s %10s - %-50s\n", "#{$task->id}", $task->status, $task->name); } $taskId = GitPlanbox_Util::readline("Which task id would you like to pause? You can also say all... "); if (strtolower($taskId) === 'all') { foreach ($tasksByStatus['inprogress'] as $task) { $this->_pauseTimerForTask($session, $storyId, $task->id); } } elseif (isset($tasksByTaskId[intVal($taskId)])) { $task = $tasksByTaskId[intVal($taskId)]; $this->_pauseTimerForTask($session, $storyId, $task->id); } else { if (!$storyId) { throw new Exception("I didn't understand that story id."); } } return 0; }
public function run($arguments, CLImaxController $cliController) { // First try the storyId specified on the command line $storyId = NULL; if (isset($arguments[0])) { $storyId = ltrim($arguments[0], "# "); } // If no storyId was specified on the command line, try to // parse it out of the current branch name if (!$storyId) { $storyId = GitPlanbox_Util::currentStoryId(); } // If we still don't have a story id, tell the user to please // specify one if (!$storyId) { throw new Exception("Couldn't auto-detect a story_id, please specify one like so: git planbox status 12345"); } // Get the story from Planbox $session = GitPlanbox_Session::create(); try { $story = $session->post('get_story', array('story_id' => $storyId)); } catch (GitPlanbox_ApplicationErrorException $e) { throw new Exception("Unable to fetch story {$storyId} from Planbox."); } // Find all SHAs referenced in the comments $shas = array(); foreach ($story->comments as $comment) { $commentShas = array(); preg_match_all('/[a-fA-F0-9]{40}/', $comment->text, $commentShas); if (isset($commentShas[0])) { $shas = array_merge($shas, $commentShas[0]); } } $shas = array_unique($shas); // Run `git branch --contains {$sha}` on each sha to see where it's been merged to $infos = array(); foreach ($shas as $sha) { $info = array('sha' => $sha); // Get the commit message for this sha $command = "git log -1 --pretty=oneline --abbrev-commit {$sha} 2>&1"; $output = ''; exec($command, $output, $returnCode); $commitMessage = array_shift($output); $info['message'] = $commitMessage; // Find out which git branches contain this sha $command = "git branch -a --contains {$sha} 2>&1"; $output = ''; exec($command, $output, $returnCode); if ($returnCode === 0) { $branches = array(); $branchesText = trim(preg_replace('/\\s+/', ' ', str_replace('*', '', implode("\n", $output)))); if ($branchesText) { $branches = explode("\n", $branchesText); } $info['branches'] = $branches; } else { $info['branches'] = array(); } array_push($infos, $info); } // Pretty print the story status and the branches it's merged to print "Story #{$storyId} is currently: {$story->status}\n"; if (count($infos) > 0) { print "Commits:\n"; } foreach ($infos as $info) { if (isset($info['branches']) && $info['branches']) { print " \"{$info['message']}\" is on branches: " . implode(', ', $info['branches']) . "\n"; } else { print " \"{$info['message']}\" is not on any branches.\n"; } } // Success! return 0; }