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