gitweb: Don't use quotemeta on internally generated strings
[git/raj.git] / gitweb / gitweb.perl
bloba99e116a4a6ec487d5a0118e2ef845215341404a
1 #!/usr/bin/perl
3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
21 our $cgi = new CGI;
22 our $version = "++GIT_VERSION++";
23 our $my_url = $cgi->url();
24 our $my_uri = $cgi->url(-absolute => 1);
26 # core git executable to use
27 # this can just be "git" if your webserver has a sensible PATH
28 our $GIT = "++GIT_BINDIR++/git";
30 # absolute fs-path which will be prepended to the project path
31 #our $projectroot = "/pub/scm";
32 our $projectroot = "++GITWEB_PROJECTROOT++";
34 # target of the home link on top of all pages
35 our $home_link = $my_uri || "/";
37 # string of the home link on top of all pages
38 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
40 # name of your site or organization to appear in page titles
41 # replace this with something more descriptive for clearer bookmarks
42 our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
44 # html text to include at home page
45 our $home_text = "++GITWEB_HOMETEXT++";
47 # URI of default stylesheet
48 our $stylesheet = "++GITWEB_CSS++";
49 # URI of GIT logo
50 our $logo = "++GITWEB_LOGO++";
51 # URI of GIT favicon, assumed to be image/png type
52 our $favicon = "++GITWEB_FAVICON++";
54 # source of projects list
55 our $projects_list = "++GITWEB_LIST++";
57 # show repository only if this file exists
58 # (only effective if this variable evaluates to true)
59 our $export_ok = "++GITWEB_EXPORT_OK++";
61 # only allow viewing of repositories also shown on the overview page
62 our $strict_export = "++GITWEB_STRICT_EXPORT++";
64 # list of git base URLs used for URL to where fetch project from,
65 # i.e. full URL is "$git_base_url/$project"
66 our @git_base_url_list = ("++GITWEB_BASE_URL++");
68 # default blob_plain mimetype and default charset for text/plain blob
69 our $default_blob_plain_mimetype = 'text/plain';
70 our $default_text_plain_charset = undef;
72 # file to use for guessing MIME types before trying /etc/mime.types
73 # (relative to the current git repository)
74 our $mimetypes_file = undef;
76 # You define site-wide feature defaults here; override them with
77 # $GITWEB_CONFIG as necessary.
78 our %feature = (
79 # feature => {
80 # 'sub' => feature-sub (subroutine),
81 # 'override' => allow-override (boolean),
82 # 'default' => [ default options...] (array reference)}
84 # if feature is overridable (it means that allow-override has true value,
85 # then feature-sub will be called with default options as parameters;
86 # return value of feature-sub indicates if to enable specified feature
88 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
90 'blame' => {
91 'sub' => \&feature_blame,
92 'override' => 0,
93 'default' => [0]},
95 'snapshot' => {
96 'sub' => \&feature_snapshot,
97 'override' => 0,
98 # => [content-encoding, suffix, program]
99 'default' => ['x-gzip', 'gz', 'gzip']},
101 'pickaxe' => {
102 'sub' => \&feature_pickaxe,
103 'override' => 0,
104 'default' => [1]},
107 sub gitweb_check_feature {
108 my ($name) = @_;
109 return unless exists $feature{$name};
110 my ($sub, $override, @defaults) = (
111 $feature{$name}{'sub'},
112 $feature{$name}{'override'},
113 @{$feature{$name}{'default'}});
114 if (!$override) { return @defaults; }
115 return $sub->(@defaults);
118 # To enable system wide have in $GITWEB_CONFIG
119 # $feature{'blame'}{'default'} = [1];
120 # To have project specific config enable override in $GITWEB_CONFIG
121 # $feature{'blame'}{'override'} = 1;
122 # and in project config gitweb.blame = 0|1;
124 sub feature_blame {
125 my ($val) = git_get_project_config('blame', '--bool');
127 if ($val eq 'true') {
128 return 1;
129 } elsif ($val eq 'false') {
130 return 0;
133 return $_[0];
136 # To disable system wide have in $GITWEB_CONFIG
137 # $feature{'snapshot'}{'default'} = [undef];
138 # To have project specific config enable override in $GITWEB_CONFIG
139 # $feature{'blame'}{'override'} = 1;
140 # and in project config gitweb.snapshot = none|gzip|bzip2
142 sub feature_snapshot {
143 my ($ctype, $suffix, $command) = @_;
145 my ($val) = git_get_project_config('snapshot');
147 if ($val eq 'gzip') {
148 return ('x-gzip', 'gz', 'gzip');
149 } elsif ($val eq 'bzip2') {
150 return ('x-bzip2', 'bz2', 'bzip2');
151 } elsif ($val eq 'none') {
152 return ();
155 return ($ctype, $suffix, $command);
158 sub gitweb_have_snapshot {
159 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
160 my $have_snapshot = (defined $ctype && defined $suffix);
162 return $have_snapshot;
165 # To enable system wide have in $GITWEB_CONFIG
166 # $feature{'pickaxe'}{'default'} = [1];
167 # To have project specific config enable override in $GITWEB_CONFIG
168 # $feature{'pickaxe'}{'override'} = 1;
169 # and in project config gitweb.pickaxe = 0|1;
171 sub feature_pickaxe {
172 my ($val) = git_get_project_config('pickaxe', '--bool');
174 if ($val eq 'true') {
175 return (1);
176 } elsif ($val eq 'false') {
177 return (0);
180 return ($_[0]);
183 # rename detection options for git-diff and git-diff-tree
184 # - default is '-M', with the cost proportional to
185 # (number of removed files) * (number of new files).
186 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
187 # (number of changed files + number of removed files) * (number of new files)
188 # - even more costly is '-C', '--find-copies-harder' with cost
189 # (number of files in the original tree) * (number of new files)
190 # - one might want to include '-B' option, e.g. '-B', '-M'
191 our @diff_opts = ('-M'); # taken from git_commit
193 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
194 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
196 # version of the core git binary
197 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
199 $projects_list ||= $projectroot;
201 # ======================================================================
202 # input validation and dispatch
203 our $action = $cgi->param('a');
204 if (defined $action) {
205 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
206 die_error(undef, "Invalid action parameter");
210 # parameters which are pathnames
211 our $project = $cgi->param('p');
212 if (defined $project) {
213 if (!validate_pathname($project) ||
214 !(-d "$projectroot/$project") ||
215 !(-e "$projectroot/$project/HEAD") ||
216 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
217 ($strict_export && !project_in_list($project))) {
218 undef $project;
219 die_error(undef, "No such project");
223 our $file_name = $cgi->param('f');
224 if (defined $file_name) {
225 if (!validate_pathname($file_name)) {
226 die_error(undef, "Invalid file parameter");
230 our $file_parent = $cgi->param('fp');
231 if (defined $file_parent) {
232 if (!validate_pathname($file_parent)) {
233 die_error(undef, "Invalid file parent parameter");
237 # parameters which are refnames
238 our $hash = $cgi->param('h');
239 if (defined $hash) {
240 if (!validate_refname($hash)) {
241 die_error(undef, "Invalid hash parameter");
245 our $hash_parent = $cgi->param('hp');
246 if (defined $hash_parent) {
247 if (!validate_refname($hash_parent)) {
248 die_error(undef, "Invalid hash parent parameter");
252 our $hash_base = $cgi->param('hb');
253 if (defined $hash_base) {
254 if (!validate_refname($hash_base)) {
255 die_error(undef, "Invalid hash base parameter");
259 our $hash_parent_base = $cgi->param('hpb');
260 if (defined $hash_parent_base) {
261 if (!validate_refname($hash_parent_base)) {
262 die_error(undef, "Invalid hash parent base parameter");
266 # other parameters
267 our $page = $cgi->param('pg');
268 if (defined $page) {
269 if ($page =~ m/[^0-9]/) {
270 die_error(undef, "Invalid page parameter");
274 our $searchtext = $cgi->param('s');
275 if (defined $searchtext) {
276 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
277 die_error(undef, "Invalid search parameter");
279 $searchtext = quotemeta $searchtext;
282 # now read PATH_INFO and use it as alternative to parameters
283 sub evaluate_path_info {
284 return if defined $project;
285 my $path_info = $ENV{"PATH_INFO"};
286 return if !$path_info;
287 $path_info =~ s,^/+,,;
288 return if !$path_info;
289 # find which part of PATH_INFO is project
290 $project = $path_info;
291 $project =~ s,/+$,,;
292 while ($project && !-e "$projectroot/$project/HEAD") {
293 $project =~ s,/*[^/]*$,,;
295 # validate project
296 $project = validate_pathname($project);
297 if (!$project ||
298 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
299 ($strict_export && !project_in_list($project))) {
300 undef $project;
301 return;
303 # do not change any parameters if an action is given using the query string
304 return if $action;
305 $path_info =~ s,^$project/*,,;
306 my ($refname, $pathname) = split(/:/, $path_info, 2);
307 if (defined $pathname) {
308 # we got "project.git/branch:filename" or "project.git/branch:dir/"
309 # we could use git_get_type(branch:pathname), but it needs $git_dir
310 $pathname =~ s,^/+,,;
311 if (!$pathname || substr($pathname, -1) eq "/") {
312 $action ||= "tree";
313 $pathname =~ s,/$,,;
314 } else {
315 $action ||= "blob_plain";
317 $hash_base ||= validate_refname($refname);
318 $file_name ||= validate_pathname($pathname);
319 } elsif (defined $refname) {
320 # we got "project.git/branch"
321 $action ||= "shortlog";
322 $hash ||= validate_refname($refname);
325 evaluate_path_info();
327 # path to the current git repository
328 our $git_dir;
329 $git_dir = "$projectroot/$project" if $project;
331 # dispatch
332 my %actions = (
333 "blame" => \&git_blame2,
334 "blobdiff" => \&git_blobdiff,
335 "blobdiff_plain" => \&git_blobdiff_plain,
336 "blob" => \&git_blob,
337 "blob_plain" => \&git_blob_plain,
338 "commitdiff" => \&git_commitdiff,
339 "commitdiff_plain" => \&git_commitdiff_plain,
340 "commit" => \&git_commit,
341 "heads" => \&git_heads,
342 "history" => \&git_history,
343 "log" => \&git_log,
344 "rss" => \&git_rss,
345 "search" => \&git_search,
346 "shortlog" => \&git_shortlog,
347 "summary" => \&git_summary,
348 "tag" => \&git_tag,
349 "tags" => \&git_tags,
350 "tree" => \&git_tree,
351 "snapshot" => \&git_snapshot,
352 # those below don't need $project
353 "opml" => \&git_opml,
354 "project_list" => \&git_project_list,
355 "project_index" => \&git_project_index,
358 if (defined $project) {
359 $action ||= 'summary';
360 } else {
361 $action ||= 'project_list';
363 if (!defined($actions{$action})) {
364 die_error(undef, "Unknown action");
366 if ($action !~ m/^(opml|project_list|project_index)$/ &&
367 !$project) {
368 die_error(undef, "Project needed");
370 $actions{$action}->();
371 exit;
373 ## ======================================================================
374 ## action links
376 sub href(%) {
377 my %params = @_;
379 my @mapping = (
380 project => "p",
381 action => "a",
382 file_name => "f",
383 file_parent => "fp",
384 hash => "h",
385 hash_parent => "hp",
386 hash_base => "hb",
387 hash_parent_base => "hpb",
388 page => "pg",
389 order => "o",
390 searchtext => "s",
392 my %mapping = @mapping;
394 $params{'project'} = $project unless exists $params{'project'};
396 my @result = ();
397 for (my $i = 0; $i < @mapping; $i += 2) {
398 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
399 if (defined $params{$name}) {
400 push @result, $symbol . "=" . esc_param($params{$name});
403 return "$my_uri?" . join(';', @result);
407 ## ======================================================================
408 ## validation, quoting/unquoting and escaping
410 sub validate_pathname {
411 my $input = shift || return undef;
413 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
414 # at the beginning, at the end, and between slashes.
415 # also this catches doubled slashes
416 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
417 return undef;
419 # no null characters
420 if ($input =~ m!\0!) {
421 return undef;
423 return $input;
426 sub validate_refname {
427 my $input = shift || return undef;
429 # textual hashes are O.K.
430 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
431 return $input;
433 # it must be correct pathname
434 $input = validate_pathname($input)
435 or return undef;
436 # restrictions on ref name according to git-check-ref-format
437 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
438 return undef;
440 return $input;
443 # quote unsafe chars, but keep the slash, even when it's not
444 # correct, but quoted slashes look too horrible in bookmarks
445 sub esc_param {
446 my $str = shift;
447 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
448 $str =~ s/\+/%2B/g;
449 $str =~ s/ /\+/g;
450 return $str;
453 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
454 sub esc_url {
455 my $str = shift;
456 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
457 $str =~ s/\+/%2B/g;
458 $str =~ s/ /\+/g;
459 return $str;
462 # replace invalid utf8 character with SUBSTITUTION sequence
463 sub esc_html {
464 my $str = shift;
465 $str = decode("utf8", $str, Encode::FB_DEFAULT);
466 $str = escapeHTML($str);
467 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
468 return $str;
471 # git may return quoted and escaped filenames
472 sub unquote {
473 my $str = shift;
474 if ($str =~ m/^"(.*)"$/) {
475 $str = $1;
476 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
478 return $str;
481 # escape tabs (convert tabs to spaces)
482 sub untabify {
483 my $line = shift;
485 while ((my $pos = index($line, "\t")) != -1) {
486 if (my $count = (8 - ($pos % 8))) {
487 my $spaces = ' ' x $count;
488 $line =~ s/\t/$spaces/;
492 return $line;
495 sub project_in_list {
496 my $project = shift;
497 my @list = git_get_projects_list();
498 return @list && scalar(grep { $_->{'path'} eq $project } @list);
501 ## ----------------------------------------------------------------------
502 ## HTML aware string manipulation
504 sub chop_str {
505 my $str = shift;
506 my $len = shift;
507 my $add_len = shift || 10;
509 # allow only $len chars, but don't cut a word if it would fit in $add_len
510 # if it doesn't fit, cut it if it's still longer than the dots we would add
511 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
512 my $body = $1;
513 my $tail = $2;
514 if (length($tail) > 4) {
515 $tail = " ...";
516 $body =~ s/&[^;]*$//; # remove chopped character entities
518 return "$body$tail";
521 ## ----------------------------------------------------------------------
522 ## functions returning short strings
524 # CSS class for given age value (in seconds)
525 sub age_class {
526 my $age = shift;
528 if ($age < 60*60*2) {
529 return "age0";
530 } elsif ($age < 60*60*24*2) {
531 return "age1";
532 } else {
533 return "age2";
537 # convert age in seconds to "nn units ago" string
538 sub age_string {
539 my $age = shift;
540 my $age_str;
542 if ($age > 60*60*24*365*2) {
543 $age_str = (int $age/60/60/24/365);
544 $age_str .= " years ago";
545 } elsif ($age > 60*60*24*(365/12)*2) {
546 $age_str = int $age/60/60/24/(365/12);
547 $age_str .= " months ago";
548 } elsif ($age > 60*60*24*7*2) {
549 $age_str = int $age/60/60/24/7;
550 $age_str .= " weeks ago";
551 } elsif ($age > 60*60*24*2) {
552 $age_str = int $age/60/60/24;
553 $age_str .= " days ago";
554 } elsif ($age > 60*60*2) {
555 $age_str = int $age/60/60;
556 $age_str .= " hours ago";
557 } elsif ($age > 60*2) {
558 $age_str = int $age/60;
559 $age_str .= " min ago";
560 } elsif ($age > 2) {
561 $age_str = int $age;
562 $age_str .= " sec ago";
563 } else {
564 $age_str .= " right now";
566 return $age_str;
569 # convert file mode in octal to symbolic file mode string
570 sub mode_str {
571 my $mode = oct shift;
573 if (S_ISDIR($mode & S_IFMT)) {
574 return 'drwxr-xr-x';
575 } elsif (S_ISLNK($mode)) {
576 return 'lrwxrwxrwx';
577 } elsif (S_ISREG($mode)) {
578 # git cares only about the executable bit
579 if ($mode & S_IXUSR) {
580 return '-rwxr-xr-x';
581 } else {
582 return '-rw-r--r--';
584 } else {
585 return '----------';
589 # convert file mode in octal to file type string
590 sub file_type {
591 my $mode = shift;
593 if ($mode !~ m/^[0-7]+$/) {
594 return $mode;
595 } else {
596 $mode = oct $mode;
599 if (S_ISDIR($mode & S_IFMT)) {
600 return "directory";
601 } elsif (S_ISLNK($mode)) {
602 return "symlink";
603 } elsif (S_ISREG($mode)) {
604 return "file";
605 } else {
606 return "unknown";
610 ## ----------------------------------------------------------------------
611 ## functions returning short HTML fragments, or transforming HTML fragments
612 ## which don't beling to other sections
614 # format line of commit message or tag comment
615 sub format_log_line_html {
616 my $line = shift;
618 $line = esc_html($line);
619 $line =~ s/ /&nbsp;/g;
620 if ($line =~ m/([0-9a-fA-F]{40})/) {
621 my $hash_text = $1;
622 if (git_get_type($hash_text) eq "commit") {
623 my $link =
624 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
625 -class => "text"}, $hash_text);
626 $line =~ s/$hash_text/$link/;
629 return $line;
632 # format marker of refs pointing to given object
633 sub format_ref_marker {
634 my ($refs, $id) = @_;
635 my $markers = '';
637 if (defined $refs->{$id}) {
638 foreach my $ref (@{$refs->{$id}}) {
639 my ($type, $name) = qw();
640 # e.g. tags/v2.6.11 or heads/next
641 if ($ref =~ m!^(.*?)s?/(.*)$!) {
642 $type = $1;
643 $name = $2;
644 } else {
645 $type = "ref";
646 $name = $ref;
649 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
653 if ($markers) {
654 return ' <span class="refs">'. $markers . '</span>';
655 } else {
656 return "";
660 # format, perhaps shortened and with markers, title line
661 sub format_subject_html {
662 my ($long, $short, $href, $extra) = @_;
663 $extra = '' unless defined($extra);
665 if (length($short) < length($long)) {
666 return $cgi->a({-href => $href, -class => "list subject",
667 -title => $long},
668 esc_html($short) . $extra);
669 } else {
670 return $cgi->a({-href => $href, -class => "list subject"},
671 esc_html($long) . $extra);
675 sub format_diff_line {
676 my $line = shift;
677 my $char = substr($line, 0, 1);
678 my $diff_class = "";
680 chomp $line;
682 if ($char eq '+') {
683 $diff_class = " add";
684 } elsif ($char eq "-") {
685 $diff_class = " rem";
686 } elsif ($char eq "@") {
687 $diff_class = " chunk_header";
688 } elsif ($char eq "\\") {
689 $diff_class = " incomplete";
691 $line = untabify($line);
692 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
695 ## ----------------------------------------------------------------------
696 ## git utility subroutines, invoking git commands
698 # returns path to the core git executable and the --git-dir parameter as list
699 sub git_cmd {
700 return $GIT, '--git-dir='.$git_dir;
703 # returns path to the core git executable and the --git-dir parameter as string
704 sub git_cmd_str {
705 return join(' ', git_cmd());
708 # get HEAD ref of given project as hash
709 sub git_get_head_hash {
710 my $project = shift;
711 my $o_git_dir = $git_dir;
712 my $retval = undef;
713 $git_dir = "$projectroot/$project";
714 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
715 my $head = <$fd>;
716 close $fd;
717 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
718 $retval = $1;
721 if (defined $o_git_dir) {
722 $git_dir = $o_git_dir;
724 return $retval;
727 # get type of given object
728 sub git_get_type {
729 my $hash = shift;
731 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
732 my $type = <$fd>;
733 close $fd or return;
734 chomp $type;
735 return $type;
738 sub git_get_project_config {
739 my ($key, $type) = @_;
741 return unless ($key);
742 $key =~ s/^gitweb\.//;
743 return if ($key =~ m/\W/);
745 my @x = (git_cmd(), 'repo-config');
746 if (defined $type) { push @x, $type; }
747 push @x, "--get";
748 push @x, "gitweb.$key";
749 my $val = qx(@x);
750 chomp $val;
751 return ($val);
754 # get hash of given path at given ref
755 sub git_get_hash_by_path {
756 my $base = shift;
757 my $path = shift || return undef;
758 my $type = shift;
760 $path =~ s,/+$,,;
762 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
763 or die_error(undef, "Open git-ls-tree failed");
764 my $line = <$fd>;
765 close $fd or return undef;
767 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
768 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
769 if (defined $type && $type ne $2) {
770 # type doesn't match
771 return undef;
773 return $3;
776 ## ......................................................................
777 ## git utility functions, directly accessing git repository
779 sub git_get_project_description {
780 my $path = shift;
782 open my $fd, "$projectroot/$path/description" or return undef;
783 my $descr = <$fd>;
784 close $fd;
785 chomp $descr;
786 return $descr;
789 sub git_get_project_url_list {
790 my $path = shift;
792 open my $fd, "$projectroot/$path/cloneurl" or return;
793 my @git_project_url_list = map { chomp; $_ } <$fd>;
794 close $fd;
796 return wantarray ? @git_project_url_list : \@git_project_url_list;
799 sub git_get_projects_list {
800 my @list;
802 if (-d $projects_list) {
803 # search in directory
804 my $dir = $projects_list;
805 my $pfxlen = length("$dir");
807 File::Find::find({
808 follow_fast => 1, # follow symbolic links
809 dangling_symlinks => 0, # ignore dangling symlinks, silently
810 wanted => sub {
811 # skip project-list toplevel, if we get it.
812 return if (m!^[/.]$!);
813 # only directories can be git repositories
814 return unless (-d $_);
816 my $subdir = substr($File::Find::name, $pfxlen + 1);
817 # we check related file in $projectroot
818 if (-e "$projectroot/$subdir/HEAD" && (!$export_ok ||
819 -e "$projectroot/$subdir/$export_ok")) {
820 push @list, { path => $subdir };
821 $File::Find::prune = 1;
824 }, "$dir");
826 } elsif (-f $projects_list) {
827 # read from file(url-encoded):
828 # 'git%2Fgit.git Linus+Torvalds'
829 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
830 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
831 open my ($fd), $projects_list or return;
832 while (my $line = <$fd>) {
833 chomp $line;
834 my ($path, $owner) = split ' ', $line;
835 $path = unescape($path);
836 $owner = unescape($owner);
837 if (!defined $path) {
838 next;
840 if (-e "$projectroot/$path/HEAD" && (!$export_ok ||
841 -e "$projectroot/$path/$export_ok")) {
842 my $pr = {
843 path => $path,
844 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
846 push @list, $pr
849 close $fd;
851 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
852 return @list;
855 sub git_get_project_owner {
856 my $project = shift;
857 my $owner;
859 return undef unless $project;
861 # read from file (url-encoded):
862 # 'git%2Fgit.git Linus+Torvalds'
863 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
864 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
865 if (-f $projects_list) {
866 open (my $fd , $projects_list);
867 while (my $line = <$fd>) {
868 chomp $line;
869 my ($pr, $ow) = split ' ', $line;
870 $pr = unescape($pr);
871 $ow = unescape($ow);
872 if ($pr eq $project) {
873 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
874 last;
877 close $fd;
879 if (!defined $owner) {
880 $owner = get_file_owner("$projectroot/$project");
883 return $owner;
886 sub git_get_references {
887 my $type = shift || "";
888 my %refs;
889 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
890 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
891 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
892 or return;
894 while (my $line = <$fd>) {
895 chomp $line;
896 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
897 if (defined $refs{$1}) {
898 push @{$refs{$1}}, $2;
899 } else {
900 $refs{$1} = [ $2 ];
904 close $fd or return;
905 return \%refs;
908 sub git_get_rev_name_tags {
909 my $hash = shift || return undef;
911 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
912 or return;
913 my $name_rev = <$fd>;
914 close $fd;
916 if ($name_rev =~ m|^$hash tags/(.*)$|) {
917 return $1;
918 } else {
919 # catches also '$hash undefined' output
920 return undef;
924 ## ----------------------------------------------------------------------
925 ## parse to hash functions
927 sub parse_date {
928 my $epoch = shift;
929 my $tz = shift || "-0000";
931 my %date;
932 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
933 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
934 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
935 $date{'hour'} = $hour;
936 $date{'minute'} = $min;
937 $date{'mday'} = $mday;
938 $date{'day'} = $days[$wday];
939 $date{'month'} = $months[$mon];
940 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
941 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
942 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
943 $mday, $months[$mon], $hour ,$min;
945 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
946 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
947 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
948 $date{'hour_local'} = $hour;
949 $date{'minute_local'} = $min;
950 $date{'tz_local'} = $tz;
951 return %date;
954 sub parse_tag {
955 my $tag_id = shift;
956 my %tag;
957 my @comment;
959 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
960 $tag{'id'} = $tag_id;
961 while (my $line = <$fd>) {
962 chomp $line;
963 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
964 $tag{'object'} = $1;
965 } elsif ($line =~ m/^type (.+)$/) {
966 $tag{'type'} = $1;
967 } elsif ($line =~ m/^tag (.+)$/) {
968 $tag{'name'} = $1;
969 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
970 $tag{'author'} = $1;
971 $tag{'epoch'} = $2;
972 $tag{'tz'} = $3;
973 } elsif ($line =~ m/--BEGIN/) {
974 push @comment, $line;
975 last;
976 } elsif ($line eq "") {
977 last;
980 push @comment, <$fd>;
981 $tag{'comment'} = \@comment;
982 close $fd or return;
983 if (!defined $tag{'name'}) {
984 return
986 return %tag
989 sub parse_commit {
990 my $commit_id = shift;
991 my $commit_text = shift;
993 my @commit_lines;
994 my %co;
996 if (defined $commit_text) {
997 @commit_lines = @$commit_text;
998 } else {
999 $/ = "\0";
1000 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
1001 or return;
1002 @commit_lines = split '\n', <$fd>;
1003 close $fd or return;
1004 $/ = "\n";
1005 pop @commit_lines;
1007 my $header = shift @commit_lines;
1008 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1009 return;
1011 ($co{'id'}, my @parents) = split ' ', $header;
1012 $co{'parents'} = \@parents;
1013 $co{'parent'} = $parents[0];
1014 while (my $line = shift @commit_lines) {
1015 last if $line eq "\n";
1016 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1017 $co{'tree'} = $1;
1018 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1019 $co{'author'} = $1;
1020 $co{'author_epoch'} = $2;
1021 $co{'author_tz'} = $3;
1022 if ($co{'author'} =~ m/^([^<]+) </) {
1023 $co{'author_name'} = $1;
1024 } else {
1025 $co{'author_name'} = $co{'author'};
1027 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1028 $co{'committer'} = $1;
1029 $co{'committer_epoch'} = $2;
1030 $co{'committer_tz'} = $3;
1031 $co{'committer_name'} = $co{'committer'};
1032 $co{'committer_name'} =~ s/ <.*//;
1035 if (!defined $co{'tree'}) {
1036 return;
1039 foreach my $title (@commit_lines) {
1040 $title =~ s/^ //;
1041 if ($title ne "") {
1042 $co{'title'} = chop_str($title, 80, 5);
1043 # remove leading stuff of merges to make the interesting part visible
1044 if (length($title) > 50) {
1045 $title =~ s/^Automatic //;
1046 $title =~ s/^merge (of|with) /Merge ... /i;
1047 if (length($title) > 50) {
1048 $title =~ s/(http|rsync):\/\///;
1050 if (length($title) > 50) {
1051 $title =~ s/(master|www|rsync)\.//;
1053 if (length($title) > 50) {
1054 $title =~ s/kernel.org:?//;
1056 if (length($title) > 50) {
1057 $title =~ s/\/pub\/scm//;
1060 $co{'title_short'} = chop_str($title, 50, 5);
1061 last;
1064 # remove added spaces
1065 foreach my $line (@commit_lines) {
1066 $line =~ s/^ //;
1068 $co{'comment'} = \@commit_lines;
1070 my $age = time - $co{'committer_epoch'};
1071 $co{'age'} = $age;
1072 $co{'age_string'} = age_string($age);
1073 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1074 if ($age > 60*60*24*7*2) {
1075 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1076 $co{'age_string_age'} = $co{'age_string'};
1077 } else {
1078 $co{'age_string_date'} = $co{'age_string'};
1079 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1081 return %co;
1084 # parse ref from ref_file, given by ref_id, with given type
1085 sub parse_ref {
1086 my $ref_file = shift;
1087 my $ref_id = shift;
1088 my $type = shift || git_get_type($ref_id);
1089 my %ref_item;
1091 $ref_item{'type'} = $type;
1092 $ref_item{'id'} = $ref_id;
1093 $ref_item{'epoch'} = 0;
1094 $ref_item{'age'} = "unknown";
1095 if ($type eq "tag") {
1096 my %tag = parse_tag($ref_id);
1097 $ref_item{'comment'} = $tag{'comment'};
1098 if ($tag{'type'} eq "commit") {
1099 my %co = parse_commit($tag{'object'});
1100 $ref_item{'epoch'} = $co{'committer_epoch'};
1101 $ref_item{'age'} = $co{'age_string'};
1102 } elsif (defined($tag{'epoch'})) {
1103 my $age = time - $tag{'epoch'};
1104 $ref_item{'epoch'} = $tag{'epoch'};
1105 $ref_item{'age'} = age_string($age);
1107 $ref_item{'reftype'} = $tag{'type'};
1108 $ref_item{'name'} = $tag{'name'};
1109 $ref_item{'refid'} = $tag{'object'};
1110 } elsif ($type eq "commit"){
1111 my %co = parse_commit($ref_id);
1112 $ref_item{'reftype'} = "commit";
1113 $ref_item{'name'} = $ref_file;
1114 $ref_item{'title'} = $co{'title'};
1115 $ref_item{'refid'} = $ref_id;
1116 $ref_item{'epoch'} = $co{'committer_epoch'};
1117 $ref_item{'age'} = $co{'age_string'};
1118 } else {
1119 $ref_item{'reftype'} = $type;
1120 $ref_item{'name'} = $ref_file;
1121 $ref_item{'refid'} = $ref_id;
1124 return %ref_item;
1127 # parse line of git-diff-tree "raw" output
1128 sub parse_difftree_raw_line {
1129 my $line = shift;
1130 my %res;
1132 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1133 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1134 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1135 $res{'from_mode'} = $1;
1136 $res{'to_mode'} = $2;
1137 $res{'from_id'} = $3;
1138 $res{'to_id'} = $4;
1139 $res{'status'} = $5;
1140 $res{'similarity'} = $6;
1141 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1142 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1143 } else {
1144 $res{'file'} = unquote($7);
1147 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1148 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1149 $res{'commit'} = $1;
1152 return wantarray ? %res : \%res;
1155 # parse line of git-ls-tree output
1156 sub parse_ls_tree_line ($;%) {
1157 my $line = shift;
1158 my %opts = @_;
1159 my %res;
1161 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1162 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1164 $res{'mode'} = $1;
1165 $res{'type'} = $2;
1166 $res{'hash'} = $3;
1167 if ($opts{'-z'}) {
1168 $res{'name'} = $4;
1169 } else {
1170 $res{'name'} = unquote($4);
1173 return wantarray ? %res : \%res;
1176 ## ......................................................................
1177 ## parse to array of hashes functions
1179 sub git_get_refs_list {
1180 my $type = shift || "";
1181 my %refs;
1182 my @reflist;
1184 my @refs;
1185 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1186 or return;
1187 while (my $line = <$fd>) {
1188 chomp $line;
1189 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1190 if (defined $refs{$1}) {
1191 push @{$refs{$1}}, $2;
1192 } else {
1193 $refs{$1} = [ $2 ];
1196 if (! $4) { # unpeeled, direct reference
1197 push @refs, { hash => $1, name => $3 }; # without type
1198 } elsif ($3 eq $refs[-1]{'name'}) {
1199 # most likely a tag is followed by its peeled
1200 # (deref) one, and when that happens we know the
1201 # previous one was of type 'tag'.
1202 $refs[-1]{'type'} = "tag";
1206 close $fd;
1208 foreach my $ref (@refs) {
1209 my $ref_file = $ref->{'name'};
1210 my $ref_id = $ref->{'hash'};
1212 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1213 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1215 push @reflist, \%ref_item;
1217 # sort refs by age
1218 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1219 return (\@reflist, \%refs);
1222 ## ----------------------------------------------------------------------
1223 ## filesystem-related functions
1225 sub get_file_owner {
1226 my $path = shift;
1228 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1229 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1230 if (!defined $gcos) {
1231 return undef;
1233 my $owner = $gcos;
1234 $owner =~ s/[,;].*$//;
1235 return decode("utf8", $owner, Encode::FB_DEFAULT);
1238 ## ......................................................................
1239 ## mimetype related functions
1241 sub mimetype_guess_file {
1242 my $filename = shift;
1243 my $mimemap = shift;
1244 -r $mimemap or return undef;
1246 my %mimemap;
1247 open(MIME, $mimemap) or return undef;
1248 while (<MIME>) {
1249 next if m/^#/; # skip comments
1250 my ($mime, $exts) = split(/\t+/);
1251 if (defined $exts) {
1252 my @exts = split(/\s+/, $exts);
1253 foreach my $ext (@exts) {
1254 $mimemap{$ext} = $mime;
1258 close(MIME);
1260 $filename =~ /\.([^.]*)$/;
1261 return $mimemap{$1};
1264 sub mimetype_guess {
1265 my $filename = shift;
1266 my $mime;
1267 $filename =~ /\./ or return undef;
1269 if ($mimetypes_file) {
1270 my $file = $mimetypes_file;
1271 if ($file !~ m!^/!) { # if it is relative path
1272 # it is relative to project
1273 $file = "$projectroot/$project/$file";
1275 $mime = mimetype_guess_file($filename, $file);
1277 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1278 return $mime;
1281 sub blob_mimetype {
1282 my $fd = shift;
1283 my $filename = shift;
1285 if ($filename) {
1286 my $mime = mimetype_guess($filename);
1287 $mime and return $mime;
1290 # just in case
1291 return $default_blob_plain_mimetype unless $fd;
1293 if (-T $fd) {
1294 return 'text/plain' .
1295 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1296 } elsif (! $filename) {
1297 return 'application/octet-stream';
1298 } elsif ($filename =~ m/\.png$/i) {
1299 return 'image/png';
1300 } elsif ($filename =~ m/\.gif$/i) {
1301 return 'image/gif';
1302 } elsif ($filename =~ m/\.jpe?g$/i) {
1303 return 'image/jpeg';
1304 } else {
1305 return 'application/octet-stream';
1309 ## ======================================================================
1310 ## functions printing HTML: header, footer, error page
1312 sub git_header_html {
1313 my $status = shift || "200 OK";
1314 my $expires = shift;
1316 my $title = "$site_name git";
1317 if (defined $project) {
1318 $title .= " - $project";
1319 if (defined $action) {
1320 $title .= "/$action";
1321 if (defined $file_name) {
1322 $title .= " - " . esc_html($file_name);
1323 if ($action eq "tree" && $file_name !~ m|/$|) {
1324 $title .= "/";
1329 my $content_type;
1330 # require explicit support from the UA if we are to send the page as
1331 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1332 # we have to do this because MSIE sometimes globs '*/*', pretending to
1333 # support xhtml+xml but choking when it gets what it asked for.
1334 if (defined $cgi->http('HTTP_ACCEPT') &&
1335 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1336 $cgi->Accept('application/xhtml+xml') != 0) {
1337 $content_type = 'application/xhtml+xml';
1338 } else {
1339 $content_type = 'text/html';
1341 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1342 -status=> $status, -expires => $expires);
1343 print <<EOF;
1344 <?xml version="1.0" encoding="utf-8"?>
1345 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1346 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1347 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1348 <!-- git core binaries version $git_version -->
1349 <head>
1350 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1351 <meta name="generator" content="gitweb/$version git/$git_version"/>
1352 <meta name="robots" content="index, nofollow"/>
1353 <title>$title</title>
1354 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1356 if (defined $project) {
1357 printf('<link rel="alternate" title="%s log" '.
1358 'href="%s" type="application/rss+xml"/>'."\n",
1359 esc_param($project), href(action=>"rss"));
1360 } else {
1361 printf('<link rel="alternate" title="%s projects list" '.
1362 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1363 $site_name, href(project=>undef, action=>"project_index"));
1364 printf('<link rel="alternate" title="%s projects logs" '.
1365 'href="%s" type="text/x-opml"/>'."\n",
1366 $site_name, href(project=>undef, action=>"opml"));
1368 if (defined $favicon) {
1369 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1372 print "</head>\n" .
1373 "<body>\n" .
1374 "<div class=\"page_header\">\n" .
1375 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1376 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1377 "</a>\n";
1378 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1379 if (defined $project) {
1380 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1381 if (defined $action) {
1382 print " / $action";
1384 print "\n";
1385 if (!defined $searchtext) {
1386 $searchtext = "";
1388 my $search_hash;
1389 if (defined $hash_base) {
1390 $search_hash = $hash_base;
1391 } elsif (defined $hash) {
1392 $search_hash = $hash;
1393 } else {
1394 $search_hash = "HEAD";
1396 $cgi->param("a", "search");
1397 $cgi->param("h", $search_hash);
1398 print $cgi->startform(-method => "get", -action => $my_uri) .
1399 "<div class=\"search\">\n" .
1400 $cgi->hidden(-name => "p") . "\n" .
1401 $cgi->hidden(-name => "a") . "\n" .
1402 $cgi->hidden(-name => "h") . "\n" .
1403 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1404 "</div>" .
1405 $cgi->end_form() . "\n";
1407 print "</div>\n";
1410 sub git_footer_html {
1411 print "<div class=\"page_footer\">\n";
1412 if (defined $project) {
1413 my $descr = git_get_project_description($project);
1414 if (defined $descr) {
1415 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1417 print $cgi->a({-href => href(action=>"rss"),
1418 -class => "rss_logo"}, "RSS") . "\n";
1419 } else {
1420 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1421 -class => "rss_logo"}, "OPML") . " ";
1422 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1423 -class => "rss_logo"}, "TXT") . "\n";
1425 print "</div>\n" .
1426 "</body>\n" .
1427 "</html>";
1430 sub die_error {
1431 my $status = shift || "403 Forbidden";
1432 my $error = shift || "Malformed query, file missing or permission denied";
1434 git_header_html($status);
1435 print <<EOF;
1436 <div class="page_body">
1437 <br /><br />
1438 $status - $error
1439 <br />
1440 </div>
1442 git_footer_html();
1443 exit;
1446 ## ----------------------------------------------------------------------
1447 ## functions printing or outputting HTML: navigation
1449 sub git_print_page_nav {
1450 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1451 $extra = '' if !defined $extra; # pager or formats
1453 my @navs = qw(summary shortlog log commit commitdiff tree);
1454 if ($suppress) {
1455 @navs = grep { $_ ne $suppress } @navs;
1458 my %arg = map { $_ => {action=>$_} } @navs;
1459 if (defined $head) {
1460 for (qw(commit commitdiff)) {
1461 $arg{$_}{hash} = $head;
1463 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1464 for (qw(shortlog log)) {
1465 $arg{$_}{hash} = $head;
1469 $arg{tree}{hash} = $treehead if defined $treehead;
1470 $arg{tree}{hash_base} = $treebase if defined $treebase;
1472 print "<div class=\"page_nav\">\n" .
1473 (join " | ",
1474 map { $_ eq $current ?
1475 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1476 } @navs);
1477 print "<br/>\n$extra<br/>\n" .
1478 "</div>\n";
1481 sub format_paging_nav {
1482 my ($action, $hash, $head, $page, $nrevs) = @_;
1483 my $paging_nav;
1486 if ($hash ne $head || $page) {
1487 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1488 } else {
1489 $paging_nav .= "HEAD";
1492 if ($page > 0) {
1493 $paging_nav .= " &sdot; " .
1494 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1495 -accesskey => "p", -title => "Alt-p"}, "prev");
1496 } else {
1497 $paging_nav .= " &sdot; prev";
1500 if ($nrevs >= (100 * ($page+1)-1)) {
1501 $paging_nav .= " &sdot; " .
1502 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1503 -accesskey => "n", -title => "Alt-n"}, "next");
1504 } else {
1505 $paging_nav .= " &sdot; next";
1508 return $paging_nav;
1511 ## ......................................................................
1512 ## functions printing or outputting HTML: div
1514 sub git_print_header_div {
1515 my ($action, $title, $hash, $hash_base) = @_;
1516 my %args = ();
1518 $args{action} = $action;
1519 $args{hash} = $hash if $hash;
1520 $args{hash_base} = $hash_base if $hash_base;
1522 print "<div class=\"header\">\n" .
1523 $cgi->a({-href => href(%args), -class => "title"},
1524 $title ? $title : $action) .
1525 "\n</div>\n";
1528 #sub git_print_authorship (\%) {
1529 sub git_print_authorship {
1530 my $co = shift;
1532 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1533 print "<div class=\"author_date\">" .
1534 esc_html($co->{'author_name'}) .
1535 " [$ad{'rfc2822'}";
1536 if ($ad{'hour_local'} < 6) {
1537 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1538 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1539 } else {
1540 printf(" (%02d:%02d %s)",
1541 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1543 print "]</div>\n";
1546 sub git_print_page_path {
1547 my $name = shift;
1548 my $type = shift;
1549 my $hb = shift;
1551 if (!defined $name) {
1552 print "<div class=\"page_path\">/</div>\n";
1553 } else {
1554 my @dirname = split '/', $name;
1555 my $basename = pop @dirname;
1556 my $fullname = '';
1558 print "<div class=\"page_path\">";
1559 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1560 -title => 'tree root'}, "[$project]");
1561 print " / ";
1562 foreach my $dir (@dirname) {
1563 $fullname .= ($fullname ? '/' : '') . $dir;
1564 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1565 hash_base=>$hb),
1566 -title => $fullname}, esc_html($dir));
1567 print " / ";
1569 if (defined $type && $type eq 'blob') {
1570 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1571 hash_base=>$hb),
1572 -title => $name}, esc_html($basename));
1573 } elsif (defined $type && $type eq 'tree') {
1574 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1575 hash_base=>$hb),
1576 -title => $name}, esc_html($basename));
1577 } else {
1578 print esc_html($basename);
1580 print "<br/></div>\n";
1584 # sub git_print_log (\@;%) {
1585 sub git_print_log ($;%) {
1586 my $log = shift;
1587 my %opts = @_;
1589 if ($opts{'-remove_title'}) {
1590 # remove title, i.e. first line of log
1591 shift @$log;
1593 # remove leading empty lines
1594 while (defined $log->[0] && $log->[0] eq "") {
1595 shift @$log;
1598 # print log
1599 my $signoff = 0;
1600 my $empty = 0;
1601 foreach my $line (@$log) {
1602 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1603 $signoff = 1;
1604 $empty = 0;
1605 if (! $opts{'-remove_signoff'}) {
1606 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1607 next;
1608 } else {
1609 # remove signoff lines
1610 next;
1612 } else {
1613 $signoff = 0;
1616 # print only one empty line
1617 # do not print empty line after signoff
1618 if ($line eq "") {
1619 next if ($empty || $signoff);
1620 $empty = 1;
1621 } else {
1622 $empty = 0;
1625 print format_log_line_html($line) . "<br/>\n";
1628 if ($opts{'-final_empty_line'}) {
1629 # end with single empty line
1630 print "<br/>\n" unless $empty;
1634 sub git_print_simplified_log {
1635 my $log = shift;
1636 my $remove_title = shift;
1638 git_print_log($log,
1639 -final_empty_line=> 1,
1640 -remove_title => $remove_title);
1643 # print tree entry (row of git_tree), but without encompassing <tr> element
1644 sub git_print_tree_entry {
1645 my ($t, $basedir, $hash_base, $have_blame) = @_;
1647 my %base_key = ();
1648 $base_key{hash_base} = $hash_base if defined $hash_base;
1650 # The format of a table row is: mode list link. Where mode is
1651 # the mode of the entry, list is the name of the entry, an href,
1652 # and link is the action links of the entry.
1654 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1655 if ($t->{'type'} eq "blob") {
1656 print "<td class=\"list\">" .
1657 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1658 file_name=>"$basedir$t->{'name'}", %base_key),
1659 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1660 print "<td class=\"link\">";
1661 if ($have_blame) {
1662 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1663 file_name=>"$basedir$t->{'name'}", %base_key)},
1664 "blame");
1666 if (defined $hash_base) {
1667 if ($have_blame) {
1668 print " | ";
1670 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1671 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1672 "history");
1674 print " | " .
1675 $cgi->a({-href => href(action=>"blob_plain",
1676 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1677 "raw");
1678 print "</td>\n";
1680 } elsif ($t->{'type'} eq "tree") {
1681 print "<td class=\"list\">";
1682 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1683 file_name=>"$basedir$t->{'name'}", %base_key)},
1684 esc_html($t->{'name'}));
1685 print "</td>\n";
1686 print "<td class=\"link\">";
1687 if (defined $hash_base) {
1688 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1689 file_name=>"$basedir$t->{'name'}")},
1690 "history");
1692 print "</td>\n";
1696 ## ......................................................................
1697 ## functions printing large fragments of HTML
1699 sub git_difftree_body {
1700 my ($difftree, $hash, $parent) = @_;
1702 print "<div class=\"list_head\">\n";
1703 if ($#{$difftree} > 10) {
1704 print(($#{$difftree} + 1) . " files changed:\n");
1706 print "</div>\n";
1708 print "<table class=\"diff_tree\">\n";
1709 my $alternate = 1;
1710 my $patchno = 0;
1711 foreach my $line (@{$difftree}) {
1712 my %diff = parse_difftree_raw_line($line);
1714 if ($alternate) {
1715 print "<tr class=\"dark\">\n";
1716 } else {
1717 print "<tr class=\"light\">\n";
1719 $alternate ^= 1;
1721 my ($to_mode_oct, $to_mode_str, $to_file_type);
1722 my ($from_mode_oct, $from_mode_str, $from_file_type);
1723 if ($diff{'to_mode'} ne ('0' x 6)) {
1724 $to_mode_oct = oct $diff{'to_mode'};
1725 if (S_ISREG($to_mode_oct)) { # only for regular file
1726 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1728 $to_file_type = file_type($diff{'to_mode'});
1730 if ($diff{'from_mode'} ne ('0' x 6)) {
1731 $from_mode_oct = oct $diff{'from_mode'};
1732 if (S_ISREG($to_mode_oct)) { # only for regular file
1733 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1735 $from_file_type = file_type($diff{'from_mode'});
1738 if ($diff{'status'} eq "A") { # created
1739 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1740 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1741 $mode_chng .= "]</span>";
1742 print "<td>";
1743 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1744 hash_base=>$hash, file_name=>$diff{'file'}),
1745 -class => "list"}, esc_html($diff{'file'}));
1746 print "</td>\n";
1747 print "<td>$mode_chng</td>\n";
1748 print "<td class=\"link\">";
1749 if ($action eq 'commitdiff') {
1750 # link to patch
1751 $patchno++;
1752 print $cgi->a({-href => "#patch$patchno"}, "patch");
1754 print "</td>\n";
1756 } elsif ($diff{'status'} eq "D") { # deleted
1757 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1758 print "<td>";
1759 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1760 hash_base=>$parent, file_name=>$diff{'file'}),
1761 -class => "list"}, esc_html($diff{'file'}));
1762 print "</td>\n";
1763 print "<td>$mode_chng</td>\n";
1764 print "<td class=\"link\">";
1765 if ($action eq 'commitdiff') {
1766 # link to patch
1767 $patchno++;
1768 print $cgi->a({-href => "#patch$patchno"}, "patch");
1769 print " | ";
1771 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1772 file_name=>$diff{'file'})},
1773 "blame") . " | ";
1774 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1775 file_name=>$diff{'file'})},
1776 "history");
1777 print "</td>\n";
1779 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1780 my $mode_chnge = "";
1781 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1782 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1783 if ($from_file_type != $to_file_type) {
1784 $mode_chnge .= " from $from_file_type to $to_file_type";
1786 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1787 if ($from_mode_str && $to_mode_str) {
1788 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1789 } elsif ($to_mode_str) {
1790 $mode_chnge .= " mode: $to_mode_str";
1793 $mode_chnge .= "]</span>\n";
1795 print "<td>";
1796 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1797 hash_base=>$hash, file_name=>$diff{'file'}),
1798 -class => "list"}, esc_html($diff{'file'}));
1799 print "</td>\n";
1800 print "<td>$mode_chnge</td>\n";
1801 print "<td class=\"link\">";
1802 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1803 if ($action eq 'commitdiff') {
1804 # link to patch
1805 $patchno++;
1806 print $cgi->a({-href => "#patch$patchno"}, "patch");
1807 } else {
1808 print $cgi->a({-href => href(action=>"blobdiff",
1809 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1810 hash_base=>$hash, hash_parent_base=>$parent,
1811 file_name=>$diff{'file'})},
1812 "diff");
1814 print " | ";
1816 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1817 file_name=>$diff{'file'})},
1818 "blame") . " | ";
1819 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1820 file_name=>$diff{'file'})},
1821 "history");
1822 print "</td>\n";
1824 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1825 my %status_name = ('R' => 'moved', 'C' => 'copied');
1826 my $nstatus = $status_name{$diff{'status'}};
1827 my $mode_chng = "";
1828 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1829 # mode also for directories, so we cannot use $to_mode_str
1830 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1832 print "<td>" .
1833 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1834 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1835 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1836 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1837 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1838 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1839 -class => "list"}, esc_html($diff{'from_file'})) .
1840 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1841 "<td class=\"link\">";
1842 if ($diff{'to_id'} ne $diff{'from_id'}) {
1843 if ($action eq 'commitdiff') {
1844 # link to patch
1845 $patchno++;
1846 print $cgi->a({-href => "#patch$patchno"}, "patch");
1847 } else {
1848 print $cgi->a({-href => href(action=>"blobdiff",
1849 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1850 hash_base=>$hash, hash_parent_base=>$parent,
1851 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1852 "diff");
1854 print " | ";
1856 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1857 file_name=>$diff{'from_file'})},
1858 "blame") . " | ";
1859 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1860 file_name=>$diff{'from_file'})},
1861 "history");
1862 print "</td>\n";
1864 } # we should not encounter Unmerged (U) or Unknown (X) status
1865 print "</tr>\n";
1867 print "</table>\n";
1870 sub git_patchset_body {
1871 my ($fd, $difftree, $hash, $hash_parent) = @_;
1873 my $patch_idx = 0;
1874 my $in_header = 0;
1875 my $patch_found = 0;
1876 my $diffinfo;
1878 print "<div class=\"patchset\">\n";
1880 LINE:
1881 while (my $patch_line = <$fd>) {
1882 chomp $patch_line;
1884 if ($patch_line =~ m/^diff /) { # "git diff" header
1885 # beginning of patch (in patchset)
1886 if ($patch_found) {
1887 # close previous patch
1888 print "</div>\n"; # class="patch"
1889 } else {
1890 # first patch in patchset
1891 $patch_found = 1;
1893 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1895 if (ref($difftree->[$patch_idx]) eq "HASH") {
1896 $diffinfo = $difftree->[$patch_idx];
1897 } else {
1898 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1900 $patch_idx++;
1902 # for now, no extended header, hence we skip empty patches
1903 # companion to next LINE if $in_header;
1904 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1905 $in_header = 1;
1906 next LINE;
1909 if ($diffinfo->{'status'} eq "A") { # added
1910 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1911 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1912 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1913 $diffinfo->{'to_id'}) . "(new)" .
1914 "</div>\n"; # class="diff_info"
1916 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1917 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1918 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1919 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1920 $diffinfo->{'from_id'}) . "(deleted)" .
1921 "</div>\n"; # class="diff_info"
1923 } elsif ($diffinfo->{'status'} eq "R" || # renamed
1924 $diffinfo->{'status'} eq "C" || # copied
1925 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1926 print "<div class=\"diff_info\">" .
1927 file_type($diffinfo->{'from_mode'}) . ":" .
1928 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1929 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1930 $diffinfo->{'from_id'}) .
1931 " -> " .
1932 file_type($diffinfo->{'to_mode'}) . ":" .
1933 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1934 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1935 $diffinfo->{'to_id'});
1936 print "</div>\n"; # class="diff_info"
1938 } else { # modified, mode changed, ...
1939 print "<div class=\"diff_info\">" .
1940 file_type($diffinfo->{'from_mode'}) . ":" .
1941 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1942 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1943 $diffinfo->{'from_id'}) .
1944 " -> " .
1945 file_type($diffinfo->{'to_mode'}) . ":" .
1946 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1947 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1948 $diffinfo->{'to_id'});
1949 print "</div>\n"; # class="diff_info"
1952 #print "<div class=\"diff extended_header\">\n";
1953 $in_header = 1;
1954 next LINE;
1955 } # start of patch in patchset
1958 if ($in_header && $patch_line =~ m/^---/) {
1959 #print "</div>\n"; # class="diff extended_header"
1960 $in_header = 0;
1962 my $file = $diffinfo->{'from_file'};
1963 $file ||= $diffinfo->{'file'};
1964 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1965 hash=>$diffinfo->{'from_id'}, file_name=>$file),
1966 -class => "list"}, esc_html($file));
1967 $patch_line =~ s|a/.*$|a/$file|g;
1968 print "<div class=\"diff from_file\">$patch_line</div>\n";
1970 $patch_line = <$fd>;
1971 chomp $patch_line;
1973 #$patch_line =~ m/^+++/;
1974 $file = $diffinfo->{'to_file'};
1975 $file ||= $diffinfo->{'file'};
1976 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1977 hash=>$diffinfo->{'to_id'}, file_name=>$file),
1978 -class => "list"}, esc_html($file));
1979 $patch_line =~ s|b/.*|b/$file|g;
1980 print "<div class=\"diff to_file\">$patch_line</div>\n";
1982 next LINE;
1984 next LINE if $in_header;
1986 print format_diff_line($patch_line);
1988 print "</div>\n" if $patch_found; # class="patch"
1990 print "</div>\n"; # class="patchset"
1993 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1995 sub git_shortlog_body {
1996 # uses global variable $project
1997 my ($revlist, $from, $to, $refs, $extra) = @_;
1999 $from = 0 unless defined $from;
2000 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2002 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2003 my $alternate = 1;
2004 for (my $i = $from; $i <= $to; $i++) {
2005 my $commit = $revlist->[$i];
2006 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2007 my $ref = format_ref_marker($refs, $commit);
2008 my %co = parse_commit($commit);
2009 if ($alternate) {
2010 print "<tr class=\"dark\">\n";
2011 } else {
2012 print "<tr class=\"light\">\n";
2014 $alternate ^= 1;
2015 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2016 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2017 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2018 "<td>";
2019 print format_subject_html($co{'title'}, $co{'title_short'},
2020 href(action=>"commit", hash=>$commit), $ref);
2021 print "</td>\n" .
2022 "<td class=\"link\">" .
2023 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2024 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") . " | " .
2025 $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2026 print "</td>\n" .
2027 "</tr>\n";
2029 if (defined $extra) {
2030 print "<tr>\n" .
2031 "<td colspan=\"4\">$extra</td>\n" .
2032 "</tr>\n";
2034 print "</table>\n";
2037 sub git_history_body {
2038 # Warning: assumes constant type (blob or tree) during history
2039 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2041 $from = 0 unless defined $from;
2042 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2044 print "<table class=\"history\" cellspacing=\"0\">\n";
2045 my $alternate = 1;
2046 for (my $i = $from; $i <= $to; $i++) {
2047 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2048 next;
2051 my $commit = $1;
2052 my %co = parse_commit($commit);
2053 if (!%co) {
2054 next;
2057 my $ref = format_ref_marker($refs, $commit);
2059 if ($alternate) {
2060 print "<tr class=\"dark\">\n";
2061 } else {
2062 print "<tr class=\"light\">\n";
2064 $alternate ^= 1;
2065 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2066 # shortlog uses chop_str($co{'author_name'}, 10)
2067 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2068 "<td>";
2069 # originally git_history used chop_str($co{'title'}, 50)
2070 print format_subject_html($co{'title'}, $co{'title_short'},
2071 href(action=>"commit", hash=>$commit), $ref);
2072 print "</td>\n" .
2073 "<td class=\"link\">" .
2074 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2075 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2076 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
2078 if ($ftype eq 'blob') {
2079 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2080 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2081 if (defined $blob_current && defined $blob_parent &&
2082 $blob_current ne $blob_parent) {
2083 print " | " .
2084 $cgi->a({-href => href(action=>"blobdiff",
2085 hash=>$blob_current, hash_parent=>$blob_parent,
2086 hash_base=>$hash_base, hash_parent_base=>$commit,
2087 file_name=>$file_name)},
2088 "diff to current");
2091 print "</td>\n" .
2092 "</tr>\n";
2094 if (defined $extra) {
2095 print "<tr>\n" .
2096 "<td colspan=\"4\">$extra</td>\n" .
2097 "</tr>\n";
2099 print "</table>\n";
2102 sub git_tags_body {
2103 # uses global variable $project
2104 my ($taglist, $from, $to, $extra) = @_;
2105 $from = 0 unless defined $from;
2106 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2108 print "<table class=\"tags\" cellspacing=\"0\">\n";
2109 my $alternate = 1;
2110 for (my $i = $from; $i <= $to; $i++) {
2111 my $entry = $taglist->[$i];
2112 my %tag = %$entry;
2113 my $comment_lines = $tag{'comment'};
2114 my $comment = shift @$comment_lines;
2115 my $comment_short;
2116 if (defined $comment) {
2117 $comment_short = chop_str($comment, 30, 5);
2119 if ($alternate) {
2120 print "<tr class=\"dark\">\n";
2121 } else {
2122 print "<tr class=\"light\">\n";
2124 $alternate ^= 1;
2125 print "<td><i>$tag{'age'}</i></td>\n" .
2126 "<td>" .
2127 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2128 -class => "list name"}, esc_html($tag{'name'})) .
2129 "</td>\n" .
2130 "<td>";
2131 if (defined $comment) {
2132 print format_subject_html($comment, $comment_short,
2133 href(action=>"tag", hash=>$tag{'id'}));
2135 print "</td>\n" .
2136 "<td class=\"selflink\">";
2137 if ($tag{'type'} eq "tag") {
2138 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2139 } else {
2140 print "&nbsp;";
2142 print "</td>\n" .
2143 "<td class=\"link\">" . " | " .
2144 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2145 if ($tag{'reftype'} eq "commit") {
2146 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2147 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2148 } elsif ($tag{'reftype'} eq "blob") {
2149 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2151 print "</td>\n" .
2152 "</tr>";
2154 if (defined $extra) {
2155 print "<tr>\n" .
2156 "<td colspan=\"5\">$extra</td>\n" .
2157 "</tr>\n";
2159 print "</table>\n";
2162 sub git_heads_body {
2163 # uses global variable $project
2164 my ($headlist, $head, $from, $to, $extra) = @_;
2165 $from = 0 unless defined $from;
2166 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2168 print "<table class=\"heads\" cellspacing=\"0\">\n";
2169 my $alternate = 1;
2170 for (my $i = $from; $i <= $to; $i++) {
2171 my $entry = $headlist->[$i];
2172 my %tag = %$entry;
2173 my $curr = $tag{'id'} eq $head;
2174 if ($alternate) {
2175 print "<tr class=\"dark\">\n";
2176 } else {
2177 print "<tr class=\"light\">\n";
2179 $alternate ^= 1;
2180 print "<td><i>$tag{'age'}</i></td>\n" .
2181 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2182 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2183 -class => "list name"},esc_html($tag{'name'})) .
2184 "</td>\n" .
2185 "<td class=\"link\">" .
2186 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2187 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2188 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2189 "</td>\n" .
2190 "</tr>";
2192 if (defined $extra) {
2193 print "<tr>\n" .
2194 "<td colspan=\"3\">$extra</td>\n" .
2195 "</tr>\n";
2197 print "</table>\n";
2200 ## ======================================================================
2201 ## ======================================================================
2202 ## actions
2204 sub git_project_list {
2205 my $order = $cgi->param('o');
2206 if (defined $order && $order !~ m/project|descr|owner|age/) {
2207 die_error(undef, "Unknown order parameter");
2210 my @list = git_get_projects_list();
2211 my @projects;
2212 if (!@list) {
2213 die_error(undef, "No projects found");
2215 foreach my $pr (@list) {
2216 my $head = git_get_head_hash($pr->{'path'});
2217 if (!defined $head) {
2218 next;
2220 $git_dir = "$projectroot/$pr->{'path'}";
2221 my %co = parse_commit($head);
2222 if (!%co) {
2223 next;
2225 $pr->{'commit'} = \%co;
2226 if (!defined $pr->{'descr'}) {
2227 my $descr = git_get_project_description($pr->{'path'}) || "";
2228 $pr->{'descr'} = chop_str($descr, 25, 5);
2230 if (!defined $pr->{'owner'}) {
2231 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2233 push @projects, $pr;
2236 git_header_html();
2237 if (-f $home_text) {
2238 print "<div class=\"index_include\">\n";
2239 open (my $fd, $home_text);
2240 print <$fd>;
2241 close $fd;
2242 print "</div>\n";
2244 print "<table class=\"project_list\">\n" .
2245 "<tr>\n";
2246 $order ||= "project";
2247 if ($order eq "project") {
2248 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2249 print "<th>Project</th>\n";
2250 } else {
2251 print "<th>" .
2252 $cgi->a({-href => href(project=>undef, order=>'project'),
2253 -class => "header"}, "Project") .
2254 "</th>\n";
2256 if ($order eq "descr") {
2257 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2258 print "<th>Description</th>\n";
2259 } else {
2260 print "<th>" .
2261 $cgi->a({-href => href(project=>undef, order=>'descr'),
2262 -class => "header"}, "Description") .
2263 "</th>\n";
2265 if ($order eq "owner") {
2266 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2267 print "<th>Owner</th>\n";
2268 } else {
2269 print "<th>" .
2270 $cgi->a({-href => href(project=>undef, order=>'owner'),
2271 -class => "header"}, "Owner") .
2272 "</th>\n";
2274 if ($order eq "age") {
2275 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2276 print "<th>Last Change</th>\n";
2277 } else {
2278 print "<th>" .
2279 $cgi->a({-href => href(project=>undef, order=>'age'),
2280 -class => "header"}, "Last Change") .
2281 "</th>\n";
2283 print "<th></th>\n" .
2284 "</tr>\n";
2285 my $alternate = 1;
2286 foreach my $pr (@projects) {
2287 if ($alternate) {
2288 print "<tr class=\"dark\">\n";
2289 } else {
2290 print "<tr class=\"light\">\n";
2292 $alternate ^= 1;
2293 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2294 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2295 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2296 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2297 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2298 $pr->{'commit'}{'age_string'} . "</td>\n" .
2299 "<td class=\"link\">" .
2300 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2301 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2302 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2303 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2304 "</td>\n" .
2305 "</tr>\n";
2307 print "</table>\n";
2308 git_footer_html();
2311 sub git_project_index {
2312 my @projects = git_get_projects_list();
2314 print $cgi->header(
2315 -type => 'text/plain',
2316 -charset => 'utf-8',
2317 -content_disposition => 'inline; filename="index.aux"');
2319 foreach my $pr (@projects) {
2320 if (!exists $pr->{'owner'}) {
2321 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2324 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2325 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2326 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2327 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2328 $path =~ s/ /\+/g;
2329 $owner =~ s/ /\+/g;
2331 print "$path $owner\n";
2335 sub git_summary {
2336 my $descr = git_get_project_description($project) || "none";
2337 my $head = git_get_head_hash($project);
2338 my %co = parse_commit($head);
2339 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2341 my $owner = git_get_project_owner($project);
2343 my ($reflist, $refs) = git_get_refs_list();
2345 my @taglist;
2346 my @headlist;
2347 foreach my $ref (@$reflist) {
2348 if ($ref->{'name'} =~ s!^heads/!!) {
2349 push @headlist, $ref;
2350 } else {
2351 $ref->{'name'} =~ s!^tags/!!;
2352 push @taglist, $ref;
2356 git_header_html();
2357 git_print_page_nav('summary','', $head);
2359 print "<div class=\"title\">&nbsp;</div>\n";
2360 print "<table cellspacing=\"0\">\n" .
2361 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2362 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2363 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2364 # use per project git URL list in $projectroot/$project/cloneurl
2365 # or make project git URL from git base URL and project name
2366 my $url_tag = "URL";
2367 my @url_list = git_get_project_url_list($project);
2368 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2369 foreach my $git_url (@url_list) {
2370 next unless $git_url;
2371 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2372 $url_tag = "";
2374 print "</table>\n";
2376 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2377 git_get_head_hash($project)
2378 or die_error(undef, "Open git-rev-list failed");
2379 my @revlist = map { chomp; $_ } <$fd>;
2380 close $fd;
2381 git_print_header_div('shortlog');
2382 git_shortlog_body(\@revlist, 0, 15, $refs,
2383 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2385 if (@taglist) {
2386 git_print_header_div('tags');
2387 git_tags_body(\@taglist, 0, 15,
2388 $cgi->a({-href => href(action=>"tags")}, "..."));
2391 if (@headlist) {
2392 git_print_header_div('heads');
2393 git_heads_body(\@headlist, $head, 0, 15,
2394 $cgi->a({-href => href(action=>"heads")}, "..."));
2397 git_footer_html();
2400 sub git_tag {
2401 my $head = git_get_head_hash($project);
2402 git_header_html();
2403 git_print_page_nav('','', $head,undef,$head);
2404 my %tag = parse_tag($hash);
2405 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2406 print "<div class=\"title_text\">\n" .
2407 "<table cellspacing=\"0\">\n" .
2408 "<tr>\n" .
2409 "<td>object</td>\n" .
2410 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2411 $tag{'object'}) . "</td>\n" .
2412 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2413 $tag{'type'}) . "</td>\n" .
2414 "</tr>\n";
2415 if (defined($tag{'author'})) {
2416 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2417 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2418 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2419 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2420 "</td></tr>\n";
2422 print "</table>\n\n" .
2423 "</div>\n";
2424 print "<div class=\"page_body\">";
2425 my $comment = $tag{'comment'};
2426 foreach my $line (@$comment) {
2427 print esc_html($line) . "<br/>\n";
2429 print "</div>\n";
2430 git_footer_html();
2433 sub git_blame2 {
2434 my $fd;
2435 my $ftype;
2437 my ($have_blame) = gitweb_check_feature('blame');
2438 if (!$have_blame) {
2439 die_error('403 Permission denied', "Permission denied");
2441 die_error('404 Not Found', "File name not defined") if (!$file_name);
2442 $hash_base ||= git_get_head_hash($project);
2443 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2444 my %co = parse_commit($hash_base)
2445 or die_error(undef, "Reading commit failed");
2446 if (!defined $hash) {
2447 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2448 or die_error(undef, "Error looking up file");
2450 $ftype = git_get_type($hash);
2451 if ($ftype !~ "blob") {
2452 die_error("400 Bad Request", "Object is not a blob");
2454 open ($fd, "-|", git_cmd(), "blame", '-l', '--', $file_name, $hash_base)
2455 or die_error(undef, "Open git-blame failed");
2456 git_header_html();
2457 my $formats_nav =
2458 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2459 "blob") .
2460 " | " .
2461 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2462 "history") .
2463 " | " .
2464 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2465 "HEAD");
2466 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2467 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2468 git_print_page_path($file_name, $ftype, $hash_base);
2469 my @rev_color = (qw(light2 dark2));
2470 my $num_colors = scalar(@rev_color);
2471 my $current_color = 0;
2472 my $last_rev;
2473 print <<HTML;
2474 <div class="page_body">
2475 <table class="blame">
2476 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2477 HTML
2478 while (<$fd>) {
2479 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2480 my $full_rev = $1;
2481 my $rev = substr($full_rev, 0, 8);
2482 my $lineno = $2;
2483 my $data = $3;
2485 if (!defined $last_rev) {
2486 $last_rev = $full_rev;
2487 } elsif ($last_rev ne $full_rev) {
2488 $last_rev = $full_rev;
2489 $current_color = ++$current_color % $num_colors;
2491 print "<tr class=\"$rev_color[$current_color]\">\n";
2492 print "<td class=\"sha1\">" .
2493 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2494 esc_html($rev)) . "</td>\n";
2495 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2496 esc_html($lineno) . "</a></td>\n";
2497 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2498 print "</tr>\n";
2500 print "</table>\n";
2501 print "</div>";
2502 close $fd
2503 or print "Reading blob failed\n";
2504 git_footer_html();
2507 sub git_blame {
2508 my $fd;
2510 my ($have_blame) = gitweb_check_feature('blame');
2511 if (!$have_blame) {
2512 die_error('403 Permission denied', "Permission denied");
2514 die_error('404 Not Found', "File name not defined") if (!$file_name);
2515 $hash_base ||= git_get_head_hash($project);
2516 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2517 my %co = parse_commit($hash_base)
2518 or die_error(undef, "Reading commit failed");
2519 if (!defined $hash) {
2520 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2521 or die_error(undef, "Error lookup file");
2523 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2524 or die_error(undef, "Open git-annotate failed");
2525 git_header_html();
2526 my $formats_nav =
2527 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2528 "blob") .
2529 " | " .
2530 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2531 "history") .
2532 " | " .
2533 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2534 "HEAD");
2535 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2536 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2537 git_print_page_path($file_name, 'blob', $hash_base);
2538 print "<div class=\"page_body\">\n";
2539 print <<HTML;
2540 <table class="blame">
2541 <tr>
2542 <th>Commit</th>
2543 <th>Age</th>
2544 <th>Author</th>
2545 <th>Line</th>
2546 <th>Data</th>
2547 </tr>
2548 HTML
2549 my @line_class = (qw(light dark));
2550 my $line_class_len = scalar (@line_class);
2551 my $line_class_num = $#line_class;
2552 while (my $line = <$fd>) {
2553 my $long_rev;
2554 my $short_rev;
2555 my $author;
2556 my $time;
2557 my $lineno;
2558 my $data;
2559 my $age;
2560 my $age_str;
2561 my $age_class;
2563 chomp $line;
2564 $line_class_num = ($line_class_num + 1) % $line_class_len;
2566 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2567 $long_rev = $1;
2568 $author = $2;
2569 $time = $3;
2570 $lineno = $4;
2571 $data = $5;
2572 } else {
2573 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2574 next;
2576 $short_rev = substr ($long_rev, 0, 8);
2577 $age = time () - $time;
2578 $age_str = age_string ($age);
2579 $age_str =~ s/ /&nbsp;/g;
2580 $age_class = age_class($age);
2581 $author = esc_html ($author);
2582 $author =~ s/ /&nbsp;/g;
2584 $data = untabify($data);
2585 $data = esc_html ($data);
2587 print <<HTML;
2588 <tr class="$line_class[$line_class_num]">
2589 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2590 <td class="$age_class">$age_str</td>
2591 <td>$author</td>
2592 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2593 <td class="pre">$data</td>
2594 </tr>
2595 HTML
2596 } # while (my $line = <$fd>)
2597 print "</table>\n\n";
2598 close $fd
2599 or print "Reading blob failed.\n";
2600 print "</div>";
2601 git_footer_html();
2604 sub git_tags {
2605 my $head = git_get_head_hash($project);
2606 git_header_html();
2607 git_print_page_nav('','', $head,undef,$head);
2608 git_print_header_div('summary', $project);
2610 my ($taglist) = git_get_refs_list("tags");
2611 if (@$taglist) {
2612 git_tags_body($taglist);
2614 git_footer_html();
2617 sub git_heads {
2618 my $head = git_get_head_hash($project);
2619 git_header_html();
2620 git_print_page_nav('','', $head,undef,$head);
2621 git_print_header_div('summary', $project);
2623 my ($headlist) = git_get_refs_list("heads");
2624 if (@$headlist) {
2625 git_heads_body($headlist, $head);
2627 git_footer_html();
2630 sub git_blob_plain {
2631 my $expires;
2633 if (!defined $hash) {
2634 if (defined $file_name) {
2635 my $base = $hash_base || git_get_head_hash($project);
2636 $hash = git_get_hash_by_path($base, $file_name, "blob")
2637 or die_error(undef, "Error lookup file");
2638 } else {
2639 die_error(undef, "No file name defined");
2641 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2642 # blobs defined by non-textual hash id's can be cached
2643 $expires = "+1d";
2646 my $type = shift;
2647 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2648 or die_error(undef, "Couldn't cat $file_name, $hash");
2650 $type ||= blob_mimetype($fd, $file_name);
2652 # save as filename, even when no $file_name is given
2653 my $save_as = "$hash";
2654 if (defined $file_name) {
2655 $save_as = $file_name;
2656 } elsif ($type =~ m/^text\//) {
2657 $save_as .= '.txt';
2660 print $cgi->header(
2661 -type => "$type",
2662 -expires=>$expires,
2663 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2664 undef $/;
2665 binmode STDOUT, ':raw';
2666 print <$fd>;
2667 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2668 $/ = "\n";
2669 close $fd;
2672 sub git_blob {
2673 my $expires;
2675 if (!defined $hash) {
2676 if (defined $file_name) {
2677 my $base = $hash_base || git_get_head_hash($project);
2678 $hash = git_get_hash_by_path($base, $file_name, "blob")
2679 or die_error(undef, "Error lookup file");
2680 } else {
2681 die_error(undef, "No file name defined");
2683 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2684 # blobs defined by non-textual hash id's can be cached
2685 $expires = "+1d";
2688 my ($have_blame) = gitweb_check_feature('blame');
2689 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2690 or die_error(undef, "Couldn't cat $file_name, $hash");
2691 my $mimetype = blob_mimetype($fd, $file_name);
2692 if ($mimetype !~ m/^text\//) {
2693 close $fd;
2694 return git_blob_plain($mimetype);
2696 git_header_html(undef, $expires);
2697 my $formats_nav = '';
2698 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2699 if (defined $file_name) {
2700 if ($have_blame) {
2701 $formats_nav .=
2702 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2703 hash=>$hash, file_name=>$file_name)},
2704 "blame") .
2705 " | ";
2707 $formats_nav .=
2708 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2709 hash=>$hash, file_name=>$file_name)},
2710 "history") .
2711 " | " .
2712 $cgi->a({-href => href(action=>"blob_plain",
2713 hash=>$hash, file_name=>$file_name)},
2714 "raw") .
2715 " | " .
2716 $cgi->a({-href => href(action=>"blob",
2717 hash_base=>"HEAD", file_name=>$file_name)},
2718 "HEAD");
2719 } else {
2720 $formats_nav .=
2721 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2723 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2724 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2725 } else {
2726 print "<div class=\"page_nav\">\n" .
2727 "<br/><br/></div>\n" .
2728 "<div class=\"title\">$hash</div>\n";
2730 git_print_page_path($file_name, "blob", $hash_base);
2731 print "<div class=\"page_body\">\n";
2732 my $nr;
2733 while (my $line = <$fd>) {
2734 chomp $line;
2735 $nr++;
2736 $line = untabify($line);
2737 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2738 $nr, $nr, $nr, esc_html($line);
2740 close $fd
2741 or print "Reading blob failed.\n";
2742 print "</div>";
2743 git_footer_html();
2746 sub git_tree {
2747 my $have_snapshot = gitweb_have_snapshot();
2749 if (!defined $hash) {
2750 $hash = git_get_head_hash($project);
2751 if (defined $file_name) {
2752 my $base = $hash_base || $hash;
2753 $hash = git_get_hash_by_path($base, $file_name, "tree");
2755 if (!defined $hash_base) {
2756 $hash_base = $hash;
2759 $/ = "\0";
2760 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2761 or die_error(undef, "Open git-ls-tree failed");
2762 my @entries = map { chomp; $_ } <$fd>;
2763 close $fd or die_error(undef, "Reading tree failed");
2764 $/ = "\n";
2766 my $refs = git_get_references();
2767 my $ref = format_ref_marker($refs, $hash_base);
2768 git_header_html();
2769 my $base = "";
2770 my ($have_blame) = gitweb_check_feature('blame');
2771 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2772 my @views_nav = ();
2773 if (defined $file_name) {
2774 push @views_nav,
2775 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2776 hash=>$hash, file_name=>$file_name)},
2777 "history"),
2778 $cgi->a({-href => href(action=>"tree",
2779 hash_base=>"HEAD", file_name=>$file_name)},
2780 "HEAD"),
2782 if ($have_snapshot) {
2783 # FIXME: Should be available when we have no hash base as well.
2784 push @views_nav,
2785 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2786 "snapshot");
2788 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2789 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2790 } else {
2791 undef $hash_base;
2792 print "<div class=\"page_nav\">\n";
2793 print "<br/><br/></div>\n";
2794 print "<div class=\"title\">$hash</div>\n";
2796 if (defined $file_name) {
2797 $base = esc_html("$file_name/");
2799 git_print_page_path($file_name, 'tree', $hash_base);
2800 print "<div class=\"page_body\">\n";
2801 print "<table cellspacing=\"0\">\n";
2802 my $alternate = 1;
2803 foreach my $line (@entries) {
2804 my %t = parse_ls_tree_line($line, -z => 1);
2806 if ($alternate) {
2807 print "<tr class=\"dark\">\n";
2808 } else {
2809 print "<tr class=\"light\">\n";
2811 $alternate ^= 1;
2813 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2815 print "</tr>\n";
2817 print "</table>\n" .
2818 "</div>";
2819 git_footer_html();
2822 sub git_snapshot {
2823 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2824 my $have_snapshot = (defined $ctype && defined $suffix);
2825 if (!$have_snapshot) {
2826 die_error('403 Permission denied', "Permission denied");
2829 if (!defined $hash) {
2830 $hash = git_get_head_hash($project);
2833 my $filename = basename($project) . "-$hash.tar.$suffix";
2835 print $cgi->header(
2836 -type => 'application/x-tar',
2837 -content_encoding => $ctype,
2838 -content_disposition => 'inline; filename="' . "$filename" . '"',
2839 -status => '200 OK');
2841 my $git_command = git_cmd_str();
2842 open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2843 die_error(undef, "Execute git-tar-tree failed.");
2844 binmode STDOUT, ':raw';
2845 print <$fd>;
2846 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2847 close $fd;
2851 sub git_log {
2852 my $head = git_get_head_hash($project);
2853 if (!defined $hash) {
2854 $hash = $head;
2856 if (!defined $page) {
2857 $page = 0;
2859 my $refs = git_get_references();
2861 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2862 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2863 or die_error(undef, "Open git-rev-list failed");
2864 my @revlist = map { chomp; $_ } <$fd>;
2865 close $fd;
2867 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2869 git_header_html();
2870 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2872 if (!@revlist) {
2873 my %co = parse_commit($hash);
2875 git_print_header_div('summary', $project);
2876 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2878 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2879 my $commit = $revlist[$i];
2880 my $ref = format_ref_marker($refs, $commit);
2881 my %co = parse_commit($commit);
2882 next if !%co;
2883 my %ad = parse_date($co{'author_epoch'});
2884 git_print_header_div('commit',
2885 "<span class=\"age\">$co{'age_string'}</span>" .
2886 esc_html($co{'title'}) . $ref,
2887 $commit);
2888 print "<div class=\"title_text\">\n" .
2889 "<div class=\"log_link\">\n" .
2890 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2891 " | " .
2892 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2893 " | " .
2894 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
2895 "<br/>\n" .
2896 "</div>\n" .
2897 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2898 "</div>\n";
2900 print "<div class=\"log_body\">\n";
2901 git_print_simplified_log($co{'comment'});
2902 print "</div>\n";
2904 git_footer_html();
2907 sub git_commit {
2908 my %co = parse_commit($hash);
2909 if (!%co) {
2910 die_error(undef, "Unknown commit object");
2912 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2913 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2915 my $parent = $co{'parent'};
2916 if (!defined $parent) {
2917 $parent = "--root";
2919 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2920 or die_error(undef, "Open git-diff-tree failed");
2921 my @difftree = map { chomp; $_ } <$fd>;
2922 close $fd or die_error(undef, "Reading git-diff-tree failed");
2924 # non-textual hash id's can be cached
2925 my $expires;
2926 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2927 $expires = "+1d";
2929 my $refs = git_get_references();
2930 my $ref = format_ref_marker($refs, $co{'id'});
2932 my $have_snapshot = gitweb_have_snapshot();
2934 my @views_nav = ();
2935 if (defined $file_name && defined $co{'parent'}) {
2936 push @views_nav,
2937 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2938 "blame");
2940 if (defined $co{'parent'}) {
2941 push @views_nav,
2942 $cgi->a({-href => href(action=>"shortlog", hash=>$hash)}, "shortlog"),
2943 $cgi->a({-href => href(action=>"log", hash=>$hash)}, "log");
2945 git_header_html(undef, $expires);
2946 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2947 $hash, $co{'tree'}, $hash,
2948 join (' | ', @views_nav));
2950 if (defined $co{'parent'}) {
2951 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2952 } else {
2953 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2955 print "<div class=\"title_text\">\n" .
2956 "<table cellspacing=\"0\">\n";
2957 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2958 "<tr>" .
2959 "<td></td><td> $ad{'rfc2822'}";
2960 if ($ad{'hour_local'} < 6) {
2961 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2962 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2963 } else {
2964 printf(" (%02d:%02d %s)",
2965 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2967 print "</td>" .
2968 "</tr>\n";
2969 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2970 print "<tr><td></td><td> $cd{'rfc2822'}" .
2971 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2972 "</td></tr>\n";
2973 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2974 print "<tr>" .
2975 "<td>tree</td>" .
2976 "<td class=\"sha1\">" .
2977 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2978 class => "list"}, $co{'tree'}) .
2979 "</td>" .
2980 "<td class=\"link\">" .
2981 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2982 "tree");
2983 if ($have_snapshot) {
2984 print " | " .
2985 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2987 print "</td>" .
2988 "</tr>\n";
2989 my $parents = $co{'parents'};
2990 foreach my $par (@$parents) {
2991 print "<tr>" .
2992 "<td>parent</td>" .
2993 "<td class=\"sha1\">" .
2994 $cgi->a({-href => href(action=>"commit", hash=>$par),
2995 class => "list"}, $par) .
2996 "</td>" .
2997 "<td class=\"link\">" .
2998 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2999 " | " .
3000 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3001 "</td>" .
3002 "</tr>\n";
3004 print "</table>".
3005 "</div>\n";
3007 print "<div class=\"page_body\">\n";
3008 git_print_log($co{'comment'});
3009 print "</div>\n";
3011 git_difftree_body(\@difftree, $hash, $parent);
3013 git_footer_html();
3016 sub git_blobdiff {
3017 my $format = shift || 'html';
3019 my $fd;
3020 my @difftree;
3021 my %diffinfo;
3022 my $expires;
3024 # preparing $fd and %diffinfo for git_patchset_body
3025 # new style URI
3026 if (defined $hash_base && defined $hash_parent_base) {
3027 if (defined $file_name) {
3028 # read raw output
3029 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3030 "--", $file_name
3031 or die_error(undef, "Open git-diff-tree failed");
3032 @difftree = map { chomp; $_ } <$fd>;
3033 close $fd
3034 or die_error(undef, "Reading git-diff-tree failed");
3035 @difftree
3036 or die_error('404 Not Found', "Blob diff not found");
3038 } elsif (defined $hash &&
3039 $hash =~ /[0-9a-fA-F]{40}/) {
3040 # try to find filename from $hash
3042 # read filtered raw output
3043 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3044 or die_error(undef, "Open git-diff-tree failed");
3045 @difftree =
3046 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3047 # $hash == to_id
3048 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3049 map { chomp; $_ } <$fd>;
3050 close $fd
3051 or die_error(undef, "Reading git-diff-tree failed");
3052 @difftree
3053 or die_error('404 Not Found', "Blob diff not found");
3055 } else {
3056 die_error('404 Not Found', "Missing one of the blob diff parameters");
3059 if (@difftree > 1) {
3060 die_error('404 Not Found', "Ambiguous blob diff specification");
3063 %diffinfo = parse_difftree_raw_line($difftree[0]);
3064 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3065 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3067 $hash_parent ||= $diffinfo{'from_id'};
3068 $hash ||= $diffinfo{'to_id'};
3070 # non-textual hash id's can be cached
3071 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3072 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3073 $expires = '+1d';
3076 # open patch output
3077 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3078 '-p', $hash_parent_base, $hash_base,
3079 "--", $file_name
3080 or die_error(undef, "Open git-diff-tree failed");
3083 # old/legacy style URI
3084 if (!%diffinfo && # if new style URI failed
3085 defined $hash && defined $hash_parent) {
3086 # fake git-diff-tree raw output
3087 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3088 $diffinfo{'from_id'} = $hash_parent;
3089 $diffinfo{'to_id'} = $hash;
3090 if (defined $file_name) {
3091 if (defined $file_parent) {
3092 $diffinfo{'status'} = '2';
3093 $diffinfo{'from_file'} = $file_parent;
3094 $diffinfo{'to_file'} = $file_name;
3095 } else { # assume not renamed
3096 $diffinfo{'status'} = '1';
3097 $diffinfo{'from_file'} = $file_name;
3098 $diffinfo{'to_file'} = $file_name;
3100 } else { # no filename given
3101 $diffinfo{'status'} = '2';
3102 $diffinfo{'from_file'} = $hash_parent;
3103 $diffinfo{'to_file'} = $hash;
3106 # non-textual hash id's can be cached
3107 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3108 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3109 $expires = '+1d';
3112 # open patch output
3113 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3114 or die_error(undef, "Open git-diff failed");
3115 } else {
3116 die_error('404 Not Found', "Missing one of the blob diff parameters")
3117 unless %diffinfo;
3120 # header
3121 if ($format eq 'html') {
3122 my $formats_nav =
3123 $cgi->a({-href => href(action=>"blobdiff_plain",
3124 hash=>$hash, hash_parent=>$hash_parent,
3125 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3126 file_name=>$file_name, file_parent=>$file_parent)},
3127 "raw");
3128 git_header_html(undef, $expires);
3129 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3130 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3131 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3132 } else {
3133 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3134 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3136 if (defined $file_name) {
3137 git_print_page_path($file_name, "blob", $hash_base);
3138 } else {
3139 print "<div class=\"page_path\"></div>\n";
3142 } elsif ($format eq 'plain') {
3143 print $cgi->header(
3144 -type => 'text/plain',
3145 -charset => 'utf-8',
3146 -expires => $expires,
3147 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3149 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3151 } else {
3152 die_error(undef, "Unknown blobdiff format");
3155 # patch
3156 if ($format eq 'html') {
3157 print "<div class=\"page_body\">\n";
3159 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3160 close $fd;
3162 print "</div>\n"; # class="page_body"
3163 git_footer_html();
3165 } else {
3166 while (my $line = <$fd>) {
3167 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3168 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3170 print $line;
3172 last if $line =~ m!^\+\+\+!;
3174 local $/ = undef;
3175 print <$fd>;
3176 close $fd;
3180 sub git_blobdiff_plain {
3181 git_blobdiff('plain');
3184 sub git_commitdiff {
3185 my $format = shift || 'html';
3186 my %co = parse_commit($hash);
3187 if (!%co) {
3188 die_error(undef, "Unknown commit object");
3190 if (!defined $hash_parent) {
3191 $hash_parent = $co{'parent'} || '--root';
3194 # read commitdiff
3195 my $fd;
3196 my @difftree;
3197 if ($format eq 'html') {
3198 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3199 "--patch-with-raw", "--full-index", $hash_parent, $hash
3200 or die_error(undef, "Open git-diff-tree failed");
3202 while (chomp(my $line = <$fd>)) {
3203 # empty line ends raw part of diff-tree output
3204 last unless $line;
3205 push @difftree, $line;
3208 } elsif ($format eq 'plain') {
3209 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3210 '-p', $hash_parent, $hash
3211 or die_error(undef, "Open git-diff-tree failed");
3213 } else {
3214 die_error(undef, "Unknown commitdiff format");
3217 # non-textual hash id's can be cached
3218 my $expires;
3219 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3220 $expires = "+1d";
3223 # write commit message
3224 if ($format eq 'html') {
3225 my $refs = git_get_references();
3226 my $ref = format_ref_marker($refs, $co{'id'});
3227 my $formats_nav =
3228 $cgi->a({-href => href(action=>"commitdiff_plain",
3229 hash=>$hash, hash_parent=>$hash_parent)},
3230 "raw");
3232 git_header_html(undef, $expires);
3233 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3234 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3235 git_print_authorship(\%co);
3236 print "<div class=\"page_body\">\n";
3237 print "<div class=\"log\">\n";
3238 git_print_simplified_log($co{'comment'}, 1); # skip title
3239 print "</div>\n"; # class="log"
3241 } elsif ($format eq 'plain') {
3242 my $refs = git_get_references("tags");
3243 my $tagname = git_get_rev_name_tags($hash);
3244 my $filename = basename($project) . "-$hash.patch";
3246 print $cgi->header(
3247 -type => 'text/plain',
3248 -charset => 'utf-8',
3249 -expires => $expires,
3250 -content_disposition => 'inline; filename="' . "$filename" . '"');
3251 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3252 print <<TEXT;
3253 From: $co{'author'}
3254 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3255 Subject: $co{'title'}
3256 TEXT
3257 print "X-Git-Tag: $tagname\n" if $tagname;
3258 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3260 foreach my $line (@{$co{'comment'}}) {
3261 print "$line\n";
3263 print "---\n\n";
3266 # write patch
3267 if ($format eq 'html') {
3268 git_difftree_body(\@difftree, $hash, $hash_parent);
3269 print "<br/>\n";
3271 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3272 close $fd;
3273 print "</div>\n"; # class="page_body"
3274 git_footer_html();
3276 } elsif ($format eq 'plain') {
3277 local $/ = undef;
3278 print <$fd>;
3279 close $fd
3280 or print "Reading git-diff-tree failed\n";
3284 sub git_commitdiff_plain {
3285 git_commitdiff('plain');
3288 sub git_history {
3289 if (!defined $hash_base) {
3290 $hash_base = git_get_head_hash($project);
3292 if (!defined $page) {
3293 $page = 0;
3295 my $ftype;
3296 my %co = parse_commit($hash_base);
3297 if (!%co) {
3298 die_error(undef, "Unknown commit object");
3301 my $refs = git_get_references();
3302 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3304 if (!defined $hash && defined $file_name) {
3305 $hash = git_get_hash_by_path($hash_base, $file_name);
3307 if (defined $hash) {
3308 $ftype = git_get_type($hash);
3311 open my $fd, "-|",
3312 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3313 or die_error(undef, "Open git-rev-list-failed");
3314 my @revlist = map { chomp; $_ } <$fd>;
3315 close $fd
3316 or die_error(undef, "Reading git-rev-list failed");
3318 my $paging_nav = '';
3319 if ($page > 0) {
3320 $paging_nav .=
3321 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3322 file_name=>$file_name)},
3323 "first");
3324 $paging_nav .= " &sdot; " .
3325 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3326 file_name=>$file_name, page=>$page-1),
3327 -accesskey => "p", -title => "Alt-p"}, "prev");
3328 } else {
3329 $paging_nav .= "first";
3330 $paging_nav .= " &sdot; prev";
3332 if ($#revlist >= (100 * ($page+1)-1)) {
3333 $paging_nav .= " &sdot; " .
3334 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3335 file_name=>$file_name, page=>$page+1),
3336 -accesskey => "n", -title => "Alt-n"}, "next");
3337 } else {
3338 $paging_nav .= " &sdot; next";
3340 my $next_link = '';
3341 if ($#revlist >= (100 * ($page+1)-1)) {
3342 $next_link =
3343 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3344 file_name=>$file_name, page=>$page+1),
3345 -title => "Alt-n"}, "next");
3348 git_header_html();
3349 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3350 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3351 git_print_page_path($file_name, $ftype, $hash_base);
3353 git_history_body(\@revlist, ($page * 100), $#revlist,
3354 $refs, $hash_base, $ftype, $next_link);
3356 git_footer_html();
3359 sub git_search {
3360 if (!defined $searchtext) {
3361 die_error(undef, "Text field empty");
3363 if (!defined $hash) {
3364 $hash = git_get_head_hash($project);
3366 my %co = parse_commit($hash);
3367 if (!%co) {
3368 die_error(undef, "Unknown commit object");
3371 my $commit_search = 1;
3372 my $author_search = 0;
3373 my $committer_search = 0;
3374 my $pickaxe_search = 0;
3375 if ($searchtext =~ s/^author\\://i) {
3376 $author_search = 1;
3377 } elsif ($searchtext =~ s/^committer\\://i) {
3378 $committer_search = 1;
3379 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3380 $commit_search = 0;
3381 $pickaxe_search = 1;
3383 # pickaxe may take all resources of your box and run for several minutes
3384 # with every query - so decide by yourself how public you make this feature
3385 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3386 if (!$have_pickaxe) {
3387 die_error('403 Permission denied', "Permission denied");
3390 git_header_html();
3391 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3392 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3394 print "<table cellspacing=\"0\">\n";
3395 my $alternate = 1;
3396 if ($commit_search) {
3397 $/ = "\0";
3398 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3399 while (my $commit_text = <$fd>) {
3400 if (!grep m/$searchtext/i, $commit_text) {
3401 next;
3403 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3404 next;
3406 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3407 next;
3409 my @commit_lines = split "\n", $commit_text;
3410 my %co = parse_commit(undef, \@commit_lines);
3411 if (!%co) {
3412 next;
3414 if ($alternate) {
3415 print "<tr class=\"dark\">\n";
3416 } else {
3417 print "<tr class=\"light\">\n";
3419 $alternate ^= 1;
3420 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3421 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3422 "<td>" .
3423 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3424 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3425 my $comment = $co{'comment'};
3426 foreach my $line (@$comment) {
3427 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3428 my $lead = esc_html($1) || "";
3429 $lead = chop_str($lead, 30, 10);
3430 my $match = esc_html($2) || "";
3431 my $trail = esc_html($3) || "";
3432 $trail = chop_str($trail, 30, 10);
3433 my $text = "$lead<span class=\"match\">$match</span>$trail";
3434 print chop_str($text, 80, 5) . "<br/>\n";
3437 print "</td>\n" .
3438 "<td class=\"link\">" .
3439 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3440 " | " .
3441 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3442 print "</td>\n" .
3443 "</tr>\n";
3445 close $fd;
3448 if ($pickaxe_search) {
3449 $/ = "\n";
3450 my $git_command = git_cmd_str();
3451 open my $fd, "-|", "$git_command rev-list $hash | " .
3452 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3453 undef %co;
3454 my @files;
3455 while (my $line = <$fd>) {
3456 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3457 my %set;
3458 $set{'file'} = $6;
3459 $set{'from_id'} = $3;
3460 $set{'to_id'} = $4;
3461 $set{'id'} = $set{'to_id'};
3462 if ($set{'id'} =~ m/0{40}/) {
3463 $set{'id'} = $set{'from_id'};
3465 if ($set{'id'} =~ m/0{40}/) {
3466 next;
3468 push @files, \%set;
3469 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3470 if (%co) {
3471 if ($alternate) {
3472 print "<tr class=\"dark\">\n";
3473 } else {
3474 print "<tr class=\"light\">\n";
3476 $alternate ^= 1;
3477 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3478 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3479 "<td>" .
3480 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3481 -class => "list subject"},
3482 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3483 while (my $setref = shift @files) {
3484 my %set = %$setref;
3485 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3486 hash=>$set{'id'}, file_name=>$set{'file'}),
3487 -class => "list"},
3488 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3489 "<br/>\n";
3491 print "</td>\n" .
3492 "<td class=\"link\">" .
3493 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3494 " | " .
3495 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3496 print "</td>\n" .
3497 "</tr>\n";
3499 %co = parse_commit($1);
3502 close $fd;
3504 print "</table>\n";
3505 git_footer_html();
3508 sub git_shortlog {
3509 my $head = git_get_head_hash($project);
3510 if (!defined $hash) {
3511 $hash = $head;
3513 if (!defined $page) {
3514 $page = 0;
3516 my $refs = git_get_references();
3518 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3519 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3520 or die_error(undef, "Open git-rev-list failed");
3521 my @revlist = map { chomp; $_ } <$fd>;
3522 close $fd;
3524 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3525 my $next_link = '';
3526 if ($#revlist >= (100 * ($page+1)-1)) {
3527 $next_link =
3528 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3529 -title => "Alt-n"}, "next");
3533 git_header_html();
3534 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3535 git_print_header_div('summary', $project);
3537 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3539 git_footer_html();
3542 ## ......................................................................
3543 ## feeds (RSS, OPML)
3545 sub git_rss {
3546 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3547 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3548 or die_error(undef, "Open git-rev-list failed");
3549 my @revlist = map { chomp; $_ } <$fd>;
3550 close $fd or die_error(undef, "Reading git-rev-list failed");
3551 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3552 print <<XML;
3553 <?xml version="1.0" encoding="utf-8"?>
3554 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3555 <channel>
3556 <title>$project $my_uri $my_url</title>
3557 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3558 <description>$project log</description>
3559 <language>en</language>
3562 for (my $i = 0; $i <= $#revlist; $i++) {
3563 my $commit = $revlist[$i];
3564 my %co = parse_commit($commit);
3565 # we read 150, we always show 30 and the ones more recent than 48 hours
3566 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3567 last;
3569 my %cd = parse_date($co{'committer_epoch'});
3570 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3571 $co{'parent'}, $co{'id'}
3572 or next;
3573 my @difftree = map { chomp; $_ } <$fd>;
3574 close $fd
3575 or next;
3576 print "<item>\n" .
3577 "<title>" .
3578 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3579 "</title>\n" .
3580 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3581 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3582 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3583 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3584 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3585 "<content:encoded>" .
3586 "<![CDATA[\n";
3587 my $comment = $co{'comment'};
3588 foreach my $line (@$comment) {
3589 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3590 print "$line<br/>\n";
3592 print "<br/>\n";
3593 foreach my $line (@difftree) {
3594 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3595 next;
3597 my $file = esc_html(unquote($7));
3598 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3599 print "$file<br/>\n";
3601 print "]]>\n" .
3602 "</content:encoded>\n" .
3603 "</item>\n";
3605 print "</channel></rss>";
3608 sub git_opml {
3609 my @list = git_get_projects_list();
3611 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3612 print <<XML;
3613 <?xml version="1.0" encoding="utf-8"?>
3614 <opml version="1.0">
3615 <head>
3616 <title>$site_name Git OPML Export</title>
3617 </head>
3618 <body>
3619 <outline text="git RSS feeds">
3622 foreach my $pr (@list) {
3623 my %proj = %$pr;
3624 my $head = git_get_head_hash($proj{'path'});
3625 if (!defined $head) {
3626 next;
3628 $git_dir = "$projectroot/$proj{'path'}";
3629 my %co = parse_commit($head);
3630 if (!%co) {
3631 next;
3634 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3635 my $rss = "$my_url?p=$proj{'path'};a=rss";
3636 my $html = "$my_url?p=$proj{'path'};a=summary";
3637 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3639 print <<XML;
3640 </outline>
3641 </body>
3642 </opml>