Skip to content

Commit f9e8294

Browse files
author
Radu Murzea
committed
Added v1.0 of pre-commit hook
0 parents  commit f9e8294

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

pre-commit

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/sh
2+
3+
# Copyright (c) 2014 Radu Murzea.
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
#
23+
# -----------------------------------------------------------------------------
24+
#
25+
# This script will scan the entire list of changed files and check each one if it
26+
# conforms with the specified rule-set. This is checked with the help of PHP CodeSniffer
27+
#
28+
# Make sure you have "phpcs" in your PATH/CLASSPATH variable so that the system will know where to get it from
29+
#
30+
# Author: Radu Murzea
31+
# Date: 02 June 2014
32+
# Version: 1.0
33+
34+
# let's say our repository working directory is called repo
35+
# but we're currently in repo/.git/hooks, so we need to calculate the current directory's grandparent
36+
37+
# right now, we're in /repo/.git/hooks, so let's get that path
38+
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
39+
40+
# let's calculate the parent directory, which is: /repo/.git
41+
PARENT_DIR="$(dirname "$CURRENT_DIR")"
42+
43+
# calculate grandparent directory (which is what we want): /repo/
44+
REPO_ROOT="$(dirname "$PARENT_DIR")"
45+
46+
# this is the reason we need to obtain the path
47+
# to the "repo" directory: because "git diff" has to be executed on it, it won't work otherwise.
48+
for RELATIVE_PATH in `cd "$REPO_ROOT";git diff --name-only HEAD`; do
49+
50+
# calculate the full path of each modified file
51+
FULL_FILE_PATH="$REPO_ROOT/$RELATIVE_PATH"
52+
53+
# check with PHP CodeSniffer if the file is OK
54+
# edit the path to the rule-set if it differs on your machine
55+
# execution will stop when a file is found that breaks the specified rule-set
56+
exec phpcs -np --standard=/D/xampp/php/pear/PHP/CodeSniffer/Standards/PSR2/ruleset.xml "$FULL_FILE_PATH"
57+
done

0 commit comments

Comments
 (0)