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++";
49 # URI of GIT logo (72x27 size)
50 our $logo = "++GITWEB_LOGO++";
51 # URI of GIT favicon, assumed to be image/png type
52 our $favicon = "++GITWEB_FAVICON++";
54 # URI and label (title) of GIT logo link
55 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
56 #our $logo_label = "git documentation";
57 our $logo_url = "http://git.or.cz/";
58 our $logo_label = "git homepage";
60 # source of projects list
61 our $projects_list = "++GITWEB_LIST++";
63 # show repository only if this file exists
64 # (only effective if this variable evaluates to true)
65 our $export_ok = "++GITWEB_EXPORT_OK++";
67 # only allow viewing of repositories also shown on the overview page
68 our $strict_export = "++GITWEB_STRICT_EXPORT++";
70 # list of git base URLs used for URL to where fetch project from,
71 # i.e. full URL is "$git_base_url/$project"
72 our @git_base_url_list = ("++GITWEB_BASE_URL++");
74 # default blob_plain mimetype and default charset for text/plain blob
75 our $default_blob_plain_mimetype = 'text/plain';
76 our $default_text_plain_charset = undef;
78 # file to use for guessing MIME types before trying /etc/mime.types
79 # (relative to the current git repository)
80 our $mimetypes_file = undef;
82 # You define site-wide feature defaults here; override them with
83 # $GITWEB_CONFIG as necessary.
86 # 'sub' => feature-sub (subroutine),
87 # 'override' => allow-override (boolean),
88 # 'default' => [ default options...] (array reference)}
90 # if feature is overridable (it means that allow-override has true value,
91 # then feature-sub will be called with default options as parameters;
92 # return value of feature-sub indicates if to enable specified feature
94 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
96 # Enable the 'blame' blob view, showing the last commit that modified
97 # each line in the file. This can be very CPU-intensive.
99 # To enable system wide have in $GITWEB_CONFIG
100 # $feature{'blame'}{'default'} = [1];
101 # To have project specific config enable override in $GITWEB_CONFIG
102 # $feature{'blame'}{'override'} = 1;
103 # and in project config gitweb.blame = 0|1;
105 'sub' => \&feature_blame,
109 # Enable the 'snapshot' link, providing a compressed tarball of any
110 # tree. This can potentially generate high traffic if you have large
113 # To disable system wide have in $GITWEB_CONFIG
114 # $feature{'snapshot'}{'default'} = [undef];
115 # To have project specific config enable override in $GITWEB_CONFIG
116 # $feature{'blame'}{'override'} = 1;
117 # and in project config gitweb.snapshot = none|gzip|bzip2;
119 'sub' => \&feature_snapshot,
121 # => [content-encoding, suffix, program]
122 'default' => ['x-gzip', 'gz', 'gzip']},
124 # Enable the pickaxe search, which will list the commits that modified
125 # a given string in a file. This can be practical and quite faster
126 # alternative to 'blame', but still potentially CPU-intensive.
128 # To enable system wide have in $GITWEB_CONFIG
129 # $feature{'pickaxe'}{'default'} = [1];
130 # To have project specific config enable override in $GITWEB_CONFIG
131 # $feature{'pickaxe'}{'override'} = 1;
132 # and in project config gitweb.pickaxe = 0|1;
134 'sub' => \&feature_pickaxe,
138 # Make gitweb use an alternative format of the URLs which can be
139 # more readable and natural-looking: project name is embedded
140 # directly in the path and the query string contains other
141 # auxiliary information. All gitweb installations recognize
142 # URL in either format; this configures in which formats gitweb
145 # To enable system wide have in $GITWEB_CONFIG
146 # $feature{'pathinfo'}{'default'} = [1];
147 # Project specific override is not supported.
149 # Note that you will need to change the default location of CSS,
150 # favicon, logo and possibly other files to an absolute URL. Also,
151 # if gitweb.cgi serves as your indexfile, you will need to force
152 # $my_uri to contain the script name in your $GITWEB_CONFIG.
158 sub gitweb_check_feature {
160 return unless exists $feature{$name};
161 my ($sub, $override, @defaults) = (
162 $feature{$name}{'sub'},
163 $feature{$name}{'override'},
164 @{$feature{$name}{'default'}});
165 if (!$override) { return @defaults; }
167 warn "feature $name is not overrideable";
170 return $sub->(@defaults);
174 my ($val) = git_get_project_config('blame', '--bool');
176 if ($val eq 'true') {
178 } elsif ($val eq 'false') {
185 sub feature_snapshot {
186 my ($ctype, $suffix, $command) = @_;
188 my ($val) = git_get_project_config('snapshot');
190 if ($val eq 'gzip') {
191 return ('x-gzip', 'gz', 'gzip');
192 } elsif ($val eq 'bzip2') {
193 return ('x-bzip2', 'bz2', 'bzip2');
194 } elsif ($val eq 'none') {
198 return ($ctype, $suffix, $command);
201 sub gitweb_have_snapshot {
202 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
203 my $have_snapshot = (defined $ctype && defined $suffix);
205 return $have_snapshot;
208 sub feature_pickaxe {
209 my ($val) = git_get_project_config('pickaxe', '--bool');
211 if ($val eq 'true') {
213 } elsif ($val eq 'false') {
220 # rename detection options for git-diff and git-diff-tree
221 # - default is '-M', with the cost proportional to
222 # (number of removed files) * (number of new files).
223 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
224 # (number of changed files + number of removed files) * (number of new files)
225 # - even more costly is '-C', '--find-copies-harder' with cost
226 # (number of files in the original tree) * (number of new files)
227 # - one might want to include '-B' option, e.g. '-B', '-M'
228 our @diff_opts = ('-M'); # taken from git_commit
230 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
231 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
233 # version of the core git binary
234 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
236 $projects_list ||= $projectroot;
238 # ======================================================================
239 # input validation and dispatch
240 our $action = $cgi->param('a');
241 if (defined $action) {
242 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
243 die_error(undef, "Invalid action parameter");
247 # parameters which are pathnames
248 our $project = $cgi->param('p');
249 if (defined $project) {
250 if (!validate_pathname($project) ||
251 !(-d "$projectroot/$project") ||
252 !(-e "$projectroot/$project/HEAD") ||
253 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
254 ($strict_export && !project_in_list($project))) {
256 die_error(undef, "No such project");
260 our $file_name = $cgi->param('f');
261 if (defined $file_name) {
262 if (!validate_pathname($file_name)) {
263 die_error(undef, "Invalid file parameter");
267 our $file_parent = $cgi->param('fp');
268 if (defined $file_parent) {
269 if (!validate_pathname($file_parent)) {
270 die_error(undef, "Invalid file parent parameter");
274 # parameters which are refnames
275 our $hash = $cgi->param('h');
277 if (!validate_refname($hash)) {
278 die_error(undef, "Invalid hash parameter");
282 our $hash_parent = $cgi->param('hp');
283 if (defined $hash_parent) {
284 if (!validate_refname($hash_parent)) {
285 die_error(undef, "Invalid hash parent parameter");
289 our $hash_base = $cgi->param('hb');
290 if (defined $hash_base) {
291 if (!validate_refname($hash_base)) {
292 die_error(undef, "Invalid hash base parameter");
296 our $hash_parent_base = $cgi->param('hpb');
297 if (defined $hash_parent_base) {
298 if (!validate_refname($hash_parent_base)) {
299 die_error(undef, "Invalid hash parent base parameter");
304 our $page = $cgi->param('pg');
306 if ($page =~ m/[^0-9]/) {
307 die_error(undef, "Invalid page parameter");
311 our $searchtext = $cgi->param('s');
312 if (defined $searchtext) {
313 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
314 die_error(undef, "Invalid search parameter");
316 $searchtext = quotemeta $searchtext;
319 # now read PATH_INFO and use it as alternative to parameters
320 sub evaluate_path_info {
321 return if defined $project;
322 my $path_info = $ENV{"PATH_INFO"};
323 return if !$path_info;
324 $path_info =~ s,^/+,,;
325 return if !$path_info;
326 # find which part of PATH_INFO is project
327 $project = $path_info;
329 while ($project && !-e "$projectroot/$project/HEAD") {
330 $project =~ s,/*[^/]*$,,;
333 $project = validate_pathname($project);
335 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
336 ($strict_export && !project_in_list($project))) {
340 # do not change any parameters if an action is given using the query string
342 $path_info =~ s,^$project/*,,;
343 my ($refname, $pathname) = split(/:/, $path_info, 2);
344 if (defined $pathname) {
345 # we got "project.git/branch:filename" or "project.git/branch:dir/"
346 # we could use git_get_type(branch:pathname), but it needs $git_dir
347 $pathname =~ s,^/+,,;
348 if (!$pathname || substr($pathname, -1) eq "/") {
352 $action ||= "blob_plain";
354 $hash_base ||= validate_refname($refname);
355 $file_name ||= validate_pathname($pathname);
356 } elsif (defined $refname) {
357 # we got "project.git/branch"
358 $action ||= "shortlog";
359 $hash ||= validate_refname($refname);
362 evaluate_path_info();
364 # path to the current git repository
366 $git_dir = "$projectroot/$project" if $project;
370 "blame" => \&git_blame2,
371 "blobdiff" => \&git_blobdiff,
372 "blobdiff_plain" => \&git_blobdiff_plain,
373 "blob" => \&git_blob,
374 "blob_plain" => \&git_blob_plain,
375 "commitdiff" => \&git_commitdiff,
376 "commitdiff_plain" => \&git_commitdiff_plain,
377 "commit" => \&git_commit,
378 "heads" => \&git_heads,
379 "history" => \&git_history,
382 "search" => \&git_search,
383 "shortlog" => \&git_shortlog,
384 "summary" => \&git_summary,
386 "tags" => \&git_tags,
387 "tree" => \&git_tree,
388 "snapshot" => \&git_snapshot,
389 # those below don't need $project
390 "opml" => \&git_opml,
391 "project_list" => \&git_project_list,
392 "project_index" => \&git_project_index,
395 if (defined $project) {
396 $action ||= 'summary';
398 $action ||= 'project_list';
400 if (!defined($actions{$action})) {
401 die_error(undef, "Unknown action");
403 if ($action !~ m/^(opml|project_list|project_index)$/ &&
405 die_error(undef, "Project needed");
407 $actions{$action}->();
410 ## ======================================================================
417 # XXX: Warning: If you touch this, check the search form for updating,
428 hash_parent_base => "hpb",
433 my %mapping = @mapping;
435 $params{'project'} = $project unless exists $params{'project'};
437 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
439 # use PATH_INFO for project name
440 $href .= "/$params{'project'}" if defined $params{'project'};
441 delete $params{'project'};
443 # Summary just uses the project path URL
444 if (defined $params{'action'} && $params{'action'} eq 'summary') {
445 delete $params{'action'};
449 # now encode the parameters explicitly
451 for (my $i = 0; $i < @mapping; $i += 2) {
452 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
453 if (defined $params{$name}) {
454 push @result, $symbol . "=" . esc_param($params{$name});
457 $href .= "?" . join(';', @result) if scalar @result;
463 ## ======================================================================
464 ## validation, quoting/unquoting and escaping
466 sub validate_pathname {
467 my $input = shift || return undef;
469 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
470 # at the beginning, at the end, and between slashes.
471 # also this catches doubled slashes
472 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
476 if ($input =~ m!\0!) {
482 sub validate_refname {
483 my $input = shift || return undef;
485 # textual hashes are O.K.
486 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
489 # it must be correct pathname
490 $input = validate_pathname($input)
492 # restrictions on ref name according to git-check-ref-format
493 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
499 # very thin wrapper for decode("utf8", $str, Encode::FB_DEFAULT);
502 return decode("utf8", $str, Encode::FB_DEFAULT);
505 # quote unsafe chars, but keep the slash, even when it's not
506 # correct, but quoted slashes look too horrible in bookmarks
509 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
515 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
518 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
524 # replace invalid utf8 character with SUBSTITUTION sequence
527 $str = to_utf8($str);
528 $str = escapeHTML($str);
529 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
530 $str =~ s/\033/^[/g; # "escape" ESCAPE (\e) character (e.g. commit 20a3847d8a5032ce41f90dcc68abfb36e6fee9b1)
534 # git may return quoted and escaped filenames
537 if ($str =~ m/^"(.*)"$/) {
539 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
544 # escape tabs (convert tabs to spaces)
548 while ((my $pos = index($line, "\t")) != -1) {
549 if (my $count = (8 - ($pos % 8))) {
550 my $spaces = ' ' x $count;
551 $line =~ s/\t/$spaces/;
558 sub project_in_list {
560 my @list = git_get_projects_list();
561 return @list && scalar(grep { $_->{'path'} eq $project } @list);
564 ## ----------------------------------------------------------------------
565 ## HTML aware string manipulation
570 my $add_len = shift || 10;
572 # allow only $len chars, but don't cut a word if it would fit in $add_len
573 # if it doesn't fit, cut it if it's still longer than the dots we would add
574 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
577 if (length($tail) > 4) {
579 $body =~ s/&[^;]*$//; # remove chopped character entities
584 ## ----------------------------------------------------------------------
585 ## functions returning short strings
587 # CSS class for given age value (in seconds)
591 if ($age < 60*60*2) {
593 } elsif ($age < 60*60*24*2) {
600 # convert age in seconds to "nn units ago" string
605 if ($age > 60*60*24*365*2) {
606 $age_str = (int $age/60/60/24/365);
607 $age_str .= " years ago";
608 } elsif ($age > 60*60*24*(365/12)*2) {
609 $age_str = int $age/60/60/24/(365/12);
610 $age_str .= " months ago";
611 } elsif ($age > 60*60*24*7*2) {
612 $age_str = int $age/60/60/24/7;
613 $age_str .= " weeks ago";
614 } elsif ($age > 60*60*24*2) {
615 $age_str = int $age/60/60/24;
616 $age_str .= " days ago";
617 } elsif ($age > 60*60*2) {
618 $age_str = int $age/60/60;
619 $age_str .= " hours ago";
620 } elsif ($age > 60*2) {
621 $age_str = int $age/60;
622 $age_str .= " min ago";
625 $age_str .= " sec ago";
627 $age_str .= " right now";
632 # convert file mode in octal to symbolic file mode string
634 my $mode = oct shift;
636 if (S_ISDIR($mode & S_IFMT)) {
638 } elsif (S_ISLNK($mode)) {
640 } elsif (S_ISREG($mode)) {
641 # git cares only about the executable bit
642 if ($mode & S_IXUSR) {
652 # convert file mode in octal to file type string
656 if ($mode !~ m/^[0-7]+$/) {
662 if (S_ISDIR($mode & S_IFMT)) {
664 } elsif (S_ISLNK($mode)) {
666 } elsif (S_ISREG($mode)) {
673 ## ----------------------------------------------------------------------
674 ## functions returning short HTML fragments, or transforming HTML fragments
675 ## which don't beling to other sections
677 # format line of commit message or tag comment
678 sub format_log_line_html {
681 $line = esc_html($line);
682 $line =~ s/ / /g;
683 if ($line =~ m/([0-9a-fA-F]{40})/) {
685 if (git_get_type($hash_text) eq "commit") {
687 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
688 -class => "text"}, $hash_text);
689 $line =~ s/$hash_text/$link/;
695 # format marker of refs pointing to given object
696 sub format_ref_marker {
697 my ($refs, $id) = @_;
700 if (defined $refs->{$id}) {
701 foreach my $ref (@{$refs->{$id}}) {
702 my ($type, $name) = qw();
703 # e.g. tags/v2.6.11 or heads/next
704 if ($ref =~ m!^(.*?)s?/(.*)$!) {
712 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
717 return ' <span class="refs">'. $markers . '</span>';
723 # format, perhaps shortened and with markers, title line
724 sub format_subject_html {
725 my ($long, $short, $href, $extra) = @_;
726 $extra = '' unless defined($extra);
728 if (length($short) < length($long)) {
729 return $cgi->a({-href => $href, -class => "list subject",
730 -title => to_utf8($long)},
731 esc_html($short) . $extra);
733 return $cgi->a({-href => $href, -class => "list subject"},
734 esc_html($long) . $extra);
738 sub format_diff_line {
740 my $char = substr($line, 0, 1);
746 $diff_class = " add";
747 } elsif ($char eq "-") {
748 $diff_class = " rem";
749 } elsif ($char eq "@") {
750 $diff_class = " chunk_header";
751 } elsif ($char eq "\\") {
752 $diff_class = " incomplete";
754 $line = untabify($line);
755 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
758 ## ----------------------------------------------------------------------
759 ## git utility subroutines, invoking git commands
761 # returns path to the core git executable and the --git-dir parameter as list
763 return $GIT, '--git-dir='.$git_dir;
766 # returns path to the core git executable and the --git-dir parameter as string
768 return join(' ', git_cmd());
771 # get HEAD ref of given project as hash
772 sub git_get_head_hash {
774 my $o_git_dir = $git_dir;
776 $git_dir = "$projectroot/$project";
777 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
780 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
784 if (defined $o_git_dir) {
785 $git_dir = $o_git_dir;
790 # get type of given object
794 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
801 sub git_get_project_config {
802 my ($key, $type) = @_;
804 return unless ($key);
805 $key =~ s/^gitweb\.//;
806 return if ($key =~ m/\W/);
808 my @x = (git_cmd(), 'repo-config');
809 if (defined $type) { push @x, $type; }
811 push @x, "gitweb.$key";
817 # get hash of given path at given ref
818 sub git_get_hash_by_path {
820 my $path = shift || return undef;
825 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
826 or die_error(undef, "Open git-ls-tree failed");
828 close $fd or return undef;
830 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
831 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
832 if (defined $type && $type ne $2) {
839 ## ......................................................................
840 ## git utility functions, directly accessing git repository
842 sub git_get_project_description {
845 open my $fd, "$projectroot/$path/description" or return undef;
852 sub git_get_project_url_list {
855 open my $fd, "$projectroot/$path/cloneurl" or return;
856 my @git_project_url_list = map { chomp; $_ } <$fd>;
859 return wantarray ? @git_project_url_list : \@git_project_url_list;
862 sub git_get_projects_list {
865 if (-d $projects_list) {
866 # search in directory
867 my $dir = $projects_list;
868 my $pfxlen = length("$dir");
871 follow_fast => 1, # follow symbolic links
872 dangling_symlinks => 0, # ignore dangling symlinks, silently
874 # skip project-list toplevel, if we get it.
875 return if (m!^[/.]$!);
876 # only directories can be git repositories
877 return unless (-d $_);
879 my $subdir = substr($File::Find::name, $pfxlen + 1);
880 # we check related file in $projectroot
881 if (-e "$projectroot/$subdir/HEAD" && (!$export_ok ||
882 -e "$projectroot/$subdir/$export_ok")) {
883 push @list, { path => $subdir };
884 $File::Find::prune = 1;
889 } elsif (-f $projects_list) {
890 # read from file(url-encoded):
891 # 'git%2Fgit.git Linus+Torvalds'
892 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
893 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
894 open my ($fd), $projects_list or return;
895 while (my $line = <$fd>) {
897 my ($path, $owner) = split ' ', $line;
898 $path = unescape($path);
899 $owner = unescape($owner);
900 if (!defined $path) {
903 if (-e "$projectroot/$path/HEAD" && (!$export_ok ||
904 -e "$projectroot/$path/$export_ok")) {
907 owner => to_utf8($owner),
914 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
918 sub git_get_project_owner {
922 return undef unless $project;
924 # read from file (url-encoded):
925 # 'git%2Fgit.git Linus+Torvalds'
926 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
927 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
928 if (-f $projects_list) {
929 open (my $fd , $projects_list);
930 while (my $line = <$fd>) {
932 my ($pr, $ow) = split ' ', $line;
935 if ($pr eq $project) {
936 $owner = to_utf8($ow);
942 if (!defined $owner) {
943 $owner = get_file_owner("$projectroot/$project");
949 sub git_get_references {
950 my $type = shift || "";
952 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
953 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
954 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
957 while (my $line = <$fd>) {
959 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
960 if (defined $refs{$1}) {
961 push @{$refs{$1}}, $2;
971 sub git_get_rev_name_tags {
972 my $hash = shift || return undef;
974 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
976 my $name_rev = <$fd>;
979 if ($name_rev =~ m|^$hash tags/(.*)$|) {
982 # catches also '$hash undefined' output
987 ## ----------------------------------------------------------------------
988 ## parse to hash functions
992 my $tz = shift || "-0000";
995 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
996 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
997 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
998 $date{'hour'} = $hour;
999 $date{'minute'} = $min;
1000 $date{'mday'} = $mday;
1001 $date{'day'} = $days[$wday];
1002 $date{'month'} = $months[$mon];
1003 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1004 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1005 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1006 $mday, $months[$mon], $hour ,$min;
1008 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1009 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1010 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1011 $date{'hour_local'} = $hour;
1012 $date{'minute_local'} = $min;
1013 $date{'tz_local'} = $tz;
1022 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1023 $tag{'id'} = $tag_id;
1024 while (my $line = <$fd>) {
1026 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1027 $tag{'object'} = $1;
1028 } elsif ($line =~ m/^type (.+)$/) {
1030 } elsif ($line =~ m/^tag (.+)$/) {
1032 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1033 $tag{'author'} = $1;
1036 } elsif ($line =~ m/--BEGIN/) {
1037 push @comment, $line;
1039 } elsif ($line eq "") {
1043 push @comment, <$fd>;
1044 $tag{'comment'} = \@comment;
1045 close $fd or return;
1046 if (!defined $tag{'name'}) {
1053 my $commit_id = shift;
1054 my $commit_text = shift;
1059 if (defined $commit_text) {
1060 @commit_lines = @$commit_text;
1063 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
1065 @commit_lines = split '\n', <$fd>;
1066 close $fd or return;
1070 my $header = shift @commit_lines;
1071 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1074 ($co{'id'}, my @parents) = split ' ', $header;
1075 $co{'parents'} = \@parents;
1076 $co{'parent'} = $parents[0];
1077 while (my $line = shift @commit_lines) {
1078 last if $line eq "\n";
1079 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1081 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1083 $co{'author_epoch'} = $2;
1084 $co{'author_tz'} = $3;
1085 if ($co{'author'} =~ m/^([^<]+) </) {
1086 $co{'author_name'} = $1;
1088 $co{'author_name'} = $co{'author'};
1090 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1091 $co{'committer'} = $1;
1092 $co{'committer_epoch'} = $2;
1093 $co{'committer_tz'} = $3;
1094 $co{'committer_name'} = $co{'committer'};
1095 $co{'committer_name'} =~ s/ <.*//;
1098 if (!defined $co{'tree'}) {
1102 foreach my $title (@commit_lines) {
1105 $co{'title'} = chop_str($title, 80, 5);
1106 # remove leading stuff of merges to make the interesting part visible
1107 if (length($title) > 50) {
1108 $title =~ s/^Automatic //;
1109 $title =~ s/^merge (of|with) /Merge ... /i;
1110 if (length($title) > 50) {
1111 $title =~ s/(http|rsync):\/\///;
1113 if (length($title) > 50) {
1114 $title =~ s/(master|www|rsync)\.//;
1116 if (length($title) > 50) {
1117 $title =~ s/kernel.org:?//;
1119 if (length($title) > 50) {
1120 $title =~ s/\/pub\/scm//;
1123 $co{'title_short'} = chop_str($title, 50, 5);
1127 if ($co{'title'} eq "") {
1128 $co{'title'} = $co{'title_short'} = '(no commit message)';
1130 # remove added spaces
1131 foreach my $line (@commit_lines) {
1134 $co{'comment'} = \@commit_lines;
1136 my $age = time - $co{'committer_epoch'};
1138 $co{'age_string'} = age_string($age);
1139 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1140 if ($age > 60*60*24*7*2) {
1141 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1142 $co{'age_string_age'} = $co{'age_string'};
1144 $co{'age_string_date'} = $co{'age_string'};
1145 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1150 # parse ref from ref_file, given by ref_id, with given type
1152 my $ref_file = shift;
1154 my $type = shift || git_get_type($ref_id);
1157 $ref_item{'type'} = $type;
1158 $ref_item{'id'} = $ref_id;
1159 $ref_item{'epoch'} = 0;
1160 $ref_item{'age'} = "unknown";
1161 if ($type eq "tag") {
1162 my %tag = parse_tag($ref_id);
1163 $ref_item{'comment'} = $tag{'comment'};
1164 if ($tag{'type'} eq "commit") {
1165 my %co = parse_commit($tag{'object'});
1166 $ref_item{'epoch'} = $co{'committer_epoch'};
1167 $ref_item{'age'} = $co{'age_string'};
1168 } elsif (defined($tag{'epoch'})) {
1169 my $age = time - $tag{'epoch'};
1170 $ref_item{'epoch'} = $tag{'epoch'};
1171 $ref_item{'age'} = age_string($age);
1173 $ref_item{'reftype'} = $tag{'type'};
1174 $ref_item{'name'} = $tag{'name'};
1175 $ref_item{'refid'} = $tag{'object'};
1176 } elsif ($type eq "commit"){
1177 my %co = parse_commit($ref_id);
1178 $ref_item{'reftype'} = "commit";
1179 $ref_item{'name'} = $ref_file;
1180 $ref_item{'title'} = $co{'title'};
1181 $ref_item{'refid'} = $ref_id;
1182 $ref_item{'epoch'} = $co{'committer_epoch'};
1183 $ref_item{'age'} = $co{'age_string'};
1185 $ref_item{'reftype'} = $type;
1186 $ref_item{'name'} = $ref_file;
1187 $ref_item{'refid'} = $ref_id;
1193 # parse line of git-diff-tree "raw" output
1194 sub parse_difftree_raw_line {
1198 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1199 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1200 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1201 $res{'from_mode'} = $1;
1202 $res{'to_mode'} = $2;
1203 $res{'from_id'} = $3;
1205 $res{'status'} = $5;
1206 $res{'similarity'} = $6;
1207 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1208 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1210 $res{'file'} = unquote($7);
1213 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1214 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1215 $res{'commit'} = $1;
1218 return wantarray ? %res : \%res;
1221 # parse line of git-ls-tree output
1222 sub parse_ls_tree_line ($;%) {
1227 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1228 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1236 $res{'name'} = unquote($4);
1239 return wantarray ? %res : \%res;
1242 ## ......................................................................
1243 ## parse to array of hashes functions
1245 sub git_get_refs_list {
1246 my $type = shift || "";
1251 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1253 while (my $line = <$fd>) {
1255 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1256 if (defined $refs{$1}) {
1257 push @{$refs{$1}}, $2;
1262 if (! $4) { # unpeeled, direct reference
1263 push @refs, { hash => $1, name => $3 }; # without type
1264 } elsif ($3 eq $refs[-1]{'name'}) {
1265 # most likely a tag is followed by its peeled
1266 # (deref) one, and when that happens we know the
1267 # previous one was of type 'tag'.
1268 $refs[-1]{'type'} = "tag";
1274 foreach my $ref (@refs) {
1275 my $ref_file = $ref->{'name'};
1276 my $ref_id = $ref->{'hash'};
1278 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1279 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1281 push @reflist, \%ref_item;
1284 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1285 return (\@reflist, \%refs);
1288 ## ----------------------------------------------------------------------
1289 ## filesystem-related functions
1291 sub get_file_owner {
1294 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1295 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1296 if (!defined $gcos) {
1300 $owner =~ s/[,;].*$//;
1301 return to_utf8($owner);
1304 ## ......................................................................
1305 ## mimetype related functions
1307 sub mimetype_guess_file {
1308 my $filename = shift;
1309 my $mimemap = shift;
1310 -r $mimemap or return undef;
1313 open(MIME, $mimemap) or return undef;
1315 next if m/^#/; # skip comments
1316 my ($mime, $exts) = split(/\t+/);
1317 if (defined $exts) {
1318 my @exts = split(/\s+/, $exts);
1319 foreach my $ext (@exts) {
1320 $mimemap{$ext} = $mime;
1326 $filename =~ /\.([^.]*)$/;
1327 return $mimemap{$1};
1330 sub mimetype_guess {
1331 my $filename = shift;
1333 $filename =~ /\./ or return undef;
1335 if ($mimetypes_file) {
1336 my $file = $mimetypes_file;
1337 if ($file !~ m!^/!) { # if it is relative path
1338 # it is relative to project
1339 $file = "$projectroot/$project/$file";
1341 $mime = mimetype_guess_file($filename, $file);
1343 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1349 my $filename = shift;
1352 my $mime = mimetype_guess($filename);
1353 $mime and return $mime;
1357 return $default_blob_plain_mimetype unless $fd;
1360 return 'text/plain' .
1361 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1362 } elsif (! $filename) {
1363 return 'application/octet-stream';
1364 } elsif ($filename =~ m/\.png$/i) {
1366 } elsif ($filename =~ m/\.gif$/i) {
1368 } elsif ($filename =~ m/\.jpe?g$/i) {
1369 return 'image/jpeg';
1371 return 'application/octet-stream';
1375 ## ======================================================================
1376 ## functions printing HTML: header, footer, error page
1378 sub git_header_html {
1379 my $status = shift || "200 OK";
1380 my $expires = shift;
1382 my $title = "$site_name git";
1383 if (defined $project) {
1384 $title .= " - $project";
1385 if (defined $action) {
1386 $title .= "/$action";
1387 if (defined $file_name) {
1388 $title .= " - " . esc_html($file_name);
1389 if ($action eq "tree" && $file_name !~ m|/$|) {
1396 # require explicit support from the UA if we are to send the page as
1397 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1398 # we have to do this because MSIE sometimes globs '*/*', pretending to
1399 # support xhtml+xml but choking when it gets what it asked for.
1400 if (defined $cgi->http('HTTP_ACCEPT') &&
1401 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1402 $cgi->Accept('application/xhtml+xml') != 0) {
1403 $content_type = 'application/xhtml+xml';
1405 $content_type = 'text/html';
1407 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1408 -status=> $status, -expires => $expires);
1410 <?xml version="1.0" encoding="utf-8"?>
1411 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1412 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1413 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1414 <!-- git core binaries version $git_version -->
1416 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1417 <meta name="generator" content="gitweb/$version git/$git_version"/>
1418 <meta name="robots" content="index, nofollow"/>
1419 <title>$title</title>
1420 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1422 if (defined $project) {
1423 printf('<link rel="alternate" title="%s log" '.
1424 'href="%s" type="application/rss+xml"/>'."\n",
1425 esc_param($project), href(action=>"rss"));
1427 printf('<link rel="alternate" title="%s projects list" '.
1428 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1429 $site_name, href(project=>undef, action=>"project_index"));
1430 printf('<link rel="alternate" title="%s projects logs" '.
1431 'href="%s" type="text/x-opml"/>'."\n",
1432 $site_name, href(project=>undef, action=>"opml"));
1434 if (defined $favicon) {
1435 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1440 "<div class=\"page_header\">\n" .
1441 $cgi->a({-href => esc_url($logo_url),
1442 -title => $logo_label},
1443 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1444 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1445 if (defined $project) {
1446 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1447 if (defined $action) {
1451 if (!defined $searchtext) {
1455 if (defined $hash_base) {
1456 $search_hash = $hash_base;
1457 } elsif (defined $hash) {
1458 $search_hash = $hash;
1460 $search_hash = "HEAD";
1462 $cgi->param("a", "search");
1463 $cgi->param("h", $search_hash);
1464 $cgi->param("p", $project);
1465 print $cgi->startform(-method => "get", -action => $my_uri) .
1466 "<div class=\"search\">\n" .
1467 $cgi->hidden(-name => "p") . "\n" .
1468 $cgi->hidden(-name => "a") . "\n" .
1469 $cgi->hidden(-name => "h") . "\n" .
1470 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1472 $cgi->end_form() . "\n";
1477 sub git_footer_html {
1478 print "<div class=\"page_footer\">\n";
1479 if (defined $project) {
1480 my $descr = git_get_project_description($project);
1481 if (defined $descr) {
1482 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1484 print $cgi->a({-href => href(action=>"rss"),
1485 -class => "rss_logo"}, "RSS") . "\n";
1487 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1488 -class => "rss_logo"}, "OPML") . " ";
1489 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1490 -class => "rss_logo"}, "TXT") . "\n";
1498 my $status = shift || "403 Forbidden";
1499 my $error = shift || "Malformed query, file missing or permission denied";
1501 git_header_html($status);
1503 <div class="page_body">
1513 ## ----------------------------------------------------------------------
1514 ## functions printing or outputting HTML: navigation
1516 sub git_print_page_nav {
1517 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1518 $extra = '' if !defined $extra; # pager or formats
1520 my @navs = qw(summary shortlog log commit commitdiff tree);
1522 @navs = grep { $_ ne $suppress } @navs;
1525 my %arg = map { $_ => {action=>$_} } @navs;
1526 if (defined $head) {
1527 for (qw(commit commitdiff)) {
1528 $arg{$_}{hash} = $head;
1530 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1531 for (qw(shortlog log)) {
1532 $arg{$_}{hash} = $head;
1536 $arg{tree}{hash} = $treehead if defined $treehead;
1537 $arg{tree}{hash_base} = $treebase if defined $treebase;
1539 print "<div class=\"page_nav\">\n" .
1541 map { $_ eq $current ?
1542 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1544 print "<br/>\n$extra<br/>\n" .
1548 sub format_paging_nav {
1549 my ($action, $hash, $head, $page, $nrevs) = @_;
1553 if ($hash ne $head || $page) {
1554 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1556 $paging_nav .= "HEAD";
1560 $paging_nav .= " ⋅ " .
1561 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1562 -accesskey => "p", -title => "Alt-p"}, "prev");
1564 $paging_nav .= " ⋅ prev";
1567 if ($nrevs >= (100 * ($page+1)-1)) {
1568 $paging_nav .= " ⋅ " .
1569 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1570 -accesskey => "n", -title => "Alt-n"}, "next");
1572 $paging_nav .= " ⋅ next";
1578 ## ......................................................................
1579 ## functions printing or outputting HTML: div
1581 sub git_print_header_div {
1582 my ($action, $title, $hash, $hash_base) = @_;
1585 $args{action} = $action;
1586 $args{hash} = $hash if $hash;
1587 $args{hash_base} = $hash_base if $hash_base;
1589 print "<div class=\"header\">\n" .
1590 $cgi->a({-href => href(%args), -class => "title"},
1591 $title ? $title : $action) .
1595 #sub git_print_authorship (\%) {
1596 sub git_print_authorship {
1599 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1600 print "<div class=\"author_date\">" .
1601 esc_html($co->{'author_name'}) .
1603 if ($ad{'hour_local'} < 6) {
1604 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1605 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1607 printf(" (%02d:%02d %s)",
1608 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1613 sub git_print_page_path {
1618 if (!defined $name) {
1619 print "<div class=\"page_path\">/</div>\n";
1621 my @dirname = split '/', $name;
1622 my $basename = pop @dirname;
1625 print "<div class=\"page_path\">";
1626 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1627 -title => 'tree root'}, "[$project]");
1629 foreach my $dir (@dirname) {
1630 $fullname .= ($fullname ? '/' : '') . $dir;
1631 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1633 -title => $fullname}, esc_html($dir));
1636 if (defined $type && $type eq 'blob') {
1637 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1639 -title => $name}, esc_html($basename));
1640 } elsif (defined $type && $type eq 'tree') {
1641 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1643 -title => $name}, esc_html($basename));
1645 print esc_html($basename);
1647 print "<br/></div>\n";
1651 # sub git_print_log (\@;%) {
1652 sub git_print_log ($;%) {
1656 if ($opts{'-remove_title'}) {
1657 # remove title, i.e. first line of log
1660 # remove leading empty lines
1661 while (defined $log->[0] && $log->[0] eq "") {
1668 foreach my $line (@$log) {
1669 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1672 if (! $opts{'-remove_signoff'}) {
1673 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1676 # remove signoff lines
1683 # print only one empty line
1684 # do not print empty line after signoff
1686 next if ($empty || $signoff);
1692 print format_log_line_html($line) . "<br/>\n";
1695 if ($opts{'-final_empty_line'}) {
1696 # end with single empty line
1697 print "<br/>\n" unless $empty;
1701 sub git_print_simplified_log {
1703 my $remove_title = shift;
1706 -final_empty_line=> 1,
1707 -remove_title => $remove_title);
1710 # print tree entry (row of git_tree), but without encompassing <tr> element
1711 sub git_print_tree_entry {
1712 my ($t, $basedir, $hash_base, $have_blame) = @_;
1715 $base_key{hash_base} = $hash_base if defined $hash_base;
1717 # The format of a table row is: mode list link. Where mode is
1718 # the mode of the entry, list is the name of the entry, an href,
1719 # and link is the action links of the entry.
1721 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1722 if ($t->{'type'} eq "blob") {
1723 print "<td class=\"list\">" .
1724 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1725 file_name=>"$basedir$t->{'name'}", %base_key),
1726 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1727 print "<td class=\"link\">";
1729 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1730 file_name=>"$basedir$t->{'name'}", %base_key)},
1733 if (defined $hash_base) {
1737 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1738 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1742 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1743 file_name=>"$basedir$t->{'name'}")},
1747 } elsif ($t->{'type'} eq "tree") {
1748 print "<td class=\"list\">";
1749 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1750 file_name=>"$basedir$t->{'name'}", %base_key)},
1751 esc_html($t->{'name'}));
1753 print "<td class=\"link\">";
1754 if (defined $hash_base) {
1755 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1756 file_name=>"$basedir$t->{'name'}")},
1763 ## ......................................................................
1764 ## functions printing large fragments of HTML
1766 sub git_difftree_body {
1767 my ($difftree, $hash, $parent) = @_;
1769 print "<div class=\"list_head\">\n";
1770 if ($#{$difftree} > 10) {
1771 print(($#{$difftree} + 1) . " files changed:\n");
1775 print "<table class=\"diff_tree\">\n";
1778 foreach my $line (@{$difftree}) {
1779 my %diff = parse_difftree_raw_line($line);
1782 print "<tr class=\"dark\">\n";
1784 print "<tr class=\"light\">\n";
1788 my ($to_mode_oct, $to_mode_str, $to_file_type);
1789 my ($from_mode_oct, $from_mode_str, $from_file_type);
1790 if ($diff{'to_mode'} ne ('0' x 6)) {
1791 $to_mode_oct = oct $diff{'to_mode'};
1792 if (S_ISREG($to_mode_oct)) { # only for regular file
1793 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1795 $to_file_type = file_type($diff{'to_mode'});
1797 if ($diff{'from_mode'} ne ('0' x 6)) {
1798 $from_mode_oct = oct $diff{'from_mode'};
1799 if (S_ISREG($to_mode_oct)) { # only for regular file
1800 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1802 $from_file_type = file_type($diff{'from_mode'});
1805 if ($diff{'status'} eq "A") { # created
1806 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1807 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1808 $mode_chng .= "]</span>";
1810 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1811 hash_base=>$hash, file_name=>$diff{'file'}),
1812 -class => "list"}, esc_html($diff{'file'}));
1814 print "<td>$mode_chng</td>\n";
1815 print "<td class=\"link\">";
1816 if ($action eq 'commitdiff') {
1819 print $cgi->a({-href => "#patch$patchno"}, "patch");
1823 } elsif ($diff{'status'} eq "D") { # deleted
1824 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1826 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1827 hash_base=>$parent, file_name=>$diff{'file'}),
1828 -class => "list"}, esc_html($diff{'file'}));
1830 print "<td>$mode_chng</td>\n";
1831 print "<td class=\"link\">";
1832 if ($action eq 'commitdiff') {
1835 print $cgi->a({-href => "#patch$patchno"}, "patch");
1838 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1839 file_name=>$diff{'file'})},
1841 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1842 file_name=>$diff{'file'})},
1846 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1847 my $mode_chnge = "";
1848 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1849 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1850 if ($from_file_type != $to_file_type) {
1851 $mode_chnge .= " from $from_file_type to $to_file_type";
1853 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1854 if ($from_mode_str && $to_mode_str) {
1855 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1856 } elsif ($to_mode_str) {
1857 $mode_chnge .= " mode: $to_mode_str";
1860 $mode_chnge .= "]</span>\n";
1863 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1864 hash_base=>$hash, file_name=>$diff{'file'}),
1865 -class => "list"}, esc_html($diff{'file'}));
1867 print "<td>$mode_chnge</td>\n";
1868 print "<td class=\"link\">";
1869 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1870 if ($action eq 'commitdiff') {
1873 print $cgi->a({-href => "#patch$patchno"}, "patch");
1875 print $cgi->a({-href => href(action=>"blobdiff",
1876 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1877 hash_base=>$hash, hash_parent_base=>$parent,
1878 file_name=>$diff{'file'})},
1883 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1884 file_name=>$diff{'file'})},
1886 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1887 file_name=>$diff{'file'})},
1891 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1892 my %status_name = ('R' => 'moved', 'C' => 'copied');
1893 my $nstatus = $status_name{$diff{'status'}};
1895 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1896 # mode also for directories, so we cannot use $to_mode_str
1897 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1900 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1901 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1902 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1903 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1904 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1905 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1906 -class => "list"}, esc_html($diff{'from_file'})) .
1907 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1908 "<td class=\"link\">";
1909 if ($diff{'to_id'} ne $diff{'from_id'}) {
1910 if ($action eq 'commitdiff') {
1913 print $cgi->a({-href => "#patch$patchno"}, "patch");
1915 print $cgi->a({-href => href(action=>"blobdiff",
1916 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1917 hash_base=>$hash, hash_parent_base=>$parent,
1918 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1923 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1924 file_name=>$diff{'from_file'})},
1926 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1927 file_name=>$diff{'from_file'})},
1931 } # we should not encounter Unmerged (U) or Unknown (X) status
1937 sub git_patchset_body {
1938 my ($fd, $difftree, $hash, $hash_parent) = @_;
1942 my $patch_found = 0;
1945 print "<div class=\"patchset\">\n";
1948 while (my $patch_line = <$fd>) {
1951 if ($patch_line =~ m/^diff /) { # "git diff" header
1952 # beginning of patch (in patchset)
1954 # close previous patch
1955 print "</div>\n"; # class="patch"
1957 # first patch in patchset
1960 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1962 if (ref($difftree->[$patch_idx]) eq "HASH") {
1963 $diffinfo = $difftree->[$patch_idx];
1965 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1969 # for now, no extended header, hence we skip empty patches
1970 # companion to next LINE if $in_header;
1971 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1976 if ($diffinfo->{'status'} eq "A") { # added
1977 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1978 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1979 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1980 $diffinfo->{'to_id'}) . " (new)" .
1981 "</div>\n"; # class="diff_info"
1983 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1984 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1985 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1986 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1987 $diffinfo->{'from_id'}) . " (deleted)" .
1988 "</div>\n"; # class="diff_info"
1990 } elsif ($diffinfo->{'status'} eq "R" || # renamed
1991 $diffinfo->{'status'} eq "C" || # copied
1992 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1993 print "<div class=\"diff_info\">" .
1994 file_type($diffinfo->{'from_mode'}) . ":" .
1995 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1996 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1997 $diffinfo->{'from_id'}) .
1999 file_type($diffinfo->{'to_mode'}) . ":" .
2000 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2001 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
2002 $diffinfo->{'to_id'});
2003 print "</div>\n"; # class="diff_info"
2005 } else { # modified, mode changed, ...
2006 print "<div class=\"diff_info\">" .
2007 file_type($diffinfo->{'from_mode'}) . ":" .
2008 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2009 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2010 $diffinfo->{'from_id'}) .
2012 file_type($diffinfo->{'to_mode'}) . ":" .
2013 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2014 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2015 $diffinfo->{'to_id'});
2016 print "</div>\n"; # class="diff_info"
2019 #print "<div class=\"diff extended_header\">\n";
2022 } # start of patch in patchset
2025 if ($in_header && $patch_line =~ m/^---/) {
2026 #print "</div>\n"; # class="diff extended_header"
2029 my $file = $diffinfo->{'from_file'};
2030 $file ||= $diffinfo->{'file'};
2031 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2032 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2033 -class => "list"}, esc_html($file));
2034 $patch_line =~ s|a/.*$|a/$file|g;
2035 print "<div class=\"diff from_file\">$patch_line</div>\n";
2037 $patch_line = <$fd>;
2040 #$patch_line =~ m/^+++/;
2041 $file = $diffinfo->{'to_file'};
2042 $file ||= $diffinfo->{'file'};
2043 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2044 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2045 -class => "list"}, esc_html($file));
2046 $patch_line =~ s|b/.*|b/$file|g;
2047 print "<div class=\"diff to_file\">$patch_line</div>\n";
2051 next LINE if $in_header;
2053 print format_diff_line($patch_line);
2055 print "</div>\n" if $patch_found; # class="patch"
2057 print "</div>\n"; # class="patchset"
2060 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2062 sub git_shortlog_body {
2063 # uses global variable $project
2064 my ($revlist, $from, $to, $refs, $extra) = @_;
2066 $from = 0 unless defined $from;
2067 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2069 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2071 for (my $i = $from; $i <= $to; $i++) {
2072 my $commit = $revlist->[$i];
2073 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2074 my $ref = format_ref_marker($refs, $commit);
2075 my %co = parse_commit($commit);
2077 print "<tr class=\"dark\">\n";
2079 print "<tr class=\"light\">\n";
2082 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2083 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2084 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2086 print format_subject_html($co{'title'}, $co{'title_short'},
2087 href(action=>"commit", hash=>$commit), $ref);
2089 "<td class=\"link\">" .
2090 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2091 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2092 if (gitweb_have_snapshot()) {
2093 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2098 if (defined $extra) {
2100 "<td colspan=\"4\">$extra</td>\n" .
2106 sub git_history_body {
2107 # Warning: assumes constant type (blob or tree) during history
2108 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2110 $from = 0 unless defined $from;
2111 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2113 print "<table class=\"history\" cellspacing=\"0\">\n";
2115 for (my $i = $from; $i <= $to; $i++) {
2116 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2121 my %co = parse_commit($commit);
2126 my $ref = format_ref_marker($refs, $commit);
2129 print "<tr class=\"dark\">\n";
2131 print "<tr class=\"light\">\n";
2134 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2135 # shortlog uses chop_str($co{'author_name'}, 10)
2136 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2138 # originally git_history used chop_str($co{'title'}, 50)
2139 print format_subject_html($co{'title'}, $co{'title_short'},
2140 href(action=>"commit", hash=>$commit), $ref);
2142 "<td class=\"link\">" .
2143 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2144 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2146 if ($ftype eq 'blob') {
2147 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2148 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2149 if (defined $blob_current && defined $blob_parent &&
2150 $blob_current ne $blob_parent) {
2152 $cgi->a({-href => href(action=>"blobdiff",
2153 hash=>$blob_current, hash_parent=>$blob_parent,
2154 hash_base=>$hash_base, hash_parent_base=>$commit,
2155 file_name=>$file_name)},
2162 if (defined $extra) {
2164 "<td colspan=\"4\">$extra</td>\n" .
2171 # uses global variable $project
2172 my ($taglist, $from, $to, $extra) = @_;
2173 $from = 0 unless defined $from;
2174 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2176 print "<table class=\"tags\" cellspacing=\"0\">\n";
2178 for (my $i = $from; $i <= $to; $i++) {
2179 my $entry = $taglist->[$i];
2181 my $comment_lines = $tag{'comment'};
2182 my $comment = shift @$comment_lines;
2184 if (defined $comment) {
2185 $comment_short = chop_str($comment, 30, 5);
2188 print "<tr class=\"dark\">\n";
2190 print "<tr class=\"light\">\n";
2193 print "<td><i>$tag{'age'}</i></td>\n" .
2195 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2196 -class => "list name"}, esc_html($tag{'name'})) .
2199 if (defined $comment) {
2200 print format_subject_html($comment, $comment_short,
2201 href(action=>"tag", hash=>$tag{'id'}));
2204 "<td class=\"selflink\">";
2205 if ($tag{'type'} eq "tag") {
2206 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2211 "<td class=\"link\">" . " | " .
2212 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2213 if ($tag{'reftype'} eq "commit") {
2214 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2215 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2216 } elsif ($tag{'reftype'} eq "blob") {
2217 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2222 if (defined $extra) {
2224 "<td colspan=\"5\">$extra</td>\n" .
2230 sub git_heads_body {
2231 # uses global variable $project
2232 my ($headlist, $head, $from, $to, $extra) = @_;
2233 $from = 0 unless defined $from;
2234 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2236 print "<table class=\"heads\" cellspacing=\"0\">\n";
2238 for (my $i = $from; $i <= $to; $i++) {
2239 my $entry = $headlist->[$i];
2241 my $curr = $tag{'id'} eq $head;
2243 print "<tr class=\"dark\">\n";
2245 print "<tr class=\"light\">\n";
2248 print "<td><i>$tag{'age'}</i></td>\n" .
2249 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2250 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2251 -class => "list name"},esc_html($tag{'name'})) .
2253 "<td class=\"link\">" .
2254 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2255 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2256 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2260 if (defined $extra) {
2262 "<td colspan=\"3\">$extra</td>\n" .
2268 ## ======================================================================
2269 ## ======================================================================
2272 sub git_project_list {
2273 my $order = $cgi->param('o');
2274 if (defined $order && $order !~ m/project|descr|owner|age/) {
2275 die_error(undef, "Unknown order parameter");
2278 my @list = git_get_projects_list();
2281 die_error(undef, "No projects found");
2283 foreach my $pr (@list) {
2284 my $head = git_get_head_hash($pr->{'path'});
2285 if (!defined $head) {
2288 $git_dir = "$projectroot/$pr->{'path'}";
2289 my %co = parse_commit($head);
2293 $pr->{'commit'} = \%co;
2294 if (!defined $pr->{'descr'}) {
2295 my $descr = git_get_project_description($pr->{'path'}) || "";
2296 $pr->{'descr'} = chop_str($descr, 25, 5);
2298 if (!defined $pr->{'owner'}) {
2299 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2301 push @projects, $pr;
2305 if (-f $home_text) {
2306 print "<div class=\"index_include\">\n";
2307 open (my $fd, $home_text);
2312 print "<table class=\"project_list\">\n" .
2314 $order ||= "project";
2315 if ($order eq "project") {
2316 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2317 print "<th>Project</th>\n";
2320 $cgi->a({-href => href(project=>undef, order=>'project'),
2321 -class => "header"}, "Project") .
2324 if ($order eq "descr") {
2325 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2326 print "<th>Description</th>\n";
2329 $cgi->a({-href => href(project=>undef, order=>'descr'),
2330 -class => "header"}, "Description") .
2333 if ($order eq "owner") {
2334 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2335 print "<th>Owner</th>\n";
2338 $cgi->a({-href => href(project=>undef, order=>'owner'),
2339 -class => "header"}, "Owner") .
2342 if ($order eq "age") {
2343 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2344 print "<th>Last Change</th>\n";
2347 $cgi->a({-href => href(project=>undef, order=>'age'),
2348 -class => "header"}, "Last Change") .
2351 print "<th></th>\n" .
2354 foreach my $pr (@projects) {
2356 print "<tr class=\"dark\">\n";
2358 print "<tr class=\"light\">\n";
2361 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2362 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2363 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2364 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2365 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2366 $pr->{'commit'}{'age_string'} . "</td>\n" .
2367 "<td class=\"link\">" .
2368 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2369 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2370 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2371 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2379 sub git_project_index {
2380 my @projects = git_get_projects_list();
2383 -type => 'text/plain',
2384 -charset => 'utf-8',
2385 -content_disposition => 'inline; filename="index.aux"');
2387 foreach my $pr (@projects) {
2388 if (!exists $pr->{'owner'}) {
2389 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2392 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2393 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2394 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2395 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2399 print "$path $owner\n";
2404 my $descr = git_get_project_description($project) || "none";
2405 my $head = git_get_head_hash($project);
2406 my %co = parse_commit($head);
2407 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2409 my $owner = git_get_project_owner($project);
2411 my ($reflist, $refs) = git_get_refs_list();
2415 foreach my $ref (@$reflist) {
2416 if ($ref->{'name'} =~ s!^heads/!!) {
2417 push @headlist, $ref;
2419 $ref->{'name'} =~ s!^tags/!!;
2420 push @taglist, $ref;
2425 git_print_page_nav('summary','', $head);
2427 print "<div class=\"title\"> </div>\n";
2428 print "<table cellspacing=\"0\">\n" .
2429 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2430 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2431 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2432 # use per project git URL list in $projectroot/$project/cloneurl
2433 # or make project git URL from git base URL and project name
2434 my $url_tag = "URL";
2435 my @url_list = git_get_project_url_list($project);
2436 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2437 foreach my $git_url (@url_list) {
2438 next unless $git_url;
2439 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2444 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2445 git_get_head_hash($project)
2446 or die_error(undef, "Open git-rev-list failed");
2447 my @revlist = map { chomp; $_ } <$fd>;
2449 git_print_header_div('shortlog');
2450 git_shortlog_body(\@revlist, 0, 15, $refs,
2451 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2454 git_print_header_div('tags');
2455 git_tags_body(\@taglist, 0, 15,
2456 $cgi->a({-href => href(action=>"tags")}, "..."));
2460 git_print_header_div('heads');
2461 git_heads_body(\@headlist, $head, 0, 15,
2462 $cgi->a({-href => href(action=>"heads")}, "..."));
2469 my $head = git_get_head_hash($project);
2471 git_print_page_nav('','', $head,undef,$head);
2472 my %tag = parse_tag($hash);
2473 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2474 print "<div class=\"title_text\">\n" .
2475 "<table cellspacing=\"0\">\n" .
2477 "<td>object</td>\n" .
2478 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2479 $tag{'object'}) . "</td>\n" .
2480 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2481 $tag{'type'}) . "</td>\n" .
2483 if (defined($tag{'author'})) {
2484 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2485 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2486 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2487 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2490 print "</table>\n\n" .
2492 print "<div class=\"page_body\">";
2493 my $comment = $tag{'comment'};
2494 foreach my $line (@$comment) {
2495 print esc_html($line) . "<br/>\n";
2505 my ($have_blame) = gitweb_check_feature('blame');
2507 die_error('403 Permission denied', "Permission denied");
2509 die_error('404 Not Found', "File name not defined") if (!$file_name);
2510 $hash_base ||= git_get_head_hash($project);
2511 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2512 my %co = parse_commit($hash_base)
2513 or die_error(undef, "Reading commit failed");
2514 if (!defined $hash) {
2515 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2516 or die_error(undef, "Error looking up file");
2518 $ftype = git_get_type($hash);
2519 if ($ftype !~ "blob") {
2520 die_error("400 Bad Request", "Object is not a blob");
2522 open ($fd, "-|", git_cmd(), "blame", '-l', '--', $file_name, $hash_base)
2523 or die_error(undef, "Open git-blame failed");
2526 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2529 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2532 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2534 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2535 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2536 git_print_page_path($file_name, $ftype, $hash_base);
2537 my @rev_color = (qw(light2 dark2));
2538 my $num_colors = scalar(@rev_color);
2539 my $current_color = 0;
2542 <div class="page_body">
2543 <table class="blame">
2544 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2547 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2549 my $rev = substr($full_rev, 0, 8);
2553 if (!defined $last_rev) {
2554 $last_rev = $full_rev;
2555 } elsif ($last_rev ne $full_rev) {
2556 $last_rev = $full_rev;
2557 $current_color = ++$current_color % $num_colors;
2559 print "<tr class=\"$rev_color[$current_color]\">\n";
2560 print "<td class=\"sha1\">" .
2561 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2562 esc_html($rev)) . "</td>\n";
2563 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2564 esc_html($lineno) . "</a></td>\n";
2565 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2571 or print "Reading blob failed\n";
2578 my ($have_blame) = gitweb_check_feature('blame');
2580 die_error('403 Permission denied', "Permission denied");
2582 die_error('404 Not Found', "File name not defined") if (!$file_name);
2583 $hash_base ||= git_get_head_hash($project);
2584 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2585 my %co = parse_commit($hash_base)
2586 or die_error(undef, "Reading commit failed");
2587 if (!defined $hash) {
2588 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2589 or die_error(undef, "Error lookup file");
2591 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2592 or die_error(undef, "Open git-annotate failed");
2595 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2598 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2601 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2603 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2604 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2605 git_print_page_path($file_name, 'blob', $hash_base);
2606 print "<div class=\"page_body\">\n";
2608 <table class="blame">
2617 my @line_class = (qw(light dark));
2618 my $line_class_len = scalar (@line_class);
2619 my $line_class_num = $#line_class;
2620 while (my $line = <$fd>) {
2632 $line_class_num = ($line_class_num + 1) % $line_class_len;
2634 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2641 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2644 $short_rev = substr ($long_rev, 0, 8);
2645 $age = time () - $time;
2646 $age_str = age_string ($age);
2647 $age_str =~ s/ / /g;
2648 $age_class = age_class($age);
2649 $author = esc_html ($author);
2650 $author =~ s/ / /g;
2652 $data = untabify($data);
2653 $data = esc_html ($data);
2656 <tr class="$line_class[$line_class_num]">
2657 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2658 <td class="$age_class">$age_str</td>
2660 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2661 <td class="pre">$data</td>
2664 } # while (my $line = <$fd>)
2665 print "</table>\n\n";
2667 or print "Reading blob failed.\n";
2673 my $head = git_get_head_hash($project);
2675 git_print_page_nav('','', $head,undef,$head);
2676 git_print_header_div('summary', $project);
2678 my ($taglist) = git_get_refs_list("tags");
2680 git_tags_body($taglist);
2686 my $head = git_get_head_hash($project);
2688 git_print_page_nav('','', $head,undef,$head);
2689 git_print_header_div('summary', $project);
2691 my ($headlist) = git_get_refs_list("heads");
2693 git_heads_body($headlist, $head);
2698 sub git_blob_plain {
2701 if (!defined $hash) {
2702 if (defined $file_name) {
2703 my $base = $hash_base || git_get_head_hash($project);
2704 $hash = git_get_hash_by_path($base, $file_name, "blob")
2705 or die_error(undef, "Error lookup file");
2707 die_error(undef, "No file name defined");
2709 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2710 # blobs defined by non-textual hash id's can be cached
2715 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2716 or die_error(undef, "Couldn't cat $file_name, $hash");
2718 $type ||= blob_mimetype($fd, $file_name);
2720 # save as filename, even when no $file_name is given
2721 my $save_as = "$hash";
2722 if (defined $file_name) {
2723 $save_as = $file_name;
2724 } elsif ($type =~ m/^text\//) {
2731 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2733 binmode STDOUT, ':raw';
2735 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2743 if (!defined $hash) {
2744 if (defined $file_name) {
2745 my $base = $hash_base || git_get_head_hash($project);
2746 $hash = git_get_hash_by_path($base, $file_name, "blob")
2747 or die_error(undef, "Error lookup file");
2749 die_error(undef, "No file name defined");
2751 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2752 # blobs defined by non-textual hash id's can be cached
2756 my ($have_blame) = gitweb_check_feature('blame');
2757 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2758 or die_error(undef, "Couldn't cat $file_name, $hash");
2759 my $mimetype = blob_mimetype($fd, $file_name);
2760 if ($mimetype !~ m/^text\//) {
2762 return git_blob_plain($mimetype);
2764 git_header_html(undef, $expires);
2765 my $formats_nav = '';
2766 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2767 if (defined $file_name) {
2770 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2771 hash=>$hash, file_name=>$file_name)},
2776 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2777 hash=>$hash, file_name=>$file_name)},
2780 $cgi->a({-href => href(action=>"blob_plain",
2781 hash=>$hash, file_name=>$file_name)},
2784 $cgi->a({-href => href(action=>"blob",
2785 hash_base=>"HEAD", file_name=>$file_name)},
2789 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2791 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2792 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2794 print "<div class=\"page_nav\">\n" .
2795 "<br/><br/></div>\n" .
2796 "<div class=\"title\">$hash</div>\n";
2798 git_print_page_path($file_name, "blob", $hash_base);
2799 print "<div class=\"page_body\">\n";
2801 while (my $line = <$fd>) {
2804 $line = untabify($line);
2805 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2806 $nr, $nr, $nr, esc_html($line);
2809 or print "Reading blob failed.\n";
2815 my $have_snapshot = gitweb_have_snapshot();
2817 if (!defined $hash_base) {
2818 $hash_base = "HEAD";
2820 if (!defined $hash) {
2821 if (defined $file_name) {
2822 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2828 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2829 or die_error(undef, "Open git-ls-tree failed");
2830 my @entries = map { chomp; $_ } <$fd>;
2831 close $fd or die_error(undef, "Reading tree failed");
2834 my $refs = git_get_references();
2835 my $ref = format_ref_marker($refs, $hash_base);
2838 my ($have_blame) = gitweb_check_feature('blame');
2839 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2841 if (defined $file_name) {
2843 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2844 hash=>$hash, file_name=>$file_name)},
2846 $cgi->a({-href => href(action=>"tree",
2847 hash_base=>"HEAD", file_name=>$file_name)},
2850 if ($have_snapshot) {
2851 # FIXME: Should be available when we have no hash base as well.
2853 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2856 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2857 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2860 print "<div class=\"page_nav\">\n";
2861 print "<br/><br/></div>\n";
2862 print "<div class=\"title\">$hash</div>\n";
2864 if (defined $file_name) {
2865 $basedir = $file_name;
2866 if ($basedir ne '' && substr($basedir, -1) ne '/') {
2870 git_print_page_path($file_name, 'tree', $hash_base);
2871 print "<div class=\"page_body\">\n";
2872 print "<table cellspacing=\"0\">\n";
2874 foreach my $line (@entries) {
2875 my %t = parse_ls_tree_line($line, -z => 1);
2878 print "<tr class=\"dark\">\n";
2880 print "<tr class=\"light\">\n";
2884 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
2888 print "</table>\n" .
2894 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2895 my $have_snapshot = (defined $ctype && defined $suffix);
2896 if (!$have_snapshot) {
2897 die_error('403 Permission denied', "Permission denied");
2900 if (!defined $hash) {
2901 $hash = git_get_head_hash($project);
2904 my $filename = basename($project) . "-$hash.tar.$suffix";
2907 -type => 'application/x-tar',
2908 -content_encoding => $ctype,
2909 -content_disposition => 'inline; filename="' . "$filename" . '"',
2910 -status => '200 OK');
2912 my $git = git_cmd_str();
2913 my $name = $project;
2914 $name =~ s/\047/\047\\\047\047/g;
2916 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
2917 or die_error(undef, "Execute git-tar-tree failed.");
2918 binmode STDOUT, ':raw';
2920 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2926 my $head = git_get_head_hash($project);
2927 if (!defined $hash) {
2930 if (!defined $page) {
2933 my $refs = git_get_references();
2935 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2936 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2937 or die_error(undef, "Open git-rev-list failed");
2938 my @revlist = map { chomp; $_ } <$fd>;
2941 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2944 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2947 my %co = parse_commit($hash);
2949 git_print_header_div('summary', $project);
2950 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2952 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2953 my $commit = $revlist[$i];
2954 my $ref = format_ref_marker($refs, $commit);
2955 my %co = parse_commit($commit);
2957 my %ad = parse_date($co{'author_epoch'});
2958 git_print_header_div('commit',
2959 "<span class=\"age\">$co{'age_string'}</span>" .
2960 esc_html($co{'title'}) . $ref,
2962 print "<div class=\"title_text\">\n" .
2963 "<div class=\"log_link\">\n" .
2964 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2966 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2968 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
2971 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2974 print "<div class=\"log_body\">\n";
2975 git_print_simplified_log($co{'comment'});
2982 my %co = parse_commit($hash);
2984 die_error(undef, "Unknown commit object");
2986 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2987 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2989 my $parent = $co{'parent'};
2990 if (!defined $parent) {
2993 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2994 or die_error(undef, "Open git-diff-tree failed");
2995 my @difftree = map { chomp; $_ } <$fd>;
2996 close $fd or die_error(undef, "Reading git-diff-tree failed");
2998 # non-textual hash id's can be cached
3000 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3003 my $refs = git_get_references();
3004 my $ref = format_ref_marker($refs, $co{'id'});
3006 my $have_snapshot = gitweb_have_snapshot();
3009 if (defined $file_name && defined $co{'parent'}) {
3011 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3014 git_header_html(undef, $expires);
3015 git_print_page_nav('commit', '',
3016 $hash, $co{'tree'}, $hash,
3017 join (' | ', @views_nav));
3019 if (defined $co{'parent'}) {
3020 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3022 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3024 print "<div class=\"title_text\">\n" .
3025 "<table cellspacing=\"0\">\n";
3026 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3028 "<td></td><td> $ad{'rfc2822'}";
3029 if ($ad{'hour_local'} < 6) {
3030 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3031 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3033 printf(" (%02d:%02d %s)",
3034 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3038 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3039 print "<tr><td></td><td> $cd{'rfc2822'}" .
3040 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3042 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3045 "<td class=\"sha1\">" .
3046 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3047 class => "list"}, $co{'tree'}) .
3049 "<td class=\"link\">" .
3050 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3052 if ($have_snapshot) {
3054 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3058 my $parents = $co{'parents'};
3059 foreach my $par (@$parents) {
3062 "<td class=\"sha1\">" .
3063 $cgi->a({-href => href(action=>"commit", hash=>$par),
3064 class => "list"}, $par) .
3066 "<td class=\"link\">" .
3067 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3069 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3076 print "<div class=\"page_body\">\n";
3077 git_print_log($co{'comment'});
3080 git_difftree_body(\@difftree, $hash, $parent);
3086 my $format = shift || 'html';
3093 # preparing $fd and %diffinfo for git_patchset_body
3095 if (defined $hash_base && defined $hash_parent_base) {
3096 if (defined $file_name) {
3098 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3100 or die_error(undef, "Open git-diff-tree failed");
3101 @difftree = map { chomp; $_ } <$fd>;
3103 or die_error(undef, "Reading git-diff-tree failed");
3105 or die_error('404 Not Found', "Blob diff not found");
3107 } elsif (defined $hash &&
3108 $hash =~ /[0-9a-fA-F]{40}/) {
3109 # try to find filename from $hash
3111 # read filtered raw output
3112 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3113 or die_error(undef, "Open git-diff-tree failed");
3115 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3117 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3118 map { chomp; $_ } <$fd>;
3120 or die_error(undef, "Reading git-diff-tree failed");
3122 or die_error('404 Not Found', "Blob diff not found");
3125 die_error('404 Not Found', "Missing one of the blob diff parameters");
3128 if (@difftree > 1) {
3129 die_error('404 Not Found', "Ambiguous blob diff specification");
3132 %diffinfo = parse_difftree_raw_line($difftree[0]);
3133 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3134 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3136 $hash_parent ||= $diffinfo{'from_id'};
3137 $hash ||= $diffinfo{'to_id'};
3139 # non-textual hash id's can be cached
3140 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3141 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3146 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3147 '-p', $hash_parent_base, $hash_base,
3149 or die_error(undef, "Open git-diff-tree failed");
3152 # old/legacy style URI
3153 if (!%diffinfo && # if new style URI failed
3154 defined $hash && defined $hash_parent) {
3155 # fake git-diff-tree raw output
3156 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3157 $diffinfo{'from_id'} = $hash_parent;
3158 $diffinfo{'to_id'} = $hash;
3159 if (defined $file_name) {
3160 if (defined $file_parent) {
3161 $diffinfo{'status'} = '2';
3162 $diffinfo{'from_file'} = $file_parent;
3163 $diffinfo{'to_file'} = $file_name;
3164 } else { # assume not renamed
3165 $diffinfo{'status'} = '1';
3166 $diffinfo{'from_file'} = $file_name;
3167 $diffinfo{'to_file'} = $file_name;
3169 } else { # no filename given
3170 $diffinfo{'status'} = '2';
3171 $diffinfo{'from_file'} = $hash_parent;
3172 $diffinfo{'to_file'} = $hash;
3175 # non-textual hash id's can be cached
3176 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3177 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3182 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3183 or die_error(undef, "Open git-diff failed");
3185 die_error('404 Not Found', "Missing one of the blob diff parameters")
3190 if ($format eq 'html') {
3192 $cgi->a({-href => href(action=>"blobdiff_plain",
3193 hash=>$hash, hash_parent=>$hash_parent,
3194 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3195 file_name=>$file_name, file_parent=>$file_parent)},
3197 git_header_html(undef, $expires);
3198 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3199 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3200 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3202 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3203 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3205 if (defined $file_name) {
3206 git_print_page_path($file_name, "blob", $hash_base);
3208 print "<div class=\"page_path\"></div>\n";
3211 } elsif ($format eq 'plain') {
3213 -type => 'text/plain',
3214 -charset => 'utf-8',
3215 -expires => $expires,
3216 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3218 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3221 die_error(undef, "Unknown blobdiff format");
3225 if ($format eq 'html') {
3226 print "<div class=\"page_body\">\n";
3228 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3231 print "</div>\n"; # class="page_body"
3235 while (my $line = <$fd>) {
3236 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3237 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3241 last if $line =~ m!^\+\+\+!;
3249 sub git_blobdiff_plain {
3250 git_blobdiff('plain');
3253 sub git_commitdiff {
3254 my $format = shift || 'html';
3255 my %co = parse_commit($hash);
3257 die_error(undef, "Unknown commit object");
3259 if (!defined $hash_parent) {
3260 $hash_parent = $co{'parent'} || '--root';
3266 if ($format eq 'html') {
3267 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3268 "--patch-with-raw", "--full-index", $hash_parent, $hash
3269 or die_error(undef, "Open git-diff-tree failed");
3271 while (chomp(my $line = <$fd>)) {
3272 # empty line ends raw part of diff-tree output
3274 push @difftree, $line;
3277 } elsif ($format eq 'plain') {
3278 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3279 '-p', $hash_parent, $hash
3280 or die_error(undef, "Open git-diff-tree failed");
3283 die_error(undef, "Unknown commitdiff format");
3286 # non-textual hash id's can be cached
3288 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3292 # write commit message
3293 if ($format eq 'html') {
3294 my $refs = git_get_references();
3295 my $ref = format_ref_marker($refs, $co{'id'});
3297 $cgi->a({-href => href(action=>"commitdiff_plain",
3298 hash=>$hash, hash_parent=>$hash_parent)},
3301 git_header_html(undef, $expires);
3302 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3303 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3304 git_print_authorship(\%co);
3305 print "<div class=\"page_body\">\n";
3306 print "<div class=\"log\">\n";
3307 git_print_simplified_log($co{'comment'}, 1); # skip title
3308 print "</div>\n"; # class="log"
3310 } elsif ($format eq 'plain') {
3311 my $refs = git_get_references("tags");
3312 my $tagname = git_get_rev_name_tags($hash);
3313 my $filename = basename($project) . "-$hash.patch";
3316 -type => 'text/plain',
3317 -charset => 'utf-8',
3318 -expires => $expires,
3319 -content_disposition => 'inline; filename="' . "$filename" . '"');
3320 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3323 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3324 Subject: $co{'title'}
3326 print "X-Git-Tag: $tagname\n" if $tagname;
3327 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3329 foreach my $line (@{$co{'comment'}}) {
3336 if ($format eq 'html') {
3337 git_difftree_body(\@difftree, $hash, $hash_parent);
3340 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3342 print "</div>\n"; # class="page_body"
3345 } elsif ($format eq 'plain') {
3349 or print "Reading git-diff-tree failed\n";
3353 sub git_commitdiff_plain {
3354 git_commitdiff('plain');
3358 if (!defined $hash_base) {
3359 $hash_base = git_get_head_hash($project);
3361 if (!defined $page) {
3365 my %co = parse_commit($hash_base);
3367 die_error(undef, "Unknown commit object");
3370 my $refs = git_get_references();
3371 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3373 if (!defined $hash && defined $file_name) {
3374 $hash = git_get_hash_by_path($hash_base, $file_name);
3376 if (defined $hash) {
3377 $ftype = git_get_type($hash);
3381 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3382 or die_error(undef, "Open git-rev-list-failed");
3383 my @revlist = map { chomp; $_ } <$fd>;
3385 or die_error(undef, "Reading git-rev-list failed");
3387 my $paging_nav = '';
3390 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3391 file_name=>$file_name)},
3393 $paging_nav .= " ⋅ " .
3394 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3395 file_name=>$file_name, page=>$page-1),
3396 -accesskey => "p", -title => "Alt-p"}, "prev");
3398 $paging_nav .= "first";
3399 $paging_nav .= " ⋅ prev";
3401 if ($#revlist >= (100 * ($page+1)-1)) {
3402 $paging_nav .= " ⋅ " .
3403 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3404 file_name=>$file_name, page=>$page+1),
3405 -accesskey => "n", -title => "Alt-n"}, "next");
3407 $paging_nav .= " ⋅ next";
3410 if ($#revlist >= (100 * ($page+1)-1)) {
3412 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3413 file_name=>$file_name, page=>$page+1),
3414 -title => "Alt-n"}, "next");
3418 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3419 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3420 git_print_page_path($file_name, $ftype, $hash_base);
3422 git_history_body(\@revlist, ($page * 100), $#revlist,
3423 $refs, $hash_base, $ftype, $next_link);
3429 if (!defined $searchtext) {
3430 die_error(undef, "Text field empty");
3432 if (!defined $hash) {
3433 $hash = git_get_head_hash($project);
3435 my %co = parse_commit($hash);
3437 die_error(undef, "Unknown commit object");
3440 my $commit_search = 1;
3441 my $author_search = 0;
3442 my $committer_search = 0;
3443 my $pickaxe_search = 0;
3444 if ($searchtext =~ s/^author\\://i) {
3446 } elsif ($searchtext =~ s/^committer\\://i) {
3447 $committer_search = 1;
3448 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3450 $pickaxe_search = 1;
3452 # pickaxe may take all resources of your box and run for several minutes
3453 # with every query - so decide by yourself how public you make this feature
3454 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3455 if (!$have_pickaxe) {
3456 die_error('403 Permission denied', "Permission denied");
3460 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3461 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3463 print "<table cellspacing=\"0\">\n";
3465 if ($commit_search) {
3467 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3468 while (my $commit_text = <$fd>) {
3469 if (!grep m/$searchtext/i, $commit_text) {
3472 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3475 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3478 my @commit_lines = split "\n", $commit_text;
3479 my %co = parse_commit(undef, \@commit_lines);
3484 print "<tr class=\"dark\">\n";
3486 print "<tr class=\"light\">\n";
3489 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3490 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3492 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3493 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3494 my $comment = $co{'comment'};
3495 foreach my $line (@$comment) {
3496 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3497 my $lead = esc_html($1) || "";
3498 $lead = chop_str($lead, 30, 10);
3499 my $match = esc_html($2) || "";
3500 my $trail = esc_html($3) || "";
3501 $trail = chop_str($trail, 30, 10);
3502 my $text = "$lead<span class=\"match\">$match</span>$trail";
3503 print chop_str($text, 80, 5) . "<br/>\n";
3507 "<td class=\"link\">" .
3508 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3510 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3517 if ($pickaxe_search) {
3519 my $git_command = git_cmd_str();
3520 open my $fd, "-|", "$git_command rev-list $hash | " .
3521 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3524 while (my $line = <$fd>) {
3525 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3528 $set{'from_id'} = $3;
3530 $set{'id'} = $set{'to_id'};
3531 if ($set{'id'} =~ m/0{40}/) {
3532 $set{'id'} = $set{'from_id'};
3534 if ($set{'id'} =~ m/0{40}/) {
3538 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3541 print "<tr class=\"dark\">\n";
3543 print "<tr class=\"light\">\n";
3546 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3547 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3549 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3550 -class => "list subject"},
3551 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3552 while (my $setref = shift @files) {
3554 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3555 hash=>$set{'id'}, file_name=>$set{'file'}),
3557 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3561 "<td class=\"link\">" .
3562 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3564 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3568 %co = parse_commit($1);
3578 my $head = git_get_head_hash($project);
3579 if (!defined $hash) {
3582 if (!defined $page) {
3585 my $refs = git_get_references();
3587 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3588 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3589 or die_error(undef, "Open git-rev-list failed");
3590 my @revlist = map { chomp; $_ } <$fd>;
3593 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3595 if ($#revlist >= (100 * ($page+1)-1)) {
3597 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3598 -title => "Alt-n"}, "next");
3603 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3604 git_print_header_div('summary', $project);
3606 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3611 ## ......................................................................
3612 ## feeds (RSS, OPML)
3615 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3616 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3617 or die_error(undef, "Open git-rev-list failed");
3618 my @revlist = map { chomp; $_ } <$fd>;
3619 close $fd or die_error(undef, "Reading git-rev-list failed");
3620 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3622 <?xml version="1.0" encoding="utf-8"?>
3623 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3625 <title>$project $my_uri $my_url</title>
3626 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3627 <description>$project log</description>
3628 <language>en</language>
3631 for (my $i = 0; $i <= $#revlist; $i++) {
3632 my $commit = $revlist[$i];
3633 my %co = parse_commit($commit);
3634 # we read 150, we always show 30 and the ones more recent than 48 hours
3635 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3638 my %cd = parse_date($co{'committer_epoch'});
3639 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3640 $co{'parent'}, $co{'id'}
3642 my @difftree = map { chomp; $_ } <$fd>;
3647 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3649 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3650 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3651 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3652 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3653 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3654 "<content:encoded>" .
3656 my $comment = $co{'comment'};
3657 foreach my $line (@$comment) {
3658 $line = to_utf8($line);
3659 print "$line<br/>\n";
3662 foreach my $line (@difftree) {
3663 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3666 my $file = esc_html(unquote($7));
3667 $file = to_utf8($file);
3668 print "$file<br/>\n";
3671 "</content:encoded>\n" .
3674 print "</channel></rss>";
3678 my @list = git_get_projects_list();
3680 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3682 <?xml version="1.0" encoding="utf-8"?>
3683 <opml version="1.0">
3685 <title>$site_name Git OPML Export</title>
3688 <outline text="git RSS feeds">
3691 foreach my $pr (@list) {
3693 my $head = git_get_head_hash($proj{'path'});
3694 if (!defined $head) {
3697 $git_dir = "$projectroot/$proj{'path'}";
3698 my %co = parse_commit($head);
3703 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3704 my $rss = "$my_url?p=$proj{'path'};a=rss";
3705 my $html = "$my_url?p=$proj{'path'};a=summary";
3706 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";