-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGit.php
More file actions
150 lines (122 loc) · 4.1 KB
/
Git.php
File metadata and controls
150 lines (122 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace modules\gitclient;
class Git {
private $commands = [];
private $directory = ".";
private $gitBin = "git";
private $remote;
private $directRun = true;
private $noOutput = false;
public function changeDirectory($directory) : void{
$this->addCommand("cd $directory");
$this->directory = $directory;
}
public function addRemote($remoteName, $remoteUrl):void{
$this->addCommand($this->gitBin." remote add $remoteName $remoteUrl");
}
public function setRemote($remote): void{
$this->remote = $remote;
}
public function commit($message) : void {
$this->addCommand($this->gitBin." commit -m \"".escapeshellarg($message)."\"");
}
public function initIfNot() : void {
if (!file_exists($this->directory."/.git")) {
$this->addCommand($this->gitBin." init");
}
}
public function add($what) : void {
$this->addCommand($this->gitBin." add ".escapeshellarg($what));
}
public function clearCommands() : void {
$this->commands = [];
}
/**
* @param $var1 / Remote / Branch (If param length == 1)
* @param null $var2 / Branch
*/
public function push($var1, $var2 = null) : void {
$remote = "";
$branch = "master";
if ($var2 == null) {
$remote = $this->remote;
$branch = $var1;
} else {
$remote = $var1;
$branch = $var2;
}
$this->addCommand($this->gitBin." push ".escapeshellarg($remote)." ".escapeshellarg($branch));
if ($this->directRun) $this->run();
}
public function pull($var1, $var2 = null) : void {
$remote = "";
$branch = "master";
if ($var2 == null) {
$remote = $this->remote;
$branch = $var1;
} else {
$remote = $var1;
$branch = $var2;
}
$this->addCommand($this->gitBin." pull ".escapeshellarg($remote)." ".escapeshellarg($branch));
if ($this->directRun) $this->run();
}
private function addCommand(string $cmd):void{
$this->commands["ID_".rand(0000,1111).hash("SHA1", $cmd)] = $cmd;
}
/**
* Runs the commands
*/
public function run() : void {
$command = "";
foreach ($this->commands as $cmdid=>$cmd) {
$command .= $cmd . "\n";
unset($this->commands[$cmdid]);
}
$this->exec($command);
}
public function runGit($cmd, $runInstantly=false){
$this->addCommand($this->gitBin." ".$cmd);
if ($runInstantly) $this->run();
}
private function exec($cmd) : void {
$src = "";
if ($this->noOutput) {
$src = @ob_get_contents();
@ob_clean();
}
$disablefunc = [];
$disablefunc = explode(",", str_replace(" ", "", @ini_get("disable_functions")));
if(is_callable("exec") && !in_array("exec", $disablefunc)) {
exec($cmd);
} elseif(is_callable("system") && !in_array("system", $disablefunc)) {
system($cmd);
} elseif(is_callable("passthru") && !in_array("passthru", $disablefunc)) {
passthru($cmd);
} elseif(is_callable("popen") && !in_array("popen", $disablefunc) && is_resource($h = popen($cmd, "r"))) {
$result = "";
if(is_callable("fread") && !in_array("fread", $disablefunc)) {
while(!feof($h)) {
$result .= fread($h, 1024);
}
} else {
while(!feof($h)) {
$result .= fgets($h, 1024);
}
}
pclose($h);
} else
trigger_error("Cannot execute the command due to server restrictions.", E_USER_WARNING);
if ($this->noOutput) {
$rs = @ob_get_contents();
@ob_clean();
echo $src;
}
}
public function setDirectRun(bool $directRun) : void {
$this->directRun = $directRun;
}
public function setNoOutput(bool $noOutput) : void {
$this->noOutput = $noOutput;
}
}