pack-refs: Store the full name of the ref even when packing only tags.
[git/jrn.git] / gitweb / gitweb.perl
blobbf5f8299442ffe3ad8c4de6629af79667a67e5e2
1 #!/usr/bin/perl
3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
21 our $cgi = new CGI;
22 our $version = "++GIT_VERSION++";
23 our $my_url = $cgi->url();
24 our $my_uri = $cgi->url(-absolute => 1);
26 # core git executable to use
27 # this can just be "git" if your webserver has a sensible PATH
28 our $GIT = "++GIT_BINDIR++/git";
30 # absolute fs-path which will be prepended to the project path
31 #our $projectroot = "/pub/scm";
32 our $projectroot = "++GITWEB_PROJECTROOT++";
34 # 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++"
43 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
45 # filename of html text to include at top of each page
46 our $site_header = "++GITWEB_SITE_HEADER++";
47 # html text to include at home page
48 our $home_text = "++GITWEB_HOMETEXT++";
49 # filename of html text to include at bottom of each page
50 our $site_footer = "++GITWEB_SITE_FOOTER++";
52 # URI of stylesheets
53 our @stylesheets = ("++GITWEB_CSS++");
54 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
55 our $stylesheet = undef;
56 # URI of GIT logo (72x27 size)
57 our $logo = "++GITWEB_LOGO++";
58 # URI of GIT favicon, assumed to be image/png type
59 our $favicon = "++GITWEB_FAVICON++";
61 # URI and label (title) of GIT logo link
62 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
63 #our $logo_label = "git documentation";
64 our $logo_url = "http://git.or.cz/";
65 our $logo_label = "git homepage";
67 # source of projects list
68 our $projects_list = "++GITWEB_LIST++";
70 # show repository only if this file exists
71 # (only effective if this variable evaluates to true)
72 our $export_ok = "++GITWEB_EXPORT_OK++";
74 # only allow viewing of repositories also shown on the overview page
75 our $strict_export = "++GITWEB_STRICT_EXPORT++";
77 # list of git base URLs used for URL to where fetch project from,
78 # i.e. full URL is "$git_base_url/$project"
79 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
81 # default blob_plain mimetype and default charset for text/plain blob
82 our $default_blob_plain_mimetype = 'text/plain';
83 our $default_text_plain_charset = undef;
85 # file to use for guessing MIME types before trying /etc/mime.types
86 # (relative to the current git repository)
87 our $mimetypes_file = undef;
89 # You define site-wide feature defaults here; override them with
90 # $GITWEB_CONFIG as necessary.
91 our %feature = (
92 # feature => {
93 # 'sub' => feature-sub (subroutine),
94 # 'override' => allow-override (boolean),
95 # 'default' => [ default options...] (array reference)}
97 # if feature is overridable (it means that allow-override has true value,
98 # then feature-sub will be called with default options as parameters;
99 # return value of feature-sub indicates if to enable specified feature
101 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
103 # Enable the 'blame' blob view, showing the last commit that modified
104 # each line in the file. This can be very CPU-intensive.
106 # To enable system wide have in $GITWEB_CONFIG
107 # $feature{'blame'}{'default'} = [1];
108 # To have project specific config enable override in $GITWEB_CONFIG
109 # $feature{'blame'}{'override'} = 1;
110 # and in project config gitweb.blame = 0|1;
111 'blame' => {
112 'sub' => \&feature_blame,
113 'override' => 0,
114 'default' => [0]},
116 # Enable the 'snapshot' link, providing a compressed tarball of any
117 # tree. This can potentially generate high traffic if you have large
118 # project.
120 # To disable system wide have in $GITWEB_CONFIG
121 # $feature{'snapshot'}{'default'} = [undef];
122 # To have project specific config enable override in $GITWEB_CONFIG
123 # $feature{'blame'}{'override'} = 1;
124 # and in project config gitweb.snapshot = none|gzip|bzip2;
125 'snapshot' => {
126 'sub' => \&feature_snapshot,
127 'override' => 0,
128 # => [content-encoding, suffix, program]
129 'default' => ['x-gzip', 'gz', 'gzip']},
131 # Enable the pickaxe search, which will list the commits that modified
132 # a given string in a file. This can be practical and quite faster
133 # alternative to 'blame', but still potentially CPU-intensive.
135 # To enable system wide have in $GITWEB_CONFIG
136 # $feature{'pickaxe'}{'default'} = [1];
137 # To have project specific config enable override in $GITWEB_CONFIG
138 # $feature{'pickaxe'}{'override'} = 1;
139 # and in project config gitweb.pickaxe = 0|1;
140 'pickaxe' => {
141 'sub' => \&feature_pickaxe,
142 'override' => 0,
143 'default' => [1]},
145 # Make gitweb use an alternative format of the URLs which can be
146 # more readable and natural-looking: project name is embedded
147 # directly in the path and the query string contains other
148 # auxiliary information. All gitweb installations recognize
149 # URL in either format; this configures in which formats gitweb
150 # generates links.
152 # To enable system wide have in $GITWEB_CONFIG
153 # $feature{'pathinfo'}{'default'} = [1];
154 # Project specific override is not supported.
156 # Note that you will need to change the default location of CSS,
157 # favicon, logo and possibly other files to an absolute URL. Also,
158 # if gitweb.cgi serves as your indexfile, you will need to force
159 # $my_uri to contain the script name in your $GITWEB_CONFIG.
160 'pathinfo' => {
161 'override' => 0,
162 'default' => [0]},
165 sub gitweb_check_feature {
166 my ($name) = @_;
167 return unless exists $feature{$name};
168 my ($sub, $override, @defaults) = (
169 $feature{$name}{'sub'},
170 $feature{$name}{'override'},
171 @{$feature{$name}{'default'}});
172 if (!$override) { return @defaults; }
173 if (!defined $sub) {
174 warn "feature $name is not overrideable";
175 return @defaults;
177 return $sub->(@defaults);
180 sub feature_blame {
181 my ($val) = git_get_project_config('blame', '--bool');
183 if ($val eq 'true') {
184 return 1;
185 } elsif ($val eq 'false') {
186 return 0;
189 return $_[0];
192 sub feature_snapshot {
193 my ($ctype, $suffix, $command) = @_;
195 my ($val) = git_get_project_config('snapshot');
197 if ($val eq 'gzip') {
198 return ('x-gzip', 'gz', 'gzip');
199 } elsif ($val eq 'bzip2') {
200 return ('x-bzip2', 'bz2', 'bzip2');
201 } elsif ($val eq 'none') {
202 return ();
205 return ($ctype, $suffix, $command);
208 sub gitweb_have_snapshot {
209 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
210 my $have_snapshot = (defined $ctype && defined $suffix);
212 return $have_snapshot;
215 sub feature_pickaxe {
216 my ($val) = git_get_project_config('pickaxe', '--bool');
218 if ($val eq 'true') {
219 return (1);
220 } elsif ($val eq 'false') {
221 return (0);
224 return ($_[0]);
227 # checking HEAD file with -e is fragile if the repository was
228 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
229 # and then pruned.
230 sub check_head_link {
231 my ($dir) = @_;
232 my $headfile = "$dir/HEAD";
233 return ((-e $headfile) ||
234 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
237 sub check_export_ok {
238 my ($dir) = @_;
239 return (check_head_link($dir) &&
240 (!$export_ok || -e "$dir/$export_ok"));
243 # rename detection options for git-diff and git-diff-tree
244 # - default is '-M', with the cost proportional to
245 # (number of removed files) * (number of new files).
246 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
247 # (number of changed files + number of removed files) * (number of new files)
248 # - even more costly is '-C', '--find-copies-harder' with cost
249 # (number of files in the original tree) * (number of new files)
250 # - one might want to include '-B' option, e.g. '-B', '-M'
251 our @diff_opts = ('-M'); # taken from git_commit
253 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
254 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
256 # version of the core git binary
257 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
259 $projects_list ||= $projectroot;
261 # ======================================================================
262 # input validation and dispatch
263 our $action = $cgi->param('a');
264 if (defined $action) {
265 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
266 die_error(undef, "Invalid action parameter");
270 # parameters which are pathnames
271 our $project = $cgi->param('p');
272 if (defined $project) {
273 if (!validate_pathname($project) ||
274 !(-d "$projectroot/$project") ||
275 !check_head_link("$projectroot/$project") ||
276 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
277 ($strict_export && !project_in_list($project))) {
278 undef $project;
279 die_error(undef, "No such project");
283 our $file_name = $cgi->param('f');
284 if (defined $file_name) {
285 if (!validate_pathname($file_name)) {
286 die_error(undef, "Invalid file parameter");
290 our $file_parent = $cgi->param('fp');
291 if (defined $file_parent) {
292 if (!validate_pathname($file_parent)) {
293 die_error(undef, "Invalid file parent parameter");
297 # parameters which are refnames
298 our $hash = $cgi->param('h');
299 if (defined $hash) {
300 if (!validate_refname($hash)) {
301 die_error(undef, "Invalid hash parameter");
305 our $hash_parent = $cgi->param('hp');
306 if (defined $hash_parent) {
307 if (!validate_refname($hash_parent)) {
308 die_error(undef, "Invalid hash parent parameter");
312 our $hash_base = $cgi->param('hb');
313 if (defined $hash_base) {
314 if (!validate_refname($hash_base)) {
315 die_error(undef, "Invalid hash base parameter");
319 our $hash_parent_base = $cgi->param('hpb');
320 if (defined $hash_parent_base) {
321 if (!validate_refname($hash_parent_base)) {
322 die_error(undef, "Invalid hash parent base parameter");
326 # other parameters
327 our $page = $cgi->param('pg');
328 if (defined $page) {
329 if ($page =~ m/[^0-9]/) {
330 die_error(undef, "Invalid page parameter");
334 our $searchtext = $cgi->param('s');
335 if (defined $searchtext) {
336 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
337 die_error(undef, "Invalid search parameter");
339 $searchtext = quotemeta $searchtext;
342 our $searchtype = $cgi->param('st');
343 if (defined $searchtype) {
344 if ($searchtype =~ m/[^a-z]/) {
345 die_error(undef, "Invalid searchtype parameter");
349 # now read PATH_INFO and use it as alternative to parameters
350 sub evaluate_path_info {
351 return if defined $project;
352 my $path_info = $ENV{"PATH_INFO"};
353 return if !$path_info;
354 $path_info =~ s,^/+,,;
355 return if !$path_info;
356 # find which part of PATH_INFO is project
357 $project = $path_info;
358 $project =~ s,/+$,,;
359 while ($project && !check_head_link("$projectroot/$project")) {
360 $project =~ s,/*[^/]*$,,;
362 # validate project
363 $project = validate_pathname($project);
364 if (!$project ||
365 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
366 ($strict_export && !project_in_list($project))) {
367 undef $project;
368 return;
370 # do not change any parameters if an action is given using the query string
371 return if $action;
372 $path_info =~ s,^$project/*,,;
373 my ($refname, $pathname) = split(/:/, $path_info, 2);
374 if (defined $pathname) {
375 # we got "project.git/branch:filename" or "project.git/branch:dir/"
376 # we could use git_get_type(branch:pathname), but it needs $git_dir
377 $pathname =~ s,^/+,,;
378 if (!$pathname || substr($pathname, -1) eq "/") {
379 $action ||= "tree";
380 $pathname =~ s,/$,,;
381 } else {
382 $action ||= "blob_plain";
384 $hash_base ||= validate_refname($refname);
385 $file_name ||= validate_pathname($pathname);
386 } elsif (defined $refname) {
387 # we got "project.git/branch"
388 $action ||= "shortlog";
389 $hash ||= validate_refname($refname);
392 evaluate_path_info();
394 # path to the current git repository
395 our $git_dir;
396 $git_dir = "$projectroot/$project" if $project;
398 # dispatch
399 my %actions = (
400 "blame" => \&git_blame2,
401 "blobdiff" => \&git_blobdiff,
402 "blobdiff_plain" => \&git_blobdiff_plain,
403 "blob" => \&git_blob,
404 "blob_plain" => \&git_blob_plain,
405 "commitdiff" => \&git_commitdiff,
406 "commitdiff_plain" => \&git_commitdiff_plain,
407 "commit" => \&git_commit,
408 "heads" => \&git_heads,
409 "history" => \&git_history,
410 "log" => \&git_log,
411 "rss" => \&git_rss,
412 "search" => \&git_search,
413 "search_help" => \&git_search_help,
414 "shortlog" => \&git_shortlog,
415 "summary" => \&git_summary,
416 "tag" => \&git_tag,
417 "tags" => \&git_tags,
418 "tree" => \&git_tree,
419 "snapshot" => \&git_snapshot,
420 # those below don't need $project
421 "opml" => \&git_opml,
422 "project_list" => \&git_project_list,
423 "project_index" => \&git_project_index,
426 if (defined $project) {
427 $action ||= 'summary';
428 } else {
429 $action ||= 'project_list';
431 if (!defined($actions{$action})) {
432 die_error(undef, "Unknown action");
434 if ($action !~ m/^(opml|project_list|project_index)$/ &&
435 !$project) {
436 die_error(undef, "Project needed");
438 $actions{$action}->();
439 exit;
441 ## ======================================================================
442 ## action links
444 sub href(%) {
445 my %params = @_;
446 my $href = $my_uri;
448 # XXX: Warning: If you touch this, check the search form for updating,
449 # too.
451 my @mapping = (
452 project => "p",
453 action => "a",
454 file_name => "f",
455 file_parent => "fp",
456 hash => "h",
457 hash_parent => "hp",
458 hash_base => "hb",
459 hash_parent_base => "hpb",
460 page => "pg",
461 order => "o",
462 searchtext => "s",
463 searchtype => "st",
465 my %mapping = @mapping;
467 $params{'project'} = $project unless exists $params{'project'};
469 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
470 if ($use_pathinfo) {
471 # use PATH_INFO for project name
472 $href .= "/$params{'project'}" if defined $params{'project'};
473 delete $params{'project'};
475 # Summary just uses the project path URL
476 if (defined $params{'action'} && $params{'action'} eq 'summary') {
477 delete $params{'action'};
481 # now encode the parameters explicitly
482 my @result = ();
483 for (my $i = 0; $i < @mapping; $i += 2) {
484 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
485 if (defined $params{$name}) {
486 push @result, $symbol . "=" . esc_param($params{$name});
489 $href .= "?" . join(';', @result) if scalar @result;
491 return $href;
495 ## ======================================================================
496 ## validation, quoting/unquoting and escaping
498 sub validate_pathname {
499 my $input = shift || return undef;
501 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
502 # at the beginning, at the end, and between slashes.
503 # also this catches doubled slashes
504 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
505 return undef;
507 # no null characters
508 if ($input =~ m!\0!) {
509 return undef;
511 return $input;
514 sub validate_refname {
515 my $input = shift || return undef;
517 # textual hashes are O.K.
518 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
519 return $input;
521 # it must be correct pathname
522 $input = validate_pathname($input)
523 or return undef;
524 # restrictions on ref name according to git-check-ref-format
525 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
526 return undef;
528 return $input;
531 # very thin wrapper for decode("utf8", $str, Encode::FB_DEFAULT);
532 sub to_utf8 {
533 my $str = shift;
534 return decode("utf8", $str, Encode::FB_DEFAULT);
537 # quote unsafe chars, but keep the slash, even when it's not
538 # correct, but quoted slashes look too horrible in bookmarks
539 sub esc_param {
540 my $str = shift;
541 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
542 $str =~ s/\+/%2B/g;
543 $str =~ s/ /\+/g;
544 return $str;
547 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
548 sub esc_url {
549 my $str = shift;
550 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
551 $str =~ s/\+/%2B/g;
552 $str =~ s/ /\+/g;
553 return $str;
556 # replace invalid utf8 character with SUBSTITUTION sequence
557 sub esc_html {
558 my $str = shift;
559 $str = to_utf8($str);
560 $str = escapeHTML($str);
561 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
562 $str =~ s/\033/^[/g; # "escape" ESCAPE (\e) character (e.g. commit 20a3847d8a5032ce41f90dcc68abfb36e6fee9b1)
563 return $str;
566 # git may return quoted and escaped filenames
567 sub unquote {
568 my $str = shift;
569 if ($str =~ m/^"(.*)"$/) {
570 $str = $1;
571 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
573 return $str;
576 # escape tabs (convert tabs to spaces)
577 sub untabify {
578 my $line = shift;
580 while ((my $pos = index($line, "\t")) != -1) {
581 if (my $count = (8 - ($pos % 8))) {
582 my $spaces = ' ' x $count;
583 $line =~ s/\t/$spaces/;
587 return $line;
590 sub project_in_list {
591 my $project = shift;
592 my @list = git_get_projects_list();
593 return @list && scalar(grep { $_->{'path'} eq $project } @list);
596 ## ----------------------------------------------------------------------
597 ## HTML aware string manipulation
599 sub chop_str {
600 my $str = shift;
601 my $len = shift;
602 my $add_len = shift || 10;
604 # allow only $len chars, but don't cut a word if it would fit in $add_len
605 # if it doesn't fit, cut it if it's still longer than the dots we would add
606 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
607 my $body = $1;
608 my $tail = $2;
609 if (length($tail) > 4) {
610 $tail = " ...";
611 $body =~ s/&[^;]*$//; # remove chopped character entities
613 return "$body$tail";
616 ## ----------------------------------------------------------------------
617 ## functions returning short strings
619 # CSS class for given age value (in seconds)
620 sub age_class {
621 my $age = shift;
623 if ($age < 60*60*2) {
624 return "age0";
625 } elsif ($age < 60*60*24*2) {
626 return "age1";
627 } else {
628 return "age2";
632 # convert age in seconds to "nn units ago" string
633 sub age_string {
634 my $age = shift;
635 my $age_str;
637 if ($age > 60*60*24*365*2) {
638 $age_str = (int $age/60/60/24/365);
639 $age_str .= " years ago";
640 } elsif ($age > 60*60*24*(365/12)*2) {
641 $age_str = int $age/60/60/24/(365/12);
642 $age_str .= " months ago";
643 } elsif ($age > 60*60*24*7*2) {
644 $age_str = int $age/60/60/24/7;
645 $age_str .= " weeks ago";
646 } elsif ($age > 60*60*24*2) {
647 $age_str = int $age/60/60/24;
648 $age_str .= " days ago";
649 } elsif ($age > 60*60*2) {
650 $age_str = int $age/60/60;
651 $age_str .= " hours ago";
652 } elsif ($age > 60*2) {
653 $age_str = int $age/60;
654 $age_str .= " min ago";
655 } elsif ($age > 2) {
656 $age_str = int $age;
657 $age_str .= " sec ago";
658 } else {
659 $age_str .= " right now";
661 return $age_str;
664 # convert file mode in octal to symbolic file mode string
665 sub mode_str {
666 my $mode = oct shift;
668 if (S_ISDIR($mode & S_IFMT)) {
669 return 'drwxr-xr-x';
670 } elsif (S_ISLNK($mode)) {
671 return 'lrwxrwxrwx';
672 } elsif (S_ISREG($mode)) {
673 # git cares only about the executable bit
674 if ($mode & S_IXUSR) {
675 return '-rwxr-xr-x';
676 } else {
677 return '-rw-r--r--';
679 } else {
680 return '----------';
684 # convert file mode in octal to file type string
685 sub file_type {
686 my $mode = shift;
688 if ($mode !~ m/^[0-7]+$/) {
689 return $mode;
690 } else {
691 $mode = oct $mode;
694 if (S_ISDIR($mode & S_IFMT)) {
695 return "directory";
696 } elsif (S_ISLNK($mode)) {
697 return "symlink";
698 } elsif (S_ISREG($mode)) {
699 return "file";
700 } else {
701 return "unknown";
705 ## ----------------------------------------------------------------------
706 ## functions returning short HTML fragments, or transforming HTML fragments
707 ## which don't beling to other sections
709 # format line of commit message or tag comment
710 sub format_log_line_html {
711 my $line = shift;
713 $line = esc_html($line);
714 $line =~ s/ /&nbsp;/g;
715 if ($line =~ m/([0-9a-fA-F]{40})/) {
716 my $hash_text = $1;
717 if (git_get_type($hash_text) eq "commit") {
718 my $link =
719 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
720 -class => "text"}, $hash_text);
721 $line =~ s/$hash_text/$link/;
724 return $line;
727 # format marker of refs pointing to given object
728 sub format_ref_marker {
729 my ($refs, $id) = @_;
730 my $markers = '';
732 if (defined $refs->{$id}) {
733 foreach my $ref (@{$refs->{$id}}) {
734 my ($type, $name) = qw();
735 # e.g. tags/v2.6.11 or heads/next
736 if ($ref =~ m!^(.*?)s?/(.*)$!) {
737 $type = $1;
738 $name = $2;
739 } else {
740 $type = "ref";
741 $name = $ref;
744 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
748 if ($markers) {
749 return ' <span class="refs">'. $markers . '</span>';
750 } else {
751 return "";
755 # format, perhaps shortened and with markers, title line
756 sub format_subject_html {
757 my ($long, $short, $href, $extra) = @_;
758 $extra = '' unless defined($extra);
760 if (length($short) < length($long)) {
761 return $cgi->a({-href => $href, -class => "list subject",
762 -title => to_utf8($long)},
763 esc_html($short) . $extra);
764 } else {
765 return $cgi->a({-href => $href, -class => "list subject"},
766 esc_html($long) . $extra);
770 sub format_diff_line {
771 my $line = shift;
772 my $char = substr($line, 0, 1);
773 my $diff_class = "";
775 chomp $line;
777 if ($char eq '+') {
778 $diff_class = " add";
779 } elsif ($char eq "-") {
780 $diff_class = " rem";
781 } elsif ($char eq "@") {
782 $diff_class = " chunk_header";
783 } elsif ($char eq "\\") {
784 $diff_class = " incomplete";
786 $line = untabify($line);
787 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
790 ## ----------------------------------------------------------------------
791 ## git utility subroutines, invoking git commands
793 # returns path to the core git executable and the --git-dir parameter as list
794 sub git_cmd {
795 return $GIT, '--git-dir='.$git_dir;
798 # returns path to the core git executable and the --git-dir parameter as string
799 sub git_cmd_str {
800 return join(' ', git_cmd());
803 # get HEAD ref of given project as hash
804 sub git_get_head_hash {
805 my $project = shift;
806 my $o_git_dir = $git_dir;
807 my $retval = undef;
808 $git_dir = "$projectroot/$project";
809 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
810 my $head = <$fd>;
811 close $fd;
812 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
813 $retval = $1;
816 if (defined $o_git_dir) {
817 $git_dir = $o_git_dir;
819 return $retval;
822 # get type of given object
823 sub git_get_type {
824 my $hash = shift;
826 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
827 my $type = <$fd>;
828 close $fd or return;
829 chomp $type;
830 return $type;
833 sub git_get_project_config {
834 my ($key, $type) = @_;
836 return unless ($key);
837 $key =~ s/^gitweb\.//;
838 return if ($key =~ m/\W/);
840 my @x = (git_cmd(), 'repo-config');
841 if (defined $type) { push @x, $type; }
842 push @x, "--get";
843 push @x, "gitweb.$key";
844 my $val = qx(@x);
845 chomp $val;
846 return ($val);
849 # get hash of given path at given ref
850 sub git_get_hash_by_path {
851 my $base = shift;
852 my $path = shift || return undef;
853 my $type = shift;
855 $path =~ s,/+$,,;
857 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
858 or die_error(undef, "Open git-ls-tree failed");
859 my $line = <$fd>;
860 close $fd or return undef;
862 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
863 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
864 if (defined $type && $type ne $2) {
865 # type doesn't match
866 return undef;
868 return $3;
871 ## ......................................................................
872 ## git utility functions, directly accessing git repository
874 sub git_get_project_description {
875 my $path = shift;
877 open my $fd, "$projectroot/$path/description" or return undef;
878 my $descr = <$fd>;
879 close $fd;
880 chomp $descr;
881 return $descr;
884 sub git_get_project_url_list {
885 my $path = shift;
887 open my $fd, "$projectroot/$path/cloneurl" or return;
888 my @git_project_url_list = map { chomp; $_ } <$fd>;
889 close $fd;
891 return wantarray ? @git_project_url_list : \@git_project_url_list;
894 sub git_get_projects_list {
895 my @list;
897 if (-d $projects_list) {
898 # search in directory
899 my $dir = $projects_list;
900 my $pfxlen = length("$dir");
902 File::Find::find({
903 follow_fast => 1, # follow symbolic links
904 dangling_symlinks => 0, # ignore dangling symlinks, silently
905 wanted => sub {
906 # skip project-list toplevel, if we get it.
907 return if (m!^[/.]$!);
908 # only directories can be git repositories
909 return unless (-d $_);
911 my $subdir = substr($File::Find::name, $pfxlen + 1);
912 # we check related file in $projectroot
913 if (check_export_ok("$projectroot/$subdir")) {
914 push @list, { path => $subdir };
915 $File::Find::prune = 1;
918 }, "$dir");
920 } elsif (-f $projects_list) {
921 # read from file(url-encoded):
922 # 'git%2Fgit.git Linus+Torvalds'
923 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
924 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
925 open my ($fd), $projects_list or return;
926 while (my $line = <$fd>) {
927 chomp $line;
928 my ($path, $owner) = split ' ', $line;
929 $path = unescape($path);
930 $owner = unescape($owner);
931 if (!defined $path) {
932 next;
934 if (check_export_ok("$projectroot/$path")) {
935 my $pr = {
936 path => $path,
937 owner => to_utf8($owner),
939 push @list, $pr
942 close $fd;
944 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
945 return @list;
948 sub git_get_project_owner {
949 my $project = shift;
950 my $owner;
952 return undef unless $project;
954 # read from file (url-encoded):
955 # 'git%2Fgit.git Linus+Torvalds'
956 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
957 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
958 if (-f $projects_list) {
959 open (my $fd , $projects_list);
960 while (my $line = <$fd>) {
961 chomp $line;
962 my ($pr, $ow) = split ' ', $line;
963 $pr = unescape($pr);
964 $ow = unescape($ow);
965 if ($pr eq $project) {
966 $owner = to_utf8($ow);
967 last;
970 close $fd;
972 if (!defined $owner) {
973 $owner = get_file_owner("$projectroot/$project");
976 return $owner;
979 sub git_get_last_activity {
980 my ($path) = @_;
981 my $fd;
983 $git_dir = "$projectroot/$path";
984 open($fd, "-|", git_cmd(), 'for-each-ref',
985 '--format=%(refname) %(committer)',
986 '--sort=-committerdate',
987 'refs/heads') or return;
988 my $most_recent = <$fd>;
989 close $fd or return;
990 if ($most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
991 my $timestamp = $1;
992 my $age = time - $timestamp;
993 return ($age, age_string($age));
997 sub git_get_references {
998 my $type = shift || "";
999 my %refs;
1000 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1001 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1002 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1003 or return;
1005 while (my $line = <$fd>) {
1006 chomp $line;
1007 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
1008 if (defined $refs{$1}) {
1009 push @{$refs{$1}}, $2;
1010 } else {
1011 $refs{$1} = [ $2 ];
1015 close $fd or return;
1016 return \%refs;
1019 sub git_get_rev_name_tags {
1020 my $hash = shift || return undef;
1022 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1023 or return;
1024 my $name_rev = <$fd>;
1025 close $fd;
1027 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1028 return $1;
1029 } else {
1030 # catches also '$hash undefined' output
1031 return undef;
1035 ## ----------------------------------------------------------------------
1036 ## parse to hash functions
1038 sub parse_date {
1039 my $epoch = shift;
1040 my $tz = shift || "-0000";
1042 my %date;
1043 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1044 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1045 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1046 $date{'hour'} = $hour;
1047 $date{'minute'} = $min;
1048 $date{'mday'} = $mday;
1049 $date{'day'} = $days[$wday];
1050 $date{'month'} = $months[$mon];
1051 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1052 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1053 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1054 $mday, $months[$mon], $hour ,$min;
1056 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1057 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1058 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1059 $date{'hour_local'} = $hour;
1060 $date{'minute_local'} = $min;
1061 $date{'tz_local'} = $tz;
1062 $date{'iso-tz'} = sprintf ("%04d-%02d-%02d %02d:%02d:%02d %s",
1063 1900+$year, $mon+1, $mday,
1064 $hour, $min, $sec, $tz);
1065 return %date;
1068 sub parse_tag {
1069 my $tag_id = shift;
1070 my %tag;
1071 my @comment;
1073 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1074 $tag{'id'} = $tag_id;
1075 while (my $line = <$fd>) {
1076 chomp $line;
1077 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1078 $tag{'object'} = $1;
1079 } elsif ($line =~ m/^type (.+)$/) {
1080 $tag{'type'} = $1;
1081 } elsif ($line =~ m/^tag (.+)$/) {
1082 $tag{'name'} = $1;
1083 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1084 $tag{'author'} = $1;
1085 $tag{'epoch'} = $2;
1086 $tag{'tz'} = $3;
1087 } elsif ($line =~ m/--BEGIN/) {
1088 push @comment, $line;
1089 last;
1090 } elsif ($line eq "") {
1091 last;
1094 push @comment, <$fd>;
1095 $tag{'comment'} = \@comment;
1096 close $fd or return;
1097 if (!defined $tag{'name'}) {
1098 return
1100 return %tag
1103 sub parse_commit {
1104 my $commit_id = shift;
1105 my $commit_text = shift;
1107 my @commit_lines;
1108 my %co;
1110 if (defined $commit_text) {
1111 @commit_lines = @$commit_text;
1112 } else {
1113 local $/ = "\0";
1114 open my $fd, "-|", git_cmd(), "rev-list",
1115 "--header", "--parents", "--max-count=1",
1116 $commit_id, "--"
1117 or return;
1118 @commit_lines = split '\n', <$fd>;
1119 close $fd or return;
1120 pop @commit_lines;
1122 my $header = shift @commit_lines;
1123 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1124 return;
1126 ($co{'id'}, my @parents) = split ' ', $header;
1127 $co{'parents'} = \@parents;
1128 $co{'parent'} = $parents[0];
1129 while (my $line = shift @commit_lines) {
1130 last if $line eq "\n";
1131 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1132 $co{'tree'} = $1;
1133 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1134 $co{'author'} = $1;
1135 $co{'author_epoch'} = $2;
1136 $co{'author_tz'} = $3;
1137 if ($co{'author'} =~ m/^([^<]+) </) {
1138 $co{'author_name'} = $1;
1139 } else {
1140 $co{'author_name'} = $co{'author'};
1142 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1143 $co{'committer'} = $1;
1144 $co{'committer_epoch'} = $2;
1145 $co{'committer_tz'} = $3;
1146 $co{'committer_name'} = $co{'committer'};
1147 $co{'committer_name'} =~ s/ <.*//;
1150 if (!defined $co{'tree'}) {
1151 return;
1154 foreach my $title (@commit_lines) {
1155 $title =~ s/^ //;
1156 if ($title ne "") {
1157 $co{'title'} = chop_str($title, 80, 5);
1158 # remove leading stuff of merges to make the interesting part visible
1159 if (length($title) > 50) {
1160 $title =~ s/^Automatic //;
1161 $title =~ s/^merge (of|with) /Merge ... /i;
1162 if (length($title) > 50) {
1163 $title =~ s/(http|rsync):\/\///;
1165 if (length($title) > 50) {
1166 $title =~ s/(master|www|rsync)\.//;
1168 if (length($title) > 50) {
1169 $title =~ s/kernel.org:?//;
1171 if (length($title) > 50) {
1172 $title =~ s/\/pub\/scm//;
1175 $co{'title_short'} = chop_str($title, 50, 5);
1176 last;
1179 if ($co{'title'} eq "") {
1180 $co{'title'} = $co{'title_short'} = '(no commit message)';
1182 # remove added spaces
1183 foreach my $line (@commit_lines) {
1184 $line =~ s/^ //;
1186 $co{'comment'} = \@commit_lines;
1188 my $age = time - $co{'committer_epoch'};
1189 $co{'age'} = $age;
1190 $co{'age_string'} = age_string($age);
1191 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1192 if ($age > 60*60*24*7*2) {
1193 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1194 $co{'age_string_age'} = $co{'age_string'};
1195 } else {
1196 $co{'age_string_date'} = $co{'age_string'};
1197 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1199 return %co;
1202 # parse ref from ref_file, given by ref_id, with given type
1203 sub parse_ref {
1204 my $ref_file = shift;
1205 my $ref_id = shift;
1206 my $type = shift || git_get_type($ref_id);
1207 my %ref_item;
1209 $ref_item{'type'} = $type;
1210 $ref_item{'id'} = $ref_id;
1211 $ref_item{'epoch'} = 0;
1212 $ref_item{'age'} = "unknown";
1213 if ($type eq "tag") {
1214 my %tag = parse_tag($ref_id);
1215 $ref_item{'comment'} = $tag{'comment'};
1216 if ($tag{'type'} eq "commit") {
1217 my %co = parse_commit($tag{'object'});
1218 $ref_item{'epoch'} = $co{'committer_epoch'};
1219 $ref_item{'age'} = $co{'age_string'};
1220 } elsif (defined($tag{'epoch'})) {
1221 my $age = time - $tag{'epoch'};
1222 $ref_item{'epoch'} = $tag{'epoch'};
1223 $ref_item{'age'} = age_string($age);
1225 $ref_item{'reftype'} = $tag{'type'};
1226 $ref_item{'name'} = $tag{'name'};
1227 $ref_item{'refid'} = $tag{'object'};
1228 } elsif ($type eq "commit"){
1229 my %co = parse_commit($ref_id);
1230 $ref_item{'reftype'} = "commit";
1231 $ref_item{'name'} = $ref_file;
1232 $ref_item{'title'} = $co{'title'};
1233 $ref_item{'refid'} = $ref_id;
1234 $ref_item{'epoch'} = $co{'committer_epoch'};
1235 $ref_item{'age'} = $co{'age_string'};
1236 } else {
1237 $ref_item{'reftype'} = $type;
1238 $ref_item{'name'} = $ref_file;
1239 $ref_item{'refid'} = $ref_id;
1242 return %ref_item;
1245 # parse line of git-diff-tree "raw" output
1246 sub parse_difftree_raw_line {
1247 my $line = shift;
1248 my %res;
1250 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1251 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1252 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1253 $res{'from_mode'} = $1;
1254 $res{'to_mode'} = $2;
1255 $res{'from_id'} = $3;
1256 $res{'to_id'} = $4;
1257 $res{'status'} = $5;
1258 $res{'similarity'} = $6;
1259 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1260 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1261 } else {
1262 $res{'file'} = unquote($7);
1265 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1266 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1267 $res{'commit'} = $1;
1270 return wantarray ? %res : \%res;
1273 # parse line of git-ls-tree output
1274 sub parse_ls_tree_line ($;%) {
1275 my $line = shift;
1276 my %opts = @_;
1277 my %res;
1279 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1280 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1282 $res{'mode'} = $1;
1283 $res{'type'} = $2;
1284 $res{'hash'} = $3;
1285 if ($opts{'-z'}) {
1286 $res{'name'} = $4;
1287 } else {
1288 $res{'name'} = unquote($4);
1291 return wantarray ? %res : \%res;
1294 ## ......................................................................
1295 ## parse to array of hashes functions
1297 sub git_get_refs_list {
1298 my $type = shift || "";
1299 my %refs;
1300 my @reflist;
1302 my @refs;
1303 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1304 or return;
1305 while (my $line = <$fd>) {
1306 chomp $line;
1307 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1308 if (defined $refs{$1}) {
1309 push @{$refs{$1}}, $2;
1310 } else {
1311 $refs{$1} = [ $2 ];
1314 if (! $4) { # unpeeled, direct reference
1315 push @refs, { hash => $1, name => $3 }; # without type
1316 } elsif ($3 eq $refs[-1]{'name'}) {
1317 # most likely a tag is followed by its peeled
1318 # (deref) one, and when that happens we know the
1319 # previous one was of type 'tag'.
1320 $refs[-1]{'type'} = "tag";
1324 close $fd;
1326 foreach my $ref (@refs) {
1327 my $ref_file = $ref->{'name'};
1328 my $ref_id = $ref->{'hash'};
1330 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1331 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1333 push @reflist, \%ref_item;
1335 # sort refs by age
1336 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1337 return (\@reflist, \%refs);
1340 ## ----------------------------------------------------------------------
1341 ## filesystem-related functions
1343 sub get_file_owner {
1344 my $path = shift;
1346 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1347 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1348 if (!defined $gcos) {
1349 return undef;
1351 my $owner = $gcos;
1352 $owner =~ s/[,;].*$//;
1353 return to_utf8($owner);
1356 ## ......................................................................
1357 ## mimetype related functions
1359 sub mimetype_guess_file {
1360 my $filename = shift;
1361 my $mimemap = shift;
1362 -r $mimemap or return undef;
1364 my %mimemap;
1365 open(MIME, $mimemap) or return undef;
1366 while (<MIME>) {
1367 next if m/^#/; # skip comments
1368 my ($mime, $exts) = split(/\t+/);
1369 if (defined $exts) {
1370 my @exts = split(/\s+/, $exts);
1371 foreach my $ext (@exts) {
1372 $mimemap{$ext} = $mime;
1376 close(MIME);
1378 $filename =~ /\.([^.]*)$/;
1379 return $mimemap{$1};
1382 sub mimetype_guess {
1383 my $filename = shift;
1384 my $mime;
1385 $filename =~ /\./ or return undef;
1387 if ($mimetypes_file) {
1388 my $file = $mimetypes_file;
1389 if ($file !~ m!^/!) { # if it is relative path
1390 # it is relative to project
1391 $file = "$projectroot/$project/$file";
1393 $mime = mimetype_guess_file($filename, $file);
1395 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1396 return $mime;
1399 sub blob_mimetype {
1400 my $fd = shift;
1401 my $filename = shift;
1403 if ($filename) {
1404 my $mime = mimetype_guess($filename);
1405 $mime and return $mime;
1408 # just in case
1409 return $default_blob_plain_mimetype unless $fd;
1411 if (-T $fd) {
1412 return 'text/plain' .
1413 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1414 } elsif (! $filename) {
1415 return 'application/octet-stream';
1416 } elsif ($filename =~ m/\.png$/i) {
1417 return 'image/png';
1418 } elsif ($filename =~ m/\.gif$/i) {
1419 return 'image/gif';
1420 } elsif ($filename =~ m/\.jpe?g$/i) {
1421 return 'image/jpeg';
1422 } else {
1423 return 'application/octet-stream';
1427 ## ======================================================================
1428 ## functions printing HTML: header, footer, error page
1430 sub git_header_html {
1431 my $status = shift || "200 OK";
1432 my $expires = shift;
1434 my $title = "$site_name";
1435 if (defined $project) {
1436 $title .= " - $project";
1437 if (defined $action) {
1438 $title .= "/$action";
1439 if (defined $file_name) {
1440 $title .= " - " . esc_html($file_name);
1441 if ($action eq "tree" && $file_name !~ m|/$|) {
1442 $title .= "/";
1447 my $content_type;
1448 # require explicit support from the UA if we are to send the page as
1449 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1450 # we have to do this because MSIE sometimes globs '*/*', pretending to
1451 # support xhtml+xml but choking when it gets what it asked for.
1452 if (defined $cgi->http('HTTP_ACCEPT') &&
1453 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1454 $cgi->Accept('application/xhtml+xml') != 0) {
1455 $content_type = 'application/xhtml+xml';
1456 } else {
1457 $content_type = 'text/html';
1459 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1460 -status=> $status, -expires => $expires);
1461 print <<EOF;
1462 <?xml version="1.0" encoding="utf-8"?>
1463 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1464 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1465 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1466 <!-- git core binaries version $git_version -->
1467 <head>
1468 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1469 <meta name="generator" content="gitweb/$version git/$git_version"/>
1470 <meta name="robots" content="index, nofollow"/>
1471 <title>$title</title>
1473 # print out each stylesheet that exist
1474 if (defined $stylesheet) {
1475 #provides backwards capability for those people who define style sheet in a config file
1476 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1477 } else {
1478 foreach my $stylesheet (@stylesheets) {
1479 next unless $stylesheet;
1480 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1483 if (defined $project) {
1484 printf('<link rel="alternate" title="%s log" '.
1485 'href="%s" type="application/rss+xml"/>'."\n",
1486 esc_param($project), href(action=>"rss"));
1487 } else {
1488 printf('<link rel="alternate" title="%s projects list" '.
1489 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1490 $site_name, href(project=>undef, action=>"project_index"));
1491 printf('<link rel="alternate" title="%s projects logs" '.
1492 'href="%s" type="text/x-opml"/>'."\n",
1493 $site_name, href(project=>undef, action=>"opml"));
1495 if (defined $favicon) {
1496 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1499 print "</head>\n" .
1500 "<body>\n";
1502 if (-f $site_header) {
1503 open (my $fd, $site_header);
1504 print <$fd>;
1505 close $fd;
1508 print "<div class=\"page_header\">\n" .
1509 $cgi->a({-href => esc_url($logo_url),
1510 -title => $logo_label},
1511 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1512 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1513 if (defined $project) {
1514 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1515 if (defined $action) {
1516 print " / $action";
1518 print "\n";
1519 if (!defined $searchtext) {
1520 $searchtext = "";
1522 my $search_hash;
1523 if (defined $hash_base) {
1524 $search_hash = $hash_base;
1525 } elsif (defined $hash) {
1526 $search_hash = $hash;
1527 } else {
1528 $search_hash = "HEAD";
1530 $cgi->param("a", "search");
1531 $cgi->param("h", $search_hash);
1532 $cgi->param("p", $project);
1533 print $cgi->startform(-method => "get", -action => $my_uri) .
1534 "<div class=\"search\">\n" .
1535 $cgi->hidden(-name => "p") . "\n" .
1536 $cgi->hidden(-name => "a") . "\n" .
1537 $cgi->hidden(-name => "h") . "\n" .
1538 $cgi->popup_menu(-name => 'st', -default => 'commit',
1539 -values => ['commit', 'author', 'committer', 'pickaxe']) .
1540 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1541 " search:\n",
1542 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1543 "</div>" .
1544 $cgi->end_form() . "\n";
1546 print "</div>\n";
1549 sub git_footer_html {
1550 print "<div class=\"page_footer\">\n";
1551 if (defined $project) {
1552 my $descr = git_get_project_description($project);
1553 if (defined $descr) {
1554 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1556 print $cgi->a({-href => href(action=>"rss"),
1557 -class => "rss_logo"}, "RSS") . "\n";
1558 } else {
1559 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1560 -class => "rss_logo"}, "OPML") . " ";
1561 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1562 -class => "rss_logo"}, "TXT") . "\n";
1564 print "</div>\n" ;
1566 if (-f $site_footer) {
1567 open (my $fd, $site_footer);
1568 print <$fd>;
1569 close $fd;
1572 print "</body>\n" .
1573 "</html>";
1576 sub die_error {
1577 my $status = shift || "403 Forbidden";
1578 my $error = shift || "Malformed query, file missing or permission denied";
1580 git_header_html($status);
1581 print <<EOF;
1582 <div class="page_body">
1583 <br /><br />
1584 $status - $error
1585 <br />
1586 </div>
1588 git_footer_html();
1589 exit;
1592 ## ----------------------------------------------------------------------
1593 ## functions printing or outputting HTML: navigation
1595 sub git_print_page_nav {
1596 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1597 $extra = '' if !defined $extra; # pager or formats
1599 my @navs = qw(summary shortlog log commit commitdiff tree);
1600 if ($suppress) {
1601 @navs = grep { $_ ne $suppress } @navs;
1604 my %arg = map { $_ => {action=>$_} } @navs;
1605 if (defined $head) {
1606 for (qw(commit commitdiff)) {
1607 $arg{$_}{hash} = $head;
1609 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1610 for (qw(shortlog log)) {
1611 $arg{$_}{hash} = $head;
1615 $arg{tree}{hash} = $treehead if defined $treehead;
1616 $arg{tree}{hash_base} = $treebase if defined $treebase;
1618 print "<div class=\"page_nav\">\n" .
1619 (join " | ",
1620 map { $_ eq $current ?
1621 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1622 } @navs);
1623 print "<br/>\n$extra<br/>\n" .
1624 "</div>\n";
1627 sub format_paging_nav {
1628 my ($action, $hash, $head, $page, $nrevs) = @_;
1629 my $paging_nav;
1632 if ($hash ne $head || $page) {
1633 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1634 } else {
1635 $paging_nav .= "HEAD";
1638 if ($page > 0) {
1639 $paging_nav .= " &sdot; " .
1640 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1641 -accesskey => "p", -title => "Alt-p"}, "prev");
1642 } else {
1643 $paging_nav .= " &sdot; prev";
1646 if ($nrevs >= (100 * ($page+1)-1)) {
1647 $paging_nav .= " &sdot; " .
1648 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1649 -accesskey => "n", -title => "Alt-n"}, "next");
1650 } else {
1651 $paging_nav .= " &sdot; next";
1654 return $paging_nav;
1657 ## ......................................................................
1658 ## functions printing or outputting HTML: div
1660 sub git_print_header_div {
1661 my ($action, $title, $hash, $hash_base) = @_;
1662 my %args = ();
1664 $args{action} = $action;
1665 $args{hash} = $hash if $hash;
1666 $args{hash_base} = $hash_base if $hash_base;
1668 print "<div class=\"header\">\n" .
1669 $cgi->a({-href => href(%args), -class => "title"},
1670 $title ? $title : $action) .
1671 "\n</div>\n";
1674 #sub git_print_authorship (\%) {
1675 sub git_print_authorship {
1676 my $co = shift;
1678 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1679 print "<div class=\"author_date\">" .
1680 esc_html($co->{'author_name'}) .
1681 " [$ad{'rfc2822'}";
1682 if ($ad{'hour_local'} < 6) {
1683 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1684 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1685 } else {
1686 printf(" (%02d:%02d %s)",
1687 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1689 print "]</div>\n";
1692 sub git_print_page_path {
1693 my $name = shift;
1694 my $type = shift;
1695 my $hb = shift;
1698 print "<div class=\"page_path\">";
1699 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1700 -title => 'tree root'}, "[$project]");
1701 print " / ";
1702 if (defined $name) {
1703 my @dirname = split '/', $name;
1704 my $basename = pop @dirname;
1705 my $fullname = '';
1707 foreach my $dir (@dirname) {
1708 $fullname .= ($fullname ? '/' : '') . $dir;
1709 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1710 hash_base=>$hb),
1711 -title => $fullname}, esc_html($dir));
1712 print " / ";
1714 if (defined $type && $type eq 'blob') {
1715 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1716 hash_base=>$hb),
1717 -title => $name}, esc_html($basename));
1718 } elsif (defined $type && $type eq 'tree') {
1719 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1720 hash_base=>$hb),
1721 -title => $name}, esc_html($basename));
1722 print " / ";
1723 } else {
1724 print esc_html($basename);
1727 print "<br/></div>\n";
1730 # sub git_print_log (\@;%) {
1731 sub git_print_log ($;%) {
1732 my $log = shift;
1733 my %opts = @_;
1735 if ($opts{'-remove_title'}) {
1736 # remove title, i.e. first line of log
1737 shift @$log;
1739 # remove leading empty lines
1740 while (defined $log->[0] && $log->[0] eq "") {
1741 shift @$log;
1744 # print log
1745 my $signoff = 0;
1746 my $empty = 0;
1747 foreach my $line (@$log) {
1748 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1749 $signoff = 1;
1750 $empty = 0;
1751 if (! $opts{'-remove_signoff'}) {
1752 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1753 next;
1754 } else {
1755 # remove signoff lines
1756 next;
1758 } else {
1759 $signoff = 0;
1762 # print only one empty line
1763 # do not print empty line after signoff
1764 if ($line eq "") {
1765 next if ($empty || $signoff);
1766 $empty = 1;
1767 } else {
1768 $empty = 0;
1771 print format_log_line_html($line) . "<br/>\n";
1774 if ($opts{'-final_empty_line'}) {
1775 # end with single empty line
1776 print "<br/>\n" unless $empty;
1780 # print tree entry (row of git_tree), but without encompassing <tr> element
1781 sub git_print_tree_entry {
1782 my ($t, $basedir, $hash_base, $have_blame) = @_;
1784 my %base_key = ();
1785 $base_key{hash_base} = $hash_base if defined $hash_base;
1787 # The format of a table row is: mode list link. Where mode is
1788 # the mode of the entry, list is the name of the entry, an href,
1789 # and link is the action links of the entry.
1791 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1792 if ($t->{'type'} eq "blob") {
1793 print "<td class=\"list\">" .
1794 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1795 file_name=>"$basedir$t->{'name'}", %base_key),
1796 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1797 print "<td class=\"link\">";
1798 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1799 file_name=>"$basedir$t->{'name'}", %base_key)},
1800 "blob");
1801 if ($have_blame) {
1802 print " | " .
1803 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1804 file_name=>"$basedir$t->{'name'}", %base_key)},
1805 "blame");
1807 if (defined $hash_base) {
1808 print " | " .
1809 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1810 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1811 "history");
1813 print " | " .
1814 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1815 file_name=>"$basedir$t->{'name'}")},
1816 "raw");
1817 print "</td>\n";
1819 } elsif ($t->{'type'} eq "tree") {
1820 print "<td class=\"list\">";
1821 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1822 file_name=>"$basedir$t->{'name'}", %base_key)},
1823 esc_html($t->{'name'}));
1824 print "</td>\n";
1825 print "<td class=\"link\">";
1826 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1827 file_name=>"$basedir$t->{'name'}", %base_key)},
1828 "tree");
1829 if (defined $hash_base) {
1830 print " | " .
1831 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1832 file_name=>"$basedir$t->{'name'}")},
1833 "history");
1835 print "</td>\n";
1839 ## ......................................................................
1840 ## functions printing large fragments of HTML
1842 sub git_difftree_body {
1843 my ($difftree, $hash, $parent) = @_;
1845 print "<div class=\"list_head\">\n";
1846 if ($#{$difftree} > 10) {
1847 print(($#{$difftree} + 1) . " files changed:\n");
1849 print "</div>\n";
1851 print "<table class=\"diff_tree\">\n";
1852 my $alternate = 1;
1853 my $patchno = 0;
1854 foreach my $line (@{$difftree}) {
1855 my %diff = parse_difftree_raw_line($line);
1857 if ($alternate) {
1858 print "<tr class=\"dark\">\n";
1859 } else {
1860 print "<tr class=\"light\">\n";
1862 $alternate ^= 1;
1864 my ($to_mode_oct, $to_mode_str, $to_file_type);
1865 my ($from_mode_oct, $from_mode_str, $from_file_type);
1866 if ($diff{'to_mode'} ne ('0' x 6)) {
1867 $to_mode_oct = oct $diff{'to_mode'};
1868 if (S_ISREG($to_mode_oct)) { # only for regular file
1869 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1871 $to_file_type = file_type($diff{'to_mode'});
1873 if ($diff{'from_mode'} ne ('0' x 6)) {
1874 $from_mode_oct = oct $diff{'from_mode'};
1875 if (S_ISREG($to_mode_oct)) { # only for regular file
1876 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1878 $from_file_type = file_type($diff{'from_mode'});
1881 if ($diff{'status'} eq "A") { # created
1882 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1883 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1884 $mode_chng .= "]</span>";
1885 print "<td>";
1886 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1887 hash_base=>$hash, file_name=>$diff{'file'}),
1888 -class => "list"}, esc_html($diff{'file'}));
1889 print "</td>\n";
1890 print "<td>$mode_chng</td>\n";
1891 print "<td class=\"link\">";
1892 if ($action eq 'commitdiff') {
1893 # link to patch
1894 $patchno++;
1895 print $cgi->a({-href => "#patch$patchno"}, "patch");
1897 print "</td>\n";
1899 } elsif ($diff{'status'} eq "D") { # deleted
1900 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1901 print "<td>";
1902 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1903 hash_base=>$parent, file_name=>$diff{'file'}),
1904 -class => "list"}, esc_html($diff{'file'}));
1905 print "</td>\n";
1906 print "<td>$mode_chng</td>\n";
1907 print "<td class=\"link\">";
1908 if ($action eq 'commitdiff') {
1909 # link to patch
1910 $patchno++;
1911 print $cgi->a({-href => "#patch$patchno"}, "patch");
1912 print " | ";
1914 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1915 hash_base=>$parent, file_name=>$diff{'file'})},
1916 "blob") . " | ";
1917 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1918 file_name=>$diff{'file'})},
1919 "blame") . " | ";
1920 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1921 file_name=>$diff{'file'})},
1922 "history");
1923 print "</td>\n";
1925 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1926 my $mode_chnge = "";
1927 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1928 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1929 if ($from_file_type != $to_file_type) {
1930 $mode_chnge .= " from $from_file_type to $to_file_type";
1932 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1933 if ($from_mode_str && $to_mode_str) {
1934 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1935 } elsif ($to_mode_str) {
1936 $mode_chnge .= " mode: $to_mode_str";
1939 $mode_chnge .= "]</span>\n";
1941 print "<td>";
1942 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1943 hash_base=>$hash, file_name=>$diff{'file'}),
1944 -class => "list"}, esc_html($diff{'file'}));
1945 print "</td>\n";
1946 print "<td>$mode_chnge</td>\n";
1947 print "<td class=\"link\">";
1948 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1949 if ($action eq 'commitdiff') {
1950 # link to patch
1951 $patchno++;
1952 print $cgi->a({-href => "#patch$patchno"}, "patch");
1953 } else {
1954 print $cgi->a({-href => href(action=>"blobdiff",
1955 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1956 hash_base=>$hash, hash_parent_base=>$parent,
1957 file_name=>$diff{'file'})},
1958 "diff");
1960 print " | ";
1962 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1963 hash_base=>$hash, file_name=>$diff{'file'})},
1964 "blob") . " | ";
1965 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1966 file_name=>$diff{'file'})},
1967 "blame") . " | ";
1968 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1969 file_name=>$diff{'file'})},
1970 "history");
1971 print "</td>\n";
1973 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1974 my %status_name = ('R' => 'moved', 'C' => 'copied');
1975 my $nstatus = $status_name{$diff{'status'}};
1976 my $mode_chng = "";
1977 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1978 # mode also for directories, so we cannot use $to_mode_str
1979 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1981 print "<td>" .
1982 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1983 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1984 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1985 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1986 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1987 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1988 -class => "list"}, esc_html($diff{'from_file'})) .
1989 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1990 "<td class=\"link\">";
1991 if ($diff{'to_id'} ne $diff{'from_id'}) {
1992 if ($action eq 'commitdiff') {
1993 # link to patch
1994 $patchno++;
1995 print $cgi->a({-href => "#patch$patchno"}, "patch");
1996 } else {
1997 print $cgi->a({-href => href(action=>"blobdiff",
1998 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1999 hash_base=>$hash, hash_parent_base=>$parent,
2000 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
2001 "diff");
2003 print " | ";
2005 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
2006 hash_base=>$parent, file_name=>$diff{'from_file'})},
2007 "blob") . " | ";
2008 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2009 file_name=>$diff{'from_file'})},
2010 "blame") . " | ";
2011 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2012 file_name=>$diff{'from_file'})},
2013 "history");
2014 print "</td>\n";
2016 } # we should not encounter Unmerged (U) or Unknown (X) status
2017 print "</tr>\n";
2019 print "</table>\n";
2022 sub git_patchset_body {
2023 my ($fd, $difftree, $hash, $hash_parent) = @_;
2025 my $patch_idx = 0;
2026 my $in_header = 0;
2027 my $patch_found = 0;
2028 my $diffinfo;
2030 print "<div class=\"patchset\">\n";
2032 LINE:
2033 while (my $patch_line = <$fd>) {
2034 chomp $patch_line;
2036 if ($patch_line =~ m/^diff /) { # "git diff" header
2037 # beginning of patch (in patchset)
2038 if ($patch_found) {
2039 # close previous patch
2040 print "</div>\n"; # class="patch"
2041 } else {
2042 # first patch in patchset
2043 $patch_found = 1;
2045 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2047 if (ref($difftree->[$patch_idx]) eq "HASH") {
2048 $diffinfo = $difftree->[$patch_idx];
2049 } else {
2050 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2052 $patch_idx++;
2054 # for now, no extended header, hence we skip empty patches
2055 # companion to next LINE if $in_header;
2056 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
2057 $in_header = 1;
2058 next LINE;
2061 if ($diffinfo->{'status'} eq "A") { # added
2062 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
2063 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2064 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2065 $diffinfo->{'to_id'}) . " (new)" .
2066 "</div>\n"; # class="diff_info"
2068 } elsif ($diffinfo->{'status'} eq "D") { # deleted
2069 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
2070 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2071 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2072 $diffinfo->{'from_id'}) . " (deleted)" .
2073 "</div>\n"; # class="diff_info"
2075 } elsif ($diffinfo->{'status'} eq "R" || # renamed
2076 $diffinfo->{'status'} eq "C" || # copied
2077 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
2078 print "<div class=\"diff_info\">" .
2079 file_type($diffinfo->{'from_mode'}) . ":" .
2080 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2081 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
2082 $diffinfo->{'from_id'}) .
2083 " -> " .
2084 file_type($diffinfo->{'to_mode'}) . ":" .
2085 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2086 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
2087 $diffinfo->{'to_id'});
2088 print "</div>\n"; # class="diff_info"
2090 } else { # modified, mode changed, ...
2091 print "<div class=\"diff_info\">" .
2092 file_type($diffinfo->{'from_mode'}) . ":" .
2093 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2094 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2095 $diffinfo->{'from_id'}) .
2096 " -> " .
2097 file_type($diffinfo->{'to_mode'}) . ":" .
2098 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2099 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2100 $diffinfo->{'to_id'});
2101 print "</div>\n"; # class="diff_info"
2104 #print "<div class=\"diff extended_header\">\n";
2105 $in_header = 1;
2106 next LINE;
2107 } # start of patch in patchset
2110 if ($in_header && $patch_line =~ m/^---/) {
2111 #print "</div>\n"; # class="diff extended_header"
2112 $in_header = 0;
2114 my $file = $diffinfo->{'from_file'};
2115 $file ||= $diffinfo->{'file'};
2116 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2117 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2118 -class => "list"}, esc_html($file));
2119 $patch_line =~ s|a/.*$|a/$file|g;
2120 print "<div class=\"diff from_file\">$patch_line</div>\n";
2122 $patch_line = <$fd>;
2123 chomp $patch_line;
2125 #$patch_line =~ m/^+++/;
2126 $file = $diffinfo->{'to_file'};
2127 $file ||= $diffinfo->{'file'};
2128 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2129 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2130 -class => "list"}, esc_html($file));
2131 $patch_line =~ s|b/.*|b/$file|g;
2132 print "<div class=\"diff to_file\">$patch_line</div>\n";
2134 next LINE;
2136 next LINE if $in_header;
2138 print format_diff_line($patch_line);
2140 print "</div>\n" if $patch_found; # class="patch"
2142 print "</div>\n"; # class="patchset"
2145 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2147 sub git_shortlog_body {
2148 # uses global variable $project
2149 my ($revlist, $from, $to, $refs, $extra) = @_;
2151 $from = 0 unless defined $from;
2152 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2154 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2155 my $alternate = 1;
2156 for (my $i = $from; $i <= $to; $i++) {
2157 my $commit = $revlist->[$i];
2158 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2159 my $ref = format_ref_marker($refs, $commit);
2160 my %co = parse_commit($commit);
2161 if ($alternate) {
2162 print "<tr class=\"dark\">\n";
2163 } else {
2164 print "<tr class=\"light\">\n";
2166 $alternate ^= 1;
2167 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2168 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2169 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2170 "<td>";
2171 print format_subject_html($co{'title'}, $co{'title_short'},
2172 href(action=>"commit", hash=>$commit), $ref);
2173 print "</td>\n" .
2174 "<td class=\"link\">" .
2175 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2176 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2177 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2178 if (gitweb_have_snapshot()) {
2179 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2181 print "</td>\n" .
2182 "</tr>\n";
2184 if (defined $extra) {
2185 print "<tr>\n" .
2186 "<td colspan=\"4\">$extra</td>\n" .
2187 "</tr>\n";
2189 print "</table>\n";
2192 sub git_history_body {
2193 # Warning: assumes constant type (blob or tree) during history
2194 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2196 $from = 0 unless defined $from;
2197 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2199 print "<table class=\"history\" cellspacing=\"0\">\n";
2200 my $alternate = 1;
2201 for (my $i = $from; $i <= $to; $i++) {
2202 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2203 next;
2206 my $commit = $1;
2207 my %co = parse_commit($commit);
2208 if (!%co) {
2209 next;
2212 my $ref = format_ref_marker($refs, $commit);
2214 if ($alternate) {
2215 print "<tr class=\"dark\">\n";
2216 } else {
2217 print "<tr class=\"light\">\n";
2219 $alternate ^= 1;
2220 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2221 # shortlog uses chop_str($co{'author_name'}, 10)
2222 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2223 "<td>";
2224 # originally git_history used chop_str($co{'title'}, 50)
2225 print format_subject_html($co{'title'}, $co{'title_short'},
2226 href(action=>"commit", hash=>$commit), $ref);
2227 print "</td>\n" .
2228 "<td class=\"link\">" .
2229 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2230 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2232 if ($ftype eq 'blob') {
2233 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2234 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2235 if (defined $blob_current && defined $blob_parent &&
2236 $blob_current ne $blob_parent) {
2237 print " | " .
2238 $cgi->a({-href => href(action=>"blobdiff",
2239 hash=>$blob_current, hash_parent=>$blob_parent,
2240 hash_base=>$hash_base, hash_parent_base=>$commit,
2241 file_name=>$file_name)},
2242 "diff to current");
2245 print "</td>\n" .
2246 "</tr>\n";
2248 if (defined $extra) {
2249 print "<tr>\n" .
2250 "<td colspan=\"4\">$extra</td>\n" .
2251 "</tr>\n";
2253 print "</table>\n";
2256 sub git_tags_body {
2257 # uses global variable $project
2258 my ($taglist, $from, $to, $extra) = @_;
2259 $from = 0 unless defined $from;
2260 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2262 print "<table class=\"tags\" cellspacing=\"0\">\n";
2263 my $alternate = 1;
2264 for (my $i = $from; $i <= $to; $i++) {
2265 my $entry = $taglist->[$i];
2266 my %tag = %$entry;
2267 my $comment_lines = $tag{'comment'};
2268 my $comment = shift @$comment_lines;
2269 my $comment_short;
2270 if (defined $comment) {
2271 $comment_short = chop_str($comment, 30, 5);
2273 if ($alternate) {
2274 print "<tr class=\"dark\">\n";
2275 } else {
2276 print "<tr class=\"light\">\n";
2278 $alternate ^= 1;
2279 print "<td><i>$tag{'age'}</i></td>\n" .
2280 "<td>" .
2281 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2282 -class => "list name"}, esc_html($tag{'name'})) .
2283 "</td>\n" .
2284 "<td>";
2285 if (defined $comment) {
2286 print format_subject_html($comment, $comment_short,
2287 href(action=>"tag", hash=>$tag{'id'}));
2289 print "</td>\n" .
2290 "<td class=\"selflink\">";
2291 if ($tag{'type'} eq "tag") {
2292 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2293 } else {
2294 print "&nbsp;";
2296 print "</td>\n" .
2297 "<td class=\"link\">" . " | " .
2298 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2299 if ($tag{'reftype'} eq "commit") {
2300 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2301 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2302 } elsif ($tag{'reftype'} eq "blob") {
2303 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2305 print "</td>\n" .
2306 "</tr>";
2308 if (defined $extra) {
2309 print "<tr>\n" .
2310 "<td colspan=\"5\">$extra</td>\n" .
2311 "</tr>\n";
2313 print "</table>\n";
2316 sub git_heads_body {
2317 # uses global variable $project
2318 my ($headlist, $head, $from, $to, $extra) = @_;
2319 $from = 0 unless defined $from;
2320 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2322 print "<table class=\"heads\" cellspacing=\"0\">\n";
2323 my $alternate = 1;
2324 for (my $i = $from; $i <= $to; $i++) {
2325 my $entry = $headlist->[$i];
2326 my %tag = %$entry;
2327 my $curr = $tag{'id'} eq $head;
2328 if ($alternate) {
2329 print "<tr class=\"dark\">\n";
2330 } else {
2331 print "<tr class=\"light\">\n";
2333 $alternate ^= 1;
2334 print "<td><i>$tag{'age'}</i></td>\n" .
2335 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2336 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2337 -class => "list name"},esc_html($tag{'name'})) .
2338 "</td>\n" .
2339 "<td class=\"link\">" .
2340 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2341 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2342 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2343 "</td>\n" .
2344 "</tr>";
2346 if (defined $extra) {
2347 print "<tr>\n" .
2348 "<td colspan=\"3\">$extra</td>\n" .
2349 "</tr>\n";
2351 print "</table>\n";
2354 ## ======================================================================
2355 ## ======================================================================
2356 ## actions
2358 sub git_project_list {
2359 my $order = $cgi->param('o');
2360 if (defined $order && $order !~ m/project|descr|owner|age/) {
2361 die_error(undef, "Unknown order parameter");
2364 my @list = git_get_projects_list();
2365 my @projects;
2366 if (!@list) {
2367 die_error(undef, "No projects found");
2369 foreach my $pr (@list) {
2370 my (@aa) = git_get_last_activity($pr->{'path'});
2371 unless (@aa) {
2372 next;
2374 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2375 if (!defined $pr->{'descr'}) {
2376 my $descr = git_get_project_description($pr->{'path'}) || "";
2377 $pr->{'descr'} = chop_str($descr, 25, 5);
2379 if (!defined $pr->{'owner'}) {
2380 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2382 push @projects, $pr;
2385 git_header_html();
2386 if (-f $home_text) {
2387 print "<div class=\"index_include\">\n";
2388 open (my $fd, $home_text);
2389 print <$fd>;
2390 close $fd;
2391 print "</div>\n";
2393 print "<table class=\"project_list\">\n" .
2394 "<tr>\n";
2395 $order ||= "project";
2396 if ($order eq "project") {
2397 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2398 print "<th>Project</th>\n";
2399 } else {
2400 print "<th>" .
2401 $cgi->a({-href => href(project=>undef, order=>'project'),
2402 -class => "header"}, "Project") .
2403 "</th>\n";
2405 if ($order eq "descr") {
2406 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2407 print "<th>Description</th>\n";
2408 } else {
2409 print "<th>" .
2410 $cgi->a({-href => href(project=>undef, order=>'descr'),
2411 -class => "header"}, "Description") .
2412 "</th>\n";
2414 if ($order eq "owner") {
2415 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2416 print "<th>Owner</th>\n";
2417 } else {
2418 print "<th>" .
2419 $cgi->a({-href => href(project=>undef, order=>'owner'),
2420 -class => "header"}, "Owner") .
2421 "</th>\n";
2423 if ($order eq "age") {
2424 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2425 print "<th>Last Change</th>\n";
2426 } else {
2427 print "<th>" .
2428 $cgi->a({-href => href(project=>undef, order=>'age'),
2429 -class => "header"}, "Last Change") .
2430 "</th>\n";
2432 print "<th></th>\n" .
2433 "</tr>\n";
2434 my $alternate = 1;
2435 foreach my $pr (@projects) {
2436 if ($alternate) {
2437 print "<tr class=\"dark\">\n";
2438 } else {
2439 print "<tr class=\"light\">\n";
2441 $alternate ^= 1;
2442 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2443 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2444 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2445 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2446 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2447 $pr->{'age_string'} . "</td>\n" .
2448 "<td class=\"link\">" .
2449 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2450 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2451 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2452 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2453 "</td>\n" .
2454 "</tr>\n";
2456 print "</table>\n";
2457 git_footer_html();
2460 sub git_project_index {
2461 my @projects = git_get_projects_list();
2463 print $cgi->header(
2464 -type => 'text/plain',
2465 -charset => 'utf-8',
2466 -content_disposition => 'inline; filename="index.aux"');
2468 foreach my $pr (@projects) {
2469 if (!exists $pr->{'owner'}) {
2470 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2473 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2474 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2475 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2476 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2477 $path =~ s/ /\+/g;
2478 $owner =~ s/ /\+/g;
2480 print "$path $owner\n";
2484 sub git_summary {
2485 my $descr = git_get_project_description($project) || "none";
2486 my $head = git_get_head_hash($project);
2487 my %co = parse_commit($head);
2488 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2490 my $owner = git_get_project_owner($project);
2492 my ($reflist, $refs) = git_get_refs_list();
2494 my @taglist;
2495 my @headlist;
2496 foreach my $ref (@$reflist) {
2497 if ($ref->{'name'} =~ s!^heads/!!) {
2498 push @headlist, $ref;
2499 } else {
2500 $ref->{'name'} =~ s!^tags/!!;
2501 push @taglist, $ref;
2505 git_header_html();
2506 git_print_page_nav('summary','', $head);
2508 print "<div class=\"title\">&nbsp;</div>\n";
2509 print "<table cellspacing=\"0\">\n" .
2510 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2511 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2512 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2513 # use per project git URL list in $projectroot/$project/cloneurl
2514 # or make project git URL from git base URL and project name
2515 my $url_tag = "URL";
2516 my @url_list = git_get_project_url_list($project);
2517 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2518 foreach my $git_url (@url_list) {
2519 next unless $git_url;
2520 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2521 $url_tag = "";
2523 print "</table>\n";
2525 if (-s "$projectroot/$project/README.html") {
2526 if (open my $fd, "$projectroot/$project/README.html") {
2527 print "<div class=\"title\">readme</div>\n";
2528 print $_ while (<$fd>);
2529 close $fd;
2533 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2534 git_get_head_hash($project), "--"
2535 or die_error(undef, "Open git-rev-list failed");
2536 my @revlist = map { chomp; $_ } <$fd>;
2537 close $fd;
2538 git_print_header_div('shortlog');
2539 git_shortlog_body(\@revlist, 0, 15, $refs,
2540 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2542 if (@taglist) {
2543 git_print_header_div('tags');
2544 git_tags_body(\@taglist, 0, 15,
2545 $cgi->a({-href => href(action=>"tags")}, "..."));
2548 if (@headlist) {
2549 git_print_header_div('heads');
2550 git_heads_body(\@headlist, $head, 0, 15,
2551 $cgi->a({-href => href(action=>"heads")}, "..."));
2554 git_footer_html();
2557 sub git_tag {
2558 my $head = git_get_head_hash($project);
2559 git_header_html();
2560 git_print_page_nav('','', $head,undef,$head);
2561 my %tag = parse_tag($hash);
2562 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2563 print "<div class=\"title_text\">\n" .
2564 "<table cellspacing=\"0\">\n" .
2565 "<tr>\n" .
2566 "<td>object</td>\n" .
2567 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2568 $tag{'object'}) . "</td>\n" .
2569 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2570 $tag{'type'}) . "</td>\n" .
2571 "</tr>\n";
2572 if (defined($tag{'author'})) {
2573 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2574 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2575 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2576 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2577 "</td></tr>\n";
2579 print "</table>\n\n" .
2580 "</div>\n";
2581 print "<div class=\"page_body\">";
2582 my $comment = $tag{'comment'};
2583 foreach my $line (@$comment) {
2584 print esc_html($line) . "<br/>\n";
2586 print "</div>\n";
2587 git_footer_html();
2590 sub git_blame2 {
2591 my $fd;
2592 my $ftype;
2594 my ($have_blame) = gitweb_check_feature('blame');
2595 if (!$have_blame) {
2596 die_error('403 Permission denied', "Permission denied");
2598 die_error('404 Not Found', "File name not defined") if (!$file_name);
2599 $hash_base ||= git_get_head_hash($project);
2600 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2601 my %co = parse_commit($hash_base)
2602 or die_error(undef, "Reading commit failed");
2603 if (!defined $hash) {
2604 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2605 or die_error(undef, "Error looking up file");
2607 $ftype = git_get_type($hash);
2608 if ($ftype !~ "blob") {
2609 die_error("400 Bad Request", "Object is not a blob");
2611 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
2612 $file_name, $hash_base)
2613 or die_error(undef, "Open git-blame failed");
2614 git_header_html();
2615 my $formats_nav =
2616 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2617 "blob") .
2618 " | " .
2619 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2620 "history") .
2621 " | " .
2622 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2623 "HEAD");
2624 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2625 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2626 git_print_page_path($file_name, $ftype, $hash_base);
2627 my @rev_color = (qw(light2 dark2));
2628 my $num_colors = scalar(@rev_color);
2629 my $current_color = 0;
2630 my $last_rev;
2631 print <<HTML;
2632 <div class="page_body">
2633 <table class="blame">
2634 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2635 HTML
2636 my %metainfo = ();
2637 while (1) {
2638 $_ = <$fd>;
2639 last unless defined $_;
2640 my ($full_rev, $orig_lineno, $lineno, $group_size) =
2641 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
2642 if (!exists $metainfo{$full_rev}) {
2643 $metainfo{$full_rev} = {};
2645 my $meta = $metainfo{$full_rev};
2646 while (<$fd>) {
2647 last if (s/^\t//);
2648 if (/^(\S+) (.*)$/) {
2649 $meta->{$1} = $2;
2652 my $data = $_;
2653 my $rev = substr($full_rev, 0, 8);
2654 my $author = $meta->{'author'};
2655 my %date = parse_date($meta->{'author-time'},
2656 $meta->{'author-tz'});
2657 my $date = $date{'iso-tz'};
2658 if ($group_size) {
2659 $current_color = ++$current_color % $num_colors;
2661 print "<tr class=\"$rev_color[$current_color]\">\n";
2662 if ($group_size) {
2663 print "<td class=\"sha1\"";
2664 print " title=\"". esc_html($author) . ", $date\"";
2665 print " rowspan=\"$group_size\"" if ($group_size > 1);
2666 print ">";
2667 print $cgi->a({-href => href(action=>"commit",
2668 hash=>$full_rev,
2669 file_name=>$file_name)},
2670 esc_html($rev));
2671 print "</td>\n";
2673 my $blamed = href(action => 'blame',
2674 file_name => $meta->{'filename'},
2675 hash_base => $full_rev);
2676 print "<td class=\"linenr\">";
2677 print $cgi->a({ -href => "$blamed#l$orig_lineno",
2678 -id => "l$lineno",
2679 -class => "linenr" },
2680 esc_html($lineno));
2681 print "</td>";
2682 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2683 print "</tr>\n";
2685 print "</table>\n";
2686 print "</div>";
2687 close $fd
2688 or print "Reading blob failed\n";
2689 git_footer_html();
2692 sub git_blame {
2693 my $fd;
2695 my ($have_blame) = gitweb_check_feature('blame');
2696 if (!$have_blame) {
2697 die_error('403 Permission denied', "Permission denied");
2699 die_error('404 Not Found', "File name not defined") if (!$file_name);
2700 $hash_base ||= git_get_head_hash($project);
2701 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2702 my %co = parse_commit($hash_base)
2703 or die_error(undef, "Reading commit failed");
2704 if (!defined $hash) {
2705 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2706 or die_error(undef, "Error lookup file");
2708 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2709 or die_error(undef, "Open git-annotate failed");
2710 git_header_html();
2711 my $formats_nav =
2712 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2713 "blob") .
2714 " | " .
2715 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2716 "history") .
2717 " | " .
2718 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2719 "HEAD");
2720 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2721 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2722 git_print_page_path($file_name, 'blob', $hash_base);
2723 print "<div class=\"page_body\">\n";
2724 print <<HTML;
2725 <table class="blame">
2726 <tr>
2727 <th>Commit</th>
2728 <th>Age</th>
2729 <th>Author</th>
2730 <th>Line</th>
2731 <th>Data</th>
2732 </tr>
2733 HTML
2734 my @line_class = (qw(light dark));
2735 my $line_class_len = scalar (@line_class);
2736 my $line_class_num = $#line_class;
2737 while (my $line = <$fd>) {
2738 my $long_rev;
2739 my $short_rev;
2740 my $author;
2741 my $time;
2742 my $lineno;
2743 my $data;
2744 my $age;
2745 my $age_str;
2746 my $age_class;
2748 chomp $line;
2749 $line_class_num = ($line_class_num + 1) % $line_class_len;
2751 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2752 $long_rev = $1;
2753 $author = $2;
2754 $time = $3;
2755 $lineno = $4;
2756 $data = $5;
2757 } else {
2758 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2759 next;
2761 $short_rev = substr ($long_rev, 0, 8);
2762 $age = time () - $time;
2763 $age_str = age_string ($age);
2764 $age_str =~ s/ /&nbsp;/g;
2765 $age_class = age_class($age);
2766 $author = esc_html ($author);
2767 $author =~ s/ /&nbsp;/g;
2769 $data = untabify($data);
2770 $data = esc_html ($data);
2772 print <<HTML;
2773 <tr class="$line_class[$line_class_num]">
2774 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2775 <td class="$age_class">$age_str</td>
2776 <td>$author</td>
2777 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2778 <td class="pre">$data</td>
2779 </tr>
2780 HTML
2781 } # while (my $line = <$fd>)
2782 print "</table>\n\n";
2783 close $fd
2784 or print "Reading blob failed.\n";
2785 print "</div>";
2786 git_footer_html();
2789 sub git_tags {
2790 my $head = git_get_head_hash($project);
2791 git_header_html();
2792 git_print_page_nav('','', $head,undef,$head);
2793 git_print_header_div('summary', $project);
2795 my ($taglist) = git_get_refs_list("tags");
2796 if (@$taglist) {
2797 git_tags_body($taglist);
2799 git_footer_html();
2802 sub git_heads {
2803 my $head = git_get_head_hash($project);
2804 git_header_html();
2805 git_print_page_nav('','', $head,undef,$head);
2806 git_print_header_div('summary', $project);
2808 my ($headlist) = git_get_refs_list("heads");
2809 if (@$headlist) {
2810 git_heads_body($headlist, $head);
2812 git_footer_html();
2815 sub git_blob_plain {
2816 my $expires;
2818 if (!defined $hash) {
2819 if (defined $file_name) {
2820 my $base = $hash_base || git_get_head_hash($project);
2821 $hash = git_get_hash_by_path($base, $file_name, "blob")
2822 or die_error(undef, "Error lookup file");
2823 } else {
2824 die_error(undef, "No file name defined");
2826 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2827 # blobs defined by non-textual hash id's can be cached
2828 $expires = "+1d";
2831 my $type = shift;
2832 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2833 or die_error(undef, "Couldn't cat $file_name, $hash");
2835 $type ||= blob_mimetype($fd, $file_name);
2837 # save as filename, even when no $file_name is given
2838 my $save_as = "$hash";
2839 if (defined $file_name) {
2840 $save_as = $file_name;
2841 } elsif ($type =~ m/^text\//) {
2842 $save_as .= '.txt';
2845 print $cgi->header(
2846 -type => "$type",
2847 -expires=>$expires,
2848 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2849 undef $/;
2850 binmode STDOUT, ':raw';
2851 print <$fd>;
2852 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2853 $/ = "\n";
2854 close $fd;
2857 sub git_blob {
2858 my $expires;
2860 if (!defined $hash) {
2861 if (defined $file_name) {
2862 my $base = $hash_base || git_get_head_hash($project);
2863 $hash = git_get_hash_by_path($base, $file_name, "blob")
2864 or die_error(undef, "Error lookup file");
2865 } else {
2866 die_error(undef, "No file name defined");
2868 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2869 # blobs defined by non-textual hash id's can be cached
2870 $expires = "+1d";
2873 my ($have_blame) = gitweb_check_feature('blame');
2874 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2875 or die_error(undef, "Couldn't cat $file_name, $hash");
2876 my $mimetype = blob_mimetype($fd, $file_name);
2877 if ($mimetype !~ m/^text\//) {
2878 close $fd;
2879 return git_blob_plain($mimetype);
2881 git_header_html(undef, $expires);
2882 my $formats_nav = '';
2883 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2884 if (defined $file_name) {
2885 if ($have_blame) {
2886 $formats_nav .=
2887 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2888 hash=>$hash, file_name=>$file_name)},
2889 "blame") .
2890 " | ";
2892 $formats_nav .=
2893 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2894 hash=>$hash, file_name=>$file_name)},
2895 "history") .
2896 " | " .
2897 $cgi->a({-href => href(action=>"blob_plain",
2898 hash=>$hash, file_name=>$file_name)},
2899 "raw") .
2900 " | " .
2901 $cgi->a({-href => href(action=>"blob",
2902 hash_base=>"HEAD", file_name=>$file_name)},
2903 "HEAD");
2904 } else {
2905 $formats_nav .=
2906 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2908 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2909 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2910 } else {
2911 print "<div class=\"page_nav\">\n" .
2912 "<br/><br/></div>\n" .
2913 "<div class=\"title\">$hash</div>\n";
2915 git_print_page_path($file_name, "blob", $hash_base);
2916 print "<div class=\"page_body\">\n";
2917 my $nr;
2918 while (my $line = <$fd>) {
2919 chomp $line;
2920 $nr++;
2921 $line = untabify($line);
2922 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2923 $nr, $nr, $nr, esc_html($line);
2925 close $fd
2926 or print "Reading blob failed.\n";
2927 print "</div>";
2928 git_footer_html();
2931 sub git_tree {
2932 my $have_snapshot = gitweb_have_snapshot();
2934 if (!defined $hash_base) {
2935 $hash_base = "HEAD";
2937 if (!defined $hash) {
2938 if (defined $file_name) {
2939 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2940 } else {
2941 $hash = $hash_base;
2944 $/ = "\0";
2945 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2946 or die_error(undef, "Open git-ls-tree failed");
2947 my @entries = map { chomp; $_ } <$fd>;
2948 close $fd or die_error(undef, "Reading tree failed");
2949 $/ = "\n";
2951 my $refs = git_get_references();
2952 my $ref = format_ref_marker($refs, $hash_base);
2953 git_header_html();
2954 my $basedir = '';
2955 my ($have_blame) = gitweb_check_feature('blame');
2956 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2957 my @views_nav = ();
2958 if (defined $file_name) {
2959 push @views_nav,
2960 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2961 hash=>$hash, file_name=>$file_name)},
2962 "history"),
2963 $cgi->a({-href => href(action=>"tree",
2964 hash_base=>"HEAD", file_name=>$file_name)},
2965 "HEAD"),
2967 if ($have_snapshot) {
2968 # FIXME: Should be available when we have no hash base as well.
2969 push @views_nav,
2970 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2971 "snapshot");
2973 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2974 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2975 } else {
2976 undef $hash_base;
2977 print "<div class=\"page_nav\">\n";
2978 print "<br/><br/></div>\n";
2979 print "<div class=\"title\">$hash</div>\n";
2981 if (defined $file_name) {
2982 $basedir = $file_name;
2983 if ($basedir ne '' && substr($basedir, -1) ne '/') {
2984 $basedir .= '/';
2987 git_print_page_path($file_name, 'tree', $hash_base);
2988 print "<div class=\"page_body\">\n";
2989 print "<table cellspacing=\"0\">\n";
2990 my $alternate = 1;
2991 # '..' (top directory) link if possible
2992 if (defined $hash_base &&
2993 defined $file_name && $file_name =~ m![^/]+$!) {
2994 if ($alternate) {
2995 print "<tr class=\"dark\">\n";
2996 } else {
2997 print "<tr class=\"light\">\n";
2999 $alternate ^= 1;
3001 my $up = $file_name;
3002 $up =~ s!/?[^/]+$!!;
3003 undef $up unless $up;
3004 # based on git_print_tree_entry
3005 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3006 print '<td class="list">';
3007 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3008 file_name=>$up)},
3009 "..");
3010 print "</td>\n";
3011 print "<td class=\"link\"></td>\n";
3013 print "</tr>\n";
3015 foreach my $line (@entries) {
3016 my %t = parse_ls_tree_line($line, -z => 1);
3018 if ($alternate) {
3019 print "<tr class=\"dark\">\n";
3020 } else {
3021 print "<tr class=\"light\">\n";
3023 $alternate ^= 1;
3025 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3027 print "</tr>\n";
3029 print "</table>\n" .
3030 "</div>";
3031 git_footer_html();
3034 sub git_snapshot {
3035 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3036 my $have_snapshot = (defined $ctype && defined $suffix);
3037 if (!$have_snapshot) {
3038 die_error('403 Permission denied', "Permission denied");
3041 if (!defined $hash) {
3042 $hash = git_get_head_hash($project);
3045 my $filename = basename($project) . "-$hash.tar.$suffix";
3047 print $cgi->header(
3048 -type => 'application/x-tar',
3049 -content_encoding => $ctype,
3050 -content_disposition => 'inline; filename="' . "$filename" . '"',
3051 -status => '200 OK');
3053 my $git = git_cmd_str();
3054 my $name = $project;
3055 $name =~ s/\047/\047\\\047\047/g;
3056 open my $fd, "-|",
3057 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3058 or die_error(undef, "Execute git-tar-tree failed.");
3059 binmode STDOUT, ':raw';
3060 print <$fd>;
3061 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3062 close $fd;
3066 sub git_log {
3067 my $head = git_get_head_hash($project);
3068 if (!defined $hash) {
3069 $hash = $head;
3071 if (!defined $page) {
3072 $page = 0;
3074 my $refs = git_get_references();
3076 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3077 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash, "--"
3078 or die_error(undef, "Open git-rev-list failed");
3079 my @revlist = map { chomp; $_ } <$fd>;
3080 close $fd;
3082 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
3084 git_header_html();
3085 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3087 if (!@revlist) {
3088 my %co = parse_commit($hash);
3090 git_print_header_div('summary', $project);
3091 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3093 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
3094 my $commit = $revlist[$i];
3095 my $ref = format_ref_marker($refs, $commit);
3096 my %co = parse_commit($commit);
3097 next if !%co;
3098 my %ad = parse_date($co{'author_epoch'});
3099 git_print_header_div('commit',
3100 "<span class=\"age\">$co{'age_string'}</span>" .
3101 esc_html($co{'title'}) . $ref,
3102 $commit);
3103 print "<div class=\"title_text\">\n" .
3104 "<div class=\"log_link\">\n" .
3105 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3106 " | " .
3107 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3108 " | " .
3109 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3110 "<br/>\n" .
3111 "</div>\n" .
3112 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3113 "</div>\n";
3115 print "<div class=\"log_body\">\n";
3116 git_print_log($co{'comment'}, -final_empty_line=> 1);
3117 print "</div>\n";
3119 git_footer_html();
3122 sub git_commit {
3123 my %co = parse_commit($hash);
3124 if (!%co) {
3125 die_error(undef, "Unknown commit object");
3127 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3128 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3130 my $parent = $co{'parent'};
3131 if (!defined $parent) {
3132 $parent = "--root";
3134 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
3135 @diff_opts, $parent, $hash, "--"
3136 or die_error(undef, "Open git-diff-tree failed");
3137 my @difftree = map { chomp; $_ } <$fd>;
3138 close $fd or die_error(undef, "Reading git-diff-tree failed");
3140 # non-textual hash id's can be cached
3141 my $expires;
3142 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3143 $expires = "+1d";
3145 my $refs = git_get_references();
3146 my $ref = format_ref_marker($refs, $co{'id'});
3148 my $have_snapshot = gitweb_have_snapshot();
3150 my @views_nav = ();
3151 if (defined $file_name && defined $co{'parent'}) {
3152 push @views_nav,
3153 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3154 "blame");
3156 git_header_html(undef, $expires);
3157 git_print_page_nav('commit', '',
3158 $hash, $co{'tree'}, $hash,
3159 join (' | ', @views_nav));
3161 if (defined $co{'parent'}) {
3162 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3163 } else {
3164 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3166 print "<div class=\"title_text\">\n" .
3167 "<table cellspacing=\"0\">\n";
3168 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3169 "<tr>" .
3170 "<td></td><td> $ad{'rfc2822'}";
3171 if ($ad{'hour_local'} < 6) {
3172 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3173 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3174 } else {
3175 printf(" (%02d:%02d %s)",
3176 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3178 print "</td>" .
3179 "</tr>\n";
3180 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3181 print "<tr><td></td><td> $cd{'rfc2822'}" .
3182 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3183 "</td></tr>\n";
3184 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3185 print "<tr>" .
3186 "<td>tree</td>" .
3187 "<td class=\"sha1\">" .
3188 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3189 class => "list"}, $co{'tree'}) .
3190 "</td>" .
3191 "<td class=\"link\">" .
3192 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3193 "tree");
3194 if ($have_snapshot) {
3195 print " | " .
3196 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3198 print "</td>" .
3199 "</tr>\n";
3200 my $parents = $co{'parents'};
3201 foreach my $par (@$parents) {
3202 print "<tr>" .
3203 "<td>parent</td>" .
3204 "<td class=\"sha1\">" .
3205 $cgi->a({-href => href(action=>"commit", hash=>$par),
3206 class => "list"}, $par) .
3207 "</td>" .
3208 "<td class=\"link\">" .
3209 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3210 " | " .
3211 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3212 "</td>" .
3213 "</tr>\n";
3215 print "</table>".
3216 "</div>\n";
3218 print "<div class=\"page_body\">\n";
3219 git_print_log($co{'comment'});
3220 print "</div>\n";
3222 git_difftree_body(\@difftree, $hash, $parent);
3224 git_footer_html();
3227 sub git_blobdiff {
3228 my $format = shift || 'html';
3230 my $fd;
3231 my @difftree;
3232 my %diffinfo;
3233 my $expires;
3235 # preparing $fd and %diffinfo for git_patchset_body
3236 # new style URI
3237 if (defined $hash_base && defined $hash_parent_base) {
3238 if (defined $file_name) {
3239 # read raw output
3240 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3241 $hash_parent_base, $hash_base,
3242 "--", $file_name
3243 or die_error(undef, "Open git-diff-tree failed");
3244 @difftree = map { chomp; $_ } <$fd>;
3245 close $fd
3246 or die_error(undef, "Reading git-diff-tree failed");
3247 @difftree
3248 or die_error('404 Not Found', "Blob diff not found");
3250 } elsif (defined $hash &&
3251 $hash =~ /[0-9a-fA-F]{40}/) {
3252 # try to find filename from $hash
3254 # read filtered raw output
3255 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3256 $hash_parent_base, $hash_base, "--"
3257 or die_error(undef, "Open git-diff-tree failed");
3258 @difftree =
3259 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3260 # $hash == to_id
3261 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3262 map { chomp; $_ } <$fd>;
3263 close $fd
3264 or die_error(undef, "Reading git-diff-tree failed");
3265 @difftree
3266 or die_error('404 Not Found', "Blob diff not found");
3268 } else {
3269 die_error('404 Not Found', "Missing one of the blob diff parameters");
3272 if (@difftree > 1) {
3273 die_error('404 Not Found', "Ambiguous blob diff specification");
3276 %diffinfo = parse_difftree_raw_line($difftree[0]);
3277 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3278 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3280 $hash_parent ||= $diffinfo{'from_id'};
3281 $hash ||= $diffinfo{'to_id'};
3283 # non-textual hash id's can be cached
3284 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3285 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3286 $expires = '+1d';
3289 # open patch output
3290 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3291 '-p', $hash_parent_base, $hash_base,
3292 "--", $file_name
3293 or die_error(undef, "Open git-diff-tree failed");
3296 # old/legacy style URI
3297 if (!%diffinfo && # if new style URI failed
3298 defined $hash && defined $hash_parent) {
3299 # fake git-diff-tree raw output
3300 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3301 $diffinfo{'from_id'} = $hash_parent;
3302 $diffinfo{'to_id'} = $hash;
3303 if (defined $file_name) {
3304 if (defined $file_parent) {
3305 $diffinfo{'status'} = '2';
3306 $diffinfo{'from_file'} = $file_parent;
3307 $diffinfo{'to_file'} = $file_name;
3308 } else { # assume not renamed
3309 $diffinfo{'status'} = '1';
3310 $diffinfo{'from_file'} = $file_name;
3311 $diffinfo{'to_file'} = $file_name;
3313 } else { # no filename given
3314 $diffinfo{'status'} = '2';
3315 $diffinfo{'from_file'} = $hash_parent;
3316 $diffinfo{'to_file'} = $hash;
3319 # non-textual hash id's can be cached
3320 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3321 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3322 $expires = '+1d';
3325 # open patch output
3326 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts,
3327 $hash_parent, $hash, "--"
3328 or die_error(undef, "Open git-diff failed");
3329 } else {
3330 die_error('404 Not Found', "Missing one of the blob diff parameters")
3331 unless %diffinfo;
3334 # header
3335 if ($format eq 'html') {
3336 my $formats_nav =
3337 $cgi->a({-href => href(action=>"blobdiff_plain",
3338 hash=>$hash, hash_parent=>$hash_parent,
3339 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3340 file_name=>$file_name, file_parent=>$file_parent)},
3341 "raw");
3342 git_header_html(undef, $expires);
3343 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3344 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3345 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3346 } else {
3347 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3348 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3350 if (defined $file_name) {
3351 git_print_page_path($file_name, "blob", $hash_base);
3352 } else {
3353 print "<div class=\"page_path\"></div>\n";
3356 } elsif ($format eq 'plain') {
3357 print $cgi->header(
3358 -type => 'text/plain',
3359 -charset => 'utf-8',
3360 -expires => $expires,
3361 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3363 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3365 } else {
3366 die_error(undef, "Unknown blobdiff format");
3369 # patch
3370 if ($format eq 'html') {
3371 print "<div class=\"page_body\">\n";
3373 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3374 close $fd;
3376 print "</div>\n"; # class="page_body"
3377 git_footer_html();
3379 } else {
3380 while (my $line = <$fd>) {
3381 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3382 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3384 print $line;
3386 last if $line =~ m!^\+\+\+!;
3388 local $/ = undef;
3389 print <$fd>;
3390 close $fd;
3394 sub git_blobdiff_plain {
3395 git_blobdiff('plain');
3398 sub git_commitdiff {
3399 my $format = shift || 'html';
3400 my %co = parse_commit($hash);
3401 if (!%co) {
3402 die_error(undef, "Unknown commit object");
3405 # we need to prepare $formats_nav before any parameter munging
3406 my $formats_nav;
3407 if ($format eq 'html') {
3408 $formats_nav =
3409 $cgi->a({-href => href(action=>"commitdiff_plain",
3410 hash=>$hash, hash_parent=>$hash_parent)},
3411 "raw");
3413 if (defined $hash_parent) {
3414 # commitdiff with two commits given
3415 my $hash_parent_short = $hash_parent;
3416 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3417 $hash_parent_short = substr($hash_parent, 0, 7);
3419 $formats_nav .=
3420 ' (from: ' .
3421 $cgi->a({-href => href(action=>"commitdiff",
3422 hash=>$hash_parent)},
3423 esc_html($hash_parent_short)) .
3424 ')';
3425 } elsif (!$co{'parent'}) {
3426 # --root commitdiff
3427 $formats_nav .= ' (initial)';
3428 } elsif (scalar @{$co{'parents'}} == 1) {
3429 # single parent commit
3430 $formats_nav .=
3431 ' (parent: ' .
3432 $cgi->a({-href => href(action=>"commitdiff",
3433 hash=>$co{'parent'})},
3434 esc_html(substr($co{'parent'}, 0, 7))) .
3435 ')';
3436 } else {
3437 # merge commit
3438 $formats_nav .=
3439 ' (merge: ' .
3440 join(' ', map {
3441 $cgi->a({-href => href(action=>"commitdiff",
3442 hash=>$_)},
3443 esc_html(substr($_, 0, 7)));
3444 } @{$co{'parents'}} ) .
3445 ')';
3449 if (!defined $hash_parent) {
3450 $hash_parent = $co{'parent'} || '--root';
3453 # read commitdiff
3454 my $fd;
3455 my @difftree;
3456 if ($format eq 'html') {
3457 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3458 "--no-commit-id", "--patch-with-raw", "--full-index",
3459 $hash_parent, $hash, "--"
3460 or die_error(undef, "Open git-diff-tree failed");
3462 while (chomp(my $line = <$fd>)) {
3463 # empty line ends raw part of diff-tree output
3464 last unless $line;
3465 push @difftree, $line;
3468 } elsif ($format eq 'plain') {
3469 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3470 '-p', $hash_parent, $hash, "--"
3471 or die_error(undef, "Open git-diff-tree failed");
3473 } else {
3474 die_error(undef, "Unknown commitdiff format");
3477 # non-textual hash id's can be cached
3478 my $expires;
3479 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3480 $expires = "+1d";
3483 # write commit message
3484 if ($format eq 'html') {
3485 my $refs = git_get_references();
3486 my $ref = format_ref_marker($refs, $co{'id'});
3488 git_header_html(undef, $expires);
3489 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3490 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3491 git_print_authorship(\%co);
3492 print "<div class=\"page_body\">\n";
3493 if (@{$co{'comment'}} > 1) {
3494 print "<div class=\"log\">\n";
3495 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
3496 print "</div>\n"; # class="log"
3499 } elsif ($format eq 'plain') {
3500 my $refs = git_get_references("tags");
3501 my $tagname = git_get_rev_name_tags($hash);
3502 my $filename = basename($project) . "-$hash.patch";
3504 print $cgi->header(
3505 -type => 'text/plain',
3506 -charset => 'utf-8',
3507 -expires => $expires,
3508 -content_disposition => 'inline; filename="' . "$filename" . '"');
3509 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3510 print <<TEXT;
3511 From: $co{'author'}
3512 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3513 Subject: $co{'title'}
3514 TEXT
3515 print "X-Git-Tag: $tagname\n" if $tagname;
3516 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3518 foreach my $line (@{$co{'comment'}}) {
3519 print "$line\n";
3521 print "---\n\n";
3524 # write patch
3525 if ($format eq 'html') {
3526 git_difftree_body(\@difftree, $hash, $hash_parent);
3527 print "<br/>\n";
3529 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3530 close $fd;
3531 print "</div>\n"; # class="page_body"
3532 git_footer_html();
3534 } elsif ($format eq 'plain') {
3535 local $/ = undef;
3536 print <$fd>;
3537 close $fd
3538 or print "Reading git-diff-tree failed\n";
3542 sub git_commitdiff_plain {
3543 git_commitdiff('plain');
3546 sub git_history {
3547 if (!defined $hash_base) {
3548 $hash_base = git_get_head_hash($project);
3550 if (!defined $page) {
3551 $page = 0;
3553 my $ftype;
3554 my %co = parse_commit($hash_base);
3555 if (!%co) {
3556 die_error(undef, "Unknown commit object");
3559 my $refs = git_get_references();
3560 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3562 if (!defined $hash && defined $file_name) {
3563 $hash = git_get_hash_by_path($hash_base, $file_name);
3565 if (defined $hash) {
3566 $ftype = git_get_type($hash);
3569 open my $fd, "-|",
3570 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3571 or die_error(undef, "Open git-rev-list-failed");
3572 my @revlist = map { chomp; $_ } <$fd>;
3573 close $fd
3574 or die_error(undef, "Reading git-rev-list failed");
3576 my $paging_nav = '';
3577 if ($page > 0) {
3578 $paging_nav .=
3579 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3580 file_name=>$file_name)},
3581 "first");
3582 $paging_nav .= " &sdot; " .
3583 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3584 file_name=>$file_name, page=>$page-1),
3585 -accesskey => "p", -title => "Alt-p"}, "prev");
3586 } else {
3587 $paging_nav .= "first";
3588 $paging_nav .= " &sdot; prev";
3590 if ($#revlist >= (100 * ($page+1)-1)) {
3591 $paging_nav .= " &sdot; " .
3592 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3593 file_name=>$file_name, page=>$page+1),
3594 -accesskey => "n", -title => "Alt-n"}, "next");
3595 } else {
3596 $paging_nav .= " &sdot; next";
3598 my $next_link = '';
3599 if ($#revlist >= (100 * ($page+1)-1)) {
3600 $next_link =
3601 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3602 file_name=>$file_name, page=>$page+1),
3603 -title => "Alt-n"}, "next");
3606 git_header_html();
3607 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3608 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3609 git_print_page_path($file_name, $ftype, $hash_base);
3611 git_history_body(\@revlist, ($page * 100), $#revlist,
3612 $refs, $hash_base, $ftype, $next_link);
3614 git_footer_html();
3617 sub git_search {
3618 if (!defined $searchtext) {
3619 die_error(undef, "Text field empty");
3621 if (!defined $hash) {
3622 $hash = git_get_head_hash($project);
3624 my %co = parse_commit($hash);
3625 if (!%co) {
3626 die_error(undef, "Unknown commit object");
3629 $searchtype ||= 'commit';
3630 if ($searchtype eq 'pickaxe') {
3631 # pickaxe may take all resources of your box and run for several minutes
3632 # with every query - so decide by yourself how public you make this feature
3633 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3634 if (!$have_pickaxe) {
3635 die_error('403 Permission denied', "Permission denied");
3639 git_header_html();
3640 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3641 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3643 print "<table cellspacing=\"0\">\n";
3644 my $alternate = 1;
3645 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
3646 $/ = "\0";
3647 open my $fd, "-|", git_cmd(), "rev-list",
3648 "--header", "--parents", $hash, "--"
3649 or next;
3650 while (my $commit_text = <$fd>) {
3651 if (!grep m/$searchtext/i, $commit_text) {
3652 next;
3654 if ($searchtype eq 'author' && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3655 next;
3657 if ($searchtype eq 'committer' && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3658 next;
3660 my @commit_lines = split "\n", $commit_text;
3661 my %co = parse_commit(undef, \@commit_lines);
3662 if (!%co) {
3663 next;
3665 if ($alternate) {
3666 print "<tr class=\"dark\">\n";
3667 } else {
3668 print "<tr class=\"light\">\n";
3670 $alternate ^= 1;
3671 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3672 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3673 "<td>" .
3674 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3675 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3676 my $comment = $co{'comment'};
3677 foreach my $line (@$comment) {
3678 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3679 my $lead = esc_html($1) || "";
3680 $lead = chop_str($lead, 30, 10);
3681 my $match = esc_html($2) || "";
3682 my $trail = esc_html($3) || "";
3683 $trail = chop_str($trail, 30, 10);
3684 my $text = "$lead<span class=\"match\">$match</span>$trail";
3685 print chop_str($text, 80, 5) . "<br/>\n";
3688 print "</td>\n" .
3689 "<td class=\"link\">" .
3690 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3691 " | " .
3692 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3693 print "</td>\n" .
3694 "</tr>\n";
3696 close $fd;
3699 if ($searchtype eq 'pickaxe') {
3700 $/ = "\n";
3701 my $git_command = git_cmd_str();
3702 open my $fd, "-|", "$git_command rev-list $hash | " .
3703 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3704 undef %co;
3705 my @files;
3706 while (my $line = <$fd>) {
3707 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3708 my %set;
3709 $set{'file'} = $6;
3710 $set{'from_id'} = $3;
3711 $set{'to_id'} = $4;
3712 $set{'id'} = $set{'to_id'};
3713 if ($set{'id'} =~ m/0{40}/) {
3714 $set{'id'} = $set{'from_id'};
3716 if ($set{'id'} =~ m/0{40}/) {
3717 next;
3719 push @files, \%set;
3720 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3721 if (%co) {
3722 if ($alternate) {
3723 print "<tr class=\"dark\">\n";
3724 } else {
3725 print "<tr class=\"light\">\n";
3727 $alternate ^= 1;
3728 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3729 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3730 "<td>" .
3731 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3732 -class => "list subject"},
3733 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3734 while (my $setref = shift @files) {
3735 my %set = %$setref;
3736 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3737 hash=>$set{'id'}, file_name=>$set{'file'}),
3738 -class => "list"},
3739 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3740 "<br/>\n";
3742 print "</td>\n" .
3743 "<td class=\"link\">" .
3744 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3745 " | " .
3746 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3747 print "</td>\n" .
3748 "</tr>\n";
3750 %co = parse_commit($1);
3753 close $fd;
3755 print "</table>\n";
3756 git_footer_html();
3759 sub git_search_help {
3760 git_header_html();
3761 git_print_page_nav('','', $hash,$hash,$hash);
3762 print <<EOT;
3763 <dl>
3764 <dt><b>commit</b></dt>
3765 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
3766 <dt><b>author</b></dt>
3767 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
3768 <dt><b>committer</b></dt>
3769 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
3771 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3772 if ($have_pickaxe) {
3773 print <<EOT;
3774 <dt><b>pickaxe</b></dt>
3775 <dd>All commits that caused the string to appear or disappear from any file (changes that
3776 added, removed or "modified" the string) will be listed. This search can take a while and
3777 takes a lot of strain on the server, so please use it wisely.</dd>
3780 print "</dl>\n";
3781 git_footer_html();
3784 sub git_shortlog {
3785 my $head = git_get_head_hash($project);
3786 if (!defined $hash) {
3787 $hash = $head;
3789 if (!defined $page) {
3790 $page = 0;
3792 my $refs = git_get_references();
3794 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3795 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash, "--"
3796 or die_error(undef, "Open git-rev-list failed");
3797 my @revlist = map { chomp; $_ } <$fd>;
3798 close $fd;
3800 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3801 my $next_link = '';
3802 if ($#revlist >= (100 * ($page+1)-1)) {
3803 $next_link =
3804 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3805 -title => "Alt-n"}, "next");
3809 git_header_html();
3810 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3811 git_print_header_div('summary', $project);
3813 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3815 git_footer_html();
3818 ## ......................................................................
3819 ## feeds (RSS, OPML)
3821 sub git_rss {
3822 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3823 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150",
3824 git_get_head_hash($project), "--"
3825 or die_error(undef, "Open git-rev-list failed");
3826 my @revlist = map { chomp; $_ } <$fd>;
3827 close $fd or die_error(undef, "Reading git-rev-list failed");
3828 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3829 print <<XML;
3830 <?xml version="1.0" encoding="utf-8"?>
3831 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3832 <channel>
3833 <title>$project $my_uri $my_url</title>
3834 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3835 <description>$project log</description>
3836 <language>en</language>
3839 for (my $i = 0; $i <= $#revlist; $i++) {
3840 my $commit = $revlist[$i];
3841 my %co = parse_commit($commit);
3842 # we read 150, we always show 30 and the ones more recent than 48 hours
3843 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3844 last;
3846 my %cd = parse_date($co{'committer_epoch'});
3847 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3848 $co{'parent'}, $co{'id'}, "--"
3849 or next;
3850 my @difftree = map { chomp; $_ } <$fd>;
3851 close $fd
3852 or next;
3853 print "<item>\n" .
3854 "<title>" .
3855 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3856 "</title>\n" .
3857 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3858 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3859 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3860 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3861 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3862 "<content:encoded>" .
3863 "<![CDATA[\n";
3864 my $comment = $co{'comment'};
3865 foreach my $line (@$comment) {
3866 $line = to_utf8($line);
3867 print "$line<br/>\n";
3869 print "<br/>\n";
3870 foreach my $line (@difftree) {
3871 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3872 next;
3874 my $file = esc_html(unquote($7));
3875 $file = to_utf8($file);
3876 print "$file<br/>\n";
3878 print "]]>\n" .
3879 "</content:encoded>\n" .
3880 "</item>\n";
3882 print "</channel></rss>";
3885 sub git_opml {
3886 my @list = git_get_projects_list();
3888 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3889 print <<XML;
3890 <?xml version="1.0" encoding="utf-8"?>
3891 <opml version="1.0">
3892 <head>
3893 <title>$site_name OPML Export</title>
3894 </head>
3895 <body>
3896 <outline text="git RSS feeds">
3899 foreach my $pr (@list) {
3900 my %proj = %$pr;
3901 my $head = git_get_head_hash($proj{'path'});
3902 if (!defined $head) {
3903 next;
3905 $git_dir = "$projectroot/$proj{'path'}";
3906 my %co = parse_commit($head);
3907 if (!%co) {
3908 next;
3911 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3912 my $rss = "$my_url?p=$proj{'path'};a=rss";
3913 my $html = "$my_url?p=$proj{'path'};a=summary";
3914 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3916 print <<XML;
3917 </outline>
3918 </body>
3919 </opml>