jablonka.czprosek.czf

websvn

Subversion Repositories:
[/] [include/] [diff_inc.php] - Blame information for rev 5

 

Line No. Rev Author Line
15simandl<?php
2// WebSVN - Subversion repository viewing via the web using PHP
3// Copyright (C) 2004-2006 Tim Armes
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18//
19// --
20//
21// diff_inc.php
22//
23// Diff to files
24 
25ini_set('include_path', $locwebsvnreal.'/lib/pear'.$config->pathSeparator.ini_get('include_path'));
26@include_once('Text/Diff.php');
27@include_once('Text/Diff/Renderer.php');
28@include_once('Text/Diff/Renderer/unified.php');
29 
30$arrayBased = false;
31$fileBased = false;
32 
33function nextLine(&$obj) {
34 global $arrayBased, $fileBased;
35 if ($arrayBased) return array_shift($obj);
36 if ($fileBased) return fgets($obj);
37 return '';
38}
39 
40function endOfFile(&$obj) {
41 global $arrayBased, $fileBased;
42 if ($arrayBased) return count($obj) == 0;
43 if ($fileBased) return feof($obj);
44 return true;
45}
46 
47function diff_result($all, $rep, $ent, $newtname, $oldtname, $obj) {
48 $contentEncoding = $rep->getContentEncoding();
49 
50 $ofile = fopen($oldtname, 'r');
51 $nfile = fopen($newtname, 'r');
52 
53 // Get the first real line
54 $line = nextLine($obj);
55 
56 $index = 0;
57 $listing = array();
58 
59 $curoline = 1;
60 $curnline = 1;
61 
62 while (!endOfFile($obj)) {
63 // Get the first line of this range
64 $oline = 0;
65 sscanf($line, "@@ -%d", $oline);
66 $line = substr($line, strpos($line, "+"));
67 $nline = 0;
68 sscanf($line, "+%d", $nline);
69 
70 while ($curoline < $oline || $curnline < $nline) {
71 if ($all) {
72 $listing[$index]["rev1diffclass"] = "diff";
73 $listing[$index]["rev2diffclass"] = "diff";
74 }
75 
76 if ($curoline < $oline) {
77 $nl = fgets($ofile);
78 
79 if ($all) {
80 $line = rtrim($nl);
81 if ($ent) $line = replaceEntities($line, $rep);
82 else $line = toOutputEncoding($line, $contentEncoding);
83 if (strip_tags($line) == '') $line = '&nbsp;';
84 
85 $listing[$index]["rev1line"] = hardspace($line);
86 }
87 
88 $curoline++;
89 } else if ($all) {
90 $listing[$index]["rev1line"] = "&nbsp;";
91 }
92 
93 if ($curnline < $nline) {
94 $nl = fgets($nfile);
95 
96 if ($all) {
97 $line = rtrim($nl);
98 if ($ent) $line = replaceEntities($line, $rep);
99 else $line = toOutputEncoding($line, $contentEncoding);
100 if (strip_tags($line) == '') $line = '&nbsp;';
101 
102 $listing[$index]["rev2line"] = hardspace($line);
103 }
104 $curnline++;
105 
106 } else if ($all) {
107 $listing[$index]["rev2line"] = "&nbsp;";
108 }
109 
110 if ($all) {
111 $listing[$index]["rev1lineno"] = 0;
112 $listing[$index]["rev2lineno"] = 0;
113 
114 $index++;
115 }
116 }
117 
118 if (!$all) {
119 // Output the line numbers
120 $listing[$index]["rev1lineno"] = $oline;
121 $listing[$index]["rev2lineno"] = $nline;
122 $index++;
123 }
124 
125 $fin = false;
126 while (!endOfFile($obj) && !$fin) {
127 $line = nextLine($obj);
128 if ($line === false || $line === '' || strncmp($line, "@@", 2) == 0) {
129 $fin = true;
130 } else {
131 $listing[$index]["rev1lineno"] = 0;
132 $listing[$index]["rev2lineno"] = 0;
133 
134 $mod = $line{0};
135 
136 $line = rtrim(substr($line, 1));
137 if ($ent) $line = replaceEntities($line, $rep);
138 else $line = toOutputEncoding($line, $contentEncoding);
139 if (strip_tags($line) == '') $line = '&nbsp;';
140 $text = hardspace($line);
141 $listing[$index]["rev1line"] = $text;
142 
143 switch ($mod) {
144 case "-":
145 $listing[$index]["rev1diffclass"] = "diffdeleted";
146 $listing[$index]["rev2diffclass"] = "diff";
147 
148 $listing[$index]["rev1line"] = $text;
149 $listing[$index]["rev2line"] = "&nbsp;";
150 
151 fgets($ofile);
152 $curoline++;
153 
154 break;
155 
156 case "+":
157 // Try to mark "changed" line sensibly
158 if (!empty($listing[$index-1]) && empty($listing[$index-1]["rev1lineno"]) && @$listing[$index-1]["rev1diffclass"] == "diffdeleted" && @$listing[$index-1]["rev2diffclass"] == "diff") {
159 $i = $index - 1;
160 while (!empty($listing[$i-1]) && empty($listing[$i-1]["rev1lineno"]) && $listing[$i-1]["rev1diffclass"] == "diffdeleted" && $listing[$i-1]["rev2diffclass"] == "diff") {
161 $i--;
162 }
163 
164 $listing[$i]["rev1diffclass"] = "diffchanged";
165 $listing[$i]["rev2diffclass"] = "diffchanged";
166 $listing[$i]["rev2line"] = $text;
167 
168 fgets($nfile);
169 $curnline++;
170 
171 // Don't increment the current index count
172 $index--;
173 
174 } else {
175 $listing[$index]["rev1diffclass"] = "diff";
176 $listing[$index]["rev2diffclass"] = "diffadded";
177 
178 $listing[$index]["rev1line"] = "&nbsp;";
179 $listing[$index]["rev2line"] = $text;
180 
181 fgets($nfile);
182 $curnline++;
183 }
184 break;
185 
186 default:
187 $listing[$index]["rev1diffclass"] = "diff";
188 $listing[$index]["rev2diffclass"] = "diff";
189 
190 $nl = fgets($ofile);
191 $line = rtrim($nl);
192 if ($ent) $line = replaceEntities($line, $rep);
193 else $line = toOutputEncoding($line, $contentEncoding);
194 if (strip_tags($line) == '') $line = '&nbsp;';
195 $listing[$index]["rev1line"] = hardspace($line);
196 $curoline++;
197 
198 $nl = fgets($nfile);
199 $line = rtrim($nl);
200 if ($ent) $line = replaceEntities($line, $rep);
201 else $line = toOutputEncoding($line, $contentEncoding);
202 if (strip_tags($line) == '') $line = '&nbsp;';
203 $listing[$index]["rev2line"] = hardspace($line);
204 $curnline++;
205 
206 break;
207 }
208 }
209 
210 if (!$fin) {
211 $index++;
212 }
213 }
214 }
215 
216 // Output the rest of the files
217 if ($all) {
218 while (!feof($ofile) || !feof($nfile)) {
219 $listing[$index]["rev1diffclass"] = "diff";
220 $listing[$index]["rev2diffclass"] = "diff";
221 
222 $line = rtrim(fgets($ofile));
223 if ($ent) $line = replaceEntities($line, $rep);
224 else $line = toOutputEncoding($line, $contentEncoding);
225 if (strip_tags($line) == '') $line = '&nbsp;';
226 
227 if (!feof($ofile)) {
228 $listing[$index]["rev1line"] = hardspace($line);
229 } else {
230 $listing[$index]["rev1line"] = "&nbsp;";
231 }
232 
233 $line = rtrim(fgets($nfile));
234 if ($ent) $line = replaceEntities(rtrim(fgets($nfile)), $rep);
235 else $line = toOutputEncoding($line, $contentEncoding);
236 if (strip_tags($line) == '') $line = '&nbsp;';
237 
238 if (!feof($nfile)) {
239 $listing[$index]["rev2line"] = hardspace($line);
240 } else {
241 $listing[$index]["rev2line"] = "&nbsp;";
242 }
243 
244 $listing[$index]["rev1lineno"] = 0;
245 $listing[$index]["rev2lineno"] = 0;
246 
247 $index++;
248 }
249 }
250 
251 fclose($ofile);
252 fclose($nfile);
253 
254 return $listing;
255}
256 
257function command_diff($all, $ignoreWhitespace, $rep, $ent, $newtname, $oldtname) {
258 global $config, $lang, $arrayBased, $fileBased;
259 
260 $context = 5;
261 
262 if ($all) {
263 // Setting the context to 0 makes diff generate the wrong line numbers!
264 $context = 1;
265 }
266 
267 // Open a pipe to the diff command with $context lines of context
268 
269 $cmd = quoteCommand($config->diff." -w -U $context \"$oldtname\" \"$newtname\"");
270 
271 $descriptorspec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
272 
273 $resource = proc_open($cmd, $descriptorspec, $pipes);
274 $error = "";
275 
276 if (is_resource($resource)) {
277 // We don't need to write
278 fclose($pipes[0]);
279 
280 $diff = $pipes[1];
281 
282 // Ignore the 3 header lines
283 $line = fgets($diff);
284 $line = fgets($diff);
285 
286 $arrayBased = false;
287 $fileBased = true;
288 $listing = diff_result($all, $rep, $ent, $newtname, $oldtname, $diff);
289 fclose($pipes[1]);
290 
291 while (!feof($pipes[2])) {
292 $error .= fgets($pipes[2]);
293 }
294 
295 $error = toOutputEncoding(trim($error));
296 
297 if (!empty($error)) $error = "<p>".$lang['BADCMD'].": <code>".$cmd."</code></p><p>".nl2br($error)."</p>";
298 
299 fclose($pipes[2]);
300 
301 proc_close($resource);
302 
303 } else {
304 $error = "<p>".$lang['BADCMD'].": <code>".$cmd."</code></p>";
305 }
306 
307 if (!empty($error)) {
308 echo $error;
309 
310 if (is_resource($resource)) {
311 fclose($pipes[0]);
312 fclose($pipes[1]);
313 fclose($pipes[2]);
314 
315 proc_close($resource);
316 }
317 exit;
318 }
319 
320 return $listing;
321}
322 
323function inline_diff($all, $ignoreWhitespace, $rep, $ent, $newtname, $oldtname) {
324 global $arrayBased, $fileBased;
325 
326 $context = 5;
327 if ($all) {
328 // Setting the context to 0 makes diff generate the wrong line numbers!
329 $context = 1;
330 }
331 
332 // modify error reporting level to suppress deprecated/strict warning "Assigning the return value of new by reference"
333 $bckLevel = error_reporting();
334 $removeLevel = 0;
335 if (version_compare(PHP_VERSION, '5.3.0alpha') !== -1) {
336 $removeLevel = E_DEPRECATED;
337 } else if (version_compare(PHP_VERSION, '5.0.0') !== -1) {
338 $removeLevel = E_STRICT;
339 }
340 $modLevel = $bckLevel & (~$removeLevel);
341 error_reporting($modLevel);
342 
343 // Create the diff class
344 $fromLines = explode("\n", file_get_contents($oldtname));
345 $toLines = explode("\n", file_get_contents($newtname));
346 if (!$ignoreWhitespace) {
347 $diff = new Text_Diff('auto', array($fromLines, $toLines));
348 } else {
349 $whitespaces = array(' ', "\t", "\n", "\r");
350 $mappedFromLines = array();
351 foreach ($fromLines as $line) {
352 $mappedFromLines[] = str_replace($whitespaces, array(), $line);
353 }
354 $mappedToLines = array();
355 foreach ($toLines as $line) {
356 $mappedToLines[] = str_replace($whitespaces, array(), $line);
357 }
358 $diff = new Text_MappedDiff($fromLines, $toLines, $mappedFromLines, $mappedToLines);
359 }
360 $renderer = new Text_Diff_Renderer_unified(array('leading_context_lines' => $context, 'trailing_context_lines' => $context));
361 $rendered = explode("\n", $renderer->render($diff));
362 
363 // restore previous error reporting level
364 error_reporting($bckLevel);
365 
366 $arrayBased = true;
367 $fileBased = false;
368 $listing = diff_result($all, $rep, $ent, $newtname, $oldtname, $rendered);
369 
370 return $listing;
371}
372 
373function do_diff($all, $ignoreWhitespace, $rep, $ent, $newtname, $oldtname) {
374 if (class_exists('Text_Diff')) {
375 return inline_diff($all, $ignoreWhitespace, $rep, $ent, $newtname, $oldtname);
376 } else {
377 return command_diff($all, $ignoreWhitespace, $rep, $ent, $newtname, $oldtname);
378 }
379}

Powered by WebSVN 2.2.1