-
Notifications
You must be signed in to change notification settings - Fork 2
/
eventum-svn-hook.php
executable file
·154 lines (129 loc) · 3.69 KB
/
eventum-svn-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
#!/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';
$original_argv = $argv;
$default_options = array(
'n' => 'svn',
);
$options = _getopt('n:') + $default_options;
$PROGRAM = basename(realpath(array_shift($argv)), '.php');
$eventum_url = array_shift($argv);
$scm_name = $options['n'];
try {
main($scm_name, $argv);
} catch (Exception $e) {
error_log("ERROR[$PROGRAM]: " . $e->getMessage());
error_log('Debug saved to: ' . save_environment($original_argv));
exit(1);
}
exit(0);
function main($scm_name, $argv)
{
if (count($argv) != 2) {
throw new InvalidArgumentException('Invalid arguments');
}
$repos = $argv[0];
$rev = $argv[1];
global $svnlook;
if (!isset($svnlook)) {
$svnlook = '/usr/bin/svnlook';
}
if (!is_executable($svnlook)) {
throw new BadFunctionCallException('svnlook is not executable, edit $svnlook');
}
$results = svnlook('info', $repos, $rev);
list($username, $date, $commit_msg) = svn_commit_info($results);
// parse the commit message and get all issue numbers we can find
$issues = match_issues($commit_msg);
if (!$issues) {
return;
}
$files = svn_commit_files($repos, $rev);
$params = array(
'scm' => 'svn',
'scm_name' => $scm_name,
'username' => $username,
'commit_msg' => $commit_msg,
'issue' => $issues,
'files' => $files,
'commitid' => $rev,
);
scm_ping($params);
}
/**
* Process username, date and commit message from svnlook output
*
* @param array $results
* @return array
*/
function svn_commit_info($results)
{
// get commit date and username and commit message
$username = array_shift($results);
$date = array_shift($results);
// ignore commit message length value
array_shift($results);
// get the full commit message
$commit_msg = implode("\n", $results);
return array($username, $date, $commit_msg);
}
/**
* Get files affected from $rev
*
* @param string $rev
* @return array
*/
function svn_commit_files($repo, $rev)
{
// create array with predefined keys
$files = array(
'added',
'removed',
'modified',
);
$files = array_fill_keys($files, array());
$changes = svnlook('changed', $repo, $rev);
foreach ($changes as $change) {
// http://svnbook.red-bean.com/en/1.7/svn.ref.svnlook.c.changed.html
// flags:
// - 'A ' Item added to repository
// - 'D ' Item deleted from repository
// - 'U ' File contents changed
// - '_U' Properties of item changed; note the leading underscore
// - 'UU' File contents and properties changed
list($change_info, $filename) = preg_split('/\s+/', $change, 2);
$flags = preg_split('//', $change_info, -1, PREG_SPLIT_NO_EMPTY);
if (in_array('A', $flags, true)) {
$change_type = 'added';
} elseif (in_array('D', $flags, true)) {
$change_type = 'removed';
} else {
$change_type = 'modified';
}
$files[$change_type][] = $filename;
}
return $files;
}
/**
* Execute svnlook command on $repo for $revision
*
* @param string $command
* @param string $repo
* @param int $revision
* @return array
*/
function svnlook($command, $repo, $revision)
{
global $svnlook;
return execx("$svnlook $command $repo -r $revision");
}