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
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
22 our $version = "++GIT_VERSION++";
23 our $my_url = $cgi->url();
24 our $my_uri = $cgi->url(-absolute => 1);
26 # core git executable to use
27 # this can just be "git" if your webserver has a sensible PATH
28 our $GIT = "++GIT_BINDIR++/git";
30 # absolute fs-path which will be prepended to the project path
31 #our $projectroot = "/pub/scm";
32 our $projectroot = "++GITWEB_PROJECTROOT++";
34 # target of the home link on top of all pages
35 our $home_link = $my_uri || "/";
37 # string of the home link on top of all pages
38 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
40 # name of your site or organization to appear in page titles
41 # replace this with something more descriptive for clearer bookmarks
42 our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
44 # html text to include at home page
45 our $home_text = "++GITWEB_HOMETEXT++";
47 # URI of default stylesheet
48 our $stylesheet = "++GITWEB_CSS++";
50 our $logo = "++GITWEB_LOGO++";
51 # URI of GIT favicon, assumed to be image/png type
52 our $favicon = "++GITWEB_FAVICON++";
54 # source of projects list
55 our $projects_list = "++GITWEB_LIST++";
57 # show repository only if this file exists
58 # (only effective if this variable evaluates to true)
59 our $export_ok = "++GITWEB_EXPORT_OK++";
61 # only allow viewing of repositories also shown on the overview page
62 our $strict_export = "++GITWEB_STRICT_EXPORT++";
64 # list of git base URLs used for URL to where fetch project from,
65 # i.e. full URL is "$git_base_url/$project"
66 our @git_base_url_list = ("++GITWEB_BASE_URL++");
68 # default blob_plain mimetype and default charset for text/plain blob
69 our $default_blob_plain_mimetype = 'text/plain';
70 our $default_text_plain_charset = undef;
72 # file to use for guessing MIME types before trying /etc/mime.types
73 # (relative to the current git repository)
74 our $mimetypes_file = undef;
76 # You define site-wide feature defaults here; override them with
77 # $GITWEB_CONFIG as necessary.
80 # 'sub' => feature-sub (subroutine),
81 # 'override' => allow-override (boolean),
82 # 'default' => [ default options...] (array reference)}
84 # if feature is overridable (it means that allow-override has true value,
85 # then feature-sub will be called with default options as parameters;
86 # return value of feature-sub indicates if to enable specified feature
88 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
91 'sub' => \&feature_blame,
96 'sub' => \&feature_snapshot,
98 # => [content-encoding, suffix, program]
99 'default' => ['x-gzip', 'gz', 'gzip']},
102 'sub' => \&feature_pickaxe,
111 sub gitweb_check_feature {
113 return unless exists $feature{$name};
114 my ($sub, $override, @defaults) = (
115 $feature{$name}{'sub'},
116 $feature{$name}{'override'},
117 @{$feature{$name}{'default'}});
118 if (!$override) { return @defaults; }
120 warn "feature $name is not overrideable";
123 return $sub->(@defaults);
126 # To enable system wide have in $GITWEB_CONFIG
127 # $feature{'blame'}{'default'} = [1];
128 # To have project specific config enable override in $GITWEB_CONFIG
129 # $feature{'blame'}{'override'} = 1;
130 # and in project config gitweb.blame = 0|1;
133 my ($val) = git_get_project_config('blame', '--bool');
135 if ($val eq 'true') {
137 } elsif ($val eq 'false') {
144 # To disable system wide have in $GITWEB_CONFIG
145 # $feature{'snapshot'}{'default'} = [undef];
146 # To have project specific config enable override in $GITWEB_CONFIG
147 # $feature{'blame'}{'override'} = 1;
148 # and in project config gitweb.snapshot = none|gzip|bzip2
150 sub feature_snapshot {
151 my ($ctype, $suffix, $command) = @_;
153 my ($val) = git_get_project_config('snapshot');
155 if ($val eq 'gzip') {
156 return ('x-gzip', 'gz', 'gzip');
157 } elsif ($val eq 'bzip2') {
158 return ('x-bzip2', 'bz2', 'bzip2');
159 } elsif ($val eq 'none') {
163 return ($ctype, $suffix, $command);
166 sub gitweb_have_snapshot {
167 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
168 my $have_snapshot = (defined $ctype && defined $suffix);
170 return $have_snapshot;
173 # To enable system wide have in $GITWEB_CONFIG
174 # $feature{'pickaxe'}{'default'} = [1];
175 # To have project specific config enable override in $GITWEB_CONFIG
176 # $feature{'pickaxe'}{'override'} = 1;
177 # and in project config gitweb.pickaxe = 0|1;
179 sub feature_pickaxe {
180 my ($val) = git_get_project_config('pickaxe', '--bool');
182 if ($val eq 'true') {
184 } elsif ($val eq 'false') {
191 # rename detection options for git-diff and git-diff-tree
192 # - default is '-M', with the cost proportional to
193 # (number of removed files) * (number of new files).
194 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
195 # (number of changed files + number of removed files) * (number of new files)
196 # - even more costly is '-C', '--find-copies-harder' with cost
197 # (number of files in the original tree) * (number of new files)
198 # - one might want to include '-B' option, e.g. '-B', '-M'
199 our @diff_opts = ('-M'); # taken from git_commit
201 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
202 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
204 # version of the core git binary
205 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
207 $projects_list ||= $projectroot;
209 # ======================================================================
210 # input validation and dispatch
211 our $action = $cgi->param('a');
212 if (defined $action) {
213 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
214 die_error(undef, "Invalid action parameter");
218 # parameters which are pathnames
219 our $project = $cgi->param('p');
220 if (defined $project) {
221 if (!validate_pathname($project) ||
222 !(-d "$projectroot/$project") ||
223 !(-e "$projectroot/$project/HEAD") ||
224 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
225 ($strict_export && !project_in_list($project))) {
227 die_error(undef, "No such project");
231 our $file_name = $cgi->param('f');
232 if (defined $file_name) {
233 if (!validate_pathname($file_name)) {
234 die_error(undef, "Invalid file parameter");
238 our $file_parent = $cgi->param('fp');
239 if (defined $file_parent) {
240 if (!validate_pathname($file_parent)) {
241 die_error(undef, "Invalid file parent parameter");
245 # parameters which are refnames
246 our $hash = $cgi->param('h');
248 if (!validate_refname($hash)) {
249 die_error(undef, "Invalid hash parameter");
253 our $hash_parent = $cgi->param('hp');
254 if (defined $hash_parent) {
255 if (!validate_refname($hash_parent)) {
256 die_error(undef, "Invalid hash parent parameter");
260 our $hash_base = $cgi->param('hb');
261 if (defined $hash_base) {
262 if (!validate_refname($hash_base)) {
263 die_error(undef, "Invalid hash base parameter");
267 our $hash_parent_base = $cgi->param('hpb');
268 if (defined $hash_parent_base) {
269 if (!validate_refname($hash_parent_base)) {
270 die_error(undef, "Invalid hash parent base parameter");
275 our $page = $cgi->param('pg');
277 if ($page =~ m/[^0-9]/) {
278 die_error(undef, "Invalid page parameter");
282 our $searchtext = $cgi->param('s');
283 if (defined $searchtext) {
284 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
285 die_error(undef, "Invalid search parameter");
287 $searchtext = quotemeta $searchtext;
290 # now read PATH_INFO and use it as alternative to parameters
291 sub evaluate_path_info {
292 return if defined $project;
293 my $path_info = $ENV{"PATH_INFO"};
294 return if !$path_info;
295 $path_info =~ s,^/+,,;
296 return if !$path_info;
297 # find which part of PATH_INFO is project
298 $project = $path_info;
300 while ($project && !-e "$projectroot/$project/HEAD") {
301 $project =~ s,/*[^/]*$,,;
304 $project = validate_pathname($project);
306 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
307 ($strict_export && !project_in_list($project))) {
311 # do not change any parameters if an action is given using the query string
313 $path_info =~ s,^$project/*,,;
314 my ($refname, $pathname) = split(/:/, $path_info, 2);
315 if (defined $pathname) {
316 # we got "project.git/branch:filename" or "project.git/branch:dir/"
317 # we could use git_get_type(branch:pathname), but it needs $git_dir
318 $pathname =~ s,^/+,,;
319 if (!$pathname || substr($pathname, -1) eq "/") {
323 $action ||= "blob_plain";
325 $hash_base ||= validate_refname($refname);
326 $file_name ||= validate_pathname($pathname);
327 } elsif (defined $refname) {
328 # we got "project.git/branch"
329 $action ||= "shortlog";
330 $hash ||= validate_refname($refname);
333 evaluate_path_info();
335 # path to the current git repository
337 $git_dir = "$projectroot/$project" if $project;
341 "blame" => \&git_blame2,
342 "blobdiff" => \&git_blobdiff,
343 "blobdiff_plain" => \&git_blobdiff_plain,
344 "blob" => \&git_blob,
345 "blob_plain" => \&git_blob_plain,
346 "commitdiff" => \&git_commitdiff,
347 "commitdiff_plain" => \&git_commitdiff_plain,
348 "commit" => \&git_commit,
349 "heads" => \&git_heads,
350 "history" => \&git_history,
353 "search" => \&git_search,
354 "shortlog" => \&git_shortlog,
355 "summary" => \&git_summary,
357 "tags" => \&git_tags,
358 "tree" => \&git_tree,
359 "snapshot" => \&git_snapshot,
360 # those below don't need $project
361 "opml" => \&git_opml,
362 "project_list" => \&git_project_list,
363 "project_index" => \&git_project_index,
366 if (defined $project) {
367 $action ||= 'summary';
369 $action ||= 'project_list';
371 if (!defined($actions{$action})) {
372 die_error(undef, "Unknown action");
374 if ($action !~ m/^(opml|project_list|project_index)$/ &&
376 die_error(undef, "Project needed");
378 $actions{$action}->();
381 ## ======================================================================
396 hash_parent_base => "hpb",
401 my %mapping = @mapping;
403 $params{'project'} = $project unless exists $params{'project'};
405 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
407 # use PATH_INFO for project name
408 $href .= "/$params{'project'}" if defined $params{'project'};
409 delete $params{'project'};
411 # Summary just uses the project path URL
412 if (defined $params{'action'} && $params{'action'} eq 'summary') {
413 delete $params{'action'};
417 # now encode the parameters explicitly
419 for (my $i = 0; $i < @mapping; $i += 2) {
420 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
421 if (defined $params{$name}) {
422 push @result, $symbol . "=" . esc_param($params{$name});
425 $href .= "?" . join(';', @result) if scalar @result;
431 ## ======================================================================
432 ## validation, quoting/unquoting and escaping
434 sub validate_pathname {
435 my $input = shift || return undef;
437 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
438 # at the beginning, at the end, and between slashes.
439 # also this catches doubled slashes
440 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
444 if ($input =~ m!\0!) {
450 sub validate_refname {
451 my $input = shift || return undef;
453 # textual hashes are O.K.
454 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
457 # it must be correct pathname
458 $input = validate_pathname($input)
460 # restrictions on ref name according to git-check-ref-format
461 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
467 # quote unsafe chars, but keep the slash, even when it's not
468 # correct, but quoted slashes look too horrible in bookmarks
471 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
477 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
480 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
486 # replace invalid utf8 character with SUBSTITUTION sequence
489 $str = decode("utf8", $str, Encode::FB_DEFAULT);
490 $str = escapeHTML($str);
491 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
495 # git may return quoted and escaped filenames
498 if ($str =~ m/^"(.*)"$/) {
500 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
505 # escape tabs (convert tabs to spaces)
509 while ((my $pos = index($line, "\t")) != -1) {
510 if (my $count = (8 - ($pos % 8))) {
511 my $spaces = ' ' x $count;
512 $line =~ s/\t/$spaces/;
519 sub project_in_list {
521 my @list = git_get_projects_list();
522 return @list && scalar(grep { $_->{'path'} eq $project } @list);
525 ## ----------------------------------------------------------------------
526 ## HTML aware string manipulation
531 my $add_len = shift || 10;
533 # allow only $len chars, but don't cut a word if it would fit in $add_len
534 # if it doesn't fit, cut it if it's still longer than the dots we would add
535 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
538 if (length($tail) > 4) {
540 $body =~ s/&[^;]*$//; # remove chopped character entities
545 ## ----------------------------------------------------------------------
546 ## functions returning short strings
548 # CSS class for given age value (in seconds)
552 if ($age < 60*60*2) {
554 } elsif ($age < 60*60*24*2) {
561 # convert age in seconds to "nn units ago" string
566 if ($age > 60*60*24*365*2) {
567 $age_str = (int $age/60/60/24/365);
568 $age_str .= " years ago";
569 } elsif ($age > 60*60*24*(365/12)*2) {
570 $age_str = int $age/60/60/24/(365/12);
571 $age_str .= " months ago";
572 } elsif ($age > 60*60*24*7*2) {
573 $age_str = int $age/60/60/24/7;
574 $age_str .= " weeks ago";
575 } elsif ($age > 60*60*24*2) {
576 $age_str = int $age/60/60/24;
577 $age_str .= " days ago";
578 } elsif ($age > 60*60*2) {
579 $age_str = int $age/60/60;
580 $age_str .= " hours ago";
581 } elsif ($age > 60*2) {
582 $age_str = int $age/60;
583 $age_str .= " min ago";
586 $age_str .= " sec ago";
588 $age_str .= " right now";
593 # convert file mode in octal to symbolic file mode string
595 my $mode = oct shift;
597 if (S_ISDIR($mode & S_IFMT)) {
599 } elsif (S_ISLNK($mode)) {
601 } elsif (S_ISREG($mode)) {
602 # git cares only about the executable bit
603 if ($mode & S_IXUSR) {
613 # convert file mode in octal to file type string
617 if ($mode !~ m/^[0-7]+$/) {
623 if (S_ISDIR($mode & S_IFMT)) {
625 } elsif (S_ISLNK($mode)) {
627 } elsif (S_ISREG($mode)) {
634 ## ----------------------------------------------------------------------
635 ## functions returning short HTML fragments, or transforming HTML fragments
636 ## which don't beling to other sections
638 # format line of commit message or tag comment
639 sub format_log_line_html {
642 $line = esc_html($line);
643 $line =~ s/ / /g;
644 if ($line =~ m/([0-9a-fA-F]{40})/) {
646 if (git_get_type($hash_text) eq "commit") {
648 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
649 -class => "text"}, $hash_text);
650 $line =~ s/$hash_text/$link/;
656 # format marker of refs pointing to given object
657 sub format_ref_marker {
658 my ($refs, $id) = @_;
661 if (defined $refs->{$id}) {
662 foreach my $ref (@{$refs->{$id}}) {
663 my ($type, $name) = qw();
664 # e.g. tags/v2.6.11 or heads/next
665 if ($ref =~ m!^(.*?)s?/(.*)$!) {
673 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
678 return ' <span class="refs">'. $markers . '</span>';
684 # format, perhaps shortened and with markers, title line
685 sub format_subject_html {
686 my ($long, $short, $href, $extra) = @_;
687 $extra = '' unless defined($extra);
689 if (length($short) < length($long)) {
690 return $cgi->a({-href => $href, -class => "list subject",
691 -title => decode("utf8", $long, Encode::FB_DEFAULT)},
692 esc_html($short) . $extra);
694 return $cgi->a({-href => $href, -class => "list subject"},
695 esc_html($long) . $extra);
699 sub format_diff_line {
701 my $char = substr($line, 0, 1);
707 $diff_class = " add";
708 } elsif ($char eq "-") {
709 $diff_class = " rem";
710 } elsif ($char eq "@") {
711 $diff_class = " chunk_header";
712 } elsif ($char eq "\\") {
713 $diff_class = " incomplete";
715 $line = untabify($line);
716 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
719 ## ----------------------------------------------------------------------
720 ## git utility subroutines, invoking git commands
722 # returns path to the core git executable and the --git-dir parameter as list
724 return $GIT, '--git-dir='.$git_dir;
727 # returns path to the core git executable and the --git-dir parameter as string
729 return join(' ', git_cmd());
732 # get HEAD ref of given project as hash
733 sub git_get_head_hash {
735 my $o_git_dir = $git_dir;
737 $git_dir = "$projectroot/$project";
738 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
741 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
745 if (defined $o_git_dir) {
746 $git_dir = $o_git_dir;
751 # get type of given object
755 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
762 sub git_get_project_config {
763 my ($key, $type) = @_;
765 return unless ($key);
766 $key =~ s/^gitweb\.//;
767 return if ($key =~ m/\W/);
769 my @x = (git_cmd(), 'repo-config');
770 if (defined $type) { push @x, $type; }
772 push @x, "gitweb.$key";
778 # get hash of given path at given ref
779 sub git_get_hash_by_path {
781 my $path = shift || return undef;
786 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
787 or die_error(undef, "Open git-ls-tree failed");
789 close $fd or return undef;
791 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
792 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
793 if (defined $type && $type ne $2) {
800 ## ......................................................................
801 ## git utility functions, directly accessing git repository
803 sub git_get_project_description {
806 open my $fd, "$projectroot/$path/description" or return undef;
813 sub git_get_project_url_list {
816 open my $fd, "$projectroot/$path/cloneurl" or return;
817 my @git_project_url_list = map { chomp; $_ } <$fd>;
820 return wantarray ? @git_project_url_list : \@git_project_url_list;
823 sub git_get_projects_list {
826 if (-d $projects_list) {
827 # search in directory
828 my $dir = $projects_list;
829 my $pfxlen = length("$dir");
832 follow_fast => 1, # follow symbolic links
833 dangling_symlinks => 0, # ignore dangling symlinks, silently
835 # skip project-list toplevel, if we get it.
836 return if (m!^[/.]$!);
837 # only directories can be git repositories
838 return unless (-d $_);
840 my $subdir = substr($File::Find::name, $pfxlen + 1);
841 # we check related file in $projectroot
842 if (-e "$projectroot/$subdir/HEAD" && (!$export_ok ||
843 -e "$projectroot/$subdir/$export_ok")) {
844 push @list, { path => $subdir };
845 $File::Find::prune = 1;
850 } elsif (-f $projects_list) {
851 # read from file(url-encoded):
852 # 'git%2Fgit.git Linus+Torvalds'
853 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
854 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
855 open my ($fd), $projects_list or return;
856 while (my $line = <$fd>) {
858 my ($path, $owner) = split ' ', $line;
859 $path = unescape($path);
860 $owner = unescape($owner);
861 if (!defined $path) {
864 if (-e "$projectroot/$path/HEAD" && (!$export_ok ||
865 -e "$projectroot/$path/$export_ok")) {
868 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
875 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
879 sub git_get_project_owner {
883 return undef unless $project;
885 # read from file (url-encoded):
886 # 'git%2Fgit.git Linus+Torvalds'
887 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
888 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
889 if (-f $projects_list) {
890 open (my $fd , $projects_list);
891 while (my $line = <$fd>) {
893 my ($pr, $ow) = split ' ', $line;
896 if ($pr eq $project) {
897 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
903 if (!defined $owner) {
904 $owner = get_file_owner("$projectroot/$project");
910 sub git_get_references {
911 my $type = shift || "";
913 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
914 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
915 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
918 while (my $line = <$fd>) {
920 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
921 if (defined $refs{$1}) {
922 push @{$refs{$1}}, $2;
932 sub git_get_rev_name_tags {
933 my $hash = shift || return undef;
935 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
937 my $name_rev = <$fd>;
940 if ($name_rev =~ m|^$hash tags/(.*)$|) {
943 # catches also '$hash undefined' output
948 ## ----------------------------------------------------------------------
949 ## parse to hash functions
953 my $tz = shift || "-0000";
956 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
957 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
958 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
959 $date{'hour'} = $hour;
960 $date{'minute'} = $min;
961 $date{'mday'} = $mday;
962 $date{'day'} = $days[$wday];
963 $date{'month'} = $months[$mon];
964 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
965 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
966 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
967 $mday, $months[$mon], $hour ,$min;
969 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
970 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
971 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
972 $date{'hour_local'} = $hour;
973 $date{'minute_local'} = $min;
974 $date{'tz_local'} = $tz;
983 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
984 $tag{'id'} = $tag_id;
985 while (my $line = <$fd>) {
987 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
989 } elsif ($line =~ m/^type (.+)$/) {
991 } elsif ($line =~ m/^tag (.+)$/) {
993 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
997 } elsif ($line =~ m/--BEGIN/) {
998 push @comment, $line;
1000 } elsif ($line eq "") {
1004 push @comment, <$fd>;
1005 $tag{'comment'} = \@comment;
1006 close $fd or return;
1007 if (!defined $tag{'name'}) {
1014 my $commit_id = shift;
1015 my $commit_text = shift;
1020 if (defined $commit_text) {
1021 @commit_lines = @$commit_text;
1024 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
1026 @commit_lines = split '\n', <$fd>;
1027 close $fd or return;
1031 my $header = shift @commit_lines;
1032 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1035 ($co{'id'}, my @parents) = split ' ', $header;
1036 $co{'parents'} = \@parents;
1037 $co{'parent'} = $parents[0];
1038 while (my $line = shift @commit_lines) {
1039 last if $line eq "\n";
1040 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1042 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1044 $co{'author_epoch'} = $2;
1045 $co{'author_tz'} = $3;
1046 if ($co{'author'} =~ m/^([^<]+) </) {
1047 $co{'author_name'} = $1;
1049 $co{'author_name'} = $co{'author'};
1051 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1052 $co{'committer'} = $1;
1053 $co{'committer_epoch'} = $2;
1054 $co{'committer_tz'} = $3;
1055 $co{'committer_name'} = $co{'committer'};
1056 $co{'committer_name'} =~ s/ <.*//;
1059 if (!defined $co{'tree'}) {
1063 foreach my $title (@commit_lines) {
1066 $co{'title'} = chop_str($title, 80, 5);
1067 # remove leading stuff of merges to make the interesting part visible
1068 if (length($title) > 50) {
1069 $title =~ s/^Automatic //;
1070 $title =~ s/^merge (of|with) /Merge ... /i;
1071 if (length($title) > 50) {
1072 $title =~ s/(http|rsync):\/\///;
1074 if (length($title) > 50) {
1075 $title =~ s/(master|www|rsync)\.//;
1077 if (length($title) > 50) {
1078 $title =~ s/kernel.org:?//;
1080 if (length($title) > 50) {
1081 $title =~ s/\/pub\/scm//;
1084 $co{'title_short'} = chop_str($title, 50, 5);
1088 # remove added spaces
1089 foreach my $line (@commit_lines) {
1092 $co{'comment'} = \@commit_lines;
1094 my $age = time - $co{'committer_epoch'};
1096 $co{'age_string'} = age_string($age);
1097 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1098 if ($age > 60*60*24*7*2) {
1099 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1100 $co{'age_string_age'} = $co{'age_string'};
1102 $co{'age_string_date'} = $co{'age_string'};
1103 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1108 # parse ref from ref_file, given by ref_id, with given type
1110 my $ref_file = shift;
1112 my $type = shift || git_get_type($ref_id);
1115 $ref_item{'type'} = $type;
1116 $ref_item{'id'} = $ref_id;
1117 $ref_item{'epoch'} = 0;
1118 $ref_item{'age'} = "unknown";
1119 if ($type eq "tag") {
1120 my %tag = parse_tag($ref_id);
1121 $ref_item{'comment'} = $tag{'comment'};
1122 if ($tag{'type'} eq "commit") {
1123 my %co = parse_commit($tag{'object'});
1124 $ref_item{'epoch'} = $co{'committer_epoch'};
1125 $ref_item{'age'} = $co{'age_string'};
1126 } elsif (defined($tag{'epoch'})) {
1127 my $age = time - $tag{'epoch'};
1128 $ref_item{'epoch'} = $tag{'epoch'};
1129 $ref_item{'age'} = age_string($age);
1131 $ref_item{'reftype'} = $tag{'type'};
1132 $ref_item{'name'} = $tag{'name'};
1133 $ref_item{'refid'} = $tag{'object'};
1134 } elsif ($type eq "commit"){
1135 my %co = parse_commit($ref_id);
1136 $ref_item{'reftype'} = "commit";
1137 $ref_item{'name'} = $ref_file;
1138 $ref_item{'title'} = $co{'title'};
1139 $ref_item{'refid'} = $ref_id;
1140 $ref_item{'epoch'} = $co{'committer_epoch'};
1141 $ref_item{'age'} = $co{'age_string'};
1143 $ref_item{'reftype'} = $type;
1144 $ref_item{'name'} = $ref_file;
1145 $ref_item{'refid'} = $ref_id;
1151 # parse line of git-diff-tree "raw" output
1152 sub parse_difftree_raw_line {
1156 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1157 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1158 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1159 $res{'from_mode'} = $1;
1160 $res{'to_mode'} = $2;
1161 $res{'from_id'} = $3;
1163 $res{'status'} = $5;
1164 $res{'similarity'} = $6;
1165 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1166 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1168 $res{'file'} = unquote($7);
1171 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1172 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1173 $res{'commit'} = $1;
1176 return wantarray ? %res : \%res;
1179 # parse line of git-ls-tree output
1180 sub parse_ls_tree_line ($;%) {
1185 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1186 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1194 $res{'name'} = unquote($4);
1197 return wantarray ? %res : \%res;
1200 ## ......................................................................
1201 ## parse to array of hashes functions
1203 sub git_get_refs_list {
1204 my $type = shift || "";
1209 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1211 while (my $line = <$fd>) {
1213 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1214 if (defined $refs{$1}) {
1215 push @{$refs{$1}}, $2;
1220 if (! $4) { # unpeeled, direct reference
1221 push @refs, { hash => $1, name => $3 }; # without type
1222 } elsif ($3 eq $refs[-1]{'name'}) {
1223 # most likely a tag is followed by its peeled
1224 # (deref) one, and when that happens we know the
1225 # previous one was of type 'tag'.
1226 $refs[-1]{'type'} = "tag";
1232 foreach my $ref (@refs) {
1233 my $ref_file = $ref->{'name'};
1234 my $ref_id = $ref->{'hash'};
1236 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1237 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1239 push @reflist, \%ref_item;
1242 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1243 return (\@reflist, \%refs);
1246 ## ----------------------------------------------------------------------
1247 ## filesystem-related functions
1249 sub get_file_owner {
1252 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1253 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1254 if (!defined $gcos) {
1258 $owner =~ s/[,;].*$//;
1259 return decode("utf8", $owner, Encode::FB_DEFAULT);
1262 ## ......................................................................
1263 ## mimetype related functions
1265 sub mimetype_guess_file {
1266 my $filename = shift;
1267 my $mimemap = shift;
1268 -r $mimemap or return undef;
1271 open(MIME, $mimemap) or return undef;
1273 next if m/^#/; # skip comments
1274 my ($mime, $exts) = split(/\t+/);
1275 if (defined $exts) {
1276 my @exts = split(/\s+/, $exts);
1277 foreach my $ext (@exts) {
1278 $mimemap{$ext} = $mime;
1284 $filename =~ /\.([^.]*)$/;
1285 return $mimemap{$1};
1288 sub mimetype_guess {
1289 my $filename = shift;
1291 $filename =~ /\./ or return undef;
1293 if ($mimetypes_file) {
1294 my $file = $mimetypes_file;
1295 if ($file !~ m!^/!) { # if it is relative path
1296 # it is relative to project
1297 $file = "$projectroot/$project/$file";
1299 $mime = mimetype_guess_file($filename, $file);
1301 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1307 my $filename = shift;
1310 my $mime = mimetype_guess($filename);
1311 $mime and return $mime;
1315 return $default_blob_plain_mimetype unless $fd;
1318 return 'text/plain' .
1319 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1320 } elsif (! $filename) {
1321 return 'application/octet-stream';
1322 } elsif ($filename =~ m/\.png$/i) {
1324 } elsif ($filename =~ m/\.gif$/i) {
1326 } elsif ($filename =~ m/\.jpe?g$/i) {
1327 return 'image/jpeg';
1329 return 'application/octet-stream';
1333 ## ======================================================================
1334 ## functions printing HTML: header, footer, error page
1336 sub git_header_html {
1337 my $status = shift || "200 OK";
1338 my $expires = shift;
1340 my $title = "$site_name git";
1341 if (defined $project) {
1342 $title .= " - $project";
1343 if (defined $action) {
1344 $title .= "/$action";
1345 if (defined $file_name) {
1346 $title .= " - " . esc_html($file_name);
1347 if ($action eq "tree" && $file_name !~ m|/$|) {
1354 # require explicit support from the UA if we are to send the page as
1355 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1356 # we have to do this because MSIE sometimes globs '*/*', pretending to
1357 # support xhtml+xml but choking when it gets what it asked for.
1358 if (defined $cgi->http('HTTP_ACCEPT') &&
1359 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1360 $cgi->Accept('application/xhtml+xml') != 0) {
1361 $content_type = 'application/xhtml+xml';
1363 $content_type = 'text/html';
1365 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1366 -status=> $status, -expires => $expires);
1368 <?xml version="1.0" encoding="utf-8"?>
1369 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1370 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1371 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1372 <!-- git core binaries version $git_version -->
1374 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1375 <meta name="generator" content="gitweb/$version git/$git_version"/>
1376 <meta name="robots" content="index, nofollow"/>
1377 <title>$title</title>
1378 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1380 if (defined $project) {
1381 printf('<link rel="alternate" title="%s log" '.
1382 'href="%s" type="application/rss+xml"/>'."\n",
1383 esc_param($project), href(action=>"rss"));
1385 printf('<link rel="alternate" title="%s projects list" '.
1386 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1387 $site_name, href(project=>undef, action=>"project_index"));
1388 printf('<link rel="alternate" title="%s projects logs" '.
1389 'href="%s" type="text/x-opml"/>'."\n",
1390 $site_name, href(project=>undef, action=>"opml"));
1392 if (defined $favicon) {
1393 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1398 "<div class=\"page_header\">\n" .
1399 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1400 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1402 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1403 if (defined $project) {
1404 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1405 if (defined $action) {
1409 if (!defined $searchtext) {
1413 if (defined $hash_base) {
1414 $search_hash = $hash_base;
1415 } elsif (defined $hash) {
1416 $search_hash = $hash;
1418 $search_hash = "HEAD";
1420 $cgi->param("a", "search");
1421 $cgi->param("h", $search_hash);
1422 print $cgi->startform(-method => "get", -action => $my_uri) .
1423 "<div class=\"search\">\n" .
1424 $cgi->hidden(-name => "p") . "\n" .
1425 $cgi->hidden(-name => "a") . "\n" .
1426 $cgi->hidden(-name => "h") . "\n" .
1427 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1429 $cgi->end_form() . "\n";
1434 sub git_footer_html {
1435 print "<div class=\"page_footer\">\n";
1436 if (defined $project) {
1437 my $descr = git_get_project_description($project);
1438 if (defined $descr) {
1439 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1441 print $cgi->a({-href => href(action=>"rss"),
1442 -class => "rss_logo"}, "RSS") . "\n";
1444 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1445 -class => "rss_logo"}, "OPML") . " ";
1446 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1447 -class => "rss_logo"}, "TXT") . "\n";
1455 my $status = shift || "403 Forbidden";
1456 my $error = shift || "Malformed query, file missing or permission denied";
1458 git_header_html($status);
1460 <div class="page_body">
1470 ## ----------------------------------------------------------------------
1471 ## functions printing or outputting HTML: navigation
1473 sub git_print_page_nav {
1474 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1475 $extra = '' if !defined $extra; # pager or formats
1477 my @navs = qw(summary shortlog log commit commitdiff tree);
1479 @navs = grep { $_ ne $suppress } @navs;
1482 my %arg = map { $_ => {action=>$_} } @navs;
1483 if (defined $head) {
1484 for (qw(commit commitdiff)) {
1485 $arg{$_}{hash} = $head;
1487 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1488 for (qw(shortlog log)) {
1489 $arg{$_}{hash} = $head;
1493 $arg{tree}{hash} = $treehead if defined $treehead;
1494 $arg{tree}{hash_base} = $treebase if defined $treebase;
1496 print "<div class=\"page_nav\">\n" .
1498 map { $_ eq $current ?
1499 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1501 print "<br/>\n$extra<br/>\n" .
1505 sub format_paging_nav {
1506 my ($action, $hash, $head, $page, $nrevs) = @_;
1510 if ($hash ne $head || $page) {
1511 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1513 $paging_nav .= "HEAD";
1517 $paging_nav .= " ⋅ " .
1518 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1519 -accesskey => "p", -title => "Alt-p"}, "prev");
1521 $paging_nav .= " ⋅ prev";
1524 if ($nrevs >= (100 * ($page+1)-1)) {
1525 $paging_nav .= " ⋅ " .
1526 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1527 -accesskey => "n", -title => "Alt-n"}, "next");
1529 $paging_nav .= " ⋅ next";
1535 ## ......................................................................
1536 ## functions printing or outputting HTML: div
1538 sub git_print_header_div {
1539 my ($action, $title, $hash, $hash_base) = @_;
1542 $args{action} = $action;
1543 $args{hash} = $hash if $hash;
1544 $args{hash_base} = $hash_base if $hash_base;
1546 print "<div class=\"header\">\n" .
1547 $cgi->a({-href => href(%args), -class => "title"},
1548 $title ? $title : $action) .
1552 #sub git_print_authorship (\%) {
1553 sub git_print_authorship {
1556 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1557 print "<div class=\"author_date\">" .
1558 esc_html($co->{'author_name'}) .
1560 if ($ad{'hour_local'} < 6) {
1561 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1562 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1564 printf(" (%02d:%02d %s)",
1565 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1570 sub git_print_page_path {
1575 if (!defined $name) {
1576 print "<div class=\"page_path\">/</div>\n";
1578 my @dirname = split '/', $name;
1579 my $basename = pop @dirname;
1582 print "<div class=\"page_path\">";
1583 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1584 -title => 'tree root'}, "[$project]");
1586 foreach my $dir (@dirname) {
1587 $fullname .= ($fullname ? '/' : '') . $dir;
1588 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1590 -title => $fullname}, esc_html($dir));
1593 if (defined $type && $type eq 'blob') {
1594 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1596 -title => $name}, esc_html($basename));
1597 } elsif (defined $type && $type eq 'tree') {
1598 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1600 -title => $name}, esc_html($basename));
1602 print esc_html($basename);
1604 print "<br/></div>\n";
1608 # sub git_print_log (\@;%) {
1609 sub git_print_log ($;%) {
1613 if ($opts{'-remove_title'}) {
1614 # remove title, i.e. first line of log
1617 # remove leading empty lines
1618 while (defined $log->[0] && $log->[0] eq "") {
1625 foreach my $line (@$log) {
1626 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1629 if (! $opts{'-remove_signoff'}) {
1630 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1633 # remove signoff lines
1640 # print only one empty line
1641 # do not print empty line after signoff
1643 next if ($empty || $signoff);
1649 print format_log_line_html($line) . "<br/>\n";
1652 if ($opts{'-final_empty_line'}) {
1653 # end with single empty line
1654 print "<br/>\n" unless $empty;
1658 sub git_print_simplified_log {
1660 my $remove_title = shift;
1663 -final_empty_line=> 1,
1664 -remove_title => $remove_title);
1667 # print tree entry (row of git_tree), but without encompassing <tr> element
1668 sub git_print_tree_entry {
1669 my ($t, $basedir, $hash_base, $have_blame) = @_;
1672 $base_key{hash_base} = $hash_base if defined $hash_base;
1674 # The format of a table row is: mode list link. Where mode is
1675 # the mode of the entry, list is the name of the entry, an href,
1676 # and link is the action links of the entry.
1678 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1679 if ($t->{'type'} eq "blob") {
1680 print "<td class=\"list\">" .
1681 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1682 file_name=>"$basedir$t->{'name'}", %base_key),
1683 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1684 print "<td class=\"link\">";
1686 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1687 file_name=>"$basedir$t->{'name'}", %base_key)},
1690 if (defined $hash_base) {
1694 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1695 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1699 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1700 file_name=>"$basedir$t->{'name'}")},
1704 } elsif ($t->{'type'} eq "tree") {
1705 print "<td class=\"list\">";
1706 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1707 file_name=>"$basedir$t->{'name'}", %base_key)},
1708 esc_html($t->{'name'}));
1710 print "<td class=\"link\">";
1711 if (defined $hash_base) {
1712 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1713 file_name=>"$basedir$t->{'name'}")},
1720 ## ......................................................................
1721 ## functions printing large fragments of HTML
1723 sub git_difftree_body {
1724 my ($difftree, $hash, $parent) = @_;
1726 print "<div class=\"list_head\">\n";
1727 if ($#{$difftree} > 10) {
1728 print(($#{$difftree} + 1) . " files changed:\n");
1732 print "<table class=\"diff_tree\">\n";
1735 foreach my $line (@{$difftree}) {
1736 my %diff = parse_difftree_raw_line($line);
1739 print "<tr class=\"dark\">\n";
1741 print "<tr class=\"light\">\n";
1745 my ($to_mode_oct, $to_mode_str, $to_file_type);
1746 my ($from_mode_oct, $from_mode_str, $from_file_type);
1747 if ($diff{'to_mode'} ne ('0' x 6)) {
1748 $to_mode_oct = oct $diff{'to_mode'};
1749 if (S_ISREG($to_mode_oct)) { # only for regular file
1750 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1752 $to_file_type = file_type($diff{'to_mode'});
1754 if ($diff{'from_mode'} ne ('0' x 6)) {
1755 $from_mode_oct = oct $diff{'from_mode'};
1756 if (S_ISREG($to_mode_oct)) { # only for regular file
1757 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1759 $from_file_type = file_type($diff{'from_mode'});
1762 if ($diff{'status'} eq "A") { # created
1763 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1764 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1765 $mode_chng .= "]</span>";
1767 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1768 hash_base=>$hash, file_name=>$diff{'file'}),
1769 -class => "list"}, esc_html($diff{'file'}));
1771 print "<td>$mode_chng</td>\n";
1772 print "<td class=\"link\">";
1773 if ($action eq 'commitdiff') {
1776 print $cgi->a({-href => "#patch$patchno"}, "patch");
1780 } elsif ($diff{'status'} eq "D") { # deleted
1781 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1783 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1784 hash_base=>$parent, file_name=>$diff{'file'}),
1785 -class => "list"}, esc_html($diff{'file'}));
1787 print "<td>$mode_chng</td>\n";
1788 print "<td class=\"link\">";
1789 if ($action eq 'commitdiff') {
1792 print $cgi->a({-href => "#patch$patchno"}, "patch");
1795 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1796 file_name=>$diff{'file'})},
1798 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1799 file_name=>$diff{'file'})},
1803 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1804 my $mode_chnge = "";
1805 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1806 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1807 if ($from_file_type != $to_file_type) {
1808 $mode_chnge .= " from $from_file_type to $to_file_type";
1810 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1811 if ($from_mode_str && $to_mode_str) {
1812 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1813 } elsif ($to_mode_str) {
1814 $mode_chnge .= " mode: $to_mode_str";
1817 $mode_chnge .= "]</span>\n";
1820 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1821 hash_base=>$hash, file_name=>$diff{'file'}),
1822 -class => "list"}, esc_html($diff{'file'}));
1824 print "<td>$mode_chnge</td>\n";
1825 print "<td class=\"link\">";
1826 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1827 if ($action eq 'commitdiff') {
1830 print $cgi->a({-href => "#patch$patchno"}, "patch");
1832 print $cgi->a({-href => href(action=>"blobdiff",
1833 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1834 hash_base=>$hash, hash_parent_base=>$parent,
1835 file_name=>$diff{'file'})},
1840 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1841 file_name=>$diff{'file'})},
1843 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1844 file_name=>$diff{'file'})},
1848 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1849 my %status_name = ('R' => 'moved', 'C' => 'copied');
1850 my $nstatus = $status_name{$diff{'status'}};
1852 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1853 # mode also for directories, so we cannot use $to_mode_str
1854 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1857 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1858 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1859 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1860 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1861 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1862 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1863 -class => "list"}, esc_html($diff{'from_file'})) .
1864 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1865 "<td class=\"link\">";
1866 if ($diff{'to_id'} ne $diff{'from_id'}) {
1867 if ($action eq 'commitdiff') {
1870 print $cgi->a({-href => "#patch$patchno"}, "patch");
1872 print $cgi->a({-href => href(action=>"blobdiff",
1873 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1874 hash_base=>$hash, hash_parent_base=>$parent,
1875 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1880 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1881 file_name=>$diff{'from_file'})},
1883 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1884 file_name=>$diff{'from_file'})},
1888 } # we should not encounter Unmerged (U) or Unknown (X) status
1894 sub git_patchset_body {
1895 my ($fd, $difftree, $hash, $hash_parent) = @_;
1899 my $patch_found = 0;
1902 print "<div class=\"patchset\">\n";
1905 while (my $patch_line = <$fd>) {
1908 if ($patch_line =~ m/^diff /) { # "git diff" header
1909 # beginning of patch (in patchset)
1911 # close previous patch
1912 print "</div>\n"; # class="patch"
1914 # first patch in patchset
1917 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1919 if (ref($difftree->[$patch_idx]) eq "HASH") {
1920 $diffinfo = $difftree->[$patch_idx];
1922 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1926 # for now, no extended header, hence we skip empty patches
1927 # companion to next LINE if $in_header;
1928 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1933 if ($diffinfo->{'status'} eq "A") { # added
1934 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1935 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1936 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1937 $diffinfo->{'to_id'}) . "(new)" .
1938 "</div>\n"; # class="diff_info"
1940 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1941 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1942 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1943 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1944 $diffinfo->{'from_id'}) . "(deleted)" .
1945 "</div>\n"; # class="diff_info"
1947 } elsif ($diffinfo->{'status'} eq "R" || # renamed
1948 $diffinfo->{'status'} eq "C" || # copied
1949 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1950 print "<div class=\"diff_info\">" .
1951 file_type($diffinfo->{'from_mode'}) . ":" .
1952 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1953 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1954 $diffinfo->{'from_id'}) .
1956 file_type($diffinfo->{'to_mode'}) . ":" .
1957 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1958 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1959 $diffinfo->{'to_id'});
1960 print "</div>\n"; # class="diff_info"
1962 } else { # modified, mode changed, ...
1963 print "<div class=\"diff_info\">" .
1964 file_type($diffinfo->{'from_mode'}) . ":" .
1965 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1966 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1967 $diffinfo->{'from_id'}) .
1969 file_type($diffinfo->{'to_mode'}) . ":" .
1970 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1971 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1972 $diffinfo->{'to_id'});
1973 print "</div>\n"; # class="diff_info"
1976 #print "<div class=\"diff extended_header\">\n";
1979 } # start of patch in patchset
1982 if ($in_header && $patch_line =~ m/^---/) {
1983 #print "</div>\n"; # class="diff extended_header"
1986 my $file = $diffinfo->{'from_file'};
1987 $file ||= $diffinfo->{'file'};
1988 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1989 hash=>$diffinfo->{'from_id'}, file_name=>$file),
1990 -class => "list"}, esc_html($file));
1991 $patch_line =~ s|a/.*$|a/$file|g;
1992 print "<div class=\"diff from_file\">$patch_line</div>\n";
1994 $patch_line = <$fd>;
1997 #$patch_line =~ m/^+++/;
1998 $file = $diffinfo->{'to_file'};
1999 $file ||= $diffinfo->{'file'};
2000 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2001 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2002 -class => "list"}, esc_html($file));
2003 $patch_line =~ s|b/.*|b/$file|g;
2004 print "<div class=\"diff to_file\">$patch_line</div>\n";
2008 next LINE if $in_header;
2010 print format_diff_line($patch_line);
2012 print "</div>\n" if $patch_found; # class="patch"
2014 print "</div>\n"; # class="patchset"
2017 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2019 sub git_shortlog_body {
2020 # uses global variable $project
2021 my ($revlist, $from, $to, $refs, $extra) = @_;
2023 $from = 0 unless defined $from;
2024 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2026 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2028 for (my $i = $from; $i <= $to; $i++) {
2029 my $commit = $revlist->[$i];
2030 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2031 my $ref = format_ref_marker($refs, $commit);
2032 my %co = parse_commit($commit);
2034 print "<tr class=\"dark\">\n";
2036 print "<tr class=\"light\">\n";
2039 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2040 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2041 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2043 print format_subject_html($co{'title'}, $co{'title_short'},
2044 href(action=>"commit", hash=>$commit), $ref);
2046 "<td class=\"link\">" .
2047 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2048 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") . " | " .
2049 $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2053 if (defined $extra) {
2055 "<td colspan=\"4\">$extra</td>\n" .
2061 sub git_history_body {
2062 # Warning: assumes constant type (blob or tree) during history
2063 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2065 $from = 0 unless defined $from;
2066 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2068 print "<table class=\"history\" cellspacing=\"0\">\n";
2070 for (my $i = $from; $i <= $to; $i++) {
2071 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2076 my %co = parse_commit($commit);
2081 my $ref = format_ref_marker($refs, $commit);
2084 print "<tr class=\"dark\">\n";
2086 print "<tr class=\"light\">\n";
2089 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2090 # shortlog uses chop_str($co{'author_name'}, 10)
2091 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2093 # originally git_history used chop_str($co{'title'}, 50)
2094 print format_subject_html($co{'title'}, $co{'title_short'},
2095 href(action=>"commit", hash=>$commit), $ref);
2097 "<td class=\"link\">" .
2098 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2099 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2101 if ($ftype eq 'blob') {
2102 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2103 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2104 if (defined $blob_current && defined $blob_parent &&
2105 $blob_current ne $blob_parent) {
2107 $cgi->a({-href => href(action=>"blobdiff",
2108 hash=>$blob_current, hash_parent=>$blob_parent,
2109 hash_base=>$hash_base, hash_parent_base=>$commit,
2110 file_name=>$file_name)},
2117 if (defined $extra) {
2119 "<td colspan=\"4\">$extra</td>\n" .
2126 # uses global variable $project
2127 my ($taglist, $from, $to, $extra) = @_;
2128 $from = 0 unless defined $from;
2129 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2131 print "<table class=\"tags\" cellspacing=\"0\">\n";
2133 for (my $i = $from; $i <= $to; $i++) {
2134 my $entry = $taglist->[$i];
2136 my $comment_lines = $tag{'comment'};
2137 my $comment = shift @$comment_lines;
2139 if (defined $comment) {
2140 $comment_short = chop_str($comment, 30, 5);
2143 print "<tr class=\"dark\">\n";
2145 print "<tr class=\"light\">\n";
2148 print "<td><i>$tag{'age'}</i></td>\n" .
2150 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2151 -class => "list name"}, esc_html($tag{'name'})) .
2154 if (defined $comment) {
2155 print format_subject_html($comment, $comment_short,
2156 href(action=>"tag", hash=>$tag{'id'}));
2159 "<td class=\"selflink\">";
2160 if ($tag{'type'} eq "tag") {
2161 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2166 "<td class=\"link\">" . " | " .
2167 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2168 if ($tag{'reftype'} eq "commit") {
2169 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2170 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2171 } elsif ($tag{'reftype'} eq "blob") {
2172 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2177 if (defined $extra) {
2179 "<td colspan=\"5\">$extra</td>\n" .
2185 sub git_heads_body {
2186 # uses global variable $project
2187 my ($headlist, $head, $from, $to, $extra) = @_;
2188 $from = 0 unless defined $from;
2189 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2191 print "<table class=\"heads\" cellspacing=\"0\">\n";
2193 for (my $i = $from; $i <= $to; $i++) {
2194 my $entry = $headlist->[$i];
2196 my $curr = $tag{'id'} eq $head;
2198 print "<tr class=\"dark\">\n";
2200 print "<tr class=\"light\">\n";
2203 print "<td><i>$tag{'age'}</i></td>\n" .
2204 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2205 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2206 -class => "list name"},esc_html($tag{'name'})) .
2208 "<td class=\"link\">" .
2209 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2210 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2211 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2215 if (defined $extra) {
2217 "<td colspan=\"3\">$extra</td>\n" .
2223 ## ======================================================================
2224 ## ======================================================================
2227 sub git_project_list {
2228 my $order = $cgi->param('o');
2229 if (defined $order && $order !~ m/project|descr|owner|age/) {
2230 die_error(undef, "Unknown order parameter");
2233 my @list = git_get_projects_list();
2236 die_error(undef, "No projects found");
2238 foreach my $pr (@list) {
2239 my $head = git_get_head_hash($pr->{'path'});
2240 if (!defined $head) {
2243 $git_dir = "$projectroot/$pr->{'path'}";
2244 my %co = parse_commit($head);
2248 $pr->{'commit'} = \%co;
2249 if (!defined $pr->{'descr'}) {
2250 my $descr = git_get_project_description($pr->{'path'}) || "";
2251 $pr->{'descr'} = chop_str($descr, 25, 5);
2253 if (!defined $pr->{'owner'}) {
2254 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2256 push @projects, $pr;
2260 if (-f $home_text) {
2261 print "<div class=\"index_include\">\n";
2262 open (my $fd, $home_text);
2267 print "<table class=\"project_list\">\n" .
2269 $order ||= "project";
2270 if ($order eq "project") {
2271 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2272 print "<th>Project</th>\n";
2275 $cgi->a({-href => href(project=>undef, order=>'project'),
2276 -class => "header"}, "Project") .
2279 if ($order eq "descr") {
2280 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2281 print "<th>Description</th>\n";
2284 $cgi->a({-href => href(project=>undef, order=>'descr'),
2285 -class => "header"}, "Description") .
2288 if ($order eq "owner") {
2289 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2290 print "<th>Owner</th>\n";
2293 $cgi->a({-href => href(project=>undef, order=>'owner'),
2294 -class => "header"}, "Owner") .
2297 if ($order eq "age") {
2298 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2299 print "<th>Last Change</th>\n";
2302 $cgi->a({-href => href(project=>undef, order=>'age'),
2303 -class => "header"}, "Last Change") .
2306 print "<th></th>\n" .
2309 foreach my $pr (@projects) {
2311 print "<tr class=\"dark\">\n";
2313 print "<tr class=\"light\">\n";
2316 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2317 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2318 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2319 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2320 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2321 $pr->{'commit'}{'age_string'} . "</td>\n" .
2322 "<td class=\"link\">" .
2323 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2324 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2325 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2326 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2334 sub git_project_index {
2335 my @projects = git_get_projects_list();
2338 -type => 'text/plain',
2339 -charset => 'utf-8',
2340 -content_disposition => 'inline; filename="index.aux"');
2342 foreach my $pr (@projects) {
2343 if (!exists $pr->{'owner'}) {
2344 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2347 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2348 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2349 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2350 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2354 print "$path $owner\n";
2359 my $descr = git_get_project_description($project) || "none";
2360 my $head = git_get_head_hash($project);
2361 my %co = parse_commit($head);
2362 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2364 my $owner = git_get_project_owner($project);
2366 my ($reflist, $refs) = git_get_refs_list();
2370 foreach my $ref (@$reflist) {
2371 if ($ref->{'name'} =~ s!^heads/!!) {
2372 push @headlist, $ref;
2374 $ref->{'name'} =~ s!^tags/!!;
2375 push @taglist, $ref;
2380 git_print_page_nav('summary','', $head);
2382 print "<div class=\"title\"> </div>\n";
2383 print "<table cellspacing=\"0\">\n" .
2384 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2385 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2386 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2387 # use per project git URL list in $projectroot/$project/cloneurl
2388 # or make project git URL from git base URL and project name
2389 my $url_tag = "URL";
2390 my @url_list = git_get_project_url_list($project);
2391 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2392 foreach my $git_url (@url_list) {
2393 next unless $git_url;
2394 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2399 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2400 git_get_head_hash($project)
2401 or die_error(undef, "Open git-rev-list failed");
2402 my @revlist = map { chomp; $_ } <$fd>;
2404 git_print_header_div('shortlog');
2405 git_shortlog_body(\@revlist, 0, 15, $refs,
2406 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2409 git_print_header_div('tags');
2410 git_tags_body(\@taglist, 0, 15,
2411 $cgi->a({-href => href(action=>"tags")}, "..."));
2415 git_print_header_div('heads');
2416 git_heads_body(\@headlist, $head, 0, 15,
2417 $cgi->a({-href => href(action=>"heads")}, "..."));
2424 my $head = git_get_head_hash($project);
2426 git_print_page_nav('','', $head,undef,$head);
2427 my %tag = parse_tag($hash);
2428 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2429 print "<div class=\"title_text\">\n" .
2430 "<table cellspacing=\"0\">\n" .
2432 "<td>object</td>\n" .
2433 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2434 $tag{'object'}) . "</td>\n" .
2435 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2436 $tag{'type'}) . "</td>\n" .
2438 if (defined($tag{'author'})) {
2439 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2440 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2441 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2442 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2445 print "</table>\n\n" .
2447 print "<div class=\"page_body\">";
2448 my $comment = $tag{'comment'};
2449 foreach my $line (@$comment) {
2450 print esc_html($line) . "<br/>\n";
2460 my ($have_blame) = gitweb_check_feature('blame');
2462 die_error('403 Permission denied', "Permission denied");
2464 die_error('404 Not Found', "File name not defined") if (!$file_name);
2465 $hash_base ||= git_get_head_hash($project);
2466 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2467 my %co = parse_commit($hash_base)
2468 or die_error(undef, "Reading commit failed");
2469 if (!defined $hash) {
2470 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2471 or die_error(undef, "Error looking up file");
2473 $ftype = git_get_type($hash);
2474 if ($ftype !~ "blob") {
2475 die_error("400 Bad Request", "Object is not a blob");
2477 open ($fd, "-|", git_cmd(), "blame", '-l', '--', $file_name, $hash_base)
2478 or die_error(undef, "Open git-blame failed");
2481 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2484 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2487 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2489 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2490 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2491 git_print_page_path($file_name, $ftype, $hash_base);
2492 my @rev_color = (qw(light2 dark2));
2493 my $num_colors = scalar(@rev_color);
2494 my $current_color = 0;
2497 <div class="page_body">
2498 <table class="blame">
2499 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2502 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2504 my $rev = substr($full_rev, 0, 8);
2508 if (!defined $last_rev) {
2509 $last_rev = $full_rev;
2510 } elsif ($last_rev ne $full_rev) {
2511 $last_rev = $full_rev;
2512 $current_color = ++$current_color % $num_colors;
2514 print "<tr class=\"$rev_color[$current_color]\">\n";
2515 print "<td class=\"sha1\">" .
2516 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2517 esc_html($rev)) . "</td>\n";
2518 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2519 esc_html($lineno) . "</a></td>\n";
2520 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2526 or print "Reading blob failed\n";
2533 my ($have_blame) = gitweb_check_feature('blame');
2535 die_error('403 Permission denied', "Permission denied");
2537 die_error('404 Not Found', "File name not defined") if (!$file_name);
2538 $hash_base ||= git_get_head_hash($project);
2539 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2540 my %co = parse_commit($hash_base)
2541 or die_error(undef, "Reading commit failed");
2542 if (!defined $hash) {
2543 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2544 or die_error(undef, "Error lookup file");
2546 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2547 or die_error(undef, "Open git-annotate failed");
2550 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2553 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2556 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2558 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2559 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2560 git_print_page_path($file_name, 'blob', $hash_base);
2561 print "<div class=\"page_body\">\n";
2563 <table class="blame">
2572 my @line_class = (qw(light dark));
2573 my $line_class_len = scalar (@line_class);
2574 my $line_class_num = $#line_class;
2575 while (my $line = <$fd>) {
2587 $line_class_num = ($line_class_num + 1) % $line_class_len;
2589 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2596 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2599 $short_rev = substr ($long_rev, 0, 8);
2600 $age = time () - $time;
2601 $age_str = age_string ($age);
2602 $age_str =~ s/ / /g;
2603 $age_class = age_class($age);
2604 $author = esc_html ($author);
2605 $author =~ s/ / /g;
2607 $data = untabify($data);
2608 $data = esc_html ($data);
2611 <tr class="$line_class[$line_class_num]">
2612 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2613 <td class="$age_class">$age_str</td>
2615 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2616 <td class="pre">$data</td>
2619 } # while (my $line = <$fd>)
2620 print "</table>\n\n";
2622 or print "Reading blob failed.\n";
2628 my $head = git_get_head_hash($project);
2630 git_print_page_nav('','', $head,undef,$head);
2631 git_print_header_div('summary', $project);
2633 my ($taglist) = git_get_refs_list("tags");
2635 git_tags_body($taglist);
2641 my $head = git_get_head_hash($project);
2643 git_print_page_nav('','', $head,undef,$head);
2644 git_print_header_div('summary', $project);
2646 my ($headlist) = git_get_refs_list("heads");
2648 git_heads_body($headlist, $head);
2653 sub git_blob_plain {
2656 if (!defined $hash) {
2657 if (defined $file_name) {
2658 my $base = $hash_base || git_get_head_hash($project);
2659 $hash = git_get_hash_by_path($base, $file_name, "blob")
2660 or die_error(undef, "Error lookup file");
2662 die_error(undef, "No file name defined");
2664 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2665 # blobs defined by non-textual hash id's can be cached
2670 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2671 or die_error(undef, "Couldn't cat $file_name, $hash");
2673 $type ||= blob_mimetype($fd, $file_name);
2675 # save as filename, even when no $file_name is given
2676 my $save_as = "$hash";
2677 if (defined $file_name) {
2678 $save_as = $file_name;
2679 } elsif ($type =~ m/^text\//) {
2686 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2688 binmode STDOUT, ':raw';
2690 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2698 if (!defined $hash) {
2699 if (defined $file_name) {
2700 my $base = $hash_base || git_get_head_hash($project);
2701 $hash = git_get_hash_by_path($base, $file_name, "blob")
2702 or die_error(undef, "Error lookup file");
2704 die_error(undef, "No file name defined");
2706 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2707 # blobs defined by non-textual hash id's can be cached
2711 my ($have_blame) = gitweb_check_feature('blame');
2712 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2713 or die_error(undef, "Couldn't cat $file_name, $hash");
2714 my $mimetype = blob_mimetype($fd, $file_name);
2715 if ($mimetype !~ m/^text\//) {
2717 return git_blob_plain($mimetype);
2719 git_header_html(undef, $expires);
2720 my $formats_nav = '';
2721 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2722 if (defined $file_name) {
2725 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2726 hash=>$hash, file_name=>$file_name)},
2731 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2732 hash=>$hash, file_name=>$file_name)},
2735 $cgi->a({-href => href(action=>"blob_plain",
2736 hash=>$hash, file_name=>$file_name)},
2739 $cgi->a({-href => href(action=>"blob",
2740 hash_base=>"HEAD", file_name=>$file_name)},
2744 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2746 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2747 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2749 print "<div class=\"page_nav\">\n" .
2750 "<br/><br/></div>\n" .
2751 "<div class=\"title\">$hash</div>\n";
2753 git_print_page_path($file_name, "blob", $hash_base);
2754 print "<div class=\"page_body\">\n";
2756 while (my $line = <$fd>) {
2759 $line = untabify($line);
2760 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2761 $nr, $nr, $nr, esc_html($line);
2764 or print "Reading blob failed.\n";
2770 my $have_snapshot = gitweb_have_snapshot();
2772 if (!defined $hash_base) {
2773 $hash_base = "HEAD";
2775 if (!defined $hash) {
2776 if (defined $file_name) {
2777 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2783 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2784 or die_error(undef, "Open git-ls-tree failed");
2785 my @entries = map { chomp; $_ } <$fd>;
2786 close $fd or die_error(undef, "Reading tree failed");
2789 my $refs = git_get_references();
2790 my $ref = format_ref_marker($refs, $hash_base);
2793 my ($have_blame) = gitweb_check_feature('blame');
2794 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2796 if (defined $file_name) {
2798 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2799 hash=>$hash, file_name=>$file_name)},
2801 $cgi->a({-href => href(action=>"tree",
2802 hash_base=>"HEAD", file_name=>$file_name)},
2805 if ($have_snapshot) {
2806 # FIXME: Should be available when we have no hash base as well.
2808 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2811 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2812 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2815 print "<div class=\"page_nav\">\n";
2816 print "<br/><br/></div>\n";
2817 print "<div class=\"title\">$hash</div>\n";
2819 if (defined $file_name) {
2820 $base = esc_html("$file_name/");
2822 git_print_page_path($file_name, 'tree', $hash_base);
2823 print "<div class=\"page_body\">\n";
2824 print "<table cellspacing=\"0\">\n";
2826 foreach my $line (@entries) {
2827 my %t = parse_ls_tree_line($line, -z => 1);
2830 print "<tr class=\"dark\">\n";
2832 print "<tr class=\"light\">\n";
2836 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2840 print "</table>\n" .
2846 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2847 my $have_snapshot = (defined $ctype && defined $suffix);
2848 if (!$have_snapshot) {
2849 die_error('403 Permission denied', "Permission denied");
2852 if (!defined $hash) {
2853 $hash = git_get_head_hash($project);
2856 my $filename = basename($project) . "-$hash.tar.$suffix";
2859 -type => 'application/x-tar',
2860 -content_encoding => $ctype,
2861 -content_disposition => 'inline; filename="' . "$filename" . '"',
2862 -status => '200 OK');
2864 my $git_command = git_cmd_str();
2865 open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2866 die_error(undef, "Execute git-tar-tree failed.");
2867 binmode STDOUT, ':raw';
2869 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2875 my $head = git_get_head_hash($project);
2876 if (!defined $hash) {
2879 if (!defined $page) {
2882 my $refs = git_get_references();
2884 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2885 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2886 or die_error(undef, "Open git-rev-list failed");
2887 my @revlist = map { chomp; $_ } <$fd>;
2890 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2893 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2896 my %co = parse_commit($hash);
2898 git_print_header_div('summary', $project);
2899 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2901 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2902 my $commit = $revlist[$i];
2903 my $ref = format_ref_marker($refs, $commit);
2904 my %co = parse_commit($commit);
2906 my %ad = parse_date($co{'author_epoch'});
2907 git_print_header_div('commit',
2908 "<span class=\"age\">$co{'age_string'}</span>" .
2909 esc_html($co{'title'}) . $ref,
2911 print "<div class=\"title_text\">\n" .
2912 "<div class=\"log_link\">\n" .
2913 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2915 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2917 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
2920 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2923 print "<div class=\"log_body\">\n";
2924 git_print_simplified_log($co{'comment'});
2931 my %co = parse_commit($hash);
2933 die_error(undef, "Unknown commit object");
2935 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2936 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2938 my $parent = $co{'parent'};
2939 if (!defined $parent) {
2942 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2943 or die_error(undef, "Open git-diff-tree failed");
2944 my @difftree = map { chomp; $_ } <$fd>;
2945 close $fd or die_error(undef, "Reading git-diff-tree failed");
2947 # non-textual hash id's can be cached
2949 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2952 my $refs = git_get_references();
2953 my $ref = format_ref_marker($refs, $co{'id'});
2955 my $have_snapshot = gitweb_have_snapshot();
2958 if (defined $file_name && defined $co{'parent'}) {
2960 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2963 if (defined $co{'parent'}) {
2965 $cgi->a({-href => href(action=>"shortlog", hash=>$hash)}, "shortlog"),
2966 $cgi->a({-href => href(action=>"log", hash=>$hash)}, "log");
2968 git_header_html(undef, $expires);
2969 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2970 $hash, $co{'tree'}, $hash,
2971 join (' | ', @views_nav));
2973 if (defined $co{'parent'}) {
2974 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2976 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2978 print "<div class=\"title_text\">\n" .
2979 "<table cellspacing=\"0\">\n";
2980 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2982 "<td></td><td> $ad{'rfc2822'}";
2983 if ($ad{'hour_local'} < 6) {
2984 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2985 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2987 printf(" (%02d:%02d %s)",
2988 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2992 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2993 print "<tr><td></td><td> $cd{'rfc2822'}" .
2994 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2996 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2999 "<td class=\"sha1\">" .
3000 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3001 class => "list"}, $co{'tree'}) .
3003 "<td class=\"link\">" .
3004 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3006 if ($have_snapshot) {
3008 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3012 my $parents = $co{'parents'};
3013 foreach my $par (@$parents) {
3016 "<td class=\"sha1\">" .
3017 $cgi->a({-href => href(action=>"commit", hash=>$par),
3018 class => "list"}, $par) .
3020 "<td class=\"link\">" .
3021 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3023 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3030 print "<div class=\"page_body\">\n";
3031 git_print_log($co{'comment'});
3034 git_difftree_body(\@difftree, $hash, $parent);
3040 my $format = shift || 'html';
3047 # preparing $fd and %diffinfo for git_patchset_body
3049 if (defined $hash_base && defined $hash_parent_base) {
3050 if (defined $file_name) {
3052 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3054 or die_error(undef, "Open git-diff-tree failed");
3055 @difftree = map { chomp; $_ } <$fd>;
3057 or die_error(undef, "Reading git-diff-tree failed");
3059 or die_error('404 Not Found', "Blob diff not found");
3061 } elsif (defined $hash &&
3062 $hash =~ /[0-9a-fA-F]{40}/) {
3063 # try to find filename from $hash
3065 # read filtered raw output
3066 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3067 or die_error(undef, "Open git-diff-tree failed");
3069 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3071 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3072 map { chomp; $_ } <$fd>;
3074 or die_error(undef, "Reading git-diff-tree failed");
3076 or die_error('404 Not Found', "Blob diff not found");
3079 die_error('404 Not Found', "Missing one of the blob diff parameters");
3082 if (@difftree > 1) {
3083 die_error('404 Not Found', "Ambiguous blob diff specification");
3086 %diffinfo = parse_difftree_raw_line($difftree[0]);
3087 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3088 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3090 $hash_parent ||= $diffinfo{'from_id'};
3091 $hash ||= $diffinfo{'to_id'};
3093 # non-textual hash id's can be cached
3094 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3095 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3100 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3101 '-p', $hash_parent_base, $hash_base,
3103 or die_error(undef, "Open git-diff-tree failed");
3106 # old/legacy style URI
3107 if (!%diffinfo && # if new style URI failed
3108 defined $hash && defined $hash_parent) {
3109 # fake git-diff-tree raw output
3110 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3111 $diffinfo{'from_id'} = $hash_parent;
3112 $diffinfo{'to_id'} = $hash;
3113 if (defined $file_name) {
3114 if (defined $file_parent) {
3115 $diffinfo{'status'} = '2';
3116 $diffinfo{'from_file'} = $file_parent;
3117 $diffinfo{'to_file'} = $file_name;
3118 } else { # assume not renamed
3119 $diffinfo{'status'} = '1';
3120 $diffinfo{'from_file'} = $file_name;
3121 $diffinfo{'to_file'} = $file_name;
3123 } else { # no filename given
3124 $diffinfo{'status'} = '2';
3125 $diffinfo{'from_file'} = $hash_parent;
3126 $diffinfo{'to_file'} = $hash;
3129 # non-textual hash id's can be cached
3130 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3131 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3136 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3137 or die_error(undef, "Open git-diff failed");
3139 die_error('404 Not Found', "Missing one of the blob diff parameters")
3144 if ($format eq 'html') {
3146 $cgi->a({-href => href(action=>"blobdiff_plain",
3147 hash=>$hash, hash_parent=>$hash_parent,
3148 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3149 file_name=>$file_name, file_parent=>$file_parent)},
3151 git_header_html(undef, $expires);
3152 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3153 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3154 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3156 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3157 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3159 if (defined $file_name) {
3160 git_print_page_path($file_name, "blob", $hash_base);
3162 print "<div class=\"page_path\"></div>\n";
3165 } elsif ($format eq 'plain') {
3167 -type => 'text/plain',
3168 -charset => 'utf-8',
3169 -expires => $expires,
3170 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3172 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3175 die_error(undef, "Unknown blobdiff format");
3179 if ($format eq 'html') {
3180 print "<div class=\"page_body\">\n";
3182 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3185 print "</div>\n"; # class="page_body"
3189 while (my $line = <$fd>) {
3190 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3191 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3195 last if $line =~ m!^\+\+\+!;
3203 sub git_blobdiff_plain {
3204 git_blobdiff('plain');
3207 sub git_commitdiff {
3208 my $format = shift || 'html';
3209 my %co = parse_commit($hash);
3211 die_error(undef, "Unknown commit object");
3213 if (!defined $hash_parent) {
3214 $hash_parent = $co{'parent'} || '--root';
3220 if ($format eq 'html') {
3221 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3222 "--patch-with-raw", "--full-index", $hash_parent, $hash
3223 or die_error(undef, "Open git-diff-tree failed");
3225 while (chomp(my $line = <$fd>)) {
3226 # empty line ends raw part of diff-tree output
3228 push @difftree, $line;
3231 } elsif ($format eq 'plain') {
3232 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3233 '-p', $hash_parent, $hash
3234 or die_error(undef, "Open git-diff-tree failed");
3237 die_error(undef, "Unknown commitdiff format");
3240 # non-textual hash id's can be cached
3242 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3246 # write commit message
3247 if ($format eq 'html') {
3248 my $refs = git_get_references();
3249 my $ref = format_ref_marker($refs, $co{'id'});
3251 $cgi->a({-href => href(action=>"commitdiff_plain",
3252 hash=>$hash, hash_parent=>$hash_parent)},
3255 git_header_html(undef, $expires);
3256 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3257 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3258 git_print_authorship(\%co);
3259 print "<div class=\"page_body\">\n";
3260 print "<div class=\"log\">\n";
3261 git_print_simplified_log($co{'comment'}, 1); # skip title
3262 print "</div>\n"; # class="log"
3264 } elsif ($format eq 'plain') {
3265 my $refs = git_get_references("tags");
3266 my $tagname = git_get_rev_name_tags($hash);
3267 my $filename = basename($project) . "-$hash.patch";
3270 -type => 'text/plain',
3271 -charset => 'utf-8',
3272 -expires => $expires,
3273 -content_disposition => 'inline; filename="' . "$filename" . '"');
3274 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3277 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3278 Subject: $co{'title'}
3280 print "X-Git-Tag: $tagname\n" if $tagname;
3281 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3283 foreach my $line (@{$co{'comment'}}) {
3290 if ($format eq 'html') {
3291 git_difftree_body(\@difftree, $hash, $hash_parent);
3294 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3296 print "</div>\n"; # class="page_body"
3299 } elsif ($format eq 'plain') {
3303 or print "Reading git-diff-tree failed\n";
3307 sub git_commitdiff_plain {
3308 git_commitdiff('plain');
3312 if (!defined $hash_base) {
3313 $hash_base = git_get_head_hash($project);
3315 if (!defined $page) {
3319 my %co = parse_commit($hash_base);
3321 die_error(undef, "Unknown commit object");
3324 my $refs = git_get_references();
3325 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3327 if (!defined $hash && defined $file_name) {
3328 $hash = git_get_hash_by_path($hash_base, $file_name);
3330 if (defined $hash) {
3331 $ftype = git_get_type($hash);
3335 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3336 or die_error(undef, "Open git-rev-list-failed");
3337 my @revlist = map { chomp; $_ } <$fd>;
3339 or die_error(undef, "Reading git-rev-list failed");
3341 my $paging_nav = '';
3344 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3345 file_name=>$file_name)},
3347 $paging_nav .= " ⋅ " .
3348 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3349 file_name=>$file_name, page=>$page-1),
3350 -accesskey => "p", -title => "Alt-p"}, "prev");
3352 $paging_nav .= "first";
3353 $paging_nav .= " ⋅ prev";
3355 if ($#revlist >= (100 * ($page+1)-1)) {
3356 $paging_nav .= " ⋅ " .
3357 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3358 file_name=>$file_name, page=>$page+1),
3359 -accesskey => "n", -title => "Alt-n"}, "next");
3361 $paging_nav .= " ⋅ next";
3364 if ($#revlist >= (100 * ($page+1)-1)) {
3366 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3367 file_name=>$file_name, page=>$page+1),
3368 -title => "Alt-n"}, "next");
3372 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3373 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3374 git_print_page_path($file_name, $ftype, $hash_base);
3376 git_history_body(\@revlist, ($page * 100), $#revlist,
3377 $refs, $hash_base, $ftype, $next_link);
3383 if (!defined $searchtext) {
3384 die_error(undef, "Text field empty");
3386 if (!defined $hash) {
3387 $hash = git_get_head_hash($project);
3389 my %co = parse_commit($hash);
3391 die_error(undef, "Unknown commit object");
3394 my $commit_search = 1;
3395 my $author_search = 0;
3396 my $committer_search = 0;
3397 my $pickaxe_search = 0;
3398 if ($searchtext =~ s/^author\\://i) {
3400 } elsif ($searchtext =~ s/^committer\\://i) {
3401 $committer_search = 1;
3402 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3404 $pickaxe_search = 1;
3406 # pickaxe may take all resources of your box and run for several minutes
3407 # with every query - so decide by yourself how public you make this feature
3408 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3409 if (!$have_pickaxe) {
3410 die_error('403 Permission denied', "Permission denied");
3414 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3415 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3417 print "<table cellspacing=\"0\">\n";
3419 if ($commit_search) {
3421 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3422 while (my $commit_text = <$fd>) {
3423 if (!grep m/$searchtext/i, $commit_text) {
3426 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3429 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3432 my @commit_lines = split "\n", $commit_text;
3433 my %co = parse_commit(undef, \@commit_lines);
3438 print "<tr class=\"dark\">\n";
3440 print "<tr class=\"light\">\n";
3443 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3444 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3446 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3447 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3448 my $comment = $co{'comment'};
3449 foreach my $line (@$comment) {
3450 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3451 my $lead = esc_html($1) || "";
3452 $lead = chop_str($lead, 30, 10);
3453 my $match = esc_html($2) || "";
3454 my $trail = esc_html($3) || "";
3455 $trail = chop_str($trail, 30, 10);
3456 my $text = "$lead<span class=\"match\">$match</span>$trail";
3457 print chop_str($text, 80, 5) . "<br/>\n";
3461 "<td class=\"link\">" .
3462 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3464 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3471 if ($pickaxe_search) {
3473 my $git_command = git_cmd_str();
3474 open my $fd, "-|", "$git_command rev-list $hash | " .
3475 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3478 while (my $line = <$fd>) {
3479 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3482 $set{'from_id'} = $3;
3484 $set{'id'} = $set{'to_id'};
3485 if ($set{'id'} =~ m/0{40}/) {
3486 $set{'id'} = $set{'from_id'};
3488 if ($set{'id'} =~ m/0{40}/) {
3492 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3495 print "<tr class=\"dark\">\n";
3497 print "<tr class=\"light\">\n";
3500 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3501 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3503 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3504 -class => "list subject"},
3505 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3506 while (my $setref = shift @files) {
3508 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3509 hash=>$set{'id'}, file_name=>$set{'file'}),
3511 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3515 "<td class=\"link\">" .
3516 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3518 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3522 %co = parse_commit($1);
3532 my $head = git_get_head_hash($project);
3533 if (!defined $hash) {
3536 if (!defined $page) {
3539 my $refs = git_get_references();
3541 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3542 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3543 or die_error(undef, "Open git-rev-list failed");
3544 my @revlist = map { chomp; $_ } <$fd>;
3547 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3549 if ($#revlist >= (100 * ($page+1)-1)) {
3551 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3552 -title => "Alt-n"}, "next");
3557 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3558 git_print_header_div('summary', $project);
3560 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3565 ## ......................................................................
3566 ## feeds (RSS, OPML)
3569 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3570 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3571 or die_error(undef, "Open git-rev-list failed");
3572 my @revlist = map { chomp; $_ } <$fd>;
3573 close $fd or die_error(undef, "Reading git-rev-list failed");
3574 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3576 <?xml version="1.0" encoding="utf-8"?>
3577 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3579 <title>$project $my_uri $my_url</title>
3580 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3581 <description>$project log</description>
3582 <language>en</language>
3585 for (my $i = 0; $i <= $#revlist; $i++) {
3586 my $commit = $revlist[$i];
3587 my %co = parse_commit($commit);
3588 # we read 150, we always show 30 and the ones more recent than 48 hours
3589 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3592 my %cd = parse_date($co{'committer_epoch'});
3593 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3594 $co{'parent'}, $co{'id'}
3596 my @difftree = map { chomp; $_ } <$fd>;
3601 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3603 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3604 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3605 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3606 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3607 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3608 "<content:encoded>" .
3610 my $comment = $co{'comment'};
3611 foreach my $line (@$comment) {
3612 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3613 print "$line<br/>\n";
3616 foreach my $line (@difftree) {
3617 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3620 my $file = esc_html(unquote($7));
3621 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3622 print "$file<br/>\n";
3625 "</content:encoded>\n" .
3628 print "</channel></rss>";
3632 my @list = git_get_projects_list();
3634 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3636 <?xml version="1.0" encoding="utf-8"?>
3637 <opml version="1.0">
3639 <title>$site_name Git OPML Export</title>
3642 <outline text="git RSS feeds">
3645 foreach my $pr (@list) {
3647 my $head = git_get_head_hash($proj{'path'});
3648 if (!defined $head) {
3651 $git_dir = "$projectroot/$proj{'path'}";
3652 my %co = parse_commit($head);
3657 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3658 my $rss = "$my_url?p=$proj{'path'};a=rss";
3659 my $html = "$my_url?p=$proj{'path'};a=summary";
3660 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";