PHP Git class

Source: Internet
Author: User
In my personal writing, I used PHP to execute GIT commands. The main purpose is to use git push hooks to access PHP. The push trigger automatic deployment can only implement the pull method. Can other methods be implemented in a similar way? Php *** ClassGit * @ authorJacky. Zhuo * @ time2015-11-01 * Git operation class classGit {private $ _ path; priva

In my personal writing, I used PHP to execute GIT commands. The main purpose is to use git push hooks to access PHP. The push trigger automatic deployment can only implement the pull method. Can other methods be implemented in a similar way? Php/*** Class Git * @ author Jacky. Zhuo * @ time 2015-11-01 * // Git operation class Git {private $ _ path; priva

Compiled by myself and executed GIT commands in PHP
The main purpose of writing is to use git's push hook to access this PHP to implement push trigger automatic deployment.

Only the pull method is implemented. Other methods can be implemented in a similar way.

 {'_'. $ Field }=$ $ field;} // set the path protocol if ($ isHttps) {$ this-> _ isHttps = true;} // set the project suffix if ($ suffix) {$ this-> _ suffix = $ suffix;} // set the log address if ($ log) {$ this-> _ log = $ log ;} else {$ this-> _ log = _ DIR __. '/git_log.txt';} // set the git command to return the record file address if ($ output) {$ this-> _ output = $ output ;} else {$ this-> _ output = _ DIR __. '/git_output.txt';} // set the git command parameter $ this-> _ descriptorspec = array (0 => array ('pipe', 'R '), 1 => array ('pipe', 'w'), 2 => array ('file', $ this-> _ output, 'A ')); // generate remote url $ this-> _ createUrl ();} /*** pull method ** from server pull to local ** @ param string $ branch default: 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 () {}/** generate git Address url ** git address format [https: //] [username]: [password] @ [host]/[owner]/[projectName]. [suffix] ** @ reutrn void */private function _ createUrl () {$ url = ''; // Add the Protocol if ($ this-> _ isHttps) {$ url. = 'https: // ';} else {$ url. = 'HTTP: // ';} // Add username and password $ url. = urlencode ($ this-> _ username ). ':'. urlencode ($ this-> _ password); // Add the address $ url. = '@'. $ this-> _ host. '/'; // Add the Project address $ url. = $ this-> _ owner. '/'. $ this-> _ projectName; // Add the project suffix $ url. = '. '. $ this-> _ suffix; $ this-> _ url = $ url ;} /** log method ** record access logs ** @ param string $ msg log * @ 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);}/** Run Command Line ** this method can run command line, the command line log is automatically recorded ** @ param string $ cmd the command line to run * @ 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 ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.