Skip to content

Commit

Permalink
Merge pull request #41 from weierophinney/hotfix/do-not-checkout-to-c…
Browse files Browse the repository at this point in the history
…ommit

Do not checkout the branch passed to `CommitFileViaConsole`
  • Loading branch information
Ocramius authored Aug 12, 2020
2 parents 6e9cab0 + f9fd4e7 commit 1e630f7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
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');
}
}

0 comments on commit 1e630f7

Please sign in to comment.