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