]>
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 . ' ' . $repospath . $path . '@' . $revision);
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), $this->registry
->paths
->fetch_rev_num());
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"];
519 $keys = array_keys($logs);
522 for ($i = 0; $i < count($keys); $i++
)
524 if ($rev > $keys["$i"] AND $rev < $keys[ $i + 1 ])
526 return $logs["$keys[$i]"];
535 * Prints the file changed list
539 * @public array List of file changes
540 * @public string The repository
541 * @public integer Current revision
543 * @return string Processed HTML
545 function construct_file_changes($changes, $repos, $revision)
551 foreach ($changes AS $file)
553 switch ($file['action'])
560 $class = 'file_delete';
561 $tooltip = 'Deleted';
564 $class = 'file_modify';
565 $tooltip = 'Modified';
568 $class = 'file_replace';
569 $tooltip = 'Replaced';
573 $show['from'] = (bool)$file['from'];
577 $class = 'file_move';
578 $tooltip = 'Moved/Copied';
579 preg_match('#(.*):([0-9]+)#', $file['from'], $matches);
580 $link['from'] = $viewsvn->paths->out('view.php' . $viewsvn->paths->fetch_rev_str(false, $matches[2]), $repos . $matches[1]);
583 $link['file'] = $viewsvn->paths->out('view.php' . $viewsvn->paths->fetch_rev_str(false, $revision), $repos . $file['file']);
585 eval('$files .= "' . $viewsvn->template->fetch('file_change
') . '";');
593 * Annotation/blame system; constructs an array
594 * that is ready for output
602 * Array of blame information
605 var $blame = array();
608 * Raw "svn blame
" output
614 * Constructor: create blame and store data
616 * @param string Repository
618 * @param integer Revision
620 function SVNBlame($repos, $path, $revision)
624 $this->rawoutput = $viewsvn->svn->blame($repos, $path, $revision);
629 * Returns blame for display
633 * @return array Blame data
641 * Parses the blame data
649 foreach ($this->rawoutput AS $line)
651 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
653 $this->blame[] = array(
654 'rev' => $matches[1],
655 'author' => $matches[2],
656 'line' => $matches[3],
657 'lineno' => $lineno++
661 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
663 $this->blame[] = array(
664 'rev' => $matches[1],
665 'author' => $matches[2],
667 'lineno' => $lineno++
675 * Log management system; creates a complex list
676 * of SVN log information
690 * Raw "svn log
" output
696 * Constructor: create log store for the given file
698 * @param string Repository
700 * @param integer Lower revision
701 * @param integer Higher revision
703 function SVNLog($repos, $path, $lorev, $hirev)
707 $this->rawoutput = $viewsvn->svn->log($repos, $path, $lorev, $hirev);
712 * Returns logs for display
716 * @return array Log data
724 * Splits up the raw output into a usable log
732 for ($i = 1; $i <= count($this->rawoutput) - 1; $i++)
734 $line = $this->rawoutput["$i"];
736 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
738 if (isset($this->logs
["$lastrev"]))
740 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
743 $this->logs["$matches[1]"] = array(
744 'rev' => $matches[1],
745 'author' => $matches[2],
746 'date' => $matches[3],
747 'timezone' => $matches[4],
748 'lines' => $matches[6],
752 $lastrev = $matches[1];
754 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
756 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
758 $matches[2] = $amatches[1];
761 $this->logs["$lastrev"]['files'][] = array(
762 'action' => $matches[1],
763 'file' => trim($matches[2]),
764 'from' => (isset($amatches[2]) ? $amatches[2] : '')
769 if (trim($line) != 'Changed paths:')
771 $this->logs
["$lastrev"]['message'] .= $line . "\n
";
776 if (isset($this->logs["$lastrev"]))
778 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
783 * Trims the last dash line off a message
787 * @param string Message with annoying-ass line
789 * @return string Clean string
791 function strip_last_line($string)
793 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
798 * Diff system; constructs a diff array that
799 * is ready for output
806 * Array of diff information
812 * Raw "svn diff" output
818 * Constructor: create and store diff data
820 * @param string Repository
822 * @param integer Lower revision
823 * @param integer Higher revision
825 function SVNDiff($repos, $path, $lorev, $hirev)
829 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
834 * Returns diffs for display
838 * @return array Diff data
846 * Processes and prepares diff data
853 $indexcounter = null;
857 foreach ($this->rawoutput
AS $line)
859 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
862 $this->diff
["$index"][ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
863 $lines['old'] = $this->diff["$index"]["$chunk"]['hunk']['old']['line'] - 1;
864 $lines['new'] = $this->diff["$index"]["$chunk"]['hunk']['new']['line'] - 1;
868 if ($indexcounter <= 3 AND $indexcounter !== null)
874 else if ($indexcounter == 3)
876 $indexcounter = null;
881 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
884 $content = $matches[2];
888 $this->diff["$index"]["$chunk"][] = array(
891 'oldlineno' => ++$lines['old'],
892 'newlineno' => ++$lines['new']
897 else if ($act == '+')
899 // potential line delta
900 if (count($delstack) > 0)
902 $lastline = array_shift($delstack);
904 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
906 // create two sets of ends for the two contents
907 $delta['endo'] = strlen($lastline['line']) - $delta['end'];
908 $delta['endn'] = strlen($content) - $delta['end'];
910 $diffo = $delta['endo'] - $delta['start'];
911 $diffn = $delta['endn'] - $delta['start'];
913 if (strlen($lastline['line']) > $delta['endo'] - $diffo)
915 $removed = substr($lastline['line'], $delta['start'], $diffo);
916 $this->diff["$index"]["$chunk"]["$lastline[INDEX
]"]['line'] = substr_replace($lastline['line'], '{@-' . '-}' . $removed . '{/@-' . '-}', $delta['start'], $diffo);
919 if (strlen($content) > $delta['endn'] - $diffn)
921 $added = substr($content, $delta['start'], $diffn);
922 $content = substr_replace($content, '{@+' . '+}' . $added . '{/@+' . '+}', $delta['start'], $diffn);
927 $this->diff["$index"]["$chunk"][] = array(
931 'newlineno' => ++$lines['new']
934 else if ($act == '-')
936 $this->diff["$index"]["$chunk"][] = $thearray = array(
939 'oldlineno' => ++$lines['old'],
943 $key = count($this->diff["$index"]["$chunk"]) - 2;
944 $thearray['INDEX'] = $key;
946 array_push($delstack, $thearray);
952 if (preg_match('#^Index: (.*?)$#', $line, $matches))
954 $index = $matches[1];
960 $this->diff["$index"]["$chunk"][] = array(
963 'oldlineno' => ++$lines['old'],
964 'newlineno' => ++$lines['new']
973 * Returns the amount of change that occured
978 * @param string Old line
979 * @param string New line
981 * @return array Difference of positions
983 function fetch_diff_extent($old, $new)
986 $min = min(strlen($old), strlen($new));
988 for ($start = 0; $start < $min; $start++)
990 if ($old{"$start
"} != $new{"$start
"})
996 $max = max(strlen($old), strlen($new));
998 for ($end = 0; $end < $min; $end++)
1000 $oldpos = strlen($old) - $end;
1001 $newpos = strlen($new) - $end;
1003 if ($old{"$oldpos
"} != $new{"$newpos
"})
1011 if ($start == 0 AND $end == $max)
1016 return array('start' => $start, 'end' => $end);
1020 /*=====================================================================*\
1021 || ###################################################################
1024 || ###################################################################
1025 \*=====================================================================*/