- Blame now works
[viewsvn.git] / includes / svnlib.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * Command line library and SVN-PHP bridge extension
24 *
25 * @package ViewSVN
26 */
27
28 /**
29 * This class acts as the bridge between the command line Xquery class
30 * and the various display files. It handles preparation of Xquery
31 * code.
32 *
33 * @package ViewSVN
34 * @version $Id$
35 */
36 class SVNLib
37 {
38 /**
39 * Path to the SVN binary
40 * @var string
41 * @access private
42 */
43 var $svnpath;
44
45 /**
46 * Controller
47 * @var object
48 * @access private
49 */
50 var $controller;
51
52 // ###################################################################
53 /**
54 * Constructor: validate SVN path
55 *
56 * @param object Controller
57 */
58 function SVNLib(&$controller)
59 {
60 $this->controller =& $controller;
61
62 $this->svnpath =& $this->controller->xquery->cmd($this->controller->registry->svnpath);
63
64 $access = $this->controller->xquery->exec($this->svnpath . ' --version');
65
66 if (!$access)
67 {
68 $this->controller->registry->trigger->error($this->controller->registry->lang->string('The SVN binary could not be located'));
69 }
70
71 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
72 {
73 $this->controller->registry->trigger->error($this->controller->registry->lang->string('The SVN binary does not appear to be valid (it failed our tests)'));
74 }
75 }
76
77 // ###################################################################
78 /**
79 * Executes the SVN binary
80 *
81 * @access private
82 *
83 * @param string Command
84 *
85 * @return array Output
86 */
87 function svn($command)
88 {
89 $output = $this->controller->xquery->exec($this->svnpath . ' ' . $command . ' 2>&1');
90
91 $temp = implode("\n", $output);
92 if (strpos($temp, '(apr' . '_err=') !== false)
93 {
94 $this->controller->registry->trigger->error(nl2br($temp));
95 }
96 return $output;
97 }
98
99 // ###################################################################
100 /**
101 * This function is used to prepare a common command. It sends the
102 * specified command along with the repository path and the current
103 * node. The revision is either fectched from the argument list or
104 * from the node, too.
105 *
106 * @access public
107 *
108 * @param string SVN command
109 * @param bool Alternate revision
110 *
111 * @return array Lines of output
112 */
113 function command($command, $rev = false)
114 {
115 global $viewsvn;
116
117 $revision = ($rev !== false ? $rev : SVNCommon::rev($this->controller->revnum));
118
119 return $this->svn($command . ' ' . $this->controller->repospath . $this->controller->path . ($revision !== null ? '@' . $revision : ''));
120 }
121
122 // ###################################################################
123 /**
124 * A library complicator function that handles the diff command because
125 * the standard command() system does not work with more complex args.
126 *
127 * @access public
128 *
129 * @param integer Lower revision
130 * @param integer Higher revision
131 *
132 * @return array Lines of diff output
133 */
134 function diff($lorev, $hirev)
135 {
136 global $viewsvn;
137
138 $hirev = SVNCommon::rev($hirev);
139 $lorev = SVNCommon::rev($lorev);
140 if ($lorev == 'HEAD')
141 {
142 $lorev = 1;
143 }
144
145 if (is_integer($hirev) AND is_integer($lorev))
146 {
147 if ($lorev > $hirev)
148 {
149 $lorev = $hirev - 1;
150 }
151 if ($lorev == $hirev)
152 {
153 $lorev = 0;
154 }
155 }
156
157 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $this->controller->repospath . $this->controller->path);
158 }
159
160 // ###################################################################
161 /**
162 * A library complicator function to create log output. This is needed
163 * because command() doesn't handle ranged revisions.
164 *
165 * @access public
166 *
167 * @param string Repository
168 * @param string Path
169 * @param integer Lower revision
170 * @param integer Higher revision
171 *
172 * @return array Lines of log output
173 */
174 function log($lorev, $hirev)
175 {
176 global $viewsvn;
177
178 $hirev = $this->rev($hirev);
179 $lorev = $this->rev($hirev);
180 if ($lorev == 'HEAD')
181 {
182 $lorev = 0;
183 }
184
185 if (is_integer($hirev) AND is_integer($lorev))
186 {
187 if ($lorev > $hirev)
188 {
189 $lorev = $hirev - 1;
190 }
191 if ($lorev == $hirev)
192 {
193 $lorev = 0;
194 }
195 }
196
197 $repospath = $viewsvn->repos->fetch_path($repos, false);
198
199 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
200 }
201 }
202
203 /**
204 * Diff system; constructs a diff array that is ready for output
205 *
206 * @package ViewSVN
207 * @version $Id$
208 */
209 class SVNDiff
210 {
211 /**
212 * Array of diff information
213 * @var array
214 * @access private
215 */
216 var $diff = array();
217
218 /**
219 * Raw "svn diff" output
220 * @var array
221 * @access private
222 */
223 var $rawoutput;
224
225 // ###################################################################
226 /**
227 * Constructor: create and store diff data
228 *
229 * @param object Controller
230 * @param integer Lower revision
231 * @param integer Higher revision
232 */
233 function SVNDiff(&$controller, $lorev, $hirev)
234 {
235 $this->rawoutput = $controller->library->diff($lorev, $hirev);
236 $this->process();
237 }
238
239 // ###################################################################
240 /**
241 * Returns diffs for display
242 *
243 * @access public
244 *
245 * @return array Diff data
246 */
247 function fetch()
248 {
249 return $this->diff;
250 }
251
252 // ###################################################################
253 /**
254 * Processes and prepares diff data
255 *
256 * @access private
257 */
258 function process()
259 {
260 global $viewsvn;
261
262 $chunk = 0;
263 $indexcounter = null;
264 $curprop = '';
265
266 $delstack = array();
267
268 foreach ($this->rawoutput AS $line)
269 {
270 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
271 {
272 $property = false;
273 $delstack = array();
274 $this->diff["$index"][ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
275 $lines['old'] = $this->diff["$index"]["$chunk"]['hunk']['old']['line'] - 1;
276 $lines['new'] = $this->diff["$index"]["$chunk"]['hunk']['new']['line'] - 1;
277 continue;
278 }
279 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
280 {
281 $property = true;
282 $index = $bits[1];
283 $this->diff["$index"]['props'] = array();
284 continue;
285 }
286
287 if ($indexcounter <= 3 AND $indexcounter !== null)
288 {
289 $indexcounter++;
290 continue;
291 }
292 else if ($indexcounter == 3)
293 {
294 $indexcounter = null;
295 continue;
296 }
297
298 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
299 {
300 $act = $matches[1];
301 $content = $matches[2];
302
303 if ($act == ' ')
304 {
305 $this->diff["$index"]["$chunk"][] = array(
306 'line' => $content,
307 'act' => '',
308 'oldlineno' => ++$lines['old'],
309 'newlineno' => ++$lines['new']
310 );
311
312 $delstack = array();
313 }
314 else if ($act == '+')
315 {
316 // potential line delta
317 if (sizeof($delstack) > 0)
318 {
319 $lastline = array_shift($delstack);
320
321 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
322 {
323 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
324 {
325 $end = strlen($lastline['line']) + $delta['end'];
326 $viewsvn->debug("RM delta- = " . $end);
327 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
328 $this->diff["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
329 }
330
331 if (strlen($content) > $delta['start'] - $delta['end'])
332 {
333 $end = strlen($content) + $delta['end'];
334 $viewsvn->debug("MK delta+ = " . $end);
335 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
336 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
337 }
338 }
339 }
340
341 $this->diff["$index"]["$chunk"][] = array(
342 'line' => $content,
343 'act' => '+',
344 'oldlineno' => '',
345 'newlineno' => ++$lines['new']
346 );
347 }
348 else if ($act == '-')
349 {
350 $this->diff["$index"]["$chunk"][] = $thearray = array(
351 'line' => $content,
352 'act' => '-',
353 'oldlineno' => ++$lines['old'],
354 'newlineno' => ''
355 );
356
357 $key = sizeof($this->diff["$index"]["$chunk"]) - 2;
358 $thearray['INDEX'] = $key;
359
360 array_push($delstack, $thearray);
361 }
362 }
363 // whitespace lines
364 else
365 {
366 if (preg_match('#^Index: (.*?)$#', $line, $matches))
367 {
368 $index = $matches[1];
369 $indexcounter = 1;
370 $chunk = 0;
371 continue;
372 }
373
374 if ($property)
375 {
376 if (preg_match('#^__*_$#', trim($line)))
377 {
378 $viewsvn->debug("skipping: $line");
379 continue;
380 }
381
382 if (preg_match('#Name: (.*?)$#', $line, $matches))
383 {
384 $curprop = $matches[1];
385 $viewsvn->debug("prop: $curprop");
386 continue;
387 }
388 else
389 {
390 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
391 {
392 $mode = 'add';
393 $this->diff["$index"]['props']["$curprop"]['add'] .= $matches[1];
394 }
395 else if (preg_match('#^\s+?\-(.*)#', $line, $matches))
396 {
397 $mode = 'del';
398 $this->diff["$index"]['props']["$curprop"]['del'] .= $matches[1];
399 }
400 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
401 {
402 $this->diff["$index"]['props']["$curprop"]["$mode"] .= "\n" . $line;
403 }
404 continue;
405 }
406 }
407
408 $this->diff["$index"]["$chunk"][] = array(
409 'line' => '',
410 'act' => '',
411 'oldlineno' => ++$lines['old'],
412 'newlineno' => ++$lines['new']
413 );
414
415 $delstack = array();
416 }
417 }
418 }
419
420 // ###################################################################
421 /**
422 * Returns the amount of change that occured between two lines
423 *
424 * @access private
425 *
426 * @param string Old line
427 * @param string New line
428 *
429 * @return array Difference of positions
430 */
431 function fetch_diff_extent($old, $new)
432 {
433 global $viewsvn;
434
435 $start = 0;
436 $min = min(strlen($old), strlen($new));
437
438 $viewsvn->debug("min1 = $min");
439
440 while ($start < $min AND $old["$start"] == $new["$start"])
441 {
442 $start++;
443 }
444
445 $end = -1;
446 $min = $min - $start;
447
448 $viewsvn->debug("min2 = $min");
449
450 $viewsvn->debug("checking: " . $old[ strlen($old) + $end ] . ' == ' . $new[ strlen($new) + $end ]);
451
452 while (-$end <= $min AND $old[ strlen($old) + $end ] == $new[ strlen($new) + $end ])
453 {
454 $end--;
455 }
456
457 return array('start' => $start, 'end' => $end + 1);
458 }
459 }
460
461 /*=====================================================================*\
462 || ###################################################################
463 || # $HeadURL$
464 || # $Id$
465 || ###################################################################
466 \*=====================================================================*/
467 ?>