Skip to content

Commit 1e630f7

Browse files
authored
Merge pull request #41 from weierophinney/hotfix/do-not-checkout-to-commit
Do not checkout the branch passed to `CommitFileViaConsole`
2 parents 6e9cab0 + f9fd4e7 commit 1e630f7

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25-
- Nothing.
25+
- [#41](https://github.com/laminas/automatic-releases/pull/41) fixes an issue that occurred when attempting to commit changes to the `CHANGELOG.md`.
2626

2727
## 1.1.0 - 2020-08-06
2828

src/Changelog/BumpAndCommitChangelogVersionViaKeepAChangelog.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,14 @@ public function __invoke(
6666
Assert::stringNotEmpty($newVersion);
6767
$bumper->updateChangelog($newVersion);
6868

69+
$message = sprintf(self::COMMIT_TEMPLATE, $newVersion, self::CHANGELOG_FILE, $newVersion);
70+
Assert::notEmpty($message);
71+
6972
($this->commitFile)(
7073
$repositoryDirectory,
7174
$sourceBranch,
7275
self::CHANGELOG_FILE,
73-
sprintf(self::COMMIT_TEMPLATE, $newVersion, self::CHANGELOG_FILE, $newVersion)
76+
$message
7477
);
7578

7679
($this->push)($repositoryDirectory, $sourceBranch->name());

src/Changelog/CommitReleaseChangelogViaKeepAChangelog.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Phly\KeepAChangelog\Version\SetDateForChangelogReleaseListener;
1616
use Psr\Log\LoggerInterface;
1717
use Symfony\Component\Console\Output\NullOutput;
18+
use Webmozart\Assert\Assert;
1819

1920
use function file_exists;
2021
use function sprintf;
@@ -69,11 +70,14 @@ public function __invoke(
6970
return;
7071
}
7172

73+
$message = sprintf(self::COMMIT_TEMPLATE, $versionString, self::CHANGELOG_FILE);
74+
Assert::notEmpty($message);
75+
7276
($this->commitFile)(
7377
$repositoryDirectory,
7478
$sourceBranch,
7579
self::CHANGELOG_FILE,
76-
sprintf(self::COMMIT_TEMPLATE, $versionString, self::CHANGELOG_FILE)
80+
$message
7781
);
7882

7983
($this->push)($repositoryDirectory, $sourceBranch->name());

src/Git/CommitFile.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
interface CommitFile
1010
{
11+
/**
12+
* @psalm-param non-empty-string $repositoryDirectory
13+
* @psalm-param non-empty-string $filename
14+
* @psalm-param non-empty-string $commitMessage
15+
*/
1116
public function __invoke(
1217
string $repositoryDirectory,
1318
BranchName $sourceBranch,

src/Git/CommitFileViaConsole.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
use Laminas\AutomaticReleases\Git\Value\BranchName;
88
use Symfony\Component\Process\Process;
9+
use Webmozart\Assert\Assert;
10+
11+
use function sprintf;
12+
use function trim;
913

1014
final class CommitFileViaConsole implements CommitFile
1115
{
@@ -15,8 +19,7 @@ public function __invoke(
1519
string $filename,
1620
string $commitMessage
1721
): void {
18-
(new Process(['git', 'checkout', $sourceBranch->name()], $repositoryDirectory))
19-
->mustRun();
22+
$this->assertWeAreOnBranch($sourceBranch, $repositoryDirectory, $filename);
2023

2124
(new Process(['git', 'add', $filename], $repositoryDirectory))
2225
->mustRun();
@@ -26,4 +29,25 @@ public function __invoke(
2629
$repositoryDirectory
2730
))->mustRun();
2831
}
32+
33+
/**
34+
* @param non-empty-string $repositoryDirectory
35+
* @param non-empty-string $filename
36+
*/
37+
private function assertWeAreOnBranch(
38+
BranchName $expectedBranch,
39+
string $repositoryDirectory,
40+
string $filename
41+
): void {
42+
$process = new Process(['git', 'branch', '--show-current'], $repositoryDirectory);
43+
$process->mustRun();
44+
45+
$output = trim($process->getOutput());
46+
47+
Assert::same($output, $expectedBranch->name(), sprintf(
48+
'Cannot commit file %s to branch %s, as a different branch is currently checked out.',
49+
$filename,
50+
$expectedBranch->name()
51+
));
52+
}
2953
}

test/unit/Git/CommitFileViaConsoleTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Laminas\AutomaticReleases\Test\Unit\Git;
66

7+
use InvalidArgumentException;
78
use Laminas\AutomaticReleases\Git\CommitFileViaConsole;
89
use Laminas\AutomaticReleases\Git\Value\BranchName;
910
use PHPUnit\Framework\TestCase;
@@ -74,4 +75,24 @@ public function testAddsAndCommitsFileProvidedWithAuthorAndCommitMessageProvided
7475
self::assertStringContainsString($commitMessage, $commitDetails);
7576
self::assertStringContainsString('diff --git a/README.md b/README.md', $commitDetails);
7677
}
78+
79+
public function testFailsIfNotOnCorrectBranch(): void
80+
{
81+
(new Process(
82+
['git', 'switch', '-c', '1.1.x'],
83+
$this->checkout
84+
))
85+
->mustRun();
86+
87+
$filename = sprintf('%s/README.md', $this->checkout);
88+
file_put_contents(
89+
$filename,
90+
"# README\n\nThis is a test file to test commits from laminas/automatic-releases."
91+
);
92+
93+
$this->expectException(InvalidArgumentException::class);
94+
$this->expectExceptionMessage('different branch');
95+
(new CommitFileViaConsole())
96+
->__invoke($this->checkout, BranchName::fromName('1.0.x'), 'README.md', 'commit message');
97+
}
7798
}

0 commit comments

Comments
 (0)