Skip to content

Commit 6d222ec

Browse files
author
alexandresalome
committed
refactor diff to not be aware of repository #19
1 parent c009ffa commit 6d222ec

File tree

11 files changed

+95
-102
lines changed

11 files changed

+95
-102
lines changed

doc/api/commit.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ There are also two other methods for your convenience:
120120
// The body (rest of the message)
121121
$commit->getBodyMessage();
122122
123-
124123
Diff of a commit
125124
----------------
126125

doc/api/diff.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ Here is an exhaustive list of the *File* object:
6161
6262
$file->getChanges(); // See next chapter
6363
64-
On a *File* object, you can also access old and new contents with:
65-
66-
.. code-block:: php
67-
68-
echo 'Old content: ', $file->getOldBlob()->getContent();
69-
echo 'New content: ', $file->getNewBlob()->getContent();
70-
7164
The FileChange object
7265
---------------------
7366

src/Gitonomy/Git/Commit.php

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

1515
use Gitonomy\Git\Util\StringHelper;
16+
use Gitonomy\Git\Diff\Diff;
1617

1718
/**
1819
* Representation of a Git commit.
@@ -156,7 +157,9 @@ private function initialize()
156157

157158
public function getDiff()
158159
{
159-
return new Diff($this->repository, array($this->hash));
160+
$args = array('-r', '-p', '-m', '-M', '--no-commit-id', '--full-index', $this->hash);
161+
162+
return Diff::parse($this->repository->run('diff-tree', $args));
160163
}
161164

162165
/**

src/Gitonomy/Git/Diff.php renamed to src/Gitonomy/Git/Diff/Diff.php

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
* with this source code in the file LICENSE.
1111
*/
1212

13-
namespace Gitonomy\Git;
13+
namespace Gitonomy\Git\Diff;
14+
15+
use Gitonomy\Git\Parser\DiffParser;
1416

1517
/**
1618
* Representation of a diff.
@@ -19,26 +21,11 @@
1921
*/
2022
class Diff
2123
{
22-
/**
23-
* @var Repository
24-
*/
25-
protected $repository;
26-
27-
/**
28-
* @var array
29-
*/
30-
protected $revisions;
31-
3224
/**
3325
* @var array
3426
*/
3527
protected $files;
3628

37-
/**
38-
* @var boolean
39-
*/
40-
protected $isTree;
41-
4229
/**
4330
* @var string
4431
*/
@@ -51,32 +38,28 @@ class Diff
5138
* @var string $revision A string revision, passed to git diff command
5239
* @var boolean $isTree Indicates if revisions are commit-trees to compare
5340
*/
54-
public function __construct(Repository $repository, $revisions, $isTree = true)
41+
public function __construct(array $files)
5542
{
56-
$this->repository = $repository;
57-
$this->revisions = (array) $revisions;
58-
$this->isTree = $isTree;
43+
$this->files = $files;
5944
}
6045

6146
/**
62-
* @return array
47+
* @return Diff
6348
*/
64-
public function getRevisions()
49+
static public function parse($rawDiff)
6550
{
66-
return $this->revisions;
51+
$parser = new DiffParser();
52+
$parser->parse($rawDiff);
53+
54+
return new Diff($parser->files);
6755
}
6856

69-
protected function initialize()
57+
/**
58+
* @return array
59+
*/
60+
public function getRevisions()
7061
{
71-
$args = array('-r', '-p', '-m', '-M', '--no-commit-id', '--full-index');
72-
$args = array_merge($args, $this->revisions);
73-
$this->rawDiff = $this->repository->run($this->isTree ? 'diff-tree' : 'diff', $args);
74-
75-
$parser = new Parser\DiffParser();
76-
$parser->setRepository($this->repository);
77-
$parser->parse($this->rawDiff);
78-
79-
$this->files = $parser->files;
62+
return $this->revisions;
8063
}
8164

8265
/**
@@ -86,8 +69,6 @@ protected function initialize()
8669
*/
8770
public function getFiles()
8871
{
89-
$this->initialize();
90-
9172
return $this->files;
9273
}
9374

@@ -98,8 +79,20 @@ public function getFiles()
9879
*/
9980
public function getRawDiff()
10081
{
101-
$this->initialize();
102-
10382
return $this->rawDiff;
10483
}
84+
85+
public function toArray()
86+
{
87+
return array_map(function (File $file) {
88+
return $file->toArray();
89+
}, $this->files);
90+
}
91+
92+
public static function fromArray(array $array)
93+
{
94+
return new Diff(array_map(function ($array) {
95+
return File::fromArray($array);
96+
}, $array));
97+
}
10598
}

src/Gitonomy/Git/Diff/File.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
*/
2020
class File
2121
{
22-
/**
23-
* @var Repository
24-
*/
25-
protected $repository;
26-
2722
/**
2823
* @var string
2924
*/
@@ -67,9 +62,8 @@ class File
6762
/**
6863
* Instanciates a new Diff File object.
6964
*/
70-
public function __construct(Repository $repository, $oldName, $newName, $oldMode, $newMode, $oldIndex, $newIndex, $isBinary)
65+
public function __construct($oldName, $newName, $oldMode, $newMode, $oldIndex, $newIndex, $isBinary)
7166
{
72-
$this->repository = $repository;
7367
$this->oldName = $oldName;
7468
$this->newName = $newName;
7569
$this->oldMode = $oldMode;
@@ -191,32 +185,40 @@ public function getNewIndex()
191185
return $this->newIndex;
192186
}
193187

194-
public function getOldBlob()
195-
{
196-
if (null === $this->oldIndex) {
197-
throw new \LogicException('Can\'t instanciate Blob: no old index');
198-
}
199-
200-
return $this->repository->getBlob($this->oldIndex);
201-
}
202-
203-
public function getNewBlob()
188+
public function isBinary()
204189
{
205-
if (null === $this->newIndex) {
206-
throw new \LogicException('Can\'t instanciate Blob: no old index');
207-
}
208-
209-
return $this->repository->getBlob($this->newIndex);
190+
return $this->isBinary;
210191
}
211192

212193
public function getChanges()
213194
{
214195
return $this->changes;
215196
}
216197

217-
public function isBinary()
198+
public function toArray()
218199
{
219-
return $this->isBinary;
200+
return array(
201+
'old_name' => $this->oldName,
202+
'new_name' => $this->newName,
203+
'old_mode' => $this->oldMode,
204+
'new_mode' => $this->newMode,
205+
'old_index' => $this->oldIndex,
206+
'new_index' => $this->newIndex,
207+
'is_binary' => $this->isBinary,
208+
'changes' => array_map(function (FileChange $change) {
209+
return $change->toArray();
210+
}, $this->changes)
211+
);
220212
}
221213

214+
public static function fromArray(array $array)
215+
{
216+
$file = new File($array['old_name'], $array['new_name'], $array['old_mode'], $array['new_mode'], $array['old_index'], $array['new_index'], $array['is_binary']);
217+
218+
foreach ($array['changes'] as $change) {
219+
$file->addChange(FileChange::fromArray($change));
220+
}
221+
222+
return $file;
223+
}
222224
}

src/Gitonomy/Git/Diff/FileChange.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,20 @@ public function getLines()
6969
{
7070
return $this->lines;
7171
}
72+
73+
public function toArray()
74+
{
75+
return array(
76+
'range_old_start' => $this->rangeOldStart,
77+
'range_old_count' => $this->rangeOldCount,
78+
'range_new_start' => $this->rangeNewStart,
79+
'range_new_count' => $this->rangeNewCount,
80+
'lines' => $this->lines,
81+
);
82+
}
83+
84+
public static function fromArray(array $array)
85+
{
86+
return new FileChange($array['range_old_start'], $array['range_old_count'], $array['range_new_start'], $array['range_new_count'], $array['lines']);
87+
}
7288
}

src/Gitonomy/Git/Parser/DiffParser.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,9 @@
1919
class DiffParser extends ParserBase
2020
{
2121
public $files;
22-
protected $repository;
23-
24-
public function setRepository(Repository $repository)
25-
{
26-
$this->repository = $repository;
27-
}
2822

2923
protected function doParse()
3024
{
31-
if (null === $this->repository) {
32-
throw new \RuntimeException('Can\'t work without Repository');
33-
}
34-
3525
$this->files = array();
3626

3727
while (!$this->isFinished()) {
@@ -103,7 +93,7 @@ protected function doParse()
10393
$newName = $newName === '/dev/null' ? null : substr($newName, 2);
10494
$oldIndex = preg_match('/^0+$/', $oldIndex) ? null : $oldIndex;
10595
$newIndex = preg_match('/^0+$/', $newIndex) ? null : $newIndex;
106-
$file = new File($this->repository, $oldName, $newName, $oldMode, $newMode, $oldIndex, $newIndex, $isBinary);
96+
$file = new File($oldName, $newName, $oldMode, $newMode, $oldIndex, $newIndex, $isBinary);
10797

10898
// 5. Diff
10999
while ($this->expects('@@ ')) {

src/Gitonomy/Git/Parser/ParserBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function consumeHash()
8282
protected function consumeRegexp($regexp)
8383
{
8484
if (!preg_match($regexp.'A', $this->content, $vars, null, $this->cursor)) {
85-
throw new \RuntimeException('No match for regexp '.$regexp);
85+
throw new \RuntimeException('No match for regexp '.$regexp.' Upcoming: '.substr($this->content, $this->cursor, 30));
8686
}
8787

8888
$this->cursor += strlen($vars[0]);

src/Gitonomy/Git/Repository.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Psr\Log\LoggerInterface;
1818

1919
use Gitonomy\Git\Exception\RuntimeException;
20+
use Gitonomy\Git\Diff\Diff;
2021

2122
/**
2223
* Git repository object.
@@ -280,7 +281,9 @@ public function getLog($revisions = null, $paths = null, $offset = null, $limit
280281
*/
281282
public function getDiff($revision)
282283
{
283-
return new Diff($this, $revision);
284+
$args = array('-r', '-p', '-m', '-M', '--no-commit-id', '--full-index', $revision);
285+
286+
return new Diff($this->run('diff', $args));
284287
}
285288

286289
/**

tests/Gitonomy/Git/Tests/CommitTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace Gitonomy\Git\Tests;
1414

1515
use Gitonomy\Git\Commit;
16-
use Gitonomy\Git\Diff;
16+
use Gitonomy\Git\Diff\Diff;
1717

1818
class CommitTest extends AbstractTest
1919
{
@@ -27,7 +27,6 @@ public function testGetDiff($repository)
2727
$diff = $commit->getDiff();
2828

2929
$this->assertTrue($diff instanceof Diff, "getDiff() returns a Diff object");
30-
$this->assertEquals(array($commit->getHash()), $diff->getRevisions(), "getDiff() revision is correct");
3130
}
3231

3332
/**

0 commit comments

Comments
 (0)