Skip to content

Commit

Permalink
fix: remove remote url repository requirement
Browse files Browse the repository at this point in the history
Refs: #14
  • Loading branch information
marcocesarato committed May 15, 2021
1 parent f020d9e commit dacd7c3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 30 deletions.
25 changes: 25 additions & 0 deletions src/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class Changelog
*/
protected $remote = [];

/**
* Has remote url.
*
* @var bool
*/
protected $hasRemoteUrl = false;

/**
* Changelog constructor.
*/
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand Down
66 changes: 38 additions & 28 deletions src/DefaultCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,19 @@ 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();
$appVersion = $this->getApplication()->getVersion();
$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;
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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);
Expand All @@ -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);

Expand Down
13 changes: 11 additions & 2 deletions src/Git/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit dacd7c3

Please sign in to comment.