批量拉取git最新代码

//~/auto_update.php
ini_set('date.timezone','PRC');

function checkNetwork(){
    $hostname = 'git.example.com';
    $ip = gethostbyname($hostname);
    if(!$ip || $ip === $hostname) {
        return false;
    } else {
        return true;
    }
}

function autoPull($dirName) {
    echo "================================\n";
    echo date('Y-m-d H:i:s'),"\t",$dirName,"\n";
    echo "================================\n";
    $res = shell_exec('git -C '.$dirName.' status');
    $lines = explode("\n", trim($res));

    $len = count($lines);
    $lastLine = $lines[$len-1];
    if($lines[0]=='On branch master') {

        if(strpos($lines[1] ,'Your branch is behind \'origin/master\'')===0){

            if($lastLine=='nothing to commit, working directory clean') {
                system('git -C '.$dirName.' fetch origin');
                system('git -C '.$dirName.' pull --no-commit --rebase origin master');
                system('git -C '.$dirName.' submodule update --init --recursive ');
            }
        }
    }
    echo "done\n\n\n";
}

function autoPullAll(){
    $baseDirs = array(
        '/Users/fyn/my_project/src',
        '/Users/fyn/my_project/libs',
    );
    foreach ($baseDirs as $baseDir) {
    
        $handler = opendir($baseDir);
        while($dirName = readdir($handler)) {
            if(strpos($dirName,'.')!==0){
                $dirName = $baseDir.'/'.$dirName;
                if(is_dir($dirName)) {
                    autoPull($dirName);
                }
            }
    
        }
    }
}
if(checkNetwork()) {
    $begin = time();
    echo "Begin: ", date('Y-m-d H:i:s', $begin),"\n";
    autoPullAll();
    $end = time();
    echo "End: ", date('Y-m-d H:i:s', $end),' (',$end-$begin,'s)',"\n";
} else {
    echo 'No network: ',date('Y-m-d H:i:s'),"\n";
}
#crontab -e
MAILTO=""
*/5 * * * * php ~/my_project/auto_update.php>~/my_project/auto_update.log

 

发表回复

您的电子邮箱地址不会被公开。