1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* GitElephant - An abstraction layer for git written in PHP |
5
|
|
|
* Copyright (C) 2013 Matteo Giachino |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program. If not, see [http://www.gnu.org/licenses/]. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace GitElephant\Status; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class StatusFile |
25
|
|
|
* |
26
|
|
|
* @package GitElephant\Status |
27
|
|
|
*/ |
28
|
|
|
class StatusFile |
29
|
|
|
{ |
30
|
|
|
public const UNTRACKED = '?'; |
31
|
|
|
public const IGNORED = '!'; |
32
|
|
|
public const UNMODIFIED = ''; |
33
|
|
|
public const MODIFIED = 'M'; |
34
|
|
|
public const ADDED = 'A'; |
35
|
|
|
public const DELETED = 'D'; |
36
|
|
|
public const RENAMED = 'R'; |
37
|
|
|
public const COPIED = 'C'; |
38
|
|
|
public const UPDATED_BUT_UNMERGED = 'U'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $x; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $y; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $name; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
private $renamed; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
private $type; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $description; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $x X section of the status --porcelain output |
72
|
|
|
* @param string $y Y section of the status --porcelain output |
73
|
|
|
* @param string $name file name |
74
|
|
|
* @param string $renamed new file name (if renamed) |
75
|
|
|
*/ |
76
|
9 |
|
private function __construct(string $x, string $y, string $name, string $renamed = null) |
77
|
|
|
{ |
78
|
9 |
|
$this->x = ' ' === $x ? null : $x; |
79
|
9 |
|
$this->y = ' ' === $y ? null : $y; |
80
|
9 |
|
$this->name = $name; |
81
|
9 |
|
$this->renamed = $renamed; |
82
|
9 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $x X section of the status --porcelain output |
86
|
|
|
* @param string $y Y section of the status --porcelain output |
87
|
|
|
* @param string $name file name |
88
|
|
|
* @param string $renamed new file name (if renamed) |
89
|
|
|
* |
90
|
|
|
* @return StatusFile |
91
|
|
|
*/ |
92
|
9 |
|
public static function create( |
93
|
|
|
string $x, |
94
|
|
|
string $y, |
95
|
|
|
string $name, |
96
|
|
|
string $renamed = null |
97
|
|
|
): \GitElephant\Status\StatusFile { |
98
|
9 |
|
return new self($x, $y, $name, $renamed); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
5 |
|
public function isRenamed(): bool |
105
|
|
|
{ |
106
|
5 |
|
return $this->renamed !== null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the file name |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
1 |
|
public function getName(): string |
115
|
|
|
{ |
116
|
1 |
|
return $this->name; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the renamed |
121
|
|
|
* |
122
|
|
|
* @return string|null |
123
|
|
|
*/ |
124
|
5 |
|
public function getRenamed(): ?string |
125
|
|
|
{ |
126
|
5 |
|
return $this->renamed; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the status of the index |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
6 |
|
public function getIndexStatus(): ?string |
135
|
|
|
{ |
136
|
6 |
|
return $this->x; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the status of the working tree |
141
|
|
|
* |
142
|
|
|
* @return string|null |
143
|
|
|
*/ |
144
|
1 |
|
public function getWorkingTreeStatus(): ?string |
145
|
|
|
{ |
146
|
1 |
|
return $this->y; |
147
|
|
|
} |
148
|
1 |
|
|
149
|
|
|
/** |
150
|
|
|
* description of the status |
151
|
|
|
* |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function calculateDescription(): void |
155
|
|
|
{ |
156
|
|
|
$status = $this->x . $this->y; |
157
|
|
|
$matching = [ |
158
|
|
|
'/ [MD]/' => 'not updated', |
159
|
|
|
'/M[MD]/' => 'updated in index', |
160
|
|
|
'/A[MD]/' => 'added to index', |
161
|
|
|
'/D[M]/' => 'deleted from index', |
162
|
|
|
'/R[MD]/' => 'renamed in index', |
163
|
|
|
'/C[MD]/' => 'copied in index', |
164
|
|
|
'/[MARC] /' => 'index and work tree matches', |
165
|
|
|
'/[MARC]M/' => 'work tree changed since index', |
166
|
|
|
'/[MARC]D/' => 'deleted in work tree', |
167
|
1 |
|
'/DD/' => 'unmerged, both deleted', |
168
|
1 |
|
'/AU/' => 'unmerged, added by us', |
169
|
1 |
|
'/UD/' => 'unmerged, deleted by them', |
170
|
1 |
|
'/UA/' => 'unmerged, added by them', |
171
|
|
|
'/DU/' => 'unmerged, deleted by us', |
172
|
|
|
'/AA/' => 'unmerged, both added', |
173
|
|
|
'/UU/' => 'unmerged, both modified', |
174
|
1 |
|
'/\?\?/' => 'untracked', |
175
|
1 |
|
'/!!/' => 'ignored', |
176
|
|
|
]; |
177
|
|
|
$out = []; |
178
|
|
|
foreach ($matching as $pattern => $label) { |
179
|
|
|
if (preg_match($pattern, $status)) { |
180
|
|
|
$out[] = $label; |
181
|
|
|
} |
182
|
1 |
|
} |
183
|
|
|
|
184
|
1 |
|
$this->description = implode(', ', $out); |
185
|
1 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set Description |
189
|
|
|
* |
190
|
|
|
* @param string $description the description variable |
191
|
|
|
*/ |
192
|
|
|
public function setDescription(string $description): void |
193
|
|
|
{ |
194
|
|
|
$this->description = $description; |
195
|
|
|
} |
196
|
|
|
|
197
|
2 |
|
/** |
198
|
|
|
* Get Description. |
199
|
2 |
|
* Note that in certain environments, git might |
200
|
1 |
|
* format the output differently, leading to the description |
201
|
|
|
* being an empty string. Use setDescription(string) to set it yourself. |
202
|
|
|
* |
203
|
2 |
|
* @see #calulcateDescription() |
204
|
|
|
* @see #setDescription($description) |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function getDescription(): string |
208
|
|
|
{ |
209
|
|
|
if ($this->description === null) { |
210
|
|
|
$this->calculateDescription(); |
211
|
1 |
|
} |
212
|
|
|
|
213
|
1 |
|
return $this->description; |
214
|
1 |
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Set Type |
218
|
|
|
* |
219
|
|
|
* @param string $type the type variable |
220
|
|
|
*/ |
221
|
|
|
public function setType(string $type): void |
222
|
1 |
|
{ |
223
|
|
|
$this->type = $type; |
224
|
1 |
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get the Type of status/change. |
228
|
|
|
* Please note that this type might not be set by default. |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function getType(): ?string |
233
|
|
|
{ |
234
|
|
|
return $this->type; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|