Skip to content

Commit d6cd88d

Browse files
author
Alexandre Salomé
committed
Commit and Reference inherit from Revision
1 parent 2d670a8 commit d6cd88d

File tree

10 files changed

+78
-149
lines changed

10 files changed

+78
-149
lines changed

doc/api/revision.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ To resolve a revision to a commit:
2525

2626
.. code-block:: php
2727
28-
$commit = $revision->getResolved();
28+
$commit = $revision->getCommit();

src/Gitonomy/Git/Commit.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,19 @@
1212

1313
namespace Gitonomy\Git;
1414

15-
use Gitonomy\Git\Util\StringHelper;
1615
use Gitonomy\Git\Diff\Diff;
17-
use Gitonomy\Git\Exception\ReferenceNotFoundException;
1816
use Gitonomy\Git\Exception\InvalidArgumentException;
17+
use Gitonomy\Git\Exception\ReferenceNotFoundException;
1918
use Gitonomy\Git\Exception\ProcessException;
19+
use Gitonomy\Git\Util\StringHelper;
2020

2121
/**
2222
* Representation of a Git commit.
2323
*
2424
* @author Alexandre Salomé <[email protected]>
2525
*/
26-
class Commit
26+
class Commit extends Revision
2727
{
28-
/**
29-
* The repository associated to the commit.
30-
*
31-
* @var Gitonomy\Git\Repository
32-
*/
33-
private $repository;
34-
3528
/**
3629
* Hash of the commit.
3730
*
@@ -126,8 +119,12 @@ class Commit
126119
*/
127120
public function __construct(Repository $repository, $hash)
128121
{
129-
$this->repository = $repository;
122+
if (!preg_match('/^[a-f0-9]{40}$/', $hash)) {
123+
throw new ReferenceNotFoundException($hash);
124+
}
125+
130126
$this->hash = $hash;
127+
parent::__construct($repository, $hash, $hash);
131128
}
132129

133130
/**
@@ -477,12 +474,4 @@ public function getBodyMessage()
477474

478475
return implode("\n", $lines);
479476
}
480-
481-
/**
482-
* @return Repository
483-
*/
484-
public function getRepository()
485-
{
486-
return $this->repository;
487-
}
488477
}

src/Gitonomy/Git/Reference.php

Lines changed: 2 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -18,105 +18,15 @@
1818
* @author Alexandre Salomé <[email protected]>
1919
* @author Julien DIDIER <[email protected]>
2020
*/
21-
abstract class Reference
21+
abstract class Reference extends Revision
2222
{
23-
/**
24-
* Current repository.
25-
*
26-
* @var Gitonomy\Git\Repository
27-
*/
28-
protected $repository;
29-
30-
/**
31-
* Fullname reference.
32-
*
33-
* @var string
34-
*/
35-
protected $fullname;
36-
37-
/**
38-
* Hash of the commit.
39-
*
40-
* @var string
41-
*/
42-
protected $commitHash;
43-
44-
/**
45-
* Constructor.
46-
*
47-
* @param Gitonomy\Git\Repository $repository A repository object
48-
*
49-
* @param string $fullname Fullname of the reference
50-
*
51-
* @param string $commitHash The commit hash
52-
*/
53-
public function __construct(Repository $repository, $fullname, $commitHash)
54-
{
55-
$this->repository = $repository;
56-
$this->fullname = $fullname;
57-
$this->commitHash = $commitHash;
58-
}
59-
60-
/**
61-
* Returns the usual name of the reference.
62-
*
63-
* @return string
64-
*/
65-
abstract public function getName();
66-
67-
/**
68-
* Returns the fullname of the reference.
69-
*
70-
* @return string
71-
*/
7223
public function getFullname()
7324
{
74-
return $this->fullname;
75-
}
76-
77-
/**
78-
* Returns the commit associated to the reference.
79-
*
80-
* @return Gitonomy\Git\Commit
81-
*/
82-
public function getCommit()
83-
{
84-
return $this->repository->getCommit($this->commitHash);
85-
}
86-
87-
public function getCommitHash()
88-
{
89-
return $this->commitHash;
90-
}
91-
92-
/**
93-
* Returns the last modification date of the reference.
94-
*
95-
* @return DateTime
96-
*/
97-
public function getLastModification()
98-
{
99-
return $this->getCommit()->getAuthorDate();
25+
return $this->revision;
10026
}
10127

10228
public function delete()
10329
{
10430
$this->repository->getReferences()->delete($this->getFullname());
10531
}
106-
107-
/**
108-
* @return Repository
109-
*/
110-
public function getRepository()
111-
{
112-
return $this->repository;
113-
}
114-
115-
/**
116-
* @return Log
117-
*/
118-
public function getLog()
119-
{
120-
return new Log($this->repository, $this->getFullname());
121-
}
12232
}

src/Gitonomy/Git/Reference/Branch.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ class Branch extends Reference
2424
{
2525
private $local = null;
2626

27-
/**
28-
* {@inheritdoc}
29-
*/
3027
public function getName()
3128
{
32-
if (preg_match('#^refs/heads/(?<name>.*)$#', $this->fullname, $vars)) {
29+
$fullname = $this->getFullname();
30+
31+
if (preg_match('#^refs/heads/(?<name>.*)$#', $fullname, $vars)) {
3332
return $vars['name'];
3433
}
3534

36-
if (preg_match('#^refs/remotes/(?<remote>[^/]*)/(?<name>.*)$#', $this->fullname, $vars)) {
35+
if (preg_match('#^refs/remotes/(?<remote>[^/]*)/(?<name>.*)$#', $fullname, $vars)) {
3736
return $vars['remote'].'/'.$vars['name'];
3837
}
3938

40-
throw new RuntimeException(sprintf('Cannot extract branch name from "%s"', $this->fullname));
39+
throw new RuntimeException(sprintf('Cannot extract branch name from "%s"', $fullname));
4140
}
4241

4342
public function isRemote()
@@ -57,7 +56,7 @@ public function isLocal()
5756
private function detectBranchType()
5857
{
5958
if (null === $this->local) {
60-
$this->local = !preg_match('#^refs/remotes/(?<remote>[^/]*)/(?<name>.*)$#', $this->fullname);
59+
$this->local = !preg_match('#^refs/remotes/(?<remote>[^/]*)/(?<name>.*)$#', $this->getFullname());
6160
}
6261
}
6362
}

src/Gitonomy/Git/Reference/Stash.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,4 @@
1919
*/
2020
class Stash extends Reference
2121
{
22-
/**
23-
* {@inheritdoc}
24-
*/
25-
public function getName()
26-
{
27-
return 'stash';
28-
}
2922
}

src/Gitonomy/Git/Reference/Tag.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
*/
2323
class Tag extends Reference
2424
{
25-
/**
26-
* {@inheritdoc}
27-
*/
2825
public function getName()
2926
{
3027
if (!preg_match('#^refs/tags/(.*)$#', $this->fullname, $vars)) {

src/Gitonomy/Git/Repository.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,16 @@ public function setLogger(LoggerInterface $logger)
563563
return $this;
564564
}
565565

566+
/**
567+
* Returns repository logger.
568+
*
569+
* @return LoggerInterface the logger or null
570+
*/
571+
public function getLogger()
572+
{
573+
return $this->logger;
574+
}
575+
566576
/**
567577
* Clones the current repository to a new directory and return instance of new repository.
568578
*

src/Gitonomy/Git/Revision.php

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Gitonomy\Git;
1414

1515
use Gitonomy\Git\Exception\ReferenceNotFoundException;
16+
use Gitonomy\Git\Exception\InvalidArgumentException;
1617
use Gitonomy\Git\Exception\ProcessException;
1718

1819
/**
@@ -28,44 +29,76 @@ class Revision
2829
/**
2930
* @var string
3031
*/
31-
protected $name;
32+
protected $revision;
3233

3334
/**
3435
* @var Commit
3536
*/
36-
protected $resolved;
37+
protected $commitHash;
3738

38-
public function __construct(Repository $repository, $name)
39+
public function __construct(Repository $repository, $revision, $commitHash = null)
3940
{
4041
$this->repository = $repository;
41-
$this->name = $name;
42+
$this->revision = $revision;
43+
$this->commitHash = $commitHash;
4244
}
4345

4446
/**
4547
* @return Log
4648
*/
4749
public function getLog($paths = null, $offset = null, $limit = null)
4850
{
49-
return $this->repository->getLog($this->name, $paths, $offset, $limit);
51+
return $this->repository->getLog($this->revision, $paths, $offset, $limit);
5052
}
5153

5254
/**
53-
* Resolves the revision to a commit hash.
55+
* Returns the commit associated to the reference.
5456
*
55-
* @return Commit
57+
* @return Gitonomy\Git\Commit
5658
*/
57-
public function getResolved()
59+
public function getCommit()
5860
{
59-
if (null !== $this->resolved) {
60-
return $this->resolved;
61+
return $this->repository->getCommit($this->getCommitHash());
62+
}
63+
64+
public function getCommitHash()
65+
{
66+
if (null !== $this->commitHash) {
67+
return $this->commitHash;
6168
}
6269

6370
try {
64-
$result = $this->repository->run('rev-parse', array('--verify', $this->name));
71+
$result = $this->repository->run('rev-parse', array('--verify', $this->revision));
6572
} catch (ProcessException $e) {
66-
throw new ReferenceNotFoundException(sprintf('Can not find reference "%s"', $this->name));
73+
throw new ReferenceNotFoundException(sprintf('Can not find revision "%s"', $this->revision));
6774
}
6875

69-
return $this->resolved = $this->repository->getCommit(trim($result));
76+
return $this->commitHash = trim($result);
77+
}
78+
79+
/**
80+
* Returns the last modification date of the reference.
81+
*
82+
* @return DateTime
83+
*/
84+
public function getLastModification()
85+
{
86+
return $this->getCommit()->getAuthorDate();
87+
}
88+
89+
/**
90+
* @return string
91+
*/
92+
public function getRevision()
93+
{
94+
return $this->revision;
95+
}
96+
97+
/**
98+
* @return Repository
99+
*/
100+
public function getRepository()
101+
{
102+
return $this->repository;
70103
}
71104
}

tests/Gitonomy/Git/Tests/CommitTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ public function testGetHash($repository)
4343
* @dataProvider provideFoobar
4444
*
4545
* @expectedException Gitonomy\Git\Exception\ReferenceNotFoundException
46-
* @expectedExceptionMessage Can not find reference "that-hash-doest-not-exists"
46+
* @expectedExceptionMessage Reference not found: "that-hash-doest-not-exists"
4747
*/
4848
public function testInvalideHashThrowException($repository)
4949
{
5050
$commit = new Commit($repository, 'that-hash-doest-not-exists');
51-
52-
$commit->getTreeHash();
5351
}
5452

5553
/**

tests/Gitonomy/Git/Tests/RevisionTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ public function testGetCommit($repository)
2727

2828
$this->assertTrue($revision instanceof Revision, "Revision object type");
2929

30-
$commit = $revision->getResolved();
30+
$commit = $revision->getCommit();
3131

32-
$this->assertTrue($commit instanceof Commit, "getResolved returns a Commit");
32+
$this->assertTrue($commit instanceof Commit, "getCommit returns a Commit");
3333

3434
$this->assertEquals(self::BEFORE_LONGFILE_COMMIT, $commit->getHash(), "Resolution is correct");
3535
}
3636

3737
/**
3838
* @dataProvider provideFoobar
3939
* @expectedException Gitonomy\Git\Exception\ReferenceNotFoundException
40-
* @expectedExceptionMessage Can not find reference "non-existent-commit"
40+
* @expectedExceptionMessage Can not find revision "non-existent-commit"
4141
*/
4242
public function testGetFailingReference($repository)
4343
{
44-
$revision = $repository->getRevision('non-existent-commit')->getResolved();
44+
$revision = $repository->getRevision('non-existent-commit')->getCommit();
4545
}
4646

4747
/**

0 commit comments

Comments
 (0)