|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of CaptainHook. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace SebastianFeldmann\Git\Operator; |
|
13
|
|
|
|
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
use SebastianFeldmann\Git\Command\DiffIndex\GetStagedFiles; |
|
16
|
|
|
use SebastianFeldmann\Git\Command\DiffIndex\GetStagedFiles\FilterByStatus; |
|
17
|
|
|
use SebastianFeldmann\Git\Command\RevParse\GetCommitHash; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Index CommitMessage |
|
21
|
|
|
* |
|
22
|
|
|
* @package SebastianFeldmann\Git |
|
23
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
24
|
|
|
* @link https://github.com/sebastianfeldmann/git |
|
25
|
|
|
* @since Class available since Release 0.9.0 |
|
26
|
|
|
*/ |
|
27
|
|
|
class Index extends Base |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* List of changed files. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $files; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Changed files by file type |
|
38
|
|
|
* |
|
39
|
|
|
* @var array[] |
|
40
|
|
|
*/ |
|
41
|
|
|
private $types = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Files sorted by suffix yet |
|
45
|
|
|
* |
|
46
|
|
|
* @var bool |
|
47
|
|
|
*/ |
|
48
|
|
|
private $typesResolved = false; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get the lst of files that changed. |
|
52
|
|
|
* |
|
53
|
4 |
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
4 |
|
public function getStagedFiles(): array |
|
56
|
4 |
|
{ |
|
57
|
|
|
if (null === $this->files) { |
|
58
|
4 |
|
$this->resolveFiles(); |
|
59
|
|
|
} |
|
60
|
|
|
return $this->files; |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Where there files changed of a given type. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $suffix |
|
67
|
1 |
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function hasStagedFilesOfType($suffix): bool |
|
70
|
|
|
{ |
|
71
|
|
|
return count($this->getStagedFilesOfType($suffix)) > 0; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return list of changed files of a given type. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $suffix |
|
78
|
2 |
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public function getStagedFilesOfType($suffix): array |
|
81
|
2 |
|
{ |
|
82
|
|
|
if (!$this->typesResolved) { |
|
83
|
2 |
|
$this->resolveFileTypes(); |
|
84
|
|
|
} |
|
85
|
|
|
return isset($this->types[$suffix]) ? $this->types[$suffix] : []; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
4 |
|
* Resolve the list of files that changed. |
|
90
|
|
|
*/ |
|
91
|
4 |
|
private function resolveFiles() |
|
92
|
|
|
{ |
|
93
|
4 |
|
$this->files = []; |
|
94
|
3 |
|
|
|
95
|
3 |
|
if ($this->isHeadValid()) { |
|
96
|
3 |
|
$cmd = new GetStagedFiles($this->repo->getRoot()); |
|
97
|
3 |
|
$formatter = new FilterByStatus(['A', 'M']); |
|
98
|
|
|
$result = $this->runner->run($cmd, $formatter); |
|
99
|
4 |
|
$this->files = $result->getFormattedOutput(); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
2 |
|
* Sort files by file suffix. |
|
105
|
|
|
*/ |
|
106
|
2 |
|
private function resolveFileTypes() |
|
107
|
2 |
|
{ |
|
108
|
2 |
|
foreach ($this->getStagedFiles() as $file) { |
|
109
|
|
|
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); |
|
110
|
2 |
|
$this->types[$ext][] = $file; |
|
111
|
2 |
|
} |
|
112
|
|
|
$this->typesResolved = true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Check head validity. |
|
117
|
|
|
* |
|
118
|
4 |
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
|
|
private function isHeadValid(): bool |
|
121
|
4 |
|
{ |
|
122
|
4 |
|
try { |
|
123
|
3 |
|
$cmd = new GetCommitHash($this->repo->getRoot()); |
|
124
|
1 |
|
$result = $this->runner->run($cmd); |
|
125
|
1 |
|
return $result->isSuccessful(); |
|
126
|
|
|
} catch (RuntimeException $e) { |
|
127
|
|
|
return false; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|