From 3ee8946a2719c28b39bd7c8027d81075be2de071 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Sat, 27 Aug 2016 17:43:21 -0300 Subject: [PATCH] Check branch sync status before squash --- src/Helper/GitHelper.php | 80 +++++++++++++++------------------- tests/Helper/GitHelperTest.php | 2 +- 2 files changed, 36 insertions(+), 46 deletions(-) diff --git a/src/Helper/GitHelper.php b/src/Helper/GitHelper.php index eef59687..cd866b18 100644 --- a/src/Helper/GitHelper.php +++ b/src/Helper/GitHelper.php @@ -12,6 +12,7 @@ namespace Gush\Helper; use Gush\Exception\CannotSquashMultipleAuthors; +use Gush\Exception\MergeWorkflowException; use Gush\Exception\UserException; use Gush\Exception\WorkingTreeIsNotReady; use Gush\Operation\RemoteMergeOperation; @@ -399,7 +400,7 @@ public function mergeBranch($base, $sourceBranch, $commitMessage, $fastForward = 'allow_failures' => false, ], [ - 'line' => ['git', 'commit', '-F', $tmpName], + 'line' => ['git', 'commit', '--file', $tmpName], 'allow_failures' => false, ], ] @@ -446,7 +447,7 @@ public function mergeBranchWithLog($base, $sourceBranch, $commitMessage, $source $tmpName = $this->filesystemHelper->newTempFilename(); file_put_contents($tmpName, $commitMessage); - $this->processHelper->runCommand(['git', 'commit', '-F', $tmpName]); + $this->processHelper->runCommand(['git', 'commit', '--file', $tmpName]); return trim($this->processHelper->runCommand('git rev-parse HEAD')); } @@ -456,15 +457,7 @@ public function addNotes($notes, $commitHash, $ref) $tmpName = $this->filesystemHelper->newTempFilename(); file_put_contents($tmpName, $notes); - $commands = [ - 'git', - 'notes', - '--ref='.$ref, - 'add', - '-F', - $tmpName, - $commitHash, - ]; + $commands = ['git', 'notes', '--ref='.$ref, 'add', '--file', $tmpName, $commitHash]; $this->processHelper->runCommand($commands, true); } @@ -474,7 +467,7 @@ public function pushToRemote($remote, $ref, $setUpstream = false, $force = false $command = ['git', 'push', $remote]; if ($setUpstream) { - $command[] = '-u'; + $command[] = '--set-upstream'; } if ($force) { @@ -608,21 +601,26 @@ public function squashCommits($base, $branchName, $ignoreMultipleAuthors = false // Get commits only in the branch but not in base (in reverse order) // we can't use --max-count here because that is applied before the reversing! // - // using git-log works better then finding the fork-point with git-merge-base + // using git-log works better than finding the fork-point with git-merge-base // because this protects against edge cases were there is no valid fork-point - $firstCommitHash = StringUtil::splitLines($this->processHelper->runCommand( - [ - 'git', - '--no-pager', - 'log', - '--oneline', - '--no-color', - '--format=%H', - '--reverse', - $base.'..'.$branchName, - ] - ))[0]; + $firstCommitHash = StringUtil::splitLines($this->processHelper->runCommand([ + 'git', + '--no-pager', + 'log', + '--oneline', + '--no-color', + '--format=%H', + '--reverse', + $base.'..'.$branchName, + ]))[0]; + + $currentBaseHeadCommit = $this->processHelper->runCommand(['git', 'rev-parse', $base]); + $lastKnownCommonCommit = $this->processHelper->runCommand(['git', 'merge-base', '--fork-point', $base, $branchName]); + + if ($currentBaseHeadCommit !== $lastKnownCommonCommit) { + throw new MergeWorkflowException(sprintf('Failed while trying to perform merge against "%s", history is out of sync.', $base)); + } // 0=author anything higher then 0 is the full body $commitData = StringUtil::splitLines( @@ -643,23 +641,7 @@ public function squashCommits($base, $branchName, $ignoreMultipleAuthors = false $message = implode("\n", $commitData); $this->reset($base); - $this->commit( - $message, - [ - 'a', - '-author' => $author, - ] - ); - - try { - // Ensure squashed commits don't introduce regressions at base branch - $this->processHelper->runCommand(['git', 'pull', '--rebase', $base]); - } catch (\Exception $e) { - // Error, abort the rebase process - $this->processHelper->runCommand(['git', 'rebase', '--abort'], true); - - throw new \RuntimeException(sprintf('Git rebase failed while trying to synchronize history against "%s".', $base), 0, $e); - } + $this->commit($message, ['all', 'author' => $author]); } public function syncWithRemote($remote, $branchName = null) @@ -693,9 +675,17 @@ public function commit($message, array $options = []) foreach ($options as $option => $value) { if (is_int($option)) { - $params[] = '-'.$value; + if (1 === strlen($value)) { + $params[] = '-'.$value; + } else { + $params[] = '--'.$value; + } } else { - $params[] = '-'.$option; + if (1 === strlen($option)) { + $params[] = '-'.$option; + } else { + $params[] = '--'.$option; + } $params[] = $value; } } @@ -703,7 +693,7 @@ public function commit($message, array $options = []) $tmpName = $this->filesystemHelper->newTempFilename(); file_put_contents($tmpName, $message); - $this->processHelper->runCommand(array_merge(['git', 'commit', '-F', $tmpName], $params)); + $this->processHelper->runCommand(array_merge(['git', 'commit', '--file', $tmpName], $params)); } /** diff --git a/tests/Helper/GitHelperTest.php b/tests/Helper/GitHelperTest.php index 559c80ad..84ad536a 100644 --- a/tests/Helper/GitHelperTest.php +++ b/tests/Helper/GitHelperTest.php @@ -176,7 +176,7 @@ public function merges_remote_branch_in_clean_wc() 'allow_failures' => false, ], [ - 'line' => ['git', 'commit', '-F', $tmpName], + 'line' => ['git', 'commit', '--file', $tmpName], 'allow_failures' => false, ], ]