Skip to content

Commit 3db6719

Browse files
author
alexandresalome
committed
add isRename and isChangeMode on a diff File
1 parent d7a7590 commit 3db6719

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

doc/api/diff.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ modifications for a single file.
4242
The File object
4343
---------------
4444

45-
Here is an exhaustive list of the *File* object:
45+
Here is an exhaustive list of the *File* class methods:
4646

4747
.. code-block:: php
4848
@@ -51,8 +51,12 @@ Here is an exhaustive list of the *File* object:
5151
$file->getOldDiff();
5252
$file->getNewDiff();
5353
54-
$file->isNew(); // Indicates file was created
55-
$file->isDelete(); // Indicates file was deleted
54+
$file->isCreation();
55+
$file->isDeletion();
56+
$file->isModification();
57+
58+
$file->isRename();
59+
$file->isChangeMode();
5660
5761
$file->getAdditions(); // Number of added lines
5862
$file->getDeletions(); // Number of deleted lines

src/Gitonomy/Git/Diff/File.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ public function isModification()
105105
return null !== $this->oldName && null !== $this->newName;
106106
}
107107

108+
/**
109+
* Indicates if it's a rename.
110+
*
111+
* A rename can only occurs if it's a modification (not a creation or a deletion).
112+
*
113+
* @return boolean
114+
*/
115+
public function isRename()
116+
{
117+
return $this->isModification() && $this->oldName !== $this->newName;
118+
}
119+
120+
/**
121+
* Indicates if the file mode has changed.
122+
*/
123+
public function isChangeMode()
124+
{
125+
return $this->isModification() && $this->oldMode !== $this->newMode;
126+
}
127+
108128
/**
109129
* Indicates if this diff file is a deletion.
110130
*

tests/Gitonomy/Git/Tests/DiffTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
class DiffTest extends AbstractTest
1818
{
19-
const DELETE_COMMIT = '519d5693c72c925cd59205d9f11e9fa1d550028b';
20-
const CREATE_COMMIT = 'e6fa3c792facc06faa049a6938c84c411954deb5';
19+
const DELETE_COMMIT = '519d5693c72c925cd59205d9f11e9fa1d550028b';
20+
const CREATE_COMMIT = 'e6fa3c792facc06faa049a6938c84c411954deb5';
21+
const RENAME_COMMIT = '6640e0ef31518054847a1876328e26ee64083e0a';
22+
const CHANGEMODE_COMMIT = '93da965f58170f13017477b9a608657e87e23230';
2123

2224
/**
2325
* @dataProvider provideFoobar
@@ -91,6 +93,38 @@ public function testGetFiles_Deletion($repository)
9193
$this->assertEquals(1, $files[0]->getDeletions(), "1 line deleted");
9294
}
9395

96+
/**
97+
* @dataProvider provideFoobar
98+
*/
99+
public function testGetFiles_Rename($repository)
100+
{
101+
$files = $repository->getCommit(self::RENAME_COMMIT)->getDiff()->getFiles();
102+
103+
$this->assertEquals(1, count($files), "1 files modified");
104+
105+
$this->assertTrue($files[0]->isModification());
106+
$this->assertTrue($files[0]->isRename());
107+
$this->assertFalse($files[0]->isDeletion());
108+
$this->assertFalse($files[0]->isCreation());
109+
$this->assertFalse($files[0]->isChangeMode());
110+
}
111+
112+
/**
113+
* @dataProvider provideFoobar
114+
*/
115+
public function testGetFiles_Changemode($repository)
116+
{
117+
$files = $repository->getCommit(self::CHANGEMODE_COMMIT)->getDiff()->getFiles();
118+
119+
$this->assertEquals(1, count($files), "1 files modified");
120+
121+
$this->assertTrue($files[0]->isModification());
122+
$this->assertTrue($files[0]->isChangeMode());
123+
$this->assertFalse($files[0]->isDeletion());
124+
$this->assertFalse($files[0]->isCreation());
125+
$this->assertFalse($files[0]->isRename());
126+
}
127+
94128
/**
95129
* @dataProvider provideFoobar
96130
*/

0 commit comments

Comments
 (0)