-
Notifications
You must be signed in to change notification settings - Fork 19
/
KirbyGitHelper.php
129 lines (104 loc) · 3.53 KB
/
KirbyGitHelper.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
<?php
namespace Blanko\Kirby\GCAPC;
use Git;
use Exception;
class KirbyGitHelper
{
private $kirby;
private $repo;
private $repoPath;
private $branch;
private $pullOnChange;
private $pushOnChange;
private $commitOnChange;
private $gitBin;
private $windowsMode;
public function __construct($repoPath = false)
{
$this->kirby = kirby();
$this->repoPath = $repoPath ? $repoPath : option('blankogmbh.gcapc.path', $this->kirby->root("content"));
$this->branch = option('blankogmbh.gcapc.branch', '');
}
private function initRepo()
{
if ($this->repo) {
return true;
}
if (!class_exists("Git")) {
throw new Exception('Git class not found. Make sure you run composer install inside this plugins directory');
}
$this->pullOnChange = option('blankogmbh.gcapc.pull', false);
$this->pushOnChange = option('blankogmbh.gcapc.push', false);
$this->commitOnChange = option('blankogmbh.gcapc.commit', true);
$this->gitBin = option('blankogmbh.gcapc.gitBin', '');
$this->windowsMode = option('blankogmbh.gcapc.windowsMode', false);
if ($this->windowsMode) {
Git::windows_mode();
}
if ($this->gitBin) {
Git::set_bin($this->gitBin);
}
$this->repo = Git::open($this->repoPath);
if (!$this->repo->test_git()) {
throw new Exception('git could not be found or is not working properly. ' . Git::get_bin());
}
}
private function getRepo()
{
if ($this->repo == null) {
$this->initRepo();
}
return $this->repo;
}
public function commit($commitMessage, $author = null)
{
$this->getRepo()->add('-A');
$command = "commit -m " . escapeshellarg($commitMessage);
if ($author) {
$command .= " --author=" . escapeshellarg($author);
}
// we use the raw run command here to optionally supply the git author
// IMPORTANT: make sure all arguments are escaped through escapeshellarg();
$this->getRepo()->run($command);
}
public function push($branch = false)
{
$branch = $branch ? $branch : $this->branch;
$this->getRepo()->push('origin', $branch);
}
public function pull($branch = false)
{
$branch = $branch ? $branch : $this->branch;
$this->getRepo()->pull('origin', $branch);
}
public function kirbyChange($commitMessage)
{
try {
$this->initRepo();
if ($this->branch) {
$this->getRepo()->checkout($this->branch);
}
if ($this->pullOnChange) {
$this->pull();
}
if ($this->commitOnChange) {
$user = $this->kirby->user();
$author = null;
if ($user) {
$author = $user->name() . " <" . $user->email() . ">";
}
$this->commit($commitMessage, $author);
}
if ($this->pushOnChange) {
$this->push();
}
} catch(Exception $exception) {
// only show exceptions when explicitly enabled
if (option('blankogmbh.gcapc.displayErrors', false)) {
throw new Exception('Unable to update git: ' . $exception->getMessage());
}
// still log for debug
error_log('Unable to update git: ' . $exception->getMessage(), E_USER_ERROR);
}
}
}