From 80f6894bcc32959aba421af1dcc2f3fb1032eeef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20L=C3=B3pez?= Date: Wed, 27 Nov 2024 11:13:40 +0100 Subject: [PATCH 1/2] Issue #19: Added functionality to check if there is git installed and we are in a git repository --- src/BaseCommand.php | 31 +++++++++++++++++++++++++---- src/DrupalArtifactBuilderCreate.php | 1 - src/DrupalArtifactBuilderGit.php | 4 ++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/BaseCommand.php b/src/BaseCommand.php index 010e72c..5c6abfe 100644 --- a/src/BaseCommand.php +++ b/src/BaseCommand.php @@ -177,11 +177,15 @@ protected function assertArtifactContentIsClean() { $this->getSymlinks(), $this->getConfiguration()->getInclude(), )); - $files_changed = trim($this->runCommand(sprintf("git status -s %s", implode(' ', $artifact_content)))->getOutput()); - if (strlen($files_changed > 0)) { - $list = implode("\n", array_map(function($item) { $parts = explode(' ', $item); return $parts[1]; }, explode("\n", $files_changed))); - throw new \Exception("There are changes in the repository (changed and/or untracked files), please run this artifact generation script with folder tree clean. Files changed:\n$list"); + + if ($this->gitCommandExist() && $this->isRootARepository()) { + $files_changed = trim($this->runCommand(sprintf("git status -s %s", implode(' ', $artifact_content)))->getOutput()); + if (strlen($files_changed > 0)) { + $list = implode("\n", array_map(function($item) { $parts = explode(' ', $item); return $parts[1]; }, explode("\n", $files_changed))); + throw new \Exception("There are changes in the repository (changed and/or untracked files), please run this artifact generation script with folder tree clean. Files changed:\n$list"); + } } + } /** @@ -236,4 +240,23 @@ protected function getSymlinks() { return ['docroot', 'web', 'public_html']; } + /** + * Check if git command exists + * + * @return bool + */ + protected function gitCommandExist() { + return $this->runCommand('which git')->isSuccessful(); + } + + /** + * Check if we are in a git repository + * + * @return bool + */ + protected function isRootARepository() { + $command_output = $this->runCommand('git rev-parse --is-inside-work-tree || true')->getOutput(); + return $command_output === 'true'; + } + } diff --git a/src/DrupalArtifactBuilderCreate.php b/src/DrupalArtifactBuilderCreate.php index a95ff0e..2667312 100644 --- a/src/DrupalArtifactBuilderCreate.php +++ b/src/DrupalArtifactBuilderCreate.php @@ -38,7 +38,6 @@ protected function execute(InputInterface $input, OutputInterface $output) : int protected function generateArtifact() { // Create the folder with the artifact. $this->createArtifactFolder(); - $this->assertArtifactContentIsClean(); $this->log('Cleaning previous artifact'); $this->log('##########################'); diff --git a/src/DrupalArtifactBuilderGit.php b/src/DrupalArtifactBuilderGit.php index bdb6ec4..41a0aa5 100644 --- a/src/DrupalArtifactBuilderGit.php +++ b/src/DrupalArtifactBuilderGit.php @@ -32,6 +32,10 @@ protected function configure() { protected function initialize(InputInterface $input, OutputInterface $output) { parent::initialize($input, $output); + if (!$this->gitCommandExist()) { + throw new \RuntimeException("Git command not found. Git must be installed and available in the PATH variable to generate an artifact."); + } + // Branch setup. if ($input->hasOption('repository') && !empty($input->getOption('repository'))) { $this->config->setRepository($input->getOption('repository')); From 4a3528e14f1659e82d1ae6d1b7f3f0d76309874c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20L=C3=B3pez?= Date: Wed, 27 Nov 2024 12:06:01 +0100 Subject: [PATCH 2/2] Issue #19: Renamed gitRepository function --- src/BaseCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BaseCommand.php b/src/BaseCommand.php index 5c6abfe..55aa4f1 100644 --- a/src/BaseCommand.php +++ b/src/BaseCommand.php @@ -178,7 +178,7 @@ protected function assertArtifactContentIsClean() { $this->getConfiguration()->getInclude(), )); - if ($this->gitCommandExist() && $this->isRootARepository()) { + if ($this->gitCommandExist() && $this->isGitRepository()) { $files_changed = trim($this->runCommand(sprintf("git status -s %s", implode(' ', $artifact_content)))->getOutput()); if (strlen($files_changed > 0)) { $list = implode("\n", array_map(function($item) { $parts = explode(' ', $item); return $parts[1]; }, explode("\n", $files_changed))); @@ -254,7 +254,7 @@ protected function gitCommandExist() { * * @return bool */ - protected function isRootARepository() { + protected function isGitRepository() { $command_output = $this->runCommand('git rev-parse --is-inside-work-tree || true')->getOutput(); return $command_output === 'true'; }