Skip to content

Commit 1a029ea

Browse files
Allow run-path to overwrite absolute path
In order to not use absolute path references for symlinks that point to binaries outside the repository users can now use the run-path config value to disable the detection behaviour and set the path to the Captain binary manually. Issue #219
1 parent c3a6acf commit 1a029ea

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/Hook/Template/Local/Shell.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ protected function getHookLines(string $hook): array
6363
];
6464
}
6565

66-
$executable = $this->config->getPhpPath() === ''
67-
? $this->pathInfo->getExecutablePath()
68-
: $this->config->getPhpPath() . ' ' . $this->pathInfo->getExecutablePath();
69-
7066
return array_merge(
7167
[
7268
'#!/bin/sh',
@@ -78,12 +74,28 @@ protected function getHookLines(string $hook): array
7874
$useTTY,
7975
[
8076
'',
81-
$executable
77+
$this->getExecutable()
8278
. ' $INTERACTIVE'
8379
. ' --configuration=' . $this->pathInfo->getConfigPath()
8480
. ' --bootstrap=' . $this->config->getBootstrap()
8581
. ' hook:' . $hook . ' "$@"' . $useStdIn,
8682
]
8783
);
8884
}
85+
86+
/**
87+
* Returns the path to the executable including a configured php executable
88+
*
89+
* @return string
90+
*/
91+
private function getExecutable(): string
92+
{
93+
$executable = !empty($this->config->getPhpPath()) ? $this->config->getPhpPath() . ' ' : '';
94+
95+
if (!empty($this->config->getRunPath())) {
96+
return $executable . $this->config->getRunPath();
97+
}
98+
99+
return $executable . $this->pathInfo->getExecutablePath();
100+
}
89101
}

tests/unit/Hook/Template/Local/ShellTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ public function testTemplateWithDefinedPHP(): void
6363
$this->assertStringContainsString('vendor/bin/captainhook $INTERACTIVE', $code);
6464
}
6565

66+
/**
67+
* Tests Shell::getCode
68+
*/
69+
public function testTemplateWithDefinedPHPAndRunPath(): void
70+
{
71+
$pathInfo = $this->createMock(PathInfo::class);
72+
$pathInfo->method('getExecutablePath')->willReturn('vendor/bin/captainhook');
73+
$pathInfo->method('getConfigPath')->willReturn('captainhook.json');
74+
75+
$config = $this->createConfigMock(false, 'captainhook.json');
76+
$config->method('getBootstrap')->willReturn('vendor/autoload.php');
77+
$config->method('getPhpPath')->willReturn('/usr/bin/php7.4');
78+
$config->method('getRunPath')->willReturn('tools/captainhook.phar');
79+
80+
$template = new Shell($pathInfo, $config, false);
81+
$code = $template->getCode('commit-msg');
82+
83+
$this->assertStringContainsString('#!/bin/sh', $code);
84+
$this->assertStringContainsString('commit-msg', $code);
85+
$this->assertStringContainsString('/usr/bin/php7.4', $code);
86+
$this->assertStringContainsString('tools/captainhook.phar $INTERACTIVE', $code);
87+
}
88+
6689
/**
6790
* Tests Shell::getCode
6891
*/

0 commit comments

Comments
 (0)