|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\git; |
|
4
|
|
|
|
|
5
|
|
|
use Cz\Git\GitRepository; |
|
6
|
|
|
use Cz\Git\GitException; |
|
7
|
|
|
|
|
8
|
|
|
class UGitRepository extends GitRepository { |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Runs command. |
|
12
|
|
|
* @param string|array |
|
13
|
|
|
* @return self |
|
14
|
|
|
* @throws Cz\Git\GitException |
|
15
|
|
|
*/ |
|
16
|
|
|
protected function run($cmd/*, $options = NULL*/){ |
|
|
|
|
|
|
17
|
|
|
$args = func_get_args(); |
|
18
|
|
|
$cmd = $this->_processCommand($args); |
|
19
|
|
|
exec($cmd . ' 2>&1', $output, $ret); |
|
20
|
|
|
|
|
21
|
|
|
if($ret !== 0) |
|
22
|
|
|
{ |
|
23
|
|
|
throw new GitException("Command '$cmd' failed (exit-code $ret).", $ret); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return $this; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function _processCommand(array $args) |
|
30
|
|
|
{ |
|
31
|
|
|
$cmd = array(); |
|
32
|
|
|
|
|
33
|
|
|
$programName = array_shift($args); |
|
34
|
|
|
|
|
35
|
|
|
foreach($args as $arg) |
|
36
|
|
|
{ |
|
37
|
|
|
if(is_array($arg)) |
|
38
|
|
|
{ |
|
39
|
|
|
foreach($arg as $key => $value) |
|
40
|
|
|
{ |
|
41
|
|
|
$_c = ''; |
|
42
|
|
|
|
|
43
|
|
|
if(is_string($key)){ |
|
44
|
|
|
$_c = "$key "; |
|
45
|
|
|
} |
|
46
|
|
|
if(is_array($value)){ |
|
47
|
|
|
foreach ($value as $v){ |
|
48
|
|
|
$cmd[] = $_c . escapeshellarg($v); |
|
49
|
|
|
} |
|
50
|
|
|
}else{ |
|
51
|
|
|
$cmd[] = $_c . escapeshellarg($value); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
elseif(is_scalar($arg) && !is_bool($arg)) |
|
56
|
|
|
{ |
|
57
|
|
|
$cmd[] = escapeshellarg($arg); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
return "$programName " . implode(' ', $cmd); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns list of untracked files in repo. |
|
65
|
|
|
* @return string[]|NULL NULL => no files untracked |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getUntrackedFiles(){ |
|
68
|
|
|
return $this->extractFromCommand('git ls-files --others --exclude-standard', function($value) { |
|
69
|
|
|
return trim($value); |
|
70
|
|
|
}); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns list of modified files in repo. |
|
75
|
|
|
* @return string[]|NULL NULL => no files modified |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getModifiedFiles(){ |
|
78
|
|
|
try{ |
|
79
|
|
|
return $this->extractFromCommand('git diff --name-status HEAD', function($array) { |
|
80
|
|
|
$array=trim(preg_replace('!\s+!', ' ', $array)); |
|
81
|
|
|
return explode(' ', $array); |
|
82
|
|
|
}); |
|
83
|
|
|
}catch (\Cz\Git\GitException $e){ |
|
|
|
|
|
|
84
|
|
|
return []; |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
View Code Duplication |
public function getChangesInFile($filename){ |
|
|
|
|
|
|
89
|
|
|
try{ |
|
90
|
|
|
$output=$this->extractFromCommand('git diff '.$filename); |
|
91
|
|
|
if(is_array($output)) |
|
92
|
|
|
return implode('\r\n', $output); |
|
93
|
|
|
return $output; |
|
94
|
|
|
}catch (\Cz\Git\GitException $e){ |
|
|
|
|
|
|
95
|
|
|
return ""; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
View Code Duplication |
public function getChangesInCommit($commitHash){ |
|
|
|
|
|
|
100
|
|
|
try{ |
|
101
|
|
|
$output=$this->extractFromCommand("git show {$commitHash}"); |
|
102
|
|
|
if(is_array($output)) |
|
103
|
|
|
return implode('\r\n', $output); |
|
104
|
|
|
return $output; |
|
105
|
|
|
}catch (\Cz\Git\GitException $e){ |
|
|
|
|
|
|
106
|
|
|
return ""; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Returns the remote URL |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
View Code Duplication |
public function getRemoteUrl(){ |
|
|
|
|
|
|
115
|
|
|
try{ |
|
116
|
|
|
$values= $this->extractFromCommand('git config --get remote.origin.url',function($str){ |
|
117
|
|
|
return trim($str); |
|
118
|
|
|
}); |
|
119
|
|
|
if(isset($values)){ |
|
120
|
|
|
return implode(" ", $values); |
|
121
|
|
|
} |
|
122
|
|
|
}catch (\Cz\Git\GitException $e){ |
|
|
|
|
|
|
123
|
|
|
return ""; |
|
124
|
|
|
} |
|
125
|
|
|
return ""; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Ignore file(s). |
|
130
|
|
|
* `git update-index --assume-unchanged <file>` |
|
131
|
|
|
* @param $files string|string[] |
|
132
|
|
|
* @throws Cz\Git\GitException |
|
133
|
|
|
* @return self |
|
134
|
|
|
*/ |
|
135
|
|
|
public function ignoreFiles($files){ |
|
136
|
|
|
if(!is_array($files)){ |
|
137
|
|
|
$file = func_get_args(); |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
$this->begin(); |
|
140
|
|
|
$this->run('git reset', NULL,["--"=>$files]); |
|
141
|
|
|
return $this->end(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function getCommits(){ |
|
145
|
|
|
$nonPushed=$this->getNonPushedCommitHash(); |
|
146
|
|
|
try{ |
|
147
|
|
|
return $this->extractFromCommand('git log --pretty=format:"%h___%an___%ar___%s___%H"', function($str) use($nonPushed){ |
|
148
|
|
|
$array=explode("___", $str); |
|
149
|
|
|
$pushed=true; |
|
150
|
|
|
if(is_array($nonPushed)) |
|
151
|
|
|
$pushed=!in_array($array[0], $nonPushed); |
|
152
|
|
|
return new GitCommit($array[0],$array[1],$array[2],$array[3],$array[4],$pushed); |
|
153
|
|
|
}); |
|
154
|
|
|
}catch (\Cz\Git\GitException $e){ |
|
|
|
|
|
|
155
|
|
|
return []; |
|
156
|
|
|
} |
|
157
|
|
|
return []; |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function getNonPushedCommitHash(){ |
|
161
|
|
|
try{ |
|
162
|
|
|
return $this->extractFromCommand('git log origin/master..master --pretty=format:"%h"'); |
|
163
|
|
|
}catch (\Cz\Git\GitException $e){ |
|
|
|
|
|
|
164
|
|
|
return []; |
|
165
|
|
|
} |
|
166
|
|
|
return []; |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.