-
Notifications
You must be signed in to change notification settings - Fork 127
/
Rcs.php
346 lines (303 loc) · 10.4 KB
/
Rcs.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* Horde_Vcs_Rcs implementation.
*
* Copyright 2004-2007 Jeff Schwentner <jeffrey.schwentner@lmco.com>
*
* @author Jeff Schwentner <jeffrey.schwentner@lmco.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @package Vcs
*/
class Horde_Vcs_Rcs extends Horde_Vcs_Base
{
/**
* The current driver.
*
* @var string
*/
protected $_driver = 'Rcs';
/**
* TODO
*/
public function isValidRevision($rev)
{
return $rev && preg_match('/^\d+\.(\d+\.)*\d+$/', $rev);
}
/**
* Create a range of revisions between two revision numbers.
*
* @param Horde_Vcs_File_Rcs $file The desired file.
* @param string $r1 The initial revision.
* @param string $r2 The ending revision.
*
* @return array The revision range, or empty if there is no straight
* line path between the revisions.
*/
public function getRevisionRange(Horde_Vcs_File_Base $file, $r1, $r2)
{
switch ($this->cmp($r1, $r2)) {
case 0:
return array();
case 1:
$curr = $this->prev($r1);
$stop = $this->prev($r2);
$flip = true;
break;
case -1:
$curr = $r2;
$stop = $r1;
$flip = false;
break;
}
$ret_array = array();
do {
$ret_array[] = $curr;
$curr = $this->prev($curr);
if ($curr === $stop) {
return ($flip) ? array_reverse($ret_array) : $ret_array;
}
} while (!is_null($curr) && ($this->cmp($curr, $stop) != -1));
return array();
}
/**
* TODO
*/
public function getFile($filename, $opts = array())
{
$filename = ltrim($filename, '/');
$fname = $filename . ',v';
if (!@is_file($this->sourceroot . '/' . $fname)) {
throw new Horde_Vcs_Exception(sprintf('File "%s" not found', $filename));
}
return parent::getFile($fname, $opts);
}
/**
* Checks an RCS file in with a specified change log.
*
* @param string $filepath Location of file to check in.
* @param string $message Log of changes since last version.
* @param string $user The user name to use for the check in.
* @param boolean $newBinary Does the change involve binary data?
*
* @return string The new revision number on success.
*/
public function ci($filepath, $message, $user = null, $newBinary = false)
{
putenv('LOGNAME=' . ($user ? $user : 'guest'));
$ci_cmd = $this->getPath('ci') . ' ' . escapeshellarg($filepath) . ' 2>&1';
$rcs_cmd = $this->getPath('rcs') . ' -i -kb ' . escapeshellarg($filepath) . ' 2>&1';
$output = '';
$message_lines = explode("\n", $message);
$pipe_def = array(0 => array("pipe", 'r'),
1 => array("pipe", 'w'));
$process = proc_open($newBinary ? $rcs_cmd : $ci_cmd, $pipe_def, $pipes);
if (is_resource($process)) {
foreach ($message_lines as $line) {
if ($line == '.\n') {
$line = '. \n';
}
fwrite($pipes[0], $line);
}
fwrite($pipes[0], "\n.\n");
fclose($pipes[0]);
while (!feof($pipes[1])) {
$output .= fread($pipes[1], 8192);
}
fclose($pipes[1]);
proc_close($process);
} else {
throw new Horde_Vcs_Exception('Failed to open pipe in ci()');
}
if ($newBinary) {
exec($ci_cmd . ' 2>&1', $return_array, $retval);
if ($retval) {
throw new Horde_Vcs_Exception("Unable to spawn ci on $filepath from ci()");
} else {
foreach ($return_array as $line) {
$output .= $line;
}
}
}
$rev_start = strpos($output, 'new revision: ');
// If no new revision, see if this is an initial checkin.
if ($rev_start === false) {
$rev_start = strpos($output, 'initial revision: ');
$rev_end = strpos($output, ' ', $rev_start);
} else {
$rev_end = strpos($output, ';', $rev_start);
}
if ($rev_start !== false && $rev_end !== false) {
$rev_start += 14;
return substr($output, $rev_start, $rev_end - $rev_start);
} else {
unlock($filepath);
$temp_pos = strpos($output, 'file is unchanged');
if ($temp_pos !== false) {
throw new Horde_Vcs_Exception('Check-in Failure: ' . basename($filepath) . ' has not been modified');
} else {
throw new Horde_Vcs_Exception("Failed to checkin $filepath, $ci_cmd, $output");
}
}
}
/**
* Checks the locks on a CVS/RCS file.
*
* @param string $filepath Location of file.
* @param string &$locked_by Returns the username holding the lock.
*
* @return boolean True on success.
*/
public function isLocked($filepath, &$locked_by)
{
$cmd = $this->getPath('rlog') . ' -L ' . escapeshellarg($filepath);
exec($cmd . ' 2>&1', $return_array, $retval);
if ($retval) {
throw new Horde_Vcs_Exception("Unable to spawn rlog on $filepath from isLocked()");
} else {
$output = '';
foreach ($return_array as $line) {
$output .= $line;
}
$start_name = strpos($output, 'locked by: ');
$end_name = strpos($output, ';', $start_name);
if ($start_name !== false && $end_name !== false) {
$start_name += 11;
$locked_by = substr($output, $start_name, $end_name - $start_name);
return true;
} elseif (strlen($output) == 0) {
return false;
} else {
throw new Horde_Vcs_Exception('Failure running rlog in isLocked()');
}
}
}
/**
* Locks a CVS/RCS file.
*
* @param string $filepath Location of file.
* @param string $user User name to lock the file with
*
* @return boolean True on success.
*/
public function lock($filepath, $user = null)
{
// Get username for RCS tag.
if ($user) {
putenv('LOGNAME=' . $user);
} else {
putenv('LOGNAME=guest');
}
$cmd = $this->getPath('rcs') . ' -l ' . escapeshellarg($filepath);
exec($cmd . ' 2>&1', $return_array, $retval);
if ($retval) {
throw new Horde_Vcs_Exception('Failed to spawn rcs ("' . $cmd . '") on "' . $filepath . '" (returned ' . $retval . ')');
} else {
$output = '';
foreach ($return_array as $line) {
$output .= $line;
}
$locked_pos = strpos($output, 'locked');
if ($locked_pos !== false) {
return true;
} else {
throw new Horde_Vcs_Exception('Failed to lock "' . $filepath . '" (Ran "' . $cmd . '", got return code ' . $retval . ', output: ' . $output . ')');
}
}
}
/**
* Unlocks a CVS/RCS file.
*
* @param string $filepath Location of file.
* @param string $user User name to unlock the file with
*
* @return boolean True on success.
*/
public function unlock($filepath, $user = null)
{
// Get username for RCS tag.
if ($user) {
putenv('LOGNAME=' . $user);
} else {
putenv('LOGNAME=guest');
}
$cmd = $this->getPath('rcs') . ' -u ' . escapeshellarg($filepath);
exec($cmd . ' 2>&1', $return_array, $retval);
if ($retval) {
throw new Horde_Vcs_Exception('Failed to spawn rcs ("' . $cmd . '") on "' . $filepath . '" (returned ' . $retval . ')');
} else {
$output = '';
foreach ($return_array as $line) {
$output .= $line;
}
$unlocked_pos = strpos($output, 'unlocked');
if ($unlocked_pos !== false) {
return true;
} else {
// Already unlocked.
return true;
}
}
}
/**
* Given a revision number, remove a given number of portions from
* it. For example, if we remove 2 portions of 1.2.3.4, we are
* left with 1.2.
*
* @param string $val Input revision.
* @param integer $amount Number of portions to strip.
*
* @return string Stripped revision number.
*/
public function strip($val, $amount = 1)
{
$this->assertValidRevision($val);
$pos = 0;
while ($amount-- > 0 && ($pos = strrpos($val, '.')) !== false) {
$val = substr($val, 0, $pos);
}
return ($pos !== false) ? $val : false;
}
/**
* Given two revision numbers, this figures out which one is
* greater than the other by stepping along the decimal points
* until a difference is found, at which point a sign comparison
* of the two is returned.
*
* @param string $rev1 Period delimited revision number
* @param string $rev2 Second period delimited revision number
*
* @return integer 1 if the first is greater, -1 if the second if greater,
* and 0 if they are equal
*/
public function cmp($rev1, $rev2)
{
return strnatcasecmp($rev1, $rev2);
}
/**
* Return the logical revision before this one. Normally, this
* will be the revision minus one, but in the case of a new
* branch, we strip off the last two decimal places to return the
* original branch point.
*
* @param string $rev Revision number to decrement.
*
* @return string Revision number, or null if none could be determined.
*/
public function prev($rev)
{
$last_dot = strrpos($rev, '.');
$val = substr($rev, ++$last_dot);
if (--$val > 0) {
return substr($rev, 0, $last_dot) . $val;
} else {
--$last_dot;
while (--$last_dot) {
if ($rev[$last_dot] == '.') {
return substr($rev, 0, $last_dot);
} elseif (is_null($rev[$last_dot])) {
return null;
}
}
}
}
}