个人编写, 用php执行git命令
主要编写目地是利用git的push钩子访问该php能实现push触发自动部署
只实现了pull方法, 其余方法可以以类似方法实现
{'_' . $field} = $$field; } //设置路径协议 if($ishttps) { $this->_ishttps = true; } //设置项目后缀 if($suffix) { $this->_suffix = $suffix; } //设置log地址 if($log) { $this->_log = $log; } else { $this->_log = __dir__ . '/git_log.txt'; } //设置git命令返回记录文件地址 if($output) { $this->_output = $output; } else { $this->_output = __dir__ . '/git_output.txt'; } //设置git命令参数 $this->_descriptorspec = array( 0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('file', $this->_output, 'a') ); //生成远程url $this->_createurl(); } /** * pull方法 * * 从服务器中pull到本地 * * @param string $branch 默认为 origin master * @return boolen */ public function pull($branch = 'origin master') { $cmd = 'git pull ' . $this->_url . ' ' . $branch; return $this->_runcmd($cmd); } public function fetch() { } public function push() { } public function gclone() { } /* * 生成git地址url * * git地址格式 [https://][username]:[password]@[host]/[owner]/[projectname].[suffix] * * @reutrn void */ private function _createurl() { $url = ''; //添加协议 if($this->_ishttps) { $url .= 'https://'; } else { $url .= 'http://'; } //添加用户名密码 $url .= urlencode($this->_username) . ':' . urlencode($this->_password); //添加地址 $url .= '@' . $this->_host . '/'; //添加项目地址 $url .= $this->_owner . '/' . $this->_projectname; //添加项目后缀 $url .= '.' . $this->_suffix; $this->_url = $url; } /* * log方法 * * 记录访问日志 * * @param string $msg 日志 * @return void */ private function _writelog($msg) { $log = date('y-m-d h:i:s') . :\n; $log .= $msg; $log .= -------------------------------------\n; file_put_contents($this->_log, $log, file_append | lock_ex); } /* * 运行命令行 * * 该方法可以运行命令行, 并会自动记录命令行日志 * * @param string $cmd 要运行的命令行 * @return boolen */ private function _runcmd($cmd) { file_put_contents($this->_output, date('y-m-d h:i:s') . :\n, file_append); $process = proc_open($cmd, $this->_descriptorspec, $pipes, $this->_path); if(is_resource($process)) { $output = stream_get_contents($pipes[1]); fclose($pipes[1]); } else { $output = 'no command'; } $returnvalue = proc_close($process); file_put_contents($this->_output, ---------------------------------\n, file_append); if($returnvalue) { $this->_writelog('command faild.' . \n); return false; } else { $this->_writelog('command success: ' . $output); return true; } }}try { $git = new git(__dir__, 'project', 'owner', 'username', 'password', 'host', 'suffix', true);} catch(exception $e) { echo $e->getmessage(); die;}$git->pull('test');
