|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of CaptainHook. |
|
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\CaptainHook\Git\Resolver; |
|
11
|
|
|
|
|
12
|
|
|
class ChangedFiles |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* List of changed files. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
private $files; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Changed files by file type |
|
23
|
|
|
* |
|
24
|
|
|
* @var array[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $types = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Files sorted by suffix yet |
|
30
|
|
|
* |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
private $typesResolved = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get the lst of files that changed. |
|
37
|
|
|
* |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function getChangedFiles() |
|
41
|
|
|
{ |
|
42
|
4 |
|
if (null === $this->files) { |
|
43
|
4 |
|
$this->resolveFiles(); |
|
44
|
|
|
} |
|
45
|
4 |
|
return $this->files; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Where there files changed of a given type. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $suffix |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function hasChangedFilesOfType($suffix) |
|
55
|
|
|
{ |
|
56
|
2 |
|
return count($this->getChangedFilesOfType($suffix)) > 0; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Return list of changed files of a given type. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $suffix |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function getChangedFilesOfType($suffix) |
|
66
|
|
|
{ |
|
67
|
2 |
|
if (!$this->typesResolved) { |
|
68
|
2 |
|
$this->resolveFileTypes(); |
|
69
|
|
|
} |
|
70
|
2 |
|
return isset($this->types[$suffix]) ? $this->types[$suffix] : []; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Resolve the list of files that changed. |
|
75
|
|
|
*/ |
|
76
|
4 |
|
private function resolveFiles() |
|
77
|
|
|
{ |
|
78
|
4 |
|
$this->files = []; |
|
79
|
4 |
|
$headSHA1 = null; |
|
80
|
4 |
|
$return = null; |
|
81
|
|
|
// get current commit hash |
|
82
|
4 |
|
exec('git rev-parse --verify HEAD 2> /dev/null', $headSHA1, $return); |
|
83
|
|
|
|
|
84
|
4 |
|
if (0 === $return) { |
|
85
|
|
|
// get changed files |
|
86
|
4 |
|
exec("git diff-index --cached --name-status HEAD | egrep '^(A|M)' | awk '{print $2;}'", $this->files); |
|
87
|
|
|
} |
|
88
|
4 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Sort files by file suffix. |
|
92
|
|
|
*/ |
|
93
|
2 |
|
private function resolveFileTypes() |
|
94
|
|
|
{ |
|
95
|
2 |
|
foreach ($this->getChangedFiles() as $file) { |
|
96
|
1 |
|
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); |
|
97
|
1 |
|
$this->types[$ext][] = $file; |
|
98
|
|
|
} |
|
99
|
2 |
|
$this->typesResolved = true; |
|
100
|
2 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|