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 string Repository
158 * @param integer Lower revision
159 * @param integer Higher revision
161 * @return array Lines of diff output
163 function diff($repos, $path, $lorev, $hirev)
167 $hirev = $this->rev($hirev);
168 $lorev = $this->rev($lorev);
169 if ($lorev == 'HEAD')
174 if (is_integer($hirev) AND is_integer($lorev))
180 if ($lorev == $hirev)
186 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
188 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
196 * @param string Repository
198 * @param integer Lower revision
199 * @param integer Higher revision
201 * @return array Lines of log output
203 function log($repos, $path, $lorev, $hirev)
207 $hirev = $this->rev($hirev);
208 $lorev = $this->rev($hirev);
209 if ($lorev == 'HEAD')
214 if (is_integer($hirev) AND is_integer($lorev))
220 if ($lorev == $hirev)
226 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
228 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
232 * SVN Wrapper: ls (list)
236 * @param object Controller object
238 * @return array Lines of list output
240 function ls(&$controller)
242 return $this->std('list');
247 * Annotation/blame system; constructs an array
248 * that is ready for output
256 * Array of blame information
259 var $blame = array();
262 * Raw "svn blame" output
268 * Constructor: create blame and store data
270 * @param string Repository
272 * @param integer Revision
274 function SVNBlame(&$controller)
276 $this->rawoutput
= $controller->library
->command('blame');
281 * Returns blame for display
285 * @return array Blame data
293 * Parses the blame data
301 foreach ($this->rawoutput
AS $line)
303 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
305 $this->blame
[] = array(
306 'rev' => $matches[1],
307 'author' => $matches[2],
308 'line' => $matches[3],
309 'lineno' => $lineno++
313 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
315 $this->blame
[] = array(
316 'rev' => $matches[1],
317 'author' => $matches[2],
319 'lineno' => $lineno++
327 * Log management system; creates a complex list
328 * of SVN log information
342 * Raw "svn log" output
348 * Constructor: create log store for the given file
350 * @param string Repository
352 * @param integer Lower revision
353 * @param integer Higher revision
355 function SVNLog(&$controller)
357 $this->rawoutput
= $controller->library
->command('log', null
);
362 * Returns logs for display
366 * @return array Log data
374 * Splits up the raw output into a usable log
382 for ($i = 1; $i <= sizeof($this->rawoutput
) - 1; $i++
)
384 $line = $this->rawoutput
["$i"];
386 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
388 if (isset($this->logs
["$lastrev"]))
390 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
393 $this->logs
["$matches[1]"] = array(
394 'rev' => $matches[1],
395 'author' => $matches[2],
396 'date' => $matches[3],
397 'timezone' => $matches[4],
398 'lines' => $matches[6],
402 $lastrev = $matches[1];
404 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
406 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
408 $matches[2] = $amatches[1];
411 $this->logs
["$lastrev"]['files'][] = array(
412 'action' => $matches[1],
413 'file' => trim($matches[2]),
414 'from' => (isset($amatches[2]) ?
$amatches[2] : '')
419 if (trim($line) != 'Changed paths:')
421 $this->logs
["$lastrev"]['message'] .= $line . "\n";
426 if (isset($this->logs
["$lastrev"]))
428 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
433 * Trims the last dash line off a message
437 * @param string Message with annoying-ass line
439 * @return string Clean string
441 function strip_last_line($string)
443 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
448 * Diff system; constructs a diff array that
449 * is ready for output
456 * Array of diff information
462 * Raw "svn diff" output
468 * Constructor: create and store diff data
470 * @param string Repository
472 * @param integer Lower revision
473 * @param integer Higher revision
475 function SVNDiff($repos, $path, $lorev, $hirev)
479 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
484 * Returns diffs for display
488 * @return array Diff data
496 * Processes and prepares diff data
505 $indexcounter = null
;
510 foreach ($this->rawoutput
AS $line)
512 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
516 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
517 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
518 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
521 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
525 $this->diff
["$index"]['props'] = array();
529 if ($indexcounter <= 3 AND $indexcounter !== null
)
534 else if ($indexcounter == 3)
536 $indexcounter = null
;
540 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
543 $content = $matches[2];
547 $this->diff
["$index"]["$chunk"][] = array(
550 'oldlineno' => ++
$lines['old'],
551 'newlineno' => ++
$lines['new']
556 else if ($act == '+')
558 // potential line delta
559 if (sizeof($delstack) > 0)
561 $lastline = array_shift($delstack);
563 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
565 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
567 $end = strlen($lastline['line']) +
$delta['end'];
568 $viewsvn->debug("RM delta- = " . $end);
569 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
570 $this->diff
["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
573 if (strlen($content) > $delta['start'] - $delta['end'])
575 $end = strlen($content) +
$delta['end'];
576 $viewsvn->debug("MK delta+ = " . $end);
577 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
578 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
583 $this->diff
["$index"]["$chunk"][] = array(
587 'newlineno' => ++
$lines['new']
590 else if ($act == '-')
592 $this->diff
["$index"]["$chunk"][] = $thearray = array(
595 'oldlineno' => ++
$lines['old'],
599 $key = sizeof($this->diff
["$index"]["$chunk"]) - 2;
600 $thearray['INDEX'] = $key;
602 array_push($delstack, $thearray);
608 if (preg_match('#^Index: (.*?)$#', $line, $matches))
610 $index = $matches[1];
618 if (preg_match('#^__*_$#', trim($line)))
620 $viewsvn->debug("skipping: $line");
624 if (preg_match('#Name: (.*?)$#', $line, $matches))
626 $curprop = $matches[1];
627 $viewsvn->debug("prop: $curprop");
632 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
635 $this->diff
["$index"]['props']["$curprop"]['add'] .= $matches[1];
637 else if (preg_match('#^\s+?\-(.*)#', $line, $matches))
640 $this->diff
["$index"]['props']["$curprop"]['del'] .= $matches[1];
642 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
644 $this->diff
["$index"]['props']["$curprop"]["$mode"] .= "\n" . $line;
650 $this->diff
["$index"]["$chunk"][] = array(
653 'oldlineno' => ++
$lines['old'],
654 'newlineno' => ++
$lines['new']
663 * Returns the amount of change that occured
668 * @param string Old line
669 * @param string New line
671 * @return array Difference of positions
673 function fetch_diff_extent($old, $new)
678 $min = min(strlen($old), strlen($new));
680 $viewsvn->debug("min1 = $min");
682 while ($start < $min AND $old["$start"] == $new["$start"])
688 $min = $min - $start;
690 $viewsvn->debug("min2 = $min");
692 $viewsvn->debug("checking: " . $old[ strlen($old) +
$end ] . ' == ' . $new[ strlen($new) +
$end ]);
694 while (-$end <= $min AND $old[ strlen($old) +
$end ] == $new[ strlen($new) +
$end ])
699 return array('start' => $start, 'end' => $end +
1);
703 /*=====================================================================*\
704 || ###################################################################
707 || ###################################################################
708 \*=====================================================================*/