diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a5c462..7df91c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 2.4.1 - 2023-04-19 +- Add support for no existing repo with new git client + ## 2.4.0 - 2023-04-17 - Replace `cpliakas/git-wrapper` with `gitonomy/gitlib` - Allow customising commit message made in `code/up` command diff --git a/src/Services/Git/GitonomyClient.php b/src/Services/Git/GitonomyClient.php index 32a6b6f..9600166 100644 --- a/src/Services/Git/GitonomyClient.php +++ b/src/Services/Git/GitonomyClient.php @@ -5,6 +5,7 @@ use fortrabbit\Copy\Exceptions\GitException; use Gitonomy\Git\Admin; use Gitonomy\Git\Exception\ProcessException; +use Gitonomy\Git\Exception\ReferenceNotFoundException; use Gitonomy\Git\Reference\Branch; use Gitonomy\Git\Repository; use LogicException; @@ -38,11 +39,19 @@ public function pull(string $upstream, string $branch = 'master'): string public function getLocalHead(): ?string { - $head = $this->repository->getHead(); - if ($head instanceof Branch) { - return $head->getName(); + try { + $head = $this->repository->getHead(); + + if ($head instanceof Branch) { + return $head->getName(); + } + + return $head->getFullName(); + } catch (ReferenceNotFoundException) { + return NULL; } - return $head->getFullName(); + + return NULL; } public function getLocalBranches(): array @@ -72,11 +81,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; + foreach ($remotes as $mixed) { + if ($mixed === '') continue; + + [$key, $value] = explode("\t", $mixed); + $map[$key] = strtok($value, " "); + } + $remotes = $map; return $remotes; } @@ -114,7 +125,6 @@ public function addRemote(string $name, ?string $url) public function setDirectory(string $directory) { $this->directory = $directory; - $this->repository = new Repository($directory); } public function getDirectory(): string @@ -124,7 +134,8 @@ public function getDirectory(): string public function init() { - Admin::init($this->directory, $this->repository->isBare()); + Admin::init($this->directory, false); + $this->repository = new Repository($this->directory); } public function log(...$argsOrOptions): string