File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change @@ -111,6 +111,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
111
111
<file baseinstalldir =" PHP/CodeSniffer" name =" ExactMatch.php" role =" php" />
112
112
<file baseinstalldir =" PHP/CodeSniffer" name =" Filter.php" role =" php" />
113
113
<file baseinstalldir =" PHP/CodeSniffer" name =" GitModified.php" role =" php" />
114
+ <file baseinstalldir =" PHP/CodeSniffer" name =" GitStaged.php" role =" php" />
114
115
</dir >
115
116
<dir name =" Generators" >
116
117
<file baseinstalldir =" PHP/CodeSniffer" name =" Generator.php" role =" php" />
Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * A filter to only include files that have been staged for commit in a Git repository.
4
+ *
5
+ * This filter is the ideal companion for your pre-commit git hook.
6
+ *
7
+ * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
8
+ * @copyright 2018 Juliette Reinders Folmer. All rights reserved.
9
+ * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
10
+ */
11
+
12
+ namespace PHP_CodeSniffer \Filters ;
13
+
14
+ use PHP_CodeSniffer \Util ;
15
+
16
+ class GitStaged extends ExactMatch
17
+ {
18
+
19
+
20
+ /**
21
+ * Get a list of blacklisted file paths.
22
+ *
23
+ * @return array
24
+ */
25
+ protected function getBlacklist ()
26
+ {
27
+ return [];
28
+
29
+ }//end getBlacklist()
30
+
31
+
32
+ /**
33
+ * Get a list of whitelisted file paths.
34
+ *
35
+ * @return array
36
+ */
37
+ protected function getWhitelist ()
38
+ {
39
+ $ modified = [];
40
+
41
+ $ cmd = 'git diff --cached --name-only -- ' .escapeshellarg ($ this ->basedir );
42
+ $ output = [];
43
+ exec ($ cmd , $ output );
44
+
45
+ $ basedir = $ this ->basedir ;
46
+ if (is_dir ($ basedir ) === false ) {
47
+ $ basedir = dirname ($ basedir );
48
+ }
49
+
50
+ foreach ($ output as $ path ) {
51
+ $ path = Util \Common::realpath ($ path );
52
+ if ($ path === false ) {
53
+ // Skip deleted files.
54
+ continue ;
55
+ }
56
+
57
+ do {
58
+ $ modified [$ path ] = true ;
59
+ $ path = dirname ($ path );
60
+ } while ($ path !== $ basedir );
61
+ }
62
+
63
+ return $ modified ;
64
+
65
+ }//end getWhitelist()
66
+
67
+
68
+ }//end class
You can’t perform that action at this time.
0 commit comments