<?php
/**
* Git 根据commit id 自动打更新包
* @author MAGENJIE(magenjie@feeyo.com)
* @datetime 2016-11-04T14:58:08+0800
*/
ini_set('date.timezone','Asia/Shanghai');
echo "git pull origin master | please wait...".PHP_EOL;
exec("git pull origin master").PHP_EOL;
echo "git pull success!".PHP_EOL;
echo "git log --pretty=format:\"%h--%an--%ad--%s\" --date=format:%Y%m%d%H%M --since=2.days".PHP_EOL;
exec("git log --pretty=format:\"%h--%an--%ad--%s\" --date=format:%Y%m%d%H%M --since=2.days > git_log.txt").PHP_EOL;
if (file_exists("git_log.txt")) {
$log = readGitLog();
if (!file_exists("last_commit_id.txt")) {
echo "last_commit_id.txt not exists,please check it!";return;
}
$old_commit_id = readLastCommitId();
if ($old_commit_id === FALSE) {
echo "last_commit_id not exists!";return;
}
echo "last_commit_id: {$old_commit_id}".PHP_EOL;
$text = "";
foreach ($log as $key => $value) {
if ($key === 0) {
$new_commit_id = $value[0];
$new_commit_date = $value[2];
}
$text .= $value[3];
if ($value[0] === $old_commit_id) {
break;
}
}
$file_name = "goms-tsn-{$new_commit_date}.tar.gz";
echo "Filename: {$file_name}".PHP_EOL;
exec("git diff {$old_commit_id} {$new_commit_id} --name-only | xargs tar -czvf patches/{$file_name}",$out,$var);
echo "new_commit_id: {$new_commit_id}".PHP_EOL;
echo "########################".PHP_EOL;
if ($var) {
echo "#########Failed#########".PHP_EOL;
}else{
echo "########Success!########".PHP_EOL;
exec("rm -rf git_log.txt");
file_put_contents("last_commit_id.txt",$new_commit_id." ".$new_commit_date." ".date("Y-m-d H:i:s").PHP_EOL,FILE_APPEND);
file_put_contents("email.txt",$text.PHP_EOL.$file_name);
}
echo "########################".PHP_EOL;
}else{
echo "The command done failed ! log file not find!";
}
function readGitLog(){
$file = fopen("git_log.txt", "r");
$log = array();
$i = 0;
while(!feof($file)){
$content = fgets($file);//fgets()函数从文件指针中读取一行
if (empty($content)) {
continue;
}
$log[$i] = explode("--",$content);
$i++;
}
fclose($file);
return $log;
}
function readLastCommitId(){
$fp = fopen("last_commit_id.txt", 'r');
fseek($fp, -1, SEEK_END);
$s = '';
while(($c = fgetc($fp)) !== false) {
if($c == "\n" && $s) break;
$s = $c . $s;
fseek($fp, -2, SEEK_CUR);
}
fclose($fp);
if (empty($s)) {
return FALSE;
}else{
return explode(" ",$s)[0];
}
}
?>