Indefero

Indefero Git Source Tree


Root/src/IDF/Diff.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?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.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# 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
{
    public $repo = '';
    public $diff = '';
    protected $lines = array();
 
    public $files = array();
 
    public function __construct($diff, $repo='')
    {
        $this->repo = $repo;
        $this->diff = $diff;
        $this->lines = preg_split("/\015\012|\015|\012/", $diff);
    }
 
    public function parse()
    {
        $current_file = '';
        $current_chunk = 0;
        $lline = 0;
        $rline = 0;
        $files = array();
        foreach ($this->lines as $line) {
            if (0 === strpos($line, 'diff --git a')) {
                $current_file = self::getFile($line);
                $files[$current_file] = array();
                $files[$current_file]['chunks'] = array();
                $files[$current_file]['chunks_def'] = array();
                $current_chunk = 0;
                continue;
            }
            if (0 === strpos($line, '@@ ')) {
                $files[$current_file]['chunks_def'][] = self::getChunk($line);
                $files[$current_file]['chunks'][] = array();
                $current_chunk++;
                $lline = $files[$current_file]['chunks_def'][$current_chunk-1][0][0];
                $rline = $files[$current_file]['chunks_def'][$current_chunk-1][1][0];
                continue;
            }
            if (0 === strpos($line, '---') or 0 === strpos($line, '+++')) {
                continue;
            }
            if (0 === strpos($line, '-')) {
                $files[$current_file]['chunks'][$current_chunk-1][] = array($lline, '', substr($line, 1));
                $lline++;
                continue;
            }
            if (0 === strpos($line, '+')) {
                $files[$current_file]['chunks'][$current_chunk-1][] = array('', $rline, substr($line, 1));
                $rline++;
                continue;
            }
            if (0 === strpos($line, ' ')) {
                $files[$current_file]['chunks'][$current_chunk-1][] = array($lline, $rline, substr($line, 1));
                $rline++;
                $lline++;
                continue;
            }
        }
        $this->files = $files;
        return $files;
    }
 
    public static function getFile($line)
    {
        $line = substr(trim($line), 10);
        $n = (int) strlen($line)/2;
        return trim(substr($line, 3, $n-3));
    }
 
    /**
     * Return the html version of a parsed diff.
     */
    public function as_html()
    {
        $out = '';
        foreach ($this->files as $filename=>$file) {
            $out .= "\n".'<table class="diff" summary="">'."\n";
            $out .= '<tr id="diff-'.md5($filename).'"><th colspan="3">'.Pluf_esc($filename).'</th></tr>'."\n";
            $cc = 1;
            foreach ($file['chunks'] as $chunk) {
                foreach ($chunk as $line) {
                    if ($line[0] and $line[1]) {
                        $class = 'diff-c';
                    } elseif ($line[0]) {
                        $class = 'diff-r';
                    } else {
                        $class = 'diff-a';
                    }
                    $line_content = $this->padLine(Pluf_esc($line[2]));
                    $out .= sprintf('<tr class="diff-line"><td class="diff-lc">%s</td><td class="diff-lc">%s</td><td class="%s mono">%s</td></tr>'."\n", $line[0], $line[1], $class, $line_content);
                }
                if (count($file['chunks']) > $cc)
                $out .= '<tr class="diff-next"><td>...</td><td>...</td><td> </td></tr>'."\n";
                $cc++;
            }
            $out .= '</table>';
        }
        return $out;
    }
 
 
    public function padLine($line)
    {
        $n = strlen($line);
        for ($i=0;$i<$n;$i++) {
            if (substr($line, $i, 1) != ' ') {
                break;
            }
        }
        return str_repeat(' ', $i).substr($line, $i);
    }
 
    /**
     * @return array array(array(start, n), array(start, n))
     */
    public static function getChunk($line)
    {
        $elts = split(' ', $line);
        $res = array();
        for ($i=1;$i<3;$i++) {
            $res[] = split(',', trim(substr($elts[$i], 1)));
        }
        return $res;
    }
 
}

Archive Download this file

Page rendered in 0.11424s using 11 queries.