diff --git a/CHANGELOG.md b/CHANGELOG.md index d29a7518..33add75c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#41](https://github.com/laminas/automatic-releases/pull/41) fixes an issue that occurred when attempting to commit changes to the `CHANGELOG.md`. ## 1.1.0 - 2020-08-06 diff --git a/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php b/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php index 4693ee60..550af625 100644 --- a/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php +++ b/src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php @@ -66,11 +66,14 @@ public function __invoke( Assert::stringNotEmpty($newVersion); $bumper->updateChangelog($newVersion); + $message = sprintf(self::COMMIT_TEMPLATE, $newVersion, self::CHANGELOG_FILE, $newVersion); + Assert::notEmpty($message); + ($this->commitFile)( $repositoryDirectory, $sourceBranch, self::CHANGELOG_FILE, - sprintf(self::COMMIT_TEMPLATE, $newVersion, self::CHANGELOG_FILE, $newVersion) + $message ); ($this->push)($repositoryDirectory, $sourceBranch->name()); diff --git a/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php b/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php index 2975fc3b..73829e42 100644 --- a/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php +++ b/src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php @@ -15,6 +15,7 @@ use Phly\KeepAChangelog\Version\SetDateForChangelogReleaseListener; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Output\NullOutput; +use Webmozart\Assert\Assert; use function file_exists; use function sprintf; @@ -69,11 +70,14 @@ public function __invoke( return; } + $message = sprintf(self::COMMIT_TEMPLATE, $versionString, self::CHANGELOG_FILE); + Assert::notEmpty($message); + ($this->commitFile)( $repositoryDirectory, $sourceBranch, self::CHANGELOG_FILE, - sprintf(self::COMMIT_TEMPLATE, $versionString, self::CHANGELOG_FILE) + $message ); ($this->push)($repositoryDirectory, $sourceBranch->name()); diff --git a/src/Git/CommitFile.php b/src/Git/CommitFile.php index 6ca835c5..f0f17e19 100644 --- a/src/Git/CommitFile.php +++ b/src/Git/CommitFile.php @@ -8,6 +8,11 @@ interface CommitFile { + /** + * @psalm-param non-empty-string $repositoryDirectory + * @psalm-param non-empty-string $filename + * @psalm-param non-empty-string $commitMessage + */ public function __invoke( string $repositoryDirectory, BranchName $sourceBranch, diff --git a/src/Git/CommitFileViaConsole.php b/src/Git/CommitFileViaConsole.php index f7ea6dc9..340f9efc 100644 --- a/src/Git/CommitFileViaConsole.php +++ b/src/Git/CommitFileViaConsole.php @@ -6,6 +6,10 @@ use Laminas\AutomaticReleases\Git\Value\BranchName; use Symfony\Component\Process\Process; +use Webmozart\Assert\Assert; + +use function sprintf; +use function trim; final class CommitFileViaConsole implements CommitFile { @@ -15,8 +19,7 @@ public function __invoke( string $filename, string $commitMessage ): void { - (new Process(['git', 'checkout', $sourceBranch->name()], $repositoryDirectory)) - ->mustRun(); + $this->assertWeAreOnBranch($sourceBranch, $repositoryDirectory, $filename); (new Process(['git', 'add', $filename], $repositoryDirectory)) ->mustRun(); @@ -26,4 +29,25 @@ public function __invoke( $repositoryDirectory ))->mustRun(); } + + /** + * @param non-empty-string $repositoryDirectory + * @param non-empty-string $filename + */ + private function assertWeAreOnBranch( + BranchName $expectedBranch, + string $repositoryDirectory, + string $filename + ): void { + $process = new Process(['git', 'branch', '--show-current'], $repositoryDirectory); + $process->mustRun(); + + $output = trim($process->getOutput()); + + Assert::same($output, $expectedBranch->name(), sprintf( + 'Cannot commit file %s to branch %s, as a different branch is currently checked out.', + $filename, + $expectedBranch->name() + )); + } } diff --git a/test/unit/Git/CommitFileViaConsoleTest.php b/test/unit/Git/CommitFileViaConsoleTest.php index 9666c50e..a1ee2079 100644 --- a/test/unit/Git/CommitFileViaConsoleTest.php +++ b/test/unit/Git/CommitFileViaConsoleTest.php @@ -4,6 +4,7 @@ namespace Laminas\AutomaticReleases\Test\Unit\Git; +use InvalidArgumentException; use Laminas\AutomaticReleases\Git\CommitFileViaConsole; use Laminas\AutomaticReleases\Git\Value\BranchName; use PHPUnit\Framework\TestCase; @@ -74,4 +75,24 @@ public function testAddsAndCommitsFileProvidedWithAuthorAndCommitMessageProvided self::assertStringContainsString($commitMessage, $commitDetails); self::assertStringContainsString('diff --git a/README.md b/README.md', $commitDetails); } + + public function testFailsIfNotOnCorrectBranch(): void + { + (new Process( + ['git', 'switch', '-c', '1.1.x'], + $this->checkout + )) + ->mustRun(); + + $filename = sprintf('%s/README.md', $this->checkout); + file_put_contents( + $filename, + "# README\n\nThis is a test file to test commits from laminas/automatic-releases." + ); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('different branch'); + (new CommitFileViaConsole()) + ->__invoke($this->checkout, BranchName::fromName('1.0.x'), 'README.md', 'commit message'); + } }