gitweb: Document features better
[git/jnareb-git.git] / gitweb / gitweb.perl
blob1d5217e334ee7da3ec8ef82eeda5a0ffcde40e9e
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 our $cgi = new CGI;
22 our $version = "++GIT_VERSION++";
23 our $my_url = $cgi->url();
24 our $my_uri = $cgi->url(-absolute => 1);
26 # core git executable to use
27 # this can just be "git" if your webserver has a sensible PATH
28 our $GIT = "++GIT_BINDIR++/git";
30 # absolute fs-path which will be prepended to the project path
31 #our $projectroot = "/pub/scm";
32 our $projectroot = "++GITWEB_PROJECTROOT++";
34 # target of the home link on top of all pages
35 our $home_link = $my_uri || "/";
37 # string of the home link on top of all pages
38 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
40 # name of your site or organization to appear in page titles
41 # replace this with something more descriptive for clearer bookmarks
42 our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
44 # html text to include at home page
45 our $home_text = "++GITWEB_HOMETEXT++";
47 # URI of default stylesheet
48 our $stylesheet = "++GITWEB_CSS++";
49 # URI of GIT logo
50 our $logo = "++GITWEB_LOGO++";
51 # URI of GIT favicon, assumed to be image/png type
52 our $favicon = "++GITWEB_FAVICON++";
54 # source of projects list
55 our $projects_list = "++GITWEB_LIST++";
57 # show repository only if this file exists
58 # (only effective if this variable evaluates to true)
59 our $export_ok = "++GITWEB_EXPORT_OK++";
61 # only allow viewing of repositories also shown on the overview page
62 our $strict_export = "++GITWEB_STRICT_EXPORT++";
64 # list of git base URLs used for URL to where fetch project from,
65 # i.e. full URL is "$git_base_url/$project"
66 our @git_base_url_list = ("++GITWEB_BASE_URL++");
68 # default blob_plain mimetype and default charset for text/plain blob
69 our $default_blob_plain_mimetype = 'text/plain';
70 our $default_text_plain_charset = undef;
72 # file to use for guessing MIME types before trying /etc/mime.types
73 # (relative to the current git repository)
74 our $mimetypes_file = undef;
76 # You define site-wide feature defaults here; override them with
77 # $GITWEB_CONFIG as necessary.
78 our %feature = (
79 # feature => {
80 # 'sub' => feature-sub (subroutine),
81 # 'override' => allow-override (boolean),
82 # 'default' => [ default options...] (array reference)}
84 # if feature is overridable (it means that allow-override has true value,
85 # then feature-sub will be called with default options as parameters;
86 # return value of feature-sub indicates if to enable specified feature
88 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
90 # Enable the 'blame' blob view, showing the last commit that modified
91 # each line in the file. This can be very CPU-intensive.
93 # To enable system wide have in $GITWEB_CONFIG
94 # $feature{'blame'}{'default'} = [1];
95 # To have project specific config enable override in $GITWEB_CONFIG
96 # $feature{'blame'}{'override'} = 1;
97 # and in project config gitweb.blame = 0|1;
98 'blame' => {
99 'sub' => \&feature_blame,
100 'override' => 0,
101 'default' => [0]},
103 # Enable the 'snapshot' link, providing a compressed tarball of any
104 # tree. This can potentially generate high traffic if you have large
105 # project.
107 # To disable system wide have in $GITWEB_CONFIG
108 # $feature{'snapshot'}{'default'} = [undef];
109 # To have project specific config enable override in $GITWEB_CONFIG
110 # $feature{'blame'}{'override'} = 1;
111 # and in project config gitweb.snapshot = none|gzip|bzip2;
112 'snapshot' => {
113 'sub' => \&feature_snapshot,
114 'override' => 0,
115 # => [content-encoding, suffix, program]
116 'default' => ['x-gzip', 'gz', 'gzip']},
118 # Enable the pickaxe search, which will list the commits that modified
119 # a given string in a file. This can be practical and quite faster
120 # alternative to 'blame', but still potentially CPU-intensive.
122 # To enable system wide have in $GITWEB_CONFIG
123 # $feature{'pickaxe'}{'default'} = [1];
124 # To have project specific config enable override in $GITWEB_CONFIG
125 # $feature{'pickaxe'}{'override'} = 1;
126 # and in project config gitweb.pickaxe = 0|1;
127 'pickaxe' => {
128 'sub' => \&feature_pickaxe,
129 'override' => 0,
130 'default' => [1]},
132 # Make gitweb use an alternative format of the URLs which can be
133 # more readable and natural-looking: project name is embedded
134 # directly in the path and the query string contains other
135 # auxiliary information. All gitweb installations recognize
136 # URL in either format; this configures in which formats gitweb
137 # generates links.
139 # To enable system wide have in $GITWEB_CONFIG
140 # $feature{'pathinfo'}{'default'} = [1];
141 # Project specific override is not supported.
143 # Note that you will need to change the default location of CSS,
144 # favicon, logo and possibly other files to an absolute URL. Also,
145 # if gitweb.cgi serves as your indexfile, you will need to force
146 # $my_uri to contain the script name in your $GITWEB_CONFIG.
147 'pathinfo' => {
148 'override' => 0,
149 'default' => [0]},
152 sub gitweb_check_feature {
153 my ($name) = @_;
154 return unless exists $feature{$name};
155 my ($sub, $override, @defaults) = (
156 $feature{$name}{'sub'},
157 $feature{$name}{'override'},
158 @{$feature{$name}{'default'}});
159 if (!$override) { return @defaults; }
160 if (!defined $sub) {
161 warn "feature $name is not overrideable";
162 return @defaults;
164 return $sub->(@defaults);
167 sub feature_blame {
168 my ($val) = git_get_project_config('blame', '--bool');
170 if ($val eq 'true') {
171 return 1;
172 } elsif ($val eq 'false') {
173 return 0;
176 return $_[0];
179 sub feature_snapshot {
180 my ($ctype, $suffix, $command) = @_;
182 my ($val) = git_get_project_config('snapshot');
184 if ($val eq 'gzip') {
185 return ('x-gzip', 'gz', 'gzip');
186 } elsif ($val eq 'bzip2') {
187 return ('x-bzip2', 'bz2', 'bzip2');
188 } elsif ($val eq 'none') {
189 return ();
192 return ($ctype, $suffix, $command);
195 sub gitweb_have_snapshot {
196 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
197 my $have_snapshot = (defined $ctype && defined $suffix);
199 return $have_snapshot;
202 sub feature_pickaxe {
203 my ($val) = git_get_project_config('pickaxe', '--bool');
205 if ($val eq 'true') {
206 return (1);
207 } elsif ($val eq 'false') {
208 return (0);
211 return ($_[0]);
214 # rename detection options for git-diff and git-diff-tree
215 # - default is '-M', with the cost proportional to
216 # (number of removed files) * (number of new files).
217 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
218 # (number of changed files + number of removed files) * (number of new files)
219 # - even more costly is '-C', '--find-copies-harder' with cost
220 # (number of files in the original tree) * (number of new files)
221 # - one might want to include '-B' option, e.g. '-B', '-M'
222 our @diff_opts = ('-M'); # taken from git_commit
224 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
225 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
227 # version of the core git binary
228 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
230 $projects_list ||= $projectroot;
232 # ======================================================================
233 # input validation and dispatch
234 our $action = $cgi->param('a');
235 if (defined $action) {
236 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
237 die_error(undef, "Invalid action parameter");
241 # parameters which are pathnames
242 our $project = $cgi->param('p');
243 if (defined $project) {
244 if (!validate_pathname($project) ||
245 !(-d "$projectroot/$project") ||
246 !(-e "$projectroot/$project/HEAD") ||
247 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
248 ($strict_export && !project_in_list($project))) {
249 undef $project;
250 die_error(undef, "No such project");
254 our $file_name = $cgi->param('f');
255 if (defined $file_name) {
256 if (!validate_pathname($file_name)) {
257 die_error(undef, "Invalid file parameter");
261 our $file_parent = $cgi->param('fp');
262 if (defined $file_parent) {
263 if (!validate_pathname($file_parent)) {
264 die_error(undef, "Invalid file parent parameter");
268 # parameters which are refnames
269 our $hash = $cgi->param('h');
270 if (defined $hash) {
271 if (!validate_refname($hash)) {
272 die_error(undef, "Invalid hash parameter");
276 our $hash_parent = $cgi->param('hp');
277 if (defined $hash_parent) {
278 if (!validate_refname($hash_parent)) {
279 die_error(undef, "Invalid hash parent parameter");
283 our $hash_base = $cgi->param('hb');
284 if (defined $hash_base) {
285 if (!validate_refname($hash_base)) {
286 die_error(undef, "Invalid hash base parameter");
290 our $hash_parent_base = $cgi->param('hpb');
291 if (defined $hash_parent_base) {
292 if (!validate_refname($hash_parent_base)) {
293 die_error(undef, "Invalid hash parent base parameter");
297 # other parameters
298 our $page = $cgi->param('pg');
299 if (defined $page) {
300 if ($page =~ m/[^0-9]/) {
301 die_error(undef, "Invalid page parameter");
305 our $searchtext = $cgi->param('s');
306 if (defined $searchtext) {
307 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
308 die_error(undef, "Invalid search parameter");
310 $searchtext = quotemeta $searchtext;
313 # now read PATH_INFO and use it as alternative to parameters
314 sub evaluate_path_info {
315 return if defined $project;
316 my $path_info = $ENV{"PATH_INFO"};
317 return if !$path_info;
318 $path_info =~ s,^/+,,;
319 return if !$path_info;
320 # find which part of PATH_INFO is project
321 $project = $path_info;
322 $project =~ s,/+$,,;
323 while ($project && !-e "$projectroot/$project/HEAD") {
324 $project =~ s,/*[^/]*$,,;
326 # validate project
327 $project = validate_pathname($project);
328 if (!$project ||
329 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
330 ($strict_export && !project_in_list($project))) {
331 undef $project;
332 return;
334 # do not change any parameters if an action is given using the query string
335 return if $action;
336 $path_info =~ s,^$project/*,,;
337 my ($refname, $pathname) = split(/:/, $path_info, 2);
338 if (defined $pathname) {
339 # we got "project.git/branch:filename" or "project.git/branch:dir/"
340 # we could use git_get_type(branch:pathname), but it needs $git_dir
341 $pathname =~ s,^/+,,;
342 if (!$pathname || substr($pathname, -1) eq "/") {
343 $action ||= "tree";
344 $pathname =~ s,/$,,;
345 } else {
346 $action ||= "blob_plain";
348 $hash_base ||= validate_refname($refname);
349 $file_name ||= validate_pathname($pathname);
350 } elsif (defined $refname) {
351 # we got "project.git/branch"
352 $action ||= "shortlog";
353 $hash ||= validate_refname($refname);
356 evaluate_path_info();
358 # path to the current git repository
359 our $git_dir;
360 $git_dir = "$projectroot/$project" if $project;
362 # dispatch
363 my %actions = (
364 "blame" => \&git_blame2,
365 "blobdiff" => \&git_blobdiff,
366 "blobdiff_plain" => \&git_blobdiff_plain,
367 "blob" => \&git_blob,
368 "blob_plain" => \&git_blob_plain,
369 "commitdiff" => \&git_commitdiff,
370 "commitdiff_plain" => \&git_commitdiff_plain,
371 "commit" => \&git_commit,
372 "heads" => \&git_heads,
373 "history" => \&git_history,
374 "log" => \&git_log,
375 "rss" => \&git_rss,
376 "search" => \&git_search,
377 "shortlog" => \&git_shortlog,
378 "summary" => \&git_summary,
379 "tag" => \&git_tag,
380 "tags" => \&git_tags,
381 "tree" => \&git_tree,
382 "snapshot" => \&git_snapshot,
383 # those below don't need $project
384 "opml" => \&git_opml,
385 "project_list" => \&git_project_list,
386 "project_index" => \&git_project_index,
389 if (defined $project) {
390 $action ||= 'summary';
391 } else {
392 $action ||= 'project_list';
394 if (!defined($actions{$action})) {
395 die_error(undef, "Unknown action");
397 if ($action !~ m/^(opml|project_list|project_index)$/ &&
398 !$project) {
399 die_error(undef, "Project needed");
401 $actions{$action}->();
402 exit;
404 ## ======================================================================
405 ## action links
407 sub href(%) {
408 my %params = @_;
409 my $href = $my_uri;
411 my @mapping = (
412 project => "p",
413 action => "a",
414 file_name => "f",
415 file_parent => "fp",
416 hash => "h",
417 hash_parent => "hp",
418 hash_base => "hb",
419 hash_parent_base => "hpb",
420 page => "pg",
421 order => "o",
422 searchtext => "s",
424 my %mapping = @mapping;
426 $params{'project'} = $project unless exists $params{'project'};
428 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
429 if ($use_pathinfo) {
430 # use PATH_INFO for project name
431 $href .= "/$params{'project'}" if defined $params{'project'};
432 delete $params{'project'};
434 # Summary just uses the project path URL
435 if (defined $params{'action'} && $params{'action'} eq 'summary') {
436 delete $params{'action'};
440 # now encode the parameters explicitly
441 my @result = ();
442 for (my $i = 0; $i < @mapping; $i += 2) {
443 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
444 if (defined $params{$name}) {
445 push @result, $symbol . "=" . esc_param($params{$name});
448 $href .= "?" . join(';', @result) if scalar @result;
450 return $href;
454 ## ======================================================================
455 ## validation, quoting/unquoting and escaping
457 sub validate_pathname {
458 my $input = shift || return undef;
460 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
461 # at the beginning, at the end, and between slashes.
462 # also this catches doubled slashes
463 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
464 return undef;
466 # no null characters
467 if ($input =~ m!\0!) {
468 return undef;
470 return $input;
473 sub validate_refname {
474 my $input = shift || return undef;
476 # textual hashes are O.K.
477 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
478 return $input;
480 # it must be correct pathname
481 $input = validate_pathname($input)
482 or return undef;
483 # restrictions on ref name according to git-check-ref-format
484 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
485 return undef;
487 return $input;
490 # quote unsafe chars, but keep the slash, even when it's not
491 # correct, but quoted slashes look too horrible in bookmarks
492 sub esc_param {
493 my $str = shift;
494 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
495 $str =~ s/\+/%2B/g;
496 $str =~ s/ /\+/g;
497 return $str;
500 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
501 sub esc_url {
502 my $str = shift;
503 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
504 $str =~ s/\+/%2B/g;
505 $str =~ s/ /\+/g;
506 return $str;
509 # replace invalid utf8 character with SUBSTITUTION sequence
510 sub esc_html {
511 my $str = shift;
512 $str = decode("utf8", $str, Encode::FB_DEFAULT);
513 $str = escapeHTML($str);
514 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
515 return $str;
518 # git may return quoted and escaped filenames
519 sub unquote {
520 my $str = shift;
521 if ($str =~ m/^"(.*)"$/) {
522 $str = $1;
523 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
525 return $str;
528 # escape tabs (convert tabs to spaces)
529 sub untabify {
530 my $line = shift;
532 while ((my $pos = index($line, "\t")) != -1) {
533 if (my $count = (8 - ($pos % 8))) {
534 my $spaces = ' ' x $count;
535 $line =~ s/\t/$spaces/;
539 return $line;
542 sub project_in_list {
543 my $project = shift;
544 my @list = git_get_projects_list();
545 return @list && scalar(grep { $_->{'path'} eq $project } @list);
548 ## ----------------------------------------------------------------------
549 ## HTML aware string manipulation
551 sub chop_str {
552 my $str = shift;
553 my $len = shift;
554 my $add_len = shift || 10;
556 # allow only $len chars, but don't cut a word if it would fit in $add_len
557 # if it doesn't fit, cut it if it's still longer than the dots we would add
558 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
559 my $body = $1;
560 my $tail = $2;
561 if (length($tail) > 4) {
562 $tail = " ...";
563 $body =~ s/&[^;]*$//; # remove chopped character entities
565 return "$body$tail";
568 ## ----------------------------------------------------------------------
569 ## functions returning short strings
571 # CSS class for given age value (in seconds)
572 sub age_class {
573 my $age = shift;
575 if ($age < 60*60*2) {
576 return "age0";
577 } elsif ($age < 60*60*24*2) {
578 return "age1";
579 } else {
580 return "age2";
584 # convert age in seconds to "nn units ago" string
585 sub age_string {
586 my $age = shift;
587 my $age_str;
589 if ($age > 60*60*24*365*2) {
590 $age_str = (int $age/60/60/24/365);
591 $age_str .= " years ago";
592 } elsif ($age > 60*60*24*(365/12)*2) {
593 $age_str = int $age/60/60/24/(365/12);
594 $age_str .= " months ago";
595 } elsif ($age > 60*60*24*7*2) {
596 $age_str = int $age/60/60/24/7;
597 $age_str .= " weeks ago";
598 } elsif ($age > 60*60*24*2) {
599 $age_str = int $age/60/60/24;
600 $age_str .= " days ago";
601 } elsif ($age > 60*60*2) {
602 $age_str = int $age/60/60;
603 $age_str .= " hours ago";
604 } elsif ($age > 60*2) {
605 $age_str = int $age/60;
606 $age_str .= " min ago";
607 } elsif ($age > 2) {
608 $age_str = int $age;
609 $age_str .= " sec ago";
610 } else {
611 $age_str .= " right now";
613 return $age_str;
616 # convert file mode in octal to symbolic file mode string
617 sub mode_str {
618 my $mode = oct shift;
620 if (S_ISDIR($mode & S_IFMT)) {
621 return 'drwxr-xr-x';
622 } elsif (S_ISLNK($mode)) {
623 return 'lrwxrwxrwx';
624 } elsif (S_ISREG($mode)) {
625 # git cares only about the executable bit
626 if ($mode & S_IXUSR) {
627 return '-rwxr-xr-x';
628 } else {
629 return '-rw-r--r--';
631 } else {
632 return '----------';
636 # convert file mode in octal to file type string
637 sub file_type {
638 my $mode = shift;
640 if ($mode !~ m/^[0-7]+$/) {
641 return $mode;
642 } else {
643 $mode = oct $mode;
646 if (S_ISDIR($mode & S_IFMT)) {
647 return "directory";
648 } elsif (S_ISLNK($mode)) {
649 return "symlink";
650 } elsif (S_ISREG($mode)) {
651 return "file";
652 } else {
653 return "unknown";
657 ## ----------------------------------------------------------------------
658 ## functions returning short HTML fragments, or transforming HTML fragments
659 ## which don't beling to other sections
661 # format line of commit message or tag comment
662 sub format_log_line_html {
663 my $line = shift;
665 $line = esc_html($line);
666 $line =~ s/ /&nbsp;/g;
667 if ($line =~ m/([0-9a-fA-F]{40})/) {
668 my $hash_text = $1;
669 if (git_get_type($hash_text) eq "commit") {
670 my $link =
671 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
672 -class => "text"}, $hash_text);
673 $line =~ s/$hash_text/$link/;
676 return $line;
679 # format marker of refs pointing to given object
680 sub format_ref_marker {
681 my ($refs, $id) = @_;
682 my $markers = '';
684 if (defined $refs->{$id}) {
685 foreach my $ref (@{$refs->{$id}}) {
686 my ($type, $name) = qw();
687 # e.g. tags/v2.6.11 or heads/next
688 if ($ref =~ m!^(.*?)s?/(.*)$!) {
689 $type = $1;
690 $name = $2;
691 } else {
692 $type = "ref";
693 $name = $ref;
696 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
700 if ($markers) {
701 return ' <span class="refs">'. $markers . '</span>';
702 } else {
703 return "";
707 # format, perhaps shortened and with markers, title line
708 sub format_subject_html {
709 my ($long, $short, $href, $extra) = @_;
710 $extra = '' unless defined($extra);
712 if (length($short) < length($long)) {
713 return $cgi->a({-href => $href, -class => "list subject",
714 -title => decode("utf8", $long, Encode::FB_DEFAULT)},
715 esc_html($short) . $extra);
716 } else {
717 return $cgi->a({-href => $href, -class => "list subject"},
718 esc_html($long) . $extra);
722 sub format_diff_line {
723 my $line = shift;
724 my $char = substr($line, 0, 1);
725 my $diff_class = "";
727 chomp $line;
729 if ($char eq '+') {
730 $diff_class = " add";
731 } elsif ($char eq "-") {
732 $diff_class = " rem";
733 } elsif ($char eq "@") {
734 $diff_class = " chunk_header";
735 } elsif ($char eq "\\") {
736 $diff_class = " incomplete";
738 $line = untabify($line);
739 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
742 ## ----------------------------------------------------------------------
743 ## git utility subroutines, invoking git commands
745 # returns path to the core git executable and the --git-dir parameter as list
746 sub git_cmd {
747 return $GIT, '--git-dir='.$git_dir;
750 # returns path to the core git executable and the --git-dir parameter as string
751 sub git_cmd_str {
752 return join(' ', git_cmd());
755 # get HEAD ref of given project as hash
756 sub git_get_head_hash {
757 my $project = shift;
758 my $o_git_dir = $git_dir;
759 my $retval = undef;
760 $git_dir = "$projectroot/$project";
761 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
762 my $head = <$fd>;
763 close $fd;
764 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
765 $retval = $1;
768 if (defined $o_git_dir) {
769 $git_dir = $o_git_dir;
771 return $retval;
774 # get type of given object
775 sub git_get_type {
776 my $hash = shift;
778 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
779 my $type = <$fd>;
780 close $fd or return;
781 chomp $type;
782 return $type;
785 sub git_get_project_config {
786 my ($key, $type) = @_;
788 return unless ($key);
789 $key =~ s/^gitweb\.//;
790 return if ($key =~ m/\W/);
792 my @x = (git_cmd(), 'repo-config');
793 if (defined $type) { push @x, $type; }
794 push @x, "--get";
795 push @x, "gitweb.$key";
796 my $val = qx(@x);
797 chomp $val;
798 return ($val);
801 # get hash of given path at given ref
802 sub git_get_hash_by_path {
803 my $base = shift;
804 my $path = shift || return undef;
805 my $type = shift;
807 $path =~ s,/+$,,;
809 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
810 or die_error(undef, "Open git-ls-tree failed");
811 my $line = <$fd>;
812 close $fd or return undef;
814 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
815 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
816 if (defined $type && $type ne $2) {
817 # type doesn't match
818 return undef;
820 return $3;
823 ## ......................................................................
824 ## git utility functions, directly accessing git repository
826 sub git_get_project_description {
827 my $path = shift;
829 open my $fd, "$projectroot/$path/description" or return undef;
830 my $descr = <$fd>;
831 close $fd;
832 chomp $descr;
833 return $descr;
836 sub git_get_project_url_list {
837 my $path = shift;
839 open my $fd, "$projectroot/$path/cloneurl" or return;
840 my @git_project_url_list = map { chomp; $_ } <$fd>;
841 close $fd;
843 return wantarray ? @git_project_url_list : \@git_project_url_list;
846 sub git_get_projects_list {
847 my @list;
849 if (-d $projects_list) {
850 # search in directory
851 my $dir = $projects_list;
852 my $pfxlen = length("$dir");
854 File::Find::find({
855 follow_fast => 1, # follow symbolic links
856 dangling_symlinks => 0, # ignore dangling symlinks, silently
857 wanted => sub {
858 # skip project-list toplevel, if we get it.
859 return if (m!^[/.]$!);
860 # only directories can be git repositories
861 return unless (-d $_);
863 my $subdir = substr($File::Find::name, $pfxlen + 1);
864 # we check related file in $projectroot
865 if (-e "$projectroot/$subdir/HEAD" && (!$export_ok ||
866 -e "$projectroot/$subdir/$export_ok")) {
867 push @list, { path => $subdir };
868 $File::Find::prune = 1;
871 }, "$dir");
873 } elsif (-f $projects_list) {
874 # read from file(url-encoded):
875 # 'git%2Fgit.git Linus+Torvalds'
876 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
877 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
878 open my ($fd), $projects_list or return;
879 while (my $line = <$fd>) {
880 chomp $line;
881 my ($path, $owner) = split ' ', $line;
882 $path = unescape($path);
883 $owner = unescape($owner);
884 if (!defined $path) {
885 next;
887 if (-e "$projectroot/$path/HEAD" && (!$export_ok ||
888 -e "$projectroot/$path/$export_ok")) {
889 my $pr = {
890 path => $path,
891 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
893 push @list, $pr
896 close $fd;
898 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
899 return @list;
902 sub git_get_project_owner {
903 my $project = shift;
904 my $owner;
906 return undef unless $project;
908 # read from file (url-encoded):
909 # 'git%2Fgit.git Linus+Torvalds'
910 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
911 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
912 if (-f $projects_list) {
913 open (my $fd , $projects_list);
914 while (my $line = <$fd>) {
915 chomp $line;
916 my ($pr, $ow) = split ' ', $line;
917 $pr = unescape($pr);
918 $ow = unescape($ow);
919 if ($pr eq $project) {
920 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
921 last;
924 close $fd;
926 if (!defined $owner) {
927 $owner = get_file_owner("$projectroot/$project");
930 return $owner;
933 sub git_get_references {
934 my $type = shift || "";
935 my %refs;
936 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
937 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
938 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
939 or return;
941 while (my $line = <$fd>) {
942 chomp $line;
943 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
944 if (defined $refs{$1}) {
945 push @{$refs{$1}}, $2;
946 } else {
947 $refs{$1} = [ $2 ];
951 close $fd or return;
952 return \%refs;
955 sub git_get_rev_name_tags {
956 my $hash = shift || return undef;
958 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
959 or return;
960 my $name_rev = <$fd>;
961 close $fd;
963 if ($name_rev =~ m|^$hash tags/(.*)$|) {
964 return $1;
965 } else {
966 # catches also '$hash undefined' output
967 return undef;
971 ## ----------------------------------------------------------------------
972 ## parse to hash functions
974 sub parse_date {
975 my $epoch = shift;
976 my $tz = shift || "-0000";
978 my %date;
979 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
980 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
981 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
982 $date{'hour'} = $hour;
983 $date{'minute'} = $min;
984 $date{'mday'} = $mday;
985 $date{'day'} = $days[$wday];
986 $date{'month'} = $months[$mon];
987 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
988 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
989 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
990 $mday, $months[$mon], $hour ,$min;
992 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
993 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
994 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
995 $date{'hour_local'} = $hour;
996 $date{'minute_local'} = $min;
997 $date{'tz_local'} = $tz;
998 return %date;
1001 sub parse_tag {
1002 my $tag_id = shift;
1003 my %tag;
1004 my @comment;
1006 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1007 $tag{'id'} = $tag_id;
1008 while (my $line = <$fd>) {
1009 chomp $line;
1010 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1011 $tag{'object'} = $1;
1012 } elsif ($line =~ m/^type (.+)$/) {
1013 $tag{'type'} = $1;
1014 } elsif ($line =~ m/^tag (.+)$/) {
1015 $tag{'name'} = $1;
1016 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1017 $tag{'author'} = $1;
1018 $tag{'epoch'} = $2;
1019 $tag{'tz'} = $3;
1020 } elsif ($line =~ m/--BEGIN/) {
1021 push @comment, $line;
1022 last;
1023 } elsif ($line eq "") {
1024 last;
1027 push @comment, <$fd>;
1028 $tag{'comment'} = \@comment;
1029 close $fd or return;
1030 if (!defined $tag{'name'}) {
1031 return
1033 return %tag
1036 sub parse_commit {
1037 my $commit_id = shift;
1038 my $commit_text = shift;
1040 my @commit_lines;
1041 my %co;
1043 if (defined $commit_text) {
1044 @commit_lines = @$commit_text;
1045 } else {
1046 $/ = "\0";
1047 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
1048 or return;
1049 @commit_lines = split '\n', <$fd>;
1050 close $fd or return;
1051 $/ = "\n";
1052 pop @commit_lines;
1054 my $header = shift @commit_lines;
1055 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1056 return;
1058 ($co{'id'}, my @parents) = split ' ', $header;
1059 $co{'parents'} = \@parents;
1060 $co{'parent'} = $parents[0];
1061 while (my $line = shift @commit_lines) {
1062 last if $line eq "\n";
1063 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1064 $co{'tree'} = $1;
1065 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1066 $co{'author'} = $1;
1067 $co{'author_epoch'} = $2;
1068 $co{'author_tz'} = $3;
1069 if ($co{'author'} =~ m/^([^<]+) </) {
1070 $co{'author_name'} = $1;
1071 } else {
1072 $co{'author_name'} = $co{'author'};
1074 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1075 $co{'committer'} = $1;
1076 $co{'committer_epoch'} = $2;
1077 $co{'committer_tz'} = $3;
1078 $co{'committer_name'} = $co{'committer'};
1079 $co{'committer_name'} =~ s/ <.*//;
1082 if (!defined $co{'tree'}) {
1083 return;
1086 foreach my $title (@commit_lines) {
1087 $title =~ s/^ //;
1088 if ($title ne "") {
1089 $co{'title'} = chop_str($title, 80, 5);
1090 # remove leading stuff of merges to make the interesting part visible
1091 if (length($title) > 50) {
1092 $title =~ s/^Automatic //;
1093 $title =~ s/^merge (of|with) /Merge ... /i;
1094 if (length($title) > 50) {
1095 $title =~ s/(http|rsync):\/\///;
1097 if (length($title) > 50) {
1098 $title =~ s/(master|www|rsync)\.//;
1100 if (length($title) > 50) {
1101 $title =~ s/kernel.org:?//;
1103 if (length($title) > 50) {
1104 $title =~ s/\/pub\/scm//;
1107 $co{'title_short'} = chop_str($title, 50, 5);
1108 last;
1111 # remove added spaces
1112 foreach my $line (@commit_lines) {
1113 $line =~ s/^ //;
1115 $co{'comment'} = \@commit_lines;
1117 my $age = time - $co{'committer_epoch'};
1118 $co{'age'} = $age;
1119 $co{'age_string'} = age_string($age);
1120 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1121 if ($age > 60*60*24*7*2) {
1122 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1123 $co{'age_string_age'} = $co{'age_string'};
1124 } else {
1125 $co{'age_string_date'} = $co{'age_string'};
1126 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1128 return %co;
1131 # parse ref from ref_file, given by ref_id, with given type
1132 sub parse_ref {
1133 my $ref_file = shift;
1134 my $ref_id = shift;
1135 my $type = shift || git_get_type($ref_id);
1136 my %ref_item;
1138 $ref_item{'type'} = $type;
1139 $ref_item{'id'} = $ref_id;
1140 $ref_item{'epoch'} = 0;
1141 $ref_item{'age'} = "unknown";
1142 if ($type eq "tag") {
1143 my %tag = parse_tag($ref_id);
1144 $ref_item{'comment'} = $tag{'comment'};
1145 if ($tag{'type'} eq "commit") {
1146 my %co = parse_commit($tag{'object'});
1147 $ref_item{'epoch'} = $co{'committer_epoch'};
1148 $ref_item{'age'} = $co{'age_string'};
1149 } elsif (defined($tag{'epoch'})) {
1150 my $age = time - $tag{'epoch'};
1151 $ref_item{'epoch'} = $tag{'epoch'};
1152 $ref_item{'age'} = age_string($age);
1154 $ref_item{'reftype'} = $tag{'type'};
1155 $ref_item{'name'} = $tag{'name'};
1156 $ref_item{'refid'} = $tag{'object'};
1157 } elsif ($type eq "commit"){
1158 my %co = parse_commit($ref_id);
1159 $ref_item{'reftype'} = "commit";
1160 $ref_item{'name'} = $ref_file;
1161 $ref_item{'title'} = $co{'title'};
1162 $ref_item{'refid'} = $ref_id;
1163 $ref_item{'epoch'} = $co{'committer_epoch'};
1164 $ref_item{'age'} = $co{'age_string'};
1165 } else {
1166 $ref_item{'reftype'} = $type;
1167 $ref_item{'name'} = $ref_file;
1168 $ref_item{'refid'} = $ref_id;
1171 return %ref_item;
1174 # parse line of git-diff-tree "raw" output
1175 sub parse_difftree_raw_line {
1176 my $line = shift;
1177 my %res;
1179 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1180 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1181 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1182 $res{'from_mode'} = $1;
1183 $res{'to_mode'} = $2;
1184 $res{'from_id'} = $3;
1185 $res{'to_id'} = $4;
1186 $res{'status'} = $5;
1187 $res{'similarity'} = $6;
1188 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1189 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1190 } else {
1191 $res{'file'} = unquote($7);
1194 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1195 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1196 $res{'commit'} = $1;
1199 return wantarray ? %res : \%res;
1202 # parse line of git-ls-tree output
1203 sub parse_ls_tree_line ($;%) {
1204 my $line = shift;
1205 my %opts = @_;
1206 my %res;
1208 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1209 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1211 $res{'mode'} = $1;
1212 $res{'type'} = $2;
1213 $res{'hash'} = $3;
1214 if ($opts{'-z'}) {
1215 $res{'name'} = $4;
1216 } else {
1217 $res{'name'} = unquote($4);
1220 return wantarray ? %res : \%res;
1223 ## ......................................................................
1224 ## parse to array of hashes functions
1226 sub git_get_refs_list {
1227 my $type = shift || "";
1228 my %refs;
1229 my @reflist;
1231 my @refs;
1232 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1233 or return;
1234 while (my $line = <$fd>) {
1235 chomp $line;
1236 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1237 if (defined $refs{$1}) {
1238 push @{$refs{$1}}, $2;
1239 } else {
1240 $refs{$1} = [ $2 ];
1243 if (! $4) { # unpeeled, direct reference
1244 push @refs, { hash => $1, name => $3 }; # without type
1245 } elsif ($3 eq $refs[-1]{'name'}) {
1246 # most likely a tag is followed by its peeled
1247 # (deref) one, and when that happens we know the
1248 # previous one was of type 'tag'.
1249 $refs[-1]{'type'} = "tag";
1253 close $fd;
1255 foreach my $ref (@refs) {
1256 my $ref_file = $ref->{'name'};
1257 my $ref_id = $ref->{'hash'};
1259 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1260 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1262 push @reflist, \%ref_item;
1264 # sort refs by age
1265 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1266 return (\@reflist, \%refs);
1269 ## ----------------------------------------------------------------------
1270 ## filesystem-related functions
1272 sub get_file_owner {
1273 my $path = shift;
1275 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1276 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1277 if (!defined $gcos) {
1278 return undef;
1280 my $owner = $gcos;
1281 $owner =~ s/[,;].*$//;
1282 return decode("utf8", $owner, Encode::FB_DEFAULT);
1285 ## ......................................................................
1286 ## mimetype related functions
1288 sub mimetype_guess_file {
1289 my $filename = shift;
1290 my $mimemap = shift;
1291 -r $mimemap or return undef;
1293 my %mimemap;
1294 open(MIME, $mimemap) or return undef;
1295 while (<MIME>) {
1296 next if m/^#/; # skip comments
1297 my ($mime, $exts) = split(/\t+/);
1298 if (defined $exts) {
1299 my @exts = split(/\s+/, $exts);
1300 foreach my $ext (@exts) {
1301 $mimemap{$ext} = $mime;
1305 close(MIME);
1307 $filename =~ /\.([^.]*)$/;
1308 return $mimemap{$1};
1311 sub mimetype_guess {
1312 my $filename = shift;
1313 my $mime;
1314 $filename =~ /\./ or return undef;
1316 if ($mimetypes_file) {
1317 my $file = $mimetypes_file;
1318 if ($file !~ m!^/!) { # if it is relative path
1319 # it is relative to project
1320 $file = "$projectroot/$project/$file";
1322 $mime = mimetype_guess_file($filename, $file);
1324 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1325 return $mime;
1328 sub blob_mimetype {
1329 my $fd = shift;
1330 my $filename = shift;
1332 if ($filename) {
1333 my $mime = mimetype_guess($filename);
1334 $mime and return $mime;
1337 # just in case
1338 return $default_blob_plain_mimetype unless $fd;
1340 if (-T $fd) {
1341 return 'text/plain' .
1342 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1343 } elsif (! $filename) {
1344 return 'application/octet-stream';
1345 } elsif ($filename =~ m/\.png$/i) {
1346 return 'image/png';
1347 } elsif ($filename =~ m/\.gif$/i) {
1348 return 'image/gif';
1349 } elsif ($filename =~ m/\.jpe?g$/i) {
1350 return 'image/jpeg';
1351 } else {
1352 return 'application/octet-stream';
1356 ## ======================================================================
1357 ## functions printing HTML: header, footer, error page
1359 sub git_header_html {
1360 my $status = shift || "200 OK";
1361 my $expires = shift;
1363 my $title = "$site_name git";
1364 if (defined $project) {
1365 $title .= " - $project";
1366 if (defined $action) {
1367 $title .= "/$action";
1368 if (defined $file_name) {
1369 $title .= " - " . esc_html($file_name);
1370 if ($action eq "tree" && $file_name !~ m|/$|) {
1371 $title .= "/";
1376 my $content_type;
1377 # require explicit support from the UA if we are to send the page as
1378 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1379 # we have to do this because MSIE sometimes globs '*/*', pretending to
1380 # support xhtml+xml but choking when it gets what it asked for.
1381 if (defined $cgi->http('HTTP_ACCEPT') &&
1382 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1383 $cgi->Accept('application/xhtml+xml') != 0) {
1384 $content_type = 'application/xhtml+xml';
1385 } else {
1386 $content_type = 'text/html';
1388 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1389 -status=> $status, -expires => $expires);
1390 print <<EOF;
1391 <?xml version="1.0" encoding="utf-8"?>
1392 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1393 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1394 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1395 <!-- git core binaries version $git_version -->
1396 <head>
1397 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1398 <meta name="generator" content="gitweb/$version git/$git_version"/>
1399 <meta name="robots" content="index, nofollow"/>
1400 <title>$title</title>
1401 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1403 if (defined $project) {
1404 printf('<link rel="alternate" title="%s log" '.
1405 'href="%s" type="application/rss+xml"/>'."\n",
1406 esc_param($project), href(action=>"rss"));
1407 } else {
1408 printf('<link rel="alternate" title="%s projects list" '.
1409 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1410 $site_name, href(project=>undef, action=>"project_index"));
1411 printf('<link rel="alternate" title="%s projects logs" '.
1412 'href="%s" type="text/x-opml"/>'."\n",
1413 $site_name, href(project=>undef, action=>"opml"));
1415 if (defined $favicon) {
1416 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1419 print "</head>\n" .
1420 "<body>\n" .
1421 "<div class=\"page_header\">\n" .
1422 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1423 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1424 "</a>\n";
1425 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1426 if (defined $project) {
1427 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1428 if (defined $action) {
1429 print " / $action";
1431 print "\n";
1432 if (!defined $searchtext) {
1433 $searchtext = "";
1435 my $search_hash;
1436 if (defined $hash_base) {
1437 $search_hash = $hash_base;
1438 } elsif (defined $hash) {
1439 $search_hash = $hash;
1440 } else {
1441 $search_hash = "HEAD";
1443 $cgi->param("a", "search");
1444 $cgi->param("h", $search_hash);
1445 print $cgi->startform(-method => "get", -action => $my_uri) .
1446 "<div class=\"search\">\n" .
1447 $cgi->hidden(-name => "p") . "\n" .
1448 $cgi->hidden(-name => "a") . "\n" .
1449 $cgi->hidden(-name => "h") . "\n" .
1450 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1451 "</div>" .
1452 $cgi->end_form() . "\n";
1454 print "</div>\n";
1457 sub git_footer_html {
1458 print "<div class=\"page_footer\">\n";
1459 if (defined $project) {
1460 my $descr = git_get_project_description($project);
1461 if (defined $descr) {
1462 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1464 print $cgi->a({-href => href(action=>"rss"),
1465 -class => "rss_logo"}, "RSS") . "\n";
1466 } else {
1467 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1468 -class => "rss_logo"}, "OPML") . " ";
1469 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1470 -class => "rss_logo"}, "TXT") . "\n";
1472 print "</div>\n" .
1473 "</body>\n" .
1474 "</html>";
1477 sub die_error {
1478 my $status = shift || "403 Forbidden";
1479 my $error = shift || "Malformed query, file missing or permission denied";
1481 git_header_html($status);
1482 print <<EOF;
1483 <div class="page_body">
1484 <br /><br />
1485 $status - $error
1486 <br />
1487 </div>
1489 git_footer_html();
1490 exit;
1493 ## ----------------------------------------------------------------------
1494 ## functions printing or outputting HTML: navigation
1496 sub git_print_page_nav {
1497 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1498 $extra = '' if !defined $extra; # pager or formats
1500 my @navs = qw(summary shortlog log commit commitdiff tree);
1501 if ($suppress) {
1502 @navs = grep { $_ ne $suppress } @navs;
1505 my %arg = map { $_ => {action=>$_} } @navs;
1506 if (defined $head) {
1507 for (qw(commit commitdiff)) {
1508 $arg{$_}{hash} = $head;
1510 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1511 for (qw(shortlog log)) {
1512 $arg{$_}{hash} = $head;
1516 $arg{tree}{hash} = $treehead if defined $treehead;
1517 $arg{tree}{hash_base} = $treebase if defined $treebase;
1519 print "<div class=\"page_nav\">\n" .
1520 (join " | ",
1521 map { $_ eq $current ?
1522 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1523 } @navs);
1524 print "<br/>\n$extra<br/>\n" .
1525 "</div>\n";
1528 sub format_paging_nav {
1529 my ($action, $hash, $head, $page, $nrevs) = @_;
1530 my $paging_nav;
1533 if ($hash ne $head || $page) {
1534 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1535 } else {
1536 $paging_nav .= "HEAD";
1539 if ($page > 0) {
1540 $paging_nav .= " &sdot; " .
1541 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1542 -accesskey => "p", -title => "Alt-p"}, "prev");
1543 } else {
1544 $paging_nav .= " &sdot; prev";
1547 if ($nrevs >= (100 * ($page+1)-1)) {
1548 $paging_nav .= " &sdot; " .
1549 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1550 -accesskey => "n", -title => "Alt-n"}, "next");
1551 } else {
1552 $paging_nav .= " &sdot; next";
1555 return $paging_nav;
1558 ## ......................................................................
1559 ## functions printing or outputting HTML: div
1561 sub git_print_header_div {
1562 my ($action, $title, $hash, $hash_base) = @_;
1563 my %args = ();
1565 $args{action} = $action;
1566 $args{hash} = $hash if $hash;
1567 $args{hash_base} = $hash_base if $hash_base;
1569 print "<div class=\"header\">\n" .
1570 $cgi->a({-href => href(%args), -class => "title"},
1571 $title ? $title : $action) .
1572 "\n</div>\n";
1575 #sub git_print_authorship (\%) {
1576 sub git_print_authorship {
1577 my $co = shift;
1579 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1580 print "<div class=\"author_date\">" .
1581 esc_html($co->{'author_name'}) .
1582 " [$ad{'rfc2822'}";
1583 if ($ad{'hour_local'} < 6) {
1584 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1585 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1586 } else {
1587 printf(" (%02d:%02d %s)",
1588 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1590 print "]</div>\n";
1593 sub git_print_page_path {
1594 my $name = shift;
1595 my $type = shift;
1596 my $hb = shift;
1598 if (!defined $name) {
1599 print "<div class=\"page_path\">/</div>\n";
1600 } else {
1601 my @dirname = split '/', $name;
1602 my $basename = pop @dirname;
1603 my $fullname = '';
1605 print "<div class=\"page_path\">";
1606 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1607 -title => 'tree root'}, "[$project]");
1608 print " / ";
1609 foreach my $dir (@dirname) {
1610 $fullname .= ($fullname ? '/' : '') . $dir;
1611 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1612 hash_base=>$hb),
1613 -title => $fullname}, esc_html($dir));
1614 print " / ";
1616 if (defined $type && $type eq 'blob') {
1617 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1618 hash_base=>$hb),
1619 -title => $name}, esc_html($basename));
1620 } elsif (defined $type && $type eq 'tree') {
1621 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1622 hash_base=>$hb),
1623 -title => $name}, esc_html($basename));
1624 } else {
1625 print esc_html($basename);
1627 print "<br/></div>\n";
1631 # sub git_print_log (\@;%) {
1632 sub git_print_log ($;%) {
1633 my $log = shift;
1634 my %opts = @_;
1636 if ($opts{'-remove_title'}) {
1637 # remove title, i.e. first line of log
1638 shift @$log;
1640 # remove leading empty lines
1641 while (defined $log->[0] && $log->[0] eq "") {
1642 shift @$log;
1645 # print log
1646 my $signoff = 0;
1647 my $empty = 0;
1648 foreach my $line (@$log) {
1649 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1650 $signoff = 1;
1651 $empty = 0;
1652 if (! $opts{'-remove_signoff'}) {
1653 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1654 next;
1655 } else {
1656 # remove signoff lines
1657 next;
1659 } else {
1660 $signoff = 0;
1663 # print only one empty line
1664 # do not print empty line after signoff
1665 if ($line eq "") {
1666 next if ($empty || $signoff);
1667 $empty = 1;
1668 } else {
1669 $empty = 0;
1672 print format_log_line_html($line) . "<br/>\n";
1675 if ($opts{'-final_empty_line'}) {
1676 # end with single empty line
1677 print "<br/>\n" unless $empty;
1681 sub git_print_simplified_log {
1682 my $log = shift;
1683 my $remove_title = shift;
1685 git_print_log($log,
1686 -final_empty_line=> 1,
1687 -remove_title => $remove_title);
1690 # print tree entry (row of git_tree), but without encompassing <tr> element
1691 sub git_print_tree_entry {
1692 my ($t, $basedir, $hash_base, $have_blame) = @_;
1694 my %base_key = ();
1695 $base_key{hash_base} = $hash_base if defined $hash_base;
1697 # The format of a table row is: mode list link. Where mode is
1698 # the mode of the entry, list is the name of the entry, an href,
1699 # and link is the action links of the entry.
1701 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1702 if ($t->{'type'} eq "blob") {
1703 print "<td class=\"list\">" .
1704 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1705 file_name=>"$basedir$t->{'name'}", %base_key),
1706 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1707 print "<td class=\"link\">";
1708 if ($have_blame) {
1709 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1710 file_name=>"$basedir$t->{'name'}", %base_key)},
1711 "blame");
1713 if (defined $hash_base) {
1714 if ($have_blame) {
1715 print " | ";
1717 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1718 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1719 "history");
1721 print " | " .
1722 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1723 file_name=>"$basedir$t->{'name'}")},
1724 "raw");
1725 print "</td>\n";
1727 } elsif ($t->{'type'} eq "tree") {
1728 print "<td class=\"list\">";
1729 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1730 file_name=>"$basedir$t->{'name'}", %base_key)},
1731 esc_html($t->{'name'}));
1732 print "</td>\n";
1733 print "<td class=\"link\">";
1734 if (defined $hash_base) {
1735 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1736 file_name=>"$basedir$t->{'name'}")},
1737 "history");
1739 print "</td>\n";
1743 ## ......................................................................
1744 ## functions printing large fragments of HTML
1746 sub git_difftree_body {
1747 my ($difftree, $hash, $parent) = @_;
1749 print "<div class=\"list_head\">\n";
1750 if ($#{$difftree} > 10) {
1751 print(($#{$difftree} + 1) . " files changed:\n");
1753 print "</div>\n";
1755 print "<table class=\"diff_tree\">\n";
1756 my $alternate = 1;
1757 my $patchno = 0;
1758 foreach my $line (@{$difftree}) {
1759 my %diff = parse_difftree_raw_line($line);
1761 if ($alternate) {
1762 print "<tr class=\"dark\">\n";
1763 } else {
1764 print "<tr class=\"light\">\n";
1766 $alternate ^= 1;
1768 my ($to_mode_oct, $to_mode_str, $to_file_type);
1769 my ($from_mode_oct, $from_mode_str, $from_file_type);
1770 if ($diff{'to_mode'} ne ('0' x 6)) {
1771 $to_mode_oct = oct $diff{'to_mode'};
1772 if (S_ISREG($to_mode_oct)) { # only for regular file
1773 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1775 $to_file_type = file_type($diff{'to_mode'});
1777 if ($diff{'from_mode'} ne ('0' x 6)) {
1778 $from_mode_oct = oct $diff{'from_mode'};
1779 if (S_ISREG($to_mode_oct)) { # only for regular file
1780 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1782 $from_file_type = file_type($diff{'from_mode'});
1785 if ($diff{'status'} eq "A") { # created
1786 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1787 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1788 $mode_chng .= "]</span>";
1789 print "<td>";
1790 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1791 hash_base=>$hash, file_name=>$diff{'file'}),
1792 -class => "list"}, esc_html($diff{'file'}));
1793 print "</td>\n";
1794 print "<td>$mode_chng</td>\n";
1795 print "<td class=\"link\">";
1796 if ($action eq 'commitdiff') {
1797 # link to patch
1798 $patchno++;
1799 print $cgi->a({-href => "#patch$patchno"}, "patch");
1801 print "</td>\n";
1803 } elsif ($diff{'status'} eq "D") { # deleted
1804 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1805 print "<td>";
1806 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1807 hash_base=>$parent, file_name=>$diff{'file'}),
1808 -class => "list"}, esc_html($diff{'file'}));
1809 print "</td>\n";
1810 print "<td>$mode_chng</td>\n";
1811 print "<td class=\"link\">";
1812 if ($action eq 'commitdiff') {
1813 # link to patch
1814 $patchno++;
1815 print $cgi->a({-href => "#patch$patchno"}, "patch");
1816 print " | ";
1818 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1819 file_name=>$diff{'file'})},
1820 "blame") . " | ";
1821 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1822 file_name=>$diff{'file'})},
1823 "history");
1824 print "</td>\n";
1826 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1827 my $mode_chnge = "";
1828 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1829 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1830 if ($from_file_type != $to_file_type) {
1831 $mode_chnge .= " from $from_file_type to $to_file_type";
1833 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1834 if ($from_mode_str && $to_mode_str) {
1835 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1836 } elsif ($to_mode_str) {
1837 $mode_chnge .= " mode: $to_mode_str";
1840 $mode_chnge .= "]</span>\n";
1842 print "<td>";
1843 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1844 hash_base=>$hash, file_name=>$diff{'file'}),
1845 -class => "list"}, esc_html($diff{'file'}));
1846 print "</td>\n";
1847 print "<td>$mode_chnge</td>\n";
1848 print "<td class=\"link\">";
1849 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1850 if ($action eq 'commitdiff') {
1851 # link to patch
1852 $patchno++;
1853 print $cgi->a({-href => "#patch$patchno"}, "patch");
1854 } else {
1855 print $cgi->a({-href => href(action=>"blobdiff",
1856 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1857 hash_base=>$hash, hash_parent_base=>$parent,
1858 file_name=>$diff{'file'})},
1859 "diff");
1861 print " | ";
1863 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1864 file_name=>$diff{'file'})},
1865 "blame") . " | ";
1866 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1867 file_name=>$diff{'file'})},
1868 "history");
1869 print "</td>\n";
1871 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1872 my %status_name = ('R' => 'moved', 'C' => 'copied');
1873 my $nstatus = $status_name{$diff{'status'}};
1874 my $mode_chng = "";
1875 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1876 # mode also for directories, so we cannot use $to_mode_str
1877 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1879 print "<td>" .
1880 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1881 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1882 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1883 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1884 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1885 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1886 -class => "list"}, esc_html($diff{'from_file'})) .
1887 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1888 "<td class=\"link\">";
1889 if ($diff{'to_id'} ne $diff{'from_id'}) {
1890 if ($action eq 'commitdiff') {
1891 # link to patch
1892 $patchno++;
1893 print $cgi->a({-href => "#patch$patchno"}, "patch");
1894 } else {
1895 print $cgi->a({-href => href(action=>"blobdiff",
1896 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1897 hash_base=>$hash, hash_parent_base=>$parent,
1898 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1899 "diff");
1901 print " | ";
1903 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1904 file_name=>$diff{'from_file'})},
1905 "blame") . " | ";
1906 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1907 file_name=>$diff{'from_file'})},
1908 "history");
1909 print "</td>\n";
1911 } # we should not encounter Unmerged (U) or Unknown (X) status
1912 print "</tr>\n";
1914 print "</table>\n";
1917 sub git_patchset_body {
1918 my ($fd, $difftree, $hash, $hash_parent) = @_;
1920 my $patch_idx = 0;
1921 my $in_header = 0;
1922 my $patch_found = 0;
1923 my $diffinfo;
1925 print "<div class=\"patchset\">\n";
1927 LINE:
1928 while (my $patch_line = <$fd>) {
1929 chomp $patch_line;
1931 if ($patch_line =~ m/^diff /) { # "git diff" header
1932 # beginning of patch (in patchset)
1933 if ($patch_found) {
1934 # close previous patch
1935 print "</div>\n"; # class="patch"
1936 } else {
1937 # first patch in patchset
1938 $patch_found = 1;
1940 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1942 if (ref($difftree->[$patch_idx]) eq "HASH") {
1943 $diffinfo = $difftree->[$patch_idx];
1944 } else {
1945 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1947 $patch_idx++;
1949 # for now, no extended header, hence we skip empty patches
1950 # companion to next LINE if $in_header;
1951 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1952 $in_header = 1;
1953 next LINE;
1956 if ($diffinfo->{'status'} eq "A") { # added
1957 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1958 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1959 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1960 $diffinfo->{'to_id'}) . "(new)" .
1961 "</div>\n"; # class="diff_info"
1963 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1964 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1965 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1966 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1967 $diffinfo->{'from_id'}) . "(deleted)" .
1968 "</div>\n"; # class="diff_info"
1970 } elsif ($diffinfo->{'status'} eq "R" || # renamed
1971 $diffinfo->{'status'} eq "C" || # copied
1972 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1973 print "<div class=\"diff_info\">" .
1974 file_type($diffinfo->{'from_mode'}) . ":" .
1975 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1976 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1977 $diffinfo->{'from_id'}) .
1978 " -> " .
1979 file_type($diffinfo->{'to_mode'}) . ":" .
1980 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1981 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1982 $diffinfo->{'to_id'});
1983 print "</div>\n"; # class="diff_info"
1985 } else { # modified, mode changed, ...
1986 print "<div class=\"diff_info\">" .
1987 file_type($diffinfo->{'from_mode'}) . ":" .
1988 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1989 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1990 $diffinfo->{'from_id'}) .
1991 " -> " .
1992 file_type($diffinfo->{'to_mode'}) . ":" .
1993 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1994 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1995 $diffinfo->{'to_id'});
1996 print "</div>\n"; # class="diff_info"
1999 #print "<div class=\"diff extended_header\">\n";
2000 $in_header = 1;
2001 next LINE;
2002 } # start of patch in patchset
2005 if ($in_header && $patch_line =~ m/^---/) {
2006 #print "</div>\n"; # class="diff extended_header"
2007 $in_header = 0;
2009 my $file = $diffinfo->{'from_file'};
2010 $file ||= $diffinfo->{'file'};
2011 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2012 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2013 -class => "list"}, esc_html($file));
2014 $patch_line =~ s|a/.*$|a/$file|g;
2015 print "<div class=\"diff from_file\">$patch_line</div>\n";
2017 $patch_line = <$fd>;
2018 chomp $patch_line;
2020 #$patch_line =~ m/^+++/;
2021 $file = $diffinfo->{'to_file'};
2022 $file ||= $diffinfo->{'file'};
2023 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2024 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2025 -class => "list"}, esc_html($file));
2026 $patch_line =~ s|b/.*|b/$file|g;
2027 print "<div class=\"diff to_file\">$patch_line</div>\n";
2029 next LINE;
2031 next LINE if $in_header;
2033 print format_diff_line($patch_line);
2035 print "</div>\n" if $patch_found; # class="patch"
2037 print "</div>\n"; # class="patchset"
2040 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2042 sub git_shortlog_body {
2043 # uses global variable $project
2044 my ($revlist, $from, $to, $refs, $extra) = @_;
2046 $from = 0 unless defined $from;
2047 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2049 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2050 my $alternate = 1;
2051 for (my $i = $from; $i <= $to; $i++) {
2052 my $commit = $revlist->[$i];
2053 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2054 my $ref = format_ref_marker($refs, $commit);
2055 my %co = parse_commit($commit);
2056 if ($alternate) {
2057 print "<tr class=\"dark\">\n";
2058 } else {
2059 print "<tr class=\"light\">\n";
2061 $alternate ^= 1;
2062 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2063 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2064 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2065 "<td>";
2066 print format_subject_html($co{'title'}, $co{'title_short'},
2067 href(action=>"commit", hash=>$commit), $ref);
2068 print "</td>\n" .
2069 "<td class=\"link\">" .
2070 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2071 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") . " | " .
2072 $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2073 print "</td>\n" .
2074 "</tr>\n";
2076 if (defined $extra) {
2077 print "<tr>\n" .
2078 "<td colspan=\"4\">$extra</td>\n" .
2079 "</tr>\n";
2081 print "</table>\n";
2084 sub git_history_body {
2085 # Warning: assumes constant type (blob or tree) during history
2086 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2088 $from = 0 unless defined $from;
2089 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2091 print "<table class=\"history\" cellspacing=\"0\">\n";
2092 my $alternate = 1;
2093 for (my $i = $from; $i <= $to; $i++) {
2094 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2095 next;
2098 my $commit = $1;
2099 my %co = parse_commit($commit);
2100 if (!%co) {
2101 next;
2104 my $ref = format_ref_marker($refs, $commit);
2106 if ($alternate) {
2107 print "<tr class=\"dark\">\n";
2108 } else {
2109 print "<tr class=\"light\">\n";
2111 $alternate ^= 1;
2112 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2113 # shortlog uses chop_str($co{'author_name'}, 10)
2114 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2115 "<td>";
2116 # originally git_history used chop_str($co{'title'}, 50)
2117 print format_subject_html($co{'title'}, $co{'title_short'},
2118 href(action=>"commit", hash=>$commit), $ref);
2119 print "</td>\n" .
2120 "<td class=\"link\">" .
2121 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2122 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2124 if ($ftype eq 'blob') {
2125 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2126 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2127 if (defined $blob_current && defined $blob_parent &&
2128 $blob_current ne $blob_parent) {
2129 print " | " .
2130 $cgi->a({-href => href(action=>"blobdiff",
2131 hash=>$blob_current, hash_parent=>$blob_parent,
2132 hash_base=>$hash_base, hash_parent_base=>$commit,
2133 file_name=>$file_name)},
2134 "diff to current");
2137 print "</td>\n" .
2138 "</tr>\n";
2140 if (defined $extra) {
2141 print "<tr>\n" .
2142 "<td colspan=\"4\">$extra</td>\n" .
2143 "</tr>\n";
2145 print "</table>\n";
2148 sub git_tags_body {
2149 # uses global variable $project
2150 my ($taglist, $from, $to, $extra) = @_;
2151 $from = 0 unless defined $from;
2152 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2154 print "<table class=\"tags\" cellspacing=\"0\">\n";
2155 my $alternate = 1;
2156 for (my $i = $from; $i <= $to; $i++) {
2157 my $entry = $taglist->[$i];
2158 my %tag = %$entry;
2159 my $comment_lines = $tag{'comment'};
2160 my $comment = shift @$comment_lines;
2161 my $comment_short;
2162 if (defined $comment) {
2163 $comment_short = chop_str($comment, 30, 5);
2165 if ($alternate) {
2166 print "<tr class=\"dark\">\n";
2167 } else {
2168 print "<tr class=\"light\">\n";
2170 $alternate ^= 1;
2171 print "<td><i>$tag{'age'}</i></td>\n" .
2172 "<td>" .
2173 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2174 -class => "list name"}, esc_html($tag{'name'})) .
2175 "</td>\n" .
2176 "<td>";
2177 if (defined $comment) {
2178 print format_subject_html($comment, $comment_short,
2179 href(action=>"tag", hash=>$tag{'id'}));
2181 print "</td>\n" .
2182 "<td class=\"selflink\">";
2183 if ($tag{'type'} eq "tag") {
2184 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2185 } else {
2186 print "&nbsp;";
2188 print "</td>\n" .
2189 "<td class=\"link\">" . " | " .
2190 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2191 if ($tag{'reftype'} eq "commit") {
2192 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2193 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2194 } elsif ($tag{'reftype'} eq "blob") {
2195 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2197 print "</td>\n" .
2198 "</tr>";
2200 if (defined $extra) {
2201 print "<tr>\n" .
2202 "<td colspan=\"5\">$extra</td>\n" .
2203 "</tr>\n";
2205 print "</table>\n";
2208 sub git_heads_body {
2209 # uses global variable $project
2210 my ($headlist, $head, $from, $to, $extra) = @_;
2211 $from = 0 unless defined $from;
2212 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2214 print "<table class=\"heads\" cellspacing=\"0\">\n";
2215 my $alternate = 1;
2216 for (my $i = $from; $i <= $to; $i++) {
2217 my $entry = $headlist->[$i];
2218 my %tag = %$entry;
2219 my $curr = $tag{'id'} eq $head;
2220 if ($alternate) {
2221 print "<tr class=\"dark\">\n";
2222 } else {
2223 print "<tr class=\"light\">\n";
2225 $alternate ^= 1;
2226 print "<td><i>$tag{'age'}</i></td>\n" .
2227 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2228 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2229 -class => "list name"},esc_html($tag{'name'})) .
2230 "</td>\n" .
2231 "<td class=\"link\">" .
2232 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2233 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2234 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2235 "</td>\n" .
2236 "</tr>";
2238 if (defined $extra) {
2239 print "<tr>\n" .
2240 "<td colspan=\"3\">$extra</td>\n" .
2241 "</tr>\n";
2243 print "</table>\n";
2246 ## ======================================================================
2247 ## ======================================================================
2248 ## actions
2250 sub git_project_list {
2251 my $order = $cgi->param('o');
2252 if (defined $order && $order !~ m/project|descr|owner|age/) {
2253 die_error(undef, "Unknown order parameter");
2256 my @list = git_get_projects_list();
2257 my @projects;
2258 if (!@list) {
2259 die_error(undef, "No projects found");
2261 foreach my $pr (@list) {
2262 my $head = git_get_head_hash($pr->{'path'});
2263 if (!defined $head) {
2264 next;
2266 $git_dir = "$projectroot/$pr->{'path'}";
2267 my %co = parse_commit($head);
2268 if (!%co) {
2269 next;
2271 $pr->{'commit'} = \%co;
2272 if (!defined $pr->{'descr'}) {
2273 my $descr = git_get_project_description($pr->{'path'}) || "";
2274 $pr->{'descr'} = chop_str($descr, 25, 5);
2276 if (!defined $pr->{'owner'}) {
2277 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2279 push @projects, $pr;
2282 git_header_html();
2283 if (-f $home_text) {
2284 print "<div class=\"index_include\">\n";
2285 open (my $fd, $home_text);
2286 print <$fd>;
2287 close $fd;
2288 print "</div>\n";
2290 print "<table class=\"project_list\">\n" .
2291 "<tr>\n";
2292 $order ||= "project";
2293 if ($order eq "project") {
2294 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2295 print "<th>Project</th>\n";
2296 } else {
2297 print "<th>" .
2298 $cgi->a({-href => href(project=>undef, order=>'project'),
2299 -class => "header"}, "Project") .
2300 "</th>\n";
2302 if ($order eq "descr") {
2303 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2304 print "<th>Description</th>\n";
2305 } else {
2306 print "<th>" .
2307 $cgi->a({-href => href(project=>undef, order=>'descr'),
2308 -class => "header"}, "Description") .
2309 "</th>\n";
2311 if ($order eq "owner") {
2312 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2313 print "<th>Owner</th>\n";
2314 } else {
2315 print "<th>" .
2316 $cgi->a({-href => href(project=>undef, order=>'owner'),
2317 -class => "header"}, "Owner") .
2318 "</th>\n";
2320 if ($order eq "age") {
2321 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2322 print "<th>Last Change</th>\n";
2323 } else {
2324 print "<th>" .
2325 $cgi->a({-href => href(project=>undef, order=>'age'),
2326 -class => "header"}, "Last Change") .
2327 "</th>\n";
2329 print "<th></th>\n" .
2330 "</tr>\n";
2331 my $alternate = 1;
2332 foreach my $pr (@projects) {
2333 if ($alternate) {
2334 print "<tr class=\"dark\">\n";
2335 } else {
2336 print "<tr class=\"light\">\n";
2338 $alternate ^= 1;
2339 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2340 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2341 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2342 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2343 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2344 $pr->{'commit'}{'age_string'} . "</td>\n" .
2345 "<td class=\"link\">" .
2346 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2347 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2348 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2349 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2350 "</td>\n" .
2351 "</tr>\n";
2353 print "</table>\n";
2354 git_footer_html();
2357 sub git_project_index {
2358 my @projects = git_get_projects_list();
2360 print $cgi->header(
2361 -type => 'text/plain',
2362 -charset => 'utf-8',
2363 -content_disposition => 'inline; filename="index.aux"');
2365 foreach my $pr (@projects) {
2366 if (!exists $pr->{'owner'}) {
2367 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2370 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2371 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2372 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2373 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2374 $path =~ s/ /\+/g;
2375 $owner =~ s/ /\+/g;
2377 print "$path $owner\n";
2381 sub git_summary {
2382 my $descr = git_get_project_description($project) || "none";
2383 my $head = git_get_head_hash($project);
2384 my %co = parse_commit($head);
2385 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2387 my $owner = git_get_project_owner($project);
2389 my ($reflist, $refs) = git_get_refs_list();
2391 my @taglist;
2392 my @headlist;
2393 foreach my $ref (@$reflist) {
2394 if ($ref->{'name'} =~ s!^heads/!!) {
2395 push @headlist, $ref;
2396 } else {
2397 $ref->{'name'} =~ s!^tags/!!;
2398 push @taglist, $ref;
2402 git_header_html();
2403 git_print_page_nav('summary','', $head);
2405 print "<div class=\"title\">&nbsp;</div>\n";
2406 print "<table cellspacing=\"0\">\n" .
2407 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2408 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2409 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2410 # use per project git URL list in $projectroot/$project/cloneurl
2411 # or make project git URL from git base URL and project name
2412 my $url_tag = "URL";
2413 my @url_list = git_get_project_url_list($project);
2414 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2415 foreach my $git_url (@url_list) {
2416 next unless $git_url;
2417 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2418 $url_tag = "";
2420 print "</table>\n";
2422 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2423 git_get_head_hash($project)
2424 or die_error(undef, "Open git-rev-list failed");
2425 my @revlist = map { chomp; $_ } <$fd>;
2426 close $fd;
2427 git_print_header_div('shortlog');
2428 git_shortlog_body(\@revlist, 0, 15, $refs,
2429 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2431 if (@taglist) {
2432 git_print_header_div('tags');
2433 git_tags_body(\@taglist, 0, 15,
2434 $cgi->a({-href => href(action=>"tags")}, "..."));
2437 if (@headlist) {
2438 git_print_header_div('heads');
2439 git_heads_body(\@headlist, $head, 0, 15,
2440 $cgi->a({-href => href(action=>"heads")}, "..."));
2443 git_footer_html();
2446 sub git_tag {
2447 my $head = git_get_head_hash($project);
2448 git_header_html();
2449 git_print_page_nav('','', $head,undef,$head);
2450 my %tag = parse_tag($hash);
2451 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2452 print "<div class=\"title_text\">\n" .
2453 "<table cellspacing=\"0\">\n" .
2454 "<tr>\n" .
2455 "<td>object</td>\n" .
2456 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2457 $tag{'object'}) . "</td>\n" .
2458 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2459 $tag{'type'}) . "</td>\n" .
2460 "</tr>\n";
2461 if (defined($tag{'author'})) {
2462 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2463 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2464 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2465 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2466 "</td></tr>\n";
2468 print "</table>\n\n" .
2469 "</div>\n";
2470 print "<div class=\"page_body\">";
2471 my $comment = $tag{'comment'};
2472 foreach my $line (@$comment) {
2473 print esc_html($line) . "<br/>\n";
2475 print "</div>\n";
2476 git_footer_html();
2479 sub git_blame2 {
2480 my $fd;
2481 my $ftype;
2483 my ($have_blame) = gitweb_check_feature('blame');
2484 if (!$have_blame) {
2485 die_error('403 Permission denied', "Permission denied");
2487 die_error('404 Not Found', "File name not defined") if (!$file_name);
2488 $hash_base ||= git_get_head_hash($project);
2489 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2490 my %co = parse_commit($hash_base)
2491 or die_error(undef, "Reading commit failed");
2492 if (!defined $hash) {
2493 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2494 or die_error(undef, "Error looking up file");
2496 $ftype = git_get_type($hash);
2497 if ($ftype !~ "blob") {
2498 die_error("400 Bad Request", "Object is not a blob");
2500 open ($fd, "-|", git_cmd(), "blame", '-l', '--', $file_name, $hash_base)
2501 or die_error(undef, "Open git-blame failed");
2502 git_header_html();
2503 my $formats_nav =
2504 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2505 "blob") .
2506 " | " .
2507 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2508 "history") .
2509 " | " .
2510 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2511 "HEAD");
2512 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2513 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2514 git_print_page_path($file_name, $ftype, $hash_base);
2515 my @rev_color = (qw(light2 dark2));
2516 my $num_colors = scalar(@rev_color);
2517 my $current_color = 0;
2518 my $last_rev;
2519 print <<HTML;
2520 <div class="page_body">
2521 <table class="blame">
2522 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2523 HTML
2524 while (<$fd>) {
2525 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2526 my $full_rev = $1;
2527 my $rev = substr($full_rev, 0, 8);
2528 my $lineno = $2;
2529 my $data = $3;
2531 if (!defined $last_rev) {
2532 $last_rev = $full_rev;
2533 } elsif ($last_rev ne $full_rev) {
2534 $last_rev = $full_rev;
2535 $current_color = ++$current_color % $num_colors;
2537 print "<tr class=\"$rev_color[$current_color]\">\n";
2538 print "<td class=\"sha1\">" .
2539 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2540 esc_html($rev)) . "</td>\n";
2541 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2542 esc_html($lineno) . "</a></td>\n";
2543 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2544 print "</tr>\n";
2546 print "</table>\n";
2547 print "</div>";
2548 close $fd
2549 or print "Reading blob failed\n";
2550 git_footer_html();
2553 sub git_blame {
2554 my $fd;
2556 my ($have_blame) = gitweb_check_feature('blame');
2557 if (!$have_blame) {
2558 die_error('403 Permission denied', "Permission denied");
2560 die_error('404 Not Found', "File name not defined") if (!$file_name);
2561 $hash_base ||= git_get_head_hash($project);
2562 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2563 my %co = parse_commit($hash_base)
2564 or die_error(undef, "Reading commit failed");
2565 if (!defined $hash) {
2566 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2567 or die_error(undef, "Error lookup file");
2569 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2570 or die_error(undef, "Open git-annotate failed");
2571 git_header_html();
2572 my $formats_nav =
2573 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2574 "blob") .
2575 " | " .
2576 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2577 "history") .
2578 " | " .
2579 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2580 "HEAD");
2581 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2582 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2583 git_print_page_path($file_name, 'blob', $hash_base);
2584 print "<div class=\"page_body\">\n";
2585 print <<HTML;
2586 <table class="blame">
2587 <tr>
2588 <th>Commit</th>
2589 <th>Age</th>
2590 <th>Author</th>
2591 <th>Line</th>
2592 <th>Data</th>
2593 </tr>
2594 HTML
2595 my @line_class = (qw(light dark));
2596 my $line_class_len = scalar (@line_class);
2597 my $line_class_num = $#line_class;
2598 while (my $line = <$fd>) {
2599 my $long_rev;
2600 my $short_rev;
2601 my $author;
2602 my $time;
2603 my $lineno;
2604 my $data;
2605 my $age;
2606 my $age_str;
2607 my $age_class;
2609 chomp $line;
2610 $line_class_num = ($line_class_num + 1) % $line_class_len;
2612 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2613 $long_rev = $1;
2614 $author = $2;
2615 $time = $3;
2616 $lineno = $4;
2617 $data = $5;
2618 } else {
2619 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2620 next;
2622 $short_rev = substr ($long_rev, 0, 8);
2623 $age = time () - $time;
2624 $age_str = age_string ($age);
2625 $age_str =~ s/ /&nbsp;/g;
2626 $age_class = age_class($age);
2627 $author = esc_html ($author);
2628 $author =~ s/ /&nbsp;/g;
2630 $data = untabify($data);
2631 $data = esc_html ($data);
2633 print <<HTML;
2634 <tr class="$line_class[$line_class_num]">
2635 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2636 <td class="$age_class">$age_str</td>
2637 <td>$author</td>
2638 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2639 <td class="pre">$data</td>
2640 </tr>
2641 HTML
2642 } # while (my $line = <$fd>)
2643 print "</table>\n\n";
2644 close $fd
2645 or print "Reading blob failed.\n";
2646 print "</div>";
2647 git_footer_html();
2650 sub git_tags {
2651 my $head = git_get_head_hash($project);
2652 git_header_html();
2653 git_print_page_nav('','', $head,undef,$head);
2654 git_print_header_div('summary', $project);
2656 my ($taglist) = git_get_refs_list("tags");
2657 if (@$taglist) {
2658 git_tags_body($taglist);
2660 git_footer_html();
2663 sub git_heads {
2664 my $head = git_get_head_hash($project);
2665 git_header_html();
2666 git_print_page_nav('','', $head,undef,$head);
2667 git_print_header_div('summary', $project);
2669 my ($headlist) = git_get_refs_list("heads");
2670 if (@$headlist) {
2671 git_heads_body($headlist, $head);
2673 git_footer_html();
2676 sub git_blob_plain {
2677 my $expires;
2679 if (!defined $hash) {
2680 if (defined $file_name) {
2681 my $base = $hash_base || git_get_head_hash($project);
2682 $hash = git_get_hash_by_path($base, $file_name, "blob")
2683 or die_error(undef, "Error lookup file");
2684 } else {
2685 die_error(undef, "No file name defined");
2687 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2688 # blobs defined by non-textual hash id's can be cached
2689 $expires = "+1d";
2692 my $type = shift;
2693 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2694 or die_error(undef, "Couldn't cat $file_name, $hash");
2696 $type ||= blob_mimetype($fd, $file_name);
2698 # save as filename, even when no $file_name is given
2699 my $save_as = "$hash";
2700 if (defined $file_name) {
2701 $save_as = $file_name;
2702 } elsif ($type =~ m/^text\//) {
2703 $save_as .= '.txt';
2706 print $cgi->header(
2707 -type => "$type",
2708 -expires=>$expires,
2709 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2710 undef $/;
2711 binmode STDOUT, ':raw';
2712 print <$fd>;
2713 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2714 $/ = "\n";
2715 close $fd;
2718 sub git_blob {
2719 my $expires;
2721 if (!defined $hash) {
2722 if (defined $file_name) {
2723 my $base = $hash_base || git_get_head_hash($project);
2724 $hash = git_get_hash_by_path($base, $file_name, "blob")
2725 or die_error(undef, "Error lookup file");
2726 } else {
2727 die_error(undef, "No file name defined");
2729 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2730 # blobs defined by non-textual hash id's can be cached
2731 $expires = "+1d";
2734 my ($have_blame) = gitweb_check_feature('blame');
2735 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2736 or die_error(undef, "Couldn't cat $file_name, $hash");
2737 my $mimetype = blob_mimetype($fd, $file_name);
2738 if ($mimetype !~ m/^text\//) {
2739 close $fd;
2740 return git_blob_plain($mimetype);
2742 git_header_html(undef, $expires);
2743 my $formats_nav = '';
2744 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2745 if (defined $file_name) {
2746 if ($have_blame) {
2747 $formats_nav .=
2748 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2749 hash=>$hash, file_name=>$file_name)},
2750 "blame") .
2751 " | ";
2753 $formats_nav .=
2754 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2755 hash=>$hash, file_name=>$file_name)},
2756 "history") .
2757 " | " .
2758 $cgi->a({-href => href(action=>"blob_plain",
2759 hash=>$hash, file_name=>$file_name)},
2760 "raw") .
2761 " | " .
2762 $cgi->a({-href => href(action=>"blob",
2763 hash_base=>"HEAD", file_name=>$file_name)},
2764 "HEAD");
2765 } else {
2766 $formats_nav .=
2767 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2769 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2770 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2771 } else {
2772 print "<div class=\"page_nav\">\n" .
2773 "<br/><br/></div>\n" .
2774 "<div class=\"title\">$hash</div>\n";
2776 git_print_page_path($file_name, "blob", $hash_base);
2777 print "<div class=\"page_body\">\n";
2778 my $nr;
2779 while (my $line = <$fd>) {
2780 chomp $line;
2781 $nr++;
2782 $line = untabify($line);
2783 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2784 $nr, $nr, $nr, esc_html($line);
2786 close $fd
2787 or print "Reading blob failed.\n";
2788 print "</div>";
2789 git_footer_html();
2792 sub git_tree {
2793 my $have_snapshot = gitweb_have_snapshot();
2795 if (!defined $hash_base) {
2796 $hash_base = "HEAD";
2798 if (!defined $hash) {
2799 if (defined $file_name) {
2800 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2801 } else {
2802 $hash = $hash_base;
2805 $/ = "\0";
2806 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2807 or die_error(undef, "Open git-ls-tree failed");
2808 my @entries = map { chomp; $_ } <$fd>;
2809 close $fd or die_error(undef, "Reading tree failed");
2810 $/ = "\n";
2812 my $refs = git_get_references();
2813 my $ref = format_ref_marker($refs, $hash_base);
2814 git_header_html();
2815 my $base = "";
2816 my ($have_blame) = gitweb_check_feature('blame');
2817 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2818 my @views_nav = ();
2819 if (defined $file_name) {
2820 push @views_nav,
2821 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2822 hash=>$hash, file_name=>$file_name)},
2823 "history"),
2824 $cgi->a({-href => href(action=>"tree",
2825 hash_base=>"HEAD", file_name=>$file_name)},
2826 "HEAD"),
2828 if ($have_snapshot) {
2829 # FIXME: Should be available when we have no hash base as well.
2830 push @views_nav,
2831 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2832 "snapshot");
2834 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2835 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2836 } else {
2837 undef $hash_base;
2838 print "<div class=\"page_nav\">\n";
2839 print "<br/><br/></div>\n";
2840 print "<div class=\"title\">$hash</div>\n";
2842 if (defined $file_name) {
2843 $base = esc_html("$file_name/");
2845 git_print_page_path($file_name, 'tree', $hash_base);
2846 print "<div class=\"page_body\">\n";
2847 print "<table cellspacing=\"0\">\n";
2848 my $alternate = 1;
2849 foreach my $line (@entries) {
2850 my %t = parse_ls_tree_line($line, -z => 1);
2852 if ($alternate) {
2853 print "<tr class=\"dark\">\n";
2854 } else {
2855 print "<tr class=\"light\">\n";
2857 $alternate ^= 1;
2859 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2861 print "</tr>\n";
2863 print "</table>\n" .
2864 "</div>";
2865 git_footer_html();
2868 sub git_snapshot {
2869 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2870 my $have_snapshot = (defined $ctype && defined $suffix);
2871 if (!$have_snapshot) {
2872 die_error('403 Permission denied', "Permission denied");
2875 if (!defined $hash) {
2876 $hash = git_get_head_hash($project);
2879 my $filename = basename($project) . "-$hash.tar.$suffix";
2881 print $cgi->header(
2882 -type => 'application/x-tar',
2883 -content_encoding => $ctype,
2884 -content_disposition => 'inline; filename="' . "$filename" . '"',
2885 -status => '200 OK');
2887 my $git_command = git_cmd_str();
2888 open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2889 die_error(undef, "Execute git-tar-tree failed.");
2890 binmode STDOUT, ':raw';
2891 print <$fd>;
2892 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2893 close $fd;
2897 sub git_log {
2898 my $head = git_get_head_hash($project);
2899 if (!defined $hash) {
2900 $hash = $head;
2902 if (!defined $page) {
2903 $page = 0;
2905 my $refs = git_get_references();
2907 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2908 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2909 or die_error(undef, "Open git-rev-list failed");
2910 my @revlist = map { chomp; $_ } <$fd>;
2911 close $fd;
2913 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2915 git_header_html();
2916 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2918 if (!@revlist) {
2919 my %co = parse_commit($hash);
2921 git_print_header_div('summary', $project);
2922 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2924 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2925 my $commit = $revlist[$i];
2926 my $ref = format_ref_marker($refs, $commit);
2927 my %co = parse_commit($commit);
2928 next if !%co;
2929 my %ad = parse_date($co{'author_epoch'});
2930 git_print_header_div('commit',
2931 "<span class=\"age\">$co{'age_string'}</span>" .
2932 esc_html($co{'title'}) . $ref,
2933 $commit);
2934 print "<div class=\"title_text\">\n" .
2935 "<div class=\"log_link\">\n" .
2936 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2937 " | " .
2938 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2939 " | " .
2940 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
2941 "<br/>\n" .
2942 "</div>\n" .
2943 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2944 "</div>\n";
2946 print "<div class=\"log_body\">\n";
2947 git_print_simplified_log($co{'comment'});
2948 print "</div>\n";
2950 git_footer_html();
2953 sub git_commit {
2954 my %co = parse_commit($hash);
2955 if (!%co) {
2956 die_error(undef, "Unknown commit object");
2958 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2959 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2961 my $parent = $co{'parent'};
2962 if (!defined $parent) {
2963 $parent = "--root";
2965 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2966 or die_error(undef, "Open git-diff-tree failed");
2967 my @difftree = map { chomp; $_ } <$fd>;
2968 close $fd or die_error(undef, "Reading git-diff-tree failed");
2970 # non-textual hash id's can be cached
2971 my $expires;
2972 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2973 $expires = "+1d";
2975 my $refs = git_get_references();
2976 my $ref = format_ref_marker($refs, $co{'id'});
2978 my $have_snapshot = gitweb_have_snapshot();
2980 my @views_nav = ();
2981 if (defined $file_name && defined $co{'parent'}) {
2982 push @views_nav,
2983 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2984 "blame");
2986 if (defined $co{'parent'}) {
2987 push @views_nav,
2988 $cgi->a({-href => href(action=>"shortlog", hash=>$hash)}, "shortlog"),
2989 $cgi->a({-href => href(action=>"log", hash=>$hash)}, "log");
2991 git_header_html(undef, $expires);
2992 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2993 $hash, $co{'tree'}, $hash,
2994 join (' | ', @views_nav));
2996 if (defined $co{'parent'}) {
2997 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2998 } else {
2999 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3001 print "<div class=\"title_text\">\n" .
3002 "<table cellspacing=\"0\">\n";
3003 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3004 "<tr>" .
3005 "<td></td><td> $ad{'rfc2822'}";
3006 if ($ad{'hour_local'} < 6) {
3007 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3008 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3009 } else {
3010 printf(" (%02d:%02d %s)",
3011 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3013 print "</td>" .
3014 "</tr>\n";
3015 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3016 print "<tr><td></td><td> $cd{'rfc2822'}" .
3017 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3018 "</td></tr>\n";
3019 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3020 print "<tr>" .
3021 "<td>tree</td>" .
3022 "<td class=\"sha1\">" .
3023 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3024 class => "list"}, $co{'tree'}) .
3025 "</td>" .
3026 "<td class=\"link\">" .
3027 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3028 "tree");
3029 if ($have_snapshot) {
3030 print " | " .
3031 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3033 print "</td>" .
3034 "</tr>\n";
3035 my $parents = $co{'parents'};
3036 foreach my $par (@$parents) {
3037 print "<tr>" .
3038 "<td>parent</td>" .
3039 "<td class=\"sha1\">" .
3040 $cgi->a({-href => href(action=>"commit", hash=>$par),
3041 class => "list"}, $par) .
3042 "</td>" .
3043 "<td class=\"link\">" .
3044 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3045 " | " .
3046 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3047 "</td>" .
3048 "</tr>\n";
3050 print "</table>".
3051 "</div>\n";
3053 print "<div class=\"page_body\">\n";
3054 git_print_log($co{'comment'});
3055 print "</div>\n";
3057 git_difftree_body(\@difftree, $hash, $parent);
3059 git_footer_html();
3062 sub git_blobdiff {
3063 my $format = shift || 'html';
3065 my $fd;
3066 my @difftree;
3067 my %diffinfo;
3068 my $expires;
3070 # preparing $fd and %diffinfo for git_patchset_body
3071 # new style URI
3072 if (defined $hash_base && defined $hash_parent_base) {
3073 if (defined $file_name) {
3074 # read raw output
3075 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3076 "--", $file_name
3077 or die_error(undef, "Open git-diff-tree failed");
3078 @difftree = map { chomp; $_ } <$fd>;
3079 close $fd
3080 or die_error(undef, "Reading git-diff-tree failed");
3081 @difftree
3082 or die_error('404 Not Found', "Blob diff not found");
3084 } elsif (defined $hash &&
3085 $hash =~ /[0-9a-fA-F]{40}/) {
3086 # try to find filename from $hash
3088 # read filtered raw output
3089 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3090 or die_error(undef, "Open git-diff-tree failed");
3091 @difftree =
3092 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3093 # $hash == to_id
3094 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3095 map { chomp; $_ } <$fd>;
3096 close $fd
3097 or die_error(undef, "Reading git-diff-tree failed");
3098 @difftree
3099 or die_error('404 Not Found', "Blob diff not found");
3101 } else {
3102 die_error('404 Not Found', "Missing one of the blob diff parameters");
3105 if (@difftree > 1) {
3106 die_error('404 Not Found', "Ambiguous blob diff specification");
3109 %diffinfo = parse_difftree_raw_line($difftree[0]);
3110 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3111 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3113 $hash_parent ||= $diffinfo{'from_id'};
3114 $hash ||= $diffinfo{'to_id'};
3116 # non-textual hash id's can be cached
3117 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3118 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3119 $expires = '+1d';
3122 # open patch output
3123 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3124 '-p', $hash_parent_base, $hash_base,
3125 "--", $file_name
3126 or die_error(undef, "Open git-diff-tree failed");
3129 # old/legacy style URI
3130 if (!%diffinfo && # if new style URI failed
3131 defined $hash && defined $hash_parent) {
3132 # fake git-diff-tree raw output
3133 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3134 $diffinfo{'from_id'} = $hash_parent;
3135 $diffinfo{'to_id'} = $hash;
3136 if (defined $file_name) {
3137 if (defined $file_parent) {
3138 $diffinfo{'status'} = '2';
3139 $diffinfo{'from_file'} = $file_parent;
3140 $diffinfo{'to_file'} = $file_name;
3141 } else { # assume not renamed
3142 $diffinfo{'status'} = '1';
3143 $diffinfo{'from_file'} = $file_name;
3144 $diffinfo{'to_file'} = $file_name;
3146 } else { # no filename given
3147 $diffinfo{'status'} = '2';
3148 $diffinfo{'from_file'} = $hash_parent;
3149 $diffinfo{'to_file'} = $hash;
3152 # non-textual hash id's can be cached
3153 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3154 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3155 $expires = '+1d';
3158 # open patch output
3159 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3160 or die_error(undef, "Open git-diff failed");
3161 } else {
3162 die_error('404 Not Found', "Missing one of the blob diff parameters")
3163 unless %diffinfo;
3166 # header
3167 if ($format eq 'html') {
3168 my $formats_nav =
3169 $cgi->a({-href => href(action=>"blobdiff_plain",
3170 hash=>$hash, hash_parent=>$hash_parent,
3171 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3172 file_name=>$file_name, file_parent=>$file_parent)},
3173 "raw");
3174 git_header_html(undef, $expires);
3175 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3176 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3177 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3178 } else {
3179 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3180 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3182 if (defined $file_name) {
3183 git_print_page_path($file_name, "blob", $hash_base);
3184 } else {
3185 print "<div class=\"page_path\"></div>\n";
3188 } elsif ($format eq 'plain') {
3189 print $cgi->header(
3190 -type => 'text/plain',
3191 -charset => 'utf-8',
3192 -expires => $expires,
3193 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3195 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3197 } else {
3198 die_error(undef, "Unknown blobdiff format");
3201 # patch
3202 if ($format eq 'html') {
3203 print "<div class=\"page_body\">\n";
3205 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3206 close $fd;
3208 print "</div>\n"; # class="page_body"
3209 git_footer_html();
3211 } else {
3212 while (my $line = <$fd>) {
3213 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3214 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3216 print $line;
3218 last if $line =~ m!^\+\+\+!;
3220 local $/ = undef;
3221 print <$fd>;
3222 close $fd;
3226 sub git_blobdiff_plain {
3227 git_blobdiff('plain');
3230 sub git_commitdiff {
3231 my $format = shift || 'html';
3232 my %co = parse_commit($hash);
3233 if (!%co) {
3234 die_error(undef, "Unknown commit object");
3236 if (!defined $hash_parent) {
3237 $hash_parent = $co{'parent'} || '--root';
3240 # read commitdiff
3241 my $fd;
3242 my @difftree;
3243 if ($format eq 'html') {
3244 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3245 "--patch-with-raw", "--full-index", $hash_parent, $hash
3246 or die_error(undef, "Open git-diff-tree failed");
3248 while (chomp(my $line = <$fd>)) {
3249 # empty line ends raw part of diff-tree output
3250 last unless $line;
3251 push @difftree, $line;
3254 } elsif ($format eq 'plain') {
3255 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3256 '-p', $hash_parent, $hash
3257 or die_error(undef, "Open git-diff-tree failed");
3259 } else {
3260 die_error(undef, "Unknown commitdiff format");
3263 # non-textual hash id's can be cached
3264 my $expires;
3265 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3266 $expires = "+1d";
3269 # write commit message
3270 if ($format eq 'html') {
3271 my $refs = git_get_references();
3272 my $ref = format_ref_marker($refs, $co{'id'});
3273 my $formats_nav =
3274 $cgi->a({-href => href(action=>"commitdiff_plain",
3275 hash=>$hash, hash_parent=>$hash_parent)},
3276 "raw");
3278 git_header_html(undef, $expires);
3279 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3280 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3281 git_print_authorship(\%co);
3282 print "<div class=\"page_body\">\n";
3283 print "<div class=\"log\">\n";
3284 git_print_simplified_log($co{'comment'}, 1); # skip title
3285 print "</div>\n"; # class="log"
3287 } elsif ($format eq 'plain') {
3288 my $refs = git_get_references("tags");
3289 my $tagname = git_get_rev_name_tags($hash);
3290 my $filename = basename($project) . "-$hash.patch";
3292 print $cgi->header(
3293 -type => 'text/plain',
3294 -charset => 'utf-8',
3295 -expires => $expires,
3296 -content_disposition => 'inline; filename="' . "$filename" . '"');
3297 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3298 print <<TEXT;
3299 From: $co{'author'}
3300 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3301 Subject: $co{'title'}
3302 TEXT
3303 print "X-Git-Tag: $tagname\n" if $tagname;
3304 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3306 foreach my $line (@{$co{'comment'}}) {
3307 print "$line\n";
3309 print "---\n\n";
3312 # write patch
3313 if ($format eq 'html') {
3314 git_difftree_body(\@difftree, $hash, $hash_parent);
3315 print "<br/>\n";
3317 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3318 close $fd;
3319 print "</div>\n"; # class="page_body"
3320 git_footer_html();
3322 } elsif ($format eq 'plain') {
3323 local $/ = undef;
3324 print <$fd>;
3325 close $fd
3326 or print "Reading git-diff-tree failed\n";
3330 sub git_commitdiff_plain {
3331 git_commitdiff('plain');
3334 sub git_history {
3335 if (!defined $hash_base) {
3336 $hash_base = git_get_head_hash($project);
3338 if (!defined $page) {
3339 $page = 0;
3341 my $ftype;
3342 my %co = parse_commit($hash_base);
3343 if (!%co) {
3344 die_error(undef, "Unknown commit object");
3347 my $refs = git_get_references();
3348 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3350 if (!defined $hash && defined $file_name) {
3351 $hash = git_get_hash_by_path($hash_base, $file_name);
3353 if (defined $hash) {
3354 $ftype = git_get_type($hash);
3357 open my $fd, "-|",
3358 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3359 or die_error(undef, "Open git-rev-list-failed");
3360 my @revlist = map { chomp; $_ } <$fd>;
3361 close $fd
3362 or die_error(undef, "Reading git-rev-list failed");
3364 my $paging_nav = '';
3365 if ($page > 0) {
3366 $paging_nav .=
3367 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3368 file_name=>$file_name)},
3369 "first");
3370 $paging_nav .= " &sdot; " .
3371 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3372 file_name=>$file_name, page=>$page-1),
3373 -accesskey => "p", -title => "Alt-p"}, "prev");
3374 } else {
3375 $paging_nav .= "first";
3376 $paging_nav .= " &sdot; prev";
3378 if ($#revlist >= (100 * ($page+1)-1)) {
3379 $paging_nav .= " &sdot; " .
3380 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3381 file_name=>$file_name, page=>$page+1),
3382 -accesskey => "n", -title => "Alt-n"}, "next");
3383 } else {
3384 $paging_nav .= " &sdot; next";
3386 my $next_link = '';
3387 if ($#revlist >= (100 * ($page+1)-1)) {
3388 $next_link =
3389 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3390 file_name=>$file_name, page=>$page+1),
3391 -title => "Alt-n"}, "next");
3394 git_header_html();
3395 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3396 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3397 git_print_page_path($file_name, $ftype, $hash_base);
3399 git_history_body(\@revlist, ($page * 100), $#revlist,
3400 $refs, $hash_base, $ftype, $next_link);
3402 git_footer_html();
3405 sub git_search {
3406 if (!defined $searchtext) {
3407 die_error(undef, "Text field empty");
3409 if (!defined $hash) {
3410 $hash = git_get_head_hash($project);
3412 my %co = parse_commit($hash);
3413 if (!%co) {
3414 die_error(undef, "Unknown commit object");
3417 my $commit_search = 1;
3418 my $author_search = 0;
3419 my $committer_search = 0;
3420 my $pickaxe_search = 0;
3421 if ($searchtext =~ s/^author\\://i) {
3422 $author_search = 1;
3423 } elsif ($searchtext =~ s/^committer\\://i) {
3424 $committer_search = 1;
3425 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3426 $commit_search = 0;
3427 $pickaxe_search = 1;
3429 # pickaxe may take all resources of your box and run for several minutes
3430 # with every query - so decide by yourself how public you make this feature
3431 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3432 if (!$have_pickaxe) {
3433 die_error('403 Permission denied', "Permission denied");
3436 git_header_html();
3437 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3438 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3440 print "<table cellspacing=\"0\">\n";
3441 my $alternate = 1;
3442 if ($commit_search) {
3443 $/ = "\0";
3444 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3445 while (my $commit_text = <$fd>) {
3446 if (!grep m/$searchtext/i, $commit_text) {
3447 next;
3449 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3450 next;
3452 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3453 next;
3455 my @commit_lines = split "\n", $commit_text;
3456 my %co = parse_commit(undef, \@commit_lines);
3457 if (!%co) {
3458 next;
3460 if ($alternate) {
3461 print "<tr class=\"dark\">\n";
3462 } else {
3463 print "<tr class=\"light\">\n";
3465 $alternate ^= 1;
3466 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3467 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3468 "<td>" .
3469 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3470 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3471 my $comment = $co{'comment'};
3472 foreach my $line (@$comment) {
3473 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3474 my $lead = esc_html($1) || "";
3475 $lead = chop_str($lead, 30, 10);
3476 my $match = esc_html($2) || "";
3477 my $trail = esc_html($3) || "";
3478 $trail = chop_str($trail, 30, 10);
3479 my $text = "$lead<span class=\"match\">$match</span>$trail";
3480 print chop_str($text, 80, 5) . "<br/>\n";
3483 print "</td>\n" .
3484 "<td class=\"link\">" .
3485 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3486 " | " .
3487 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3488 print "</td>\n" .
3489 "</tr>\n";
3491 close $fd;
3494 if ($pickaxe_search) {
3495 $/ = "\n";
3496 my $git_command = git_cmd_str();
3497 open my $fd, "-|", "$git_command rev-list $hash | " .
3498 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3499 undef %co;
3500 my @files;
3501 while (my $line = <$fd>) {
3502 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3503 my %set;
3504 $set{'file'} = $6;
3505 $set{'from_id'} = $3;
3506 $set{'to_id'} = $4;
3507 $set{'id'} = $set{'to_id'};
3508 if ($set{'id'} =~ m/0{40}/) {
3509 $set{'id'} = $set{'from_id'};
3511 if ($set{'id'} =~ m/0{40}/) {
3512 next;
3514 push @files, \%set;
3515 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3516 if (%co) {
3517 if ($alternate) {
3518 print "<tr class=\"dark\">\n";
3519 } else {
3520 print "<tr class=\"light\">\n";
3522 $alternate ^= 1;
3523 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3524 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3525 "<td>" .
3526 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3527 -class => "list subject"},
3528 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3529 while (my $setref = shift @files) {
3530 my %set = %$setref;
3531 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3532 hash=>$set{'id'}, file_name=>$set{'file'}),
3533 -class => "list"},
3534 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3535 "<br/>\n";
3537 print "</td>\n" .
3538 "<td class=\"link\">" .
3539 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3540 " | " .
3541 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3542 print "</td>\n" .
3543 "</tr>\n";
3545 %co = parse_commit($1);
3548 close $fd;
3550 print "</table>\n";
3551 git_footer_html();
3554 sub git_shortlog {
3555 my $head = git_get_head_hash($project);
3556 if (!defined $hash) {
3557 $hash = $head;
3559 if (!defined $page) {
3560 $page = 0;
3562 my $refs = git_get_references();
3564 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3565 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3566 or die_error(undef, "Open git-rev-list failed");
3567 my @revlist = map { chomp; $_ } <$fd>;
3568 close $fd;
3570 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3571 my $next_link = '';
3572 if ($#revlist >= (100 * ($page+1)-1)) {
3573 $next_link =
3574 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3575 -title => "Alt-n"}, "next");
3579 git_header_html();
3580 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3581 git_print_header_div('summary', $project);
3583 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3585 git_footer_html();
3588 ## ......................................................................
3589 ## feeds (RSS, OPML)
3591 sub git_rss {
3592 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3593 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3594 or die_error(undef, "Open git-rev-list failed");
3595 my @revlist = map { chomp; $_ } <$fd>;
3596 close $fd or die_error(undef, "Reading git-rev-list failed");
3597 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3598 print <<XML;
3599 <?xml version="1.0" encoding="utf-8"?>
3600 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3601 <channel>
3602 <title>$project $my_uri $my_url</title>
3603 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3604 <description>$project log</description>
3605 <language>en</language>
3608 for (my $i = 0; $i <= $#revlist; $i++) {
3609 my $commit = $revlist[$i];
3610 my %co = parse_commit($commit);
3611 # we read 150, we always show 30 and the ones more recent than 48 hours
3612 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3613 last;
3615 my %cd = parse_date($co{'committer_epoch'});
3616 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3617 $co{'parent'}, $co{'id'}
3618 or next;
3619 my @difftree = map { chomp; $_ } <$fd>;
3620 close $fd
3621 or next;
3622 print "<item>\n" .
3623 "<title>" .
3624 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3625 "</title>\n" .
3626 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3627 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3628 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3629 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3630 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3631 "<content:encoded>" .
3632 "<![CDATA[\n";
3633 my $comment = $co{'comment'};
3634 foreach my $line (@$comment) {
3635 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3636 print "$line<br/>\n";
3638 print "<br/>\n";
3639 foreach my $line (@difftree) {
3640 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3641 next;
3643 my $file = esc_html(unquote($7));
3644 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3645 print "$file<br/>\n";
3647 print "]]>\n" .
3648 "</content:encoded>\n" .
3649 "</item>\n";
3651 print "</channel></rss>";
3654 sub git_opml {
3655 my @list = git_get_projects_list();
3657 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3658 print <<XML;
3659 <?xml version="1.0" encoding="utf-8"?>
3660 <opml version="1.0">
3661 <head>
3662 <title>$site_name Git OPML Export</title>
3663 </head>
3664 <body>
3665 <outline text="git RSS feeds">
3668 foreach my $pr (@list) {
3669 my %proj = %$pr;
3670 my $head = git_get_head_hash($proj{'path'});
3671 if (!defined $head) {
3672 next;
3674 $git_dir = "$projectroot/$proj{'path'}";
3675 my %co = parse_commit($head);
3676 if (!%co) {
3677 next;
3680 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3681 my $rss = "$my_url?p=$proj{'path'};a=rss";
3682 my $html = "$my_url?p=$proj{'path'};a=summary";
3683 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3685 print <<XML;
3686 </outline>
3687 </body>
3688 </opml>