-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Git.php
216 lines (170 loc) · 4.4 KB
/
Git.php
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace Oblik\KirbyGit;
use Exception;
class Git
{
protected $config;
/**
* Executable to run.
*/
protected $bin;
/**
* Absolute path to a Git repo.
*/
protected $repo;
/**
* The currently checked out branch in the repo.
*/
protected $branchCurrent;
/**
* Branch used to merge in a fast-forward-only fashion.
*/
protected $branchMerge;
/**
* Remote used when comparing commits or pushing.
*/
protected $remote;
/**
* File where each executed command is logged.
*/
protected $logfile;
public function __construct(array $config = [])
{
$this->config = $config;
$this->bin = $this->option('bin');
$this->repo = realpath($this->option('repo'));
if ($this->repo === false) {
throw new Exception('Inavlid repo path');
}
$this->branchCurrent = $this->branch();
if (empty($this->branchCurrent)) {
throw new Exception('No checked out branch');
}
$this->branchMerge = $this->option('merge');
$this->remote = $this->option('remote');
$this->logfile = $this->option('log');
}
public function option(string $name)
{
return $this->config[$name] ?? option("oblik.git.$name", null);
}
public function exec(string $command)
{
$code = null;
$output = [];
$cmd = "{$this->bin} -C {$this->repo} {$command} 2>&1";
if ($this->logfile) {
file_put_contents($this->logfile, $cmd . PHP_EOL, FILE_APPEND);
}
exec($cmd, $output, $code);
if ($code !== 0) {
$message = implode(PHP_EOL, $output);
throw new Exception($this->deduceError($message) ?? $message);
}
return $output;
}
public function deduceError(string $message)
{
if (
strpos($message, 'usage: git ') !== false ||
strpos($message, 'not a git command') !== false
) {
return 'It seems you have an outdated Git version: ' . $this->version();
}
if (
strpos($message, 'Not possible to fast-forward') !== false ||
strpos($message, 'would be overwritten by merge') !== false
) {
return 'Refusing to pull remote changes because they’re not merged with the local changes.';
}
}
public function version()
{
$version = null;
try {
$output = [];
exec('git --version', $output);
$version = implode('', $output);
} catch (Exception $e) {
$version = 'unknown';
}
return $version;
}
public function branch()
{
return $this->exec('branch --show-current')[0] ?? null;
}
public function status()
{
$output = $this->exec('status -u --porcelain');
$data = array_map(function ($line) {
$matches = null;
$data = [];
if (preg_match('!(.{2}) (.+)!', $line, $matches)) {
$axes = str_split($matches[1]);
$data['file'] = $matches[2];
if ($axes[0] !== ' ') {
$data['staged'] = $axes[0];
}
if ($axes[1] !== ' ') {
$data['unstaged'] = $axes[1];
}
}
return $data;
}, $output);
return array_filter_recursive($data);
}
public function add()
{
return $this->exec('add . --verbose');
}
public function commit($message)
{
$committer = kirby()->user();
$name = $committer->name()->value();
$email = $committer->email();
if (empty($name) || empty($email)) {
$name = 'Kirby Git';
$email = '[email protected]';
}
$message = escapeshellarg($message);
$name = escapeshellarg($name);
$email = escapeshellarg($email);
return $this->exec("-c user.name={$name} -c user.email={$email} commit --message={$message} --no-status");
}
public function log(int $page, int $limit)
{
$list = $this->exec("rev-list --count {$this->branchCurrent}");
$count = (int) ($list[0] ?? 0);
$new = $this->exec("log {$this->remote}/{$this->branchCurrent}..{$this->branchCurrent} --format=%h");
$format = '"%h|%an|%ae|%ad|%s"';
$skip = escapeshellarg(($page - 1) * $limit);
$limit = escapeshellarg($limit);
$commits = $this->exec("log {$this->branchCurrent} --pretty=format:{$format} --skip={$skip} --max-count={$limit}");
foreach ($commits as $i => $str) {
$data = str_getcsv($str, '|');
$hash = $data[0];
$commits[$i] = [
'new' => in_array($hash, $new) !== false,
'hash' => $hash,
'name' => $data[1],
'email' => $data[2],
'date' => $data[3],
'subject' => $data[4]
];
}
return [
'total' => $count,
'new' => count($new),
'commits' => $commits
];
}
public function push()
{
return $this->exec("push {$this->remote} {$this->branchCurrent}");
}
public function pull()
{
return $this->exec("pull {$this->remote} {$this->branchMerge} --ff-only");
}
}