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 * Constructor: validate SVN path
37 * @param string Path to SVN binary
39 function SVNLib($svnpath)
43 $this->svnpath
= $viewsvn->shell
->cmd($svnpath);
45 $access = $viewsvn->shell
->exec($this->svnpath
. ' --version');
49 $viewsvn->trigger
->error('svn binary could not be found');
52 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
54 $viewsvn->trigger
->error('svn binary does not pass test');
59 * Prepares data for output
63 * @param string Standard data
65 * @return string Output-ready data
67 function format($string)
70 $string = htmlspecialchars($string);
73 $string = str_replace("\t", ' ', $string);
76 $string = str_replace(' ', ' ', $string);
78 // convert advanced diff
79 $string = str_replace(array('{@++}', '{@--}'), array('<span class="diff_add">', '<span class="diff_del">'), $string);
80 $string = str_replace(array('{/@++}', '{/@--}'), '</span>', $string);
83 $string = nl2br($string);
89 * Executes the SVN binary
93 * @param string Command
95 * @return array Output
97 function svn($command)
101 return $viewsvn->shell
->exec($this->svnpath
. ' ' . $command . ' 2>&1');
105 * SVN Wrapper: standard command system
109 * @param string SVN command
110 * @param string Repository
112 * @param integer Revision
114 * @return array Lines of output
116 function std($command, $repos, $path, $revision)
120 $revision = $this->rev($revision);
121 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
123 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
131 * @param string Repository
133 * @param integer Revision
135 * @return array Lines of blame output
137 function blame($repos, $path, $revision)
139 return $this->std('blame', $repos, $path, $revision);
147 * @param string Repository
149 * @param integer Revision
151 * @return array Lines of cat output
153 function cat($repos, $path, $revision)
157 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
159 return $this->svn('cat ' . $repospath . $path . '@' . $revision);
167 * @param string Repository
169 * @param integer Lower revision
170 * @param integer Higher revision
172 * @return array Lines of diff output
174 function diff($repos, $path, $lorev, $hirev)
178 $hirev = $this->rev($hirev);
179 $lorev = $this->rev($lorev);
180 if ($lorev == 'HEAD')
185 if (is_integer($hirev) AND is_integer($lorev))
191 if ($lorev == $hirev)
197 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
199 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
207 * @param string Repository
209 * @param integer Lower revision
210 * @param integer Higher revision
212 * @return array Lines of log output
214 function log($repos, $path, $lorev, $hirev)
218 $hirev = $this->rev($hirev);
219 $lorev = $this->rev($hirev);
220 if ($lorev == 'HEAD')
225 if (is_integer($hirev) AND is_integer($lorev))
231 if ($lorev == $hirev)
237 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
239 return $this->svn('log -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
243 * SVN Wrapper: ls (list)
247 * @param string Repository
249 * @param integer Revision
251 * @return array Lines of list output
253 function ls($repos, $path, $revision)
255 return $this->std('list', $repos, $path, $revision);
259 * Generates a clean revision number
263 * @param integer Revision number
265 * @return mixed Cleaned revision or HEAD
267 function rev($revision)
269 if (($revision = intval($revision)) < 1)
278 * Annotation/blame system; constructs an array
279 * that is ready for output
287 * Array of blame information
290 var $blame = array();
293 * Raw "svn blame" output
299 * Constructor: create blame and store data
301 * @param string Repository
303 * @param integer Revision
305 function SVNBlame($repos, $path, $revision)
309 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
314 * Returns blame for display
318 * @return array Blame data
326 * Parses the blame data
334 foreach ($this->rawoutput
AS $line)
336 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
338 $this->blame
[] = array(
339 'rev' => $matches[1],
340 'author' => $matches[2],
341 'line' => $matches[3],
342 'lineno' => $lineno++
346 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
348 $this->blame
[] = array(
349 'rev' => $matches[1],
350 'author' => $matches[2],
352 'lineno' => $lineno++
360 * Log management system; creates a complex list
361 * of SVN log information
375 * Raw "svn log" output
381 * Constructor: create log store for the given file
383 * @param string Repository
385 * @param integer Lower revision
386 * @param integer Higher revision
388 function SVNLog($repos, $path, $lorev, $hirev)
392 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
397 * Returns logs for display
401 * @return array Log data
409 * Splits up the raw output into a usable log
417 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
419 $line = $this->rawoutput
["$i"];
421 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
423 if (isset($this->logs
["$lastrev"]))
425 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
428 $this->logs
["$matches[1]"] = array(
429 'rev' => $matches[1],
430 'author' => $matches[2],
431 'date' => $matches[3],
432 'timezone' => $matches[4],
433 'lines' => $matches[6],
437 $lastrev = $matches[1];
441 $this->logs
["$lastrev"]['message'] .= $line . "\n";
445 if (isset($this->logs
["$lastrev"]))
447 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
452 * Trims the last dash line off a message
456 * @param string Message with annoying-ass line
458 * @return string Clean string
460 function strip_last_line($string)
462 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
467 * Diff system; constructs a diff array that
468 * is ready for output
475 * Array of diff information
481 * Raw "svn diff" output
487 * Constructor: create and store diff data
489 * @param string Repository
491 * @param integer Lower revision
492 * @param integer Higher revision
494 function SVNDiff($repos, $path, $lorev, $hirev)
498 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
503 * Returns diffs for display
507 * @return array Diff data
515 * Processes and prepares diff data
526 foreach ($this->rawoutput
AS $line)
528 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
534 $this->diff
[ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
535 $lines['old'] = $this->diff
["$chunk"]['hunk']['old']['line'] - 1;
536 $lines['new'] = $this->diff
["$chunk"]['hunk']['new']['line'] - 1;
540 // ensures we don't capture before the first hunk
546 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
549 $content = $matches[2];
553 $this->diff
["$chunk"][] = array(
556 'oldlineno' => ++
$lines['old'],
557 'newlineno' => ++
$lines['new']
560 else if ($act == '+')
562 // potential line delta
565 if ($delta = @$this->fetch_diff_extent($lastcontent, $content))
567 // create two sets of ends for the two contents
568 $delta['endo'] = strlen($lastcontent) - $delta['end'];
569 $delta['endn'] = strlen($content) - $delta['end'];
571 $diffo = $delta['endo'] - $delta['start'];
572 $diffn = $delta['endn'] - $delta['start'];
574 if (strlen($lastcontent) > $delta['endo'] - $diffo)
576 $removed = substr($lastcontent, $delta['start'], $diffo);
577 $this->diff
["$chunk"][ count($this->diff
["$chunk"]) - 2 ]['line'] = substr_replace($lastcontent, '{@--}' . $removed . '{/@--}', $delta['start'], $diffo);
580 if (strlen($content) > $delta['endn'] - $diffn)
582 $added = substr($content, $delta['start'], $diffn);
583 $content = substr_replace($content, '{@++}' . $added . '{/@++}', $delta['start'], $diffn);
588 $this->diff
["$chunk"][] = array(
592 'newlineno' => ++
$lines['new']
595 else if ($act == '-')
597 $lastcontent = $content;
599 $this->diff
["$chunk"][] = array(
602 'oldlineno' => ++
$lines['old'],
614 $this->diff
["$chunk"][] = array(
617 'oldlineno' => ++
$lines['old'],
618 'newlineno' => ++
$lines['new']
625 * Returns the amount of change that occured
630 * @param string Old line
631 * @param string New line
633 * @return array Difference of positions
635 function fetch_diff_extent($old, $new)
638 $min = min(strlen($old), strlen($new));
640 for ($start = 0; $start < $min; $start++
)
642 if ($old{"$start"} != $new{"$start"})
648 $max = max(strlen($old), strlen($new));
650 for ($end = 0; $end < $min; $end++
)
652 $oldpos = strlen($old) - $end;
653 $newpos = strlen($new) - $end;
655 if ($old{"$oldpos"} != $new{"$newpos"})
657 echo $old{"$oldpos"} . ':' . $new{"$newpos"};
664 if ($start == 0 AND $end == $max)
669 return array('start' => $start, 'end' => $end);
673 /*=====================================================================*\
674 || ###################################################################
677 || ###################################################################
678 \*=====================================================================*/