Skip to content

Commit 1fca76e

Browse files
author
alexandresalome
committed
add methods to get diff of staged and pending modifications
1 parent d5a615e commit 1fca76e

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

doc/api/workingcopy.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ can access this object using the *getWorkingCopy* on a *Repository* object:
99
$repo = new Repository('/path/to/working-dir');
1010
$wc = $repo->getWorkingCopy();
1111
12-
1312
Checkout a revision
1413
-------------------
1514

@@ -25,3 +24,22 @@ second argument, which will be passed as argument with ``-b``:
2524
$wc->checkout('origin/master', 'master');
2625
2726
You can also pass a *Reference* or a *Commit*.
27+
28+
Staged modifications
29+
--------------------
30+
31+
You can get a diff of modifications pending in staging area. To get the ``Diff`` object,
32+
call method ``getDiffStaged()``:
33+
34+
.. code-block:: php
35+
36+
$diff = $wc->getDiffStaged();
37+
38+
Pending modifications
39+
---------------------
40+
41+
You can get pending modifications on tracked files by calling method ``getDiffPending()``:
42+
43+
.. code-block:: php
44+
45+
$diff = $wc->getDiffPending();

src/Gitonomy/Git/Diff/File.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,9 @@ public static function fromArray(array $array)
219219

220220
return $file;
221221
}
222+
223+
public function getAnchor()
224+
{
225+
return substr($this->newIndex, 0, 12);
226+
}
222227
}

src/Gitonomy/Git/WorkingCopy.php

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

1313
namespace Gitonomy\Git;
1414

15+
use Gitonomy\Git\Diff\Diff;
16+
1517
/**
1618
* @author Alexandre Salomé <[email protected]>
1719
*/
@@ -31,6 +33,26 @@ public function __construct(Repository $repository)
3133
}
3234
}
3335

36+
public function getStatus()
37+
{
38+
return WorkingStatus::parseOutput();
39+
}
40+
41+
public function getUntrackedFiles()
42+
{
43+
return array();
44+
}
45+
46+
public function getDiffPending()
47+
{
48+
return Diff::parse($this->run('diff', array('-r', '-p', '-m', '-M', '--full-index')));
49+
}
50+
51+
public function getDiffStaged()
52+
{
53+
return Diff::parse($this->run('diff', array('-r', '-p', '-m', '-M', '--full-index', '--staged')));
54+
}
55+
3456
/**
3557
* @return WorkingCopy
3658
*/

tests/Gitonomy/Git/Tests/WorkingCopyTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,37 @@ public function testCheckout()
3939
$this->assertEquals("new-feature", $head->getName(), "HEAD is branch new-feature");
4040
}
4141

42+
public function testDiffStaged()
43+
{
44+
$repository = self::createFoobarRepository(false);
45+
$wc = $repository->getWorkingCopy();
46+
47+
$diffStaged = $wc->getDiffStaged();
48+
$this->assertCount(0, $diffStaged->getFiles());
49+
50+
$file = $repository->getWorkingDir().'/foobar-test';
51+
file_put_contents($file, 'test');
52+
$repository->run('add', array($file));
53+
54+
$diffStaged = $wc->getDiffStaged();
55+
$this->assertCount(1, $diffStaged->getFiles());
56+
}
57+
58+
public function testDiffPending()
59+
{
60+
$repository = self::createFoobarRepository(false);
61+
$wc = $repository->getWorkingCopy();
62+
63+
$diffPending = $wc->getDiffPending();
64+
$this->assertCount(0, $diffPending->getFiles());
65+
66+
$file = $repository->getWorkingDir().'/test.sh';
67+
file_put_contents($file, 'test');
68+
69+
$diffPending = $wc->getDiffPending();
70+
$this->assertCount(1, $diffPending->getFiles());
71+
}
72+
4273
/**
4374
* @expectedException RuntimeException
4475
*/

0 commit comments

Comments
 (0)