Skip to content

Commit

Permalink
Added new static method cloneRepository (alias for `git clone <URL> <…
Browse files Browse the repository at this point in the history
…dir>`)

Signed-off-by: Jan Pecha <[email protected]>
  • Loading branch information
janpecha committed Sep 12, 2013
1 parent f530479 commit 3baaf88
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/GitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,5 +523,81 @@ public static function init($directory)

return new static($repo);
}



/**
* Clones GIT repository from $url into $directory
* @param string
* @param string|NULL
* @return self
*/
public static function cloneRepository($url, $directory = NULL)
{
if($directory !== NULL && is_dir("$directory/.git"))
{
throw new GitException("Repo already exists in $directory.");
}

$cwd = getcwd();

if($directory === NULL)
{
$directory = self::extractRepositoryNameFromUrl($url);
$directory = "$cwd/$directory";
}
elseif(!self::isAbsolute($directory))
{
$directory = "$cwd/$directory";
}

exec('git clone -q ' . escapeshellarg($url) . ' ' . escapeshellarg($directory), $output, $returnCode);

if($returnCode !== 0)
{
throw new GitException("Git clone failed (directory $directory).");
}

return new static($directory);
}



/**
* @param string /path/to/repo.git | host.xz:foo/.git | ...
* @return string repo | foo | ...
*/
public static function extractRepositoryNameFromUrl($url)
{
// /path/to/repo.git => repo
// host.xz:foo/.git => foo
$directory = rtrim($url, '/');
if(substr($directory, -5) === '/.git')
{
$directory = substr($directory, 0, -5);
}

$directory = basename($directory, '.git');

if(($pos = strrpos($directory, ':')) !== FALSE)
{
$directory = substr($directory, $pos + 1);
}

return $directory;
}



/**
* Is path absolute?
* Method from Nette\Utils\FileSystem
* @link https://github.com/nette/nette/blob/master/Nette/Utils/FileSystem.php
* @return bool
*/
public static function isAbsolute($path)
{
return (bool) preg_match('#[/\\\\]|[a-zA-Z]:[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $path);
}
}

10 changes: 10 additions & 0 deletions src/IGit.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ function commit($message, $params = NULL);
* @return bool
*/
function isChanges();



/**
* Clones GIT repository from $url into $directory
* @param string
* @param string|NULL
* @return self
*/
static function cloneRepository($url, $directory = NULL);
}


Expand Down
57 changes: 57 additions & 0 deletions tests/GitPhp/clone.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
use Tester\Assert;
use Cz\Git\GitRepository;
require __DIR__ . '/bootstrap.php';
require __DIR__ . '/../../src/IGit.php';
require __DIR__ . '/../../src/GitRepository.php';

$cwd = getcwd();
chdir(TEMP_DIR);
$repo = GitRepository::cloneRepository('https://github.com/czproject/git-php.git');
chdir($cwd);

// repo already exists
#Assert::exception(function() {
# GitRepository::init(TEMP_DIR);
#}, 'Cz\Git\GitException');

Assert::same(realpath(TEMP_DIR . '/git-php'), $repo->getRepositoryPath());

// repo is empty
Assert::false($repo->isChanges());
Assert::same(array(
'v1.0.0', 'v1.0.1', 'v1.0.2', 'v2.0.0',
),$repo->getTags());

$branches = $repo->getBranches();
Assert::true(in_array('master', $branches));
Assert::true(in_array('remotes/origin/master', $branches));
Assert::true(in_array('remotes/origin/version-2.0.0', $branches));

Assert::same(array('master'),$repo->getLocalBranches());

// Specificky adresar
Tester\Helpers::purge(TEMP_DIR);
$repo = GitRepository::cloneRepository('https://github.com/czproject/git-php.git', TEMP_DIR . '/git-php2');

// repo already exists
#Assert::exception(function() {
# GitRepository::init(TEMP_DIR);
#}, 'Cz\Git\GitException');

Assert::same(realpath(TEMP_DIR . '/git-php2'), $repo->getRepositoryPath());

// repo is empty
Assert::false($repo->isChanges());
Assert::same(array(
'v1.0.0', 'v1.0.1', 'v1.0.2', 'v2.0.0',
),$repo->getTags());

$branches = $repo->getBranches();
Assert::true(in_array('master', $branches));
Assert::true(in_array('remotes/origin/master', $branches));
Assert::true(in_array('remotes/origin/version-2.0.0', $branches));

Assert::same(array('master'),$repo->getLocalBranches());


13 changes: 13 additions & 0 deletions tests/GitPhp/extractRepositoryNameFromUrl().phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
use Tester\Assert;
use Cz\Git\GitRepository;
require __DIR__ . '/bootstrap.php';
require __DIR__ . '/../../src/IGit.php';
require __DIR__ . '/../../src/GitRepository.php';

Assert::same('repo', GitRepository::extractRepositoryNameFromUrl('/path/to/repo.git'));
Assert::same('repo', GitRepository::extractRepositoryNameFromUrl('/path/to/repo/.git'));
Assert::same('foo', GitRepository::extractRepositoryNameFromUrl('host.xz:foo/.git'));
Assert::same('repo', GitRepository::extractRepositoryNameFromUrl('file:///path/to/repo.git/'));
Assert::same('git-php', GitRepository::extractRepositoryNameFromUrl('https://github.com/czproject/git-php.git'));

0 comments on commit 3baaf88

Please sign in to comment.