From 1af280dc3c22e200b88c39d1fb4ad68be54a4cfe Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 28 Aug 2005 04:33:53 +0000 Subject: [PATCH] Basic diff stuff is kind of working --- dev/difftest.php | 37 ++++++++++- includes/svnlib.php | 154 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+), 2 deletions(-) diff --git a/dev/difftest.php b/dev/difftest.php index 4dc35e6..1c74be0 100644 --- a/dev/difftest.php +++ b/dev/difftest.php @@ -10,11 +10,44 @@ || ################################################################### || \*=====================================================================*/ +chdir('./../'); require_once('./global.php'); -$output = $viewsvn->svn->blame('bugtrack', '/trunk/index.php', 0); +//$output = $viewsvn->svn->blame('bugtrack', '/trunk/index.php', 0); -print_r($output); +// diff /bugtrack/trunk/index@351:359 + +$diff = new SVNDiff('bugtrack', '/trunk/index.php', 351, 359); + +echo ''; + +foreach ($diff->fetch() AS $hunk) +{ + //print_r($hunk); + foreach ($hunk AS $key => $line) + { + if ($key == 'hunk' AND isset($line['old'])) + { + echo ' + + + '; + continue; + } + + echo ' + + + + + '; + } +} + +echo ' +
' . print_r($line, true) . '
' . $line['oldlineno'] . '' . $line['newlineno'] . '' . $viewsvn->svn->format($line['line']) . '
'; + +print_r($diff); /*=====================================================================*\ || ################################################################### diff --git a/includes/svnlib.php b/includes/svnlib.php index 7d5ae1c..5e78012 100644 --- a/includes/svnlib.php +++ b/includes/svnlib.php @@ -155,6 +155,46 @@ class SVNLib return $this->svn('cat ' . $repospath . $path . '@' . $revision); } + /** + * SVN Wrapper: diff + * + * @access protected + * + * @param string Repository + * @param string Path + * @param integer Lower revision + * @param integer Higher revision + * + * @return array Lines of diff output + */ + function diff($repos, $path, $lorev, $hirev) + { + global $viewsvn; + + $hirev = $this->rev($hirev); + $lorev = $this->rev($lorev); + if ($lorev == 'HEAD') + { + $lorev = 0; + } + + if (is_integer($hirev) AND is_integer($lorev)) + { + if ($lorev > $hirev) + { + $lorev = $hirev - 1; + } + if ($lorev == $hirev) + { + $lorev = 0; + } + } + + $repospath = $viewsvn->repos->fetch_path($repos, false); + + return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path); + } + /** * SVN Wrapper: log * @@ -247,6 +287,7 @@ class SVNBlame /** * Raw "svn blame" output + * @var array */ var $rawoutput; @@ -418,6 +459,119 @@ class SVNLog } } +/** +* Diff system; constructs a diff array that +* is ready for output +* +* @package ViewSVN +*/ +class SVNDiff +{ + /** + * Array of diff information + * @var array + */ + var $diff = array(); + + /** + * Raw "svn diff" output + * @var array + */ + var $rawoutput; + + /** + * Constructor: create and store diff data + * + * @param string Repository + * @param string Path + * @param integer Lower revision + * @param integer Higher revision + */ + function SVNDiff($repos, $path, $lorev, $hirev) + { + global $viewsvn; + + $this->rawoutput = $viewsvn->svn->diff($repos, $path, $lorev, $hirev); + $this->process(); + } + + /** + * Returns diffs for display + * + * @access public + * + * @return array Diff data + */ + function fetch() + { + return $this->diff; + } + + /** + * Processes and prepares diff data + * + * @access private + */ + function process() + { + $chunk = 0; + $capture = false; + + foreach ($this->rawoutput AS $line) + { + if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits)) + { + $capture = true; + $this->diff[ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4])); + $lines['old'] = $this->diff["$chunk"]['hunk']['old']['line'] - 1; + $lines['new'] = $this->diff["$chunk"]['hunk']['new']['line'] - 1; + continue; + } + + // ensures we don't capture before the first hunk + if (!$capture) + { + continue; + } + + if (preg_match('#^([\+\- ])(.*)#', $line, $matches)) + { + $act = $matches[1]; + $content = $matches[2]; + + if ($act == ' ') + { + $this->diff["$chunk"][] = array( + 'line' => $content, + 'act' => '', + 'oldlineno' => ++$lines['old'], + 'newlineno' => ++$lines['new'] + ); + } + else if ($act == '+') + { + $this->diff["$chunk"][] = array( + 'line' => $content, + 'act' => '+', + 'oldlineno' => '', + 'newlineno' => ++$lines['new'] + ); + } + else if ($act == '-') + { + $this->diff["$chunk"][] = array( + 'line' => $content, + 'act' => '-', + 'oldlineno' => ++$lines['old'], + 'newlineno' => '' + ); + } + } + } + + } +} + /*=====================================================================*\ || ################################################################### || # $HeadURL$ -- 2.43.5