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() : realpath($root); |
68
|
|
|
// check for existing .git dir |
69
|
11 |
|
if (!is_dir($path . DIRECTORY_SEPARATOR . '.git')) { |
70
|
1 |
|
throw new \RuntimeException(sprintf('Invalid git repository: %s', $root)); |
71
|
|
|
} |
72
|
10 |
|
$this->root = $path; |
73
|
10 |
|
$this->dotGitDir = $this->root . DIRECTORY_SEPARATOR . '.git'; |
74
|
10 |
|
$this->runner = null == $runner ? new Runner\Simple() : $runner; |
75
|
10 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Root path getter. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
public function getRoot() : string |
83
|
|
|
{ |
84
|
1 |
|
return $this->root; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the path to the hooks directory. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
2 |
|
public function getHooksDir() : string |
93
|
|
|
{ |
94
|
2 |
|
return $this->dotGitDir . DIRECTORY_SEPARATOR . 'hooks'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check for a hook file. |
99
|
|
|
* |
100
|
|
|
* @param string $hook |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
1 |
|
public function hookExists($hook) : bool |
104
|
|
|
{ |
105
|
1 |
|
return file_exists($this->getHooksDir() . DIRECTORY_SEPARATOR . $hook); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* CommitMessage setter. |
110
|
|
|
* |
111
|
|
|
* @param \SebastianFeldmann\Git\CommitMessage $commitMsg |
112
|
|
|
*/ |
113
|
1 |
|
public function setCommitMsg(CommitMessage $commitMsg) |
114
|
|
|
{ |
115
|
1 |
|
$this->commitMsg = $commitMsg; |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* CommitMessage getter. |
120
|
|
|
* |
121
|
|
|
* @return \SebastianFeldmann\Git\CommitMessage |
122
|
|
|
*/ |
123
|
2 |
|
public function getCommitMsg() : CommitMessage |
124
|
|
|
{ |
125
|
2 |
|
if (null === $this->commitMsg) { |
126
|
1 |
|
throw new \RuntimeException('No commit message available'); |
127
|
|
|
} |
128
|
1 |
|
return $this->commitMsg; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Is there a merge in progress. |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
2 |
|
public function isMerging() : bool |
137
|
|
|
{ |
138
|
2 |
|
foreach (['MERGE_MSG', 'MERGE_HEAD', 'MERGE_MODE'] as $fileName) { |
139
|
2 |
|
if (file_exists($this->dotGitDir . DIRECTORY_SEPARATOR . $fileName)) { |
140
|
2 |
|
return true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
1 |
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get index operator. |
148
|
|
|
* |
149
|
|
|
* @return \SebastianFeldmann\Git\Operator\Index |
150
|
|
|
*/ |
151
|
1 |
|
public function getIndexOperator() : Operator\Index |
152
|
|
|
{ |
153
|
1 |
|
return $this->getOperator('Index'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get info operator. |
158
|
|
|
* |
159
|
|
|
* @return \SebastianFeldmann\Git\Operator\Info |
160
|
|
|
*/ |
161
|
1 |
|
public function getInfoOperator() : Operator\Info |
162
|
|
|
{ |
163
|
1 |
|
return $this->getOperator('Info'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get log operator. |
168
|
|
|
* |
169
|
|
|
* @return \SebastianFeldmann\Git\Operator\Log |
170
|
|
|
*/ |
171
|
1 |
|
public function getLogOperator() : Operator\Log |
172
|
|
|
{ |
173
|
1 |
|
return $this->getOperator('Log'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get config operator. |
178
|
|
|
* |
179
|
|
|
* @return \SebastianFeldmann\Git\Operator\Config |
180
|
|
|
*/ |
181
|
1 |
|
public function getConfigOperator() : Operator\Config |
182
|
|
|
{ |
183
|
1 |
|
return $this->getOperator('Config'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Return requested operator. |
188
|
|
|
* |
189
|
|
|
* @param string $name |
190
|
|
|
* @return mixed |
191
|
|
|
*/ |
192
|
4 |
|
private function getOperator(string $name) |
193
|
|
|
{ |
194
|
4 |
|
if (!isset($this->operator[$name])) { |
195
|
4 |
|
$class = '\\SebastianFeldmann\\Git\\Operator\\' . $name; |
196
|
4 |
|
$this->operator[$name] = new $class($this->runner, $this); |
197
|
|
|
} |
198
|
4 |
|
return $this->operator[$name]; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|