Skip to content

Commit 6a8c584

Browse files
committed
Use closer exceptions
1 parent 0f86d88 commit 6a8c584

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

src/Gitonomy/Git/Commit.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Gitonomy\Git\Util\StringHelper;
1616
use Gitonomy\Git\Diff\Diff;
17+
use Gitonomy\Git\Exception\ReferenceNotFoundException;
1718

1819
/**
1920
* Representation of a Git commit.
@@ -139,7 +140,12 @@ private function initialize()
139140
}
140141

141142
$parser = new Parser\CommitParser();
142-
$result = $this->repository->run('cat-file', array('commit', $this->hash));
143+
try {
144+
$result = $this->repository->run('cat-file', array('commit', $this->hash));
145+
} catch (\RuntimeException $e) {
146+
throw new ReferenceNotFoundException(sprintf('Can not find reference "%s"', $this->hash));
147+
}
148+
143149
$parser->parse($result);
144150

145151
$this->treeHash = $parser->tree;

src/Gitonomy/Git/Revision.php

Lines changed: 7 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\ReferenceNotFoundException;
16+
1517
/**
1618
* @author Alexandre Salomé <[email protected]>
1719
*/
@@ -57,7 +59,11 @@ public function getResolved()
5759
return $this->resolved;
5860
}
5961

60-
$result = $this->repository->run('rev-parse', array('--verify', $this->name));
62+
try {
63+
$result = $this->repository->run('rev-parse', array('--verify', $this->name));
64+
} catch (\RuntimeException $e) {
65+
throw new ReferenceNotFoundException(sprintf('Can not find reference "%s"', $this->name));
66+
}
6167

6268
return $this->resolved = $this->repository->getCommit(trim($result));
6369
}

tests/Gitonomy/Git/Tests/CommitTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ public function testGetHash($repository)
3939
$this->assertEquals(self::LONGFILE_COMMIT, $commit->getHash());
4040
}
4141

42+
/**
43+
* @dataProvider provideFoobar
44+
*
45+
* @expectedException Gitonomy\Git\Exception\ReferenceNotFoundException
46+
* @expectedExceptionMessage Can not find reference "that-hash-doest-not-exists"
47+
*/
48+
public function testInvalideHashThrowException($repository)
49+
{
50+
$commit = new Commit($repository, 'that-hash-doest-not-exists');
51+
52+
$commit->getTreeHash();
53+
}
54+
4255
/**
4356
* @dataProvider provideFoobar
4457
*/

tests/Gitonomy/Git/Tests/RevisionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public function testGetCommit($repository)
3434
$this->assertEquals(self::BEFORE_LONGFILE_COMMIT, $commit->getHash(), "Resolution is correct");
3535
}
3636

37+
/**
38+
* @dataProvider provideFoobar
39+
* @expectedException Gitonomy\Git\Exception\ReferenceNotFoundException
40+
* @expectedExceptionMessage Can not find reference "non-existent-commit"
41+
*/
42+
public function testGetFailingReference($repository)
43+
{
44+
$revision = $repository->getRevision('non-existent-commit')->getResolved();
45+
}
46+
3747
/**
3848
* @dataProvider provideFoobar
3949
*/

0 commit comments

Comments
 (0)