Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UGitRepository.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\utils\git;
4
5use Cz\Git\GitRepository;
6use Cz\Git\GitException;
8
9class UGitRepository extends GitRepository {
10
19 protected function run($cmd/*, $options = NULL*/){
20 $args = func_get_args ();
21 $cmd = $this->_processCommand ( $args );
22 exec ( $cmd . ' 2>&1', $output, $ret );
23
24 if ($ret !== 0) {
25 throw new GitException ( implode("<br>",$output), $ret );
26 }
27
28 return $this;
29 }
30
38 public static function init($directory, array $params = NULL){
39 if(is_dir("$directory/.git")){
40 throw new GitException("Repo already exists in $directory.");
41 }
42
43 if(!is_dir($directory) && !@mkdir($directory, 0777, TRUE)){
44 throw new GitException("Unable to create directory '$directory'.");
45 }
46
47 $cwd = getcwd();
48 chdir($directory);
49 exec(self::processCommand(array(
50 'git init',
51 $params,
52 $directory,
53 )), $output, $returnCode);
54
55 if($returnCode !== 0){
56 throw new GitException(implode("<br>", $output));
57 }
58
59 $repo = getcwd();
60 chdir($cwd);
61 return new static($repo);
62 }
63
64 protected function _processCommand(array $args) {
65 $cmd = array ();
66
67 $programName = array_shift ( $args );
68
69 foreach ( $args as $arg ) {
70 if (is_array ( $arg )) {
71 foreach ( $arg as $key => $value ) {
72 $_c = '';
73
74 if (is_string ( $key )) {
75 $_c = "$key ";
76 }
77 if (is_array ( $value )) {
78 foreach ( $value as $v ) {
79 $cmd [] = $_c . escapeshellarg ( $v );
80 }
81 } else {
82 $cmd [] = $_c . escapeshellarg ( $value );
83 }
84 }
85 } elseif (is_scalar ( $arg ) && ! is_bool ( $arg )) {
86 $cmd [] = escapeshellarg ( $arg );
87 }
88 }
89 return "$programName " . implode ( ' ', $cmd );
90 }
91
97 public function getUntrackedFiles() {
98 return $this->extractFromCommand ( 'git ls-files --others --exclude-standard', function ($value) {
99 return trim ( $value );
100 } );
101 }
102
108 public function getModifiedFiles() {
109 try {
110 return $this->extractFromCommand ( 'git diff --name-status HEAD', function ($array) {
111 $array = trim ( preg_replace ( '!\s+!', ' ', $array ) );
112 return explode ( ' ', $array );
113 } );
114 } catch ( \Cz\Git\GitException $e ) {
115 return [ ];
116 }
117 }
118
119 public function getChangesInFile($filename) {
120 try {
121 $output = $this->extractFromCommand ( 'git diff ' . $filename );
122 if (is_array ( $output ))
123 return implode ( '\r\n', $output );
124 return $output;
125 } catch ( \Cz\Git\GitException $e ) {
126 return "";
127 }
128 }
129
130 public function getChangesInCommit($commitHash) {
131 try {
132 $output = $this->extractFromCommand ( "git show {$commitHash}" );
133 if (is_array ( $output ))
134 return implode ( '\r\n', $output );
135 return $output;
136 } catch ( \Cz\Git\GitException $e ) {
137 return "";
138 }
139 }
140
146 public function getRemoteUrl() {
147 try {
148 $values = $this->extractFromCommand ( 'git config --get remote.origin.url', function ($str) {
149 return trim ( $str );
150 } );
151 if (isset ( $values )) {
152 return implode ( " ", $values );
153 }
154 } catch ( \Cz\Git\GitException $e ) {
155 return "";
156 }
157 return "";
158 }
159
168 public function ignoreFiles($files) {
169 if (! is_array ( $files )) {
170 $files = func_get_args ();
171 }
172 $this->begin ();
173 $this->run ( 'git reset', NULL, [ "--" => $files ] );
174 return $this->end ();
175 }
176
177 public function getCommits() {
178 $remoteBranchsSize=\count($this->getRemoteBranchs());
179 $nonPushed = $this->getNonPushedCommitHash ();
180 try {
181 return $this->extractFromCommand ( 'git log --pretty=format:"%h___%an___%ar___%s___%H"', function ($str) use ($nonPushed,$remoteBranchsSize) {
182 $array = \explode ( "___", $str );
183 $pushed = true;
184 if($remoteBranchsSize==0){
185 $pushed=false;
186 }else{
187 if (\is_array ( $nonPushed ))
188 $pushed = ! in_array ( $array [0], $nonPushed );
189 }
190 return new GitCommit ( $array [0], $array [1], $array [2], $array [3], $array [4], $pushed );
191 } );
192 } catch ( \Cz\Git\GitException $e ) {
193 return [ ];
194 }
195 return [ ];
196 }
197
198 public function getNonPushedCommitHash($branch="master") {
199 try {
200 return $this->extractFromCommand ( 'git log origin/'.$branch.'..'.$branch.' --pretty=format:"%h"' );
201 } catch ( \Cz\Git\GitException $e ) {
202 return [ ];
203 }
204 return [ ];
205 }
206
207 public function setUpstream($branch="master"){
208 $this->begin ();
209 $this->run ( 'git branch -u origin/'.$branch);
210 return $this->end ();
211 }
212
213 public function getRemoteBranchs(){
214 $url=$this->getRemoteUrl();
215 if(UString::isNotNull($url)){
216 try{
217 $result= $this->extractFromCommand('git ls-remote --heads '.$url);
218 if(!is_array($result)){
219 $result=[];
220 }
221 return $result;
222 } catch ( \Cz\Git\GitException $e ) {
223 return [ ];
224 }
225 }
226 return [];
227 }
228}
String utilities.
Definition UString.php:15
static init($directory, array $params=NULL)
Init repo in directory.
getModifiedFiles()
Returns list of modified files in repo.
getUntrackedFiles()
Returns list of untracked files in repo.
ignoreFiles($files)
Ignore file(s).
getRemoteUrl()
Returns the remote URL.