diff --git a/freepbx_git.php b/freepbx_git.php index 5f863da..e2e4048 100755 --- a/freepbx_git.php +++ b/freepbx_git.php @@ -48,6 +48,7 @@ array('--setup', 'Setup new freepbx dev tools environment (use --force to re-setup environment)'), array('--clean', 'Prunes all tags and branches that do no exist on the remote, can be used with the -m command for individual'), array('--refresh', 'Updates all local modules with their remote changes (!!you will lose all untracked branches!!)'), + array('--force', 'With --refresh, force-update local tags on conflict without prompting (also used by --setup)'), array('--refreshhard', 'Updates all local modules with their remote changes (!!you will lose all untracked branches and work!!)'), array('--addmergedriver', 'Updates/Adds Relevant Merge Drivers'), array('--switch=', 'Switch all local modules to branch'), @@ -233,15 +234,22 @@ } if(isset($options['refresh'])) { + // --force (or -y) forces tag updates without prompting. Otherwise ask once + // upfront whether to force automatically or be asked on each conflict. + $force = isset($options['force']) || isset($options['y']); + if(!$force) { + $answer = freepbx::getInput("On tag conflicts, force tag updates automatically? (y = always force / n = ask for each repo)", 'n'); + $force = (strtolower(trim($answer)) == 'y'); + } foreach(glob($directory."/*", GLOB_ONLYDIR) as $dir) { - freepbx::refreshRepo($dir); + freepbx::refreshRepo($dir,'origin',null,false,$force); } exit(0); } if(isset($options['refreshhard'])) { foreach(glob($directory."/*", GLOB_ONLYDIR) as $dir) { - freepbx::refreshRepo($dir,'origin',null,true); + freepbx::refreshRepo($dir,'origin',null,true,true); } exit(0); } diff --git a/libraries/Git.php b/libraries/Git.php index c76230a..1170948 100644 --- a/libraries/Git.php +++ b/libraries/Git.php @@ -723,9 +723,9 @@ public function merge($branch) { * @access public * @return string */ - public function fetch() { + public function fetch($force = false) { $this->run("fetch -q"); - $this->run("fetch --tags -q"); + $this->run("fetch --tags -q" . ($force ? " --force" : "")); return true; } diff --git a/libraries/freepbx.php b/libraries/freepbx.php index 8fe60bf..8652008 100644 --- a/libraries/freepbx.php +++ b/libraries/freepbx.php @@ -50,8 +50,26 @@ public static function switchBranch($directory,$branch) { $repo->clean(true,true); freepbx::out("Done"); freepbx::outn("\tFetching Changes..."); - $repo->fetch(); - freepbx::out("Done"); + try { + $repo->fetch(); + freepbx::out("Done"); + } catch (Exception $e) { + // A plain "fetch --tags" aborts with "would clobber existing tag" + // when a remote tag has been moved/recreated. Force only if told to, + // otherwise ask before forcing. + freepbx::out("Failed"); + if(!$force) { + $answer = freepbx::getInput("\tFetch failed (a remote tag may have been moved). Force update of local tags? (y/n)", 'n'); + $force = (strtolower(trim($answer)) == 'y'); + } + if($force) { + freepbx::outn("\tForcing tag update..."); + $repo->fetch(true); + freepbx::out("Done"); + } else { + freepbx::out("\tError: unable to fetch, local tags conflict with the remote. Skipping forced update."); + } + } freepbx::outn("\tChecking out ".$branch." ..."); try { $repo->checkout($branch); @@ -87,9 +105,11 @@ public static function switchBranch($directory,$branch) { * @param string $directory Location of repo * @param string $remote The name of the remote origin * @param string $final_branch The final branch to checkout after updating (null means whatever it was on before) + * @param bool $hard True to reset --hard instead of stashing local changes + * @param bool $force True to force-update local tags on conflict without prompting * @return bool */ - public static function refreshRepo($directory, $remote = 'origin', $final_branch = null, $hard = false) { + public static function refreshRepo($directory, $remote = 'origin', $final_branch = null, $hard = false, $force = false) { $rawname = basename($directory); if($rawname === 'framework') { freepbx::out("Refusing to refresh framework as it will break everything. Do it manually");