Skip to content

Commit

Permalink
Merge pull request #159 from fortrabbit/feature/replace-cpliakas-git-…
Browse files Browse the repository at this point in the history
…wrapper

Replace cpliakas/git-wrapper with gitonomy/gitlib
  • Loading branch information
Oliver Stark authored Apr 17, 2023
2 parents 6c893dd + ee59868 commit d50d2cc
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 88 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
"require": {
"php": "^8.0.2",
"albertofem/rsync-lib": "^1.0.0",
"symplify/git-wrapper": "^10.0",
"craftcms/cms": "^4.0.0",
"craftcms/plugin-installer": "^1.5.6",
"fortrabbit/craft-auto-migrate": "^2.5.0",
"gitonomy/gitlib": "^1.3",
"ostark/yii2-artisan-bridge": "^1.3.1",
"symfony/process": "^5.0 | ^6.0",
"vlucas/phpdotenv": "^3.4.0 | ^5.4",
"symfony/yaml": "^4.2 | ^5.0",
"fortrabbit/craft-auto-migrate":"^2.5.0"
"vlucas/phpdotenv": "^3.4.0 | ^5.4"
},
"require-dev": {
"craftcms/phpstan": "dev-main",
Expand Down
7 changes: 3 additions & 4 deletions src/Actions/CodeDownAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace fortrabbit\Copy\Actions;

use Symplify\GitWrapper\Exception\GitException;
use fortrabbit\Copy\Exceptions\GitException;
use yii\console\ExitCode;

class CodeDownAction extends StageAwareBaseAction
Expand All @@ -22,15 +22,15 @@ public function run(?string $stage = null): int
);

$git = $this->plugin->git;
$git->getWorkingCopy()->init();
$git->getClient()->init();

$localBranches = $git->getLocalBranches();
$branch = $git->getLocalHead();

if (count($localBranches) > 1) {
$question = 'Select a local branch (checkout):';
$branch = str_replace('* ', '', $this->choice($question, $localBranches, $branch));
$git->run('checkout', [$branch]);
$git->getClient()->checkout($branch);
}

// Run 'before' commands and stop on error
Expand All @@ -44,7 +44,6 @@ public function run(?string $stage = null): int

try {
$this->section("git pull ({$upstream}/{$branch})");
$git->getWorkingCopy()->getWrapper()->streamOutput();
$git->pull($upstream, $branch);
} catch (GitException $gitException) {
$lines = count(explode(PHP_EOL, $gitException->getMessage()));
Expand Down
17 changes: 8 additions & 9 deletions src/Actions/CodeUpAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use craft\helpers\App;
use craft\helpers\FileHelper;
use Exception;
use fortrabbit\Copy\Exceptions\GitException;
use fortrabbit\Copy\Services\Git;
use fortrabbit\Copy\Services\LocalFilesystem;
use Symplify\GitWrapper\Exception\GitException;
use ostark\Yii2ArtisanBridge\base\Commands;
use Throwable;
use yii\console\ExitCode;
Expand Down Expand Up @@ -53,7 +53,7 @@ public function run(?string $stage = null): int
}

$git = $this->plugin->git;
$git->getWorkingCopy()->init();
$git->getClient()->init();

// Project .gitignore
$git->assureDotGitignore();
Expand All @@ -64,7 +64,7 @@ public function run(?string $stage = null): int
if (count($localBranches) > 1) {
$question = 'Select a local branch (checkout):';
$branch = str_replace('* ', '', $this->choice($question, $localBranches, $branch));
$git->run('checkout', [$branch]);
$git->getClient()->checkout($branch);
}

// Ask for remote
Expand All @@ -78,7 +78,7 @@ public function run(?string $stage = null): int
$this->assureVolumesAreIgnored();

try {
if ($log = $git->getWorkingCopy()->log(
if ($log = $git->getClient()->log(
'--format=(%h) %cr: %s ',
"{$upstream}/master..HEAD"
)) {
Expand All @@ -87,11 +87,11 @@ public function run(?string $stage = null): int
} catch (Throwable) {
}

if (! $git->getWorkingCopy()->hasChanges() && ! $this->confirm('About to push latest commits, proceed?', true)) {
if (! $git->getClient()->hasChanges() && ! $this->confirm('About to push latest commits, proceed?', true)) {
return ExitCode::OK;
}

if ($status = $git->getWorkingCopy()->getStatus()) {
if ($status = $git->getClient()->getStatus()) {
// Changed files
$this->noteBlock('Uncommitted changes:' . PHP_EOL . $status);

Expand All @@ -112,8 +112,8 @@ public function run(?string $stage = null): int
}

// Add and commit
$git->getWorkingCopy()->add('.');
$git->getWorkingCopy()->commit($msg);
$git->getClient()->add('.');
$git->getClient()->commit($msg);

// Ask for the branch name again if this is the first commit in the repo
if ($branch === NULL) {
Expand All @@ -130,7 +130,6 @@ public function run(?string $stage = null): int

try {
$this->section("git push ({$msg})");
$git->getWorkingCopy()->getWrapper()->streamOutput();
$git->push($upstream, "{$branch}:master");
} catch (GitException $gitException) {
$lines = count(explode(PHP_EOL, $gitException->getMessage()));
Expand Down
11 changes: 11 additions & 0 deletions src/Exceptions/GitException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace fortrabbit\Copy\Exceptions;

use yii\base\Exception;

class GitException extends Exception
{
}
92 changes: 20 additions & 72 deletions src/Services/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@

use Exception;
use fortrabbit\Copy\Plugin;
use Symplify\GitWrapper\Exception\GitException;
use Symplify\GitWrapper\GitWorkingCopy;
use Symplify\GitWrapper\GitWrapper;
use fortrabbit\Copy\Services\Git\Client;
use fortrabbit\Copy\Services\Git\GitonomyClient;
use InvalidArgumentException;
use LogicException;

/**
* Git Service
*/
final class Git
{
private function __construct(private GitWorkingCopy $gitWorkingCopy)
{
private function __construct(private Client $gitClient)
{
}

/**
Expand All @@ -28,10 +26,10 @@ private function __construct(private GitWorkingCopy $gitWorkingCopy)
*/
public static function fromDirectory(string $directory): \fortrabbit\Copy\Services\Git
{
$wrapper = new GitWrapper('git');
$wrapper->setTimeout(300);
$client = new GitonomyClient();
$client->setDirectory($directory);

return new self($wrapper->workingCopy($directory));
return new self($client);
}

/**
Expand All @@ -47,44 +45,33 @@ public static function fromClone(
array $options = [
]
): \fortrabbit\Copy\Services\Git {
$wrapper = new GitWrapper('git');
$wrapper->setTimeout(300);
$client = new GitonomyClient();
$client->clone($repository, $directory, $options);

return new self($wrapper->cloneRepository($repository, $directory, $options));
return new self($client);
}

public function push(string $upstream, string $branch = 'master'): string
{
return $this->gitWorkingCopy->push($upstream, $branch);
return $this->gitClient->push($upstream, $branch);
}

public function pull(string $upstream, string $branch = 'master'): string
{
return $this->gitWorkingCopy->pull($upstream, $branch);
return $this->gitClient->pull($upstream, $branch);
}

public function getLocalHead(): ?string
{
foreach ($this->getLocalBranches() as $key => $name) {
if (stristr($name, '*')) {
return $key;
}
}

return null;
return $this->gitClient->getLocalHead();
}

/**
* @return array<string, string>
*/
public function getLocalBranches(): array
{
$localBranches = [];
foreach (explode(PHP_EOL, trim($this->gitWorkingCopy->run('branch'))) as $branch) {
$localBranches[trim(ltrim($branch, '*'))] = $branch;
}

return $localBranches;
return $this->gitClient->getLocalBranches();
}

/**
Expand All @@ -93,54 +80,15 @@ public function getLocalBranches(): array
*/
public function getRemotes(?string $for = 'push'): array
{
if (! in_array($for, ['push', 'pull'], true)) {
throw new LogicException(
sprintf(
'Argument 1 passed to %s must be "pull" or "push", %s given.',
'fortrabbit\Copy\Services\Git::getRemotes()',
$for
)
);
}

try {
$remotes = $this->gitWorkingCopy->getRemotes();
} catch (GitException) {
return [];
}

foreach ($remotes as $name => $upstreams) {
$remotes[$name] = $upstreams[$for];
}

return $remotes;
return $this->gitClient->getRemotes($for);
}

/**
* Returns remote tracking upstream/branch for HEAD.
*/
public function getTracking(bool $includeBranch = false): ?string
{
try {
$result = $this->run('rev-parse', ['@{u}', [
'abbrev-ref' => true,
'symbolic-full-name' => true,
]]);
} catch (GitException) {
return null;
}

if ($includeBranch) {
return $result;
}

// Split upstream/branch and return upstream only
return explode('/', $result)[0];
}

public function run(string $command, array $argsAndOptions = []): string
{
return $this->gitWorkingCopy->run($command, $argsAndOptions);
return $this->gitClient->getTracking($includeBranch);
}

/**
Expand All @@ -158,14 +106,14 @@ public function addRemote(string $sshRemote): string
}

$app = explode('@', $sshRemote)[0];
$this->getWorkingCopy()->addRemote($app, "{$sshRemote}:{$app}.git");
$this->gitClient->addRemote($app, "{$sshRemote}:{$app}.git");

return $app;
}

public function getWorkingCopy(): GitWorkingCopy
public function getClient(): Client
{
return $this->gitWorkingCopy;
return $this->gitClient;
}

/**
Expand All @@ -175,7 +123,7 @@ public function getWorkingCopy(): GitWorkingCopy
*/
public function assureDotGitignore(): bool
{
$path = $this->getWorkingCopy()->getDirectory();
$path = $this->gitClient->getDirectory();
$gitignoreFile = "{$path}/.gitignore";
$gitignoreExampleFile = Plugin::PLUGIN_ROOT_PATH . '/.gitignore.example';

Expand Down
44 changes: 44 additions & 0 deletions src/Services/Git/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace fortrabbit\Copy\Services\Git;

interface Client
{
public function clone(
string $repository,
?string $directory = null,
array $options = []
);

public function push(string $upstream, string $branch = 'master'): string;

public function pull(string $upstream, string $branch = 'master'): string;

public function getLocalHead(): ?string;

public function getLocalBranches(): array;

public function getRemotes(?string $for = 'push'): array;

public function getTracking(bool $includeBranch = false): ?string;

public function checkout(string $branch);

public function addRemote(string $name, ?string $url);

public function setDirectory(string $directory);

public function getDirectory(): string;

public function init();

public function log(...$argsOrOptions): string;

public function hasChanges(): bool;

public function getStatus(): string;

public function add(string $filepattern, array $options = []): string;

public function commit(...$argsOrOptions): string;
}
Loading

0 comments on commit d50d2cc

Please sign in to comment.