gitweb: Add links to blobdiffs in from-file/to-file header for merges
[debian-git.git] / gitweb / gitweb.perl
blobc7acfade608ce3b6e49c9d061896dcb4bd96c2e0
1 #!/usr/bin/perl
3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
21 BEGIN {
22 CGI->compile() if $ENV{'MOD_PERL'};
25 our $cgi = new CGI;
26 our $version = "++GIT_VERSION++";
27 our $my_url = $cgi->url();
28 our $my_uri = $cgi->url(-absolute => 1);
30 # core git executable to use
31 # this can just be "git" if your webserver has a sensible PATH
32 our $GIT = "++GIT_BINDIR++/git";
34 # absolute fs-path which will be prepended to the project path
35 #our $projectroot = "/pub/scm";
36 our $projectroot = "++GITWEB_PROJECTROOT++";
38 # target of the home link on top of all pages
39 our $home_link = $my_uri || "/";
41 # string of the home link on top of all pages
42 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
44 # name of your site or organization to appear in page titles
45 # replace this with something more descriptive for clearer bookmarks
46 our $site_name = "++GITWEB_SITENAME++"
47 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
49 # filename of html text to include at top of each page
50 our $site_header = "++GITWEB_SITE_HEADER++";
51 # html text to include at home page
52 our $home_text = "++GITWEB_HOMETEXT++";
53 # filename of html text to include at bottom of each page
54 our $site_footer = "++GITWEB_SITE_FOOTER++";
56 # URI of stylesheets
57 our @stylesheets = ("++GITWEB_CSS++");
58 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
59 our $stylesheet = undef;
60 # URI of GIT logo (72x27 size)
61 our $logo = "++GITWEB_LOGO++";
62 # URI of GIT favicon, assumed to be image/png type
63 our $favicon = "++GITWEB_FAVICON++";
65 # URI and label (title) of GIT logo link
66 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
67 #our $logo_label = "git documentation";
68 our $logo_url = "http://git.or.cz/";
69 our $logo_label = "git homepage";
71 # source of projects list
72 our $projects_list = "++GITWEB_LIST++";
74 # default order of projects list
75 # valid values are none, project, descr, owner, and age
76 our $default_projects_order = "project";
78 # show repository only if this file exists
79 # (only effective if this variable evaluates to true)
80 our $export_ok = "++GITWEB_EXPORT_OK++";
82 # only allow viewing of repositories also shown on the overview page
83 our $strict_export = "++GITWEB_STRICT_EXPORT++";
85 # list of git base URLs used for URL to where fetch project from,
86 # i.e. full URL is "$git_base_url/$project"
87 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
89 # default blob_plain mimetype and default charset for text/plain blob
90 our $default_blob_plain_mimetype = 'text/plain';
91 our $default_text_plain_charset = undef;
93 # file to use for guessing MIME types before trying /etc/mime.types
94 # (relative to the current git repository)
95 our $mimetypes_file = undef;
97 # assume this charset if line contains non-UTF-8 characters;
98 # it should be valid encoding (see Encoding::Supported(3pm) for list),
99 # for which encoding all byte sequences are valid, for example
100 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
101 # could be even 'utf-8' for the old behavior)
102 our $fallback_encoding = 'latin1';
104 # You define site-wide feature defaults here; override them with
105 # $GITWEB_CONFIG as necessary.
106 our %feature = (
107 # feature => {
108 # 'sub' => feature-sub (subroutine),
109 # 'override' => allow-override (boolean),
110 # 'default' => [ default options...] (array reference)}
112 # if feature is overridable (it means that allow-override has true value),
113 # then feature-sub will be called with default options as parameters;
114 # return value of feature-sub indicates if to enable specified feature
116 # if there is no 'sub' key (no feature-sub), then feature cannot be
117 # overriden
119 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
121 # Enable the 'blame' blob view, showing the last commit that modified
122 # each line in the file. This can be very CPU-intensive.
124 # To enable system wide have in $GITWEB_CONFIG
125 # $feature{'blame'}{'default'} = [1];
126 # To have project specific config enable override in $GITWEB_CONFIG
127 # $feature{'blame'}{'override'} = 1;
128 # and in project config gitweb.blame = 0|1;
129 'blame' => {
130 'sub' => \&feature_blame,
131 'override' => 0,
132 'default' => [0]},
134 # Enable the 'snapshot' link, providing a compressed tarball of any
135 # tree. This can potentially generate high traffic if you have large
136 # project.
138 # To disable system wide have in $GITWEB_CONFIG
139 # $feature{'snapshot'}{'default'} = [undef];
140 # To have project specific config enable override in $GITWEB_CONFIG
141 # $feature{'snapshot'}{'override'} = 1;
142 # and in project config gitweb.snapshot = none|gzip|bzip2|zip;
143 'snapshot' => {
144 'sub' => \&feature_snapshot,
145 'override' => 0,
146 # => [content-encoding, suffix, program]
147 'default' => ['x-gzip', 'gz', 'gzip']},
149 # Enable text search, which will list the commits which match author,
150 # committer or commit text to a given string. Enabled by default.
151 # Project specific override is not supported.
152 'search' => {
153 'override' => 0,
154 'default' => [1]},
156 # Enable grep search, which will list the files in currently selected
157 # tree containing the given string. Enabled by default. This can be
158 # potentially CPU-intensive, of course.
160 # To enable system wide have in $GITWEB_CONFIG
161 # $feature{'grep'}{'default'} = [1];
162 # To have project specific config enable override in $GITWEB_CONFIG
163 # $feature{'grep'}{'override'} = 1;
164 # and in project config gitweb.grep = 0|1;
165 'grep' => {
166 'override' => 0,
167 'default' => [1]},
169 # Enable the pickaxe search, which will list the commits that modified
170 # a given string in a file. This can be practical and quite faster
171 # alternative to 'blame', but still potentially CPU-intensive.
173 # To enable system wide have in $GITWEB_CONFIG
174 # $feature{'pickaxe'}{'default'} = [1];
175 # To have project specific config enable override in $GITWEB_CONFIG
176 # $feature{'pickaxe'}{'override'} = 1;
177 # and in project config gitweb.pickaxe = 0|1;
178 'pickaxe' => {
179 'sub' => \&feature_pickaxe,
180 'override' => 0,
181 'default' => [1]},
183 # Make gitweb use an alternative format of the URLs which can be
184 # more readable and natural-looking: project name is embedded
185 # directly in the path and the query string contains other
186 # auxiliary information. All gitweb installations recognize
187 # URL in either format; this configures in which formats gitweb
188 # generates links.
190 # To enable system wide have in $GITWEB_CONFIG
191 # $feature{'pathinfo'}{'default'} = [1];
192 # Project specific override is not supported.
194 # Note that you will need to change the default location of CSS,
195 # favicon, logo and possibly other files to an absolute URL. Also,
196 # if gitweb.cgi serves as your indexfile, you will need to force
197 # $my_uri to contain the script name in your $GITWEB_CONFIG.
198 'pathinfo' => {
199 'override' => 0,
200 'default' => [0]},
202 # Make gitweb consider projects in project root subdirectories
203 # to be forks of existing projects. Given project $projname.git,
204 # projects matching $projname/*.git will not be shown in the main
205 # projects list, instead a '+' mark will be added to $projname
206 # there and a 'forks' view will be enabled for the project, listing
207 # all the forks. If project list is taken from a file, forks have
208 # to be listed after the main project.
210 # To enable system wide have in $GITWEB_CONFIG
211 # $feature{'forks'}{'default'} = [1];
212 # Project specific override is not supported.
213 'forks' => {
214 'override' => 0,
215 'default' => [0]},
218 sub gitweb_check_feature {
219 my ($name) = @_;
220 return unless exists $feature{$name};
221 my ($sub, $override, @defaults) = (
222 $feature{$name}{'sub'},
223 $feature{$name}{'override'},
224 @{$feature{$name}{'default'}});
225 if (!$override) { return @defaults; }
226 if (!defined $sub) {
227 warn "feature $name is not overrideable";
228 return @defaults;
230 return $sub->(@defaults);
233 sub feature_blame {
234 my ($val) = git_get_project_config('blame', '--bool');
236 if ($val eq 'true') {
237 return 1;
238 } elsif ($val eq 'false') {
239 return 0;
242 return $_[0];
245 sub feature_snapshot {
246 my ($ctype, $suffix, $command) = @_;
248 my ($val) = git_get_project_config('snapshot');
250 if ($val eq 'gzip') {
251 return ('x-gzip', 'gz', 'gzip');
252 } elsif ($val eq 'bzip2') {
253 return ('x-bzip2', 'bz2', 'bzip2');
254 } elsif ($val eq 'zip') {
255 return ('x-zip', 'zip', '');
256 } elsif ($val eq 'none') {
257 return ();
260 return ($ctype, $suffix, $command);
263 sub gitweb_have_snapshot {
264 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
265 my $have_snapshot = (defined $ctype && defined $suffix);
267 return $have_snapshot;
270 sub feature_grep {
271 my ($val) = git_get_project_config('grep', '--bool');
273 if ($val eq 'true') {
274 return (1);
275 } elsif ($val eq 'false') {
276 return (0);
279 return ($_[0]);
282 sub feature_pickaxe {
283 my ($val) = git_get_project_config('pickaxe', '--bool');
285 if ($val eq 'true') {
286 return (1);
287 } elsif ($val eq 'false') {
288 return (0);
291 return ($_[0]);
294 # checking HEAD file with -e is fragile if the repository was
295 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
296 # and then pruned.
297 sub check_head_link {
298 my ($dir) = @_;
299 my $headfile = "$dir/HEAD";
300 return ((-e $headfile) ||
301 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
304 sub check_export_ok {
305 my ($dir) = @_;
306 return (check_head_link($dir) &&
307 (!$export_ok || -e "$dir/$export_ok"));
310 # rename detection options for git-diff and git-diff-tree
311 # - default is '-M', with the cost proportional to
312 # (number of removed files) * (number of new files).
313 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
314 # (number of changed files + number of removed files) * (number of new files)
315 # - even more costly is '-C', '--find-copies-harder' with cost
316 # (number of files in the original tree) * (number of new files)
317 # - one might want to include '-B' option, e.g. '-B', '-M'
318 our @diff_opts = ('-M'); # taken from git_commit
320 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
321 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
323 # version of the core git binary
324 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
326 $projects_list ||= $projectroot;
328 # ======================================================================
329 # input validation and dispatch
330 our $action = $cgi->param('a');
331 if (defined $action) {
332 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
333 die_error(undef, "Invalid action parameter");
337 # parameters which are pathnames
338 our $project = $cgi->param('p');
339 if (defined $project) {
340 if (!validate_pathname($project) ||
341 !(-d "$projectroot/$project") ||
342 !check_head_link("$projectroot/$project") ||
343 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
344 ($strict_export && !project_in_list($project))) {
345 undef $project;
346 die_error(undef, "No such project");
350 our $file_name = $cgi->param('f');
351 if (defined $file_name) {
352 if (!validate_pathname($file_name)) {
353 die_error(undef, "Invalid file parameter");
357 our $file_parent = $cgi->param('fp');
358 if (defined $file_parent) {
359 if (!validate_pathname($file_parent)) {
360 die_error(undef, "Invalid file parent parameter");
364 # parameters which are refnames
365 our $hash = $cgi->param('h');
366 if (defined $hash) {
367 if (!validate_refname($hash)) {
368 die_error(undef, "Invalid hash parameter");
372 our $hash_parent = $cgi->param('hp');
373 if (defined $hash_parent) {
374 if (!validate_refname($hash_parent)) {
375 die_error(undef, "Invalid hash parent parameter");
379 our $hash_base = $cgi->param('hb');
380 if (defined $hash_base) {
381 if (!validate_refname($hash_base)) {
382 die_error(undef, "Invalid hash base parameter");
386 our $hash_parent_base = $cgi->param('hpb');
387 if (defined $hash_parent_base) {
388 if (!validate_refname($hash_parent_base)) {
389 die_error(undef, "Invalid hash parent base parameter");
393 # other parameters
394 our $page = $cgi->param('pg');
395 if (defined $page) {
396 if ($page =~ m/[^0-9]/) {
397 die_error(undef, "Invalid page parameter");
401 our $searchtype = $cgi->param('st');
402 if (defined $searchtype) {
403 if ($searchtype =~ m/[^a-z]/) {
404 die_error(undef, "Invalid searchtype parameter");
408 our $searchtext = $cgi->param('s');
409 our $search_regexp;
410 if (defined $searchtext) {
411 if ($searchtype ne 'grep' and $searchtype ne 'pickaxe' and $searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
412 die_error(undef, "Invalid search parameter");
414 if (length($searchtext) < 2) {
415 die_error(undef, "At least two characters are required for search parameter");
417 $search_regexp = quotemeta $searchtext;
420 # now read PATH_INFO and use it as alternative to parameters
421 sub evaluate_path_info {
422 return if defined $project;
423 my $path_info = $ENV{"PATH_INFO"};
424 return if !$path_info;
425 $path_info =~ s,^/+,,;
426 return if !$path_info;
427 # find which part of PATH_INFO is project
428 $project = $path_info;
429 $project =~ s,/+$,,;
430 while ($project && !check_head_link("$projectroot/$project")) {
431 $project =~ s,/*[^/]*$,,;
433 # validate project
434 $project = validate_pathname($project);
435 if (!$project ||
436 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
437 ($strict_export && !project_in_list($project))) {
438 undef $project;
439 return;
441 # do not change any parameters if an action is given using the query string
442 return if $action;
443 $path_info =~ s,^$project/*,,;
444 my ($refname, $pathname) = split(/:/, $path_info, 2);
445 if (defined $pathname) {
446 # we got "project.git/branch:filename" or "project.git/branch:dir/"
447 # we could use git_get_type(branch:pathname), but it needs $git_dir
448 $pathname =~ s,^/+,,;
449 if (!$pathname || substr($pathname, -1) eq "/") {
450 $action ||= "tree";
451 $pathname =~ s,/$,,;
452 } else {
453 $action ||= "blob_plain";
455 $hash_base ||= validate_refname($refname);
456 $file_name ||= validate_pathname($pathname);
457 } elsif (defined $refname) {
458 # we got "project.git/branch"
459 $action ||= "shortlog";
460 $hash ||= validate_refname($refname);
463 evaluate_path_info();
465 # path to the current git repository
466 our $git_dir;
467 $git_dir = "$projectroot/$project" if $project;
469 # dispatch
470 my %actions = (
471 "blame" => \&git_blame2,
472 "blobdiff" => \&git_blobdiff,
473 "blobdiff_plain" => \&git_blobdiff_plain,
474 "blob" => \&git_blob,
475 "blob_plain" => \&git_blob_plain,
476 "commitdiff" => \&git_commitdiff,
477 "commitdiff_plain" => \&git_commitdiff_plain,
478 "commit" => \&git_commit,
479 "forks" => \&git_forks,
480 "heads" => \&git_heads,
481 "history" => \&git_history,
482 "log" => \&git_log,
483 "rss" => \&git_rss,
484 "atom" => \&git_atom,
485 "search" => \&git_search,
486 "search_help" => \&git_search_help,
487 "shortlog" => \&git_shortlog,
488 "summary" => \&git_summary,
489 "tag" => \&git_tag,
490 "tags" => \&git_tags,
491 "tree" => \&git_tree,
492 "snapshot" => \&git_snapshot,
493 "object" => \&git_object,
494 # those below don't need $project
495 "opml" => \&git_opml,
496 "project_list" => \&git_project_list,
497 "project_index" => \&git_project_index,
500 if (!defined $action) {
501 if (defined $hash) {
502 $action = git_get_type($hash);
503 } elsif (defined $hash_base && defined $file_name) {
504 $action = git_get_type("$hash_base:$file_name");
505 } elsif (defined $project) {
506 $action = 'summary';
507 } else {
508 $action = 'project_list';
511 if (!defined($actions{$action})) {
512 die_error(undef, "Unknown action");
514 if ($action !~ m/^(opml|project_list|project_index)$/ &&
515 !$project) {
516 die_error(undef, "Project needed");
518 $actions{$action}->();
519 exit;
521 ## ======================================================================
522 ## action links
524 sub href(%) {
525 my %params = @_;
526 # default is to use -absolute url() i.e. $my_uri
527 my $href = $params{-full} ? $my_url : $my_uri;
529 # XXX: Warning: If you touch this, check the search form for updating,
530 # too.
532 my @mapping = (
533 project => "p",
534 action => "a",
535 file_name => "f",
536 file_parent => "fp",
537 hash => "h",
538 hash_parent => "hp",
539 hash_base => "hb",
540 hash_parent_base => "hpb",
541 page => "pg",
542 order => "o",
543 searchtext => "s",
544 searchtype => "st",
546 my %mapping = @mapping;
548 $params{'project'} = $project unless exists $params{'project'};
550 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
551 if ($use_pathinfo) {
552 # use PATH_INFO for project name
553 $href .= "/$params{'project'}" if defined $params{'project'};
554 delete $params{'project'};
556 # Summary just uses the project path URL
557 if (defined $params{'action'} && $params{'action'} eq 'summary') {
558 delete $params{'action'};
562 # now encode the parameters explicitly
563 my @result = ();
564 for (my $i = 0; $i < @mapping; $i += 2) {
565 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
566 if (defined $params{$name}) {
567 push @result, $symbol . "=" . esc_param($params{$name});
570 $href .= "?" . join(';', @result) if scalar @result;
572 return $href;
576 ## ======================================================================
577 ## validation, quoting/unquoting and escaping
579 sub validate_pathname {
580 my $input = shift || return undef;
582 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
583 # at the beginning, at the end, and between slashes.
584 # also this catches doubled slashes
585 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
586 return undef;
588 # no null characters
589 if ($input =~ m!\0!) {
590 return undef;
592 return $input;
595 sub validate_refname {
596 my $input = shift || return undef;
598 # textual hashes are O.K.
599 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
600 return $input;
602 # it must be correct pathname
603 $input = validate_pathname($input)
604 or return undef;
605 # restrictions on ref name according to git-check-ref-format
606 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
607 return undef;
609 return $input;
612 # decode sequences of octets in utf8 into Perl's internal form,
613 # which is utf-8 with utf8 flag set if needed. gitweb writes out
614 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
615 sub to_utf8 {
616 my $str = shift;
617 my $res;
618 eval { $res = decode_utf8($str, Encode::FB_CROAK); };
619 if (defined $res) {
620 return $res;
621 } else {
622 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
626 # quote unsafe chars, but keep the slash, even when it's not
627 # correct, but quoted slashes look too horrible in bookmarks
628 sub esc_param {
629 my $str = shift;
630 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
631 $str =~ s/\+/%2B/g;
632 $str =~ s/ /\+/g;
633 return $str;
636 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
637 sub esc_url {
638 my $str = shift;
639 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
640 $str =~ s/\+/%2B/g;
641 $str =~ s/ /\+/g;
642 return $str;
645 # replace invalid utf8 character with SUBSTITUTION sequence
646 sub esc_html ($;%) {
647 my $str = shift;
648 my %opts = @_;
650 $str = to_utf8($str);
651 $str = $cgi->escapeHTML($str);
652 if ($opts{'-nbsp'}) {
653 $str =~ s/ /&nbsp;/g;
655 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
656 return $str;
659 # quote control characters and escape filename to HTML
660 sub esc_path {
661 my $str = shift;
662 my %opts = @_;
664 $str = to_utf8($str);
665 $str = $cgi->escapeHTML($str);
666 if ($opts{'-nbsp'}) {
667 $str =~ s/ /&nbsp;/g;
669 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
670 return $str;
673 # Make control characters "printable", using character escape codes (CEC)
674 sub quot_cec {
675 my $cntrl = shift;
676 my %es = ( # character escape codes, aka escape sequences
677 "\t" => '\t', # tab (HT)
678 "\n" => '\n', # line feed (LF)
679 "\r" => '\r', # carrige return (CR)
680 "\f" => '\f', # form feed (FF)
681 "\b" => '\b', # backspace (BS)
682 "\a" => '\a', # alarm (bell) (BEL)
683 "\e" => '\e', # escape (ESC)
684 "\013" => '\v', # vertical tab (VT)
685 "\000" => '\0', # nul character (NUL)
687 my $chr = ( (exists $es{$cntrl})
688 ? $es{$cntrl}
689 : sprintf('\%03o', ord($cntrl)) );
690 return "<span class=\"cntrl\">$chr</span>";
693 # Alternatively use unicode control pictures codepoints,
694 # Unicode "printable representation" (PR)
695 sub quot_upr {
696 my $cntrl = shift;
697 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
698 return "<span class=\"cntrl\">$chr</span>";
701 # git may return quoted and escaped filenames
702 sub unquote {
703 my $str = shift;
705 sub unq {
706 my $seq = shift;
707 my %es = ( # character escape codes, aka escape sequences
708 't' => "\t", # tab (HT, TAB)
709 'n' => "\n", # newline (NL)
710 'r' => "\r", # return (CR)
711 'f' => "\f", # form feed (FF)
712 'b' => "\b", # backspace (BS)
713 'a' => "\a", # alarm (bell) (BEL)
714 'e' => "\e", # escape (ESC)
715 'v' => "\013", # vertical tab (VT)
718 if ($seq =~ m/^[0-7]{1,3}$/) {
719 # octal char sequence
720 return chr(oct($seq));
721 } elsif (exists $es{$seq}) {
722 # C escape sequence, aka character escape code
723 return $es{$seq}
725 # quoted ordinary character
726 return $seq;
729 if ($str =~ m/^"(.*)"$/) {
730 # needs unquoting
731 $str = $1;
732 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
734 return $str;
737 # escape tabs (convert tabs to spaces)
738 sub untabify {
739 my $line = shift;
741 while ((my $pos = index($line, "\t")) != -1) {
742 if (my $count = (8 - ($pos % 8))) {
743 my $spaces = ' ' x $count;
744 $line =~ s/\t/$spaces/;
748 return $line;
751 sub project_in_list {
752 my $project = shift;
753 my @list = git_get_projects_list();
754 return @list && scalar(grep { $_->{'path'} eq $project } @list);
757 ## ----------------------------------------------------------------------
758 ## HTML aware string manipulation
760 sub chop_str {
761 my $str = shift;
762 my $len = shift;
763 my $add_len = shift || 10;
765 # allow only $len chars, but don't cut a word if it would fit in $add_len
766 # if it doesn't fit, cut it if it's still longer than the dots we would add
767 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
768 my $body = $1;
769 my $tail = $2;
770 if (length($tail) > 4) {
771 $tail = " ...";
772 $body =~ s/&[^;]*$//; # remove chopped character entities
774 return "$body$tail";
777 ## ----------------------------------------------------------------------
778 ## functions returning short strings
780 # CSS class for given age value (in seconds)
781 sub age_class {
782 my $age = shift;
784 if (!defined $age) {
785 return "noage";
786 } elsif ($age < 60*60*2) {
787 return "age0";
788 } elsif ($age < 60*60*24*2) {
789 return "age1";
790 } else {
791 return "age2";
795 # convert age in seconds to "nn units ago" string
796 sub age_string {
797 my $age = shift;
798 my $age_str;
800 if ($age > 60*60*24*365*2) {
801 $age_str = (int $age/60/60/24/365);
802 $age_str .= " years ago";
803 } elsif ($age > 60*60*24*(365/12)*2) {
804 $age_str = int $age/60/60/24/(365/12);
805 $age_str .= " months ago";
806 } elsif ($age > 60*60*24*7*2) {
807 $age_str = int $age/60/60/24/7;
808 $age_str .= " weeks ago";
809 } elsif ($age > 60*60*24*2) {
810 $age_str = int $age/60/60/24;
811 $age_str .= " days ago";
812 } elsif ($age > 60*60*2) {
813 $age_str = int $age/60/60;
814 $age_str .= " hours ago";
815 } elsif ($age > 60*2) {
816 $age_str = int $age/60;
817 $age_str .= " min ago";
818 } elsif ($age > 2) {
819 $age_str = int $age;
820 $age_str .= " sec ago";
821 } else {
822 $age_str .= " right now";
824 return $age_str;
827 # convert file mode in octal to symbolic file mode string
828 sub mode_str {
829 my $mode = oct shift;
831 if (S_ISDIR($mode & S_IFMT)) {
832 return 'drwxr-xr-x';
833 } elsif (S_ISLNK($mode)) {
834 return 'lrwxrwxrwx';
835 } elsif (S_ISREG($mode)) {
836 # git cares only about the executable bit
837 if ($mode & S_IXUSR) {
838 return '-rwxr-xr-x';
839 } else {
840 return '-rw-r--r--';
842 } else {
843 return '----------';
847 # convert file mode in octal to file type string
848 sub file_type {
849 my $mode = shift;
851 if ($mode !~ m/^[0-7]+$/) {
852 return $mode;
853 } else {
854 $mode = oct $mode;
857 if (S_ISDIR($mode & S_IFMT)) {
858 return "directory";
859 } elsif (S_ISLNK($mode)) {
860 return "symlink";
861 } elsif (S_ISREG($mode)) {
862 return "file";
863 } else {
864 return "unknown";
868 # convert file mode in octal to file type description string
869 sub file_type_long {
870 my $mode = shift;
872 if ($mode !~ m/^[0-7]+$/) {
873 return $mode;
874 } else {
875 $mode = oct $mode;
878 if (S_ISDIR($mode & S_IFMT)) {
879 return "directory";
880 } elsif (S_ISLNK($mode)) {
881 return "symlink";
882 } elsif (S_ISREG($mode)) {
883 if ($mode & S_IXUSR) {
884 return "executable";
885 } else {
886 return "file";
888 } else {
889 return "unknown";
894 ## ----------------------------------------------------------------------
895 ## functions returning short HTML fragments, or transforming HTML fragments
896 ## which don't belong to other sections
898 # format line of commit message.
899 sub format_log_line_html {
900 my $line = shift;
902 $line = esc_html($line, -nbsp=>1);
903 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
904 my $hash_text = $1;
905 my $link =
906 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
907 -class => "text"}, $hash_text);
908 $line =~ s/$hash_text/$link/;
910 return $line;
913 # format marker of refs pointing to given object
914 sub format_ref_marker {
915 my ($refs, $id) = @_;
916 my $markers = '';
918 if (defined $refs->{$id}) {
919 foreach my $ref (@{$refs->{$id}}) {
920 my ($type, $name) = qw();
921 # e.g. tags/v2.6.11 or heads/next
922 if ($ref =~ m!^(.*?)s?/(.*)$!) {
923 $type = $1;
924 $name = $2;
925 } else {
926 $type = "ref";
927 $name = $ref;
930 $markers .= " <span class=\"$type\" title=\"$ref\">" .
931 esc_html($name) . "</span>";
935 if ($markers) {
936 return ' <span class="refs">'. $markers . '</span>';
937 } else {
938 return "";
942 # format, perhaps shortened and with markers, title line
943 sub format_subject_html {
944 my ($long, $short, $href, $extra) = @_;
945 $extra = '' unless defined($extra);
947 if (length($short) < length($long)) {
948 return $cgi->a({-href => $href, -class => "list subject",
949 -title => to_utf8($long)},
950 esc_html($short) . $extra);
951 } else {
952 return $cgi->a({-href => $href, -class => "list subject"},
953 esc_html($long) . $extra);
957 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
958 sub format_git_diff_header_line {
959 my $line = shift;
960 my $diffinfo = shift;
961 my ($from, $to) = @_;
963 if ($diffinfo->{'nparents'}) {
964 # combined diff
965 $line =~ s!^(diff (.*?) )"?.*$!$1!;
966 if ($to->{'href'}) {
967 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
968 esc_path($to->{'file'}));
969 } else { # file was deleted (no href)
970 $line .= esc_path($to->{'file'});
972 } else {
973 # "ordinary" diff
974 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
975 if ($from->{'href'}) {
976 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
977 'a/' . esc_path($from->{'file'}));
978 } else { # file was added (no href)
979 $line .= 'a/' . esc_path($from->{'file'});
981 $line .= ' ';
982 if ($to->{'href'}) {
983 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
984 'b/' . esc_path($to->{'file'}));
985 } else { # file was deleted
986 $line .= 'b/' . esc_path($to->{'file'});
990 return "<div class=\"diff header\">$line</div>\n";
993 # format extended diff header line, before patch itself
994 sub format_extended_diff_header_line {
995 my $line = shift;
996 my $diffinfo = shift;
997 my ($from, $to) = @_;
999 # match <path>
1000 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1001 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1002 esc_path($from->{'file'}));
1004 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1005 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1006 esc_path($to->{'file'}));
1008 # match single <mode>
1009 if ($line =~ m/\s(\d{6})$/) {
1010 $line .= '<span class="info"> (' .
1011 file_type_long($1) .
1012 ')</span>';
1014 # match <hash>
1015 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1016 # can match only for combined diff
1017 $line = 'index ';
1018 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1019 if ($from->{'href'}[$i]) {
1020 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1021 -class=>"hash"},
1022 substr($diffinfo->{'from_id'}[$i],0,7));
1023 } else {
1024 $line .= '0' x 7;
1026 # separator
1027 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1029 $line .= '..';
1030 if ($to->{'href'}) {
1031 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1032 substr($diffinfo->{'to_id'},0,7));
1033 } else {
1034 $line .= '0' x 7;
1037 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1038 # can match only for ordinary diff
1039 my ($from_link, $to_link);
1040 if ($from->{'href'}) {
1041 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1042 substr($diffinfo->{'from_id'},0,7));
1043 } else {
1044 $from_link = '0' x 7;
1046 if ($to->{'href'}) {
1047 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1048 substr($diffinfo->{'to_id'},0,7));
1049 } else {
1050 $to_link = '0' x 7;
1052 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1053 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1056 return $line . "<br/>\n";
1059 # format from-file/to-file diff header
1060 sub format_diff_from_to_header {
1061 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1062 my $line;
1063 my $result = '';
1065 $line = $from_line;
1066 #assert($line =~ m/^---/) if DEBUG;
1067 # no extra formatting for "^--- /dev/null"
1068 if (! $diffinfo->{'nparents'}) {
1069 # ordinary (single parent) diff
1070 if ($line =~ m!^--- "?a/!) {
1071 if ($from->{'href'}) {
1072 $line = '--- a/' .
1073 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1074 esc_path($from->{'file'}));
1075 } else {
1076 $line = '--- a/' .
1077 esc_path($from->{'file'});
1080 $result .= qq!<div class="diff from_file">$line</div>\n!;
1082 } else {
1083 # combined diff (merge commit)
1084 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1085 if ($from->{'href'}[$i]) {
1086 $line = '--- ' .
1087 $cgi->a({-href=>href(action=>"blobdiff",
1088 hash_parent=>$diffinfo->{'from_id'}[$i],
1089 hash_parent_base=>$parents[$i],
1090 file_parent=>$from->{'file'}[$i],
1091 hash=>$diffinfo->{'to_id'},
1092 hash_base=>$hash,
1093 file_name=>$to->{'file'}),
1094 -class=>"path",
1095 -title=>"diff" . ($i+1)},
1096 $i+1) .
1097 '/' .
1098 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1099 esc_path($from->{'file'}[$i]));
1100 } else {
1101 $line = '--- /dev/null';
1103 $result .= qq!<div class="diff from_file">$line</div>\n!;
1107 $line = $to_line;
1108 #assert($line =~ m/^\+\+\+/) if DEBUG;
1109 # no extra formatting for "^+++ /dev/null"
1110 if ($line =~ m!^\+\+\+ "?b/!) {
1111 if ($to->{'href'}) {
1112 $line = '+++ b/' .
1113 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1114 esc_path($to->{'file'}));
1115 } else {
1116 $line = '+++ b/' .
1117 esc_path($to->{'file'});
1120 $result .= qq!<div class="diff to_file">$line</div>\n!;
1122 return $result;
1125 # format patch (diff) line (not to be used for diff headers)
1126 sub format_diff_line {
1127 my $line = shift;
1128 my ($from, $to) = @_;
1129 my $diff_class = "";
1131 chomp $line;
1133 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1134 # combined diff
1135 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1136 if ($line =~ m/^\@{3}/) {
1137 $diff_class = " chunk_header";
1138 } elsif ($line =~ m/^\\/) {
1139 $diff_class = " incomplete";
1140 } elsif ($prefix =~ tr/+/+/) {
1141 $diff_class = " add";
1142 } elsif ($prefix =~ tr/-/-/) {
1143 $diff_class = " rem";
1145 } else {
1146 # assume ordinary diff
1147 my $char = substr($line, 0, 1);
1148 if ($char eq '+') {
1149 $diff_class = " add";
1150 } elsif ($char eq '-') {
1151 $diff_class = " rem";
1152 } elsif ($char eq '@') {
1153 $diff_class = " chunk_header";
1154 } elsif ($char eq "\\") {
1155 $diff_class = " incomplete";
1158 $line = untabify($line);
1159 if ($from && $to && $line =~ m/^\@{2} /) {
1160 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1161 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1163 $from_lines = 0 unless defined $from_lines;
1164 $to_lines = 0 unless defined $to_lines;
1166 if ($from->{'href'}) {
1167 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1168 -class=>"list"}, $from_text);
1170 if ($to->{'href'}) {
1171 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1172 -class=>"list"}, $to_text);
1174 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1175 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1176 return "<div class=\"diff$diff_class\">$line</div>\n";
1177 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1178 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1179 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1181 @from_text = split(' ', $ranges);
1182 for (my $i = 0; $i < @from_text; ++$i) {
1183 ($from_start[$i], $from_nlines[$i]) =
1184 (split(',', substr($from_text[$i], 1)), 0);
1187 $to_text = pop @from_text;
1188 $to_start = pop @from_start;
1189 $to_nlines = pop @from_nlines;
1191 $line = "<span class=\"chunk_info\">$prefix ";
1192 for (my $i = 0; $i < @from_text; ++$i) {
1193 if ($from->{'href'}[$i]) {
1194 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1195 -class=>"list"}, $from_text[$i]);
1196 } else {
1197 $line .= $from_text[$i];
1199 $line .= " ";
1201 if ($to->{'href'}) {
1202 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1203 -class=>"list"}, $to_text);
1204 } else {
1205 $line .= $to_text;
1207 $line .= " $prefix</span>" .
1208 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1209 return "<div class=\"diff$diff_class\">$line</div>\n";
1211 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1214 ## ----------------------------------------------------------------------
1215 ## git utility subroutines, invoking git commands
1217 # returns path to the core git executable and the --git-dir parameter as list
1218 sub git_cmd {
1219 return $GIT, '--git-dir='.$git_dir;
1222 # returns path to the core git executable and the --git-dir parameter as string
1223 sub git_cmd_str {
1224 return join(' ', git_cmd());
1227 # get HEAD ref of given project as hash
1228 sub git_get_head_hash {
1229 my $project = shift;
1230 my $o_git_dir = $git_dir;
1231 my $retval = undef;
1232 $git_dir = "$projectroot/$project";
1233 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1234 my $head = <$fd>;
1235 close $fd;
1236 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1237 $retval = $1;
1240 if (defined $o_git_dir) {
1241 $git_dir = $o_git_dir;
1243 return $retval;
1246 # get type of given object
1247 sub git_get_type {
1248 my $hash = shift;
1250 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1251 my $type = <$fd>;
1252 close $fd or return;
1253 chomp $type;
1254 return $type;
1257 sub git_get_project_config {
1258 my ($key, $type) = @_;
1260 return unless ($key);
1261 $key =~ s/^gitweb\.//;
1262 return if ($key =~ m/\W/);
1264 my @x = (git_cmd(), 'config');
1265 if (defined $type) { push @x, $type; }
1266 push @x, "--get";
1267 push @x, "gitweb.$key";
1268 my $val = qx(@x);
1269 chomp $val;
1270 return ($val);
1273 # get hash of given path at given ref
1274 sub git_get_hash_by_path {
1275 my $base = shift;
1276 my $path = shift || return undef;
1277 my $type = shift;
1279 $path =~ s,/+$,,;
1281 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1282 or die_error(undef, "Open git-ls-tree failed");
1283 my $line = <$fd>;
1284 close $fd or return undef;
1286 if (!defined $line) {
1287 # there is no tree or hash given by $path at $base
1288 return undef;
1291 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1292 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1293 if (defined $type && $type ne $2) {
1294 # type doesn't match
1295 return undef;
1297 return $3;
1300 # get path of entry with given hash at given tree-ish (ref)
1301 # used to get 'from' filename for combined diff (merge commit) for renames
1302 sub git_get_path_by_hash {
1303 my $base = shift || return;
1304 my $hash = shift || return;
1306 local $/ = "\0";
1308 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1309 or return undef;
1310 while (my $line = <$fd>) {
1311 chomp $line;
1313 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1314 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1315 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1316 close $fd;
1317 return $1;
1320 close $fd;
1321 return undef;
1324 ## ......................................................................
1325 ## git utility functions, directly accessing git repository
1327 sub git_get_project_description {
1328 my $path = shift;
1330 open my $fd, "$projectroot/$path/description" or return undef;
1331 my $descr = <$fd>;
1332 close $fd;
1333 if (defined $descr) {
1334 chomp $descr;
1336 return $descr;
1339 sub git_get_project_url_list {
1340 my $path = shift;
1342 open my $fd, "$projectroot/$path/cloneurl" or return;
1343 my @git_project_url_list = map { chomp; $_ } <$fd>;
1344 close $fd;
1346 return wantarray ? @git_project_url_list : \@git_project_url_list;
1349 sub git_get_projects_list {
1350 my ($filter) = @_;
1351 my @list;
1353 $filter ||= '';
1354 $filter =~ s/\.git$//;
1356 my ($check_forks) = gitweb_check_feature('forks');
1358 if (-d $projects_list) {
1359 # search in directory
1360 my $dir = $projects_list . ($filter ? "/$filter" : '');
1361 # remove the trailing "/"
1362 $dir =~ s!/+$!!;
1363 my $pfxlen = length("$dir");
1365 File::Find::find({
1366 follow_fast => 1, # follow symbolic links
1367 dangling_symlinks => 0, # ignore dangling symlinks, silently
1368 wanted => sub {
1369 # skip project-list toplevel, if we get it.
1370 return if (m!^[/.]$!);
1371 # only directories can be git repositories
1372 return unless (-d $_);
1374 my $subdir = substr($File::Find::name, $pfxlen + 1);
1375 # we check related file in $projectroot
1376 if ($check_forks and $subdir =~ m#/.#) {
1377 $File::Find::prune = 1;
1378 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1379 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1380 $File::Find::prune = 1;
1383 }, "$dir");
1385 } elsif (-f $projects_list) {
1386 # read from file(url-encoded):
1387 # 'git%2Fgit.git Linus+Torvalds'
1388 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1389 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1390 my %paths;
1391 open my ($fd), $projects_list or return;
1392 PROJECT:
1393 while (my $line = <$fd>) {
1394 chomp $line;
1395 my ($path, $owner) = split ' ', $line;
1396 $path = unescape($path);
1397 $owner = unescape($owner);
1398 if (!defined $path) {
1399 next;
1401 if ($filter ne '') {
1402 # looking for forks;
1403 my $pfx = substr($path, 0, length($filter));
1404 if ($pfx ne $filter) {
1405 next PROJECT;
1407 my $sfx = substr($path, length($filter));
1408 if ($sfx !~ /^\/.*\.git$/) {
1409 next PROJECT;
1411 } elsif ($check_forks) {
1412 PATH:
1413 foreach my $filter (keys %paths) {
1414 # looking for forks;
1415 my $pfx = substr($path, 0, length($filter));
1416 if ($pfx ne $filter) {
1417 next PATH;
1419 my $sfx = substr($path, length($filter));
1420 if ($sfx !~ /^\/.*\.git$/) {
1421 next PATH;
1423 # is a fork, don't include it in
1424 # the list
1425 next PROJECT;
1428 if (check_export_ok("$projectroot/$path")) {
1429 my $pr = {
1430 path => $path,
1431 owner => to_utf8($owner),
1433 push @list, $pr;
1434 (my $forks_path = $path) =~ s/\.git$//;
1435 $paths{$forks_path}++;
1438 close $fd;
1440 return @list;
1443 sub git_get_project_owner {
1444 my $project = shift;
1445 my $owner;
1447 return undef unless $project;
1449 # read from file (url-encoded):
1450 # 'git%2Fgit.git Linus+Torvalds'
1451 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1452 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1453 if (-f $projects_list) {
1454 open (my $fd , $projects_list);
1455 while (my $line = <$fd>) {
1456 chomp $line;
1457 my ($pr, $ow) = split ' ', $line;
1458 $pr = unescape($pr);
1459 $ow = unescape($ow);
1460 if ($pr eq $project) {
1461 $owner = to_utf8($ow);
1462 last;
1465 close $fd;
1467 if (!defined $owner) {
1468 $owner = get_file_owner("$projectroot/$project");
1471 return $owner;
1474 sub git_get_last_activity {
1475 my ($path) = @_;
1476 my $fd;
1478 $git_dir = "$projectroot/$path";
1479 open($fd, "-|", git_cmd(), 'for-each-ref',
1480 '--format=%(committer)',
1481 '--sort=-committerdate',
1482 '--count=1',
1483 'refs/heads') or return;
1484 my $most_recent = <$fd>;
1485 close $fd or return;
1486 if (defined $most_recent &&
1487 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1488 my $timestamp = $1;
1489 my $age = time - $timestamp;
1490 return ($age, age_string($age));
1494 sub git_get_references {
1495 my $type = shift || "";
1496 my %refs;
1497 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1498 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1499 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1500 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1501 or return;
1503 while (my $line = <$fd>) {
1504 chomp $line;
1505 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1506 if (defined $refs{$1}) {
1507 push @{$refs{$1}}, $2;
1508 } else {
1509 $refs{$1} = [ $2 ];
1513 close $fd or return;
1514 return \%refs;
1517 sub git_get_rev_name_tags {
1518 my $hash = shift || return undef;
1520 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1521 or return;
1522 my $name_rev = <$fd>;
1523 close $fd;
1525 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1526 return $1;
1527 } else {
1528 # catches also '$hash undefined' output
1529 return undef;
1533 ## ----------------------------------------------------------------------
1534 ## parse to hash functions
1536 sub parse_date {
1537 my $epoch = shift;
1538 my $tz = shift || "-0000";
1540 my %date;
1541 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1542 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1543 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1544 $date{'hour'} = $hour;
1545 $date{'minute'} = $min;
1546 $date{'mday'} = $mday;
1547 $date{'day'} = $days[$wday];
1548 $date{'month'} = $months[$mon];
1549 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1550 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1551 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1552 $mday, $months[$mon], $hour ,$min;
1553 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1554 1900+$year, $mon, $mday, $hour ,$min, $sec;
1556 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1557 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1558 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1559 $date{'hour_local'} = $hour;
1560 $date{'minute_local'} = $min;
1561 $date{'tz_local'} = $tz;
1562 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1563 1900+$year, $mon+1, $mday,
1564 $hour, $min, $sec, $tz);
1565 return %date;
1568 sub parse_tag {
1569 my $tag_id = shift;
1570 my %tag;
1571 my @comment;
1573 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1574 $tag{'id'} = $tag_id;
1575 while (my $line = <$fd>) {
1576 chomp $line;
1577 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1578 $tag{'object'} = $1;
1579 } elsif ($line =~ m/^type (.+)$/) {
1580 $tag{'type'} = $1;
1581 } elsif ($line =~ m/^tag (.+)$/) {
1582 $tag{'name'} = $1;
1583 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1584 $tag{'author'} = $1;
1585 $tag{'epoch'} = $2;
1586 $tag{'tz'} = $3;
1587 } elsif ($line =~ m/--BEGIN/) {
1588 push @comment, $line;
1589 last;
1590 } elsif ($line eq "") {
1591 last;
1594 push @comment, <$fd>;
1595 $tag{'comment'} = \@comment;
1596 close $fd or return;
1597 if (!defined $tag{'name'}) {
1598 return
1600 return %tag
1603 sub parse_commit_text {
1604 my ($commit_text, $withparents) = @_;
1605 my @commit_lines = split '\n', $commit_text;
1606 my %co;
1608 pop @commit_lines; # Remove '\0'
1610 if (! @commit_lines) {
1611 return;
1614 my $header = shift @commit_lines;
1615 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1616 return;
1618 ($co{'id'}, my @parents) = split ' ', $header;
1619 while (my $line = shift @commit_lines) {
1620 last if $line eq "\n";
1621 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1622 $co{'tree'} = $1;
1623 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1624 push @parents, $1;
1625 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1626 $co{'author'} = $1;
1627 $co{'author_epoch'} = $2;
1628 $co{'author_tz'} = $3;
1629 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1630 $co{'author_name'} = $1;
1631 $co{'author_email'} = $2;
1632 } else {
1633 $co{'author_name'} = $co{'author'};
1635 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1636 $co{'committer'} = $1;
1637 $co{'committer_epoch'} = $2;
1638 $co{'committer_tz'} = $3;
1639 $co{'committer_name'} = $co{'committer'};
1640 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1641 $co{'committer_name'} = $1;
1642 $co{'committer_email'} = $2;
1643 } else {
1644 $co{'committer_name'} = $co{'committer'};
1648 if (!defined $co{'tree'}) {
1649 return;
1651 $co{'parents'} = \@parents;
1652 $co{'parent'} = $parents[0];
1654 foreach my $title (@commit_lines) {
1655 $title =~ s/^ //;
1656 if ($title ne "") {
1657 $co{'title'} = chop_str($title, 80, 5);
1658 # remove leading stuff of merges to make the interesting part visible
1659 if (length($title) > 50) {
1660 $title =~ s/^Automatic //;
1661 $title =~ s/^merge (of|with) /Merge ... /i;
1662 if (length($title) > 50) {
1663 $title =~ s/(http|rsync):\/\///;
1665 if (length($title) > 50) {
1666 $title =~ s/(master|www|rsync)\.//;
1668 if (length($title) > 50) {
1669 $title =~ s/kernel.org:?//;
1671 if (length($title) > 50) {
1672 $title =~ s/\/pub\/scm//;
1675 $co{'title_short'} = chop_str($title, 50, 5);
1676 last;
1679 if ($co{'title'} eq "") {
1680 $co{'title'} = $co{'title_short'} = '(no commit message)';
1682 # remove added spaces
1683 foreach my $line (@commit_lines) {
1684 $line =~ s/^ //;
1686 $co{'comment'} = \@commit_lines;
1688 my $age = time - $co{'committer_epoch'};
1689 $co{'age'} = $age;
1690 $co{'age_string'} = age_string($age);
1691 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1692 if ($age > 60*60*24*7*2) {
1693 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1694 $co{'age_string_age'} = $co{'age_string'};
1695 } else {
1696 $co{'age_string_date'} = $co{'age_string'};
1697 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1699 return %co;
1702 sub parse_commit {
1703 my ($commit_id) = @_;
1704 my %co;
1706 local $/ = "\0";
1708 open my $fd, "-|", git_cmd(), "rev-list",
1709 "--parents",
1710 "--header",
1711 "--max-count=1",
1712 $commit_id,
1713 "--",
1714 or die_error(undef, "Open git-rev-list failed");
1715 %co = parse_commit_text(<$fd>, 1);
1716 close $fd;
1718 return %co;
1721 sub parse_commits {
1722 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1723 my @cos;
1725 $maxcount ||= 1;
1726 $skip ||= 0;
1728 local $/ = "\0";
1730 open my $fd, "-|", git_cmd(), "rev-list",
1731 "--header",
1732 ($arg ? ($arg) : ()),
1733 ("--max-count=" . $maxcount),
1734 ("--skip=" . $skip),
1735 $commit_id,
1736 "--",
1737 ($filename ? ($filename) : ())
1738 or die_error(undef, "Open git-rev-list failed");
1739 while (my $line = <$fd>) {
1740 my %co = parse_commit_text($line);
1741 push @cos, \%co;
1743 close $fd;
1745 return wantarray ? @cos : \@cos;
1748 # parse ref from ref_file, given by ref_id, with given type
1749 sub parse_ref {
1750 my $ref_file = shift;
1751 my $ref_id = shift;
1752 my $type = shift || git_get_type($ref_id);
1753 my %ref_item;
1755 $ref_item{'type'} = $type;
1756 $ref_item{'id'} = $ref_id;
1757 $ref_item{'epoch'} = 0;
1758 $ref_item{'age'} = "unknown";
1759 if ($type eq "tag") {
1760 my %tag = parse_tag($ref_id);
1761 $ref_item{'comment'} = $tag{'comment'};
1762 if ($tag{'type'} eq "commit") {
1763 my %co = parse_commit($tag{'object'});
1764 $ref_item{'epoch'} = $co{'committer_epoch'};
1765 $ref_item{'age'} = $co{'age_string'};
1766 } elsif (defined($tag{'epoch'})) {
1767 my $age = time - $tag{'epoch'};
1768 $ref_item{'epoch'} = $tag{'epoch'};
1769 $ref_item{'age'} = age_string($age);
1771 $ref_item{'reftype'} = $tag{'type'};
1772 $ref_item{'name'} = $tag{'name'};
1773 $ref_item{'refid'} = $tag{'object'};
1774 } elsif ($type eq "commit"){
1775 my %co = parse_commit($ref_id);
1776 $ref_item{'reftype'} = "commit";
1777 $ref_item{'name'} = $ref_file;
1778 $ref_item{'title'} = $co{'title'};
1779 $ref_item{'refid'} = $ref_id;
1780 $ref_item{'epoch'} = $co{'committer_epoch'};
1781 $ref_item{'age'} = $co{'age_string'};
1782 } else {
1783 $ref_item{'reftype'} = $type;
1784 $ref_item{'name'} = $ref_file;
1785 $ref_item{'refid'} = $ref_id;
1788 return %ref_item;
1791 # parse line of git-diff-tree "raw" output
1792 sub parse_difftree_raw_line {
1793 my $line = shift;
1794 my %res;
1796 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1797 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1798 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1799 $res{'from_mode'} = $1;
1800 $res{'to_mode'} = $2;
1801 $res{'from_id'} = $3;
1802 $res{'to_id'} = $4;
1803 $res{'status'} = $5;
1804 $res{'similarity'} = $6;
1805 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1806 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1807 } else {
1808 $res{'file'} = unquote($7);
1811 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1812 # combined diff (for merge commit)
1813 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1814 $res{'nparents'} = length($1);
1815 $res{'from_mode'} = [ split(' ', $2) ];
1816 $res{'to_mode'} = pop @{$res{'from_mode'}};
1817 $res{'from_id'} = [ split(' ', $3) ];
1818 $res{'to_id'} = pop @{$res{'from_id'}};
1819 $res{'status'} = [ split('', $4) ];
1820 $res{'to_file'} = unquote($5);
1822 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1823 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1824 $res{'commit'} = $1;
1827 return wantarray ? %res : \%res;
1830 # parse line of git-ls-tree output
1831 sub parse_ls_tree_line ($;%) {
1832 my $line = shift;
1833 my %opts = @_;
1834 my %res;
1836 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1837 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1839 $res{'mode'} = $1;
1840 $res{'type'} = $2;
1841 $res{'hash'} = $3;
1842 if ($opts{'-z'}) {
1843 $res{'name'} = $4;
1844 } else {
1845 $res{'name'} = unquote($4);
1848 return wantarray ? %res : \%res;
1851 # generates _two_ hashes, references to which are passed as 2 and 3 argument
1852 sub parse_from_to_diffinfo {
1853 my ($diffinfo, $from, $to, @parents) = @_;
1855 if ($diffinfo->{'nparents'}) {
1856 # combined diff
1857 $from->{'file'} = [];
1858 $from->{'href'} = [];
1859 fill_from_file_info($diffinfo, @parents)
1860 unless exists $diffinfo->{'from_file'};
1861 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1862 $from->{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
1863 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
1864 $from->{'href'}[$i] = href(action=>"blob",
1865 hash_base=>$parents[$i],
1866 hash=>$diffinfo->{'from_id'}[$i],
1867 file_name=>$from->{'file'}[$i]);
1868 } else {
1869 $from->{'href'}[$i] = undef;
1872 } else {
1873 $from->{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
1874 if ($diffinfo->{'status'} ne "A") { # not new (added) file
1875 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
1876 hash=>$diffinfo->{'from_id'},
1877 file_name=>$from->{'file'});
1878 } else {
1879 delete $from->{'href'};
1883 $to->{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
1884 if (!is_deleted($diffinfo)) { # file exists in result
1885 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
1886 hash=>$diffinfo->{'to_id'},
1887 file_name=>$to->{'file'});
1888 } else {
1889 delete $to->{'href'};
1893 ## ......................................................................
1894 ## parse to array of hashes functions
1896 sub git_get_heads_list {
1897 my $limit = shift;
1898 my @headslist;
1900 open my $fd, '-|', git_cmd(), 'for-each-ref',
1901 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1902 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1903 'refs/heads'
1904 or return;
1905 while (my $line = <$fd>) {
1906 my %ref_item;
1908 chomp $line;
1909 my ($refinfo, $committerinfo) = split(/\0/, $line);
1910 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1911 my ($committer, $epoch, $tz) =
1912 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1913 $name =~ s!^refs/heads/!!;
1915 $ref_item{'name'} = $name;
1916 $ref_item{'id'} = $hash;
1917 $ref_item{'title'} = $title || '(no commit message)';
1918 $ref_item{'epoch'} = $epoch;
1919 if ($epoch) {
1920 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1921 } else {
1922 $ref_item{'age'} = "unknown";
1925 push @headslist, \%ref_item;
1927 close $fd;
1929 return wantarray ? @headslist : \@headslist;
1932 sub git_get_tags_list {
1933 my $limit = shift;
1934 my @tagslist;
1936 open my $fd, '-|', git_cmd(), 'for-each-ref',
1937 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1938 '--format=%(objectname) %(objecttype) %(refname) '.
1939 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1940 'refs/tags'
1941 or return;
1942 while (my $line = <$fd>) {
1943 my %ref_item;
1945 chomp $line;
1946 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1947 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1948 my ($creator, $epoch, $tz) =
1949 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1950 $name =~ s!^refs/tags/!!;
1952 $ref_item{'type'} = $type;
1953 $ref_item{'id'} = $id;
1954 $ref_item{'name'} = $name;
1955 if ($type eq "tag") {
1956 $ref_item{'subject'} = $title;
1957 $ref_item{'reftype'} = $reftype;
1958 $ref_item{'refid'} = $refid;
1959 } else {
1960 $ref_item{'reftype'} = $type;
1961 $ref_item{'refid'} = $id;
1964 if ($type eq "tag" || $type eq "commit") {
1965 $ref_item{'epoch'} = $epoch;
1966 if ($epoch) {
1967 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1968 } else {
1969 $ref_item{'age'} = "unknown";
1973 push @tagslist, \%ref_item;
1975 close $fd;
1977 return wantarray ? @tagslist : \@tagslist;
1980 ## ----------------------------------------------------------------------
1981 ## filesystem-related functions
1983 sub get_file_owner {
1984 my $path = shift;
1986 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1987 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1988 if (!defined $gcos) {
1989 return undef;
1991 my $owner = $gcos;
1992 $owner =~ s/[,;].*$//;
1993 return to_utf8($owner);
1996 ## ......................................................................
1997 ## mimetype related functions
1999 sub mimetype_guess_file {
2000 my $filename = shift;
2001 my $mimemap = shift;
2002 -r $mimemap or return undef;
2004 my %mimemap;
2005 open(MIME, $mimemap) or return undef;
2006 while (<MIME>) {
2007 next if m/^#/; # skip comments
2008 my ($mime, $exts) = split(/\t+/);
2009 if (defined $exts) {
2010 my @exts = split(/\s+/, $exts);
2011 foreach my $ext (@exts) {
2012 $mimemap{$ext} = $mime;
2016 close(MIME);
2018 $filename =~ /\.([^.]*)$/;
2019 return $mimemap{$1};
2022 sub mimetype_guess {
2023 my $filename = shift;
2024 my $mime;
2025 $filename =~ /\./ or return undef;
2027 if ($mimetypes_file) {
2028 my $file = $mimetypes_file;
2029 if ($file !~ m!^/!) { # if it is relative path
2030 # it is relative to project
2031 $file = "$projectroot/$project/$file";
2033 $mime = mimetype_guess_file($filename, $file);
2035 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2036 return $mime;
2039 sub blob_mimetype {
2040 my $fd = shift;
2041 my $filename = shift;
2043 if ($filename) {
2044 my $mime = mimetype_guess($filename);
2045 $mime and return $mime;
2048 # just in case
2049 return $default_blob_plain_mimetype unless $fd;
2051 if (-T $fd) {
2052 return 'text/plain' .
2053 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2054 } elsif (! $filename) {
2055 return 'application/octet-stream';
2056 } elsif ($filename =~ m/\.png$/i) {
2057 return 'image/png';
2058 } elsif ($filename =~ m/\.gif$/i) {
2059 return 'image/gif';
2060 } elsif ($filename =~ m/\.jpe?g$/i) {
2061 return 'image/jpeg';
2062 } else {
2063 return 'application/octet-stream';
2067 ## ======================================================================
2068 ## functions printing HTML: header, footer, error page
2070 sub git_header_html {
2071 my $status = shift || "200 OK";
2072 my $expires = shift;
2074 my $title = "$site_name";
2075 if (defined $project) {
2076 $title .= " - " . to_utf8($project);
2077 if (defined $action) {
2078 $title .= "/$action";
2079 if (defined $file_name) {
2080 $title .= " - " . esc_path($file_name);
2081 if ($action eq "tree" && $file_name !~ m|/$|) {
2082 $title .= "/";
2087 my $content_type;
2088 # require explicit support from the UA if we are to send the page as
2089 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2090 # we have to do this because MSIE sometimes globs '*/*', pretending to
2091 # support xhtml+xml but choking when it gets what it asked for.
2092 if (defined $cgi->http('HTTP_ACCEPT') &&
2093 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2094 $cgi->Accept('application/xhtml+xml') != 0) {
2095 $content_type = 'application/xhtml+xml';
2096 } else {
2097 $content_type = 'text/html';
2099 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2100 -status=> $status, -expires => $expires);
2101 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2102 print <<EOF;
2103 <?xml version="1.0" encoding="utf-8"?>
2104 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2105 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2106 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2107 <!-- git core binaries version $git_version -->
2108 <head>
2109 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2110 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2111 <meta name="robots" content="index, nofollow"/>
2112 <title>$title</title>
2114 # print out each stylesheet that exist
2115 if (defined $stylesheet) {
2116 #provides backwards capability for those people who define style sheet in a config file
2117 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2118 } else {
2119 foreach my $stylesheet (@stylesheets) {
2120 next unless $stylesheet;
2121 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2124 if (defined $project) {
2125 printf('<link rel="alternate" title="%s log RSS feed" '.
2126 'href="%s" type="application/rss+xml" />'."\n",
2127 esc_param($project), href(action=>"rss"));
2128 printf('<link rel="alternate" title="%s log Atom feed" '.
2129 'href="%s" type="application/atom+xml" />'."\n",
2130 esc_param($project), href(action=>"atom"));
2131 } else {
2132 printf('<link rel="alternate" title="%s projects list" '.
2133 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2134 $site_name, href(project=>undef, action=>"project_index"));
2135 printf('<link rel="alternate" title="%s projects feeds" '.
2136 'href="%s" type="text/x-opml"/>'."\n",
2137 $site_name, href(project=>undef, action=>"opml"));
2139 if (defined $favicon) {
2140 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2143 print "</head>\n" .
2144 "<body>\n";
2146 if (-f $site_header) {
2147 open (my $fd, $site_header);
2148 print <$fd>;
2149 close $fd;
2152 print "<div class=\"page_header\">\n" .
2153 $cgi->a({-href => esc_url($logo_url),
2154 -title => $logo_label},
2155 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2156 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2157 if (defined $project) {
2158 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2159 if (defined $action) {
2160 print " / $action";
2162 print "\n";
2164 print "</div>\n";
2166 my ($have_search) = gitweb_check_feature('search');
2167 if ((defined $project) && ($have_search)) {
2168 if (!defined $searchtext) {
2169 $searchtext = "";
2171 my $search_hash;
2172 if (defined $hash_base) {
2173 $search_hash = $hash_base;
2174 } elsif (defined $hash) {
2175 $search_hash = $hash;
2176 } else {
2177 $search_hash = "HEAD";
2179 $cgi->param("a", "search");
2180 $cgi->param("h", $search_hash);
2181 $cgi->param("p", $project);
2182 print $cgi->startform(-method => "get", -action => $my_uri) .
2183 "<div class=\"search\">\n" .
2184 $cgi->hidden(-name => "p") . "\n" .
2185 $cgi->hidden(-name => "a") . "\n" .
2186 $cgi->hidden(-name => "h") . "\n" .
2187 $cgi->popup_menu(-name => 'st', -default => 'commit',
2188 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2189 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2190 " search:\n",
2191 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2192 "</div>" .
2193 $cgi->end_form() . "\n";
2197 sub git_footer_html {
2198 print "<div class=\"page_footer\">\n";
2199 if (defined $project) {
2200 my $descr = git_get_project_description($project);
2201 if (defined $descr) {
2202 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2204 print $cgi->a({-href => href(action=>"rss"),
2205 -class => "rss_logo"}, "RSS") . " ";
2206 print $cgi->a({-href => href(action=>"atom"),
2207 -class => "rss_logo"}, "Atom") . "\n";
2208 } else {
2209 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2210 -class => "rss_logo"}, "OPML") . " ";
2211 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2212 -class => "rss_logo"}, "TXT") . "\n";
2214 print "</div>\n" ;
2216 if (-f $site_footer) {
2217 open (my $fd, $site_footer);
2218 print <$fd>;
2219 close $fd;
2222 print "</body>\n" .
2223 "</html>";
2226 sub die_error {
2227 my $status = shift || "403 Forbidden";
2228 my $error = shift || "Malformed query, file missing or permission denied";
2230 git_header_html($status);
2231 print <<EOF;
2232 <div class="page_body">
2233 <br /><br />
2234 $status - $error
2235 <br />
2236 </div>
2238 git_footer_html();
2239 exit;
2242 ## ----------------------------------------------------------------------
2243 ## functions printing or outputting HTML: navigation
2245 sub git_print_page_nav {
2246 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2247 $extra = '' if !defined $extra; # pager or formats
2249 my @navs = qw(summary shortlog log commit commitdiff tree);
2250 if ($suppress) {
2251 @navs = grep { $_ ne $suppress } @navs;
2254 my %arg = map { $_ => {action=>$_} } @navs;
2255 if (defined $head) {
2256 for (qw(commit commitdiff)) {
2257 $arg{$_}{'hash'} = $head;
2259 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2260 for (qw(shortlog log)) {
2261 $arg{$_}{'hash'} = $head;
2265 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2266 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2268 print "<div class=\"page_nav\">\n" .
2269 (join " | ",
2270 map { $_ eq $current ?
2271 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2272 } @navs);
2273 print "<br/>\n$extra<br/>\n" .
2274 "</div>\n";
2277 sub format_paging_nav {
2278 my ($action, $hash, $head, $page, $nrevs) = @_;
2279 my $paging_nav;
2282 if ($hash ne $head || $page) {
2283 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2284 } else {
2285 $paging_nav .= "HEAD";
2288 if ($page > 0) {
2289 $paging_nav .= " &sdot; " .
2290 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2291 -accesskey => "p", -title => "Alt-p"}, "prev");
2292 } else {
2293 $paging_nav .= " &sdot; prev";
2296 if ($nrevs >= (100 * ($page+1)-1)) {
2297 $paging_nav .= " &sdot; " .
2298 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2299 -accesskey => "n", -title => "Alt-n"}, "next");
2300 } else {
2301 $paging_nav .= " &sdot; next";
2304 return $paging_nav;
2307 ## ......................................................................
2308 ## functions printing or outputting HTML: div
2310 sub git_print_header_div {
2311 my ($action, $title, $hash, $hash_base) = @_;
2312 my %args = ();
2314 $args{'action'} = $action;
2315 $args{'hash'} = $hash if $hash;
2316 $args{'hash_base'} = $hash_base if $hash_base;
2318 print "<div class=\"header\">\n" .
2319 $cgi->a({-href => href(%args), -class => "title"},
2320 $title ? $title : $action) .
2321 "\n</div>\n";
2324 #sub git_print_authorship (\%) {
2325 sub git_print_authorship {
2326 my $co = shift;
2328 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2329 print "<div class=\"author_date\">" .
2330 esc_html($co->{'author_name'}) .
2331 " [$ad{'rfc2822'}";
2332 if ($ad{'hour_local'} < 6) {
2333 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2334 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2335 } else {
2336 printf(" (%02d:%02d %s)",
2337 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2339 print "]</div>\n";
2342 sub git_print_page_path {
2343 my $name = shift;
2344 my $type = shift;
2345 my $hb = shift;
2348 print "<div class=\"page_path\">";
2349 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2350 -title => 'tree root'}, to_utf8("[$project]"));
2351 print " / ";
2352 if (defined $name) {
2353 my @dirname = split '/', $name;
2354 my $basename = pop @dirname;
2355 my $fullname = '';
2357 foreach my $dir (@dirname) {
2358 $fullname .= ($fullname ? '/' : '') . $dir;
2359 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2360 hash_base=>$hb),
2361 -title => $fullname}, esc_path($dir));
2362 print " / ";
2364 if (defined $type && $type eq 'blob') {
2365 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2366 hash_base=>$hb),
2367 -title => $name}, esc_path($basename));
2368 } elsif (defined $type && $type eq 'tree') {
2369 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2370 hash_base=>$hb),
2371 -title => $name}, esc_path($basename));
2372 print " / ";
2373 } else {
2374 print esc_path($basename);
2377 print "<br/></div>\n";
2380 # sub git_print_log (\@;%) {
2381 sub git_print_log ($;%) {
2382 my $log = shift;
2383 my %opts = @_;
2385 if ($opts{'-remove_title'}) {
2386 # remove title, i.e. first line of log
2387 shift @$log;
2389 # remove leading empty lines
2390 while (defined $log->[0] && $log->[0] eq "") {
2391 shift @$log;
2394 # print log
2395 my $signoff = 0;
2396 my $empty = 0;
2397 foreach my $line (@$log) {
2398 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2399 $signoff = 1;
2400 $empty = 0;
2401 if (! $opts{'-remove_signoff'}) {
2402 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2403 next;
2404 } else {
2405 # remove signoff lines
2406 next;
2408 } else {
2409 $signoff = 0;
2412 # print only one empty line
2413 # do not print empty line after signoff
2414 if ($line eq "") {
2415 next if ($empty || $signoff);
2416 $empty = 1;
2417 } else {
2418 $empty = 0;
2421 print format_log_line_html($line) . "<br/>\n";
2424 if ($opts{'-final_empty_line'}) {
2425 # end with single empty line
2426 print "<br/>\n" unless $empty;
2430 # return link target (what link points to)
2431 sub git_get_link_target {
2432 my $hash = shift;
2433 my $link_target;
2435 # read link
2436 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2437 or return;
2439 local $/;
2440 $link_target = <$fd>;
2442 close $fd
2443 or return;
2445 return $link_target;
2448 # given link target, and the directory (basedir) the link is in,
2449 # return target of link relative to top directory (top tree);
2450 # return undef if it is not possible (including absolute links).
2451 sub normalize_link_target {
2452 my ($link_target, $basedir, $hash_base) = @_;
2454 # we can normalize symlink target only if $hash_base is provided
2455 return unless $hash_base;
2457 # absolute symlinks (beginning with '/') cannot be normalized
2458 return if (substr($link_target, 0, 1) eq '/');
2460 # normalize link target to path from top (root) tree (dir)
2461 my $path;
2462 if ($basedir) {
2463 $path = $basedir . '/' . $link_target;
2464 } else {
2465 # we are in top (root) tree (dir)
2466 $path = $link_target;
2469 # remove //, /./, and /../
2470 my @path_parts;
2471 foreach my $part (split('/', $path)) {
2472 # discard '.' and ''
2473 next if (!$part || $part eq '.');
2474 # handle '..'
2475 if ($part eq '..') {
2476 if (@path_parts) {
2477 pop @path_parts;
2478 } else {
2479 # link leads outside repository (outside top dir)
2480 return;
2482 } else {
2483 push @path_parts, $part;
2486 $path = join('/', @path_parts);
2488 return $path;
2491 # print tree entry (row of git_tree), but without encompassing <tr> element
2492 sub git_print_tree_entry {
2493 my ($t, $basedir, $hash_base, $have_blame) = @_;
2495 my %base_key = ();
2496 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2498 # The format of a table row is: mode list link. Where mode is
2499 # the mode of the entry, list is the name of the entry, an href,
2500 # and link is the action links of the entry.
2502 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2503 if ($t->{'type'} eq "blob") {
2504 print "<td class=\"list\">" .
2505 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2506 file_name=>"$basedir$t->{'name'}", %base_key),
2507 -class => "list"}, esc_path($t->{'name'}));
2508 if (S_ISLNK(oct $t->{'mode'})) {
2509 my $link_target = git_get_link_target($t->{'hash'});
2510 if ($link_target) {
2511 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2512 if (defined $norm_target) {
2513 print " -> " .
2514 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2515 file_name=>$norm_target),
2516 -title => $norm_target}, esc_path($link_target));
2517 } else {
2518 print " -> " . esc_path($link_target);
2522 print "</td>\n";
2523 print "<td class=\"link\">";
2524 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2525 file_name=>"$basedir$t->{'name'}", %base_key)},
2526 "blob");
2527 if ($have_blame) {
2528 print " | " .
2529 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2530 file_name=>"$basedir$t->{'name'}", %base_key)},
2531 "blame");
2533 if (defined $hash_base) {
2534 print " | " .
2535 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2536 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2537 "history");
2539 print " | " .
2540 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2541 file_name=>"$basedir$t->{'name'}")},
2542 "raw");
2543 print "</td>\n";
2545 } elsif ($t->{'type'} eq "tree") {
2546 print "<td class=\"list\">";
2547 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2548 file_name=>"$basedir$t->{'name'}", %base_key)},
2549 esc_path($t->{'name'}));
2550 print "</td>\n";
2551 print "<td class=\"link\">";
2552 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2553 file_name=>"$basedir$t->{'name'}", %base_key)},
2554 "tree");
2555 if (defined $hash_base) {
2556 print " | " .
2557 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2558 file_name=>"$basedir$t->{'name'}")},
2559 "history");
2561 print "</td>\n";
2565 ## ......................................................................
2566 ## functions printing large fragments of HTML
2568 sub fill_from_file_info {
2569 my ($diff, @parents) = @_;
2571 $diff->{'from_file'} = [ ];
2572 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2573 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2574 if ($diff->{'status'}[$i] eq 'R' ||
2575 $diff->{'status'}[$i] eq 'C') {
2576 $diff->{'from_file'}[$i] =
2577 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2581 return $diff;
2584 # parameters can be strings, or references to arrays of strings
2585 sub from_ids_eq {
2586 my ($a, $b) = @_;
2588 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2589 for (my $i = 0; $i < @$a; ++$i) {
2590 return 0 unless ($a->[$i] eq $b->[$i]);
2592 return 1;
2593 } elsif (!ref($a) && !ref($b)) {
2594 return $a eq $b;
2595 } else {
2596 return 0;
2600 sub is_deleted {
2601 my $diffinfo = shift;
2603 return $diffinfo->{'to_id'} eq ('0' x 40);
2606 sub git_difftree_body {
2607 my ($difftree, $hash, @parents) = @_;
2608 my ($parent) = $parents[0];
2609 my ($have_blame) = gitweb_check_feature('blame');
2610 print "<div class=\"list_head\">\n";
2611 if ($#{$difftree} > 10) {
2612 print(($#{$difftree} + 1) . " files changed:\n");
2614 print "</div>\n";
2616 print "<table class=\"" .
2617 (@parents > 1 ? "combined " : "") .
2618 "diff_tree\">\n";
2620 # header only for combined diff in 'commitdiff' view
2621 my $has_header = @parents > 1 && $action eq 'commitdiff';
2622 if ($has_header) {
2623 # table header
2624 print "<thead><tr>\n" .
2625 "<th></th><th></th>\n"; # filename, patchN link
2626 for (my $i = 0; $i < @parents; $i++) {
2627 my $par = $parents[$i];
2628 print "<th>" .
2629 $cgi->a({-href => href(action=>"commitdiff",
2630 hash=>$hash, hash_parent=>$par),
2631 -title => 'commitdiff to parent number ' .
2632 ($i+1) . ': ' . substr($par,0,7)},
2633 $i+1) .
2634 "&nbsp;</th>\n";
2636 print "</tr></thead>\n<tbody>\n";
2639 my $alternate = 1;
2640 my $patchno = 0;
2641 foreach my $line (@{$difftree}) {
2642 my $diff;
2643 if (ref($line) eq "HASH") {
2644 # pre-parsed (or generated by hand)
2645 $diff = $line;
2646 } else {
2647 $diff = parse_difftree_raw_line($line);
2650 if ($alternate) {
2651 print "<tr class=\"dark\">\n";
2652 } else {
2653 print "<tr class=\"light\">\n";
2655 $alternate ^= 1;
2657 if (exists $diff->{'nparents'}) { # combined diff
2659 fill_from_file_info($diff, @parents)
2660 unless exists $diff->{'from_file'};
2662 if (!is_deleted($diff)) {
2663 # file exists in the result (child) commit
2664 print "<td>" .
2665 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2666 file_name=>$diff->{'to_file'},
2667 hash_base=>$hash),
2668 -class => "list"}, esc_path($diff->{'to_file'})) .
2669 "</td>\n";
2670 } else {
2671 print "<td>" .
2672 esc_path($diff->{'to_file'}) .
2673 "</td>\n";
2676 if ($action eq 'commitdiff') {
2677 # link to patch
2678 $patchno++;
2679 print "<td class=\"link\">" .
2680 $cgi->a({-href => "#patch$patchno"}, "patch") .
2681 " | " .
2682 "</td>\n";
2685 my $has_history = 0;
2686 my $not_deleted = 0;
2687 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2688 my $hash_parent = $parents[$i];
2689 my $from_hash = $diff->{'from_id'}[$i];
2690 my $from_path = $diff->{'from_file'}[$i];
2691 my $status = $diff->{'status'}[$i];
2693 $has_history ||= ($status ne 'A');
2694 $not_deleted ||= ($status ne 'D');
2696 if ($status eq 'A') {
2697 print "<td class=\"link\" align=\"right\"> | </td>\n";
2698 } elsif ($status eq 'D') {
2699 print "<td class=\"link\">" .
2700 $cgi->a({-href => href(action=>"blob",
2701 hash_base=>$hash,
2702 hash=>$from_hash,
2703 file_name=>$from_path)},
2704 "blob" . ($i+1)) .
2705 " | </td>\n";
2706 } else {
2707 if ($diff->{'to_id'} eq $from_hash) {
2708 print "<td class=\"link nochange\">";
2709 } else {
2710 print "<td class=\"link\">";
2712 print $cgi->a({-href => href(action=>"blobdiff",
2713 hash=>$diff->{'to_id'},
2714 hash_parent=>$from_hash,
2715 hash_base=>$hash,
2716 hash_parent_base=>$hash_parent,
2717 file_name=>$diff->{'to_file'},
2718 file_parent=>$from_path)},
2719 "diff" . ($i+1)) .
2720 " | </td>\n";
2724 print "<td class=\"link\">";
2725 if ($not_deleted) {
2726 print $cgi->a({-href => href(action=>"blob",
2727 hash=>$diff->{'to_id'},
2728 file_name=>$diff->{'to_file'},
2729 hash_base=>$hash)},
2730 "blob");
2731 print " | " if ($has_history);
2733 if ($has_history) {
2734 print $cgi->a({-href => href(action=>"history",
2735 file_name=>$diff->{'to_file'},
2736 hash_base=>$hash)},
2737 "history");
2739 print "</td>\n";
2741 print "</tr>\n";
2742 next; # instead of 'else' clause, to avoid extra indent
2744 # else ordinary diff
2746 my ($to_mode_oct, $to_mode_str, $to_file_type);
2747 my ($from_mode_oct, $from_mode_str, $from_file_type);
2748 if ($diff->{'to_mode'} ne ('0' x 6)) {
2749 $to_mode_oct = oct $diff->{'to_mode'};
2750 if (S_ISREG($to_mode_oct)) { # only for regular file
2751 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2753 $to_file_type = file_type($diff->{'to_mode'});
2755 if ($diff->{'from_mode'} ne ('0' x 6)) {
2756 $from_mode_oct = oct $diff->{'from_mode'};
2757 if (S_ISREG($to_mode_oct)) { # only for regular file
2758 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2760 $from_file_type = file_type($diff->{'from_mode'});
2763 if ($diff->{'status'} eq "A") { # created
2764 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2765 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2766 $mode_chng .= "]</span>";
2767 print "<td>";
2768 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2769 hash_base=>$hash, file_name=>$diff->{'file'}),
2770 -class => "list"}, esc_path($diff->{'file'}));
2771 print "</td>\n";
2772 print "<td>$mode_chng</td>\n";
2773 print "<td class=\"link\">";
2774 if ($action eq 'commitdiff') {
2775 # link to patch
2776 $patchno++;
2777 print $cgi->a({-href => "#patch$patchno"}, "patch");
2778 print " | ";
2780 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2781 hash_base=>$hash, file_name=>$diff->{'file'})},
2782 "blob");
2783 print "</td>\n";
2785 } elsif ($diff->{'status'} eq "D") { # deleted
2786 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2787 print "<td>";
2788 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2789 hash_base=>$parent, file_name=>$diff->{'file'}),
2790 -class => "list"}, esc_path($diff->{'file'}));
2791 print "</td>\n";
2792 print "<td>$mode_chng</td>\n";
2793 print "<td class=\"link\">";
2794 if ($action eq 'commitdiff') {
2795 # link to patch
2796 $patchno++;
2797 print $cgi->a({-href => "#patch$patchno"}, "patch");
2798 print " | ";
2800 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2801 hash_base=>$parent, file_name=>$diff->{'file'})},
2802 "blob") . " | ";
2803 if ($have_blame) {
2804 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2805 file_name=>$diff->{'file'})},
2806 "blame") . " | ";
2808 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2809 file_name=>$diff->{'file'})},
2810 "history");
2811 print "</td>\n";
2813 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
2814 my $mode_chnge = "";
2815 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2816 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2817 if ($from_file_type ne $to_file_type) {
2818 $mode_chnge .= " from $from_file_type to $to_file_type";
2820 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2821 if ($from_mode_str && $to_mode_str) {
2822 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2823 } elsif ($to_mode_str) {
2824 $mode_chnge .= " mode: $to_mode_str";
2827 $mode_chnge .= "]</span>\n";
2829 print "<td>";
2830 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2831 hash_base=>$hash, file_name=>$diff->{'file'}),
2832 -class => "list"}, esc_path($diff->{'file'}));
2833 print "</td>\n";
2834 print "<td>$mode_chnge</td>\n";
2835 print "<td class=\"link\">";
2836 if ($action eq 'commitdiff') {
2837 # link to patch
2838 $patchno++;
2839 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2840 " | ";
2841 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2842 # "commit" view and modified file (not onlu mode changed)
2843 print $cgi->a({-href => href(action=>"blobdiff",
2844 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2845 hash_base=>$hash, hash_parent_base=>$parent,
2846 file_name=>$diff->{'file'})},
2847 "diff") .
2848 " | ";
2850 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2851 hash_base=>$hash, file_name=>$diff->{'file'})},
2852 "blob") . " | ";
2853 if ($have_blame) {
2854 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2855 file_name=>$diff->{'file'})},
2856 "blame") . " | ";
2858 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2859 file_name=>$diff->{'file'})},
2860 "history");
2861 print "</td>\n";
2863 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
2864 my %status_name = ('R' => 'moved', 'C' => 'copied');
2865 my $nstatus = $status_name{$diff->{'status'}};
2866 my $mode_chng = "";
2867 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2868 # mode also for directories, so we cannot use $to_mode_str
2869 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2871 print "<td>" .
2872 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2873 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
2874 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
2875 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2876 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2877 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
2878 -class => "list"}, esc_path($diff->{'from_file'})) .
2879 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2880 "<td class=\"link\">";
2881 if ($action eq 'commitdiff') {
2882 # link to patch
2883 $patchno++;
2884 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2885 " | ";
2886 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2887 # "commit" view and modified file (not only pure rename or copy)
2888 print $cgi->a({-href => href(action=>"blobdiff",
2889 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2890 hash_base=>$hash, hash_parent_base=>$parent,
2891 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
2892 "diff") .
2893 " | ";
2895 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2896 hash_base=>$parent, file_name=>$diff->{'to_file'})},
2897 "blob") . " | ";
2898 if ($have_blame) {
2899 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2900 file_name=>$diff->{'to_file'})},
2901 "blame") . " | ";
2903 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2904 file_name=>$diff->{'to_file'})},
2905 "history");
2906 print "</td>\n";
2908 } # we should not encounter Unmerged (U) or Unknown (X) status
2909 print "</tr>\n";
2911 print "</tbody>" if $has_header;
2912 print "</table>\n";
2915 sub git_patchset_body {
2916 my ($fd, $difftree, $hash, @hash_parents) = @_;
2917 my ($hash_parent) = $hash_parents[0];
2919 my $patch_idx = 0;
2920 my $patch_number = 0;
2921 my $patch_line;
2922 my $diffinfo;
2923 my (%from, %to);
2925 print "<div class=\"patchset\">\n";
2927 # skip to first patch
2928 while ($patch_line = <$fd>) {
2929 chomp $patch_line;
2931 last if ($patch_line =~ m/^diff /);
2934 PATCH:
2935 while ($patch_line) {
2936 my @diff_header;
2937 my ($from_id, $to_id);
2939 # git diff header
2940 #assert($patch_line =~ m/^diff /) if DEBUG;
2941 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2942 $patch_number++;
2943 push @diff_header, $patch_line;
2945 # extended diff header
2946 EXTENDED_HEADER:
2947 while ($patch_line = <$fd>) {
2948 chomp $patch_line;
2950 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
2952 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2953 $from_id = $1;
2954 $to_id = $2;
2955 } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2956 $from_id = [ split(',', $1) ];
2957 $to_id = $2;
2960 push @diff_header, $patch_line;
2962 my $last_patch_line = $patch_line;
2964 # check if current patch belong to current raw line
2965 # and parse raw git-diff line if needed
2966 if (defined $diffinfo &&
2967 defined $from_id && defined $to_id &&
2968 from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
2969 $diffinfo->{'to_id'} eq $to_id) {
2970 # this is continuation of a split patch
2971 print "<div class=\"patch cont\">\n";
2972 } else {
2973 # advance raw git-diff output if needed
2974 $patch_idx++ if defined $diffinfo;
2976 # read and prepare patch information
2977 if (ref($difftree->[$patch_idx]) eq "HASH") {
2978 # pre-parsed (or generated by hand)
2979 $diffinfo = $difftree->[$patch_idx];
2980 } else {
2981 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2983 # modifies %from, %to hashes
2984 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
2985 if ($diffinfo->{'nparents'}) {
2986 # combined diff
2987 $from{'file'} = [];
2988 $from{'href'} = [];
2989 fill_from_file_info($diffinfo, @hash_parents)
2990 unless exists $diffinfo->{'from_file'};
2991 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2992 $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2993 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2994 $from{'href'}[$i] = href(action=>"blob",
2995 hash_base=>$hash_parents[$i],
2996 hash=>$diffinfo->{'from_id'}[$i],
2997 file_name=>$from{'file'}[$i]);
2998 } else {
2999 $from{'href'}[$i] = undef;
3002 } else {
3003 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
3004 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3005 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3006 hash=>$diffinfo->{'from_id'},
3007 file_name=>$from{'file'});
3008 } else {
3009 delete $from{'href'};
3013 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
3014 if (!is_deleted($diffinfo)) { # file exists in result
3015 $to{'href'} = href(action=>"blob", hash_base=>$hash,
3016 hash=>$diffinfo->{'to_id'},
3017 file_name=>$to{'file'});
3018 } else {
3019 delete $to{'href'};
3021 # this is first patch for raw difftree line with $patch_idx index
3022 # we index @$difftree array from 0, but number patches from 1
3023 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3026 # print "git diff" header
3027 $patch_line = shift @diff_header;
3028 print format_git_diff_header_line($patch_line, $diffinfo,
3029 \%from, \%to);
3031 # print extended diff header
3032 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
3033 EXTENDED_HEADER:
3034 foreach $patch_line (@diff_header) {
3035 print format_extended_diff_header_line($patch_line, $diffinfo,
3036 \%from, \%to);
3038 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
3040 # from-file/to-file diff header
3041 $patch_line = $last_patch_line;
3042 if (! $patch_line) {
3043 print "</div>\n"; # class="patch"
3044 last PATCH;
3046 next PATCH if ($patch_line =~ m/^diff /);
3047 #assert($patch_line =~ m/^---/) if DEBUG;
3048 #assert($patch_line eq $last_patch_line) if DEBUG;
3050 $patch_line = <$fd>;
3051 chomp $patch_line;
3052 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3054 print format_diff_from_to_header($last_patch_line, $patch_line,
3055 $diffinfo, \%from, \%to,
3056 @hash_parents);
3058 # the patch itself
3059 LINE:
3060 while ($patch_line = <$fd>) {
3061 chomp $patch_line;
3063 next PATCH if ($patch_line =~ m/^diff /);
3065 print format_diff_line($patch_line, \%from, \%to);
3068 } continue {
3069 print "</div>\n"; # class="patch"
3072 if ($patch_number == 0) {
3073 if (@hash_parents > 1) {
3074 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3075 } else {
3076 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3080 print "</div>\n"; # class="patchset"
3083 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3085 sub git_project_list_body {
3086 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3088 my ($check_forks) = gitweb_check_feature('forks');
3090 my @projects;
3091 foreach my $pr (@$projlist) {
3092 my (@aa) = git_get_last_activity($pr->{'path'});
3093 unless (@aa) {
3094 next;
3096 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3097 if (!defined $pr->{'descr'}) {
3098 my $descr = git_get_project_description($pr->{'path'}) || "";
3099 $pr->{'descr_long'} = to_utf8($descr);
3100 $pr->{'descr'} = chop_str($descr, 25, 5);
3102 if (!defined $pr->{'owner'}) {
3103 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
3105 if ($check_forks) {
3106 my $pname = $pr->{'path'};
3107 if (($pname =~ s/\.git$//) &&
3108 ($pname !~ /\/$/) &&
3109 (-d "$projectroot/$pname")) {
3110 $pr->{'forks'} = "-d $projectroot/$pname";
3112 else {
3113 $pr->{'forks'} = 0;
3116 push @projects, $pr;
3119 $order ||= $default_projects_order;
3120 $from = 0 unless defined $from;
3121 $to = $#projects if (!defined $to || $#projects < $to);
3123 print "<table class=\"project_list\">\n";
3124 unless ($no_header) {
3125 print "<tr>\n";
3126 if ($check_forks) {
3127 print "<th></th>\n";
3129 if ($order eq "project") {
3130 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3131 print "<th>Project</th>\n";
3132 } else {
3133 print "<th>" .
3134 $cgi->a({-href => href(project=>undef, order=>'project'),
3135 -class => "header"}, "Project") .
3136 "</th>\n";
3138 if ($order eq "descr") {
3139 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3140 print "<th>Description</th>\n";
3141 } else {
3142 print "<th>" .
3143 $cgi->a({-href => href(project=>undef, order=>'descr'),
3144 -class => "header"}, "Description") .
3145 "</th>\n";
3147 if ($order eq "owner") {
3148 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3149 print "<th>Owner</th>\n";
3150 } else {
3151 print "<th>" .
3152 $cgi->a({-href => href(project=>undef, order=>'owner'),
3153 -class => "header"}, "Owner") .
3154 "</th>\n";
3156 if ($order eq "age") {
3157 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3158 print "<th>Last Change</th>\n";
3159 } else {
3160 print "<th>" .
3161 $cgi->a({-href => href(project=>undef, order=>'age'),
3162 -class => "header"}, "Last Change") .
3163 "</th>\n";
3165 print "<th></th>\n" .
3166 "</tr>\n";
3168 my $alternate = 1;
3169 for (my $i = $from; $i <= $to; $i++) {
3170 my $pr = $projects[$i];
3171 if ($alternate) {
3172 print "<tr class=\"dark\">\n";
3173 } else {
3174 print "<tr class=\"light\">\n";
3176 $alternate ^= 1;
3177 if ($check_forks) {
3178 print "<td>";
3179 if ($pr->{'forks'}) {
3180 print "<!-- $pr->{'forks'} -->\n";
3181 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3183 print "</td>\n";
3185 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3186 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3187 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3188 -class => "list", -title => $pr->{'descr_long'}},
3189 esc_html($pr->{'descr'})) . "</td>\n" .
3190 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
3191 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3192 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3193 "<td class=\"link\">" .
3194 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3195 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3196 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3197 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3198 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3199 "</td>\n" .
3200 "</tr>\n";
3202 if (defined $extra) {
3203 print "<tr>\n";
3204 if ($check_forks) {
3205 print "<td></td>\n";
3207 print "<td colspan=\"5\">$extra</td>\n" .
3208 "</tr>\n";
3210 print "</table>\n";
3213 sub git_shortlog_body {
3214 # uses global variable $project
3215 my ($commitlist, $from, $to, $refs, $extra) = @_;
3217 my $have_snapshot = gitweb_have_snapshot();
3219 $from = 0 unless defined $from;
3220 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3222 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3223 my $alternate = 1;
3224 for (my $i = $from; $i <= $to; $i++) {
3225 my %co = %{$commitlist->[$i]};
3226 my $commit = $co{'id'};
3227 my $ref = format_ref_marker($refs, $commit);
3228 if ($alternate) {
3229 print "<tr class=\"dark\">\n";
3230 } else {
3231 print "<tr class=\"light\">\n";
3233 $alternate ^= 1;
3234 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3235 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3236 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3237 "<td>";
3238 print format_subject_html($co{'title'}, $co{'title_short'},
3239 href(action=>"commit", hash=>$commit), $ref);
3240 print "</td>\n" .
3241 "<td class=\"link\">" .
3242 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3243 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3244 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3245 if ($have_snapshot) {
3246 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
3248 print "</td>\n" .
3249 "</tr>\n";
3251 if (defined $extra) {
3252 print "<tr>\n" .
3253 "<td colspan=\"4\">$extra</td>\n" .
3254 "</tr>\n";
3256 print "</table>\n";
3259 sub git_history_body {
3260 # Warning: assumes constant type (blob or tree) during history
3261 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3263 $from = 0 unless defined $from;
3264 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3266 print "<table class=\"history\" cellspacing=\"0\">\n";
3267 my $alternate = 1;
3268 for (my $i = $from; $i <= $to; $i++) {
3269 my %co = %{$commitlist->[$i]};
3270 if (!%co) {
3271 next;
3273 my $commit = $co{'id'};
3275 my $ref = format_ref_marker($refs, $commit);
3277 if ($alternate) {
3278 print "<tr class=\"dark\">\n";
3279 } else {
3280 print "<tr class=\"light\">\n";
3282 $alternate ^= 1;
3283 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3284 # shortlog uses chop_str($co{'author_name'}, 10)
3285 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3286 "<td>";
3287 # originally git_history used chop_str($co{'title'}, 50)
3288 print format_subject_html($co{'title'}, $co{'title_short'},
3289 href(action=>"commit", hash=>$commit), $ref);
3290 print "</td>\n" .
3291 "<td class=\"link\">" .
3292 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3293 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3295 if ($ftype eq 'blob') {
3296 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3297 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3298 if (defined $blob_current && defined $blob_parent &&
3299 $blob_current ne $blob_parent) {
3300 print " | " .
3301 $cgi->a({-href => href(action=>"blobdiff",
3302 hash=>$blob_current, hash_parent=>$blob_parent,
3303 hash_base=>$hash_base, hash_parent_base=>$commit,
3304 file_name=>$file_name)},
3305 "diff to current");
3308 print "</td>\n" .
3309 "</tr>\n";
3311 if (defined $extra) {
3312 print "<tr>\n" .
3313 "<td colspan=\"4\">$extra</td>\n" .
3314 "</tr>\n";
3316 print "</table>\n";
3319 sub git_tags_body {
3320 # uses global variable $project
3321 my ($taglist, $from, $to, $extra) = @_;
3322 $from = 0 unless defined $from;
3323 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3325 print "<table class=\"tags\" cellspacing=\"0\">\n";
3326 my $alternate = 1;
3327 for (my $i = $from; $i <= $to; $i++) {
3328 my $entry = $taglist->[$i];
3329 my %tag = %$entry;
3330 my $comment = $tag{'subject'};
3331 my $comment_short;
3332 if (defined $comment) {
3333 $comment_short = chop_str($comment, 30, 5);
3335 if ($alternate) {
3336 print "<tr class=\"dark\">\n";
3337 } else {
3338 print "<tr class=\"light\">\n";
3340 $alternate ^= 1;
3341 if (defined $tag{'age'}) {
3342 print "<td><i>$tag{'age'}</i></td>\n";
3343 } else {
3344 print "<td></td>\n";
3346 print "<td>" .
3347 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3348 -class => "list name"}, esc_html($tag{'name'})) .
3349 "</td>\n" .
3350 "<td>";
3351 if (defined $comment) {
3352 print format_subject_html($comment, $comment_short,
3353 href(action=>"tag", hash=>$tag{'id'}));
3355 print "</td>\n" .
3356 "<td class=\"selflink\">";
3357 if ($tag{'type'} eq "tag") {
3358 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3359 } else {
3360 print "&nbsp;";
3362 print "</td>\n" .
3363 "<td class=\"link\">" . " | " .
3364 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3365 if ($tag{'reftype'} eq "commit") {
3366 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3367 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3368 } elsif ($tag{'reftype'} eq "blob") {
3369 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3371 print "</td>\n" .
3372 "</tr>";
3374 if (defined $extra) {
3375 print "<tr>\n" .
3376 "<td colspan=\"5\">$extra</td>\n" .
3377 "</tr>\n";
3379 print "</table>\n";
3382 sub git_heads_body {
3383 # uses global variable $project
3384 my ($headlist, $head, $from, $to, $extra) = @_;
3385 $from = 0 unless defined $from;
3386 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3388 print "<table class=\"heads\" cellspacing=\"0\">\n";
3389 my $alternate = 1;
3390 for (my $i = $from; $i <= $to; $i++) {
3391 my $entry = $headlist->[$i];
3392 my %ref = %$entry;
3393 my $curr = $ref{'id'} eq $head;
3394 if ($alternate) {
3395 print "<tr class=\"dark\">\n";
3396 } else {
3397 print "<tr class=\"light\">\n";
3399 $alternate ^= 1;
3400 print "<td><i>$ref{'age'}</i></td>\n" .
3401 ($curr ? "<td class=\"current_head\">" : "<td>") .
3402 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3403 -class => "list name"},esc_html($ref{'name'})) .
3404 "</td>\n" .
3405 "<td class=\"link\">" .
3406 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3407 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3408 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3409 "</td>\n" .
3410 "</tr>";
3412 if (defined $extra) {
3413 print "<tr>\n" .
3414 "<td colspan=\"3\">$extra</td>\n" .
3415 "</tr>\n";
3417 print "</table>\n";
3420 sub git_search_grep_body {
3421 my ($commitlist, $from, $to, $extra) = @_;
3422 $from = 0 unless defined $from;
3423 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3425 print "<table class=\"grep\" cellspacing=\"0\">\n";
3426 my $alternate = 1;
3427 for (my $i = $from; $i <= $to; $i++) {
3428 my %co = %{$commitlist->[$i]};
3429 if (!%co) {
3430 next;
3432 my $commit = $co{'id'};
3433 if ($alternate) {
3434 print "<tr class=\"dark\">\n";
3435 } else {
3436 print "<tr class=\"light\">\n";
3438 $alternate ^= 1;
3439 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3440 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3441 "<td>" .
3442 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3443 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3444 my $comment = $co{'comment'};
3445 foreach my $line (@$comment) {
3446 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3447 my $lead = esc_html($1) || "";
3448 $lead = chop_str($lead, 30, 10);
3449 my $match = esc_html($2) || "";
3450 my $trail = esc_html($3) || "";
3451 $trail = chop_str($trail, 30, 10);
3452 my $text = "$lead<span class=\"match\">$match</span>$trail";
3453 print chop_str($text, 80, 5) . "<br/>\n";
3456 print "</td>\n" .
3457 "<td class=\"link\">" .
3458 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3459 " | " .
3460 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3461 print "</td>\n" .
3462 "</tr>\n";
3464 if (defined $extra) {
3465 print "<tr>\n" .
3466 "<td colspan=\"3\">$extra</td>\n" .
3467 "</tr>\n";
3469 print "</table>\n";
3472 ## ======================================================================
3473 ## ======================================================================
3474 ## actions
3476 sub git_project_list {
3477 my $order = $cgi->param('o');
3478 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3479 die_error(undef, "Unknown order parameter");
3482 my @list = git_get_projects_list();
3483 if (!@list) {
3484 die_error(undef, "No projects found");
3487 git_header_html();
3488 if (-f $home_text) {
3489 print "<div class=\"index_include\">\n";
3490 open (my $fd, $home_text);
3491 print <$fd>;
3492 close $fd;
3493 print "</div>\n";
3495 git_project_list_body(\@list, $order);
3496 git_footer_html();
3499 sub git_forks {
3500 my $order = $cgi->param('o');
3501 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3502 die_error(undef, "Unknown order parameter");
3505 my @list = git_get_projects_list($project);
3506 if (!@list) {
3507 die_error(undef, "No forks found");
3510 git_header_html();
3511 git_print_page_nav('','');
3512 git_print_header_div('summary', "$project forks");
3513 git_project_list_body(\@list, $order);
3514 git_footer_html();
3517 sub git_project_index {
3518 my @projects = git_get_projects_list($project);
3520 print $cgi->header(
3521 -type => 'text/plain',
3522 -charset => 'utf-8',
3523 -content_disposition => 'inline; filename="index.aux"');
3525 foreach my $pr (@projects) {
3526 if (!exists $pr->{'owner'}) {
3527 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}");
3530 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3531 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3532 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3533 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3534 $path =~ s/ /\+/g;
3535 $owner =~ s/ /\+/g;
3537 print "$path $owner\n";
3541 sub git_summary {
3542 my $descr = git_get_project_description($project) || "none";
3543 my %co = parse_commit("HEAD");
3544 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3545 my $head = $co{'id'};
3547 my $owner = git_get_project_owner($project);
3549 my $refs = git_get_references();
3550 # These get_*_list functions return one more to allow us to see if
3551 # there are more ...
3552 my @taglist = git_get_tags_list(16);
3553 my @headlist = git_get_heads_list(16);
3554 my @forklist;
3555 my ($check_forks) = gitweb_check_feature('forks');
3557 if ($check_forks) {
3558 @forklist = git_get_projects_list($project);
3561 git_header_html();
3562 git_print_page_nav('summary','', $head);
3564 print "<div class=\"title\">&nbsp;</div>\n";
3565 print "<table cellspacing=\"0\">\n" .
3566 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3567 "<tr><td>owner</td><td>$owner</td></tr>\n";
3568 if (defined $cd{'rfc2822'}) {
3569 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3572 # use per project git URL list in $projectroot/$project/cloneurl
3573 # or make project git URL from git base URL and project name
3574 my $url_tag = "URL";
3575 my @url_list = git_get_project_url_list($project);
3576 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3577 foreach my $git_url (@url_list) {
3578 next unless $git_url;
3579 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3580 $url_tag = "";
3582 print "</table>\n";
3584 if (-s "$projectroot/$project/README.html") {
3585 if (open my $fd, "$projectroot/$project/README.html") {
3586 print "<div class=\"title\">readme</div>\n";
3587 print $_ while (<$fd>);
3588 close $fd;
3592 # we need to request one more than 16 (0..15) to check if
3593 # those 16 are all
3594 my @commitlist = $head ? parse_commits($head, 17) : ();
3595 if (@commitlist) {
3596 git_print_header_div('shortlog');
3597 git_shortlog_body(\@commitlist, 0, 15, $refs,
3598 $#commitlist <= 15 ? undef :
3599 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3602 if (@taglist) {
3603 git_print_header_div('tags');
3604 git_tags_body(\@taglist, 0, 15,
3605 $#taglist <= 15 ? undef :
3606 $cgi->a({-href => href(action=>"tags")}, "..."));
3609 if (@headlist) {
3610 git_print_header_div('heads');
3611 git_heads_body(\@headlist, $head, 0, 15,
3612 $#headlist <= 15 ? undef :
3613 $cgi->a({-href => href(action=>"heads")}, "..."));
3616 if (@forklist) {
3617 git_print_header_div('forks');
3618 git_project_list_body(\@forklist, undef, 0, 15,
3619 $#forklist <= 15 ? undef :
3620 $cgi->a({-href => href(action=>"forks")}, "..."),
3621 'noheader');
3624 git_footer_html();
3627 sub git_tag {
3628 my $head = git_get_head_hash($project);
3629 git_header_html();
3630 git_print_page_nav('','', $head,undef,$head);
3631 my %tag = parse_tag($hash);
3633 if (! %tag) {
3634 die_error(undef, "Unknown tag object");
3637 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3638 print "<div class=\"title_text\">\n" .
3639 "<table cellspacing=\"0\">\n" .
3640 "<tr>\n" .
3641 "<td>object</td>\n" .
3642 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3643 $tag{'object'}) . "</td>\n" .
3644 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3645 $tag{'type'}) . "</td>\n" .
3646 "</tr>\n";
3647 if (defined($tag{'author'})) {
3648 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3649 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3650 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3651 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3652 "</td></tr>\n";
3654 print "</table>\n\n" .
3655 "</div>\n";
3656 print "<div class=\"page_body\">";
3657 my $comment = $tag{'comment'};
3658 foreach my $line (@$comment) {
3659 chomp $line;
3660 print esc_html($line, -nbsp=>1) . "<br/>\n";
3662 print "</div>\n";
3663 git_footer_html();
3666 sub git_blame2 {
3667 my $fd;
3668 my $ftype;
3670 my ($have_blame) = gitweb_check_feature('blame');
3671 if (!$have_blame) {
3672 die_error('403 Permission denied', "Permission denied");
3674 die_error('404 Not Found', "File name not defined") if (!$file_name);
3675 $hash_base ||= git_get_head_hash($project);
3676 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3677 my %co = parse_commit($hash_base)
3678 or die_error(undef, "Reading commit failed");
3679 if (!defined $hash) {
3680 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3681 or die_error(undef, "Error looking up file");
3683 $ftype = git_get_type($hash);
3684 if ($ftype !~ "blob") {
3685 die_error('400 Bad Request', "Object is not a blob");
3687 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3688 $file_name, $hash_base)
3689 or die_error(undef, "Open git-blame failed");
3690 git_header_html();
3691 my $formats_nav =
3692 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3693 "blob") .
3694 " | " .
3695 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3696 "history") .
3697 " | " .
3698 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3699 "HEAD");
3700 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3701 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3702 git_print_page_path($file_name, $ftype, $hash_base);
3703 my @rev_color = (qw(light2 dark2));
3704 my $num_colors = scalar(@rev_color);
3705 my $current_color = 0;
3706 my $last_rev;
3707 print <<HTML;
3708 <div class="page_body">
3709 <table class="blame">
3710 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3711 HTML
3712 my %metainfo = ();
3713 while (1) {
3714 $_ = <$fd>;
3715 last unless defined $_;
3716 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3717 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3718 if (!exists $metainfo{$full_rev}) {
3719 $metainfo{$full_rev} = {};
3721 my $meta = $metainfo{$full_rev};
3722 while (<$fd>) {
3723 last if (s/^\t//);
3724 if (/^(\S+) (.*)$/) {
3725 $meta->{$1} = $2;
3728 my $data = $_;
3729 chomp $data;
3730 my $rev = substr($full_rev, 0, 8);
3731 my $author = $meta->{'author'};
3732 my %date = parse_date($meta->{'author-time'},
3733 $meta->{'author-tz'});
3734 my $date = $date{'iso-tz'};
3735 if ($group_size) {
3736 $current_color = ++$current_color % $num_colors;
3738 print "<tr class=\"$rev_color[$current_color]\">\n";
3739 if ($group_size) {
3740 print "<td class=\"sha1\"";
3741 print " title=\"". esc_html($author) . ", $date\"";
3742 print " rowspan=\"$group_size\"" if ($group_size > 1);
3743 print ">";
3744 print $cgi->a({-href => href(action=>"commit",
3745 hash=>$full_rev,
3746 file_name=>$file_name)},
3747 esc_html($rev));
3748 print "</td>\n";
3750 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3751 or die_error(undef, "Open git-rev-parse failed");
3752 my $parent_commit = <$dd>;
3753 close $dd;
3754 chomp($parent_commit);
3755 my $blamed = href(action => 'blame',
3756 file_name => $meta->{'filename'},
3757 hash_base => $parent_commit);
3758 print "<td class=\"linenr\">";
3759 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3760 -id => "l$lineno",
3761 -class => "linenr" },
3762 esc_html($lineno));
3763 print "</td>";
3764 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3765 print "</tr>\n";
3767 print "</table>\n";
3768 print "</div>";
3769 close $fd
3770 or print "Reading blob failed\n";
3771 git_footer_html();
3774 sub git_blame {
3775 my $fd;
3777 my ($have_blame) = gitweb_check_feature('blame');
3778 if (!$have_blame) {
3779 die_error('403 Permission denied', "Permission denied");
3781 die_error('404 Not Found', "File name not defined") if (!$file_name);
3782 $hash_base ||= git_get_head_hash($project);
3783 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3784 my %co = parse_commit($hash_base)
3785 or die_error(undef, "Reading commit failed");
3786 if (!defined $hash) {
3787 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3788 or die_error(undef, "Error lookup file");
3790 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3791 or die_error(undef, "Open git-annotate failed");
3792 git_header_html();
3793 my $formats_nav =
3794 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3795 "blob") .
3796 " | " .
3797 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3798 "history") .
3799 " | " .
3800 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3801 "HEAD");
3802 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3803 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3804 git_print_page_path($file_name, 'blob', $hash_base);
3805 print "<div class=\"page_body\">\n";
3806 print <<HTML;
3807 <table class="blame">
3808 <tr>
3809 <th>Commit</th>
3810 <th>Age</th>
3811 <th>Author</th>
3812 <th>Line</th>
3813 <th>Data</th>
3814 </tr>
3815 HTML
3816 my @line_class = (qw(light dark));
3817 my $line_class_len = scalar (@line_class);
3818 my $line_class_num = $#line_class;
3819 while (my $line = <$fd>) {
3820 my $long_rev;
3821 my $short_rev;
3822 my $author;
3823 my $time;
3824 my $lineno;
3825 my $data;
3826 my $age;
3827 my $age_str;
3828 my $age_class;
3830 chomp $line;
3831 $line_class_num = ($line_class_num + 1) % $line_class_len;
3833 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3834 $long_rev = $1;
3835 $author = $2;
3836 $time = $3;
3837 $lineno = $4;
3838 $data = $5;
3839 } else {
3840 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3841 next;
3843 $short_rev = substr ($long_rev, 0, 8);
3844 $age = time () - $time;
3845 $age_str = age_string ($age);
3846 $age_str =~ s/ /&nbsp;/g;
3847 $age_class = age_class($age);
3848 $author = esc_html ($author);
3849 $author =~ s/ /&nbsp;/g;
3851 $data = untabify($data);
3852 $data = esc_html ($data);
3854 print <<HTML;
3855 <tr class="$line_class[$line_class_num]">
3856 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3857 <td class="$age_class">$age_str</td>
3858 <td>$author</td>
3859 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3860 <td class="pre">$data</td>
3861 </tr>
3862 HTML
3863 } # while (my $line = <$fd>)
3864 print "</table>\n\n";
3865 close $fd
3866 or print "Reading blob failed.\n";
3867 print "</div>";
3868 git_footer_html();
3871 sub git_tags {
3872 my $head = git_get_head_hash($project);
3873 git_header_html();
3874 git_print_page_nav('','', $head,undef,$head);
3875 git_print_header_div('summary', $project);
3877 my @tagslist = git_get_tags_list();
3878 if (@tagslist) {
3879 git_tags_body(\@tagslist);
3881 git_footer_html();
3884 sub git_heads {
3885 my $head = git_get_head_hash($project);
3886 git_header_html();
3887 git_print_page_nav('','', $head,undef,$head);
3888 git_print_header_div('summary', $project);
3890 my @headslist = git_get_heads_list();
3891 if (@headslist) {
3892 git_heads_body(\@headslist, $head);
3894 git_footer_html();
3897 sub git_blob_plain {
3898 my $expires;
3900 if (!defined $hash) {
3901 if (defined $file_name) {
3902 my $base = $hash_base || git_get_head_hash($project);
3903 $hash = git_get_hash_by_path($base, $file_name, "blob")
3904 or die_error(undef, "Error lookup file");
3905 } else {
3906 die_error(undef, "No file name defined");
3908 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3909 # blobs defined by non-textual hash id's can be cached
3910 $expires = "+1d";
3913 my $type = shift;
3914 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3915 or die_error(undef, "Couldn't cat $file_name, $hash");
3917 $type ||= blob_mimetype($fd, $file_name);
3919 # save as filename, even when no $file_name is given
3920 my $save_as = "$hash";
3921 if (defined $file_name) {
3922 $save_as = $file_name;
3923 } elsif ($type =~ m/^text\//) {
3924 $save_as .= '.txt';
3927 print $cgi->header(
3928 -type => "$type",
3929 -expires=>$expires,
3930 -content_disposition => 'inline; filename="' . "$save_as" . '"');
3931 undef $/;
3932 binmode STDOUT, ':raw';
3933 print <$fd>;
3934 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3935 $/ = "\n";
3936 close $fd;
3939 sub git_blob {
3940 my $expires;
3942 if (!defined $hash) {
3943 if (defined $file_name) {
3944 my $base = $hash_base || git_get_head_hash($project);
3945 $hash = git_get_hash_by_path($base, $file_name, "blob")
3946 or die_error(undef, "Error lookup file");
3947 } else {
3948 die_error(undef, "No file name defined");
3950 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3951 # blobs defined by non-textual hash id's can be cached
3952 $expires = "+1d";
3955 my ($have_blame) = gitweb_check_feature('blame');
3956 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3957 or die_error(undef, "Couldn't cat $file_name, $hash");
3958 my $mimetype = blob_mimetype($fd, $file_name);
3959 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
3960 close $fd;
3961 return git_blob_plain($mimetype);
3963 # we can have blame only for text/* mimetype
3964 $have_blame &&= ($mimetype =~ m!^text/!);
3966 git_header_html(undef, $expires);
3967 my $formats_nav = '';
3968 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3969 if (defined $file_name) {
3970 if ($have_blame) {
3971 $formats_nav .=
3972 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3973 hash=>$hash, file_name=>$file_name)},
3974 "blame") .
3975 " | ";
3977 $formats_nav .=
3978 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3979 hash=>$hash, file_name=>$file_name)},
3980 "history") .
3981 " | " .
3982 $cgi->a({-href => href(action=>"blob_plain",
3983 hash=>$hash, file_name=>$file_name)},
3984 "raw") .
3985 " | " .
3986 $cgi->a({-href => href(action=>"blob",
3987 hash_base=>"HEAD", file_name=>$file_name)},
3988 "HEAD");
3989 } else {
3990 $formats_nav .=
3991 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3993 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3994 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3995 } else {
3996 print "<div class=\"page_nav\">\n" .
3997 "<br/><br/></div>\n" .
3998 "<div class=\"title\">$hash</div>\n";
4000 git_print_page_path($file_name, "blob", $hash_base);
4001 print "<div class=\"page_body\">\n";
4002 if ($mimetype =~ m!^text/!) {
4003 my $nr;
4004 while (my $line = <$fd>) {
4005 chomp $line;
4006 $nr++;
4007 $line = untabify($line);
4008 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4009 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4011 } elsif ($mimetype =~ m!^image/!) {
4012 print qq!<img type="$mimetype"!;
4013 if ($file_name) {
4014 print qq! alt="$file_name" title="$file_name"!;
4016 print qq! src="! .
4017 href(action=>"blob_plain", hash=>$hash,
4018 hash_base=>$hash_base, file_name=>$file_name) .
4019 qq!" />\n!;
4021 close $fd
4022 or print "Reading blob failed.\n";
4023 print "</div>";
4024 git_footer_html();
4027 sub git_tree {
4028 my $have_snapshot = gitweb_have_snapshot();
4030 if (!defined $hash_base) {
4031 $hash_base = "HEAD";
4033 if (!defined $hash) {
4034 if (defined $file_name) {
4035 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4036 } else {
4037 $hash = $hash_base;
4040 $/ = "\0";
4041 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4042 or die_error(undef, "Open git-ls-tree failed");
4043 my @entries = map { chomp; $_ } <$fd>;
4044 close $fd or die_error(undef, "Reading tree failed");
4045 $/ = "\n";
4047 my $refs = git_get_references();
4048 my $ref = format_ref_marker($refs, $hash_base);
4049 git_header_html();
4050 my $basedir = '';
4051 my ($have_blame) = gitweb_check_feature('blame');
4052 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4053 my @views_nav = ();
4054 if (defined $file_name) {
4055 push @views_nav,
4056 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4057 hash=>$hash, file_name=>$file_name)},
4058 "history"),
4059 $cgi->a({-href => href(action=>"tree",
4060 hash_base=>"HEAD", file_name=>$file_name)},
4061 "HEAD"),
4063 if ($have_snapshot) {
4064 # FIXME: Should be available when we have no hash base as well.
4065 push @views_nav,
4066 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
4067 "snapshot");
4069 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4070 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4071 } else {
4072 undef $hash_base;
4073 print "<div class=\"page_nav\">\n";
4074 print "<br/><br/></div>\n";
4075 print "<div class=\"title\">$hash</div>\n";
4077 if (defined $file_name) {
4078 $basedir = $file_name;
4079 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4080 $basedir .= '/';
4083 git_print_page_path($file_name, 'tree', $hash_base);
4084 print "<div class=\"page_body\">\n";
4085 print "<table cellspacing=\"0\">\n";
4086 my $alternate = 1;
4087 # '..' (top directory) link if possible
4088 if (defined $hash_base &&
4089 defined $file_name && $file_name =~ m![^/]+$!) {
4090 if ($alternate) {
4091 print "<tr class=\"dark\">\n";
4092 } else {
4093 print "<tr class=\"light\">\n";
4095 $alternate ^= 1;
4097 my $up = $file_name;
4098 $up =~ s!/?[^/]+$!!;
4099 undef $up unless $up;
4100 # based on git_print_tree_entry
4101 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4102 print '<td class="list">';
4103 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4104 file_name=>$up)},
4105 "..");
4106 print "</td>\n";
4107 print "<td class=\"link\"></td>\n";
4109 print "</tr>\n";
4111 foreach my $line (@entries) {
4112 my %t = parse_ls_tree_line($line, -z => 1);
4114 if ($alternate) {
4115 print "<tr class=\"dark\">\n";
4116 } else {
4117 print "<tr class=\"light\">\n";
4119 $alternate ^= 1;
4121 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4123 print "</tr>\n";
4125 print "</table>\n" .
4126 "</div>";
4127 git_footer_html();
4130 sub git_snapshot {
4131 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
4132 my $have_snapshot = (defined $ctype && defined $suffix);
4133 if (!$have_snapshot) {
4134 die_error('403 Permission denied', "Permission denied");
4137 if (!defined $hash) {
4138 $hash = git_get_head_hash($project);
4141 my $git = git_cmd_str();
4142 my $name = $project;
4143 $name =~ s/\047/\047\\\047\047/g;
4144 my $filename = to_utf8(basename($project));
4145 my $cmd;
4146 if ($suffix eq 'zip') {
4147 $filename .= "-$hash.$suffix";
4148 $cmd = "$git archive --format=zip --prefix=\'$name\'/ $hash";
4149 } else {
4150 $filename .= "-$hash.tar.$suffix";
4151 $cmd = "$git archive --format=tar --prefix=\'$name\'/ $hash | $command";
4154 print $cgi->header(
4155 -type => "application/$ctype",
4156 -content_disposition => 'inline; filename="' . "$filename" . '"',
4157 -status => '200 OK');
4159 open my $fd, "-|", $cmd
4160 or die_error(undef, "Execute git-archive failed");
4161 binmode STDOUT, ':raw';
4162 print <$fd>;
4163 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4164 close $fd;
4168 sub git_log {
4169 my $head = git_get_head_hash($project);
4170 if (!defined $hash) {
4171 $hash = $head;
4173 if (!defined $page) {
4174 $page = 0;
4176 my $refs = git_get_references();
4178 my @commitlist = parse_commits($hash, 101, (100 * $page));
4180 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
4182 git_header_html();
4183 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4185 if (!@commitlist) {
4186 my %co = parse_commit($hash);
4188 git_print_header_div('summary', $project);
4189 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4191 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4192 for (my $i = 0; $i <= $to; $i++) {
4193 my %co = %{$commitlist[$i]};
4194 next if !%co;
4195 my $commit = $co{'id'};
4196 my $ref = format_ref_marker($refs, $commit);
4197 my %ad = parse_date($co{'author_epoch'});
4198 git_print_header_div('commit',
4199 "<span class=\"age\">$co{'age_string'}</span>" .
4200 esc_html($co{'title'}) . $ref,
4201 $commit);
4202 print "<div class=\"title_text\">\n" .
4203 "<div class=\"log_link\">\n" .
4204 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4205 " | " .
4206 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4207 " | " .
4208 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4209 "<br/>\n" .
4210 "</div>\n" .
4211 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4212 "</div>\n";
4214 print "<div class=\"log_body\">\n";
4215 git_print_log($co{'comment'}, -final_empty_line=> 1);
4216 print "</div>\n";
4218 if ($#commitlist >= 100) {
4219 print "<div class=\"page_nav\">\n";
4220 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4221 -accesskey => "n", -title => "Alt-n"}, "next");
4222 print "</div>\n";
4224 git_footer_html();
4227 sub git_commit {
4228 $hash ||= $hash_base || "HEAD";
4229 my %co = parse_commit($hash);
4230 if (!%co) {
4231 die_error(undef, "Unknown commit object");
4233 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4234 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4236 my $parent = $co{'parent'};
4237 my $parents = $co{'parents'}; # listref
4239 # we need to prepare $formats_nav before any parameter munging
4240 my $formats_nav;
4241 if (!defined $parent) {
4242 # --root commitdiff
4243 $formats_nav .= '(initial)';
4244 } elsif (@$parents == 1) {
4245 # single parent commit
4246 $formats_nav .=
4247 '(parent: ' .
4248 $cgi->a({-href => href(action=>"commit",
4249 hash=>$parent)},
4250 esc_html(substr($parent, 0, 7))) .
4251 ')';
4252 } else {
4253 # merge commit
4254 $formats_nav .=
4255 '(merge: ' .
4256 join(' ', map {
4257 $cgi->a({-href => href(action=>"commit",
4258 hash=>$_)},
4259 esc_html(substr($_, 0, 7)));
4260 } @$parents ) .
4261 ')';
4264 if (!defined $parent) {
4265 $parent = "--root";
4267 my @difftree;
4268 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4269 @diff_opts,
4270 (@$parents <= 1 ? $parent : '-c'),
4271 $hash, "--"
4272 or die_error(undef, "Open git-diff-tree failed");
4273 @difftree = map { chomp; $_ } <$fd>;
4274 close $fd or die_error(undef, "Reading git-diff-tree failed");
4276 # non-textual hash id's can be cached
4277 my $expires;
4278 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4279 $expires = "+1d";
4281 my $refs = git_get_references();
4282 my $ref = format_ref_marker($refs, $co{'id'});
4284 my $have_snapshot = gitweb_have_snapshot();
4286 git_header_html(undef, $expires);
4287 git_print_page_nav('commit', '',
4288 $hash, $co{'tree'}, $hash,
4289 $formats_nav);
4291 if (defined $co{'parent'}) {
4292 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4293 } else {
4294 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4296 print "<div class=\"title_text\">\n" .
4297 "<table cellspacing=\"0\">\n";
4298 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4299 "<tr>" .
4300 "<td></td><td> $ad{'rfc2822'}";
4301 if ($ad{'hour_local'} < 6) {
4302 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4303 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4304 } else {
4305 printf(" (%02d:%02d %s)",
4306 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4308 print "</td>" .
4309 "</tr>\n";
4310 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4311 print "<tr><td></td><td> $cd{'rfc2822'}" .
4312 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4313 "</td></tr>\n";
4314 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4315 print "<tr>" .
4316 "<td>tree</td>" .
4317 "<td class=\"sha1\">" .
4318 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4319 class => "list"}, $co{'tree'}) .
4320 "</td>" .
4321 "<td class=\"link\">" .
4322 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4323 "tree");
4324 if ($have_snapshot) {
4325 print " | " .
4326 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
4328 print "</td>" .
4329 "</tr>\n";
4331 foreach my $par (@$parents) {
4332 print "<tr>" .
4333 "<td>parent</td>" .
4334 "<td class=\"sha1\">" .
4335 $cgi->a({-href => href(action=>"commit", hash=>$par),
4336 class => "list"}, $par) .
4337 "</td>" .
4338 "<td class=\"link\">" .
4339 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4340 " | " .
4341 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4342 "</td>" .
4343 "</tr>\n";
4345 print "</table>".
4346 "</div>\n";
4348 print "<div class=\"page_body\">\n";
4349 git_print_log($co{'comment'});
4350 print "</div>\n";
4352 git_difftree_body(\@difftree, $hash, @$parents);
4354 git_footer_html();
4357 sub git_object {
4358 # object is defined by:
4359 # - hash or hash_base alone
4360 # - hash_base and file_name
4361 my $type;
4363 # - hash or hash_base alone
4364 if ($hash || ($hash_base && !defined $file_name)) {
4365 my $object_id = $hash || $hash_base;
4367 my $git_command = git_cmd_str();
4368 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4369 or die_error('404 Not Found', "Object does not exist");
4370 $type = <$fd>;
4371 chomp $type;
4372 close $fd
4373 or die_error('404 Not Found', "Object does not exist");
4375 # - hash_base and file_name
4376 } elsif ($hash_base && defined $file_name) {
4377 $file_name =~ s,/+$,,;
4379 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4380 or die_error('404 Not Found', "Base object does not exist");
4382 # here errors should not hapen
4383 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4384 or die_error(undef, "Open git-ls-tree failed");
4385 my $line = <$fd>;
4386 close $fd;
4388 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4389 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4390 die_error('404 Not Found', "File or directory for given base does not exist");
4392 $type = $2;
4393 $hash = $3;
4394 } else {
4395 die_error('404 Not Found', "Not enough information to find object");
4398 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4399 hash=>$hash, hash_base=>$hash_base,
4400 file_name=>$file_name),
4401 -status => '302 Found');
4404 sub git_blobdiff {
4405 my $format = shift || 'html';
4407 my $fd;
4408 my @difftree;
4409 my %diffinfo;
4410 my $expires;
4412 # preparing $fd and %diffinfo for git_patchset_body
4413 # new style URI
4414 if (defined $hash_base && defined $hash_parent_base) {
4415 if (defined $file_name) {
4416 # read raw output
4417 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4418 $hash_parent_base, $hash_base,
4419 "--", (defined $file_parent ? $file_parent : ()), $file_name
4420 or die_error(undef, "Open git-diff-tree failed");
4421 @difftree = map { chomp; $_ } <$fd>;
4422 close $fd
4423 or die_error(undef, "Reading git-diff-tree failed");
4424 @difftree
4425 or die_error('404 Not Found', "Blob diff not found");
4427 } elsif (defined $hash &&
4428 $hash =~ /[0-9a-fA-F]{40}/) {
4429 # try to find filename from $hash
4431 # read filtered raw output
4432 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4433 $hash_parent_base, $hash_base, "--"
4434 or die_error(undef, "Open git-diff-tree failed");
4435 @difftree =
4436 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4437 # $hash == to_id
4438 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4439 map { chomp; $_ } <$fd>;
4440 close $fd
4441 or die_error(undef, "Reading git-diff-tree failed");
4442 @difftree
4443 or die_error('404 Not Found', "Blob diff not found");
4445 } else {
4446 die_error('404 Not Found', "Missing one of the blob diff parameters");
4449 if (@difftree > 1) {
4450 die_error('404 Not Found', "Ambiguous blob diff specification");
4453 %diffinfo = parse_difftree_raw_line($difftree[0]);
4454 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4455 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4457 $hash_parent ||= $diffinfo{'from_id'};
4458 $hash ||= $diffinfo{'to_id'};
4460 # non-textual hash id's can be cached
4461 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4462 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4463 $expires = '+1d';
4466 # open patch output
4467 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4468 '-p', ($format eq 'html' ? "--full-index" : ()),
4469 $hash_parent_base, $hash_base,
4470 "--", (defined $file_parent ? $file_parent : ()), $file_name
4471 or die_error(undef, "Open git-diff-tree failed");
4474 # old/legacy style URI
4475 if (!%diffinfo && # if new style URI failed
4476 defined $hash && defined $hash_parent) {
4477 # fake git-diff-tree raw output
4478 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4479 $diffinfo{'from_id'} = $hash_parent;
4480 $diffinfo{'to_id'} = $hash;
4481 if (defined $file_name) {
4482 if (defined $file_parent) {
4483 $diffinfo{'status'} = '2';
4484 $diffinfo{'from_file'} = $file_parent;
4485 $diffinfo{'to_file'} = $file_name;
4486 } else { # assume not renamed
4487 $diffinfo{'status'} = '1';
4488 $diffinfo{'from_file'} = $file_name;
4489 $diffinfo{'to_file'} = $file_name;
4491 } else { # no filename given
4492 $diffinfo{'status'} = '2';
4493 $diffinfo{'from_file'} = $hash_parent;
4494 $diffinfo{'to_file'} = $hash;
4497 # non-textual hash id's can be cached
4498 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4499 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4500 $expires = '+1d';
4503 # open patch output
4504 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4505 '-p', ($format eq 'html' ? "--full-index" : ()),
4506 $hash_parent, $hash, "--"
4507 or die_error(undef, "Open git-diff failed");
4508 } else {
4509 die_error('404 Not Found', "Missing one of the blob diff parameters")
4510 unless %diffinfo;
4513 # header
4514 if ($format eq 'html') {
4515 my $formats_nav =
4516 $cgi->a({-href => href(action=>"blobdiff_plain",
4517 hash=>$hash, hash_parent=>$hash_parent,
4518 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4519 file_name=>$file_name, file_parent=>$file_parent)},
4520 "raw");
4521 git_header_html(undef, $expires);
4522 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4523 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4524 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4525 } else {
4526 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4527 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4529 if (defined $file_name) {
4530 git_print_page_path($file_name, "blob", $hash_base);
4531 } else {
4532 print "<div class=\"page_path\"></div>\n";
4535 } elsif ($format eq 'plain') {
4536 print $cgi->header(
4537 -type => 'text/plain',
4538 -charset => 'utf-8',
4539 -expires => $expires,
4540 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4542 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4544 } else {
4545 die_error(undef, "Unknown blobdiff format");
4548 # patch
4549 if ($format eq 'html') {
4550 print "<div class=\"page_body\">\n";
4552 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4553 close $fd;
4555 print "</div>\n"; # class="page_body"
4556 git_footer_html();
4558 } else {
4559 while (my $line = <$fd>) {
4560 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4561 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4563 print $line;
4565 last if $line =~ m!^\+\+\+!;
4567 local $/ = undef;
4568 print <$fd>;
4569 close $fd;
4573 sub git_blobdiff_plain {
4574 git_blobdiff('plain');
4577 sub git_commitdiff {
4578 my $format = shift || 'html';
4579 $hash ||= $hash_base || "HEAD";
4580 my %co = parse_commit($hash);
4581 if (!%co) {
4582 die_error(undef, "Unknown commit object");
4585 # we need to prepare $formats_nav before any parameter munging
4586 my $formats_nav;
4587 if ($format eq 'html') {
4588 $formats_nav =
4589 $cgi->a({-href => href(action=>"commitdiff_plain",
4590 hash=>$hash, hash_parent=>$hash_parent)},
4591 "raw");
4593 if (defined $hash_parent) {
4594 # commitdiff with two commits given
4595 my $hash_parent_short = $hash_parent;
4596 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4597 $hash_parent_short = substr($hash_parent, 0, 7);
4599 $formats_nav .=
4600 ' (from';
4601 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4602 if ($co{'parents'}[$i] eq $hash_parent) {
4603 $formats_nav .= ' parent ' . ($i+1);
4604 last;
4607 $formats_nav .= ': ' .
4608 $cgi->a({-href => href(action=>"commitdiff",
4609 hash=>$hash_parent)},
4610 esc_html($hash_parent_short)) .
4611 ')';
4612 } elsif (!$co{'parent'}) {
4613 # --root commitdiff
4614 $formats_nav .= ' (initial)';
4615 } elsif (scalar @{$co{'parents'}} == 1) {
4616 # single parent commit
4617 $formats_nav .=
4618 ' (parent: ' .
4619 $cgi->a({-href => href(action=>"commitdiff",
4620 hash=>$co{'parent'})},
4621 esc_html(substr($co{'parent'}, 0, 7))) .
4622 ')';
4623 } else {
4624 # merge commit
4625 $formats_nav .=
4626 ' (merge: ' .
4627 join(' ', map {
4628 $cgi->a({-href => href(action=>"commitdiff",
4629 hash=>$_)},
4630 esc_html(substr($_, 0, 7)));
4631 } @{$co{'parents'}} ) .
4632 ')';
4636 my $hash_parent_param = $hash_parent;
4637 if (!defined $hash_parent) {
4638 $hash_parent_param =
4639 @{$co{'parents'}} > 1 ? '-c' : $co{'parent'} || '--root';
4642 # read commitdiff
4643 my $fd;
4644 my @difftree;
4645 if ($format eq 'html') {
4646 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4647 "--no-commit-id", "--patch-with-raw", "--full-index",
4648 $hash_parent_param, $hash, "--"
4649 or die_error(undef, "Open git-diff-tree failed");
4651 while (my $line = <$fd>) {
4652 chomp $line;
4653 # empty line ends raw part of diff-tree output
4654 last unless $line;
4655 push @difftree, scalar parse_difftree_raw_line($line);
4658 } elsif ($format eq 'plain') {
4659 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4660 '-p', $hash_parent_param, $hash, "--"
4661 or die_error(undef, "Open git-diff-tree failed");
4663 } else {
4664 die_error(undef, "Unknown commitdiff format");
4667 # non-textual hash id's can be cached
4668 my $expires;
4669 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4670 $expires = "+1d";
4673 # write commit message
4674 if ($format eq 'html') {
4675 my $refs = git_get_references();
4676 my $ref = format_ref_marker($refs, $co{'id'});
4678 git_header_html(undef, $expires);
4679 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4680 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4681 git_print_authorship(\%co);
4682 print "<div class=\"page_body\">\n";
4683 if (@{$co{'comment'}} > 1) {
4684 print "<div class=\"log\">\n";
4685 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4686 print "</div>\n"; # class="log"
4689 } elsif ($format eq 'plain') {
4690 my $refs = git_get_references("tags");
4691 my $tagname = git_get_rev_name_tags($hash);
4692 my $filename = basename($project) . "-$hash.patch";
4694 print $cgi->header(
4695 -type => 'text/plain',
4696 -charset => 'utf-8',
4697 -expires => $expires,
4698 -content_disposition => 'inline; filename="' . "$filename" . '"');
4699 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4700 print <<TEXT;
4701 From: $co{'author'}
4702 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4703 Subject: $co{'title'}
4704 TEXT
4705 print "X-Git-Tag: $tagname\n" if $tagname;
4706 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4708 foreach my $line (@{$co{'comment'}}) {
4709 print "$line\n";
4711 print "---\n\n";
4714 # write patch
4715 if ($format eq 'html') {
4716 git_difftree_body(\@difftree, $hash, $hash_parent || @{$co{'parents'}});
4717 print "<br/>\n";
4719 git_patchset_body($fd, \@difftree, $hash, $hash_parent || @{$co{'parents'}});
4720 close $fd;
4721 print "</div>\n"; # class="page_body"
4722 git_footer_html();
4724 } elsif ($format eq 'plain') {
4725 local $/ = undef;
4726 print <$fd>;
4727 close $fd
4728 or print "Reading git-diff-tree failed\n";
4732 sub git_commitdiff_plain {
4733 git_commitdiff('plain');
4736 sub git_history {
4737 if (!defined $hash_base) {
4738 $hash_base = git_get_head_hash($project);
4740 if (!defined $page) {
4741 $page = 0;
4743 my $ftype;
4744 my %co = parse_commit($hash_base);
4745 if (!%co) {
4746 die_error(undef, "Unknown commit object");
4749 my $refs = git_get_references();
4750 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4752 if (!defined $hash && defined $file_name) {
4753 $hash = git_get_hash_by_path($hash_base, $file_name);
4755 if (defined $hash) {
4756 $ftype = git_get_type($hash);
4759 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4761 my $paging_nav = '';
4762 if ($page > 0) {
4763 $paging_nav .=
4764 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4765 file_name=>$file_name)},
4766 "first");
4767 $paging_nav .= " &sdot; " .
4768 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4769 file_name=>$file_name, page=>$page-1),
4770 -accesskey => "p", -title => "Alt-p"}, "prev");
4771 } else {
4772 $paging_nav .= "first";
4773 $paging_nav .= " &sdot; prev";
4775 if ($#commitlist >= 100) {
4776 $paging_nav .= " &sdot; " .
4777 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4778 file_name=>$file_name, page=>$page+1),
4779 -accesskey => "n", -title => "Alt-n"}, "next");
4780 } else {
4781 $paging_nav .= " &sdot; next";
4783 my $next_link = '';
4784 if ($#commitlist >= 100) {
4785 $next_link =
4786 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4787 file_name=>$file_name, page=>$page+1),
4788 -accesskey => "n", -title => "Alt-n"}, "next");
4791 git_header_html();
4792 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4793 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4794 git_print_page_path($file_name, $ftype, $hash_base);
4796 git_history_body(\@commitlist, 0, 99,
4797 $refs, $hash_base, $ftype, $next_link);
4799 git_footer_html();
4802 sub git_search {
4803 my ($have_search) = gitweb_check_feature('search');
4804 if (!$have_search) {
4805 die_error('403 Permission denied', "Permission denied");
4807 if (!defined $searchtext) {
4808 die_error(undef, "Text field empty");
4810 if (!defined $hash) {
4811 $hash = git_get_head_hash($project);
4813 my %co = parse_commit($hash);
4814 if (!%co) {
4815 die_error(undef, "Unknown commit object");
4817 if (!defined $page) {
4818 $page = 0;
4821 $searchtype ||= 'commit';
4822 if ($searchtype eq 'pickaxe') {
4823 # pickaxe may take all resources of your box and run for several minutes
4824 # with every query - so decide by yourself how public you make this feature
4825 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4826 if (!$have_pickaxe) {
4827 die_error('403 Permission denied', "Permission denied");
4830 if ($searchtype eq 'grep') {
4831 my ($have_grep) = gitweb_check_feature('grep');
4832 if (!$have_grep) {
4833 die_error('403 Permission denied', "Permission denied");
4837 git_header_html();
4839 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4840 my $greptype;
4841 if ($searchtype eq 'commit') {
4842 $greptype = "--grep=";
4843 } elsif ($searchtype eq 'author') {
4844 $greptype = "--author=";
4845 } elsif ($searchtype eq 'committer') {
4846 $greptype = "--committer=";
4848 $greptype .= $search_regexp;
4849 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
4851 my $paging_nav = '';
4852 if ($page > 0) {
4853 $paging_nav .=
4854 $cgi->a({-href => href(action=>"search", hash=>$hash,
4855 searchtext=>$searchtext, searchtype=>$searchtype)},
4856 "first");
4857 $paging_nav .= " &sdot; " .
4858 $cgi->a({-href => href(action=>"search", hash=>$hash,
4859 searchtext=>$searchtext, searchtype=>$searchtype,
4860 page=>$page-1),
4861 -accesskey => "p", -title => "Alt-p"}, "prev");
4862 } else {
4863 $paging_nav .= "first";
4864 $paging_nav .= " &sdot; prev";
4866 if ($#commitlist >= 100) {
4867 $paging_nav .= " &sdot; " .
4868 $cgi->a({-href => href(action=>"search", hash=>$hash,
4869 searchtext=>$searchtext, searchtype=>$searchtype,
4870 page=>$page+1),
4871 -accesskey => "n", -title => "Alt-n"}, "next");
4872 } else {
4873 $paging_nav .= " &sdot; next";
4875 my $next_link = '';
4876 if ($#commitlist >= 100) {
4877 $next_link =
4878 $cgi->a({-href => href(action=>"search", hash=>$hash,
4879 searchtext=>$searchtext, searchtype=>$searchtype,
4880 page=>$page+1),
4881 -accesskey => "n", -title => "Alt-n"}, "next");
4884 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
4885 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4886 git_search_grep_body(\@commitlist, 0, 99, $next_link);
4889 if ($searchtype eq 'pickaxe') {
4890 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4891 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4893 print "<table cellspacing=\"0\">\n";
4894 my $alternate = 1;
4895 $/ = "\n";
4896 my $git_command = git_cmd_str();
4897 my $searchqtext = $searchtext;
4898 $searchqtext =~ s/'/'\\''/;
4899 open my $fd, "-|", "$git_command rev-list $hash | " .
4900 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
4901 undef %co;
4902 my @files;
4903 while (my $line = <$fd>) {
4904 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
4905 my %set;
4906 $set{'file'} = $6;
4907 $set{'from_id'} = $3;
4908 $set{'to_id'} = $4;
4909 $set{'id'} = $set{'to_id'};
4910 if ($set{'id'} =~ m/0{40}/) {
4911 $set{'id'} = $set{'from_id'};
4913 if ($set{'id'} =~ m/0{40}/) {
4914 next;
4916 push @files, \%set;
4917 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
4918 if (%co) {
4919 if ($alternate) {
4920 print "<tr class=\"dark\">\n";
4921 } else {
4922 print "<tr class=\"light\">\n";
4924 $alternate ^= 1;
4925 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4926 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4927 "<td>" .
4928 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4929 -class => "list subject"},
4930 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4931 while (my $setref = shift @files) {
4932 my %set = %$setref;
4933 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
4934 hash=>$set{'id'}, file_name=>$set{'file'}),
4935 -class => "list"},
4936 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
4937 "<br/>\n";
4939 print "</td>\n" .
4940 "<td class=\"link\">" .
4941 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4942 " | " .
4943 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4944 print "</td>\n" .
4945 "</tr>\n";
4947 %co = parse_commit($1);
4950 close $fd;
4952 print "</table>\n";
4955 if ($searchtype eq 'grep') {
4956 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4957 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4959 print "<table cellspacing=\"0\">\n";
4960 my $alternate = 1;
4961 my $matches = 0;
4962 $/ = "\n";
4963 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
4964 my $lastfile = '';
4965 while (my $line = <$fd>) {
4966 chomp $line;
4967 my ($file, $lno, $ltext, $binary);
4968 last if ($matches++ > 1000);
4969 if ($line =~ /^Binary file (.+) matches$/) {
4970 $file = $1;
4971 $binary = 1;
4972 } else {
4973 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
4975 if ($file ne $lastfile) {
4976 $lastfile and print "</td></tr>\n";
4977 if ($alternate++) {
4978 print "<tr class=\"dark\">\n";
4979 } else {
4980 print "<tr class=\"light\">\n";
4982 print "<td class=\"list\">".
4983 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
4984 file_name=>"$file"),
4985 -class => "list"}, esc_path($file));
4986 print "</td><td>\n";
4987 $lastfile = $file;
4989 if ($binary) {
4990 print "<div class=\"binary\">Binary file</div>\n";
4991 } else {
4992 $ltext = untabify($ltext);
4993 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
4994 $ltext = esc_html($1, -nbsp=>1);
4995 $ltext .= '<span class="match">';
4996 $ltext .= esc_html($2, -nbsp=>1);
4997 $ltext .= '</span>';
4998 $ltext .= esc_html($3, -nbsp=>1);
4999 } else {
5000 $ltext = esc_html($ltext, -nbsp=>1);
5002 print "<div class=\"pre\">" .
5003 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5004 file_name=>"$file").'#l'.$lno,
5005 -class => "linenr"}, sprintf('%4i', $lno))
5006 . ' ' . $ltext . "</div>\n";
5009 if ($lastfile) {
5010 print "</td></tr>\n";
5011 if ($matches > 1000) {
5012 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5014 } else {
5015 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5017 close $fd;
5019 print "</table>\n";
5021 git_footer_html();
5024 sub git_search_help {
5025 git_header_html();
5026 git_print_page_nav('','', $hash,$hash,$hash);
5027 print <<EOT;
5028 <dl>
5029 <dt><b>commit</b></dt>
5030 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
5032 my ($have_grep) = gitweb_check_feature('grep');
5033 if ($have_grep) {
5034 print <<EOT;
5035 <dt><b>grep</b></dt>
5036 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5037 a different one) are searched for the given
5038 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5039 (POSIX extended) and the matches are listed. On large
5040 trees, this search can take a while and put some strain on the server, so please use it with
5041 some consideration.</dd>
5044 print <<EOT;
5045 <dt><b>author</b></dt>
5046 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5047 <dt><b>committer</b></dt>
5048 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5050 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5051 if ($have_pickaxe) {
5052 print <<EOT;
5053 <dt><b>pickaxe</b></dt>
5054 <dd>All commits that caused the string to appear or disappear from any file (changes that
5055 added, removed or "modified" the string) will be listed. This search can take a while and
5056 takes a lot of strain on the server, so please use it wisely.</dd>
5059 print "</dl>\n";
5060 git_footer_html();
5063 sub git_shortlog {
5064 my $head = git_get_head_hash($project);
5065 if (!defined $hash) {
5066 $hash = $head;
5068 if (!defined $page) {
5069 $page = 0;
5071 my $refs = git_get_references();
5073 my @commitlist = parse_commits($hash, 101, (100 * $page));
5075 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
5076 my $next_link = '';
5077 if ($#commitlist >= 100) {
5078 $next_link =
5079 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
5080 -accesskey => "n", -title => "Alt-n"}, "next");
5083 git_header_html();
5084 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5085 git_print_header_div('summary', $project);
5087 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5089 git_footer_html();
5092 ## ......................................................................
5093 ## feeds (RSS, Atom; OPML)
5095 sub git_feed {
5096 my $format = shift || 'atom';
5097 my ($have_blame) = gitweb_check_feature('blame');
5099 # Atom: http://www.atomenabled.org/developers/syndication/
5100 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5101 if ($format ne 'rss' && $format ne 'atom') {
5102 die_error(undef, "Unknown web feed format");
5105 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5106 my $head = $hash || 'HEAD';
5107 my @commitlist = parse_commits($head, 150);
5109 my %latest_commit;
5110 my %latest_date;
5111 my $content_type = "application/$format+xml";
5112 if (defined $cgi->http('HTTP_ACCEPT') &&
5113 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5114 # browser (feed reader) prefers text/xml
5115 $content_type = 'text/xml';
5117 if (defined($commitlist[0])) {
5118 %latest_commit = %{$commitlist[0]};
5119 %latest_date = parse_date($latest_commit{'author_epoch'});
5120 print $cgi->header(
5121 -type => $content_type,
5122 -charset => 'utf-8',
5123 -last_modified => $latest_date{'rfc2822'});
5124 } else {
5125 print $cgi->header(
5126 -type => $content_type,
5127 -charset => 'utf-8');
5130 # Optimization: skip generating the body if client asks only
5131 # for Last-Modified date.
5132 return if ($cgi->request_method() eq 'HEAD');
5134 # header variables
5135 my $title = "$site_name - $project/$action";
5136 my $feed_type = 'log';
5137 if (defined $hash) {
5138 $title .= " - '$hash'";
5139 $feed_type = 'branch log';
5140 if (defined $file_name) {
5141 $title .= " :: $file_name";
5142 $feed_type = 'history';
5144 } elsif (defined $file_name) {
5145 $title .= " - $file_name";
5146 $feed_type = 'history';
5148 $title .= " $feed_type";
5149 my $descr = git_get_project_description($project);
5150 if (defined $descr) {
5151 $descr = esc_html($descr);
5152 } else {
5153 $descr = "$project " .
5154 ($format eq 'rss' ? 'RSS' : 'Atom') .
5155 " feed";
5157 my $owner = git_get_project_owner($project);
5158 $owner = esc_html($owner);
5160 #header
5161 my $alt_url;
5162 if (defined $file_name) {
5163 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5164 } elsif (defined $hash) {
5165 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5166 } else {
5167 $alt_url = href(-full=>1, action=>"summary");
5169 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5170 if ($format eq 'rss') {
5171 print <<XML;
5172 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5173 <channel>
5175 print "<title>$title</title>\n" .
5176 "<link>$alt_url</link>\n" .
5177 "<description>$descr</description>\n" .
5178 "<language>en</language>\n";
5179 } elsif ($format eq 'atom') {
5180 print <<XML;
5181 <feed xmlns="http://www.w3.org/2005/Atom">
5183 print "<title>$title</title>\n" .
5184 "<subtitle>$descr</subtitle>\n" .
5185 '<link rel="alternate" type="text/html" href="' .
5186 $alt_url . '" />' . "\n" .
5187 '<link rel="self" type="' . $content_type . '" href="' .
5188 $cgi->self_url() . '" />' . "\n" .
5189 "<id>" . href(-full=>1) . "</id>\n" .
5190 # use project owner for feed author
5191 "<author><name>$owner</name></author>\n";
5192 if (defined $favicon) {
5193 print "<icon>" . esc_url($favicon) . "</icon>\n";
5195 if (defined $logo_url) {
5196 # not twice as wide as tall: 72 x 27 pixels
5197 print "<logo>" . esc_url($logo) . "</logo>\n";
5199 if (! %latest_date) {
5200 # dummy date to keep the feed valid until commits trickle in:
5201 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5202 } else {
5203 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5207 # contents
5208 for (my $i = 0; $i <= $#commitlist; $i++) {
5209 my %co = %{$commitlist[$i]};
5210 my $commit = $co{'id'};
5211 # we read 150, we always show 30 and the ones more recent than 48 hours
5212 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5213 last;
5215 my %cd = parse_date($co{'author_epoch'});
5217 # get list of changed files
5218 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5219 $co{'parent'} || "--root",
5220 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5221 or next;
5222 my @difftree = map { chomp; $_ } <$fd>;
5223 close $fd
5224 or next;
5226 # print element (entry, item)
5227 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5228 if ($format eq 'rss') {
5229 print "<item>\n" .
5230 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5231 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5232 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5233 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5234 "<link>$co_url</link>\n" .
5235 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5236 "<content:encoded>" .
5237 "<![CDATA[\n";
5238 } elsif ($format eq 'atom') {
5239 print "<entry>\n" .
5240 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5241 "<updated>$cd{'iso-8601'}</updated>\n" .
5242 "<author>\n" .
5243 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5244 if ($co{'author_email'}) {
5245 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5247 print "</author>\n" .
5248 # use committer for contributor
5249 "<contributor>\n" .
5250 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5251 if ($co{'committer_email'}) {
5252 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5254 print "</contributor>\n" .
5255 "<published>$cd{'iso-8601'}</published>\n" .
5256 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5257 "<id>$co_url</id>\n" .
5258 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5259 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5261 my $comment = $co{'comment'};
5262 print "<pre>\n";
5263 foreach my $line (@$comment) {
5264 $line = esc_html($line);
5265 print "$line\n";
5267 print "</pre><ul>\n";
5268 foreach my $difftree_line (@difftree) {
5269 my %difftree = parse_difftree_raw_line($difftree_line);
5270 next if !$difftree{'from_id'};
5272 my $file = $difftree{'file'} || $difftree{'to_file'};
5274 print "<li>" .
5275 "[" .
5276 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5277 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5278 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5279 file_name=>$file, file_parent=>$difftree{'from_file'}),
5280 -title => "diff"}, 'D');
5281 if ($have_blame) {
5282 print $cgi->a({-href => href(-full=>1, action=>"blame",
5283 file_name=>$file, hash_base=>$commit),
5284 -title => "blame"}, 'B');
5286 # if this is not a feed of a file history
5287 if (!defined $file_name || $file_name ne $file) {
5288 print $cgi->a({-href => href(-full=>1, action=>"history",
5289 file_name=>$file, hash=>$commit),
5290 -title => "history"}, 'H');
5292 $file = esc_path($file);
5293 print "] ".
5294 "$file</li>\n";
5296 if ($format eq 'rss') {
5297 print "</ul>]]>\n" .
5298 "</content:encoded>\n" .
5299 "</item>\n";
5300 } elsif ($format eq 'atom') {
5301 print "</ul>\n</div>\n" .
5302 "</content>\n" .
5303 "</entry>\n";
5307 # end of feed
5308 if ($format eq 'rss') {
5309 print "</channel>\n</rss>\n";
5310 } elsif ($format eq 'atom') {
5311 print "</feed>\n";
5315 sub git_rss {
5316 git_feed('rss');
5319 sub git_atom {
5320 git_feed('atom');
5323 sub git_opml {
5324 my @list = git_get_projects_list();
5326 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5327 print <<XML;
5328 <?xml version="1.0" encoding="utf-8"?>
5329 <opml version="1.0">
5330 <head>
5331 <title>$site_name OPML Export</title>
5332 </head>
5333 <body>
5334 <outline text="git RSS feeds">
5337 foreach my $pr (@list) {
5338 my %proj = %$pr;
5339 my $head = git_get_head_hash($proj{'path'});
5340 if (!defined $head) {
5341 next;
5343 $git_dir = "$projectroot/$proj{'path'}";
5344 my %co = parse_commit($head);
5345 if (!%co) {
5346 next;
5349 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5350 my $rss = "$my_url?p=$proj{'path'};a=rss";
5351 my $html = "$my_url?p=$proj{'path'};a=summary";
5352 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5354 print <<XML;
5355 </outline>
5356 </body>
5357 </opml>