gitweb: use for-each-ref to show the latest activity across branches
[tgit.git] / gitweb / gitweb.perl
blobe119e334231318f2d9b5a0edb0d3ce71848b8739
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 # filename of html text to include at top of each page
45 our $site_header = "++GITWEB_SITE_HEADER++";
46 # html text to include at home page
47 our $home_text = "++GITWEB_HOMETEXT++";
48 # filename of html text to include at bottom of each page
49 our $site_footer = "++GITWEB_SITE_FOOTER++";
51 # URI of stylesheets
52 our @stylesheets = ("++GITWEB_CSS++");
53 our $stylesheet;
54 # default is not to define style sheet, but it can be overwritten later
55 undef $stylesheet;
57 # URI of GIT logo
58 our $logo = "++GITWEB_LOGO++";
59 # URI of GIT favicon, assumed to be image/png type
60 our $favicon = "++GITWEB_FAVICON++";
62 # source of projects list
63 our $projects_list = "++GITWEB_LIST++";
65 # show repository only if this file exists
66 # (only effective if this variable evaluates to true)
67 our $export_ok = "++GITWEB_EXPORT_OK++";
69 # only allow viewing of repositories also shown on the overview page
70 our $strict_export = "++GITWEB_STRICT_EXPORT++";
72 # list of git base URLs used for URL to where fetch project from,
73 # i.e. full URL is "$git_base_url/$project"
74 our @git_base_url_list = ("++GITWEB_BASE_URL++");
76 # default blob_plain mimetype and default charset for text/plain blob
77 our $default_blob_plain_mimetype = 'text/plain';
78 our $default_text_plain_charset = undef;
80 # file to use for guessing MIME types before trying /etc/mime.types
81 # (relative to the current git repository)
82 our $mimetypes_file = undef;
84 # You define site-wide feature defaults here; override them with
85 # $GITWEB_CONFIG as necessary.
86 our %feature = (
87 # feature => {
88 # 'sub' => feature-sub (subroutine),
89 # 'override' => allow-override (boolean),
90 # 'default' => [ default options...] (array reference)}
92 # if feature is overridable (it means that allow-override has true value,
93 # then feature-sub will be called with default options as parameters;
94 # return value of feature-sub indicates if to enable specified feature
96 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
98 'blame' => {
99 'sub' => \&feature_blame,
100 'override' => 0,
101 'default' => [0]},
103 'snapshot' => {
104 'sub' => \&feature_snapshot,
105 'override' => 0,
106 # => [content-encoding, suffix, program]
107 'default' => ['x-gzip', 'gz', 'gzip']},
109 'pickaxe' => {
110 'sub' => \&feature_pickaxe,
111 'override' => 0,
112 'default' => [1]},
115 sub gitweb_check_feature {
116 my ($name) = @_;
117 return unless exists $feature{$name};
118 my ($sub, $override, @defaults) = (
119 $feature{$name}{'sub'},
120 $feature{$name}{'override'},
121 @{$feature{$name}{'default'}});
122 if (!$override) { return @defaults; }
123 return $sub->(@defaults);
126 # To enable system wide have in $GITWEB_CONFIG
127 # $feature{'blame'}{'default'} = [1];
128 # To have project specific config enable override in $GITWEB_CONFIG
129 # $feature{'blame'}{'override'} = 1;
130 # and in project config gitweb.blame = 0|1;
132 sub feature_blame {
133 my ($val) = git_get_project_config('blame', '--bool');
135 if ($val eq 'true') {
136 return 1;
137 } elsif ($val eq 'false') {
138 return 0;
141 return $_[0];
144 # To disable system wide have in $GITWEB_CONFIG
145 # $feature{'snapshot'}{'default'} = [undef];
146 # To have project specific config enable override in $GITWEB_CONFIG
147 # $feature{'blame'}{'override'} = 1;
148 # and in project config gitweb.snapshot = none|gzip|bzip2
150 sub feature_snapshot {
151 my ($ctype, $suffix, $command) = @_;
153 my ($val) = git_get_project_config('snapshot');
155 if ($val eq 'gzip') {
156 return ('x-gzip', 'gz', 'gzip');
157 } elsif ($val eq 'bzip2') {
158 return ('x-bzip2', 'bz2', 'bzip2');
159 } elsif ($val eq 'none') {
160 return ();
163 return ($ctype, $suffix, $command);
166 sub gitweb_have_snapshot {
167 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
168 my $have_snapshot = (defined $ctype && defined $suffix);
170 return $have_snapshot;
173 # To enable system wide have in $GITWEB_CONFIG
174 # $feature{'pickaxe'}{'default'} = [1];
175 # To have project specific config enable override in $GITWEB_CONFIG
176 # $feature{'pickaxe'}{'override'} = 1;
177 # and in project config gitweb.pickaxe = 0|1;
179 sub feature_pickaxe {
180 my ($val) = git_get_project_config('pickaxe', '--bool');
182 if ($val eq 'true') {
183 return (1);
184 } elsif ($val eq 'false') {
185 return (0);
188 return ($_[0]);
191 # checking HEAD file with -e is fragile if the repository was
192 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
193 # and then pruned.
194 sub check_head_link {
195 my ($dir) = @_;
196 my $headfile = "$dir/HEAD";
197 return ((-e $headfile) ||
198 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
201 sub check_export_ok {
202 my ($dir) = @_;
203 return (check_head_link($dir) &&
204 (!$export_ok || -e "$dir/$export_ok"));
207 # rename detection options for git-diff and git-diff-tree
208 # - default is '-M', with the cost proportional to
209 # (number of removed files) * (number of new files).
210 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
211 # (number of changed files + number of removed files) * (number of new files)
212 # - even more costly is '-C', '--find-copies-harder' with cost
213 # (number of files in the original tree) * (number of new files)
214 # - one might want to include '-B' option, e.g. '-B', '-M'
215 our @diff_opts = ('-M'); # taken from git_commit
217 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
218 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
220 # version of the core git binary
221 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
223 $projects_list ||= $projectroot;
225 # ======================================================================
226 # input validation and dispatch
227 our $action = $cgi->param('a');
228 if (defined $action) {
229 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
230 die_error(undef, "Invalid action parameter");
234 # parameters which are pathnames
235 our $project = $cgi->param('p');
236 if (defined $project) {
237 if (!validate_pathname($project) ||
238 !(-d "$projectroot/$project") ||
239 !check_head_link("$projectroot/$project") ||
240 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
241 ($strict_export && !project_in_list($project))) {
242 undef $project;
243 die_error(undef, "No such project");
247 our $file_name = $cgi->param('f');
248 if (defined $file_name) {
249 if (!validate_pathname($file_name)) {
250 die_error(undef, "Invalid file parameter");
254 our $file_parent = $cgi->param('fp');
255 if (defined $file_parent) {
256 if (!validate_pathname($file_parent)) {
257 die_error(undef, "Invalid file parent parameter");
261 # parameters which are refnames
262 our $hash = $cgi->param('h');
263 if (defined $hash) {
264 if (!validate_refname($hash)) {
265 die_error(undef, "Invalid hash parameter");
269 our $hash_parent = $cgi->param('hp');
270 if (defined $hash_parent) {
271 if (!validate_refname($hash_parent)) {
272 die_error(undef, "Invalid hash parent parameter");
276 our $hash_base = $cgi->param('hb');
277 if (defined $hash_base) {
278 if (!validate_refname($hash_base)) {
279 die_error(undef, "Invalid hash base parameter");
283 our $hash_parent_base = $cgi->param('hpb');
284 if (defined $hash_parent_base) {
285 if (!validate_refname($hash_parent_base)) {
286 die_error(undef, "Invalid hash parent base parameter");
290 # other parameters
291 our $page = $cgi->param('pg');
292 if (defined $page) {
293 if ($page =~ m/[^0-9]/) {
294 die_error(undef, "Invalid page parameter");
298 our $searchtext = $cgi->param('s');
299 if (defined $searchtext) {
300 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
301 die_error(undef, "Invalid search parameter");
303 $searchtext = quotemeta $searchtext;
306 # now read PATH_INFO and use it as alternative to parameters
307 sub evaluate_path_info {
308 return if defined $project;
309 my $path_info = $ENV{"PATH_INFO"};
310 return if !$path_info;
311 $path_info =~ s,^/+,,;
312 return if !$path_info;
313 # find which part of PATH_INFO is project
314 $project = $path_info;
315 $project =~ s,/+$,,;
316 while ($project && !check_head_link("$projectroot/$project")) {
317 $project =~ s,/*[^/]*$,,;
319 # validate project
320 $project = validate_pathname($project);
321 if (!$project ||
322 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
323 ($strict_export && !project_in_list($project))) {
324 undef $project;
325 return;
327 # do not change any parameters if an action is given using the query string
328 return if $action;
329 $path_info =~ s,^$project/*,,;
330 my ($refname, $pathname) = split(/:/, $path_info, 2);
331 if (defined $pathname) {
332 # we got "project.git/branch:filename" or "project.git/branch:dir/"
333 # we could use git_get_type(branch:pathname), but it needs $git_dir
334 $pathname =~ s,^/+,,;
335 if (!$pathname || substr($pathname, -1) eq "/") {
336 $action ||= "tree";
337 $pathname =~ s,/$,,;
338 } else {
339 $action ||= "blob_plain";
341 $hash_base ||= validate_refname($refname);
342 $file_name ||= validate_pathname($pathname);
343 } elsif (defined $refname) {
344 # we got "project.git/branch"
345 $action ||= "shortlog";
346 $hash ||= validate_refname($refname);
349 evaluate_path_info();
351 # path to the current git repository
352 our $git_dir;
353 $git_dir = "$projectroot/$project" if $project;
355 # dispatch
356 my %actions = (
357 "blame" => \&git_blame2,
358 "blobdiff" => \&git_blobdiff,
359 "blobdiff_plain" => \&git_blobdiff_plain,
360 "blob" => \&git_blob,
361 "blob_plain" => \&git_blob_plain,
362 "commitdiff" => \&git_commitdiff,
363 "commitdiff_plain" => \&git_commitdiff_plain,
364 "commit" => \&git_commit,
365 "heads" => \&git_heads,
366 "history" => \&git_history,
367 "log" => \&git_log,
368 "rss" => \&git_rss,
369 "search" => \&git_search,
370 "shortlog" => \&git_shortlog,
371 "summary" => \&git_summary,
372 "tag" => \&git_tag,
373 "tags" => \&git_tags,
374 "tree" => \&git_tree,
375 "snapshot" => \&git_snapshot,
376 # those below don't need $project
377 "opml" => \&git_opml,
378 "project_list" => \&git_project_list,
379 "project_index" => \&git_project_index,
382 if (defined $project) {
383 $action ||= 'summary';
384 } else {
385 $action ||= 'project_list';
387 if (!defined($actions{$action})) {
388 die_error(undef, "Unknown action");
390 if ($action !~ m/^(opml|project_list|project_index)$/ &&
391 !$project) {
392 die_error(undef, "Project needed");
394 $actions{$action}->();
395 exit;
397 ## ======================================================================
398 ## action links
400 sub href(%) {
401 my %params = @_;
403 my @mapping = (
404 project => "p",
405 action => "a",
406 file_name => "f",
407 file_parent => "fp",
408 hash => "h",
409 hash_parent => "hp",
410 hash_base => "hb",
411 hash_parent_base => "hpb",
412 page => "pg",
413 order => "o",
414 searchtext => "s",
416 my %mapping = @mapping;
418 $params{'project'} = $project unless exists $params{'project'};
420 my @result = ();
421 for (my $i = 0; $i < @mapping; $i += 2) {
422 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
423 if (defined $params{$name}) {
424 push @result, $symbol . "=" . esc_param($params{$name});
427 return "$my_uri?" . join(';', @result);
431 ## ======================================================================
432 ## validation, quoting/unquoting and escaping
434 sub validate_pathname {
435 my $input = shift || return undef;
437 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
438 # at the beginning, at the end, and between slashes.
439 # also this catches doubled slashes
440 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
441 return undef;
443 # no null characters
444 if ($input =~ m!\0!) {
445 return undef;
447 return $input;
450 sub validate_refname {
451 my $input = shift || return undef;
453 # textual hashes are O.K.
454 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
455 return $input;
457 # it must be correct pathname
458 $input = validate_pathname($input)
459 or return undef;
460 # restrictions on ref name according to git-check-ref-format
461 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
462 return undef;
464 return $input;
467 # quote unsafe chars, but keep the slash, even when it's not
468 # correct, but quoted slashes look too horrible in bookmarks
469 sub esc_param {
470 my $str = shift;
471 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
472 $str =~ s/\+/%2B/g;
473 $str =~ s/ /\+/g;
474 return $str;
477 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
478 sub esc_url {
479 my $str = shift;
480 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
481 $str =~ s/\+/%2B/g;
482 $str =~ s/ /\+/g;
483 return $str;
486 # replace invalid utf8 character with SUBSTITUTION sequence
487 sub esc_html {
488 my $str = shift;
489 $str = decode("utf8", $str, Encode::FB_DEFAULT);
490 $str = escapeHTML($str);
491 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
492 $str =~ s/\033/^[/g; # "escape" ESCAPE (\e) character (e.g. commit 20a3847d8a5032ce41f90dcc68abfb36e6fee9b1)
493 return $str;
496 # git may return quoted and escaped filenames
497 sub unquote {
498 my $str = shift;
499 if ($str =~ m/^"(.*)"$/) {
500 $str = $1;
501 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
503 return $str;
506 # escape tabs (convert tabs to spaces)
507 sub untabify {
508 my $line = shift;
510 while ((my $pos = index($line, "\t")) != -1) {
511 if (my $count = (8 - ($pos % 8))) {
512 my $spaces = ' ' x $count;
513 $line =~ s/\t/$spaces/;
517 return $line;
520 sub project_in_list {
521 my $project = shift;
522 my @list = git_get_projects_list();
523 return @list && scalar(grep { $_->{'path'} eq $project } @list);
526 ## ----------------------------------------------------------------------
527 ## HTML aware string manipulation
529 sub chop_str {
530 my $str = shift;
531 my $len = shift;
532 my $add_len = shift || 10;
534 # allow only $len chars, but don't cut a word if it would fit in $add_len
535 # if it doesn't fit, cut it if it's still longer than the dots we would add
536 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
537 my $body = $1;
538 my $tail = $2;
539 if (length($tail) > 4) {
540 $tail = " ...";
541 $body =~ s/&[^;]*$//; # remove chopped character entities
543 return "$body$tail";
546 ## ----------------------------------------------------------------------
547 ## functions returning short strings
549 # CSS class for given age value (in seconds)
550 sub age_class {
551 my $age = shift;
553 if ($age < 60*60*2) {
554 return "age0";
555 } elsif ($age < 60*60*24*2) {
556 return "age1";
557 } else {
558 return "age2";
562 # convert age in seconds to "nn units ago" string
563 sub age_string {
564 my $age = shift;
565 my $age_str;
567 if ($age > 60*60*24*365*2) {
568 $age_str = (int $age/60/60/24/365);
569 $age_str .= " years ago";
570 } elsif ($age > 60*60*24*(365/12)*2) {
571 $age_str = int $age/60/60/24/(365/12);
572 $age_str .= " months ago";
573 } elsif ($age > 60*60*24*7*2) {
574 $age_str = int $age/60/60/24/7;
575 $age_str .= " weeks ago";
576 } elsif ($age > 60*60*24*2) {
577 $age_str = int $age/60/60/24;
578 $age_str .= " days ago";
579 } elsif ($age > 60*60*2) {
580 $age_str = int $age/60/60;
581 $age_str .= " hours ago";
582 } elsif ($age > 60*2) {
583 $age_str = int $age/60;
584 $age_str .= " min ago";
585 } elsif ($age > 2) {
586 $age_str = int $age;
587 $age_str .= " sec ago";
588 } else {
589 $age_str .= " right now";
591 return $age_str;
594 # convert file mode in octal to symbolic file mode string
595 sub mode_str {
596 my $mode = oct shift;
598 if (S_ISDIR($mode & S_IFMT)) {
599 return 'drwxr-xr-x';
600 } elsif (S_ISLNK($mode)) {
601 return 'lrwxrwxrwx';
602 } elsif (S_ISREG($mode)) {
603 # git cares only about the executable bit
604 if ($mode & S_IXUSR) {
605 return '-rwxr-xr-x';
606 } else {
607 return '-rw-r--r--';
609 } else {
610 return '----------';
614 # convert file mode in octal to file type string
615 sub file_type {
616 my $mode = shift;
618 if ($mode !~ m/^[0-7]+$/) {
619 return $mode;
620 } else {
621 $mode = oct $mode;
624 if (S_ISDIR($mode & S_IFMT)) {
625 return "directory";
626 } elsif (S_ISLNK($mode)) {
627 return "symlink";
628 } elsif (S_ISREG($mode)) {
629 return "file";
630 } else {
631 return "unknown";
635 ## ----------------------------------------------------------------------
636 ## functions returning short HTML fragments, or transforming HTML fragments
637 ## which don't beling to other sections
639 # format line of commit message or tag comment
640 sub format_log_line_html {
641 my $line = shift;
643 $line = esc_html($line);
644 $line =~ s/ /&nbsp;/g;
645 if ($line =~ m/([0-9a-fA-F]{40})/) {
646 my $hash_text = $1;
647 if (git_get_type($hash_text) eq "commit") {
648 my $link =
649 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
650 -class => "text"}, $hash_text);
651 $line =~ s/$hash_text/$link/;
654 return $line;
657 # format marker of refs pointing to given object
658 sub format_ref_marker {
659 my ($refs, $id) = @_;
660 my $markers = '';
662 if (defined $refs->{$id}) {
663 foreach my $ref (@{$refs->{$id}}) {
664 my ($type, $name) = qw();
665 # e.g. tags/v2.6.11 or heads/next
666 if ($ref =~ m!^(.*?)s?/(.*)$!) {
667 $type = $1;
668 $name = $2;
669 } else {
670 $type = "ref";
671 $name = $ref;
674 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
678 if ($markers) {
679 return ' <span class="refs">'. $markers . '</span>';
680 } else {
681 return "";
685 # format, perhaps shortened and with markers, title line
686 sub format_subject_html {
687 my ($long, $short, $href, $extra) = @_;
688 $extra = '' unless defined($extra);
690 if (length($short) < length($long)) {
691 return $cgi->a({-href => $href, -class => "list subject",
692 -title => decode("utf8", $long, Encode::FB_DEFAULT)},
693 esc_html($short) . $extra);
694 } else {
695 return $cgi->a({-href => $href, -class => "list subject"},
696 esc_html($long) . $extra);
700 sub format_diff_line {
701 my $line = shift;
702 my $char = substr($line, 0, 1);
703 my $diff_class = "";
705 chomp $line;
707 if ($char eq '+') {
708 $diff_class = " add";
709 } elsif ($char eq "-") {
710 $diff_class = " rem";
711 } elsif ($char eq "@") {
712 $diff_class = " chunk_header";
713 } elsif ($char eq "\\") {
714 $diff_class = " incomplete";
716 $line = untabify($line);
717 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
720 ## ----------------------------------------------------------------------
721 ## git utility subroutines, invoking git commands
723 # returns path to the core git executable and the --git-dir parameter as list
724 sub git_cmd {
725 return $GIT, '--git-dir='.$git_dir;
728 # returns path to the core git executable and the --git-dir parameter as string
729 sub git_cmd_str {
730 return join(' ', git_cmd());
733 # get HEAD ref of given project as hash
734 sub git_get_head_hash {
735 my $project = shift;
736 my $o_git_dir = $git_dir;
737 my $retval = undef;
738 $git_dir = "$projectroot/$project";
739 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
740 my $head = <$fd>;
741 close $fd;
742 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
743 $retval = $1;
746 if (defined $o_git_dir) {
747 $git_dir = $o_git_dir;
749 return $retval;
752 # get type of given object
753 sub git_get_type {
754 my $hash = shift;
756 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
757 my $type = <$fd>;
758 close $fd or return;
759 chomp $type;
760 return $type;
763 sub git_get_project_config {
764 my ($key, $type) = @_;
766 return unless ($key);
767 $key =~ s/^gitweb\.//;
768 return if ($key =~ m/\W/);
770 my @x = (git_cmd(), 'repo-config');
771 if (defined $type) { push @x, $type; }
772 push @x, "--get";
773 push @x, "gitweb.$key";
774 my $val = qx(@x);
775 chomp $val;
776 return ($val);
779 # get hash of given path at given ref
780 sub git_get_hash_by_path {
781 my $base = shift;
782 my $path = shift || return undef;
783 my $type = shift;
785 $path =~ s,/+$,,;
787 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
788 or die_error(undef, "Open git-ls-tree failed");
789 my $line = <$fd>;
790 close $fd or return undef;
792 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
793 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
794 if (defined $type && $type ne $2) {
795 # type doesn't match
796 return undef;
798 return $3;
801 ## ......................................................................
802 ## git utility functions, directly accessing git repository
804 sub git_get_project_description {
805 my $path = shift;
807 open my $fd, "$projectroot/$path/description" or return undef;
808 my $descr = <$fd>;
809 close $fd;
810 chomp $descr;
811 return $descr;
814 sub git_get_project_url_list {
815 my $path = shift;
817 open my $fd, "$projectroot/$path/cloneurl" or return;
818 my @git_project_url_list = map { chomp; $_ } <$fd>;
819 close $fd;
821 return wantarray ? @git_project_url_list : \@git_project_url_list;
824 sub git_get_projects_list {
825 my @list;
827 if (-d $projects_list) {
828 # search in directory
829 my $dir = $projects_list;
830 my $pfxlen = length("$dir");
832 File::Find::find({
833 follow_fast => 1, # follow symbolic links
834 dangling_symlinks => 0, # ignore dangling symlinks, silently
835 wanted => sub {
836 # skip project-list toplevel, if we get it.
837 return if (m!^[/.]$!);
838 # only directories can be git repositories
839 return unless (-d $_);
841 my $subdir = substr($File::Find::name, $pfxlen + 1);
842 # we check related file in $projectroot
843 if (check_export_ok("$projectroot/$subdir")) {
844 push @list, { path => $subdir };
845 $File::Find::prune = 1;
848 }, "$dir");
850 } elsif (-f $projects_list) {
851 # read from file(url-encoded):
852 # 'git%2Fgit.git Linus+Torvalds'
853 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
854 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
855 open my ($fd), $projects_list or return;
856 while (my $line = <$fd>) {
857 chomp $line;
858 my ($path, $owner) = split ' ', $line;
859 $path = unescape($path);
860 $owner = unescape($owner);
861 if (!defined $path) {
862 next;
864 if (check_export_ok("$projectroot/$path")) {
865 my $pr = {
866 path => $path,
867 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
869 push @list, $pr
872 close $fd;
874 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
875 return @list;
878 sub git_get_project_owner {
879 my $project = shift;
880 my $owner;
882 return undef unless $project;
884 # read from file (url-encoded):
885 # 'git%2Fgit.git Linus+Torvalds'
886 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
887 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
888 if (-f $projects_list) {
889 open (my $fd , $projects_list);
890 while (my $line = <$fd>) {
891 chomp $line;
892 my ($pr, $ow) = split ' ', $line;
893 $pr = unescape($pr);
894 $ow = unescape($ow);
895 if ($pr eq $project) {
896 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
897 last;
900 close $fd;
902 if (!defined $owner) {
903 $owner = get_file_owner("$projectroot/$project");
906 return $owner;
909 sub git_get_references {
910 my $type = shift || "";
911 my %refs;
912 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
913 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
914 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
915 or return;
917 while (my $line = <$fd>) {
918 chomp $line;
919 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
920 if (defined $refs{$1}) {
921 push @{$refs{$1}}, $2;
922 } else {
923 $refs{$1} = [ $2 ];
927 close $fd or return;
928 return \%refs;
931 sub git_get_rev_name_tags {
932 my $hash = shift || return undef;
934 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
935 or return;
936 my $name_rev = <$fd>;
937 close $fd;
939 if ($name_rev =~ m|^$hash tags/(.*)$|) {
940 return $1;
941 } else {
942 # catches also '$hash undefined' output
943 return undef;
947 ## ----------------------------------------------------------------------
948 ## parse to hash functions
950 sub parse_date {
951 my $epoch = shift;
952 my $tz = shift || "-0000";
954 my %date;
955 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
956 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
957 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
958 $date{'hour'} = $hour;
959 $date{'minute'} = $min;
960 $date{'mday'} = $mday;
961 $date{'day'} = $days[$wday];
962 $date{'month'} = $months[$mon];
963 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
964 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
965 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
966 $mday, $months[$mon], $hour ,$min;
968 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
969 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
970 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
971 $date{'hour_local'} = $hour;
972 $date{'minute_local'} = $min;
973 $date{'tz_local'} = $tz;
974 return %date;
977 sub parse_tag {
978 my $tag_id = shift;
979 my %tag;
980 my @comment;
982 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
983 $tag{'id'} = $tag_id;
984 while (my $line = <$fd>) {
985 chomp $line;
986 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
987 $tag{'object'} = $1;
988 } elsif ($line =~ m/^type (.+)$/) {
989 $tag{'type'} = $1;
990 } elsif ($line =~ m/^tag (.+)$/) {
991 $tag{'name'} = $1;
992 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
993 $tag{'author'} = $1;
994 $tag{'epoch'} = $2;
995 $tag{'tz'} = $3;
996 } elsif ($line =~ m/--BEGIN/) {
997 push @comment, $line;
998 last;
999 } elsif ($line eq "") {
1000 last;
1003 push @comment, <$fd>;
1004 $tag{'comment'} = \@comment;
1005 close $fd or return;
1006 if (!defined $tag{'name'}) {
1007 return
1009 return %tag
1012 sub git_get_last_activity {
1013 my ($path) = @_;
1014 my $fd;
1016 $git_dir = "$projectroot/$path";
1017 open($fd, "-|", git_cmd(), 'for-each-ref',
1018 '--format=%(refname) %(committer)',
1019 '--sort=-committerdate',
1020 'refs/heads') or return;
1021 my $most_recent = <$fd>;
1022 close $fd or return;
1023 if ($most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1024 my $timestamp = $1;
1025 my $age = time - $timestamp;
1026 return ($age, age_string($age));
1030 sub parse_commit {
1031 my $commit_id = shift;
1032 my $commit_text = shift;
1034 my @commit_lines;
1035 my %co;
1037 if (defined $commit_text) {
1038 @commit_lines = @$commit_text;
1039 } else {
1040 $/ = "\0";
1041 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
1042 or return;
1043 @commit_lines = split '\n', <$fd>;
1044 close $fd or return;
1045 $/ = "\n";
1046 pop @commit_lines;
1048 my $header = shift @commit_lines;
1049 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1050 return;
1052 ($co{'id'}, my @parents) = split ' ', $header;
1053 $co{'parents'} = \@parents;
1054 $co{'parent'} = $parents[0];
1055 while (my $line = shift @commit_lines) {
1056 last if $line eq "\n";
1057 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1058 $co{'tree'} = $1;
1059 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1060 $co{'author'} = $1;
1061 $co{'author_epoch'} = $2;
1062 $co{'author_tz'} = $3;
1063 if ($co{'author'} =~ m/^([^<]+) </) {
1064 $co{'author_name'} = $1;
1065 } else {
1066 $co{'author_name'} = $co{'author'};
1068 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1069 $co{'committer'} = $1;
1070 $co{'committer_epoch'} = $2;
1071 $co{'committer_tz'} = $3;
1072 $co{'committer_name'} = $co{'committer'};
1073 $co{'committer_name'} =~ s/ <.*//;
1076 if (!defined $co{'tree'}) {
1077 return;
1080 foreach my $title (@commit_lines) {
1081 $title =~ s/^ //;
1082 if ($title ne "") {
1083 $co{'title'} = chop_str($title, 80, 5);
1084 # remove leading stuff of merges to make the interesting part visible
1085 if (length($title) > 50) {
1086 $title =~ s/^Automatic //;
1087 $title =~ s/^merge (of|with) /Merge ... /i;
1088 if (length($title) > 50) {
1089 $title =~ s/(http|rsync):\/\///;
1091 if (length($title) > 50) {
1092 $title =~ s/(master|www|rsync)\.//;
1094 if (length($title) > 50) {
1095 $title =~ s/kernel.org:?//;
1097 if (length($title) > 50) {
1098 $title =~ s/\/pub\/scm//;
1101 $co{'title_short'} = chop_str($title, 50, 5);
1102 last;
1105 # remove added spaces
1106 foreach my $line (@commit_lines) {
1107 $line =~ s/^ //;
1109 $co{'comment'} = \@commit_lines;
1111 my $age = time - $co{'committer_epoch'};
1112 $co{'age'} = $age;
1113 $co{'age_string'} = age_string($age);
1114 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1115 if ($age > 60*60*24*7*2) {
1116 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1117 $co{'age_string_age'} = $co{'age_string'};
1118 } else {
1119 $co{'age_string_date'} = $co{'age_string'};
1120 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1122 return %co;
1125 # parse ref from ref_file, given by ref_id, with given type
1126 sub parse_ref {
1127 my $ref_file = shift;
1128 my $ref_id = shift;
1129 my $type = shift || git_get_type($ref_id);
1130 my %ref_item;
1132 $ref_item{'type'} = $type;
1133 $ref_item{'id'} = $ref_id;
1134 $ref_item{'epoch'} = 0;
1135 $ref_item{'age'} = "unknown";
1136 if ($type eq "tag") {
1137 my %tag = parse_tag($ref_id);
1138 $ref_item{'comment'} = $tag{'comment'};
1139 if ($tag{'type'} eq "commit") {
1140 my %co = parse_commit($tag{'object'});
1141 $ref_item{'epoch'} = $co{'committer_epoch'};
1142 $ref_item{'age'} = $co{'age_string'};
1143 } elsif (defined($tag{'epoch'})) {
1144 my $age = time - $tag{'epoch'};
1145 $ref_item{'epoch'} = $tag{'epoch'};
1146 $ref_item{'age'} = age_string($age);
1148 $ref_item{'reftype'} = $tag{'type'};
1149 $ref_item{'name'} = $tag{'name'};
1150 $ref_item{'refid'} = $tag{'object'};
1151 } elsif ($type eq "commit"){
1152 my %co = parse_commit($ref_id);
1153 $ref_item{'reftype'} = "commit";
1154 $ref_item{'name'} = $ref_file;
1155 $ref_item{'title'} = $co{'title'};
1156 $ref_item{'refid'} = $ref_id;
1157 $ref_item{'epoch'} = $co{'committer_epoch'};
1158 $ref_item{'age'} = $co{'age_string'};
1159 } else {
1160 $ref_item{'reftype'} = $type;
1161 $ref_item{'name'} = $ref_file;
1162 $ref_item{'refid'} = $ref_id;
1165 return %ref_item;
1168 # parse line of git-diff-tree "raw" output
1169 sub parse_difftree_raw_line {
1170 my $line = shift;
1171 my %res;
1173 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1174 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1175 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1176 $res{'from_mode'} = $1;
1177 $res{'to_mode'} = $2;
1178 $res{'from_id'} = $3;
1179 $res{'to_id'} = $4;
1180 $res{'status'} = $5;
1181 $res{'similarity'} = $6;
1182 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1183 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1184 } else {
1185 $res{'file'} = unquote($7);
1188 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1189 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1190 $res{'commit'} = $1;
1193 return wantarray ? %res : \%res;
1196 # parse line of git-ls-tree output
1197 sub parse_ls_tree_line ($;%) {
1198 my $line = shift;
1199 my %opts = @_;
1200 my %res;
1202 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1203 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1205 $res{'mode'} = $1;
1206 $res{'type'} = $2;
1207 $res{'hash'} = $3;
1208 if ($opts{'-z'}) {
1209 $res{'name'} = $4;
1210 } else {
1211 $res{'name'} = unquote($4);
1214 return wantarray ? %res : \%res;
1217 ## ......................................................................
1218 ## parse to array of hashes functions
1220 sub git_get_refs_list {
1221 my $type = shift || "";
1222 my %refs;
1223 my @reflist;
1225 my @refs;
1226 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1227 or return;
1228 while (my $line = <$fd>) {
1229 chomp $line;
1230 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1231 if (defined $refs{$1}) {
1232 push @{$refs{$1}}, $2;
1233 } else {
1234 $refs{$1} = [ $2 ];
1237 if (! $4) { # unpeeled, direct reference
1238 push @refs, { hash => $1, name => $3 }; # without type
1239 } elsif ($3 eq $refs[-1]{'name'}) {
1240 # most likely a tag is followed by its peeled
1241 # (deref) one, and when that happens we know the
1242 # previous one was of type 'tag'.
1243 $refs[-1]{'type'} = "tag";
1247 close $fd;
1249 foreach my $ref (@refs) {
1250 my $ref_file = $ref->{'name'};
1251 my $ref_id = $ref->{'hash'};
1253 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1254 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1256 push @reflist, \%ref_item;
1258 # sort refs by age
1259 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1260 return (\@reflist, \%refs);
1263 ## ----------------------------------------------------------------------
1264 ## filesystem-related functions
1266 sub get_file_owner {
1267 my $path = shift;
1269 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1270 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1271 if (!defined $gcos) {
1272 return undef;
1274 my $owner = $gcos;
1275 $owner =~ s/[,;].*$//;
1276 return decode("utf8", $owner, Encode::FB_DEFAULT);
1279 ## ......................................................................
1280 ## mimetype related functions
1282 sub mimetype_guess_file {
1283 my $filename = shift;
1284 my $mimemap = shift;
1285 -r $mimemap or return undef;
1287 my %mimemap;
1288 open(MIME, $mimemap) or return undef;
1289 while (<MIME>) {
1290 next if m/^#/; # skip comments
1291 my ($mime, $exts) = split(/\t+/);
1292 if (defined $exts) {
1293 my @exts = split(/\s+/, $exts);
1294 foreach my $ext (@exts) {
1295 $mimemap{$ext} = $mime;
1299 close(MIME);
1301 $filename =~ /\.([^.]*)$/;
1302 return $mimemap{$1};
1305 sub mimetype_guess {
1306 my $filename = shift;
1307 my $mime;
1308 $filename =~ /\./ or return undef;
1310 if ($mimetypes_file) {
1311 my $file = $mimetypes_file;
1312 if ($file !~ m!^/!) { # if it is relative path
1313 # it is relative to project
1314 $file = "$projectroot/$project/$file";
1316 $mime = mimetype_guess_file($filename, $file);
1318 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1319 return $mime;
1322 sub blob_mimetype {
1323 my $fd = shift;
1324 my $filename = shift;
1326 if ($filename) {
1327 my $mime = mimetype_guess($filename);
1328 $mime and return $mime;
1331 # just in case
1332 return $default_blob_plain_mimetype unless $fd;
1334 if (-T $fd) {
1335 return 'text/plain' .
1336 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1337 } elsif (! $filename) {
1338 return 'application/octet-stream';
1339 } elsif ($filename =~ m/\.png$/i) {
1340 return 'image/png';
1341 } elsif ($filename =~ m/\.gif$/i) {
1342 return 'image/gif';
1343 } elsif ($filename =~ m/\.jpe?g$/i) {
1344 return 'image/jpeg';
1345 } else {
1346 return 'application/octet-stream';
1350 ## ======================================================================
1351 ## functions printing HTML: header, footer, error page
1353 sub git_header_html {
1354 my $status = shift || "200 OK";
1355 my $expires = shift;
1357 my $title = "$site_name git";
1358 if (defined $project) {
1359 $title .= " - $project";
1360 if (defined $action) {
1361 $title .= "/$action";
1362 if (defined $file_name) {
1363 $title .= " - " . esc_html($file_name);
1364 if ($action eq "tree" && $file_name !~ m|/$|) {
1365 $title .= "/";
1370 my $content_type;
1371 # require explicit support from the UA if we are to send the page as
1372 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1373 # we have to do this because MSIE sometimes globs '*/*', pretending to
1374 # support xhtml+xml but choking when it gets what it asked for.
1375 if (defined $cgi->http('HTTP_ACCEPT') &&
1376 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1377 $cgi->Accept('application/xhtml+xml') != 0) {
1378 $content_type = 'application/xhtml+xml';
1379 } else {
1380 $content_type = 'text/html';
1382 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1383 -status=> $status, -expires => $expires);
1384 print <<EOF;
1385 <?xml version="1.0" encoding="utf-8"?>
1386 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1387 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1388 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1389 <!-- git core binaries version $git_version -->
1390 <head>
1391 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1392 <meta name="generator" content="gitweb/$version git/$git_version"/>
1393 <meta name="robots" content="index, nofollow"/>
1394 <title>$title</title>
1396 # print out each stylesheet that exist
1397 if (defined $stylesheet) {
1398 #provides backwards capability for those people who define style sheet in a config file
1399 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1400 } else {
1401 foreach my $stylesheet (@stylesheets) {
1402 next unless $stylesheet;
1403 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1406 if (defined $project) {
1407 printf('<link rel="alternate" title="%s log" '.
1408 'href="%s" type="application/rss+xml"/>'."\n",
1409 esc_param($project), href(action=>"rss"));
1410 } else {
1411 printf('<link rel="alternate" title="%s projects list" '.
1412 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1413 $site_name, href(project=>undef, action=>"project_index"));
1414 printf('<link rel="alternate" title="%s projects logs" '.
1415 'href="%s" type="text/x-opml"/>'."\n",
1416 $site_name, href(project=>undef, action=>"opml"));
1418 if (defined $favicon) {
1419 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1422 print "</head>\n" .
1423 "<body>\n";
1425 if (-f $site_header) {
1426 open (my $fd, $site_header);
1427 print <$fd>;
1428 close $fd;
1431 print "<div class=\"page_header\">\n" .
1432 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1433 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1434 "</a>\n";
1435 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1436 if (defined $project) {
1437 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1438 if (defined $action) {
1439 print " / $action";
1441 print "\n";
1442 if (!defined $searchtext) {
1443 $searchtext = "";
1445 my $search_hash;
1446 if (defined $hash_base) {
1447 $search_hash = $hash_base;
1448 } elsif (defined $hash) {
1449 $search_hash = $hash;
1450 } else {
1451 $search_hash = "HEAD";
1453 $cgi->param("a", "search");
1454 $cgi->param("h", $search_hash);
1455 print $cgi->startform(-method => "get", -action => $my_uri) .
1456 "<div class=\"search\">\n" .
1457 $cgi->hidden(-name => "p") . "\n" .
1458 $cgi->hidden(-name => "a") . "\n" .
1459 $cgi->hidden(-name => "h") . "\n" .
1460 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1461 "</div>" .
1462 $cgi->end_form() . "\n";
1464 print "</div>\n";
1467 sub git_footer_html {
1468 print "<div class=\"page_footer\">\n";
1469 if (defined $project) {
1470 my $descr = git_get_project_description($project);
1471 if (defined $descr) {
1472 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1474 print $cgi->a({-href => href(action=>"rss"),
1475 -class => "rss_logo"}, "RSS") . "\n";
1476 } else {
1477 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1478 -class => "rss_logo"}, "OPML") . " ";
1479 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1480 -class => "rss_logo"}, "TXT") . "\n";
1482 print "</div>\n" ;
1484 if (-f $site_footer) {
1485 open (my $fd, $site_footer);
1486 print <$fd>;
1487 close $fd;
1490 print "</body>\n" .
1491 "</html>";
1494 sub die_error {
1495 my $status = shift || "403 Forbidden";
1496 my $error = shift || "Malformed query, file missing or permission denied";
1498 git_header_html($status);
1499 print <<EOF;
1500 <div class="page_body">
1501 <br /><br />
1502 $status - $error
1503 <br />
1504 </div>
1506 git_footer_html();
1507 exit;
1510 ## ----------------------------------------------------------------------
1511 ## functions printing or outputting HTML: navigation
1513 sub git_print_page_nav {
1514 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1515 $extra = '' if !defined $extra; # pager or formats
1517 my @navs = qw(summary shortlog log commit commitdiff tree);
1518 if ($suppress) {
1519 @navs = grep { $_ ne $suppress } @navs;
1522 my %arg = map { $_ => {action=>$_} } @navs;
1523 if (defined $head) {
1524 for (qw(commit commitdiff)) {
1525 $arg{$_}{hash} = $head;
1527 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1528 for (qw(shortlog log)) {
1529 $arg{$_}{hash} = $head;
1533 $arg{tree}{hash} = $treehead if defined $treehead;
1534 $arg{tree}{hash_base} = $treebase if defined $treebase;
1536 print "<div class=\"page_nav\">\n" .
1537 (join " | ",
1538 map { $_ eq $current ?
1539 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1540 } @navs);
1541 print "<br/>\n$extra<br/>\n" .
1542 "</div>\n";
1545 sub format_paging_nav {
1546 my ($action, $hash, $head, $page, $nrevs) = @_;
1547 my $paging_nav;
1550 if ($hash ne $head || $page) {
1551 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1552 } else {
1553 $paging_nav .= "HEAD";
1556 if ($page > 0) {
1557 $paging_nav .= " &sdot; " .
1558 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1559 -accesskey => "p", -title => "Alt-p"}, "prev");
1560 } else {
1561 $paging_nav .= " &sdot; prev";
1564 if ($nrevs >= (100 * ($page+1)-1)) {
1565 $paging_nav .= " &sdot; " .
1566 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1567 -accesskey => "n", -title => "Alt-n"}, "next");
1568 } else {
1569 $paging_nav .= " &sdot; next";
1572 return $paging_nav;
1575 ## ......................................................................
1576 ## functions printing or outputting HTML: div
1578 sub git_print_header_div {
1579 my ($action, $title, $hash, $hash_base) = @_;
1580 my %args = ();
1582 $args{action} = $action;
1583 $args{hash} = $hash if $hash;
1584 $args{hash_base} = $hash_base if $hash_base;
1586 print "<div class=\"header\">\n" .
1587 $cgi->a({-href => href(%args), -class => "title"},
1588 $title ? $title : $action) .
1589 "\n</div>\n";
1592 #sub git_print_authorship (\%) {
1593 sub git_print_authorship {
1594 my $co = shift;
1596 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1597 print "<div class=\"author_date\">" .
1598 esc_html($co->{'author_name'}) .
1599 " [$ad{'rfc2822'}";
1600 if ($ad{'hour_local'} < 6) {
1601 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1602 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1603 } else {
1604 printf(" (%02d:%02d %s)",
1605 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1607 print "]</div>\n";
1610 sub git_print_page_path {
1611 my $name = shift;
1612 my $type = shift;
1613 my $hb = shift;
1615 if (!defined $name) {
1616 print "<div class=\"page_path\">/</div>\n";
1617 } else {
1618 my @dirname = split '/', $name;
1619 my $basename = pop @dirname;
1620 my $fullname = '';
1622 print "<div class=\"page_path\">";
1623 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1624 -title => 'tree root'}, "[$project]");
1625 print " / ";
1626 foreach my $dir (@dirname) {
1627 $fullname .= ($fullname ? '/' : '') . $dir;
1628 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1629 hash_base=>$hb),
1630 -title => $fullname}, esc_html($dir));
1631 print " / ";
1633 if (defined $type && $type eq 'blob') {
1634 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1635 hash_base=>$hb),
1636 -title => $name}, esc_html($basename));
1637 } elsif (defined $type && $type eq 'tree') {
1638 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1639 hash_base=>$hb),
1640 -title => $name}, esc_html($basename));
1641 } else {
1642 print esc_html($basename);
1644 print "<br/></div>\n";
1648 # sub git_print_log (\@;%) {
1649 sub git_print_log ($;%) {
1650 my $log = shift;
1651 my %opts = @_;
1653 if ($opts{'-remove_title'}) {
1654 # remove title, i.e. first line of log
1655 shift @$log;
1657 # remove leading empty lines
1658 while (defined $log->[0] && $log->[0] eq "") {
1659 shift @$log;
1662 # print log
1663 my $signoff = 0;
1664 my $empty = 0;
1665 foreach my $line (@$log) {
1666 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1667 $signoff = 1;
1668 $empty = 0;
1669 if (! $opts{'-remove_signoff'}) {
1670 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1671 next;
1672 } else {
1673 # remove signoff lines
1674 next;
1676 } else {
1677 $signoff = 0;
1680 # print only one empty line
1681 # do not print empty line after signoff
1682 if ($line eq "") {
1683 next if ($empty || $signoff);
1684 $empty = 1;
1685 } else {
1686 $empty = 0;
1689 print format_log_line_html($line) . "<br/>\n";
1692 if ($opts{'-final_empty_line'}) {
1693 # end with single empty line
1694 print "<br/>\n" unless $empty;
1698 sub git_print_simplified_log {
1699 my $log = shift;
1700 my $remove_title = shift;
1702 git_print_log($log,
1703 -final_empty_line=> 1,
1704 -remove_title => $remove_title);
1707 # print tree entry (row of git_tree), but without encompassing <tr> element
1708 sub git_print_tree_entry {
1709 my ($t, $basedir, $hash_base, $have_blame) = @_;
1711 my %base_key = ();
1712 $base_key{hash_base} = $hash_base if defined $hash_base;
1714 # The format of a table row is: mode list link. Where mode is
1715 # the mode of the entry, list is the name of the entry, an href,
1716 # and link is the action links of the entry.
1718 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1719 if ($t->{'type'} eq "blob") {
1720 print "<td class=\"list\">" .
1721 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1722 file_name=>"$basedir$t->{'name'}", %base_key),
1723 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1724 print "<td class=\"link\">";
1725 if ($have_blame) {
1726 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1727 file_name=>"$basedir$t->{'name'}", %base_key)},
1728 "blame");
1730 if (defined $hash_base) {
1731 if ($have_blame) {
1732 print " | ";
1734 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1735 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1736 "history");
1738 print " | " .
1739 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1740 file_name=>"$basedir$t->{'name'}")},
1741 "raw");
1742 print "</td>\n";
1744 } elsif ($t->{'type'} eq "tree") {
1745 print "<td class=\"list\">";
1746 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1747 file_name=>"$basedir$t->{'name'}", %base_key)},
1748 esc_html($t->{'name'}));
1749 print "</td>\n";
1750 print "<td class=\"link\">";
1751 if (defined $hash_base) {
1752 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1753 file_name=>"$basedir$t->{'name'}")},
1754 "history");
1756 print "</td>\n";
1760 ## ......................................................................
1761 ## functions printing large fragments of HTML
1763 sub git_difftree_body {
1764 my ($difftree, $hash, $parent) = @_;
1766 print "<div class=\"list_head\">\n";
1767 if ($#{$difftree} > 10) {
1768 print(($#{$difftree} + 1) . " files changed:\n");
1770 print "</div>\n";
1772 print "<table class=\"diff_tree\">\n";
1773 my $alternate = 1;
1774 my $patchno = 0;
1775 foreach my $line (@{$difftree}) {
1776 my %diff = parse_difftree_raw_line($line);
1778 if ($alternate) {
1779 print "<tr class=\"dark\">\n";
1780 } else {
1781 print "<tr class=\"light\">\n";
1783 $alternate ^= 1;
1785 my ($to_mode_oct, $to_mode_str, $to_file_type);
1786 my ($from_mode_oct, $from_mode_str, $from_file_type);
1787 if ($diff{'to_mode'} ne ('0' x 6)) {
1788 $to_mode_oct = oct $diff{'to_mode'};
1789 if (S_ISREG($to_mode_oct)) { # only for regular file
1790 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1792 $to_file_type = file_type($diff{'to_mode'});
1794 if ($diff{'from_mode'} ne ('0' x 6)) {
1795 $from_mode_oct = oct $diff{'from_mode'};
1796 if (S_ISREG($to_mode_oct)) { # only for regular file
1797 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1799 $from_file_type = file_type($diff{'from_mode'});
1802 if ($diff{'status'} eq "A") { # created
1803 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1804 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1805 $mode_chng .= "]</span>";
1806 print "<td>";
1807 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1808 hash_base=>$hash, file_name=>$diff{'file'}),
1809 -class => "list"}, esc_html($diff{'file'}));
1810 print "</td>\n";
1811 print "<td>$mode_chng</td>\n";
1812 print "<td class=\"link\">";
1813 if ($action eq 'commitdiff') {
1814 # link to patch
1815 $patchno++;
1816 print $cgi->a({-href => "#patch$patchno"}, "patch");
1818 print "</td>\n";
1820 } elsif ($diff{'status'} eq "D") { # deleted
1821 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1822 print "<td>";
1823 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1824 hash_base=>$parent, file_name=>$diff{'file'}),
1825 -class => "list"}, esc_html($diff{'file'}));
1826 print "</td>\n";
1827 print "<td>$mode_chng</td>\n";
1828 print "<td class=\"link\">";
1829 if ($action eq 'commitdiff') {
1830 # link to patch
1831 $patchno++;
1832 print $cgi->a({-href => "#patch$patchno"}, "patch");
1833 print " | ";
1835 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1836 file_name=>$diff{'file'})},
1837 "blame") . " | ";
1838 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1839 file_name=>$diff{'file'})},
1840 "history");
1841 print "</td>\n";
1843 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1844 my $mode_chnge = "";
1845 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1846 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1847 if ($from_file_type != $to_file_type) {
1848 $mode_chnge .= " from $from_file_type to $to_file_type";
1850 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1851 if ($from_mode_str && $to_mode_str) {
1852 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1853 } elsif ($to_mode_str) {
1854 $mode_chnge .= " mode: $to_mode_str";
1857 $mode_chnge .= "]</span>\n";
1859 print "<td>";
1860 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1861 hash_base=>$hash, file_name=>$diff{'file'}),
1862 -class => "list"}, esc_html($diff{'file'}));
1863 print "</td>\n";
1864 print "<td>$mode_chnge</td>\n";
1865 print "<td class=\"link\">";
1866 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1867 if ($action eq 'commitdiff') {
1868 # link to patch
1869 $patchno++;
1870 print $cgi->a({-href => "#patch$patchno"}, "patch");
1871 } else {
1872 print $cgi->a({-href => href(action=>"blobdiff",
1873 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1874 hash_base=>$hash, hash_parent_base=>$parent,
1875 file_name=>$diff{'file'})},
1876 "diff");
1878 print " | ";
1880 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1881 file_name=>$diff{'file'})},
1882 "blame") . " | ";
1883 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1884 file_name=>$diff{'file'})},
1885 "history");
1886 print "</td>\n";
1888 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1889 my %status_name = ('R' => 'moved', 'C' => 'copied');
1890 my $nstatus = $status_name{$diff{'status'}};
1891 my $mode_chng = "";
1892 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1893 # mode also for directories, so we cannot use $to_mode_str
1894 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1896 print "<td>" .
1897 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1898 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1899 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1900 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1901 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1902 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1903 -class => "list"}, esc_html($diff{'from_file'})) .
1904 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1905 "<td class=\"link\">";
1906 if ($diff{'to_id'} ne $diff{'from_id'}) {
1907 if ($action eq 'commitdiff') {
1908 # link to patch
1909 $patchno++;
1910 print $cgi->a({-href => "#patch$patchno"}, "patch");
1911 } else {
1912 print $cgi->a({-href => href(action=>"blobdiff",
1913 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1914 hash_base=>$hash, hash_parent_base=>$parent,
1915 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1916 "diff");
1918 print " | ";
1920 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1921 file_name=>$diff{'from_file'})},
1922 "blame") . " | ";
1923 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1924 file_name=>$diff{'from_file'})},
1925 "history");
1926 print "</td>\n";
1928 } # we should not encounter Unmerged (U) or Unknown (X) status
1929 print "</tr>\n";
1931 print "</table>\n";
1934 sub git_patchset_body {
1935 my ($fd, $difftree, $hash, $hash_parent) = @_;
1937 my $patch_idx = 0;
1938 my $in_header = 0;
1939 my $patch_found = 0;
1940 my $diffinfo;
1942 print "<div class=\"patchset\">\n";
1944 LINE:
1945 while (my $patch_line = <$fd>) {
1946 chomp $patch_line;
1948 if ($patch_line =~ m/^diff /) { # "git diff" header
1949 # beginning of patch (in patchset)
1950 if ($patch_found) {
1951 # close previous patch
1952 print "</div>\n"; # class="patch"
1953 } else {
1954 # first patch in patchset
1955 $patch_found = 1;
1957 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1959 if (ref($difftree->[$patch_idx]) eq "HASH") {
1960 $diffinfo = $difftree->[$patch_idx];
1961 } else {
1962 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1964 $patch_idx++;
1966 # for now, no extended header, hence we skip empty patches
1967 # companion to next LINE if $in_header;
1968 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1969 $in_header = 1;
1970 next LINE;
1973 if ($diffinfo->{'status'} eq "A") { # added
1974 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1975 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1976 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1977 $diffinfo->{'to_id'}) . "(new)" .
1978 "</div>\n"; # class="diff_info"
1980 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1981 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1982 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1983 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1984 $diffinfo->{'from_id'}) . "(deleted)" .
1985 "</div>\n"; # class="diff_info"
1987 } elsif ($diffinfo->{'status'} eq "R" || # renamed
1988 $diffinfo->{'status'} eq "C" || # copied
1989 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1990 print "<div class=\"diff_info\">" .
1991 file_type($diffinfo->{'from_mode'}) . ":" .
1992 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1993 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1994 $diffinfo->{'from_id'}) .
1995 " -> " .
1996 file_type($diffinfo->{'to_mode'}) . ":" .
1997 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1998 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1999 $diffinfo->{'to_id'});
2000 print "</div>\n"; # class="diff_info"
2002 } else { # modified, mode changed, ...
2003 print "<div class=\"diff_info\">" .
2004 file_type($diffinfo->{'from_mode'}) . ":" .
2005 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2006 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2007 $diffinfo->{'from_id'}) .
2008 " -> " .
2009 file_type($diffinfo->{'to_mode'}) . ":" .
2010 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2011 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2012 $diffinfo->{'to_id'});
2013 print "</div>\n"; # class="diff_info"
2016 #print "<div class=\"diff extended_header\">\n";
2017 $in_header = 1;
2018 next LINE;
2019 } # start of patch in patchset
2022 if ($in_header && $patch_line =~ m/^---/) {
2023 #print "</div>\n"; # class="diff extended_header"
2024 $in_header = 0;
2026 my $file = $diffinfo->{'from_file'};
2027 $file ||= $diffinfo->{'file'};
2028 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2029 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2030 -class => "list"}, esc_html($file));
2031 $patch_line =~ s|a/.*$|a/$file|g;
2032 print "<div class=\"diff from_file\">$patch_line</div>\n";
2034 $patch_line = <$fd>;
2035 chomp $patch_line;
2037 #$patch_line =~ m/^+++/;
2038 $file = $diffinfo->{'to_file'};
2039 $file ||= $diffinfo->{'file'};
2040 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2041 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2042 -class => "list"}, esc_html($file));
2043 $patch_line =~ s|b/.*|b/$file|g;
2044 print "<div class=\"diff to_file\">$patch_line</div>\n";
2046 next LINE;
2048 next LINE if $in_header;
2050 print format_diff_line($patch_line);
2052 print "</div>\n" if $patch_found; # class="patch"
2054 print "</div>\n"; # class="patchset"
2057 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2059 sub git_shortlog_body {
2060 # uses global variable $project
2061 my ($revlist, $from, $to, $refs, $extra) = @_;
2063 $from = 0 unless defined $from;
2064 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2066 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2067 my $alternate = 1;
2068 for (my $i = $from; $i <= $to; $i++) {
2069 my $commit = $revlist->[$i];
2070 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2071 my $ref = format_ref_marker($refs, $commit);
2072 my %co = parse_commit($commit);
2073 if ($alternate) {
2074 print "<tr class=\"dark\">\n";
2075 } else {
2076 print "<tr class=\"light\">\n";
2078 $alternate ^= 1;
2079 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2080 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2081 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2082 "<td>";
2083 print format_subject_html($co{'title'}, $co{'title_short'},
2084 href(action=>"commit", hash=>$commit), $ref);
2085 print "</td>\n" .
2086 "<td class=\"link\">" .
2087 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2088 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") . " | " .
2089 $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2090 print "</td>\n" .
2091 "</tr>\n";
2093 if (defined $extra) {
2094 print "<tr>\n" .
2095 "<td colspan=\"4\">$extra</td>\n" .
2096 "</tr>\n";
2098 print "</table>\n";
2101 sub git_history_body {
2102 # Warning: assumes constant type (blob or tree) during history
2103 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2105 $from = 0 unless defined $from;
2106 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2108 print "<table class=\"history\" cellspacing=\"0\">\n";
2109 my $alternate = 1;
2110 for (my $i = $from; $i <= $to; $i++) {
2111 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2112 next;
2115 my $commit = $1;
2116 my %co = parse_commit($commit);
2117 if (!%co) {
2118 next;
2121 my $ref = format_ref_marker($refs, $commit);
2123 if ($alternate) {
2124 print "<tr class=\"dark\">\n";
2125 } else {
2126 print "<tr class=\"light\">\n";
2128 $alternate ^= 1;
2129 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2130 # shortlog uses chop_str($co{'author_name'}, 10)
2131 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2132 "<td>";
2133 # originally git_history used chop_str($co{'title'}, 50)
2134 print format_subject_html($co{'title'}, $co{'title_short'},
2135 href(action=>"commit", hash=>$commit), $ref);
2136 print "</td>\n" .
2137 "<td class=\"link\">" .
2138 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2139 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2141 if ($ftype eq 'blob') {
2142 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2143 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2144 if (defined $blob_current && defined $blob_parent &&
2145 $blob_current ne $blob_parent) {
2146 print " | " .
2147 $cgi->a({-href => href(action=>"blobdiff",
2148 hash=>$blob_current, hash_parent=>$blob_parent,
2149 hash_base=>$hash_base, hash_parent_base=>$commit,
2150 file_name=>$file_name)},
2151 "diff to current");
2154 print "</td>\n" .
2155 "</tr>\n";
2157 if (defined $extra) {
2158 print "<tr>\n" .
2159 "<td colspan=\"4\">$extra</td>\n" .
2160 "</tr>\n";
2162 print "</table>\n";
2165 sub git_tags_body {
2166 # uses global variable $project
2167 my ($taglist, $from, $to, $extra) = @_;
2168 $from = 0 unless defined $from;
2169 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2171 print "<table class=\"tags\" cellspacing=\"0\">\n";
2172 my $alternate = 1;
2173 for (my $i = $from; $i <= $to; $i++) {
2174 my $entry = $taglist->[$i];
2175 my %tag = %$entry;
2176 my $comment_lines = $tag{'comment'};
2177 my $comment = shift @$comment_lines;
2178 my $comment_short;
2179 if (defined $comment) {
2180 $comment_short = chop_str($comment, 30, 5);
2182 if ($alternate) {
2183 print "<tr class=\"dark\">\n";
2184 } else {
2185 print "<tr class=\"light\">\n";
2187 $alternate ^= 1;
2188 print "<td><i>$tag{'age'}</i></td>\n" .
2189 "<td>" .
2190 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2191 -class => "list name"}, esc_html($tag{'name'})) .
2192 "</td>\n" .
2193 "<td>";
2194 if (defined $comment) {
2195 print format_subject_html($comment, $comment_short,
2196 href(action=>"tag", hash=>$tag{'id'}));
2198 print "</td>\n" .
2199 "<td class=\"selflink\">";
2200 if ($tag{'type'} eq "tag") {
2201 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2202 } else {
2203 print "&nbsp;";
2205 print "</td>\n" .
2206 "<td class=\"link\">" . " | " .
2207 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2208 if ($tag{'reftype'} eq "commit") {
2209 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2210 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2211 } elsif ($tag{'reftype'} eq "blob") {
2212 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2214 print "</td>\n" .
2215 "</tr>";
2217 if (defined $extra) {
2218 print "<tr>\n" .
2219 "<td colspan=\"5\">$extra</td>\n" .
2220 "</tr>\n";
2222 print "</table>\n";
2225 sub git_heads_body {
2226 # uses global variable $project
2227 my ($headlist, $head, $from, $to, $extra) = @_;
2228 $from = 0 unless defined $from;
2229 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2231 print "<table class=\"heads\" cellspacing=\"0\">\n";
2232 my $alternate = 1;
2233 for (my $i = $from; $i <= $to; $i++) {
2234 my $entry = $headlist->[$i];
2235 my %tag = %$entry;
2236 my $curr = $tag{'id'} eq $head;
2237 if ($alternate) {
2238 print "<tr class=\"dark\">\n";
2239 } else {
2240 print "<tr class=\"light\">\n";
2242 $alternate ^= 1;
2243 print "<td><i>$tag{'age'}</i></td>\n" .
2244 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2245 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2246 -class => "list name"},esc_html($tag{'name'})) .
2247 "</td>\n" .
2248 "<td class=\"link\">" .
2249 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2250 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2251 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2252 "</td>\n" .
2253 "</tr>";
2255 if (defined $extra) {
2256 print "<tr>\n" .
2257 "<td colspan=\"3\">$extra</td>\n" .
2258 "</tr>\n";
2260 print "</table>\n";
2263 ## ======================================================================
2264 ## ======================================================================
2265 ## actions
2267 sub git_project_list {
2268 my $order = $cgi->param('o');
2269 if (defined $order && $order !~ m/project|descr|owner|age/) {
2270 die_error(undef, "Unknown order parameter");
2273 my @list = git_get_projects_list();
2274 my @projects;
2275 if (!@list) {
2276 die_error(undef, "No projects found");
2278 foreach my $pr (@list) {
2279 my (@aa) = git_get_last_activity($pr->{'path'});
2280 unless (@aa) {
2281 next;
2283 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2284 if (!defined $pr->{'descr'}) {
2285 my $descr = git_get_project_description($pr->{'path'}) || "";
2286 $pr->{'descr'} = chop_str($descr, 25, 5);
2288 if (!defined $pr->{'owner'}) {
2289 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2291 push @projects, $pr;
2294 git_header_html();
2295 if (-f $home_text) {
2296 print "<div class=\"index_include\">\n";
2297 open (my $fd, $home_text);
2298 print <$fd>;
2299 close $fd;
2300 print "</div>\n";
2302 print "<table class=\"project_list\">\n" .
2303 "<tr>\n";
2304 $order ||= "project";
2305 if ($order eq "project") {
2306 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2307 print "<th>Project</th>\n";
2308 } else {
2309 print "<th>" .
2310 $cgi->a({-href => href(project=>undef, order=>'project'),
2311 -class => "header"}, "Project") .
2312 "</th>\n";
2314 if ($order eq "descr") {
2315 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2316 print "<th>Description</th>\n";
2317 } else {
2318 print "<th>" .
2319 $cgi->a({-href => href(project=>undef, order=>'descr'),
2320 -class => "header"}, "Description") .
2321 "</th>\n";
2323 if ($order eq "owner") {
2324 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2325 print "<th>Owner</th>\n";
2326 } else {
2327 print "<th>" .
2328 $cgi->a({-href => href(project=>undef, order=>'owner'),
2329 -class => "header"}, "Owner") .
2330 "</th>\n";
2332 if ($order eq "age") {
2333 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2334 print "<th>Last Change</th>\n";
2335 } else {
2336 print "<th>" .
2337 $cgi->a({-href => href(project=>undef, order=>'age'),
2338 -class => "header"}, "Last Change") .
2339 "</th>\n";
2341 print "<th></th>\n" .
2342 "</tr>\n";
2343 my $alternate = 1;
2344 foreach my $pr (@projects) {
2345 if ($alternate) {
2346 print "<tr class=\"dark\">\n";
2347 } else {
2348 print "<tr class=\"light\">\n";
2350 $alternate ^= 1;
2351 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2352 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2353 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2354 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2355 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2356 $pr->{'age_string'} . "</td>\n" .
2357 "<td class=\"link\">" .
2358 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2359 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2360 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2361 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2362 "</td>\n" .
2363 "</tr>\n";
2365 print "</table>\n";
2366 git_footer_html();
2369 sub git_project_index {
2370 my @projects = git_get_projects_list();
2372 print $cgi->header(
2373 -type => 'text/plain',
2374 -charset => 'utf-8',
2375 -content_disposition => 'inline; filename="index.aux"');
2377 foreach my $pr (@projects) {
2378 if (!exists $pr->{'owner'}) {
2379 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2382 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2383 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2384 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2385 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2386 $path =~ s/ /\+/g;
2387 $owner =~ s/ /\+/g;
2389 print "$path $owner\n";
2393 sub git_summary {
2394 my $descr = git_get_project_description($project) || "none";
2395 my $head = git_get_head_hash($project);
2396 my %co = parse_commit($head);
2397 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2399 my $owner = git_get_project_owner($project);
2401 my ($reflist, $refs) = git_get_refs_list();
2403 my @taglist;
2404 my @headlist;
2405 foreach my $ref (@$reflist) {
2406 if ($ref->{'name'} =~ s!^heads/!!) {
2407 push @headlist, $ref;
2408 } else {
2409 $ref->{'name'} =~ s!^tags/!!;
2410 push @taglist, $ref;
2414 git_header_html();
2415 git_print_page_nav('summary','', $head);
2417 print "<div class=\"title\">&nbsp;</div>\n";
2418 print "<table cellspacing=\"0\">\n" .
2419 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2420 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2421 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2422 # use per project git URL list in $projectroot/$project/cloneurl
2423 # or make project git URL from git base URL and project name
2424 my $url_tag = "URL";
2425 my @url_list = git_get_project_url_list($project);
2426 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2427 foreach my $git_url (@url_list) {
2428 next unless $git_url;
2429 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2430 $url_tag = "";
2432 print "</table>\n";
2434 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2435 git_get_head_hash($project)
2436 or die_error(undef, "Open git-rev-list failed");
2437 my @revlist = map { chomp; $_ } <$fd>;
2438 close $fd;
2439 git_print_header_div('shortlog');
2440 git_shortlog_body(\@revlist, 0, 15, $refs,
2441 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2443 if (@taglist) {
2444 git_print_header_div('tags');
2445 git_tags_body(\@taglist, 0, 15,
2446 $cgi->a({-href => href(action=>"tags")}, "..."));
2449 if (@headlist) {
2450 git_print_header_div('heads');
2451 git_heads_body(\@headlist, $head, 0, 15,
2452 $cgi->a({-href => href(action=>"heads")}, "..."));
2455 git_footer_html();
2458 sub git_tag {
2459 my $head = git_get_head_hash($project);
2460 git_header_html();
2461 git_print_page_nav('','', $head,undef,$head);
2462 my %tag = parse_tag($hash);
2463 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2464 print "<div class=\"title_text\">\n" .
2465 "<table cellspacing=\"0\">\n" .
2466 "<tr>\n" .
2467 "<td>object</td>\n" .
2468 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2469 $tag{'object'}) . "</td>\n" .
2470 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2471 $tag{'type'}) . "</td>\n" .
2472 "</tr>\n";
2473 if (defined($tag{'author'})) {
2474 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2475 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2476 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2477 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2478 "</td></tr>\n";
2480 print "</table>\n\n" .
2481 "</div>\n";
2482 print "<div class=\"page_body\">";
2483 my $comment = $tag{'comment'};
2484 foreach my $line (@$comment) {
2485 print esc_html($line) . "<br/>\n";
2487 print "</div>\n";
2488 git_footer_html();
2491 sub git_blame2 {
2492 my $fd;
2493 my $ftype;
2495 my ($have_blame) = gitweb_check_feature('blame');
2496 if (!$have_blame) {
2497 die_error('403 Permission denied', "Permission denied");
2499 die_error('404 Not Found', "File name not defined") if (!$file_name);
2500 $hash_base ||= git_get_head_hash($project);
2501 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2502 my %co = parse_commit($hash_base)
2503 or die_error(undef, "Reading commit failed");
2504 if (!defined $hash) {
2505 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2506 or die_error(undef, "Error looking up file");
2508 $ftype = git_get_type($hash);
2509 if ($ftype !~ "blob") {
2510 die_error("400 Bad Request", "Object is not a blob");
2512 open ($fd, "-|", git_cmd(), "blame", '-l', '--', $file_name, $hash_base)
2513 or die_error(undef, "Open git-blame failed");
2514 git_header_html();
2515 my $formats_nav =
2516 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2517 "blob") .
2518 " | " .
2519 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2520 "history") .
2521 " | " .
2522 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2523 "HEAD");
2524 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2525 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2526 git_print_page_path($file_name, $ftype, $hash_base);
2527 my @rev_color = (qw(light2 dark2));
2528 my $num_colors = scalar(@rev_color);
2529 my $current_color = 0;
2530 my $last_rev;
2531 print <<HTML;
2532 <div class="page_body">
2533 <table class="blame">
2534 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2535 HTML
2536 while (<$fd>) {
2537 my ($full_rev, $author, $date, $lineno, $data) =
2538 /^([0-9a-f]{40}).*?\s\((.*?)\s+([-\d]+ [:\d]+ [-+\d]+)\s+(\d+)\)\s(.*)/;
2539 my $rev = substr($full_rev, 0, 8);
2540 my $print_c8 = 0;
2542 if (!defined $last_rev) {
2543 $last_rev = $full_rev;
2544 $print_c8 = 1;
2545 } elsif ($last_rev ne $full_rev) {
2546 $last_rev = $full_rev;
2547 $current_color = ++$current_color % $num_colors;
2548 $print_c8 = 1;
2550 print "<tr class=\"$rev_color[$current_color]\">\n";
2551 print "<td class=\"sha1\"";
2552 if ($print_c8 == 1) {
2553 print " title=\"$author, $date\"";
2555 print ">";
2556 if ($print_c8 == 1) {
2557 print $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2558 esc_html($rev));
2560 print "</td>\n";
2561 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2562 esc_html($lineno) . "</a></td>\n";
2563 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2564 print "</tr>\n";
2566 print "</table>\n";
2567 print "</div>";
2568 close $fd
2569 or print "Reading blob failed\n";
2570 git_footer_html();
2573 sub git_blame {
2574 my $fd;
2576 my ($have_blame) = gitweb_check_feature('blame');
2577 if (!$have_blame) {
2578 die_error('403 Permission denied', "Permission denied");
2580 die_error('404 Not Found', "File name not defined") if (!$file_name);
2581 $hash_base ||= git_get_head_hash($project);
2582 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2583 my %co = parse_commit($hash_base)
2584 or die_error(undef, "Reading commit failed");
2585 if (!defined $hash) {
2586 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2587 or die_error(undef, "Error lookup file");
2589 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2590 or die_error(undef, "Open git-annotate failed");
2591 git_header_html();
2592 my $formats_nav =
2593 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2594 "blob") .
2595 " | " .
2596 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2597 "history") .
2598 " | " .
2599 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2600 "HEAD");
2601 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2602 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2603 git_print_page_path($file_name, 'blob', $hash_base);
2604 print "<div class=\"page_body\">\n";
2605 print <<HTML;
2606 <table class="blame">
2607 <tr>
2608 <th>Commit</th>
2609 <th>Age</th>
2610 <th>Author</th>
2611 <th>Line</th>
2612 <th>Data</th>
2613 </tr>
2614 HTML
2615 my @line_class = (qw(light dark));
2616 my $line_class_len = scalar (@line_class);
2617 my $line_class_num = $#line_class;
2618 while (my $line = <$fd>) {
2619 my $long_rev;
2620 my $short_rev;
2621 my $author;
2622 my $time;
2623 my $lineno;
2624 my $data;
2625 my $age;
2626 my $age_str;
2627 my $age_class;
2629 chomp $line;
2630 $line_class_num = ($line_class_num + 1) % $line_class_len;
2632 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2633 $long_rev = $1;
2634 $author = $2;
2635 $time = $3;
2636 $lineno = $4;
2637 $data = $5;
2638 } else {
2639 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2640 next;
2642 $short_rev = substr ($long_rev, 0, 8);
2643 $age = time () - $time;
2644 $age_str = age_string ($age);
2645 $age_str =~ s/ /&nbsp;/g;
2646 $age_class = age_class($age);
2647 $author = esc_html ($author);
2648 $author =~ s/ /&nbsp;/g;
2650 $data = untabify($data);
2651 $data = esc_html ($data);
2653 print <<HTML;
2654 <tr class="$line_class[$line_class_num]">
2655 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2656 <td class="$age_class">$age_str</td>
2657 <td>$author</td>
2658 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2659 <td class="pre">$data</td>
2660 </tr>
2661 HTML
2662 } # while (my $line = <$fd>)
2663 print "</table>\n\n";
2664 close $fd
2665 or print "Reading blob failed.\n";
2666 print "</div>";
2667 git_footer_html();
2670 sub git_tags {
2671 my $head = git_get_head_hash($project);
2672 git_header_html();
2673 git_print_page_nav('','', $head,undef,$head);
2674 git_print_header_div('summary', $project);
2676 my ($taglist) = git_get_refs_list("tags");
2677 if (@$taglist) {
2678 git_tags_body($taglist);
2680 git_footer_html();
2683 sub git_heads {
2684 my $head = git_get_head_hash($project);
2685 git_header_html();
2686 git_print_page_nav('','', $head,undef,$head);
2687 git_print_header_div('summary', $project);
2689 my ($headlist) = git_get_refs_list("heads");
2690 if (@$headlist) {
2691 git_heads_body($headlist, $head);
2693 git_footer_html();
2696 sub git_blob_plain {
2697 my $expires;
2699 if (!defined $hash) {
2700 if (defined $file_name) {
2701 my $base = $hash_base || git_get_head_hash($project);
2702 $hash = git_get_hash_by_path($base, $file_name, "blob")
2703 or die_error(undef, "Error lookup file");
2704 } else {
2705 die_error(undef, "No file name defined");
2707 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2708 # blobs defined by non-textual hash id's can be cached
2709 $expires = "+1d";
2712 my $type = shift;
2713 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2714 or die_error(undef, "Couldn't cat $file_name, $hash");
2716 $type ||= blob_mimetype($fd, $file_name);
2718 # save as filename, even when no $file_name is given
2719 my $save_as = "$hash";
2720 if (defined $file_name) {
2721 $save_as = $file_name;
2722 } elsif ($type =~ m/^text\//) {
2723 $save_as .= '.txt';
2726 print $cgi->header(
2727 -type => "$type",
2728 -expires=>$expires,
2729 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2730 undef $/;
2731 binmode STDOUT, ':raw';
2732 print <$fd>;
2733 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2734 $/ = "\n";
2735 close $fd;
2738 sub git_blob {
2739 my $expires;
2741 if (!defined $hash) {
2742 if (defined $file_name) {
2743 my $base = $hash_base || git_get_head_hash($project);
2744 $hash = git_get_hash_by_path($base, $file_name, "blob")
2745 or die_error(undef, "Error lookup file");
2746 } else {
2747 die_error(undef, "No file name defined");
2749 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2750 # blobs defined by non-textual hash id's can be cached
2751 $expires = "+1d";
2754 my ($have_blame) = gitweb_check_feature('blame');
2755 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2756 or die_error(undef, "Couldn't cat $file_name, $hash");
2757 my $mimetype = blob_mimetype($fd, $file_name);
2758 if ($mimetype !~ m/^text\//) {
2759 close $fd;
2760 return git_blob_plain($mimetype);
2762 git_header_html(undef, $expires);
2763 my $formats_nav = '';
2764 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2765 if (defined $file_name) {
2766 if ($have_blame) {
2767 $formats_nav .=
2768 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2769 hash=>$hash, file_name=>$file_name)},
2770 "blame") .
2771 " | ";
2773 $formats_nav .=
2774 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2775 hash=>$hash, file_name=>$file_name)},
2776 "history") .
2777 " | " .
2778 $cgi->a({-href => href(action=>"blob_plain",
2779 hash=>$hash, file_name=>$file_name)},
2780 "raw") .
2781 " | " .
2782 $cgi->a({-href => href(action=>"blob",
2783 hash_base=>"HEAD", file_name=>$file_name)},
2784 "HEAD");
2785 } else {
2786 $formats_nav .=
2787 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2789 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2790 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2791 } else {
2792 print "<div class=\"page_nav\">\n" .
2793 "<br/><br/></div>\n" .
2794 "<div class=\"title\">$hash</div>\n";
2796 git_print_page_path($file_name, "blob", $hash_base);
2797 print "<div class=\"page_body\">\n";
2798 my $nr;
2799 while (my $line = <$fd>) {
2800 chomp $line;
2801 $nr++;
2802 $line = untabify($line);
2803 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2804 $nr, $nr, $nr, esc_html($line);
2806 close $fd
2807 or print "Reading blob failed.\n";
2808 print "</div>";
2809 git_footer_html();
2812 sub git_tree {
2813 my $have_snapshot = gitweb_have_snapshot();
2815 if (!defined $hash_base) {
2816 $hash_base = "HEAD";
2818 if (!defined $hash) {
2819 if (defined $file_name) {
2820 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2821 } else {
2822 $hash = $hash_base;
2825 $/ = "\0";
2826 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2827 or die_error(undef, "Open git-ls-tree failed");
2828 my @entries = map { chomp; $_ } <$fd>;
2829 close $fd or die_error(undef, "Reading tree failed");
2830 $/ = "\n";
2832 my $refs = git_get_references();
2833 my $ref = format_ref_marker($refs, $hash_base);
2834 git_header_html();
2835 my $base = "";
2836 my ($have_blame) = gitweb_check_feature('blame');
2837 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2838 my @views_nav = ();
2839 if (defined $file_name) {
2840 push @views_nav,
2841 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2842 hash=>$hash, file_name=>$file_name)},
2843 "history"),
2844 $cgi->a({-href => href(action=>"tree",
2845 hash_base=>"HEAD", file_name=>$file_name)},
2846 "HEAD"),
2848 if ($have_snapshot) {
2849 # FIXME: Should be available when we have no hash base as well.
2850 push @views_nav,
2851 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2852 "snapshot");
2854 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2855 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2856 } else {
2857 undef $hash_base;
2858 print "<div class=\"page_nav\">\n";
2859 print "<br/><br/></div>\n";
2860 print "<div class=\"title\">$hash</div>\n";
2862 if (defined $file_name) {
2863 $base = esc_html("$file_name/");
2865 git_print_page_path($file_name, 'tree', $hash_base);
2866 print "<div class=\"page_body\">\n";
2867 print "<table cellspacing=\"0\">\n";
2868 my $alternate = 1;
2869 foreach my $line (@entries) {
2870 my %t = parse_ls_tree_line($line, -z => 1);
2872 if ($alternate) {
2873 print "<tr class=\"dark\">\n";
2874 } else {
2875 print "<tr class=\"light\">\n";
2877 $alternate ^= 1;
2879 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2881 print "</tr>\n";
2883 print "</table>\n" .
2884 "</div>";
2885 git_footer_html();
2888 sub git_snapshot {
2889 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2890 my $have_snapshot = (defined $ctype && defined $suffix);
2891 if (!$have_snapshot) {
2892 die_error('403 Permission denied', "Permission denied");
2895 if (!defined $hash) {
2896 $hash = git_get_head_hash($project);
2899 my $filename = basename($project) . "-$hash.tar.$suffix";
2901 print $cgi->header(
2902 -type => 'application/x-tar',
2903 -content_encoding => $ctype,
2904 -content_disposition => 'inline; filename="' . "$filename" . '"',
2905 -status => '200 OK');
2907 my $git_command = git_cmd_str();
2908 open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2909 die_error(undef, "Execute git-tar-tree failed.");
2910 binmode STDOUT, ':raw';
2911 print <$fd>;
2912 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2913 close $fd;
2917 sub git_log {
2918 my $head = git_get_head_hash($project);
2919 if (!defined $hash) {
2920 $hash = $head;
2922 if (!defined $page) {
2923 $page = 0;
2925 my $refs = git_get_references();
2927 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2928 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2929 or die_error(undef, "Open git-rev-list failed");
2930 my @revlist = map { chomp; $_ } <$fd>;
2931 close $fd;
2933 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2935 git_header_html();
2936 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2938 if (!@revlist) {
2939 my %co = parse_commit($hash);
2941 git_print_header_div('summary', $project);
2942 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2944 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2945 my $commit = $revlist[$i];
2946 my $ref = format_ref_marker($refs, $commit);
2947 my %co = parse_commit($commit);
2948 next if !%co;
2949 my %ad = parse_date($co{'author_epoch'});
2950 git_print_header_div('commit',
2951 "<span class=\"age\">$co{'age_string'}</span>" .
2952 esc_html($co{'title'}) . $ref,
2953 $commit);
2954 print "<div class=\"title_text\">\n" .
2955 "<div class=\"log_link\">\n" .
2956 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2957 " | " .
2958 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2959 " | " .
2960 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
2961 "<br/>\n" .
2962 "</div>\n" .
2963 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2964 "</div>\n";
2966 print "<div class=\"log_body\">\n";
2967 git_print_simplified_log($co{'comment'});
2968 print "</div>\n";
2970 git_footer_html();
2973 sub git_commit {
2974 my %co = parse_commit($hash);
2975 if (!%co) {
2976 die_error(undef, "Unknown commit object");
2978 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2979 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2981 my $parent = $co{'parent'};
2982 if (!defined $parent) {
2983 $parent = "--root";
2985 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2986 or die_error(undef, "Open git-diff-tree failed");
2987 my @difftree = map { chomp; $_ } <$fd>;
2988 close $fd or die_error(undef, "Reading git-diff-tree failed");
2990 # non-textual hash id's can be cached
2991 my $expires;
2992 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2993 $expires = "+1d";
2995 my $refs = git_get_references();
2996 my $ref = format_ref_marker($refs, $co{'id'});
2998 my $have_snapshot = gitweb_have_snapshot();
3000 my @views_nav = ();
3001 if (defined $file_name && defined $co{'parent'}) {
3002 push @views_nav,
3003 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3004 "blame");
3006 if (defined $co{'parent'}) {
3007 push @views_nav,
3008 $cgi->a({-href => href(action=>"shortlog", hash=>$hash)}, "shortlog"),
3009 $cgi->a({-href => href(action=>"log", hash=>$hash)}, "log");
3011 git_header_html(undef, $expires);
3012 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
3013 $hash, $co{'tree'}, $hash,
3014 join (' | ', @views_nav));
3016 if (defined $co{'parent'}) {
3017 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3018 } else {
3019 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3021 print "<div class=\"title_text\">\n" .
3022 "<table cellspacing=\"0\">\n";
3023 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3024 "<tr>" .
3025 "<td></td><td> $ad{'rfc2822'}";
3026 if ($ad{'hour_local'} < 6) {
3027 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3028 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3029 } else {
3030 printf(" (%02d:%02d %s)",
3031 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3033 print "</td>" .
3034 "</tr>\n";
3035 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3036 print "<tr><td></td><td> $cd{'rfc2822'}" .
3037 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3038 "</td></tr>\n";
3039 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3040 print "<tr>" .
3041 "<td>tree</td>" .
3042 "<td class=\"sha1\">" .
3043 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3044 class => "list"}, $co{'tree'}) .
3045 "</td>" .
3046 "<td class=\"link\">" .
3047 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3048 "tree");
3049 if ($have_snapshot) {
3050 print " | " .
3051 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3053 print "</td>" .
3054 "</tr>\n";
3055 my $parents = $co{'parents'};
3056 foreach my $par (@$parents) {
3057 print "<tr>" .
3058 "<td>parent</td>" .
3059 "<td class=\"sha1\">" .
3060 $cgi->a({-href => href(action=>"commit", hash=>$par),
3061 class => "list"}, $par) .
3062 "</td>" .
3063 "<td class=\"link\">" .
3064 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3065 " | " .
3066 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3067 "</td>" .
3068 "</tr>\n";
3070 print "</table>".
3071 "</div>\n";
3073 print "<div class=\"page_body\">\n";
3074 git_print_log($co{'comment'});
3075 print "</div>\n";
3077 git_difftree_body(\@difftree, $hash, $parent);
3079 git_footer_html();
3082 sub git_blobdiff {
3083 my $format = shift || 'html';
3085 my $fd;
3086 my @difftree;
3087 my %diffinfo;
3088 my $expires;
3090 # preparing $fd and %diffinfo for git_patchset_body
3091 # new style URI
3092 if (defined $hash_base && defined $hash_parent_base) {
3093 if (defined $file_name) {
3094 # read raw output
3095 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3096 "--", $file_name
3097 or die_error(undef, "Open git-diff-tree failed");
3098 @difftree = map { chomp; $_ } <$fd>;
3099 close $fd
3100 or die_error(undef, "Reading git-diff-tree failed");
3101 @difftree
3102 or die_error('404 Not Found', "Blob diff not found");
3104 } elsif (defined $hash &&
3105 $hash =~ /[0-9a-fA-F]{40}/) {
3106 # try to find filename from $hash
3108 # read filtered raw output
3109 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3110 or die_error(undef, "Open git-diff-tree failed");
3111 @difftree =
3112 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3113 # $hash == to_id
3114 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3115 map { chomp; $_ } <$fd>;
3116 close $fd
3117 or die_error(undef, "Reading git-diff-tree failed");
3118 @difftree
3119 or die_error('404 Not Found', "Blob diff not found");
3121 } else {
3122 die_error('404 Not Found', "Missing one of the blob diff parameters");
3125 if (@difftree > 1) {
3126 die_error('404 Not Found', "Ambiguous blob diff specification");
3129 %diffinfo = parse_difftree_raw_line($difftree[0]);
3130 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3131 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3133 $hash_parent ||= $diffinfo{'from_id'};
3134 $hash ||= $diffinfo{'to_id'};
3136 # non-textual hash id's can be cached
3137 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3138 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3139 $expires = '+1d';
3142 # open patch output
3143 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3144 '-p', $hash_parent_base, $hash_base,
3145 "--", $file_name
3146 or die_error(undef, "Open git-diff-tree failed");
3149 # old/legacy style URI
3150 if (!%diffinfo && # if new style URI failed
3151 defined $hash && defined $hash_parent) {
3152 # fake git-diff-tree raw output
3153 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3154 $diffinfo{'from_id'} = $hash_parent;
3155 $diffinfo{'to_id'} = $hash;
3156 if (defined $file_name) {
3157 if (defined $file_parent) {
3158 $diffinfo{'status'} = '2';
3159 $diffinfo{'from_file'} = $file_parent;
3160 $diffinfo{'to_file'} = $file_name;
3161 } else { # assume not renamed
3162 $diffinfo{'status'} = '1';
3163 $diffinfo{'from_file'} = $file_name;
3164 $diffinfo{'to_file'} = $file_name;
3166 } else { # no filename given
3167 $diffinfo{'status'} = '2';
3168 $diffinfo{'from_file'} = $hash_parent;
3169 $diffinfo{'to_file'} = $hash;
3172 # non-textual hash id's can be cached
3173 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3174 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3175 $expires = '+1d';
3178 # open patch output
3179 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3180 or die_error(undef, "Open git-diff failed");
3181 } else {
3182 die_error('404 Not Found', "Missing one of the blob diff parameters")
3183 unless %diffinfo;
3186 # header
3187 if ($format eq 'html') {
3188 my $formats_nav =
3189 $cgi->a({-href => href(action=>"blobdiff_plain",
3190 hash=>$hash, hash_parent=>$hash_parent,
3191 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3192 file_name=>$file_name, file_parent=>$file_parent)},
3193 "raw");
3194 git_header_html(undef, $expires);
3195 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3196 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3197 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3198 } else {
3199 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3200 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3202 if (defined $file_name) {
3203 git_print_page_path($file_name, "blob", $hash_base);
3204 } else {
3205 print "<div class=\"page_path\"></div>\n";
3208 } elsif ($format eq 'plain') {
3209 print $cgi->header(
3210 -type => 'text/plain',
3211 -charset => 'utf-8',
3212 -expires => $expires,
3213 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3215 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3217 } else {
3218 die_error(undef, "Unknown blobdiff format");
3221 # patch
3222 if ($format eq 'html') {
3223 print "<div class=\"page_body\">\n";
3225 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3226 close $fd;
3228 print "</div>\n"; # class="page_body"
3229 git_footer_html();
3231 } else {
3232 while (my $line = <$fd>) {
3233 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3234 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3236 print $line;
3238 last if $line =~ m!^\+\+\+!;
3240 local $/ = undef;
3241 print <$fd>;
3242 close $fd;
3246 sub git_blobdiff_plain {
3247 git_blobdiff('plain');
3250 sub git_commitdiff {
3251 my $format = shift || 'html';
3252 my %co = parse_commit($hash);
3253 if (!%co) {
3254 die_error(undef, "Unknown commit object");
3256 if (!defined $hash_parent) {
3257 $hash_parent = $co{'parent'} || '--root';
3260 # read commitdiff
3261 my $fd;
3262 my @difftree;
3263 if ($format eq 'html') {
3264 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3265 "--patch-with-raw", "--full-index", $hash_parent, $hash
3266 or die_error(undef, "Open git-diff-tree failed");
3268 while (chomp(my $line = <$fd>)) {
3269 # empty line ends raw part of diff-tree output
3270 last unless $line;
3271 push @difftree, $line;
3274 } elsif ($format eq 'plain') {
3275 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3276 '-p', $hash_parent, $hash
3277 or die_error(undef, "Open git-diff-tree failed");
3279 } else {
3280 die_error(undef, "Unknown commitdiff format");
3283 # non-textual hash id's can be cached
3284 my $expires;
3285 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3286 $expires = "+1d";
3289 # write commit message
3290 if ($format eq 'html') {
3291 my $refs = git_get_references();
3292 my $ref = format_ref_marker($refs, $co{'id'});
3293 my $formats_nav =
3294 $cgi->a({-href => href(action=>"commitdiff_plain",
3295 hash=>$hash, hash_parent=>$hash_parent)},
3296 "raw");
3298 git_header_html(undef, $expires);
3299 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3300 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3301 git_print_authorship(\%co);
3302 print "<div class=\"page_body\">\n";
3303 print "<div class=\"log\">\n";
3304 git_print_simplified_log($co{'comment'}, 1); # skip title
3305 print "</div>\n"; # class="log"
3307 } elsif ($format eq 'plain') {
3308 my $refs = git_get_references("tags");
3309 my $tagname = git_get_rev_name_tags($hash);
3310 my $filename = basename($project) . "-$hash.patch";
3312 print $cgi->header(
3313 -type => 'text/plain',
3314 -charset => 'utf-8',
3315 -expires => $expires,
3316 -content_disposition => 'inline; filename="' . "$filename" . '"');
3317 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3318 print <<TEXT;
3319 From: $co{'author'}
3320 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3321 Subject: $co{'title'}
3322 TEXT
3323 print "X-Git-Tag: $tagname\n" if $tagname;
3324 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3326 foreach my $line (@{$co{'comment'}}) {
3327 print "$line\n";
3329 print "---\n\n";
3332 # write patch
3333 if ($format eq 'html') {
3334 git_difftree_body(\@difftree, $hash, $hash_parent);
3335 print "<br/>\n";
3337 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3338 close $fd;
3339 print "</div>\n"; # class="page_body"
3340 git_footer_html();
3342 } elsif ($format eq 'plain') {
3343 local $/ = undef;
3344 print <$fd>;
3345 close $fd
3346 or print "Reading git-diff-tree failed\n";
3350 sub git_commitdiff_plain {
3351 git_commitdiff('plain');
3354 sub git_history {
3355 if (!defined $hash_base) {
3356 $hash_base = git_get_head_hash($project);
3358 if (!defined $page) {
3359 $page = 0;
3361 my $ftype;
3362 my %co = parse_commit($hash_base);
3363 if (!%co) {
3364 die_error(undef, "Unknown commit object");
3367 my $refs = git_get_references();
3368 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3370 if (!defined $hash && defined $file_name) {
3371 $hash = git_get_hash_by_path($hash_base, $file_name);
3373 if (defined $hash) {
3374 $ftype = git_get_type($hash);
3377 open my $fd, "-|",
3378 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3379 or die_error(undef, "Open git-rev-list-failed");
3380 my @revlist = map { chomp; $_ } <$fd>;
3381 close $fd
3382 or die_error(undef, "Reading git-rev-list failed");
3384 my $paging_nav = '';
3385 if ($page > 0) {
3386 $paging_nav .=
3387 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3388 file_name=>$file_name)},
3389 "first");
3390 $paging_nav .= " &sdot; " .
3391 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3392 file_name=>$file_name, page=>$page-1),
3393 -accesskey => "p", -title => "Alt-p"}, "prev");
3394 } else {
3395 $paging_nav .= "first";
3396 $paging_nav .= " &sdot; prev";
3398 if ($#revlist >= (100 * ($page+1)-1)) {
3399 $paging_nav .= " &sdot; " .
3400 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3401 file_name=>$file_name, page=>$page+1),
3402 -accesskey => "n", -title => "Alt-n"}, "next");
3403 } else {
3404 $paging_nav .= " &sdot; next";
3406 my $next_link = '';
3407 if ($#revlist >= (100 * ($page+1)-1)) {
3408 $next_link =
3409 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3410 file_name=>$file_name, page=>$page+1),
3411 -title => "Alt-n"}, "next");
3414 git_header_html();
3415 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3416 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3417 git_print_page_path($file_name, $ftype, $hash_base);
3419 git_history_body(\@revlist, ($page * 100), $#revlist,
3420 $refs, $hash_base, $ftype, $next_link);
3422 git_footer_html();
3425 sub git_search {
3426 if (!defined $searchtext) {
3427 die_error(undef, "Text field empty");
3429 if (!defined $hash) {
3430 $hash = git_get_head_hash($project);
3432 my %co = parse_commit($hash);
3433 if (!%co) {
3434 die_error(undef, "Unknown commit object");
3437 my $commit_search = 1;
3438 my $author_search = 0;
3439 my $committer_search = 0;
3440 my $pickaxe_search = 0;
3441 if ($searchtext =~ s/^author\\://i) {
3442 $author_search = 1;
3443 } elsif ($searchtext =~ s/^committer\\://i) {
3444 $committer_search = 1;
3445 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3446 $commit_search = 0;
3447 $pickaxe_search = 1;
3449 # pickaxe may take all resources of your box and run for several minutes
3450 # with every query - so decide by yourself how public you make this feature
3451 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3452 if (!$have_pickaxe) {
3453 die_error('403 Permission denied', "Permission denied");
3456 git_header_html();
3457 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3458 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3460 print "<table cellspacing=\"0\">\n";
3461 my $alternate = 1;
3462 if ($commit_search) {
3463 $/ = "\0";
3464 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3465 while (my $commit_text = <$fd>) {
3466 if (!grep m/$searchtext/i, $commit_text) {
3467 next;
3469 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3470 next;
3472 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3473 next;
3475 my @commit_lines = split "\n", $commit_text;
3476 my %co = parse_commit(undef, \@commit_lines);
3477 if (!%co) {
3478 next;
3480 if ($alternate) {
3481 print "<tr class=\"dark\">\n";
3482 } else {
3483 print "<tr class=\"light\">\n";
3485 $alternate ^= 1;
3486 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3487 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3488 "<td>" .
3489 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3490 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3491 my $comment = $co{'comment'};
3492 foreach my $line (@$comment) {
3493 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3494 my $lead = esc_html($1) || "";
3495 $lead = chop_str($lead, 30, 10);
3496 my $match = esc_html($2) || "";
3497 my $trail = esc_html($3) || "";
3498 $trail = chop_str($trail, 30, 10);
3499 my $text = "$lead<span class=\"match\">$match</span>$trail";
3500 print chop_str($text, 80, 5) . "<br/>\n";
3503 print "</td>\n" .
3504 "<td class=\"link\">" .
3505 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3506 " | " .
3507 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3508 print "</td>\n" .
3509 "</tr>\n";
3511 close $fd;
3514 if ($pickaxe_search) {
3515 $/ = "\n";
3516 my $git_command = git_cmd_str();
3517 open my $fd, "-|", "$git_command rev-list $hash | " .
3518 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3519 undef %co;
3520 my @files;
3521 while (my $line = <$fd>) {
3522 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3523 my %set;
3524 $set{'file'} = $6;
3525 $set{'from_id'} = $3;
3526 $set{'to_id'} = $4;
3527 $set{'id'} = $set{'to_id'};
3528 if ($set{'id'} =~ m/0{40}/) {
3529 $set{'id'} = $set{'from_id'};
3531 if ($set{'id'} =~ m/0{40}/) {
3532 next;
3534 push @files, \%set;
3535 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3536 if (%co) {
3537 if ($alternate) {
3538 print "<tr class=\"dark\">\n";
3539 } else {
3540 print "<tr class=\"light\">\n";
3542 $alternate ^= 1;
3543 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3544 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3545 "<td>" .
3546 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3547 -class => "list subject"},
3548 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3549 while (my $setref = shift @files) {
3550 my %set = %$setref;
3551 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3552 hash=>$set{'id'}, file_name=>$set{'file'}),
3553 -class => "list"},
3554 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3555 "<br/>\n";
3557 print "</td>\n" .
3558 "<td class=\"link\">" .
3559 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3560 " | " .
3561 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3562 print "</td>\n" .
3563 "</tr>\n";
3565 %co = parse_commit($1);
3568 close $fd;
3570 print "</table>\n";
3571 git_footer_html();
3574 sub git_shortlog {
3575 my $head = git_get_head_hash($project);
3576 if (!defined $hash) {
3577 $hash = $head;
3579 if (!defined $page) {
3580 $page = 0;
3582 my $refs = git_get_references();
3584 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3585 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3586 or die_error(undef, "Open git-rev-list failed");
3587 my @revlist = map { chomp; $_ } <$fd>;
3588 close $fd;
3590 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3591 my $next_link = '';
3592 if ($#revlist >= (100 * ($page+1)-1)) {
3593 $next_link =
3594 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3595 -title => "Alt-n"}, "next");
3599 git_header_html();
3600 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3601 git_print_header_div('summary', $project);
3603 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3605 git_footer_html();
3608 ## ......................................................................
3609 ## feeds (RSS, OPML)
3611 sub git_rss {
3612 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3613 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3614 or die_error(undef, "Open git-rev-list failed");
3615 my @revlist = map { chomp; $_ } <$fd>;
3616 close $fd or die_error(undef, "Reading git-rev-list failed");
3617 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3618 print <<XML;
3619 <?xml version="1.0" encoding="utf-8"?>
3620 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3621 <channel>
3622 <title>$project $my_uri $my_url</title>
3623 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3624 <description>$project log</description>
3625 <language>en</language>
3628 for (my $i = 0; $i <= $#revlist; $i++) {
3629 my $commit = $revlist[$i];
3630 my %co = parse_commit($commit);
3631 # we read 150, we always show 30 and the ones more recent than 48 hours
3632 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3633 last;
3635 my %cd = parse_date($co{'committer_epoch'});
3636 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3637 $co{'parent'}, $co{'id'}
3638 or next;
3639 my @difftree = map { chomp; $_ } <$fd>;
3640 close $fd
3641 or next;
3642 print "<item>\n" .
3643 "<title>" .
3644 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3645 "</title>\n" .
3646 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3647 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3648 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3649 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3650 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3651 "<content:encoded>" .
3652 "<![CDATA[\n";
3653 my $comment = $co{'comment'};
3654 foreach my $line (@$comment) {
3655 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3656 print "$line<br/>\n";
3658 print "<br/>\n";
3659 foreach my $line (@difftree) {
3660 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3661 next;
3663 my $file = esc_html(unquote($7));
3664 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3665 print "$file<br/>\n";
3667 print "]]>\n" .
3668 "</content:encoded>\n" .
3669 "</item>\n";
3671 print "</channel></rss>";
3674 sub git_opml {
3675 my @list = git_get_projects_list();
3677 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3678 print <<XML;
3679 <?xml version="1.0" encoding="utf-8"?>
3680 <opml version="1.0">
3681 <head>
3682 <title>$site_name Git OPML Export</title>
3683 </head>
3684 <body>
3685 <outline text="git RSS feeds">
3688 foreach my $pr (@list) {
3689 my %proj = %$pr;
3690 my $head = git_get_head_hash($proj{'path'});
3691 if (!defined $head) {
3692 next;
3694 $git_dir = "$projectroot/$proj{'path'}";
3695 my %co = parse_commit($head);
3696 if (!%co) {
3697 next;
3700 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3701 my $rss = "$my_url?p=$proj{'path'};a=rss";
3702 my $html = "$my_url?p=$proj{'path'};a=summary";
3703 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3705 print <<XML;
3706 </outline>
3707 </body>
3708 </opml>