Skip to content

Instantly share code, notes, and snippets.

@stephenjtong
Forked from proofek/pre-receive
Last active April 29, 2016 08:17
Show Gist options
  • Save stephenjtong/6095a54c357de75ff665b4235483e4a5 to your computer and use it in GitHub Desktop.
Save stephenjtong/6095a54c357de75ff665b4235483e4a5 to your computer and use it in GitHub Desktop.
two git pre-receive for php
#!/usr/bin/php
<?php
echo "\nRunning php linter...\n";
$params = explode(' ', file_get_contents('php://stdin'));
$ref = trim($params[1]);
$diff = array();
$return = 0;
exec("git diff --name-only $params[0] $params[1] 2> /dev/null", $diff, $return);
if ($return > 0) {
echo "Could not run git diff\n\n";
exit(1);
}
$filename_pattern = '/\.php$/';
foreach ($diff as $file) {
if (!preg_match($filename_pattern, $file)) {
continue;
}
$tree = array();
$return = 0;
exec("git ls-tree $ref $file 2> /dev/null", $tree, $return);
if ($return > 0 || empty($tree)) {
echo "Could not run git ls-tree\n\n";
exit(1);
}
$tree = preg_split('/\s/', $tree[0]);
$fileContents = array();
exec("git cat-file $tree[1] $tree[2] 2> /dev/null", $fileContents, $return);
if ($return > 0) {
echo "Could not run git cat-file\n\n";
exit(1);
}
$fileContents = implode("\n", $fileContents);
$pipes = array();
$proc = proc_open('php -l',
array(0 => array('pipe', 'r'),
1 => array('pipe', 'w')),
$pipes);
if (!is_resource($proc)) {
echo "Could not creater php linter process\n\n";
exit(1);
}
fwrite($pipes[0], $fileContents);
fclose($pipes[0]);
fclose($pipes[1]);
$resultCode = proc_close($proc);
if ($resultCode != 0) {
echo "Error parsing file '$file'\n\n";
exit($resultCode);
}
}
echo "No errors detected\n\n";
exit(0);
#! /bin/bash
errors_buffer=""
working_dir=""
function php_syntax_check() {
local errors=$(php -l $file 2>&1 | grep "Parse error")
if [ "$errors" != "" ]; then
if [ "$errors_buffer" != "" ]; then
errors_buffer="$errors_buffer\n$errors"
else
errors_buffer="$errors"
fi
echo "Syntax errors found in file: $file "
fi
}
function keywords_check(){
grep -P '\bdd\b' "$file" &>/dev/null
if [ $? -ne 1 ]; then
local errors="Found 'dd' in $file"
if [ "$errors_buffer" != "" ]; then
errors_buffer="$errors_buffer\n$errors"
else
errors_buffer="$errors"
fi
fi
grep -P '\bdie\b' "$file" &>/dev/null
if [ $? -ne 1 ]; then
local errors="Found 'die' in $file"
if [ "$errors_buffer" != "" ]; then
errors_buffer="$errors_buffer\n$errors"
else
errors_buffer="$errors"
fi
fi
grep -P '\bexit\b' "$file" &>/dev/null
if [ $? -ne 1 ]; then
local errors="Found 'exit' in $file"
if [ "$errors_buffer" != "" ]; then
errors_buffer="$errors_buffer\n$errors"
else
errors_buffer="$errors"
fi
fi
grep -P '\bdump\b' "$file" &>/dev/null
if [ $? -ne 1 ]; then
local errors="Found 'dump' in $file"
if [ "$errors_buffer" != "" ]; then
errors_buffer="$errors_buffer\n$errors"
else
errors_buffer="$errors"
fi
fi
grep -P '\bprint_r\b' "$file" &>/dev/null
if [ $? -ne 1 ]; then
local errors="Found 'print_r' in $file"
if [ "$errors_buffer" != "" ]; then
errors_buffer="$errors_buffer\n$errors"
else
errors_buffer="$errors"
fi
fi
grep -P '\bvar_dump\b' "$file" &>/dev/null
if [ $? -ne 1 ]; then
local errors="Found 'var_dump' in $file"
if [ "$errors_buffer" != "" ]; then
errors_buffer="$errors_buffer\n$errors"
else
errors_buffer="$errors"
fi
fi
}
function bom_check() {
if [ "`head -c 3 -- "$file"`" == $'\xef\xbb\xbf' ]; then
local errors="Found BOM in $file"
if [ "$errors_buffer" != "" ]; then
errors_buffer="$errors_buffer\n$errors"
else
errors_buffer="$errors"
fi
fi
}
function line_ending_check() {
grep -Hnq $'\r' "$file"; RET=$?
if [ $RET -ne 1 ]; then
errors="Windows/Mac newlines found in $file"
if [ "$errors_buffer" != "" ]; then
errors_buffer="$errors_buffer\n$errors"
else
errors_buffer="$errors"
fi
fi
}
while read old_rev new_rev ref_name
do
zero="0000000000000000000000000000000000000000"
if [ "$old_rev" = "$zero" ]; then
# Created new branch
list=$(git diff-tree -r ${new_rev} | awk '{if ($5 != "D") print $6}')
elif [ "$new_rev" = "$zero" ]; then
# Deleted branch
exit 0
else
list=$(git diff-tree -r ${old_rev}..${new_rev} | awk '{if ($5 != "D") print $6}')
fi
#echo "DEBUG \$list: $list"
# if commit not contain php-files -exit
if [ -z "$list" ]; then
exit 0
fi
working_dir="/tmp/_git_$newrev/"
mkdir -p $working_dir
echo $list |\
xargs git archive --format=tar $new_rev|\
(cd $working_dir && tar xf -)
cd $working_dir
for file in $list
do
echo $file
# Check php syntax
extension=$(echo "$file" | grep ".php$")
if [ "$extension" != "" ]; then
keywords_check $file
php_syntax_check $file
fi
extension=$(echo "$file" | grep -E "(java|js|php|ini|xml|yml|htm|html|tpl|sh|bat|conf|less)$")
if [ "$extension" != "" ]; then
# Check BOM
bom_check $file
for file in $list
do
echo $file
# Check php syntax
extension=$(echo "$file" | grep ".php$")
if [ "$extension" != "" ]; then
#keywords_check $file
php_syntax_check $file
fi
extension=$(echo "$file" | grep -E "(java|js|php|ini|xml|yml|htm|html|tpl|sh|bat|conf|less)$")
if [ "$extension" != "" ]; then
# Check BOM
bom_check $file
# Check line endings
line_ending_check $file
fi
done
cd - > /dev/null
rm -rf $working_dir
if [ "$errors_buffer" != "" ]; then
echo
echo "These errors were found in try-to-push files: "
echo -e $errors_buffer
echo
echo "Can't push, please fix errors first."
exit 1
else
echo "Pushed successfully."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment