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