From 27d51642c8d4522ad65eef35b5a5b13ecd96b34e Mon Sep 17 00:00:00 2001 From: Steve Buzonas Date: Thu, 26 Feb 2015 09:47:50 -0500 Subject: [PATCH] stash untracked and force checkout if discarding changes --- src/Composer/Downloader/GitDownloader.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index 60a896256..a566b5e85 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -218,12 +218,15 @@ class GitDownloader extends VcsDownloader */ protected function updateToCommit($path, $reference, $branch, $date) { + $discardChanges = $this->config->get('discard-changes'); + $force = true === $discardChanges || 'stash' === $discardChanges ? '-f ' : ''; + // This uses the "--" sequence to separate branch from file parameters. // // Otherwise git tries the branch name as well as file name. // If the non-existent branch is actually the name of a file, the file // is checked out. - $template = 'git checkout %s -- && git reset --hard %1$s --'; + $template = 'git checkout '.$force.'%s -- && git reset --hard %1$s --'; $branch = preg_replace('{(?:^dev-|(?:\.x)?-dev$)}i', '', $branch); $branches = null; @@ -237,7 +240,7 @@ class GitDownloader extends VcsDownloader && $branches && preg_match('{^\s+composer/'.preg_quote($reference).'$}m', $branches) ) { - $command = sprintf('git checkout -B %s %s -- && git reset --hard %2$s --', ProcessExecutor::escape($branch), ProcessExecutor::escape('composer/'.$reference)); + $command = sprintf('git checkout '.$force.'-B %s %s -- && git reset --hard %2$s --', ProcessExecutor::escape($branch), ProcessExecutor::escape('composer/'.$reference)); if (0 === $this->process->execute($command, $output, $path)) { return; } @@ -251,7 +254,7 @@ class GitDownloader extends VcsDownloader } $command = sprintf('git checkout %s --', ProcessExecutor::escape($branch)); - $fallbackCommand = sprintf('git checkout -B %s %s --', ProcessExecutor::escape($branch), ProcessExecutor::escape('composer/'.$branch)); + $fallbackCommand = sprintf('git checkout '.$force.'-B %s %s --', ProcessExecutor::escape($branch), ProcessExecutor::escape('composer/'.$branch)); if (0 === $this->process->execute($command, $output, $path) || 0 === $this->process->execute($fallbackCommand, $output, $path) ) { @@ -323,7 +326,7 @@ class GitDownloader extends VcsDownloader protected function stashChanges($path) { $path = $this->normalizePath($path); - if (0 !== $this->process->execute('git stash', $output, $path)) { + if (0 !== $this->process->execute('git stash --include-untracked', $output, $path)) { throw new \RuntimeException("Could not stash changes\n\n:".$this->process->getErrorOutput()); }