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