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
106 * @param bool Alternate revision
108 * @return array Lines of output
110 function command($command, $rev = false
)
114 $revision = ($rev !== false ?
$rev : SVNCommon
::rev($this->controller
->revnum
));
116 return $this->svn($command . ' ' . $this->controller
->repospath
. $this->controller
->path
. ($revision !== null ?
'@' . $revision : ''));
124 * @param string Repository
126 * @param integer Revision
128 * @return array Lines of blame output
130 function blame($repos, $path, $revision)
132 return $this->std('blame', $repos, $path, $revision);
140 * @param string Repository
142 * @param integer Revision
144 * @return array Lines of cat output
146 function cat($repos, $path, $revision)
148 return $this->std('cat', $repos, $path, $revision);
156 * @param integer Lower revision
157 * @param integer Higher revision
159 * @return array Lines of diff output
161 function diff($lorev, $hirev)
165 $hirev = SVNCommon
::rev($hirev);
166 $lorev = SVNCommon
::rev($lorev);
167 if ($lorev == 'HEAD')
172 if (is_integer($hirev) AND is_integer($lorev))
178 if ($lorev == $hirev)
184 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $this->controller
->repospath
. $this->controller
->path
);
192 * @param string Repository
194 * @param integer Lower revision
195 * @param integer Higher revision
197 * @return array Lines of log output
199 function log($repos, $path, $lorev, $hirev)
203 $hirev = $this->rev($hirev);
204 $lorev = $this->rev($hirev);
205 if ($lorev == 'HEAD')
210 if (is_integer($hirev) AND is_integer($lorev))
216 if ($lorev == $hirev)
222 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
224 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
228 * SVN Wrapper: ls (list)
232 * @param object Controller object
234 * @return array Lines of list output
236 function ls(&$controller)
238 return $this->std('list');
243 * Annotation/blame system; constructs an array
244 * that is ready for output
252 * Array of blame information
255 var $blame = array();
258 * Raw "svn blame" output
264 * Constructor: create blame and store data
266 * @param string Repository
268 * @param integer Revision
270 function SVNBlame(&$controller)
272 $this->rawoutput
= $controller->library
->command('blame');
277 * Returns blame for display
281 * @return array Blame data
289 * Parses the blame data
297 foreach ($this->rawoutput
AS $line)
299 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
301 $this->blame
[] = array(
302 'rev' => $matches[1],
303 'author' => $matches[2],
304 'line' => $matches[3],
305 'lineno' => $lineno++
309 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
311 $this->blame
[] = array(
312 'rev' => $matches[1],
313 'author' => $matches[2],
315 'lineno' => $lineno++
323 * Log management system; creates a complex list
324 * of SVN log information
338 * Raw "svn log" output
344 * Constructor: create log store for the given file
346 * @param string Repository
348 * @param integer Lower revision
349 * @param integer Higher revision
351 function SVNLog(&$controller)
353 $this->rawoutput
= $controller->library
->command('log', null
);
358 * Returns logs for display
362 * @return array Log data
370 * Splits up the raw output into a usable log
378 for ($i = 1; $i <= sizeof($this->rawoutput
) - 1; $i++
)
380 $line = $this->rawoutput
["$i"];
382 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
384 if (isset($this->logs
["$lastrev"]))
386 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
389 $this->logs
["$matches[1]"] = array(
390 'rev' => $matches[1],
391 'author' => $matches[2],
392 'date' => $matches[3],
393 'timezone' => $matches[4],
394 'lines' => $matches[6],
398 $lastrev = $matches[1];
400 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
402 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
404 $matches[2] = $amatches[1];
407 $this->logs
["$lastrev"]['files'][] = array(
408 'action' => $matches[1],
409 'file' => trim($matches[2]),
410 'from' => (isset($amatches[2]) ?
$amatches[2] : '')
415 if (trim($line) != 'Changed paths:')
417 $this->logs
["$lastrev"]['message'] .= $line . "\n";
422 if (isset($this->logs
["$lastrev"]))
424 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
429 * Trims the last dash line off a message
433 * @param string Message with annoying-ass line
435 * @return string Clean string
437 function strip_last_line($string)
439 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
444 * Diff system; constructs a diff array that
445 * is ready for output
452 * Array of diff information
458 * Raw "svn diff" output
464 * Constructor: create and store diff data
466 * @param string Repository
468 * @param integer Lower revision
469 * @param integer Higher revision
471 function SVNDiff(&$controller, $lorev, $hirev)
473 $this->rawoutput
= $controller->library
->diff($lorev, $hirev);
478 * Returns diffs for display
482 * @return array Diff data
490 * Processes and prepares diff data
499 $indexcounter = null
;
504 foreach ($this->rawoutput
AS $line)
506 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
510 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
511 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
512 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
515 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
519 $this->diff
["$index"]['props'] = array();
523 if ($indexcounter <= 3 AND $indexcounter !== null
)
528 else if ($indexcounter == 3)
530 $indexcounter = null
;
534 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
537 $content = $matches[2];
541 $this->diff
["$index"]["$chunk"][] = array(
544 'oldlineno' => ++
$lines['old'],
545 'newlineno' => ++
$lines['new']
550 else if ($act == '+')
552 // potential line delta
553 if (sizeof($delstack) > 0)
555 $lastline = array_shift($delstack);
557 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
559 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
561 $end = strlen($lastline['line']) +
$delta['end'];
562 $viewsvn->debug("RM delta- = " . $end);
563 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
564 $this->diff
["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
567 if (strlen($content) > $delta['start'] - $delta['end'])
569 $end = strlen($content) +
$delta['end'];
570 $viewsvn->debug("MK delta+ = " . $end);
571 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
572 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
577 $this->diff
["$index"]["$chunk"][] = array(
581 'newlineno' => ++
$lines['new']
584 else if ($act == '-')
586 $this->diff
["$index"]["$chunk"][] = $thearray = array(
589 'oldlineno' => ++
$lines['old'],
593 $key = sizeof($this->diff
["$index"]["$chunk"]) - 2;
594 $thearray['INDEX'] = $key;
596 array_push($delstack, $thearray);
602 if (preg_match('#^Index: (.*?)$#', $line, $matches))
604 $index = $matches[1];
612 if (preg_match('#^__*_$#', trim($line)))
614 $viewsvn->debug("skipping: $line");
618 if (preg_match('#Name: (.*?)$#', $line, $matches))
620 $curprop = $matches[1];
621 $viewsvn->debug("prop: $curprop");
626 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
629 $this->diff
["$index"]['props']["$curprop"]['add'] .= $matches[1];
631 else if (preg_match('#^\s+?\-(.*)#', $line, $matches))
634 $this->diff
["$index"]['props']["$curprop"]['del'] .= $matches[1];
636 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
638 $this->diff
["$index"]['props']["$curprop"]["$mode"] .= "\n" . $line;
644 $this->diff
["$index"]["$chunk"][] = array(
647 'oldlineno' => ++
$lines['old'],
648 'newlineno' => ++
$lines['new']
657 * Returns the amount of change that occured
662 * @param string Old line
663 * @param string New line
665 * @return array Difference of positions
667 function fetch_diff_extent($old, $new)
672 $min = min(strlen($old), strlen($new));
674 $viewsvn->debug("min1 = $min");
676 while ($start < $min AND $old["$start"] == $new["$start"])
682 $min = $min - $start;
684 $viewsvn->debug("min2 = $min");
686 $viewsvn->debug("checking: " . $old[ strlen($old) +
$end ] . ' == ' . $new[ strlen($new) +
$end ]);
688 while (-$end <= $min AND $old[ strlen($old) +
$end ] == $new[ strlen($new) +
$end ])
693 return array('start' => $start, 'end' => $end +
1);
697 /*=====================================================================*\
698 || ###################################################################
701 || ###################################################################
702 \*=====================================================================*/