Dead code stripping
[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 * Annotation/blame system; constructs an array that is ready for output
205 *
206 * @package ViewSVN
207 * @version $Id$
208 */
209 class SVNBlame
210 {
211 /**
212 * Array of blame information
213 * @var array
214 * @access private
215 */
216 var $blame = array();
217
218 /**
219 * Raw "svn blame" output
220 * @var array
221 * @access private
222 */
223 var $rawoutput;
224
225 // ###################################################################
226 /**
227 * Constructor: create blame and store data
228 *
229 * @param object Controller
230 */
231 function SVNBlame(&$controller)
232 {
233 $this->rawoutput = $controller->library->command('blame');
234 $this->process();
235 }
236
237 // ###################################################################
238 /**
239 * Returns blame for display
240 *
241 * @access public
242 *
243 * @return array Blame data
244 */
245 function fetch()
246 {
247 return $this->blame;
248 }
249
250 // ###################################################################
251 /**
252 * Parses the blame data
253 *
254 * @access private
255 */
256 function process()
257 {
258 $lineno = 1;
259
260 foreach ($this->rawoutput AS $line)
261 {
262 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
263 {
264 $this->blame[] = array(
265 'rev' => $matches[1],
266 'author' => $matches[2],
267 'line' => $matches[3],
268 'lineno' => $lineno++
269 );
270 }
271 // a blank line
272 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
273 {
274 $this->blame[] = array(
275 'rev' => $matches[1],
276 'author' => $matches[2],
277 'line' => '',
278 'lineno' => $lineno++
279 );
280 }
281 }
282 }
283 }
284
285 /**
286 * Log management system; creates a complex list of SVN log information
287 *
288 * @package ViewSVN
289 * @version $Id$
290 */
291 class SVNLog
292 {
293 /**
294 * Array of logs
295 * @var array
296 * @access private
297 */
298 var $logs = array();
299
300 /**
301 * Raw "svn log" output
302 * @var array
303 * @access private
304 */
305 var $rawoutput;
306
307 // ###################################################################
308 /**
309 * Constructor: create log store for the given file
310 *
311 * @param object Controller
312 */
313 function SVNLog(&$controller)
314 {
315 $this->rawoutput = $controller->library->command('log', null);
316 $this->process();
317 }
318
319 // ###################################################################
320 /**
321 * Returns logs for display
322 *
323 * @access public
324 *
325 * @return array Log data
326 */
327 function fetch()
328 {
329 return $this->logs;
330 }
331
332 // ###################################################################
333 /**
334 * Splits up the raw output into a usable log
335 *
336 * @access private
337 */
338 function process()
339 {
340 $lastrev = 0;
341
342 for ($i = 1; $i <= sizeof($this->rawoutput) - 1; $i++)
343 {
344 $line = $this->rawoutput["$i"];
345
346 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
347 {
348 if (isset($this->logs["$lastrev"]))
349 {
350 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
351 }
352
353 $this->logs["$matches[1]"] = array(
354 'rev' => $matches[1],
355 'author' => $matches[2],
356 'date' => $matches[3],
357 'timezone' => $matches[4],
358 'lines' => $matches[6],
359 'message' => ''
360 );
361
362 $lastrev = $matches[1];
363 }
364 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
365 {
366 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
367 {
368 $matches[2] = $amatches[1];
369 }
370
371 $this->logs["$lastrev"]['files'][] = array(
372 'action' => $matches[1],
373 'file' => trim($matches[2]),
374 'from' => (isset($amatches[2]) ? $amatches[2] : '')
375 );
376 }
377 else
378 {
379 if (trim($line) != 'Changed paths:')
380 {
381 $this->logs["$lastrev"]['message'] .= $line . "\n";
382 }
383 }
384 }
385
386 if (isset($this->logs["$lastrev"]))
387 {
388 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
389 }
390 }
391
392 // ###################################################################
393 /**
394 * Trims the last dash line off a message
395 *
396 * @access private
397 *
398 * @param string Message with annoying-ass line
399 *
400 * @return string Clean string
401 */
402 function strip_last_line($string)
403 {
404 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
405 }
406 }
407
408 /**
409 * Diff system; constructs a diff array that is ready for output
410 *
411 * @package ViewSVN
412 * @version $Id$
413 */
414 class SVNDiff
415 {
416 /**
417 * Array of diff information
418 * @var array
419 * @access private
420 */
421 var $diff = array();
422
423 /**
424 * Raw "svn diff" output
425 * @var array
426 * @access private
427 */
428 var $rawoutput;
429
430 // ###################################################################
431 /**
432 * Constructor: create and store diff data
433 *
434 * @param object Controller
435 * @param integer Lower revision
436 * @param integer Higher revision
437 */
438 function SVNDiff(&$controller, $lorev, $hirev)
439 {
440 $this->rawoutput = $controller->library->diff($lorev, $hirev);
441 $this->process();
442 }
443
444 // ###################################################################
445 /**
446 * Returns diffs for display
447 *
448 * @access public
449 *
450 * @return array Diff data
451 */
452 function fetch()
453 {
454 return $this->diff;
455 }
456
457 // ###################################################################
458 /**
459 * Processes and prepares diff data
460 *
461 * @access private
462 */
463 function process()
464 {
465 global $viewsvn;
466
467 $chunk = 0;
468 $indexcounter = null;
469 $curprop = '';
470
471 $delstack = array();
472
473 foreach ($this->rawoutput AS $line)
474 {
475 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
476 {
477 $property = false;
478 $delstack = array();
479 $this->diff["$index"][ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
480 $lines['old'] = $this->diff["$index"]["$chunk"]['hunk']['old']['line'] - 1;
481 $lines['new'] = $this->diff["$index"]["$chunk"]['hunk']['new']['line'] - 1;
482 continue;
483 }
484 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
485 {
486 $property = true;
487 $index = $bits[1];
488 $this->diff["$index"]['props'] = array();
489 continue;
490 }
491
492 if ($indexcounter <= 3 AND $indexcounter !== null)
493 {
494 $indexcounter++;
495 continue;
496 }
497 else if ($indexcounter == 3)
498 {
499 $indexcounter = null;
500 continue;
501 }
502
503 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
504 {
505 $act = $matches[1];
506 $content = $matches[2];
507
508 if ($act == ' ')
509 {
510 $this->diff["$index"]["$chunk"][] = array(
511 'line' => $content,
512 'act' => '',
513 'oldlineno' => ++$lines['old'],
514 'newlineno' => ++$lines['new']
515 );
516
517 $delstack = array();
518 }
519 else if ($act == '+')
520 {
521 // potential line delta
522 if (sizeof($delstack) > 0)
523 {
524 $lastline = array_shift($delstack);
525
526 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
527 {
528 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
529 {
530 $end = strlen($lastline['line']) + $delta['end'];
531 $viewsvn->debug("RM delta- = " . $end);
532 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
533 $this->diff["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
534 }
535
536 if (strlen($content) > $delta['start'] - $delta['end'])
537 {
538 $end = strlen($content) + $delta['end'];
539 $viewsvn->debug("MK delta+ = " . $end);
540 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
541 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
542 }
543 }
544 }
545
546 $this->diff["$index"]["$chunk"][] = array(
547 'line' => $content,
548 'act' => '+',
549 'oldlineno' => '',
550 'newlineno' => ++$lines['new']
551 );
552 }
553 else if ($act == '-')
554 {
555 $this->diff["$index"]["$chunk"][] = $thearray = array(
556 'line' => $content,
557 'act' => '-',
558 'oldlineno' => ++$lines['old'],
559 'newlineno' => ''
560 );
561
562 $key = sizeof($this->diff["$index"]["$chunk"]) - 2;
563 $thearray['INDEX'] = $key;
564
565 array_push($delstack, $thearray);
566 }
567 }
568 // whitespace lines
569 else
570 {
571 if (preg_match('#^Index: (.*?)$#', $line, $matches))
572 {
573 $index = $matches[1];
574 $indexcounter = 1;
575 $chunk = 0;
576 continue;
577 }
578
579 if ($property)
580 {
581 if (preg_match('#^__*_$#', trim($line)))
582 {
583 $viewsvn->debug("skipping: $line");
584 continue;
585 }
586
587 if (preg_match('#Name: (.*?)$#', $line, $matches))
588 {
589 $curprop = $matches[1];
590 $viewsvn->debug("prop: $curprop");
591 continue;
592 }
593 else
594 {
595 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
596 {
597 $mode = 'add';
598 $this->diff["$index"]['props']["$curprop"]['add'] .= $matches[1];
599 }
600 else if (preg_match('#^\s+?\-(.*)#', $line, $matches))
601 {
602 $mode = 'del';
603 $this->diff["$index"]['props']["$curprop"]['del'] .= $matches[1];
604 }
605 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
606 {
607 $this->diff["$index"]['props']["$curprop"]["$mode"] .= "\n" . $line;
608 }
609 continue;
610 }
611 }
612
613 $this->diff["$index"]["$chunk"][] = array(
614 'line' => '',
615 'act' => '',
616 'oldlineno' => ++$lines['old'],
617 'newlineno' => ++$lines['new']
618 );
619
620 $delstack = array();
621 }
622 }
623 }
624
625 // ###################################################################
626 /**
627 * Returns the amount of change that occured between two lines
628 *
629 * @access private
630 *
631 * @param string Old line
632 * @param string New line
633 *
634 * @return array Difference of positions
635 */
636 function fetch_diff_extent($old, $new)
637 {
638 global $viewsvn;
639
640 $start = 0;
641 $min = min(strlen($old), strlen($new));
642
643 $viewsvn->debug("min1 = $min");
644
645 while ($start < $min AND $old["$start"] == $new["$start"])
646 {
647 $start++;
648 }
649
650 $end = -1;
651 $min = $min - $start;
652
653 $viewsvn->debug("min2 = $min");
654
655 $viewsvn->debug("checking: " . $old[ strlen($old) + $end ] . ' == ' . $new[ strlen($new) + $end ]);
656
657 while (-$end <= $min AND $old[ strlen($old) + $end ] == $new[ strlen($new) + $end ])
658 {
659 $end--;
660 }
661
662 return array('start' => $start, 'end' => $end + 1);
663 }
664 }
665
666 /*=====================================================================*\
667 || ###################################################################
668 || # $HeadURL$
669 || # $Id$
670 || ###################################################################
671 \*=====================================================================*/
672 ?>