Basic diff stuff is kind of working
[viewsvn.git] / includes / svnlib.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # ViewSVN [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * Command line interface with the SVN commands
15 *
16 * @package ViewSVN
17 */
18
19 /**
20 * Interacts with the command line subsystem to
21 * access SVN information
22 *
23 * @package ViewSVN
24 * @version $Id$
25 */
26 class SVNLib
27 {
28 /**
29 * Path to the SVN binary
30 * @var string
31 */
32 var $svnpath;
33
34 /**
35 * Constructor: validate SVN path
36 *
37 * @param string Path to SVN binary
38 */
39 function SVNLib($svnpath)
40 {
41 global $viewsvn;
42
43 $this->svnpath = $viewsvn->shell->cmd($svnpath);
44
45 $access = $viewsvn->shell->exec($this->svnpath . ' --version');
46
47 if (!$access)
48 {
49 $viewsvn->trigger->error('svn binary could not be found');
50 }
51
52 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
53 {
54 $viewsvn->trigger->error('svn binary does not pass test');
55 }
56 }
57
58 /**
59 * Prepares data for output
60 *
61 * @access public
62 *
63 * @param string Standard data
64 *
65 * @return string Output-ready data
66 */
67 function format($string)
68 {
69 // convert entities
70 $string = htmlspecialchars($string);
71
72 // tabs to 5 spaces
73 $string = str_replace("\t", ' ', $string);
74
75 // spaces to nbsp
76 $string = str_replace(' ', '&nbsp;', $string);
77
78 // nl2br
79 $string = nl2br($string);
80
81 return $string;
82 }
83
84 /**
85 * Executes the SVN binary
86 *
87 * @access private
88 *
89 * @param string Command
90 *
91 * @return array Output
92 */
93 function svn($command)
94 {
95 global $viewsvn;
96
97 return $viewsvn->shell->exec($this->svnpath . ' ' . $command . ' 2>&1');
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 = $this->rev($revision);
117 $repospath = $viewsvn->repos->fetch_path($repos, false);
118
119 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
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 global $viewsvn;
152
153 $repospath = $viewsvn->repos->fetch_path($repos, false);
154
155 return $this->svn('cat ' . $repospath . $path . '@' . $revision);
156 }
157
158 /**
159 * SVN Wrapper: diff
160 *
161 * @access protected
162 *
163 * @param string Repository
164 * @param string Path
165 * @param integer Lower revision
166 * @param integer Higher revision
167 *
168 * @return array Lines of diff output
169 */
170 function diff($repos, $path, $lorev, $hirev)
171 {
172 global $viewsvn;
173
174 $hirev = $this->rev($hirev);
175 $lorev = $this->rev($lorev);
176 if ($lorev == 'HEAD')
177 {
178 $lorev = 0;
179 }
180
181 if (is_integer($hirev) AND is_integer($lorev))
182 {
183 if ($lorev > $hirev)
184 {
185 $lorev = $hirev - 1;
186 }
187 if ($lorev == $hirev)
188 {
189 $lorev = 0;
190 }
191 }
192
193 $repospath = $viewsvn->repos->fetch_path($repos, false);
194
195 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
196 }
197
198 /**
199 * SVN Wrapper: log
200 *
201 * @access protected
202 *
203 * @param string Repository
204 * @param string Path
205 * @param integer Lower revision
206 * @param integer Higher revision
207 *
208 * @return array Lines of log output
209 */
210 function log($repos, $path, $lorev, $hirev)
211 {
212 global $viewsvn;
213
214 $hirev = $this->rev($hirev);
215 $lorev = $this->rev($hirev);
216 if ($lorev == 'HEAD')
217 {
218 $lorev = 0;
219 }
220
221 if (is_integer($hirev) AND is_integer($lorev))
222 {
223 if ($lorev > $hirev)
224 {
225 $lorev = $hirev - 1;
226 }
227 if ($lorev == $hirev)
228 {
229 $lorev = 0;
230 }
231 }
232
233 $repospath = $viewsvn->repos->fetch_path($repos, false);
234
235 return $this->svn('log -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
236 }
237
238 /**
239 * SVN Wrapper: ls (list)
240 *
241 * @access protected
242 *
243 * @param string Repository
244 * @param string Path
245 * @param integer Revision
246 *
247 * @return array Lines of list output
248 */
249 function ls($repos, $path, $revision)
250 {
251 return $this->std('list', $repos, $path, $revision);
252 }
253
254 /**
255 * Generates a clean revision number
256 *
257 * @access public
258 *
259 * @param integer Revision number
260 *
261 * @return mixed Cleaned revision or HEAD
262 */
263 function rev($revision)
264 {
265 if (($revision = intval($revision)) < 1)
266 {
267 $revision = 'HEAD';
268 }
269 return $revision;
270 }
271 }
272
273 /**
274 * Annotation/blame system; constructs an array
275 * that is ready for output
276 *
277 * @package ViewSVN
278 * @version $Id$
279 */
280 class SVNBlame
281 {
282 /**
283 * Array of blame information
284 * @var array
285 */
286 var $blame = array();
287
288 /**
289 * Raw "svn blame" output
290 * @var array
291 */
292 var $rawoutput;
293
294 /**
295 * Constructor: create blame and store data
296 *
297 * @param string Repository
298 * @param string Path
299 * @param integer Revision
300 */
301 function SVNBlame($repos, $path, $revision)
302 {
303 global $viewsvn;
304
305 $this->rawoutput = $viewsvn->svn->blame($repos, $path, $revision);
306 $this->process();
307 }
308
309 /**
310 * Returns blame for display
311 *
312 * @access public
313 *
314 * @return array Blame data
315 */
316 function fetch()
317 {
318 return $this->blame;
319 }
320
321 /**
322 * Parses the blame data
323 *
324 * @access private
325 */
326 function process()
327 {
328 $lineno = 1;
329
330 foreach ($this->rawoutput AS $line)
331 {
332 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
333 {
334 $this->blame[] = array(
335 'rev' => $matches[1],
336 'author' => $matches[2],
337 'line' => $matches[3],
338 'lineno' => $lineno++
339 );
340 }
341 // a blank line
342 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
343 {
344 $this->blame[] = array(
345 'rev' => $matches[1],
346 'author' => $matches[2],
347 'line' => '',
348 'lineno' => $lineno++
349 );
350 }
351 }
352 }
353 }
354
355 /**
356 * Log management system; creates a complex list
357 * of SVN log information
358 *
359 * @package ViewSVN
360 * @version $Id$
361 */
362 class SVNLog
363 {
364 /**
365 * Array of logs
366 * @var array
367 */
368 var $logs = array();
369
370 /**
371 * Raw "svn log" output
372 * @var array
373 */
374 var $rawoutput;
375
376 /**
377 * Constructor: create log store for the given file
378 *
379 * @param string Repository
380 * @param string Path
381 * @param integer Lower revision
382 * @param integer Higher revision
383 */
384 function SVNLog($repos, $path, $lorev, $hirev)
385 {
386 global $viewsvn;
387
388 $this->rawoutput = $viewsvn->svn->log($repos, $path, $lorev, $hirev);
389 $this->process();
390 }
391
392 /**
393 * Returns logs for display
394 *
395 * @access public
396 *
397 * @return array Log data
398 */
399 function fetch()
400 {
401 return $this->logs;
402 }
403
404 /**
405 * Splits up the raw output into a usable log
406 *
407 * @access private
408 */
409 function process()
410 {
411 $lastrev = 0;
412
413 for ($i = 1; $i <= count($this->rawoutput) - 1; $i++)
414 {
415 $line = $this->rawoutput["$i"];
416
417 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
418 {
419 if (isset($this->logs["$lastrev"]))
420 {
421 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
422 }
423
424 $this->logs["$matches[1]"] = array(
425 'rev' => $matches[1],
426 'author' => $matches[2],
427 'date' => $matches[3],
428 'timezone' => $matches[4],
429 'lines' => $matches[6],
430 'message' => ''
431 );
432
433 $lastrev = $matches[1];
434 }
435 else
436 {
437 $this->logs["$lastrev"]['message'] .= $line . "\n";
438 }
439 }
440
441 if (isset($this->logs["$lastrev"]))
442 {
443 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
444 }
445 }
446
447 /**
448 * Trims the last dash line off a message
449 *
450 * @access private
451 *
452 * @param string Message with annoying-ass line
453 *
454 * @return string Clean string
455 */
456 function strip_last_line($string)
457 {
458 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
459 }
460 }
461
462 /**
463 * Diff system; constructs a diff array that
464 * is ready for output
465 *
466 * @package ViewSVN
467 */
468 class SVNDiff
469 {
470 /**
471 * Array of diff information
472 * @var array
473 */
474 var $diff = array();
475
476 /**
477 * Raw "svn diff" output
478 * @var array
479 */
480 var $rawoutput;
481
482 /**
483 * Constructor: create and store diff data
484 *
485 * @param string Repository
486 * @param string Path
487 * @param integer Lower revision
488 * @param integer Higher revision
489 */
490 function SVNDiff($repos, $path, $lorev, $hirev)
491 {
492 global $viewsvn;
493
494 $this->rawoutput = $viewsvn->svn->diff($repos, $path, $lorev, $hirev);
495 $this->process();
496 }
497
498 /**
499 * Returns diffs for display
500 *
501 * @access public
502 *
503 * @return array Diff data
504 */
505 function fetch()
506 {
507 return $this->diff;
508 }
509
510 /**
511 * Processes and prepares diff data
512 *
513 * @access private
514 */
515 function process()
516 {
517 $chunk = 0;
518 $capture = false;
519
520 foreach ($this->rawoutput AS $line)
521 {
522 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
523 {
524 $capture = true;
525 $this->diff[ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
526 $lines['old'] = $this->diff["$chunk"]['hunk']['old']['line'] - 1;
527 $lines['new'] = $this->diff["$chunk"]['hunk']['new']['line'] - 1;
528 continue;
529 }
530
531 // ensures we don't capture before the first hunk
532 if (!$capture)
533 {
534 continue;
535 }
536
537 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
538 {
539 $act = $matches[1];
540 $content = $matches[2];
541
542 if ($act == ' ')
543 {
544 $this->diff["$chunk"][] = array(
545 'line' => $content,
546 'act' => '',
547 'oldlineno' => ++$lines['old'],
548 'newlineno' => ++$lines['new']
549 );
550 }
551 else if ($act == '+')
552 {
553 $this->diff["$chunk"][] = array(
554 'line' => $content,
555 'act' => '+',
556 'oldlineno' => '',
557 'newlineno' => ++$lines['new']
558 );
559 }
560 else if ($act == '-')
561 {
562 $this->diff["$chunk"][] = array(
563 'line' => $content,
564 'act' => '-',
565 'oldlineno' => ++$lines['old'],
566 'newlineno' => ''
567 );
568 }
569 }
570 }
571
572 }
573 }
574
575 /*=====================================================================*\
576 || ###################################################################
577 || # $HeadURL$
578 || # $Id$
579 || ###################################################################
580 \*=====================================================================*/
581 ?>