Skip to content

Commit 9fea656

Browse files
authored
Adds feature to check if a branch name exists in a repository without cloning it. (#202)
1 parent 11a3dfc commit 9fea656

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/Gitonomy/Git/Admin.php

+24
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ public static function isValidRepository($url, array $options = [])
6767
return $process->isSuccessFul();
6868
}
6969

70+
/**
71+
* Checks the validity of a git repository url without cloning it and
72+
* check if a certain branch exists in that repository.
73+
*
74+
* This will use the `ls-remote` command of git against the given url.
75+
* Usually, this command returns 0 when successful, and 128 when the
76+
* repository is not found.
77+
*
78+
* @param string $url url of repository to check
79+
* @param string $branchName name of branch to check
80+
* @param array $options options for Repository creation
81+
*
82+
* @return bool true if url is valid and branch exists
83+
*/
84+
public static function isValidRepositoryAndBranch($url, $branchName, array $options = [])
85+
{
86+
$process = static::getProcess('ls-remote', ['--heads', $url, $branchName], $options);
87+
88+
$process->run();
89+
$processOutput = $process->getOutput();
90+
91+
return $process->isSuccessFul() && strpos($processOutput, $branchName) !== false;
92+
}
93+
7094
/**
7195
* Clone a repository to a local path.
7296
*

tests/Gitonomy/Git/Tests/AdminTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ public function testCheckInvalidRepository()
155155
$this->assertFalse(Admin::isValidRepository($url));
156156
}
157157

158+
/**
159+
* @dataProvider provideFoobar
160+
*/
161+
public function testCheckValidRepositoryAndBranch($repository)
162+
{
163+
$url = $repository->getGitDir();
164+
$this->assertTrue(Admin::isValidRepositoryAndBranch($url, 'master'));
165+
}
166+
167+
/**
168+
* @dataProvider provideFoobar
169+
*/
170+
public function testCheckInvalidRepositoryAndBranch($repository)
171+
{
172+
$url = $repository->getGitDir();
173+
$this->assertFalse(Admin::isValidRepositoryAndBranch($url, 'invalid-branch-name'));
174+
}
175+
158176
public function testExistingFile()
159177
{
160178
$this->expectException(RuntimeException::class);

0 commit comments

Comments
 (0)