Skip to content

Commit d9618b2

Browse files
arcticfalconBrainMaestro
authored andcommitted
Add option to force Windows compatibility (#10)
* Add option to force Windows compatibility * Remove unnecessary explicit equality check
1 parent d11d884 commit d9618b2

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Option | Description | Command
6969
------ | ----------- | -------
7070
`no-lock` | Do not create a lock file | `cghooks add --no-lock`
7171
`ignore-lock` | Add the lock file to .gitignore | `cghooks add --ignore-lock`
72+
`force-win` | Force windows bash compatibility | `cghooks add --force-win`
7273

7374
The `lock` file contains a list of all added hooks.
7475

src/Commands/AddCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ protected function configure()
2828
->addOption('no-lock', 'l', InputOption::VALUE_NONE, 'Do not create a lock file')
2929
->addOption('ignore-lock', 'i', InputOption::VALUE_NONE, 'Add the lock file to .gitignore')
3030
->addOption('git-dir', 'g', InputOption::VALUE_REQUIRED, 'Path to git directory', '.git')
31+
->addOption('force-win', null, InputOption::VALUE_NONE, 'Force windows bash compatibility')
3132
;
3233
}
3334

3435
protected function execute(InputInterface $input, OutputInterface $output)
3536
{
3637
$addedHooks = [];
3738
$gitDir = $input->getOption('git-dir');
39+
$forceWindows = $input->getOption('force-win');
3840
$hookDir = "{$gitDir}/hooks";
3941

4042
if (! is_dir($hookDir)) {
@@ -47,7 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4749
if (file_exists($filename)) {
4850
$output->writeln("<comment>{$hook} already exists</comment>");
4951
} else {
50-
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
52+
if ($forceWindows || strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
5153
// On windows we need to add a SHEBANG
5254
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
5355
$script = '#!/bin/bash' . PHP_EOL . $script;

src/Commands/UpdateCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ protected function configure()
2525
->setDescription('Update git hooks specified in the composer config')
2626
->setHelp('This command allows you to update git hooks')
2727
->addOption('git-dir', 'g', InputOption::VALUE_REQUIRED, 'Path to git directory', '.git')
28+
->addOption('force-win', null, InputOption::VALUE_NONE, 'Force windows bash compatibility')
2829
;
2930
}
3031

3132
protected function execute(InputInterface $input, OutputInterface $output)
3233
{
3334
$gitDir = $input->getOption('git-dir');
35+
$forceWindows = $input->getOption('force-win');
3436
$hookDir = "{$gitDir}/hooks";
3537

3638
if (! is_dir($hookDir)) {
@@ -42,7 +44,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4244

4345
$operation = file_exists($filename) ? 'Updated' : 'Added';
4446

45-
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
47+
if ($forceWindows || strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
4648
// On windows we need to add a SHEBANG
4749
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
4850
$script = '#!/bin/bash' . PHP_EOL . $script;

tests/AddCommandTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,20 @@ public function it_create_git_hooks_path_when_hooks_dir_not_exists()
161161

162162
rmdir($hookDir);
163163
}
164+
165+
/**
166+
* @test
167+
*/
168+
public function it_adds_win_bash_compat_if_the_force_windows_option_is_passed()
169+
{
170+
$this->commandTester->execute(['--force-win' => true]);
171+
172+
foreach (array_keys(self::$hooks) as $hook) {
173+
$this->assertContains("Added {$hook} hook", $this->commandTester->getDisplay());
174+
175+
$content = file_get_contents(".git/hooks/" . $hook);
176+
$this->assertNotFalse(strpos($content, "#!/bin/bash"));
177+
$this->assertEquals(strpos($content, "#!/bin/bash"), 0);
178+
}
179+
}
164180
}

tests/UpdateCommandTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,22 @@ public function it_create_git_hooks_path_when_hooks_dir_not_exists()
101101

102102
rmdir($hookDir);
103103
}
104+
105+
106+
/**
107+
* @test
108+
*/
109+
public function it_adds_win_bash_compat_if_the_force_windows_option_is_passed()
110+
{
111+
self::createHooks();
112+
$this->commandTester->execute(['--force-win' => true]);
113+
114+
foreach (array_keys(self::$hooks) as $hook) {
115+
$this->assertContains("Updated {$hook} hook", $this->commandTester->getDisplay());
116+
117+
$content = file_get_contents(".git/hooks/" . $hook);
118+
$this->assertNotFalse(strpos($content, "#!/bin/bash"));
119+
$this->assertEquals(strpos($content, "#!/bin/bash"), 0);
120+
}
121+
}
104122
}

0 commit comments

Comments
 (0)