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);
79 $string = nl2br($string);
85 * Executes the SVN binary
89 * @param string Command
91 * @return array Output
93 function svn($command)
97 return $viewsvn->shell
->exec($this->svnpath
. ' ' . $command . ' 2>&1');
101 * SVN Wrapper: standard command system
105 * @param string SVN command
106 * @param string Repository
108 * @param integer Revision
110 * @return array Lines of output
112 function std($command, $repos, $path, $revision)
116 $revision = $this->rev($revision);
117 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
119 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
127 * @param string Repository
129 * @param integer Revision
131 * @return array Lines of blame output
133 function blame($repos, $path, $revision)
135 return $this->std('blame', $repos, $path, $revision);
143 * @param string Repository
145 * @param integer Revision
147 * @return array Lines of cat output
149 function cat($repos, $path, $revision)
153 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
155 return $this->svn('cat ' . $repospath . $path . '@' . $revision);
163 * @param string Repository
165 * @param integer Lower revision
166 * @param integer Higher revision
168 * @return array Lines of diff output
170 function diff($repos, $path, $lorev, $hirev)
174 $hirev = $this->rev($hirev);
175 $lorev = $this->rev($lorev);
176 if ($lorev == 'HEAD')
181 if (is_integer($hirev) AND is_integer($lorev))
187 if ($lorev == $hirev)
193 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
195 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
203 * @param string Repository
205 * @param integer Lower revision
206 * @param integer Higher revision
208 * @return array Lines of log output
210 function log($repos, $path, $lorev, $hirev)
214 $hirev = $this->rev($hirev);
215 $lorev = $this->rev($hirev);
216 if ($lorev == 'HEAD')
221 if (is_integer($hirev) AND is_integer($lorev))
227 if ($lorev == $hirev)
233 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
235 return $this->svn('log -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
239 * SVN Wrapper: ls (list)
243 * @param string Repository
245 * @param integer Revision
247 * @return array Lines of list output
249 function ls($repos, $path, $revision)
251 return $this->std('list', $repos, $path, $revision);
255 * Generates a clean revision number
259 * @param integer Revision number
261 * @return mixed Cleaned revision or HEAD
263 function rev($revision)
265 if (($revision = intval($revision)) < 1)
274 * Annotation/blame system; constructs an array
275 * that is ready for output
283 * Array of blame information
286 var $blame = array();
289 * Raw "svn blame" output
295 * Constructor: create blame and store data
297 * @param string Repository
299 * @param integer Revision
301 function SVNBlame($repos, $path, $revision)
305 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
310 * Returns blame for display
314 * @return array Blame data
322 * Parses the blame data
330 foreach ($this->rawoutput
AS $line)
332 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
334 $this->blame
[] = array(
335 'rev' => $matches[1],
336 'author' => $matches[2],
337 'line' => $matches[3],
338 'lineno' => $lineno++
342 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
344 $this->blame
[] = array(
345 'rev' => $matches[1],
346 'author' => $matches[2],
348 'lineno' => $lineno++
356 * Log management system; creates a complex list
357 * of SVN log information
371 * Raw "svn log" output
377 * Constructor: create log store for the given file
379 * @param string Repository
381 * @param integer Lower revision
382 * @param integer Higher revision
384 function SVNLog($repos, $path, $lorev, $hirev)
388 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
393 * Returns logs for display
397 * @return array Log data
405 * Splits up the raw output into a usable log
413 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
415 $line = $this->rawoutput
["$i"];
417 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
419 if (isset($this->logs
["$lastrev"]))
421 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
424 $this->logs
["$matches[1]"] = array(
425 'rev' => $matches[1],
426 'author' => $matches[2],
427 'date' => $matches[3],
428 'timezone' => $matches[4],
429 'lines' => $matches[6],
433 $lastrev = $matches[1];
437 $this->logs
["$lastrev"]['message'] .= $line . "\n";
441 if (isset($this->logs
["$lastrev"]))
443 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
448 * Trims the last dash line off a message
452 * @param string Message with annoying-ass line
454 * @return string Clean string
456 function strip_last_line($string)
458 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
463 * Diff system; constructs a diff array that
464 * is ready for output
471 * Array of diff information
477 * Raw "svn diff" output
483 * Constructor: create and store diff data
485 * @param string Repository
487 * @param integer Lower revision
488 * @param integer Higher revision
490 function SVNDiff($repos, $path, $lorev, $hirev)
494 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
499 * Returns diffs for display
503 * @return array Diff data
511 * Processes and prepares diff data
520 foreach ($this->rawoutput
AS $line)
522 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
525 $this->diff
[ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
526 $lines['old'] = $this->diff
["$chunk"]['hunk']['old']['line'] - 1;
527 $lines['new'] = $this->diff
["$chunk"]['hunk']['new']['line'] - 1;
531 // ensures we don't capture before the first hunk
537 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
540 $content = $matches[2];
544 $this->diff
["$chunk"][] = array(
547 'oldlineno' => ++
$lines['old'],
548 'newlineno' => ++
$lines['new']
551 else if ($act == '+')
553 $this->diff
["$chunk"][] = array(
557 'newlineno' => ++
$lines['new']
560 else if ($act == '-')
562 $this->diff
["$chunk"][] = array(
565 'oldlineno' => ++
$lines['old'],
573 $this->diff
["$chunk"][] = array(
576 'oldlineno' => ++
$lines['old'],
577 'newlineno' => ++
$lines['new']
584 /*=====================================================================*\
585 || ###################################################################
588 || ###################################################################
589 \*=====================================================================*/