3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
22 CGI->compile() if $ENV{'MOD_PERL'};
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++";
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 # the width (in characters) of the projects list "Description" column
75 our $projects_list_description_width = 25;
77 # default order of projects list
78 # valid values are none, project, descr, owner, and age
79 our $default_projects_order = "project";
81 # show repository only if this file exists
82 # (only effective if this variable evaluates to true)
83 our $export_ok = "++GITWEB_EXPORT_OK++";
85 # only allow viewing of repositories also shown on the overview page
86 our $strict_export = "++GITWEB_STRICT_EXPORT++";
88 # list of git base URLs used for URL to where fetch project from,
89 # i.e. full URL is "$git_base_url/$project"
90 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
92 # default blob_plain mimetype and default charset for text/plain blob
93 our $default_blob_plain_mimetype = 'text/plain';
94 our $default_text_plain_charset = undef;
96 # file to use for guessing MIME types before trying /etc/mime.types
97 # (relative to the current git repository)
98 our $mimetypes_file = undef;
100 # assume this charset if line contains non-UTF-8 characters;
101 # it should be valid encoding (see Encoding::Supported(3pm) for list),
102 # for which encoding all byte sequences are valid, for example
103 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
104 # could be even 'utf-8' for the old behavior)
105 our $fallback_encoding = 'latin1';
107 # rename detection options for git-diff and git-diff-tree
108 # - default is '-M', with the cost proportional to
109 # (number of removed files) * (number of new files).
110 # - more costly is '-C' (which implies '-M'), with the cost proportional to
111 # (number of changed files + number of removed files) * (number of new files)
112 # - even more costly is '-C', '--find-copies-harder' with cost
113 # (number of files in the original tree) * (number of new files)
114 # - one might want to include '-B' option, e.g. '-B', '-M'
115 our @diff_opts = ('-M'); # taken from git_commit
117 # information about snapshot formats that gitweb is capable of serving
118 our %known_snapshot_formats = (
120 # 'display' => display name,
121 # 'type' => mime type,
122 # 'suffix' => filename suffix,
123 # 'format' => --format for git-archive,
124 # 'compressor' => [compressor command and arguments]
125 # (array reference, optional)}
128 'display' => 'tar.gz',
129 'type' => 'application/x-gzip',
130 'suffix' => '.tar.gz',
132 'compressor' => ['gzip']},
135 'display' => 'tar.bz2',
136 'type' => 'application/x-bzip2',
137 'suffix' => '.tar.bz2',
139 'compressor' => ['bzip2']},
143 'type' => 'application/x-zip',
148 # Aliases so we understand old gitweb.snapshot values in repository
150 our %known_snapshot_format_aliases = (
154 # backward compatibility: legacy gitweb config support
155 'x-gzip' => undef, 'gz' => undef,
156 'x-bzip2' => undef, 'bz2' => undef,
157 'x-zip' => undef, '' => undef,
160 # You define site-wide feature defaults here; override them with
161 # $GITWEB_CONFIG as necessary.
164 # 'sub' => feature-sub (subroutine),
165 # 'override' => allow-override (boolean),
166 # 'default' => [ default options...] (array reference)}
168 # if feature is overridable (it means that allow-override has true value),
169 # then feature-sub will be called with default options as parameters;
170 # return value of feature-sub indicates if to enable specified feature
172 # if there is no 'sub' key (no feature-sub), then feature cannot be
175 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
177 # Enable the 'blame' blob view, showing the last commit that modified
178 # each line in the file. This can be very CPU-intensive.
180 # To enable system wide have in $GITWEB_CONFIG
181 # $feature{'blame'}{'default'} = [1];
182 # To have project specific config enable override in $GITWEB_CONFIG
183 # $feature{'blame'}{'override'} = 1;
184 # and in project config gitweb.blame = 0|1;
186 'sub' => \&feature_blame,
190 # Enable the 'snapshot' link, providing a compressed archive of any
191 # tree. This can potentially generate high traffic if you have large
194 # Value is a list of formats defined in %known_snapshot_formats that
196 # To disable system wide have in $GITWEB_CONFIG
197 # $feature{'snapshot'}{'default'} = [];
198 # To have project specific config enable override in $GITWEB_CONFIG
199 # $feature{'snapshot'}{'override'} = 1;
200 # and in project config, a comma-separated list of formats or "none"
201 # to disable. Example: gitweb.snapshot = tbz2,zip;
203 'sub' => \&feature_snapshot,
205 'default' => ['tgz']},
207 # Enable text search, which will list the commits which match author,
208 # committer or commit text to a given string. Enabled by default.
209 # Project specific override is not supported.
214 # Enable grep search, which will list the files in currently selected
215 # tree containing the given string. Enabled by default. This can be
216 # potentially CPU-intensive, of course.
218 # To enable system wide have in $GITWEB_CONFIG
219 # $feature{'grep'}{'default'} = [1];
220 # To have project specific config enable override in $GITWEB_CONFIG
221 # $feature{'grep'}{'override'} = 1;
222 # and in project config gitweb.grep = 0|1;
227 # Enable the pickaxe search, which will list the commits that modified
228 # a given string in a file. This can be practical and quite faster
229 # alternative to 'blame', but still potentially CPU-intensive.
231 # To enable system wide have in $GITWEB_CONFIG
232 # $feature{'pickaxe'}{'default'} = [1];
233 # To have project specific config enable override in $GITWEB_CONFIG
234 # $feature{'pickaxe'}{'override'} = 1;
235 # and in project config gitweb.pickaxe = 0|1;
237 'sub' => \&feature_pickaxe,
241 # Make gitweb use an alternative format of the URLs which can be
242 # more readable and natural-looking: project name is embedded
243 # directly in the path and the query string contains other
244 # auxiliary information. All gitweb installations recognize
245 # URL in either format; this configures in which formats gitweb
248 # To enable system wide have in $GITWEB_CONFIG
249 # $feature{'pathinfo'}{'default'} = [1];
250 # Project specific override is not supported.
252 # Note that you will need to change the default location of CSS,
253 # favicon, logo and possibly other files to an absolute URL. Also,
254 # if gitweb.cgi serves as your indexfile, you will need to force
255 # $my_uri to contain the script name in your $GITWEB_CONFIG.
260 # Make gitweb consider projects in project root subdirectories
261 # to be forks of existing projects. Given project $projname.git,
262 # projects matching $projname/*.git will not be shown in the main
263 # projects list, instead a '+' mark will be added to $projname
264 # there and a 'forks' view will be enabled for the project, listing
265 # all the forks. If project list is taken from a file, forks have
266 # to be listed after the main project.
268 # To enable system wide have in $GITWEB_CONFIG
269 # $feature{'forks'}{'default'} = [1];
270 # Project specific override is not supported.
276 sub gitweb_check_feature {
278 return unless exists $feature{$name};
279 my ($sub, $override, @defaults) = (
280 $feature{$name}{'sub'},
281 $feature{$name}{'override'},
282 @{$feature{$name}{'default'}});
283 if (!$override) { return @defaults; }
285 warn "feature $name is not overrideable";
288 return $sub->(@defaults);
292 my ($val) = git_get_project_config('blame', '--bool');
294 if ($val eq 'true') {
296 } elsif ($val eq 'false') {
303 sub feature_snapshot {
306 my ($val) = git_get_project_config('snapshot');
309 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
310 @fmts = grep { defined } map {
311 exists $known_snapshot_format_aliases{$_} ?
312 $known_snapshot_format_aliases{$_} : $_ } @fmts;
313 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
320 my ($val) = git_get_project_config('grep', '--bool');
322 if ($val eq 'true') {
324 } elsif ($val eq 'false') {
331 sub feature_pickaxe {
332 my ($val) = git_get_project_config('pickaxe', '--bool');
334 if ($val eq 'true') {
336 } elsif ($val eq 'false') {
343 # checking HEAD file with -e is fragile if the repository was
344 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
346 sub check_head_link {
348 my $headfile = "$dir/HEAD";
349 return ((-e $headfile) ||
350 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
353 sub check_export_ok {
355 return (check_head_link($dir) &&
356 (!$export_ok || -e "$dir/$export_ok"));
359 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
360 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
362 # version of the core git binary
363 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
365 $projects_list ||= $projectroot;
367 # ======================================================================
368 # input validation and dispatch
369 our $action = $cgi->param('a');
370 if (defined $action) {
371 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
372 die_error(undef, "Invalid action parameter");
376 # parameters which are pathnames
377 our $project = $cgi->param('p');
378 if (defined $project) {
379 if (!validate_pathname($project) ||
380 !(-d "$projectroot/$project") ||
381 !check_head_link("$projectroot/$project") ||
382 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
383 ($strict_export && !project_in_list($project))) {
385 die_error(undef, "No such project");
389 our $file_name = $cgi->param('f');
390 if (defined $file_name) {
391 if (!validate_pathname($file_name)) {
392 die_error(undef, "Invalid file parameter");
396 our $file_parent = $cgi->param('fp');
397 if (defined $file_parent) {
398 if (!validate_pathname($file_parent)) {
399 die_error(undef, "Invalid file parent parameter");
403 # parameters which are refnames
404 our $hash = $cgi->param('h');
406 if (!validate_refname($hash)) {
407 die_error(undef, "Invalid hash parameter");
411 our $hash_parent = $cgi->param('hp');
412 if (defined $hash_parent) {
413 if (!validate_refname($hash_parent)) {
414 die_error(undef, "Invalid hash parent parameter");
418 our $hash_base = $cgi->param('hb');
419 if (defined $hash_base) {
420 if (!validate_refname($hash_base)) {
421 die_error(undef, "Invalid hash base parameter");
425 my %allowed_options = (
426 "--no-merges" => [ qw(rss atom log shortlog history) ],
429 our @extra_options = $cgi->param('opt');
430 if (defined @extra_options) {
431 foreach(@extra_options)
433 if (not grep(/^$_$/, keys %allowed_options)) {
434 die_error(undef, "Invalid option parameter");
436 if (not grep(/^$action$/, @{$allowed_options{$_}})) {
437 die_error(undef, "Invalid option parameter for this action");
442 our $hash_parent_base = $cgi->param('hpb');
443 if (defined $hash_parent_base) {
444 if (!validate_refname($hash_parent_base)) {
445 die_error(undef, "Invalid hash parent base parameter");
450 our $page = $cgi->param('pg');
452 if ($page =~ m/[^0-9]/) {
453 die_error(undef, "Invalid page parameter");
457 our $searchtype = $cgi->param('st');
458 if (defined $searchtype) {
459 if ($searchtype =~ m/[^a-z]/) {
460 die_error(undef, "Invalid searchtype parameter");
464 our $searchtext = $cgi->param('s');
466 if (defined $searchtext) {
467 if ($searchtype ne 'grep' and $searchtype ne 'pickaxe' and $searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
468 die_error(undef, "Invalid search parameter");
470 if (length($searchtext) < 2) {
471 die_error(undef, "At least two characters are required for search parameter");
473 $search_regexp = quotemeta $searchtext;
476 # now read PATH_INFO and use it as alternative to parameters
477 sub evaluate_path_info {
478 return if defined $project;
479 my $path_info = $ENV{"PATH_INFO"};
480 return if !$path_info;
481 $path_info =~ s,^/+,,;
482 return if !$path_info;
483 # find which part of PATH_INFO is project
484 $project = $path_info;
486 while ($project && !check_head_link("$projectroot/$project")) {
487 $project =~ s,/*[^/]*$,,;
490 $project = validate_pathname($project);
492 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
493 ($strict_export && !project_in_list($project))) {
497 # do not change any parameters if an action is given using the query string
499 $path_info =~ s,^$project/*,,;
500 my ($refname, $pathname) = split(/:/, $path_info, 2);
501 if (defined $pathname) {
502 # we got "project.git/branch:filename" or "project.git/branch:dir/"
503 # we could use git_get_type(branch:pathname), but it needs $git_dir
504 $pathname =~ s,^/+,,;
505 if (!$pathname || substr($pathname, -1) eq "/") {
509 $action ||= "blob_plain";
511 $hash_base ||= validate_refname($refname);
512 $file_name ||= validate_pathname($pathname);
513 } elsif (defined $refname) {
514 # we got "project.git/branch"
515 $action ||= "shortlog";
516 $hash ||= validate_refname($refname);
519 evaluate_path_info();
521 # path to the current git repository
523 $git_dir = "$projectroot/$project" if $project;
527 "blame" => \&git_blame2,
528 "blobdiff" => \&git_blobdiff,
529 "blobdiff_plain" => \&git_blobdiff_plain,
530 "blob" => \&git_blob,
531 "blob_plain" => \&git_blob_plain,
532 "commitdiff" => \&git_commitdiff,
533 "commitdiff_plain" => \&git_commitdiff_plain,
534 "commit" => \&git_commit,
535 "forks" => \&git_forks,
536 "heads" => \&git_heads,
537 "history" => \&git_history,
540 "atom" => \&git_atom,
541 "search" => \&git_search,
542 "search_help" => \&git_search_help,
543 "shortlog" => \&git_shortlog,
544 "summary" => \&git_summary,
546 "tags" => \&git_tags,
547 "tree" => \&git_tree,
548 "snapshot" => \&git_snapshot,
549 "object" => \&git_object,
550 # those below don't need $project
551 "opml" => \&git_opml,
552 "project_list" => \&git_project_list,
553 "project_index" => \&git_project_index,
556 if (!defined $action) {
558 $action = git_get_type($hash);
559 } elsif (defined $hash_base && defined $file_name) {
560 $action = git_get_type("$hash_base:$file_name");
561 } elsif (defined $project) {
564 $action = 'project_list';
567 if (!defined($actions{$action})) {
568 die_error(undef, "Unknown action");
570 if ($action !~ m/^(opml|project_list|project_index)$/ &&
572 die_error(undef, "Project needed");
574 $actions{$action}->();
577 ## ======================================================================
582 # default is to use -absolute url() i.e. $my_uri
583 my $href = $params{-full} ? $my_url : $my_uri;
585 # XXX: Warning: If you touch this, check the search form for updating,
593 extra_options => "opt",
597 hash_parent_base => "hpb",
602 snapshot_format => "sf",
604 my %mapping = @mapping;
606 $params{'project'} = $project unless exists $params{'project'};
608 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
610 # use PATH_INFO for project name
611 $href .= "/$params{'project'}" if defined $params{'project'};
612 delete $params{'project'};
614 # Summary just uses the project path URL
615 if (defined $params{'action'} && $params{'action'} eq 'summary') {
616 delete $params{'action'};
620 # now encode the parameters explicitly
622 for (my $i = 0; $i < @mapping; $i += 2) {
623 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
624 if (defined $params{$name}) {
625 push @result, $symbol . "=" . esc_param($params{$name});
628 $href .= "?" . join(';', @result) if scalar @result;
634 ## ======================================================================
635 ## validation, quoting/unquoting and escaping
637 sub validate_pathname {
638 my $input = shift || return undef;
640 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
641 # at the beginning, at the end, and between slashes.
642 # also this catches doubled slashes
643 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
647 if ($input =~ m!\0!) {
653 sub validate_refname {
654 my $input = shift || return undef;
656 # textual hashes are O.K.
657 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
660 # it must be correct pathname
661 $input = validate_pathname($input)
663 # restrictions on ref name according to git-check-ref-format
664 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
670 # decode sequences of octets in utf8 into Perl's internal form,
671 # which is utf-8 with utf8 flag set if needed. gitweb writes out
672 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
676 eval { $res = decode_utf8($str, Encode::FB_CROAK); };
680 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
684 # quote unsafe chars, but keep the slash, even when it's not
685 # correct, but quoted slashes look too horrible in bookmarks
688 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
694 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
697 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
703 # replace invalid utf8 character with SUBSTITUTION sequence
708 $str = to_utf8($str);
709 $str = $cgi->escapeHTML($str);
710 if ($opts{'-nbsp'}) {
711 $str =~ s/ / /g;
713 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
717 # quote control characters and escape filename to HTML
722 $str = to_utf8($str);
723 $str = $cgi->escapeHTML($str);
724 if ($opts{'-nbsp'}) {
725 $str =~ s/ / /g;
727 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
731 # Make control characters "printable", using character escape codes (CEC)
734 my %es = ( # character escape codes, aka escape sequences
735 "\t" => '\t', # tab (HT)
736 "\n" => '\n', # line feed (LF)
737 "\r" => '\r', # carrige return (CR)
738 "\f" => '\f', # form feed (FF)
739 "\b" => '\b', # backspace (BS)
740 "\a" => '\a', # alarm (bell) (BEL)
741 "\e" => '\e', # escape (ESC)
742 "\013" => '\v', # vertical tab (VT)
743 "\000" => '\0', # nul character (NUL)
745 my $chr = ( (exists $es{$cntrl})
747 : sprintf('\%03o', ord($cntrl)) );
748 return "<span class=\"cntrl\">$chr</span>";
751 # Alternatively use unicode control pictures codepoints,
752 # Unicode "printable representation" (PR)
755 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
756 return "<span class=\"cntrl\">$chr</span>";
759 # git may return quoted and escaped filenames
765 my %es = ( # character escape codes, aka escape sequences
766 't' => "\t", # tab (HT, TAB)
767 'n' => "\n", # newline (NL)
768 'r' => "\r", # return (CR)
769 'f' => "\f", # form feed (FF)
770 'b' => "\b", # backspace (BS)
771 'a' => "\a", # alarm (bell) (BEL)
772 'e' => "\e", # escape (ESC)
773 'v' => "\013", # vertical tab (VT)
776 if ($seq =~ m/^[0-7]{1,3}$/) {
777 # octal char sequence
778 return chr(oct($seq));
779 } elsif (exists $es{$seq}) {
780 # C escape sequence, aka character escape code
783 # quoted ordinary character
787 if ($str =~ m/^"(.*)"$/) {
790 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
795 # escape tabs (convert tabs to spaces)
799 while ((my $pos = index($line, "\t")) != -1) {
800 if (my $count = (8 - ($pos % 8))) {
801 my $spaces = ' ' x $count;
802 $line =~ s/\t/$spaces/;
809 sub project_in_list {
811 my @list = git_get_projects_list();
812 return @list && scalar(grep { $_->{'path'} eq $project } @list);
815 ## ----------------------------------------------------------------------
816 ## HTML aware string manipulation
821 my $add_len = shift || 10;
823 # allow only $len chars, but don't cut a word if it would fit in $add_len
824 # if it doesn't fit, cut it if it's still longer than the dots we would add
825 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
828 if (length($tail) > 4) {
830 $body =~ s/&[^;]*$//; # remove chopped character entities
835 ## ----------------------------------------------------------------------
836 ## functions returning short strings
838 # CSS class for given age value (in seconds)
844 } elsif ($age < 60*60*2) {
846 } elsif ($age < 60*60*24*2) {
853 # convert age in seconds to "nn units ago" string
858 if ($age > 60*60*24*365*2) {
859 $age_str = (int $age/60/60/24/365);
860 $age_str .= " years ago";
861 } elsif ($age > 60*60*24*(365/12)*2) {
862 $age_str = int $age/60/60/24/(365/12);
863 $age_str .= " months ago";
864 } elsif ($age > 60*60*24*7*2) {
865 $age_str = int $age/60/60/24/7;
866 $age_str .= " weeks ago";
867 } elsif ($age > 60*60*24*2) {
868 $age_str = int $age/60/60/24;
869 $age_str .= " days ago";
870 } elsif ($age > 60*60*2) {
871 $age_str = int $age/60/60;
872 $age_str .= " hours ago";
873 } elsif ($age > 60*2) {
874 $age_str = int $age/60;
875 $age_str .= " min ago";
878 $age_str .= " sec ago";
880 $age_str .= " right now";
885 # convert file mode in octal to symbolic file mode string
887 my $mode = oct shift;
889 if (S_ISDIR($mode & S_IFMT)) {
891 } elsif (S_ISLNK($mode)) {
893 } elsif (S_ISREG($mode)) {
894 # git cares only about the executable bit
895 if ($mode & S_IXUSR) {
905 # convert file mode in octal to file type string
909 if ($mode !~ m/^[0-7]+$/) {
915 if (S_ISDIR($mode & S_IFMT)) {
917 } elsif (S_ISLNK($mode)) {
919 } elsif (S_ISREG($mode)) {
926 # convert file mode in octal to file type description string
930 if ($mode !~ m/^[0-7]+$/) {
936 if (S_ISDIR($mode & S_IFMT)) {
938 } elsif (S_ISLNK($mode)) {
940 } elsif (S_ISREG($mode)) {
941 if ($mode & S_IXUSR) {
952 ## ----------------------------------------------------------------------
953 ## functions returning short HTML fragments, or transforming HTML fragments
954 ## which don't belong to other sections
956 # format line of commit message.
957 sub format_log_line_html {
960 $line = esc_html($line, -nbsp=>1);
961 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
964 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
965 -class => "text"}, $hash_text);
966 $line =~ s/$hash_text/$link/;
971 # format marker of refs pointing to given object
972 sub format_ref_marker {
973 my ($refs, $id) = @_;
976 if (defined $refs->{$id}) {
977 foreach my $ref (@{$refs->{$id}}) {
978 my ($type, $name) = qw();
979 # e.g. tags/v2.6.11 or heads/next
980 if ($ref =~ m!^(.*?)s?/(.*)$!) {
988 $markers .= " <span class=\"$type\" title=\"$ref\">" .
989 esc_html($name) . "</span>";
994 return ' <span class="refs">'. $markers . '</span>';
1000 # format, perhaps shortened and with markers, title line
1001 sub format_subject_html {
1002 my ($long, $short, $href, $extra) = @_;
1003 $extra = '' unless defined($extra);
1005 if (length($short) < length($long)) {
1006 return $cgi->a({-href => $href, -class => "list subject",
1007 -title => to_utf8($long)},
1008 esc_html($short) . $extra);
1010 return $cgi->a({-href => $href, -class => "list subject"},
1011 esc_html($long) . $extra);
1015 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1016 sub format_git_diff_header_line {
1018 my $diffinfo = shift;
1019 my ($from, $to) = @_;
1021 if ($diffinfo->{'nparents'}) {
1023 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1024 if ($to->{'href'}) {
1025 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1026 esc_path($to->{'file'}));
1027 } else { # file was deleted (no href)
1028 $line .= esc_path($to->{'file'});
1032 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1033 if ($from->{'href'}) {
1034 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1035 'a/' . esc_path($from->{'file'}));
1036 } else { # file was added (no href)
1037 $line .= 'a/' . esc_path($from->{'file'});
1040 if ($to->{'href'}) {
1041 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1042 'b/' . esc_path($to->{'file'}));
1043 } else { # file was deleted
1044 $line .= 'b/' . esc_path($to->{'file'});
1048 return "<div class=\"diff header\">$line</div>\n";
1051 # format extended diff header line, before patch itself
1052 sub format_extended_diff_header_line {
1054 my $diffinfo = shift;
1055 my ($from, $to) = @_;
1058 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1059 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1060 esc_path($from->{'file'}));
1062 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1063 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1064 esc_path($to->{'file'}));
1066 # match single <mode>
1067 if ($line =~ m/\s(\d{6})$/) {
1068 $line .= '<span class="info"> (' .
1069 file_type_long($1) .
1073 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1074 # can match only for combined diff
1076 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1077 if ($from->{'href'}[$i]) {
1078 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1080 substr($diffinfo->{'from_id'}[$i],0,7));
1085 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1088 if ($to->{'href'}) {
1089 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1090 substr($diffinfo->{'to_id'},0,7));
1095 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1096 # can match only for ordinary diff
1097 my ($from_link, $to_link);
1098 if ($from->{'href'}) {
1099 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1100 substr($diffinfo->{'from_id'},0,7));
1102 $from_link = '0' x 7;
1104 if ($to->{'href'}) {
1105 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1106 substr($diffinfo->{'to_id'},0,7));
1110 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1111 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1114 return $line . "<br/>\n";
1117 # format from-file/to-file diff header
1118 sub format_diff_from_to_header {
1119 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1124 #assert($line =~ m/^---/) if DEBUG;
1125 # no extra formatting for "^--- /dev/null"
1126 if (! $diffinfo->{'nparents'}) {
1127 # ordinary (single parent) diff
1128 if ($line =~ m!^--- "?a/!) {
1129 if ($from->{'href'}) {
1131 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1132 esc_path($from->{'file'}));
1135 esc_path($from->{'file'});
1138 $result .= qq!<div class="diff from_file">$line</div>\n!;
1141 # combined diff (merge commit)
1142 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1143 if ($from->{'href'}[$i]) {
1145 $cgi->a({-href=>href(action=>"blobdiff",
1146 hash_parent=>$diffinfo->{'from_id'}[$i],
1147 hash_parent_base=>$parents[$i],
1148 file_parent=>$from->{'file'}[$i],
1149 hash=>$diffinfo->{'to_id'},
1151 file_name=>$to->{'file'}),
1153 -title=>"diff" . ($i+1)},
1156 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1157 esc_path($from->{'file'}[$i]));
1159 $line = '--- /dev/null';
1161 $result .= qq!<div class="diff from_file">$line</div>\n!;
1166 #assert($line =~ m/^\+\+\+/) if DEBUG;
1167 # no extra formatting for "^+++ /dev/null"
1168 if ($line =~ m!^\+\+\+ "?b/!) {
1169 if ($to->{'href'}) {
1171 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1172 esc_path($to->{'file'}));
1175 esc_path($to->{'file'});
1178 $result .= qq!<div class="diff to_file">$line</div>\n!;
1183 # create note for patch simplified by combined diff
1184 sub format_diff_cc_simplified {
1185 my ($diffinfo, @parents) = @_;
1188 $result .= "<div class=\"diff header\">" .
1190 if (!is_deleted($diffinfo)) {
1191 $result .= $cgi->a({-href => href(action=>"blob",
1193 hash=>$diffinfo->{'to_id'},
1194 file_name=>$diffinfo->{'to_file'}),
1196 esc_path($diffinfo->{'to_file'}));
1198 $result .= esc_path($diffinfo->{'to_file'});
1200 $result .= "</div>\n" . # class="diff header"
1201 "<div class=\"diff nodifferences\">" .
1203 "</div>\n"; # class="diff nodifferences"
1208 # format patch (diff) line (not to be used for diff headers)
1209 sub format_diff_line {
1211 my ($from, $to) = @_;
1212 my $diff_class = "";
1216 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1218 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1219 if ($line =~ m/^\@{3}/) {
1220 $diff_class = " chunk_header";
1221 } elsif ($line =~ m/^\\/) {
1222 $diff_class = " incomplete";
1223 } elsif ($prefix =~ tr/+/+/) {
1224 $diff_class = " add";
1225 } elsif ($prefix =~ tr/-/-/) {
1226 $diff_class = " rem";
1229 # assume ordinary diff
1230 my $char = substr($line, 0, 1);
1232 $diff_class = " add";
1233 } elsif ($char eq '-') {
1234 $diff_class = " rem";
1235 } elsif ($char eq '@') {
1236 $diff_class = " chunk_header";
1237 } elsif ($char eq "\\") {
1238 $diff_class = " incomplete";
1241 $line = untabify($line);
1242 if ($from && $to && $line =~ m/^\@{2} /) {
1243 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1244 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1246 $from_lines = 0 unless defined $from_lines;
1247 $to_lines = 0 unless defined $to_lines;
1249 if ($from->{'href'}) {
1250 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1251 -class=>"list"}, $from_text);
1253 if ($to->{'href'}) {
1254 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1255 -class=>"list"}, $to_text);
1257 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1258 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1259 return "<div class=\"diff$diff_class\">$line</div>\n";
1260 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1261 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1262 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1264 @from_text = split(' ', $ranges);
1265 for (my $i = 0; $i < @from_text; ++$i) {
1266 ($from_start[$i], $from_nlines[$i]) =
1267 (split(',', substr($from_text[$i], 1)), 0);
1270 $to_text = pop @from_text;
1271 $to_start = pop @from_start;
1272 $to_nlines = pop @from_nlines;
1274 $line = "<span class=\"chunk_info\">$prefix ";
1275 for (my $i = 0; $i < @from_text; ++$i) {
1276 if ($from->{'href'}[$i]) {
1277 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1278 -class=>"list"}, $from_text[$i]);
1280 $line .= $from_text[$i];
1284 if ($to->{'href'}) {
1285 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1286 -class=>"list"}, $to_text);
1290 $line .= " $prefix</span>" .
1291 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1292 return "<div class=\"diff$diff_class\">$line</div>\n";
1294 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1297 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1298 # linked. Pass the hash of the tree/commit to snapshot.
1299 sub format_snapshot_links {
1301 my @snapshot_fmts = gitweb_check_feature('snapshot');
1302 my $num_fmts = @snapshot_fmts;
1303 if ($num_fmts > 1) {
1304 # A parenthesized list of links bearing format names.
1305 return "snapshot (" . join(' ', map
1312 }, $known_snapshot_formats{$_}{'display'})
1313 , @snapshot_fmts) . ")";
1314 } elsif ($num_fmts == 1) {
1315 # A single "snapshot" link whose tooltip bears the format name.
1316 my ($fmt) = @snapshot_fmts;
1321 snapshot_format=>$fmt
1323 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1325 } else { # $num_fmts == 0
1330 ## ----------------------------------------------------------------------
1331 ## git utility subroutines, invoking git commands
1333 # returns path to the core git executable and the --git-dir parameter as list
1335 return $GIT, '--git-dir='.$git_dir;
1338 # returns path to the core git executable and the --git-dir parameter as string
1340 return join(' ', git_cmd());
1343 # get HEAD ref of given project as hash
1344 sub git_get_head_hash {
1345 my $project = shift;
1346 my $o_git_dir = $git_dir;
1348 $git_dir = "$projectroot/$project";
1349 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1352 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1356 if (defined $o_git_dir) {
1357 $git_dir = $o_git_dir;
1362 # get type of given object
1366 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1368 close $fd or return;
1373 sub git_get_project_config {
1374 my ($key, $type) = @_;
1376 return unless ($key);
1377 $key =~ s/^gitweb\.//;
1378 return if ($key =~ m/\W/);
1380 my @x = (git_cmd(), 'config');
1381 if (defined $type) { push @x, $type; }
1383 push @x, "gitweb.$key";
1389 # get hash of given path at given ref
1390 sub git_get_hash_by_path {
1392 my $path = shift || return undef;
1397 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1398 or die_error(undef, "Open git-ls-tree failed");
1400 close $fd or return undef;
1402 if (!defined $line) {
1403 # there is no tree or hash given by $path at $base
1407 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1408 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1409 if (defined $type && $type ne $2) {
1410 # type doesn't match
1416 # get path of entry with given hash at given tree-ish (ref)
1417 # used to get 'from' filename for combined diff (merge commit) for renames
1418 sub git_get_path_by_hash {
1419 my $base = shift || return;
1420 my $hash = shift || return;
1424 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1426 while (my $line = <$fd>) {
1429 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1430 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1431 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1440 ## ......................................................................
1441 ## git utility functions, directly accessing git repository
1443 sub git_get_project_description {
1446 open my $fd, "$projectroot/$path/description" or return undef;
1449 if (defined $descr) {
1455 sub git_get_project_url_list {
1458 open my $fd, "$projectroot/$path/cloneurl" or return;
1459 my @git_project_url_list = map { chomp; $_ } <$fd>;
1462 return wantarray ? @git_project_url_list : \@git_project_url_list;
1465 sub git_get_projects_list {
1470 $filter =~ s/\.git$//;
1472 my ($check_forks) = gitweb_check_feature('forks');
1474 if (-d $projects_list) {
1475 # search in directory
1476 my $dir = $projects_list . ($filter ? "/$filter" : '');
1477 # remove the trailing "/"
1479 my $pfxlen = length("$dir");
1482 follow_fast => 1, # follow symbolic links
1483 dangling_symlinks => 0, # ignore dangling symlinks, silently
1485 # skip project-list toplevel, if we get it.
1486 return if (m!^[/.]$!);
1487 # only directories can be git repositories
1488 return unless (-d $_);
1490 my $subdir = substr($File::Find::name, $pfxlen + 1);
1491 # we check related file in $projectroot
1492 if ($check_forks and $subdir =~ m#/.#) {
1493 $File::Find::prune = 1;
1494 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1495 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1496 $File::Find::prune = 1;
1501 } elsif (-f $projects_list) {
1502 # read from file(url-encoded):
1503 # 'git%2Fgit.git Linus+Torvalds'
1504 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1505 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1507 open my ($fd), $projects_list or return;
1509 while (my $line = <$fd>) {
1511 my ($path, $owner) = split ' ', $line;
1512 $path = unescape($path);
1513 $owner = unescape($owner);
1514 if (!defined $path) {
1517 if ($filter ne '') {
1518 # looking for forks;
1519 my $pfx = substr($path, 0, length($filter));
1520 if ($pfx ne $filter) {
1523 my $sfx = substr($path, length($filter));
1524 if ($sfx !~ /^\/.*\.git$/) {
1527 } elsif ($check_forks) {
1529 foreach my $filter (keys %paths) {
1530 # looking for forks;
1531 my $pfx = substr($path, 0, length($filter));
1532 if ($pfx ne $filter) {
1535 my $sfx = substr($path, length($filter));
1536 if ($sfx !~ /^\/.*\.git$/) {
1539 # is a fork, don't include it in
1544 if (check_export_ok("$projectroot/$path")) {
1547 owner => to_utf8($owner),
1550 (my $forks_path = $path) =~ s/\.git$//;
1551 $paths{$forks_path}++;
1559 our $gitweb_project_owner = undef;
1560 sub git_get_project_list_from_file {
1562 return if (defined $gitweb_project_owner);
1564 $gitweb_project_owner = {};
1565 # read from file (url-encoded):
1566 # 'git%2Fgit.git Linus+Torvalds'
1567 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1568 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1569 if (-f $projects_list) {
1570 open (my $fd , $projects_list);
1571 while (my $line = <$fd>) {
1573 my ($pr, $ow) = split ' ', $line;
1574 $pr = unescape($pr);
1575 $ow = unescape($ow);
1576 $gitweb_project_owner->{$pr} = to_utf8($ow);
1582 sub git_get_project_owner {
1583 my $project = shift;
1586 return undef unless $project;
1588 if (!defined $gitweb_project_owner) {
1589 git_get_project_list_from_file();
1592 if (exists $gitweb_project_owner->{$project}) {
1593 $owner = $gitweb_project_owner->{$project};
1595 if (!defined $owner) {
1596 $owner = get_file_owner("$projectroot/$project");
1602 sub git_get_last_activity {
1606 $git_dir = "$projectroot/$path";
1607 open($fd, "-|", git_cmd(), 'for-each-ref',
1608 '--format=%(committer)',
1609 '--sort=-committerdate',
1611 'refs/heads') or return;
1612 my $most_recent = <$fd>;
1613 close $fd or return;
1614 if (defined $most_recent &&
1615 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1617 my $age = time - $timestamp;
1618 return ($age, age_string($age));
1620 return (undef, undef);
1623 sub git_get_references {
1624 my $type = shift || "";
1626 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1627 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1628 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1629 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1632 while (my $line = <$fd>) {
1634 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1635 if (defined $refs{$1}) {
1636 push @{$refs{$1}}, $2;
1642 close $fd or return;
1646 sub git_get_rev_name_tags {
1647 my $hash = shift || return undef;
1649 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1651 my $name_rev = <$fd>;
1654 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1657 # catches also '$hash undefined' output
1662 ## ----------------------------------------------------------------------
1663 ## parse to hash functions
1667 my $tz = shift || "-0000";
1670 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1671 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1672 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1673 $date{'hour'} = $hour;
1674 $date{'minute'} = $min;
1675 $date{'mday'} = $mday;
1676 $date{'day'} = $days[$wday];
1677 $date{'month'} = $months[$mon];
1678 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1679 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1680 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1681 $mday, $months[$mon], $hour ,$min;
1682 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1683 1900+$year, $mon, $mday, $hour ,$min, $sec;
1685 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1686 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1687 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1688 $date{'hour_local'} = $hour;
1689 $date{'minute_local'} = $min;
1690 $date{'tz_local'} = $tz;
1691 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1692 1900+$year, $mon+1, $mday,
1693 $hour, $min, $sec, $tz);
1702 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1703 $tag{'id'} = $tag_id;
1704 while (my $line = <$fd>) {
1706 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1707 $tag{'object'} = $1;
1708 } elsif ($line =~ m/^type (.+)$/) {
1710 } elsif ($line =~ m/^tag (.+)$/) {
1712 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1713 $tag{'author'} = $1;
1716 } elsif ($line =~ m/--BEGIN/) {
1717 push @comment, $line;
1719 } elsif ($line eq "") {
1723 push @comment, <$fd>;
1724 $tag{'comment'} = \@comment;
1725 close $fd or return;
1726 if (!defined $tag{'name'}) {
1732 sub parse_commit_text {
1733 my ($commit_text, $withparents) = @_;
1734 my @commit_lines = split '\n', $commit_text;
1737 pop @commit_lines; # Remove '\0'
1739 if (! @commit_lines) {
1743 my $header = shift @commit_lines;
1744 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1747 ($co{'id'}, my @parents) = split ' ', $header;
1748 while (my $line = shift @commit_lines) {
1749 last if $line eq "\n";
1750 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1752 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1754 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1756 $co{'author_epoch'} = $2;
1757 $co{'author_tz'} = $3;
1758 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1759 $co{'author_name'} = $1;
1760 $co{'author_email'} = $2;
1762 $co{'author_name'} = $co{'author'};
1764 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1765 $co{'committer'} = $1;
1766 $co{'committer_epoch'} = $2;
1767 $co{'committer_tz'} = $3;
1768 $co{'committer_name'} = $co{'committer'};
1769 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1770 $co{'committer_name'} = $1;
1771 $co{'committer_email'} = $2;
1773 $co{'committer_name'} = $co{'committer'};
1777 if (!defined $co{'tree'}) {
1780 $co{'parents'} = \@parents;
1781 $co{'parent'} = $parents[0];
1783 foreach my $title (@commit_lines) {
1786 $co{'title'} = chop_str($title, 80, 5);
1787 # remove leading stuff of merges to make the interesting part visible
1788 if (length($title) > 50) {
1789 $title =~ s/^Automatic //;
1790 $title =~ s/^merge (of|with) /Merge ... /i;
1791 if (length($title) > 50) {
1792 $title =~ s/(http|rsync):\/\///;
1794 if (length($title) > 50) {
1795 $title =~ s/(master|www|rsync)\.//;
1797 if (length($title) > 50) {
1798 $title =~ s/kernel.org:?//;
1800 if (length($title) > 50) {
1801 $title =~ s/\/pub\/scm//;
1804 $co{'title_short'} = chop_str($title, 50, 5);
1808 if ($co{'title'} eq "") {
1809 $co{'title'} = $co{'title_short'} = '(no commit message)';
1811 # remove added spaces
1812 foreach my $line (@commit_lines) {
1815 $co{'comment'} = \@commit_lines;
1817 my $age = time - $co{'committer_epoch'};
1819 $co{'age_string'} = age_string($age);
1820 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1821 if ($age > 60*60*24*7*2) {
1822 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1823 $co{'age_string_age'} = $co{'age_string'};
1825 $co{'age_string_date'} = $co{'age_string'};
1826 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1832 my ($commit_id) = @_;
1837 open my $fd, "-|", git_cmd(), "rev-list",
1843 or die_error(undef, "Open git-rev-list failed");
1844 %co = parse_commit_text(<$fd>, 1);
1851 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1859 open my $fd, "-|", git_cmd(), "rev-list",
1861 ($arg ? ($arg) : ()),
1862 ("--max-count=" . $maxcount),
1863 ("--skip=" . $skip),
1867 ($filename ? ($filename) : ())
1868 or die_error(undef, "Open git-rev-list failed");
1869 while (my $line = <$fd>) {
1870 my %co = parse_commit_text($line);
1875 return wantarray ? @cos : \@cos;
1878 # parse ref from ref_file, given by ref_id, with given type
1880 my $ref_file = shift;
1882 my $type = shift || git_get_type($ref_id);
1885 $ref_item{'type'} = $type;
1886 $ref_item{'id'} = $ref_id;
1887 $ref_item{'epoch'} = 0;
1888 $ref_item{'age'} = "unknown";
1889 if ($type eq "tag") {
1890 my %tag = parse_tag($ref_id);
1891 $ref_item{'comment'} = $tag{'comment'};
1892 if ($tag{'type'} eq "commit") {
1893 my %co = parse_commit($tag{'object'});
1894 $ref_item{'epoch'} = $co{'committer_epoch'};
1895 $ref_item{'age'} = $co{'age_string'};
1896 } elsif (defined($tag{'epoch'})) {
1897 my $age = time - $tag{'epoch'};
1898 $ref_item{'epoch'} = $tag{'epoch'};
1899 $ref_item{'age'} = age_string($age);
1901 $ref_item{'reftype'} = $tag{'type'};
1902 $ref_item{'name'} = $tag{'name'};
1903 $ref_item{'refid'} = $tag{'object'};
1904 } elsif ($type eq "commit"){
1905 my %co = parse_commit($ref_id);
1906 $ref_item{'reftype'} = "commit";
1907 $ref_item{'name'} = $ref_file;
1908 $ref_item{'title'} = $co{'title'};
1909 $ref_item{'refid'} = $ref_id;
1910 $ref_item{'epoch'} = $co{'committer_epoch'};
1911 $ref_item{'age'} = $co{'age_string'};
1913 $ref_item{'reftype'} = $type;
1914 $ref_item{'name'} = $ref_file;
1915 $ref_item{'refid'} = $ref_id;
1921 # parse line of git-diff-tree "raw" output
1922 sub parse_difftree_raw_line {
1926 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1927 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1928 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1929 $res{'from_mode'} = $1;
1930 $res{'to_mode'} = $2;
1931 $res{'from_id'} = $3;
1933 $res{'status'} = $5;
1934 $res{'similarity'} = $6;
1935 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1936 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1938 $res{'file'} = unquote($7);
1941 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1942 # combined diff (for merge commit)
1943 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1944 $res{'nparents'} = length($1);
1945 $res{'from_mode'} = [ split(' ', $2) ];
1946 $res{'to_mode'} = pop @{$res{'from_mode'}};
1947 $res{'from_id'} = [ split(' ', $3) ];
1948 $res{'to_id'} = pop @{$res{'from_id'}};
1949 $res{'status'} = [ split('', $4) ];
1950 $res{'to_file'} = unquote($5);
1952 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1953 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1954 $res{'commit'} = $1;
1957 return wantarray ? %res : \%res;
1960 # parse line of git-ls-tree output
1961 sub parse_ls_tree_line ($;%) {
1966 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1967 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1975 $res{'name'} = unquote($4);
1978 return wantarray ? %res : \%res;
1981 # generates _two_ hashes, references to which are passed as 2 and 3 argument
1982 sub parse_from_to_diffinfo {
1983 my ($diffinfo, $from, $to, @parents) = @_;
1985 if ($diffinfo->{'nparents'}) {
1987 $from->{'file'} = [];
1988 $from->{'href'} = [];
1989 fill_from_file_info($diffinfo, @parents)
1990 unless exists $diffinfo->{'from_file'};
1991 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1992 $from->{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
1993 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
1994 $from->{'href'}[$i] = href(action=>"blob",
1995 hash_base=>$parents[$i],
1996 hash=>$diffinfo->{'from_id'}[$i],
1997 file_name=>$from->{'file'}[$i]);
1999 $from->{'href'}[$i] = undef;
2003 $from->{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2004 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2005 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2006 hash=>$diffinfo->{'from_id'},
2007 file_name=>$from->{'file'});
2009 delete $from->{'href'};
2013 $to->{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2014 if (!is_deleted($diffinfo)) { # file exists in result
2015 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2016 hash=>$diffinfo->{'to_id'},
2017 file_name=>$to->{'file'});
2019 delete $to->{'href'};
2023 ## ......................................................................
2024 ## parse to array of hashes functions
2026 sub git_get_heads_list {
2030 open my $fd, '-|', git_cmd(), 'for-each-ref',
2031 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2032 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2035 while (my $line = <$fd>) {
2039 my ($refinfo, $committerinfo) = split(/\0/, $line);
2040 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2041 my ($committer, $epoch, $tz) =
2042 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2043 $name =~ s!^refs/heads/!!;
2045 $ref_item{'name'} = $name;
2046 $ref_item{'id'} = $hash;
2047 $ref_item{'title'} = $title || '(no commit message)';
2048 $ref_item{'epoch'} = $epoch;
2050 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2052 $ref_item{'age'} = "unknown";
2055 push @headslist, \%ref_item;
2059 return wantarray ? @headslist : \@headslist;
2062 sub git_get_tags_list {
2066 open my $fd, '-|', git_cmd(), 'for-each-ref',
2067 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2068 '--format=%(objectname) %(objecttype) %(refname) '.
2069 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2072 while (my $line = <$fd>) {
2076 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2077 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2078 my ($creator, $epoch, $tz) =
2079 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2080 $name =~ s!^refs/tags/!!;
2082 $ref_item{'type'} = $type;
2083 $ref_item{'id'} = $id;
2084 $ref_item{'name'} = $name;
2085 if ($type eq "tag") {
2086 $ref_item{'subject'} = $title;
2087 $ref_item{'reftype'} = $reftype;
2088 $ref_item{'refid'} = $refid;
2090 $ref_item{'reftype'} = $type;
2091 $ref_item{'refid'} = $id;
2094 if ($type eq "tag" || $type eq "commit") {
2095 $ref_item{'epoch'} = $epoch;
2097 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2099 $ref_item{'age'} = "unknown";
2103 push @tagslist, \%ref_item;
2107 return wantarray ? @tagslist : \@tagslist;
2110 ## ----------------------------------------------------------------------
2111 ## filesystem-related functions
2113 sub get_file_owner {
2116 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2117 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2118 if (!defined $gcos) {
2122 $owner =~ s/[,;].*$//;
2123 return to_utf8($owner);
2126 ## ......................................................................
2127 ## mimetype related functions
2129 sub mimetype_guess_file {
2130 my $filename = shift;
2131 my $mimemap = shift;
2132 -r $mimemap or return undef;
2135 open(MIME, $mimemap) or return undef;
2137 next if m/^#/; # skip comments
2138 my ($mime, $exts) = split(/\t+/);
2139 if (defined $exts) {
2140 my @exts = split(/\s+/, $exts);
2141 foreach my $ext (@exts) {
2142 $mimemap{$ext} = $mime;
2148 $filename =~ /\.([^.]*)$/;
2149 return $mimemap{$1};
2152 sub mimetype_guess {
2153 my $filename = shift;
2155 $filename =~ /\./ or return undef;
2157 if ($mimetypes_file) {
2158 my $file = $mimetypes_file;
2159 if ($file !~ m!^/!) { # if it is relative path
2160 # it is relative to project
2161 $file = "$projectroot/$project/$file";
2163 $mime = mimetype_guess_file($filename, $file);
2165 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2171 my $filename = shift;
2174 my $mime = mimetype_guess($filename);
2175 $mime and return $mime;
2179 return $default_blob_plain_mimetype unless $fd;
2182 return 'text/plain' .
2183 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2184 } elsif (! $filename) {
2185 return 'application/octet-stream';
2186 } elsif ($filename =~ m/\.png$/i) {
2188 } elsif ($filename =~ m/\.gif$/i) {
2190 } elsif ($filename =~ m/\.jpe?g$/i) {
2191 return 'image/jpeg';
2193 return 'application/octet-stream';
2197 ## ======================================================================
2198 ## functions printing HTML: header, footer, error page
2200 sub git_header_html {
2201 my $status = shift || "200 OK";
2202 my $expires = shift;
2204 my $title = "$site_name";
2205 if (defined $project) {
2206 $title .= " - " . to_utf8($project);
2207 if (defined $action) {
2208 $title .= "/$action";
2209 if (defined $file_name) {
2210 $title .= " - " . esc_path($file_name);
2211 if ($action eq "tree" && $file_name !~ m|/$|) {
2218 # require explicit support from the UA if we are to send the page as
2219 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2220 # we have to do this because MSIE sometimes globs '*/*', pretending to
2221 # support xhtml+xml but choking when it gets what it asked for.
2222 if (defined $cgi->http('HTTP_ACCEPT') &&
2223 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2224 $cgi->Accept('application/xhtml+xml') != 0) {
2225 $content_type = 'application/xhtml+xml';
2227 $content_type = 'text/html';
2229 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2230 -status=> $status, -expires => $expires);
2231 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2233 <?xml version="1.0" encoding="utf-8"?>
2234 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2235 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2236 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2237 <!-- git core binaries version $git_version -->
2239 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2240 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2241 <meta name="robots" content="index, nofollow"/>
2242 <title>$title</title>
2244 # print out each stylesheet that exist
2245 if (defined $stylesheet) {
2246 #provides backwards capability for those people who define style sheet in a config file
2247 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2249 foreach my $stylesheet (@stylesheets) {
2250 next unless $stylesheet;
2251 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2254 if (defined $project) {
2255 printf('<link rel="alternate" title="%s log RSS feed" '.
2256 'href="%s" type="application/rss+xml" />'."\n",
2257 esc_param($project), href(action=>"rss"));
2258 printf('<link rel="alternate" title="%s log Atom feed" '.
2259 'href="%s" type="application/atom+xml" />'."\n",
2260 esc_param($project), href(action=>"atom"));
2262 printf('<link rel="alternate" title="%s projects list" '.
2263 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2264 $site_name, href(project=>undef, action=>"project_index"));
2265 printf('<link rel="alternate" title="%s projects feeds" '.
2266 'href="%s" type="text/x-opml"/>'."\n",
2267 $site_name, href(project=>undef, action=>"opml"));
2269 if (defined $favicon) {
2270 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2276 if (-f $site_header) {
2277 open (my $fd, $site_header);
2282 print "<div class=\"page_header\">\n" .
2283 $cgi->a({-href => esc_url($logo_url),
2284 -title => $logo_label},
2285 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2286 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2287 if (defined $project) {
2288 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2289 if (defined $action) {
2296 my ($have_search) = gitweb_check_feature('search');
2297 if ((defined $project) && ($have_search)) {
2298 if (!defined $searchtext) {
2302 if (defined $hash_base) {
2303 $search_hash = $hash_base;
2304 } elsif (defined $hash) {
2305 $search_hash = $hash;
2307 $search_hash = "HEAD";
2309 my $action = $my_uri;
2310 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2311 if ($use_pathinfo) {
2312 $action .= "/$project";
2314 $cgi->param("p", $project);
2316 $cgi->param("a", "search");
2317 $cgi->param("h", $search_hash);
2318 print $cgi->startform(-method => "get", -action => $action) .
2319 "<div class=\"search\">\n" .
2320 (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") .
2321 $cgi->hidden(-name => "a") . "\n" .
2322 $cgi->hidden(-name => "h") . "\n" .
2323 $cgi->popup_menu(-name => 'st', -default => 'commit',
2324 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2325 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2327 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2329 $cgi->end_form() . "\n";
2333 sub git_footer_html {
2334 print "<div class=\"page_footer\">\n";
2335 if (defined $project) {
2336 my $descr = git_get_project_description($project);
2337 if (defined $descr) {
2338 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2340 print $cgi->a({-href => href(action=>"rss"),
2341 -class => "rss_logo"}, "RSS") . " ";
2342 print $cgi->a({-href => href(action=>"atom"),
2343 -class => "rss_logo"}, "Atom") . "\n";
2345 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2346 -class => "rss_logo"}, "OPML") . " ";
2347 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2348 -class => "rss_logo"}, "TXT") . "\n";
2352 if (-f $site_footer) {
2353 open (my $fd, $site_footer);
2363 my $status = shift || "403 Forbidden";
2364 my $error = shift || "Malformed query, file missing or permission denied";
2366 git_header_html($status);
2368 <div class="page_body">
2378 ## ----------------------------------------------------------------------
2379 ## functions printing or outputting HTML: navigation
2381 sub git_print_page_nav {
2382 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2383 $extra = '' if !defined $extra; # pager or formats
2385 my @navs = qw(summary shortlog log commit commitdiff tree);
2387 @navs = grep { $_ ne $suppress } @navs;
2390 my %arg = map { $_ => {action=>$_} } @navs;
2391 if (defined $head) {
2392 for (qw(commit commitdiff)) {
2393 $arg{$_}{'hash'} = $head;
2395 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2396 for (qw(shortlog log)) {
2397 $arg{$_}{'hash'} = $head;
2401 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2402 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2404 print "<div class=\"page_nav\">\n" .
2406 map { $_ eq $current ?
2407 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2409 print "<br/>\n$extra<br/>\n" .
2413 sub format_paging_nav {
2414 my ($action, $hash, $head, $page, $nrevs) = @_;
2418 if ($hash ne $head || $page) {
2419 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2421 $paging_nav .= "HEAD";
2425 $paging_nav .= " ⋅ " .
2426 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2427 -accesskey => "p", -title => "Alt-p"}, "prev");
2429 $paging_nav .= " ⋅ prev";
2432 if ($nrevs >= (100 * ($page+1)-1)) {
2433 $paging_nav .= " ⋅ " .
2434 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2435 -accesskey => "n", -title => "Alt-n"}, "next");
2437 $paging_nav .= " ⋅ next";
2443 ## ......................................................................
2444 ## functions printing or outputting HTML: div
2446 sub git_print_header_div {
2447 my ($action, $title, $hash, $hash_base) = @_;
2450 $args{'action'} = $action;
2451 $args{'hash'} = $hash if $hash;
2452 $args{'hash_base'} = $hash_base if $hash_base;
2454 print "<div class=\"header\">\n" .
2455 $cgi->a({-href => href(%args), -class => "title"},
2456 $title ? $title : $action) .
2460 #sub git_print_authorship (\%) {
2461 sub git_print_authorship {
2464 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2465 print "<div class=\"author_date\">" .
2466 esc_html($co->{'author_name'}) .
2468 if ($ad{'hour_local'} < 6) {
2469 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2470 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2472 printf(" (%02d:%02d %s)",
2473 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2478 sub git_print_page_path {
2484 print "<div class=\"page_path\">";
2485 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2486 -title => 'tree root'}, to_utf8("[$project]"));
2488 if (defined $name) {
2489 my @dirname = split '/', $name;
2490 my $basename = pop @dirname;
2493 foreach my $dir (@dirname) {
2494 $fullname .= ($fullname ? '/' : '') . $dir;
2495 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2497 -title => $fullname}, esc_path($dir));
2500 if (defined $type && $type eq 'blob') {
2501 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2503 -title => $name}, esc_path($basename));
2504 } elsif (defined $type && $type eq 'tree') {
2505 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2507 -title => $name}, esc_path($basename));
2510 print esc_path($basename);
2513 print "<br/></div>\n";
2516 # sub git_print_log (\@;%) {
2517 sub git_print_log ($;%) {
2521 if ($opts{'-remove_title'}) {
2522 # remove title, i.e. first line of log
2525 # remove leading empty lines
2526 while (defined $log->[0] && $log->[0] eq "") {
2533 foreach my $line (@$log) {
2534 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2537 if (! $opts{'-remove_signoff'}) {
2538 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2541 # remove signoff lines
2548 # print only one empty line
2549 # do not print empty line after signoff
2551 next if ($empty || $signoff);
2557 print format_log_line_html($line) . "<br/>\n";
2560 if ($opts{'-final_empty_line'}) {
2561 # end with single empty line
2562 print "<br/>\n" unless $empty;
2566 # return link target (what link points to)
2567 sub git_get_link_target {
2572 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2576 $link_target = <$fd>;
2581 return $link_target;
2584 # given link target, and the directory (basedir) the link is in,
2585 # return target of link relative to top directory (top tree);
2586 # return undef if it is not possible (including absolute links).
2587 sub normalize_link_target {
2588 my ($link_target, $basedir, $hash_base) = @_;
2590 # we can normalize symlink target only if $hash_base is provided
2591 return unless $hash_base;
2593 # absolute symlinks (beginning with '/') cannot be normalized
2594 return if (substr($link_target, 0, 1) eq '/');
2596 # normalize link target to path from top (root) tree (dir)
2599 $path = $basedir . '/' . $link_target;
2601 # we are in top (root) tree (dir)
2602 $path = $link_target;
2605 # remove //, /./, and /../
2607 foreach my $part (split('/', $path)) {
2608 # discard '.' and ''
2609 next if (!$part || $part eq '.');
2611 if ($part eq '..') {
2615 # link leads outside repository (outside top dir)
2619 push @path_parts, $part;
2622 $path = join('/', @path_parts);
2627 # print tree entry (row of git_tree), but without encompassing <tr> element
2628 sub git_print_tree_entry {
2629 my ($t, $basedir, $hash_base, $have_blame) = @_;
2632 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2634 # The format of a table row is: mode list link. Where mode is
2635 # the mode of the entry, list is the name of the entry, an href,
2636 # and link is the action links of the entry.
2638 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2639 if ($t->{'type'} eq "blob") {
2640 print "<td class=\"list\">" .
2641 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2642 file_name=>"$basedir$t->{'name'}", %base_key),
2643 -class => "list"}, esc_path($t->{'name'}));
2644 if (S_ISLNK(oct $t->{'mode'})) {
2645 my $link_target = git_get_link_target($t->{'hash'});
2647 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2648 if (defined $norm_target) {
2650 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2651 file_name=>$norm_target),
2652 -title => $norm_target}, esc_path($link_target));
2654 print " -> " . esc_path($link_target);
2659 print "<td class=\"link\">";
2660 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2661 file_name=>"$basedir$t->{'name'}", %base_key)},
2665 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2666 file_name=>"$basedir$t->{'name'}", %base_key)},
2669 if (defined $hash_base) {
2671 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2672 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2676 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2677 file_name=>"$basedir$t->{'name'}")},
2681 } elsif ($t->{'type'} eq "tree") {
2682 print "<td class=\"list\">";
2683 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2684 file_name=>"$basedir$t->{'name'}", %base_key)},
2685 esc_path($t->{'name'}));
2687 print "<td class=\"link\">";
2688 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2689 file_name=>"$basedir$t->{'name'}", %base_key)},
2691 if (defined $hash_base) {
2693 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2694 file_name=>"$basedir$t->{'name'}")},
2701 ## ......................................................................
2702 ## functions printing large fragments of HTML
2704 sub fill_from_file_info {
2705 my ($diff, @parents) = @_;
2707 $diff->{'from_file'} = [ ];
2708 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2709 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2710 if ($diff->{'status'}[$i] eq 'R' ||
2711 $diff->{'status'}[$i] eq 'C') {
2712 $diff->{'from_file'}[$i] =
2713 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2720 # parameters can be strings, or references to arrays of strings
2724 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2725 for (my $i = 0; $i < @$a; ++$i) {
2726 return 0 unless ($a->[$i] eq $b->[$i]);
2729 } elsif (!ref($a) && !ref($b)) {
2737 my $diffinfo = shift;
2739 return $diffinfo->{'to_id'} eq ('0' x 40);
2742 sub git_difftree_body {
2743 my ($difftree, $hash, @parents) = @_;
2744 my ($parent) = $parents[0];
2745 my ($have_blame) = gitweb_check_feature('blame');
2746 print "<div class=\"list_head\">\n";
2747 if ($#{$difftree} > 10) {
2748 print(($#{$difftree} + 1) . " files changed:\n");
2752 print "<table class=\"" .
2753 (@parents > 1 ? "combined " : "") .
2756 # header only for combined diff in 'commitdiff' view
2757 my $has_header = @parents > 1 && $action eq 'commitdiff';
2760 print "<thead><tr>\n" .
2761 "<th></th><th></th>\n"; # filename, patchN link
2762 for (my $i = 0; $i < @parents; $i++) {
2763 my $par = $parents[$i];
2765 $cgi->a({-href => href(action=>"commitdiff",
2766 hash=>$hash, hash_parent=>$par),
2767 -title => 'commitdiff to parent number ' .
2768 ($i+1) . ': ' . substr($par,0,7)},
2772 print "</tr></thead>\n<tbody>\n";
2777 foreach my $line (@{$difftree}) {
2779 if (ref($line) eq "HASH") {
2780 # pre-parsed (or generated by hand)
2783 $diff = parse_difftree_raw_line($line);
2787 print "<tr class=\"dark\">\n";
2789 print "<tr class=\"light\">\n";
2793 if (exists $diff->{'nparents'}) { # combined diff
2795 fill_from_file_info($diff, @parents)
2796 unless exists $diff->{'from_file'};
2798 if (!is_deleted($diff)) {
2799 # file exists in the result (child) commit
2801 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2802 file_name=>$diff->{'to_file'},
2804 -class => "list"}, esc_path($diff->{'to_file'})) .
2808 esc_path($diff->{'to_file'}) .
2812 if ($action eq 'commitdiff') {
2815 print "<td class=\"link\">" .
2816 $cgi->a({-href => "#patch$patchno"}, "patch") .
2821 my $has_history = 0;
2822 my $not_deleted = 0;
2823 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2824 my $hash_parent = $parents[$i];
2825 my $from_hash = $diff->{'from_id'}[$i];
2826 my $from_path = $diff->{'from_file'}[$i];
2827 my $status = $diff->{'status'}[$i];
2829 $has_history ||= ($status ne 'A');
2830 $not_deleted ||= ($status ne 'D');
2832 if ($status eq 'A') {
2833 print "<td class=\"link\" align=\"right\"> | </td>\n";
2834 } elsif ($status eq 'D') {
2835 print "<td class=\"link\">" .
2836 $cgi->a({-href => href(action=>"blob",
2839 file_name=>$from_path)},
2843 if ($diff->{'to_id'} eq $from_hash) {
2844 print "<td class=\"link nochange\">";
2846 print "<td class=\"link\">";
2848 print $cgi->a({-href => href(action=>"blobdiff",
2849 hash=>$diff->{'to_id'},
2850 hash_parent=>$from_hash,
2852 hash_parent_base=>$hash_parent,
2853 file_name=>$diff->{'to_file'},
2854 file_parent=>$from_path)},
2860 print "<td class=\"link\">";
2862 print $cgi->a({-href => href(action=>"blob",
2863 hash=>$diff->{'to_id'},
2864 file_name=>$diff->{'to_file'},
2867 print " | " if ($has_history);
2870 print $cgi->a({-href => href(action=>"history",
2871 file_name=>$diff->{'to_file'},
2878 next; # instead of 'else' clause, to avoid extra indent
2880 # else ordinary diff
2882 my ($to_mode_oct, $to_mode_str, $to_file_type);
2883 my ($from_mode_oct, $from_mode_str, $from_file_type);
2884 if ($diff->{'to_mode'} ne ('0' x 6)) {
2885 $to_mode_oct = oct $diff->{'to_mode'};
2886 if (S_ISREG($to_mode_oct)) { # only for regular file
2887 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2889 $to_file_type = file_type($diff->{'to_mode'});
2891 if ($diff->{'from_mode'} ne ('0' x 6)) {
2892 $from_mode_oct = oct $diff->{'from_mode'};
2893 if (S_ISREG($to_mode_oct)) { # only for regular file
2894 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2896 $from_file_type = file_type($diff->{'from_mode'});
2899 if ($diff->{'status'} eq "A") { # created
2900 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2901 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2902 $mode_chng .= "]</span>";
2904 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2905 hash_base=>$hash, file_name=>$diff->{'file'}),
2906 -class => "list"}, esc_path($diff->{'file'}));
2908 print "<td>$mode_chng</td>\n";
2909 print "<td class=\"link\">";
2910 if ($action eq 'commitdiff') {
2913 print $cgi->a({-href => "#patch$patchno"}, "patch");
2916 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2917 hash_base=>$hash, file_name=>$diff->{'file'})},
2921 } elsif ($diff->{'status'} eq "D") { # deleted
2922 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2924 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2925 hash_base=>$parent, file_name=>$diff->{'file'}),
2926 -class => "list"}, esc_path($diff->{'file'}));
2928 print "<td>$mode_chng</td>\n";
2929 print "<td class=\"link\">";
2930 if ($action eq 'commitdiff') {
2933 print $cgi->a({-href => "#patch$patchno"}, "patch");
2936 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2937 hash_base=>$parent, file_name=>$diff->{'file'})},
2940 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2941 file_name=>$diff->{'file'})},
2944 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2945 file_name=>$diff->{'file'})},
2949 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
2950 my $mode_chnge = "";
2951 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2952 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2953 if ($from_file_type ne $to_file_type) {
2954 $mode_chnge .= " from $from_file_type to $to_file_type";
2956 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2957 if ($from_mode_str && $to_mode_str) {
2958 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2959 } elsif ($to_mode_str) {
2960 $mode_chnge .= " mode: $to_mode_str";
2963 $mode_chnge .= "]</span>\n";
2966 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2967 hash_base=>$hash, file_name=>$diff->{'file'}),
2968 -class => "list"}, esc_path($diff->{'file'}));
2970 print "<td>$mode_chnge</td>\n";
2971 print "<td class=\"link\">";
2972 if ($action eq 'commitdiff') {
2975 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2977 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2978 # "commit" view and modified file (not onlu mode changed)
2979 print $cgi->a({-href => href(action=>"blobdiff",
2980 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2981 hash_base=>$hash, hash_parent_base=>$parent,
2982 file_name=>$diff->{'file'})},
2986 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2987 hash_base=>$hash, file_name=>$diff->{'file'})},
2990 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2991 file_name=>$diff->{'file'})},
2994 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2995 file_name=>$diff->{'file'})},
2999 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3000 my %status_name = ('R' => 'moved', 'C' => 'copied');
3001 my $nstatus = $status_name{$diff->{'status'}};
3003 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3004 # mode also for directories, so we cannot use $to_mode_str
3005 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3008 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3009 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3010 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3011 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3012 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3013 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3014 -class => "list"}, esc_path($diff->{'from_file'})) .
3015 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3016 "<td class=\"link\">";
3017 if ($action eq 'commitdiff') {
3020 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3022 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3023 # "commit" view and modified file (not only pure rename or copy)
3024 print $cgi->a({-href => href(action=>"blobdiff",
3025 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3026 hash_base=>$hash, hash_parent_base=>$parent,
3027 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3031 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3032 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3035 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3036 file_name=>$diff->{'to_file'})},
3039 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3040 file_name=>$diff->{'to_file'})},
3044 } # we should not encounter Unmerged (U) or Unknown (X) status
3047 print "</tbody>" if $has_header;
3051 sub git_patchset_body {
3052 my ($fd, $difftree, $hash, @hash_parents) = @_;
3053 my ($hash_parent) = $hash_parents[0];
3056 my $patch_number = 0;
3061 print "<div class=\"patchset\">\n";
3063 # skip to first patch
3064 while ($patch_line = <$fd>) {
3067 last if ($patch_line =~ m/^diff /);
3071 while ($patch_line) {
3073 my ($from_id, $to_id);
3076 #assert($patch_line =~ m/^diff /) if DEBUG;
3077 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3079 push @diff_header, $patch_line;
3081 # extended diff header
3083 while ($patch_line = <$fd>) {
3086 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3088 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
3091 } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
3092 $from_id = [ split(',', $1) ];
3096 push @diff_header, $patch_line;
3098 my $last_patch_line = $patch_line;
3100 # check if current patch belong to current raw line
3101 # and parse raw git-diff line if needed
3102 if (defined $diffinfo &&
3103 defined $from_id && defined $to_id &&
3104 from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
3105 $diffinfo->{'to_id'} eq $to_id) {
3106 # this is continuation of a split patch
3107 print "<div class=\"patch cont\">\n";
3109 # advance raw git-diff output if needed
3110 $patch_idx++ if defined $diffinfo;
3112 # compact combined diff output can have some patches skipped
3113 # find which patch (using pathname of result) we are at now
3115 if ($diff_header[0] =~ m!^diff --cc "?(.*)"?$!) {
3120 # read and prepare patch information
3121 if (ref($difftree->[$patch_idx]) eq "HASH") {
3122 # pre-parsed (or generated by hand)
3123 $diffinfo = $difftree->[$patch_idx];
3125 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3128 # check if current raw line has no patch (it got simplified)
3129 if (defined $to_name && $to_name ne $diffinfo->{'to_file'}) {
3130 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3131 format_diff_cc_simplified($diffinfo, @hash_parents) .
3132 "</div>\n"; # class="patch"
3137 } until (!defined $to_name || $to_name eq $diffinfo->{'to_file'} ||
3138 $patch_idx > $#$difftree);
3139 # modifies %from, %to hashes
3140 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3141 if ($diffinfo->{'nparents'}) {
3145 fill_from_file_info($diffinfo, @hash_parents)
3146 unless exists $diffinfo->{'from_file'};
3147 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3148 $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
3149 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3150 $from{'href'}[$i] = href(action=>"blob",
3151 hash_base=>$hash_parents[$i],
3152 hash=>$diffinfo->{'from_id'}[$i],
3153 file_name=>$from{'file'}[$i]);
3155 $from{'href'}[$i] = undef;
3159 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
3160 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3161 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3162 hash=>$diffinfo->{'from_id'},
3163 file_name=>$from{'file'});
3165 delete $from{'href'};
3169 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
3170 if (!is_deleted($diffinfo)) { # file exists in result
3171 $to{'href'} = href(action=>"blob", hash_base=>$hash,
3172 hash=>$diffinfo->{'to_id'},
3173 file_name=>$to{'file'});
3177 # this is first patch for raw difftree line with $patch_idx index
3178 # we index @$difftree array from 0, but number patches from 1
3179 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3182 # print "git diff" header
3183 $patch_line = shift @diff_header;
3184 print format_git_diff_header_line($patch_line, $diffinfo,
3187 # print extended diff header
3188 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
3190 foreach $patch_line (@diff_header) {
3191 print format_extended_diff_header_line($patch_line, $diffinfo,
3194 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
3196 # from-file/to-file diff header
3197 $patch_line = $last_patch_line;
3198 if (! $patch_line) {
3199 print "</div>\n"; # class="patch"
3202 next PATCH if ($patch_line =~ m/^diff /);
3203 #assert($patch_line =~ m/^---/) if DEBUG;
3204 #assert($patch_line eq $last_patch_line) if DEBUG;
3206 $patch_line = <$fd>;
3208 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3210 print format_diff_from_to_header($last_patch_line, $patch_line,
3211 $diffinfo, \%from, \%to,
3216 while ($patch_line = <$fd>) {
3219 next PATCH if ($patch_line =~ m/^diff /);
3221 print format_diff_line($patch_line, \%from, \%to);
3225 print "</div>\n"; # class="patch"
3228 # for compact combined (--cc) format, with chunk and patch simpliciaction
3229 # patchset might be empty, but there might be unprocessed raw lines
3230 for ($patch_idx++ if $patch_number > 0;
3231 $patch_idx < @$difftree;
3233 # read and prepare patch information
3234 if (ref($difftree->[$patch_idx]) eq "HASH") {
3235 # pre-parsed (or generated by hand)
3236 $diffinfo = $difftree->[$patch_idx];
3238 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3241 # generate anchor for "patch" links in difftree / whatchanged part
3242 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3243 format_diff_cc_simplified($diffinfo, @hash_parents) .
3244 "</div>\n"; # class="patch"
3249 if ($patch_number == 0) {
3250 if (@hash_parents > 1) {
3251 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3253 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3257 print "</div>\n"; # class="patchset"
3260 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3262 sub git_project_list_body {
3263 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3265 my ($check_forks) = gitweb_check_feature('forks');
3268 foreach my $pr (@$projlist) {
3269 my (@aa) = git_get_last_activity($pr->{'path'});
3273 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3274 if (!defined $pr->{'descr'}) {
3275 my $descr = git_get_project_description($pr->{'path'}) || "";
3276 $pr->{'descr_long'} = to_utf8($descr);
3277 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3279 if (!defined $pr->{'owner'}) {
3280 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3283 my $pname = $pr->{'path'};
3284 if (($pname =~ s/\.git$//) &&
3285 ($pname !~ /\/$/) &&
3286 (-d "$projectroot/$pname")) {
3287 $pr->{'forks'} = "-d $projectroot/$pname";
3293 push @projects, $pr;
3296 $order ||= $default_projects_order;
3297 $from = 0 unless defined $from;
3298 $to = $#projects if (!defined $to || $#projects < $to);
3300 print "<table class=\"project_list\">\n";
3301 unless ($no_header) {
3304 print "<th></th>\n";
3306 if ($order eq "project") {
3307 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3308 print "<th>Project</th>\n";
3311 $cgi->a({-href => href(project=>undef, order=>'project'),
3312 -class => "header"}, "Project") .
3315 if ($order eq "descr") {
3316 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3317 print "<th>Description</th>\n";
3320 $cgi->a({-href => href(project=>undef, order=>'descr'),
3321 -class => "header"}, "Description") .
3324 if ($order eq "owner") {
3325 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3326 print "<th>Owner</th>\n";
3329 $cgi->a({-href => href(project=>undef, order=>'owner'),
3330 -class => "header"}, "Owner") .
3333 if ($order eq "age") {
3334 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3335 print "<th>Last Change</th>\n";
3338 $cgi->a({-href => href(project=>undef, order=>'age'),
3339 -class => "header"}, "Last Change") .
3342 print "<th></th>\n" .
3346 for (my $i = $from; $i <= $to; $i++) {
3347 my $pr = $projects[$i];
3349 print "<tr class=\"dark\">\n";
3351 print "<tr class=\"light\">\n";
3356 if ($pr->{'forks'}) {
3357 print "<!-- $pr->{'forks'} -->\n";
3358 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3362 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3363 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3364 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3365 -class => "list", -title => $pr->{'descr_long'}},
3366 esc_html($pr->{'descr'})) . "</td>\n" .
3367 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
3368 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3369 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3370 "<td class=\"link\">" .
3371 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3372 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3373 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3374 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3375 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3379 if (defined $extra) {
3382 print "<td></td>\n";
3384 print "<td colspan=\"5\">$extra</td>\n" .
3390 sub git_shortlog_body {
3391 # uses global variable $project
3392 my ($commitlist, $from, $to, $refs, $extra) = @_;
3394 $from = 0 unless defined $from;
3395 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3397 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3399 for (my $i = $from; $i <= $to; $i++) {
3400 my %co = %{$commitlist->[$i]};
3401 my $commit = $co{'id'};
3402 my $ref = format_ref_marker($refs, $commit);
3404 print "<tr class=\"dark\">\n";
3406 print "<tr class=\"light\">\n";
3409 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3410 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3411 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3413 print format_subject_html($co{'title'}, $co{'title_short'},
3414 href(action=>"commit", hash=>$commit), $ref);
3416 "<td class=\"link\">" .
3417 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3418 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3419 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3420 my $snapshot_links = format_snapshot_links($commit);
3421 if (defined $snapshot_links) {
3422 print " | " . $snapshot_links;
3427 if (defined $extra) {
3429 "<td colspan=\"4\">$extra</td>\n" .
3435 sub git_history_body {
3436 # Warning: assumes constant type (blob or tree) during history
3437 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3439 $from = 0 unless defined $from;
3440 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3442 print "<table class=\"history\" cellspacing=\"0\">\n";
3444 for (my $i = $from; $i <= $to; $i++) {
3445 my %co = %{$commitlist->[$i]};
3449 my $commit = $co{'id'};
3451 my $ref = format_ref_marker($refs, $commit);
3454 print "<tr class=\"dark\">\n";
3456 print "<tr class=\"light\">\n";
3459 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3460 # shortlog uses chop_str($co{'author_name'}, 10)
3461 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3463 # originally git_history used chop_str($co{'title'}, 50)
3464 print format_subject_html($co{'title'}, $co{'title_short'},
3465 href(action=>"commit", hash=>$commit), $ref);
3467 "<td class=\"link\">" .
3468 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3469 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3471 if ($ftype eq 'blob') {
3472 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3473 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3474 if (defined $blob_current && defined $blob_parent &&
3475 $blob_current ne $blob_parent) {
3477 $cgi->a({-href => href(action=>"blobdiff",
3478 hash=>$blob_current, hash_parent=>$blob_parent,
3479 hash_base=>$hash_base, hash_parent_base=>$commit,
3480 file_name=>$file_name)},
3487 if (defined $extra) {
3489 "<td colspan=\"4\">$extra</td>\n" .
3496 # uses global variable $project
3497 my ($taglist, $from, $to, $extra) = @_;
3498 $from = 0 unless defined $from;
3499 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3501 print "<table class=\"tags\" cellspacing=\"0\">\n";
3503 for (my $i = $from; $i <= $to; $i++) {
3504 my $entry = $taglist->[$i];
3506 my $comment = $tag{'subject'};
3508 if (defined $comment) {
3509 $comment_short = chop_str($comment, 30, 5);
3512 print "<tr class=\"dark\">\n";
3514 print "<tr class=\"light\">\n";
3517 if (defined $tag{'age'}) {
3518 print "<td><i>$tag{'age'}</i></td>\n";
3520 print "<td></td>\n";
3523 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3524 -class => "list name"}, esc_html($tag{'name'})) .
3527 if (defined $comment) {
3528 print format_subject_html($comment, $comment_short,
3529 href(action=>"tag", hash=>$tag{'id'}));
3532 "<td class=\"selflink\">";
3533 if ($tag{'type'} eq "tag") {
3534 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3539 "<td class=\"link\">" . " | " .
3540 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3541 if ($tag{'reftype'} eq "commit") {
3542 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3543 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3544 } elsif ($tag{'reftype'} eq "blob") {
3545 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3550 if (defined $extra) {
3552 "<td colspan=\"5\">$extra</td>\n" .
3558 sub git_heads_body {
3559 # uses global variable $project
3560 my ($headlist, $head, $from, $to, $extra) = @_;
3561 $from = 0 unless defined $from;
3562 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3564 print "<table class=\"heads\" cellspacing=\"0\">\n";
3566 for (my $i = $from; $i <= $to; $i++) {
3567 my $entry = $headlist->[$i];
3569 my $curr = $ref{'id'} eq $head;
3571 print "<tr class=\"dark\">\n";
3573 print "<tr class=\"light\">\n";
3576 print "<td><i>$ref{'age'}</i></td>\n" .
3577 ($curr ? "<td class=\"current_head\">" : "<td>") .
3578 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3579 -class => "list name"},esc_html($ref{'name'})) .
3581 "<td class=\"link\">" .
3582 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3583 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3584 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3588 if (defined $extra) {
3590 "<td colspan=\"3\">$extra</td>\n" .
3596 sub git_search_grep_body {
3597 my ($commitlist, $from, $to, $extra) = @_;
3598 $from = 0 unless defined $from;
3599 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3601 print "<table class=\"grep\" cellspacing=\"0\">\n";
3603 for (my $i = $from; $i <= $to; $i++) {
3604 my %co = %{$commitlist->[$i]};
3608 my $commit = $co{'id'};
3610 print "<tr class=\"dark\">\n";
3612 print "<tr class=\"light\">\n";
3615 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3616 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3618 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3619 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3620 my $comment = $co{'comment'};
3621 foreach my $line (@$comment) {
3622 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3623 my $lead = esc_html($1) || "";
3624 $lead = chop_str($lead, 30, 10);
3625 my $match = esc_html($2) || "";
3626 my $trail = esc_html($3) || "";
3627 $trail = chop_str($trail, 30, 10);
3628 my $text = "$lead<span class=\"match\">$match</span>$trail";
3629 print chop_str($text, 80, 5) . "<br/>\n";
3633 "<td class=\"link\">" .
3634 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3636 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3640 if (defined $extra) {
3642 "<td colspan=\"3\">$extra</td>\n" .
3648 ## ======================================================================
3649 ## ======================================================================
3652 sub git_project_list {
3653 my $order = $cgi->param('o');
3654 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3655 die_error(undef, "Unknown order parameter");
3658 my @list = git_get_projects_list();
3660 die_error(undef, "No projects found");
3664 if (-f $home_text) {
3665 print "<div class=\"index_include\">\n";
3666 open (my $fd, $home_text);
3671 git_project_list_body(\@list, $order);
3676 my $order = $cgi->param('o');
3677 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3678 die_error(undef, "Unknown order parameter");
3681 my @list = git_get_projects_list($project);
3683 die_error(undef, "No forks found");
3687 git_print_page_nav('','');
3688 git_print_header_div('summary', "$project forks");
3689 git_project_list_body(\@list, $order);
3693 sub git_project_index {
3694 my @projects = git_get_projects_list($project);
3697 -type => 'text/plain',
3698 -charset => 'utf-8',
3699 -content_disposition => 'inline; filename="index.aux"');
3701 foreach my $pr (@projects) {
3702 if (!exists $pr->{'owner'}) {
3703 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
3706 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3707 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3708 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3709 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3713 print "$path $owner\n";
3718 my $descr = git_get_project_description($project) || "none";
3719 my %co = parse_commit("HEAD");
3720 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3721 my $head = $co{'id'};
3723 my $owner = git_get_project_owner($project);
3725 my $refs = git_get_references();
3726 # These get_*_list functions return one more to allow us to see if
3727 # there are more ...
3728 my @taglist = git_get_tags_list(16);
3729 my @headlist = git_get_heads_list(16);
3731 my ($check_forks) = gitweb_check_feature('forks');
3734 @forklist = git_get_projects_list($project);
3738 git_print_page_nav('summary','', $head);
3740 print "<div class=\"title\"> </div>\n";
3741 print "<table cellspacing=\"0\">\n" .
3742 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3743 "<tr><td>owner</td><td>$owner</td></tr>\n";
3744 if (defined $cd{'rfc2822'}) {
3745 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3748 # use per project git URL list in $projectroot/$project/cloneurl
3749 # or make project git URL from git base URL and project name
3750 my $url_tag = "URL";
3751 my @url_list = git_get_project_url_list($project);
3752 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3753 foreach my $git_url (@url_list) {
3754 next unless $git_url;
3755 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3760 if (-s "$projectroot/$project/README.html") {
3761 if (open my $fd, "$projectroot/$project/README.html") {
3762 print "<div class=\"title\">readme</div>\n";
3763 print $_ while (<$fd>);
3768 # we need to request one more than 16 (0..15) to check if
3770 my @commitlist = $head ? parse_commits($head, 17) : ();
3772 git_print_header_div('shortlog');
3773 git_shortlog_body(\@commitlist, 0, 15, $refs,
3774 $#commitlist <= 15 ? undef :
3775 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3779 git_print_header_div('tags');
3780 git_tags_body(\@taglist, 0, 15,
3781 $#taglist <= 15 ? undef :
3782 $cgi->a({-href => href(action=>"tags")}, "..."));
3786 git_print_header_div('heads');
3787 git_heads_body(\@headlist, $head, 0, 15,
3788 $#headlist <= 15 ? undef :
3789 $cgi->a({-href => href(action=>"heads")}, "..."));
3793 git_print_header_div('forks');
3794 git_project_list_body(\@forklist, undef, 0, 15,
3795 $#forklist <= 15 ? undef :
3796 $cgi->a({-href => href(action=>"forks")}, "..."),
3804 my $head = git_get_head_hash($project);
3806 git_print_page_nav('','', $head,undef,$head);
3807 my %tag = parse_tag($hash);
3810 die_error(undef, "Unknown tag object");
3813 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3814 print "<div class=\"title_text\">\n" .
3815 "<table cellspacing=\"0\">\n" .
3817 "<td>object</td>\n" .
3818 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3819 $tag{'object'}) . "</td>\n" .
3820 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3821 $tag{'type'}) . "</td>\n" .
3823 if (defined($tag{'author'})) {
3824 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3825 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3826 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3827 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3830 print "</table>\n\n" .
3832 print "<div class=\"page_body\">";
3833 my $comment = $tag{'comment'};
3834 foreach my $line (@$comment) {
3836 print esc_html($line, -nbsp=>1) . "<br/>\n";
3846 my ($have_blame) = gitweb_check_feature('blame');
3848 die_error('403 Permission denied', "Permission denied");
3850 die_error('404 Not Found', "File name not defined") if (!$file_name);
3851 $hash_base ||= git_get_head_hash($project);
3852 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3853 my %co = parse_commit($hash_base)
3854 or die_error(undef, "Reading commit failed");
3855 if (!defined $hash) {
3856 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3857 or die_error(undef, "Error looking up file");
3859 $ftype = git_get_type($hash);
3860 if ($ftype !~ "blob") {
3861 die_error('400 Bad Request', "Object is not a blob");
3863 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3864 $file_name, $hash_base)
3865 or die_error(undef, "Open git-blame failed");
3868 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3871 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3874 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3876 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3877 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3878 git_print_page_path($file_name, $ftype, $hash_base);
3879 my @rev_color = (qw(light2 dark2));
3880 my $num_colors = scalar(@rev_color);
3881 my $current_color = 0;
3884 <div class="page_body">
3885 <table class="blame">
3886 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3891 last unless defined $_;
3892 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3893 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3894 if (!exists $metainfo{$full_rev}) {
3895 $metainfo{$full_rev} = {};
3897 my $meta = $metainfo{$full_rev};
3900 if (/^(\S+) (.*)$/) {
3906 my $rev = substr($full_rev, 0, 8);
3907 my $author = $meta->{'author'};
3908 my %date = parse_date($meta->{'author-time'},
3909 $meta->{'author-tz'});
3910 my $date = $date{'iso-tz'};
3912 $current_color = ++$current_color % $num_colors;
3914 print "<tr class=\"$rev_color[$current_color]\">\n";
3916 print "<td class=\"sha1\"";
3917 print " title=\"". esc_html($author) . ", $date\"";
3918 print " rowspan=\"$group_size\"" if ($group_size > 1);
3920 print $cgi->a({-href => href(action=>"commit",
3922 file_name=>$file_name)},
3926 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3927 or die_error(undef, "Open git-rev-parse failed");
3928 my $parent_commit = <$dd>;
3930 chomp($parent_commit);
3931 my $blamed = href(action => 'blame',
3932 file_name => $meta->{'filename'},
3933 hash_base => $parent_commit);
3934 print "<td class=\"linenr\">";
3935 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3937 -class => "linenr" },
3940 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3946 or print "Reading blob failed\n";
3953 my ($have_blame) = gitweb_check_feature('blame');
3955 die_error('403 Permission denied', "Permission denied");
3957 die_error('404 Not Found', "File name not defined") if (!$file_name);
3958 $hash_base ||= git_get_head_hash($project);
3959 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3960 my %co = parse_commit($hash_base)
3961 or die_error(undef, "Reading commit failed");
3962 if (!defined $hash) {
3963 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3964 or die_error(undef, "Error lookup file");
3966 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3967 or die_error(undef, "Open git-annotate failed");
3970 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3973 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3976 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3978 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3979 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3980 git_print_page_path($file_name, 'blob', $hash_base);
3981 print "<div class=\"page_body\">\n";
3983 <table class="blame">
3992 my @line_class = (qw(light dark));
3993 my $line_class_len = scalar (@line_class);
3994 my $line_class_num = $#line_class;
3995 while (my $line = <$fd>) {
4007 $line_class_num = ($line_class_num + 1) % $line_class_len;
4009 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
4016 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
4019 $short_rev = substr ($long_rev, 0, 8);
4020 $age = time () - $time;
4021 $age_str = age_string ($age);
4022 $age_str =~ s/ / /g;
4023 $age_class = age_class($age);
4024 $author = esc_html ($author);
4025 $author =~ s/ / /g;
4027 $data = untabify($data);
4028 $data = esc_html ($data);
4031 <tr class="$line_class[$line_class_num]">
4032 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
4033 <td class="$age_class">$age_str</td>
4035 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4036 <td class="pre">$data</td>
4039 } # while (my $line = <$fd>)
4040 print "</table>\n\n";
4042 or print "Reading blob failed.\n";
4048 my $head = git_get_head_hash($project);
4050 git_print_page_nav('','', $head,undef,$head);
4051 git_print_header_div('summary', $project);
4053 my @tagslist = git_get_tags_list();
4055 git_tags_body(\@tagslist);
4061 my $head = git_get_head_hash($project);
4063 git_print_page_nav('','', $head,undef,$head);
4064 git_print_header_div('summary', $project);
4066 my @headslist = git_get_heads_list();
4068 git_heads_body(\@headslist, $head);
4073 sub git_blob_plain {
4076 if (!defined $hash) {
4077 if (defined $file_name) {
4078 my $base = $hash_base || git_get_head_hash($project);
4079 $hash = git_get_hash_by_path($base, $file_name, "blob")
4080 or die_error(undef, "Error lookup file");
4082 die_error(undef, "No file name defined");
4084 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4085 # blobs defined by non-textual hash id's can be cached
4090 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4091 or die_error(undef, "Couldn't cat $file_name, $hash");
4093 $type ||= blob_mimetype($fd, $file_name);
4095 # save as filename, even when no $file_name is given
4096 my $save_as = "$hash";
4097 if (defined $file_name) {
4098 $save_as = $file_name;
4099 } elsif ($type =~ m/^text\//) {
4106 -content_disposition => 'inline; filename="' . "$save_as" . '"');
4108 binmode STDOUT, ':raw';
4110 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4118 if (!defined $hash) {
4119 if (defined $file_name) {
4120 my $base = $hash_base || git_get_head_hash($project);
4121 $hash = git_get_hash_by_path($base, $file_name, "blob")
4122 or die_error(undef, "Error lookup file");
4124 die_error(undef, "No file name defined");
4126 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4127 # blobs defined by non-textual hash id's can be cached
4131 my ($have_blame) = gitweb_check_feature('blame');
4132 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4133 or die_error(undef, "Couldn't cat $file_name, $hash");
4134 my $mimetype = blob_mimetype($fd, $file_name);
4135 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
4137 return git_blob_plain($mimetype);
4139 # we can have blame only for text/* mimetype
4140 $have_blame &&= ($mimetype =~ m!^text/!);
4142 git_header_html(undef, $expires);
4143 my $formats_nav = '';
4144 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4145 if (defined $file_name) {
4148 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
4149 hash=>$hash, file_name=>$file_name)},
4154 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4155 hash=>$hash, file_name=>$file_name)},
4158 $cgi->a({-href => href(action=>"blob_plain",
4159 hash=>$hash, file_name=>$file_name)},
4162 $cgi->a({-href => href(action=>"blob",
4163 hash_base=>"HEAD", file_name=>$file_name)},
4167 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
4169 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4170 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4172 print "<div class=\"page_nav\">\n" .
4173 "<br/><br/></div>\n" .
4174 "<div class=\"title\">$hash</div>\n";
4176 git_print_page_path($file_name, "blob", $hash_base);
4177 print "<div class=\"page_body\">\n";
4178 if ($mimetype =~ m!^text/!) {
4180 while (my $line = <$fd>) {
4183 $line = untabify($line);
4184 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4185 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4187 } elsif ($mimetype =~ m!^image/!) {
4188 print qq!<img type="$mimetype"!;
4190 print qq! alt="$file_name" title="$file_name"!;
4193 href(action=>"blob_plain", hash=>$hash,
4194 hash_base=>$hash_base, file_name=>$file_name) .
4198 or print "Reading blob failed.\n";
4204 if (!defined $hash_base) {
4205 $hash_base = "HEAD";
4207 if (!defined $hash) {
4208 if (defined $file_name) {
4209 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4215 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4216 or die_error(undef, "Open git-ls-tree failed");
4217 my @entries = map { chomp; $_ } <$fd>;
4218 close $fd or die_error(undef, "Reading tree failed");
4221 my $refs = git_get_references();
4222 my $ref = format_ref_marker($refs, $hash_base);
4225 my ($have_blame) = gitweb_check_feature('blame');
4226 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4228 if (defined $file_name) {
4230 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4231 hash=>$hash, file_name=>$file_name)},
4233 $cgi->a({-href => href(action=>"tree",
4234 hash_base=>"HEAD", file_name=>$file_name)},
4237 my $snapshot_links = format_snapshot_links($hash);
4238 if (defined $snapshot_links) {
4239 # FIXME: Should be available when we have no hash base as well.
4240 push @views_nav, $snapshot_links;
4242 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4243 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4246 print "<div class=\"page_nav\">\n";
4247 print "<br/><br/></div>\n";
4248 print "<div class=\"title\">$hash</div>\n";
4250 if (defined $file_name) {
4251 $basedir = $file_name;
4252 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4256 git_print_page_path($file_name, 'tree', $hash_base);
4257 print "<div class=\"page_body\">\n";
4258 print "<table cellspacing=\"0\">\n";
4260 # '..' (top directory) link if possible
4261 if (defined $hash_base &&
4262 defined $file_name && $file_name =~ m![^/]+$!) {
4264 print "<tr class=\"dark\">\n";
4266 print "<tr class=\"light\">\n";
4270 my $up = $file_name;
4271 $up =~ s!/?[^/]+$!!;
4272 undef $up unless $up;
4273 # based on git_print_tree_entry
4274 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4275 print '<td class="list">';
4276 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4280 print "<td class=\"link\"></td>\n";
4284 foreach my $line (@entries) {
4285 my %t = parse_ls_tree_line($line, -z => 1);
4288 print "<tr class=\"dark\">\n";
4290 print "<tr class=\"light\">\n";
4294 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4298 print "</table>\n" .
4304 my @supported_fmts = gitweb_check_feature('snapshot');
4306 my $format = $cgi->param('sf');
4307 unless ($format =~ m/[a-z0-9]+/
4308 && exists($known_snapshot_formats{$format})
4309 && grep($_ eq $format, @supported_fmts)) {
4310 die_error(undef, "Unsupported snapshot format");
4313 if (!defined $hash) {
4314 $hash = git_get_head_hash($project);
4317 my $git_command = git_cmd_str();
4318 my $name = $project;
4319 $name =~ s,([^/])/*\.git$,$1,;
4320 $name = basename($name);
4321 my $filename = to_utf8($name);
4322 $name =~ s/\047/\047\\\047\047/g;
4324 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4325 $cmd = "$git_command archive " .
4326 "--format=$known_snapshot_formats{$format}{'format'}" .
4327 "--prefix=\'$name\'/ $hash";
4328 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4329 $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
4333 -type => $known_snapshot_formats{$format}{'type'},
4334 -content_disposition => 'inline; filename="' . "$filename" . '"',
4335 -status => '200 OK');
4337 open my $fd, "-|", $cmd
4338 or die_error(undef, "Execute git-archive failed");
4339 binmode STDOUT, ':raw';
4341 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4346 my $head = git_get_head_hash($project);
4347 if (!defined $hash) {
4350 if (!defined $page) {
4353 my $refs = git_get_references();
4355 my @commitlist = parse_commits($hash, 101, (100 * $page));
4357 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
4360 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4363 my %co = parse_commit($hash);
4365 git_print_header_div('summary', $project);
4366 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4368 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4369 for (my $i = 0; $i <= $to; $i++) {
4370 my %co = %{$commitlist[$i]};
4372 my $commit = $co{'id'};
4373 my $ref = format_ref_marker($refs, $commit);
4374 my %ad = parse_date($co{'author_epoch'});
4375 git_print_header_div('commit',
4376 "<span class=\"age\">$co{'age_string'}</span>" .
4377 esc_html($co{'title'}) . $ref,
4379 print "<div class=\"title_text\">\n" .
4380 "<div class=\"log_link\">\n" .
4381 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4383 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4385 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4388 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4391 print "<div class=\"log_body\">\n";
4392 git_print_log($co{'comment'}, -final_empty_line=> 1);
4395 if ($#commitlist >= 100) {
4396 print "<div class=\"page_nav\">\n";
4397 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4398 -accesskey => "n", -title => "Alt-n"}, "next");
4405 $hash ||= $hash_base || "HEAD";
4406 my %co = parse_commit($hash);
4408 die_error(undef, "Unknown commit object");
4410 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4411 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4413 my $parent = $co{'parent'};
4414 my $parents = $co{'parents'}; # listref
4416 # we need to prepare $formats_nav before any parameter munging
4418 if (!defined $parent) {
4420 $formats_nav .= '(initial)';
4421 } elsif (@$parents == 1) {
4422 # single parent commit
4425 $cgi->a({-href => href(action=>"commit",
4427 esc_html(substr($parent, 0, 7))) .
4434 $cgi->a({-href => href(action=>"commit",
4436 esc_html(substr($_, 0, 7)));
4441 if (!defined $parent) {
4445 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4447 (@$parents <= 1 ? $parent : '-c'),
4449 or die_error(undef, "Open git-diff-tree failed");
4450 @difftree = map { chomp; $_ } <$fd>;
4451 close $fd or die_error(undef, "Reading git-diff-tree failed");
4453 # non-textual hash id's can be cached
4455 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4458 my $refs = git_get_references();
4459 my $ref = format_ref_marker($refs, $co{'id'});
4461 git_header_html(undef, $expires);
4462 git_print_page_nav('commit', '',
4463 $hash, $co{'tree'}, $hash,
4466 if (defined $co{'parent'}) {
4467 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4469 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4471 print "<div class=\"title_text\">\n" .
4472 "<table cellspacing=\"0\">\n";
4473 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4475 "<td></td><td> $ad{'rfc2822'}";
4476 if ($ad{'hour_local'} < 6) {
4477 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4478 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4480 printf(" (%02d:%02d %s)",
4481 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4485 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4486 print "<tr><td></td><td> $cd{'rfc2822'}" .
4487 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4489 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4492 "<td class=\"sha1\">" .
4493 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4494 class => "list"}, $co{'tree'}) .
4496 "<td class=\"link\">" .
4497 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4499 my $snapshot_links = format_snapshot_links($hash);
4500 if (defined $snapshot_links) {
4501 print " | " . $snapshot_links;
4506 foreach my $par (@$parents) {
4509 "<td class=\"sha1\">" .
4510 $cgi->a({-href => href(action=>"commit", hash=>$par),
4511 class => "list"}, $par) .
4513 "<td class=\"link\">" .
4514 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4516 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4523 print "<div class=\"page_body\">\n";
4524 git_print_log($co{'comment'});
4527 git_difftree_body(\@difftree, $hash, @$parents);
4533 # object is defined by:
4534 # - hash or hash_base alone
4535 # - hash_base and file_name
4538 # - hash or hash_base alone
4539 if ($hash || ($hash_base && !defined $file_name)) {
4540 my $object_id = $hash || $hash_base;
4542 my $git_command = git_cmd_str();
4543 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4544 or die_error('404 Not Found', "Object does not exist");
4548 or die_error('404 Not Found', "Object does not exist");
4550 # - hash_base and file_name
4551 } elsif ($hash_base && defined $file_name) {
4552 $file_name =~ s,/+$,,;
4554 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4555 or die_error('404 Not Found', "Base object does not exist");
4557 # here errors should not hapen
4558 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4559 or die_error(undef, "Open git-ls-tree failed");
4563 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4564 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4565 die_error('404 Not Found', "File or directory for given base does not exist");
4570 die_error('404 Not Found', "Not enough information to find object");
4573 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4574 hash=>$hash, hash_base=>$hash_base,
4575 file_name=>$file_name),
4576 -status => '302 Found');
4580 my $format = shift || 'html';
4587 # preparing $fd and %diffinfo for git_patchset_body
4589 if (defined $hash_base && defined $hash_parent_base) {
4590 if (defined $file_name) {
4592 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4593 $hash_parent_base, $hash_base,
4594 "--", (defined $file_parent ? $file_parent : ()), $file_name
4595 or die_error(undef, "Open git-diff-tree failed");
4596 @difftree = map { chomp; $_ } <$fd>;
4598 or die_error(undef, "Reading git-diff-tree failed");
4600 or die_error('404 Not Found', "Blob diff not found");
4602 } elsif (defined $hash &&
4603 $hash =~ /[0-9a-fA-F]{40}/) {
4604 # try to find filename from $hash
4606 # read filtered raw output
4607 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4608 $hash_parent_base, $hash_base, "--"
4609 or die_error(undef, "Open git-diff-tree failed");
4611 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4613 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4614 map { chomp; $_ } <$fd>;
4616 or die_error(undef, "Reading git-diff-tree failed");
4618 or die_error('404 Not Found', "Blob diff not found");
4621 die_error('404 Not Found', "Missing one of the blob diff parameters");
4624 if (@difftree > 1) {
4625 die_error('404 Not Found', "Ambiguous blob diff specification");
4628 %diffinfo = parse_difftree_raw_line($difftree[0]);
4629 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4630 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4632 $hash_parent ||= $diffinfo{'from_id'};
4633 $hash ||= $diffinfo{'to_id'};
4635 # non-textual hash id's can be cached
4636 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4637 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4642 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4643 '-p', ($format eq 'html' ? "--full-index" : ()),
4644 $hash_parent_base, $hash_base,
4645 "--", (defined $file_parent ? $file_parent : ()), $file_name
4646 or die_error(undef, "Open git-diff-tree failed");
4649 # old/legacy style URI
4650 if (!%diffinfo && # if new style URI failed
4651 defined $hash && defined $hash_parent) {
4652 # fake git-diff-tree raw output
4653 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4654 $diffinfo{'from_id'} = $hash_parent;
4655 $diffinfo{'to_id'} = $hash;
4656 if (defined $file_name) {
4657 if (defined $file_parent) {
4658 $diffinfo{'status'} = '2';
4659 $diffinfo{'from_file'} = $file_parent;
4660 $diffinfo{'to_file'} = $file_name;
4661 } else { # assume not renamed
4662 $diffinfo{'status'} = '1';
4663 $diffinfo{'from_file'} = $file_name;
4664 $diffinfo{'to_file'} = $file_name;
4666 } else { # no filename given
4667 $diffinfo{'status'} = '2';
4668 $diffinfo{'from_file'} = $hash_parent;
4669 $diffinfo{'to_file'} = $hash;
4672 # non-textual hash id's can be cached
4673 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4674 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4679 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4680 '-p', ($format eq 'html' ? "--full-index" : ()),
4681 $hash_parent, $hash, "--"
4682 or die_error(undef, "Open git-diff failed");
4684 die_error('404 Not Found', "Missing one of the blob diff parameters")
4689 if ($format eq 'html') {
4691 $cgi->a({-href => href(action=>"blobdiff_plain",
4692 hash=>$hash, hash_parent=>$hash_parent,
4693 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4694 file_name=>$file_name, file_parent=>$file_parent)},
4696 git_header_html(undef, $expires);
4697 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4698 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4699 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4701 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4702 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4704 if (defined $file_name) {
4705 git_print_page_path($file_name, "blob", $hash_base);
4707 print "<div class=\"page_path\"></div>\n";
4710 } elsif ($format eq 'plain') {
4712 -type => 'text/plain',
4713 -charset => 'utf-8',
4714 -expires => $expires,
4715 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4717 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4720 die_error(undef, "Unknown blobdiff format");
4724 if ($format eq 'html') {
4725 print "<div class=\"page_body\">\n";
4727 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4730 print "</div>\n"; # class="page_body"
4734 while (my $line = <$fd>) {
4735 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4736 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4740 last if $line =~ m!^\+\+\+!;
4748 sub git_blobdiff_plain {
4749 git_blobdiff('plain');
4752 sub git_commitdiff {
4753 my $format = shift || 'html';
4754 $hash ||= $hash_base || "HEAD";
4755 my %co = parse_commit($hash);
4757 die_error(undef, "Unknown commit object");
4760 # choose format for commitdiff for merge
4761 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4762 $hash_parent = '--cc';
4764 # we need to prepare $formats_nav before almost any parameter munging
4766 if ($format eq 'html') {
4768 $cgi->a({-href => href(action=>"commitdiff_plain",
4769 hash=>$hash, hash_parent=>$hash_parent)},
4772 if (defined $hash_parent &&
4773 $hash_parent ne '-c' && $hash_parent ne '--cc') {
4774 # commitdiff with two commits given
4775 my $hash_parent_short = $hash_parent;
4776 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4777 $hash_parent_short = substr($hash_parent, 0, 7);
4781 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4782 if ($co{'parents'}[$i] eq $hash_parent) {
4783 $formats_nav .= ' parent ' . ($i+1);
4787 $formats_nav .= ': ' .
4788 $cgi->a({-href => href(action=>"commitdiff",
4789 hash=>$hash_parent)},
4790 esc_html($hash_parent_short)) .
4792 } elsif (!$co{'parent'}) {
4794 $formats_nav .= ' (initial)';
4795 } elsif (scalar @{$co{'parents'}} == 1) {
4796 # single parent commit
4799 $cgi->a({-href => href(action=>"commitdiff",
4800 hash=>$co{'parent'})},
4801 esc_html(substr($co{'parent'}, 0, 7))) .
4805 if ($hash_parent eq '--cc') {
4806 $formats_nav .= ' | ' .
4807 $cgi->a({-href => href(action=>"commitdiff",
4808 hash=>$hash, hash_parent=>'-c')},
4810 } else { # $hash_parent eq '-c'
4811 $formats_nav .= ' | ' .
4812 $cgi->a({-href => href(action=>"commitdiff",
4813 hash=>$hash, hash_parent=>'--cc')},
4819 $cgi->a({-href => href(action=>"commitdiff",
4821 esc_html(substr($_, 0, 7)));
4822 } @{$co{'parents'}} ) .
4827 my $hash_parent_param = $hash_parent;
4828 if (!defined $hash_parent_param) {
4829 # --cc for multiple parents, --root for parentless
4830 $hash_parent_param =
4831 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
4837 if ($format eq 'html') {
4838 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4839 "--no-commit-id", "--patch-with-raw", "--full-index",
4840 $hash_parent_param, $hash, "--"
4841 or die_error(undef, "Open git-diff-tree failed");
4843 while (my $line = <$fd>) {
4845 # empty line ends raw part of diff-tree output
4847 push @difftree, scalar parse_difftree_raw_line($line);
4850 } elsif ($format eq 'plain') {
4851 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4852 '-p', $hash_parent_param, $hash, "--"
4853 or die_error(undef, "Open git-diff-tree failed");
4856 die_error(undef, "Unknown commitdiff format");
4859 # non-textual hash id's can be cached
4861 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4865 # write commit message
4866 if ($format eq 'html') {
4867 my $refs = git_get_references();
4868 my $ref = format_ref_marker($refs, $co{'id'});
4870 git_header_html(undef, $expires);
4871 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4872 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4873 git_print_authorship(\%co);
4874 print "<div class=\"page_body\">\n";
4875 if (@{$co{'comment'}} > 1) {
4876 print "<div class=\"log\">\n";
4877 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4878 print "</div>\n"; # class="log"
4881 } elsif ($format eq 'plain') {
4882 my $refs = git_get_references("tags");
4883 my $tagname = git_get_rev_name_tags($hash);
4884 my $filename = basename($project) . "-$hash.patch";
4887 -type => 'text/plain',
4888 -charset => 'utf-8',
4889 -expires => $expires,
4890 -content_disposition => 'inline; filename="' . "$filename" . '"');
4891 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4894 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4895 Subject: $co{'title'}
4897 print "X-Git-Tag: $tagname\n" if $tagname;
4898 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4900 foreach my $line (@{$co{'comment'}}) {
4907 if ($format eq 'html') {
4908 my $use_parents = !defined $hash_parent ||
4909 $hash_parent eq '-c' || $hash_parent eq '--cc';
4910 git_difftree_body(\@difftree, $hash,
4911 $use_parents ? @{$co{'parents'}} : $hash_parent);
4914 git_patchset_body($fd, \@difftree, $hash,
4915 $use_parents ? @{$co{'parents'}} : $hash_parent);
4917 print "</div>\n"; # class="page_body"
4920 } elsif ($format eq 'plain') {
4924 or print "Reading git-diff-tree failed\n";
4928 sub git_commitdiff_plain {
4929 git_commitdiff('plain');
4933 if (!defined $hash_base) {
4934 $hash_base = git_get_head_hash($project);
4936 if (!defined $page) {
4940 my %co = parse_commit($hash_base);
4942 die_error(undef, "Unknown commit object");
4945 my $refs = git_get_references();
4946 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4948 if (!defined $hash && defined $file_name) {
4949 $hash = git_get_hash_by_path($hash_base, $file_name);
4951 if (defined $hash) {
4952 $ftype = git_get_type($hash);
4955 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4957 my $paging_nav = '';
4960 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4961 file_name=>$file_name)},
4963 $paging_nav .= " ⋅ " .
4964 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4965 file_name=>$file_name, page=>$page-1),
4966 -accesskey => "p", -title => "Alt-p"}, "prev");
4968 $paging_nav .= "first";
4969 $paging_nav .= " ⋅ prev";
4971 if ($#commitlist >= 100) {
4972 $paging_nav .= " ⋅ " .
4973 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4974 file_name=>$file_name, page=>$page+1),
4975 -accesskey => "n", -title => "Alt-n"}, "next");
4977 $paging_nav .= " ⋅ next";
4980 if ($#commitlist >= 100) {
4982 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4983 file_name=>$file_name, page=>$page+1),
4984 -accesskey => "n", -title => "Alt-n"}, "next");
4988 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4989 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4990 git_print_page_path($file_name, $ftype, $hash_base);
4992 git_history_body(\@commitlist, 0, 99,
4993 $refs, $hash_base, $ftype, $next_link);
4999 my ($have_search) = gitweb_check_feature('search');
5000 if (!$have_search) {
5001 die_error('403 Permission denied', "Permission denied");
5003 if (!defined $searchtext) {
5004 die_error(undef, "Text field empty");
5006 if (!defined $hash) {
5007 $hash = git_get_head_hash($project);
5009 my %co = parse_commit($hash);
5011 die_error(undef, "Unknown commit object");
5013 if (!defined $page) {
5017 $searchtype ||= 'commit';
5018 if ($searchtype eq 'pickaxe') {
5019 # pickaxe may take all resources of your box and run for several minutes
5020 # with every query - so decide by yourself how public you make this feature
5021 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5022 if (!$have_pickaxe) {
5023 die_error('403 Permission denied', "Permission denied");
5026 if ($searchtype eq 'grep') {
5027 my ($have_grep) = gitweb_check_feature('grep');
5029 die_error('403 Permission denied', "Permission denied");
5035 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5037 if ($searchtype eq 'commit') {
5038 $greptype = "--grep=";
5039 } elsif ($searchtype eq 'author') {
5040 $greptype = "--author=";
5041 } elsif ($searchtype eq 'committer') {
5042 $greptype = "--committer=";
5044 $greptype .= $search_regexp;
5045 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
5047 my $paging_nav = '';
5050 $cgi->a({-href => href(action=>"search", hash=>$hash,
5051 searchtext=>$searchtext, searchtype=>$searchtype)},
5053 $paging_nav .= " ⋅ " .
5054 $cgi->a({-href => href(action=>"search", hash=>$hash,
5055 searchtext=>$searchtext, searchtype=>$searchtype,
5057 -accesskey => "p", -title => "Alt-p"}, "prev");
5059 $paging_nav .= "first";
5060 $paging_nav .= " ⋅ prev";
5062 if ($#commitlist >= 100) {
5063 $paging_nav .= " ⋅ " .
5064 $cgi->a({-href => href(action=>"search", hash=>$hash,
5065 searchtext=>$searchtext, searchtype=>$searchtype,
5067 -accesskey => "n", -title => "Alt-n"}, "next");
5069 $paging_nav .= " ⋅ next";
5072 if ($#commitlist >= 100) {
5074 $cgi->a({-href => href(action=>"search", hash=>$hash,
5075 searchtext=>$searchtext, searchtype=>$searchtype,
5077 -accesskey => "n", -title => "Alt-n"}, "next");
5080 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5081 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5082 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5085 if ($searchtype eq 'pickaxe') {
5086 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5087 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5089 print "<table cellspacing=\"0\">\n";
5092 my $git_command = git_cmd_str();
5093 my $searchqtext = $searchtext;
5094 $searchqtext =~ s/'/'\\''/;
5095 open my $fd, "-|", "$git_command rev-list $hash | " .
5096 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
5099 while (my $line = <$fd>) {
5100 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
5103 $set{'from_id'} = $3;
5105 $set{'id'} = $set{'to_id'};
5106 if ($set{'id'} =~ m/0{40}/) {
5107 $set{'id'} = $set{'from_id'};
5109 if ($set{'id'} =~ m/0{40}/) {
5113 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
5116 print "<tr class=\"dark\">\n";
5118 print "<tr class=\"light\">\n";
5121 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5122 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
5124 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5125 -class => "list subject"},
5126 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
5127 while (my $setref = shift @files) {
5129 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5130 hash=>$set{'id'}, file_name=>$set{'file'}),
5132 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5136 "<td class=\"link\">" .
5137 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5139 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5143 %co = parse_commit($1);
5151 if ($searchtype eq 'grep') {
5152 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5153 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5155 print "<table cellspacing=\"0\">\n";
5159 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
5161 while (my $line = <$fd>) {
5163 my ($file, $lno, $ltext, $binary);
5164 last if ($matches++ > 1000);
5165 if ($line =~ /^Binary file (.+) matches$/) {
5169 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5171 if ($file ne $lastfile) {
5172 $lastfile and print "</td></tr>\n";
5174 print "<tr class=\"dark\">\n";
5176 print "<tr class=\"light\">\n";
5178 print "<td class=\"list\">".
5179 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5180 file_name=>"$file"),
5181 -class => "list"}, esc_path($file));
5182 print "</td><td>\n";
5186 print "<div class=\"binary\">Binary file</div>\n";
5188 $ltext = untabify($ltext);
5189 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
5190 $ltext = esc_html($1, -nbsp=>1);
5191 $ltext .= '<span class="match">';
5192 $ltext .= esc_html($2, -nbsp=>1);
5193 $ltext .= '</span>';
5194 $ltext .= esc_html($3, -nbsp=>1);
5196 $ltext = esc_html($ltext, -nbsp=>1);
5198 print "<div class=\"pre\">" .
5199 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5200 file_name=>"$file").'#l'.$lno,
5201 -class => "linenr"}, sprintf('%4i', $lno))
5202 . ' ' . $ltext . "</div>\n";
5206 print "</td></tr>\n";
5207 if ($matches > 1000) {
5208 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5211 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5220 sub git_search_help {
5222 git_print_page_nav('','', $hash,$hash,$hash);
5225 <dt><b>commit</b></dt>
5226 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
5228 my ($have_grep) = gitweb_check_feature('grep');
5231 <dt><b>grep</b></dt>
5232 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5233 a different one) are searched for the given
5234 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5235 (POSIX extended) and the matches are listed. On large
5236 trees, this search can take a while and put some strain on the server, so please use it with
5237 some consideration.</dd>
5241 <dt><b>author</b></dt>
5242 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5243 <dt><b>committer</b></dt>
5244 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5246 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5247 if ($have_pickaxe) {
5249 <dt><b>pickaxe</b></dt>
5250 <dd>All commits that caused the string to appear or disappear from any file (changes that
5251 added, removed or "modified" the string) will be listed. This search can take a while and
5252 takes a lot of strain on the server, so please use it wisely.</dd>
5260 my $head = git_get_head_hash($project);
5261 if (!defined $hash) {
5264 if (!defined $page) {
5267 my $refs = git_get_references();
5269 my @commitlist = parse_commits($hash, 101, (100 * $page));
5271 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
5273 if ($#commitlist >= 100) {
5275 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
5276 -accesskey => "n", -title => "Alt-n"}, "next");
5280 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5281 git_print_header_div('summary', $project);
5283 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5288 ## ......................................................................
5289 ## feeds (RSS, Atom; OPML)
5292 my $format = shift || 'atom';
5293 my ($have_blame) = gitweb_check_feature('blame');
5295 # Atom: http://www.atomenabled.org/developers/syndication/
5296 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5297 if ($format ne 'rss' && $format ne 'atom') {
5298 die_error(undef, "Unknown web feed format");
5301 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5302 my $head = $hash || 'HEAD';
5303 my @commitlist = parse_commits($head, 150);
5307 my $content_type = "application/$format+xml";
5308 if (defined $cgi->http('HTTP_ACCEPT') &&
5309 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5310 # browser (feed reader) prefers text/xml
5311 $content_type = 'text/xml';
5313 if (defined($commitlist[0])) {
5314 %latest_commit = %{$commitlist[0]};
5315 %latest_date = parse_date($latest_commit{'author_epoch'});
5317 -type => $content_type,
5318 -charset => 'utf-8',
5319 -last_modified => $latest_date{'rfc2822'});
5322 -type => $content_type,
5323 -charset => 'utf-8');
5326 # Optimization: skip generating the body if client asks only
5327 # for Last-Modified date.
5328 return if ($cgi->request_method() eq 'HEAD');
5331 my $title = "$site_name - $project/$action";
5332 my $feed_type = 'log';
5333 if (defined $hash) {
5334 $title .= " - '$hash'";
5335 $feed_type = 'branch log';
5336 if (defined $file_name) {
5337 $title .= " :: $file_name";
5338 $feed_type = 'history';
5340 } elsif (defined $file_name) {
5341 $title .= " - $file_name";
5342 $feed_type = 'history';
5344 $title .= " $feed_type";
5345 my $descr = git_get_project_description($project);
5346 if (defined $descr) {
5347 $descr = esc_html($descr);
5349 $descr = "$project " .
5350 ($format eq 'rss' ? 'RSS' : 'Atom') .
5353 my $owner = git_get_project_owner($project);
5354 $owner = esc_html($owner);
5358 if (defined $file_name) {
5359 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5360 } elsif (defined $hash) {
5361 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5363 $alt_url = href(-full=>1, action=>"summary");
5365 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5366 if ($format eq 'rss') {
5368 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5371 print "<title>$title</title>\n" .
5372 "<link>$alt_url</link>\n" .
5373 "<description>$descr</description>\n" .
5374 "<language>en</language>\n";
5375 } elsif ($format eq 'atom') {
5377 <feed xmlns="http://www.w3.org/2005/Atom">
5379 print "<title>$title</title>\n" .
5380 "<subtitle>$descr</subtitle>\n" .
5381 '<link rel="alternate" type="text/html" href="' .
5382 $alt_url . '" />' . "\n" .
5383 '<link rel="self" type="' . $content_type . '" href="' .
5384 $cgi->self_url() . '" />' . "\n" .
5385 "<id>" . href(-full=>1) . "</id>\n" .
5386 # use project owner for feed author
5387 "<author><name>$owner</name></author>\n";
5388 if (defined $favicon) {
5389 print "<icon>" . esc_url($favicon) . "</icon>\n";
5391 if (defined $logo_url) {
5392 # not twice as wide as tall: 72 x 27 pixels
5393 print "<logo>" . esc_url($logo) . "</logo>\n";
5395 if (! %latest_date) {
5396 # dummy date to keep the feed valid until commits trickle in:
5397 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5399 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5404 for (my $i = 0; $i <= $#commitlist; $i++) {
5405 my %co = %{$commitlist[$i]};
5406 my $commit = $co{'id'};
5407 # we read 150, we always show 30 and the ones more recent than 48 hours
5408 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5411 my %cd = parse_date($co{'author_epoch'});
5413 # get list of changed files
5414 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5415 $co{'parent'} || "--root",
5416 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5418 my @difftree = map { chomp; $_ } <$fd>;
5422 # print element (entry, item)
5423 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5424 if ($format eq 'rss') {
5426 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5427 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5428 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5429 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5430 "<link>$co_url</link>\n" .
5431 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5432 "<content:encoded>" .
5434 } elsif ($format eq 'atom') {
5436 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5437 "<updated>$cd{'iso-8601'}</updated>\n" .
5439 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5440 if ($co{'author_email'}) {
5441 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5443 print "</author>\n" .
5444 # use committer for contributor
5446 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5447 if ($co{'committer_email'}) {
5448 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5450 print "</contributor>\n" .
5451 "<published>$cd{'iso-8601'}</published>\n" .
5452 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5453 "<id>$co_url</id>\n" .
5454 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5455 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5457 my $comment = $co{'comment'};
5459 foreach my $line (@$comment) {
5460 $line = esc_html($line);
5463 print "</pre><ul>\n";
5464 foreach my $difftree_line (@difftree) {
5465 my %difftree = parse_difftree_raw_line($difftree_line);
5466 next if !$difftree{'from_id'};
5468 my $file = $difftree{'file'} || $difftree{'to_file'};
5472 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5473 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5474 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5475 file_name=>$file, file_parent=>$difftree{'from_file'}),
5476 -title => "diff"}, 'D');
5478 print $cgi->a({-href => href(-full=>1, action=>"blame",
5479 file_name=>$file, hash_base=>$commit),
5480 -title => "blame"}, 'B');
5482 # if this is not a feed of a file history
5483 if (!defined $file_name || $file_name ne $file) {
5484 print $cgi->a({-href => href(-full=>1, action=>"history",
5485 file_name=>$file, hash=>$commit),
5486 -title => "history"}, 'H');
5488 $file = esc_path($file);
5492 if ($format eq 'rss') {
5493 print "</ul>]]>\n" .
5494 "</content:encoded>\n" .
5496 } elsif ($format eq 'atom') {
5497 print "</ul>\n</div>\n" .
5504 if ($format eq 'rss') {
5505 print "</channel>\n</rss>\n";
5506 } elsif ($format eq 'atom') {
5520 my @list = git_get_projects_list();
5522 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5524 <?xml version="1.0" encoding="utf-8"?>
5525 <opml version="1.0">
5527 <title>$site_name OPML Export</title>
5530 <outline text="git RSS feeds">
5533 foreach my $pr (@list) {
5535 my $head = git_get_head_hash($proj{'path'});
5536 if (!defined $head) {
5539 $git_dir = "$projectroot/$proj{'path'}";
5540 my %co = parse_commit($head);
5545 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5546 my $rss = "$my_url?p=$proj{'path'};a=rss";
5547 my $html = "$my_url?p=$proj{'path'};a=summary";
5548 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";