gitweb: Whitespace cleanup: realign, reindent
[git/repo.git] / gitweb / gitweb.perl
blob63dc477eab81b9bea57e44910e34c2953de8af85
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 # location for temporary files needed for diffs
35 our $git_temp = "/tmp/gitweb";
37 # target of the home link on top of all pages
38 our $home_link = $my_uri || "/";
40 # string of the home link on top of all pages
41 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
43 # name of your site or organization to appear in page titles
44 # replace this with something more descriptive for clearer bookmarks
45 our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
47 # html text to include at home page
48 our $home_text = "++GITWEB_HOMETEXT++";
50 # URI of default stylesheet
51 our $stylesheet = "++GITWEB_CSS++";
52 # URI of GIT logo
53 our $logo = "++GITWEB_LOGO++";
55 # source of projects list
56 our $projects_list = "++GITWEB_LIST++";
58 # list of git base URLs used for URL to where fetch project from,
59 # i.e. full URL is "$git_base_url/$project"
60 our @git_base_url_list = ("++GITWEB_BASE_URL++");
62 # default blob_plain mimetype and default charset for text/plain blob
63 our $default_blob_plain_mimetype = 'text/plain';
64 our $default_text_plain_charset = undef;
66 # file to use for guessing MIME types before trying /etc/mime.types
67 # (relative to the current git repository)
68 our $mimetypes_file = undef;
70 # You define site-wide feature defaults here; override them with
71 # $GITWEB_CONFIG as necessary.
72 our %feature = (
73 # feature => {'sub' => feature-sub, 'override' => allow-override, 'default' => [ default options...]
74 # if feature is overridable, feature-sub will be called with default options;
75 # return value indicates if to enable specified feature
77 'blame' => {
78 'sub' => \&feature_blame,
79 'override' => 0,
80 'default' => [0]},
82 'snapshot' => {
83 'sub' => \&feature_snapshot,
84 'override' => 0,
85 # => [content-encoding, suffix, program]
86 'default' => ['x-gzip', 'gz', 'gzip']},
89 sub gitweb_check_feature {
90 my ($name) = @_;
91 return undef unless exists $feature{$name};
92 my ($sub, $override, @defaults) = (
93 $feature{$name}{'sub'},
94 $feature{$name}{'override'},
95 @{$feature{$name}{'default'}});
96 if (!$override) { return @defaults; }
97 return $sub->(@defaults);
100 # To enable system wide have in $GITWEB_CONFIG
101 # $feature{'blame'}{'default'} = [1];
102 # To have project specific config enable override in $GITWEB_CONFIG
103 # $feature{'blame'}{'override'} = 1;
104 # and in project config gitweb.blame = 0|1;
106 sub feature_blame {
107 my ($val) = git_get_project_config('blame', '--bool');
109 if ($val eq 'true') {
110 return 1;
111 } elsif ($val eq 'false') {
112 return 0;
115 return $_[0];
118 # To disable system wide have in $GITWEB_CONFIG
119 # $feature{'snapshot'}{'default'} = [undef];
120 # To have project specific config enable override in $GITWEB_CONFIG
121 # $feature{'blame'}{'override'} = 1;
122 # and in project config gitweb.snapshot = none|gzip|bzip2
124 sub feature_snapshot {
125 my ($ctype, $suffix, $command) = @_;
127 my ($val) = git_get_project_config('snapshot');
129 if ($val eq 'gzip') {
130 return ('x-gzip', 'gz', 'gzip');
131 } elsif ($val eq 'bzip2') {
132 return ('x-bzip2', 'bz2', 'bzip2');
133 } elsif ($val eq 'none') {
134 return ();
137 return ($ctype, $suffix, $command);
140 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
141 require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
143 # version of the core git binary
144 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
146 $projects_list ||= $projectroot;
147 if (! -d $git_temp) {
148 mkdir($git_temp, 0700) || die_error(undef, "Couldn't mkdir $git_temp");
151 # ======================================================================
152 # input validation and dispatch
153 our $action = $cgi->param('a');
154 if (defined $action) {
155 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
156 die_error(undef, "Invalid action parameter");
158 # action which does not check rest of parameters
159 if ($action eq "opml") {
160 git_opml();
161 exit;
165 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
166 if (defined $project) {
167 $project =~ s|^/||;
168 $project =~ s|/$||;
169 $project = undef unless $project;
171 if (defined $project) {
172 if (!validate_input($project)) {
173 die_error(undef, "Invalid project parameter");
175 if (!(-d "$projectroot/$project")) {
176 die_error(undef, "No such directory");
178 if (!(-e "$projectroot/$project/HEAD")) {
179 die_error(undef, "No such project");
181 $ENV{'GIT_DIR'} = "$projectroot/$project";
182 } else {
183 git_project_list();
184 exit;
187 our $file_name = $cgi->param('f');
188 if (defined $file_name) {
189 if (!validate_input($file_name)) {
190 die_error(undef, "Invalid file parameter");
194 our $file_parent = $cgi->param('fp');
195 if (defined $file_parent) {
196 if (!validate_input($file_parent)) {
197 die_error(undef, "Invalid file parent parameter");
201 our $hash = $cgi->param('h');
202 if (defined $hash) {
203 if (!validate_input($hash)) {
204 die_error(undef, "Invalid hash parameter");
208 our $hash_parent = $cgi->param('hp');
209 if (defined $hash_parent) {
210 if (!validate_input($hash_parent)) {
211 die_error(undef, "Invalid hash parent parameter");
215 our $hash_base = $cgi->param('hb');
216 if (defined $hash_base) {
217 if (!validate_input($hash_base)) {
218 die_error(undef, "Invalid hash base parameter");
222 our $page = $cgi->param('pg');
223 if (defined $page) {
224 if ($page =~ m/[^0-9]$/) {
225 die_error(undef, "Invalid page parameter");
229 our $searchtext = $cgi->param('s');
230 if (defined $searchtext) {
231 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
232 die_error(undef, "Invalid search parameter");
234 $searchtext = quotemeta $searchtext;
237 # dispatch
238 my %actions = (
239 "blame" => \&git_blame2,
240 "blobdiff" => \&git_blobdiff,
241 "blobdiff_plain" => \&git_blobdiff_plain,
242 "blob" => \&git_blob,
243 "blob_plain" => \&git_blob_plain,
244 "commitdiff" => \&git_commitdiff,
245 "commitdiff_plain" => \&git_commitdiff_plain,
246 "commit" => \&git_commit,
247 "heads" => \&git_heads,
248 "history" => \&git_history,
249 "log" => \&git_log,
250 "rss" => \&git_rss,
251 "search" => \&git_search,
252 "shortlog" => \&git_shortlog,
253 "summary" => \&git_summary,
254 "tag" => \&git_tag,
255 "tags" => \&git_tags,
256 "tree" => \&git_tree,
257 "snapshot" => \&git_snapshot,
260 $action = 'summary' if (!defined($action));
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 %mapping = (
272 action => "a",
273 project => "p",
274 file_name => "f",
275 file_parent => "fp",
276 hash => "h",
277 hash_parent => "hp",
278 hash_base => "hb",
279 page => "pg",
280 searchtext => "s",
283 my %params = @_;
284 $params{"project"} ||= $project;
286 my $href = "$my_uri?";
287 $href .= esc_param( join(";",
288 map {
289 "$mapping{$_}=$params{$_}" if defined $params{$_}
290 } keys %params
291 ) );
293 return $href;
297 ## ======================================================================
298 ## validation, quoting/unquoting and escaping
300 sub validate_input {
301 my $input = shift;
303 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
304 return $input;
306 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
307 return undef;
309 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
310 return undef;
312 return $input;
315 # quote unsafe chars, but keep the slash, even when it's not
316 # correct, but quoted slashes look too horrible in bookmarks
317 sub esc_param {
318 my $str = shift;
319 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
320 $str =~ s/\+/%2B/g;
321 $str =~ s/ /\+/g;
322 return $str;
325 # replace invalid utf8 character with SUBSTITUTION sequence
326 sub esc_html {
327 my $str = shift;
328 $str = decode("utf8", $str, Encode::FB_DEFAULT);
329 $str = escapeHTML($str);
330 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
331 return $str;
334 # git may return quoted and escaped filenames
335 sub unquote {
336 my $str = shift;
337 if ($str =~ m/^"(.*)"$/) {
338 $str = $1;
339 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
341 return $str;
344 # escape tabs (convert tabs to spaces)
345 sub untabify {
346 my $line = shift;
348 while ((my $pos = index($line, "\t")) != -1) {
349 if (my $count = (8 - ($pos % 8))) {
350 my $spaces = ' ' x $count;
351 $line =~ s/\t/$spaces/;
355 return $line;
358 ## ----------------------------------------------------------------------
359 ## HTML aware string manipulation
361 sub chop_str {
362 my $str = shift;
363 my $len = shift;
364 my $add_len = shift || 10;
366 # allow only $len chars, but don't cut a word if it would fit in $add_len
367 # if it doesn't fit, cut it if it's still longer than the dots we would add
368 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
369 my $body = $1;
370 my $tail = $2;
371 if (length($tail) > 4) {
372 $tail = " ...";
373 $body =~ s/&[^;]*$//; # remove chopped character entities
375 return "$body$tail";
378 ## ----------------------------------------------------------------------
379 ## functions returning short strings
381 # CSS class for given age value (in seconds)
382 sub age_class {
383 my $age = shift;
385 if ($age < 60*60*2) {
386 return "age0";
387 } elsif ($age < 60*60*24*2) {
388 return "age1";
389 } else {
390 return "age2";
394 # convert age in seconds to "nn units ago" string
395 sub age_string {
396 my $age = shift;
397 my $age_str;
399 if ($age > 60*60*24*365*2) {
400 $age_str = (int $age/60/60/24/365);
401 $age_str .= " years ago";
402 } elsif ($age > 60*60*24*(365/12)*2) {
403 $age_str = int $age/60/60/24/(365/12);
404 $age_str .= " months ago";
405 } elsif ($age > 60*60*24*7*2) {
406 $age_str = int $age/60/60/24/7;
407 $age_str .= " weeks ago";
408 } elsif ($age > 60*60*24*2) {
409 $age_str = int $age/60/60/24;
410 $age_str .= " days ago";
411 } elsif ($age > 60*60*2) {
412 $age_str = int $age/60/60;
413 $age_str .= " hours ago";
414 } elsif ($age > 60*2) {
415 $age_str = int $age/60;
416 $age_str .= " min ago";
417 } elsif ($age > 2) {
418 $age_str = int $age;
419 $age_str .= " sec ago";
420 } else {
421 $age_str .= " right now";
423 return $age_str;
426 # convert file mode in octal to symbolic file mode string
427 sub mode_str {
428 my $mode = oct shift;
430 if (S_ISDIR($mode & S_IFMT)) {
431 return 'drwxr-xr-x';
432 } elsif (S_ISLNK($mode)) {
433 return 'lrwxrwxrwx';
434 } elsif (S_ISREG($mode)) {
435 # git cares only about the executable bit
436 if ($mode & S_IXUSR) {
437 return '-rwxr-xr-x';
438 } else {
439 return '-rw-r--r--';
441 } else {
442 return '----------';
446 # convert file mode in octal to file type string
447 sub file_type {
448 my $mode = oct shift;
450 if (S_ISDIR($mode & S_IFMT)) {
451 return "directory";
452 } elsif (S_ISLNK($mode)) {
453 return "symlink";
454 } elsif (S_ISREG($mode)) {
455 return "file";
456 } else {
457 return "unknown";
461 ## ----------------------------------------------------------------------
462 ## functions returning short HTML fragments, or transforming HTML fragments
463 ## which don't beling to other sections
465 # format line of commit message or tag comment
466 sub format_log_line_html {
467 my $line = shift;
469 $line = esc_html($line);
470 $line =~ s/ /&nbsp;/g;
471 if ($line =~ m/([0-9a-fA-F]{40})/) {
472 my $hash_text = $1;
473 if (git_get_type($hash_text) eq "commit") {
474 my $link =
475 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
476 -class => "text"}, $hash_text);
477 $line =~ s/$hash_text/$link/;
480 return $line;
483 # format marker of refs pointing to given object
484 sub format_ref_marker {
485 my ($refs, $id) = @_;
486 my $markers = '';
488 if (defined $refs->{$id}) {
489 foreach my $ref (@{$refs->{$id}}) {
490 my ($type, $name) = qw();
491 # e.g. tags/v2.6.11 or heads/next
492 if ($ref =~ m!^(.*?)s?/(.*)$!) {
493 $type = $1;
494 $name = $2;
495 } else {
496 $type = "ref";
497 $name = $ref;
500 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
504 if ($markers) {
505 return ' <span class="refs">'. $markers . '</span>';
506 } else {
507 return "";
511 # format, perhaps shortened and with markers, title line
512 sub format_subject_html {
513 my ($long, $short, $href, $extra) = @_;
514 $extra = '' unless defined($extra);
516 if (length($short) < length($long)) {
517 return $cgi->a({-href => $href, -class => "list subject",
518 -title => $long},
519 esc_html($short) . $extra);
520 } else {
521 return $cgi->a({-href => $href, -class => "list subject"},
522 esc_html($long) . $extra);
526 ## ----------------------------------------------------------------------
527 ## git utility subroutines, invoking git commands
529 # get HEAD ref of given project as hash
530 sub git_get_head_hash {
531 my $project = shift;
532 my $oENV = $ENV{'GIT_DIR'};
533 my $retval = undef;
534 $ENV{'GIT_DIR'} = "$projectroot/$project";
535 if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
536 my $head = <$fd>;
537 close $fd;
538 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
539 $retval = $1;
542 if (defined $oENV) {
543 $ENV{'GIT_DIR'} = $oENV;
545 return $retval;
548 # get type of given object
549 sub git_get_type {
550 my $hash = shift;
552 open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
553 my $type = <$fd>;
554 close $fd or return;
555 chomp $type;
556 return $type;
559 sub git_get_project_config {
560 my ($key, $type) = @_;
562 return unless ($key);
563 $key =~ s/^gitweb\.//;
564 return if ($key =~ m/\W/);
566 my @x = ($GIT, 'repo-config');
567 if (defined $type) { push @x, $type; }
568 push @x, "--get";
569 push @x, "gitweb.$key";
570 my $val = qx(@x);
571 chomp $val;
572 return ($val);
575 # get hash of given path at given ref
576 sub git_get_hash_by_path {
577 my $base = shift;
578 my $path = shift || return undef;
580 my $tree = $base;
582 open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
583 or die_error(undef, "Open git-ls-tree failed");
584 my $line = <$fd>;
585 close $fd or return undef;
587 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
588 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
589 return $3;
592 ## ......................................................................
593 ## git utility functions, directly accessing git repository
595 # assumes that PATH is not symref
596 sub git_get_hash_by_ref {
597 my $path = shift;
599 open my $fd, "$projectroot/$path" or return undef;
600 my $head = <$fd>;
601 close $fd;
602 chomp $head;
603 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
604 return $head;
608 sub git_get_project_description {
609 my $path = shift;
611 open my $fd, "$projectroot/$path/description" or return undef;
612 my $descr = <$fd>;
613 close $fd;
614 chomp $descr;
615 return $descr;
618 sub git_get_project_url_list {
619 my $path = shift;
621 open my $fd, "$projectroot/$path/cloneurl" or return undef;
622 my @git_project_url_list = map { chomp; $_ } <$fd>;
623 close $fd;
625 return wantarray ? @git_project_url_list : \@git_project_url_list;
628 sub git_get_projects_list {
629 my @list;
631 if (-d $projects_list) {
632 # search in directory
633 my $dir = $projects_list;
634 opendir my ($dh), $dir or return undef;
635 while (my $dir = readdir($dh)) {
636 if (-e "$projectroot/$dir/HEAD") {
637 my $pr = {
638 path => $dir,
640 push @list, $pr
643 closedir($dh);
644 } elsif (-f $projects_list) {
645 # read from file(url-encoded):
646 # 'git%2Fgit.git Linus+Torvalds'
647 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
648 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
649 open my ($fd), $projects_list or return undef;
650 while (my $line = <$fd>) {
651 chomp $line;
652 my ($path, $owner) = split ' ', $line;
653 $path = unescape($path);
654 $owner = unescape($owner);
655 if (!defined $path) {
656 next;
658 if (-e "$projectroot/$path/HEAD") {
659 my $pr = {
660 path => $path,
661 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
663 push @list, $pr
666 close $fd;
668 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
669 return @list;
672 sub git_get_project_owner {
673 my $project = shift;
674 my $owner;
676 return undef unless $project;
678 # read from file (url-encoded):
679 # 'git%2Fgit.git Linus+Torvalds'
680 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
681 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
682 if (-f $projects_list) {
683 open (my $fd , $projects_list);
684 while (my $line = <$fd>) {
685 chomp $line;
686 my ($pr, $ow) = split ' ', $line;
687 $pr = unescape($pr);
688 $ow = unescape($ow);
689 if ($pr eq $project) {
690 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
691 last;
694 close $fd;
696 if (!defined $owner) {
697 $owner = get_file_owner("$projectroot/$project");
700 return $owner;
703 sub git_get_references {
704 my $type = shift || "";
705 my %refs;
706 my $fd;
707 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
708 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
709 if (-f "$projectroot/$project/info/refs") {
710 open $fd, "$projectroot/$project/info/refs"
711 or return;
712 } else {
713 open $fd, "-|", $GIT, "ls-remote", "."
714 or return;
717 while (my $line = <$fd>) {
718 chomp $line;
719 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
720 if (defined $refs{$1}) {
721 push @{$refs{$1}}, $2;
722 } else {
723 $refs{$1} = [ $2 ];
727 close $fd or return;
728 return \%refs;
731 ## ----------------------------------------------------------------------
732 ## parse to hash functions
734 sub parse_date {
735 my $epoch = shift;
736 my $tz = shift || "-0000";
738 my %date;
739 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
740 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
741 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
742 $date{'hour'} = $hour;
743 $date{'minute'} = $min;
744 $date{'mday'} = $mday;
745 $date{'day'} = $days[$wday];
746 $date{'month'} = $months[$mon];
747 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
748 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
749 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
750 $mday, $months[$mon], $hour ,$min;
752 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
753 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
754 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
755 $date{'hour_local'} = $hour;
756 $date{'minute_local'} = $min;
757 $date{'tz_local'} = $tz;
758 return %date;
761 sub parse_tag {
762 my $tag_id = shift;
763 my %tag;
764 my @comment;
766 open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
767 $tag{'id'} = $tag_id;
768 while (my $line = <$fd>) {
769 chomp $line;
770 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
771 $tag{'object'} = $1;
772 } elsif ($line =~ m/^type (.+)$/) {
773 $tag{'type'} = $1;
774 } elsif ($line =~ m/^tag (.+)$/) {
775 $tag{'name'} = $1;
776 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
777 $tag{'author'} = $1;
778 $tag{'epoch'} = $2;
779 $tag{'tz'} = $3;
780 } elsif ($line =~ m/--BEGIN/) {
781 push @comment, $line;
782 last;
783 } elsif ($line eq "") {
784 last;
787 push @comment, <$fd>;
788 $tag{'comment'} = \@comment;
789 close $fd or return;
790 if (!defined $tag{'name'}) {
791 return
793 return %tag
796 sub parse_commit {
797 my $commit_id = shift;
798 my $commit_text = shift;
800 my @commit_lines;
801 my %co;
803 if (defined $commit_text) {
804 @commit_lines = @$commit_text;
805 } else {
806 $/ = "\0";
807 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id
808 or return;
809 @commit_lines = split '\n', <$fd>;
810 close $fd or return;
811 $/ = "\n";
812 pop @commit_lines;
814 my $header = shift @commit_lines;
815 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
816 return;
818 ($co{'id'}, my @parents) = split ' ', $header;
819 $co{'parents'} = \@parents;
820 $co{'parent'} = $parents[0];
821 while (my $line = shift @commit_lines) {
822 last if $line eq "\n";
823 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
824 $co{'tree'} = $1;
825 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
826 $co{'author'} = $1;
827 $co{'author_epoch'} = $2;
828 $co{'author_tz'} = $3;
829 if ($co{'author'} =~ m/^([^<]+) </) {
830 $co{'author_name'} = $1;
831 } else {
832 $co{'author_name'} = $co{'author'};
834 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
835 $co{'committer'} = $1;
836 $co{'committer_epoch'} = $2;
837 $co{'committer_tz'} = $3;
838 $co{'committer_name'} = $co{'committer'};
839 $co{'committer_name'} =~ s/ <.*//;
842 if (!defined $co{'tree'}) {
843 return;
846 foreach my $title (@commit_lines) {
847 $title =~ s/^ //;
848 if ($title ne "") {
849 $co{'title'} = chop_str($title, 80, 5);
850 # remove leading stuff of merges to make the interesting part visible
851 if (length($title) > 50) {
852 $title =~ s/^Automatic //;
853 $title =~ s/^merge (of|with) /Merge ... /i;
854 if (length($title) > 50) {
855 $title =~ s/(http|rsync):\/\///;
857 if (length($title) > 50) {
858 $title =~ s/(master|www|rsync)\.//;
860 if (length($title) > 50) {
861 $title =~ s/kernel.org:?//;
863 if (length($title) > 50) {
864 $title =~ s/\/pub\/scm//;
867 $co{'title_short'} = chop_str($title, 50, 5);
868 last;
871 # remove added spaces
872 foreach my $line (@commit_lines) {
873 $line =~ s/^ //;
875 $co{'comment'} = \@commit_lines;
877 my $age = time - $co{'committer_epoch'};
878 $co{'age'} = $age;
879 $co{'age_string'} = age_string($age);
880 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
881 if ($age > 60*60*24*7*2) {
882 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
883 $co{'age_string_age'} = $co{'age_string'};
884 } else {
885 $co{'age_string_date'} = $co{'age_string'};
886 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
888 return %co;
891 # parse ref from ref_file, given by ref_id, with given type
892 sub parse_ref {
893 my $ref_file = shift;
894 my $ref_id = shift;
895 my $type = shift || git_get_type($ref_id);
896 my %ref_item;
898 $ref_item{'type'} = $type;
899 $ref_item{'id'} = $ref_id;
900 $ref_item{'epoch'} = 0;
901 $ref_item{'age'} = "unknown";
902 if ($type eq "tag") {
903 my %tag = parse_tag($ref_id);
904 $ref_item{'comment'} = $tag{'comment'};
905 if ($tag{'type'} eq "commit") {
906 my %co = parse_commit($tag{'object'});
907 $ref_item{'epoch'} = $co{'committer_epoch'};
908 $ref_item{'age'} = $co{'age_string'};
909 } elsif (defined($tag{'epoch'})) {
910 my $age = time - $tag{'epoch'};
911 $ref_item{'epoch'} = $tag{'epoch'};
912 $ref_item{'age'} = age_string($age);
914 $ref_item{'reftype'} = $tag{'type'};
915 $ref_item{'name'} = $tag{'name'};
916 $ref_item{'refid'} = $tag{'object'};
917 } elsif ($type eq "commit"){
918 my %co = parse_commit($ref_id);
919 $ref_item{'reftype'} = "commit";
920 $ref_item{'name'} = $ref_file;
921 $ref_item{'title'} = $co{'title'};
922 $ref_item{'refid'} = $ref_id;
923 $ref_item{'epoch'} = $co{'committer_epoch'};
924 $ref_item{'age'} = $co{'age_string'};
925 } else {
926 $ref_item{'reftype'} = $type;
927 $ref_item{'name'} = $ref_file;
928 $ref_item{'refid'} = $ref_id;
931 return %ref_item;
934 # parse line of git-diff-tree "raw" output
935 sub parse_difftree_raw_line {
936 my $line = shift;
937 my %res;
939 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
940 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
941 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
942 $res{'from_mode'} = $1;
943 $res{'to_mode'} = $2;
944 $res{'from_id'} = $3;
945 $res{'to_id'} = $4;
946 $res{'status'} = $5;
947 $res{'similarity'} = $6;
948 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
949 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
950 } else {
951 $res{'file'} = unquote($7);
954 # 'c512b523472485aef4fff9e57b229d9d243c967f'
955 #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
956 # $res{'commit'} = $1;
959 return wantarray ? %res : \%res;
962 ## ......................................................................
963 ## parse to array of hashes functions
965 sub git_get_refs_list {
966 my $ref_dir = shift;
967 my @reflist;
969 my @refs;
970 my $pfxlen = length("$projectroot/$project/$ref_dir");
971 File::Find::find(sub {
972 return if (/^\./);
973 if (-f $_) {
974 push @refs, substr($File::Find::name, $pfxlen + 1);
976 }, "$projectroot/$project/$ref_dir");
978 foreach my $ref_file (@refs) {
979 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
980 my $type = git_get_type($ref_id) || next;
981 my %ref_item = parse_ref($ref_file, $ref_id, $type);
983 push @reflist, \%ref_item;
985 # sort refs by age
986 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
987 return \@reflist;
990 ## ----------------------------------------------------------------------
991 ## filesystem-related functions
993 sub get_file_owner {
994 my $path = shift;
996 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
997 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
998 if (!defined $gcos) {
999 return undef;
1001 my $owner = $gcos;
1002 $owner =~ s/[,;].*$//;
1003 return decode("utf8", $owner, Encode::FB_DEFAULT);
1006 ## ......................................................................
1007 ## mimetype related functions
1009 sub mimetype_guess_file {
1010 my $filename = shift;
1011 my $mimemap = shift;
1012 -r $mimemap or return undef;
1014 my %mimemap;
1015 open(MIME, $mimemap) or return undef;
1016 while (<MIME>) {
1017 next if m/^#/; # skip comments
1018 my ($mime, $exts) = split(/\t+/);
1019 if (defined $exts) {
1020 my @exts = split(/\s+/, $exts);
1021 foreach my $ext (@exts) {
1022 $mimemap{$ext} = $mime;
1026 close(MIME);
1028 $filename =~ /\.(.*?)$/;
1029 return $mimemap{$1};
1032 sub mimetype_guess {
1033 my $filename = shift;
1034 my $mime;
1035 $filename =~ /\./ or return undef;
1037 if ($mimetypes_file) {
1038 my $file = $mimetypes_file;
1039 if ($file !~ m!^/!) { # if it is relative path
1040 # it is relative to project
1041 $file = "$projectroot/$project/$file";
1043 $mime = mimetype_guess_file($filename, $file);
1045 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1046 return $mime;
1049 sub blob_mimetype {
1050 my $fd = shift;
1051 my $filename = shift;
1053 if ($filename) {
1054 my $mime = mimetype_guess($filename);
1055 $mime and return $mime;
1058 # just in case
1059 return $default_blob_plain_mimetype unless $fd;
1061 if (-T $fd) {
1062 return 'text/plain' .
1063 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1064 } elsif (! $filename) {
1065 return 'application/octet-stream';
1066 } elsif ($filename =~ m/\.png$/i) {
1067 return 'image/png';
1068 } elsif ($filename =~ m/\.gif$/i) {
1069 return 'image/gif';
1070 } elsif ($filename =~ m/\.jpe?g$/i) {
1071 return 'image/jpeg';
1072 } else {
1073 return 'application/octet-stream';
1077 ## ======================================================================
1078 ## functions printing HTML: header, footer, error page
1080 sub git_header_html {
1081 my $status = shift || "200 OK";
1082 my $expires = shift;
1084 my $title = "$site_name git";
1085 if (defined $project) {
1086 $title .= " - $project";
1087 if (defined $action) {
1088 $title .= "/$action";
1089 if (defined $file_name) {
1090 $title .= " - $file_name";
1091 if ($action eq "tree" && $file_name !~ m|/$|) {
1092 $title .= "/";
1097 my $content_type;
1098 # require explicit support from the UA if we are to send the page as
1099 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1100 # we have to do this because MSIE sometimes globs '*/*', pretending to
1101 # support xhtml+xml but choking when it gets what it asked for.
1102 if (defined $cgi->http('HTTP_ACCEPT') &&
1103 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1104 $cgi->Accept('application/xhtml+xml') != 0) {
1105 $content_type = 'application/xhtml+xml';
1106 } else {
1107 $content_type = 'text/html';
1109 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1110 -status=> $status, -expires => $expires);
1111 print <<EOF;
1112 <?xml version="1.0" encoding="utf-8"?>
1113 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1114 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1115 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1116 <!-- git core binaries version $git_version -->
1117 <head>
1118 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1119 <meta name="generator" content="gitweb/$version git/$git_version"/>
1120 <meta name="robots" content="index, nofollow"/>
1121 <title>$title</title>
1122 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1124 if (defined $project) {
1125 printf('<link rel="alternate" title="%s log" '.
1126 'href="%s" type="application/rss+xml"/>'."\n",
1127 esc_param($project), href(action=>"rss"));
1130 print "</head>\n" .
1131 "<body>\n" .
1132 "<div class=\"page_header\">\n" .
1133 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1134 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1135 "</a>\n";
1136 print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1137 if (defined $project) {
1138 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1139 if (defined $action) {
1140 print " / $action";
1142 print "\n";
1143 if (!defined $searchtext) {
1144 $searchtext = "";
1146 my $search_hash;
1147 if (defined $hash_base) {
1148 $search_hash = $hash_base;
1149 } elsif (defined $hash) {
1150 $search_hash = $hash;
1151 } else {
1152 $search_hash = "HEAD";
1154 $cgi->param("a", "search");
1155 $cgi->param("h", $search_hash);
1156 print $cgi->startform(-method => "get", -action => $my_uri) .
1157 "<div class=\"search\">\n" .
1158 $cgi->hidden(-name => "p") . "\n" .
1159 $cgi->hidden(-name => "a") . "\n" .
1160 $cgi->hidden(-name => "h") . "\n" .
1161 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1162 "</div>" .
1163 $cgi->end_form() . "\n";
1165 print "</div>\n";
1168 sub git_footer_html {
1169 print "<div class=\"page_footer\">\n";
1170 if (defined $project) {
1171 my $descr = git_get_project_description($project);
1172 if (defined $descr) {
1173 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1175 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1176 } else {
1177 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1179 print "</div>\n" .
1180 "</body>\n" .
1181 "</html>";
1184 sub die_error {
1185 my $status = shift || "403 Forbidden";
1186 my $error = shift || "Malformed query, file missing or permission denied";
1188 git_header_html($status);
1189 print "<div class=\"page_body\">\n" .
1190 "<br/><br/>\n" .
1191 "$status - $error\n" .
1192 "<br/>\n" .
1193 "</div>\n";
1194 git_footer_html();
1195 exit;
1198 ## ----------------------------------------------------------------------
1199 ## functions printing or outputting HTML: navigation
1201 sub git_print_page_nav {
1202 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1203 $extra = '' if !defined $extra; # pager or formats
1205 my @navs = qw(summary shortlog log commit commitdiff tree);
1206 if ($suppress) {
1207 @navs = grep { $_ ne $suppress } @navs;
1210 my %arg = map { $_ => {action=>$_} } @navs;
1211 if (defined $head) {
1212 for (qw(commit commitdiff)) {
1213 $arg{$_}{hash} = $head;
1215 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1216 for (qw(shortlog log)) {
1217 $arg{$_}{hash} = $head;
1221 $arg{tree}{hash} = $treehead if defined $treehead;
1222 $arg{tree}{hash_base} = $treebase if defined $treebase;
1224 print "<div class=\"page_nav\">\n" .
1225 (join " | ",
1226 map { $_ eq $current ?
1227 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1228 } @navs);
1229 print "<br/>\n$extra<br/>\n" .
1230 "</div>\n";
1233 sub format_paging_nav {
1234 my ($action, $hash, $head, $page, $nrevs) = @_;
1235 my $paging_nav;
1238 if ($hash ne $head || $page) {
1239 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1240 } else {
1241 $paging_nav .= "HEAD";
1244 if ($page > 0) {
1245 $paging_nav .= " &sdot; " .
1246 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1247 -accesskey => "p", -title => "Alt-p"}, "prev");
1248 } else {
1249 $paging_nav .= " &sdot; prev";
1252 if ($nrevs >= (100 * ($page+1)-1)) {
1253 $paging_nav .= " &sdot; " .
1254 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1255 -accesskey => "n", -title => "Alt-n"}, "next");
1256 } else {
1257 $paging_nav .= " &sdot; next";
1260 return $paging_nav;
1263 ## ......................................................................
1264 ## functions printing or outputting HTML: div
1266 sub git_print_header_div {
1267 my ($action, $title, $hash, $hash_base) = @_;
1268 my %args = ();
1270 $args{action} = $action;
1271 $args{hash} = $hash if $hash;
1272 $args{hash_base} = $hash_base if $hash_base;
1274 print "<div class=\"header\">\n" .
1275 $cgi->a({-href => href(%args), -class => "title"},
1276 $title ? $title : $action) .
1277 "\n</div>\n";
1280 sub git_print_page_path {
1281 my $name = shift;
1282 my $type = shift;
1283 my $hb = shift;
1285 if (!defined $name) {
1286 print "<div class=\"page_path\">/</div>\n";
1287 } elsif (defined $type && $type eq 'blob') {
1288 print "<div class=\"page_path\">";
1289 if (defined $hb) {
1290 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1291 hash_base=>$hb)},
1292 esc_html($name));
1293 } else {
1294 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1295 esc_html($name));
1297 print "<br/></div>\n";
1298 } else {
1299 print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1303 sub git_print_log {
1304 my $log = shift;
1306 # remove leading empty lines
1307 while (defined $log->[0] && $log->[0] eq "") {
1308 shift @$log;
1311 # print log
1312 my $signoff = 0;
1313 my $empty = 0;
1314 foreach my $line (@$log) {
1315 # print only one empty line
1316 # do not print empty line after signoff
1317 if ($line eq "") {
1318 next if ($empty || $signoff);
1319 $empty = 1;
1320 } else {
1321 $empty = 0;
1323 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1324 $signoff = 1;
1325 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1326 } else {
1327 $signoff = 0;
1328 print format_log_line_html($line) . "<br/>\n";
1333 sub git_print_simplified_log {
1334 my $log = shift;
1335 my $remove_title = shift;
1337 shift @$log if $remove_title;
1338 # remove leading empty lines
1339 while (defined $log->[0] && $log->[0] eq "") {
1340 shift @$log;
1343 # simplify and print log
1344 my $empty = 0;
1345 foreach my $line (@$log) {
1346 # remove signoff lines
1347 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1348 next;
1350 # print only one empty line
1351 if ($line eq "") {
1352 next if $empty;
1353 $empty = 1;
1354 } else {
1355 $empty = 0;
1357 print format_log_line_html($line) . "<br/>\n";
1359 # end with single empty line
1360 print "<br/>\n" unless $empty;
1363 ## ......................................................................
1364 ## functions printing large fragments of HTML
1366 sub git_difftree_body {
1367 my ($difftree, $parent) = @_;
1369 print "<div class=\"list_head\">\n";
1370 if ($#{$difftree} > 10) {
1371 print(($#{$difftree} + 1) . " files changed:\n");
1373 print "</div>\n";
1375 print "<table class=\"diff_tree\">\n";
1376 my $alternate = 0;
1377 foreach my $line (@{$difftree}) {
1378 my %diff = parse_difftree_raw_line($line);
1380 if ($alternate) {
1381 print "<tr class=\"dark\">\n";
1382 } else {
1383 print "<tr class=\"light\">\n";
1385 $alternate ^= 1;
1387 my ($to_mode_oct, $to_mode_str, $to_file_type);
1388 my ($from_mode_oct, $from_mode_str, $from_file_type);
1389 if ($diff{'to_mode'} ne ('0' x 6)) {
1390 $to_mode_oct = oct $diff{'to_mode'};
1391 if (S_ISREG($to_mode_oct)) { # only for regular file
1392 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1394 $to_file_type = file_type($diff{'to_mode'});
1396 if ($diff{'from_mode'} ne ('0' x 6)) {
1397 $from_mode_oct = oct $diff{'from_mode'};
1398 if (S_ISREG($to_mode_oct)) { # only for regular file
1399 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1401 $from_file_type = file_type($diff{'from_mode'});
1404 if ($diff{'status'} eq "A") { # created
1405 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1406 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1407 $mode_chng .= "]</span>";
1408 print "<td>" .
1409 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1410 hash_base=>$hash, file_name=>$diff{'file'}),
1411 -class => "list"}, esc_html($diff{'file'})) .
1412 "</td>\n" .
1413 "<td>$mode_chng</td>\n" .
1414 "<td class=\"link\">" .
1415 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1416 hash_base=>$hash, file_name=>$diff{'file'})},
1417 "blob") .
1418 "</td>\n";
1420 } elsif ($diff{'status'} eq "D") { # deleted
1421 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1422 print "<td>" .
1423 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1424 hash_base=>$parent, file_name=>$diff{'file'}),
1425 -class => "list"}, esc_html($diff{'file'})) .
1426 "</td>\n" .
1427 "<td>$mode_chng</td>\n" .
1428 "<td class=\"link\">" .
1429 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1430 hash_base=>$parent, file_name=>$diff{'file'})},
1431 "blob") .
1432 " | " .
1433 $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1434 file_name=>$diff{'file'})},\
1435 "history") .
1436 "</td>\n";
1438 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1439 my $mode_chnge = "";
1440 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1441 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1442 if ($from_file_type != $to_file_type) {
1443 $mode_chnge .= " from $from_file_type to $to_file_type";
1445 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1446 if ($from_mode_str && $to_mode_str) {
1447 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1448 } elsif ($to_mode_str) {
1449 $mode_chnge .= " mode: $to_mode_str";
1452 $mode_chnge .= "]</span>\n";
1454 print "<td>";
1455 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1456 print $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1457 hash_base=>$hash, file_name=>$diff{'file'}),
1458 -class => "list"}, esc_html($diff{'file'}));
1459 } else { # only mode changed
1460 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1461 hash_base=>$hash, file_name=>$diff{'file'}),
1462 -class => "list"}, esc_html($diff{'file'}));
1464 print "</td>\n" .
1465 "<td>$mode_chnge</td>\n" .
1466 "<td class=\"link\">" .
1467 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1468 hash_base=>$hash, file_name=>$diff{'file'})},
1469 "blob");
1470 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1471 print " | " .
1472 $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1473 hash_base=>$hash, file_name=>$diff{'file'})},
1474 "diff");
1476 print " | " .
1477 $cgi->a({-href => href(action=>"history",
1478 hash_base=>$hash, file_name=>$diff{'file'})},
1479 "history");
1480 print "</td>\n";
1482 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1483 my %status_name = ('R' => 'moved', 'C' => 'copied');
1484 my $nstatus = $status_name{$diff{'status'}};
1485 my $mode_chng = "";
1486 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1487 # mode also for directories, so we cannot use $to_mode_str
1488 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1490 print "<td>" .
1491 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1492 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1493 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1494 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1495 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1496 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1497 -class => "list"}, esc_html($diff{'from_file'})) .
1498 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1499 "<td class=\"link\">" .
1500 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1501 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1502 "blob");
1503 if ($diff{'to_id'} ne $diff{'from_id'}) {
1504 print " | " .
1505 $cgi->a({-href => href(action=>"blobdiff", hash_base=>$hash,
1506 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1507 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1508 "diff");
1510 print "</td>\n";
1512 } # we should not encounter Unmerged (U) or Unknown (X) status
1513 print "</tr>\n";
1515 print "</table>\n";
1518 sub git_shortlog_body {
1519 # uses global variable $project
1520 my ($revlist, $from, $to, $refs, $extra) = @_;
1522 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1523 my $have_snapshot = (defined $ctype && defined $suffix);
1525 $from = 0 unless defined $from;
1526 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1528 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1529 my $alternate = 0;
1530 for (my $i = $from; $i <= $to; $i++) {
1531 my $commit = $revlist->[$i];
1532 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1533 my $ref = format_ref_marker($refs, $commit);
1534 my %co = parse_commit($commit);
1535 if ($alternate) {
1536 print "<tr class=\"dark\">\n";
1537 } else {
1538 print "<tr class=\"light\">\n";
1540 $alternate ^= 1;
1541 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1542 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1543 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1544 "<td>";
1545 print format_subject_html($co{'title'}, $co{'title_short'},
1546 href(action=>"commit", hash=>$commit), $ref);
1547 print "</td>\n" .
1548 "<td class=\"link\">" .
1549 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1550 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1551 if ($have_snapshot) {
1552 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1554 print "</td>\n" .
1555 "</tr>\n";
1557 if (defined $extra) {
1558 print "<tr>\n" .
1559 "<td colspan=\"4\">$extra</td>\n" .
1560 "</tr>\n";
1562 print "</table>\n";
1565 sub git_history_body {
1566 # Warning: assumes constant type (blob or tree) during history
1567 my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1569 print "<table class=\"history\" cellspacing=\"0\">\n";
1570 my $alternate = 0;
1571 while (my $line = <$fd>) {
1572 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1573 next;
1576 my $commit = $1;
1577 my %co = parse_commit($commit);
1578 if (!%co) {
1579 next;
1582 my $ref = format_ref_marker($refs, $commit);
1584 if ($alternate) {
1585 print "<tr class=\"dark\">\n";
1586 } else {
1587 print "<tr class=\"light\">\n";
1589 $alternate ^= 1;
1590 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1591 # shortlog uses chop_str($co{'author_name'}, 10)
1592 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1593 "<td>";
1594 # originally git_history used chop_str($co{'title'}, 50)
1595 print format_subject_html($co{'title'}, $co{'title_short'},
1596 href(action=>"commit", hash=>$commit), $ref);
1597 print "</td>\n" .
1598 "<td class=\"link\">" .
1599 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1600 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1601 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1603 if ($ftype eq 'blob') {
1604 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1605 my $blob_parent = git_get_hash_by_path($commit, $file_name);
1606 if (defined $blob_current && defined $blob_parent &&
1607 $blob_current ne $blob_parent) {
1608 print " | " .
1609 $cgi->a({-href => href(action=>"blobdiff", hash=>$blob_current, hash_parent=>$blob_parent,
1610 hash_base=>$commit, file_name=>$file_name)},
1611 "diff to current");
1614 print "</td>\n" .
1615 "</tr>\n";
1617 if (defined $extra) {
1618 print "<tr>\n" .
1619 "<td colspan=\"4\">$extra</td>\n" .
1620 "</tr>\n";
1622 print "</table>\n";
1625 sub git_tags_body {
1626 # uses global variable $project
1627 my ($taglist, $from, $to, $extra) = @_;
1628 $from = 0 unless defined $from;
1629 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1631 print "<table class=\"tags\" cellspacing=\"0\">\n";
1632 my $alternate = 0;
1633 for (my $i = $from; $i <= $to; $i++) {
1634 my $entry = $taglist->[$i];
1635 my %tag = %$entry;
1636 my $comment_lines = $tag{'comment'};
1637 my $comment = shift @$comment_lines;
1638 my $comment_short;
1639 if (defined $comment) {
1640 $comment_short = chop_str($comment, 30, 5);
1642 if ($alternate) {
1643 print "<tr class=\"dark\">\n";
1644 } else {
1645 print "<tr class=\"light\">\n";
1647 $alternate ^= 1;
1648 print "<td><i>$tag{'age'}</i></td>\n" .
1649 "<td>" .
1650 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1651 -class => "list name"}, esc_html($tag{'name'})) .
1652 "</td>\n" .
1653 "<td>";
1654 if (defined $comment) {
1655 print format_subject_html($comment, $comment_short,
1656 href(action=>"tag", hash=>$tag{'id'}));
1658 print "</td>\n" .
1659 "<td class=\"selflink\">";
1660 if ($tag{'type'} eq "tag") {
1661 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1662 } else {
1663 print "&nbsp;";
1665 print "</td>\n" .
1666 "<td class=\"link\">" . " | " .
1667 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1668 if ($tag{'reftype'} eq "commit") {
1669 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1670 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1671 } elsif ($tag{'reftype'} eq "blob") {
1672 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1674 print "</td>\n" .
1675 "</tr>";
1677 if (defined $extra) {
1678 print "<tr>\n" .
1679 "<td colspan=\"5\">$extra</td>\n" .
1680 "</tr>\n";
1682 print "</table>\n";
1685 sub git_heads_body {
1686 # uses global variable $project
1687 my ($taglist, $head, $from, $to, $extra) = @_;
1688 $from = 0 unless defined $from;
1689 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1691 print "<table class=\"heads\" cellspacing=\"0\">\n";
1692 my $alternate = 0;
1693 for (my $i = $from; $i <= $to; $i++) {
1694 my $entry = $taglist->[$i];
1695 my %tag = %$entry;
1696 my $curr = $tag{'id'} eq $head;
1697 if ($alternate) {
1698 print "<tr class=\"dark\">\n";
1699 } else {
1700 print "<tr class=\"light\">\n";
1702 $alternate ^= 1;
1703 print "<td><i>$tag{'age'}</i></td>\n" .
1704 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1705 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1706 -class => "list name"},esc_html($tag{'name'})) .
1707 "</td>\n" .
1708 "<td class=\"link\">" .
1709 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1710 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1711 "</td>\n" .
1712 "</tr>";
1714 if (defined $extra) {
1715 print "<tr>\n" .
1716 "<td colspan=\"3\">$extra</td>\n" .
1717 "</tr>\n";
1719 print "</table>\n";
1722 ## ----------------------------------------------------------------------
1723 ## functions printing large fragments, format as one of arguments
1725 sub git_diff_print {
1726 my $from = shift;
1727 my $from_name = shift;
1728 my $to = shift;
1729 my $to_name = shift;
1730 my $format = shift || "html";
1732 my $from_tmp = "/dev/null";
1733 my $to_tmp = "/dev/null";
1734 my $pid = $$;
1736 # create tmp from-file
1737 if (defined $from) {
1738 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1739 open my $fd2, "> $from_tmp";
1740 open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1741 my @file = <$fd>;
1742 print $fd2 @file;
1743 close $fd2;
1744 close $fd;
1747 # create tmp to-file
1748 if (defined $to) {
1749 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1750 open my $fd2, "> $to_tmp";
1751 open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1752 my @file = <$fd>;
1753 print $fd2 @file;
1754 close $fd2;
1755 close $fd;
1758 open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1759 if ($format eq "plain") {
1760 undef $/;
1761 print <$fd>;
1762 $/ = "\n";
1763 } else {
1764 while (my $line = <$fd>) {
1765 chomp $line;
1766 my $char = substr($line, 0, 1);
1767 my $diff_class = "";
1768 if ($char eq '+') {
1769 $diff_class = " add";
1770 } elsif ($char eq "-") {
1771 $diff_class = " rem";
1772 } elsif ($char eq "@") {
1773 $diff_class = " chunk_header";
1774 } elsif ($char eq "\\") {
1775 # skip errors
1776 next;
1778 $line = untabify($line);
1779 print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1782 close $fd;
1784 if (defined $from) {
1785 unlink($from_tmp);
1787 if (defined $to) {
1788 unlink($to_tmp);
1793 ## ======================================================================
1794 ## ======================================================================
1795 ## actions
1797 sub git_project_list {
1798 my $order = $cgi->param('o');
1799 if (defined $order && $order !~ m/project|descr|owner|age/) {
1800 die_error(undef, "Unknown order parameter");
1803 my @list = git_get_projects_list();
1804 my @projects;
1805 if (!@list) {
1806 die_error(undef, "No projects found");
1808 foreach my $pr (@list) {
1809 my $head = git_get_head_hash($pr->{'path'});
1810 if (!defined $head) {
1811 next;
1813 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1814 my %co = parse_commit($head);
1815 if (!%co) {
1816 next;
1818 $pr->{'commit'} = \%co;
1819 if (!defined $pr->{'descr'}) {
1820 my $descr = git_get_project_description($pr->{'path'}) || "";
1821 $pr->{'descr'} = chop_str($descr, 25, 5);
1823 if (!defined $pr->{'owner'}) {
1824 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1826 push @projects, $pr;
1829 git_header_html();
1830 if (-f $home_text) {
1831 print "<div class=\"index_include\">\n";
1832 open (my $fd, $home_text);
1833 print <$fd>;
1834 close $fd;
1835 print "</div>\n";
1837 print "<table class=\"project_list\">\n" .
1838 "<tr>\n";
1839 $order ||= "project";
1840 if ($order eq "project") {
1841 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
1842 print "<th>Project</th>\n";
1843 } else {
1844 print "<th>" .
1845 $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
1846 -class => "header"}, "Project") .
1847 "</th>\n";
1849 if ($order eq "descr") {
1850 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
1851 print "<th>Description</th>\n";
1852 } else {
1853 print "<th>" .
1854 $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
1855 -class => "header"}, "Description") .
1856 "</th>\n";
1858 if ($order eq "owner") {
1859 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
1860 print "<th>Owner</th>\n";
1861 } else {
1862 print "<th>" .
1863 $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
1864 -class => "header"}, "Owner") .
1865 "</th>\n";
1867 if ($order eq "age") {
1868 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
1869 print "<th>Last Change</th>\n";
1870 } else {
1871 print "<th>" .
1872 $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
1873 -class => "header"}, "Last Change") .
1874 "</th>\n";
1876 print "<th></th>\n" .
1877 "</tr>\n";
1878 my $alternate = 0;
1879 foreach my $pr (@projects) {
1880 if ($alternate) {
1881 print "<tr class=\"dark\">\n";
1882 } else {
1883 print "<tr class=\"light\">\n";
1885 $alternate ^= 1;
1886 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
1887 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
1888 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
1889 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
1890 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
1891 $pr->{'commit'}{'age_string'} . "</td>\n" .
1892 "<td class=\"link\">" .
1893 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
1894 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
1895 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
1896 "</td>\n" .
1897 "</tr>\n";
1899 print "</table>\n";
1900 git_footer_html();
1903 sub git_summary {
1904 my $descr = git_get_project_description($project) || "none";
1905 my $head = git_get_head_hash($project);
1906 my %co = parse_commit($head);
1907 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
1909 my $owner = git_get_project_owner($project);
1911 my $refs = git_get_references();
1912 git_header_html();
1913 git_print_page_nav('summary','', $head);
1915 print "<div class=\"title\">&nbsp;</div>\n";
1916 print "<table cellspacing=\"0\">\n" .
1917 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
1918 "<tr><td>owner</td><td>$owner</td></tr>\n" .
1919 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
1920 # use per project git URL list in $projectroot/$project/cloneurl
1921 # or make project git URL from git base URL and project name
1922 my $url_tag = "URL";
1923 my @url_list = git_get_project_url_list($project);
1924 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
1925 foreach my $git_url (@url_list) {
1926 next unless $git_url;
1927 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
1928 $url_tag = "";
1930 print "</table>\n";
1932 open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
1933 or die_error(undef, "Open git-rev-list failed");
1934 my @revlist = map { chomp; $_ } <$fd>;
1935 close $fd;
1936 git_print_header_div('shortlog');
1937 git_shortlog_body(\@revlist, 0, 15, $refs,
1938 $cgi->a({-href => href(action=>"shortlog")}, "..."));
1940 my $taglist = git_get_refs_list("refs/tags");
1941 if (defined @$taglist) {
1942 git_print_header_div('tags');
1943 git_tags_body($taglist, 0, 15,
1944 $cgi->a({-href => href(action=>"tags")}, "..."));
1947 my $headlist = git_get_refs_list("refs/heads");
1948 if (defined @$headlist) {
1949 git_print_header_div('heads');
1950 git_heads_body($headlist, $head, 0, 15,
1951 $cgi->a({-href => href(action=>"heads")}, "..."));
1954 git_footer_html();
1957 sub git_tag {
1958 my $head = git_get_head_hash($project);
1959 git_header_html();
1960 git_print_page_nav('','', $head,undef,$head);
1961 my %tag = parse_tag($hash);
1962 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
1963 print "<div class=\"title_text\">\n" .
1964 "<table cellspacing=\"0\">\n" .
1965 "<tr>\n" .
1966 "<td>object</td>\n" .
1967 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
1968 $tag{'object'}) . "</td>\n" .
1969 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
1970 $tag{'type'}) . "</td>\n" .
1971 "</tr>\n";
1972 if (defined($tag{'author'})) {
1973 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
1974 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
1975 print "<tr><td></td><td>" . $ad{'rfc2822'} .
1976 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
1977 "</td></tr>\n";
1979 print "</table>\n\n" .
1980 "</div>\n";
1981 print "<div class=\"page_body\">";
1982 my $comment = $tag{'comment'};
1983 foreach my $line (@$comment) {
1984 print esc_html($line) . "<br/>\n";
1986 print "</div>\n";
1987 git_footer_html();
1990 sub git_blame2 {
1991 my $fd;
1992 my $ftype;
1994 if (!gitweb_check_feature('blame')) {
1995 die_error('403 Permission denied', "Permission denied");
1997 die_error('404 Not Found', "File name not defined") if (!$file_name);
1998 $hash_base ||= git_get_head_hash($project);
1999 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2000 my %co = parse_commit($hash_base)
2001 or die_error(undef, "Reading commit failed");
2002 if (!defined $hash) {
2003 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2004 or die_error(undef, "Error looking up file");
2006 $ftype = git_get_type($hash);
2007 if ($ftype !~ "blob") {
2008 die_error("400 Bad Request", "Object is not a blob");
2010 open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2011 or die_error(undef, "Open git-blame failed");
2012 git_header_html();
2013 my $formats_nav =
2014 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2015 "blob") .
2016 " | " .
2017 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2018 "head");
2019 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2020 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2021 git_print_page_path($file_name, $ftype, $hash_base);
2022 my @rev_color = (qw(light2 dark2));
2023 my $num_colors = scalar(@rev_color);
2024 my $current_color = 0;
2025 my $last_rev;
2026 print "<div class=\"page_body\">\n";
2027 print "<table class=\"blame\">\n";
2028 print "<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n";
2029 while (<$fd>) {
2030 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2031 my $full_rev = $1;
2032 my $rev = substr($full_rev, 0, 8);
2033 my $lineno = $2;
2034 my $data = $3;
2036 if (!defined $last_rev) {
2037 $last_rev = $full_rev;
2038 } elsif ($last_rev ne $full_rev) {
2039 $last_rev = $full_rev;
2040 $current_color = ++$current_color % $num_colors;
2042 print "<tr class=\"$rev_color[$current_color]\">\n";
2043 print "<td class=\"sha1\">" .
2044 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2045 esc_html($rev)) . "</td>\n";
2046 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2047 esc_html($lineno) . "</a></td>\n";
2048 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2049 print "</tr>\n";
2051 print "</table>\n";
2052 print "</div>";
2053 close $fd
2054 or print "Reading blob failed\n";
2055 git_footer_html();
2058 sub git_blame {
2059 my $fd;
2061 if (!gitweb_check_feature('blame')) {
2062 die_error('403 Permission denied', "Permission denied");
2064 die_error('404 Not Found', "File name not defined") if (!$file_name);
2065 $hash_base ||= git_get_head_hash($project);
2066 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2067 my %co = parse_commit($hash_base)
2068 or die_error(undef, "Reading commit failed");
2069 if (!defined $hash) {
2070 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2071 or die_error(undef, "Error lookup file");
2073 open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2074 or die_error(undef, "Open git-annotate failed");
2075 git_header_html();
2076 my $formats_nav =
2077 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2078 "blob") .
2079 " | " .
2080 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2081 "head");
2082 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2083 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2084 git_print_page_path($file_name, 'blob', $hash_base);
2085 print "<div class=\"page_body\">\n";
2086 print <<HTML;
2087 <table class="blame">
2088 <tr>
2089 <th>Commit</th>
2090 <th>Age</th>
2091 <th>Author</th>
2092 <th>Line</th>
2093 <th>Data</th>
2094 </tr>
2095 HTML
2096 my @line_class = (qw(light dark));
2097 my $line_class_len = scalar (@line_class);
2098 my $line_class_num = $#line_class;
2099 while (my $line = <$fd>) {
2100 my $long_rev;
2101 my $short_rev;
2102 my $author;
2103 my $time;
2104 my $lineno;
2105 my $data;
2106 my $age;
2107 my $age_str;
2108 my $age_class;
2110 chomp $line;
2111 $line_class_num = ($line_class_num + 1) % $line_class_len;
2113 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
2114 $long_rev = $1;
2115 $author = $2;
2116 $time = $3;
2117 $lineno = $4;
2118 $data = $5;
2119 } else {
2120 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2121 next;
2123 $short_rev = substr ($long_rev, 0, 8);
2124 $age = time () - $time;
2125 $age_str = age_string ($age);
2126 $age_str =~ s/ /&nbsp;/g;
2127 $age_class = age_class($age);
2128 $author = esc_html ($author);
2129 $author =~ s/ /&nbsp;/g;
2131 $data = untabify($data);
2132 $data = esc_html ($data);
2134 print <<HTML;
2135 <tr class="$line_class[$line_class_num]">
2136 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2137 <td class="$age_class">$age_str</td>
2138 <td>$author</td>
2139 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2140 <td class="pre">$data</td>
2141 </tr>
2142 HTML
2143 } # while (my $line = <$fd>)
2144 print "</table>\n\n";
2145 close $fd
2146 or print "Reading blob failed.\n";
2147 print "</div>";
2148 git_footer_html();
2151 sub git_tags {
2152 my $head = git_get_head_hash($project);
2153 git_header_html();
2154 git_print_page_nav('','', $head,undef,$head);
2155 git_print_header_div('summary', $project);
2157 my $taglist = git_get_refs_list("refs/tags");
2158 if (defined @$taglist) {
2159 git_tags_body($taglist);
2161 git_footer_html();
2164 sub git_heads {
2165 my $head = git_get_head_hash($project);
2166 git_header_html();
2167 git_print_page_nav('','', $head,undef,$head);
2168 git_print_header_div('summary', $project);
2170 my $taglist = git_get_refs_list("refs/heads");
2171 if (defined @$taglist) {
2172 git_heads_body($taglist, $head);
2174 git_footer_html();
2177 sub git_blob_plain {
2178 if (!defined $hash) {
2179 if (defined $file_name) {
2180 my $base = $hash_base || git_get_head_hash($project);
2181 $hash = git_get_hash_by_path($base, $file_name, "blob")
2182 or die_error(undef, "Error lookup file");
2183 } else {
2184 die_error(undef, "No file name defined");
2187 my $type = shift;
2188 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2189 or die_error(undef, "Couldn't cat $file_name, $hash");
2191 $type ||= blob_mimetype($fd, $file_name);
2193 # save as filename, even when no $file_name is given
2194 my $save_as = "$hash";
2195 if (defined $file_name) {
2196 $save_as = $file_name;
2197 } elsif ($type =~ m/^text\//) {
2198 $save_as .= '.txt';
2201 print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
2202 undef $/;
2203 binmode STDOUT, ':raw';
2204 print <$fd>;
2205 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2206 $/ = "\n";
2207 close $fd;
2210 sub git_blob {
2211 if (!defined $hash) {
2212 if (defined $file_name) {
2213 my $base = $hash_base || git_get_head_hash($project);
2214 $hash = git_get_hash_by_path($base, $file_name, "blob")
2215 or die_error(undef, "Error lookup file");
2216 } else {
2217 die_error(undef, "No file name defined");
2220 my $have_blame = gitweb_check_feature('blame');
2221 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2222 or die_error(undef, "Couldn't cat $file_name, $hash");
2223 my $mimetype = blob_mimetype($fd, $file_name);
2224 if ($mimetype !~ m/^text\//) {
2225 close $fd;
2226 return git_blob_plain($mimetype);
2228 git_header_html();
2229 my $formats_nav = '';
2230 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2231 if (defined $file_name) {
2232 if ($have_blame) {
2233 $formats_nav .=
2234 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2235 hash=>$hash, file_name=>$file_name)},
2236 "blame") .
2237 " | ";
2239 $formats_nav .=
2240 $cgi->a({-href => href(action=>"blob_plain",
2241 hash=>$hash, file_name=>$file_name)},
2242 "plain") .
2243 " | " .
2244 $cgi->a({-href => href(action=>"blob",
2245 hash_base=>"HEAD", file_name=>$file_name)},
2246 "head");
2247 } else {
2248 $formats_nav .=
2249 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2251 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2252 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2253 } else {
2254 print "<div class=\"page_nav\">\n" .
2255 "<br/><br/></div>\n" .
2256 "<div class=\"title\">$hash</div>\n";
2258 git_print_page_path($file_name, "blob", $hash_base);
2259 print "<div class=\"page_body\">\n";
2260 my $nr;
2261 while (my $line = <$fd>) {
2262 chomp $line;
2263 $nr++;
2264 $line = untabify($line);
2265 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2266 $nr, $nr, $nr, esc_html($line);
2268 close $fd
2269 or print "Reading blob failed.\n";
2270 print "</div>";
2271 git_footer_html();
2274 sub git_tree {
2275 if (!defined $hash) {
2276 $hash = git_get_head_hash($project);
2277 if (defined $file_name) {
2278 my $base = $hash_base || $hash;
2279 $hash = git_get_hash_by_path($base, $file_name, "tree");
2281 if (!defined $hash_base) {
2282 $hash_base = $hash;
2285 $/ = "\0";
2286 open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2287 or die_error(undef, "Open git-ls-tree failed");
2288 my @entries = map { chomp; $_ } <$fd>;
2289 close $fd or die_error(undef, "Reading tree failed");
2290 $/ = "\n";
2292 my $refs = git_get_references();
2293 my $ref = format_ref_marker($refs, $hash_base);
2294 git_header_html();
2295 my %base_key = ();
2296 my $base = "";
2297 my $have_blame = gitweb_check_feature('blame');
2298 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2299 $base_key{hash_base} = $hash_base;
2300 git_print_page_nav('tree','', $hash_base);
2301 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2302 } else {
2303 print "<div class=\"page_nav\">\n";
2304 print "<br/><br/></div>\n";
2305 print "<div class=\"title\">$hash</div>\n";
2307 if (defined $file_name) {
2308 $base = esc_html("$file_name/");
2310 git_print_page_path($file_name, 'tree', $hash_base);
2311 print "<div class=\"page_body\">\n";
2312 print "<table cellspacing=\"0\">\n";
2313 my $alternate = 0;
2314 foreach my $line (@entries) {
2315 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2316 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2317 my $t_mode = $1;
2318 my $t_type = $2;
2319 my $t_hash = $3;
2320 my $t_name = validate_input($4);
2321 if ($alternate) {
2322 print "<tr class=\"dark\">\n";
2323 } else {
2324 print "<tr class=\"light\">\n";
2326 $alternate ^= 1;
2327 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2328 if ($t_type eq "blob") {
2329 print "<td class=\"list\">" .
2330 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2331 -class => "list"}, esc_html($t_name)) .
2332 "</td>\n" .
2333 "<td class=\"link\">" .
2334 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2335 "blob");
2336 if ($have_blame) {
2337 print " | " .
2338 $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2339 "blame");
2341 print " | " .
2342 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2343 hash=>$t_hash, file_name=>"$base$t_name")},
2344 "history") .
2345 " | " .
2346 $cgi->a({-href => href(action=>"blob_plain",
2347 hash=>$t_hash, file_name=>"$base$t_name")},
2348 "raw") .
2349 "</td>\n";
2350 } elsif ($t_type eq "tree") {
2351 print "<td class=\"list\">" .
2352 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2353 esc_html($t_name)) .
2354 "</td>\n" .
2355 "<td class=\"link\">" .
2356 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2357 "tree") .
2358 " | " .
2359 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2360 "history") .
2361 "</td>\n";
2363 print "</tr>\n";
2365 print "</table>\n" .
2366 "</div>";
2367 git_footer_html();
2370 sub git_snapshot {
2372 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2373 my $have_snapshot = (defined $ctype && defined $suffix);
2374 if (!$have_snapshot) {
2375 die_error('403 Permission denied', "Permission denied");
2378 if (!defined $hash) {
2379 $hash = git_get_head_hash($project);
2382 my $filename = basename($project) . "-$hash.tar.$suffix";
2384 print $cgi->header(-type => 'application/x-tar',
2385 -content-encoding => $ctype,
2386 '-content-disposition' => "inline; filename=\"$filename\"",
2387 -status => '200 OK');
2389 open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2390 die_error(undef, "Execute git-tar-tree failed.");
2391 binmode STDOUT, ':raw';
2392 print <$fd>;
2393 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2394 close $fd;
2398 sub git_log {
2399 my $head = git_get_head_hash($project);
2400 if (!defined $hash) {
2401 $hash = $head;
2403 if (!defined $page) {
2404 $page = 0;
2406 my $refs = git_get_references();
2408 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2409 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2410 or die_error(undef, "Open git-rev-list failed");
2411 my @revlist = map { chomp; $_ } <$fd>;
2412 close $fd;
2414 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2416 git_header_html();
2417 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2419 if (!@revlist) {
2420 my %co = parse_commit($hash);
2422 git_print_header_div('summary', $project);
2423 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2425 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2426 my $commit = $revlist[$i];
2427 my $ref = format_ref_marker($refs, $commit);
2428 my %co = parse_commit($commit);
2429 next if !%co;
2430 my %ad = parse_date($co{'author_epoch'});
2431 git_print_header_div('commit',
2432 "<span class=\"age\">$co{'age_string'}</span>" .
2433 esc_html($co{'title'}) . $ref,
2434 $commit);
2435 print "<div class=\"title_text\">\n" .
2436 "<div class=\"log_link\">\n" .
2437 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2438 " | " .
2439 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2440 "<br/>\n" .
2441 "</div>\n" .
2442 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2443 "</div>\n";
2445 print "<div class=\"log_body\">\n";
2446 git_print_simplified_log($co{'comment'});
2447 print "</div>\n";
2449 git_footer_html();
2452 sub git_commit {
2453 my %co = parse_commit($hash);
2454 if (!%co) {
2455 die_error(undef, "Unknown commit object");
2457 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2458 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2460 my $parent = $co{'parent'};
2461 if (!defined $parent) {
2462 $parent = "--root";
2464 open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2465 or die_error(undef, "Open git-diff-tree failed");
2466 my @difftree = map { chomp; $_ } <$fd>;
2467 close $fd or die_error(undef, "Reading git-diff-tree failed");
2469 # non-textual hash id's can be cached
2470 my $expires;
2471 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2472 $expires = "+1d";
2474 my $refs = git_get_references();
2475 my $ref = format_ref_marker($refs, $co{'id'});
2477 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2478 my $have_snapshot = (defined $ctype && defined $suffix);
2480 my $formats_nav = '';
2481 if (defined $file_name && defined $co{'parent'}) {
2482 my $parent = $co{'parent'};
2483 $formats_nav .=
2484 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2485 "blame");
2487 git_header_html(undef, $expires);
2488 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2489 $hash, $co{'tree'}, $hash,
2490 $formats_nav);
2492 if (defined $co{'parent'}) {
2493 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2494 } else {
2495 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2497 print "<div class=\"title_text\">\n" .
2498 "<table cellspacing=\"0\">\n";
2499 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2500 "<tr>" .
2501 "<td></td><td> $ad{'rfc2822'}";
2502 if ($ad{'hour_local'} < 6) {
2503 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2504 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2505 } else {
2506 printf(" (%02d:%02d %s)",
2507 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2509 print "</td>" .
2510 "</tr>\n";
2511 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2512 print "<tr><td></td><td> $cd{'rfc2822'}" .
2513 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2514 "</td></tr>\n";
2515 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2516 print "<tr>" .
2517 "<td>tree</td>" .
2518 "<td class=\"sha1\">" .
2519 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2520 class => "list"}, $co{'tree'}) .
2521 "</td>" .
2522 "<td class=\"link\">" .
2523 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2524 "tree");
2525 if ($have_snapshot) {
2526 print " | " .
2527 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2529 print "</td>" .
2530 "</tr>\n";
2531 my $parents = $co{'parents'};
2532 foreach my $par (@$parents) {
2533 print "<tr>" .
2534 "<td>parent</td>" .
2535 "<td class=\"sha1\">" .
2536 $cgi->a({-href => href(action=>"commit", hash=>$par),
2537 class => "list"}, $par) .
2538 "</td>" .
2539 "<td class=\"link\">" .
2540 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2541 " | " .
2542 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2543 "</td>" .
2544 "</tr>\n";
2546 print "</table>".
2547 "</div>\n";
2549 print "<div class=\"page_body\">\n";
2550 git_print_log($co{'comment'});
2551 print "</div>\n";
2553 git_difftree_body(\@difftree, $parent);
2555 git_footer_html();
2558 sub git_blobdiff {
2559 mkdir($git_temp, 0700);
2560 git_header_html();
2561 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2562 my $formats_nav =
2563 $cgi->a({-href => href(action=>"blobdiff_plain",
2564 hash=>$hash, hash_parent=>$hash_parent)},
2565 "plain");
2566 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2567 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2568 } else {
2569 print "<div class=\"page_nav\">\n" .
2570 "<br/><br/></div>\n" .
2571 "<div class=\"title\">$hash vs $hash_parent</div>\n";
2573 git_print_page_path($file_name, "blob", $hash_base);
2574 print "<div class=\"page_body\">\n" .
2575 "<div class=\"diff_info\">blob:" .
2576 $cgi->a({-href => href(action=>"blob", hash=>$hash_parent,
2577 hash_base=>$hash_base, file_name=>($file_parent || $file_name))},
2578 $hash_parent) .
2579 " -> blob:" .
2580 $cgi->a({-href => href(action=>"blob", hash=>$hash,
2581 hash_base=>$hash_base, file_name=>$file_name)},
2582 $hash) .
2583 "</div>\n";
2584 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2585 print "</div>"; # page_body
2586 git_footer_html();
2589 sub git_blobdiff_plain {
2590 mkdir($git_temp, 0700);
2591 print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2592 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2595 sub git_commitdiff {
2596 mkdir($git_temp, 0700);
2597 my %co = parse_commit($hash);
2598 if (!%co) {
2599 die_error(undef, "Unknown commit object");
2601 if (!defined $hash_parent) {
2602 $hash_parent = $co{'parent'} || '--root';
2604 open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2605 or die_error(undef, "Open git-diff-tree failed");
2606 my @difftree = map { chomp; $_ } <$fd>;
2607 close $fd or die_error(undef, "Reading git-diff-tree failed");
2609 # non-textual hash id's can be cached
2610 my $expires;
2611 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2612 $expires = "+1d";
2614 my $refs = git_get_references();
2615 my $ref = format_ref_marker($refs, $co{'id'});
2616 my $formats_nav =
2617 $cgi->a({-href => href(action=>"commitdiff_plain", hash=>$hash, hash_parent=>$hash_parent)},
2618 "plain");
2619 git_header_html(undef, $expires);
2620 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2621 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2622 print "<div class=\"page_body\">\n";
2623 git_print_simplified_log($co{'comment'}, 1); # skip title
2624 print "<br/>\n";
2625 foreach my $line (@difftree) {
2626 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2627 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2628 if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2629 next;
2631 my $from_mode = $1;
2632 my $to_mode = $2;
2633 my $from_id = $3;
2634 my $to_id = $4;
2635 my $status = $5;
2636 my $file = validate_input(unquote($6));
2637 if ($status eq "A") {
2638 print "<div class=\"diff_info\">" . file_type($to_mode) . ":" .
2639 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2640 hash=>$to_id, file_name=>$file)},
2641 $to_id) . "(new)" .
2642 "</div>\n";
2643 git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2644 } elsif ($status eq "D") {
2645 print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
2646 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2647 hash=>$from_id, file_name=>$file)},
2648 $from_id) . "(deleted)" .
2649 "</div>\n";
2650 git_diff_print($from_id, "a/$file", undef, "/dev/null");
2651 } elsif ($status eq "M") {
2652 if ($from_id ne $to_id) {
2653 print "<div class=\"diff_info\">" .
2654 file_type($from_mode) . ":" .
2655 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2656 hash=>$from_id, file_name=>$file)},
2657 $from_id) .
2658 " -> " .
2659 file_type($to_mode) . ":" .
2660 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2661 hash=>$to_id, file_name=>$file)},
2662 $to_id);
2663 print "</div>\n";
2664 git_diff_print($from_id, "a/$file", $to_id, "b/$file");
2668 print "<br/>\n" .
2669 "</div>";
2670 git_footer_html();
2673 sub git_commitdiff_plain {
2674 mkdir($git_temp, 0700);
2675 my %co = parse_commit($hash);
2676 if (!%co) {
2677 die_error(undef, "Unknown commit object");
2679 if (!defined $hash_parent) {
2680 $hash_parent = $co{'parent'} || '--root';
2682 open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2683 or die_error(undef, "Open git-diff-tree failed");
2684 my @difftree = map { chomp; $_ } <$fd>;
2685 close $fd or die_error(undef, "Reading diff-tree failed");
2687 # try to figure out the next tag after this commit
2688 my $tagname;
2689 my $refs = git_get_references("tags");
2690 open $fd, "-|", $GIT, "rev-list", "HEAD";
2691 my @commits = map { chomp; $_ } <$fd>;
2692 close $fd;
2693 foreach my $commit (@commits) {
2694 if (defined $refs->{$commit}) {
2695 $tagname = $refs->{$commit}
2697 if ($commit eq $hash) {
2698 last;
2702 print $cgi->header(-type => "text/plain",
2703 -charset => 'utf-8',
2704 '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
2705 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2706 my $comment = $co{'comment'};
2707 print "From: $co{'author'}\n" .
2708 "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
2709 "Subject: $co{'title'}\n";
2710 if (defined $tagname) {
2711 print "X-Git-Tag: $tagname\n";
2713 print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2714 "\n";
2716 foreach my $line (@$comment) {;
2717 print "$line\n";
2719 print "---\n\n";
2721 foreach my $line (@difftree) {
2722 if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2723 next;
2725 my $from_id = $3;
2726 my $to_id = $4;
2727 my $status = $5;
2728 my $file = $6;
2729 if ($status eq "A") {
2730 git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2731 } elsif ($status eq "D") {
2732 git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2733 } elsif ($status eq "M") {
2734 git_diff_print($from_id, "a/$file", $to_id, "b/$file", "plain");
2739 sub git_history {
2740 if (!defined $hash_base) {
2741 $hash_base = git_get_head_hash($project);
2743 my $ftype;
2744 my %co = parse_commit($hash_base);
2745 if (!%co) {
2746 die_error(undef, "Unknown commit object");
2748 my $refs = git_get_references();
2749 git_header_html();
2750 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2751 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2752 if (!defined $hash && defined $file_name) {
2753 $hash = git_get_hash_by_path($hash_base, $file_name);
2755 if (defined $hash) {
2756 $ftype = git_get_type($hash);
2758 git_print_page_path($file_name, $ftype, $hash_base);
2760 open my $fd, "-|",
2761 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2762 git_history_body($fd, $refs, $hash_base, $ftype);
2764 close $fd;
2765 git_footer_html();
2768 sub git_search {
2769 if (!defined $searchtext) {
2770 die_error(undef, "Text field empty");
2772 if (!defined $hash) {
2773 $hash = git_get_head_hash($project);
2775 my %co = parse_commit($hash);
2776 if (!%co) {
2777 die_error(undef, "Unknown commit object");
2779 # pickaxe may take all resources of your box and run for several minutes
2780 # with every query - so decide by yourself how public you make this feature :)
2781 my $commit_search = 1;
2782 my $author_search = 0;
2783 my $committer_search = 0;
2784 my $pickaxe_search = 0;
2785 if ($searchtext =~ s/^author\\://i) {
2786 $author_search = 1;
2787 } elsif ($searchtext =~ s/^committer\\://i) {
2788 $committer_search = 1;
2789 } elsif ($searchtext =~ s/^pickaxe\\://i) {
2790 $commit_search = 0;
2791 $pickaxe_search = 1;
2793 git_header_html();
2794 git_print_page_nav('','', $hash,$co{'tree'},$hash);
2795 git_print_header_div('commit', esc_html($co{'title'}), $hash);
2797 print "<table cellspacing=\"0\">\n";
2798 my $alternate = 0;
2799 if ($commit_search) {
2800 $/ = "\0";
2801 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2802 while (my $commit_text = <$fd>) {
2803 if (!grep m/$searchtext/i, $commit_text) {
2804 next;
2806 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2807 next;
2809 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2810 next;
2812 my @commit_lines = split "\n", $commit_text;
2813 my %co = parse_commit(undef, \@commit_lines);
2814 if (!%co) {
2815 next;
2817 if ($alternate) {
2818 print "<tr class=\"dark\">\n";
2819 } else {
2820 print "<tr class=\"light\">\n";
2822 $alternate ^= 1;
2823 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2824 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2825 "<td>" .
2826 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2827 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2828 my $comment = $co{'comment'};
2829 foreach my $line (@$comment) {
2830 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2831 my $lead = esc_html($1) || "";
2832 $lead = chop_str($lead, 30, 10);
2833 my $match = esc_html($2) || "";
2834 my $trail = esc_html($3) || "";
2835 $trail = chop_str($trail, 30, 10);
2836 my $text = "$lead<span class=\"match\">$match</span>$trail";
2837 print chop_str($text, 80, 5) . "<br/>\n";
2840 print "</td>\n" .
2841 "<td class=\"link\">" .
2842 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2843 " | " .
2844 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2845 print "</td>\n" .
2846 "</tr>\n";
2848 close $fd;
2851 if ($pickaxe_search) {
2852 $/ = "\n";
2853 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2854 undef %co;
2855 my @files;
2856 while (my $line = <$fd>) {
2857 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2858 my %set;
2859 $set{'file'} = $6;
2860 $set{'from_id'} = $3;
2861 $set{'to_id'} = $4;
2862 $set{'id'} = $set{'to_id'};
2863 if ($set{'id'} =~ m/0{40}/) {
2864 $set{'id'} = $set{'from_id'};
2866 if ($set{'id'} =~ m/0{40}/) {
2867 next;
2869 push @files, \%set;
2870 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2871 if (%co) {
2872 if ($alternate) {
2873 print "<tr class=\"dark\">\n";
2874 } else {
2875 print "<tr class=\"light\">\n";
2877 $alternate ^= 1;
2878 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2879 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2880 "<td>" .
2881 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
2882 -class => "list subject"},
2883 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2884 while (my $setref = shift @files) {
2885 my %set = %$setref;
2886 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
2887 hash=>$set{'id'}, file_name=>$set{'file'}),
2888 -class => "list"},
2889 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2890 "<br/>\n";
2892 print "</td>\n" .
2893 "<td class=\"link\">" .
2894 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2895 " | " .
2896 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2897 print "</td>\n" .
2898 "</tr>\n";
2900 %co = parse_commit($1);
2903 close $fd;
2905 print "</table>\n";
2906 git_footer_html();
2909 sub git_shortlog {
2910 my $head = git_get_head_hash($project);
2911 if (!defined $hash) {
2912 $hash = $head;
2914 if (!defined $page) {
2915 $page = 0;
2917 my $refs = git_get_references();
2919 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2920 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2921 or die_error(undef, "Open git-rev-list failed");
2922 my @revlist = map { chomp; $_ } <$fd>;
2923 close $fd;
2925 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
2926 my $next_link = '';
2927 if ($#revlist >= (100 * ($page+1)-1)) {
2928 $next_link =
2929 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
2930 -title => "Alt-n"}, "next");
2934 git_header_html();
2935 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
2936 git_print_header_div('summary', $project);
2938 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
2940 git_footer_html();
2943 ## ......................................................................
2944 ## feeds (RSS, OPML)
2946 sub git_rss {
2947 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
2948 open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
2949 or die_error(undef, "Open git-rev-list failed");
2950 my @revlist = map { chomp; $_ } <$fd>;
2951 close $fd or die_error(undef, "Reading git-rev-list failed");
2952 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2953 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2954 "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
2955 print "<channel>\n";
2956 print "<title>$project</title>\n".
2957 "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
2958 "<description>$project log</description>\n".
2959 "<language>en</language>\n";
2961 for (my $i = 0; $i <= $#revlist; $i++) {
2962 my $commit = $revlist[$i];
2963 my %co = parse_commit($commit);
2964 # we read 150, we always show 30 and the ones more recent than 48 hours
2965 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
2966 last;
2968 my %cd = parse_date($co{'committer_epoch'});
2969 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
2970 my @difftree = map { chomp; $_ } <$fd>;
2971 close $fd or next;
2972 print "<item>\n" .
2973 "<title>" .
2974 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
2975 "</title>\n" .
2976 "<author>" . esc_html($co{'author'}) . "</author>\n" .
2977 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
2978 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
2979 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
2980 "<description>" . esc_html($co{'title'}) . "</description>\n" .
2981 "<content:encoded>" .
2982 "<![CDATA[\n";
2983 my $comment = $co{'comment'};
2984 foreach my $line (@$comment) {
2985 $line = decode("utf8", $line, Encode::FB_DEFAULT);
2986 print "$line<br/>\n";
2988 print "<br/>\n";
2989 foreach my $line (@difftree) {
2990 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
2991 next;
2993 my $file = validate_input(unquote($7));
2994 $file = decode("utf8", $file, Encode::FB_DEFAULT);
2995 print "$file<br/>\n";
2997 print "]]>\n" .
2998 "</content:encoded>\n" .
2999 "</item>\n";
3001 print "</channel></rss>";
3004 sub git_opml {
3005 my @list = git_get_projects_list();
3007 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3008 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
3009 "<opml version=\"1.0\">\n".
3010 "<head>".
3011 " <title>$site_name Git OPML Export</title>\n".
3012 "</head>\n".
3013 "<body>\n".
3014 "<outline text=\"git RSS feeds\">\n";
3016 foreach my $pr (@list) {
3017 my %proj = %$pr;
3018 my $head = git_get_head_hash($proj{'path'});
3019 if (!defined $head) {
3020 next;
3022 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3023 my %co = parse_commit($head);
3024 if (!%co) {
3025 next;
3028 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3029 my $rss = "$my_url?p=$proj{'path'};a=rss";
3030 my $html = "$my_url?p=$proj{'path'};a=summary";
3031 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3033 print "</outline>\n".
3034 "</body>\n".
3035 "</opml>\n";