Commit c7e3176b authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Corrige la gestion du flag "--no-remote".

Ainsi que les transitions lors du passage de l'un à l'autre.
parent 69b0c1ad
No related merge requests found
Pipeline #13114 failed with stages
in 46 seconds
Showing with 204 additions and 52 deletions
+204 -52
<?php
/*
* This file is part of "irstea/make-shim".
* (c) 2019-2020 Irstea <dsi.poleis@irstea.fr>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Irstea\MakeShim\Repository;
use GitWrapper\GitException;
use GitWrapper\GitWorkingCopy;
use GitWrapper\GitWrapper;
use Psr\Log\LoggerInterface;
final class DefaultRemoteStrategy extends NoRemoteStrategy
{
/**
* @var string
*/
private $remote;
public function __construct(GitWrapper $wrapper, string $path, string $remote, LoggerInterface $logger = null)
{
parent::__construct($wrapper, $path, $logger);
$this->remote = $remote;
}
protected function doPrepare(): GitWorkingCopy
{
if (!$this->isInited()) {
try {
return $this->wrapper->cloneRepository($this->remote, $this->path);
} catch (GitException $gitException) {
$this->logger->warning("Could not clone from {$this->remote}: {$gitException->getMessage()}", ['exception' => $gitException]);
}
}
$workingCopy = parent::doPrepare();
$remotes = $workingCopy->getRemotes();
if (empty($remotes['origin'])) {
$workingCopy->addRemote('origin', $this->remote);
$this->logger->debug("Added remote 'origin': {$this->remote}");
} elseif ($remotes['origin']['fetch'] !== $this->remote) {
$workingCopy->remote('set-url', 'origin', $this->remote);
$this->logger->debug("Set remote 'origin': {$this->remote}");
}
return $workingCopy;
}
public function reset(): void
{
$workingCopy = $this->prepare();
$this->logger->debug('Fetching from origin');
$workingCopy->fetch('origin', 'master', '--tags', '--prune', '--force');
$this->logger->debug('Resetting to origin/master');
$workingCopy->reset('--hard', 'origin/master');
}
public function push(array $refs): bool
{
$workingCopy = $this->prepare();
$workingCopy->push('--force', $this->remote, ...$refs);
$this->logger->notice("Successfully pushed to $this->remote: " . implode(', ', $refs));
$workingCopy->run('prune');
}
}
......@@ -10,14 +10,11 @@
namespace Irstea\MakeShim\Repository;
use GitWrapper\GitException;
use GitWrapper\GitWorkingCopy;
use GitWrapper\GitWrapper;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use function Safe\mkdir;
use Webmozart\PathUtil\Path;
/**
* Class Factory.
......@@ -45,47 +42,19 @@ final class Factory implements FactoryInterface, LoggerAwareInterface
/**
* {@inheritdoc}
*
* @throws \Safe\Exceptions\FilesystemException
*/
public function create(string $path, ?string $remote): RepositoryInterface
{
try {
if (!\is_dir(Path::join($path, '.git'))) {
if ($remote) {
$this->logger->debug("Cloning $remote into $path");
try {
return $this->createRepository($this->wrapper->cloneRepository($remote, $path), $remote);
} catch (GitException $gitException) {
$this->logger->error("Could not clone $remote: " . $gitException->getMessage());
}
}
$this->logger->debug("Creating a new repository: $path");
mkdir($path, 0777, true);
return $this->createRepository($this->wrapper->init($path), $remote);
}
$this->logger->debug("Reusing working copy at $path");
$workingCopy = $this->wrapper->workingCopy($path);
$workingCopy->fetch('origin', 'master', '--tags', '--prune', '--force');
$workingCopy->reset('--hard', 'origin/master');
$strategy = $remote ? new DefaultRemoteStrategy($this->wrapper, $path, $remote, $this->logger) : new NoRemoteStrategy($this->wrapper, $path, $this->logger);
$workingCopy = $strategy->prepare();
$strategy->reset();
return $this->createRepository($workingCopy, $remote);
return new Repository($workingCopy, $strategy, $this->logger);
} catch (GitException $gitException) {
throw \Irstea\MakeShim\Exception\GitException::fromGitWrapper($gitException);
}
}
/**
* @param GitWorkingCopy $workingCopy
* @param string|null $remote
*
* @return RepositoryInterface
*/
private function createRepository(GitWorkingCopy $workingCopy, ?string $remote): RepositoryInterface
{
return new Repository($workingCopy, $remote, $this->logger);
}
}
<?php
/*
* This file is part of "irstea/make-shim".
* (c) 2019-2020 Irstea <dsi.poleis@irstea.fr>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Irstea\MakeShim\Repository;
use GitWrapper\GitWorkingCopy;
use GitWrapper\GitWrapper;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use function Safe\mkdir;
class NoRemoteStrategy implements RemoteStrategyInterface
{
/**
* @var string
*/
protected $path;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var GitWrapper
*/
protected $wrapper;
/**
* @var GitWorkingCopy|null
*/
private $workingCopy;
public function __construct(GitWrapper $wrapper, string $path, LoggerInterface $logger = null)
{
$this->wrapper = $wrapper;
$this->path = $path;
$this->logger = $logger ?: new NullLogger();
}
protected function isInited(): bool
{
return is_dir($this->path . '/.git');
}
/**
* @throws \Safe\Exceptions\FilesystemException
*/
public function prepare(): GitWorkingCopy
{
if (!$this->workingCopy) {
$this->workingCopy = $this->doPrepare();
}
return $this->workingCopy;
}
/**
* @throws \Safe\Exceptions\FilesystemException
*/
protected function doPrepare(): GitWorkingCopy
{
if (!$this->isInited()) {
$this->logger->debug("Creating a new repository: {$this->path}");
mkdir($this->path, 0777, true);
return $this->wrapper->init($this->path);
}
$this->logger->debug("Reusing existing repository: {$this->path}");
return $this->wrapper->workingCopy($this->path);
}
public function reset(): void
{
$this->logger->debug('Resetting to master');
$this->prepare()->reset('--hard');
}
public function push(array $refs): bool
{
$this->logger->info('Not pushing reference(s): ' . join(', ', $refs));
return false;
}
}
<?php
/*
* This file is part of "irstea/make-shim".
* (c) 2019-2020 Irstea <dsi.poleis@irstea.fr>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Irstea\MakeShim\Repository;
use GitWrapper\GitWorkingCopy;
interface RemoteStrategyInterface
{
public function prepare(): GitWorkingCopy;
public function reset(): void;
public function push(array $refs): bool;
}
......@@ -34,22 +34,22 @@ final class Repository implements RepositoryInterface, LoggerAwareInterface
private $changedRefs = [];
/**
* @var string|null
* @var RemoteStrategyInterface
*/
private $remote;
private $remoteStrategy;
/**
* Repository constructor.
*
* @param GitWorkingCopy $workingCopy
* @param string|null $remote
* @param LoggerInterface|null $logger
* @param GitWorkingCopy $workingCopy
* @param RemoteStrategyInterface $remoteStrategy
* @param LoggerInterface|null $logger
*/
public function __construct(GitWorkingCopy $workingCopy, ?string $remote, LoggerInterface $logger = null)
public function __construct(GitWorkingCopy $workingCopy, RemoteStrategyInterface $remoteStrategy, LoggerInterface $logger = null)
{
$this->workingCopy = $workingCopy;
$this->logger = $logger ?: new NullLogger();
$this->remote = $remote;
$this->remoteStrategy = $remoteStrategy;
}
/**
......@@ -115,16 +115,13 @@ final class Repository implements RepositoryInterface, LoggerAwareInterface
public function push(): bool
{
try {
if (!$this->remote || !$this->hasChangedRefs()) {
return false;
}
if ($this->hasChangedRefs() && $this->remoteStrategy->push(array_keys($this->changedRefs))) {
$this->changedRefs = [];
$this->workingCopy->push('--force', $this->remote, ...array_keys($this->changedRefs));
$this->logger->notice("Successfully pushed to $this->remote");
$this->workingCopy->run('prune');
$this->changedRefs = [];
return true;
}
return true;
return false;
} catch (GitException $gitException) {
throw \Irstea\MakeShim\Exception\GitException::fromGitWrapper($gitException);
}
......
......@@ -47,6 +47,7 @@ $container = new Container();
$container['application'] = static function (Container $c) {
$application = new Application('make-shim', Versions::getVersion('irstea/make-shim'));
$application->add($c['build-command']);
$application->setDefaultCommand($c['build-command']->getName(), true);
return $application;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment