Skip to content

Commit

Permalink
Rewrote PushViaConsole so that a temporary branch is used locally, …
Browse files Browse the repository at this point in the history
…when pushing an alias
  • Loading branch information
Ocramius committed Jul 9, 2020
1 parent c505bbf commit 064a760
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Git/PushViaConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Laminas\AutomaticReleases\Git;

use Symfony\Component\Process\Process;
use function uniqid;

final class PushViaConsole implements Push
{
Expand All @@ -13,9 +14,22 @@ public function __invoke(
string $symbol,
?string $alias = null
): void {
$pushedRef = $alias !== null ? $symbol . ':' . $alias : $symbol;
if ($alias === null) {
(new Process(['git', 'push', 'origin', $symbol], $repositoryDirectory))
->mustRun();

(new Process(['git', 'push', 'origin', $pushedRef], $repositoryDirectory))
return;
}

$localTemporaryBranch = uniqid('temporary-branch', true);

(new Process(['git', 'branch', $localTemporaryBranch, $symbol], $repositoryDirectory))
->mustRun();

(new Process(['git', 'push', 'origin', $localTemporaryBranch . ':' . $alias], $repositoryDirectory))
->mustRun();

(new Process(['git', 'branch', '-D', $localTemporaryBranch], $repositoryDirectory))
->mustRun();
}
}
43 changes: 43 additions & 0 deletions test/unit/Git/PushViaConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ protected function setUp(): void
->mustRun();
}

protected function tearDown(): void
{
$sourceBranches = (new Process(['git', 'branch'], $this->source))
->mustRun()
->getOutput();

self::assertStringNotContainsString('temporary-branch', $sourceBranches);

parent::tearDown();
}

public function testPushesSelectedGitRef(): void
{
(new PushViaConsole())
Expand All @@ -86,4 +97,36 @@ public function testPushesSelectedGitRefAsAlias(): void
self::assertStringNotContainsString('pushed-branch', $destinationBranches);
self::assertStringNotContainsString('ignored-branch', $destinationBranches);
}

public function testPushesSelectedTag(): void
{
(new Process(['git', 'tag', 'tag-name', '-m', 'pushed tag'], $this->source))
->mustRun();

(new PushViaConsole())
->__invoke($this->source, 'tag-name');

$destinationBranches = (new Process(['git', 'tag'], $this->destination))
->mustRun()
->getOutput();

self::assertStringContainsString('tag-name', $destinationBranches);
}

public function testPushesSelectedTagAsAliasBranch(): void
{
(new Process(['git', 'tag', 'tag-name', '-m', 'pushed tag'], $this->source))
->mustRun();

(new PushViaConsole())
->__invoke($this->source, 'tag-name', 'pushed-alias');

$destinationBranches = (new Process(['git', 'branch'], $this->destination))
->mustRun()
->getOutput();

self::assertStringContainsString('pushed-alias', $destinationBranches);
self::assertStringNotContainsString('tag-name', $destinationBranches);
self::assertStringNotContainsString('ignored-branch', $destinationBranches);
}
}

0 comments on commit 064a760

Please sign in to comment.