Advertisement
Guest User

Untitled

a guest
Dec 18th, 2015
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. ###############################################################################
  2. #
  3. # PHP Syntax Check for Git pre-commit hook for Windows PowerShell
  4. #
  5. # Author: Vojtech Kusy <wojtha@gmail.com>, Kyle Wiering <kyle.wiering@yahoo.com>
  6. #
  7. ###############################################################################
  8.  
  9. ### INSTRUCTIONS ###
  10.  
  11. # Place the code to file "pre-commit" (no extension) and add it to the one of
  12. # the following locations:
  13. # 1) Repository hooks folder - C:\Path\To\Repository\.git\hooks
  14. # 2) User profile template - C:\Users\<USER>\.git\templates\hooks
  15. # 3) Global shared templates - C:\Program Files (x86)\Git\share\git-core\templates\hooks
  16. #
  17. # The hooks from user profile or from shared templates are copied from there
  18. # each time you create or clone new repository.
  19.  
  20. ### SETTINGS ###
  21.  
  22. # Path to the php.exe
  23. $php_exe = "C:\Program Files (x86)\Zend\ZendServer\bin\php.exe";
  24. # Extensions of the PHP files
  25. $php_ext = "php|engine|theme|install|inc|module|test"
  26. # Flag, if set to 1 git will unstage all files with errors, se to 0 to disable
  27. $unstage_on_error = 0;
  28.  
  29. ### FUNCTIONS ###
  30.  
  31. function php_syntax_check {
  32. param([string]$php_bin, [string]$extensions, [int]$reset)
  33.  
  34. $err_counter = 0;
  35.  
  36. write-host "Pre-commit PHP syntax check:" -foregroundcolor "white"
  37.  
  38. git diff-index --name-only --cached HEAD -- | foreach {
  39. if ($_ -match ".*\.($extensions)$") {
  40. $file = $matches[0];
  41. If (Test-Path $file){
  42. $errors = & $php_bin -d display_errors=On -l $file
  43. if ($errors -match "No syntax errors detected in $file") {
  44. write-host $file ": OK" -foregroundcolor "green"
  45. }
  46. else {
  47. write-host $file ":" $errors -foregroundcolor "red"
  48. if ($reset) {
  49. git reset -q HEAD $file
  50. write-host "Unstaging" $file "..." -foregroundcolor "magenta"
  51. }
  52. $err_counter++
  53. }
  54. }
  55. else {
  56. write-host $file ": OK" -foregroundcolor "green"
  57. }
  58. }
  59. }
  60.  
  61. if ($err_counter -gt 0) {
  62. exit 1
  63. }
  64. }
  65.  
  66.  
  67. php_syntax_check $php_exe $php_ext $unstage_on_error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement