$return = array(0, "Git::create() executed successfully"); } return $return; }, 'Git::create([ $source ])' => function () { $return = null; $repo = Git::create(DIR . "/createfrom", DIR . "/test.git"); if (!Git::is_repo($repo)) { $return = array(2, "Git::create([ \$source ]) failed to produce expected output."); } else { $return = array(0, "Git::create([ \$source ]) executed successfully"); } return $return; }, 'Git::open()' => function () { $return = null; $repo = Git::open(DIR . "/test.git"); if (!Git::is_repo($repo)) { $return = array(2, "Git::open() failed to produce expected output."); } else { $return = array(0, "Git::open() executed successfully"); } return $return; }); class Git_TestSuiteControl { protected $warnings = 0; protected $errors = 0; public static function rm($dir) { if (!file_exists($dir)) { return true; }
function get_update($sname, $file_lock, $base_url, $path, $mail_lock, $remote_git = '', $remote_branch = '') { session_name($sname); session_start(); set_time_limit(300); ignore_user_abort(true); $stime = time(); $work = false; //检测、设置工作标志,存在session里(同一个session_name在一个页面未结束前会保持读写锁状态) if (empty($_SESSION['working'])) { $work = true; } if (file_exists($file_lock)) { $last_work_time = filemtime($file_lock); if ($last_work_time > 0 && time() - $last_work_time < 120) { //上次更新至今有120秒 echo "检测到正在进行工作中,本页面停止载入,请稍后再次访问。"; return false; //exit; } $work = true; unlink($file_lock); } elseif (empty($_SESSION['work_time']) || $stime - $_SESSION['work_time'] > LOCK_TIME) { //保证 LOCK_TIME 秒内只访问一次 $work = true; } if ($work && !file_exists($file_lock)) { $_SESSION['working'] = true; $_SESSION['work_time'] = $stime; file_put_contents($file_lock, $stime); } else { echo "距离上次获取请求时间间隔多短,请稍后再次访问。"; return false; //exit; } echo date("Y-m-d H:i:s") . " 读取列表中...<br>"; $list = get_list($base_url); //获取列表 if (count($list) < 2) { echo "读取Wiki列表失败,等待下次检测。<br>"; $_SESSION['work_time'] = $stime - LOCK_TIME; //取消检查时间,让检测可在稍后再次发起 unlink($file_lock); return false; } session_write_close(); //解除session,防止使其他访问页面一直等待session mk_dir($path); if (IS_WIN) { Git::windows_mode(); } if (!file_exists($path . '.git')) { echo date("Y-m-d H:i:s") . " 没有git库,尝试创建...<br>"; if ($remote_git) { //是否设置了远程仓库 echo "尝试从远程仓库克隆数据...<br>"; $ret = Git::clone_remote($path, $remote_git, $remote_branch); //从远程仓库clone(可指定分支) if (!Git::is_repo($ret) || !file_exists($path . '.git') || !$ret) { echo "从远程仓库克隆失败,本地创建...<br>"; $ret = Git::create($path); //如果clone失败,则本地创建 } } else { $ret = Git::create($path); } //直接本地创建 echo date("Y-m-d H:i:s") . " 创建结果:" . (Git::is_repo($ret) ? '成功' : '失败') . "<br>"; } $files = ls_file($path); foreach ($files as $file) { if (!is_dir($path . $file)) { unlink($path . $file); } } $tmp_arr = parse_url($base_url); $url_base = $tmp_arr['scheme'] . '://' . $tmp_arr['host']; write($path . 'list.txt', json($list)); //写出列表 echo date("Y-m-d H:i:s") . " 读取wiki列表完毕,开始读取内容页...<br>"; $count = 0; $content = get_content($base_url); write($path . 'index.html', $content); $count++; foreach ($list as $arr) { if (isset($arr['ul'])) { foreach ($arr['ul'] as $a) { if (substr($a['url'], 0, 1) == '.' || substr($a['url'], 0, 1) == '/') { //地址以.或/开头,则为wiki文档 $content = get_content((substr($a['url'], 0, 1) == '/' ? $url_base : (substr($a['url'], 0, 1) == '.' ? $url_base . $tmp_arr['path'] : '')) . $a['url']); if ($content) { write($path . $a['title'] . '.html', $content); $count++; } } } } } echo date("Y-m-d H:i:s") . " 获取操作完成,读取页面数量:" . $count . "<br>"; $repo = Git::open($path); $ret = $repo->status(true); $no_commit = preg_match('/nothing to commit, working directory clean/', $ret); if ($no_commit) { echo " 未检测到更新,共计用时:" . (time() - $stime) . "秒<br>"; } else { echo "待更新内容:<hr>" . $ret . "<hr>"; $ret0 = $repo->add(); $repo->run('config --global user.email "' . GIT_EMAIL . '"'); //git config --global user.email "*****@*****.**" $repo->run('config --global user.name "' . GIT_NAME . '"'); //git config --global user.name "Your Name" $repo->run('config --global core.quotepath false'); //配置git显示中文不转码 $ret = $repo->commit('check time: ' . date("Y-m-d H:i:s")); echo time() . " 已进行git提交,共计用时:" . (time() - $stime) . "秒<br><br>"; if ($remote_git) { $branch = $repo->active_branch(); echo "检测到远程仓库参数,提交到远程仓库...<br>"; $repo->run("remote add {$stime} " . $remote_git); //添加远程仓库 $repo->run("push -f {$stime} {$branch}:" . (empty($remote_branch) ? 'master' : "{$remote_branch}")); //强制覆盖远程仓库(可指定分支) $repo->run("remote remove {$stime}"); //删除远程仓库 } echo "提交git日志内容如下:<hr>" . nl2br(htmlspecialchars($ret)); $ret2 = $repo->run('log --stat -p -1'); echo "<hr>其他日志:<br>" . nl2br(htmlspecialchars($ret0)) . nl2br(htmlspecialchars($ret2)); unlink($mail_lock); } unlink($file_lock); return !$no_commit; }