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