Skip to content

Adds feature to check if a branch name exists in a repository without… #202

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 1 commit into from
May 11, 2023
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
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