|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of SebastianFeldmann\Git. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace SebastianFeldmann\Git; |
|
13
|
|
|
|
|
14
|
|
|
use SebastianFeldmann\Cli\Command\Runner; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Repository |
|
18
|
|
|
* |
|
19
|
|
|
* @package SebastianFeldmann\Git |
|
20
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
21
|
|
|
* @link https://github.com/sebastianfeldmann/git |
|
22
|
|
|
* @since Class available since Release 0.9.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class Repository |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Path to git repository root |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $root; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Path to .git directory |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $dotGitDir; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Commit message. |
|
42
|
|
|
* |
|
43
|
|
|
* @var \SebastianFeldmann\Git\CommitMessage |
|
44
|
|
|
*/ |
|
45
|
|
|
private $commitMsg; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Executes cli commands |
|
49
|
|
|
* |
|
50
|
|
|
* @var \SebastianFeldmann\Cli\Command\Runner |
|
51
|
|
|
*/ |
|
52
|
|
|
private $runner; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Map of operators |
|
56
|
|
|
* |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
private $operator = []; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Repository constructor |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $root |
|
65
|
11 |
|
* @param \SebastianFeldmann\Cli\Command\Runner $runner |
|
66
|
|
|
*/ |
|
67
|
11 |
|
public function __construct(string $root = '', Runner $runner = null) |
|
68
|
11 |
|
{ |
|
69
|
11 |
|
$path = empty($root) ? getcwd() : $root; |
|
70
|
11 |
|
$this->root = $path; |
|
71
|
11 |
|
$this->dotGitDir = $this->root . '/.git'; |
|
72
|
|
|
$this->runner = null == $runner ? new Runner\Simple() : $runner; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Root path getter |
|
77
|
|
|
* |
|
78
|
1 |
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function getRoot(): string |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->root; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns the path to the hooks directory |
|
87
|
|
|
* |
|
88
|
2 |
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function getHooksDir(): string |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->dotGitDir . DIRECTORY_SEPARATOR . 'hooks'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Check for a hook file. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $hook |
|
99
|
1 |
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function hookExists($hook): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return file_exists($this->getHooksDir() . DIRECTORY_SEPARATOR . $hook); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* CommitMessage setter. |
|
108
|
|
|
* |
|
109
|
|
|
* @param \SebastianFeldmann\Git\CommitMessage $commitMsg |
|
110
|
1 |
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function setCommitMsg(CommitMessage $commitMsg): void |
|
113
|
1 |
|
{ |
|
114
|
|
|
$this->commitMsg = $commitMsg; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* CommitMessage getter. |
|
119
|
|
|
* |
|
120
|
2 |
|
* @return \SebastianFeldmann\Git\CommitMessage |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function getCommitMsg(): CommitMessage |
|
123
|
1 |
|
{ |
|
124
|
|
|
if (null === $this->commitMsg) { |
|
125
|
1 |
|
throw new \RuntimeException('No commit message available'); |
|
126
|
|
|
} |
|
127
|
|
|
return $this->commitMsg; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Is there a merge in progress. |
|
132
|
|
|
* |
|
133
|
2 |
|
* @return bool |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public function isMerging(): bool |
|
136
|
2 |
|
{ |
|
137
|
2 |
|
foreach (['MERGE_MSG', 'MERGE_HEAD', 'MERGE_MODE'] as $fileName) { |
|
138
|
|
|
if (file_exists($this->dotGitDir . DIRECTORY_SEPARATOR . $fileName)) { |
|
139
|
|
|
return true; |
|
140
|
1 |
|
} |
|
141
|
|
|
} |
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get index operator. |
|
147
|
|
|
* |
|
148
|
1 |
|
* @return \SebastianFeldmann\Git\Operator\Index |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function getIndexOperator(): Operator\Index |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->getOperator('Index'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get info operator. |
|
157
|
|
|
* |
|
158
|
1 |
|
* @return \SebastianFeldmann\Git\Operator\Info |
|
159
|
|
|
*/ |
|
160
|
1 |
|
public function getInfoOperator(): Operator\Info |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->getOperator('Info'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get log operator. |
|
167
|
|
|
* |
|
168
|
1 |
|
* @return \SebastianFeldmann\Git\Operator\Log |
|
169
|
|
|
*/ |
|
170
|
1 |
|
public function getLogOperator(): Operator\Log |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->getOperator('Log'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Get config operator. |
|
177
|
|
|
* |
|
178
|
1 |
|
* @return \SebastianFeldmann\Git\Operator\Config |
|
179
|
|
|
*/ |
|
180
|
1 |
|
public function getConfigOperator(): Operator\Config |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->getOperator('Config'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get diff operator. |
|
187
|
|
|
* |
|
188
|
1 |
|
* @return \SebastianFeldmann\Git\Operator\Diff |
|
189
|
|
|
*/ |
|
190
|
1 |
|
public function getDiffOperator(): Operator\Diff |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->getOperator('Diff'); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Return requested operator. |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $name |
|
199
|
5 |
|
* @return mixed |
|
200
|
|
|
*/ |
|
201
|
5 |
|
private function getOperator(string $name) |
|
202
|
5 |
|
{ |
|
203
|
5 |
|
if (!isset($this->operator[$name])) { |
|
204
|
|
|
$class = '\\SebastianFeldmann\\Git\\Operator\\' . $name; |
|
205
|
5 |
|
$this->operator[$name] = new $class($this->runner, $this); |
|
206
|
|
|
} |
|
207
|
|
|
return $this->operator[$name]; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Creates a Repository but makes sure the repository exists |
|
212
|
|
|
* |
|
213
|
|
|
* @param string $root |
|
214
|
|
|
* @param \SebastianFeldmann\Cli\Command\Runner|null $runner |
|
215
|
2 |
|
* @return \SebastianFeldmann\Git\Repository |
|
216
|
|
|
*/ |
|
217
|
2 |
|
public static function createVerified(string $root, Runner $runner = null): Repository |
|
218
|
1 |
|
{ |
|
219
|
|
|
if (!self::isGitRepository($root)) { |
|
220
|
1 |
|
throw new \RuntimeException(sprintf('Invalid git repository: %s', $root)); |
|
221
|
|
|
} |
|
222
|
|
|
return new Repository($root, $runner); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param string $root |
|
227
|
2 |
|
* @return bool |
|
228
|
|
|
*/ |
|
229
|
2 |
|
public static function isGitRepository(string $root): bool |
|
230
|
|
|
{ |
|
231
|
|
|
return is_dir($root . '/.git'); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|