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 return $viewsvn->shell
->exec($this->svnpath
. ' ' . $command . ' 2>&1');
113 * SVN Wrapper: standard command system
117 * @param string SVN command
118 * @param string Repository
120 * @param integer Revision
122 * @return array Lines of output
124 function std($command, $repos, $path, $revision)
128 $revision = $this->rev($revision);
129 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
131 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
139 * @param string Repository
141 * @param integer Revision
143 * @return array Lines of blame output
145 function blame($repos, $path, $revision)
147 return $this->std('blame', $repos, $path, $revision);
155 * @param string Repository
157 * @param integer Revision
159 * @return array Lines of cat output
161 function cat($repos, $path, $revision)
165 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
167 return $this->svn('cat ' . $repospath . $path . '@' . $revision);
175 * @param string Repository
177 * @param integer Lower revision
178 * @param integer Higher revision
180 * @return array Lines of diff output
182 function diff($repos, $path, $lorev, $hirev)
186 $hirev = $this->rev($hirev);
187 $lorev = $this->rev($lorev);
188 if ($lorev == 'HEAD')
193 if (is_integer($hirev) AND is_integer($lorev))
199 if ($lorev == $hirev)
205 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
207 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
215 * @param string Repository
217 * @param integer Lower revision
218 * @param integer Higher revision
220 * @return array Lines of log output
222 function log($repos, $path, $lorev, $hirev)
226 $hirev = $this->rev($hirev);
227 $lorev = $this->rev($hirev);
228 if ($lorev == 'HEAD')
233 if (is_integer($hirev) AND is_integer($lorev))
239 if ($lorev == $hirev)
245 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
247 return $this->svn('log -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
251 * SVN Wrapper: ls (list)
255 * @param string Repository
257 * @param integer Revision
259 * @return array Lines of list output
261 function ls($repos, $path, $revision)
263 return $this->std('list', $repos, $path, $revision);
267 * Generates a clean revision number
271 * @param integer Revision number
273 * @return mixed Cleaned revision or HEAD
275 function rev($revision)
277 if (($revision = intval($revision)) < 1)
286 * Commonly executed SVN commands that return data
287 * used in many parts of the system
301 * Constructor: bind with registry
307 $this->registry
=& $viewsvn;
311 * Checks to see if the given universal path is
316 * @param string Universal path
318 * @return bool Directory or not
320 function isdir($path)
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
)
336 * Annotation/blame system; constructs an array
337 * that is ready for output
345 * Array of blame information
348 var $blame = array();
351 * Raw "svn blame" output
357 * Constructor: create blame and store data
359 * @param string Repository
361 * @param integer Revision
363 function SVNBlame($repos, $path, $revision)
367 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
372 * Returns blame for display
376 * @return array Blame data
384 * Parses the blame data
392 foreach ($this->rawoutput
AS $line)
394 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
396 $this->blame
[] = array(
397 'rev' => $matches[1],
398 'author' => $matches[2],
399 'line' => $matches[3],
400 'lineno' => $lineno++
404 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
406 $this->blame
[] = array(
407 'rev' => $matches[1],
408 'author' => $matches[2],
410 'lineno' => $lineno++
418 * Log management system; creates a complex list
419 * of SVN log information
433 * Raw "svn log" output
439 * Constructor: create log store for the given file
441 * @param string Repository
443 * @param integer Lower revision
444 * @param integer Higher revision
446 function SVNLog($repos, $path, $lorev, $hirev)
450 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
455 * Returns logs for display
459 * @return array Log data
467 * Splits up the raw output into a usable log
475 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
477 $line = $this->rawoutput
["$i"];
479 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
481 if (isset($this->logs
["$lastrev"]))
483 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
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],
495 $lastrev = $matches[1];
499 $this->logs
["$lastrev"]['message'] .= $line . "\n";
503 if (isset($this->logs
["$lastrev"]))
505 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
510 * Trims the last dash line off a message
514 * @param string Message with annoying-ass line
516 * @return string Clean string
518 function strip_last_line($string)
520 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
525 * Diff system; constructs a diff array that
526 * is ready for output
533 * Array of diff information
539 * Raw "svn diff" output
545 * Constructor: create and store diff data
547 * @param string Repository
549 * @param integer Lower revision
550 * @param integer Higher revision
552 function SVNDiff($repos, $path, $lorev, $hirev)
556 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
561 * Returns diffs for display
565 * @return array Diff data
573 * Processes and prepares diff data
580 $indexcounter = null
;
585 foreach ($this->rawoutput
AS $line)
587 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
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;
598 if ($indexcounter <= 5 AND $indexcounter !== null
)
603 else if ($indexcounter == 5)
605 $indexcounter = null
;
609 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
612 $content = $matches[2];
616 $this->diff
["$index"]["$chunk"][] = array(
619 'oldlineno' => ++
$lines['old'],
620 'newlineno' => ++
$lines['new']
623 else if ($act == '+')
625 // potential line delta
628 if ($delta = @$this->fetch_diff_extent($lastcontent, $content))
630 // create two sets of ends for the two contents
631 $delta['endo'] = strlen($lastcontent) - $delta['end'];
632 $delta['endn'] = strlen($content) - $delta['end'];
634 $diffo = $delta['endo'] - $delta['start'];
635 $diffn = $delta['endn'] - $delta['start'];
637 if (strlen($lastcontent) > $delta['endo'] - $diffo)
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);
643 if (strlen($content) > $delta['endn'] - $diffn)
645 $added = substr($content, $delta['start'], $diffn);
646 $content = substr_replace($content, '{@++}' . $added . '{/@++}', $delta['start'], $diffn);
651 $this->diff
["$index"]["$chunk"][] = array(
655 'newlineno' => ++
$lines['new']
658 else if ($act == '-')
660 $lastcontent = $content;
662 $this->diff
["$index"]["$chunk"][] = array(
665 'oldlineno' => ++
$lines['old'],
675 if (preg_match('#^Index: (.*?)$#', $line, $matches))
677 $index = $matches[1];
685 $this->diff
["$index"]["$chunk"][] = array(
688 'oldlineno' => ++
$lines['old'],
689 'newlineno' => ++
$lines['new']
696 * Returns the amount of change that occured
701 * @param string Old line
702 * @param string New line
704 * @return array Difference of positions
706 function fetch_diff_extent($old, $new)
709 $min = min(strlen($old), strlen($new));
711 for ($start = 0; $start < $min; $start++
)
713 if ($old{"$start"} != $new{"$start"})
719 $max = max(strlen($old), strlen($new));
721 for ($end = 0; $end < $min; $end++
)
723 $oldpos = strlen($old) - $end;
724 $newpos = strlen($new) - $end;
726 if ($old{"$oldpos"} != $new{"$newpos"})
734 if ($start == 0 AND $end == $max)
739 return array('start' => $start, 'end' => $end);
743 /*=====================================================================*\
744 || ###################################################################
747 || ###################################################################
748 \*=====================================================================*/