]>
src.bluestatic.org Git - viewsvn.git/blob - includes/class_diff.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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 \*=====================================================================*/
25 * This class parses a diff file from a unified diff file and turns it
26 * into a HTML-prettied output
29 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
37 * Raw unified diff text
43 * The current index (file)
48 // ###################################################################
52 * @param string Raw unified diff output
54 public function __construct($raw)
59 // ###################################################################
61 * This function initiates the parsing process and returns the output
62 * in hunks, line-by-line
64 * @return array Array of outputted hunked information
66 public function fetch()
76 foreach ($this->raw
AS $line)
78 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
82 $this->diff
["$index"][ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
83 $lines['old'] = $this->diff["$index"]["$chunk"]['hunk']['old']['line'] - 1;
84 $lines['new'] = $this->diff["$index"]["$chunk"]['hunk']['new']['line'] - 1;
87 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
91 $this->diff["$index"]['props'] = array();
95 if ($indexcounter <= 3 AND $indexcounter !== null)
100 else if ($indexcounter == 3)
102 $indexcounter = null;
106 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
109 $content = $matches[2];
113 $this->diff
["$index"]["$chunk"][] = array(
116 'oldlineno' => ++
$lines['old'],
117 'newlineno' => ++
$lines['new']
122 else if ($act == '+')
124 // potential line delta
125 if (sizeof($delstack) > 0)
127 $lastline = array_shift($delstack);
129 if ($delta = @$this->_fetchDiffExtent($lastline['line'], $content))
131 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
133 $end = strlen($lastline['line']) +
$delta['end'];
134 BSRegister
::Debug("RM delta- = " . $end);
135 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
136 $this->diff
["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
139 if (strlen($content) > $delta['start'] - $delta['end'])
141 $end = strlen($content) +
$delta['end'];
142 BSRegister
::Debug("MK delta+ = " . $end);
143 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
144 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
149 $this->diff
["$index"]["$chunk"][] = array(
153 'newlineno' => ++
$lines['new']
156 else if ($act == '-')
158 $this->diff
["$index"]["$chunk"][] = $thearray = array(
161 'oldlineno' => ++
$lines['old'],
165 $key = sizeof($this->diff
["$index"]["$chunk"]) - 2;
166 $thearray['INDEX'] = $key;
168 array_push($delstack, $thearray);
174 if (preg_match('#^Index: (.*?)$#', $line, $matches))
176 $index = $matches[1];
184 if (preg_match('#^__*_$#', trim($line)))
186 BSRegister
::Debug("skipping: $line");
190 if (preg_match('#Name: (.*?)$#', $line, $matches))
192 $curprop = $matches[1];
193 BSRegister::Debug("prop
: $curprop");
198 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
201 $this->diff
["$index"]['props']["$curprop"]['add'] .= $matches[1];
203 else if (preg_match('#^\s+?\-(.*)#', $line, $matches))
206 $this->diff
["$index"]['props']["$curprop"]['del'] .= $matches[1];
208 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
210 $this->diff
["$index"]['props']["$curprop"]["$mode"] .= "\n
" . $line;
216 $this->diff["$index"]["$chunk"][] = array(
219 'oldlineno' => ++$lines['old'],
220 'newlineno' => ++$lines['new']
230 // ###################################################################
232 * Returns the amount of change that occured between two lines
234 * @param string Old line
235 * @param string New line
237 * @return array Difference of positions
239 private function _fetchDiffExtent($old, $new)
244 $min = min(strlen($old), strlen($new));
246 BSRegister::Debug("min1
= $min");
248 while ($start < $min AND $old["$start"] == $new["$start"])
254 $min = $min - $start;
256 BSRegister
::Debug("min2 = $min");
258 BSRegister::Debug("checking
: " . $old[ strlen($old) + $end ] . ' == ' . $new[ strlen($new) + $end ]);
260 while (-$end <= $min AND $old[ strlen($old) + $end ] == $new[ strlen($new) + $end ])
265 return array('start' => $start, 'end' => $end + 1);
269 /*=====================================================================*\
270 || ###################################################################
273 || ###################################################################
274 \*=====================================================================*/