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">', '<span class="diff_del">'), $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_error=') !== false
OR strpos($temp, 'svn:') !== 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)
171 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
173 return $this->svn('cat ' . $repospath . $path . '@' . $revision);
181 * @param string Repository
183 * @param integer Lower revision
184 * @param integer Higher revision
186 * @return array Lines of diff output
188 function diff($repos, $path, $lorev, $hirev)
192 $hirev = $this->rev($hirev);
193 $lorev = $this->rev($lorev);
194 if ($lorev == 'HEAD')
199 if (is_integer($hirev) AND is_integer($lorev))
205 if ($lorev == $hirev)
211 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
213 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
221 * @param string Repository
223 * @param integer Lower revision
224 * @param integer Higher revision
226 * @return array Lines of log output
228 function log($repos, $path, $lorev, $hirev)
232 $hirev = $this->rev($hirev);
233 $lorev = $this->rev($hirev);
234 if ($lorev == 'HEAD')
239 if (is_integer($hirev) AND is_integer($lorev))
245 if ($lorev == $hirev)
251 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
253 return $this->svn('log -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
257 * SVN Wrapper: ls (list)
261 * @param string Repository
263 * @param integer Revision
265 * @return array Lines of list output
267 function ls($repos, $path, $revision)
269 return $this->std('list', $repos, $path, $revision);
273 * Generates a clean revision number
277 * @param integer Revision number
279 * @return mixed Cleaned revision or HEAD
281 function rev($revision)
283 if (($revision = intval($revision)) < 1)
292 * Commonly executed SVN commands that return data
293 * used in many parts of the system
307 * Constructor: bind with registry
313 $this->registry
=& $viewsvn;
317 * Checks to see if the given universal path is
322 * @param string Universal path
324 * @return bool Directory or not
326 function isdir($path)
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
)
342 * Annotation/blame system; constructs an array
343 * that is ready for output
351 * Array of blame information
354 var $blame = array();
357 * Raw "svn blame" output
363 * Constructor: create blame and store data
365 * @param string Repository
367 * @param integer Revision
369 function SVNBlame($repos, $path, $revision)
373 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
378 * Returns blame for display
382 * @return array Blame data
390 * Parses the blame data
398 foreach ($this->rawoutput
AS $line)
400 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
402 $this->blame
[] = array(
403 'rev' => $matches[1],
404 'author' => $matches[2],
405 'line' => $matches[3],
406 'lineno' => $lineno++
410 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
412 $this->blame
[] = array(
413 'rev' => $matches[1],
414 'author' => $matches[2],
416 'lineno' => $lineno++
424 * Log management system; creates a complex list
425 * of SVN log information
439 * Raw "svn log" output
445 * Constructor: create log store for the given file
447 * @param string Repository
449 * @param integer Lower revision
450 * @param integer Higher revision
452 function SVNLog($repos, $path, $lorev, $hirev)
456 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
461 * Returns logs for display
465 * @return array Log data
473 * Splits up the raw output into a usable log
481 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
483 $line = $this->rawoutput
["$i"];
485 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
487 if (isset($this->logs
["$lastrev"]))
489 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
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],
501 $lastrev = $matches[1];
505 $this->logs
["$lastrev"]['message'] .= $line . "\n";
509 if (isset($this->logs
["$lastrev"]))
511 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
516 * Trims the last dash line off a message
520 * @param string Message with annoying-ass line
522 * @return string Clean string
524 function strip_last_line($string)
526 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
531 * Diff system; constructs a diff array that
532 * is ready for output
539 * Array of diff information
545 * Raw "svn diff" output
551 * Constructor: create and store diff data
553 * @param string Repository
555 * @param integer Lower revision
556 * @param integer Higher revision
558 function SVNDiff($repos, $path, $lorev, $hirev)
562 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
567 * Returns diffs for display
571 * @return array Diff data
579 * Processes and prepares diff data
586 $indexcounter = null
;
591 foreach ($this->rawoutput
AS $line)
593 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
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;
604 if ($indexcounter <= 5 AND $indexcounter !== null
)
609 else if ($indexcounter == 5)
611 $indexcounter = null
;
615 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
618 $content = $matches[2];
622 $this->diff
["$index"]["$chunk"][] = array(
625 'oldlineno' => ++
$lines['old'],
626 'newlineno' => ++
$lines['new']
629 else if ($act == '+')
631 // potential line delta
634 if ($delta = @$this->fetch_diff_extent($lastcontent, $content))
636 // create two sets of ends for the two contents
637 $delta['endo'] = strlen($lastcontent) - $delta['end'];
638 $delta['endn'] = strlen($content) - $delta['end'];
640 $diffo = $delta['endo'] - $delta['start'];
641 $diffn = $delta['endn'] - $delta['start'];
643 if (strlen($lastcontent) > $delta['endo'] - $diffo)
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);
649 if (strlen($content) > $delta['endn'] - $diffn)
651 $added = substr($content, $delta['start'], $diffn);
652 $content = substr_replace($content, '{@++}' . $added . '{/@++}', $delta['start'], $diffn);
657 $this->diff
["$index"]["$chunk"][] = array(
661 'newlineno' => ++
$lines['new']
664 else if ($act == '-')
666 $lastcontent = $content;
668 $this->diff
["$index"]["$chunk"][] = array(
671 'oldlineno' => ++
$lines['old'],
681 if (preg_match('#^Index: (.*?)$#', $line, $matches))
683 $index = $matches[1];
691 $this->diff
["$index"]["$chunk"][] = array(
694 'oldlineno' => ++
$lines['old'],
695 'newlineno' => ++
$lines['new']
702 * Returns the amount of change that occured
707 * @param string Old line
708 * @param string New line
710 * @return array Difference of positions
712 function fetch_diff_extent($old, $new)
715 $min = min(strlen($old), strlen($new));
717 for ($start = 0; $start < $min; $start++
)
719 if ($old{"$start"} != $new{"$start"})
725 $max = max(strlen($old), strlen($new));
727 for ($end = 0; $end < $min; $end++
)
729 $oldpos = strlen($old) - $end;
730 $newpos = strlen($new) - $end;
732 if ($old{"$oldpos"} != $new{"$newpos"})
740 if ($start == 0 AND $end == $max)
745 return array('start' => $start, 'end' => $end);
749 /*=====================================================================*\
750 || ###################################################################
753 || ###################################################################
754 \*=====================================================================*/