鍍金池/ 問答/PHP  Linux  網(wǎng)絡(luò)安全/ git怎么實(shí)現(xiàn)過濾文件內(nèi)容里的關(guān)鍵字,不允許提交?

git怎么實(shí)現(xiàn)過濾文件內(nèi)容里的關(guān)鍵字,不允許提交?

比如PHP代碼里的join,foreach里的sql?

回答
編輯回答
任她鬧

換個(gè)思路通過忽略文件間接實(shí)現(xiàn):?jiǎn)为?dú)弄個(gè)文件,把想忽略的內(nèi)容放到里面,使用時(shí)代碼中引入文件內(nèi)容,gitignore 這個(gè)文件。

2017年6月9日 09:57
編輯回答
毀憶

githook

2017年10月14日 11:18
編輯回答
陌南塵

用別的方法實(shí)現(xiàn)了,雖然有點(diǎn)2.。。

//入口文件引入代碼審查文件
if (is_file('./check_code.php')) {
    require_once('./check_code.php');
}

check_code.php

<?php
$keyword_arr = array('join', 'JOIN');
$allow = 255; //目錄中目前存在的join
foreach ($keyword_arr as $value) {
    $cmd = 'grep -r --exclude-dir=Runtime '.$value.' * | wc -l';
    $r = execInBackground($cmd);
    if($r>$allow){
        exit('代碼里包含的join查過額定數(shù)量'.$allow.',現(xiàn)為'.$r);
    }
}

function execInBackground($cmd) {
    $sum = 11;
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        $r = pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        $r = exec($cmd . " > /dev/null &");   
    }
    return $r;
} 
?>
2018年9月4日 22:22
編輯回答
獨(dú)白

項(xiàng)目?jī)?nèi)部的.git/hooks里面有很多git-hook的sample shell腳本。

通過判斷commit的內(nèi)容里有無相關(guān)禁詞,來做是否提交。

#!/bin/bash

if [ #有關(guān)鍵詞 ]; then
    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    echo ""
    echo "禁止使用XXXX關(guān)鍵詞代碼!"
    echo ""
    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    exit 1
fi

當(dāng)然,判斷commit內(nèi)容會(huì)比較復(fù)雜,需要先拿到commit內(nèi)容,再判斷。

2018年7月9日 14:01