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($viewsvn->lang
->string('The SVN binary could not be located'));
69 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
71 $viewsvn->trigger
->error($viewsvn->lang
->string('The SVN binary does not appear to be valid (it failed our tests)'));
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)
450 $revs = $this->fetch_revs($path);
452 if ($current == 'HEAD')
454 $current = $this->fetch_head_rev($path);
457 $index = array_search($current, $revs['revs']);
458 if ($current === false
)
460 $message->trigger
->error(sprintf($viewsvn->lang
->string('Revision r%1$s is not in path %2$s'), $current, $path));
463 if (isset($revs['revs'][ $index +
1 ]))
465 return $revs['revs'][ $index +
1 ];
478 * @param string Universal path
480 * @return array Log data
482 function fetch_logs($path)
484 if (!isset($this->logs
["$path"]))
486 $log = new SVNLog($this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 0, $this->registry
->paths
->fetch_rev_num());
488 $this->logs
["$path"] = $log->fetch();
491 return $this->logs
["$path"];
495 * Returns a given log entry for a path
500 * @param string Universal path
501 * @param integer Arbitrary revision
503 * @return array Log entry
505 function fetch_log($path, $rev)
507 $logs = $this->fetch_logs($path);
509 $rev = $this->registry
->svn
->rev($rev);
512 $rev = $this->fetch_head_rev($path);
515 if (isset($logs["$rev"]))
517 return $logs["$rev"];
521 $keys = array_keys($logs);
524 for ($i = 0; $i < count($keys); $i++
)
526 if ($rev > $keys["$i"] AND $rev < $keys[ $i +
1 ])
528 return $logs["$keys[$i]"];
537 * Prints the file changed list
541 * @public array List of file changes
542 * @public string The repository
543 * @public integer Current revision
545 * @return string Processed HTML
547 function construct_file_changes($changes, $repos, $revision)
553 foreach ($changes AS $file)
555 switch ($file['action'])
559 $tooltip = $viewsvn->lang
->string('Added');
562 $class = 'file_delete';
563 $tooltip = $viewsvn->lang
->string('Deleted');
566 $class = 'file_modify';
567 $tooltip = $viewsvn->lang
->string('Modified');
570 $class = 'file_replace';
571 $tooltip = $viewsvn->lang
->string('Replaced');
575 $show['from'] = (bool
)$file['from'];
579 $class = 'file_move';
580 $tooltip = 'Moved/Copied';
581 preg_match('#(.*):([0-9]+)#', $file['from'], $matches);
582 $link['from'] = $viewsvn->paths
->out('view.php' . $viewsvn->paths
->fetch_rev_str(false
, $matches[2]), $repos . $matches[1]);
585 $link['file'] = $viewsvn->paths
->out('view.php' . $viewsvn->paths
->fetch_rev_str(false
, $revision), $repos . $file['file']);
587 eval('$files .= "' . $viewsvn->template
->fetch('file_change') . '";');
595 * Annotation/blame system; constructs an array
596 * that is ready for output
604 * Array of blame information
607 var $blame = array();
610 * Raw "svn blame" output
616 * Constructor: create blame and store data
618 * @param string Repository
620 * @param integer Revision
622 function SVNBlame($repos, $path, $revision)
626 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
631 * Returns blame for display
635 * @return array Blame data
643 * Parses the blame data
651 foreach ($this->rawoutput
AS $line)
653 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
655 $this->blame
[] = array(
656 'rev' => $matches[1],
657 'author' => $matches[2],
658 'line' => $matches[3],
659 'lineno' => $lineno++
663 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
665 $this->blame
[] = array(
666 'rev' => $matches[1],
667 'author' => $matches[2],
669 'lineno' => $lineno++
677 * Log management system; creates a complex list
678 * of SVN log information
692 * Raw "svn log" output
698 * Constructor: create log store for the given file
700 * @param string Repository
702 * @param integer Lower revision
703 * @param integer Higher revision
705 function SVNLog($repos, $path, $lorev, $hirev)
709 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
714 * Returns logs for display
718 * @return array Log data
726 * Splits up the raw output into a usable log
734 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
736 $line = $this->rawoutput
["$i"];
738 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
740 if (isset($this->logs
["$lastrev"]))
742 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
745 $this->logs
["$matches[1]"] = array(
746 'rev' => $matches[1],
747 'author' => $matches[2],
748 'date' => $matches[3],
749 'timezone' => $matches[4],
750 'lines' => $matches[6],
754 $lastrev = $matches[1];
756 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
758 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
760 $matches[2] = $amatches[1];
763 $this->logs
["$lastrev"]['files'][] = array(
764 'action' => $matches[1],
765 'file' => trim($matches[2]),
766 'from' => (isset($amatches[2]) ?
$amatches[2] : '')
771 if (trim($line) != 'Changed paths:')
773 $this->logs
["$lastrev"]['message'] .= $line . "\n";
778 if (isset($this->logs
["$lastrev"]))
780 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
785 * Trims the last dash line off a message
789 * @param string Message with annoying-ass line
791 * @return string Clean string
793 function strip_last_line($string)
795 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
800 * Diff system; constructs a diff array that
801 * is ready for output
808 * Array of diff information
814 * Raw "svn diff" output
820 * Constructor: create and store diff data
822 * @param string Repository
824 * @param integer Lower revision
825 * @param integer Higher revision
827 function SVNDiff($repos, $path, $lorev, $hirev)
831 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
836 * Returns diffs for display
840 * @return array Diff data
848 * Processes and prepares diff data
855 $indexcounter = null
;
859 foreach ($this->rawoutput
AS $line)
861 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
864 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
865 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
866 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
870 if ($indexcounter <= 3 AND $indexcounter !== null
)
876 else if ($indexcounter == 3)
878 $indexcounter = null
;
883 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
886 $content = $matches[2];
890 $this->diff
["$index"]["$chunk"][] = array(
893 'oldlineno' => ++
$lines['old'],
894 'newlineno' => ++
$lines['new']
899 else if ($act == '+')
901 // potential line delta
902 if (count($delstack) > 0)
904 $lastline = array_shift($delstack);
906 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
908 // create two sets of ends for the two contents
909 $delta['endo'] = strlen($lastline['line']) - $delta['end'];
910 $delta['endn'] = strlen($content) - $delta['end'];
912 $diffo = $delta['endo'] - $delta['start'];
913 $diffn = $delta['endn'] - $delta['start'];
915 if (strlen($lastline['line']) > $delta['endo'] - $diffo)
917 $removed = substr($lastline['line'], $delta['start'], $diffo);
918 $this->diff
["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr_replace($lastline['line'], '{@-' . '-}' . $removed . '{/@-' . '-}', $delta['start'], $diffo);
921 if (strlen($content) > $delta['endn'] - $diffn)
923 $added = substr($content, $delta['start'], $diffn);
924 $content = substr_replace($content, '{@+' . '+}' . $added . '{/@+' . '+}', $delta['start'], $diffn);
929 $this->diff
["$index"]["$chunk"][] = array(
933 'newlineno' => ++
$lines['new']
936 else if ($act == '-')
938 $this->diff
["$index"]["$chunk"][] = $thearray = array(
941 'oldlineno' => ++
$lines['old'],
945 $key = count($this->diff
["$index"]["$chunk"]) - 2;
946 $thearray['INDEX'] = $key;
948 array_push($delstack, $thearray);
954 if (preg_match('#^Index: (.*?)$#', $line, $matches))
956 $index = $matches[1];
962 $this->diff
["$index"]["$chunk"][] = array(
965 'oldlineno' => ++
$lines['old'],
966 'newlineno' => ++
$lines['new']
975 * Returns the amount of change that occured
980 * @param string Old line
981 * @param string New line
983 * @return array Difference of positions
985 function fetch_diff_extent($old, $new)
988 $min = min(strlen($old), strlen($new));
990 for ($start = 0; $start < $min; $start++
)
992 if ($old{"$start"} != $new{"$start"})
998 $max = max(strlen($old), strlen($new));
1000 for ($end = 0; $end < $min; $end++
)
1002 $oldpos = strlen($old) - $end;
1003 $newpos = strlen($new) - $end;
1005 if ($old{"$oldpos"} != $new{"$newpos"})
1013 if ($start == 0 AND $end == $max)
1018 return array('start' => $start, 'end' => $end);
1022 /*=====================================================================*\
1023 || ###################################################################
1026 || ###################################################################
1027 \*=====================================================================*/