From a06a6badaf45c409051d5723e1db08dfc424347d Mon Sep 17 00:00:00 2001 From: Anatolij Vasilev Date: Thu, 6 Oct 2022 15:46:13 +0200 Subject: [PATCH 1/5] added code from PR #151 --- src/Gitonomy/Git/Reference/Branch.php | 46 ++++++++++++++++++++++ tests/Gitonomy/Git/Tests/ReferenceTest.php | 24 +++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/Gitonomy/Git/Reference/Branch.php b/src/Gitonomy/Git/Reference/Branch.php index 0f263a76..dab18831 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,50 @@ 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 $compareOnlyWithRemote + * + * @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) { + throw new RuntimeException( + sprintf('Cannot determine if merged to the branch "%s"', $destinationBranchName), + $e->getCode(), + $e + ); + } + + if (!$result) { + return false; + } + + $output = explode("\n", trim(str_replace(['*', 'remotes/'], '', $result))); + $filtered_output = array_filter($output, static function ($v) { + return false === StringHelper::strpos($v, '->'); + }); + $trimmed_output = array_map('trim', $filtered_output); + + return (in_array($this->getName(), $trimmed_output, true)); + } + private function detectBranchType() { if (null === $this->local) { diff --git a/tests/Gitonomy/Git/Tests/ReferenceTest.php b/tests/Gitonomy/Git/Tests/ReferenceTest.php index d24a5e64..862dff7e 100644 --- a/tests/Gitonomy/Git/Tests/ReferenceTest.php +++ b/tests/Gitonomy/Git/Tests/ReferenceTest.php @@ -209,4 +209,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 67ac35f867a5ec853e65222d4edaf3c6e1b3645f Mon Sep 17 00:00:00 2001 From: Anatolij Vasilev Date: Thu, 6 Oct 2022 16:26:46 +0200 Subject: [PATCH 2/5] added user credentials which are needed for the next commands --- tests/Gitonomy/Git/Tests/ReferenceTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Gitonomy/Git/Tests/ReferenceTest.php b/tests/Gitonomy/Git/Tests/ReferenceTest.php index 862dff7e..3e4176e4 100644 --- a/tests/Gitonomy/Git/Tests/ReferenceTest.php +++ b/tests/Gitonomy/Git/Tests/ReferenceTest.php @@ -219,6 +219,10 @@ public function testIsBranchMergedToMaster() $master = $repository->getReferences()->getBranch('master'); $references = $repository->getReferences(); + + $repository->run('config', ['--local', 'user.name', '"Unit Test"']); + $repository->run('config', ['--local', 'user.email', '"unit_test@unit-test.com"']); + $branch = $references->createBranch('foobar-new', $master->getCommit()->getHash()); $this->assertTrue($branch->isMergedTo('master')); From 06b970d651a3035f2020a44b5fc980bd14eb1104 Mon Sep 17 00:00:00 2001 From: Anatolij Vasilev Date: Thu, 6 Oct 2022 16:28:20 +0200 Subject: [PATCH 3/5] stylistic changes to satisfy style-ci --- src/Gitonomy/Git/Reference/Branch.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Gitonomy/Git/Reference/Branch.php b/src/Gitonomy/Git/Reference/Branch.php index dab18831..571b4ffe 100644 --- a/src/Gitonomy/Git/Reference/Branch.php +++ b/src/Gitonomy/Git/Reference/Branch.php @@ -56,7 +56,6 @@ public function isLocal() } /** - * * Check if this branch is merged to a destination branch * Optionally, check only with remote branches * @@ -96,7 +95,7 @@ public function isMergedTo($destinationBranchName = 'master', $compareOnlyWithRe }); $trimmed_output = array_map('trim', $filtered_output); - return (in_array($this->getName(), $trimmed_output, true)); + return in_array($this->getName(), $trimmed_output, true); } private function detectBranchType() From d7fb3bbfb8db782d758e0e3d38074441dc54d0f5 Mon Sep 17 00:00:00 2001 From: Anatolij Vasilev Date: Thu, 6 Oct 2022 16:30:06 +0200 Subject: [PATCH 4/5] moved user credentials to the top --- tests/Gitonomy/Git/Tests/ReferenceTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Gitonomy/Git/Tests/ReferenceTest.php b/tests/Gitonomy/Git/Tests/ReferenceTest.php index 3e4176e4..0f975577 100644 --- a/tests/Gitonomy/Git/Tests/ReferenceTest.php +++ b/tests/Gitonomy/Git/Tests/ReferenceTest.php @@ -217,12 +217,12 @@ public function testIsBranchMergedToMaster() { $repository = self::createFoobarRepository(false); - $master = $repository->getReferences()->getBranch('master'); - $references = $repository->getReferences(); - $repository->run('config', ['--local', 'user.name', '"Unit Test"']); $repository->run('config', ['--local', 'user.email', '"unit_test@unit-test.com"']); + $master = $repository->getReferences()->getBranch('master'); + $references = $repository->getReferences(); + $branch = $references->createBranch('foobar-new', $master->getCommit()->getHash()); $this->assertTrue($branch->isMergedTo('master')); From 0fdf03c386d7888d37ad86f7ede5a0163b815fe3 Mon Sep 17 00:00:00 2001 From: Anatolij Vasilev Date: Thu, 6 Oct 2022 16:31:55 +0200 Subject: [PATCH 5/5] stylistic changes to satisfy style-ci --- 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 571b4ffe..0d27fb88 100644 --- a/src/Gitonomy/Git/Reference/Branch.php +++ b/src/Gitonomy/Git/Reference/Branch.php @@ -57,7 +57,7 @@ public function isLocal() /** * Check if this branch is merged to a destination branch - * Optionally, check only with remote branches + * Optionally, check only with remote branches. * * @param string $destinationBranchName * @param bool $compareOnlyWithRemote