From 313cb6a5b370e6fe3eee9edd3521de72c9389fee Mon Sep 17 00:00:00 2001 From: Stig Lindqvist Date: Wed, 13 Jun 2012 14:02:16 +1200 Subject: [PATCH] BUGFIX: CLI error on LoadModulesTask_GitLoader::update() This fixes: - git would complain when doing git checkout {branch name} when already on {branch name} - the current branch was not calculated correctly - removed unused variable --- tools/LoadModulesTask.php | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/tools/LoadModulesTask.php b/tools/LoadModulesTask.php index d7c446e..f609ac4 100644 --- a/tools/LoadModulesTask.php +++ b/tools/LoadModulesTask.php @@ -119,7 +119,6 @@ class LoadModulesTask extends SilverStripeBuildTask { protected function loadModule($moduleName, $url, $devBuild = false, $storeLocally=false, $usePiston=false) { $git = strrpos($url, '.git') == (strlen($url) - 4); $branch = 'master'; - $cmd = ''; $originalName = $moduleName; @@ -365,24 +364,42 @@ class LoadModulesTask_GitLoader extends LoadModulesTask_Loader { return trim($this->callingTask->exec("cd $this->name && $statCmd && cd \"$currentDir\"", true)); } - function update($overwrite = true) { + /** + * + * @param boolean $overwrite + */ + public function update($overwrite = true) { $branch = $this->branch; $currentDir = getcwd(); $commitId = null; - if (strpos($branch, LoadModulesTask::MODULE_SEPARATOR) > 0) { + // Extract the branch and the commit to use for the update + if(strpos($branch, LoadModulesTask::MODULE_SEPARATOR) > 0) { $commitId = substr($branch, strpos($branch, LoadModulesTask::MODULE_SEPARATOR) + 1); $branch = substr($branch, 0, strpos($branch, LoadModulesTask::MODULE_SEPARATOR)); } - $currentBranch = trim($this->callingTask->exec("cd $moduleName && git branch && cd \"$currentDir\"", true)); - + // Finds the current checked out branch + $cliBranches = trim($this->callingTask->exec("cd $this->name && git branch && cd \"$currentDir\"", true)); + $currentBranch = 'master'; + foreach(explode(PHP_EOL, $cliBranches) as $cliBranch) { + if(strstr($cliBranch, '* ') !== false) { + $currentBranch = str_replace('* ', '', $cliBranch); + break; + } + } + $overwriteOpt = $overwrite ? '-f' : ''; - $this->callingTask->exec("cd $this->name && git checkout $overwriteOpt $branch && git pull origin $branch && cd \"$currentDir\""); - + // We are already on the target branch, don't checkout it again + if($currentBranch == $branch) { + $this->callingTask->exec("cd $this->name && git pull origin $branch && cd \"$currentDir\""); + } else { + $this->callingTask->exec("cd $this->name && git checkout $overwriteOpt $branch && git pull origin $branch && cd \"$currentDir\""); + } + if ($commitId) { - $this->callingTask->exec("cd $this->name && git pull && git checkout $commitId && cd \"$currentDir\""); + $this->callingTask->exec("cd $this->name && git checkout $commitId && cd \"$currentDir\""); } }