MarkJane
6/26/2017 - 10:18 AM

Git 打包脚本, 过滤了不存在的文件

Git 打包脚本, 过滤了不存在的文件,文件太多时请使用 : 252 cat file_log.txt | xargs -d ' ' -L 10 -P 0 tar -czvf goms-pvg.tar.gz 253 cat file_log.txt | xargs -d ' ' -L 10 -P 0 tar -uvf goms-pvg.tar.gz 254 cat file_log.txt | xargs tar -uvf goms-pvg.tar.gz 255 cat file_log.txt | xargs tar -czuvf goms-pvg.tar.gz 256 cat file_log.txt | xargs tar -zuvf goms-pvg.tar.gz 257 cat file_log.txt | xargs tar -cuvf goms-pvg.tar.gz

4af8cae 201611031644 2016-11-03 17:43:00
f99e085 201710101527 2017-10-10 15:43:07
<?php
/**
 * Git 根据commit id 自动打更新包
 * @author MAGENJIE(magenjie@feeyo.com)
 * @datetime 2016-11-04T14:58:08+0800
 * xargs 文件太多输出会换行 导致 tar 识别有问题。
 */
ini_set('date.timezone','Asia/Shanghai');
$airport_iata = "chinaacdm";
echo $airport_iata.PHP_EOL;
echo "git pull origin master | please wait...".PHP_EOL;
exec("git pull origin master");
echo "git pull success!".PHP_EOL;
echo "git log --pretty=format:\"%h--%an--%ad--%s\" --date=format:%Y%m%d%H%M --since=15.days".PHP_EOL;
exec("git log --pretty=format:\"%h--%an--%ad--%s\" --date=format:%Y%m%d%H%M --since=15.days > git_log.txt");
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];
		}
		//过滤 Merge branch 'master' of 
		if(strpos($value[3],"Merge branch 'master' of ") === false){
			$text .= $value[3];
		}
		if ($value[0] === $old_commit_id) {
			break;
		}
	}
	if($old_commit_id === $new_commit_id){
		echo "nothing to commit!";return;
	}
	//$file_name = "goms-tsn-{$new_commit_date}.tar.gz";
	//$file_name = "goms-{$airport_iata}.tar.gz";
	$file_name = "chinaacdm.tar.gz";
	echo "Filename: {$file_name}".PHP_EOL;
	// 需要过滤掉不存在的文件
	echo "git diff {$old_commit_id} {$new_commit_id} --name-only > file_log.txt".PHP_EOL;
	exec("git diff {$old_commit_id} {$new_commit_id} --name-only > file_log.txt");
	$judge = judgeFileExist();
	echo "cat file_log.txt | xargs tar -czvf {$file_name}".PHP_EOL;
	exec("cat file_log.txt | xargs tar -czvf {$file_name}",$out,$var);
	//exec("git diff {$old_commit_id} {$new_commit_id} --name-only | xargs tar -czvf {$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_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",$airport_iata.PHP_EOL.$text.PHP_EOL.$file_name);
	echo "########################".PHP_EOL;
	//FTP 自动上传;
}else{
	echo "The command done failed ! log file not find!";
}

function judgeFileExist(){
	$i = 0;
	$file = fopen("file_log.txt", "r");
	$files = array();
	while( !feof($file) ){ //输出文本中所有的行,直到文件结束为止。
		$files[$i] = fgets($file);//fgets()函数从文件指针中读取一行
		$i++;
	}
	fclose($file);
	system("cat /dev/null > file_log.txt");
	$files = array_filter($files);
	foreach ($files as $file) {
		$file = trim($file);
		if (!empty($file) && file_exists($file)) {
			// 判断字符串中是否有空格;
			if (preg_match("/\s/", $file)) {
				file_put_contents("file_log.txt","'{$file}' ",FILE_APPEND);	
			}else{
				file_put_contents("file_log.txt","{$file} ",FILE_APPEND);	
			}
		}
	}
	return true;
}

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];
	}
}
?>