Skip to content

Commit 676162c

Browse files
authored
Merge pull request #106 from ggcasuso/feature/execute-hooks-only-if-previous-was-successful
Concat hooks with &&
2 parents 97888dd + a5d081d commit 676162c

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

src/Commands/AddCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private function addHook($hook, $contents)
8282
// On windows, the shebang needs to point to bash
8383
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
8484
$shebang = ($this->windows ? '#!/bin/bash' : '#!/bin/sh') . PHP_EOL . PHP_EOL;
85-
$contents = is_array($contents) ? implode(PHP_EOL, $contents) : $contents;
85+
$contents = get_hook_contents($contents);
8686
$hookContents = $shebang . $contents . PHP_EOL;
8787

8888
if (! $this->force && $exists) {

src/Commands/HookCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function configure()
2929

3030
protected function execute(InputInterface $input, OutputInterface $output)
3131
{
32-
$contents = is_array($this->contents) ? implode(PHP_EOL, $this->contents) : $this->contents;
32+
$contents = get_hook_contents($this->contents);
3333

3434
$outputMessage = [];
3535
$returnCode = 0;

src/helpers.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,17 @@ function git_dir()
5757
return realpath($gitDir);
5858
}
5959
}
60+
61+
if (! function_exists('get_hook_contents')) {
62+
/**
63+
* Return hook contents
64+
*
65+
* @param array|string $contents
66+
*
67+
* @return string
68+
*/
69+
function get_hook_contents($contents)
70+
{
71+
return is_array($contents) ? implode(" && \\" . PHP_EOL, $contents) : $contents;
72+
}
73+
}

tests/AddCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public function it_handles_commands_defined_in_an_array()
260260
$this->assertContains("Added {$hook} hook", $this->commandTester->getDisplay());
261261

262262
$content = file_get_contents(".git/hooks/" . $hook);
263-
$this->assertContains(implode(PHP_EOL, $scripts), $content);
263+
$this->assertContains(implode(" && \\" . PHP_EOL, $scripts), $content);
264264
}
265265
}
266266

tests/HookCommandTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,24 @@ public function it_tests_hooks_that_exist()
2020
$this->assertContains(str_replace('echo ', '', $script), $commandTester->getDisplay());
2121
}
2222
}
23+
24+
/**
25+
* @test
26+
*/
27+
public function it_terminates_if_previous_hook_fails()
28+
{
29+
$hook = [
30+
'pre-commit' => [
31+
'echo execution-error;exit 1',
32+
'echo before-commit'
33+
],
34+
];
35+
36+
$command = new HookCommand('pre-commit', $hook['pre-commit']);
37+
$commandTester = new CommandTester($command);
38+
39+
$commandTester->execute([]);
40+
$this->assertContains('execution-error', $commandTester->getDisplay());
41+
$this->assertNotContains('before-commit', $commandTester->getDisplay());
42+
}
2343
}

tests/UpdateCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function it_handles_commands_defined_in_an_array()
138138
$this->assertContains("Updated {$hook} hook", $this->commandTester->getDisplay());
139139

140140
$content = file_get_contents(".git/hooks/" . $hook);
141-
$this->assertContains(implode(PHP_EOL, $scripts), $content);
141+
$this->assertContains(implode(" && \\" . PHP_EOL, $scripts), $content);
142142
}
143143
}
144144

0 commit comments

Comments
 (0)