Skip to content

Commit

Permalink
Add new methd getVerboseMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
PululuK committed Feb 1, 2022
1 parent 044dbe5 commit 76b0c35
Show file tree
Hide file tree
Showing 3 changed files with 763 additions and 763 deletions.
300 changes: 148 additions & 152 deletions src/Git.php
Original file line number Diff line number Diff line change
@@ -1,154 +1,150 @@
<?php

namespace CzProject\GitPhp;


class Git
{
/** @var IRunner */
protected $runner;

/** @var bool */
protected $verboseException;

public function __construct(IRunner $runner = NULL, $verboseException = false)
{
$this->runner = $runner !== NULL ? $runner : new Runners\CliRunner;
$this->verboseException = $verboseException;
}


/**
* @param string $directory
* @return GitRepository
*/
public function open($directory)
{
return new GitRepository($directory, $this->runner, $this->verboseException);
}


/**
* Init repo in directory
* @param string $directory
* @param array<mixed>|NULL $params
* @return GitRepository
* @throws GitException
*/
public function init($directory, array $params = NULL)
{
if (is_dir("$directory/.git")) {
throw new GitException("Repo already exists in $directory.");
}

if (!is_dir($directory) && !@mkdir($directory, 0777, TRUE)) { // intentionally @; not atomic; from Nette FW
throw new GitException("Unable to create directory '$directory'.");
}

try {
$this->run($directory, [
'init',
$params,
$directory
]);

} catch (GitException $e) {
throw new GitException("Git init failed (directory $directory).", $e->getCode(), $e);
}

return $this->open($directory);
}


/**
* Clones GIT repository from $url into $directory
* @param string $url
* @param string|NULL $directory
* @param array<mixed>|NULL $params
* @return GitRepository
* @throws GitException
*/
public function cloneRepository($url, $directory = NULL, array $params = NULL)
{
if ($directory !== NULL && is_dir("$directory/.git")) {
throw new GitException("Repo already exists in $directory.");
}

$cwd = $this->runner->getCwd();

if ($directory === NULL) {
$directory = Helpers::extractRepositoryNameFromUrl($url);
$directory = "$cwd/$directory";

} elseif(!Helpers::isAbsolute($directory)) {
$directory = "$cwd/$directory";
}

if ($params === NULL) {
$params = '-q';
}

try {
$this->run($cwd, [
'clone',
$params,
$url,
$directory
]);

} catch (GitException $e) {
$stderr = '';
$result = $e->getRunnerResult();

if ($result !== NULL && $result->hasErrorOutput()) {
$stderr = implode(PHP_EOL, $result->getErrorOutput());
}

throw new GitException("Git clone failed (directory $directory)." . ($stderr !== '' ? ("\n$stderr") : ''));
}

return $this->open($directory);
}


/**
* @param string $url
* @param array<string>|NULL $refs
* @return bool
*/
public function isRemoteUrlReadable($url, array $refs = NULL)
{
$result = $this->runner->run($this->runner->getCwd(), [
'ls-remote',
'--heads',
'--quiet',
'--exit-code',
$url,
$refs,
], [
'GIT_TERMINAL_PROMPT' => 0,
]);

return $result->isOk();
}


/**
* @param string $cwd
* @param array<mixed> $args
* @param array<string, scalar> $env
* @return RunnerResult
* @throws GitException
*/
private function run($cwd, array $args, array $env = NULL)
{
$result = $this->runner->run($cwd, $args, $env);

if (!$result->isOk()) {
$exceptionMessage = $this->verboseException ? $result->toText() : "Command '{$result->getCommand()}' failed (exit-code {$result->getExitCode()}).";
throw new GitException($exceptionMessage, $result->getExitCode(), NULL, $result);
}

return $result;
}
}
namespace CzProject\GitPhp;


class Git
{
/** @var IRunner */
protected $runner;


public function __construct(IRunner $runner = NULL)
{
$this->runner = $runner !== NULL ? $runner : new Runners\CliRunner;
}


/**
* @param string $directory
* @return GitRepository
*/
public function open($directory)
{
return new GitRepository($directory, $this->runner);
}


/**
* Init repo in directory
* @param string $directory
* @param array<mixed>|NULL $params
* @return GitRepository
* @throws GitException
*/
public function init($directory, array $params = NULL)
{
if (is_dir("$directory/.git")) {
throw new GitException("Repo already exists in $directory.");
}

if (!is_dir($directory) && !@mkdir($directory, 0777, TRUE)) { // intentionally @; not atomic; from Nette FW
throw new GitException("Unable to create directory '$directory'.");
}

try {
$this->run($directory, [
'init',
$params,
$directory
]);

} catch (GitException $e) {
throw new GitException("Git init failed (directory $directory).", $e->getCode(), $e);
}

return $this->open($directory);
}


/**
* Clones GIT repository from $url into $directory
* @param string $url
* @param string|NULL $directory
* @param array<mixed>|NULL $params
* @return GitRepository
* @throws GitException
*/
public function cloneRepository($url, $directory = NULL, array $params = NULL)
{
if ($directory !== NULL && is_dir("$directory/.git")) {
throw new GitException("Repo already exists in $directory.");
}

$cwd = $this->runner->getCwd();

if ($directory === NULL) {
$directory = Helpers::extractRepositoryNameFromUrl($url);
$directory = "$cwd/$directory";

} elseif(!Helpers::isAbsolute($directory)) {
$directory = "$cwd/$directory";
}

if ($params === NULL) {
$params = '-q';
}

try {
$this->run($cwd, [
'clone',
$params,
$url,
$directory
]);

} catch (GitException $e) {
$stderr = '';
$result = $e->getRunnerResult();

if ($result !== NULL && $result->hasErrorOutput()) {
$stderr = implode(PHP_EOL, $result->getErrorOutput());
}

throw new GitException("Git clone failed (directory $directory)." . ($stderr !== '' ? ("\n$stderr") : ''));
}

return $this->open($directory);
}


/**
* @param string $url
* @param array<string>|NULL $refs
* @return bool
*/
public function isRemoteUrlReadable($url, array $refs = NULL)
{
$result = $this->runner->run($this->runner->getCwd(), [
'ls-remote',
'--heads',
'--quiet',
'--exit-code',
$url,
$refs,
], [
'GIT_TERMINAL_PROMPT' => 0,
]);

return $result->isOk();
}


/**
* @param string $cwd
* @param array<mixed> $args
* @param array<string, scalar> $env
* @return RunnerResult
* @throws GitException
*/
private function run($cwd, array $args, array $env = NULL)
{
$result = $this->runner->run($cwd, $args, $env);

if (!$result->isOk()) {
throw new GitException("Command '{$result->getCommand()}' failed (exit-code {$result->getExitCode()}).", $result->getExitCode(), NULL, $result);
}

return $result;
}
}
Loading

0 comments on commit 76b0c35

Please sign in to comment.