From c39d5941ebc6f06cc3b166d45f24ec6ba03691d0 Mon Sep 17 00:00:00 2001 From: f1r3ph03n1xX Date: Wed, 13 Oct 2021 19:56:01 +0200 Subject: [PATCH] Prevent error output if we are in a git worktree Add Output of an comment: ARRRRR! We ARRR in a worktree, no install attempted --- src/ComposerPlugin.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/ComposerPlugin.php b/src/ComposerPlugin.php index 7080251..e317c07 100644 --- a/src/ComposerPlugin.php +++ b/src/ComposerPlugin.php @@ -66,6 +66,11 @@ class ComposerPlugin implements PluginInterface, EventSubscriberInterface */ private $gitDirectory; + /** + * @var bool + */ + private $isWorktree = false; + /** * Activate the plugin * @@ -145,6 +150,10 @@ public function installHooks(Event $event): void $this->detectConfiguration(); $this->detectGitDir(); + if ($this->isWorktree) { + $this->io->write(' ARRRRR! We ARRR in a worktree, no install attempted'); + return; + } $this->detectCaptainExecutable(); if (!file_exists($this->executable)) { @@ -274,6 +283,15 @@ private function detectGitDir(): void if (is_dir($possibleGitDir)) { $this->gitDirectory = $possibleGitDir; return; + } elseif (is_file($possibleGitDir)) { + $gitfile = file($possibleGitDir); + $match = []; + preg_match('#^gitdir: (?[a-zA-Z/\.]*\.git)#', $gitfile[0] ?? '', $match); + $dir = $match['gitdir'] ?? ''; + if (is_dir($dir)) { + $this->isWorktree = true; + } + } // if we checked the root directory already, break to prevent endless loop @@ -283,6 +301,9 @@ private function detectGitDir(): void $path = \dirname($path); } + if ($this->isWorktree) { + return; + } throw new RuntimeException($this->pluginErrorMessage('git directory not found')); }