]>
src.bluestatic.org Git - viewsvn.git/blob - includes/svnlib.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
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
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 \*=====================================================================*/
23 * Command line library and SVN-PHP bridge extension
29 * This class acts as the bridge between the command line Xquery class
30 * and the various display files. It handles preparation of Xquery
39 * Path to the SVN binary
52 // ###################################################################
54 * Constructor: validate SVN path
56 * @param object Controller
58 function SVNLib (& $controller )
60 $this- > controller
=& $controller ;
62 $this- > svnpath
=& $this- > controller
-> xquery
-> cmd ( $this- > controller
-> registry
-> svnpath
);
64 $access = $this- > controller
-> xquery
-> exec ( $this- > svnpath
. ' --version' );
68 $this- > controller
-> registry
-> trigger
-> error ( $this- > controller
-> registry
-> lang
-> string ( 'The SVN binary could not be located' ));
71 if (! preg_match ( '#^svn, version (.*?)\)$#i' , trim ( $access [ 0 ])))
73 $this- > controller
-> registry
-> trigger
-> error ( $this- > controller
-> registry
-> lang
-> string ( 'The SVN binary does not appear to be valid (it failed our tests)' ));
77 // ###################################################################
79 * Executes the SVN binary
83 * @param string Command
85 * @return array Output
87 function svn ( $command )
89 $output = $this- > controller
-> xquery
-> exec ( $this- > svnpath
. ' ' . $command . ' 2>&1' );
91 // make sure that we keep escaped chars
92 //$output = str_replace(array('\t', '\n', '\r'), array('\\\t', '\\\n', '\\\r'), $output);
93 //$output = preg_replace('#\\\(.)#', '\\\\\\\\' . '\1', $output);
94 //$output = str_replace('\\', '\\\\', $output);
96 $temp = implode ( " \n " , $output );
97 if ( strpos ( $temp , '(apr' . '_err=' ) !== false )
99 $this- > controller
-> registry
-> trigger
-> error ( nl2br ( $temp ));
104 // ###################################################################
106 * This function is used to prepare a common command. It sends the
107 * specified command along with the repository path and the current
108 * node. The revision is either fectched from the argument list or
109 * from the node, too.
113 * @param string SVN command
114 * @param bool Alternate revision
116 * @return array Lines of output
118 function command ( $command , $rev = false )
122 $revision = ( $rev !== false ? $rev : SVNCommon
:: rev ( $this- > controller
-> revnum
));
124 return $this- > svn ( $command . ' ' . $this- > controller
-> repospath
. $this- > controller
-> path
. ( $revision !== null ? '@' . $revision : '' ));
127 // ###################################################################
129 * A library complicator function that handles the diff command because
130 * the standard command() system does not work with more complex args.
134 * @param integer Lower revision
135 * @param integer Higher revision
137 * @return array Lines of diff output
139 function diff ( $lorev , $hirev )
143 $hirev = SVNCommon
:: rev ( $hirev );
144 $lorev = SVNCommon
:: rev ( $lorev );
145 if ( $lorev == 'HEAD' )
150 if ( is_integer ( $hirev ) AND is_integer ( $lorev ))
156 if ( $lorev == $hirev )
162 return $this- > svn ( 'diff -r' . $lorev . ':' . $hirev . ' ' . $this- > controller
-> repospath
. $this- > controller
-> path
);
165 // ###################################################################
167 * A library complicator function to create log output. This is needed
168 * because command() doesn't handle ranged revisions.
172 * @param string Repository
174 * @param integer Lower revision
175 * @param integer Higher revision
177 * @return array Lines of log output
179 function log ( $lorev , $hirev )
183 $hirev = $this- > rev ( $hirev );
184 $lorev = $this- > rev ( $hirev );
185 if ( $lorev == 'HEAD' )
190 if ( is_integer ( $hirev ) AND is_integer ( $lorev ))
196 if ( $lorev == $hirev )
202 $repospath = $viewsvn- > repos
-> fetch_path ( $repos , false );
204 return $this- > svn ( 'log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path );
209 * Annotation/blame system; constructs an array that is ready for output
217 * Array of blame information
221 var $blame = array ();
224 * Raw "svn blame" output
230 // ###################################################################
232 * Constructor: create blame and store data
234 * @param object Controller
236 function SVNBlame (& $controller )
238 $this- > rawoutput
= $controller- > library
-> command ( 'blame' );
242 // ###################################################################
244 * Returns blame for display
248 * @return array Blame data
255 // ###################################################################
257 * Parses the blame data
265 foreach ( $this- > rawoutput
AS $line )
267 if ( preg_match ( '#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#' , $line , $matches ))
269 $this- > blame
[] = array (
270 'rev' => $matches [ 1 ],
271 'author' => $matches [ 2 ],
272 'line' => $matches [ 3 ],
273 'lineno' => $lineno ++
277 else if ( preg_match ( '#^\s+([0-9]+)\s+([\w\.\-_]+)$#' , $line , $matches ))
279 $this- > blame
[] = array (
280 'rev' => $matches [ 1 ],
281 'author' => $matches [ 2 ],
283 'lineno' => $lineno ++
291 * Log management system; creates a complex list of SVN log information
306 * Raw "svn log" output
312 // ###################################################################
314 * Constructor: create log store for the given file
316 * @param object Controller
318 function SVNLog (& $controller )
320 $this- > rawoutput
= $controller- > library
-> command ( 'log' , null );
324 // ###################################################################
326 * Returns logs for display
330 * @return array Log data
337 // ###################################################################
339 * Splits up the raw output into a usable log
347 for ( $i = 1 ; $i <= sizeof ( $this- > rawoutput
) - 1 ; $i ++
)
349 $line = $this- > rawoutput
[ " $i" ];
351 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line , $matches ))
353 if (isset( $this- >logs[" $lastrev" ]))
355 $this- > logs
[ " $lastrev" ]['message'] = $this- >strip_last_line( $this- >logs[" $lastrev" ][ 'message' ]);
358 $this- > logs
[ " $matches [1]" ] = array (
359 'rev' => $matches [ 1 ],
360 'author' => $matches [ 2 ],
361 'date' => $matches [ 3 ],
362 'timezone' => $matches [ 4 ],
363 'lines' => $matches [ 6 ],
367 $lastrev = $matches [ 1 ];
369 else if ( preg_match ( '#^\s+([ADMR])\s(.*)#' , $line , $matches ))
371 if ( preg_match ( '#(.*) \(from (.*?)\)$#' , $matches [ 2 ], $amatches ))
373 $matches [ 2 ] = $amatches [ 1 ];
376 $this- > logs
[ " $lastrev" ]['files'][] = array(
377 'action' => $matches [1],
378 'file' => trim( $matches [2]),
379 'from' => (isset( $amatches [2]) ? $amatches [2] : '')
384 if (trim( $line ) != 'Changed paths:')
386 $this- >logs[" $lastrev" ][ 'message' ] .= $line . " \n " ;
391 if ( isset ( $this- > logs
[ " $lastrev" ]))
393 $this- >logs[" $lastrev" ][ 'message' ] = $this- > strip_last_line ( $this- > logs
[ " $lastrev" ]['message']);
397 // ###################################################################
399 * Trims the last dash line off a message
403 * @param string Message with annoying-ass line
405 * @return string Clean string
407 function strip_last_line( $string )
409 return trim(preg_replace(" #\n(.*?)\n$#", '', $string));
414 * Diff system; constructs a diff array that is ready for output
422 * Array of diff information
429 * Raw "svn diff" output
435 // ###################################################################
437 * Constructor: create and store diff data
439 * @param object Controller
440 * @param integer Lower revision
441 * @param integer Higher revision
443 function SVNDiff (& $controller , $lorev , $hirev )
445 $this- > rawoutput
= $controller- > library
-> diff ( $lorev , $hirev );
449 // ###################################################################
451 * Returns diffs for display
455 * @return array Diff data
462 // ###################################################################
464 * Processes and prepares diff data
473 $indexcounter = null ;
478 foreach ( $this- > rawoutput
AS $line )
480 if ( preg_match ( '#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#' , $line , $bits ))
484 $this- > diff
[ " $index" ][ ++ $chunk ]['hunk'] = array('old' => array('line' => $bits [1], 'count' => $bits [2]), 'new' => array('line' => $bits [3], 'count' => $bits [4]));
485 $lines ['old'] = $this- >diff[" $index" ][ " $chunk" ]['hunk']['old']['line'] - 1;
486 $lines ['new'] = $this- >diff[" $index" ][ " $chunk" ]['hunk']['new']['line'] - 1;
489 else if (preg_match('#^Property changes on: (.*?)$#', $line , $bits ))
493 $this- >diff[" $index" ][ 'props' ] = array ();
497 if ( $indexcounter <= 3 AND $indexcounter !== null )
502 else if ( $indexcounter == 3 )
504 $indexcounter = null ;
508 if ( preg_match ( '#^([\+\- ])(.*)#' , $line , $matches ) AND ! $property )
511 $content = $matches [ 2 ];
515 $this- > diff
[ " $index" ][" $chunk" ][] = array (
518 'oldlineno' => ++
$lines [ 'old' ],
519 'newlineno' => ++
$lines [ 'new' ]
524 else if ( $act == '+' )
526 // potential line delta
527 if ( sizeof ( $delstack ) > 0 )
529 $lastline = array_shift ( $delstack );
531 if ( $delta = @ $this- > fetch_diff_extent ( $lastline [ 'line' ], $content ))
533 if ( strlen ( $lastline [ 'line' ]) > ( $delta [ 'start' ] - $delta [ 'end' ]))
535 $end = strlen ( $lastline [ 'line' ]) +
$delta [ 'end' ];
536 $viewsvn- > debug ( "RM delta- = " . $end );
537 $change = ' {@-' . '-} ' . substr ( $lastline [ 'line' ], $delta [ 'start' ], $end - $delta [ 'start' ]) . ' {/@-' . '-} ' ;
538 $this- > diff
[ " $index" ][" $chunk" ][ " $lastline [INDEX]" ][ 'line' ] = substr ( $lastline [ 'line' ], 0 , $delta [ 'start' ]) . $change . substr ( $lastline [ 'line' ], $end );
541 if ( strlen ( $content ) > $delta [ 'start' ] - $delta [ 'end' ])
543 $end = strlen ( $content ) +
$delta [ 'end' ];
544 $viewsvn- > debug ( "MK delta+ = " . $end );
545 $change = ' {@+' . '+} ' . substr ( $content , $delta [ 'start' ], $end - $delta [ 'start' ]) . ' {/@+' . '+} ' ;
546 $content = substr ( $content , 0 , $delta [ 'start' ]) . $change . substr ( $content , $end );
551 $this- > diff
[ " $index" ][" $chunk" ][] = array (
555 'newlineno' => ++
$lines [ 'new' ]
558 else if ( $act == '-' )
560 $this- > diff
[ " $index" ][" $chunk" ][] = $thearray = array (
563 'oldlineno' => ++
$lines [ 'old' ],
567 $key = sizeof ( $this- > diff
[ " $index" ][" $chunk" ]) - 2 ;
568 $thearray [ 'INDEX' ] = $key ;
570 array_push ( $delstack , $thearray );
576 if ( preg_match ( '#^Index: (.*?)$#' , $line , $matches ))
578 $index = $matches [ 1 ];
586 if ( preg_match ( '#^__*_$#' , trim ( $line )))
588 $viewsvn- > debug ( "skipping: $line" );
592 if (preg_match('#Name: (.*?)$#', $line , $matches ))
594 $curprop = $matches [1];
595 $viewsvn- >debug(" prop
: $curprop" );
600 if ( preg_match ( '#^\s+?\+(.*)#' , $line , $matches ))
603 $this- > diff
[ " $index" ]['props'][" $curprop" ][ 'add' ] .= $matches [ 1 ];
605 else if ( preg_match ( '#^\s+?\-(.*)#' , $line , $matches ))
608 $this- > diff
[ " $index" ]['props'][" $curprop" ][ 'del' ] .= $matches [ 1 ];
610 else if (! preg_match ( '#^\s+[\+\- ](.*)#' , $line ) AND trim ( $line ) != '' )
612 $this- > diff
[ " $index" ]['props'][" $curprop" ][ " $mode" ] .= " \n
" . $line ;
618 $this- >diff[" $index" ][ " $chunk" ][] = array(
621 'oldlineno' => ++ $lines ['old'],
622 'newlineno' => ++ $lines ['new']
630 // ###################################################################
632 * Returns the amount of change that occured between two lines
636 * @param string Old line
637 * @param string New line
639 * @return array Difference of positions
641 function fetch_diff_extent( $old , $new )
646 $min = min(strlen( $old ), strlen( $new ));
648 $viewsvn- >debug(" min1
= $min" );
650 while ( $start < $min AND $old [ " $start" ] == $new [" $start" ])
656 $min = $min - $start ;
658 $viewsvn- > debug ( "min2 = $min" );
660 $viewsvn- >debug(" checking
: " . $old [ strlen( $old ) + $end ] . ' == ' . $new [ strlen( $new ) + $end ]);
662 while (- $end <= $min AND $old [ strlen( $old ) + $end ] == $new [ strlen( $new ) + $end ])
667 return array('start' => $start , 'end' => $end + 1);
671 /*=====================================================================*\
672 || ###################################################################
675 || ###################################################################
676 \*=====================================================================*/