Skip to content

Commit bad5278

Browse files
jingyu liuBrainMaestro
jingyu liu
authored andcommitted
Create hooks directory when it does not exist (BrainMaestro#5)
1 parent 726a647 commit bad5278

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/Commands/AddCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
3535
{
3636
$addedHooks = [];
3737
$gitDir = $input->getOption('git-dir');
38+
$hookDir = "{$gitDir}/hooks";
39+
40+
if (! is_dir($hookDir)) {
41+
mkdir($hookDir, 0700, true);
42+
}
3843

3944
foreach ($this->hooks as $hook => $script) {
4045
$filename = "{$gitDir}/hooks/{$hook}";

src/Commands/UpdateCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ protected function configure()
3131
protected function execute(InputInterface $input, OutputInterface $output)
3232
{
3333
$gitDir = $input->getOption('git-dir');
34+
$hookDir = "{$gitDir}/hooks";
35+
36+
if (! is_dir($hookDir)) {
37+
mkdir($hookDir, 0700, true);
38+
}
3439

3540
foreach ($this->hooks as $hook => $script) {
3641
$filename = "{$gitDir}/hooks/{$hook}";

tests/AddCommandTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,25 @@ public function it_does_not_create_a_lock_file_when_no_hooks_were_added()
120120
$this->assertFalse(file_exists(".git/hooks/{$hook}"));
121121
}
122122
}
123+
124+
/**
125+
* @test
126+
*/
127+
public function it_create_git_hooks_path_when_hooks_dir_not_exists()
128+
{
129+
$gitDirs = ['test-git-dir', '.git'];
130+
131+
foreach ($gitDirs as $gitDir) {
132+
if (file_exists($hookDir = "{$gitDir}/hooks")) {
133+
rmdir($hookDir);
134+
}
135+
136+
$this->commandTester->execute(['--git-dir' => $gitDir]);
137+
138+
foreach (array_keys(self::$hooks) as $hook) {
139+
$this->assertTrue(file_exists("{$gitDir}/hooks/{$hook}"));
140+
unlink("{$gitDir}/hooks/{$hook}");
141+
}
142+
}
143+
}
123144
}

tests/UpdateCommandTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,29 @@ public function it_uses_a_different_git_path_if_specified()
6060

6161
passthru("rm -rf {$gitDir}");
6262
}
63+
64+
/**
65+
* @test
66+
*/
67+
public function it_create_git_hooks_path_when_hooks_dir_not_exists()
68+
{
69+
$gitDirs = ['test-git-dir', '.git'];
70+
71+
foreach ($gitDirs as $gitDir) {
72+
if (file_exists($hookDir = "{$gitDir}/hooks")) {
73+
rmdir($hookDir);
74+
}
75+
76+
$this->commandTester->execute(['--git-dir' => $gitDir]);
77+
78+
foreach (array_keys(self::$hooks) as $hook) {
79+
$this->assertTrue(file_exists("{$gitDir}/hooks/{$hook}"));
80+
unlink("{$gitDir}/hooks/{$hook}");
81+
}
82+
83+
if ($gitDir != '.git') {
84+
passthru("rm -rf {$gitDir}");
85+
}
86+
}
87+
}
6388
}

0 commit comments

Comments
 (0)