19 protected function run($cmd){
20 $args = func_get_args ();
22 exec ( $cmd .
' 2>&1', $output, $ret );
25 throw new GitException ( implode(
"<br>",$output), $ret );
38 public static function init($directory, array $params = NULL){
39 if(is_dir(
"$directory/.git")){
40 throw new GitException(
"Repo already exists in $directory.");
43 if(!is_dir($directory) && !@mkdir($directory, 0777, TRUE)){
44 throw new GitException(
"Unable to create directory '$directory'.");
49 exec(self::processCommand(array(
53 )), $output, $returnCode);
55 if($returnCode !== 0){
56 throw new GitException(implode(
"<br>", $output));
61 return new static($repo);
67 $programName = array_shift ( $args );
69 foreach ( $args as $arg ) {
70 if (is_array ( $arg )) {
71 foreach ( $arg as $key => $value ) {
74 if (is_string ( $key )) {
77 if (is_array ( $value )) {
78 foreach ( $value as $v ) {
79 $cmd [] = $_c . escapeshellarg ( $v );
82 $cmd [] = $_c . escapeshellarg ( $value );
85 } elseif (is_scalar ( $arg ) && ! is_bool ( $arg )) {
86 $cmd [] = escapeshellarg ( $arg );
89 return "$programName " . implode (
' ', $cmd );
98 return $this->extractFromCommand (
'git ls-files --others --exclude-standard',
function ($value) {
99 return trim ( $value );
110 return $this->extractFromCommand (
'git diff --name-status HEAD',
function ($array) {
111 $array = trim ( preg_replace (
'!\s+!',
' ', $array ) );
112 return explode (
' ', $array );
114 }
catch ( \Cz\Git\GitException $e ) {
121 $output = $this->extractFromCommand (
'git diff ' . $filename );
122 if (is_array ( $output ))
123 return implode (
'\r\n', $output );
125 }
catch ( \Cz\Git\GitException $e ) {
132 $output = $this->extractFromCommand (
"git show {$commitHash}" );
133 if (is_array ( $output ))
134 return implode (
'\r\n', $output );
136 }
catch ( \Cz\Git\GitException $e ) {
148 $values = $this->extractFromCommand (
'git config --get remote.origin.url',
function ($str) {
149 return trim ( $str );
151 if (isset ( $values )) {
152 return implode (
" ", $values );
154 }
catch ( \Cz\Git\GitException $e ) {
169 if (! is_array ( $files )) {
170 $files = func_get_args ();
173 $this->
run (
'git reset', NULL, [
"--" => $files ] );
174 return $this->end ();
181 return $this->extractFromCommand (
'git log --pretty=format:"%h___%an___%ar___%s___%H"',
function ($str) use ($nonPushed,$remoteBranchsSize) {
182 $array = explode (
"___", $str );
184 if($remoteBranchsSize==0){
187 if (is_array ( $nonPushed ))
188 $pushed = ! in_array ( $array [0], $nonPushed );
190 return new GitCommit ( $array [0], $array [1], $array [2], $array [3], $array [4], $pushed );
192 }
catch ( \Cz\Git\GitException $e ) {
200 return $this->extractFromCommand (
'git log origin/'.$branch.
'..'.$branch.
' --pretty=format:"%h"' );
201 }
catch ( \Cz\Git\GitException $e ) {
209 $this->
run (
'git branch -u origin/'.$branch);
210 return $this->end ();
217 $result= $this->extractFromCommand(
'git ls-remote --heads '.$url);
218 if(!is_array($result)){
222 }
catch ( \Cz\Git\GitException $e ) {
static init($directory, array $params=NULL)
Init repo in directory.
getRemoteUrl()
Returns the remote URL.
setUpstream($branch="master")
getChangesInFile($filename)
getUntrackedFiles()
Returns list of untracked files in repo.
getModifiedFiles()
Returns list of modified files in repo.
getNonPushedCommitHash($branch="master")
_processCommand(array $args)
ignoreFiles($files)
Ignore file(s).
getChangesInCommit($commitHash)