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 # filename of html text to include at top of each page
45 our $site_header = "++GITWEB_SITE_HEADER++";
46 # html text to include at home page
47 our $home_text = "++GITWEB_HOMETEXT++";
48 # filename of html text to include at bottom of each page
49 our $site_footer = "++GITWEB_SITE_FOOTER++";
52 our @stylesheets = ("++GITWEB_CSS++");
54 # default is not to define style sheet, but it can be overwritten later
57 # URI of default stylesheet
58 our $stylesheet = "++GITWEB_CSS++";
59 # URI of GIT logo (72x27 size)
60 our $logo = "++GITWEB_LOGO++";
61 # URI of GIT favicon, assumed to be image/png type
62 our $favicon = "++GITWEB_FAVICON++";
64 # URI and label (title) of GIT logo link
65 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
66 #our $logo_label = "git documentation";
67 our $logo_url = "http://git.or.cz/";
68 our $logo_label = "git homepage";
70 # source of projects list
71 our $projects_list = "++GITWEB_LIST++";
73 # show repository only if this file exists
74 # (only effective if this variable evaluates to true)
75 our $export_ok = "++GITWEB_EXPORT_OK++";
77 # only allow viewing of repositories also shown on the overview page
78 our $strict_export = "++GITWEB_STRICT_EXPORT++";
80 # list of git base URLs used for URL to where fetch project from,
81 # i.e. full URL is "$git_base_url/$project"
82 our @git_base_url_list = ("++GITWEB_BASE_URL++");
84 # default blob_plain mimetype and default charset for text/plain blob
85 our $default_blob_plain_mimetype = 'text/plain';
86 our $default_text_plain_charset = undef;
88 # file to use for guessing MIME types before trying /etc/mime.types
89 # (relative to the current git repository)
90 our $mimetypes_file = undef;
92 # You define site-wide feature defaults here; override them with
93 # $GITWEB_CONFIG as necessary.
96 # 'sub' => feature-sub (subroutine),
97 # 'override' => allow-override (boolean),
98 # 'default' => [ default options...] (array reference)}
100 # if feature is overridable (it means that allow-override has true value,
101 # then feature-sub will be called with default options as parameters;
102 # return value of feature-sub indicates if to enable specified feature
104 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
106 # Enable the 'blame' blob view, showing the last commit that modified
107 # each line in the file. This can be very CPU-intensive.
109 # To enable system wide have in $GITWEB_CONFIG
110 # $feature{'blame'}{'default'} = [1];
111 # To have project specific config enable override in $GITWEB_CONFIG
112 # $feature{'blame'}{'override'} = 1;
113 # and in project config gitweb.blame = 0|1;
115 'sub' => \&feature_blame,
119 # Enable the 'snapshot' link, providing a compressed tarball of any
120 # tree. This can potentially generate high traffic if you have large
123 # To disable system wide have in $GITWEB_CONFIG
124 # $feature{'snapshot'}{'default'} = [undef];
125 # To have project specific config enable override in $GITWEB_CONFIG
126 # $feature{'blame'}{'override'} = 1;
127 # and in project config gitweb.snapshot = none|gzip|bzip2;
129 'sub' => \&feature_snapshot,
131 # => [content-encoding, suffix, program]
132 'default' => ['x-gzip', 'gz', 'gzip']},
134 # Enable the pickaxe search, which will list the commits that modified
135 # a given string in a file. This can be practical and quite faster
136 # alternative to 'blame', but still potentially CPU-intensive.
138 # To enable system wide have in $GITWEB_CONFIG
139 # $feature{'pickaxe'}{'default'} = [1];
140 # To have project specific config enable override in $GITWEB_CONFIG
141 # $feature{'pickaxe'}{'override'} = 1;
142 # and in project config gitweb.pickaxe = 0|1;
144 'sub' => \&feature_pickaxe,
148 # Make gitweb use an alternative format of the URLs which can be
149 # more readable and natural-looking: project name is embedded
150 # directly in the path and the query string contains other
151 # auxiliary information. All gitweb installations recognize
152 # URL in either format; this configures in which formats gitweb
155 # To enable system wide have in $GITWEB_CONFIG
156 # $feature{'pathinfo'}{'default'} = [1];
157 # Project specific override is not supported.
159 # Note that you will need to change the default location of CSS,
160 # favicon, logo and possibly other files to an absolute URL. Also,
161 # if gitweb.cgi serves as your indexfile, you will need to force
162 # $my_uri to contain the script name in your $GITWEB_CONFIG.
168 sub gitweb_check_feature {
170 return unless exists $feature{$name};
171 my ($sub, $override, @defaults) = (
172 $feature{$name}{'sub'},
173 $feature{$name}{'override'},
174 @{$feature{$name}{'default'}});
175 if (!$override) { return @defaults; }
177 warn "feature $name is not overrideable";
180 return $sub->(@defaults);
184 my ($val) = git_get_project_config('blame', '--bool');
186 if ($val eq 'true') {
188 } elsif ($val eq 'false') {
195 sub feature_snapshot {
196 my ($ctype, $suffix, $command) = @_;
198 my ($val) = git_get_project_config('snapshot');
200 if ($val eq 'gzip') {
201 return ('x-gzip', 'gz', 'gzip');
202 } elsif ($val eq 'bzip2') {
203 return ('x-bzip2', 'bz2', 'bzip2');
204 } elsif ($val eq 'none') {
208 return ($ctype, $suffix, $command);
211 sub gitweb_have_snapshot {
212 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
213 my $have_snapshot = (defined $ctype && defined $suffix);
215 return $have_snapshot;
218 sub feature_pickaxe {
219 my ($val) = git_get_project_config('pickaxe', '--bool');
221 if ($val eq 'true') {
223 } elsif ($val eq 'false') {
230 # checking HEAD file with -e is fragile if the repository was
231 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
233 sub check_head_link {
235 my $headfile = "$dir/HEAD";
236 return ((-e $headfile) ||
237 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
240 sub check_export_ok {
242 return (check_head_link($dir) &&
243 (!$export_ok || -e "$dir/$export_ok"));
246 # rename detection options for git-diff and git-diff-tree
247 # - default is '-M', with the cost proportional to
248 # (number of removed files) * (number of new files).
249 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
250 # (number of changed files + number of removed files) * (number of new files)
251 # - even more costly is '-C', '--find-copies-harder' with cost
252 # (number of files in the original tree) * (number of new files)
253 # - one might want to include '-B' option, e.g. '-B', '-M'
254 our @diff_opts = ('-M'); # taken from git_commit
256 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
257 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
259 # version of the core git binary
260 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
262 $projects_list ||= $projectroot;
264 # ======================================================================
265 # input validation and dispatch
266 our $action = $cgi->param('a');
267 if (defined $action) {
268 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
269 die_error(undef, "Invalid action parameter");
273 # parameters which are pathnames
274 our $project = $cgi->param('p');
275 if (defined $project) {
276 if (!validate_pathname($project) ||
277 !(-d "$projectroot/$project") ||
278 !check_head_link("$projectroot/$project") ||
279 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
280 ($strict_export && !project_in_list($project))) {
282 die_error(undef, "No such project");
286 our $file_name = $cgi->param('f');
287 if (defined $file_name) {
288 if (!validate_pathname($file_name)) {
289 die_error(undef, "Invalid file parameter");
293 our $file_parent = $cgi->param('fp');
294 if (defined $file_parent) {
295 if (!validate_pathname($file_parent)) {
296 die_error(undef, "Invalid file parent parameter");
300 # parameters which are refnames
301 our $hash = $cgi->param('h');
303 if (!validate_refname($hash)) {
304 die_error(undef, "Invalid hash parameter");
308 our $hash_parent = $cgi->param('hp');
309 if (defined $hash_parent) {
310 if (!validate_refname($hash_parent)) {
311 die_error(undef, "Invalid hash parent parameter");
315 our $hash_base = $cgi->param('hb');
316 if (defined $hash_base) {
317 if (!validate_refname($hash_base)) {
318 die_error(undef, "Invalid hash base parameter");
322 our $hash_parent_base = $cgi->param('hpb');
323 if (defined $hash_parent_base) {
324 if (!validate_refname($hash_parent_base)) {
325 die_error(undef, "Invalid hash parent base parameter");
330 our $page = $cgi->param('pg');
332 if ($page =~ m/[^0-9]/) {
333 die_error(undef, "Invalid page parameter");
337 our $searchtext = $cgi->param('s');
338 if (defined $searchtext) {
339 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
340 die_error(undef, "Invalid search parameter");
342 $searchtext = quotemeta $searchtext;
345 # now read PATH_INFO and use it as alternative to parameters
346 sub evaluate_path_info {
347 return if defined $project;
348 my $path_info = $ENV{"PATH_INFO"};
349 return if !$path_info;
350 $path_info =~ s,^/+,,;
351 return if !$path_info;
352 # find which part of PATH_INFO is project
353 $project = $path_info;
355 while ($project && !check_head_link("$projectroot/$project")) {
356 $project =~ s,/*[^/]*$,,;
359 $project = validate_pathname($project);
361 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
362 ($strict_export && !project_in_list($project))) {
366 # do not change any parameters if an action is given using the query string
368 $path_info =~ s,^$project/*,,;
369 my ($refname, $pathname) = split(/:/, $path_info, 2);
370 if (defined $pathname) {
371 # we got "project.git/branch:filename" or "project.git/branch:dir/"
372 # we could use git_get_type(branch:pathname), but it needs $git_dir
373 $pathname =~ s,^/+,,;
374 if (!$pathname || substr($pathname, -1) eq "/") {
378 $action ||= "blob_plain";
380 $hash_base ||= validate_refname($refname);
381 $file_name ||= validate_pathname($pathname);
382 } elsif (defined $refname) {
383 # we got "project.git/branch"
384 $action ||= "shortlog";
385 $hash ||= validate_refname($refname);
388 evaluate_path_info();
390 # path to the current git repository
392 $git_dir = "$projectroot/$project" if $project;
396 "blame" => \&git_blame2,
397 "blobdiff" => \&git_blobdiff,
398 "blobdiff_plain" => \&git_blobdiff_plain,
399 "blob" => \&git_blob,
400 "blob_plain" => \&git_blob_plain,
401 "commitdiff" => \&git_commitdiff,
402 "commitdiff_plain" => \&git_commitdiff_plain,
403 "commit" => \&git_commit,
404 "heads" => \&git_heads,
405 "history" => \&git_history,
408 "search" => \&git_search,
409 "shortlog" => \&git_shortlog,
410 "summary" => \&git_summary,
412 "tags" => \&git_tags,
413 "tree" => \&git_tree,
414 "snapshot" => \&git_snapshot,
415 # those below don't need $project
416 "opml" => \&git_opml,
417 "project_list" => \&git_project_list,
418 "project_index" => \&git_project_index,
421 if (defined $project) {
422 $action ||= 'summary';
424 $action ||= 'project_list';
426 if (!defined($actions{$action})) {
427 die_error(undef, "Unknown action");
429 if ($action !~ m/^(opml|project_list|project_index)$/ &&
431 die_error(undef, "Project needed");
433 $actions{$action}->();
436 ## ======================================================================
443 # XXX: Warning: If you touch this, check the search form for updating,
454 hash_parent_base => "hpb",
459 my %mapping = @mapping;
461 $params{'project'} = $project unless exists $params{'project'};
463 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
465 # use PATH_INFO for project name
466 $href .= "/$params{'project'}" if defined $params{'project'};
467 delete $params{'project'};
469 # Summary just uses the project path URL
470 if (defined $params{'action'} && $params{'action'} eq 'summary') {
471 delete $params{'action'};
475 # now encode the parameters explicitly
477 for (my $i = 0; $i < @mapping; $i += 2) {
478 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
479 if (defined $params{$name}) {
480 push @result, $symbol . "=" . esc_param($params{$name});
483 $href .= "?" . join(';', @result) if scalar @result;
489 ## ======================================================================
490 ## validation, quoting/unquoting and escaping
492 sub validate_pathname {
493 my $input = shift || return undef;
495 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
496 # at the beginning, at the end, and between slashes.
497 # also this catches doubled slashes
498 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
502 if ($input =~ m!\0!) {
508 sub validate_refname {
509 my $input = shift || return undef;
511 # textual hashes are O.K.
512 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
515 # it must be correct pathname
516 $input = validate_pathname($input)
518 # restrictions on ref name according to git-check-ref-format
519 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
525 # very thin wrapper for decode("utf8", $str, Encode::FB_DEFAULT);
528 return decode("utf8", $str, Encode::FB_DEFAULT);
531 # quote unsafe chars, but keep the slash, even when it's not
532 # correct, but quoted slashes look too horrible in bookmarks
535 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
541 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
544 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
550 # replace invalid utf8 character with SUBSTITUTION sequence
553 $str = to_utf8($str);
554 $str = escapeHTML($str);
555 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
556 $str =~ s/\033/^[/g; # "escape" ESCAPE (\e) character (e.g. commit 20a3847d8a5032ce41f90dcc68abfb36e6fee9b1)
560 # git may return quoted and escaped filenames
563 if ($str =~ m/^"(.*)"$/) {
565 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
570 # escape tabs (convert tabs to spaces)
574 while ((my $pos = index($line, "\t")) != -1) {
575 if (my $count = (8 - ($pos % 8))) {
576 my $spaces = ' ' x $count;
577 $line =~ s/\t/$spaces/;
584 sub project_in_list {
586 my @list = git_get_projects_list();
587 return @list && scalar(grep { $_->{'path'} eq $project } @list);
590 ## ----------------------------------------------------------------------
591 ## HTML aware string manipulation
596 my $add_len = shift || 10;
598 # allow only $len chars, but don't cut a word if it would fit in $add_len
599 # if it doesn't fit, cut it if it's still longer than the dots we would add
600 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
603 if (length($tail) > 4) {
605 $body =~ s/&[^;]*$//; # remove chopped character entities
610 ## ----------------------------------------------------------------------
611 ## functions returning short strings
613 # CSS class for given age value (in seconds)
617 if ($age < 60*60*2) {
619 } elsif ($age < 60*60*24*2) {
626 # convert age in seconds to "nn units ago" string
631 if ($age > 60*60*24*365*2) {
632 $age_str = (int $age/60/60/24/365);
633 $age_str .= " years ago";
634 } elsif ($age > 60*60*24*(365/12)*2) {
635 $age_str = int $age/60/60/24/(365/12);
636 $age_str .= " months ago";
637 } elsif ($age > 60*60*24*7*2) {
638 $age_str = int $age/60/60/24/7;
639 $age_str .= " weeks ago";
640 } elsif ($age > 60*60*24*2) {
641 $age_str = int $age/60/60/24;
642 $age_str .= " days ago";
643 } elsif ($age > 60*60*2) {
644 $age_str = int $age/60/60;
645 $age_str .= " hours ago";
646 } elsif ($age > 60*2) {
647 $age_str = int $age/60;
648 $age_str .= " min ago";
651 $age_str .= " sec ago";
653 $age_str .= " right now";
658 # convert file mode in octal to symbolic file mode string
660 my $mode = oct shift;
662 if (S_ISDIR($mode & S_IFMT)) {
664 } elsif (S_ISLNK($mode)) {
666 } elsif (S_ISREG($mode)) {
667 # git cares only about the executable bit
668 if ($mode & S_IXUSR) {
678 # convert file mode in octal to file type string
682 if ($mode !~ m/^[0-7]+$/) {
688 if (S_ISDIR($mode & S_IFMT)) {
690 } elsif (S_ISLNK($mode)) {
692 } elsif (S_ISREG($mode)) {
699 ## ----------------------------------------------------------------------
700 ## functions returning short HTML fragments, or transforming HTML fragments
701 ## which don't beling to other sections
703 # format line of commit message or tag comment
704 sub format_log_line_html {
707 $line = esc_html($line);
708 $line =~ s/ / /g;
709 if ($line =~ m/([0-9a-fA-F]{40})/) {
711 if (git_get_type($hash_text) eq "commit") {
713 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
714 -class => "text"}, $hash_text);
715 $line =~ s/$hash_text/$link/;
721 # format marker of refs pointing to given object
722 sub format_ref_marker {
723 my ($refs, $id) = @_;
726 if (defined $refs->{$id}) {
727 foreach my $ref (@{$refs->{$id}}) {
728 my ($type, $name) = qw();
729 # e.g. tags/v2.6.11 or heads/next
730 if ($ref =~ m!^(.*?)s?/(.*)$!) {
738 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
743 return ' <span class="refs">'. $markers . '</span>';
749 # format, perhaps shortened and with markers, title line
750 sub format_subject_html {
751 my ($long, $short, $href, $extra) = @_;
752 $extra = '' unless defined($extra);
754 if (length($short) < length($long)) {
755 return $cgi->a({-href => $href, -class => "list subject",
756 -title => to_utf8($long)},
757 esc_html($short) . $extra);
759 return $cgi->a({-href => $href, -class => "list subject"},
760 esc_html($long) . $extra);
764 sub format_diff_line {
766 my $char = substr($line, 0, 1);
772 $diff_class = " add";
773 } elsif ($char eq "-") {
774 $diff_class = " rem";
775 } elsif ($char eq "@") {
776 $diff_class = " chunk_header";
777 } elsif ($char eq "\\") {
778 $diff_class = " incomplete";
780 $line = untabify($line);
781 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
784 ## ----------------------------------------------------------------------
785 ## git utility subroutines, invoking git commands
787 # returns path to the core git executable and the --git-dir parameter as list
789 return $GIT, '--git-dir='.$git_dir;
792 # returns path to the core git executable and the --git-dir parameter as string
794 return join(' ', git_cmd());
797 # get HEAD ref of given project as hash
798 sub git_get_head_hash {
800 my $o_git_dir = $git_dir;
802 $git_dir = "$projectroot/$project";
803 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
806 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
810 if (defined $o_git_dir) {
811 $git_dir = $o_git_dir;
816 # get type of given object
820 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
827 sub git_get_project_config {
828 my ($key, $type) = @_;
830 return unless ($key);
831 $key =~ s/^gitweb\.//;
832 return if ($key =~ m/\W/);
834 my @x = (git_cmd(), 'repo-config');
835 if (defined $type) { push @x, $type; }
837 push @x, "gitweb.$key";
843 # get hash of given path at given ref
844 sub git_get_hash_by_path {
846 my $path = shift || return undef;
851 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
852 or die_error(undef, "Open git-ls-tree failed");
854 close $fd or return undef;
856 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
857 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
858 if (defined $type && $type ne $2) {
865 ## ......................................................................
866 ## git utility functions, directly accessing git repository
868 sub git_get_project_description {
871 open my $fd, "$projectroot/$path/description" or return undef;
878 sub git_get_project_url_list {
881 open my $fd, "$projectroot/$path/cloneurl" or return;
882 my @git_project_url_list = map { chomp; $_ } <$fd>;
885 return wantarray ? @git_project_url_list : \@git_project_url_list;
888 sub git_get_projects_list {
891 if (-d $projects_list) {
892 # search in directory
893 my $dir = $projects_list;
894 my $pfxlen = length("$dir");
897 follow_fast => 1, # follow symbolic links
898 dangling_symlinks => 0, # ignore dangling symlinks, silently
900 # skip project-list toplevel, if we get it.
901 return if (m!^[/.]$!);
902 # only directories can be git repositories
903 return unless (-d $_);
905 my $subdir = substr($File::Find::name, $pfxlen + 1);
906 # we check related file in $projectroot
907 if (check_export_ok("$projectroot/$subdir")) {
908 push @list, { path => $subdir };
909 $File::Find::prune = 1;
914 } elsif (-f $projects_list) {
915 # read from file(url-encoded):
916 # 'git%2Fgit.git Linus+Torvalds'
917 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
918 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
919 open my ($fd), $projects_list or return;
920 while (my $line = <$fd>) {
922 my ($path, $owner) = split ' ', $line;
923 $path = unescape($path);
924 $owner = unescape($owner);
925 if (!defined $path) {
928 if (check_export_ok("$projectroot/$path")) {
931 owner => to_utf8($owner),
938 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
942 sub git_get_project_owner {
946 return undef unless $project;
948 # read from file (url-encoded):
949 # 'git%2Fgit.git Linus+Torvalds'
950 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
951 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
952 if (-f $projects_list) {
953 open (my $fd , $projects_list);
954 while (my $line = <$fd>) {
956 my ($pr, $ow) = split ' ', $line;
959 if ($pr eq $project) {
960 $owner = to_utf8($ow);
966 if (!defined $owner) {
967 $owner = get_file_owner("$projectroot/$project");
973 sub git_get_references {
974 my $type = shift || "";
976 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
977 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
978 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
981 while (my $line = <$fd>) {
983 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
984 if (defined $refs{$1}) {
985 push @{$refs{$1}}, $2;
995 sub git_get_rev_name_tags {
996 my $hash = shift || return undef;
998 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1000 my $name_rev = <$fd>;
1003 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1006 # catches also '$hash undefined' output
1011 ## ----------------------------------------------------------------------
1012 ## parse to hash functions
1016 my $tz = shift || "-0000";
1019 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1020 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1021 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1022 $date{'hour'} = $hour;
1023 $date{'minute'} = $min;
1024 $date{'mday'} = $mday;
1025 $date{'day'} = $days[$wday];
1026 $date{'month'} = $months[$mon];
1027 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1028 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1029 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1030 $mday, $months[$mon], $hour ,$min;
1032 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1033 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1034 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1035 $date{'hour_local'} = $hour;
1036 $date{'minute_local'} = $min;
1037 $date{'tz_local'} = $tz;
1046 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1047 $tag{'id'} = $tag_id;
1048 while (my $line = <$fd>) {
1050 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1051 $tag{'object'} = $1;
1052 } elsif ($line =~ m/^type (.+)$/) {
1054 } elsif ($line =~ m/^tag (.+)$/) {
1056 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1057 $tag{'author'} = $1;
1060 } elsif ($line =~ m/--BEGIN/) {
1061 push @comment, $line;
1063 } elsif ($line eq "") {
1067 push @comment, <$fd>;
1068 $tag{'comment'} = \@comment;
1069 close $fd or return;
1070 if (!defined $tag{'name'}) {
1076 sub git_get_last_activity {
1080 $git_dir = "$projectroot/$path";
1081 open($fd, "-|", git_cmd(), 'for-each-ref',
1082 '--format=%(refname) %(committer)',
1083 '--sort=-committerdate',
1084 'refs/heads') or return;
1085 my $most_recent = <$fd>;
1086 close $fd or return;
1087 if ($most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1089 my $age = time - $timestamp;
1090 return ($age, age_string($age));
1095 my $commit_id = shift;
1096 my $commit_text = shift;
1101 if (defined $commit_text) {
1102 @commit_lines = @$commit_text;
1105 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
1107 @commit_lines = split '\n', <$fd>;
1108 close $fd or return;
1111 my $header = shift @commit_lines;
1112 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1115 ($co{'id'}, my @parents) = split ' ', $header;
1116 $co{'parents'} = \@parents;
1117 $co{'parent'} = $parents[0];
1118 while (my $line = shift @commit_lines) {
1119 last if $line eq "\n";
1120 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1122 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1124 $co{'author_epoch'} = $2;
1125 $co{'author_tz'} = $3;
1126 if ($co{'author'} =~ m/^([^<]+) </) {
1127 $co{'author_name'} = $1;
1129 $co{'author_name'} = $co{'author'};
1131 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1132 $co{'committer'} = $1;
1133 $co{'committer_epoch'} = $2;
1134 $co{'committer_tz'} = $3;
1135 $co{'committer_name'} = $co{'committer'};
1136 $co{'committer_name'} =~ s/ <.*//;
1139 if (!defined $co{'tree'}) {
1143 foreach my $title (@commit_lines) {
1146 $co{'title'} = chop_str($title, 80, 5);
1147 # remove leading stuff of merges to make the interesting part visible
1148 if (length($title) > 50) {
1149 $title =~ s/^Automatic //;
1150 $title =~ s/^merge (of|with) /Merge ... /i;
1151 if (length($title) > 50) {
1152 $title =~ s/(http|rsync):\/\///;
1154 if (length($title) > 50) {
1155 $title =~ s/(master|www|rsync)\.//;
1157 if (length($title) > 50) {
1158 $title =~ s/kernel.org:?//;
1160 if (length($title) > 50) {
1161 $title =~ s/\/pub\/scm//;
1164 $co{'title_short'} = chop_str($title, 50, 5);
1168 if ($co{'title'} eq "") {
1169 $co{'title'} = $co{'title_short'} = '(no commit message)';
1171 # remove added spaces
1172 foreach my $line (@commit_lines) {
1175 $co{'comment'} = \@commit_lines;
1177 my $age = time - $co{'committer_epoch'};
1179 $co{'age_string'} = age_string($age);
1180 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1181 if ($age > 60*60*24*7*2) {
1182 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1183 $co{'age_string_age'} = $co{'age_string'};
1185 $co{'age_string_date'} = $co{'age_string'};
1186 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1191 # parse ref from ref_file, given by ref_id, with given type
1193 my $ref_file = shift;
1195 my $type = shift || git_get_type($ref_id);
1198 $ref_item{'type'} = $type;
1199 $ref_item{'id'} = $ref_id;
1200 $ref_item{'epoch'} = 0;
1201 $ref_item{'age'} = "unknown";
1202 if ($type eq "tag") {
1203 my %tag = parse_tag($ref_id);
1204 $ref_item{'comment'} = $tag{'comment'};
1205 if ($tag{'type'} eq "commit") {
1206 my %co = parse_commit($tag{'object'});
1207 $ref_item{'epoch'} = $co{'committer_epoch'};
1208 $ref_item{'age'} = $co{'age_string'};
1209 } elsif (defined($tag{'epoch'})) {
1210 my $age = time - $tag{'epoch'};
1211 $ref_item{'epoch'} = $tag{'epoch'};
1212 $ref_item{'age'} = age_string($age);
1214 $ref_item{'reftype'} = $tag{'type'};
1215 $ref_item{'name'} = $tag{'name'};
1216 $ref_item{'refid'} = $tag{'object'};
1217 } elsif ($type eq "commit"){
1218 my %co = parse_commit($ref_id);
1219 $ref_item{'reftype'} = "commit";
1220 $ref_item{'name'} = $ref_file;
1221 $ref_item{'title'} = $co{'title'};
1222 $ref_item{'refid'} = $ref_id;
1223 $ref_item{'epoch'} = $co{'committer_epoch'};
1224 $ref_item{'age'} = $co{'age_string'};
1226 $ref_item{'reftype'} = $type;
1227 $ref_item{'name'} = $ref_file;
1228 $ref_item{'refid'} = $ref_id;
1234 # parse line of git-diff-tree "raw" output
1235 sub parse_difftree_raw_line {
1239 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1240 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1241 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1242 $res{'from_mode'} = $1;
1243 $res{'to_mode'} = $2;
1244 $res{'from_id'} = $3;
1246 $res{'status'} = $5;
1247 $res{'similarity'} = $6;
1248 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1249 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1251 $res{'file'} = unquote($7);
1254 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1255 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1256 $res{'commit'} = $1;
1259 return wantarray ? %res : \%res;
1262 # parse line of git-ls-tree output
1263 sub parse_ls_tree_line ($;%) {
1268 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1269 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1277 $res{'name'} = unquote($4);
1280 return wantarray ? %res : \%res;
1283 ## ......................................................................
1284 ## parse to array of hashes functions
1286 sub git_get_refs_list {
1287 my $type = shift || "";
1292 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1294 while (my $line = <$fd>) {
1296 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1297 if (defined $refs{$1}) {
1298 push @{$refs{$1}}, $2;
1303 if (! $4) { # unpeeled, direct reference
1304 push @refs, { hash => $1, name => $3 }; # without type
1305 } elsif ($3 eq $refs[-1]{'name'}) {
1306 # most likely a tag is followed by its peeled
1307 # (deref) one, and when that happens we know the
1308 # previous one was of type 'tag'.
1309 $refs[-1]{'type'} = "tag";
1315 foreach my $ref (@refs) {
1316 my $ref_file = $ref->{'name'};
1317 my $ref_id = $ref->{'hash'};
1319 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1320 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1322 push @reflist, \%ref_item;
1325 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1326 return (\@reflist, \%refs);
1329 ## ----------------------------------------------------------------------
1330 ## filesystem-related functions
1332 sub get_file_owner {
1335 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1336 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1337 if (!defined $gcos) {
1341 $owner =~ s/[,;].*$//;
1342 return to_utf8($owner);
1345 ## ......................................................................
1346 ## mimetype related functions
1348 sub mimetype_guess_file {
1349 my $filename = shift;
1350 my $mimemap = shift;
1351 -r $mimemap or return undef;
1354 open(MIME, $mimemap) or return undef;
1356 next if m/^#/; # skip comments
1357 my ($mime, $exts) = split(/\t+/);
1358 if (defined $exts) {
1359 my @exts = split(/\s+/, $exts);
1360 foreach my $ext (@exts) {
1361 $mimemap{$ext} = $mime;
1367 $filename =~ /\.([^.]*)$/;
1368 return $mimemap{$1};
1371 sub mimetype_guess {
1372 my $filename = shift;
1374 $filename =~ /\./ or return undef;
1376 if ($mimetypes_file) {
1377 my $file = $mimetypes_file;
1378 if ($file !~ m!^/!) { # if it is relative path
1379 # it is relative to project
1380 $file = "$projectroot/$project/$file";
1382 $mime = mimetype_guess_file($filename, $file);
1384 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1390 my $filename = shift;
1393 my $mime = mimetype_guess($filename);
1394 $mime and return $mime;
1398 return $default_blob_plain_mimetype unless $fd;
1401 return 'text/plain' .
1402 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1403 } elsif (! $filename) {
1404 return 'application/octet-stream';
1405 } elsif ($filename =~ m/\.png$/i) {
1407 } elsif ($filename =~ m/\.gif$/i) {
1409 } elsif ($filename =~ m/\.jpe?g$/i) {
1410 return 'image/jpeg';
1412 return 'application/octet-stream';
1416 ## ======================================================================
1417 ## functions printing HTML: header, footer, error page
1419 sub git_header_html {
1420 my $status = shift || "200 OK";
1421 my $expires = shift;
1423 my $title = "$site_name git";
1424 if (defined $project) {
1425 $title .= " - $project";
1426 if (defined $action) {
1427 $title .= "/$action";
1428 if (defined $file_name) {
1429 $title .= " - " . esc_html($file_name);
1430 if ($action eq "tree" && $file_name !~ m|/$|) {
1437 # require explicit support from the UA if we are to send the page as
1438 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1439 # we have to do this because MSIE sometimes globs '*/*', pretending to
1440 # support xhtml+xml but choking when it gets what it asked for.
1441 if (defined $cgi->http('HTTP_ACCEPT') &&
1442 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1443 $cgi->Accept('application/xhtml+xml') != 0) {
1444 $content_type = 'application/xhtml+xml';
1446 $content_type = 'text/html';
1448 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1449 -status=> $status, -expires => $expires);
1451 <?xml version="1.0" encoding="utf-8"?>
1452 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1453 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1454 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1455 <!-- git core binaries version $git_version -->
1457 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1458 <meta name="generator" content="gitweb/$version git/$git_version"/>
1459 <meta name="robots" content="index, nofollow"/>
1460 <title>$title</title>
1462 # print out each stylesheet that exist
1463 if (defined $stylesheet) {
1464 #provides backwards capability for those people who define style sheet in a config file
1465 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1467 foreach my $stylesheet (@stylesheets) {
1468 next unless $stylesheet;
1469 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1472 if (defined $project) {
1473 printf('<link rel="alternate" title="%s log" '.
1474 'href="%s" type="application/rss+xml"/>'."\n",
1475 esc_param($project), href(action=>"rss"));
1477 printf('<link rel="alternate" title="%s projects list" '.
1478 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1479 $site_name, href(project=>undef, action=>"project_index"));
1480 printf('<link rel="alternate" title="%s projects logs" '.
1481 'href="%s" type="text/x-opml"/>'."\n",
1482 $site_name, href(project=>undef, action=>"opml"));
1484 if (defined $favicon) {
1485 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1491 if (-f $site_header) {
1492 open (my $fd, $site_header);
1497 print "<div class=\"page_header\">\n" .
1498 $cgi->a({-href => esc_url($logo_url),
1499 -title => $logo_label},
1500 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1501 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1502 if (defined $project) {
1503 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1504 if (defined $action) {
1508 if (!defined $searchtext) {
1512 if (defined $hash_base) {
1513 $search_hash = $hash_base;
1514 } elsif (defined $hash) {
1515 $search_hash = $hash;
1517 $search_hash = "HEAD";
1519 $cgi->param("a", "search");
1520 $cgi->param("h", $search_hash);
1521 $cgi->param("p", $project);
1522 print $cgi->startform(-method => "get", -action => $my_uri) .
1523 "<div class=\"search\">\n" .
1524 $cgi->hidden(-name => "p") . "\n" .
1525 $cgi->hidden(-name => "a") . "\n" .
1526 $cgi->hidden(-name => "h") . "\n" .
1527 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1529 $cgi->end_form() . "\n";
1534 sub git_footer_html {
1535 print "<div class=\"page_footer\">\n";
1536 if (defined $project) {
1537 my $descr = git_get_project_description($project);
1538 if (defined $descr) {
1539 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1541 print $cgi->a({-href => href(action=>"rss"),
1542 -class => "rss_logo"}, "RSS") . "\n";
1544 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1545 -class => "rss_logo"}, "OPML") . " ";
1546 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1547 -class => "rss_logo"}, "TXT") . "\n";
1551 if (-f $site_footer) {
1552 open (my $fd, $site_footer);
1562 my $status = shift || "403 Forbidden";
1563 my $error = shift || "Malformed query, file missing or permission denied";
1565 git_header_html($status);
1567 <div class="page_body">
1577 ## ----------------------------------------------------------------------
1578 ## functions printing or outputting HTML: navigation
1580 sub git_print_page_nav {
1581 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1582 $extra = '' if !defined $extra; # pager or formats
1584 my @navs = qw(summary shortlog log commit commitdiff tree);
1586 @navs = grep { $_ ne $suppress } @navs;
1589 my %arg = map { $_ => {action=>$_} } @navs;
1590 if (defined $head) {
1591 for (qw(commit commitdiff)) {
1592 $arg{$_}{hash} = $head;
1594 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1595 for (qw(shortlog log)) {
1596 $arg{$_}{hash} = $head;
1600 $arg{tree}{hash} = $treehead if defined $treehead;
1601 $arg{tree}{hash_base} = $treebase if defined $treebase;
1603 print "<div class=\"page_nav\">\n" .
1605 map { $_ eq $current ?
1606 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1608 print "<br/>\n$extra<br/>\n" .
1612 sub format_paging_nav {
1613 my ($action, $hash, $head, $page, $nrevs) = @_;
1617 if ($hash ne $head || $page) {
1618 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1620 $paging_nav .= "HEAD";
1624 $paging_nav .= " ⋅ " .
1625 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1626 -accesskey => "p", -title => "Alt-p"}, "prev");
1628 $paging_nav .= " ⋅ prev";
1631 if ($nrevs >= (100 * ($page+1)-1)) {
1632 $paging_nav .= " ⋅ " .
1633 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1634 -accesskey => "n", -title => "Alt-n"}, "next");
1636 $paging_nav .= " ⋅ next";
1642 ## ......................................................................
1643 ## functions printing or outputting HTML: div
1645 sub git_print_header_div {
1646 my ($action, $title, $hash, $hash_base) = @_;
1649 $args{action} = $action;
1650 $args{hash} = $hash if $hash;
1651 $args{hash_base} = $hash_base if $hash_base;
1653 print "<div class=\"header\">\n" .
1654 $cgi->a({-href => href(%args), -class => "title"},
1655 $title ? $title : $action) .
1659 #sub git_print_authorship (\%) {
1660 sub git_print_authorship {
1663 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1664 print "<div class=\"author_date\">" .
1665 esc_html($co->{'author_name'}) .
1667 if ($ad{'hour_local'} < 6) {
1668 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1669 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1671 printf(" (%02d:%02d %s)",
1672 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1677 sub git_print_page_path {
1683 print "<div class=\"page_path\">";
1684 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1685 -title => 'tree root'}, "[$project]");
1687 if (defined $name) {
1688 my @dirname = split '/', $name;
1689 my $basename = pop @dirname;
1692 foreach my $dir (@dirname) {
1693 $fullname .= ($fullname ? '/' : '') . $dir;
1694 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1696 -title => $fullname}, esc_html($dir));
1699 if (defined $type && $type eq 'blob') {
1700 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1702 -title => $name}, esc_html($basename));
1703 } elsif (defined $type && $type eq 'tree') {
1704 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1706 -title => $name}, esc_html($basename));
1709 print esc_html($basename);
1712 print "<br/></div>\n";
1715 # sub git_print_log (\@;%) {
1716 sub git_print_log ($;%) {
1720 if ($opts{'-remove_title'}) {
1721 # remove title, i.e. first line of log
1724 # remove leading empty lines
1725 while (defined $log->[0] && $log->[0] eq "") {
1732 foreach my $line (@$log) {
1733 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1736 if (! $opts{'-remove_signoff'}) {
1737 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1740 # remove signoff lines
1747 # print only one empty line
1748 # do not print empty line after signoff
1750 next if ($empty || $signoff);
1756 print format_log_line_html($line) . "<br/>\n";
1759 if ($opts{'-final_empty_line'}) {
1760 # end with single empty line
1761 print "<br/>\n" unless $empty;
1765 sub git_print_simplified_log {
1767 my $remove_title = shift;
1770 -final_empty_line=> 1,
1771 -remove_title => $remove_title);
1774 # print tree entry (row of git_tree), but without encompassing <tr> element
1775 sub git_print_tree_entry {
1776 my ($t, $basedir, $hash_base, $have_blame) = @_;
1779 $base_key{hash_base} = $hash_base if defined $hash_base;
1781 # The format of a table row is: mode list link. Where mode is
1782 # the mode of the entry, list is the name of the entry, an href,
1783 # and link is the action links of the entry.
1785 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1786 if ($t->{'type'} eq "blob") {
1787 print "<td class=\"list\">" .
1788 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1789 file_name=>"$basedir$t->{'name'}", %base_key),
1790 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1791 print "<td class=\"link\">";
1793 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1794 file_name=>"$basedir$t->{'name'}", %base_key)},
1797 if (defined $hash_base) {
1801 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1802 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1806 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1807 file_name=>"$basedir$t->{'name'}")},
1811 } elsif ($t->{'type'} eq "tree") {
1812 print "<td class=\"list\">";
1813 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1814 file_name=>"$basedir$t->{'name'}", %base_key)},
1815 esc_html($t->{'name'}));
1817 print "<td class=\"link\">";
1818 if (defined $hash_base) {
1819 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1820 file_name=>"$basedir$t->{'name'}")},
1827 ## ......................................................................
1828 ## functions printing large fragments of HTML
1830 sub git_difftree_body {
1831 my ($difftree, $hash, $parent) = @_;
1833 print "<div class=\"list_head\">\n";
1834 if ($#{$difftree} > 10) {
1835 print(($#{$difftree} + 1) . " files changed:\n");
1839 print "<table class=\"diff_tree\">\n";
1842 foreach my $line (@{$difftree}) {
1843 my %diff = parse_difftree_raw_line($line);
1846 print "<tr class=\"dark\">\n";
1848 print "<tr class=\"light\">\n";
1852 my ($to_mode_oct, $to_mode_str, $to_file_type);
1853 my ($from_mode_oct, $from_mode_str, $from_file_type);
1854 if ($diff{'to_mode'} ne ('0' x 6)) {
1855 $to_mode_oct = oct $diff{'to_mode'};
1856 if (S_ISREG($to_mode_oct)) { # only for regular file
1857 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1859 $to_file_type = file_type($diff{'to_mode'});
1861 if ($diff{'from_mode'} ne ('0' x 6)) {
1862 $from_mode_oct = oct $diff{'from_mode'};
1863 if (S_ISREG($to_mode_oct)) { # only for regular file
1864 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1866 $from_file_type = file_type($diff{'from_mode'});
1869 if ($diff{'status'} eq "A") { # created
1870 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1871 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1872 $mode_chng .= "]</span>";
1874 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1875 hash_base=>$hash, file_name=>$diff{'file'}),
1876 -class => "list"}, esc_html($diff{'file'}));
1878 print "<td>$mode_chng</td>\n";
1879 print "<td class=\"link\">";
1880 if ($action eq 'commitdiff') {
1883 print $cgi->a({-href => "#patch$patchno"}, "patch");
1887 } elsif ($diff{'status'} eq "D") { # deleted
1888 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1890 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1891 hash_base=>$parent, file_name=>$diff{'file'}),
1892 -class => "list"}, esc_html($diff{'file'}));
1894 print "<td>$mode_chng</td>\n";
1895 print "<td class=\"link\">";
1896 if ($action eq 'commitdiff') {
1899 print $cgi->a({-href => "#patch$patchno"}, "patch");
1902 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1903 file_name=>$diff{'file'})},
1905 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1906 file_name=>$diff{'file'})},
1910 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1911 my $mode_chnge = "";
1912 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1913 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1914 if ($from_file_type != $to_file_type) {
1915 $mode_chnge .= " from $from_file_type to $to_file_type";
1917 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1918 if ($from_mode_str && $to_mode_str) {
1919 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1920 } elsif ($to_mode_str) {
1921 $mode_chnge .= " mode: $to_mode_str";
1924 $mode_chnge .= "]</span>\n";
1927 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1928 hash_base=>$hash, file_name=>$diff{'file'}),
1929 -class => "list"}, esc_html($diff{'file'}));
1931 print "<td>$mode_chnge</td>\n";
1932 print "<td class=\"link\">";
1933 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1934 if ($action eq 'commitdiff') {
1937 print $cgi->a({-href => "#patch$patchno"}, "patch");
1939 print $cgi->a({-href => href(action=>"blobdiff",
1940 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1941 hash_base=>$hash, hash_parent_base=>$parent,
1942 file_name=>$diff{'file'})},
1947 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1948 file_name=>$diff{'file'})},
1950 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1951 file_name=>$diff{'file'})},
1955 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1956 my %status_name = ('R' => 'moved', 'C' => 'copied');
1957 my $nstatus = $status_name{$diff{'status'}};
1959 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1960 # mode also for directories, so we cannot use $to_mode_str
1961 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1964 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1965 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1966 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1967 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1968 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1969 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1970 -class => "list"}, esc_html($diff{'from_file'})) .
1971 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1972 "<td class=\"link\">";
1973 if ($diff{'to_id'} ne $diff{'from_id'}) {
1974 if ($action eq 'commitdiff') {
1977 print $cgi->a({-href => "#patch$patchno"}, "patch");
1979 print $cgi->a({-href => href(action=>"blobdiff",
1980 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1981 hash_base=>$hash, hash_parent_base=>$parent,
1982 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1987 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1988 file_name=>$diff{'from_file'})},
1990 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1991 file_name=>$diff{'from_file'})},
1995 } # we should not encounter Unmerged (U) or Unknown (X) status
2001 sub git_patchset_body {
2002 my ($fd, $difftree, $hash, $hash_parent) = @_;
2006 my $patch_found = 0;
2009 print "<div class=\"patchset\">\n";
2012 while (my $patch_line = <$fd>) {
2015 if ($patch_line =~ m/^diff /) { # "git diff" header
2016 # beginning of patch (in patchset)
2018 # close previous patch
2019 print "</div>\n"; # class="patch"
2021 # first patch in patchset
2024 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2026 if (ref($difftree->[$patch_idx]) eq "HASH") {
2027 $diffinfo = $difftree->[$patch_idx];
2029 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2033 # for now, no extended header, hence we skip empty patches
2034 # companion to next LINE if $in_header;
2035 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
2040 if ($diffinfo->{'status'} eq "A") { # added
2041 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
2042 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2043 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2044 $diffinfo->{'to_id'}) . " (new)" .
2045 "</div>\n"; # class="diff_info"
2047 } elsif ($diffinfo->{'status'} eq "D") { # deleted
2048 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
2049 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2050 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2051 $diffinfo->{'from_id'}) . " (deleted)" .
2052 "</div>\n"; # class="diff_info"
2054 } elsif ($diffinfo->{'status'} eq "R" || # renamed
2055 $diffinfo->{'status'} eq "C" || # copied
2056 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
2057 print "<div class=\"diff_info\">" .
2058 file_type($diffinfo->{'from_mode'}) . ":" .
2059 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2060 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
2061 $diffinfo->{'from_id'}) .
2063 file_type($diffinfo->{'to_mode'}) . ":" .
2064 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2065 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
2066 $diffinfo->{'to_id'});
2067 print "</div>\n"; # class="diff_info"
2069 } else { # modified, mode changed, ...
2070 print "<div class=\"diff_info\">" .
2071 file_type($diffinfo->{'from_mode'}) . ":" .
2072 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2073 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2074 $diffinfo->{'from_id'}) .
2076 file_type($diffinfo->{'to_mode'}) . ":" .
2077 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2078 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2079 $diffinfo->{'to_id'});
2080 print "</div>\n"; # class="diff_info"
2083 #print "<div class=\"diff extended_header\">\n";
2086 } # start of patch in patchset
2089 if ($in_header && $patch_line =~ m/^---/) {
2090 #print "</div>\n"; # class="diff extended_header"
2093 my $file = $diffinfo->{'from_file'};
2094 $file ||= $diffinfo->{'file'};
2095 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2096 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2097 -class => "list"}, esc_html($file));
2098 $patch_line =~ s|a/.*$|a/$file|g;
2099 print "<div class=\"diff from_file\">$patch_line</div>\n";
2101 $patch_line = <$fd>;
2104 #$patch_line =~ m/^+++/;
2105 $file = $diffinfo->{'to_file'};
2106 $file ||= $diffinfo->{'file'};
2107 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2108 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2109 -class => "list"}, esc_html($file));
2110 $patch_line =~ s|b/.*|b/$file|g;
2111 print "<div class=\"diff to_file\">$patch_line</div>\n";
2115 next LINE if $in_header;
2117 print format_diff_line($patch_line);
2119 print "</div>\n" if $patch_found; # class="patch"
2121 print "</div>\n"; # class="patchset"
2124 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2126 sub git_shortlog_body {
2127 # uses global variable $project
2128 my ($revlist, $from, $to, $refs, $extra) = @_;
2130 $from = 0 unless defined $from;
2131 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2133 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2135 for (my $i = $from; $i <= $to; $i++) {
2136 my $commit = $revlist->[$i];
2137 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2138 my $ref = format_ref_marker($refs, $commit);
2139 my %co = parse_commit($commit);
2141 print "<tr class=\"dark\">\n";
2143 print "<tr class=\"light\">\n";
2146 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2147 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2148 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2150 print format_subject_html($co{'title'}, $co{'title_short'},
2151 href(action=>"commit", hash=>$commit), $ref);
2153 "<td class=\"link\">" .
2154 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2155 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2156 if (gitweb_have_snapshot()) {
2157 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2162 if (defined $extra) {
2164 "<td colspan=\"4\">$extra</td>\n" .
2170 sub git_history_body {
2171 # Warning: assumes constant type (blob or tree) during history
2172 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2174 $from = 0 unless defined $from;
2175 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2177 print "<table class=\"history\" cellspacing=\"0\">\n";
2179 for (my $i = $from; $i <= $to; $i++) {
2180 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2185 my %co = parse_commit($commit);
2190 my $ref = format_ref_marker($refs, $commit);
2193 print "<tr class=\"dark\">\n";
2195 print "<tr class=\"light\">\n";
2198 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2199 # shortlog uses chop_str($co{'author_name'}, 10)
2200 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2202 # originally git_history used chop_str($co{'title'}, 50)
2203 print format_subject_html($co{'title'}, $co{'title_short'},
2204 href(action=>"commit", hash=>$commit), $ref);
2206 "<td class=\"link\">" .
2207 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2208 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2210 if ($ftype eq 'blob') {
2211 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2212 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2213 if (defined $blob_current && defined $blob_parent &&
2214 $blob_current ne $blob_parent) {
2216 $cgi->a({-href => href(action=>"blobdiff",
2217 hash=>$blob_current, hash_parent=>$blob_parent,
2218 hash_base=>$hash_base, hash_parent_base=>$commit,
2219 file_name=>$file_name)},
2226 if (defined $extra) {
2228 "<td colspan=\"4\">$extra</td>\n" .
2235 # uses global variable $project
2236 my ($taglist, $from, $to, $extra) = @_;
2237 $from = 0 unless defined $from;
2238 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2240 print "<table class=\"tags\" cellspacing=\"0\">\n";
2242 for (my $i = $from; $i <= $to; $i++) {
2243 my $entry = $taglist->[$i];
2245 my $comment_lines = $tag{'comment'};
2246 my $comment = shift @$comment_lines;
2248 if (defined $comment) {
2249 $comment_short = chop_str($comment, 30, 5);
2252 print "<tr class=\"dark\">\n";
2254 print "<tr class=\"light\">\n";
2257 print "<td><i>$tag{'age'}</i></td>\n" .
2259 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2260 -class => "list name"}, esc_html($tag{'name'})) .
2263 if (defined $comment) {
2264 print format_subject_html($comment, $comment_short,
2265 href(action=>"tag", hash=>$tag{'id'}));
2268 "<td class=\"selflink\">";
2269 if ($tag{'type'} eq "tag") {
2270 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2275 "<td class=\"link\">" . " | " .
2276 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2277 if ($tag{'reftype'} eq "commit") {
2278 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2279 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2280 } elsif ($tag{'reftype'} eq "blob") {
2281 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2286 if (defined $extra) {
2288 "<td colspan=\"5\">$extra</td>\n" .
2294 sub git_heads_body {
2295 # uses global variable $project
2296 my ($headlist, $head, $from, $to, $extra) = @_;
2297 $from = 0 unless defined $from;
2298 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2300 print "<table class=\"heads\" cellspacing=\"0\">\n";
2302 for (my $i = $from; $i <= $to; $i++) {
2303 my $entry = $headlist->[$i];
2305 my $curr = $tag{'id'} eq $head;
2307 print "<tr class=\"dark\">\n";
2309 print "<tr class=\"light\">\n";
2312 print "<td><i>$tag{'age'}</i></td>\n" .
2313 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2314 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2315 -class => "list name"},esc_html($tag{'name'})) .
2317 "<td class=\"link\">" .
2318 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2319 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2320 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2324 if (defined $extra) {
2326 "<td colspan=\"3\">$extra</td>\n" .
2332 ## ======================================================================
2333 ## ======================================================================
2336 sub git_project_list {
2337 my $order = $cgi->param('o');
2338 if (defined $order && $order !~ m/project|descr|owner|age/) {
2339 die_error(undef, "Unknown order parameter");
2342 my @list = git_get_projects_list();
2345 die_error(undef, "No projects found");
2347 foreach my $pr (@list) {
2348 my (@aa) = git_get_last_activity($pr->{'path'});
2352 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2353 if (!defined $pr->{'descr'}) {
2354 my $descr = git_get_project_description($pr->{'path'}) || "";
2355 $pr->{'descr'} = chop_str($descr, 25, 5);
2357 if (!defined $pr->{'owner'}) {
2358 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2360 push @projects, $pr;
2364 if (-f $home_text) {
2365 print "<div class=\"index_include\">\n";
2366 open (my $fd, $home_text);
2371 print "<table class=\"project_list\">\n" .
2373 $order ||= "project";
2374 if ($order eq "project") {
2375 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2376 print "<th>Project</th>\n";
2379 $cgi->a({-href => href(project=>undef, order=>'project'),
2380 -class => "header"}, "Project") .
2383 if ($order eq "descr") {
2384 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2385 print "<th>Description</th>\n";
2388 $cgi->a({-href => href(project=>undef, order=>'descr'),
2389 -class => "header"}, "Description") .
2392 if ($order eq "owner") {
2393 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2394 print "<th>Owner</th>\n";
2397 $cgi->a({-href => href(project=>undef, order=>'owner'),
2398 -class => "header"}, "Owner") .
2401 if ($order eq "age") {
2402 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2403 print "<th>Last Change</th>\n";
2406 $cgi->a({-href => href(project=>undef, order=>'age'),
2407 -class => "header"}, "Last Change") .
2410 print "<th></th>\n" .
2413 foreach my $pr (@projects) {
2415 print "<tr class=\"dark\">\n";
2417 print "<tr class=\"light\">\n";
2420 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2421 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2422 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2423 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2424 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2425 $pr->{'age_string'} . "</td>\n" .
2426 "<td class=\"link\">" .
2427 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2428 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2429 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2430 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2438 sub git_project_index {
2439 my @projects = git_get_projects_list();
2442 -type => 'text/plain',
2443 -charset => 'utf-8',
2444 -content_disposition => 'inline; filename="index.aux"');
2446 foreach my $pr (@projects) {
2447 if (!exists $pr->{'owner'}) {
2448 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2451 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2452 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2453 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2454 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2458 print "$path $owner\n";
2463 my $descr = git_get_project_description($project) || "none";
2464 my $head = git_get_head_hash($project);
2465 my %co = parse_commit($head);
2466 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2468 my $owner = git_get_project_owner($project);
2470 my ($reflist, $refs) = git_get_refs_list();
2474 foreach my $ref (@$reflist) {
2475 if ($ref->{'name'} =~ s!^heads/!!) {
2476 push @headlist, $ref;
2478 $ref->{'name'} =~ s!^tags/!!;
2479 push @taglist, $ref;
2484 git_print_page_nav('summary','', $head);
2486 print "<div class=\"title\"> </div>\n";
2487 print "<table cellspacing=\"0\">\n" .
2488 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2489 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2490 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2491 # use per project git URL list in $projectroot/$project/cloneurl
2492 # or make project git URL from git base URL and project name
2493 my $url_tag = "URL";
2494 my @url_list = git_get_project_url_list($project);
2495 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2496 foreach my $git_url (@url_list) {
2497 next unless $git_url;
2498 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2503 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2504 git_get_head_hash($project)
2505 or die_error(undef, "Open git-rev-list failed");
2506 my @revlist = map { chomp; $_ } <$fd>;
2508 git_print_header_div('shortlog');
2509 git_shortlog_body(\@revlist, 0, 15, $refs,
2510 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2513 git_print_header_div('tags');
2514 git_tags_body(\@taglist, 0, 15,
2515 $cgi->a({-href => href(action=>"tags")}, "..."));
2519 git_print_header_div('heads');
2520 git_heads_body(\@headlist, $head, 0, 15,
2521 $cgi->a({-href => href(action=>"heads")}, "..."));
2528 my $head = git_get_head_hash($project);
2530 git_print_page_nav('','', $head,undef,$head);
2531 my %tag = parse_tag($hash);
2532 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2533 print "<div class=\"title_text\">\n" .
2534 "<table cellspacing=\"0\">\n" .
2536 "<td>object</td>\n" .
2537 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2538 $tag{'object'}) . "</td>\n" .
2539 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2540 $tag{'type'}) . "</td>\n" .
2542 if (defined($tag{'author'})) {
2543 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2544 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2545 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2546 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2549 print "</table>\n\n" .
2551 print "<div class=\"page_body\">";
2552 my $comment = $tag{'comment'};
2553 foreach my $line (@$comment) {
2554 print esc_html($line) . "<br/>\n";
2564 my ($have_blame) = gitweb_check_feature('blame');
2566 die_error('403 Permission denied', "Permission denied");
2568 die_error('404 Not Found', "File name not defined") if (!$file_name);
2569 $hash_base ||= git_get_head_hash($project);
2570 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2571 my %co = parse_commit($hash_base)
2572 or die_error(undef, "Reading commit failed");
2573 if (!defined $hash) {
2574 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2575 or die_error(undef, "Error looking up file");
2577 $ftype = git_get_type($hash);
2578 if ($ftype !~ "blob") {
2579 die_error("400 Bad Request", "Object is not a blob");
2581 open ($fd, "-|", git_cmd(), "blame", '-l', '--', $file_name, $hash_base)
2582 or die_error(undef, "Open git-blame failed");
2585 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2588 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2591 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2593 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2594 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2595 git_print_page_path($file_name, $ftype, $hash_base);
2596 my @rev_color = (qw(light2 dark2));
2597 my $num_colors = scalar(@rev_color);
2598 my $current_color = 0;
2601 <div class="page_body">
2602 <table class="blame">
2603 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2606 my ($full_rev, $author, $date, $lineno, $data) =
2607 /^([0-9a-f]{40}).*?\s\((.*?)\s+([-\d]+ [:\d]+ [-+\d]+)\s+(\d+)\)\s(.*)/;
2608 my $rev = substr($full_rev, 0, 8);
2611 if (!defined $last_rev) {
2612 $last_rev = $full_rev;
2614 } elsif ($last_rev ne $full_rev) {
2615 $last_rev = $full_rev;
2616 $current_color = ++$current_color % $num_colors;
2619 print "<tr class=\"$rev_color[$current_color]\">\n";
2620 print "<td class=\"sha1\"";
2621 if ($print_c8 == 1) {
2622 print " title=\"$author, $date\"";
2625 if ($print_c8 == 1) {
2626 print $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2630 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2631 esc_html($lineno) . "</a></td>\n";
2632 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2638 or print "Reading blob failed\n";
2645 my ($have_blame) = gitweb_check_feature('blame');
2647 die_error('403 Permission denied', "Permission denied");
2649 die_error('404 Not Found', "File name not defined") if (!$file_name);
2650 $hash_base ||= git_get_head_hash($project);
2651 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2652 my %co = parse_commit($hash_base)
2653 or die_error(undef, "Reading commit failed");
2654 if (!defined $hash) {
2655 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2656 or die_error(undef, "Error lookup file");
2658 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2659 or die_error(undef, "Open git-annotate failed");
2662 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2665 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2668 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2670 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2671 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2672 git_print_page_path($file_name, 'blob', $hash_base);
2673 print "<div class=\"page_body\">\n";
2675 <table class="blame">
2684 my @line_class = (qw(light dark));
2685 my $line_class_len = scalar (@line_class);
2686 my $line_class_num = $#line_class;
2687 while (my $line = <$fd>) {
2699 $line_class_num = ($line_class_num + 1) % $line_class_len;
2701 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2708 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2711 $short_rev = substr ($long_rev, 0, 8);
2712 $age = time () - $time;
2713 $age_str = age_string ($age);
2714 $age_str =~ s/ / /g;
2715 $age_class = age_class($age);
2716 $author = esc_html ($author);
2717 $author =~ s/ / /g;
2719 $data = untabify($data);
2720 $data = esc_html ($data);
2723 <tr class="$line_class[$line_class_num]">
2724 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2725 <td class="$age_class">$age_str</td>
2727 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2728 <td class="pre">$data</td>
2731 } # while (my $line = <$fd>)
2732 print "</table>\n\n";
2734 or print "Reading blob failed.\n";
2740 my $head = git_get_head_hash($project);
2742 git_print_page_nav('','', $head,undef,$head);
2743 git_print_header_div('summary', $project);
2745 my ($taglist) = git_get_refs_list("tags");
2747 git_tags_body($taglist);
2753 my $head = git_get_head_hash($project);
2755 git_print_page_nav('','', $head,undef,$head);
2756 git_print_header_div('summary', $project);
2758 my ($headlist) = git_get_refs_list("heads");
2760 git_heads_body($headlist, $head);
2765 sub git_blob_plain {
2768 if (!defined $hash) {
2769 if (defined $file_name) {
2770 my $base = $hash_base || git_get_head_hash($project);
2771 $hash = git_get_hash_by_path($base, $file_name, "blob")
2772 or die_error(undef, "Error lookup file");
2774 die_error(undef, "No file name defined");
2776 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2777 # blobs defined by non-textual hash id's can be cached
2782 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2783 or die_error(undef, "Couldn't cat $file_name, $hash");
2785 $type ||= blob_mimetype($fd, $file_name);
2787 # save as filename, even when no $file_name is given
2788 my $save_as = "$hash";
2789 if (defined $file_name) {
2790 $save_as = $file_name;
2791 } elsif ($type =~ m/^text\//) {
2798 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2800 binmode STDOUT, ':raw';
2802 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2810 if (!defined $hash) {
2811 if (defined $file_name) {
2812 my $base = $hash_base || git_get_head_hash($project);
2813 $hash = git_get_hash_by_path($base, $file_name, "blob")
2814 or die_error(undef, "Error lookup file");
2816 die_error(undef, "No file name defined");
2818 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2819 # blobs defined by non-textual hash id's can be cached
2823 my ($have_blame) = gitweb_check_feature('blame');
2824 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2825 or die_error(undef, "Couldn't cat $file_name, $hash");
2826 my $mimetype = blob_mimetype($fd, $file_name);
2827 if ($mimetype !~ m/^text\//) {
2829 return git_blob_plain($mimetype);
2831 git_header_html(undef, $expires);
2832 my $formats_nav = '';
2833 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2834 if (defined $file_name) {
2837 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2838 hash=>$hash, file_name=>$file_name)},
2843 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2844 hash=>$hash, file_name=>$file_name)},
2847 $cgi->a({-href => href(action=>"blob_plain",
2848 hash=>$hash, file_name=>$file_name)},
2851 $cgi->a({-href => href(action=>"blob",
2852 hash_base=>"HEAD", file_name=>$file_name)},
2856 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2858 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2859 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2861 print "<div class=\"page_nav\">\n" .
2862 "<br/><br/></div>\n" .
2863 "<div class=\"title\">$hash</div>\n";
2865 git_print_page_path($file_name, "blob", $hash_base);
2866 print "<div class=\"page_body\">\n";
2868 while (my $line = <$fd>) {
2871 $line = untabify($line);
2872 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2873 $nr, $nr, $nr, esc_html($line);
2876 or print "Reading blob failed.\n";
2882 my $have_snapshot = gitweb_have_snapshot();
2884 if (!defined $hash_base) {
2885 $hash_base = "HEAD";
2887 if (!defined $hash) {
2888 if (defined $file_name) {
2889 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2895 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2896 or die_error(undef, "Open git-ls-tree failed");
2897 my @entries = map { chomp; $_ } <$fd>;
2898 close $fd or die_error(undef, "Reading tree failed");
2901 my $refs = git_get_references();
2902 my $ref = format_ref_marker($refs, $hash_base);
2905 my ($have_blame) = gitweb_check_feature('blame');
2906 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2908 if (defined $file_name) {
2910 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2911 hash=>$hash, file_name=>$file_name)},
2913 $cgi->a({-href => href(action=>"tree",
2914 hash_base=>"HEAD", file_name=>$file_name)},
2917 if ($have_snapshot) {
2918 # FIXME: Should be available when we have no hash base as well.
2920 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2923 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2924 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2927 print "<div class=\"page_nav\">\n";
2928 print "<br/><br/></div>\n";
2929 print "<div class=\"title\">$hash</div>\n";
2931 if (defined $file_name) {
2932 $basedir = $file_name;
2933 if ($basedir ne '' && substr($basedir, -1) ne '/') {
2937 git_print_page_path($file_name, 'tree', $hash_base);
2938 print "<div class=\"page_body\">\n";
2939 print "<table cellspacing=\"0\">\n";
2941 # '..' (top directory) link if possible
2942 if (defined $hash_base &&
2943 defined $file_name && $file_name =~ m![^/]+$!) {
2945 print "<tr class=\"dark\">\n";
2947 print "<tr class=\"light\">\n";
2951 my $up = $file_name;
2952 $up =~ s!/?[^/]+$!!;
2953 undef $up unless $up;
2954 # based on git_print_tree_entry
2955 print '<td class="mode">' . mode_str('040000') . "</td>\n";
2956 print '<td class="list">';
2957 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
2961 print "<td class=\"link\"></td>\n";
2965 foreach my $line (@entries) {
2966 my %t = parse_ls_tree_line($line, -z => 1);
2969 print "<tr class=\"dark\">\n";
2971 print "<tr class=\"light\">\n";
2975 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
2979 print "</table>\n" .
2985 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2986 my $have_snapshot = (defined $ctype && defined $suffix);
2987 if (!$have_snapshot) {
2988 die_error('403 Permission denied', "Permission denied");
2991 if (!defined $hash) {
2992 $hash = git_get_head_hash($project);
2995 my $filename = basename($project) . "-$hash.tar.$suffix";
2998 -type => 'application/x-tar',
2999 -content_encoding => $ctype,
3000 -content_disposition => 'inline; filename="' . "$filename" . '"',
3001 -status => '200 OK');
3003 my $git = git_cmd_str();
3004 my $name = $project;
3005 $name =~ s/\047/\047\\\047\047/g;
3007 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3008 or die_error(undef, "Execute git-tar-tree failed.");
3009 binmode STDOUT, ':raw';
3011 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3017 my $head = git_get_head_hash($project);
3018 if (!defined $hash) {
3021 if (!defined $page) {
3024 my $refs = git_get_references();
3026 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3027 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3028 or die_error(undef, "Open git-rev-list failed");
3029 my @revlist = map { chomp; $_ } <$fd>;
3032 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
3035 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3038 my %co = parse_commit($hash);
3040 git_print_header_div('summary', $project);
3041 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3043 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
3044 my $commit = $revlist[$i];
3045 my $ref = format_ref_marker($refs, $commit);
3046 my %co = parse_commit($commit);
3048 my %ad = parse_date($co{'author_epoch'});
3049 git_print_header_div('commit',
3050 "<span class=\"age\">$co{'age_string'}</span>" .
3051 esc_html($co{'title'}) . $ref,
3053 print "<div class=\"title_text\">\n" .
3054 "<div class=\"log_link\">\n" .
3055 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3057 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3059 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3062 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3065 print "<div class=\"log_body\">\n";
3066 git_print_simplified_log($co{'comment'});
3073 my %co = parse_commit($hash);
3075 die_error(undef, "Unknown commit object");
3077 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3078 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3080 my $parent = $co{'parent'};
3081 if (!defined $parent) {
3084 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
3085 or die_error(undef, "Open git-diff-tree failed");
3086 my @difftree = map { chomp; $_ } <$fd>;
3087 close $fd or die_error(undef, "Reading git-diff-tree failed");
3089 # non-textual hash id's can be cached
3091 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3094 my $refs = git_get_references();
3095 my $ref = format_ref_marker($refs, $co{'id'});
3097 my $have_snapshot = gitweb_have_snapshot();
3100 if (defined $file_name && defined $co{'parent'}) {
3102 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3105 git_header_html(undef, $expires);
3106 git_print_page_nav('commit', '',
3107 $hash, $co{'tree'}, $hash,
3108 join (' | ', @views_nav));
3110 if (defined $co{'parent'}) {
3111 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3113 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3115 print "<div class=\"title_text\">\n" .
3116 "<table cellspacing=\"0\">\n";
3117 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3119 "<td></td><td> $ad{'rfc2822'}";
3120 if ($ad{'hour_local'} < 6) {
3121 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3122 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3124 printf(" (%02d:%02d %s)",
3125 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3129 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3130 print "<tr><td></td><td> $cd{'rfc2822'}" .
3131 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3133 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3136 "<td class=\"sha1\">" .
3137 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3138 class => "list"}, $co{'tree'}) .
3140 "<td class=\"link\">" .
3141 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3143 if ($have_snapshot) {
3145 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3149 my $parents = $co{'parents'};
3150 foreach my $par (@$parents) {
3153 "<td class=\"sha1\">" .
3154 $cgi->a({-href => href(action=>"commit", hash=>$par),
3155 class => "list"}, $par) .
3157 "<td class=\"link\">" .
3158 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3160 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3167 print "<div class=\"page_body\">\n";
3168 git_print_log($co{'comment'});
3171 git_difftree_body(\@difftree, $hash, $parent);
3177 my $format = shift || 'html';
3184 # preparing $fd and %diffinfo for git_patchset_body
3186 if (defined $hash_base && defined $hash_parent_base) {
3187 if (defined $file_name) {
3189 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3191 or die_error(undef, "Open git-diff-tree failed");
3192 @difftree = map { chomp; $_ } <$fd>;
3194 or die_error(undef, "Reading git-diff-tree failed");
3196 or die_error('404 Not Found', "Blob diff not found");
3198 } elsif (defined $hash &&
3199 $hash =~ /[0-9a-fA-F]{40}/) {
3200 # try to find filename from $hash
3202 # read filtered raw output
3203 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3204 or die_error(undef, "Open git-diff-tree failed");
3206 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3208 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3209 map { chomp; $_ } <$fd>;
3211 or die_error(undef, "Reading git-diff-tree failed");
3213 or die_error('404 Not Found', "Blob diff not found");
3216 die_error('404 Not Found', "Missing one of the blob diff parameters");
3219 if (@difftree > 1) {
3220 die_error('404 Not Found', "Ambiguous blob diff specification");
3223 %diffinfo = parse_difftree_raw_line($difftree[0]);
3224 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3225 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3227 $hash_parent ||= $diffinfo{'from_id'};
3228 $hash ||= $diffinfo{'to_id'};
3230 # non-textual hash id's can be cached
3231 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3232 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3237 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3238 '-p', $hash_parent_base, $hash_base,
3240 or die_error(undef, "Open git-diff-tree failed");
3243 # old/legacy style URI
3244 if (!%diffinfo && # if new style URI failed
3245 defined $hash && defined $hash_parent) {
3246 # fake git-diff-tree raw output
3247 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3248 $diffinfo{'from_id'} = $hash_parent;
3249 $diffinfo{'to_id'} = $hash;
3250 if (defined $file_name) {
3251 if (defined $file_parent) {
3252 $diffinfo{'status'} = '2';
3253 $diffinfo{'from_file'} = $file_parent;
3254 $diffinfo{'to_file'} = $file_name;
3255 } else { # assume not renamed
3256 $diffinfo{'status'} = '1';
3257 $diffinfo{'from_file'} = $file_name;
3258 $diffinfo{'to_file'} = $file_name;
3260 } else { # no filename given
3261 $diffinfo{'status'} = '2';
3262 $diffinfo{'from_file'} = $hash_parent;
3263 $diffinfo{'to_file'} = $hash;
3266 # non-textual hash id's can be cached
3267 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3268 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3273 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3274 or die_error(undef, "Open git-diff failed");
3276 die_error('404 Not Found', "Missing one of the blob diff parameters")
3281 if ($format eq 'html') {
3283 $cgi->a({-href => href(action=>"blobdiff_plain",
3284 hash=>$hash, hash_parent=>$hash_parent,
3285 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3286 file_name=>$file_name, file_parent=>$file_parent)},
3288 git_header_html(undef, $expires);
3289 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3290 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3291 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3293 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3294 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3296 if (defined $file_name) {
3297 git_print_page_path($file_name, "blob", $hash_base);
3299 print "<div class=\"page_path\"></div>\n";
3302 } elsif ($format eq 'plain') {
3304 -type => 'text/plain',
3305 -charset => 'utf-8',
3306 -expires => $expires,
3307 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3309 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3312 die_error(undef, "Unknown blobdiff format");
3316 if ($format eq 'html') {
3317 print "<div class=\"page_body\">\n";
3319 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3322 print "</div>\n"; # class="page_body"
3326 while (my $line = <$fd>) {
3327 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3328 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3332 last if $line =~ m!^\+\+\+!;
3340 sub git_blobdiff_plain {
3341 git_blobdiff('plain');
3344 sub git_commitdiff {
3345 my $format = shift || 'html';
3346 my %co = parse_commit($hash);
3348 die_error(undef, "Unknown commit object");
3350 if (!defined $hash_parent) {
3351 $hash_parent = $co{'parent'} || '--root';
3357 if ($format eq 'html') {
3358 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3359 "--patch-with-raw", "--full-index", $hash_parent, $hash
3360 or die_error(undef, "Open git-diff-tree failed");
3362 while (chomp(my $line = <$fd>)) {
3363 # empty line ends raw part of diff-tree output
3365 push @difftree, $line;
3368 } elsif ($format eq 'plain') {
3369 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3370 '-p', $hash_parent, $hash
3371 or die_error(undef, "Open git-diff-tree failed");
3374 die_error(undef, "Unknown commitdiff format");
3377 # non-textual hash id's can be cached
3379 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3383 # write commit message
3384 if ($format eq 'html') {
3385 my $refs = git_get_references();
3386 my $ref = format_ref_marker($refs, $co{'id'});
3388 $cgi->a({-href => href(action=>"commitdiff_plain",
3389 hash=>$hash, hash_parent=>$hash_parent)},
3392 git_header_html(undef, $expires);
3393 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3394 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3395 git_print_authorship(\%co);
3396 print "<div class=\"page_body\">\n";
3397 print "<div class=\"log\">\n";
3398 git_print_simplified_log($co{'comment'}, 1); # skip title
3399 print "</div>\n"; # class="log"
3401 } elsif ($format eq 'plain') {
3402 my $refs = git_get_references("tags");
3403 my $tagname = git_get_rev_name_tags($hash);
3404 my $filename = basename($project) . "-$hash.patch";
3407 -type => 'text/plain',
3408 -charset => 'utf-8',
3409 -expires => $expires,
3410 -content_disposition => 'inline; filename="' . "$filename" . '"');
3411 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3414 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3415 Subject: $co{'title'}
3417 print "X-Git-Tag: $tagname\n" if $tagname;
3418 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3420 foreach my $line (@{$co{'comment'}}) {
3427 if ($format eq 'html') {
3428 git_difftree_body(\@difftree, $hash, $hash_parent);
3431 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3433 print "</div>\n"; # class="page_body"
3436 } elsif ($format eq 'plain') {
3440 or print "Reading git-diff-tree failed\n";
3444 sub git_commitdiff_plain {
3445 git_commitdiff('plain');
3449 if (!defined $hash_base) {
3450 $hash_base = git_get_head_hash($project);
3452 if (!defined $page) {
3456 my %co = parse_commit($hash_base);
3458 die_error(undef, "Unknown commit object");
3461 my $refs = git_get_references();
3462 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3464 if (!defined $hash && defined $file_name) {
3465 $hash = git_get_hash_by_path($hash_base, $file_name);
3467 if (defined $hash) {
3468 $ftype = git_get_type($hash);
3472 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3473 or die_error(undef, "Open git-rev-list-failed");
3474 my @revlist = map { chomp; $_ } <$fd>;
3476 or die_error(undef, "Reading git-rev-list failed");
3478 my $paging_nav = '';
3481 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3482 file_name=>$file_name)},
3484 $paging_nav .= " ⋅ " .
3485 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3486 file_name=>$file_name, page=>$page-1),
3487 -accesskey => "p", -title => "Alt-p"}, "prev");
3489 $paging_nav .= "first";
3490 $paging_nav .= " ⋅ prev";
3492 if ($#revlist >= (100 * ($page+1)-1)) {
3493 $paging_nav .= " ⋅ " .
3494 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3495 file_name=>$file_name, page=>$page+1),
3496 -accesskey => "n", -title => "Alt-n"}, "next");
3498 $paging_nav .= " ⋅ next";
3501 if ($#revlist >= (100 * ($page+1)-1)) {
3503 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3504 file_name=>$file_name, page=>$page+1),
3505 -title => "Alt-n"}, "next");
3509 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3510 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3511 git_print_page_path($file_name, $ftype, $hash_base);
3513 git_history_body(\@revlist, ($page * 100), $#revlist,
3514 $refs, $hash_base, $ftype, $next_link);
3520 if (!defined $searchtext) {
3521 die_error(undef, "Text field empty");
3523 if (!defined $hash) {
3524 $hash = git_get_head_hash($project);
3526 my %co = parse_commit($hash);
3528 die_error(undef, "Unknown commit object");
3531 my $commit_search = 1;
3532 my $author_search = 0;
3533 my $committer_search = 0;
3534 my $pickaxe_search = 0;
3535 if ($searchtext =~ s/^author\\://i) {
3537 } elsif ($searchtext =~ s/^committer\\://i) {
3538 $committer_search = 1;
3539 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3541 $pickaxe_search = 1;
3543 # pickaxe may take all resources of your box and run for several minutes
3544 # with every query - so decide by yourself how public you make this feature
3545 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3546 if (!$have_pickaxe) {
3547 die_error('403 Permission denied', "Permission denied");
3551 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3552 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3554 print "<table cellspacing=\"0\">\n";
3556 if ($commit_search) {
3558 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3559 while (my $commit_text = <$fd>) {
3560 if (!grep m/$searchtext/i, $commit_text) {
3563 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3566 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3569 my @commit_lines = split "\n", $commit_text;
3570 my %co = parse_commit(undef, \@commit_lines);
3575 print "<tr class=\"dark\">\n";
3577 print "<tr class=\"light\">\n";
3580 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3581 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3583 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3584 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3585 my $comment = $co{'comment'};
3586 foreach my $line (@$comment) {
3587 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3588 my $lead = esc_html($1) || "";
3589 $lead = chop_str($lead, 30, 10);
3590 my $match = esc_html($2) || "";
3591 my $trail = esc_html($3) || "";
3592 $trail = chop_str($trail, 30, 10);
3593 my $text = "$lead<span class=\"match\">$match</span>$trail";
3594 print chop_str($text, 80, 5) . "<br/>\n";
3598 "<td class=\"link\">" .
3599 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3601 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3608 if ($pickaxe_search) {
3610 my $git_command = git_cmd_str();
3611 open my $fd, "-|", "$git_command rev-list $hash | " .
3612 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3615 while (my $line = <$fd>) {
3616 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3619 $set{'from_id'} = $3;
3621 $set{'id'} = $set{'to_id'};
3622 if ($set{'id'} =~ m/0{40}/) {
3623 $set{'id'} = $set{'from_id'};
3625 if ($set{'id'} =~ m/0{40}/) {
3629 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3632 print "<tr class=\"dark\">\n";
3634 print "<tr class=\"light\">\n";
3637 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3638 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3640 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3641 -class => "list subject"},
3642 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3643 while (my $setref = shift @files) {
3645 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3646 hash=>$set{'id'}, file_name=>$set{'file'}),
3648 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3652 "<td class=\"link\">" .
3653 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3655 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3659 %co = parse_commit($1);
3669 my $head = git_get_head_hash($project);
3670 if (!defined $hash) {
3673 if (!defined $page) {
3676 my $refs = git_get_references();
3678 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3679 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3680 or die_error(undef, "Open git-rev-list failed");
3681 my @revlist = map { chomp; $_ } <$fd>;
3684 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3686 if ($#revlist >= (100 * ($page+1)-1)) {
3688 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3689 -title => "Alt-n"}, "next");
3694 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3695 git_print_header_div('summary', $project);
3697 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3702 ## ......................................................................
3703 ## feeds (RSS, OPML)
3706 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3707 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3708 or die_error(undef, "Open git-rev-list failed");
3709 my @revlist = map { chomp; $_ } <$fd>;
3710 close $fd or die_error(undef, "Reading git-rev-list failed");
3711 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3713 <?xml version="1.0" encoding="utf-8"?>
3714 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3716 <title>$project $my_uri $my_url</title>
3717 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3718 <description>$project log</description>
3719 <language>en</language>
3722 for (my $i = 0; $i <= $#revlist; $i++) {
3723 my $commit = $revlist[$i];
3724 my %co = parse_commit($commit);
3725 # we read 150, we always show 30 and the ones more recent than 48 hours
3726 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3729 my %cd = parse_date($co{'committer_epoch'});
3730 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3731 $co{'parent'}, $co{'id'}
3733 my @difftree = map { chomp; $_ } <$fd>;
3738 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3740 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3741 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3742 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3743 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3744 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3745 "<content:encoded>" .
3747 my $comment = $co{'comment'};
3748 foreach my $line (@$comment) {
3749 $line = to_utf8($line);
3750 print "$line<br/>\n";
3753 foreach my $line (@difftree) {
3754 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3757 my $file = esc_html(unquote($7));
3758 $file = to_utf8($file);
3759 print "$file<br/>\n";
3762 "</content:encoded>\n" .
3765 print "</channel></rss>";
3769 my @list = git_get_projects_list();
3771 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3773 <?xml version="1.0" encoding="utf-8"?>
3774 <opml version="1.0">
3776 <title>$site_name Git OPML Export</title>
3779 <outline text="git RSS feeds">
3782 foreach my $pr (@list) {
3784 my $head = git_get_head_hash($proj{'path'});
3785 if (!defined $head) {
3788 $git_dir = "$projectroot/$proj{'path'}";
3789 my %co = parse_commit($head);
3794 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3795 my $rss = "$my_url?p=$proj{'path'};a=rss";
3796 my $html = "$my_url?p=$proj{'path'};a=summary";
3797 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";