Skip to content

Commit 9f6bdf7

Browse files
Merge pull request #222 from feature/diff-filter-conditions
Allow diff filter setting for conditions
2 parents cd4595a + 91f5c2f commit 9f6bdf7

File tree

8 files changed

+60
-23
lines changed

8 files changed

+60
-23
lines changed

src/Hook/Condition/FileStaged.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,25 @@ abstract class FileStaged extends File
3131
*
3232
* @var array<string>
3333
*/
34-
protected $filesToWatch;
34+
protected array $filesToWatch;
3535

3636
/**
37-
* FileChange constructor
37+
* --diff-filter options
3838
*
39-
* @param array<string> $files
39+
* @var array<int, string>
4040
*/
41-
public function __construct(array $files)
41+
protected array $diffFilter;
42+
43+
/**
44+
* FileStaged constructor
45+
*
46+
* @param array<int, string> $files
47+
* @param array<int, string> $diffFilter
48+
*/
49+
public function __construct(array $files, array $diffFilter = [])
4250
{
4351
$this->filesToWatch = $files;
52+
$this->diffFilter = $diffFilter;
4453
}
4554

4655
/**
@@ -70,6 +79,6 @@ abstract public function isTrue(IO $io, Repository $repository): bool;
7079
*/
7180
protected function getStagedFiles(Repository $repository)
7281
{
73-
return $repository->getIndexOperator()->getStagedFiles();
82+
return $repository->getIndexOperator()->getStagedFiles($this->diffFilter);
7483
}
7584
}

src/Hook/Condition/FileStaged/InDirectory.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,25 @@ class InDirectory implements Condition, Constrained
4545
*
4646
* @var string
4747
*/
48-
private $directory;
48+
private string $directory;
49+
50+
/**
51+
* --diff-filter options
52+
*
53+
* @var array<int, string>
54+
*/
55+
private array $diffFilter;
4956

5057
/**
5158
* InDirectory constructor
5259
*
53-
* @param string $directory
60+
* @param string $directory
61+
* @param array<int, string> $diffFilter
5462
*/
55-
public function __construct(string $directory)
63+
public function __construct(string $directory, array $diffFilter = [])
5664
{
57-
$this->directory = $directory;
65+
$this->directory = $directory;
66+
$this->diffFilter = $diffFilter;
5867
}
5968

6069
/**
@@ -76,7 +85,7 @@ public static function getRestriction(): Restriction
7685
*/
7786
public function isTrue(IO $io, Repository $repository): bool
7887
{
79-
$files = $repository->getIndexOperator()->getStagedFiles();
88+
$files = $repository->getIndexOperator()->getStagedFiles($this->diffFilter);
8089

8190
$filtered = [];
8291
foreach ($files as $file) {

src/Hook/Condition/FileStaged/OfType.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
* Class OfType
2323
*
2424
* All FileStaged conditions are only applicable for `pre-commit` hooks.
25+
* The diff filter argument is optional.
2526
*
2627
* Example configuration:
2728
*
2829
* "action": "some-action"
2930
* "conditions": [
3031
* {"exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\OfType",
3132
* "args": [
32-
* "php"
33+
* "php",
34+
* ["A", "C"]
3335
* ]}
3436
* ]
3537
*
@@ -45,16 +47,25 @@ class OfType implements Condition, Constrained
4547
*
4648
* @var string
4749
*/
48-
private $suffix;
50+
private string $suffix;
51+
52+
/**
53+
* --diff-filter option
54+
*
55+
* @var array<int, string>
56+
*/
57+
private array $diffFilter;
4958

5059
/**
5160
* OfType constructor
5261
*
53-
* @param string $type
62+
* @param string $type
63+
* @param array<int, string> $filter
5464
*/
55-
public function __construct(string $type)
65+
public function __construct(string $type, array $filter = [])
5666
{
57-
$this->suffix = $type;
67+
$this->suffix = $type;
68+
$this->diffFilter = $filter;
5869
}
5970

6071
/**
@@ -76,7 +87,7 @@ public static function getRestriction(): Restriction
7687
*/
7788
public function isTrue(IO $io, Repository $repository): bool
7889
{
79-
$files = $repository->getIndexOperator()->getStagedFilesOfType($this->suffix);
90+
$files = $repository->getIndexOperator()->getStagedFilesOfType($this->suffix, $this->diffFilter);
8091
return count($files) > 0;
8192
}
8293
}

src/Hook/Condition/FileStaged/ThatIs.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* "conditions": [
3030
* {"exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\ThatIs",
3131
* "args": [
32-
* {"ofType": "php", "inDirectory": "foo/"}
32+
* {"ofType": "php", "inDirectory": "foo/", "diff-filter": ["A", "C"]}
3333
* ]}
3434
* ]
3535
*
@@ -54,15 +54,23 @@ class ThatIs implements Condition, Constrained
5454
*/
5555
private array $suffixes;
5656

57+
/**
58+
* --diff-filter options
59+
*
60+
* @var array<int, string>
61+
*/
62+
private array $diffFilter;
63+
5764
/**
5865
* OfType constructor
5966
*
60-
* @param array<string, string> $options
67+
* @param array<string, mixed> $options
6168
*/
6269
public function __construct(array $options)
6370
{
6471
$this->directories = (array)($options['inDirectory'] ?? []);
6572
$this->suffixes = (array)($options['ofType'] ?? []);
73+
$this->diffFilter = (array)($options['diffFilter'] ?? []);
6674
}
6775

6876
/**
@@ -84,7 +92,7 @@ public static function getRestriction(): Restriction
8492
*/
8593
public function isTrue(IO $io, Repository $repository): bool
8694
{
87-
$files = $repository->getIndexOperator()->getStagedFiles();
95+
$files = $repository->getIndexOperator()->getStagedFiles($this->diffFilter);
8896
$files = $this->filterFilesByDirectory($files);
8997
$files = $this->filterFilesByType($files);
9098
return count($files) > 0;

tests/unit/Hook/Condition/FileStaged/AllTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testIsTrue(): void
6969
$repository = $this->createRepositoryMock('');
7070
$repository->expects($this->once())->method('getIndexOperator')->willReturn($operator);
7171

72-
$fileStaged = new All(['foo.php', 'bar.php']);
72+
$fileStaged = new All(['foo.php', 'bar.php'], ['A', 'C']);
7373

7474
$this->assertTrue($fileStaged->isTrue($io, $repository));
7575
}

tests/unit/Hook/Condition/FileStaged/InDirectoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testStagedFalse(): void
5353
$index = $this->createGitIndexOperator(['src/foo.php', 'src/bar.php']);
5454
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index);
5555

56-
$condition = new InDirectory('tests/');
56+
$condition = new InDirectory('tests/', ['A', 'C']);
5757
$this->assertFalse($condition->isTrue($io, $repo));
5858
}
5959
}

tests/unit/Hook/Condition/FileStaged/OfTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testStagedFalse(): void
5555
$index->expects($this->once())->method('getStagedFilesOfType')->willReturn([]);
5656
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index);
5757

58-
$ofType = new OfType('js');
58+
$ofType = new OfType('js', ['A', 'C']);
5959
$this->assertFalse($ofType->isTrue($io, $repo));
6060
}
6161
}

tests/unit/Hook/Condition/FileStaged/ThatIsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testStagedFalseMultipleDirectory(): void
123123
$index = $this->createGitIndexOperator(['foo/foo.php', 'bar/bar.js', 'fiz/baz.txt']);
124124
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index);
125125

126-
$thatIs = new ThatIs(['inDirectory' => ['foobar/', 'baz/']]);
126+
$thatIs = new ThatIs(['inDirectory' => ['foobar/', 'baz/'], 'diffFilter' => ['A', 'C']]);
127127
$this->assertFalse($thatIs->isTrue($io, $repo));
128128
}
129129

0 commit comments

Comments
 (0)