Skip to content

Commit 90d05de

Browse files
author
Alexandre Salomé
committed
Merge pull request #39 from MattKetmo/feat-check-remote
Check the validity of a git url without cloning it
2 parents 09d51b5 + 88818ec commit 90d05de

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/Gitonomy/Git/Admin.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ public static function init($path, $bare = true, array $options = array())
6060
return new Repository($path, $options);
6161
}
6262

63+
/**
64+
* Checks the validity of a git repository url without cloning it.
65+
*
66+
* This will use the `ls-remote` command of git against the given url.
67+
* Usually, this command returns 0 when successful, and 128 when the
68+
* repository is not found.
69+
*
70+
* @param string $url url of repository to check
71+
* @param array $options options for Repository creation
72+
*
73+
* @return boolean true if url is valid
74+
*/
75+
public static function isValidRepository($url, array $options = array())
76+
{
77+
$command = isset($options['command']) ? $options['command'] : 'git';
78+
$builder = ProcessBuilder::create(array($command, 'ls-remote'));
79+
$builder->add($url);
80+
81+
$process = $builder->getProcess();
82+
$process->run();
83+
84+
return $process->isSuccessFul();
85+
}
86+
6387
/**
6488
* Clone a repository to a local path.
6589
*

tests/Gitonomy/Git/Tests/AdminTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ public function testMirror($repository)
100100
}
101101
}
102102

103+
/**
104+
* @dataProvider provideFoobar
105+
*/
106+
public function testCheckValidRepository($repository)
107+
{
108+
$url = $repository->getGitDir();
109+
$this->assertTrue(Admin::isValidRepository($url));
110+
}
111+
112+
public function testCheckInvalidRepository()
113+
{
114+
$url = $this->tmpDir.'/invalid.git';
115+
mkdir($url);
116+
117+
$this->assertFalse(Admin::isValidRepository($url));
118+
}
119+
103120
/**
104121
* @expectedException RuntimeException
105122
*/

0 commit comments

Comments
 (0)