forked from horde/horde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base.php
534 lines (477 loc) · 15.8 KB
/
Base.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<?php
/**
* Version Control generalized library.
*
* Copyright 2008-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @package Vcs
*/
abstract class Horde_Vcs_Base
{
/**
* The source root of the repository.
*
* @var string
*/
public $sourceroot;
/**
* Hash with the locations of all necessary binaries.
*
* @var array
*/
protected $_paths = array();
/**
* Hash caching the parsed users file.
*
* @var array
*/
protected $_users = array();
/**
* The current driver.
*
* @var string
*/
protected $_driver;
/**
* If caching is desired, a Horde_Cache object.
*
* @var Horde_Cache
*/
protected $_cache;
/**
* Driver features.
*
* @var array
*/
protected $_features = array(
'deleted' => false,
'patchsets' => false,
'branches' => false,
'snapshots' => false);
/**
* Current cache version.
*
* @var integer
*/
protected $_cacheVersion = 5;
/**
* The available diff types.
*
* @var array
*/
protected $_diffTypes = array('column', 'context', 'ed', 'unified');
/**
* Constructor.
*/
public function __construct($params = array())
{
$this->_cache = empty($params['cache']) ? null : $params['cache'];
$this->sourceroot = $params['sourceroot'];
$this->_paths = $params['paths'];
}
/**
* Does this driver support the given feature?
*
* @return boolean True if driver supports the given feature.
*/
public function hasFeature($feature)
{
return !empty($this->_features[$feature]);
}
/**
* Validation function to ensure that a revision string is of the right
* form.
*
* @param mixed $rev The purported revision string.
*
* @return boolean True if a valid revision string.
*/
abstract public function isValidRevision($rev);
/**
* Throw an exception if the revision number isn't valid.
*
* @param mixed $rev The revision number.
*
* @throws Horde_Vcs_Exception
*/
public function assertValidRevision($rev)
{
if (!$this->isValidRevision($rev)) {
throw new Horde_Vcs_Exception('Invalid revision number "' . $rev . '"');
}
}
/**
* Given two revisions, this figures out which one is greater than the
* the other.
*
* @param string $rev1 Revision string.
* @param string $rev2 Second revision string.
*
* @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 strcasecmp($rev1, $rev2);
}
/**
* TODO
*/
public function isFile($where)
{
return true;
}
/**
* Create a range of revisions between two revision numbers.
*
* @param Horde_Vcs_File_Base $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)
{
return array();
}
/**
* Obtain the differences between two revisions of a file.
*
* @param Horde_Vcs_File_Base $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:
* - 'human': (boolean) DEFAULT: false
* - 'num': (integer) DEFAULT: 3
* - 'type': (string) DEFAULT: 'unified'
* - 'ws': (boolean) DEFAULT: true
*
* @return string The diff string.
* @throws Horde_Vcs_Exception
*/
public function diff(Horde_Vcs_File_Base $file, $rev1, $rev2,
$opts = array())
{
$opts = array_merge(array(
'num' => 3,
'type' => 'unified',
'ws' => true
), $opts);
if ($rev1) {
$this->assertValidRevision($rev1);
}
$diff = $this->_diff($file, $rev1, $rev2, $opts);
return empty($opts['human'])
? $diff
: $this->_humanReadableDiff($diff);
}
/**
* Obtain the differences between two revisions of a file.
*
* @param Horde_Vcs_File_Base $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)
{
return false;
}
/**
* Obtain a tree containing information about the changes between
* two revisions.
*
* @param array $raw An array of lines of the raw unified diff,
* normally obtained through Horde_Vcs_Diff::get().
*
* @return array @TODO
*/
protected function _humanReadableDiff($raw)
{
$ret = array();
/* Hold the left and right columns of lines for change
* blocks. */
$cols = array(array(), array());
$state = 'empty';
/* Iterate through every line of the diff. */
foreach ($raw as $line) {
/* Look for a header which indicates the start of a diff
* chunk. */
if (preg_match('/^@@ \-([0-9]+).*\+([0-9]+).*@@(.*)/', $line, $regs)) {
/* Push any previous header information to the return
* stack. */
if (isset($data)) {
$ret[] = $data;
}
$data = array('type' => 'header', 'oldline' => $regs[1],
'newline' => $regs[2], 'contents'> array());
$data['function'] = isset($regs[3]) ? $regs[3] : '';
$state = 'dump';
} elseif ($state != 'empty') {
/* We are in a chunk, so split out the action (+/-)
* and the line. */
preg_match('/^([\+\- ])(.*)/', $line, $regs);
if (count($regs) > 2) {
$action = $regs[1];
$content = $regs[2];
} else {
$action = ' ';
$content = '';
}
if ($action == '+') {
/* This is just an addition line. */
if ($state == 'dump' || $state == 'add') {
/* Start adding to the addition stack. */
$cols[0][] = $content;
$state = 'add';
} else {
/* This is inside a change block, so start
* accumulating lines. */
$state = 'change';
$cols[1][] = $content;
}
} elseif ($action == '-') {
/* This is a removal line. */
$state = 'remove';
$cols[0][] = $content;
} else {
/* An empty block with no action. */
switch ($state) {
case 'add':
$data['contents'][] = array('type' => 'add', 'lines' => $cols[0]);
break;
case 'remove':
/* We have some removal lines pending in our
* stack, so flush them. */
$data['contents'][] = array('type' => 'remove', 'lines' => $cols[0]);
break;
case 'change':
/* We have both remove and addition lines, so
* this is a change block. */
$data['contents'][] = array('type' => 'change', 'old' => $cols[0], 'new' => $cols[1]);
break;
}
$cols = array(array(), array());
$data['contents'][] = array('type' => 'empty', 'line' => $content);
$state = 'dump';
}
}
}
/* Just flush any remaining entries in the columns stack. */
switch ($state) {
case 'add':
$data['contents'][] = array('type' => 'add', 'lines' => $cols[0]);
break;
case 'remove':
/* We have some removal lines pending in our stack, so
* flush them. */
$data['contents'][] = array('type' => 'remove', 'lines' => $cols[0]);
break;
case 'change':
/* We have both remove and addition lines, so this is a
* change block. */
$data['contents'][] = array('type' => 'change', 'old' => $cols[0], 'new' => $cols[1]);
break;
}
if (isset($data)) {
$ret[] = $data;
}
return $ret;
}
/**
* Return the list of available diff types.
*
* @return array The list of available diff types for use with get().
*/
public function availableDiffTypes()
{
return $this->_diffTypes;
}
/**
* Returns the location of the specified binary.
*
* @param string $binary An external program name.
*
* @return boolean|string The location of the external program or false
* if it wasn't specified.
*/
public function getPath($binary)
{
if (isset($this->_paths[$binary])) {
return $this->_paths[$binary];
}
return false;
}
/**
* Parse the users file, if present in the sourceroot, and return
* a hash containing the requisite information, keyed on the
* username, and with the 'desc', 'name', and 'mail' values inside.
*
* @return array User data.
* @throws Horde_Vcs_Exception
*/
public function getUsers($usersfile)
{
/* Check that we haven't already parsed users. */
if (isset($this->_users[$usersfile])) {
return $this->_users[$usersfile];
}
if (!@is_file($usersfile) ||
!($fl = @fopen($usersfile, VC_WINDOWS ? 'rb' : 'r'))) {
throw new Horde_Vcs_Exception('Invalid users file: ' . $usersfile);
}
/* Discard the first line, since it'll be the header info. */
fgets($fl, 4096);
/* Parse the rest of the lines into a hash, keyed on username. */
$users = array();
while ($line = fgets($fl, 4096)) {
if (!preg_match('/^\s*$/', $line) &&
preg_match('/^(\w+)\s+(.+)\s+([\w\.\-\_]+@[\w\.\-\_]+)\s+(.*)$/', $line, $regs)) {
$users[$regs[1]] = array(
'name' => trim($regs[2]),
'mail' => trim($regs[3]),
'desc' => trim($regs[4])
);
}
}
$this->_users[$usersfile] = $users;
return $users;
}
/**
* Returns a directory object.
*
* @param string $where Path to the directory.
* @param array $opts Any additional options (depends on driver):
* - 'showattic': (boolean) Parse any Attic/
* sub-directory contents too.
* - 'rev': (string) Generate directory list for
* a certain branch or revision.
*
* @return Horde_Vcs_Directory_Base A directory object.
*/
public function getDirectory($where, $opts = array())
{
$class = 'Horde_Vcs_Directory_' . $this->_driver;
return new $class($this, $where, $opts);
}
/**
* Function which 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.
*/
public function checkout($fullname, $rev)
{
return null;
}
/**
* TODO
*
* $opts:
* 'branch' - (string)
*/
public function getFile($filename, $opts = array())
{
$class = 'Horde_Vcs_File_' . $this->_driver;
ksort($opts);
$cacheId = implode('|', array($class, $this->sourceroot, $filename, serialize($opts), $this->_cacheVersion));
$fetchedFromCache = false;
if (!empty($this->_cache)) {
// TODO: Can't use filemtime() - Git bare repos contain no files
if (file_exists($filename)) {
$ctime = time() - filemtime($filename);
} else {
$ctime = 60;
}
if ($this->_cache->exists($cacheId, $ctime)) {
$ob = unserialize($this->_cache->get($cacheId, $ctime));
$fetchedFromCache = true;
}
}
if (empty($ob) || !$ob) {
$ob = new $class($filename, $opts);
}
$ob->setRepository($this);
if (!empty($this->_cache) && !$fetchedFromCache) {
$this->_cache->set($cacheId, serialize($ob));
}
return $ob;
}
/**
* TODO
*
* @param array $opts Options:
* - 'file': (string) The filename to process.
* REQUIRED for this driver.
* - 'range': (array) The patchsets to process.
* DEFAULT: None (all patchsets are processed).
* - 'timezone': (string) The current timezone.
*
* @return Horde_Vcs_Patchset Patchset object.
*/
public function getPatchset($opts = array())
{
$class = 'Horde_Vcs_Patchset_' . $this->_driver;
if (!is_array($opts)) {
$opts = array();
}
ksort($opts);
$cacheId = implode('|', array($class, $this->sourceroot, serialize($opts), $this->_cacheVersion));
if (!empty($this->_cache)) {
if (isset($opts['file']) && file_exists($opts['file'])) {
$ctime = time() - filemtime($opts['file']);
} else {
$ctime = 60;
}
if ($this->_cache->exists($cacheId, $ctime)) {
return unserialize($this->_cache->get($cacheId, $ctime));
}
}
$ob = new $class($this, $opts);
if (!empty($this->_cache)) {
$this->_cache->set($cacheId, serialize($ob));
}
return $ob;
}
/**
* TODO
*/
public function annotate($fileob, $rev)
{
return array();
}
/**
* Returns an abbreviated form of the revision, for display.
*
* @param string $rev The revision string.
*
* @return string The abbreviated string.
*/
public function abbrev($rev)
{
return $rev;
}
/**
* @TODO ?
*/
public function getDefaultBranch()
{
return 'HEAD';
}
}