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"];
524 * Prints the file changed list
528 * @public array List of file changes
529 * @public string The repository
530 * @public integer Current revision
532 * @return string Processed HTML
534 function construct_file_changes($changes, $repos, $revision)
540 foreach ($changes AS $file)
542 switch ($file['action'])
549 $class = 'file_delete';
550 $tooltip = 'Deleted';
553 $class = 'file_modify';
554 $tooltip = 'Modified';
557 $class = 'file_replace';
558 $tooltip = 'Replaced';
562 $show['from'] = (bool
)$file['from'];
566 $class = 'file_move';
567 $tooltip = 'Moved/Copied';
568 preg_match('#(.*):([0-9]+)#', $file['from'], $matches);
569 $link['from'] = $viewsvn->paths
->out('view.php' . $viewsvn->paths
->fetch_rev_str(false
, $matches[2]), $repos . $matches[1]);
572 $link['file'] = $viewsvn->paths
->out('view.php' . $viewsvn->paths
->fetch_rev_str(false
, $revision), $file['file']);
574 eval('$files .= "' . $viewsvn->template
->fetch('file_change') . '";');
582 * Annotation/blame system; constructs an array
583 * that is ready for output
591 * Array of blame information
594 var $blame = array();
597 * Raw "svn blame" output
603 * Constructor: create blame and store data
605 * @param string Repository
607 * @param integer Revision
609 function SVNBlame($repos, $path, $revision)
613 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
618 * Returns blame for display
622 * @return array Blame data
630 * Parses the blame data
638 foreach ($this->rawoutput
AS $line)
640 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
642 $this->blame
[] = array(
643 'rev' => $matches[1],
644 'author' => $matches[2],
645 'line' => $matches[3],
646 'lineno' => $lineno++
650 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
652 $this->blame
[] = array(
653 'rev' => $matches[1],
654 'author' => $matches[2],
656 'lineno' => $lineno++
664 * Log management system; creates a complex list
665 * of SVN log information
679 * Raw "svn log" output
685 * Constructor: create log store for the given file
687 * @param string Repository
689 * @param integer Lower revision
690 * @param integer Higher revision
692 function SVNLog($repos, $path, $lorev, $hirev)
696 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
701 * Returns logs for display
705 * @return array Log data
713 * Splits up the raw output into a usable log
721 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
723 $line = $this->rawoutput
["$i"];
725 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
727 if (isset($this->logs
["$lastrev"]))
729 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
732 $this->logs
["$matches[1]"] = array(
733 'rev' => $matches[1],
734 'author' => $matches[2],
735 'date' => $matches[3],
736 'timezone' => $matches[4],
737 'lines' => $matches[6],
741 $lastrev = $matches[1];
743 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
745 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
747 $matches[2] = $amatches[1];
750 $this->logs
["$lastrev"]['files'][] = array(
751 'action' => $matches[1],
752 'file' => trim($matches[2]),
753 'from' => (isset($amatches[2]) ?
$amatches[2] : '')
758 if (trim($line) != 'Changed paths:')
760 $this->logs
["$lastrev"]['message'] .= $line . "\n";
765 if (isset($this->logs
["$lastrev"]))
767 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
772 * Trims the last dash line off a message
776 * @param string Message with annoying-ass line
778 * @return string Clean string
780 function strip_last_line($string)
782 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
787 * Diff system; constructs a diff array that
788 * is ready for output
795 * Array of diff information
801 * Raw "svn diff" output
807 * Constructor: create and store diff data
809 * @param string Repository
811 * @param integer Lower revision
812 * @param integer Higher revision
814 function SVNDiff($repos, $path, $lorev, $hirev)
818 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
823 * Returns diffs for display
827 * @return array Diff data
835 * Processes and prepares diff data
842 $indexcounter = null
;
846 foreach ($this->rawoutput
AS $line)
848 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
851 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
852 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
853 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
857 if ($indexcounter <= 3 AND $indexcounter !== null
)
863 else if ($indexcounter == 3)
865 $indexcounter = null
;
870 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
873 $content = $matches[2];
877 $this->diff
["$index"]["$chunk"][] = array(
880 'oldlineno' => ++
$lines['old'],
881 'newlineno' => ++
$lines['new']
886 else if ($act == '+')
888 // potential line delta
889 if (count($delstack) > 0)
891 $lastline = array_shift($delstack);
893 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
895 // create two sets of ends for the two contents
896 $delta['endo'] = strlen($lastline['line']) - $delta['end'];
897 $delta['endn'] = strlen($content) - $delta['end'];
899 $diffo = $delta['endo'] - $delta['start'];
900 $diffn = $delta['endn'] - $delta['start'];
902 if (strlen($lastline['line']) > $delta['endo'] - $diffo)
904 $removed = substr($lastline['line'], $delta['start'], $diffo);
905 $this->diff
["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr_replace($lastline['line'], '{@-' . '-}' . $removed . '{/@-' . '-}', $delta['start'], $diffo);
908 if (strlen($content) > $delta['endn'] - $diffn)
910 $added = substr($content, $delta['start'], $diffn);
911 $content = substr_replace($content, '{@+' . '+}' . $added . '{/@+' . '+}', $delta['start'], $diffn);
916 $this->diff
["$index"]["$chunk"][] = array(
920 'newlineno' => ++
$lines['new']
923 else if ($act == '-')
925 $this->diff
["$index"]["$chunk"][] = $thearray = array(
928 'oldlineno' => ++
$lines['old'],
932 $key = count($this->diff
["$index"]["$chunk"]) - 2;
933 $thearray['INDEX'] = $key;
935 array_push($delstack, $thearray);
941 if (preg_match('#^Index: (.*?)$#', $line, $matches))
943 $index = $matches[1];
949 $this->diff
["$index"]["$chunk"][] = array(
952 'oldlineno' => ++
$lines['old'],
953 'newlineno' => ++
$lines['new']
962 * Returns the amount of change that occured
967 * @param string Old line
968 * @param string New line
970 * @return array Difference of positions
972 function fetch_diff_extent($old, $new)
975 $min = min(strlen($old), strlen($new));
977 for ($start = 0; $start < $min; $start++
)
979 if ($old{"$start"} != $new{"$start"})
985 $max = max(strlen($old), strlen($new));
987 for ($end = 0; $end < $min; $end++
)
989 $oldpos = strlen($old) - $end;
990 $newpos = strlen($new) - $end;
992 if ($old{"$oldpos"} != $new{"$newpos"})
1000 if ($start == 0 AND $end == $max)
1005 return array('start' => $start, 'end' => $end);
1009 /*=====================================================================*\
1010 || ###################################################################
1013 || ###################################################################
1014 \*=====================================================================*/