Skip to content
This repository was archived by the owner on Dec 8, 2017. It is now read-only.

Commit b82dcbc

Browse files
author
Samuel Parkinson
committed
Fix hook install command when the hook doesn't exist.
1 parent cd31a9a commit b82dcbc

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

src/Command/HookInstallCommand.php

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
class HookInstallCommand extends Command
2323
{
24-
const ARGUMENT_TARGET = 'target';
25-
const ARGUMENT_LINK = 'link';
24+
const ARG_SOURCE = 'source';
25+
const ARG_TARGET = 'target';
2626

2727
protected function configure()
2828
{
@@ -31,13 +31,13 @@ protected function configure()
3131
$this->setDescription('Symlink a hook to the given target.');
3232

3333
$this->addArgument(
34-
self::ARGUMENT_TARGET,
34+
self::ARG_SOURCE,
3535
InputArgument::REQUIRED,
3636
'The hook to link, either a path to a file or the filename of a hook in the hooks folder.'
3737
);
3838

3939
$this->addArgument(
40-
self::ARGUMENT_LINK,
40+
self::ARG_TARGET,
4141
InputArgument::REQUIRED,
4242
'The target location, including the filename (e.g. .git/hooks/pre-commit).'
4343
);
@@ -47,58 +47,45 @@ protected function configure()
4747

4848
protected function execute(InputInterface $input, OutputInterface $output)
4949
{
50-
$hookArgument = $input->getArgument(self::ARGUMENT_TARGET);
51-
52-
$target = $this->getTargetPath($hookArgument);
53-
$link = $input->getArgument(self::ARGUMENT_LINK);
50+
$source = realpath($input->getArgument(self::ARG_SOURCE));
51+
$target = $input->getArgument(self::ARG_TARGET);
5452
$force = $input->getOption('force');
5553

5654
if ($output->isVeryVerbose()) {
57-
$message = sprintf('<info>Using %s for the install path.</info>', $link);
55+
$message = sprintf('<info>Using %s as the hook.</info>', $source);
5856
$output->writeln($message);
5957

60-
$message = sprintf('<info>Using %s as the hook.</info>', $target);
58+
$message = sprintf('<info>Using %s for the install path.</info>', $target);
6159
$output->writeln($message);
6260
}
6361

64-
if (! is_dir(dirname($link))) {
65-
$message = sprintf('<error>The directory at %s does not exist.</error>', $link);
62+
if (! file_exists($source)) {
63+
$error = sprintf('<error>The hook %s does not exist!</error>', $source);
64+
$output->writeln($error);
65+
exit(1);
66+
}
67+
68+
if (! is_dir(dirname($target))) {
69+
$message = sprintf('<error>The directory at %s does not exist.</error>', $target);
6670
$output->writeln($message);
6771
exit(1);
6872
}
6973

70-
if (file_exists($link) && $force) {
71-
unlink($link);
74+
if (file_exists($target) && $force) {
75+
unlink($target);
7276

73-
$message = sprintf('<comment>Removed existing file at %s.</comment>', $link);
77+
$message = sprintf('<comment>Removed existing file at %s.</comment>', $target);
7478
$output->writeln($message);
7579
}
7680

77-
if (! file_exists($link) || $force) {
78-
symlink($target, $link);
79-
chmod($link, 0755);
81+
if (! file_exists($target) || $force) {
82+
symlink($source, $target);
83+
chmod($target, 0755);
8084
$output->writeln('Symlink created.');
8185
} else {
82-
$message = sprintf('<error>A file at %s already exists.</error>', $link);
86+
$message = sprintf('<error>A file at %s already exists.</error>', $target);
8387
$output->writeln($message);
8488
exit(1);
8589
}
8690
}
87-
88-
/**
89-
* @param $hookArgument string
90-
* @return string
91-
*/
92-
protected function getTargetPath($hookArgument)
93-
{
94-
$target = realpath($hookArgument);
95-
96-
if (! file_exists($target)) {
97-
$error = sprintf('<error>The hook %s does not exist!</error>', $target);
98-
$output->writeln($error);
99-
exit(1);
100-
}
101-
102-
return $target;
103-
}
10491
}

0 commit comments

Comments
 (0)