Skip to content

Commit be00927

Browse files
committed
Possibility to list only git staged files
1 parent be331ac commit be00927

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

src/Git/ChangedFile.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ public function getFilePath(): string
9393
return $this->file;
9494
}
9595

96+
/**
97+
* @return bool
98+
*/
99+
public function isStaged(): bool
100+
{
101+
return $this->isAdded() || $this->isModified() || $this->isCopied();
102+
}
103+
96104
/**
97105
* @return bool
98106
*/
@@ -125,6 +133,14 @@ public function isUntracked(): bool
125133
return $this->X & static::N || $this->Y & static::N;
126134
}
127135

136+
/**
137+
* @return bool
138+
*/
139+
public function isCopied(): bool
140+
{
141+
return $this->X & static::C || $this->Y & static::C;
142+
}
143+
128144
/**
129145
* @return string
130146
*/

src/Git/ChangedFiles.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ public function getFiles(): Collection
3535
return $this->files;
3636
}
3737

38+
/**
39+
* Get list of staged files
40+
*
41+
* @return Collection
42+
*/
43+
public function getStaged(): Collection
44+
{
45+
return $this->files->filter(function (ChangedFile $file) {
46+
return $file->isStaged();
47+
});
48+
}
49+
3850
/**
3951
* Get added to commit files
4052
*

tests/Datasets/ModifiedFilesDataset.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
false,
99
false,
1010
false,
11+
false,
1112
],
1213
'added and modified files' => [
1314
'AM src/ChangedFiles.php',
@@ -16,6 +17,7 @@
1617
false,
1718
false,
1819
true,
20+
true,
1921
],
2022
'modified file' => [
2123
' M src/Console/Commands/CommitMessage.php',
@@ -24,6 +26,7 @@
2426
false,
2527
false,
2628
false,
29+
true,
2730
],
2831
'deleted file' => [
2932
' D LICENSE',
@@ -32,6 +35,7 @@
3235
true,
3336
false,
3437
false,
38+
false,
3539
],
3640
'untracked file' => [
3741
'?? LICENSE',
@@ -40,6 +44,7 @@
4044
false,
4145
true,
4246
false,
47+
false,
4348
],
4449
]);
4550

tests/Unit/Git/ChangedFileTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
use Igorsgm\GitHooks\Git\ChangedFile;
44

5-
test('Gets file meta', function (string $file, bool $isAdded, bool $isModified, bool $isDeleted, bool $isUntracked, bool $inCommit) {
5+
test('Gets file meta', function (string $file, bool $isAdded, bool $isModified, bool $isDeleted, bool $isUntracked, bool $inCommit, bool $isStaged) {
66
$file = new ChangedFile($file);
77

88
$this->assertEquals($isAdded, $file->isAdded());
99
$this->assertEquals($isModified, $file->isModified());
1010
$this->assertEquals($isDeleted, $file->isDeleted());
1111
$this->assertEquals($isUntracked, $file->isUntracked());
1212
$this->assertEquals($inCommit, $file->isInCommit());
13+
$this->assertEquals($isStaged, $file->isStaged());
1314
})->with('modifiedFilesMeta');
1415

1516
test('Gets files', function () {

tests/Unit/Git/ChangedFilesTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@
2424
]);
2525
})->with('modifiedFilesList');
2626

27+
test('Gets staged files', function ($modifiedFiles) {
28+
$files = new ChangedFiles($modifiedFiles);
29+
30+
$filesAddedToCommit = $files->getStaged()->map->__toString()->values()->all();
31+
32+
expect($filesAddedToCommit)
33+
->toBe([
34+
'M src/Console/Commands/CommitMessage.php',
35+
'M src/Contracts/MessageHook.php',
36+
'AM src/Git/ChangedFile.php',
37+
'AM src/Git/ChangedFiles.php',
38+
'A src/Git/CommitMessage.php',
39+
'M tests/Console/Commands/CommitMessageTest.php',
40+
'M tests/Console/Commands/PrepareCommitMessageTest.php',
41+
'AM tests/Git/ChangedFileTest.php',
42+
'AM tests/Git/ChangedFilesTest.php',
43+
]);
44+
})->with('modifiedFilesList');
45+
2746
test('Gets deleted to commit files', function ($modifiedFiles) {
2847
$files = new ChangedFiles($modifiedFiles);
2948

0 commit comments

Comments
 (0)