Trigger an error on matching error info
[viewsvn.git] / includes / svnlib.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # ViewSVN [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * Command line interface with the SVN commands
15 *
16 * @package ViewSVN
17 */
18
19 /**
20 * Interacts with the command line subsystem to
21 * access SVN information
22 *
23 * @package ViewSVN
24 * @version $Id$
25 */
26 class SVNLib
27 {
28 /**
29 * Path to the SVN binary
30 * @var string
31 */
32 var $svnpath;
33
34 /**
35 * Common command system
36 * @var object
37 */
38 var $common;
39
40 /**
41 * Constructor: validate SVN path
42 *
43 * @param string Path to SVN binary
44 */
45 function SVNLib($svnpath)
46 {
47 global $viewsvn;
48
49 $this->svnpath = $viewsvn->shell->cmd($svnpath);
50
51 $this->common =& new SVNCommon();
52
53 $access = $viewsvn->shell->exec($this->svnpath . ' --version');
54
55 if (!$access)
56 {
57 $viewsvn->trigger->error('svn binary could not be found');
58 }
59
60 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
61 {
62 $viewsvn->trigger->error('svn binary does not pass test');
63 }
64 }
65
66 /**
67 * Prepares data for output
68 *
69 * @access public
70 *
71 * @param string Standard data
72 *
73 * @return string Output-ready data
74 */
75 function format($string)
76 {
77 // convert entities
78 $string = htmlspecialchars($string);
79
80 // tabs to 5 spaces
81 $string = str_replace("\t", ' ', $string);
82
83 // spaces to nbsp
84 $string = str_replace(' ', '&nbsp;', $string);
85
86 // convert advanced diff
87 $string = str_replace(array('{@++}', '{@--}'), array('<span class="diff_add">', '<span class="diff_del">'), $string);
88 $string = str_replace(array('{/@++}', '{/@--}'), '</span>', $string);
89
90 // nl2br
91 $string = nl2br($string);
92
93 return $string;
94 }
95
96 /**
97 * Executes the SVN binary
98 *
99 * @access private
100 *
101 * @param string Command
102 *
103 * @return array Output
104 */
105 function svn($command)
106 {
107 global $viewsvn;
108
109 $output = $viewsvn->shell->exec($this->svnpath . ' ' . $command . ' 2>&1');
110 $temp = implode("\n", $output);
111 if (strpos($temp, 'apr_error=') !== false OR strpos($temp, 'svn:') !== false)
112 {
113 $viewsvn->trigger->error(nl2br($temp));
114 }
115 return $output;
116 }
117
118 /**
119 * SVN Wrapper: standard command system
120 *
121 * @access private
122 *
123 * @param string SVN command
124 * @param string Repository
125 * @param string Path
126 * @param integer Revision
127 *
128 * @return array Lines of output
129 */
130 function std($command, $repos, $path, $revision)
131 {
132 global $viewsvn;
133
134 $revision = $this->rev($revision);
135 $repospath = $viewsvn->repos->fetch_path($repos, false);
136
137 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
138 }
139
140 /**
141 * SVN Wrapper: blame
142 *
143 * @access protected
144 *
145 * @param string Repository
146 * @param string Path
147 * @param integer Revision
148 *
149 * @return array Lines of blame output
150 */
151 function blame($repos, $path, $revision)
152 {
153 return $this->std('blame', $repos, $path, $revision);
154 }
155
156 /**
157 * SVN Wrapper: cat
158 *
159 * @access protected
160 *
161 * @param string Repository
162 * @param string Path
163 * @param integer Revision
164 *
165 * @return array Lines of cat output
166 */
167 function cat($repos, $path, $revision)
168 {
169 global $viewsvn;
170
171 $repospath = $viewsvn->repos->fetch_path($repos, false);
172
173 return $this->svn('cat ' . $repospath . $path . '@' . $revision);
174 }
175
176 /**
177 * SVN Wrapper: diff
178 *
179 * @access protected
180 *
181 * @param string Repository
182 * @param string Path
183 * @param integer Lower revision
184 * @param integer Higher revision
185 *
186 * @return array Lines of diff output
187 */
188 function diff($repos, $path, $lorev, $hirev)
189 {
190 global $viewsvn;
191
192 $hirev = $this->rev($hirev);
193 $lorev = $this->rev($lorev);
194 if ($lorev == 'HEAD')
195 {
196 $lorev = 1;
197 }
198
199 if (is_integer($hirev) AND is_integer($lorev))
200 {
201 if ($lorev > $hirev)
202 {
203 $lorev = $hirev - 1;
204 }
205 if ($lorev == $hirev)
206 {
207 $lorev = 0;
208 }
209 }
210
211 $repospath = $viewsvn->repos->fetch_path($repos, false);
212
213 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
214 }
215
216 /**
217 * SVN Wrapper: log
218 *
219 * @access protected
220 *
221 * @param string Repository
222 * @param string Path
223 * @param integer Lower revision
224 * @param integer Higher revision
225 *
226 * @return array Lines of log output
227 */
228 function log($repos, $path, $lorev, $hirev)
229 {
230 global $viewsvn;
231
232 $hirev = $this->rev($hirev);
233 $lorev = $this->rev($hirev);
234 if ($lorev == 'HEAD')
235 {
236 $lorev = 0;
237 }
238
239 if (is_integer($hirev) AND is_integer($lorev))
240 {
241 if ($lorev > $hirev)
242 {
243 $lorev = $hirev - 1;
244 }
245 if ($lorev == $hirev)
246 {
247 $lorev = 0;
248 }
249 }
250
251 $repospath = $viewsvn->repos->fetch_path($repos, false);
252
253 return $this->svn('log -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
254 }
255
256 /**
257 * SVN Wrapper: ls (list)
258 *
259 * @access protected
260 *
261 * @param string Repository
262 * @param string Path
263 * @param integer Revision
264 *
265 * @return array Lines of list output
266 */
267 function ls($repos, $path, $revision)
268 {
269 return $this->std('list', $repos, $path, $revision);
270 }
271
272 /**
273 * Generates a clean revision number
274 *
275 * @access public
276 *
277 * @param integer Revision number
278 *
279 * @return mixed Cleaned revision or HEAD
280 */
281 function rev($revision)
282 {
283 if (($revision = intval($revision)) < 1)
284 {
285 $revision = 'HEAD';
286 }
287 return $revision;
288 }
289 }
290
291 /**
292 * Commonly executed SVN commands that return data
293 * used in many parts of the system
294 *
295 * @package ViewSVN
296 * @version $Id$
297 */
298 class SVNCommon
299 {
300 /**
301 * Registry object
302 * @var object
303 */
304 var $registry;
305
306 /**
307 * Constructor: bind with registry
308 */
309 function SVNCommon()
310 {
311 global $viewsvn;
312
313 $this->registry =& $viewsvn;
314 }
315
316 /**
317 * Checks to see if the given universal path is
318 * a directory
319 *
320 * @access public
321 *
322 * @param string Universal path
323 *
324 * @return bool Directory or not
325 */
326 function isdir($path)
327 {
328 $output = $this->registry->svn->cat($this->registry->paths->fetch_repos($path), $this->registry->paths->fetch_path($path), 'HEAD');
329 $output = implode("\n", $output);
330 if (strpos($output, 'svn:') === false)
331 {
332 return false;
333 }
334 else
335 {
336 return true;
337 }
338 }
339 }
340
341 /**
342 * Annotation/blame system; constructs an array
343 * that is ready for output
344 *
345 * @package ViewSVN
346 * @version $Id$
347 */
348 class SVNBlame
349 {
350 /**
351 * Array of blame information
352 * @var array
353 */
354 var $blame = array();
355
356 /**
357 * Raw "svn blame" output
358 * @var array
359 */
360 var $rawoutput;
361
362 /**
363 * Constructor: create blame and store data
364 *
365 * @param string Repository
366 * @param string Path
367 * @param integer Revision
368 */
369 function SVNBlame($repos, $path, $revision)
370 {
371 global $viewsvn;
372
373 $this->rawoutput = $viewsvn->svn->blame($repos, $path, $revision);
374 $this->process();
375 }
376
377 /**
378 * Returns blame for display
379 *
380 * @access public
381 *
382 * @return array Blame data
383 */
384 function fetch()
385 {
386 return $this->blame;
387 }
388
389 /**
390 * Parses the blame data
391 *
392 * @access private
393 */
394 function process()
395 {
396 $lineno = 1;
397
398 foreach ($this->rawoutput AS $line)
399 {
400 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
401 {
402 $this->blame[] = array(
403 'rev' => $matches[1],
404 'author' => $matches[2],
405 'line' => $matches[3],
406 'lineno' => $lineno++
407 );
408 }
409 // a blank line
410 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
411 {
412 $this->blame[] = array(
413 'rev' => $matches[1],
414 'author' => $matches[2],
415 'line' => '',
416 'lineno' => $lineno++
417 );
418 }
419 }
420 }
421 }
422
423 /**
424 * Log management system; creates a complex list
425 * of SVN log information
426 *
427 * @package ViewSVN
428 * @version $Id$
429 */
430 class SVNLog
431 {
432 /**
433 * Array of logs
434 * @var array
435 */
436 var $logs = array();
437
438 /**
439 * Raw "svn log" output
440 * @var array
441 */
442 var $rawoutput;
443
444 /**
445 * Constructor: create log store for the given file
446 *
447 * @param string Repository
448 * @param string Path
449 * @param integer Lower revision
450 * @param integer Higher revision
451 */
452 function SVNLog($repos, $path, $lorev, $hirev)
453 {
454 global $viewsvn;
455
456 $this->rawoutput = $viewsvn->svn->log($repos, $path, $lorev, $hirev);
457 $this->process();
458 }
459
460 /**
461 * Returns logs for display
462 *
463 * @access public
464 *
465 * @return array Log data
466 */
467 function fetch()
468 {
469 return $this->logs;
470 }
471
472 /**
473 * Splits up the raw output into a usable log
474 *
475 * @access private
476 */
477 function process()
478 {
479 $lastrev = 0;
480
481 for ($i = 1; $i <= count($this->rawoutput) - 1; $i++)
482 {
483 $line = $this->rawoutput["$i"];
484
485 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
486 {
487 if (isset($this->logs["$lastrev"]))
488 {
489 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
490 }
491
492 $this->logs["$matches[1]"] = array(
493 'rev' => $matches[1],
494 'author' => $matches[2],
495 'date' => $matches[3],
496 'timezone' => $matches[4],
497 'lines' => $matches[6],
498 'message' => ''
499 );
500
501 $lastrev = $matches[1];
502 }
503 else
504 {
505 $this->logs["$lastrev"]['message'] .= $line . "\n";
506 }
507 }
508
509 if (isset($this->logs["$lastrev"]))
510 {
511 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
512 }
513 }
514
515 /**
516 * Trims the last dash line off a message
517 *
518 * @access private
519 *
520 * @param string Message with annoying-ass line
521 *
522 * @return string Clean string
523 */
524 function strip_last_line($string)
525 {
526 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
527 }
528 }
529
530 /**
531 * Diff system; constructs a diff array that
532 * is ready for output
533 *
534 * @package ViewSVN
535 */
536 class SVNDiff
537 {
538 /**
539 * Array of diff information
540 * @var array
541 */
542 var $diff = array();
543
544 /**
545 * Raw "svn diff" output
546 * @var array
547 */
548 var $rawoutput;
549
550 /**
551 * Constructor: create and store diff data
552 *
553 * @param string Repository
554 * @param string Path
555 * @param integer Lower revision
556 * @param integer Higher revision
557 */
558 function SVNDiff($repos, $path, $lorev, $hirev)
559 {
560 global $viewsvn;
561
562 $this->rawoutput = $viewsvn->svn->diff($repos, $path, $lorev, $hirev);
563 $this->process();
564 }
565
566 /**
567 * Returns diffs for display
568 *
569 * @access public
570 *
571 * @return array Diff data
572 */
573 function fetch()
574 {
575 return $this->diff;
576 }
577
578 /**
579 * Processes and prepares diff data
580 *
581 * @access private
582 */
583 function process()
584 {
585 $chunk = 0;
586 $indexcounter = null;
587
588 $lastact = '';
589 $lastcontent = '';
590
591 foreach ($this->rawoutput AS $line)
592 {
593 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
594 {
595 $lastact = '';
596 $lastcontent = '';
597
598 $this->diff["$index"][ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
599 $lines['old'] = $this->diff["$index"]["$chunk"]['hunk']['old']['line'] - 1;
600 $lines['new'] = $this->diff["$index"]["$chunk"]['hunk']['new']['line'] - 1;
601 continue;
602 }
603
604 if ($indexcounter <= 5 AND $indexcounter !== null)
605 {
606 $indexcounter++;
607 continue;
608 }
609 else if ($indexcounter == 5)
610 {
611 $indexcounter = null;
612 continue;
613 }
614
615 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
616 {
617 $act = $matches[1];
618 $content = $matches[2];
619
620 if ($act == ' ')
621 {
622 $this->diff["$index"]["$chunk"][] = array(
623 'line' => $content,
624 'act' => '',
625 'oldlineno' => ++$lines['old'],
626 'newlineno' => ++$lines['new']
627 );
628 }
629 else if ($act == '+')
630 {
631 // potential line delta
632 if ($lastact == '-')
633 {
634 if ($delta = @$this->fetch_diff_extent($lastcontent, $content))
635 {
636 // create two sets of ends for the two contents
637 $delta['endo'] = strlen($lastcontent) - $delta['end'];
638 $delta['endn'] = strlen($content) - $delta['end'];
639
640 $diffo = $delta['endo'] - $delta['start'];
641 $diffn = $delta['endn'] - $delta['start'];
642
643 if (strlen($lastcontent) > $delta['endo'] - $diffo)
644 {
645 $removed = substr($lastcontent, $delta['start'], $diffo);
646 $this->diff["$index"]["$chunk"][ count($this->diff["$index"]["$chunk"]) - 2 ]['line'] = substr_replace($lastcontent, '{@--}' . $removed . '{/@--}', $delta['start'], $diffo);
647 }
648
649 if (strlen($content) > $delta['endn'] - $diffn)
650 {
651 $added = substr($content, $delta['start'], $diffn);
652 $content = substr_replace($content, '{@++}' . $added . '{/@++}', $delta['start'], $diffn);
653 }
654 }
655 }
656
657 $this->diff["$index"]["$chunk"][] = array(
658 'line' => $content,
659 'act' => '+',
660 'oldlineno' => '',
661 'newlineno' => ++$lines['new']
662 );
663 }
664 else if ($act == '-')
665 {
666 $lastcontent = $content;
667
668 $this->diff["$index"]["$chunk"][] = array(
669 'line' => $content,
670 'act' => '-',
671 'oldlineno' => ++$lines['old'],
672 'newlineno' => ''
673 );
674 }
675
676 $lastact = $act;
677 }
678 // whitespace lines
679 else
680 {
681 if (preg_match('#^Index: (.*?)$#', $line, $matches))
682 {
683 $index = $matches[1];
684 $indexcounter = 1;
685 $chunk = 0;
686 continue;
687 }
688
689 $lastact = '';
690
691 $this->diff["$index"]["$chunk"][] = array(
692 'line' => '',
693 'act' => '',
694 'oldlineno' => ++$lines['old'],
695 'newlineno' => ++$lines['new']
696 );
697 }
698 }
699 }
700
701 /**
702 * Returns the amount of change that occured
703 * between two lines
704 *
705 * @access private
706 *
707 * @param string Old line
708 * @param string New line
709 *
710 * @return array Difference of positions
711 */
712 function fetch_diff_extent($old, $new)
713 {
714 $start = 0;
715 $min = min(strlen($old), strlen($new));
716
717 for ($start = 0; $start < $min; $start++)
718 {
719 if ($old{"$start"} != $new{"$start"})
720 {
721 break;
722 }
723 }
724
725 $max = max(strlen($old), strlen($new));
726
727 for ($end = 0; $end < $min; $end++)
728 {
729 $oldpos = strlen($old) - $end;
730 $newpos = strlen($new) - $end;
731
732 if ($old{"$oldpos"} != $new{"$newpos"})
733 {
734 break;
735 }
736 }
737
738 $end--;
739
740 if ($start == 0 AND $end == $max)
741 {
742 return false;
743 }
744
745 return array('start' => $start, 'end' => $end);
746 }
747 }
748
749 /*=====================================================================*\
750 || ###################################################################
751 || # $HeadURL$
752 || # $Id$
753 || ###################################################################
754 \*=====================================================================*/
755 ?>