]> git.ipfire.org Git - thirdparty/git.git/blame - gitweb/gitweb.perl
gitweb: optionally read config from GITWEB_CONFIG
[thirdparty/git.git] / gitweb / gitweb.perl
CommitLineData
161332a5
KS
1#!/usr/bin/perl
2
c994d620 3# gitweb - simple web interface to track changes in git repositories
22fafb99 4#
00cd0794
KS
5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6# (C) 2005, Christian Gierke
823d5dc8 7#
d8f1c5c2 8# This program is licensed under the GPLv2
161332a5
KS
9
10use strict;
11use warnings;
19806691 12use CGI qw(:standard :escapeHTML -nosticky);
7403d50b 13use CGI::Util qw(unescape);
161332a5 14use CGI::Carp qw(fatalsToBrowser);
40c13813 15use Encode;
b87d78d6 16use Fcntl ':mode';
7a13b999 17use File::Find qw();
10bb9036 18binmode STDOUT, ':utf8';
161332a5 19
4a87b43e 20our $cgi = new CGI;
5d043a3d 21our $version = "@@GIT_VERSION@@";
4a87b43e
DS
22our $my_url = $cgi->url();
23our $my_uri = $cgi->url(-absolute => 1);
24our $rss_link = "";
3e029299 25
e130ddaa
AT
26# core git executable to use
27# this can just be "git" if your webserver has a sensible PATH
5d043a3d 28our $GIT = "@@GIT_BINDIR@@/git";
3f7f2710 29
b87d78d6 30# absolute fs-path which will be prepended to the project path
4a87b43e 31#our $projectroot = "/pub/scm";
5d043a3d 32our $projectroot = "@@GITWEB_PROJECTROOT@@";
b87d78d6 33
b87d78d6 34# location for temporary files needed for diffs
4a87b43e 35our $git_temp = "/tmp/gitweb";
034df39e 36
b87d78d6 37# target of the home link on top of all pages
4a87b43e 38our $home_link = $my_uri;
b87d78d6 39
49da1daf
AT
40# name of your site or organization to appear in page titles
41# replace this with something more descriptive for clearer bookmarks
5d043a3d 42our $site_name = "@@GITWEB_SITENAME@@" || $ENV{'SERVER_NAME'} || "Untitled";
49da1daf 43
8ab1da2c 44# html text to include at home page
5d043a3d 45our $home_text = "@@GITWEB_HOMETEXT@@";
8ab1da2c 46
aedd9425 47# URI of default stylesheet
5d043a3d 48our $stylesheet = "@@GITWEB_CSS@@";
281f2f6b
MW
49# URI of GIT logo
50our $logo = "@@GITWEB_LOGO@@";
aedd9425 51
09bd7898 52# source of projects list
c8d138a8 53our $projects_list = "@@GITWEB_LIST@@";
b87d78d6 54
f5aa79d9 55# default blob_plain mimetype and default charset for text/plain blob
4a87b43e
DS
56our $default_blob_plain_mimetype = 'text/plain';
57our $default_text_plain_charset = undef;
f5aa79d9 58
2d007374
PB
59# file to use for guessing MIME types before trying /etc/mime.types
60# (relative to the current git repository)
4a87b43e 61our $mimetypes_file = undef;
2d007374 62
c8d138a8
JK
63our $GITWEB_CONFIG = "@@GITWEB_CONFIG@@";
64require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
65
66# version of the core git binary
67our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
68
69$projects_list ||= $projectroot;
70if (! -d $git_temp) {
71 mkdir($git_temp, 0700) || die_error("Couldn't mkdir $git_temp");
72}
73
09bd7898 74# input validation and dispatch
4a87b43e 75our $action = $cgi->param('a');
09bd7898 76if (defined $action) {
c91da262 77 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
09bd7898
KS
78 undef $action;
79 die_error(undef, "Invalid action parameter.");
b87d78d6 80 }
281f2f6b 81 if ($action eq "opml") {
c994d620
KS
82 git_opml();
83 exit;
09bd7898 84 }
b87d78d6 85}
44ad2978 86
4a87b43e 87our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
b87d78d6 88if (defined $project) {
49f582a0 89 $project =~ s|^/||; $project =~ s|/$||;
c91da262
KS
90 $project = validate_input($project);
91 if (!defined($project)) {
92 die_error(undef, "Invalid project parameter.");
9cd3d988
KS
93 }
94 if (!(-d "$projectroot/$project")) {
b87d78d6 95 undef $project;
09bd7898 96 die_error(undef, "No such directory.");
b87d78d6
KS
97 }
98 if (!(-e "$projectroot/$project/HEAD")) {
99 undef $project;
09bd7898 100 die_error(undef, "No such project.");
9cd3d988 101 }
232ff553 102 $rss_link = "<link rel=\"alternate\" title=\"" . esc_param($project) . " log\" href=\"" .
5be01bc8 103 "$my_uri?" . esc_param("p=$project;a=rss") . "\" type=\"application/rss+xml\"/>";
4fac5294 104 $ENV{'GIT_DIR'} = "$projectroot/$project";
09bd7898 105} else {
ede5e100 106 git_project_list();
09bd7898 107 exit;
a59d4afd 108}
6191f8e1 109
4a87b43e 110our $file_name = $cgi->param('f');
b87d78d6 111if (defined $file_name) {
c91da262
KS
112 $file_name = validate_input($file_name);
113 if (!defined($file_name)) {
114 die_error(undef, "Invalid file parameter.");
b87d78d6 115 }
a59d4afd 116}
6191f8e1 117
4a87b43e 118our $hash = $cgi->param('h');
4fac5294 119if (defined $hash) {
c91da262
KS
120 $hash = validate_input($hash);
121 if (!defined($hash)) {
122 die_error(undef, "Invalid hash parameter.");
4fac5294 123 }
a59d4afd 124}
6191f8e1 125
4a87b43e 126our $hash_parent = $cgi->param('hp');
c91da262
KS
127if (defined $hash_parent) {
128 $hash_parent = validate_input($hash_parent);
129 if (!defined($hash_parent)) {
130 die_error(undef, "Invalid hash parent parameter.");
131 }
09bd7898
KS
132}
133
4a87b43e 134our $hash_base = $cgi->param('hb');
c91da262
KS
135if (defined $hash_base) {
136 $hash_base = validate_input($hash_base);
137 if (!defined($hash_base)) {
138 die_error(undef, "Invalid hash base parameter.");
139 }
a59d4afd 140}
6191f8e1 141
4a87b43e 142our $page = $cgi->param('pg');
ea4a6df4 143if (defined $page) {
c91da262 144 if ($page =~ m/[^0-9]$/) {
ea4a6df4
KS
145 undef $page;
146 die_error(undef, "Invalid page parameter.");
b87d78d6 147 }
2ad9331e 148}
823d5dc8 149
4a87b43e 150our $searchtext = $cgi->param('s');
19806691
KS
151if (defined $searchtext) {
152 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
153 undef $searchtext;
154 die_error(undef, "Invalid search parameter.");
155 }
156 $searchtext = quotemeta $searchtext;
157}
158
717b8311 159# dispatch
8e85cdc4
ML
160my %actions = (
161 "blame" => \&git_blame2,
162 "blobdiff" => \&git_blobdiff,
163 "blobdiff_plain" => \&git_blobdiff_plain,
164 "blob" => \&git_blob,
165 "blob_plain" => \&git_blob_plain,
166 "commitdiff" => \&git_commitdiff,
167 "commitdiff_plain" => \&git_commitdiff_plain,
168 "commit" => \&git_commit,
169 "heads" => \&git_heads,
170 "history" => \&git_history,
171 "log" => \&git_log,
172 "rss" => \&git_rss,
173 "search" => \&git_search,
174 "shortlog" => \&git_shortlog,
175 "summary" => \&git_summary,
176 "tag" => \&git_tag,
177 "tags" => \&git_tags,
178 "tree" => \&git_tree,
179);
180
181$action = 'summary' if (!defined($action));
182if (!defined($actions{$action})) {
09bd7898
KS
183 undef $action;
184 die_error(undef, "Unknown action.");
09bd7898 185}
8e85cdc4
ML
186$actions{$action}->();
187exit;
09bd7898 188
717b8311
JN
189## ======================================================================
190## validation, quoting/unquoting and escaping
191
192sub validate_input {
193 my $input = shift;
194
195 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
196 return $input;
197 }
198 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
199 return undef;
200 }
201 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
202 return undef;
203 }
204 return $input;
205}
206
232ff553
KS
207# quote unsafe chars, but keep the slash, even when it's not
208# correct, but quoted slashes look too horrible in bookmarks
209sub esc_param {
353347b0 210 my $str = shift;
232ff553 211 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
18216710 212 $str =~ s/\+/%2B/g;
a9e60b7d 213 $str =~ s/ /\+/g;
353347b0
KS
214 return $str;
215}
216
232ff553 217# replace invalid utf8 character with SUBSTITUTION sequence
40c13813
KS
218sub esc_html {
219 my $str = shift;
40c13813 220 $str = decode("utf8", $str, Encode::FB_DEFAULT);
10bb9036 221 $str = escapeHTML($str);
3dc13832 222 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
40c13813
KS
223 return $str;
224}
225
232ff553
KS
226# git may return quoted and escaped filenames
227sub unquote {
228 my $str = shift;
229 if ($str =~ m/^"(.*)"$/) {
230 $str = $1;
231 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
232 }
233 return $str;
234}
235
717b8311
JN
236## ----------------------------------------------------------------------
237## HTML aware string manipulation
238
239sub chop_str {
240 my $str = shift;
241 my $len = shift;
242 my $add_len = shift || 10;
243
244 # allow only $len chars, but don't cut a word if it would fit in $add_len
245 # if it doesn't fit, cut it if it's still longer than the dots we would add
246 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
247 my $body = $1;
248 my $tail = $2;
249 if (length($tail) > 4) {
250 $tail = " ...";
251 $body =~ s/&[^;]*$//; # remove chopped character entities
252 }
253 return "$body$tail";
254}
255
256## ----------------------------------------------------------------------
257## functions returning short strings
258
1f1ab5f0
JN
259# CSS class for given age value (in seconds)
260sub age_class {
261 my $age = shift;
262
263 if ($age < 60*60*2) {
264 return "age0";
265 } elsif ($age < 60*60*24*2) {
266 return "age1";
267 } else {
268 return "age2";
269 }
270}
271
717b8311
JN
272# convert age in seconds to "nn units ago" string
273sub age_string {
274 my $age = shift;
275 my $age_str;
a59d4afd 276
717b8311
JN
277 if ($age > 60*60*24*365*2) {
278 $age_str = (int $age/60/60/24/365);
279 $age_str .= " years ago";
280 } elsif ($age > 60*60*24*(365/12)*2) {
281 $age_str = int $age/60/60/24/(365/12);
282 $age_str .= " months ago";
283 } elsif ($age > 60*60*24*7*2) {
284 $age_str = int $age/60/60/24/7;
285 $age_str .= " weeks ago";
286 } elsif ($age > 60*60*24*2) {
287 $age_str = int $age/60/60/24;
288 $age_str .= " days ago";
289 } elsif ($age > 60*60*2) {
290 $age_str = int $age/60/60;
291 $age_str .= " hours ago";
292 } elsif ($age > 60*2) {
293 $age_str = int $age/60;
294 $age_str .= " min ago";
295 } elsif ($age > 2) {
296 $age_str = int $age;
297 $age_str .= " sec ago";
f6801d66 298 } else {
717b8311 299 $age_str .= " right now";
4c02e3c5 300 }
717b8311 301 return $age_str;
161332a5
KS
302}
303
717b8311
JN
304# convert file mode in octal to symbolic file mode string
305sub mode_str {
306 my $mode = oct shift;
307
308 if (S_ISDIR($mode & S_IFMT)) {
309 return 'drwxr-xr-x';
310 } elsif (S_ISLNK($mode)) {
311 return 'lrwxrwxrwx';
312 } elsif (S_ISREG($mode)) {
313 # git cares only about the executable bit
314 if ($mode & S_IXUSR) {
315 return '-rwxr-xr-x';
316 } else {
317 return '-rw-r--r--';
318 };
c994d620 319 } else {
717b8311 320 return '----------';
ff7669a5 321 }
161332a5
KS
322}
323
717b8311
JN
324# convert file mode in octal to file type string
325sub file_type {
326 my $mode = oct shift;
664f4cc5 327
717b8311
JN
328 if (S_ISDIR($mode & S_IFMT)) {
329 return "directory";
330 } elsif (S_ISLNK($mode)) {
331 return "symlink";
332 } elsif (S_ISREG($mode)) {
333 return "file";
334 } else {
335 return "unknown";
336 }
a59d4afd
KS
337}
338
717b8311
JN
339## ----------------------------------------------------------------------
340## functions returning short HTML fragments, or transforming HTML fragments
341## which don't beling to other sections
b18f9bf4 342
717b8311
JN
343# format line of commit message or tag comment
344sub format_log_line_html {
345 my $line = shift;
b18f9bf4 346
717b8311
JN
347 $line = esc_html($line);
348 $line =~ s/ /&nbsp;/g;
349 if ($line =~ m/([0-9a-fA-F]{40})/) {
350 my $hash_text = $1;
351 if (git_get_type($hash_text) eq "commit") {
352 my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
353 $line =~ s/$hash_text/$link/;
b18f9bf4
JN
354 }
355 }
717b8311 356 return $line;
b18f9bf4
JN
357}
358
717b8311
JN
359# format marker of refs pointing to given object
360sub git_get_referencing {
361 my ($refs, $id) = @_;
27fb8c40 362
717b8311
JN
363 if (defined $refs->{$id}) {
364 return ' <span class="tag">' . esc_html($refs->{$id}) . '</span>';
365 } else {
366 return "";
367 }
27fb8c40
JN
368}
369
717b8311
JN
370## ----------------------------------------------------------------------
371## git utility subroutines, invoking git commands
42f7eb94 372
717b8311 373# get HEAD ref of given project as hash
df2c37a5
JH
374sub git_read_head {
375 my $project = shift;
376 my $oENV = $ENV{'GIT_DIR'};
377 my $retval = undef;
378 $ENV{'GIT_DIR'} = "$projectroot/$project";
e130ddaa 379 if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
df2c37a5
JH
380 my $head = <$fd>;
381 close $fd;
2c5c008b
KS
382 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
383 $retval = $1;
df2c37a5
JH
384 }
385 }
2c5c008b
KS
386 if (defined $oENV) {
387 $ENV{'GIT_DIR'} = $oENV;
388 }
df2c37a5
JH
389 return $retval;
390}
391
717b8311
JN
392# get type of given object
393sub git_get_type {
394 my $hash = shift;
395
396 open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
397 my $type = <$fd>;
398 close $fd or return;
399 chomp $type;
400 return $type;
401}
402
403sub git_get_project_config {
404 my $key = shift;
405
406 return unless ($key);
407 $key =~ s/^gitweb\.//;
408 return if ($key =~ m/\W/);
409
410 my $val = qx($GIT repo-config --get gitweb.$key);
411 return ($val);
412}
413
414sub git_get_project_config_bool {
415 my $val = git_get_project_config (@_);
416 if ($val and $val =~ m/true|yes|on/) {
417 return (1);
418 }
419 return; # implicit false
420}
421
422# get hash of given path at given ref
423sub git_get_hash_by_path {
424 my $base = shift;
425 my $path = shift || return undef;
426
427 my $tree = $base;
428
429 open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
430 or die_error(undef, "Open git-ls-tree failed.");
431 my $line = <$fd>;
432 close $fd or return undef;
433
434 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
435 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
436 return $3;
437}
438
439## ......................................................................
440## git utility functions, directly accessing git repository
441
442# assumes that PATH is not symref
ede5e100 443sub git_read_hash {
54b0a43c 444 my $path = shift;
09bd7898 445
19806691 446 open my $fd, "$projectroot/$path" or return undef;
12a88f2f
KS
447 my $head = <$fd>;
448 close $fd;
449 chomp $head;
b87d78d6
KS
450 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
451 return $head;
b87d78d6
KS
452 }
453}
454
09bd7898 455sub git_read_description {
b87d78d6 456 my $path = shift;
09bd7898 457
19806691 458 open my $fd, "$projectroot/$path/description" or return undef;
b87d78d6
KS
459 my $descr = <$fd>;
460 close $fd;
461 chomp $descr;
462 return $descr;
12a88f2f
KS
463}
464
717b8311
JN
465sub git_read_projects {
466 my @list;
467
468 if (-d $projects_list) {
469 # search in directory
470 my $dir = $projects_list;
471 opendir my ($dh), $dir or return undef;
472 while (my $dir = readdir($dh)) {
473 if (-e "$projectroot/$dir/HEAD") {
474 my $pr = {
475 path => $dir,
476 };
477 push @list, $pr
478 }
479 }
480 closedir($dh);
481 } elsif (-f $projects_list) {
482 # read from file(url-encoded):
483 # 'git%2Fgit.git Linus+Torvalds'
484 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
485 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
486 open my ($fd), $projects_list or return undef;
487 while (my $line = <$fd>) {
488 chomp $line;
489 my ($path, $owner) = split ' ', $line;
490 $path = unescape($path);
491 $owner = unescape($owner);
492 if (!defined $path) {
493 next;
494 }
495 if (-e "$projectroot/$path/HEAD") {
496 my $pr = {
497 path => $path,
498 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
499 };
500 push @list, $pr
501 }
502 }
503 close $fd;
504 }
505 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
506 return @list;
507}
508
509sub read_info_ref {
510 my $type = shift || "";
511 my %refs;
512 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
513 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
514 open my $fd, "$projectroot/$project/info/refs" or return;
515 while (my $line = <$fd>) {
516 chomp $line;
517 # attention: for $type == "" it saves only last path part of ref name
518 # e.g. from 'refs/heads/jn/gitweb' it would leave only 'gitweb'
519 if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
520 if (defined $refs{$1}) {
521 $refs{$1} .= " / $2";
522 } else {
523 $refs{$1} = $2;
524 }
525 }
526 }
527 close $fd or return;
528 return \%refs;
529}
530
531## ----------------------------------------------------------------------
532## parse to hash functions
533
534sub date_str {
535 my $epoch = shift;
536 my $tz = shift || "-0000";
537
538 my %date;
539 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
540 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
541 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
542 $date{'hour'} = $hour;
543 $date{'minute'} = $min;
544 $date{'mday'} = $mday;
545 $date{'day'} = $days[$wday];
546 $date{'month'} = $months[$mon];
547 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
548 $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
549
550 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
551 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
552 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
553 $date{'hour_local'} = $hour;
554 $date{'minute_local'} = $min;
555 $date{'tz_local'} = $tz;
556 return %date;
557}
558
ede5e100
KS
559sub git_read_tag {
560 my $tag_id = shift;
561 my %tag;
d8a20ba9 562 my @comment;
ede5e100 563
b9182987 564 open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
d8a20ba9 565 $tag{'id'} = $tag_id;
ede5e100
KS
566 while (my $line = <$fd>) {
567 chomp $line;
568 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
569 $tag{'object'} = $1;
7ab0d2b6 570 } elsif ($line =~ m/^type (.+)$/) {
ede5e100 571 $tag{'type'} = $1;
7ab0d2b6 572 } elsif ($line =~ m/^tag (.+)$/) {
ede5e100 573 $tag{'name'} = $1;
d8a20ba9
KS
574 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
575 $tag{'author'} = $1;
576 $tag{'epoch'} = $2;
577 $tag{'tz'} = $3;
578 } elsif ($line =~ m/--BEGIN/) {
579 push @comment, $line;
580 last;
581 } elsif ($line eq "") {
582 last;
ede5e100
KS
583 }
584 }
d8a20ba9
KS
585 push @comment, <$fd>;
586 $tag{'comment'} = \@comment;
19806691 587 close $fd or return;
ede5e100
KS
588 if (!defined $tag{'name'}) {
589 return
590 };
591 return %tag
592}
593
09bd7898 594sub git_read_commit {
19806691
KS
595 my $commit_id = shift;
596 my $commit_text = shift;
597
598 my @commit_lines;
703ac710 599 my %co;
703ac710 600
19806691
KS
601 if (defined $commit_text) {
602 @commit_lines = @$commit_text;
603 } else {
25f422fb 604 $/ = "\0";
b9182987 605 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id or return;
25f422fb 606 @commit_lines = split '\n', <$fd>;
19806691 607 close $fd or return;
25f422fb
KS
608 $/ = "\n";
609 pop @commit_lines;
19806691 610 }
25f422fb
KS
611 my $header = shift @commit_lines;
612 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
613 return;
614 }
615 ($co{'id'}, my @parents) = split ' ', $header;
616 $co{'parents'} = \@parents;
617 $co{'parent'} = $parents[0];
19806691 618 while (my $line = shift @commit_lines) {
b87d78d6 619 last if $line eq "\n";
7ab0d2b6 620 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
703ac710 621 $co{'tree'} = $1;
022be3d0 622 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3f714537 623 $co{'author'} = $1;
185f09e5
KS
624 $co{'author_epoch'} = $2;
625 $co{'author_tz'} = $3;
2bf7a52c
KS
626 if ($co{'author'} =~ m/^([^<]+) </) {
627 $co{'author_name'} = $1;
628 } else {
629 $co{'author_name'} = $co{'author'};
630 }
86eed32d
KS
631 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
632 $co{'committer'} = $1;
185f09e5
KS
633 $co{'committer_epoch'} = $2;
634 $co{'committer_tz'} = $3;
991910a9
KS
635 $co{'committer_name'} = $co{'committer'};
636 $co{'committer_name'} =~ s/ <.*//;
703ac710
KS
637 }
638 }
ede5e100 639 if (!defined $co{'tree'}) {
25f422fb 640 return;
ede5e100 641 };
25f422fb 642
19806691 643 foreach my $title (@commit_lines) {
c2488d06 644 $title =~ s/^ //;
19806691 645 if ($title ne "") {
48c771f4 646 $co{'title'} = chop_str($title, 80, 5);
19806691
KS
647 # remove leading stuff of merges to make the interesting part visible
648 if (length($title) > 50) {
649 $title =~ s/^Automatic //;
650 $title =~ s/^merge (of|with) /Merge ... /i;
651 if (length($title) > 50) {
652 $title =~ s/(http|rsync):\/\///;
653 }
654 if (length($title) > 50) {
655 $title =~ s/(master|www|rsync)\.//;
656 }
657 if (length($title) > 50) {
658 $title =~ s/kernel.org:?//;
659 }
660 if (length($title) > 50) {
661 $title =~ s/\/pub\/scm//;
662 }
663 }
48c771f4 664 $co{'title_short'} = chop_str($title, 50, 5);
19806691
KS
665 last;
666 }
667 }
25f422fb
KS
668 # remove added spaces
669 foreach my $line (@commit_lines) {
670 $line =~ s/^ //;
671 }
672 $co{'comment'} = \@commit_lines;
2ae100df
KS
673
674 my $age = time - $co{'committer_epoch'};
675 $co{'age'} = $age;
d263a6bd 676 $co{'age_string'} = age_string($age);
71be1e79
KS
677 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
678 if ($age > 60*60*24*7*2) {
1b1cd421 679 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
71be1e79
KS
680 $co{'age_string_age'} = $co{'age_string'};
681 } else {
682 $co{'age_string_date'} = $co{'age_string'};
1b1cd421 683 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
71be1e79 684 }
703ac710
KS
685 return %co;
686}
687
717b8311
JN
688## ......................................................................
689## parse to array of hashes functions
4c02e3c5 690
717b8311
JN
691sub git_read_refs {
692 my $ref_dir = shift;
693 my @reflist;
4c02e3c5 694
717b8311 695 my @refs;
7a13b999
JH
696 my $pfxlen = length("$projectroot/$project/$ref_dir");
697 File::Find::find(sub {
698 return if (/^\./);
699 if (-f $_) {
700 push @refs, substr($File::Find::name, $pfxlen + 1);
717b8311 701 }
7a13b999
JH
702 }, "$projectroot/$project/$ref_dir");
703
717b8311
JN
704 foreach my $ref_file (@refs) {
705 my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
706 my $type = git_get_type($ref_id) || next;
707 my %ref_item;
708 my %co;
709 $ref_item{'type'} = $type;
710 $ref_item{'id'} = $ref_id;
711 $ref_item{'epoch'} = 0;
712 $ref_item{'age'} = "unknown";
713 if ($type eq "tag") {
714 my %tag = git_read_tag($ref_id);
715 $ref_item{'comment'} = $tag{'comment'};
716 if ($tag{'type'} eq "commit") {
717 %co = git_read_commit($tag{'object'});
718 $ref_item{'epoch'} = $co{'committer_epoch'};
719 $ref_item{'age'} = $co{'age_string'};
720 } elsif (defined($tag{'epoch'})) {
721 my $age = time - $tag{'epoch'};
722 $ref_item{'epoch'} = $tag{'epoch'};
723 $ref_item{'age'} = age_string($age);
19806691 724 }
717b8311
JN
725 $ref_item{'reftype'} = $tag{'type'};
726 $ref_item{'name'} = $tag{'name'};
727 $ref_item{'refid'} = $tag{'object'};
728 } elsif ($type eq "commit"){
729 %co = git_read_commit($ref_id);
730 $ref_item{'reftype'} = "commit";
731 $ref_item{'name'} = $ref_file;
732 $ref_item{'title'} = $co{'title'};
733 $ref_item{'refid'} = $ref_id;
734 $ref_item{'epoch'} = $co{'committer_epoch'};
735 $ref_item{'age'} = $co{'age_string'};
54b0a43c 736 } else {
717b8311
JN
737 $ref_item{'reftype'} = $type;
738 $ref_item{'name'} = $ref_file;
739 $ref_item{'refid'} = $ref_id;
f49201a9 740 }
991910a9 741
717b8311
JN
742 push @reflist, \%ref_item;
743 }
744 # sort tags by age
745 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
746 return \@reflist;
86eed32d
KS
747}
748
717b8311
JN
749## ----------------------------------------------------------------------
750## filesystem-related functions
022be3d0 751
c07ad4b9
KS
752sub get_file_owner {
753 my $path = shift;
754
755 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
756 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
757 if (!defined $gcos) {
758 return undef;
759 }
760 my $owner = $gcos;
761 $owner =~ s/[,;].*$//;
281bf0cf 762 return decode("utf8", $owner, Encode::FB_DEFAULT);
c07ad4b9
KS
763}
764
717b8311
JN
765## ......................................................................
766## mimetype related functions
09bd7898 767
717b8311
JN
768sub mimetype_guess_file {
769 my $filename = shift;
770 my $mimemap = shift;
771 -r $mimemap or return undef;
772
773 my %mimemap;
774 open(MIME, $mimemap) or return undef;
775 while (<MIME>) {
776 my ($mime, $exts) = split(/\t+/);
46b059d7
JH
777 if (defined $exts) {
778 my @exts = split(/\s+/, $exts);
779 foreach my $ext (@exts) {
780 $mimemap{$ext} = $mime;
781 }
09bd7898 782 }
09bd7898 783 }
717b8311 784 close(MIME);
09bd7898 785
717b8311
JN
786 $filename =~ /\.(.*?)$/;
787 return $mimemap{$1};
788}
5996ca08 789
717b8311
JN
790sub mimetype_guess {
791 my $filename = shift;
792 my $mime;
793 $filename =~ /\./ or return undef;
5996ca08 794
717b8311
JN
795 if ($mimetypes_file) {
796 my $file = $mimetypes_file;
797 #$file =~ m#^/# or $file = "$projectroot/$path/$file";
798 $mime = mimetype_guess_file($filename, $file);
799 }
800 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
801 return $mime;
5996ca08
FF
802}
803
717b8311
JN
804sub git_blob_plain_mimetype {
805 my $fd = shift;
806 my $filename = shift;
5996ca08 807
717b8311
JN
808 if ($filename) {
809 my $mime = mimetype_guess($filename);
810 $mime and return $mime;
d8d17b5d 811 }
717b8311
JN
812
813 # just in case
814 return $default_blob_plain_mimetype unless $fd;
815
816 if (-T $fd) {
817 return 'text/plain' .
818 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
819 } elsif (! $filename) {
820 return 'application/octet-stream';
821 } elsif ($filename =~ m/\.png$/i) {
822 return 'image/png';
823 } elsif ($filename =~ m/\.gif$/i) {
824 return 'image/gif';
825 } elsif ($filename =~ m/\.jpe?g$/i) {
826 return 'image/jpeg';
d8d17b5d 827 } else {
717b8311 828 return 'application/octet-stream';
f7ab660c 829 }
717b8311
JN
830}
831
832## ======================================================================
833## functions printing HTML: header, footer, error page
834
835sub git_header_html {
836 my $status = shift || "200 OK";
837 my $expires = shift;
838
839 my $title = "$site_name git";
840 if (defined $project) {
841 $title .= " - $project";
842 if (defined $action) {
843 $title .= "/$action";
844 if (defined $file_name) {
845 $title .= " - $file_name";
846 if ($action eq "tree" && $file_name !~ m|/$|) {
847 $title .= "/";
848 }
849 }
850 }
f7ab660c 851 }
717b8311
JN
852 my $content_type;
853 # require explicit support from the UA if we are to send the page as
854 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
855 # we have to do this because MSIE sometimes globs '*/*', pretending to
856 # support xhtml+xml but choking when it gets what it asked for.
857 if ($cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ && $cgi->Accept('application/xhtml+xml') != 0) {
858 $content_type = 'application/xhtml+xml';
f7ab660c 859 } else {
717b8311 860 $content_type = 'text/html';
f7ab660c 861 }
717b8311
JN
862 print $cgi->header(-type=>$content_type, -charset => 'utf-8', -status=> $status, -expires => $expires);
863 print <<EOF;
864<?xml version="1.0" encoding="utf-8"?>
865<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
866<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
867<!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
868<!-- git core binaries version $git_version -->
869<head>
870<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
871<meta name="robots" content="index, nofollow"/>
872<title>$title</title>
873<link rel="stylesheet" type="text/css" href="$stylesheet"/>
874$rss_link
875</head>
876<body>
877EOF
878 print "<div class=\"page_header\">\n" .
879 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
281f2f6b 880 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
717b8311
JN
881 "</a>\n";
882 print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
883 if (defined $project) {
884 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
885 if (defined $action) {
886 print " / $action";
887 }
888 print "\n";
889 if (!defined $searchtext) {
890 $searchtext = "";
891 }
892 my $search_hash;
893 if (defined $hash_base) {
894 $search_hash = $hash_base;
895 } elsif (defined $hash) {
896 $search_hash = $hash;
bddec01d 897 } else {
717b8311 898 $search_hash = "HEAD";
bddec01d 899 }
717b8311
JN
900 $cgi->param("a", "search");
901 $cgi->param("h", $search_hash);
902 print $cgi->startform(-method => "get", -action => $my_uri) .
903 "<div class=\"search\">\n" .
904 $cgi->hidden(-name => "p") . "\n" .
905 $cgi->hidden(-name => "a") . "\n" .
906 $cgi->hidden(-name => "h") . "\n" .
907 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
908 "</div>" .
909 $cgi->end_form() . "\n";
b87d78d6 910 }
717b8311
JN
911 print "</div>\n";
912}
913
914sub git_footer_html {
915 print "<div class=\"page_footer\">\n";
916 if (defined $project) {
917 my $descr = git_read_description($project);
918 if (defined $descr) {
919 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
920 }
921 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
922 } else {
923 print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
924 }
925 print "</div>\n" .
926 "</body>\n" .
927 "</html>";
928}
929
930sub die_error {
931 my $status = shift || "403 Forbidden";
932 my $error = shift || "Malformed query, file missing or permission denied";
933
934 git_header_html($status);
935 print "<div class=\"page_body\">\n" .
936 "<br/><br/>\n" .
937 "$status - $error\n" .
938 "<br/>\n" .
939 "</div>\n";
b87d78d6 940 git_footer_html();
717b8311 941 exit;
161332a5
KS
942}
943
717b8311
JN
944## ----------------------------------------------------------------------
945## functions printing or outputting HTML: navigation
946
947sub git_page_nav {
948 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
949 $extra = '' if !defined $extra; # pager or formats
950
951 my @navs = qw(summary shortlog log commit commitdiff tree);
952 if ($suppress) {
953 @navs = grep { $_ ne $suppress } @navs;
954 }
955
956 my %arg = map { $_, ''} @navs;
957 if (defined $head) {
958 for (qw(commit commitdiff)) {
959 $arg{$_} = ";h=$head";
960 }
961 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
962 for (qw(shortlog log)) {
963 $arg{$_} = ";h=$head";
045e531a 964 }
6a928415
KS
965 }
966 }
717b8311
JN
967 $arg{tree} .= ";h=$treehead" if defined $treehead;
968 $arg{tree} .= ";hb=$treebase" if defined $treebase;
969
970 print "<div class=\"page_nav\">\n" .
971 (join " | ",
972 map { $_ eq $current
973 ? $_
974 : $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$_$arg{$_}")}, "$_")
975 }
976 @navs);
977 print "<br/>\n$extra<br/>\n" .
978 "</div>\n";
6a928415
KS
979}
980
717b8311
JN
981sub git_get_paging_nav {
982 my ($action, $hash, $head, $page, $nrevs) = @_;
983 my $paging_nav;
594e212b 984
717b8311
JN
985
986 if ($hash ne $head || $page) {
987 $paging_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action")}, "HEAD");
594e212b 988 } else {
717b8311
JN
989 $paging_nav .= "HEAD";
990 }
991
992 if ($page > 0) {
993 $paging_nav .= " &sdot; " .
994 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page-1)),
995 -accesskey => "p", -title => "Alt-p"}, "prev");
996 } else {
997 $paging_nav .= " &sdot; prev";
998 }
999
1000 if ($nrevs >= (100 * ($page+1)-1)) {
1001 $paging_nav .= " &sdot; " .
1002 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page+1)),
1003 -accesskey => "n", -title => "Alt-n"}, "next");
1004 } else {
1005 $paging_nav .= " &sdot; next";
594e212b 1006 }
717b8311
JN
1007
1008 return $paging_nav;
594e212b
JN
1009}
1010
717b8311
JN
1011## ......................................................................
1012## functions printing or outputting HTML: div
1013
1014sub git_header_div {
1015 my ($action, $title, $hash, $hash_base) = @_;
1016 my $rest = '';
1017
1018 $rest .= ";h=$hash" if $hash;
1019 $rest .= ";hb=$hash_base" if $hash_base;
1020
1021 print "<div class=\"header\">\n" .
1022 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action$rest"),
1023 -class => "title"}, $title ? $title : $action) . "\n" .
1024 "</div>\n";
1025}
ede5e100 1026
717b8311
JN
1027sub git_print_page_path {
1028 my $name = shift;
1029 my $type = shift;
ede5e100 1030
717b8311
JN
1031 if (!defined $name) {
1032 print "<div class=\"page_path\"><b>/</b></div>\n";
5d1acf4d 1033 } elsif (defined $type && $type eq 'blob') {
717b8311
JN
1034 print "<div class=\"page_path\"><b>" .
1035 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($name)) . "</b><br/></div>\n";
1036 } else {
1037 print "<div class=\"page_path\"><b>" . esc_html($name) . "</b><br/></div>\n";
ede5e100 1038 }
ede5e100
KS
1039}
1040
717b8311
JN
1041## ......................................................................
1042## functions printing large fragments of HTML
1043
9f5dcb81
JN
1044sub git_shortlog_body {
1045 # uses global variable $project
1046 my ($revlist, $from, $to, $refs, $extra) = @_;
1047 $from = 0 unless defined $from;
1048 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1049
1050 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1051 my $alternate = 0;
1052 for (my $i = $from; $i <= $to; $i++) {
1053 my $commit = $revlist->[$i];
1054 #my $ref = defined $refs ? git_get_referencing($refs, $commit) : '';
1055 my $ref = git_get_referencing($refs, $commit);
1056 my %co = git_read_commit($commit);
1057 my %ad = date_str($co{'author_epoch'});
1058 if ($alternate) {
1059 print "<tr class=\"dark\">\n";
1060 } else {
1061 print "<tr class=\"light\">\n";
1062 }
1063 $alternate ^= 1;
1064 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1065 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1066 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1067 "<td>";
1068 if (length($co{'title_short'}) < length($co{'title'})) {
1069 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"),
1070 -class => "list", -title => "$co{'title'}"},
1071 "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
1072 } else {
1073 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"),
1074 -class => "list"},
1075 "<b>" . esc_html($co{'title'}) . "$ref</b>");
1076 }
1077 print "</td>\n" .
1078 "<td class=\"link\">" .
1079 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") . " | " .
1080 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1081 "</td>\n" .
1082 "</tr>\n";
1083 }
1084 if (defined $extra) {
1085 print "<tr>\n" .
1086 "<td colspan=\"4\">$extra</td>\n" .
1087 "</tr>\n";
1088 }
1089 print "</table>\n";
1090}
1091
717b8311
JN
1092sub git_tags_body {
1093 # uses global variable $project
1094 my ($taglist, $from, $to, $extra) = @_;
1095 $from = 0 unless defined $from;
1096 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1097
1098 print "<table class=\"tags\" cellspacing=\"0\">\n";
1099 my $alternate = 0;
1100 for (my $i = $from; $i <= $to; $i++) {
1101 my $entry = $taglist->[$i];
1102 my %tag = %$entry;
1103 my $comment_lines = $tag{'comment'};
1104 my $comment = shift @$comment_lines;
1105 my $comment_short;
1106 if (defined $comment) {
1107 $comment_short = chop_str($comment, 30, 5);
1108 }
1109 if ($alternate) {
1110 print "<tr class=\"dark\">\n";
1111 } else {
1112 print "<tr class=\"light\">\n";
1113 }
1114 $alternate ^= 1;
1115 print "<td><i>$tag{'age'}</i></td>\n" .
1116 "<td>" .
1117 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"),
1118 -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1119 "</td>\n" .
1120 "<td>";
1121 if (defined $comment) {
1122 if (length($comment_short) < length($comment)) {
1123 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
1124 -class => "list", -title => $comment}, $comment_short);
1125 } else {
1126 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
1127 -class => "list"}, $comment);
1128 }
1129 }
1130 print "</td>\n" .
1131 "<td class=\"selflink\">";
1132 if ($tag{'type'} eq "tag") {
1133 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag");
1134 } else {
1135 print "&nbsp;";
1136 }
1137 print "</td>\n" .
1138 "<td class=\"link\">" . " | " .
1139 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1140 if ($tag{'reftype'} eq "commit") {
1141 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1142 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1143 } elsif ($tag{'reftype'} eq "blob") {
1144 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$tag{'refid'}")}, "raw");
1145 }
1146 print "</td>\n" .
1147 "</tr>";
1148 }
1149 if (defined $extra) {
1150 print "<tr>\n" .
1151 "<td colspan=\"5\">$extra</td>\n" .
1152 "</tr>\n";
1153 }
1154 print "</table>\n";
1155}
1156
1157sub git_heads_body {
1158 # uses global variable $project
1159 my ($taglist, $head, $from, $to, $extra) = @_;
1160 $from = 0 unless defined $from;
1161 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1162
1163 print "<table class=\"heads\" cellspacing=\"0\">\n";
1164 my $alternate = 0;
1165 for (my $i = $from; $i <= $to; $i++) {
1166 my $entry = $taglist->[$i];
1167 my %tag = %$entry;
1168 my $curr = $tag{'id'} eq $head;
1169 if ($alternate) {
1170 print "<tr class=\"dark\">\n";
1171 } else {
1172 print "<tr class=\"light\">\n";
1173 }
1174 $alternate ^= 1;
1175 print "<td><i>$tag{'age'}</i></td>\n" .
1176 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1177 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"),
1178 -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1179 "</td>\n" .
1180 "<td class=\"link\">" .
1181 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") . " | " .
1182 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1183 "</td>\n" .
1184 "</tr>";
1185 }
1186 if (defined $extra) {
1187 print "<tr>\n" .
1188 "<td colspan=\"3\">$extra</td>\n" .
1189 "</tr>\n";
1190 }
1191 print "</table>\n";
1192}
1193
1194## ----------------------------------------------------------------------
1195## functions printing large fragments, format as one of arguments
1196
1197sub git_diff_print {
1198 my $from = shift;
1199 my $from_name = shift;
1200 my $to = shift;
1201 my $to_name = shift;
1202 my $format = shift || "html";
1203
1204 my $from_tmp = "/dev/null";
1205 my $to_tmp = "/dev/null";
1206 my $pid = $$;
1207
1208 # create tmp from-file
1209 if (defined $from) {
1210 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1211 open my $fd2, "> $from_tmp";
1212 open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1213 my @file = <$fd>;
1214 print $fd2 @file;
1215 close $fd2;
1216 close $fd;
1217 }
1218
1219 # create tmp to-file
1220 if (defined $to) {
1221 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1222 open my $fd2, "> $to_tmp";
1223 open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1224 my @file = <$fd>;
1225 print $fd2 @file;
1226 close $fd2;
1227 close $fd;
1228 }
1229
1230 open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1231 if ($format eq "plain") {
1232 undef $/;
1233 print <$fd>;
1234 $/ = "\n";
1235 } else {
1236 while (my $line = <$fd>) {
1237 chomp $line;
1238 my $char = substr($line, 0, 1);
1239 my $diff_class = "";
1240 if ($char eq '+') {
1241 $diff_class = " add";
1242 } elsif ($char eq "-") {
1243 $diff_class = " rem";
1244 } elsif ($char eq "@") {
1245 $diff_class = " chunk_header";
1246 } elsif ($char eq "\\") {
1247 # skip errors
1248 next;
1249 }
1250 while ((my $pos = index($line, "\t")) != -1) {
1251 if (my $count = (8 - (($pos-1) % 8))) {
1252 my $spaces = ' ' x $count;
1253 $line =~ s/\t/$spaces/;
1254 }
1255 }
1256 print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1257 }
1258 }
1259 close $fd;
1260
1261 if (defined $from) {
1262 unlink($from_tmp);
1263 }
1264 if (defined $to) {
1265 unlink($to_tmp);
1266 }
1267}
1268
1269
1270## ======================================================================
1271## ======================================================================
1272## actions
1273
717b8311 1274sub git_project_list {
6326b60c
JN
1275 my $order = $cgi->param('o');
1276 if (defined $order && $order !~ m/project|descr|owner|age/) {
1277 die_error(undef, "Invalid order parameter '$order'.");
1278 }
1279
717b8311
JN
1280 my @list = git_read_projects();
1281 my @projects;
1282 if (!@list) {
6326b60c 1283 die_error(undef, "No projects found.");
717b8311
JN
1284 }
1285 foreach my $pr (@list) {
1286 my $head = git_read_head($pr->{'path'});
1287 if (!defined $head) {
1288 next;
9f5dcb81 1289 }
717b8311
JN
1290 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1291 my %co = git_read_commit($head);
1292 if (!%co) {
1293 next;
9f5dcb81 1294 }
717b8311
JN
1295 $pr->{'commit'} = \%co;
1296 if (!defined $pr->{'descr'}) {
1297 my $descr = git_read_description($pr->{'path'}) || "";
1298 $pr->{'descr'} = chop_str($descr, 25, 5);
9f5dcb81 1299 }
717b8311
JN
1300 if (!defined $pr->{'owner'}) {
1301 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
9f5dcb81 1302 }
717b8311 1303 push @projects, $pr;
9f5dcb81 1304 }
6326b60c 1305
717b8311
JN
1306 git_header_html();
1307 if (-f $home_text) {
1308 print "<div class=\"index_include\">\n";
1309 open (my $fd, $home_text);
1310 print <$fd>;
1311 close $fd;
1312 print "</div>\n";
9f5dcb81 1313 }
717b8311
JN
1314 print "<table class=\"project_list\">\n" .
1315 "<tr>\n";
6326b60c
JN
1316 $order ||= "project";
1317 if ($order eq "project") {
717b8311
JN
1318 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
1319 print "<th>Project</th>\n";
1320 } else {
6326b60c
JN
1321 print "<th>" .
1322 $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
1323 -class => "header"}, "Project") .
1324 "</th>\n";
717b8311 1325 }
6326b60c 1326 if ($order eq "descr") {
717b8311
JN
1327 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
1328 print "<th>Description</th>\n";
1329 } else {
6326b60c
JN
1330 print "<th>" .
1331 $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
1332 -class => "header"}, "Description") .
1333 "</th>\n";
717b8311 1334 }
6326b60c 1335 if ($order eq "owner") {
717b8311
JN
1336 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
1337 print "<th>Owner</th>\n";
1338 } else {
6326b60c
JN
1339 print "<th>" .
1340 $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
1341 -class => "header"}, "Owner") .
1342 "</th>\n";
717b8311 1343 }
6326b60c 1344 if ($order eq "age") {
717b8311
JN
1345 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
1346 print "<th>Last Change</th>\n";
1347 } else {
6326b60c
JN
1348 print "<th>" .
1349 $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
1350 -class => "header"}, "Last Change") .
1351 "</th>\n";
717b8311
JN
1352 }
1353 print "<th></th>\n" .
1354 "</tr>\n";
9f5dcb81 1355 my $alternate = 0;
717b8311 1356 foreach my $pr (@projects) {
9f5dcb81
JN
1357 if ($alternate) {
1358 print "<tr class=\"dark\">\n";
1359 } else {
1360 print "<tr class=\"light\">\n";
1361 }
1362 $alternate ^= 1;
6326b60c
JN
1363 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"),
1364 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
717b8311
JN
1365 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
1366 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
6326b60c
JN
1367 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
1368 $pr->{'commit'}{'age_string'} . "</td>\n" .
9f5dcb81 1369 "<td class=\"link\">" .
6326b60c
JN
1370 $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") . " | " .
1371 $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") . " | " .
1372 $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
9f5dcb81 1373 "</td>\n" .
9f5dcb81
JN
1374 "</tr>\n";
1375 }
1376 print "</table>\n";
717b8311 1377 git_footer_html();
9f5dcb81
JN
1378}
1379
ede5e100
KS
1380sub git_summary {
1381 my $descr = git_read_description($project) || "none";
df2c37a5 1382 my $head = git_read_head($project);
ede5e100
KS
1383 my %co = git_read_commit($head);
1384 my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1385
1386 my $owner;
1387 if (-f $projects_list) {
1388 open (my $fd , $projects_list);
1389 while (my $line = <$fd>) {
1390 chomp $line;
7403d50b
KS
1391 my ($pr, $ow) = split ' ', $line;
1392 $pr = unescape($pr);
1393 $ow = unescape($ow);
ede5e100 1394 if ($pr eq $project) {
281bf0cf 1395 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
ede5e100
KS
1396 last;
1397 }
1398 }
1399 close $fd;
1400 }
1401 if (!defined $owner) {
1402 $owner = get_file_owner("$projectroot/$project");
1403 }
1404
6a928415 1405 my $refs = read_info_ref();
ede5e100 1406 git_header_html();
0d83ddc4 1407 git_page_nav('summary','', $head);
9f5dcb81 1408
19806691 1409 print "<div class=\"title\">&nbsp;</div>\n";
bddec01d 1410 print "<table cellspacing=\"0\">\n" .
40c13813 1411 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
ede5e100
KS
1412 "<tr><td>owner</td><td>$owner</td></tr>\n" .
1413 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
bddec01d 1414 "</table>\n";
9f5dcb81 1415
b9182987 1416 open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_read_head($project)
044bfdc8 1417 or die_error(undef, "Open git-rev-list failed.");
0881d2d1 1418 my @revlist = map { chomp; $_ } <$fd>;
ede5e100 1419 close $fd;
27fb8c40 1420 git_header_div('shortlog');
9f5dcb81
JN
1421 git_shortlog_body(\@revlist, 0, 15, $refs,
1422 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "..."));
ede5e100 1423
0db37973 1424 my $taglist = git_read_refs("refs/tags");
ede5e100 1425 if (defined @$taglist) {
27fb8c40 1426 git_header_div('tags');
9f5dcb81
JN
1427 git_tags_body($taglist, 0, 15,
1428 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "..."));
ede5e100 1429 }
0db37973 1430
d8f1c5c2
KS
1431 my $headlist = git_read_refs("refs/heads");
1432 if (defined @$headlist) {
27fb8c40 1433 git_header_div('heads');
b77aeb24 1434 git_heads_body($headlist, $head, 0, 15,
9f5dcb81 1435 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "..."));
0db37973 1436 }
9f5dcb81 1437
ede5e100
KS
1438 git_footer_html();
1439}
1440
d8a20ba9 1441sub git_tag {
df2c37a5 1442 my $head = git_read_head($project);
d8a20ba9 1443 git_header_html();
0d83ddc4 1444 git_page_nav('','', $head,undef,$head);
d8a20ba9 1445 my %tag = git_read_tag($hash);
27fb8c40 1446 git_header_div('commit', esc_html($tag{'name'}), $hash);
d8a20ba9
KS
1447 print "<div class=\"title_text\">\n" .
1448 "<table cellspacing=\"0\">\n" .
e4669df9
KS
1449 "<tr>\n" .
1450 "<td>object</td>\n" .
232ff553
KS
1451 "<td>" . $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'object'}) . "</td>\n" .
1452 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'type'}) . "</td>\n" .
e4669df9 1453 "</tr>\n";
d8a20ba9
KS
1454 if (defined($tag{'author'})) {
1455 my %ad = date_str($tag{'epoch'}, $tag{'tz'});
40c13813 1456 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
d8a20ba9
KS
1457 print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1458 }
1459 print "</table>\n\n" .
1460 "</div>\n";
1461 print "<div class=\"page_body\">";
1462 my $comment = $tag{'comment'};
1463 foreach my $line (@$comment) {
40c13813 1464 print esc_html($line) . "<br/>\n";
d8a20ba9
KS
1465 }
1466 print "</div>\n";
1467 git_footer_html();
1468}
1469
1f2857ea
LT
1470sub git_blame2 {
1471 my $fd;
1472 my $ftype;
1473 die_error(undef, "Permission denied.") if (!git_get_project_config_bool ('blame'));
1474 die_error('404 Not Found', "File name not defined") if (!$file_name);
1475 $hash_base ||= git_read_head($project);
1476 die_error(undef, "Reading commit failed") unless ($hash_base);
1477 my %co = git_read_commit($hash_base)
1478 or die_error(undef, "Reading commit failed");
1479 if (!defined $hash) {
1480 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1481 or die_error(undef, "Error looking up file");
1482 }
1483 $ftype = git_get_type($hash);
1484 if ($ftype !~ "blob") {
1485 die_error("400 Bad Request", "object is not a blob");
1486 }
1487 open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
044bfdc8 1488 or die_error(undef, "Open git-blame failed.");
1f2857ea 1489 git_header_html();
0d83ddc4
JN
1490 my $formats_nav =
1491 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1492 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1493 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
27fb8c40 1494 git_header_div('commit', esc_html($co{'title'}), $hash_base);
1f2857ea 1495 git_print_page_path($file_name, $ftype);
cc1bf97e
LT
1496 my @rev_color = (qw(light dark));
1497 my $num_colors = scalar(@rev_color);
1498 my $current_color = 0;
1499 my $last_rev;
1f2857ea 1500 print "<div class=\"page_body\">\n";
1f2857ea
LT
1501 print "<table class=\"blame\">\n";
1502 print "<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n";
acb0f6f3
LT
1503 while (<$fd>) {
1504 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
1505 my $full_rev = $1;
1f2857ea 1506 my $rev = substr($full_rev, 0, 8);
acb0f6f3
LT
1507 my $lineno = $2;
1508 my $data = $3;
1f2857ea 1509
cc1bf97e
LT
1510 if (!defined $last_rev) {
1511 $last_rev = $full_rev;
1512 } elsif ($last_rev ne $full_rev) {
1513 $last_rev = $full_rev;
1514 $current_color = ++$current_color % $num_colors;
1515 }
1516 print "<tr class=\"$rev_color[$current_color]\">\n";
1f2857ea
LT
1517 print "<td class=\"sha1\">" .
1518 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$full_rev;f=$file_name")}, esc_html($rev)) . "</td>\n";
1519 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" . esc_html($lineno) . "</a></td>\n";
1520 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
1521 print "</tr>\n";
1522 }
1523 print "</table>\n";
1524 print "</div>";
1f2857ea
LT
1525 close $fd or print "Reading blob failed\n";
1526 git_footer_html();
1527}
1528
e34ef621
FF
1529sub git_blame {
1530 my $fd;
5996ca08 1531 die_error('403 Permission denied', "Permission denied.") if (!git_get_project_config_bool ('blame'));
e34ef621
FF
1532 die_error('404 Not Found', "What file will it be, master?") if (!$file_name);
1533 $hash_base ||= git_read_head($project);
1534 die_error(undef, "Reading commit failed.") unless ($hash_base);
1535 my %co = git_read_commit($hash_base)
1536 or die_error(undef, "Reading commit failed.");
1537 if (!defined $hash) {
1538 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1539 or die_error(undef, "Error lookup file.");
1540 }
e130ddaa 1541 open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
044bfdc8 1542 or die_error(undef, "Open git-annotate failed.");
e34ef621 1543 git_header_html();
0d83ddc4
JN
1544 my $formats_nav =
1545 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1546 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1547 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
27fb8c40 1548 git_header_div('commit', esc_html($co{'title'}), $hash_base);
5d1acf4d 1549 git_print_page_path($file_name, 'blob');
e34ef621
FF
1550 print "<div class=\"page_body\">\n";
1551 print <<HTML;
1f1ab5f0 1552<table class="blame">
e34ef621
FF
1553 <tr>
1554 <th>Commit</th>
1555 <th>Age</th>
1556 <th>Author</th>
1557 <th>Line</th>
1558 <th>Data</th>
1559 </tr>
1560HTML
1561 my @line_class = (qw(light dark));
1562 my $line_class_len = scalar (@line_class);
1563 my $line_class_num = $#line_class;
1564 while (my $line = <$fd>) {
1565 my $long_rev;
1566 my $short_rev;
1567 my $author;
1568 my $time;
1569 my $lineno;
1570 my $data;
1571 my $age;
1572 my $age_str;
1f1ab5f0 1573 my $age_class;
e34ef621
FF
1574
1575 chomp $line;
1576 $line_class_num = ($line_class_num + 1) % $line_class_len;
1577
1578 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
1579 $long_rev = $1;
1580 $author = $2;
1581 $time = $3;
1582 $lineno = $4;
1583 $data = $5;
1584 } else {
1f1ab5f0 1585 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
e34ef621
FF
1586 next;
1587 }
1588 $short_rev = substr ($long_rev, 0, 8);
1589 $age = time () - $time;
1590 $age_str = age_string ($age);
1591 $age_str =~ s/ /&nbsp;/g;
1f1ab5f0 1592 $age_class = age_class($age);
e34ef621
FF
1593 $author = esc_html ($author);
1594 $author =~ s/ /&nbsp;/g;
1595 # escape tabs
1596 while ((my $pos = index($data, "\t")) != -1) {
1597 if (my $count = (8 - ($pos % 8))) {
1598 my $spaces = ' ' x $count;
1599 $data =~ s/\t/$spaces/;
1600 }
717b8311
JN
1601 }
1602 $data = esc_html ($data);
2d007374 1603
717b8311
JN
1604 print <<HTML;
1605 <tr class="$line_class[$line_class_num]">
1606 <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1607 <td class="$age_class">$age_str</td>
1608 <td>$author</td>
1609 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1610 <td class="pre">$data</td>
1611 </tr>
1612HTML
1613 } # while (my $line = <$fd>)
1614 print "</table>\n\n";
1615 close $fd or print "Reading blob failed.\n";
1616 print "</div>";
1617 git_footer_html();
2d007374
PB
1618}
1619
717b8311
JN
1620sub git_tags {
1621 my $head = git_read_head($project);
1622 git_header_html();
1623 git_page_nav('','', $head,undef,$head);
1624 git_header_div('summary', $project);
2d007374 1625
717b8311
JN
1626 my $taglist = git_read_refs("refs/tags");
1627 if (defined @$taglist) {
1628 git_tags_body($taglist);
2d007374 1629 }
717b8311 1630 git_footer_html();
2d007374
PB
1631}
1632
717b8311
JN
1633sub git_heads {
1634 my $head = git_read_head($project);
1635 git_header_html();
1636 git_page_nav('','', $head,undef,$head);
1637 git_header_div('summary', $project);
930cf7dd 1638
717b8311
JN
1639 my $taglist = git_read_refs("refs/heads");
1640 my $alternate = 0;
1641 if (defined @$taglist) {
1642 git_heads_body($taglist, $head);
f5aa79d9 1643 }
717b8311 1644 git_footer_html();
f5aa79d9
JN
1645}
1646
19806691 1647sub git_blob_plain {
cff0771b 1648 if (!defined $hash) {
5be01bc8
JN
1649 if (defined $file_name) {
1650 my $base = $hash_base || git_read_head($project);
1651 $hash = git_get_hash_by_path($base, $file_name, "blob")
1652 or die_error(undef, "Error lookup file.");
1653 } else {
1654 die_error(undef, "No file name defined.");
1655 }
1656 }
930cf7dd 1657 my $type = shift;
044bfdc8
JN
1658 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1659 or die_error("Couldn't cat $file_name, $hash");
930cf7dd
LT
1660
1661 $type ||= git_blob_plain_mimetype($fd, $file_name);
f5aa79d9
JN
1662
1663 # save as filename, even when no $file_name is given
1664 my $save_as = "$hash";
9312944d
KS
1665 if (defined $file_name) {
1666 $save_as = $file_name;
f5aa79d9
JN
1667 } elsif ($type =~ m/^text\//) {
1668 $save_as .= '.txt';
9312944d 1669 }
f5aa79d9
JN
1670
1671 print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
19806691 1672 undef $/;
ad14e931 1673 binmode STDOUT, ':raw';
19806691 1674 print <$fd>;
ad14e931 1675 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
19806691
KS
1676 $/ = "\n";
1677 close $fd;
1678}
1679
930cf7dd 1680sub git_blob {
cff0771b 1681 if (!defined $hash) {
5be01bc8
JN
1682 if (defined $file_name) {
1683 my $base = $hash_base || git_read_head($project);
1684 $hash = git_get_hash_by_path($base, $file_name, "blob")
1685 or die_error(undef, "Error lookup file.");
1686 } else {
1687 die_error(undef, "No file name defined.");
1688 }
1689 }
930cf7dd 1690 my $have_blame = git_get_project_config_bool ('blame');
044bfdc8
JN
1691 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1692 or die_error(undef, "Couldn't cat $file_name, $hash.");
930cf7dd
LT
1693 my $mimetype = git_blob_plain_mimetype($fd, $file_name);
1694 if ($mimetype !~ m/^text\//) {
1695 close $fd;
1696 return git_blob_plain($mimetype);
1697 }
1698 git_header_html();
0d83ddc4 1699 my $formats_nav = '';
930cf7dd 1700 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
930cf7dd
LT
1701 if (defined $file_name) {
1702 if ($have_blame) {
0d83ddc4 1703 $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | ";
930cf7dd 1704 }
0d83ddc4
JN
1705 $formats_nav .=
1706 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1707 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head");
930cf7dd 1708 } else {
0d83ddc4 1709 $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain");
930cf7dd 1710 }
0d83ddc4 1711 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
27fb8c40 1712 git_header_div('commit', esc_html($co{'title'}), $hash_base);
930cf7dd
LT
1713 } else {
1714 print "<div class=\"page_nav\">\n" .
1715 "<br/><br/></div>\n" .
1716 "<div class=\"title\">$hash</div>\n";
1717 }
63433106 1718 git_print_page_path($file_name, "blob");
930cf7dd
LT
1719 print "<div class=\"page_body\">\n";
1720 my $nr;
1721 while (my $line = <$fd>) {
1722 chomp $line;
1723 $nr++;
1724 while ((my $pos = index($line, "\t")) != -1) {
1725 if (my $count = (8 - ($pos % 8))) {
1726 my $spaces = ' ' x $count;
1727 $line =~ s/\t/$spaces/;
1728 }
1729 }
1730 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1731 }
1732 close $fd or print "Reading blob failed.\n";
1733 print "</div>";
1734 git_footer_html();
1735}
1736
09bd7898 1737sub git_tree {
b87d78d6 1738 if (!defined $hash) {
df2c37a5 1739 $hash = git_read_head($project);
09bd7898 1740 if (defined $file_name) {
df2c37a5 1741 my $base = $hash_base || $hash;
09bd7898
KS
1742 $hash = git_get_hash_by_path($base, $file_name, "tree");
1743 }
10dba28d 1744 if (!defined $hash_base) {
df2c37a5 1745 $hash_base = $hash;
10dba28d 1746 }
e925f38c 1747 }
232ff553 1748 $/ = "\0";
044bfdc8
JN
1749 open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
1750 or die_error(undef, "Open git-ls-tree failed.");
0881d2d1 1751 my @entries = map { chomp; $_ } <$fd>;
19806691 1752 close $fd or die_error(undef, "Reading tree failed.");
232ff553 1753 $/ = "\n";
d63577da 1754
edde3735 1755 my $refs = read_info_ref();
594e212b 1756 my $ref = git_get_referencing($refs, $hash_base);
12a88f2f 1757 git_header_html();
09bd7898 1758 my $base_key = "";
09bd7898
KS
1759 my $base = "";
1760 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1761 $base_key = ";hb=$hash_base";
0d83ddc4 1762 git_page_nav('tree','', $hash_base);
27fb8c40 1763 git_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
d63577da
KS
1764 } else {
1765 print "<div class=\"page_nav\">\n";
1766 print "<br/><br/></div>\n";
1767 print "<div class=\"title\">$hash</div>\n";
1768 }
09bd7898 1769 if (defined $file_name) {
232ff553 1770 $base = esc_html("$file_name/");
09bd7898 1771 }
5d1acf4d 1772 git_print_page_path($file_name, 'tree');
fbb592a9 1773 print "<div class=\"page_body\">\n";
42f7eb94 1774 print "<table cellspacing=\"0\">\n";
bddec01d 1775 my $alternate = 0;
161332a5 1776 foreach my $line (@entries) {
c068cff1 1777 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
19806691 1778 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
d767d59c 1779 my $t_mode = $1;
161332a5
KS
1780 my $t_type = $2;
1781 my $t_hash = $3;
232ff553 1782 my $t_name = validate_input($4);
bddec01d 1783 if ($alternate) {
c994d620 1784 print "<tr class=\"dark\">\n";
bddec01d 1785 } else {
c994d620 1786 print "<tr class=\"light\">\n";
bddec01d
KS
1787 }
1788 $alternate ^= 1;
1f1ab5f0 1789 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
161332a5 1790 if ($t_type eq "blob") {
10dba28d 1791 print "<td class=\"list\">" .
232ff553 1792 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name"), -class => "list"}, esc_html($t_name)) .
c994d620
KS
1793 "</td>\n" .
1794 "<td class=\"link\">" .
232ff553 1795 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") .
e34ef621 1796# " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") .
cff0771b 1797 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$t_hash;hb=$hash_base;f=$base$t_name")}, "history") .
25b7c18e 1798 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") .
42f7eb94 1799 "</td>\n";
161332a5 1800 } elsif ($t_type eq "tree") {
10dba28d 1801 print "<td class=\"list\">" .
232ff553 1802 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
19806691 1803 "</td>\n" .
c994d620 1804 "<td class=\"link\">" .
232ff553 1805 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
c6e1d9ed 1806 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash_base;f=$base$t_name")}, "history") .
c994d620 1807 "</td>\n";
161332a5 1808 }
42f7eb94 1809 print "</tr>\n";
161332a5 1810 }
42f7eb94
KS
1811 print "</table>\n" .
1812 "</div>";
12a88f2f 1813 git_footer_html();
09bd7898
KS
1814}
1815
09bd7898 1816sub git_log {
df2c37a5 1817 my $head = git_read_head($project);
0db37973 1818 if (!defined $hash) {
19806691 1819 $hash = $head;
0db37973 1820 }
ea4a6df4
KS
1821 if (!defined $page) {
1822 $page = 0;
b87d78d6 1823 }
6a928415 1824 my $refs = read_info_ref();
ea4a6df4
KS
1825
1826 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
044bfdc8
JN
1827 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
1828 or die_error(undef, "Open git-rev-list failed.");
0881d2d1 1829 my @revlist = map { chomp; $_ } <$fd>;
ea4a6df4
KS
1830 close $fd;
1831
6855f42e 1832 my $paging_nav = git_get_paging_nav('log', $hash, $head, $page, $#revlist);
0d83ddc4
JN
1833
1834 git_header_html();
1835 git_page_nav('log','', $hash,undef,undef, $paging_nav);
1836
b87d78d6 1837 if (!@revlist) {
0db37973 1838 my %co = git_read_commit($hash);
27fb8c40
JN
1839
1840 git_header_div('summary', $project);
e925f38c 1841 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
161332a5 1842 }
c994d620
KS
1843 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
1844 my $commit = $revlist[$i];
594e212b 1845 my $ref = git_get_referencing($refs, $commit);
09bd7898 1846 my %co = git_read_commit($commit);
b87d78d6 1847 next if !%co;
034df39e 1848 my %ad = date_str($co{'author_epoch'});
27fb8c40
JN
1849 git_header_div('commit',
1850 "<span class=\"age\">$co{'age_string'}</span>" .
1851 esc_html($co{'title'}) . $ref,
1852 $commit);
034df39e
KS
1853 print "<div class=\"title_text\">\n" .
1854 "<div class=\"log_link\">\n" .
232ff553
KS
1855 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1856 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
eb28240b 1857 "<br/>\n" .
034df39e 1858 "</div>\n" .
40c13813 1859 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
034df39e
KS
1860 "</div>\n" .
1861 "<div class=\"log_body\">\n";
1862 my $comment = $co{'comment'};
09bd7898 1863 my $empty = 0;
034df39e 1864 foreach my $line (@$comment) {
10dba28d 1865 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
09bd7898
KS
1866 next;
1867 }
1868 if ($line eq "") {
1869 if ($empty) {
1870 next;
1871 }
1872 $empty = 1;
1873 } else {
1874 $empty = 0;
1875 }
f49201a9 1876 print format_log_line_html($line) . "<br/>\n";
034df39e 1877 }
09bd7898
KS
1878 if (!$empty) {
1879 print "<br/>\n";
1880 }
1881 print "</div>\n";
e334d18c 1882 }
034df39e 1883 git_footer_html();
09bd7898
KS
1884}
1885
1886sub git_commit {
1887 my %co = git_read_commit($hash);
034df39e 1888 if (!%co) {
09bd7898 1889 die_error(undef, "Unknown commit object.");
d63577da 1890 }
185f09e5
KS
1891 my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1892 my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
161332a5 1893
d8a20ba9
KS
1894 my $parent = $co{'parent'};
1895 if (!defined $parent) {
b9182987 1896 $parent = "--root";
6191f8e1 1897 }
b9182987 1898 open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
044bfdc8 1899 or die_error(undef, "Open git-diff-tree failed.");
0881d2d1 1900 my @difftree = map { chomp; $_ } <$fd>;
b9182987 1901 close $fd or die_error(undef, "Reading git-diff-tree failed.");
11044297
KS
1902
1903 # non-textual hash id's can be cached
1904 my $expires;
1905 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1906 $expires = "+1d";
1907 }
edde3735 1908 my $refs = read_info_ref();
594e212b 1909 my $ref = git_get_referencing($refs, $co{'id'});
0d83ddc4 1910 my $formats_nav = '';
4f7b34c9
LT
1911 if (defined $file_name && defined $co{'parent'}) {
1912 my $parent = $co{'parent'};
0d83ddc4 1913 $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;hb=$parent;f=$file_name")}, "blame");
4f7b34c9 1914 }
594e212b 1915 git_header_html(undef, $expires);
0d83ddc4
JN
1916 git_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
1917 $hash, $co{'tree'}, $hash,
1918 $formats_nav);
4f7b34c9 1919
b87d78d6 1920 if (defined $co{'parent'}) {
27fb8c40 1921 git_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
b87d78d6 1922 } else {
594e212b 1923 git_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
b87d78d6 1924 }
6191f8e1 1925 print "<div class=\"title_text\">\n" .
b87d78d6 1926 "<table cellspacing=\"0\">\n";
40c13813 1927 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
bddec01d
KS
1928 "<tr>" .
1929 "<td></td><td> $ad{'rfc2822'}";
927dcec4 1930 if ($ad{'hour_local'} < 6) {
1f1ab5f0 1931 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
b87d78d6
KS
1932 } else {
1933 printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1934 }
bddec01d
KS
1935 print "</td>" .
1936 "</tr>\n";
40c13813 1937 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
e925f38c 1938 print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1f1ab5f0 1939 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
bddec01d
KS
1940 print "<tr>" .
1941 "<td>tree</td>" .
1f1ab5f0 1942 "<td class=\"sha1\">" .
232ff553 1943 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), class => "list"}, $co{'tree'}) .
19806691 1944 "</td>" .
232ff553 1945 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
bddec01d
KS
1946 "</td>" .
1947 "</tr>\n";
8adc4bd4 1948 my $parents = $co{'parents'};
3e029299 1949 foreach my $par (@$parents) {
bddec01d
KS
1950 print "<tr>" .
1951 "<td>parent</td>" .
1f1ab5f0 1952 "<td class=\"sha1\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par"), class => "list"}, $par) . "</td>" .
bddec01d 1953 "<td class=\"link\">" .
232ff553
KS
1954 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par")}, "commit") .
1955 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash;hp=$par")}, "commitdiff") .
bddec01d
KS
1956 "</td>" .
1957 "</tr>\n";
3e029299 1958 }
7a9b4c5f 1959 print "</table>".
b87d78d6 1960 "</div>\n";
fbb592a9 1961 print "<div class=\"page_body\">\n";
3e029299 1962 my $comment = $co{'comment'};
09bd7898
KS
1963 my $empty = 0;
1964 my $signed = 0;
3e029299 1965 foreach my $line (@$comment) {
09bd7898
KS
1966 # print only one empty line
1967 if ($line eq "") {
1968 if ($empty || $signed) {
1969 next;
1970 }
1971 $empty = 1;
1972 } else {
1973 $empty = 0;
1974 }
10dba28d 1975 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
09bd7898 1976 $signed = 1;
1f1ab5f0 1977 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3e029299 1978 } else {
09bd7898 1979 $signed = 0;
f49201a9 1980 print format_log_line_html($line) . "<br/>\n";
3e029299
KS
1981 }
1982 }
927dcec4 1983 print "</div>\n";
09bd7898 1984 print "<div class=\"list_head\">\n";
6191f8e1 1985 if ($#difftree > 10) {
09bd7898 1986 print(($#difftree + 1) . " files changed:\n");
6191f8e1 1987 }
09bd7898 1988 print "</div>\n";
1f1ab5f0 1989 print "<table class=\"diff_tree\">\n";
bddec01d 1990 my $alternate = 0;
161332a5 1991 foreach my $line (@difftree) {
19806691
KS
1992 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1993 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
d8a20ba9
KS
1994 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1995 next;
1996 }
19806691
KS
1997 my $from_mode = $1;
1998 my $to_mode = $2;
1999 my $from_id = $3;
2000 my $to_id = $4;
2001 my $status = $5;
ea4a6df4 2002 my $similarity = $6;
232ff553 2003 my $file = validate_input(unquote($7));
bddec01d 2004 if ($alternate) {
c994d620 2005 print "<tr class=\"dark\">\n";
bddec01d 2006 } else {
c994d620 2007 print "<tr class=\"light\">\n";
bddec01d
KS
2008 }
2009 $alternate ^= 1;
f6375b24 2010 if ($status eq "A") {
10dba28d 2011 my $mode_chng = "";
19806691
KS
2012 if (S_ISREG(oct $to_mode)) {
2013 $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
10dba28d
KS
2014 }
2015 print "<td>" .
232ff553 2016 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
1f1ab5f0 2017 "<td><span class=\"file_status new\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
232ff553 2018 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob") . "</td>\n";
19806691 2019 } elsif ($status eq "D") {
10dba28d 2020 print "<td>" .
232ff553 2021 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
1f1ab5f0 2022 "<td><span class=\"file_status deleted\">[deleted " . file_type($from_mode). "]</span></td>\n" .
10dba28d 2023 "<td class=\"link\">" .
232ff553 2024 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, "blob") .
c6e1d9ed 2025 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") .
10dba28d 2026 "</td>\n"
19806691 2027 } elsif ($status eq "M" || $status eq "T") {
10dba28d
KS
2028 my $mode_chnge = "";
2029 if ($from_mode != $to_mode) {
1f1ab5f0 2030 $mode_chnge = " <span class=\"file_status mode_chnge\">[changed";
10dba28d
KS
2031 if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
2032 $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
b87d78d6 2033 }
10dba28d
KS
2034 if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
2035 if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
2036 $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
2037 } elsif (S_ISREG($to_mode)) {
2038 $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
b87d78d6 2039 }
9cd3d988 2040 }
10dba28d
KS
2041 $mode_chnge .= "]</span>\n";
2042 }
2043 print "<td>";
2044 if ($to_id ne $from_id) {
232ff553 2045 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
10dba28d 2046 } else {
232ff553 2047 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
161332a5 2048 }
10dba28d
KS
2049 print "</td>\n" .
2050 "<td>$mode_chnge</td>\n" .
2051 "<td class=\"link\">";
232ff553 2052 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob");
10dba28d 2053 if ($to_id ne $from_id) {
232ff553 2054 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file")}, "diff");
10dba28d 2055 }
c6e1d9ed 2056 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") . "\n";
10dba28d 2057 print "</td>\n";
dcea8d0b
KS
2058 } elsif ($status eq "R") {
2059 my ($from_file, $to_file) = split "\t", $file;
2060 my $mode_chng = "";
2061 if ($from_mode != $to_mode) {
2062 $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
2063 }
2064 print "<td>" .
232ff553 2065 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file"), -class => "list"}, esc_html($to_file)) . "</td>\n" .
1f1ab5f0 2066 "<td><span class=\"file_status moved\">[moved from " .
232ff553 2067 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$from_file"), -class => "list"}, esc_html($from_file)) .
ea4a6df4 2068 " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
dcea8d0b 2069 "<td class=\"link\">" .
232ff553 2070 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
dcea8d0b 2071 if ($to_id ne $from_id) {
232ff553 2072 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file")}, "diff");
dcea8d0b
KS
2073 }
2074 print "</td>\n";
161332a5 2075 }
10dba28d 2076 print "</tr>\n";
161332a5 2077 }
bddec01d 2078 print "</table>\n";
12a88f2f 2079 git_footer_html();
09bd7898
KS
2080}
2081
2082sub git_blobdiff {
19806691 2083 mkdir($git_temp, 0700);
12a88f2f 2084 git_header_html();
09bd7898 2085 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
0d83ddc4
JN
2086 my $formats_nav =
2087 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
2088 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
27fb8c40 2089 git_header_div('commit', esc_html($co{'title'}), $hash_base);
09bd7898
KS
2090 } else {
2091 print "<div class=\"page_nav\">\n" .
2092 "<br/><br/></div>\n" .
2093 "<div class=\"title\">$hash vs $hash_parent</div>\n";
2094 }
63433106 2095 git_print_page_path($file_name, "blob");
9cd3d988 2096 print "<div class=\"page_body\">\n" .
c07ad4b9 2097 "<div class=\"diff_info\">blob:" .
232ff553 2098 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) .
2735983d 2099 " -> blob:" .
232ff553 2100 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, $hash) .
c07ad4b9 2101 "</div>\n";
19806691 2102 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
c07ad4b9 2103 print "</div>";
12a88f2f 2104 git_footer_html();
09bd7898
KS
2105}
2106
19806691
KS
2107sub git_blobdiff_plain {
2108 mkdir($git_temp, 0700);
2109 print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2110 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2111}
2112
09bd7898 2113sub git_commitdiff {
19806691 2114 mkdir($git_temp, 0700);
09bd7898 2115 my %co = git_read_commit($hash);
034df39e 2116 if (!%co) {
09bd7898 2117 die_error(undef, "Unknown commit object.");
d63577da 2118 }
bddec01d
KS
2119 if (!defined $hash_parent) {
2120 $hash_parent = $co{'parent'};
2121 }
b9182987 2122 open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
044bfdc8 2123 or die_error(undef, "Open git-diff-tree failed.");
0881d2d1 2124 my @difftree = map { chomp; $_ } <$fd>;
19806691 2125 close $fd or die_error(undef, "Reading diff-tree failed.");
161332a5 2126
11044297
KS
2127 # non-textual hash id's can be cached
2128 my $expires;
2129 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2130 $expires = "+1d";
2131 }
edde3735 2132 my $refs = read_info_ref();
594e212b 2133 my $ref = git_get_referencing($refs, $co{'id'});
0d83ddc4
JN
2134 my $formats_nav =
2135 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
594e212b 2136 git_header_html(undef, $expires);
0d83ddc4 2137 git_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
27fb8c40 2138 git_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
c07ad4b9 2139 print "<div class=\"page_body\">\n";
0db37973
KS
2140 my $comment = $co{'comment'};
2141 my $empty = 0;
2142 my $signed = 0;
fa378499 2143 my @log = @$comment;
a4d26ef0 2144 # remove first and empty lines after that
fa378499 2145 shift @log;
a4d26ef0
KS
2146 while (defined $log[0] && $log[0] eq "") {
2147 shift @log;
2148 }
fa378499 2149 foreach my $line (@log) {
10dba28d 2150 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
0db37973
KS
2151 next;
2152 }
2153 if ($line eq "") {
2154 if ($empty) {
2155 next;
2156 }
2157 $empty = 1;
2158 } else {
2159 $empty = 0;
2160 }
f49201a9 2161 print format_log_line_html($line) . "<br/>\n";
0db37973
KS
2162 }
2163 print "<br/>\n";
4c02e3c5 2164 foreach my $line (@difftree) {
19806691
KS
2165 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2166 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2167 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2168 my $from_mode = $1;
2169 my $to_mode = $2;
2170 my $from_id = $3;
2171 my $to_id = $4;
2172 my $status = $5;
232ff553 2173 my $file = validate_input(unquote($6));
f6375b24 2174 if ($status eq "A") {
5be01bc8 2175 print "<div class=\"diff_info\">" . file_type($to_mode) . ":" .
232ff553 2176 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id) . "(new)" .
19806691
KS
2177 "</div>\n";
2178 git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2179 } elsif ($status eq "D") {
2180 print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
232ff553 2181 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) . "(deleted)" .
19806691
KS
2182 "</div>\n";
2183 git_diff_print($from_id, "a/$file", undef, "/dev/null");
2184 } elsif ($status eq "M") {
2185 if ($from_id ne $to_id) {
2186 print "<div class=\"diff_info\">" .
232ff553 2187 file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) .
19806691 2188 " -> " .
232ff553 2189 file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id);
19806691
KS
2190 print "</div>\n";
2191 git_diff_print($from_id, "a/$file", $to_id, "b/$file");
4c02e3c5
KS
2192 }
2193 }
161332a5 2194 }
c07ad4b9
KS
2195 print "<br/>\n" .
2196 "</div>";
12a88f2f 2197 git_footer_html();
09bd7898
KS
2198}
2199
19806691
KS
2200sub git_commitdiff_plain {
2201 mkdir($git_temp, 0700);
b9182987 2202 open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
044bfdc8 2203 or die_error(undef, "Open git-diff-tree failed.");
0881d2d1 2204 my @difftree = map { chomp; $_ } <$fd>;
19806691
KS
2205 close $fd or die_error(undef, "Reading diff-tree failed.");
2206
1b1cd421
KS
2207 # try to figure out the next tag after this commit
2208 my $tagname;
4df11910 2209 my $refs = read_info_ref("tags");
b9182987 2210 open $fd, "-|", $GIT, "rev-list", "HEAD";
0881d2d1 2211 my @commits = map { chomp; $_ } <$fd>;
6a928415
KS
2212 close $fd;
2213 foreach my $commit (@commits) {
2214 if (defined $refs->{$commit}) {
2215 $tagname = $refs->{$commit}
1b1cd421
KS
2216 }
2217 if ($commit eq $hash) {
2218 last;
2219 }
2220 }
1b1cd421 2221
9312944d 2222 print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
c994d620
KS
2223 my %co = git_read_commit($hash);
2224 my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
c994d620 2225 my $comment = $co{'comment'};
1b1cd421 2226 print "From: $co{'author'}\n" .
c994d620 2227 "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
1b1cd421
KS
2228 "Subject: $co{'title'}\n";
2229 if (defined $tagname) {
5be01bc8 2230 print "X-Git-Tag: $tagname\n";
1b1cd421
KS
2231 }
2232 print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
c994d620 2233 "\n";
1b1cd421 2234
c994d620 2235 foreach my $line (@$comment) {;
c2488d06 2236 print "$line\n";
c994d620 2237 }
1b1cd421
KS
2238 print "---\n\n";
2239
19806691
KS
2240 foreach my $line (@difftree) {
2241 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2242 my $from_id = $3;
2243 my $to_id = $4;
2244 my $status = $5;
2245 my $file = $6;
f6375b24 2246 if ($status eq "A") {
19806691
KS
2247 git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2248 } elsif ($status eq "D") {
2249 git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2250 } elsif ($status eq "M") {
2251 git_diff_print($from_id, "a/$file", $to_id, "b/$file", "plain");
2252 }
2253 }
2254}
2255
09bd7898 2256sub git_history {
c6e1d9ed
LT
2257 if (!defined $hash_base) {
2258 $hash_base = git_read_head($project);
09bd7898 2259 }
63433106 2260 my $ftype;
c6e1d9ed 2261 my %co = git_read_commit($hash_base);
09bd7898
KS
2262 if (!%co) {
2263 die_error(undef, "Unknown commit object.");
2ae100df 2264 }
6a928415 2265 my $refs = read_info_ref();
d51e902a 2266 git_header_html();
0d83ddc4 2267 git_page_nav('','', $hash_base,$co{'tree'},$hash_base);
27fb8c40 2268 git_header_div('commit', esc_html($co{'title'}), $hash_base);
93d5f061
LT
2269 if (!defined $hash && defined $file_name) {
2270 $hash = git_get_hash_by_path($hash_base, $file_name);
2271 }
cff0771b 2272 if (defined $hash) {
63433106 2273 $ftype = git_get_type($hash);
cff0771b 2274 }
63433106 2275 git_print_page_path($file_name, $ftype);
10dba28d 2276
cdd4037d 2277 open my $fd, "-|",
822c1859 2278 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
bddec01d
KS
2279 print "<table cellspacing=\"0\">\n";
2280 my $alternate = 0;
b87d78d6 2281 while (my $line = <$fd>) {
d05c19ee 2282 if ($line =~ m/^([0-9a-fA-F]{40})/){
cdd4037d 2283 my $commit = $1;
09bd7898 2284 my %co = git_read_commit($commit);
b87d78d6
KS
2285 if (!%co) {
2286 next;
2287 }
594e212b 2288 my $ref = git_get_referencing($refs, $commit);
bddec01d 2289 if ($alternate) {
c994d620 2290 print "<tr class=\"dark\">\n";
bddec01d 2291 } else {
c994d620 2292 print "<tr class=\"light\">\n";
bddec01d
KS
2293 }
2294 $alternate ^= 1;
71be1e79 2295 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
40c13813 2296 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
232ff553 2297 "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
4df11910 2298 esc_html(chop_str($co{'title'}, 50)) . "$ref</b>") . "</td>\n" .
10dba28d 2299 "<td class=\"link\">" .
232ff553
KS
2300 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2301 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
498934a7 2302 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$ftype;hb=$commit;f=$file_name")}, $ftype);
c6e1d9ed 2303 my $blob = git_get_hash_by_path($hash_base, $file_name);
42f7eb94
KS
2304 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2305 if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
c994d620 2306 print " | " .
232ff553 2307 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
c994d620 2308 "diff to current");
42f7eb94 2309 }
10dba28d
KS
2310 print "</td>\n" .
2311 "</tr>\n";
2ae100df 2312 }
d51e902a 2313 }
bddec01d 2314 print "</table>\n";
b87d78d6 2315 close $fd;
d51e902a 2316 git_footer_html();
161332a5 2317}
19806691
KS
2318
2319sub git_search {
2320 if (!defined $searchtext) {
2321 die_error("", "Text field empty.");
2322 }
2323 if (!defined $hash) {
df2c37a5 2324 $hash = git_read_head($project);
19806691
KS
2325 }
2326 my %co = git_read_commit($hash);
2327 if (!%co) {
2328 die_error(undef, "Unknown commit object.");
2329 }
c994d620
KS
2330 # pickaxe may take all resources of your box and run for several minutes
2331 # with every query - so decide by yourself how public you make this feature :)
2332 my $commit_search = 1;
2333 my $author_search = 0;
2334 my $committer_search = 0;
2335 my $pickaxe_search = 0;
2336 if ($searchtext =~ s/^author\\://i) {
2337 $author_search = 1;
2338 } elsif ($searchtext =~ s/^committer\\://i) {
2339 $committer_search = 1;
2340 } elsif ($searchtext =~ s/^pickaxe\\://i) {
2341 $commit_search = 0;
2342 $pickaxe_search = 1;
2343 }
19806691 2344 git_header_html();
0d83ddc4 2345 git_page_nav('','', $hash,$co{'tree'},$hash);
27fb8c40 2346 git_header_div('commit', esc_html($co{'title'}), $hash);
19806691 2347
19806691 2348 print "<table cellspacing=\"0\">\n";
19806691 2349 my $alternate = 0;
c994d620
KS
2350 if ($commit_search) {
2351 $/ = "\0";
b9182987 2352 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
c994d620
KS
2353 while (my $commit_text = <$fd>) {
2354 if (!grep m/$searchtext/i, $commit_text) {
2355 next;
19806691 2356 }
c994d620
KS
2357 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2358 next;
19806691 2359 }
c994d620 2360 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
19806691
KS
2361 next;
2362 }
c994d620 2363 my @commit_lines = split "\n", $commit_text;
25f422fb 2364 my %co = git_read_commit(undef, \@commit_lines);
c994d620
KS
2365 if (!%co) {
2366 next;
2367 }
2368 if ($alternate) {
2369 print "<tr class=\"dark\">\n";
2370 } else {
2371 print "<tr class=\"light\">\n";
2372 }
2373 $alternate ^= 1;
71be1e79 2374 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
40c13813 2375 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
c994d620 2376 "<td>" .
232ff553 2377 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" . esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
c994d620
KS
2378 my $comment = $co{'comment'};
2379 foreach my $line (@$comment) {
2380 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
40c13813 2381 my $lead = esc_html($1) || "";
c994d620 2382 $lead = chop_str($lead, 30, 10);
40c13813
KS
2383 my $match = esc_html($2) || "";
2384 my $trail = esc_html($3) || "";
c994d620 2385 $trail = chop_str($trail, 30, 10);
1f1ab5f0 2386 my $text = "$lead<span class=\"match\">$match</span>$trail";
c994d620 2387 print chop_str($text, 80, 5) . "<br/>\n";
19806691 2388 }
c994d620
KS
2389 }
2390 print "</td>\n" .
2391 "<td class=\"link\">" .
232ff553
KS
2392 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2393 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
c994d620
KS
2394 print "</td>\n" .
2395 "</tr>\n";
2396 }
2397 close $fd;
2398 }
2399
2400 if ($pickaxe_search) {
2401 $/ = "\n";
e130ddaa 2402 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
c994d620
KS
2403 undef %co;
2404 my @files;
2405 while (my $line = <$fd>) {
2406 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2407 my %set;
2408 $set{'file'} = $6;
2409 $set{'from_id'} = $3;
2410 $set{'to_id'} = $4;
2411 $set{'id'} = $set{'to_id'};
2412 if ($set{'id'} =~ m/0{40}/) {
2413 $set{'id'} = $set{'from_id'};
19806691 2414 }
c994d620
KS
2415 if ($set{'id'} =~ m/0{40}/) {
2416 next;
2417 }
2418 push @files, \%set;
53b89d8d 2419 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
c994d620
KS
2420 if (%co) {
2421 if ($alternate) {
2422 print "<tr class=\"dark\">\n";
2423 } else {
2424 print "<tr class=\"light\">\n";
2425 }
2426 $alternate ^= 1;
71be1e79 2427 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
40c13813 2428 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
c994d620 2429 "<td>" .
232ff553 2430 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
40c13813 2431 esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
c994d620
KS
2432 while (my $setref = shift @files) {
2433 my %set = %$setref;
232ff553 2434 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
1f1ab5f0 2435 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
c994d620
KS
2436 "<br/>\n";
2437 }
2438 print "</td>\n" .
2439 "<td class=\"link\">" .
232ff553
KS
2440 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2441 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
c994d620
KS
2442 print "</td>\n" .
2443 "</tr>\n";
2444 }
2445 %co = git_read_commit($1);
19806691 2446 }
19806691 2447 }
c994d620 2448 close $fd;
19806691
KS
2449 }
2450 print "</table>\n";
19806691
KS
2451 git_footer_html();
2452}
2453
2454sub git_shortlog {
df2c37a5 2455 my $head = git_read_head($project);
19806691
KS
2456 if (!defined $hash) {
2457 $hash = $head;
2458 }
ea4a6df4
KS
2459 if (!defined $page) {
2460 $page = 0;
2461 }
6a928415 2462 my $refs = read_info_ref();
ea4a6df4
KS
2463
2464 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
044bfdc8
JN
2465 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2466 or die_error(undef, "Open git-rev-list failed.");
0881d2d1 2467 my @revlist = map { chomp; $_ } <$fd>;
19806691 2468 close $fd;
ea4a6df4 2469
6855f42e 2470 my $paging_nav = git_get_paging_nav('shortlog', $hash, $head, $page, $#revlist);
9f5dcb81
JN
2471 my $next_link = '';
2472 if ($#revlist >= (100 * ($page+1)-1)) {
2473 $next_link =
2474 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)),
2475 -title => "Alt-n"}, "next");
2476 }
2477
0d83ddc4
JN
2478
2479 git_header_html();
2480 git_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
27fb8c40 2481 git_header_div('summary', $project);
0d83ddc4 2482
9f5dcb81
JN
2483 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
2484
19806691
KS
2485 git_footer_html();
2486}
717b8311
JN
2487
2488## ......................................................................
2489## feeds (RSS, OPML)
2490
2491sub git_rss {
2492 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
2493 open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_read_head($project)
2494 or die_error(undef, "Open git-rev-list failed.");
2495 my @revlist = map { chomp; $_ } <$fd>;
2496 close $fd or die_error(undef, "Reading rev-list failed.");
2497 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2498 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2499 "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
2500 print "<channel>\n";
2501 print "<title>$project</title>\n".
2502 "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
2503 "<description>$project log</description>\n".
2504 "<language>en</language>\n";
2505
2506 for (my $i = 0; $i <= $#revlist; $i++) {
2507 my $commit = $revlist[$i];
2508 my %co = git_read_commit($commit);
2509 # we read 150, we always show 30 and the ones more recent than 48 hours
2510 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
2511 last;
2512 }
2513 my %cd = date_str($co{'committer_epoch'});
2514 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
2515 my @difftree = map { chomp; $_ } <$fd>;
2516 close $fd or next;
2517 print "<item>\n" .
2518 "<title>" .
2519 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
2520 "</title>\n" .
2521 "<author>" . esc_html($co{'author'}) . "</author>\n" .
2522 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
2523 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
2524 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
2525 "<description>" . esc_html($co{'title'}) . "</description>\n" .
2526 "<content:encoded>" .
2527 "<![CDATA[\n";
2528 my $comment = $co{'comment'};
2529 foreach my $line (@$comment) {
2530 $line = decode("utf8", $line, Encode::FB_DEFAULT);
2531 print "$line<br/>\n";
2532 }
2533 print "<br/>\n";
2534 foreach my $line (@difftree) {
2535 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
2536 next;
2537 }
2538 my $file = validate_input(unquote($7));
2539 $file = decode("utf8", $file, Encode::FB_DEFAULT);
2540 print "$file<br/>\n";
2541 }
2542 print "]]>\n" .
2543 "</content:encoded>\n" .
2544 "</item>\n";
2545 }
2546 print "</channel></rss>";
2547}
2548
2549sub git_opml {
2550 my @list = git_read_projects();
2551
2552 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2553 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2554 "<opml version=\"1.0\">\n".
2555 "<head>".
2556 " <title>$site_name Git OPML Export</title>\n".
2557 "</head>\n".
2558 "<body>\n".
2559 "<outline text=\"git RSS feeds\">\n";
2560
2561 foreach my $pr (@list) {
2562 my %proj = %$pr;
2563 my $head = git_read_head($proj{'path'});
2564 if (!defined $head) {
2565 next;
2566 }
2567 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
2568 my %co = git_read_commit($head);
2569 if (!%co) {
2570 next;
2571 }
2572
2573 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
2574 my $rss = "$my_url?p=$proj{'path'};a=rss";
2575 my $html = "$my_url?p=$proj{'path'};a=summary";
2576 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
2577 }
2578 print "</outline>\n".
2579 "</body>\n".
2580 "</opml>\n";
2581}