Skip to content

Do not checkout the branch passed to CommitFileViaConsole #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
6 changes: 5 additions & 1 deletion src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
5 changes: 5 additions & 0 deletions src/Git/CommitFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 26 additions & 2 deletions src/Git/CommitFileViaConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand All @@ -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()
));
}
}
21 changes: 21 additions & 0 deletions test/unit/Git/CommitFileViaConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
}