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 # target of the home link on top of all pages
35 our $home_link = $my_uri || "/";
37 # string of the home link on top of all pages
38 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
40 # name of your site or organization to appear in page titles
41 # replace this with something more descriptive for clearer bookmarks
42 our $site_name = "++GITWEB_SITENAME++"
43 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
45 # filename of html text to include at top of each page
46 our $site_header = "++GITWEB_SITE_HEADER++";
47 # html text to include at home page
48 our $home_text = "++GITWEB_HOMETEXT++";
49 # filename of html text to include at bottom of each page
50 our $site_footer = "++GITWEB_SITE_FOOTER++";
53 our @stylesheets = ("++GITWEB_CSS++");
54 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
55 our $stylesheet = undef;
56 # URI of GIT logo (72x27 size)
57 our $logo = "++GITWEB_LOGO++";
58 # URI of GIT favicon, assumed to be image/png type
59 our $favicon = "++GITWEB_FAVICON++";
61 # URI and label (title) of GIT logo link
62 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
63 #our $logo_label = "git documentation";
64 our $logo_url = "http://git.or.cz/";
65 our $logo_label = "git homepage";
67 # source of projects list
68 our $projects_list = "++GITWEB_LIST++";
70 # show repository only if this file exists
71 # (only effective if this variable evaluates to true)
72 our $export_ok = "++GITWEB_EXPORT_OK++";
74 # only allow viewing of repositories also shown on the overview page
75 our $strict_export = "++GITWEB_STRICT_EXPORT++";
77 # list of git base URLs used for URL to where fetch project from,
78 # i.e. full URL is "$git_base_url/$project"
79 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
81 # default blob_plain mimetype and default charset for text/plain blob
82 our $default_blob_plain_mimetype = 'text/plain';
83 our $default_text_plain_charset = undef;
85 # file to use for guessing MIME types before trying /etc/mime.types
86 # (relative to the current git repository)
87 our $mimetypes_file = undef;
89 # You define site-wide feature defaults here; override them with
90 # $GITWEB_CONFIG as necessary.
93 # 'sub' => feature-sub (subroutine),
94 # 'override' => allow-override (boolean),
95 # 'default' => [ default options...] (array reference)}
97 # if feature is overridable (it means that allow-override has true value,
98 # then feature-sub will be called with default options as parameters;
99 # return value of feature-sub indicates if to enable specified feature
101 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
103 # Enable the 'blame' blob view, showing the last commit that modified
104 # each line in the file. This can be very CPU-intensive.
106 # To enable system wide have in $GITWEB_CONFIG
107 # $feature{'blame'}{'default'} = [1];
108 # To have project specific config enable override in $GITWEB_CONFIG
109 # $feature{'blame'}{'override'} = 1;
110 # and in project config gitweb.blame = 0|1;
112 'sub' => \&feature_blame,
116 # Enable the 'snapshot' link, providing a compressed tarball of any
117 # tree. This can potentially generate high traffic if you have large
120 # To disable system wide have in $GITWEB_CONFIG
121 # $feature{'snapshot'}{'default'} = [undef];
122 # To have project specific config enable override in $GITWEB_CONFIG
123 # $feature{'snapshot'}{'override'} = 1;
124 # and in project config gitweb.snapshot = none|gzip|bzip2;
126 'sub' => \&feature_snapshot,
128 # => [content-encoding, suffix, program]
129 'default' => ['x-gzip', 'gz', 'gzip']},
131 # Enable the pickaxe search, which will list the commits that modified
132 # a given string in a file. This can be practical and quite faster
133 # alternative to 'blame', but still potentially CPU-intensive.
135 # To enable system wide have in $GITWEB_CONFIG
136 # $feature{'pickaxe'}{'default'} = [1];
137 # To have project specific config enable override in $GITWEB_CONFIG
138 # $feature{'pickaxe'}{'override'} = 1;
139 # and in project config gitweb.pickaxe = 0|1;
141 'sub' => \&feature_pickaxe,
145 # Make gitweb use an alternative format of the URLs which can be
146 # more readable and natural-looking: project name is embedded
147 # directly in the path and the query string contains other
148 # auxiliary information. All gitweb installations recognize
149 # URL in either format; this configures in which formats gitweb
152 # To enable system wide have in $GITWEB_CONFIG
153 # $feature{'pathinfo'}{'default'} = [1];
154 # Project specific override is not supported.
156 # Note that you will need to change the default location of CSS,
157 # favicon, logo and possibly other files to an absolute URL. Also,
158 # if gitweb.cgi serves as your indexfile, you will need to force
159 # $my_uri to contain the script name in your $GITWEB_CONFIG.
164 # Make gitweb consider projects in project root subdirectories
165 # to be forks of existing projects. Given project $projname.git,
166 # projects matching $projname/*.git will not be shown in the main
167 # projects list, instead a '+' mark will be added to $projname
168 # there and a 'forks' view will be enabled for the project, listing
169 # all the forks. This feature is supported only if project list
170 # is taken from a directory, not file.
172 # To enable system wide have in $GITWEB_CONFIG
173 # $feature{'forks'}{'default'} = [1];
174 # Project specific override is not supported.
180 sub gitweb_check_feature {
182 return unless exists $feature{$name};
183 my ($sub, $override, @defaults) = (
184 $feature{$name}{'sub'},
185 $feature{$name}{'override'},
186 @{$feature{$name}{'default'}});
187 if (!$override) { return @defaults; }
189 warn "feature $name is not overrideable";
192 return $sub->(@defaults);
196 my ($val) = git_get_project_config('blame', '--bool');
198 if ($val eq 'true') {
200 } elsif ($val eq 'false') {
207 sub feature_snapshot {
208 my ($ctype, $suffix, $command) = @_;
210 my ($val) = git_get_project_config('snapshot');
212 if ($val eq 'gzip') {
213 return ('x-gzip', 'gz', 'gzip');
214 } elsif ($val eq 'bzip2') {
215 return ('x-bzip2', 'bz2', 'bzip2');
216 } elsif ($val eq 'none') {
220 return ($ctype, $suffix, $command);
223 sub gitweb_have_snapshot {
224 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
225 my $have_snapshot = (defined $ctype && defined $suffix);
227 return $have_snapshot;
230 sub feature_pickaxe {
231 my ($val) = git_get_project_config('pickaxe', '--bool');
233 if ($val eq 'true') {
235 } elsif ($val eq 'false') {
242 # checking HEAD file with -e is fragile if the repository was
243 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
245 sub check_head_link {
247 my $headfile = "$dir/HEAD";
248 return ((-e $headfile) ||
249 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
252 sub check_export_ok {
254 return (check_head_link($dir) &&
255 (!$export_ok || -e "$dir/$export_ok"));
258 # rename detection options for git-diff and git-diff-tree
259 # - default is '-M', with the cost proportional to
260 # (number of removed files) * (number of new files).
261 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
262 # (number of changed files + number of removed files) * (number of new files)
263 # - even more costly is '-C', '--find-copies-harder' with cost
264 # (number of files in the original tree) * (number of new files)
265 # - one might want to include '-B' option, e.g. '-B', '-M'
266 our @diff_opts = ('-M'); # taken from git_commit
268 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
269 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
271 # version of the core git binary
272 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
274 $projects_list ||= $projectroot;
276 # ======================================================================
277 # input validation and dispatch
278 our $action = $cgi->param('a');
279 if (defined $action) {
280 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
281 die_error(undef, "Invalid action parameter");
285 # parameters which are pathnames
286 our $project = $cgi->param('p');
287 if (defined $project) {
288 if (!validate_pathname($project) ||
289 !(-d "$projectroot/$project") ||
290 !check_head_link("$projectroot/$project") ||
291 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
292 ($strict_export && !project_in_list($project))) {
294 die_error(undef, "No such project");
298 our $file_name = $cgi->param('f');
299 if (defined $file_name) {
300 if (!validate_pathname($file_name)) {
301 die_error(undef, "Invalid file parameter");
305 our $file_parent = $cgi->param('fp');
306 if (defined $file_parent) {
307 if (!validate_pathname($file_parent)) {
308 die_error(undef, "Invalid file parent parameter");
312 # parameters which are refnames
313 our $hash = $cgi->param('h');
315 if (!validate_refname($hash)) {
316 die_error(undef, "Invalid hash parameter");
320 our $hash_parent = $cgi->param('hp');
321 if (defined $hash_parent) {
322 if (!validate_refname($hash_parent)) {
323 die_error(undef, "Invalid hash parent parameter");
327 our $hash_base = $cgi->param('hb');
328 if (defined $hash_base) {
329 if (!validate_refname($hash_base)) {
330 die_error(undef, "Invalid hash base parameter");
334 our $hash_parent_base = $cgi->param('hpb');
335 if (defined $hash_parent_base) {
336 if (!validate_refname($hash_parent_base)) {
337 die_error(undef, "Invalid hash parent base parameter");
342 our $page = $cgi->param('pg');
344 if ($page =~ m/[^0-9]/) {
345 die_error(undef, "Invalid page parameter");
349 our $searchtext = $cgi->param('s');
350 if (defined $searchtext) {
351 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
352 die_error(undef, "Invalid search parameter");
354 if (length($searchtext) < 2) {
355 die_error(undef, "At least two characters are required for search parameter");
357 $searchtext = quotemeta $searchtext;
360 our $searchtype = $cgi->param('st');
361 if (defined $searchtype) {
362 if ($searchtype =~ m/[^a-z]/) {
363 die_error(undef, "Invalid searchtype parameter");
367 # now read PATH_INFO and use it as alternative to parameters
368 sub evaluate_path_info {
369 return if defined $project;
370 my $path_info = $ENV{"PATH_INFO"};
371 return if !$path_info;
372 $path_info =~ s,^/+,,;
373 return if !$path_info;
374 # find which part of PATH_INFO is project
375 $project = $path_info;
377 while ($project && !check_head_link("$projectroot/$project")) {
378 $project =~ s,/*[^/]*$,,;
381 $project = validate_pathname($project);
383 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
384 ($strict_export && !project_in_list($project))) {
388 # do not change any parameters if an action is given using the query string
390 $path_info =~ s,^$project/*,,;
391 my ($refname, $pathname) = split(/:/, $path_info, 2);
392 if (defined $pathname) {
393 # we got "project.git/branch:filename" or "project.git/branch:dir/"
394 # we could use git_get_type(branch:pathname), but it needs $git_dir
395 $pathname =~ s,^/+,,;
396 if (!$pathname || substr($pathname, -1) eq "/") {
400 $action ||= "blob_plain";
402 $hash_base ||= validate_refname($refname);
403 $file_name ||= validate_pathname($pathname);
404 } elsif (defined $refname) {
405 # we got "project.git/branch"
406 $action ||= "shortlog";
407 $hash ||= validate_refname($refname);
410 evaluate_path_info();
412 # path to the current git repository
414 $git_dir = "$projectroot/$project" if $project;
418 "blame" => \&git_blame2,
419 "blobdiff" => \&git_blobdiff,
420 "blobdiff_plain" => \&git_blobdiff_plain,
421 "blob" => \&git_blob,
422 "blob_plain" => \&git_blob_plain,
423 "commitdiff" => \&git_commitdiff,
424 "commitdiff_plain" => \&git_commitdiff_plain,
425 "commit" => \&git_commit,
426 "forks" => \&git_forks,
427 "heads" => \&git_heads,
428 "history" => \&git_history,
431 "atom" => \&git_atom,
432 "search" => \&git_search,
433 "search_help" => \&git_search_help,
434 "shortlog" => \&git_shortlog,
435 "summary" => \&git_summary,
437 "tags" => \&git_tags,
438 "tree" => \&git_tree,
439 "snapshot" => \&git_snapshot,
440 "object" => \&git_object,
441 # those below don't need $project
442 "opml" => \&git_opml,
443 "project_list" => \&git_project_list,
444 "project_index" => \&git_project_index,
447 if (defined $project) {
448 $action ||= 'summary';
450 $action ||= 'project_list';
452 if (!defined($actions{$action})) {
453 die_error(undef, "Unknown action");
455 if ($action !~ m/^(opml|project_list|project_index)$/ &&
457 die_error(undef, "Project needed");
459 $actions{$action}->();
462 ## ======================================================================
467 # default is to use -absolute url() i.e. $my_uri
468 my $href = $params{-full} ? $my_url : $my_uri;
470 # XXX: Warning: If you touch this, check the search form for updating,
481 hash_parent_base => "hpb",
487 my %mapping = @mapping;
489 $params{'project'} = $project unless exists $params{'project'};
491 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
493 # use PATH_INFO for project name
494 $href .= "/$params{'project'}" if defined $params{'project'};
495 delete $params{'project'};
497 # Summary just uses the project path URL
498 if (defined $params{'action'} && $params{'action'} eq 'summary') {
499 delete $params{'action'};
503 # now encode the parameters explicitly
505 for (my $i = 0; $i < @mapping; $i += 2) {
506 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
507 if (defined $params{$name}) {
508 push @result, $symbol . "=" . esc_param($params{$name});
511 $href .= "?" . join(';', @result) if scalar @result;
517 ## ======================================================================
518 ## validation, quoting/unquoting and escaping
520 sub validate_pathname {
521 my $input = shift || return undef;
523 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
524 # at the beginning, at the end, and between slashes.
525 # also this catches doubled slashes
526 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
530 if ($input =~ m!\0!) {
536 sub validate_refname {
537 my $input = shift || return undef;
539 # textual hashes are O.K.
540 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
543 # it must be correct pathname
544 $input = validate_pathname($input)
546 # restrictions on ref name according to git-check-ref-format
547 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
553 # very thin wrapper for decode("utf8", $str, Encode::FB_DEFAULT);
556 return decode("utf8", $str, Encode::FB_DEFAULT);
559 # quote unsafe chars, but keep the slash, even when it's not
560 # correct, but quoted slashes look too horrible in bookmarks
563 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
569 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
572 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
578 # replace invalid utf8 character with SUBSTITUTION sequence
583 $str = to_utf8($str);
584 $str = escapeHTML($str);
585 if ($opts{'-nbsp'}) {
586 $str =~ s/ / /g;
588 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
592 # quote control characters and escape filename to HTML
597 $str = to_utf8($str);
598 $str = escapeHTML($str);
599 if ($opts{'-nbsp'}) {
600 $str =~ s/ / /g;
602 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
606 # Make control characters "printable", using character escape codes (CEC)
609 my %es = ( # character escape codes, aka escape sequences
610 "\t" => '\t', # tab (HT)
611 "\n" => '\n', # line feed (LF)
612 "\r" => '\r', # carrige return (CR)
613 "\f" => '\f', # form feed (FF)
614 "\b" => '\b', # backspace (BS)
615 "\a" => '\a', # alarm (bell) (BEL)
616 "\e" => '\e', # escape (ESC)
617 "\013" => '\v', # vertical tab (VT)
618 "\000" => '\0', # nul character (NUL)
620 my $chr = ( (exists $es{$cntrl})
622 : sprintf('\%03o', ord($cntrl)) );
623 return "<span class=\"cntrl\">$chr</span>";
626 # Alternatively use unicode control pictures codepoints,
627 # Unicode "printable representation" (PR)
630 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
631 return "<span class=\"cntrl\">$chr</span>";
634 # git may return quoted and escaped filenames
640 my %es = ( # character escape codes, aka escape sequences
641 't' => "\t", # tab (HT, TAB)
642 'n' => "\n", # newline (NL)
643 'r' => "\r", # return (CR)
644 'f' => "\f", # form feed (FF)
645 'b' => "\b", # backspace (BS)
646 'a' => "\a", # alarm (bell) (BEL)
647 'e' => "\e", # escape (ESC)
648 'v' => "\013", # vertical tab (VT)
651 if ($seq =~ m/^[0-7]{1,3}$/) {
652 # octal char sequence
653 return chr(oct($seq));
654 } elsif (exists $es{$seq}) {
655 # C escape sequence, aka character escape code
658 # quoted ordinary character
662 if ($str =~ m/^"(.*)"$/) {
665 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
670 # escape tabs (convert tabs to spaces)
674 while ((my $pos = index($line, "\t")) != -1) {
675 if (my $count = (8 - ($pos % 8))) {
676 my $spaces = ' ' x $count;
677 $line =~ s/\t/$spaces/;
684 sub project_in_list {
686 my @list = git_get_projects_list();
687 return @list && scalar(grep { $_->{'path'} eq $project } @list);
690 ## ----------------------------------------------------------------------
691 ## HTML aware string manipulation
696 my $add_len = shift || 10;
698 # allow only $len chars, but don't cut a word if it would fit in $add_len
699 # if it doesn't fit, cut it if it's still longer than the dots we would add
700 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
703 if (length($tail) > 4) {
705 $body =~ s/&[^;]*$//; # remove chopped character entities
710 ## ----------------------------------------------------------------------
711 ## functions returning short strings
713 # CSS class for given age value (in seconds)
717 if ($age < 60*60*2) {
719 } elsif ($age < 60*60*24*2) {
726 # convert age in seconds to "nn units ago" string
731 if ($age > 60*60*24*365*2) {
732 $age_str = (int $age/60/60/24/365);
733 $age_str .= " years ago";
734 } elsif ($age > 60*60*24*(365/12)*2) {
735 $age_str = int $age/60/60/24/(365/12);
736 $age_str .= " months ago";
737 } elsif ($age > 60*60*24*7*2) {
738 $age_str = int $age/60/60/24/7;
739 $age_str .= " weeks ago";
740 } elsif ($age > 60*60*24*2) {
741 $age_str = int $age/60/60/24;
742 $age_str .= " days ago";
743 } elsif ($age > 60*60*2) {
744 $age_str = int $age/60/60;
745 $age_str .= " hours ago";
746 } elsif ($age > 60*2) {
747 $age_str = int $age/60;
748 $age_str .= " min ago";
751 $age_str .= " sec ago";
753 $age_str .= " right now";
758 # convert file mode in octal to symbolic file mode string
760 my $mode = oct shift;
762 if (S_ISDIR($mode & S_IFMT)) {
764 } elsif (S_ISLNK($mode)) {
766 } elsif (S_ISREG($mode)) {
767 # git cares only about the executable bit
768 if ($mode & S_IXUSR) {
778 # convert file mode in octal to file type string
782 if ($mode !~ m/^[0-7]+$/) {
788 if (S_ISDIR($mode & S_IFMT)) {
790 } elsif (S_ISLNK($mode)) {
792 } elsif (S_ISREG($mode)) {
799 # convert file mode in octal to file type description string
803 if ($mode !~ m/^[0-7]+$/) {
809 if (S_ISDIR($mode & S_IFMT)) {
811 } elsif (S_ISLNK($mode)) {
813 } elsif (S_ISREG($mode)) {
814 if ($mode & S_IXUSR) {
825 ## ----------------------------------------------------------------------
826 ## functions returning short HTML fragments, or transforming HTML fragments
827 ## which don't beling to other sections
829 # format line of commit message.
830 sub format_log_line_html {
833 $line = esc_html($line, -nbsp=>1);
834 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
837 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
838 -class => "text"}, $hash_text);
839 $line =~ s/$hash_text/$link/;
844 # format marker of refs pointing to given object
845 sub format_ref_marker {
846 my ($refs, $id) = @_;
849 if (defined $refs->{$id}) {
850 foreach my $ref (@{$refs->{$id}}) {
851 my ($type, $name) = qw();
852 # e.g. tags/v2.6.11 or heads/next
853 if ($ref =~ m!^(.*?)s?/(.*)$!) {
861 $markers .= " <span class=\"$type\" title=\"$ref\">" .
862 esc_html($name) . "</span>";
867 return ' <span class="refs">'. $markers . '</span>';
873 # format, perhaps shortened and with markers, title line
874 sub format_subject_html {
875 my ($long, $short, $href, $extra) = @_;
876 $extra = '' unless defined($extra);
878 if (length($short) < length($long)) {
879 return $cgi->a({-href => $href, -class => "list subject",
880 -title => to_utf8($long)},
881 esc_html($short) . $extra);
883 return $cgi->a({-href => $href, -class => "list subject"},
884 esc_html($long) . $extra);
888 # format patch (diff) line (rather not to be used for diff headers)
889 sub format_diff_line {
891 my ($from, $to) = @_;
892 my $char = substr($line, 0, 1);
898 $diff_class = " add";
899 } elsif ($char eq "-") {
900 $diff_class = " rem";
901 } elsif ($char eq "@") {
902 $diff_class = " chunk_header";
903 } elsif ($char eq "\\") {
904 $diff_class = " incomplete";
906 $line = untabify($line);
907 if ($from && $to && $line =~ m/^\@{2} /) {
908 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
909 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
911 $from_lines = 0 unless defined $from_lines;
912 $to_lines = 0 unless defined $to_lines;
914 if ($from->{'href'}) {
915 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
916 -class=>"list"}, $from_text);
919 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
920 -class=>"list"}, $to_text);
922 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
923 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
924 return "<div class=\"diff$diff_class\">$line</div>\n";
926 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
929 ## ----------------------------------------------------------------------
930 ## git utility subroutines, invoking git commands
932 # returns path to the core git executable and the --git-dir parameter as list
934 return $GIT, '--git-dir='.$git_dir;
937 # returns path to the core git executable and the --git-dir parameter as string
939 return join(' ', git_cmd());
942 # get HEAD ref of given project as hash
943 sub git_get_head_hash {
945 my $o_git_dir = $git_dir;
947 $git_dir = "$projectroot/$project";
948 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
951 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
955 if (defined $o_git_dir) {
956 $git_dir = $o_git_dir;
961 # get type of given object
965 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
972 sub git_get_project_config {
973 my ($key, $type) = @_;
975 return unless ($key);
976 $key =~ s/^gitweb\.//;
977 return if ($key =~ m/\W/);
979 my @x = (git_cmd(), 'repo-config');
980 if (defined $type) { push @x, $type; }
982 push @x, "gitweb.$key";
988 # get hash of given path at given ref
989 sub git_get_hash_by_path {
991 my $path = shift || return undef;
996 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
997 or die_error(undef, "Open git-ls-tree failed");
999 close $fd or return undef;
1001 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1002 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1003 if (defined $type && $type ne $2) {
1004 # type doesn't match
1010 ## ......................................................................
1011 ## git utility functions, directly accessing git repository
1013 sub git_get_project_description {
1016 open my $fd, "$projectroot/$path/description" or return undef;
1023 sub git_get_project_url_list {
1026 open my $fd, "$projectroot/$path/cloneurl" or return;
1027 my @git_project_url_list = map { chomp; $_ } <$fd>;
1030 return wantarray ? @git_project_url_list : \@git_project_url_list;
1033 sub git_get_projects_list {
1038 $filter =~ s/\.git$//;
1040 if (-d $projects_list) {
1041 # search in directory
1042 my $dir = $projects_list . ($filter ? "/$filter" : '');
1043 # remove the trailing "/"
1045 my $pfxlen = length("$dir");
1047 my ($check_forks) = gitweb_check_feature('forks');
1050 follow_fast => 1, # follow symbolic links
1051 dangling_symlinks => 0, # ignore dangling symlinks, silently
1053 # skip project-list toplevel, if we get it.
1054 return if (m!^[/.]$!);
1055 # only directories can be git repositories
1056 return unless (-d $_);
1058 my $subdir = substr($File::Find::name, $pfxlen + 1);
1059 # we check related file in $projectroot
1060 if ($check_forks and $subdir =~ m#/.#) {
1061 $File::Find::prune = 1;
1062 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1063 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1064 $File::Find::prune = 1;
1069 } elsif (-f $projects_list) {
1070 # read from file(url-encoded):
1071 # 'git%2Fgit.git Linus+Torvalds'
1072 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1073 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1074 open my ($fd), $projects_list or return;
1075 while (my $line = <$fd>) {
1077 my ($path, $owner) = split ' ', $line;
1078 $path = unescape($path);
1079 $owner = unescape($owner);
1080 if (!defined $path) {
1083 if ($filter ne '') {
1084 # looking for forks;
1085 my $pfx = substr($path, 0, length($filter));
1086 if ($pfx ne $filter) {
1089 my $sfx = substr($path, length($filter));
1090 if ($sfx !~ /^\/.*\.git$/) {
1094 if (check_export_ok("$projectroot/$path")) {
1097 owner => to_utf8($owner),
1104 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
1108 sub git_get_project_owner {
1109 my $project = shift;
1112 return undef unless $project;
1114 # read from file (url-encoded):
1115 # 'git%2Fgit.git Linus+Torvalds'
1116 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1117 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1118 if (-f $projects_list) {
1119 open (my $fd , $projects_list);
1120 while (my $line = <$fd>) {
1122 my ($pr, $ow) = split ' ', $line;
1123 $pr = unescape($pr);
1124 $ow = unescape($ow);
1125 if ($pr eq $project) {
1126 $owner = to_utf8($ow);
1132 if (!defined $owner) {
1133 $owner = get_file_owner("$projectroot/$project");
1139 sub git_get_last_activity {
1143 $git_dir = "$projectroot/$path";
1144 open($fd, "-|", git_cmd(), 'for-each-ref',
1145 '--format=%(committer)',
1146 '--sort=-committerdate',
1148 'refs/heads') or return;
1149 my $most_recent = <$fd>;
1150 close $fd or return;
1151 if ($most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1153 my $age = time - $timestamp;
1154 return ($age, age_string($age));
1158 sub git_get_references {
1159 my $type = shift || "";
1161 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1162 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1163 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1164 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1167 while (my $line = <$fd>) {
1169 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1170 if (defined $refs{$1}) {
1171 push @{$refs{$1}}, $2;
1177 close $fd or return;
1181 sub git_get_rev_name_tags {
1182 my $hash = shift || return undef;
1184 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1186 my $name_rev = <$fd>;
1189 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1192 # catches also '$hash undefined' output
1197 ## ----------------------------------------------------------------------
1198 ## parse to hash functions
1202 my $tz = shift || "-0000";
1205 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1206 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1207 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1208 $date{'hour'} = $hour;
1209 $date{'minute'} = $min;
1210 $date{'mday'} = $mday;
1211 $date{'day'} = $days[$wday];
1212 $date{'month'} = $months[$mon];
1213 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1214 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1215 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1216 $mday, $months[$mon], $hour ,$min;
1217 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1218 1900+$year, $mon, $mday, $hour ,$min, $sec;
1220 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1221 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1222 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1223 $date{'hour_local'} = $hour;
1224 $date{'minute_local'} = $min;
1225 $date{'tz_local'} = $tz;
1226 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1227 1900+$year, $mon+1, $mday,
1228 $hour, $min, $sec, $tz);
1237 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1238 $tag{'id'} = $tag_id;
1239 while (my $line = <$fd>) {
1241 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1242 $tag{'object'} = $1;
1243 } elsif ($line =~ m/^type (.+)$/) {
1245 } elsif ($line =~ m/^tag (.+)$/) {
1247 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1248 $tag{'author'} = $1;
1251 } elsif ($line =~ m/--BEGIN/) {
1252 push @comment, $line;
1254 } elsif ($line eq "") {
1258 push @comment, <$fd>;
1259 $tag{'comment'} = \@comment;
1260 close $fd or return;
1261 if (!defined $tag{'name'}) {
1268 my $commit_id = shift;
1269 my $commit_text = shift;
1274 if (defined $commit_text) {
1275 @commit_lines = @$commit_text;
1278 open my $fd, "-|", git_cmd(), "rev-list",
1279 "--header", "--parents", "--max-count=1",
1282 @commit_lines = split '\n', <$fd>;
1283 close $fd or return;
1286 my $header = shift @commit_lines;
1287 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1290 ($co{'id'}, my @parents) = split ' ', $header;
1291 $co{'parents'} = \@parents;
1292 $co{'parent'} = $parents[0];
1293 while (my $line = shift @commit_lines) {
1294 last if $line eq "\n";
1295 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1297 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1299 $co{'author_epoch'} = $2;
1300 $co{'author_tz'} = $3;
1301 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1302 $co{'author_name'} = $1;
1303 $co{'author_email'} = $2;
1305 $co{'author_name'} = $co{'author'};
1307 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1308 $co{'committer'} = $1;
1309 $co{'committer_epoch'} = $2;
1310 $co{'committer_tz'} = $3;
1311 $co{'committer_name'} = $co{'committer'};
1312 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1313 $co{'committer_name'} = $1;
1314 $co{'committer_email'} = $2;
1316 $co{'committer_name'} = $co{'committer'};
1320 if (!defined $co{'tree'}) {
1324 foreach my $title (@commit_lines) {
1327 $co{'title'} = chop_str($title, 80, 5);
1328 # remove leading stuff of merges to make the interesting part visible
1329 if (length($title) > 50) {
1330 $title =~ s/^Automatic //;
1331 $title =~ s/^merge (of|with) /Merge ... /i;
1332 if (length($title) > 50) {
1333 $title =~ s/(http|rsync):\/\///;
1335 if (length($title) > 50) {
1336 $title =~ s/(master|www|rsync)\.//;
1338 if (length($title) > 50) {
1339 $title =~ s/kernel.org:?//;
1341 if (length($title) > 50) {
1342 $title =~ s/\/pub\/scm//;
1345 $co{'title_short'} = chop_str($title, 50, 5);
1349 if ($co{'title'} eq "") {
1350 $co{'title'} = $co{'title_short'} = '(no commit message)';
1352 # remove added spaces
1353 foreach my $line (@commit_lines) {
1356 $co{'comment'} = \@commit_lines;
1358 my $age = time - $co{'committer_epoch'};
1360 $co{'age_string'} = age_string($age);
1361 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1362 if ($age > 60*60*24*7*2) {
1363 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1364 $co{'age_string_age'} = $co{'age_string'};
1366 $co{'age_string_date'} = $co{'age_string'};
1367 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1372 # parse ref from ref_file, given by ref_id, with given type
1374 my $ref_file = shift;
1376 my $type = shift || git_get_type($ref_id);
1379 $ref_item{'type'} = $type;
1380 $ref_item{'id'} = $ref_id;
1381 $ref_item{'epoch'} = 0;
1382 $ref_item{'age'} = "unknown";
1383 if ($type eq "tag") {
1384 my %tag = parse_tag($ref_id);
1385 $ref_item{'comment'} = $tag{'comment'};
1386 if ($tag{'type'} eq "commit") {
1387 my %co = parse_commit($tag{'object'});
1388 $ref_item{'epoch'} = $co{'committer_epoch'};
1389 $ref_item{'age'} = $co{'age_string'};
1390 } elsif (defined($tag{'epoch'})) {
1391 my $age = time - $tag{'epoch'};
1392 $ref_item{'epoch'} = $tag{'epoch'};
1393 $ref_item{'age'} = age_string($age);
1395 $ref_item{'reftype'} = $tag{'type'};
1396 $ref_item{'name'} = $tag{'name'};
1397 $ref_item{'refid'} = $tag{'object'};
1398 } elsif ($type eq "commit"){
1399 my %co = parse_commit($ref_id);
1400 $ref_item{'reftype'} = "commit";
1401 $ref_item{'name'} = $ref_file;
1402 $ref_item{'title'} = $co{'title'};
1403 $ref_item{'refid'} = $ref_id;
1404 $ref_item{'epoch'} = $co{'committer_epoch'};
1405 $ref_item{'age'} = $co{'age_string'};
1407 $ref_item{'reftype'} = $type;
1408 $ref_item{'name'} = $ref_file;
1409 $ref_item{'refid'} = $ref_id;
1415 # parse line of git-diff-tree "raw" output
1416 sub parse_difftree_raw_line {
1420 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1421 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1422 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1423 $res{'from_mode'} = $1;
1424 $res{'to_mode'} = $2;
1425 $res{'from_id'} = $3;
1427 $res{'status'} = $5;
1428 $res{'similarity'} = $6;
1429 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1430 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1432 $res{'file'} = unquote($7);
1435 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1436 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1437 $res{'commit'} = $1;
1440 return wantarray ? %res : \%res;
1443 # parse line of git-ls-tree output
1444 sub parse_ls_tree_line ($;%) {
1449 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1450 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1458 $res{'name'} = unquote($4);
1461 return wantarray ? %res : \%res;
1464 ## ......................................................................
1465 ## parse to array of hashes functions
1467 sub git_get_heads_list {
1471 open my $fd, '-|', git_cmd(), 'for-each-ref',
1472 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1473 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1476 while (my $line = <$fd>) {
1480 my ($refinfo, $committerinfo) = split(/\0/, $line);
1481 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1482 my ($committer, $epoch, $tz) =
1483 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1484 $name =~ s!^refs/heads/!!;
1486 $ref_item{'name'} = $name;
1487 $ref_item{'id'} = $hash;
1488 $ref_item{'title'} = $title || '(no commit message)';
1489 $ref_item{'epoch'} = $epoch;
1491 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1493 $ref_item{'age'} = "unknown";
1496 push @headslist, \%ref_item;
1500 return wantarray ? @headslist : \@headslist;
1503 sub git_get_tags_list {
1507 open my $fd, '-|', git_cmd(), 'for-each-ref',
1508 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1509 '--format=%(objectname) %(objecttype) %(refname) '.
1510 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1513 while (my $line = <$fd>) {
1517 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1518 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1519 my ($creator, $epoch, $tz) =
1520 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1521 $name =~ s!^refs/tags/!!;
1523 $ref_item{'type'} = $type;
1524 $ref_item{'id'} = $id;
1525 $ref_item{'name'} = $name;
1526 if ($type eq "tag") {
1527 $ref_item{'subject'} = $title;
1528 $ref_item{'reftype'} = $reftype;
1529 $ref_item{'refid'} = $refid;
1531 $ref_item{'reftype'} = $type;
1532 $ref_item{'refid'} = $id;
1535 if ($type eq "tag" || $type eq "commit") {
1536 $ref_item{'epoch'} = $epoch;
1538 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1540 $ref_item{'age'} = "unknown";
1544 push @tagslist, \%ref_item;
1548 return wantarray ? @tagslist : \@tagslist;
1551 ## ----------------------------------------------------------------------
1552 ## filesystem-related functions
1554 sub get_file_owner {
1557 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1558 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1559 if (!defined $gcos) {
1563 $owner =~ s/[,;].*$//;
1564 return to_utf8($owner);
1567 ## ......................................................................
1568 ## mimetype related functions
1570 sub mimetype_guess_file {
1571 my $filename = shift;
1572 my $mimemap = shift;
1573 -r $mimemap or return undef;
1576 open(MIME, $mimemap) or return undef;
1578 next if m/^#/; # skip comments
1579 my ($mime, $exts) = split(/\t+/);
1580 if (defined $exts) {
1581 my @exts = split(/\s+/, $exts);
1582 foreach my $ext (@exts) {
1583 $mimemap{$ext} = $mime;
1589 $filename =~ /\.([^.]*)$/;
1590 return $mimemap{$1};
1593 sub mimetype_guess {
1594 my $filename = shift;
1596 $filename =~ /\./ or return undef;
1598 if ($mimetypes_file) {
1599 my $file = $mimetypes_file;
1600 if ($file !~ m!^/!) { # if it is relative path
1601 # it is relative to project
1602 $file = "$projectroot/$project/$file";
1604 $mime = mimetype_guess_file($filename, $file);
1606 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1612 my $filename = shift;
1615 my $mime = mimetype_guess($filename);
1616 $mime and return $mime;
1620 return $default_blob_plain_mimetype unless $fd;
1623 return 'text/plain' .
1624 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1625 } elsif (! $filename) {
1626 return 'application/octet-stream';
1627 } elsif ($filename =~ m/\.png$/i) {
1629 } elsif ($filename =~ m/\.gif$/i) {
1631 } elsif ($filename =~ m/\.jpe?g$/i) {
1632 return 'image/jpeg';
1634 return 'application/octet-stream';
1638 ## ======================================================================
1639 ## functions printing HTML: header, footer, error page
1641 sub git_header_html {
1642 my $status = shift || "200 OK";
1643 my $expires = shift;
1645 my $title = "$site_name";
1646 if (defined $project) {
1647 $title .= " - $project";
1648 if (defined $action) {
1649 $title .= "/$action";
1650 if (defined $file_name) {
1651 $title .= " - " . esc_path($file_name);
1652 if ($action eq "tree" && $file_name !~ m|/$|) {
1659 # require explicit support from the UA if we are to send the page as
1660 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1661 # we have to do this because MSIE sometimes globs '*/*', pretending to
1662 # support xhtml+xml but choking when it gets what it asked for.
1663 if (defined $cgi->http('HTTP_ACCEPT') &&
1664 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1665 $cgi->Accept('application/xhtml+xml') != 0) {
1666 $content_type = 'application/xhtml+xml';
1668 $content_type = 'text/html';
1670 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1671 -status=> $status, -expires => $expires);
1673 <?xml version="1.0" encoding="utf-8"?>
1674 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1675 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1676 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1677 <!-- git core binaries version $git_version -->
1679 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1680 <meta name="generator" content="gitweb/$version git/$git_version"/>
1681 <meta name="robots" content="index, nofollow"/>
1682 <title>$title</title>
1684 # print out each stylesheet that exist
1685 if (defined $stylesheet) {
1686 #provides backwards capability for those people who define style sheet in a config file
1687 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1689 foreach my $stylesheet (@stylesheets) {
1690 next unless $stylesheet;
1691 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1694 if (defined $project) {
1695 printf('<link rel="alternate" title="%s log RSS feed" '.
1696 'href="%s" type="application/rss+xml" />'."\n",
1697 esc_param($project), href(action=>"rss"));
1698 printf('<link rel="alternate" title="%s log Atom feed" '.
1699 'href="%s" type="application/atom+xml" />'."\n",
1700 esc_param($project), href(action=>"atom"));
1702 printf('<link rel="alternate" title="%s projects list" '.
1703 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1704 $site_name, href(project=>undef, action=>"project_index"));
1705 printf('<link rel="alternate" title="%s projects feeds" '.
1706 'href="%s" type="text/x-opml"/>'."\n",
1707 $site_name, href(project=>undef, action=>"opml"));
1709 if (defined $favicon) {
1710 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1716 if (-f $site_header) {
1717 open (my $fd, $site_header);
1722 print "<div class=\"page_header\">\n" .
1723 $cgi->a({-href => esc_url($logo_url),
1724 -title => $logo_label},
1725 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1726 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1727 if (defined $project) {
1728 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1729 if (defined $action) {
1733 if (!defined $searchtext) {
1737 if (defined $hash_base) {
1738 $search_hash = $hash_base;
1739 } elsif (defined $hash) {
1740 $search_hash = $hash;
1742 $search_hash = "HEAD";
1744 $cgi->param("a", "search");
1745 $cgi->param("h", $search_hash);
1746 $cgi->param("p", $project);
1747 print $cgi->startform(-method => "get", -action => $my_uri) .
1748 "<div class=\"search\">\n" .
1749 $cgi->hidden(-name => "p") . "\n" .
1750 $cgi->hidden(-name => "a") . "\n" .
1751 $cgi->hidden(-name => "h") . "\n" .
1752 $cgi->popup_menu(-name => 'st', -default => 'commit',
1753 -values => ['commit', 'author', 'committer', 'pickaxe']) .
1754 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1756 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1758 $cgi->end_form() . "\n";
1763 sub git_footer_html {
1764 print "<div class=\"page_footer\">\n";
1765 if (defined $project) {
1766 my $descr = git_get_project_description($project);
1767 if (defined $descr) {
1768 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1770 print $cgi->a({-href => href(action=>"rss"),
1771 -class => "rss_logo"}, "RSS") . " ";
1772 print $cgi->a({-href => href(action=>"atom"),
1773 -class => "rss_logo"}, "Atom") . "\n";
1775 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1776 -class => "rss_logo"}, "OPML") . " ";
1777 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1778 -class => "rss_logo"}, "TXT") . "\n";
1782 if (-f $site_footer) {
1783 open (my $fd, $site_footer);
1793 my $status = shift || "403 Forbidden";
1794 my $error = shift || "Malformed query, file missing or permission denied";
1796 git_header_html($status);
1798 <div class="page_body">
1808 ## ----------------------------------------------------------------------
1809 ## functions printing or outputting HTML: navigation
1811 sub git_print_page_nav {
1812 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1813 $extra = '' if !defined $extra; # pager or formats
1815 my @navs = qw(summary shortlog log commit commitdiff tree);
1817 @navs = grep { $_ ne $suppress } @navs;
1820 my %arg = map { $_ => {action=>$_} } @navs;
1821 if (defined $head) {
1822 for (qw(commit commitdiff)) {
1823 $arg{$_}{hash} = $head;
1825 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1826 for (qw(shortlog log)) {
1827 $arg{$_}{hash} = $head;
1831 $arg{tree}{hash} = $treehead if defined $treehead;
1832 $arg{tree}{hash_base} = $treebase if defined $treebase;
1834 print "<div class=\"page_nav\">\n" .
1836 map { $_ eq $current ?
1837 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1839 print "<br/>\n$extra<br/>\n" .
1843 sub format_paging_nav {
1844 my ($action, $hash, $head, $page, $nrevs) = @_;
1848 if ($hash ne $head || $page) {
1849 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1851 $paging_nav .= "HEAD";
1855 $paging_nav .= " ⋅ " .
1856 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1857 -accesskey => "p", -title => "Alt-p"}, "prev");
1859 $paging_nav .= " ⋅ prev";
1862 if ($nrevs >= (100 * ($page+1)-1)) {
1863 $paging_nav .= " ⋅ " .
1864 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1865 -accesskey => "n", -title => "Alt-n"}, "next");
1867 $paging_nav .= " ⋅ next";
1873 ## ......................................................................
1874 ## functions printing or outputting HTML: div
1876 sub git_print_header_div {
1877 my ($action, $title, $hash, $hash_base) = @_;
1880 $args{action} = $action;
1881 $args{hash} = $hash if $hash;
1882 $args{hash_base} = $hash_base if $hash_base;
1884 print "<div class=\"header\">\n" .
1885 $cgi->a({-href => href(%args), -class => "title"},
1886 $title ? $title : $action) .
1890 #sub git_print_authorship (\%) {
1891 sub git_print_authorship {
1894 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1895 print "<div class=\"author_date\">" .
1896 esc_html($co->{'author_name'}) .
1898 if ($ad{'hour_local'} < 6) {
1899 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1900 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1902 printf(" (%02d:%02d %s)",
1903 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1908 sub git_print_page_path {
1914 print "<div class=\"page_path\">";
1915 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1916 -title => 'tree root'}, "[$project]");
1918 if (defined $name) {
1919 my @dirname = split '/', $name;
1920 my $basename = pop @dirname;
1923 foreach my $dir (@dirname) {
1924 $fullname .= ($fullname ? '/' : '') . $dir;
1925 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1927 -title => esc_html($fullname)}, esc_path($dir));
1930 if (defined $type && $type eq 'blob') {
1931 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1933 -title => esc_html($name)}, esc_path($basename));
1934 } elsif (defined $type && $type eq 'tree') {
1935 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1937 -title => esc_html($name)}, esc_path($basename));
1940 print esc_path($basename);
1943 print "<br/></div>\n";
1946 # sub git_print_log (\@;%) {
1947 sub git_print_log ($;%) {
1951 if ($opts{'-remove_title'}) {
1952 # remove title, i.e. first line of log
1955 # remove leading empty lines
1956 while (defined $log->[0] && $log->[0] eq "") {
1963 foreach my $line (@$log) {
1964 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1967 if (! $opts{'-remove_signoff'}) {
1968 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1971 # remove signoff lines
1978 # print only one empty line
1979 # do not print empty line after signoff
1981 next if ($empty || $signoff);
1987 print format_log_line_html($line) . "<br/>\n";
1990 if ($opts{'-final_empty_line'}) {
1991 # end with single empty line
1992 print "<br/>\n" unless $empty;
1996 # return link target (what link points to)
1997 sub git_get_link_target {
2002 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2006 $link_target = <$fd>;
2011 return $link_target;
2014 # given link target, and the directory (basedir) the link is in,
2015 # return target of link relative to top directory (top tree);
2016 # return undef if it is not possible (including absolute links).
2017 sub normalize_link_target {
2018 my ($link_target, $basedir, $hash_base) = @_;
2020 # we can normalize symlink target only if $hash_base is provided
2021 return unless $hash_base;
2023 # absolute symlinks (beginning with '/') cannot be normalized
2024 return if (substr($link_target, 0, 1) eq '/');
2026 # normalize link target to path from top (root) tree (dir)
2029 $path = $basedir . '/' . $link_target;
2031 # we are in top (root) tree (dir)
2032 $path = $link_target;
2035 # remove //, /./, and /../
2037 foreach my $part (split('/', $path)) {
2038 # discard '.' and ''
2039 next if (!$part || $part eq '.');
2041 if ($part eq '..') {
2045 # link leads outside repository (outside top dir)
2049 push @path_parts, $part;
2052 $path = join('/', @path_parts);
2057 # print tree entry (row of git_tree), but without encompassing <tr> element
2058 sub git_print_tree_entry {
2059 my ($t, $basedir, $hash_base, $have_blame) = @_;
2062 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2064 # The format of a table row is: mode list link. Where mode is
2065 # the mode of the entry, list is the name of the entry, an href,
2066 # and link is the action links of the entry.
2068 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2069 if ($t->{'type'} eq "blob") {
2070 print "<td class=\"list\">" .
2071 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2072 file_name=>"$basedir$t->{'name'}", %base_key),
2073 -class => "list"}, esc_path($t->{'name'}));
2074 if (S_ISLNK(oct $t->{'mode'})) {
2075 my $link_target = git_get_link_target($t->{'hash'});
2077 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2078 if (defined $norm_target) {
2080 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2081 file_name=>$norm_target),
2082 -title => $norm_target}, esc_path($link_target));
2084 print " -> " . esc_path($link_target);
2089 print "<td class=\"link\">";
2090 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2091 file_name=>"$basedir$t->{'name'}", %base_key)},
2095 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2096 file_name=>"$basedir$t->{'name'}", %base_key)},
2099 if (defined $hash_base) {
2101 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2102 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2106 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2107 file_name=>"$basedir$t->{'name'}")},
2111 } elsif ($t->{'type'} eq "tree") {
2112 print "<td class=\"list\">";
2113 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2114 file_name=>"$basedir$t->{'name'}", %base_key)},
2115 esc_path($t->{'name'}));
2117 print "<td class=\"link\">";
2118 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2119 file_name=>"$basedir$t->{'name'}", %base_key)},
2121 if (defined $hash_base) {
2123 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2124 file_name=>"$basedir$t->{'name'}")},
2131 ## ......................................................................
2132 ## functions printing large fragments of HTML
2134 sub git_difftree_body {
2135 my ($difftree, $hash, $parent) = @_;
2136 my ($have_blame) = gitweb_check_feature('blame');
2137 print "<div class=\"list_head\">\n";
2138 if ($#{$difftree} > 10) {
2139 print(($#{$difftree} + 1) . " files changed:\n");
2143 print "<table class=\"diff_tree\">\n";
2146 foreach my $line (@{$difftree}) {
2147 my %diff = parse_difftree_raw_line($line);
2150 print "<tr class=\"dark\">\n";
2152 print "<tr class=\"light\">\n";
2156 my ($to_mode_oct, $to_mode_str, $to_file_type);
2157 my ($from_mode_oct, $from_mode_str, $from_file_type);
2158 if ($diff{'to_mode'} ne ('0' x 6)) {
2159 $to_mode_oct = oct $diff{'to_mode'};
2160 if (S_ISREG($to_mode_oct)) { # only for regular file
2161 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2163 $to_file_type = file_type($diff{'to_mode'});
2165 if ($diff{'from_mode'} ne ('0' x 6)) {
2166 $from_mode_oct = oct $diff{'from_mode'};
2167 if (S_ISREG($to_mode_oct)) { # only for regular file
2168 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2170 $from_file_type = file_type($diff{'from_mode'});
2173 if ($diff{'status'} eq "A") { # created
2174 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2175 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2176 $mode_chng .= "]</span>";
2178 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2179 hash_base=>$hash, file_name=>$diff{'file'}),
2180 -class => "list"}, esc_path($diff{'file'}));
2182 print "<td>$mode_chng</td>\n";
2183 print "<td class=\"link\">";
2184 if ($action eq 'commitdiff') {
2187 print $cgi->a({-href => "#patch$patchno"}, "patch");
2190 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2191 hash_base=>$hash, file_name=>$diff{'file'})},
2195 } elsif ($diff{'status'} eq "D") { # deleted
2196 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2198 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
2199 hash_base=>$parent, file_name=>$diff{'file'}),
2200 -class => "list"}, esc_path($diff{'file'}));
2202 print "<td>$mode_chng</td>\n";
2203 print "<td class=\"link\">";
2204 if ($action eq 'commitdiff') {
2207 print $cgi->a({-href => "#patch$patchno"}, "patch");
2210 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
2211 hash_base=>$parent, file_name=>$diff{'file'})},
2214 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2215 file_name=>$diff{'file'})},
2218 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2219 file_name=>$diff{'file'})},
2223 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
2224 my $mode_chnge = "";
2225 if ($diff{'from_mode'} != $diff{'to_mode'}) {
2226 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2227 if ($from_file_type != $to_file_type) {
2228 $mode_chnge .= " from $from_file_type to $to_file_type";
2230 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2231 if ($from_mode_str && $to_mode_str) {
2232 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2233 } elsif ($to_mode_str) {
2234 $mode_chnge .= " mode: $to_mode_str";
2237 $mode_chnge .= "]</span>\n";
2240 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2241 hash_base=>$hash, file_name=>$diff{'file'}),
2242 -class => "list"}, esc_path($diff{'file'}));
2244 print "<td>$mode_chnge</td>\n";
2245 print "<td class=\"link\">";
2246 if ($action eq 'commitdiff') {
2249 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2251 } elsif ($diff{'to_id'} ne $diff{'from_id'}) {
2252 # "commit" view and modified file (not onlu mode changed)
2253 print $cgi->a({-href => href(action=>"blobdiff",
2254 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
2255 hash_base=>$hash, hash_parent_base=>$parent,
2256 file_name=>$diff{'file'})},
2260 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2261 hash_base=>$hash, file_name=>$diff{'file'})},
2264 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2265 file_name=>$diff{'file'})},
2268 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2269 file_name=>$diff{'file'})},
2273 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
2274 my %status_name = ('R' => 'moved', 'C' => 'copied');
2275 my $nstatus = $status_name{$diff{'status'}};
2277 if ($diff{'from_mode'} != $diff{'to_mode'}) {
2278 # mode also for directories, so we cannot use $to_mode_str
2279 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2282 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2283 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
2284 -class => "list"}, esc_path($diff{'to_file'})) . "</td>\n" .
2285 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2286 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2287 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
2288 -class => "list"}, esc_path($diff{'from_file'})) .
2289 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2290 "<td class=\"link\">";
2291 if ($action eq 'commitdiff') {
2294 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2296 } elsif ($diff{'to_id'} ne $diff{'from_id'}) {
2297 # "commit" view and modified file (not only pure rename or copy)
2298 print $cgi->a({-href => href(action=>"blobdiff",
2299 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
2300 hash_base=>$hash, hash_parent_base=>$parent,
2301 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
2305 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2306 hash_base=>$parent, file_name=>$diff{'to_file'})},
2309 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2310 file_name=>$diff{'to_file'})},
2313 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2314 file_name=>$diff{'to_file'})},
2318 } # we should not encounter Unmerged (U) or Unknown (X) status
2324 sub git_patchset_body {
2325 my ($fd, $difftree, $hash, $hash_parent) = @_;
2331 my ($from_id, $to_id);
2333 print "<div class=\"patchset\">\n";
2335 # skip to first patch
2336 while ($patch_line = <$fd>) {
2339 last if ($patch_line =~ m/^diff /);
2343 while ($patch_line) {
2347 #assert($patch_line =~ m/^diff /) if DEBUG;
2348 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2349 push @diff_header, $patch_line;
2351 # extended diff header
2353 while ($patch_line = <$fd>) {
2356 last EXTENDED_HEADER if ($patch_line =~ m/^--- /);
2358 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2363 push @diff_header, $patch_line;
2365 #last PATCH unless $patch_line;
2366 my $last_patch_line = $patch_line;
2368 # check if current patch belong to current raw line
2369 # and parse raw git-diff line if needed
2370 if (defined $diffinfo &&
2371 $diffinfo->{'from_id'} eq $from_id &&
2372 $diffinfo->{'to_id'} eq $to_id) {
2373 # this is split patch
2374 print "<div class=\"patch cont\">\n";
2376 # advance raw git-diff output if needed
2377 $patch_idx++ if defined $diffinfo;
2379 # read and prepare patch information
2380 if (ref($difftree->[$patch_idx]) eq "HASH") {
2381 # pre-parsed (or generated by hand)
2382 $diffinfo = $difftree->[$patch_idx];
2384 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2386 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2387 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2388 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2389 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2390 hash=>$diffinfo->{'from_id'},
2391 file_name=>$from{'file'});
2393 if ($diffinfo->{'status'} ne "D") { # not deleted file
2394 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2395 hash=>$diffinfo->{'to_id'},
2396 file_name=>$to{'file'});
2398 # this is first patch for raw difftree line with $patch_idx index
2399 # we index @$difftree array from 0, but number patches from 1
2400 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2403 # print "git diff" header
2404 $patch_line = shift @diff_header;
2405 $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2406 if ($from{'href'}) {
2407 $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2408 'a/' . esc_path($from{'file'}));
2409 } else { # file was added
2410 $patch_line .= 'a/' . esc_path($from{'file'});
2414 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2415 'b/' . esc_path($to{'file'}));
2416 } else { # file was deleted
2417 $patch_line .= 'b/' . esc_path($to{'file'});
2419 print "<div class=\"diff header\">$patch_line</div>\n";
2421 # print extended diff header
2422 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
2424 foreach $patch_line (@diff_header) {
2426 if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2427 $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2428 esc_path($from{'file'}));
2430 if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2431 $patch_line = $cgi->a({-href=>$to{'href'}, -class=>"path"},
2432 esc_path($to{'file'}));
2435 if ($patch_line =~ m/\s(\d{6})$/) {
2436 $patch_line .= '<span class="info"> (' .
2437 file_type_long($1) .
2441 if ($patch_line =~ m/^index/) {
2442 my ($from_link, $to_link);
2443 if ($from{'href'}) {
2444 $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2445 substr($diffinfo->{'from_id'},0,7));
2447 $from_link = '0' x 7;
2450 $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2451 substr($diffinfo->{'to_id'},0,7));
2456 # my ($from_hash, $to_hash) =
2457 # ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/);
2458 # my ($from_id, $to_id) =
2459 # ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2460 # ($from_hash eq $from_id) && ($to_hash eq $to_id);
2462 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2463 $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2465 print $patch_line . "<br/>\n";
2467 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
2469 # from-file/to-file diff header
2470 $patch_line = $last_patch_line;
2471 #assert($patch_line =~ m/^---/) if DEBUG;
2472 if ($from{'href'}) {
2473 $patch_line = '--- a/' .
2474 $cgi->a({-href=>$from{'href'}, -class=>"path"},
2475 esc_path($from{'file'}));
2477 print "<div class=\"diff from_file\">$patch_line</div>\n";
2479 $patch_line = <$fd>;
2480 #last PATCH unless $patch_line;
2483 #assert($patch_line =~ m/^+++/) if DEBUG;
2485 $patch_line = '+++ b/' .
2486 $cgi->a({-href=>$to{'href'}, -class=>"path"},
2487 esc_path($to{'file'}));
2489 print "<div class=\"diff to_file\">$patch_line</div>\n";
2493 while ($patch_line = <$fd>) {
2496 next PATCH if ($patch_line =~ m/^diff /);
2498 print format_diff_line($patch_line, \%from, \%to);
2502 print "</div>\n"; # class="patch"
2505 print "</div>\n"; # class="patchset"
2508 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2510 sub git_project_list_body {
2511 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2513 my ($check_forks) = gitweb_check_feature('forks');
2516 foreach my $pr (@$projlist) {
2517 my (@aa) = git_get_last_activity($pr->{'path'});
2521 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2522 if (!defined $pr->{'descr'}) {
2523 my $descr = git_get_project_description($pr->{'path'}) || "";
2524 $pr->{'descr_long'} = to_utf8($descr);
2525 $pr->{'descr'} = chop_str($descr, 25, 5);
2527 if (!defined $pr->{'owner'}) {
2528 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2531 my $pname = $pr->{'path'};
2532 if (($pname =~ s/\.git$//) &&
2533 ($pname !~ /\/$/) &&
2534 (-d "$projectroot/$pname")) {
2535 $pr->{'forks'} = "-d $projectroot/$pname";
2541 push @projects, $pr;
2544 $order ||= "project";
2545 $from = 0 unless defined $from;
2546 $to = $#projects if (!defined $to || $#projects < $to);
2548 print "<table class=\"project_list\">\n";
2549 unless ($no_header) {
2552 print "<th></th>\n";
2554 if ($order eq "project") {
2555 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2556 print "<th>Project</th>\n";
2559 $cgi->a({-href => href(project=>undef, order=>'project'),
2560 -class => "header"}, "Project") .
2563 if ($order eq "descr") {
2564 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2565 print "<th>Description</th>\n";
2568 $cgi->a({-href => href(project=>undef, order=>'descr'),
2569 -class => "header"}, "Description") .
2572 if ($order eq "owner") {
2573 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2574 print "<th>Owner</th>\n";
2577 $cgi->a({-href => href(project=>undef, order=>'owner'),
2578 -class => "header"}, "Owner") .
2581 if ($order eq "age") {
2582 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2583 print "<th>Last Change</th>\n";
2586 $cgi->a({-href => href(project=>undef, order=>'age'),
2587 -class => "header"}, "Last Change") .
2590 print "<th></th>\n" .
2594 for (my $i = $from; $i <= $to; $i++) {
2595 my $pr = $projects[$i];
2597 print "<tr class=\"dark\">\n";
2599 print "<tr class=\"light\">\n";
2604 if ($pr->{'forks'}) {
2605 print "<!-- $pr->{'forks'} -->\n";
2606 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2610 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2611 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2612 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2613 -class => "list", -title => $pr->{'descr_long'}},
2614 esc_html($pr->{'descr'})) . "</td>\n" .
2615 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2616 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2617 $pr->{'age_string'} . "</td>\n" .
2618 "<td class=\"link\">" .
2619 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2620 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2621 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2622 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2623 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
2627 if (defined $extra) {
2630 print "<td></td>\n";
2632 print "<td colspan=\"5\">$extra</td>\n" .
2638 sub git_shortlog_body {
2639 # uses global variable $project
2640 my ($revlist, $from, $to, $refs, $extra) = @_;
2642 my $have_snapshot = gitweb_have_snapshot();
2644 $from = 0 unless defined $from;
2645 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2647 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2649 for (my $i = $from; $i <= $to; $i++) {
2650 my $commit = $revlist->[$i];
2651 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2652 my $ref = format_ref_marker($refs, $commit);
2653 my %co = parse_commit($commit);
2655 print "<tr class=\"dark\">\n";
2657 print "<tr class=\"light\">\n";
2660 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2661 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2662 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2664 print format_subject_html($co{'title'}, $co{'title_short'},
2665 href(action=>"commit", hash=>$commit), $ref);
2667 "<td class=\"link\">" .
2668 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2669 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2670 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2671 if ($have_snapshot) {
2672 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2677 if (defined $extra) {
2679 "<td colspan=\"4\">$extra</td>\n" .
2685 sub git_history_body {
2686 # Warning: assumes constant type (blob or tree) during history
2687 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2689 $from = 0 unless defined $from;
2690 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2692 print "<table class=\"history\" cellspacing=\"0\">\n";
2694 for (my $i = $from; $i <= $to; $i++) {
2695 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2700 my %co = parse_commit($commit);
2705 my $ref = format_ref_marker($refs, $commit);
2708 print "<tr class=\"dark\">\n";
2710 print "<tr class=\"light\">\n";
2713 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2714 # shortlog uses chop_str($co{'author_name'}, 10)
2715 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2717 # originally git_history used chop_str($co{'title'}, 50)
2718 print format_subject_html($co{'title'}, $co{'title_short'},
2719 href(action=>"commit", hash=>$commit), $ref);
2721 "<td class=\"link\">" .
2722 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2723 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2725 if ($ftype eq 'blob') {
2726 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2727 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2728 if (defined $blob_current && defined $blob_parent &&
2729 $blob_current ne $blob_parent) {
2731 $cgi->a({-href => href(action=>"blobdiff",
2732 hash=>$blob_current, hash_parent=>$blob_parent,
2733 hash_base=>$hash_base, hash_parent_base=>$commit,
2734 file_name=>$file_name)},
2741 if (defined $extra) {
2743 "<td colspan=\"4\">$extra</td>\n" .
2750 # uses global variable $project
2751 my ($taglist, $from, $to, $extra) = @_;
2752 $from = 0 unless defined $from;
2753 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2755 print "<table class=\"tags\" cellspacing=\"0\">\n";
2757 for (my $i = $from; $i <= $to; $i++) {
2758 my $entry = $taglist->[$i];
2760 my $comment = $tag{'subject'};
2762 if (defined $comment) {
2763 $comment_short = chop_str($comment, 30, 5);
2766 print "<tr class=\"dark\">\n";
2768 print "<tr class=\"light\">\n";
2771 print "<td><i>$tag{'age'}</i></td>\n" .
2773 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2774 -class => "list name"}, esc_html($tag{'name'})) .
2777 if (defined $comment) {
2778 print format_subject_html($comment, $comment_short,
2779 href(action=>"tag", hash=>$tag{'id'}));
2782 "<td class=\"selflink\">";
2783 if ($tag{'type'} eq "tag") {
2784 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2789 "<td class=\"link\">" . " | " .
2790 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2791 if ($tag{'reftype'} eq "commit") {
2792 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2793 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
2794 } elsif ($tag{'reftype'} eq "blob") {
2795 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2800 if (defined $extra) {
2802 "<td colspan=\"5\">$extra</td>\n" .
2808 sub git_heads_body {
2809 # uses global variable $project
2810 my ($headlist, $head, $from, $to, $extra) = @_;
2811 $from = 0 unless defined $from;
2812 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2814 print "<table class=\"heads\" cellspacing=\"0\">\n";
2816 for (my $i = $from; $i <= $to; $i++) {
2817 my $entry = $headlist->[$i];
2819 my $curr = $ref{'id'} eq $head;
2821 print "<tr class=\"dark\">\n";
2823 print "<tr class=\"light\">\n";
2826 print "<td><i>$ref{'age'}</i></td>\n" .
2827 ($curr ? "<td class=\"current_head\">" : "<td>") .
2828 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
2829 -class => "list name"},esc_html($ref{'name'})) .
2831 "<td class=\"link\">" .
2832 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
2833 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
2834 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
2838 if (defined $extra) {
2840 "<td colspan=\"3\">$extra</td>\n" .
2846 ## ======================================================================
2847 ## ======================================================================
2850 sub git_project_list {
2851 my $order = $cgi->param('o');
2852 if (defined $order && $order !~ m/project|descr|owner|age/) {
2853 die_error(undef, "Unknown order parameter");
2856 my @list = git_get_projects_list();
2858 die_error(undef, "No projects found");
2862 if (-f $home_text) {
2863 print "<div class=\"index_include\">\n";
2864 open (my $fd, $home_text);
2869 git_project_list_body(\@list, $order);
2874 my $order = $cgi->param('o');
2875 if (defined $order && $order !~ m/project|descr|owner|age/) {
2876 die_error(undef, "Unknown order parameter");
2879 my @list = git_get_projects_list($project);
2881 die_error(undef, "No forks found");
2885 git_print_page_nav('','');
2886 git_print_header_div('summary', "$project forks");
2887 git_project_list_body(\@list, $order);
2891 sub git_project_index {
2892 my @projects = git_get_projects_list($project);
2895 -type => 'text/plain',
2896 -charset => 'utf-8',
2897 -content_disposition => 'inline; filename="index.aux"');
2899 foreach my $pr (@projects) {
2900 if (!exists $pr->{'owner'}) {
2901 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2904 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2905 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2906 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2907 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2911 print "$path $owner\n";
2916 my $descr = git_get_project_description($project) || "none";
2917 my %co = parse_commit("HEAD");
2918 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2919 my $head = $co{'id'};
2921 my $owner = git_get_project_owner($project);
2923 my $refs = git_get_references();
2924 # These get_*_list functions return one more to allow us to see if
2925 # there are more ...
2926 my @taglist = git_get_tags_list(16);
2927 my @headlist = git_get_heads_list(16);
2929 my ($check_forks) = gitweb_check_feature('forks');
2932 @forklist = git_get_projects_list($project);
2936 git_print_page_nav('summary','', $head);
2938 print "<div class=\"title\"> </div>\n";
2939 print "<table cellspacing=\"0\">\n" .
2940 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2941 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2942 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2943 # use per project git URL list in $projectroot/$project/cloneurl
2944 # or make project git URL from git base URL and project name
2945 my $url_tag = "URL";
2946 my @url_list = git_get_project_url_list($project);
2947 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2948 foreach my $git_url (@url_list) {
2949 next unless $git_url;
2950 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2955 if (-s "$projectroot/$project/README.html") {
2956 if (open my $fd, "$projectroot/$project/README.html") {
2957 print "<div class=\"title\">readme</div>\n";
2958 print $_ while (<$fd>);
2963 # we need to request one more than 16 (0..15) to check if
2965 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2967 or die_error(undef, "Open git-rev-list failed");
2968 my @revlist = map { chomp; $_ } <$fd>;
2970 git_print_header_div('shortlog');
2971 git_shortlog_body(\@revlist, 0, 15, $refs,
2972 $#revlist <= 15 ? undef :
2973 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2976 git_print_header_div('tags');
2977 git_tags_body(\@taglist, 0, 15,
2978 $#taglist <= 15 ? undef :
2979 $cgi->a({-href => href(action=>"tags")}, "..."));
2983 git_print_header_div('heads');
2984 git_heads_body(\@headlist, $head, 0, 15,
2985 $#headlist <= 15 ? undef :
2986 $cgi->a({-href => href(action=>"heads")}, "..."));
2990 git_print_header_div('forks');
2991 git_project_list_body(\@forklist, undef, 0, 15,
2992 $#forklist <= 15 ? undef :
2993 $cgi->a({-href => href(action=>"forks")}, "..."),
3001 my $head = git_get_head_hash($project);
3003 git_print_page_nav('','', $head,undef,$head);
3004 my %tag = parse_tag($hash);
3005 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3006 print "<div class=\"title_text\">\n" .
3007 "<table cellspacing=\"0\">\n" .
3009 "<td>object</td>\n" .
3010 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3011 $tag{'object'}) . "</td>\n" .
3012 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3013 $tag{'type'}) . "</td>\n" .
3015 if (defined($tag{'author'})) {
3016 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3017 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3018 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3019 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3022 print "</table>\n\n" .
3024 print "<div class=\"page_body\">";
3025 my $comment = $tag{'comment'};
3026 foreach my $line (@$comment) {
3028 print esc_html($line, -nbsp=>1) . "<br/>\n";
3038 my ($have_blame) = gitweb_check_feature('blame');
3040 die_error('403 Permission denied', "Permission denied");
3042 die_error('404 Not Found', "File name not defined") if (!$file_name);
3043 $hash_base ||= git_get_head_hash($project);
3044 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3045 my %co = parse_commit($hash_base)
3046 or die_error(undef, "Reading commit failed");
3047 if (!defined $hash) {
3048 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3049 or die_error(undef, "Error looking up file");
3051 $ftype = git_get_type($hash);
3052 if ($ftype !~ "blob") {
3053 die_error("400 Bad Request", "Object is not a blob");
3055 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3056 $file_name, $hash_base)
3057 or die_error(undef, "Open git-blame failed");
3060 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3063 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3066 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3068 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3069 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3070 git_print_page_path($file_name, $ftype, $hash_base);
3071 my @rev_color = (qw(light2 dark2));
3072 my $num_colors = scalar(@rev_color);
3073 my $current_color = 0;
3076 <div class="page_body">
3077 <table class="blame">
3078 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3083 last unless defined $_;
3084 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3085 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3086 if (!exists $metainfo{$full_rev}) {
3087 $metainfo{$full_rev} = {};
3089 my $meta = $metainfo{$full_rev};
3092 if (/^(\S+) (.*)$/) {
3098 my $rev = substr($full_rev, 0, 8);
3099 my $author = $meta->{'author'};
3100 my %date = parse_date($meta->{'author-time'},
3101 $meta->{'author-tz'});
3102 my $date = $date{'iso-tz'};
3104 $current_color = ++$current_color % $num_colors;
3106 print "<tr class=\"$rev_color[$current_color]\">\n";
3108 print "<td class=\"sha1\"";
3109 print " title=\"". esc_html($author) . ", $date\"";
3110 print " rowspan=\"$group_size\"" if ($group_size > 1);
3112 print $cgi->a({-href => href(action=>"commit",
3114 file_name=>$file_name)},
3118 my $blamed = href(action => 'blame',
3119 file_name => $meta->{'filename'},
3120 hash_base => $full_rev);
3121 print "<td class=\"linenr\">";
3122 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3124 -class => "linenr" },
3127 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3133 or print "Reading blob failed\n";
3140 my ($have_blame) = gitweb_check_feature('blame');
3142 die_error('403 Permission denied', "Permission denied");
3144 die_error('404 Not Found', "File name not defined") if (!$file_name);
3145 $hash_base ||= git_get_head_hash($project);
3146 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3147 my %co = parse_commit($hash_base)
3148 or die_error(undef, "Reading commit failed");
3149 if (!defined $hash) {
3150 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3151 or die_error(undef, "Error lookup file");
3153 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3154 or die_error(undef, "Open git-annotate failed");
3157 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3160 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3163 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3165 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3166 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3167 git_print_page_path($file_name, 'blob', $hash_base);
3168 print "<div class=\"page_body\">\n";
3170 <table class="blame">
3179 my @line_class = (qw(light dark));
3180 my $line_class_len = scalar (@line_class);
3181 my $line_class_num = $#line_class;
3182 while (my $line = <$fd>) {
3194 $line_class_num = ($line_class_num + 1) % $line_class_len;
3196 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3203 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3206 $short_rev = substr ($long_rev, 0, 8);
3207 $age = time () - $time;
3208 $age_str = age_string ($age);
3209 $age_str =~ s/ / /g;
3210 $age_class = age_class($age);
3211 $author = esc_html ($author);
3212 $author =~ s/ / /g;
3214 $data = untabify($data);
3215 $data = esc_html ($data);
3218 <tr class="$line_class[$line_class_num]">
3219 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3220 <td class="$age_class">$age_str</td>
3222 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3223 <td class="pre">$data</td>
3226 } # while (my $line = <$fd>)
3227 print "</table>\n\n";
3229 or print "Reading blob failed.\n";
3235 my $head = git_get_head_hash($project);
3237 git_print_page_nav('','', $head,undef,$head);
3238 git_print_header_div('summary', $project);
3240 my @tagslist = git_get_tags_list();
3242 git_tags_body(\@tagslist);
3248 my $head = git_get_head_hash($project);
3250 git_print_page_nav('','', $head,undef,$head);
3251 git_print_header_div('summary', $project);
3253 my @headslist = git_get_heads_list();
3255 git_heads_body(\@headslist, $head);
3260 sub git_blob_plain {
3263 if (!defined $hash) {
3264 if (defined $file_name) {
3265 my $base = $hash_base || git_get_head_hash($project);
3266 $hash = git_get_hash_by_path($base, $file_name, "blob")
3267 or die_error(undef, "Error lookup file");
3269 die_error(undef, "No file name defined");
3271 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3272 # blobs defined by non-textual hash id's can be cached
3277 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3278 or die_error(undef, "Couldn't cat $file_name, $hash");
3280 $type ||= blob_mimetype($fd, $file_name);
3282 # save as filename, even when no $file_name is given
3283 my $save_as = "$hash";
3284 if (defined $file_name) {
3285 $save_as = $file_name;
3286 } elsif ($type =~ m/^text\//) {
3293 -content_disposition => 'inline; filename="' . "$save_as" . '"');
3295 binmode STDOUT, ':raw';
3297 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3305 if (!defined $hash) {
3306 if (defined $file_name) {
3307 my $base = $hash_base || git_get_head_hash($project);
3308 $hash = git_get_hash_by_path($base, $file_name, "blob")
3309 or die_error(undef, "Error lookup file");
3311 die_error(undef, "No file name defined");
3313 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3314 # blobs defined by non-textual hash id's can be cached
3318 my ($have_blame) = gitweb_check_feature('blame');
3319 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3320 or die_error(undef, "Couldn't cat $file_name, $hash");
3321 my $mimetype = blob_mimetype($fd, $file_name);
3322 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
3324 return git_blob_plain($mimetype);
3326 # we can have blame only for text/* mimetype
3327 $have_blame &&= ($mimetype =~ m!^text/!);
3329 git_header_html(undef, $expires);
3330 my $formats_nav = '';
3331 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3332 if (defined $file_name) {
3335 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3336 hash=>$hash, file_name=>$file_name)},
3341 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3342 hash=>$hash, file_name=>$file_name)},
3345 $cgi->a({-href => href(action=>"blob_plain",
3346 hash=>$hash, file_name=>$file_name)},
3349 $cgi->a({-href => href(action=>"blob",
3350 hash_base=>"HEAD", file_name=>$file_name)},
3354 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3356 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3357 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3359 print "<div class=\"page_nav\">\n" .
3360 "<br/><br/></div>\n" .
3361 "<div class=\"title\">$hash</div>\n";
3363 git_print_page_path($file_name, "blob", $hash_base);
3364 print "<div class=\"page_body\">\n";
3365 if ($mimetype =~ m!^text/!) {
3367 while (my $line = <$fd>) {
3370 $line = untabify($line);
3371 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3372 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3374 } elsif ($mimetype =~ m!^image/!) {
3375 print qq!<img type="$mimetype"!;
3377 print qq! alt="$file_name" title="$file_name"!;
3380 href(action=>"blob_plain", hash=>$hash,
3381 hash_base=>$hash_base, file_name=>$file_name) .
3385 or print "Reading blob failed.\n";
3391 my $have_snapshot = gitweb_have_snapshot();
3393 if (!defined $hash_base) {
3394 $hash_base = "HEAD";
3396 if (!defined $hash) {
3397 if (defined $file_name) {
3398 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
3404 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3405 or die_error(undef, "Open git-ls-tree failed");
3406 my @entries = map { chomp; $_ } <$fd>;
3407 close $fd or die_error(undef, "Reading tree failed");
3410 my $refs = git_get_references();
3411 my $ref = format_ref_marker($refs, $hash_base);
3414 my ($have_blame) = gitweb_check_feature('blame');
3415 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3417 if (defined $file_name) {
3419 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3420 hash=>$hash, file_name=>$file_name)},
3422 $cgi->a({-href => href(action=>"tree",
3423 hash_base=>"HEAD", file_name=>$file_name)},
3426 if ($have_snapshot) {
3427 # FIXME: Should be available when we have no hash base as well.
3429 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3432 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3433 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3436 print "<div class=\"page_nav\">\n";
3437 print "<br/><br/></div>\n";
3438 print "<div class=\"title\">$hash</div>\n";
3440 if (defined $file_name) {
3441 $basedir = $file_name;
3442 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3446 git_print_page_path($file_name, 'tree', $hash_base);
3447 print "<div class=\"page_body\">\n";
3448 print "<table cellspacing=\"0\">\n";
3450 # '..' (top directory) link if possible
3451 if (defined $hash_base &&
3452 defined $file_name && $file_name =~ m![^/]+$!) {
3454 print "<tr class=\"dark\">\n";
3456 print "<tr class=\"light\">\n";
3460 my $up = $file_name;
3461 $up =~ s!/?[^/]+$!!;
3462 undef $up unless $up;
3463 # based on git_print_tree_entry
3464 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3465 print '<td class="list">';
3466 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3470 print "<td class=\"link\"></td>\n";
3474 foreach my $line (@entries) {
3475 my %t = parse_ls_tree_line($line, -z => 1);
3478 print "<tr class=\"dark\">\n";
3480 print "<tr class=\"light\">\n";
3484 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3488 print "</table>\n" .
3494 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3495 my $have_snapshot = (defined $ctype && defined $suffix);
3496 if (!$have_snapshot) {
3497 die_error('403 Permission denied', "Permission denied");
3500 if (!defined $hash) {
3501 $hash = git_get_head_hash($project);
3504 my $filename = basename($project) . "-$hash.tar.$suffix";
3507 -type => "application/$ctype",
3508 -content_disposition => 'inline; filename="' . "$filename" . '"',
3509 -status => '200 OK');
3511 my $git = git_cmd_str();
3512 my $name = $project;
3513 $name =~ s/\047/\047\\\047\047/g;
3515 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3516 or die_error(undef, "Execute git-tar-tree failed.");
3517 binmode STDOUT, ':raw';
3519 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3525 my $head = git_get_head_hash($project);
3526 if (!defined $hash) {
3529 if (!defined $page) {
3532 my $refs = git_get_references();
3534 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3535 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash, "--"
3536 or die_error(undef, "Open git-rev-list failed");
3537 my @revlist = map { chomp; $_ } <$fd>;
3540 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
3543 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3546 my %co = parse_commit($hash);
3548 git_print_header_div('summary', $project);
3549 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3551 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
3552 my $commit = $revlist[$i];
3553 my $ref = format_ref_marker($refs, $commit);
3554 my %co = parse_commit($commit);
3556 my %ad = parse_date($co{'author_epoch'});
3557 git_print_header_div('commit',
3558 "<span class=\"age\">$co{'age_string'}</span>" .
3559 esc_html($co{'title'}) . $ref,
3561 print "<div class=\"title_text\">\n" .
3562 "<div class=\"log_link\">\n" .
3563 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3565 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3567 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3570 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3573 print "<div class=\"log_body\">\n";
3574 git_print_log($co{'comment'}, -final_empty_line=> 1);
3581 $hash ||= $hash_base || "HEAD";
3582 my %co = parse_commit($hash);
3584 die_error(undef, "Unknown commit object");
3586 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3587 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3589 my $parent = $co{'parent'};
3590 my $parents = $co{'parents'}; # listref
3592 # we need to prepare $formats_nav before any parameter munging
3594 if (!defined $parent) {
3596 $formats_nav .= '(initial)';
3597 } elsif (@$parents == 1) {
3598 # single parent commit
3601 $cgi->a({-href => href(action=>"commit",
3603 esc_html(substr($parent, 0, 7))) .
3610 $cgi->a({-href => href(action=>"commitdiff",
3612 esc_html(substr($_, 0, 7)));
3617 if (!defined $parent) {
3621 if (@$parents <= 1) {
3622 # difftree output is not printed for merges
3623 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
3624 @diff_opts, $parent, $hash, "--"
3625 or die_error(undef, "Open git-diff-tree failed");
3626 @difftree = map { chomp; $_ } <$fd>;
3627 close $fd or die_error(undef, "Reading git-diff-tree failed");
3630 # non-textual hash id's can be cached
3632 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3635 my $refs = git_get_references();
3636 my $ref = format_ref_marker($refs, $co{'id'});
3638 my $have_snapshot = gitweb_have_snapshot();
3640 git_header_html(undef, $expires);
3641 git_print_page_nav('commit', '',
3642 $hash, $co{'tree'}, $hash,
3645 if (defined $co{'parent'}) {
3646 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3648 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3650 print "<div class=\"title_text\">\n" .
3651 "<table cellspacing=\"0\">\n";
3652 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3654 "<td></td><td> $ad{'rfc2822'}";
3655 if ($ad{'hour_local'} < 6) {
3656 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3657 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3659 printf(" (%02d:%02d %s)",
3660 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3664 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3665 print "<tr><td></td><td> $cd{'rfc2822'}" .
3666 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3668 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3671 "<td class=\"sha1\">" .
3672 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3673 class => "list"}, $co{'tree'}) .
3675 "<td class=\"link\">" .
3676 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3678 if ($have_snapshot) {
3680 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3685 foreach my $par (@$parents) {
3688 "<td class=\"sha1\">" .
3689 $cgi->a({-href => href(action=>"commit", hash=>$par),
3690 class => "list"}, $par) .
3692 "<td class=\"link\">" .
3693 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3695 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3702 print "<div class=\"page_body\">\n";
3703 git_print_log($co{'comment'});
3706 if (@$parents <= 1) {
3707 # do not output difftree/whatchanged for merges
3708 git_difftree_body(\@difftree, $hash, $parent);
3715 # object is defined by:
3716 # - hash or hash_base alone
3717 # - hash_base and file_name
3720 # - hash or hash_base alone
3721 if ($hash || ($hash_base && !defined $file_name)) {
3722 my $object_id = $hash || $hash_base;
3724 my $git_command = git_cmd_str();
3725 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
3726 or die_error('404 Not Found', "Object does not exist");
3730 or die_error('404 Not Found', "Object does not exist");
3732 # - hash_base and file_name
3733 } elsif ($hash_base && defined $file_name) {
3734 $file_name =~ s,/+$,,;
3736 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
3737 or die_error('404 Not Found', "Base object does not exist");
3739 # here errors should not hapen
3740 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
3741 or die_error(undef, "Open git-ls-tree failed");
3745 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3746 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
3747 die_error('404 Not Found', "File or directory for given base does not exist");
3752 die_error('404 Not Found', "Not enough information to find object");
3755 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
3756 hash=>$hash, hash_base=>$hash_base,
3757 file_name=>$file_name),
3758 -status => '302 Found');
3762 my $format = shift || 'html';
3769 # preparing $fd and %diffinfo for git_patchset_body
3771 if (defined $hash_base && defined $hash_parent_base) {
3772 if (defined $file_name) {
3774 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3775 $hash_parent_base, $hash_base,
3777 or die_error(undef, "Open git-diff-tree failed");
3778 @difftree = map { chomp; $_ } <$fd>;
3780 or die_error(undef, "Reading git-diff-tree failed");
3782 or die_error('404 Not Found', "Blob diff not found");
3784 } elsif (defined $hash &&
3785 $hash =~ /[0-9a-fA-F]{40}/) {
3786 # try to find filename from $hash
3788 # read filtered raw output
3789 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3790 $hash_parent_base, $hash_base, "--"
3791 or die_error(undef, "Open git-diff-tree failed");
3793 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3795 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3796 map { chomp; $_ } <$fd>;
3798 or die_error(undef, "Reading git-diff-tree failed");
3800 or die_error('404 Not Found', "Blob diff not found");
3803 die_error('404 Not Found', "Missing one of the blob diff parameters");
3806 if (@difftree > 1) {
3807 die_error('404 Not Found', "Ambiguous blob diff specification");
3810 %diffinfo = parse_difftree_raw_line($difftree[0]);
3811 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3812 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3814 $hash_parent ||= $diffinfo{'from_id'};
3815 $hash ||= $diffinfo{'to_id'};
3817 # non-textual hash id's can be cached
3818 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3819 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3824 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3825 '-p', $hash_parent_base, $hash_base,
3827 or die_error(undef, "Open git-diff-tree failed");
3830 # old/legacy style URI
3831 if (!%diffinfo && # if new style URI failed
3832 defined $hash && defined $hash_parent) {
3833 # fake git-diff-tree raw output
3834 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3835 $diffinfo{'from_id'} = $hash_parent;
3836 $diffinfo{'to_id'} = $hash;
3837 if (defined $file_name) {
3838 if (defined $file_parent) {
3839 $diffinfo{'status'} = '2';
3840 $diffinfo{'from_file'} = $file_parent;
3841 $diffinfo{'to_file'} = $file_name;
3842 } else { # assume not renamed
3843 $diffinfo{'status'} = '1';
3844 $diffinfo{'from_file'} = $file_name;
3845 $diffinfo{'to_file'} = $file_name;
3847 } else { # no filename given
3848 $diffinfo{'status'} = '2';
3849 $diffinfo{'from_file'} = $hash_parent;
3850 $diffinfo{'to_file'} = $hash;
3853 # non-textual hash id's can be cached
3854 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3855 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3860 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts,
3861 $hash_parent, $hash, "--"
3862 or die_error(undef, "Open git-diff failed");
3864 die_error('404 Not Found', "Missing one of the blob diff parameters")
3869 if ($format eq 'html') {
3871 $cgi->a({-href => href(action=>"blobdiff_plain",
3872 hash=>$hash, hash_parent=>$hash_parent,
3873 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3874 file_name=>$file_name, file_parent=>$file_parent)},
3876 git_header_html(undef, $expires);
3877 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3878 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3879 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3881 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3882 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3884 if (defined $file_name) {
3885 git_print_page_path($file_name, "blob", $hash_base);
3887 print "<div class=\"page_path\"></div>\n";
3890 } elsif ($format eq 'plain') {
3892 -type => 'text/plain',
3893 -charset => 'utf-8',
3894 -expires => $expires,
3895 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3897 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3900 die_error(undef, "Unknown blobdiff format");
3904 if ($format eq 'html') {
3905 print "<div class=\"page_body\">\n";
3907 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3910 print "</div>\n"; # class="page_body"
3914 while (my $line = <$fd>) {
3915 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
3916 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
3920 last if $line =~ m!^\+\+\+!;
3928 sub git_blobdiff_plain {
3929 git_blobdiff('plain');
3932 sub git_commitdiff {
3933 my $format = shift || 'html';
3934 $hash ||= $hash_base || "HEAD";
3935 my %co = parse_commit($hash);
3937 die_error(undef, "Unknown commit object");
3940 # we need to prepare $formats_nav before any parameter munging
3942 if ($format eq 'html') {
3944 $cgi->a({-href => href(action=>"commitdiff_plain",
3945 hash=>$hash, hash_parent=>$hash_parent)},
3948 if (defined $hash_parent) {
3949 # commitdiff with two commits given
3950 my $hash_parent_short = $hash_parent;
3951 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3952 $hash_parent_short = substr($hash_parent, 0, 7);
3956 $cgi->a({-href => href(action=>"commitdiff",
3957 hash=>$hash_parent)},
3958 esc_html($hash_parent_short)) .
3960 } elsif (!$co{'parent'}) {
3962 $formats_nav .= ' (initial)';
3963 } elsif (scalar @{$co{'parents'}} == 1) {
3964 # single parent commit
3967 $cgi->a({-href => href(action=>"commitdiff",
3968 hash=>$co{'parent'})},
3969 esc_html(substr($co{'parent'}, 0, 7))) .
3976 $cgi->a({-href => href(action=>"commitdiff",
3978 esc_html(substr($_, 0, 7)));
3979 } @{$co{'parents'}} ) .
3984 if (!defined $hash_parent) {
3985 $hash_parent = $co{'parent'} || '--root';
3991 if ($format eq 'html') {
3992 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3993 "--no-commit-id", "--patch-with-raw", "--full-index",
3994 $hash_parent, $hash, "--"
3995 or die_error(undef, "Open git-diff-tree failed");
3997 while (my $line = <$fd>) {
3999 # empty line ends raw part of diff-tree output
4001 push @difftree, $line;
4004 } elsif ($format eq 'plain') {
4005 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4006 '-p', $hash_parent, $hash, "--"
4007 or die_error(undef, "Open git-diff-tree failed");
4010 die_error(undef, "Unknown commitdiff format");
4013 # non-textual hash id's can be cached
4015 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4019 # write commit message
4020 if ($format eq 'html') {
4021 my $refs = git_get_references();
4022 my $ref = format_ref_marker($refs, $co{'id'});
4024 git_header_html(undef, $expires);
4025 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4026 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4027 git_print_authorship(\%co);
4028 print "<div class=\"page_body\">\n";
4029 if (@{$co{'comment'}} > 1) {
4030 print "<div class=\"log\">\n";
4031 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4032 print "</div>\n"; # class="log"
4035 } elsif ($format eq 'plain') {
4036 my $refs = git_get_references("tags");
4037 my $tagname = git_get_rev_name_tags($hash);
4038 my $filename = basename($project) . "-$hash.patch";
4041 -type => 'text/plain',
4042 -charset => 'utf-8',
4043 -expires => $expires,
4044 -content_disposition => 'inline; filename="' . "$filename" . '"');
4045 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4048 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4049 Subject: $co{'title'}
4051 print "X-Git-Tag: $tagname\n" if $tagname;
4052 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4054 foreach my $line (@{$co{'comment'}}) {
4061 if ($format eq 'html') {
4062 git_difftree_body(\@difftree, $hash, $hash_parent);
4065 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
4067 print "</div>\n"; # class="page_body"
4070 } elsif ($format eq 'plain') {
4074 or print "Reading git-diff-tree failed\n";
4078 sub git_commitdiff_plain {
4079 git_commitdiff('plain');
4083 if (!defined $hash_base) {
4084 $hash_base = git_get_head_hash($project);
4086 if (!defined $page) {
4090 my %co = parse_commit($hash_base);
4092 die_error(undef, "Unknown commit object");
4095 my $refs = git_get_references();
4096 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4098 if (!defined $hash && defined $file_name) {
4099 $hash = git_get_hash_by_path($hash_base, $file_name);
4101 if (defined $hash) {
4102 $ftype = git_get_type($hash);
4106 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
4107 or die_error(undef, "Open git-rev-list-failed");
4108 my @revlist = map { chomp; $_ } <$fd>;
4110 or die_error(undef, "Reading git-rev-list failed");
4112 my $paging_nav = '';
4115 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4116 file_name=>$file_name)},
4118 $paging_nav .= " ⋅ " .
4119 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4120 file_name=>$file_name, page=>$page-1),
4121 -accesskey => "p", -title => "Alt-p"}, "prev");
4123 $paging_nav .= "first";
4124 $paging_nav .= " ⋅ prev";
4126 if ($#revlist >= (100 * ($page+1)-1)) {
4127 $paging_nav .= " ⋅ " .
4128 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4129 file_name=>$file_name, page=>$page+1),
4130 -accesskey => "n", -title => "Alt-n"}, "next");
4132 $paging_nav .= " ⋅ next";
4135 if ($#revlist >= (100 * ($page+1)-1)) {
4137 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4138 file_name=>$file_name, page=>$page+1),
4139 -title => "Alt-n"}, "next");
4143 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4144 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4145 git_print_page_path($file_name, $ftype, $hash_base);
4147 git_history_body(\@revlist, ($page * 100), $#revlist,
4148 $refs, $hash_base, $ftype, $next_link);
4154 if (!defined $searchtext) {
4155 die_error(undef, "Text field empty");
4157 if (!defined $hash) {
4158 $hash = git_get_head_hash($project);
4160 my %co = parse_commit($hash);
4162 die_error(undef, "Unknown commit object");
4165 $searchtype ||= 'commit';
4166 if ($searchtype eq 'pickaxe') {
4167 # pickaxe may take all resources of your box and run for several minutes
4168 # with every query - so decide by yourself how public you make this feature
4169 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4170 if (!$have_pickaxe) {
4171 die_error('403 Permission denied', "Permission denied");
4176 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4177 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4179 print "<table cellspacing=\"0\">\n";
4181 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4183 if ($searchtype eq 'commit') {
4184 $greptype = "--grep=";
4185 } elsif ($searchtype eq 'author') {
4186 $greptype = "--author=";
4187 } elsif ($searchtype eq 'committer') {
4188 $greptype = "--committer=";
4191 open my $fd, "-|", git_cmd(), "rev-list",
4192 "--header", "--parents", ($greptype . $searchtext),
4195 while (my $commit_text = <$fd>) {
4196 my @commit_lines = split "\n", $commit_text;
4197 my %co = parse_commit(undef, \@commit_lines);
4202 print "<tr class=\"dark\">\n";
4204 print "<tr class=\"light\">\n";
4207 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4208 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4210 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
4211 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4212 my $comment = $co{'comment'};
4213 foreach my $line (@$comment) {
4214 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
4215 my $lead = esc_html($1) || "";
4216 $lead = chop_str($lead, 30, 10);
4217 my $match = esc_html($2) || "";
4218 my $trail = esc_html($3) || "";
4219 $trail = chop_str($trail, 30, 10);
4220 my $text = "$lead<span class=\"match\">$match</span>$trail";
4221 print chop_str($text, 80, 5) . "<br/>\n";
4225 "<td class=\"link\">" .
4226 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4228 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4235 if ($searchtype eq 'pickaxe') {
4237 my $git_command = git_cmd_str();
4238 open my $fd, "-|", "$git_command rev-list $hash | " .
4239 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
4242 while (my $line = <$fd>) {
4243 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
4246 $set{'from_id'} = $3;
4248 $set{'id'} = $set{'to_id'};
4249 if ($set{'id'} =~ m/0{40}/) {
4250 $set{'id'} = $set{'from_id'};
4252 if ($set{'id'} =~ m/0{40}/) {
4256 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
4259 print "<tr class=\"dark\">\n";
4261 print "<tr class=\"light\">\n";
4264 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4265 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4267 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4268 -class => "list subject"},
4269 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4270 while (my $setref = shift @files) {
4272 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
4273 hash=>$set{'id'}, file_name=>$set{'file'}),
4275 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
4279 "<td class=\"link\">" .
4280 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4282 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4286 %co = parse_commit($1);
4295 sub git_search_help {
4297 git_print_page_nav('','', $hash,$hash,$hash);
4300 <dt><b>commit</b></dt>
4301 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
4302 <dt><b>author</b></dt>
4303 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
4304 <dt><b>committer</b></dt>
4305 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
4307 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4308 if ($have_pickaxe) {
4310 <dt><b>pickaxe</b></dt>
4311 <dd>All commits that caused the string to appear or disappear from any file (changes that
4312 added, removed or "modified" the string) will be listed. This search can take a while and
4313 takes a lot of strain on the server, so please use it wisely.</dd>
4321 my $head = git_get_head_hash($project);
4322 if (!defined $hash) {
4325 if (!defined $page) {
4328 my $refs = git_get_references();
4330 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4331 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash, "--"
4332 or die_error(undef, "Open git-rev-list failed");
4333 my @revlist = map { chomp; $_ } <$fd>;
4336 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
4338 if ($#revlist >= (100 * ($page+1)-1)) {
4340 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
4341 -title => "Alt-n"}, "next");
4346 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
4347 git_print_header_div('summary', $project);
4349 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
4354 ## ......................................................................
4355 ## feeds (RSS, Atom; OPML)
4358 my $format = shift || 'atom';
4359 my ($have_blame) = gitweb_check_feature('blame');
4361 # Atom: http://www.atomenabled.org/developers/syndication/
4362 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
4363 if ($format ne 'rss' && $format ne 'atom') {
4364 die_error(undef, "Unknown web feed format");
4367 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
4368 my $head = $hash || 'HEAD';
4369 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150",
4370 $head, "--", (defined $file_name ? $file_name : ())
4371 or die_error(undef, "Open git-rev-list failed");
4372 my @revlist = map { chomp; $_ } <$fd>;
4373 close $fd or die_error(undef, "Reading git-rev-list failed");
4377 my $content_type = "application/$format+xml";
4378 if (defined $cgi->http('HTTP_ACCEPT') &&
4379 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
4380 # browser (feed reader) prefers text/xml
4381 $content_type = 'text/xml';
4383 if (defined($revlist[0])) {
4384 %latest_commit = parse_commit($revlist[0]);
4385 %latest_date = parse_date($latest_commit{'author_epoch'});
4387 -type => $content_type,
4388 -charset => 'utf-8',
4389 -last_modified => $latest_date{'rfc2822'});
4392 -type => $content_type,
4393 -charset => 'utf-8');
4396 # Optimization: skip generating the body if client asks only
4397 # for Last-Modified date.
4398 return if ($cgi->request_method() eq 'HEAD');
4401 my $title = "$site_name - $project/$action";
4402 my $feed_type = 'log';
4403 if (defined $hash) {
4404 $title .= " - '$hash'";
4405 $feed_type = 'branch log';
4406 if (defined $file_name) {
4407 $title .= " :: $file_name";
4408 $feed_type = 'history';
4410 } elsif (defined $file_name) {
4411 $title .= " - $file_name";
4412 $feed_type = 'history';
4414 $title .= " $feed_type";
4415 my $descr = git_get_project_description($project);
4416 if (defined $descr) {
4417 $descr = esc_html($descr);
4419 $descr = "$project " .
4420 ($format eq 'rss' ? 'RSS' : 'Atom') .
4423 my $owner = git_get_project_owner($project);
4424 $owner = esc_html($owner);
4428 if (defined $file_name) {
4429 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
4430 } elsif (defined $hash) {
4431 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
4433 $alt_url = href(-full=>1, action=>"summary");
4435 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
4436 if ($format eq 'rss') {
4438 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
4441 print "<title>$title</title>\n" .
4442 "<link>$alt_url</link>\n" .
4443 "<description>$descr</description>\n" .
4444 "<language>en</language>\n";
4445 } elsif ($format eq 'atom') {
4447 <feed xmlns="http://www.w3.org/2005/Atom">
4449 print "<title>$title</title>\n" .
4450 "<subtitle>$descr</subtitle>\n" .
4451 '<link rel="alternate" type="text/html" href="' .
4452 $alt_url . '" />' . "\n" .
4453 '<link rel="self" type="' . $content_type . '" href="' .
4454 $cgi->self_url() . '" />' . "\n" .
4455 "<id>" . href(-full=>1) . "</id>\n" .
4456 # use project owner for feed author
4457 "<author><name>$owner</name></author>\n";
4458 if (defined $favicon) {
4459 print "<icon>" . esc_url($favicon) . "</icon>\n";
4461 if (defined $logo_url) {
4462 # not twice as wide as tall: 72 x 27 pixels
4463 print "<logo>" . esc_url($logo) . "</logo>\n";
4465 if (! %latest_date) {
4466 # dummy date to keep the feed valid until commits trickle in:
4467 print "<updated>1970-01-01T00:00:00Z</updated>\n";
4469 print "<updated>$latest_date{'iso-8601'}</updated>\n";
4474 for (my $i = 0; $i <= $#revlist; $i++) {
4475 my $commit = $revlist[$i];
4476 my %co = parse_commit($commit);
4477 # we read 150, we always show 30 and the ones more recent than 48 hours
4478 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
4481 my %cd = parse_date($co{'author_epoch'});
4483 # get list of changed files
4484 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4485 $co{'parent'}, $co{'id'}, "--", (defined $file_name ? $file_name : ())
4487 my @difftree = map { chomp; $_ } <$fd>;
4491 # print element (entry, item)
4492 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
4493 if ($format eq 'rss') {
4495 "<title>" . esc_html($co{'title'}) . "</title>\n" .
4496 "<author>" . esc_html($co{'author'}) . "</author>\n" .
4497 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
4498 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
4499 "<link>$co_url</link>\n" .
4500 "<description>" . esc_html($co{'title'}) . "</description>\n" .
4501 "<content:encoded>" .
4503 } elsif ($format eq 'atom') {
4505 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
4506 "<updated>$cd{'iso-8601'}</updated>\n" .
4508 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
4509 if ($co{'author_email'}) {
4510 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
4512 print "</author>\n" .
4513 # use committer for contributor
4515 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
4516 if ($co{'committer_email'}) {
4517 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
4519 print "</contributor>\n" .
4520 "<published>$cd{'iso-8601'}</published>\n" .
4521 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
4522 "<id>$co_url</id>\n" .
4523 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
4524 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
4526 my $comment = $co{'comment'};
4528 foreach my $line (@$comment) {
4529 $line = esc_html($line);
4532 print "</pre><ul>\n";
4533 foreach my $difftree_line (@difftree) {
4534 my %difftree = parse_difftree_raw_line($difftree_line);
4535 next if !$difftree{'from_id'};
4537 my $file = $difftree{'file'} || $difftree{'to_file'};
4541 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
4542 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
4543 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
4544 file_name=>$file, file_parent=>$difftree{'from_file'}),
4545 -title => "diff"}, 'D');
4547 print $cgi->a({-href => href(-full=>1, action=>"blame",
4548 file_name=>$file, hash_base=>$commit),
4549 -title => "blame"}, 'B');
4551 # if this is not a feed of a file history
4552 if (!defined $file_name || $file_name ne $file) {
4553 print $cgi->a({-href => href(-full=>1, action=>"history",
4554 file_name=>$file, hash=>$commit),
4555 -title => "history"}, 'H');
4557 $file = esc_path($file);
4561 if ($format eq 'rss') {
4562 print "</ul>]]>\n" .
4563 "</content:encoded>\n" .
4565 } elsif ($format eq 'atom') {
4566 print "</ul>\n</div>\n" .
4573 if ($format eq 'rss') {
4574 print "</channel>\n</rss>\n";
4575 } elsif ($format eq 'atom') {
4589 my @list = git_get_projects_list();
4591 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
4593 <?xml version="1.0" encoding="utf-8"?>
4594 <opml version="1.0">
4596 <title>$site_name OPML Export</title>
4599 <outline text="git RSS feeds">
4602 foreach my $pr (@list) {
4604 my $head = git_get_head_hash($proj{'path'});
4605 if (!defined $head) {
4608 $git_dir = "$projectroot/$proj{'path'}";
4609 my %co = parse_commit($head);
4614 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
4615 my $rss = "$my_url?p=$proj{'path'};a=rss";
4616 my $html = "$my_url?p=$proj{'path'};a=summary";
4617 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";