gitweb: Easier adding/changing parameters to current URL
[git.git] / gitweb / gitweb.perl
blobc93c546fbfa9c59c5de54d9bb9b2c788d7acf5d9
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 # fs traversing limit for getting project list
39 # the number is relative to the projectroot
40 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
42 # target of the home link on top of all pages
43 our $home_link = $my_uri || "/";
45 # string of the home link on top of all pages
46 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
48 # name of your site or organization to appear in page titles
49 # replace this with something more descriptive for clearer bookmarks
50 our $site_name = "++GITWEB_SITENAME++"
51 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
53 # filename of html text to include at top of each page
54 our $site_header = "++GITWEB_SITE_HEADER++";
55 # html text to include at home page
56 our $home_text = "++GITWEB_HOMETEXT++";
57 # filename of html text to include at bottom of each page
58 our $site_footer = "++GITWEB_SITE_FOOTER++";
60 # URI of stylesheets
61 our @stylesheets = ("++GITWEB_CSS++");
62 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
63 our $stylesheet = undef;
64 # URI of GIT logo (72x27 size)
65 our $logo = "++GITWEB_LOGO++";
66 # URI of GIT favicon, assumed to be image/png type
67 our $favicon = "++GITWEB_FAVICON++";
69 # URI and label (title) of GIT logo link
70 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
71 #our $logo_label = "git documentation";
72 our $logo_url = "http://git.or.cz/";
73 our $logo_label = "git homepage";
75 # source of projects list
76 our $projects_list = "++GITWEB_LIST++";
78 # the width (in characters) of the projects list "Description" column
79 our $projects_list_description_width = 25;
81 # default order of projects list
82 # valid values are none, project, descr, owner, and age
83 our $default_projects_order = "project";
85 # show repository only if this file exists
86 # (only effective if this variable evaluates to true)
87 our $export_ok = "++GITWEB_EXPORT_OK++";
89 # only allow viewing of repositories also shown on the overview page
90 our $strict_export = "++GITWEB_STRICT_EXPORT++";
92 # list of git base URLs used for URL to where fetch project from,
93 # i.e. full URL is "$git_base_url/$project"
94 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
96 # default blob_plain mimetype and default charset for text/plain blob
97 our $default_blob_plain_mimetype = 'text/plain';
98 our $default_text_plain_charset = undef;
100 # file to use for guessing MIME types before trying /etc/mime.types
101 # (relative to the current git repository)
102 our $mimetypes_file = undef;
104 # assume this charset if line contains non-UTF-8 characters;
105 # it should be valid encoding (see Encoding::Supported(3pm) for list),
106 # for which encoding all byte sequences are valid, for example
107 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
108 # could be even 'utf-8' for the old behavior)
109 our $fallback_encoding = 'latin1';
111 # rename detection options for git-diff and git-diff-tree
112 # - default is '-M', with the cost proportional to
113 # (number of removed files) * (number of new files).
114 # - more costly is '-C' (which implies '-M'), with the cost proportional to
115 # (number of changed files + number of removed files) * (number of new files)
116 # - even more costly is '-C', '--find-copies-harder' with cost
117 # (number of files in the original tree) * (number of new files)
118 # - one might want to include '-B' option, e.g. '-B', '-M'
119 our @diff_opts = ('-M'); # taken from git_commit
121 # information about snapshot formats that gitweb is capable of serving
122 our %known_snapshot_formats = (
123 # name => {
124 # 'display' => display name,
125 # 'type' => mime type,
126 # 'suffix' => filename suffix,
127 # 'format' => --format for git-archive,
128 # 'compressor' => [compressor command and arguments]
129 # (array reference, optional)}
131 'tgz' => {
132 'display' => 'tar.gz',
133 'type' => 'application/x-gzip',
134 'suffix' => '.tar.gz',
135 'format' => 'tar',
136 'compressor' => ['gzip']},
138 'tbz2' => {
139 'display' => 'tar.bz2',
140 'type' => 'application/x-bzip2',
141 'suffix' => '.tar.bz2',
142 'format' => 'tar',
143 'compressor' => ['bzip2']},
145 'zip' => {
146 'display' => 'zip',
147 'type' => 'application/x-zip',
148 'suffix' => '.zip',
149 'format' => 'zip'},
152 # Aliases so we understand old gitweb.snapshot values in repository
153 # configuration.
154 our %known_snapshot_format_aliases = (
155 'gzip' => 'tgz',
156 'bzip2' => 'tbz2',
158 # backward compatibility: legacy gitweb config support
159 'x-gzip' => undef, 'gz' => undef,
160 'x-bzip2' => undef, 'bz2' => undef,
161 'x-zip' => undef, '' => undef,
164 # You define site-wide feature defaults here; override them with
165 # $GITWEB_CONFIG as necessary.
166 our %feature = (
167 # feature => {
168 # 'sub' => feature-sub (subroutine),
169 # 'override' => allow-override (boolean),
170 # 'default' => [ default options...] (array reference)}
172 # if feature is overridable (it means that allow-override has true value),
173 # then feature-sub will be called with default options as parameters;
174 # return value of feature-sub indicates if to enable specified feature
176 # if there is no 'sub' key (no feature-sub), then feature cannot be
177 # overriden
179 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
181 # Enable the 'blame' blob view, showing the last commit that modified
182 # each line in the file. This can be very CPU-intensive.
184 # To enable system wide have in $GITWEB_CONFIG
185 # $feature{'blame'}{'default'} = [1];
186 # To have project specific config enable override in $GITWEB_CONFIG
187 # $feature{'blame'}{'override'} = 1;
188 # and in project config gitweb.blame = 0|1;
189 'blame' => {
190 'sub' => \&feature_blame,
191 'override' => 0,
192 'default' => [0]},
194 # Enable the 'snapshot' link, providing a compressed archive of any
195 # tree. This can potentially generate high traffic if you have large
196 # project.
198 # Value is a list of formats defined in %known_snapshot_formats that
199 # you wish to offer.
200 # To disable system wide have in $GITWEB_CONFIG
201 # $feature{'snapshot'}{'default'} = [];
202 # To have project specific config enable override in $GITWEB_CONFIG
203 # $feature{'snapshot'}{'override'} = 1;
204 # and in project config, a comma-separated list of formats or "none"
205 # to disable. Example: gitweb.snapshot = tbz2,zip;
206 'snapshot' => {
207 'sub' => \&feature_snapshot,
208 'override' => 0,
209 'default' => ['tgz']},
211 # Enable text search, which will list the commits which match author,
212 # committer or commit text to a given string. Enabled by default.
213 # Project specific override is not supported.
214 'search' => {
215 'override' => 0,
216 'default' => [1]},
218 # Enable grep search, which will list the files in currently selected
219 # tree containing the given string. Enabled by default. This can be
220 # potentially CPU-intensive, of course.
222 # To enable system wide have in $GITWEB_CONFIG
223 # $feature{'grep'}{'default'} = [1];
224 # To have project specific config enable override in $GITWEB_CONFIG
225 # $feature{'grep'}{'override'} = 1;
226 # and in project config gitweb.grep = 0|1;
227 'grep' => {
228 'override' => 0,
229 'default' => [1]},
231 # Enable the pickaxe search, which will list the commits that modified
232 # a given string in a file. This can be practical and quite faster
233 # alternative to 'blame', but still potentially CPU-intensive.
235 # To enable system wide have in $GITWEB_CONFIG
236 # $feature{'pickaxe'}{'default'} = [1];
237 # To have project specific config enable override in $GITWEB_CONFIG
238 # $feature{'pickaxe'}{'override'} = 1;
239 # and in project config gitweb.pickaxe = 0|1;
240 'pickaxe' => {
241 'sub' => \&feature_pickaxe,
242 'override' => 0,
243 'default' => [1]},
245 # Make gitweb use an alternative format of the URLs which can be
246 # more readable and natural-looking: project name is embedded
247 # directly in the path and the query string contains other
248 # auxiliary information. All gitweb installations recognize
249 # URL in either format; this configures in which formats gitweb
250 # generates links.
252 # To enable system wide have in $GITWEB_CONFIG
253 # $feature{'pathinfo'}{'default'} = [1];
254 # Project specific override is not supported.
256 # Note that you will need to change the default location of CSS,
257 # favicon, logo and possibly other files to an absolute URL. Also,
258 # if gitweb.cgi serves as your indexfile, you will need to force
259 # $my_uri to contain the script name in your $GITWEB_CONFIG.
260 'pathinfo' => {
261 'override' => 0,
262 'default' => [0]},
264 # Make gitweb consider projects in project root subdirectories
265 # to be forks of existing projects. Given project $projname.git,
266 # projects matching $projname/*.git will not be shown in the main
267 # projects list, instead a '+' mark will be added to $projname
268 # there and a 'forks' view will be enabled for the project, listing
269 # all the forks. If project list is taken from a file, forks have
270 # to be listed after the main project.
272 # To enable system wide have in $GITWEB_CONFIG
273 # $feature{'forks'}{'default'} = [1];
274 # Project specific override is not supported.
275 'forks' => {
276 'override' => 0,
277 'default' => [0]},
280 sub gitweb_check_feature {
281 my ($name) = @_;
282 return unless exists $feature{$name};
283 my ($sub, $override, @defaults) = (
284 $feature{$name}{'sub'},
285 $feature{$name}{'override'},
286 @{$feature{$name}{'default'}});
287 if (!$override) { return @defaults; }
288 if (!defined $sub) {
289 warn "feature $name is not overrideable";
290 return @defaults;
292 return $sub->(@defaults);
295 sub feature_blame {
296 my ($val) = git_get_project_config('blame', '--bool');
298 if ($val eq 'true') {
299 return 1;
300 } elsif ($val eq 'false') {
301 return 0;
304 return $_[0];
307 sub feature_snapshot {
308 my (@fmts) = @_;
310 my ($val) = git_get_project_config('snapshot');
312 if ($val) {
313 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
316 return @fmts;
319 sub feature_grep {
320 my ($val) = git_get_project_config('grep', '--bool');
322 if ($val eq 'true') {
323 return (1);
324 } elsif ($val eq 'false') {
325 return (0);
328 return ($_[0]);
331 sub feature_pickaxe {
332 my ($val) = git_get_project_config('pickaxe', '--bool');
334 if ($val eq 'true') {
335 return (1);
336 } elsif ($val eq 'false') {
337 return (0);
340 return ($_[0]);
343 # checking HEAD file with -e is fragile if the repository was
344 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
345 # and then pruned.
346 sub check_head_link {
347 my ($dir) = @_;
348 my $headfile = "$dir/HEAD";
349 return ((-e $headfile) ||
350 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
353 sub check_export_ok {
354 my ($dir) = @_;
355 return (check_head_link($dir) &&
356 (!$export_ok || -e "$dir/$export_ok"));
359 # process alternate names for backward compatibility
360 # filter out unsupported (unknown) snapshot formats
361 sub filter_snapshot_fmts {
362 my @fmts = @_;
364 @fmts = map {
365 exists $known_snapshot_format_aliases{$_} ?
366 $known_snapshot_format_aliases{$_} : $_} @fmts;
367 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
371 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
372 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
374 # version of the core git binary
375 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
377 $projects_list ||= $projectroot;
379 # ======================================================================
380 # input validation and dispatch
381 our $action = $cgi->param('a');
382 if (defined $action) {
383 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
384 die_error(undef, "Invalid action parameter");
388 # parameters which are pathnames
389 our $project = $cgi->param('p');
390 if (defined $project) {
391 if (!validate_pathname($project) ||
392 !(-d "$projectroot/$project") ||
393 !check_head_link("$projectroot/$project") ||
394 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
395 ($strict_export && !project_in_list($project))) {
396 undef $project;
397 die_error(undef, "No such project");
401 our $file_name = $cgi->param('f');
402 if (defined $file_name) {
403 if (!validate_pathname($file_name)) {
404 die_error(undef, "Invalid file parameter");
408 our $file_parent = $cgi->param('fp');
409 if (defined $file_parent) {
410 if (!validate_pathname($file_parent)) {
411 die_error(undef, "Invalid file parent parameter");
415 # parameters which are refnames
416 our $hash = $cgi->param('h');
417 if (defined $hash) {
418 if (!validate_refname($hash)) {
419 die_error(undef, "Invalid hash parameter");
423 our $hash_parent = $cgi->param('hp');
424 if (defined $hash_parent) {
425 if (!validate_refname($hash_parent)) {
426 die_error(undef, "Invalid hash parent parameter");
430 our $hash_base = $cgi->param('hb');
431 if (defined $hash_base) {
432 if (!validate_refname($hash_base)) {
433 die_error(undef, "Invalid hash base parameter");
437 my %allowed_options = (
438 "--no-merges" => [ qw(rss atom log shortlog history) ],
441 our @extra_options = $cgi->param('opt');
442 if (defined @extra_options) {
443 foreach my $opt (@extra_options) {
444 if (not exists $allowed_options{$opt}) {
445 die_error(undef, "Invalid option parameter");
447 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
448 die_error(undef, "Invalid option parameter for this action");
453 our $hash_parent_base = $cgi->param('hpb');
454 if (defined $hash_parent_base) {
455 if (!validate_refname($hash_parent_base)) {
456 die_error(undef, "Invalid hash parent base parameter");
460 # other parameters
461 our $page = $cgi->param('pg');
462 if (defined $page) {
463 if ($page =~ m/[^0-9]/) {
464 die_error(undef, "Invalid page parameter");
468 our $searchtype = $cgi->param('st');
469 if (defined $searchtype) {
470 if ($searchtype =~ m/[^a-z]/) {
471 die_error(undef, "Invalid searchtype parameter");
475 our $searchtext = $cgi->param('s');
476 our $search_regexp;
477 if (defined $searchtext) {
478 if (length($searchtext) < 2) {
479 die_error(undef, "At least two characters are required for search parameter");
481 $search_regexp = quotemeta $searchtext;
484 # now read PATH_INFO and use it as alternative to parameters
485 sub evaluate_path_info {
486 return if defined $project;
487 my $path_info = $ENV{"PATH_INFO"};
488 return if !$path_info;
489 $path_info =~ s,^/+,,;
490 return if !$path_info;
491 # find which part of PATH_INFO is project
492 $project = $path_info;
493 $project =~ s,/+$,,;
494 while ($project && !check_head_link("$projectroot/$project")) {
495 $project =~ s,/*[^/]*$,,;
497 # validate project
498 $project = validate_pathname($project);
499 if (!$project ||
500 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
501 ($strict_export && !project_in_list($project))) {
502 undef $project;
503 return;
505 # do not change any parameters if an action is given using the query string
506 return if $action;
507 $path_info =~ s,^$project/*,,;
508 my ($refname, $pathname) = split(/:/, $path_info, 2);
509 if (defined $pathname) {
510 # we got "project.git/branch:filename" or "project.git/branch:dir/"
511 # we could use git_get_type(branch:pathname), but it needs $git_dir
512 $pathname =~ s,^/+,,;
513 if (!$pathname || substr($pathname, -1) eq "/") {
514 $action ||= "tree";
515 $pathname =~ s,/$,,;
516 } else {
517 $action ||= "blob_plain";
519 $hash_base ||= validate_refname($refname);
520 $file_name ||= validate_pathname($pathname);
521 } elsif (defined $refname) {
522 # we got "project.git/branch"
523 $action ||= "shortlog";
524 $hash ||= validate_refname($refname);
527 evaluate_path_info();
529 # path to the current git repository
530 our $git_dir;
531 $git_dir = "$projectroot/$project" if $project;
533 # dispatch
534 my %actions = (
535 "blame" => \&git_blame2,
536 "blobdiff" => \&git_blobdiff,
537 "blobdiff_plain" => \&git_blobdiff_plain,
538 "blob" => \&git_blob,
539 "blob_plain" => \&git_blob_plain,
540 "commitdiff" => \&git_commitdiff,
541 "commitdiff_plain" => \&git_commitdiff_plain,
542 "commit" => \&git_commit,
543 "forks" => \&git_forks,
544 "heads" => \&git_heads,
545 "history" => \&git_history,
546 "log" => \&git_log,
547 "rss" => \&git_rss,
548 "atom" => \&git_atom,
549 "search" => \&git_search,
550 "search_help" => \&git_search_help,
551 "shortlog" => \&git_shortlog,
552 "summary" => \&git_summary,
553 "tag" => \&git_tag,
554 "tags" => \&git_tags,
555 "tree" => \&git_tree,
556 "snapshot" => \&git_snapshot,
557 "object" => \&git_object,
558 # those below don't need $project
559 "opml" => \&git_opml,
560 "project_list" => \&git_project_list,
561 "project_index" => \&git_project_index,
564 if (!defined $action) {
565 if (defined $hash) {
566 $action = git_get_type($hash);
567 } elsif (defined $hash_base && defined $file_name) {
568 $action = git_get_type("$hash_base:$file_name");
569 } elsif (defined $project) {
570 $action = 'summary';
571 } else {
572 $action = 'project_list';
575 if (!defined($actions{$action})) {
576 die_error(undef, "Unknown action");
578 if ($action !~ m/^(opml|project_list|project_index)$/ &&
579 !$project) {
580 die_error(undef, "Project needed");
582 $actions{$action}->();
583 exit;
585 ## ======================================================================
586 ## action links
588 sub href(%) {
589 my %params = @_;
590 # default is to use -absolute url() i.e. $my_uri
591 my $href = $params{-full} ? $my_url : $my_uri;
593 # XXX: Warning: If you touch this, check the search form for updating,
594 # too.
596 my @mapping = (
597 project => "p",
598 action => "a",
599 file_name => "f",
600 file_parent => "fp",
601 hash => "h",
602 hash_parent => "hp",
603 hash_base => "hb",
604 hash_parent_base => "hpb",
605 page => "pg",
606 order => "o",
607 searchtext => "s",
608 searchtype => "st",
609 snapshot_format => "sf",
610 extra_options => "opt",
612 my %mapping = @mapping;
614 if ($params{-replay}) {
615 while (my ($name, $symbol) = each %mapping) {
616 if (!exists $params{$name}) {
617 # to allow for multivalued params we use arrayref form
618 $params{$name} = [ $cgi->param($symbol) ];
623 $params{'project'} = $project unless exists $params{'project'};
625 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
626 if ($use_pathinfo) {
627 # use PATH_INFO for project name
628 $href .= "/$params{'project'}" if defined $params{'project'};
629 delete $params{'project'};
631 # Summary just uses the project path URL
632 if (defined $params{'action'} && $params{'action'} eq 'summary') {
633 delete $params{'action'};
637 # now encode the parameters explicitly
638 my @result = ();
639 for (my $i = 0; $i < @mapping; $i += 2) {
640 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
641 if (defined $params{$name}) {
642 if (ref($params{$name}) eq "ARRAY") {
643 foreach my $par (@{$params{$name}}) {
644 push @result, $symbol . "=" . esc_param($par);
646 } else {
647 push @result, $symbol . "=" . esc_param($params{$name});
651 $href .= "?" . join(';', @result) if scalar @result;
653 return $href;
657 ## ======================================================================
658 ## validation, quoting/unquoting and escaping
660 sub validate_pathname {
661 my $input = shift || return undef;
663 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
664 # at the beginning, at the end, and between slashes.
665 # also this catches doubled slashes
666 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
667 return undef;
669 # no null characters
670 if ($input =~ m!\0!) {
671 return undef;
673 return $input;
676 sub validate_refname {
677 my $input = shift || return undef;
679 # textual hashes are O.K.
680 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
681 return $input;
683 # it must be correct pathname
684 $input = validate_pathname($input)
685 or return undef;
686 # restrictions on ref name according to git-check-ref-format
687 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
688 return undef;
690 return $input;
693 # decode sequences of octets in utf8 into Perl's internal form,
694 # which is utf-8 with utf8 flag set if needed. gitweb writes out
695 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
696 sub to_utf8 {
697 my $str = shift;
698 my $res;
699 eval { $res = decode_utf8($str, Encode::FB_CROAK); };
700 if (defined $res) {
701 return $res;
702 } else {
703 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
707 # quote unsafe chars, but keep the slash, even when it's not
708 # correct, but quoted slashes look too horrible in bookmarks
709 sub esc_param {
710 my $str = shift;
711 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
712 $str =~ s/\+/%2B/g;
713 $str =~ s/ /\+/g;
714 return $str;
717 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
718 sub esc_url {
719 my $str = shift;
720 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
721 $str =~ s/\+/%2B/g;
722 $str =~ s/ /\+/g;
723 return $str;
726 # replace invalid utf8 character with SUBSTITUTION sequence
727 sub esc_html ($;%) {
728 my $str = shift;
729 my %opts = @_;
731 $str = to_utf8($str);
732 $str = $cgi->escapeHTML($str);
733 if ($opts{'-nbsp'}) {
734 $str =~ s/ /&nbsp;/g;
736 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
737 return $str;
740 # quote control characters and escape filename to HTML
741 sub esc_path {
742 my $str = shift;
743 my %opts = @_;
745 $str = to_utf8($str);
746 $str = $cgi->escapeHTML($str);
747 if ($opts{'-nbsp'}) {
748 $str =~ s/ /&nbsp;/g;
750 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
751 return $str;
754 # Make control characters "printable", using character escape codes (CEC)
755 sub quot_cec {
756 my $cntrl = shift;
757 my %es = ( # character escape codes, aka escape sequences
758 "\t" => '\t', # tab (HT)
759 "\n" => '\n', # line feed (LF)
760 "\r" => '\r', # carrige return (CR)
761 "\f" => '\f', # form feed (FF)
762 "\b" => '\b', # backspace (BS)
763 "\a" => '\a', # alarm (bell) (BEL)
764 "\e" => '\e', # escape (ESC)
765 "\013" => '\v', # vertical tab (VT)
766 "\000" => '\0', # nul character (NUL)
768 my $chr = ( (exists $es{$cntrl})
769 ? $es{$cntrl}
770 : sprintf('\%03o', ord($cntrl)) );
771 return "<span class=\"cntrl\">$chr</span>";
774 # Alternatively use unicode control pictures codepoints,
775 # Unicode "printable representation" (PR)
776 sub quot_upr {
777 my $cntrl = shift;
778 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
779 return "<span class=\"cntrl\">$chr</span>";
782 # git may return quoted and escaped filenames
783 sub unquote {
784 my $str = shift;
786 sub unq {
787 my $seq = shift;
788 my %es = ( # character escape codes, aka escape sequences
789 't' => "\t", # tab (HT, TAB)
790 'n' => "\n", # newline (NL)
791 'r' => "\r", # return (CR)
792 'f' => "\f", # form feed (FF)
793 'b' => "\b", # backspace (BS)
794 'a' => "\a", # alarm (bell) (BEL)
795 'e' => "\e", # escape (ESC)
796 'v' => "\013", # vertical tab (VT)
799 if ($seq =~ m/^[0-7]{1,3}$/) {
800 # octal char sequence
801 return chr(oct($seq));
802 } elsif (exists $es{$seq}) {
803 # C escape sequence, aka character escape code
804 return $es{$seq}
806 # quoted ordinary character
807 return $seq;
810 if ($str =~ m/^"(.*)"$/) {
811 # needs unquoting
812 $str = $1;
813 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
815 return $str;
818 # escape tabs (convert tabs to spaces)
819 sub untabify {
820 my $line = shift;
822 while ((my $pos = index($line, "\t")) != -1) {
823 if (my $count = (8 - ($pos % 8))) {
824 my $spaces = ' ' x $count;
825 $line =~ s/\t/$spaces/;
829 return $line;
832 sub project_in_list {
833 my $project = shift;
834 my @list = git_get_projects_list();
835 return @list && scalar(grep { $_->{'path'} eq $project } @list);
838 ## ----------------------------------------------------------------------
839 ## HTML aware string manipulation
841 sub chop_str {
842 my $str = shift;
843 my $len = shift;
844 my $add_len = shift || 10;
846 # allow only $len chars, but don't cut a word if it would fit in $add_len
847 # if it doesn't fit, cut it if it's still longer than the dots we would add
848 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
849 my $body = $1;
850 my $tail = $2;
851 if (length($tail) > 4) {
852 $tail = " ...";
853 $body =~ s/&[^;]*$//; # remove chopped character entities
855 return "$body$tail";
858 # takes the same arguments as chop_str, but also wraps a <span> around the
859 # result with a title attribute if it does get chopped. Additionally, the
860 # string is HTML-escaped.
861 sub chop_and_escape_str {
862 my $str = shift;
863 my $len = shift;
864 my $add_len = shift || 10;
866 my $chopped = chop_str($str, $len, $add_len);
867 if ($chopped eq $str) {
868 return esc_html($chopped);
869 } else {
870 return qq{<span title="} . esc_html($str) . qq{">} .
871 esc_html($chopped) . qq{</span>};
875 ## ----------------------------------------------------------------------
876 ## functions returning short strings
878 # CSS class for given age value (in seconds)
879 sub age_class {
880 my $age = shift;
882 if (!defined $age) {
883 return "noage";
884 } elsif ($age < 60*60*2) {
885 return "age0";
886 } elsif ($age < 60*60*24*2) {
887 return "age1";
888 } else {
889 return "age2";
893 # convert age in seconds to "nn units ago" string
894 sub age_string {
895 my $age = shift;
896 my $age_str;
898 if ($age > 60*60*24*365*2) {
899 $age_str = (int $age/60/60/24/365);
900 $age_str .= " years ago";
901 } elsif ($age > 60*60*24*(365/12)*2) {
902 $age_str = int $age/60/60/24/(365/12);
903 $age_str .= " months ago";
904 } elsif ($age > 60*60*24*7*2) {
905 $age_str = int $age/60/60/24/7;
906 $age_str .= " weeks ago";
907 } elsif ($age > 60*60*24*2) {
908 $age_str = int $age/60/60/24;
909 $age_str .= " days ago";
910 } elsif ($age > 60*60*2) {
911 $age_str = int $age/60/60;
912 $age_str .= " hours ago";
913 } elsif ($age > 60*2) {
914 $age_str = int $age/60;
915 $age_str .= " min ago";
916 } elsif ($age > 2) {
917 $age_str = int $age;
918 $age_str .= " sec ago";
919 } else {
920 $age_str .= " right now";
922 return $age_str;
925 use constant {
926 S_IFINVALID => 0030000,
927 S_IFGITLINK => 0160000,
930 # submodule/subproject, a commit object reference
931 sub S_ISGITLINK($) {
932 my $mode = shift;
934 return (($mode & S_IFMT) == S_IFGITLINK)
937 # convert file mode in octal to symbolic file mode string
938 sub mode_str {
939 my $mode = oct shift;
941 if (S_ISGITLINK($mode)) {
942 return 'm---------';
943 } elsif (S_ISDIR($mode & S_IFMT)) {
944 return 'drwxr-xr-x';
945 } elsif (S_ISLNK($mode)) {
946 return 'lrwxrwxrwx';
947 } elsif (S_ISREG($mode)) {
948 # git cares only about the executable bit
949 if ($mode & S_IXUSR) {
950 return '-rwxr-xr-x';
951 } else {
952 return '-rw-r--r--';
954 } else {
955 return '----------';
959 # convert file mode in octal to file type string
960 sub file_type {
961 my $mode = shift;
963 if ($mode !~ m/^[0-7]+$/) {
964 return $mode;
965 } else {
966 $mode = oct $mode;
969 if (S_ISGITLINK($mode)) {
970 return "submodule";
971 } elsif (S_ISDIR($mode & S_IFMT)) {
972 return "directory";
973 } elsif (S_ISLNK($mode)) {
974 return "symlink";
975 } elsif (S_ISREG($mode)) {
976 return "file";
977 } else {
978 return "unknown";
982 # convert file mode in octal to file type description string
983 sub file_type_long {
984 my $mode = shift;
986 if ($mode !~ m/^[0-7]+$/) {
987 return $mode;
988 } else {
989 $mode = oct $mode;
992 if (S_ISGITLINK($mode)) {
993 return "submodule";
994 } elsif (S_ISDIR($mode & S_IFMT)) {
995 return "directory";
996 } elsif (S_ISLNK($mode)) {
997 return "symlink";
998 } elsif (S_ISREG($mode)) {
999 if ($mode & S_IXUSR) {
1000 return "executable";
1001 } else {
1002 return "file";
1004 } else {
1005 return "unknown";
1010 ## ----------------------------------------------------------------------
1011 ## functions returning short HTML fragments, or transforming HTML fragments
1012 ## which don't belong to other sections
1014 # format line of commit message.
1015 sub format_log_line_html {
1016 my $line = shift;
1018 $line = esc_html($line, -nbsp=>1);
1019 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1020 my $hash_text = $1;
1021 my $link =
1022 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1023 -class => "text"}, $hash_text);
1024 $line =~ s/$hash_text/$link/;
1026 return $line;
1029 # format marker of refs pointing to given object
1030 sub format_ref_marker {
1031 my ($refs, $id) = @_;
1032 my $markers = '';
1034 if (defined $refs->{$id}) {
1035 foreach my $ref (@{$refs->{$id}}) {
1036 my ($type, $name) = qw();
1037 # e.g. tags/v2.6.11 or heads/next
1038 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1039 $type = $1;
1040 $name = $2;
1041 } else {
1042 $type = "ref";
1043 $name = $ref;
1046 $markers .= " <span class=\"$type\" title=\"$ref\">" .
1047 esc_html($name) . "</span>";
1051 if ($markers) {
1052 return ' <span class="refs">'. $markers . '</span>';
1053 } else {
1054 return "";
1058 # format, perhaps shortened and with markers, title line
1059 sub format_subject_html {
1060 my ($long, $short, $href, $extra) = @_;
1061 $extra = '' unless defined($extra);
1063 if (length($short) < length($long)) {
1064 return $cgi->a({-href => $href, -class => "list subject",
1065 -title => to_utf8($long)},
1066 esc_html($short) . $extra);
1067 } else {
1068 return $cgi->a({-href => $href, -class => "list subject"},
1069 esc_html($long) . $extra);
1073 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1074 sub format_git_diff_header_line {
1075 my $line = shift;
1076 my $diffinfo = shift;
1077 my ($from, $to) = @_;
1079 if ($diffinfo->{'nparents'}) {
1080 # combined diff
1081 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1082 if ($to->{'href'}) {
1083 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1084 esc_path($to->{'file'}));
1085 } else { # file was deleted (no href)
1086 $line .= esc_path($to->{'file'});
1088 } else {
1089 # "ordinary" diff
1090 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1091 if ($from->{'href'}) {
1092 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1093 'a/' . esc_path($from->{'file'}));
1094 } else { # file was added (no href)
1095 $line .= 'a/' . esc_path($from->{'file'});
1097 $line .= ' ';
1098 if ($to->{'href'}) {
1099 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1100 'b/' . esc_path($to->{'file'}));
1101 } else { # file was deleted
1102 $line .= 'b/' . esc_path($to->{'file'});
1106 return "<div class=\"diff header\">$line</div>\n";
1109 # format extended diff header line, before patch itself
1110 sub format_extended_diff_header_line {
1111 my $line = shift;
1112 my $diffinfo = shift;
1113 my ($from, $to) = @_;
1115 # match <path>
1116 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1117 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1118 esc_path($from->{'file'}));
1120 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1121 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1122 esc_path($to->{'file'}));
1124 # match single <mode>
1125 if ($line =~ m/\s(\d{6})$/) {
1126 $line .= '<span class="info"> (' .
1127 file_type_long($1) .
1128 ')</span>';
1130 # match <hash>
1131 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1132 # can match only for combined diff
1133 $line = 'index ';
1134 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1135 if ($from->{'href'}[$i]) {
1136 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1137 -class=>"hash"},
1138 substr($diffinfo->{'from_id'}[$i],0,7));
1139 } else {
1140 $line .= '0' x 7;
1142 # separator
1143 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1145 $line .= '..';
1146 if ($to->{'href'}) {
1147 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1148 substr($diffinfo->{'to_id'},0,7));
1149 } else {
1150 $line .= '0' x 7;
1153 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1154 # can match only for ordinary diff
1155 my ($from_link, $to_link);
1156 if ($from->{'href'}) {
1157 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1158 substr($diffinfo->{'from_id'},0,7));
1159 } else {
1160 $from_link = '0' x 7;
1162 if ($to->{'href'}) {
1163 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1164 substr($diffinfo->{'to_id'},0,7));
1165 } else {
1166 $to_link = '0' x 7;
1168 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1169 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1172 return $line . "<br/>\n";
1175 # format from-file/to-file diff header
1176 sub format_diff_from_to_header {
1177 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1178 my $line;
1179 my $result = '';
1181 $line = $from_line;
1182 #assert($line =~ m/^---/) if DEBUG;
1183 # no extra formatting for "^--- /dev/null"
1184 if (! $diffinfo->{'nparents'}) {
1185 # ordinary (single parent) diff
1186 if ($line =~ m!^--- "?a/!) {
1187 if ($from->{'href'}) {
1188 $line = '--- a/' .
1189 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1190 esc_path($from->{'file'}));
1191 } else {
1192 $line = '--- a/' .
1193 esc_path($from->{'file'});
1196 $result .= qq!<div class="diff from_file">$line</div>\n!;
1198 } else {
1199 # combined diff (merge commit)
1200 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1201 if ($from->{'href'}[$i]) {
1202 $line = '--- ' .
1203 $cgi->a({-href=>href(action=>"blobdiff",
1204 hash_parent=>$diffinfo->{'from_id'}[$i],
1205 hash_parent_base=>$parents[$i],
1206 file_parent=>$from->{'file'}[$i],
1207 hash=>$diffinfo->{'to_id'},
1208 hash_base=>$hash,
1209 file_name=>$to->{'file'}),
1210 -class=>"path",
1211 -title=>"diff" . ($i+1)},
1212 $i+1) .
1213 '/' .
1214 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1215 esc_path($from->{'file'}[$i]));
1216 } else {
1217 $line = '--- /dev/null';
1219 $result .= qq!<div class="diff from_file">$line</div>\n!;
1223 $line = $to_line;
1224 #assert($line =~ m/^\+\+\+/) if DEBUG;
1225 # no extra formatting for "^+++ /dev/null"
1226 if ($line =~ m!^\+\+\+ "?b/!) {
1227 if ($to->{'href'}) {
1228 $line = '+++ b/' .
1229 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1230 esc_path($to->{'file'}));
1231 } else {
1232 $line = '+++ b/' .
1233 esc_path($to->{'file'});
1236 $result .= qq!<div class="diff to_file">$line</div>\n!;
1238 return $result;
1241 # create note for patch simplified by combined diff
1242 sub format_diff_cc_simplified {
1243 my ($diffinfo, @parents) = @_;
1244 my $result = '';
1246 $result .= "<div class=\"diff header\">" .
1247 "diff --cc ";
1248 if (!is_deleted($diffinfo)) {
1249 $result .= $cgi->a({-href => href(action=>"blob",
1250 hash_base=>$hash,
1251 hash=>$diffinfo->{'to_id'},
1252 file_name=>$diffinfo->{'to_file'}),
1253 -class => "path"},
1254 esc_path($diffinfo->{'to_file'}));
1255 } else {
1256 $result .= esc_path($diffinfo->{'to_file'});
1258 $result .= "</div>\n" . # class="diff header"
1259 "<div class=\"diff nodifferences\">" .
1260 "Simple merge" .
1261 "</div>\n"; # class="diff nodifferences"
1263 return $result;
1266 # format patch (diff) line (not to be used for diff headers)
1267 sub format_diff_line {
1268 my $line = shift;
1269 my ($from, $to) = @_;
1270 my $diff_class = "";
1272 chomp $line;
1274 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1275 # combined diff
1276 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1277 if ($line =~ m/^\@{3}/) {
1278 $diff_class = " chunk_header";
1279 } elsif ($line =~ m/^\\/) {
1280 $diff_class = " incomplete";
1281 } elsif ($prefix =~ tr/+/+/) {
1282 $diff_class = " add";
1283 } elsif ($prefix =~ tr/-/-/) {
1284 $diff_class = " rem";
1286 } else {
1287 # assume ordinary diff
1288 my $char = substr($line, 0, 1);
1289 if ($char eq '+') {
1290 $diff_class = " add";
1291 } elsif ($char eq '-') {
1292 $diff_class = " rem";
1293 } elsif ($char eq '@') {
1294 $diff_class = " chunk_header";
1295 } elsif ($char eq "\\") {
1296 $diff_class = " incomplete";
1299 $line = untabify($line);
1300 if ($from && $to && $line =~ m/^\@{2} /) {
1301 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1302 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1304 $from_lines = 0 unless defined $from_lines;
1305 $to_lines = 0 unless defined $to_lines;
1307 if ($from->{'href'}) {
1308 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1309 -class=>"list"}, $from_text);
1311 if ($to->{'href'}) {
1312 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1313 -class=>"list"}, $to_text);
1315 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1316 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1317 return "<div class=\"diff$diff_class\">$line</div>\n";
1318 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1319 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1320 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1322 @from_text = split(' ', $ranges);
1323 for (my $i = 0; $i < @from_text; ++$i) {
1324 ($from_start[$i], $from_nlines[$i]) =
1325 (split(',', substr($from_text[$i], 1)), 0);
1328 $to_text = pop @from_text;
1329 $to_start = pop @from_start;
1330 $to_nlines = pop @from_nlines;
1332 $line = "<span class=\"chunk_info\">$prefix ";
1333 for (my $i = 0; $i < @from_text; ++$i) {
1334 if ($from->{'href'}[$i]) {
1335 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1336 -class=>"list"}, $from_text[$i]);
1337 } else {
1338 $line .= $from_text[$i];
1340 $line .= " ";
1342 if ($to->{'href'}) {
1343 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1344 -class=>"list"}, $to_text);
1345 } else {
1346 $line .= $to_text;
1348 $line .= " $prefix</span>" .
1349 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1350 return "<div class=\"diff$diff_class\">$line</div>\n";
1352 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1355 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1356 # linked. Pass the hash of the tree/commit to snapshot.
1357 sub format_snapshot_links {
1358 my ($hash) = @_;
1359 my @snapshot_fmts = gitweb_check_feature('snapshot');
1360 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1361 my $num_fmts = @snapshot_fmts;
1362 if ($num_fmts > 1) {
1363 # A parenthesized list of links bearing format names.
1364 # e.g. "snapshot (_tar.gz_ _zip_)"
1365 return "snapshot (" . join(' ', map
1366 $cgi->a({
1367 -href => href(
1368 action=>"snapshot",
1369 hash=>$hash,
1370 snapshot_format=>$_
1372 }, $known_snapshot_formats{$_}{'display'})
1373 , @snapshot_fmts) . ")";
1374 } elsif ($num_fmts == 1) {
1375 # A single "snapshot" link whose tooltip bears the format name.
1376 # i.e. "_snapshot_"
1377 my ($fmt) = @snapshot_fmts;
1378 return
1379 $cgi->a({
1380 -href => href(
1381 action=>"snapshot",
1382 hash=>$hash,
1383 snapshot_format=>$fmt
1385 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1386 }, "snapshot");
1387 } else { # $num_fmts == 0
1388 return undef;
1392 ## ----------------------------------------------------------------------
1393 ## git utility subroutines, invoking git commands
1395 # returns path to the core git executable and the --git-dir parameter as list
1396 sub git_cmd {
1397 return $GIT, '--git-dir='.$git_dir;
1400 # returns path to the core git executable and the --git-dir parameter as string
1401 sub git_cmd_str {
1402 return join(' ', git_cmd());
1405 # get HEAD ref of given project as hash
1406 sub git_get_head_hash {
1407 my $project = shift;
1408 my $o_git_dir = $git_dir;
1409 my $retval = undef;
1410 $git_dir = "$projectroot/$project";
1411 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1412 my $head = <$fd>;
1413 close $fd;
1414 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1415 $retval = $1;
1418 if (defined $o_git_dir) {
1419 $git_dir = $o_git_dir;
1421 return $retval;
1424 # get type of given object
1425 sub git_get_type {
1426 my $hash = shift;
1428 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1429 my $type = <$fd>;
1430 close $fd or return;
1431 chomp $type;
1432 return $type;
1435 sub git_get_project_config {
1436 my ($key, $type) = @_;
1438 return unless ($key);
1439 $key =~ s/^gitweb\.//;
1440 return if ($key =~ m/\W/);
1442 my @x = (git_cmd(), 'config');
1443 if (defined $type) { push @x, $type; }
1444 push @x, "--get";
1445 push @x, "gitweb.$key";
1446 my $val = qx(@x);
1447 chomp $val;
1448 return ($val);
1451 # get hash of given path at given ref
1452 sub git_get_hash_by_path {
1453 my $base = shift;
1454 my $path = shift || return undef;
1455 my $type = shift;
1457 $path =~ s,/+$,,;
1459 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1460 or die_error(undef, "Open git-ls-tree failed");
1461 my $line = <$fd>;
1462 close $fd or return undef;
1464 if (!defined $line) {
1465 # there is no tree or hash given by $path at $base
1466 return undef;
1469 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1470 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1471 if (defined $type && $type ne $2) {
1472 # type doesn't match
1473 return undef;
1475 return $3;
1478 # get path of entry with given hash at given tree-ish (ref)
1479 # used to get 'from' filename for combined diff (merge commit) for renames
1480 sub git_get_path_by_hash {
1481 my $base = shift || return;
1482 my $hash = shift || return;
1484 local $/ = "\0";
1486 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1487 or return undef;
1488 while (my $line = <$fd>) {
1489 chomp $line;
1491 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1492 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1493 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1494 close $fd;
1495 return $1;
1498 close $fd;
1499 return undef;
1502 ## ......................................................................
1503 ## git utility functions, directly accessing git repository
1505 sub git_get_project_description {
1506 my $path = shift;
1508 open my $fd, "$projectroot/$path/description" or return undef;
1509 my $descr = <$fd>;
1510 close $fd;
1511 if (defined $descr) {
1512 chomp $descr;
1514 return $descr;
1517 sub git_get_project_url_list {
1518 my $path = shift;
1520 open my $fd, "$projectroot/$path/cloneurl" or return;
1521 my @git_project_url_list = map { chomp; $_ } <$fd>;
1522 close $fd;
1524 return wantarray ? @git_project_url_list : \@git_project_url_list;
1527 sub git_get_projects_list {
1528 my ($filter) = @_;
1529 my @list;
1531 $filter ||= '';
1532 $filter =~ s/\.git$//;
1534 my ($check_forks) = gitweb_check_feature('forks');
1536 if (-d $projects_list) {
1537 # search in directory
1538 my $dir = $projects_list . ($filter ? "/$filter" : '');
1539 # remove the trailing "/"
1540 $dir =~ s!/+$!!;
1541 my $pfxlen = length("$dir");
1542 my $pfxdepth = ($dir =~ tr!/!!);
1544 File::Find::find({
1545 follow_fast => 1, # follow symbolic links
1546 follow_skip => 2, # ignore duplicates
1547 dangling_symlinks => 0, # ignore dangling symlinks, silently
1548 wanted => sub {
1549 # skip project-list toplevel, if we get it.
1550 return if (m!^[/.]$!);
1551 # only directories can be git repositories
1552 return unless (-d $_);
1553 # don't traverse too deep (Find is super slow on os x)
1554 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1555 $File::Find::prune = 1;
1556 return;
1559 my $subdir = substr($File::Find::name, $pfxlen + 1);
1560 # we check related file in $projectroot
1561 if ($check_forks and $subdir =~ m#/.#) {
1562 $File::Find::prune = 1;
1563 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1564 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1565 $File::Find::prune = 1;
1568 }, "$dir");
1570 } elsif (-f $projects_list) {
1571 # read from file(url-encoded):
1572 # 'git%2Fgit.git Linus+Torvalds'
1573 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1574 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1575 my %paths;
1576 open my ($fd), $projects_list or return;
1577 PROJECT:
1578 while (my $line = <$fd>) {
1579 chomp $line;
1580 my ($path, $owner) = split ' ', $line;
1581 $path = unescape($path);
1582 $owner = unescape($owner);
1583 if (!defined $path) {
1584 next;
1586 if ($filter ne '') {
1587 # looking for forks;
1588 my $pfx = substr($path, 0, length($filter));
1589 if ($pfx ne $filter) {
1590 next PROJECT;
1592 my $sfx = substr($path, length($filter));
1593 if ($sfx !~ /^\/.*\.git$/) {
1594 next PROJECT;
1596 } elsif ($check_forks) {
1597 PATH:
1598 foreach my $filter (keys %paths) {
1599 # looking for forks;
1600 my $pfx = substr($path, 0, length($filter));
1601 if ($pfx ne $filter) {
1602 next PATH;
1604 my $sfx = substr($path, length($filter));
1605 if ($sfx !~ /^\/.*\.git$/) {
1606 next PATH;
1608 # is a fork, don't include it in
1609 # the list
1610 next PROJECT;
1613 if (check_export_ok("$projectroot/$path")) {
1614 my $pr = {
1615 path => $path,
1616 owner => to_utf8($owner),
1618 push @list, $pr;
1619 (my $forks_path = $path) =~ s/\.git$//;
1620 $paths{$forks_path}++;
1623 close $fd;
1625 return @list;
1628 our $gitweb_project_owner = undef;
1629 sub git_get_project_list_from_file {
1631 return if (defined $gitweb_project_owner);
1633 $gitweb_project_owner = {};
1634 # read from file (url-encoded):
1635 # 'git%2Fgit.git Linus+Torvalds'
1636 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1637 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1638 if (-f $projects_list) {
1639 open (my $fd , $projects_list);
1640 while (my $line = <$fd>) {
1641 chomp $line;
1642 my ($pr, $ow) = split ' ', $line;
1643 $pr = unescape($pr);
1644 $ow = unescape($ow);
1645 $gitweb_project_owner->{$pr} = to_utf8($ow);
1647 close $fd;
1651 sub git_get_project_owner {
1652 my $project = shift;
1653 my $owner;
1655 return undef unless $project;
1657 if (!defined $gitweb_project_owner) {
1658 git_get_project_list_from_file();
1661 if (exists $gitweb_project_owner->{$project}) {
1662 $owner = $gitweb_project_owner->{$project};
1664 if (!defined $owner) {
1665 $owner = get_file_owner("$projectroot/$project");
1668 return $owner;
1671 sub git_get_last_activity {
1672 my ($path) = @_;
1673 my $fd;
1675 $git_dir = "$projectroot/$path";
1676 open($fd, "-|", git_cmd(), 'for-each-ref',
1677 '--format=%(committer)',
1678 '--sort=-committerdate',
1679 '--count=1',
1680 'refs/heads') or return;
1681 my $most_recent = <$fd>;
1682 close $fd or return;
1683 if (defined $most_recent &&
1684 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1685 my $timestamp = $1;
1686 my $age = time - $timestamp;
1687 return ($age, age_string($age));
1689 return (undef, undef);
1692 sub git_get_references {
1693 my $type = shift || "";
1694 my %refs;
1695 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1696 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1697 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1698 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1699 or return;
1701 while (my $line = <$fd>) {
1702 chomp $line;
1703 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1704 if (defined $refs{$1}) {
1705 push @{$refs{$1}}, $2;
1706 } else {
1707 $refs{$1} = [ $2 ];
1711 close $fd or return;
1712 return \%refs;
1715 sub git_get_rev_name_tags {
1716 my $hash = shift || return undef;
1718 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1719 or return;
1720 my $name_rev = <$fd>;
1721 close $fd;
1723 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1724 return $1;
1725 } else {
1726 # catches also '$hash undefined' output
1727 return undef;
1731 ## ----------------------------------------------------------------------
1732 ## parse to hash functions
1734 sub parse_date {
1735 my $epoch = shift;
1736 my $tz = shift || "-0000";
1738 my %date;
1739 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1740 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1741 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1742 $date{'hour'} = $hour;
1743 $date{'minute'} = $min;
1744 $date{'mday'} = $mday;
1745 $date{'day'} = $days[$wday];
1746 $date{'month'} = $months[$mon];
1747 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1748 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1749 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1750 $mday, $months[$mon], $hour ,$min;
1751 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1752 1900+$year, $mon, $mday, $hour ,$min, $sec;
1754 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1755 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1756 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1757 $date{'hour_local'} = $hour;
1758 $date{'minute_local'} = $min;
1759 $date{'tz_local'} = $tz;
1760 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1761 1900+$year, $mon+1, $mday,
1762 $hour, $min, $sec, $tz);
1763 return %date;
1766 sub parse_tag {
1767 my $tag_id = shift;
1768 my %tag;
1769 my @comment;
1771 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1772 $tag{'id'} = $tag_id;
1773 while (my $line = <$fd>) {
1774 chomp $line;
1775 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1776 $tag{'object'} = $1;
1777 } elsif ($line =~ m/^type (.+)$/) {
1778 $tag{'type'} = $1;
1779 } elsif ($line =~ m/^tag (.+)$/) {
1780 $tag{'name'} = $1;
1781 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1782 $tag{'author'} = $1;
1783 $tag{'epoch'} = $2;
1784 $tag{'tz'} = $3;
1785 } elsif ($line =~ m/--BEGIN/) {
1786 push @comment, $line;
1787 last;
1788 } elsif ($line eq "") {
1789 last;
1792 push @comment, <$fd>;
1793 $tag{'comment'} = \@comment;
1794 close $fd or return;
1795 if (!defined $tag{'name'}) {
1796 return
1798 return %tag
1801 sub parse_commit_text {
1802 my ($commit_text, $withparents) = @_;
1803 my @commit_lines = split '\n', $commit_text;
1804 my %co;
1806 pop @commit_lines; # Remove '\0'
1808 if (! @commit_lines) {
1809 return;
1812 my $header = shift @commit_lines;
1813 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1814 return;
1816 ($co{'id'}, my @parents) = split ' ', $header;
1817 while (my $line = shift @commit_lines) {
1818 last if $line eq "\n";
1819 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1820 $co{'tree'} = $1;
1821 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1822 push @parents, $1;
1823 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1824 $co{'author'} = $1;
1825 $co{'author_epoch'} = $2;
1826 $co{'author_tz'} = $3;
1827 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1828 $co{'author_name'} = $1;
1829 $co{'author_email'} = $2;
1830 } else {
1831 $co{'author_name'} = $co{'author'};
1833 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1834 $co{'committer'} = $1;
1835 $co{'committer_epoch'} = $2;
1836 $co{'committer_tz'} = $3;
1837 $co{'committer_name'} = $co{'committer'};
1838 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1839 $co{'committer_name'} = $1;
1840 $co{'committer_email'} = $2;
1841 } else {
1842 $co{'committer_name'} = $co{'committer'};
1846 if (!defined $co{'tree'}) {
1847 return;
1849 $co{'parents'} = \@parents;
1850 $co{'parent'} = $parents[0];
1852 foreach my $title (@commit_lines) {
1853 $title =~ s/^ //;
1854 if ($title ne "") {
1855 $co{'title'} = chop_str($title, 80, 5);
1856 # remove leading stuff of merges to make the interesting part visible
1857 if (length($title) > 50) {
1858 $title =~ s/^Automatic //;
1859 $title =~ s/^merge (of|with) /Merge ... /i;
1860 if (length($title) > 50) {
1861 $title =~ s/(http|rsync):\/\///;
1863 if (length($title) > 50) {
1864 $title =~ s/(master|www|rsync)\.//;
1866 if (length($title) > 50) {
1867 $title =~ s/kernel.org:?//;
1869 if (length($title) > 50) {
1870 $title =~ s/\/pub\/scm//;
1873 $co{'title_short'} = chop_str($title, 50, 5);
1874 last;
1877 if ($co{'title'} eq "") {
1878 $co{'title'} = $co{'title_short'} = '(no commit message)';
1880 # remove added spaces
1881 foreach my $line (@commit_lines) {
1882 $line =~ s/^ //;
1884 $co{'comment'} = \@commit_lines;
1886 my $age = time - $co{'committer_epoch'};
1887 $co{'age'} = $age;
1888 $co{'age_string'} = age_string($age);
1889 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1890 if ($age > 60*60*24*7*2) {
1891 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1892 $co{'age_string_age'} = $co{'age_string'};
1893 } else {
1894 $co{'age_string_date'} = $co{'age_string'};
1895 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1897 return %co;
1900 sub parse_commit {
1901 my ($commit_id) = @_;
1902 my %co;
1904 local $/ = "\0";
1906 open my $fd, "-|", git_cmd(), "rev-list",
1907 "--parents",
1908 "--header",
1909 "--max-count=1",
1910 $commit_id,
1911 "--",
1912 or die_error(undef, "Open git-rev-list failed");
1913 %co = parse_commit_text(<$fd>, 1);
1914 close $fd;
1916 return %co;
1919 sub parse_commits {
1920 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1921 my @cos;
1923 $maxcount ||= 1;
1924 $skip ||= 0;
1926 local $/ = "\0";
1928 open my $fd, "-|", git_cmd(), "rev-list",
1929 "--header",
1930 ($arg ? ($arg) : ()),
1931 ("--max-count=" . $maxcount),
1932 ("--skip=" . $skip),
1933 @extra_options,
1934 $commit_id,
1935 "--",
1936 ($filename ? ($filename) : ())
1937 or die_error(undef, "Open git-rev-list failed");
1938 while (my $line = <$fd>) {
1939 my %co = parse_commit_text($line);
1940 push @cos, \%co;
1942 close $fd;
1944 return wantarray ? @cos : \@cos;
1947 # parse ref from ref_file, given by ref_id, with given type
1948 sub parse_ref {
1949 my $ref_file = shift;
1950 my $ref_id = shift;
1951 my $type = shift || git_get_type($ref_id);
1952 my %ref_item;
1954 $ref_item{'type'} = $type;
1955 $ref_item{'id'} = $ref_id;
1956 $ref_item{'epoch'} = 0;
1957 $ref_item{'age'} = "unknown";
1958 if ($type eq "tag") {
1959 my %tag = parse_tag($ref_id);
1960 $ref_item{'comment'} = $tag{'comment'};
1961 if ($tag{'type'} eq "commit") {
1962 my %co = parse_commit($tag{'object'});
1963 $ref_item{'epoch'} = $co{'committer_epoch'};
1964 $ref_item{'age'} = $co{'age_string'};
1965 } elsif (defined($tag{'epoch'})) {
1966 my $age = time - $tag{'epoch'};
1967 $ref_item{'epoch'} = $tag{'epoch'};
1968 $ref_item{'age'} = age_string($age);
1970 $ref_item{'reftype'} = $tag{'type'};
1971 $ref_item{'name'} = $tag{'name'};
1972 $ref_item{'refid'} = $tag{'object'};
1973 } elsif ($type eq "commit"){
1974 my %co = parse_commit($ref_id);
1975 $ref_item{'reftype'} = "commit";
1976 $ref_item{'name'} = $ref_file;
1977 $ref_item{'title'} = $co{'title'};
1978 $ref_item{'refid'} = $ref_id;
1979 $ref_item{'epoch'} = $co{'committer_epoch'};
1980 $ref_item{'age'} = $co{'age_string'};
1981 } else {
1982 $ref_item{'reftype'} = $type;
1983 $ref_item{'name'} = $ref_file;
1984 $ref_item{'refid'} = $ref_id;
1987 return %ref_item;
1990 # parse line of git-diff-tree "raw" output
1991 sub parse_difftree_raw_line {
1992 my $line = shift;
1993 my %res;
1995 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1996 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1997 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1998 $res{'from_mode'} = $1;
1999 $res{'to_mode'} = $2;
2000 $res{'from_id'} = $3;
2001 $res{'to_id'} = $4;
2002 $res{'status'} = $res{'status_str'} = $5;
2003 $res{'similarity'} = $6;
2004 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2005 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2006 } else {
2007 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2010 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2011 # combined diff (for merge commit)
2012 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2013 $res{'nparents'} = length($1);
2014 $res{'from_mode'} = [ split(' ', $2) ];
2015 $res{'to_mode'} = pop @{$res{'from_mode'}};
2016 $res{'from_id'} = [ split(' ', $3) ];
2017 $res{'to_id'} = pop @{$res{'from_id'}};
2018 $res{'status_str'} = $4;
2019 $res{'status'} = [ split('', $4) ];
2020 $res{'to_file'} = unquote($5);
2022 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2023 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2024 $res{'commit'} = $1;
2027 return wantarray ? %res : \%res;
2030 # wrapper: return parsed line of git-diff-tree "raw" output
2031 # (the argument might be raw line, or parsed info)
2032 sub parsed_difftree_line {
2033 my $line_or_ref = shift;
2035 if (ref($line_or_ref) eq "HASH") {
2036 # pre-parsed (or generated by hand)
2037 return $line_or_ref;
2038 } else {
2039 return parse_difftree_raw_line($line_or_ref);
2043 # parse line of git-ls-tree output
2044 sub parse_ls_tree_line ($;%) {
2045 my $line = shift;
2046 my %opts = @_;
2047 my %res;
2049 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2050 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2052 $res{'mode'} = $1;
2053 $res{'type'} = $2;
2054 $res{'hash'} = $3;
2055 if ($opts{'-z'}) {
2056 $res{'name'} = $4;
2057 } else {
2058 $res{'name'} = unquote($4);
2061 return wantarray ? %res : \%res;
2064 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2065 sub parse_from_to_diffinfo {
2066 my ($diffinfo, $from, $to, @parents) = @_;
2068 if ($diffinfo->{'nparents'}) {
2069 # combined diff
2070 $from->{'file'} = [];
2071 $from->{'href'} = [];
2072 fill_from_file_info($diffinfo, @parents)
2073 unless exists $diffinfo->{'from_file'};
2074 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2075 $from->{'file'}[$i] =
2076 defined $diffinfo->{'from_file'}[$i] ?
2077 $diffinfo->{'from_file'}[$i] :
2078 $diffinfo->{'to_file'};
2079 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2080 $from->{'href'}[$i] = href(action=>"blob",
2081 hash_base=>$parents[$i],
2082 hash=>$diffinfo->{'from_id'}[$i],
2083 file_name=>$from->{'file'}[$i]);
2084 } else {
2085 $from->{'href'}[$i] = undef;
2088 } else {
2089 # ordinary (not combined) diff
2090 $from->{'file'} = $diffinfo->{'from_file'};
2091 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2092 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2093 hash=>$diffinfo->{'from_id'},
2094 file_name=>$from->{'file'});
2095 } else {
2096 delete $from->{'href'};
2100 $to->{'file'} = $diffinfo->{'to_file'};
2101 if (!is_deleted($diffinfo)) { # file exists in result
2102 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2103 hash=>$diffinfo->{'to_id'},
2104 file_name=>$to->{'file'});
2105 } else {
2106 delete $to->{'href'};
2110 ## ......................................................................
2111 ## parse to array of hashes functions
2113 sub git_get_heads_list {
2114 my $limit = shift;
2115 my @headslist;
2117 open my $fd, '-|', git_cmd(), 'for-each-ref',
2118 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2119 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2120 'refs/heads'
2121 or return;
2122 while (my $line = <$fd>) {
2123 my %ref_item;
2125 chomp $line;
2126 my ($refinfo, $committerinfo) = split(/\0/, $line);
2127 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2128 my ($committer, $epoch, $tz) =
2129 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2130 $name =~ s!^refs/heads/!!;
2132 $ref_item{'name'} = $name;
2133 $ref_item{'id'} = $hash;
2134 $ref_item{'title'} = $title || '(no commit message)';
2135 $ref_item{'epoch'} = $epoch;
2136 if ($epoch) {
2137 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2138 } else {
2139 $ref_item{'age'} = "unknown";
2142 push @headslist, \%ref_item;
2144 close $fd;
2146 return wantarray ? @headslist : \@headslist;
2149 sub git_get_tags_list {
2150 my $limit = shift;
2151 my @tagslist;
2153 open my $fd, '-|', git_cmd(), 'for-each-ref',
2154 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2155 '--format=%(objectname) %(objecttype) %(refname) '.
2156 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2157 'refs/tags'
2158 or return;
2159 while (my $line = <$fd>) {
2160 my %ref_item;
2162 chomp $line;
2163 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2164 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2165 my ($creator, $epoch, $tz) =
2166 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2167 $name =~ s!^refs/tags/!!;
2169 $ref_item{'type'} = $type;
2170 $ref_item{'id'} = $id;
2171 $ref_item{'name'} = $name;
2172 if ($type eq "tag") {
2173 $ref_item{'subject'} = $title;
2174 $ref_item{'reftype'} = $reftype;
2175 $ref_item{'refid'} = $refid;
2176 } else {
2177 $ref_item{'reftype'} = $type;
2178 $ref_item{'refid'} = $id;
2181 if ($type eq "tag" || $type eq "commit") {
2182 $ref_item{'epoch'} = $epoch;
2183 if ($epoch) {
2184 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2185 } else {
2186 $ref_item{'age'} = "unknown";
2190 push @tagslist, \%ref_item;
2192 close $fd;
2194 return wantarray ? @tagslist : \@tagslist;
2197 ## ----------------------------------------------------------------------
2198 ## filesystem-related functions
2200 sub get_file_owner {
2201 my $path = shift;
2203 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2204 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2205 if (!defined $gcos) {
2206 return undef;
2208 my $owner = $gcos;
2209 $owner =~ s/[,;].*$//;
2210 return to_utf8($owner);
2213 ## ......................................................................
2214 ## mimetype related functions
2216 sub mimetype_guess_file {
2217 my $filename = shift;
2218 my $mimemap = shift;
2219 -r $mimemap or return undef;
2221 my %mimemap;
2222 open(MIME, $mimemap) or return undef;
2223 while (<MIME>) {
2224 next if m/^#/; # skip comments
2225 my ($mime, $exts) = split(/\t+/);
2226 if (defined $exts) {
2227 my @exts = split(/\s+/, $exts);
2228 foreach my $ext (@exts) {
2229 $mimemap{$ext} = $mime;
2233 close(MIME);
2235 $filename =~ /\.([^.]*)$/;
2236 return $mimemap{$1};
2239 sub mimetype_guess {
2240 my $filename = shift;
2241 my $mime;
2242 $filename =~ /\./ or return undef;
2244 if ($mimetypes_file) {
2245 my $file = $mimetypes_file;
2246 if ($file !~ m!^/!) { # if it is relative path
2247 # it is relative to project
2248 $file = "$projectroot/$project/$file";
2250 $mime = mimetype_guess_file($filename, $file);
2252 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2253 return $mime;
2256 sub blob_mimetype {
2257 my $fd = shift;
2258 my $filename = shift;
2260 if ($filename) {
2261 my $mime = mimetype_guess($filename);
2262 $mime and return $mime;
2265 # just in case
2266 return $default_blob_plain_mimetype unless $fd;
2268 if (-T $fd) {
2269 return 'text/plain' .
2270 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2271 } elsif (! $filename) {
2272 return 'application/octet-stream';
2273 } elsif ($filename =~ m/\.png$/i) {
2274 return 'image/png';
2275 } elsif ($filename =~ m/\.gif$/i) {
2276 return 'image/gif';
2277 } elsif ($filename =~ m/\.jpe?g$/i) {
2278 return 'image/jpeg';
2279 } else {
2280 return 'application/octet-stream';
2284 ## ======================================================================
2285 ## functions printing HTML: header, footer, error page
2287 sub git_header_html {
2288 my $status = shift || "200 OK";
2289 my $expires = shift;
2291 my $title = "$site_name";
2292 if (defined $project) {
2293 $title .= " - " . to_utf8($project);
2294 if (defined $action) {
2295 $title .= "/$action";
2296 if (defined $file_name) {
2297 $title .= " - " . esc_path($file_name);
2298 if ($action eq "tree" && $file_name !~ m|/$|) {
2299 $title .= "/";
2304 my $content_type;
2305 # require explicit support from the UA if we are to send the page as
2306 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2307 # we have to do this because MSIE sometimes globs '*/*', pretending to
2308 # support xhtml+xml but choking when it gets what it asked for.
2309 if (defined $cgi->http('HTTP_ACCEPT') &&
2310 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2311 $cgi->Accept('application/xhtml+xml') != 0) {
2312 $content_type = 'application/xhtml+xml';
2313 } else {
2314 $content_type = 'text/html';
2316 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2317 -status=> $status, -expires => $expires);
2318 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2319 print <<EOF;
2320 <?xml version="1.0" encoding="utf-8"?>
2321 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2322 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2323 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2324 <!-- git core binaries version $git_version -->
2325 <head>
2326 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2327 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2328 <meta name="robots" content="index, nofollow"/>
2329 <title>$title</title>
2331 # print out each stylesheet that exist
2332 if (defined $stylesheet) {
2333 #provides backwards capability for those people who define style sheet in a config file
2334 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2335 } else {
2336 foreach my $stylesheet (@stylesheets) {
2337 next unless $stylesheet;
2338 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2341 if (defined $project) {
2342 printf('<link rel="alternate" title="%s log RSS feed" '.
2343 'href="%s" type="application/rss+xml" />'."\n",
2344 esc_param($project), href(action=>"rss"));
2345 printf('<link rel="alternate" title="%s log RSS feed (no merges)" '.
2346 'href="%s" type="application/rss+xml" />'."\n",
2347 esc_param($project), href(action=>"rss",
2348 extra_options=>"--no-merges"));
2349 printf('<link rel="alternate" title="%s log Atom feed" '.
2350 'href="%s" type="application/atom+xml" />'."\n",
2351 esc_param($project), href(action=>"atom"));
2352 printf('<link rel="alternate" title="%s log Atom feed (no merges)" '.
2353 'href="%s" type="application/atom+xml" />'."\n",
2354 esc_param($project), href(action=>"atom",
2355 extra_options=>"--no-merges"));
2356 } else {
2357 printf('<link rel="alternate" title="%s projects list" '.
2358 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2359 $site_name, href(project=>undef, action=>"project_index"));
2360 printf('<link rel="alternate" title="%s projects feeds" '.
2361 'href="%s" type="text/x-opml"/>'."\n",
2362 $site_name, href(project=>undef, action=>"opml"));
2364 if (defined $favicon) {
2365 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2368 print "</head>\n" .
2369 "<body>\n";
2371 if (-f $site_header) {
2372 open (my $fd, $site_header);
2373 print <$fd>;
2374 close $fd;
2377 print "<div class=\"page_header\">\n" .
2378 $cgi->a({-href => esc_url($logo_url),
2379 -title => $logo_label},
2380 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2381 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2382 if (defined $project) {
2383 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2384 if (defined $action) {
2385 print " / $action";
2387 print "\n";
2389 print "</div>\n";
2391 my ($have_search) = gitweb_check_feature('search');
2392 if ((defined $project) && ($have_search)) {
2393 if (!defined $searchtext) {
2394 $searchtext = "";
2396 my $search_hash;
2397 if (defined $hash_base) {
2398 $search_hash = $hash_base;
2399 } elsif (defined $hash) {
2400 $search_hash = $hash;
2401 } else {
2402 $search_hash = "HEAD";
2404 my $action = $my_uri;
2405 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2406 if ($use_pathinfo) {
2407 $action .= "/$project";
2408 } else {
2409 $cgi->param("p", $project);
2411 $cgi->param("a", "search");
2412 $cgi->param("h", $search_hash);
2413 print $cgi->startform(-method => "get", -action => $action) .
2414 "<div class=\"search\">\n" .
2415 (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") .
2416 $cgi->hidden(-name => "a") . "\n" .
2417 $cgi->hidden(-name => "h") . "\n" .
2418 $cgi->popup_menu(-name => 'st', -default => 'commit',
2419 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2420 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2421 " search:\n",
2422 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2423 "</div>" .
2424 $cgi->end_form() . "\n";
2428 sub git_footer_html {
2429 print "<div class=\"page_footer\">\n";
2430 if (defined $project) {
2431 my $descr = git_get_project_description($project);
2432 if (defined $descr) {
2433 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2435 print $cgi->a({-href => href(action=>"rss"),
2436 -class => "rss_logo"}, "RSS") . " ";
2437 print $cgi->a({-href => href(action=>"atom"),
2438 -class => "rss_logo"}, "Atom") . "\n";
2439 } else {
2440 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2441 -class => "rss_logo"}, "OPML") . " ";
2442 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2443 -class => "rss_logo"}, "TXT") . "\n";
2445 print "</div>\n" ;
2447 if (-f $site_footer) {
2448 open (my $fd, $site_footer);
2449 print <$fd>;
2450 close $fd;
2453 print "</body>\n" .
2454 "</html>";
2457 sub die_error {
2458 my $status = shift || "403 Forbidden";
2459 my $error = shift || "Malformed query, file missing or permission denied";
2461 git_header_html($status);
2462 print <<EOF;
2463 <div class="page_body">
2464 <br /><br />
2465 $status - $error
2466 <br />
2467 </div>
2469 git_footer_html();
2470 exit;
2473 ## ----------------------------------------------------------------------
2474 ## functions printing or outputting HTML: navigation
2476 sub git_print_page_nav {
2477 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2478 $extra = '' if !defined $extra; # pager or formats
2480 my @navs = qw(summary shortlog log commit commitdiff tree);
2481 if ($suppress) {
2482 @navs = grep { $_ ne $suppress } @navs;
2485 my %arg = map { $_ => {action=>$_} } @navs;
2486 if (defined $head) {
2487 for (qw(commit commitdiff)) {
2488 $arg{$_}{'hash'} = $head;
2490 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2491 for (qw(shortlog log)) {
2492 $arg{$_}{'hash'} = $head;
2496 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2497 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2499 print "<div class=\"page_nav\">\n" .
2500 (join " | ",
2501 map { $_ eq $current ?
2502 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2503 } @navs);
2504 print "<br/>\n$extra<br/>\n" .
2505 "</div>\n";
2508 sub format_paging_nav {
2509 my ($action, $hash, $head, $page, $nrevs) = @_;
2510 my $paging_nav;
2513 if ($hash ne $head || $page) {
2514 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2515 } else {
2516 $paging_nav .= "HEAD";
2519 if ($page > 0) {
2520 $paging_nav .= " &sdot; " .
2521 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2522 -accesskey => "p", -title => "Alt-p"}, "prev");
2523 } else {
2524 $paging_nav .= " &sdot; prev";
2527 if ($nrevs >= (100 * ($page+1)-1)) {
2528 $paging_nav .= " &sdot; " .
2529 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2530 -accesskey => "n", -title => "Alt-n"}, "next");
2531 } else {
2532 $paging_nav .= " &sdot; next";
2535 return $paging_nav;
2538 ## ......................................................................
2539 ## functions printing or outputting HTML: div
2541 sub git_print_header_div {
2542 my ($action, $title, $hash, $hash_base) = @_;
2543 my %args = ();
2545 $args{'action'} = $action;
2546 $args{'hash'} = $hash if $hash;
2547 $args{'hash_base'} = $hash_base if $hash_base;
2549 print "<div class=\"header\">\n" .
2550 $cgi->a({-href => href(%args), -class => "title"},
2551 $title ? $title : $action) .
2552 "\n</div>\n";
2555 #sub git_print_authorship (\%) {
2556 sub git_print_authorship {
2557 my $co = shift;
2559 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2560 print "<div class=\"author_date\">" .
2561 esc_html($co->{'author_name'}) .
2562 " [$ad{'rfc2822'}";
2563 if ($ad{'hour_local'} < 6) {
2564 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2565 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2566 } else {
2567 printf(" (%02d:%02d %s)",
2568 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2570 print "]</div>\n";
2573 sub git_print_page_path {
2574 my $name = shift;
2575 my $type = shift;
2576 my $hb = shift;
2579 print "<div class=\"page_path\">";
2580 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2581 -title => 'tree root'}, to_utf8("[$project]"));
2582 print " / ";
2583 if (defined $name) {
2584 my @dirname = split '/', $name;
2585 my $basename = pop @dirname;
2586 my $fullname = '';
2588 foreach my $dir (@dirname) {
2589 $fullname .= ($fullname ? '/' : '') . $dir;
2590 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2591 hash_base=>$hb),
2592 -title => $fullname}, esc_path($dir));
2593 print " / ";
2595 if (defined $type && $type eq 'blob') {
2596 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2597 hash_base=>$hb),
2598 -title => $name}, esc_path($basename));
2599 } elsif (defined $type && $type eq 'tree') {
2600 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2601 hash_base=>$hb),
2602 -title => $name}, esc_path($basename));
2603 print " / ";
2604 } else {
2605 print esc_path($basename);
2608 print "<br/></div>\n";
2611 # sub git_print_log (\@;%) {
2612 sub git_print_log ($;%) {
2613 my $log = shift;
2614 my %opts = @_;
2616 if ($opts{'-remove_title'}) {
2617 # remove title, i.e. first line of log
2618 shift @$log;
2620 # remove leading empty lines
2621 while (defined $log->[0] && $log->[0] eq "") {
2622 shift @$log;
2625 # print log
2626 my $signoff = 0;
2627 my $empty = 0;
2628 foreach my $line (@$log) {
2629 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2630 $signoff = 1;
2631 $empty = 0;
2632 if (! $opts{'-remove_signoff'}) {
2633 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2634 next;
2635 } else {
2636 # remove signoff lines
2637 next;
2639 } else {
2640 $signoff = 0;
2643 # print only one empty line
2644 # do not print empty line after signoff
2645 if ($line eq "") {
2646 next if ($empty || $signoff);
2647 $empty = 1;
2648 } else {
2649 $empty = 0;
2652 print format_log_line_html($line) . "<br/>\n";
2655 if ($opts{'-final_empty_line'}) {
2656 # end with single empty line
2657 print "<br/>\n" unless $empty;
2661 # return link target (what link points to)
2662 sub git_get_link_target {
2663 my $hash = shift;
2664 my $link_target;
2666 # read link
2667 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2668 or return;
2670 local $/;
2671 $link_target = <$fd>;
2673 close $fd
2674 or return;
2676 return $link_target;
2679 # given link target, and the directory (basedir) the link is in,
2680 # return target of link relative to top directory (top tree);
2681 # return undef if it is not possible (including absolute links).
2682 sub normalize_link_target {
2683 my ($link_target, $basedir, $hash_base) = @_;
2685 # we can normalize symlink target only if $hash_base is provided
2686 return unless $hash_base;
2688 # absolute symlinks (beginning with '/') cannot be normalized
2689 return if (substr($link_target, 0, 1) eq '/');
2691 # normalize link target to path from top (root) tree (dir)
2692 my $path;
2693 if ($basedir) {
2694 $path = $basedir . '/' . $link_target;
2695 } else {
2696 # we are in top (root) tree (dir)
2697 $path = $link_target;
2700 # remove //, /./, and /../
2701 my @path_parts;
2702 foreach my $part (split('/', $path)) {
2703 # discard '.' and ''
2704 next if (!$part || $part eq '.');
2705 # handle '..'
2706 if ($part eq '..') {
2707 if (@path_parts) {
2708 pop @path_parts;
2709 } else {
2710 # link leads outside repository (outside top dir)
2711 return;
2713 } else {
2714 push @path_parts, $part;
2717 $path = join('/', @path_parts);
2719 return $path;
2722 # print tree entry (row of git_tree), but without encompassing <tr> element
2723 sub git_print_tree_entry {
2724 my ($t, $basedir, $hash_base, $have_blame) = @_;
2726 my %base_key = ();
2727 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2729 # The format of a table row is: mode list link. Where mode is
2730 # the mode of the entry, list is the name of the entry, an href,
2731 # and link is the action links of the entry.
2733 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2734 if ($t->{'type'} eq "blob") {
2735 print "<td class=\"list\">" .
2736 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2737 file_name=>"$basedir$t->{'name'}", %base_key),
2738 -class => "list"}, esc_path($t->{'name'}));
2739 if (S_ISLNK(oct $t->{'mode'})) {
2740 my $link_target = git_get_link_target($t->{'hash'});
2741 if ($link_target) {
2742 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2743 if (defined $norm_target) {
2744 print " -> " .
2745 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2746 file_name=>$norm_target),
2747 -title => $norm_target}, esc_path($link_target));
2748 } else {
2749 print " -> " . esc_path($link_target);
2753 print "</td>\n";
2754 print "<td class=\"link\">";
2755 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2756 file_name=>"$basedir$t->{'name'}", %base_key)},
2757 "blob");
2758 if ($have_blame) {
2759 print " | " .
2760 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2761 file_name=>"$basedir$t->{'name'}", %base_key)},
2762 "blame");
2764 if (defined $hash_base) {
2765 print " | " .
2766 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2767 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2768 "history");
2770 print " | " .
2771 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2772 file_name=>"$basedir$t->{'name'}")},
2773 "raw");
2774 print "</td>\n";
2776 } elsif ($t->{'type'} eq "tree") {
2777 print "<td class=\"list\">";
2778 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2779 file_name=>"$basedir$t->{'name'}", %base_key)},
2780 esc_path($t->{'name'}));
2781 print "</td>\n";
2782 print "<td class=\"link\">";
2783 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2784 file_name=>"$basedir$t->{'name'}", %base_key)},
2785 "tree");
2786 if (defined $hash_base) {
2787 print " | " .
2788 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2789 file_name=>"$basedir$t->{'name'}")},
2790 "history");
2792 print "</td>\n";
2793 } else {
2794 # unknown object: we can only present history for it
2795 # (this includes 'commit' object, i.e. submodule support)
2796 print "<td class=\"list\">" .
2797 esc_path($t->{'name'}) .
2798 "</td>\n";
2799 print "<td class=\"link\">";
2800 if (defined $hash_base) {
2801 print $cgi->a({-href => href(action=>"history",
2802 hash_base=>$hash_base,
2803 file_name=>"$basedir$t->{'name'}")},
2804 "history");
2806 print "</td>\n";
2810 ## ......................................................................
2811 ## functions printing large fragments of HTML
2813 # get pre-image filenames for merge (combined) diff
2814 sub fill_from_file_info {
2815 my ($diff, @parents) = @_;
2817 $diff->{'from_file'} = [ ];
2818 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2819 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2820 if ($diff->{'status'}[$i] eq 'R' ||
2821 $diff->{'status'}[$i] eq 'C') {
2822 $diff->{'from_file'}[$i] =
2823 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2827 return $diff;
2830 # is current raw difftree line of file deletion
2831 sub is_deleted {
2832 my $diffinfo = shift;
2834 return $diffinfo->{'status_str'} =~ /D/;
2837 # does patch correspond to [previous] difftree raw line
2838 # $diffinfo - hashref of parsed raw diff format
2839 # $patchinfo - hashref of parsed patch diff format
2840 # (the same keys as in $diffinfo)
2841 sub is_patch_split {
2842 my ($diffinfo, $patchinfo) = @_;
2844 return defined $diffinfo && defined $patchinfo
2845 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
2849 sub git_difftree_body {
2850 my ($difftree, $hash, @parents) = @_;
2851 my ($parent) = $parents[0];
2852 my ($have_blame) = gitweb_check_feature('blame');
2853 print "<div class=\"list_head\">\n";
2854 if ($#{$difftree} > 10) {
2855 print(($#{$difftree} + 1) . " files changed:\n");
2857 print "</div>\n";
2859 print "<table class=\"" .
2860 (@parents > 1 ? "combined " : "") .
2861 "diff_tree\">\n";
2863 # header only for combined diff in 'commitdiff' view
2864 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
2865 if ($has_header) {
2866 # table header
2867 print "<thead><tr>\n" .
2868 "<th></th><th></th>\n"; # filename, patchN link
2869 for (my $i = 0; $i < @parents; $i++) {
2870 my $par = $parents[$i];
2871 print "<th>" .
2872 $cgi->a({-href => href(action=>"commitdiff",
2873 hash=>$hash, hash_parent=>$par),
2874 -title => 'commitdiff to parent number ' .
2875 ($i+1) . ': ' . substr($par,0,7)},
2876 $i+1) .
2877 "&nbsp;</th>\n";
2879 print "</tr></thead>\n<tbody>\n";
2882 my $alternate = 1;
2883 my $patchno = 0;
2884 foreach my $line (@{$difftree}) {
2885 my $diff = parsed_difftree_line($line);
2887 if ($alternate) {
2888 print "<tr class=\"dark\">\n";
2889 } else {
2890 print "<tr class=\"light\">\n";
2892 $alternate ^= 1;
2894 if (exists $diff->{'nparents'}) { # combined diff
2896 fill_from_file_info($diff, @parents)
2897 unless exists $diff->{'from_file'};
2899 if (!is_deleted($diff)) {
2900 # file exists in the result (child) commit
2901 print "<td>" .
2902 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2903 file_name=>$diff->{'to_file'},
2904 hash_base=>$hash),
2905 -class => "list"}, esc_path($diff->{'to_file'})) .
2906 "</td>\n";
2907 } else {
2908 print "<td>" .
2909 esc_path($diff->{'to_file'}) .
2910 "</td>\n";
2913 if ($action eq 'commitdiff') {
2914 # link to patch
2915 $patchno++;
2916 print "<td class=\"link\">" .
2917 $cgi->a({-href => "#patch$patchno"}, "patch") .
2918 " | " .
2919 "</td>\n";
2922 my $has_history = 0;
2923 my $not_deleted = 0;
2924 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2925 my $hash_parent = $parents[$i];
2926 my $from_hash = $diff->{'from_id'}[$i];
2927 my $from_path = $diff->{'from_file'}[$i];
2928 my $status = $diff->{'status'}[$i];
2930 $has_history ||= ($status ne 'A');
2931 $not_deleted ||= ($status ne 'D');
2933 if ($status eq 'A') {
2934 print "<td class=\"link\" align=\"right\"> | </td>\n";
2935 } elsif ($status eq 'D') {
2936 print "<td class=\"link\">" .
2937 $cgi->a({-href => href(action=>"blob",
2938 hash_base=>$hash,
2939 hash=>$from_hash,
2940 file_name=>$from_path)},
2941 "blob" . ($i+1)) .
2942 " | </td>\n";
2943 } else {
2944 if ($diff->{'to_id'} eq $from_hash) {
2945 print "<td class=\"link nochange\">";
2946 } else {
2947 print "<td class=\"link\">";
2949 print $cgi->a({-href => href(action=>"blobdiff",
2950 hash=>$diff->{'to_id'},
2951 hash_parent=>$from_hash,
2952 hash_base=>$hash,
2953 hash_parent_base=>$hash_parent,
2954 file_name=>$diff->{'to_file'},
2955 file_parent=>$from_path)},
2956 "diff" . ($i+1)) .
2957 " | </td>\n";
2961 print "<td class=\"link\">";
2962 if ($not_deleted) {
2963 print $cgi->a({-href => href(action=>"blob",
2964 hash=>$diff->{'to_id'},
2965 file_name=>$diff->{'to_file'},
2966 hash_base=>$hash)},
2967 "blob");
2968 print " | " if ($has_history);
2970 if ($has_history) {
2971 print $cgi->a({-href => href(action=>"history",
2972 file_name=>$diff->{'to_file'},
2973 hash_base=>$hash)},
2974 "history");
2976 print "</td>\n";
2978 print "</tr>\n";
2979 next; # instead of 'else' clause, to avoid extra indent
2981 # else ordinary diff
2983 my ($to_mode_oct, $to_mode_str, $to_file_type);
2984 my ($from_mode_oct, $from_mode_str, $from_file_type);
2985 if ($diff->{'to_mode'} ne ('0' x 6)) {
2986 $to_mode_oct = oct $diff->{'to_mode'};
2987 if (S_ISREG($to_mode_oct)) { # only for regular file
2988 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2990 $to_file_type = file_type($diff->{'to_mode'});
2992 if ($diff->{'from_mode'} ne ('0' x 6)) {
2993 $from_mode_oct = oct $diff->{'from_mode'};
2994 if (S_ISREG($to_mode_oct)) { # only for regular file
2995 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2997 $from_file_type = file_type($diff->{'from_mode'});
3000 if ($diff->{'status'} eq "A") { # created
3001 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3002 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3003 $mode_chng .= "]</span>";
3004 print "<td>";
3005 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3006 hash_base=>$hash, file_name=>$diff->{'file'}),
3007 -class => "list"}, esc_path($diff->{'file'}));
3008 print "</td>\n";
3009 print "<td>$mode_chng</td>\n";
3010 print "<td class=\"link\">";
3011 if ($action eq 'commitdiff') {
3012 # link to patch
3013 $patchno++;
3014 print $cgi->a({-href => "#patch$patchno"}, "patch");
3015 print " | ";
3017 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3018 hash_base=>$hash, file_name=>$diff->{'file'})},
3019 "blob");
3020 print "</td>\n";
3022 } elsif ($diff->{'status'} eq "D") { # deleted
3023 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3024 print "<td>";
3025 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3026 hash_base=>$parent, file_name=>$diff->{'file'}),
3027 -class => "list"}, esc_path($diff->{'file'}));
3028 print "</td>\n";
3029 print "<td>$mode_chng</td>\n";
3030 print "<td class=\"link\">";
3031 if ($action eq 'commitdiff') {
3032 # link to patch
3033 $patchno++;
3034 print $cgi->a({-href => "#patch$patchno"}, "patch");
3035 print " | ";
3037 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3038 hash_base=>$parent, file_name=>$diff->{'file'})},
3039 "blob") . " | ";
3040 if ($have_blame) {
3041 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3042 file_name=>$diff->{'file'})},
3043 "blame") . " | ";
3045 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3046 file_name=>$diff->{'file'})},
3047 "history");
3048 print "</td>\n";
3050 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3051 my $mode_chnge = "";
3052 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3053 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3054 if ($from_file_type ne $to_file_type) {
3055 $mode_chnge .= " from $from_file_type to $to_file_type";
3057 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3058 if ($from_mode_str && $to_mode_str) {
3059 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3060 } elsif ($to_mode_str) {
3061 $mode_chnge .= " mode: $to_mode_str";
3064 $mode_chnge .= "]</span>\n";
3066 print "<td>";
3067 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3068 hash_base=>$hash, file_name=>$diff->{'file'}),
3069 -class => "list"}, esc_path($diff->{'file'}));
3070 print "</td>\n";
3071 print "<td>$mode_chnge</td>\n";
3072 print "<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 onlu mode changed)
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->{'file'})},
3084 "diff") .
3085 " | ";
3087 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3088 hash_base=>$hash, file_name=>$diff->{'file'})},
3089 "blob") . " | ";
3090 if ($have_blame) {
3091 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3092 file_name=>$diff->{'file'})},
3093 "blame") . " | ";
3095 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3096 file_name=>$diff->{'file'})},
3097 "history");
3098 print "</td>\n";
3100 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3101 my %status_name = ('R' => 'moved', 'C' => 'copied');
3102 my $nstatus = $status_name{$diff->{'status'}};
3103 my $mode_chng = "";
3104 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3105 # mode also for directories, so we cannot use $to_mode_str
3106 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3108 print "<td>" .
3109 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3110 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3111 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3112 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3113 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3114 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3115 -class => "list"}, esc_path($diff->{'from_file'})) .
3116 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3117 "<td class=\"link\">";
3118 if ($action eq 'commitdiff') {
3119 # link to patch
3120 $patchno++;
3121 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3122 " | ";
3123 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3124 # "commit" view and modified file (not only pure rename or copy)
3125 print $cgi->a({-href => href(action=>"blobdiff",
3126 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3127 hash_base=>$hash, hash_parent_base=>$parent,
3128 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3129 "diff") .
3130 " | ";
3132 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3133 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3134 "blob") . " | ";
3135 if ($have_blame) {
3136 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3137 file_name=>$diff->{'to_file'})},
3138 "blame") . " | ";
3140 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3141 file_name=>$diff->{'to_file'})},
3142 "history");
3143 print "</td>\n";
3145 } # we should not encounter Unmerged (U) or Unknown (X) status
3146 print "</tr>\n";
3148 print "</tbody>" if $has_header;
3149 print "</table>\n";
3152 sub git_patchset_body {
3153 my ($fd, $difftree, $hash, @hash_parents) = @_;
3154 my ($hash_parent) = $hash_parents[0];
3156 my $is_combined = (@hash_parents > 1);
3157 my $patch_idx = 0;
3158 my $patch_number = 0;
3159 my $patch_line;
3160 my $diffinfo;
3161 my $to_name;
3162 my (%from, %to);
3164 print "<div class=\"patchset\">\n";
3166 # skip to first patch
3167 while ($patch_line = <$fd>) {
3168 chomp $patch_line;
3170 last if ($patch_line =~ m/^diff /);
3173 PATCH:
3174 while ($patch_line) {
3176 # parse "git diff" header line
3177 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3178 # $1 is from_name, which we do not use
3179 $to_name = unquote($2);
3180 $to_name =~ s!^b/!!;
3181 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3182 # $1 is 'cc' or 'combined', which we do not use
3183 $to_name = unquote($2);
3184 } else {
3185 $to_name = undef;
3188 # check if current patch belong to current raw line
3189 # and parse raw git-diff line if needed
3190 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3191 # this is continuation of a split patch
3192 print "<div class=\"patch cont\">\n";
3193 } else {
3194 # advance raw git-diff output if needed
3195 $patch_idx++ if defined $diffinfo;
3197 # read and prepare patch information
3198 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3200 # compact combined diff output can have some patches skipped
3201 # find which patch (using pathname of result) we are at now;
3202 if ($is_combined) {
3203 while ($to_name ne $diffinfo->{'to_file'}) {
3204 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3205 format_diff_cc_simplified($diffinfo, @hash_parents) .
3206 "</div>\n"; # class="patch"
3208 $patch_idx++;
3209 $patch_number++;
3211 last if $patch_idx > $#$difftree;
3212 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3216 # modifies %from, %to hashes
3217 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3219 # this is first patch for raw difftree line with $patch_idx index
3220 # we index @$difftree array from 0, but number patches from 1
3221 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3224 # git diff header
3225 #assert($patch_line =~ m/^diff /) if DEBUG;
3226 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3227 $patch_number++;
3228 # print "git diff" header
3229 print format_git_diff_header_line($patch_line, $diffinfo,
3230 \%from, \%to);
3232 # print extended diff header
3233 print "<div class=\"diff extended_header\">\n";
3234 EXTENDED_HEADER:
3235 while ($patch_line = <$fd>) {
3236 chomp $patch_line;
3238 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3240 print format_extended_diff_header_line($patch_line, $diffinfo,
3241 \%from, \%to);
3243 print "</div>\n"; # class="diff extended_header"
3245 # from-file/to-file diff header
3246 if (! $patch_line) {
3247 print "</div>\n"; # class="patch"
3248 last PATCH;
3250 next PATCH if ($patch_line =~ m/^diff /);
3251 #assert($patch_line =~ m/^---/) if DEBUG;
3253 my $last_patch_line = $patch_line;
3254 $patch_line = <$fd>;
3255 chomp $patch_line;
3256 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3258 print format_diff_from_to_header($last_patch_line, $patch_line,
3259 $diffinfo, \%from, \%to,
3260 @hash_parents);
3262 # the patch itself
3263 LINE:
3264 while ($patch_line = <$fd>) {
3265 chomp $patch_line;
3267 next PATCH if ($patch_line =~ m/^diff /);
3269 print format_diff_line($patch_line, \%from, \%to);
3272 } continue {
3273 print "</div>\n"; # class="patch"
3276 # for compact combined (--cc) format, with chunk and patch simpliciaction
3277 # patchset might be empty, but there might be unprocessed raw lines
3278 for (++$patch_idx if $patch_number > 0;
3279 $patch_idx < @$difftree;
3280 ++$patch_idx) {
3281 # read and prepare patch information
3282 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3284 # generate anchor for "patch" links in difftree / whatchanged part
3285 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3286 format_diff_cc_simplified($diffinfo, @hash_parents) .
3287 "</div>\n"; # class="patch"
3289 $patch_number++;
3292 if ($patch_number == 0) {
3293 if (@hash_parents > 1) {
3294 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3295 } else {
3296 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3300 print "</div>\n"; # class="patchset"
3303 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3305 sub git_project_list_body {
3306 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3308 my ($check_forks) = gitweb_check_feature('forks');
3310 my @projects;
3311 foreach my $pr (@$projlist) {
3312 my (@aa) = git_get_last_activity($pr->{'path'});
3313 unless (@aa) {
3314 next;
3316 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3317 if (!defined $pr->{'descr'}) {
3318 my $descr = git_get_project_description($pr->{'path'}) || "";
3319 $pr->{'descr_long'} = to_utf8($descr);
3320 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3322 if (!defined $pr->{'owner'}) {
3323 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3325 if ($check_forks) {
3326 my $pname = $pr->{'path'};
3327 if (($pname =~ s/\.git$//) &&
3328 ($pname !~ /\/$/) &&
3329 (-d "$projectroot/$pname")) {
3330 $pr->{'forks'} = "-d $projectroot/$pname";
3332 else {
3333 $pr->{'forks'} = 0;
3336 push @projects, $pr;
3339 $order ||= $default_projects_order;
3340 $from = 0 unless defined $from;
3341 $to = $#projects if (!defined $to || $#projects < $to);
3343 print "<table class=\"project_list\">\n";
3344 unless ($no_header) {
3345 print "<tr>\n";
3346 if ($check_forks) {
3347 print "<th></th>\n";
3349 if ($order eq "project") {
3350 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3351 print "<th>Project</th>\n";
3352 } else {
3353 print "<th>" .
3354 $cgi->a({-href => href(project=>undef, order=>'project'),
3355 -class => "header"}, "Project") .
3356 "</th>\n";
3358 if ($order eq "descr") {
3359 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3360 print "<th>Description</th>\n";
3361 } else {
3362 print "<th>" .
3363 $cgi->a({-href => href(project=>undef, order=>'descr'),
3364 -class => "header"}, "Description") .
3365 "</th>\n";
3367 if ($order eq "owner") {
3368 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3369 print "<th>Owner</th>\n";
3370 } else {
3371 print "<th>" .
3372 $cgi->a({-href => href(project=>undef, order=>'owner'),
3373 -class => "header"}, "Owner") .
3374 "</th>\n";
3376 if ($order eq "age") {
3377 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3378 print "<th>Last Change</th>\n";
3379 } else {
3380 print "<th>" .
3381 $cgi->a({-href => href(project=>undef, order=>'age'),
3382 -class => "header"}, "Last Change") .
3383 "</th>\n";
3385 print "<th></th>\n" .
3386 "</tr>\n";
3388 my $alternate = 1;
3389 for (my $i = $from; $i <= $to; $i++) {
3390 my $pr = $projects[$i];
3391 if ($alternate) {
3392 print "<tr class=\"dark\">\n";
3393 } else {
3394 print "<tr class=\"light\">\n";
3396 $alternate ^= 1;
3397 if ($check_forks) {
3398 print "<td>";
3399 if ($pr->{'forks'}) {
3400 print "<!-- $pr->{'forks'} -->\n";
3401 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3403 print "</td>\n";
3405 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3406 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3407 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3408 -class => "list", -title => $pr->{'descr_long'}},
3409 esc_html($pr->{'descr'})) . "</td>\n" .
3410 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3411 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3412 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3413 "<td class=\"link\">" .
3414 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3415 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3416 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3417 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3418 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3419 "</td>\n" .
3420 "</tr>\n";
3422 if (defined $extra) {
3423 print "<tr>\n";
3424 if ($check_forks) {
3425 print "<td></td>\n";
3427 print "<td colspan=\"5\">$extra</td>\n" .
3428 "</tr>\n";
3430 print "</table>\n";
3433 sub git_shortlog_body {
3434 # uses global variable $project
3435 my ($commitlist, $from, $to, $refs, $extra) = @_;
3437 $from = 0 unless defined $from;
3438 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3440 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3441 my $alternate = 1;
3442 for (my $i = $from; $i <= $to; $i++) {
3443 my %co = %{$commitlist->[$i]};
3444 my $commit = $co{'id'};
3445 my $ref = format_ref_marker($refs, $commit);
3446 if ($alternate) {
3447 print "<tr class=\"dark\">\n";
3448 } else {
3449 print "<tr class=\"light\">\n";
3451 $alternate ^= 1;
3452 my $author = chop_and_escape_str($co{'author_name'}, 10);
3453 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3454 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3455 "<td><i>" . $author . "</i></td>\n" .
3456 "<td>";
3457 print format_subject_html($co{'title'}, $co{'title_short'},
3458 href(action=>"commit", hash=>$commit), $ref);
3459 print "</td>\n" .
3460 "<td class=\"link\">" .
3461 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3462 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3463 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3464 my $snapshot_links = format_snapshot_links($commit);
3465 if (defined $snapshot_links) {
3466 print " | " . $snapshot_links;
3468 print "</td>\n" .
3469 "</tr>\n";
3471 if (defined $extra) {
3472 print "<tr>\n" .
3473 "<td colspan=\"4\">$extra</td>\n" .
3474 "</tr>\n";
3476 print "</table>\n";
3479 sub git_history_body {
3480 # Warning: assumes constant type (blob or tree) during history
3481 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3483 $from = 0 unless defined $from;
3484 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3486 print "<table class=\"history\" cellspacing=\"0\">\n";
3487 my $alternate = 1;
3488 for (my $i = $from; $i <= $to; $i++) {
3489 my %co = %{$commitlist->[$i]};
3490 if (!%co) {
3491 next;
3493 my $commit = $co{'id'};
3495 my $ref = format_ref_marker($refs, $commit);
3497 if ($alternate) {
3498 print "<tr class=\"dark\">\n";
3499 } else {
3500 print "<tr class=\"light\">\n";
3502 $alternate ^= 1;
3503 # shortlog uses chop_str($co{'author_name'}, 10)
3504 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3505 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3506 "<td><i>" . $author . "</i></td>\n" .
3507 "<td>";
3508 # originally git_history used chop_str($co{'title'}, 50)
3509 print format_subject_html($co{'title'}, $co{'title_short'},
3510 href(action=>"commit", hash=>$commit), $ref);
3511 print "</td>\n" .
3512 "<td class=\"link\">" .
3513 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3514 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3516 if ($ftype eq 'blob') {
3517 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3518 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3519 if (defined $blob_current && defined $blob_parent &&
3520 $blob_current ne $blob_parent) {
3521 print " | " .
3522 $cgi->a({-href => href(action=>"blobdiff",
3523 hash=>$blob_current, hash_parent=>$blob_parent,
3524 hash_base=>$hash_base, hash_parent_base=>$commit,
3525 file_name=>$file_name)},
3526 "diff to current");
3529 print "</td>\n" .
3530 "</tr>\n";
3532 if (defined $extra) {
3533 print "<tr>\n" .
3534 "<td colspan=\"4\">$extra</td>\n" .
3535 "</tr>\n";
3537 print "</table>\n";
3540 sub git_tags_body {
3541 # uses global variable $project
3542 my ($taglist, $from, $to, $extra) = @_;
3543 $from = 0 unless defined $from;
3544 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3546 print "<table class=\"tags\" cellspacing=\"0\">\n";
3547 my $alternate = 1;
3548 for (my $i = $from; $i <= $to; $i++) {
3549 my $entry = $taglist->[$i];
3550 my %tag = %$entry;
3551 my $comment = $tag{'subject'};
3552 my $comment_short;
3553 if (defined $comment) {
3554 $comment_short = chop_str($comment, 30, 5);
3556 if ($alternate) {
3557 print "<tr class=\"dark\">\n";
3558 } else {
3559 print "<tr class=\"light\">\n";
3561 $alternate ^= 1;
3562 if (defined $tag{'age'}) {
3563 print "<td><i>$tag{'age'}</i></td>\n";
3564 } else {
3565 print "<td></td>\n";
3567 print "<td>" .
3568 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3569 -class => "list name"}, esc_html($tag{'name'})) .
3570 "</td>\n" .
3571 "<td>";
3572 if (defined $comment) {
3573 print format_subject_html($comment, $comment_short,
3574 href(action=>"tag", hash=>$tag{'id'}));
3576 print "</td>\n" .
3577 "<td class=\"selflink\">";
3578 if ($tag{'type'} eq "tag") {
3579 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3580 } else {
3581 print "&nbsp;";
3583 print "</td>\n" .
3584 "<td class=\"link\">" . " | " .
3585 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3586 if ($tag{'reftype'} eq "commit") {
3587 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3588 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3589 } elsif ($tag{'reftype'} eq "blob") {
3590 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3592 print "</td>\n" .
3593 "</tr>";
3595 if (defined $extra) {
3596 print "<tr>\n" .
3597 "<td colspan=\"5\">$extra</td>\n" .
3598 "</tr>\n";
3600 print "</table>\n";
3603 sub git_heads_body {
3604 # uses global variable $project
3605 my ($headlist, $head, $from, $to, $extra) = @_;
3606 $from = 0 unless defined $from;
3607 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3609 print "<table class=\"heads\" cellspacing=\"0\">\n";
3610 my $alternate = 1;
3611 for (my $i = $from; $i <= $to; $i++) {
3612 my $entry = $headlist->[$i];
3613 my %ref = %$entry;
3614 my $curr = $ref{'id'} eq $head;
3615 if ($alternate) {
3616 print "<tr class=\"dark\">\n";
3617 } else {
3618 print "<tr class=\"light\">\n";
3620 $alternate ^= 1;
3621 print "<td><i>$ref{'age'}</i></td>\n" .
3622 ($curr ? "<td class=\"current_head\">" : "<td>") .
3623 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3624 -class => "list name"},esc_html($ref{'name'})) .
3625 "</td>\n" .
3626 "<td class=\"link\">" .
3627 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3628 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3629 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3630 "</td>\n" .
3631 "</tr>";
3633 if (defined $extra) {
3634 print "<tr>\n" .
3635 "<td colspan=\"3\">$extra</td>\n" .
3636 "</tr>\n";
3638 print "</table>\n";
3641 sub git_search_grep_body {
3642 my ($commitlist, $from, $to, $extra) = @_;
3643 $from = 0 unless defined $from;
3644 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3646 print "<table class=\"grep\" cellspacing=\"0\">\n";
3647 my $alternate = 1;
3648 for (my $i = $from; $i <= $to; $i++) {
3649 my %co = %{$commitlist->[$i]};
3650 if (!%co) {
3651 next;
3653 my $commit = $co{'id'};
3654 if ($alternate) {
3655 print "<tr class=\"dark\">\n";
3656 } else {
3657 print "<tr class=\"light\">\n";
3659 $alternate ^= 1;
3660 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3661 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3662 "<td><i>" . $author . "</i></td>\n" .
3663 "<td>" .
3664 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3665 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3666 my $comment = $co{'comment'};
3667 foreach my $line (@$comment) {
3668 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3669 my $lead = esc_html($1) || "";
3670 $lead = chop_str($lead, 30, 10);
3671 my $match = esc_html($2) || "";
3672 my $trail = esc_html($3) || "";
3673 $trail = chop_str($trail, 30, 10);
3674 my $text = "$lead<span class=\"match\">$match</span>$trail";
3675 print chop_str($text, 80, 5) . "<br/>\n";
3678 print "</td>\n" .
3679 "<td class=\"link\">" .
3680 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3681 " | " .
3682 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3683 print "</td>\n" .
3684 "</tr>\n";
3686 if (defined $extra) {
3687 print "<tr>\n" .
3688 "<td colspan=\"3\">$extra</td>\n" .
3689 "</tr>\n";
3691 print "</table>\n";
3694 ## ======================================================================
3695 ## ======================================================================
3696 ## actions
3698 sub git_project_list {
3699 my $order = $cgi->param('o');
3700 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3701 die_error(undef, "Unknown order parameter");
3704 my @list = git_get_projects_list();
3705 if (!@list) {
3706 die_error(undef, "No projects found");
3709 git_header_html();
3710 if (-f $home_text) {
3711 print "<div class=\"index_include\">\n";
3712 open (my $fd, $home_text);
3713 print <$fd>;
3714 close $fd;
3715 print "</div>\n";
3717 git_project_list_body(\@list, $order);
3718 git_footer_html();
3721 sub git_forks {
3722 my $order = $cgi->param('o');
3723 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3724 die_error(undef, "Unknown order parameter");
3727 my @list = git_get_projects_list($project);
3728 if (!@list) {
3729 die_error(undef, "No forks found");
3732 git_header_html();
3733 git_print_page_nav('','');
3734 git_print_header_div('summary', "$project forks");
3735 git_project_list_body(\@list, $order);
3736 git_footer_html();
3739 sub git_project_index {
3740 my @projects = git_get_projects_list($project);
3742 print $cgi->header(
3743 -type => 'text/plain',
3744 -charset => 'utf-8',
3745 -content_disposition => 'inline; filename="index.aux"');
3747 foreach my $pr (@projects) {
3748 if (!exists $pr->{'owner'}) {
3749 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
3752 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3753 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3754 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3755 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3756 $path =~ s/ /\+/g;
3757 $owner =~ s/ /\+/g;
3759 print "$path $owner\n";
3763 sub git_summary {
3764 my $descr = git_get_project_description($project) || "none";
3765 my %co = parse_commit("HEAD");
3766 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3767 my $head = $co{'id'};
3769 my $owner = git_get_project_owner($project);
3771 my $refs = git_get_references();
3772 # These get_*_list functions return one more to allow us to see if
3773 # there are more ...
3774 my @taglist = git_get_tags_list(16);
3775 my @headlist = git_get_heads_list(16);
3776 my @forklist;
3777 my ($check_forks) = gitweb_check_feature('forks');
3779 if ($check_forks) {
3780 @forklist = git_get_projects_list($project);
3783 git_header_html();
3784 git_print_page_nav('summary','', $head);
3786 print "<div class=\"title\">&nbsp;</div>\n";
3787 print "<table cellspacing=\"0\">\n" .
3788 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3789 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
3790 if (defined $cd{'rfc2822'}) {
3791 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3794 # use per project git URL list in $projectroot/$project/cloneurl
3795 # or make project git URL from git base URL and project name
3796 my $url_tag = "URL";
3797 my @url_list = git_get_project_url_list($project);
3798 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3799 foreach my $git_url (@url_list) {
3800 next unless $git_url;
3801 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3802 $url_tag = "";
3804 print "</table>\n";
3806 if (-s "$projectroot/$project/README.html") {
3807 if (open my $fd, "$projectroot/$project/README.html") {
3808 print "<div class=\"title\">readme</div>\n";
3809 print $_ while (<$fd>);
3810 close $fd;
3814 # we need to request one more than 16 (0..15) to check if
3815 # those 16 are all
3816 my @commitlist = $head ? parse_commits($head, 17) : ();
3817 if (@commitlist) {
3818 git_print_header_div('shortlog');
3819 git_shortlog_body(\@commitlist, 0, 15, $refs,
3820 $#commitlist <= 15 ? undef :
3821 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3824 if (@taglist) {
3825 git_print_header_div('tags');
3826 git_tags_body(\@taglist, 0, 15,
3827 $#taglist <= 15 ? undef :
3828 $cgi->a({-href => href(action=>"tags")}, "..."));
3831 if (@headlist) {
3832 git_print_header_div('heads');
3833 git_heads_body(\@headlist, $head, 0, 15,
3834 $#headlist <= 15 ? undef :
3835 $cgi->a({-href => href(action=>"heads")}, "..."));
3838 if (@forklist) {
3839 git_print_header_div('forks');
3840 git_project_list_body(\@forklist, undef, 0, 15,
3841 $#forklist <= 15 ? undef :
3842 $cgi->a({-href => href(action=>"forks")}, "..."),
3843 'noheader');
3846 git_footer_html();
3849 sub git_tag {
3850 my $head = git_get_head_hash($project);
3851 git_header_html();
3852 git_print_page_nav('','', $head,undef,$head);
3853 my %tag = parse_tag($hash);
3855 if (! %tag) {
3856 die_error(undef, "Unknown tag object");
3859 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3860 print "<div class=\"title_text\">\n" .
3861 "<table cellspacing=\"0\">\n" .
3862 "<tr>\n" .
3863 "<td>object</td>\n" .
3864 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3865 $tag{'object'}) . "</td>\n" .
3866 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3867 $tag{'type'}) . "</td>\n" .
3868 "</tr>\n";
3869 if (defined($tag{'author'})) {
3870 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3871 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3872 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3873 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3874 "</td></tr>\n";
3876 print "</table>\n\n" .
3877 "</div>\n";
3878 print "<div class=\"page_body\">";
3879 my $comment = $tag{'comment'};
3880 foreach my $line (@$comment) {
3881 chomp $line;
3882 print esc_html($line, -nbsp=>1) . "<br/>\n";
3884 print "</div>\n";
3885 git_footer_html();
3888 sub git_blame2 {
3889 my $fd;
3890 my $ftype;
3892 my ($have_blame) = gitweb_check_feature('blame');
3893 if (!$have_blame) {
3894 die_error('403 Permission denied', "Permission denied");
3896 die_error('404 Not Found', "File name not defined") if (!$file_name);
3897 $hash_base ||= git_get_head_hash($project);
3898 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3899 my %co = parse_commit($hash_base)
3900 or die_error(undef, "Reading commit failed");
3901 if (!defined $hash) {
3902 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3903 or die_error(undef, "Error looking up file");
3905 $ftype = git_get_type($hash);
3906 if ($ftype !~ "blob") {
3907 die_error('400 Bad Request', "Object is not a blob");
3909 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3910 $file_name, $hash_base)
3911 or die_error(undef, "Open git-blame failed");
3912 git_header_html();
3913 my $formats_nav =
3914 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3915 "blob") .
3916 " | " .
3917 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3918 "history") .
3919 " | " .
3920 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3921 "HEAD");
3922 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3923 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3924 git_print_page_path($file_name, $ftype, $hash_base);
3925 my @rev_color = (qw(light2 dark2));
3926 my $num_colors = scalar(@rev_color);
3927 my $current_color = 0;
3928 my $last_rev;
3929 print <<HTML;
3930 <div class="page_body">
3931 <table class="blame">
3932 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3933 HTML
3934 my %metainfo = ();
3935 while (1) {
3936 $_ = <$fd>;
3937 last unless defined $_;
3938 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3939 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3940 if (!exists $metainfo{$full_rev}) {
3941 $metainfo{$full_rev} = {};
3943 my $meta = $metainfo{$full_rev};
3944 while (<$fd>) {
3945 last if (s/^\t//);
3946 if (/^(\S+) (.*)$/) {
3947 $meta->{$1} = $2;
3950 my $data = $_;
3951 chomp $data;
3952 my $rev = substr($full_rev, 0, 8);
3953 my $author = $meta->{'author'};
3954 my %date = parse_date($meta->{'author-time'},
3955 $meta->{'author-tz'});
3956 my $date = $date{'iso-tz'};
3957 if ($group_size) {
3958 $current_color = ++$current_color % $num_colors;
3960 print "<tr class=\"$rev_color[$current_color]\">\n";
3961 if ($group_size) {
3962 print "<td class=\"sha1\"";
3963 print " title=\"". esc_html($author) . ", $date\"";
3964 print " rowspan=\"$group_size\"" if ($group_size > 1);
3965 print ">";
3966 print $cgi->a({-href => href(action=>"commit",
3967 hash=>$full_rev,
3968 file_name=>$file_name)},
3969 esc_html($rev));
3970 print "</td>\n";
3972 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3973 or die_error(undef, "Open git-rev-parse failed");
3974 my $parent_commit = <$dd>;
3975 close $dd;
3976 chomp($parent_commit);
3977 my $blamed = href(action => 'blame',
3978 file_name => $meta->{'filename'},
3979 hash_base => $parent_commit);
3980 print "<td class=\"linenr\">";
3981 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3982 -id => "l$lineno",
3983 -class => "linenr" },
3984 esc_html($lineno));
3985 print "</td>";
3986 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3987 print "</tr>\n";
3989 print "</table>\n";
3990 print "</div>";
3991 close $fd
3992 or print "Reading blob failed\n";
3993 git_footer_html();
3996 sub git_blame {
3997 my $fd;
3999 my ($have_blame) = gitweb_check_feature('blame');
4000 if (!$have_blame) {
4001 die_error('403 Permission denied', "Permission denied");
4003 die_error('404 Not Found', "File name not defined") if (!$file_name);
4004 $hash_base ||= git_get_head_hash($project);
4005 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4006 my %co = parse_commit($hash_base)
4007 or die_error(undef, "Reading commit failed");
4008 if (!defined $hash) {
4009 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4010 or die_error(undef, "Error lookup file");
4012 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
4013 or die_error(undef, "Open git-annotate failed");
4014 git_header_html();
4015 my $formats_nav =
4016 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4017 "blob") .
4018 " | " .
4019 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4020 "history") .
4021 " | " .
4022 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4023 "HEAD");
4024 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4025 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4026 git_print_page_path($file_name, 'blob', $hash_base);
4027 print "<div class=\"page_body\">\n";
4028 print <<HTML;
4029 <table class="blame">
4030 <tr>
4031 <th>Commit</th>
4032 <th>Age</th>
4033 <th>Author</th>
4034 <th>Line</th>
4035 <th>Data</th>
4036 </tr>
4037 HTML
4038 my @line_class = (qw(light dark));
4039 my $line_class_len = scalar (@line_class);
4040 my $line_class_num = $#line_class;
4041 while (my $line = <$fd>) {
4042 my $long_rev;
4043 my $short_rev;
4044 my $author;
4045 my $time;
4046 my $lineno;
4047 my $data;
4048 my $age;
4049 my $age_str;
4050 my $age_class;
4052 chomp $line;
4053 $line_class_num = ($line_class_num + 1) % $line_class_len;
4055 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
4056 $long_rev = $1;
4057 $author = $2;
4058 $time = $3;
4059 $lineno = $4;
4060 $data = $5;
4061 } else {
4062 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
4063 next;
4065 $short_rev = substr ($long_rev, 0, 8);
4066 $age = time () - $time;
4067 $age_str = age_string ($age);
4068 $age_str =~ s/ /&nbsp;/g;
4069 $age_class = age_class($age);
4070 $author = esc_html ($author);
4071 $author =~ s/ /&nbsp;/g;
4073 $data = untabify($data);
4074 $data = esc_html ($data);
4076 print <<HTML;
4077 <tr class="$line_class[$line_class_num]">
4078 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
4079 <td class="$age_class">$age_str</td>
4080 <td>$author</td>
4081 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4082 <td class="pre">$data</td>
4083 </tr>
4084 HTML
4085 } # while (my $line = <$fd>)
4086 print "</table>\n\n";
4087 close $fd
4088 or print "Reading blob failed.\n";
4089 print "</div>";
4090 git_footer_html();
4093 sub git_tags {
4094 my $head = git_get_head_hash($project);
4095 git_header_html();
4096 git_print_page_nav('','', $head,undef,$head);
4097 git_print_header_div('summary', $project);
4099 my @tagslist = git_get_tags_list();
4100 if (@tagslist) {
4101 git_tags_body(\@tagslist);
4103 git_footer_html();
4106 sub git_heads {
4107 my $head = git_get_head_hash($project);
4108 git_header_html();
4109 git_print_page_nav('','', $head,undef,$head);
4110 git_print_header_div('summary', $project);
4112 my @headslist = git_get_heads_list();
4113 if (@headslist) {
4114 git_heads_body(\@headslist, $head);
4116 git_footer_html();
4119 sub git_blob_plain {
4120 my $expires;
4122 if (!defined $hash) {
4123 if (defined $file_name) {
4124 my $base = $hash_base || git_get_head_hash($project);
4125 $hash = git_get_hash_by_path($base, $file_name, "blob")
4126 or die_error(undef, "Error lookup file");
4127 } else {
4128 die_error(undef, "No file name defined");
4130 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4131 # blobs defined by non-textual hash id's can be cached
4132 $expires = "+1d";
4135 my $type = shift;
4136 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4137 or die_error(undef, "Couldn't cat $file_name, $hash");
4139 $type ||= blob_mimetype($fd, $file_name);
4141 # save as filename, even when no $file_name is given
4142 my $save_as = "$hash";
4143 if (defined $file_name) {
4144 $save_as = $file_name;
4145 } elsif ($type =~ m/^text\//) {
4146 $save_as .= '.txt';
4149 print $cgi->header(
4150 -type => "$type",
4151 -expires=>$expires,
4152 -content_disposition => 'inline; filename="' . "$save_as" . '"');
4153 undef $/;
4154 binmode STDOUT, ':raw';
4155 print <$fd>;
4156 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4157 $/ = "\n";
4158 close $fd;
4161 sub git_blob {
4162 my $expires;
4164 if (!defined $hash) {
4165 if (defined $file_name) {
4166 my $base = $hash_base || git_get_head_hash($project);
4167 $hash = git_get_hash_by_path($base, $file_name, "blob")
4168 or die_error(undef, "Error lookup file");
4169 } else {
4170 die_error(undef, "No file name defined");
4172 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4173 # blobs defined by non-textual hash id's can be cached
4174 $expires = "+1d";
4177 my ($have_blame) = gitweb_check_feature('blame');
4178 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4179 or die_error(undef, "Couldn't cat $file_name, $hash");
4180 my $mimetype = blob_mimetype($fd, $file_name);
4181 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
4182 close $fd;
4183 return git_blob_plain($mimetype);
4185 # we can have blame only for text/* mimetype
4186 $have_blame &&= ($mimetype =~ m!^text/!);
4188 git_header_html(undef, $expires);
4189 my $formats_nav = '';
4190 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4191 if (defined $file_name) {
4192 if ($have_blame) {
4193 $formats_nav .=
4194 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
4195 hash=>$hash, file_name=>$file_name)},
4196 "blame") .
4197 " | ";
4199 $formats_nav .=
4200 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4201 hash=>$hash, file_name=>$file_name)},
4202 "history") .
4203 " | " .
4204 $cgi->a({-href => href(action=>"blob_plain",
4205 hash=>$hash, file_name=>$file_name)},
4206 "raw") .
4207 " | " .
4208 $cgi->a({-href => href(action=>"blob",
4209 hash_base=>"HEAD", file_name=>$file_name)},
4210 "HEAD");
4211 } else {
4212 $formats_nav .=
4213 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
4215 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4216 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4217 } else {
4218 print "<div class=\"page_nav\">\n" .
4219 "<br/><br/></div>\n" .
4220 "<div class=\"title\">$hash</div>\n";
4222 git_print_page_path($file_name, "blob", $hash_base);
4223 print "<div class=\"page_body\">\n";
4224 if ($mimetype =~ m!^text/!) {
4225 my $nr;
4226 while (my $line = <$fd>) {
4227 chomp $line;
4228 $nr++;
4229 $line = untabify($line);
4230 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4231 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4233 } elsif ($mimetype =~ m!^image/!) {
4234 print qq!<img type="$mimetype"!;
4235 if ($file_name) {
4236 print qq! alt="$file_name" title="$file_name"!;
4238 print qq! src="! .
4239 href(action=>"blob_plain", hash=>$hash,
4240 hash_base=>$hash_base, file_name=>$file_name) .
4241 qq!" />\n!;
4243 close $fd
4244 or print "Reading blob failed.\n";
4245 print "</div>";
4246 git_footer_html();
4249 sub git_tree {
4250 if (!defined $hash_base) {
4251 $hash_base = "HEAD";
4253 if (!defined $hash) {
4254 if (defined $file_name) {
4255 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4256 } else {
4257 $hash = $hash_base;
4260 $/ = "\0";
4261 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4262 or die_error(undef, "Open git-ls-tree failed");
4263 my @entries = map { chomp; $_ } <$fd>;
4264 close $fd or die_error(undef, "Reading tree failed");
4265 $/ = "\n";
4267 my $refs = git_get_references();
4268 my $ref = format_ref_marker($refs, $hash_base);
4269 git_header_html();
4270 my $basedir = '';
4271 my ($have_blame) = gitweb_check_feature('blame');
4272 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4273 my @views_nav = ();
4274 if (defined $file_name) {
4275 push @views_nav,
4276 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4277 hash=>$hash, file_name=>$file_name)},
4278 "history"),
4279 $cgi->a({-href => href(action=>"tree",
4280 hash_base=>"HEAD", file_name=>$file_name)},
4281 "HEAD"),
4283 my $snapshot_links = format_snapshot_links($hash);
4284 if (defined $snapshot_links) {
4285 # FIXME: Should be available when we have no hash base as well.
4286 push @views_nav, $snapshot_links;
4288 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4289 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4290 } else {
4291 undef $hash_base;
4292 print "<div class=\"page_nav\">\n";
4293 print "<br/><br/></div>\n";
4294 print "<div class=\"title\">$hash</div>\n";
4296 if (defined $file_name) {
4297 $basedir = $file_name;
4298 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4299 $basedir .= '/';
4302 git_print_page_path($file_name, 'tree', $hash_base);
4303 print "<div class=\"page_body\">\n";
4304 print "<table cellspacing=\"0\">\n";
4305 my $alternate = 1;
4306 # '..' (top directory) link if possible
4307 if (defined $hash_base &&
4308 defined $file_name && $file_name =~ m![^/]+$!) {
4309 if ($alternate) {
4310 print "<tr class=\"dark\">\n";
4311 } else {
4312 print "<tr class=\"light\">\n";
4314 $alternate ^= 1;
4316 my $up = $file_name;
4317 $up =~ s!/?[^/]+$!!;
4318 undef $up unless $up;
4319 # based on git_print_tree_entry
4320 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4321 print '<td class="list">';
4322 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4323 file_name=>$up)},
4324 "..");
4325 print "</td>\n";
4326 print "<td class=\"link\"></td>\n";
4328 print "</tr>\n";
4330 foreach my $line (@entries) {
4331 my %t = parse_ls_tree_line($line, -z => 1);
4333 if ($alternate) {
4334 print "<tr class=\"dark\">\n";
4335 } else {
4336 print "<tr class=\"light\">\n";
4338 $alternate ^= 1;
4340 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4342 print "</tr>\n";
4344 print "</table>\n" .
4345 "</div>";
4346 git_footer_html();
4349 sub git_snapshot {
4350 my @supported_fmts = gitweb_check_feature('snapshot');
4351 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4353 my $format = $cgi->param('sf');
4354 if (!@supported_fmts) {
4355 die_error('403 Permission denied', "Permission denied");
4357 # default to first supported snapshot format
4358 $format ||= $supported_fmts[0];
4359 if ($format !~ m/^[a-z0-9]+$/) {
4360 die_error(undef, "Invalid snapshot format parameter");
4361 } elsif (!exists($known_snapshot_formats{$format})) {
4362 die_error(undef, "Unknown snapshot format");
4363 } elsif (!grep($_ eq $format, @supported_fmts)) {
4364 die_error(undef, "Unsupported snapshot format");
4367 if (!defined $hash) {
4368 $hash = git_get_head_hash($project);
4371 my $git_command = git_cmd_str();
4372 my $name = $project;
4373 $name =~ s,([^/])/*\.git$,$1,;
4374 $name = basename($name);
4375 my $filename = to_utf8($name);
4376 $name =~ s/\047/\047\\\047\047/g;
4377 my $cmd;
4378 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4379 $cmd = "$git_command archive " .
4380 "--format=$known_snapshot_formats{$format}{'format'} " .
4381 "--prefix=\'$name\'/ $hash";
4382 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4383 $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
4386 print $cgi->header(
4387 -type => $known_snapshot_formats{$format}{'type'},
4388 -content_disposition => 'inline; filename="' . "$filename" . '"',
4389 -status => '200 OK');
4391 open my $fd, "-|", $cmd
4392 or die_error(undef, "Execute git-archive failed");
4393 binmode STDOUT, ':raw';
4394 print <$fd>;
4395 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4396 close $fd;
4399 sub git_log {
4400 my $head = git_get_head_hash($project);
4401 if (!defined $hash) {
4402 $hash = $head;
4404 if (!defined $page) {
4405 $page = 0;
4407 my $refs = git_get_references();
4409 my @commitlist = parse_commits($hash, 101, (100 * $page));
4411 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
4413 git_header_html();
4414 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4416 if (!@commitlist) {
4417 my %co = parse_commit($hash);
4419 git_print_header_div('summary', $project);
4420 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4422 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4423 for (my $i = 0; $i <= $to; $i++) {
4424 my %co = %{$commitlist[$i]};
4425 next if !%co;
4426 my $commit = $co{'id'};
4427 my $ref = format_ref_marker($refs, $commit);
4428 my %ad = parse_date($co{'author_epoch'});
4429 git_print_header_div('commit',
4430 "<span class=\"age\">$co{'age_string'}</span>" .
4431 esc_html($co{'title'}) . $ref,
4432 $commit);
4433 print "<div class=\"title_text\">\n" .
4434 "<div class=\"log_link\">\n" .
4435 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4436 " | " .
4437 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4438 " | " .
4439 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4440 "<br/>\n" .
4441 "</div>\n" .
4442 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4443 "</div>\n";
4445 print "<div class=\"log_body\">\n";
4446 git_print_log($co{'comment'}, -final_empty_line=> 1);
4447 print "</div>\n";
4449 if ($#commitlist >= 100) {
4450 print "<div class=\"page_nav\">\n";
4451 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4452 -accesskey => "n", -title => "Alt-n"}, "next");
4453 print "</div>\n";
4455 git_footer_html();
4458 sub git_commit {
4459 $hash ||= $hash_base || "HEAD";
4460 my %co = parse_commit($hash);
4461 if (!%co) {
4462 die_error(undef, "Unknown commit object");
4464 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4465 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4467 my $parent = $co{'parent'};
4468 my $parents = $co{'parents'}; # listref
4470 # we need to prepare $formats_nav before any parameter munging
4471 my $formats_nav;
4472 if (!defined $parent) {
4473 # --root commitdiff
4474 $formats_nav .= '(initial)';
4475 } elsif (@$parents == 1) {
4476 # single parent commit
4477 $formats_nav .=
4478 '(parent: ' .
4479 $cgi->a({-href => href(action=>"commit",
4480 hash=>$parent)},
4481 esc_html(substr($parent, 0, 7))) .
4482 ')';
4483 } else {
4484 # merge commit
4485 $formats_nav .=
4486 '(merge: ' .
4487 join(' ', map {
4488 $cgi->a({-href => href(action=>"commit",
4489 hash=>$_)},
4490 esc_html(substr($_, 0, 7)));
4491 } @$parents ) .
4492 ')';
4495 if (!defined $parent) {
4496 $parent = "--root";
4498 my @difftree;
4499 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4500 @diff_opts,
4501 (@$parents <= 1 ? $parent : '-c'),
4502 $hash, "--"
4503 or die_error(undef, "Open git-diff-tree failed");
4504 @difftree = map { chomp; $_ } <$fd>;
4505 close $fd or die_error(undef, "Reading git-diff-tree failed");
4507 # non-textual hash id's can be cached
4508 my $expires;
4509 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4510 $expires = "+1d";
4512 my $refs = git_get_references();
4513 my $ref = format_ref_marker($refs, $co{'id'});
4515 git_header_html(undef, $expires);
4516 git_print_page_nav('commit', '',
4517 $hash, $co{'tree'}, $hash,
4518 $formats_nav);
4520 if (defined $co{'parent'}) {
4521 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4522 } else {
4523 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4525 print "<div class=\"title_text\">\n" .
4526 "<table cellspacing=\"0\">\n";
4527 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4528 "<tr>" .
4529 "<td></td><td> $ad{'rfc2822'}";
4530 if ($ad{'hour_local'} < 6) {
4531 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4532 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4533 } else {
4534 printf(" (%02d:%02d %s)",
4535 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4537 print "</td>" .
4538 "</tr>\n";
4539 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4540 print "<tr><td></td><td> $cd{'rfc2822'}" .
4541 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4542 "</td></tr>\n";
4543 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4544 print "<tr>" .
4545 "<td>tree</td>" .
4546 "<td class=\"sha1\">" .
4547 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4548 class => "list"}, $co{'tree'}) .
4549 "</td>" .
4550 "<td class=\"link\">" .
4551 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4552 "tree");
4553 my $snapshot_links = format_snapshot_links($hash);
4554 if (defined $snapshot_links) {
4555 print " | " . $snapshot_links;
4557 print "</td>" .
4558 "</tr>\n";
4560 foreach my $par (@$parents) {
4561 print "<tr>" .
4562 "<td>parent</td>" .
4563 "<td class=\"sha1\">" .
4564 $cgi->a({-href => href(action=>"commit", hash=>$par),
4565 class => "list"}, $par) .
4566 "</td>" .
4567 "<td class=\"link\">" .
4568 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4569 " | " .
4570 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4571 "</td>" .
4572 "</tr>\n";
4574 print "</table>".
4575 "</div>\n";
4577 print "<div class=\"page_body\">\n";
4578 git_print_log($co{'comment'});
4579 print "</div>\n";
4581 git_difftree_body(\@difftree, $hash, @$parents);
4583 git_footer_html();
4586 sub git_object {
4587 # object is defined by:
4588 # - hash or hash_base alone
4589 # - hash_base and file_name
4590 my $type;
4592 # - hash or hash_base alone
4593 if ($hash || ($hash_base && !defined $file_name)) {
4594 my $object_id = $hash || $hash_base;
4596 my $git_command = git_cmd_str();
4597 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4598 or die_error('404 Not Found', "Object does not exist");
4599 $type = <$fd>;
4600 chomp $type;
4601 close $fd
4602 or die_error('404 Not Found', "Object does not exist");
4604 # - hash_base and file_name
4605 } elsif ($hash_base && defined $file_name) {
4606 $file_name =~ s,/+$,,;
4608 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4609 or die_error('404 Not Found', "Base object does not exist");
4611 # here errors should not hapen
4612 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4613 or die_error(undef, "Open git-ls-tree failed");
4614 my $line = <$fd>;
4615 close $fd;
4617 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4618 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4619 die_error('404 Not Found', "File or directory for given base does not exist");
4621 $type = $2;
4622 $hash = $3;
4623 } else {
4624 die_error('404 Not Found', "Not enough information to find object");
4627 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4628 hash=>$hash, hash_base=>$hash_base,
4629 file_name=>$file_name),
4630 -status => '302 Found');
4633 sub git_blobdiff {
4634 my $format = shift || 'html';
4636 my $fd;
4637 my @difftree;
4638 my %diffinfo;
4639 my $expires;
4641 # preparing $fd and %diffinfo for git_patchset_body
4642 # new style URI
4643 if (defined $hash_base && defined $hash_parent_base) {
4644 if (defined $file_name) {
4645 # read raw output
4646 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4647 $hash_parent_base, $hash_base,
4648 "--", (defined $file_parent ? $file_parent : ()), $file_name
4649 or die_error(undef, "Open git-diff-tree failed");
4650 @difftree = map { chomp; $_ } <$fd>;
4651 close $fd
4652 or die_error(undef, "Reading git-diff-tree failed");
4653 @difftree
4654 or die_error('404 Not Found', "Blob diff not found");
4656 } elsif (defined $hash &&
4657 $hash =~ /[0-9a-fA-F]{40}/) {
4658 # try to find filename from $hash
4660 # read filtered raw output
4661 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4662 $hash_parent_base, $hash_base, "--"
4663 or die_error(undef, "Open git-diff-tree failed");
4664 @difftree =
4665 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4666 # $hash == to_id
4667 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4668 map { chomp; $_ } <$fd>;
4669 close $fd
4670 or die_error(undef, "Reading git-diff-tree failed");
4671 @difftree
4672 or die_error('404 Not Found', "Blob diff not found");
4674 } else {
4675 die_error('404 Not Found', "Missing one of the blob diff parameters");
4678 if (@difftree > 1) {
4679 die_error('404 Not Found', "Ambiguous blob diff specification");
4682 %diffinfo = parse_difftree_raw_line($difftree[0]);
4683 $file_parent ||= $diffinfo{'from_file'} || $file_name;
4684 $file_name ||= $diffinfo{'to_file'};
4686 $hash_parent ||= $diffinfo{'from_id'};
4687 $hash ||= $diffinfo{'to_id'};
4689 # non-textual hash id's can be cached
4690 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4691 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4692 $expires = '+1d';
4695 # open patch output
4696 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4697 '-p', ($format eq 'html' ? "--full-index" : ()),
4698 $hash_parent_base, $hash_base,
4699 "--", (defined $file_parent ? $file_parent : ()), $file_name
4700 or die_error(undef, "Open git-diff-tree failed");
4703 # old/legacy style URI
4704 if (!%diffinfo && # if new style URI failed
4705 defined $hash && defined $hash_parent) {
4706 # fake git-diff-tree raw output
4707 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4708 $diffinfo{'from_id'} = $hash_parent;
4709 $diffinfo{'to_id'} = $hash;
4710 if (defined $file_name) {
4711 if (defined $file_parent) {
4712 $diffinfo{'status'} = '2';
4713 $diffinfo{'from_file'} = $file_parent;
4714 $diffinfo{'to_file'} = $file_name;
4715 } else { # assume not renamed
4716 $diffinfo{'status'} = '1';
4717 $diffinfo{'from_file'} = $file_name;
4718 $diffinfo{'to_file'} = $file_name;
4720 } else { # no filename given
4721 $diffinfo{'status'} = '2';
4722 $diffinfo{'from_file'} = $hash_parent;
4723 $diffinfo{'to_file'} = $hash;
4726 # non-textual hash id's can be cached
4727 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4728 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4729 $expires = '+1d';
4732 # open patch output
4733 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4734 '-p', ($format eq 'html' ? "--full-index" : ()),
4735 $hash_parent, $hash, "--"
4736 or die_error(undef, "Open git-diff failed");
4737 } else {
4738 die_error('404 Not Found', "Missing one of the blob diff parameters")
4739 unless %diffinfo;
4742 # header
4743 if ($format eq 'html') {
4744 my $formats_nav =
4745 $cgi->a({-href => href(action=>"blobdiff_plain",
4746 hash=>$hash, hash_parent=>$hash_parent,
4747 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4748 file_name=>$file_name, file_parent=>$file_parent)},
4749 "raw");
4750 git_header_html(undef, $expires);
4751 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4752 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4753 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4754 } else {
4755 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4756 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4758 if (defined $file_name) {
4759 git_print_page_path($file_name, "blob", $hash_base);
4760 } else {
4761 print "<div class=\"page_path\"></div>\n";
4764 } elsif ($format eq 'plain') {
4765 print $cgi->header(
4766 -type => 'text/plain',
4767 -charset => 'utf-8',
4768 -expires => $expires,
4769 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4771 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4773 } else {
4774 die_error(undef, "Unknown blobdiff format");
4777 # patch
4778 if ($format eq 'html') {
4779 print "<div class=\"page_body\">\n";
4781 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4782 close $fd;
4784 print "</div>\n"; # class="page_body"
4785 git_footer_html();
4787 } else {
4788 while (my $line = <$fd>) {
4789 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4790 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4792 print $line;
4794 last if $line =~ m!^\+\+\+!;
4796 local $/ = undef;
4797 print <$fd>;
4798 close $fd;
4802 sub git_blobdiff_plain {
4803 git_blobdiff('plain');
4806 sub git_commitdiff {
4807 my $format = shift || 'html';
4808 $hash ||= $hash_base || "HEAD";
4809 my %co = parse_commit($hash);
4810 if (!%co) {
4811 die_error(undef, "Unknown commit object");
4814 # choose format for commitdiff for merge
4815 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4816 $hash_parent = '--cc';
4818 # we need to prepare $formats_nav before almost any parameter munging
4819 my $formats_nav;
4820 if ($format eq 'html') {
4821 $formats_nav =
4822 $cgi->a({-href => href(action=>"commitdiff_plain",
4823 hash=>$hash, hash_parent=>$hash_parent)},
4824 "raw");
4826 if (defined $hash_parent &&
4827 $hash_parent ne '-c' && $hash_parent ne '--cc') {
4828 # commitdiff with two commits given
4829 my $hash_parent_short = $hash_parent;
4830 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4831 $hash_parent_short = substr($hash_parent, 0, 7);
4833 $formats_nav .=
4834 ' (from';
4835 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4836 if ($co{'parents'}[$i] eq $hash_parent) {
4837 $formats_nav .= ' parent ' . ($i+1);
4838 last;
4841 $formats_nav .= ': ' .
4842 $cgi->a({-href => href(action=>"commitdiff",
4843 hash=>$hash_parent)},
4844 esc_html($hash_parent_short)) .
4845 ')';
4846 } elsif (!$co{'parent'}) {
4847 # --root commitdiff
4848 $formats_nav .= ' (initial)';
4849 } elsif (scalar @{$co{'parents'}} == 1) {
4850 # single parent commit
4851 $formats_nav .=
4852 ' (parent: ' .
4853 $cgi->a({-href => href(action=>"commitdiff",
4854 hash=>$co{'parent'})},
4855 esc_html(substr($co{'parent'}, 0, 7))) .
4856 ')';
4857 } else {
4858 # merge commit
4859 if ($hash_parent eq '--cc') {
4860 $formats_nav .= ' | ' .
4861 $cgi->a({-href => href(action=>"commitdiff",
4862 hash=>$hash, hash_parent=>'-c')},
4863 'combined');
4864 } else { # $hash_parent eq '-c'
4865 $formats_nav .= ' | ' .
4866 $cgi->a({-href => href(action=>"commitdiff",
4867 hash=>$hash, hash_parent=>'--cc')},
4868 'compact');
4870 $formats_nav .=
4871 ' (merge: ' .
4872 join(' ', map {
4873 $cgi->a({-href => href(action=>"commitdiff",
4874 hash=>$_)},
4875 esc_html(substr($_, 0, 7)));
4876 } @{$co{'parents'}} ) .
4877 ')';
4881 my $hash_parent_param = $hash_parent;
4882 if (!defined $hash_parent_param) {
4883 # --cc for multiple parents, --root for parentless
4884 $hash_parent_param =
4885 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
4888 # read commitdiff
4889 my $fd;
4890 my @difftree;
4891 if ($format eq 'html') {
4892 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4893 "--no-commit-id", "--patch-with-raw", "--full-index",
4894 $hash_parent_param, $hash, "--"
4895 or die_error(undef, "Open git-diff-tree failed");
4897 while (my $line = <$fd>) {
4898 chomp $line;
4899 # empty line ends raw part of diff-tree output
4900 last unless $line;
4901 push @difftree, scalar parse_difftree_raw_line($line);
4904 } elsif ($format eq 'plain') {
4905 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4906 '-p', $hash_parent_param, $hash, "--"
4907 or die_error(undef, "Open git-diff-tree failed");
4909 } else {
4910 die_error(undef, "Unknown commitdiff format");
4913 # non-textual hash id's can be cached
4914 my $expires;
4915 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4916 $expires = "+1d";
4919 # write commit message
4920 if ($format eq 'html') {
4921 my $refs = git_get_references();
4922 my $ref = format_ref_marker($refs, $co{'id'});
4924 git_header_html(undef, $expires);
4925 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4926 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4927 git_print_authorship(\%co);
4928 print "<div class=\"page_body\">\n";
4929 if (@{$co{'comment'}} > 1) {
4930 print "<div class=\"log\">\n";
4931 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4932 print "</div>\n"; # class="log"
4935 } elsif ($format eq 'plain') {
4936 my $refs = git_get_references("tags");
4937 my $tagname = git_get_rev_name_tags($hash);
4938 my $filename = basename($project) . "-$hash.patch";
4940 print $cgi->header(
4941 -type => 'text/plain',
4942 -charset => 'utf-8',
4943 -expires => $expires,
4944 -content_disposition => 'inline; filename="' . "$filename" . '"');
4945 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4946 print <<TEXT;
4947 From: $co{'author'}
4948 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4949 Subject: $co{'title'}
4950 TEXT
4951 print "X-Git-Tag: $tagname\n" if $tagname;
4952 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4954 foreach my $line (@{$co{'comment'}}) {
4955 print "$line\n";
4957 print "---\n\n";
4960 # write patch
4961 if ($format eq 'html') {
4962 my $use_parents = !defined $hash_parent ||
4963 $hash_parent eq '-c' || $hash_parent eq '--cc';
4964 git_difftree_body(\@difftree, $hash,
4965 $use_parents ? @{$co{'parents'}} : $hash_parent);
4966 print "<br/>\n";
4968 git_patchset_body($fd, \@difftree, $hash,
4969 $use_parents ? @{$co{'parents'}} : $hash_parent);
4970 close $fd;
4971 print "</div>\n"; # class="page_body"
4972 git_footer_html();
4974 } elsif ($format eq 'plain') {
4975 local $/ = undef;
4976 print <$fd>;
4977 close $fd
4978 or print "Reading git-diff-tree failed\n";
4982 sub git_commitdiff_plain {
4983 git_commitdiff('plain');
4986 sub git_history {
4987 if (!defined $hash_base) {
4988 $hash_base = git_get_head_hash($project);
4990 if (!defined $page) {
4991 $page = 0;
4993 my $ftype;
4994 my %co = parse_commit($hash_base);
4995 if (!%co) {
4996 die_error(undef, "Unknown commit object");
4999 my $refs = git_get_references();
5000 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5002 if (!defined $hash && defined $file_name) {
5003 $hash = git_get_hash_by_path($hash_base, $file_name);
5005 if (defined $hash) {
5006 $ftype = git_get_type($hash);
5009 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
5011 my $paging_nav = '';
5012 if ($page > 0) {
5013 $paging_nav .=
5014 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5015 file_name=>$file_name)},
5016 "first");
5017 $paging_nav .= " &sdot; " .
5018 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5019 file_name=>$file_name, page=>$page-1),
5020 -accesskey => "p", -title => "Alt-p"}, "prev");
5021 } else {
5022 $paging_nav .= "first";
5023 $paging_nav .= " &sdot; prev";
5025 if ($#commitlist >= 100) {
5026 $paging_nav .= " &sdot; " .
5027 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5028 file_name=>$file_name, page=>$page+1),
5029 -accesskey => "n", -title => "Alt-n"}, "next");
5030 } else {
5031 $paging_nav .= " &sdot; next";
5033 my $next_link = '';
5034 if ($#commitlist >= 100) {
5035 $next_link =
5036 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5037 file_name=>$file_name, page=>$page+1),
5038 -accesskey => "n", -title => "Alt-n"}, "next");
5041 git_header_html();
5042 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5043 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5044 git_print_page_path($file_name, $ftype, $hash_base);
5046 git_history_body(\@commitlist, 0, 99,
5047 $refs, $hash_base, $ftype, $next_link);
5049 git_footer_html();
5052 sub git_search {
5053 my ($have_search) = gitweb_check_feature('search');
5054 if (!$have_search) {
5055 die_error('403 Permission denied', "Permission denied");
5057 if (!defined $searchtext) {
5058 die_error(undef, "Text field empty");
5060 if (!defined $hash) {
5061 $hash = git_get_head_hash($project);
5063 my %co = parse_commit($hash);
5064 if (!%co) {
5065 die_error(undef, "Unknown commit object");
5067 if (!defined $page) {
5068 $page = 0;
5071 $searchtype ||= 'commit';
5072 if ($searchtype eq 'pickaxe') {
5073 # pickaxe may take all resources of your box and run for several minutes
5074 # with every query - so decide by yourself how public you make this feature
5075 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5076 if (!$have_pickaxe) {
5077 die_error('403 Permission denied', "Permission denied");
5080 if ($searchtype eq 'grep') {
5081 my ($have_grep) = gitweb_check_feature('grep');
5082 if (!$have_grep) {
5083 die_error('403 Permission denied', "Permission denied");
5087 git_header_html();
5089 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5090 my $greptype;
5091 if ($searchtype eq 'commit') {
5092 $greptype = "--grep=";
5093 } elsif ($searchtype eq 'author') {
5094 $greptype = "--author=";
5095 } elsif ($searchtype eq 'committer') {
5096 $greptype = "--committer=";
5098 $greptype .= $search_regexp;
5099 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
5101 my $paging_nav = '';
5102 if ($page > 0) {
5103 $paging_nav .=
5104 $cgi->a({-href => href(action=>"search", hash=>$hash,
5105 searchtext=>$searchtext, searchtype=>$searchtype)},
5106 "first");
5107 $paging_nav .= " &sdot; " .
5108 $cgi->a({-href => href(action=>"search", hash=>$hash,
5109 searchtext=>$searchtext, searchtype=>$searchtype,
5110 page=>$page-1),
5111 -accesskey => "p", -title => "Alt-p"}, "prev");
5112 } else {
5113 $paging_nav .= "first";
5114 $paging_nav .= " &sdot; prev";
5116 if ($#commitlist >= 100) {
5117 $paging_nav .= " &sdot; " .
5118 $cgi->a({-href => href(action=>"search", hash=>$hash,
5119 searchtext=>$searchtext, searchtype=>$searchtype,
5120 page=>$page+1),
5121 -accesskey => "n", -title => "Alt-n"}, "next");
5122 } else {
5123 $paging_nav .= " &sdot; next";
5125 my $next_link = '';
5126 if ($#commitlist >= 100) {
5127 $next_link =
5128 $cgi->a({-href => href(action=>"search", hash=>$hash,
5129 searchtext=>$searchtext, searchtype=>$searchtype,
5130 page=>$page+1),
5131 -accesskey => "n", -title => "Alt-n"}, "next");
5134 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5135 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5136 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5139 if ($searchtype eq 'pickaxe') {
5140 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5141 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5143 print "<table cellspacing=\"0\">\n";
5144 my $alternate = 1;
5145 $/ = "\n";
5146 my $git_command = git_cmd_str();
5147 my $searchqtext = $searchtext;
5148 $searchqtext =~ s/'/'\\''/;
5149 open my $fd, "-|", "$git_command rev-list $hash | " .
5150 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
5151 undef %co;
5152 my @files;
5153 while (my $line = <$fd>) {
5154 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
5155 my %set;
5156 $set{'file'} = $6;
5157 $set{'from_id'} = $3;
5158 $set{'to_id'} = $4;
5159 $set{'id'} = $set{'to_id'};
5160 if ($set{'id'} =~ m/0{40}/) {
5161 $set{'id'} = $set{'from_id'};
5163 if ($set{'id'} =~ m/0{40}/) {
5164 next;
5166 push @files, \%set;
5167 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
5168 if (%co) {
5169 if ($alternate) {
5170 print "<tr class=\"dark\">\n";
5171 } else {
5172 print "<tr class=\"light\">\n";
5174 $alternate ^= 1;
5175 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5176 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5177 "<td><i>" . $author . "</i></td>\n" .
5178 "<td>" .
5179 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5180 -class => "list subject"},
5181 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5182 while (my $setref = shift @files) {
5183 my %set = %$setref;
5184 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5185 hash=>$set{'id'}, file_name=>$set{'file'}),
5186 -class => "list"},
5187 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5188 "<br/>\n";
5190 print "</td>\n" .
5191 "<td class=\"link\">" .
5192 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5193 " | " .
5194 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5195 print "</td>\n" .
5196 "</tr>\n";
5198 %co = parse_commit($1);
5201 close $fd;
5203 print "</table>\n";
5206 if ($searchtype eq 'grep') {
5207 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5208 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5210 print "<table cellspacing=\"0\">\n";
5211 my $alternate = 1;
5212 my $matches = 0;
5213 $/ = "\n";
5214 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
5215 my $lastfile = '';
5216 while (my $line = <$fd>) {
5217 chomp $line;
5218 my ($file, $lno, $ltext, $binary);
5219 last if ($matches++ > 1000);
5220 if ($line =~ /^Binary file (.+) matches$/) {
5221 $file = $1;
5222 $binary = 1;
5223 } else {
5224 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5226 if ($file ne $lastfile) {
5227 $lastfile and print "</td></tr>\n";
5228 if ($alternate++) {
5229 print "<tr class=\"dark\">\n";
5230 } else {
5231 print "<tr class=\"light\">\n";
5233 print "<td class=\"list\">".
5234 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5235 file_name=>"$file"),
5236 -class => "list"}, esc_path($file));
5237 print "</td><td>\n";
5238 $lastfile = $file;
5240 if ($binary) {
5241 print "<div class=\"binary\">Binary file</div>\n";
5242 } else {
5243 $ltext = untabify($ltext);
5244 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
5245 $ltext = esc_html($1, -nbsp=>1);
5246 $ltext .= '<span class="match">';
5247 $ltext .= esc_html($2, -nbsp=>1);
5248 $ltext .= '</span>';
5249 $ltext .= esc_html($3, -nbsp=>1);
5250 } else {
5251 $ltext = esc_html($ltext, -nbsp=>1);
5253 print "<div class=\"pre\">" .
5254 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5255 file_name=>"$file").'#l'.$lno,
5256 -class => "linenr"}, sprintf('%4i', $lno))
5257 . ' ' . $ltext . "</div>\n";
5260 if ($lastfile) {
5261 print "</td></tr>\n";
5262 if ($matches > 1000) {
5263 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5265 } else {
5266 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5268 close $fd;
5270 print "</table>\n";
5272 git_footer_html();
5275 sub git_search_help {
5276 git_header_html();
5277 git_print_page_nav('','', $hash,$hash,$hash);
5278 print <<EOT;
5279 <dl>
5280 <dt><b>commit</b></dt>
5281 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
5283 my ($have_grep) = gitweb_check_feature('grep');
5284 if ($have_grep) {
5285 print <<EOT;
5286 <dt><b>grep</b></dt>
5287 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5288 a different one) are searched for the given
5289 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5290 (POSIX extended) and the matches are listed. On large
5291 trees, this search can take a while and put some strain on the server, so please use it with
5292 some consideration.</dd>
5295 print <<EOT;
5296 <dt><b>author</b></dt>
5297 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5298 <dt><b>committer</b></dt>
5299 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5301 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5302 if ($have_pickaxe) {
5303 print <<EOT;
5304 <dt><b>pickaxe</b></dt>
5305 <dd>All commits that caused the string to appear or disappear from any file (changes that
5306 added, removed or "modified" the string) will be listed. This search can take a while and
5307 takes a lot of strain on the server, so please use it wisely.</dd>
5310 print "</dl>\n";
5311 git_footer_html();
5314 sub git_shortlog {
5315 my $head = git_get_head_hash($project);
5316 if (!defined $hash) {
5317 $hash = $head;
5319 if (!defined $page) {
5320 $page = 0;
5322 my $refs = git_get_references();
5324 my @commitlist = parse_commits($hash, 101, (100 * $page));
5326 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
5327 my $next_link = '';
5328 if ($#commitlist >= 100) {
5329 $next_link =
5330 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
5331 -accesskey => "n", -title => "Alt-n"}, "next");
5334 git_header_html();
5335 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5336 git_print_header_div('summary', $project);
5338 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5340 git_footer_html();
5343 ## ......................................................................
5344 ## feeds (RSS, Atom; OPML)
5346 sub git_feed {
5347 my $format = shift || 'atom';
5348 my ($have_blame) = gitweb_check_feature('blame');
5350 # Atom: http://www.atomenabled.org/developers/syndication/
5351 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5352 if ($format ne 'rss' && $format ne 'atom') {
5353 die_error(undef, "Unknown web feed format");
5356 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5357 my $head = $hash || 'HEAD';
5358 my @commitlist = parse_commits($head, 150, 0, undef, $file_name);
5360 my %latest_commit;
5361 my %latest_date;
5362 my $content_type = "application/$format+xml";
5363 if (defined $cgi->http('HTTP_ACCEPT') &&
5364 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5365 # browser (feed reader) prefers text/xml
5366 $content_type = 'text/xml';
5368 if (defined($commitlist[0])) {
5369 %latest_commit = %{$commitlist[0]};
5370 %latest_date = parse_date($latest_commit{'author_epoch'});
5371 print $cgi->header(
5372 -type => $content_type,
5373 -charset => 'utf-8',
5374 -last_modified => $latest_date{'rfc2822'});
5375 } else {
5376 print $cgi->header(
5377 -type => $content_type,
5378 -charset => 'utf-8');
5381 # Optimization: skip generating the body if client asks only
5382 # for Last-Modified date.
5383 return if ($cgi->request_method() eq 'HEAD');
5385 # header variables
5386 my $title = "$site_name - $project/$action";
5387 my $feed_type = 'log';
5388 if (defined $hash) {
5389 $title .= " - '$hash'";
5390 $feed_type = 'branch log';
5391 if (defined $file_name) {
5392 $title .= " :: $file_name";
5393 $feed_type = 'history';
5395 } elsif (defined $file_name) {
5396 $title .= " - $file_name";
5397 $feed_type = 'history';
5399 $title .= " $feed_type";
5400 my $descr = git_get_project_description($project);
5401 if (defined $descr) {
5402 $descr = esc_html($descr);
5403 } else {
5404 $descr = "$project " .
5405 ($format eq 'rss' ? 'RSS' : 'Atom') .
5406 " feed";
5408 my $owner = git_get_project_owner($project);
5409 $owner = esc_html($owner);
5411 #header
5412 my $alt_url;
5413 if (defined $file_name) {
5414 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5415 } elsif (defined $hash) {
5416 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5417 } else {
5418 $alt_url = href(-full=>1, action=>"summary");
5420 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5421 if ($format eq 'rss') {
5422 print <<XML;
5423 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5424 <channel>
5426 print "<title>$title</title>\n" .
5427 "<link>$alt_url</link>\n" .
5428 "<description>$descr</description>\n" .
5429 "<language>en</language>\n";
5430 } elsif ($format eq 'atom') {
5431 print <<XML;
5432 <feed xmlns="http://www.w3.org/2005/Atom">
5434 print "<title>$title</title>\n" .
5435 "<subtitle>$descr</subtitle>\n" .
5436 '<link rel="alternate" type="text/html" href="' .
5437 $alt_url . '" />' . "\n" .
5438 '<link rel="self" type="' . $content_type . '" href="' .
5439 $cgi->self_url() . '" />' . "\n" .
5440 "<id>" . href(-full=>1) . "</id>\n" .
5441 # use project owner for feed author
5442 "<author><name>$owner</name></author>\n";
5443 if (defined $favicon) {
5444 print "<icon>" . esc_url($favicon) . "</icon>\n";
5446 if (defined $logo_url) {
5447 # not twice as wide as tall: 72 x 27 pixels
5448 print "<logo>" . esc_url($logo) . "</logo>\n";
5450 if (! %latest_date) {
5451 # dummy date to keep the feed valid until commits trickle in:
5452 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5453 } else {
5454 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5458 # contents
5459 for (my $i = 0; $i <= $#commitlist; $i++) {
5460 my %co = %{$commitlist[$i]};
5461 my $commit = $co{'id'};
5462 # we read 150, we always show 30 and the ones more recent than 48 hours
5463 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5464 last;
5466 my %cd = parse_date($co{'author_epoch'});
5468 # get list of changed files
5469 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5470 $co{'parent'} || "--root",
5471 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5472 or next;
5473 my @difftree = map { chomp; $_ } <$fd>;
5474 close $fd
5475 or next;
5477 # print element (entry, item)
5478 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5479 if ($format eq 'rss') {
5480 print "<item>\n" .
5481 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5482 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5483 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5484 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5485 "<link>$co_url</link>\n" .
5486 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5487 "<content:encoded>" .
5488 "<![CDATA[\n";
5489 } elsif ($format eq 'atom') {
5490 print "<entry>\n" .
5491 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5492 "<updated>$cd{'iso-8601'}</updated>\n" .
5493 "<author>\n" .
5494 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5495 if ($co{'author_email'}) {
5496 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5498 print "</author>\n" .
5499 # use committer for contributor
5500 "<contributor>\n" .
5501 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5502 if ($co{'committer_email'}) {
5503 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5505 print "</contributor>\n" .
5506 "<published>$cd{'iso-8601'}</published>\n" .
5507 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5508 "<id>$co_url</id>\n" .
5509 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5510 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5512 my $comment = $co{'comment'};
5513 print "<pre>\n";
5514 foreach my $line (@$comment) {
5515 $line = esc_html($line);
5516 print "$line\n";
5518 print "</pre><ul>\n";
5519 foreach my $difftree_line (@difftree) {
5520 my %difftree = parse_difftree_raw_line($difftree_line);
5521 next if !$difftree{'from_id'};
5523 my $file = $difftree{'file'} || $difftree{'to_file'};
5525 print "<li>" .
5526 "[" .
5527 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5528 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5529 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5530 file_name=>$file, file_parent=>$difftree{'from_file'}),
5531 -title => "diff"}, 'D');
5532 if ($have_blame) {
5533 print $cgi->a({-href => href(-full=>1, action=>"blame",
5534 file_name=>$file, hash_base=>$commit),
5535 -title => "blame"}, 'B');
5537 # if this is not a feed of a file history
5538 if (!defined $file_name || $file_name ne $file) {
5539 print $cgi->a({-href => href(-full=>1, action=>"history",
5540 file_name=>$file, hash=>$commit),
5541 -title => "history"}, 'H');
5543 $file = esc_path($file);
5544 print "] ".
5545 "$file</li>\n";
5547 if ($format eq 'rss') {
5548 print "</ul>]]>\n" .
5549 "</content:encoded>\n" .
5550 "</item>\n";
5551 } elsif ($format eq 'atom') {
5552 print "</ul>\n</div>\n" .
5553 "</content>\n" .
5554 "</entry>\n";
5558 # end of feed
5559 if ($format eq 'rss') {
5560 print "</channel>\n</rss>\n";
5561 } elsif ($format eq 'atom') {
5562 print "</feed>\n";
5566 sub git_rss {
5567 git_feed('rss');
5570 sub git_atom {
5571 git_feed('atom');
5574 sub git_opml {
5575 my @list = git_get_projects_list();
5577 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5578 print <<XML;
5579 <?xml version="1.0" encoding="utf-8"?>
5580 <opml version="1.0">
5581 <head>
5582 <title>$site_name OPML Export</title>
5583 </head>
5584 <body>
5585 <outline text="git RSS feeds">
5588 foreach my $pr (@list) {
5589 my %proj = %$pr;
5590 my $head = git_get_head_hash($proj{'path'});
5591 if (!defined $head) {
5592 next;
5594 $git_dir = "$projectroot/$proj{'path'}";
5595 my %co = parse_commit($head);
5596 if (!%co) {
5597 next;
5600 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5601 my $rss = "$my_url?p=$proj{'path'};a=rss";
5602 my $html = "$my_url?p=$proj{'path'};a=summary";
5603 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5605 print <<XML;
5606 </outline>
5607 </body>
5608 </opml>