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(): string |
94
|
|
|
{ |
95
|
2 |
|
return $this->dotGitDir . DIRECTORY_SEPARATOR . 'hooks'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check for a hook file. |
100
|
|
|
* |
101
|
|
|
* @param string $hook |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
1 |
|
public function hookExists($hook): bool |
105
|
|
|
{ |
106
|
1 |
|
return file_exists($this->getHooksDir() . DIRECTORY_SEPARATOR . $hook); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* CommitMessage setter. |
111
|
|
|
* |
112
|
|
|
* @param \SebastianFeldmann\Git\CommitMessage $commitMsg |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
1 |
|
public function setCommitMsg(CommitMessage $commitMsg): void |
116
|
|
|
{ |
117
|
1 |
|
$this->commitMsg = $commitMsg; |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* CommitMessage getter. |
122
|
|
|
* |
123
|
|
|
* @return \SebastianFeldmann\Git\CommitMessage |
124
|
|
|
*/ |
125
|
2 |
|
public function getCommitMsg(): CommitMessage |
126
|
|
|
{ |
127
|
2 |
|
if (null === $this->commitMsg) { |
128
|
1 |
|
throw new \RuntimeException('No commit message available'); |
129
|
|
|
} |
130
|
1 |
|
return $this->commitMsg; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Is there a merge in progress. |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
2 |
|
public function isMerging(): bool |
139
|
|
|
{ |
140
|
2 |
|
foreach (['MERGE_MSG', 'MERGE_HEAD', 'MERGE_MODE'] as $fileName) { |
141
|
2 |
|
if (file_exists($this->dotGitDir . DIRECTORY_SEPARATOR . $fileName)) { |
142
|
2 |
|
return true; |
143
|
|
|
} |
144
|
|
|
} |
145
|
1 |
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get index operator. |
150
|
|
|
* |
151
|
|
|
* @return \SebastianFeldmann\Git\Operator\Index |
152
|
|
|
*/ |
153
|
1 |
|
public function getIndexOperator(): Operator\Index |
154
|
|
|
{ |
155
|
1 |
|
return $this->getOperator('Index'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get info operator. |
160
|
|
|
* |
161
|
|
|
* @return \SebastianFeldmann\Git\Operator\Info |
162
|
|
|
*/ |
163
|
1 |
|
public function getInfoOperator(): Operator\Info |
164
|
|
|
{ |
165
|
1 |
|
return $this->getOperator('Info'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get log operator. |
170
|
|
|
* |
171
|
|
|
* @return \SebastianFeldmann\Git\Operator\Log |
172
|
|
|
*/ |
173
|
1 |
|
public function getLogOperator(): Operator\Log |
174
|
|
|
{ |
175
|
1 |
|
return $this->getOperator('Log'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get config operator. |
180
|
|
|
* |
181
|
|
|
* @return \SebastianFeldmann\Git\Operator\Config |
182
|
|
|
*/ |
183
|
1 |
|
public function getConfigOperator(): Operator\Config |
184
|
|
|
{ |
185
|
1 |
|
return $this->getOperator('Config'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get diff operator. |
190
|
|
|
* |
191
|
|
|
* @return \SebastianFeldmann\Git\Operator\Diff |
192
|
|
|
*/ |
193
|
1 |
|
public function getDiffOperator(): Operator\Diff |
194
|
|
|
{ |
195
|
1 |
|
return $this->getOperator('Diff'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Return requested operator. |
200
|
|
|
* |
201
|
|
|
* @param string $name |
202
|
|
|
* @return mixed |
203
|
|
|
*/ |
204
|
5 |
|
private function getOperator(string $name) |
205
|
|
|
{ |
206
|
5 |
|
if (!isset($this->operator[$name])) { |
207
|
5 |
|
$class = '\\SebastianFeldmann\\Git\\Operator\\' . $name; |
208
|
5 |
|
$this->operator[$name] = new $class($this->runner, $this); |
209
|
|
|
} |
210
|
5 |
|
return $this->operator[$name]; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|