e738ea2ba44518d346ffee94d23216324078e075
[git.git] / gitweb.cgi
1 #!/usr/bin/perl
2
3 # gitweb.pl - simple web interface to track changes in git repositories
4 #
5 # (C) 2005, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke <ch@gierke.de>
7 #
8 # This program is licensed under the GPL v2, or a later version
9
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Fcntl ':mode';
16
17 my $cgi = new CGI;
18 my $version =           "148";
19 my $my_url =            $cgi->url();
20 my $my_uri =            $cgi->url(-absolute => 1);
21 my $rss_link = "";
22
23 # absolute fs-path which will be prepended to the project path
24 my $projectroot =       "/pub/scm";
25
26 # location of the git-core binaries
27 my $gitbin =            "/usr/bin";
28
29 # location for temporary files needed for diffs
30 my $gittmp =            "/tmp/gitweb";
31
32 # target of the home link on top of all pages
33 my $home_link =         $my_uri;
34
35 # html text to include at home page
36 my $home_text =         "indextext.html";
37
38 # source of projects list
39 #my $projects_list = $projectroot;
40 my $projects_list = "index/index.aux";
41
42 # input validation and dispatch
43 my $action = $cgi->param('a');
44 if (defined $action) {
45         if ($action =~ m/[^0-9a-zA-Z\.\-]+/) {
46                 undef $action;
47                 die_error(undef, "Invalid action parameter.");
48         }
49         if ($action eq "git-logo.png") {
50                 git_logo();
51                 exit;
52         }
53 } else {
54         $action = "summary";
55 }
56
57 my $project = $cgi->param('p');
58 if (defined $project) {
59         if ($project =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
60                 undef $project;
61                 die_error(undef, "Non-canonical project parameter.");
62         }
63         if ($project =~ m/[^a-zA-Z0-9_\.\/\-\+\#\~]/) {
64                 undef $project;
65                 die_error(undef, "Invalid character in project parameter.");
66         }
67         if (!(-d "$projectroot/$project")) {
68                 undef $project;
69                 die_error(undef, "No such directory.");
70         }
71         if (!(-e "$projectroot/$project/HEAD")) {
72                 undef $project;
73                 die_error(undef, "No such project.");
74         }
75         $rss_link = "<link rel=\"alternate\" title=\"$project log\" href=\"$my_uri?p=$project;a=rss\" type=\"application/rss+xml\"/>";
76         $ENV{'GIT_OBJECT_DIRECTORY'} = "$projectroot/$project/objects";
77         $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$project/objects";
78 } else {
79         git_project_list();
80         exit;
81 }
82
83 my $file_name = $cgi->param('f');
84 if (defined $file_name) {
85         if ($file_name =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
86                 undef $file_name;
87                 die_error(undef, "Non-canonical file parameter.");
88         }
89         if ($file_name =~ m/[^a-zA-Z0-9_\.\/\-\+\#\~\:\!]/) {
90                 undef $file_name;
91                 die_error(undef, "Invalid character in file parameter.");
92         }
93 }
94
95 my $hash = $cgi->param('h');
96 if (defined $hash && !($hash =~ m/^[0-9a-fA-F]{40}$/)) {
97         undef $hash;
98         die_error(undef, "Invalid hash parameter.");
99 }
100
101 my $hash_parent = $cgi->param('hp');
102 if (defined $hash_parent && !($hash_parent =~ m/^[0-9a-fA-F]{40}$/)) {
103         undef $hash_parent;
104         die_error(undef, "Invalid hash_parent parameter.");
105 }
106
107 my $hash_base = $cgi->param('hb');
108 if (defined $hash_base && !($hash_base =~ m/^[0-9a-fA-F]{40}$/)) {
109         undef $hash_base;
110         die_error(undef, "Invalid parent hash parameter.");
111 }
112
113 my $time_back = $cgi->param('t');
114 if (defined $time_back) {
115         if ($time_back =~ m/^[^0-9]+$/) {
116                 undef $time_back;
117                 die_error(undef, "Invalid time parameter.");
118         }
119 }
120
121 if ($action eq "summary") {
122         git_summary();
123         exit;
124 } elsif ($action eq "tags") {
125         git_tags();
126         exit;
127 } elsif ($action eq "blob") {
128         git_blob();
129         exit;
130 } elsif ($action eq "tree") {
131         git_tree();
132         exit;
133 } elsif ($action eq "rss") {
134         git_rss();
135         exit;
136 } elsif ($action eq "commit") {
137         git_commit();
138         exit;
139 } elsif ($action eq "log") {
140         git_log();
141         exit;
142 } elsif ($action eq "blobdiff") {
143         git_blobdiff();
144         exit;
145 } elsif ($action eq "commitdiff") {
146         git_commitdiff();
147         exit;
148 } elsif ($action eq "history") {
149         git_history();
150         exit;
151 } else {
152         undef $action;
153         die_error(undef, "Unknown action.");
154         exit;
155 }
156
157 sub git_header_html {
158         my $status = shift || "200 OK";
159
160         my $title = "git";
161         if (defined $project) {
162                 $title .= " - $project";
163                 if (defined $action) {
164                         $title .= "/$action";
165                 }
166         }
167         print $cgi->header(-type=>'text/html',  -charset => 'utf-8', -status=> $status);
168         print <<EOF;
169 <?xml version="1.0" encoding="utf-8"?>
170 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
171 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
172 <!-- git web interface v$version, (C) 2005, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke <ch\@gierke.de> -->
173 <head>
174 <title>$title</title>
175 $rss_link
176 <style type="text/css">
177 body { font-family: sans-serif; font-size: 12px; margin:0px; border:solid #d9d8d1; border-width:1px; margin:10px; }
178 a { color:#0000cc; }
179 a:hover, a:visited, a:active { color:#880000; }
180 div.page_header { height:25px; padding:8px; font-size:18px; font-weight:bold; background-color:#d9d8d1; }
181 div.page_header a:visited { color:#0000cc; }
182 div.page_header a:hover { color:#880000; }
183 div.page_nav { padding:8px; }
184 div.page_nav a:visited { color:#0000cc; }
185 div.page_path { font-weight:bold; padding:8px; border:solid #d9d8d1; border-width:0px 0px 1px}
186 div.page_footer { height:17px; padding:4px 8px; background-color: #d9d8d1; }
187 div.page_footer_text { float:left; color:#555555; font-style:italic; }
188 div.page_body { padding:8px; }
189 div.title, a.title {
190         display:block; padding:6px 8px;
191         font-weight:bold; background-color:#edece6; text-decoration:none; color:#000000;
192 }
193 a.title:hover { background-color: #d9d8d1; }
194 div.title_text { padding:6px 8px; border: solid #d9d8d1; border-width:0px 0px 1px; }
195 div.log_body { padding:8px 8px 8px 150px; }
196 span.log_age { position:relative; float:left; width:142px; font-style:italic; }
197 div.log_link {
198         font-size:10px; font-family:sans-serif; font-style:normal;
199         position:relative; float:left; width:142px;
200 }
201 div.list { display:block; padding:4px 8px 2px; }
202 div.list_head {
203         display:block; padding:6px 6px 4px; border:solid #d9d8d1;
204         border-width:0px 0px 1px; font-style:italic;
205 }
206 div.list a { text-decoration:none; color:#000000; }
207 div.list a:hover { color:#880000; }
208 div.list_link {
209         padding:4px 8px 6px; border:solid #d9d8d1; border-width:0px 0px 1px;
210         font-family:sans-serif; font-size:10px;
211 }
212 td { padding:5px 15px 0px 0px; font-size:12px; }
213 th { padding-right:10px; font-size:12px; text-align:left; }
214 td.link { font-family:sans-serif; font-size:10px; }
215 td.pre { font-family:monospace; font-size:12px; white-space:pre; padding:2px 15px 0px 0px; }
216 div.pre { font-family:monospace; font-size:12px; white-space:pre; }
217 div.diff_info { font-family:monospace; color:#000099; background-color:#edece6; font-style:italic; }
218 div.index_include { border:solid #d9d8d1; border-width:0px 0px 1px; padding:12px 8px; }
219 a.rss_logo { float:right; padding:3px 0px; width:35px; line-height:10px;
220         border:1px solid; border-color:#fcc7a5 #7d3302 #3e1a01 #ff954e;
221         color:#ffffff; background-color:#ff6600;
222         font-weight:bold; font-family:sans-serif; font-size:10px;
223         text-align:center; text-decoration:none;
224 }
225 a.rss_logo:hover { background-color:#ee5500; }
226 </style>
227 </head>
228 <body>
229 EOF
230         print "<div class=\"page_header\">\n" .
231               "<a href=\"http://kernel.org/pub/software/scm/git/docs/\">" .
232               "<img src=\"$my_uri?a=git-logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/></a>";
233         print $cgi->a({-href => $home_link}, "projects") . " / ";
234         if (defined $project) {
235                 print $cgi->a({-href => "$my_uri?p=$project;a=summary"}, escapeHTML($project));
236                 if (defined $action) {
237                         print " / $action";
238                 }
239         }
240         print "</div>\n";
241 }
242
243 sub git_footer_html {
244         print "<div class=\"page_footer\">\n";
245         if (defined $project) {
246                 my $descr = git_read_description($project);
247                 if (defined $descr) {
248                         print "<div class=\"page_footer_text\">" . escapeHTML($descr) . "</div>\n";
249                 }
250                 print $cgi->a({-href => "$my_uri?p=$project;a=rss", -class => "rss_logo"}, "RSS") . "\n";
251         }
252         print "</div>\n" .
253               "</body>\n" .
254               "</html>";
255 }
256
257 sub die_error {
258         my $status = shift || "403 Forbidden";
259         my $error = shift || "Malformed query, file missing or permission denied"; 
260
261         git_header_html($status);
262         print "<div class=\"page_body\">\n" .
263               "<br/><br/>\n";
264         print "$status - $error\n";
265         print "<br/></div>\n";
266         git_footer_html();
267         exit;
268 }
269
270 sub git_get_type {
271         my $hash = shift;
272
273         open my $fd, "-|", "$gitbin/git-cat-file -t $hash" || return;
274         my $type = <$fd>;
275         close $fd;
276         chomp $type;
277         return $type;
278 }
279
280 sub git_read_hash {
281         my $path = shift;
282
283         open my $fd, "$projectroot/$path" || return undef;
284         my $head = <$fd>;
285         close $fd;
286         chomp $head;
287         if ($head =~ m/^[0-9a-fA-F]{40}$/) {
288                 return $head;
289         }
290 }
291
292 sub git_read_description {
293         my $path = shift;
294
295         open my $fd, "$projectroot/$path/description" || return undef;
296         my $descr = <$fd>;
297         close $fd;
298         chomp $descr;
299         return $descr;
300 }
301
302 sub git_read_tag {
303         my $tag_id = shift;
304         my %tag;
305
306         open my $fd, "-|", "$gitbin/git-cat-file tag $tag_id" || return;
307         while (my $line = <$fd>) {
308                 chomp $line;
309                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
310                         $tag{'object'} = $1;
311                 } elsif ($line =~ m/^type (.*)$/) {
312                         $tag{'type'} = $1;
313                 } elsif ($line =~ m/^tag (.*)$/) {
314                         $tag{'name'} = $1;
315                 }
316         }
317         close $fd || return;
318         if (!defined $tag{'name'}) {
319                 return
320         };
321         return %tag
322 }
323
324 sub git_read_commit {
325         my $commit = shift;
326         my %co;
327         my @parents;
328
329         open my $fd, "-|", "$gitbin/git-cat-file commit $commit" || return;
330         while (my $line = <$fd>) {
331                 last if $line eq "\n";
332                 chomp $line;
333                 if ($line =~ m/^tree (.*)$/) {
334                         $co{'tree'} = $1;
335                 } elsif ($line =~ m/^parent (.*)$/) {
336                         push @parents, $1;
337                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
338                         $co{'author'} = $1;
339                         $co{'author_epoch'} = $2;
340                         $co{'author_tz'} = $3;
341                         $co{'author_name'} = $co{'author'};
342                         $co{'author_name'} =~ s/ <.*//;
343                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
344                         $co{'committer'} = $1;
345                         $co{'committer_epoch'} = $2;
346                         $co{'committer_tz'} = $3;
347                         $co{'committer_name'} = $co{'committer'};
348                         $co{'committer_name'} =~ s/ <.*//;
349                 }
350         }
351         if (!defined $co{'tree'}) {
352                 close $fd;
353                 return undef
354         };
355         $co{'parents'} = \@parents;
356         $co{'parent'} = $parents[0];
357         my (@comment) = map { chomp; $_ } <$fd>;
358         $co{'comment'} = \@comment;
359         $comment[0] =~ m/^(.{0,60}[^ ]*)/;
360         $co{'title'} = $1;
361         if ($comment[0] ne $co{'title'}) {
362                 $co{'title'} .= " ...";
363         }
364         close $fd || return;
365
366         my $age = time - $co{'committer_epoch'};
367         $co{'age'} = $age;
368         if ($age > 60*60*24*365*2) {
369                 $co{'age_string'} = (int $age/60/60/24/365);
370                 $co{'age_string'} .= " years ago";
371         } elsif ($age > 60*60*24*(365/12)*2) {
372                 $co{'age_string'} = int $age/60/60/24/(365/12);
373                 $co{'age_string'} .= " months ago";
374         } elsif ($age > 60*60*24*7*2) {
375                 $co{'age_string'} = int $age/60/60/24/7;
376                 $co{'age_string'} .= " weeks ago";
377         } elsif ($age > 60*60*24*2) {
378                 $co{'age_string'} = int $age/60/60/24;
379                 $co{'age_string'} .= " days ago";
380         } elsif ($age > 60*60*2) {
381                 $co{'age_string'} = int $age/60/60;
382                 $co{'age_string'} .= " hours ago";
383         } elsif ($age > 60*2) {
384                 $co{'age_string'} = int $age/60;
385                 $co{'age_string'} .= " minutes ago";
386         } elsif ($age > 2) {
387                 $co{'age_string'} = int $age;
388                 $co{'age_string'} .= " seconds ago";
389         } else {
390                 $co{'age_string'} .= " right now";
391         }
392         return %co;
393 }
394
395 sub git_diff_html {
396         my $from = shift;
397         my $from_name = shift;
398         my $to = shift;
399         my $to_name = shift;
400
401         my $from_tmp = "/dev/null";
402         my $to_tmp = "/dev/null";
403         my $pid = $$;
404
405         # create tmp from-file
406         if (defined $from) {
407                 $from_tmp = "$gittmp/gitweb_" . $$ . "_from";
408                 open my $fd2, "> $from_tmp";
409                 open my $fd, "-|", "$gitbin/git-cat-file blob $from";
410                 my @file = <$fd>;
411                 print $fd2 @file;
412                 close $fd2;
413                 close $fd;
414         }
415
416         # create tmp to-file
417         if (defined $to) {
418                 $to_tmp = "$gittmp/gitweb_" . $$ . "_to";
419                 open my $fd2, "> $to_tmp";
420                 open my $fd, "-|", "$gitbin/git-cat-file blob $to";
421                 my @file = <$fd>;
422                 print $fd2 @file;
423                 close $fd2;
424                 close $fd;
425         }
426
427         open my $fd, "-|", "/usr/bin/diff -u -p -L $from_name -L $to_name $from_tmp $to_tmp";
428         while (my $line = <$fd>) {
429                 chomp($line);
430                 my $char = substr($line, 0, 1);
431                 my $color = "";
432                 if ($char eq '+') {
433                         $color = " style=\"color:#008800;\"";
434                 } elsif ($char eq '-') {
435                         $color = " style=\"color:#cc0000;\"";
436                 } elsif ($char eq '@') {
437                         $color = " style=\"color:#990099;\"";
438                 } elsif ($char eq '\\') {
439                         # skip errors
440                         next;
441                 }
442                 print "<div class=\"pre\"$color>" . escapeHTML($line) . "</div>\n";
443         }
444         close $fd;
445
446         if (defined $from) {
447                 unlink($from_tmp);
448         }
449         if (defined $to) {
450                 unlink($to_tmp);
451         }
452 }
453
454 sub mode_str {
455         my $mode = oct shift;
456
457         if (S_ISDIR($mode & S_IFMT)) {
458                 return 'drwxr-xr-x';
459         } elsif (S_ISLNK($mode)) {
460                 return 'lrwxrwxrwx';
461         } elsif (S_ISREG($mode)) {
462                 # git cares only about the executable bit
463                 if ($mode & S_IXUSR) {
464                         return '-rwxr-xr-x';
465                 } else {
466                         return '-rw-r--r--';
467                 };
468         } else {
469                 return '----------';
470         }
471 }
472
473 sub file_type {
474         my $mode = oct shift;
475
476         if (S_ISDIR($mode & S_IFMT)) {
477                 return "directory";
478         } elsif (S_ISLNK($mode)) {
479                 return "symlink";
480         } elsif (S_ISREG($mode)) {
481                 return "file";
482         } else {
483                 return "unknown";
484         }
485 }
486
487 sub date_str {
488         my $epoch = shift;
489         my $tz = shift || "-0000";
490
491         my %date;
492         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
493         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
494         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
495         $date{'hour'} = $hour;
496         $date{'minute'} = $min;
497         $date{'mday'} = $mday;
498         $date{'day'} = $days[$wday];
499         $date{'month'} = $months[$mon];
500         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
501         $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
502
503         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
504         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
505         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
506         $date{'hour_local'} = $hour;
507         $date{'minute_local'} = $min;
508         $date{'tz_local'} = $tz;
509         return %date;
510 }
511
512 # git-logo (cached in browser for one day)
513 sub git_logo {
514         print $cgi->header(-type => 'image/png', -expires => '+1d');
515         # cat git-logo.png | hexdump -e '16/1 " %02x"  "\n"' | sed 's/ /\\x/g'
516         print   "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
517                 "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
518                 "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
519                 "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
520                 "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
521                 "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
522                 "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
523                 "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
524                 "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
525                 "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
526                 "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
527                 "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
528                 "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
529 }
530
531 sub get_file_owner {
532         my $path = shift;
533
534         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
535         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
536         if (!defined $gcos) {
537                 return undef;
538         }
539         my $owner = $gcos;
540         $owner =~ s/[,;].*$//;
541         return $owner;
542 }
543
544 sub git_project_list {
545         my @list;
546
547         if (-d $projects_list) {
548                 # search in directory
549                 my $dir = $projects_list;
550                 opendir my $dh, $dir || return undef;
551                 while (my $dir = readdir($dh)) {
552                         if (-e "$projectroot/$dir/HEAD") {
553                                 my $pr = {
554                                         path => $dir,
555                                 };
556                                 push @list, $pr
557                         }
558                 }
559                 closedir($dh);
560         } elsif (-f $projects_list) {
561                 # read from file:
562                 # 'git/git.git:Linus Torvalds'
563                 # 'linux/hotplug/udev.git'
564                 open my $fd , $projects_list || return undef;
565                 while (my $line = <$fd>) {
566                         chomp $line;
567                         my ($path, $owner) = split ' ', $line;
568                         $path = unescape($path);
569                         $owner = unescape($owner);
570                         if (!defined $path) {
571                                 next;
572                         }
573                         if (-e "$projectroot/$path/HEAD") {
574                                 my $pr = {
575                                         path => $path,
576                                         owner => $owner,
577                                 };
578                                 push @list, $pr
579                         }
580                 }
581                 close $fd;
582         }
583
584         if (!@list) {
585                 die_error(undef, "No project found.");
586         }
587         @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
588
589         git_header_html();
590         if (-f $home_text) {
591                 print "<div class=\"index_include\">\n";
592                 open (my $fd, $home_text);
593                 print <$fd>;
594                 close $fd;
595                 print "</div>\n";
596         }
597         print "<div class=\"page_body\"><br/>\n" .
598               "<table cellspacing=\"0\">\n" .
599               "<tr>\n" .
600               "<th>Project</th>\n" .
601               "<th>Description</th>\n" .
602               "<th>Owner</th>\n" .
603               "<th>last change</th>\n" .
604               "<th></th>\n" .
605               "</tr>\n";
606         foreach my $pr (@list) {
607                 my %proj = %$pr;
608                 my $head = git_read_hash("$proj{'path'}/HEAD");
609                 if (!defined $head) {
610                         next;
611                 }
612                 $ENV{'GIT_OBJECT_DIRECTORY'} = "$projectroot/$proj{'path'}/objects";
613                 $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$proj{'path'}/objects";
614                 my %co = git_read_commit($head);
615                 if (!%co) {
616                         next;
617                 }
618                 my $descr = git_read_description($proj{'path'}) || "";
619                 # get directory owner if not already specified
620                 if (!defined $proj{'owner'}) {
621                         $proj{'owner'} = get_file_owner("$projectroot/$proj{'path'}") || "";
622                 }
623                 print "<tr>\n" .
624                       "<td>" . $cgi->a({-href => "$my_uri?p=$proj{'path'};a=summary"}, escapeHTML($proj{'path'})) . "</td>\n" .
625                       "<td>$descr</td>\n" .
626                       "<td><i>$proj{'owner'}</i></td>\n";
627                 my $colored_age;
628                 if ($co{'age'} < 60*60*2) {
629                         $colored_age = "<span style =\"color: #009900;\"><b><i>$co{'age_string'}</i></b></span>";
630                 } elsif ($co{'age'} < 60*60*24*2) {
631                         $colored_age = "<span style =\"color: #009900;\"><i>$co{'age_string'}</i></span>";
632                 } else {
633                         $colored_age = "<i>$co{'age_string'}</i>";
634                 }
635                 print "<td>$colored_age</td>\n" .
636                       "<td class=\"link\">" .
637                       $cgi->a({-href => "$my_uri?p=$proj{'path'};a=summary"}, "summary") .
638                       " | " . $cgi->a({-href => "$my_uri?p=$proj{'path'};a=log"}, "log") .
639                       " | " . $cgi->a({-href => "$my_uri?p=$proj{'path'};a=tree"}, "tree") .
640                       "</td>\n" .
641                       "</tr>\n";
642         }
643         print "</table>\n" .
644               "<br/>\n" .
645               "</div>\n";
646         git_footer_html();
647 }
648
649 sub git_read_tags {
650         my @taglist;
651
652         opendir my $dh, "$projectroot/$project/refs/tags";
653         my @tags = grep !m/^\./, readdir $dh;
654         closedir($dh);
655         foreach my $tag_file (@tags) {
656                 my $tag_id = git_read_hash("$project/refs/tags/$tag_file");
657                 my $type = git_get_type($tag_id) || next;
658                 my %tag_item;
659                 my %co;
660                 if ($type eq "tag") {
661                         my %tag = git_read_tag($tag_id);
662                         if ($tag{'type'} eq "commit") {
663                                 %co = git_read_commit($tag{'object'});
664                         }
665                         $tag_item{'type'} = $tag{'type'};
666                         $tag_item{'name'} = $tag{'name'};
667                         $tag_item{'id'} = $tag{'object'};
668                 } elsif ($type eq "commit"){
669                         %co = git_read_commit($tag_id);
670                         $tag_item{'type'} = "commit";
671                         $tag_item{'name'} = $tag_file;
672                         $tag_item{'id'} = $tag_id;
673                 }
674                 $tag_item{'epoch'} = $co{'author_epoch'} || 0;
675                 $tag_item{'age'} = $co{'age_string'} || "unknown";
676
677                 push @taglist, \%tag_item;
678         }
679         # sort tags by age
680         @taglist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @taglist;
681         return \@taglist;
682 }
683
684 sub git_summary {
685         my $descr = git_read_description($project) || "none";
686         my $head = git_read_hash("$project/HEAD");
687         $ENV{'GIT_OBJECT_DIRECTORY'} = "$projectroot/$project/objects";
688         $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$project/objects";
689         my %co = git_read_commit($head);
690         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
691
692         my $owner;
693         if (-f $projects_list) {
694                 open (my $fd , $projects_list);
695                 while (my $line = <$fd>) {
696                         chomp $line;
697                         my ($pr, $ow) = split ' ', $line;
698                         $pr = unescape($pr);
699                         $ow = unescape($ow);
700                         if ($pr eq $project) {
701                                 $owner = $ow;
702                                 last;
703                         }
704                 }
705                 close $fd;
706         }
707         if (!defined $owner) {
708                 $owner = get_file_owner("$projectroot/$project");
709         }
710
711         git_header_html();
712         print "<div class=\"page_nav\">\n" .
713               $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
714               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree"}, "tree") .
715               "<br/><br/>\n" .
716               "</div>\n";
717         print "<div class=\"title\">project</div>\n";
718         print "<div class=\"page_body\">\n" .
719               "<table cellspacing=\"0\">\n" .
720               "<tr><td>description</td><td>" . escapeHTML($descr) . "</td></tr>\n" .
721               "<tr><td>owner</td><td>$owner</td></tr>\n" .
722               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
723               "</table>\n" .
724               "<br/></div>\n";
725         open my $fd, "-|", "$gitbin/git-rev-list --max-count=11 " . git_read_hash("$project/HEAD") || die_error(undef, "Open failed.");
726         my (@revlist) = map { chomp; $_ } <$fd>;
727         close $fd;
728         print "<div>\n" .
729               $cgi->a({-href => "$my_uri?p=$project;a=log", -class => "title"}, "recent commits") .
730               "</div>\n";
731         my $i = 10;
732         foreach my $commit (@revlist) {
733                 my %co = git_read_commit($commit);
734                 my %ad = date_str($co{'author_epoch'});
735                 print "<div class=\"list\">\n" .
736                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"},
737                       "<span class=\"log_age\">$co{'age_string'}</span>" . escapeHTML($co{'title'})) . "\n" .
738                       "</div>\n";
739                 if (--$i == 0) {
740                         print "<div class=\"list\">" . $cgi->a({-href => "$my_uri?p=$project;a=log"}, "...") . "</div>\n";
741                         last;
742                 }
743         }
744         print "<div class=\"list\"><br/></div>\n";
745
746         my $taglist = git_read_tags();
747         if (defined @$taglist) {
748                 print "<div>\n" .
749                       $cgi->a({-href => "$my_uri?p=$project;a=tags", -class => "title"}, "recent tags") .
750                       "</div>\n";
751                 my $i = 10;
752                 foreach my $entry (@$taglist) {
753                         my %tag = %$entry;
754                         print "<div class=\"list\">\n" .
755                               $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'id'}"},
756                               "<span class=\"log_age\">$tag{'age'}</span>" .  escapeHTML($tag{'name'})) . "\n" .
757                               "</div>\n";
758                         if (--$i == 0) {
759                                 print "<div class=\"list\">" . $cgi->a({-href => "$my_uri?p=$project;a=tags"}, "...") . "</div>\n";
760                                 last;
761                         }
762                 }
763                 print "<div class=\"list\"><br/></div>\n";
764         }
765         git_footer_html();
766 }
767
768 sub git_tags {
769         my $head = git_read_hash("$project/HEAD");
770         git_header_html();
771         print "<div class=\"page_nav\">\n" .
772               $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
773               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$head"}, "commit") .
774               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree"}, "tree") .
775               "<br/><br/>\n" .
776               "</div>\n";
777         my $taglist = git_read_tags();
778         print "<div>\n" .
779               $cgi->a({-href => "$my_uri?p=$project;a=summary", -class => "title"}, "tags") .
780               "</div>\n";
781         if (defined @$taglist) {
782                 foreach my $entry (@$taglist) {
783                         my %tag = %$entry;
784                         print "<div class=\"list\">\n" .
785                               $cgi->a({-href => "$my_uri?p=$project;a=$tag{'type'};h=$tag{'id'}"},
786                               "<span class=\"log_age\">$tag{'age'}</span>" .  escapeHTML($tag{'name'})) . "\n" .
787                               "</div>\n";
788                 }
789         }
790         print "<div class=\"list\"><br/></div>\n";
791         git_footer_html();
792 }
793
794 sub git_get_hash_by_path {
795         my $base = shift;
796         my $path = shift;
797
798         my $tree = $base;
799         my @parts = split '/', $path;
800         while (my $part = shift @parts) {
801                 open my $fd, "-|", "$gitbin/git-ls-tree $tree" || die_error(undef, "Open git-ls-tree failed.");
802                 my (@entries) = map { chomp; $_ } <$fd>;
803                 close $fd || return undef;
804                 foreach my $line (@entries) {
805                         #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
806                         $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
807                         my $t_mode = $1;
808                         my $t_type = $2;
809                         my $t_hash = $3;
810                         my $t_name = $4;
811                         if ($t_name eq $part) {
812                                 if (!(@parts)) {
813                                         return $t_hash;
814                                 }
815                                 if ($t_type eq "tree") {
816                                         $tree = $t_hash;
817                                 }
818                                 last;
819                         }
820                 }
821         }
822 }
823
824 sub git_blob {
825         if (!defined $hash && defined $file_name) {
826                 my $base = $hash_base || git_read_hash("$project/HEAD");
827                 $hash = git_get_hash_by_path($base, $file_name, "blob");
828         }
829         open my $fd, "-|", "$gitbin/git-cat-file blob $hash" || die_error(undef, "Open failed.");
830         my $base = $file_name || "";
831         git_header_html();
832         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
833                 print "<div class=\"page_nav\">\n" .
834                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
835                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
836                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree");
837                 if (defined $file_name) {
838                         print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base;f=$file_name"}, "history");
839                 }
840                 print "<br/><br/>\n" .
841                       "</div>\n";
842                 print "<div>" .
843                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) .
844                       "</div>\n";
845         } else {
846                 print "<div class=\"page_nav\">\n" .
847                       "<br/><br/></div>\n" .
848                       "<div class=\"title\">$hash</div>\n";
849         }
850         if (defined $file_name) {
851                 print "<div class=\"page_path\">/$file_name</div>\n";
852         }
853         print "<div class=\"page_body\">\n";
854         my $nr;
855         while (my $line = <$fd>) {
856                 chomp $line;
857                 $nr++;
858                 print "<div class=\"pre\">";
859                 printf "<span style=\"color:#999999;\">%4i</span>", $nr;
860                 print " " .escapeHTML($line) . "</div>\n";
861         }
862         close $fd || print "Reading blob failed.\n";
863         print "</div>";
864         git_footer_html();
865 }
866
867 sub git_tree {
868         if (!defined $hash) {
869                 $hash = git_read_hash("$project/HEAD");
870                 if (defined $file_name) {
871                         my $base = $hash_base || git_read_hash("$project/HEAD");
872                         $hash = git_get_hash_by_path($base, $file_name, "tree");
873                 }
874         }
875         if (!defined $hash_base) {
876                 $hash_base = git_read_hash("$project/HEAD");
877         }
878         open my $fd, "-|", "$gitbin/git-ls-tree $hash" || die_error(undef, "Open git-ls-tree failed.");
879         my (@entries) = map { chomp; $_ } <$fd>;
880         close $fd || die_error(undef, "Reading tree failed.");
881
882         git_header_html();
883         my $base_key = "";
884         my $file_key = "";
885         my $base = "";
886         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
887                 $base_key = ";hb=$hash_base";
888                 print "<div class=\"page_nav\">\n" .
889                       $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
890                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
891                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
892                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree") .
893                       "<br/><br/>\n" .
894                       "</div>\n";
895                 print "<div>\n" .
896                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
897                       "</div>\n";
898         } else {
899                 print "<div class=\"page_nav\">\n";
900                 print "<br/><br/></div>\n";
901                 print "<div class=\"title\">$hash</div>\n";
902         }
903         if (defined $file_name) {
904                 $base = "$file_name/";
905                 print "<div class=\"page_path\">/$file_name</div>\n";
906         } else {
907                 print "<div class=\"page_path\">/</div>\n";
908         }
909         print "<div class=\"page_body\">\n";
910         print "<table cellspacing=\"0\">\n";
911         foreach my $line (@entries) {
912                 #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
913                 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
914                 my $t_mode = $1;
915                 my $t_type = $2;
916                 my $t_hash = $3;
917                 my $t_name = $4;
918                 $file_key = ";f=$base$t_name";
919                 print "<tr>\n" .
920                       "<td class=\"pre\">" . mode_str($t_mode) . "</td>\n";
921                 if ($t_type eq "blob") {
922                         print "<td class=\"pre\">$t_name</td>\n";
923                         print "<td class=\"link\">" .
924                               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash" . $base_key . $file_key}, "blob") .
925                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base" . $file_key}, "history") .
926                               "</td>\n";
927                 } elsif ($t_type eq "tree") {
928                         print "<td class=\"pre\">" .
929                               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$t_hash" . $base_key . $file_key}, $t_name) .
930                               "</td>\n";
931                 }
932                 print "</tr>\n";
933         }
934         print "</table>\n" .
935               "</div>";
936         git_footer_html();
937 }
938
939 sub git_rss {
940         open my $fd, "-|", "$gitbin/git-rev-list --max-count=20 " . git_read_hash("$project/HEAD") || die_error(undef, "Open failed.");
941         my (@revlist) = map { chomp; $_ } <$fd>;
942         close $fd || die_error(undef, "Reading rev-list failed.");
943
944         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
945         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
946               "<rss version=\"0.91\">\n";
947         print "<channel>\n";
948         print "<title>$project</title>\n".
949               "<link> $my_url/$project/log</link>\n".
950               "<description>$project log</description>\n".
951               "<language>en</language>\n";
952
953         foreach my $commit (@revlist) {
954                 my %co = git_read_commit($commit);
955                 my %ad = date_str($co{'author_epoch'});
956                 print "<item>\n" .
957                       "\t<title>" . sprintf("%d %s %02d:%02d", $ad{'mday'}, $ad{'month'}, $ad{'hour'}, $ad{'minute'}) . " - " . escapeHTML($co{'title'}) . "</title>\n" .
958                       "\t<link> $my_url?p=$project;a=commit;h=$commit</link>\n" .
959                       "\t<description>";
960                 my $comment = $co{'comment'};
961                 foreach my $line (@$comment) {
962                         print escapeHTML($line) . "<br/>\n";
963                 }
964                 print "\t</description>\n" .
965                       "</item>\n";
966         }
967         print "</channel></rss>";
968 }
969
970 sub git_log {
971         my $head = git_read_hash("$project/HEAD");
972         my $limit_option = "";
973         if (!defined $time_back) {
974                 $limit_option = "--max-count=10";
975         } elsif ($time_back > 0) {
976                 my $date = time - $time_back*24*60*60;
977                 $limit_option = "--max-age=$date";
978         }
979         open my $fd, "-|", "$gitbin/git-rev-list $limit_option $head" || die_error(undef, "Open failed.");
980         my (@revlist) = map { chomp; $_ } <$fd>;
981         close $fd || die_error(undef, "Reading rev-list failed.");
982
983         git_header_html();
984         print "<div class=\"page_nav\">\n";
985         print $cgi->a({-href => "$my_uri?p=$project;a=log"}, "last 10") . " | " .
986               $cgi->a({-href => "$my_uri?p=$project;a=log;t=1"}, "day") . " | " .
987               $cgi->a({-href => "$my_uri?p=$project;a=log;t=7"}, "week") . " | " .
988               $cgi->a({-href => "$my_uri?p=$project;a=log;t=31"}, "month") . " | " .
989               $cgi->a({-href => "$my_uri?p=$project;a=log;t=365"}, "year") . " | " .
990               $cgi->a({-href => "$my_uri?p=$project;a=log;t=0"}, "all") . "<br/>\n";
991         print "<br/>\n" .
992               "</div>\n";
993
994         if (!@revlist) {
995                 my %co = git_read_commit($head);
996                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
997         }
998
999         foreach my $commit (@revlist) {
1000                 my %co = git_read_commit($commit);
1001                 next if !%co;
1002                 my %ad = date_str($co{'author_epoch'});
1003                 print "<div>\n" .
1004                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit", -class => "title"},
1005                       "<span class=\"log_age\">$co{'age_string'}</span>" . escapeHTML($co{'title'})) . "\n" .
1006                       "</div>\n";
1007                 print "<div class=\"title_text\">\n" .
1008                       "<div class=\"log_link\">\n" .
1009                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1010                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "commitdiff") .
1011                       "<br/>\n" .
1012                       "</div>\n" .
1013                       "<i>" . escapeHTML($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
1014                       "</div>\n" .
1015                       "<div class=\"log_body\">\n";
1016                 my $comment = $co{'comment'};
1017                 my $empty = 0;
1018                 foreach my $line (@$comment) {
1019                         if ($line =~ m/^(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1020                                 next;
1021                         }
1022                         if ($line eq "") {
1023                                 if ($empty) {
1024                                         next;
1025                                 }
1026                                 $empty = 1;
1027                         } else {
1028                                 $empty = 0;
1029                         }
1030                         print escapeHTML($line) . "<br/>\n";
1031                 }
1032                 if (!$empty) {
1033                         print "<br/>\n";
1034                 }
1035                 print "</div>\n";
1036         }
1037         git_footer_html();
1038 }
1039
1040 sub git_commit {
1041         my %co = git_read_commit($hash);
1042         if (!%co) {
1043                 die_error(undef, "Unknown commit object.");
1044         }
1045         my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1046         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1047
1048         my @difftree;
1049         if (defined $co{'parent'}) {
1050                 open my $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $hash" || die_error(undef, "Open failed.");
1051                 @difftree = map { chomp; $_ } <$fd>;
1052                 close $fd || die_error(undef, "Reading diff-tree failed.");
1053         } else {
1054                 # fake git-diff-tree output for initial revision
1055                 open my $fd, "-|", "$gitbin/git-ls-tree -r $hash" || die_error(undef, "Open failed.");
1056                 @difftree = map { chomp;  "+" . $_ } <$fd>;
1057                 close $fd || die_error(undef, "Reading ls-tree failed.");
1058         }
1059         git_header_html();
1060         print "<div class=\"page_nav\">\n" .
1061               $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1062               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit");
1063         if (defined $co{'parent'}) {
1064                 print " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff");
1065         }
1066         print " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") . "\n" .
1067               "<br/><br/></div>\n";
1068         if (defined $co{'parent'}) {
1069                 print "<div>\n" .
1070                       $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1071                       "</div>\n";
1072         } else {
1073                 print "<div>\n" .
1074                       $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1075                       "</div>\n";
1076         }
1077         print "<div class=\"title_text\">\n" .
1078               "<table cellspacing=\"0\">\n";
1079         print "<tr><td>author</td><td>" . escapeHTML($co{'author'}) . "</td></tr>\n".
1080               "<tr><td></td><td> $ad{'rfc2822'}";
1081         if ($ad{'hour_local'} < 6) {
1082                 printf(" (<span style=\"color: #cc0000;\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1083         } else {
1084                 printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1085         }
1086         print "</td></tr>\n";
1087         print "<tr><td>committer</td><td>" . escapeHTML($co{'committer'}) . "</td></tr>\n";
1088         print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1089         print "<tr><td>commit</td><td style=\"font-family: monospace;\">$hash</td></tr>\n";
1090         print "<tr><td>tree</td><td style=\"font-family: monospace;\">" .
1091               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, $co{'tree'}) . "</td></tr>\n";
1092         my $parents  = $co{'parents'};
1093         foreach my $par (@$parents) {
1094                 print "<tr><td>parent</td><td style=\"font-family: monospace;\">" .
1095                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$par"}, $par) . "</td></tr>\n";
1096         }
1097         print "</table>". 
1098               "</div>\n";
1099         print "<div class=\"page_body\">\n";
1100         my $comment = $co{'comment'};
1101         my $empty = 0;
1102         my $signed = 0;
1103         foreach my $line (@$comment) {
1104                 # print only one empty line
1105                 if ($line eq "") {
1106                         if ($empty || $signed) {
1107                                 next;
1108                         }
1109                         $empty = 1;
1110                 } else {
1111                         $empty = 0;
1112                 }
1113                 if ($line =~ m/^(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1114                         $signed = 1;
1115                         print "<span style=\"color: #888888\">" . escapeHTML($line) . "</span><br/>\n";
1116                 } else {
1117                         $signed = 0;
1118                         print escapeHTML($line) . "<br/>\n";
1119                 }
1120         }
1121         print "</div>\n";
1122         print "<div class=\"list_head\">\n";
1123         if ($#difftree > 10) {
1124                 print(($#difftree + 1) . " files changed:\n");
1125         }
1126         print "</div>\n";
1127         foreach my $line (@difftree) {
1128                 # '*100644->100644      blob    9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596      net/ipv4/route.c'
1129                 # '+100644      blob    4a83ab6cd565d21ab0385bac6643826b83c2fcd4        arch/arm/lib/bitops.h'
1130                 # '*100664->100644      blob    b1a8e3dd5556b61dd771d32307c6ee5d7150fa43->b1a8e3dd5556b61dd771d32307c6ee5d7150fa43      show-files.c'
1131                 # '*100664->100644      blob    d08e895238bac36d8220586fdc28c27e1a7a76d3->d08e895238bac36d8220586fdc28c27e1a7a76d3      update-cache.c'
1132                 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
1133                 my $op = $1;
1134                 my $mode = $2;
1135                 my $type = $3;
1136                 my $id = $4;
1137                 my $file = $5;
1138                 if ($type eq "blob") {
1139                         if ($op eq "+") {
1140                                 my $mode_chng = "";
1141                                 if (S_ISREG(oct $mode)) {
1142                                         $mode_chng = sprintf(" with mode: %04o", (oct $mode) & 0777);
1143                                 }
1144                                 print "<div class=\"list\">\n" .
1145                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"},
1146                                       escapeHTML($file) . " <span style=\"color: #008000;\">[new " . file_type($mode) . "$mode_chng]</span>") . "\n" .
1147                                       "</div>\n";
1148                                 print "<div class=\"list_link\">\n" .
1149                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, "blob") . "\n" .
1150                                       "</div>\n";
1151                         } elsif ($op eq "-") {
1152                                 print "<div class=\"list\">\n" .
1153                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"},
1154                                       escapeHTML($file) .  " <span style=\"color: #c00000;\">[deleted " . file_type($mode) . "]</span>") . "\n" .
1155                                       "</div>";
1156                                 print "<div class=\"list_link\">\n" .
1157                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, "blob") . " | " .
1158                                       $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "\n" .
1159                                       "</div>\n";
1160                         } elsif ($op eq "*") {
1161                                 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
1162                                 my $from_id = $1;
1163                                 my $to_id = $2;
1164                                 $mode =~ m/^([0-7]{6})->([0-7]{6})$/;
1165                                 my $from_mode = $1;
1166                                 my $to_mode = $2;
1167                                 my $mode_chnge = "";
1168                                 if ($from_mode != $to_mode) {
1169                                         $mode_chnge = " <span style=\"color: #777777;\">[changed";
1170                                         if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
1171                                                 $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
1172                                         }
1173                                         if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
1174                                                 if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
1175                                                         $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
1176                                                 } elsif (S_ISREG($to_mode)) {
1177                                                         $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
1178                                                 }
1179                                         }
1180                                         $mode_chnge .= "]</span>\n";
1181                                 }
1182                                 print "<div class=\"list\">\n";
1183                                 if ($to_id ne $from_id) {
1184                                         print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"},
1185                                               escapeHTML($file) . $mode_chnge) . "\n" .
1186                                               "</div>\n";
1187                                 } else {
1188                                         print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"},
1189                                               escapeHTML($file) . $mode_chnge) . "\n" .
1190                                               "</div>\n";
1191                                 }
1192                                 print "<div class=\"list_link\">\n";
1193                                 if ($to_id ne $from_id) {
1194                                         print $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"}, "diff") . " | ";
1195                                 }
1196                                 print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, "blob") . " | " .
1197                                       $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash;f=$file"}, "history") . "\n" .
1198                                       "</div>\n";
1199                         }
1200                 }
1201         }
1202         git_footer_html();
1203 }
1204
1205 sub git_blobdiff {
1206         mkdir($gittmp, 0700);
1207         git_header_html();
1208         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1209                 print "<div class=\"page_nav\">\n" .
1210                       $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1211                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
1212                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
1213                       " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree");
1214                         if (defined $file_name) {
1215                                 print " | " . $cgi->a({-href => "$my_uri?p=$project;a=history;h=$hash_base;f=$file_name"}, "history");
1216                         }
1217                 print "<br/><br/>\n" .
1218                       "</div>\n";
1219                 print "<div>\n" .
1220                       $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1221                       "</div>\n";
1222         } else {
1223                 print "<div class=\"page_nav\">\n" .
1224                       "<br/><br/></div>\n" .
1225                       "<div class=\"title\">$hash vs $hash_parent</div>\n";
1226         }
1227         if (defined $file_name) {
1228                 print "<div class=\"page_path\">\n" .
1229                       "/$file_name\n" .
1230                       "</div>\n";
1231         }
1232         print "<div class=\"page_body\">\n" .
1233               "<div class=\"diff_info\">blob:" .
1234               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name"}, $hash_parent) .
1235               " -> blob:" .
1236               $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name"}, $hash) .
1237               "</div>\n";
1238         git_diff_html($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
1239         print "</div>";
1240         git_footer_html();
1241 }
1242
1243 sub git_commitdiff {
1244         mkdir($gittmp, 0700);
1245         my %co = git_read_commit($hash);
1246         if (!%co) {
1247                 die_error(undef, "Unknown commit object.");
1248         }
1249         open my $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $hash" || die_error(undef, "Open failed.");
1250         my (@difftree) = map { chomp; $_ } <$fd>;
1251         close $fd || die_error(undef, "Reading diff-tree failed.");
1252
1253         git_header_html();
1254         print "<div class=\"page_nav\">\n" .
1255               $cgi->a({-href => "$my_uri?p=$project;a=log"}, "log") .
1256               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") .
1257               " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") .
1258               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" .  $co{'tree'} . ";hb=$hash"}, "tree") .
1259               "<br/><br/></div>\n";
1260         print "<div>\n" .
1261               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1262               "</div>\n";
1263         print "<div class=\"page_body\">\n";
1264         foreach my $line (@difftree) {
1265                 # '*100644->100644      blob    8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154      Makefile'
1266                 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
1267                 my $op = $1;
1268                 my $mode = $2;
1269                 my $type = $3;
1270                 my $id = $4;
1271                 my $file = $5;
1272                 if ($type eq "blob") {
1273                         if ($op eq "+") {
1274                                 print "<div class=\"diff_info\">" .  file_type($mode) . ":" .
1275                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, $id) . "(new)" .
1276                                       "</div>\n";
1277                                 git_diff_html(undef, "/dev/null", $id, "b/$file");
1278                         } elsif ($op eq "-") {
1279                                 print "<div class=\"diff_info\">" . file_type($mode) . ":" .
1280                                       $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id;hb=$hash;f=$file"}, $id) . "(deleted)" .
1281                                       "</div>\n";
1282                                 git_diff_html($id, "a/$file", undef, "/dev/null");
1283                         } elsif ($op eq "*") {
1284                                 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
1285                                 my $from_id = $1;
1286                                 my $to_id = $2;
1287                                 $mode =~ m/([0-7]+)->([0-7]+)/;
1288                                 my $from_mode = $1;
1289                                 my $to_mode = $2;
1290                                 if ($from_id ne $to_id) {
1291                                         print "<div class=\"diff_info\">" .
1292                                               file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from_id;hb=$hash;f=$file"}, $from_id) .
1293                                               " -> " .
1294                                               file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to_id;hb=$hash;f=$file"}, $to_id);
1295                                         print "</div>\n";
1296                                         git_diff_html($from_id, "a/$file",  $to_id, "b/$file");
1297                                 }
1298                         }
1299                 }
1300         }
1301         print "<br/>\n" .
1302               "</div>";
1303         git_footer_html();
1304 }
1305
1306 sub git_history {
1307         if (!defined $hash) {
1308                 $hash = git_read_hash("$project/HEAD");
1309         }
1310         my %co = git_read_commit($hash);
1311         if (!%co) {
1312                 die_error(undef, "Unknown commit object.");
1313         }
1314         git_header_html();
1315         print "<div class=\"page_nav\">\n" .
1316               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | " .
1317               $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "commitdiff") . " | " .
1318               $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash"}, "tree") .
1319               "<br/><br/>\n" .
1320               "</div>\n";
1321         print "<div>\n" .
1322               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash", -class => "title"}, escapeHTML($co{'title'})) . "\n" .
1323               "</div>\n";
1324         print "<div class=\"page_path\">\n" .
1325               "/$file_name<br/>\n";
1326         print "</div>\n";
1327         open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin $file_name";
1328         my $commit;
1329         while (my $line = <$fd>) {
1330                 if ($line =~ m/^([0-9a-fA-F]{40}) /){
1331                         $commit = $1;
1332                         next;
1333                 }
1334                 if ($line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/ && (defined $commit)) {
1335                         my $type = $3;
1336                         my $file = $5;
1337                         if ($file ne $file_name || $type ne "blob") {
1338                                 next;
1339                         }
1340                         my %co = git_read_commit($commit);
1341                         if (!%co) {
1342                                 next;
1343                         }
1344                         print "<div class=\"list\">\n" .
1345                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"},
1346                               "<span class=\"log_age\">$co{'age_string'}</span>" . escapeHTML($co{'title'})) . "\n" .
1347                               "</div>\n";
1348                         print "<div class=\"list_link\">\n" .
1349                               $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "commit") .
1350                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=" .  $co{'tree'} . ";hb=$commit"}, "tree") .
1351                               " | " . $cgi->a({-href => "$my_uri?p=$project;a=blob;hb=$commit;f=$file"}, "blob");
1352                         my $blob = git_get_hash_by_path($hash, $file_name);
1353                         my $blob_parent = git_get_hash_by_path($commit, $file_name);
1354                         if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
1355                                 print " | " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file"}, "diff");
1356                         }
1357                         print "<br/>\n" .
1358                               "</div>\n";
1359                         undef $commit;
1360                 }
1361         }
1362         close $fd;
1363         git_footer_html();
1364 }