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