Skip to content

Commit b2905bc

Browse files
authored
Merge pull request BrainMaestro#97 from black-silence/accept_existing_correct_hooks
change message when hooks already exist with correct contents
2 parents 02f95c8 + abd9044 commit b2905bc

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

cghooks

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use BrainMaestro\GitHooks\Commands\ListCommand;
2222
use BrainMaestro\GitHooks\Commands\HookCommand;
2323
use Symfony\Component\Console\Application;
2424

25-
$application = new Application('Composer Git Hooks', '2.8.0');
25+
$application = new Application('Composer Git Hooks', '2.8.1');
2626

2727
$application->add(new AddCommand());
2828
$application->add(new UpdateCommand());

src/Commands/AddCommand.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class AddCommand extends Command
99
{
1010
private $addedHooks = [];
11+
private $upToDateHooks = [];
1112

1213
protected $force;
1314
protected $noLock;
@@ -51,7 +52,10 @@ protected function command()
5152
$this->addHook($hook, $contents);
5253
}
5354

54-
if (! count($this->addedHooks)) {
55+
if (! empty($this->hooks) && count($this->upToDateHooks) === count($this->hooks)) {
56+
$this->info('All hooks are up to date');
57+
return;
58+
} elseif (! count($this->addedHooks)) {
5559
$this->error('No hooks were added. Try updating');
5660
return;
5761
}
@@ -78,13 +82,22 @@ private function addHook($hook, $contents)
7882
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
7983
$shebang = ($this->windows ? '#!/bin/bash' : '#!/bin/sh') . PHP_EOL . PHP_EOL;
8084
$contents = is_array($contents) ? implode(PHP_EOL, $contents) : $contents;
85+
$hookContents = $shebang . $contents . PHP_EOL;
8186

8287
if (! $this->force && $exists) {
88+
$actualContents = file_get_contents($filename);
89+
90+
if ($actualContents === $hookContents) {
91+
$this->debug("[{$hook}] is up to date");
92+
$this->upToDateHooks[] = $hook;
93+
return;
94+
}
95+
8396
$this->debug("[{$hook}] already exists");
8497
return;
8598
}
8699

87-
file_put_contents($filename, $shebang . $contents . PHP_EOL);
100+
file_put_contents($filename, $hookContents);
88101
chmod($filename, 0755);
89102

90103
$operation = $exists ? 'Updated' : 'Added';

tests/AddCommandTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ public function it_does_not_add_hooks_that_already_exist()
6363
$this->assertContains('No hooks were added. Try updating', $this->commandTester->getDisplay());
6464
}
6565

66+
/**
67+
* @test
68+
*/
69+
public function it_detects_existing_correct_hooks()
70+
{
71+
$originalHooks = self::$hooks;
72+
self::$hooks = [
73+
'pre-commit' => '#!/bin/sh' . PHP_EOL . PHP_EOL . 'echo before-commit' . PHP_EOL,
74+
'post-commit' => '#!/bin/sh' . PHP_EOL . PHP_EOL . 'echo after-commit' . PHP_EOL,
75+
];
76+
77+
self::createHooks();
78+
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
79+
80+
foreach (array_keys(self::$hooks) as $hook) {
81+
$this->assertContains("{$hook} is up to date", $this->commandTester->getDisplay());
82+
}
83+
$this->assertContains('All hooks are up to date', $this->commandTester->getDisplay());
84+
85+
self::$hooks = $originalHooks;
86+
}
87+
6688
/**
6789
* @test
6890
*/

0 commit comments

Comments
 (0)