3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
22 our $version = "++GIT_VERSION++";
23 our $my_url = $cgi->url();
24 our $my_uri = $cgi->url(-absolute => 1);
26 # core git executable to use
27 # this can just be "git" if your webserver has a sensible PATH
28 our $GIT = "++GIT_BINDIR++/git";
30 # absolute fs-path which will be prepended to the project path
31 #our $projectroot = "/pub/scm";
32 our $projectroot = "++GITWEB_PROJECTROOT++";
34 # location for temporary files needed for diffs
35 our $git_temp = "/tmp/gitweb";
37 # target of the home link on top of all pages
38 our $home_link = $my_uri || "/";
40 # string of the home link on top of all pages
41 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
43 # name of your site or organization to appear in page titles
44 # replace this with something more descriptive for clearer bookmarks
45 our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
47 # html text to include at home page
48 our $home_text = "++GITWEB_HOMETEXT++";
50 # URI of default stylesheet
51 our $stylesheet = "++GITWEB_CSS++";
53 our $logo = "++GITWEB_LOGO++";
55 # source of projects list
56 our $projects_list = "++GITWEB_LIST++";
58 # list of git base URLs used for URL to where fetch project from,
59 # i.e. full URL is "$git_base_url/$project"
60 our @git_base_url_list = ("++GITWEB_BASE_URL++");
62 # default blob_plain mimetype and default charset for text/plain blob
63 our $default_blob_plain_mimetype = 'text/plain';
64 our $default_text_plain_charset = undef;
66 # file to use for guessing MIME types before trying /etc/mime.types
67 # (relative to the current git repository)
68 our $mimetypes_file = undef;
70 # You define site-wide feature defaults here; override them with
71 # $GITWEB_CONFIG as necessary.
73 # feature => {'sub' => feature-sub, 'override' => allow-override, 'default' => [ default options...]
74 # if feature is overridable, feature-sub will be called with default options;
75 # return value indicates if to enable specified feature
78 'sub' => \&feature_blame,
83 'sub' => \&feature_snapshot,
85 # => [content-encoding, suffix, program]
86 'default' => ['x-gzip', 'gz', 'gzip']},
89 sub gitweb_check_feature {
91 return undef unless exists $feature{$name};
92 my ($sub, $override, @defaults) = (
93 $feature{$name}{'sub'},
94 $feature{$name}{'override'},
95 @{$feature{$name}{'default'}});
96 if (!$override) { return @defaults; }
97 return $sub->(@defaults);
100 # To enable system wide have in $GITWEB_CONFIG
101 # $feature{'blame'}{'default'} = [1];
102 # To have project specific config enable override in $GITWEB_CONFIG
103 # $feature{'blame'}{'override'} = 1;
104 # and in project config gitweb.blame = 0|1;
107 my ($val) = git_get_project_config('blame', '--bool');
109 if ($val eq 'true') {
111 } elsif ($val eq 'false') {
118 # To disable system wide have in $GITWEB_CONFIG
119 # $feature{'snapshot'}{'default'} = [undef];
120 # To have project specific config enable override in $GITWEB_CONFIG
121 # $feature{'blame'}{'override'} = 1;
122 # and in project config gitweb.snapshot = none|gzip|bzip2
124 sub feature_snapshot {
125 my ($ctype, $suffix, $command) = @_;
127 my ($val) = git_get_project_config('snapshot');
129 if ($val eq 'gzip') {
130 return ('x-gzip', 'gz', 'gzip');
131 } elsif ($val eq 'bzip2') {
132 return ('x-bzip2', 'bz2', 'bzip2');
133 } elsif ($val eq 'none') {
137 return ($ctype, $suffix, $command);
140 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
141 require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
143 # version of the core git binary
144 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
146 $projects_list ||= $projectroot;
147 if (! -d $git_temp) {
148 mkdir($git_temp, 0700) || die_error(undef, "Couldn't mkdir $git_temp");
151 # ======================================================================
152 # input validation and dispatch
153 our $action = $cgi->param('a');
154 if (defined $action) {
155 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
156 die_error(undef, "Invalid action parameter");
160 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
161 if (defined $project) {
164 $project = undef unless $project;
166 if (defined $project) {
167 if (!validate_input($project)) {
168 die_error(undef, "Invalid project parameter");
170 if (!(-d "$projectroot/$project")) {
171 die_error(undef, "No such directory");
173 if (!(-e "$projectroot/$project/HEAD")) {
174 die_error(undef, "No such project");
176 $ENV{'GIT_DIR'} = "$projectroot/$project";
179 our $file_name = $cgi->param('f');
180 if (defined $file_name) {
181 if (!validate_input($file_name)) {
182 die_error(undef, "Invalid file parameter");
186 our $file_parent = $cgi->param('fp');
187 if (defined $file_parent) {
188 if (!validate_input($file_parent)) {
189 die_error(undef, "Invalid file parent parameter");
193 our $hash = $cgi->param('h');
195 if (!validate_input($hash)) {
196 die_error(undef, "Invalid hash parameter");
200 our $hash_parent = $cgi->param('hp');
201 if (defined $hash_parent) {
202 if (!validate_input($hash_parent)) {
203 die_error(undef, "Invalid hash parent parameter");
207 our $hash_base = $cgi->param('hb');
208 if (defined $hash_base) {
209 if (!validate_input($hash_base)) {
210 die_error(undef, "Invalid hash base parameter");
214 our $page = $cgi->param('pg');
216 if ($page =~ m/[^0-9]$/) {
217 die_error(undef, "Invalid page parameter");
221 our $searchtext = $cgi->param('s');
222 if (defined $searchtext) {
223 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
224 die_error(undef, "Invalid search parameter");
226 $searchtext = quotemeta $searchtext;
231 "blame" => \&git_blame2,
232 "blobdiff" => \&git_blobdiff,
233 "blobdiff_plain" => \&git_blobdiff_plain,
234 "blob" => \&git_blob,
235 "blob_plain" => \&git_blob_plain,
236 "commitdiff" => \&git_commitdiff,
237 "commitdiff_plain" => \&git_commitdiff_plain,
238 "commit" => \&git_commit,
239 "heads" => \&git_heads,
240 "history" => \&git_history,
243 "search" => \&git_search,
244 "shortlog" => \&git_shortlog,
245 "summary" => \&git_summary,
247 "tags" => \&git_tags,
248 "tree" => \&git_tree,
249 "snapshot" => \&git_snapshot,
250 # those below don't need $project
251 "opml" => \&git_opml,
252 "project_list" => \&git_project_list,
255 if (defined $project) {
256 $action ||= 'summary';
258 $action ||= 'project_list';
260 if (!defined($actions{$action})) {
261 die_error(undef, "Unknown action");
263 $actions{$action}->();
266 ## ======================================================================
283 my %mapping = @mapping;
285 $params{"project"} ||= $project;
288 for (my $i = 0; $i < @mapping; $i += 2) {
289 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
290 if (defined $params{$name}) {
291 push @result, $symbol . "=" . esc_param($params{$name});
294 return "$my_uri?" . join(';', @result);
298 ## ======================================================================
299 ## validation, quoting/unquoting and escaping
304 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
307 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
310 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
316 # quote unsafe chars, but keep the slash, even when it's not
317 # correct, but quoted slashes look too horrible in bookmarks
320 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
326 # replace invalid utf8 character with SUBSTITUTION sequence
329 $str = decode("utf8", $str, Encode::FB_DEFAULT);
330 $str = escapeHTML($str);
331 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
335 # git may return quoted and escaped filenames
338 if ($str =~ m/^"(.*)"$/) {
340 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
345 # escape tabs (convert tabs to spaces)
349 while ((my $pos = index($line, "\t")) != -1) {
350 if (my $count = (8 - ($pos % 8))) {
351 my $spaces = ' ' x $count;
352 $line =~ s/\t/$spaces/;
359 ## ----------------------------------------------------------------------
360 ## HTML aware string manipulation
365 my $add_len = shift || 10;
367 # allow only $len chars, but don't cut a word if it would fit in $add_len
368 # if it doesn't fit, cut it if it's still longer than the dots we would add
369 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
372 if (length($tail) > 4) {
374 $body =~ s/&[^;]*$//; # remove chopped character entities
379 ## ----------------------------------------------------------------------
380 ## functions returning short strings
382 # CSS class for given age value (in seconds)
386 if ($age < 60*60*2) {
388 } elsif ($age < 60*60*24*2) {
395 # convert age in seconds to "nn units ago" string
400 if ($age > 60*60*24*365*2) {
401 $age_str = (int $age/60/60/24/365);
402 $age_str .= " years ago";
403 } elsif ($age > 60*60*24*(365/12)*2) {
404 $age_str = int $age/60/60/24/(365/12);
405 $age_str .= " months ago";
406 } elsif ($age > 60*60*24*7*2) {
407 $age_str = int $age/60/60/24/7;
408 $age_str .= " weeks ago";
409 } elsif ($age > 60*60*24*2) {
410 $age_str = int $age/60/60/24;
411 $age_str .= " days ago";
412 } elsif ($age > 60*60*2) {
413 $age_str = int $age/60/60;
414 $age_str .= " hours ago";
415 } elsif ($age > 60*2) {
416 $age_str = int $age/60;
417 $age_str .= " min ago";
420 $age_str .= " sec ago";
422 $age_str .= " right now";
427 # convert file mode in octal to symbolic file mode string
429 my $mode = oct shift;
431 if (S_ISDIR($mode & S_IFMT)) {
433 } elsif (S_ISLNK($mode)) {
435 } elsif (S_ISREG($mode)) {
436 # git cares only about the executable bit
437 if ($mode & S_IXUSR) {
447 # convert file mode in octal to file type string
449 my $mode = oct shift;
451 if (S_ISDIR($mode & S_IFMT)) {
453 } elsif (S_ISLNK($mode)) {
455 } elsif (S_ISREG($mode)) {
462 ## ----------------------------------------------------------------------
463 ## functions returning short HTML fragments, or transforming HTML fragments
464 ## which don't beling to other sections
466 # format line of commit message or tag comment
467 sub format_log_line_html {
470 $line = esc_html($line);
471 $line =~ s/ / /g;
472 if ($line =~ m/([0-9a-fA-F]{40})/) {
474 if (git_get_type($hash_text) eq "commit") {
476 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
477 -class => "text"}, $hash_text);
478 $line =~ s/$hash_text/$link/;
484 # format marker of refs pointing to given object
485 sub format_ref_marker {
486 my ($refs, $id) = @_;
489 if (defined $refs->{$id}) {
490 foreach my $ref (@{$refs->{$id}}) {
491 my ($type, $name) = qw();
492 # e.g. tags/v2.6.11 or heads/next
493 if ($ref =~ m!^(.*?)s?/(.*)$!) {
501 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
506 return ' <span class="refs">'. $markers . '</span>';
512 # format, perhaps shortened and with markers, title line
513 sub format_subject_html {
514 my ($long, $short, $href, $extra) = @_;
515 $extra = '' unless defined($extra);
517 if (length($short) < length($long)) {
518 return $cgi->a({-href => $href, -class => "list subject",
520 esc_html($short) . $extra);
522 return $cgi->a({-href => $href, -class => "list subject"},
523 esc_html($long) . $extra);
527 sub format_diff_line {
529 my $char = substr($line, 0, 1);
535 $diff_class = " add";
536 } elsif ($char eq "-") {
537 $diff_class = " rem";
538 } elsif ($char eq "@") {
539 $diff_class = " chunk_header";
540 } elsif ($char eq "\\") {
541 $diff_class = " incomplete";
543 $line = untabify($line);
544 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
547 ## ----------------------------------------------------------------------
548 ## git utility subroutines, invoking git commands
550 # get HEAD ref of given project as hash
551 sub git_get_head_hash {
553 my $oENV = $ENV{'GIT_DIR'};
555 $ENV{'GIT_DIR'} = "$projectroot/$project";
556 if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
559 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
564 $ENV{'GIT_DIR'} = $oENV;
569 # get type of given object
573 open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
580 sub git_get_project_config {
581 my ($key, $type) = @_;
583 return unless ($key);
584 $key =~ s/^gitweb\.//;
585 return if ($key =~ m/\W/);
587 my @x = ($GIT, 'repo-config');
588 if (defined $type) { push @x, $type; }
590 push @x, "gitweb.$key";
596 # get hash of given path at given ref
597 sub git_get_hash_by_path {
599 my $path = shift || return undef;
603 open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
604 or die_error(undef, "Open git-ls-tree failed");
606 close $fd or return undef;
608 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
609 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
613 ## ......................................................................
614 ## git utility functions, directly accessing git repository
616 # assumes that PATH is not symref
617 sub git_get_hash_by_ref {
620 open my $fd, "$projectroot/$path" or return undef;
624 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
629 sub git_get_project_description {
632 open my $fd, "$projectroot/$path/description" or return undef;
639 sub git_get_project_url_list {
642 open my $fd, "$projectroot/$path/cloneurl" or return undef;
643 my @git_project_url_list = map { chomp; $_ } <$fd>;
646 return wantarray ? @git_project_url_list : \@git_project_url_list;
649 sub git_get_projects_list {
652 if (-d $projects_list) {
653 # search in directory
654 my $dir = $projects_list;
655 opendir my ($dh), $dir or return undef;
656 while (my $dir = readdir($dh)) {
657 if (-e "$projectroot/$dir/HEAD") {
665 } elsif (-f $projects_list) {
666 # read from file(url-encoded):
667 # 'git%2Fgit.git Linus+Torvalds'
668 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
669 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
670 open my ($fd), $projects_list or return undef;
671 while (my $line = <$fd>) {
673 my ($path, $owner) = split ' ', $line;
674 $path = unescape($path);
675 $owner = unescape($owner);
676 if (!defined $path) {
679 if (-e "$projectroot/$path/HEAD") {
682 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
689 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
693 sub git_get_project_owner {
697 return undef unless $project;
699 # read from file (url-encoded):
700 # 'git%2Fgit.git Linus+Torvalds'
701 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
702 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
703 if (-f $projects_list) {
704 open (my $fd , $projects_list);
705 while (my $line = <$fd>) {
707 my ($pr, $ow) = split ' ', $line;
710 if ($pr eq $project) {
711 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
717 if (!defined $owner) {
718 $owner = get_file_owner("$projectroot/$project");
724 sub git_get_references {
725 my $type = shift || "";
728 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
729 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
730 if (-f "$projectroot/$project/info/refs") {
731 open $fd, "$projectroot/$project/info/refs"
734 open $fd, "-|", $GIT, "ls-remote", "."
738 while (my $line = <$fd>) {
740 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
741 if (defined $refs{$1}) {
742 push @{$refs{$1}}, $2;
752 sub git_get_following_references {
753 my $hash = shift || return undef;
755 my $base = shift || $hash_base || "HEAD";
757 my $refs = git_get_references($type);
758 open my $fd, "-|", $GIT, "rev-list", $base
760 my @commits = map { chomp; $_ } <$fd>;
767 foreach my $commit (@commits) {
768 foreach my $ref (@{$refs->{$commit}}) {
770 push @reflist, $lastref;
772 if ($commit eq $hash) {
777 return wantarray ? @reflist : $lastref;
780 sub git_get_preceding_references {
781 my $hash = shift || return undef;
784 my $refs = git_get_references($type);
785 open my $fd, "-|", $GIT, "rev-list", $hash
787 my @commits = map { chomp; $_ } <$fd>;
793 foreach my $commit (@commits) {
794 foreach my $ref (@{$refs->{$commit}}) {
795 return $ref unless wantarray;
803 sub git_get_rev_name_tags {
804 my $hash = shift || return undef;
806 open my $fd, "-|", $GIT, "name-rev", "--tags", $hash
808 my $name_rev = <$fd>;
811 if ($name_rev =~ m|^$hash tags/(.*)$|) {
814 # catches also '$hash undefined' output
819 ## ----------------------------------------------------------------------
820 ## parse to hash functions
824 my $tz = shift || "-0000";
827 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
828 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
829 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
830 $date{'hour'} = $hour;
831 $date{'minute'} = $min;
832 $date{'mday'} = $mday;
833 $date{'day'} = $days[$wday];
834 $date{'month'} = $months[$mon];
835 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
836 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
837 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
838 $mday, $months[$mon], $hour ,$min;
840 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
841 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
842 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
843 $date{'hour_local'} = $hour;
844 $date{'minute_local'} = $min;
845 $date{'tz_local'} = $tz;
854 open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
855 $tag{'id'} = $tag_id;
856 while (my $line = <$fd>) {
858 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
860 } elsif ($line =~ m/^type (.+)$/) {
862 } elsif ($line =~ m/^tag (.+)$/) {
864 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
868 } elsif ($line =~ m/--BEGIN/) {
869 push @comment, $line;
871 } elsif ($line eq "") {
875 push @comment, <$fd>;
876 $tag{'comment'} = \@comment;
878 if (!defined $tag{'name'}) {
885 my $commit_id = shift;
886 my $commit_text = shift;
891 if (defined $commit_text) {
892 @commit_lines = @$commit_text;
895 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id
897 @commit_lines = split '\n', <$fd>;
902 my $header = shift @commit_lines;
903 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
906 ($co{'id'}, my @parents) = split ' ', $header;
907 $co{'parents'} = \@parents;
908 $co{'parent'} = $parents[0];
909 while (my $line = shift @commit_lines) {
910 last if $line eq "\n";
911 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
913 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
915 $co{'author_epoch'} = $2;
916 $co{'author_tz'} = $3;
917 if ($co{'author'} =~ m/^([^<]+) </) {
918 $co{'author_name'} = $1;
920 $co{'author_name'} = $co{'author'};
922 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
923 $co{'committer'} = $1;
924 $co{'committer_epoch'} = $2;
925 $co{'committer_tz'} = $3;
926 $co{'committer_name'} = $co{'committer'};
927 $co{'committer_name'} =~ s/ <.*//;
930 if (!defined $co{'tree'}) {
934 foreach my $title (@commit_lines) {
937 $co{'title'} = chop_str($title, 80, 5);
938 # remove leading stuff of merges to make the interesting part visible
939 if (length($title) > 50) {
940 $title =~ s/^Automatic //;
941 $title =~ s/^merge (of|with) /Merge ... /i;
942 if (length($title) > 50) {
943 $title =~ s/(http|rsync):\/\///;
945 if (length($title) > 50) {
946 $title =~ s/(master|www|rsync)\.//;
948 if (length($title) > 50) {
949 $title =~ s/kernel.org:?//;
951 if (length($title) > 50) {
952 $title =~ s/\/pub\/scm//;
955 $co{'title_short'} = chop_str($title, 50, 5);
959 # remove added spaces
960 foreach my $line (@commit_lines) {
963 $co{'comment'} = \@commit_lines;
965 my $age = time - $co{'committer_epoch'};
967 $co{'age_string'} = age_string($age);
968 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
969 if ($age > 60*60*24*7*2) {
970 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
971 $co{'age_string_age'} = $co{'age_string'};
973 $co{'age_string_date'} = $co{'age_string'};
974 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
979 # parse ref from ref_file, given by ref_id, with given type
981 my $ref_file = shift;
983 my $type = shift || git_get_type($ref_id);
986 $ref_item{'type'} = $type;
987 $ref_item{'id'} = $ref_id;
988 $ref_item{'epoch'} = 0;
989 $ref_item{'age'} = "unknown";
990 if ($type eq "tag") {
991 my %tag = parse_tag($ref_id);
992 $ref_item{'comment'} = $tag{'comment'};
993 if ($tag{'type'} eq "commit") {
994 my %co = parse_commit($tag{'object'});
995 $ref_item{'epoch'} = $co{'committer_epoch'};
996 $ref_item{'age'} = $co{'age_string'};
997 } elsif (defined($tag{'epoch'})) {
998 my $age = time - $tag{'epoch'};
999 $ref_item{'epoch'} = $tag{'epoch'};
1000 $ref_item{'age'} = age_string($age);
1002 $ref_item{'reftype'} = $tag{'type'};
1003 $ref_item{'name'} = $tag{'name'};
1004 $ref_item{'refid'} = $tag{'object'};
1005 } elsif ($type eq "commit"){
1006 my %co = parse_commit($ref_id);
1007 $ref_item{'reftype'} = "commit";
1008 $ref_item{'name'} = $ref_file;
1009 $ref_item{'title'} = $co{'title'};
1010 $ref_item{'refid'} = $ref_id;
1011 $ref_item{'epoch'} = $co{'committer_epoch'};
1012 $ref_item{'age'} = $co{'age_string'};
1014 $ref_item{'reftype'} = $type;
1015 $ref_item{'name'} = $ref_file;
1016 $ref_item{'refid'} = $ref_id;
1022 # parse line of git-diff-tree "raw" output
1023 sub parse_difftree_raw_line {
1027 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1028 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1029 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1030 $res{'from_mode'} = $1;
1031 $res{'to_mode'} = $2;
1032 $res{'from_id'} = $3;
1034 $res{'status'} = $5;
1035 $res{'similarity'} = $6;
1036 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1037 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1039 $res{'file'} = unquote($7);
1042 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1043 #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1044 # $res{'commit'} = $1;
1047 return wantarray ? %res : \%res;
1050 ## ......................................................................
1051 ## parse to array of hashes functions
1053 sub git_get_refs_list {
1054 my $ref_dir = shift;
1058 my $pfxlen = length("$projectroot/$project/$ref_dir");
1059 File::Find::find(sub {
1062 push @refs, substr($File::Find::name, $pfxlen + 1);
1064 }, "$projectroot/$project/$ref_dir");
1066 foreach my $ref_file (@refs) {
1067 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
1068 my $type = git_get_type($ref_id) || next;
1069 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1071 push @reflist, \%ref_item;
1074 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1078 ## ----------------------------------------------------------------------
1079 ## filesystem-related functions
1081 sub get_file_owner {
1084 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1085 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1086 if (!defined $gcos) {
1090 $owner =~ s/[,;].*$//;
1091 return decode("utf8", $owner, Encode::FB_DEFAULT);
1094 ## ......................................................................
1095 ## mimetype related functions
1097 sub mimetype_guess_file {
1098 my $filename = shift;
1099 my $mimemap = shift;
1100 -r $mimemap or return undef;
1103 open(MIME, $mimemap) or return undef;
1105 next if m/^#/; # skip comments
1106 my ($mime, $exts) = split(/\t+/);
1107 if (defined $exts) {
1108 my @exts = split(/\s+/, $exts);
1109 foreach my $ext (@exts) {
1110 $mimemap{$ext} = $mime;
1116 $filename =~ /\.(.*?)$/;
1117 return $mimemap{$1};
1120 sub mimetype_guess {
1121 my $filename = shift;
1123 $filename =~ /\./ or return undef;
1125 if ($mimetypes_file) {
1126 my $file = $mimetypes_file;
1127 if ($file !~ m!^/!) { # if it is relative path
1128 # it is relative to project
1129 $file = "$projectroot/$project/$file";
1131 $mime = mimetype_guess_file($filename, $file);
1133 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1139 my $filename = shift;
1142 my $mime = mimetype_guess($filename);
1143 $mime and return $mime;
1147 return $default_blob_plain_mimetype unless $fd;
1150 return 'text/plain' .
1151 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1152 } elsif (! $filename) {
1153 return 'application/octet-stream';
1154 } elsif ($filename =~ m/\.png$/i) {
1156 } elsif ($filename =~ m/\.gif$/i) {
1158 } elsif ($filename =~ m/\.jpe?g$/i) {
1159 return 'image/jpeg';
1161 return 'application/octet-stream';
1165 ## ======================================================================
1166 ## functions printing HTML: header, footer, error page
1168 sub git_header_html {
1169 my $status = shift || "200 OK";
1170 my $expires = shift;
1172 my $title = "$site_name git";
1173 if (defined $project) {
1174 $title .= " - $project";
1175 if (defined $action) {
1176 $title .= "/$action";
1177 if (defined $file_name) {
1178 $title .= " - $file_name";
1179 if ($action eq "tree" && $file_name !~ m|/$|) {
1186 # require explicit support from the UA if we are to send the page as
1187 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1188 # we have to do this because MSIE sometimes globs '*/*', pretending to
1189 # support xhtml+xml but choking when it gets what it asked for.
1190 if (defined $cgi->http('HTTP_ACCEPT') &&
1191 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1192 $cgi->Accept('application/xhtml+xml') != 0) {
1193 $content_type = 'application/xhtml+xml';
1195 $content_type = 'text/html';
1197 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1198 -status=> $status, -expires => $expires);
1200 <?xml version="1.0" encoding="utf-8"?>
1201 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1202 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1203 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1204 <!-- git core binaries version $git_version -->
1206 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1207 <meta name="generator" content="gitweb/$version git/$git_version"/>
1208 <meta name="robots" content="index, nofollow"/>
1209 <title>$title</title>
1210 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1212 if (defined $project) {
1213 printf('<link rel="alternate" title="%s log" '.
1214 'href="%s" type="application/rss+xml"/>'."\n",
1215 esc_param($project), href(action=>"rss"));
1220 "<div class=\"page_header\">\n" .
1221 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1222 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1224 print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1225 if (defined $project) {
1226 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1227 if (defined $action) {
1231 if (!defined $searchtext) {
1235 if (defined $hash_base) {
1236 $search_hash = $hash_base;
1237 } elsif (defined $hash) {
1238 $search_hash = $hash;
1240 $search_hash = "HEAD";
1242 $cgi->param("a", "search");
1243 $cgi->param("h", $search_hash);
1244 print $cgi->startform(-method => "get", -action => $my_uri) .
1245 "<div class=\"search\">\n" .
1246 $cgi->hidden(-name => "p") . "\n" .
1247 $cgi->hidden(-name => "a") . "\n" .
1248 $cgi->hidden(-name => "h") . "\n" .
1249 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1251 $cgi->end_form() . "\n";
1256 sub git_footer_html {
1257 print "<div class=\"page_footer\">\n";
1258 if (defined $project) {
1259 my $descr = git_get_project_description($project);
1260 if (defined $descr) {
1261 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1263 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1265 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1273 my $status = shift || "403 Forbidden";
1274 my $error = shift || "Malformed query, file missing or permission denied";
1276 git_header_html($status);
1278 <div class="page_body">
1288 ## ----------------------------------------------------------------------
1289 ## functions printing or outputting HTML: navigation
1291 sub git_print_page_nav {
1292 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1293 $extra = '' if !defined $extra; # pager or formats
1295 my @navs = qw(summary shortlog log commit commitdiff tree);
1297 @navs = grep { $_ ne $suppress } @navs;
1300 my %arg = map { $_ => {action=>$_} } @navs;
1301 if (defined $head) {
1302 for (qw(commit commitdiff)) {
1303 $arg{$_}{hash} = $head;
1305 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1306 for (qw(shortlog log)) {
1307 $arg{$_}{hash} = $head;
1311 $arg{tree}{hash} = $treehead if defined $treehead;
1312 $arg{tree}{hash_base} = $treebase if defined $treebase;
1314 print "<div class=\"page_nav\">\n" .
1316 map { $_ eq $current ?
1317 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1319 print "<br/>\n$extra<br/>\n" .
1323 sub format_paging_nav {
1324 my ($action, $hash, $head, $page, $nrevs) = @_;
1328 if ($hash ne $head || $page) {
1329 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1331 $paging_nav .= "HEAD";
1335 $paging_nav .= " ⋅ " .
1336 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1337 -accesskey => "p", -title => "Alt-p"}, "prev");
1339 $paging_nav .= " ⋅ prev";
1342 if ($nrevs >= (100 * ($page+1)-1)) {
1343 $paging_nav .= " ⋅ " .
1344 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1345 -accesskey => "n", -title => "Alt-n"}, "next");
1347 $paging_nav .= " ⋅ next";
1353 ## ......................................................................
1354 ## functions printing or outputting HTML: div
1356 sub git_print_header_div {
1357 my ($action, $title, $hash, $hash_base) = @_;
1360 $args{action} = $action;
1361 $args{hash} = $hash if $hash;
1362 $args{hash_base} = $hash_base if $hash_base;
1364 print "<div class=\"header\">\n" .
1365 $cgi->a({-href => href(%args), -class => "title"},
1366 $title ? $title : $action) .
1370 sub git_print_page_path {
1375 if (!defined $name) {
1376 print "<div class=\"page_path\">/</div>\n";
1377 } elsif (defined $type && $type eq 'blob') {
1378 print "<div class=\"page_path\">";
1380 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1384 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1387 print "<br/></div>\n";
1389 print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1396 # remove leading empty lines
1397 while (defined $log->[0] && $log->[0] eq "") {
1404 foreach my $line (@$log) {
1405 # print only one empty line
1406 # do not print empty line after signoff
1408 next if ($empty || $signoff);
1413 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1415 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1418 print format_log_line_html($line) . "<br/>\n";
1423 sub git_print_simplified_log {
1425 my $remove_title = shift;
1427 shift @$log if $remove_title;
1428 # remove leading empty lines
1429 while (defined $log->[0] && $log->[0] eq "") {
1433 # simplify and print log
1435 foreach my $line (@$log) {
1436 # remove signoff lines
1437 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1440 # print only one empty line
1447 print format_log_line_html($line) . "<br/>\n";
1449 # end with single empty line
1450 print "<br/>\n" unless $empty;
1453 ## ......................................................................
1454 ## functions printing large fragments of HTML
1456 sub git_difftree_body {
1457 my ($difftree, $hash, $parent) = @_;
1459 print "<div class=\"list_head\">\n";
1460 if ($#{$difftree} > 10) {
1461 print(($#{$difftree} + 1) . " files changed:\n");
1465 print "<table class=\"diff_tree\">\n";
1467 foreach my $line (@{$difftree}) {
1468 my %diff = parse_difftree_raw_line($line);
1471 print "<tr class=\"dark\">\n";
1473 print "<tr class=\"light\">\n";
1477 my ($to_mode_oct, $to_mode_str, $to_file_type);
1478 my ($from_mode_oct, $from_mode_str, $from_file_type);
1479 if ($diff{'to_mode'} ne ('0' x 6)) {
1480 $to_mode_oct = oct $diff{'to_mode'};
1481 if (S_ISREG($to_mode_oct)) { # only for regular file
1482 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1484 $to_file_type = file_type($diff{'to_mode'});
1486 if ($diff{'from_mode'} ne ('0' x 6)) {
1487 $from_mode_oct = oct $diff{'from_mode'};
1488 if (S_ISREG($to_mode_oct)) { # only for regular file
1489 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1491 $from_file_type = file_type($diff{'from_mode'});
1494 if ($diff{'status'} eq "A") { # created
1495 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1496 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1497 $mode_chng .= "]</span>";
1499 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1500 hash_base=>$hash, file_name=>$diff{'file'}),
1501 -class => "list"}, esc_html($diff{'file'})) .
1503 "<td>$mode_chng</td>\n" .
1504 "<td class=\"link\">" .
1505 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1506 hash_base=>$hash, file_name=>$diff{'file'})},
1510 } elsif ($diff{'status'} eq "D") { # deleted
1511 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1513 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1514 hash_base=>$parent, file_name=>$diff{'file'}),
1515 -class => "list"}, esc_html($diff{'file'})) .
1517 "<td>$mode_chng</td>\n" .
1518 "<td class=\"link\">" .
1519 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1520 hash_base=>$parent, file_name=>$diff{'file'})},
1523 $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1524 file_name=>$diff{'file'})},\
1528 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1529 my $mode_chnge = "";
1530 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1531 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1532 if ($from_file_type != $to_file_type) {
1533 $mode_chnge .= " from $from_file_type to $to_file_type";
1535 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1536 if ($from_mode_str && $to_mode_str) {
1537 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1538 } elsif ($to_mode_str) {
1539 $mode_chnge .= " mode: $to_mode_str";
1542 $mode_chnge .= "]</span>\n";
1545 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1546 print $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1547 hash_base=>$hash, file_name=>$diff{'file'}),
1548 -class => "list"}, esc_html($diff{'file'}));
1549 } else { # only mode changed
1550 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1551 hash_base=>$hash, file_name=>$diff{'file'}),
1552 -class => "list"}, esc_html($diff{'file'}));
1555 "<td>$mode_chnge</td>\n" .
1556 "<td class=\"link\">" .
1557 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1558 hash_base=>$hash, file_name=>$diff{'file'})},
1560 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1562 $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1563 hash_base=>$hash, file_name=>$diff{'file'})},
1567 $cgi->a({-href => href(action=>"history",
1568 hash_base=>$hash, file_name=>$diff{'file'})},
1572 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1573 my %status_name = ('R' => 'moved', 'C' => 'copied');
1574 my $nstatus = $status_name{$diff{'status'}};
1576 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1577 # mode also for directories, so we cannot use $to_mode_str
1578 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1581 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1582 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1583 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1584 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1585 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1586 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1587 -class => "list"}, esc_html($diff{'from_file'})) .
1588 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1589 "<td class=\"link\">" .
1590 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1591 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1593 if ($diff{'to_id'} ne $diff{'from_id'}) {
1595 $cgi->a({-href => href(action=>"blobdiff", hash_base=>$hash,
1596 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1597 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1602 } # we should not encounter Unmerged (U) or Unknown (X) status
1608 sub git_patchset_body {
1609 my ($fd, $difftree, $hash, $hash_parent) = @_;
1613 my $patch_found = 0;
1616 print "<div class=\"patchset\">\n";
1619 while (my $patch_line @$fd>) {
1622 if ($patch_line =~ m/^diff /) { # "git diff" header
1623 # beginning of patch (in patchset)
1625 # close previous patch
1626 print "</div>\n"; # class="patch"
1628 # first patch in patchset
1631 print "<div class=\"patch\">\n";
1633 %diffinfo = parse_difftree_raw_line($difftree->[$patch_idx++]);
1635 # for now, no extended header, hence we skip empty patches
1636 # companion to next LINE if $in_header;
1637 if ($diffinfo{'from_id'} eq $diffinfo{'to_id'}) { # no change
1642 if ($diffinfo{'status'} eq "A") { # added
1643 print "<div class=\"diff_info\">" . file_type($diffinfo{'to_mode'}) . ":" .
1644 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1645 hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1646 $diffinfo{'to_id'}) . "(new)" .
1647 "</div>\n"; # class="diff_info"
1649 } elsif ($diffinfo{'status'} eq "D") { # deleted
1650 print "<div class=\"diff_info\">" . file_type($diffinfo{'from_mode'}) . ":" .
1651 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1652 hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1653 $diffinfo{'from_id'}) . "(deleted)" .
1654 "</div>\n"; # class="diff_info"
1656 } elsif ($diffinfo{'status'} eq "R" || # renamed
1657 $diffinfo{'status'} eq "C") { # copied
1658 print "<div class=\"diff_info\">" .
1659 file_type($diffinfo{'from_mode'}) . ":" .
1660 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1661 hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'from_file'})},
1662 $diffinfo{'from_id'}) .
1664 file_type($diffinfo{'to_mode'}) . ":" .
1665 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1666 hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'to_file'})},
1667 $diffinfo{'to_id'});
1668 print "</div>\n"; # class="diff_info"
1670 } else { # modified, mode changed, ...
1671 print "<div class=\"diff_info\">" .
1672 file_type($diffinfo{'from_mode'}) . ":" .
1673 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1674 hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1675 $diffinfo{'from_id'}) .
1677 file_type($diffinfo{'to_mode'}) . ":" .
1678 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1679 hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1680 $diffinfo{'to_id'});
1681 print "</div>\n"; # class="diff_info"
1684 #print "<div class=\"diff extended_header\">\n";
1687 } # start of patch in patchset
1690 if ($in_header && $patch_line =~ m/^---/) {
1694 next LINE if $in_header;
1696 print format_diff_line($patch_line);
1698 print "</div>\n" if $patch_found; # class="patch"
1700 print "</div>\n"; # class="patchset"
1703 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1705 sub git_shortlog_body {
1706 # uses global variable $project
1707 my ($revlist, $from, $to, $refs, $extra) = @_;
1709 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1710 my $have_snapshot = (defined $ctype && defined $suffix);
1712 $from = 0 unless defined $from;
1713 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1715 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1717 for (my $i = $from; $i <= $to; $i++) {
1718 my $commit = $revlist->[$i];
1719 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1720 my $ref = format_ref_marker($refs, $commit);
1721 my %co = parse_commit($commit);
1723 print "<tr class=\"dark\">\n";
1725 print "<tr class=\"light\">\n";
1728 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1729 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1730 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1732 print format_subject_html($co{'title'}, $co{'title_short'},
1733 href(action=>"commit", hash=>$commit), $ref);
1735 "<td class=\"link\">" .
1736 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1737 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1738 if ($have_snapshot) {
1739 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1744 if (defined $extra) {
1746 "<td colspan=\"4\">$extra</td>\n" .
1752 sub git_history_body {
1753 # Warning: assumes constant type (blob or tree) during history
1754 my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1756 print "<table class=\"history\" cellspacing=\"0\">\n";
1758 while (my $line = <$fd>) {
1759 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1764 my %co = parse_commit($commit);
1769 my $ref = format_ref_marker($refs, $commit);
1772 print "<tr class=\"dark\">\n";
1774 print "<tr class=\"light\">\n";
1777 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1778 # shortlog uses chop_str($co{'author_name'}, 10)
1779 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1781 # originally git_history used chop_str($co{'title'}, 50)
1782 print format_subject_html($co{'title'}, $co{'title_short'},
1783 href(action=>"commit", hash=>$commit), $ref);
1785 "<td class=\"link\">" .
1786 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1787 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1788 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1790 if ($ftype eq 'blob') {
1791 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1792 my $blob_parent = git_get_hash_by_path($commit, $file_name);
1793 if (defined $blob_current && defined $blob_parent &&
1794 $blob_current ne $blob_parent) {
1796 $cgi->a({-href => href(action=>"blobdiff", hash=>$blob_current, hash_parent=>$blob_parent,
1797 hash_base=>$commit, file_name=>$file_name)},
1804 if (defined $extra) {
1806 "<td colspan=\"4\">$extra</td>\n" .
1813 # uses global variable $project
1814 my ($taglist, $from, $to, $extra) = @_;
1815 $from = 0 unless defined $from;
1816 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1818 print "<table class=\"tags\" cellspacing=\"0\">\n";
1820 for (my $i = $from; $i <= $to; $i++) {
1821 my $entry = $taglist->[$i];
1823 my $comment_lines = $tag{'comment'};
1824 my $comment = shift @$comment_lines;
1826 if (defined $comment) {
1827 $comment_short = chop_str($comment, 30, 5);
1830 print "<tr class=\"dark\">\n";
1832 print "<tr class=\"light\">\n";
1835 print "<td><i>$tag{'age'}</i></td>\n" .
1837 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1838 -class => "list name"}, esc_html($tag{'name'})) .
1841 if (defined $comment) {
1842 print format_subject_html($comment, $comment_short,
1843 href(action=>"tag", hash=>$tag{'id'}));
1846 "<td class=\"selflink\">";
1847 if ($tag{'type'} eq "tag") {
1848 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1853 "<td class=\"link\">" . " | " .
1854 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1855 if ($tag{'reftype'} eq "commit") {
1856 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1857 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1858 } elsif ($tag{'reftype'} eq "blob") {
1859 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1864 if (defined $extra) {
1866 "<td colspan=\"5\">$extra</td>\n" .
1872 sub git_heads_body {
1873 # uses global variable $project
1874 my ($taglist, $head, $from, $to, $extra) = @_;
1875 $from = 0 unless defined $from;
1876 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1878 print "<table class=\"heads\" cellspacing=\"0\">\n";
1880 for (my $i = $from; $i <= $to; $i++) {
1881 my $entry = $taglist->[$i];
1883 my $curr = $tag{'id'} eq $head;
1885 print "<tr class=\"dark\">\n";
1887 print "<tr class=\"light\">\n";
1890 print "<td><i>$tag{'age'}</i></td>\n" .
1891 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1892 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1893 -class => "list name"},esc_html($tag{'name'})) .
1895 "<td class=\"link\">" .
1896 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1897 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1901 if (defined $extra) {
1903 "<td colspan=\"3\">$extra</td>\n" .
1909 ## ----------------------------------------------------------------------
1910 ## functions printing large fragments, format as one of arguments
1912 sub git_diff_print {
1914 my $from_name = shift;
1916 my $to_name = shift;
1917 my $format = shift || "html";
1919 my $from_tmp = "/dev/null";
1920 my $to_tmp = "/dev/null";
1923 # create tmp from-file
1924 if (defined $from) {
1925 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1926 open my $fd2, "> $from_tmp";
1927 open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1934 # create tmp to-file
1936 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1937 open my $fd2, "> $to_tmp";
1938 open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1945 open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1946 if ($format eq "plain") {
1951 while (my $line = <$fd>) {
1953 my $char = substr($line, 0, 1);
1954 my $diff_class = "";
1956 $diff_class = " add";
1957 } elsif ($char eq "-") {
1958 $diff_class = " rem";
1959 } elsif ($char eq "@") {
1960 $diff_class = " chunk_header";
1961 } elsif ($char eq "\\") {
1965 $line = untabify($line);
1966 print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1971 if (defined $from) {
1980 ## ======================================================================
1981 ## ======================================================================
1984 sub git_project_list {
1985 my $order = $cgi->param('o');
1986 if (defined $order && $order !~ m/project|descr|owner|age/) {
1987 die_error(undef, "Unknown order parameter");
1990 my @list = git_get_projects_list();
1993 die_error(undef, "No projects found");
1995 foreach my $pr (@list) {
1996 my $head = git_get_head_hash($pr->{'path'});
1997 if (!defined $head) {
2000 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
2001 my %co = parse_commit($head);
2005 $pr->{'commit'} = \%co;
2006 if (!defined $pr->{'descr'}) {
2007 my $descr = git_get_project_description($pr->{'path'}) || "";
2008 $pr->{'descr'} = chop_str($descr, 25, 5);
2010 if (!defined $pr->{'owner'}) {
2011 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2013 push @projects, $pr;
2017 if (-f $home_text) {
2018 print "<div class=\"index_include\">\n";
2019 open (my $fd, $home_text);
2024 print "<table class=\"project_list\">\n" .
2026 $order ||= "project";
2027 if ($order eq "project") {
2028 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2029 print "<th>Project</th>\n";
2032 $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
2033 -class => "header"}, "Project") .
2036 if ($order eq "descr") {
2037 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2038 print "<th>Description</th>\n";
2041 $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
2042 -class => "header"}, "Description") .
2045 if ($order eq "owner") {
2046 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2047 print "<th>Owner</th>\n";
2050 $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
2051 -class => "header"}, "Owner") .
2054 if ($order eq "age") {
2055 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2056 print "<th>Last Change</th>\n";
2059 $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
2060 -class => "header"}, "Last Change") .
2063 print "<th></th>\n" .
2066 foreach my $pr (@projects) {
2068 print "<tr class=\"dark\">\n";
2070 print "<tr class=\"light\">\n";
2073 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2074 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2075 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2076 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2077 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2078 $pr->{'commit'}{'age_string'} . "</td>\n" .
2079 "<td class=\"link\">" .
2080 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2081 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2082 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2091 my $descr = git_get_project_description($project) || "none";
2092 my $head = git_get_head_hash($project);
2093 my %co = parse_commit($head);
2094 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2096 my $owner = git_get_project_owner($project);
2098 my $refs = git_get_references();
2100 git_print_page_nav('summary','', $head);
2102 print "<div class=\"title\"> </div>\n";
2103 print "<table cellspacing=\"0\">\n" .
2104 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2105 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2106 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2107 # use per project git URL list in $projectroot/$project/cloneurl
2108 # or make project git URL from git base URL and project name
2109 my $url_tag = "URL";
2110 my @url_list = git_get_project_url_list($project);
2111 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2112 foreach my $git_url (@url_list) {
2113 next unless $git_url;
2114 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2119 open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
2120 or die_error(undef, "Open git-rev-list failed");
2121 my @revlist = map { chomp; $_ } <$fd>;
2123 git_print_header_div('shortlog');
2124 git_shortlog_body(\@revlist, 0, 15, $refs,
2125 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2127 my $taglist = git_get_refs_list("refs/tags");
2128 if (defined @$taglist) {
2129 git_print_header_div('tags');
2130 git_tags_body($taglist, 0, 15,
2131 $cgi->a({-href => href(action=>"tags")}, "..."));
2134 my $headlist = git_get_refs_list("refs/heads");
2135 if (defined @$headlist) {
2136 git_print_header_div('heads');
2137 git_heads_body($headlist, $head, 0, 15,
2138 $cgi->a({-href => href(action=>"heads")}, "..."));
2145 my $head = git_get_head_hash($project);
2147 git_print_page_nav('','', $head,undef,$head);
2148 my %tag = parse_tag($hash);
2149 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2150 print "<div class=\"title_text\">\n" .
2151 "<table cellspacing=\"0\">\n" .
2153 "<td>object</td>\n" .
2154 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2155 $tag{'object'}) . "</td>\n" .
2156 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2157 $tag{'type'}) . "</td>\n" .
2159 if (defined($tag{'author'})) {
2160 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2161 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2162 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2163 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2166 print "</table>\n\n" .
2168 print "<div class=\"page_body\">";
2169 my $comment = $tag{'comment'};
2170 foreach my $line (@$comment) {
2171 print esc_html($line) . "<br/>\n";
2181 if (!gitweb_check_feature('blame')) {
2182 die_error('403 Permission denied', "Permission denied");
2184 die_error('404 Not Found', "File name not defined") if (!$file_name);
2185 $hash_base ||= git_get_head_hash($project);
2186 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2187 my %co = parse_commit($hash_base)
2188 or die_error(undef, "Reading commit failed");
2189 if (!defined $hash) {
2190 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2191 or die_error(undef, "Error looking up file");
2193 $ftype = git_get_type($hash);
2194 if ($ftype !~ "blob") {
2195 die_error("400 Bad Request", "Object is not a blob");
2197 open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2198 or die_error(undef, "Open git-blame failed");
2201 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2204 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2206 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2207 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2208 git_print_page_path($file_name, $ftype, $hash_base);
2209 my @rev_color = (qw(light2 dark2));
2210 my $num_colors = scalar(@rev_color);
2211 my $current_color = 0;
2214 <div class="page_body">
2215 <table class="blame">
2216 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2219 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2221 my $rev = substr($full_rev, 0, 8);
2225 if (!defined $last_rev) {
2226 $last_rev = $full_rev;
2227 } elsif ($last_rev ne $full_rev) {
2228 $last_rev = $full_rev;
2229 $current_color = ++$current_color % $num_colors;
2231 print "<tr class=\"$rev_color[$current_color]\">\n";
2232 print "<td class=\"sha1\">" .
2233 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2234 esc_html($rev)) . "</td>\n";
2235 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2236 esc_html($lineno) . "</a></td>\n";
2237 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2243 or print "Reading blob failed\n";
2250 if (!gitweb_check_feature('blame')) {
2251 die_error('403 Permission denied', "Permission denied");
2253 die_error('404 Not Found', "File name not defined") if (!$file_name);
2254 $hash_base ||= git_get_head_hash($project);
2255 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2256 my %co = parse_commit($hash_base)
2257 or die_error(undef, "Reading commit failed");
2258 if (!defined $hash) {
2259 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2260 or die_error(undef, "Error lookup file");
2262 open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2263 or die_error(undef, "Open git-annotate failed");
2266 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2269 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2271 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2272 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2273 git_print_page_path($file_name, 'blob', $hash_base);
2274 print "<div class=\"page_body\">\n";
2276 <table class="blame">
2285 my @line_class = (qw(light dark));
2286 my $line_class_len = scalar (@line_class);
2287 my $line_class_num = $#line_class;
2288 while (my $line = <$fd>) {
2300 $line_class_num = ($line_class_num + 1) % $line_class_len;
2302 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
2309 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2312 $short_rev = substr ($long_rev, 0, 8);
2313 $age = time () - $time;
2314 $age_str = age_string ($age);
2315 $age_str =~ s/ / /g;
2316 $age_class = age_class($age);
2317 $author = esc_html ($author);
2318 $author =~ s/ / /g;
2320 $data = untabify($data);
2321 $data = esc_html ($data);
2324 <tr class="$line_class[$line_class_num]">
2325 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2326 <td class="$age_class">$age_str</td>
2328 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2329 <td class="pre">$data</td>
2332 } # while (my $line = <$fd>)
2333 print "</table>\n\n";
2335 or print "Reading blob failed.\n";
2341 my $head = git_get_head_hash($project);
2343 git_print_page_nav('','', $head,undef,$head);
2344 git_print_header_div('summary', $project);
2346 my $taglist = git_get_refs_list("refs/tags");
2347 if (defined @$taglist) {
2348 git_tags_body($taglist);
2354 my $head = git_get_head_hash($project);
2356 git_print_page_nav('','', $head,undef,$head);
2357 git_print_header_div('summary', $project);
2359 my $taglist = git_get_refs_list("refs/heads");
2360 if (defined @$taglist) {
2361 git_heads_body($taglist, $head);
2366 sub git_blob_plain {
2367 if (!defined $hash) {
2368 if (defined $file_name) {
2369 my $base = $hash_base || git_get_head_hash($project);
2370 $hash = git_get_hash_by_path($base, $file_name, "blob")
2371 or die_error(undef, "Error lookup file");
2373 die_error(undef, "No file name defined");
2377 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2378 or die_error(undef, "Couldn't cat $file_name, $hash");
2380 $type ||= blob_mimetype($fd, $file_name);
2382 # save as filename, even when no $file_name is given
2383 my $save_as = "$hash";
2384 if (defined $file_name) {
2385 $save_as = $file_name;
2386 } elsif ($type =~ m/^text\//) {
2390 print $cgi->header(-type => "$type",
2391 -content_disposition => "inline; filename=\"$save_as\"");
2393 binmode STDOUT, ':raw';
2395 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2401 if (!defined $hash) {
2402 if (defined $file_name) {
2403 my $base = $hash_base || git_get_head_hash($project);
2404 $hash = git_get_hash_by_path($base, $file_name, "blob")
2405 or die_error(undef, "Error lookup file");
2407 die_error(undef, "No file name defined");
2410 my $have_blame = gitweb_check_feature('blame');
2411 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2412 or die_error(undef, "Couldn't cat $file_name, $hash");
2413 my $mimetype = blob_mimetype($fd, $file_name);
2414 if ($mimetype !~ m/^text\//) {
2416 return git_blob_plain($mimetype);
2419 my $formats_nav = '';
2420 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2421 if (defined $file_name) {
2424 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2425 hash=>$hash, file_name=>$file_name)},
2430 $cgi->a({-href => href(action=>"blob_plain",
2431 hash=>$hash, file_name=>$file_name)},
2434 $cgi->a({-href => href(action=>"blob",
2435 hash_base=>"HEAD", file_name=>$file_name)},
2439 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2441 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2442 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2444 print "<div class=\"page_nav\">\n" .
2445 "<br/><br/></div>\n" .
2446 "<div class=\"title\">$hash</div>\n";
2448 git_print_page_path($file_name, "blob", $hash_base);
2449 print "<div class=\"page_body\">\n";
2451 while (my $line = <$fd>) {
2454 $line = untabify($line);
2455 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2456 $nr, $nr, $nr, esc_html($line);
2459 or print "Reading blob failed.\n";
2465 if (!defined $hash) {
2466 $hash = git_get_head_hash($project);
2467 if (defined $file_name) {
2468 my $base = $hash_base || $hash;
2469 $hash = git_get_hash_by_path($base, $file_name, "tree");
2471 if (!defined $hash_base) {
2476 open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2477 or die_error(undef, "Open git-ls-tree failed");
2478 my @entries = map { chomp; $_ } <$fd>;
2479 close $fd or die_error(undef, "Reading tree failed");
2482 my $refs = git_get_references();
2483 my $ref = format_ref_marker($refs, $hash_base);
2487 my $have_blame = gitweb_check_feature('blame');
2488 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2489 $base_key{hash_base} = $hash_base;
2490 git_print_page_nav('tree','', $hash_base);
2491 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2493 print "<div class=\"page_nav\">\n";
2494 print "<br/><br/></div>\n";
2495 print "<div class=\"title\">$hash</div>\n";
2497 if (defined $file_name) {
2498 $base = esc_html("$file_name/");
2500 git_print_page_path($file_name, 'tree', $hash_base);
2501 print "<div class=\"page_body\">\n";
2502 print "<table cellspacing=\"0\">\n";
2504 foreach my $line (@entries) {
2505 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2506 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2510 my $t_name = validate_input($4);
2512 print "<tr class=\"dark\">\n";
2514 print "<tr class=\"light\">\n";
2517 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2518 if ($t_type eq "blob") {
2519 print "<td class=\"list\">" .
2520 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2521 -class => "list"}, esc_html($t_name)) .
2523 "<td class=\"link\">" .
2524 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2528 $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2532 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2533 hash=>$t_hash, file_name=>"$base$t_name")},
2536 $cgi->a({-href => href(action=>"blob_plain",
2537 hash=>$t_hash, file_name=>"$base$t_name")},
2540 } elsif ($t_type eq "tree") {
2541 print "<td class=\"list\">" .
2542 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2543 esc_html($t_name)) .
2545 "<td class=\"link\">" .
2546 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2549 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2555 print "</table>\n" .
2562 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2563 my $have_snapshot = (defined $ctype && defined $suffix);
2564 if (!$have_snapshot) {
2565 die_error('403 Permission denied', "Permission denied");
2568 if (!defined $hash) {
2569 $hash = git_get_head_hash($project);
2572 my $filename = basename($project) . "-$hash.tar.$suffix";
2574 print $cgi->header(-type => 'application/x-tar',
2575 -content_encoding => $ctype,
2576 -content_disposition => "inline; filename=\"$filename\"",
2577 -status => '200 OK');
2579 open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2580 die_error(undef, "Execute git-tar-tree failed.");
2581 binmode STDOUT, ':raw';
2583 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2589 my $head = git_get_head_hash($project);
2590 if (!defined $hash) {
2593 if (!defined $page) {
2596 my $refs = git_get_references();
2598 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2599 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2600 or die_error(undef, "Open git-rev-list failed");
2601 my @revlist = map { chomp; $_ } <$fd>;
2604 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2607 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2610 my %co = parse_commit($hash);
2612 git_print_header_div('summary', $project);
2613 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2615 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2616 my $commit = $revlist[$i];
2617 my $ref = format_ref_marker($refs, $commit);
2618 my %co = parse_commit($commit);
2620 my %ad = parse_date($co{'author_epoch'});
2621 git_print_header_div('commit',
2622 "<span class=\"age\">$co{'age_string'}</span>" .
2623 esc_html($co{'title'}) . $ref,
2625 print "<div class=\"title_text\">\n" .
2626 "<div class=\"log_link\">\n" .
2627 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2629 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2632 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2635 print "<div class=\"log_body\">\n";
2636 git_print_simplified_log($co{'comment'});
2643 my %co = parse_commit($hash);
2645 die_error(undef, "Unknown commit object");
2647 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2648 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2650 my $parent = $co{'parent'};
2651 if (!defined $parent) {
2654 open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2655 or die_error(undef, "Open git-diff-tree failed");
2656 my @difftree = map { chomp; $_ } <$fd>;
2657 close $fd or die_error(undef, "Reading git-diff-tree failed");
2659 # non-textual hash id's can be cached
2661 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2664 my $refs = git_get_references();
2665 my $ref = format_ref_marker($refs, $co{'id'});
2667 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2668 my $have_snapshot = (defined $ctype && defined $suffix);
2670 my $formats_nav = '';
2671 if (defined $file_name && defined $co{'parent'}) {
2672 my $parent = $co{'parent'};
2674 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2677 git_header_html(undef, $expires);
2678 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2679 $hash, $co{'tree'}, $hash,
2682 if (defined $co{'parent'}) {
2683 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2685 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2687 print "<div class=\"title_text\">\n" .
2688 "<table cellspacing=\"0\">\n";
2689 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2691 "<td></td><td> $ad{'rfc2822'}";
2692 if ($ad{'hour_local'} < 6) {
2693 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2694 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2696 printf(" (%02d:%02d %s)",
2697 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2701 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2702 print "<tr><td></td><td> $cd{'rfc2822'}" .
2703 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2705 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2708 "<td class=\"sha1\">" .
2709 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2710 class => "list"}, $co{'tree'}) .
2712 "<td class=\"link\">" .
2713 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2715 if ($have_snapshot) {
2717 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2721 my $parents = $co{'parents'};
2722 foreach my $par (@$parents) {
2725 "<td class=\"sha1\">" .
2726 $cgi->a({-href => href(action=>"commit", hash=>$par),
2727 class => "list"}, $par) .
2729 "<td class=\"link\">" .
2730 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2732 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2739 print "<div class=\"page_body\">\n";
2740 git_print_log($co{'comment'});
2743 git_difftree_body(\@difftree, $hash, $parent);
2749 mkdir($git_temp, 0700);
2751 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2753 $cgi->a({-href => href(action=>"blobdiff_plain",
2754 hash=>$hash, hash_parent=>$hash_parent)},
2756 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2757 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2760 <div class="page_nav"><br/><br/></div>
2761 <div class="title">$hash vs $hash_parent</div>
2764 git_print_page_path($file_name, "blob", $hash_base);
2765 print "<div class=\"page_body\">\n" .
2766 "<div class=\"diff_info\">blob:" .
2767 $cgi->a({-href => href(action=>"blob", hash=>$hash_parent,
2768 hash_base=>$hash_base, file_name=>($file_parent || $file_name))},
2771 $cgi->a({-href => href(action=>"blob", hash=>$hash,
2772 hash_base=>$hash_base, file_name=>$file_name)},
2775 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2776 print "</div>"; # page_body
2780 sub git_blobdiff_plain {
2781 mkdir($git_temp, 0700);
2782 print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2783 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2786 sub git_commitdiff {
2787 my $format = shift || 'html';
2788 my %co = parse_commit($hash);
2790 die_error(undef, "Unknown commit object");
2792 if (!defined $hash_parent) {
2793 $hash_parent = $co{'parent'} || '--root';
2799 if ($format eq 'html') {
2800 open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C',
2801 "--patch-with-raw", "--full-index", $hash_parent, $hash
2802 or die_error(undef, "Open git-diff-tree failed");
2804 while (chomp(my $line = <$fd>)) {
2805 # empty line ends raw part of diff-tree output
2807 push @difftree, $line;
2810 } elsif ($format eq 'plain') {
2811 open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-B', $hash_parent, $hash
2812 or die_error(undef, "Open git-diff-tree failed");
2815 die_error(undef, "Unknown commitdiff format");
2818 # non-textual hash id's can be cached
2820 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2824 # write commit message
2825 if ($format eq 'html') {
2826 my $refs = git_get_references();
2827 my $ref = format_ref_marker($refs, $co{'id'});
2829 $cgi->a({-href => href(action=>"commitdiff_plain",
2830 hash=>$hash, hash_parent=>$hash_parent)},
2833 git_header_html(undef, $expires);
2834 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2835 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2836 print "<div class=\"page_body\">\n";
2837 print "<div class=\"log\">\n";
2838 git_print_simplified_log($co{'comment'}, 1); # skip title
2839 print "</div>\n"; # class="log"
2841 } elsif ($format eq 'plain') {
2842 my $refs = git_get_references("tags");
2844 if (exists $refs->{$hash}) {
2845 @tagnames = map { s|^tags/|| } $refs->{$hash};
2847 my $filename = basename($project) . "-$hash.patch";
2850 -type => 'text/plain',
2851 -charset => 'utf-8',
2852 -expires => $expires,
2853 -content_disposition => qq(inline; filename="$filename"));
2854 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2857 Date: $ad{'rfc2822'} ($ad{'tz_local'})
2858 Subject: $co{'title'}
2860 foreach my $tag (@tagnames) {
2861 print "X-Git-Tag: $tag\n";
2863 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2864 foreach my $line (@{$co{'comment'}}) {
2871 if ($format eq 'html') {
2872 #git_difftree_body(\@difftree, $hash, $hash_parent);
2875 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
2877 print "</div>\n"; # class="page_body"
2880 } elsif ($format eq 'plain') {
2884 or print "Reading git-diff-tree failed\n";
2888 sub git_commitdiff_plain {
2889 git_commitdiff('plain');
2893 if (!defined $hash_base) {
2894 $hash_base = git_get_head_hash($project);
2897 my %co = parse_commit($hash_base);
2899 die_error(undef, "Unknown commit object");
2901 my $refs = git_get_references();
2903 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2904 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2905 if (!defined $hash && defined $file_name) {
2906 $hash = git_get_hash_by_path($hash_base, $file_name);
2908 if (defined $hash) {
2909 $ftype = git_get_type($hash);
2911 git_print_page_path($file_name, $ftype, $hash_base);
2914 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2915 git_history_body($fd, $refs, $hash_base, $ftype);
2922 if (!defined $searchtext) {
2923 die_error(undef, "Text field empty");
2925 if (!defined $hash) {
2926 $hash = git_get_head_hash($project);
2928 my %co = parse_commit($hash);
2930 die_error(undef, "Unknown commit object");
2932 # pickaxe may take all resources of your box and run for several minutes
2933 # with every query - so decide by yourself how public you make this feature :)
2934 my $commit_search = 1;
2935 my $author_search = 0;
2936 my $committer_search = 0;
2937 my $pickaxe_search = 0;
2938 if ($searchtext =~ s/^author\\://i) {
2940 } elsif ($searchtext =~ s/^committer\\://i) {
2941 $committer_search = 1;
2942 } elsif ($searchtext =~ s/^pickaxe\\://i) {
2944 $pickaxe_search = 1;
2947 git_print_page_nav('','', $hash,$co{'tree'},$hash);
2948 git_print_header_div('commit', esc_html($co{'title'}), $hash);
2950 print "<table cellspacing=\"0\">\n";
2952 if ($commit_search) {
2954 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2955 while (my $commit_text = <$fd>) {
2956 if (!grep m/$searchtext/i, $commit_text) {
2959 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2962 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2965 my @commit_lines = split "\n", $commit_text;
2966 my %co = parse_commit(undef, \@commit_lines);
2971 print "<tr class=\"dark\">\n";
2973 print "<tr class=\"light\">\n";
2976 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2977 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2979 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2980 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2981 my $comment = $co{'comment'};
2982 foreach my $line (@$comment) {
2983 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2984 my $lead = esc_html($1) || "";
2985 $lead = chop_str($lead, 30, 10);
2986 my $match = esc_html($2) || "";
2987 my $trail = esc_html($3) || "";
2988 $trail = chop_str($trail, 30, 10);
2989 my $text = "$lead<span class=\"match\">$match</span>$trail";
2990 print chop_str($text, 80, 5) . "<br/>\n";
2994 "<td class=\"link\">" .
2995 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2997 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3004 if ($pickaxe_search) {
3006 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
3009 while (my $line = <$fd>) {
3010 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3013 $set{'from_id'} = $3;
3015 $set{'id'} = $set{'to_id'};
3016 if ($set{'id'} =~ m/0{40}/) {
3017 $set{'id'} = $set{'from_id'};
3019 if ($set{'id'} =~ m/0{40}/) {
3023 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3026 print "<tr class=\"dark\">\n";
3028 print "<tr class=\"light\">\n";
3031 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3032 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3034 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3035 -class => "list subject"},
3036 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3037 while (my $setref = shift @files) {
3039 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3040 hash=>$set{'id'}, file_name=>$set{'file'}),
3042 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3046 "<td class=\"link\">" .
3047 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3049 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3053 %co = parse_commit($1);
3063 my $head = git_get_head_hash($project);
3064 if (!defined $hash) {
3067 if (!defined $page) {
3070 my $refs = git_get_references();
3072 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3073 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
3074 or die_error(undef, "Open git-rev-list failed");
3075 my @revlist = map { chomp; $_ } <$fd>;
3078 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3080 if ($#revlist >= (100 * ($page+1)-1)) {
3082 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3083 -title => "Alt-n"}, "next");
3088 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3089 git_print_header_div('summary', $project);
3091 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3096 ## ......................................................................
3097 ## feeds (RSS, OPML)
3100 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3101 open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
3102 or die_error(undef, "Open git-rev-list failed");
3103 my @revlist = map { chomp; $_ } <$fd>;
3104 close $fd or die_error(undef, "Reading git-rev-list failed");
3105 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3107 <?xml version="1.0" encoding="utf-8"?>
3108 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3110 <title>$project $my_uri $my_url</title>
3111 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3112 <description>$project log</description>
3113 <language>en</language>
3116 for (my $i = 0; $i <= $#revlist; $i++) {
3117 my $commit = $revlist[$i];
3118 my %co = parse_commit($commit);
3119 # we read 150, we always show 30 and the ones more recent than 48 hours
3120 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3123 my %cd = parse_date($co{'committer_epoch'});
3124 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
3125 my @difftree = map { chomp; $_ } <$fd>;
3129 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3131 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3132 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3133 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3134 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3135 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3136 "<content:encoded>" .
3138 my $comment = $co{'comment'};
3139 foreach my $line (@$comment) {
3140 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3141 print "$line<br/>\n";
3144 foreach my $line (@difftree) {
3145 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3148 my $file = validate_input(unquote($7));
3149 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3150 print "$file<br/>\n";
3153 "</content:encoded>\n" .
3156 print "</channel></rss>";
3160 my @list = git_get_projects_list();
3162 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3164 <?xml version="1.0" encoding="utf-8"?>
3165 <opml version="1.0">
3167 <title>$site_name Git OPML Export</title>
3170 <outline text="git RSS feeds">
3173 foreach my $pr (@list) {
3175 my $head = git_get_head_hash($proj{'path'});
3176 if (!defined $head) {
3179 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3180 my %co = parse_commit($head);
3185 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3186 my $rss = "$my_url?p=$proj{'path'};a=rss";
3187 my $html = "$my_url?p=$proj{'path'};a=summary";
3188 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";