-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheventum-cvs-hook.php
executable file
·315 lines (267 loc) · 7.8 KB
/
eventum-cvs-hook.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env php
<?php
/*
* This file is part of the Eventum (Issue Tracking System) package.
*
* @copyright (c) Eventum Team
* @license GNU General Public License, version 2 or later (GPL-2+)
*
* For the full copyright and license information,
* please see the COPYING and AUTHORS files
* that were distributed with this source code.
*/
require_once __DIR__ . '/helpers.php';
$default_options = array(
'n' => 'cvs',
);
$options = _getopt('l:n:') + $default_options;
if (isset($options['l'])) {
$context = load_cvs_context($options['l']);
} else {
$context = create_context($argv);
}
$context['arguments'] = array_slice($context['argv'], 2);
$context['scm_name'] = $options['n'];
// these need to be global. for now
$eventum_url = $context['argv'][1];
$PROGRAM = basename($context['argv'][0], '.php');
try {
main($context);
} catch (Exception $e) {
error_log("ERROR[$PROGRAM]: " . $e->getMessage());
error_log('Debug saved to: ' . store_environment($context));
exit(1);
}
exit(0);
function main($context)
{
$commit_msg = cvs_commit_msg($context['stdin']);
// parse the commit message and get all issue numbers we can find
$issues = match_issues($commit_msg);
if (!$issues) {
return;
}
list($username, $modified_files) = cvs_commit_info($context['arguments']);
list($commitid, $files) = cvs_get_files($modified_files);
$params = array(
'scm' => 'cvs',
'commit_date' => $context['time'],
'scm_name' => $context['scm_name'],
'username' => $username,
'commit_msg' => $commit_msg,
'issue' => $issues,
'files' => $files,
'commitid' => $commitid,
);
scm_ping($params);
}
/**
* create files payload
*
* @param array $modified_files
* @return array
*/
function cvs_get_files($modified_files)
{
// create array with predefined keys
$files = array(
'added',
'removed',
'modified',
);
$files = array_fill_keys($files, array());
$commitid = array();
foreach ($modified_files as $file) {
$filename = $file['filename'];
if (!$file['old_revision']) {
$change_type = 'added';
} elseif (!$file['new_revision']) {
$change_type = 'removed';
} else {
$change_type = 'modified';
}
$files['versions'][$filename] = array($file['old_revision'], $file['new_revision']);
$files[$change_type][] = $filename;
$commitid[] = $file['commitid'];
}
// flatten commitid
$commitid = array_unique($commitid);
if (count($commitid) > 1) {
throw new InvalidArgumentException('Commit Id should be unique');
}
$commitid = current($commitid);
return array($commitid, $files);
}
/**
* @param array $argv
* @return array of username, modified files
*/
function cvs_commit_info($argv)
{
// user who is committing these changes
$username = array_shift($argv);
if (count($argv) === 1) {
$modified_files = cvs_parse_info_1_11($argv);
} else {
$modified_files = cvs_parse_info_1_12($argv);
}
// parse told to skip (for example adding new dir)
if (!$modified_files) {
exit(0);
}
return array($username, $modified_files);
}
/**
* assume the old way ("PATH {FILE,rev1,rev2 }+")
* CVSROOT/loginfo: ALL eventum-cvs-hook $USER %{sVv}
*
* @return array
*/
function cvs_parse_info_1_11($argv)
{
$args = explode(' ', array_shift($argv));
// save what the name of the module is
$cvs_module = array_shift($args);
// skip if we're importing or adding new dirrectory
$msg = implode(' ', array_slice($args, -3));
if (in_array($msg, array('- Imported sources', '- New directory'))) {
return null;
}
// now parse the list of modified files
$modified_files = array();
foreach ($args as $file_info) {
list($filename, $old_revision, $new_revision) = explode(',', $file_info);
$modified_files[] = array(
'filename' => "$cvs_module/$filename",
'old_revision' => cvs_filter_none($old_revision),
'new_revision' => cvs_filter_none($new_revision),
);
}
return $modified_files;
}
/**
* assume the new way ("PATH" {"FILE" "rev1" "rev2"}+)
* CVSROOT/loginfo: ALL eventum-cvs-hook $USER "%p" %{sVv}
*
* @return array
*/
function cvs_parse_info_1_12($args)
{
// save what the name of the module is
$cvs_module = array_shift($args);
// skip if we're importing or adding new directory
// TODO: checked old way with CVS 1.11, but not checked the new way
$msg = implode(' ', array_slice($args, -3));
if (in_array($msg, array('- Imported sources', '- New directory'))) {
return null;
}
// now parse the list of modified files
$modified_files = array();
$use_rcs = !file_exists('CVS/Entries');
while ($file_info = array_splice($args, 0, 3)) {
list($filename, $old_revision, $new_revision) = $file_info;
$old_revision = cvs_filter_none($old_revision);
$new_revision = cvs_filter_none($new_revision);
$modified_files[] = array(
'filename' => "$cvs_module/$filename",
'old_revision' => $old_revision,
'new_revision' => $new_revision,
'commitid' => $use_rcs ? rcs_commitid($filename, $old_revision, $new_revision) : cvs_commitid($filename),
);
}
return $modified_files;
}
/**
* @param string $pattern
* @param array $input
* @return string|null
* @internal
*/
function cvs_match_commitid($pattern, array $input)
{
// find line matching 'Commit Identifier'
$lines = preg_grep($pattern, $input);
if (!$lines) {
return null;
}
// match commit id
if (!preg_match($pattern, current($lines), $m)) {
return null;
}
return $m['commitid'];
}
/**
* Extract 'commitid' from file using rcs
*
* @param string $filename
* @param string|null $r1
* @param string|null $r2
* @return string
*/
function rcs_commitid($filename, $r1, $r2)
{
if ($r2) {
// added or modified file
$result = execx('rcs log -r' . escapeshellarg($r2) . ' ' . escapeshellarg($filename));
} else {
// deleted file
// NOTE: as revision not given, this may not work if file is added again meanwhile
$result = execx('rcs log ' . escapeshellarg("Attic/$filename"));
}
// date: 2019/08/13 09:04:57; author: glen; state: Exp; lines: +2 -3; commitid: uoHAXoFNyk8CxQyB
$pattern = '/^date:.+commitid:\s+(?P<commitid>\S+)/sm';
return cvs_match_commitid($pattern, $result);
}
/**
* Extract 'commitid' from file, Requires CVS 1.12+
*
* @param string $filename
* @return string
*/
function cvs_commitid($filename)
{
$result = execx('cvs -Qn status ' . escapeshellarg($filename));
$pattern = '/Commit Identifier:\s+(?P<commitid>\S+)/';
return cvs_match_commitid($pattern, $result);
}
/**
* filter out NONE revision
*
* @param string $rev
*/
function cvs_filter_none($rev)
{
if ($rev !== 'NONE') {
return $rev;
}
return null;
}
/**
* Obtain CVS commit message
*
* @return string
*/
function cvs_commit_msg($input)
{
// get the full commit message
$commit_msg = rtrim(substr($input, strpos($input, 'Log Message:') + strlen('Log Message:') + 1));
return $commit_msg;
}
/**
* @param string $dump_file
* @return array
*/
function load_cvs_context($dump_file)
{
$context = load_context($dump_file);
// extract real cwd to use
$root = preg_quote($context['env']['CVSROOT'], '/');
if (preg_match("/^Update of (?P<cwd>$root.+)$/m", $context['stdin'], $m)) {
$context['original_cwd'] = $context['cwd'];
$context['cwd'] = $m['cwd'];
}
if (chdir($context['cwd']) === false) {
throw new RuntimeException("Unable to change directory to: {$context['cwd']}");
}
return $context;
}