2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
23 * Command line interface with the SVN commands
29 * Interacts with the command line subsystem to
30 * access SVN information
38 * Path to the SVN binary
51 * Constructor: validate SVN path
53 * @param object Controller
55 function SVNLib(&$controller)
57 $this->controller
=& $controller;
59 $this->svnpath
=& $this->controller
->xquery
->cmd($this->controller
->registry
->svnpath
);
61 $access = $this->controller
->xquery
->exec($this->svnpath
. ' --version');
65 $this->controller
->registry
->trigger
->error($this->controller
->registry
->lang
->string('The SVN binary could not be located'));
68 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
70 $this->controller
->registry
->trigger
->error($this->controller
->registry
->lang
->string('The SVN binary does not appear to be valid (it failed our tests)'));
75 * Executes the SVN binary
79 * @param string Command
81 * @return array Output
83 function svn($command)
85 $output = $this->controller
->xquery
->exec($this->svnpath
. ' ' . $command . ' 2>&1');
87 // make sure that we keep escaped chars
88 //$output = str_replace(array('\t', '\n', '\r'), array('\\\t', '\\\n', '\\\r'), $output);
89 //$output = preg_replace('#\\\(.)#', '\\\\\\\\' . '\1', $output);
90 //$output = str_replace('\\', '\\\\', $output);
92 $temp = implode("\n", $output);
93 if (strpos($temp, '(apr' . '_err=') !== false
)
95 $this->controller
->registry
->trigger
->error(nl2br($temp));
101 * SVN Wrapper: standard command system
105 * @param string SVN command
107 * @return array Lines of output
109 function command($command)
113 $revision = SVNCommon
::rev($this->controller
->revnum
);
115 return $this->svn($command . ' ' . $this->controller
->repospath
. $this->controller
->path
. '@' . $revision);
123 * @param string Repository
125 * @param integer Revision
127 * @return array Lines of blame output
129 function blame($repos, $path, $revision)
131 return $this->std('blame', $repos, $path, $revision);
139 * @param string Repository
141 * @param integer Revision
143 * @return array Lines of cat output
145 function cat($repos, $path, $revision)
147 return $this->std('cat', $repos, $path, $revision);
155 * @param string Repository
157 * @param integer Lower revision
158 * @param integer Higher revision
160 * @return array Lines of diff output
162 function diff($repos, $path, $lorev, $hirev)
166 $hirev = $this->rev($hirev);
167 $lorev = $this->rev($lorev);
168 if ($lorev == 'HEAD')
173 if (is_integer($hirev) AND is_integer($lorev))
179 if ($lorev == $hirev)
185 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
187 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
195 * @param string Repository
197 * @param integer Lower revision
198 * @param integer Higher revision
200 * @return array Lines of log output
202 function log($repos, $path, $lorev, $hirev)
206 $hirev = $this->rev($hirev);
207 $lorev = $this->rev($hirev);
208 if ($lorev == 'HEAD')
213 if (is_integer($hirev) AND is_integer($lorev))
219 if ($lorev == $hirev)
225 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
227 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
231 * SVN Wrapper: ls (list)
235 * @param object Controller object
237 * @return array Lines of list output
239 function ls(&$controller)
241 return $this->std('list');
246 * Annotation/blame system; constructs an array
247 * that is ready for output
255 * Array of blame information
258 var $blame = array();
261 * Raw "svn blame" output
267 * Constructor: create blame and store data
269 * @param string Repository
271 * @param integer Revision
273 function SVNBlame(&$controller)
275 $this->rawoutput
= $controller->library
->command('blame');
280 * Returns blame for display
284 * @return array Blame data
292 * Parses the blame data
300 foreach ($this->rawoutput
AS $line)
302 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
304 $this->blame
[] = array(
305 'rev' => $matches[1],
306 'author' => $matches[2],
307 'line' => $matches[3],
308 'lineno' => $lineno++
312 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
314 $this->blame
[] = array(
315 'rev' => $matches[1],
316 'author' => $matches[2],
318 'lineno' => $lineno++
326 * Log management system; creates a complex list
327 * of SVN log information
341 * Raw "svn log" output
347 * Constructor: create log store for the given file
349 * @param string Repository
351 * @param integer Lower revision
352 * @param integer Higher revision
354 function SVNLog($repos, $path, $lorev, $hirev)
358 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
363 * Returns logs for display
367 * @return array Log data
375 * Splits up the raw output into a usable log
383 for ($i = 1; $i <= sizeof($this->rawoutput
) - 1; $i++
)
385 $line = $this->rawoutput
["$i"];
387 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
389 if (isset($this->logs
["$lastrev"]))
391 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
394 $this->logs
["$matches[1]"] = array(
395 'rev' => $matches[1],
396 'author' => $matches[2],
397 'date' => $matches[3],
398 'timezone' => $matches[4],
399 'lines' => $matches[6],
403 $lastrev = $matches[1];
405 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
407 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
409 $matches[2] = $amatches[1];
412 $this->logs
["$lastrev"]['files'][] = array(
413 'action' => $matches[1],
414 'file' => trim($matches[2]),
415 'from' => (isset($amatches[2]) ?
$amatches[2] : '')
420 if (trim($line) != 'Changed paths:')
422 $this->logs
["$lastrev"]['message'] .= $line . "\n";
427 if (isset($this->logs
["$lastrev"]))
429 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
434 * Trims the last dash line off a message
438 * @param string Message with annoying-ass line
440 * @return string Clean string
442 function strip_last_line($string)
444 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
449 * Diff system; constructs a diff array that
450 * is ready for output
457 * Array of diff information
463 * Raw "svn diff" output
469 * Constructor: create and store diff data
471 * @param string Repository
473 * @param integer Lower revision
474 * @param integer Higher revision
476 function SVNDiff($repos, $path, $lorev, $hirev)
480 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
485 * Returns diffs for display
489 * @return array Diff data
497 * Processes and prepares diff data
506 $indexcounter = null
;
511 foreach ($this->rawoutput
AS $line)
513 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
517 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
518 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
519 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
522 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
526 $this->diff
["$index"]['props'] = array();
530 if ($indexcounter <= 3 AND $indexcounter !== null
)
535 else if ($indexcounter == 3)
537 $indexcounter = null
;
541 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
544 $content = $matches[2];
548 $this->diff
["$index"]["$chunk"][] = array(
551 'oldlineno' => ++
$lines['old'],
552 'newlineno' => ++
$lines['new']
557 else if ($act == '+')
559 // potential line delta
560 if (sizeof($delstack) > 0)
562 $lastline = array_shift($delstack);
564 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
566 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
568 $end = strlen($lastline['line']) +
$delta['end'];
569 $viewsvn->debug("RM delta- = " . $end);
570 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
571 $this->diff
["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
574 if (strlen($content) > $delta['start'] - $delta['end'])
576 $end = strlen($content) +
$delta['end'];
577 $viewsvn->debug("MK delta+ = " . $end);
578 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
579 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
584 $this->diff
["$index"]["$chunk"][] = array(
588 'newlineno' => ++
$lines['new']
591 else if ($act == '-')
593 $this->diff
["$index"]["$chunk"][] = $thearray = array(
596 'oldlineno' => ++
$lines['old'],
600 $key = sizeof($this->diff
["$index"]["$chunk"]) - 2;
601 $thearray['INDEX'] = $key;
603 array_push($delstack, $thearray);
609 if (preg_match('#^Index: (.*?)$#', $line, $matches))
611 $index = $matches[1];
619 if (preg_match('#^__*_$#', trim($line)))
621 $viewsvn->debug("skipping: $line");
625 if (preg_match('#Name: (.*?)$#', $line, $matches))
627 $curprop = $matches[1];
628 $viewsvn->debug("prop: $curprop");
633 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
636 $this->diff
["$index"]['props']["$curprop"]['add'] .= $matches[1];
638 else if (preg_match('#^\s+?\-(.*)#', $line, $matches))
641 $this->diff
["$index"]['props']["$curprop"]['del'] .= $matches[1];
643 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
645 $this->diff
["$index"]['props']["$curprop"]["$mode"] .= "\n" . $line;
651 $this->diff
["$index"]["$chunk"][] = array(
654 'oldlineno' => ++
$lines['old'],
655 'newlineno' => ++
$lines['new']
664 * Returns the amount of change that occured
669 * @param string Old line
670 * @param string New line
672 * @return array Difference of positions
674 function fetch_diff_extent($old, $new)
679 $min = min(strlen($old), strlen($new));
681 $viewsvn->debug("min1 = $min");
683 while ($start < $min AND $old["$start"] == $new["$start"])
689 $min = $min - $start;
691 $viewsvn->debug("min2 = $min");
693 $viewsvn->debug("checking: " . $old[ strlen($old) +
$end ] . ' == ' . $new[ strlen($new) +
$end ]);
695 while (-$end <= $min AND $old[ strlen($old) +
$end ] == $new[ strlen($new) +
$end ])
700 return array('start' => $start, 'end' => $end +
1);
704 /*=====================================================================*\
705 || ###################################################################
708 || ###################################################################
709 \*=====================================================================*/