|
12 | 12 |
|
13 | 13 | namespace Gitonomy\Git\Reference;
|
14 | 14 |
|
| 15 | +use Gitonomy\Git\Exception\ProcessException; |
15 | 16 | use Gitonomy\Git\Exception\RuntimeException;
|
16 | 17 | use Gitonomy\Git\Reference;
|
| 18 | +use Gitonomy\Git\Util\StringHelper; |
17 | 19 |
|
18 | 20 | /**
|
19 | 21 | * Representation of a branch reference.
|
@@ -53,6 +55,49 @@ public function isLocal()
|
53 | 55 | return $this->local;
|
54 | 56 | }
|
55 | 57 |
|
| 58 | + /** |
| 59 | + * Check if this branch is merged to a destination branch |
| 60 | + * Optionally, check only with remote branches. |
| 61 | + * |
| 62 | + * @param string $destinationBranchName |
| 63 | + * @param bool $compareOnlyWithRemote |
| 64 | + * |
| 65 | + * @return null|bool |
| 66 | + */ |
| 67 | + public function isMergedTo($destinationBranchName = 'master', $compareOnlyWithRemote = false) |
| 68 | + { |
| 69 | + $arguments = ['-a']; |
| 70 | + |
| 71 | + if ($compareOnlyWithRemote) { |
| 72 | + $arguments = ['-r']; |
| 73 | + } |
| 74 | + |
| 75 | + $arguments[] = '--merged'; |
| 76 | + $arguments[] = $destinationBranchName; |
| 77 | + |
| 78 | + try { |
| 79 | + $result = $this->repository->run('branch', $arguments); |
| 80 | + } catch (ProcessException $e) { |
| 81 | + throw new RuntimeException( |
| 82 | + sprintf('Cannot determine if merged to the branch "%s"', $destinationBranchName), |
| 83 | + $e->getCode(), |
| 84 | + $e |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + if (!$result) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + $output = explode("\n", trim(str_replace(['*', 'remotes/'], '', $result))); |
| 93 | + $filtered_output = array_filter($output, static function ($v) { |
| 94 | + return false === StringHelper::strpos($v, '->'); |
| 95 | + }); |
| 96 | + $trimmed_output = array_map('trim', $filtered_output); |
| 97 | + |
| 98 | + return in_array($this->getName(), $trimmed_output, true); |
| 99 | + } |
| 100 | + |
56 | 101 | private function detectBranchType()
|
57 | 102 | {
|
58 | 103 | if (null === $this->local) {
|
|
0 commit comments