gitweb: Fix and simplify "split patch" detection
[git/jnareb-git.git] / gitweb / gitweb.perl
blob8c1e02c1044e318c34693e07e81d39a686fdfff5
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 # the width (in characters) of the projects list "Description" column
75 our $projects_list_description_width = 25;
77 # default order of projects list
78 # valid values are none, project, descr, owner, and age
79 our $default_projects_order = "project";
81 # show repository only if this file exists
82 # (only effective if this variable evaluates to true)
83 our $export_ok = "++GITWEB_EXPORT_OK++";
85 # only allow viewing of repositories also shown on the overview page
86 our $strict_export = "++GITWEB_STRICT_EXPORT++";
88 # list of git base URLs used for URL to where fetch project from,
89 # i.e. full URL is "$git_base_url/$project"
90 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
92 # default blob_plain mimetype and default charset for text/plain blob
93 our $default_blob_plain_mimetype = 'text/plain';
94 our $default_text_plain_charset = undef;
96 # file to use for guessing MIME types before trying /etc/mime.types
97 # (relative to the current git repository)
98 our $mimetypes_file = undef;
100 # assume this charset if line contains non-UTF-8 characters;
101 # it should be valid encoding (see Encoding::Supported(3pm) for list),
102 # for which encoding all byte sequences are valid, for example
103 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
104 # could be even 'utf-8' for the old behavior)
105 our $fallback_encoding = 'latin1';
107 # rename detection options for git-diff and git-diff-tree
108 # - default is '-M', with the cost proportional to
109 # (number of removed files) * (number of new files).
110 # - more costly is '-C' (which implies '-M'), with the cost proportional to
111 # (number of changed files + number of removed files) * (number of new files)
112 # - even more costly is '-C', '--find-copies-harder' with cost
113 # (number of files in the original tree) * (number of new files)
114 # - one might want to include '-B' option, e.g. '-B', '-M'
115 our @diff_opts = ('-M'); # taken from git_commit
117 # information about snapshot formats that gitweb is capable of serving
118 our %known_snapshot_formats = (
119 # name => {
120 # 'display' => display name,
121 # 'type' => mime type,
122 # 'suffix' => filename suffix,
123 # 'format' => --format for git-archive,
124 # 'compressor' => [compressor command and arguments]
125 # (array reference, optional)}
127 'tgz' => {
128 'display' => 'tar.gz',
129 'type' => 'application/x-gzip',
130 'suffix' => '.tar.gz',
131 'format' => 'tar',
132 'compressor' => ['gzip']},
134 'tbz2' => {
135 'display' => 'tar.bz2',
136 'type' => 'application/x-bzip2',
137 'suffix' => '.tar.bz2',
138 'format' => 'tar',
139 'compressor' => ['bzip2']},
141 'zip' => {
142 'display' => 'zip',
143 'type' => 'application/x-zip',
144 'suffix' => '.zip',
145 'format' => 'zip'},
148 # Aliases so we understand old gitweb.snapshot values in repository
149 # configuration.
150 our %known_snapshot_format_aliases = (
151 'gzip' => 'tgz',
152 'bzip2' => 'tbz2',
154 # backward compatibility: legacy gitweb config support
155 'x-gzip' => undef, 'gz' => undef,
156 'x-bzip2' => undef, 'bz2' => undef,
157 'x-zip' => undef, '' => undef,
160 # You define site-wide feature defaults here; override them with
161 # $GITWEB_CONFIG as necessary.
162 our %feature = (
163 # feature => {
164 # 'sub' => feature-sub (subroutine),
165 # 'override' => allow-override (boolean),
166 # 'default' => [ default options...] (array reference)}
168 # if feature is overridable (it means that allow-override has true value),
169 # then feature-sub will be called with default options as parameters;
170 # return value of feature-sub indicates if to enable specified feature
172 # if there is no 'sub' key (no feature-sub), then feature cannot be
173 # overriden
175 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
177 # Enable the 'blame' blob view, showing the last commit that modified
178 # each line in the file. This can be very CPU-intensive.
180 # To enable system wide have in $GITWEB_CONFIG
181 # $feature{'blame'}{'default'} = [1];
182 # To have project specific config enable override in $GITWEB_CONFIG
183 # $feature{'blame'}{'override'} = 1;
184 # and in project config gitweb.blame = 0|1;
185 'blame' => {
186 'sub' => \&feature_blame,
187 'override' => 0,
188 'default' => [0]},
190 # Enable the 'snapshot' link, providing a compressed archive of any
191 # tree. This can potentially generate high traffic if you have large
192 # project.
194 # Value is a list of formats defined in %known_snapshot_formats that
195 # you wish to offer.
196 # To disable system wide have in $GITWEB_CONFIG
197 # $feature{'snapshot'}{'default'} = [];
198 # To have project specific config enable override in $GITWEB_CONFIG
199 # $feature{'snapshot'}{'override'} = 1;
200 # and in project config, a comma-separated list of formats or "none"
201 # to disable. Example: gitweb.snapshot = tbz2,zip;
202 'snapshot' => {
203 'sub' => \&feature_snapshot,
204 'override' => 0,
205 'default' => ['tgz']},
207 # Enable text search, which will list the commits which match author,
208 # committer or commit text to a given string. Enabled by default.
209 # Project specific override is not supported.
210 'search' => {
211 'override' => 0,
212 'default' => [1]},
214 # Enable grep search, which will list the files in currently selected
215 # tree containing the given string. Enabled by default. This can be
216 # potentially CPU-intensive, of course.
218 # To enable system wide have in $GITWEB_CONFIG
219 # $feature{'grep'}{'default'} = [1];
220 # To have project specific config enable override in $GITWEB_CONFIG
221 # $feature{'grep'}{'override'} = 1;
222 # and in project config gitweb.grep = 0|1;
223 'grep' => {
224 'override' => 0,
225 'default' => [1]},
227 # Enable the pickaxe search, which will list the commits that modified
228 # a given string in a file. This can be practical and quite faster
229 # alternative to 'blame', but still potentially CPU-intensive.
231 # To enable system wide have in $GITWEB_CONFIG
232 # $feature{'pickaxe'}{'default'} = [1];
233 # To have project specific config enable override in $GITWEB_CONFIG
234 # $feature{'pickaxe'}{'override'} = 1;
235 # and in project config gitweb.pickaxe = 0|1;
236 'pickaxe' => {
237 'sub' => \&feature_pickaxe,
238 'override' => 0,
239 'default' => [1]},
241 # Make gitweb use an alternative format of the URLs which can be
242 # more readable and natural-looking: project name is embedded
243 # directly in the path and the query string contains other
244 # auxiliary information. All gitweb installations recognize
245 # URL in either format; this configures in which formats gitweb
246 # generates links.
248 # To enable system wide have in $GITWEB_CONFIG
249 # $feature{'pathinfo'}{'default'} = [1];
250 # Project specific override is not supported.
252 # Note that you will need to change the default location of CSS,
253 # favicon, logo and possibly other files to an absolute URL. Also,
254 # if gitweb.cgi serves as your indexfile, you will need to force
255 # $my_uri to contain the script name in your $GITWEB_CONFIG.
256 'pathinfo' => {
257 'override' => 0,
258 'default' => [0]},
260 # Make gitweb consider projects in project root subdirectories
261 # to be forks of existing projects. Given project $projname.git,
262 # projects matching $projname/*.git will not be shown in the main
263 # projects list, instead a '+' mark will be added to $projname
264 # there and a 'forks' view will be enabled for the project, listing
265 # all the forks. If project list is taken from a file, forks have
266 # to be listed after the main project.
268 # To enable system wide have in $GITWEB_CONFIG
269 # $feature{'forks'}{'default'} = [1];
270 # Project specific override is not supported.
271 'forks' => {
272 'override' => 0,
273 'default' => [0]},
276 sub gitweb_check_feature {
277 my ($name) = @_;
278 return unless exists $feature{$name};
279 my ($sub, $override, @defaults) = (
280 $feature{$name}{'sub'},
281 $feature{$name}{'override'},
282 @{$feature{$name}{'default'}});
283 if (!$override) { return @defaults; }
284 if (!defined $sub) {
285 warn "feature $name is not overrideable";
286 return @defaults;
288 return $sub->(@defaults);
291 sub feature_blame {
292 my ($val) = git_get_project_config('blame', '--bool');
294 if ($val eq 'true') {
295 return 1;
296 } elsif ($val eq 'false') {
297 return 0;
300 return $_[0];
303 sub feature_snapshot {
304 my (@fmts) = @_;
306 my ($val) = git_get_project_config('snapshot');
308 if ($val) {
309 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
312 return @fmts;
315 sub feature_grep {
316 my ($val) = git_get_project_config('grep', '--bool');
318 if ($val eq 'true') {
319 return (1);
320 } elsif ($val eq 'false') {
321 return (0);
324 return ($_[0]);
327 sub feature_pickaxe {
328 my ($val) = git_get_project_config('pickaxe', '--bool');
330 if ($val eq 'true') {
331 return (1);
332 } elsif ($val eq 'false') {
333 return (0);
336 return ($_[0]);
339 # checking HEAD file with -e is fragile if the repository was
340 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
341 # and then pruned.
342 sub check_head_link {
343 my ($dir) = @_;
344 my $headfile = "$dir/HEAD";
345 return ((-e $headfile) ||
346 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
349 sub check_export_ok {
350 my ($dir) = @_;
351 return (check_head_link($dir) &&
352 (!$export_ok || -e "$dir/$export_ok"));
355 # process alternate names for backward compatibility
356 # filter out unsupported (unknown) snapshot formats
357 sub filter_snapshot_fmts {
358 my @fmts = @_;
360 @fmts = map {
361 exists $known_snapshot_format_aliases{$_} ?
362 $known_snapshot_format_aliases{$_} : $_} @fmts;
363 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
367 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
368 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
370 # version of the core git binary
371 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
373 $projects_list ||= $projectroot;
375 # ======================================================================
376 # input validation and dispatch
377 our $action = $cgi->param('a');
378 if (defined $action) {
379 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
380 die_error(undef, "Invalid action parameter");
384 # parameters which are pathnames
385 our $project = $cgi->param('p');
386 if (defined $project) {
387 if (!validate_pathname($project) ||
388 !(-d "$projectroot/$project") ||
389 !check_head_link("$projectroot/$project") ||
390 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
391 ($strict_export && !project_in_list($project))) {
392 undef $project;
393 die_error(undef, "No such project");
397 our $file_name = $cgi->param('f');
398 if (defined $file_name) {
399 if (!validate_pathname($file_name)) {
400 die_error(undef, "Invalid file parameter");
404 our $file_parent = $cgi->param('fp');
405 if (defined $file_parent) {
406 if (!validate_pathname($file_parent)) {
407 die_error(undef, "Invalid file parent parameter");
411 # parameters which are refnames
412 our $hash = $cgi->param('h');
413 if (defined $hash) {
414 if (!validate_refname($hash)) {
415 die_error(undef, "Invalid hash parameter");
419 our $hash_parent = $cgi->param('hp');
420 if (defined $hash_parent) {
421 if (!validate_refname($hash_parent)) {
422 die_error(undef, "Invalid hash parent parameter");
426 our $hash_base = $cgi->param('hb');
427 if (defined $hash_base) {
428 if (!validate_refname($hash_base)) {
429 die_error(undef, "Invalid hash base parameter");
433 my %allowed_options = (
434 "--no-merges" => [ qw(rss atom log shortlog history) ],
437 our @extra_options = $cgi->param('opt');
438 if (defined @extra_options) {
439 foreach my $opt (@extra_options) {
440 if (not exists $allowed_options{$opt}) {
441 die_error(undef, "Invalid option parameter");
443 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
444 die_error(undef, "Invalid option parameter for this action");
449 our $hash_parent_base = $cgi->param('hpb');
450 if (defined $hash_parent_base) {
451 if (!validate_refname($hash_parent_base)) {
452 die_error(undef, "Invalid hash parent base parameter");
456 # other parameters
457 our $page = $cgi->param('pg');
458 if (defined $page) {
459 if ($page =~ m/[^0-9]/) {
460 die_error(undef, "Invalid page parameter");
464 our $searchtype = $cgi->param('st');
465 if (defined $searchtype) {
466 if ($searchtype =~ m/[^a-z]/) {
467 die_error(undef, "Invalid searchtype parameter");
471 our $searchtext = $cgi->param('s');
472 our $search_regexp;
473 if (defined $searchtext) {
474 if (length($searchtext) < 2) {
475 die_error(undef, "At least two characters are required for search parameter");
477 $search_regexp = quotemeta $searchtext;
480 # now read PATH_INFO and use it as alternative to parameters
481 sub evaluate_path_info {
482 return if defined $project;
483 my $path_info = $ENV{"PATH_INFO"};
484 return if !$path_info;
485 $path_info =~ s,^/+,,;
486 return if !$path_info;
487 # find which part of PATH_INFO is project
488 $project = $path_info;
489 $project =~ s,/+$,,;
490 while ($project && !check_head_link("$projectroot/$project")) {
491 $project =~ s,/*[^/]*$,,;
493 # validate project
494 $project = validate_pathname($project);
495 if (!$project ||
496 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
497 ($strict_export && !project_in_list($project))) {
498 undef $project;
499 return;
501 # do not change any parameters if an action is given using the query string
502 return if $action;
503 $path_info =~ s,^$project/*,,;
504 my ($refname, $pathname) = split(/:/, $path_info, 2);
505 if (defined $pathname) {
506 # we got "project.git/branch:filename" or "project.git/branch:dir/"
507 # we could use git_get_type(branch:pathname), but it needs $git_dir
508 $pathname =~ s,^/+,,;
509 if (!$pathname || substr($pathname, -1) eq "/") {
510 $action ||= "tree";
511 $pathname =~ s,/$,,;
512 } else {
513 $action ||= "blob_plain";
515 $hash_base ||= validate_refname($refname);
516 $file_name ||= validate_pathname($pathname);
517 } elsif (defined $refname) {
518 # we got "project.git/branch"
519 $action ||= "shortlog";
520 $hash ||= validate_refname($refname);
523 evaluate_path_info();
525 # path to the current git repository
526 our $git_dir;
527 $git_dir = "$projectroot/$project" if $project;
529 # dispatch
530 my %actions = (
531 "blame" => \&git_blame2,
532 "blobdiff" => \&git_blobdiff,
533 "blobdiff_plain" => \&git_blobdiff_plain,
534 "blob" => \&git_blob,
535 "blob_plain" => \&git_blob_plain,
536 "commitdiff" => \&git_commitdiff,
537 "commitdiff_plain" => \&git_commitdiff_plain,
538 "commit" => \&git_commit,
539 "forks" => \&git_forks,
540 "heads" => \&git_heads,
541 "history" => \&git_history,
542 "log" => \&git_log,
543 "rss" => \&git_rss,
544 "atom" => \&git_atom,
545 "search" => \&git_search,
546 "search_help" => \&git_search_help,
547 "shortlog" => \&git_shortlog,
548 "summary" => \&git_summary,
549 "tag" => \&git_tag,
550 "tags" => \&git_tags,
551 "tree" => \&git_tree,
552 "snapshot" => \&git_snapshot,
553 "object" => \&git_object,
554 # those below don't need $project
555 "opml" => \&git_opml,
556 "project_list" => \&git_project_list,
557 "project_index" => \&git_project_index,
560 if (!defined $action) {
561 if (defined $hash) {
562 $action = git_get_type($hash);
563 } elsif (defined $hash_base && defined $file_name) {
564 $action = git_get_type("$hash_base:$file_name");
565 } elsif (defined $project) {
566 $action = 'summary';
567 } else {
568 $action = 'project_list';
571 if (!defined($actions{$action})) {
572 die_error(undef, "Unknown action");
574 if ($action !~ m/^(opml|project_list|project_index)$/ &&
575 !$project) {
576 die_error(undef, "Project needed");
578 $actions{$action}->();
579 exit;
581 ## ======================================================================
582 ## action links
584 sub href(%) {
585 my %params = @_;
586 # default is to use -absolute url() i.e. $my_uri
587 my $href = $params{-full} ? $my_url : $my_uri;
589 # XXX: Warning: If you touch this, check the search form for updating,
590 # too.
592 my @mapping = (
593 project => "p",
594 action => "a",
595 file_name => "f",
596 file_parent => "fp",
597 hash => "h",
598 hash_parent => "hp",
599 hash_base => "hb",
600 hash_parent_base => "hpb",
601 page => "pg",
602 order => "o",
603 searchtext => "s",
604 searchtype => "st",
605 snapshot_format => "sf",
606 extra_options => "opt",
608 my %mapping = @mapping;
610 $params{'project'} = $project unless exists $params{'project'};
612 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
613 if ($use_pathinfo) {
614 # use PATH_INFO for project name
615 $href .= "/$params{'project'}" if defined $params{'project'};
616 delete $params{'project'};
618 # Summary just uses the project path URL
619 if (defined $params{'action'} && $params{'action'} eq 'summary') {
620 delete $params{'action'};
624 # now encode the parameters explicitly
625 my @result = ();
626 for (my $i = 0; $i < @mapping; $i += 2) {
627 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
628 if (defined $params{$name}) {
629 if (ref($params{$name}) eq "ARRAY") {
630 foreach my $par (@{$params{$name}}) {
631 push @result, $symbol . "=" . esc_param($par);
633 } else {
634 push @result, $symbol . "=" . esc_param($params{$name});
638 $href .= "?" . join(';', @result) if scalar @result;
640 return $href;
644 ## ======================================================================
645 ## validation, quoting/unquoting and escaping
647 sub validate_pathname {
648 my $input = shift || return undef;
650 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
651 # at the beginning, at the end, and between slashes.
652 # also this catches doubled slashes
653 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
654 return undef;
656 # no null characters
657 if ($input =~ m!\0!) {
658 return undef;
660 return $input;
663 sub validate_refname {
664 my $input = shift || return undef;
666 # textual hashes are O.K.
667 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
668 return $input;
670 # it must be correct pathname
671 $input = validate_pathname($input)
672 or return undef;
673 # restrictions on ref name according to git-check-ref-format
674 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
675 return undef;
677 return $input;
680 # decode sequences of octets in utf8 into Perl's internal form,
681 # which is utf-8 with utf8 flag set if needed. gitweb writes out
682 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
683 sub to_utf8 {
684 my $str = shift;
685 my $res;
686 eval { $res = decode_utf8($str, Encode::FB_CROAK); };
687 if (defined $res) {
688 return $res;
689 } else {
690 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
694 # quote unsafe chars, but keep the slash, even when it's not
695 # correct, but quoted slashes look too horrible in bookmarks
696 sub esc_param {
697 my $str = shift;
698 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
699 $str =~ s/\+/%2B/g;
700 $str =~ s/ /\+/g;
701 return $str;
704 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
705 sub esc_url {
706 my $str = shift;
707 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
708 $str =~ s/\+/%2B/g;
709 $str =~ s/ /\+/g;
710 return $str;
713 # replace invalid utf8 character with SUBSTITUTION sequence
714 sub esc_html ($;%) {
715 my $str = shift;
716 my %opts = @_;
718 $str = to_utf8($str);
719 $str = $cgi->escapeHTML($str);
720 if ($opts{'-nbsp'}) {
721 $str =~ s/ /&nbsp;/g;
723 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
724 return $str;
727 # quote control characters and escape filename to HTML
728 sub esc_path {
729 my $str = shift;
730 my %opts = @_;
732 $str = to_utf8($str);
733 $str = $cgi->escapeHTML($str);
734 if ($opts{'-nbsp'}) {
735 $str =~ s/ /&nbsp;/g;
737 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
738 return $str;
741 # Make control characters "printable", using character escape codes (CEC)
742 sub quot_cec {
743 my $cntrl = shift;
744 my %es = ( # character escape codes, aka escape sequences
745 "\t" => '\t', # tab (HT)
746 "\n" => '\n', # line feed (LF)
747 "\r" => '\r', # carrige return (CR)
748 "\f" => '\f', # form feed (FF)
749 "\b" => '\b', # backspace (BS)
750 "\a" => '\a', # alarm (bell) (BEL)
751 "\e" => '\e', # escape (ESC)
752 "\013" => '\v', # vertical tab (VT)
753 "\000" => '\0', # nul character (NUL)
755 my $chr = ( (exists $es{$cntrl})
756 ? $es{$cntrl}
757 : sprintf('\%03o', ord($cntrl)) );
758 return "<span class=\"cntrl\">$chr</span>";
761 # Alternatively use unicode control pictures codepoints,
762 # Unicode "printable representation" (PR)
763 sub quot_upr {
764 my $cntrl = shift;
765 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
766 return "<span class=\"cntrl\">$chr</span>";
769 # git may return quoted and escaped filenames
770 sub unquote {
771 my $str = shift;
773 sub unq {
774 my $seq = shift;
775 my %es = ( # character escape codes, aka escape sequences
776 't' => "\t", # tab (HT, TAB)
777 'n' => "\n", # newline (NL)
778 'r' => "\r", # return (CR)
779 'f' => "\f", # form feed (FF)
780 'b' => "\b", # backspace (BS)
781 'a' => "\a", # alarm (bell) (BEL)
782 'e' => "\e", # escape (ESC)
783 'v' => "\013", # vertical tab (VT)
786 if ($seq =~ m/^[0-7]{1,3}$/) {
787 # octal char sequence
788 return chr(oct($seq));
789 } elsif (exists $es{$seq}) {
790 # C escape sequence, aka character escape code
791 return $es{$seq}
793 # quoted ordinary character
794 return $seq;
797 if ($str =~ m/^"(.*)"$/) {
798 # needs unquoting
799 $str = $1;
800 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
802 return $str;
805 # escape tabs (convert tabs to spaces)
806 sub untabify {
807 my $line = shift;
809 while ((my $pos = index($line, "\t")) != -1) {
810 if (my $count = (8 - ($pos % 8))) {
811 my $spaces = ' ' x $count;
812 $line =~ s/\t/$spaces/;
816 return $line;
819 sub project_in_list {
820 my $project = shift;
821 my @list = git_get_projects_list();
822 return @list && scalar(grep { $_->{'path'} eq $project } @list);
825 ## ----------------------------------------------------------------------
826 ## HTML aware string manipulation
828 sub chop_str {
829 my $str = shift;
830 my $len = shift;
831 my $add_len = shift || 10;
833 # allow only $len chars, but don't cut a word if it would fit in $add_len
834 # if it doesn't fit, cut it if it's still longer than the dots we would add
835 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
836 my $body = $1;
837 my $tail = $2;
838 if (length($tail) > 4) {
839 $tail = " ...";
840 $body =~ s/&[^;]*$//; # remove chopped character entities
842 return "$body$tail";
845 ## ----------------------------------------------------------------------
846 ## functions returning short strings
848 # CSS class for given age value (in seconds)
849 sub age_class {
850 my $age = shift;
852 if (!defined $age) {
853 return "noage";
854 } elsif ($age < 60*60*2) {
855 return "age0";
856 } elsif ($age < 60*60*24*2) {
857 return "age1";
858 } else {
859 return "age2";
863 # convert age in seconds to "nn units ago" string
864 sub age_string {
865 my $age = shift;
866 my $age_str;
868 if ($age > 60*60*24*365*2) {
869 $age_str = (int $age/60/60/24/365);
870 $age_str .= " years ago";
871 } elsif ($age > 60*60*24*(365/12)*2) {
872 $age_str = int $age/60/60/24/(365/12);
873 $age_str .= " months ago";
874 } elsif ($age > 60*60*24*7*2) {
875 $age_str = int $age/60/60/24/7;
876 $age_str .= " weeks ago";
877 } elsif ($age > 60*60*24*2) {
878 $age_str = int $age/60/60/24;
879 $age_str .= " days ago";
880 } elsif ($age > 60*60*2) {
881 $age_str = int $age/60/60;
882 $age_str .= " hours ago";
883 } elsif ($age > 60*2) {
884 $age_str = int $age/60;
885 $age_str .= " min ago";
886 } elsif ($age > 2) {
887 $age_str = int $age;
888 $age_str .= " sec ago";
889 } else {
890 $age_str .= " right now";
892 return $age_str;
895 use constant {
896 S_IFINVALID => 0030000,
897 S_IFGITLINK => 0160000,
900 # submodule/subproject, a commit object reference
901 sub S_ISGITLINK($) {
902 my $mode = shift;
904 return (($mode & S_IFMT) == S_IFGITLINK)
907 # convert file mode in octal to symbolic file mode string
908 sub mode_str {
909 my $mode = oct shift;
911 if (S_ISGITLINK($mode)) {
912 return 'm---------';
913 } elsif (S_ISDIR($mode & S_IFMT)) {
914 return 'drwxr-xr-x';
915 } elsif (S_ISLNK($mode)) {
916 return 'lrwxrwxrwx';
917 } elsif (S_ISREG($mode)) {
918 # git cares only about the executable bit
919 if ($mode & S_IXUSR) {
920 return '-rwxr-xr-x';
921 } else {
922 return '-rw-r--r--';
924 } else {
925 return '----------';
929 # convert file mode in octal to file type string
930 sub file_type {
931 my $mode = shift;
933 if ($mode !~ m/^[0-7]+$/) {
934 return $mode;
935 } else {
936 $mode = oct $mode;
939 if (S_ISGITLINK($mode)) {
940 return "submodule";
941 } elsif (S_ISDIR($mode & S_IFMT)) {
942 return "directory";
943 } elsif (S_ISLNK($mode)) {
944 return "symlink";
945 } elsif (S_ISREG($mode)) {
946 return "file";
947 } else {
948 return "unknown";
952 # convert file mode in octal to file type description string
953 sub file_type_long {
954 my $mode = shift;
956 if ($mode !~ m/^[0-7]+$/) {
957 return $mode;
958 } else {
959 $mode = oct $mode;
962 if (S_ISGITLINK($mode)) {
963 return "submodule";
964 } elsif (S_ISDIR($mode & S_IFMT)) {
965 return "directory";
966 } elsif (S_ISLNK($mode)) {
967 return "symlink";
968 } elsif (S_ISREG($mode)) {
969 if ($mode & S_IXUSR) {
970 return "executable";
971 } else {
972 return "file";
974 } else {
975 return "unknown";
980 ## ----------------------------------------------------------------------
981 ## functions returning short HTML fragments, or transforming HTML fragments
982 ## which don't belong to other sections
984 # format line of commit message.
985 sub format_log_line_html {
986 my $line = shift;
988 $line = esc_html($line, -nbsp=>1);
989 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
990 my $hash_text = $1;
991 my $link =
992 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
993 -class => "text"}, $hash_text);
994 $line =~ s/$hash_text/$link/;
996 return $line;
999 # format marker of refs pointing to given object
1000 sub format_ref_marker {
1001 my ($refs, $id) = @_;
1002 my $markers = '';
1004 if (defined $refs->{$id}) {
1005 foreach my $ref (@{$refs->{$id}}) {
1006 my ($type, $name) = qw();
1007 # e.g. tags/v2.6.11 or heads/next
1008 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1009 $type = $1;
1010 $name = $2;
1011 } else {
1012 $type = "ref";
1013 $name = $ref;
1016 $markers .= " <span class=\"$type\" title=\"$ref\">" .
1017 esc_html($name) . "</span>";
1021 if ($markers) {
1022 return ' <span class="refs">'. $markers . '</span>';
1023 } else {
1024 return "";
1028 # format, perhaps shortened and with markers, title line
1029 sub format_subject_html {
1030 my ($long, $short, $href, $extra) = @_;
1031 $extra = '' unless defined($extra);
1033 if (length($short) < length($long)) {
1034 return $cgi->a({-href => $href, -class => "list subject",
1035 -title => to_utf8($long)},
1036 esc_html($short) . $extra);
1037 } else {
1038 return $cgi->a({-href => $href, -class => "list subject"},
1039 esc_html($long) . $extra);
1043 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1044 sub format_git_diff_header_line {
1045 my $line = shift;
1046 my $diffinfo = shift;
1047 my ($from, $to) = @_;
1049 if ($diffinfo->{'nparents'}) {
1050 # combined diff
1051 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1052 if ($to->{'href'}) {
1053 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1054 esc_path($to->{'file'}));
1055 } else { # file was deleted (no href)
1056 $line .= esc_path($to->{'file'});
1058 } else {
1059 # "ordinary" diff
1060 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1061 if ($from->{'href'}) {
1062 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1063 'a/' . esc_path($from->{'file'}));
1064 } else { # file was added (no href)
1065 $line .= 'a/' . esc_path($from->{'file'});
1067 $line .= ' ';
1068 if ($to->{'href'}) {
1069 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1070 'b/' . esc_path($to->{'file'}));
1071 } else { # file was deleted
1072 $line .= 'b/' . esc_path($to->{'file'});
1076 return "<div class=\"diff header\">$line</div>\n";
1079 # format extended diff header line, before patch itself
1080 sub format_extended_diff_header_line {
1081 my $line = shift;
1082 my $diffinfo = shift;
1083 my ($from, $to) = @_;
1085 # match <path>
1086 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1087 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1088 esc_path($from->{'file'}));
1090 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1091 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1092 esc_path($to->{'file'}));
1094 # match single <mode>
1095 if ($line =~ m/\s(\d{6})$/) {
1096 $line .= '<span class="info"> (' .
1097 file_type_long($1) .
1098 ')</span>';
1100 # match <hash>
1101 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1102 # can match only for combined diff
1103 $line = 'index ';
1104 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1105 if ($from->{'href'}[$i]) {
1106 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1107 -class=>"hash"},
1108 substr($diffinfo->{'from_id'}[$i],0,7));
1109 } else {
1110 $line .= '0' x 7;
1112 # separator
1113 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1115 $line .= '..';
1116 if ($to->{'href'}) {
1117 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1118 substr($diffinfo->{'to_id'},0,7));
1119 } else {
1120 $line .= '0' x 7;
1123 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1124 # can match only for ordinary diff
1125 my ($from_link, $to_link);
1126 if ($from->{'href'}) {
1127 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1128 substr($diffinfo->{'from_id'},0,7));
1129 } else {
1130 $from_link = '0' x 7;
1132 if ($to->{'href'}) {
1133 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1134 substr($diffinfo->{'to_id'},0,7));
1135 } else {
1136 $to_link = '0' x 7;
1138 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1139 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1142 return $line . "<br/>\n";
1145 # format from-file/to-file diff header
1146 sub format_diff_from_to_header {
1147 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1148 my $line;
1149 my $result = '';
1151 $line = $from_line;
1152 #assert($line =~ m/^---/) if DEBUG;
1153 # no extra formatting for "^--- /dev/null"
1154 if (! $diffinfo->{'nparents'}) {
1155 # ordinary (single parent) diff
1156 if ($line =~ m!^--- "?a/!) {
1157 if ($from->{'href'}) {
1158 $line = '--- a/' .
1159 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1160 esc_path($from->{'file'}));
1161 } else {
1162 $line = '--- a/' .
1163 esc_path($from->{'file'});
1166 $result .= qq!<div class="diff from_file">$line</div>\n!;
1168 } else {
1169 # combined diff (merge commit)
1170 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1171 if ($from->{'href'}[$i]) {
1172 $line = '--- ' .
1173 $cgi->a({-href=>href(action=>"blobdiff",
1174 hash_parent=>$diffinfo->{'from_id'}[$i],
1175 hash_parent_base=>$parents[$i],
1176 file_parent=>$from->{'file'}[$i],
1177 hash=>$diffinfo->{'to_id'},
1178 hash_base=>$hash,
1179 file_name=>$to->{'file'}),
1180 -class=>"path",
1181 -title=>"diff" . ($i+1)},
1182 $i+1) .
1183 '/' .
1184 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1185 esc_path($from->{'file'}[$i]));
1186 } else {
1187 $line = '--- /dev/null';
1189 $result .= qq!<div class="diff from_file">$line</div>\n!;
1193 $line = $to_line;
1194 #assert($line =~ m/^\+\+\+/) if DEBUG;
1195 # no extra formatting for "^+++ /dev/null"
1196 if ($line =~ m!^\+\+\+ "?b/!) {
1197 if ($to->{'href'}) {
1198 $line = '+++ b/' .
1199 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1200 esc_path($to->{'file'}));
1201 } else {
1202 $line = '+++ b/' .
1203 esc_path($to->{'file'});
1206 $result .= qq!<div class="diff to_file">$line</div>\n!;
1208 return $result;
1211 # create note for patch simplified by combined diff
1212 sub format_diff_cc_simplified {
1213 my ($diffinfo, @parents) = @_;
1214 my $result = '';
1216 $result .= "<div class=\"diff header\">" .
1217 "diff --cc ";
1218 if (!is_deleted($diffinfo)) {
1219 $result .= $cgi->a({-href => href(action=>"blob",
1220 hash_base=>$hash,
1221 hash=>$diffinfo->{'to_id'},
1222 file_name=>$diffinfo->{'to_file'}),
1223 -class => "path"},
1224 esc_path($diffinfo->{'to_file'}));
1225 } else {
1226 $result .= esc_path($diffinfo->{'to_file'});
1228 $result .= "</div>\n" . # class="diff header"
1229 "<div class=\"diff nodifferences\">" .
1230 "Simple merge" .
1231 "</div>\n"; # class="diff nodifferences"
1233 return $result;
1236 # format patch (diff) line (not to be used for diff headers)
1237 sub format_diff_line {
1238 my $line = shift;
1239 my ($from, $to) = @_;
1240 my $diff_class = "";
1242 chomp $line;
1244 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1245 # combined diff
1246 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1247 if ($line =~ m/^\@{3}/) {
1248 $diff_class = " chunk_header";
1249 } elsif ($line =~ m/^\\/) {
1250 $diff_class = " incomplete";
1251 } elsif ($prefix =~ tr/+/+/) {
1252 $diff_class = " add";
1253 } elsif ($prefix =~ tr/-/-/) {
1254 $diff_class = " rem";
1256 } else {
1257 # assume ordinary diff
1258 my $char = substr($line, 0, 1);
1259 if ($char eq '+') {
1260 $diff_class = " add";
1261 } elsif ($char eq '-') {
1262 $diff_class = " rem";
1263 } elsif ($char eq '@') {
1264 $diff_class = " chunk_header";
1265 } elsif ($char eq "\\") {
1266 $diff_class = " incomplete";
1269 $line = untabify($line);
1270 if ($from && $to && $line =~ m/^\@{2} /) {
1271 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1272 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1274 $from_lines = 0 unless defined $from_lines;
1275 $to_lines = 0 unless defined $to_lines;
1277 if ($from->{'href'}) {
1278 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1279 -class=>"list"}, $from_text);
1281 if ($to->{'href'}) {
1282 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1283 -class=>"list"}, $to_text);
1285 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1286 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1287 return "<div class=\"diff$diff_class\">$line</div>\n";
1288 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1289 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1290 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1292 @from_text = split(' ', $ranges);
1293 for (my $i = 0; $i < @from_text; ++$i) {
1294 ($from_start[$i], $from_nlines[$i]) =
1295 (split(',', substr($from_text[$i], 1)), 0);
1298 $to_text = pop @from_text;
1299 $to_start = pop @from_start;
1300 $to_nlines = pop @from_nlines;
1302 $line = "<span class=\"chunk_info\">$prefix ";
1303 for (my $i = 0; $i < @from_text; ++$i) {
1304 if ($from->{'href'}[$i]) {
1305 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1306 -class=>"list"}, $from_text[$i]);
1307 } else {
1308 $line .= $from_text[$i];
1310 $line .= " ";
1312 if ($to->{'href'}) {
1313 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1314 -class=>"list"}, $to_text);
1315 } else {
1316 $line .= $to_text;
1318 $line .= " $prefix</span>" .
1319 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1320 return "<div class=\"diff$diff_class\">$line</div>\n";
1322 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1325 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1326 # linked. Pass the hash of the tree/commit to snapshot.
1327 sub format_snapshot_links {
1328 my ($hash) = @_;
1329 my @snapshot_fmts = gitweb_check_feature('snapshot');
1330 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1331 my $num_fmts = @snapshot_fmts;
1332 if ($num_fmts > 1) {
1333 # A parenthesized list of links bearing format names.
1334 # e.g. "snapshot (_tar.gz_ _zip_)"
1335 return "snapshot (" . join(' ', map
1336 $cgi->a({
1337 -href => href(
1338 action=>"snapshot",
1339 hash=>$hash,
1340 snapshot_format=>$_
1342 }, $known_snapshot_formats{$_}{'display'})
1343 , @snapshot_fmts) . ")";
1344 } elsif ($num_fmts == 1) {
1345 # A single "snapshot" link whose tooltip bears the format name.
1346 # i.e. "_snapshot_"
1347 my ($fmt) = @snapshot_fmts;
1348 return
1349 $cgi->a({
1350 -href => href(
1351 action=>"snapshot",
1352 hash=>$hash,
1353 snapshot_format=>$fmt
1355 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1356 }, "snapshot");
1357 } else { # $num_fmts == 0
1358 return undef;
1362 ## ----------------------------------------------------------------------
1363 ## git utility subroutines, invoking git commands
1365 # returns path to the core git executable and the --git-dir parameter as list
1366 sub git_cmd {
1367 return $GIT, '--git-dir='.$git_dir;
1370 # returns path to the core git executable and the --git-dir parameter as string
1371 sub git_cmd_str {
1372 return join(' ', git_cmd());
1375 # get HEAD ref of given project as hash
1376 sub git_get_head_hash {
1377 my $project = shift;
1378 my $o_git_dir = $git_dir;
1379 my $retval = undef;
1380 $git_dir = "$projectroot/$project";
1381 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1382 my $head = <$fd>;
1383 close $fd;
1384 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1385 $retval = $1;
1388 if (defined $o_git_dir) {
1389 $git_dir = $o_git_dir;
1391 return $retval;
1394 # get type of given object
1395 sub git_get_type {
1396 my $hash = shift;
1398 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1399 my $type = <$fd>;
1400 close $fd or return;
1401 chomp $type;
1402 return $type;
1405 sub git_get_project_config {
1406 my ($key, $type) = @_;
1408 return unless ($key);
1409 $key =~ s/^gitweb\.//;
1410 return if ($key =~ m/\W/);
1412 my @x = (git_cmd(), 'config');
1413 if (defined $type) { push @x, $type; }
1414 push @x, "--get";
1415 push @x, "gitweb.$key";
1416 my $val = qx(@x);
1417 chomp $val;
1418 return ($val);
1421 # get hash of given path at given ref
1422 sub git_get_hash_by_path {
1423 my $base = shift;
1424 my $path = shift || return undef;
1425 my $type = shift;
1427 $path =~ s,/+$,,;
1429 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1430 or die_error(undef, "Open git-ls-tree failed");
1431 my $line = <$fd>;
1432 close $fd or return undef;
1434 if (!defined $line) {
1435 # there is no tree or hash given by $path at $base
1436 return undef;
1439 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1440 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1441 if (defined $type && $type ne $2) {
1442 # type doesn't match
1443 return undef;
1445 return $3;
1448 # get path of entry with given hash at given tree-ish (ref)
1449 # used to get 'from' filename for combined diff (merge commit) for renames
1450 sub git_get_path_by_hash {
1451 my $base = shift || return;
1452 my $hash = shift || return;
1454 local $/ = "\0";
1456 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1457 or return undef;
1458 while (my $line = <$fd>) {
1459 chomp $line;
1461 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1462 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1463 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1464 close $fd;
1465 return $1;
1468 close $fd;
1469 return undef;
1472 ## ......................................................................
1473 ## git utility functions, directly accessing git repository
1475 sub git_get_project_description {
1476 my $path = shift;
1478 open my $fd, "$projectroot/$path/description" or return undef;
1479 my $descr = <$fd>;
1480 close $fd;
1481 if (defined $descr) {
1482 chomp $descr;
1484 return $descr;
1487 sub git_get_project_url_list {
1488 my $path = shift;
1490 open my $fd, "$projectroot/$path/cloneurl" or return;
1491 my @git_project_url_list = map { chomp; $_ } <$fd>;
1492 close $fd;
1494 return wantarray ? @git_project_url_list : \@git_project_url_list;
1497 sub git_get_projects_list {
1498 my ($filter) = @_;
1499 my @list;
1501 $filter ||= '';
1502 $filter =~ s/\.git$//;
1504 my ($check_forks) = gitweb_check_feature('forks');
1506 if (-d $projects_list) {
1507 # search in directory
1508 my $dir = $projects_list . ($filter ? "/$filter" : '');
1509 # remove the trailing "/"
1510 $dir =~ s!/+$!!;
1511 my $pfxlen = length("$dir");
1513 File::Find::find({
1514 follow_fast => 1, # follow symbolic links
1515 follow_skip => 2, # ignore duplicates
1516 dangling_symlinks => 0, # ignore dangling symlinks, silently
1517 wanted => sub {
1518 # skip project-list toplevel, if we get it.
1519 return if (m!^[/.]$!);
1520 # only directories can be git repositories
1521 return unless (-d $_);
1523 my $subdir = substr($File::Find::name, $pfxlen + 1);
1524 # we check related file in $projectroot
1525 if ($check_forks and $subdir =~ m#/.#) {
1526 $File::Find::prune = 1;
1527 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1528 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1529 $File::Find::prune = 1;
1532 }, "$dir");
1534 } elsif (-f $projects_list) {
1535 # read from file(url-encoded):
1536 # 'git%2Fgit.git Linus+Torvalds'
1537 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1538 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1539 my %paths;
1540 open my ($fd), $projects_list or return;
1541 PROJECT:
1542 while (my $line = <$fd>) {
1543 chomp $line;
1544 my ($path, $owner) = split ' ', $line;
1545 $path = unescape($path);
1546 $owner = unescape($owner);
1547 if (!defined $path) {
1548 next;
1550 if ($filter ne '') {
1551 # looking for forks;
1552 my $pfx = substr($path, 0, length($filter));
1553 if ($pfx ne $filter) {
1554 next PROJECT;
1556 my $sfx = substr($path, length($filter));
1557 if ($sfx !~ /^\/.*\.git$/) {
1558 next PROJECT;
1560 } elsif ($check_forks) {
1561 PATH:
1562 foreach my $filter (keys %paths) {
1563 # looking for forks;
1564 my $pfx = substr($path, 0, length($filter));
1565 if ($pfx ne $filter) {
1566 next PATH;
1568 my $sfx = substr($path, length($filter));
1569 if ($sfx !~ /^\/.*\.git$/) {
1570 next PATH;
1572 # is a fork, don't include it in
1573 # the list
1574 next PROJECT;
1577 if (check_export_ok("$projectroot/$path")) {
1578 my $pr = {
1579 path => $path,
1580 owner => to_utf8($owner),
1582 push @list, $pr;
1583 (my $forks_path = $path) =~ s/\.git$//;
1584 $paths{$forks_path}++;
1587 close $fd;
1589 return @list;
1592 our $gitweb_project_owner = undef;
1593 sub git_get_project_list_from_file {
1595 return if (defined $gitweb_project_owner);
1597 $gitweb_project_owner = {};
1598 # read from file (url-encoded):
1599 # 'git%2Fgit.git Linus+Torvalds'
1600 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1601 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1602 if (-f $projects_list) {
1603 open (my $fd , $projects_list);
1604 while (my $line = <$fd>) {
1605 chomp $line;
1606 my ($pr, $ow) = split ' ', $line;
1607 $pr = unescape($pr);
1608 $ow = unescape($ow);
1609 $gitweb_project_owner->{$pr} = to_utf8($ow);
1611 close $fd;
1615 sub git_get_project_owner {
1616 my $project = shift;
1617 my $owner;
1619 return undef unless $project;
1621 if (!defined $gitweb_project_owner) {
1622 git_get_project_list_from_file();
1625 if (exists $gitweb_project_owner->{$project}) {
1626 $owner = $gitweb_project_owner->{$project};
1628 if (!defined $owner) {
1629 $owner = get_file_owner("$projectroot/$project");
1632 return $owner;
1635 sub git_get_last_activity {
1636 my ($path) = @_;
1637 my $fd;
1639 $git_dir = "$projectroot/$path";
1640 open($fd, "-|", git_cmd(), 'for-each-ref',
1641 '--format=%(committer)',
1642 '--sort=-committerdate',
1643 '--count=1',
1644 'refs/heads') or return;
1645 my $most_recent = <$fd>;
1646 close $fd or return;
1647 if (defined $most_recent &&
1648 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1649 my $timestamp = $1;
1650 my $age = time - $timestamp;
1651 return ($age, age_string($age));
1653 return (undef, undef);
1656 sub git_get_references {
1657 my $type = shift || "";
1658 my %refs;
1659 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1660 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1661 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1662 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1663 or return;
1665 while (my $line = <$fd>) {
1666 chomp $line;
1667 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1668 if (defined $refs{$1}) {
1669 push @{$refs{$1}}, $2;
1670 } else {
1671 $refs{$1} = [ $2 ];
1675 close $fd or return;
1676 return \%refs;
1679 sub git_get_rev_name_tags {
1680 my $hash = shift || return undef;
1682 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1683 or return;
1684 my $name_rev = <$fd>;
1685 close $fd;
1687 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1688 return $1;
1689 } else {
1690 # catches also '$hash undefined' output
1691 return undef;
1695 ## ----------------------------------------------------------------------
1696 ## parse to hash functions
1698 sub parse_date {
1699 my $epoch = shift;
1700 my $tz = shift || "-0000";
1702 my %date;
1703 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1704 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1705 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1706 $date{'hour'} = $hour;
1707 $date{'minute'} = $min;
1708 $date{'mday'} = $mday;
1709 $date{'day'} = $days[$wday];
1710 $date{'month'} = $months[$mon];
1711 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1712 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1713 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1714 $mday, $months[$mon], $hour ,$min;
1715 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1716 1900+$year, $mon, $mday, $hour ,$min, $sec;
1718 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1719 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1720 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1721 $date{'hour_local'} = $hour;
1722 $date{'minute_local'} = $min;
1723 $date{'tz_local'} = $tz;
1724 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1725 1900+$year, $mon+1, $mday,
1726 $hour, $min, $sec, $tz);
1727 return %date;
1730 sub parse_tag {
1731 my $tag_id = shift;
1732 my %tag;
1733 my @comment;
1735 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1736 $tag{'id'} = $tag_id;
1737 while (my $line = <$fd>) {
1738 chomp $line;
1739 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1740 $tag{'object'} = $1;
1741 } elsif ($line =~ m/^type (.+)$/) {
1742 $tag{'type'} = $1;
1743 } elsif ($line =~ m/^tag (.+)$/) {
1744 $tag{'name'} = $1;
1745 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1746 $tag{'author'} = $1;
1747 $tag{'epoch'} = $2;
1748 $tag{'tz'} = $3;
1749 } elsif ($line =~ m/--BEGIN/) {
1750 push @comment, $line;
1751 last;
1752 } elsif ($line eq "") {
1753 last;
1756 push @comment, <$fd>;
1757 $tag{'comment'} = \@comment;
1758 close $fd or return;
1759 if (!defined $tag{'name'}) {
1760 return
1762 return %tag
1765 sub parse_commit_text {
1766 my ($commit_text, $withparents) = @_;
1767 my @commit_lines = split '\n', $commit_text;
1768 my %co;
1770 pop @commit_lines; # Remove '\0'
1772 if (! @commit_lines) {
1773 return;
1776 my $header = shift @commit_lines;
1777 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1778 return;
1780 ($co{'id'}, my @parents) = split ' ', $header;
1781 while (my $line = shift @commit_lines) {
1782 last if $line eq "\n";
1783 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1784 $co{'tree'} = $1;
1785 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1786 push @parents, $1;
1787 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1788 $co{'author'} = $1;
1789 $co{'author_epoch'} = $2;
1790 $co{'author_tz'} = $3;
1791 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1792 $co{'author_name'} = $1;
1793 $co{'author_email'} = $2;
1794 } else {
1795 $co{'author_name'} = $co{'author'};
1797 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1798 $co{'committer'} = $1;
1799 $co{'committer_epoch'} = $2;
1800 $co{'committer_tz'} = $3;
1801 $co{'committer_name'} = $co{'committer'};
1802 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1803 $co{'committer_name'} = $1;
1804 $co{'committer_email'} = $2;
1805 } else {
1806 $co{'committer_name'} = $co{'committer'};
1810 if (!defined $co{'tree'}) {
1811 return;
1813 $co{'parents'} = \@parents;
1814 $co{'parent'} = $parents[0];
1816 foreach my $title (@commit_lines) {
1817 $title =~ s/^ //;
1818 if ($title ne "") {
1819 $co{'title'} = chop_str($title, 80, 5);
1820 # remove leading stuff of merges to make the interesting part visible
1821 if (length($title) > 50) {
1822 $title =~ s/^Automatic //;
1823 $title =~ s/^merge (of|with) /Merge ... /i;
1824 if (length($title) > 50) {
1825 $title =~ s/(http|rsync):\/\///;
1827 if (length($title) > 50) {
1828 $title =~ s/(master|www|rsync)\.//;
1830 if (length($title) > 50) {
1831 $title =~ s/kernel.org:?//;
1833 if (length($title) > 50) {
1834 $title =~ s/\/pub\/scm//;
1837 $co{'title_short'} = chop_str($title, 50, 5);
1838 last;
1841 if ($co{'title'} eq "") {
1842 $co{'title'} = $co{'title_short'} = '(no commit message)';
1844 # remove added spaces
1845 foreach my $line (@commit_lines) {
1846 $line =~ s/^ //;
1848 $co{'comment'} = \@commit_lines;
1850 my $age = time - $co{'committer_epoch'};
1851 $co{'age'} = $age;
1852 $co{'age_string'} = age_string($age);
1853 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1854 if ($age > 60*60*24*7*2) {
1855 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1856 $co{'age_string_age'} = $co{'age_string'};
1857 } else {
1858 $co{'age_string_date'} = $co{'age_string'};
1859 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1861 return %co;
1864 sub parse_commit {
1865 my ($commit_id) = @_;
1866 my %co;
1868 local $/ = "\0";
1870 open my $fd, "-|", git_cmd(), "rev-list",
1871 "--parents",
1872 "--header",
1873 "--max-count=1",
1874 $commit_id,
1875 "--",
1876 or die_error(undef, "Open git-rev-list failed");
1877 %co = parse_commit_text(<$fd>, 1);
1878 close $fd;
1880 return %co;
1883 sub parse_commits {
1884 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1885 my @cos;
1887 $maxcount ||= 1;
1888 $skip ||= 0;
1890 local $/ = "\0";
1892 open my $fd, "-|", git_cmd(), "rev-list",
1893 "--header",
1894 ($arg ? ($arg) : ()),
1895 ("--max-count=" . $maxcount),
1896 ("--skip=" . $skip),
1897 @extra_options,
1898 $commit_id,
1899 "--",
1900 ($filename ? ($filename) : ())
1901 or die_error(undef, "Open git-rev-list failed");
1902 while (my $line = <$fd>) {
1903 my %co = parse_commit_text($line);
1904 push @cos, \%co;
1906 close $fd;
1908 return wantarray ? @cos : \@cos;
1911 # parse ref from ref_file, given by ref_id, with given type
1912 sub parse_ref {
1913 my $ref_file = shift;
1914 my $ref_id = shift;
1915 my $type = shift || git_get_type($ref_id);
1916 my %ref_item;
1918 $ref_item{'type'} = $type;
1919 $ref_item{'id'} = $ref_id;
1920 $ref_item{'epoch'} = 0;
1921 $ref_item{'age'} = "unknown";
1922 if ($type eq "tag") {
1923 my %tag = parse_tag($ref_id);
1924 $ref_item{'comment'} = $tag{'comment'};
1925 if ($tag{'type'} eq "commit") {
1926 my %co = parse_commit($tag{'object'});
1927 $ref_item{'epoch'} = $co{'committer_epoch'};
1928 $ref_item{'age'} = $co{'age_string'};
1929 } elsif (defined($tag{'epoch'})) {
1930 my $age = time - $tag{'epoch'};
1931 $ref_item{'epoch'} = $tag{'epoch'};
1932 $ref_item{'age'} = age_string($age);
1934 $ref_item{'reftype'} = $tag{'type'};
1935 $ref_item{'name'} = $tag{'name'};
1936 $ref_item{'refid'} = $tag{'object'};
1937 } elsif ($type eq "commit"){
1938 my %co = parse_commit($ref_id);
1939 $ref_item{'reftype'} = "commit";
1940 $ref_item{'name'} = $ref_file;
1941 $ref_item{'title'} = $co{'title'};
1942 $ref_item{'refid'} = $ref_id;
1943 $ref_item{'epoch'} = $co{'committer_epoch'};
1944 $ref_item{'age'} = $co{'age_string'};
1945 } else {
1946 $ref_item{'reftype'} = $type;
1947 $ref_item{'name'} = $ref_file;
1948 $ref_item{'refid'} = $ref_id;
1951 return %ref_item;
1954 # parse line of git-diff-tree "raw" output
1955 sub parse_difftree_raw_line {
1956 my $line = shift;
1957 my %res;
1959 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1960 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1961 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1962 $res{'from_mode'} = $1;
1963 $res{'to_mode'} = $2;
1964 $res{'from_id'} = $3;
1965 $res{'to_id'} = $4;
1966 $res{'status'} = $res{'status_str'} = $5;
1967 $res{'similarity'} = $6;
1968 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1969 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1970 } else {
1971 $res{'file'} = unquote($7);
1974 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1975 # combined diff (for merge commit)
1976 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1977 $res{'nparents'} = length($1);
1978 $res{'from_mode'} = [ split(' ', $2) ];
1979 $res{'to_mode'} = pop @{$res{'from_mode'}};
1980 $res{'from_id'} = [ split(' ', $3) ];
1981 $res{'to_id'} = pop @{$res{'from_id'}};
1982 $res{'status_str'} = $4;
1983 $res{'status'} = [ split('', $4) ];
1984 $res{'to_file'} = unquote($5);
1986 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1987 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1988 $res{'commit'} = $1;
1991 return wantarray ? %res : \%res;
1994 # parse line of git-ls-tree output
1995 sub parse_ls_tree_line ($;%) {
1996 my $line = shift;
1997 my %opts = @_;
1998 my %res;
2000 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2001 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2003 $res{'mode'} = $1;
2004 $res{'type'} = $2;
2005 $res{'hash'} = $3;
2006 if ($opts{'-z'}) {
2007 $res{'name'} = $4;
2008 } else {
2009 $res{'name'} = unquote($4);
2012 return wantarray ? %res : \%res;
2015 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2016 sub parse_from_to_diffinfo {
2017 my ($diffinfo, $from, $to, @parents) = @_;
2019 if ($diffinfo->{'nparents'}) {
2020 # combined diff
2021 $from->{'file'} = [];
2022 $from->{'href'} = [];
2023 fill_from_file_info($diffinfo, @parents)
2024 unless exists $diffinfo->{'from_file'};
2025 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2026 $from->{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2027 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2028 $from->{'href'}[$i] = href(action=>"blob",
2029 hash_base=>$parents[$i],
2030 hash=>$diffinfo->{'from_id'}[$i],
2031 file_name=>$from->{'file'}[$i]);
2032 } else {
2033 $from->{'href'}[$i] = undef;
2036 } else {
2037 $from->{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2038 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2039 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2040 hash=>$diffinfo->{'from_id'},
2041 file_name=>$from->{'file'});
2042 } else {
2043 delete $from->{'href'};
2047 $to->{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2048 if (!is_deleted($diffinfo)) { # file exists in result
2049 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2050 hash=>$diffinfo->{'to_id'},
2051 file_name=>$to->{'file'});
2052 } else {
2053 delete $to->{'href'};
2057 ## ......................................................................
2058 ## parse to array of hashes functions
2060 sub git_get_heads_list {
2061 my $limit = shift;
2062 my @headslist;
2064 open my $fd, '-|', git_cmd(), 'for-each-ref',
2065 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2066 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2067 'refs/heads'
2068 or return;
2069 while (my $line = <$fd>) {
2070 my %ref_item;
2072 chomp $line;
2073 my ($refinfo, $committerinfo) = split(/\0/, $line);
2074 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2075 my ($committer, $epoch, $tz) =
2076 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2077 $name =~ s!^refs/heads/!!;
2079 $ref_item{'name'} = $name;
2080 $ref_item{'id'} = $hash;
2081 $ref_item{'title'} = $title || '(no commit message)';
2082 $ref_item{'epoch'} = $epoch;
2083 if ($epoch) {
2084 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2085 } else {
2086 $ref_item{'age'} = "unknown";
2089 push @headslist, \%ref_item;
2091 close $fd;
2093 return wantarray ? @headslist : \@headslist;
2096 sub git_get_tags_list {
2097 my $limit = shift;
2098 my @tagslist;
2100 open my $fd, '-|', git_cmd(), 'for-each-ref',
2101 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2102 '--format=%(objectname) %(objecttype) %(refname) '.
2103 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2104 'refs/tags'
2105 or return;
2106 while (my $line = <$fd>) {
2107 my %ref_item;
2109 chomp $line;
2110 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2111 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2112 my ($creator, $epoch, $tz) =
2113 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2114 $name =~ s!^refs/tags/!!;
2116 $ref_item{'type'} = $type;
2117 $ref_item{'id'} = $id;
2118 $ref_item{'name'} = $name;
2119 if ($type eq "tag") {
2120 $ref_item{'subject'} = $title;
2121 $ref_item{'reftype'} = $reftype;
2122 $ref_item{'refid'} = $refid;
2123 } else {
2124 $ref_item{'reftype'} = $type;
2125 $ref_item{'refid'} = $id;
2128 if ($type eq "tag" || $type eq "commit") {
2129 $ref_item{'epoch'} = $epoch;
2130 if ($epoch) {
2131 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2132 } else {
2133 $ref_item{'age'} = "unknown";
2137 push @tagslist, \%ref_item;
2139 close $fd;
2141 return wantarray ? @tagslist : \@tagslist;
2144 ## ----------------------------------------------------------------------
2145 ## filesystem-related functions
2147 sub get_file_owner {
2148 my $path = shift;
2150 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2151 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2152 if (!defined $gcos) {
2153 return undef;
2155 my $owner = $gcos;
2156 $owner =~ s/[,;].*$//;
2157 return to_utf8($owner);
2160 ## ......................................................................
2161 ## mimetype related functions
2163 sub mimetype_guess_file {
2164 my $filename = shift;
2165 my $mimemap = shift;
2166 -r $mimemap or return undef;
2168 my %mimemap;
2169 open(MIME, $mimemap) or return undef;
2170 while (<MIME>) {
2171 next if m/^#/; # skip comments
2172 my ($mime, $exts) = split(/\t+/);
2173 if (defined $exts) {
2174 my @exts = split(/\s+/, $exts);
2175 foreach my $ext (@exts) {
2176 $mimemap{$ext} = $mime;
2180 close(MIME);
2182 $filename =~ /\.([^.]*)$/;
2183 return $mimemap{$1};
2186 sub mimetype_guess {
2187 my $filename = shift;
2188 my $mime;
2189 $filename =~ /\./ or return undef;
2191 if ($mimetypes_file) {
2192 my $file = $mimetypes_file;
2193 if ($file !~ m!^/!) { # if it is relative path
2194 # it is relative to project
2195 $file = "$projectroot/$project/$file";
2197 $mime = mimetype_guess_file($filename, $file);
2199 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2200 return $mime;
2203 sub blob_mimetype {
2204 my $fd = shift;
2205 my $filename = shift;
2207 if ($filename) {
2208 my $mime = mimetype_guess($filename);
2209 $mime and return $mime;
2212 # just in case
2213 return $default_blob_plain_mimetype unless $fd;
2215 if (-T $fd) {
2216 return 'text/plain' .
2217 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2218 } elsif (! $filename) {
2219 return 'application/octet-stream';
2220 } elsif ($filename =~ m/\.png$/i) {
2221 return 'image/png';
2222 } elsif ($filename =~ m/\.gif$/i) {
2223 return 'image/gif';
2224 } elsif ($filename =~ m/\.jpe?g$/i) {
2225 return 'image/jpeg';
2226 } else {
2227 return 'application/octet-stream';
2231 ## ======================================================================
2232 ## functions printing HTML: header, footer, error page
2234 sub git_header_html {
2235 my $status = shift || "200 OK";
2236 my $expires = shift;
2238 my $title = "$site_name";
2239 if (defined $project) {
2240 $title .= " - " . to_utf8($project);
2241 if (defined $action) {
2242 $title .= "/$action";
2243 if (defined $file_name) {
2244 $title .= " - " . esc_path($file_name);
2245 if ($action eq "tree" && $file_name !~ m|/$|) {
2246 $title .= "/";
2251 my $content_type;
2252 # require explicit support from the UA if we are to send the page as
2253 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2254 # we have to do this because MSIE sometimes globs '*/*', pretending to
2255 # support xhtml+xml but choking when it gets what it asked for.
2256 if (defined $cgi->http('HTTP_ACCEPT') &&
2257 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2258 $cgi->Accept('application/xhtml+xml') != 0) {
2259 $content_type = 'application/xhtml+xml';
2260 } else {
2261 $content_type = 'text/html';
2263 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2264 -status=> $status, -expires => $expires);
2265 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2266 print <<EOF;
2267 <?xml version="1.0" encoding="utf-8"?>
2268 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2269 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2270 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2271 <!-- git core binaries version $git_version -->
2272 <head>
2273 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2274 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2275 <meta name="robots" content="index, nofollow"/>
2276 <title>$title</title>
2278 # print out each stylesheet that exist
2279 if (defined $stylesheet) {
2280 #provides backwards capability for those people who define style sheet in a config file
2281 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2282 } else {
2283 foreach my $stylesheet (@stylesheets) {
2284 next unless $stylesheet;
2285 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2288 if (defined $project) {
2289 printf('<link rel="alternate" title="%s log RSS feed" '.
2290 'href="%s" type="application/rss+xml" />'."\n",
2291 esc_param($project), href(action=>"rss"));
2292 printf('<link rel="alternate" title="%s log RSS feed (no merges)" '.
2293 'href="%s" type="application/rss+xml" />'."\n",
2294 esc_param($project), href(action=>"rss",
2295 extra_options=>"--no-merges"));
2296 printf('<link rel="alternate" title="%s log Atom feed" '.
2297 'href="%s" type="application/atom+xml" />'."\n",
2298 esc_param($project), href(action=>"atom"));
2299 printf('<link rel="alternate" title="%s log Atom feed (no merges)" '.
2300 'href="%s" type="application/atom+xml" />'."\n",
2301 esc_param($project), href(action=>"atom",
2302 extra_options=>"--no-merges"));
2303 } else {
2304 printf('<link rel="alternate" title="%s projects list" '.
2305 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2306 $site_name, href(project=>undef, action=>"project_index"));
2307 printf('<link rel="alternate" title="%s projects feeds" '.
2308 'href="%s" type="text/x-opml"/>'."\n",
2309 $site_name, href(project=>undef, action=>"opml"));
2311 if (defined $favicon) {
2312 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2315 print "</head>\n" .
2316 "<body>\n";
2318 if (-f $site_header) {
2319 open (my $fd, $site_header);
2320 print <$fd>;
2321 close $fd;
2324 print "<div class=\"page_header\">\n" .
2325 $cgi->a({-href => esc_url($logo_url),
2326 -title => $logo_label},
2327 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2328 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2329 if (defined $project) {
2330 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2331 if (defined $action) {
2332 print " / $action";
2334 print "\n";
2336 print "</div>\n";
2338 my ($have_search) = gitweb_check_feature('search');
2339 if ((defined $project) && ($have_search)) {
2340 if (!defined $searchtext) {
2341 $searchtext = "";
2343 my $search_hash;
2344 if (defined $hash_base) {
2345 $search_hash = $hash_base;
2346 } elsif (defined $hash) {
2347 $search_hash = $hash;
2348 } else {
2349 $search_hash = "HEAD";
2351 my $action = $my_uri;
2352 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2353 if ($use_pathinfo) {
2354 $action .= "/$project";
2355 } else {
2356 $cgi->param("p", $project);
2358 $cgi->param("a", "search");
2359 $cgi->param("h", $search_hash);
2360 print $cgi->startform(-method => "get", -action => $action) .
2361 "<div class=\"search\">\n" .
2362 (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") .
2363 $cgi->hidden(-name => "a") . "\n" .
2364 $cgi->hidden(-name => "h") . "\n" .
2365 $cgi->popup_menu(-name => 'st', -default => 'commit',
2366 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2367 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2368 " search:\n",
2369 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2370 "</div>" .
2371 $cgi->end_form() . "\n";
2375 sub git_footer_html {
2376 print "<div class=\"page_footer\">\n";
2377 if (defined $project) {
2378 my $descr = git_get_project_description($project);
2379 if (defined $descr) {
2380 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2382 print $cgi->a({-href => href(action=>"rss"),
2383 -class => "rss_logo"}, "RSS") . " ";
2384 print $cgi->a({-href => href(action=>"atom"),
2385 -class => "rss_logo"}, "Atom") . "\n";
2386 } else {
2387 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2388 -class => "rss_logo"}, "OPML") . " ";
2389 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2390 -class => "rss_logo"}, "TXT") . "\n";
2392 print "</div>\n" ;
2394 if (-f $site_footer) {
2395 open (my $fd, $site_footer);
2396 print <$fd>;
2397 close $fd;
2400 print "</body>\n" .
2401 "</html>";
2404 sub die_error {
2405 my $status = shift || "403 Forbidden";
2406 my $error = shift || "Malformed query, file missing or permission denied";
2408 git_header_html($status);
2409 print <<EOF;
2410 <div class="page_body">
2411 <br /><br />
2412 $status - $error
2413 <br />
2414 </div>
2416 git_footer_html();
2417 exit;
2420 ## ----------------------------------------------------------------------
2421 ## functions printing or outputting HTML: navigation
2423 sub git_print_page_nav {
2424 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2425 $extra = '' if !defined $extra; # pager or formats
2427 my @navs = qw(summary shortlog log commit commitdiff tree);
2428 if ($suppress) {
2429 @navs = grep { $_ ne $suppress } @navs;
2432 my %arg = map { $_ => {action=>$_} } @navs;
2433 if (defined $head) {
2434 for (qw(commit commitdiff)) {
2435 $arg{$_}{'hash'} = $head;
2437 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2438 for (qw(shortlog log)) {
2439 $arg{$_}{'hash'} = $head;
2443 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2444 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2446 print "<div class=\"page_nav\">\n" .
2447 (join " | ",
2448 map { $_ eq $current ?
2449 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2450 } @navs);
2451 print "<br/>\n$extra<br/>\n" .
2452 "</div>\n";
2455 sub format_paging_nav {
2456 my ($action, $hash, $head, $page, $nrevs) = @_;
2457 my $paging_nav;
2460 if ($hash ne $head || $page) {
2461 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2462 } else {
2463 $paging_nav .= "HEAD";
2466 if ($page > 0) {
2467 $paging_nav .= " &sdot; " .
2468 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2469 -accesskey => "p", -title => "Alt-p"}, "prev");
2470 } else {
2471 $paging_nav .= " &sdot; prev";
2474 if ($nrevs >= (100 * ($page+1)-1)) {
2475 $paging_nav .= " &sdot; " .
2476 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2477 -accesskey => "n", -title => "Alt-n"}, "next");
2478 } else {
2479 $paging_nav .= " &sdot; next";
2482 return $paging_nav;
2485 ## ......................................................................
2486 ## functions printing or outputting HTML: div
2488 sub git_print_header_div {
2489 my ($action, $title, $hash, $hash_base) = @_;
2490 my %args = ();
2492 $args{'action'} = $action;
2493 $args{'hash'} = $hash if $hash;
2494 $args{'hash_base'} = $hash_base if $hash_base;
2496 print "<div class=\"header\">\n" .
2497 $cgi->a({-href => href(%args), -class => "title"},
2498 $title ? $title : $action) .
2499 "\n</div>\n";
2502 #sub git_print_authorship (\%) {
2503 sub git_print_authorship {
2504 my $co = shift;
2506 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2507 print "<div class=\"author_date\">" .
2508 esc_html($co->{'author_name'}) .
2509 " [$ad{'rfc2822'}";
2510 if ($ad{'hour_local'} < 6) {
2511 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2512 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2513 } else {
2514 printf(" (%02d:%02d %s)",
2515 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2517 print "]</div>\n";
2520 sub git_print_page_path {
2521 my $name = shift;
2522 my $type = shift;
2523 my $hb = shift;
2526 print "<div class=\"page_path\">";
2527 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2528 -title => 'tree root'}, to_utf8("[$project]"));
2529 print " / ";
2530 if (defined $name) {
2531 my @dirname = split '/', $name;
2532 my $basename = pop @dirname;
2533 my $fullname = '';
2535 foreach my $dir (@dirname) {
2536 $fullname .= ($fullname ? '/' : '') . $dir;
2537 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2538 hash_base=>$hb),
2539 -title => $fullname}, esc_path($dir));
2540 print " / ";
2542 if (defined $type && $type eq 'blob') {
2543 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2544 hash_base=>$hb),
2545 -title => $name}, esc_path($basename));
2546 } elsif (defined $type && $type eq 'tree') {
2547 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2548 hash_base=>$hb),
2549 -title => $name}, esc_path($basename));
2550 print " / ";
2551 } else {
2552 print esc_path($basename);
2555 print "<br/></div>\n";
2558 # sub git_print_log (\@;%) {
2559 sub git_print_log ($;%) {
2560 my $log = shift;
2561 my %opts = @_;
2563 if ($opts{'-remove_title'}) {
2564 # remove title, i.e. first line of log
2565 shift @$log;
2567 # remove leading empty lines
2568 while (defined $log->[0] && $log->[0] eq "") {
2569 shift @$log;
2572 # print log
2573 my $signoff = 0;
2574 my $empty = 0;
2575 foreach my $line (@$log) {
2576 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2577 $signoff = 1;
2578 $empty = 0;
2579 if (! $opts{'-remove_signoff'}) {
2580 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2581 next;
2582 } else {
2583 # remove signoff lines
2584 next;
2586 } else {
2587 $signoff = 0;
2590 # print only one empty line
2591 # do not print empty line after signoff
2592 if ($line eq "") {
2593 next if ($empty || $signoff);
2594 $empty = 1;
2595 } else {
2596 $empty = 0;
2599 print format_log_line_html($line) . "<br/>\n";
2602 if ($opts{'-final_empty_line'}) {
2603 # end with single empty line
2604 print "<br/>\n" unless $empty;
2608 # return link target (what link points to)
2609 sub git_get_link_target {
2610 my $hash = shift;
2611 my $link_target;
2613 # read link
2614 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2615 or return;
2617 local $/;
2618 $link_target = <$fd>;
2620 close $fd
2621 or return;
2623 return $link_target;
2626 # given link target, and the directory (basedir) the link is in,
2627 # return target of link relative to top directory (top tree);
2628 # return undef if it is not possible (including absolute links).
2629 sub normalize_link_target {
2630 my ($link_target, $basedir, $hash_base) = @_;
2632 # we can normalize symlink target only if $hash_base is provided
2633 return unless $hash_base;
2635 # absolute symlinks (beginning with '/') cannot be normalized
2636 return if (substr($link_target, 0, 1) eq '/');
2638 # normalize link target to path from top (root) tree (dir)
2639 my $path;
2640 if ($basedir) {
2641 $path = $basedir . '/' . $link_target;
2642 } else {
2643 # we are in top (root) tree (dir)
2644 $path = $link_target;
2647 # remove //, /./, and /../
2648 my @path_parts;
2649 foreach my $part (split('/', $path)) {
2650 # discard '.' and ''
2651 next if (!$part || $part eq '.');
2652 # handle '..'
2653 if ($part eq '..') {
2654 if (@path_parts) {
2655 pop @path_parts;
2656 } else {
2657 # link leads outside repository (outside top dir)
2658 return;
2660 } else {
2661 push @path_parts, $part;
2664 $path = join('/', @path_parts);
2666 return $path;
2669 # print tree entry (row of git_tree), but without encompassing <tr> element
2670 sub git_print_tree_entry {
2671 my ($t, $basedir, $hash_base, $have_blame) = @_;
2673 my %base_key = ();
2674 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2676 # The format of a table row is: mode list link. Where mode is
2677 # the mode of the entry, list is the name of the entry, an href,
2678 # and link is the action links of the entry.
2680 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2681 if ($t->{'type'} eq "blob") {
2682 print "<td class=\"list\">" .
2683 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2684 file_name=>"$basedir$t->{'name'}", %base_key),
2685 -class => "list"}, esc_path($t->{'name'}));
2686 if (S_ISLNK(oct $t->{'mode'})) {
2687 my $link_target = git_get_link_target($t->{'hash'});
2688 if ($link_target) {
2689 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2690 if (defined $norm_target) {
2691 print " -> " .
2692 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2693 file_name=>$norm_target),
2694 -title => $norm_target}, esc_path($link_target));
2695 } else {
2696 print " -> " . esc_path($link_target);
2700 print "</td>\n";
2701 print "<td class=\"link\">";
2702 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2703 file_name=>"$basedir$t->{'name'}", %base_key)},
2704 "blob");
2705 if ($have_blame) {
2706 print " | " .
2707 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2708 file_name=>"$basedir$t->{'name'}", %base_key)},
2709 "blame");
2711 if (defined $hash_base) {
2712 print " | " .
2713 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2714 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2715 "history");
2717 print " | " .
2718 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2719 file_name=>"$basedir$t->{'name'}")},
2720 "raw");
2721 print "</td>\n";
2723 } elsif ($t->{'type'} eq "tree") {
2724 print "<td class=\"list\">";
2725 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2726 file_name=>"$basedir$t->{'name'}", %base_key)},
2727 esc_path($t->{'name'}));
2728 print "</td>\n";
2729 print "<td class=\"link\">";
2730 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2731 file_name=>"$basedir$t->{'name'}", %base_key)},
2732 "tree");
2733 if (defined $hash_base) {
2734 print " | " .
2735 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2736 file_name=>"$basedir$t->{'name'}")},
2737 "history");
2739 print "</td>\n";
2740 } else {
2741 # unknown object: we can only present history for it
2742 # (this includes 'commit' object, i.e. submodule support)
2743 print "<td class=\"list\">" .
2744 esc_path($t->{'name'}) .
2745 "</td>\n";
2746 print "<td class=\"link\">";
2747 if (defined $hash_base) {
2748 print $cgi->a({-href => href(action=>"history",
2749 hash_base=>$hash_base,
2750 file_name=>"$basedir$t->{'name'}")},
2751 "history");
2753 print "</td>\n";
2757 ## ......................................................................
2758 ## functions printing large fragments of HTML
2760 sub fill_from_file_info {
2761 my ($diff, @parents) = @_;
2763 $diff->{'from_file'} = [ ];
2764 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2765 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2766 if ($diff->{'status'}[$i] eq 'R' ||
2767 $diff->{'status'}[$i] eq 'C') {
2768 $diff->{'from_file'}[$i] =
2769 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2773 return $diff;
2776 # parameters can be strings, or references to arrays of strings
2777 sub from_ids_eq {
2778 my ($a, $b) = @_;
2780 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2781 for (my $i = 0; $i < @$a; ++$i) {
2782 return 0 unless ($a->[$i] eq $b->[$i]);
2784 return 1;
2785 } elsif (!ref($a) && !ref($b)) {
2786 return $a eq $b;
2787 } else {
2788 return 0;
2792 sub is_deleted {
2793 my $diffinfo = shift;
2795 return $diffinfo->{'to_id'} eq ('0' x 40);
2798 sub git_difftree_body {
2799 my ($difftree, $hash, @parents) = @_;
2800 my ($parent) = $parents[0];
2801 my ($have_blame) = gitweb_check_feature('blame');
2802 print "<div class=\"list_head\">\n";
2803 if ($#{$difftree} > 10) {
2804 print(($#{$difftree} + 1) . " files changed:\n");
2806 print "</div>\n";
2808 print "<table class=\"" .
2809 (@parents > 1 ? "combined " : "") .
2810 "diff_tree\">\n";
2812 # header only for combined diff in 'commitdiff' view
2813 my $has_header = @parents > 1 && $action eq 'commitdiff';
2814 if ($has_header) {
2815 # table header
2816 print "<thead><tr>\n" .
2817 "<th></th><th></th>\n"; # filename, patchN link
2818 for (my $i = 0; $i < @parents; $i++) {
2819 my $par = $parents[$i];
2820 print "<th>" .
2821 $cgi->a({-href => href(action=>"commitdiff",
2822 hash=>$hash, hash_parent=>$par),
2823 -title => 'commitdiff to parent number ' .
2824 ($i+1) . ': ' . substr($par,0,7)},
2825 $i+1) .
2826 "&nbsp;</th>\n";
2828 print "</tr></thead>\n<tbody>\n";
2831 my $alternate = 1;
2832 my $patchno = 0;
2833 foreach my $line (@{$difftree}) {
2834 my $diff;
2835 if (ref($line) eq "HASH") {
2836 # pre-parsed (or generated by hand)
2837 $diff = $line;
2838 } else {
2839 $diff = parse_difftree_raw_line($line);
2842 if ($alternate) {
2843 print "<tr class=\"dark\">\n";
2844 } else {
2845 print "<tr class=\"light\">\n";
2847 $alternate ^= 1;
2849 if (exists $diff->{'nparents'}) { # combined diff
2851 fill_from_file_info($diff, @parents)
2852 unless exists $diff->{'from_file'};
2854 if (!is_deleted($diff)) {
2855 # file exists in the result (child) commit
2856 print "<td>" .
2857 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2858 file_name=>$diff->{'to_file'},
2859 hash_base=>$hash),
2860 -class => "list"}, esc_path($diff->{'to_file'})) .
2861 "</td>\n";
2862 } else {
2863 print "<td>" .
2864 esc_path($diff->{'to_file'}) .
2865 "</td>\n";
2868 if ($action eq 'commitdiff') {
2869 # link to patch
2870 $patchno++;
2871 print "<td class=\"link\">" .
2872 $cgi->a({-href => "#patch$patchno"}, "patch") .
2873 " | " .
2874 "</td>\n";
2877 my $has_history = 0;
2878 my $not_deleted = 0;
2879 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2880 my $hash_parent = $parents[$i];
2881 my $from_hash = $diff->{'from_id'}[$i];
2882 my $from_path = $diff->{'from_file'}[$i];
2883 my $status = $diff->{'status'}[$i];
2885 $has_history ||= ($status ne 'A');
2886 $not_deleted ||= ($status ne 'D');
2888 if ($status eq 'A') {
2889 print "<td class=\"link\" align=\"right\"> | </td>\n";
2890 } elsif ($status eq 'D') {
2891 print "<td class=\"link\">" .
2892 $cgi->a({-href => href(action=>"blob",
2893 hash_base=>$hash,
2894 hash=>$from_hash,
2895 file_name=>$from_path)},
2896 "blob" . ($i+1)) .
2897 " | </td>\n";
2898 } else {
2899 if ($diff->{'to_id'} eq $from_hash) {
2900 print "<td class=\"link nochange\">";
2901 } else {
2902 print "<td class=\"link\">";
2904 print $cgi->a({-href => href(action=>"blobdiff",
2905 hash=>$diff->{'to_id'},
2906 hash_parent=>$from_hash,
2907 hash_base=>$hash,
2908 hash_parent_base=>$hash_parent,
2909 file_name=>$diff->{'to_file'},
2910 file_parent=>$from_path)},
2911 "diff" . ($i+1)) .
2912 " | </td>\n";
2916 print "<td class=\"link\">";
2917 if ($not_deleted) {
2918 print $cgi->a({-href => href(action=>"blob",
2919 hash=>$diff->{'to_id'},
2920 file_name=>$diff->{'to_file'},
2921 hash_base=>$hash)},
2922 "blob");
2923 print " | " if ($has_history);
2925 if ($has_history) {
2926 print $cgi->a({-href => href(action=>"history",
2927 file_name=>$diff->{'to_file'},
2928 hash_base=>$hash)},
2929 "history");
2931 print "</td>\n";
2933 print "</tr>\n";
2934 next; # instead of 'else' clause, to avoid extra indent
2936 # else ordinary diff
2938 my ($to_mode_oct, $to_mode_str, $to_file_type);
2939 my ($from_mode_oct, $from_mode_str, $from_file_type);
2940 if ($diff->{'to_mode'} ne ('0' x 6)) {
2941 $to_mode_oct = oct $diff->{'to_mode'};
2942 if (S_ISREG($to_mode_oct)) { # only for regular file
2943 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2945 $to_file_type = file_type($diff->{'to_mode'});
2947 if ($diff->{'from_mode'} ne ('0' x 6)) {
2948 $from_mode_oct = oct $diff->{'from_mode'};
2949 if (S_ISREG($to_mode_oct)) { # only for regular file
2950 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2952 $from_file_type = file_type($diff->{'from_mode'});
2955 if ($diff->{'status'} eq "A") { # created
2956 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2957 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2958 $mode_chng .= "]</span>";
2959 print "<td>";
2960 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2961 hash_base=>$hash, file_name=>$diff->{'file'}),
2962 -class => "list"}, esc_path($diff->{'file'}));
2963 print "</td>\n";
2964 print "<td>$mode_chng</td>\n";
2965 print "<td class=\"link\">";
2966 if ($action eq 'commitdiff') {
2967 # link to patch
2968 $patchno++;
2969 print $cgi->a({-href => "#patch$patchno"}, "patch");
2970 print " | ";
2972 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2973 hash_base=>$hash, file_name=>$diff->{'file'})},
2974 "blob");
2975 print "</td>\n";
2977 } elsif ($diff->{'status'} eq "D") { # deleted
2978 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2979 print "<td>";
2980 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2981 hash_base=>$parent, file_name=>$diff->{'file'}),
2982 -class => "list"}, esc_path($diff->{'file'}));
2983 print "</td>\n";
2984 print "<td>$mode_chng</td>\n";
2985 print "<td class=\"link\">";
2986 if ($action eq 'commitdiff') {
2987 # link to patch
2988 $patchno++;
2989 print $cgi->a({-href => "#patch$patchno"}, "patch");
2990 print " | ";
2992 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2993 hash_base=>$parent, file_name=>$diff->{'file'})},
2994 "blob") . " | ";
2995 if ($have_blame) {
2996 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2997 file_name=>$diff->{'file'})},
2998 "blame") . " | ";
3000 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3001 file_name=>$diff->{'file'})},
3002 "history");
3003 print "</td>\n";
3005 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3006 my $mode_chnge = "";
3007 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3008 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3009 if ($from_file_type ne $to_file_type) {
3010 $mode_chnge .= " from $from_file_type to $to_file_type";
3012 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3013 if ($from_mode_str && $to_mode_str) {
3014 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3015 } elsif ($to_mode_str) {
3016 $mode_chnge .= " mode: $to_mode_str";
3019 $mode_chnge .= "]</span>\n";
3021 print "<td>";
3022 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3023 hash_base=>$hash, file_name=>$diff->{'file'}),
3024 -class => "list"}, esc_path($diff->{'file'}));
3025 print "</td>\n";
3026 print "<td>$mode_chnge</td>\n";
3027 print "<td class=\"link\">";
3028 if ($action eq 'commitdiff') {
3029 # link to patch
3030 $patchno++;
3031 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3032 " | ";
3033 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3034 # "commit" view and modified file (not onlu mode changed)
3035 print $cgi->a({-href => href(action=>"blobdiff",
3036 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3037 hash_base=>$hash, hash_parent_base=>$parent,
3038 file_name=>$diff->{'file'})},
3039 "diff") .
3040 " | ";
3042 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3043 hash_base=>$hash, file_name=>$diff->{'file'})},
3044 "blob") . " | ";
3045 if ($have_blame) {
3046 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3047 file_name=>$diff->{'file'})},
3048 "blame") . " | ";
3050 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3051 file_name=>$diff->{'file'})},
3052 "history");
3053 print "</td>\n";
3055 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3056 my %status_name = ('R' => 'moved', 'C' => 'copied');
3057 my $nstatus = $status_name{$diff->{'status'}};
3058 my $mode_chng = "";
3059 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3060 # mode also for directories, so we cannot use $to_mode_str
3061 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3063 print "<td>" .
3064 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3065 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3066 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3067 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3068 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3069 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3070 -class => "list"}, esc_path($diff->{'from_file'})) .
3071 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3072 "<td class=\"link\">";
3073 if ($action eq 'commitdiff') {
3074 # link to patch
3075 $patchno++;
3076 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3077 " | ";
3078 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3079 # "commit" view and modified file (not only pure rename or copy)
3080 print $cgi->a({-href => href(action=>"blobdiff",
3081 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3082 hash_base=>$hash, hash_parent_base=>$parent,
3083 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3084 "diff") .
3085 " | ";
3087 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3088 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3089 "blob") . " | ";
3090 if ($have_blame) {
3091 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3092 file_name=>$diff->{'to_file'})},
3093 "blame") . " | ";
3095 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3096 file_name=>$diff->{'to_file'})},
3097 "history");
3098 print "</td>\n";
3100 } # we should not encounter Unmerged (U) or Unknown (X) status
3101 print "</tr>\n";
3103 print "</tbody>" if $has_header;
3104 print "</table>\n";
3107 sub git_patchset_body {
3108 my ($fd, $difftree, $hash, @hash_parents) = @_;
3109 my ($hash_parent) = $hash_parents[0];
3111 my $patch_idx = 0;
3112 my $patch_number = 0;
3113 my $patch_line;
3114 my $diffinfo;
3115 my (%from, %to);
3116 my $patches_per_difftree_line = 1;
3118 print "<div class=\"patchset\">\n";
3120 # skip to first patch
3121 while ($patch_line = <$fd>) {
3122 chomp $patch_line;
3124 last if ($patch_line =~ m/^diff /);
3127 PATCH:
3128 while ($patch_line) {
3130 # check if current patch belong to current raw line
3131 # and parse raw git-diff line if needed
3132 if ($patches_per_difftree_line > 1) {
3133 # this is continuation of a split patch
3134 print "<div class=\"patch cont\">\n";
3135 $patches_per_difftree_line--;
3136 } else {
3137 # advance raw git-diff output if needed
3138 $patch_idx++ if defined $diffinfo;
3140 # compact combined diff output can have some patches skipped
3141 # find which patch (using pathname of result) we are at now
3142 my $to_name;
3143 if ($patch_line =~ m!^diff --cc "?(.*)"?$!) {
3144 $to_name = $1;
3147 do {
3148 # read and prepare patch information
3149 if (ref($difftree->[$patch_idx]) eq "HASH") {
3150 # pre-parsed (or generated by hand)
3151 $diffinfo = $difftree->[$patch_idx];
3152 } else {
3153 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3156 # check if current raw line has no patch (it got simplified)
3157 if (defined $to_name && $to_name ne $diffinfo->{'to_file'}) {
3158 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3159 format_diff_cc_simplified($diffinfo, @hash_parents) .
3160 "</div>\n"; # class="patch"
3162 $patch_idx++;
3163 $patch_number++;
3165 } until (!defined $to_name || $to_name eq $diffinfo->{'to_file'} ||
3166 $patch_idx > $#$difftree);
3168 # modifies %from, %to hashes
3169 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3170 if ($diffinfo->{'nparents'}) {
3171 # combined diff
3172 $from{'file'} = [];
3173 $from{'href'} = [];
3174 fill_from_file_info($diffinfo, @hash_parents)
3175 unless exists $diffinfo->{'from_file'};
3176 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3177 $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
3178 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3179 $from{'href'}[$i] = href(action=>"blob",
3180 hash_base=>$hash_parents[$i],
3181 hash=>$diffinfo->{'from_id'}[$i],
3182 file_name=>$from{'file'}[$i]);
3183 } else {
3184 $from{'href'}[$i] = undef;
3187 } else {
3188 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
3189 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3190 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3191 hash=>$diffinfo->{'from_id'},
3192 file_name=>$from{'file'});
3193 } else {
3194 delete $from{'href'};
3198 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
3199 if (!is_deleted($diffinfo)) { # file exists in result
3200 $to{'href'} = href(action=>"blob", hash_base=>$hash,
3201 hash=>$diffinfo->{'to_id'},
3202 file_name=>$to{'file'});
3203 } else {
3204 delete $to{'href'};
3206 # this is first patch for raw difftree line with $patch_idx index
3207 # we index @$difftree array from 0, but number patches from 1
3208 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3210 # typechange diff with 'T' status has two patches per one raw line
3211 if ($diffinfo->{'status_str'} =~ /T/) {
3212 $patches_per_difftree_line = 2;
3216 # git diff header
3217 #assert($patch_line =~ m/^diff /) if DEBUG;
3218 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3219 $patch_number++;
3220 # print "git diff" header
3221 print format_git_diff_header_line($patch_line, $diffinfo,
3222 \%from, \%to);
3224 # print extended diff header
3225 print "<div class=\"diff extended_header\">\n";
3226 EXTENDED_HEADER:
3227 while ($patch_line = <$fd>) {
3228 chomp $patch_line;
3230 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3232 print format_extended_diff_header_line($patch_line, $diffinfo,
3233 \%from, \%to);
3235 print "</div>\n"; # class="diff extended_header"
3237 # from-file/to-file diff header
3238 if (! $patch_line) {
3239 print "</div>\n"; # class="patch"
3240 last PATCH;
3242 next PATCH if ($patch_line =~ m/^diff /);
3243 #assert($patch_line =~ m/^---/) if DEBUG;
3245 my $last_patch_line = $patch_line;
3246 $patch_line = <$fd>;
3247 chomp $patch_line;
3248 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3250 print format_diff_from_to_header($last_patch_line, $patch_line,
3251 $diffinfo, \%from, \%to,
3252 @hash_parents);
3254 # the patch itself
3255 LINE:
3256 while ($patch_line = <$fd>) {
3257 chomp $patch_line;
3259 next PATCH if ($patch_line =~ m/^diff /);
3261 print format_diff_line($patch_line, \%from, \%to);
3264 } continue {
3265 print "</div>\n"; # class="patch"
3268 # for compact combined (--cc) format, with chunk and patch simpliciaction
3269 # patchset might be empty, but there might be unprocessed raw lines
3270 for ($patch_idx++ if $patch_number > 0;
3271 $patch_idx < @$difftree;
3272 $patch_idx++) {
3273 # read and prepare patch information
3274 if (ref($difftree->[$patch_idx]) eq "HASH") {
3275 # pre-parsed (or generated by hand)
3276 $diffinfo = $difftree->[$patch_idx];
3277 } else {
3278 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3281 # generate anchor for "patch" links in difftree / whatchanged part
3282 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3283 format_diff_cc_simplified($diffinfo, @hash_parents) .
3284 "</div>\n"; # class="patch"
3286 $patch_number++;
3289 if ($patch_number == 0) {
3290 if (@hash_parents > 1) {
3291 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3292 } else {
3293 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3297 print "</div>\n"; # class="patchset"
3300 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3302 sub git_project_list_body {
3303 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3305 my ($check_forks) = gitweb_check_feature('forks');
3307 my @projects;
3308 foreach my $pr (@$projlist) {
3309 my (@aa) = git_get_last_activity($pr->{'path'});
3310 unless (@aa) {
3311 next;
3313 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3314 if (!defined $pr->{'descr'}) {
3315 my $descr = git_get_project_description($pr->{'path'}) || "";
3316 $pr->{'descr_long'} = to_utf8($descr);
3317 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3319 if (!defined $pr->{'owner'}) {
3320 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3322 if ($check_forks) {
3323 my $pname = $pr->{'path'};
3324 if (($pname =~ s/\.git$//) &&
3325 ($pname !~ /\/$/) &&
3326 (-d "$projectroot/$pname")) {
3327 $pr->{'forks'} = "-d $projectroot/$pname";
3329 else {
3330 $pr->{'forks'} = 0;
3333 push @projects, $pr;
3336 $order ||= $default_projects_order;
3337 $from = 0 unless defined $from;
3338 $to = $#projects if (!defined $to || $#projects < $to);
3340 print "<table class=\"project_list\">\n";
3341 unless ($no_header) {
3342 print "<tr>\n";
3343 if ($check_forks) {
3344 print "<th></th>\n";
3346 if ($order eq "project") {
3347 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3348 print "<th>Project</th>\n";
3349 } else {
3350 print "<th>" .
3351 $cgi->a({-href => href(project=>undef, order=>'project'),
3352 -class => "header"}, "Project") .
3353 "</th>\n";
3355 if ($order eq "descr") {
3356 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3357 print "<th>Description</th>\n";
3358 } else {
3359 print "<th>" .
3360 $cgi->a({-href => href(project=>undef, order=>'descr'),
3361 -class => "header"}, "Description") .
3362 "</th>\n";
3364 if ($order eq "owner") {
3365 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3366 print "<th>Owner</th>\n";
3367 } else {
3368 print "<th>" .
3369 $cgi->a({-href => href(project=>undef, order=>'owner'),
3370 -class => "header"}, "Owner") .
3371 "</th>\n";
3373 if ($order eq "age") {
3374 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3375 print "<th>Last Change</th>\n";
3376 } else {
3377 print "<th>" .
3378 $cgi->a({-href => href(project=>undef, order=>'age'),
3379 -class => "header"}, "Last Change") .
3380 "</th>\n";
3382 print "<th></th>\n" .
3383 "</tr>\n";
3385 my $alternate = 1;
3386 for (my $i = $from; $i <= $to; $i++) {
3387 my $pr = $projects[$i];
3388 if ($alternate) {
3389 print "<tr class=\"dark\">\n";
3390 } else {
3391 print "<tr class=\"light\">\n";
3393 $alternate ^= 1;
3394 if ($check_forks) {
3395 print "<td>";
3396 if ($pr->{'forks'}) {
3397 print "<!-- $pr->{'forks'} -->\n";
3398 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3400 print "</td>\n";
3402 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3403 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3404 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3405 -class => "list", -title => $pr->{'descr_long'}},
3406 esc_html($pr->{'descr'})) . "</td>\n" .
3407 "<td><i>" . esc_html(chop_str($pr->{'owner'}, 15)) . "</i></td>\n";
3408 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3409 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3410 "<td class=\"link\">" .
3411 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3412 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3413 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3414 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3415 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3416 "</td>\n" .
3417 "</tr>\n";
3419 if (defined $extra) {
3420 print "<tr>\n";
3421 if ($check_forks) {
3422 print "<td></td>\n";
3424 print "<td colspan=\"5\">$extra</td>\n" .
3425 "</tr>\n";
3427 print "</table>\n";
3430 sub git_shortlog_body {
3431 # uses global variable $project
3432 my ($commitlist, $from, $to, $refs, $extra) = @_;
3434 $from = 0 unless defined $from;
3435 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3437 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3438 my $alternate = 1;
3439 for (my $i = $from; $i <= $to; $i++) {
3440 my %co = %{$commitlist->[$i]};
3441 my $commit = $co{'id'};
3442 my $ref = format_ref_marker($refs, $commit);
3443 if ($alternate) {
3444 print "<tr class=\"dark\">\n";
3445 } else {
3446 print "<tr class=\"light\">\n";
3448 $alternate ^= 1;
3449 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3450 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3451 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3452 "<td>";
3453 print format_subject_html($co{'title'}, $co{'title_short'},
3454 href(action=>"commit", hash=>$commit), $ref);
3455 print "</td>\n" .
3456 "<td class=\"link\">" .
3457 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3458 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3459 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3460 my $snapshot_links = format_snapshot_links($commit);
3461 if (defined $snapshot_links) {
3462 print " | " . $snapshot_links;
3464 print "</td>\n" .
3465 "</tr>\n";
3467 if (defined $extra) {
3468 print "<tr>\n" .
3469 "<td colspan=\"4\">$extra</td>\n" .
3470 "</tr>\n";
3472 print "</table>\n";
3475 sub git_history_body {
3476 # Warning: assumes constant type (blob or tree) during history
3477 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3479 $from = 0 unless defined $from;
3480 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3482 print "<table class=\"history\" cellspacing=\"0\">\n";
3483 my $alternate = 1;
3484 for (my $i = $from; $i <= $to; $i++) {
3485 my %co = %{$commitlist->[$i]};
3486 if (!%co) {
3487 next;
3489 my $commit = $co{'id'};
3491 my $ref = format_ref_marker($refs, $commit);
3493 if ($alternate) {
3494 print "<tr class=\"dark\">\n";
3495 } else {
3496 print "<tr class=\"light\">\n";
3498 $alternate ^= 1;
3499 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3500 # shortlog uses chop_str($co{'author_name'}, 10)
3501 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3502 "<td>";
3503 # originally git_history used chop_str($co{'title'}, 50)
3504 print format_subject_html($co{'title'}, $co{'title_short'},
3505 href(action=>"commit", hash=>$commit), $ref);
3506 print "</td>\n" .
3507 "<td class=\"link\">" .
3508 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3509 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3511 if ($ftype eq 'blob') {
3512 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3513 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3514 if (defined $blob_current && defined $blob_parent &&
3515 $blob_current ne $blob_parent) {
3516 print " | " .
3517 $cgi->a({-href => href(action=>"blobdiff",
3518 hash=>$blob_current, hash_parent=>$blob_parent,
3519 hash_base=>$hash_base, hash_parent_base=>$commit,
3520 file_name=>$file_name)},
3521 "diff to current");
3524 print "</td>\n" .
3525 "</tr>\n";
3527 if (defined $extra) {
3528 print "<tr>\n" .
3529 "<td colspan=\"4\">$extra</td>\n" .
3530 "</tr>\n";
3532 print "</table>\n";
3535 sub git_tags_body {
3536 # uses global variable $project
3537 my ($taglist, $from, $to, $extra) = @_;
3538 $from = 0 unless defined $from;
3539 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3541 print "<table class=\"tags\" cellspacing=\"0\">\n";
3542 my $alternate = 1;
3543 for (my $i = $from; $i <= $to; $i++) {
3544 my $entry = $taglist->[$i];
3545 my %tag = %$entry;
3546 my $comment = $tag{'subject'};
3547 my $comment_short;
3548 if (defined $comment) {
3549 $comment_short = chop_str($comment, 30, 5);
3551 if ($alternate) {
3552 print "<tr class=\"dark\">\n";
3553 } else {
3554 print "<tr class=\"light\">\n";
3556 $alternate ^= 1;
3557 if (defined $tag{'age'}) {
3558 print "<td><i>$tag{'age'}</i></td>\n";
3559 } else {
3560 print "<td></td>\n";
3562 print "<td>" .
3563 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3564 -class => "list name"}, esc_html($tag{'name'})) .
3565 "</td>\n" .
3566 "<td>";
3567 if (defined $comment) {
3568 print format_subject_html($comment, $comment_short,
3569 href(action=>"tag", hash=>$tag{'id'}));
3571 print "</td>\n" .
3572 "<td class=\"selflink\">";
3573 if ($tag{'type'} eq "tag") {
3574 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3575 } else {
3576 print "&nbsp;";
3578 print "</td>\n" .
3579 "<td class=\"link\">" . " | " .
3580 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3581 if ($tag{'reftype'} eq "commit") {
3582 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3583 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3584 } elsif ($tag{'reftype'} eq "blob") {
3585 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3587 print "</td>\n" .
3588 "</tr>";
3590 if (defined $extra) {
3591 print "<tr>\n" .
3592 "<td colspan=\"5\">$extra</td>\n" .
3593 "</tr>\n";
3595 print "</table>\n";
3598 sub git_heads_body {
3599 # uses global variable $project
3600 my ($headlist, $head, $from, $to, $extra) = @_;
3601 $from = 0 unless defined $from;
3602 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3604 print "<table class=\"heads\" cellspacing=\"0\">\n";
3605 my $alternate = 1;
3606 for (my $i = $from; $i <= $to; $i++) {
3607 my $entry = $headlist->[$i];
3608 my %ref = %$entry;
3609 my $curr = $ref{'id'} eq $head;
3610 if ($alternate) {
3611 print "<tr class=\"dark\">\n";
3612 } else {
3613 print "<tr class=\"light\">\n";
3615 $alternate ^= 1;
3616 print "<td><i>$ref{'age'}</i></td>\n" .
3617 ($curr ? "<td class=\"current_head\">" : "<td>") .
3618 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3619 -class => "list name"},esc_html($ref{'name'})) .
3620 "</td>\n" .
3621 "<td class=\"link\">" .
3622 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3623 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3624 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3625 "</td>\n" .
3626 "</tr>";
3628 if (defined $extra) {
3629 print "<tr>\n" .
3630 "<td colspan=\"3\">$extra</td>\n" .
3631 "</tr>\n";
3633 print "</table>\n";
3636 sub git_search_grep_body {
3637 my ($commitlist, $from, $to, $extra) = @_;
3638 $from = 0 unless defined $from;
3639 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3641 print "<table class=\"grep\" cellspacing=\"0\">\n";
3642 my $alternate = 1;
3643 for (my $i = $from; $i <= $to; $i++) {
3644 my %co = %{$commitlist->[$i]};
3645 if (!%co) {
3646 next;
3648 my $commit = $co{'id'};
3649 if ($alternate) {
3650 print "<tr class=\"dark\">\n";
3651 } else {
3652 print "<tr class=\"light\">\n";
3654 $alternate ^= 1;
3655 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3656 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3657 "<td>" .
3658 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3659 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3660 my $comment = $co{'comment'};
3661 foreach my $line (@$comment) {
3662 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3663 my $lead = esc_html($1) || "";
3664 $lead = chop_str($lead, 30, 10);
3665 my $match = esc_html($2) || "";
3666 my $trail = esc_html($3) || "";
3667 $trail = chop_str($trail, 30, 10);
3668 my $text = "$lead<span class=\"match\">$match</span>$trail";
3669 print chop_str($text, 80, 5) . "<br/>\n";
3672 print "</td>\n" .
3673 "<td class=\"link\">" .
3674 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3675 " | " .
3676 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3677 print "</td>\n" .
3678 "</tr>\n";
3680 if (defined $extra) {
3681 print "<tr>\n" .
3682 "<td colspan=\"3\">$extra</td>\n" .
3683 "</tr>\n";
3685 print "</table>\n";
3688 ## ======================================================================
3689 ## ======================================================================
3690 ## actions
3692 sub git_project_list {
3693 my $order = $cgi->param('o');
3694 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3695 die_error(undef, "Unknown order parameter");
3698 my @list = git_get_projects_list();
3699 if (!@list) {
3700 die_error(undef, "No projects found");
3703 git_header_html();
3704 if (-f $home_text) {
3705 print "<div class=\"index_include\">\n";
3706 open (my $fd, $home_text);
3707 print <$fd>;
3708 close $fd;
3709 print "</div>\n";
3711 git_project_list_body(\@list, $order);
3712 git_footer_html();
3715 sub git_forks {
3716 my $order = $cgi->param('o');
3717 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3718 die_error(undef, "Unknown order parameter");
3721 my @list = git_get_projects_list($project);
3722 if (!@list) {
3723 die_error(undef, "No forks found");
3726 git_header_html();
3727 git_print_page_nav('','');
3728 git_print_header_div('summary', "$project forks");
3729 git_project_list_body(\@list, $order);
3730 git_footer_html();
3733 sub git_project_index {
3734 my @projects = git_get_projects_list($project);
3736 print $cgi->header(
3737 -type => 'text/plain',
3738 -charset => 'utf-8',
3739 -content_disposition => 'inline; filename="index.aux"');
3741 foreach my $pr (@projects) {
3742 if (!exists $pr->{'owner'}) {
3743 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
3746 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3747 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3748 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3749 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3750 $path =~ s/ /\+/g;
3751 $owner =~ s/ /\+/g;
3753 print "$path $owner\n";
3757 sub git_summary {
3758 my $descr = git_get_project_description($project) || "none";
3759 my %co = parse_commit("HEAD");
3760 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3761 my $head = $co{'id'};
3763 my $owner = git_get_project_owner($project);
3765 my $refs = git_get_references();
3766 # These get_*_list functions return one more to allow us to see if
3767 # there are more ...
3768 my @taglist = git_get_tags_list(16);
3769 my @headlist = git_get_heads_list(16);
3770 my @forklist;
3771 my ($check_forks) = gitweb_check_feature('forks');
3773 if ($check_forks) {
3774 @forklist = git_get_projects_list($project);
3777 git_header_html();
3778 git_print_page_nav('summary','', $head);
3780 print "<div class=\"title\">&nbsp;</div>\n";
3781 print "<table cellspacing=\"0\">\n" .
3782 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3783 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
3784 if (defined $cd{'rfc2822'}) {
3785 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3788 # use per project git URL list in $projectroot/$project/cloneurl
3789 # or make project git URL from git base URL and project name
3790 my $url_tag = "URL";
3791 my @url_list = git_get_project_url_list($project);
3792 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3793 foreach my $git_url (@url_list) {
3794 next unless $git_url;
3795 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3796 $url_tag = "";
3798 print "</table>\n";
3800 if (-s "$projectroot/$project/README.html") {
3801 if (open my $fd, "$projectroot/$project/README.html") {
3802 print "<div class=\"title\">readme</div>\n";
3803 print $_ while (<$fd>);
3804 close $fd;
3808 # we need to request one more than 16 (0..15) to check if
3809 # those 16 are all
3810 my @commitlist = $head ? parse_commits($head, 17) : ();
3811 if (@commitlist) {
3812 git_print_header_div('shortlog');
3813 git_shortlog_body(\@commitlist, 0, 15, $refs,
3814 $#commitlist <= 15 ? undef :
3815 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3818 if (@taglist) {
3819 git_print_header_div('tags');
3820 git_tags_body(\@taglist, 0, 15,
3821 $#taglist <= 15 ? undef :
3822 $cgi->a({-href => href(action=>"tags")}, "..."));
3825 if (@headlist) {
3826 git_print_header_div('heads');
3827 git_heads_body(\@headlist, $head, 0, 15,
3828 $#headlist <= 15 ? undef :
3829 $cgi->a({-href => href(action=>"heads")}, "..."));
3832 if (@forklist) {
3833 git_print_header_div('forks');
3834 git_project_list_body(\@forklist, undef, 0, 15,
3835 $#forklist <= 15 ? undef :
3836 $cgi->a({-href => href(action=>"forks")}, "..."),
3837 'noheader');
3840 git_footer_html();
3843 sub git_tag {
3844 my $head = git_get_head_hash($project);
3845 git_header_html();
3846 git_print_page_nav('','', $head,undef,$head);
3847 my %tag = parse_tag($hash);
3849 if (! %tag) {
3850 die_error(undef, "Unknown tag object");
3853 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3854 print "<div class=\"title_text\">\n" .
3855 "<table cellspacing=\"0\">\n" .
3856 "<tr>\n" .
3857 "<td>object</td>\n" .
3858 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3859 $tag{'object'}) . "</td>\n" .
3860 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3861 $tag{'type'}) . "</td>\n" .
3862 "</tr>\n";
3863 if (defined($tag{'author'})) {
3864 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3865 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3866 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3867 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3868 "</td></tr>\n";
3870 print "</table>\n\n" .
3871 "</div>\n";
3872 print "<div class=\"page_body\">";
3873 my $comment = $tag{'comment'};
3874 foreach my $line (@$comment) {
3875 chomp $line;
3876 print esc_html($line, -nbsp=>1) . "<br/>\n";
3878 print "</div>\n";
3879 git_footer_html();
3882 sub git_blame2 {
3883 my $fd;
3884 my $ftype;
3886 my ($have_blame) = gitweb_check_feature('blame');
3887 if (!$have_blame) {
3888 die_error('403 Permission denied', "Permission denied");
3890 die_error('404 Not Found', "File name not defined") if (!$file_name);
3891 $hash_base ||= git_get_head_hash($project);
3892 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3893 my %co = parse_commit($hash_base)
3894 or die_error(undef, "Reading commit failed");
3895 if (!defined $hash) {
3896 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3897 or die_error(undef, "Error looking up file");
3899 $ftype = git_get_type($hash);
3900 if ($ftype !~ "blob") {
3901 die_error('400 Bad Request', "Object is not a blob");
3903 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3904 $file_name, $hash_base)
3905 or die_error(undef, "Open git-blame failed");
3906 git_header_html();
3907 my $formats_nav =
3908 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3909 "blob") .
3910 " | " .
3911 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3912 "history") .
3913 " | " .
3914 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3915 "HEAD");
3916 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3917 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3918 git_print_page_path($file_name, $ftype, $hash_base);
3919 my @rev_color = (qw(light2 dark2));
3920 my $num_colors = scalar(@rev_color);
3921 my $current_color = 0;
3922 my $last_rev;
3923 print <<HTML;
3924 <div class="page_body">
3925 <table class="blame">
3926 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3927 HTML
3928 my %metainfo = ();
3929 while (1) {
3930 $_ = <$fd>;
3931 last unless defined $_;
3932 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3933 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3934 if (!exists $metainfo{$full_rev}) {
3935 $metainfo{$full_rev} = {};
3937 my $meta = $metainfo{$full_rev};
3938 while (<$fd>) {
3939 last if (s/^\t//);
3940 if (/^(\S+) (.*)$/) {
3941 $meta->{$1} = $2;
3944 my $data = $_;
3945 chomp $data;
3946 my $rev = substr($full_rev, 0, 8);
3947 my $author = $meta->{'author'};
3948 my %date = parse_date($meta->{'author-time'},
3949 $meta->{'author-tz'});
3950 my $date = $date{'iso-tz'};
3951 if ($group_size) {
3952 $current_color = ++$current_color % $num_colors;
3954 print "<tr class=\"$rev_color[$current_color]\">\n";
3955 if ($group_size) {
3956 print "<td class=\"sha1\"";
3957 print " title=\"". esc_html($author) . ", $date\"";
3958 print " rowspan=\"$group_size\"" if ($group_size > 1);
3959 print ">";
3960 print $cgi->a({-href => href(action=>"commit",
3961 hash=>$full_rev,
3962 file_name=>$file_name)},
3963 esc_html($rev));
3964 print "</td>\n";
3966 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3967 or die_error(undef, "Open git-rev-parse failed");
3968 my $parent_commit = <$dd>;
3969 close $dd;
3970 chomp($parent_commit);
3971 my $blamed = href(action => 'blame',
3972 file_name => $meta->{'filename'},
3973 hash_base => $parent_commit);
3974 print "<td class=\"linenr\">";
3975 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3976 -id => "l$lineno",
3977 -class => "linenr" },
3978 esc_html($lineno));
3979 print "</td>";
3980 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3981 print "</tr>\n";
3983 print "</table>\n";
3984 print "</div>";
3985 close $fd
3986 or print "Reading blob failed\n";
3987 git_footer_html();
3990 sub git_blame {
3991 my $fd;
3993 my ($have_blame) = gitweb_check_feature('blame');
3994 if (!$have_blame) {
3995 die_error('403 Permission denied', "Permission denied");
3997 die_error('404 Not Found', "File name not defined") if (!$file_name);
3998 $hash_base ||= git_get_head_hash($project);
3999 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4000 my %co = parse_commit($hash_base)
4001 or die_error(undef, "Reading commit failed");
4002 if (!defined $hash) {
4003 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4004 or die_error(undef, "Error lookup file");
4006 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
4007 or die_error(undef, "Open git-annotate failed");
4008 git_header_html();
4009 my $formats_nav =
4010 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4011 "blob") .
4012 " | " .
4013 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4014 "history") .
4015 " | " .
4016 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4017 "HEAD");
4018 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4019 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4020 git_print_page_path($file_name, 'blob', $hash_base);
4021 print "<div class=\"page_body\">\n";
4022 print <<HTML;
4023 <table class="blame">
4024 <tr>
4025 <th>Commit</th>
4026 <th>Age</th>
4027 <th>Author</th>
4028 <th>Line</th>
4029 <th>Data</th>
4030 </tr>
4031 HTML
4032 my @line_class = (qw(light dark));
4033 my $line_class_len = scalar (@line_class);
4034 my $line_class_num = $#line_class;
4035 while (my $line = <$fd>) {
4036 my $long_rev;
4037 my $short_rev;
4038 my $author;
4039 my $time;
4040 my $lineno;
4041 my $data;
4042 my $age;
4043 my $age_str;
4044 my $age_class;
4046 chomp $line;
4047 $line_class_num = ($line_class_num + 1) % $line_class_len;
4049 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
4050 $long_rev = $1;
4051 $author = $2;
4052 $time = $3;
4053 $lineno = $4;
4054 $data = $5;
4055 } else {
4056 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
4057 next;
4059 $short_rev = substr ($long_rev, 0, 8);
4060 $age = time () - $time;
4061 $age_str = age_string ($age);
4062 $age_str =~ s/ /&nbsp;/g;
4063 $age_class = age_class($age);
4064 $author = esc_html ($author);
4065 $author =~ s/ /&nbsp;/g;
4067 $data = untabify($data);
4068 $data = esc_html ($data);
4070 print <<HTML;
4071 <tr class="$line_class[$line_class_num]">
4072 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
4073 <td class="$age_class">$age_str</td>
4074 <td>$author</td>
4075 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4076 <td class="pre">$data</td>
4077 </tr>
4078 HTML
4079 } # while (my $line = <$fd>)
4080 print "</table>\n\n";
4081 close $fd
4082 or print "Reading blob failed.\n";
4083 print "</div>";
4084 git_footer_html();
4087 sub git_tags {
4088 my $head = git_get_head_hash($project);
4089 git_header_html();
4090 git_print_page_nav('','', $head,undef,$head);
4091 git_print_header_div('summary', $project);
4093 my @tagslist = git_get_tags_list();
4094 if (@tagslist) {
4095 git_tags_body(\@tagslist);
4097 git_footer_html();
4100 sub git_heads {
4101 my $head = git_get_head_hash($project);
4102 git_header_html();
4103 git_print_page_nav('','', $head,undef,$head);
4104 git_print_header_div('summary', $project);
4106 my @headslist = git_get_heads_list();
4107 if (@headslist) {
4108 git_heads_body(\@headslist, $head);
4110 git_footer_html();
4113 sub git_blob_plain {
4114 my $expires;
4116 if (!defined $hash) {
4117 if (defined $file_name) {
4118 my $base = $hash_base || git_get_head_hash($project);
4119 $hash = git_get_hash_by_path($base, $file_name, "blob")
4120 or die_error(undef, "Error lookup file");
4121 } else {
4122 die_error(undef, "No file name defined");
4124 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4125 # blobs defined by non-textual hash id's can be cached
4126 $expires = "+1d";
4129 my $type = shift;
4130 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4131 or die_error(undef, "Couldn't cat $file_name, $hash");
4133 $type ||= blob_mimetype($fd, $file_name);
4135 # save as filename, even when no $file_name is given
4136 my $save_as = "$hash";
4137 if (defined $file_name) {
4138 $save_as = $file_name;
4139 } elsif ($type =~ m/^text\//) {
4140 $save_as .= '.txt';
4143 print $cgi->header(
4144 -type => "$type",
4145 -expires=>$expires,
4146 -content_disposition => 'inline; filename="' . "$save_as" . '"');
4147 undef $/;
4148 binmode STDOUT, ':raw';
4149 print <$fd>;
4150 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4151 $/ = "\n";
4152 close $fd;
4155 sub git_blob {
4156 my $expires;
4158 if (!defined $hash) {
4159 if (defined $file_name) {
4160 my $base = $hash_base || git_get_head_hash($project);
4161 $hash = git_get_hash_by_path($base, $file_name, "blob")
4162 or die_error(undef, "Error lookup file");
4163 } else {
4164 die_error(undef, "No file name defined");
4166 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4167 # blobs defined by non-textual hash id's can be cached
4168 $expires = "+1d";
4171 my ($have_blame) = gitweb_check_feature('blame');
4172 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4173 or die_error(undef, "Couldn't cat $file_name, $hash");
4174 my $mimetype = blob_mimetype($fd, $file_name);
4175 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
4176 close $fd;
4177 return git_blob_plain($mimetype);
4179 # we can have blame only for text/* mimetype
4180 $have_blame &&= ($mimetype =~ m!^text/!);
4182 git_header_html(undef, $expires);
4183 my $formats_nav = '';
4184 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4185 if (defined $file_name) {
4186 if ($have_blame) {
4187 $formats_nav .=
4188 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
4189 hash=>$hash, file_name=>$file_name)},
4190 "blame") .
4191 " | ";
4193 $formats_nav .=
4194 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4195 hash=>$hash, file_name=>$file_name)},
4196 "history") .
4197 " | " .
4198 $cgi->a({-href => href(action=>"blob_plain",
4199 hash=>$hash, file_name=>$file_name)},
4200 "raw") .
4201 " | " .
4202 $cgi->a({-href => href(action=>"blob",
4203 hash_base=>"HEAD", file_name=>$file_name)},
4204 "HEAD");
4205 } else {
4206 $formats_nav .=
4207 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
4209 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4210 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4211 } else {
4212 print "<div class=\"page_nav\">\n" .
4213 "<br/><br/></div>\n" .
4214 "<div class=\"title\">$hash</div>\n";
4216 git_print_page_path($file_name, "blob", $hash_base);
4217 print "<div class=\"page_body\">\n";
4218 if ($mimetype =~ m!^text/!) {
4219 my $nr;
4220 while (my $line = <$fd>) {
4221 chomp $line;
4222 $nr++;
4223 $line = untabify($line);
4224 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4225 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4227 } elsif ($mimetype =~ m!^image/!) {
4228 print qq!<img type="$mimetype"!;
4229 if ($file_name) {
4230 print qq! alt="$file_name" title="$file_name"!;
4232 print qq! src="! .
4233 href(action=>"blob_plain", hash=>$hash,
4234 hash_base=>$hash_base, file_name=>$file_name) .
4235 qq!" />\n!;
4237 close $fd
4238 or print "Reading blob failed.\n";
4239 print "</div>";
4240 git_footer_html();
4243 sub git_tree {
4244 if (!defined $hash_base) {
4245 $hash_base = "HEAD";
4247 if (!defined $hash) {
4248 if (defined $file_name) {
4249 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4250 } else {
4251 $hash = $hash_base;
4254 $/ = "\0";
4255 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4256 or die_error(undef, "Open git-ls-tree failed");
4257 my @entries = map { chomp; $_ } <$fd>;
4258 close $fd or die_error(undef, "Reading tree failed");
4259 $/ = "\n";
4261 my $refs = git_get_references();
4262 my $ref = format_ref_marker($refs, $hash_base);
4263 git_header_html();
4264 my $basedir = '';
4265 my ($have_blame) = gitweb_check_feature('blame');
4266 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4267 my @views_nav = ();
4268 if (defined $file_name) {
4269 push @views_nav,
4270 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4271 hash=>$hash, file_name=>$file_name)},
4272 "history"),
4273 $cgi->a({-href => href(action=>"tree",
4274 hash_base=>"HEAD", file_name=>$file_name)},
4275 "HEAD"),
4277 my $snapshot_links = format_snapshot_links($hash);
4278 if (defined $snapshot_links) {
4279 # FIXME: Should be available when we have no hash base as well.
4280 push @views_nav, $snapshot_links;
4282 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4283 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4284 } else {
4285 undef $hash_base;
4286 print "<div class=\"page_nav\">\n";
4287 print "<br/><br/></div>\n";
4288 print "<div class=\"title\">$hash</div>\n";
4290 if (defined $file_name) {
4291 $basedir = $file_name;
4292 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4293 $basedir .= '/';
4296 git_print_page_path($file_name, 'tree', $hash_base);
4297 print "<div class=\"page_body\">\n";
4298 print "<table cellspacing=\"0\">\n";
4299 my $alternate = 1;
4300 # '..' (top directory) link if possible
4301 if (defined $hash_base &&
4302 defined $file_name && $file_name =~ m![^/]+$!) {
4303 if ($alternate) {
4304 print "<tr class=\"dark\">\n";
4305 } else {
4306 print "<tr class=\"light\">\n";
4308 $alternate ^= 1;
4310 my $up = $file_name;
4311 $up =~ s!/?[^/]+$!!;
4312 undef $up unless $up;
4313 # based on git_print_tree_entry
4314 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4315 print '<td class="list">';
4316 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4317 file_name=>$up)},
4318 "..");
4319 print "</td>\n";
4320 print "<td class=\"link\"></td>\n";
4322 print "</tr>\n";
4324 foreach my $line (@entries) {
4325 my %t = parse_ls_tree_line($line, -z => 1);
4327 if ($alternate) {
4328 print "<tr class=\"dark\">\n";
4329 } else {
4330 print "<tr class=\"light\">\n";
4332 $alternate ^= 1;
4334 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4336 print "</tr>\n";
4338 print "</table>\n" .
4339 "</div>";
4340 git_footer_html();
4343 sub git_snapshot {
4344 my @supported_fmts = gitweb_check_feature('snapshot');
4345 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4347 my $format = $cgi->param('sf');
4348 if (!@supported_fmts) {
4349 die_error('403 Permission denied', "Permission denied");
4351 # default to first supported snapshot format
4352 $format ||= $supported_fmts[0];
4353 if ($format !~ m/^[a-z0-9]+$/) {
4354 die_error(undef, "Invalid snapshot format parameter");
4355 } elsif (!exists($known_snapshot_formats{$format})) {
4356 die_error(undef, "Unknown snapshot format");
4357 } elsif (!grep($_ eq $format, @supported_fmts)) {
4358 die_error(undef, "Unsupported snapshot format");
4361 if (!defined $hash) {
4362 $hash = git_get_head_hash($project);
4365 my $git_command = git_cmd_str();
4366 my $name = $project;
4367 $name =~ s,([^/])/*\.git$,$1,;
4368 $name = basename($name);
4369 my $filename = to_utf8($name);
4370 $name =~ s/\047/\047\\\047\047/g;
4371 my $cmd;
4372 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4373 $cmd = "$git_command archive " .
4374 "--format=$known_snapshot_formats{$format}{'format'} " .
4375 "--prefix=\'$name\'/ $hash";
4376 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4377 $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
4380 print $cgi->header(
4381 -type => $known_snapshot_formats{$format}{'type'},
4382 -content_disposition => 'inline; filename="' . "$filename" . '"',
4383 -status => '200 OK');
4385 open my $fd, "-|", $cmd
4386 or die_error(undef, "Execute git-archive failed");
4387 binmode STDOUT, ':raw';
4388 print <$fd>;
4389 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4390 close $fd;
4393 sub git_log {
4394 my $head = git_get_head_hash($project);
4395 if (!defined $hash) {
4396 $hash = $head;
4398 if (!defined $page) {
4399 $page = 0;
4401 my $refs = git_get_references();
4403 my @commitlist = parse_commits($hash, 101, (100 * $page));
4405 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
4407 git_header_html();
4408 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4410 if (!@commitlist) {
4411 my %co = parse_commit($hash);
4413 git_print_header_div('summary', $project);
4414 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4416 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4417 for (my $i = 0; $i <= $to; $i++) {
4418 my %co = %{$commitlist[$i]};
4419 next if !%co;
4420 my $commit = $co{'id'};
4421 my $ref = format_ref_marker($refs, $commit);
4422 my %ad = parse_date($co{'author_epoch'});
4423 git_print_header_div('commit',
4424 "<span class=\"age\">$co{'age_string'}</span>" .
4425 esc_html($co{'title'}) . $ref,
4426 $commit);
4427 print "<div class=\"title_text\">\n" .
4428 "<div class=\"log_link\">\n" .
4429 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4430 " | " .
4431 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4432 " | " .
4433 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4434 "<br/>\n" .
4435 "</div>\n" .
4436 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4437 "</div>\n";
4439 print "<div class=\"log_body\">\n";
4440 git_print_log($co{'comment'}, -final_empty_line=> 1);
4441 print "</div>\n";
4443 if ($#commitlist >= 100) {
4444 print "<div class=\"page_nav\">\n";
4445 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4446 -accesskey => "n", -title => "Alt-n"}, "next");
4447 print "</div>\n";
4449 git_footer_html();
4452 sub git_commit {
4453 $hash ||= $hash_base || "HEAD";
4454 my %co = parse_commit($hash);
4455 if (!%co) {
4456 die_error(undef, "Unknown commit object");
4458 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4459 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4461 my $parent = $co{'parent'};
4462 my $parents = $co{'parents'}; # listref
4464 # we need to prepare $formats_nav before any parameter munging
4465 my $formats_nav;
4466 if (!defined $parent) {
4467 # --root commitdiff
4468 $formats_nav .= '(initial)';
4469 } elsif (@$parents == 1) {
4470 # single parent commit
4471 $formats_nav .=
4472 '(parent: ' .
4473 $cgi->a({-href => href(action=>"commit",
4474 hash=>$parent)},
4475 esc_html(substr($parent, 0, 7))) .
4476 ')';
4477 } else {
4478 # merge commit
4479 $formats_nav .=
4480 '(merge: ' .
4481 join(' ', map {
4482 $cgi->a({-href => href(action=>"commit",
4483 hash=>$_)},
4484 esc_html(substr($_, 0, 7)));
4485 } @$parents ) .
4486 ')';
4489 if (!defined $parent) {
4490 $parent = "--root";
4492 my @difftree;
4493 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4494 @diff_opts,
4495 (@$parents <= 1 ? $parent : '-c'),
4496 $hash, "--"
4497 or die_error(undef, "Open git-diff-tree failed");
4498 @difftree = map { chomp; $_ } <$fd>;
4499 close $fd or die_error(undef, "Reading git-diff-tree failed");
4501 # non-textual hash id's can be cached
4502 my $expires;
4503 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4504 $expires = "+1d";
4506 my $refs = git_get_references();
4507 my $ref = format_ref_marker($refs, $co{'id'});
4509 git_header_html(undef, $expires);
4510 git_print_page_nav('commit', '',
4511 $hash, $co{'tree'}, $hash,
4512 $formats_nav);
4514 if (defined $co{'parent'}) {
4515 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4516 } else {
4517 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4519 print "<div class=\"title_text\">\n" .
4520 "<table cellspacing=\"0\">\n";
4521 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4522 "<tr>" .
4523 "<td></td><td> $ad{'rfc2822'}";
4524 if ($ad{'hour_local'} < 6) {
4525 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4526 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4527 } else {
4528 printf(" (%02d:%02d %s)",
4529 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4531 print "</td>" .
4532 "</tr>\n";
4533 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4534 print "<tr><td></td><td> $cd{'rfc2822'}" .
4535 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4536 "</td></tr>\n";
4537 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4538 print "<tr>" .
4539 "<td>tree</td>" .
4540 "<td class=\"sha1\">" .
4541 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4542 class => "list"}, $co{'tree'}) .
4543 "</td>" .
4544 "<td class=\"link\">" .
4545 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4546 "tree");
4547 my $snapshot_links = format_snapshot_links($hash);
4548 if (defined $snapshot_links) {
4549 print " | " . $snapshot_links;
4551 print "</td>" .
4552 "</tr>\n";
4554 foreach my $par (@$parents) {
4555 print "<tr>" .
4556 "<td>parent</td>" .
4557 "<td class=\"sha1\">" .
4558 $cgi->a({-href => href(action=>"commit", hash=>$par),
4559 class => "list"}, $par) .
4560 "</td>" .
4561 "<td class=\"link\">" .
4562 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4563 " | " .
4564 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4565 "</td>" .
4566 "</tr>\n";
4568 print "</table>".
4569 "</div>\n";
4571 print "<div class=\"page_body\">\n";
4572 git_print_log($co{'comment'});
4573 print "</div>\n";
4575 git_difftree_body(\@difftree, $hash, @$parents);
4577 git_footer_html();
4580 sub git_object {
4581 # object is defined by:
4582 # - hash or hash_base alone
4583 # - hash_base and file_name
4584 my $type;
4586 # - hash or hash_base alone
4587 if ($hash || ($hash_base && !defined $file_name)) {
4588 my $object_id = $hash || $hash_base;
4590 my $git_command = git_cmd_str();
4591 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4592 or die_error('404 Not Found', "Object does not exist");
4593 $type = <$fd>;
4594 chomp $type;
4595 close $fd
4596 or die_error('404 Not Found', "Object does not exist");
4598 # - hash_base and file_name
4599 } elsif ($hash_base && defined $file_name) {
4600 $file_name =~ s,/+$,,;
4602 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4603 or die_error('404 Not Found', "Base object does not exist");
4605 # here errors should not hapen
4606 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4607 or die_error(undef, "Open git-ls-tree failed");
4608 my $line = <$fd>;
4609 close $fd;
4611 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4612 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4613 die_error('404 Not Found', "File or directory for given base does not exist");
4615 $type = $2;
4616 $hash = $3;
4617 } else {
4618 die_error('404 Not Found', "Not enough information to find object");
4621 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4622 hash=>$hash, hash_base=>$hash_base,
4623 file_name=>$file_name),
4624 -status => '302 Found');
4627 sub git_blobdiff {
4628 my $format = shift || 'html';
4630 my $fd;
4631 my @difftree;
4632 my %diffinfo;
4633 my $expires;
4635 # preparing $fd and %diffinfo for git_patchset_body
4636 # new style URI
4637 if (defined $hash_base && defined $hash_parent_base) {
4638 if (defined $file_name) {
4639 # read raw output
4640 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4641 $hash_parent_base, $hash_base,
4642 "--", (defined $file_parent ? $file_parent : ()), $file_name
4643 or die_error(undef, "Open git-diff-tree failed");
4644 @difftree = map { chomp; $_ } <$fd>;
4645 close $fd
4646 or die_error(undef, "Reading git-diff-tree failed");
4647 @difftree
4648 or die_error('404 Not Found', "Blob diff not found");
4650 } elsif (defined $hash &&
4651 $hash =~ /[0-9a-fA-F]{40}/) {
4652 # try to find filename from $hash
4654 # read filtered raw output
4655 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4656 $hash_parent_base, $hash_base, "--"
4657 or die_error(undef, "Open git-diff-tree failed");
4658 @difftree =
4659 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4660 # $hash == to_id
4661 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4662 map { chomp; $_ } <$fd>;
4663 close $fd
4664 or die_error(undef, "Reading git-diff-tree failed");
4665 @difftree
4666 or die_error('404 Not Found', "Blob diff not found");
4668 } else {
4669 die_error('404 Not Found', "Missing one of the blob diff parameters");
4672 if (@difftree > 1) {
4673 die_error('404 Not Found', "Ambiguous blob diff specification");
4676 %diffinfo = parse_difftree_raw_line($difftree[0]);
4677 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4678 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4680 $hash_parent ||= $diffinfo{'from_id'};
4681 $hash ||= $diffinfo{'to_id'};
4683 # non-textual hash id's can be cached
4684 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4685 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4686 $expires = '+1d';
4689 # open patch output
4690 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4691 '-p', ($format eq 'html' ? "--full-index" : ()),
4692 $hash_parent_base, $hash_base,
4693 "--", (defined $file_parent ? $file_parent : ()), $file_name
4694 or die_error(undef, "Open git-diff-tree failed");
4697 # old/legacy style URI
4698 if (!%diffinfo && # if new style URI failed
4699 defined $hash && defined $hash_parent) {
4700 # fake git-diff-tree raw output
4701 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4702 $diffinfo{'from_id'} = $hash_parent;
4703 $diffinfo{'to_id'} = $hash;
4704 if (defined $file_name) {
4705 if (defined $file_parent) {
4706 $diffinfo{'status'} = '2';
4707 $diffinfo{'from_file'} = $file_parent;
4708 $diffinfo{'to_file'} = $file_name;
4709 } else { # assume not renamed
4710 $diffinfo{'status'} = '1';
4711 $diffinfo{'from_file'} = $file_name;
4712 $diffinfo{'to_file'} = $file_name;
4714 } else { # no filename given
4715 $diffinfo{'status'} = '2';
4716 $diffinfo{'from_file'} = $hash_parent;
4717 $diffinfo{'to_file'} = $hash;
4720 # non-textual hash id's can be cached
4721 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4722 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4723 $expires = '+1d';
4726 # open patch output
4727 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4728 '-p', ($format eq 'html' ? "--full-index" : ()),
4729 $hash_parent, $hash, "--"
4730 or die_error(undef, "Open git-diff failed");
4731 } else {
4732 die_error('404 Not Found', "Missing one of the blob diff parameters")
4733 unless %diffinfo;
4736 # header
4737 if ($format eq 'html') {
4738 my $formats_nav =
4739 $cgi->a({-href => href(action=>"blobdiff_plain",
4740 hash=>$hash, hash_parent=>$hash_parent,
4741 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4742 file_name=>$file_name, file_parent=>$file_parent)},
4743 "raw");
4744 git_header_html(undef, $expires);
4745 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4746 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4747 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4748 } else {
4749 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4750 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4752 if (defined $file_name) {
4753 git_print_page_path($file_name, "blob", $hash_base);
4754 } else {
4755 print "<div class=\"page_path\"></div>\n";
4758 } elsif ($format eq 'plain') {
4759 print $cgi->header(
4760 -type => 'text/plain',
4761 -charset => 'utf-8',
4762 -expires => $expires,
4763 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4765 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4767 } else {
4768 die_error(undef, "Unknown blobdiff format");
4771 # patch
4772 if ($format eq 'html') {
4773 print "<div class=\"page_body\">\n";
4775 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4776 close $fd;
4778 print "</div>\n"; # class="page_body"
4779 git_footer_html();
4781 } else {
4782 while (my $line = <$fd>) {
4783 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4784 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4786 print $line;
4788 last if $line =~ m!^\+\+\+!;
4790 local $/ = undef;
4791 print <$fd>;
4792 close $fd;
4796 sub git_blobdiff_plain {
4797 git_blobdiff('plain');
4800 sub git_commitdiff {
4801 my $format = shift || 'html';
4802 $hash ||= $hash_base || "HEAD";
4803 my %co = parse_commit($hash);
4804 if (!%co) {
4805 die_error(undef, "Unknown commit object");
4808 # choose format for commitdiff for merge
4809 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4810 $hash_parent = '--cc';
4812 # we need to prepare $formats_nav before almost any parameter munging
4813 my $formats_nav;
4814 if ($format eq 'html') {
4815 $formats_nav =
4816 $cgi->a({-href => href(action=>"commitdiff_plain",
4817 hash=>$hash, hash_parent=>$hash_parent)},
4818 "raw");
4820 if (defined $hash_parent &&
4821 $hash_parent ne '-c' && $hash_parent ne '--cc') {
4822 # commitdiff with two commits given
4823 my $hash_parent_short = $hash_parent;
4824 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4825 $hash_parent_short = substr($hash_parent, 0, 7);
4827 $formats_nav .=
4828 ' (from';
4829 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4830 if ($co{'parents'}[$i] eq $hash_parent) {
4831 $formats_nav .= ' parent ' . ($i+1);
4832 last;
4835 $formats_nav .= ': ' .
4836 $cgi->a({-href => href(action=>"commitdiff",
4837 hash=>$hash_parent)},
4838 esc_html($hash_parent_short)) .
4839 ')';
4840 } elsif (!$co{'parent'}) {
4841 # --root commitdiff
4842 $formats_nav .= ' (initial)';
4843 } elsif (scalar @{$co{'parents'}} == 1) {
4844 # single parent commit
4845 $formats_nav .=
4846 ' (parent: ' .
4847 $cgi->a({-href => href(action=>"commitdiff",
4848 hash=>$co{'parent'})},
4849 esc_html(substr($co{'parent'}, 0, 7))) .
4850 ')';
4851 } else {
4852 # merge commit
4853 if ($hash_parent eq '--cc') {
4854 $formats_nav .= ' | ' .
4855 $cgi->a({-href => href(action=>"commitdiff",
4856 hash=>$hash, hash_parent=>'-c')},
4857 'combined');
4858 } else { # $hash_parent eq '-c'
4859 $formats_nav .= ' | ' .
4860 $cgi->a({-href => href(action=>"commitdiff",
4861 hash=>$hash, hash_parent=>'--cc')},
4862 'compact');
4864 $formats_nav .=
4865 ' (merge: ' .
4866 join(' ', map {
4867 $cgi->a({-href => href(action=>"commitdiff",
4868 hash=>$_)},
4869 esc_html(substr($_, 0, 7)));
4870 } @{$co{'parents'}} ) .
4871 ')';
4875 my $hash_parent_param = $hash_parent;
4876 if (!defined $hash_parent_param) {
4877 # --cc for multiple parents, --root for parentless
4878 $hash_parent_param =
4879 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
4882 # read commitdiff
4883 my $fd;
4884 my @difftree;
4885 if ($format eq 'html') {
4886 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4887 "--no-commit-id", "--patch-with-raw", "--full-index",
4888 $hash_parent_param, $hash, "--"
4889 or die_error(undef, "Open git-diff-tree failed");
4891 while (my $line = <$fd>) {
4892 chomp $line;
4893 # empty line ends raw part of diff-tree output
4894 last unless $line;
4895 push @difftree, scalar parse_difftree_raw_line($line);
4898 } elsif ($format eq 'plain') {
4899 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4900 '-p', $hash_parent_param, $hash, "--"
4901 or die_error(undef, "Open git-diff-tree failed");
4903 } else {
4904 die_error(undef, "Unknown commitdiff format");
4907 # non-textual hash id's can be cached
4908 my $expires;
4909 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4910 $expires = "+1d";
4913 # write commit message
4914 if ($format eq 'html') {
4915 my $refs = git_get_references();
4916 my $ref = format_ref_marker($refs, $co{'id'});
4918 git_header_html(undef, $expires);
4919 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4920 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4921 git_print_authorship(\%co);
4922 print "<div class=\"page_body\">\n";
4923 if (@{$co{'comment'}} > 1) {
4924 print "<div class=\"log\">\n";
4925 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4926 print "</div>\n"; # class="log"
4929 } elsif ($format eq 'plain') {
4930 my $refs = git_get_references("tags");
4931 my $tagname = git_get_rev_name_tags($hash);
4932 my $filename = basename($project) . "-$hash.patch";
4934 print $cgi->header(
4935 -type => 'text/plain',
4936 -charset => 'utf-8',
4937 -expires => $expires,
4938 -content_disposition => 'inline; filename="' . "$filename" . '"');
4939 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4940 print <<TEXT;
4941 From: $co{'author'}
4942 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4943 Subject: $co{'title'}
4944 TEXT
4945 print "X-Git-Tag: $tagname\n" if $tagname;
4946 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4948 foreach my $line (@{$co{'comment'}}) {
4949 print "$line\n";
4951 print "---\n\n";
4954 # write patch
4955 if ($format eq 'html') {
4956 my $use_parents = !defined $hash_parent ||
4957 $hash_parent eq '-c' || $hash_parent eq '--cc';
4958 git_difftree_body(\@difftree, $hash,
4959 $use_parents ? @{$co{'parents'}} : $hash_parent);
4960 print "<br/>\n";
4962 git_patchset_body($fd, \@difftree, $hash,
4963 $use_parents ? @{$co{'parents'}} : $hash_parent);
4964 close $fd;
4965 print "</div>\n"; # class="page_body"
4966 git_footer_html();
4968 } elsif ($format eq 'plain') {
4969 local $/ = undef;
4970 print <$fd>;
4971 close $fd
4972 or print "Reading git-diff-tree failed\n";
4976 sub git_commitdiff_plain {
4977 git_commitdiff('plain');
4980 sub git_history {
4981 if (!defined $hash_base) {
4982 $hash_base = git_get_head_hash($project);
4984 if (!defined $page) {
4985 $page = 0;
4987 my $ftype;
4988 my %co = parse_commit($hash_base);
4989 if (!%co) {
4990 die_error(undef, "Unknown commit object");
4993 my $refs = git_get_references();
4994 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4996 if (!defined $hash && defined $file_name) {
4997 $hash = git_get_hash_by_path($hash_base, $file_name);
4999 if (defined $hash) {
5000 $ftype = git_get_type($hash);
5003 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
5005 my $paging_nav = '';
5006 if ($page > 0) {
5007 $paging_nav .=
5008 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5009 file_name=>$file_name)},
5010 "first");
5011 $paging_nav .= " &sdot; " .
5012 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5013 file_name=>$file_name, page=>$page-1),
5014 -accesskey => "p", -title => "Alt-p"}, "prev");
5015 } else {
5016 $paging_nav .= "first";
5017 $paging_nav .= " &sdot; prev";
5019 if ($#commitlist >= 100) {
5020 $paging_nav .= " &sdot; " .
5021 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5022 file_name=>$file_name, page=>$page+1),
5023 -accesskey => "n", -title => "Alt-n"}, "next");
5024 } else {
5025 $paging_nav .= " &sdot; next";
5027 my $next_link = '';
5028 if ($#commitlist >= 100) {
5029 $next_link =
5030 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5031 file_name=>$file_name, page=>$page+1),
5032 -accesskey => "n", -title => "Alt-n"}, "next");
5035 git_header_html();
5036 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5037 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5038 git_print_page_path($file_name, $ftype, $hash_base);
5040 git_history_body(\@commitlist, 0, 99,
5041 $refs, $hash_base, $ftype, $next_link);
5043 git_footer_html();
5046 sub git_search {
5047 my ($have_search) = gitweb_check_feature('search');
5048 if (!$have_search) {
5049 die_error('403 Permission denied', "Permission denied");
5051 if (!defined $searchtext) {
5052 die_error(undef, "Text field empty");
5054 if (!defined $hash) {
5055 $hash = git_get_head_hash($project);
5057 my %co = parse_commit($hash);
5058 if (!%co) {
5059 die_error(undef, "Unknown commit object");
5061 if (!defined $page) {
5062 $page = 0;
5065 $searchtype ||= 'commit';
5066 if ($searchtype eq 'pickaxe') {
5067 # pickaxe may take all resources of your box and run for several minutes
5068 # with every query - so decide by yourself how public you make this feature
5069 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5070 if (!$have_pickaxe) {
5071 die_error('403 Permission denied', "Permission denied");
5074 if ($searchtype eq 'grep') {
5075 my ($have_grep) = gitweb_check_feature('grep');
5076 if (!$have_grep) {
5077 die_error('403 Permission denied', "Permission denied");
5081 git_header_html();
5083 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5084 my $greptype;
5085 if ($searchtype eq 'commit') {
5086 $greptype = "--grep=";
5087 } elsif ($searchtype eq 'author') {
5088 $greptype = "--author=";
5089 } elsif ($searchtype eq 'committer') {
5090 $greptype = "--committer=";
5092 $greptype .= $search_regexp;
5093 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
5095 my $paging_nav = '';
5096 if ($page > 0) {
5097 $paging_nav .=
5098 $cgi->a({-href => href(action=>"search", hash=>$hash,
5099 searchtext=>$searchtext, searchtype=>$searchtype)},
5100 "first");
5101 $paging_nav .= " &sdot; " .
5102 $cgi->a({-href => href(action=>"search", hash=>$hash,
5103 searchtext=>$searchtext, searchtype=>$searchtype,
5104 page=>$page-1),
5105 -accesskey => "p", -title => "Alt-p"}, "prev");
5106 } else {
5107 $paging_nav .= "first";
5108 $paging_nav .= " &sdot; prev";
5110 if ($#commitlist >= 100) {
5111 $paging_nav .= " &sdot; " .
5112 $cgi->a({-href => href(action=>"search", hash=>$hash,
5113 searchtext=>$searchtext, searchtype=>$searchtype,
5114 page=>$page+1),
5115 -accesskey => "n", -title => "Alt-n"}, "next");
5116 } else {
5117 $paging_nav .= " &sdot; next";
5119 my $next_link = '';
5120 if ($#commitlist >= 100) {
5121 $next_link =
5122 $cgi->a({-href => href(action=>"search", hash=>$hash,
5123 searchtext=>$searchtext, searchtype=>$searchtype,
5124 page=>$page+1),
5125 -accesskey => "n", -title => "Alt-n"}, "next");
5128 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5129 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5130 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5133 if ($searchtype eq 'pickaxe') {
5134 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5135 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5137 print "<table cellspacing=\"0\">\n";
5138 my $alternate = 1;
5139 $/ = "\n";
5140 my $git_command = git_cmd_str();
5141 my $searchqtext = $searchtext;
5142 $searchqtext =~ s/'/'\\''/;
5143 open my $fd, "-|", "$git_command rev-list $hash | " .
5144 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
5145 undef %co;
5146 my @files;
5147 while (my $line = <$fd>) {
5148 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
5149 my %set;
5150 $set{'file'} = $6;
5151 $set{'from_id'} = $3;
5152 $set{'to_id'} = $4;
5153 $set{'id'} = $set{'to_id'};
5154 if ($set{'id'} =~ m/0{40}/) {
5155 $set{'id'} = $set{'from_id'};
5157 if ($set{'id'} =~ m/0{40}/) {
5158 next;
5160 push @files, \%set;
5161 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
5162 if (%co) {
5163 if ($alternate) {
5164 print "<tr class=\"dark\">\n";
5165 } else {
5166 print "<tr class=\"light\">\n";
5168 $alternate ^= 1;
5169 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5170 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
5171 "<td>" .
5172 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5173 -class => "list subject"},
5174 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
5175 while (my $setref = shift @files) {
5176 my %set = %$setref;
5177 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5178 hash=>$set{'id'}, file_name=>$set{'file'}),
5179 -class => "list"},
5180 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5181 "<br/>\n";
5183 print "</td>\n" .
5184 "<td class=\"link\">" .
5185 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5186 " | " .
5187 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5188 print "</td>\n" .
5189 "</tr>\n";
5191 %co = parse_commit($1);
5194 close $fd;
5196 print "</table>\n";
5199 if ($searchtype eq 'grep') {
5200 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5201 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5203 print "<table cellspacing=\"0\">\n";
5204 my $alternate = 1;
5205 my $matches = 0;
5206 $/ = "\n";
5207 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
5208 my $lastfile = '';
5209 while (my $line = <$fd>) {
5210 chomp $line;
5211 my ($file, $lno, $ltext, $binary);
5212 last if ($matches++ > 1000);
5213 if ($line =~ /^Binary file (.+) matches$/) {
5214 $file = $1;
5215 $binary = 1;
5216 } else {
5217 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5219 if ($file ne $lastfile) {
5220 $lastfile and print "</td></tr>\n";
5221 if ($alternate++) {
5222 print "<tr class=\"dark\">\n";
5223 } else {
5224 print "<tr class=\"light\">\n";
5226 print "<td class=\"list\">".
5227 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5228 file_name=>"$file"),
5229 -class => "list"}, esc_path($file));
5230 print "</td><td>\n";
5231 $lastfile = $file;
5233 if ($binary) {
5234 print "<div class=\"binary\">Binary file</div>\n";
5235 } else {
5236 $ltext = untabify($ltext);
5237 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
5238 $ltext = esc_html($1, -nbsp=>1);
5239 $ltext .= '<span class="match">';
5240 $ltext .= esc_html($2, -nbsp=>1);
5241 $ltext .= '</span>';
5242 $ltext .= esc_html($3, -nbsp=>1);
5243 } else {
5244 $ltext = esc_html($ltext, -nbsp=>1);
5246 print "<div class=\"pre\">" .
5247 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5248 file_name=>"$file").'#l'.$lno,
5249 -class => "linenr"}, sprintf('%4i', $lno))
5250 . ' ' . $ltext . "</div>\n";
5253 if ($lastfile) {
5254 print "</td></tr>\n";
5255 if ($matches > 1000) {
5256 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5258 } else {
5259 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5261 close $fd;
5263 print "</table>\n";
5265 git_footer_html();
5268 sub git_search_help {
5269 git_header_html();
5270 git_print_page_nav('','', $hash,$hash,$hash);
5271 print <<EOT;
5272 <dl>
5273 <dt><b>commit</b></dt>
5274 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
5276 my ($have_grep) = gitweb_check_feature('grep');
5277 if ($have_grep) {
5278 print <<EOT;
5279 <dt><b>grep</b></dt>
5280 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5281 a different one) are searched for the given
5282 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5283 (POSIX extended) and the matches are listed. On large
5284 trees, this search can take a while and put some strain on the server, so please use it with
5285 some consideration.</dd>
5288 print <<EOT;
5289 <dt><b>author</b></dt>
5290 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5291 <dt><b>committer</b></dt>
5292 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5294 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5295 if ($have_pickaxe) {
5296 print <<EOT;
5297 <dt><b>pickaxe</b></dt>
5298 <dd>All commits that caused the string to appear or disappear from any file (changes that
5299 added, removed or "modified" the string) will be listed. This search can take a while and
5300 takes a lot of strain on the server, so please use it wisely.</dd>
5303 print "</dl>\n";
5304 git_footer_html();
5307 sub git_shortlog {
5308 my $head = git_get_head_hash($project);
5309 if (!defined $hash) {
5310 $hash = $head;
5312 if (!defined $page) {
5313 $page = 0;
5315 my $refs = git_get_references();
5317 my @commitlist = parse_commits($hash, 101, (100 * $page));
5319 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
5320 my $next_link = '';
5321 if ($#commitlist >= 100) {
5322 $next_link =
5323 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
5324 -accesskey => "n", -title => "Alt-n"}, "next");
5327 git_header_html();
5328 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5329 git_print_header_div('summary', $project);
5331 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5333 git_footer_html();
5336 ## ......................................................................
5337 ## feeds (RSS, Atom; OPML)
5339 sub git_feed {
5340 my $format = shift || 'atom';
5341 my ($have_blame) = gitweb_check_feature('blame');
5343 # Atom: http://www.atomenabled.org/developers/syndication/
5344 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5345 if ($format ne 'rss' && $format ne 'atom') {
5346 die_error(undef, "Unknown web feed format");
5349 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5350 my $head = $hash || 'HEAD';
5351 my @commitlist = parse_commits($head, 150, 0, undef, $file_name);
5353 my %latest_commit;
5354 my %latest_date;
5355 my $content_type = "application/$format+xml";
5356 if (defined $cgi->http('HTTP_ACCEPT') &&
5357 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5358 # browser (feed reader) prefers text/xml
5359 $content_type = 'text/xml';
5361 if (defined($commitlist[0])) {
5362 %latest_commit = %{$commitlist[0]};
5363 %latest_date = parse_date($latest_commit{'author_epoch'});
5364 print $cgi->header(
5365 -type => $content_type,
5366 -charset => 'utf-8',
5367 -last_modified => $latest_date{'rfc2822'});
5368 } else {
5369 print $cgi->header(
5370 -type => $content_type,
5371 -charset => 'utf-8');
5374 # Optimization: skip generating the body if client asks only
5375 # for Last-Modified date.
5376 return if ($cgi->request_method() eq 'HEAD');
5378 # header variables
5379 my $title = "$site_name - $project/$action";
5380 my $feed_type = 'log';
5381 if (defined $hash) {
5382 $title .= " - '$hash'";
5383 $feed_type = 'branch log';
5384 if (defined $file_name) {
5385 $title .= " :: $file_name";
5386 $feed_type = 'history';
5388 } elsif (defined $file_name) {
5389 $title .= " - $file_name";
5390 $feed_type = 'history';
5392 $title .= " $feed_type";
5393 my $descr = git_get_project_description($project);
5394 if (defined $descr) {
5395 $descr = esc_html($descr);
5396 } else {
5397 $descr = "$project " .
5398 ($format eq 'rss' ? 'RSS' : 'Atom') .
5399 " feed";
5401 my $owner = git_get_project_owner($project);
5402 $owner = esc_html($owner);
5404 #header
5405 my $alt_url;
5406 if (defined $file_name) {
5407 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5408 } elsif (defined $hash) {
5409 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5410 } else {
5411 $alt_url = href(-full=>1, action=>"summary");
5413 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5414 if ($format eq 'rss') {
5415 print <<XML;
5416 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5417 <channel>
5419 print "<title>$title</title>\n" .
5420 "<link>$alt_url</link>\n" .
5421 "<description>$descr</description>\n" .
5422 "<language>en</language>\n";
5423 } elsif ($format eq 'atom') {
5424 print <<XML;
5425 <feed xmlns="http://www.w3.org/2005/Atom">
5427 print "<title>$title</title>\n" .
5428 "<subtitle>$descr</subtitle>\n" .
5429 '<link rel="alternate" type="text/html" href="' .
5430 $alt_url . '" />' . "\n" .
5431 '<link rel="self" type="' . $content_type . '" href="' .
5432 $cgi->self_url() . '" />' . "\n" .
5433 "<id>" . href(-full=>1) . "</id>\n" .
5434 # use project owner for feed author
5435 "<author><name>$owner</name></author>\n";
5436 if (defined $favicon) {
5437 print "<icon>" . esc_url($favicon) . "</icon>\n";
5439 if (defined $logo_url) {
5440 # not twice as wide as tall: 72 x 27 pixels
5441 print "<logo>" . esc_url($logo) . "</logo>\n";
5443 if (! %latest_date) {
5444 # dummy date to keep the feed valid until commits trickle in:
5445 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5446 } else {
5447 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5451 # contents
5452 for (my $i = 0; $i <= $#commitlist; $i++) {
5453 my %co = %{$commitlist[$i]};
5454 my $commit = $co{'id'};
5455 # we read 150, we always show 30 and the ones more recent than 48 hours
5456 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5457 last;
5459 my %cd = parse_date($co{'author_epoch'});
5461 # get list of changed files
5462 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5463 $co{'parent'} || "--root",
5464 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5465 or next;
5466 my @difftree = map { chomp; $_ } <$fd>;
5467 close $fd
5468 or next;
5470 # print element (entry, item)
5471 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5472 if ($format eq 'rss') {
5473 print "<item>\n" .
5474 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5475 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5476 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5477 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5478 "<link>$co_url</link>\n" .
5479 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5480 "<content:encoded>" .
5481 "<![CDATA[\n";
5482 } elsif ($format eq 'atom') {
5483 print "<entry>\n" .
5484 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5485 "<updated>$cd{'iso-8601'}</updated>\n" .
5486 "<author>\n" .
5487 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5488 if ($co{'author_email'}) {
5489 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5491 print "</author>\n" .
5492 # use committer for contributor
5493 "<contributor>\n" .
5494 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5495 if ($co{'committer_email'}) {
5496 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5498 print "</contributor>\n" .
5499 "<published>$cd{'iso-8601'}</published>\n" .
5500 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5501 "<id>$co_url</id>\n" .
5502 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5503 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5505 my $comment = $co{'comment'};
5506 print "<pre>\n";
5507 foreach my $line (@$comment) {
5508 $line = esc_html($line);
5509 print "$line\n";
5511 print "</pre><ul>\n";
5512 foreach my $difftree_line (@difftree) {
5513 my %difftree = parse_difftree_raw_line($difftree_line);
5514 next if !$difftree{'from_id'};
5516 my $file = $difftree{'file'} || $difftree{'to_file'};
5518 print "<li>" .
5519 "[" .
5520 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5521 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5522 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5523 file_name=>$file, file_parent=>$difftree{'from_file'}),
5524 -title => "diff"}, 'D');
5525 if ($have_blame) {
5526 print $cgi->a({-href => href(-full=>1, action=>"blame",
5527 file_name=>$file, hash_base=>$commit),
5528 -title => "blame"}, 'B');
5530 # if this is not a feed of a file history
5531 if (!defined $file_name || $file_name ne $file) {
5532 print $cgi->a({-href => href(-full=>1, action=>"history",
5533 file_name=>$file, hash=>$commit),
5534 -title => "history"}, 'H');
5536 $file = esc_path($file);
5537 print "] ".
5538 "$file</li>\n";
5540 if ($format eq 'rss') {
5541 print "</ul>]]>\n" .
5542 "</content:encoded>\n" .
5543 "</item>\n";
5544 } elsif ($format eq 'atom') {
5545 print "</ul>\n</div>\n" .
5546 "</content>\n" .
5547 "</entry>\n";
5551 # end of feed
5552 if ($format eq 'rss') {
5553 print "</channel>\n</rss>\n";
5554 } elsif ($format eq 'atom') {
5555 print "</feed>\n";
5559 sub git_rss {
5560 git_feed('rss');
5563 sub git_atom {
5564 git_feed('atom');
5567 sub git_opml {
5568 my @list = git_get_projects_list();
5570 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5571 print <<XML;
5572 <?xml version="1.0" encoding="utf-8"?>
5573 <opml version="1.0">
5574 <head>
5575 <title>$site_name OPML Export</title>
5576 </head>
5577 <body>
5578 <outline text="git RSS feeds">
5581 foreach my $pr (@list) {
5582 my %proj = %$pr;
5583 my $head = git_get_head_hash($proj{'path'});
5584 if (!defined $head) {
5585 next;
5587 $git_dir = "$projectroot/$proj{'path'}";
5588 my %co = parse_commit($head);
5589 if (!%co) {
5590 next;
5593 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5594 my $rss = "$my_url?p=$proj{'path'};a=rss";
5595 my $html = "$my_url?p=$proj{'path'};a=summary";
5596 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5598 print <<XML;
5599 </outline>
5600 </body>
5601 </opml>