Skip to content

Commit 58f5af5

Browse files
author
alexandresalome
committed
inject Repository in Diff as an optional attribute
1 parent 0d2e33f commit 58f5af5

File tree

5 files changed

+81
-6
lines changed

5 files changed

+81
-6
lines changed

src/Gitonomy/Git/Commit.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ public function getDiff()
164164
{
165165
$args = array('-r', '-p', '-m', '-M', '--no-commit-id', '--full-index', $this->hash);
166166

167-
return Diff::parse($this->repository->run('diff-tree', $args));
167+
$diff = Diff::parse($this->repository->run('diff-tree', $args));
168+
$diff->setRepository($this->repository);
169+
170+
return $diff;
168171
}
169172

170173
/**
@@ -474,4 +477,12 @@ public function getBodyMessage()
474477

475478
return implode("\n", $lines);
476479
}
480+
481+
/**
482+
* @inheritdoc
483+
*/
484+
public function getCommit()
485+
{
486+
return $this;
487+
}
477488
}

src/Gitonomy/Git/Diff/Diff.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Gitonomy\Git\Diff;
1414

1515
use Gitonomy\Git\Parser\DiffParser;
16+
use Gitonomy\Git\Repository;
1617

1718
/**
1819
* Representation of a diff.
@@ -54,6 +55,13 @@ public static function parse($rawDiff)
5455
return new Diff($parser->files, $rawDiff);
5556
}
5657

58+
public function setRepository(Repository $repository)
59+
{
60+
foreach ($this->files as $file) {
61+
$file->setRepository($repository);
62+
}
63+
}
64+
5765
/**
5866
* @return array
5967
*/

src/Gitonomy/Git/Diff/File.php

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

1313
namespace Gitonomy\Git\Diff;
1414

15+
use Gitonomy\Git\Repository;
16+
1517
/**
1618
* @author Alexandre Salomé <alexandre.salome@gmail.com>
1719
*/
@@ -57,6 +59,11 @@ class File
5759
*/
5860
protected $changes;
5961

62+
/**
63+
* @var Repository
64+
*/
65+
protected $repository;
66+
6067
/**
6168
* Instanciates a new File object.
6269
*/
@@ -224,4 +231,40 @@ public function getAnchor()
224231
{
225232
return substr($this->newIndex, 0, 12);
226233
}
234+
235+
public function getRepository()
236+
{
237+
return $this->repository;
238+
}
239+
240+
public function setRepository(Repository $repository)
241+
{
242+
$this->repository = $repository;
243+
}
244+
245+
public function getOldBlob()
246+
{
247+
if (null === $this->repository) {
248+
throw new \RuntimeException('Repository is missing to return Blob object.');
249+
}
250+
251+
if ($this->isCreation()) {
252+
throw new \LogicException('Can\'t return old Blob on a creation');
253+
}
254+
255+
return $this->repository->getBlob($this->oldIndex);
256+
}
257+
258+
public function getNewBlob()
259+
{
260+
if (null === $this->repository) {
261+
throw new \RuntimeException('Repository is missing to return Blob object.');
262+
}
263+
264+
if ($this->isDeletion()) {
265+
throw new \LogicException('Can\'t return new Blob on a deletion');
266+
}
267+
268+
return $this->repository->getBlob($this->newIndex);
269+
}
227270
}

src/Gitonomy/Git/Repository.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,18 @@ public function getLog($revisions = null, $paths = null, $offset = null, $limit
385385
/**
386386
* @return Diff
387387
*/
388-
public function getDiff($revision)
388+
public function getDiff($revisions)
389389
{
390-
$args = array_merge(array('-r', '-p', '-m', '-M', '--no-commit-id', '--full-index'), (array) $revision);
390+
if (null !== $revisions && !$revisions instanceof RevisionList) {
391+
$revisions = new RevisionList($repository, $revisions);
392+
}
393+
394+
$args = array_merge(array('-r', '-p', '-m', '-M', '--no-commit-id', '--full-index'), $revisions->getAsTextArray());
395+
396+
$diff = Diff::parse($this->run('diff', $args));
397+
$diff->setRepository($this);
391398

392-
return Diff::parse($this->run('diff', $args));
399+
return $diff;
393400
}
394401

395402
/**

src/Gitonomy/Git/WorkingCopy.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,18 @@ public function getUntrackedFiles()
4848

4949
public function getDiffPending()
5050
{
51-
return Diff::parse($this->run('diff', array('-r', '-p', '-m', '-M', '--full-index')));
51+
$diff = Diff::parse($this->run('diff', array('-r', '-p', '-m', '-M', '--full-index')));
52+
$diff->setRepository($this->repository);
53+
54+
return $this;
5255
}
5356

5457
public function getDiffStaged()
5558
{
56-
return Diff::parse($this->run('diff', array('-r', '-p', '-m', '-M', '--full-index', '--staged')));
59+
$diff = Diff::parse($this->run('diff', array('-r', '-p', '-m', '-M', '--full-index', '--staged')));
60+
$diff->setRepository($this->repository);
61+
62+
return $this;
5763
}
5864

5965
/**

0 commit comments

Comments
 (0)