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