gitweb: Remove excessively redundant entries from git_difftree_body
[git.git] / gitweb / gitweb.perl
blob88a8bcdbff21c7d4d27379b28c2acf7a135cb4b2
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 # To enable system wide have in $GITWEB_CONFIG
159 # $feature{'pickaxe'}{'default'} = [1];
160 # To have project specific config enable override in $GITWEB_CONFIG
161 # $feature{'pickaxe'}{'override'} = 1;
162 # and in project config gitweb.pickaxe = 0|1;
164 sub feature_pickaxe {
165 my ($val) = git_get_project_config('pickaxe', '--bool');
167 if ($val eq 'true') {
168 return (1);
169 } elsif ($val eq 'false') {
170 return (0);
173 return ($_[0]);
176 # rename detection options for git-diff and git-diff-tree
177 # - default is '-M', with the cost proportional to
178 # (number of removed files) * (number of new files).
179 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
180 # (number of changed files + number of removed files) * (number of new files)
181 # - even more costly is '-C', '--find-copies-harder' with cost
182 # (number of files in the original tree) * (number of new files)
183 # - one might want to include '-B' option, e.g. '-B', '-M'
184 our @diff_opts = ('-M'); # taken from git_commit
186 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
187 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
189 # version of the core git binary
190 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
192 $projects_list ||= $projectroot;
194 # ======================================================================
195 # input validation and dispatch
196 our $action = $cgi->param('a');
197 if (defined $action) {
198 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
199 die_error(undef, "Invalid action parameter");
203 # parameters which are pathnames
204 our $project = $cgi->param('p');
205 if (defined $project) {
206 if (!validate_pathname($project) ||
207 !(-d "$projectroot/$project") ||
208 !(-e "$projectroot/$project/HEAD") ||
209 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
210 ($strict_export && !project_in_list($project))) {
211 undef $project;
212 die_error(undef, "No such project");
216 our $file_name = $cgi->param('f');
217 if (defined $file_name) {
218 if (!validate_pathname($file_name)) {
219 die_error(undef, "Invalid file parameter");
223 our $file_parent = $cgi->param('fp');
224 if (defined $file_parent) {
225 if (!validate_pathname($file_parent)) {
226 die_error(undef, "Invalid file parent parameter");
230 # parameters which are refnames
231 our $hash = $cgi->param('h');
232 if (defined $hash) {
233 if (!validate_refname($hash)) {
234 die_error(undef, "Invalid hash parameter");
238 our $hash_parent = $cgi->param('hp');
239 if (defined $hash_parent) {
240 if (!validate_refname($hash_parent)) {
241 die_error(undef, "Invalid hash parent parameter");
245 our $hash_base = $cgi->param('hb');
246 if (defined $hash_base) {
247 if (!validate_refname($hash_base)) {
248 die_error(undef, "Invalid hash base parameter");
252 our $hash_parent_base = $cgi->param('hpb');
253 if (defined $hash_parent_base) {
254 if (!validate_refname($hash_parent_base)) {
255 die_error(undef, "Invalid hash parent base parameter");
259 # other parameters
260 our $page = $cgi->param('pg');
261 if (defined $page) {
262 if ($page =~ m/[^0-9]/) {
263 die_error(undef, "Invalid page parameter");
267 our $searchtext = $cgi->param('s');
268 if (defined $searchtext) {
269 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
270 die_error(undef, "Invalid search parameter");
272 $searchtext = quotemeta $searchtext;
275 # now read PATH_INFO and use it as alternative to parameters
276 sub evaluate_path_info {
277 return if defined $project;
278 my $path_info = $ENV{"PATH_INFO"};
279 return if !$path_info;
280 $path_info =~ s,^/+,,;
281 return if !$path_info;
282 # find which part of PATH_INFO is project
283 $project = $path_info;
284 $project =~ s,/+$,,;
285 while ($project && !-e "$projectroot/$project/HEAD") {
286 $project =~ s,/*[^/]*$,,;
288 # validate project
289 $project = validate_pathname($project);
290 if (!$project ||
291 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
292 ($strict_export && !project_in_list($project))) {
293 undef $project;
294 return;
296 # do not change any parameters if an action is given using the query string
297 return if $action;
298 $path_info =~ s,^$project/*,,;
299 my ($refname, $pathname) = split(/:/, $path_info, 2);
300 if (defined $pathname) {
301 # we got "project.git/branch:filename" or "project.git/branch:dir/"
302 # we could use git_get_type(branch:pathname), but it needs $git_dir
303 $pathname =~ s,^/+,,;
304 if (!$pathname || substr($pathname, -1) eq "/") {
305 $action ||= "tree";
306 $pathname =~ s,/$,,;
307 } else {
308 $action ||= "blob_plain";
310 $hash_base ||= validate_refname($refname);
311 $file_name ||= validate_pathname($pathname);
312 } elsif (defined $refname) {
313 # we got "project.git/branch"
314 $action ||= "shortlog";
315 $hash ||= validate_refname($refname);
318 evaluate_path_info();
320 # path to the current git repository
321 our $git_dir;
322 $git_dir = "$projectroot/$project" if $project;
324 # dispatch
325 my %actions = (
326 "blame" => \&git_blame2,
327 "blobdiff" => \&git_blobdiff,
328 "blobdiff_plain" => \&git_blobdiff_plain,
329 "blob" => \&git_blob,
330 "blob_plain" => \&git_blob_plain,
331 "commitdiff" => \&git_commitdiff,
332 "commitdiff_plain" => \&git_commitdiff_plain,
333 "commit" => \&git_commit,
334 "heads" => \&git_heads,
335 "history" => \&git_history,
336 "log" => \&git_log,
337 "rss" => \&git_rss,
338 "search" => \&git_search,
339 "shortlog" => \&git_shortlog,
340 "summary" => \&git_summary,
341 "tag" => \&git_tag,
342 "tags" => \&git_tags,
343 "tree" => \&git_tree,
344 "snapshot" => \&git_snapshot,
345 # those below don't need $project
346 "opml" => \&git_opml,
347 "project_list" => \&git_project_list,
348 "project_index" => \&git_project_index,
351 if (defined $project) {
352 $action ||= 'summary';
353 } else {
354 $action ||= 'project_list';
356 if (!defined($actions{$action})) {
357 die_error(undef, "Unknown action");
359 if ($action !~ m/^(opml|project_list|project_index)$/ &&
360 !$project) {
361 die_error(undef, "Project needed");
363 $actions{$action}->();
364 exit;
366 ## ======================================================================
367 ## action links
369 sub href(%) {
370 my %params = @_;
372 my @mapping = (
373 project => "p",
374 action => "a",
375 file_name => "f",
376 file_parent => "fp",
377 hash => "h",
378 hash_parent => "hp",
379 hash_base => "hb",
380 hash_parent_base => "hpb",
381 page => "pg",
382 order => "o",
383 searchtext => "s",
385 my %mapping = @mapping;
387 $params{'project'} = $project unless exists $params{'project'};
389 my @result = ();
390 for (my $i = 0; $i < @mapping; $i += 2) {
391 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
392 if (defined $params{$name}) {
393 push @result, $symbol . "=" . esc_param($params{$name});
396 return "$my_uri?" . join(';', @result);
400 ## ======================================================================
401 ## validation, quoting/unquoting and escaping
403 sub validate_pathname {
404 my $input = shift || return undef;
406 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
407 # at the beginning, at the end, and between slashes.
408 # also this catches doubled slashes
409 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
410 return undef;
412 # no null characters
413 if ($input =~ m!\0!) {
414 return undef;
416 return $input;
419 sub validate_refname {
420 my $input = shift || return undef;
422 # textual hashes are O.K.
423 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
424 return $input;
426 # it must be correct pathname
427 $input = validate_pathname($input)
428 or return undef;
429 # restrictions on ref name according to git-check-ref-format
430 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
431 return undef;
433 return $input;
436 # quote unsafe chars, but keep the slash, even when it's not
437 # correct, but quoted slashes look too horrible in bookmarks
438 sub esc_param {
439 my $str = shift;
440 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
441 $str =~ s/\+/%2B/g;
442 $str =~ s/ /\+/g;
443 return $str;
446 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
447 sub esc_url {
448 my $str = shift;
449 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
450 $str =~ s/\+/%2B/g;
451 $str =~ s/ /\+/g;
452 return $str;
455 # replace invalid utf8 character with SUBSTITUTION sequence
456 sub esc_html {
457 my $str = shift;
458 $str = decode("utf8", $str, Encode::FB_DEFAULT);
459 $str = escapeHTML($str);
460 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
461 return $str;
464 # git may return quoted and escaped filenames
465 sub unquote {
466 my $str = shift;
467 if ($str =~ m/^"(.*)"$/) {
468 $str = $1;
469 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
471 return $str;
474 # escape tabs (convert tabs to spaces)
475 sub untabify {
476 my $line = shift;
478 while ((my $pos = index($line, "\t")) != -1) {
479 if (my $count = (8 - ($pos % 8))) {
480 my $spaces = ' ' x $count;
481 $line =~ s/\t/$spaces/;
485 return $line;
488 sub project_in_list {
489 my $project = shift;
490 my @list = git_get_projects_list();
491 return @list && scalar(grep { $_->{'path'} eq $project } @list);
494 ## ----------------------------------------------------------------------
495 ## HTML aware string manipulation
497 sub chop_str {
498 my $str = shift;
499 my $len = shift;
500 my $add_len = shift || 10;
502 # allow only $len chars, but don't cut a word if it would fit in $add_len
503 # if it doesn't fit, cut it if it's still longer than the dots we would add
504 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
505 my $body = $1;
506 my $tail = $2;
507 if (length($tail) > 4) {
508 $tail = " ...";
509 $body =~ s/&[^;]*$//; # remove chopped character entities
511 return "$body$tail";
514 ## ----------------------------------------------------------------------
515 ## functions returning short strings
517 # CSS class for given age value (in seconds)
518 sub age_class {
519 my $age = shift;
521 if ($age < 60*60*2) {
522 return "age0";
523 } elsif ($age < 60*60*24*2) {
524 return "age1";
525 } else {
526 return "age2";
530 # convert age in seconds to "nn units ago" string
531 sub age_string {
532 my $age = shift;
533 my $age_str;
535 if ($age > 60*60*24*365*2) {
536 $age_str = (int $age/60/60/24/365);
537 $age_str .= " years ago";
538 } elsif ($age > 60*60*24*(365/12)*2) {
539 $age_str = int $age/60/60/24/(365/12);
540 $age_str .= " months ago";
541 } elsif ($age > 60*60*24*7*2) {
542 $age_str = int $age/60/60/24/7;
543 $age_str .= " weeks ago";
544 } elsif ($age > 60*60*24*2) {
545 $age_str = int $age/60/60/24;
546 $age_str .= " days ago";
547 } elsif ($age > 60*60*2) {
548 $age_str = int $age/60/60;
549 $age_str .= " hours ago";
550 } elsif ($age > 60*2) {
551 $age_str = int $age/60;
552 $age_str .= " min ago";
553 } elsif ($age > 2) {
554 $age_str = int $age;
555 $age_str .= " sec ago";
556 } else {
557 $age_str .= " right now";
559 return $age_str;
562 # convert file mode in octal to symbolic file mode string
563 sub mode_str {
564 my $mode = oct shift;
566 if (S_ISDIR($mode & S_IFMT)) {
567 return 'drwxr-xr-x';
568 } elsif (S_ISLNK($mode)) {
569 return 'lrwxrwxrwx';
570 } elsif (S_ISREG($mode)) {
571 # git cares only about the executable bit
572 if ($mode & S_IXUSR) {
573 return '-rwxr-xr-x';
574 } else {
575 return '-rw-r--r--';
577 } else {
578 return '----------';
582 # convert file mode in octal to file type string
583 sub file_type {
584 my $mode = shift;
586 if ($mode !~ m/^[0-7]+$/) {
587 return $mode;
588 } else {
589 $mode = oct $mode;
592 if (S_ISDIR($mode & S_IFMT)) {
593 return "directory";
594 } elsif (S_ISLNK($mode)) {
595 return "symlink";
596 } elsif (S_ISREG($mode)) {
597 return "file";
598 } else {
599 return "unknown";
603 ## ----------------------------------------------------------------------
604 ## functions returning short HTML fragments, or transforming HTML fragments
605 ## which don't beling to other sections
607 # format line of commit message or tag comment
608 sub format_log_line_html {
609 my $line = shift;
611 $line = esc_html($line);
612 $line =~ s/ /&nbsp;/g;
613 if ($line =~ m/([0-9a-fA-F]{40})/) {
614 my $hash_text = $1;
615 if (git_get_type($hash_text) eq "commit") {
616 my $link =
617 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
618 -class => "text"}, $hash_text);
619 $line =~ s/$hash_text/$link/;
622 return $line;
625 # format marker of refs pointing to given object
626 sub format_ref_marker {
627 my ($refs, $id) = @_;
628 my $markers = '';
630 if (defined $refs->{$id}) {
631 foreach my $ref (@{$refs->{$id}}) {
632 my ($type, $name) = qw();
633 # e.g. tags/v2.6.11 or heads/next
634 if ($ref =~ m!^(.*?)s?/(.*)$!) {
635 $type = $1;
636 $name = $2;
637 } else {
638 $type = "ref";
639 $name = $ref;
642 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
646 if ($markers) {
647 return ' <span class="refs">'. $markers . '</span>';
648 } else {
649 return "";
653 # format, perhaps shortened and with markers, title line
654 sub format_subject_html {
655 my ($long, $short, $href, $extra) = @_;
656 $extra = '' unless defined($extra);
658 if (length($short) < length($long)) {
659 return $cgi->a({-href => $href, -class => "list subject",
660 -title => $long},
661 esc_html($short) . $extra);
662 } else {
663 return $cgi->a({-href => $href, -class => "list subject"},
664 esc_html($long) . $extra);
668 sub format_diff_line {
669 my $line = shift;
670 my $char = substr($line, 0, 1);
671 my $diff_class = "";
673 chomp $line;
675 if ($char eq '+') {
676 $diff_class = " add";
677 } elsif ($char eq "-") {
678 $diff_class = " rem";
679 } elsif ($char eq "@") {
680 $diff_class = " chunk_header";
681 } elsif ($char eq "\\") {
682 $diff_class = " incomplete";
684 $line = untabify($line);
685 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
688 ## ----------------------------------------------------------------------
689 ## git utility subroutines, invoking git commands
691 # returns path to the core git executable and the --git-dir parameter as list
692 sub git_cmd {
693 return $GIT, '--git-dir='.$git_dir;
696 # returns path to the core git executable and the --git-dir parameter as string
697 sub git_cmd_str {
698 return join(' ', git_cmd());
701 # get HEAD ref of given project as hash
702 sub git_get_head_hash {
703 my $project = shift;
704 my $o_git_dir = $git_dir;
705 my $retval = undef;
706 $git_dir = "$projectroot/$project";
707 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
708 my $head = <$fd>;
709 close $fd;
710 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
711 $retval = $1;
714 if (defined $o_git_dir) {
715 $git_dir = $o_git_dir;
717 return $retval;
720 # get type of given object
721 sub git_get_type {
722 my $hash = shift;
724 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
725 my $type = <$fd>;
726 close $fd or return;
727 chomp $type;
728 return $type;
731 sub git_get_project_config {
732 my ($key, $type) = @_;
734 return unless ($key);
735 $key =~ s/^gitweb\.//;
736 return if ($key =~ m/\W/);
738 my @x = (git_cmd(), 'repo-config');
739 if (defined $type) { push @x, $type; }
740 push @x, "--get";
741 push @x, "gitweb.$key";
742 my $val = qx(@x);
743 chomp $val;
744 return ($val);
747 # get hash of given path at given ref
748 sub git_get_hash_by_path {
749 my $base = shift;
750 my $path = shift || return undef;
751 my $type = shift;
753 $path =~ s,/+$,,;
755 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
756 or die_error(undef, "Open git-ls-tree failed");
757 my $line = <$fd>;
758 close $fd or return undef;
760 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
761 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
762 if (defined $type && $type ne $2) {
763 # type doesn't match
764 return undef;
766 return $3;
769 ## ......................................................................
770 ## git utility functions, directly accessing git repository
772 sub git_get_project_description {
773 my $path = shift;
775 open my $fd, "$projectroot/$path/description" or return undef;
776 my $descr = <$fd>;
777 close $fd;
778 chomp $descr;
779 return $descr;
782 sub git_get_project_url_list {
783 my $path = shift;
785 open my $fd, "$projectroot/$path/cloneurl" or return;
786 my @git_project_url_list = map { chomp; $_ } <$fd>;
787 close $fd;
789 return wantarray ? @git_project_url_list : \@git_project_url_list;
792 sub git_get_projects_list {
793 my @list;
795 if (-d $projects_list) {
796 # search in directory
797 my $dir = $projects_list;
798 my $pfxlen = length("$dir");
800 File::Find::find({
801 follow_fast => 1, # follow symbolic links
802 dangling_symlinks => 0, # ignore dangling symlinks, silently
803 wanted => sub {
804 # skip project-list toplevel, if we get it.
805 return if (m!^[/.]$!);
806 # only directories can be git repositories
807 return unless (-d $_);
809 my $subdir = substr($File::Find::name, $pfxlen + 1);
810 # we check related file in $projectroot
811 if (-e "$projectroot/$subdir/HEAD" && (!$export_ok ||
812 -e "$projectroot/$subdir/$export_ok")) {
813 push @list, { path => $subdir };
814 $File::Find::prune = 1;
817 }, "$dir");
819 } elsif (-f $projects_list) {
820 # read from file(url-encoded):
821 # 'git%2Fgit.git Linus+Torvalds'
822 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
823 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
824 open my ($fd), $projects_list or return;
825 while (my $line = <$fd>) {
826 chomp $line;
827 my ($path, $owner) = split ' ', $line;
828 $path = unescape($path);
829 $owner = unescape($owner);
830 if (!defined $path) {
831 next;
833 if (-e "$projectroot/$path/HEAD" && (!$export_ok ||
834 -e "$projectroot/$path/$export_ok")) {
835 my $pr = {
836 path => $path,
837 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
839 push @list, $pr
842 close $fd;
844 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
845 return @list;
848 sub git_get_project_owner {
849 my $project = shift;
850 my $owner;
852 return undef unless $project;
854 # read from file (url-encoded):
855 # 'git%2Fgit.git Linus+Torvalds'
856 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
857 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
858 if (-f $projects_list) {
859 open (my $fd , $projects_list);
860 while (my $line = <$fd>) {
861 chomp $line;
862 my ($pr, $ow) = split ' ', $line;
863 $pr = unescape($pr);
864 $ow = unescape($ow);
865 if ($pr eq $project) {
866 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
867 last;
870 close $fd;
872 if (!defined $owner) {
873 $owner = get_file_owner("$projectroot/$project");
876 return $owner;
879 sub git_get_references {
880 my $type = shift || "";
881 my %refs;
882 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
883 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
884 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
885 or return;
887 while (my $line = <$fd>) {
888 chomp $line;
889 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
890 if (defined $refs{$1}) {
891 push @{$refs{$1}}, $2;
892 } else {
893 $refs{$1} = [ $2 ];
897 close $fd or return;
898 return \%refs;
901 sub git_get_rev_name_tags {
902 my $hash = shift || return undef;
904 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
905 or return;
906 my $name_rev = <$fd>;
907 close $fd;
909 if ($name_rev =~ m|^$hash tags/(.*)$|) {
910 return $1;
911 } else {
912 # catches also '$hash undefined' output
913 return undef;
917 ## ----------------------------------------------------------------------
918 ## parse to hash functions
920 sub parse_date {
921 my $epoch = shift;
922 my $tz = shift || "-0000";
924 my %date;
925 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
926 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
927 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
928 $date{'hour'} = $hour;
929 $date{'minute'} = $min;
930 $date{'mday'} = $mday;
931 $date{'day'} = $days[$wday];
932 $date{'month'} = $months[$mon];
933 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
934 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
935 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
936 $mday, $months[$mon], $hour ,$min;
938 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
939 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
940 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
941 $date{'hour_local'} = $hour;
942 $date{'minute_local'} = $min;
943 $date{'tz_local'} = $tz;
944 return %date;
947 sub parse_tag {
948 my $tag_id = shift;
949 my %tag;
950 my @comment;
952 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
953 $tag{'id'} = $tag_id;
954 while (my $line = <$fd>) {
955 chomp $line;
956 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
957 $tag{'object'} = $1;
958 } elsif ($line =~ m/^type (.+)$/) {
959 $tag{'type'} = $1;
960 } elsif ($line =~ m/^tag (.+)$/) {
961 $tag{'name'} = $1;
962 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
963 $tag{'author'} = $1;
964 $tag{'epoch'} = $2;
965 $tag{'tz'} = $3;
966 } elsif ($line =~ m/--BEGIN/) {
967 push @comment, $line;
968 last;
969 } elsif ($line eq "") {
970 last;
973 push @comment, <$fd>;
974 $tag{'comment'} = \@comment;
975 close $fd or return;
976 if (!defined $tag{'name'}) {
977 return
979 return %tag
982 sub parse_commit {
983 my $commit_id = shift;
984 my $commit_text = shift;
986 my @commit_lines;
987 my %co;
989 if (defined $commit_text) {
990 @commit_lines = @$commit_text;
991 } else {
992 $/ = "\0";
993 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
994 or return;
995 @commit_lines = split '\n', <$fd>;
996 close $fd or return;
997 $/ = "\n";
998 pop @commit_lines;
1000 my $header = shift @commit_lines;
1001 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1002 return;
1004 ($co{'id'}, my @parents) = split ' ', $header;
1005 $co{'parents'} = \@parents;
1006 $co{'parent'} = $parents[0];
1007 while (my $line = shift @commit_lines) {
1008 last if $line eq "\n";
1009 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1010 $co{'tree'} = $1;
1011 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1012 $co{'author'} = $1;
1013 $co{'author_epoch'} = $2;
1014 $co{'author_tz'} = $3;
1015 if ($co{'author'} =~ m/^([^<]+) </) {
1016 $co{'author_name'} = $1;
1017 } else {
1018 $co{'author_name'} = $co{'author'};
1020 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1021 $co{'committer'} = $1;
1022 $co{'committer_epoch'} = $2;
1023 $co{'committer_tz'} = $3;
1024 $co{'committer_name'} = $co{'committer'};
1025 $co{'committer_name'} =~ s/ <.*//;
1028 if (!defined $co{'tree'}) {
1029 return;
1032 foreach my $title (@commit_lines) {
1033 $title =~ s/^ //;
1034 if ($title ne "") {
1035 $co{'title'} = chop_str($title, 80, 5);
1036 # remove leading stuff of merges to make the interesting part visible
1037 if (length($title) > 50) {
1038 $title =~ s/^Automatic //;
1039 $title =~ s/^merge (of|with) /Merge ... /i;
1040 if (length($title) > 50) {
1041 $title =~ s/(http|rsync):\/\///;
1043 if (length($title) > 50) {
1044 $title =~ s/(master|www|rsync)\.//;
1046 if (length($title) > 50) {
1047 $title =~ s/kernel.org:?//;
1049 if (length($title) > 50) {
1050 $title =~ s/\/pub\/scm//;
1053 $co{'title_short'} = chop_str($title, 50, 5);
1054 last;
1057 # remove added spaces
1058 foreach my $line (@commit_lines) {
1059 $line =~ s/^ //;
1061 $co{'comment'} = \@commit_lines;
1063 my $age = time - $co{'committer_epoch'};
1064 $co{'age'} = $age;
1065 $co{'age_string'} = age_string($age);
1066 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1067 if ($age > 60*60*24*7*2) {
1068 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1069 $co{'age_string_age'} = $co{'age_string'};
1070 } else {
1071 $co{'age_string_date'} = $co{'age_string'};
1072 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1074 return %co;
1077 # parse ref from ref_file, given by ref_id, with given type
1078 sub parse_ref {
1079 my $ref_file = shift;
1080 my $ref_id = shift;
1081 my $type = shift || git_get_type($ref_id);
1082 my %ref_item;
1084 $ref_item{'type'} = $type;
1085 $ref_item{'id'} = $ref_id;
1086 $ref_item{'epoch'} = 0;
1087 $ref_item{'age'} = "unknown";
1088 if ($type eq "tag") {
1089 my %tag = parse_tag($ref_id);
1090 $ref_item{'comment'} = $tag{'comment'};
1091 if ($tag{'type'} eq "commit") {
1092 my %co = parse_commit($tag{'object'});
1093 $ref_item{'epoch'} = $co{'committer_epoch'};
1094 $ref_item{'age'} = $co{'age_string'};
1095 } elsif (defined($tag{'epoch'})) {
1096 my $age = time - $tag{'epoch'};
1097 $ref_item{'epoch'} = $tag{'epoch'};
1098 $ref_item{'age'} = age_string($age);
1100 $ref_item{'reftype'} = $tag{'type'};
1101 $ref_item{'name'} = $tag{'name'};
1102 $ref_item{'refid'} = $tag{'object'};
1103 } elsif ($type eq "commit"){
1104 my %co = parse_commit($ref_id);
1105 $ref_item{'reftype'} = "commit";
1106 $ref_item{'name'} = $ref_file;
1107 $ref_item{'title'} = $co{'title'};
1108 $ref_item{'refid'} = $ref_id;
1109 $ref_item{'epoch'} = $co{'committer_epoch'};
1110 $ref_item{'age'} = $co{'age_string'};
1111 } else {
1112 $ref_item{'reftype'} = $type;
1113 $ref_item{'name'} = $ref_file;
1114 $ref_item{'refid'} = $ref_id;
1117 return %ref_item;
1120 # parse line of git-diff-tree "raw" output
1121 sub parse_difftree_raw_line {
1122 my $line = shift;
1123 my %res;
1125 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1126 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1127 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1128 $res{'from_mode'} = $1;
1129 $res{'to_mode'} = $2;
1130 $res{'from_id'} = $3;
1131 $res{'to_id'} = $4;
1132 $res{'status'} = $5;
1133 $res{'similarity'} = $6;
1134 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1135 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1136 } else {
1137 $res{'file'} = unquote($7);
1140 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1141 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1142 $res{'commit'} = $1;
1145 return wantarray ? %res : \%res;
1148 # parse line of git-ls-tree output
1149 sub parse_ls_tree_line ($;%) {
1150 my $line = shift;
1151 my %opts = @_;
1152 my %res;
1154 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1155 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1157 $res{'mode'} = $1;
1158 $res{'type'} = $2;
1159 $res{'hash'} = $3;
1160 if ($opts{'-z'}) {
1161 $res{'name'} = $4;
1162 } else {
1163 $res{'name'} = unquote($4);
1166 return wantarray ? %res : \%res;
1169 ## ......................................................................
1170 ## parse to array of hashes functions
1172 sub git_get_refs_list {
1173 my $type = shift || "";
1174 my %refs;
1175 my @reflist;
1177 my @refs;
1178 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1179 or return;
1180 while (my $line = <$fd>) {
1181 chomp $line;
1182 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1183 if (defined $refs{$1}) {
1184 push @{$refs{$1}}, $2;
1185 } else {
1186 $refs{$1} = [ $2 ];
1189 if (! $4) { # unpeeled, direct reference
1190 push @refs, { hash => $1, name => $3 }; # without type
1191 } elsif ($3 eq $refs[-1]{'name'}) {
1192 # most likely a tag is followed by its peeled
1193 # (deref) one, and when that happens we know the
1194 # previous one was of type 'tag'.
1195 $refs[-1]{'type'} = "tag";
1199 close $fd;
1201 foreach my $ref (@refs) {
1202 my $ref_file = $ref->{'name'};
1203 my $ref_id = $ref->{'hash'};
1205 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1206 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1208 push @reflist, \%ref_item;
1210 # sort refs by age
1211 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1212 return (\@reflist, \%refs);
1215 ## ----------------------------------------------------------------------
1216 ## filesystem-related functions
1218 sub get_file_owner {
1219 my $path = shift;
1221 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1222 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1223 if (!defined $gcos) {
1224 return undef;
1226 my $owner = $gcos;
1227 $owner =~ s/[,;].*$//;
1228 return decode("utf8", $owner, Encode::FB_DEFAULT);
1231 ## ......................................................................
1232 ## mimetype related functions
1234 sub mimetype_guess_file {
1235 my $filename = shift;
1236 my $mimemap = shift;
1237 -r $mimemap or return undef;
1239 my %mimemap;
1240 open(MIME, $mimemap) or return undef;
1241 while (<MIME>) {
1242 next if m/^#/; # skip comments
1243 my ($mime, $exts) = split(/\t+/);
1244 if (defined $exts) {
1245 my @exts = split(/\s+/, $exts);
1246 foreach my $ext (@exts) {
1247 $mimemap{$ext} = $mime;
1251 close(MIME);
1253 $filename =~ /\.([^.]*)$/;
1254 return $mimemap{$1};
1257 sub mimetype_guess {
1258 my $filename = shift;
1259 my $mime;
1260 $filename =~ /\./ or return undef;
1262 if ($mimetypes_file) {
1263 my $file = $mimetypes_file;
1264 if ($file !~ m!^/!) { # if it is relative path
1265 # it is relative to project
1266 $file = "$projectroot/$project/$file";
1268 $mime = mimetype_guess_file($filename, $file);
1270 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1271 return $mime;
1274 sub blob_mimetype {
1275 my $fd = shift;
1276 my $filename = shift;
1278 if ($filename) {
1279 my $mime = mimetype_guess($filename);
1280 $mime and return $mime;
1283 # just in case
1284 return $default_blob_plain_mimetype unless $fd;
1286 if (-T $fd) {
1287 return 'text/plain' .
1288 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1289 } elsif (! $filename) {
1290 return 'application/octet-stream';
1291 } elsif ($filename =~ m/\.png$/i) {
1292 return 'image/png';
1293 } elsif ($filename =~ m/\.gif$/i) {
1294 return 'image/gif';
1295 } elsif ($filename =~ m/\.jpe?g$/i) {
1296 return 'image/jpeg';
1297 } else {
1298 return 'application/octet-stream';
1302 ## ======================================================================
1303 ## functions printing HTML: header, footer, error page
1305 sub git_header_html {
1306 my $status = shift || "200 OK";
1307 my $expires = shift;
1309 my $title = "$site_name git";
1310 if (defined $project) {
1311 $title .= " - $project";
1312 if (defined $action) {
1313 $title .= "/$action";
1314 if (defined $file_name) {
1315 $title .= " - " . esc_html($file_name);
1316 if ($action eq "tree" && $file_name !~ m|/$|) {
1317 $title .= "/";
1322 my $content_type;
1323 # require explicit support from the UA if we are to send the page as
1324 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1325 # we have to do this because MSIE sometimes globs '*/*', pretending to
1326 # support xhtml+xml but choking when it gets what it asked for.
1327 if (defined $cgi->http('HTTP_ACCEPT') &&
1328 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1329 $cgi->Accept('application/xhtml+xml') != 0) {
1330 $content_type = 'application/xhtml+xml';
1331 } else {
1332 $content_type = 'text/html';
1334 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1335 -status=> $status, -expires => $expires);
1336 print <<EOF;
1337 <?xml version="1.0" encoding="utf-8"?>
1338 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1339 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1340 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1341 <!-- git core binaries version $git_version -->
1342 <head>
1343 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1344 <meta name="generator" content="gitweb/$version git/$git_version"/>
1345 <meta name="robots" content="index, nofollow"/>
1346 <title>$title</title>
1347 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1349 if (defined $project) {
1350 printf('<link rel="alternate" title="%s log" '.
1351 'href="%s" type="application/rss+xml"/>'."\n",
1352 esc_param($project), href(action=>"rss"));
1353 } else {
1354 printf('<link rel="alternate" title="%s projects list" '.
1355 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1356 $site_name, href(project=>undef, action=>"project_index"));
1357 printf('<link rel="alternate" title="%s projects logs" '.
1358 'href="%s" type="text/x-opml"/>'."\n",
1359 $site_name, href(project=>undef, action=>"opml"));
1361 if (defined $favicon) {
1362 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1365 print "</head>\n" .
1366 "<body>\n" .
1367 "<div class=\"page_header\">\n" .
1368 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1369 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1370 "</a>\n";
1371 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1372 if (defined $project) {
1373 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1374 if (defined $action) {
1375 print " / $action";
1377 print "\n";
1378 if (!defined $searchtext) {
1379 $searchtext = "";
1381 my $search_hash;
1382 if (defined $hash_base) {
1383 $search_hash = $hash_base;
1384 } elsif (defined $hash) {
1385 $search_hash = $hash;
1386 } else {
1387 $search_hash = "HEAD";
1389 $cgi->param("a", "search");
1390 $cgi->param("h", $search_hash);
1391 print $cgi->startform(-method => "get", -action => $my_uri) .
1392 "<div class=\"search\">\n" .
1393 $cgi->hidden(-name => "p") . "\n" .
1394 $cgi->hidden(-name => "a") . "\n" .
1395 $cgi->hidden(-name => "h") . "\n" .
1396 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1397 "</div>" .
1398 $cgi->end_form() . "\n";
1400 print "</div>\n";
1403 sub git_footer_html {
1404 print "<div class=\"page_footer\">\n";
1405 if (defined $project) {
1406 my $descr = git_get_project_description($project);
1407 if (defined $descr) {
1408 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1410 print $cgi->a({-href => href(action=>"rss"),
1411 -class => "rss_logo"}, "RSS") . "\n";
1412 } else {
1413 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1414 -class => "rss_logo"}, "OPML") . " ";
1415 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1416 -class => "rss_logo"}, "TXT") . "\n";
1418 print "</div>\n" .
1419 "</body>\n" .
1420 "</html>";
1423 sub die_error {
1424 my $status = shift || "403 Forbidden";
1425 my $error = shift || "Malformed query, file missing or permission denied";
1427 git_header_html($status);
1428 print <<EOF;
1429 <div class="page_body">
1430 <br /><br />
1431 $status - $error
1432 <br />
1433 </div>
1435 git_footer_html();
1436 exit;
1439 ## ----------------------------------------------------------------------
1440 ## functions printing or outputting HTML: navigation
1442 sub git_print_page_nav {
1443 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1444 $extra = '' if !defined $extra; # pager or formats
1446 my @navs = qw(summary shortlog log commit commitdiff tree);
1447 if ($suppress) {
1448 @navs = grep { $_ ne $suppress } @navs;
1451 my %arg = map { $_ => {action=>$_} } @navs;
1452 if (defined $head) {
1453 for (qw(commit commitdiff)) {
1454 $arg{$_}{hash} = $head;
1456 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1457 for (qw(shortlog log)) {
1458 $arg{$_}{hash} = $head;
1462 $arg{tree}{hash} = $treehead if defined $treehead;
1463 $arg{tree}{hash_base} = $treebase if defined $treebase;
1465 print "<div class=\"page_nav\">\n" .
1466 (join " | ",
1467 map { $_ eq $current ?
1468 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1469 } @navs);
1470 print "<br/>\n$extra<br/>\n" .
1471 "</div>\n";
1474 sub format_paging_nav {
1475 my ($action, $hash, $head, $page, $nrevs) = @_;
1476 my $paging_nav;
1479 if ($hash ne $head || $page) {
1480 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1481 } else {
1482 $paging_nav .= "HEAD";
1485 if ($page > 0) {
1486 $paging_nav .= " &sdot; " .
1487 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1488 -accesskey => "p", -title => "Alt-p"}, "prev");
1489 } else {
1490 $paging_nav .= " &sdot; prev";
1493 if ($nrevs >= (100 * ($page+1)-1)) {
1494 $paging_nav .= " &sdot; " .
1495 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1496 -accesskey => "n", -title => "Alt-n"}, "next");
1497 } else {
1498 $paging_nav .= " &sdot; next";
1501 return $paging_nav;
1504 ## ......................................................................
1505 ## functions printing or outputting HTML: div
1507 sub git_print_header_div {
1508 my ($action, $title, $hash, $hash_base) = @_;
1509 my %args = ();
1511 $args{action} = $action;
1512 $args{hash} = $hash if $hash;
1513 $args{hash_base} = $hash_base if $hash_base;
1515 print "<div class=\"header\">\n" .
1516 $cgi->a({-href => href(%args), -class => "title"},
1517 $title ? $title : $action) .
1518 "\n</div>\n";
1521 #sub git_print_authorship (\%) {
1522 sub git_print_authorship {
1523 my $co = shift;
1525 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1526 print "<div class=\"author_date\">" .
1527 esc_html($co->{'author_name'}) .
1528 " [$ad{'rfc2822'}";
1529 if ($ad{'hour_local'} < 6) {
1530 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1531 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1532 } else {
1533 printf(" (%02d:%02d %s)",
1534 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1536 print "]</div>\n";
1539 sub git_print_page_path {
1540 my $name = shift;
1541 my $type = shift;
1542 my $hb = shift;
1544 if (!defined $name) {
1545 print "<div class=\"page_path\">/</div>\n";
1546 } else {
1547 my @dirname = split '/', $name;
1548 my $basename = pop @dirname;
1549 my $fullname = '';
1551 print "<div class=\"page_path\">";
1552 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1553 -title => 'tree root'}, "[$project]");
1554 print " / ";
1555 foreach my $dir (@dirname) {
1556 $fullname .= ($fullname ? '/' : '') . $dir;
1557 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1558 hash_base=>$hb),
1559 -title => $fullname}, esc_html($dir));
1560 print " / ";
1562 if (defined $type && $type eq 'blob') {
1563 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1564 hash_base=>$hb),
1565 -title => $name}, esc_html($basename));
1566 } elsif (defined $type && $type eq 'tree') {
1567 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1568 hash_base=>$hb),
1569 -title => $name}, esc_html($basename));
1570 } else {
1571 print esc_html($basename);
1573 print "<br/></div>\n";
1577 # sub git_print_log (\@;%) {
1578 sub git_print_log ($;%) {
1579 my $log = shift;
1580 my %opts = @_;
1582 if ($opts{'-remove_title'}) {
1583 # remove title, i.e. first line of log
1584 shift @$log;
1586 # remove leading empty lines
1587 while (defined $log->[0] && $log->[0] eq "") {
1588 shift @$log;
1591 # print log
1592 my $signoff = 0;
1593 my $empty = 0;
1594 foreach my $line (@$log) {
1595 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1596 $signoff = 1;
1597 $empty = 0;
1598 if (! $opts{'-remove_signoff'}) {
1599 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1600 next;
1601 } else {
1602 # remove signoff lines
1603 next;
1605 } else {
1606 $signoff = 0;
1609 # print only one empty line
1610 # do not print empty line after signoff
1611 if ($line eq "") {
1612 next if ($empty || $signoff);
1613 $empty = 1;
1614 } else {
1615 $empty = 0;
1618 print format_log_line_html($line) . "<br/>\n";
1621 if ($opts{'-final_empty_line'}) {
1622 # end with single empty line
1623 print "<br/>\n" unless $empty;
1627 sub git_print_simplified_log {
1628 my $log = shift;
1629 my $remove_title = shift;
1631 git_print_log($log,
1632 -final_empty_line=> 1,
1633 -remove_title => $remove_title);
1636 # print tree entry (row of git_tree), but without encompassing <tr> element
1637 sub git_print_tree_entry {
1638 my ($t, $basedir, $hash_base, $have_blame) = @_;
1640 my %base_key = ();
1641 $base_key{hash_base} = $hash_base if defined $hash_base;
1643 # The format of a table row is: mode list link. Where mode is
1644 # the mode of the entry, list is the name of the entry, an href,
1645 # and link is the action links of the entry.
1647 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1648 if ($t->{'type'} eq "blob") {
1649 print "<td class=\"list\">" .
1650 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1651 file_name=>"$basedir$t->{'name'}", %base_key),
1652 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1653 print "<td class=\"link\">";
1654 if ($have_blame) {
1655 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1656 file_name=>"$basedir$t->{'name'}", %base_key)},
1657 "blame");
1659 if (defined $hash_base) {
1660 if ($have_blame) {
1661 print " | ";
1663 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1664 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1665 "history");
1667 print " | " .
1668 $cgi->a({-href => href(action=>"blob_plain",
1669 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1670 "raw");
1671 print "</td>\n";
1673 } elsif ($t->{'type'} eq "tree") {
1674 print "<td class=\"list\">";
1675 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1676 file_name=>"$basedir$t->{'name'}", %base_key)},
1677 esc_html($t->{'name'}));
1678 print "</td>\n";
1679 print "<td class=\"link\">";
1680 if (defined $hash_base) {
1681 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1682 file_name=>"$basedir$t->{'name'}")},
1683 "history");
1685 print "</td>\n";
1689 ## ......................................................................
1690 ## functions printing large fragments of HTML
1692 sub git_difftree_body {
1693 my ($difftree, $hash, $parent) = @_;
1695 print "<div class=\"list_head\">\n";
1696 if ($#{$difftree} > 10) {
1697 print(($#{$difftree} + 1) . " files changed:\n");
1699 print "</div>\n";
1701 print "<table class=\"diff_tree\">\n";
1702 my $alternate = 0;
1703 my $patchno = 0;
1704 foreach my $line (@{$difftree}) {
1705 my %diff = parse_difftree_raw_line($line);
1707 if ($alternate) {
1708 print "<tr class=\"dark\">\n";
1709 } else {
1710 print "<tr class=\"light\">\n";
1712 $alternate ^= 1;
1714 my ($to_mode_oct, $to_mode_str, $to_file_type);
1715 my ($from_mode_oct, $from_mode_str, $from_file_type);
1716 if ($diff{'to_mode'} ne ('0' x 6)) {
1717 $to_mode_oct = oct $diff{'to_mode'};
1718 if (S_ISREG($to_mode_oct)) { # only for regular file
1719 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1721 $to_file_type = file_type($diff{'to_mode'});
1723 if ($diff{'from_mode'} ne ('0' x 6)) {
1724 $from_mode_oct = oct $diff{'from_mode'};
1725 if (S_ISREG($to_mode_oct)) { # only for regular file
1726 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1728 $from_file_type = file_type($diff{'from_mode'});
1731 if ($diff{'status'} eq "A") { # created
1732 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1733 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1734 $mode_chng .= "]</span>";
1735 print "<td>";
1736 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1737 hash_base=>$hash, file_name=>$diff{'file'}),
1738 -class => "list"}, esc_html($diff{'file'}));
1739 print "</td>\n";
1740 print "<td>$mode_chng</td>\n";
1741 print "<td class=\"link\">";
1742 if ($action eq 'commitdiff') {
1743 # link to patch
1744 $patchno++;
1745 print $cgi->a({-href => "#patch$patchno"}, "patch");
1747 print "</td>\n";
1749 } elsif ($diff{'status'} eq "D") { # deleted
1750 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1751 print "<td>";
1752 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1753 hash_base=>$parent, file_name=>$diff{'file'}),
1754 -class => "list"}, esc_html($diff{'file'}));
1755 print "</td>\n";
1756 print "<td>$mode_chng</td>\n";
1757 print "<td class=\"link\">";
1758 if ($action eq 'commitdiff') {
1759 # link to patch
1760 $patchno++;
1761 print $cgi->a({-href => "#patch$patchno"}, "patch");
1762 print " | ";
1764 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1765 file_name=>$diff{'file'})},
1766 "history");
1767 print "</td>\n";
1769 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1770 my $mode_chnge = "";
1771 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1772 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1773 if ($from_file_type != $to_file_type) {
1774 $mode_chnge .= " from $from_file_type to $to_file_type";
1776 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1777 if ($from_mode_str && $to_mode_str) {
1778 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1779 } elsif ($to_mode_str) {
1780 $mode_chnge .= " mode: $to_mode_str";
1783 $mode_chnge .= "]</span>\n";
1785 print "<td>";
1786 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1787 hash_base=>$hash, file_name=>$diff{'file'}),
1788 -class => "list"}, esc_html($diff{'file'}));
1789 print "</td>\n";
1790 print "<td>$mode_chnge</td>\n";
1791 print "<td class=\"link\">";
1792 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1793 if ($action eq 'commitdiff') {
1794 # link to patch
1795 $patchno++;
1796 print $cgi->a({-href => "#patch$patchno"}, "patch");
1797 } else {
1798 print $cgi->a({-href => href(action=>"blobdiff",
1799 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1800 hash_base=>$hash, hash_parent_base=>$parent,
1801 file_name=>$diff{'file'})},
1802 "diff");
1804 print " | ";
1806 print $cgi->a({-href => href(action=>"history",
1807 hash_base=>$hash, file_name=>$diff{'file'})},
1808 "history");
1809 print "</td>\n";
1811 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1812 my %status_name = ('R' => 'moved', 'C' => 'copied');
1813 my $nstatus = $status_name{$diff{'status'}};
1814 my $mode_chng = "";
1815 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1816 # mode also for directories, so we cannot use $to_mode_str
1817 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1819 print "<td>" .
1820 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1821 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1822 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1823 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1824 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1825 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1826 -class => "list"}, esc_html($diff{'from_file'})) .
1827 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1828 "<td class=\"link\">";
1829 if ($diff{'to_id'} ne $diff{'from_id'}) {
1830 if ($action eq 'commitdiff') {
1831 # link to patch
1832 $patchno++;
1833 print " | " .
1834 $cgi->a({-href => "#patch$patchno"}, "patch");
1835 } else {
1836 print " | " .
1837 $cgi->a({-href => href(action=>"blobdiff",
1838 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1839 hash_base=>$hash, hash_parent_base=>$parent,
1840 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1841 "diff");
1844 print "</td>\n";
1846 } # we should not encounter Unmerged (U) or Unknown (X) status
1847 print "</tr>\n";
1849 print "</table>\n";
1852 sub git_patchset_body {
1853 my ($fd, $difftree, $hash, $hash_parent) = @_;
1855 my $patch_idx = 0;
1856 my $in_header = 0;
1857 my $patch_found = 0;
1858 my $diffinfo;
1860 print "<div class=\"patchset\">\n";
1862 LINE:
1863 while (my $patch_line = <$fd>) {
1864 chomp $patch_line;
1866 if ($patch_line =~ m/^diff /) { # "git diff" header
1867 # beginning of patch (in patchset)
1868 if ($patch_found) {
1869 # close previous patch
1870 print "</div>\n"; # class="patch"
1871 } else {
1872 # first patch in patchset
1873 $patch_found = 1;
1875 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1877 if (ref($difftree->[$patch_idx]) eq "HASH") {
1878 $diffinfo = $difftree->[$patch_idx];
1879 } else {
1880 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1882 $patch_idx++;
1884 # for now, no extended header, hence we skip empty patches
1885 # companion to next LINE if $in_header;
1886 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1887 $in_header = 1;
1888 next LINE;
1891 if ($diffinfo->{'status'} eq "A") { # added
1892 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1893 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1894 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1895 $diffinfo->{'to_id'}) . "(new)" .
1896 "</div>\n"; # class="diff_info"
1898 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1899 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1900 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1901 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1902 $diffinfo->{'from_id'}) . "(deleted)" .
1903 "</div>\n"; # class="diff_info"
1905 } elsif ($diffinfo->{'status'} eq "R" || # renamed
1906 $diffinfo->{'status'} eq "C" || # copied
1907 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1908 print "<div class=\"diff_info\">" .
1909 file_type($diffinfo->{'from_mode'}) . ":" .
1910 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1911 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1912 $diffinfo->{'from_id'}) .
1913 " -> " .
1914 file_type($diffinfo->{'to_mode'}) . ":" .
1915 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1916 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1917 $diffinfo->{'to_id'});
1918 print "</div>\n"; # class="diff_info"
1920 } else { # modified, mode changed, ...
1921 print "<div class=\"diff_info\">" .
1922 file_type($diffinfo->{'from_mode'}) . ":" .
1923 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1924 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1925 $diffinfo->{'from_id'}) .
1926 " -> " .
1927 file_type($diffinfo->{'to_mode'}) . ":" .
1928 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1929 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1930 $diffinfo->{'to_id'});
1931 print "</div>\n"; # class="diff_info"
1934 #print "<div class=\"diff extended_header\">\n";
1935 $in_header = 1;
1936 next LINE;
1937 } # start of patch in patchset
1940 if ($in_header && $patch_line =~ m/^---/) {
1941 #print "</div>\n"; # class="diff extended_header"
1942 $in_header = 0;
1944 my $file = $diffinfo->{'from_file'};
1945 $file ||= $diffinfo->{'file'};
1946 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1947 hash=>$diffinfo->{'from_id'}, file_name=>$file),
1948 -class => "list"}, esc_html($file));
1949 $patch_line =~ s|a/.*$|a/$file|g;
1950 print "<div class=\"diff from_file\">$patch_line</div>\n";
1952 $patch_line = <$fd>;
1953 chomp $patch_line;
1955 #$patch_line =~ m/^+++/;
1956 $file = $diffinfo->{'to_file'};
1957 $file ||= $diffinfo->{'file'};
1958 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1959 hash=>$diffinfo->{'to_id'}, file_name=>$file),
1960 -class => "list"}, esc_html($file));
1961 $patch_line =~ s|b/.*|b/$file|g;
1962 print "<div class=\"diff to_file\">$patch_line</div>\n";
1964 next LINE;
1966 next LINE if $in_header;
1968 print format_diff_line($patch_line);
1970 print "</div>\n" if $patch_found; # class="patch"
1972 print "</div>\n"; # class="patchset"
1975 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1977 sub git_shortlog_body {
1978 # uses global variable $project
1979 my ($revlist, $from, $to, $refs, $extra) = @_;
1981 $from = 0 unless defined $from;
1982 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1984 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1985 my $alternate = 0;
1986 for (my $i = $from; $i <= $to; $i++) {
1987 my $commit = $revlist->[$i];
1988 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1989 my $ref = format_ref_marker($refs, $commit);
1990 my %co = parse_commit($commit);
1991 if ($alternate) {
1992 print "<tr class=\"dark\">\n";
1993 } else {
1994 print "<tr class=\"light\">\n";
1996 $alternate ^= 1;
1997 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1998 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1999 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2000 "<td>";
2001 print format_subject_html($co{'title'}, $co{'title_short'},
2002 href(action=>"commit", hash=>$commit), $ref);
2003 print "</td>\n" .
2004 "<td class=\"link\">" .
2005 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2006 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2007 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2008 print "</td>\n" .
2009 "</tr>\n";
2011 if (defined $extra) {
2012 print "<tr>\n" .
2013 "<td colspan=\"4\">$extra</td>\n" .
2014 "</tr>\n";
2016 print "</table>\n";
2019 sub git_history_body {
2020 # Warning: assumes constant type (blob or tree) during history
2021 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2023 $from = 0 unless defined $from;
2024 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2026 print "<table class=\"history\" cellspacing=\"0\">\n";
2027 my $alternate = 0;
2028 for (my $i = $from; $i <= $to; $i++) {
2029 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2030 next;
2033 my $commit = $1;
2034 my %co = parse_commit($commit);
2035 if (!%co) {
2036 next;
2039 my $ref = format_ref_marker($refs, $commit);
2041 if ($alternate) {
2042 print "<tr class=\"dark\">\n";
2043 } else {
2044 print "<tr class=\"light\">\n";
2046 $alternate ^= 1;
2047 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2048 # shortlog uses chop_str($co{'author_name'}, 10)
2049 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2050 "<td>";
2051 # originally git_history used chop_str($co{'title'}, 50)
2052 print format_subject_html($co{'title'}, $co{'title_short'},
2053 href(action=>"commit", hash=>$commit), $ref);
2054 print "</td>\n" .
2055 "<td class=\"link\">" .
2056 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2057 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2058 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
2060 if ($ftype eq 'blob') {
2061 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2062 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2063 if (defined $blob_current && defined $blob_parent &&
2064 $blob_current ne $blob_parent) {
2065 print " | " .
2066 $cgi->a({-href => href(action=>"blobdiff",
2067 hash=>$blob_current, hash_parent=>$blob_parent,
2068 hash_base=>$hash_base, hash_parent_base=>$commit,
2069 file_name=>$file_name)},
2070 "diff to current");
2073 print "</td>\n" .
2074 "</tr>\n";
2076 if (defined $extra) {
2077 print "<tr>\n" .
2078 "<td colspan=\"4\">$extra</td>\n" .
2079 "</tr>\n";
2081 print "</table>\n";
2084 sub git_tags_body {
2085 # uses global variable $project
2086 my ($taglist, $from, $to, $extra) = @_;
2087 $from = 0 unless defined $from;
2088 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2090 print "<table class=\"tags\" cellspacing=\"0\">\n";
2091 my $alternate = 0;
2092 for (my $i = $from; $i <= $to; $i++) {
2093 my $entry = $taglist->[$i];
2094 my %tag = %$entry;
2095 my $comment_lines = $tag{'comment'};
2096 my $comment = shift @$comment_lines;
2097 my $comment_short;
2098 if (defined $comment) {
2099 $comment_short = chop_str($comment, 30, 5);
2101 if ($alternate) {
2102 print "<tr class=\"dark\">\n";
2103 } else {
2104 print "<tr class=\"light\">\n";
2106 $alternate ^= 1;
2107 print "<td><i>$tag{'age'}</i></td>\n" .
2108 "<td>" .
2109 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2110 -class => "list name"}, esc_html($tag{'name'})) .
2111 "</td>\n" .
2112 "<td>";
2113 if (defined $comment) {
2114 print format_subject_html($comment, $comment_short,
2115 href(action=>"tag", hash=>$tag{'id'}));
2117 print "</td>\n" .
2118 "<td class=\"selflink\">";
2119 if ($tag{'type'} eq "tag") {
2120 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2121 } else {
2122 print "&nbsp;";
2124 print "</td>\n" .
2125 "<td class=\"link\">" . " | " .
2126 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2127 if ($tag{'reftype'} eq "commit") {
2128 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2129 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2130 } elsif ($tag{'reftype'} eq "blob") {
2131 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2133 print "</td>\n" .
2134 "</tr>";
2136 if (defined $extra) {
2137 print "<tr>\n" .
2138 "<td colspan=\"5\">$extra</td>\n" .
2139 "</tr>\n";
2141 print "</table>\n";
2144 sub git_heads_body {
2145 # uses global variable $project
2146 my ($headlist, $head, $from, $to, $extra) = @_;
2147 $from = 0 unless defined $from;
2148 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2150 print "<table class=\"heads\" cellspacing=\"0\">\n";
2151 my $alternate = 0;
2152 for (my $i = $from; $i <= $to; $i++) {
2153 my $entry = $headlist->[$i];
2154 my %tag = %$entry;
2155 my $curr = $tag{'id'} eq $head;
2156 if ($alternate) {
2157 print "<tr class=\"dark\">\n";
2158 } else {
2159 print "<tr class=\"light\">\n";
2161 $alternate ^= 1;
2162 print "<td><i>$tag{'age'}</i></td>\n" .
2163 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2164 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2165 -class => "list name"},esc_html($tag{'name'})) .
2166 "</td>\n" .
2167 "<td class=\"link\">" .
2168 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2169 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2170 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2171 "</td>\n" .
2172 "</tr>";
2174 if (defined $extra) {
2175 print "<tr>\n" .
2176 "<td colspan=\"3\">$extra</td>\n" .
2177 "</tr>\n";
2179 print "</table>\n";
2182 ## ======================================================================
2183 ## ======================================================================
2184 ## actions
2186 sub git_project_list {
2187 my $order = $cgi->param('o');
2188 if (defined $order && $order !~ m/project|descr|owner|age/) {
2189 die_error(undef, "Unknown order parameter");
2192 my @list = git_get_projects_list();
2193 my @projects;
2194 if (!@list) {
2195 die_error(undef, "No projects found");
2197 foreach my $pr (@list) {
2198 my $head = git_get_head_hash($pr->{'path'});
2199 if (!defined $head) {
2200 next;
2202 $git_dir = "$projectroot/$pr->{'path'}";
2203 my %co = parse_commit($head);
2204 if (!%co) {
2205 next;
2207 $pr->{'commit'} = \%co;
2208 if (!defined $pr->{'descr'}) {
2209 my $descr = git_get_project_description($pr->{'path'}) || "";
2210 $pr->{'descr'} = chop_str($descr, 25, 5);
2212 if (!defined $pr->{'owner'}) {
2213 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2215 push @projects, $pr;
2218 git_header_html();
2219 if (-f $home_text) {
2220 print "<div class=\"index_include\">\n";
2221 open (my $fd, $home_text);
2222 print <$fd>;
2223 close $fd;
2224 print "</div>\n";
2226 print "<table class=\"project_list\">\n" .
2227 "<tr>\n";
2228 $order ||= "project";
2229 if ($order eq "project") {
2230 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2231 print "<th>Project</th>\n";
2232 } else {
2233 print "<th>" .
2234 $cgi->a({-href => href(project=>undef, order=>'project'),
2235 -class => "header"}, "Project") .
2236 "</th>\n";
2238 if ($order eq "descr") {
2239 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2240 print "<th>Description</th>\n";
2241 } else {
2242 print "<th>" .
2243 $cgi->a({-href => href(project=>undef, order=>'descr'),
2244 -class => "header"}, "Description") .
2245 "</th>\n";
2247 if ($order eq "owner") {
2248 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2249 print "<th>Owner</th>\n";
2250 } else {
2251 print "<th>" .
2252 $cgi->a({-href => href(project=>undef, order=>'owner'),
2253 -class => "header"}, "Owner") .
2254 "</th>\n";
2256 if ($order eq "age") {
2257 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2258 print "<th>Last Change</th>\n";
2259 } else {
2260 print "<th>" .
2261 $cgi->a({-href => href(project=>undef, order=>'age'),
2262 -class => "header"}, "Last Change") .
2263 "</th>\n";
2265 print "<th></th>\n" .
2266 "</tr>\n";
2267 my $alternate = 0;
2268 foreach my $pr (@projects) {
2269 if ($alternate) {
2270 print "<tr class=\"dark\">\n";
2271 } else {
2272 print "<tr class=\"light\">\n";
2274 $alternate ^= 1;
2275 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2276 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2277 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2278 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2279 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2280 $pr->{'commit'}{'age_string'} . "</td>\n" .
2281 "<td class=\"link\">" .
2282 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2283 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2284 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2285 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2286 "</td>\n" .
2287 "</tr>\n";
2289 print "</table>\n";
2290 git_footer_html();
2293 sub git_project_index {
2294 my @projects = git_get_projects_list();
2296 print $cgi->header(
2297 -type => 'text/plain',
2298 -charset => 'utf-8',
2299 -content_disposition => 'inline; filename="index.aux"');
2301 foreach my $pr (@projects) {
2302 if (!exists $pr->{'owner'}) {
2303 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2306 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2307 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2308 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2309 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2310 $path =~ s/ /\+/g;
2311 $owner =~ s/ /\+/g;
2313 print "$path $owner\n";
2317 sub git_summary {
2318 my $descr = git_get_project_description($project) || "none";
2319 my $head = git_get_head_hash($project);
2320 my %co = parse_commit($head);
2321 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2323 my $owner = git_get_project_owner($project);
2325 my ($reflist, $refs) = git_get_refs_list();
2327 my @taglist;
2328 my @headlist;
2329 foreach my $ref (@$reflist) {
2330 if ($ref->{'name'} =~ s!^heads/!!) {
2331 push @headlist, $ref;
2332 } else {
2333 $ref->{'name'} =~ s!^tags/!!;
2334 push @taglist, $ref;
2338 git_header_html();
2339 git_print_page_nav('summary','', $head);
2341 print "<div class=\"title\">&nbsp;</div>\n";
2342 print "<table cellspacing=\"0\">\n" .
2343 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2344 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2345 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2346 # use per project git URL list in $projectroot/$project/cloneurl
2347 # or make project git URL from git base URL and project name
2348 my $url_tag = "URL";
2349 my @url_list = git_get_project_url_list($project);
2350 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2351 foreach my $git_url (@url_list) {
2352 next unless $git_url;
2353 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2354 $url_tag = "";
2356 print "</table>\n";
2358 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2359 git_get_head_hash($project)
2360 or die_error(undef, "Open git-rev-list failed");
2361 my @revlist = map { chomp; $_ } <$fd>;
2362 close $fd;
2363 git_print_header_div('shortlog');
2364 git_shortlog_body(\@revlist, 0, 15, $refs,
2365 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2367 if (@taglist) {
2368 git_print_header_div('tags');
2369 git_tags_body(\@taglist, 0, 15,
2370 $cgi->a({-href => href(action=>"tags")}, "..."));
2373 if (@headlist) {
2374 git_print_header_div('heads');
2375 git_heads_body(\@headlist, $head, 0, 15,
2376 $cgi->a({-href => href(action=>"heads")}, "..."));
2379 git_footer_html();
2382 sub git_tag {
2383 my $head = git_get_head_hash($project);
2384 git_header_html();
2385 git_print_page_nav('','', $head,undef,$head);
2386 my %tag = parse_tag($hash);
2387 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2388 print "<div class=\"title_text\">\n" .
2389 "<table cellspacing=\"0\">\n" .
2390 "<tr>\n" .
2391 "<td>object</td>\n" .
2392 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2393 $tag{'object'}) . "</td>\n" .
2394 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2395 $tag{'type'}) . "</td>\n" .
2396 "</tr>\n";
2397 if (defined($tag{'author'})) {
2398 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2399 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2400 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2401 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2402 "</td></tr>\n";
2404 print "</table>\n\n" .
2405 "</div>\n";
2406 print "<div class=\"page_body\">";
2407 my $comment = $tag{'comment'};
2408 foreach my $line (@$comment) {
2409 print esc_html($line) . "<br/>\n";
2411 print "</div>\n";
2412 git_footer_html();
2415 sub git_blame2 {
2416 my $fd;
2417 my $ftype;
2419 my ($have_blame) = gitweb_check_feature('blame');
2420 if (!$have_blame) {
2421 die_error('403 Permission denied', "Permission denied");
2423 die_error('404 Not Found', "File name not defined") if (!$file_name);
2424 $hash_base ||= git_get_head_hash($project);
2425 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2426 my %co = parse_commit($hash_base)
2427 or die_error(undef, "Reading commit failed");
2428 if (!defined $hash) {
2429 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2430 or die_error(undef, "Error looking up file");
2432 $ftype = git_get_type($hash);
2433 if ($ftype !~ "blob") {
2434 die_error("400 Bad Request", "Object is not a blob");
2436 open ($fd, "-|", git_cmd(), "blame", '-l', '--', $file_name, $hash_base)
2437 or die_error(undef, "Open git-blame failed");
2438 git_header_html();
2439 my $formats_nav =
2440 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2441 "blob") .
2442 " | " .
2443 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2444 "history") .
2445 " | " .
2446 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2447 "HEAD");
2448 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2449 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2450 git_print_page_path($file_name, $ftype, $hash_base);
2451 my @rev_color = (qw(light2 dark2));
2452 my $num_colors = scalar(@rev_color);
2453 my $current_color = 0;
2454 my $last_rev;
2455 print <<HTML;
2456 <div class="page_body">
2457 <table class="blame">
2458 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2459 HTML
2460 while (<$fd>) {
2461 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2462 my $full_rev = $1;
2463 my $rev = substr($full_rev, 0, 8);
2464 my $lineno = $2;
2465 my $data = $3;
2467 if (!defined $last_rev) {
2468 $last_rev = $full_rev;
2469 } elsif ($last_rev ne $full_rev) {
2470 $last_rev = $full_rev;
2471 $current_color = ++$current_color % $num_colors;
2473 print "<tr class=\"$rev_color[$current_color]\">\n";
2474 print "<td class=\"sha1\">" .
2475 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2476 esc_html($rev)) . "</td>\n";
2477 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2478 esc_html($lineno) . "</a></td>\n";
2479 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2480 print "</tr>\n";
2482 print "</table>\n";
2483 print "</div>";
2484 close $fd
2485 or print "Reading blob failed\n";
2486 git_footer_html();
2489 sub git_blame {
2490 my $fd;
2492 my ($have_blame) = gitweb_check_feature('blame');
2493 if (!$have_blame) {
2494 die_error('403 Permission denied', "Permission denied");
2496 die_error('404 Not Found', "File name not defined") if (!$file_name);
2497 $hash_base ||= git_get_head_hash($project);
2498 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2499 my %co = parse_commit($hash_base)
2500 or die_error(undef, "Reading commit failed");
2501 if (!defined $hash) {
2502 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2503 or die_error(undef, "Error lookup file");
2505 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2506 or die_error(undef, "Open git-annotate failed");
2507 git_header_html();
2508 my $formats_nav =
2509 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2510 "blob") .
2511 " | " .
2512 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2513 "history") .
2514 " | " .
2515 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2516 "HEAD");
2517 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2518 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2519 git_print_page_path($file_name, 'blob', $hash_base);
2520 print "<div class=\"page_body\">\n";
2521 print <<HTML;
2522 <table class="blame">
2523 <tr>
2524 <th>Commit</th>
2525 <th>Age</th>
2526 <th>Author</th>
2527 <th>Line</th>
2528 <th>Data</th>
2529 </tr>
2530 HTML
2531 my @line_class = (qw(light dark));
2532 my $line_class_len = scalar (@line_class);
2533 my $line_class_num = $#line_class;
2534 while (my $line = <$fd>) {
2535 my $long_rev;
2536 my $short_rev;
2537 my $author;
2538 my $time;
2539 my $lineno;
2540 my $data;
2541 my $age;
2542 my $age_str;
2543 my $age_class;
2545 chomp $line;
2546 $line_class_num = ($line_class_num + 1) % $line_class_len;
2548 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2549 $long_rev = $1;
2550 $author = $2;
2551 $time = $3;
2552 $lineno = $4;
2553 $data = $5;
2554 } else {
2555 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2556 next;
2558 $short_rev = substr ($long_rev, 0, 8);
2559 $age = time () - $time;
2560 $age_str = age_string ($age);
2561 $age_str =~ s/ /&nbsp;/g;
2562 $age_class = age_class($age);
2563 $author = esc_html ($author);
2564 $author =~ s/ /&nbsp;/g;
2566 $data = untabify($data);
2567 $data = esc_html ($data);
2569 print <<HTML;
2570 <tr class="$line_class[$line_class_num]">
2571 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2572 <td class="$age_class">$age_str</td>
2573 <td>$author</td>
2574 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2575 <td class="pre">$data</td>
2576 </tr>
2577 HTML
2578 } # while (my $line = <$fd>)
2579 print "</table>\n\n";
2580 close $fd
2581 or print "Reading blob failed.\n";
2582 print "</div>";
2583 git_footer_html();
2586 sub git_tags {
2587 my $head = git_get_head_hash($project);
2588 git_header_html();
2589 git_print_page_nav('','', $head,undef,$head);
2590 git_print_header_div('summary', $project);
2592 my ($taglist) = git_get_refs_list("tags");
2593 if (@$taglist) {
2594 git_tags_body($taglist);
2596 git_footer_html();
2599 sub git_heads {
2600 my $head = git_get_head_hash($project);
2601 git_header_html();
2602 git_print_page_nav('','', $head,undef,$head);
2603 git_print_header_div('summary', $project);
2605 my ($headlist) = git_get_refs_list("heads");
2606 if (@$headlist) {
2607 git_heads_body($headlist, $head);
2609 git_footer_html();
2612 sub git_blob_plain {
2613 my $expires;
2615 if (!defined $hash) {
2616 if (defined $file_name) {
2617 my $base = $hash_base || git_get_head_hash($project);
2618 $hash = git_get_hash_by_path($base, $file_name, "blob")
2619 or die_error(undef, "Error lookup file");
2620 } else {
2621 die_error(undef, "No file name defined");
2623 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2624 # blobs defined by non-textual hash id's can be cached
2625 $expires = "+1d";
2628 my $type = shift;
2629 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2630 or die_error(undef, "Couldn't cat $file_name, $hash");
2632 $type ||= blob_mimetype($fd, $file_name);
2634 # save as filename, even when no $file_name is given
2635 my $save_as = "$hash";
2636 if (defined $file_name) {
2637 $save_as = $file_name;
2638 } elsif ($type =~ m/^text\//) {
2639 $save_as .= '.txt';
2642 print $cgi->header(
2643 -type => "$type",
2644 -expires=>$expires,
2645 -content_disposition => 'inline; filename="' . quotemeta($save_as) . '"');
2646 undef $/;
2647 binmode STDOUT, ':raw';
2648 print <$fd>;
2649 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2650 $/ = "\n";
2651 close $fd;
2654 sub git_blob {
2655 my $expires;
2657 if (!defined $hash) {
2658 if (defined $file_name) {
2659 my $base = $hash_base || git_get_head_hash($project);
2660 $hash = git_get_hash_by_path($base, $file_name, "blob")
2661 or die_error(undef, "Error lookup file");
2662 } else {
2663 die_error(undef, "No file name defined");
2665 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2666 # blobs defined by non-textual hash id's can be cached
2667 $expires = "+1d";
2670 my ($have_blame) = gitweb_check_feature('blame');
2671 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2672 or die_error(undef, "Couldn't cat $file_name, $hash");
2673 my $mimetype = blob_mimetype($fd, $file_name);
2674 if ($mimetype !~ m/^text\//) {
2675 close $fd;
2676 return git_blob_plain($mimetype);
2678 git_header_html(undef, $expires);
2679 my $formats_nav = '';
2680 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2681 if (defined $file_name) {
2682 if ($have_blame) {
2683 $formats_nav .=
2684 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2685 hash=>$hash, file_name=>$file_name)},
2686 "blame") .
2687 " | ";
2689 $formats_nav .=
2690 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2691 hash=>$hash, file_name=>$file_name)},
2692 "history") .
2693 " | " .
2694 $cgi->a({-href => href(action=>"blob_plain",
2695 hash=>$hash, file_name=>$file_name)},
2696 "raw") .
2697 " | " .
2698 $cgi->a({-href => href(action=>"blob",
2699 hash_base=>"HEAD", file_name=>$file_name)},
2700 "HEAD");
2701 } else {
2702 $formats_nav .=
2703 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2705 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2706 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2707 } else {
2708 print "<div class=\"page_nav\">\n" .
2709 "<br/><br/></div>\n" .
2710 "<div class=\"title\">$hash</div>\n";
2712 git_print_page_path($file_name, "blob", $hash_base);
2713 print "<div class=\"page_body\">\n";
2714 my $nr;
2715 while (my $line = <$fd>) {
2716 chomp $line;
2717 $nr++;
2718 $line = untabify($line);
2719 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2720 $nr, $nr, $nr, esc_html($line);
2722 close $fd
2723 or print "Reading blob failed.\n";
2724 print "</div>";
2725 git_footer_html();
2728 sub git_tree {
2729 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2730 my $have_snapshot = (defined $ctype && defined $suffix);
2732 if (!defined $hash) {
2733 $hash = git_get_head_hash($project);
2734 if (defined $file_name) {
2735 my $base = $hash_base || $hash;
2736 $hash = git_get_hash_by_path($base, $file_name, "tree");
2738 if (!defined $hash_base) {
2739 $hash_base = $hash;
2742 $/ = "\0";
2743 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2744 or die_error(undef, "Open git-ls-tree failed");
2745 my @entries = map { chomp; $_ } <$fd>;
2746 close $fd or die_error(undef, "Reading tree failed");
2747 $/ = "\n";
2749 my $refs = git_get_references();
2750 my $ref = format_ref_marker($refs, $hash_base);
2751 git_header_html();
2752 my $base = "";
2753 my ($have_blame) = gitweb_check_feature('blame');
2754 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2755 my @views_nav = ();
2756 if (defined $file_name) {
2757 push @views_nav,
2758 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2759 hash=>$hash, file_name=>$file_name)},
2760 "history"),
2761 $cgi->a({-href => href(action=>"tree",
2762 hash_base=>"HEAD", file_name=>$file_name)},
2763 "HEAD"),
2765 if ($have_snapshot) {
2766 # FIXME: Should be available when we have no hash base as well.
2767 push @views_nav,
2768 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2769 "snapshot");
2771 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2772 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2773 } else {
2774 undef $hash_base;
2775 print "<div class=\"page_nav\">\n";
2776 print "<br/><br/></div>\n";
2777 print "<div class=\"title\">$hash</div>\n";
2779 if (defined $file_name) {
2780 $base = esc_html("$file_name/");
2782 git_print_page_path($file_name, 'tree', $hash_base);
2783 print "<div class=\"page_body\">\n";
2784 print "<table cellspacing=\"0\">\n";
2785 my $alternate = 0;
2786 foreach my $line (@entries) {
2787 my %t = parse_ls_tree_line($line, -z => 1);
2789 if ($alternate) {
2790 print "<tr class=\"dark\">\n";
2791 } else {
2792 print "<tr class=\"light\">\n";
2794 $alternate ^= 1;
2796 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2798 print "</tr>\n";
2800 print "</table>\n" .
2801 "</div>";
2802 git_footer_html();
2805 sub git_snapshot {
2807 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2808 my $have_snapshot = (defined $ctype && defined $suffix);
2809 if (!$have_snapshot) {
2810 die_error('403 Permission denied', "Permission denied");
2813 if (!defined $hash) {
2814 $hash = git_get_head_hash($project);
2817 my $filename = basename($project) . "-$hash.tar.$suffix";
2819 print $cgi->header(
2820 -type => 'application/x-tar',
2821 -content_encoding => $ctype,
2822 -content_disposition => 'inline; filename="' . quotemeta($filename) . '"',
2823 -status => '200 OK');
2825 my $git_command = git_cmd_str();
2826 open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2827 die_error(undef, "Execute git-tar-tree failed.");
2828 binmode STDOUT, ':raw';
2829 print <$fd>;
2830 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2831 close $fd;
2835 sub git_log {
2836 my $head = git_get_head_hash($project);
2837 if (!defined $hash) {
2838 $hash = $head;
2840 if (!defined $page) {
2841 $page = 0;
2843 my $refs = git_get_references();
2845 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2846 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2847 or die_error(undef, "Open git-rev-list failed");
2848 my @revlist = map { chomp; $_ } <$fd>;
2849 close $fd;
2851 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2853 git_header_html();
2854 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2856 if (!@revlist) {
2857 my %co = parse_commit($hash);
2859 git_print_header_div('summary', $project);
2860 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2862 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2863 my $commit = $revlist[$i];
2864 my $ref = format_ref_marker($refs, $commit);
2865 my %co = parse_commit($commit);
2866 next if !%co;
2867 my %ad = parse_date($co{'author_epoch'});
2868 git_print_header_div('commit',
2869 "<span class=\"age\">$co{'age_string'}</span>" .
2870 esc_html($co{'title'}) . $ref,
2871 $commit);
2872 print "<div class=\"title_text\">\n" .
2873 "<div class=\"log_link\">\n" .
2874 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2875 " | " .
2876 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2877 " | " .
2878 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
2879 "<br/>\n" .
2880 "</div>\n" .
2881 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2882 "</div>\n";
2884 print "<div class=\"log_body\">\n";
2885 git_print_simplified_log($co{'comment'});
2886 print "</div>\n";
2888 git_footer_html();
2891 sub git_commit {
2892 my %co = parse_commit($hash);
2893 if (!%co) {
2894 die_error(undef, "Unknown commit object");
2896 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2897 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2899 my $parent = $co{'parent'};
2900 if (!defined $parent) {
2901 $parent = "--root";
2903 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2904 or die_error(undef, "Open git-diff-tree failed");
2905 my @difftree = map { chomp; $_ } <$fd>;
2906 close $fd or die_error(undef, "Reading git-diff-tree failed");
2908 # non-textual hash id's can be cached
2909 my $expires;
2910 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2911 $expires = "+1d";
2913 my $refs = git_get_references();
2914 my $ref = format_ref_marker($refs, $co{'id'});
2916 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2917 my $have_snapshot = (defined $ctype && defined $suffix);
2919 my @views_nav = ();
2920 if (defined $file_name && defined $co{'parent'}) {
2921 my $parent = $co{'parent'};
2922 push @views_nav,
2923 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2924 "blame");
2926 if (defined $co{'parent'}) {
2927 push @views_nav,
2928 $cgi->a({-href => href(action=>"shortlog", hash=>$hash)}, "shortlog"),
2929 $cgi->a({-href => href(action=>"log", hash=>$hash)}, "log");
2931 git_header_html(undef, $expires);
2932 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2933 $hash, $co{'tree'}, $hash,
2934 join (' | ', @views_nav));
2936 if (defined $co{'parent'}) {
2937 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2938 } else {
2939 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2941 print "<div class=\"title_text\">\n" .
2942 "<table cellspacing=\"0\">\n";
2943 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2944 "<tr>" .
2945 "<td></td><td> $ad{'rfc2822'}";
2946 if ($ad{'hour_local'} < 6) {
2947 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2948 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2949 } else {
2950 printf(" (%02d:%02d %s)",
2951 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2953 print "</td>" .
2954 "</tr>\n";
2955 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2956 print "<tr><td></td><td> $cd{'rfc2822'}" .
2957 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2958 "</td></tr>\n";
2959 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2960 print "<tr>" .
2961 "<td>tree</td>" .
2962 "<td class=\"sha1\">" .
2963 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2964 class => "list"}, $co{'tree'}) .
2965 "</td>" .
2966 "<td class=\"link\">" .
2967 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2968 "tree");
2969 if ($have_snapshot) {
2970 print " | " .
2971 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2973 print "</td>" .
2974 "</tr>\n";
2975 my $parents = $co{'parents'};
2976 foreach my $par (@$parents) {
2977 print "<tr>" .
2978 "<td>parent</td>" .
2979 "<td class=\"sha1\">" .
2980 $cgi->a({-href => href(action=>"commit", hash=>$par),
2981 class => "list"}, $par) .
2982 "</td>" .
2983 "<td class=\"link\">" .
2984 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2985 " | " .
2986 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
2987 "</td>" .
2988 "</tr>\n";
2990 print "</table>".
2991 "</div>\n";
2993 print "<div class=\"page_body\">\n";
2994 git_print_log($co{'comment'});
2995 print "</div>\n";
2997 git_difftree_body(\@difftree, $hash, $parent);
2999 git_footer_html();
3002 sub git_blobdiff {
3003 my $format = shift || 'html';
3005 my $fd;
3006 my @difftree;
3007 my %diffinfo;
3008 my $expires;
3010 # preparing $fd and %diffinfo for git_patchset_body
3011 # new style URI
3012 if (defined $hash_base && defined $hash_parent_base) {
3013 if (defined $file_name) {
3014 # read raw output
3015 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3016 "--", $file_name
3017 or die_error(undef, "Open git-diff-tree failed");
3018 @difftree = map { chomp; $_ } <$fd>;
3019 close $fd
3020 or die_error(undef, "Reading git-diff-tree failed");
3021 @difftree
3022 or die_error('404 Not Found', "Blob diff not found");
3024 } elsif (defined $hash &&
3025 $hash =~ /[0-9a-fA-F]{40}/) {
3026 # try to find filename from $hash
3028 # read filtered raw output
3029 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3030 or die_error(undef, "Open git-diff-tree failed");
3031 @difftree =
3032 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3033 # $hash == to_id
3034 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3035 map { chomp; $_ } <$fd>;
3036 close $fd
3037 or die_error(undef, "Reading git-diff-tree failed");
3038 @difftree
3039 or die_error('404 Not Found', "Blob diff not found");
3041 } else {
3042 die_error('404 Not Found', "Missing one of the blob diff parameters");
3045 if (@difftree > 1) {
3046 die_error('404 Not Found', "Ambiguous blob diff specification");
3049 %diffinfo = parse_difftree_raw_line($difftree[0]);
3050 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3051 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3053 $hash_parent ||= $diffinfo{'from_id'};
3054 $hash ||= $diffinfo{'to_id'};
3056 # non-textual hash id's can be cached
3057 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3058 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3059 $expires = '+1d';
3062 # open patch output
3063 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3064 '-p', $hash_parent_base, $hash_base,
3065 "--", $file_name
3066 or die_error(undef, "Open git-diff-tree failed");
3069 # old/legacy style URI
3070 if (!%diffinfo && # if new style URI failed
3071 defined $hash && defined $hash_parent) {
3072 # fake git-diff-tree raw output
3073 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3074 $diffinfo{'from_id'} = $hash_parent;
3075 $diffinfo{'to_id'} = $hash;
3076 if (defined $file_name) {
3077 if (defined $file_parent) {
3078 $diffinfo{'status'} = '2';
3079 $diffinfo{'from_file'} = $file_parent;
3080 $diffinfo{'to_file'} = $file_name;
3081 } else { # assume not renamed
3082 $diffinfo{'status'} = '1';
3083 $diffinfo{'from_file'} = $file_name;
3084 $diffinfo{'to_file'} = $file_name;
3086 } else { # no filename given
3087 $diffinfo{'status'} = '2';
3088 $diffinfo{'from_file'} = $hash_parent;
3089 $diffinfo{'to_file'} = $hash;
3092 # non-textual hash id's can be cached
3093 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3094 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3095 $expires = '+1d';
3098 # open patch output
3099 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3100 or die_error(undef, "Open git-diff failed");
3101 } else {
3102 die_error('404 Not Found', "Missing one of the blob diff parameters")
3103 unless %diffinfo;
3106 # header
3107 if ($format eq 'html') {
3108 my $formats_nav =
3109 $cgi->a({-href => href(action=>"blobdiff_plain",
3110 hash=>$hash, hash_parent=>$hash_parent,
3111 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3112 file_name=>$file_name, file_parent=>$file_parent)},
3113 "raw");
3114 git_header_html(undef, $expires);
3115 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3116 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3117 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3118 } else {
3119 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3120 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3122 if (defined $file_name) {
3123 git_print_page_path($file_name, "blob", $hash_base);
3124 } else {
3125 print "<div class=\"page_path\"></div>\n";
3128 } elsif ($format eq 'plain') {
3129 print $cgi->header(
3130 -type => 'text/plain',
3131 -charset => 'utf-8',
3132 -expires => $expires,
3133 -content_disposition => 'inline; filename="' . quotemeta($file_name) . '.patch"');
3135 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3137 } else {
3138 die_error(undef, "Unknown blobdiff format");
3141 # patch
3142 if ($format eq 'html') {
3143 print "<div class=\"page_body\">\n";
3145 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3146 close $fd;
3148 print "</div>\n"; # class="page_body"
3149 git_footer_html();
3151 } else {
3152 while (my $line = <$fd>) {
3153 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3154 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3156 print $line;
3158 last if $line =~ m!^\+\+\+!;
3160 local $/ = undef;
3161 print <$fd>;
3162 close $fd;
3166 sub git_blobdiff_plain {
3167 git_blobdiff('plain');
3170 sub git_commitdiff {
3171 my $format = shift || 'html';
3172 my %co = parse_commit($hash);
3173 if (!%co) {
3174 die_error(undef, "Unknown commit object");
3176 if (!defined $hash_parent) {
3177 $hash_parent = $co{'parent'} || '--root';
3180 # read commitdiff
3181 my $fd;
3182 my @difftree;
3183 if ($format eq 'html') {
3184 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3185 "--patch-with-raw", "--full-index", $hash_parent, $hash
3186 or die_error(undef, "Open git-diff-tree failed");
3188 while (chomp(my $line = <$fd>)) {
3189 # empty line ends raw part of diff-tree output
3190 last unless $line;
3191 push @difftree, $line;
3194 } elsif ($format eq 'plain') {
3195 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3196 '-p', $hash_parent, $hash
3197 or die_error(undef, "Open git-diff-tree failed");
3199 } else {
3200 die_error(undef, "Unknown commitdiff format");
3203 # non-textual hash id's can be cached
3204 my $expires;
3205 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3206 $expires = "+1d";
3209 # write commit message
3210 if ($format eq 'html') {
3211 my $refs = git_get_references();
3212 my $ref = format_ref_marker($refs, $co{'id'});
3213 my $formats_nav =
3214 $cgi->a({-href => href(action=>"commitdiff_plain",
3215 hash=>$hash, hash_parent=>$hash_parent)},
3216 "raw");
3218 git_header_html(undef, $expires);
3219 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3220 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3221 git_print_authorship(\%co);
3222 print "<div class=\"page_body\">\n";
3223 print "<div class=\"log\">\n";
3224 git_print_simplified_log($co{'comment'}, 1); # skip title
3225 print "</div>\n"; # class="log"
3227 } elsif ($format eq 'plain') {
3228 my $refs = git_get_references("tags");
3229 my $tagname = git_get_rev_name_tags($hash);
3230 my $filename = basename($project) . "-$hash.patch";
3232 print $cgi->header(
3233 -type => 'text/plain',
3234 -charset => 'utf-8',
3235 -expires => $expires,
3236 -content_disposition => 'inline; filename="' . quotemeta($filename) . '"');
3237 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3238 print <<TEXT;
3239 From: $co{'author'}
3240 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3241 Subject: $co{'title'}
3242 TEXT
3243 print "X-Git-Tag: $tagname\n" if $tagname;
3244 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3246 foreach my $line (@{$co{'comment'}}) {
3247 print "$line\n";
3249 print "---\n\n";
3252 # write patch
3253 if ($format eq 'html') {
3254 git_difftree_body(\@difftree, $hash, $hash_parent);
3255 print "<br/>\n";
3257 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3258 close $fd;
3259 print "</div>\n"; # class="page_body"
3260 git_footer_html();
3262 } elsif ($format eq 'plain') {
3263 local $/ = undef;
3264 print <$fd>;
3265 close $fd
3266 or print "Reading git-diff-tree failed\n";
3270 sub git_commitdiff_plain {
3271 git_commitdiff('plain');
3274 sub git_history {
3275 if (!defined $hash_base) {
3276 $hash_base = git_get_head_hash($project);
3278 if (!defined $page) {
3279 $page = 0;
3281 my $ftype;
3282 my %co = parse_commit($hash_base);
3283 if (!%co) {
3284 die_error(undef, "Unknown commit object");
3287 my $refs = git_get_references();
3288 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3290 if (!defined $hash && defined $file_name) {
3291 $hash = git_get_hash_by_path($hash_base, $file_name);
3293 if (defined $hash) {
3294 $ftype = git_get_type($hash);
3297 open my $fd, "-|",
3298 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3299 or die_error(undef, "Open git-rev-list-failed");
3300 my @revlist = map { chomp; $_ } <$fd>;
3301 close $fd
3302 or die_error(undef, "Reading git-rev-list failed");
3304 my $paging_nav = '';
3305 if ($page > 0) {
3306 $paging_nav .=
3307 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3308 file_name=>$file_name)},
3309 "first");
3310 $paging_nav .= " &sdot; " .
3311 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3312 file_name=>$file_name, page=>$page-1),
3313 -accesskey => "p", -title => "Alt-p"}, "prev");
3314 } else {
3315 $paging_nav .= "first";
3316 $paging_nav .= " &sdot; prev";
3318 if ($#revlist >= (100 * ($page+1)-1)) {
3319 $paging_nav .= " &sdot; " .
3320 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3321 file_name=>$file_name, page=>$page+1),
3322 -accesskey => "n", -title => "Alt-n"}, "next");
3323 } else {
3324 $paging_nav .= " &sdot; next";
3326 my $next_link = '';
3327 if ($#revlist >= (100 * ($page+1)-1)) {
3328 $next_link =
3329 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3330 file_name=>$file_name, page=>$page+1),
3331 -title => "Alt-n"}, "next");
3334 git_header_html();
3335 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3336 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3337 git_print_page_path($file_name, $ftype, $hash_base);
3339 git_history_body(\@revlist, ($page * 100), $#revlist,
3340 $refs, $hash_base, $ftype, $next_link);
3342 git_footer_html();
3345 sub git_search {
3346 if (!defined $searchtext) {
3347 die_error(undef, "Text field empty");
3349 if (!defined $hash) {
3350 $hash = git_get_head_hash($project);
3352 my %co = parse_commit($hash);
3353 if (!%co) {
3354 die_error(undef, "Unknown commit object");
3357 my $commit_search = 1;
3358 my $author_search = 0;
3359 my $committer_search = 0;
3360 my $pickaxe_search = 0;
3361 if ($searchtext =~ s/^author\\://i) {
3362 $author_search = 1;
3363 } elsif ($searchtext =~ s/^committer\\://i) {
3364 $committer_search = 1;
3365 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3366 $commit_search = 0;
3367 $pickaxe_search = 1;
3369 # pickaxe may take all resources of your box and run for several minutes
3370 # with every query - so decide by yourself how public you make this feature
3371 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3372 if (!$have_pickaxe) {
3373 die_error('403 Permission denied', "Permission denied");
3376 git_header_html();
3377 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3378 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3380 print "<table cellspacing=\"0\">\n";
3381 my $alternate = 0;
3382 if ($commit_search) {
3383 $/ = "\0";
3384 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3385 while (my $commit_text = <$fd>) {
3386 if (!grep m/$searchtext/i, $commit_text) {
3387 next;
3389 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3390 next;
3392 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3393 next;
3395 my @commit_lines = split "\n", $commit_text;
3396 my %co = parse_commit(undef, \@commit_lines);
3397 if (!%co) {
3398 next;
3400 if ($alternate) {
3401 print "<tr class=\"dark\">\n";
3402 } else {
3403 print "<tr class=\"light\">\n";
3405 $alternate ^= 1;
3406 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3407 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3408 "<td>" .
3409 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3410 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3411 my $comment = $co{'comment'};
3412 foreach my $line (@$comment) {
3413 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3414 my $lead = esc_html($1) || "";
3415 $lead = chop_str($lead, 30, 10);
3416 my $match = esc_html($2) || "";
3417 my $trail = esc_html($3) || "";
3418 $trail = chop_str($trail, 30, 10);
3419 my $text = "$lead<span class=\"match\">$match</span>$trail";
3420 print chop_str($text, 80, 5) . "<br/>\n";
3423 print "</td>\n" .
3424 "<td class=\"link\">" .
3425 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3426 " | " .
3427 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3428 print "</td>\n" .
3429 "</tr>\n";
3431 close $fd;
3434 if ($pickaxe_search) {
3435 $/ = "\n";
3436 my $git_command = git_cmd_str();
3437 open my $fd, "-|", "$git_command rev-list $hash | " .
3438 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3439 undef %co;
3440 my @files;
3441 while (my $line = <$fd>) {
3442 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3443 my %set;
3444 $set{'file'} = $6;
3445 $set{'from_id'} = $3;
3446 $set{'to_id'} = $4;
3447 $set{'id'} = $set{'to_id'};
3448 if ($set{'id'} =~ m/0{40}/) {
3449 $set{'id'} = $set{'from_id'};
3451 if ($set{'id'} =~ m/0{40}/) {
3452 next;
3454 push @files, \%set;
3455 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3456 if (%co) {
3457 if ($alternate) {
3458 print "<tr class=\"dark\">\n";
3459 } else {
3460 print "<tr class=\"light\">\n";
3462 $alternate ^= 1;
3463 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3464 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3465 "<td>" .
3466 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3467 -class => "list subject"},
3468 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3469 while (my $setref = shift @files) {
3470 my %set = %$setref;
3471 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3472 hash=>$set{'id'}, file_name=>$set{'file'}),
3473 -class => "list"},
3474 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3475 "<br/>\n";
3477 print "</td>\n" .
3478 "<td class=\"link\">" .
3479 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3480 " | " .
3481 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3482 print "</td>\n" .
3483 "</tr>\n";
3485 %co = parse_commit($1);
3488 close $fd;
3490 print "</table>\n";
3491 git_footer_html();
3494 sub git_shortlog {
3495 my $head = git_get_head_hash($project);
3496 if (!defined $hash) {
3497 $hash = $head;
3499 if (!defined $page) {
3500 $page = 0;
3502 my $refs = git_get_references();
3504 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3505 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3506 or die_error(undef, "Open git-rev-list failed");
3507 my @revlist = map { chomp; $_ } <$fd>;
3508 close $fd;
3510 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3511 my $next_link = '';
3512 if ($#revlist >= (100 * ($page+1)-1)) {
3513 $next_link =
3514 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3515 -title => "Alt-n"}, "next");
3519 git_header_html();
3520 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3521 git_print_header_div('summary', $project);
3523 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3525 git_footer_html();
3528 ## ......................................................................
3529 ## feeds (RSS, OPML)
3531 sub git_rss {
3532 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3533 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3534 or die_error(undef, "Open git-rev-list failed");
3535 my @revlist = map { chomp; $_ } <$fd>;
3536 close $fd or die_error(undef, "Reading git-rev-list failed");
3537 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3538 print <<XML;
3539 <?xml version="1.0" encoding="utf-8"?>
3540 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3541 <channel>
3542 <title>$project $my_uri $my_url</title>
3543 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3544 <description>$project log</description>
3545 <language>en</language>
3548 for (my $i = 0; $i <= $#revlist; $i++) {
3549 my $commit = $revlist[$i];
3550 my %co = parse_commit($commit);
3551 # we read 150, we always show 30 and the ones more recent than 48 hours
3552 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3553 last;
3555 my %cd = parse_date($co{'committer_epoch'});
3556 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3557 $co{'parent'}, $co{'id'}
3558 or next;
3559 my @difftree = map { chomp; $_ } <$fd>;
3560 close $fd
3561 or next;
3562 print "<item>\n" .
3563 "<title>" .
3564 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3565 "</title>\n" .
3566 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3567 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3568 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3569 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3570 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3571 "<content:encoded>" .
3572 "<![CDATA[\n";
3573 my $comment = $co{'comment'};
3574 foreach my $line (@$comment) {
3575 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3576 print "$line<br/>\n";
3578 print "<br/>\n";
3579 foreach my $line (@difftree) {
3580 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3581 next;
3583 my $file = esc_html(unquote($7));
3584 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3585 print "$file<br/>\n";
3587 print "]]>\n" .
3588 "</content:encoded>\n" .
3589 "</item>\n";
3591 print "</channel></rss>";
3594 sub git_opml {
3595 my @list = git_get_projects_list();
3597 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3598 print <<XML;
3599 <?xml version="1.0" encoding="utf-8"?>
3600 <opml version="1.0">
3601 <head>
3602 <title>$site_name Git OPML Export</title>
3603 </head>
3604 <body>
3605 <outline text="git RSS feeds">
3608 foreach my $pr (@list) {
3609 my %proj = %$pr;
3610 my $head = git_get_head_hash($proj{'path'});
3611 if (!defined $head) {
3612 next;
3614 $git_dir = "$projectroot/$proj{'path'}";
3615 my %co = parse_commit($head);
3616 if (!%co) {
3617 next;
3620 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3621 my $rss = "$my_url?p=$proj{'path'};a=rss";
3622 my $html = "$my_url?p=$proj{'path'};a=summary";
3623 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3625 print <<XML;
3626 </outline>
3627 </body>
3628 </opml>