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
44 * Common command system
50 * Constructor: validate SVN path
52 * @param string Path to SVN binary
54 function SVNLib($svnpath)
58 $this->svnpath
= $viewsvn->shell
->cmd($svnpath);
60 $this->common
=& new SVNCommon();
62 $access = $viewsvn->shell
->exec($this->svnpath
. ' --version');
66 $viewsvn->trigger
->error('svn binary could not be found');
69 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
71 $viewsvn->trigger
->error('svn binary does not pass test');
76 * Prepares data for output
80 * @param string Standard data
82 * @return string Output-ready data
84 function format($string)
87 $string = htmlspecialchars($string);
90 $string = str_replace("\t", ' ', $string);
93 $string = str_replace(' ', ' ', $string);
95 // convert advanced diff
96 $string = str_replace(array('{@++}', '{@--}'), array('<span class="diff_add_bit">', '<span class="diff_del_bit">'), $string);
97 $string = str_replace(array('{/@++}', '{/@--}'), '</span>', $string);
100 $string = nl2br($string);
106 * Executes the SVN binary
110 * @param string Command
112 * @return array Output
114 function svn($command)
118 $output = $viewsvn->shell
->exec($this->svnpath
. ' ' . $command . ' 2>&1');
119 $temp = implode("\n", $output);
120 if (strpos($temp, '(apr' . '_err=') !== false
)
122 $viewsvn->trigger
->error(nl2br($temp));
128 * SVN Wrapper: standard command system
132 * @param string SVN command
133 * @param string Repository
135 * @param integer Revision
137 * @return array Lines of output
139 function std($command, $repos, $path, $revision)
143 $revision = $this->rev($revision);
144 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
146 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
154 * @param string Repository
156 * @param integer Revision
158 * @return array Lines of blame output
160 function blame($repos, $path, $revision)
162 return $this->std('blame', $repos, $path, $revision);
170 * @param string Repository
172 * @param integer Revision
174 * @return array Lines of cat output
176 function cat($repos, $path, $revision)
178 return $this->std('cat', $repos, $path, $revision);
186 * @param string Repository
188 * @param integer Lower revision
189 * @param integer Higher revision
191 * @return array Lines of diff output
193 function diff($repos, $path, $lorev, $hirev)
197 $hirev = $this->rev($hirev);
198 $lorev = $this->rev($lorev);
199 if ($lorev == 'HEAD')
204 if (is_integer($hirev) AND is_integer($lorev))
210 if ($lorev == $hirev)
216 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
218 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
226 * @param string Repository
228 * @param integer Lower revision
229 * @param integer Higher revision
231 * @return array Lines of log output
233 function log($repos, $path, $lorev, $hirev)
237 $hirev = $this->rev($hirev);
238 $lorev = $this->rev($hirev);
239 if ($lorev == 'HEAD')
244 if (is_integer($hirev) AND is_integer($lorev))
250 if ($lorev == $hirev)
256 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
258 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
262 * SVN Wrapper: ls (list)
266 * @param string Repository
268 * @param integer Revision
270 * @return array Lines of list output
272 function ls($repos, $path, $revision)
274 return $this->std('list', $repos, $path, $revision);
278 * Generates a clean revision number
282 * @param integer Revision number
284 * @return mixed Cleaned revision or HEAD
286 function rev($revision)
288 if (($revision = intval($revision)) < 1)
297 * Commonly executed SVN commands that return data
298 * used in many parts of the system
324 * Constructor: bind with registry
330 $this->registry
=& $viewsvn;
334 * Checks to see if the given universal path is
339 * @param string Universal path
341 * @return bool Directory or not
343 function isdir($path)
345 $output = $this->registry
->svn
->std('info', $this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 'HEAD');
347 foreach ($output AS $line)
349 if (preg_match('#^Node Kind: (.*)#', $line, $matches))
351 if (trim(strtolower($matches[1])) == 'directory')
362 * Get a list of revisions for a path
366 * @param string Universal path
368 * @return array Key revisions
370 function fetch_revs($path)
372 if (!isset($this->revisions
["$path"]))
374 $log = $this->fetch_logs($path);
376 $revs = array_keys($log);
378 $this->revisions
["$path"] = array(
380 'START' => $revs[ count($revs) - 1 ],
385 return $this->revisions
["$path"];
389 * Gets the revision that is marked as HEAD
393 * @param string Universal path
395 * @return integer Revision
397 function fetch_head_rev($path)
399 $revs = $this->fetch_revs($path);
400 return $revs['HEAD'];
404 * Returns the previous revision to the one given
408 * @param string Universal path
409 * @param integer Arbitrary revision
411 * @return integer Previous revision (-1 if none)
413 function fetch_prev_rev($path, $current)
415 $revs = $this->fetch_revs($path);
417 if ($current == 'HEAD')
419 $current = $this->fetch_head_rev($path);
422 $index = array_search($current, $revs['revs']);
423 if ($current === false
)
425 $message->trigger
->error('revision ' . $current . ' is not in ' . $path);
428 if (isset($revs['revs'][ $index +
1 ]))
430 return $revs['revs'][ $index +
1 ];
443 * @param string Universal path
445 * @return array Log data
447 function fetch_logs($path)
449 if (!isset($this->logs
["$path"]))
451 $log = new SVNLog($this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 0, 0);
453 $this->logs
["$path"] = $log->fetch();
456 return $this->logs
["$path"];
460 * Returns a given log entry for a path
465 * @param string Universal path
466 * @param integer Arbitrary revision
468 * @return array Log entry
470 function fetch_log($path, $rev)
472 $logs = $this->fetch_logs($path);
474 $rev = $this->registry
->svn
->rev($rev);
477 $rev = $this->fetch_head_rev($path);
480 if (isset($logs["$rev"]))
482 return $logs["$rev"];
492 * Annotation/blame system; constructs an array
493 * that is ready for output
501 * Array of blame information
504 var $blame = array();
507 * Raw "svn blame" output
513 * Constructor: create blame and store data
515 * @param string Repository
517 * @param integer Revision
519 function SVNBlame($repos, $path, $revision)
523 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
528 * Returns blame for display
532 * @return array Blame data
540 * Parses the blame data
548 foreach ($this->rawoutput
AS $line)
550 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
552 $this->blame
[] = array(
553 'rev' => $matches[1],
554 'author' => $matches[2],
555 'line' => $matches[3],
556 'lineno' => $lineno++
560 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
562 $this->blame
[] = array(
563 'rev' => $matches[1],
564 'author' => $matches[2],
566 'lineno' => $lineno++
574 * Log management system; creates a complex list
575 * of SVN log information
589 * Raw "svn log" output
595 * Constructor: create log store for the given file
597 * @param string Repository
599 * @param integer Lower revision
600 * @param integer Higher revision
602 function SVNLog($repos, $path, $lorev, $hirev)
606 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
611 * Returns logs for display
615 * @return array Log data
623 * Splits up the raw output into a usable log
631 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
633 $line = $this->rawoutput
["$i"];
635 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
637 if (isset($this->logs
["$lastrev"]))
639 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
642 $this->logs
["$matches[1]"] = array(
643 'rev' => $matches[1],
644 'author' => $matches[2],
645 'date' => $matches[3],
646 'timezone' => $matches[4],
647 'lines' => $matches[6],
651 $lastrev = $matches[1];
653 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
655 $this->logs
["$lastrev"]['files'][] = array(
656 'action' => $matches[1],
657 'file' => $matches[2]
662 if (trim($line) != 'Changed paths:')
664 $this->logs
["$lastrev"]['message'] .= $line . "\n";
669 if (isset($this->logs
["$lastrev"]))
671 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
676 * Trims the last dash line off a message
680 * @param string Message with annoying-ass line
682 * @return string Clean string
684 function strip_last_line($string)
686 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
691 * Diff system; constructs a diff array that
692 * is ready for output
699 * Array of diff information
705 * Raw "svn diff" output
711 * Constructor: create and store diff data
713 * @param string Repository
715 * @param integer Lower revision
716 * @param integer Higher revision
718 function SVNDiff($repos, $path, $lorev, $hirev)
722 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
727 * Returns diffs for display
731 * @return array Diff data
739 * Processes and prepares diff data
746 $indexcounter = null
;
751 foreach ($this->rawoutput
AS $line)
753 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
758 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
759 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
760 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
764 if ($indexcounter <= 5 AND $indexcounter !== null
)
769 else if ($indexcounter == 5)
771 $indexcounter = null
;
775 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
778 $content = $matches[2];
782 $this->diff
["$index"]["$chunk"][] = array(
785 'oldlineno' => ++
$lines['old'],
786 'newlineno' => ++
$lines['new']
789 else if ($act == '+')
791 // potential line delta
794 if ($delta = @$this->fetch_diff_extent($lastcontent, $content))
796 // create two sets of ends for the two contents
797 $delta['endo'] = strlen($lastcontent) - $delta['end'];
798 $delta['endn'] = strlen($content) - $delta['end'];
800 $diffo = $delta['endo'] - $delta['start'];
801 $diffn = $delta['endn'] - $delta['start'];
803 if (strlen($lastcontent) > $delta['endo'] - $diffo)
805 $removed = substr($lastcontent, $delta['start'], $diffo);
806 $this->diff
["$index"]["$chunk"][ count($this->diff
["$index"]["$chunk"]) - 2 ]['line'] = substr_replace($lastcontent, '{@--}' . $removed . '{/@--}', $delta['start'], $diffo);
809 if (strlen($content) > $delta['endn'] - $diffn)
811 $added = substr($content, $delta['start'], $diffn);
812 $content = substr_replace($content, '{@++}' . $added . '{/@++}', $delta['start'], $diffn);
817 $this->diff
["$index"]["$chunk"][] = array(
821 'newlineno' => ++
$lines['new']
824 else if ($act == '-')
826 $lastcontent = $content;
828 $this->diff
["$index"]["$chunk"][] = array(
831 'oldlineno' => ++
$lines['old'],
841 if (preg_match('#^Index: (.*?)$#', $line, $matches))
843 $index = $matches[1];
851 $this->diff
["$index"]["$chunk"][] = array(
854 'oldlineno' => ++
$lines['old'],
855 'newlineno' => ++
$lines['new']
862 * Returns the amount of change that occured
867 * @param string Old line
868 * @param string New line
870 * @return array Difference of positions
872 function fetch_diff_extent($old, $new)
875 $min = min(strlen($old), strlen($new));
877 for ($start = 0; $start < $min; $start++
)
879 if ($old{"$start"} != $new{"$start"})
885 $max = max(strlen($old), strlen($new));
887 for ($end = 0; $end < $min; $end++
)
889 $oldpos = strlen($old) - $end;
890 $newpos = strlen($new) - $end;
892 if ($old{"$oldpos"} != $new{"$newpos"})
900 if ($start == 0 AND $end == $max)
905 return array('start' => $start, 'end' => $end);
909 /*=====================================================================*\
910 || ###################################################################
913 || ###################################################################
914 \*=====================================================================*/