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 library and SVN-PHP bridge extension
29 * This class acts as the bridge between the command line Xquery class
30 * and the various display files. It handles preparation of Xquery
39 * Path to the SVN binary
52 // ###################################################################
54 * Constructor: validate SVN path
56 * @param object Controller
58 function SVNLib(&$controller)
60 $this->controller
=& $controller;
62 $this->svnpath
=& $this->controller
->xquery
->cmd($this->controller
->registry
->svnpath
);
64 $access = $this->controller
->xquery
->exec($this->svnpath
. ' --version');
68 $this->controller
->registry
->trigger
->error($this->controller
->registry
->lang
->string('The SVN binary could not be located'));
71 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
73 $this->controller
->registry
->trigger
->error($this->controller
->registry
->lang
->string('The SVN binary does not appear to be valid (it failed our tests)'));
77 // ###################################################################
79 * Executes the SVN binary
83 * @param string Command
85 * @return array Output
87 function svn($command)
89 $output = $this->controller
->xquery
->exec($this->svnpath
. ' ' . $command . ' 2>&1');
91 $temp = implode("\n", $output);
92 if (strpos($temp, '(apr' . '_err=') !== false
)
94 $this->controller
->registry
->trigger
->error(nl2br($temp));
99 // ###################################################################
101 * This function is used to prepare a common command. It sends the
102 * specified command along with the repository path and the current
103 * node. The revision is either fectched from the argument list or
104 * from the node, too.
108 * @param string SVN command
109 * @param bool Alternate revision
111 * @return array Lines of output
113 function command($command, $rev = false
)
117 $revision = ($rev !== false ?
$rev : SVNCommon
::rev($this->controller
->revnum
));
119 return $this->svn($command . ' ' . $this->controller
->repospath
. $this->controller
->path
. ($revision !== null ?
'@' . $revision : ''));
122 // ###################################################################
124 * A library complicator function that handles the diff command because
125 * the standard command() system does not work with more complex args.
129 * @param integer Lower revision
130 * @param integer Higher revision
132 * @return array Lines of diff output
134 function diff($lorev, $hirev)
138 $hirev = SVNCommon
::rev($hirev);
139 $lorev = SVNCommon
::rev($lorev);
140 if ($lorev == 'HEAD')
145 if (is_integer($hirev) AND is_integer($lorev))
151 if ($lorev == $hirev)
157 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $this->controller
->repospath
. $this->controller
->path
);
160 // ###################################################################
162 * A library complicator function to create log output. This is needed
163 * because command() doesn't handle ranged revisions.
167 * @param string Repository
169 * @param integer Lower revision
170 * @param integer Higher revision
172 * @return array Lines of log output
174 function log($lorev, $hirev)
178 $hirev = $this->rev($hirev);
179 $lorev = $this->rev($hirev);
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('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
204 * Annotation/blame system; constructs an array that is ready for output
212 * Array of blame information
216 var $blame = array();
219 * Raw "svn blame" output
225 // ###################################################################
227 * Constructor: create blame and store data
229 * @param object Controller
231 function SVNBlame(&$controller)
233 $this->rawoutput
= $controller->library
->command('blame');
237 // ###################################################################
239 * Returns blame for display
243 * @return array Blame data
250 // ###################################################################
252 * Parses the blame data
260 foreach ($this->rawoutput
AS $line)
262 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
264 $this->blame
[] = array(
265 'rev' => $matches[1],
266 'author' => $matches[2],
267 'line' => $matches[3],
268 'lineno' => $lineno++
272 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
274 $this->blame
[] = array(
275 'rev' => $matches[1],
276 'author' => $matches[2],
278 'lineno' => $lineno++
286 * Log management system; creates a complex list of SVN log information
301 * Raw "svn log" output
307 // ###################################################################
309 * Constructor: create log store for the given file
311 * @param object Controller
313 function SVNLog(&$controller)
315 $this->rawoutput
= $controller->library
->command('log', null
);
319 // ###################################################################
321 * Returns logs for display
325 * @return array Log data
332 // ###################################################################
334 * Splits up the raw output into a usable log
342 for ($i = 1; $i <= sizeof($this->rawoutput
) - 1; $i++
)
344 $line = $this->rawoutput
["$i"];
346 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
348 if (isset($this->logs
["$lastrev"]))
350 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
353 $this->logs
["$matches[1]"] = array(
354 'rev' => $matches[1],
355 'author' => $matches[2],
356 'date' => $matches[3],
357 'timezone' => $matches[4],
358 'lines' => $matches[6],
362 $lastrev = $matches[1];
364 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
366 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
368 $matches[2] = $amatches[1];
371 $this->logs
["$lastrev"]['files'][] = array(
372 'action' => $matches[1],
373 'file' => trim($matches[2]),
374 'from' => (isset($amatches[2]) ?
$amatches[2] : '')
379 if (trim($line) != 'Changed paths:')
381 $this->logs
["$lastrev"]['message'] .= $line . "\n";
386 if (isset($this->logs
["$lastrev"]))
388 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
392 // ###################################################################
394 * Trims the last dash line off a message
398 * @param string Message with annoying-ass line
400 * @return string Clean string
402 function strip_last_line($string)
404 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
409 * Diff system; constructs a diff array that is ready for output
417 * Array of diff information
424 * Raw "svn diff" output
430 // ###################################################################
432 * Constructor: create and store diff data
434 * @param object Controller
435 * @param integer Lower revision
436 * @param integer Higher revision
438 function SVNDiff(&$controller, $lorev, $hirev)
440 $this->rawoutput
= $controller->library
->diff($lorev, $hirev);
444 // ###################################################################
446 * Returns diffs for display
450 * @return array Diff data
457 // ###################################################################
459 * Processes and prepares diff data
468 $indexcounter = null
;
473 foreach ($this->rawoutput
AS $line)
475 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
479 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
480 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
481 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
484 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
488 $this->diff
["$index"]['props'] = array();
492 if ($indexcounter <= 3 AND $indexcounter !== null
)
497 else if ($indexcounter == 3)
499 $indexcounter = null
;
503 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
506 $content = $matches[2];
510 $this->diff
["$index"]["$chunk"][] = array(
513 'oldlineno' => ++
$lines['old'],
514 'newlineno' => ++
$lines['new']
519 else if ($act == '+')
521 // potential line delta
522 if (sizeof($delstack) > 0)
524 $lastline = array_shift($delstack);
526 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
528 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
530 $end = strlen($lastline['line']) +
$delta['end'];
531 $viewsvn->debug("RM delta- = " . $end);
532 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
533 $this->diff
["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
536 if (strlen($content) > $delta['start'] - $delta['end'])
538 $end = strlen($content) +
$delta['end'];
539 $viewsvn->debug("MK delta+ = " . $end);
540 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
541 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
546 $this->diff
["$index"]["$chunk"][] = array(
550 'newlineno' => ++
$lines['new']
553 else if ($act == '-')
555 $this->diff
["$index"]["$chunk"][] = $thearray = array(
558 'oldlineno' => ++
$lines['old'],
562 $key = sizeof($this->diff
["$index"]["$chunk"]) - 2;
563 $thearray['INDEX'] = $key;
565 array_push($delstack, $thearray);
571 if (preg_match('#^Index: (.*?)$#', $line, $matches))
573 $index = $matches[1];
581 if (preg_match('#^__*_$#', trim($line)))
583 $viewsvn->debug("skipping: $line");
587 if (preg_match('#Name: (.*?)$#', $line, $matches))
589 $curprop = $matches[1];
590 $viewsvn->debug("prop: $curprop");
595 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
598 $this->diff
["$index"]['props']["$curprop"]['add'] .= $matches[1];
600 else if (preg_match('#^\s+?\-(.*)#', $line, $matches))
603 $this->diff
["$index"]['props']["$curprop"]['del'] .= $matches[1];
605 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
607 $this->diff
["$index"]['props']["$curprop"]["$mode"] .= "\n" . $line;
613 $this->diff
["$index"]["$chunk"][] = array(
616 'oldlineno' => ++
$lines['old'],
617 'newlineno' => ++
$lines['new']
625 // ###################################################################
627 * Returns the amount of change that occured between two lines
631 * @param string Old line
632 * @param string New line
634 * @return array Difference of positions
636 function fetch_diff_extent($old, $new)
641 $min = min(strlen($old), strlen($new));
643 $viewsvn->debug("min1 = $min");
645 while ($start < $min AND $old["$start"] == $new["$start"])
651 $min = $min - $start;
653 $viewsvn->debug("min2 = $min");
655 $viewsvn->debug("checking: " . $old[ strlen($old) +
$end ] . ' == ' . $new[ strlen($new) +
$end ]);
657 while (-$end <= $min AND $old[ strlen($old) +
$end ] == $new[ strlen($new) +
$end ])
662 return array('start' => $start, 'end' => $end +
1);
666 /*=====================================================================*\
667 || ###################################################################
670 || ###################################################################
671 \*=====================================================================*/