]> src.bluestatic.org Git - viewsvn.git/blob - includes/svnlib.php
Yet another escape fixer
[viewsvn.git] / includes / svnlib.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Command line interface with the SVN commands
24 *
25 * @package ViewSVN
26 */
27
28 /**
29 * Interacts with the command line subsystem to
30 * access SVN information
31 *
32 * @package ViewSVN
33 * @version $Id$
34 */
35 class SVNLib
36 {
37 /**
38 * Path to the SVN binary
39 * @var string
40 */
41 var $svnpath;
42
43 /**
44 * Common command system
45 * @var object
46 */
47 var $common;
48
49 /**
50 * Constructor: validate SVN path
51 *
52 * @param string Path to SVN binary
53 */
54 function SVNLib($svnpath)
55 {
56 global $viewsvn;
57
58 $this->svnpath = $viewsvn->shell->cmd($svnpath);
59
60 $this->common =& new SVNCommon();
61
62 $access = $viewsvn->shell->exec($this->svnpath . ' --version');
63
64 if (!$access)
65 {
66 $viewsvn->trigger->error('svn binary could not be found');
67 }
68
69 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
70 {
71 $viewsvn->trigger->error('svn binary does not pass test');
72 }
73 }
74
75 /**
76 * Prepares data for output
77 *
78 * @access public
79 *
80 * @param string Standard data
81 *
82 * @return string Output-ready data
83 */
84 function format($string)
85 {
86 // convert entities
87 $string = htmlspecialchars($string);
88
89 // tabs to 5 spaces
90 $string = str_replace("\t", ' ', $string);
91
92 // spaces to nbsp
93 if (true)
94 {
95 $string = preg_replace('#( +)#e', '$this->format_spaces("\1")', $string);
96 }
97 // no word wrap
98 else
99 {
100 $string = str_replace(' ', '&nbsp;', $string);
101 }
102
103 // convert advanced diff
104 $string = str_replace(array('{@+' . '+}', '{@-' . '-}'), array('<span class="diff_add_bit">', '<span class="diff_del_bit">'), $string);
105 $string = str_replace(array('{/@+' . '+}', '{/@-' . '-}'), '</span>', $string);
106
107 // nl2br
108 $string = nl2br($string);
109
110 return $string;
111 }
112
113 /**
114 * Counts the spaces and replaces two or more ones
115 *
116 * @access private
117 *
118 * @param string Spaced string
119 *
120 * @return string &nbsp;'d string
121 */
122 function format_spaces($thestring)
123 {
124 if (strlen($thestring) >= 2)
125 {
126 $thestring = str_replace(' ', '&nbsp;', $thestring);
127 }
128
129 return $thestring;
130 }
131
132 /**
133 * Executes the SVN binary
134 *
135 * @access private
136 *
137 * @param string Command
138 *
139 * @return array Output
140 */
141 function svn($command)
142 {
143 global $viewsvn;
144
145 $output = $viewsvn->shell->exec($this->svnpath . ' ' . $command . ' 2>&1');
146
147 // make sure that we keep escaped chars
148 //$output = str_replace(array('\t', '\n', '\r'), array('\\\t', '\\\n', '\\\r'), $output);
149 //$output = preg_replace('#\\\(.)#', '\\\\\\\\' . '\1', $output);
150 $output = str_replace('\\', '\\\\', $output);
151
152 $temp = implode("\n", $output);
153 if (strpos($temp, '(apr' . '_err=') !== false)
154 {
155 $viewsvn->trigger->error(nl2br($temp));
156 }
157 return $output;
158 }
159
160 /**
161 * SVN Wrapper: standard command system
162 *
163 * @access private
164 *
165 * @param string SVN command
166 * @param string Repository
167 * @param string Path
168 * @param integer Revision
169 *
170 * @return array Lines of output
171 */
172 function std($command, $repos, $path, $revision)
173 {
174 global $viewsvn;
175
176 $revision = $this->rev($revision);
177 $repospath = $viewsvn->repos->fetch_path($repos, false);
178
179 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
180 }
181
182 /**
183 * SVN Wrapper: blame
184 *
185 * @access protected
186 *
187 * @param string Repository
188 * @param string Path
189 * @param integer Revision
190 *
191 * @return array Lines of blame output
192 */
193 function blame($repos, $path, $revision)
194 {
195 return $this->std('blame', $repos, $path, $revision);
196 }
197
198 /**
199 * SVN Wrapper: cat
200 *
201 * @access protected
202 *
203 * @param string Repository
204 * @param string Path
205 * @param integer Revision
206 *
207 * @return array Lines of cat output
208 */
209 function cat($repos, $path, $revision)
210 {
211 return $this->std('cat', $repos, $path, $revision);
212 }
213
214 /**
215 * SVN Wrapper: diff
216 *
217 * @access protected
218 *
219 * @param string Repository
220 * @param string Path
221 * @param integer Lower revision
222 * @param integer Higher revision
223 *
224 * @return array Lines of diff output
225 */
226 function diff($repos, $path, $lorev, $hirev)
227 {
228 global $viewsvn;
229
230 $hirev = $this->rev($hirev);
231 $lorev = $this->rev($lorev);
232 if ($lorev == 'HEAD')
233 {
234 $lorev = 1;
235 }
236
237 if (is_integer($hirev) AND is_integer($lorev))
238 {
239 if ($lorev > $hirev)
240 {
241 $lorev = $hirev - 1;
242 }
243 if ($lorev == $hirev)
244 {
245 $lorev = 0;
246 }
247 }
248
249 $repospath = $viewsvn->repos->fetch_path($repos, false);
250
251 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
252 }
253
254 /**
255 * SVN Wrapper: log
256 *
257 * @access protected
258 *
259 * @param string Repository
260 * @param string Path
261 * @param integer Lower revision
262 * @param integer Higher revision
263 *
264 * @return array Lines of log output
265 */
266 function log($repos, $path, $lorev, $hirev)
267 {
268 global $viewsvn;
269
270 $hirev = $this->rev($hirev);
271 $lorev = $this->rev($hirev);
272 if ($lorev == 'HEAD')
273 {
274 $lorev = 0;
275 }
276
277 if (is_integer($hirev) AND is_integer($lorev))
278 {
279 if ($lorev > $hirev)
280 {
281 $lorev = $hirev - 1;
282 }
283 if ($lorev == $hirev)
284 {
285 $lorev = 0;
286 }
287 }
288
289 $repospath = $viewsvn->repos->fetch_path($repos, false);
290
291 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
292 }
293
294 /**
295 * SVN Wrapper: ls (list)
296 *
297 * @access protected
298 *
299 * @param string Repository
300 * @param string Path
301 * @param integer Revision
302 *
303 * @return array Lines of list output
304 */
305 function ls($repos, $path, $revision)
306 {
307 return $this->std('list', $repos, $path, $revision);
308 }
309
310 /**
311 * Generates a clean revision number
312 *
313 * @access public
314 *
315 * @param integer Revision number
316 *
317 * @return mixed Cleaned revision or HEAD
318 */
319 function rev($revision)
320 {
321 if (($revision = intval($revision)) < 1)
322 {
323 $revision = 'HEAD';
324 }
325 return $revision;
326 }
327 }
328
329 /**
330 * Commonly executed SVN commands that return data
331 * used in many parts of the system
332 *
333 * @package ViewSVN
334 * @version $Id$
335 */
336 class SVNCommon
337 {
338 /**
339 * Registry object
340 * @var object
341 */
342 var $registry;
343
344 /**
345 * List of revisions
346 * @var array
347 */
348 var $revisions;
349
350 /**
351 * List of logs
352 * @var array
353 */
354 var $logs;
355
356 /**
357 * Constructor: bind with registry
358 */
359 function SVNCommon()
360 {
361 global $viewsvn;
362
363 $this->registry =& $viewsvn;
364 }
365
366 /**
367 * Checks to see if the given universal path is
368 * a directory
369 *
370 * @access public
371 *
372 * @param string Universal path
373 *
374 * @return bool Directory or not
375 */
376 function isdir($path)
377 {
378 $output = $this->registry->svn->std('info', $this->registry->paths->fetch_repos($path), $this->registry->paths->fetch_path($path), 'HEAD');
379
380 foreach ($output AS $line)
381 {
382 if (preg_match('#^Node Kind: (.*)#', $line, $matches))
383 {
384 if (trim(strtolower($matches[1])) == 'directory')
385 {
386 return true;
387 }
388 }
389 }
390
391 return false;
392 }
393
394 /**
395 * Get a list of revisions for a path
396 *
397 * @access public
398 *
399 * @param string Universal path
400 *
401 * @return array Key revisions
402 */
403 function fetch_revs($path)
404 {
405 if (!isset($this->revisions["$path"]))
406 {
407 $log = $this->fetch_logs($path);
408
409 $revs = array_keys($log);
410
411 $this->revisions["$path"] = array(
412 'HEAD' => $revs[0],
413 'START' => $revs[ count($revs) - 1 ],
414 'revs' => $revs
415 );
416 }
417
418 return $this->revisions["$path"];
419 }
420
421 /**
422 * Gets the revision that is marked as HEAD
423 *
424 * @access public
425 *
426 * @param string Universal path
427 *
428 * @return integer Revision
429 */
430 function fetch_head_rev($path)
431 {
432 $revs = $this->fetch_revs($path);
433 return $revs['HEAD'];
434 }
435
436 /**
437 * Returns the previous revision to the one given
438 *
439 * @access public
440 *
441 * @param string Universal path
442 * @param integer Arbitrary revision
443 *
444 * @return integer Previous revision (-1 if none)
445 */
446 function fetch_prev_rev($path, $current)
447 {
448 $revs = $this->fetch_revs($path);
449
450 if ($current == 'HEAD')
451 {
452 $current = $this->fetch_head_rev($path);
453 }
454
455 $index = array_search($current, $revs['revs']);
456 if ($current === false)
457 {
458 $message->trigger->error('revision ' . $current . ' is not in ' . $path);
459 }
460
461 if (isset($revs['revs'][ $index + 1 ]))
462 {
463 return $revs['revs'][ $index + 1 ];
464 }
465 else
466 {
467 return -1;
468 }
469 }
470
471 /**
472 * Get a list of logs
473 *
474 * @access public
475 *
476 * @param string Universal path
477 *
478 * @return array Log data
479 */
480 function fetch_logs($path)
481 {
482 if (!isset($this->logs["$path"]))
483 {
484 $log = new SVNLog($this->registry->paths->fetch_repos($path), $this->registry->paths->fetch_path($path), 0, 0);
485
486 $this->logs["$path"] = $log->fetch();
487 }
488
489 return $this->logs["$path"];
490 }
491
492 /**
493 * Returns a given log entry for a path
494 * and revision
495 *
496 * @access public
497 *
498 * @param string Universal path
499 * @param integer Arbitrary revision
500 *
501 * @return array Log entry
502 */
503 function fetch_log($path, $rev)
504 {
505 $logs = $this->fetch_logs($path);
506
507 $rev = $this->registry->svn->rev($rev);
508 if ($rev == 'HEAD')
509 {
510 $rev = $this->fetch_head_rev($path);
511 }
512
513 if (isset($logs["$rev"]))
514 {
515 return $logs["$rev"];
516 }
517 else
518 {
519 return null;
520 }
521 }
522 }
523
524 /**
525 * Annotation/blame system; constructs an array
526 * that is ready for output
527 *
528 * @package ViewSVN
529 * @version $Id$
530 */
531 class SVNBlame
532 {
533 /**
534 * Array of blame information
535 * @var array
536 */
537 var $blame = array();
538
539 /**
540 * Raw "svn blame" output
541 * @var array
542 */
543 var $rawoutput;
544
545 /**
546 * Constructor: create blame and store data
547 *
548 * @param string Repository
549 * @param string Path
550 * @param integer Revision
551 */
552 function SVNBlame($repos, $path, $revision)
553 {
554 global $viewsvn;
555
556 $this->rawoutput = $viewsvn->svn->blame($repos, $path, $revision);
557 $this->process();
558 }
559
560 /**
561 * Returns blame for display
562 *
563 * @access public
564 *
565 * @return array Blame data
566 */
567 function fetch()
568 {
569 return $this->blame;
570 }
571
572 /**
573 * Parses the blame data
574 *
575 * @access private
576 */
577 function process()
578 {
579 $lineno = 1;
580
581 foreach ($this->rawoutput AS $line)
582 {
583 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
584 {
585 $this->blame[] = array(
586 'rev' => $matches[1],
587 'author' => $matches[2],
588 'line' => $matches[3],
589 'lineno' => $lineno++
590 );
591 }
592 // a blank line
593 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
594 {
595 $this->blame[] = array(
596 'rev' => $matches[1],
597 'author' => $matches[2],
598 'line' => '',
599 'lineno' => $lineno++
600 );
601 }
602 }
603 }
604 }
605
606 /**
607 * Log management system; creates a complex list
608 * of SVN log information
609 *
610 * @package ViewSVN
611 * @version $Id$
612 */
613 class SVNLog
614 {
615 /**
616 * Array of logs
617 * @var array
618 */
619 var $logs = array();
620
621 /**
622 * Raw "svn log" output
623 * @var array
624 */
625 var $rawoutput;
626
627 /**
628 * Constructor: create log store for the given file
629 *
630 * @param string Repository
631 * @param string Path
632 * @param integer Lower revision
633 * @param integer Higher revision
634 */
635 function SVNLog($repos, $path, $lorev, $hirev)
636 {
637 global $viewsvn;
638
639 $this->rawoutput = $viewsvn->svn->log($repos, $path, $lorev, $hirev);
640 $this->process();
641 }
642
643 /**
644 * Returns logs for display
645 *
646 * @access public
647 *
648 * @return array Log data
649 */
650 function fetch()
651 {
652 return $this->logs;
653 }
654
655 /**
656 * Splits up the raw output into a usable log
657 *
658 * @access private
659 */
660 function process()
661 {
662 $lastrev = 0;
663
664 for ($i = 1; $i <= count($this->rawoutput) - 1; $i++)
665 {
666 $line = $this->rawoutput["$i"];
667
668 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
669 {
670 if (isset($this->logs["$lastrev"]))
671 {
672 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
673 }
674
675 $this->logs["$matches[1]"] = array(
676 'rev' => $matches[1],
677 'author' => $matches[2],
678 'date' => $matches[3],
679 'timezone' => $matches[4],
680 'lines' => $matches[6],
681 'message' => ''
682 );
683
684 $lastrev = $matches[1];
685 }
686 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
687 {
688 $this->logs["$lastrev"]['files'][] = array(
689 'action' => $matches[1],
690 'file' => $matches[2]
691 );
692 }
693 else
694 {
695 if (trim($line) != 'Changed paths:')
696 {
697 $this->logs["$lastrev"]['message'] .= $line . "\n";
698 }
699 }
700 }
701
702 if (isset($this->logs["$lastrev"]))
703 {
704 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
705 }
706 }
707
708 /**
709 * Trims the last dash line off a message
710 *
711 * @access private
712 *
713 * @param string Message with annoying-ass line
714 *
715 * @return string Clean string
716 */
717 function strip_last_line($string)
718 {
719 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
720 }
721 }
722
723 /**
724 * Diff system; constructs a diff array that
725 * is ready for output
726 *
727 * @package ViewSVN
728 */
729 class SVNDiff
730 {
731 /**
732 * Array of diff information
733 * @var array
734 */
735 var $diff = array();
736
737 /**
738 * Raw "svn diff" output
739 * @var array
740 */
741 var $rawoutput;
742
743 /**
744 * Constructor: create and store diff data
745 *
746 * @param string Repository
747 * @param string Path
748 * @param integer Lower revision
749 * @param integer Higher revision
750 */
751 function SVNDiff($repos, $path, $lorev, $hirev)
752 {
753 global $viewsvn;
754
755 $this->rawoutput = $viewsvn->svn->diff($repos, $path, $lorev, $hirev);
756 $this->process();
757 }
758
759 /**
760 * Returns diffs for display
761 *
762 * @access public
763 *
764 * @return array Diff data
765 */
766 function fetch()
767 {
768 return $this->diff;
769 }
770
771 /**
772 * Processes and prepares diff data
773 *
774 * @access private
775 */
776 function process()
777 {
778 $chunk = 0;
779 $indexcounter = null;
780
781 $lastact = '';
782 $lastcontent = '';
783
784 foreach ($this->rawoutput AS $line)
785 {
786 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
787 {
788 $lastact = '';
789 $lastcontent = '';
790
791 $this->diff["$index"][ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
792 $lines['old'] = $this->diff["$index"]["$chunk"]['hunk']['old']['line'] - 1;
793 $lines['new'] = $this->diff["$index"]["$chunk"]['hunk']['new']['line'] - 1;
794 continue;
795 }
796
797 if ($indexcounter <= 5 AND $indexcounter !== null)
798 {
799 $indexcounter++;
800 continue;
801 }
802 else if ($indexcounter == 5)
803 {
804 $indexcounter = null;
805 continue;
806 }
807
808 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
809 {
810 $act = $matches[1];
811 $content = $matches[2];
812
813 if ($act == ' ')
814 {
815 $this->diff["$index"]["$chunk"][] = array(
816 'line' => $content,
817 'act' => '',
818 'oldlineno' => ++$lines['old'],
819 'newlineno' => ++$lines['new']
820 );
821 }
822 else if ($act == '+')
823 {
824 // potential line delta
825 if ($lastact == '-')
826 {
827 if ($delta = @$this->fetch_diff_extent($lastcontent, $content))
828 {
829 // create two sets of ends for the two contents
830 $delta['endo'] = strlen($lastcontent) - $delta['end'];
831 $delta['endn'] = strlen($content) - $delta['end'];
832
833 $diffo = $delta['endo'] - $delta['start'];
834 $diffn = $delta['endn'] - $delta['start'];
835
836 if (strlen($lastcontent) > $delta['endo'] - $diffo)
837 {
838 $removed = substr($lastcontent, $delta['start'], $diffo);
839 $this->diff["$index"]["$chunk"][ count($this->diff["$index"]["$chunk"]) - 2 ]['line'] = substr_replace($lastcontent, '{@-' . '-}' . $removed . '{/@-' . '-}', $delta['start'], $diffo);
840 }
841
842 if (strlen($content) > $delta['endn'] - $diffn)
843 {
844 $added = substr($content, $delta['start'], $diffn);
845 $content = substr_replace($content, '{@+' . '+}' . $added . '{/@+' . '+}', $delta['start'], $diffn);
846 }
847 }
848 }
849
850 $this->diff["$index"]["$chunk"][] = array(
851 'line' => $content,
852 'act' => '+',
853 'oldlineno' => '',
854 'newlineno' => ++$lines['new']
855 );
856 }
857 else if ($act == '-')
858 {
859 $lastcontent = $content;
860
861 $this->diff["$index"]["$chunk"][] = array(
862 'line' => $content,
863 'act' => '-',
864 'oldlineno' => ++$lines['old'],
865 'newlineno' => ''
866 );
867 }
868
869 $lastact = $act;
870 }
871 // whitespace lines
872 else
873 {
874 if (preg_match('#^Index: (.*?)$#', $line, $matches))
875 {
876 $index = $matches[1];
877 $indexcounter = 1;
878 $chunk = 0;
879 continue;
880 }
881
882 $lastact = '';
883
884 $this->diff["$index"]["$chunk"][] = array(
885 'line' => '',
886 'act' => '',
887 'oldlineno' => ++$lines['old'],
888 'newlineno' => ++$lines['new']
889 );
890 }
891 }
892 }
893
894 /**
895 * Returns the amount of change that occured
896 * between two lines
897 *
898 * @access private
899 *
900 * @param string Old line
901 * @param string New line
902 *
903 * @return array Difference of positions
904 */
905 function fetch_diff_extent($old, $new)
906 {
907 $start = 0;
908 $min = min(strlen($old), strlen($new));
909
910 for ($start = 0; $start < $min; $start++)
911 {
912 if ($old{"$start"} != $new{"$start"})
913 {
914 break;
915 }
916 }
917
918 $max = max(strlen($old), strlen($new));
919
920 for ($end = 0; $end < $min; $end++)
921 {
922 $oldpos = strlen($old) - $end;
923 $newpos = strlen($new) - $end;
924
925 if ($old{"$oldpos"} != $new{"$newpos"})
926 {
927 break;
928 }
929 }
930
931 $end--;
932
933 if ($start == 0 AND $end == $max)
934 {
935 return false;
936 }
937
938 return array('start' => $start, 'end' => $end);
939 }
940 }
941
942 /*=====================================================================*\
943 || ###################################################################
944 || # $HeadURL$
945 || # $Id$
946 || ###################################################################
947 \*=====================================================================*/
948 ?>