From ea9275e64e27c1e124e3c0bdc6881fcc0f8070e4 Mon Sep 17 00:00:00 2001 From: Shane Perera Date: Wed, 6 Nov 2019 17:38:29 +1100 Subject: [PATCH 1/2] Allow to check if a branch is merged to the master (or another branch) --- src/Gitonomy/Git/Reference/Branch.php | 42 ++++++++++++++++++++++ tests/Gitonomy/Git/Tests/ReferenceTest.php | 24 +++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/Gitonomy/Git/Reference/Branch.php b/src/Gitonomy/Git/Reference/Branch.php index 0f263a7..b7e7b1d 100644 --- a/src/Gitonomy/Git/Reference/Branch.php +++ b/src/Gitonomy/Git/Reference/Branch.php @@ -12,8 +12,10 @@ namespace Gitonomy\Git\Reference; +use Gitonomy\Git\Exception\ProcessException; use Gitonomy\Git\Exception\RuntimeException; use Gitonomy\Git\Reference; +use Gitonomy\Git\Util\StringHelper; /** * Representation of a branch reference. @@ -53,6 +55,46 @@ public function isLocal() return $this->local; } + /** + * + * Check if this branch is merged to a destination branch + * Optionally, check only with remote branches + * + * @param string $destinationBranchName + * @param bool $onlyRemote + * + * @return null|bool + */ + public function isMergedTo($destinationBranchName = 'master', $compareOnlyWithRemote = false) + { + $arguments = ['-a']; + + if ($compareOnlyWithRemote) { + $arguments = ['-r']; + } + + $arguments[] = '--merged'; + $arguments[] = $destinationBranchName; + + try { + $result = $this->repository->run('branch', $arguments); + } catch (ProcessException $e) { + return null; + } + + if (!$result) { + return null; + } + + $output = explode("\n", trim(str_replace(['*', 'remotes/'], '', $result))); + $output = array_filter($output, function ($v) { + return false === StringHelper::strpos($v, '->'); + }); + $output = array_map('trim', $output); + + return (in_array($this->getName(), $output)); + } + private function detectBranchType() { if (null === $this->local) { diff --git a/tests/Gitonomy/Git/Tests/ReferenceTest.php b/tests/Gitonomy/Git/Tests/ReferenceTest.php index d1c00fc..9a7f4a0 100644 --- a/tests/Gitonomy/Git/Tests/ReferenceTest.php +++ b/tests/Gitonomy/Git/Tests/ReferenceTest.php @@ -202,4 +202,28 @@ public function testCreateAndDeleteBranch($repository) $branch->delete(); $this->assertFalse($references->hasBranch('foobar'), 'Branch foobar removed'); } + + /** + * @dataProvider provideFoobar + */ + public function testIsBranchMergedToMaster() + { + $repository = self::createFoobarRepository(false); + + $master = $repository->getReferences()->getBranch('master'); + $references = $repository->getReferences(); + $branch = $references->createBranch('foobar-new', $master->getCommit()->getHash()); + + $this->assertTrue($branch->isMergedTo('master')); + + $wc = $repository->getWorkingCopy(); + $wc->checkout('foobar-new'); + + $file = $repository->getWorkingDir().'/foobar-test.txt'; + file_put_contents($file, 'test'); + $repository->run('add', [$file]); + $repository->run('commit', ['-m', 'foobar-test.txt updated']); + + $this->assertFalse($branch->isMergedTo('master')); + } } From 7f7f2e8afcb37e529c171897b624991f802632cf Mon Sep 17 00:00:00 2001 From: Shane Perera Date: Wed, 6 Nov 2019 17:39:15 +1100 Subject: [PATCH 2/2] Fix typo --- src/Gitonomy/Git/Reference/Branch.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Gitonomy/Git/Reference/Branch.php b/src/Gitonomy/Git/Reference/Branch.php index b7e7b1d..23a3fa2 100644 --- a/src/Gitonomy/Git/Reference/Branch.php +++ b/src/Gitonomy/Git/Reference/Branch.php @@ -61,7 +61,7 @@ public function isLocal() * Optionally, check only with remote branches * * @param string $destinationBranchName - * @param bool $onlyRemote + * @param bool $compareOnlyWithRemote * * @return null|bool */