Skip to content

Commit f8c6084

Browse files
author
Alexandre Salomé
committed
Merge pull request #32 from gitonomy/feat-closest-exception
Fixed some code
2 parents 0f86d88 + c1d3bc6 commit f8c6084

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

src/Gitonomy/Git/Commit.php

Lines changed: 13 additions & 3 deletions
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;
@@ -252,12 +258,16 @@ public function getTree()
252258
/**
253259
* @return Commit
254260
*/
255-
public function getLastModification($path, $lastHash = null)
261+
public function getLastModification($path)
256262
{
257-
if (preg_match('#^/#', $path)) {
263+
if (0 === strpos($path, '/')) {
258264
$path = StringHelper::substr($path, 1);
259265
}
260266

267+
if ($getWorkingDir = $this->repository->getWorkingDir()) {
268+
$path = $getWorkingDir.'/'.$path;
269+
}
270+
261271
$result = $this->repository->run('log', array('--format=%H', '-n', 1, $this->hash, '--', $path));
262272

263273
return $this->repository->getCommit(trim($result));

src/Gitonomy/Git/Repository.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,16 +514,16 @@ public function run($command, $args = array())
514514
{
515515
$process = $this->getProcess($command, $args);
516516

517-
if (null !== $this->logger) {
518-
$this->logger->info(sprintf('run command: %s "%s" ', $command, implode('", "', $args)));
517+
if ($this->logger) {
518+
$this->logger->info(sprintf('run command: %s "%s" ', $command, implode(' ', $args)));
519519
$before = microtime(true);
520520
}
521521

522522
$process->run();
523523

524524
$output = $process->getOutput();
525525

526-
if (null !== $this->logger && true === $this->debug) {
526+
if ($this->logger && $this->debug) {
527527
$duration = microtime(true) - $before;
528528
$this->logger->debug(sprintf('last command (%s) duration: %sms', $command, sprintf('%.2f', $duration*1000)));
529529
$this->logger->debug(sprintf('last command (%s) return code: %s', $command, $process->getExitCode()));
@@ -533,11 +533,11 @@ public function run($command, $args = array())
533533
if (!$process->isSuccessful()) {
534534
$error = sprintf("error while running %s\n output: \"%s\"", $command, $process->getErrorOutput());
535535

536-
if (null !== $this->logger) {
536+
if ($this->logger) {
537537
$this->logger->error($error);
538538
}
539539

540-
if (true === $this->debug) {
540+
if ($this->debug) {
541541
throw new RuntimeException($process);
542542
}
543543

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)