Skip to content

Commit 2d0e047

Browse files
committed
create and delete reference
1 parent 192010e commit 2d0e047

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

doc/api/references.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ On each of those objects, you can access those informations:
5151
// Get the last modification
5252
$lastModification = $master->getLastModification();
5353
54+
Create and delete reference
55+
---------------------------
56+
57+
You can create new tags and branches on repository, using helper methods
58+
on ReferenceBag object:
59+
60+
.. code-block:: php
61+
62+
// create a branch
63+
$references = $repository->getReferences();
64+
$branch = $references->createBranch('foobar', 'a8b7e4...'); // commit to reference
65+
66+
// create a tag
67+
$references = $repository->getReferences();
68+
$tag = $references->createTag('0.3', 'a8b7e4...'); // commit to reference
69+
70+
// delete a branch or a tag
71+
$branch->delete();
72+
5473
Resolution from a commit
5574
------------------------
5675

src/Gitonomy/Git/Reference.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Reference in a Git repository.
1717
*
1818
* @author Alexandre Salomé <[email protected]>
19+
* @author Julien DIDIER <[email protected]>
1920
*/
2021
abstract class Reference
2122
{
@@ -97,4 +98,9 @@ public function getLastModification()
9798
{
9899
return $this->getCommit()->getAuthorDate();
99100
}
101+
102+
public function delete()
103+
{
104+
$this->repository->getReferences()->delete($this->getFullname());
105+
}
100106
}

src/Gitonomy/Git/ReferenceBag.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616

1717
use Gitonomy\Git\Exception\ReferenceNotFoundException;
1818
use Gitonomy\Git\Exception\RuntimeException;
19+
use Gitonomy\Git\Reference\Branch;
20+
use Gitonomy\Git\Reference\Tag;
1921

2022
/**
2123
* Reference set associated to a repository.
2224
*
2325
* @author Alexandre Salomé <[email protected]>
26+
* @author Julien DIDIER <[email protected]>
2427
*/
2528
class ReferenceBag implements \Countable, \IteratorAggregate
2629
{
@@ -97,6 +100,38 @@ public function has($fullname)
97100
return isset($this->references[$fullname]);
98101
}
99102

103+
public function update(Reference $reference)
104+
{
105+
$fullname = $reference->getFullname();
106+
107+
$this->repository->run('update-ref', array($fullname, $reference->getCommitHash()));
108+
109+
$this->references[$fullname] = $reference;
110+
111+
return $reference;
112+
}
113+
114+
public function createBranch($name, $commitHash)
115+
{
116+
$branch = new Branch($this->repository, 'refs/heads/'.$name, $commitHash);
117+
118+
return $this->update($branch);
119+
}
120+
121+
public function createTag($name, $commitHash)
122+
{
123+
$tag = new Tag($this->repository, 'refs/tags/'.$name, $commitHash);
124+
125+
return $this->update($tag);
126+
}
127+
128+
public function delete($fullname)
129+
{
130+
$this->repository->run('update-ref', array('-d', $fullname));
131+
132+
unset($this->references[$fullname]);
133+
}
134+
100135
public function hasBranches()
101136
{
102137
$this->initialize();

tests/Gitonomy/Git/Tests/ReferenceTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,30 @@ public function testIterable()
109109
}
110110
$this->assertGreaterThanOrEqual(2, $i, "At least two references in repository");
111111
}
112+
113+
public function testCreateAndDeleteTag()
114+
{
115+
$references = $this->getLibRepository()->getReferences();
116+
$tag = $references->createTag('0.0', self::INITIAL_COMMIT);
117+
118+
$this->assertTrue($references->hasTag('0.0'), "Tag 0.0 created");
119+
$this->assertEquals(self::INITIAL_COMMIT, $tag->getCommit()->getHash());
120+
$this->assertSame($tag, $references->getTag('0.0'));
121+
122+
$tag->delete();
123+
$this->assertFalse($references->hasTag('0.0'), "Tag 0.0 removed");
124+
}
125+
126+
public function testCreateAndDeleteBranch()
127+
{
128+
$references = $this->getLibRepository()->getReferences();
129+
$branch = $references->createBranch('foobar', self::INITIAL_COMMIT);
130+
131+
$this->assertTrue($references->hasBranch('foobar'), "Branch foobar created");
132+
$this->assertEquals(self::INITIAL_COMMIT, $branch->getCommit()->getHash());
133+
$this->assertSame($branch, $references->getBranch('foobar'));
134+
135+
$branch->delete();
136+
$this->assertFalse($references->hasBranch('foobar'), "Branch foobar removed");
137+
}
112138
}

tests/Gitonomy/Git/Tests/TestBase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Gitonomy\Git\Tests;
1414

15+
use Gitonomy\Git\Admin;
1516
use Gitonomy\Git\Repository;
1617

1718
class TestBase extends \PHPUnit_Framework_TestCase
@@ -43,6 +44,11 @@ public function createTempDir()
4344
return $tmpDir;
4445
}
4546

47+
public function createEmptyRepository()
48+
{
49+
return Admin::init($this->createTempDir());
50+
}
51+
4652
public function deleteDir($dir)
4753
{
4854
$iterator = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS);

0 commit comments

Comments
 (0)