Checking in experimental diff system that's been rewritten
[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 * Common command system
45 * @var object
46 */
47 var $common;
48
49 /**
50 * Constructor: validate SVN path
51 *
52 * @param string Path to SVN binary
53 */
54 function SVNLib($svnpath)
55 {
56 global $viewsvn;
57
58 $this->svnpath = $viewsvn->shell->cmd($svnpath);
59
60 $this->common =& new SVNCommon();
61
62 $access = $viewsvn->shell->exec($this->svnpath . ' --version');
63
64 if (!$access)
65 {
66 $viewsvn->trigger->error($viewsvn->lang->string('The SVN binary could not be located'));
67 }
68
69 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
70 {
71 $viewsvn->trigger->error($viewsvn->lang->string('The SVN binary does not appear to be valid (it failed our tests)'));
72 }
73 }
74
75 /**
76 * Prepares data for output
77 *
78 * @access public
79 *
80 * @param string Standard data
81 *
82 * @return string Output-ready data
83 */
84 function format($string)
85 {
86 // convert entities
87 $string = htmlspecialchars($string);
88
89 // tabs to 5 spaces
90 $string = str_replace("\t", ' ', $string);
91
92 // spaces to nbsp
93 if (true)
94 {
95 $string = preg_replace('#( +)#e', '$this->format_spaces("\1")', $string);
96 }
97 // no word wrap
98 else
99 {
100 $string = str_replace(' ', '&nbsp;', $string);
101 }
102
103 // convert advanced diff
104 $string = str_replace(array('{@+' . '+}', '{@-' . '-}'), array('<span class="diff_add_bit">', '<span class="diff_del_bit">'), $string);
105 $string = str_replace(array('{/@+' . '+}', '{/@-' . '-}'), '</span>', $string);
106
107 // nl2br
108 $string = nl2br($string);
109
110 return $string;
111 }
112
113 /**
114 * Counts the spaces and replaces two or more ones
115 *
116 * @access private
117 *
118 * @param string Spaced string
119 *
120 * @return string &nbsp;'d string
121 */
122 function format_spaces($thestring)
123 {
124 if (strlen($thestring) >= 2)
125 {
126 $thestring = str_replace(' ', '&nbsp;', $thestring);
127 }
128
129 return $thestring;
130 }
131
132 /**
133 * Executes the SVN binary
134 *
135 * @access private
136 *
137 * @param string Command
138 *
139 * @return array Output
140 */
141 function svn($command)
142 {
143 global $viewsvn;
144
145 $output = $viewsvn->shell->exec($this->svnpath . ' ' . $command . ' 2>&1');
146
147 // make sure that we keep escaped chars
148 //$output = str_replace(array('\t', '\n', '\r'), array('\\\t', '\\\n', '\\\r'), $output);
149 //$output = preg_replace('#\\\(.)#', '\\\\\\\\' . '\1', $output);
150 //$output = str_replace('\\', '\\\\', $output);
151
152 $temp = implode("\n", $output);
153 if (strpos($temp, '(apr' . '_err=') !== false)
154 {
155 $viewsvn->trigger->error(nl2br($temp));
156 }
157 return $output;
158 }
159
160 /**
161 * SVN Wrapper: standard command system
162 *
163 * @access private
164 *
165 * @param string SVN command
166 * @param string Repository
167 * @param string Path
168 * @param integer Revision
169 *
170 * @return array Lines of output
171 */
172 function std($command, $repos, $path, $revision)
173 {
174 global $viewsvn;
175
176 $revision = $this->rev($revision);
177 $repospath = $viewsvn->repos->fetch_path($repos, false);
178
179 return $this->svn($command . ' ' . $repospath . $path . '@' . $revision);
180 }
181
182 /**
183 * SVN Wrapper: blame
184 *
185 * @access protected
186 *
187 * @param string Repository
188 * @param string Path
189 * @param integer Revision
190 *
191 * @return array Lines of blame output
192 */
193 function blame($repos, $path, $revision)
194 {
195 return $this->std('blame', $repos, $path, $revision);
196 }
197
198 /**
199 * SVN Wrapper: cat
200 *
201 * @access protected
202 *
203 * @param string Repository
204 * @param string Path
205 * @param integer Revision
206 *
207 * @return array Lines of cat output
208 */
209 function cat($repos, $path, $revision)
210 {
211 return $this->std('cat', $repos, $path, $revision);
212 }
213
214 /**
215 * SVN Wrapper: diff
216 *
217 * @access protected
218 *
219 * @param string Repository
220 * @param string Path
221 * @param integer Lower revision
222 * @param integer Higher revision
223 *
224 * @return array Lines of diff output
225 */
226 function diff($repos, $path, $lorev, $hirev)
227 {
228 global $viewsvn;
229
230 $hirev = $this->rev($hirev);
231 $lorev = $this->rev($lorev);
232 if ($lorev == 'HEAD')
233 {
234 $lorev = 1;
235 }
236
237 if (is_integer($hirev) AND is_integer($lorev))
238 {
239 if ($lorev > $hirev)
240 {
241 $lorev = $hirev - 1;
242 }
243 if ($lorev == $hirev)
244 {
245 $lorev = 0;
246 }
247 }
248
249 $repospath = $viewsvn->repos->fetch_path($repos, false);
250
251 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
252 }
253
254 /**
255 * SVN Wrapper: log
256 *
257 * @access protected
258 *
259 * @param string Repository
260 * @param string Path
261 * @param integer Lower revision
262 * @param integer Higher revision
263 *
264 * @return array Lines of log output
265 */
266 function log($repos, $path, $lorev, $hirev)
267 {
268 global $viewsvn;
269
270 $hirev = $this->rev($hirev);
271 $lorev = $this->rev($hirev);
272 if ($lorev == 'HEAD')
273 {
274 $lorev = 0;
275 }
276
277 if (is_integer($hirev) AND is_integer($lorev))
278 {
279 if ($lorev > $hirev)
280 {
281 $lorev = $hirev - 1;
282 }
283 if ($lorev == $hirev)
284 {
285 $lorev = 0;
286 }
287 }
288
289 $repospath = $viewsvn->repos->fetch_path($repos, false);
290
291 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
292 }
293
294 /**
295 * SVN Wrapper: ls (list)
296 *
297 * @access protected
298 *
299 * @param string Repository
300 * @param string Path
301 * @param integer Revision
302 *
303 * @return array Lines of list output
304 */
305 function ls($repos, $path, $revision)
306 {
307 return $this->std('list', $repos, $path, $revision);
308 }
309
310 /**
311 * Generates a clean revision number
312 *
313 * @access public
314 *
315 * @param integer Revision number
316 *
317 * @return mixed Cleaned revision or HEAD
318 */
319 function rev($revision)
320 {
321 if (($revision = intval($revision)) < 1)
322 {
323 $revision = 'HEAD';
324 }
325 return $revision;
326 }
327 }
328
329 /**
330 * Commonly executed SVN commands that return data
331 * used in many parts of the system
332 *
333 * @package ViewSVN
334 * @version $Id$
335 */
336 class SVNCommon
337 {
338 /**
339 * Registry object
340 * @var object
341 */
342 var $registry;
343
344 /**
345 * List of revisions
346 * @var array
347 */
348 var $revisions;
349
350 /**
351 * List of logs
352 * @var array
353 */
354 var $logs;
355
356 /**
357 * Constructor: bind with registry
358 */
359 function SVNCommon()
360 {
361 global $viewsvn;
362
363 $this->registry =& $viewsvn;
364 }
365
366 /**
367 * Checks to see if the given universal path is
368 * a directory
369 *
370 * @access public
371 *
372 * @param string Universal path
373 *
374 * @return bool Directory or not
375 */
376 function isdir($path)
377 {
378 $output = $this->registry->svn->std('info', $this->registry->paths->fetch_repos($path), $this->registry->paths->fetch_path($path), $this->registry->paths->fetch_rev_num());
379
380 foreach ($output AS $line)
381 {
382 if (preg_match('#^Node Kind: (.*)#', $line, $matches))
383 {
384 if (trim(strtolower($matches[1])) == 'directory')
385 {
386 return true;
387 }
388 }
389 }
390
391 return false;
392 }
393
394 /**
395 * Get a list of revisions for a path
396 *
397 * @access public
398 *
399 * @param string Universal path
400 *
401 * @return array Key revisions
402 */
403 function fetch_revs($path)
404 {
405 if (!isset($this->revisions["$path"]))
406 {
407 $log = $this->fetch_logs($path);
408
409 $revs = array_keys($log);
410
411 $this->revisions["$path"] = array(
412 'HEAD' => $revs[0],
413 'START' => $revs[ count($revs) - 1 ],
414 'revs' => $revs
415 );
416 }
417
418 return $this->revisions["$path"];
419 }
420
421 /**
422 * Gets the revision that is marked as HEAD
423 *
424 * @access public
425 *
426 * @param string Universal path
427 *
428 * @return integer Revision
429 */
430 function fetch_head_rev($path)
431 {
432 $revs = $this->fetch_revs($path);
433 return $revs['HEAD'];
434 }
435
436 /**
437 * Returns the previous revision to the one given
438 *
439 * @access public
440 *
441 * @param string Universal path
442 * @param integer Arbitrary revision
443 *
444 * @return integer Previous revision (-1 if none)
445 */
446 function fetch_prev_rev($path, $current)
447 {
448 global $viewsvn;
449
450 $revs = $this->fetch_revs($path);
451
452 if ($current == 'HEAD')
453 {
454 $current = $this->fetch_head_rev($path);
455 }
456
457 $index = array_search($current, $revs['revs']);
458 if ($current === false)
459 {
460 $message->trigger->error(sprintf($viewsvn->lang->string('Revision r%1$s is not in path %2$s'), $current, $path));
461 }
462
463 if (isset($revs['revs'][ $index + 1 ]))
464 {
465 return $revs['revs'][ $index + 1 ];
466 }
467 else
468 {
469 return -1;
470 }
471 }
472
473 /**
474 * Get a list of logs
475 *
476 * @access public
477 *
478 * @param string Universal path
479 *
480 * @return array Log data
481 */
482 function fetch_logs($path)
483 {
484 if (!isset($this->logs["$path"]))
485 {
486 $log = new SVNLog($this->registry->paths->fetch_repos($path), $this->registry->paths->fetch_path($path), 0, $this->registry->paths->fetch_rev_num());
487
488 $this->logs["$path"] = $log->fetch();
489 }
490
491 return $this->logs["$path"];
492 }
493
494 /**
495 * Returns a given log entry for a path
496 * and revision
497 *
498 * @access public
499 *
500 * @param string Universal path
501 * @param integer Arbitrary revision
502 *
503 * @return array Log entry
504 */
505 function fetch_log($path, $rev)
506 {
507 $logs = $this->fetch_logs($path);
508
509 $rev = $this->registry->svn->rev($rev);
510 if ($rev == 'HEAD')
511 {
512 $rev = $this->fetch_head_rev($path);
513 }
514
515 if (isset($logs["$rev"]))
516 {
517 return $logs["$rev"];
518 }
519 else
520 {
521 $keys = array_keys($logs);
522 sort($keys);
523
524 for ($i = 0; $i < count($keys); $i++)
525 {
526 if ($rev > $keys["$i"] AND $rev < $keys[ $i + 1 ])
527 {
528 return $logs["$keys[$i]"];
529 }
530 }
531
532 return null;
533 }
534 }
535
536 /**
537 * Prints the file changed list
538 *
539 * @access public
540 *
541 * @public array List of file changes
542 * @public string The repository
543 * @public integer Current revision
544 *
545 * @return string Processed HTML
546 */
547 function construct_file_changes($changes, $repos, $revision)
548 {
549 global $viewsvn;
550
551 $files = '';
552
553 foreach ($changes AS $file)
554 {
555 switch ($file['action'])
556 {
557 case 'A':
558 $class = 'file_add';
559 $tooltip = $viewsvn->lang->string('Added');
560 break;
561 case 'D':
562 $class = 'file_delete';
563 $tooltip = $viewsvn->lang->string('Deleted');
564 break;
565 case 'M':
566 $class = 'file_modify';
567 $tooltip = $viewsvn->lang->string('Modified');
568 break;
569 case 'R':
570 $class = 'file_replace';
571 $tooltip = $viewsvn->lang->string('Replaced');
572 break;
573 }
574
575 $show['from'] = (bool)$file['from'];
576
577 if ($file['from'])
578 {
579 $class = 'file_move';
580 $tooltip = 'Moved/Copied';
581 preg_match('#(.*):([0-9]+)#', $file['from'], $matches);
582 $link['from'] = $viewsvn->paths->out('view.php' . $viewsvn->paths->fetch_rev_str(false, $matches[2]), $repos . $matches[1]);
583 }
584
585 $link['file'] = $viewsvn->paths->out('view.php' . $viewsvn->paths->fetch_rev_str(false, $revision), $repos . $file['file']);
586
587 eval('$files .= "' . $viewsvn->template->fetch('file_change') . '";');
588 }
589
590 return $files;
591 }
592 }
593
594 /**
595 * Annotation/blame system; constructs an array
596 * that is ready for output
597 *
598 * @package ViewSVN
599 * @version $Id$
600 */
601 class SVNBlame
602 {
603 /**
604 * Array of blame information
605 * @var array
606 */
607 var $blame = array();
608
609 /**
610 * Raw "svn blame" output
611 * @var array
612 */
613 var $rawoutput;
614
615 /**
616 * Constructor: create blame and store data
617 *
618 * @param string Repository
619 * @param string Path
620 * @param integer Revision
621 */
622 function SVNBlame($repos, $path, $revision)
623 {
624 global $viewsvn;
625
626 $this->rawoutput = $viewsvn->svn->blame($repos, $path, $revision);
627 $this->process();
628 }
629
630 /**
631 * Returns blame for display
632 *
633 * @access public
634 *
635 * @return array Blame data
636 */
637 function fetch()
638 {
639 return $this->blame;
640 }
641
642 /**
643 * Parses the blame data
644 *
645 * @access private
646 */
647 function process()
648 {
649 $lineno = 1;
650
651 foreach ($this->rawoutput AS $line)
652 {
653 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
654 {
655 $this->blame[] = array(
656 'rev' => $matches[1],
657 'author' => $matches[2],
658 'line' => $matches[3],
659 'lineno' => $lineno++
660 );
661 }
662 // a blank line
663 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
664 {
665 $this->blame[] = array(
666 'rev' => $matches[1],
667 'author' => $matches[2],
668 'line' => '',
669 'lineno' => $lineno++
670 );
671 }
672 }
673 }
674 }
675
676 /**
677 * Log management system; creates a complex list
678 * of SVN log information
679 *
680 * @package ViewSVN
681 * @version $Id$
682 */
683 class SVNLog
684 {
685 /**
686 * Array of logs
687 * @var array
688 */
689 var $logs = array();
690
691 /**
692 * Raw "svn log" output
693 * @var array
694 */
695 var $rawoutput;
696
697 /**
698 * Constructor: create log store for the given file
699 *
700 * @param string Repository
701 * @param string Path
702 * @param integer Lower revision
703 * @param integer Higher revision
704 */
705 function SVNLog($repos, $path, $lorev, $hirev)
706 {
707 global $viewsvn;
708
709 $this->rawoutput = $viewsvn->svn->log($repos, $path, $lorev, $hirev);
710 $this->process();
711 }
712
713 /**
714 * Returns logs for display
715 *
716 * @access public
717 *
718 * @return array Log data
719 */
720 function fetch()
721 {
722 return $this->logs;
723 }
724
725 /**
726 * Splits up the raw output into a usable log
727 *
728 * @access private
729 */
730 function process()
731 {
732 $lastrev = 0;
733
734 for ($i = 1; $i <= count($this->rawoutput) - 1; $i++)
735 {
736 $line = $this->rawoutput["$i"];
737
738 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
739 {
740 if (isset($this->logs["$lastrev"]))
741 {
742 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
743 }
744
745 $this->logs["$matches[1]"] = array(
746 'rev' => $matches[1],
747 'author' => $matches[2],
748 'date' => $matches[3],
749 'timezone' => $matches[4],
750 'lines' => $matches[6],
751 'message' => ''
752 );
753
754 $lastrev = $matches[1];
755 }
756 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
757 {
758 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
759 {
760 $matches[2] = $amatches[1];
761 }
762
763 $this->logs["$lastrev"]['files'][] = array(
764 'action' => $matches[1],
765 'file' => trim($matches[2]),
766 'from' => (isset($amatches[2]) ? $amatches[2] : '')
767 );
768 }
769 else
770 {
771 if (trim($line) != 'Changed paths:')
772 {
773 $this->logs["$lastrev"]['message'] .= $line . "\n";
774 }
775 }
776 }
777
778 if (isset($this->logs["$lastrev"]))
779 {
780 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
781 }
782 }
783
784 /**
785 * Trims the last dash line off a message
786 *
787 * @access private
788 *
789 * @param string Message with annoying-ass line
790 *
791 * @return string Clean string
792 */
793 function strip_last_line($string)
794 {
795 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
796 }
797 }
798
799 /**
800 * Diff system; constructs a diff array that
801 * is ready for output
802 *
803 * @package ViewSVN
804 */
805 class SVNDiff
806 {
807 /**
808 * Array of diff information
809 * @var array
810 */
811 var $diff = array();
812
813 /**
814 * Raw "svn diff" output
815 * @var array
816 */
817 var $rawoutput;
818
819 /**
820 * Constructor: create and store diff data
821 *
822 * @param string Repository
823 * @param string Path
824 * @param integer Lower revision
825 * @param integer Higher revision
826 */
827 function SVNDiff($repos, $path, $lorev, $hirev)
828 {
829 global $viewsvn;
830
831 $this->rawoutput = $viewsvn->svn->diff($repos, $path, $lorev, $hirev);
832 $this->process();
833 }
834
835 /**
836 * Returns diffs for display
837 *
838 * @access public
839 *
840 * @return array Diff data
841 */
842 function fetch()
843 {
844 return $this->diff;
845 }
846
847 /**
848 * Processes and prepares diff data
849 *
850 * @access private
851 */
852 function process()
853 {
854 global $viewsvn;
855
856 $chunk = 0;
857 $indexcounter = null;
858
859 $delstack = array();
860
861 foreach ($this->rawoutput AS $line)
862 {
863 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
864 {
865 $delstack = array();
866 $this->diff["$index"][ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
867 $lines['old'] = $this->diff["$index"]["$chunk"]['hunk']['old']['line'] - 1;
868 $lines['new'] = $this->diff["$index"]["$chunk"]['hunk']['new']['line'] - 1;
869 continue;
870 }
871
872 if ($indexcounter <= 3 AND $indexcounter !== null)
873 {
874 //echo $line . "\n";
875 $indexcounter++;
876 continue;
877 }
878 else if ($indexcounter == 3)
879 {
880 $indexcounter = null;
881 //echo "\n\n\n";
882 continue;
883 }
884
885 if (preg_match('#^([\+\- ])(.*)#', $line, $matches))
886 {
887 $act = $matches[1];
888 $content = $matches[2];
889
890 if ($act == ' ')
891 {
892 $this->diff["$index"]["$chunk"][] = array(
893 'line' => $content,
894 'act' => '',
895 'oldlineno' => ++$lines['old'],
896 'newlineno' => ++$lines['new']
897 );
898
899 $delstack = array();
900 }
901 else if ($act == '+')
902 {
903 // potential line delta
904 if (count($delstack) > 0)
905 {
906 $lastline = array_shift($delstack);
907
908 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
909 {
910 //$delta['end'] = $delta['start'] + -$delta['end'];
911 //print_r($delta);
912 //$change = '';
913 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
914 {
915 $end = strlen($lastline['line']) + $delta['end'];
916 $viewsvn->debug("RM delta- = " . $end);
917 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
918 $this->diff["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
919 }
920
921 //$change = '';
922 if (strlen($content) > $delta['start'] - $delta['end'])
923 {
924 $end = strlen($content) + $delta['end'];
925 $viewsvn->debug("MK delta+ = " . $end);
926 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
927 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
928 }
929
930 // create two sets of ends for the two contents
931 /*$delta['endo'] = strlen($lastline['line']) - $delta['end'];
932 $delta['endn'] = strlen($content) - $delta['end'];
933
934 $diffo = $delta['endo'] - $delta['start'];
935 $diffn = $delta['endn'] - $delta['start'];
936
937 if (strlen($lastline['line']) > $delta['endo'] - $diffo)
938 {
939 $removed = substr($lastline['line'], $delta['start'], $diffo);
940 $this->diff["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr_replace($lastline['line'], '{@-' . '-}' . $removed . '{/@-' . '-}', $delta['start'], $diffo);
941 }
942
943 if (strlen($content) > $delta['endn'] - $diffn)
944 {
945 $added = substr($content, $delta['start'], $diffn);
946 $content = substr_replace($content, '{@+' . '+}' . $added . '{/@+' . '+}', $delta['start'], $diffn);
947 }*/
948 }
949 }
950
951 $this->diff["$index"]["$chunk"][] = array(
952 'line' => $content,
953 'act' => '+',
954 'oldlineno' => '',
955 'newlineno' => ++$lines['new']
956 );
957 }
958 else if ($act == '-')
959 {
960 $this->diff["$index"]["$chunk"][] = $thearray = array(
961 'line' => $content,
962 'act' => '-',
963 'oldlineno' => ++$lines['old'],
964 'newlineno' => ''
965 );
966
967 $key = count($this->diff["$index"]["$chunk"]) - 2;
968 $thearray['INDEX'] = $key;
969
970 array_push($delstack, $thearray);
971 }
972 }
973 // whitespace lines
974 else
975 {
976 if (preg_match('#^Index: (.*?)$#', $line, $matches))
977 {
978 $index = $matches[1];
979 $indexcounter = 1;
980 $chunk = 0;
981 continue;
982 }
983
984 $this->diff["$index"]["$chunk"][] = array(
985 'line' => '',
986 'act' => '',
987 'oldlineno' => ++$lines['old'],
988 'newlineno' => ++$lines['new']
989 );
990
991 $delstack = array();
992 }
993 }
994 }
995
996 /**
997 * Returns the amount of change that occured
998 * between two lines
999 *
1000 * @access private
1001 *
1002 * @param string Old line
1003 * @param string New line
1004 *
1005 * @return array Difference of positions
1006 */
1007 function fetch_diff_extent($old, $new)
1008 {
1009 global $viewsvn;
1010
1011 $start = 0;
1012 $min = min(strlen($old), strlen($new));
1013
1014 $viewsvn->debug("min1 = $min");
1015
1016 while ($start < $min AND $old{"$start"} == $new{"$start"})
1017 {
1018 $start++;
1019 }
1020
1021 $end = -1;
1022 $min = $min - $start;
1023
1024 $viewsvn->debug("min2 = $min");
1025
1026 $viewsvn->debug("checking: " . $old{ strlen($old) + $end } . ' == ' . $new{ strlen($new) + $end });
1027
1028 while (-$end <= $min AND $old{ strlen($old) + $end } == $new{ strlen($new) + $end })
1029 {
1030 $end--;
1031 }
1032
1033 return array('start' => $start, 'end' => $end + 1, 'min' => $min);
1034 }
1035
1036 /**
1037 * Returns the amount of change that occured
1038 * between two lines
1039 *
1040 * @access private
1041 * @deprecated Use the fetch_diff_extent() function; this one calculates incorrect end deltas
1042 *
1043 * @param string Old line
1044 * @param string New line
1045 *
1046 * @return array Difference of positions
1047 */
1048 function fetch_diff_extent2($old, $new)
1049 {
1050 $start = 0;
1051 $min = min(strlen($old), strlen($new));
1052
1053 for ($start = 0; $start < $min; $start++)
1054 {
1055 if ($old{"$start"} != $new{"$start"})
1056 {
1057 break;
1058 }
1059 }
1060
1061 $max = max(strlen($old), strlen($new));
1062
1063 for ($end = 0; $end < $min; $end++)
1064 {
1065 $oldpos = strlen($old) - $end;
1066 $newpos = strlen($new) - $end;
1067
1068 if ($old{"$oldpos"} != $new{"$newpos"})
1069 {
1070 break;
1071 }
1072 }
1073
1074 $end--;
1075
1076 if ($start == 0 AND $end == $max)
1077 {
1078 return false;
1079 }
1080
1081 return array('start' => $start, 'end' => $end);
1082 }
1083 }
1084
1085 /*=====================================================================*\
1086 || ###################################################################
1087 || # $HeadURL$
1088 || # $Id$
1089 || ###################################################################
1090 \*=====================================================================*/
1091 ?>