gitweb: Split git_patchset_body into separate subroutines
[git/dscho.git] / gitweb / gitweb.perl
blobaee4f239ae2e75d1eed2ebbb926145aa57be5936
1 #!/usr/bin/perl
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
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
21 BEGIN {
22 CGI->compile() if $ENV{'MOD_PERL'};
25 our $cgi = new CGI;
26 our $version = "++GIT_VERSION++";
27 our $my_url = $cgi->url();
28 our $my_uri = $cgi->url(-absolute => 1);
30 # core git executable to use
31 # this can just be "git" if your webserver has a sensible PATH
32 our $GIT = "++GIT_BINDIR++/git";
34 # absolute fs-path which will be prepended to the project path
35 #our $projectroot = "/pub/scm";
36 our $projectroot = "++GITWEB_PROJECTROOT++";
38 # target of the home link on top of all pages
39 our $home_link = $my_uri || "/";
41 # string of the home link on top of all pages
42 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
44 # name of your site or organization to appear in page titles
45 # replace this with something more descriptive for clearer bookmarks
46 our $site_name = "++GITWEB_SITENAME++"
47 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
49 # filename of html text to include at top of each page
50 our $site_header = "++GITWEB_SITE_HEADER++";
51 # html text to include at home page
52 our $home_text = "++GITWEB_HOMETEXT++";
53 # filename of html text to include at bottom of each page
54 our $site_footer = "++GITWEB_SITE_FOOTER++";
56 # URI of stylesheets
57 our @stylesheets = ("++GITWEB_CSS++");
58 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
59 our $stylesheet = undef;
60 # URI of GIT logo (72x27 size)
61 our $logo = "++GITWEB_LOGO++";
62 # URI of GIT favicon, assumed to be image/png type
63 our $favicon = "++GITWEB_FAVICON++";
65 # URI and label (title) of GIT logo link
66 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
67 #our $logo_label = "git documentation";
68 our $logo_url = "http://git.or.cz/";
69 our $logo_label = "git homepage";
71 # source of projects list
72 our $projects_list = "++GITWEB_LIST++";
74 # default order of projects list
75 # valid values are none, project, descr, owner, and age
76 our $default_projects_order = "project";
78 # show repository only if this file exists
79 # (only effective if this variable evaluates to true)
80 our $export_ok = "++GITWEB_EXPORT_OK++";
82 # only allow viewing of repositories also shown on the overview page
83 our $strict_export = "++GITWEB_STRICT_EXPORT++";
85 # list of git base URLs used for URL to where fetch project from,
86 # i.e. full URL is "$git_base_url/$project"
87 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
89 # default blob_plain mimetype and default charset for text/plain blob
90 our $default_blob_plain_mimetype = 'text/plain';
91 our $default_text_plain_charset = undef;
93 # file to use for guessing MIME types before trying /etc/mime.types
94 # (relative to the current git repository)
95 our $mimetypes_file = undef;
97 # assume this charset if line contains non-UTF-8 characters;
98 # it should be valid encoding (see Encoding::Supported(3pm) for list),
99 # for which encoding all byte sequences are valid, for example
100 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
101 # could be even 'utf-8' for the old behavior)
102 our $fallback_encoding = 'latin1';
104 # You define site-wide feature defaults here; override them with
105 # $GITWEB_CONFIG as necessary.
106 our %feature = (
107 # feature => {
108 # 'sub' => feature-sub (subroutine),
109 # 'override' => allow-override (boolean),
110 # 'default' => [ default options...] (array reference)}
112 # if feature is overridable (it means that allow-override has true value),
113 # then feature-sub will be called with default options as parameters;
114 # return value of feature-sub indicates if to enable specified feature
116 # if there is no 'sub' key (no feature-sub), then feature cannot be
117 # overriden
119 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
121 # Enable the 'blame' blob view, showing the last commit that modified
122 # each line in the file. This can be very CPU-intensive.
124 # To enable system wide have in $GITWEB_CONFIG
125 # $feature{'blame'}{'default'} = [1];
126 # To have project specific config enable override in $GITWEB_CONFIG
127 # $feature{'blame'}{'override'} = 1;
128 # and in project config gitweb.blame = 0|1;
129 'blame' => {
130 'sub' => \&feature_blame,
131 'override' => 0,
132 'default' => [0]},
134 # Enable the 'snapshot' link, providing a compressed tarball of any
135 # tree. This can potentially generate high traffic if you have large
136 # project.
138 # To disable system wide have in $GITWEB_CONFIG
139 # $feature{'snapshot'}{'default'} = [undef];
140 # To have project specific config enable override in $GITWEB_CONFIG
141 # $feature{'snapshot'}{'override'} = 1;
142 # and in project config gitweb.snapshot = none|gzip|bzip2|zip;
143 'snapshot' => {
144 'sub' => \&feature_snapshot,
145 'override' => 0,
146 # => [content-encoding, suffix, program]
147 'default' => ['x-gzip', 'gz', 'gzip']},
149 # Enable text search, which will list the commits which match author,
150 # committer or commit text to a given string. Enabled by default.
151 # Project specific override is not supported.
152 'search' => {
153 'override' => 0,
154 'default' => [1]},
156 # Enable grep search, which will list the files in currently selected
157 # tree containing the given string. Enabled by default. This can be
158 # potentially CPU-intensive, of course.
160 # To enable system wide have in $GITWEB_CONFIG
161 # $feature{'grep'}{'default'} = [1];
162 # To have project specific config enable override in $GITWEB_CONFIG
163 # $feature{'grep'}{'override'} = 1;
164 # and in project config gitweb.grep = 0|1;
165 'grep' => {
166 'override' => 0,
167 'default' => [1]},
169 # Enable the pickaxe search, which will list the commits that modified
170 # a given string in a file. This can be practical and quite faster
171 # alternative to 'blame', but still potentially CPU-intensive.
173 # To enable system wide have in $GITWEB_CONFIG
174 # $feature{'pickaxe'}{'default'} = [1];
175 # To have project specific config enable override in $GITWEB_CONFIG
176 # $feature{'pickaxe'}{'override'} = 1;
177 # and in project config gitweb.pickaxe = 0|1;
178 'pickaxe' => {
179 'sub' => \&feature_pickaxe,
180 'override' => 0,
181 'default' => [1]},
183 # Make gitweb use an alternative format of the URLs which can be
184 # more readable and natural-looking: project name is embedded
185 # directly in the path and the query string contains other
186 # auxiliary information. All gitweb installations recognize
187 # URL in either format; this configures in which formats gitweb
188 # generates links.
190 # To enable system wide have in $GITWEB_CONFIG
191 # $feature{'pathinfo'}{'default'} = [1];
192 # Project specific override is not supported.
194 # Note that you will need to change the default location of CSS,
195 # favicon, logo and possibly other files to an absolute URL. Also,
196 # if gitweb.cgi serves as your indexfile, you will need to force
197 # $my_uri to contain the script name in your $GITWEB_CONFIG.
198 'pathinfo' => {
199 'override' => 0,
200 'default' => [0]},
202 # Make gitweb consider projects in project root subdirectories
203 # to be forks of existing projects. Given project $projname.git,
204 # projects matching $projname/*.git will not be shown in the main
205 # projects list, instead a '+' mark will be added to $projname
206 # there and a 'forks' view will be enabled for the project, listing
207 # all the forks. If project list is taken from a file, forks have
208 # to be listed after the main project.
210 # To enable system wide have in $GITWEB_CONFIG
211 # $feature{'forks'}{'default'} = [1];
212 # Project specific override is not supported.
213 'forks' => {
214 'override' => 0,
215 'default' => [0]},
218 sub gitweb_check_feature {
219 my ($name) = @_;
220 return unless exists $feature{$name};
221 my ($sub, $override, @defaults) = (
222 $feature{$name}{'sub'},
223 $feature{$name}{'override'},
224 @{$feature{$name}{'default'}});
225 if (!$override) { return @defaults; }
226 if (!defined $sub) {
227 warn "feature $name is not overrideable";
228 return @defaults;
230 return $sub->(@defaults);
233 sub feature_blame {
234 my ($val) = git_get_project_config('blame', '--bool');
236 if ($val eq 'true') {
237 return 1;
238 } elsif ($val eq 'false') {
239 return 0;
242 return $_[0];
245 sub feature_snapshot {
246 my ($ctype, $suffix, $command) = @_;
248 my ($val) = git_get_project_config('snapshot');
250 if ($val eq 'gzip') {
251 return ('x-gzip', 'gz', 'gzip');
252 } elsif ($val eq 'bzip2') {
253 return ('x-bzip2', 'bz2', 'bzip2');
254 } elsif ($val eq 'zip') {
255 return ('x-zip', 'zip', '');
256 } elsif ($val eq 'none') {
257 return ();
260 return ($ctype, $suffix, $command);
263 sub gitweb_have_snapshot {
264 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
265 my $have_snapshot = (defined $ctype && defined $suffix);
267 return $have_snapshot;
270 sub feature_grep {
271 my ($val) = git_get_project_config('grep', '--bool');
273 if ($val eq 'true') {
274 return (1);
275 } elsif ($val eq 'false') {
276 return (0);
279 return ($_[0]);
282 sub feature_pickaxe {
283 my ($val) = git_get_project_config('pickaxe', '--bool');
285 if ($val eq 'true') {
286 return (1);
287 } elsif ($val eq 'false') {
288 return (0);
291 return ($_[0]);
294 # checking HEAD file with -e is fragile if the repository was
295 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
296 # and then pruned.
297 sub check_head_link {
298 my ($dir) = @_;
299 my $headfile = "$dir/HEAD";
300 return ((-e $headfile) ||
301 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
304 sub check_export_ok {
305 my ($dir) = @_;
306 return (check_head_link($dir) &&
307 (!$export_ok || -e "$dir/$export_ok"));
310 # rename detection options for git-diff and git-diff-tree
311 # - default is '-M', with the cost proportional to
312 # (number of removed files) * (number of new files).
313 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
314 # (number of changed files + number of removed files) * (number of new files)
315 # - even more costly is '-C', '--find-copies-harder' with cost
316 # (number of files in the original tree) * (number of new files)
317 # - one might want to include '-B' option, e.g. '-B', '-M'
318 our @diff_opts = ('-M'); # taken from git_commit
320 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
321 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
323 # version of the core git binary
324 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
326 $projects_list ||= $projectroot;
328 # ======================================================================
329 # input validation and dispatch
330 our $action = $cgi->param('a');
331 if (defined $action) {
332 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
333 die_error(undef, "Invalid action parameter");
337 # parameters which are pathnames
338 our $project = $cgi->param('p');
339 if (defined $project) {
340 if (!validate_pathname($project) ||
341 !(-d "$projectroot/$project") ||
342 !check_head_link("$projectroot/$project") ||
343 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
344 ($strict_export && !project_in_list($project))) {
345 undef $project;
346 die_error(undef, "No such project");
350 our $file_name = $cgi->param('f');
351 if (defined $file_name) {
352 if (!validate_pathname($file_name)) {
353 die_error(undef, "Invalid file parameter");
357 our $file_parent = $cgi->param('fp');
358 if (defined $file_parent) {
359 if (!validate_pathname($file_parent)) {
360 die_error(undef, "Invalid file parent parameter");
364 # parameters which are refnames
365 our $hash = $cgi->param('h');
366 if (defined $hash) {
367 if (!validate_refname($hash)) {
368 die_error(undef, "Invalid hash parameter");
372 our $hash_parent = $cgi->param('hp');
373 if (defined $hash_parent) {
374 if (!validate_refname($hash_parent)) {
375 die_error(undef, "Invalid hash parent parameter");
379 our $hash_base = $cgi->param('hb');
380 if (defined $hash_base) {
381 if (!validate_refname($hash_base)) {
382 die_error(undef, "Invalid hash base parameter");
386 our $hash_parent_base = $cgi->param('hpb');
387 if (defined $hash_parent_base) {
388 if (!validate_refname($hash_parent_base)) {
389 die_error(undef, "Invalid hash parent base parameter");
393 # other parameters
394 our $page = $cgi->param('pg');
395 if (defined $page) {
396 if ($page =~ m/[^0-9]/) {
397 die_error(undef, "Invalid page parameter");
401 our $searchtype = $cgi->param('st');
402 if (defined $searchtype) {
403 if ($searchtype =~ m/[^a-z]/) {
404 die_error(undef, "Invalid searchtype parameter");
408 our $searchtext = $cgi->param('s');
409 our $search_regexp;
410 if (defined $searchtext) {
411 if ($searchtype ne 'grep' and $searchtype ne 'pickaxe' and $searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
412 die_error(undef, "Invalid search parameter");
414 if (length($searchtext) < 2) {
415 die_error(undef, "At least two characters are required for search parameter");
417 $search_regexp = quotemeta $searchtext;
420 # now read PATH_INFO and use it as alternative to parameters
421 sub evaluate_path_info {
422 return if defined $project;
423 my $path_info = $ENV{"PATH_INFO"};
424 return if !$path_info;
425 $path_info =~ s,^/+,,;
426 return if !$path_info;
427 # find which part of PATH_INFO is project
428 $project = $path_info;
429 $project =~ s,/+$,,;
430 while ($project && !check_head_link("$projectroot/$project")) {
431 $project =~ s,/*[^/]*$,,;
433 # validate project
434 $project = validate_pathname($project);
435 if (!$project ||
436 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
437 ($strict_export && !project_in_list($project))) {
438 undef $project;
439 return;
441 # do not change any parameters if an action is given using the query string
442 return if $action;
443 $path_info =~ s,^$project/*,,;
444 my ($refname, $pathname) = split(/:/, $path_info, 2);
445 if (defined $pathname) {
446 # we got "project.git/branch:filename" or "project.git/branch:dir/"
447 # we could use git_get_type(branch:pathname), but it needs $git_dir
448 $pathname =~ s,^/+,,;
449 if (!$pathname || substr($pathname, -1) eq "/") {
450 $action ||= "tree";
451 $pathname =~ s,/$,,;
452 } else {
453 $action ||= "blob_plain";
455 $hash_base ||= validate_refname($refname);
456 $file_name ||= validate_pathname($pathname);
457 } elsif (defined $refname) {
458 # we got "project.git/branch"
459 $action ||= "shortlog";
460 $hash ||= validate_refname($refname);
463 evaluate_path_info();
465 # path to the current git repository
466 our $git_dir;
467 $git_dir = "$projectroot/$project" if $project;
469 # dispatch
470 my %actions = (
471 "blame" => \&git_blame2,
472 "blobdiff" => \&git_blobdiff,
473 "blobdiff_plain" => \&git_blobdiff_plain,
474 "blob" => \&git_blob,
475 "blob_plain" => \&git_blob_plain,
476 "commitdiff" => \&git_commitdiff,
477 "commitdiff_plain" => \&git_commitdiff_plain,
478 "commit" => \&git_commit,
479 "forks" => \&git_forks,
480 "heads" => \&git_heads,
481 "history" => \&git_history,
482 "log" => \&git_log,
483 "rss" => \&git_rss,
484 "atom" => \&git_atom,
485 "search" => \&git_search,
486 "search_help" => \&git_search_help,
487 "shortlog" => \&git_shortlog,
488 "summary" => \&git_summary,
489 "tag" => \&git_tag,
490 "tags" => \&git_tags,
491 "tree" => \&git_tree,
492 "snapshot" => \&git_snapshot,
493 "object" => \&git_object,
494 # those below don't need $project
495 "opml" => \&git_opml,
496 "project_list" => \&git_project_list,
497 "project_index" => \&git_project_index,
500 if (!defined $action) {
501 if (defined $hash) {
502 $action = git_get_type($hash);
503 } elsif (defined $hash_base && defined $file_name) {
504 $action = git_get_type("$hash_base:$file_name");
505 } elsif (defined $project) {
506 $action = 'summary';
507 } else {
508 $action = 'project_list';
511 if (!defined($actions{$action})) {
512 die_error(undef, "Unknown action");
514 if ($action !~ m/^(opml|project_list|project_index)$/ &&
515 !$project) {
516 die_error(undef, "Project needed");
518 $actions{$action}->();
519 exit;
521 ## ======================================================================
522 ## action links
524 sub href(%) {
525 my %params = @_;
526 # default is to use -absolute url() i.e. $my_uri
527 my $href = $params{-full} ? $my_url : $my_uri;
529 # XXX: Warning: If you touch this, check the search form for updating,
530 # too.
532 my @mapping = (
533 project => "p",
534 action => "a",
535 file_name => "f",
536 file_parent => "fp",
537 hash => "h",
538 hash_parent => "hp",
539 hash_base => "hb",
540 hash_parent_base => "hpb",
541 page => "pg",
542 order => "o",
543 searchtext => "s",
544 searchtype => "st",
546 my %mapping = @mapping;
548 $params{'project'} = $project unless exists $params{'project'};
550 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
551 if ($use_pathinfo) {
552 # use PATH_INFO for project name
553 $href .= "/$params{'project'}" if defined $params{'project'};
554 delete $params{'project'};
556 # Summary just uses the project path URL
557 if (defined $params{'action'} && $params{'action'} eq 'summary') {
558 delete $params{'action'};
562 # now encode the parameters explicitly
563 my @result = ();
564 for (my $i = 0; $i < @mapping; $i += 2) {
565 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
566 if (defined $params{$name}) {
567 push @result, $symbol . "=" . esc_param($params{$name});
570 $href .= "?" . join(';', @result) if scalar @result;
572 return $href;
576 ## ======================================================================
577 ## validation, quoting/unquoting and escaping
579 sub validate_pathname {
580 my $input = shift || return undef;
582 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
583 # at the beginning, at the end, and between slashes.
584 # also this catches doubled slashes
585 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
586 return undef;
588 # no null characters
589 if ($input =~ m!\0!) {
590 return undef;
592 return $input;
595 sub validate_refname {
596 my $input = shift || return undef;
598 # textual hashes are O.K.
599 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
600 return $input;
602 # it must be correct pathname
603 $input = validate_pathname($input)
604 or return undef;
605 # restrictions on ref name according to git-check-ref-format
606 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
607 return undef;
609 return $input;
612 # decode sequences of octets in utf8 into Perl's internal form,
613 # which is utf-8 with utf8 flag set if needed. gitweb writes out
614 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
615 sub to_utf8 {
616 my $str = shift;
617 my $res;
618 eval { $res = decode_utf8($str, Encode::FB_CROAK); };
619 if (defined $res) {
620 return $res;
621 } else {
622 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
626 # quote unsafe chars, but keep the slash, even when it's not
627 # correct, but quoted slashes look too horrible in bookmarks
628 sub esc_param {
629 my $str = shift;
630 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
631 $str =~ s/\+/%2B/g;
632 $str =~ s/ /\+/g;
633 return $str;
636 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
637 sub esc_url {
638 my $str = shift;
639 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
640 $str =~ s/\+/%2B/g;
641 $str =~ s/ /\+/g;
642 return $str;
645 # replace invalid utf8 character with SUBSTITUTION sequence
646 sub esc_html ($;%) {
647 my $str = shift;
648 my %opts = @_;
650 $str = to_utf8($str);
651 $str = $cgi->escapeHTML($str);
652 if ($opts{'-nbsp'}) {
653 $str =~ s/ /&nbsp;/g;
655 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
656 return $str;
659 # quote control characters and escape filename to HTML
660 sub esc_path {
661 my $str = shift;
662 my %opts = @_;
664 $str = to_utf8($str);
665 $str = $cgi->escapeHTML($str);
666 if ($opts{'-nbsp'}) {
667 $str =~ s/ /&nbsp;/g;
669 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
670 return $str;
673 # Make control characters "printable", using character escape codes (CEC)
674 sub quot_cec {
675 my $cntrl = shift;
676 my %es = ( # character escape codes, aka escape sequences
677 "\t" => '\t', # tab (HT)
678 "\n" => '\n', # line feed (LF)
679 "\r" => '\r', # carrige return (CR)
680 "\f" => '\f', # form feed (FF)
681 "\b" => '\b', # backspace (BS)
682 "\a" => '\a', # alarm (bell) (BEL)
683 "\e" => '\e', # escape (ESC)
684 "\013" => '\v', # vertical tab (VT)
685 "\000" => '\0', # nul character (NUL)
687 my $chr = ( (exists $es{$cntrl})
688 ? $es{$cntrl}
689 : sprintf('\%03o', ord($cntrl)) );
690 return "<span class=\"cntrl\">$chr</span>";
693 # Alternatively use unicode control pictures codepoints,
694 # Unicode "printable representation" (PR)
695 sub quot_upr {
696 my $cntrl = shift;
697 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
698 return "<span class=\"cntrl\">$chr</span>";
701 # git may return quoted and escaped filenames
702 sub unquote {
703 my $str = shift;
705 sub unq {
706 my $seq = shift;
707 my %es = ( # character escape codes, aka escape sequences
708 't' => "\t", # tab (HT, TAB)
709 'n' => "\n", # newline (NL)
710 'r' => "\r", # return (CR)
711 'f' => "\f", # form feed (FF)
712 'b' => "\b", # backspace (BS)
713 'a' => "\a", # alarm (bell) (BEL)
714 'e' => "\e", # escape (ESC)
715 'v' => "\013", # vertical tab (VT)
718 if ($seq =~ m/^[0-7]{1,3}$/) {
719 # octal char sequence
720 return chr(oct($seq));
721 } elsif (exists $es{$seq}) {
722 # C escape sequence, aka character escape code
723 return $es{$seq}
725 # quoted ordinary character
726 return $seq;
729 if ($str =~ m/^"(.*)"$/) {
730 # needs unquoting
731 $str = $1;
732 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
734 return $str;
737 # escape tabs (convert tabs to spaces)
738 sub untabify {
739 my $line = shift;
741 while ((my $pos = index($line, "\t")) != -1) {
742 if (my $count = (8 - ($pos % 8))) {
743 my $spaces = ' ' x $count;
744 $line =~ s/\t/$spaces/;
748 return $line;
751 sub project_in_list {
752 my $project = shift;
753 my @list = git_get_projects_list();
754 return @list && scalar(grep { $_->{'path'} eq $project } @list);
757 ## ----------------------------------------------------------------------
758 ## HTML aware string manipulation
760 sub chop_str {
761 my $str = shift;
762 my $len = shift;
763 my $add_len = shift || 10;
765 # allow only $len chars, but don't cut a word if it would fit in $add_len
766 # if it doesn't fit, cut it if it's still longer than the dots we would add
767 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
768 my $body = $1;
769 my $tail = $2;
770 if (length($tail) > 4) {
771 $tail = " ...";
772 $body =~ s/&[^;]*$//; # remove chopped character entities
774 return "$body$tail";
777 ## ----------------------------------------------------------------------
778 ## functions returning short strings
780 # CSS class for given age value (in seconds)
781 sub age_class {
782 my $age = shift;
784 if (!defined $age) {
785 return "noage";
786 } elsif ($age < 60*60*2) {
787 return "age0";
788 } elsif ($age < 60*60*24*2) {
789 return "age1";
790 } else {
791 return "age2";
795 # convert age in seconds to "nn units ago" string
796 sub age_string {
797 my $age = shift;
798 my $age_str;
800 if ($age > 60*60*24*365*2) {
801 $age_str = (int $age/60/60/24/365);
802 $age_str .= " years ago";
803 } elsif ($age > 60*60*24*(365/12)*2) {
804 $age_str = int $age/60/60/24/(365/12);
805 $age_str .= " months ago";
806 } elsif ($age > 60*60*24*7*2) {
807 $age_str = int $age/60/60/24/7;
808 $age_str .= " weeks ago";
809 } elsif ($age > 60*60*24*2) {
810 $age_str = int $age/60/60/24;
811 $age_str .= " days ago";
812 } elsif ($age > 60*60*2) {
813 $age_str = int $age/60/60;
814 $age_str .= " hours ago";
815 } elsif ($age > 60*2) {
816 $age_str = int $age/60;
817 $age_str .= " min ago";
818 } elsif ($age > 2) {
819 $age_str = int $age;
820 $age_str .= " sec ago";
821 } else {
822 $age_str .= " right now";
824 return $age_str;
827 # convert file mode in octal to symbolic file mode string
828 sub mode_str {
829 my $mode = oct shift;
831 if (S_ISDIR($mode & S_IFMT)) {
832 return 'drwxr-xr-x';
833 } elsif (S_ISLNK($mode)) {
834 return 'lrwxrwxrwx';
835 } elsif (S_ISREG($mode)) {
836 # git cares only about the executable bit
837 if ($mode & S_IXUSR) {
838 return '-rwxr-xr-x';
839 } else {
840 return '-rw-r--r--';
842 } else {
843 return '----------';
847 # convert file mode in octal to file type string
848 sub file_type {
849 my $mode = shift;
851 if ($mode !~ m/^[0-7]+$/) {
852 return $mode;
853 } else {
854 $mode = oct $mode;
857 if (S_ISDIR($mode & S_IFMT)) {
858 return "directory";
859 } elsif (S_ISLNK($mode)) {
860 return "symlink";
861 } elsif (S_ISREG($mode)) {
862 return "file";
863 } else {
864 return "unknown";
868 # convert file mode in octal to file type description string
869 sub file_type_long {
870 my $mode = shift;
872 if ($mode !~ m/^[0-7]+$/) {
873 return $mode;
874 } else {
875 $mode = oct $mode;
878 if (S_ISDIR($mode & S_IFMT)) {
879 return "directory";
880 } elsif (S_ISLNK($mode)) {
881 return "symlink";
882 } elsif (S_ISREG($mode)) {
883 if ($mode & S_IXUSR) {
884 return "executable";
885 } else {
886 return "file";
888 } else {
889 return "unknown";
894 ## ----------------------------------------------------------------------
895 ## functions returning short HTML fragments, or transforming HTML fragments
896 ## which don't belong to other sections
898 # format line of commit message.
899 sub format_log_line_html {
900 my $line = shift;
902 $line = esc_html($line, -nbsp=>1);
903 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
904 my $hash_text = $1;
905 my $link =
906 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
907 -class => "text"}, $hash_text);
908 $line =~ s/$hash_text/$link/;
910 return $line;
913 # format marker of refs pointing to given object
914 sub format_ref_marker {
915 my ($refs, $id) = @_;
916 my $markers = '';
918 if (defined $refs->{$id}) {
919 foreach my $ref (@{$refs->{$id}}) {
920 my ($type, $name) = qw();
921 # e.g. tags/v2.6.11 or heads/next
922 if ($ref =~ m!^(.*?)s?/(.*)$!) {
923 $type = $1;
924 $name = $2;
925 } else {
926 $type = "ref";
927 $name = $ref;
930 $markers .= " <span class=\"$type\" title=\"$ref\">" .
931 esc_html($name) . "</span>";
935 if ($markers) {
936 return ' <span class="refs">'. $markers . '</span>';
937 } else {
938 return "";
942 # format, perhaps shortened and with markers, title line
943 sub format_subject_html {
944 my ($long, $short, $href, $extra) = @_;
945 $extra = '' unless defined($extra);
947 if (length($short) < length($long)) {
948 return $cgi->a({-href => $href, -class => "list subject",
949 -title => to_utf8($long)},
950 esc_html($short) . $extra);
951 } else {
952 return $cgi->a({-href => $href, -class => "list subject"},
953 esc_html($long) . $extra);
957 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
958 sub format_git_diff_header_line {
959 my $line = shift;
960 my $diffinfo = shift;
961 my ($from, $to) = @_;
963 if ($diffinfo->{'nparents'}) {
964 # combined diff
965 $line =~ s!^(diff (.*?) )"?.*$!$1!;
966 if ($to->{'href'}) {
967 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
968 esc_path($to->{'file'}));
969 } else { # file was deleted (no href)
970 $line .= esc_path($to->{'file'});
972 } else {
973 # "ordinary" diff
974 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
975 if ($from->{'href'}) {
976 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
977 'a/' . esc_path($from->{'file'}));
978 } else { # file was added (no href)
979 $line .= 'a/' . esc_path($from->{'file'});
981 $line .= ' ';
982 if ($to->{'href'}) {
983 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
984 'b/' . esc_path($to->{'file'}));
985 } else { # file was deleted
986 $line .= 'b/' . esc_path($to->{'file'});
990 return "<div class=\"diff header\">$line</div>\n";
993 # format extended diff header line, before patch itself
994 sub format_extended_diff_header_line {
995 my $line = shift;
996 my $diffinfo = shift;
997 my ($from, $to) = @_;
999 # match <path>
1000 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1001 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1002 esc_path($from->{'file'}));
1004 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1005 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1006 esc_path($to->{'file'}));
1008 # match single <mode>
1009 if ($line =~ m/\s(\d{6})$/) {
1010 $line .= '<span class="info"> (' .
1011 file_type_long($1) .
1012 ')</span>';
1014 # match <hash>
1015 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1016 # can match only for combined diff
1017 $line = 'index ';
1018 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1019 if ($from->{'href'}[$i]) {
1020 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1021 -class=>"hash"},
1022 substr($diffinfo->{'from_id'}[$i],0,7));
1023 } else {
1024 $line .= '0' x 7;
1026 # separator
1027 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1029 $line .= '..';
1030 if ($to->{'href'}) {
1031 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1032 substr($diffinfo->{'to_id'},0,7));
1033 } else {
1034 $line .= '0' x 7;
1037 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1038 # can match only for ordinary diff
1039 my ($from_link, $to_link);
1040 if ($from->{'href'}) {
1041 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1042 substr($diffinfo->{'from_id'},0,7));
1043 } else {
1044 $from_link = '0' x 7;
1046 if ($to->{'href'}) {
1047 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1048 substr($diffinfo->{'to_id'},0,7));
1049 } else {
1050 $to_link = '0' x 7;
1052 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1053 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1056 return $line . "<br/>\n";
1059 # format from-file/to-file diff header
1060 sub format_diff_from_to_header {
1061 my ($from_line, $to_line, $diffinfo, $from, $to) = @_;
1062 my $line;
1063 my $result = '';
1065 $line = $from_line;
1066 #assert($line =~ m/^---/) if DEBUG;
1067 # no extra formatting "^--- /dev/null"
1068 if ($line =~ m!^--- "?a/!) {
1069 if (!$diffinfo->{'nparents'} && # multiple 'from'
1070 $from->{'href'}) {
1071 $line = '--- a/' .
1072 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1073 esc_path($from->{'file'}));
1074 } else {
1075 $line = '--- a/' .
1076 esc_path($from->{'file'});
1079 $result .= qq!<div class="diff from_file">$line</div>\n!;
1081 $line = $to_line;
1082 #assert($line =~ m/^\+\+\+/) if DEBUG;
1083 # no extra formatting for "^+++ /dev/null"
1084 if ($line =~ m!^\+\+\+ "?b/!) {
1085 if ($to->{'href'}) {
1086 $line = '+++ b/' .
1087 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1088 esc_path($to->{'file'}));
1089 } else {
1090 $line = '+++ b/' .
1091 esc_path($to->{'file'});
1094 $result .= qq!<div class="diff to_file">$line</div>\n!;
1096 return $result;
1099 # format patch (diff) line (not to be used for diff headers)
1100 sub format_diff_line {
1101 my $line = shift;
1102 my ($from, $to) = @_;
1103 my $diff_class = "";
1105 chomp $line;
1107 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1108 # combined diff
1109 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1110 if ($line =~ m/^\@{3}/) {
1111 $diff_class = " chunk_header";
1112 } elsif ($line =~ m/^\\/) {
1113 $diff_class = " incomplete";
1114 } elsif ($prefix =~ tr/+/+/) {
1115 $diff_class = " add";
1116 } elsif ($prefix =~ tr/-/-/) {
1117 $diff_class = " rem";
1119 } else {
1120 # assume ordinary diff
1121 my $char = substr($line, 0, 1);
1122 if ($char eq '+') {
1123 $diff_class = " add";
1124 } elsif ($char eq '-') {
1125 $diff_class = " rem";
1126 } elsif ($char eq '@') {
1127 $diff_class = " chunk_header";
1128 } elsif ($char eq "\\") {
1129 $diff_class = " incomplete";
1132 $line = untabify($line);
1133 if ($from && $to && $line =~ m/^\@{2} /) {
1134 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1135 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1137 $from_lines = 0 unless defined $from_lines;
1138 $to_lines = 0 unless defined $to_lines;
1140 if ($from->{'href'}) {
1141 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1142 -class=>"list"}, $from_text);
1144 if ($to->{'href'}) {
1145 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1146 -class=>"list"}, $to_text);
1148 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1149 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1150 return "<div class=\"diff$diff_class\">$line</div>\n";
1151 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1152 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1153 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1155 @from_text = split(' ', $ranges);
1156 for (my $i = 0; $i < @from_text; ++$i) {
1157 ($from_start[$i], $from_nlines[$i]) =
1158 (split(',', substr($from_text[$i], 1)), 0);
1161 $to_text = pop @from_text;
1162 $to_start = pop @from_start;
1163 $to_nlines = pop @from_nlines;
1165 $line = "<span class=\"chunk_info\">$prefix ";
1166 for (my $i = 0; $i < @from_text; ++$i) {
1167 if ($from->{'href'}[$i]) {
1168 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1169 -class=>"list"}, $from_text[$i]);
1170 } else {
1171 $line .= $from_text[$i];
1173 $line .= " ";
1175 if ($to->{'href'}) {
1176 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1177 -class=>"list"}, $to_text);
1178 } else {
1179 $line .= $to_text;
1181 $line .= " $prefix</span>" .
1182 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1183 return "<div class=\"diff$diff_class\">$line</div>\n";
1185 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1188 ## ----------------------------------------------------------------------
1189 ## git utility subroutines, invoking git commands
1191 # returns path to the core git executable and the --git-dir parameter as list
1192 sub git_cmd {
1193 return $GIT, '--git-dir='.$git_dir;
1196 # returns path to the core git executable and the --git-dir parameter as string
1197 sub git_cmd_str {
1198 return join(' ', git_cmd());
1201 # get HEAD ref of given project as hash
1202 sub git_get_head_hash {
1203 my $project = shift;
1204 my $o_git_dir = $git_dir;
1205 my $retval = undef;
1206 $git_dir = "$projectroot/$project";
1207 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1208 my $head = <$fd>;
1209 close $fd;
1210 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1211 $retval = $1;
1214 if (defined $o_git_dir) {
1215 $git_dir = $o_git_dir;
1217 return $retval;
1220 # get type of given object
1221 sub git_get_type {
1222 my $hash = shift;
1224 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1225 my $type = <$fd>;
1226 close $fd or return;
1227 chomp $type;
1228 return $type;
1231 sub git_get_project_config {
1232 my ($key, $type) = @_;
1234 return unless ($key);
1235 $key =~ s/^gitweb\.//;
1236 return if ($key =~ m/\W/);
1238 my @x = (git_cmd(), 'config');
1239 if (defined $type) { push @x, $type; }
1240 push @x, "--get";
1241 push @x, "gitweb.$key";
1242 my $val = qx(@x);
1243 chomp $val;
1244 return ($val);
1247 # get hash of given path at given ref
1248 sub git_get_hash_by_path {
1249 my $base = shift;
1250 my $path = shift || return undef;
1251 my $type = shift;
1253 $path =~ s,/+$,,;
1255 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1256 or die_error(undef, "Open git-ls-tree failed");
1257 my $line = <$fd>;
1258 close $fd or return undef;
1260 if (!defined $line) {
1261 # there is no tree or hash given by $path at $base
1262 return undef;
1265 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1266 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1267 if (defined $type && $type ne $2) {
1268 # type doesn't match
1269 return undef;
1271 return $3;
1274 # get path of entry with given hash at given tree-ish (ref)
1275 # used to get 'from' filename for combined diff (merge commit) for renames
1276 sub git_get_path_by_hash {
1277 my $base = shift || return;
1278 my $hash = shift || return;
1280 local $/ = "\0";
1282 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1283 or return undef;
1284 while (my $line = <$fd>) {
1285 chomp $line;
1287 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1288 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1289 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1290 close $fd;
1291 return $1;
1294 close $fd;
1295 return undef;
1298 ## ......................................................................
1299 ## git utility functions, directly accessing git repository
1301 sub git_get_project_description {
1302 my $path = shift;
1304 open my $fd, "$projectroot/$path/description" or return undef;
1305 my $descr = <$fd>;
1306 close $fd;
1307 if (defined $descr) {
1308 chomp $descr;
1310 return $descr;
1313 sub git_get_project_url_list {
1314 my $path = shift;
1316 open my $fd, "$projectroot/$path/cloneurl" or return;
1317 my @git_project_url_list = map { chomp; $_ } <$fd>;
1318 close $fd;
1320 return wantarray ? @git_project_url_list : \@git_project_url_list;
1323 sub git_get_projects_list {
1324 my ($filter) = @_;
1325 my @list;
1327 $filter ||= '';
1328 $filter =~ s/\.git$//;
1330 my ($check_forks) = gitweb_check_feature('forks');
1332 if (-d $projects_list) {
1333 # search in directory
1334 my $dir = $projects_list . ($filter ? "/$filter" : '');
1335 # remove the trailing "/"
1336 $dir =~ s!/+$!!;
1337 my $pfxlen = length("$dir");
1339 File::Find::find({
1340 follow_fast => 1, # follow symbolic links
1341 dangling_symlinks => 0, # ignore dangling symlinks, silently
1342 wanted => sub {
1343 # skip project-list toplevel, if we get it.
1344 return if (m!^[/.]$!);
1345 # only directories can be git repositories
1346 return unless (-d $_);
1348 my $subdir = substr($File::Find::name, $pfxlen + 1);
1349 # we check related file in $projectroot
1350 if ($check_forks and $subdir =~ m#/.#) {
1351 $File::Find::prune = 1;
1352 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1353 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1354 $File::Find::prune = 1;
1357 }, "$dir");
1359 } elsif (-f $projects_list) {
1360 # read from file(url-encoded):
1361 # 'git%2Fgit.git Linus+Torvalds'
1362 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1363 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1364 my %paths;
1365 open my ($fd), $projects_list or return;
1366 PROJECT:
1367 while (my $line = <$fd>) {
1368 chomp $line;
1369 my ($path, $owner) = split ' ', $line;
1370 $path = unescape($path);
1371 $owner = unescape($owner);
1372 if (!defined $path) {
1373 next;
1375 if ($filter ne '') {
1376 # looking for forks;
1377 my $pfx = substr($path, 0, length($filter));
1378 if ($pfx ne $filter) {
1379 next PROJECT;
1381 my $sfx = substr($path, length($filter));
1382 if ($sfx !~ /^\/.*\.git$/) {
1383 next PROJECT;
1385 } elsif ($check_forks) {
1386 PATH:
1387 foreach my $filter (keys %paths) {
1388 # looking for forks;
1389 my $pfx = substr($path, 0, length($filter));
1390 if ($pfx ne $filter) {
1391 next PATH;
1393 my $sfx = substr($path, length($filter));
1394 if ($sfx !~ /^\/.*\.git$/) {
1395 next PATH;
1397 # is a fork, don't include it in
1398 # the list
1399 next PROJECT;
1402 if (check_export_ok("$projectroot/$path")) {
1403 my $pr = {
1404 path => $path,
1405 owner => to_utf8($owner),
1407 push @list, $pr;
1408 (my $forks_path = $path) =~ s/\.git$//;
1409 $paths{$forks_path}++;
1412 close $fd;
1414 return @list;
1417 sub git_get_project_owner {
1418 my $project = shift;
1419 my $owner;
1421 return undef unless $project;
1423 # read from file (url-encoded):
1424 # 'git%2Fgit.git Linus+Torvalds'
1425 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1426 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1427 if (-f $projects_list) {
1428 open (my $fd , $projects_list);
1429 while (my $line = <$fd>) {
1430 chomp $line;
1431 my ($pr, $ow) = split ' ', $line;
1432 $pr = unescape($pr);
1433 $ow = unescape($ow);
1434 if ($pr eq $project) {
1435 $owner = to_utf8($ow);
1436 last;
1439 close $fd;
1441 if (!defined $owner) {
1442 $owner = get_file_owner("$projectroot/$project");
1445 return $owner;
1448 sub git_get_last_activity {
1449 my ($path) = @_;
1450 my $fd;
1452 $git_dir = "$projectroot/$path";
1453 open($fd, "-|", git_cmd(), 'for-each-ref',
1454 '--format=%(committer)',
1455 '--sort=-committerdate',
1456 '--count=1',
1457 'refs/heads') or return;
1458 my $most_recent = <$fd>;
1459 close $fd or return;
1460 if (defined $most_recent &&
1461 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1462 my $timestamp = $1;
1463 my $age = time - $timestamp;
1464 return ($age, age_string($age));
1468 sub git_get_references {
1469 my $type = shift || "";
1470 my %refs;
1471 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1472 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1473 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1474 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1475 or return;
1477 while (my $line = <$fd>) {
1478 chomp $line;
1479 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1480 if (defined $refs{$1}) {
1481 push @{$refs{$1}}, $2;
1482 } else {
1483 $refs{$1} = [ $2 ];
1487 close $fd or return;
1488 return \%refs;
1491 sub git_get_rev_name_tags {
1492 my $hash = shift || return undef;
1494 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1495 or return;
1496 my $name_rev = <$fd>;
1497 close $fd;
1499 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1500 return $1;
1501 } else {
1502 # catches also '$hash undefined' output
1503 return undef;
1507 ## ----------------------------------------------------------------------
1508 ## parse to hash functions
1510 sub parse_date {
1511 my $epoch = shift;
1512 my $tz = shift || "-0000";
1514 my %date;
1515 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1516 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1517 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1518 $date{'hour'} = $hour;
1519 $date{'minute'} = $min;
1520 $date{'mday'} = $mday;
1521 $date{'day'} = $days[$wday];
1522 $date{'month'} = $months[$mon];
1523 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1524 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1525 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1526 $mday, $months[$mon], $hour ,$min;
1527 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1528 1900+$year, $mon, $mday, $hour ,$min, $sec;
1530 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1531 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1532 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1533 $date{'hour_local'} = $hour;
1534 $date{'minute_local'} = $min;
1535 $date{'tz_local'} = $tz;
1536 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1537 1900+$year, $mon+1, $mday,
1538 $hour, $min, $sec, $tz);
1539 return %date;
1542 sub parse_tag {
1543 my $tag_id = shift;
1544 my %tag;
1545 my @comment;
1547 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1548 $tag{'id'} = $tag_id;
1549 while (my $line = <$fd>) {
1550 chomp $line;
1551 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1552 $tag{'object'} = $1;
1553 } elsif ($line =~ m/^type (.+)$/) {
1554 $tag{'type'} = $1;
1555 } elsif ($line =~ m/^tag (.+)$/) {
1556 $tag{'name'} = $1;
1557 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1558 $tag{'author'} = $1;
1559 $tag{'epoch'} = $2;
1560 $tag{'tz'} = $3;
1561 } elsif ($line =~ m/--BEGIN/) {
1562 push @comment, $line;
1563 last;
1564 } elsif ($line eq "") {
1565 last;
1568 push @comment, <$fd>;
1569 $tag{'comment'} = \@comment;
1570 close $fd or return;
1571 if (!defined $tag{'name'}) {
1572 return
1574 return %tag
1577 sub parse_commit_text {
1578 my ($commit_text, $withparents) = @_;
1579 my @commit_lines = split '\n', $commit_text;
1580 my %co;
1582 pop @commit_lines; # Remove '\0'
1584 if (! @commit_lines) {
1585 return;
1588 my $header = shift @commit_lines;
1589 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1590 return;
1592 ($co{'id'}, my @parents) = split ' ', $header;
1593 while (my $line = shift @commit_lines) {
1594 last if $line eq "\n";
1595 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1596 $co{'tree'} = $1;
1597 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1598 push @parents, $1;
1599 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1600 $co{'author'} = $1;
1601 $co{'author_epoch'} = $2;
1602 $co{'author_tz'} = $3;
1603 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1604 $co{'author_name'} = $1;
1605 $co{'author_email'} = $2;
1606 } else {
1607 $co{'author_name'} = $co{'author'};
1609 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1610 $co{'committer'} = $1;
1611 $co{'committer_epoch'} = $2;
1612 $co{'committer_tz'} = $3;
1613 $co{'committer_name'} = $co{'committer'};
1614 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1615 $co{'committer_name'} = $1;
1616 $co{'committer_email'} = $2;
1617 } else {
1618 $co{'committer_name'} = $co{'committer'};
1622 if (!defined $co{'tree'}) {
1623 return;
1625 $co{'parents'} = \@parents;
1626 $co{'parent'} = $parents[0];
1628 foreach my $title (@commit_lines) {
1629 $title =~ s/^ //;
1630 if ($title ne "") {
1631 $co{'title'} = chop_str($title, 80, 5);
1632 # remove leading stuff of merges to make the interesting part visible
1633 if (length($title) > 50) {
1634 $title =~ s/^Automatic //;
1635 $title =~ s/^merge (of|with) /Merge ... /i;
1636 if (length($title) > 50) {
1637 $title =~ s/(http|rsync):\/\///;
1639 if (length($title) > 50) {
1640 $title =~ s/(master|www|rsync)\.//;
1642 if (length($title) > 50) {
1643 $title =~ s/kernel.org:?//;
1645 if (length($title) > 50) {
1646 $title =~ s/\/pub\/scm//;
1649 $co{'title_short'} = chop_str($title, 50, 5);
1650 last;
1653 if ($co{'title'} eq "") {
1654 $co{'title'} = $co{'title_short'} = '(no commit message)';
1656 # remove added spaces
1657 foreach my $line (@commit_lines) {
1658 $line =~ s/^ //;
1660 $co{'comment'} = \@commit_lines;
1662 my $age = time - $co{'committer_epoch'};
1663 $co{'age'} = $age;
1664 $co{'age_string'} = age_string($age);
1665 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1666 if ($age > 60*60*24*7*2) {
1667 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1668 $co{'age_string_age'} = $co{'age_string'};
1669 } else {
1670 $co{'age_string_date'} = $co{'age_string'};
1671 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1673 return %co;
1676 sub parse_commit {
1677 my ($commit_id) = @_;
1678 my %co;
1680 local $/ = "\0";
1682 open my $fd, "-|", git_cmd(), "rev-list",
1683 "--parents",
1684 "--header",
1685 "--max-count=1",
1686 $commit_id,
1687 "--",
1688 or die_error(undef, "Open git-rev-list failed");
1689 %co = parse_commit_text(<$fd>, 1);
1690 close $fd;
1692 return %co;
1695 sub parse_commits {
1696 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1697 my @cos;
1699 $maxcount ||= 1;
1700 $skip ||= 0;
1702 local $/ = "\0";
1704 open my $fd, "-|", git_cmd(), "rev-list",
1705 "--header",
1706 ($arg ? ($arg) : ()),
1707 ("--max-count=" . $maxcount),
1708 ("--skip=" . $skip),
1709 $commit_id,
1710 "--",
1711 ($filename ? ($filename) : ())
1712 or die_error(undef, "Open git-rev-list failed");
1713 while (my $line = <$fd>) {
1714 my %co = parse_commit_text($line);
1715 push @cos, \%co;
1717 close $fd;
1719 return wantarray ? @cos : \@cos;
1722 # parse ref from ref_file, given by ref_id, with given type
1723 sub parse_ref {
1724 my $ref_file = shift;
1725 my $ref_id = shift;
1726 my $type = shift || git_get_type($ref_id);
1727 my %ref_item;
1729 $ref_item{'type'} = $type;
1730 $ref_item{'id'} = $ref_id;
1731 $ref_item{'epoch'} = 0;
1732 $ref_item{'age'} = "unknown";
1733 if ($type eq "tag") {
1734 my %tag = parse_tag($ref_id);
1735 $ref_item{'comment'} = $tag{'comment'};
1736 if ($tag{'type'} eq "commit") {
1737 my %co = parse_commit($tag{'object'});
1738 $ref_item{'epoch'} = $co{'committer_epoch'};
1739 $ref_item{'age'} = $co{'age_string'};
1740 } elsif (defined($tag{'epoch'})) {
1741 my $age = time - $tag{'epoch'};
1742 $ref_item{'epoch'} = $tag{'epoch'};
1743 $ref_item{'age'} = age_string($age);
1745 $ref_item{'reftype'} = $tag{'type'};
1746 $ref_item{'name'} = $tag{'name'};
1747 $ref_item{'refid'} = $tag{'object'};
1748 } elsif ($type eq "commit"){
1749 my %co = parse_commit($ref_id);
1750 $ref_item{'reftype'} = "commit";
1751 $ref_item{'name'} = $ref_file;
1752 $ref_item{'title'} = $co{'title'};
1753 $ref_item{'refid'} = $ref_id;
1754 $ref_item{'epoch'} = $co{'committer_epoch'};
1755 $ref_item{'age'} = $co{'age_string'};
1756 } else {
1757 $ref_item{'reftype'} = $type;
1758 $ref_item{'name'} = $ref_file;
1759 $ref_item{'refid'} = $ref_id;
1762 return %ref_item;
1765 # parse line of git-diff-tree "raw" output
1766 sub parse_difftree_raw_line {
1767 my $line = shift;
1768 my %res;
1770 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1771 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1772 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1773 $res{'from_mode'} = $1;
1774 $res{'to_mode'} = $2;
1775 $res{'from_id'} = $3;
1776 $res{'to_id'} = $4;
1777 $res{'status'} = $5;
1778 $res{'similarity'} = $6;
1779 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1780 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1781 } else {
1782 $res{'file'} = unquote($7);
1785 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1786 # combined diff (for merge commit)
1787 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1788 $res{'nparents'} = length($1);
1789 $res{'from_mode'} = [ split(' ', $2) ];
1790 $res{'to_mode'} = pop @{$res{'from_mode'}};
1791 $res{'from_id'} = [ split(' ', $3) ];
1792 $res{'to_id'} = pop @{$res{'from_id'}};
1793 $res{'status'} = [ split('', $4) ];
1794 $res{'to_file'} = unquote($5);
1796 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1797 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1798 $res{'commit'} = $1;
1801 return wantarray ? %res : \%res;
1804 # parse line of git-ls-tree output
1805 sub parse_ls_tree_line ($;%) {
1806 my $line = shift;
1807 my %opts = @_;
1808 my %res;
1810 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1811 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1813 $res{'mode'} = $1;
1814 $res{'type'} = $2;
1815 $res{'hash'} = $3;
1816 if ($opts{'-z'}) {
1817 $res{'name'} = $4;
1818 } else {
1819 $res{'name'} = unquote($4);
1822 return wantarray ? %res : \%res;
1825 # generates _two_ hashes, references to which are passed as 2 and 3 argument
1826 sub parse_from_to_diffinfo {
1827 my ($diffinfo, $from, $to, @parents) = @_;
1829 if ($diffinfo->{'nparents'}) {
1830 # combined diff
1831 $from->{'file'} = [];
1832 $from->{'href'} = [];
1833 fill_from_file_info($diffinfo, @parents)
1834 unless exists $diffinfo->{'from_file'};
1835 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1836 $from->{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
1837 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
1838 $from->{'href'}[$i] = href(action=>"blob",
1839 hash_base=>$parents[$i],
1840 hash=>$diffinfo->{'from_id'}[$i],
1841 file_name=>$from->{'file'}[$i]);
1842 } else {
1843 $from->{'href'}[$i] = undef;
1846 } else {
1847 $from->{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
1848 if ($diffinfo->{'status'} ne "A") { # not new (added) file
1849 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
1850 hash=>$diffinfo->{'from_id'},
1851 file_name=>$from->{'file'});
1852 } else {
1853 delete $from->{'href'};
1857 $to->{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
1858 if (!is_deleted($diffinfo)) { # file exists in result
1859 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
1860 hash=>$diffinfo->{'to_id'},
1861 file_name=>$to->{'file'});
1862 } else {
1863 delete $to->{'href'};
1867 ## ......................................................................
1868 ## parse to array of hashes functions
1870 sub git_get_heads_list {
1871 my $limit = shift;
1872 my @headslist;
1874 open my $fd, '-|', git_cmd(), 'for-each-ref',
1875 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1876 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1877 'refs/heads'
1878 or return;
1879 while (my $line = <$fd>) {
1880 my %ref_item;
1882 chomp $line;
1883 my ($refinfo, $committerinfo) = split(/\0/, $line);
1884 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1885 my ($committer, $epoch, $tz) =
1886 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1887 $name =~ s!^refs/heads/!!;
1889 $ref_item{'name'} = $name;
1890 $ref_item{'id'} = $hash;
1891 $ref_item{'title'} = $title || '(no commit message)';
1892 $ref_item{'epoch'} = $epoch;
1893 if ($epoch) {
1894 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1895 } else {
1896 $ref_item{'age'} = "unknown";
1899 push @headslist, \%ref_item;
1901 close $fd;
1903 return wantarray ? @headslist : \@headslist;
1906 sub git_get_tags_list {
1907 my $limit = shift;
1908 my @tagslist;
1910 open my $fd, '-|', git_cmd(), 'for-each-ref',
1911 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1912 '--format=%(objectname) %(objecttype) %(refname) '.
1913 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1914 'refs/tags'
1915 or return;
1916 while (my $line = <$fd>) {
1917 my %ref_item;
1919 chomp $line;
1920 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1921 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1922 my ($creator, $epoch, $tz) =
1923 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1924 $name =~ s!^refs/tags/!!;
1926 $ref_item{'type'} = $type;
1927 $ref_item{'id'} = $id;
1928 $ref_item{'name'} = $name;
1929 if ($type eq "tag") {
1930 $ref_item{'subject'} = $title;
1931 $ref_item{'reftype'} = $reftype;
1932 $ref_item{'refid'} = $refid;
1933 } else {
1934 $ref_item{'reftype'} = $type;
1935 $ref_item{'refid'} = $id;
1938 if ($type eq "tag" || $type eq "commit") {
1939 $ref_item{'epoch'} = $epoch;
1940 if ($epoch) {
1941 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1942 } else {
1943 $ref_item{'age'} = "unknown";
1947 push @tagslist, \%ref_item;
1949 close $fd;
1951 return wantarray ? @tagslist : \@tagslist;
1954 ## ----------------------------------------------------------------------
1955 ## filesystem-related functions
1957 sub get_file_owner {
1958 my $path = shift;
1960 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1961 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1962 if (!defined $gcos) {
1963 return undef;
1965 my $owner = $gcos;
1966 $owner =~ s/[,;].*$//;
1967 return to_utf8($owner);
1970 ## ......................................................................
1971 ## mimetype related functions
1973 sub mimetype_guess_file {
1974 my $filename = shift;
1975 my $mimemap = shift;
1976 -r $mimemap or return undef;
1978 my %mimemap;
1979 open(MIME, $mimemap) or return undef;
1980 while (<MIME>) {
1981 next if m/^#/; # skip comments
1982 my ($mime, $exts) = split(/\t+/);
1983 if (defined $exts) {
1984 my @exts = split(/\s+/, $exts);
1985 foreach my $ext (@exts) {
1986 $mimemap{$ext} = $mime;
1990 close(MIME);
1992 $filename =~ /\.([^.]*)$/;
1993 return $mimemap{$1};
1996 sub mimetype_guess {
1997 my $filename = shift;
1998 my $mime;
1999 $filename =~ /\./ or return undef;
2001 if ($mimetypes_file) {
2002 my $file = $mimetypes_file;
2003 if ($file !~ m!^/!) { # if it is relative path
2004 # it is relative to project
2005 $file = "$projectroot/$project/$file";
2007 $mime = mimetype_guess_file($filename, $file);
2009 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2010 return $mime;
2013 sub blob_mimetype {
2014 my $fd = shift;
2015 my $filename = shift;
2017 if ($filename) {
2018 my $mime = mimetype_guess($filename);
2019 $mime and return $mime;
2022 # just in case
2023 return $default_blob_plain_mimetype unless $fd;
2025 if (-T $fd) {
2026 return 'text/plain' .
2027 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2028 } elsif (! $filename) {
2029 return 'application/octet-stream';
2030 } elsif ($filename =~ m/\.png$/i) {
2031 return 'image/png';
2032 } elsif ($filename =~ m/\.gif$/i) {
2033 return 'image/gif';
2034 } elsif ($filename =~ m/\.jpe?g$/i) {
2035 return 'image/jpeg';
2036 } else {
2037 return 'application/octet-stream';
2041 ## ======================================================================
2042 ## functions printing HTML: header, footer, error page
2044 sub git_header_html {
2045 my $status = shift || "200 OK";
2046 my $expires = shift;
2048 my $title = "$site_name";
2049 if (defined $project) {
2050 $title .= " - " . to_utf8($project);
2051 if (defined $action) {
2052 $title .= "/$action";
2053 if (defined $file_name) {
2054 $title .= " - " . esc_path($file_name);
2055 if ($action eq "tree" && $file_name !~ m|/$|) {
2056 $title .= "/";
2061 my $content_type;
2062 # require explicit support from the UA if we are to send the page as
2063 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2064 # we have to do this because MSIE sometimes globs '*/*', pretending to
2065 # support xhtml+xml but choking when it gets what it asked for.
2066 if (defined $cgi->http('HTTP_ACCEPT') &&
2067 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2068 $cgi->Accept('application/xhtml+xml') != 0) {
2069 $content_type = 'application/xhtml+xml';
2070 } else {
2071 $content_type = 'text/html';
2073 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2074 -status=> $status, -expires => $expires);
2075 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2076 print <<EOF;
2077 <?xml version="1.0" encoding="utf-8"?>
2078 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2079 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2080 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2081 <!-- git core binaries version $git_version -->
2082 <head>
2083 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2084 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2085 <meta name="robots" content="index, nofollow"/>
2086 <title>$title</title>
2088 # print out each stylesheet that exist
2089 if (defined $stylesheet) {
2090 #provides backwards capability for those people who define style sheet in a config file
2091 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2092 } else {
2093 foreach my $stylesheet (@stylesheets) {
2094 next unless $stylesheet;
2095 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2098 if (defined $project) {
2099 printf('<link rel="alternate" title="%s log RSS feed" '.
2100 'href="%s" type="application/rss+xml" />'."\n",
2101 esc_param($project), href(action=>"rss"));
2102 printf('<link rel="alternate" title="%s log Atom feed" '.
2103 'href="%s" type="application/atom+xml" />'."\n",
2104 esc_param($project), href(action=>"atom"));
2105 } else {
2106 printf('<link rel="alternate" title="%s projects list" '.
2107 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2108 $site_name, href(project=>undef, action=>"project_index"));
2109 printf('<link rel="alternate" title="%s projects feeds" '.
2110 'href="%s" type="text/x-opml"/>'."\n",
2111 $site_name, href(project=>undef, action=>"opml"));
2113 if (defined $favicon) {
2114 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2117 print "</head>\n" .
2118 "<body>\n";
2120 if (-f $site_header) {
2121 open (my $fd, $site_header);
2122 print <$fd>;
2123 close $fd;
2126 print "<div class=\"page_header\">\n" .
2127 $cgi->a({-href => esc_url($logo_url),
2128 -title => $logo_label},
2129 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2130 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2131 if (defined $project) {
2132 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2133 if (defined $action) {
2134 print " / $action";
2136 print "\n";
2138 print "</div>\n";
2140 my ($have_search) = gitweb_check_feature('search');
2141 if ((defined $project) && ($have_search)) {
2142 if (!defined $searchtext) {
2143 $searchtext = "";
2145 my $search_hash;
2146 if (defined $hash_base) {
2147 $search_hash = $hash_base;
2148 } elsif (defined $hash) {
2149 $search_hash = $hash;
2150 } else {
2151 $search_hash = "HEAD";
2153 $cgi->param("a", "search");
2154 $cgi->param("h", $search_hash);
2155 $cgi->param("p", $project);
2156 print $cgi->startform(-method => "get", -action => $my_uri) .
2157 "<div class=\"search\">\n" .
2158 $cgi->hidden(-name => "p") . "\n" .
2159 $cgi->hidden(-name => "a") . "\n" .
2160 $cgi->hidden(-name => "h") . "\n" .
2161 $cgi->popup_menu(-name => 'st', -default => 'commit',
2162 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2163 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2164 " search:\n",
2165 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2166 "</div>" .
2167 $cgi->end_form() . "\n";
2171 sub git_footer_html {
2172 print "<div class=\"page_footer\">\n";
2173 if (defined $project) {
2174 my $descr = git_get_project_description($project);
2175 if (defined $descr) {
2176 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2178 print $cgi->a({-href => href(action=>"rss"),
2179 -class => "rss_logo"}, "RSS") . " ";
2180 print $cgi->a({-href => href(action=>"atom"),
2181 -class => "rss_logo"}, "Atom") . "\n";
2182 } else {
2183 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2184 -class => "rss_logo"}, "OPML") . " ";
2185 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2186 -class => "rss_logo"}, "TXT") . "\n";
2188 print "</div>\n" ;
2190 if (-f $site_footer) {
2191 open (my $fd, $site_footer);
2192 print <$fd>;
2193 close $fd;
2196 print "</body>\n" .
2197 "</html>";
2200 sub die_error {
2201 my $status = shift || "403 Forbidden";
2202 my $error = shift || "Malformed query, file missing or permission denied";
2204 git_header_html($status);
2205 print <<EOF;
2206 <div class="page_body">
2207 <br /><br />
2208 $status - $error
2209 <br />
2210 </div>
2212 git_footer_html();
2213 exit;
2216 ## ----------------------------------------------------------------------
2217 ## functions printing or outputting HTML: navigation
2219 sub git_print_page_nav {
2220 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2221 $extra = '' if !defined $extra; # pager or formats
2223 my @navs = qw(summary shortlog log commit commitdiff tree);
2224 if ($suppress) {
2225 @navs = grep { $_ ne $suppress } @navs;
2228 my %arg = map { $_ => {action=>$_} } @navs;
2229 if (defined $head) {
2230 for (qw(commit commitdiff)) {
2231 $arg{$_}{'hash'} = $head;
2233 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2234 for (qw(shortlog log)) {
2235 $arg{$_}{'hash'} = $head;
2239 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2240 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2242 print "<div class=\"page_nav\">\n" .
2243 (join " | ",
2244 map { $_ eq $current ?
2245 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2246 } @navs);
2247 print "<br/>\n$extra<br/>\n" .
2248 "</div>\n";
2251 sub format_paging_nav {
2252 my ($action, $hash, $head, $page, $nrevs) = @_;
2253 my $paging_nav;
2256 if ($hash ne $head || $page) {
2257 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2258 } else {
2259 $paging_nav .= "HEAD";
2262 if ($page > 0) {
2263 $paging_nav .= " &sdot; " .
2264 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2265 -accesskey => "p", -title => "Alt-p"}, "prev");
2266 } else {
2267 $paging_nav .= " &sdot; prev";
2270 if ($nrevs >= (100 * ($page+1)-1)) {
2271 $paging_nav .= " &sdot; " .
2272 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2273 -accesskey => "n", -title => "Alt-n"}, "next");
2274 } else {
2275 $paging_nav .= " &sdot; next";
2278 return $paging_nav;
2281 ## ......................................................................
2282 ## functions printing or outputting HTML: div
2284 sub git_print_header_div {
2285 my ($action, $title, $hash, $hash_base) = @_;
2286 my %args = ();
2288 $args{'action'} = $action;
2289 $args{'hash'} = $hash if $hash;
2290 $args{'hash_base'} = $hash_base if $hash_base;
2292 print "<div class=\"header\">\n" .
2293 $cgi->a({-href => href(%args), -class => "title"},
2294 $title ? $title : $action) .
2295 "\n</div>\n";
2298 #sub git_print_authorship (\%) {
2299 sub git_print_authorship {
2300 my $co = shift;
2302 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2303 print "<div class=\"author_date\">" .
2304 esc_html($co->{'author_name'}) .
2305 " [$ad{'rfc2822'}";
2306 if ($ad{'hour_local'} < 6) {
2307 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2308 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2309 } else {
2310 printf(" (%02d:%02d %s)",
2311 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2313 print "]</div>\n";
2316 sub git_print_page_path {
2317 my $name = shift;
2318 my $type = shift;
2319 my $hb = shift;
2322 print "<div class=\"page_path\">";
2323 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2324 -title => 'tree root'}, to_utf8("[$project]"));
2325 print " / ";
2326 if (defined $name) {
2327 my @dirname = split '/', $name;
2328 my $basename = pop @dirname;
2329 my $fullname = '';
2331 foreach my $dir (@dirname) {
2332 $fullname .= ($fullname ? '/' : '') . $dir;
2333 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2334 hash_base=>$hb),
2335 -title => $fullname}, esc_path($dir));
2336 print " / ";
2338 if (defined $type && $type eq 'blob') {
2339 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2340 hash_base=>$hb),
2341 -title => $name}, esc_path($basename));
2342 } elsif (defined $type && $type eq 'tree') {
2343 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2344 hash_base=>$hb),
2345 -title => $name}, esc_path($basename));
2346 print " / ";
2347 } else {
2348 print esc_path($basename);
2351 print "<br/></div>\n";
2354 # sub git_print_log (\@;%) {
2355 sub git_print_log ($;%) {
2356 my $log = shift;
2357 my %opts = @_;
2359 if ($opts{'-remove_title'}) {
2360 # remove title, i.e. first line of log
2361 shift @$log;
2363 # remove leading empty lines
2364 while (defined $log->[0] && $log->[0] eq "") {
2365 shift @$log;
2368 # print log
2369 my $signoff = 0;
2370 my $empty = 0;
2371 foreach my $line (@$log) {
2372 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2373 $signoff = 1;
2374 $empty = 0;
2375 if (! $opts{'-remove_signoff'}) {
2376 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2377 next;
2378 } else {
2379 # remove signoff lines
2380 next;
2382 } else {
2383 $signoff = 0;
2386 # print only one empty line
2387 # do not print empty line after signoff
2388 if ($line eq "") {
2389 next if ($empty || $signoff);
2390 $empty = 1;
2391 } else {
2392 $empty = 0;
2395 print format_log_line_html($line) . "<br/>\n";
2398 if ($opts{'-final_empty_line'}) {
2399 # end with single empty line
2400 print "<br/>\n" unless $empty;
2404 # return link target (what link points to)
2405 sub git_get_link_target {
2406 my $hash = shift;
2407 my $link_target;
2409 # read link
2410 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2411 or return;
2413 local $/;
2414 $link_target = <$fd>;
2416 close $fd
2417 or return;
2419 return $link_target;
2422 # given link target, and the directory (basedir) the link is in,
2423 # return target of link relative to top directory (top tree);
2424 # return undef if it is not possible (including absolute links).
2425 sub normalize_link_target {
2426 my ($link_target, $basedir, $hash_base) = @_;
2428 # we can normalize symlink target only if $hash_base is provided
2429 return unless $hash_base;
2431 # absolute symlinks (beginning with '/') cannot be normalized
2432 return if (substr($link_target, 0, 1) eq '/');
2434 # normalize link target to path from top (root) tree (dir)
2435 my $path;
2436 if ($basedir) {
2437 $path = $basedir . '/' . $link_target;
2438 } else {
2439 # we are in top (root) tree (dir)
2440 $path = $link_target;
2443 # remove //, /./, and /../
2444 my @path_parts;
2445 foreach my $part (split('/', $path)) {
2446 # discard '.' and ''
2447 next if (!$part || $part eq '.');
2448 # handle '..'
2449 if ($part eq '..') {
2450 if (@path_parts) {
2451 pop @path_parts;
2452 } else {
2453 # link leads outside repository (outside top dir)
2454 return;
2456 } else {
2457 push @path_parts, $part;
2460 $path = join('/', @path_parts);
2462 return $path;
2465 # print tree entry (row of git_tree), but without encompassing <tr> element
2466 sub git_print_tree_entry {
2467 my ($t, $basedir, $hash_base, $have_blame) = @_;
2469 my %base_key = ();
2470 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2472 # The format of a table row is: mode list link. Where mode is
2473 # the mode of the entry, list is the name of the entry, an href,
2474 # and link is the action links of the entry.
2476 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2477 if ($t->{'type'} eq "blob") {
2478 print "<td class=\"list\">" .
2479 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2480 file_name=>"$basedir$t->{'name'}", %base_key),
2481 -class => "list"}, esc_path($t->{'name'}));
2482 if (S_ISLNK(oct $t->{'mode'})) {
2483 my $link_target = git_get_link_target($t->{'hash'});
2484 if ($link_target) {
2485 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2486 if (defined $norm_target) {
2487 print " -> " .
2488 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2489 file_name=>$norm_target),
2490 -title => $norm_target}, esc_path($link_target));
2491 } else {
2492 print " -> " . esc_path($link_target);
2496 print "</td>\n";
2497 print "<td class=\"link\">";
2498 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2499 file_name=>"$basedir$t->{'name'}", %base_key)},
2500 "blob");
2501 if ($have_blame) {
2502 print " | " .
2503 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2504 file_name=>"$basedir$t->{'name'}", %base_key)},
2505 "blame");
2507 if (defined $hash_base) {
2508 print " | " .
2509 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2510 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2511 "history");
2513 print " | " .
2514 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2515 file_name=>"$basedir$t->{'name'}")},
2516 "raw");
2517 print "</td>\n";
2519 } elsif ($t->{'type'} eq "tree") {
2520 print "<td class=\"list\">";
2521 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2522 file_name=>"$basedir$t->{'name'}", %base_key)},
2523 esc_path($t->{'name'}));
2524 print "</td>\n";
2525 print "<td class=\"link\">";
2526 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2527 file_name=>"$basedir$t->{'name'}", %base_key)},
2528 "tree");
2529 if (defined $hash_base) {
2530 print " | " .
2531 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2532 file_name=>"$basedir$t->{'name'}")},
2533 "history");
2535 print "</td>\n";
2539 ## ......................................................................
2540 ## functions printing large fragments of HTML
2542 sub fill_from_file_info {
2543 my ($diff, @parents) = @_;
2545 $diff->{'from_file'} = [ ];
2546 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2547 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2548 if ($diff->{'status'}[$i] eq 'R' ||
2549 $diff->{'status'}[$i] eq 'C') {
2550 $diff->{'from_file'}[$i] =
2551 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2555 return $diff;
2558 # parameters can be strings, or references to arrays of strings
2559 sub from_ids_eq {
2560 my ($a, $b) = @_;
2562 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2563 for (my $i = 0; $i < @$a; ++$i) {
2564 return 0 unless ($a->[$i] eq $b->[$i]);
2566 return 1;
2567 } elsif (!ref($a) && !ref($b)) {
2568 return $a eq $b;
2569 } else {
2570 return 0;
2574 sub is_deleted {
2575 my $diffinfo = shift;
2577 return $diffinfo->{'to_id'} eq ('0' x 40);
2580 sub git_difftree_body {
2581 my ($difftree, $hash, @parents) = @_;
2582 my ($parent) = $parents[0];
2583 my ($have_blame) = gitweb_check_feature('blame');
2584 print "<div class=\"list_head\">\n";
2585 if ($#{$difftree} > 10) {
2586 print(($#{$difftree} + 1) . " files changed:\n");
2588 print "</div>\n";
2590 print "<table class=\"" .
2591 (@parents > 1 ? "combined " : "") .
2592 "diff_tree\">\n";
2594 # header only for combined diff in 'commitdiff' view
2595 my $has_header = @parents > 1 && $action eq 'commitdiff';
2596 if ($has_header) {
2597 # table header
2598 print "<thead><tr>\n" .
2599 "<th></th><th></th>\n"; # filename, patchN link
2600 for (my $i = 0; $i < @parents; $i++) {
2601 my $par = $parents[$i];
2602 print "<th>" .
2603 $cgi->a({-href => href(action=>"commitdiff",
2604 hash=>$hash, hash_parent=>$par),
2605 -title => 'commitdiff to parent number ' .
2606 ($i+1) . ': ' . substr($par,0,7)},
2607 $i+1) .
2608 "&nbsp;</th>\n";
2610 print "</tr></thead>\n<tbody>\n";
2613 my $alternate = 1;
2614 my $patchno = 0;
2615 foreach my $line (@{$difftree}) {
2616 my $diff;
2617 if (ref($line) eq "HASH") {
2618 # pre-parsed (or generated by hand)
2619 $diff = $line;
2620 } else {
2621 $diff = parse_difftree_raw_line($line);
2624 if ($alternate) {
2625 print "<tr class=\"dark\">\n";
2626 } else {
2627 print "<tr class=\"light\">\n";
2629 $alternate ^= 1;
2631 if (exists $diff->{'nparents'}) { # combined diff
2633 fill_from_file_info($diff, @parents)
2634 unless exists $diff->{'from_file'};
2636 if (!is_deleted($diff)) {
2637 # file exists in the result (child) commit
2638 print "<td>" .
2639 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2640 file_name=>$diff->{'to_file'},
2641 hash_base=>$hash),
2642 -class => "list"}, esc_path($diff->{'to_file'})) .
2643 "</td>\n";
2644 } else {
2645 print "<td>" .
2646 esc_path($diff->{'to_file'}) .
2647 "</td>\n";
2650 if ($action eq 'commitdiff') {
2651 # link to patch
2652 $patchno++;
2653 print "<td class=\"link\">" .
2654 $cgi->a({-href => "#patch$patchno"}, "patch") .
2655 " | " .
2656 "</td>\n";
2659 my $has_history = 0;
2660 my $not_deleted = 0;
2661 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2662 my $hash_parent = $parents[$i];
2663 my $from_hash = $diff->{'from_id'}[$i];
2664 my $from_path = $diff->{'from_file'}[$i];
2665 my $status = $diff->{'status'}[$i];
2667 $has_history ||= ($status ne 'A');
2668 $not_deleted ||= ($status ne 'D');
2670 if ($status eq 'A') {
2671 print "<td class=\"link\" align=\"right\"> | </td>\n";
2672 } elsif ($status eq 'D') {
2673 print "<td class=\"link\">" .
2674 $cgi->a({-href => href(action=>"blob",
2675 hash_base=>$hash,
2676 hash=>$from_hash,
2677 file_name=>$from_path)},
2678 "blob" . ($i+1)) .
2679 " | </td>\n";
2680 } else {
2681 if ($diff->{'to_id'} eq $from_hash) {
2682 print "<td class=\"link nochange\">";
2683 } else {
2684 print "<td class=\"link\">";
2686 print $cgi->a({-href => href(action=>"blobdiff",
2687 hash=>$diff->{'to_id'},
2688 hash_parent=>$from_hash,
2689 hash_base=>$hash,
2690 hash_parent_base=>$hash_parent,
2691 file_name=>$diff->{'to_file'},
2692 file_parent=>$from_path)},
2693 "diff" . ($i+1)) .
2694 " | </td>\n";
2698 print "<td class=\"link\">";
2699 if ($not_deleted) {
2700 print $cgi->a({-href => href(action=>"blob",
2701 hash=>$diff->{'to_id'},
2702 file_name=>$diff->{'to_file'},
2703 hash_base=>$hash)},
2704 "blob");
2705 print " | " if ($has_history);
2707 if ($has_history) {
2708 print $cgi->a({-href => href(action=>"history",
2709 file_name=>$diff->{'to_file'},
2710 hash_base=>$hash)},
2711 "history");
2713 print "</td>\n";
2715 print "</tr>\n";
2716 next; # instead of 'else' clause, to avoid extra indent
2718 # else ordinary diff
2720 my ($to_mode_oct, $to_mode_str, $to_file_type);
2721 my ($from_mode_oct, $from_mode_str, $from_file_type);
2722 if ($diff->{'to_mode'} ne ('0' x 6)) {
2723 $to_mode_oct = oct $diff->{'to_mode'};
2724 if (S_ISREG($to_mode_oct)) { # only for regular file
2725 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2727 $to_file_type = file_type($diff->{'to_mode'});
2729 if ($diff->{'from_mode'} ne ('0' x 6)) {
2730 $from_mode_oct = oct $diff->{'from_mode'};
2731 if (S_ISREG($to_mode_oct)) { # only for regular file
2732 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2734 $from_file_type = file_type($diff->{'from_mode'});
2737 if ($diff->{'status'} eq "A") { # created
2738 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2739 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2740 $mode_chng .= "]</span>";
2741 print "<td>";
2742 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2743 hash_base=>$hash, file_name=>$diff->{'file'}),
2744 -class => "list"}, esc_path($diff->{'file'}));
2745 print "</td>\n";
2746 print "<td>$mode_chng</td>\n";
2747 print "<td class=\"link\">";
2748 if ($action eq 'commitdiff') {
2749 # link to patch
2750 $patchno++;
2751 print $cgi->a({-href => "#patch$patchno"}, "patch");
2752 print " | ";
2754 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2755 hash_base=>$hash, file_name=>$diff->{'file'})},
2756 "blob");
2757 print "</td>\n";
2759 } elsif ($diff->{'status'} eq "D") { # deleted
2760 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2761 print "<td>";
2762 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2763 hash_base=>$parent, file_name=>$diff->{'file'}),
2764 -class => "list"}, esc_path($diff->{'file'}));
2765 print "</td>\n";
2766 print "<td>$mode_chng</td>\n";
2767 print "<td class=\"link\">";
2768 if ($action eq 'commitdiff') {
2769 # link to patch
2770 $patchno++;
2771 print $cgi->a({-href => "#patch$patchno"}, "patch");
2772 print " | ";
2774 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2775 hash_base=>$parent, file_name=>$diff->{'file'})},
2776 "blob") . " | ";
2777 if ($have_blame) {
2778 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2779 file_name=>$diff->{'file'})},
2780 "blame") . " | ";
2782 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2783 file_name=>$diff->{'file'})},
2784 "history");
2785 print "</td>\n";
2787 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
2788 my $mode_chnge = "";
2789 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2790 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2791 if ($from_file_type ne $to_file_type) {
2792 $mode_chnge .= " from $from_file_type to $to_file_type";
2794 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2795 if ($from_mode_str && $to_mode_str) {
2796 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2797 } elsif ($to_mode_str) {
2798 $mode_chnge .= " mode: $to_mode_str";
2801 $mode_chnge .= "]</span>\n";
2803 print "<td>";
2804 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2805 hash_base=>$hash, file_name=>$diff->{'file'}),
2806 -class => "list"}, esc_path($diff->{'file'}));
2807 print "</td>\n";
2808 print "<td>$mode_chnge</td>\n";
2809 print "<td class=\"link\">";
2810 if ($action eq 'commitdiff') {
2811 # link to patch
2812 $patchno++;
2813 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2814 " | ";
2815 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2816 # "commit" view and modified file (not onlu mode changed)
2817 print $cgi->a({-href => href(action=>"blobdiff",
2818 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2819 hash_base=>$hash, hash_parent_base=>$parent,
2820 file_name=>$diff->{'file'})},
2821 "diff") .
2822 " | ";
2824 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2825 hash_base=>$hash, file_name=>$diff->{'file'})},
2826 "blob") . " | ";
2827 if ($have_blame) {
2828 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2829 file_name=>$diff->{'file'})},
2830 "blame") . " | ";
2832 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2833 file_name=>$diff->{'file'})},
2834 "history");
2835 print "</td>\n";
2837 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
2838 my %status_name = ('R' => 'moved', 'C' => 'copied');
2839 my $nstatus = $status_name{$diff->{'status'}};
2840 my $mode_chng = "";
2841 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2842 # mode also for directories, so we cannot use $to_mode_str
2843 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2845 print "<td>" .
2846 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2847 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
2848 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
2849 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2850 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2851 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
2852 -class => "list"}, esc_path($diff->{'from_file'})) .
2853 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2854 "<td class=\"link\">";
2855 if ($action eq 'commitdiff') {
2856 # link to patch
2857 $patchno++;
2858 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2859 " | ";
2860 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2861 # "commit" view and modified file (not only pure rename or copy)
2862 print $cgi->a({-href => href(action=>"blobdiff",
2863 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2864 hash_base=>$hash, hash_parent_base=>$parent,
2865 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
2866 "diff") .
2867 " | ";
2869 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2870 hash_base=>$parent, file_name=>$diff->{'to_file'})},
2871 "blob") . " | ";
2872 if ($have_blame) {
2873 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2874 file_name=>$diff->{'to_file'})},
2875 "blame") . " | ";
2877 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2878 file_name=>$diff->{'to_file'})},
2879 "history");
2880 print "</td>\n";
2882 } # we should not encounter Unmerged (U) or Unknown (X) status
2883 print "</tr>\n";
2885 print "</tbody>" if $has_header;
2886 print "</table>\n";
2889 sub git_patchset_body {
2890 my ($fd, $difftree, $hash, @hash_parents) = @_;
2891 my ($hash_parent) = $hash_parents[0];
2893 my $patch_idx = 0;
2894 my $patch_number = 0;
2895 my $patch_line;
2896 my $diffinfo;
2897 my (%from, %to);
2899 print "<div class=\"patchset\">\n";
2901 # skip to first patch
2902 while ($patch_line = <$fd>) {
2903 chomp $patch_line;
2905 last if ($patch_line =~ m/^diff /);
2908 PATCH:
2909 while ($patch_line) {
2910 my @diff_header;
2911 my ($from_id, $to_id);
2913 # git diff header
2914 #assert($patch_line =~ m/^diff /) if DEBUG;
2915 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2916 $patch_number++;
2917 push @diff_header, $patch_line;
2919 # extended diff header
2920 EXTENDED_HEADER:
2921 while ($patch_line = <$fd>) {
2922 chomp $patch_line;
2924 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
2926 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2927 $from_id = $1;
2928 $to_id = $2;
2929 } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2930 $from_id = [ split(',', $1) ];
2931 $to_id = $2;
2934 push @diff_header, $patch_line;
2936 my $last_patch_line = $patch_line;
2938 # check if current patch belong to current raw line
2939 # and parse raw git-diff line if needed
2940 if (defined $diffinfo &&
2941 defined $from_id && defined $to_id &&
2942 from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
2943 $diffinfo->{'to_id'} eq $to_id) {
2944 # this is continuation of a split patch
2945 print "<div class=\"patch cont\">\n";
2946 } else {
2947 # advance raw git-diff output if needed
2948 $patch_idx++ if defined $diffinfo;
2950 # read and prepare patch information
2951 if (ref($difftree->[$patch_idx]) eq "HASH") {
2952 # pre-parsed (or generated by hand)
2953 $diffinfo = $difftree->[$patch_idx];
2954 } else {
2955 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2957 # modifies %from, %to hashes
2958 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
2959 if ($diffinfo->{'nparents'}) {
2960 # combined diff
2961 $from{'file'} = [];
2962 $from{'href'} = [];
2963 fill_from_file_info($diffinfo, @hash_parents)
2964 unless exists $diffinfo->{'from_file'};
2965 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2966 $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2967 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2968 $from{'href'}[$i] = href(action=>"blob",
2969 hash_base=>$hash_parents[$i],
2970 hash=>$diffinfo->{'from_id'}[$i],
2971 file_name=>$from{'file'}[$i]);
2972 } else {
2973 $from{'href'}[$i] = undef;
2976 } else {
2977 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2978 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2979 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2980 hash=>$diffinfo->{'from_id'},
2981 file_name=>$from{'file'});
2982 } else {
2983 delete $from{'href'};
2987 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2988 if (!is_deleted($diffinfo)) { # file exists in result
2989 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2990 hash=>$diffinfo->{'to_id'},
2991 file_name=>$to{'file'});
2992 } else {
2993 delete $to{'href'};
2995 # this is first patch for raw difftree line with $patch_idx index
2996 # we index @$difftree array from 0, but number patches from 1
2997 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3000 # print "git diff" header
3001 $patch_line = shift @diff_header;
3002 print format_git_diff_header_line($patch_line, $diffinfo,
3003 \%from, \%to);
3005 # print extended diff header
3006 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
3007 EXTENDED_HEADER:
3008 foreach $patch_line (@diff_header) {
3009 print format_extended_diff_header_line($patch_line, $diffinfo,
3010 \%from, \%to);
3012 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
3014 # from-file/to-file diff header
3015 $patch_line = $last_patch_line;
3016 if (! $patch_line) {
3017 print "</div>\n"; # class="patch"
3018 last PATCH;
3020 next PATCH if ($patch_line =~ m/^diff /);
3021 #assert($patch_line =~ m/^---/) if DEBUG;
3022 #assert($patch_line eq $last_patch_line) if DEBUG;
3024 $patch_line = <$fd>;
3025 chomp $patch_line;
3026 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3028 print format_diff_from_to_header($last_patch_line, $patch_line,
3029 $diffinfo, \%from, \%to);
3031 # the patch itself
3032 LINE:
3033 while ($patch_line = <$fd>) {
3034 chomp $patch_line;
3036 next PATCH if ($patch_line =~ m/^diff /);
3038 print format_diff_line($patch_line, \%from, \%to);
3041 } continue {
3042 print "</div>\n"; # class="patch"
3045 if ($patch_number == 0) {
3046 if (@hash_parents > 1) {
3047 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3048 } else {
3049 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3053 print "</div>\n"; # class="patchset"
3056 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3058 sub git_project_list_body {
3059 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3061 my ($check_forks) = gitweb_check_feature('forks');
3063 my @projects;
3064 foreach my $pr (@$projlist) {
3065 my (@aa) = git_get_last_activity($pr->{'path'});
3066 unless (@aa) {
3067 next;
3069 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3070 if (!defined $pr->{'descr'}) {
3071 my $descr = git_get_project_description($pr->{'path'}) || "";
3072 $pr->{'descr_long'} = to_utf8($descr);
3073 $pr->{'descr'} = chop_str($descr, 25, 5);
3075 if (!defined $pr->{'owner'}) {
3076 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
3078 if ($check_forks) {
3079 my $pname = $pr->{'path'};
3080 if (($pname =~ s/\.git$//) &&
3081 ($pname !~ /\/$/) &&
3082 (-d "$projectroot/$pname")) {
3083 $pr->{'forks'} = "-d $projectroot/$pname";
3085 else {
3086 $pr->{'forks'} = 0;
3089 push @projects, $pr;
3092 $order ||= $default_projects_order;
3093 $from = 0 unless defined $from;
3094 $to = $#projects if (!defined $to || $#projects < $to);
3096 print "<table class=\"project_list\">\n";
3097 unless ($no_header) {
3098 print "<tr>\n";
3099 if ($check_forks) {
3100 print "<th></th>\n";
3102 if ($order eq "project") {
3103 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3104 print "<th>Project</th>\n";
3105 } else {
3106 print "<th>" .
3107 $cgi->a({-href => href(project=>undef, order=>'project'),
3108 -class => "header"}, "Project") .
3109 "</th>\n";
3111 if ($order eq "descr") {
3112 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3113 print "<th>Description</th>\n";
3114 } else {
3115 print "<th>" .
3116 $cgi->a({-href => href(project=>undef, order=>'descr'),
3117 -class => "header"}, "Description") .
3118 "</th>\n";
3120 if ($order eq "owner") {
3121 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3122 print "<th>Owner</th>\n";
3123 } else {
3124 print "<th>" .
3125 $cgi->a({-href => href(project=>undef, order=>'owner'),
3126 -class => "header"}, "Owner") .
3127 "</th>\n";
3129 if ($order eq "age") {
3130 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3131 print "<th>Last Change</th>\n";
3132 } else {
3133 print "<th>" .
3134 $cgi->a({-href => href(project=>undef, order=>'age'),
3135 -class => "header"}, "Last Change") .
3136 "</th>\n";
3138 print "<th></th>\n" .
3139 "</tr>\n";
3141 my $alternate = 1;
3142 for (my $i = $from; $i <= $to; $i++) {
3143 my $pr = $projects[$i];
3144 if ($alternate) {
3145 print "<tr class=\"dark\">\n";
3146 } else {
3147 print "<tr class=\"light\">\n";
3149 $alternate ^= 1;
3150 if ($check_forks) {
3151 print "<td>";
3152 if ($pr->{'forks'}) {
3153 print "<!-- $pr->{'forks'} -->\n";
3154 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3156 print "</td>\n";
3158 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3159 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3160 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3161 -class => "list", -title => $pr->{'descr_long'}},
3162 esc_html($pr->{'descr'})) . "</td>\n" .
3163 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
3164 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3165 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3166 "<td class=\"link\">" .
3167 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3168 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3169 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3170 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3171 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3172 "</td>\n" .
3173 "</tr>\n";
3175 if (defined $extra) {
3176 print "<tr>\n";
3177 if ($check_forks) {
3178 print "<td></td>\n";
3180 print "<td colspan=\"5\">$extra</td>\n" .
3181 "</tr>\n";
3183 print "</table>\n";
3186 sub git_shortlog_body {
3187 # uses global variable $project
3188 my ($commitlist, $from, $to, $refs, $extra) = @_;
3190 my $have_snapshot = gitweb_have_snapshot();
3192 $from = 0 unless defined $from;
3193 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3195 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3196 my $alternate = 1;
3197 for (my $i = $from; $i <= $to; $i++) {
3198 my %co = %{$commitlist->[$i]};
3199 my $commit = $co{'id'};
3200 my $ref = format_ref_marker($refs, $commit);
3201 if ($alternate) {
3202 print "<tr class=\"dark\">\n";
3203 } else {
3204 print "<tr class=\"light\">\n";
3206 $alternate ^= 1;
3207 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3208 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3209 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3210 "<td>";
3211 print format_subject_html($co{'title'}, $co{'title_short'},
3212 href(action=>"commit", hash=>$commit), $ref);
3213 print "</td>\n" .
3214 "<td class=\"link\">" .
3215 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3216 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3217 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3218 if ($have_snapshot) {
3219 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
3221 print "</td>\n" .
3222 "</tr>\n";
3224 if (defined $extra) {
3225 print "<tr>\n" .
3226 "<td colspan=\"4\">$extra</td>\n" .
3227 "</tr>\n";
3229 print "</table>\n";
3232 sub git_history_body {
3233 # Warning: assumes constant type (blob or tree) during history
3234 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3236 $from = 0 unless defined $from;
3237 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3239 print "<table class=\"history\" cellspacing=\"0\">\n";
3240 my $alternate = 1;
3241 for (my $i = $from; $i <= $to; $i++) {
3242 my %co = %{$commitlist->[$i]};
3243 if (!%co) {
3244 next;
3246 my $commit = $co{'id'};
3248 my $ref = format_ref_marker($refs, $commit);
3250 if ($alternate) {
3251 print "<tr class=\"dark\">\n";
3252 } else {
3253 print "<tr class=\"light\">\n";
3255 $alternate ^= 1;
3256 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3257 # shortlog uses chop_str($co{'author_name'}, 10)
3258 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3259 "<td>";
3260 # originally git_history used chop_str($co{'title'}, 50)
3261 print format_subject_html($co{'title'}, $co{'title_short'},
3262 href(action=>"commit", hash=>$commit), $ref);
3263 print "</td>\n" .
3264 "<td class=\"link\">" .
3265 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3266 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3268 if ($ftype eq 'blob') {
3269 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3270 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3271 if (defined $blob_current && defined $blob_parent &&
3272 $blob_current ne $blob_parent) {
3273 print " | " .
3274 $cgi->a({-href => href(action=>"blobdiff",
3275 hash=>$blob_current, hash_parent=>$blob_parent,
3276 hash_base=>$hash_base, hash_parent_base=>$commit,
3277 file_name=>$file_name)},
3278 "diff to current");
3281 print "</td>\n" .
3282 "</tr>\n";
3284 if (defined $extra) {
3285 print "<tr>\n" .
3286 "<td colspan=\"4\">$extra</td>\n" .
3287 "</tr>\n";
3289 print "</table>\n";
3292 sub git_tags_body {
3293 # uses global variable $project
3294 my ($taglist, $from, $to, $extra) = @_;
3295 $from = 0 unless defined $from;
3296 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3298 print "<table class=\"tags\" cellspacing=\"0\">\n";
3299 my $alternate = 1;
3300 for (my $i = $from; $i <= $to; $i++) {
3301 my $entry = $taglist->[$i];
3302 my %tag = %$entry;
3303 my $comment = $tag{'subject'};
3304 my $comment_short;
3305 if (defined $comment) {
3306 $comment_short = chop_str($comment, 30, 5);
3308 if ($alternate) {
3309 print "<tr class=\"dark\">\n";
3310 } else {
3311 print "<tr class=\"light\">\n";
3313 $alternate ^= 1;
3314 if (defined $tag{'age'}) {
3315 print "<td><i>$tag{'age'}</i></td>\n";
3316 } else {
3317 print "<td></td>\n";
3319 print "<td>" .
3320 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3321 -class => "list name"}, esc_html($tag{'name'})) .
3322 "</td>\n" .
3323 "<td>";
3324 if (defined $comment) {
3325 print format_subject_html($comment, $comment_short,
3326 href(action=>"tag", hash=>$tag{'id'}));
3328 print "</td>\n" .
3329 "<td class=\"selflink\">";
3330 if ($tag{'type'} eq "tag") {
3331 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3332 } else {
3333 print "&nbsp;";
3335 print "</td>\n" .
3336 "<td class=\"link\">" . " | " .
3337 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3338 if ($tag{'reftype'} eq "commit") {
3339 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3340 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3341 } elsif ($tag{'reftype'} eq "blob") {
3342 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3344 print "</td>\n" .
3345 "</tr>";
3347 if (defined $extra) {
3348 print "<tr>\n" .
3349 "<td colspan=\"5\">$extra</td>\n" .
3350 "</tr>\n";
3352 print "</table>\n";
3355 sub git_heads_body {
3356 # uses global variable $project
3357 my ($headlist, $head, $from, $to, $extra) = @_;
3358 $from = 0 unless defined $from;
3359 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3361 print "<table class=\"heads\" cellspacing=\"0\">\n";
3362 my $alternate = 1;
3363 for (my $i = $from; $i <= $to; $i++) {
3364 my $entry = $headlist->[$i];
3365 my %ref = %$entry;
3366 my $curr = $ref{'id'} eq $head;
3367 if ($alternate) {
3368 print "<tr class=\"dark\">\n";
3369 } else {
3370 print "<tr class=\"light\">\n";
3372 $alternate ^= 1;
3373 print "<td><i>$ref{'age'}</i></td>\n" .
3374 ($curr ? "<td class=\"current_head\">" : "<td>") .
3375 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3376 -class => "list name"},esc_html($ref{'name'})) .
3377 "</td>\n" .
3378 "<td class=\"link\">" .
3379 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3380 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3381 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3382 "</td>\n" .
3383 "</tr>";
3385 if (defined $extra) {
3386 print "<tr>\n" .
3387 "<td colspan=\"3\">$extra</td>\n" .
3388 "</tr>\n";
3390 print "</table>\n";
3393 sub git_search_grep_body {
3394 my ($commitlist, $from, $to, $extra) = @_;
3395 $from = 0 unless defined $from;
3396 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3398 print "<table class=\"grep\" cellspacing=\"0\">\n";
3399 my $alternate = 1;
3400 for (my $i = $from; $i <= $to; $i++) {
3401 my %co = %{$commitlist->[$i]};
3402 if (!%co) {
3403 next;
3405 my $commit = $co{'id'};
3406 if ($alternate) {
3407 print "<tr class=\"dark\">\n";
3408 } else {
3409 print "<tr class=\"light\">\n";
3411 $alternate ^= 1;
3412 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3413 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3414 "<td>" .
3415 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3416 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3417 my $comment = $co{'comment'};
3418 foreach my $line (@$comment) {
3419 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3420 my $lead = esc_html($1) || "";
3421 $lead = chop_str($lead, 30, 10);
3422 my $match = esc_html($2) || "";
3423 my $trail = esc_html($3) || "";
3424 $trail = chop_str($trail, 30, 10);
3425 my $text = "$lead<span class=\"match\">$match</span>$trail";
3426 print chop_str($text, 80, 5) . "<br/>\n";
3429 print "</td>\n" .
3430 "<td class=\"link\">" .
3431 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3432 " | " .
3433 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3434 print "</td>\n" .
3435 "</tr>\n";
3437 if (defined $extra) {
3438 print "<tr>\n" .
3439 "<td colspan=\"3\">$extra</td>\n" .
3440 "</tr>\n";
3442 print "</table>\n";
3445 ## ======================================================================
3446 ## ======================================================================
3447 ## actions
3449 sub git_project_list {
3450 my $order = $cgi->param('o');
3451 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3452 die_error(undef, "Unknown order parameter");
3455 my @list = git_get_projects_list();
3456 if (!@list) {
3457 die_error(undef, "No projects found");
3460 git_header_html();
3461 if (-f $home_text) {
3462 print "<div class=\"index_include\">\n";
3463 open (my $fd, $home_text);
3464 print <$fd>;
3465 close $fd;
3466 print "</div>\n";
3468 git_project_list_body(\@list, $order);
3469 git_footer_html();
3472 sub git_forks {
3473 my $order = $cgi->param('o');
3474 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3475 die_error(undef, "Unknown order parameter");
3478 my @list = git_get_projects_list($project);
3479 if (!@list) {
3480 die_error(undef, "No forks found");
3483 git_header_html();
3484 git_print_page_nav('','');
3485 git_print_header_div('summary', "$project forks");
3486 git_project_list_body(\@list, $order);
3487 git_footer_html();
3490 sub git_project_index {
3491 my @projects = git_get_projects_list($project);
3493 print $cgi->header(
3494 -type => 'text/plain',
3495 -charset => 'utf-8',
3496 -content_disposition => 'inline; filename="index.aux"');
3498 foreach my $pr (@projects) {
3499 if (!exists $pr->{'owner'}) {
3500 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}");
3503 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3504 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3505 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3506 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3507 $path =~ s/ /\+/g;
3508 $owner =~ s/ /\+/g;
3510 print "$path $owner\n";
3514 sub git_summary {
3515 my $descr = git_get_project_description($project) || "none";
3516 my %co = parse_commit("HEAD");
3517 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3518 my $head = $co{'id'};
3520 my $owner = git_get_project_owner($project);
3522 my $refs = git_get_references();
3523 # These get_*_list functions return one more to allow us to see if
3524 # there are more ...
3525 my @taglist = git_get_tags_list(16);
3526 my @headlist = git_get_heads_list(16);
3527 my @forklist;
3528 my ($check_forks) = gitweb_check_feature('forks');
3530 if ($check_forks) {
3531 @forklist = git_get_projects_list($project);
3534 git_header_html();
3535 git_print_page_nav('summary','', $head);
3537 print "<div class=\"title\">&nbsp;</div>\n";
3538 print "<table cellspacing=\"0\">\n" .
3539 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3540 "<tr><td>owner</td><td>$owner</td></tr>\n";
3541 if (defined $cd{'rfc2822'}) {
3542 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3545 # use per project git URL list in $projectroot/$project/cloneurl
3546 # or make project git URL from git base URL and project name
3547 my $url_tag = "URL";
3548 my @url_list = git_get_project_url_list($project);
3549 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3550 foreach my $git_url (@url_list) {
3551 next unless $git_url;
3552 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3553 $url_tag = "";
3555 print "</table>\n";
3557 if (-s "$projectroot/$project/README.html") {
3558 if (open my $fd, "$projectroot/$project/README.html") {
3559 print "<div class=\"title\">readme</div>\n";
3560 print $_ while (<$fd>);
3561 close $fd;
3565 # we need to request one more than 16 (0..15) to check if
3566 # those 16 are all
3567 my @commitlist = $head ? parse_commits($head, 17) : ();
3568 if (@commitlist) {
3569 git_print_header_div('shortlog');
3570 git_shortlog_body(\@commitlist, 0, 15, $refs,
3571 $#commitlist <= 15 ? undef :
3572 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3575 if (@taglist) {
3576 git_print_header_div('tags');
3577 git_tags_body(\@taglist, 0, 15,
3578 $#taglist <= 15 ? undef :
3579 $cgi->a({-href => href(action=>"tags")}, "..."));
3582 if (@headlist) {
3583 git_print_header_div('heads');
3584 git_heads_body(\@headlist, $head, 0, 15,
3585 $#headlist <= 15 ? undef :
3586 $cgi->a({-href => href(action=>"heads")}, "..."));
3589 if (@forklist) {
3590 git_print_header_div('forks');
3591 git_project_list_body(\@forklist, undef, 0, 15,
3592 $#forklist <= 15 ? undef :
3593 $cgi->a({-href => href(action=>"forks")}, "..."),
3594 'noheader');
3597 git_footer_html();
3600 sub git_tag {
3601 my $head = git_get_head_hash($project);
3602 git_header_html();
3603 git_print_page_nav('','', $head,undef,$head);
3604 my %tag = parse_tag($hash);
3606 if (! %tag) {
3607 die_error(undef, "Unknown tag object");
3610 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3611 print "<div class=\"title_text\">\n" .
3612 "<table cellspacing=\"0\">\n" .
3613 "<tr>\n" .
3614 "<td>object</td>\n" .
3615 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3616 $tag{'object'}) . "</td>\n" .
3617 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3618 $tag{'type'}) . "</td>\n" .
3619 "</tr>\n";
3620 if (defined($tag{'author'})) {
3621 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3622 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3623 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3624 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3625 "</td></tr>\n";
3627 print "</table>\n\n" .
3628 "</div>\n";
3629 print "<div class=\"page_body\">";
3630 my $comment = $tag{'comment'};
3631 foreach my $line (@$comment) {
3632 chomp $line;
3633 print esc_html($line, -nbsp=>1) . "<br/>\n";
3635 print "</div>\n";
3636 git_footer_html();
3639 sub git_blame2 {
3640 my $fd;
3641 my $ftype;
3643 my ($have_blame) = gitweb_check_feature('blame');
3644 if (!$have_blame) {
3645 die_error('403 Permission denied', "Permission denied");
3647 die_error('404 Not Found', "File name not defined") if (!$file_name);
3648 $hash_base ||= git_get_head_hash($project);
3649 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3650 my %co = parse_commit($hash_base)
3651 or die_error(undef, "Reading commit failed");
3652 if (!defined $hash) {
3653 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3654 or die_error(undef, "Error looking up file");
3656 $ftype = git_get_type($hash);
3657 if ($ftype !~ "blob") {
3658 die_error('400 Bad Request', "Object is not a blob");
3660 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3661 $file_name, $hash_base)
3662 or die_error(undef, "Open git-blame failed");
3663 git_header_html();
3664 my $formats_nav =
3665 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3666 "blob") .
3667 " | " .
3668 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3669 "history") .
3670 " | " .
3671 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3672 "HEAD");
3673 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3674 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3675 git_print_page_path($file_name, $ftype, $hash_base);
3676 my @rev_color = (qw(light2 dark2));
3677 my $num_colors = scalar(@rev_color);
3678 my $current_color = 0;
3679 my $last_rev;
3680 print <<HTML;
3681 <div class="page_body">
3682 <table class="blame">
3683 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3684 HTML
3685 my %metainfo = ();
3686 while (1) {
3687 $_ = <$fd>;
3688 last unless defined $_;
3689 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3690 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3691 if (!exists $metainfo{$full_rev}) {
3692 $metainfo{$full_rev} = {};
3694 my $meta = $metainfo{$full_rev};
3695 while (<$fd>) {
3696 last if (s/^\t//);
3697 if (/^(\S+) (.*)$/) {
3698 $meta->{$1} = $2;
3701 my $data = $_;
3702 chomp $data;
3703 my $rev = substr($full_rev, 0, 8);
3704 my $author = $meta->{'author'};
3705 my %date = parse_date($meta->{'author-time'},
3706 $meta->{'author-tz'});
3707 my $date = $date{'iso-tz'};
3708 if ($group_size) {
3709 $current_color = ++$current_color % $num_colors;
3711 print "<tr class=\"$rev_color[$current_color]\">\n";
3712 if ($group_size) {
3713 print "<td class=\"sha1\"";
3714 print " title=\"". esc_html($author) . ", $date\"";
3715 print " rowspan=\"$group_size\"" if ($group_size > 1);
3716 print ">";
3717 print $cgi->a({-href => href(action=>"commit",
3718 hash=>$full_rev,
3719 file_name=>$file_name)},
3720 esc_html($rev));
3721 print "</td>\n";
3723 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3724 or die_error(undef, "Open git-rev-parse failed");
3725 my $parent_commit = <$dd>;
3726 close $dd;
3727 chomp($parent_commit);
3728 my $blamed = href(action => 'blame',
3729 file_name => $meta->{'filename'},
3730 hash_base => $parent_commit);
3731 print "<td class=\"linenr\">";
3732 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3733 -id => "l$lineno",
3734 -class => "linenr" },
3735 esc_html($lineno));
3736 print "</td>";
3737 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3738 print "</tr>\n";
3740 print "</table>\n";
3741 print "</div>";
3742 close $fd
3743 or print "Reading blob failed\n";
3744 git_footer_html();
3747 sub git_blame {
3748 my $fd;
3750 my ($have_blame) = gitweb_check_feature('blame');
3751 if (!$have_blame) {
3752 die_error('403 Permission denied', "Permission denied");
3754 die_error('404 Not Found', "File name not defined") if (!$file_name);
3755 $hash_base ||= git_get_head_hash($project);
3756 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3757 my %co = parse_commit($hash_base)
3758 or die_error(undef, "Reading commit failed");
3759 if (!defined $hash) {
3760 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3761 or die_error(undef, "Error lookup file");
3763 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3764 or die_error(undef, "Open git-annotate failed");
3765 git_header_html();
3766 my $formats_nav =
3767 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3768 "blob") .
3769 " | " .
3770 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3771 "history") .
3772 " | " .
3773 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3774 "HEAD");
3775 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3776 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3777 git_print_page_path($file_name, 'blob', $hash_base);
3778 print "<div class=\"page_body\">\n";
3779 print <<HTML;
3780 <table class="blame">
3781 <tr>
3782 <th>Commit</th>
3783 <th>Age</th>
3784 <th>Author</th>
3785 <th>Line</th>
3786 <th>Data</th>
3787 </tr>
3788 HTML
3789 my @line_class = (qw(light dark));
3790 my $line_class_len = scalar (@line_class);
3791 my $line_class_num = $#line_class;
3792 while (my $line = <$fd>) {
3793 my $long_rev;
3794 my $short_rev;
3795 my $author;
3796 my $time;
3797 my $lineno;
3798 my $data;
3799 my $age;
3800 my $age_str;
3801 my $age_class;
3803 chomp $line;
3804 $line_class_num = ($line_class_num + 1) % $line_class_len;
3806 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3807 $long_rev = $1;
3808 $author = $2;
3809 $time = $3;
3810 $lineno = $4;
3811 $data = $5;
3812 } else {
3813 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3814 next;
3816 $short_rev = substr ($long_rev, 0, 8);
3817 $age = time () - $time;
3818 $age_str = age_string ($age);
3819 $age_str =~ s/ /&nbsp;/g;
3820 $age_class = age_class($age);
3821 $author = esc_html ($author);
3822 $author =~ s/ /&nbsp;/g;
3824 $data = untabify($data);
3825 $data = esc_html ($data);
3827 print <<HTML;
3828 <tr class="$line_class[$line_class_num]">
3829 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3830 <td class="$age_class">$age_str</td>
3831 <td>$author</td>
3832 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3833 <td class="pre">$data</td>
3834 </tr>
3835 HTML
3836 } # while (my $line = <$fd>)
3837 print "</table>\n\n";
3838 close $fd
3839 or print "Reading blob failed.\n";
3840 print "</div>";
3841 git_footer_html();
3844 sub git_tags {
3845 my $head = git_get_head_hash($project);
3846 git_header_html();
3847 git_print_page_nav('','', $head,undef,$head);
3848 git_print_header_div('summary', $project);
3850 my @tagslist = git_get_tags_list();
3851 if (@tagslist) {
3852 git_tags_body(\@tagslist);
3854 git_footer_html();
3857 sub git_heads {
3858 my $head = git_get_head_hash($project);
3859 git_header_html();
3860 git_print_page_nav('','', $head,undef,$head);
3861 git_print_header_div('summary', $project);
3863 my @headslist = git_get_heads_list();
3864 if (@headslist) {
3865 git_heads_body(\@headslist, $head);
3867 git_footer_html();
3870 sub git_blob_plain {
3871 my $expires;
3873 if (!defined $hash) {
3874 if (defined $file_name) {
3875 my $base = $hash_base || git_get_head_hash($project);
3876 $hash = git_get_hash_by_path($base, $file_name, "blob")
3877 or die_error(undef, "Error lookup file");
3878 } else {
3879 die_error(undef, "No file name defined");
3881 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3882 # blobs defined by non-textual hash id's can be cached
3883 $expires = "+1d";
3886 my $type = shift;
3887 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3888 or die_error(undef, "Couldn't cat $file_name, $hash");
3890 $type ||= blob_mimetype($fd, $file_name);
3892 # save as filename, even when no $file_name is given
3893 my $save_as = "$hash";
3894 if (defined $file_name) {
3895 $save_as = $file_name;
3896 } elsif ($type =~ m/^text\//) {
3897 $save_as .= '.txt';
3900 print $cgi->header(
3901 -type => "$type",
3902 -expires=>$expires,
3903 -content_disposition => 'inline; filename="' . "$save_as" . '"');
3904 undef $/;
3905 binmode STDOUT, ':raw';
3906 print <$fd>;
3907 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3908 $/ = "\n";
3909 close $fd;
3912 sub git_blob {
3913 my $expires;
3915 if (!defined $hash) {
3916 if (defined $file_name) {
3917 my $base = $hash_base || git_get_head_hash($project);
3918 $hash = git_get_hash_by_path($base, $file_name, "blob")
3919 or die_error(undef, "Error lookup file");
3920 } else {
3921 die_error(undef, "No file name defined");
3923 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3924 # blobs defined by non-textual hash id's can be cached
3925 $expires = "+1d";
3928 my ($have_blame) = gitweb_check_feature('blame');
3929 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3930 or die_error(undef, "Couldn't cat $file_name, $hash");
3931 my $mimetype = blob_mimetype($fd, $file_name);
3932 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
3933 close $fd;
3934 return git_blob_plain($mimetype);
3936 # we can have blame only for text/* mimetype
3937 $have_blame &&= ($mimetype =~ m!^text/!);
3939 git_header_html(undef, $expires);
3940 my $formats_nav = '';
3941 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3942 if (defined $file_name) {
3943 if ($have_blame) {
3944 $formats_nav .=
3945 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3946 hash=>$hash, file_name=>$file_name)},
3947 "blame") .
3948 " | ";
3950 $formats_nav .=
3951 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3952 hash=>$hash, file_name=>$file_name)},
3953 "history") .
3954 " | " .
3955 $cgi->a({-href => href(action=>"blob_plain",
3956 hash=>$hash, file_name=>$file_name)},
3957 "raw") .
3958 " | " .
3959 $cgi->a({-href => href(action=>"blob",
3960 hash_base=>"HEAD", file_name=>$file_name)},
3961 "HEAD");
3962 } else {
3963 $formats_nav .=
3964 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3966 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3967 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3968 } else {
3969 print "<div class=\"page_nav\">\n" .
3970 "<br/><br/></div>\n" .
3971 "<div class=\"title\">$hash</div>\n";
3973 git_print_page_path($file_name, "blob", $hash_base);
3974 print "<div class=\"page_body\">\n";
3975 if ($mimetype =~ m!^text/!) {
3976 my $nr;
3977 while (my $line = <$fd>) {
3978 chomp $line;
3979 $nr++;
3980 $line = untabify($line);
3981 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3982 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3984 } elsif ($mimetype =~ m!^image/!) {
3985 print qq!<img type="$mimetype"!;
3986 if ($file_name) {
3987 print qq! alt="$file_name" title="$file_name"!;
3989 print qq! src="! .
3990 href(action=>"blob_plain", hash=>$hash,
3991 hash_base=>$hash_base, file_name=>$file_name) .
3992 qq!" />\n!;
3994 close $fd
3995 or print "Reading blob failed.\n";
3996 print "</div>";
3997 git_footer_html();
4000 sub git_tree {
4001 my $have_snapshot = gitweb_have_snapshot();
4003 if (!defined $hash_base) {
4004 $hash_base = "HEAD";
4006 if (!defined $hash) {
4007 if (defined $file_name) {
4008 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4009 } else {
4010 $hash = $hash_base;
4013 $/ = "\0";
4014 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4015 or die_error(undef, "Open git-ls-tree failed");
4016 my @entries = map { chomp; $_ } <$fd>;
4017 close $fd or die_error(undef, "Reading tree failed");
4018 $/ = "\n";
4020 my $refs = git_get_references();
4021 my $ref = format_ref_marker($refs, $hash_base);
4022 git_header_html();
4023 my $basedir = '';
4024 my ($have_blame) = gitweb_check_feature('blame');
4025 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4026 my @views_nav = ();
4027 if (defined $file_name) {
4028 push @views_nav,
4029 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4030 hash=>$hash, file_name=>$file_name)},
4031 "history"),
4032 $cgi->a({-href => href(action=>"tree",
4033 hash_base=>"HEAD", file_name=>$file_name)},
4034 "HEAD"),
4036 if ($have_snapshot) {
4037 # FIXME: Should be available when we have no hash base as well.
4038 push @views_nav,
4039 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
4040 "snapshot");
4042 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4043 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4044 } else {
4045 undef $hash_base;
4046 print "<div class=\"page_nav\">\n";
4047 print "<br/><br/></div>\n";
4048 print "<div class=\"title\">$hash</div>\n";
4050 if (defined $file_name) {
4051 $basedir = $file_name;
4052 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4053 $basedir .= '/';
4056 git_print_page_path($file_name, 'tree', $hash_base);
4057 print "<div class=\"page_body\">\n";
4058 print "<table cellspacing=\"0\">\n";
4059 my $alternate = 1;
4060 # '..' (top directory) link if possible
4061 if (defined $hash_base &&
4062 defined $file_name && $file_name =~ m![^/]+$!) {
4063 if ($alternate) {
4064 print "<tr class=\"dark\">\n";
4065 } else {
4066 print "<tr class=\"light\">\n";
4068 $alternate ^= 1;
4070 my $up = $file_name;
4071 $up =~ s!/?[^/]+$!!;
4072 undef $up unless $up;
4073 # based on git_print_tree_entry
4074 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4075 print '<td class="list">';
4076 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4077 file_name=>$up)},
4078 "..");
4079 print "</td>\n";
4080 print "<td class=\"link\"></td>\n";
4082 print "</tr>\n";
4084 foreach my $line (@entries) {
4085 my %t = parse_ls_tree_line($line, -z => 1);
4087 if ($alternate) {
4088 print "<tr class=\"dark\">\n";
4089 } else {
4090 print "<tr class=\"light\">\n";
4092 $alternate ^= 1;
4094 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4096 print "</tr>\n";
4098 print "</table>\n" .
4099 "</div>";
4100 git_footer_html();
4103 sub git_snapshot {
4104 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
4105 my $have_snapshot = (defined $ctype && defined $suffix);
4106 if (!$have_snapshot) {
4107 die_error('403 Permission denied', "Permission denied");
4110 if (!defined $hash) {
4111 $hash = git_get_head_hash($project);
4114 my $git = git_cmd_str();
4115 my $name = $project;
4116 $name =~ s/\047/\047\\\047\047/g;
4117 my $filename = to_utf8(basename($project));
4118 my $cmd;
4119 if ($suffix eq 'zip') {
4120 $filename .= "-$hash.$suffix";
4121 $cmd = "$git archive --format=zip --prefix=\'$name\'/ $hash";
4122 } else {
4123 $filename .= "-$hash.tar.$suffix";
4124 $cmd = "$git archive --format=tar --prefix=\'$name\'/ $hash | $command";
4127 print $cgi->header(
4128 -type => "application/$ctype",
4129 -content_disposition => 'inline; filename="' . "$filename" . '"',
4130 -status => '200 OK');
4132 open my $fd, "-|", $cmd
4133 or die_error(undef, "Execute git-archive failed");
4134 binmode STDOUT, ':raw';
4135 print <$fd>;
4136 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4137 close $fd;
4141 sub git_log {
4142 my $head = git_get_head_hash($project);
4143 if (!defined $hash) {
4144 $hash = $head;
4146 if (!defined $page) {
4147 $page = 0;
4149 my $refs = git_get_references();
4151 my @commitlist = parse_commits($hash, 101, (100 * $page));
4153 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
4155 git_header_html();
4156 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4158 if (!@commitlist) {
4159 my %co = parse_commit($hash);
4161 git_print_header_div('summary', $project);
4162 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4164 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4165 for (my $i = 0; $i <= $to; $i++) {
4166 my %co = %{$commitlist[$i]};
4167 next if !%co;
4168 my $commit = $co{'id'};
4169 my $ref = format_ref_marker($refs, $commit);
4170 my %ad = parse_date($co{'author_epoch'});
4171 git_print_header_div('commit',
4172 "<span class=\"age\">$co{'age_string'}</span>" .
4173 esc_html($co{'title'}) . $ref,
4174 $commit);
4175 print "<div class=\"title_text\">\n" .
4176 "<div class=\"log_link\">\n" .
4177 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4178 " | " .
4179 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4180 " | " .
4181 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4182 "<br/>\n" .
4183 "</div>\n" .
4184 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4185 "</div>\n";
4187 print "<div class=\"log_body\">\n";
4188 git_print_log($co{'comment'}, -final_empty_line=> 1);
4189 print "</div>\n";
4191 if ($#commitlist >= 100) {
4192 print "<div class=\"page_nav\">\n";
4193 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4194 -accesskey => "n", -title => "Alt-n"}, "next");
4195 print "</div>\n";
4197 git_footer_html();
4200 sub git_commit {
4201 $hash ||= $hash_base || "HEAD";
4202 my %co = parse_commit($hash);
4203 if (!%co) {
4204 die_error(undef, "Unknown commit object");
4206 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4207 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4209 my $parent = $co{'parent'};
4210 my $parents = $co{'parents'}; # listref
4212 # we need to prepare $formats_nav before any parameter munging
4213 my $formats_nav;
4214 if (!defined $parent) {
4215 # --root commitdiff
4216 $formats_nav .= '(initial)';
4217 } elsif (@$parents == 1) {
4218 # single parent commit
4219 $formats_nav .=
4220 '(parent: ' .
4221 $cgi->a({-href => href(action=>"commit",
4222 hash=>$parent)},
4223 esc_html(substr($parent, 0, 7))) .
4224 ')';
4225 } else {
4226 # merge commit
4227 $formats_nav .=
4228 '(merge: ' .
4229 join(' ', map {
4230 $cgi->a({-href => href(action=>"commit",
4231 hash=>$_)},
4232 esc_html(substr($_, 0, 7)));
4233 } @$parents ) .
4234 ')';
4237 if (!defined $parent) {
4238 $parent = "--root";
4240 my @difftree;
4241 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4242 @diff_opts,
4243 (@$parents <= 1 ? $parent : '-c'),
4244 $hash, "--"
4245 or die_error(undef, "Open git-diff-tree failed");
4246 @difftree = map { chomp; $_ } <$fd>;
4247 close $fd or die_error(undef, "Reading git-diff-tree failed");
4249 # non-textual hash id's can be cached
4250 my $expires;
4251 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4252 $expires = "+1d";
4254 my $refs = git_get_references();
4255 my $ref = format_ref_marker($refs, $co{'id'});
4257 my $have_snapshot = gitweb_have_snapshot();
4259 git_header_html(undef, $expires);
4260 git_print_page_nav('commit', '',
4261 $hash, $co{'tree'}, $hash,
4262 $formats_nav);
4264 if (defined $co{'parent'}) {
4265 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4266 } else {
4267 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4269 print "<div class=\"title_text\">\n" .
4270 "<table cellspacing=\"0\">\n";
4271 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4272 "<tr>" .
4273 "<td></td><td> $ad{'rfc2822'}";
4274 if ($ad{'hour_local'} < 6) {
4275 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4276 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4277 } else {
4278 printf(" (%02d:%02d %s)",
4279 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4281 print "</td>" .
4282 "</tr>\n";
4283 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4284 print "<tr><td></td><td> $cd{'rfc2822'}" .
4285 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4286 "</td></tr>\n";
4287 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4288 print "<tr>" .
4289 "<td>tree</td>" .
4290 "<td class=\"sha1\">" .
4291 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4292 class => "list"}, $co{'tree'}) .
4293 "</td>" .
4294 "<td class=\"link\">" .
4295 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4296 "tree");
4297 if ($have_snapshot) {
4298 print " | " .
4299 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
4301 print "</td>" .
4302 "</tr>\n";
4304 foreach my $par (@$parents) {
4305 print "<tr>" .
4306 "<td>parent</td>" .
4307 "<td class=\"sha1\">" .
4308 $cgi->a({-href => href(action=>"commit", hash=>$par),
4309 class => "list"}, $par) .
4310 "</td>" .
4311 "<td class=\"link\">" .
4312 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4313 " | " .
4314 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4315 "</td>" .
4316 "</tr>\n";
4318 print "</table>".
4319 "</div>\n";
4321 print "<div class=\"page_body\">\n";
4322 git_print_log($co{'comment'});
4323 print "</div>\n";
4325 git_difftree_body(\@difftree, $hash, @$parents);
4327 git_footer_html();
4330 sub git_object {
4331 # object is defined by:
4332 # - hash or hash_base alone
4333 # - hash_base and file_name
4334 my $type;
4336 # - hash or hash_base alone
4337 if ($hash || ($hash_base && !defined $file_name)) {
4338 my $object_id = $hash || $hash_base;
4340 my $git_command = git_cmd_str();
4341 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4342 or die_error('404 Not Found', "Object does not exist");
4343 $type = <$fd>;
4344 chomp $type;
4345 close $fd
4346 or die_error('404 Not Found', "Object does not exist");
4348 # - hash_base and file_name
4349 } elsif ($hash_base && defined $file_name) {
4350 $file_name =~ s,/+$,,;
4352 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4353 or die_error('404 Not Found', "Base object does not exist");
4355 # here errors should not hapen
4356 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4357 or die_error(undef, "Open git-ls-tree failed");
4358 my $line = <$fd>;
4359 close $fd;
4361 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4362 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4363 die_error('404 Not Found', "File or directory for given base does not exist");
4365 $type = $2;
4366 $hash = $3;
4367 } else {
4368 die_error('404 Not Found', "Not enough information to find object");
4371 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4372 hash=>$hash, hash_base=>$hash_base,
4373 file_name=>$file_name),
4374 -status => '302 Found');
4377 sub git_blobdiff {
4378 my $format = shift || 'html';
4380 my $fd;
4381 my @difftree;
4382 my %diffinfo;
4383 my $expires;
4385 # preparing $fd and %diffinfo for git_patchset_body
4386 # new style URI
4387 if (defined $hash_base && defined $hash_parent_base) {
4388 if (defined $file_name) {
4389 # read raw output
4390 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4391 $hash_parent_base, $hash_base,
4392 "--", (defined $file_parent ? $file_parent : ()), $file_name
4393 or die_error(undef, "Open git-diff-tree failed");
4394 @difftree = map { chomp; $_ } <$fd>;
4395 close $fd
4396 or die_error(undef, "Reading git-diff-tree failed");
4397 @difftree
4398 or die_error('404 Not Found', "Blob diff not found");
4400 } elsif (defined $hash &&
4401 $hash =~ /[0-9a-fA-F]{40}/) {
4402 # try to find filename from $hash
4404 # read filtered raw output
4405 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4406 $hash_parent_base, $hash_base, "--"
4407 or die_error(undef, "Open git-diff-tree failed");
4408 @difftree =
4409 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4410 # $hash == to_id
4411 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4412 map { chomp; $_ } <$fd>;
4413 close $fd
4414 or die_error(undef, "Reading git-diff-tree failed");
4415 @difftree
4416 or die_error('404 Not Found', "Blob diff not found");
4418 } else {
4419 die_error('404 Not Found', "Missing one of the blob diff parameters");
4422 if (@difftree > 1) {
4423 die_error('404 Not Found', "Ambiguous blob diff specification");
4426 %diffinfo = parse_difftree_raw_line($difftree[0]);
4427 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4428 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4430 $hash_parent ||= $diffinfo{'from_id'};
4431 $hash ||= $diffinfo{'to_id'};
4433 # non-textual hash id's can be cached
4434 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4435 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4436 $expires = '+1d';
4439 # open patch output
4440 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4441 '-p', ($format eq 'html' ? "--full-index" : ()),
4442 $hash_parent_base, $hash_base,
4443 "--", (defined $file_parent ? $file_parent : ()), $file_name
4444 or die_error(undef, "Open git-diff-tree failed");
4447 # old/legacy style URI
4448 if (!%diffinfo && # if new style URI failed
4449 defined $hash && defined $hash_parent) {
4450 # fake git-diff-tree raw output
4451 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4452 $diffinfo{'from_id'} = $hash_parent;
4453 $diffinfo{'to_id'} = $hash;
4454 if (defined $file_name) {
4455 if (defined $file_parent) {
4456 $diffinfo{'status'} = '2';
4457 $diffinfo{'from_file'} = $file_parent;
4458 $diffinfo{'to_file'} = $file_name;
4459 } else { # assume not renamed
4460 $diffinfo{'status'} = '1';
4461 $diffinfo{'from_file'} = $file_name;
4462 $diffinfo{'to_file'} = $file_name;
4464 } else { # no filename given
4465 $diffinfo{'status'} = '2';
4466 $diffinfo{'from_file'} = $hash_parent;
4467 $diffinfo{'to_file'} = $hash;
4470 # non-textual hash id's can be cached
4471 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4472 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4473 $expires = '+1d';
4476 # open patch output
4477 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4478 '-p', ($format eq 'html' ? "--full-index" : ()),
4479 $hash_parent, $hash, "--"
4480 or die_error(undef, "Open git-diff failed");
4481 } else {
4482 die_error('404 Not Found', "Missing one of the blob diff parameters")
4483 unless %diffinfo;
4486 # header
4487 if ($format eq 'html') {
4488 my $formats_nav =
4489 $cgi->a({-href => href(action=>"blobdiff_plain",
4490 hash=>$hash, hash_parent=>$hash_parent,
4491 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4492 file_name=>$file_name, file_parent=>$file_parent)},
4493 "raw");
4494 git_header_html(undef, $expires);
4495 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4496 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4497 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4498 } else {
4499 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4500 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4502 if (defined $file_name) {
4503 git_print_page_path($file_name, "blob", $hash_base);
4504 } else {
4505 print "<div class=\"page_path\"></div>\n";
4508 } elsif ($format eq 'plain') {
4509 print $cgi->header(
4510 -type => 'text/plain',
4511 -charset => 'utf-8',
4512 -expires => $expires,
4513 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4515 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4517 } else {
4518 die_error(undef, "Unknown blobdiff format");
4521 # patch
4522 if ($format eq 'html') {
4523 print "<div class=\"page_body\">\n";
4525 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4526 close $fd;
4528 print "</div>\n"; # class="page_body"
4529 git_footer_html();
4531 } else {
4532 while (my $line = <$fd>) {
4533 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4534 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4536 print $line;
4538 last if $line =~ m!^\+\+\+!;
4540 local $/ = undef;
4541 print <$fd>;
4542 close $fd;
4546 sub git_blobdiff_plain {
4547 git_blobdiff('plain');
4550 sub git_commitdiff {
4551 my $format = shift || 'html';
4552 $hash ||= $hash_base || "HEAD";
4553 my %co = parse_commit($hash);
4554 if (!%co) {
4555 die_error(undef, "Unknown commit object");
4558 # we need to prepare $formats_nav before any parameter munging
4559 my $formats_nav;
4560 if ($format eq 'html') {
4561 $formats_nav =
4562 $cgi->a({-href => href(action=>"commitdiff_plain",
4563 hash=>$hash, hash_parent=>$hash_parent)},
4564 "raw");
4566 if (defined $hash_parent) {
4567 # commitdiff with two commits given
4568 my $hash_parent_short = $hash_parent;
4569 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4570 $hash_parent_short = substr($hash_parent, 0, 7);
4572 $formats_nav .=
4573 ' (from';
4574 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4575 if ($co{'parents'}[$i] eq $hash_parent) {
4576 $formats_nav .= ' parent ' . ($i+1);
4577 last;
4580 $formats_nav .= ': ' .
4581 $cgi->a({-href => href(action=>"commitdiff",
4582 hash=>$hash_parent)},
4583 esc_html($hash_parent_short)) .
4584 ')';
4585 } elsif (!$co{'parent'}) {
4586 # --root commitdiff
4587 $formats_nav .= ' (initial)';
4588 } elsif (scalar @{$co{'parents'}} == 1) {
4589 # single parent commit
4590 $formats_nav .=
4591 ' (parent: ' .
4592 $cgi->a({-href => href(action=>"commitdiff",
4593 hash=>$co{'parent'})},
4594 esc_html(substr($co{'parent'}, 0, 7))) .
4595 ')';
4596 } else {
4597 # merge commit
4598 $formats_nav .=
4599 ' (merge: ' .
4600 join(' ', map {
4601 $cgi->a({-href => href(action=>"commitdiff",
4602 hash=>$_)},
4603 esc_html(substr($_, 0, 7)));
4604 } @{$co{'parents'}} ) .
4605 ')';
4609 my $hash_parent_param = $hash_parent;
4610 if (!defined $hash_parent) {
4611 $hash_parent_param =
4612 @{$co{'parents'}} > 1 ? '-c' : $co{'parent'} || '--root';
4615 # read commitdiff
4616 my $fd;
4617 my @difftree;
4618 if ($format eq 'html') {
4619 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4620 "--no-commit-id", "--patch-with-raw", "--full-index",
4621 $hash_parent_param, $hash, "--"
4622 or die_error(undef, "Open git-diff-tree failed");
4624 while (my $line = <$fd>) {
4625 chomp $line;
4626 # empty line ends raw part of diff-tree output
4627 last unless $line;
4628 push @difftree, scalar parse_difftree_raw_line($line);
4631 } elsif ($format eq 'plain') {
4632 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4633 '-p', $hash_parent_param, $hash, "--"
4634 or die_error(undef, "Open git-diff-tree failed");
4636 } else {
4637 die_error(undef, "Unknown commitdiff format");
4640 # non-textual hash id's can be cached
4641 my $expires;
4642 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4643 $expires = "+1d";
4646 # write commit message
4647 if ($format eq 'html') {
4648 my $refs = git_get_references();
4649 my $ref = format_ref_marker($refs, $co{'id'});
4651 git_header_html(undef, $expires);
4652 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4653 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4654 git_print_authorship(\%co);
4655 print "<div class=\"page_body\">\n";
4656 if (@{$co{'comment'}} > 1) {
4657 print "<div class=\"log\">\n";
4658 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4659 print "</div>\n"; # class="log"
4662 } elsif ($format eq 'plain') {
4663 my $refs = git_get_references("tags");
4664 my $tagname = git_get_rev_name_tags($hash);
4665 my $filename = basename($project) . "-$hash.patch";
4667 print $cgi->header(
4668 -type => 'text/plain',
4669 -charset => 'utf-8',
4670 -expires => $expires,
4671 -content_disposition => 'inline; filename="' . "$filename" . '"');
4672 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4673 print <<TEXT;
4674 From: $co{'author'}
4675 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4676 Subject: $co{'title'}
4677 TEXT
4678 print "X-Git-Tag: $tagname\n" if $tagname;
4679 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4681 foreach my $line (@{$co{'comment'}}) {
4682 print "$line\n";
4684 print "---\n\n";
4687 # write patch
4688 if ($format eq 'html') {
4689 git_difftree_body(\@difftree, $hash, $hash_parent || @{$co{'parents'}});
4690 print "<br/>\n";
4692 git_patchset_body($fd, \@difftree, $hash, $hash_parent || @{$co{'parents'}});
4693 close $fd;
4694 print "</div>\n"; # class="page_body"
4695 git_footer_html();
4697 } elsif ($format eq 'plain') {
4698 local $/ = undef;
4699 print <$fd>;
4700 close $fd
4701 or print "Reading git-diff-tree failed\n";
4705 sub git_commitdiff_plain {
4706 git_commitdiff('plain');
4709 sub git_history {
4710 if (!defined $hash_base) {
4711 $hash_base = git_get_head_hash($project);
4713 if (!defined $page) {
4714 $page = 0;
4716 my $ftype;
4717 my %co = parse_commit($hash_base);
4718 if (!%co) {
4719 die_error(undef, "Unknown commit object");
4722 my $refs = git_get_references();
4723 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4725 if (!defined $hash && defined $file_name) {
4726 $hash = git_get_hash_by_path($hash_base, $file_name);
4728 if (defined $hash) {
4729 $ftype = git_get_type($hash);
4732 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4734 my $paging_nav = '';
4735 if ($page > 0) {
4736 $paging_nav .=
4737 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4738 file_name=>$file_name)},
4739 "first");
4740 $paging_nav .= " &sdot; " .
4741 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4742 file_name=>$file_name, page=>$page-1),
4743 -accesskey => "p", -title => "Alt-p"}, "prev");
4744 } else {
4745 $paging_nav .= "first";
4746 $paging_nav .= " &sdot; prev";
4748 if ($#commitlist >= 100) {
4749 $paging_nav .= " &sdot; " .
4750 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4751 file_name=>$file_name, page=>$page+1),
4752 -accesskey => "n", -title => "Alt-n"}, "next");
4753 } else {
4754 $paging_nav .= " &sdot; next";
4756 my $next_link = '';
4757 if ($#commitlist >= 100) {
4758 $next_link =
4759 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4760 file_name=>$file_name, page=>$page+1),
4761 -accesskey => "n", -title => "Alt-n"}, "next");
4764 git_header_html();
4765 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4766 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4767 git_print_page_path($file_name, $ftype, $hash_base);
4769 git_history_body(\@commitlist, 0, 99,
4770 $refs, $hash_base, $ftype, $next_link);
4772 git_footer_html();
4775 sub git_search {
4776 my ($have_search) = gitweb_check_feature('search');
4777 if (!$have_search) {
4778 die_error('403 Permission denied', "Permission denied");
4780 if (!defined $searchtext) {
4781 die_error(undef, "Text field empty");
4783 if (!defined $hash) {
4784 $hash = git_get_head_hash($project);
4786 my %co = parse_commit($hash);
4787 if (!%co) {
4788 die_error(undef, "Unknown commit object");
4790 if (!defined $page) {
4791 $page = 0;
4794 $searchtype ||= 'commit';
4795 if ($searchtype eq 'pickaxe') {
4796 # pickaxe may take all resources of your box and run for several minutes
4797 # with every query - so decide by yourself how public you make this feature
4798 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4799 if (!$have_pickaxe) {
4800 die_error('403 Permission denied', "Permission denied");
4803 if ($searchtype eq 'grep') {
4804 my ($have_grep) = gitweb_check_feature('grep');
4805 if (!$have_grep) {
4806 die_error('403 Permission denied', "Permission denied");
4810 git_header_html();
4812 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4813 my $greptype;
4814 if ($searchtype eq 'commit') {
4815 $greptype = "--grep=";
4816 } elsif ($searchtype eq 'author') {
4817 $greptype = "--author=";
4818 } elsif ($searchtype eq 'committer') {
4819 $greptype = "--committer=";
4821 $greptype .= $search_regexp;
4822 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
4824 my $paging_nav = '';
4825 if ($page > 0) {
4826 $paging_nav .=
4827 $cgi->a({-href => href(action=>"search", hash=>$hash,
4828 searchtext=>$searchtext, searchtype=>$searchtype)},
4829 "first");
4830 $paging_nav .= " &sdot; " .
4831 $cgi->a({-href => href(action=>"search", hash=>$hash,
4832 searchtext=>$searchtext, searchtype=>$searchtype,
4833 page=>$page-1),
4834 -accesskey => "p", -title => "Alt-p"}, "prev");
4835 } else {
4836 $paging_nav .= "first";
4837 $paging_nav .= " &sdot; prev";
4839 if ($#commitlist >= 100) {
4840 $paging_nav .= " &sdot; " .
4841 $cgi->a({-href => href(action=>"search", hash=>$hash,
4842 searchtext=>$searchtext, searchtype=>$searchtype,
4843 page=>$page+1),
4844 -accesskey => "n", -title => "Alt-n"}, "next");
4845 } else {
4846 $paging_nav .= " &sdot; next";
4848 my $next_link = '';
4849 if ($#commitlist >= 100) {
4850 $next_link =
4851 $cgi->a({-href => href(action=>"search", hash=>$hash,
4852 searchtext=>$searchtext, searchtype=>$searchtype,
4853 page=>$page+1),
4854 -accesskey => "n", -title => "Alt-n"}, "next");
4857 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
4858 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4859 git_search_grep_body(\@commitlist, 0, 99, $next_link);
4862 if ($searchtype eq 'pickaxe') {
4863 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4864 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4866 print "<table cellspacing=\"0\">\n";
4867 my $alternate = 1;
4868 $/ = "\n";
4869 my $git_command = git_cmd_str();
4870 my $searchqtext = $searchtext;
4871 $searchqtext =~ s/'/'\\''/;
4872 open my $fd, "-|", "$git_command rev-list $hash | " .
4873 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
4874 undef %co;
4875 my @files;
4876 while (my $line = <$fd>) {
4877 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
4878 my %set;
4879 $set{'file'} = $6;
4880 $set{'from_id'} = $3;
4881 $set{'to_id'} = $4;
4882 $set{'id'} = $set{'to_id'};
4883 if ($set{'id'} =~ m/0{40}/) {
4884 $set{'id'} = $set{'from_id'};
4886 if ($set{'id'} =~ m/0{40}/) {
4887 next;
4889 push @files, \%set;
4890 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
4891 if (%co) {
4892 if ($alternate) {
4893 print "<tr class=\"dark\">\n";
4894 } else {
4895 print "<tr class=\"light\">\n";
4897 $alternate ^= 1;
4898 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4899 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4900 "<td>" .
4901 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4902 -class => "list subject"},
4903 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4904 while (my $setref = shift @files) {
4905 my %set = %$setref;
4906 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
4907 hash=>$set{'id'}, file_name=>$set{'file'}),
4908 -class => "list"},
4909 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
4910 "<br/>\n";
4912 print "</td>\n" .
4913 "<td class=\"link\">" .
4914 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4915 " | " .
4916 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4917 print "</td>\n" .
4918 "</tr>\n";
4920 %co = parse_commit($1);
4923 close $fd;
4925 print "</table>\n";
4928 if ($searchtype eq 'grep') {
4929 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4930 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4932 print "<table cellspacing=\"0\">\n";
4933 my $alternate = 1;
4934 my $matches = 0;
4935 $/ = "\n";
4936 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
4937 my $lastfile = '';
4938 while (my $line = <$fd>) {
4939 chomp $line;
4940 my ($file, $lno, $ltext, $binary);
4941 last if ($matches++ > 1000);
4942 if ($line =~ /^Binary file (.+) matches$/) {
4943 $file = $1;
4944 $binary = 1;
4945 } else {
4946 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
4948 if ($file ne $lastfile) {
4949 $lastfile and print "</td></tr>\n";
4950 if ($alternate++) {
4951 print "<tr class=\"dark\">\n";
4952 } else {
4953 print "<tr class=\"light\">\n";
4955 print "<td class=\"list\">".
4956 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
4957 file_name=>"$file"),
4958 -class => "list"}, esc_path($file));
4959 print "</td><td>\n";
4960 $lastfile = $file;
4962 if ($binary) {
4963 print "<div class=\"binary\">Binary file</div>\n";
4964 } else {
4965 $ltext = untabify($ltext);
4966 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
4967 $ltext = esc_html($1, -nbsp=>1);
4968 $ltext .= '<span class="match">';
4969 $ltext .= esc_html($2, -nbsp=>1);
4970 $ltext .= '</span>';
4971 $ltext .= esc_html($3, -nbsp=>1);
4972 } else {
4973 $ltext = esc_html($ltext, -nbsp=>1);
4975 print "<div class=\"pre\">" .
4976 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
4977 file_name=>"$file").'#l'.$lno,
4978 -class => "linenr"}, sprintf('%4i', $lno))
4979 . ' ' . $ltext . "</div>\n";
4982 if ($lastfile) {
4983 print "</td></tr>\n";
4984 if ($matches > 1000) {
4985 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
4987 } else {
4988 print "<div class=\"diff nodifferences\">No matches found</div>\n";
4990 close $fd;
4992 print "</table>\n";
4994 git_footer_html();
4997 sub git_search_help {
4998 git_header_html();
4999 git_print_page_nav('','', $hash,$hash,$hash);
5000 print <<EOT;
5001 <dl>
5002 <dt><b>commit</b></dt>
5003 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
5005 my ($have_grep) = gitweb_check_feature('grep');
5006 if ($have_grep) {
5007 print <<EOT;
5008 <dt><b>grep</b></dt>
5009 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5010 a different one) are searched for the given
5011 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5012 (POSIX extended) and the matches are listed. On large
5013 trees, this search can take a while and put some strain on the server, so please use it with
5014 some consideration.</dd>
5017 print <<EOT;
5018 <dt><b>author</b></dt>
5019 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5020 <dt><b>committer</b></dt>
5021 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5023 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5024 if ($have_pickaxe) {
5025 print <<EOT;
5026 <dt><b>pickaxe</b></dt>
5027 <dd>All commits that caused the string to appear or disappear from any file (changes that
5028 added, removed or "modified" the string) will be listed. This search can take a while and
5029 takes a lot of strain on the server, so please use it wisely.</dd>
5032 print "</dl>\n";
5033 git_footer_html();
5036 sub git_shortlog {
5037 my $head = git_get_head_hash($project);
5038 if (!defined $hash) {
5039 $hash = $head;
5041 if (!defined $page) {
5042 $page = 0;
5044 my $refs = git_get_references();
5046 my @commitlist = parse_commits($hash, 101, (100 * $page));
5048 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
5049 my $next_link = '';
5050 if ($#commitlist >= 100) {
5051 $next_link =
5052 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
5053 -accesskey => "n", -title => "Alt-n"}, "next");
5056 git_header_html();
5057 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5058 git_print_header_div('summary', $project);
5060 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5062 git_footer_html();
5065 ## ......................................................................
5066 ## feeds (RSS, Atom; OPML)
5068 sub git_feed {
5069 my $format = shift || 'atom';
5070 my ($have_blame) = gitweb_check_feature('blame');
5072 # Atom: http://www.atomenabled.org/developers/syndication/
5073 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5074 if ($format ne 'rss' && $format ne 'atom') {
5075 die_error(undef, "Unknown web feed format");
5078 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5079 my $head = $hash || 'HEAD';
5080 my @commitlist = parse_commits($head, 150);
5082 my %latest_commit;
5083 my %latest_date;
5084 my $content_type = "application/$format+xml";
5085 if (defined $cgi->http('HTTP_ACCEPT') &&
5086 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5087 # browser (feed reader) prefers text/xml
5088 $content_type = 'text/xml';
5090 if (defined($commitlist[0])) {
5091 %latest_commit = %{$commitlist[0]};
5092 %latest_date = parse_date($latest_commit{'author_epoch'});
5093 print $cgi->header(
5094 -type => $content_type,
5095 -charset => 'utf-8',
5096 -last_modified => $latest_date{'rfc2822'});
5097 } else {
5098 print $cgi->header(
5099 -type => $content_type,
5100 -charset => 'utf-8');
5103 # Optimization: skip generating the body if client asks only
5104 # for Last-Modified date.
5105 return if ($cgi->request_method() eq 'HEAD');
5107 # header variables
5108 my $title = "$site_name - $project/$action";
5109 my $feed_type = 'log';
5110 if (defined $hash) {
5111 $title .= " - '$hash'";
5112 $feed_type = 'branch log';
5113 if (defined $file_name) {
5114 $title .= " :: $file_name";
5115 $feed_type = 'history';
5117 } elsif (defined $file_name) {
5118 $title .= " - $file_name";
5119 $feed_type = 'history';
5121 $title .= " $feed_type";
5122 my $descr = git_get_project_description($project);
5123 if (defined $descr) {
5124 $descr = esc_html($descr);
5125 } else {
5126 $descr = "$project " .
5127 ($format eq 'rss' ? 'RSS' : 'Atom') .
5128 " feed";
5130 my $owner = git_get_project_owner($project);
5131 $owner = esc_html($owner);
5133 #header
5134 my $alt_url;
5135 if (defined $file_name) {
5136 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5137 } elsif (defined $hash) {
5138 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5139 } else {
5140 $alt_url = href(-full=>1, action=>"summary");
5142 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5143 if ($format eq 'rss') {
5144 print <<XML;
5145 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5146 <channel>
5148 print "<title>$title</title>\n" .
5149 "<link>$alt_url</link>\n" .
5150 "<description>$descr</description>\n" .
5151 "<language>en</language>\n";
5152 } elsif ($format eq 'atom') {
5153 print <<XML;
5154 <feed xmlns="http://www.w3.org/2005/Atom">
5156 print "<title>$title</title>\n" .
5157 "<subtitle>$descr</subtitle>\n" .
5158 '<link rel="alternate" type="text/html" href="' .
5159 $alt_url . '" />' . "\n" .
5160 '<link rel="self" type="' . $content_type . '" href="' .
5161 $cgi->self_url() . '" />' . "\n" .
5162 "<id>" . href(-full=>1) . "</id>\n" .
5163 # use project owner for feed author
5164 "<author><name>$owner</name></author>\n";
5165 if (defined $favicon) {
5166 print "<icon>" . esc_url($favicon) . "</icon>\n";
5168 if (defined $logo_url) {
5169 # not twice as wide as tall: 72 x 27 pixels
5170 print "<logo>" . esc_url($logo) . "</logo>\n";
5172 if (! %latest_date) {
5173 # dummy date to keep the feed valid until commits trickle in:
5174 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5175 } else {
5176 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5180 # contents
5181 for (my $i = 0; $i <= $#commitlist; $i++) {
5182 my %co = %{$commitlist[$i]};
5183 my $commit = $co{'id'};
5184 # we read 150, we always show 30 and the ones more recent than 48 hours
5185 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5186 last;
5188 my %cd = parse_date($co{'author_epoch'});
5190 # get list of changed files
5191 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5192 $co{'parent'} || "--root",
5193 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5194 or next;
5195 my @difftree = map { chomp; $_ } <$fd>;
5196 close $fd
5197 or next;
5199 # print element (entry, item)
5200 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5201 if ($format eq 'rss') {
5202 print "<item>\n" .
5203 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5204 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5205 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5206 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5207 "<link>$co_url</link>\n" .
5208 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5209 "<content:encoded>" .
5210 "<![CDATA[\n";
5211 } elsif ($format eq 'atom') {
5212 print "<entry>\n" .
5213 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5214 "<updated>$cd{'iso-8601'}</updated>\n" .
5215 "<author>\n" .
5216 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5217 if ($co{'author_email'}) {
5218 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5220 print "</author>\n" .
5221 # use committer for contributor
5222 "<contributor>\n" .
5223 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5224 if ($co{'committer_email'}) {
5225 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5227 print "</contributor>\n" .
5228 "<published>$cd{'iso-8601'}</published>\n" .
5229 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5230 "<id>$co_url</id>\n" .
5231 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5232 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5234 my $comment = $co{'comment'};
5235 print "<pre>\n";
5236 foreach my $line (@$comment) {
5237 $line = esc_html($line);
5238 print "$line\n";
5240 print "</pre><ul>\n";
5241 foreach my $difftree_line (@difftree) {
5242 my %difftree = parse_difftree_raw_line($difftree_line);
5243 next if !$difftree{'from_id'};
5245 my $file = $difftree{'file'} || $difftree{'to_file'};
5247 print "<li>" .
5248 "[" .
5249 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5250 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5251 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5252 file_name=>$file, file_parent=>$difftree{'from_file'}),
5253 -title => "diff"}, 'D');
5254 if ($have_blame) {
5255 print $cgi->a({-href => href(-full=>1, action=>"blame",
5256 file_name=>$file, hash_base=>$commit),
5257 -title => "blame"}, 'B');
5259 # if this is not a feed of a file history
5260 if (!defined $file_name || $file_name ne $file) {
5261 print $cgi->a({-href => href(-full=>1, action=>"history",
5262 file_name=>$file, hash=>$commit),
5263 -title => "history"}, 'H');
5265 $file = esc_path($file);
5266 print "] ".
5267 "$file</li>\n";
5269 if ($format eq 'rss') {
5270 print "</ul>]]>\n" .
5271 "</content:encoded>\n" .
5272 "</item>\n";
5273 } elsif ($format eq 'atom') {
5274 print "</ul>\n</div>\n" .
5275 "</content>\n" .
5276 "</entry>\n";
5280 # end of feed
5281 if ($format eq 'rss') {
5282 print "</channel>\n</rss>\n";
5283 } elsif ($format eq 'atom') {
5284 print "</feed>\n";
5288 sub git_rss {
5289 git_feed('rss');
5292 sub git_atom {
5293 git_feed('atom');
5296 sub git_opml {
5297 my @list = git_get_projects_list();
5299 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5300 print <<XML;
5301 <?xml version="1.0" encoding="utf-8"?>
5302 <opml version="1.0">
5303 <head>
5304 <title>$site_name OPML Export</title>
5305 </head>
5306 <body>
5307 <outline text="git RSS feeds">
5310 foreach my $pr (@list) {
5311 my %proj = %$pr;
5312 my $head = git_get_head_hash($proj{'path'});
5313 if (!defined $head) {
5314 next;
5316 $git_dir = "$projectroot/$proj{'path'}";
5317 my %co = parse_commit($head);
5318 if (!%co) {
5319 next;
5322 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5323 my $rss = "$my_url?p=$proj{'path'};a=rss";
5324 my $html = "$my_url?p=$proj{'path'};a=summary";
5325 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5327 print <<XML;
5328 </outline>
5329 </body>
5330 </opml>