added commit difftree
[phpgitweb.git] / htdocs / pages / commit.class.php
1 <?php
2 /* commit.class.php - phpgitweb
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
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.
9  * 
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.
14  * 
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/>. 
17  */
18
19 require_once('pages/shortlog.class.php');
20
21 class difftree {
22         
23         public function generate_difftree($project, $commit, $patch_link) {
24                 $difftree = new ContentProvider('commit', 'difftree');
25                 $tree = GitCommand::get_commit_changes($project['path'], $commit['id'], $commit['parent']);
26                 $entry_count = 0;
27                 if(count($commit['parent']) > 1)
28                         $difftree->set('class', ' combined');
29                 else
30                         $difftree->set('class', '');
31                 foreach($tree as $entry) {
32                         $entry_count++;
33                         $difftree->append('tree', $this->tree_entry($entry_count, (($entry_count % 2) ? 'dark' : 'light'), $commit, $entry, $patch_link));
34                 }
35                 if(count($tree) == 0)
36                         $difftree->set('tree', '');
37                 return $difftree;
38         }
39         
40         private function tree_entry($diffid, $class, $commit, $entry, $patch_link) {
41                 $tree = new ContentProvider('commit', 'tree');
42                 $tree->set('class', $class);
43                 $tree->set('hash', $commit['id']);
44                 $tree->set('file', $entry['file']);
45                 
46                 if(count($commit['parent']) > 1) {
47                         if(preg_match('/^[0]{40}$/', $entry['to_id'])) //file doesn't exist in the result (child) commit
48                                 $tree->set('file', $entry['file']);
49                         else
50                                 $tree->set('file', new ContentProvider('commit', 'tree_file_link', array('file' => $entry['file'])));
51                         
52                         $tree->set('specials', '');
53                         
54                         if($patch_link)
55                                 $tree->append('merge', new ContentProvider('commit', 'tree_merge_patch_link', array("patch_marker" => "patch".$diffid)));
56                         
57                         $has_history = false;
58                         $not_deleted = false;
59                         
60                         for($i = 0; $i < count($commit['parent']); $i++) {
61                                 $status = $entry['status'][$i];
62                                 $merge = null;
63                                 
64                                 $has_history = ($has_history || ($status != 'A'));
65                                 $not_deleted = ($not_deleted || ($status != 'D'));
66                                 
67                                 switch($status) {
68                                 case 'A': //Added
69                                         $merge = new ContentProvider('commit', 'tree_merge_new');
70                                         break;
71                                 case 'D': //Deleted
72                                         $merge = new ContentProvider('commit', 'tree_merge');
73                                         $merge->set('class', '');
74                                         $blob_link = new ContentProvider('commit', 'tree_merge_blob', array('hash' => $commit['parent'][$i], 'file' => $entry['file']));
75                                         $merge->set('links', array($blob_link, ' | '));
76                                         break;
77                                 default:
78                                         $merge = new ContentProvider('commit', 'tree_merge');
79                                         if($entry['from_id'][$i] == $entry['to_id']) {
80                                                 $merge->set('class', ' nochange');
81                                                 $merge->set('links', ' | ');
82                                         } else {
83                                                 $merge->set('class', '');
84                                                 $merge->set('links', new ContentProvider('commit', 'tree_merge_diff', array('hash' => $commit['id'], 'parent' => $commit['parent'][$i], 'file' => $entry['file'], 'id' => ($i + 1))));
85                                         }
86                                 }
87                                 $tree->append('merge', $merge);
88                         }
89                         
90                         $tree->set('links', '');
91                         if($not_deleted) {
92                                 $tree->append('links', new ContentProvider('commit', 'tree_merge_blob', array('hash' => $commit['id'], 'file' => $entry['file'])));
93                                 if($has_history)
94                                         $tree->append('links', ' | ');
95                         }
96                         if($has_history)
97                                 $tree->append('links', new ContentProvider('commit', 'tree_merge_history', array('hash' => $commit['id'], 'file' => $entry['file'])));
98                         
99                 } else {
100                         $tree->set('file', new ContentProvider('commit', 'tree_file_link', array('file' => $entry['file'])));
101                         $tree->set('merge', '');
102                         
103                         $from_type = Tools::get_filetype($entry['from_mode']);
104                         $to_type = Tools::get_filetype($entry['to_mode']);
105                         
106                         $from_mode = (Tools::is_regular_file($entry['from_mode']) ? Tools::get_file_permissions($entry['from_mode']) : null);
107                         $to_mode = (Tools::is_regular_file($entry['to_mode']) ? Tools::get_file_permissions($entry['to_mode']) : null);
108                         
109                         $link_placeholders = array(
110                                         "hash" => $commit['id'],
111                                         "file" => $entry['file'],
112                                         "parent" => (count($commit['parent']) ? $commit['parent'][0] : ""),
113                                 );
114                         if($patch_link)
115                                 $tree->append('links', new ContentProvider('commit', 'tree_patch_link', array("patch_marker" => "patch".$diffid)));
116                         
117                         switch($entry['status']) {
118                         case 'A': //Added
119                                 $tree->set('specials', new ContentProvider('commit', (Tools::is_regular_file($entry['to_mode']) ? 'tree_new_file' : 'tree_new'), array('type' => $to_type, 'mode' => $to_mode)));
120                                 $tree->append('links', new ContentProvider('commit', 'tree_new_links', $link_placeholders));
121                                 break;
122                         case 'D': //Deleted
123                                 $tree->set('specials', new ContentProvider('commit', 'tree_deleted', array('type' => $from_type)));
124                                 $tree->append('links', new ContentProvider('commit', 'tree_deleted_links', $link_placeholders));
125                                 break;
126                         case 'M': //Modified
127                         case 'T': //Type changed
128                                 if($entry['from_mode'] != $entry['to_mode']) {
129                                         $modified = new ContentProvider('commit', 'tree_changed');
130                                         $tree->set('specials', $modified);
131                                         if($from_type != $to_type)
132                                                 $modified->append('changes', new ContentProvider('commit', 'tree_changed_type', array('from' => $from_type, 'to' => $to_type)));
133                                         if($from_mode != $to_mode && $to_mode) {
134                                                 if($from_mode)
135                                                         $modified->append('changes', new ContentProvider('commit', 'tree_changed_mode', array('from' => $from_mode, 'to' => $to_mode)));
136                                                 else
137                                                         $modified->append('changes', new ContentProvider('commit', 'tree_changed_mode_to', array('to' => $to_mode)));
138                                         }
139                                 } else
140                                         $tree->set('specials', '');
141                                 if($entry['from_id'] != $entry['to_id'])
142                                         $tree->append('links', new ContentProvider('commit', 'tree_changed_links_diff', $link_placeholders));
143                                 $tree->append('links', new ContentProvider('commit', 'tree_changed_links', $link_placeholders));
144                                 break;
145                         case 'R': //Renamed
146                         case 'C': //Copied
147                                 $actions = array('R' => 'tree_moved', 'C' => 'tree_copied');
148                                 $move = new ContentProvider('commit', $actions[$entry['status']]);
149                                 $tree->set('specials', $move);
150                                 $tree->set('file', $entry['to_file']);
151                                 $move->set('file', $entry['from_file']);
152                                 $move->set('hash', $commit['parent'][0]);
153                                 $move->set('similarity', $entry['similarity']);
154                                 if($from_mode != $to_mode)
155                                         $move->set('mode', new ContentProvider('commit', 'tree_moved_mode', array('mode' => $to_mode)));
156                                 else
157                                         $move->set('mode', '');
158                                 if($entry['from_id'] != $entry['to_id'])
159                                         $tree->append('links', new ContentProvider('commit', 'tree_moved_links_diff', $link_placeholders));
160                                 $tree->append('links', new ContentProvider('commit', 'tree_moved_links', $link_placeholders));
161                                 break;
162                         default:
163                                 
164                         }
165                         
166                 }
167                 return $tree;
168         }
169         
170 }
171
172 class page_commit {
173     private $page, $phpgitweb;
174         private $commitid;
175         
176     public function main($phpgitweb, $project) {
177                 $this->phpgitweb = $phpgitweb;
178                 if(!$project)
179                         return new ContentProvider('main', 'err400');
180                 $project['refs'] = $phpgitweb->get_project_loader()->getProjectRefs($project);
181                 $phpgitweb->append_header_nav("commit", null, true);
182                 $phpgitweb->append_title("commit");
183                 
184                 $commit_loader = new CommitLoader($project);
185                 
186                 if(array_key_exists('h', $_GET))
187                         $commitid = $_GET['h'];
188                 else
189                         $commitid = "HEAD";
190                 
191                 $commit = $commit_loader->load($commitid);
192                 if(!$commit)
193                         return new ContentProvider('main', 'err404_object');
194                 
195                 ContentProvider::overall_set('project_head', $commit['id']);
196                 
197                 
198                 $this->page = new ContentProvider('commit', 'main');
199                 $this->page->set('hash', $commit['id']);
200                 $this->page->set('author', htmlentities($commit['author']));
201                 $this->page->set('author_mail', htmlentities($commit['author_mail']));
202                 $this->page->set('author_date', gmdate('r', $commit['author_time']));
203                 $author_local_time = $commit['author_time'] + Tools::parseTimeZone($commit['author_timezone']);
204                 if(gmdate('H', $author_local_time) < 6)
205                         $this->page->set('author_local_date', '<span class="atnight">'.gmdate('H:i', $author_local_time).'</span>');
206                 else
207                         $this->page->set('author_local_date', gmdate('H:i', $author_local_time));
208                 $this->page->set('author_timezone', $commit['author_timezone']);
209                 $this->page->set('committer', htmlentities($commit['committer']));
210                 $this->page->set('committer_mail', htmlentities($commit['committer_mail']));
211                 $this->page->set('committer_date', gmdate('r', $commit['committer_time']));
212                 $committer_local_time = $commit['committer_time'] + Tools::parseTimeZone($commit['committer_timezone']);
213                 if(gmdate('H', $committer_local_time) < 6)
214                         $this->page->set('committer_local_date', '<span class="atnight">'.gmdate('H:i', $committer_local_time).'</span>');
215                 else
216                         $this->page->set('committer_local_date', gmdate('H:i', $committer_local_time));
217                 $this->page->set('committer_timezone', $commit['committer_timezone']);
218                 $this->page->set('message', htmlentities(Tools::chop_text($commit['text'], 50, 5)));
219                 $this->page->set('full_message', htmlentities($commit['text']));
220                 $this->page->set('tree_hash', $commit['tree']);
221                 foreach($commit['parent'] as $parent) {
222                         $this->page->append('parents', new ContentProvider('commit', 'parent', array('hash' => $parent, 'head' => $commit['id'])));
223                 }
224                 if(count($commit['parent']) == 0)
225                         $this->page->set('parents', '');
226                 
227                 $refs = new ContentProvider('commit', 'commit_refs');
228                 $found = false;
229                 foreach($project['refs'] as $ref => $rhash) {
230                         if(strtolower($rhash) == strtolower($commit['id'])) {
231                                 $refexp = explode('/', $ref, 3);
232                                 $reftype = $refexp[1];
233                                 if($reftype == 'heads')
234                                         $reftype = 'head';
235                                 else if($reftype == 'remotes')
236                                         $reftype = 'remote';
237                                 else if($reftype == 'tags')
238                                         $reftype = 'tag';
239                                 $refs->append('refs', new ContentProvider('commit', 'commit_ref_'.$reftype, array("name" => $refexp[2], "ref_link" => $ref)));
240                                 $found = true;
241                         }
242                 }
243                 $this->page->set('refs', ($found ? $refs : ""));
244                 
245                 $difftree = new difftree();
246                 $this->page->set('difftree', $difftree->generate_difftree($project, $commit, false));
247                 
248                 return $this->page;
249         }
250         
251         
252         
253 }
254
255 ?>