Skip to content

Commit 4a26a0a

Browse files
author
Alexandre Salomé
committed
Merge pull request #58 from pitpit/feature-cloneBranchTo
Add ability to clone a repository and point head to a specific branch in one call
2 parents b402a7c + 6af6e1b commit 4a26a0a

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

doc/api/admin.rst

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ You can clone a repository from an URL by doing:
3535
3636
Default behavior is to clone in a bare repository.
3737

38+
You can also clone a repository and point it to a specific branch. In a non-bare repository, this branch will be checked out:
39+
40+
.. code-block:: php
41+
42+
// Clone to a bare repository
43+
$repository = Gitonomy\Git\Admin::cloneBranchTo('/tmp/gitlib', 'https://github.com/gitonomy/gitlib.git', 'a-branch');
44+
45+
// Clone to a non-bare repository
46+
$repository = Gitonomy\Git\Admin::cloneBranchTo('/tmp/gitlib', 'https://github.com/gitonomy/gitlib.git', 'a-branch' false);
47+
3848
Clone a Repository object
3949
-------------------------
4050

src/Gitonomy/Git/Admin.php

+21
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ public static function cloneTo($path, $url, $bare = true, array $options = array
8484
return static::cloneRepository($path, $url, $args, $options);
8585
}
8686

87+
/**
88+
* Clone a repository branch to a local path.
89+
*
90+
* @param string $path indicates where to clone repository
91+
* @param string $url url of repository to clone
92+
* @param string $branch branch to clone
93+
* @param boolean $bare indicates if repository should be bare or have a working copy
94+
* @param array $options options for Repository creation
95+
*
96+
* @return Repository
97+
*/
98+
public static function cloneBranchTo($path, $url, $branch, $bare = true, $options = array())
99+
{
100+
$args = array('--branch', $branch);
101+
if ($bare) {
102+
$args[] = '--bare';
103+
}
104+
105+
return static::cloneRepository($path, $url, $args, $options);
106+
}
107+
87108
/**
88109
* Mirrors a repository (fetch all revisions, not only branches).
89110
*

tests/Gitonomy/Git/Tests/AdminTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Gitonomy\Git\Admin;
1616
use Gitonomy\Git\Repository;
17+
use Gitonomy\Git\Reference\Branch;
1718

1819
class AdminTest extends AbstractTest
1920
{
@@ -78,6 +79,36 @@ public function testClone($repository)
7879
}
7980
}
8081

82+
public function testCloneBranchBare()
83+
{
84+
//we can't use AbstractText::createFoobarRepository()
85+
//because it does not clone other branches than "master"
86+
//so we test it directly against the remote repository
87+
88+
$newDir = self::createTempDir();
89+
$new = Admin::cloneBranchTo($newDir, self::REPOSITORY_URL, 'new-feature');
90+
self::registerDeletion($new);
91+
92+
$head = $new->getHead();
93+
$this->assertTrue($head instanceof Branch, "HEAD is a branch");
94+
$this->assertEquals("new-feature", $head->getName(), "HEAD is branch new-feature");
95+
}
96+
97+
public function testCloneBranchNotBare()
98+
{
99+
//we can't use AbstractText::createFoobarRepository()
100+
//because it does not clone other branches than "master"
101+
//so we test it directly against remote repository
102+
103+
$newDir = self::createTempDir();
104+
$new = Admin::cloneBranchTo($newDir, self::REPOSITORY_URL, 'new-feature', false);
105+
self::registerDeletion($new);
106+
107+
$head = $new->getHead();
108+
$this->assertTrue($head instanceof Branch, "HEAD is a branch");
109+
$this->assertEquals("new-feature", $head->getName(), "HEAD is branch new-feature");
110+
}
111+
81112
/**
82113
* @dataProvider provideFoobar
83114
*/

0 commit comments

Comments
 (0)