Skip to content

Commit 76b0c35

Browse files
committed
Add new methd getVerboseMessage
1 parent 044dbe5 commit 76b0c35

File tree

3 files changed

+763
-763
lines changed

3 files changed

+763
-763
lines changed

src/Git.php

Lines changed: 148 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,150 @@
11
<?php
22

3-
namespace CzProject\GitPhp;
4-
5-
6-
class Git
7-
{
8-
/** @var IRunner */
9-
protected $runner;
10-
11-
/** @var bool */
12-
protected $verboseException;
13-
14-
public function __construct(IRunner $runner = NULL, $verboseException = false)
15-
{
16-
$this->runner = $runner !== NULL ? $runner : new Runners\CliRunner;
17-
$this->verboseException = $verboseException;
18-
}
19-
20-
21-
/**
22-
* @param string $directory
23-
* @return GitRepository
24-
*/
25-
public function open($directory)
26-
{
27-
return new GitRepository($directory, $this->runner, $this->verboseException);
28-
}
29-
30-
31-
/**
32-
* Init repo in directory
33-
* @param string $directory
34-
* @param array<mixed>|NULL $params
35-
* @return GitRepository
36-
* @throws GitException
37-
*/
38-
public function init($directory, array $params = NULL)
39-
{
40-
if (is_dir("$directory/.git")) {
41-
throw new GitException("Repo already exists in $directory.");
42-
}
43-
44-
if (!is_dir($directory) && !@mkdir($directory, 0777, TRUE)) { // intentionally @; not atomic; from Nette FW
45-
throw new GitException("Unable to create directory '$directory'.");
46-
}
47-
48-
try {
49-
$this->run($directory, [
50-
'init',
51-
$params,
52-
$directory
53-
]);
54-
55-
} catch (GitException $e) {
56-
throw new GitException("Git init failed (directory $directory).", $e->getCode(), $e);
57-
}
58-
59-
return $this->open($directory);
60-
}
61-
62-
63-
/**
64-
* Clones GIT repository from $url into $directory
65-
* @param string $url
66-
* @param string|NULL $directory
67-
* @param array<mixed>|NULL $params
68-
* @return GitRepository
69-
* @throws GitException
70-
*/
71-
public function cloneRepository($url, $directory = NULL, array $params = NULL)
72-
{
73-
if ($directory !== NULL && is_dir("$directory/.git")) {
74-
throw new GitException("Repo already exists in $directory.");
75-
}
76-
77-
$cwd = $this->runner->getCwd();
78-
79-
if ($directory === NULL) {
80-
$directory = Helpers::extractRepositoryNameFromUrl($url);
81-
$directory = "$cwd/$directory";
82-
83-
} elseif(!Helpers::isAbsolute($directory)) {
84-
$directory = "$cwd/$directory";
85-
}
86-
87-
if ($params === NULL) {
88-
$params = '-q';
89-
}
90-
91-
try {
92-
$this->run($cwd, [
93-
'clone',
94-
$params,
95-
$url,
96-
$directory
97-
]);
98-
99-
} catch (GitException $e) {
100-
$stderr = '';
101-
$result = $e->getRunnerResult();
102-
103-
if ($result !== NULL && $result->hasErrorOutput()) {
104-
$stderr = implode(PHP_EOL, $result->getErrorOutput());
105-
}
106-
107-
throw new GitException("Git clone failed (directory $directory)." . ($stderr !== '' ? ("\n$stderr") : ''));
108-
}
109-
110-
return $this->open($directory);
111-
}
112-
113-
114-
/**
115-
* @param string $url
116-
* @param array<string>|NULL $refs
117-
* @return bool
118-
*/
119-
public function isRemoteUrlReadable($url, array $refs = NULL)
120-
{
121-
$result = $this->runner->run($this->runner->getCwd(), [
122-
'ls-remote',
123-
'--heads',
124-
'--quiet',
125-
'--exit-code',
126-
$url,
127-
$refs,
128-
], [
129-
'GIT_TERMINAL_PROMPT' => 0,
130-
]);
131-
132-
return $result->isOk();
133-
}
134-
135-
136-
/**
137-
* @param string $cwd
138-
* @param array<mixed> $args
139-
* @param array<string, scalar> $env
140-
* @return RunnerResult
141-
* @throws GitException
142-
*/
143-
private function run($cwd, array $args, array $env = NULL)
144-
{
145-
$result = $this->runner->run($cwd, $args, $env);
146-
147-
if (!$result->isOk()) {
148-
$exceptionMessage = $this->verboseException ? $result->toText() : "Command '{$result->getCommand()}' failed (exit-code {$result->getExitCode()}).";
149-
throw new GitException($exceptionMessage, $result->getExitCode(), NULL, $result);
150-
}
151-
152-
return $result;
153-
}
154-
}
3+
namespace CzProject\GitPhp;
4+
5+
6+
class Git
7+
{
8+
/** @var IRunner */
9+
protected $runner;
10+
11+
12+
public function __construct(IRunner $runner = NULL)
13+
{
14+
$this->runner = $runner !== NULL ? $runner : new Runners\CliRunner;
15+
}
16+
17+
18+
/**
19+
* @param string $directory
20+
* @return GitRepository
21+
*/
22+
public function open($directory)
23+
{
24+
return new GitRepository($directory, $this->runner);
25+
}
26+
27+
28+
/**
29+
* Init repo in directory
30+
* @param string $directory
31+
* @param array<mixed>|NULL $params
32+
* @return GitRepository
33+
* @throws GitException
34+
*/
35+
public function init($directory, array $params = NULL)
36+
{
37+
if (is_dir("$directory/.git")) {
38+
throw new GitException("Repo already exists in $directory.");
39+
}
40+
41+
if (!is_dir($directory) && !@mkdir($directory, 0777, TRUE)) { // intentionally @; not atomic; from Nette FW
42+
throw new GitException("Unable to create directory '$directory'.");
43+
}
44+
45+
try {
46+
$this->run($directory, [
47+
'init',
48+
$params,
49+
$directory
50+
]);
51+
52+
} catch (GitException $e) {
53+
throw new GitException("Git init failed (directory $directory).", $e->getCode(), $e);
54+
}
55+
56+
return $this->open($directory);
57+
}
58+
59+
60+
/**
61+
* Clones GIT repository from $url into $directory
62+
* @param string $url
63+
* @param string|NULL $directory
64+
* @param array<mixed>|NULL $params
65+
* @return GitRepository
66+
* @throws GitException
67+
*/
68+
public function cloneRepository($url, $directory = NULL, array $params = NULL)
69+
{
70+
if ($directory !== NULL && is_dir("$directory/.git")) {
71+
throw new GitException("Repo already exists in $directory.");
72+
}
73+
74+
$cwd = $this->runner->getCwd();
75+
76+
if ($directory === NULL) {
77+
$directory = Helpers::extractRepositoryNameFromUrl($url);
78+
$directory = "$cwd/$directory";
79+
80+
} elseif(!Helpers::isAbsolute($directory)) {
81+
$directory = "$cwd/$directory";
82+
}
83+
84+
if ($params === NULL) {
85+
$params = '-q';
86+
}
87+
88+
try {
89+
$this->run($cwd, [
90+
'clone',
91+
$params,
92+
$url,
93+
$directory
94+
]);
95+
96+
} catch (GitException $e) {
97+
$stderr = '';
98+
$result = $e->getRunnerResult();
99+
100+
if ($result !== NULL && $result->hasErrorOutput()) {
101+
$stderr = implode(PHP_EOL, $result->getErrorOutput());
102+
}
103+
104+
throw new GitException("Git clone failed (directory $directory)." . ($stderr !== '' ? ("\n$stderr") : ''));
105+
}
106+
107+
return $this->open($directory);
108+
}
109+
110+
111+
/**
112+
* @param string $url
113+
* @param array<string>|NULL $refs
114+
* @return bool
115+
*/
116+
public function isRemoteUrlReadable($url, array $refs = NULL)
117+
{
118+
$result = $this->runner->run($this->runner->getCwd(), [
119+
'ls-remote',
120+
'--heads',
121+
'--quiet',
122+
'--exit-code',
123+
$url,
124+
$refs,
125+
], [
126+
'GIT_TERMINAL_PROMPT' => 0,
127+
]);
128+
129+
return $result->isOk();
130+
}
131+
132+
133+
/**
134+
* @param string $cwd
135+
* @param array<mixed> $args
136+
* @param array<string, scalar> $env
137+
* @return RunnerResult
138+
* @throws GitException
139+
*/
140+
private function run($cwd, array $args, array $env = NULL)
141+
{
142+
$result = $this->runner->run($cwd, $args, $env);
143+
144+
if (!$result->isOk()) {
145+
throw new GitException("Command '{$result->getCommand()}' failed (exit-code {$result->getExitCode()}).", $result->getExitCode(), NULL, $result);
146+
}
147+
148+
return $result;
149+
}
150+
}

0 commit comments

Comments
 (0)