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