Skip to content

Commit

Permalink
Adds feature to check if a branch name exists in a repository without…
Browse files Browse the repository at this point in the history
… cloning it. (#202)
  • Loading branch information
hostep authored May 11, 2023
1 parent 11a3dfc commit 9fea656
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Gitonomy/Git/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ public static function isValidRepository($url, array $options = [])
return $process->isSuccessFul();
}

/**
* Checks the validity of a git repository url without cloning it and
* check if a certain branch exists in that repository.
*
* This will use the `ls-remote` command of git against the given url.
* Usually, this command returns 0 when successful, and 128 when the
* repository is not found.
*
* @param string $url url of repository to check
* @param string $branchName name of branch to check
* @param array $options options for Repository creation
*
* @return bool true if url is valid and branch exists
*/
public static function isValidRepositoryAndBranch($url, $branchName, array $options = [])
{
$process = static::getProcess('ls-remote', ['--heads', $url, $branchName], $options);

$process->run();
$processOutput = $process->getOutput();

return $process->isSuccessFul() && strpos($processOutput, $branchName) !== false;
}

/**
* Clone a repository to a local path.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Gitonomy/Git/Tests/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ public function testCheckInvalidRepository()
$this->assertFalse(Admin::isValidRepository($url));
}

/**
* @dataProvider provideFoobar
*/
public function testCheckValidRepositoryAndBranch($repository)
{
$url = $repository->getGitDir();
$this->assertTrue(Admin::isValidRepositoryAndBranch($url, 'master'));
}

/**
* @dataProvider provideFoobar
*/
public function testCheckInvalidRepositoryAndBranch($repository)
{
$url = $repository->getGitDir();
$this->assertFalse(Admin::isValidRepositoryAndBranch($url, 'invalid-branch-name'));
}

public function testExistingFile()
{
$this->expectException(RuntimeException::class);
Expand Down

0 comments on commit 9fea656

Please sign in to comment.