Skip to content

Commit 8417dbe

Browse files
Move tty logic to a central place
1 parent 467cfe7 commit 8417dbe

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

src/Hook/Template/Local/Shell.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
use CaptainHook\App\CH;
1717
use CaptainHook\App\Hook\Template;
18+
use CaptainHook\App\Hooks;
1819
use SebastianFeldmann\Camino\Path;
1920
use SebastianFeldmann\Camino\Path\Directory;
2021
use Symfony\Component\Process\PhpExecutableFinder;
2122

2223
/**
23-
* Local class
24+
* Shell class
2425
*
2526
* Generates the sourcecode for the php hook scripts in .git/hooks/*.
2627
*
@@ -31,15 +32,6 @@
3132
*/
3233
class Shell extends Template\Local
3334
{
34-
/**
35-
* Does the hook allow user input
36-
*
37-
* @var bool[]
38-
*/
39-
private array $allowUserInput = [
40-
'prepare-commit-msg' => true
41-
];
42-
4335
/**
4436
* Returns lines of code for the local src installation
4537
*
@@ -51,7 +43,7 @@ protected function getHookLines(string $hook): array
5143
$useStdIn = ' <&0';
5244
$useTTY = [];
5345

54-
if (isset($this->allowUserInput[$hook])) {
46+
if (Hooks::allowsUserInput($hook)) {
5547
$useStdIn = '';
5648
$useTTY = [
5749
'if [ -t 1 ]; then',

src/Hooks.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,21 @@ final class Hooks
5151
*
5252
* @var string[]
5353
*/
54-
private static $virtualHookTriggers = [
54+
private static array $virtualHookTriggers = [
5555
self::POST_CHECKOUT => self::POST_CHANGE,
5656
self::POST_MERGE => self::POST_CHANGE,
5757
self::POST_REWRITE => self::POST_CHANGE,
5858
];
5959

60+
/**
61+
* Is it necessary to give the Captain access to user input
62+
*
63+
* @var array<string, bool>
64+
*/
65+
private static array $hooksAllowingUserInput = [
66+
self::PREPARE_COMMIT_MSG => true,
67+
];
68+
6069
/**
6170
* Returns the list of valid hooks
6271
*
@@ -137,6 +146,17 @@ public static function getOriginalHookArguments(string $hook): string
137146
return $arguments[$hook];
138147
}
139148

149+
/**
150+
* Does a given hook allow for user input to be used
151+
*
152+
* @param string $hook
153+
* @return bool
154+
*/
155+
public static function allowsUserInput(string $hook): bool
156+
{
157+
return self::$hooksAllowingUserInput[$hook] ?? false;
158+
}
159+
140160
/**
141161
* Tell if the given hook should trigger a virtual hook
142162
*

tests/unit/HooksTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,10 @@ public function testGetVirtualHookFail(): void
6363
$this->expectException(Exception::class);
6464
Hooks::getVirtualHook('pre-commit');
6565
}
66+
67+
public function testAllowsUserInput(): void
68+
{
69+
$this->assertTrue(Hooks::allowsUserInput(Hooks::PREPARE_COMMIT_MSG));
70+
$this->assertFalse(Hooks::allowsUserInput(Hooks::PRE_COMMIT));
71+
}
6672
}

0 commit comments

Comments
 (0)