Moving the type-specific classes from SVNCommon to SVNLib as they belong in the library
[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 interface with the SVN commands
24 *
25 * @package ViewSVN
26 */
27
28 /**
29 * Interacts with the command line subsystem to
30 * access SVN information
31 *
32 * @package ViewSVN
33 * @version $Id$
34 */
35 class SVNLib
36 {
37 /**
38 * Path to the SVN binary
39 * @var string
40 */
41 var $svnpath;
42
43 /**
44 * Controller
45 * @var object
46 * @access private
47 */
48 var $controller;
49
50 /**
51 * Constructor: validate SVN path
52 *
53 * @param object Controller
54 */
55 function SVNLib(&$controller)
56 {
57 $this->controller =& $controller;
58
59 $this->svnpath =& $this->controller->xquery->cmd($this->controller->registry->svnpath);
60
61 $access = $this->controller->xquery->exec($this->svnpath . ' --version');
62
63 if (!$access)
64 {
65 $this->controller->registry->trigger->error($this->controller->registry->lang->string('The SVN binary could not be located'));
66 }
67
68 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
69 {
70 $this->controller->registry->trigger->error($this->controller->registry->lang->string('The SVN binary does not appear to be valid (it failed our tests)'));
71 }
72 }
73
74 /**
75 * Executes the SVN binary
76 *
77 * @access private
78 *
79 * @param string Command
80 *
81 * @return array Output
82 */
83 function svn($command)
84 {
85 $output = $this->controller->xquery->exec($this->svnpath . ' ' . $command . ' 2>&1');
86
87 // make sure that we keep escaped chars
88 //$output = str_replace(array('\t', '\n', '\r'), array('\\\t', '\\\n', '\\\r'), $output);
89 //$output = preg_replace('#\\\(.)#', '\\\\\\\\' . '\1', $output);
90 //$output = str_replace('\\', '\\\\', $output);
91
92 $temp = implode("\n", $output);
93 if (strpos($temp, '(apr' . '_err=') !== false)
94 {
95 $this->controller->registry->trigger->error(nl2br($temp));
96 }
97 return $output;
98 }
99
100 /**
101 * SVN Wrapper: standard command system
102 *
103 * @access private
104 *
105 * @param string SVN command
106 * @param string Repository
107 * @param string Path
108 * @param integer Revision
109 *
110 * @return array Lines of output
111 */
112 function std($command, $repos, $path, $revision)
113 {
114 global $viewsvn;
115
116 $revision = SVNCommon::rev($revision);
117 $repospath = $viewsvn->repos->fetch_path($repos, false);
118
119 return $this->svn($command . ' ' . $repospath . $path . '@' . $revision);
120 }
121
122 /**
123 * SVN Wrapper: blame
124 *
125 * @access protected
126 *
127 * @param string Repository
128 * @param string Path
129 * @param integer Revision
130 *
131 * @return array Lines of blame output
132 */
133 function blame($repos, $path, $revision)
134 {
135 return $this->std('blame', $repos, $path, $revision);
136 }
137
138 /**
139 * SVN Wrapper: cat
140 *
141 * @access protected
142 *
143 * @param string Repository
144 * @param string Path
145 * @param integer Revision
146 *
147 * @return array Lines of cat output
148 */
149 function cat($repos, $path, $revision)
150 {
151 return $this->std('cat', $repos, $path, $revision);
152 }
153
154 /**
155 * SVN Wrapper: diff
156 *
157 * @access protected
158 *
159 * @param string Repository
160 * @param string Path
161 * @param integer Lower revision
162 * @param integer Higher revision
163 *
164 * @return array Lines of diff output
165 */
166 function diff($repos, $path, $lorev, $hirev)
167 {
168 global $viewsvn;
169
170 $hirev = $this->rev($hirev);
171 $lorev = $this->rev($lorev);
172 if ($lorev == 'HEAD')
173 {
174 $lorev = 1;
175 }
176
177 if (is_integer($hirev) AND is_integer($lorev))
178 {
179 if ($lorev > $hirev)
180 {
181 $lorev = $hirev - 1;
182 }
183 if ($lorev == $hirev)
184 {
185 $lorev = 0;
186 }
187 }
188
189 $repospath = $viewsvn->repos->fetch_path($repos, false);
190
191 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
192 }
193
194 /**
195 * SVN Wrapper: log
196 *
197 * @access protected
198 *
199 * @param string Repository
200 * @param string Path
201 * @param integer Lower revision
202 * @param integer Higher revision
203 *
204 * @return array Lines of log output
205 */
206 function log($repos, $path, $lorev, $hirev)
207 {
208 global $viewsvn;
209
210 $hirev = $this->rev($hirev);
211 $lorev = $this->rev($hirev);
212 if ($lorev == 'HEAD')
213 {
214 $lorev = 0;
215 }
216
217 if (is_integer($hirev) AND is_integer($lorev))
218 {
219 if ($lorev > $hirev)
220 {
221 $lorev = $hirev - 1;
222 }
223 if ($lorev == $hirev)
224 {
225 $lorev = 0;
226 }
227 }
228
229 $repospath = $viewsvn->repos->fetch_path($repos, false);
230
231 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
232 }
233
234 /**
235 * SVN Wrapper: ls (list)
236 *
237 * @access protected
238 *
239 * @param string Repository
240 * @param string Path
241 * @param integer Revision
242 *
243 * @return array Lines of list output
244 */
245 function ls($repos, $path, $revision)
246 {
247 return $this->std('list', $repos, $path, $revision);
248 }
249 }
250
251 /**
252 * Annotation/blame system; constructs an array
253 * that is ready for output
254 *
255 * @package ViewSVN
256 * @version $Id$
257 */
258 class SVNBlame
259 {
260 /**
261 * Array of blame information
262 * @var array
263 */
264 var $blame = array();
265
266 /**
267 * Raw "svn blame" output
268 * @var array
269 */
270 var $rawoutput;
271
272 /**
273 * Constructor: create blame and store data
274 *
275 * @param string Repository
276 * @param string Path
277 * @param integer Revision
278 */
279 function SVNBlame($repos, $path, $revision)
280 {
281 global $viewsvn;
282
283 $this->rawoutput = $viewsvn->svn->blame($repos, $path, $revision);
284 $this->process();
285 }
286
287 /**
288 * Returns blame for display
289 *
290 * @access public
291 *
292 * @return array Blame data
293 */
294 function fetch()
295 {
296 return $this->blame;
297 }
298
299 /**
300 * Parses the blame data
301 *
302 * @access private
303 */
304 function process()
305 {
306 $lineno = 1;
307
308 foreach ($this->rawoutput AS $line)
309 {
310 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
311 {
312 $this->blame[] = array(
313 'rev' => $matches[1],
314 'author' => $matches[2],
315 'line' => $matches[3],
316 'lineno' => $lineno++
317 );
318 }
319 // a blank line
320 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
321 {
322 $this->blame[] = array(
323 'rev' => $matches[1],
324 'author' => $matches[2],
325 'line' => '',
326 'lineno' => $lineno++
327 );
328 }
329 }
330 }
331 }
332
333 /**
334 * Log management system; creates a complex list
335 * of SVN log information
336 *
337 * @package ViewSVN
338 * @version $Id$
339 */
340 class SVNLog
341 {
342 /**
343 * Array of logs
344 * @var array
345 */
346 var $logs = array();
347
348 /**
349 * Raw "svn log" output
350 * @var array
351 */
352 var $rawoutput;
353
354 /**
355 * Constructor: create log store for the given file
356 *
357 * @param string Repository
358 * @param string Path
359 * @param integer Lower revision
360 * @param integer Higher revision
361 */
362 function SVNLog($repos, $path, $lorev, $hirev)
363 {
364 global $viewsvn;
365
366 $this->rawoutput = $viewsvn->svn->log($repos, $path, $lorev, $hirev);
367 $this->process();
368 }
369
370 /**
371 * Returns logs for display
372 *
373 * @access public
374 *
375 * @return array Log data
376 */
377 function fetch()
378 {
379 return $this->logs;
380 }
381
382 /**
383 * Splits up the raw output into a usable log
384 *
385 * @access private
386 */
387 function process()
388 {
389 $lastrev = 0;
390
391 for ($i = 1; $i <= count($this->rawoutput) - 1; $i++)
392 {
393 $line = $this->rawoutput["$i"];
394
395 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
396 {
397 if (isset($this->logs["$lastrev"]))
398 {
399 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
400 }
401
402 $this->logs["$matches[1]"] = array(
403 'rev' => $matches[1],
404 'author' => $matches[2],
405 'date' => $matches[3],
406 'timezone' => $matches[4],
407 'lines' => $matches[6],
408 'message' => ''
409 );
410
411 $lastrev = $matches[1];
412 }
413 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
414 {
415 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
416 {
417 $matches[2] = $amatches[1];
418 }
419
420 $this->logs["$lastrev"]['files'][] = array(
421 'action' => $matches[1],
422 'file' => trim($matches[2]),
423 'from' => (isset($amatches[2]) ? $amatches[2] : '')
424 );
425 }
426 else
427 {
428 if (trim($line) != 'Changed paths:')
429 {
430 $this->logs["$lastrev"]['message'] .= $line . "\n";
431 }
432 }
433 }
434
435 if (isset($this->logs["$lastrev"]))
436 {
437 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
438 }
439 }
440
441 /**
442 * Trims the last dash line off a message
443 *
444 * @access private
445 *
446 * @param string Message with annoying-ass line
447 *
448 * @return string Clean string
449 */
450 function strip_last_line($string)
451 {
452 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
453 }
454 }
455
456 /**
457 * Diff system; constructs a diff array that
458 * is ready for output
459 *
460 * @package ViewSVN
461 */
462 class SVNDiff
463 {
464 /**
465 * Array of diff information
466 * @var array
467 */
468 var $diff = array();
469
470 /**
471 * Raw "svn diff" output
472 * @var array
473 */
474 var $rawoutput;
475
476 /**
477 * Constructor: create and store diff data
478 *
479 * @param string Repository
480 * @param string Path
481 * @param integer Lower revision
482 * @param integer Higher revision
483 */
484 function SVNDiff($repos, $path, $lorev, $hirev)
485 {
486 global $viewsvn;
487
488 $this->rawoutput = $viewsvn->svn->diff($repos, $path, $lorev, $hirev);
489 $this->process();
490 }
491
492 /**
493 * Returns diffs for display
494 *
495 * @access public
496 *
497 * @return array Diff data
498 */
499 function fetch()
500 {
501 return $this->diff;
502 }
503
504 /**
505 * Processes and prepares diff data
506 *
507 * @access private
508 */
509 function process()
510 {
511 global $viewsvn;
512
513 $chunk = 0;
514 $indexcounter = null;
515 $curprop = '';
516
517 $delstack = array();
518
519 foreach ($this->rawoutput AS $line)
520 {
521 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
522 {
523 $property = false;
524 $delstack = array();
525 $this->diff["$index"][ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
526 $lines['old'] = $this->diff["$index"]["$chunk"]['hunk']['old']['line'] - 1;
527 $lines['new'] = $this->diff["$index"]["$chunk"]['hunk']['new']['line'] - 1;
528 continue;
529 }
530 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
531 {
532 $property = true;
533 $index = $bits[1];
534 $this->diff["$index"]['props'] = array();
535 continue;
536 }
537
538 if ($indexcounter <= 3 AND $indexcounter !== null)
539 {
540 $indexcounter++;
541 continue;
542 }
543 else if ($indexcounter == 3)
544 {
545 $indexcounter = null;
546 continue;
547 }
548
549 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
550 {
551 $act = $matches[1];
552 $content = $matches[2];
553
554 if ($act == ' ')
555 {
556 $this->diff["$index"]["$chunk"][] = array(
557 'line' => $content,
558 'act' => '',
559 'oldlineno' => ++$lines['old'],
560 'newlineno' => ++$lines['new']
561 );
562
563 $delstack = array();
564 }
565 else if ($act == '+')
566 {
567 // potential line delta
568 if (count($delstack) > 0)
569 {
570 $lastline = array_shift($delstack);
571
572 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
573 {
574 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
575 {
576 $end = strlen($lastline['line']) + $delta['end'];
577 $viewsvn->debug("RM delta- = " . $end);
578 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
579 $this->diff["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
580 }
581
582 if (strlen($content) > $delta['start'] - $delta['end'])
583 {
584 $end = strlen($content) + $delta['end'];
585 $viewsvn->debug("MK delta+ = " . $end);
586 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
587 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
588 }
589 }
590 }
591
592 $this->diff["$index"]["$chunk"][] = array(
593 'line' => $content,
594 'act' => '+',
595 'oldlineno' => '',
596 'newlineno' => ++$lines['new']
597 );
598 }
599 else if ($act == '-')
600 {
601 $this->diff["$index"]["$chunk"][] = $thearray = array(
602 'line' => $content,
603 'act' => '-',
604 'oldlineno' => ++$lines['old'],
605 'newlineno' => ''
606 );
607
608 $key = count($this->diff["$index"]["$chunk"]) - 2;
609 $thearray['INDEX'] = $key;
610
611 array_push($delstack, $thearray);
612 }
613 }
614 // whitespace lines
615 else
616 {
617 if (preg_match('#^Index: (.*?)$#', $line, $matches))
618 {
619 $index = $matches[1];
620 $indexcounter = 1;
621 $chunk = 0;
622 continue;
623 }
624
625 if ($property)
626 {
627 if (preg_match('#^__*_$#', trim($line)))
628 {
629 $viewsvn->debug("skipping: $line");
630 continue;
631 }
632
633 if (preg_match('#Name: (.*?)$#', $line, $matches))
634 {
635 $curprop = $matches[1];
636 $viewsvn->debug("prop: $curprop");
637 continue;
638 }
639 else
640 {
641 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
642 {
643 $mode = 'add';
644 $this->diff["$index"]['props']["$curprop"]['add'] .= $matches[1];
645 }
646 else if (preg_match('#^\s+?\-(.*)#', $line, $matches))
647 {
648 $mode = 'del';
649 $this->diff["$index"]['props']["$curprop"]['del'] .= $matches[1];
650 }
651 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
652 {
653 $this->diff["$index"]['props']["$curprop"]["$mode"] .= "\n" . $line;
654 }
655 continue;
656 }
657 }
658
659 $this->diff["$index"]["$chunk"][] = array(
660 'line' => '',
661 'act' => '',
662 'oldlineno' => ++$lines['old'],
663 'newlineno' => ++$lines['new']
664 );
665
666 $delstack = array();
667 }
668 }
669 }
670
671 /**
672 * Returns the amount of change that occured
673 * between two lines
674 *
675 * @access private
676 *
677 * @param string Old line
678 * @param string New line
679 *
680 * @return array Difference of positions
681 */
682 function fetch_diff_extent($old, $new)
683 {
684 global $viewsvn;
685
686 $start = 0;
687 $min = min(strlen($old), strlen($new));
688
689 $viewsvn->debug("min1 = $min");
690
691 while ($start < $min AND $old["$start"] == $new["$start"])
692 {
693 $start++;
694 }
695
696 $end = -1;
697 $min = $min - $start;
698
699 $viewsvn->debug("min2 = $min");
700
701 $viewsvn->debug("checking: " . $old[ strlen($old) + $end ] . ' == ' . $new[ strlen($new) + $end ]);
702
703 while (-$end <= $min AND $old[ strlen($old) + $end ] == $new[ strlen($new) + $end ])
704 {
705 $end--;
706 }
707
708 return array('start' => $start, 'end' => $end + 1);
709 }
710 }
711
712 /*=====================================================================*\
713 || ###################################################################
714 || # $HeadURL$
715 || # $Id$
716 || ###################################################################
717 \*=====================================================================*/
718 ?>