Skip to content

Commit ff6b55a

Browse files
Extract and add verbose output
1 parent ee0d45d commit ff6b55a

3 files changed

Lines changed: 69 additions & 10 deletions

File tree

src/Git/ChangedFiles.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook
5+
*
6+
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Git;
13+
14+
use CaptainHook\App\Console\IO;
15+
use CaptainHook\App\Git\ChangedFiles\Detector\Factory;
16+
use SebastianFeldmann\Git\Repository;
17+
18+
/**
19+
* Class ChangedFiles
20+
*
21+
* @package CaptainHook
22+
* @author Sebastian Feldmann <sf@sebastian-feldmann.info>
23+
* @link https://github.com/captainhook-git/captainhook
24+
* @since Class available since Release 5.2.0
25+
*/
26+
abstract class ChangedFiles
27+
{
28+
/**
29+
* Returns the list of changed files using the necessary Detector
30+
*
31+
* @param \CaptainHook\App\Console\IO $io
32+
* @param \SebastianFeldmann\Git\Repository $repository
33+
* @param array<string> $filter
34+
* @return array<string>
35+
*/
36+
public static function getChangedFiles(IO $io, Repository $repository, array $filter): array
37+
{
38+
$factory = new Factory();
39+
$detector = $factory->getDetector($io, $repository);
40+
41+
$files = $detector->getChangedFiles($filter);
42+
self::displayFilesFound($io, $files);
43+
return $files;
44+
}
45+
46+
/**
47+
* Output the changed files in verbose mode
48+
*
49+
* @param \CaptainHook\App\Console\IO $io
50+
* @param array<string> $files
51+
* @return void
52+
*/
53+
54+
private static function displayFilesFound(IO $io, array $files): void
55+
{
56+
if ($io->isVerbose()) {
57+
$io->write(' <info>Changed files</info>', true, IO::VERBOSE);
58+
foreach ($files as $file) {
59+
$io->write(' - ' . $file, true, IO::VERBOSE);
60+
}
61+
}
62+
}
63+
}

src/Hook/Condition/FileChanged.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace CaptainHook\App\Hook\Condition;
1313

1414
use CaptainHook\App\Console\IO;
15-
use CaptainHook\App\Git\ChangedFiles\Detector\Factory;
15+
use CaptainHook\App\Git;
1616
use CaptainHook\App\Hook\Restriction;
1717
use CaptainHook\App\Hooks;
1818
use SebastianFeldmann\Git\Repository;
@@ -86,9 +86,6 @@ abstract public function isTrue(IO $io, Repository $repository): bool;
8686
*/
8787
protected function getChangedFiles(IO $io, Repository $repository): array
8888
{
89-
$factory = new Factory();
90-
$detector = $factory->getDetector($io, $repository);
91-
92-
return $detector->getChangedFiles($this->filter);
89+
return Git\ChangedFiles::getChangedFiles($io, $repository, $this->filter);
9390
}
9491
}

src/Hook/Condition/FileChanged/OfType.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ public static function getRestriction(): Restriction
7676
*/
7777
public function isTrue(IO $io, Repository $repository): bool
7878
{
79-
$factory = new Git\ChangedFiles\Detector\Factory();
80-
$detector = $factory->getDetector($io, $repository);
81-
82-
$files = $detector->getChangedFiles(['A', 'C', 'M', 'R']);
83-
$files = FileList::filterByType($files, ['of-type' => $this->suffix]);
79+
$files = FileList::filterByType(
80+
Git\ChangedFiles::getChangedFiles($io, $repository, ['A', 'C', 'M', 'R']),
81+
['of-type' => $this->suffix]
82+
);
8483

8584
if (count($files) > 0) {
8685
return true;

0 commit comments

Comments
 (0)