Skip to content

Commit

Permalink
Merge pull request #20 from juanjol/make-git-dependency-optional
Browse files Browse the repository at this point in the history
Issue #19: Added functionality to check if there is git installed and…
  • Loading branch information
omarlopesino authored Nov 27, 2024
2 parents c15a23e + 4a3528e commit 18d8264
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
31 changes: 27 additions & 4 deletions src/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->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)));
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");
}
}

}

/**
Expand Down Expand Up @@ -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 isGitRepository() {
$command_output = $this->runCommand('git rev-parse --is-inside-work-tree || true')->getOutput();
return $command_output === 'true';
}

}
1 change: 0 additions & 1 deletion src/DrupalArtifactBuilderCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('##########################');
Expand Down
4 changes: 4 additions & 0 deletions src/DrupalArtifactBuilderGit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down

0 comments on commit 18d8264

Please sign in to comment.