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');
146 $temp = implode("\n", $output);
147 if (strpos($temp, '(apr' . '_err=') !== false
)
149 $viewsvn->trigger
->error(nl2br($temp));
155 * SVN Wrapper: standard command system
159 * @param string SVN command
160 * @param string Repository
162 * @param integer Revision
164 * @return array Lines of output
166 function std($command, $repos, $path, $revision)
170 $revision = $this->rev($revision);
171 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
173 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
181 * @param string Repository
183 * @param integer Revision
185 * @return array Lines of blame output
187 function blame($repos, $path, $revision)
189 return $this->std('blame', $repos, $path, $revision);
197 * @param string Repository
199 * @param integer Revision
201 * @return array Lines of cat output
203 function cat($repos, $path, $revision)
205 return $this->std('cat', $repos, $path, $revision);
213 * @param string Repository
215 * @param integer Lower revision
216 * @param integer Higher revision
218 * @return array Lines of diff output
220 function diff($repos, $path, $lorev, $hirev)
224 $hirev = $this->rev($hirev);
225 $lorev = $this->rev($lorev);
226 if ($lorev == 'HEAD')
231 if (is_integer($hirev) AND is_integer($lorev))
237 if ($lorev == $hirev)
243 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
245 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
253 * @param string Repository
255 * @param integer Lower revision
256 * @param integer Higher revision
258 * @return array Lines of log output
260 function log($repos, $path, $lorev, $hirev)
264 $hirev = $this->rev($hirev);
265 $lorev = $this->rev($hirev);
266 if ($lorev == 'HEAD')
271 if (is_integer($hirev) AND is_integer($lorev))
277 if ($lorev == $hirev)
283 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
285 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
289 * SVN Wrapper: ls (list)
293 * @param string Repository
295 * @param integer Revision
297 * @return array Lines of list output
299 function ls($repos, $path, $revision)
301 return $this->std('list', $repos, $path, $revision);
305 * Generates a clean revision number
309 * @param integer Revision number
311 * @return mixed Cleaned revision or HEAD
313 function rev($revision)
315 if (($revision = intval($revision)) < 1)
324 * Commonly executed SVN commands that return data
325 * used in many parts of the system
351 * Constructor: bind with registry
357 $this->registry
=& $viewsvn;
361 * Checks to see if the given universal path is
366 * @param string Universal path
368 * @return bool Directory or not
370 function isdir($path)
372 $output = $this->registry
->svn
->std('info', $this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 'HEAD');
374 foreach ($output AS $line)
376 if (preg_match('#^Node Kind: (.*)#', $line, $matches))
378 if (trim(strtolower($matches[1])) == 'directory')
389 * Get a list of revisions for a path
393 * @param string Universal path
395 * @return array Key revisions
397 function fetch_revs($path)
399 if (!isset($this->revisions
["$path"]))
401 $log = $this->fetch_logs($path);
403 $revs = array_keys($log);
405 $this->revisions
["$path"] = array(
407 'START' => $revs[ count($revs) - 1 ],
412 return $this->revisions
["$path"];
416 * Gets the revision that is marked as HEAD
420 * @param string Universal path
422 * @return integer Revision
424 function fetch_head_rev($path)
426 $revs = $this->fetch_revs($path);
427 return $revs['HEAD'];
431 * Returns the previous revision to the one given
435 * @param string Universal path
436 * @param integer Arbitrary revision
438 * @return integer Previous revision (-1 if none)
440 function fetch_prev_rev($path, $current)
442 $revs = $this->fetch_revs($path);
444 if ($current == 'HEAD')
446 $current = $this->fetch_head_rev($path);
449 $index = array_search($current, $revs['revs']);
450 if ($current === false
)
452 $message->trigger
->error('revision ' . $current . ' is not in ' . $path);
455 if (isset($revs['revs'][ $index +
1 ]))
457 return $revs['revs'][ $index +
1 ];
470 * @param string Universal path
472 * @return array Log data
474 function fetch_logs($path)
476 if (!isset($this->logs
["$path"]))
478 $log = new SVNLog($this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 0, 0);
480 $this->logs
["$path"] = $log->fetch();
483 return $this->logs
["$path"];
487 * Returns a given log entry for a path
492 * @param string Universal path
493 * @param integer Arbitrary revision
495 * @return array Log entry
497 function fetch_log($path, $rev)
499 $logs = $this->fetch_logs($path);
501 $rev = $this->registry
->svn
->rev($rev);
504 $rev = $this->fetch_head_rev($path);
507 if (isset($logs["$rev"]))
509 return $logs["$rev"];
519 * Annotation/blame system; constructs an array
520 * that is ready for output
528 * Array of blame information
531 var $blame = array();
534 * Raw "svn blame" output
540 * Constructor: create blame and store data
542 * @param string Repository
544 * @param integer Revision
546 function SVNBlame($repos, $path, $revision)
550 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
555 * Returns blame for display
559 * @return array Blame data
567 * Parses the blame data
575 foreach ($this->rawoutput
AS $line)
577 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
579 $this->blame
[] = array(
580 'rev' => $matches[1],
581 'author' => $matches[2],
582 'line' => $matches[3],
583 'lineno' => $lineno++
587 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
589 $this->blame
[] = array(
590 'rev' => $matches[1],
591 'author' => $matches[2],
593 'lineno' => $lineno++
601 * Log management system; creates a complex list
602 * of SVN log information
616 * Raw "svn log" output
622 * Constructor: create log store for the given file
624 * @param string Repository
626 * @param integer Lower revision
627 * @param integer Higher revision
629 function SVNLog($repos, $path, $lorev, $hirev)
633 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
638 * Returns logs for display
642 * @return array Log data
650 * Splits up the raw output into a usable log
658 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
660 $line = $this->rawoutput
["$i"];
662 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
664 if (isset($this->logs
["$lastrev"]))
666 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
669 $this->logs
["$matches[1]"] = array(
670 'rev' => $matches[1],
671 'author' => $matches[2],
672 'date' => $matches[3],
673 'timezone' => $matches[4],
674 'lines' => $matches[6],
678 $lastrev = $matches[1];
680 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
682 $this->logs
["$lastrev"]['files'][] = array(
683 'action' => $matches[1],
684 'file' => $matches[2]
689 if (trim($line) != 'Changed paths:')
691 $this->logs
["$lastrev"]['message'] .= $line . "\n";
696 if (isset($this->logs
["$lastrev"]))
698 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
703 * Trims the last dash line off a message
707 * @param string Message with annoying-ass line
709 * @return string Clean string
711 function strip_last_line($string)
713 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
718 * Diff system; constructs a diff array that
719 * is ready for output
726 * Array of diff information
732 * Raw "svn diff" output
738 * Constructor: create and store diff data
740 * @param string Repository
742 * @param integer Lower revision
743 * @param integer Higher revision
745 function SVNDiff($repos, $path, $lorev, $hirev)
749 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
754 * Returns diffs for display
758 * @return array Diff data
766 * Processes and prepares diff data
773 $indexcounter = null
;
778 foreach ($this->rawoutput
AS $line)
780 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
785 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
786 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
787 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
791 if ($indexcounter <= 5 AND $indexcounter !== null
)
796 else if ($indexcounter == 5)
798 $indexcounter = null
;
802 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
805 $content = $matches[2];
809 $this->diff
["$index"]["$chunk"][] = array(
812 'oldlineno' => ++
$lines['old'],
813 'newlineno' => ++
$lines['new']
816 else if ($act == '+')
818 // potential line delta
821 if ($delta = @$this->fetch_diff_extent($lastcontent, $content))
823 // create two sets of ends for the two contents
824 $delta['endo'] = strlen($lastcontent) - $delta['end'];
825 $delta['endn'] = strlen($content) - $delta['end'];
827 $diffo = $delta['endo'] - $delta['start'];
828 $diffn = $delta['endn'] - $delta['start'];
830 if (strlen($lastcontent) > $delta['endo'] - $diffo)
832 $removed = substr($lastcontent, $delta['start'], $diffo);
833 $this->diff
["$index"]["$chunk"][ count($this->diff
["$index"]["$chunk"]) - 2 ]['line'] = substr_replace($lastcontent, '{@-' . '-}' . $removed . '{/@-' . '-}', $delta['start'], $diffo);
836 if (strlen($content) > $delta['endn'] - $diffn)
838 $added = substr($content, $delta['start'], $diffn);
839 $content = substr_replace($content, '{@+' . '+}' . $added . '{/@+' . '+}', $delta['start'], $diffn);
844 $this->diff
["$index"]["$chunk"][] = array(
848 'newlineno' => ++
$lines['new']
851 else if ($act == '-')
853 $lastcontent = $content;
855 $this->diff
["$index"]["$chunk"][] = array(
858 'oldlineno' => ++
$lines['old'],
868 if (preg_match('#^Index: (.*?)$#', $line, $matches))
870 $index = $matches[1];
878 $this->diff
["$index"]["$chunk"][] = array(
881 'oldlineno' => ++
$lines['old'],
882 'newlineno' => ++
$lines['new']
889 * Returns the amount of change that occured
894 * @param string Old line
895 * @param string New line
897 * @return array Difference of positions
899 function fetch_diff_extent($old, $new)
902 $min = min(strlen($old), strlen($new));
904 for ($start = 0; $start < $min; $start++
)
906 if ($old{"$start"} != $new{"$start"})
912 $max = max(strlen($old), strlen($new));
914 for ($end = 0; $end < $min; $end++
)
916 $oldpos = strlen($old) - $end;
917 $newpos = strlen($new) - $end;
919 if ($old{"$oldpos"} != $new{"$newpos"})
927 if ($start == 0 AND $end == $max)
932 return array('start' => $start, 'end' => $end);
936 /*=====================================================================*\
937 || ###################################################################
940 || ###################################################################
941 \*=====================================================================*/