From 4ae888326329a87b548d4c9be7e6d2ed87ee70b5 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 28 Aug 2005 06:03:56 +0000 Subject: [PATCH] Advanced diff data is done --- dev/difftest.php | 28 +++++++++++++--- includes/init.php | 4 +-- includes/svnlib.php | 79 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 7 deletions(-) diff --git a/dev/difftest.php b/dev/difftest.php index bcd46de..b16b504 100644 --- a/dev/difftest.php +++ b/dev/difftest.php @@ -19,11 +19,33 @@ require_once('./global.php'); $diff = new SVNDiff('bugtrack', '/trunk/index.php', 351, 359); +echo << + + +HTML; + echo ''; foreach ($diff->fetch() AS $hunk) { - //print_r($hunk); foreach ($hunk AS $key => $line) { if ($key == 'hunk' AND isset($line['old'])) @@ -38,12 +60,10 @@ foreach ($diff->fetch() AS $hunk) if ($line['act'] == '+') { $color = '#DDFFDD'; - $textc = '#99EE99'; } else if ($line['act'] == '-') { $color = '#FFDDDD'; - $textc = '#EE9999'; } else { @@ -62,8 +82,6 @@ foreach ($diff->fetch() AS $hunk) echo '
'; -print_r($diff); - /*=====================================================================*\ || ################################################################### || # $HeadURL$ diff --git a/includes/init.php b/includes/init.php index 79aa90f..f1959a1 100644 --- a/includes/init.php +++ b/includes/init.php @@ -10,8 +10,8 @@ || ################################################################### || \*=====================================================================*/ -//error_reporting(E_ALL); -error_reporting(E_ALL & ~E_NOTICE); +error_reporting(E_ALL); +//error_reporting(E_ALL & ~E_NOTICE); define('ISSO_MT_START', microtime()); diff --git a/includes/svnlib.php b/includes/svnlib.php index 290393b..f7f9168 100644 --- a/includes/svnlib.php +++ b/includes/svnlib.php @@ -75,6 +75,10 @@ class SVNLib // spaces to nbsp $string = str_replace(' ', ' ', $string); + // convert advanced diff + $string = str_replace(array('{@++}', '{@--}'), array('', ''), $string); + $string = str_replace(array('{/@++}', '{/@--}'), '', $string); + // nl2br $string = nl2br($string); @@ -516,12 +520,17 @@ class SVNDiff { $chunk = 0; $capture = false; + $lastact = ''; + $lastcontent = ''; foreach ($this->rawoutput AS $line) { if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits)) { $capture = true; + $lastact = ''; + $lastcontent = ''; + $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; @@ -550,6 +559,26 @@ class SVNDiff } else if ($act == '+') { + // potential line delta + if ($lastact == '-') + { + if ($delta = @$this->fetch_diff_extent($lastcontent, $content)) + { + $diff = $delta['end'] - $delta['start']; + + if (strlen($lastcontent) > $delta['end'] - $diff) + { + $removed = substr($lastcontent, $delta['start'], $diff); + $this->diff["$chunk"][ count($this->diff["$chunk"]) - 2 ]['line'] = substr_replace($lastcontent, '{@--}' . $removed . '{/@--}', $delta['start'], $diff); + } + else + { + $added = substr($content, $delta['start'], $diff); + $content = substr_replace($content, '{@++}' . $added . '{/@++}', $delta['start'], $diff); + } + } + } + $this->diff["$chunk"][] = array( 'line' => $content, 'act' => '+', @@ -559,6 +588,8 @@ class SVNDiff } else if ($act == '-') { + $lastcontent = $content; + $this->diff["$chunk"][] = array( 'line' => $content, 'act' => '-', @@ -566,10 +597,14 @@ class SVNDiff 'newlineno' => '' ); } + + $lastact = $act; } // whitespace lines else { + $lastact = ''; + $this->diff["$chunk"][] = array( 'line' => '', 'act' => '', @@ -579,6 +614,50 @@ class SVNDiff } } } + + /** + * Returns the amount of change that occured + * between two lines + * + * @access private + * + * @param string Old line + * @param string New line + * + * @return array Difference of positions + */ + function fetch_diff_extent($old, $new) + { + $start = 0; + $min = min(strlen($old), strlen($new)); + + for ($start = 0; $start < $min; $start++) + { + if ($old{"$start"} != $new{"$start"}) + { + break; + } + } + + $max = max(strlen($old), strlen($new)); + + for ($end = $max; $end > 0; $end--) + { + if ($old{"$end"} != $new{"$end"}) + { + break; + } + } + + $end++; + + if ($start == 0 AND $end == $max) + { + return false; + } + + return array('start' => $start, 'end' => $end); + } } /*=====================================================================*\ -- 2.22.5