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 * Formats a SVN log message
118 * @param string Unformatted log message
120 * @return string Output-ready log message
122 function format_log_message($message)
126 $message = $viewsvn->entity_encode($message);
128 $message = preg_replace('#r([0-9]+)#e', '"<a href=\"" . $viewsvn->path . "/" . $viewsvn->paths->out("diff.php" . $viewsvn->paths->fetch_rev_str(true, "\1", 0), $viewsvn->paths->fetch_repos($viewsvn->paths->parse()) . "/") . "\">r\1</a>"', $message);
131 $lines = explode("\n", $message);
133 foreach ($lines AS $line)
135 if (preg_match('#^\s*?(\*|\-)\s?(.*)#', $line, $matches))
139 $message .= '<li>' . $matches[2] . '</li>';
143 $message .= '<ul class="list">';
144 $message .= '<li>' . $matches[2] . '</li>';
158 $message .= '<br />';
171 $message = preg_replace('#(<br />)*$#', '', $message);
177 * Counts the spaces and replaces two or more ones
181 * @param string Spaced string
183 * @return string 'd string
185 function format_spaces($thestring)
187 if (strlen($thestring) >= 2)
189 $thestring = str_replace(' ', ' ', $thestring);
196 * Executes the SVN binary
200 * @param string Command
202 * @return array Output
204 function svn($command)
208 $output = $viewsvn->shell
->exec($this->svnpath
. ' ' . $command . ' 2>&1');
210 // make sure that we keep escaped chars
211 //$output = str_replace(array('\t', '\n', '\r'), array('\\\t', '\\\n', '\\\r'), $output);
212 //$output = preg_replace('#\\\(.)#', '\\\\\\\\' . '\1', $output);
213 //$output = str_replace('\\', '\\\\', $output);
215 $temp = implode("\n", $output);
216 if (strpos($temp, '(apr' . '_err=') !== false
)
218 $viewsvn->trigger
->error(nl2br($temp));
224 * SVN Wrapper: standard command system
228 * @param string SVN command
229 * @param string Repository
231 * @param integer Revision
233 * @return array Lines of output
235 function std($command, $repos, $path, $revision)
239 $revision = $this->rev($revision);
240 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
242 return $this->svn($command . ' ' . $repospath . $path . '@' . $revision);
250 * @param string Repository
252 * @param integer Revision
254 * @return array Lines of blame output
256 function blame($repos, $path, $revision)
258 return $this->std('blame', $repos, $path, $revision);
266 * @param string Repository
268 * @param integer Revision
270 * @return array Lines of cat output
272 function cat($repos, $path, $revision)
274 return $this->std('cat', $repos, $path, $revision);
282 * @param string Repository
284 * @param integer Lower revision
285 * @param integer Higher revision
287 * @return array Lines of diff output
289 function diff($repos, $path, $lorev, $hirev)
293 $hirev = $this->rev($hirev);
294 $lorev = $this->rev($lorev);
295 if ($lorev == 'HEAD')
300 if (is_integer($hirev) AND is_integer($lorev))
306 if ($lorev == $hirev)
312 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
314 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
322 * @param string Repository
324 * @param integer Lower revision
325 * @param integer Higher revision
327 * @return array Lines of log output
329 function log($repos, $path, $lorev, $hirev)
333 $hirev = $this->rev($hirev);
334 $lorev = $this->rev($hirev);
335 if ($lorev == 'HEAD')
340 if (is_integer($hirev) AND is_integer($lorev))
346 if ($lorev == $hirev)
352 $repospath = $viewsvn->repos
->fetch_path($repos, false
);
354 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
358 * SVN Wrapper: ls (list)
362 * @param string Repository
364 * @param integer Revision
366 * @return array Lines of list output
368 function ls($repos, $path, $revision)
370 return $this->std('list', $repos, $path, $revision);
374 * Generates a clean revision number
378 * @param integer Revision number
380 * @return mixed Cleaned revision or HEAD
382 function rev($revision)
384 if (($revision = intval($revision)) < 1)
393 * Commonly executed SVN commands that return data
394 * used in many parts of the system
420 * Constructor: bind with registry
426 $this->registry
=& $viewsvn;
430 * Checks to see if the given universal path is
435 * @param string Universal path
437 * @return bool Directory or not
439 function isdir($path)
441 $output = $this->registry
->svn
->std('info', $this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), $this->registry
->paths
->revnum
);
443 foreach ($output AS $line)
445 if (preg_match('#^Node Kind: (.*)#', $line, $matches))
447 if (trim(strtolower($matches[1])) == 'directory')
458 * Get a list of revisions for a path
462 * @param string Universal path
464 * @return array Key revisions
466 function fetch_revs($path)
468 if (!isset($this->revisions
["$path"]))
470 $log = $this->fetch_logs($path);
472 $revs = array_keys($log);
474 $this->revisions
["$path"] = array(
476 'START' => $revs[ count($revs) - 1 ],
481 return $this->revisions
["$path"];
485 * Gets the revision that is marked as HEAD
489 * @param string Universal path
491 * @return integer Revision
493 function fetch_head_rev($path)
495 $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));
497 foreach ($output AS $line)
499 if (preg_match('#^Last Changed Rev: (.*)#', $line, $matches))
505 $revs = $this->fetch_revs($path);
506 return $revs['HEAD'];
510 * Returns the previous revision to the one given
514 * @param string Universal path
515 * @param integer Arbitrary revision
517 * @return integer Previous revision (-1 if none)
519 function fetch_prev_rev($path, $current)
523 $revs = $this->fetch_revs($path);
525 if ($current == 'HEAD')
527 $current = $this->fetch_head_rev($path);
530 $index = array_search($current, $revs['revs']);
531 if ($current === false
)
533 $message->trigger
->error(sprintf($viewsvn->lang
->string('Revision r%1$s is not in path %2$s'), $current, $path));
536 if (isset($revs['revs'][ $index +
1 ]))
538 return $revs['revs'][ $index +
1 ];
551 * @param string Universal path
552 * @param bool Override the cache system?
554 * @return array Log data
556 function fetch_logs($path)
558 if (!isset($this->logs
["$path"]))
560 $log = new SVNLog($this->registry
->paths
->fetch_repos($path), $this->registry
->paths
->fetch_path($path), 0, $this->registry
->paths
->revnum
);
562 $this->logs
["$path"] = $log->fetch();
565 return $this->logs
["$path"];
569 * Returns a given log entry for a path
574 * @param string Universal path
575 * @param integer Arbitrary revision
577 * @return array Log entry
579 function fetch_log($path, $rev)
581 $logs = $this->fetch_logs($path);
583 $rev = $this->registry
->svn
->rev($rev);
586 $rev = $this->fetch_head_rev($path);
589 if (isset($logs["$rev"]))
591 return $logs["$rev"];
595 $keys = array_keys($logs);
598 for ($i = 0; $i < count($keys); $i++
)
600 if ($rev > $keys["$i"] AND $rev < $keys[ $i +
1 ])
602 return $logs["$keys[$i]"];
611 * Prints the file changed list
615 * @public array List of file changes
616 * @public string The repository
617 * @public integer Current revision
619 * @return string Processed HTML
621 function construct_file_changes($changes, $repos, $revision)
627 foreach ($changes AS $file)
629 switch ($file['action'])
633 $tooltip = $viewsvn->lang
->string('Added');
636 $class = 'file_delete';
637 $tooltip = $viewsvn->lang
->string('Deleted');
640 $class = 'file_modify';
641 $tooltip = $viewsvn->lang
->string('Modified');
644 $class = 'file_replace';
645 $tooltip = $viewsvn->lang
->string('Replaced');
649 $show['from'] = (bool
)$file['from'];
653 $class = 'file_move';
654 $tooltip = 'Moved/Copied';
655 preg_match('#(.*):([0-9]+)#', $file['from'], $matches);
656 $link['from'] = $viewsvn->paths
->out('view.php' . $viewsvn->paths
->fetch_rev_str(false
, $matches[2]), $repos . $matches[1]);
659 $link['file'] = $viewsvn->paths
->out('view.php' . $viewsvn->paths
->fetch_rev_str(false
, $revision), $repos . $file['file']);
661 eval('$files .= "' . $viewsvn->template
->fetch('file_change') . '";');
669 * Annotation/blame system; constructs an array
670 * that is ready for output
678 * Array of blame information
681 var $blame = array();
684 * Raw "svn blame" output
690 * Constructor: create blame and store data
692 * @param string Repository
694 * @param integer Revision
696 function SVNBlame($repos, $path, $revision)
700 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
705 * Returns blame for display
709 * @return array Blame data
717 * Parses the blame data
725 foreach ($this->rawoutput
AS $line)
727 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
729 $this->blame
[] = array(
730 'rev' => $matches[1],
731 'author' => $matches[2],
732 'line' => $matches[3],
733 'lineno' => $lineno++
737 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
739 $this->blame
[] = array(
740 'rev' => $matches[1],
741 'author' => $matches[2],
743 'lineno' => $lineno++
751 * Log management system; creates a complex list
752 * of SVN log information
766 * Raw "svn log" output
772 * Constructor: create log store for the given file
774 * @param string Repository
776 * @param integer Lower revision
777 * @param integer Higher revision
779 function SVNLog($repos, $path, $lorev, $hirev)
783 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
788 * Returns logs for display
792 * @return array Log data
800 * Splits up the raw output into a usable log
808 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
810 $line = $this->rawoutput
["$i"];
812 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
814 if (isset($this->logs
["$lastrev"]))
816 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
819 $this->logs
["$matches[1]"] = array(
820 'rev' => $matches[1],
821 'author' => $matches[2],
822 'date' => $matches[3],
823 'timezone' => $matches[4],
824 'lines' => $matches[6],
828 $lastrev = $matches[1];
830 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
832 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
834 $matches[2] = $amatches[1];
837 $this->logs
["$lastrev"]['files'][] = array(
838 'action' => $matches[1],
839 'file' => trim($matches[2]),
840 'from' => (isset($amatches[2]) ?
$amatches[2] : '')
845 if (trim($line) != 'Changed paths:')
847 $this->logs
["$lastrev"]['message'] .= $line . "\n";
852 if (isset($this->logs
["$lastrev"]))
854 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
859 * Trims the last dash line off a message
863 * @param string Message with annoying-ass line
865 * @return string Clean string
867 function strip_last_line($string)
869 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
874 * Diff system; constructs a diff array that
875 * is ready for output
882 * Array of diff information
888 * Raw "svn diff" output
894 * Constructor: create and store diff data
896 * @param string Repository
898 * @param integer Lower revision
899 * @param integer Higher revision
901 function SVNDiff($repos, $path, $lorev, $hirev)
905 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
910 * Returns diffs for display
914 * @return array Diff data
922 * Processes and prepares diff data
931 $indexcounter = null
;
936 foreach ($this->rawoutput
AS $line)
938 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
942 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
943 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
944 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
947 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
951 $this->diff
["$index"]['props'] = array();
955 if ($indexcounter <= 3 AND $indexcounter !== null
)
960 else if ($indexcounter == 3)
962 $indexcounter = null
;
966 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
969 $content = $matches[2];
973 $this->diff
["$index"]["$chunk"][] = array(
976 'oldlineno' => ++
$lines['old'],
977 'newlineno' => ++
$lines['new']
982 else if ($act == '+')
984 // potential line delta
985 if (count($delstack) > 0)
987 $lastline = array_shift($delstack);
989 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
991 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
993 $end = strlen($lastline['line']) +
$delta['end'];
994 $viewsvn->debug("RM delta- = " . $end);
995 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
996 $this->diff
["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
999 if (strlen($content) > $delta['start'] - $delta['end'])
1001 $end = strlen($content) +
$delta['end'];
1002 $viewsvn->debug("MK delta+ = " . $end);
1003 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
1004 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
1009 $this->diff
["$index"]["$chunk"][] = array(
1013 'newlineno' => ++
$lines['new']
1016 else if ($act == '-')
1018 $this->diff
["$index"]["$chunk"][] = $thearray = array(
1021 'oldlineno' => ++
$lines['old'],
1025 $key = count($this->diff
["$index"]["$chunk"]) - 2;
1026 $thearray['INDEX'] = $key;
1028 array_push($delstack, $thearray);
1034 if (preg_match('#^Index: (.*?)$#', $line, $matches))
1036 $index = $matches[1];
1044 if (preg_match('#^__*_$#', trim($line)))
1046 $viewsvn->debug("skipping: $line");
1050 if (preg_match('#Name: (.*?)$#', $line, $matches))
1052 $curprop = $matches[1];
1053 $viewsvn->debug("prop: $curprop");
1058 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
1061 $this->diff
["$index"]['props']["$curprop"]['add'] .= $matches[1];
1063 else if (preg_match('#^\s+?\-(.*)#', $line, $matches))
1066 $this->diff
["$index"]['props']["$curprop"]['del'] .= $matches[1];
1068 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
1070 $this->diff
["$index"]['props']["$curprop"]["$mode"] .= "\n" . $line;
1076 $this->diff
["$index"]["$chunk"][] = array(
1079 'oldlineno' => ++
$lines['old'],
1080 'newlineno' => ++
$lines['new']
1083 $delstack = array();
1089 * Returns the amount of change that occured
1094 * @param string Old line
1095 * @param string New line
1097 * @return array Difference of positions
1099 function fetch_diff_extent($old, $new)
1104 $min = min(strlen($old), strlen($new));
1106 $viewsvn->debug("min1 = $min");
1108 while ($start < $min AND $old["$start"] == $new["$start"])
1114 $min = $min - $start;
1116 $viewsvn->debug("min2 = $min");
1118 $viewsvn->debug("checking: " . $old[ strlen($old) +
$end ] . ' == ' . $new[ strlen($new) +
$end ]);
1120 while (-$end <= $min AND $old[ strlen($old) +
$end ] == $new[ strlen($new) +
$end ])
1125 return array('start' => $start, 'end' => $end +
1);
1129 /*=====================================================================*\
1130 || ###################################################################
1133 || ###################################################################
1134 \*=====================================================================*/