Skip to content

Commit a5bcfd3

Browse files
author
Alexandre Salomé
committed
Merge pull request #13 from lyrixx/feat-log
Added support for log
2 parents 2d0e047 + 27eda5d commit a5bcfd3

19 files changed

+118
-30
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ php:
33
- 5.3
44
script: ./test.sh
55
before_script:
6-
- composer install
6+
- composer install --dev --prefere-source

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
"symfony/process": ">=2.1",
2727
"symfony/event-dispatcher": ">=2.1"
2828
},
29+
"require-dev": {
30+
"psr/log": "~1"
31+
},
32+
33+
"suggest": {
34+
"psr/log": "Add some log"
35+
},
2936

3037
"minimum-stability": "dev"
3138
}

src/Gitonomy/Git/Hooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function set($name, $content)
105105
/**
106106
* Removes a hook from repository.
107107
*
108-
* @param string $name Name of the hook
108+
* @param string $name Name of the hook
109109
*
110110
* @throws LogicException The hook is not present
111111
*/

src/Gitonomy/Git/ReferenceBag.php

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

1313
namespace Gitonomy\Git;
1414

15-
use Symfony\Component\Process\Process;
16-
1715
use Gitonomy\Git\Exception\ReferenceNotFoundException;
1816
use Gitonomy\Git\Exception\RuntimeException;
1917
use Gitonomy\Git\Reference\Branch;
@@ -170,8 +168,7 @@ public function resolveTags($hash)
170168

171169
$tags = array();
172170
foreach ($this->references as $k => $reference) {
173-
if ($reference instanceof Reference\Tag && $reference->getCommitHash() === $hash)
174-
{
171+
if ($reference instanceof Reference\Tag && $reference->getCommitHash() === $hash) {
175172
$tags[] = $reference;
176173
}
177174
}
@@ -192,8 +189,7 @@ public function resolveBranches($hash)
192189

193190
$tags = array();
194191
foreach ($this->references as $k => $reference) {
195-
if ($reference instanceof Reference\Branch && $reference->getCommitHash() === $hash)
196-
{
192+
if ($reference instanceof Reference\Branch && $reference->getCommitHash() === $hash) {
197193
$tags[] = $reference;
198194
}
199195
}
@@ -214,8 +210,7 @@ public function resolve($hash)
214210

215211
$result = array();
216212
foreach ($this->references as $k => $reference) {
217-
if ($reference->getCommitHash() === $hash)
218-
{
213+
if ($reference->getCommitHash() === $hash) {
219214
$result[] = $reference;
220215
}
221216
}

src/Gitonomy/Git/Repository.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
1616
use Symfony\Component\Process\ProcessBuilder;
1717
use Symfony\Component\Process\Process;
18+
use Psr\Log\LoggerInterface;
1819

1920
use Gitonomy\Git\Event\Events;
2021
use Gitonomy\Git\Event\PreCommandEvent;
@@ -63,12 +64,23 @@ class Repository
6364
*/
6465
protected $referenceBag;
6566

67+
/**
68+
* A logger
69+
*
70+
* @var LoggerInterface
71+
*/
72+
private $logger;
73+
6674
/**
6775
* Constructor.
6876
*
77+
* @param string $dir
78+
* @param string $workingDir
79+
* @param LoggerInterface $logger
80+
*
6981
* @throws InvalidArgumentException The folder does not exists
7082
*/
71-
public function __construct($dir, $workingDir = null)
83+
public function __construct($dir, $workingDir = null, LoggerInterface $logger = null)
7284
{
7385
$gitDir = realpath($dir);
7486

@@ -85,6 +97,8 @@ public function __construct($dir, $workingDir = null)
8597
$this->workingDir = $workingDir;
8698
$this->objects = array();
8799

100+
$this->logger = $logger;
101+
88102
$this->eventDispatcher = new EventDispatcher();
89103
}
90104

@@ -346,6 +360,10 @@ public function run($command, $args = array())
346360
$this->eventDispatcher->dispatch(Events::PRE_COMMAND, $event);
347361
}
348362

363+
if ($this->logger) {
364+
$this->logger->info(sprintf('run command: "%", args: "%s"', $command, print_r($args, true)));
365+
}
366+
349367
$before = microtime(true);
350368
$process->run();
351369
$duration = microtime(true) - $before;
@@ -356,11 +374,22 @@ public function run($command, $args = array())
356374
$this->eventDispatcher->dispatch(Events::POST_COMMAND, $event);
357375
}
358376

377+
$output = $process->getOutput();
378+
379+
if ($this->logger) {
380+
$this->logger->debug(sprintf('last command ("%s") return code: "%s"', $command, $process->getExitCode()));
381+
$this->logger->debug(sprintf('last command ("%s") output: "%s"', $command, $output));
382+
}
383+
359384
if (!$process->isSuccessful()) {
385+
if ($this->logger) {
386+
$this->logger->error(sprintf('last command ("%s") error output: "%s"', $command, $process->getErrorOutput()));
387+
}
388+
360389
throw new RuntimeException($process);
361390
}
362391

363-
return $process->getOutput();
392+
return $output;
364393
}
365394

366395
public function addListener($eventName, $listener, $priority = 0)
@@ -376,6 +405,20 @@ public function getWorkingCopy()
376405
return new WorkingCopy($this);
377406
}
378407

408+
/**
409+
* Set a logger
410+
*
411+
* @param LoggerInterface $logger A logger
412+
*
413+
* @return Repository The current repository
414+
*/
415+
public function setLogger(LoggerInterface $logger)
416+
{
417+
$this->logger = $logger;
418+
419+
return $this;
420+
}
421+
379422
/**
380423
* @see self::run
381424
*/
@@ -388,7 +431,7 @@ protected function getProcess($command, $args = array())
388431
}
389432

390433
$base[] = $command;
391-
434+
392435
$builder = new ProcessBuilder(array_merge($base, $args));
393436

394437
return $builder->getProcess();

tests/Gitonomy/Git/Tests/TestBase.php renamed to tests/Gitonomy/Git/Tests/AbtractTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Gitonomy\Git\Admin;
1616
use Gitonomy\Git\Repository;
1717

18-
class TestBase extends \PHPUnit_Framework_TestCase
18+
abstract class AbtractTest extends \PHPUnit_Framework_TestCase
1919
{
2020
private static $libRepo;
2121
private static $testRepo;
@@ -67,7 +67,7 @@ public function deleteDir($dir)
6767
public function getLibRepository()
6868
{
6969
if (null === self::$libRepo) {
70-
self::$libRepo = $this->getDirRepository($this->getLibDirectory());
70+
self::$libRepo = $this->createRepositoryInstance($this->getLibDirectory());
7171
}
7272

7373
return self::$libRepo;
@@ -81,7 +81,7 @@ public function getLibDirectory()
8181
public function getTestRepository()
8282
{
8383
if (null === self::$testRepo) {
84-
self::$testRepo = $this->getDirRepository($this->getTestDirectory());
84+
self::$testRepo = $this->createRepositoryInstance($this->getTestDirectory());
8585
}
8686

8787
return self::$testRepo;
@@ -92,7 +92,7 @@ public function getTestDirectory()
9292
return __DIR__.'/../../../..';
9393
}
9494

95-
protected function getDirRepository($dir)
95+
protected function createRepositoryInstance($dir)
9696
{
9797
if (!is_dir($dir)) {
9898
$this->markTestSkipped("Test sandbox folder not present");

tests/Gitonomy/Git/Tests/AdminTest.php

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

1515
use Gitonomy\Git\Admin;
1616

17-
class AdminTest extends TestBase
17+
class AdminTest extends AbtractTest
1818
{
1919
private $tmpDir;
2020

tests/Gitonomy/Git/Tests/BlameTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Gitonomy\Git\Tests;
1414

15-
class BlameTest extends TestBase
15+
class BlameTest extends AbtractTest
1616
{
1717
public function testBlame()
1818
{

tests/Gitonomy/Git/Tests/BlobTest.php

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

1515
use Gitonomy\Git\Blob;
1616

17-
class BlobTest extends TestBase
17+
class BlobTest extends AbtractTest
1818
{
1919
public function testGetContent()
2020
{

tests/Gitonomy/Git/Tests/CommitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Gitonomy\Git\Commit;
1616
use Gitonomy\Git\Diff;
1717

18-
class CommitTest extends TestBase
18+
class CommitTest extends AbtractTest
1919
{
2020
public function testGetDiff()
2121
{

0 commit comments

Comments
 (0)