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;
1038 $date{'iso-tz'} = sprintf ("%04d-%02d-%02d %02d:%02d:%02d %s",
1039 1900+$year, $mon+1, $mday,
1040 $hour, $min, $sec, $tz);
1049 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1050 $tag{'id'} = $tag_id;
1051 while (my $line = <$fd>) {
1053 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1054 $tag{'object'} = $1;
1055 } elsif ($line =~ m/^type (.+)$/) {
1057 } elsif ($line =~ m/^tag (.+)$/) {
1059 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1060 $tag{'author'} = $1;
1063 } elsif ($line =~ m/--BEGIN/) {
1064 push @comment, $line;
1066 } elsif ($line eq "") {
1070 push @comment, <$fd>;
1071 $tag{'comment'} = \@comment;
1072 close $fd or return;
1073 if (!defined $tag{'name'}) {
1080 my $commit_id = shift;
1081 my $commit_text = shift;
1086 if (defined $commit_text) {
1087 @commit_lines = @$commit_text;
1090 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
1092 @commit_lines = split '\n', <$fd>;
1093 close $fd or return;
1096 my $header = shift @commit_lines;
1097 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1100 ($co{'id'}, my @parents) = split ' ', $header;
1101 $co{'parents'} = \@parents;
1102 $co{'parent'} = $parents[0];
1103 while (my $line = shift @commit_lines) {
1104 last if $line eq "\n";
1105 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1107 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1109 $co{'author_epoch'} = $2;
1110 $co{'author_tz'} = $3;
1111 if ($co{'author'} =~ m/^([^<]+) </) {
1112 $co{'author_name'} = $1;
1114 $co{'author_name'} = $co{'author'};
1116 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1117 $co{'committer'} = $1;
1118 $co{'committer_epoch'} = $2;
1119 $co{'committer_tz'} = $3;
1120 $co{'committer_name'} = $co{'committer'};
1121 $co{'committer_name'} =~ s/ <.*//;
1124 if (!defined $co{'tree'}) {
1128 foreach my $title (@commit_lines) {
1131 $co{'title'} = chop_str($title, 80, 5);
1132 # remove leading stuff of merges to make the interesting part visible
1133 if (length($title) > 50) {
1134 $title =~ s/^Automatic //;
1135 $title =~ s/^merge (of|with) /Merge ... /i;
1136 if (length($title) > 50) {
1137 $title =~ s/(http|rsync):\/\///;
1139 if (length($title) > 50) {
1140 $title =~ s/(master|www|rsync)\.//;
1142 if (length($title) > 50) {
1143 $title =~ s/kernel.org:?//;
1145 if (length($title) > 50) {
1146 $title =~ s/\/pub\/scm//;
1149 $co{'title_short'} = chop_str($title, 50, 5);
1153 if ($co{'title'} eq "") {
1154 $co{'title'} = $co{'title_short'} = '(no commit message)';
1156 # remove added spaces
1157 foreach my $line (@commit_lines) {
1160 $co{'comment'} = \@commit_lines;
1162 my $age = time - $co{'committer_epoch'};
1164 $co{'age_string'} = age_string($age);
1165 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1166 if ($age > 60*60*24*7*2) {
1167 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1168 $co{'age_string_age'} = $co{'age_string'};
1170 $co{'age_string_date'} = $co{'age_string'};
1171 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1176 # parse ref from ref_file, given by ref_id, with given type
1178 my $ref_file = shift;
1180 my $type = shift || git_get_type($ref_id);
1183 $ref_item{'type'} = $type;
1184 $ref_item{'id'} = $ref_id;
1185 $ref_item{'epoch'} = 0;
1186 $ref_item{'age'} = "unknown";
1187 if ($type eq "tag") {
1188 my %tag = parse_tag($ref_id);
1189 $ref_item{'comment'} = $tag{'comment'};
1190 if ($tag{'type'} eq "commit") {
1191 my %co = parse_commit($tag{'object'});
1192 $ref_item{'epoch'} = $co{'committer_epoch'};
1193 $ref_item{'age'} = $co{'age_string'};
1194 } elsif (defined($tag{'epoch'})) {
1195 my $age = time - $tag{'epoch'};
1196 $ref_item{'epoch'} = $tag{'epoch'};
1197 $ref_item{'age'} = age_string($age);
1199 $ref_item{'reftype'} = $tag{'type'};
1200 $ref_item{'name'} = $tag{'name'};
1201 $ref_item{'refid'} = $tag{'object'};
1202 } elsif ($type eq "commit"){
1203 my %co = parse_commit($ref_id);
1204 $ref_item{'reftype'} = "commit";
1205 $ref_item{'name'} = $ref_file;
1206 $ref_item{'title'} = $co{'title'};
1207 $ref_item{'refid'} = $ref_id;
1208 $ref_item{'epoch'} = $co{'committer_epoch'};
1209 $ref_item{'age'} = $co{'age_string'};
1211 $ref_item{'reftype'} = $type;
1212 $ref_item{'name'} = $ref_file;
1213 $ref_item{'refid'} = $ref_id;
1219 # parse line of git-diff-tree "raw" output
1220 sub parse_difftree_raw_line {
1224 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1225 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1226 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1227 $res{'from_mode'} = $1;
1228 $res{'to_mode'} = $2;
1229 $res{'from_id'} = $3;
1231 $res{'status'} = $5;
1232 $res{'similarity'} = $6;
1233 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1234 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1236 $res{'file'} = unquote($7);
1239 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1240 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1241 $res{'commit'} = $1;
1244 return wantarray ? %res : \%res;
1247 # parse line of git-ls-tree output
1248 sub parse_ls_tree_line ($;%) {
1253 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1254 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1262 $res{'name'} = unquote($4);
1265 return wantarray ? %res : \%res;
1268 ## ......................................................................
1269 ## parse to array of hashes functions
1271 sub git_get_refs_list {
1272 my $type = shift || "";
1277 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1279 while (my $line = <$fd>) {
1281 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1282 if (defined $refs{$1}) {
1283 push @{$refs{$1}}, $2;
1288 if (! $4) { # unpeeled, direct reference
1289 push @refs, { hash => $1, name => $3 }; # without type
1290 } elsif ($3 eq $refs[-1]{'name'}) {
1291 # most likely a tag is followed by its peeled
1292 # (deref) one, and when that happens we know the
1293 # previous one was of type 'tag'.
1294 $refs[-1]{'type'} = "tag";
1300 foreach my $ref (@refs) {
1301 my $ref_file = $ref->{'name'};
1302 my $ref_id = $ref->{'hash'};
1304 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1305 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1307 push @reflist, \%ref_item;
1310 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1311 return (\@reflist, \%refs);
1314 ## ----------------------------------------------------------------------
1315 ## filesystem-related functions
1317 sub get_file_owner {
1320 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1321 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1322 if (!defined $gcos) {
1326 $owner =~ s/[,;].*$//;
1327 return to_utf8($owner);
1330 ## ......................................................................
1331 ## mimetype related functions
1333 sub mimetype_guess_file {
1334 my $filename = shift;
1335 my $mimemap = shift;
1336 -r $mimemap or return undef;
1339 open(MIME, $mimemap) or return undef;
1341 next if m/^#/; # skip comments
1342 my ($mime, $exts) = split(/\t+/);
1343 if (defined $exts) {
1344 my @exts = split(/\s+/, $exts);
1345 foreach my $ext (@exts) {
1346 $mimemap{$ext} = $mime;
1352 $filename =~ /\.([^.]*)$/;
1353 return $mimemap{$1};
1356 sub mimetype_guess {
1357 my $filename = shift;
1359 $filename =~ /\./ or return undef;
1361 if ($mimetypes_file) {
1362 my $file = $mimetypes_file;
1363 if ($file !~ m!^/!) { # if it is relative path
1364 # it is relative to project
1365 $file = "$projectroot/$project/$file";
1367 $mime = mimetype_guess_file($filename, $file);
1369 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1375 my $filename = shift;
1378 my $mime = mimetype_guess($filename);
1379 $mime and return $mime;
1383 return $default_blob_plain_mimetype unless $fd;
1386 return 'text/plain' .
1387 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1388 } elsif (! $filename) {
1389 return 'application/octet-stream';
1390 } elsif ($filename =~ m/\.png$/i) {
1392 } elsif ($filename =~ m/\.gif$/i) {
1394 } elsif ($filename =~ m/\.jpe?g$/i) {
1395 return 'image/jpeg';
1397 return 'application/octet-stream';
1401 ## ======================================================================
1402 ## functions printing HTML: header, footer, error page
1404 sub git_header_html {
1405 my $status = shift || "200 OK";
1406 my $expires = shift;
1408 my $title = "$site_name git";
1409 if (defined $project) {
1410 $title .= " - $project";
1411 if (defined $action) {
1412 $title .= "/$action";
1413 if (defined $file_name) {
1414 $title .= " - " . esc_html($file_name);
1415 if ($action eq "tree" && $file_name !~ m|/$|) {
1422 # require explicit support from the UA if we are to send the page as
1423 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1424 # we have to do this because MSIE sometimes globs '*/*', pretending to
1425 # support xhtml+xml but choking when it gets what it asked for.
1426 if (defined $cgi->http('HTTP_ACCEPT') &&
1427 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1428 $cgi->Accept('application/xhtml+xml') != 0) {
1429 $content_type = 'application/xhtml+xml';
1431 $content_type = 'text/html';
1433 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1434 -status=> $status, -expires => $expires);
1436 <?xml version="1.0" encoding="utf-8"?>
1437 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1438 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1439 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1440 <!-- git core binaries version $git_version -->
1442 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1443 <meta name="generator" content="gitweb/$version git/$git_version"/>
1444 <meta name="robots" content="index, nofollow"/>
1445 <title>$title</title>
1447 # print out each stylesheet that exist
1448 if (defined $stylesheet) {
1449 #provides backwards capability for those people who define style sheet in a config file
1450 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1452 foreach my $stylesheet (@stylesheets) {
1453 next unless $stylesheet;
1454 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1457 if (defined $project) {
1458 printf('<link rel="alternate" title="%s log" '.
1459 'href="%s" type="application/rss+xml"/>'."\n",
1460 esc_param($project), href(action=>"rss"));
1462 printf('<link rel="alternate" title="%s projects list" '.
1463 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1464 $site_name, href(project=>undef, action=>"project_index"));
1465 printf('<link rel="alternate" title="%s projects logs" '.
1466 'href="%s" type="text/x-opml"/>'."\n",
1467 $site_name, href(project=>undef, action=>"opml"));
1469 if (defined $favicon) {
1470 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1476 if (-f $site_header) {
1477 open (my $fd, $site_header);
1482 print "<div class=\"page_header\">\n" .
1483 $cgi->a({-href => esc_url($logo_url),
1484 -title => $logo_label},
1485 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1486 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1487 if (defined $project) {
1488 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1489 if (defined $action) {
1493 if (!defined $searchtext) {
1497 if (defined $hash_base) {
1498 $search_hash = $hash_base;
1499 } elsif (defined $hash) {
1500 $search_hash = $hash;
1502 $search_hash = "HEAD";
1504 $cgi->param("a", "search");
1505 $cgi->param("h", $search_hash);
1506 $cgi->param("p", $project);
1507 print $cgi->startform(-method => "get", -action => $my_uri) .
1508 "<div class=\"search\">\n" .
1509 $cgi->hidden(-name => "p") . "\n" .
1510 $cgi->hidden(-name => "a") . "\n" .
1511 $cgi->hidden(-name => "h") . "\n" .
1512 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1514 $cgi->end_form() . "\n";
1519 sub git_footer_html {
1520 print "<div class=\"page_footer\">\n";
1521 if (defined $project) {
1522 my $descr = git_get_project_description($project);
1523 if (defined $descr) {
1524 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1526 print $cgi->a({-href => href(action=>"rss"),
1527 -class => "rss_logo"}, "RSS") . "\n";
1529 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1530 -class => "rss_logo"}, "OPML") . " ";
1531 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1532 -class => "rss_logo"}, "TXT") . "\n";
1536 if (-f $site_footer) {
1537 open (my $fd, $site_footer);
1547 my $status = shift || "403 Forbidden";
1548 my $error = shift || "Malformed query, file missing or permission denied";
1550 git_header_html($status);
1552 <div class="page_body">
1562 ## ----------------------------------------------------------------------
1563 ## functions printing or outputting HTML: navigation
1565 sub git_print_page_nav {
1566 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1567 $extra = '' if !defined $extra; # pager or formats
1569 my @navs = qw(summary shortlog log commit commitdiff tree);
1571 @navs = grep { $_ ne $suppress } @navs;
1574 my %arg = map { $_ => {action=>$_} } @navs;
1575 if (defined $head) {
1576 for (qw(commit commitdiff)) {
1577 $arg{$_}{hash} = $head;
1579 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1580 for (qw(shortlog log)) {
1581 $arg{$_}{hash} = $head;
1585 $arg{tree}{hash} = $treehead if defined $treehead;
1586 $arg{tree}{hash_base} = $treebase if defined $treebase;
1588 print "<div class=\"page_nav\">\n" .
1590 map { $_ eq $current ?
1591 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1593 print "<br/>\n$extra<br/>\n" .
1597 sub format_paging_nav {
1598 my ($action, $hash, $head, $page, $nrevs) = @_;
1602 if ($hash ne $head || $page) {
1603 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1605 $paging_nav .= "HEAD";
1609 $paging_nav .= " ⋅ " .
1610 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1611 -accesskey => "p", -title => "Alt-p"}, "prev");
1613 $paging_nav .= " ⋅ prev";
1616 if ($nrevs >= (100 * ($page+1)-1)) {
1617 $paging_nav .= " ⋅ " .
1618 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1619 -accesskey => "n", -title => "Alt-n"}, "next");
1621 $paging_nav .= " ⋅ next";
1627 ## ......................................................................
1628 ## functions printing or outputting HTML: div
1630 sub git_print_header_div {
1631 my ($action, $title, $hash, $hash_base) = @_;
1634 $args{action} = $action;
1635 $args{hash} = $hash if $hash;
1636 $args{hash_base} = $hash_base if $hash_base;
1638 print "<div class=\"header\">\n" .
1639 $cgi->a({-href => href(%args), -class => "title"},
1640 $title ? $title : $action) .
1644 #sub git_print_authorship (\%) {
1645 sub git_print_authorship {
1648 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1649 print "<div class=\"author_date\">" .
1650 esc_html($co->{'author_name'}) .
1652 if ($ad{'hour_local'} < 6) {
1653 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1654 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1656 printf(" (%02d:%02d %s)",
1657 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1662 sub git_print_page_path {
1668 print "<div class=\"page_path\">";
1669 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1670 -title => 'tree root'}, "[$project]");
1672 if (defined $name) {
1673 my @dirname = split '/', $name;
1674 my $basename = pop @dirname;
1677 foreach my $dir (@dirname) {
1678 $fullname .= ($fullname ? '/' : '') . $dir;
1679 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1681 -title => $fullname}, esc_html($dir));
1684 if (defined $type && $type eq 'blob') {
1685 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1687 -title => $name}, esc_html($basename));
1688 } elsif (defined $type && $type eq 'tree') {
1689 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1691 -title => $name}, esc_html($basename));
1694 print esc_html($basename);
1697 print "<br/></div>\n";
1700 # sub git_print_log (\@;%) {
1701 sub git_print_log ($;%) {
1705 if ($opts{'-remove_title'}) {
1706 # remove title, i.e. first line of log
1709 # remove leading empty lines
1710 while (defined $log->[0] && $log->[0] eq "") {
1717 foreach my $line (@$log) {
1718 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1721 if (! $opts{'-remove_signoff'}) {
1722 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1725 # remove signoff lines
1732 # print only one empty line
1733 # do not print empty line after signoff
1735 next if ($empty || $signoff);
1741 print format_log_line_html($line) . "<br/>\n";
1744 if ($opts{'-final_empty_line'}) {
1745 # end with single empty line
1746 print "<br/>\n" unless $empty;
1750 sub git_print_simplified_log {
1752 my $remove_title = shift;
1755 -final_empty_line=> 1,
1756 -remove_title => $remove_title);
1759 # print tree entry (row of git_tree), but without encompassing <tr> element
1760 sub git_print_tree_entry {
1761 my ($t, $basedir, $hash_base, $have_blame) = @_;
1764 $base_key{hash_base} = $hash_base if defined $hash_base;
1766 # The format of a table row is: mode list link. Where mode is
1767 # the mode of the entry, list is the name of the entry, an href,
1768 # and link is the action links of the entry.
1770 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1771 if ($t->{'type'} eq "blob") {
1772 print "<td class=\"list\">" .
1773 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1774 file_name=>"$basedir$t->{'name'}", %base_key),
1775 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1776 print "<td class=\"link\">";
1778 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1779 file_name=>"$basedir$t->{'name'}", %base_key)},
1782 if (defined $hash_base) {
1786 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1787 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1791 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1792 file_name=>"$basedir$t->{'name'}")},
1796 } elsif ($t->{'type'} eq "tree") {
1797 print "<td class=\"list\">";
1798 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1799 file_name=>"$basedir$t->{'name'}", %base_key)},
1800 esc_html($t->{'name'}));
1802 print "<td class=\"link\">";
1803 if (defined $hash_base) {
1804 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1805 file_name=>"$basedir$t->{'name'}")},
1812 ## ......................................................................
1813 ## functions printing large fragments of HTML
1815 sub git_difftree_body {
1816 my ($difftree, $hash, $parent) = @_;
1818 print "<div class=\"list_head\">\n";
1819 if ($#{$difftree} > 10) {
1820 print(($#{$difftree} + 1) . " files changed:\n");
1824 print "<table class=\"diff_tree\">\n";
1827 foreach my $line (@{$difftree}) {
1828 my %diff = parse_difftree_raw_line($line);
1831 print "<tr class=\"dark\">\n";
1833 print "<tr class=\"light\">\n";
1837 my ($to_mode_oct, $to_mode_str, $to_file_type);
1838 my ($from_mode_oct, $from_mode_str, $from_file_type);
1839 if ($diff{'to_mode'} ne ('0' x 6)) {
1840 $to_mode_oct = oct $diff{'to_mode'};
1841 if (S_ISREG($to_mode_oct)) { # only for regular file
1842 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1844 $to_file_type = file_type($diff{'to_mode'});
1846 if ($diff{'from_mode'} ne ('0' x 6)) {
1847 $from_mode_oct = oct $diff{'from_mode'};
1848 if (S_ISREG($to_mode_oct)) { # only for regular file
1849 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1851 $from_file_type = file_type($diff{'from_mode'});
1854 if ($diff{'status'} eq "A") { # created
1855 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1856 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1857 $mode_chng .= "]</span>";
1859 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1860 hash_base=>$hash, file_name=>$diff{'file'}),
1861 -class => "list"}, esc_html($diff{'file'}));
1863 print "<td>$mode_chng</td>\n";
1864 print "<td class=\"link\">";
1865 if ($action eq 'commitdiff') {
1868 print $cgi->a({-href => "#patch$patchno"}, "patch");
1872 } elsif ($diff{'status'} eq "D") { # deleted
1873 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1875 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1876 hash_base=>$parent, file_name=>$diff{'file'}),
1877 -class => "list"}, esc_html($diff{'file'}));
1879 print "<td>$mode_chng</td>\n";
1880 print "<td class=\"link\">";
1881 if ($action eq 'commitdiff') {
1884 print $cgi->a({-href => "#patch$patchno"}, "patch");
1887 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1888 file_name=>$diff{'file'})},
1890 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1891 file_name=>$diff{'file'})},
1895 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1896 my $mode_chnge = "";
1897 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1898 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1899 if ($from_file_type != $to_file_type) {
1900 $mode_chnge .= " from $from_file_type to $to_file_type";
1902 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1903 if ($from_mode_str && $to_mode_str) {
1904 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1905 } elsif ($to_mode_str) {
1906 $mode_chnge .= " mode: $to_mode_str";
1909 $mode_chnge .= "]</span>\n";
1912 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1913 hash_base=>$hash, file_name=>$diff{'file'}),
1914 -class => "list"}, esc_html($diff{'file'}));
1916 print "<td>$mode_chnge</td>\n";
1917 print "<td class=\"link\">";
1918 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1919 if ($action eq 'commitdiff') {
1922 print $cgi->a({-href => "#patch$patchno"}, "patch");
1924 print $cgi->a({-href => href(action=>"blobdiff",
1925 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1926 hash_base=>$hash, hash_parent_base=>$parent,
1927 file_name=>$diff{'file'})},
1932 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1933 file_name=>$diff{'file'})},
1935 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1936 file_name=>$diff{'file'})},
1940 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1941 my %status_name = ('R' => 'moved', 'C' => 'copied');
1942 my $nstatus = $status_name{$diff{'status'}};
1944 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1945 # mode also for directories, so we cannot use $to_mode_str
1946 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1949 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1950 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1951 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1952 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1953 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1954 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1955 -class => "list"}, esc_html($diff{'from_file'})) .
1956 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1957 "<td class=\"link\">";
1958 if ($diff{'to_id'} ne $diff{'from_id'}) {
1959 if ($action eq 'commitdiff') {
1962 print $cgi->a({-href => "#patch$patchno"}, "patch");
1964 print $cgi->a({-href => href(action=>"blobdiff",
1965 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1966 hash_base=>$hash, hash_parent_base=>$parent,
1967 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1972 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1973 file_name=>$diff{'from_file'})},
1975 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1976 file_name=>$diff{'from_file'})},
1980 } # we should not encounter Unmerged (U) or Unknown (X) status
1986 sub git_patchset_body {
1987 my ($fd, $difftree, $hash, $hash_parent) = @_;
1991 my $patch_found = 0;
1994 print "<div class=\"patchset\">\n";
1997 while (my $patch_line = <$fd>) {
2000 if ($patch_line =~ m/^diff /) { # "git diff" header
2001 # beginning of patch (in patchset)
2003 # close previous patch
2004 print "</div>\n"; # class="patch"
2006 # first patch in patchset
2009 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2011 if (ref($difftree->[$patch_idx]) eq "HASH") {
2012 $diffinfo = $difftree->[$patch_idx];
2014 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2018 # for now, no extended header, hence we skip empty patches
2019 # companion to next LINE if $in_header;
2020 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
2025 if ($diffinfo->{'status'} eq "A") { # added
2026 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
2027 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2028 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2029 $diffinfo->{'to_id'}) . " (new)" .
2030 "</div>\n"; # class="diff_info"
2032 } elsif ($diffinfo->{'status'} eq "D") { # deleted
2033 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
2034 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2035 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2036 $diffinfo->{'from_id'}) . " (deleted)" .
2037 "</div>\n"; # class="diff_info"
2039 } elsif ($diffinfo->{'status'} eq "R" || # renamed
2040 $diffinfo->{'status'} eq "C" || # copied
2041 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
2042 print "<div class=\"diff_info\">" .
2043 file_type($diffinfo->{'from_mode'}) . ":" .
2044 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2045 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
2046 $diffinfo->{'from_id'}) .
2048 file_type($diffinfo->{'to_mode'}) . ":" .
2049 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2050 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
2051 $diffinfo->{'to_id'});
2052 print "</div>\n"; # class="diff_info"
2054 } else { # modified, mode changed, ...
2055 print "<div class=\"diff_info\">" .
2056 file_type($diffinfo->{'from_mode'}) . ":" .
2057 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2058 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2059 $diffinfo->{'from_id'}) .
2061 file_type($diffinfo->{'to_mode'}) . ":" .
2062 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2063 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2064 $diffinfo->{'to_id'});
2065 print "</div>\n"; # class="diff_info"
2068 #print "<div class=\"diff extended_header\">\n";
2071 } # start of patch in patchset
2074 if ($in_header && $patch_line =~ m/^---/) {
2075 #print "</div>\n"; # class="diff extended_header"
2078 my $file = $diffinfo->{'from_file'};
2079 $file ||= $diffinfo->{'file'};
2080 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2081 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2082 -class => "list"}, esc_html($file));
2083 $patch_line =~ s|a/.*$|a/$file|g;
2084 print "<div class=\"diff from_file\">$patch_line</div>\n";
2086 $patch_line = <$fd>;
2089 #$patch_line =~ m/^+++/;
2090 $file = $diffinfo->{'to_file'};
2091 $file ||= $diffinfo->{'file'};
2092 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2093 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2094 -class => "list"}, esc_html($file));
2095 $patch_line =~ s|b/.*|b/$file|g;
2096 print "<div class=\"diff to_file\">$patch_line</div>\n";
2100 next LINE if $in_header;
2102 print format_diff_line($patch_line);
2104 print "</div>\n" if $patch_found; # class="patch"
2106 print "</div>\n"; # class="patchset"
2109 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2111 sub git_shortlog_body {
2112 # uses global variable $project
2113 my ($revlist, $from, $to, $refs, $extra) = @_;
2115 $from = 0 unless defined $from;
2116 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2118 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2120 for (my $i = $from; $i <= $to; $i++) {
2121 my $commit = $revlist->[$i];
2122 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2123 my $ref = format_ref_marker($refs, $commit);
2124 my %co = parse_commit($commit);
2126 print "<tr class=\"dark\">\n";
2128 print "<tr class=\"light\">\n";
2131 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2132 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2133 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2135 print format_subject_html($co{'title'}, $co{'title_short'},
2136 href(action=>"commit", hash=>$commit), $ref);
2138 "<td class=\"link\">" .
2139 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2140 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2141 if (gitweb_have_snapshot()) {
2142 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2147 if (defined $extra) {
2149 "<td colspan=\"4\">$extra</td>\n" .
2155 sub git_history_body {
2156 # Warning: assumes constant type (blob or tree) during history
2157 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2159 $from = 0 unless defined $from;
2160 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2162 print "<table class=\"history\" cellspacing=\"0\">\n";
2164 for (my $i = $from; $i <= $to; $i++) {
2165 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2170 my %co = parse_commit($commit);
2175 my $ref = format_ref_marker($refs, $commit);
2178 print "<tr class=\"dark\">\n";
2180 print "<tr class=\"light\">\n";
2183 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2184 # shortlog uses chop_str($co{'author_name'}, 10)
2185 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2187 # originally git_history used chop_str($co{'title'}, 50)
2188 print format_subject_html($co{'title'}, $co{'title_short'},
2189 href(action=>"commit", hash=>$commit), $ref);
2191 "<td class=\"link\">" .
2192 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2193 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2195 if ($ftype eq 'blob') {
2196 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2197 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2198 if (defined $blob_current && defined $blob_parent &&
2199 $blob_current ne $blob_parent) {
2201 $cgi->a({-href => href(action=>"blobdiff",
2202 hash=>$blob_current, hash_parent=>$blob_parent,
2203 hash_base=>$hash_base, hash_parent_base=>$commit,
2204 file_name=>$file_name)},
2211 if (defined $extra) {
2213 "<td colspan=\"4\">$extra</td>\n" .
2220 # uses global variable $project
2221 my ($taglist, $from, $to, $extra) = @_;
2222 $from = 0 unless defined $from;
2223 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2225 print "<table class=\"tags\" cellspacing=\"0\">\n";
2227 for (my $i = $from; $i <= $to; $i++) {
2228 my $entry = $taglist->[$i];
2230 my $comment_lines = $tag{'comment'};
2231 my $comment = shift @$comment_lines;
2233 if (defined $comment) {
2234 $comment_short = chop_str($comment, 30, 5);
2237 print "<tr class=\"dark\">\n";
2239 print "<tr class=\"light\">\n";
2242 print "<td><i>$tag{'age'}</i></td>\n" .
2244 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2245 -class => "list name"}, esc_html($tag{'name'})) .
2248 if (defined $comment) {
2249 print format_subject_html($comment, $comment_short,
2250 href(action=>"tag", hash=>$tag{'id'}));
2253 "<td class=\"selflink\">";
2254 if ($tag{'type'} eq "tag") {
2255 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2260 "<td class=\"link\">" . " | " .
2261 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2262 if ($tag{'reftype'} eq "commit") {
2263 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2264 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2265 } elsif ($tag{'reftype'} eq "blob") {
2266 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2271 if (defined $extra) {
2273 "<td colspan=\"5\">$extra</td>\n" .
2279 sub git_heads_body {
2280 # uses global variable $project
2281 my ($headlist, $head, $from, $to, $extra) = @_;
2282 $from = 0 unless defined $from;
2283 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2285 print "<table class=\"heads\" cellspacing=\"0\">\n";
2287 for (my $i = $from; $i <= $to; $i++) {
2288 my $entry = $headlist->[$i];
2290 my $curr = $tag{'id'} eq $head;
2292 print "<tr class=\"dark\">\n";
2294 print "<tr class=\"light\">\n";
2297 print "<td><i>$tag{'age'}</i></td>\n" .
2298 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2299 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2300 -class => "list name"},esc_html($tag{'name'})) .
2302 "<td class=\"link\">" .
2303 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2304 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2305 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2309 if (defined $extra) {
2311 "<td colspan=\"3\">$extra</td>\n" .
2317 ## ======================================================================
2318 ## ======================================================================
2321 sub git_project_list {
2322 my $order = $cgi->param('o');
2323 if (defined $order && $order !~ m/project|descr|owner|age/) {
2324 die_error(undef, "Unknown order parameter");
2327 my @list = git_get_projects_list();
2330 die_error(undef, "No projects found");
2332 foreach my $pr (@list) {
2333 my $head = git_get_head_hash($pr->{'path'});
2334 if (!defined $head) {
2337 $git_dir = "$projectroot/$pr->{'path'}";
2338 my %co = parse_commit($head);
2342 $pr->{'commit'} = \%co;
2343 if (!defined $pr->{'descr'}) {
2344 my $descr = git_get_project_description($pr->{'path'}) || "";
2345 $pr->{'descr'} = chop_str($descr, 25, 5);
2347 if (!defined $pr->{'owner'}) {
2348 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2350 push @projects, $pr;
2354 if (-f $home_text) {
2355 print "<div class=\"index_include\">\n";
2356 open (my $fd, $home_text);
2361 print "<table class=\"project_list\">\n" .
2363 $order ||= "project";
2364 if ($order eq "project") {
2365 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2366 print "<th>Project</th>\n";
2369 $cgi->a({-href => href(project=>undef, order=>'project'),
2370 -class => "header"}, "Project") .
2373 if ($order eq "descr") {
2374 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2375 print "<th>Description</th>\n";
2378 $cgi->a({-href => href(project=>undef, order=>'descr'),
2379 -class => "header"}, "Description") .
2382 if ($order eq "owner") {
2383 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2384 print "<th>Owner</th>\n";
2387 $cgi->a({-href => href(project=>undef, order=>'owner'),
2388 -class => "header"}, "Owner") .
2391 if ($order eq "age") {
2392 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2393 print "<th>Last Change</th>\n";
2396 $cgi->a({-href => href(project=>undef, order=>'age'),
2397 -class => "header"}, "Last Change") .
2400 print "<th></th>\n" .
2403 foreach my $pr (@projects) {
2405 print "<tr class=\"dark\">\n";
2407 print "<tr class=\"light\">\n";
2410 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2411 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2412 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2413 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2414 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2415 $pr->{'commit'}{'age_string'} . "</td>\n" .
2416 "<td class=\"link\">" .
2417 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2418 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2419 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2420 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2428 sub git_project_index {
2429 my @projects = git_get_projects_list();
2432 -type => 'text/plain',
2433 -charset => 'utf-8',
2434 -content_disposition => 'inline; filename="index.aux"');
2436 foreach my $pr (@projects) {
2437 if (!exists $pr->{'owner'}) {
2438 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2441 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2442 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2443 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2444 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2448 print "$path $owner\n";
2453 my $descr = git_get_project_description($project) || "none";
2454 my $head = git_get_head_hash($project);
2455 my %co = parse_commit($head);
2456 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2458 my $owner = git_get_project_owner($project);
2460 my ($reflist, $refs) = git_get_refs_list();
2464 foreach my $ref (@$reflist) {
2465 if ($ref->{'name'} =~ s!^heads/!!) {
2466 push @headlist, $ref;
2468 $ref->{'name'} =~ s!^tags/!!;
2469 push @taglist, $ref;
2474 git_print_page_nav('summary','', $head);
2476 print "<div class=\"title\"> </div>\n";
2477 print "<table cellspacing=\"0\">\n" .
2478 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2479 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2480 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2481 # use per project git URL list in $projectroot/$project/cloneurl
2482 # or make project git URL from git base URL and project name
2483 my $url_tag = "URL";
2484 my @url_list = git_get_project_url_list($project);
2485 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2486 foreach my $git_url (@url_list) {
2487 next unless $git_url;
2488 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2493 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2494 git_get_head_hash($project)
2495 or die_error(undef, "Open git-rev-list failed");
2496 my @revlist = map { chomp; $_ } <$fd>;
2498 git_print_header_div('shortlog');
2499 git_shortlog_body(\@revlist, 0, 15, $refs,
2500 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2503 git_print_header_div('tags');
2504 git_tags_body(\@taglist, 0, 15,
2505 $cgi->a({-href => href(action=>"tags")}, "..."));
2509 git_print_header_div('heads');
2510 git_heads_body(\@headlist, $head, 0, 15,
2511 $cgi->a({-href => href(action=>"heads")}, "..."));
2518 my $head = git_get_head_hash($project);
2520 git_print_page_nav('','', $head,undef,$head);
2521 my %tag = parse_tag($hash);
2522 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2523 print "<div class=\"title_text\">\n" .
2524 "<table cellspacing=\"0\">\n" .
2526 "<td>object</td>\n" .
2527 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2528 $tag{'object'}) . "</td>\n" .
2529 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2530 $tag{'type'}) . "</td>\n" .
2532 if (defined($tag{'author'})) {
2533 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2534 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2535 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2536 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2539 print "</table>\n\n" .
2541 print "<div class=\"page_body\">";
2542 my $comment = $tag{'comment'};
2543 foreach my $line (@$comment) {
2544 print esc_html($line) . "<br/>\n";
2554 my ($have_blame) = gitweb_check_feature('blame');
2556 die_error('403 Permission denied', "Permission denied");
2558 die_error('404 Not Found', "File name not defined") if (!$file_name);
2559 $hash_base ||= git_get_head_hash($project);
2560 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2561 my %co = parse_commit($hash_base)
2562 or die_error(undef, "Reading commit failed");
2563 if (!defined $hash) {
2564 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2565 or die_error(undef, "Error looking up file");
2567 $ftype = git_get_type($hash);
2568 if ($ftype !~ "blob") {
2569 die_error("400 Bad Request", "Object is not a blob");
2571 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
2572 $file_name, $hash_base)
2573 or die_error(undef, "Open git-blame failed");
2576 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2579 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2582 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2584 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2585 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2586 git_print_page_path($file_name, $ftype, $hash_base);
2587 my @rev_color = (qw(light2 dark2));
2588 my $num_colors = scalar(@rev_color);
2589 my $current_color = 0;
2592 <div class="page_body">
2593 <table class="blame">
2594 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2599 last unless defined $_;
2600 my ($full_rev, $orig_lineno, $lineno, $group_size) =
2601 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
2602 if (!exists $metainfo{$full_rev}) {
2603 $metainfo{$full_rev} = {};
2605 my $meta = $metainfo{$full_rev};
2608 if (/^(\S+) (.*)$/) {
2613 my $rev = substr($full_rev, 0, 8);
2614 my $author = $meta->{'author'};
2615 my %date = parse_date($meta->{'author-time'},
2616 $meta->{'author-tz'});
2617 my $date = $date{'iso-tz'};
2619 $current_color = ++$current_color % $num_colors;
2621 print "<tr class=\"$rev_color[$current_color]\">\n";
2623 print "<td class=\"sha1\"";
2624 print " title=\"$author, $date\"";
2625 print " rowspan=\"$group_size\"" if ($group_size > 1);
2627 print $cgi->a({-href => href(action=>"commit",
2629 file_name=>$file_name)},
2633 my $blamed = href(action => 'blame',
2634 file_name => $meta->{'filename'},
2635 hash_base => $full_rev);
2636 print "<td class=\"linenr\">";
2637 print $cgi->a({ -href => "$blamed#l$orig_lineno",
2639 -class => "linenr" },
2642 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2648 or print "Reading blob failed\n";
2655 my ($have_blame) = gitweb_check_feature('blame');
2657 die_error('403 Permission denied', "Permission denied");
2659 die_error('404 Not Found', "File name not defined") if (!$file_name);
2660 $hash_base ||= git_get_head_hash($project);
2661 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2662 my %co = parse_commit($hash_base)
2663 or die_error(undef, "Reading commit failed");
2664 if (!defined $hash) {
2665 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2666 or die_error(undef, "Error lookup file");
2668 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2669 or die_error(undef, "Open git-annotate failed");
2672 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2675 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2678 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2680 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2681 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2682 git_print_page_path($file_name, 'blob', $hash_base);
2683 print "<div class=\"page_body\">\n";
2685 <table class="blame">
2694 my @line_class = (qw(light dark));
2695 my $line_class_len = scalar (@line_class);
2696 my $line_class_num = $#line_class;
2697 while (my $line = <$fd>) {
2709 $line_class_num = ($line_class_num + 1) % $line_class_len;
2711 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2718 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2721 $short_rev = substr ($long_rev, 0, 8);
2722 $age = time () - $time;
2723 $age_str = age_string ($age);
2724 $age_str =~ s/ / /g;
2725 $age_class = age_class($age);
2726 $author = esc_html ($author);
2727 $author =~ s/ / /g;
2729 $data = untabify($data);
2730 $data = esc_html ($data);
2733 <tr class="$line_class[$line_class_num]">
2734 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2735 <td class="$age_class">$age_str</td>
2737 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2738 <td class="pre">$data</td>
2741 } # while (my $line = <$fd>)
2742 print "</table>\n\n";
2744 or print "Reading blob failed.\n";
2750 my $head = git_get_head_hash($project);
2752 git_print_page_nav('','', $head,undef,$head);
2753 git_print_header_div('summary', $project);
2755 my ($taglist) = git_get_refs_list("tags");
2757 git_tags_body($taglist);
2763 my $head = git_get_head_hash($project);
2765 git_print_page_nav('','', $head,undef,$head);
2766 git_print_header_div('summary', $project);
2768 my ($headlist) = git_get_refs_list("heads");
2770 git_heads_body($headlist, $head);
2775 sub git_blob_plain {
2778 if (!defined $hash) {
2779 if (defined $file_name) {
2780 my $base = $hash_base || git_get_head_hash($project);
2781 $hash = git_get_hash_by_path($base, $file_name, "blob")
2782 or die_error(undef, "Error lookup file");
2784 die_error(undef, "No file name defined");
2786 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2787 # blobs defined by non-textual hash id's can be cached
2792 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2793 or die_error(undef, "Couldn't cat $file_name, $hash");
2795 $type ||= blob_mimetype($fd, $file_name);
2797 # save as filename, even when no $file_name is given
2798 my $save_as = "$hash";
2799 if (defined $file_name) {
2800 $save_as = $file_name;
2801 } elsif ($type =~ m/^text\//) {
2808 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2810 binmode STDOUT, ':raw';
2812 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2820 if (!defined $hash) {
2821 if (defined $file_name) {
2822 my $base = $hash_base || git_get_head_hash($project);
2823 $hash = git_get_hash_by_path($base, $file_name, "blob")
2824 or die_error(undef, "Error lookup file");
2826 die_error(undef, "No file name defined");
2828 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2829 # blobs defined by non-textual hash id's can be cached
2833 my ($have_blame) = gitweb_check_feature('blame');
2834 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2835 or die_error(undef, "Couldn't cat $file_name, $hash");
2836 my $mimetype = blob_mimetype($fd, $file_name);
2837 if ($mimetype !~ m/^text\//) {
2839 return git_blob_plain($mimetype);
2841 git_header_html(undef, $expires);
2842 my $formats_nav = '';
2843 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2844 if (defined $file_name) {
2847 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2848 hash=>$hash, file_name=>$file_name)},
2853 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2854 hash=>$hash, file_name=>$file_name)},
2857 $cgi->a({-href => href(action=>"blob_plain",
2858 hash=>$hash, file_name=>$file_name)},
2861 $cgi->a({-href => href(action=>"blob",
2862 hash_base=>"HEAD", file_name=>$file_name)},
2866 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2868 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2869 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2871 print "<div class=\"page_nav\">\n" .
2872 "<br/><br/></div>\n" .
2873 "<div class=\"title\">$hash</div>\n";
2875 git_print_page_path($file_name, "blob", $hash_base);
2876 print "<div class=\"page_body\">\n";
2878 while (my $line = <$fd>) {
2881 $line = untabify($line);
2882 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2883 $nr, $nr, $nr, esc_html($line);
2886 or print "Reading blob failed.\n";
2892 my $have_snapshot = gitweb_have_snapshot();
2894 if (!defined $hash_base) {
2895 $hash_base = "HEAD";
2897 if (!defined $hash) {
2898 if (defined $file_name) {
2899 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2905 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2906 or die_error(undef, "Open git-ls-tree failed");
2907 my @entries = map { chomp; $_ } <$fd>;
2908 close $fd or die_error(undef, "Reading tree failed");
2911 my $refs = git_get_references();
2912 my $ref = format_ref_marker($refs, $hash_base);
2915 my ($have_blame) = gitweb_check_feature('blame');
2916 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2918 if (defined $file_name) {
2920 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2921 hash=>$hash, file_name=>$file_name)},
2923 $cgi->a({-href => href(action=>"tree",
2924 hash_base=>"HEAD", file_name=>$file_name)},
2927 if ($have_snapshot) {
2928 # FIXME: Should be available when we have no hash base as well.
2930 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2933 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2934 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2937 print "<div class=\"page_nav\">\n";
2938 print "<br/><br/></div>\n";
2939 print "<div class=\"title\">$hash</div>\n";
2941 if (defined $file_name) {
2942 $basedir = $file_name;
2943 if ($basedir ne '' && substr($basedir, -1) ne '/') {
2947 git_print_page_path($file_name, 'tree', $hash_base);
2948 print "<div class=\"page_body\">\n";
2949 print "<table cellspacing=\"0\">\n";
2951 # '..' (top directory) link if possible
2952 if (defined $hash_base &&
2953 defined $file_name && $file_name =~ m![^/]+$!) {
2955 print "<tr class=\"dark\">\n";
2957 print "<tr class=\"light\">\n";
2961 my $up = $file_name;
2962 $up =~ s!/?[^/]+$!!;
2963 undef $up unless $up;
2964 # based on git_print_tree_entry
2965 print '<td class="mode">' . mode_str('040000') . "</td>\n";
2966 print '<td class="list">';
2967 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
2971 print "<td class=\"link\"></td>\n";
2975 foreach my $line (@entries) {
2976 my %t = parse_ls_tree_line($line, -z => 1);
2979 print "<tr class=\"dark\">\n";
2981 print "<tr class=\"light\">\n";
2985 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
2989 print "</table>\n" .
2995 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2996 my $have_snapshot = (defined $ctype && defined $suffix);
2997 if (!$have_snapshot) {
2998 die_error('403 Permission denied', "Permission denied");
3001 if (!defined $hash) {
3002 $hash = git_get_head_hash($project);
3005 my $filename = basename($project) . "-$hash.tar.$suffix";
3008 -type => 'application/x-tar',
3009 -content_encoding => $ctype,
3010 -content_disposition => 'inline; filename="' . "$filename" . '"',
3011 -status => '200 OK');
3013 my $git = git_cmd_str();
3014 my $name = $project;
3015 $name =~ s/\047/\047\\\047\047/g;
3017 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3018 or die_error(undef, "Execute git-tar-tree failed.");
3019 binmode STDOUT, ':raw';
3021 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3027 my $head = git_get_head_hash($project);
3028 if (!defined $hash) {
3031 if (!defined $page) {
3034 my $refs = git_get_references();
3036 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3037 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3038 or die_error(undef, "Open git-rev-list failed");
3039 my @revlist = map { chomp; $_ } <$fd>;
3042 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
3045 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3048 my %co = parse_commit($hash);
3050 git_print_header_div('summary', $project);
3051 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3053 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
3054 my $commit = $revlist[$i];
3055 my $ref = format_ref_marker($refs, $commit);
3056 my %co = parse_commit($commit);
3058 my %ad = parse_date($co{'author_epoch'});
3059 git_print_header_div('commit',
3060 "<span class=\"age\">$co{'age_string'}</span>" .
3061 esc_html($co{'title'}) . $ref,
3063 print "<div class=\"title_text\">\n" .
3064 "<div class=\"log_link\">\n" .
3065 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3067 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3069 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3072 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3075 print "<div class=\"log_body\">\n";
3076 git_print_simplified_log($co{'comment'});
3083 my %co = parse_commit($hash);
3085 die_error(undef, "Unknown commit object");
3087 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3088 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3090 my $parent = $co{'parent'};
3091 if (!defined $parent) {
3094 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
3095 or die_error(undef, "Open git-diff-tree failed");
3096 my @difftree = map { chomp; $_ } <$fd>;
3097 close $fd or die_error(undef, "Reading git-diff-tree failed");
3099 # non-textual hash id's can be cached
3101 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3104 my $refs = git_get_references();
3105 my $ref = format_ref_marker($refs, $co{'id'});
3107 my $have_snapshot = gitweb_have_snapshot();
3110 if (defined $file_name && defined $co{'parent'}) {
3112 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3115 git_header_html(undef, $expires);
3116 git_print_page_nav('commit', '',
3117 $hash, $co{'tree'}, $hash,
3118 join (' | ', @views_nav));
3120 if (defined $co{'parent'}) {
3121 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3123 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3125 print "<div class=\"title_text\">\n" .
3126 "<table cellspacing=\"0\">\n";
3127 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3129 "<td></td><td> $ad{'rfc2822'}";
3130 if ($ad{'hour_local'} < 6) {
3131 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3132 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3134 printf(" (%02d:%02d %s)",
3135 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3139 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3140 print "<tr><td></td><td> $cd{'rfc2822'}" .
3141 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3143 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3146 "<td class=\"sha1\">" .
3147 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3148 class => "list"}, $co{'tree'}) .
3150 "<td class=\"link\">" .
3151 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3153 if ($have_snapshot) {
3155 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3159 my $parents = $co{'parents'};
3160 foreach my $par (@$parents) {
3163 "<td class=\"sha1\">" .
3164 $cgi->a({-href => href(action=>"commit", hash=>$par),
3165 class => "list"}, $par) .
3167 "<td class=\"link\">" .
3168 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3170 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3177 print "<div class=\"page_body\">\n";
3178 git_print_log($co{'comment'});
3181 git_difftree_body(\@difftree, $hash, $parent);
3187 my $format = shift || 'html';
3194 # preparing $fd and %diffinfo for git_patchset_body
3196 if (defined $hash_base && defined $hash_parent_base) {
3197 if (defined $file_name) {
3199 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3201 or die_error(undef, "Open git-diff-tree failed");
3202 @difftree = map { chomp; $_ } <$fd>;
3204 or die_error(undef, "Reading git-diff-tree failed");
3206 or die_error('404 Not Found', "Blob diff not found");
3208 } elsif (defined $hash &&
3209 $hash =~ /[0-9a-fA-F]{40}/) {
3210 # try to find filename from $hash
3212 # read filtered raw output
3213 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3214 or die_error(undef, "Open git-diff-tree failed");
3216 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3218 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3219 map { chomp; $_ } <$fd>;
3221 or die_error(undef, "Reading git-diff-tree failed");
3223 or die_error('404 Not Found', "Blob diff not found");
3226 die_error('404 Not Found', "Missing one of the blob diff parameters");
3229 if (@difftree > 1) {
3230 die_error('404 Not Found', "Ambiguous blob diff specification");
3233 %diffinfo = parse_difftree_raw_line($difftree[0]);
3234 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3235 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3237 $hash_parent ||= $diffinfo{'from_id'};
3238 $hash ||= $diffinfo{'to_id'};
3240 # non-textual hash id's can be cached
3241 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3242 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3247 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3248 '-p', $hash_parent_base, $hash_base,
3250 or die_error(undef, "Open git-diff-tree failed");
3253 # old/legacy style URI
3254 if (!%diffinfo && # if new style URI failed
3255 defined $hash && defined $hash_parent) {
3256 # fake git-diff-tree raw output
3257 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3258 $diffinfo{'from_id'} = $hash_parent;
3259 $diffinfo{'to_id'} = $hash;
3260 if (defined $file_name) {
3261 if (defined $file_parent) {
3262 $diffinfo{'status'} = '2';
3263 $diffinfo{'from_file'} = $file_parent;
3264 $diffinfo{'to_file'} = $file_name;
3265 } else { # assume not renamed
3266 $diffinfo{'status'} = '1';
3267 $diffinfo{'from_file'} = $file_name;
3268 $diffinfo{'to_file'} = $file_name;
3270 } else { # no filename given
3271 $diffinfo{'status'} = '2';
3272 $diffinfo{'from_file'} = $hash_parent;
3273 $diffinfo{'to_file'} = $hash;
3276 # non-textual hash id's can be cached
3277 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3278 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3283 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3284 or die_error(undef, "Open git-diff failed");
3286 die_error('404 Not Found', "Missing one of the blob diff parameters")
3291 if ($format eq 'html') {
3293 $cgi->a({-href => href(action=>"blobdiff_plain",
3294 hash=>$hash, hash_parent=>$hash_parent,
3295 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3296 file_name=>$file_name, file_parent=>$file_parent)},
3298 git_header_html(undef, $expires);
3299 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3300 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3301 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3303 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3304 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3306 if (defined $file_name) {
3307 git_print_page_path($file_name, "blob", $hash_base);
3309 print "<div class=\"page_path\"></div>\n";
3312 } elsif ($format eq 'plain') {
3314 -type => 'text/plain',
3315 -charset => 'utf-8',
3316 -expires => $expires,
3317 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3319 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3322 die_error(undef, "Unknown blobdiff format");
3326 if ($format eq 'html') {
3327 print "<div class=\"page_body\">\n";
3329 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3332 print "</div>\n"; # class="page_body"
3336 while (my $line = <$fd>) {
3337 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3338 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3342 last if $line =~ m!^\+\+\+!;
3350 sub git_blobdiff_plain {
3351 git_blobdiff('plain');
3354 sub git_commitdiff {
3355 my $format = shift || 'html';
3356 my %co = parse_commit($hash);
3358 die_error(undef, "Unknown commit object");
3360 if (!defined $hash_parent) {
3361 $hash_parent = $co{'parent'} || '--root';
3367 if ($format eq 'html') {
3368 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3369 "--patch-with-raw", "--full-index", $hash_parent, $hash
3370 or die_error(undef, "Open git-diff-tree failed");
3372 while (chomp(my $line = <$fd>)) {
3373 # empty line ends raw part of diff-tree output
3375 push @difftree, $line;
3378 } elsif ($format eq 'plain') {
3379 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3380 '-p', $hash_parent, $hash
3381 or die_error(undef, "Open git-diff-tree failed");
3384 die_error(undef, "Unknown commitdiff format");
3387 # non-textual hash id's can be cached
3389 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3393 # write commit message
3394 if ($format eq 'html') {
3395 my $refs = git_get_references();
3396 my $ref = format_ref_marker($refs, $co{'id'});
3398 $cgi->a({-href => href(action=>"commitdiff_plain",
3399 hash=>$hash, hash_parent=>$hash_parent)},
3402 git_header_html(undef, $expires);
3403 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3404 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3405 git_print_authorship(\%co);
3406 print "<div class=\"page_body\">\n";
3407 print "<div class=\"log\">\n";
3408 git_print_simplified_log($co{'comment'}, 1); # skip title
3409 print "</div>\n"; # class="log"
3411 } elsif ($format eq 'plain') {
3412 my $refs = git_get_references("tags");
3413 my $tagname = git_get_rev_name_tags($hash);
3414 my $filename = basename($project) . "-$hash.patch";
3417 -type => 'text/plain',
3418 -charset => 'utf-8',
3419 -expires => $expires,
3420 -content_disposition => 'inline; filename="' . "$filename" . '"');
3421 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3424 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3425 Subject: $co{'title'}
3427 print "X-Git-Tag: $tagname\n" if $tagname;
3428 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3430 foreach my $line (@{$co{'comment'}}) {
3437 if ($format eq 'html') {
3438 git_difftree_body(\@difftree, $hash, $hash_parent);
3441 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3443 print "</div>\n"; # class="page_body"
3446 } elsif ($format eq 'plain') {
3450 or print "Reading git-diff-tree failed\n";
3454 sub git_commitdiff_plain {
3455 git_commitdiff('plain');
3459 if (!defined $hash_base) {
3460 $hash_base = git_get_head_hash($project);
3462 if (!defined $page) {
3466 my %co = parse_commit($hash_base);
3468 die_error(undef, "Unknown commit object");
3471 my $refs = git_get_references();
3472 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3474 if (!defined $hash && defined $file_name) {
3475 $hash = git_get_hash_by_path($hash_base, $file_name);
3477 if (defined $hash) {
3478 $ftype = git_get_type($hash);
3482 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3483 or die_error(undef, "Open git-rev-list-failed");
3484 my @revlist = map { chomp; $_ } <$fd>;
3486 or die_error(undef, "Reading git-rev-list failed");
3488 my $paging_nav = '';
3491 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3492 file_name=>$file_name)},
3494 $paging_nav .= " ⋅ " .
3495 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3496 file_name=>$file_name, page=>$page-1),
3497 -accesskey => "p", -title => "Alt-p"}, "prev");
3499 $paging_nav .= "first";
3500 $paging_nav .= " ⋅ prev";
3502 if ($#revlist >= (100 * ($page+1)-1)) {
3503 $paging_nav .= " ⋅ " .
3504 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3505 file_name=>$file_name, page=>$page+1),
3506 -accesskey => "n", -title => "Alt-n"}, "next");
3508 $paging_nav .= " ⋅ next";
3511 if ($#revlist >= (100 * ($page+1)-1)) {
3513 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3514 file_name=>$file_name, page=>$page+1),
3515 -title => "Alt-n"}, "next");
3519 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3520 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3521 git_print_page_path($file_name, $ftype, $hash_base);
3523 git_history_body(\@revlist, ($page * 100), $#revlist,
3524 $refs, $hash_base, $ftype, $next_link);
3530 if (!defined $searchtext) {
3531 die_error(undef, "Text field empty");
3533 if (!defined $hash) {
3534 $hash = git_get_head_hash($project);
3536 my %co = parse_commit($hash);
3538 die_error(undef, "Unknown commit object");
3541 my $commit_search = 1;
3542 my $author_search = 0;
3543 my $committer_search = 0;
3544 my $pickaxe_search = 0;
3545 if ($searchtext =~ s/^author\\://i) {
3547 } elsif ($searchtext =~ s/^committer\\://i) {
3548 $committer_search = 1;
3549 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3551 $pickaxe_search = 1;
3553 # pickaxe may take all resources of your box and run for several minutes
3554 # with every query - so decide by yourself how public you make this feature
3555 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3556 if (!$have_pickaxe) {
3557 die_error('403 Permission denied', "Permission denied");
3561 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3562 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3564 print "<table cellspacing=\"0\">\n";
3566 if ($commit_search) {
3568 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3569 while (my $commit_text = <$fd>) {
3570 if (!grep m/$searchtext/i, $commit_text) {
3573 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3576 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3579 my @commit_lines = split "\n", $commit_text;
3580 my %co = parse_commit(undef, \@commit_lines);
3585 print "<tr class=\"dark\">\n";
3587 print "<tr class=\"light\">\n";
3590 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3591 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3593 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3594 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3595 my $comment = $co{'comment'};
3596 foreach my $line (@$comment) {
3597 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3598 my $lead = esc_html($1) || "";
3599 $lead = chop_str($lead, 30, 10);
3600 my $match = esc_html($2) || "";
3601 my $trail = esc_html($3) || "";
3602 $trail = chop_str($trail, 30, 10);
3603 my $text = "$lead<span class=\"match\">$match</span>$trail";
3604 print chop_str($text, 80, 5) . "<br/>\n";
3608 "<td class=\"link\">" .
3609 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3611 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3618 if ($pickaxe_search) {
3620 my $git_command = git_cmd_str();
3621 open my $fd, "-|", "$git_command rev-list $hash | " .
3622 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3625 while (my $line = <$fd>) {
3626 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3629 $set{'from_id'} = $3;
3631 $set{'id'} = $set{'to_id'};
3632 if ($set{'id'} =~ m/0{40}/) {
3633 $set{'id'} = $set{'from_id'};
3635 if ($set{'id'} =~ m/0{40}/) {
3639 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3642 print "<tr class=\"dark\">\n";
3644 print "<tr class=\"light\">\n";
3647 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3648 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3650 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3651 -class => "list subject"},
3652 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3653 while (my $setref = shift @files) {
3655 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3656 hash=>$set{'id'}, file_name=>$set{'file'}),
3658 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3662 "<td class=\"link\">" .
3663 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3665 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3669 %co = parse_commit($1);
3679 my $head = git_get_head_hash($project);
3680 if (!defined $hash) {
3683 if (!defined $page) {
3686 my $refs = git_get_references();
3688 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3689 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3690 or die_error(undef, "Open git-rev-list failed");
3691 my @revlist = map { chomp; $_ } <$fd>;
3694 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3696 if ($#revlist >= (100 * ($page+1)-1)) {
3698 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3699 -title => "Alt-n"}, "next");
3704 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3705 git_print_header_div('summary', $project);
3707 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3712 ## ......................................................................
3713 ## feeds (RSS, OPML)
3716 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3717 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3718 or die_error(undef, "Open git-rev-list failed");
3719 my @revlist = map { chomp; $_ } <$fd>;
3720 close $fd or die_error(undef, "Reading git-rev-list failed");
3721 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3723 <?xml version="1.0" encoding="utf-8"?>
3724 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3726 <title>$project $my_uri $my_url</title>
3727 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3728 <description>$project log</description>
3729 <language>en</language>
3732 for (my $i = 0; $i <= $#revlist; $i++) {
3733 my $commit = $revlist[$i];
3734 my %co = parse_commit($commit);
3735 # we read 150, we always show 30 and the ones more recent than 48 hours
3736 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3739 my %cd = parse_date($co{'committer_epoch'});
3740 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3741 $co{'parent'}, $co{'id'}
3743 my @difftree = map { chomp; $_ } <$fd>;
3748 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3750 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3751 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3752 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3753 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3754 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3755 "<content:encoded>" .
3757 my $comment = $co{'comment'};
3758 foreach my $line (@$comment) {
3759 $line = to_utf8($line);
3760 print "$line<br/>\n";
3763 foreach my $line (@difftree) {
3764 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3767 my $file = esc_html(unquote($7));
3768 $file = to_utf8($file);
3769 print "$file<br/>\n";
3772 "</content:encoded>\n" .
3775 print "</channel></rss>";
3779 my @list = git_get_projects_list();
3781 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3783 <?xml version="1.0" encoding="utf-8"?>
3784 <opml version="1.0">
3786 <title>$site_name Git OPML Export</title>
3789 <outline text="git RSS feeds">
3792 foreach my $pr (@list) {
3794 my $head = git_get_head_hash($proj{'path'});
3795 if (!defined $head) {
3798 $git_dir = "$projectroot/$proj{'path'}";
3799 my %co = parse_commit($head);
3804 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3805 my $rss = "$my_url?p=$proj{'path'};a=rss";
3806 my $html = "$my_url?p=$proj{'path'};a=summary";
3807 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";