diff --git a/bin/git-close-pull-request b/bin/git-close-pull-request index 4a8ca7a..ce2b4c8 100755 --- a/bin/git-close-pull-request +++ b/bin/git-close-pull-request @@ -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);