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 $output = $this->registry
->shell
->exec($this->registry
->svn
->svnpath
. ' info ' . $this->registry
->repos
->fetch_path($this->registry
->paths
->fetch_repos($path), false
) . $this->registry
->paths
->fetch_path($path));
434 foreach ($output AS $line)
436 if (preg_match('#^Last Changed Rev: (.*)#', $line, $matches))
442 $revs = $this->fetch_revs($path);
443 return $revs['HEAD'];
447 * Returns the previous revision to the one given
451 * @param string Universal path
452 * @param integer Arbitrary revision
454 * @return integer Previous revision (-1 if none)
456 function fetch_prev_rev($path, $current)
460 $revs = $this->fetch_revs($path);
462 if ($current == 'HEAD')
464 $current = $this->fetch_head_rev($path);
467 $index = array_search($current, $revs['revs']);
468 if ($current === false
)
470 $message->trigger
->error(sprintf($viewsvn->lang
->string('Revision r%1$s is not in path %2$s'), $current, $path));
473 if (isset($revs['revs'][ $index +
1 ]))
475 return $revs['revs'][ $index +
1 ];
488 * @param string Universal path
489 * @param bool Override the cache system?
491 * @return array Log data
493 function fetch_logs($path)
495 if (!isset($this->logs
["$path"]))
497 $log = new SVNLog($this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 0, $this->registry
->paths
->fetch_rev_num());
499 $this->logs
["$path"] = $log->fetch();
502 return $this->logs
["$path"];
506 * Returns a given log entry for a path
511 * @param string Universal path
512 * @param integer Arbitrary revision
514 * @return array Log entry
516 function fetch_log($path, $rev)
518 $logs = $this->fetch_logs($path);
520 $rev = $this->registry
->svn
->rev($rev);
523 $rev = $this->fetch_head_rev($path);
526 if (isset($logs["$rev"]))
528 return $logs["$rev"];
532 $keys = array_keys($logs);
535 for ($i = 0; $i < count($keys); $i++
)
537 if ($rev > $keys["$i"] AND $rev < $keys[ $i +
1 ])
539 return $logs["$keys[$i]"];
548 * Prints the file changed list
552 * @public array List of file changes
553 * @public string The repository
554 * @public integer Current revision
556 * @return string Processed HTML
558 function construct_file_changes($changes, $repos, $revision)
564 foreach ($changes AS $file)
566 switch ($file['action'])
570 $tooltip = $viewsvn->lang
->string('Added');
573 $class = 'file_delete';
574 $tooltip = $viewsvn->lang
->string('Deleted');
577 $class = 'file_modify';
578 $tooltip = $viewsvn->lang
->string('Modified');
581 $class = 'file_replace';
582 $tooltip = $viewsvn->lang
->string('Replaced');
586 $show['from'] = (bool
)$file['from'];
590 $class = 'file_move';
591 $tooltip = 'Moved/Copied';
592 preg_match('#(.*):([0-9]+)#', $file['from'], $matches);
593 $link['from'] = $viewsvn->paths
->out('view.php' . $viewsvn->paths
->fetch_rev_str(false
, $matches[2]), $repos . $matches[1]);
596 $link['file'] = $viewsvn->paths
->out('view.php' . $viewsvn->paths
->fetch_rev_str(false
, $revision), $repos . $file['file']);
598 eval('$files .= "' . $viewsvn->template
->fetch('file_change') . '";');
606 * Annotation/blame system; constructs an array
607 * that is ready for output
615 * Array of blame information
618 var $blame = array();
621 * Raw "svn blame" output
627 * Constructor: create blame and store data
629 * @param string Repository
631 * @param integer Revision
633 function SVNBlame($repos, $path, $revision)
637 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
642 * Returns blame for display
646 * @return array Blame data
654 * Parses the blame data
662 foreach ($this->rawoutput
AS $line)
664 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
666 $this->blame
[] = array(
667 'rev' => $matches[1],
668 'author' => $matches[2],
669 'line' => $matches[3],
670 'lineno' => $lineno++
674 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
676 $this->blame
[] = array(
677 'rev' => $matches[1],
678 'author' => $matches[2],
680 'lineno' => $lineno++
688 * Log management system; creates a complex list
689 * of SVN log information
703 * Raw "svn log" output
709 * Constructor: create log store for the given file
711 * @param string Repository
713 * @param integer Lower revision
714 * @param integer Higher revision
716 function SVNLog($repos, $path, $lorev, $hirev)
720 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
725 * Returns logs for display
729 * @return array Log data
737 * Splits up the raw output into a usable log
745 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
747 $line = $this->rawoutput
["$i"];
749 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
751 if (isset($this->logs
["$lastrev"]))
753 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
756 $this->logs
["$matches[1]"] = array(
757 'rev' => $matches[1],
758 'author' => $matches[2],
759 'date' => $matches[3],
760 'timezone' => $matches[4],
761 'lines' => $matches[6],
765 $lastrev = $matches[1];
767 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
769 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
771 $matches[2] = $amatches[1];
774 $this->logs
["$lastrev"]['files'][] = array(
775 'action' => $matches[1],
776 'file' => trim($matches[2]),
777 'from' => (isset($amatches[2]) ?
$amatches[2] : '')
782 if (trim($line) != 'Changed paths:')
784 $this->logs
["$lastrev"]['message'] .= $line . "\n";
789 if (isset($this->logs
["$lastrev"]))
791 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
796 * Trims the last dash line off a message
800 * @param string Message with annoying-ass line
802 * @return string Clean string
804 function strip_last_line($string)
806 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
811 * Diff system; constructs a diff array that
812 * is ready for output
819 * Array of diff information
825 * Raw "svn diff" output
831 * Constructor: create and store diff data
833 * @param string Repository
835 * @param integer Lower revision
836 * @param integer Higher revision
838 function SVNDiff($repos, $path, $lorev, $hirev)
842 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
847 * Returns diffs for display
851 * @return array Diff data
859 * Processes and prepares diff data
868 $indexcounter = null
;
873 foreach ($this->rawoutput
AS $line)
875 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
879 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
880 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
881 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
884 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
888 $this->diff
["$index"]['props'] = array();
892 if ($indexcounter <= 3 AND $indexcounter !== null
)
897 else if ($indexcounter == 3)
899 $indexcounter = null
;
903 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
906 $content = $matches[2];
910 $this->diff
["$index"]["$chunk"][] = array(
913 'oldlineno' => ++
$lines['old'],
914 'newlineno' => ++
$lines['new']
919 else if ($act == '+')
921 // potential line delta
922 if (count($delstack) > 0)
924 $lastline = array_shift($delstack);
926 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
928 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
930 $end = strlen($lastline['line']) +
$delta['end'];
931 $viewsvn->debug("RM delta- = " . $end);
932 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
933 $this->diff
["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
936 if (strlen($content) > $delta['start'] - $delta['end'])
938 $end = strlen($content) +
$delta['end'];
939 $viewsvn->debug("MK delta+ = " . $end);
940 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
941 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
946 $this->diff
["$index"]["$chunk"][] = array(
950 'newlineno' => ++
$lines['new']
953 else if ($act == '-')
955 $this->diff
["$index"]["$chunk"][] = $thearray = array(
958 'oldlineno' => ++
$lines['old'],
962 $key = count($this->diff
["$index"]["$chunk"]) - 2;
963 $thearray['INDEX'] = $key;
965 array_push($delstack, $thearray);
971 if (preg_match('#^Index: (.*?)$#', $line, $matches))
973 $index = $matches[1];
981 if (preg_match('#^__*_$#', trim($line)))
983 $viewsvn->debug("skipping: $line");
987 if (preg_match('#Name: (.*?)$#', $line, $matches))
989 $curprop = $matches[1];
990 $viewsvn->debug("prop: $curprop");
995 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
997 $this->diff
["$index"]['props']["$curprop"] .= $matches[1];
999 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
1001 $this->diff
["$index"]['props']["$curprop"] .= "\n" . $line;
1007 $this->diff
["$index"]["$chunk"][] = array(
1010 'oldlineno' => ++
$lines['old'],
1011 'newlineno' => ++
$lines['new']
1014 $delstack = array();
1020 * Returns the amount of change that occured
1025 * @param string Old line
1026 * @param string New line
1028 * @return array Difference of positions
1030 function fetch_diff_extent($old, $new)
1035 $min = min(strlen($old), strlen($new));
1037 $viewsvn->debug("min1 = $min");
1039 while ($start < $min AND $old{"$start"} == $new{"$start"})
1045 $min = $min - $start;
1047 $viewsvn->debug("min2 = $min");
1049 $viewsvn->debug("checking: " . $old{ strlen($old) +
$end } . ' == ' . $new{ strlen($new) +
$end });
1051 while (-$end <= $min AND $old{ strlen($old) +
$end } == $new{ strlen($new) +
$end })
1056 return array('start' => $start, 'end' => $end +
1);
1060 /*=====================================================================*\
1061 || ###################################################################
1064 || ###################################################################
1065 \*=====================================================================*/