git: Ensure branches are in sync with upstream

This commit is contained in:
Oliver Davies 2020-05-27 10:55:46 +01:00
parent 35834bef8f
commit 54b762b734

View file

@ -42,7 +42,8 @@ class ClosesPullRequests
$this->confirmCiStatusIsPassing();
// TODO: Check that the current branch has a tracking branch.
$this->fetchOrigin();
// TODO: Ensure both branches are up to date.
$this->ensureFeatureBranchInSync();
$this->ensureTargetBranchInSync();
$this->checkoutTargetBranch();
$this->mergeLocalBranch();
$this->pushTargetBranch();
@ -72,6 +73,50 @@ class ClosesPullRequests
exec("git fetch origin");
}
private function ensureTargetBranchInSync(): void
{
$this->ensureBranchInSyncWithUpstream(
$this->targetBranch,
$this->targetBranch
);
}
private function ensureFeatureBranchInSync(): void
{
$this->ensureBranchInSyncWithUpstream(
$this->localBranch,
$this->remoteBranch
);
}
private function ensureBranchInSyncWithUpstream(
string $localBranch,
string $remoteBranch
): void {
echo sprintf(
'Ensuring that %s is in sync with its upstream...',
$localBranch
) . PHP_EOL;
$localCommitTip = $this->tipCommitOfBranch($localBranch);
$remoteCommitTip = $this->tipCommitOfBranch(sprintf(
'origin/%s',
$remoteBranch
));
if ($localCommitTip != $remoteCommitTip) {
die(sprintf(
'Branch %s was out of date, needs rebasing. Aborting.',
$localBranch
));
}
}
private function tipCommitOfBranch(string $branchName): string
{
return exec(sprintf('git rev-parse %s', $branchName));
}
private function checkoutTargetBranch(): void
{
print sprintf('Checking out %s...' . PHP_EOL, $this->targetBranch);