git: Ensure branches are in sync with upstream
This commit is contained in:
parent
35834bef8f
commit
54b762b734
|
@ -42,7 +42,8 @@ class ClosesPullRequests
|
||||||
$this->confirmCiStatusIsPassing();
|
$this->confirmCiStatusIsPassing();
|
||||||
// TODO: Check that the current branch has a tracking branch.
|
// TODO: Check that the current branch has a tracking branch.
|
||||||
$this->fetchOrigin();
|
$this->fetchOrigin();
|
||||||
// TODO: Ensure both branches are up to date.
|
$this->ensureFeatureBranchInSync();
|
||||||
|
$this->ensureTargetBranchInSync();
|
||||||
$this->checkoutTargetBranch();
|
$this->checkoutTargetBranch();
|
||||||
$this->mergeLocalBranch();
|
$this->mergeLocalBranch();
|
||||||
$this->pushTargetBranch();
|
$this->pushTargetBranch();
|
||||||
|
@ -72,6 +73,50 @@ class ClosesPullRequests
|
||||||
exec("git fetch origin");
|
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
|
private function checkoutTargetBranch(): void
|
||||||
{
|
{
|
||||||
print sprintf('Checking out %s...' . PHP_EOL, $this->targetBranch);
|
print sprintf('Checking out %s...' . PHP_EOL, $this->targetBranch);
|
||||||
|
|
Loading…
Reference in a new issue