-
Notifications
You must be signed in to change notification settings - Fork 3
/
Git.php
149 lines (116 loc) · 2.62 KB
/
Git.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Genesis\Commands;
use Genesis\InvalidArgumentException;
/**
* @author Adam Bisek <[email protected]>
*/
class Git extends Command
{
/** @var string */
private $gitExecutable = 'git';
private $command;
private $workingDirectory;
/**
* Redirect stderr to stdout? - for testing purposes only
* Git writes his progressive output do stderr (even if everything ok) - at least when clonning
* @var bool
*/
private $redirectStderrToStdout = FALSE;
/**
* Returns path to git executable
* @return string
*/
public function getGitExecutable()
{
return $this->gitExecutable;
}
/**
* Sets path to git executable
* @param string $gitExecutable
*/
public function setGitExecutable($gitExecutable)
{
if ($gitExecutable == '') {
throw new InvalidArgumentException("Git executable cannot be empty.");
}
$this->gitExecutable = $gitExecutable;
}
/**
* @return string
*/
public function getWorkingDirectory()
{
return $this->workingDirectory;
}
/**
* Sets path to working directory
* @param string $workingTree
*/
public function setWorkingDirectory($workingDirectory)
{
$this->workingDirectory = $workingDirectory;
}
/**
* @return boolean
*/
public function isRedirectStderrToStdout()
{
return $this->redirectStderrToStdout;
}
/**
* @param boolean $redirectStderrToStdout
*/
public function setRedirectStderrToStdout($redirectStderrToStdout)
{
$this->redirectStderrToStdout = $redirectStderrToStdout;
}
/**
* Return setted git command
* @return mixed
*/
public function getCommand()
{
return $this->command;
}
/**
* Sets git command (to be executed later)
* @param mixed $command
*/
public function setCommand($command)
{
$this->command = $command;
}
/**
* Sets git command for cloning repository (to be executed later)
*/
public function cloneRepo($url, $branch = NULL, $dir = NULL)
{
$command = "clone";
$command .= " --depth 1 --recursive $url";
$command .= ($branch ? " --branch $branch" : '');
$command .= ($dir ? ' ' . escapeshellarg($dir) : '');
$this->setCommand($command);
}
/**
* @return ExecResult
*/
public function execute()
{
$command = '';
if ($this->workingDirectory !== NULL) {
$command .= 'cd ' . escapeshellarg($this->workingDirectory) . ' && ';
}
$command .= escapeshellarg($this->gitExecutable);
$execCommand = $command . ' ' . $this->command;
if ($this->redirectStderrToStdout) {
$execCommand .= ' 2>&1';
}
return $this->exec($execCommand);
}
private function exec($command)
{
$exec = new Exec();
$exec->setCommand($command);
return $exec->execute();
}
}