]>
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('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);
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 * Counts the spaces and replaces two or more ones
118 * @param string Spaced string
120 * @return string 'd string
122 function format_spaces($thestring)
124 if (strlen($thestring) >= 2)
126 $thestring = str_replace(' ', ' ', $thestring);
133 * Executes the SVN binary
137 * @param string Command
139 * @return array Output
141 function svn($command)
145 $output = $viewsvn->shell
->exec($this->svnpath
. ' ' . $command . ' 2>&1');
147 // make sure that we keep escaped chars
148 //$output = str_replace(array('\t', '\n', '\r'), array('\\\t', '\\\n', '\\\r'), $output);
149 //$output = preg_replace('#\\\(.)#', '\\\\\\\\' . '\1', $output);
150 $output = str_replace('\\', '\\\\', $output);
152 $temp = implode("\n", $output);
153 if (strpos($temp, '(apr' . '_err=') !== false)
155 $viewsvn->trigger
->error(nl2br($temp));
161 * SVN Wrapper: standard command system
165 * @param string SVN command
166 * @param string Repository
168 * @param integer Revision
170 * @return array Lines of output
172 function std($command, $repos, $path, $revision)
176 $revision = $this->rev($revision);
177 $repospath = $viewsvn->repos
->fetch_path($repos, false);
179 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
187 * @param string Repository
189 * @param integer Revision
191 * @return array Lines of blame output
193 function blame($repos, $path, $revision)
195 return $this->std('blame', $repos, $path, $revision);
203 * @param string Repository
205 * @param integer Revision
207 * @return array Lines of cat output
209 function cat($repos, $path, $revision)
211 return $this->std('cat', $repos, $path, $revision);
219 * @param string Repository
221 * @param integer Lower revision
222 * @param integer Higher revision
224 * @return array Lines of diff output
226 function diff($repos, $path, $lorev, $hirev)
230 $hirev = $this->rev($hirev);
231 $lorev = $this->rev($lorev);
232 if ($lorev == 'HEAD')
237 if (is_integer($hirev) AND is_integer($lorev))
243 if ($lorev == $hirev)
249 $repospath = $viewsvn->repos
->fetch_path($repos, false);
251 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
259 * @param string Repository
261 * @param integer Lower revision
262 * @param integer Higher revision
264 * @return array Lines of log output
266 function log($repos, $path, $lorev, $hirev)
270 $hirev = $this->rev($hirev);
271 $lorev = $this->rev($hirev);
272 if ($lorev == 'HEAD')
277 if (is_integer($hirev) AND is_integer($lorev))
283 if ($lorev == $hirev)
289 $repospath = $viewsvn->repos
->fetch_path($repos, false);
291 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
295 * SVN Wrapper: ls (list)
299 * @param string Repository
301 * @param integer Revision
303 * @return array Lines of list output
305 function ls($repos, $path, $revision)
307 return $this->std('list', $repos, $path, $revision);
311 * Generates a clean revision number
315 * @param integer Revision number
317 * @return mixed Cleaned revision or HEAD
319 function rev($revision)
321 if (($revision = intval($revision)) < 1)
330 * Commonly executed SVN commands that return data
331 * used in many parts of the system
357 * Constructor: bind with registry
363 $this->registry
=& $viewsvn;
367 * Checks to see if the given universal path is
372 * @param string Universal path
374 * @return bool Directory or not
376 function isdir($path)
378 $output = $this->registry
->svn
->std('info', $this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 'HEAD');
380 foreach ($output AS $line)
382 if (preg_match('#^Node Kind: (.*)#', $line, $matches))
384 if (trim(strtolower($matches[1])) == 'directory')
395 * Get a list of revisions for a path
399 * @param string Universal path
401 * @return array Key revisions
403 function fetch_revs($path)
405 if (!isset($this->revisions
["$path"]))
407 $log = $this->fetch_logs($path);
409 $revs = array_keys($log);
411 $this->revisions["$path"] = array(
413 'START' => $revs[ count($revs) - 1 ],
418 return $this->revisions
["$path"];
422 * Gets the revision that is marked as HEAD
426 * @param string Universal path
428 * @return integer Revision
430 function fetch_head_rev($path)
432 $revs = $this->fetch_revs($path);
433 return $revs['HEAD'];
437 * Returns the previous revision to the one given
441 * @param string Universal path
442 * @param integer Arbitrary revision
444 * @return integer Previous revision (-1 if none)
446 function fetch_prev_rev($path, $current)
448 $revs = $this->fetch_revs($path);
450 if ($current == 'HEAD')
452 $current = $this->fetch_head_rev($path);
455 $index = array_search($current, $revs['revs']);
456 if ($current === false)
458 $message->trigger->error('revision ' . $current . ' is not in ' . $path);
461 if (isset($revs['revs'][ $index + 1 ]))
463 return $revs['revs'][ $index + 1 ];
476 * @param string Universal path
478 * @return array Log data
480 function fetch_logs($path)
482 if (!isset($this->logs["$path"]))
484 $log = new SVNLog($this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 0, 0);
486 $this->logs
["$path"] = $log->fetch();
489 return $this->logs["$path"];
493 * Returns a given log entry for a path
498 * @param string Universal path
499 * @param integer Arbitrary revision
501 * @return array Log entry
503 function fetch_log($path, $rev)
505 $logs = $this->fetch_logs($path);
507 $rev = $this->registry
->svn
->rev($rev);
510 $rev = $this->fetch_head_rev($path);
513 if (isset($logs["$rev"]))
515 return $logs["$rev"];
525 * Annotation/blame system; constructs an array
526 * that is ready for output
534 * Array of blame information
537 var $blame = array();
540 * Raw "svn blame" output
546 * Constructor: create blame and store data
548 * @param string Repository
550 * @param integer Revision
552 function SVNBlame($repos, $path, $revision)
556 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
561 * Returns blame for display
565 * @return array Blame data
573 * Parses the blame data
581 foreach ($this->rawoutput
AS $line)
583 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
585 $this->blame
[] = array(
586 'rev' => $matches[1],
587 'author' => $matches[2],
588 'line' => $matches[3],
589 'lineno' => $lineno++
593 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
595 $this->blame
[] = array(
596 'rev' => $matches[1],
597 'author' => $matches[2],
599 'lineno' => $lineno++
607 * Log management system; creates a complex list
608 * of SVN log information
622 * Raw "svn log" output
628 * Constructor: create log store for the given file
630 * @param string Repository
632 * @param integer Lower revision
633 * @param integer Higher revision
635 function SVNLog($repos, $path, $lorev, $hirev)
639 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
644 * Returns logs for display
648 * @return array Log data
656 * Splits up the raw output into a usable log
664 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
666 $line = $this->rawoutput
["$i"];
668 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
670 if (isset($this->logs["$lastrev"]))
672 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
675 $this->logs
["$matches[1]"] = array(
676 'rev' => $matches[1],
677 'author' => $matches[2],
678 'date' => $matches[3],
679 'timezone' => $matches[4],
680 'lines' => $matches[6],
684 $lastrev = $matches[1];
686 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
688 $this->logs
["$lastrev"]['files'][] = array(
689 'action' => $matches[1],
690 'file' => $matches[2]
695 if (trim($line) != 'Changed paths:')
697 $this->logs["$lastrev"]['message'] .= $line . "\n";
702 if (isset($this->logs
["$lastrev"]))
704 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
709 * Trims the last dash line off a message
713 * @param string Message with annoying-ass line
715 * @return string Clean string
717 function strip_last_line($string)
719 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
724 * Diff system; constructs a diff array that
725 * is ready for output
732 * Array of diff information
738 * Raw "svn diff" output
744 * Constructor: create and store diff data
746 * @param string Repository
748 * @param integer Lower revision
749 * @param integer Higher revision
751 function SVNDiff($repos, $path, $lorev, $hirev)
755 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
760 * Returns diffs for display
764 * @return array Diff data
772 * Processes and prepares diff data
779 $indexcounter = null;
784 foreach ($this->rawoutput
AS $line)
786 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
791 $this->diff
["$index"][ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
792 $lines['old'] = $this->diff["$index"]["$chunk"]['hunk']['old']['line'] - 1;
793 $lines['new'] = $this->diff["$index"]["$chunk"]['hunk']['new']['line'] - 1;
797 if ($indexcounter <= 5 AND $indexcounter !== null)
802 else if ($indexcounter == 5)
804 $indexcounter = null;
808 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
811 $content = $matches[2];
815 $this->diff["$index"]["$chunk"][] = array(
818 'oldlineno' => ++$lines['old'],
819 'newlineno' => ++$lines['new']
822 else if ($act == '+')
824 // potential line delta
827 if ($delta = @$this->fetch_diff_extent($lastcontent, $content))
829 // create two sets of ends for the two contents
830 $delta['endo'] = strlen($lastcontent) - $delta['end'];
831 $delta['endn'] = strlen($content) - $delta['end'];
833 $diffo = $delta['endo'] - $delta['start'];
834 $diffn = $delta['endn'] - $delta['start'];
836 if (strlen($lastcontent) > $delta['endo'] - $diffo)
838 $removed = substr($lastcontent, $delta['start'], $diffo);
839 $this->diff["$index"]["$chunk"][ count($this->diff["$index"]["$chunk"]) - 2 ]['line'] = substr_replace($lastcontent, '{@-' . '-}' . $removed . '{/@-' . '-}', $delta['start'], $diffo);
842 if (strlen($content) > $delta['endn'] - $diffn)
844 $added = substr($content, $delta['start'], $diffn);
845 $content = substr_replace($content, '{@+' . '+}' . $added . '{/@+' . '+}', $delta['start'], $diffn);
850 $this->diff["$index"]["$chunk"][] = array(
854 'newlineno' => ++$lines['new']
857 else if ($act == '-')
859 $lastcontent = $content;
861 $this->diff["$index"]["$chunk"][] = array(
864 'oldlineno' => ++$lines['old'],
874 if (preg_match('#^Index: (.*?)$#', $line, $matches))
876 $index = $matches[1];
884 $this->diff["$index"]["$chunk"][] = array(
887 'oldlineno' => ++$lines['old'],
888 'newlineno' => ++$lines['new']
895 * Returns the amount of change that occured
900 * @param string Old line
901 * @param string New line
903 * @return array Difference of positions
905 function fetch_diff_extent($old, $new)
908 $min = min(strlen($old), strlen($new));
910 for ($start = 0; $start < $min; $start++)
912 if ($old{"$start
"} != $new{"$start
"})
918 $max = max(strlen($old), strlen($new));
920 for ($end = 0; $end < $min; $end++)
922 $oldpos = strlen($old) - $end;
923 $newpos = strlen($new) - $end;
925 if ($old{"$oldpos
"} != $new{"$newpos
"})
933 if ($start == 0 AND $end == $max)
938 return array('start' => $start, 'end' => $end);
942 /*=====================================================================*\
943 || ###################################################################
946 || ###################################################################
947 \*=====================================================================*/