-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGitHelper.php
More file actions
180 lines (140 loc) · 4.35 KB
/
GitHelper.php
File metadata and controls
180 lines (140 loc) · 4.35 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
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
<?php
class GitHelper {
public $path;
public $branch;
public $shouldPull;
public $shouldPush;
public $shouldCommit;
public $userHooks;
public $gitBin;
public $windowsMode;
public $debug;
private $user;
private $repo;
private $isInitialized;
/**
* Initializes the Helper-Class by gathering plugin-options.
*/
public function initOptions() {
$this->isInitialized = True;
$this->user = kirby()->user()->name() ?? kirby()->user()->email();;
$dir = option('wottpal.git.dir');
$this->path = kirby()->roots()->$dir();
$this->branch = option('wottpal.git.branch');
$this->shouldPull = option('wottpal.git.shouldPull');
$this->shouldPush = option('wottpal.git.shouldPush');
$this->shouldCommit = option('wottpal.git.shouldCommit');
$this->userHooks = option('wottpal.git.userHooks');
$this->gitBin = option('wottpal.git.gitBin');
$this->windowsMode = option('wottpal.git.windowsMode');
$this->debug = option('wottpal.git.debug');
$this->logFile = kirby()->roots()->index() . DS . option('wottpal.git.logFile');
}
/**
* Returns `true` if user-hooks are enabled in the config
* IMPORTANT: It's a security risk to put sensible user-data under version-control.
*/
public function userHooksEnabled() {
if (!$this->isInitialized) $this->initOptions();
return $this->userHooks;
}
/**
* Returns the given path
*/
public function path() {
if (!$this->isInitialized) $this->initOptions();
return $this->path;
}
/**
* Initializes the Git.php repository object.
*/
private function initRepo()
{
if ($this->repo) return true;
if (!$this->isInitialized) $this->initOptions();
if (!class_exists("Git")) {
if (file_exists(__DIR__ . DS . 'Git.php' . DS. 'Git.php')) {
require __DIR__ . DS . 'Git.php' . DS. 'Git.php';
}
}
if (!class_exists("Git")) {
throw new Exception('Git class not found. Is the Git.php submodule installed?');
}
if ($this->gitBin) {
Git::set_bin($this->gitBin);
}
if ($this->windowsMode) {
Git::windows_mode();
}
$this->repo = Git::open($this->path);
if (!$this->repo->test_git()) {
throw new Exception('System Git could not be found or is not working properly. ' . Git::get_bin());
// trigger_error('git could not be found or is not working properly. ' . Git::get_bin());
}
}
/**
* Returns the Git.php repository.
*/
public function getRepo() {
if ($this->repo == null) $this->initRepo();
return $this->repo;
}
/**
* Commits to the repository.
*/
public function commit($message) {
$this->getRepo()->add('-A');
$this->getRepo()->commit($message);
}
/**
* Pushes to the remote repository.
*/
public function push() {
$this->getRepo()->push('origin', $this->branch);
}
/**
* Pulls from the remote repository.
*/
public function pull()
{
$this->getRepo()->pull('origin', $this->branch);
}
/**
* Returns `true` if there are changes to commit.
*/
public function hasChangesToCommit() {
$result = $this->getRepo()->run('status --porcelain');
// Didn't show new dirs
// $result = $this->getRepo()->run('ls-files -m');
return $result !== '';
}
/**
* Called from a Kirby-hook when content got changed.
*/
public function changeHandler($message)
{
if (!$this->isInitialized) $this->initOptions();
try {
if ($this->branch) $this->getRepo()->checkout($this->branch);
if ($this->shouldPull) $this->pull();
if ($this->shouldCommit && $this->hasChangesToCommit()) $this->commit("{$message}\nBy: {$this->user}", true);
if ($this->shouldPush) $this->push();
if (!$this->hasChangesToCommit() && $this->shouldCommit) $this->log('Hook fired but no changes');
elseif ($this->shouldCommit) $this->log('Committed successfully');
} catch(Exception $exception) {
$errorMessage = 'Unable to update git: ' . $exception->getMessage();
$this->log($errorMessage);
// throw new Exception($errorMessage);
}
}
/**
* Creates a new log-message in the log-file.
*/
private function log($message) {
if (!$this->isInitialized) $this->initOptions();
if (!$this->debug) return;
$date = date("Y-m-d, H:i", time());
$message = "[{$date}] {$message}\n";
file_put_contents($this->logFile, $message, FILE_APPEND);
}
}