Skip to content

Method to check if a branch is merged (added code from PR #151) #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Gitonomy/Git/Reference/Branch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -53,6 +55,49 @@ 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) {
Expand Down
28 changes: 28 additions & 0 deletions tests/Gitonomy/Git/Tests/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,32 @@ public function testCreateAndDeleteBranch($repository)
$branch->delete();
$this->assertFalse($references->hasBranch('foobar'), 'Branch foobar removed');
}

/**
* @dataProvider provideFoobar
*/
public function testIsBranchMergedToMaster()
{
$repository = self::createFoobarRepository(false);

$repository->run('config', ['--local', 'user.name', '"Unit Test"']);
$repository->run('config', ['--local', 'user.email', '"[email protected]"']);

$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'));
}
}