/
Cvs.php
272 lines (236 loc) · 8.27 KB
/
Cvs.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
<?php
/**
* Horde_Vcs_cvs implementation.
*
* Constructor args:
* - 'sourceroot': The source root for this repository
* - 'paths': Hash with the locations of all necessary binaries: 'rcsdiff',
* 'rlog', 'cvsps', 'cvsps_home', and 'temp' (the temp path).
* - 'timezone': Timezone, necessary for cvsps.
*
* Copyright 2000-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Anil Madhavapeddy <anil@recoil.org>
* @author Michael Slusarz <slusarz@horde.org>
* @package Vcs
*/
class Horde_Vcs_Cvs extends Horde_Vcs_Rcs
{
/**
* The current driver.
*
* @var string
*/
protected $_driver = 'Cvs';
/**
* Driver features.
*
* @var array
*/
protected $_features = array(
'deleted' => true,
'patchsets' => true,
'branches' => true,
'snapshots' => false);
/**
* Constructor.
*/
public function __construct($params = array())
{
parent::__construct($params);
if (!$this->getPath('cvsps')) {
$this->_features['patchsets'] = false;
}
}
/**
* TODO
*/
public function getFile($filename, $opts = array())
{
$filename = ltrim($filename, '/');
$fname = $filename . ',v';
/* Assume file is in the Attic if it doesn't exist. */
if (!@is_file($this->sourceroot . '/' . $fname)) {
$fname = dirname($filename) . '/Attic/' . basename($filename) . ',v';
}
if (!@is_file($this->sourceroot . '/' . $fname)) {
throw new Horde_Vcs_Exception(sprintf('File "%s" not found', $filename));
}
return Horde_Vcs_Base::getFile($fname, $opts);
}
/**
* TODO
*/
public function isFile($where)
{
return @is_file($where . ',v') ||
@is_file(dirname($where) . '/Attic/' . basename($where) . ',v');
}
/**
* Obtain the differences between two revisions of a file.
*
* @param Horde_Vcs_File_Cvs $file The desired file.
* @param string $rev1 Original revision number to compare
* from.
* @param string $rev2 New revision number to compare against.
* @param array $opts The following optional options:
* - 'num': (integer) DEFAULT: 3
* - 'type': (string) DEFAULT: 'unified'
* - 'ws': (boolean) DEFAULT: true
*
* @return string|boolean False on failure, or a string containing the
* diff on success.
*/
protected function _diff(Horde_Vcs_File_Base $file, $rev1, $rev2, $opts)
{
$fullName = $file->getPath();
$diff = array();
$flags = '-kk ';
if (!$opts['ws']) {
$flags .= ' -bB ';
}
switch ($opts['type']) {
case 'context':
$flags .= '-p --context=' . escapeshellarg((int)$opts['num']);
break;
case 'unified':
$flags .= '-p --unified=' . escapeshellarg((int)$opts['num']);
break;
case 'column':
$flags .= '--side-by-side --width=120';
break;
case 'ed':
$flags .= '-e';
break;
}
// Windows versions of cvs always return $where with forwards slashes.
if (VC_WINDOWS) {
$fullName = str_replace(DIRECTORY_SEPARATOR, '/', $fullName);
}
// TODO: add options for $hr options - however these may not be
// compatible with some diffs.
$command = escapeshellcmd($this->getPath('rcsdiff')) . ' ' . $flags . ' -r' . escapeshellarg($rev1) . ' -r' . escapeshellarg($rev2) . ' ' . escapeshellarg($fullName) . ' 2>&1';
if (VC_WINDOWS) {
$command .= ' < ' . escapeshellarg(__FILE__);
}
exec($command, $diff, $retval);
return ($retval > 0) ? $diff : array();
}
/**
* TODO
*
* @throws Horde_Vcs_Exception
*/
public function annotate($fileob, $rev)
{
$this->assertValidRevision($rev);
$tmpfile = Horde_Util::getTempFile('vc', true, $this->_paths['temp']);
$where = $fileob->getSourcerootPath();
$language = getenv('LC_MESSAGES');
putenv('LC_MESSAGES=C');
$proc = proc_open(
escapeshellcmd($this->getPath('cvs')) . ' -n server',
array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')),
$pipes
);
if (!$proc) {
throw new Horde_Vcs_Exception('Unable to annotate');
}
$out = array(
'Root ' . $this->sourceroot,
'Valid-responses ok error Valid-requests Checked-in Updated Merged Removed M E',
'UseUnchanged',
'Argument -r',
'Argument ' . $rev,
'Argument ' . $where
);
$dirs = explode('/', dirname($where));
while (count($dirs)) {
$out[] = 'Directory ' . implode('/', $dirs);
$out[] = $this->sourceroot . '/' . implode('/', $dirs);
array_pop($dirs);
}
$out[] = 'Directory .';
$out[] = $this->sourceroot;
$out[] = 'annotate';
foreach ($out as $line) {
fwrite($pipes[0], "$line\n");
}
fclose($pipes[0]);
putenv('LC_MESSAGES=' . $language);
stream_set_blocking($pipes[2], 0);
if ($error = stream_get_contents($pipes[2])) {
fclose($pipes[2]);
fclose($pipes[1]);
proc_close($proc);
throw new Horde_Vcs_Exception($error);
}
fclose($pipes[2]);
$lines = array();
$line = fgets($pipes[1]);
// Windows versions of cvs always return $where with forwards slashes.
if (VC_WINDOWS) {
$where = str_replace(DIRECTORY_SEPARATOR, '/', $where);
}
while ($line && !preg_match("|^E\s+Annotations for $where|", $line)) {
$line = fgets($pipes[1]);
}
if (!$line) {
fclose($pipes[1]);
proc_close($proc);
throw new Horde_Vcs_Exception('Unable to annotate');
}
$lineno = 1;
while ($line = fgets($pipes[1])) {
if (preg_match('/^M\s+([\d\.]+)\s+\((.+)\s+(\d+-\w+-\d+)\):.(.*)$/', $line, $regs)) {
$lines[] = array(
'rev' => $regs[1],
'author' => trim($regs[2]),
'date' => $regs[3],
'line' => $regs[4],
'lineno' => $lineno++
);
}
}
fclose($pipes[1]);
proc_close($proc);
return $lines;
}
/**
* Returns a file pointing to the head of the requested revision of a
* file.
*
* @param string $fullname Fully qualified pathname of the desired file
* to checkout.
* @param string $rev Revision number to check out.
*
* @return resource A stream pointer to the head of the checkout.
* @throws Horde_Vcs_Exception
*/
public function checkout($fullname, $rev)
{
$this->assertValidRevision($rev);
if (!($RCS = $this->popen(escapeshellcmd($this->getPath('co')) . ' ' . escapeshellarg('-p' . $rev) . ' ' . escapeshellarg($fullname) . ' 2>&1'))) {
throw new Horde_Vcs_Exception('Couldn\'t perform checkout of the requested file');
}
/* First line from co should be of the form :
* /path/to/filename,v --> standard out
* and we check that this is the case and error otherwise
*/
$co = fgets($RCS, 1024);
if (!preg_match('/^([\S ]+),v\s+-->\s+st(andar)?d ?out(put)?\s*$/', $co, $regs) ||
($regs[1] != $fullname)) {
throw new Horde_Vcs_Exception('Unexpected output from checkout: ' . $co);
}
/* Next line from co is of the form:
* revision 1.2.3
* TODO: compare this to $rev for consistency, atm we just
* discard the value to move input pointer along - avsm
*/
$co = fgets($RCS, 1024);
return $RCS;
}
}