From 9135a7ba1b97074eea28e60b5b675577b50cb00f Mon Sep 17 00:00:00 2001 From: Rafik Abdulwahab Date: Tue, 31 Jan 2023 12:05:29 +0100 Subject: [PATCH 1/7] hide GitWrapper behind proxy object --- src/Actions/CodeDownAction.php | 6 +- src/Actions/CodeUpAction.php | 16 +-- src/Services/Git.php | 92 ++++------------ src/Services/Git/Client.php | 46 ++++++++ src/Services/Git/GitWrapperClient.php | 152 ++++++++++++++++++++++++++ 5 files changed, 229 insertions(+), 83 deletions(-) create mode 100644 src/Services/Git/Client.php create mode 100644 src/Services/Git/GitWrapperClient.php diff --git a/src/Actions/CodeDownAction.php b/src/Actions/CodeDownAction.php index 17d0fdc..f09951d 100644 --- a/src/Actions/CodeDownAction.php +++ b/src/Actions/CodeDownAction.php @@ -22,7 +22,7 @@ public function run(?string $stage = null): int ); $git = $this->plugin->git; - $git->getWorkingCopy()->init(); + $git->getClient()->init(); $localBranches = $git->getLocalBranches(); $branch = $git->getLocalHead(); @@ -30,7 +30,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); } // Run 'before' commands and stop on error @@ -44,7 +44,7 @@ public function run(?string $stage = null): int try { $this->section("git pull ({$upstream}/{$branch})"); - $git->getWorkingCopy()->getWrapper()->streamOutput(); + $git->getClient()->streamOutput(); $git->pull($upstream, $branch); } catch (GitException $gitException) { $lines = count(explode(PHP_EOL, $gitException->getMessage())); diff --git a/src/Actions/CodeUpAction.php b/src/Actions/CodeUpAction.php index 4a4fadb..bd78368 100644 --- a/src/Actions/CodeUpAction.php +++ b/src/Actions/CodeUpAction.php @@ -51,7 +51,7 @@ public function run(?string $stage = null): int } $git = $this->plugin->git; - $git->getWorkingCopy()->init(); + $git->getClient()->init(); // Project .gitignore $git->assureDotGitignore(); @@ -62,7 +62,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 @@ -76,7 +76,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" )) { @@ -85,11 +85,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); $defaultMessage = $this->interactive ? null : 'init Craft'; @@ -104,8 +104,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) { @@ -122,7 +122,7 @@ public function run(?string $stage = null): int try { $this->section("git push ({$msg})"); - $git->getWorkingCopy()->getWrapper()->streamOutput(); + $git->getClient()->streamOutput(); $git->push($upstream, "{$branch}:master"); } catch (GitException $gitException) { $lines = count(explode(PHP_EOL, $gitException->getMessage())); diff --git a/src/Services/Git.php b/src/Services/Git.php index 50a165e..e880007 100644 --- a/src/Services/Git.php +++ b/src/Services/Git.php @@ -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\GitWrapperClient; use InvalidArgumentException; -use LogicException; /** * Git Service */ final class Git { - private function __construct(private GitWorkingCopy $gitWorkingCopy) - { + private function __construct(private Client $gitClient) + { } /** @@ -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 GitWrapperClient(); + $client->setDirectory($directory); - return new self($wrapper->workingCopy($directory)); + return new self($client); } /** @@ -47,31 +45,25 @@ public static function fromClone( array $options = [ ] ): \fortrabbit\Copy\Services\Git { - $wrapper = new GitWrapper('git'); - $wrapper->setTimeout(300); + $client = new GitWrapperClient(); + $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(); } /** @@ -79,12 +71,7 @@ public function getLocalHead(): ?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(); } /** @@ -93,27 +80,7 @@ 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); } /** @@ -121,26 +88,7 @@ public function getRemotes(?string $for = 'push'): array */ 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); } /** @@ -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; } /** @@ -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'; diff --git a/src/Services/Git/Client.php b/src/Services/Git/Client.php new file mode 100644 index 0000000..2013f16 --- /dev/null +++ b/src/Services/Git/Client.php @@ -0,0 +1,46 @@ +wrapper = new GitWrapper('git'); + $this->wrapper->setTimeout(300); + } + + public function clone(string $repository, + ?string $directory = null, + array $options = [ + ]) { + $this->setWorkingCopy($this->wrapper->cloneRepository($repository, $directory, $options)); + } + + public function setDirectory(string $directory) { + $this->setWorkingCopy($this->wrapper->workingCopy($directory)); + } + + public function push(string $upstream, string $branch = 'master'): string { + return $this->gitWorkingCopy->push($upstream, $branch); + } + + public function pull(string $upstream, string $branch = 'master'): string { + return $this->gitWorkingCopy->pull($upstream, $branch); + } + + public function getLocalHead(): ?string { + foreach ($this->getLocalBranches() as $key => $name) { + if (stristr($name, '*')) { + return $key; + } + } + + return null; + } + + public function getLocalBranches(): array { + $localBranches = []; + foreach (explode(PHP_EOL, trim($this->gitWorkingCopy->run('branch'))) as $branch) { + $localBranches[trim(ltrim($branch, '*'))] = $branch; + } + + return $localBranches; + } + + 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; + } + + public function getTracking(bool $includeBranch = false): ?string { + try { + $result = $this->gitWorkingCopy->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 checkout(string $branch) { + $this->getWorkingCopy()->run('checkout', [$branch]); + } + + public function addRemote(string $name, ?string $url) { + $this->gitWorkingCopy->addRemote($name, $url); + + } + + private function setWorkingCopy(GitWorkingCopy $gitWorkingCopy) { + $this->gitWorkingCopy = $gitWorkingCopy; + } + + private function getWorkingCopy(): GitWorkingCopy { + return $this->gitWorkingCopy; + } + + public function getDirectory(): string { + return $this->gitWorkingCopy->getDirectory(); + } + + public function init() { + $this->gitWorkingCopy->init(); + } + + public function log(...$argsOrOptions): string { + return $this->getWorkingCopy()->log($argsOrOptions); + } + + public function hasChanges(): bool { + return $this->getWorkingCopy()->hasChanges(); + } + + public function getStatus(): string { + return $this->getWorkingCopy()->getStatus(); + } + + public function add(string $filepattern, array $options = []): string { + return $this->getWorkingCopy()->add($filepattern, $options); + } + + public function commit(...$argsOrOptions): string { + return $this->getWorkingCopy()->commit($argsOrOptions); + } + + public function streamOutput(bool $streamOutput = true): void { + $this->wrapper->streamOutput(); + } + +} \ No newline at end of file From 1ef405da61ee88d3cb68c547f53e2c8948149249 Mon Sep 17 00:00:00 2001 From: Rafik Abdulwahab Date: Tue, 7 Feb 2023 13:53:32 +0100 Subject: [PATCH 2/7] replace GitWrapperClient with GitonomyClient --- composer.json | 6 +- src/Services/Git.php | 6 +- src/Services/Git/Client.php | 28 ++--- src/Services/Git/GitCommand.php | 62 ++++++++++ src/Services/Git/GitWrapperClient.php | 2 +- src/Services/Git/GitonomyClient.php | 157 ++++++++++++++++++++++++++ 6 files changed, 240 insertions(+), 21 deletions(-) create mode 100644 src/Services/Git/GitCommand.php create mode 100644 src/Services/Git/GitonomyClient.php diff --git a/composer.json b/composer.json index 22e64c7..20e1093 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Services/Git.php b/src/Services/Git.php index e880007..ec05b35 100644 --- a/src/Services/Git.php +++ b/src/Services/Git.php @@ -7,7 +7,7 @@ use Exception; use fortrabbit\Copy\Plugin; use fortrabbit\Copy\Services\Git\Client; -use fortrabbit\Copy\Services\Git\GitWrapperClient; +use fortrabbit\Copy\Services\Git\GitonomyClient; use InvalidArgumentException; /** @@ -26,7 +26,7 @@ private function __construct(private Client $gitClient) */ public static function fromDirectory(string $directory): \fortrabbit\Copy\Services\Git { - $client = new GitWrapperClient(); + $client = new GitonomyClient(); $client->setDirectory($directory); return new self($client); @@ -45,7 +45,7 @@ public static function fromClone( array $options = [ ] ): \fortrabbit\Copy\Services\Git { - $client = new GitWrapperClient(); + $client = new GitonomyClient(); $client->clone($repository, $directory, $options); return new self($client); diff --git a/src/Services/Git/Client.php b/src/Services/Git/Client.php index 2013f16..7627bfd 100644 --- a/src/Services/Git/Client.php +++ b/src/Services/Git/Client.php @@ -2,12 +2,13 @@ namespace fortrabbit\Copy\Services\Git; -interface Client { - - public function clone(string $repository, - ?string $directory = null, - array $options = [ - ]); +interface Client +{ + public function clone( + string $repository, + ?string $directory = null, + array $options = [] + ); public function push(string $upstream, string $branch = 'master'): string; @@ -31,16 +32,15 @@ public function getDirectory(): string; public function init(); - public function log(...$argsOrOptions): string; - - public function hasChanges(): bool; + public function log(...$argsOrOptions): string; - public function getStatus(): string; + public function hasChanges(): bool; - public function add(string $filepattern, array $options = []): string; + public function getStatus(): string; - public function commit(...$argsOrOptions): string; + public function add(string $filepattern, array $options = []): string; - public function streamOutput(bool $streamOutput = true): void; + public function commit(...$argsOrOptions): string; -} \ No newline at end of file + public function streamOutput(bool $streamOutput = true): void; +} diff --git a/src/Services/Git/GitCommand.php b/src/Services/Git/GitCommand.php new file mode 100644 index 0000000..0714ee0 --- /dev/null +++ b/src/Services/Git/GitCommand.php @@ -0,0 +1,62 @@ +directory = $directory; + $this->repository = Admin::cloneTo($directory, $repository, true, $options); + } + + public function push(string $upstream, string $branch = 'master'): string + { + return $this->run(GitCommand::PUSH, [$upstream, $branch]); + } + + public function pull(string $upstream, string $branch = 'master'): string + { + return $this->run(GitCommand::PULL, [$upstream, $branch]); + } + + public function getLocalHead(): ?string + { + if ($this->repository->isHeadAttached()) { + return $this->repository->getHead()->getFullName(); + } + } + + public function getLocalBranches(): array + { + $localBranches = array_filter( + $this->repository->getReferences()->getBranches(), + fn ($branch) => $branch->isLocal() + ); + + return array_map(function ($branch) { + return $branch->getName(); + }, $localBranches); + } + + 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\Client::getRemotes()', + $for + ) + ); + } + + $remotes = explode(PHP_EOL, rtrim($this->run(GitCommand::REMOTE, ['-v']))); + + return $remotes; + } + + public function getTracking(bool $includeBranch = false): ?string + { + try { + $result = $this->repository->run(GitCommand::REV_PARSE, [ + '--symbolic-full-name', + '@{u}', + '--abbrev-ref', + ]); + } catch (ProcessException) { + return null; + } + + if ($includeBranch) { + return $result; + } + + // Split upstream/branch and return upstream only + return explode('/', $result)[0]; + } + + public function checkout(string $branch) + { + $this->repository->run(GitCommand::CHECKOUT, [$branch]); + } + + public function addRemote(string $name, ?string $url) + { + } + + public function setDirectory(string $directory) + { + $this->directory = $directory; + $this->repository = new Repository($directory); + } + + public function getDirectory(): string + { + return $this->directory; + } + + public function init() + { + Admin::init($this->directory, $this->repository->isBare()); + } + + public function log(...$argsOrOptions): string + { + return $this->run(GitCommand::LOG, $argsOrOptions); + } + + public function hasChanges(): bool + { + return $this->getStatus() !== ''; + } + + public function getStatus(): string + { + return $this->run(GitCommand::STATUS, ['-s']); + } + + public function add(string $filepattern, array $options = []): string + { + return $this->run(GitCommand::ADD, [$filepattern]); + } + + public function commit(...$argsOrOptions): string + { + if (isset($argsOrOptions[0]) && is_string($argsOrOptions[0]) && !isset($argsOrOptions[1])) { + $argsOrOptions = [ + '-a', + '-m ' . $argsOrOptions[0], + ]; + } + + return $this->run(GitCommand::COMMIT, $argsOrOptions); + } + + public function streamOutput(bool $streamOutput = true): void + { + } + + private function run(string $command, array $args): string + { + return $this->repository->run($command, $args); + } +} From 04ac06943d7475315ae7fc703a757a4389cee9ee Mon Sep 17 00:00:00 2001 From: Rafik Abdulwahab Date: Thu, 9 Feb 2023 12:08:55 +0100 Subject: [PATCH 3/7] phpstan cleanup --- src/Actions/CodeDownAction.php | 2 +- src/Actions/CodeUpAction.php | 2 +- src/Exceptions/GitException.php | 11 ++ src/Services/Git/GitWrapperClient.php | 152 -------------------------- src/Services/Git/GitonomyClient.php | 15 ++- 5 files changed, 25 insertions(+), 157 deletions(-) create mode 100644 src/Exceptions/GitException.php delete mode 100644 src/Services/Git/GitWrapperClient.php diff --git a/src/Actions/CodeDownAction.php b/src/Actions/CodeDownAction.php index f09951d..9e96e34 100644 --- a/src/Actions/CodeDownAction.php +++ b/src/Actions/CodeDownAction.php @@ -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 diff --git a/src/Actions/CodeUpAction.php b/src/Actions/CodeUpAction.php index bd78368..53ea6e2 100644 --- a/src/Actions/CodeUpAction.php +++ b/src/Actions/CodeUpAction.php @@ -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; diff --git a/src/Exceptions/GitException.php b/src/Exceptions/GitException.php new file mode 100644 index 0000000..fb3efa1 --- /dev/null +++ b/src/Exceptions/GitException.php @@ -0,0 +1,11 @@ +wrapper = new GitWrapper('git'); - $this->wrapper->setTimeout(300); - } - - public function clone(string $repository, - ?string $directory = null, - array $options = [ - ]) { - $this->setWorkingCopy($this->wrapper->cloneRepository($repository, $directory, $options)); - } - - public function setDirectory(string $directory) { - $this->setWorkingCopy($this->wrapper->workingCopy($directory)); - } - - public function push(string $upstream, string $branch = 'master'): string { - return $this->gitWorkingCopy->push($upstream, $branch); - } - - public function pull(string $upstream, string $branch = 'master'): string { - return $this->gitWorkingCopy->pull($upstream, $branch); - } - - public function getLocalHead(): ?string { - foreach ($this->getLocalBranches() as $key => $name) { - if (stristr($name, '*')) { - return $key; - } - } - - return null; - } - - public function getLocalBranches(): array { - $localBranches = []; - foreach (explode(PHP_EOL, trim($this->gitWorkingCopy->run('branch'))) as $branch) { - $localBranches[trim(ltrim($branch, '*'))] = $branch; - } - - return $localBranches; - } - - 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\Client::getRemotes()', - $for - ) - ); - } - - try { - $remotes = $this->gitWorkingCopy->getRemotes(); - } catch (GitException) { - return []; - } - - foreach ($remotes as $name => $upstreams) { - $remotes[$name] = $upstreams[$for]; - } - - return $remotes; - } - - public function getTracking(bool $includeBranch = false): ?string { - try { - $result = $this->gitWorkingCopy->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 checkout(string $branch) { - $this->getWorkingCopy()->run('checkout', [$branch]); - } - - public function addRemote(string $name, ?string $url) { - $this->gitWorkingCopy->addRemote($name, $url); - - } - - private function setWorkingCopy(GitWorkingCopy $gitWorkingCopy) { - $this->gitWorkingCopy = $gitWorkingCopy; - } - - private function getWorkingCopy(): GitWorkingCopy { - return $this->gitWorkingCopy; - } - - public function getDirectory(): string { - return $this->gitWorkingCopy->getDirectory(); - } - - public function init() { - $this->gitWorkingCopy->init(); - } - - public function log(...$argsOrOptions): string { - return $this->getWorkingCopy()->log($argsOrOptions); - } - - public function hasChanges(): bool { - return $this->getWorkingCopy()->hasChanges(); - } - - public function getStatus(): string { - return $this->getWorkingCopy()->getStatus(); - } - - public function add(string $filepattern, array $options = []): string { - return $this->getWorkingCopy()->add($filepattern, $options); - } - - public function commit(...$argsOrOptions): string { - return $this->getWorkingCopy()->commit($argsOrOptions); - } - - public function streamOutput(bool $streamOutput = true): void { - $this->wrapper->streamOutput(); - } - -} \ No newline at end of file diff --git a/src/Services/Git/GitonomyClient.php b/src/Services/Git/GitonomyClient.php index b0d3b49..2a32c65 100644 --- a/src/Services/Git/GitonomyClient.php +++ b/src/Services/Git/GitonomyClient.php @@ -2,10 +2,13 @@ namespace fortrabbit\Copy\Services\Git; +use fortrabbit\Copy\Exceptions\GitException; use Gitonomy\Git\Admin; use Gitonomy\Git\Exception\ProcessException; +use Gitonomy\Git\Reference\Branch; use Gitonomy\Git\Repository; use LogicException; +use RuntimeException; class GitonomyClient implements Client { @@ -35,9 +38,11 @@ public function pull(string $upstream, string $branch = 'master'): string public function getLocalHead(): ?string { - if ($this->repository->isHeadAttached()) { - return $this->repository->getHead()->getFullName(); + $head = $this->repository->getHead(); + if ($head instanceof Branch) { + return $head->getName(); } + return $head->getFullName(); } public function getLocalBranches(): array @@ -152,6 +157,10 @@ public function streamOutput(bool $streamOutput = true): void private function run(string $command, array $args): string { - return $this->repository->run($command, $args); + try { + return $this->repository->run($command, $args); + } catch (RuntimeException $exception) { + throw new GitException($exception->getMessage(), $exception->getCode(), $exception); + } } } From 8f2ea48da567242b4850f7206615ba54b7c59d79 Mon Sep 17 00:00:00 2001 From: Rafik Abdulwahab Date: Mon, 13 Feb 2023 17:07:02 +0100 Subject: [PATCH 4/7] reference correct git client in exception log --- src/Services/Git/GitonomyClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/Git/GitonomyClient.php b/src/Services/Git/GitonomyClient.php index 2a32c65..6aa2cc5 100644 --- a/src/Services/Git/GitonomyClient.php +++ b/src/Services/Git/GitonomyClient.php @@ -63,7 +63,7 @@ public function getRemotes(?string $for = 'push'): array throw new LogicException( sprintf( 'Argument 1 passed to %s must be "pull" or "push", %s given.', - 'fortrabbit\Copy\Services\Git\Client::getRemotes()', + 'fortrabbit\Copy\Services\Git\GitonomyClient::getRemotes()', $for ) ); From 937242ebe71f48e492beaeba4eeacf6ed4ae94ca Mon Sep 17 00:00:00 2001 From: Rafik Abdulwahab Date: Mon, 3 Apr 2023 12:34:57 +0100 Subject: [PATCH 5/7] remove streamOutput references --- src/Actions/CodeDownAction.php | 1 - src/Actions/CodeUpAction.php | 1 - src/Services/Git/Client.php | 2 -- src/Services/Git/GitonomyClient.php | 4 ---- 4 files changed, 8 deletions(-) diff --git a/src/Actions/CodeDownAction.php b/src/Actions/CodeDownAction.php index 9e96e34..ece68f5 100644 --- a/src/Actions/CodeDownAction.php +++ b/src/Actions/CodeDownAction.php @@ -44,7 +44,6 @@ public function run(?string $stage = null): int try { $this->section("git pull ({$upstream}/{$branch})"); - $git->getClient()->streamOutput(); $git->pull($upstream, $branch); } catch (GitException $gitException) { $lines = count(explode(PHP_EOL, $gitException->getMessage())); diff --git a/src/Actions/CodeUpAction.php b/src/Actions/CodeUpAction.php index 53ea6e2..d04644c 100644 --- a/src/Actions/CodeUpAction.php +++ b/src/Actions/CodeUpAction.php @@ -122,7 +122,6 @@ public function run(?string $stage = null): int try { $this->section("git push ({$msg})"); - $git->getClient()->streamOutput(); $git->push($upstream, "{$branch}:master"); } catch (GitException $gitException) { $lines = count(explode(PHP_EOL, $gitException->getMessage())); diff --git a/src/Services/Git/Client.php b/src/Services/Git/Client.php index 7627bfd..3c4776c 100644 --- a/src/Services/Git/Client.php +++ b/src/Services/Git/Client.php @@ -41,6 +41,4 @@ public function getStatus(): string; public function add(string $filepattern, array $options = []): string; public function commit(...$argsOrOptions): string; - - public function streamOutput(bool $streamOutput = true): void; } diff --git a/src/Services/Git/GitonomyClient.php b/src/Services/Git/GitonomyClient.php index 6aa2cc5..cb0babb 100644 --- a/src/Services/Git/GitonomyClient.php +++ b/src/Services/Git/GitonomyClient.php @@ -151,10 +151,6 @@ public function commit(...$argsOrOptions): string return $this->run(GitCommand::COMMIT, $argsOrOptions); } - public function streamOutput(bool $streamOutput = true): void - { - } - private function run(string $command, array $args): string { try { From 3023a1d09e2a74f7f943a25658de020b5a0364d3 Mon Sep 17 00:00:00 2001 From: Rafik Abdulwahab Date: Mon, 3 Apr 2023 12:53:18 +0100 Subject: [PATCH 6/7] implement adding git remote --- src/Services/Git/GitonomyClient.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Services/Git/GitonomyClient.php b/src/Services/Git/GitonomyClient.php index cb0babb..f7d90b9 100644 --- a/src/Services/Git/GitonomyClient.php +++ b/src/Services/Git/GitonomyClient.php @@ -101,6 +101,7 @@ public function checkout(string $branch) public function addRemote(string $name, ?string $url) { + $this->repository->run(GitCommand::REMOTE, ['add', $name, $url]); } public function setDirectory(string $directory) From ee59868eb3c6667d51ef2396ae2ca59f6dbf59db Mon Sep 17 00:00:00 2001 From: Rafik Abdulwahab Date: Mon, 3 Apr 2023 16:20:32 +0100 Subject: [PATCH 7/7] correct output for remotes list --- src/Services/Git/GitonomyClient.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Services/Git/GitonomyClient.php b/src/Services/Git/GitonomyClient.php index f7d90b9..32a6b6f 100644 --- a/src/Services/Git/GitonomyClient.php +++ b/src/Services/Git/GitonomyClient.php @@ -71,6 +71,13 @@ public function getRemotes(?string $for = 'push'): array $remotes = explode(PHP_EOL, rtrim($this->run(GitCommand::REMOTE, ['-v']))); + $map = []; + foreach ($remotes as $mixed) { + [$key, $value] = explode("\t", $mixed); + $map[$key] = strtok($value, " "); + } + $remotes = $map; + return $remotes; }