Gitweb - provide site headers and footers
[git/jrn.git] / gitweb / gitweb.perl
blobe4ebce6224f8f557c7a0eeb943a9c88390d93580
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 parse_commit {
1013 my $commit_id = shift;
1014 my $commit_text = shift;
1016 my @commit_lines;
1017 my %co;
1019 if (defined $commit_text) {
1020 @commit_lines = @$commit_text;
1021 } else {
1022 $/ = "\0";
1023 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
1024 or return;
1025 @commit_lines = split '\n', <$fd>;
1026 close $fd or return;
1027 $/ = "\n";
1028 pop @commit_lines;
1030 my $header = shift @commit_lines;
1031 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1032 return;
1034 ($co{'id'}, my @parents) = split ' ', $header;
1035 $co{'parents'} = \@parents;
1036 $co{'parent'} = $parents[0];
1037 while (my $line = shift @commit_lines) {
1038 last if $line eq "\n";
1039 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1040 $co{'tree'} = $1;
1041 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1042 $co{'author'} = $1;
1043 $co{'author_epoch'} = $2;
1044 $co{'author_tz'} = $3;
1045 if ($co{'author'} =~ m/^([^<]+) </) {
1046 $co{'author_name'} = $1;
1047 } else {
1048 $co{'author_name'} = $co{'author'};
1050 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1051 $co{'committer'} = $1;
1052 $co{'committer_epoch'} = $2;
1053 $co{'committer_tz'} = $3;
1054 $co{'committer_name'} = $co{'committer'};
1055 $co{'committer_name'} =~ s/ <.*//;
1058 if (!defined $co{'tree'}) {
1059 return;
1062 foreach my $title (@commit_lines) {
1063 $title =~ s/^ //;
1064 if ($title ne "") {
1065 $co{'title'} = chop_str($title, 80, 5);
1066 # remove leading stuff of merges to make the interesting part visible
1067 if (length($title) > 50) {
1068 $title =~ s/^Automatic //;
1069 $title =~ s/^merge (of|with) /Merge ... /i;
1070 if (length($title) > 50) {
1071 $title =~ s/(http|rsync):\/\///;
1073 if (length($title) > 50) {
1074 $title =~ s/(master|www|rsync)\.//;
1076 if (length($title) > 50) {
1077 $title =~ s/kernel.org:?//;
1079 if (length($title) > 50) {
1080 $title =~ s/\/pub\/scm//;
1083 $co{'title_short'} = chop_str($title, 50, 5);
1084 last;
1087 # remove added spaces
1088 foreach my $line (@commit_lines) {
1089 $line =~ s/^ //;
1091 $co{'comment'} = \@commit_lines;
1093 my $age = time - $co{'committer_epoch'};
1094 $co{'age'} = $age;
1095 $co{'age_string'} = age_string($age);
1096 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1097 if ($age > 60*60*24*7*2) {
1098 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1099 $co{'age_string_age'} = $co{'age_string'};
1100 } else {
1101 $co{'age_string_date'} = $co{'age_string'};
1102 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1104 return %co;
1107 # parse ref from ref_file, given by ref_id, with given type
1108 sub parse_ref {
1109 my $ref_file = shift;
1110 my $ref_id = shift;
1111 my $type = shift || git_get_type($ref_id);
1112 my %ref_item;
1114 $ref_item{'type'} = $type;
1115 $ref_item{'id'} = $ref_id;
1116 $ref_item{'epoch'} = 0;
1117 $ref_item{'age'} = "unknown";
1118 if ($type eq "tag") {
1119 my %tag = parse_tag($ref_id);
1120 $ref_item{'comment'} = $tag{'comment'};
1121 if ($tag{'type'} eq "commit") {
1122 my %co = parse_commit($tag{'object'});
1123 $ref_item{'epoch'} = $co{'committer_epoch'};
1124 $ref_item{'age'} = $co{'age_string'};
1125 } elsif (defined($tag{'epoch'})) {
1126 my $age = time - $tag{'epoch'};
1127 $ref_item{'epoch'} = $tag{'epoch'};
1128 $ref_item{'age'} = age_string($age);
1130 $ref_item{'reftype'} = $tag{'type'};
1131 $ref_item{'name'} = $tag{'name'};
1132 $ref_item{'refid'} = $tag{'object'};
1133 } elsif ($type eq "commit"){
1134 my %co = parse_commit($ref_id);
1135 $ref_item{'reftype'} = "commit";
1136 $ref_item{'name'} = $ref_file;
1137 $ref_item{'title'} = $co{'title'};
1138 $ref_item{'refid'} = $ref_id;
1139 $ref_item{'epoch'} = $co{'committer_epoch'};
1140 $ref_item{'age'} = $co{'age_string'};
1141 } else {
1142 $ref_item{'reftype'} = $type;
1143 $ref_item{'name'} = $ref_file;
1144 $ref_item{'refid'} = $ref_id;
1147 return %ref_item;
1150 # parse line of git-diff-tree "raw" output
1151 sub parse_difftree_raw_line {
1152 my $line = shift;
1153 my %res;
1155 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1156 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1157 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1158 $res{'from_mode'} = $1;
1159 $res{'to_mode'} = $2;
1160 $res{'from_id'} = $3;
1161 $res{'to_id'} = $4;
1162 $res{'status'} = $5;
1163 $res{'similarity'} = $6;
1164 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1165 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1166 } else {
1167 $res{'file'} = unquote($7);
1170 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1171 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1172 $res{'commit'} = $1;
1175 return wantarray ? %res : \%res;
1178 # parse line of git-ls-tree output
1179 sub parse_ls_tree_line ($;%) {
1180 my $line = shift;
1181 my %opts = @_;
1182 my %res;
1184 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1185 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1187 $res{'mode'} = $1;
1188 $res{'type'} = $2;
1189 $res{'hash'} = $3;
1190 if ($opts{'-z'}) {
1191 $res{'name'} = $4;
1192 } else {
1193 $res{'name'} = unquote($4);
1196 return wantarray ? %res : \%res;
1199 ## ......................................................................
1200 ## parse to array of hashes functions
1202 sub git_get_refs_list {
1203 my $type = shift || "";
1204 my %refs;
1205 my @reflist;
1207 my @refs;
1208 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1209 or return;
1210 while (my $line = <$fd>) {
1211 chomp $line;
1212 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1213 if (defined $refs{$1}) {
1214 push @{$refs{$1}}, $2;
1215 } else {
1216 $refs{$1} = [ $2 ];
1219 if (! $4) { # unpeeled, direct reference
1220 push @refs, { hash => $1, name => $3 }; # without type
1221 } elsif ($3 eq $refs[-1]{'name'}) {
1222 # most likely a tag is followed by its peeled
1223 # (deref) one, and when that happens we know the
1224 # previous one was of type 'tag'.
1225 $refs[-1]{'type'} = "tag";
1229 close $fd;
1231 foreach my $ref (@refs) {
1232 my $ref_file = $ref->{'name'};
1233 my $ref_id = $ref->{'hash'};
1235 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1236 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1238 push @reflist, \%ref_item;
1240 # sort refs by age
1241 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1242 return (\@reflist, \%refs);
1245 ## ----------------------------------------------------------------------
1246 ## filesystem-related functions
1248 sub get_file_owner {
1249 my $path = shift;
1251 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1252 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1253 if (!defined $gcos) {
1254 return undef;
1256 my $owner = $gcos;
1257 $owner =~ s/[,;].*$//;
1258 return decode("utf8", $owner, Encode::FB_DEFAULT);
1261 ## ......................................................................
1262 ## mimetype related functions
1264 sub mimetype_guess_file {
1265 my $filename = shift;
1266 my $mimemap = shift;
1267 -r $mimemap or return undef;
1269 my %mimemap;
1270 open(MIME, $mimemap) or return undef;
1271 while (<MIME>) {
1272 next if m/^#/; # skip comments
1273 my ($mime, $exts) = split(/\t+/);
1274 if (defined $exts) {
1275 my @exts = split(/\s+/, $exts);
1276 foreach my $ext (@exts) {
1277 $mimemap{$ext} = $mime;
1281 close(MIME);
1283 $filename =~ /\.([^.]*)$/;
1284 return $mimemap{$1};
1287 sub mimetype_guess {
1288 my $filename = shift;
1289 my $mime;
1290 $filename =~ /\./ or return undef;
1292 if ($mimetypes_file) {
1293 my $file = $mimetypes_file;
1294 if ($file !~ m!^/!) { # if it is relative path
1295 # it is relative to project
1296 $file = "$projectroot/$project/$file";
1298 $mime = mimetype_guess_file($filename, $file);
1300 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1301 return $mime;
1304 sub blob_mimetype {
1305 my $fd = shift;
1306 my $filename = shift;
1308 if ($filename) {
1309 my $mime = mimetype_guess($filename);
1310 $mime and return $mime;
1313 # just in case
1314 return $default_blob_plain_mimetype unless $fd;
1316 if (-T $fd) {
1317 return 'text/plain' .
1318 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1319 } elsif (! $filename) {
1320 return 'application/octet-stream';
1321 } elsif ($filename =~ m/\.png$/i) {
1322 return 'image/png';
1323 } elsif ($filename =~ m/\.gif$/i) {
1324 return 'image/gif';
1325 } elsif ($filename =~ m/\.jpe?g$/i) {
1326 return 'image/jpeg';
1327 } else {
1328 return 'application/octet-stream';
1332 ## ======================================================================
1333 ## functions printing HTML: header, footer, error page
1335 sub git_header_html {
1336 my $status = shift || "200 OK";
1337 my $expires = shift;
1339 my $title = "$site_name git";
1340 if (defined $project) {
1341 $title .= " - $project";
1342 if (defined $action) {
1343 $title .= "/$action";
1344 if (defined $file_name) {
1345 $title .= " - " . esc_html($file_name);
1346 if ($action eq "tree" && $file_name !~ m|/$|) {
1347 $title .= "/";
1352 my $content_type;
1353 # require explicit support from the UA if we are to send the page as
1354 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1355 # we have to do this because MSIE sometimes globs '*/*', pretending to
1356 # support xhtml+xml but choking when it gets what it asked for.
1357 if (defined $cgi->http('HTTP_ACCEPT') &&
1358 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1359 $cgi->Accept('application/xhtml+xml') != 0) {
1360 $content_type = 'application/xhtml+xml';
1361 } else {
1362 $content_type = 'text/html';
1364 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1365 -status=> $status, -expires => $expires);
1366 print <<EOF;
1367 <?xml version="1.0" encoding="utf-8"?>
1368 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1369 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1370 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1371 <!-- git core binaries version $git_version -->
1372 <head>
1373 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1374 <meta name="generator" content="gitweb/$version git/$git_version"/>
1375 <meta name="robots" content="index, nofollow"/>
1376 <title>$title</title>
1378 # print out each stylesheet that exist
1379 if (defined $stylesheet) {
1380 #provides backwards capability for those people who define style sheet in a config file
1381 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1382 } else {
1383 foreach my $stylesheet (@stylesheets) {
1384 next unless $stylesheet;
1385 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1388 if (defined $project) {
1389 printf('<link rel="alternate" title="%s log" '.
1390 'href="%s" type="application/rss+xml"/>'."\n",
1391 esc_param($project), href(action=>"rss"));
1392 } else {
1393 printf('<link rel="alternate" title="%s projects list" '.
1394 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1395 $site_name, href(project=>undef, action=>"project_index"));
1396 printf('<link rel="alternate" title="%s projects logs" '.
1397 'href="%s" type="text/x-opml"/>'."\n",
1398 $site_name, href(project=>undef, action=>"opml"));
1400 if (defined $favicon) {
1401 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1404 print "</head>\n" .
1405 "<body>\n";
1407 if (-f $site_header) {
1408 open (my $fd, $site_header);
1409 print <$fd>;
1410 close $fd;
1413 print "<div class=\"page_header\">\n" .
1414 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1415 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1416 "</a>\n";
1417 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1418 if (defined $project) {
1419 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1420 if (defined $action) {
1421 print " / $action";
1423 print "\n";
1424 if (!defined $searchtext) {
1425 $searchtext = "";
1427 my $search_hash;
1428 if (defined $hash_base) {
1429 $search_hash = $hash_base;
1430 } elsif (defined $hash) {
1431 $search_hash = $hash;
1432 } else {
1433 $search_hash = "HEAD";
1435 $cgi->param("a", "search");
1436 $cgi->param("h", $search_hash);
1437 print $cgi->startform(-method => "get", -action => $my_uri) .
1438 "<div class=\"search\">\n" .
1439 $cgi->hidden(-name => "p") . "\n" .
1440 $cgi->hidden(-name => "a") . "\n" .
1441 $cgi->hidden(-name => "h") . "\n" .
1442 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1443 "</div>" .
1444 $cgi->end_form() . "\n";
1446 print "</div>\n";
1449 sub git_footer_html {
1450 print "<div class=\"page_footer\">\n";
1451 if (defined $project) {
1452 my $descr = git_get_project_description($project);
1453 if (defined $descr) {
1454 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1456 print $cgi->a({-href => href(action=>"rss"),
1457 -class => "rss_logo"}, "RSS") . "\n";
1458 } else {
1459 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1460 -class => "rss_logo"}, "OPML") . " ";
1461 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1462 -class => "rss_logo"}, "TXT") . "\n";
1464 print "</div>\n" ;
1466 if (-f $site_footer) {
1467 open (my $fd, $site_footer);
1468 print <$fd>;
1469 close $fd;
1472 print "</body>\n" .
1473 "</html>";
1476 sub die_error {
1477 my $status = shift || "403 Forbidden";
1478 my $error = shift || "Malformed query, file missing or permission denied";
1480 git_header_html($status);
1481 print <<EOF;
1482 <div class="page_body">
1483 <br /><br />
1484 $status - $error
1485 <br />
1486 </div>
1488 git_footer_html();
1489 exit;
1492 ## ----------------------------------------------------------------------
1493 ## functions printing or outputting HTML: navigation
1495 sub git_print_page_nav {
1496 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1497 $extra = '' if !defined $extra; # pager or formats
1499 my @navs = qw(summary shortlog log commit commitdiff tree);
1500 if ($suppress) {
1501 @navs = grep { $_ ne $suppress } @navs;
1504 my %arg = map { $_ => {action=>$_} } @navs;
1505 if (defined $head) {
1506 for (qw(commit commitdiff)) {
1507 $arg{$_}{hash} = $head;
1509 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1510 for (qw(shortlog log)) {
1511 $arg{$_}{hash} = $head;
1515 $arg{tree}{hash} = $treehead if defined $treehead;
1516 $arg{tree}{hash_base} = $treebase if defined $treebase;
1518 print "<div class=\"page_nav\">\n" .
1519 (join " | ",
1520 map { $_ eq $current ?
1521 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1522 } @navs);
1523 print "<br/>\n$extra<br/>\n" .
1524 "</div>\n";
1527 sub format_paging_nav {
1528 my ($action, $hash, $head, $page, $nrevs) = @_;
1529 my $paging_nav;
1532 if ($hash ne $head || $page) {
1533 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1534 } else {
1535 $paging_nav .= "HEAD";
1538 if ($page > 0) {
1539 $paging_nav .= " &sdot; " .
1540 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1541 -accesskey => "p", -title => "Alt-p"}, "prev");
1542 } else {
1543 $paging_nav .= " &sdot; prev";
1546 if ($nrevs >= (100 * ($page+1)-1)) {
1547 $paging_nav .= " &sdot; " .
1548 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1549 -accesskey => "n", -title => "Alt-n"}, "next");
1550 } else {
1551 $paging_nav .= " &sdot; next";
1554 return $paging_nav;
1557 ## ......................................................................
1558 ## functions printing or outputting HTML: div
1560 sub git_print_header_div {
1561 my ($action, $title, $hash, $hash_base) = @_;
1562 my %args = ();
1564 $args{action} = $action;
1565 $args{hash} = $hash if $hash;
1566 $args{hash_base} = $hash_base if $hash_base;
1568 print "<div class=\"header\">\n" .
1569 $cgi->a({-href => href(%args), -class => "title"},
1570 $title ? $title : $action) .
1571 "\n</div>\n";
1574 #sub git_print_authorship (\%) {
1575 sub git_print_authorship {
1576 my $co = shift;
1578 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1579 print "<div class=\"author_date\">" .
1580 esc_html($co->{'author_name'}) .
1581 " [$ad{'rfc2822'}";
1582 if ($ad{'hour_local'} < 6) {
1583 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1584 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1585 } else {
1586 printf(" (%02d:%02d %s)",
1587 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1589 print "]</div>\n";
1592 sub git_print_page_path {
1593 my $name = shift;
1594 my $type = shift;
1595 my $hb = shift;
1597 if (!defined $name) {
1598 print "<div class=\"page_path\">/</div>\n";
1599 } else {
1600 my @dirname = split '/', $name;
1601 my $basename = pop @dirname;
1602 my $fullname = '';
1604 print "<div class=\"page_path\">";
1605 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1606 -title => 'tree root'}, "[$project]");
1607 print " / ";
1608 foreach my $dir (@dirname) {
1609 $fullname .= ($fullname ? '/' : '') . $dir;
1610 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1611 hash_base=>$hb),
1612 -title => $fullname}, esc_html($dir));
1613 print " / ";
1615 if (defined $type && $type eq 'blob') {
1616 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1617 hash_base=>$hb),
1618 -title => $name}, esc_html($basename));
1619 } elsif (defined $type && $type eq 'tree') {
1620 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1621 hash_base=>$hb),
1622 -title => $name}, esc_html($basename));
1623 } else {
1624 print esc_html($basename);
1626 print "<br/></div>\n";
1630 # sub git_print_log (\@;%) {
1631 sub git_print_log ($;%) {
1632 my $log = shift;
1633 my %opts = @_;
1635 if ($opts{'-remove_title'}) {
1636 # remove title, i.e. first line of log
1637 shift @$log;
1639 # remove leading empty lines
1640 while (defined $log->[0] && $log->[0] eq "") {
1641 shift @$log;
1644 # print log
1645 my $signoff = 0;
1646 my $empty = 0;
1647 foreach my $line (@$log) {
1648 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1649 $signoff = 1;
1650 $empty = 0;
1651 if (! $opts{'-remove_signoff'}) {
1652 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1653 next;
1654 } else {
1655 # remove signoff lines
1656 next;
1658 } else {
1659 $signoff = 0;
1662 # print only one empty line
1663 # do not print empty line after signoff
1664 if ($line eq "") {
1665 next if ($empty || $signoff);
1666 $empty = 1;
1667 } else {
1668 $empty = 0;
1671 print format_log_line_html($line) . "<br/>\n";
1674 if ($opts{'-final_empty_line'}) {
1675 # end with single empty line
1676 print "<br/>\n" unless $empty;
1680 sub git_print_simplified_log {
1681 my $log = shift;
1682 my $remove_title = shift;
1684 git_print_log($log,
1685 -final_empty_line=> 1,
1686 -remove_title => $remove_title);
1689 # print tree entry (row of git_tree), but without encompassing <tr> element
1690 sub git_print_tree_entry {
1691 my ($t, $basedir, $hash_base, $have_blame) = @_;
1693 my %base_key = ();
1694 $base_key{hash_base} = $hash_base if defined $hash_base;
1696 # The format of a table row is: mode list link. Where mode is
1697 # the mode of the entry, list is the name of the entry, an href,
1698 # and link is the action links of the entry.
1700 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1701 if ($t->{'type'} eq "blob") {
1702 print "<td class=\"list\">" .
1703 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1704 file_name=>"$basedir$t->{'name'}", %base_key),
1705 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1706 print "<td class=\"link\">";
1707 if ($have_blame) {
1708 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1709 file_name=>"$basedir$t->{'name'}", %base_key)},
1710 "blame");
1712 if (defined $hash_base) {
1713 if ($have_blame) {
1714 print " | ";
1716 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1717 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1718 "history");
1720 print " | " .
1721 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1722 file_name=>"$basedir$t->{'name'}")},
1723 "raw");
1724 print "</td>\n";
1726 } elsif ($t->{'type'} eq "tree") {
1727 print "<td class=\"list\">";
1728 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1729 file_name=>"$basedir$t->{'name'}", %base_key)},
1730 esc_html($t->{'name'}));
1731 print "</td>\n";
1732 print "<td class=\"link\">";
1733 if (defined $hash_base) {
1734 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1735 file_name=>"$basedir$t->{'name'}")},
1736 "history");
1738 print "</td>\n";
1742 ## ......................................................................
1743 ## functions printing large fragments of HTML
1745 sub git_difftree_body {
1746 my ($difftree, $hash, $parent) = @_;
1748 print "<div class=\"list_head\">\n";
1749 if ($#{$difftree} > 10) {
1750 print(($#{$difftree} + 1) . " files changed:\n");
1752 print "</div>\n";
1754 print "<table class=\"diff_tree\">\n";
1755 my $alternate = 1;
1756 my $patchno = 0;
1757 foreach my $line (@{$difftree}) {
1758 my %diff = parse_difftree_raw_line($line);
1760 if ($alternate) {
1761 print "<tr class=\"dark\">\n";
1762 } else {
1763 print "<tr class=\"light\">\n";
1765 $alternate ^= 1;
1767 my ($to_mode_oct, $to_mode_str, $to_file_type);
1768 my ($from_mode_oct, $from_mode_str, $from_file_type);
1769 if ($diff{'to_mode'} ne ('0' x 6)) {
1770 $to_mode_oct = oct $diff{'to_mode'};
1771 if (S_ISREG($to_mode_oct)) { # only for regular file
1772 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1774 $to_file_type = file_type($diff{'to_mode'});
1776 if ($diff{'from_mode'} ne ('0' x 6)) {
1777 $from_mode_oct = oct $diff{'from_mode'};
1778 if (S_ISREG($to_mode_oct)) { # only for regular file
1779 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1781 $from_file_type = file_type($diff{'from_mode'});
1784 if ($diff{'status'} eq "A") { # created
1785 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1786 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1787 $mode_chng .= "]</span>";
1788 print "<td>";
1789 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1790 hash_base=>$hash, file_name=>$diff{'file'}),
1791 -class => "list"}, esc_html($diff{'file'}));
1792 print "</td>\n";
1793 print "<td>$mode_chng</td>\n";
1794 print "<td class=\"link\">";
1795 if ($action eq 'commitdiff') {
1796 # link to patch
1797 $patchno++;
1798 print $cgi->a({-href => "#patch$patchno"}, "patch");
1800 print "</td>\n";
1802 } elsif ($diff{'status'} eq "D") { # deleted
1803 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1804 print "<td>";
1805 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1806 hash_base=>$parent, file_name=>$diff{'file'}),
1807 -class => "list"}, esc_html($diff{'file'}));
1808 print "</td>\n";
1809 print "<td>$mode_chng</td>\n";
1810 print "<td class=\"link\">";
1811 if ($action eq 'commitdiff') {
1812 # link to patch
1813 $patchno++;
1814 print $cgi->a({-href => "#patch$patchno"}, "patch");
1815 print " | ";
1817 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1818 file_name=>$diff{'file'})},
1819 "blame") . " | ";
1820 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1821 file_name=>$diff{'file'})},
1822 "history");
1823 print "</td>\n";
1825 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1826 my $mode_chnge = "";
1827 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1828 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1829 if ($from_file_type != $to_file_type) {
1830 $mode_chnge .= " from $from_file_type to $to_file_type";
1832 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1833 if ($from_mode_str && $to_mode_str) {
1834 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1835 } elsif ($to_mode_str) {
1836 $mode_chnge .= " mode: $to_mode_str";
1839 $mode_chnge .= "]</span>\n";
1841 print "<td>";
1842 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1843 hash_base=>$hash, file_name=>$diff{'file'}),
1844 -class => "list"}, esc_html($diff{'file'}));
1845 print "</td>\n";
1846 print "<td>$mode_chnge</td>\n";
1847 print "<td class=\"link\">";
1848 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1849 if ($action eq 'commitdiff') {
1850 # link to patch
1851 $patchno++;
1852 print $cgi->a({-href => "#patch$patchno"}, "patch");
1853 } else {
1854 print $cgi->a({-href => href(action=>"blobdiff",
1855 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1856 hash_base=>$hash, hash_parent_base=>$parent,
1857 file_name=>$diff{'file'})},
1858 "diff");
1860 print " | ";
1862 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1863 file_name=>$diff{'file'})},
1864 "blame") . " | ";
1865 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1866 file_name=>$diff{'file'})},
1867 "history");
1868 print "</td>\n";
1870 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1871 my %status_name = ('R' => 'moved', 'C' => 'copied');
1872 my $nstatus = $status_name{$diff{'status'}};
1873 my $mode_chng = "";
1874 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1875 # mode also for directories, so we cannot use $to_mode_str
1876 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1878 print "<td>" .
1879 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1880 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1881 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1882 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1883 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1884 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1885 -class => "list"}, esc_html($diff{'from_file'})) .
1886 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1887 "<td class=\"link\">";
1888 if ($diff{'to_id'} ne $diff{'from_id'}) {
1889 if ($action eq 'commitdiff') {
1890 # link to patch
1891 $patchno++;
1892 print $cgi->a({-href => "#patch$patchno"}, "patch");
1893 } else {
1894 print $cgi->a({-href => href(action=>"blobdiff",
1895 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1896 hash_base=>$hash, hash_parent_base=>$parent,
1897 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1898 "diff");
1900 print " | ";
1902 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1903 file_name=>$diff{'from_file'})},
1904 "blame") . " | ";
1905 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1906 file_name=>$diff{'from_file'})},
1907 "history");
1908 print "</td>\n";
1910 } # we should not encounter Unmerged (U) or Unknown (X) status
1911 print "</tr>\n";
1913 print "</table>\n";
1916 sub git_patchset_body {
1917 my ($fd, $difftree, $hash, $hash_parent) = @_;
1919 my $patch_idx = 0;
1920 my $in_header = 0;
1921 my $patch_found = 0;
1922 my $diffinfo;
1924 print "<div class=\"patchset\">\n";
1926 LINE:
1927 while (my $patch_line = <$fd>) {
1928 chomp $patch_line;
1930 if ($patch_line =~ m/^diff /) { # "git diff" header
1931 # beginning of patch (in patchset)
1932 if ($patch_found) {
1933 # close previous patch
1934 print "</div>\n"; # class="patch"
1935 } else {
1936 # first patch in patchset
1937 $patch_found = 1;
1939 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1941 if (ref($difftree->[$patch_idx]) eq "HASH") {
1942 $diffinfo = $difftree->[$patch_idx];
1943 } else {
1944 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1946 $patch_idx++;
1948 # for now, no extended header, hence we skip empty patches
1949 # companion to next LINE if $in_header;
1950 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1951 $in_header = 1;
1952 next LINE;
1955 if ($diffinfo->{'status'} eq "A") { # added
1956 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1957 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1958 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1959 $diffinfo->{'to_id'}) . "(new)" .
1960 "</div>\n"; # class="diff_info"
1962 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1963 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1964 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1965 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1966 $diffinfo->{'from_id'}) . "(deleted)" .
1967 "</div>\n"; # class="diff_info"
1969 } elsif ($diffinfo->{'status'} eq "R" || # renamed
1970 $diffinfo->{'status'} eq "C" || # copied
1971 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1972 print "<div class=\"diff_info\">" .
1973 file_type($diffinfo->{'from_mode'}) . ":" .
1974 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1975 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1976 $diffinfo->{'from_id'}) .
1977 " -> " .
1978 file_type($diffinfo->{'to_mode'}) . ":" .
1979 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1980 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1981 $diffinfo->{'to_id'});
1982 print "</div>\n"; # class="diff_info"
1984 } else { # modified, mode changed, ...
1985 print "<div class=\"diff_info\">" .
1986 file_type($diffinfo->{'from_mode'}) . ":" .
1987 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1988 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1989 $diffinfo->{'from_id'}) .
1990 " -> " .
1991 file_type($diffinfo->{'to_mode'}) . ":" .
1992 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1993 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1994 $diffinfo->{'to_id'});
1995 print "</div>\n"; # class="diff_info"
1998 #print "<div class=\"diff extended_header\">\n";
1999 $in_header = 1;
2000 next LINE;
2001 } # start of patch in patchset
2004 if ($in_header && $patch_line =~ m/^---/) {
2005 #print "</div>\n"; # class="diff extended_header"
2006 $in_header = 0;
2008 my $file = $diffinfo->{'from_file'};
2009 $file ||= $diffinfo->{'file'};
2010 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2011 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2012 -class => "list"}, esc_html($file));
2013 $patch_line =~ s|a/.*$|a/$file|g;
2014 print "<div class=\"diff from_file\">$patch_line</div>\n";
2016 $patch_line = <$fd>;
2017 chomp $patch_line;
2019 #$patch_line =~ m/^+++/;
2020 $file = $diffinfo->{'to_file'};
2021 $file ||= $diffinfo->{'file'};
2022 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2023 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2024 -class => "list"}, esc_html($file));
2025 $patch_line =~ s|b/.*|b/$file|g;
2026 print "<div class=\"diff to_file\">$patch_line</div>\n";
2028 next LINE;
2030 next LINE if $in_header;
2032 print format_diff_line($patch_line);
2034 print "</div>\n" if $patch_found; # class="patch"
2036 print "</div>\n"; # class="patchset"
2039 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2041 sub git_shortlog_body {
2042 # uses global variable $project
2043 my ($revlist, $from, $to, $refs, $extra) = @_;
2045 $from = 0 unless defined $from;
2046 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2048 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2049 my $alternate = 1;
2050 for (my $i = $from; $i <= $to; $i++) {
2051 my $commit = $revlist->[$i];
2052 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2053 my $ref = format_ref_marker($refs, $commit);
2054 my %co = parse_commit($commit);
2055 if ($alternate) {
2056 print "<tr class=\"dark\">\n";
2057 } else {
2058 print "<tr class=\"light\">\n";
2060 $alternate ^= 1;
2061 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2062 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2063 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2064 "<td>";
2065 print format_subject_html($co{'title'}, $co{'title_short'},
2066 href(action=>"commit", hash=>$commit), $ref);
2067 print "</td>\n" .
2068 "<td class=\"link\">" .
2069 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2070 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") . " | " .
2071 $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2072 print "</td>\n" .
2073 "</tr>\n";
2075 if (defined $extra) {
2076 print "<tr>\n" .
2077 "<td colspan=\"4\">$extra</td>\n" .
2078 "</tr>\n";
2080 print "</table>\n";
2083 sub git_history_body {
2084 # Warning: assumes constant type (blob or tree) during history
2085 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2087 $from = 0 unless defined $from;
2088 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2090 print "<table class=\"history\" cellspacing=\"0\">\n";
2091 my $alternate = 1;
2092 for (my $i = $from; $i <= $to; $i++) {
2093 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2094 next;
2097 my $commit = $1;
2098 my %co = parse_commit($commit);
2099 if (!%co) {
2100 next;
2103 my $ref = format_ref_marker($refs, $commit);
2105 if ($alternate) {
2106 print "<tr class=\"dark\">\n";
2107 } else {
2108 print "<tr class=\"light\">\n";
2110 $alternate ^= 1;
2111 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2112 # shortlog uses chop_str($co{'author_name'}, 10)
2113 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2114 "<td>";
2115 # originally git_history used chop_str($co{'title'}, 50)
2116 print format_subject_html($co{'title'}, $co{'title_short'},
2117 href(action=>"commit", hash=>$commit), $ref);
2118 print "</td>\n" .
2119 "<td class=\"link\">" .
2120 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2121 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2123 if ($ftype eq 'blob') {
2124 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2125 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2126 if (defined $blob_current && defined $blob_parent &&
2127 $blob_current ne $blob_parent) {
2128 print " | " .
2129 $cgi->a({-href => href(action=>"blobdiff",
2130 hash=>$blob_current, hash_parent=>$blob_parent,
2131 hash_base=>$hash_base, hash_parent_base=>$commit,
2132 file_name=>$file_name)},
2133 "diff to current");
2136 print "</td>\n" .
2137 "</tr>\n";
2139 if (defined $extra) {
2140 print "<tr>\n" .
2141 "<td colspan=\"4\">$extra</td>\n" .
2142 "</tr>\n";
2144 print "</table>\n";
2147 sub git_tags_body {
2148 # uses global variable $project
2149 my ($taglist, $from, $to, $extra) = @_;
2150 $from = 0 unless defined $from;
2151 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2153 print "<table class=\"tags\" cellspacing=\"0\">\n";
2154 my $alternate = 1;
2155 for (my $i = $from; $i <= $to; $i++) {
2156 my $entry = $taglist->[$i];
2157 my %tag = %$entry;
2158 my $comment_lines = $tag{'comment'};
2159 my $comment = shift @$comment_lines;
2160 my $comment_short;
2161 if (defined $comment) {
2162 $comment_short = chop_str($comment, 30, 5);
2164 if ($alternate) {
2165 print "<tr class=\"dark\">\n";
2166 } else {
2167 print "<tr class=\"light\">\n";
2169 $alternate ^= 1;
2170 print "<td><i>$tag{'age'}</i></td>\n" .
2171 "<td>" .
2172 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2173 -class => "list name"}, esc_html($tag{'name'})) .
2174 "</td>\n" .
2175 "<td>";
2176 if (defined $comment) {
2177 print format_subject_html($comment, $comment_short,
2178 href(action=>"tag", hash=>$tag{'id'}));
2180 print "</td>\n" .
2181 "<td class=\"selflink\">";
2182 if ($tag{'type'} eq "tag") {
2183 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2184 } else {
2185 print "&nbsp;";
2187 print "</td>\n" .
2188 "<td class=\"link\">" . " | " .
2189 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2190 if ($tag{'reftype'} eq "commit") {
2191 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2192 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2193 } elsif ($tag{'reftype'} eq "blob") {
2194 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2196 print "</td>\n" .
2197 "</tr>";
2199 if (defined $extra) {
2200 print "<tr>\n" .
2201 "<td colspan=\"5\">$extra</td>\n" .
2202 "</tr>\n";
2204 print "</table>\n";
2207 sub git_heads_body {
2208 # uses global variable $project
2209 my ($headlist, $head, $from, $to, $extra) = @_;
2210 $from = 0 unless defined $from;
2211 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2213 print "<table class=\"heads\" cellspacing=\"0\">\n";
2214 my $alternate = 1;
2215 for (my $i = $from; $i <= $to; $i++) {
2216 my $entry = $headlist->[$i];
2217 my %tag = %$entry;
2218 my $curr = $tag{'id'} eq $head;
2219 if ($alternate) {
2220 print "<tr class=\"dark\">\n";
2221 } else {
2222 print "<tr class=\"light\">\n";
2224 $alternate ^= 1;
2225 print "<td><i>$tag{'age'}</i></td>\n" .
2226 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2227 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2228 -class => "list name"},esc_html($tag{'name'})) .
2229 "</td>\n" .
2230 "<td class=\"link\">" .
2231 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2232 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2233 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2234 "</td>\n" .
2235 "</tr>";
2237 if (defined $extra) {
2238 print "<tr>\n" .
2239 "<td colspan=\"3\">$extra</td>\n" .
2240 "</tr>\n";
2242 print "</table>\n";
2245 ## ======================================================================
2246 ## ======================================================================
2247 ## actions
2249 sub git_project_list {
2250 my $order = $cgi->param('o');
2251 if (defined $order && $order !~ m/project|descr|owner|age/) {
2252 die_error(undef, "Unknown order parameter");
2255 my @list = git_get_projects_list();
2256 my @projects;
2257 if (!@list) {
2258 die_error(undef, "No projects found");
2260 foreach my $pr (@list) {
2261 my $head = git_get_head_hash($pr->{'path'});
2262 if (!defined $head) {
2263 next;
2265 $git_dir = "$projectroot/$pr->{'path'}";
2266 my %co = parse_commit($head);
2267 if (!%co) {
2268 next;
2270 $pr->{'commit'} = \%co;
2271 if (!defined $pr->{'descr'}) {
2272 my $descr = git_get_project_description($pr->{'path'}) || "";
2273 $pr->{'descr'} = chop_str($descr, 25, 5);
2275 if (!defined $pr->{'owner'}) {
2276 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2278 push @projects, $pr;
2281 git_header_html();
2282 if (-f $home_text) {
2283 print "<div class=\"index_include\">\n";
2284 open (my $fd, $home_text);
2285 print <$fd>;
2286 close $fd;
2287 print "</div>\n";
2289 print "<table class=\"project_list\">\n" .
2290 "<tr>\n";
2291 $order ||= "project";
2292 if ($order eq "project") {
2293 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2294 print "<th>Project</th>\n";
2295 } else {
2296 print "<th>" .
2297 $cgi->a({-href => href(project=>undef, order=>'project'),
2298 -class => "header"}, "Project") .
2299 "</th>\n";
2301 if ($order eq "descr") {
2302 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2303 print "<th>Description</th>\n";
2304 } else {
2305 print "<th>" .
2306 $cgi->a({-href => href(project=>undef, order=>'descr'),
2307 -class => "header"}, "Description") .
2308 "</th>\n";
2310 if ($order eq "owner") {
2311 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2312 print "<th>Owner</th>\n";
2313 } else {
2314 print "<th>" .
2315 $cgi->a({-href => href(project=>undef, order=>'owner'),
2316 -class => "header"}, "Owner") .
2317 "</th>\n";
2319 if ($order eq "age") {
2320 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2321 print "<th>Last Change</th>\n";
2322 } else {
2323 print "<th>" .
2324 $cgi->a({-href => href(project=>undef, order=>'age'),
2325 -class => "header"}, "Last Change") .
2326 "</th>\n";
2328 print "<th></th>\n" .
2329 "</tr>\n";
2330 my $alternate = 1;
2331 foreach my $pr (@projects) {
2332 if ($alternate) {
2333 print "<tr class=\"dark\">\n";
2334 } else {
2335 print "<tr class=\"light\">\n";
2337 $alternate ^= 1;
2338 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2339 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2340 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2341 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2342 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2343 $pr->{'commit'}{'age_string'} . "</td>\n" .
2344 "<td class=\"link\">" .
2345 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2346 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2347 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2348 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2349 "</td>\n" .
2350 "</tr>\n";
2352 print "</table>\n";
2353 git_footer_html();
2356 sub git_project_index {
2357 my @projects = git_get_projects_list();
2359 print $cgi->header(
2360 -type => 'text/plain',
2361 -charset => 'utf-8',
2362 -content_disposition => 'inline; filename="index.aux"');
2364 foreach my $pr (@projects) {
2365 if (!exists $pr->{'owner'}) {
2366 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2369 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2370 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2371 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2372 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2373 $path =~ s/ /\+/g;
2374 $owner =~ s/ /\+/g;
2376 print "$path $owner\n";
2380 sub git_summary {
2381 my $descr = git_get_project_description($project) || "none";
2382 my $head = git_get_head_hash($project);
2383 my %co = parse_commit($head);
2384 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2386 my $owner = git_get_project_owner($project);
2388 my ($reflist, $refs) = git_get_refs_list();
2390 my @taglist;
2391 my @headlist;
2392 foreach my $ref (@$reflist) {
2393 if ($ref->{'name'} =~ s!^heads/!!) {
2394 push @headlist, $ref;
2395 } else {
2396 $ref->{'name'} =~ s!^tags/!!;
2397 push @taglist, $ref;
2401 git_header_html();
2402 git_print_page_nav('summary','', $head);
2404 print "<div class=\"title\">&nbsp;</div>\n";
2405 print "<table cellspacing=\"0\">\n" .
2406 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2407 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2408 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2409 # use per project git URL list in $projectroot/$project/cloneurl
2410 # or make project git URL from git base URL and project name
2411 my $url_tag = "URL";
2412 my @url_list = git_get_project_url_list($project);
2413 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2414 foreach my $git_url (@url_list) {
2415 next unless $git_url;
2416 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2417 $url_tag = "";
2419 print "</table>\n";
2421 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2422 git_get_head_hash($project)
2423 or die_error(undef, "Open git-rev-list failed");
2424 my @revlist = map { chomp; $_ } <$fd>;
2425 close $fd;
2426 git_print_header_div('shortlog');
2427 git_shortlog_body(\@revlist, 0, 15, $refs,
2428 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2430 if (@taglist) {
2431 git_print_header_div('tags');
2432 git_tags_body(\@taglist, 0, 15,
2433 $cgi->a({-href => href(action=>"tags")}, "..."));
2436 if (@headlist) {
2437 git_print_header_div('heads');
2438 git_heads_body(\@headlist, $head, 0, 15,
2439 $cgi->a({-href => href(action=>"heads")}, "..."));
2442 git_footer_html();
2445 sub git_tag {
2446 my $head = git_get_head_hash($project);
2447 git_header_html();
2448 git_print_page_nav('','', $head,undef,$head);
2449 my %tag = parse_tag($hash);
2450 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2451 print "<div class=\"title_text\">\n" .
2452 "<table cellspacing=\"0\">\n" .
2453 "<tr>\n" .
2454 "<td>object</td>\n" .
2455 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2456 $tag{'object'}) . "</td>\n" .
2457 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2458 $tag{'type'}) . "</td>\n" .
2459 "</tr>\n";
2460 if (defined($tag{'author'})) {
2461 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2462 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2463 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2464 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2465 "</td></tr>\n";
2467 print "</table>\n\n" .
2468 "</div>\n";
2469 print "<div class=\"page_body\">";
2470 my $comment = $tag{'comment'};
2471 foreach my $line (@$comment) {
2472 print esc_html($line) . "<br/>\n";
2474 print "</div>\n";
2475 git_footer_html();
2478 sub git_blame2 {
2479 my $fd;
2480 my $ftype;
2482 my ($have_blame) = gitweb_check_feature('blame');
2483 if (!$have_blame) {
2484 die_error('403 Permission denied', "Permission denied");
2486 die_error('404 Not Found', "File name not defined") if (!$file_name);
2487 $hash_base ||= git_get_head_hash($project);
2488 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2489 my %co = parse_commit($hash_base)
2490 or die_error(undef, "Reading commit failed");
2491 if (!defined $hash) {
2492 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2493 or die_error(undef, "Error looking up file");
2495 $ftype = git_get_type($hash);
2496 if ($ftype !~ "blob") {
2497 die_error("400 Bad Request", "Object is not a blob");
2499 open ($fd, "-|", git_cmd(), "blame", '-l', '--', $file_name, $hash_base)
2500 or die_error(undef, "Open git-blame failed");
2501 git_header_html();
2502 my $formats_nav =
2503 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2504 "blob") .
2505 " | " .
2506 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2507 "history") .
2508 " | " .
2509 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2510 "HEAD");
2511 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2512 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2513 git_print_page_path($file_name, $ftype, $hash_base);
2514 my @rev_color = (qw(light2 dark2));
2515 my $num_colors = scalar(@rev_color);
2516 my $current_color = 0;
2517 my $last_rev;
2518 print <<HTML;
2519 <div class="page_body">
2520 <table class="blame">
2521 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2522 HTML
2523 while (<$fd>) {
2524 my ($full_rev, $author, $date, $lineno, $data) =
2525 /^([0-9a-f]{40}).*?\s\((.*?)\s+([-\d]+ [:\d]+ [-+\d]+)\s+(\d+)\)\s(.*)/;
2526 my $rev = substr($full_rev, 0, 8);
2527 my $print_c8 = 0;
2529 if (!defined $last_rev) {
2530 $last_rev = $full_rev;
2531 $print_c8 = 1;
2532 } elsif ($last_rev ne $full_rev) {
2533 $last_rev = $full_rev;
2534 $current_color = ++$current_color % $num_colors;
2535 $print_c8 = 1;
2537 print "<tr class=\"$rev_color[$current_color]\">\n";
2538 print "<td class=\"sha1\"";
2539 if ($print_c8 == 1) {
2540 print " title=\"$author, $date\"";
2542 print ">";
2543 if ($print_c8 == 1) {
2544 print $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2545 esc_html($rev));
2547 print "</td>\n";
2548 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2549 esc_html($lineno) . "</a></td>\n";
2550 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2551 print "</tr>\n";
2553 print "</table>\n";
2554 print "</div>";
2555 close $fd
2556 or print "Reading blob failed\n";
2557 git_footer_html();
2560 sub git_blame {
2561 my $fd;
2563 my ($have_blame) = gitweb_check_feature('blame');
2564 if (!$have_blame) {
2565 die_error('403 Permission denied', "Permission denied");
2567 die_error('404 Not Found', "File name not defined") if (!$file_name);
2568 $hash_base ||= git_get_head_hash($project);
2569 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2570 my %co = parse_commit($hash_base)
2571 or die_error(undef, "Reading commit failed");
2572 if (!defined $hash) {
2573 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2574 or die_error(undef, "Error lookup file");
2576 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2577 or die_error(undef, "Open git-annotate failed");
2578 git_header_html();
2579 my $formats_nav =
2580 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2581 "blob") .
2582 " | " .
2583 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2584 "history") .
2585 " | " .
2586 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2587 "HEAD");
2588 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2589 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2590 git_print_page_path($file_name, 'blob', $hash_base);
2591 print "<div class=\"page_body\">\n";
2592 print <<HTML;
2593 <table class="blame">
2594 <tr>
2595 <th>Commit</th>
2596 <th>Age</th>
2597 <th>Author</th>
2598 <th>Line</th>
2599 <th>Data</th>
2600 </tr>
2601 HTML
2602 my @line_class = (qw(light dark));
2603 my $line_class_len = scalar (@line_class);
2604 my $line_class_num = $#line_class;
2605 while (my $line = <$fd>) {
2606 my $long_rev;
2607 my $short_rev;
2608 my $author;
2609 my $time;
2610 my $lineno;
2611 my $data;
2612 my $age;
2613 my $age_str;
2614 my $age_class;
2616 chomp $line;
2617 $line_class_num = ($line_class_num + 1) % $line_class_len;
2619 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2620 $long_rev = $1;
2621 $author = $2;
2622 $time = $3;
2623 $lineno = $4;
2624 $data = $5;
2625 } else {
2626 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2627 next;
2629 $short_rev = substr ($long_rev, 0, 8);
2630 $age = time () - $time;
2631 $age_str = age_string ($age);
2632 $age_str =~ s/ /&nbsp;/g;
2633 $age_class = age_class($age);
2634 $author = esc_html ($author);
2635 $author =~ s/ /&nbsp;/g;
2637 $data = untabify($data);
2638 $data = esc_html ($data);
2640 print <<HTML;
2641 <tr class="$line_class[$line_class_num]">
2642 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2643 <td class="$age_class">$age_str</td>
2644 <td>$author</td>
2645 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2646 <td class="pre">$data</td>
2647 </tr>
2648 HTML
2649 } # while (my $line = <$fd>)
2650 print "</table>\n\n";
2651 close $fd
2652 or print "Reading blob failed.\n";
2653 print "</div>";
2654 git_footer_html();
2657 sub git_tags {
2658 my $head = git_get_head_hash($project);
2659 git_header_html();
2660 git_print_page_nav('','', $head,undef,$head);
2661 git_print_header_div('summary', $project);
2663 my ($taglist) = git_get_refs_list("tags");
2664 if (@$taglist) {
2665 git_tags_body($taglist);
2667 git_footer_html();
2670 sub git_heads {
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 ($headlist) = git_get_refs_list("heads");
2677 if (@$headlist) {
2678 git_heads_body($headlist, $head);
2680 git_footer_html();
2683 sub git_blob_plain {
2684 my $expires;
2686 if (!defined $hash) {
2687 if (defined $file_name) {
2688 my $base = $hash_base || git_get_head_hash($project);
2689 $hash = git_get_hash_by_path($base, $file_name, "blob")
2690 or die_error(undef, "Error lookup file");
2691 } else {
2692 die_error(undef, "No file name defined");
2694 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2695 # blobs defined by non-textual hash id's can be cached
2696 $expires = "+1d";
2699 my $type = shift;
2700 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2701 or die_error(undef, "Couldn't cat $file_name, $hash");
2703 $type ||= blob_mimetype($fd, $file_name);
2705 # save as filename, even when no $file_name is given
2706 my $save_as = "$hash";
2707 if (defined $file_name) {
2708 $save_as = $file_name;
2709 } elsif ($type =~ m/^text\//) {
2710 $save_as .= '.txt';
2713 print $cgi->header(
2714 -type => "$type",
2715 -expires=>$expires,
2716 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2717 undef $/;
2718 binmode STDOUT, ':raw';
2719 print <$fd>;
2720 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2721 $/ = "\n";
2722 close $fd;
2725 sub git_blob {
2726 my $expires;
2728 if (!defined $hash) {
2729 if (defined $file_name) {
2730 my $base = $hash_base || git_get_head_hash($project);
2731 $hash = git_get_hash_by_path($base, $file_name, "blob")
2732 or die_error(undef, "Error lookup file");
2733 } else {
2734 die_error(undef, "No file name defined");
2736 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2737 # blobs defined by non-textual hash id's can be cached
2738 $expires = "+1d";
2741 my ($have_blame) = gitweb_check_feature('blame');
2742 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2743 or die_error(undef, "Couldn't cat $file_name, $hash");
2744 my $mimetype = blob_mimetype($fd, $file_name);
2745 if ($mimetype !~ m/^text\//) {
2746 close $fd;
2747 return git_blob_plain($mimetype);
2749 git_header_html(undef, $expires);
2750 my $formats_nav = '';
2751 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2752 if (defined $file_name) {
2753 if ($have_blame) {
2754 $formats_nav .=
2755 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2756 hash=>$hash, file_name=>$file_name)},
2757 "blame") .
2758 " | ";
2760 $formats_nav .=
2761 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2762 hash=>$hash, file_name=>$file_name)},
2763 "history") .
2764 " | " .
2765 $cgi->a({-href => href(action=>"blob_plain",
2766 hash=>$hash, file_name=>$file_name)},
2767 "raw") .
2768 " | " .
2769 $cgi->a({-href => href(action=>"blob",
2770 hash_base=>"HEAD", file_name=>$file_name)},
2771 "HEAD");
2772 } else {
2773 $formats_nav .=
2774 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2776 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2777 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2778 } else {
2779 print "<div class=\"page_nav\">\n" .
2780 "<br/><br/></div>\n" .
2781 "<div class=\"title\">$hash</div>\n";
2783 git_print_page_path($file_name, "blob", $hash_base);
2784 print "<div class=\"page_body\">\n";
2785 my $nr;
2786 while (my $line = <$fd>) {
2787 chomp $line;
2788 $nr++;
2789 $line = untabify($line);
2790 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2791 $nr, $nr, $nr, esc_html($line);
2793 close $fd
2794 or print "Reading blob failed.\n";
2795 print "</div>";
2796 git_footer_html();
2799 sub git_tree {
2800 my $have_snapshot = gitweb_have_snapshot();
2802 if (!defined $hash_base) {
2803 $hash_base = "HEAD";
2805 if (!defined $hash) {
2806 if (defined $file_name) {
2807 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2808 } else {
2809 $hash = $hash_base;
2812 $/ = "\0";
2813 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2814 or die_error(undef, "Open git-ls-tree failed");
2815 my @entries = map { chomp; $_ } <$fd>;
2816 close $fd or die_error(undef, "Reading tree failed");
2817 $/ = "\n";
2819 my $refs = git_get_references();
2820 my $ref = format_ref_marker($refs, $hash_base);
2821 git_header_html();
2822 my $base = "";
2823 my ($have_blame) = gitweb_check_feature('blame');
2824 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2825 my @views_nav = ();
2826 if (defined $file_name) {
2827 push @views_nav,
2828 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2829 hash=>$hash, file_name=>$file_name)},
2830 "history"),
2831 $cgi->a({-href => href(action=>"tree",
2832 hash_base=>"HEAD", file_name=>$file_name)},
2833 "HEAD"),
2835 if ($have_snapshot) {
2836 # FIXME: Should be available when we have no hash base as well.
2837 push @views_nav,
2838 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2839 "snapshot");
2841 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2842 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2843 } else {
2844 undef $hash_base;
2845 print "<div class=\"page_nav\">\n";
2846 print "<br/><br/></div>\n";
2847 print "<div class=\"title\">$hash</div>\n";
2849 if (defined $file_name) {
2850 $base = esc_html("$file_name/");
2852 git_print_page_path($file_name, 'tree', $hash_base);
2853 print "<div class=\"page_body\">\n";
2854 print "<table cellspacing=\"0\">\n";
2855 my $alternate = 1;
2856 foreach my $line (@entries) {
2857 my %t = parse_ls_tree_line($line, -z => 1);
2859 if ($alternate) {
2860 print "<tr class=\"dark\">\n";
2861 } else {
2862 print "<tr class=\"light\">\n";
2864 $alternate ^= 1;
2866 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2868 print "</tr>\n";
2870 print "</table>\n" .
2871 "</div>";
2872 git_footer_html();
2875 sub git_snapshot {
2876 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2877 my $have_snapshot = (defined $ctype && defined $suffix);
2878 if (!$have_snapshot) {
2879 die_error('403 Permission denied', "Permission denied");
2882 if (!defined $hash) {
2883 $hash = git_get_head_hash($project);
2886 my $filename = basename($project) . "-$hash.tar.$suffix";
2888 print $cgi->header(
2889 -type => 'application/x-tar',
2890 -content_encoding => $ctype,
2891 -content_disposition => 'inline; filename="' . "$filename" . '"',
2892 -status => '200 OK');
2894 my $git_command = git_cmd_str();
2895 open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2896 die_error(undef, "Execute git-tar-tree failed.");
2897 binmode STDOUT, ':raw';
2898 print <$fd>;
2899 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2900 close $fd;
2904 sub git_log {
2905 my $head = git_get_head_hash($project);
2906 if (!defined $hash) {
2907 $hash = $head;
2909 if (!defined $page) {
2910 $page = 0;
2912 my $refs = git_get_references();
2914 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2915 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2916 or die_error(undef, "Open git-rev-list failed");
2917 my @revlist = map { chomp; $_ } <$fd>;
2918 close $fd;
2920 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2922 git_header_html();
2923 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2925 if (!@revlist) {
2926 my %co = parse_commit($hash);
2928 git_print_header_div('summary', $project);
2929 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2931 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2932 my $commit = $revlist[$i];
2933 my $ref = format_ref_marker($refs, $commit);
2934 my %co = parse_commit($commit);
2935 next if !%co;
2936 my %ad = parse_date($co{'author_epoch'});
2937 git_print_header_div('commit',
2938 "<span class=\"age\">$co{'age_string'}</span>" .
2939 esc_html($co{'title'}) . $ref,
2940 $commit);
2941 print "<div class=\"title_text\">\n" .
2942 "<div class=\"log_link\">\n" .
2943 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2944 " | " .
2945 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2946 " | " .
2947 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
2948 "<br/>\n" .
2949 "</div>\n" .
2950 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2951 "</div>\n";
2953 print "<div class=\"log_body\">\n";
2954 git_print_simplified_log($co{'comment'});
2955 print "</div>\n";
2957 git_footer_html();
2960 sub git_commit {
2961 my %co = parse_commit($hash);
2962 if (!%co) {
2963 die_error(undef, "Unknown commit object");
2965 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2966 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2968 my $parent = $co{'parent'};
2969 if (!defined $parent) {
2970 $parent = "--root";
2972 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2973 or die_error(undef, "Open git-diff-tree failed");
2974 my @difftree = map { chomp; $_ } <$fd>;
2975 close $fd or die_error(undef, "Reading git-diff-tree failed");
2977 # non-textual hash id's can be cached
2978 my $expires;
2979 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2980 $expires = "+1d";
2982 my $refs = git_get_references();
2983 my $ref = format_ref_marker($refs, $co{'id'});
2985 my $have_snapshot = gitweb_have_snapshot();
2987 my @views_nav = ();
2988 if (defined $file_name && defined $co{'parent'}) {
2989 push @views_nav,
2990 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2991 "blame");
2993 if (defined $co{'parent'}) {
2994 push @views_nav,
2995 $cgi->a({-href => href(action=>"shortlog", hash=>$hash)}, "shortlog"),
2996 $cgi->a({-href => href(action=>"log", hash=>$hash)}, "log");
2998 git_header_html(undef, $expires);
2999 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
3000 $hash, $co{'tree'}, $hash,
3001 join (' | ', @views_nav));
3003 if (defined $co{'parent'}) {
3004 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3005 } else {
3006 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3008 print "<div class=\"title_text\">\n" .
3009 "<table cellspacing=\"0\">\n";
3010 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3011 "<tr>" .
3012 "<td></td><td> $ad{'rfc2822'}";
3013 if ($ad{'hour_local'} < 6) {
3014 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3015 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3016 } else {
3017 printf(" (%02d:%02d %s)",
3018 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3020 print "</td>" .
3021 "</tr>\n";
3022 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3023 print "<tr><td></td><td> $cd{'rfc2822'}" .
3024 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3025 "</td></tr>\n";
3026 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3027 print "<tr>" .
3028 "<td>tree</td>" .
3029 "<td class=\"sha1\">" .
3030 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3031 class => "list"}, $co{'tree'}) .
3032 "</td>" .
3033 "<td class=\"link\">" .
3034 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3035 "tree");
3036 if ($have_snapshot) {
3037 print " | " .
3038 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3040 print "</td>" .
3041 "</tr>\n";
3042 my $parents = $co{'parents'};
3043 foreach my $par (@$parents) {
3044 print "<tr>" .
3045 "<td>parent</td>" .
3046 "<td class=\"sha1\">" .
3047 $cgi->a({-href => href(action=>"commit", hash=>$par),
3048 class => "list"}, $par) .
3049 "</td>" .
3050 "<td class=\"link\">" .
3051 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3052 " | " .
3053 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3054 "</td>" .
3055 "</tr>\n";
3057 print "</table>".
3058 "</div>\n";
3060 print "<div class=\"page_body\">\n";
3061 git_print_log($co{'comment'});
3062 print "</div>\n";
3064 git_difftree_body(\@difftree, $hash, $parent);
3066 git_footer_html();
3069 sub git_blobdiff {
3070 my $format = shift || 'html';
3072 my $fd;
3073 my @difftree;
3074 my %diffinfo;
3075 my $expires;
3077 # preparing $fd and %diffinfo for git_patchset_body
3078 # new style URI
3079 if (defined $hash_base && defined $hash_parent_base) {
3080 if (defined $file_name) {
3081 # read raw output
3082 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3083 "--", $file_name
3084 or die_error(undef, "Open git-diff-tree failed");
3085 @difftree = map { chomp; $_ } <$fd>;
3086 close $fd
3087 or die_error(undef, "Reading git-diff-tree failed");
3088 @difftree
3089 or die_error('404 Not Found', "Blob diff not found");
3091 } elsif (defined $hash &&
3092 $hash =~ /[0-9a-fA-F]{40}/) {
3093 # try to find filename from $hash
3095 # read filtered raw output
3096 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3097 or die_error(undef, "Open git-diff-tree failed");
3098 @difftree =
3099 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3100 # $hash == to_id
3101 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3102 map { chomp; $_ } <$fd>;
3103 close $fd
3104 or die_error(undef, "Reading git-diff-tree failed");
3105 @difftree
3106 or die_error('404 Not Found', "Blob diff not found");
3108 } else {
3109 die_error('404 Not Found', "Missing one of the blob diff parameters");
3112 if (@difftree > 1) {
3113 die_error('404 Not Found', "Ambiguous blob diff specification");
3116 %diffinfo = parse_difftree_raw_line($difftree[0]);
3117 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3118 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3120 $hash_parent ||= $diffinfo{'from_id'};
3121 $hash ||= $diffinfo{'to_id'};
3123 # non-textual hash id's can be cached
3124 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3125 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3126 $expires = '+1d';
3129 # open patch output
3130 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3131 '-p', $hash_parent_base, $hash_base,
3132 "--", $file_name
3133 or die_error(undef, "Open git-diff-tree failed");
3136 # old/legacy style URI
3137 if (!%diffinfo && # if new style URI failed
3138 defined $hash && defined $hash_parent) {
3139 # fake git-diff-tree raw output
3140 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3141 $diffinfo{'from_id'} = $hash_parent;
3142 $diffinfo{'to_id'} = $hash;
3143 if (defined $file_name) {
3144 if (defined $file_parent) {
3145 $diffinfo{'status'} = '2';
3146 $diffinfo{'from_file'} = $file_parent;
3147 $diffinfo{'to_file'} = $file_name;
3148 } else { # assume not renamed
3149 $diffinfo{'status'} = '1';
3150 $diffinfo{'from_file'} = $file_name;
3151 $diffinfo{'to_file'} = $file_name;
3153 } else { # no filename given
3154 $diffinfo{'status'} = '2';
3155 $diffinfo{'from_file'} = $hash_parent;
3156 $diffinfo{'to_file'} = $hash;
3159 # non-textual hash id's can be cached
3160 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3161 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3162 $expires = '+1d';
3165 # open patch output
3166 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3167 or die_error(undef, "Open git-diff failed");
3168 } else {
3169 die_error('404 Not Found', "Missing one of the blob diff parameters")
3170 unless %diffinfo;
3173 # header
3174 if ($format eq 'html') {
3175 my $formats_nav =
3176 $cgi->a({-href => href(action=>"blobdiff_plain",
3177 hash=>$hash, hash_parent=>$hash_parent,
3178 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3179 file_name=>$file_name, file_parent=>$file_parent)},
3180 "raw");
3181 git_header_html(undef, $expires);
3182 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3183 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3184 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3185 } else {
3186 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3187 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3189 if (defined $file_name) {
3190 git_print_page_path($file_name, "blob", $hash_base);
3191 } else {
3192 print "<div class=\"page_path\"></div>\n";
3195 } elsif ($format eq 'plain') {
3196 print $cgi->header(
3197 -type => 'text/plain',
3198 -charset => 'utf-8',
3199 -expires => $expires,
3200 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3202 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3204 } else {
3205 die_error(undef, "Unknown blobdiff format");
3208 # patch
3209 if ($format eq 'html') {
3210 print "<div class=\"page_body\">\n";
3212 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3213 close $fd;
3215 print "</div>\n"; # class="page_body"
3216 git_footer_html();
3218 } else {
3219 while (my $line = <$fd>) {
3220 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3221 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3223 print $line;
3225 last if $line =~ m!^\+\+\+!;
3227 local $/ = undef;
3228 print <$fd>;
3229 close $fd;
3233 sub git_blobdiff_plain {
3234 git_blobdiff('plain');
3237 sub git_commitdiff {
3238 my $format = shift || 'html';
3239 my %co = parse_commit($hash);
3240 if (!%co) {
3241 die_error(undef, "Unknown commit object");
3243 if (!defined $hash_parent) {
3244 $hash_parent = $co{'parent'} || '--root';
3247 # read commitdiff
3248 my $fd;
3249 my @difftree;
3250 if ($format eq 'html') {
3251 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3252 "--patch-with-raw", "--full-index", $hash_parent, $hash
3253 or die_error(undef, "Open git-diff-tree failed");
3255 while (chomp(my $line = <$fd>)) {
3256 # empty line ends raw part of diff-tree output
3257 last unless $line;
3258 push @difftree, $line;
3261 } elsif ($format eq 'plain') {
3262 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3263 '-p', $hash_parent, $hash
3264 or die_error(undef, "Open git-diff-tree failed");
3266 } else {
3267 die_error(undef, "Unknown commitdiff format");
3270 # non-textual hash id's can be cached
3271 my $expires;
3272 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3273 $expires = "+1d";
3276 # write commit message
3277 if ($format eq 'html') {
3278 my $refs = git_get_references();
3279 my $ref = format_ref_marker($refs, $co{'id'});
3280 my $formats_nav =
3281 $cgi->a({-href => href(action=>"commitdiff_plain",
3282 hash=>$hash, hash_parent=>$hash_parent)},
3283 "raw");
3285 git_header_html(undef, $expires);
3286 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3287 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3288 git_print_authorship(\%co);
3289 print "<div class=\"page_body\">\n";
3290 print "<div class=\"log\">\n";
3291 git_print_simplified_log($co{'comment'}, 1); # skip title
3292 print "</div>\n"; # class="log"
3294 } elsif ($format eq 'plain') {
3295 my $refs = git_get_references("tags");
3296 my $tagname = git_get_rev_name_tags($hash);
3297 my $filename = basename($project) . "-$hash.patch";
3299 print $cgi->header(
3300 -type => 'text/plain',
3301 -charset => 'utf-8',
3302 -expires => $expires,
3303 -content_disposition => 'inline; filename="' . "$filename" . '"');
3304 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3305 print <<TEXT;
3306 From: $co{'author'}
3307 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3308 Subject: $co{'title'}
3309 TEXT
3310 print "X-Git-Tag: $tagname\n" if $tagname;
3311 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3313 foreach my $line (@{$co{'comment'}}) {
3314 print "$line\n";
3316 print "---\n\n";
3319 # write patch
3320 if ($format eq 'html') {
3321 git_difftree_body(\@difftree, $hash, $hash_parent);
3322 print "<br/>\n";
3324 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3325 close $fd;
3326 print "</div>\n"; # class="page_body"
3327 git_footer_html();
3329 } elsif ($format eq 'plain') {
3330 local $/ = undef;
3331 print <$fd>;
3332 close $fd
3333 or print "Reading git-diff-tree failed\n";
3337 sub git_commitdiff_plain {
3338 git_commitdiff('plain');
3341 sub git_history {
3342 if (!defined $hash_base) {
3343 $hash_base = git_get_head_hash($project);
3345 if (!defined $page) {
3346 $page = 0;
3348 my $ftype;
3349 my %co = parse_commit($hash_base);
3350 if (!%co) {
3351 die_error(undef, "Unknown commit object");
3354 my $refs = git_get_references();
3355 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3357 if (!defined $hash && defined $file_name) {
3358 $hash = git_get_hash_by_path($hash_base, $file_name);
3360 if (defined $hash) {
3361 $ftype = git_get_type($hash);
3364 open my $fd, "-|",
3365 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3366 or die_error(undef, "Open git-rev-list-failed");
3367 my @revlist = map { chomp; $_ } <$fd>;
3368 close $fd
3369 or die_error(undef, "Reading git-rev-list failed");
3371 my $paging_nav = '';
3372 if ($page > 0) {
3373 $paging_nav .=
3374 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3375 file_name=>$file_name)},
3376 "first");
3377 $paging_nav .= " &sdot; " .
3378 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3379 file_name=>$file_name, page=>$page-1),
3380 -accesskey => "p", -title => "Alt-p"}, "prev");
3381 } else {
3382 $paging_nav .= "first";
3383 $paging_nav .= " &sdot; prev";
3385 if ($#revlist >= (100 * ($page+1)-1)) {
3386 $paging_nav .= " &sdot; " .
3387 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3388 file_name=>$file_name, page=>$page+1),
3389 -accesskey => "n", -title => "Alt-n"}, "next");
3390 } else {
3391 $paging_nav .= " &sdot; next";
3393 my $next_link = '';
3394 if ($#revlist >= (100 * ($page+1)-1)) {
3395 $next_link =
3396 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3397 file_name=>$file_name, page=>$page+1),
3398 -title => "Alt-n"}, "next");
3401 git_header_html();
3402 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3403 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3404 git_print_page_path($file_name, $ftype, $hash_base);
3406 git_history_body(\@revlist, ($page * 100), $#revlist,
3407 $refs, $hash_base, $ftype, $next_link);
3409 git_footer_html();
3412 sub git_search {
3413 if (!defined $searchtext) {
3414 die_error(undef, "Text field empty");
3416 if (!defined $hash) {
3417 $hash = git_get_head_hash($project);
3419 my %co = parse_commit($hash);
3420 if (!%co) {
3421 die_error(undef, "Unknown commit object");
3424 my $commit_search = 1;
3425 my $author_search = 0;
3426 my $committer_search = 0;
3427 my $pickaxe_search = 0;
3428 if ($searchtext =~ s/^author\\://i) {
3429 $author_search = 1;
3430 } elsif ($searchtext =~ s/^committer\\://i) {
3431 $committer_search = 1;
3432 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3433 $commit_search = 0;
3434 $pickaxe_search = 1;
3436 # pickaxe may take all resources of your box and run for several minutes
3437 # with every query - so decide by yourself how public you make this feature
3438 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3439 if (!$have_pickaxe) {
3440 die_error('403 Permission denied', "Permission denied");
3443 git_header_html();
3444 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3445 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3447 print "<table cellspacing=\"0\">\n";
3448 my $alternate = 1;
3449 if ($commit_search) {
3450 $/ = "\0";
3451 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3452 while (my $commit_text = <$fd>) {
3453 if (!grep m/$searchtext/i, $commit_text) {
3454 next;
3456 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3457 next;
3459 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3460 next;
3462 my @commit_lines = split "\n", $commit_text;
3463 my %co = parse_commit(undef, \@commit_lines);
3464 if (!%co) {
3465 next;
3467 if ($alternate) {
3468 print "<tr class=\"dark\">\n";
3469 } else {
3470 print "<tr class=\"light\">\n";
3472 $alternate ^= 1;
3473 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3474 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3475 "<td>" .
3476 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3477 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3478 my $comment = $co{'comment'};
3479 foreach my $line (@$comment) {
3480 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3481 my $lead = esc_html($1) || "";
3482 $lead = chop_str($lead, 30, 10);
3483 my $match = esc_html($2) || "";
3484 my $trail = esc_html($3) || "";
3485 $trail = chop_str($trail, 30, 10);
3486 my $text = "$lead<span class=\"match\">$match</span>$trail";
3487 print chop_str($text, 80, 5) . "<br/>\n";
3490 print "</td>\n" .
3491 "<td class=\"link\">" .
3492 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3493 " | " .
3494 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3495 print "</td>\n" .
3496 "</tr>\n";
3498 close $fd;
3501 if ($pickaxe_search) {
3502 $/ = "\n";
3503 my $git_command = git_cmd_str();
3504 open my $fd, "-|", "$git_command rev-list $hash | " .
3505 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3506 undef %co;
3507 my @files;
3508 while (my $line = <$fd>) {
3509 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3510 my %set;
3511 $set{'file'} = $6;
3512 $set{'from_id'} = $3;
3513 $set{'to_id'} = $4;
3514 $set{'id'} = $set{'to_id'};
3515 if ($set{'id'} =~ m/0{40}/) {
3516 $set{'id'} = $set{'from_id'};
3518 if ($set{'id'} =~ m/0{40}/) {
3519 next;
3521 push @files, \%set;
3522 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3523 if (%co) {
3524 if ($alternate) {
3525 print "<tr class=\"dark\">\n";
3526 } else {
3527 print "<tr class=\"light\">\n";
3529 $alternate ^= 1;
3530 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3531 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3532 "<td>" .
3533 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3534 -class => "list subject"},
3535 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3536 while (my $setref = shift @files) {
3537 my %set = %$setref;
3538 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3539 hash=>$set{'id'}, file_name=>$set{'file'}),
3540 -class => "list"},
3541 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3542 "<br/>\n";
3544 print "</td>\n" .
3545 "<td class=\"link\">" .
3546 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3547 " | " .
3548 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3549 print "</td>\n" .
3550 "</tr>\n";
3552 %co = parse_commit($1);
3555 close $fd;
3557 print "</table>\n";
3558 git_footer_html();
3561 sub git_shortlog {
3562 my $head = git_get_head_hash($project);
3563 if (!defined $hash) {
3564 $hash = $head;
3566 if (!defined $page) {
3567 $page = 0;
3569 my $refs = git_get_references();
3571 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3572 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3573 or die_error(undef, "Open git-rev-list failed");
3574 my @revlist = map { chomp; $_ } <$fd>;
3575 close $fd;
3577 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3578 my $next_link = '';
3579 if ($#revlist >= (100 * ($page+1)-1)) {
3580 $next_link =
3581 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3582 -title => "Alt-n"}, "next");
3586 git_header_html();
3587 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3588 git_print_header_div('summary', $project);
3590 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3592 git_footer_html();
3595 ## ......................................................................
3596 ## feeds (RSS, OPML)
3598 sub git_rss {
3599 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3600 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3601 or die_error(undef, "Open git-rev-list failed");
3602 my @revlist = map { chomp; $_ } <$fd>;
3603 close $fd or die_error(undef, "Reading git-rev-list failed");
3604 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3605 print <<XML;
3606 <?xml version="1.0" encoding="utf-8"?>
3607 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3608 <channel>
3609 <title>$project $my_uri $my_url</title>
3610 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3611 <description>$project log</description>
3612 <language>en</language>
3615 for (my $i = 0; $i <= $#revlist; $i++) {
3616 my $commit = $revlist[$i];
3617 my %co = parse_commit($commit);
3618 # we read 150, we always show 30 and the ones more recent than 48 hours
3619 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3620 last;
3622 my %cd = parse_date($co{'committer_epoch'});
3623 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3624 $co{'parent'}, $co{'id'}
3625 or next;
3626 my @difftree = map { chomp; $_ } <$fd>;
3627 close $fd
3628 or next;
3629 print "<item>\n" .
3630 "<title>" .
3631 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3632 "</title>\n" .
3633 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3634 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3635 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3636 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3637 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3638 "<content:encoded>" .
3639 "<![CDATA[\n";
3640 my $comment = $co{'comment'};
3641 foreach my $line (@$comment) {
3642 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3643 print "$line<br/>\n";
3645 print "<br/>\n";
3646 foreach my $line (@difftree) {
3647 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3648 next;
3650 my $file = esc_html(unquote($7));
3651 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3652 print "$file<br/>\n";
3654 print "]]>\n" .
3655 "</content:encoded>\n" .
3656 "</item>\n";
3658 print "</channel></rss>";
3661 sub git_opml {
3662 my @list = git_get_projects_list();
3664 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3665 print <<XML;
3666 <?xml version="1.0" encoding="utf-8"?>
3667 <opml version="1.0">
3668 <head>
3669 <title>$site_name Git OPML Export</title>
3670 </head>
3671 <body>
3672 <outline text="git RSS feeds">
3675 foreach my $pr (@list) {
3676 my %proj = %$pr;
3677 my $head = git_get_head_hash($proj{'path'});
3678 if (!defined $head) {
3679 next;
3681 $git_dir = "$projectroot/$proj{'path'}";
3682 my %co = parse_commit($head);
3683 if (!%co) {
3684 next;
3687 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3688 my $rss = "$my_url?p=$proj{'path'};a=rss";
3689 my $html = "$my_url?p=$proj{'path'};a=summary";
3690 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3692 print <<XML;
3693 </outline>
3694 </body>
3695 </opml>