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