]>
src.bluestatic.org Git - viewsvn.git/blob - includes/svnlib.php
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($viewsvn->lang
->string('The SVN binary could not be located'));
69 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
71 $viewsvn->trigger
->error($viewsvn->lang
->string('The SVN binary does not appear to be valid (it failed our tests)'));
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);
95 $string = preg_replace('#( +)#e', '$this->format_spaces("\1")', $string);
100 $string = str_replace(' ', ' ', $string);
103 // convert advanced diff
104 $string = str_replace(array('{@+' . '+}', '{@-' . '-}'), array('<span class="diff_add_bit">', '<span class="diff_del_bit">'), $string);
105 $string = str_replace(array('{/@+' . '+}', '{/@-' . '-}'), '</span>', $string);
108 $string = nl2br($string);
114 * Formats a SVN log message
118 * @param string Unformatted log message
120 * @return string Output-ready log message
122 function format_log_message($message)
126 $message = $viewsvn->entity_encode($message);
128 $message = preg_replace('#r([0-9]+)#e', '"<a href=\"" . $viewsvn->path . "/" . $viewsvn->paths->out("diff.php" . $viewsvn->paths->fetch_rev_str(true, "\1", 0), $viewsvn->paths->fetch_repos($viewsvn->paths->parse()) . "/") . "\">r\1</a>"', $message);
131 $lines = explode("\n", $message);
133 foreach ($lines AS $line)
135 if (preg_match('#^\s*?(\*|\-)\s?(.*)#', $line, $matches))
139 $message .= '<li>' . $matches[2] . '</li>';
143 $message .= '<ul class="list">';
144 $message .= '<li>' . $matches[2] . '</li>';
158 $message .= '<br />';
171 $message = preg_replace('#(<br />)*$#', '', $message);
176 // ###################################################################
178 * Parses a date from Xquery XML outut
182 * @param string Date string
184 * @return string Formatted and readable date string
186 function format_date_string($string)
188 // 2005-01-23T20:42:53.703057Z
189 return preg_replace('#(....)\-(..)\-(..)T(..):(..):(..).(.*)Z#e', 'gmdate("r", mktime(\4, \5, \6, \2, \3, \1))', $string);
193 * Counts the spaces and replaces two or more ones
197 * @param string Spaced string
199 * @return string 'd string
201 function format_spaces($thestring)
203 if (strlen($thestring) >= 2)
205 $thestring = str_replace(' ', ' ', $thestring);
212 * Executes the SVN binary
216 * @param string Command
218 * @return array Output
220 function svn($command)
224 $output = $viewsvn->shell
->exec($this->svnpath
. ' ' . $command . ' 2>&1');
226 // make sure that we keep escaped chars
227 //$output = str_replace(array('\t', '\n', '\r'), array('\\\t', '\\\n', '\\\r'), $output);
228 //$output = preg_replace('#\\\(.)#', '\\\\\\\\' . '\1', $output);
229 //$output = str_replace('\\', '\\\\', $output);
231 $temp = implode("\n", $output);
232 if (strpos($temp, '(apr' . '_err=') !== false)
234 $viewsvn->trigger
->error(nl2br($temp));
240 * SVN Wrapper: standard command system
244 * @param string SVN command
245 * @param string Repository
247 * @param integer Revision
249 * @return array Lines of output
251 function std($command, $repos, $path, $revision)
255 $revision = $this->rev($revision);
256 $repospath = $viewsvn->repos
->fetch_path($repos, false);
258 return $this->svn($command . ' ' . $repospath . $path . '@' . $revision);
266 * @param string Repository
268 * @param integer Revision
270 * @return array Lines of blame output
272 function blame($repos, $path, $revision)
274 return $this->std('blame', $repos, $path, $revision);
282 * @param string Repository
284 * @param integer Revision
286 * @return array Lines of cat output
288 function cat($repos, $path, $revision)
290 return $this->std('cat', $repos, $path, $revision);
298 * @param string Repository
300 * @param integer Lower revision
301 * @param integer Higher revision
303 * @return array Lines of diff output
305 function diff($repos, $path, $lorev, $hirev)
309 $hirev = $this->rev($hirev);
310 $lorev = $this->rev($lorev);
311 if ($lorev == 'HEAD')
316 if (is_integer($hirev) AND is_integer($lorev))
322 if ($lorev == $hirev)
328 $repospath = $viewsvn->repos
->fetch_path($repos, false);
330 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
338 * @param string Repository
340 * @param integer Lower revision
341 * @param integer Higher revision
343 * @return array Lines of log output
345 function log($repos, $path, $lorev, $hirev)
349 $hirev = $this->rev($hirev);
350 $lorev = $this->rev($hirev);
351 if ($lorev == 'HEAD')
356 if (is_integer($hirev) AND is_integer($lorev))
362 if ($lorev == $hirev)
368 $repospath = $viewsvn->repos
->fetch_path($repos, false);
370 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
374 * SVN Wrapper: ls (list)
378 * @param string Repository
380 * @param integer Revision
382 * @return array Lines of list output
384 function ls($repos, $path, $revision)
386 return $this->std('list', $repos, $path, $revision);
390 * Generates a clean revision number
394 * @param integer Revision number
396 * @return mixed Cleaned revision or HEAD
398 function rev($revision)
400 if (($revision = intval($revision)) < 1)
409 * Commonly executed SVN commands that return data
410 * used in many parts of the system
436 * Constructor: bind with registry
442 $this->registry
=& $viewsvn;
446 * Checks to see if the given universal path is
451 * @param string Universal path
453 * @return bool Directory or not
455 function isdir($path)
457 $output = $this->registry
->svn
->std('info', $this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), $this->registry
->paths
->revnum
);
459 foreach ($output AS $line)
461 if (preg_match('#^Node Kind: (.*)#', $line, $matches))
463 if (trim(strtolower($matches[1])) == 'directory')
474 * Get a list of revisions for a path
478 * @param string Universal path
480 * @return array Key revisions
482 function fetch_revs($path)
484 if (!isset($this->revisions
["$path"]))
486 $log = $this->fetch_logs($path);
488 $revs = array_keys($log);
490 $this->revisions["$path"] = array(
492 'START' => $revs[ count($revs) - 1 ],
497 return $this->revisions
["$path"];
501 * Gets the revision that is marked as HEAD
505 * @param string Universal path
507 * @return integer Revision
509 function fetch_head_rev($path)
511 $output = $this->registry->shell->exec($this->registry->svn->svnpath . ' info ' . $this->registry->repos->fetch_path($this->registry->paths->fetch_repos($path), false) . $this->registry->paths->fetch_path($path));
513 foreach ($output AS $line)
515 if (preg_match('#^Last Changed Rev: (.*)#', $line, $matches))
521 $revs = $this->fetch_revs($path);
522 return $revs['HEAD'];
526 * Returns the previous revision to the one given
530 * @param string Universal path
531 * @param integer Arbitrary revision
533 * @return integer Previous revision (-1 if none)
535 function fetch_prev_rev($path, $current)
539 $revs = $this->fetch_revs($path);
541 if ($current == 'HEAD')
543 $current = $this->fetch_head_rev($path);
546 $index = array_search($current, $revs['revs']);
547 if ($current === false)
549 $message->trigger->error(sprintf($viewsvn->lang->string('Revision r%1$s is not in path %2$s'), $current, $path));
552 if (isset($revs['revs'][ $index + 1 ]))
554 return $revs['revs'][ $index + 1 ];
567 * @param string Universal path
568 * @param bool Override the cache system?
570 * @return array Log data
572 function fetch_logs($path)
574 if (!isset($this->logs["$path"]))
576 $log = new SVNLog($this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 0, $this->registry
->paths
->revnum
);
578 $this->logs
["$path"] = $log->fetch();
581 return $this->logs["$path"];
585 * Returns a given log entry for a path
590 * @param string Universal path
591 * @param integer Arbitrary revision
593 * @return array Log entry
595 function fetch_log($path, $rev)
597 $logs = $this->fetch_logs($path);
599 $rev = $this->registry
->svn
->rev($rev);
602 $rev = $this->fetch_head_rev($path);
605 if (isset($logs["$rev"]))
607 return $logs["$rev"];
611 $keys = array_keys($logs);
614 for ($i = 0; $i < count($keys); $i++
)
616 if ($rev > $keys["$i"] AND $rev < $keys[ $i + 1 ])
618 return $logs["$keys[$i]"];
627 * Prints the file changed list
631 * @public array List of file changes
632 * @public string The repository
633 * @public integer Current revision
635 * @return string Processed HTML
637 function construct_file_changes($changes, $repos, $revision)
643 foreach ($changes AS $file)
645 switch ($file['action'])
649 $tooltip = $viewsvn->lang->string('Added');
652 $class = 'file_delete';
653 $tooltip = $viewsvn->lang->string('Deleted');
656 $class = 'file_modify';
657 $tooltip = $viewsvn->lang->string('Modified');
660 $class = 'file_replace';
661 $tooltip = $viewsvn->lang->string('Replaced');
665 $show['from'] = (bool)$file['from'];
669 $class = 'file_move';
670 $tooltip = 'Moved/Copied';
671 preg_match('#(.*):([0-9]+)#', $file['from'], $matches);
672 $link['from'] = $viewsvn->paths->out('view.php' . $viewsvn->paths->fetch_rev_str(false, $matches[2]), $repos . $matches[1]);
675 $link['file'] = $viewsvn->paths->out('view.php' . $viewsvn->paths->fetch_rev_str(false, $revision), $repos . $file['file']);
677 eval('$files .= "' . $viewsvn->template->fetch('file_change
') . '";');
685 * Annotation/blame system; constructs an array
686 * that is ready for output
694 * Array of blame information
697 var $blame = array();
700 * Raw "svn blame
" output
706 * Constructor: create blame and store data
708 * @param string Repository
710 * @param integer Revision
712 function SVNBlame($repos, $path, $revision)
716 $this->rawoutput = $viewsvn->svn->blame($repos, $path, $revision);
721 * Returns blame for display
725 * @return array Blame data
733 * Parses the blame data
741 foreach ($this->rawoutput AS $line)
743 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
745 $this->blame[] = array(
746 'rev' => $matches[1],
747 'author' => $matches[2],
748 'line' => $matches[3],
749 'lineno' => $lineno++
753 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
755 $this->blame[] = array(
756 'rev' => $matches[1],
757 'author' => $matches[2],
759 'lineno' => $lineno++
767 * Log management system; creates a complex list
768 * of SVN log information
782 * Raw "svn log
" output
788 * Constructor: create log store for the given file
790 * @param string Repository
792 * @param integer Lower revision
793 * @param integer Higher revision
795 function SVNLog($repos, $path, $lorev, $hirev)
799 $this->rawoutput = $viewsvn->svn->log($repos, $path, $lorev, $hirev);
804 * Returns logs for display
808 * @return array Log data
816 * Splits up the raw output into a usable log
824 for ($i = 1; $i <= count($this->rawoutput) - 1; $i++)
826 $line = $this->rawoutput["$i"];
828 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
830 if (isset($this->logs
["$lastrev"]))
832 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
835 $this->logs["$matches[1]"] = array(
836 'rev' => $matches[1],
837 'author' => $matches[2],
838 'date' => $matches[3],
839 'timezone' => $matches[4],
840 'lines' => $matches[6],
844 $lastrev = $matches[1];
846 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
848 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
850 $matches[2] = $amatches[1];
853 $this->logs["$lastrev"]['files'][] = array(
854 'action' => $matches[1],
855 'file' => trim($matches[2]),
856 'from' => (isset($amatches[2]) ? $amatches[2] : '')
861 if (trim($line) != 'Changed paths:')
863 $this->logs
["$lastrev"]['message'] .= $line . "\n
";
868 if (isset($this->logs["$lastrev"]))
870 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
875 * Trims the last dash line off a message
879 * @param string Message with annoying-ass line
881 * @return string Clean string
883 function strip_last_line($string)
885 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
890 * Diff system; constructs a diff array that
891 * is ready for output
898 * Array of diff information
904 * Raw "svn diff" output
910 * Constructor: create and store diff data
912 * @param string Repository
914 * @param integer Lower revision
915 * @param integer Higher revision
917 function SVNDiff($repos, $path, $lorev, $hirev)
921 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
926 * Returns diffs for display
930 * @return array Diff data
938 * Processes and prepares diff data
947 $indexcounter = null;
952 foreach ($this->rawoutput
AS $line)
954 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
958 $this->diff
["$index"][ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
959 $lines['old'] = $this->diff["$index"]["$chunk"]['hunk']['old']['line'] - 1;
960 $lines['new'] = $this->diff["$index"]["$chunk"]['hunk']['new']['line'] - 1;
963 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
967 $this->diff["$index"]['props'] = array();
971 if ($indexcounter <= 3 AND $indexcounter !== null)
976 else if ($indexcounter == 3)
978 $indexcounter = null;
982 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
985 $content = $matches[2];
989 $this->diff
["$index"]["$chunk"][] = array(
992 'oldlineno' => ++
$lines['old'],
993 'newlineno' => ++
$lines['new']
998 else if ($act == '+')
1000 // potential line delta
1001 if (count($delstack) > 0)
1003 $lastline = array_shift($delstack);
1005 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
1007 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
1009 $end = strlen($lastline['line']) +
$delta['end'];
1010 $viewsvn->debug("RM delta- = " . $end);
1011 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
1012 $this->diff
["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
1015 if (strlen($content) > $delta['start'] - $delta['end'])
1017 $end = strlen($content) +
$delta['end'];
1018 $viewsvn->debug("MK delta+ = " . $end);
1019 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
1020 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
1025 $this->diff
["$index"]["$chunk"][] = array(
1029 'newlineno' => ++
$lines['new']
1032 else if ($act == '-')
1034 $this->diff
["$index"]["$chunk"][] = $thearray = array(
1037 'oldlineno' => ++
$lines['old'],
1041 $key = count($this->diff
["$index"]["$chunk"]) - 2;
1042 $thearray['INDEX'] = $key;
1044 array_push($delstack, $thearray);
1050 if (preg_match('#^Index: (.*?)$#', $line, $matches))
1052 $index = $matches[1];
1060 if (preg_match('#^__*_$#', trim($line)))
1062 $viewsvn->debug("skipping: $line");
1066 if (preg_match('#Name: (.*?)$#', $line, $matches))
1068 $curprop = $matches[1];
1069 $viewsvn->debug("prop
: $curprop");
1074 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
1077 $this->diff
["$index"]['props']["$curprop"]['add'] .= $matches[1];
1079 else if (preg_match('#^\s+?\-(.*)#', $line, $matches))
1082 $this->diff
["$index"]['props']["$curprop"]['del'] .= $matches[1];
1084 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
1086 $this->diff
["$index"]['props']["$curprop"]["$mode"] .= "\n
" . $line;
1092 $this->diff["$index"]["$chunk"][] = array(
1095 'oldlineno' => ++$lines['old'],
1096 'newlineno' => ++$lines['new']
1099 $delstack = array();
1105 * Returns the amount of change that occured
1110 * @param string Old line
1111 * @param string New line
1113 * @return array Difference of positions
1115 function fetch_diff_extent($old, $new)
1120 $min = min(strlen($old), strlen($new));
1122 $viewsvn->debug("min1
= $min");
1124 while ($start < $min AND $old["$start"] == $new["$start"])
1130 $min = $min - $start;
1132 $viewsvn->debug("min2 = $min");
1134 $viewsvn->debug("checking
: " . $old[ strlen($old) + $end ] . ' == ' . $new[ strlen($new) + $end ]);
1136 while (-$end <= $min AND $old[ strlen($old) + $end ] == $new[ strlen($new) + $end ])
1141 return array('start' => $start, 'end' => $end + 1);
1145 /*=====================================================================*\
1146 || ###################################################################
1149 || ###################################################################
1150 \*=====================================================================*/