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);
151 $temp = implode("\n", $output);
152 if (strpos($temp, '(apr' . '_err=') !== false
)
154 $viewsvn->trigger
->error(nl2br($temp));
160 * SVN Wrapper: standard command system
164 * @param string SVN command
165 * @param string Repository
167 * @param integer Revision
169 * @return array Lines of output
171 function std($command, $repos, $path, $revision)
175 $revision = $this->rev($revision);
176 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
178 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
186 * @param string Repository
188 * @param integer Revision
190 * @return array Lines of blame output
192 function blame($repos, $path, $revision)
194 return $this->std('blame', $repos, $path, $revision);
202 * @param string Repository
204 * @param integer Revision
206 * @return array Lines of cat output
208 function cat($repos, $path, $revision)
210 return $this->std('cat', $repos, $path, $revision);
218 * @param string Repository
220 * @param integer Lower revision
221 * @param integer Higher revision
223 * @return array Lines of diff output
225 function diff($repos, $path, $lorev, $hirev)
229 $hirev = $this->rev($hirev);
230 $lorev = $this->rev($lorev);
231 if ($lorev == 'HEAD')
236 if (is_integer($hirev) AND is_integer($lorev))
242 if ($lorev == $hirev)
248 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
250 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
258 * @param string Repository
260 * @param integer Lower revision
261 * @param integer Higher revision
263 * @return array Lines of log output
265 function log($repos, $path, $lorev, $hirev)
269 $hirev = $this->rev($hirev);
270 $lorev = $this->rev($hirev);
271 if ($lorev == 'HEAD')
276 if (is_integer($hirev) AND is_integer($lorev))
282 if ($lorev == $hirev)
288 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
290 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
294 * SVN Wrapper: ls (list)
298 * @param string Repository
300 * @param integer Revision
302 * @return array Lines of list output
304 function ls($repos, $path, $revision)
306 return $this->std('list', $repos, $path, $revision);
310 * Generates a clean revision number
314 * @param integer Revision number
316 * @return mixed Cleaned revision or HEAD
318 function rev($revision)
320 if (($revision = intval($revision)) < 1)
329 * Commonly executed SVN commands that return data
330 * used in many parts of the system
356 * Constructor: bind with registry
362 $this->registry
=& $viewsvn;
366 * Checks to see if the given universal path is
371 * @param string Universal path
373 * @return bool Directory or not
375 function isdir($path)
377 $output = $this->registry
->svn
->std('info', $this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 'HEAD');
379 foreach ($output AS $line)
381 if (preg_match('#^Node Kind: (.*)#', $line, $matches))
383 if (trim(strtolower($matches[1])) == 'directory')
394 * Get a list of revisions for a path
398 * @param string Universal path
400 * @return array Key revisions
402 function fetch_revs($path)
404 if (!isset($this->revisions
["$path"]))
406 $log = $this->fetch_logs($path);
408 $revs = array_keys($log);
410 $this->revisions
["$path"] = array(
412 'START' => $revs[ count($revs) - 1 ],
417 return $this->revisions
["$path"];
421 * Gets the revision that is marked as HEAD
425 * @param string Universal path
427 * @return integer Revision
429 function fetch_head_rev($path)
431 $revs = $this->fetch_revs($path);
432 return $revs['HEAD'];
436 * Returns the previous revision to the one given
440 * @param string Universal path
441 * @param integer Arbitrary revision
443 * @return integer Previous revision (-1 if none)
445 function fetch_prev_rev($path, $current)
447 $revs = $this->fetch_revs($path);
449 if ($current == 'HEAD')
451 $current = $this->fetch_head_rev($path);
454 $index = array_search($current, $revs['revs']);
455 if ($current === false
)
457 $message->trigger
->error('revision ' . $current . ' is not in ' . $path);
460 if (isset($revs['revs'][ $index +
1 ]))
462 return $revs['revs'][ $index +
1 ];
475 * @param string Universal path
477 * @return array Log data
479 function fetch_logs($path)
481 if (!isset($this->logs
["$path"]))
483 $log = new SVNLog($this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 0, 0);
485 $this->logs
["$path"] = $log->fetch();
488 return $this->logs
["$path"];
492 * Returns a given log entry for a path
497 * @param string Universal path
498 * @param integer Arbitrary revision
500 * @return array Log entry
502 function fetch_log($path, $rev)
504 $logs = $this->fetch_logs($path);
506 $rev = $this->registry
->svn
->rev($rev);
509 $rev = $this->fetch_head_rev($path);
512 if (isset($logs["$rev"]))
514 return $logs["$rev"];
524 * Annotation/blame system; constructs an array
525 * that is ready for output
533 * Array of blame information
536 var $blame = array();
539 * Raw "svn blame" output
545 * Constructor: create blame and store data
547 * @param string Repository
549 * @param integer Revision
551 function SVNBlame($repos, $path, $revision)
555 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
560 * Returns blame for display
564 * @return array Blame data
572 * Parses the blame data
580 foreach ($this->rawoutput
AS $line)
582 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
584 $this->blame
[] = array(
585 'rev' => $matches[1],
586 'author' => $matches[2],
587 'line' => $matches[3],
588 'lineno' => $lineno++
592 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
594 $this->blame
[] = array(
595 'rev' => $matches[1],
596 'author' => $matches[2],
598 'lineno' => $lineno++
606 * Log management system; creates a complex list
607 * of SVN log information
621 * Raw "svn log" output
627 * Constructor: create log store for the given file
629 * @param string Repository
631 * @param integer Lower revision
632 * @param integer Higher revision
634 function SVNLog($repos, $path, $lorev, $hirev)
638 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
643 * Returns logs for display
647 * @return array Log data
655 * Splits up the raw output into a usable log
663 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
665 $line = $this->rawoutput
["$i"];
667 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
669 if (isset($this->logs
["$lastrev"]))
671 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
674 $this->logs
["$matches[1]"] = array(
675 'rev' => $matches[1],
676 'author' => $matches[2],
677 'date' => $matches[3],
678 'timezone' => $matches[4],
679 'lines' => $matches[6],
683 $lastrev = $matches[1];
685 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
687 $this->logs
["$lastrev"]['files'][] = array(
688 'action' => $matches[1],
689 'file' => $matches[2]
694 if (trim($line) != 'Changed paths:')
696 $this->logs
["$lastrev"]['message'] .= $line . "\n";
701 if (isset($this->logs
["$lastrev"]))
703 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
708 * Trims the last dash line off a message
712 * @param string Message with annoying-ass line
714 * @return string Clean string
716 function strip_last_line($string)
718 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
723 * Diff system; constructs a diff array that
724 * is ready for output
731 * Array of diff information
737 * Raw "svn diff" output
743 * Constructor: create and store diff data
745 * @param string Repository
747 * @param integer Lower revision
748 * @param integer Higher revision
750 function SVNDiff($repos, $path, $lorev, $hirev)
754 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
759 * Returns diffs for display
763 * @return array Diff data
771 * Processes and prepares diff data
778 $indexcounter = null
;
783 foreach ($this->rawoutput
AS $line)
785 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
790 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
791 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
792 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
796 if ($indexcounter <= 5 AND $indexcounter !== null
)
801 else if ($indexcounter == 5)
803 $indexcounter = null
;
807 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
810 $content = $matches[2];
814 $this->diff
["$index"]["$chunk"][] = array(
817 'oldlineno' => ++
$lines['old'],
818 'newlineno' => ++
$lines['new']
821 else if ($act == '+')
823 // potential line delta
826 if ($delta = @$this->fetch_diff_extent($lastcontent, $content))
828 // create two sets of ends for the two contents
829 $delta['endo'] = strlen($lastcontent) - $delta['end'];
830 $delta['endn'] = strlen($content) - $delta['end'];
832 $diffo = $delta['endo'] - $delta['start'];
833 $diffn = $delta['endn'] - $delta['start'];
835 if (strlen($lastcontent) > $delta['endo'] - $diffo)
837 $removed = substr($lastcontent, $delta['start'], $diffo);
838 $this->diff
["$index"]["$chunk"][ count($this->diff
["$index"]["$chunk"]) - 2 ]['line'] = substr_replace($lastcontent, '{@-' . '-}' . $removed . '{/@-' . '-}', $delta['start'], $diffo);
841 if (strlen($content) > $delta['endn'] - $diffn)
843 $added = substr($content, $delta['start'], $diffn);
844 $content = substr_replace($content, '{@+' . '+}' . $added . '{/@+' . '+}', $delta['start'], $diffn);
849 $this->diff
["$index"]["$chunk"][] = array(
853 'newlineno' => ++
$lines['new']
856 else if ($act == '-')
858 $lastcontent = $content;
860 $this->diff
["$index"]["$chunk"][] = array(
863 'oldlineno' => ++
$lines['old'],
873 if (preg_match('#^Index: (.*?)$#', $line, $matches))
875 $index = $matches[1];
883 $this->diff
["$index"]["$chunk"][] = array(
886 'oldlineno' => ++
$lines['old'],
887 'newlineno' => ++
$lines['new']
894 * Returns the amount of change that occured
899 * @param string Old line
900 * @param string New line
902 * @return array Difference of positions
904 function fetch_diff_extent($old, $new)
907 $min = min(strlen($old), strlen($new));
909 for ($start = 0; $start < $min; $start++
)
911 if ($old{"$start"} != $new{"$start"})
917 $max = max(strlen($old), strlen($new));
919 for ($end = 0; $end < $min; $end++
)
921 $oldpos = strlen($old) - $end;
922 $newpos = strlen($new) - $end;
924 if ($old{"$oldpos"} != $new{"$newpos"})
932 if ($start == 0 AND $end == $max)
937 return array('start' => $start, 'end' => $end);
941 /*=====================================================================*\
942 || ###################################################################
945 || ###################################################################
946 \*=====================================================================*/