2010-03-29 68 views
8

我想檢查代碼提交到我的遠程git倉庫與PHP CodeSniffer並拒絕它,如果有任何問題的代碼標準。有沒有人有一個例子如何使用它在git遠程存儲庫或者例如如何使用它與預接收掛鉤?謝謝。Git預接收掛鉤啓動PHP CodeSniffer

回答

3

也許這點你在正確的方向:(原單從:http://www.squatlabs.de/versionierung/arbeiten-git-hooks德國)

#!/usr/bin/php 
<?php 

$output = array(); 
$rc  = 0; 
exec('git rev-parse --verify HEAD 2> /dev/null', $output, $rc); 
if ($rc == 0) $against = 'HEAD'; 
else   $against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; 

exec('git diff-index --cached --name-only '. $against, $output); 

$needle   = '/(\.php|\.module|\.install)$/'; 
$exit_status = 0; 

foreach ($output as $file) { 
     if (!preg_match($needle, $file)) { 
       // only check php files 
       continue; 
     } 

     $lint_output = array(); 
     $rc    = 0; 
     exec('php -l '. escapeshellarg($file), $lint_output, $rc); 
     if ($rc == 0) { 
       continue; 
     } 
     # echo implode("\n", $lint_output), "\n"; 
     $exit_status = 1; 
} 

exit($exit_status); 

你必須編輯EXEC線執行exec('PHP -l ...指向您codesniffer 。安裝

+0

遺憾的是它不與前收到鉤:( – Ralphz 2010-03-29 18:47:58

+0

NOP,你就必須努力創造在客戶端預先提交的,但你可以跟蹤所以每個開發者都必須拉鉤。AFAIK,你在推送時服務器無法測試內容... :( – FMaz008 2011-11-07 16:31:33

3

好,我找到了解決辦法:)

這是概念代碼:)預收到鉤證明:

#!/bin/bash 

while read old_sha1 new_sha1 refname; do 
    echo "ns: " $new_sha1; 
    echo "os: " $old_sha1; 

    echo "----" 

    git ls-tree -r $new_sha1 | cut -f 3 -d ' ' | cut -f 1 | while read file; do 
     git cat-file blob $file 
    done; 

    echo "----" 

done 

exit 1 

此示例代碼將只打印遠程存儲庫接收到的blob,但足以讓某人需要類似的東西(我希望)。

你可以把每一個斑點在任何你需要在這個文件中刪除文件等一些臨時文件,運行...

+0

謝謝,這對我有很大幫助 – 2012-04-11 13:24:46

2

我公司開發的前期得到git的鉤基於PHPCodeSniffer來檢查PHP,JavaScript和CSS文件的代碼樣式。

我的腳本可以從GitHub: https://github.com/blueicefield/PHP_CodeSniffer_GIT_Hook

+0

你可以用一個例子來擴展你的答案嗎?就像上面答案中的bash例子那樣,從單獨的答案中可以清楚地看到如何使用? – hakre 2012-06-10 15:04:52