]>
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 interface with the SVN commands
29 * Interacts with the command line subsystem to
30 * access SVN information
38 * Path to the SVN binary
51 * Constructor: validate SVN path
53 * @param object Controller
55 function SVNLib (& $controller )
57 $this- > controller
=& $controller ;
59 $this- > svnpath
=& $this- > controller
-> xquery
-> cmd ( $this- > controller
-> registry
-> svnpath
);
61 $access = $this- > controller
-> xquery
-> exec ( $this- > svnpath
. ' --version' );
65 $this- > controller
-> registry
-> trigger
-> error ( $this- > controller
-> registry
-> lang
-> string ( 'The SVN binary could not be located' ));
68 if (! preg_match ( '#^svn, version (.*?)\)$#i' , trim ( $access [ 0 ])))
70 $this- > controller
-> registry
-> trigger
-> error ( $this- > controller
-> registry
-> lang
-> string ( 'The SVN binary does not appear to be valid (it failed our tests)' ));
75 * Executes the SVN binary
79 * @param string Command
81 * @return array Output
83 function svn ( $command )
85 $output = $this- > controller
-> xquery
-> exec ( $this- > svnpath
. ' ' . $command . ' 2>&1' );
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);
92 $temp = implode ( " \n " , $output );
93 if ( strpos ( $temp , '(apr' . '_err=' ) !== false )
95 $this- > controller
-> registry
-> trigger
-> error ( nl2br ( $temp ));
101 * SVN Wrapper: standard command system
105 * @param string SVN command
107 * @return array Lines of output
109 function command ( $command )
113 $revision = SVNCommon
:: rev ( $this- > controller
-> revnum
);
115 return $this- > svn ( $command . ' ' . $this- > controller
-> repospath
. $this- > controller
-> path
. '@' . $revision );
123 * @param string Repository
125 * @param integer Revision
127 * @return array Lines of blame output
129 function blame ( $repos , $path , $revision )
131 return $this- > std ( 'blame' , $repos , $path , $revision );
139 * @param string Repository
141 * @param integer Revision
143 * @return array Lines of cat output
145 function cat ( $repos , $path , $revision )
147 return $this- > std ( 'cat' , $repos , $path , $revision );
155 * @param string Repository
157 * @param integer Lower revision
158 * @param integer Higher revision
160 * @return array Lines of diff output
162 function diff ( $repos , $path , $lorev , $hirev )
166 $hirev = $this- > rev ( $hirev );
167 $lorev = $this- > rev ( $lorev );
168 if ( $lorev == 'HEAD' )
173 if ( is_integer ( $hirev ) AND is_integer ( $lorev ))
179 if ( $lorev == $hirev )
185 $repospath = $viewsvn- > repos
-> fetch_path ( $repos , false );
187 return $this- > svn ( 'diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path );
195 * @param string Repository
197 * @param integer Lower revision
198 * @param integer Higher revision
200 * @return array Lines of log output
202 function log ( $repos , $path , $lorev , $hirev )
206 $hirev = $this- > rev ( $hirev );
207 $lorev = $this- > rev ( $hirev );
208 if ( $lorev == 'HEAD' )
213 if ( is_integer ( $hirev ) AND is_integer ( $lorev ))
219 if ( $lorev == $hirev )
225 $repospath = $viewsvn- > repos
-> fetch_path ( $repos , false );
227 return $this- > svn ( 'log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path );
231 * SVN Wrapper: ls (list)
235 * @param object Controller object
237 * @return array Lines of list output
239 function ls (& $controller )
241 return $this- > std ( 'list' );
246 * Annotation/blame system; constructs an array
247 * that is ready for output
255 * Array of blame information
258 var $blame = array ();
261 * Raw "svn blame" output
267 * Constructor: create blame and store data
269 * @param string Repository
271 * @param integer Revision
273 function SVNBlame ( $repos , $path , $revision )
277 $this- > rawoutput
= $viewsvn- > svn
-> blame ( $repos , $path , $revision );
282 * Returns blame for display
286 * @return array Blame data
294 * Parses the blame data
302 foreach ( $this- > rawoutput
AS $line )
304 if ( preg_match ( '#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#' , $line , $matches ))
306 $this- > blame
[] = array (
307 'rev' => $matches [ 1 ],
308 'author' => $matches [ 2 ],
309 'line' => $matches [ 3 ],
310 'lineno' => $lineno ++
314 else if ( preg_match ( '#^\s+([0-9]+)\s+([\w\.\-_]+)$#' , $line , $matches ))
316 $this- > blame
[] = array (
317 'rev' => $matches [ 1 ],
318 'author' => $matches [ 2 ],
320 'lineno' => $lineno ++
328 * Log management system; creates a complex list
329 * of SVN log information
343 * Raw "svn log" output
349 * Constructor: create log store for the given file
351 * @param string Repository
353 * @param integer Lower revision
354 * @param integer Higher revision
356 function SVNLog ( $repos , $path , $lorev , $hirev )
360 $this- > rawoutput
= $viewsvn- > svn
-> log ( $repos , $path , $lorev , $hirev );
365 * Returns logs for display
369 * @return array Log data
377 * Splits up the raw output into a usable log
385 for ( $i = 1 ; $i <= count ( $this- > rawoutput
) - 1 ; $i ++
)
387 $line = $this- > rawoutput
[ " $i" ];
389 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line , $matches ))
391 if (isset( $this- >logs[" $lastrev" ]))
393 $this- > logs
[ " $lastrev" ]['message'] = $this- >strip_last_line( $this- >logs[" $lastrev" ][ 'message' ]);
396 $this- > logs
[ " $matches [1]" ] = array (
397 'rev' => $matches [ 1 ],
398 'author' => $matches [ 2 ],
399 'date' => $matches [ 3 ],
400 'timezone' => $matches [ 4 ],
401 'lines' => $matches [ 6 ],
405 $lastrev = $matches [ 1 ];
407 else if ( preg_match ( '#^\s+([ADMR])\s(.*)#' , $line , $matches ))
409 if ( preg_match ( '#(.*) \(from (.*?)\)$#' , $matches [ 2 ], $amatches ))
411 $matches [ 2 ] = $amatches [ 1 ];
414 $this- > logs
[ " $lastrev" ]['files'][] = array(
415 'action' => $matches [1],
416 'file' => trim( $matches [2]),
417 'from' => (isset( $amatches [2]) ? $amatches [2] : '')
422 if (trim( $line ) != 'Changed paths:')
424 $this- >logs[" $lastrev" ][ 'message' ] .= $line . " \n " ;
429 if ( isset ( $this- > logs
[ " $lastrev" ]))
431 $this- >logs[" $lastrev" ][ 'message' ] = $this- > strip_last_line ( $this- > logs
[ " $lastrev" ]['message']);
436 * Trims the last dash line off a message
440 * @param string Message with annoying-ass line
442 * @return string Clean string
444 function strip_last_line( $string )
446 return trim(preg_replace(" #\n(.*?)\n$#", '', $string));
451 * Diff system; constructs a diff array that
452 * is ready for output
459 * Array of diff information
465 * Raw "svn diff" output
471 * Constructor: create and store diff data
473 * @param string Repository
475 * @param integer Lower revision
476 * @param integer Higher revision
478 function SVNDiff ( $repos , $path , $lorev , $hirev )
482 $this- > rawoutput
= $viewsvn- > svn
-> diff ( $repos , $path , $lorev , $hirev );
487 * Returns diffs for display
491 * @return array Diff data
499 * Processes and prepares diff data
508 $indexcounter = null ;
513 foreach ( $this- > rawoutput
AS $line )
515 if ( preg_match ( '#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#' , $line , $bits ))
519 $this- > diff
[ " $index" ][ ++ $chunk ]['hunk'] = array('old' => array('line' => $bits [1], 'count' => $bits [2]), 'new' => array('line' => $bits [3], 'count' => $bits [4]));
520 $lines ['old'] = $this- >diff[" $index" ][ " $chunk" ]['hunk']['old']['line'] - 1;
521 $lines ['new'] = $this- >diff[" $index" ][ " $chunk" ]['hunk']['new']['line'] - 1;
524 else if (preg_match('#^Property changes on: (.*?)$#', $line , $bits ))
528 $this- >diff[" $index" ][ 'props' ] = array ();
532 if ( $indexcounter <= 3 AND $indexcounter !== null )
537 else if ( $indexcounter == 3 )
539 $indexcounter = null ;
543 if ( preg_match ( '#^([\+\- ])(.*)#' , $line , $matches ) AND ! $property )
546 $content = $matches [ 2 ];
550 $this- > diff
[ " $index" ][" $chunk" ][] = array (
553 'oldlineno' => ++
$lines [ 'old' ],
554 'newlineno' => ++
$lines [ 'new' ]
559 else if ( $act == '+' )
561 // potential line delta
562 if ( count ( $delstack ) > 0 )
564 $lastline = array_shift ( $delstack );
566 if ( $delta = @ $this- > fetch_diff_extent ( $lastline [ 'line' ], $content ))
568 if ( strlen ( $lastline [ 'line' ]) > ( $delta [ 'start' ] - $delta [ 'end' ]))
570 $end = strlen ( $lastline [ 'line' ]) +
$delta [ 'end' ];
571 $viewsvn- > debug ( "RM delta- = " . $end );
572 $change = ' {@-' . '-} ' . substr ( $lastline [ 'line' ], $delta [ 'start' ], $end - $delta [ 'start' ]) . ' {/@-' . '-} ' ;
573 $this- > diff
[ " $index" ][" $chunk" ][ " $lastline [INDEX]" ][ 'line' ] = substr ( $lastline [ 'line' ], 0 , $delta [ 'start' ]) . $change . substr ( $lastline [ 'line' ], $end );
576 if ( strlen ( $content ) > $delta [ 'start' ] - $delta [ 'end' ])
578 $end = strlen ( $content ) +
$delta [ 'end' ];
579 $viewsvn- > debug ( "MK delta+ = " . $end );
580 $change = ' {@+' . '+} ' . substr ( $content , $delta [ 'start' ], $end - $delta [ 'start' ]) . ' {/@+' . '+} ' ;
581 $content = substr ( $content , 0 , $delta [ 'start' ]) . $change . substr ( $content , $end );
586 $this- > diff
[ " $index" ][" $chunk" ][] = array (
590 'newlineno' => ++
$lines [ 'new' ]
593 else if ( $act == '-' )
595 $this- > diff
[ " $index" ][" $chunk" ][] = $thearray = array (
598 'oldlineno' => ++
$lines [ 'old' ],
602 $key = count ( $this- > diff
[ " $index" ][" $chunk" ]) - 2 ;
603 $thearray [ 'INDEX' ] = $key ;
605 array_push ( $delstack , $thearray );
611 if ( preg_match ( '#^Index: (.*?)$#' , $line , $matches ))
613 $index = $matches [ 1 ];
621 if ( preg_match ( '#^__*_$#' , trim ( $line )))
623 $viewsvn- > debug ( "skipping: $line" );
627 if (preg_match('#Name: (.*?)$#', $line , $matches ))
629 $curprop = $matches [1];
630 $viewsvn- >debug(" prop
: $curprop" );
635 if ( preg_match ( '#^\s+?\+(.*)#' , $line , $matches ))
638 $this- > diff
[ " $index" ]['props'][" $curprop" ][ 'add' ] .= $matches [ 1 ];
640 else if ( preg_match ( '#^\s+?\-(.*)#' , $line , $matches ))
643 $this- > diff
[ " $index" ]['props'][" $curprop" ][ 'del' ] .= $matches [ 1 ];
645 else if (! preg_match ( '#^\s+[\+\- ](.*)#' , $line ) AND trim ( $line ) != '' )
647 $this- > diff
[ " $index" ]['props'][" $curprop" ][ " $mode" ] .= " \n
" . $line ;
653 $this- >diff[" $index" ][ " $chunk" ][] = array(
656 'oldlineno' => ++ $lines ['old'],
657 'newlineno' => ++ $lines ['new']
666 * Returns the amount of change that occured
671 * @param string Old line
672 * @param string New line
674 * @return array Difference of positions
676 function fetch_diff_extent( $old , $new )
681 $min = min(strlen( $old ), strlen( $new ));
683 $viewsvn- >debug(" min1
= $min" );
685 while ( $start < $min AND $old [ " $start" ] == $new [" $start" ])
691 $min = $min - $start ;
693 $viewsvn- > debug ( "min2 = $min" );
695 $viewsvn- >debug(" checking
: " . $old [ strlen( $old ) + $end ] . ' == ' . $new [ strlen( $new ) + $end ]);
697 while (- $end <= $min AND $old [ strlen( $old ) + $end ] == $new [ strlen( $new ) + $end ])
702 return array('start' => $start , 'end' => $end + 1);
706 /*=====================================================================*\
707 || ###################################################################
710 || ###################################################################
711 \*=====================================================================*/