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 \*=====================================================================*/
14 * Command line interface with the SVN commands
20 * Interacts with the command line subsystem to
21 * access SVN information
29 * Path to the SVN binary
35 * Common command system
41 * Constructor: validate SVN path
43 * @param string Path to SVN binary
45 function SVNLib($svnpath)
49 $this->svnpath
= $viewsvn->shell
->cmd($svnpath);
51 $this->common
=& new SVNCommon();
53 $access = $viewsvn->shell
->exec($this->svnpath
. ' --version');
57 $viewsvn->trigger
->error('svn binary could not be found');
60 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
62 $viewsvn->trigger
->error('svn binary does not pass test');
67 * Prepares data for output
71 * @param string Standard data
73 * @return string Output-ready data
75 function format($string)
78 $string = htmlspecialchars($string);
81 $string = str_replace("\t", ' ', $string);
84 $string = str_replace(' ', ' ', $string);
86 // convert advanced diff
87 $string = str_replace(array('{@++}', '{@--}'), array('<span class="diff_add_bit">', '<span class="diff_del_bit">'), $string);
88 $string = str_replace(array('{/@++}', '{/@--}'), '</span>', $string);
91 $string = nl2br($string);
97 * Executes the SVN binary
101 * @param string Command
103 * @return array Output
105 function svn($command)
109 $output = $viewsvn->shell
->exec($this->svnpath
. ' ' . $command . ' 2>&1');
110 $temp = implode("\n", $output);
111 if (strpos($temp, '(apr' . '_err=') !== false
)
113 $viewsvn->trigger
->error(nl2br($temp));
119 * SVN Wrapper: standard command system
123 * @param string SVN command
124 * @param string Repository
126 * @param integer Revision
128 * @return array Lines of output
130 function std($command, $repos, $path, $revision)
134 $revision = $this->rev($revision);
135 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
137 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
145 * @param string Repository
147 * @param integer Revision
149 * @return array Lines of blame output
151 function blame($repos, $path, $revision)
153 return $this->std('blame', $repos, $path, $revision);
161 * @param string Repository
163 * @param integer Revision
165 * @return array Lines of cat output
167 function cat($repos, $path, $revision)
169 return $this->std('cat', $repos, $path, $revision);
177 * @param string Repository
179 * @param integer Lower revision
180 * @param integer Higher revision
182 * @return array Lines of diff output
184 function diff($repos, $path, $lorev, $hirev)
188 $hirev = $this->rev($hirev);
189 $lorev = $this->rev($lorev);
190 if ($lorev == 'HEAD')
195 if (is_integer($hirev) AND is_integer($lorev))
201 if ($lorev == $hirev)
207 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
209 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
217 * @param string Repository
219 * @param integer Lower revision
220 * @param integer Higher revision
222 * @return array Lines of log output
224 function log($repos, $path, $lorev, $hirev)
228 $hirev = $this->rev($hirev);
229 $lorev = $this->rev($hirev);
230 if ($lorev == 'HEAD')
235 if (is_integer($hirev) AND is_integer($lorev))
241 if ($lorev == $hirev)
247 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
249 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
253 * SVN Wrapper: ls (list)
257 * @param string Repository
259 * @param integer Revision
261 * @return array Lines of list output
263 function ls($repos, $path, $revision)
265 return $this->std('list', $repos, $path, $revision);
269 * Generates a clean revision number
273 * @param integer Revision number
275 * @return mixed Cleaned revision or HEAD
277 function rev($revision)
279 if (($revision = intval($revision)) < 1)
288 * Commonly executed SVN commands that return data
289 * used in many parts of the system
315 * Constructor: bind with registry
321 $this->registry
=& $viewsvn;
325 * Checks to see if the given universal path is
330 * @param string Universal path
332 * @return bool Directory or not
334 function isdir($path)
336 $output = $this->registry
->svn
->std('info', $this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 'HEAD');
338 foreach ($output AS $line)
340 if (preg_match('#^Node Kind: (.*)#', $line, $matches))
342 if (trim(strtolower($matches[1])) == 'directory')
353 * Get a list of revisions for a path
357 * @param string Universal path
359 * @return array Key revisions
361 function fetch_revs($path)
363 if (!isset($this->revisions
["$path"]))
365 $log = $this->fetch_logs($path);
367 $revs = array_keys($log);
369 $this->revisions
["$path"] = array(
371 'START' => $revs[ count($revs) - 1 ],
376 return $this->revisions
["$path"];
380 * Gets the revision that is marked as HEAD
384 * @param string Universal path
386 * @return integer Revision
388 function fetch_head_rev($path)
390 $revs = $this->fetch_revs($path);
391 return $revs['HEAD'];
395 * Returns the previous revision to the one given
399 * @param string Universal path
400 * @param integer Arbitrary revision
402 * @return integer Previous revision (-1 if none)
404 function fetch_prev_rev($path, $current)
406 $revs = $this->fetch_revs($path);
408 if ($current == 'HEAD')
410 $current = $this->fetch_head_rev($path);
413 $index = array_search($current, $revs['revs']);
414 if ($current === false
)
416 $message->trigger
->error('revision ' . $current . ' is not in ' . $path);
419 if (isset($revs['revs'][ $index +
1 ]))
421 return $revs['revs'][ $index +
1 ];
434 * @param string Universal path
436 * @return array Log data
438 function fetch_logs($path)
440 if (!isset($this->logs
["$path"]))
442 $log = new SVNLog($this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 0, 0);
444 $this->logs
["$path"] = $log->fetch();
447 return $this->logs
["$path"];
451 * Returns a given log entry for a path
456 * @param string Universal path
457 * @param integer Arbitrary revision
459 * @return array Log entry
461 function fetch_log($path, $rev)
463 $logs = $this->fetch_logs($path);
465 $rev = $this->registry
->svn
->rev($rev);
468 $rev = $this->fetch_head_rev($path);
471 if (isset($logs["$rev"]))
473 return $logs["$rev"];
483 * Annotation/blame system; constructs an array
484 * that is ready for output
492 * Array of blame information
495 var $blame = array();
498 * Raw "svn blame" output
504 * Constructor: create blame and store data
506 * @param string Repository
508 * @param integer Revision
510 function SVNBlame($repos, $path, $revision)
514 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
519 * Returns blame for display
523 * @return array Blame data
531 * Parses the blame data
539 foreach ($this->rawoutput
AS $line)
541 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
543 $this->blame
[] = array(
544 'rev' => $matches[1],
545 'author' => $matches[2],
546 'line' => $matches[3],
547 'lineno' => $lineno++
551 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
553 $this->blame
[] = array(
554 'rev' => $matches[1],
555 'author' => $matches[2],
557 'lineno' => $lineno++
565 * Log management system; creates a complex list
566 * of SVN log information
580 * Raw "svn log" output
586 * Constructor: create log store for the given file
588 * @param string Repository
590 * @param integer Lower revision
591 * @param integer Higher revision
593 function SVNLog($repos, $path, $lorev, $hirev)
597 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
602 * Returns logs for display
606 * @return array Log data
614 * Splits up the raw output into a usable log
622 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
624 $line = $this->rawoutput
["$i"];
626 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
628 if (isset($this->logs
["$lastrev"]))
630 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
633 $this->logs
["$matches[1]"] = array(
634 'rev' => $matches[1],
635 'author' => $matches[2],
636 'date' => $matches[3],
637 'timezone' => $matches[4],
638 'lines' => $matches[6],
642 $lastrev = $matches[1];
644 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
646 $this->logs
["$lastrev"]['files'][] = array(
647 'action' => $matches[1],
648 'file' => $matches[2]
653 if (trim($line) != 'Changed paths:')
655 $this->logs
["$lastrev"]['message'] .= $line . "\n";
660 if (isset($this->logs
["$lastrev"]))
662 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
667 * Trims the last dash line off a message
671 * @param string Message with annoying-ass line
673 * @return string Clean string
675 function strip_last_line($string)
677 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
682 * Diff system; constructs a diff array that
683 * is ready for output
690 * Array of diff information
696 * Raw "svn diff" output
702 * Constructor: create and store diff data
704 * @param string Repository
706 * @param integer Lower revision
707 * @param integer Higher revision
709 function SVNDiff($repos, $path, $lorev, $hirev)
713 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
718 * Returns diffs for display
722 * @return array Diff data
730 * Processes and prepares diff data
737 $indexcounter = null
;
742 foreach ($this->rawoutput
AS $line)
744 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
749 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
750 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
751 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
755 if ($indexcounter <= 5 AND $indexcounter !== null
)
760 else if ($indexcounter == 5)
762 $indexcounter = null
;
766 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
769 $content = $matches[2];
773 $this->diff
["$index"]["$chunk"][] = array(
776 'oldlineno' => ++
$lines['old'],
777 'newlineno' => ++
$lines['new']
780 else if ($act == '+')
782 // potential line delta
785 if ($delta = @$this->fetch_diff_extent($lastcontent, $content))
787 // create two sets of ends for the two contents
788 $delta['endo'] = strlen($lastcontent) - $delta['end'];
789 $delta['endn'] = strlen($content) - $delta['end'];
791 $diffo = $delta['endo'] - $delta['start'];
792 $diffn = $delta['endn'] - $delta['start'];
794 if (strlen($lastcontent) > $delta['endo'] - $diffo)
796 $removed = substr($lastcontent, $delta['start'], $diffo);
797 $this->diff
["$index"]["$chunk"][ count($this->diff
["$index"]["$chunk"]) - 2 ]['line'] = substr_replace($lastcontent, '{@--}' . $removed . '{/@--}', $delta['start'], $diffo);
800 if (strlen($content) > $delta['endn'] - $diffn)
802 $added = substr($content, $delta['start'], $diffn);
803 $content = substr_replace($content, '{@++}' . $added . '{/@++}', $delta['start'], $diffn);
808 $this->diff
["$index"]["$chunk"][] = array(
812 'newlineno' => ++
$lines['new']
815 else if ($act == '-')
817 $lastcontent = $content;
819 $this->diff
["$index"]["$chunk"][] = array(
822 'oldlineno' => ++
$lines['old'],
832 if (preg_match('#^Index: (.*?)$#', $line, $matches))
834 $index = $matches[1];
842 $this->diff
["$index"]["$chunk"][] = array(
845 'oldlineno' => ++
$lines['old'],
846 'newlineno' => ++
$lines['new']
853 * Returns the amount of change that occured
858 * @param string Old line
859 * @param string New line
861 * @return array Difference of positions
863 function fetch_diff_extent($old, $new)
866 $min = min(strlen($old), strlen($new));
868 for ($start = 0; $start < $min; $start++
)
870 if ($old{"$start"} != $new{"$start"})
876 $max = max(strlen($old), strlen($new));
878 for ($end = 0; $end < $min; $end++
)
880 $oldpos = strlen($old) - $end;
881 $newpos = strlen($new) - $end;
883 if ($old{"$oldpos"} != $new{"$newpos"})
891 if ($start == 0 AND $end == $max)
896 return array('start' => $start, 'end' => $end);
900 /*=====================================================================*\
901 || ###################################################################
904 || ###################################################################
905 \*=====================================================================*/