Skip to content

Commit

Permalink
Check branch sync status before squash
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Aug 27, 2016
1 parent d5f874b commit 3ee8946
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 46 deletions.
80 changes: 35 additions & 45 deletions src/Helper/GitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
],
]
Expand Down Expand Up @@ -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'));
}
Expand All @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -693,17 +675,25 @@ 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;
}
}

$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));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Helper/GitHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]
Expand Down

0 comments on commit 3ee8946

Please sign in to comment.