2 /* PageClasses.diff.class.php - phpgitweb
3 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 private $commit, $tree, $diff, $data = array();
22 public function __construct($commit, $tree, $diff) {
23 $this->commit = $commit;
28 public static function generate_html($commit, $diff_data) {
32 foreach($diff_data['tree'] as $tree) {
34 if(array_key_exists($diff_id, $diff_data['diffs']))
35 $diff = $diff_data['diffs'][$diff_id];
38 if($diff && preg_match('/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/i', $diff[0], $matches))
39 $to_file = $matches[2];
40 else if($diff && preg_match('/^diff --(cc|combined) ("?.*"?)$/i', $diff[0], $matches))
41 $to_file = $matches[2];
44 if(!$to_file || strtolower($to_file) != $tree['to_file']) {
45 if($tree['parents'] > 1) {
46 //jump over missing diffs
47 $diffobj = new diff($commit, $tree, null);
48 $html[] = $diffobj->gen_contentprovider($patch_id);
55 $diffobj = new diff($commit, $tree, $diff);
56 $html[] = $diffobj->gen_contentprovider($patch_id);
59 if(!array_key_exists($diff_id, $diff_data['diffs']))
61 $diff = $diff_data['diffs'][$diff_id];
62 if(preg_match('/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/i', $diff[0], $matches))
63 $to_file = $matches[2];
64 else if(preg_match('/^diff --(cc|combined) ("?.*"?)$/i', $diff[0], $matches))
65 $to_file = $matches[2];
68 if(!array_key_exists('to_file', $tree))
69 $tree['to_file'] = $tree['file'];
70 if(strtolower($to_file) != $tree['to_file'])
72 $diffobj = new diff($commit, $tree, $diff);
73 $html[] = $diffobj->gen_contentprovider(null);
83 private function gen_diffcmd_header() {
86 $header[] = "diff --cc ";
87 $header[] = new ContentProvider('commitdiff', 'patch_link_blob', array('hash' => $this->commit['id'], 'file' => $this->tree['to_file'], 'path' => ''));
88 } else if($this->tree['parents'] > 1) {
89 preg_match('!^(diff (.*?) )"?.*$!', $this->diff[0], $match);
90 $header[] = $match[1];
91 if(preg_match('/^[0]{40}$/', $this->tree['to_id']))
92 $header[] = $this->tree['to_file'];
94 $header[] = new ContentProvider('commitdiff', 'patch_link_blob', array('hash' => $this->commit['id'], 'file' => $this->tree['to_file'], 'path' => ''));
96 preg_match('!^(diff (.*?) )"?a/.*$!', $this->diff[0], $match);
97 $header[] = $match[1];
98 if(preg_match('/^[0]{40}$/', $this->tree['from_id']))
99 $header[] = 'a/'.$this->tree['file'];
101 $header[] = new ContentProvider('commitdiff', 'patch_link_blob', array('hash' => $this->commit['parent'][0], 'file' => $this->tree['from_file'], 'path' => 'a/'));
103 if(preg_match('/^[0]{40}$/', $this->tree['to_id']))
104 $header[] = 'b/'.$this->tree['file'];
106 $header[] = new ContentProvider('commitdiff', 'patch_link_blob', array('hash' => $this->commit['id'], 'file' => $this->tree['to_file'], 'path' => 'b/'));
111 private function gen_index_header($line) {
113 if(preg_match('/^((copy|rename) from ).*$/', $line, $matches)) {
114 $header[] = $matches[1];
115 if(array_key_exists('copy', $this->data))
116 $this->data['copy']++;
118 $this->data['copy'] = 0;
119 $header[] = new ContentProvider('commitdiff', 'patch_link_blob', array('hash' => $this->commit['parent'][$this->data['copy']], 'file' => $this->tree['from_file'], 'path' => ''));
120 } else if(preg_match('/^((copy|rename) to ).*$/', $line, $matches)) {
121 $header[] = $matches[1];
122 $header[] = new ContentProvider('commitdiff', 'patch_link_blob', array('hash' => $this->commit['id'], 'file' => $this->tree['to_file'], 'path' => ''));
123 } else if(preg_match('/^(index )[0-9a-fA-F]{40},[0-9a-fA-F]{40}/', $line, $matches)) { //combined diff
124 $header[] = $matches[1];
126 foreach($this->tree['from_id'] as $from_id) {
129 if(preg_match('/^[0]{40}$/', $from_id))
130 $header[] = '0000000';
132 $header[] = new ContentProvider('commitdiff', 'patch_link_blob_id', array('hash' => $this->commit['parent'][$parent_id], 'file' => $this->tree['from_file'], 'id' => substr($from_id, 0, 7)));
135 } else if(preg_match('/^(index )([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/', $line, $matches)) {
136 $header[] = $matches[1];
137 if(preg_match('/^[0]{40}$/', $matches[2]))
138 $header[] = '0000000';
140 $header[] = new ContentProvider('commitdiff', 'patch_link_blob_id', array('hash' => $this->commit['parent'][0], 'file' => $this->tree['from_file'], 'id' => substr($matches[2], 0, 7)));
142 if(preg_match('/^[0]{40}$/', $matches[3]))
143 $header[] = '0000000';
145 $header[] = new ContentProvider('commitdiff', 'patch_link_blob_id', array('hash' => $this->commit['id'], 'file' => $this->tree['to_file'], 'id' => substr($matches[3], 0, 7)));
148 if(preg_match('/([0-7]{6})$/', $line, $matches)) {
149 $ftype = Tools::get_filetype($matches[1], true);
150 $header[] = new ContentProvider('commitdiff', 'patch_fileinfo', array('info' => $ftype));
155 private function gen_contentprovider($patch_id) {
157 $pageclass = 'patch_nodiff';
159 $pageclass = 'patch_continued';
161 $pageclass = 'patch_normal';
162 $page = new ContentProvider('commitdiff', $pageclass);
163 $page->set('diffcmd', $this->gen_diffcmd_header());
167 $first_header = true;
168 for($i = 1; $i < count($this->diff); $i++) {
169 $line = $this->diff[$i];
171 if(preg_match('/^--- |^diff /', $line))
175 $page->append('header', '<br />');
176 $page->append('header', $this->gen_index_header($line));
177 $first_header = false;
181 $lineobj = new ContentProvider('commitdiff', 'patch_diffline');
182 //if(preg_match('', $line))
185 $page->set('patch', '');