2008-07-31 22:38:29 +00:00
|
|
|
<?php
|
|
|
|
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# This file is part of InDefero, an open source project management application.
|
2011-04-02 19:37:07 +00:00
|
|
|
# Copyright (C) 2008-2011 Céondo Ltd and contributors.
|
2008-07-31 22:38:29 +00:00
|
|
|
#
|
|
|
|
# InDefero is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# InDefero is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
#
|
|
|
|
# ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Diff parser.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class IDF_Diff
|
|
|
|
{
|
2011-03-16 22:50:41 +00:00
|
|
|
public $path_strip_level = 0;
|
2008-07-31 22:38:29 +00:00
|
|
|
protected $lines = array();
|
|
|
|
|
|
|
|
public $files = array();
|
|
|
|
|
2011-03-16 22:50:41 +00:00
|
|
|
public function __construct($diff, $path_strip_level = 0)
|
2008-07-31 22:38:29 +00:00
|
|
|
{
|
2011-03-16 22:50:41 +00:00
|
|
|
$this->path_strip_level = $path_strip_level;
|
2011-10-08 23:54:51 +00:00
|
|
|
$this->lines = IDF_FileUtil::splitIntoLines($diff, true);
|
2008-07-31 22:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function parse()
|
|
|
|
{
|
|
|
|
$current_file = '';
|
|
|
|
$current_chunk = 0;
|
|
|
|
$lline = 0;
|
|
|
|
$rline = 0;
|
2008-08-02 07:38:06 +00:00
|
|
|
$files = array();
|
2009-02-02 19:55:45 +00:00
|
|
|
$indiff = false; // Used to skip the headers in the git patches
|
|
|
|
$i = 0; // Used to skip the end of a git patch with --\nversion number
|
2011-03-16 22:50:41 +00:00
|
|
|
$diffsize = count($this->lines);
|
|
|
|
while ($i < $diffsize) {
|
|
|
|
// look for the potential beginning of a diff
|
|
|
|
if (substr($this->lines[$i], 0, 4) !== '--- ') {
|
|
|
|
$i++;
|
2008-07-31 22:38:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-16 22:50:41 +00:00
|
|
|
|
|
|
|
// we're inside a diff candiate
|
|
|
|
$oldfileline = $this->lines[$i++];
|
|
|
|
$newfileline = $this->lines[$i++];
|
|
|
|
if (substr($newfileline, 0, 4) !== '+++ ') {
|
|
|
|
// not a valid diff here, move on
|
2008-07-31 22:38:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-16 22:50:41 +00:00
|
|
|
|
|
|
|
// use new file name by default
|
2011-10-05 00:44:04 +00:00
|
|
|
preg_match("/^\+\+\+ ([^\t\n\r]+)/", $newfileline, $m);
|
2011-03-16 22:50:41 +00:00
|
|
|
$current_file = $m[1];
|
|
|
|
if ($current_file === '/dev/null') {
|
|
|
|
// except if it's /dev/null, use the old one instead
|
|
|
|
// eg. mtn 0.48 and newer
|
2011-10-05 00:44:04 +00:00
|
|
|
preg_match("/^--- ([^\t\r\n]+)/", $oldfileline, $m);
|
2011-03-16 22:50:41 +00:00
|
|
|
$current_file = $m[1];
|
2008-07-31 22:38:29 +00:00
|
|
|
}
|
2011-03-16 22:50:41 +00:00
|
|
|
if ($this->path_strip_level > 0) {
|
2011-03-17 00:10:59 +00:00
|
|
|
$fileparts = explode('/', $current_file, $this->path_strip_level+1);
|
|
|
|
$current_file = array_pop($fileparts);
|
2008-07-31 22:38:29 +00:00
|
|
|
}
|
2011-03-16 22:50:41 +00:00
|
|
|
$current_chunk = 0;
|
|
|
|
$files[$current_file] = array();
|
|
|
|
$files[$current_file]['chunks'] = array();
|
|
|
|
$files[$current_file]['chunks_def'] = array();
|
|
|
|
|
|
|
|
while ($i < $diffsize && substr($this->lines[$i], 0, 3) === '@@ ') {
|
|
|
|
$elems = preg_match('/@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@.*/',
|
|
|
|
$this->lines[$i++], $results);
|
|
|
|
if ($elems != 1) {
|
|
|
|
// hunk is badly formatted
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$delstart = $results[1];
|
|
|
|
$dellines = $results[2] === '' ? 1 : $results[2];
|
|
|
|
$addstart = $results[3];
|
|
|
|
$addlines = $results[4] === '' ? 1 : $results[4];
|
2011-03-18 00:05:25 +00:00
|
|
|
|
|
|
|
$files[$current_file]['chunks_def'][] = array(
|
|
|
|
array($delstart, $dellines), array($addstart, $addlines)
|
|
|
|
);
|
2011-03-16 22:50:41 +00:00
|
|
|
$files[$current_file]['chunks'][] = array();
|
|
|
|
|
2011-03-18 00:05:25 +00:00
|
|
|
while ($i < $diffsize && ($addlines >= 0 || $dellines >= 0)) {
|
2011-06-01 22:48:38 +00:00
|
|
|
$linetype = $this->lines[$i] != '' ? $this->lines[$i][0] : false;
|
2011-10-05 00:44:04 +00:00
|
|
|
$content = substr($this->lines[$i], 1);
|
2011-03-16 22:50:41 +00:00
|
|
|
switch ($linetype) {
|
|
|
|
case ' ':
|
|
|
|
$files[$current_file]['chunks'][$current_chunk][] =
|
2011-10-05 00:44:04 +00:00
|
|
|
array($delstart, $addstart, $content);
|
2011-03-16 22:50:41 +00:00
|
|
|
$dellines--;
|
|
|
|
$addlines--;
|
|
|
|
$delstart++;
|
|
|
|
$addstart++;
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
$files[$current_file]['chunks'][$current_chunk][] =
|
2011-10-05 00:44:04 +00:00
|
|
|
array('', $addstart, $content);
|
2011-03-16 22:50:41 +00:00
|
|
|
$addlines--;
|
|
|
|
$addstart++;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
$files[$current_file]['chunks'][$current_chunk][] =
|
2011-10-05 00:44:04 +00:00
|
|
|
array($delstart, '', $content);
|
2011-03-16 22:50:41 +00:00
|
|
|
$dellines--;
|
|
|
|
$delstart++;
|
|
|
|
break;
|
2011-03-18 00:05:25 +00:00
|
|
|
case '\\':
|
2011-10-05 00:44:04 +00:00
|
|
|
// no new line at the end of this file; remove pseudo new line from last line
|
|
|
|
$cur = count($files[$current_file]['chunks'][$current_chunk]) - 1;
|
|
|
|
$files[$current_file]['chunks'][$current_chunk][$cur][2] =
|
|
|
|
rtrim($files[$current_file]['chunks'][$current_chunk][$cur][2], "\r\n");
|
2011-03-18 00:05:25 +00:00
|
|
|
continue;
|
2011-03-16 22:50:41 +00:00
|
|
|
default:
|
|
|
|
break 2;
|
|
|
|
}
|
2011-10-05 00:44:04 +00:00
|
|
|
$i++;
|
2011-03-16 22:50:41 +00:00
|
|
|
}
|
|
|
|
$current_chunk++;
|
2009-02-02 20:42:31 +00:00
|
|
|
}
|
2008-07-31 22:38:29 +00:00
|
|
|
}
|
|
|
|
$this->files = $files;
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the html version of a parsed diff.
|
|
|
|
*/
|
|
|
|
public function as_html()
|
|
|
|
{
|
|
|
|
$out = '';
|
2011-10-06 00:06:51 +00:00
|
|
|
foreach ($this->files as $filename => $file) {
|
2008-11-15 20:23:29 +00:00
|
|
|
$pretty = '';
|
2010-12-02 00:50:01 +00:00
|
|
|
$fileinfo = IDF_FileUtil::getMimeType($filename);
|
|
|
|
if (IDF_FileUtil::isSupportedExtension($fileinfo[2])) {
|
2008-11-15 20:23:29 +00:00
|
|
|
$pretty = ' prettyprint';
|
2008-11-15 20:10:37 +00:00
|
|
|
}
|
2011-10-06 00:06:51 +00:00
|
|
|
|
2008-07-31 22:38:29 +00:00
|
|
|
$cc = 1;
|
2011-10-06 00:06:51 +00:00
|
|
|
$offsets = array();
|
|
|
|
$contents = array();
|
|
|
|
|
2008-07-31 22:38:29 +00:00
|
|
|
foreach ($file['chunks'] as $chunk) {
|
|
|
|
foreach ($chunk as $line) {
|
2011-10-06 00:06:51 +00:00
|
|
|
list($left, $right, $content) = $line;
|
|
|
|
if ($left and $right) {
|
2011-10-08 22:13:34 +00:00
|
|
|
$class = 'context';
|
2011-10-06 00:06:51 +00:00
|
|
|
} elseif ($left) {
|
2011-10-08 22:13:34 +00:00
|
|
|
$class = 'removed';
|
2008-07-31 22:38:29 +00:00
|
|
|
} else {
|
2011-10-08 22:13:34 +00:00
|
|
|
$class = 'added';
|
2008-07-31 22:38:29 +00:00
|
|
|
}
|
2011-10-06 00:06:51 +00:00
|
|
|
|
2011-10-08 22:13:34 +00:00
|
|
|
$offsets[] = sprintf('<td>%s</td><td>%s</td>', $left, $right);
|
2011-10-09 01:14:43 +00:00
|
|
|
$content = IDF_FileUtil::emphasizeControlCharacters(Pluf_esc($content));
|
2011-10-06 00:06:51 +00:00
|
|
|
$contents[] = sprintf('<td class="%s%s mono">%s</td>', $class, $pretty, $content);
|
|
|
|
}
|
|
|
|
if (count($file['chunks']) > $cc) {
|
|
|
|
$offsets[] = '<td class="next">...</td><td class="next">...</td>';
|
2011-10-08 22:13:34 +00:00
|
|
|
$contents[] = '<td class="next"></td>';
|
2008-07-31 22:38:29 +00:00
|
|
|
}
|
|
|
|
$cc++;
|
|
|
|
}
|
2011-10-06 00:06:51 +00:00
|
|
|
|
2011-10-06 00:31:36 +00:00
|
|
|
list($added, $removed) = end($file['chunks_def']);
|
|
|
|
|
|
|
|
$added = $added[0] + $added[1];
|
2011-10-27 21:32:18 +00:00
|
|
|
$leftwidth = 0;
|
2011-10-06 00:31:36 +00:00
|
|
|
if ($added > 0)
|
2011-12-02 00:20:14 +00:00
|
|
|
$leftwidth = ((ceil(log10($added)) + 1) * 8) + 17;
|
2011-10-06 00:31:36 +00:00
|
|
|
|
|
|
|
$removed = $removed[0] + $removed[1];
|
2011-10-27 21:32:18 +00:00
|
|
|
$rightwidth = 0;
|
2011-10-06 00:31:36 +00:00
|
|
|
if ($removed > 0)
|
2011-12-02 00:20:14 +00:00
|
|
|
$rightwidth = ((ceil(log10($removed)) + 1) * 8) + 17;
|
2011-10-08 22:13:34 +00:00
|
|
|
|
2011-10-27 21:32:18 +00:00
|
|
|
// we need to correct the width of a single column a little
|
|
|
|
// to take less space and to hide the empty one
|
|
|
|
$class = '';
|
|
|
|
if ($leftwidth == 0) {
|
|
|
|
$class = 'left-hidden';
|
|
|
|
$rightwidth -= floor(log10($removed));
|
|
|
|
}
|
|
|
|
else if ($rightwidth == 0) {
|
|
|
|
$class = 'right-hidden';
|
|
|
|
$leftwidth -= floor(log10($added));
|
|
|
|
}
|
|
|
|
|
2011-10-08 22:13:34 +00:00
|
|
|
$inner_linecounts =
|
2011-10-27 21:32:18 +00:00
|
|
|
'<table class="diff-linecounts '.$class.'">' ."\n".
|
2011-10-08 22:13:34 +00:00
|
|
|
'<colgroup><col width="'.$leftwidth.'" /><col width="'. $rightwidth.'" /></colgroup>' ."\n".
|
|
|
|
'<tr class="line">' .
|
|
|
|
implode('</tr>'."\n".'<tr class="line">', $offsets).
|
|
|
|
'</tr>' ."\n".
|
|
|
|
'</table>' ."\n";
|
2011-10-06 00:31:36 +00:00
|
|
|
|
2011-10-06 00:06:51 +00:00
|
|
|
|
2011-10-08 22:13:34 +00:00
|
|
|
$inner_contents =
|
|
|
|
'<table class="diff-contents">' ."\n".
|
|
|
|
'<tr class="line">' .
|
|
|
|
implode('</tr>'."\n".'<tr class="line">', $contents) .
|
|
|
|
'</tr>' ."\n".
|
|
|
|
'</table>' ."\n";
|
|
|
|
|
2011-10-09 01:14:43 +00:00
|
|
|
$out .= '<table class="diff unified">' ."\n".
|
2011-10-08 22:13:34 +00:00
|
|
|
'<colgroup><col width="'.($leftwidth + $rightwidth + 1).'" /><col width="*" /></colgroup>' ."\n".
|
2011-10-06 00:06:51 +00:00
|
|
|
'<tr id="diff-'.md5($filename).'">'.
|
2011-10-08 22:13:34 +00:00
|
|
|
'<th colspan="2">'.Pluf_esc($filename).'</th>'.
|
2011-10-06 00:06:51 +00:00
|
|
|
'</tr>' ."\n".
|
2011-10-08 22:13:34 +00:00
|
|
|
'<tr>' .
|
|
|
|
'<td>'. $inner_linecounts .'</td>'. "\n".
|
|
|
|
'<td><div class="scroll">'. $inner_contents .'</div></td>'.
|
2011-10-06 00:06:51 +00:00
|
|
|
'</tr>' ."\n".
|
|
|
|
'</table>' ."\n";
|
2008-07-31 22:38:29 +00:00
|
|
|
}
|
2011-10-06 00:06:51 +00:00
|
|
|
|
2008-11-11 21:32:01 +00:00
|
|
|
return Pluf_Template::markSafe($out);
|
2008-07-31 22:38:29 +00:00
|
|
|
}
|
|
|
|
|
2008-11-30 09:26:05 +00:00
|
|
|
/**
|
|
|
|
* Review patch.
|
|
|
|
*
|
|
|
|
* Given the original file as a string and the parsed
|
|
|
|
* corresponding diff chunks, generate a side by side view of the
|
|
|
|
* original file and new file with added/removed lines.
|
|
|
|
*
|
|
|
|
* Example of use:
|
|
|
|
*
|
|
|
|
* $diff = new IDF_Diff(file_get_contents($diff_file));
|
|
|
|
* $orig = file_get_contents($orig_file);
|
|
|
|
* $diff->parse();
|
|
|
|
* echo $diff->fileCompare($orig, $diff->files[$orig_file], $diff_file);
|
|
|
|
*
|
|
|
|
* @param string Original file
|
|
|
|
* @param array Chunk description of the diff corresponding to the file
|
|
|
|
* @param string Original file name
|
|
|
|
* @param int Number of lines before/after the chunk to be displayed (10)
|
|
|
|
* @return Pluf_Template_SafeString The table body
|
|
|
|
*/
|
2010-04-30 22:56:48 +00:00
|
|
|
public function fileCompare($orig, $chunks, $filename, $context=10)
|
2008-11-30 09:26:05 +00:00
|
|
|
{
|
2011-10-09 00:13:49 +00:00
|
|
|
$orig_lines = IDF_FileUtil::splitIntoLines($orig);
|
2008-11-30 09:26:05 +00:00
|
|
|
$new_chunks = $this->mergeChunks($orig_lines, $chunks, $context);
|
|
|
|
return $this->renderCompared($new_chunks, $filename);
|
|
|
|
}
|
|
|
|
|
2011-10-08 23:54:51 +00:00
|
|
|
private function mergeChunks($orig_lines, $chunks, $context=10)
|
2008-11-30 09:26:05 +00:00
|
|
|
{
|
|
|
|
$spans = array();
|
|
|
|
$new_chunks = array();
|
|
|
|
$min_line = 0;
|
|
|
|
$max_line = 0;
|
|
|
|
//if (count($chunks['chunks_def']) == 0) return '';
|
|
|
|
foreach ($chunks['chunks_def'] as $chunk) {
|
|
|
|
$start = ($chunk[0][0] > $context) ? $chunk[0][0]-$context : 0;
|
|
|
|
$end = (($chunk[0][0]+$chunk[0][1]+$context-1) < count($orig_lines)) ? $chunk[0][0]+$chunk[0][1]+$context-1 : count($orig_lines);
|
|
|
|
$spans[] = array($start, $end);
|
|
|
|
}
|
|
|
|
// merge chunks/get the chunk lines
|
|
|
|
// these are reference lines
|
|
|
|
$chunk_lines = array();
|
|
|
|
foreach ($chunks['chunks'] as $chunk) {
|
|
|
|
foreach ($chunk as $line) {
|
|
|
|
$chunk_lines[] = $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$i = 0;
|
|
|
|
foreach ($chunks['chunks'] as $chunk) {
|
|
|
|
$n_chunk = array();
|
|
|
|
// add lines before
|
|
|
|
if ($chunk[0][0] > $spans[$i][0]) {
|
|
|
|
for ($lc=$spans[$i][0];$lc<$chunk[0][0];$lc++) {
|
|
|
|
$exists = false;
|
|
|
|
foreach ($chunk_lines as $line) {
|
2010-04-30 22:56:48 +00:00
|
|
|
if ($lc == $line[0]
|
2009-02-02 20:42:31 +00:00
|
|
|
or ($chunk[0][1]-$chunk[0][0]+$lc) == $line[1]) {
|
2008-11-30 09:26:05 +00:00
|
|
|
$exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$exists) {
|
2009-02-02 20:47:50 +00:00
|
|
|
$orig = isset($orig_lines[$lc-1]) ? $orig_lines[$lc-1] : '';
|
2008-11-30 09:26:05 +00:00
|
|
|
$n_chunk[] = array(
|
2010-04-30 22:56:48 +00:00
|
|
|
$lc,
|
2008-11-30 09:26:05 +00:00
|
|
|
$chunk[0][1]-$chunk[0][0]+$lc,
|
2009-02-02 20:47:50 +00:00
|
|
|
$orig
|
2008-11-30 09:26:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add chunk lines
|
|
|
|
foreach ($chunk as $line) {
|
|
|
|
$n_chunk[] = $line;
|
|
|
|
}
|
|
|
|
// add lines after
|
|
|
|
$lline = $line;
|
|
|
|
if (!empty($lline[0]) and $lline[0] < $spans[$i][1]) {
|
|
|
|
for ($lc=$lline[0];$lc<=$spans[$i][1];$lc++) {
|
|
|
|
$exists = false;
|
|
|
|
foreach ($chunk_lines as $line) {
|
|
|
|
if ($lc == $line[0] or ($lline[1]-$lline[0]+$lc) == $line[1]) {
|
|
|
|
$exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$exists) {
|
|
|
|
$n_chunk[] = array(
|
2010-04-30 22:56:48 +00:00
|
|
|
$lc,
|
2008-11-30 09:26:05 +00:00
|
|
|
$lline[1]-$lline[0]+$lc,
|
|
|
|
$orig_lines[$lc-1]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$new_chunks[] = $n_chunk;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
// Now, each chunk has the right length, we need to merge them
|
|
|
|
// when needed
|
|
|
|
$nnew_chunks = array();
|
|
|
|
$i = 0;
|
|
|
|
foreach ($new_chunks as $chunk) {
|
|
|
|
if ($i>0) {
|
|
|
|
$lline = end($nnew_chunks[$i-1]);
|
|
|
|
if ($chunk[0][0] <= $lline[0]+1) {
|
|
|
|
// need merging
|
|
|
|
foreach ($chunk as $line) {
|
|
|
|
if ($line[0] > $lline[0] or empty($line[0])) {
|
|
|
|
$nnew_chunks[$i-1][] = $line;
|
2010-04-30 22:56:48 +00:00
|
|
|
}
|
2008-11-30 09:26:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$nnew_chunks[] = $chunk;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$nnew_chunks[] = $chunk;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $nnew_chunks;
|
|
|
|
}
|
|
|
|
|
2011-10-08 23:54:51 +00:00
|
|
|
private function renderCompared($chunks, $filename)
|
2008-11-30 09:26:05 +00:00
|
|
|
{
|
2010-12-02 00:50:01 +00:00
|
|
|
$fileinfo = IDF_FileUtil::getMimeType($filename);
|
2008-11-30 09:26:05 +00:00
|
|
|
$pretty = '';
|
2010-12-02 00:50:01 +00:00
|
|
|
if (IDF_FileUtil::isSupportedExtension($fileinfo[2])) {
|
2008-11-30 09:26:05 +00:00
|
|
|
$pretty = ' prettyprint';
|
|
|
|
}
|
2011-10-09 01:14:43 +00:00
|
|
|
|
2008-11-30 09:26:05 +00:00
|
|
|
$cc = 1;
|
2011-10-09 01:14:43 +00:00
|
|
|
$left_offsets = array();
|
|
|
|
$left_contents = array();
|
|
|
|
$right_offsets = array();
|
|
|
|
$right_contents = array();
|
|
|
|
|
|
|
|
$max_lineno_left = $max_lineno_right = 0;
|
|
|
|
|
2008-11-30 09:26:05 +00:00
|
|
|
foreach ($chunks as $chunk) {
|
|
|
|
foreach ($chunk as $line) {
|
2011-10-09 01:14:43 +00:00
|
|
|
$left = '';
|
|
|
|
$right = '';
|
|
|
|
$content = IDF_FileUtil::emphasizeControlCharacters(Pluf_esc($line[2]));
|
|
|
|
|
2008-11-30 09:26:05 +00:00
|
|
|
if ($line[0] and $line[1]) {
|
2011-10-09 01:14:43 +00:00
|
|
|
$class = 'context';
|
|
|
|
$left = $right = $content;
|
2008-11-30 09:26:05 +00:00
|
|
|
} elseif ($line[0]) {
|
2011-10-09 01:14:43 +00:00
|
|
|
$class = 'removed';
|
|
|
|
$left = $content;
|
2008-11-30 09:26:05 +00:00
|
|
|
} else {
|
2011-10-09 01:14:43 +00:00
|
|
|
$class = 'added';
|
|
|
|
$right = $content;
|
2008-11-30 09:26:05 +00:00
|
|
|
}
|
2011-10-09 01:14:43 +00:00
|
|
|
|
|
|
|
$left_offsets[] = sprintf('<td>%s</td>', $line[0]);
|
|
|
|
$right_offsets[] = sprintf('<td>%s</td>', $line[1]);
|
|
|
|
$left_contents[] = sprintf('<td class="%s%s mono">%s</td>', $class, $pretty, $left);
|
|
|
|
$right_contents[] = sprintf('<td class="%s%s mono">%s</td>', $class, $pretty, $right);
|
|
|
|
|
|
|
|
$max_lineno_left = max($max_lineno_left, $line[0]);
|
|
|
|
$max_lineno_right = max($max_lineno_right, $line[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($chunks) > $cc) {
|
|
|
|
$left_offsets[] = '<td class="next">...</td>';
|
|
|
|
$right_offsets[] = '<td class="next">...</td>';
|
|
|
|
$left_contents[] = '<td></td>';
|
|
|
|
$right_contents[] = '<td></td>';
|
2008-11-30 09:26:05 +00:00
|
|
|
}
|
|
|
|
$cc++;
|
|
|
|
}
|
2011-10-09 01:14:43 +00:00
|
|
|
|
|
|
|
$leftwidth = 1;
|
|
|
|
if ($max_lineno_left > 0)
|
2011-12-02 00:20:14 +00:00
|
|
|
$leftwidth = ((ceil(log10($max_lineno_left)) + 1) * 8) + 17;
|
2011-10-09 01:14:43 +00:00
|
|
|
|
|
|
|
$rightwidth = 1;
|
|
|
|
if ($max_lineno_right > 0)
|
2011-12-02 00:20:14 +00:00
|
|
|
$rightwidth = ((ceil(log10($max_lineno_right)) + 1) * 8) + 17;
|
2011-10-09 01:14:43 +00:00
|
|
|
|
|
|
|
$inner_linecounts_left =
|
|
|
|
'<table class="diff-linecounts">' ."\n".
|
|
|
|
'<colgroup><col width="'.$leftwidth.'" /></colgroup>' ."\n".
|
|
|
|
'<tr class="line">' .
|
|
|
|
implode('</tr>'."\n".'<tr class="line">', $left_offsets).
|
|
|
|
'</tr>' ."\n".
|
|
|
|
'</table>' ."\n";
|
|
|
|
|
|
|
|
$inner_linecounts_right =
|
|
|
|
'<table class="diff-linecounts">' ."\n".
|
|
|
|
'<colgroup><col width="'.$rightwidth.'" /></colgroup>' ."\n".
|
|
|
|
'<tr class="line">' .
|
|
|
|
implode('</tr>'."\n".'<tr class="line">', $right_offsets).
|
|
|
|
'</tr>' ."\n".
|
|
|
|
'</table>' ."\n";
|
|
|
|
|
|
|
|
$inner_contents_left =
|
|
|
|
'<table class="diff-contents">' ."\n".
|
|
|
|
'<tr class="line">' .
|
|
|
|
implode('</tr>'."\n".'<tr class="line">', $left_contents) .
|
|
|
|
'</tr>' ."\n".
|
|
|
|
'</table>' ."\n";
|
|
|
|
|
|
|
|
$inner_contents_right =
|
|
|
|
'<table class="diff-contents">' ."\n".
|
|
|
|
'<tr class="line">' .
|
|
|
|
implode('</tr>'."\n".'<tr class="line">', $right_contents) .
|
|
|
|
'</tr>' ."\n".
|
|
|
|
'</table>' ."\n";
|
|
|
|
|
|
|
|
$out =
|
|
|
|
'<table class="diff context">' ."\n".
|
|
|
|
'<colgroup>' .
|
|
|
|
'<col width="'.($leftwidth + 1).'" /><col width="*" />' .
|
|
|
|
'<col width="'.($rightwidth + 1).'" /><col width="*" />' .
|
|
|
|
'</colgroup>' ."\n".
|
|
|
|
'<tr id="diff-'.md5($filename).'">'.
|
|
|
|
'<th colspan="4">'.Pluf_esc($filename).'</th>'.
|
|
|
|
'</tr>' ."\n".
|
|
|
|
'<tr>' .
|
|
|
|
'<th colspan="2">'.__('Old').'</th><th colspan="2">'.__('New').'</th>' .
|
|
|
|
'</tr>'.
|
|
|
|
'<tr>' .
|
|
|
|
'<td>'. $inner_linecounts_left .'</td>'. "\n".
|
|
|
|
'<td><div class="scroll">'. $inner_contents_left .'</div></td>'. "\n".
|
|
|
|
'<td>'. $inner_linecounts_right .'</td>'. "\n".
|
|
|
|
'<td><div class="scroll">'. $inner_contents_right .'</div></td>'. "\n".
|
|
|
|
'</tr>' ."\n".
|
|
|
|
'</table>' ."\n";
|
|
|
|
|
2008-11-30 09:26:05 +00:00
|
|
|
return Pluf_Template::markSafe($out);
|
|
|
|
}
|
2008-12-30 17:21:04 +00:00
|
|
|
}
|