gitweb: Faster return from git_get_preceding_references if possible
[git/wpalmer.git] / gitweb / gitweb.perl
blob01452d2c747bb29540d512ece6887c91b2ef040e
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");
160 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
161 if (defined $project) {
162 $project =~ s|^/||;
163 $project =~ s|/$||;
164 $project = undef unless $project;
166 if (defined $project) {
167 if (!validate_input($project)) {
168 die_error(undef, "Invalid project parameter");
170 if (!(-d "$projectroot/$project")) {
171 die_error(undef, "No such directory");
173 if (!(-e "$projectroot/$project/HEAD")) {
174 die_error(undef, "No such project");
176 $ENV{'GIT_DIR'} = "$projectroot/$project";
179 our $file_name = $cgi->param('f');
180 if (defined $file_name) {
181 if (!validate_input($file_name)) {
182 die_error(undef, "Invalid file parameter");
186 our $file_parent = $cgi->param('fp');
187 if (defined $file_parent) {
188 if (!validate_input($file_parent)) {
189 die_error(undef, "Invalid file parent parameter");
193 our $hash = $cgi->param('h');
194 if (defined $hash) {
195 if (!validate_input($hash)) {
196 die_error(undef, "Invalid hash parameter");
200 our $hash_parent = $cgi->param('hp');
201 if (defined $hash_parent) {
202 if (!validate_input($hash_parent)) {
203 die_error(undef, "Invalid hash parent parameter");
207 our $hash_base = $cgi->param('hb');
208 if (defined $hash_base) {
209 if (!validate_input($hash_base)) {
210 die_error(undef, "Invalid hash base parameter");
214 our $page = $cgi->param('pg');
215 if (defined $page) {
216 if ($page =~ m/[^0-9]$/) {
217 die_error(undef, "Invalid page parameter");
221 our $searchtext = $cgi->param('s');
222 if (defined $searchtext) {
223 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
224 die_error(undef, "Invalid search parameter");
226 $searchtext = quotemeta $searchtext;
229 # dispatch
230 my %actions = (
231 "blame" => \&git_blame2,
232 "blobdiff" => \&git_blobdiff,
233 "blobdiff_plain" => \&git_blobdiff_plain,
234 "blob" => \&git_blob,
235 "blob_plain" => \&git_blob_plain,
236 "commitdiff" => \&git_commitdiff,
237 "commitdiff_plain" => \&git_commitdiff_plain,
238 "commit" => \&git_commit,
239 "heads" => \&git_heads,
240 "history" => \&git_history,
241 "log" => \&git_log,
242 "rss" => \&git_rss,
243 "search" => \&git_search,
244 "shortlog" => \&git_shortlog,
245 "summary" => \&git_summary,
246 "tag" => \&git_tag,
247 "tags" => \&git_tags,
248 "tree" => \&git_tree,
249 "snapshot" => \&git_snapshot,
250 # those below don't need $project
251 "opml" => \&git_opml,
252 "project_list" => \&git_project_list,
255 if (defined $project) {
256 $action ||= 'summary';
257 } else {
258 $action ||= 'project_list';
260 if (!defined($actions{$action})) {
261 die_error(undef, "Unknown action");
263 $actions{$action}->();
264 exit;
266 ## ======================================================================
267 ## action links
269 sub href(%) {
270 my %params = @_;
272 my @mapping = (
273 action => "a",
274 project => "p",
275 file_name => "f",
276 file_parent => "fp",
277 hash => "h",
278 hash_parent => "hp",
279 hash_base => "hb",
280 page => "pg",
281 searchtext => "s",
283 my %mapping = @mapping;
285 $params{"project"} ||= $project;
287 my @result = ();
288 for (my $i = 0; $i < @mapping; $i += 2) {
289 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
290 if (defined $params{$name}) {
291 push @result, $symbol . "=" . esc_param($params{$name});
294 return "$my_uri?" . join(';', @result);
298 ## ======================================================================
299 ## validation, quoting/unquoting and escaping
301 sub validate_input {
302 my $input = shift;
304 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
305 return $input;
307 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
308 return undef;
310 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
311 return undef;
313 return $input;
316 # quote unsafe chars, but keep the slash, even when it's not
317 # correct, but quoted slashes look too horrible in bookmarks
318 sub esc_param {
319 my $str = shift;
320 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
321 $str =~ s/\+/%2B/g;
322 $str =~ s/ /\+/g;
323 return $str;
326 # replace invalid utf8 character with SUBSTITUTION sequence
327 sub esc_html {
328 my $str = shift;
329 $str = decode("utf8", $str, Encode::FB_DEFAULT);
330 $str = escapeHTML($str);
331 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
332 return $str;
335 # git may return quoted and escaped filenames
336 sub unquote {
337 my $str = shift;
338 if ($str =~ m/^"(.*)"$/) {
339 $str = $1;
340 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
342 return $str;
345 # escape tabs (convert tabs to spaces)
346 sub untabify {
347 my $line = shift;
349 while ((my $pos = index($line, "\t")) != -1) {
350 if (my $count = (8 - ($pos % 8))) {
351 my $spaces = ' ' x $count;
352 $line =~ s/\t/$spaces/;
356 return $line;
359 ## ----------------------------------------------------------------------
360 ## HTML aware string manipulation
362 sub chop_str {
363 my $str = shift;
364 my $len = shift;
365 my $add_len = shift || 10;
367 # allow only $len chars, but don't cut a word if it would fit in $add_len
368 # if it doesn't fit, cut it if it's still longer than the dots we would add
369 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
370 my $body = $1;
371 my $tail = $2;
372 if (length($tail) > 4) {
373 $tail = " ...";
374 $body =~ s/&[^;]*$//; # remove chopped character entities
376 return "$body$tail";
379 ## ----------------------------------------------------------------------
380 ## functions returning short strings
382 # CSS class for given age value (in seconds)
383 sub age_class {
384 my $age = shift;
386 if ($age < 60*60*2) {
387 return "age0";
388 } elsif ($age < 60*60*24*2) {
389 return "age1";
390 } else {
391 return "age2";
395 # convert age in seconds to "nn units ago" string
396 sub age_string {
397 my $age = shift;
398 my $age_str;
400 if ($age > 60*60*24*365*2) {
401 $age_str = (int $age/60/60/24/365);
402 $age_str .= " years ago";
403 } elsif ($age > 60*60*24*(365/12)*2) {
404 $age_str = int $age/60/60/24/(365/12);
405 $age_str .= " months ago";
406 } elsif ($age > 60*60*24*7*2) {
407 $age_str = int $age/60/60/24/7;
408 $age_str .= " weeks ago";
409 } elsif ($age > 60*60*24*2) {
410 $age_str = int $age/60/60/24;
411 $age_str .= " days ago";
412 } elsif ($age > 60*60*2) {
413 $age_str = int $age/60/60;
414 $age_str .= " hours ago";
415 } elsif ($age > 60*2) {
416 $age_str = int $age/60;
417 $age_str .= " min ago";
418 } elsif ($age > 2) {
419 $age_str = int $age;
420 $age_str .= " sec ago";
421 } else {
422 $age_str .= " right now";
424 return $age_str;
427 # convert file mode in octal to symbolic file mode string
428 sub mode_str {
429 my $mode = oct shift;
431 if (S_ISDIR($mode & S_IFMT)) {
432 return 'drwxr-xr-x';
433 } elsif (S_ISLNK($mode)) {
434 return 'lrwxrwxrwx';
435 } elsif (S_ISREG($mode)) {
436 # git cares only about the executable bit
437 if ($mode & S_IXUSR) {
438 return '-rwxr-xr-x';
439 } else {
440 return '-rw-r--r--';
442 } else {
443 return '----------';
447 # convert file mode in octal to file type string
448 sub file_type {
449 my $mode = oct shift;
451 if (S_ISDIR($mode & S_IFMT)) {
452 return "directory";
453 } elsif (S_ISLNK($mode)) {
454 return "symlink";
455 } elsif (S_ISREG($mode)) {
456 return "file";
457 } else {
458 return "unknown";
462 ## ----------------------------------------------------------------------
463 ## functions returning short HTML fragments, or transforming HTML fragments
464 ## which don't beling to other sections
466 # format line of commit message or tag comment
467 sub format_log_line_html {
468 my $line = shift;
470 $line = esc_html($line);
471 $line =~ s/ /&nbsp;/g;
472 if ($line =~ m/([0-9a-fA-F]{40})/) {
473 my $hash_text = $1;
474 if (git_get_type($hash_text) eq "commit") {
475 my $link =
476 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
477 -class => "text"}, $hash_text);
478 $line =~ s/$hash_text/$link/;
481 return $line;
484 # format marker of refs pointing to given object
485 sub format_ref_marker {
486 my ($refs, $id) = @_;
487 my $markers = '';
489 if (defined $refs->{$id}) {
490 foreach my $ref (@{$refs->{$id}}) {
491 my ($type, $name) = qw();
492 # e.g. tags/v2.6.11 or heads/next
493 if ($ref =~ m!^(.*?)s?/(.*)$!) {
494 $type = $1;
495 $name = $2;
496 } else {
497 $type = "ref";
498 $name = $ref;
501 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
505 if ($markers) {
506 return ' <span class="refs">'. $markers . '</span>';
507 } else {
508 return "";
512 # format, perhaps shortened and with markers, title line
513 sub format_subject_html {
514 my ($long, $short, $href, $extra) = @_;
515 $extra = '' unless defined($extra);
517 if (length($short) < length($long)) {
518 return $cgi->a({-href => $href, -class => "list subject",
519 -title => $long},
520 esc_html($short) . $extra);
521 } else {
522 return $cgi->a({-href => $href, -class => "list subject"},
523 esc_html($long) . $extra);
527 sub format_diff_line {
528 my $line = shift;
529 my $char = substr($line, 0, 1);
530 my $diff_class = "";
532 chomp $line;
534 if ($char eq '+') {
535 $diff_class = " add";
536 } elsif ($char eq "-") {
537 $diff_class = " rem";
538 } elsif ($char eq "@") {
539 $diff_class = " chunk_header";
540 } elsif ($char eq "\\") {
541 $diff_class = " incomplete";
543 $line = untabify($line);
544 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
547 ## ----------------------------------------------------------------------
548 ## git utility subroutines, invoking git commands
550 # get HEAD ref of given project as hash
551 sub git_get_head_hash {
552 my $project = shift;
553 my $oENV = $ENV{'GIT_DIR'};
554 my $retval = undef;
555 $ENV{'GIT_DIR'} = "$projectroot/$project";
556 if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
557 my $head = <$fd>;
558 close $fd;
559 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
560 $retval = $1;
563 if (defined $oENV) {
564 $ENV{'GIT_DIR'} = $oENV;
566 return $retval;
569 # get type of given object
570 sub git_get_type {
571 my $hash = shift;
573 open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
574 my $type = <$fd>;
575 close $fd or return;
576 chomp $type;
577 return $type;
580 sub git_get_project_config {
581 my ($key, $type) = @_;
583 return unless ($key);
584 $key =~ s/^gitweb\.//;
585 return if ($key =~ m/\W/);
587 my @x = ($GIT, 'repo-config');
588 if (defined $type) { push @x, $type; }
589 push @x, "--get";
590 push @x, "gitweb.$key";
591 my $val = qx(@x);
592 chomp $val;
593 return ($val);
596 # get hash of given path at given ref
597 sub git_get_hash_by_path {
598 my $base = shift;
599 my $path = shift || return undef;
601 my $tree = $base;
603 open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
604 or die_error(undef, "Open git-ls-tree failed");
605 my $line = <$fd>;
606 close $fd or return undef;
608 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
609 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
610 return $3;
613 ## ......................................................................
614 ## git utility functions, directly accessing git repository
616 # assumes that PATH is not symref
617 sub git_get_hash_by_ref {
618 my $path = shift;
620 open my $fd, "$projectroot/$path" or return undef;
621 my $head = <$fd>;
622 close $fd;
623 chomp $head;
624 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
625 return $head;
629 sub git_get_project_description {
630 my $path = shift;
632 open my $fd, "$projectroot/$path/description" or return undef;
633 my $descr = <$fd>;
634 close $fd;
635 chomp $descr;
636 return $descr;
639 sub git_get_project_url_list {
640 my $path = shift;
642 open my $fd, "$projectroot/$path/cloneurl" or return undef;
643 my @git_project_url_list = map { chomp; $_ } <$fd>;
644 close $fd;
646 return wantarray ? @git_project_url_list : \@git_project_url_list;
649 sub git_get_projects_list {
650 my @list;
652 if (-d $projects_list) {
653 # search in directory
654 my $dir = $projects_list;
655 opendir my ($dh), $dir or return undef;
656 while (my $dir = readdir($dh)) {
657 if (-e "$projectroot/$dir/HEAD") {
658 my $pr = {
659 path => $dir,
661 push @list, $pr
664 closedir($dh);
665 } elsif (-f $projects_list) {
666 # read from file(url-encoded):
667 # 'git%2Fgit.git Linus+Torvalds'
668 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
669 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
670 open my ($fd), $projects_list or return undef;
671 while (my $line = <$fd>) {
672 chomp $line;
673 my ($path, $owner) = split ' ', $line;
674 $path = unescape($path);
675 $owner = unescape($owner);
676 if (!defined $path) {
677 next;
679 if (-e "$projectroot/$path/HEAD") {
680 my $pr = {
681 path => $path,
682 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
684 push @list, $pr
687 close $fd;
689 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
690 return @list;
693 sub git_get_project_owner {
694 my $project = shift;
695 my $owner;
697 return undef unless $project;
699 # read from file (url-encoded):
700 # 'git%2Fgit.git Linus+Torvalds'
701 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
702 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
703 if (-f $projects_list) {
704 open (my $fd , $projects_list);
705 while (my $line = <$fd>) {
706 chomp $line;
707 my ($pr, $ow) = split ' ', $line;
708 $pr = unescape($pr);
709 $ow = unescape($ow);
710 if ($pr eq $project) {
711 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
712 last;
715 close $fd;
717 if (!defined $owner) {
718 $owner = get_file_owner("$projectroot/$project");
721 return $owner;
724 sub git_get_references {
725 my $type = shift || "";
726 my %refs;
727 my $fd;
728 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
729 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
730 if (-f "$projectroot/$project/info/refs") {
731 open $fd, "$projectroot/$project/info/refs"
732 or return;
733 } else {
734 open $fd, "-|", $GIT, "ls-remote", "."
735 or return;
738 while (my $line = <$fd>) {
739 chomp $line;
740 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
741 if (defined $refs{$1}) {
742 push @{$refs{$1}}, $2;
743 } else {
744 $refs{$1} = [ $2 ];
748 close $fd or return;
749 return \%refs;
752 sub git_get_following_references {
753 my $hash = shift || return undef;
754 my $type = shift;
755 my $base = shift || $hash_base || "HEAD";
757 my $refs = git_get_references($type);
758 open my $fd, "-|", $GIT, "rev-list", $base
759 or return undef;
760 my @commits = map { chomp; $_ } <$fd>;
761 close $fd
762 or return undef;
764 my @reflist;
765 my $lastref;
767 foreach my $commit (@commits) {
768 foreach my $ref (@{$refs->{$commit}}) {
769 $lastref = $ref;
770 push @reflist, $lastref;
772 if ($commit eq $hash) {
773 last;
777 return wantarray ? @reflist : $lastref;
780 sub git_get_preceding_references {
781 my $hash = shift || return undef;
782 my $type = shift;
784 my $refs = git_get_references($type);
785 open my $fd, "-|", $GIT, "rev-list", $hash
786 or return undef;
787 my @commits = map { chomp; $_ } <$fd>;
788 close $fd
789 or return undef;
791 my @reflist;
793 foreach my $commit (@commits) {
794 foreach my $ref (@{$refs->{$commit}}) {
795 return $ref unless wantarray;
796 push @reflist, $ref;
800 return @reflist;
803 ## ----------------------------------------------------------------------
804 ## parse to hash functions
806 sub parse_date {
807 my $epoch = shift;
808 my $tz = shift || "-0000";
810 my %date;
811 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
812 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
813 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
814 $date{'hour'} = $hour;
815 $date{'minute'} = $min;
816 $date{'mday'} = $mday;
817 $date{'day'} = $days[$wday];
818 $date{'month'} = $months[$mon];
819 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
820 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
821 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
822 $mday, $months[$mon], $hour ,$min;
824 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
825 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
826 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
827 $date{'hour_local'} = $hour;
828 $date{'minute_local'} = $min;
829 $date{'tz_local'} = $tz;
830 return %date;
833 sub parse_tag {
834 my $tag_id = shift;
835 my %tag;
836 my @comment;
838 open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
839 $tag{'id'} = $tag_id;
840 while (my $line = <$fd>) {
841 chomp $line;
842 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
843 $tag{'object'} = $1;
844 } elsif ($line =~ m/^type (.+)$/) {
845 $tag{'type'} = $1;
846 } elsif ($line =~ m/^tag (.+)$/) {
847 $tag{'name'} = $1;
848 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
849 $tag{'author'} = $1;
850 $tag{'epoch'} = $2;
851 $tag{'tz'} = $3;
852 } elsif ($line =~ m/--BEGIN/) {
853 push @comment, $line;
854 last;
855 } elsif ($line eq "") {
856 last;
859 push @comment, <$fd>;
860 $tag{'comment'} = \@comment;
861 close $fd or return;
862 if (!defined $tag{'name'}) {
863 return
865 return %tag
868 sub parse_commit {
869 my $commit_id = shift;
870 my $commit_text = shift;
872 my @commit_lines;
873 my %co;
875 if (defined $commit_text) {
876 @commit_lines = @$commit_text;
877 } else {
878 $/ = "\0";
879 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id
880 or return;
881 @commit_lines = split '\n', <$fd>;
882 close $fd or return;
883 $/ = "\n";
884 pop @commit_lines;
886 my $header = shift @commit_lines;
887 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
888 return;
890 ($co{'id'}, my @parents) = split ' ', $header;
891 $co{'parents'} = \@parents;
892 $co{'parent'} = $parents[0];
893 while (my $line = shift @commit_lines) {
894 last if $line eq "\n";
895 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
896 $co{'tree'} = $1;
897 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
898 $co{'author'} = $1;
899 $co{'author_epoch'} = $2;
900 $co{'author_tz'} = $3;
901 if ($co{'author'} =~ m/^([^<]+) </) {
902 $co{'author_name'} = $1;
903 } else {
904 $co{'author_name'} = $co{'author'};
906 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
907 $co{'committer'} = $1;
908 $co{'committer_epoch'} = $2;
909 $co{'committer_tz'} = $3;
910 $co{'committer_name'} = $co{'committer'};
911 $co{'committer_name'} =~ s/ <.*//;
914 if (!defined $co{'tree'}) {
915 return;
918 foreach my $title (@commit_lines) {
919 $title =~ s/^ //;
920 if ($title ne "") {
921 $co{'title'} = chop_str($title, 80, 5);
922 # remove leading stuff of merges to make the interesting part visible
923 if (length($title) > 50) {
924 $title =~ s/^Automatic //;
925 $title =~ s/^merge (of|with) /Merge ... /i;
926 if (length($title) > 50) {
927 $title =~ s/(http|rsync):\/\///;
929 if (length($title) > 50) {
930 $title =~ s/(master|www|rsync)\.//;
932 if (length($title) > 50) {
933 $title =~ s/kernel.org:?//;
935 if (length($title) > 50) {
936 $title =~ s/\/pub\/scm//;
939 $co{'title_short'} = chop_str($title, 50, 5);
940 last;
943 # remove added spaces
944 foreach my $line (@commit_lines) {
945 $line =~ s/^ //;
947 $co{'comment'} = \@commit_lines;
949 my $age = time - $co{'committer_epoch'};
950 $co{'age'} = $age;
951 $co{'age_string'} = age_string($age);
952 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
953 if ($age > 60*60*24*7*2) {
954 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
955 $co{'age_string_age'} = $co{'age_string'};
956 } else {
957 $co{'age_string_date'} = $co{'age_string'};
958 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
960 return %co;
963 # parse ref from ref_file, given by ref_id, with given type
964 sub parse_ref {
965 my $ref_file = shift;
966 my $ref_id = shift;
967 my $type = shift || git_get_type($ref_id);
968 my %ref_item;
970 $ref_item{'type'} = $type;
971 $ref_item{'id'} = $ref_id;
972 $ref_item{'epoch'} = 0;
973 $ref_item{'age'} = "unknown";
974 if ($type eq "tag") {
975 my %tag = parse_tag($ref_id);
976 $ref_item{'comment'} = $tag{'comment'};
977 if ($tag{'type'} eq "commit") {
978 my %co = parse_commit($tag{'object'});
979 $ref_item{'epoch'} = $co{'committer_epoch'};
980 $ref_item{'age'} = $co{'age_string'};
981 } elsif (defined($tag{'epoch'})) {
982 my $age = time - $tag{'epoch'};
983 $ref_item{'epoch'} = $tag{'epoch'};
984 $ref_item{'age'} = age_string($age);
986 $ref_item{'reftype'} = $tag{'type'};
987 $ref_item{'name'} = $tag{'name'};
988 $ref_item{'refid'} = $tag{'object'};
989 } elsif ($type eq "commit"){
990 my %co = parse_commit($ref_id);
991 $ref_item{'reftype'} = "commit";
992 $ref_item{'name'} = $ref_file;
993 $ref_item{'title'} = $co{'title'};
994 $ref_item{'refid'} = $ref_id;
995 $ref_item{'epoch'} = $co{'committer_epoch'};
996 $ref_item{'age'} = $co{'age_string'};
997 } else {
998 $ref_item{'reftype'} = $type;
999 $ref_item{'name'} = $ref_file;
1000 $ref_item{'refid'} = $ref_id;
1003 return %ref_item;
1006 # parse line of git-diff-tree "raw" output
1007 sub parse_difftree_raw_line {
1008 my $line = shift;
1009 my %res;
1011 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1012 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1013 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1014 $res{'from_mode'} = $1;
1015 $res{'to_mode'} = $2;
1016 $res{'from_id'} = $3;
1017 $res{'to_id'} = $4;
1018 $res{'status'} = $5;
1019 $res{'similarity'} = $6;
1020 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1021 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1022 } else {
1023 $res{'file'} = unquote($7);
1026 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1027 #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1028 # $res{'commit'} = $1;
1031 return wantarray ? %res : \%res;
1034 ## ......................................................................
1035 ## parse to array of hashes functions
1037 sub git_get_refs_list {
1038 my $ref_dir = shift;
1039 my @reflist;
1041 my @refs;
1042 my $pfxlen = length("$projectroot/$project/$ref_dir");
1043 File::Find::find(sub {
1044 return if (/^\./);
1045 if (-f $_) {
1046 push @refs, substr($File::Find::name, $pfxlen + 1);
1048 }, "$projectroot/$project/$ref_dir");
1050 foreach my $ref_file (@refs) {
1051 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
1052 my $type = git_get_type($ref_id) || next;
1053 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1055 push @reflist, \%ref_item;
1057 # sort refs by age
1058 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1059 return \@reflist;
1062 ## ----------------------------------------------------------------------
1063 ## filesystem-related functions
1065 sub get_file_owner {
1066 my $path = shift;
1068 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1069 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1070 if (!defined $gcos) {
1071 return undef;
1073 my $owner = $gcos;
1074 $owner =~ s/[,;].*$//;
1075 return decode("utf8", $owner, Encode::FB_DEFAULT);
1078 ## ......................................................................
1079 ## mimetype related functions
1081 sub mimetype_guess_file {
1082 my $filename = shift;
1083 my $mimemap = shift;
1084 -r $mimemap or return undef;
1086 my %mimemap;
1087 open(MIME, $mimemap) or return undef;
1088 while (<MIME>) {
1089 next if m/^#/; # skip comments
1090 my ($mime, $exts) = split(/\t+/);
1091 if (defined $exts) {
1092 my @exts = split(/\s+/, $exts);
1093 foreach my $ext (@exts) {
1094 $mimemap{$ext} = $mime;
1098 close(MIME);
1100 $filename =~ /\.(.*?)$/;
1101 return $mimemap{$1};
1104 sub mimetype_guess {
1105 my $filename = shift;
1106 my $mime;
1107 $filename =~ /\./ or return undef;
1109 if ($mimetypes_file) {
1110 my $file = $mimetypes_file;
1111 if ($file !~ m!^/!) { # if it is relative path
1112 # it is relative to project
1113 $file = "$projectroot/$project/$file";
1115 $mime = mimetype_guess_file($filename, $file);
1117 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1118 return $mime;
1121 sub blob_mimetype {
1122 my $fd = shift;
1123 my $filename = shift;
1125 if ($filename) {
1126 my $mime = mimetype_guess($filename);
1127 $mime and return $mime;
1130 # just in case
1131 return $default_blob_plain_mimetype unless $fd;
1133 if (-T $fd) {
1134 return 'text/plain' .
1135 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1136 } elsif (! $filename) {
1137 return 'application/octet-stream';
1138 } elsif ($filename =~ m/\.png$/i) {
1139 return 'image/png';
1140 } elsif ($filename =~ m/\.gif$/i) {
1141 return 'image/gif';
1142 } elsif ($filename =~ m/\.jpe?g$/i) {
1143 return 'image/jpeg';
1144 } else {
1145 return 'application/octet-stream';
1149 ## ======================================================================
1150 ## functions printing HTML: header, footer, error page
1152 sub git_header_html {
1153 my $status = shift || "200 OK";
1154 my $expires = shift;
1156 my $title = "$site_name git";
1157 if (defined $project) {
1158 $title .= " - $project";
1159 if (defined $action) {
1160 $title .= "/$action";
1161 if (defined $file_name) {
1162 $title .= " - $file_name";
1163 if ($action eq "tree" && $file_name !~ m|/$|) {
1164 $title .= "/";
1169 my $content_type;
1170 # require explicit support from the UA if we are to send the page as
1171 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1172 # we have to do this because MSIE sometimes globs '*/*', pretending to
1173 # support xhtml+xml but choking when it gets what it asked for.
1174 if (defined $cgi->http('HTTP_ACCEPT') &&
1175 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1176 $cgi->Accept('application/xhtml+xml') != 0) {
1177 $content_type = 'application/xhtml+xml';
1178 } else {
1179 $content_type = 'text/html';
1181 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1182 -status=> $status, -expires => $expires);
1183 print <<EOF;
1184 <?xml version="1.0" encoding="utf-8"?>
1185 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1186 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1187 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1188 <!-- git core binaries version $git_version -->
1189 <head>
1190 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1191 <meta name="generator" content="gitweb/$version git/$git_version"/>
1192 <meta name="robots" content="index, nofollow"/>
1193 <title>$title</title>
1194 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1196 if (defined $project) {
1197 printf('<link rel="alternate" title="%s log" '.
1198 'href="%s" type="application/rss+xml"/>'."\n",
1199 esc_param($project), href(action=>"rss"));
1202 print "</head>\n" .
1203 "<body>\n" .
1204 "<div class=\"page_header\">\n" .
1205 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1206 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1207 "</a>\n";
1208 print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1209 if (defined $project) {
1210 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1211 if (defined $action) {
1212 print " / $action";
1214 print "\n";
1215 if (!defined $searchtext) {
1216 $searchtext = "";
1218 my $search_hash;
1219 if (defined $hash_base) {
1220 $search_hash = $hash_base;
1221 } elsif (defined $hash) {
1222 $search_hash = $hash;
1223 } else {
1224 $search_hash = "HEAD";
1226 $cgi->param("a", "search");
1227 $cgi->param("h", $search_hash);
1228 print $cgi->startform(-method => "get", -action => $my_uri) .
1229 "<div class=\"search\">\n" .
1230 $cgi->hidden(-name => "p") . "\n" .
1231 $cgi->hidden(-name => "a") . "\n" .
1232 $cgi->hidden(-name => "h") . "\n" .
1233 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1234 "</div>" .
1235 $cgi->end_form() . "\n";
1237 print "</div>\n";
1240 sub git_footer_html {
1241 print "<div class=\"page_footer\">\n";
1242 if (defined $project) {
1243 my $descr = git_get_project_description($project);
1244 if (defined $descr) {
1245 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1247 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1248 } else {
1249 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1251 print "</div>\n" .
1252 "</body>\n" .
1253 "</html>";
1256 sub die_error {
1257 my $status = shift || "403 Forbidden";
1258 my $error = shift || "Malformed query, file missing or permission denied";
1260 git_header_html($status);
1261 print <<EOF;
1262 <div class="page_body">
1263 <br /><br />
1264 $status - $error
1265 <br />
1266 </div>
1268 git_footer_html();
1269 exit;
1272 ## ----------------------------------------------------------------------
1273 ## functions printing or outputting HTML: navigation
1275 sub git_print_page_nav {
1276 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1277 $extra = '' if !defined $extra; # pager or formats
1279 my @navs = qw(summary shortlog log commit commitdiff tree);
1280 if ($suppress) {
1281 @navs = grep { $_ ne $suppress } @navs;
1284 my %arg = map { $_ => {action=>$_} } @navs;
1285 if (defined $head) {
1286 for (qw(commit commitdiff)) {
1287 $arg{$_}{hash} = $head;
1289 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1290 for (qw(shortlog log)) {
1291 $arg{$_}{hash} = $head;
1295 $arg{tree}{hash} = $treehead if defined $treehead;
1296 $arg{tree}{hash_base} = $treebase if defined $treebase;
1298 print "<div class=\"page_nav\">\n" .
1299 (join " | ",
1300 map { $_ eq $current ?
1301 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1302 } @navs);
1303 print "<br/>\n$extra<br/>\n" .
1304 "</div>\n";
1307 sub format_paging_nav {
1308 my ($action, $hash, $head, $page, $nrevs) = @_;
1309 my $paging_nav;
1312 if ($hash ne $head || $page) {
1313 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1314 } else {
1315 $paging_nav .= "HEAD";
1318 if ($page > 0) {
1319 $paging_nav .= " &sdot; " .
1320 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1321 -accesskey => "p", -title => "Alt-p"}, "prev");
1322 } else {
1323 $paging_nav .= " &sdot; prev";
1326 if ($nrevs >= (100 * ($page+1)-1)) {
1327 $paging_nav .= " &sdot; " .
1328 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1329 -accesskey => "n", -title => "Alt-n"}, "next");
1330 } else {
1331 $paging_nav .= " &sdot; next";
1334 return $paging_nav;
1337 ## ......................................................................
1338 ## functions printing or outputting HTML: div
1340 sub git_print_header_div {
1341 my ($action, $title, $hash, $hash_base) = @_;
1342 my %args = ();
1344 $args{action} = $action;
1345 $args{hash} = $hash if $hash;
1346 $args{hash_base} = $hash_base if $hash_base;
1348 print "<div class=\"header\">\n" .
1349 $cgi->a({-href => href(%args), -class => "title"},
1350 $title ? $title : $action) .
1351 "\n</div>\n";
1354 sub git_print_page_path {
1355 my $name = shift;
1356 my $type = shift;
1357 my $hb = shift;
1359 if (!defined $name) {
1360 print "<div class=\"page_path\">/</div>\n";
1361 } elsif (defined $type && $type eq 'blob') {
1362 print "<div class=\"page_path\">";
1363 if (defined $hb) {
1364 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1365 hash_base=>$hb)},
1366 esc_html($name));
1367 } else {
1368 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1369 esc_html($name));
1371 print "<br/></div>\n";
1372 } else {
1373 print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1377 sub git_print_log {
1378 my $log = shift;
1380 # remove leading empty lines
1381 while (defined $log->[0] && $log->[0] eq "") {
1382 shift @$log;
1385 # print log
1386 my $signoff = 0;
1387 my $empty = 0;
1388 foreach my $line (@$log) {
1389 # print only one empty line
1390 # do not print empty line after signoff
1391 if ($line eq "") {
1392 next if ($empty || $signoff);
1393 $empty = 1;
1394 } else {
1395 $empty = 0;
1397 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1398 $signoff = 1;
1399 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1400 } else {
1401 $signoff = 0;
1402 print format_log_line_html($line) . "<br/>\n";
1407 sub git_print_simplified_log {
1408 my $log = shift;
1409 my $remove_title = shift;
1411 shift @$log if $remove_title;
1412 # remove leading empty lines
1413 while (defined $log->[0] && $log->[0] eq "") {
1414 shift @$log;
1417 # simplify and print log
1418 my $empty = 0;
1419 foreach my $line (@$log) {
1420 # remove signoff lines
1421 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1422 next;
1424 # print only one empty line
1425 if ($line eq "") {
1426 next if $empty;
1427 $empty = 1;
1428 } else {
1429 $empty = 0;
1431 print format_log_line_html($line) . "<br/>\n";
1433 # end with single empty line
1434 print "<br/>\n" unless $empty;
1437 ## ......................................................................
1438 ## functions printing large fragments of HTML
1440 sub git_difftree_body {
1441 my ($difftree, $hash, $parent) = @_;
1443 print "<div class=\"list_head\">\n";
1444 if ($#{$difftree} > 10) {
1445 print(($#{$difftree} + 1) . " files changed:\n");
1447 print "</div>\n";
1449 print "<table class=\"diff_tree\">\n";
1450 my $alternate = 0;
1451 foreach my $line (@{$difftree}) {
1452 my %diff = parse_difftree_raw_line($line);
1454 if ($alternate) {
1455 print "<tr class=\"dark\">\n";
1456 } else {
1457 print "<tr class=\"light\">\n";
1459 $alternate ^= 1;
1461 my ($to_mode_oct, $to_mode_str, $to_file_type);
1462 my ($from_mode_oct, $from_mode_str, $from_file_type);
1463 if ($diff{'to_mode'} ne ('0' x 6)) {
1464 $to_mode_oct = oct $diff{'to_mode'};
1465 if (S_ISREG($to_mode_oct)) { # only for regular file
1466 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1468 $to_file_type = file_type($diff{'to_mode'});
1470 if ($diff{'from_mode'} ne ('0' x 6)) {
1471 $from_mode_oct = oct $diff{'from_mode'};
1472 if (S_ISREG($to_mode_oct)) { # only for regular file
1473 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1475 $from_file_type = file_type($diff{'from_mode'});
1478 if ($diff{'status'} eq "A") { # created
1479 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1480 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1481 $mode_chng .= "]</span>";
1482 print "<td>" .
1483 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1484 hash_base=>$hash, file_name=>$diff{'file'}),
1485 -class => "list"}, esc_html($diff{'file'})) .
1486 "</td>\n" .
1487 "<td>$mode_chng</td>\n" .
1488 "<td class=\"link\">" .
1489 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1490 hash_base=>$hash, file_name=>$diff{'file'})},
1491 "blob") .
1492 "</td>\n";
1494 } elsif ($diff{'status'} eq "D") { # deleted
1495 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1496 print "<td>" .
1497 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1498 hash_base=>$parent, file_name=>$diff{'file'}),
1499 -class => "list"}, esc_html($diff{'file'})) .
1500 "</td>\n" .
1501 "<td>$mode_chng</td>\n" .
1502 "<td class=\"link\">" .
1503 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1504 hash_base=>$parent, file_name=>$diff{'file'})},
1505 "blob") .
1506 " | " .
1507 $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1508 file_name=>$diff{'file'})},\
1509 "history") .
1510 "</td>\n";
1512 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1513 my $mode_chnge = "";
1514 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1515 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1516 if ($from_file_type != $to_file_type) {
1517 $mode_chnge .= " from $from_file_type to $to_file_type";
1519 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1520 if ($from_mode_str && $to_mode_str) {
1521 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1522 } elsif ($to_mode_str) {
1523 $mode_chnge .= " mode: $to_mode_str";
1526 $mode_chnge .= "]</span>\n";
1528 print "<td>";
1529 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1530 print $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1531 hash_base=>$hash, file_name=>$diff{'file'}),
1532 -class => "list"}, esc_html($diff{'file'}));
1533 } else { # only mode changed
1534 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1535 hash_base=>$hash, file_name=>$diff{'file'}),
1536 -class => "list"}, esc_html($diff{'file'}));
1538 print "</td>\n" .
1539 "<td>$mode_chnge</td>\n" .
1540 "<td class=\"link\">" .
1541 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1542 hash_base=>$hash, file_name=>$diff{'file'})},
1543 "blob");
1544 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1545 print " | " .
1546 $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1547 hash_base=>$hash, file_name=>$diff{'file'})},
1548 "diff");
1550 print " | " .
1551 $cgi->a({-href => href(action=>"history",
1552 hash_base=>$hash, file_name=>$diff{'file'})},
1553 "history");
1554 print "</td>\n";
1556 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1557 my %status_name = ('R' => 'moved', 'C' => 'copied');
1558 my $nstatus = $status_name{$diff{'status'}};
1559 my $mode_chng = "";
1560 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1561 # mode also for directories, so we cannot use $to_mode_str
1562 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1564 print "<td>" .
1565 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1566 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1567 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1568 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1569 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1570 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1571 -class => "list"}, esc_html($diff{'from_file'})) .
1572 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1573 "<td class=\"link\">" .
1574 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1575 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1576 "blob");
1577 if ($diff{'to_id'} ne $diff{'from_id'}) {
1578 print " | " .
1579 $cgi->a({-href => href(action=>"blobdiff", hash_base=>$hash,
1580 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1581 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1582 "diff");
1584 print "</td>\n";
1586 } # we should not encounter Unmerged (U) or Unknown (X) status
1587 print "</tr>\n";
1589 print "</table>\n";
1592 sub git_patchset_body {
1593 my ($fd, $difftree, $hash, $hash_parent) = @_;
1595 my $patch_idx = 0;
1596 my $in_header = 0;
1597 my $patch_found = 0;
1598 my %diffinfo;
1600 print "<div class=\"patchset\">\n";
1602 LINE:
1603 while (my $patch_line @$fd>) {
1604 chomp $patch_line;
1606 if ($patch_line =~ m/^diff /) { # "git diff" header
1607 # beginning of patch (in patchset)
1608 if ($patch_found) {
1609 # close previous patch
1610 print "</div>\n"; # class="patch"
1611 } else {
1612 # first patch in patchset
1613 $patch_found = 1;
1615 print "<div class=\"patch\">\n";
1617 %diffinfo = parse_difftree_raw_line($difftree->[$patch_idx++]);
1619 # for now, no extended header, hence we skip empty patches
1620 # companion to next LINE if $in_header;
1621 if ($diffinfo{'from_id'} eq $diffinfo{'to_id'}) { # no change
1622 $in_header = 1;
1623 next LINE;
1626 if ($diffinfo{'status'} eq "A") { # added
1627 print "<div class=\"diff_info\">" . file_type($diffinfo{'to_mode'}) . ":" .
1628 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1629 hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1630 $diffinfo{'to_id'}) . "(new)" .
1631 "</div>\n"; # class="diff_info"
1633 } elsif ($diffinfo{'status'} eq "D") { # deleted
1634 print "<div class=\"diff_info\">" . file_type($diffinfo{'from_mode'}) . ":" .
1635 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1636 hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1637 $diffinfo{'from_id'}) . "(deleted)" .
1638 "</div>\n"; # class="diff_info"
1640 } elsif ($diffinfo{'status'} eq "R" || # renamed
1641 $diffinfo{'status'} eq "C") { # copied
1642 print "<div class=\"diff_info\">" .
1643 file_type($diffinfo{'from_mode'}) . ":" .
1644 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1645 hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'from_file'})},
1646 $diffinfo{'from_id'}) .
1647 " -> " .
1648 file_type($diffinfo{'to_mode'}) . ":" .
1649 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1650 hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'to_file'})},
1651 $diffinfo{'to_id'});
1652 print "</div>\n"; # class="diff_info"
1654 } else { # modified, mode changed, ...
1655 print "<div class=\"diff_info\">" .
1656 file_type($diffinfo{'from_mode'}) . ":" .
1657 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1658 hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1659 $diffinfo{'from_id'}) .
1660 " -> " .
1661 file_type($diffinfo{'to_mode'}) . ":" .
1662 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1663 hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1664 $diffinfo{'to_id'});
1665 print "</div>\n"; # class="diff_info"
1668 #print "<div class=\"diff extended_header\">\n";
1669 $in_header = 1;
1670 next LINE;
1671 } # start of patch in patchset
1674 if ($in_header && $patch_line =~ m/^---/) {
1675 #print "</div>\n"
1676 $in_header = 0;
1678 next LINE if $in_header;
1680 print format_diff_line($patch_line);
1682 print "</div>\n" if $patch_found; # class="patch"
1684 print "</div>\n"; # class="patchset"
1687 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1689 sub git_shortlog_body {
1690 # uses global variable $project
1691 my ($revlist, $from, $to, $refs, $extra) = @_;
1693 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1694 my $have_snapshot = (defined $ctype && defined $suffix);
1696 $from = 0 unless defined $from;
1697 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1699 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1700 my $alternate = 0;
1701 for (my $i = $from; $i <= $to; $i++) {
1702 my $commit = $revlist->[$i];
1703 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1704 my $ref = format_ref_marker($refs, $commit);
1705 my %co = parse_commit($commit);
1706 if ($alternate) {
1707 print "<tr class=\"dark\">\n";
1708 } else {
1709 print "<tr class=\"light\">\n";
1711 $alternate ^= 1;
1712 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1713 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1714 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1715 "<td>";
1716 print format_subject_html($co{'title'}, $co{'title_short'},
1717 href(action=>"commit", hash=>$commit), $ref);
1718 print "</td>\n" .
1719 "<td class=\"link\">" .
1720 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1721 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1722 if ($have_snapshot) {
1723 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1725 print "</td>\n" .
1726 "</tr>\n";
1728 if (defined $extra) {
1729 print "<tr>\n" .
1730 "<td colspan=\"4\">$extra</td>\n" .
1731 "</tr>\n";
1733 print "</table>\n";
1736 sub git_history_body {
1737 # Warning: assumes constant type (blob or tree) during history
1738 my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1740 print "<table class=\"history\" cellspacing=\"0\">\n";
1741 my $alternate = 0;
1742 while (my $line = <$fd>) {
1743 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1744 next;
1747 my $commit = $1;
1748 my %co = parse_commit($commit);
1749 if (!%co) {
1750 next;
1753 my $ref = format_ref_marker($refs, $commit);
1755 if ($alternate) {
1756 print "<tr class=\"dark\">\n";
1757 } else {
1758 print "<tr class=\"light\">\n";
1760 $alternate ^= 1;
1761 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1762 # shortlog uses chop_str($co{'author_name'}, 10)
1763 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1764 "<td>";
1765 # originally git_history used chop_str($co{'title'}, 50)
1766 print format_subject_html($co{'title'}, $co{'title_short'},
1767 href(action=>"commit", hash=>$commit), $ref);
1768 print "</td>\n" .
1769 "<td class=\"link\">" .
1770 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1771 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1772 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1774 if ($ftype eq 'blob') {
1775 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1776 my $blob_parent = git_get_hash_by_path($commit, $file_name);
1777 if (defined $blob_current && defined $blob_parent &&
1778 $blob_current ne $blob_parent) {
1779 print " | " .
1780 $cgi->a({-href => href(action=>"blobdiff", hash=>$blob_current, hash_parent=>$blob_parent,
1781 hash_base=>$commit, file_name=>$file_name)},
1782 "diff to current");
1785 print "</td>\n" .
1786 "</tr>\n";
1788 if (defined $extra) {
1789 print "<tr>\n" .
1790 "<td colspan=\"4\">$extra</td>\n" .
1791 "</tr>\n";
1793 print "</table>\n";
1796 sub git_tags_body {
1797 # uses global variable $project
1798 my ($taglist, $from, $to, $extra) = @_;
1799 $from = 0 unless defined $from;
1800 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1802 print "<table class=\"tags\" cellspacing=\"0\">\n";
1803 my $alternate = 0;
1804 for (my $i = $from; $i <= $to; $i++) {
1805 my $entry = $taglist->[$i];
1806 my %tag = %$entry;
1807 my $comment_lines = $tag{'comment'};
1808 my $comment = shift @$comment_lines;
1809 my $comment_short;
1810 if (defined $comment) {
1811 $comment_short = chop_str($comment, 30, 5);
1813 if ($alternate) {
1814 print "<tr class=\"dark\">\n";
1815 } else {
1816 print "<tr class=\"light\">\n";
1818 $alternate ^= 1;
1819 print "<td><i>$tag{'age'}</i></td>\n" .
1820 "<td>" .
1821 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1822 -class => "list name"}, esc_html($tag{'name'})) .
1823 "</td>\n" .
1824 "<td>";
1825 if (defined $comment) {
1826 print format_subject_html($comment, $comment_short,
1827 href(action=>"tag", hash=>$tag{'id'}));
1829 print "</td>\n" .
1830 "<td class=\"selflink\">";
1831 if ($tag{'type'} eq "tag") {
1832 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1833 } else {
1834 print "&nbsp;";
1836 print "</td>\n" .
1837 "<td class=\"link\">" . " | " .
1838 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1839 if ($tag{'reftype'} eq "commit") {
1840 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1841 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1842 } elsif ($tag{'reftype'} eq "blob") {
1843 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1845 print "</td>\n" .
1846 "</tr>";
1848 if (defined $extra) {
1849 print "<tr>\n" .
1850 "<td colspan=\"5\">$extra</td>\n" .
1851 "</tr>\n";
1853 print "</table>\n";
1856 sub git_heads_body {
1857 # uses global variable $project
1858 my ($taglist, $head, $from, $to, $extra) = @_;
1859 $from = 0 unless defined $from;
1860 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1862 print "<table class=\"heads\" cellspacing=\"0\">\n";
1863 my $alternate = 0;
1864 for (my $i = $from; $i <= $to; $i++) {
1865 my $entry = $taglist->[$i];
1866 my %tag = %$entry;
1867 my $curr = $tag{'id'} eq $head;
1868 if ($alternate) {
1869 print "<tr class=\"dark\">\n";
1870 } else {
1871 print "<tr class=\"light\">\n";
1873 $alternate ^= 1;
1874 print "<td><i>$tag{'age'}</i></td>\n" .
1875 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1876 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1877 -class => "list name"},esc_html($tag{'name'})) .
1878 "</td>\n" .
1879 "<td class=\"link\">" .
1880 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1881 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1882 "</td>\n" .
1883 "</tr>";
1885 if (defined $extra) {
1886 print "<tr>\n" .
1887 "<td colspan=\"3\">$extra</td>\n" .
1888 "</tr>\n";
1890 print "</table>\n";
1893 ## ----------------------------------------------------------------------
1894 ## functions printing large fragments, format as one of arguments
1896 sub git_diff_print {
1897 my $from = shift;
1898 my $from_name = shift;
1899 my $to = shift;
1900 my $to_name = shift;
1901 my $format = shift || "html";
1903 my $from_tmp = "/dev/null";
1904 my $to_tmp = "/dev/null";
1905 my $pid = $$;
1907 # create tmp from-file
1908 if (defined $from) {
1909 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1910 open my $fd2, "> $from_tmp";
1911 open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1912 my @file = <$fd>;
1913 print $fd2 @file;
1914 close $fd2;
1915 close $fd;
1918 # create tmp to-file
1919 if (defined $to) {
1920 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1921 open my $fd2, "> $to_tmp";
1922 open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1923 my @file = <$fd>;
1924 print $fd2 @file;
1925 close $fd2;
1926 close $fd;
1929 open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1930 if ($format eq "plain") {
1931 undef $/;
1932 print <$fd>;
1933 $/ = "\n";
1934 } else {
1935 while (my $line = <$fd>) {
1936 chomp $line;
1937 my $char = substr($line, 0, 1);
1938 my $diff_class = "";
1939 if ($char eq '+') {
1940 $diff_class = " add";
1941 } elsif ($char eq "-") {
1942 $diff_class = " rem";
1943 } elsif ($char eq "@") {
1944 $diff_class = " chunk_header";
1945 } elsif ($char eq "\\") {
1946 # skip errors
1947 next;
1949 $line = untabify($line);
1950 print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1953 close $fd;
1955 if (defined $from) {
1956 unlink($from_tmp);
1958 if (defined $to) {
1959 unlink($to_tmp);
1964 ## ======================================================================
1965 ## ======================================================================
1966 ## actions
1968 sub git_project_list {
1969 my $order = $cgi->param('o');
1970 if (defined $order && $order !~ m/project|descr|owner|age/) {
1971 die_error(undef, "Unknown order parameter");
1974 my @list = git_get_projects_list();
1975 my @projects;
1976 if (!@list) {
1977 die_error(undef, "No projects found");
1979 foreach my $pr (@list) {
1980 my $head = git_get_head_hash($pr->{'path'});
1981 if (!defined $head) {
1982 next;
1984 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1985 my %co = parse_commit($head);
1986 if (!%co) {
1987 next;
1989 $pr->{'commit'} = \%co;
1990 if (!defined $pr->{'descr'}) {
1991 my $descr = git_get_project_description($pr->{'path'}) || "";
1992 $pr->{'descr'} = chop_str($descr, 25, 5);
1994 if (!defined $pr->{'owner'}) {
1995 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1997 push @projects, $pr;
2000 git_header_html();
2001 if (-f $home_text) {
2002 print "<div class=\"index_include\">\n";
2003 open (my $fd, $home_text);
2004 print <$fd>;
2005 close $fd;
2006 print "</div>\n";
2008 print "<table class=\"project_list\">\n" .
2009 "<tr>\n";
2010 $order ||= "project";
2011 if ($order eq "project") {
2012 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2013 print "<th>Project</th>\n";
2014 } else {
2015 print "<th>" .
2016 $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
2017 -class => "header"}, "Project") .
2018 "</th>\n";
2020 if ($order eq "descr") {
2021 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2022 print "<th>Description</th>\n";
2023 } else {
2024 print "<th>" .
2025 $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
2026 -class => "header"}, "Description") .
2027 "</th>\n";
2029 if ($order eq "owner") {
2030 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2031 print "<th>Owner</th>\n";
2032 } else {
2033 print "<th>" .
2034 $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
2035 -class => "header"}, "Owner") .
2036 "</th>\n";
2038 if ($order eq "age") {
2039 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2040 print "<th>Last Change</th>\n";
2041 } else {
2042 print "<th>" .
2043 $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
2044 -class => "header"}, "Last Change") .
2045 "</th>\n";
2047 print "<th></th>\n" .
2048 "</tr>\n";
2049 my $alternate = 0;
2050 foreach my $pr (@projects) {
2051 if ($alternate) {
2052 print "<tr class=\"dark\">\n";
2053 } else {
2054 print "<tr class=\"light\">\n";
2056 $alternate ^= 1;
2057 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2058 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2059 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2060 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2061 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2062 $pr->{'commit'}{'age_string'} . "</td>\n" .
2063 "<td class=\"link\">" .
2064 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2065 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2066 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2067 "</td>\n" .
2068 "</tr>\n";
2070 print "</table>\n";
2071 git_footer_html();
2074 sub git_summary {
2075 my $descr = git_get_project_description($project) || "none";
2076 my $head = git_get_head_hash($project);
2077 my %co = parse_commit($head);
2078 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2080 my $owner = git_get_project_owner($project);
2082 my $refs = git_get_references();
2083 git_header_html();
2084 git_print_page_nav('summary','', $head);
2086 print "<div class=\"title\">&nbsp;</div>\n";
2087 print "<table cellspacing=\"0\">\n" .
2088 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2089 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2090 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2091 # use per project git URL list in $projectroot/$project/cloneurl
2092 # or make project git URL from git base URL and project name
2093 my $url_tag = "URL";
2094 my @url_list = git_get_project_url_list($project);
2095 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2096 foreach my $git_url (@url_list) {
2097 next unless $git_url;
2098 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2099 $url_tag = "";
2101 print "</table>\n";
2103 open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
2104 or die_error(undef, "Open git-rev-list failed");
2105 my @revlist = map { chomp; $_ } <$fd>;
2106 close $fd;
2107 git_print_header_div('shortlog');
2108 git_shortlog_body(\@revlist, 0, 15, $refs,
2109 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2111 my $taglist = git_get_refs_list("refs/tags");
2112 if (defined @$taglist) {
2113 git_print_header_div('tags');
2114 git_tags_body($taglist, 0, 15,
2115 $cgi->a({-href => href(action=>"tags")}, "..."));
2118 my $headlist = git_get_refs_list("refs/heads");
2119 if (defined @$headlist) {
2120 git_print_header_div('heads');
2121 git_heads_body($headlist, $head, 0, 15,
2122 $cgi->a({-href => href(action=>"heads")}, "..."));
2125 git_footer_html();
2128 sub git_tag {
2129 my $head = git_get_head_hash($project);
2130 git_header_html();
2131 git_print_page_nav('','', $head,undef,$head);
2132 my %tag = parse_tag($hash);
2133 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2134 print "<div class=\"title_text\">\n" .
2135 "<table cellspacing=\"0\">\n" .
2136 "<tr>\n" .
2137 "<td>object</td>\n" .
2138 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2139 $tag{'object'}) . "</td>\n" .
2140 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2141 $tag{'type'}) . "</td>\n" .
2142 "</tr>\n";
2143 if (defined($tag{'author'})) {
2144 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2145 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2146 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2147 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2148 "</td></tr>\n";
2150 print "</table>\n\n" .
2151 "</div>\n";
2152 print "<div class=\"page_body\">";
2153 my $comment = $tag{'comment'};
2154 foreach my $line (@$comment) {
2155 print esc_html($line) . "<br/>\n";
2157 print "</div>\n";
2158 git_footer_html();
2161 sub git_blame2 {
2162 my $fd;
2163 my $ftype;
2165 if (!gitweb_check_feature('blame')) {
2166 die_error('403 Permission denied', "Permission denied");
2168 die_error('404 Not Found', "File name not defined") if (!$file_name);
2169 $hash_base ||= git_get_head_hash($project);
2170 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2171 my %co = parse_commit($hash_base)
2172 or die_error(undef, "Reading commit failed");
2173 if (!defined $hash) {
2174 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2175 or die_error(undef, "Error looking up file");
2177 $ftype = git_get_type($hash);
2178 if ($ftype !~ "blob") {
2179 die_error("400 Bad Request", "Object is not a blob");
2181 open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2182 or die_error(undef, "Open git-blame failed");
2183 git_header_html();
2184 my $formats_nav =
2185 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2186 "blob") .
2187 " | " .
2188 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2189 "head");
2190 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2191 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2192 git_print_page_path($file_name, $ftype, $hash_base);
2193 my @rev_color = (qw(light2 dark2));
2194 my $num_colors = scalar(@rev_color);
2195 my $current_color = 0;
2196 my $last_rev;
2197 print <<HTML;
2198 <div class="page_body">
2199 <table class="blame">
2200 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2201 HTML
2202 while (<$fd>) {
2203 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2204 my $full_rev = $1;
2205 my $rev = substr($full_rev, 0, 8);
2206 my $lineno = $2;
2207 my $data = $3;
2209 if (!defined $last_rev) {
2210 $last_rev = $full_rev;
2211 } elsif ($last_rev ne $full_rev) {
2212 $last_rev = $full_rev;
2213 $current_color = ++$current_color % $num_colors;
2215 print "<tr class=\"$rev_color[$current_color]\">\n";
2216 print "<td class=\"sha1\">" .
2217 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2218 esc_html($rev)) . "</td>\n";
2219 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2220 esc_html($lineno) . "</a></td>\n";
2221 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2222 print "</tr>\n";
2224 print "</table>\n";
2225 print "</div>";
2226 close $fd
2227 or print "Reading blob failed\n";
2228 git_footer_html();
2231 sub git_blame {
2232 my $fd;
2234 if (!gitweb_check_feature('blame')) {
2235 die_error('403 Permission denied', "Permission denied");
2237 die_error('404 Not Found', "File name not defined") if (!$file_name);
2238 $hash_base ||= git_get_head_hash($project);
2239 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2240 my %co = parse_commit($hash_base)
2241 or die_error(undef, "Reading commit failed");
2242 if (!defined $hash) {
2243 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2244 or die_error(undef, "Error lookup file");
2246 open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2247 or die_error(undef, "Open git-annotate failed");
2248 git_header_html();
2249 my $formats_nav =
2250 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2251 "blob") .
2252 " | " .
2253 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2254 "head");
2255 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2256 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2257 git_print_page_path($file_name, 'blob', $hash_base);
2258 print "<div class=\"page_body\">\n";
2259 print <<HTML;
2260 <table class="blame">
2261 <tr>
2262 <th>Commit</th>
2263 <th>Age</th>
2264 <th>Author</th>
2265 <th>Line</th>
2266 <th>Data</th>
2267 </tr>
2268 HTML
2269 my @line_class = (qw(light dark));
2270 my $line_class_len = scalar (@line_class);
2271 my $line_class_num = $#line_class;
2272 while (my $line = <$fd>) {
2273 my $long_rev;
2274 my $short_rev;
2275 my $author;
2276 my $time;
2277 my $lineno;
2278 my $data;
2279 my $age;
2280 my $age_str;
2281 my $age_class;
2283 chomp $line;
2284 $line_class_num = ($line_class_num + 1) % $line_class_len;
2286 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
2287 $long_rev = $1;
2288 $author = $2;
2289 $time = $3;
2290 $lineno = $4;
2291 $data = $5;
2292 } else {
2293 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2294 next;
2296 $short_rev = substr ($long_rev, 0, 8);
2297 $age = time () - $time;
2298 $age_str = age_string ($age);
2299 $age_str =~ s/ /&nbsp;/g;
2300 $age_class = age_class($age);
2301 $author = esc_html ($author);
2302 $author =~ s/ /&nbsp;/g;
2304 $data = untabify($data);
2305 $data = esc_html ($data);
2307 print <<HTML;
2308 <tr class="$line_class[$line_class_num]">
2309 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2310 <td class="$age_class">$age_str</td>
2311 <td>$author</td>
2312 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2313 <td class="pre">$data</td>
2314 </tr>
2315 HTML
2316 } # while (my $line = <$fd>)
2317 print "</table>\n\n";
2318 close $fd
2319 or print "Reading blob failed.\n";
2320 print "</div>";
2321 git_footer_html();
2324 sub git_tags {
2325 my $head = git_get_head_hash($project);
2326 git_header_html();
2327 git_print_page_nav('','', $head,undef,$head);
2328 git_print_header_div('summary', $project);
2330 my $taglist = git_get_refs_list("refs/tags");
2331 if (defined @$taglist) {
2332 git_tags_body($taglist);
2334 git_footer_html();
2337 sub git_heads {
2338 my $head = git_get_head_hash($project);
2339 git_header_html();
2340 git_print_page_nav('','', $head,undef,$head);
2341 git_print_header_div('summary', $project);
2343 my $taglist = git_get_refs_list("refs/heads");
2344 if (defined @$taglist) {
2345 git_heads_body($taglist, $head);
2347 git_footer_html();
2350 sub git_blob_plain {
2351 if (!defined $hash) {
2352 if (defined $file_name) {
2353 my $base = $hash_base || git_get_head_hash($project);
2354 $hash = git_get_hash_by_path($base, $file_name, "blob")
2355 or die_error(undef, "Error lookup file");
2356 } else {
2357 die_error(undef, "No file name defined");
2360 my $type = shift;
2361 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2362 or die_error(undef, "Couldn't cat $file_name, $hash");
2364 $type ||= blob_mimetype($fd, $file_name);
2366 # save as filename, even when no $file_name is given
2367 my $save_as = "$hash";
2368 if (defined $file_name) {
2369 $save_as = $file_name;
2370 } elsif ($type =~ m/^text\//) {
2371 $save_as .= '.txt';
2374 print $cgi->header(-type => "$type",
2375 -content_disposition => "inline; filename=\"$save_as\"");
2376 undef $/;
2377 binmode STDOUT, ':raw';
2378 print <$fd>;
2379 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2380 $/ = "\n";
2381 close $fd;
2384 sub git_blob {
2385 if (!defined $hash) {
2386 if (defined $file_name) {
2387 my $base = $hash_base || git_get_head_hash($project);
2388 $hash = git_get_hash_by_path($base, $file_name, "blob")
2389 or die_error(undef, "Error lookup file");
2390 } else {
2391 die_error(undef, "No file name defined");
2394 my $have_blame = gitweb_check_feature('blame');
2395 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2396 or die_error(undef, "Couldn't cat $file_name, $hash");
2397 my $mimetype = blob_mimetype($fd, $file_name);
2398 if ($mimetype !~ m/^text\//) {
2399 close $fd;
2400 return git_blob_plain($mimetype);
2402 git_header_html();
2403 my $formats_nav = '';
2404 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2405 if (defined $file_name) {
2406 if ($have_blame) {
2407 $formats_nav .=
2408 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2409 hash=>$hash, file_name=>$file_name)},
2410 "blame") .
2411 " | ";
2413 $formats_nav .=
2414 $cgi->a({-href => href(action=>"blob_plain",
2415 hash=>$hash, file_name=>$file_name)},
2416 "plain") .
2417 " | " .
2418 $cgi->a({-href => href(action=>"blob",
2419 hash_base=>"HEAD", file_name=>$file_name)},
2420 "head");
2421 } else {
2422 $formats_nav .=
2423 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2425 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2426 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2427 } else {
2428 print "<div class=\"page_nav\">\n" .
2429 "<br/><br/></div>\n" .
2430 "<div class=\"title\">$hash</div>\n";
2432 git_print_page_path($file_name, "blob", $hash_base);
2433 print "<div class=\"page_body\">\n";
2434 my $nr;
2435 while (my $line = <$fd>) {
2436 chomp $line;
2437 $nr++;
2438 $line = untabify($line);
2439 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2440 $nr, $nr, $nr, esc_html($line);
2442 close $fd
2443 or print "Reading blob failed.\n";
2444 print "</div>";
2445 git_footer_html();
2448 sub git_tree {
2449 if (!defined $hash) {
2450 $hash = git_get_head_hash($project);
2451 if (defined $file_name) {
2452 my $base = $hash_base || $hash;
2453 $hash = git_get_hash_by_path($base, $file_name, "tree");
2455 if (!defined $hash_base) {
2456 $hash_base = $hash;
2459 $/ = "\0";
2460 open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2461 or die_error(undef, "Open git-ls-tree failed");
2462 my @entries = map { chomp; $_ } <$fd>;
2463 close $fd or die_error(undef, "Reading tree failed");
2464 $/ = "\n";
2466 my $refs = git_get_references();
2467 my $ref = format_ref_marker($refs, $hash_base);
2468 git_header_html();
2469 my %base_key = ();
2470 my $base = "";
2471 my $have_blame = gitweb_check_feature('blame');
2472 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2473 $base_key{hash_base} = $hash_base;
2474 git_print_page_nav('tree','', $hash_base);
2475 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2476 } else {
2477 print "<div class=\"page_nav\">\n";
2478 print "<br/><br/></div>\n";
2479 print "<div class=\"title\">$hash</div>\n";
2481 if (defined $file_name) {
2482 $base = esc_html("$file_name/");
2484 git_print_page_path($file_name, 'tree', $hash_base);
2485 print "<div class=\"page_body\">\n";
2486 print "<table cellspacing=\"0\">\n";
2487 my $alternate = 0;
2488 foreach my $line (@entries) {
2489 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2490 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2491 my $t_mode = $1;
2492 my $t_type = $2;
2493 my $t_hash = $3;
2494 my $t_name = validate_input($4);
2495 if ($alternate) {
2496 print "<tr class=\"dark\">\n";
2497 } else {
2498 print "<tr class=\"light\">\n";
2500 $alternate ^= 1;
2501 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2502 if ($t_type eq "blob") {
2503 print "<td class=\"list\">" .
2504 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2505 -class => "list"}, esc_html($t_name)) .
2506 "</td>\n" .
2507 "<td class=\"link\">" .
2508 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2509 "blob");
2510 if ($have_blame) {
2511 print " | " .
2512 $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2513 "blame");
2515 print " | " .
2516 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2517 hash=>$t_hash, file_name=>"$base$t_name")},
2518 "history") .
2519 " | " .
2520 $cgi->a({-href => href(action=>"blob_plain",
2521 hash=>$t_hash, file_name=>"$base$t_name")},
2522 "raw") .
2523 "</td>\n";
2524 } elsif ($t_type eq "tree") {
2525 print "<td class=\"list\">" .
2526 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2527 esc_html($t_name)) .
2528 "</td>\n" .
2529 "<td class=\"link\">" .
2530 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2531 "tree") .
2532 " | " .
2533 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2534 "history") .
2535 "</td>\n";
2537 print "</tr>\n";
2539 print "</table>\n" .
2540 "</div>";
2541 git_footer_html();
2544 sub git_snapshot {
2546 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2547 my $have_snapshot = (defined $ctype && defined $suffix);
2548 if (!$have_snapshot) {
2549 die_error('403 Permission denied', "Permission denied");
2552 if (!defined $hash) {
2553 $hash = git_get_head_hash($project);
2556 my $filename = basename($project) . "-$hash.tar.$suffix";
2558 print $cgi->header(-type => 'application/x-tar',
2559 -content_encoding => $ctype,
2560 -content_disposition => "inline; filename=\"$filename\"",
2561 -status => '200 OK');
2563 open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2564 die_error(undef, "Execute git-tar-tree failed.");
2565 binmode STDOUT, ':raw';
2566 print <$fd>;
2567 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2568 close $fd;
2572 sub git_log {
2573 my $head = git_get_head_hash($project);
2574 if (!defined $hash) {
2575 $hash = $head;
2577 if (!defined $page) {
2578 $page = 0;
2580 my $refs = git_get_references();
2582 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2583 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2584 or die_error(undef, "Open git-rev-list failed");
2585 my @revlist = map { chomp; $_ } <$fd>;
2586 close $fd;
2588 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2590 git_header_html();
2591 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2593 if (!@revlist) {
2594 my %co = parse_commit($hash);
2596 git_print_header_div('summary', $project);
2597 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2599 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2600 my $commit = $revlist[$i];
2601 my $ref = format_ref_marker($refs, $commit);
2602 my %co = parse_commit($commit);
2603 next if !%co;
2604 my %ad = parse_date($co{'author_epoch'});
2605 git_print_header_div('commit',
2606 "<span class=\"age\">$co{'age_string'}</span>" .
2607 esc_html($co{'title'}) . $ref,
2608 $commit);
2609 print "<div class=\"title_text\">\n" .
2610 "<div class=\"log_link\">\n" .
2611 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2612 " | " .
2613 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2614 "<br/>\n" .
2615 "</div>\n" .
2616 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2617 "</div>\n";
2619 print "<div class=\"log_body\">\n";
2620 git_print_simplified_log($co{'comment'});
2621 print "</div>\n";
2623 git_footer_html();
2626 sub git_commit {
2627 my %co = parse_commit($hash);
2628 if (!%co) {
2629 die_error(undef, "Unknown commit object");
2631 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2632 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2634 my $parent = $co{'parent'};
2635 if (!defined $parent) {
2636 $parent = "--root";
2638 open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2639 or die_error(undef, "Open git-diff-tree failed");
2640 my @difftree = map { chomp; $_ } <$fd>;
2641 close $fd or die_error(undef, "Reading git-diff-tree failed");
2643 # non-textual hash id's can be cached
2644 my $expires;
2645 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2646 $expires = "+1d";
2648 my $refs = git_get_references();
2649 my $ref = format_ref_marker($refs, $co{'id'});
2651 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2652 my $have_snapshot = (defined $ctype && defined $suffix);
2654 my $formats_nav = '';
2655 if (defined $file_name && defined $co{'parent'}) {
2656 my $parent = $co{'parent'};
2657 $formats_nav .=
2658 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2659 "blame");
2661 git_header_html(undef, $expires);
2662 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2663 $hash, $co{'tree'}, $hash,
2664 $formats_nav);
2666 if (defined $co{'parent'}) {
2667 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2668 } else {
2669 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2671 print "<div class=\"title_text\">\n" .
2672 "<table cellspacing=\"0\">\n";
2673 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2674 "<tr>" .
2675 "<td></td><td> $ad{'rfc2822'}";
2676 if ($ad{'hour_local'} < 6) {
2677 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2678 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2679 } else {
2680 printf(" (%02d:%02d %s)",
2681 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2683 print "</td>" .
2684 "</tr>\n";
2685 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2686 print "<tr><td></td><td> $cd{'rfc2822'}" .
2687 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2688 "</td></tr>\n";
2689 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2690 print "<tr>" .
2691 "<td>tree</td>" .
2692 "<td class=\"sha1\">" .
2693 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2694 class => "list"}, $co{'tree'}) .
2695 "</td>" .
2696 "<td class=\"link\">" .
2697 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2698 "tree");
2699 if ($have_snapshot) {
2700 print " | " .
2701 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2703 print "</td>" .
2704 "</tr>\n";
2705 my $parents = $co{'parents'};
2706 foreach my $par (@$parents) {
2707 print "<tr>" .
2708 "<td>parent</td>" .
2709 "<td class=\"sha1\">" .
2710 $cgi->a({-href => href(action=>"commit", hash=>$par),
2711 class => "list"}, $par) .
2712 "</td>" .
2713 "<td class=\"link\">" .
2714 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2715 " | " .
2716 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2717 "</td>" .
2718 "</tr>\n";
2720 print "</table>".
2721 "</div>\n";
2723 print "<div class=\"page_body\">\n";
2724 git_print_log($co{'comment'});
2725 print "</div>\n";
2727 git_difftree_body(\@difftree, $hash, $parent);
2729 git_footer_html();
2732 sub git_blobdiff {
2733 mkdir($git_temp, 0700);
2734 git_header_html();
2735 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2736 my $formats_nav =
2737 $cgi->a({-href => href(action=>"blobdiff_plain",
2738 hash=>$hash, hash_parent=>$hash_parent)},
2739 "plain");
2740 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2741 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2742 } else {
2743 print <<HTML;
2744 <div class="page_nav"><br/><br/></div>
2745 <div class="title">$hash vs $hash_parent</div>
2746 HTML
2748 git_print_page_path($file_name, "blob", $hash_base);
2749 print "<div class=\"page_body\">\n" .
2750 "<div class=\"diff_info\">blob:" .
2751 $cgi->a({-href => href(action=>"blob", hash=>$hash_parent,
2752 hash_base=>$hash_base, file_name=>($file_parent || $file_name))},
2753 $hash_parent) .
2754 " -> blob:" .
2755 $cgi->a({-href => href(action=>"blob", hash=>$hash,
2756 hash_base=>$hash_base, file_name=>$file_name)},
2757 $hash) .
2758 "</div>\n";
2759 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2760 print "</div>"; # page_body
2761 git_footer_html();
2764 sub git_blobdiff_plain {
2765 mkdir($git_temp, 0700);
2766 print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2767 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2770 sub git_commitdiff {
2771 my $format = shift || 'html';
2772 my %co = parse_commit($hash);
2773 if (!%co) {
2774 die_error(undef, "Unknown commit object");
2776 if (!defined $hash_parent) {
2777 $hash_parent = $co{'parent'} || '--root';
2780 # read commitdiff
2781 my $fd;
2782 my @difftree;
2783 if ($format eq 'html') {
2784 open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C',
2785 "--patch-with-raw", "--full-index", $hash_parent, $hash
2786 or die_error(undef, "Open git-diff-tree failed");
2788 while (chomp(my $line = <$fd>)) {
2789 # empty line ends raw part of diff-tree output
2790 last unless $line;
2791 push @difftree, $line;
2794 } elsif ($format eq 'plain') {
2795 open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-B', $hash_parent, $hash
2796 or die_error(undef, "Open git-diff-tree failed");
2798 } else {
2799 die_error(undef, "Unknown commitdiff format");
2802 # non-textual hash id's can be cached
2803 my $expires;
2804 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2805 $expires = "+1d";
2808 # write commit message
2809 if ($format eq 'html') {
2810 my $refs = git_get_references();
2811 my $ref = format_ref_marker($refs, $co{'id'});
2812 my $formats_nav =
2813 $cgi->a({-href => href(action=>"commitdiff_plain",
2814 hash=>$hash, hash_parent=>$hash_parent)},
2815 "plain");
2817 git_header_html(undef, $expires);
2818 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2819 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2820 print "<div class=\"page_body\">\n";
2821 print "<div class=\"log\">\n";
2822 git_print_simplified_log($co{'comment'}, 1); # skip title
2823 print "</div>\n"; # class="log"
2825 } elsif ($format eq 'plain') {
2826 my $refs = git_get_references("tags");
2827 my @tagnames;
2828 if (exists $refs->{$hash}) {
2829 @tagnames = map { s|^tags/|| } $refs->{$hash};
2831 my $filename = basename($project) . "-$hash.patch";
2833 print $cgi->header(
2834 -type => 'text/plain',
2835 -charset => 'utf-8',
2836 -expires => $expires,
2837 -content_disposition => qq(inline; filename="$filename"));
2838 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2839 print <<TEXT;
2840 From: $co{'author'}
2841 Date: $ad{'rfc2822'} ($ad{'tz_local'})
2842 Subject: $co{'title'}
2843 TEXT
2844 foreach my $tag (@tagnames) {
2845 print "X-Git-Tag: $tag\n";
2847 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2848 foreach my $line (@{$co{'comment'}}) {
2849 print "$line\n";
2851 print "---\n\n";
2854 # write patch
2855 if ($format eq 'html') {
2856 #git_difftree_body(\@difftree, $hash, $hash_parent);
2857 #print "<br/>\n";
2859 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
2860 close $fd;
2861 print "</div>\n"; # class="page_body"
2862 git_footer_html();
2864 } elsif ($format eq 'plain') {
2865 local $/ = undef;
2866 print <$fd>;
2867 close $fd
2868 or print "Reading git-diff-tree failed\n";
2872 sub git_commitdiff_plain {
2873 git_commitdiff('plain');
2876 sub git_history {
2877 if (!defined $hash_base) {
2878 $hash_base = git_get_head_hash($project);
2880 my $ftype;
2881 my %co = parse_commit($hash_base);
2882 if (!%co) {
2883 die_error(undef, "Unknown commit object");
2885 my $refs = git_get_references();
2886 git_header_html();
2887 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2888 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2889 if (!defined $hash && defined $file_name) {
2890 $hash = git_get_hash_by_path($hash_base, $file_name);
2892 if (defined $hash) {
2893 $ftype = git_get_type($hash);
2895 git_print_page_path($file_name, $ftype, $hash_base);
2897 open my $fd, "-|",
2898 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2899 git_history_body($fd, $refs, $hash_base, $ftype);
2901 close $fd;
2902 git_footer_html();
2905 sub git_search {
2906 if (!defined $searchtext) {
2907 die_error(undef, "Text field empty");
2909 if (!defined $hash) {
2910 $hash = git_get_head_hash($project);
2912 my %co = parse_commit($hash);
2913 if (!%co) {
2914 die_error(undef, "Unknown commit object");
2916 # pickaxe may take all resources of your box and run for several minutes
2917 # with every query - so decide by yourself how public you make this feature :)
2918 my $commit_search = 1;
2919 my $author_search = 0;
2920 my $committer_search = 0;
2921 my $pickaxe_search = 0;
2922 if ($searchtext =~ s/^author\\://i) {
2923 $author_search = 1;
2924 } elsif ($searchtext =~ s/^committer\\://i) {
2925 $committer_search = 1;
2926 } elsif ($searchtext =~ s/^pickaxe\\://i) {
2927 $commit_search = 0;
2928 $pickaxe_search = 1;
2930 git_header_html();
2931 git_print_page_nav('','', $hash,$co{'tree'},$hash);
2932 git_print_header_div('commit', esc_html($co{'title'}), $hash);
2934 print "<table cellspacing=\"0\">\n";
2935 my $alternate = 0;
2936 if ($commit_search) {
2937 $/ = "\0";
2938 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2939 while (my $commit_text = <$fd>) {
2940 if (!grep m/$searchtext/i, $commit_text) {
2941 next;
2943 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2944 next;
2946 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2947 next;
2949 my @commit_lines = split "\n", $commit_text;
2950 my %co = parse_commit(undef, \@commit_lines);
2951 if (!%co) {
2952 next;
2954 if ($alternate) {
2955 print "<tr class=\"dark\">\n";
2956 } else {
2957 print "<tr class=\"light\">\n";
2959 $alternate ^= 1;
2960 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2961 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2962 "<td>" .
2963 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2964 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2965 my $comment = $co{'comment'};
2966 foreach my $line (@$comment) {
2967 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2968 my $lead = esc_html($1) || "";
2969 $lead = chop_str($lead, 30, 10);
2970 my $match = esc_html($2) || "";
2971 my $trail = esc_html($3) || "";
2972 $trail = chop_str($trail, 30, 10);
2973 my $text = "$lead<span class=\"match\">$match</span>$trail";
2974 print chop_str($text, 80, 5) . "<br/>\n";
2977 print "</td>\n" .
2978 "<td class=\"link\">" .
2979 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2980 " | " .
2981 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2982 print "</td>\n" .
2983 "</tr>\n";
2985 close $fd;
2988 if ($pickaxe_search) {
2989 $/ = "\n";
2990 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2991 undef %co;
2992 my @files;
2993 while (my $line = <$fd>) {
2994 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2995 my %set;
2996 $set{'file'} = $6;
2997 $set{'from_id'} = $3;
2998 $set{'to_id'} = $4;
2999 $set{'id'} = $set{'to_id'};
3000 if ($set{'id'} =~ m/0{40}/) {
3001 $set{'id'} = $set{'from_id'};
3003 if ($set{'id'} =~ m/0{40}/) {
3004 next;
3006 push @files, \%set;
3007 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3008 if (%co) {
3009 if ($alternate) {
3010 print "<tr class=\"dark\">\n";
3011 } else {
3012 print "<tr class=\"light\">\n";
3014 $alternate ^= 1;
3015 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3016 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3017 "<td>" .
3018 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3019 -class => "list subject"},
3020 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3021 while (my $setref = shift @files) {
3022 my %set = %$setref;
3023 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3024 hash=>$set{'id'}, file_name=>$set{'file'}),
3025 -class => "list"},
3026 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3027 "<br/>\n";
3029 print "</td>\n" .
3030 "<td class=\"link\">" .
3031 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3032 " | " .
3033 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3034 print "</td>\n" .
3035 "</tr>\n";
3037 %co = parse_commit($1);
3040 close $fd;
3042 print "</table>\n";
3043 git_footer_html();
3046 sub git_shortlog {
3047 my $head = git_get_head_hash($project);
3048 if (!defined $hash) {
3049 $hash = $head;
3051 if (!defined $page) {
3052 $page = 0;
3054 my $refs = git_get_references();
3056 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3057 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
3058 or die_error(undef, "Open git-rev-list failed");
3059 my @revlist = map { chomp; $_ } <$fd>;
3060 close $fd;
3062 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3063 my $next_link = '';
3064 if ($#revlist >= (100 * ($page+1)-1)) {
3065 $next_link =
3066 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3067 -title => "Alt-n"}, "next");
3071 git_header_html();
3072 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3073 git_print_header_div('summary', $project);
3075 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3077 git_footer_html();
3080 ## ......................................................................
3081 ## feeds (RSS, OPML)
3083 sub git_rss {
3084 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3085 open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
3086 or die_error(undef, "Open git-rev-list failed");
3087 my @revlist = map { chomp; $_ } <$fd>;
3088 close $fd or die_error(undef, "Reading git-rev-list failed");
3089 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3090 print <<XML;
3091 <?xml version="1.0" encoding="utf-8"?>
3092 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3093 <channel>
3094 <title>$project $my_uri $my_url</title>
3095 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3096 <description>$project log</description>
3097 <language>en</language>
3100 for (my $i = 0; $i <= $#revlist; $i++) {
3101 my $commit = $revlist[$i];
3102 my %co = parse_commit($commit);
3103 # we read 150, we always show 30 and the ones more recent than 48 hours
3104 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3105 last;
3107 my %cd = parse_date($co{'committer_epoch'});
3108 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
3109 my @difftree = map { chomp; $_ } <$fd>;
3110 close $fd or next;
3111 print "<item>\n" .
3112 "<title>" .
3113 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3114 "</title>\n" .
3115 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3116 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3117 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3118 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3119 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3120 "<content:encoded>" .
3121 "<![CDATA[\n";
3122 my $comment = $co{'comment'};
3123 foreach my $line (@$comment) {
3124 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3125 print "$line<br/>\n";
3127 print "<br/>\n";
3128 foreach my $line (@difftree) {
3129 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3130 next;
3132 my $file = validate_input(unquote($7));
3133 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3134 print "$file<br/>\n";
3136 print "]]>\n" .
3137 "</content:encoded>\n" .
3138 "</item>\n";
3140 print "</channel></rss>";
3143 sub git_opml {
3144 my @list = git_get_projects_list();
3146 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3147 print <<XML;
3148 <?xml version="1.0" encoding="utf-8"?>
3149 <opml version="1.0">
3150 <head>
3151 <title>$site_name Git OPML Export</title>
3152 </head>
3153 <body>
3154 <outline text="git RSS feeds">
3157 foreach my $pr (@list) {
3158 my %proj = %$pr;
3159 my $head = git_get_head_hash($proj{'path'});
3160 if (!defined $head) {
3161 next;
3163 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3164 my %co = parse_commit($head);
3165 if (!%co) {
3166 next;
3169 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3170 my $rss = "$my_url?p=$proj{'path'};a=rss";
3171 my $html = "$my_url?p=$proj{'path'};a=summary";
3172 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3174 print <<XML;
3175 </outline>
3176 </body>
3177 </opml>