From dacd7c3a82c2924047fcf4128e2d604f58c94978 Mon Sep 17 00:00:00 2001 From: Marco Cesarato Date: Sat, 15 May 2021 10:00:18 +0200 Subject: [PATCH] fix: remove remote url repository requirement Refs: #14 --- src/Changelog.php | 25 ++++++++++++++++ src/DefaultCommand.php | 66 ++++++++++++++++++++++++------------------ src/Git/Repository.php | 13 +++++++-- 3 files changed, 74 insertions(+), 30 deletions(-) diff --git a/src/Changelog.php b/src/Changelog.php index 5deb017..2cccc76 100644 --- a/src/Changelog.php +++ b/src/Changelog.php @@ -29,6 +29,13 @@ class Changelog */ protected $remote = []; + /** + * Has remote url. + * + * @var bool + */ + protected $hasRemoteUrl = false; + /** * Changelog constructor. */ @@ -81,6 +88,8 @@ public function generate(string $root, InputInterface $input, SymfonyStyle $outp PackageJson::class, ]; + $this->hasRemoteUrl = Repository::hasRemoteUrl(); + // Hook pre run $this->config->preRun(); @@ -548,6 +557,10 @@ public function getCompiledString(string $format, array $values): string */ public function getCommitUrl(string $hash): string { + if (!$this->hasRemoteUrl) { + return '#'; + } + $protocol = $this->config->getUrlProtocol(); $format = $this->config->getCommitUrlFormat(); $url = $this->getCompiledString($format, ['hash' => $hash]); @@ -560,6 +573,10 @@ public function getCommitUrl(string $hash): string */ public function getCompareUrl(string $previousTag, string $currentTag): string { + if (!$this->hasRemoteUrl) { + return '#'; + } + $protocol = $this->config->getUrlProtocol(); $format = $this->config->getCompareUrlFormat(); $url = $this->getCompiledString($format, ['previousTag' => $previousTag, 'currentTag' => $currentTag]); @@ -572,6 +589,10 @@ public function getCompareUrl(string $previousTag, string $currentTag): string */ public function getIssueUrl(string $id): string { + if (!$this->hasRemoteUrl) { + return '#'; + } + $protocol = $this->config->getUrlProtocol(); $format = $this->config->getIssueUrlFormat(); $url = $this->getCompiledString($format, ['id' => $id]); @@ -584,6 +605,10 @@ public function getIssueUrl(string $id): string */ public function getUserUrl(string $user): string { + if (!$this->hasRemoteUrl) { + return '#'; + } + $protocol = $this->config->getUrlProtocol(); $format = $this->config->getUserUrlFormat(); $url = $this->getCompiledString($format, ['user' => $user]); diff --git a/src/DefaultCommand.php b/src/DefaultCommand.php index d12452b..b69ceb4 100644 --- a/src/DefaultCommand.php +++ b/src/DefaultCommand.php @@ -93,7 +93,9 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - // Initialize + /** + * Initialize. + */ $this->output = new SymfonyStyle($input, $output); if ($this->getApplication()) { $appName = $this->getApplication()->getName(); @@ -101,7 +103,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->output->title($appName . ' ' . $appVersion); } - // Retrieve configuration settings + /** + * Retrieve configuration settings. + */ $config = $input->getOption('config'); if (!empty($config) && is_file($config)) { $this->settings = require $config; @@ -111,10 +115,18 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->settings = []; } $this->config = new Configuration($this->settings); + // Get root path + $root = $input->getArgument('path'); + if (empty($root) || !is_dir($root)) { + $root = $this->config->getRoot(); + } + // Set working directory + chdir($root); - // Check environment + /** + * Check environment. + */ $this->output->writeln('Checking environment requirements'); - // Check shell exec function if (!ShellCommand::isEnabled()) { $this->output->error( @@ -124,8 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; //Command::FAILURE; } - $this->validRequirement('Shell exec enabled'); - + $this->validRequirement('Commands executions enabled'); // Check git command if (!ShellCommand::exists('git')) { $this->output->error( @@ -135,8 +146,7 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; //Command::FAILURE; } - $this->validRequirement('Git detected'); - + $this->validRequirement('Git detected correctly'); // Check git version $gitVersion = ShellCommand::exec('git --version'); $gitSemver = new SemanticVersion($gitVersion); @@ -149,38 +159,38 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; //Command::FAILURE; } - $this->validRequirement('Git version ' . $gitVersionCode); - + $this->validRequirement('Git version detected: ' . $gitVersionCode); // Check working directory - $root = $input->getArgument('path'); - if (empty($root) || !is_dir($root)) { - $root = $this->config->getRoot(); - } - // Set working directory - chdir($root); if (!Repository::isInsideWorkTree()) { $this->output->error('The directory "' . $root . '" isn\'t a valid git repository or isn\'t been detected correctly.'); return 1; //Command::FAILURE; } - $this->validRequirement('Valid git worktree detected'); - + $this->validRequirement('Valid git worktree directory: ' . $root); // Check git repository - if (empty(Repository::parseRemoteUrl())) { + if (Repository::hasRemoteUrl()) { $url = Repository::getRemoteUrl(); - $this->output->error( - 'The remote url of your git repository ("' . $url . '") not been parsed correctly. ' . - 'Please open and issue including your repository url format to make it compatible with this tool: ' . - "\nhttps://github.com/marcocesarato/php-conventional-changelog/issues" + if (empty(Repository::parseRemoteUrl())) { + $this->output->warning( + 'The remote url of your git repository ("' . $url . '") not been parsed correctly. ' . + 'Please open and issue including your repository url format to make it compatible with this tool here ' . + 'https://github.com/marcocesarato/php-conventional-changelog/issues' + ); + } else { + $this->validRequirement('Valid git remote repository url: ' . $url); + } + } else { + $this->output->warning( + 'Remote repository url not detected. ' . + 'It looks like your project is running only locally and not on a remote repository. ' . + 'Care, all urls on the changelog will be empty.' ); - - return 1; //Command::FAILURE; } - $this->validRequirement('Valid git remote repository url'); - $this->output->newLine(); - // Initialize changelog + /** + * Generate changelog. + */ $this->output->writeln('Generating changelog'); $this->changelog = new Changelog($this->config); diff --git a/src/Git/Repository.php b/src/Git/Repository.php index cf49ad0..5ec58ed 100644 --- a/src/Git/Repository.php +++ b/src/Git/Repository.php @@ -31,9 +31,8 @@ protected static function run(string $string): string public static function isInsideWorkTree(): bool { $result = self::run('git rev-parse --is-inside-work-tree'); - $remote = self::getRemoteUrl(); - return $result === 'true' && !empty($remote); + return $result === 'true'; } /** @@ -91,6 +90,16 @@ public static function getRemoteUrl(): string return preg_replace('/^(https?:\/\/)([0-9a-z.\-_:%]+@)/i', '$1', $url); } + /** + * Has remote url. + */ + public static function hasRemoteUrl(): string + { + $url = self::getRemoteUrl(); + + return !empty($url); + } + /** * Get commits. */