gitweb: Remove creating directory for temporary files
[git/debian.git] / gitweb / gitweb.perl
blob0ae18103c1cba74a7278de1b24b8052467be882b
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++";
52 # source of projects list
53 our $projects_list = "++GITWEB_LIST++";
55 # list of git base URLs used for URL to where fetch project from,
56 # i.e. full URL is "$git_base_url/$project"
57 our @git_base_url_list = ("++GITWEB_BASE_URL++");
59 # default blob_plain mimetype and default charset for text/plain blob
60 our $default_blob_plain_mimetype = 'text/plain';
61 our $default_text_plain_charset = undef;
63 # file to use for guessing MIME types before trying /etc/mime.types
64 # (relative to the current git repository)
65 our $mimetypes_file = undef;
67 # You define site-wide feature defaults here; override them with
68 # $GITWEB_CONFIG as necessary.
69 our %feature = (
70 # feature => {'sub' => feature-sub, 'override' => allow-override, 'default' => [ default options...]
71 # if feature is overridable, feature-sub will be called with default options;
72 # return value indicates if to enable specified feature
74 'blame' => {
75 'sub' => \&feature_blame,
76 'override' => 0,
77 'default' => [0]},
79 'snapshot' => {
80 'sub' => \&feature_snapshot,
81 'override' => 0,
82 # => [content-encoding, suffix, program]
83 'default' => ['x-gzip', 'gz', 'gzip']},
86 sub gitweb_check_feature {
87 my ($name) = @_;
88 return undef unless exists $feature{$name};
89 my ($sub, $override, @defaults) = (
90 $feature{$name}{'sub'},
91 $feature{$name}{'override'},
92 @{$feature{$name}{'default'}});
93 if (!$override) { return @defaults; }
94 return $sub->(@defaults);
97 # To enable system wide have in $GITWEB_CONFIG
98 # $feature{'blame'}{'default'} = [1];
99 # To have project specific config enable override in $GITWEB_CONFIG
100 # $feature{'blame'}{'override'} = 1;
101 # and in project config gitweb.blame = 0|1;
103 sub feature_blame {
104 my ($val) = git_get_project_config('blame', '--bool');
106 if ($val eq 'true') {
107 return 1;
108 } elsif ($val eq 'false') {
109 return 0;
112 return $_[0];
115 # To disable system wide have in $GITWEB_CONFIG
116 # $feature{'snapshot'}{'default'} = [undef];
117 # To have project specific config enable override in $GITWEB_CONFIG
118 # $feature{'blame'}{'override'} = 1;
119 # and in project config gitweb.snapshot = none|gzip|bzip2
121 sub feature_snapshot {
122 my ($ctype, $suffix, $command) = @_;
124 my ($val) = git_get_project_config('snapshot');
126 if ($val eq 'gzip') {
127 return ('x-gzip', 'gz', 'gzip');
128 } elsif ($val eq 'bzip2') {
129 return ('x-bzip2', 'bz2', 'bzip2');
130 } elsif ($val eq 'none') {
131 return ();
134 return ($ctype, $suffix, $command);
137 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
138 require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
140 # version of the core git binary
141 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
143 $projects_list ||= $projectroot;
145 # ======================================================================
146 # input validation and dispatch
147 our $action = $cgi->param('a');
148 if (defined $action) {
149 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
150 die_error(undef, "Invalid action parameter");
154 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
155 if (defined $project) {
156 $project =~ s|^/||;
157 $project =~ s|/$||;
158 $project = undef unless $project;
160 if (defined $project) {
161 if (!validate_input($project)) {
162 die_error(undef, "Invalid project parameter");
164 if (!(-d "$projectroot/$project")) {
165 die_error(undef, "No such directory");
167 if (!(-e "$projectroot/$project/HEAD")) {
168 die_error(undef, "No such project");
170 $ENV{'GIT_DIR'} = "$projectroot/$project";
173 our $file_name = $cgi->param('f');
174 if (defined $file_name) {
175 if (!validate_input($file_name)) {
176 die_error(undef, "Invalid file parameter");
180 our $file_parent = $cgi->param('fp');
181 if (defined $file_parent) {
182 if (!validate_input($file_parent)) {
183 die_error(undef, "Invalid file parent parameter");
187 our $hash = $cgi->param('h');
188 if (defined $hash) {
189 if (!validate_input($hash)) {
190 die_error(undef, "Invalid hash parameter");
194 our $hash_parent = $cgi->param('hp');
195 if (defined $hash_parent) {
196 if (!validate_input($hash_parent)) {
197 die_error(undef, "Invalid hash parent parameter");
201 our $hash_base = $cgi->param('hb');
202 if (defined $hash_base) {
203 if (!validate_input($hash_base)) {
204 die_error(undef, "Invalid hash base parameter");
208 our $hash_parent_base = $cgi->param('hpb');
209 if (defined $hash_parent_base) {
210 if (!validate_input($hash_parent_base)) {
211 die_error(undef, "Invalid hash parent base parameter");
215 our $page = $cgi->param('pg');
216 if (defined $page) {
217 if ($page =~ m/[^0-9]$/) {
218 die_error(undef, "Invalid page parameter");
222 our $searchtext = $cgi->param('s');
223 if (defined $searchtext) {
224 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
225 die_error(undef, "Invalid search parameter");
227 $searchtext = quotemeta $searchtext;
230 # dispatch
231 my %actions = (
232 "blame" => \&git_blame2,
233 "blobdiff" => \&git_blobdiff,
234 "blobdiff_plain" => \&git_blobdiff_plain,
235 "blob" => \&git_blob,
236 "blob_plain" => \&git_blob_plain,
237 "commitdiff" => \&git_commitdiff,
238 "commitdiff_plain" => \&git_commitdiff_plain,
239 "commit" => \&git_commit,
240 "heads" => \&git_heads,
241 "history" => \&git_history,
242 "log" => \&git_log,
243 "rss" => \&git_rss,
244 "search" => \&git_search,
245 "shortlog" => \&git_shortlog,
246 "summary" => \&git_summary,
247 "tag" => \&git_tag,
248 "tags" => \&git_tags,
249 "tree" => \&git_tree,
250 "snapshot" => \&git_snapshot,
251 # those below don't need $project
252 "opml" => \&git_opml,
253 "project_list" => \&git_project_list,
256 if (defined $project) {
257 $action ||= 'summary';
258 } else {
259 $action ||= 'project_list';
261 if (!defined($actions{$action})) {
262 die_error(undef, "Unknown action");
264 $actions{$action}->();
265 exit;
267 ## ======================================================================
268 ## action links
270 sub href(%) {
271 my %params = @_;
273 my @mapping = (
274 project => "p",
275 action => "a",
276 file_name => "f",
277 file_parent => "fp",
278 hash => "h",
279 hash_parent => "hp",
280 hash_base => "hb",
281 hash_parent_base => "hpb",
282 page => "pg",
283 searchtext => "s",
285 my %mapping = @mapping;
287 $params{"project"} ||= $project;
289 my @result = ();
290 for (my $i = 0; $i < @mapping; $i += 2) {
291 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
292 if (defined $params{$name}) {
293 push @result, $symbol . "=" . esc_param($params{$name});
296 return "$my_uri?" . join(';', @result);
300 ## ======================================================================
301 ## validation, quoting/unquoting and escaping
303 sub validate_input {
304 my $input = shift;
306 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
307 return $input;
309 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
310 return undef;
312 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
313 return undef;
315 return $input;
318 # quote unsafe chars, but keep the slash, even when it's not
319 # correct, but quoted slashes look too horrible in bookmarks
320 sub esc_param {
321 my $str = shift;
322 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
323 $str =~ s/\+/%2B/g;
324 $str =~ s/ /\+/g;
325 return $str;
328 # replace invalid utf8 character with SUBSTITUTION sequence
329 sub esc_html {
330 my $str = shift;
331 $str = decode("utf8", $str, Encode::FB_DEFAULT);
332 $str = escapeHTML($str);
333 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
334 return $str;
337 # git may return quoted and escaped filenames
338 sub unquote {
339 my $str = shift;
340 if ($str =~ m/^"(.*)"$/) {
341 $str = $1;
342 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
344 return $str;
347 # escape tabs (convert tabs to spaces)
348 sub untabify {
349 my $line = shift;
351 while ((my $pos = index($line, "\t")) != -1) {
352 if (my $count = (8 - ($pos % 8))) {
353 my $spaces = ' ' x $count;
354 $line =~ s/\t/$spaces/;
358 return $line;
361 ## ----------------------------------------------------------------------
362 ## HTML aware string manipulation
364 sub chop_str {
365 my $str = shift;
366 my $len = shift;
367 my $add_len = shift || 10;
369 # allow only $len chars, but don't cut a word if it would fit in $add_len
370 # if it doesn't fit, cut it if it's still longer than the dots we would add
371 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
372 my $body = $1;
373 my $tail = $2;
374 if (length($tail) > 4) {
375 $tail = " ...";
376 $body =~ s/&[^;]*$//; # remove chopped character entities
378 return "$body$tail";
381 ## ----------------------------------------------------------------------
382 ## functions returning short strings
384 # CSS class for given age value (in seconds)
385 sub age_class {
386 my $age = shift;
388 if ($age < 60*60*2) {
389 return "age0";
390 } elsif ($age < 60*60*24*2) {
391 return "age1";
392 } else {
393 return "age2";
397 # convert age in seconds to "nn units ago" string
398 sub age_string {
399 my $age = shift;
400 my $age_str;
402 if ($age > 60*60*24*365*2) {
403 $age_str = (int $age/60/60/24/365);
404 $age_str .= " years ago";
405 } elsif ($age > 60*60*24*(365/12)*2) {
406 $age_str = int $age/60/60/24/(365/12);
407 $age_str .= " months ago";
408 } elsif ($age > 60*60*24*7*2) {
409 $age_str = int $age/60/60/24/7;
410 $age_str .= " weeks ago";
411 } elsif ($age > 60*60*24*2) {
412 $age_str = int $age/60/60/24;
413 $age_str .= " days ago";
414 } elsif ($age > 60*60*2) {
415 $age_str = int $age/60/60;
416 $age_str .= " hours ago";
417 } elsif ($age > 60*2) {
418 $age_str = int $age/60;
419 $age_str .= " min ago";
420 } elsif ($age > 2) {
421 $age_str = int $age;
422 $age_str .= " sec ago";
423 } else {
424 $age_str .= " right now";
426 return $age_str;
429 # convert file mode in octal to symbolic file mode string
430 sub mode_str {
431 my $mode = oct shift;
433 if (S_ISDIR($mode & S_IFMT)) {
434 return 'drwxr-xr-x';
435 } elsif (S_ISLNK($mode)) {
436 return 'lrwxrwxrwx';
437 } elsif (S_ISREG($mode)) {
438 # git cares only about the executable bit
439 if ($mode & S_IXUSR) {
440 return '-rwxr-xr-x';
441 } else {
442 return '-rw-r--r--';
444 } else {
445 return '----------';
449 # convert file mode in octal to file type string
450 sub file_type {
451 my $mode = shift;
453 if ($mode !~ m/^[0-7]+$/) {
454 return $mode;
455 } else {
456 $mode = oct $mode;
459 if (S_ISDIR($mode & S_IFMT)) {
460 return "directory";
461 } elsif (S_ISLNK($mode)) {
462 return "symlink";
463 } elsif (S_ISREG($mode)) {
464 return "file";
465 } else {
466 return "unknown";
470 ## ----------------------------------------------------------------------
471 ## functions returning short HTML fragments, or transforming HTML fragments
472 ## which don't beling to other sections
474 # format line of commit message or tag comment
475 sub format_log_line_html {
476 my $line = shift;
478 $line = esc_html($line);
479 $line =~ s/ /&nbsp;/g;
480 if ($line =~ m/([0-9a-fA-F]{40})/) {
481 my $hash_text = $1;
482 if (git_get_type($hash_text) eq "commit") {
483 my $link =
484 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
485 -class => "text"}, $hash_text);
486 $line =~ s/$hash_text/$link/;
489 return $line;
492 # format marker of refs pointing to given object
493 sub format_ref_marker {
494 my ($refs, $id) = @_;
495 my $markers = '';
497 if (defined $refs->{$id}) {
498 foreach my $ref (@{$refs->{$id}}) {
499 my ($type, $name) = qw();
500 # e.g. tags/v2.6.11 or heads/next
501 if ($ref =~ m!^(.*?)s?/(.*)$!) {
502 $type = $1;
503 $name = $2;
504 } else {
505 $type = "ref";
506 $name = $ref;
509 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
513 if ($markers) {
514 return ' <span class="refs">'. $markers . '</span>';
515 } else {
516 return "";
520 # format, perhaps shortened and with markers, title line
521 sub format_subject_html {
522 my ($long, $short, $href, $extra) = @_;
523 $extra = '' unless defined($extra);
525 if (length($short) < length($long)) {
526 return $cgi->a({-href => $href, -class => "list subject",
527 -title => $long},
528 esc_html($short) . $extra);
529 } else {
530 return $cgi->a({-href => $href, -class => "list subject"},
531 esc_html($long) . $extra);
535 sub format_diff_line {
536 my $line = shift;
537 my $char = substr($line, 0, 1);
538 my $diff_class = "";
540 chomp $line;
542 if ($char eq '+') {
543 $diff_class = " add";
544 } elsif ($char eq "-") {
545 $diff_class = " rem";
546 } elsif ($char eq "@") {
547 $diff_class = " chunk_header";
548 } elsif ($char eq "\\") {
549 $diff_class = " incomplete";
551 $line = untabify($line);
552 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
555 ## ----------------------------------------------------------------------
556 ## git utility subroutines, invoking git commands
558 # get HEAD ref of given project as hash
559 sub git_get_head_hash {
560 my $project = shift;
561 my $oENV = $ENV{'GIT_DIR'};
562 my $retval = undef;
563 $ENV{'GIT_DIR'} = "$projectroot/$project";
564 if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
565 my $head = <$fd>;
566 close $fd;
567 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
568 $retval = $1;
571 if (defined $oENV) {
572 $ENV{'GIT_DIR'} = $oENV;
574 return $retval;
577 # get type of given object
578 sub git_get_type {
579 my $hash = shift;
581 open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
582 my $type = <$fd>;
583 close $fd or return;
584 chomp $type;
585 return $type;
588 sub git_get_project_config {
589 my ($key, $type) = @_;
591 return unless ($key);
592 $key =~ s/^gitweb\.//;
593 return if ($key =~ m/\W/);
595 my @x = ($GIT, 'repo-config');
596 if (defined $type) { push @x, $type; }
597 push @x, "--get";
598 push @x, "gitweb.$key";
599 my $val = qx(@x);
600 chomp $val;
601 return ($val);
604 # get hash of given path at given ref
605 sub git_get_hash_by_path {
606 my $base = shift;
607 my $path = shift || return undef;
609 my $tree = $base;
611 open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
612 or die_error(undef, "Open git-ls-tree failed");
613 my $line = <$fd>;
614 close $fd or return undef;
616 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
617 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
618 return $3;
621 # converts symbolic name to hash
622 sub git_to_hash {
623 my @params = @_;
624 return undef unless @params;
626 open my $fd, "-|", $GIT, "rev-parse", @params
627 or return undef;
628 my @hashes = map { chomp; $_ } <$fd>;
629 close $fd;
631 if (wantarray) {
632 return @hashes;
633 } elsif (scalar(@hashes) == 1) {
634 # single hash
635 return $hashes[0];
636 } else {
637 return \@hashes;
641 ## ......................................................................
642 ## git utility functions, directly accessing git repository
644 # assumes that PATH is not symref
645 sub git_get_hash_by_ref {
646 my $path = shift;
648 open my $fd, "$projectroot/$path" or return undef;
649 my $head = <$fd>;
650 close $fd;
651 chomp $head;
652 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
653 return $head;
657 sub git_get_project_description {
658 my $path = shift;
660 open my $fd, "$projectroot/$path/description" or return undef;
661 my $descr = <$fd>;
662 close $fd;
663 chomp $descr;
664 return $descr;
667 sub git_get_project_url_list {
668 my $path = shift;
670 open my $fd, "$projectroot/$path/cloneurl" or return undef;
671 my @git_project_url_list = map { chomp; $_ } <$fd>;
672 close $fd;
674 return wantarray ? @git_project_url_list : \@git_project_url_list;
677 sub git_get_projects_list {
678 my @list;
680 if (-d $projects_list) {
681 # search in directory
682 my $dir = $projects_list;
683 opendir my ($dh), $dir or return undef;
684 while (my $dir = readdir($dh)) {
685 if (-e "$projectroot/$dir/HEAD") {
686 my $pr = {
687 path => $dir,
689 push @list, $pr
692 closedir($dh);
693 } elsif (-f $projects_list) {
694 # read from file(url-encoded):
695 # 'git%2Fgit.git Linus+Torvalds'
696 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
697 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
698 open my ($fd), $projects_list or return undef;
699 while (my $line = <$fd>) {
700 chomp $line;
701 my ($path, $owner) = split ' ', $line;
702 $path = unescape($path);
703 $owner = unescape($owner);
704 if (!defined $path) {
705 next;
707 if (-e "$projectroot/$path/HEAD") {
708 my $pr = {
709 path => $path,
710 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
712 push @list, $pr
715 close $fd;
717 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
718 return @list;
721 sub git_get_project_owner {
722 my $project = shift;
723 my $owner;
725 return undef unless $project;
727 # read from file (url-encoded):
728 # 'git%2Fgit.git Linus+Torvalds'
729 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
730 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
731 if (-f $projects_list) {
732 open (my $fd , $projects_list);
733 while (my $line = <$fd>) {
734 chomp $line;
735 my ($pr, $ow) = split ' ', $line;
736 $pr = unescape($pr);
737 $ow = unescape($ow);
738 if ($pr eq $project) {
739 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
740 last;
743 close $fd;
745 if (!defined $owner) {
746 $owner = get_file_owner("$projectroot/$project");
749 return $owner;
752 sub git_get_references {
753 my $type = shift || "";
754 my %refs;
755 my $fd;
756 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
757 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
758 if (-f "$projectroot/$project/info/refs") {
759 open $fd, "$projectroot/$project/info/refs"
760 or return;
761 } else {
762 open $fd, "-|", $GIT, "ls-remote", "."
763 or return;
766 while (my $line = <$fd>) {
767 chomp $line;
768 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
769 if (defined $refs{$1}) {
770 push @{$refs{$1}}, $2;
771 } else {
772 $refs{$1} = [ $2 ];
776 close $fd or return;
777 return \%refs;
780 sub git_get_following_references {
781 my $hash = shift || return undef;
782 my $type = shift;
783 my $base = shift || $hash_base || "HEAD";
785 my $refs = git_get_references($type);
786 open my $fd, "-|", $GIT, "rev-list", $base
787 or return undef;
788 my @commits = map { chomp; $_ } <$fd>;
789 close $fd
790 or return undef;
792 my @reflist;
793 my $lastref;
795 foreach my $commit (@commits) {
796 foreach my $ref (@{$refs->{$commit}}) {
797 $lastref = $ref;
798 push @reflist, $lastref;
800 if ($commit eq $hash) {
801 last;
805 return wantarray ? @reflist : $lastref;
808 sub git_get_preceding_references {
809 my $hash = shift || return undef;
810 my $type = shift;
812 my $refs = git_get_references($type);
813 open my $fd, "-|", $GIT, "rev-list", $hash
814 or return undef;
815 my @commits = map { chomp; $_ } <$fd>;
816 close $fd
817 or return undef;
819 my @reflist;
821 foreach my $commit (@commits) {
822 foreach my $ref (@{$refs->{$commit}}) {
823 return $ref unless wantarray;
824 push @reflist, $ref;
828 return @reflist;
831 sub git_get_rev_name_tags {
832 my $hash = shift || return undef;
834 open my $fd, "-|", $GIT, "name-rev", "--tags", $hash
835 or return;
836 my $name_rev = <$fd>;
837 close $fd;
839 if ($name_rev =~ m|^$hash tags/(.*)$|) {
840 return $1;
841 } else {
842 # catches also '$hash undefined' output
843 return undef;
847 ## ----------------------------------------------------------------------
848 ## parse to hash functions
850 sub parse_date {
851 my $epoch = shift;
852 my $tz = shift || "-0000";
854 my %date;
855 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
856 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
857 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
858 $date{'hour'} = $hour;
859 $date{'minute'} = $min;
860 $date{'mday'} = $mday;
861 $date{'day'} = $days[$wday];
862 $date{'month'} = $months[$mon];
863 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
864 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
865 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
866 $mday, $months[$mon], $hour ,$min;
868 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
869 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
870 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
871 $date{'hour_local'} = $hour;
872 $date{'minute_local'} = $min;
873 $date{'tz_local'} = $tz;
874 return %date;
877 sub parse_tag {
878 my $tag_id = shift;
879 my %tag;
880 my @comment;
882 open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
883 $tag{'id'} = $tag_id;
884 while (my $line = <$fd>) {
885 chomp $line;
886 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
887 $tag{'object'} = $1;
888 } elsif ($line =~ m/^type (.+)$/) {
889 $tag{'type'} = $1;
890 } elsif ($line =~ m/^tag (.+)$/) {
891 $tag{'name'} = $1;
892 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
893 $tag{'author'} = $1;
894 $tag{'epoch'} = $2;
895 $tag{'tz'} = $3;
896 } elsif ($line =~ m/--BEGIN/) {
897 push @comment, $line;
898 last;
899 } elsif ($line eq "") {
900 last;
903 push @comment, <$fd>;
904 $tag{'comment'} = \@comment;
905 close $fd or return;
906 if (!defined $tag{'name'}) {
907 return
909 return %tag
912 sub parse_commit {
913 my $commit_id = shift;
914 my $commit_text = shift;
916 my @commit_lines;
917 my %co;
919 if (defined $commit_text) {
920 @commit_lines = @$commit_text;
921 } else {
922 $/ = "\0";
923 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id
924 or return;
925 @commit_lines = split '\n', <$fd>;
926 close $fd or return;
927 $/ = "\n";
928 pop @commit_lines;
930 my $header = shift @commit_lines;
931 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
932 return;
934 ($co{'id'}, my @parents) = split ' ', $header;
935 $co{'parents'} = \@parents;
936 $co{'parent'} = $parents[0];
937 while (my $line = shift @commit_lines) {
938 last if $line eq "\n";
939 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
940 $co{'tree'} = $1;
941 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
942 $co{'author'} = $1;
943 $co{'author_epoch'} = $2;
944 $co{'author_tz'} = $3;
945 if ($co{'author'} =~ m/^([^<]+) </) {
946 $co{'author_name'} = $1;
947 } else {
948 $co{'author_name'} = $co{'author'};
950 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
951 $co{'committer'} = $1;
952 $co{'committer_epoch'} = $2;
953 $co{'committer_tz'} = $3;
954 $co{'committer_name'} = $co{'committer'};
955 $co{'committer_name'} =~ s/ <.*//;
958 if (!defined $co{'tree'}) {
959 return;
962 foreach my $title (@commit_lines) {
963 $title =~ s/^ //;
964 if ($title ne "") {
965 $co{'title'} = chop_str($title, 80, 5);
966 # remove leading stuff of merges to make the interesting part visible
967 if (length($title) > 50) {
968 $title =~ s/^Automatic //;
969 $title =~ s/^merge (of|with) /Merge ... /i;
970 if (length($title) > 50) {
971 $title =~ s/(http|rsync):\/\///;
973 if (length($title) > 50) {
974 $title =~ s/(master|www|rsync)\.//;
976 if (length($title) > 50) {
977 $title =~ s/kernel.org:?//;
979 if (length($title) > 50) {
980 $title =~ s/\/pub\/scm//;
983 $co{'title_short'} = chop_str($title, 50, 5);
984 last;
987 # remove added spaces
988 foreach my $line (@commit_lines) {
989 $line =~ s/^ //;
991 $co{'comment'} = \@commit_lines;
993 my $age = time - $co{'committer_epoch'};
994 $co{'age'} = $age;
995 $co{'age_string'} = age_string($age);
996 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
997 if ($age > 60*60*24*7*2) {
998 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
999 $co{'age_string_age'} = $co{'age_string'};
1000 } else {
1001 $co{'age_string_date'} = $co{'age_string'};
1002 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1004 return %co;
1007 # parse ref from ref_file, given by ref_id, with given type
1008 sub parse_ref {
1009 my $ref_file = shift;
1010 my $ref_id = shift;
1011 my $type = shift || git_get_type($ref_id);
1012 my %ref_item;
1014 $ref_item{'type'} = $type;
1015 $ref_item{'id'} = $ref_id;
1016 $ref_item{'epoch'} = 0;
1017 $ref_item{'age'} = "unknown";
1018 if ($type eq "tag") {
1019 my %tag = parse_tag($ref_id);
1020 $ref_item{'comment'} = $tag{'comment'};
1021 if ($tag{'type'} eq "commit") {
1022 my %co = parse_commit($tag{'object'});
1023 $ref_item{'epoch'} = $co{'committer_epoch'};
1024 $ref_item{'age'} = $co{'age_string'};
1025 } elsif (defined($tag{'epoch'})) {
1026 my $age = time - $tag{'epoch'};
1027 $ref_item{'epoch'} = $tag{'epoch'};
1028 $ref_item{'age'} = age_string($age);
1030 $ref_item{'reftype'} = $tag{'type'};
1031 $ref_item{'name'} = $tag{'name'};
1032 $ref_item{'refid'} = $tag{'object'};
1033 } elsif ($type eq "commit"){
1034 my %co = parse_commit($ref_id);
1035 $ref_item{'reftype'} = "commit";
1036 $ref_item{'name'} = $ref_file;
1037 $ref_item{'title'} = $co{'title'};
1038 $ref_item{'refid'} = $ref_id;
1039 $ref_item{'epoch'} = $co{'committer_epoch'};
1040 $ref_item{'age'} = $co{'age_string'};
1041 } else {
1042 $ref_item{'reftype'} = $type;
1043 $ref_item{'name'} = $ref_file;
1044 $ref_item{'refid'} = $ref_id;
1047 return %ref_item;
1050 # parse line of git-diff-tree "raw" output
1051 sub parse_difftree_raw_line {
1052 my $line = shift;
1053 my %res;
1055 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1056 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1057 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1058 $res{'from_mode'} = $1;
1059 $res{'to_mode'} = $2;
1060 $res{'from_id'} = $3;
1061 $res{'to_id'} = $4;
1062 $res{'status'} = $5;
1063 $res{'similarity'} = $6;
1064 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1065 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1066 } else {
1067 $res{'file'} = unquote($7);
1070 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1071 #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1072 # $res{'commit'} = $1;
1075 return wantarray ? %res : \%res;
1078 ## ......................................................................
1079 ## parse to array of hashes functions
1081 sub git_get_refs_list {
1082 my $ref_dir = shift;
1083 my @reflist;
1085 my @refs;
1086 my $pfxlen = length("$projectroot/$project/$ref_dir");
1087 File::Find::find(sub {
1088 return if (/^\./);
1089 if (-f $_) {
1090 push @refs, substr($File::Find::name, $pfxlen + 1);
1092 }, "$projectroot/$project/$ref_dir");
1094 foreach my $ref_file (@refs) {
1095 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
1096 my $type = git_get_type($ref_id) || next;
1097 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1099 push @reflist, \%ref_item;
1101 # sort refs by age
1102 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1103 return \@reflist;
1106 ## ----------------------------------------------------------------------
1107 ## filesystem-related functions
1109 sub get_file_owner {
1110 my $path = shift;
1112 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1113 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1114 if (!defined $gcos) {
1115 return undef;
1117 my $owner = $gcos;
1118 $owner =~ s/[,;].*$//;
1119 return decode("utf8", $owner, Encode::FB_DEFAULT);
1122 ## ......................................................................
1123 ## mimetype related functions
1125 sub mimetype_guess_file {
1126 my $filename = shift;
1127 my $mimemap = shift;
1128 -r $mimemap or return undef;
1130 my %mimemap;
1131 open(MIME, $mimemap) or return undef;
1132 while (<MIME>) {
1133 next if m/^#/; # skip comments
1134 my ($mime, $exts) = split(/\t+/);
1135 if (defined $exts) {
1136 my @exts = split(/\s+/, $exts);
1137 foreach my $ext (@exts) {
1138 $mimemap{$ext} = $mime;
1142 close(MIME);
1144 $filename =~ /\.(.*?)$/;
1145 return $mimemap{$1};
1148 sub mimetype_guess {
1149 my $filename = shift;
1150 my $mime;
1151 $filename =~ /\./ or return undef;
1153 if ($mimetypes_file) {
1154 my $file = $mimetypes_file;
1155 if ($file !~ m!^/!) { # if it is relative path
1156 # it is relative to project
1157 $file = "$projectroot/$project/$file";
1159 $mime = mimetype_guess_file($filename, $file);
1161 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1162 return $mime;
1165 sub blob_mimetype {
1166 my $fd = shift;
1167 my $filename = shift;
1169 if ($filename) {
1170 my $mime = mimetype_guess($filename);
1171 $mime and return $mime;
1174 # just in case
1175 return $default_blob_plain_mimetype unless $fd;
1177 if (-T $fd) {
1178 return 'text/plain' .
1179 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1180 } elsif (! $filename) {
1181 return 'application/octet-stream';
1182 } elsif ($filename =~ m/\.png$/i) {
1183 return 'image/png';
1184 } elsif ($filename =~ m/\.gif$/i) {
1185 return 'image/gif';
1186 } elsif ($filename =~ m/\.jpe?g$/i) {
1187 return 'image/jpeg';
1188 } else {
1189 return 'application/octet-stream';
1193 ## ======================================================================
1194 ## functions printing HTML: header, footer, error page
1196 sub git_header_html {
1197 my $status = shift || "200 OK";
1198 my $expires = shift;
1200 my $title = "$site_name git";
1201 if (defined $project) {
1202 $title .= " - $project";
1203 if (defined $action) {
1204 $title .= "/$action";
1205 if (defined $file_name) {
1206 $title .= " - $file_name";
1207 if ($action eq "tree" && $file_name !~ m|/$|) {
1208 $title .= "/";
1213 my $content_type;
1214 # require explicit support from the UA if we are to send the page as
1215 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1216 # we have to do this because MSIE sometimes globs '*/*', pretending to
1217 # support xhtml+xml but choking when it gets what it asked for.
1218 if (defined $cgi->http('HTTP_ACCEPT') &&
1219 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1220 $cgi->Accept('application/xhtml+xml') != 0) {
1221 $content_type = 'application/xhtml+xml';
1222 } else {
1223 $content_type = 'text/html';
1225 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1226 -status=> $status, -expires => $expires);
1227 print <<EOF;
1228 <?xml version="1.0" encoding="utf-8"?>
1229 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1230 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1231 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1232 <!-- git core binaries version $git_version -->
1233 <head>
1234 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1235 <meta name="generator" content="gitweb/$version git/$git_version"/>
1236 <meta name="robots" content="index, nofollow"/>
1237 <title>$title</title>
1238 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1240 if (defined $project) {
1241 printf('<link rel="alternate" title="%s log" '.
1242 'href="%s" type="application/rss+xml"/>'."\n",
1243 esc_param($project), href(action=>"rss"));
1246 print "</head>\n" .
1247 "<body>\n" .
1248 "<div class=\"page_header\">\n" .
1249 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1250 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1251 "</a>\n";
1252 print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1253 if (defined $project) {
1254 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1255 if (defined $action) {
1256 print " / $action";
1258 print "\n";
1259 if (!defined $searchtext) {
1260 $searchtext = "";
1262 my $search_hash;
1263 if (defined $hash_base) {
1264 $search_hash = $hash_base;
1265 } elsif (defined $hash) {
1266 $search_hash = $hash;
1267 } else {
1268 $search_hash = "HEAD";
1270 $cgi->param("a", "search");
1271 $cgi->param("h", $search_hash);
1272 print $cgi->startform(-method => "get", -action => $my_uri) .
1273 "<div class=\"search\">\n" .
1274 $cgi->hidden(-name => "p") . "\n" .
1275 $cgi->hidden(-name => "a") . "\n" .
1276 $cgi->hidden(-name => "h") . "\n" .
1277 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1278 "</div>" .
1279 $cgi->end_form() . "\n";
1281 print "</div>\n";
1284 sub git_footer_html {
1285 print "<div class=\"page_footer\">\n";
1286 if (defined $project) {
1287 my $descr = git_get_project_description($project);
1288 if (defined $descr) {
1289 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1291 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1292 } else {
1293 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1295 print "</div>\n" .
1296 "</body>\n" .
1297 "</html>";
1300 sub die_error {
1301 my $status = shift || "403 Forbidden";
1302 my $error = shift || "Malformed query, file missing or permission denied";
1304 git_header_html($status);
1305 print <<EOF;
1306 <div class="page_body">
1307 <br /><br />
1308 $status - $error
1309 <br />
1310 </div>
1312 git_footer_html();
1313 exit;
1316 ## ----------------------------------------------------------------------
1317 ## functions printing or outputting HTML: navigation
1319 sub git_print_page_nav {
1320 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1321 $extra = '' if !defined $extra; # pager or formats
1323 my @navs = qw(summary shortlog log commit commitdiff tree);
1324 if ($suppress) {
1325 @navs = grep { $_ ne $suppress } @navs;
1328 my %arg = map { $_ => {action=>$_} } @navs;
1329 if (defined $head) {
1330 for (qw(commit commitdiff)) {
1331 $arg{$_}{hash} = $head;
1333 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1334 for (qw(shortlog log)) {
1335 $arg{$_}{hash} = $head;
1339 $arg{tree}{hash} = $treehead if defined $treehead;
1340 $arg{tree}{hash_base} = $treebase if defined $treebase;
1342 print "<div class=\"page_nav\">\n" .
1343 (join " | ",
1344 map { $_ eq $current ?
1345 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1346 } @navs);
1347 print "<br/>\n$extra<br/>\n" .
1348 "</div>\n";
1351 sub format_paging_nav {
1352 my ($action, $hash, $head, $page, $nrevs) = @_;
1353 my $paging_nav;
1356 if ($hash ne $head || $page) {
1357 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1358 } else {
1359 $paging_nav .= "HEAD";
1362 if ($page > 0) {
1363 $paging_nav .= " &sdot; " .
1364 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1365 -accesskey => "p", -title => "Alt-p"}, "prev");
1366 } else {
1367 $paging_nav .= " &sdot; prev";
1370 if ($nrevs >= (100 * ($page+1)-1)) {
1371 $paging_nav .= " &sdot; " .
1372 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1373 -accesskey => "n", -title => "Alt-n"}, "next");
1374 } else {
1375 $paging_nav .= " &sdot; next";
1378 return $paging_nav;
1381 ## ......................................................................
1382 ## functions printing or outputting HTML: div
1384 sub git_print_header_div {
1385 my ($action, $title, $hash, $hash_base) = @_;
1386 my %args = ();
1388 $args{action} = $action;
1389 $args{hash} = $hash if $hash;
1390 $args{hash_base} = $hash_base if $hash_base;
1392 print "<div class=\"header\">\n" .
1393 $cgi->a({-href => href(%args), -class => "title"},
1394 $title ? $title : $action) .
1395 "\n</div>\n";
1398 sub git_print_page_path {
1399 my $name = shift;
1400 my $type = shift;
1401 my $hb = shift;
1403 if (!defined $name) {
1404 print "<div class=\"page_path\">/</div>\n";
1405 } elsif (defined $type && $type eq 'blob') {
1406 print "<div class=\"page_path\">";
1407 if (defined $hb) {
1408 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1409 hash_base=>$hb)},
1410 esc_html($name));
1411 } else {
1412 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1413 esc_html($name));
1415 print "<br/></div>\n";
1416 } else {
1417 print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1421 sub git_print_log {
1422 my $log = shift;
1424 # remove leading empty lines
1425 while (defined $log->[0] && $log->[0] eq "") {
1426 shift @$log;
1429 # print log
1430 my $signoff = 0;
1431 my $empty = 0;
1432 foreach my $line (@$log) {
1433 # print only one empty line
1434 # do not print empty line after signoff
1435 if ($line eq "") {
1436 next if ($empty || $signoff);
1437 $empty = 1;
1438 } else {
1439 $empty = 0;
1441 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1442 $signoff = 1;
1443 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1444 } else {
1445 $signoff = 0;
1446 print format_log_line_html($line) . "<br/>\n";
1451 sub git_print_simplified_log {
1452 my $log = shift;
1453 my $remove_title = shift;
1455 shift @$log if $remove_title;
1456 # remove leading empty lines
1457 while (defined $log->[0] && $log->[0] eq "") {
1458 shift @$log;
1461 # simplify and print log
1462 my $empty = 0;
1463 foreach my $line (@$log) {
1464 # remove signoff lines
1465 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1466 next;
1468 # print only one empty line
1469 if ($line eq "") {
1470 next if $empty;
1471 $empty = 1;
1472 } else {
1473 $empty = 0;
1475 print format_log_line_html($line) . "<br/>\n";
1477 # end with single empty line
1478 print "<br/>\n" unless $empty;
1481 ## ......................................................................
1482 ## functions printing large fragments of HTML
1484 sub git_difftree_body {
1485 my ($difftree, $hash, $parent) = @_;
1487 print "<div class=\"list_head\">\n";
1488 if ($#{$difftree} > 10) {
1489 print(($#{$difftree} + 1) . " files changed:\n");
1491 print "</div>\n";
1493 print "<table class=\"diff_tree\">\n";
1494 my $alternate = 0;
1495 foreach my $line (@{$difftree}) {
1496 my %diff = parse_difftree_raw_line($line);
1498 if ($alternate) {
1499 print "<tr class=\"dark\">\n";
1500 } else {
1501 print "<tr class=\"light\">\n";
1503 $alternate ^= 1;
1505 my ($to_mode_oct, $to_mode_str, $to_file_type);
1506 my ($from_mode_oct, $from_mode_str, $from_file_type);
1507 if ($diff{'to_mode'} ne ('0' x 6)) {
1508 $to_mode_oct = oct $diff{'to_mode'};
1509 if (S_ISREG($to_mode_oct)) { # only for regular file
1510 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1512 $to_file_type = file_type($diff{'to_mode'});
1514 if ($diff{'from_mode'} ne ('0' x 6)) {
1515 $from_mode_oct = oct $diff{'from_mode'};
1516 if (S_ISREG($to_mode_oct)) { # only for regular file
1517 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1519 $from_file_type = file_type($diff{'from_mode'});
1522 if ($diff{'status'} eq "A") { # created
1523 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1524 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1525 $mode_chng .= "]</span>";
1526 print "<td>" .
1527 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1528 hash_base=>$hash, file_name=>$diff{'file'}),
1529 -class => "list"}, esc_html($diff{'file'})) .
1530 "</td>\n" .
1531 "<td>$mode_chng</td>\n" .
1532 "<td class=\"link\">" .
1533 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1534 hash_base=>$hash, file_name=>$diff{'file'})},
1535 "blob") .
1536 "</td>\n";
1538 } elsif ($diff{'status'} eq "D") { # deleted
1539 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1540 print "<td>" .
1541 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1542 hash_base=>$parent, file_name=>$diff{'file'}),
1543 -class => "list"}, esc_html($diff{'file'})) .
1544 "</td>\n" .
1545 "<td>$mode_chng</td>\n" .
1546 "<td class=\"link\">" .
1547 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1548 hash_base=>$parent, file_name=>$diff{'file'})},
1549 "blob") .
1550 " | " .
1551 $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1552 file_name=>$diff{'file'})},\
1553 "history") .
1554 "</td>\n";
1556 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1557 my $mode_chnge = "";
1558 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1559 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1560 if ($from_file_type != $to_file_type) {
1561 $mode_chnge .= " from $from_file_type to $to_file_type";
1563 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1564 if ($from_mode_str && $to_mode_str) {
1565 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1566 } elsif ($to_mode_str) {
1567 $mode_chnge .= " mode: $to_mode_str";
1570 $mode_chnge .= "]</span>\n";
1572 print "<td>";
1573 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1574 print $cgi->a({-href => href(action=>"blobdiff",
1575 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1576 hash_base=>$hash, hash_parent_base=>$parent,
1577 file_name=>$diff{'file'}),
1578 -class => "list"}, esc_html($diff{'file'}));
1579 } else { # only mode changed
1580 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1581 hash_base=>$hash, file_name=>$diff{'file'}),
1582 -class => "list"}, esc_html($diff{'file'}));
1584 print "</td>\n" .
1585 "<td>$mode_chnge</td>\n" .
1586 "<td class=\"link\">" .
1587 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1588 hash_base=>$hash, file_name=>$diff{'file'})},
1589 "blob");
1590 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1591 print " | " .
1592 $cgi->a({-href => href(action=>"blobdiff",
1593 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1594 hash_base=>$hash, hash_parent_base=>$parent,
1595 file_name=>$diff{'file'})},
1596 "diff");
1598 print " | " .
1599 $cgi->a({-href => href(action=>"history",
1600 hash_base=>$hash, file_name=>$diff{'file'})},
1601 "history");
1602 print "</td>\n";
1604 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1605 my %status_name = ('R' => 'moved', 'C' => 'copied');
1606 my $nstatus = $status_name{$diff{'status'}};
1607 my $mode_chng = "";
1608 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1609 # mode also for directories, so we cannot use $to_mode_str
1610 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1612 print "<td>" .
1613 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1614 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1615 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1616 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1617 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1618 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1619 -class => "list"}, esc_html($diff{'from_file'})) .
1620 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1621 "<td class=\"link\">" .
1622 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1623 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1624 "blob");
1625 if ($diff{'to_id'} ne $diff{'from_id'}) {
1626 print " | " .
1627 $cgi->a({-href => href(action=>"blobdiff",
1628 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1629 hash_base=>$hash, hash_parent_base=>$parent,
1630 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1631 "diff");
1633 print "</td>\n";
1635 } # we should not encounter Unmerged (U) or Unknown (X) status
1636 print "</tr>\n";
1638 print "</table>\n";
1641 sub git_patchset_body {
1642 my ($fd, $difftree, $hash, $hash_parent) = @_;
1644 my $patch_idx = 0;
1645 my $in_header = 0;
1646 my $patch_found = 0;
1647 my $diffinfo;
1649 print "<div class=\"patchset\">\n";
1651 LINE:
1652 while (my $patch_line @$fd>) {
1653 chomp $patch_line;
1655 if ($patch_line =~ m/^diff /) { # "git diff" header
1656 # beginning of patch (in patchset)
1657 if ($patch_found) {
1658 # close previous patch
1659 print "</div>\n"; # class="patch"
1660 } else {
1661 # first patch in patchset
1662 $patch_found = 1;
1664 print "<div class=\"patch\">\n";
1666 if (ref($difftree->[$patch_idx]) eq "HASH") {
1667 $diffinfo = $difftree->[$patch_idx];
1668 } else {
1669 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1671 $patch_idx++;
1673 # for now, no extended header, hence we skip empty patches
1674 # companion to next LINE if $in_header;
1675 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1676 $in_header = 1;
1677 next LINE;
1680 if ($diffinfo->{'status'} eq "A") { # added
1681 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1682 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1683 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1684 $diffinfo->{'to_id'}) . "(new)" .
1685 "</div>\n"; # class="diff_info"
1687 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1688 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1689 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1690 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1691 $diffinfo->{'from_id'}) . "(deleted)" .
1692 "</div>\n"; # class="diff_info"
1694 } elsif ($diffinfo->{'status'} eq "R" || # renamed
1695 $diffinfo->{'status'} eq "C" || # copied
1696 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1697 print "<div class=\"diff_info\">" .
1698 file_type($diffinfo->{'from_mode'}) . ":" .
1699 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1700 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1701 $diffinfo->{'from_id'}) .
1702 " -> " .
1703 file_type($diffinfo->{'to_mode'}) . ":" .
1704 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1705 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1706 $diffinfo->{'to_id'});
1707 print "</div>\n"; # class="diff_info"
1709 } else { # modified, mode changed, ...
1710 print "<div class=\"diff_info\">" .
1711 file_type($diffinfo->{'from_mode'}) . ":" .
1712 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1713 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1714 $diffinfo->{'from_id'}) .
1715 " -> " .
1716 file_type($diffinfo->{'to_mode'}) . ":" .
1717 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1718 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1719 $diffinfo->{'to_id'});
1720 print "</div>\n"; # class="diff_info"
1723 #print "<div class=\"diff extended_header\">\n";
1724 $in_header = 1;
1725 next LINE;
1726 } # start of patch in patchset
1729 if ($in_header && $patch_line =~ m/^---/) {
1730 #print "</div>\n"; # class="diff extended_header"
1731 $in_header = 0;
1733 my $file = $diffinfo->{'from_file'};
1734 $file ||= $diffinfo->{'file'};
1735 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1736 hash=>$diffinfo->{'from_id'}, file_name=>$file),
1737 -class => "list"}, esc_html($file));
1738 $patch_line =~ s|a/.*$|a/$file|g;
1739 print "<div class=\"diff from_file\">$patch_line</div>\n";
1741 $patch_line = <$fd>;
1742 chomp $patch_line;
1744 #$patch_line =~ m/^+++/;
1745 $file = $diffinfo->{'to_file'};
1746 $file ||= $diffinfo->{'file'};
1747 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1748 hash=>$diffinfo->{'to_id'}, file_name=>$file),
1749 -class => "list"}, esc_html($file));
1750 $patch_line =~ s|b/.*|b/$file|g;
1751 print "<div class=\"diff to_file\">$patch_line</div>\n";
1753 next LINE;
1755 next LINE if $in_header;
1757 print format_diff_line($patch_line);
1759 print "</div>\n" if $patch_found; # class="patch"
1761 print "</div>\n"; # class="patchset"
1764 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1766 sub git_shortlog_body {
1767 # uses global variable $project
1768 my ($revlist, $from, $to, $refs, $extra) = @_;
1770 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1771 my $have_snapshot = (defined $ctype && defined $suffix);
1773 $from = 0 unless defined $from;
1774 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1776 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1777 my $alternate = 0;
1778 for (my $i = $from; $i <= $to; $i++) {
1779 my $commit = $revlist->[$i];
1780 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1781 my $ref = format_ref_marker($refs, $commit);
1782 my %co = parse_commit($commit);
1783 if ($alternate) {
1784 print "<tr class=\"dark\">\n";
1785 } else {
1786 print "<tr class=\"light\">\n";
1788 $alternate ^= 1;
1789 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1790 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1791 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1792 "<td>";
1793 print format_subject_html($co{'title'}, $co{'title_short'},
1794 href(action=>"commit", hash=>$commit), $ref);
1795 print "</td>\n" .
1796 "<td class=\"link\">" .
1797 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1798 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1799 if ($have_snapshot) {
1800 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1802 print "</td>\n" .
1803 "</tr>\n";
1805 if (defined $extra) {
1806 print "<tr>\n" .
1807 "<td colspan=\"4\">$extra</td>\n" .
1808 "</tr>\n";
1810 print "</table>\n";
1813 sub git_history_body {
1814 # Warning: assumes constant type (blob or tree) during history
1815 my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1817 print "<table class=\"history\" cellspacing=\"0\">\n";
1818 my $alternate = 0;
1819 while (my $line = <$fd>) {
1820 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1821 next;
1824 my $commit = $1;
1825 my %co = parse_commit($commit);
1826 if (!%co) {
1827 next;
1830 my $ref = format_ref_marker($refs, $commit);
1832 if ($alternate) {
1833 print "<tr class=\"dark\">\n";
1834 } else {
1835 print "<tr class=\"light\">\n";
1837 $alternate ^= 1;
1838 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1839 # shortlog uses chop_str($co{'author_name'}, 10)
1840 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1841 "<td>";
1842 # originally git_history used chop_str($co{'title'}, 50)
1843 print format_subject_html($co{'title'}, $co{'title_short'},
1844 href(action=>"commit", hash=>$commit), $ref);
1845 print "</td>\n" .
1846 "<td class=\"link\">" .
1847 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1848 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1849 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1851 if ($ftype eq 'blob') {
1852 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1853 my $blob_parent = git_get_hash_by_path($commit, $file_name);
1854 if (defined $blob_current && defined $blob_parent &&
1855 $blob_current ne $blob_parent) {
1856 print " | " .
1857 $cgi->a({-href => href(action=>"blobdiff",
1858 hash=>$blob_current, hash_parent=>$blob_parent,
1859 hash_base=>$hash_base, hash_parent_base=>$commit,
1860 file_name=>$file_name)},
1861 "diff to current");
1864 print "</td>\n" .
1865 "</tr>\n";
1867 if (defined $extra) {
1868 print "<tr>\n" .
1869 "<td colspan=\"4\">$extra</td>\n" .
1870 "</tr>\n";
1872 print "</table>\n";
1875 sub git_tags_body {
1876 # uses global variable $project
1877 my ($taglist, $from, $to, $extra) = @_;
1878 $from = 0 unless defined $from;
1879 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1881 print "<table class=\"tags\" cellspacing=\"0\">\n";
1882 my $alternate = 0;
1883 for (my $i = $from; $i <= $to; $i++) {
1884 my $entry = $taglist->[$i];
1885 my %tag = %$entry;
1886 my $comment_lines = $tag{'comment'};
1887 my $comment = shift @$comment_lines;
1888 my $comment_short;
1889 if (defined $comment) {
1890 $comment_short = chop_str($comment, 30, 5);
1892 if ($alternate) {
1893 print "<tr class=\"dark\">\n";
1894 } else {
1895 print "<tr class=\"light\">\n";
1897 $alternate ^= 1;
1898 print "<td><i>$tag{'age'}</i></td>\n" .
1899 "<td>" .
1900 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1901 -class => "list name"}, esc_html($tag{'name'})) .
1902 "</td>\n" .
1903 "<td>";
1904 if (defined $comment) {
1905 print format_subject_html($comment, $comment_short,
1906 href(action=>"tag", hash=>$tag{'id'}));
1908 print "</td>\n" .
1909 "<td class=\"selflink\">";
1910 if ($tag{'type'} eq "tag") {
1911 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1912 } else {
1913 print "&nbsp;";
1915 print "</td>\n" .
1916 "<td class=\"link\">" . " | " .
1917 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1918 if ($tag{'reftype'} eq "commit") {
1919 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1920 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1921 } elsif ($tag{'reftype'} eq "blob") {
1922 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1924 print "</td>\n" .
1925 "</tr>";
1927 if (defined $extra) {
1928 print "<tr>\n" .
1929 "<td colspan=\"5\">$extra</td>\n" .
1930 "</tr>\n";
1932 print "</table>\n";
1935 sub git_heads_body {
1936 # uses global variable $project
1937 my ($taglist, $head, $from, $to, $extra) = @_;
1938 $from = 0 unless defined $from;
1939 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1941 print "<table class=\"heads\" cellspacing=\"0\">\n";
1942 my $alternate = 0;
1943 for (my $i = $from; $i <= $to; $i++) {
1944 my $entry = $taglist->[$i];
1945 my %tag = %$entry;
1946 my $curr = $tag{'id'} eq $head;
1947 if ($alternate) {
1948 print "<tr class=\"dark\">\n";
1949 } else {
1950 print "<tr class=\"light\">\n";
1952 $alternate ^= 1;
1953 print "<td><i>$tag{'age'}</i></td>\n" .
1954 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1955 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1956 -class => "list name"},esc_html($tag{'name'})) .
1957 "</td>\n" .
1958 "<td class=\"link\">" .
1959 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1960 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1961 "</td>\n" .
1962 "</tr>";
1964 if (defined $extra) {
1965 print "<tr>\n" .
1966 "<td colspan=\"3\">$extra</td>\n" .
1967 "</tr>\n";
1969 print "</table>\n";
1972 ## ======================================================================
1973 ## ======================================================================
1974 ## actions
1976 sub git_project_list {
1977 my $order = $cgi->param('o');
1978 if (defined $order && $order !~ m/project|descr|owner|age/) {
1979 die_error(undef, "Unknown order parameter");
1982 my @list = git_get_projects_list();
1983 my @projects;
1984 if (!@list) {
1985 die_error(undef, "No projects found");
1987 foreach my $pr (@list) {
1988 my $head = git_get_head_hash($pr->{'path'});
1989 if (!defined $head) {
1990 next;
1992 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1993 my %co = parse_commit($head);
1994 if (!%co) {
1995 next;
1997 $pr->{'commit'} = \%co;
1998 if (!defined $pr->{'descr'}) {
1999 my $descr = git_get_project_description($pr->{'path'}) || "";
2000 $pr->{'descr'} = chop_str($descr, 25, 5);
2002 if (!defined $pr->{'owner'}) {
2003 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2005 push @projects, $pr;
2008 git_header_html();
2009 if (-f $home_text) {
2010 print "<div class=\"index_include\">\n";
2011 open (my $fd, $home_text);
2012 print <$fd>;
2013 close $fd;
2014 print "</div>\n";
2016 print "<table class=\"project_list\">\n" .
2017 "<tr>\n";
2018 $order ||= "project";
2019 if ($order eq "project") {
2020 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2021 print "<th>Project</th>\n";
2022 } else {
2023 print "<th>" .
2024 $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
2025 -class => "header"}, "Project") .
2026 "</th>\n";
2028 if ($order eq "descr") {
2029 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2030 print "<th>Description</th>\n";
2031 } else {
2032 print "<th>" .
2033 $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
2034 -class => "header"}, "Description") .
2035 "</th>\n";
2037 if ($order eq "owner") {
2038 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2039 print "<th>Owner</th>\n";
2040 } else {
2041 print "<th>" .
2042 $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
2043 -class => "header"}, "Owner") .
2044 "</th>\n";
2046 if ($order eq "age") {
2047 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2048 print "<th>Last Change</th>\n";
2049 } else {
2050 print "<th>" .
2051 $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
2052 -class => "header"}, "Last Change") .
2053 "</th>\n";
2055 print "<th></th>\n" .
2056 "</tr>\n";
2057 my $alternate = 0;
2058 foreach my $pr (@projects) {
2059 if ($alternate) {
2060 print "<tr class=\"dark\">\n";
2061 } else {
2062 print "<tr class=\"light\">\n";
2064 $alternate ^= 1;
2065 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2066 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2067 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2068 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2069 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2070 $pr->{'commit'}{'age_string'} . "</td>\n" .
2071 "<td class=\"link\">" .
2072 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2073 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2074 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2075 "</td>\n" .
2076 "</tr>\n";
2078 print "</table>\n";
2079 git_footer_html();
2082 sub git_summary {
2083 my $descr = git_get_project_description($project) || "none";
2084 my $head = git_get_head_hash($project);
2085 my %co = parse_commit($head);
2086 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2088 my $owner = git_get_project_owner($project);
2090 my $refs = git_get_references();
2091 git_header_html();
2092 git_print_page_nav('summary','', $head);
2094 print "<div class=\"title\">&nbsp;</div>\n";
2095 print "<table cellspacing=\"0\">\n" .
2096 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2097 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2098 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2099 # use per project git URL list in $projectroot/$project/cloneurl
2100 # or make project git URL from git base URL and project name
2101 my $url_tag = "URL";
2102 my @url_list = git_get_project_url_list($project);
2103 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2104 foreach my $git_url (@url_list) {
2105 next unless $git_url;
2106 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2107 $url_tag = "";
2109 print "</table>\n";
2111 open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
2112 or die_error(undef, "Open git-rev-list failed");
2113 my @revlist = map { chomp; $_ } <$fd>;
2114 close $fd;
2115 git_print_header_div('shortlog');
2116 git_shortlog_body(\@revlist, 0, 15, $refs,
2117 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2119 my $taglist = git_get_refs_list("refs/tags");
2120 if (defined @$taglist) {
2121 git_print_header_div('tags');
2122 git_tags_body($taglist, 0, 15,
2123 $cgi->a({-href => href(action=>"tags")}, "..."));
2126 my $headlist = git_get_refs_list("refs/heads");
2127 if (defined @$headlist) {
2128 git_print_header_div('heads');
2129 git_heads_body($headlist, $head, 0, 15,
2130 $cgi->a({-href => href(action=>"heads")}, "..."));
2133 git_footer_html();
2136 sub git_tag {
2137 my $head = git_get_head_hash($project);
2138 git_header_html();
2139 git_print_page_nav('','', $head,undef,$head);
2140 my %tag = parse_tag($hash);
2141 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2142 print "<div class=\"title_text\">\n" .
2143 "<table cellspacing=\"0\">\n" .
2144 "<tr>\n" .
2145 "<td>object</td>\n" .
2146 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2147 $tag{'object'}) . "</td>\n" .
2148 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2149 $tag{'type'}) . "</td>\n" .
2150 "</tr>\n";
2151 if (defined($tag{'author'})) {
2152 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2153 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2154 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2155 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2156 "</td></tr>\n";
2158 print "</table>\n\n" .
2159 "</div>\n";
2160 print "<div class=\"page_body\">";
2161 my $comment = $tag{'comment'};
2162 foreach my $line (@$comment) {
2163 print esc_html($line) . "<br/>\n";
2165 print "</div>\n";
2166 git_footer_html();
2169 sub git_blame2 {
2170 my $fd;
2171 my $ftype;
2173 if (!gitweb_check_feature('blame')) {
2174 die_error('403 Permission denied', "Permission denied");
2176 die_error('404 Not Found', "File name not defined") if (!$file_name);
2177 $hash_base ||= git_get_head_hash($project);
2178 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2179 my %co = parse_commit($hash_base)
2180 or die_error(undef, "Reading commit failed");
2181 if (!defined $hash) {
2182 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2183 or die_error(undef, "Error looking up file");
2185 $ftype = git_get_type($hash);
2186 if ($ftype !~ "blob") {
2187 die_error("400 Bad Request", "Object is not a blob");
2189 open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2190 or die_error(undef, "Open git-blame failed");
2191 git_header_html();
2192 my $formats_nav =
2193 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2194 "blob") .
2195 " | " .
2196 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2197 "head");
2198 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2199 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2200 git_print_page_path($file_name, $ftype, $hash_base);
2201 my @rev_color = (qw(light2 dark2));
2202 my $num_colors = scalar(@rev_color);
2203 my $current_color = 0;
2204 my $last_rev;
2205 print <<HTML;
2206 <div class="page_body">
2207 <table class="blame">
2208 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2209 HTML
2210 while (<$fd>) {
2211 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2212 my $full_rev = $1;
2213 my $rev = substr($full_rev, 0, 8);
2214 my $lineno = $2;
2215 my $data = $3;
2217 if (!defined $last_rev) {
2218 $last_rev = $full_rev;
2219 } elsif ($last_rev ne $full_rev) {
2220 $last_rev = $full_rev;
2221 $current_color = ++$current_color % $num_colors;
2223 print "<tr class=\"$rev_color[$current_color]\">\n";
2224 print "<td class=\"sha1\">" .
2225 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2226 esc_html($rev)) . "</td>\n";
2227 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2228 esc_html($lineno) . "</a></td>\n";
2229 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2230 print "</tr>\n";
2232 print "</table>\n";
2233 print "</div>";
2234 close $fd
2235 or print "Reading blob failed\n";
2236 git_footer_html();
2239 sub git_blame {
2240 my $fd;
2242 if (!gitweb_check_feature('blame')) {
2243 die_error('403 Permission denied', "Permission denied");
2245 die_error('404 Not Found', "File name not defined") if (!$file_name);
2246 $hash_base ||= git_get_head_hash($project);
2247 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2248 my %co = parse_commit($hash_base)
2249 or die_error(undef, "Reading commit failed");
2250 if (!defined $hash) {
2251 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2252 or die_error(undef, "Error lookup file");
2254 open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2255 or die_error(undef, "Open git-annotate failed");
2256 git_header_html();
2257 my $formats_nav =
2258 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2259 "blob") .
2260 " | " .
2261 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2262 "head");
2263 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2264 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2265 git_print_page_path($file_name, 'blob', $hash_base);
2266 print "<div class=\"page_body\">\n";
2267 print <<HTML;
2268 <table class="blame">
2269 <tr>
2270 <th>Commit</th>
2271 <th>Age</th>
2272 <th>Author</th>
2273 <th>Line</th>
2274 <th>Data</th>
2275 </tr>
2276 HTML
2277 my @line_class = (qw(light dark));
2278 my $line_class_len = scalar (@line_class);
2279 my $line_class_num = $#line_class;
2280 while (my $line = <$fd>) {
2281 my $long_rev;
2282 my $short_rev;
2283 my $author;
2284 my $time;
2285 my $lineno;
2286 my $data;
2287 my $age;
2288 my $age_str;
2289 my $age_class;
2291 chomp $line;
2292 $line_class_num = ($line_class_num + 1) % $line_class_len;
2294 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
2295 $long_rev = $1;
2296 $author = $2;
2297 $time = $3;
2298 $lineno = $4;
2299 $data = $5;
2300 } else {
2301 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2302 next;
2304 $short_rev = substr ($long_rev, 0, 8);
2305 $age = time () - $time;
2306 $age_str = age_string ($age);
2307 $age_str =~ s/ /&nbsp;/g;
2308 $age_class = age_class($age);
2309 $author = esc_html ($author);
2310 $author =~ s/ /&nbsp;/g;
2312 $data = untabify($data);
2313 $data = esc_html ($data);
2315 print <<HTML;
2316 <tr class="$line_class[$line_class_num]">
2317 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2318 <td class="$age_class">$age_str</td>
2319 <td>$author</td>
2320 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2321 <td class="pre">$data</td>
2322 </tr>
2323 HTML
2324 } # while (my $line = <$fd>)
2325 print "</table>\n\n";
2326 close $fd
2327 or print "Reading blob failed.\n";
2328 print "</div>";
2329 git_footer_html();
2332 sub git_tags {
2333 my $head = git_get_head_hash($project);
2334 git_header_html();
2335 git_print_page_nav('','', $head,undef,$head);
2336 git_print_header_div('summary', $project);
2338 my $taglist = git_get_refs_list("refs/tags");
2339 if (defined @$taglist) {
2340 git_tags_body($taglist);
2342 git_footer_html();
2345 sub git_heads {
2346 my $head = git_get_head_hash($project);
2347 git_header_html();
2348 git_print_page_nav('','', $head,undef,$head);
2349 git_print_header_div('summary', $project);
2351 my $taglist = git_get_refs_list("refs/heads");
2352 if (defined @$taglist) {
2353 git_heads_body($taglist, $head);
2355 git_footer_html();
2358 sub git_blob_plain {
2359 if (!defined $hash) {
2360 if (defined $file_name) {
2361 my $base = $hash_base || git_get_head_hash($project);
2362 $hash = git_get_hash_by_path($base, $file_name, "blob")
2363 or die_error(undef, "Error lookup file");
2364 } else {
2365 die_error(undef, "No file name defined");
2368 my $type = shift;
2369 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2370 or die_error(undef, "Couldn't cat $file_name, $hash");
2372 $type ||= blob_mimetype($fd, $file_name);
2374 # save as filename, even when no $file_name is given
2375 my $save_as = "$hash";
2376 if (defined $file_name) {
2377 $save_as = $file_name;
2378 } elsif ($type =~ m/^text\//) {
2379 $save_as .= '.txt';
2382 print $cgi->header(-type => "$type",
2383 -content_disposition => "inline; filename=\"$save_as\"");
2384 undef $/;
2385 binmode STDOUT, ':raw';
2386 print <$fd>;
2387 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2388 $/ = "\n";
2389 close $fd;
2392 sub git_blob {
2393 if (!defined $hash) {
2394 if (defined $file_name) {
2395 my $base = $hash_base || git_get_head_hash($project);
2396 $hash = git_get_hash_by_path($base, $file_name, "blob")
2397 or die_error(undef, "Error lookup file");
2398 } else {
2399 die_error(undef, "No file name defined");
2402 my $have_blame = gitweb_check_feature('blame');
2403 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2404 or die_error(undef, "Couldn't cat $file_name, $hash");
2405 my $mimetype = blob_mimetype($fd, $file_name);
2406 if ($mimetype !~ m/^text\//) {
2407 close $fd;
2408 return git_blob_plain($mimetype);
2410 git_header_html();
2411 my $formats_nav = '';
2412 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2413 if (defined $file_name) {
2414 if ($have_blame) {
2415 $formats_nav .=
2416 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2417 hash=>$hash, file_name=>$file_name)},
2418 "blame") .
2419 " | ";
2421 $formats_nav .=
2422 $cgi->a({-href => href(action=>"blob_plain",
2423 hash=>$hash, file_name=>$file_name)},
2424 "plain") .
2425 " | " .
2426 $cgi->a({-href => href(action=>"blob",
2427 hash_base=>"HEAD", file_name=>$file_name)},
2428 "head");
2429 } else {
2430 $formats_nav .=
2431 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2433 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2434 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2435 } else {
2436 print "<div class=\"page_nav\">\n" .
2437 "<br/><br/></div>\n" .
2438 "<div class=\"title\">$hash</div>\n";
2440 git_print_page_path($file_name, "blob", $hash_base);
2441 print "<div class=\"page_body\">\n";
2442 my $nr;
2443 while (my $line = <$fd>) {
2444 chomp $line;
2445 $nr++;
2446 $line = untabify($line);
2447 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2448 $nr, $nr, $nr, esc_html($line);
2450 close $fd
2451 or print "Reading blob failed.\n";
2452 print "</div>";
2453 git_footer_html();
2456 sub git_tree {
2457 if (!defined $hash) {
2458 $hash = git_get_head_hash($project);
2459 if (defined $file_name) {
2460 my $base = $hash_base || $hash;
2461 $hash = git_get_hash_by_path($base, $file_name, "tree");
2463 if (!defined $hash_base) {
2464 $hash_base = $hash;
2467 $/ = "\0";
2468 open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2469 or die_error(undef, "Open git-ls-tree failed");
2470 my @entries = map { chomp; $_ } <$fd>;
2471 close $fd or die_error(undef, "Reading tree failed");
2472 $/ = "\n";
2474 my $refs = git_get_references();
2475 my $ref = format_ref_marker($refs, $hash_base);
2476 git_header_html();
2477 my %base_key = ();
2478 my $base = "";
2479 my $have_blame = gitweb_check_feature('blame');
2480 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2481 $base_key{hash_base} = $hash_base;
2482 git_print_page_nav('tree','', $hash_base);
2483 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2484 } else {
2485 print "<div class=\"page_nav\">\n";
2486 print "<br/><br/></div>\n";
2487 print "<div class=\"title\">$hash</div>\n";
2489 if (defined $file_name) {
2490 $base = esc_html("$file_name/");
2492 git_print_page_path($file_name, 'tree', $hash_base);
2493 print "<div class=\"page_body\">\n";
2494 print "<table cellspacing=\"0\">\n";
2495 my $alternate = 0;
2496 foreach my $line (@entries) {
2497 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2498 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2499 my $t_mode = $1;
2500 my $t_type = $2;
2501 my $t_hash = $3;
2502 my $t_name = validate_input($4);
2503 if ($alternate) {
2504 print "<tr class=\"dark\">\n";
2505 } else {
2506 print "<tr class=\"light\">\n";
2508 $alternate ^= 1;
2509 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2510 if ($t_type eq "blob") {
2511 print "<td class=\"list\">" .
2512 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2513 -class => "list"}, esc_html($t_name)) .
2514 "</td>\n" .
2515 "<td class=\"link\">" .
2516 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2517 "blob");
2518 if ($have_blame) {
2519 print " | " .
2520 $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2521 "blame");
2523 print " | " .
2524 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2525 hash=>$t_hash, file_name=>"$base$t_name")},
2526 "history") .
2527 " | " .
2528 $cgi->a({-href => href(action=>"blob_plain",
2529 hash=>$t_hash, file_name=>"$base$t_name")},
2530 "raw") .
2531 "</td>\n";
2532 } elsif ($t_type eq "tree") {
2533 print "<td class=\"list\">" .
2534 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2535 esc_html($t_name)) .
2536 "</td>\n" .
2537 "<td class=\"link\">" .
2538 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2539 "tree") .
2540 " | " .
2541 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2542 "history") .
2543 "</td>\n";
2545 print "</tr>\n";
2547 print "</table>\n" .
2548 "</div>";
2549 git_footer_html();
2552 sub git_snapshot {
2554 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2555 my $have_snapshot = (defined $ctype && defined $suffix);
2556 if (!$have_snapshot) {
2557 die_error('403 Permission denied', "Permission denied");
2560 if (!defined $hash) {
2561 $hash = git_get_head_hash($project);
2564 my $filename = basename($project) . "-$hash.tar.$suffix";
2566 print $cgi->header(-type => 'application/x-tar',
2567 -content_encoding => $ctype,
2568 -content_disposition => "inline; filename=\"$filename\"",
2569 -status => '200 OK');
2571 open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2572 die_error(undef, "Execute git-tar-tree failed.");
2573 binmode STDOUT, ':raw';
2574 print <$fd>;
2575 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2576 close $fd;
2580 sub git_log {
2581 my $head = git_get_head_hash($project);
2582 if (!defined $hash) {
2583 $hash = $head;
2585 if (!defined $page) {
2586 $page = 0;
2588 my $refs = git_get_references();
2590 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2591 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2592 or die_error(undef, "Open git-rev-list failed");
2593 my @revlist = map { chomp; $_ } <$fd>;
2594 close $fd;
2596 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2598 git_header_html();
2599 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2601 if (!@revlist) {
2602 my %co = parse_commit($hash);
2604 git_print_header_div('summary', $project);
2605 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2607 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2608 my $commit = $revlist[$i];
2609 my $ref = format_ref_marker($refs, $commit);
2610 my %co = parse_commit($commit);
2611 next if !%co;
2612 my %ad = parse_date($co{'author_epoch'});
2613 git_print_header_div('commit',
2614 "<span class=\"age\">$co{'age_string'}</span>" .
2615 esc_html($co{'title'}) . $ref,
2616 $commit);
2617 print "<div class=\"title_text\">\n" .
2618 "<div class=\"log_link\">\n" .
2619 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2620 " | " .
2621 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2622 "<br/>\n" .
2623 "</div>\n" .
2624 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2625 "</div>\n";
2627 print "<div class=\"log_body\">\n";
2628 git_print_simplified_log($co{'comment'});
2629 print "</div>\n";
2631 git_footer_html();
2634 sub git_commit {
2635 my %co = parse_commit($hash);
2636 if (!%co) {
2637 die_error(undef, "Unknown commit object");
2639 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2640 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2642 my $parent = $co{'parent'};
2643 if (!defined $parent) {
2644 $parent = "--root";
2646 open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2647 or die_error(undef, "Open git-diff-tree failed");
2648 my @difftree = map { chomp; $_ } <$fd>;
2649 close $fd or die_error(undef, "Reading git-diff-tree failed");
2651 # non-textual hash id's can be cached
2652 my $expires;
2653 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2654 $expires = "+1d";
2656 my $refs = git_get_references();
2657 my $ref = format_ref_marker($refs, $co{'id'});
2659 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2660 my $have_snapshot = (defined $ctype && defined $suffix);
2662 my $formats_nav = '';
2663 if (defined $file_name && defined $co{'parent'}) {
2664 my $parent = $co{'parent'};
2665 $formats_nav .=
2666 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2667 "blame");
2669 git_header_html(undef, $expires);
2670 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2671 $hash, $co{'tree'}, $hash,
2672 $formats_nav);
2674 if (defined $co{'parent'}) {
2675 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2676 } else {
2677 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2679 print "<div class=\"title_text\">\n" .
2680 "<table cellspacing=\"0\">\n";
2681 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2682 "<tr>" .
2683 "<td></td><td> $ad{'rfc2822'}";
2684 if ($ad{'hour_local'} < 6) {
2685 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2686 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2687 } else {
2688 printf(" (%02d:%02d %s)",
2689 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2691 print "</td>" .
2692 "</tr>\n";
2693 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2694 print "<tr><td></td><td> $cd{'rfc2822'}" .
2695 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2696 "</td></tr>\n";
2697 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2698 print "<tr>" .
2699 "<td>tree</td>" .
2700 "<td class=\"sha1\">" .
2701 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2702 class => "list"}, $co{'tree'}) .
2703 "</td>" .
2704 "<td class=\"link\">" .
2705 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2706 "tree");
2707 if ($have_snapshot) {
2708 print " | " .
2709 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2711 print "</td>" .
2712 "</tr>\n";
2713 my $parents = $co{'parents'};
2714 foreach my $par (@$parents) {
2715 print "<tr>" .
2716 "<td>parent</td>" .
2717 "<td class=\"sha1\">" .
2718 $cgi->a({-href => href(action=>"commit", hash=>$par),
2719 class => "list"}, $par) .
2720 "</td>" .
2721 "<td class=\"link\">" .
2722 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2723 " | " .
2724 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2725 "</td>" .
2726 "</tr>\n";
2728 print "</table>".
2729 "</div>\n";
2731 print "<div class=\"page_body\">\n";
2732 git_print_log($co{'comment'});
2733 print "</div>\n";
2735 git_difftree_body(\@difftree, $hash, $parent);
2737 git_footer_html();
2740 sub git_blobdiff {
2741 my $format = shift || 'html';
2743 my $fd;
2744 my @difftree;
2745 my %diffinfo;
2746 my $expires;
2748 # preparing $fd and %diffinfo for git_patchset_body
2749 # new style URI
2750 if (defined $hash_base && defined $hash_parent_base) {
2751 if (defined $file_name) {
2752 # read raw output
2753 open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C', $hash_parent_base, $hash_base,
2754 "--", $file_name
2755 or die_error(undef, "Open git-diff-tree failed");
2756 @difftree = map { chomp; $_ } <$fd>;
2757 close $fd
2758 or die_error(undef, "Reading git-diff-tree failed");
2759 @difftree
2760 or die_error('404 Not Found', "Blob diff not found");
2762 } elsif (defined $hash) { # try to find filename from $hash
2763 if ($hash !~ /[0-9a-fA-F]{40}/) {
2764 $hash = git_to_hash($hash);
2767 # read filtered raw output
2768 open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C', $hash_parent_base, $hash_base
2769 or die_error(undef, "Open git-diff-tree failed");
2770 @difftree =
2771 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
2772 # $hash == to_id
2773 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
2774 map { chomp; $_ } <$fd>;
2775 close $fd
2776 or die_error(undef, "Reading git-diff-tree failed");
2777 @difftree
2778 or die_error('404 Not Found', "Blob diff not found");
2780 } else {
2781 die_error('404 Not Found', "Missing one of the blob diff parameters");
2784 if (@difftree > 1) {
2785 die_error('404 Not Found', "Ambiguous blob diff specification");
2788 %diffinfo = parse_difftree_raw_line($difftree[0]);
2789 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
2790 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
2792 $hash_parent ||= $diffinfo{'from_id'};
2793 $hash ||= $diffinfo{'to_id'};
2795 # non-textual hash id's can be cached
2796 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
2797 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
2798 $expires = '+1d';
2801 # open patch output
2802 open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-M', '-C', $hash_parent_base, $hash_base,
2803 "--", $file_name
2804 or die_error(undef, "Open git-diff-tree failed");
2807 # old/legacy style URI
2808 if (!%diffinfo && # if new style URI failed
2809 defined $hash && defined $hash_parent) {
2810 # fake git-diff-tree raw output
2811 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
2812 $diffinfo{'from_id'} = $hash_parent;
2813 $diffinfo{'to_id'} = $hash;
2814 if (defined $file_name) {
2815 if (defined $file_parent) {
2816 $diffinfo{'status'} = '2';
2817 $diffinfo{'from_file'} = $file_parent;
2818 $diffinfo{'to_file'} = $file_name;
2819 } else { # assume not renamed
2820 $diffinfo{'status'} = '1';
2821 $diffinfo{'from_file'} = $file_name;
2822 $diffinfo{'to_file'} = $file_name;
2824 } else { # no filename given
2825 $diffinfo{'status'} = '2';
2826 $diffinfo{'from_file'} = $hash_parent;
2827 $diffinfo{'to_file'} = $hash;
2830 # non-textual hash id's can be cached
2831 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
2832 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
2833 $expires = '+1d';
2836 # open patch output
2837 #open $fd, "-|", $GIT, "diff", '-p', $hash_parent, $hash
2838 open $fd, "-|", $GIT, "diff", '-p', $hash, $hash_parent
2839 or die_error(undef, "Open git-diff failed");
2840 } else {
2841 die_error('404 Not Found', "Missing one of the blob diff parameters")
2842 unless %diffinfo;
2845 # header
2846 if ($format eq 'html') {
2847 my $formats_nav =
2848 $cgi->a({-href => href(action=>"blobdiff_plain",
2849 hash=>$hash, hash_parent=>$hash_parent,
2850 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
2851 file_name=>$file_name, file_parent=>$file_parent)},
2852 "plain");
2853 git_header_html(undef, $expires);
2854 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2855 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2856 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2857 } else {
2858 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
2859 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
2861 if (defined $file_name) {
2862 git_print_page_path($file_name, "blob", $hash_base);
2863 } else {
2864 print "<div class=\"page_path\"></div>\n";
2867 } elsif ($format eq 'plain') {
2868 print $cgi->header(
2869 -type => 'text/plain',
2870 -charset => 'utf-8',
2871 -expires => $expires,
2872 -content_disposition => qq(inline; filename="${file_name}.patch"));
2874 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2876 } else {
2877 die_error(undef, "Unknown blobdiff format");
2880 # patch
2881 if ($format eq 'html') {
2882 print "<div class=\"page_body\">\n";
2884 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
2885 close $fd;
2887 print "</div>\n"; # class="page_body"
2888 git_footer_html();
2890 } else {
2891 while (my $line = <$fd>) {
2892 $line =~ s!a/($hash|$hash_parent)!a/$diffinfo{'from_file'}!g;
2893 $line =~ s!b/($hash|$hash_parent)!b/$diffinfo{'to_file'}!g;
2895 print $line;
2897 last if $line =~ m!^\+\+\+!;
2899 local $/ = undef;
2900 print <$fd>;
2901 close $fd;
2905 sub git_blobdiff_plain {
2906 git_blobdiff('plain');
2909 sub git_commitdiff {
2910 my $format = shift || 'html';
2911 my %co = parse_commit($hash);
2912 if (!%co) {
2913 die_error(undef, "Unknown commit object");
2915 if (!defined $hash_parent) {
2916 $hash_parent = $co{'parent'} || '--root';
2919 # read commitdiff
2920 my $fd;
2921 my @difftree;
2922 if ($format eq 'html') {
2923 open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C',
2924 "--patch-with-raw", "--full-index", $hash_parent, $hash
2925 or die_error(undef, "Open git-diff-tree failed");
2927 while (chomp(my $line = <$fd>)) {
2928 # empty line ends raw part of diff-tree output
2929 last unless $line;
2930 push @difftree, $line;
2933 } elsif ($format eq 'plain') {
2934 open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-B', $hash_parent, $hash
2935 or die_error(undef, "Open git-diff-tree failed");
2937 } else {
2938 die_error(undef, "Unknown commitdiff format");
2941 # non-textual hash id's can be cached
2942 my $expires;
2943 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2944 $expires = "+1d";
2947 # write commit message
2948 if ($format eq 'html') {
2949 my $refs = git_get_references();
2950 my $ref = format_ref_marker($refs, $co{'id'});
2951 my $formats_nav =
2952 $cgi->a({-href => href(action=>"commitdiff_plain",
2953 hash=>$hash, hash_parent=>$hash_parent)},
2954 "plain");
2956 git_header_html(undef, $expires);
2957 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2958 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2959 print "<div class=\"page_body\">\n";
2960 print "<div class=\"log\">\n";
2961 git_print_simplified_log($co{'comment'}, 1); # skip title
2962 print "</div>\n"; # class="log"
2964 } elsif ($format eq 'plain') {
2965 my $refs = git_get_references("tags");
2966 my $tagname = git_get_rev_name_tags($hash);
2967 my $filename = basename($project) . "-$hash.patch";
2969 print $cgi->header(
2970 -type => 'text/plain',
2971 -charset => 'utf-8',
2972 -expires => $expires,
2973 -content_disposition => qq(inline; filename="$filename"));
2974 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2975 print <<TEXT;
2976 From: $co{'author'}
2977 Date: $ad{'rfc2822'} ($ad{'tz_local'})
2978 Subject: $co{'title'}
2979 TEXT
2980 print "X-Git-Tag: $tagname\n" if $tagname;
2981 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2983 foreach my $line (@{$co{'comment'}}) {
2984 print "$line\n";
2986 print "---\n\n";
2989 # write patch
2990 if ($format eq 'html') {
2991 #git_difftree_body(\@difftree, $hash, $hash_parent);
2992 #print "<br/>\n";
2994 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
2995 close $fd;
2996 print "</div>\n"; # class="page_body"
2997 git_footer_html();
2999 } elsif ($format eq 'plain') {
3000 local $/ = undef;
3001 print <$fd>;
3002 close $fd
3003 or print "Reading git-diff-tree failed\n";
3007 sub git_commitdiff_plain {
3008 git_commitdiff('plain');
3011 sub git_history {
3012 if (!defined $hash_base) {
3013 $hash_base = git_get_head_hash($project);
3015 my $ftype;
3016 my %co = parse_commit($hash_base);
3017 if (!%co) {
3018 die_error(undef, "Unknown commit object");
3020 my $refs = git_get_references();
3021 git_header_html();
3022 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
3023 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3024 if (!defined $hash && defined $file_name) {
3025 $hash = git_get_hash_by_path($hash_base, $file_name);
3027 if (defined $hash) {
3028 $ftype = git_get_type($hash);
3030 git_print_page_path($file_name, $ftype, $hash_base);
3032 open my $fd, "-|",
3033 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
3034 git_history_body($fd, $refs, $hash_base, $ftype);
3036 close $fd;
3037 git_footer_html();
3040 sub git_search {
3041 if (!defined $searchtext) {
3042 die_error(undef, "Text field empty");
3044 if (!defined $hash) {
3045 $hash = git_get_head_hash($project);
3047 my %co = parse_commit($hash);
3048 if (!%co) {
3049 die_error(undef, "Unknown commit object");
3051 # pickaxe may take all resources of your box and run for several minutes
3052 # with every query - so decide by yourself how public you make this feature :)
3053 my $commit_search = 1;
3054 my $author_search = 0;
3055 my $committer_search = 0;
3056 my $pickaxe_search = 0;
3057 if ($searchtext =~ s/^author\\://i) {
3058 $author_search = 1;
3059 } elsif ($searchtext =~ s/^committer\\://i) {
3060 $committer_search = 1;
3061 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3062 $commit_search = 0;
3063 $pickaxe_search = 1;
3065 git_header_html();
3066 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3067 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3069 print "<table cellspacing=\"0\">\n";
3070 my $alternate = 0;
3071 if ($commit_search) {
3072 $/ = "\0";
3073 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
3074 while (my $commit_text = <$fd>) {
3075 if (!grep m/$searchtext/i, $commit_text) {
3076 next;
3078 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3079 next;
3081 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3082 next;
3084 my @commit_lines = split "\n", $commit_text;
3085 my %co = parse_commit(undef, \@commit_lines);
3086 if (!%co) {
3087 next;
3089 if ($alternate) {
3090 print "<tr class=\"dark\">\n";
3091 } else {
3092 print "<tr class=\"light\">\n";
3094 $alternate ^= 1;
3095 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3096 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3097 "<td>" .
3098 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3099 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3100 my $comment = $co{'comment'};
3101 foreach my $line (@$comment) {
3102 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3103 my $lead = esc_html($1) || "";
3104 $lead = chop_str($lead, 30, 10);
3105 my $match = esc_html($2) || "";
3106 my $trail = esc_html($3) || "";
3107 $trail = chop_str($trail, 30, 10);
3108 my $text = "$lead<span class=\"match\">$match</span>$trail";
3109 print chop_str($text, 80, 5) . "<br/>\n";
3112 print "</td>\n" .
3113 "<td class=\"link\">" .
3114 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3115 " | " .
3116 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3117 print "</td>\n" .
3118 "</tr>\n";
3120 close $fd;
3123 if ($pickaxe_search) {
3124 $/ = "\n";
3125 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
3126 undef %co;
3127 my @files;
3128 while (my $line = <$fd>) {
3129 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3130 my %set;
3131 $set{'file'} = $6;
3132 $set{'from_id'} = $3;
3133 $set{'to_id'} = $4;
3134 $set{'id'} = $set{'to_id'};
3135 if ($set{'id'} =~ m/0{40}/) {
3136 $set{'id'} = $set{'from_id'};
3138 if ($set{'id'} =~ m/0{40}/) {
3139 next;
3141 push @files, \%set;
3142 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3143 if (%co) {
3144 if ($alternate) {
3145 print "<tr class=\"dark\">\n";
3146 } else {
3147 print "<tr class=\"light\">\n";
3149 $alternate ^= 1;
3150 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3151 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3152 "<td>" .
3153 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3154 -class => "list subject"},
3155 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3156 while (my $setref = shift @files) {
3157 my %set = %$setref;
3158 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3159 hash=>$set{'id'}, file_name=>$set{'file'}),
3160 -class => "list"},
3161 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3162 "<br/>\n";
3164 print "</td>\n" .
3165 "<td class=\"link\">" .
3166 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3167 " | " .
3168 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3169 print "</td>\n" .
3170 "</tr>\n";
3172 %co = parse_commit($1);
3175 close $fd;
3177 print "</table>\n";
3178 git_footer_html();
3181 sub git_shortlog {
3182 my $head = git_get_head_hash($project);
3183 if (!defined $hash) {
3184 $hash = $head;
3186 if (!defined $page) {
3187 $page = 0;
3189 my $refs = git_get_references();
3191 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3192 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
3193 or die_error(undef, "Open git-rev-list failed");
3194 my @revlist = map { chomp; $_ } <$fd>;
3195 close $fd;
3197 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3198 my $next_link = '';
3199 if ($#revlist >= (100 * ($page+1)-1)) {
3200 $next_link =
3201 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3202 -title => "Alt-n"}, "next");
3206 git_header_html();
3207 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3208 git_print_header_div('summary', $project);
3210 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3212 git_footer_html();
3215 ## ......................................................................
3216 ## feeds (RSS, OPML)
3218 sub git_rss {
3219 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3220 open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
3221 or die_error(undef, "Open git-rev-list failed");
3222 my @revlist = map { chomp; $_ } <$fd>;
3223 close $fd or die_error(undef, "Reading git-rev-list failed");
3224 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3225 print <<XML;
3226 <?xml version="1.0" encoding="utf-8"?>
3227 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3228 <channel>
3229 <title>$project $my_uri $my_url</title>
3230 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3231 <description>$project log</description>
3232 <language>en</language>
3235 for (my $i = 0; $i <= $#revlist; $i++) {
3236 my $commit = $revlist[$i];
3237 my %co = parse_commit($commit);
3238 # we read 150, we always show 30 and the ones more recent than 48 hours
3239 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3240 last;
3242 my %cd = parse_date($co{'committer_epoch'});
3243 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
3244 my @difftree = map { chomp; $_ } <$fd>;
3245 close $fd or next;
3246 print "<item>\n" .
3247 "<title>" .
3248 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3249 "</title>\n" .
3250 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3251 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3252 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3253 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3254 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3255 "<content:encoded>" .
3256 "<![CDATA[\n";
3257 my $comment = $co{'comment'};
3258 foreach my $line (@$comment) {
3259 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3260 print "$line<br/>\n";
3262 print "<br/>\n";
3263 foreach my $line (@difftree) {
3264 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3265 next;
3267 my $file = validate_input(unquote($7));
3268 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3269 print "$file<br/>\n";
3271 print "]]>\n" .
3272 "</content:encoded>\n" .
3273 "</item>\n";
3275 print "</channel></rss>";
3278 sub git_opml {
3279 my @list = git_get_projects_list();
3281 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3282 print <<XML;
3283 <?xml version="1.0" encoding="utf-8"?>
3284 <opml version="1.0">
3285 <head>
3286 <title>$site_name Git OPML Export</title>
3287 </head>
3288 <body>
3289 <outline text="git RSS feeds">
3292 foreach my $pr (@list) {
3293 my %proj = %$pr;
3294 my $head = git_get_head_hash($proj{'path'});
3295 if (!defined $head) {
3296 next;
3298 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3299 my %co = parse_commit($head);
3300 if (!%co) {
3301 next;
3304 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3305 my $rss = "$my_url?p=$proj{'path'};a=rss";
3306 my $html = "$my_url?p=$proj{'path'};a=summary";
3307 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3309 print <<XML;
3310 </outline>
3311 </body>
3312 </opml>