Skip to content

Commit d7cac7d

Browse files
author
alexandresalome
committed
remove commit hash logic from revision
revision might be a list of commits, it makes no sense to have those methods on revision. on a reference, it does.
1 parent ca28530 commit d7cac7d

File tree

4 files changed

+70
-37
lines changed

4 files changed

+70
-37
lines changed

src/Gitonomy/Git/Commit.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ public function __construct(Repository $repository, $hash)
124124
}
125125

126126
$this->hash = $hash;
127-
parent::__construct($repository, $hash, $hash);
127+
128+
parent::__construct($repository, $hash);
128129
}
129130

130131
/**
@@ -160,6 +161,9 @@ private function initialize()
160161
$this->initialized = true;
161162
}
162163

164+
/**
165+
* @return Diff
166+
*/
163167
public function getDiff()
164168
{
165169
$args = array('-r', '-p', '-m', '-M', '--no-commit-id', '--full-index', $this->hash);

src/Gitonomy/Git/Log.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Gitonomy\Git;
1414

15+
use Gitonomy\Git\Exception\ProcessException;
16+
use Gitonomy\Git\Exception\ReferenceNotFoundException;
1517
use Gitonomy\Git\Util\StringHelper;
1618

1719
/**
@@ -134,6 +136,20 @@ public function setLimit($limit)
134136
return $this;
135137
}
136138

139+
public function getSingleCommit()
140+
{
141+
$limit = $this->limit;
142+
$this->limit = 1;
143+
$commits = $this->getCommits();
144+
$this->setLimit($limit);
145+
146+
if (count($commits) === 0) {
147+
throw new ReferenceNotFoundException('The log is empty');
148+
}
149+
150+
return array_pop($commits);
151+
}
152+
137153
/**
138154
* @return array
139155
*/
@@ -160,7 +176,11 @@ public function getCommits()
160176

161177
$args = array_merge($args, $this->paths);
162178

163-
$exp = explode("\n", $this->repository->run('log', $args));
179+
try {
180+
$exp = explode("\n", $this->repository->run('log', $args));
181+
} catch (ProcessException $e) {
182+
throw new ReferenceNotFoundException(sprintf('Can not find revision "%s"', implode(' ', $this->revisions->getAsTextArray())), null, $e);
183+
}
164184

165185
$result = array();
166186
foreach ($exp as $hash) {

src/Gitonomy/Git/Reference.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
*/
2121
abstract class Reference extends Revision
2222
{
23+
protected $commitHash;
24+
25+
public function __construct(Repository $repository, $revision, $commitHash = null)
26+
{
27+
$this->repository = $repository;
28+
$this->revision = $revision;
29+
$this->commitHash = $commitHash;
30+
}
31+
2332
public function getFullname()
2433
{
2534
return $this->revision;
@@ -29,4 +38,35 @@ public function delete()
2938
{
3039
$this->repository->getReferences()->delete($this->getFullname());
3140
}
41+
42+
43+
public function getCommitHash()
44+
{
45+
if (null !== $this->commitHash) {
46+
return $this->commitHash;
47+
}
48+
49+
try {
50+
$result = $this->repository->run('rev-parse', array('--verify', $this->revision));
51+
} catch (ProcessException $e) {
52+
throw new ReferenceNotFoundException(sprintf('Can not find revision "%s"', $this->revision));
53+
}
54+
55+
return $this->commitHash = trim($result);
56+
}
57+
58+
/**
59+
* Returns the commit associated to the reference.
60+
*
61+
* @return Gitonomy\Git\Commit
62+
*/
63+
public function getCommit()
64+
{
65+
return $this->repository->getCommit($this->getCommitHash());
66+
}
67+
68+
public function getLastModification($path = null)
69+
{
70+
return $this->getCommit()->getLastModification($path);
71+
}
3272
}

src/Gitonomy/Git/Revision.php

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,10 @@ class Revision
3131
*/
3232
protected $revision;
3333

34-
/**
35-
* @var Commit
36-
*/
37-
protected $commitHash;
38-
39-
public function __construct(Repository $repository, $revision, $commitHash = null)
34+
public function __construct(Repository $repository, $revision)
4035
{
4136
$this->repository = $repository;
4237
$this->revision = $revision;
43-
$this->commitHash = $commitHash;
4438
}
4539

4640
/**
@@ -51,39 +45,14 @@ public function getLog($paths = null, $offset = null, $limit = null)
5145
return $this->repository->getLog($this, $paths, $offset, $limit);
5246
}
5347

54-
/**
55-
* Returns the commit associated to the reference.
56-
*
57-
* @return Gitonomy\Git\Commit
58-
*/
59-
public function getCommit()
60-
{
61-
return $this->repository->getCommit($this->getCommitHash());
62-
}
63-
64-
public function getCommitHash()
65-
{
66-
if (null !== $this->commitHash) {
67-
return $this->commitHash;
68-
}
69-
70-
try {
71-
$result = $this->repository->run('rev-parse', array('--verify', $this->revision));
72-
} catch (ProcessException $e) {
73-
throw new ReferenceNotFoundException(sprintf('Can not find revision "%s"', $this->revision));
74-
}
75-
76-
return $this->commitHash = trim($result);
77-
}
78-
7948
/**
8049
* Returns the last modification date of the reference.
8150
*
82-
* @return DateTime
51+
* @return Commit
8352
*/
84-
public function getLastModification($path = null)
53+
public function getCommit()
8554
{
86-
return $this->getCommit()->getLastModification($path);
55+
return $this->getLog()->getSingleCommit();
8756
}
8857

8958
/**

0 commit comments

Comments
 (0)