gitweb: New improved patchset view (WIP !!!)
[git/jnareb-git.git] / gitweb / gitweb.perl
blob29fa73bebe4ae7b6806e56ed77c58c645929fba5
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 # quote unsafe characters (TODO) and escape filename to HTML
567 sub esc_path {
568 my $str = shift;
569 return esc_html($str);
572 # git may return quoted and escaped filenames
573 sub unquote {
574 my $str = shift;
575 if ($str =~ m/^"(.*)"$/) {
576 $str = $1;
577 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
579 return $str;
582 # escape tabs (convert tabs to spaces)
583 sub untabify {
584 my $line = shift;
586 while ((my $pos = index($line, "\t")) != -1) {
587 if (my $count = (8 - ($pos % 8))) {
588 my $spaces = ' ' x $count;
589 $line =~ s/\t/$spaces/;
593 return $line;
596 sub project_in_list {
597 my $project = shift;
598 my @list = git_get_projects_list();
599 return @list && scalar(grep { $_->{'path'} eq $project } @list);
602 ## ----------------------------------------------------------------------
603 ## HTML aware string manipulation
605 sub chop_str {
606 my $str = shift;
607 my $len = shift;
608 my $add_len = shift || 10;
610 # allow only $len chars, but don't cut a word if it would fit in $add_len
611 # if it doesn't fit, cut it if it's still longer than the dots we would add
612 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
613 my $body = $1;
614 my $tail = $2;
615 if (length($tail) > 4) {
616 $tail = " ...";
617 $body =~ s/&[^;]*$//; # remove chopped character entities
619 return "$body$tail";
622 ## ----------------------------------------------------------------------
623 ## functions returning short strings
625 # CSS class for given age value (in seconds)
626 sub age_class {
627 my $age = shift;
629 if ($age < 60*60*2) {
630 return "age0";
631 } elsif ($age < 60*60*24*2) {
632 return "age1";
633 } else {
634 return "age2";
638 # convert age in seconds to "nn units ago" string
639 sub age_string {
640 my $age = shift;
641 my $age_str;
643 if ($age > 60*60*24*365*2) {
644 $age_str = (int $age/60/60/24/365);
645 $age_str .= " years ago";
646 } elsif ($age > 60*60*24*(365/12)*2) {
647 $age_str = int $age/60/60/24/(365/12);
648 $age_str .= " months ago";
649 } elsif ($age > 60*60*24*7*2) {
650 $age_str = int $age/60/60/24/7;
651 $age_str .= " weeks ago";
652 } elsif ($age > 60*60*24*2) {
653 $age_str = int $age/60/60/24;
654 $age_str .= " days ago";
655 } elsif ($age > 60*60*2) {
656 $age_str = int $age/60/60;
657 $age_str .= " hours ago";
658 } elsif ($age > 60*2) {
659 $age_str = int $age/60;
660 $age_str .= " min ago";
661 } elsif ($age > 2) {
662 $age_str = int $age;
663 $age_str .= " sec ago";
664 } else {
665 $age_str .= " right now";
667 return $age_str;
670 # convert file mode in octal to symbolic file mode string
671 sub mode_str {
672 my $mode = oct shift;
674 if (S_ISDIR($mode & S_IFMT)) {
675 return 'drwxr-xr-x';
676 } elsif (S_ISLNK($mode)) {
677 return 'lrwxrwxrwx';
678 } elsif (S_ISREG($mode)) {
679 # git cares only about the executable bit
680 if ($mode & S_IXUSR) {
681 return '-rwxr-xr-x';
682 } else {
683 return '-rw-r--r--';
685 } else {
686 return '----------';
690 # convert file mode in octal to file type string
691 sub file_type {
692 my $mode = shift;
694 if ($mode !~ m/^[0-7]+$/) {
695 return $mode;
696 } else {
697 $mode = oct $mode;
700 if (S_ISDIR($mode & S_IFMT)) {
701 return "directory";
702 } elsif (S_ISLNK($mode)) {
703 return "symlink";
704 } elsif (S_ISREG($mode)) {
705 return "file";
706 } else {
707 return "unknown";
711 ## ----------------------------------------------------------------------
712 ## functions returning short HTML fragments, or transforming HTML fragments
713 ## which don't beling to other sections
715 # format line of commit message or tag comment
716 sub format_log_line_html {
717 my $line = shift;
719 $line = esc_html($line);
720 $line =~ s/ /&nbsp;/g;
721 if ($line =~ m/([0-9a-fA-F]{40})/) {
722 my $hash_text = $1;
723 if (git_get_type($hash_text) eq "commit") {
724 my $link =
725 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
726 -class => "text"}, $hash_text);
727 $line =~ s/$hash_text/$link/;
730 return $line;
733 # format marker of refs pointing to given object
734 sub format_ref_marker {
735 my ($refs, $id) = @_;
736 my $markers = '';
738 if (defined $refs->{$id}) {
739 foreach my $ref (@{$refs->{$id}}) {
740 my ($type, $name) = qw();
741 # e.g. tags/v2.6.11 or heads/next
742 if ($ref =~ m!^(.*?)s?/(.*)$!) {
743 $type = $1;
744 $name = $2;
745 } else {
746 $type = "ref";
747 $name = $ref;
750 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
754 if ($markers) {
755 return ' <span class="refs">'. $markers . '</span>';
756 } else {
757 return "";
761 # format, perhaps shortened and with markers, title line
762 sub format_subject_html {
763 my ($long, $short, $href, $extra) = @_;
764 $extra = '' unless defined($extra);
766 if (length($short) < length($long)) {
767 return $cgi->a({-href => $href, -class => "list subject",
768 -title => to_utf8($long)},
769 esc_html($short) . $extra);
770 } else {
771 return $cgi->a({-href => $href, -class => "list subject"},
772 esc_html($long) . $extra);
776 sub format_diff_line {
777 my $line = shift;
778 my $char = substr($line, 0, 1);
779 my $diff_class = "";
781 chomp $line;
783 if ($char eq '+') {
784 $diff_class = " add";
785 } elsif ($char eq "-") {
786 $diff_class = " rem";
787 } elsif ($char eq "@") {
788 $diff_class = " chunk_header";
789 } elsif ($char eq "\\") {
790 $diff_class = " incomplete";
792 $line = untabify($line);
793 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
796 ## ----------------------------------------------------------------------
797 ## git utility subroutines, invoking git commands
799 # returns path to the core git executable and the --git-dir parameter as list
800 sub git_cmd {
801 return $GIT, '--git-dir='.$git_dir;
804 # returns path to the core git executable and the --git-dir parameter as string
805 sub git_cmd_str {
806 return join(' ', git_cmd());
809 # get HEAD ref of given project as hash
810 sub git_get_head_hash {
811 my $project = shift;
812 my $o_git_dir = $git_dir;
813 my $retval = undef;
814 $git_dir = "$projectroot/$project";
815 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
816 my $head = <$fd>;
817 close $fd;
818 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
819 $retval = $1;
822 if (defined $o_git_dir) {
823 $git_dir = $o_git_dir;
825 return $retval;
828 # get type of given object
829 sub git_get_type {
830 my $hash = shift;
832 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
833 my $type = <$fd>;
834 close $fd or return;
835 chomp $type;
836 return $type;
839 sub git_get_project_config {
840 my ($key, $type) = @_;
842 return unless ($key);
843 $key =~ s/^gitweb\.//;
844 return if ($key =~ m/\W/);
846 my @x = (git_cmd(), 'repo-config');
847 if (defined $type) { push @x, $type; }
848 push @x, "--get";
849 push @x, "gitweb.$key";
850 my $val = qx(@x);
851 chomp $val;
852 return ($val);
855 # get hash of given path at given ref
856 sub git_get_hash_by_path {
857 my $base = shift;
858 my $path = shift || return undef;
859 my $type = shift;
861 $path =~ s,/+$,,;
863 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
864 or die_error(undef, "Open git-ls-tree failed");
865 my $line = <$fd>;
866 close $fd or return undef;
868 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
869 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
870 if (defined $type && $type ne $2) {
871 # type doesn't match
872 return undef;
874 return $3;
877 ## ......................................................................
878 ## git utility functions, directly accessing git repository
880 sub git_get_project_description {
881 my $path = shift;
883 open my $fd, "$projectroot/$path/description" or return undef;
884 my $descr = <$fd>;
885 close $fd;
886 chomp $descr;
887 return $descr;
890 sub git_get_project_url_list {
891 my $path = shift;
893 open my $fd, "$projectroot/$path/cloneurl" or return;
894 my @git_project_url_list = map { chomp; $_ } <$fd>;
895 close $fd;
897 return wantarray ? @git_project_url_list : \@git_project_url_list;
900 sub git_get_projects_list {
901 my @list;
903 if (-d $projects_list) {
904 # search in directory
905 my $dir = $projects_list;
906 my $pfxlen = length("$dir");
908 File::Find::find({
909 follow_fast => 1, # follow symbolic links
910 dangling_symlinks => 0, # ignore dangling symlinks, silently
911 wanted => sub {
912 # skip project-list toplevel, if we get it.
913 return if (m!^[/.]$!);
914 # only directories can be git repositories
915 return unless (-d $_);
917 my $subdir = substr($File::Find::name, $pfxlen + 1);
918 # we check related file in $projectroot
919 if (check_export_ok("$projectroot/$subdir")) {
920 push @list, { path => $subdir };
921 $File::Find::prune = 1;
924 }, "$dir");
926 } elsif (-f $projects_list) {
927 # read from file(url-encoded):
928 # 'git%2Fgit.git Linus+Torvalds'
929 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
930 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
931 open my ($fd), $projects_list or return;
932 while (my $line = <$fd>) {
933 chomp $line;
934 my ($path, $owner) = split ' ', $line;
935 $path = unescape($path);
936 $owner = unescape($owner);
937 if (!defined $path) {
938 next;
940 if (check_export_ok("$projectroot/$path")) {
941 my $pr = {
942 path => $path,
943 owner => to_utf8($owner),
945 push @list, $pr
948 close $fd;
950 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
951 return @list;
954 sub git_get_project_owner {
955 my $project = shift;
956 my $owner;
958 return undef unless $project;
960 # read from file (url-encoded):
961 # 'git%2Fgit.git Linus+Torvalds'
962 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
963 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
964 if (-f $projects_list) {
965 open (my $fd , $projects_list);
966 while (my $line = <$fd>) {
967 chomp $line;
968 my ($pr, $ow) = split ' ', $line;
969 $pr = unescape($pr);
970 $ow = unescape($ow);
971 if ($pr eq $project) {
972 $owner = to_utf8($ow);
973 last;
976 close $fd;
978 if (!defined $owner) {
979 $owner = get_file_owner("$projectroot/$project");
982 return $owner;
985 sub git_get_last_activity {
986 my ($path) = @_;
987 my $fd;
989 $git_dir = "$projectroot/$path";
990 open($fd, "-|", git_cmd(), 'for-each-ref',
991 '--format=%(refname) %(committer)',
992 '--sort=-committerdate',
993 'refs/heads') or return;
994 my $most_recent = <$fd>;
995 close $fd or return;
996 if ($most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
997 my $timestamp = $1;
998 my $age = time - $timestamp;
999 return ($age, age_string($age));
1003 sub git_get_references {
1004 my $type = shift || "";
1005 my %refs;
1006 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1007 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1008 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1009 or return;
1011 while (my $line = <$fd>) {
1012 chomp $line;
1013 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
1014 if (defined $refs{$1}) {
1015 push @{$refs{$1}}, $2;
1016 } else {
1017 $refs{$1} = [ $2 ];
1021 close $fd or return;
1022 return \%refs;
1025 sub git_get_rev_name_tags {
1026 my $hash = shift || return undef;
1028 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1029 or return;
1030 my $name_rev = <$fd>;
1031 close $fd;
1033 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1034 return $1;
1035 } else {
1036 # catches also '$hash undefined' output
1037 return undef;
1041 ## ----------------------------------------------------------------------
1042 ## parse to hash functions
1044 sub parse_date {
1045 my $epoch = shift;
1046 my $tz = shift || "-0000";
1048 my %date;
1049 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1050 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1051 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1052 $date{'hour'} = $hour;
1053 $date{'minute'} = $min;
1054 $date{'mday'} = $mday;
1055 $date{'day'} = $days[$wday];
1056 $date{'month'} = $months[$mon];
1057 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1058 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1059 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1060 $mday, $months[$mon], $hour ,$min;
1062 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1063 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1064 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1065 $date{'hour_local'} = $hour;
1066 $date{'minute_local'} = $min;
1067 $date{'tz_local'} = $tz;
1068 $date{'iso-tz'} = sprintf ("%04d-%02d-%02d %02d:%02d:%02d %s",
1069 1900+$year, $mon+1, $mday,
1070 $hour, $min, $sec, $tz);
1071 return %date;
1074 sub parse_tag {
1075 my $tag_id = shift;
1076 my %tag;
1077 my @comment;
1079 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1080 $tag{'id'} = $tag_id;
1081 while (my $line = <$fd>) {
1082 chomp $line;
1083 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1084 $tag{'object'} = $1;
1085 } elsif ($line =~ m/^type (.+)$/) {
1086 $tag{'type'} = $1;
1087 } elsif ($line =~ m/^tag (.+)$/) {
1088 $tag{'name'} = $1;
1089 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1090 $tag{'author'} = $1;
1091 $tag{'epoch'} = $2;
1092 $tag{'tz'} = $3;
1093 } elsif ($line =~ m/--BEGIN/) {
1094 push @comment, $line;
1095 last;
1096 } elsif ($line eq "") {
1097 last;
1100 push @comment, <$fd>;
1101 $tag{'comment'} = \@comment;
1102 close $fd or return;
1103 if (!defined $tag{'name'}) {
1104 return
1106 return %tag
1109 sub parse_commit {
1110 my $commit_id = shift;
1111 my $commit_text = shift;
1113 my @commit_lines;
1114 my %co;
1116 if (defined $commit_text) {
1117 @commit_lines = @$commit_text;
1118 } else {
1119 local $/ = "\0";
1120 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
1121 or return;
1122 @commit_lines = split '\n', <$fd>;
1123 close $fd or return;
1124 pop @commit_lines;
1126 my $header = shift @commit_lines;
1127 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1128 return;
1130 ($co{'id'}, my @parents) = split ' ', $header;
1131 $co{'parents'} = \@parents;
1132 $co{'parent'} = $parents[0];
1133 while (my $line = shift @commit_lines) {
1134 last if $line eq "\n";
1135 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1136 $co{'tree'} = $1;
1137 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1138 $co{'author'} = $1;
1139 $co{'author_epoch'} = $2;
1140 $co{'author_tz'} = $3;
1141 if ($co{'author'} =~ m/^([^<]+) </) {
1142 $co{'author_name'} = $1;
1143 } else {
1144 $co{'author_name'} = $co{'author'};
1146 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1147 $co{'committer'} = $1;
1148 $co{'committer_epoch'} = $2;
1149 $co{'committer_tz'} = $3;
1150 $co{'committer_name'} = $co{'committer'};
1151 $co{'committer_name'} =~ s/ <.*//;
1154 if (!defined $co{'tree'}) {
1155 return;
1158 foreach my $title (@commit_lines) {
1159 $title =~ s/^ //;
1160 if ($title ne "") {
1161 $co{'title'} = chop_str($title, 80, 5);
1162 # remove leading stuff of merges to make the interesting part visible
1163 if (length($title) > 50) {
1164 $title =~ s/^Automatic //;
1165 $title =~ s/^merge (of|with) /Merge ... /i;
1166 if (length($title) > 50) {
1167 $title =~ s/(http|rsync):\/\///;
1169 if (length($title) > 50) {
1170 $title =~ s/(master|www|rsync)\.//;
1172 if (length($title) > 50) {
1173 $title =~ s/kernel.org:?//;
1175 if (length($title) > 50) {
1176 $title =~ s/\/pub\/scm//;
1179 $co{'title_short'} = chop_str($title, 50, 5);
1180 last;
1183 if ($co{'title'} eq "") {
1184 $co{'title'} = $co{'title_short'} = '(no commit message)';
1186 # remove added spaces
1187 foreach my $line (@commit_lines) {
1188 $line =~ s/^ //;
1190 $co{'comment'} = \@commit_lines;
1192 my $age = time - $co{'committer_epoch'};
1193 $co{'age'} = $age;
1194 $co{'age_string'} = age_string($age);
1195 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1196 if ($age > 60*60*24*7*2) {
1197 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1198 $co{'age_string_age'} = $co{'age_string'};
1199 } else {
1200 $co{'age_string_date'} = $co{'age_string'};
1201 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1203 return %co;
1206 # parse ref from ref_file, given by ref_id, with given type
1207 sub parse_ref {
1208 my $ref_file = shift;
1209 my $ref_id = shift;
1210 my $type = shift || git_get_type($ref_id);
1211 my %ref_item;
1213 $ref_item{'type'} = $type;
1214 $ref_item{'id'} = $ref_id;
1215 $ref_item{'epoch'} = 0;
1216 $ref_item{'age'} = "unknown";
1217 if ($type eq "tag") {
1218 my %tag = parse_tag($ref_id);
1219 $ref_item{'comment'} = $tag{'comment'};
1220 if ($tag{'type'} eq "commit") {
1221 my %co = parse_commit($tag{'object'});
1222 $ref_item{'epoch'} = $co{'committer_epoch'};
1223 $ref_item{'age'} = $co{'age_string'};
1224 } elsif (defined($tag{'epoch'})) {
1225 my $age = time - $tag{'epoch'};
1226 $ref_item{'epoch'} = $tag{'epoch'};
1227 $ref_item{'age'} = age_string($age);
1229 $ref_item{'reftype'} = $tag{'type'};
1230 $ref_item{'name'} = $tag{'name'};
1231 $ref_item{'refid'} = $tag{'object'};
1232 } elsif ($type eq "commit"){
1233 my %co = parse_commit($ref_id);
1234 $ref_item{'reftype'} = "commit";
1235 $ref_item{'name'} = $ref_file;
1236 $ref_item{'title'} = $co{'title'};
1237 $ref_item{'refid'} = $ref_id;
1238 $ref_item{'epoch'} = $co{'committer_epoch'};
1239 $ref_item{'age'} = $co{'age_string'};
1240 } else {
1241 $ref_item{'reftype'} = $type;
1242 $ref_item{'name'} = $ref_file;
1243 $ref_item{'refid'} = $ref_id;
1246 return %ref_item;
1249 # parse line of git-diff-tree "raw" output
1250 sub parse_difftree_raw_line {
1251 my $line = shift;
1252 my %opts = @_;
1253 my %res;
1255 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1256 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1257 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1258 $res{'from_mode'} = $1;
1259 $res{'to_mode'} = $2;
1260 $res{'from_id'} = $3;
1261 $res{'to_id'} = $4;
1262 $res{'status'} = $5;
1263 $res{'similarity'} = $6;
1264 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1265 if ($opts{'-z') {
1266 ($res{'from_file'}, $res{'to_file'}) = split("\0", $7);
1267 } else {
1268 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1270 } else {
1271 if ($opts{'-z'}) {
1272 $res{'file'} = $7;
1273 } else {
1274 $res{'file'} = unquote($7);
1278 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1279 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1280 $res{'commit'} = $1;
1283 return wantarray ? %res : \%res;
1286 # parse line of git-ls-tree output
1287 sub parse_ls_tree_line ($;%) {
1288 my $line = shift;
1289 my %opts = @_;
1290 my %res;
1292 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1293 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1295 $res{'mode'} = $1;
1296 $res{'type'} = $2;
1297 $res{'hash'} = $3;
1298 if ($opts{'-z'}) {
1299 $res{'name'} = $4;
1300 } else {
1301 $res{'name'} = unquote($4);
1304 return wantarray ? %res : \%res;
1307 ## ......................................................................
1308 ## parse to array of hashes functions
1310 sub git_get_refs_list {
1311 my $type = shift || "";
1312 my %refs;
1313 my @reflist;
1315 my @refs;
1316 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1317 or return;
1318 while (my $line = <$fd>) {
1319 chomp $line;
1320 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1321 if (defined $refs{$1}) {
1322 push @{$refs{$1}}, $2;
1323 } else {
1324 $refs{$1} = [ $2 ];
1327 if (! $4) { # unpeeled, direct reference
1328 push @refs, { hash => $1, name => $3 }; # without type
1329 } elsif ($3 eq $refs[-1]{'name'}) {
1330 # most likely a tag is followed by its peeled
1331 # (deref) one, and when that happens we know the
1332 # previous one was of type 'tag'.
1333 $refs[-1]{'type'} = "tag";
1337 close $fd;
1339 foreach my $ref (@refs) {
1340 my $ref_file = $ref->{'name'};
1341 my $ref_id = $ref->{'hash'};
1343 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1344 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1346 push @reflist, \%ref_item;
1348 # sort refs by age
1349 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1350 return (\@reflist, \%refs);
1353 ## ----------------------------------------------------------------------
1354 ## filesystem-related functions
1356 sub get_file_owner {
1357 my $path = shift;
1359 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1360 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1361 if (!defined $gcos) {
1362 return undef;
1364 my $owner = $gcos;
1365 $owner =~ s/[,;].*$//;
1366 return to_utf8($owner);
1369 ## ......................................................................
1370 ## mimetype related functions
1372 sub mimetype_guess_file {
1373 my $filename = shift;
1374 my $mimemap = shift;
1375 -r $mimemap or return undef;
1377 my %mimemap;
1378 open(MIME, $mimemap) or return undef;
1379 while (<MIME>) {
1380 next if m/^#/; # skip comments
1381 my ($mime, $exts) = split(/\t+/);
1382 if (defined $exts) {
1383 my @exts = split(/\s+/, $exts);
1384 foreach my $ext (@exts) {
1385 $mimemap{$ext} = $mime;
1389 close(MIME);
1391 $filename =~ /\.([^.]*)$/;
1392 return $mimemap{$1};
1395 sub mimetype_guess {
1396 my $filename = shift;
1397 my $mime;
1398 $filename =~ /\./ or return undef;
1400 if ($mimetypes_file) {
1401 my $file = $mimetypes_file;
1402 if ($file !~ m!^/!) { # if it is relative path
1403 # it is relative to project
1404 $file = "$projectroot/$project/$file";
1406 $mime = mimetype_guess_file($filename, $file);
1408 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1409 return $mime;
1412 sub blob_mimetype {
1413 my $fd = shift;
1414 my $filename = shift;
1416 if ($filename) {
1417 my $mime = mimetype_guess($filename);
1418 $mime and return $mime;
1421 # just in case
1422 return $default_blob_plain_mimetype unless $fd;
1424 if (-T $fd) {
1425 return 'text/plain' .
1426 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1427 } elsif (! $filename) {
1428 return 'application/octet-stream';
1429 } elsif ($filename =~ m/\.png$/i) {
1430 return 'image/png';
1431 } elsif ($filename =~ m/\.gif$/i) {
1432 return 'image/gif';
1433 } elsif ($filename =~ m/\.jpe?g$/i) {
1434 return 'image/jpeg';
1435 } else {
1436 return 'application/octet-stream';
1440 ## ======================================================================
1441 ## functions printing HTML: header, footer, error page
1443 sub git_header_html {
1444 my $status = shift || "200 OK";
1445 my $expires = shift;
1447 my $title = "$site_name";
1448 if (defined $project) {
1449 $title .= " - $project";
1450 if (defined $action) {
1451 $title .= "/$action";
1452 if (defined $file_name) {
1453 $title .= " - " . esc_html($file_name);
1454 if ($action eq "tree" && $file_name !~ m|/$|) {
1455 $title .= "/";
1460 my $content_type;
1461 # require explicit support from the UA if we are to send the page as
1462 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1463 # we have to do this because MSIE sometimes globs '*/*', pretending to
1464 # support xhtml+xml but choking when it gets what it asked for.
1465 if (defined $cgi->http('HTTP_ACCEPT') &&
1466 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1467 $cgi->Accept('application/xhtml+xml') != 0) {
1468 $content_type = 'application/xhtml+xml';
1469 } else {
1470 $content_type = 'text/html';
1472 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1473 -status=> $status, -expires => $expires);
1474 print <<EOF;
1475 <?xml version="1.0" encoding="utf-8"?>
1476 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1477 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1478 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1479 <!-- git core binaries version $git_version -->
1480 <head>
1481 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1482 <meta name="generator" content="gitweb/$version git/$git_version"/>
1483 <meta name="robots" content="index, nofollow"/>
1484 <title>$title</title>
1486 # print out each stylesheet that exist
1487 if (defined $stylesheet) {
1488 #provides backwards capability for those people who define style sheet in a config file
1489 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1490 } else {
1491 foreach my $stylesheet (@stylesheets) {
1492 next unless $stylesheet;
1493 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1496 if (defined $project) {
1497 printf('<link rel="alternate" title="%s log" '.
1498 'href="%s" type="application/rss+xml"/>'."\n",
1499 esc_param($project), href(action=>"rss"));
1500 } else {
1501 printf('<link rel="alternate" title="%s projects list" '.
1502 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1503 $site_name, href(project=>undef, action=>"project_index"));
1504 printf('<link rel="alternate" title="%s projects logs" '.
1505 'href="%s" type="text/x-opml"/>'."\n",
1506 $site_name, href(project=>undef, action=>"opml"));
1508 if (defined $favicon) {
1509 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1512 print "</head>\n" .
1513 "<body>\n";
1515 if (-f $site_header) {
1516 open (my $fd, $site_header);
1517 print <$fd>;
1518 close $fd;
1521 print "<div class=\"page_header\">\n" .
1522 $cgi->a({-href => esc_url($logo_url),
1523 -title => $logo_label},
1524 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1525 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1526 if (defined $project) {
1527 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1528 if (defined $action) {
1529 print " / $action";
1531 print "\n";
1532 if (!defined $searchtext) {
1533 $searchtext = "";
1535 my $search_hash;
1536 if (defined $hash_base) {
1537 $search_hash = $hash_base;
1538 } elsif (defined $hash) {
1539 $search_hash = $hash;
1540 } else {
1541 $search_hash = "HEAD";
1543 $cgi->param("a", "search");
1544 $cgi->param("h", $search_hash);
1545 $cgi->param("p", $project);
1546 print $cgi->startform(-method => "get", -action => $my_uri) .
1547 "<div class=\"search\">\n" .
1548 $cgi->hidden(-name => "p") . "\n" .
1549 $cgi->hidden(-name => "a") . "\n" .
1550 $cgi->hidden(-name => "h") . "\n" .
1551 $cgi->popup_menu(-name => 'st', -default => 'commit',
1552 -values => ['commit', 'author', 'committer', 'pickaxe']) .
1553 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1554 " search:\n",
1555 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1556 "</div>" .
1557 $cgi->end_form() . "\n";
1559 print "</div>\n";
1562 sub git_footer_html {
1563 print "<div class=\"page_footer\">\n";
1564 if (defined $project) {
1565 my $descr = git_get_project_description($project);
1566 if (defined $descr) {
1567 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1569 print $cgi->a({-href => href(action=>"rss"),
1570 -class => "rss_logo"}, "RSS") . "\n";
1571 } else {
1572 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1573 -class => "rss_logo"}, "OPML") . " ";
1574 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1575 -class => "rss_logo"}, "TXT") . "\n";
1577 print "</div>\n" ;
1579 if (-f $site_footer) {
1580 open (my $fd, $site_footer);
1581 print <$fd>;
1582 close $fd;
1585 print "</body>\n" .
1586 "</html>";
1589 sub die_error {
1590 my $status = shift || "403 Forbidden";
1591 my $error = shift || "Malformed query, file missing or permission denied";
1593 git_header_html($status);
1594 print <<EOF;
1595 <div class="page_body">
1596 <br /><br />
1597 $status - $error
1598 <br />
1599 </div>
1601 git_footer_html();
1602 exit;
1605 ## ----------------------------------------------------------------------
1606 ## functions printing or outputting HTML: navigation
1608 sub git_print_page_nav {
1609 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1610 $extra = '' if !defined $extra; # pager or formats
1612 my @navs = qw(summary shortlog log commit commitdiff tree);
1613 if ($suppress) {
1614 @navs = grep { $_ ne $suppress } @navs;
1617 my %arg = map { $_ => {action=>$_} } @navs;
1618 if (defined $head) {
1619 for (qw(commit commitdiff)) {
1620 $arg{$_}{hash} = $head;
1622 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1623 for (qw(shortlog log)) {
1624 $arg{$_}{hash} = $head;
1628 $arg{tree}{hash} = $treehead if defined $treehead;
1629 $arg{tree}{hash_base} = $treebase if defined $treebase;
1631 print "<div class=\"page_nav\">\n" .
1632 (join " | ",
1633 map { $_ eq $current ?
1634 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1635 } @navs);
1636 print "<br/>\n$extra<br/>\n" .
1637 "</div>\n";
1640 sub format_paging_nav {
1641 my ($action, $hash, $head, $page, $nrevs) = @_;
1642 my $paging_nav;
1645 if ($hash ne $head || $page) {
1646 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1647 } else {
1648 $paging_nav .= "HEAD";
1651 if ($page > 0) {
1652 $paging_nav .= " &sdot; " .
1653 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1654 -accesskey => "p", -title => "Alt-p"}, "prev");
1655 } else {
1656 $paging_nav .= " &sdot; prev";
1659 if ($nrevs >= (100 * ($page+1)-1)) {
1660 $paging_nav .= " &sdot; " .
1661 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1662 -accesskey => "n", -title => "Alt-n"}, "next");
1663 } else {
1664 $paging_nav .= " &sdot; next";
1667 return $paging_nav;
1670 ## ......................................................................
1671 ## functions printing or outputting HTML: div
1673 sub git_print_header_div {
1674 my ($action, $title, $hash, $hash_base) = @_;
1675 my %args = ();
1677 $args{action} = $action;
1678 $args{hash} = $hash if $hash;
1679 $args{hash_base} = $hash_base if $hash_base;
1681 print "<div class=\"header\">\n" .
1682 $cgi->a({-href => href(%args), -class => "title"},
1683 $title ? $title : $action) .
1684 "\n</div>\n";
1687 #sub git_print_authorship (\%) {
1688 sub git_print_authorship {
1689 my $co = shift;
1691 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1692 print "<div class=\"author_date\">" .
1693 esc_html($co->{'author_name'}) .
1694 " [$ad{'rfc2822'}";
1695 if ($ad{'hour_local'} < 6) {
1696 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1697 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1698 } else {
1699 printf(" (%02d:%02d %s)",
1700 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1702 print "]</div>\n";
1705 sub git_print_page_path {
1706 my $name = shift;
1707 my $type = shift;
1708 my $hb = shift;
1711 print "<div class=\"page_path\">";
1712 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1713 -title => 'tree root'}, "[$project]");
1714 print " / ";
1715 if (defined $name) {
1716 my @dirname = split '/', $name;
1717 my $basename = pop @dirname;
1718 my $fullname = '';
1720 foreach my $dir (@dirname) {
1721 $fullname .= ($fullname ? '/' : '') . $dir;
1722 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1723 hash_base=>$hb),
1724 -title => $fullname}, esc_html($dir));
1725 print " / ";
1727 if (defined $type && $type eq 'blob') {
1728 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1729 hash_base=>$hb),
1730 -title => $name}, esc_html($basename));
1731 } elsif (defined $type && $type eq 'tree') {
1732 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1733 hash_base=>$hb),
1734 -title => $name}, esc_html($basename));
1735 print " / ";
1736 } else {
1737 print esc_html($basename);
1740 print "<br/></div>\n";
1743 # sub git_print_log (\@;%) {
1744 sub git_print_log ($;%) {
1745 my $log = shift;
1746 my %opts = @_;
1748 if ($opts{'-remove_title'}) {
1749 # remove title, i.e. first line of log
1750 shift @$log;
1752 # remove leading empty lines
1753 while (defined $log->[0] && $log->[0] eq "") {
1754 shift @$log;
1757 # print log
1758 my $signoff = 0;
1759 my $empty = 0;
1760 foreach my $line (@$log) {
1761 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1762 $signoff = 1;
1763 $empty = 0;
1764 if (! $opts{'-remove_signoff'}) {
1765 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1766 next;
1767 } else {
1768 # remove signoff lines
1769 next;
1771 } else {
1772 $signoff = 0;
1775 # print only one empty line
1776 # do not print empty line after signoff
1777 if ($line eq "") {
1778 next if ($empty || $signoff);
1779 $empty = 1;
1780 } else {
1781 $empty = 0;
1784 print format_log_line_html($line) . "<br/>\n";
1787 if ($opts{'-final_empty_line'}) {
1788 # end with single empty line
1789 print "<br/>\n" unless $empty;
1793 # print tree entry (row of git_tree), but without encompassing <tr> element
1794 sub git_print_tree_entry {
1795 my ($t, $basedir, $hash_base, $have_blame) = @_;
1797 my %base_key = ();
1798 $base_key{hash_base} = $hash_base if defined $hash_base;
1800 # The format of a table row is: mode list link. Where mode is
1801 # the mode of the entry, list is the name of the entry, an href,
1802 # and link is the action links of the entry.
1804 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1805 if ($t->{'type'} eq "blob") {
1806 print "<td class=\"list\">" .
1807 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1808 file_name=>"$basedir$t->{'name'}", %base_key),
1809 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1810 print "<td class=\"link\">";
1811 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1812 file_name=>"$basedir$t->{'name'}", %base_key)},
1813 "blob");
1814 if ($have_blame) {
1815 print " | " .
1816 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1817 file_name=>"$basedir$t->{'name'}", %base_key)},
1818 "blame");
1820 if (defined $hash_base) {
1821 print " | " .
1822 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1823 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1824 "history");
1826 print " | " .
1827 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1828 file_name=>"$basedir$t->{'name'}")},
1829 "raw");
1830 print "</td>\n";
1832 } elsif ($t->{'type'} eq "tree") {
1833 print "<td class=\"list\">";
1834 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1835 file_name=>"$basedir$t->{'name'}", %base_key)},
1836 esc_html($t->{'name'}));
1837 print "</td>\n";
1838 print "<td class=\"link\">";
1839 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1840 file_name=>"$basedir$t->{'name'}", %base_key)},
1841 "tree");
1842 if (defined $hash_base) {
1843 print " | " .
1844 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1845 file_name=>"$basedir$t->{'name'}")},
1846 "history");
1848 print "</td>\n";
1852 ## ......................................................................
1853 ## functions printing large fragments of HTML
1855 sub git_difftree_body {
1856 my ($difftree, $hash, $parent) = @_;
1858 if ($#{$difftree} > 10) {
1859 print "<div class=\"list_head\">\n";
1860 print(($#{$difftree} + 1) . " files changed:\n");
1861 print "</div>\n";
1864 print "<table class=\"diff_tree\">\n";
1865 my $alternate = 1;
1866 my $patchno = 0;
1867 foreach my $line (@{$difftree}) {
1868 my %diff = parse_difftree_raw_line($line);
1870 if ($alternate) {
1871 print "<tr class=\"dark\">\n";
1872 } else {
1873 print "<tr class=\"light\">\n";
1875 $alternate ^= 1;
1877 my ($to_mode_oct, $to_mode_str, $to_file_type);
1878 my ($from_mode_oct, $from_mode_str, $from_file_type);
1879 if ($diff{'to_mode'} ne ('0' x 6)) {
1880 $to_mode_oct = oct $diff{'to_mode'};
1881 if (S_ISREG($to_mode_oct)) { # only for regular file
1882 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1884 $to_file_type = file_type($diff{'to_mode'});
1886 if ($diff{'from_mode'} ne ('0' x 6)) {
1887 $from_mode_oct = oct $diff{'from_mode'};
1888 if (S_ISREG($to_mode_oct)) { # only for regular file
1889 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1891 $from_file_type = file_type($diff{'from_mode'});
1894 if ($diff{'status'} eq "A") { # created
1895 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1896 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1897 $mode_chng .= "]</span>";
1898 print "<td>";
1899 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1900 hash_base=>$hash, file_name=>$diff{'file'}),
1901 -class => "list"}, esc_html($diff{'file'}));
1902 print "</td>\n";
1903 print "<td>$mode_chng</td>\n";
1904 print "<td class=\"link\">";
1905 if ($action eq 'commitdiff') {
1906 # link to patch
1907 $patchno++;
1908 print $cgi->a({-href => "#patch$patchno"}, "patch");
1910 print "</td>\n";
1912 } elsif ($diff{'status'} eq "D") { # deleted
1913 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1914 print "<td>";
1915 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1916 hash_base=>$parent, file_name=>$diff{'file'}),
1917 -class => "list"}, esc_html($diff{'file'}));
1918 print "</td>\n";
1919 print "<td>$mode_chng</td>\n";
1920 print "<td class=\"link\">";
1921 if ($action eq 'commitdiff') {
1922 # link to patch
1923 $patchno++;
1924 print $cgi->a({-href => "#patch$patchno"}, "patch");
1925 print " | ";
1927 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1928 hash_base=>$parent, file_name=>$diff{'file'})},
1929 "blob") . " | ";
1930 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1931 file_name=>$diff{'file'})},
1932 "blame") . " | ";
1933 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1934 file_name=>$diff{'file'})},
1935 "history");
1936 print "</td>\n";
1938 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1939 my $mode_chnge = "";
1940 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1941 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1942 if ($from_file_type != $to_file_type) {
1943 $mode_chnge .= " from $from_file_type to $to_file_type";
1945 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1946 if ($from_mode_str && $to_mode_str) {
1947 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1948 } elsif ($to_mode_str) {
1949 $mode_chnge .= " mode: $to_mode_str";
1952 $mode_chnge .= "]</span>\n";
1954 print "<td>";
1955 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1956 hash_base=>$hash, file_name=>$diff{'file'}),
1957 -class => "list"}, esc_html($diff{'file'}));
1958 print "</td>\n";
1959 print "<td>$mode_chnge</td>\n";
1960 print "<td class=\"link\">";
1961 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1962 if ($action eq 'commitdiff') {
1963 # link to patch
1964 $patchno++;
1965 print $cgi->a({-href => "#patch$patchno"}, "patch");
1966 } else {
1967 print $cgi->a({-href => href(action=>"blobdiff",
1968 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1969 hash_base=>$hash, hash_parent_base=>$parent,
1970 file_name=>$diff{'file'})},
1971 "diff");
1973 print " | ";
1975 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1976 hash_base=>$hash, file_name=>$diff{'file'})},
1977 "blob") . " | ";
1978 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1979 file_name=>$diff{'file'})},
1980 "blame") . " | ";
1981 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1982 file_name=>$diff{'file'})},
1983 "history");
1984 print "</td>\n";
1986 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1987 my %status_name = ('R' => 'moved', 'C' => 'copied');
1988 my $nstatus = $status_name{$diff{'status'}};
1989 my $mode_chng = "";
1990 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1991 # mode also for directories, so we cannot use $to_mode_str
1992 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1994 print "<td>" .
1995 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1996 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1997 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1998 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1999 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2000 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
2001 -class => "list"}, esc_html($diff{'from_file'})) .
2002 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2003 "<td class=\"link\">";
2004 if ($diff{'to_id'} ne $diff{'from_id'}) {
2005 if ($action eq 'commitdiff') {
2006 # link to patch
2007 $patchno++;
2008 print $cgi->a({-href => "#patch$patchno"}, "patch");
2009 } else {
2010 print $cgi->a({-href => href(action=>"blobdiff",
2011 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
2012 hash_base=>$hash, hash_parent_base=>$parent,
2013 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
2014 "diff");
2016 print " | ";
2018 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
2019 hash_base=>$parent, file_name=>$diff{'from_file'})},
2020 "blob") . " | ";
2021 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2022 file_name=>$diff{'from_file'})},
2023 "blame") . " | ";
2024 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2025 file_name=>$diff{'from_file'})},
2026 "history");
2027 print "</td>\n";
2029 } # we should not encounter Unmerged (U) or Unknown (X) status
2030 print "</tr>\n";
2032 print "</table>\n";
2035 sub git_patchset_body {
2036 my ($fd, $difftree, $hash, $hash_parent) = @_;
2038 my $patch_idx = 0;
2039 my $in_header = 0;
2040 my $patch_found = 0;
2041 my $skip_patch = 0;
2042 my $diffinfo;
2043 my (%from, %to);
2045 print "<div class=\"patchset\">\n";
2047 LINE:
2048 while (my $patch_line = <$fd>) {
2049 chomp $patch_line;
2051 if ($patch_line =~ m/^diff /) { # "git diff" header
2052 # beginning of patch (in patchset)
2053 $skip_patch = 0;
2055 if ($patch_found) {
2056 # close previous patch
2057 print "</div>\n"; # class="patch"
2058 } else {
2059 # first patch in patchset
2060 $patch_found = 1;
2062 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2064 # read and prepare patch information
2065 if (ref($difftree->[$patch_idx]) eq "HASH") {
2066 # pre-parsed (or generated by hand)
2067 $diffinfo = $difftree->[$patch_idx];
2068 } else {
2069 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2071 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2072 $from{'name'} = $diffinfo->{'from_file_raw'} || $diffinfo->{'file_raw'};
2073 # because of "a/file" not a/"file"
2074 $from{'quoted'} = ($from{'name'} =~ s/^"(.*)"$/$1/);
2076 my $file = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2077 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2078 hash=>$diffinfo->{'from_id'}, file_name=>$file);
2080 if ($diffinfo->{'status'} ne "D") { # not deleted file
2081 $to{'name'} = $diffinfo->{'to_file_raw'} || $diffinfo->{'file_raw'};
2082 # because of "b/file" not b/"file"
2083 $to{'quoted'} = ($to{'name'} =~ s/^"(.*)"$/$1/);
2085 my $file = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2086 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2087 hash=>$diffinfo->{'to_id'}, file_name=>$file);
2089 $patch_idx++;
2091 # for now we skip empty patches
2092 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) {
2093 # no change, empty patch
2094 $in_header = 1;
2095 $skip_patch = 0;
2096 next LINE;
2099 # print "git diff" header
2100 if ($from{'name'}) {
2101 my $from_link = $cgi->a({-href => $from{'href'}, -class => "path"},
2102 'a/' . esc_html($from{'name'}));
2103 my ($q, $qq) = $from{'quoted'} ? ('"', '&quot;') : ('', '');
2104 $patch_line =~ s|${q}a/\Q$from{'name'}\E${q}|${qq}$from_link${qq}|;
2105 } else {
2106 # at least one of %from and %to must be set
2107 $patch_line =~ s|(["]?a/\Q$to{'name'}\E["]?)|esc_html($1)|e;
2109 if ($to{'name'}) {
2110 my $to_link = $cgi->a({-href => $to{'href'}, -class => "path"},
2111 'b/' . esc_html($to{'name'}));
2112 my ($q, $qq) = $to{'quoted'} ? ('"', '&quot;') : ('', '');
2113 $patch_line =~ s|${q}b/\Q$to{'name'}\E${q}$|${qq}$to_link${qq}|;
2114 } else {
2115 # at least one of %from and %to must be set
2116 $patch_line =~ s|(["]?b/\Q$from{'name'}\E["]?)$|esc_html($1)|e;
2119 print "<div class=\"diff header\">$patch_line</div>\n";
2120 print "<div class=\"diff extended_header\">\n";
2121 $in_header = 1;
2122 next LINE;
2123 } else {
2124 next LINE if $skip_patch;
2125 } # start of patch in patchset
2127 if ($in_header) {
2128 if ($patch_line !~ m/^---/) {
2129 # match <path>
2130 if ($patch_line =~ m!^(copy|rename) from ! && $from{'name'}) {
2131 my $qq = $from{'quoted'} ? '&quot;' : '';
2132 my $from_link = $cgi->a({-href=>$from{'href'},-class=>"list"},
2133 esc_html($from{'name'}));
2134 $patch_line =~ s!from .*$!from $qq$from_link$qq!;
2136 if ($patch_line =~ m!^(copy|rename) to ! && $to{'name'}) {
2137 my $qq = $to{'quoted'} ? '&quot;' : '';
2138 my $to_link = $cgi->a({-href=>$to{'href'},-class=>"list"},
2139 esc_html($to{'name'}));
2140 $patch_line =~ s!to .*$!to $qq$to_link$qq!;
2142 # match <mode>
2143 if ($patch_line =~ m/\s(\d{6})$/) {
2144 $patch_line .= '<span class="info"> (' . file_type($1) . ')</span>';
2146 # match <hash>
2147 if ($patch_line =~ m/^index/) {
2148 my ($from_link, $to_link);
2149 if ($from{'href'}) {
2150 $from_link = $cgi->a({-href=>$from{'href'},-class=>"list"},
2151 substr($diffinfo->{'from_id'},0,7));
2152 } else {
2153 $from_link = '0' x 7;
2155 if ($to{'href'}) {
2156 $to_link = $cgi->a({-href=>$to{'href'},-class=>"list"},
2157 substr($diffinfo->{'to_id'},0,7));
2158 } else {
2159 $to_link = '0' x 7;
2161 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2162 $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2164 print $patch_line . "<br/>\n";
2166 } else {
2167 #$patch_line =~ m/^---/;
2168 print "</div>\n"; # class="diff extended_header"
2169 $in_header = 0;
2171 if ($from{'name'}) {
2172 my $qq = $from{'quoted'} ? '&quot;' : '';
2173 my $from_link = $cgi->a({-href=>$from{'href'},-class=>"list"},
2174 esc_html($from{'name'}));
2175 $patch_line =~ s!["]?a/.*$!${qq}a/$from_link${qq}!;
2177 print "<div class=\"diff from_file\">$patch_line</div>\n";
2179 $patch_line = <$fd>;
2180 chomp $patch_line;
2182 #$patch_line =~ m/^+++/;
2183 if ($to{'name'}) {
2184 my $qq = $to{'quoted'} ? '&quot;' : '';
2185 my $from_link = $cgi->a({-href=>$to{'href'},-class=>"list"},
2186 esc_html($to{'name'}));
2187 $patch_line =~ s!["]?b/.*$!${qq}b/$from_link${qq}!;
2189 print "<div class=\"diff to_file\">$patch_line</div>\n";
2193 next LINE;
2196 print format_diff_line($patch_line);
2198 print "</div>\n" if $patch_found; # class="patch"
2200 print "</div>\n"; # class="patchset"
2203 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2205 sub git_shortlog_body {
2206 # uses global variable $project
2207 my ($revlist, $from, $to, $refs, $extra) = @_;
2209 $from = 0 unless defined $from;
2210 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2212 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2213 my $alternate = 1;
2214 for (my $i = $from; $i <= $to; $i++) {
2215 my $commit = $revlist->[$i];
2216 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2217 my $ref = format_ref_marker($refs, $commit);
2218 my %co = parse_commit($commit);
2219 if ($alternate) {
2220 print "<tr class=\"dark\">\n";
2221 } else {
2222 print "<tr class=\"light\">\n";
2224 $alternate ^= 1;
2225 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2226 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2227 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2228 "<td>";
2229 print format_subject_html($co{'title'}, $co{'title_short'},
2230 href(action=>"commit", hash=>$commit), $ref);
2231 print "</td>\n" .
2232 "<td class=\"link\">" .
2233 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2234 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2235 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2236 if (gitweb_have_snapshot()) {
2237 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2239 print "</td>\n" .
2240 "</tr>\n";
2242 if (defined $extra) {
2243 print "<tr>\n" .
2244 "<td colspan=\"4\">$extra</td>\n" .
2245 "</tr>\n";
2247 print "</table>\n";
2250 sub git_history_body {
2251 # Warning: assumes constant type (blob or tree) during history
2252 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2254 $from = 0 unless defined $from;
2255 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2257 print "<table class=\"history\" cellspacing=\"0\">\n";
2258 my $alternate = 1;
2259 for (my $i = $from; $i <= $to; $i++) {
2260 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2261 next;
2264 my $commit = $1;
2265 my %co = parse_commit($commit);
2266 if (!%co) {
2267 next;
2270 my $ref = format_ref_marker($refs, $commit);
2272 if ($alternate) {
2273 print "<tr class=\"dark\">\n";
2274 } else {
2275 print "<tr class=\"light\">\n";
2277 $alternate ^= 1;
2278 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2279 # shortlog uses chop_str($co{'author_name'}, 10)
2280 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2281 "<td>";
2282 # originally git_history used chop_str($co{'title'}, 50)
2283 print format_subject_html($co{'title'}, $co{'title_short'},
2284 href(action=>"commit", hash=>$commit), $ref);
2285 print "</td>\n" .
2286 "<td class=\"link\">" .
2287 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2288 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2290 if ($ftype eq 'blob') {
2291 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2292 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2293 if (defined $blob_current && defined $blob_parent &&
2294 $blob_current ne $blob_parent) {
2295 print " | " .
2296 $cgi->a({-href => href(action=>"blobdiff",
2297 hash=>$blob_current, hash_parent=>$blob_parent,
2298 hash_base=>$hash_base, hash_parent_base=>$commit,
2299 file_name=>$file_name)},
2300 "diff to current");
2303 print "</td>\n" .
2304 "</tr>\n";
2306 if (defined $extra) {
2307 print "<tr>\n" .
2308 "<td colspan=\"4\">$extra</td>\n" .
2309 "</tr>\n";
2311 print "</table>\n";
2314 sub git_tags_body {
2315 # uses global variable $project
2316 my ($taglist, $from, $to, $extra) = @_;
2317 $from = 0 unless defined $from;
2318 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2320 print "<table class=\"tags\" cellspacing=\"0\">\n";
2321 my $alternate = 1;
2322 for (my $i = $from; $i <= $to; $i++) {
2323 my $entry = $taglist->[$i];
2324 my %tag = %$entry;
2325 my $comment_lines = $tag{'comment'};
2326 my $comment = shift @$comment_lines;
2327 my $comment_short;
2328 if (defined $comment) {
2329 $comment_short = chop_str($comment, 30, 5);
2331 if ($alternate) {
2332 print "<tr class=\"dark\">\n";
2333 } else {
2334 print "<tr class=\"light\">\n";
2336 $alternate ^= 1;
2337 print "<td><i>$tag{'age'}</i></td>\n" .
2338 "<td>" .
2339 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2340 -class => "list name"}, esc_html($tag{'name'})) .
2341 "</td>\n" .
2342 "<td>";
2343 if (defined $comment) {
2344 print format_subject_html($comment, $comment_short,
2345 href(action=>"tag", hash=>$tag{'id'}));
2347 print "</td>\n" .
2348 "<td class=\"selflink\">";
2349 if ($tag{'type'} eq "tag") {
2350 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2351 } else {
2352 print "&nbsp;";
2354 print "</td>\n" .
2355 "<td class=\"link\">" . " | " .
2356 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2357 if ($tag{'reftype'} eq "commit") {
2358 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2359 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2360 } elsif ($tag{'reftype'} eq "blob") {
2361 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2363 print "</td>\n" .
2364 "</tr>";
2366 if (defined $extra) {
2367 print "<tr>\n" .
2368 "<td colspan=\"5\">$extra</td>\n" .
2369 "</tr>\n";
2371 print "</table>\n";
2374 sub git_heads_body {
2375 # uses global variable $project
2376 my ($headlist, $head, $from, $to, $extra) = @_;
2377 $from = 0 unless defined $from;
2378 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2380 print "<table class=\"heads\" cellspacing=\"0\">\n";
2381 my $alternate = 1;
2382 for (my $i = $from; $i <= $to; $i++) {
2383 my $entry = $headlist->[$i];
2384 my %tag = %$entry;
2385 my $curr = $tag{'id'} eq $head;
2386 if ($alternate) {
2387 print "<tr class=\"dark\">\n";
2388 } else {
2389 print "<tr class=\"light\">\n";
2391 $alternate ^= 1;
2392 print "<td><i>$tag{'age'}</i></td>\n" .
2393 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2394 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2395 -class => "list name"},esc_html($tag{'name'})) .
2396 "</td>\n" .
2397 "<td class=\"link\">" .
2398 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2399 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2400 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2401 "</td>\n" .
2402 "</tr>";
2404 if (defined $extra) {
2405 print "<tr>\n" .
2406 "<td colspan=\"3\">$extra</td>\n" .
2407 "</tr>\n";
2409 print "</table>\n";
2412 ## ======================================================================
2413 ## ======================================================================
2414 ## actions
2416 sub git_project_list {
2417 my $order = $cgi->param('o');
2418 if (defined $order && $order !~ m/project|descr|owner|age/) {
2419 die_error(undef, "Unknown order parameter");
2422 my @list = git_get_projects_list();
2423 my @projects;
2424 if (!@list) {
2425 die_error(undef, "No projects found");
2427 foreach my $pr (@list) {
2428 my (@aa) = git_get_last_activity($pr->{'path'});
2429 unless (@aa) {
2430 next;
2432 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2433 if (!defined $pr->{'descr'}) {
2434 my $descr = git_get_project_description($pr->{'path'}) || "";
2435 $pr->{'descr'} = chop_str($descr, 25, 5);
2437 if (!defined $pr->{'owner'}) {
2438 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2440 push @projects, $pr;
2443 git_header_html();
2444 if (-f $home_text) {
2445 print "<div class=\"index_include\">\n";
2446 open (my $fd, $home_text);
2447 print <$fd>;
2448 close $fd;
2449 print "</div>\n";
2451 print "<table class=\"project_list\">\n" .
2452 "<tr>\n";
2453 $order ||= "project";
2454 if ($order eq "project") {
2455 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2456 print "<th>Project</th>\n";
2457 } else {
2458 print "<th>" .
2459 $cgi->a({-href => href(project=>undef, order=>'project'),
2460 -class => "header"}, "Project") .
2461 "</th>\n";
2463 if ($order eq "descr") {
2464 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2465 print "<th>Description</th>\n";
2466 } else {
2467 print "<th>" .
2468 $cgi->a({-href => href(project=>undef, order=>'descr'),
2469 -class => "header"}, "Description") .
2470 "</th>\n";
2472 if ($order eq "owner") {
2473 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2474 print "<th>Owner</th>\n";
2475 } else {
2476 print "<th>" .
2477 $cgi->a({-href => href(project=>undef, order=>'owner'),
2478 -class => "header"}, "Owner") .
2479 "</th>\n";
2481 if ($order eq "age") {
2482 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2483 print "<th>Last Change</th>\n";
2484 } else {
2485 print "<th>" .
2486 $cgi->a({-href => href(project=>undef, order=>'age'),
2487 -class => "header"}, "Last Change") .
2488 "</th>\n";
2490 print "<th></th>\n" .
2491 "</tr>\n";
2492 my $alternate = 1;
2493 foreach my $pr (@projects) {
2494 if ($alternate) {
2495 print "<tr class=\"dark\">\n";
2496 } else {
2497 print "<tr class=\"light\">\n";
2499 $alternate ^= 1;
2500 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2501 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2502 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2503 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2504 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2505 $pr->{'age_string'} . "</td>\n" .
2506 "<td class=\"link\">" .
2507 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2508 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2509 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2510 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2511 "</td>\n" .
2512 "</tr>\n";
2514 print "</table>\n";
2515 git_footer_html();
2518 sub git_project_index {
2519 my @projects = git_get_projects_list();
2521 print $cgi->header(
2522 -type => 'text/plain',
2523 -charset => 'utf-8',
2524 -content_disposition => 'inline; filename="index.aux"');
2526 foreach my $pr (@projects) {
2527 if (!exists $pr->{'owner'}) {
2528 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2531 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2532 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2533 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2534 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2535 $path =~ s/ /\+/g;
2536 $owner =~ s/ /\+/g;
2538 print "$path $owner\n";
2542 sub git_summary {
2543 my $descr = git_get_project_description($project) || "none";
2544 my $head = git_get_head_hash($project);
2545 my %co = parse_commit($head);
2546 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2548 my $owner = git_get_project_owner($project);
2550 my ($reflist, $refs) = git_get_refs_list();
2552 my @taglist;
2553 my @headlist;
2554 foreach my $ref (@$reflist) {
2555 if ($ref->{'name'} =~ s!^heads/!!) {
2556 push @headlist, $ref;
2557 } else {
2558 $ref->{'name'} =~ s!^tags/!!;
2559 push @taglist, $ref;
2563 git_header_html();
2564 git_print_page_nav('summary','', $head);
2566 print "<div class=\"title\">&nbsp;</div>\n";
2567 print "<table cellspacing=\"0\">\n" .
2568 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2569 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2570 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2571 # use per project git URL list in $projectroot/$project/cloneurl
2572 # or make project git URL from git base URL and project name
2573 my $url_tag = "URL";
2574 my @url_list = git_get_project_url_list($project);
2575 @url_list = map { $_ ? "$_/$project" : () }
2576 @git_base_url_list unless @url_list;
2577 foreach my $git_url (@url_list) {
2578 next unless $git_url;
2579 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2580 $url_tag = "";
2582 print "</table>\n";
2584 if (-s "$projectroot/$project/README.html") {
2585 if (open my $fd, "$projectroot/$project/README.html") {
2586 print "<div class=\"title\">readme</div>\n";
2587 print $_ while (<$fd>);
2588 close $fd;
2592 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2593 git_get_head_hash($project)
2594 or die_error(undef, "Open git-rev-list failed");
2595 my @revlist = map { chomp; $_ } <$fd>;
2596 close $fd;
2597 git_print_header_div('shortlog');
2598 git_shortlog_body(\@revlist, 0, 15, $refs,
2599 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2601 if (@taglist) {
2602 git_print_header_div('tags');
2603 git_tags_body(\@taglist, 0, 15,
2604 $cgi->a({-href => href(action=>"tags")}, "..."));
2607 if (@headlist) {
2608 git_print_header_div('heads');
2609 git_heads_body(\@headlist, $head, 0, 15,
2610 $cgi->a({-href => href(action=>"heads")}, "..."));
2613 git_footer_html();
2616 sub git_tag {
2617 my $head = git_get_head_hash($project);
2618 git_header_html();
2619 git_print_page_nav('','', $head,undef,$head);
2620 my %tag = parse_tag($hash);
2621 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2622 print "<div class=\"title_text\">\n" .
2623 "<table cellspacing=\"0\">\n" .
2624 "<tr>\n" .
2625 "<td>object</td>\n" .
2626 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2627 $tag{'object'}) . "</td>\n" .
2628 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2629 $tag{'type'}) . "</td>\n" .
2630 "</tr>\n";
2631 if (defined($tag{'author'})) {
2632 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2633 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2634 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2635 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2636 "</td></tr>\n";
2638 print "</table>\n\n" .
2639 "</div>\n";
2640 print "<div class=\"page_body\">";
2641 my $comment = $tag{'comment'};
2642 foreach my $line (@$comment) {
2643 print esc_html($line) . "<br/>\n";
2645 print "</div>\n";
2646 git_footer_html();
2649 sub git_blame2 {
2650 my $fd;
2651 my $ftype;
2653 my ($have_blame) = gitweb_check_feature('blame');
2654 if (!$have_blame) {
2655 die_error('403 Permission denied', "Permission denied");
2657 die_error('404 Not Found', "File name not defined") if (!$file_name);
2658 $hash_base ||= git_get_head_hash($project);
2659 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2660 my %co = parse_commit($hash_base)
2661 or die_error(undef, "Reading commit failed");
2662 if (!defined $hash) {
2663 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2664 or die_error(undef, "Error looking up file");
2666 $ftype = git_get_type($hash);
2667 if ($ftype !~ "blob") {
2668 die_error("400 Bad Request", "Object is not a blob");
2670 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
2671 $file_name, $hash_base)
2672 or die_error(undef, "Open git-blame failed");
2673 git_header_html();
2674 my $formats_nav =
2675 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2676 "blob") .
2677 " | " .
2678 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2679 "history") .
2680 " | " .
2681 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2682 "HEAD");
2683 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2684 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2685 git_print_page_path($file_name, $ftype, $hash_base);
2686 my @rev_color = (qw(light2 dark2));
2687 my $num_colors = scalar(@rev_color);
2688 my $current_color = 0;
2689 my $last_rev;
2690 print <<HTML;
2691 <div class="page_body">
2692 <table class="blame">
2693 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2694 HTML
2695 my %metainfo = ();
2696 while (1) {
2697 $_ = <$fd>;
2698 last unless defined $_;
2699 my ($full_rev, $orig_lineno, $lineno, $group_size) =
2700 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
2701 if (!exists $metainfo{$full_rev}) {
2702 $metainfo{$full_rev} = {};
2704 my $meta = $metainfo{$full_rev};
2705 while (<$fd>) {
2706 last if (s/^\t//);
2707 if (/^(\S+) (.*)$/) {
2708 $meta->{$1} = $2;
2711 my $data = $_;
2712 my $rev = substr($full_rev, 0, 8);
2713 my $author = $meta->{'author'};
2714 my %date = parse_date($meta->{'author-time'},
2715 $meta->{'author-tz'});
2716 my $date = $date{'iso-tz'};
2717 if ($group_size) {
2718 $current_color = ++$current_color % $num_colors;
2720 print "<tr class=\"$rev_color[$current_color]\">\n";
2721 if ($group_size) {
2722 print "<td class=\"sha1\"";
2723 print " title=\"$author, $date\"";
2724 print " rowspan=\"$group_size\"" if ($group_size > 1);
2725 print ">";
2726 print $cgi->a({-href => href(action=>"commit",
2727 hash=>$full_rev,
2728 file_name=>$file_name)},
2729 esc_html($rev));
2730 print "</td>\n";
2732 my $blamed = href(action => 'blame',
2733 file_name => $meta->{'filename'},
2734 hash_base => $full_rev);
2735 print "<td class=\"linenr\">";
2736 print $cgi->a({ -href => "$blamed#l$orig_lineno",
2737 -id => "l$lineno",
2738 -class => "linenr" },
2739 esc_html($lineno));
2740 print "</td>";
2741 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2742 print "</tr>\n";
2744 print "</table>\n";
2745 print "</div>";
2746 close $fd
2747 or print "Reading blob failed\n";
2748 git_footer_html();
2751 sub git_blame {
2752 my $fd;
2754 my ($have_blame) = gitweb_check_feature('blame');
2755 if (!$have_blame) {
2756 die_error('403 Permission denied', "Permission denied");
2758 die_error('404 Not Found', "File name not defined") if (!$file_name);
2759 $hash_base ||= git_get_head_hash($project);
2760 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2761 my %co = parse_commit($hash_base)
2762 or die_error(undef, "Reading commit failed");
2763 if (!defined $hash) {
2764 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2765 or die_error(undef, "Error lookup file");
2767 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2768 or die_error(undef, "Open git-annotate failed");
2769 git_header_html();
2770 my $formats_nav =
2771 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2772 "blob") .
2773 " | " .
2774 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2775 "history") .
2776 " | " .
2777 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2778 "HEAD");
2779 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2780 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2781 git_print_page_path($file_name, 'blob', $hash_base);
2782 print "<div class=\"page_body\">\n";
2783 print <<HTML;
2784 <table class="blame">
2785 <tr>
2786 <th>Commit</th>
2787 <th>Age</th>
2788 <th>Author</th>
2789 <th>Line</th>
2790 <th>Data</th>
2791 </tr>
2792 HTML
2793 my @line_class = (qw(light dark));
2794 my $line_class_len = scalar (@line_class);
2795 my $line_class_num = $#line_class;
2796 while (my $line = <$fd>) {
2797 my $long_rev;
2798 my $short_rev;
2799 my $author;
2800 my $time;
2801 my $lineno;
2802 my $data;
2803 my $age;
2804 my $age_str;
2805 my $age_class;
2807 chomp $line;
2808 $line_class_num = ($line_class_num + 1) % $line_class_len;
2810 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2811 $long_rev = $1;
2812 $author = $2;
2813 $time = $3;
2814 $lineno = $4;
2815 $data = $5;
2816 } else {
2817 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2818 next;
2820 $short_rev = substr ($long_rev, 0, 8);
2821 $age = time () - $time;
2822 $age_str = age_string ($age);
2823 $age_str =~ s/ /&nbsp;/g;
2824 $age_class = age_class($age);
2825 $author = esc_html ($author);
2826 $author =~ s/ /&nbsp;/g;
2828 $data = untabify($data);
2829 $data = esc_html ($data);
2831 print <<HTML;
2832 <tr class="$line_class[$line_class_num]">
2833 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2834 <td class="$age_class">$age_str</td>
2835 <td>$author</td>
2836 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2837 <td class="pre">$data</td>
2838 </tr>
2839 HTML
2840 } # while (my $line = <$fd>)
2841 print "</table>\n\n";
2842 close $fd
2843 or print "Reading blob failed.\n";
2844 print "</div>";
2845 git_footer_html();
2848 sub git_tags {
2849 my $head = git_get_head_hash($project);
2850 git_header_html();
2851 git_print_page_nav('','', $head,undef,$head);
2852 git_print_header_div('summary', $project);
2854 my ($taglist) = git_get_refs_list("tags");
2855 if (@$taglist) {
2856 git_tags_body($taglist);
2858 git_footer_html();
2861 sub git_heads {
2862 my $head = git_get_head_hash($project);
2863 git_header_html();
2864 git_print_page_nav('','', $head,undef,$head);
2865 git_print_header_div('summary', $project);
2867 my ($headlist) = git_get_refs_list("heads");
2868 if (@$headlist) {
2869 git_heads_body($headlist, $head);
2871 git_footer_html();
2874 sub git_blob_plain {
2875 my $expires;
2877 if (!defined $hash) {
2878 if (defined $file_name) {
2879 my $base = $hash_base || git_get_head_hash($project);
2880 $hash = git_get_hash_by_path($base, $file_name, "blob")
2881 or die_error(undef, "Error lookup file");
2882 } else {
2883 die_error(undef, "No file name defined");
2885 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2886 # blobs defined by non-textual hash id's can be cached
2887 $expires = "+1d";
2890 my $type = shift;
2891 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2892 or die_error(undef, "Couldn't cat $file_name, $hash");
2894 $type ||= blob_mimetype($fd, $file_name);
2896 # save as filename, even when no $file_name is given
2897 my $save_as = "$hash";
2898 if (defined $file_name) {
2899 $save_as = $file_name;
2900 } elsif ($type =~ m/^text\//) {
2901 $save_as .= '.txt';
2904 print $cgi->header(
2905 -type => "$type",
2906 -expires=>$expires,
2907 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2908 undef $/;
2909 binmode STDOUT, ':raw';
2910 print <$fd>;
2911 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2912 $/ = "\n";
2913 close $fd;
2916 sub git_blob {
2917 my $expires;
2919 if (!defined $hash) {
2920 if (defined $file_name) {
2921 my $base = $hash_base || git_get_head_hash($project);
2922 $hash = git_get_hash_by_path($base, $file_name, "blob")
2923 or die_error(undef, "Error lookup file");
2924 } else {
2925 die_error(undef, "No file name defined");
2927 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2928 # blobs defined by non-textual hash id's can be cached
2929 $expires = "+1d";
2932 my ($have_blame) = gitweb_check_feature('blame');
2933 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2934 or die_error(undef, "Couldn't cat $file_name, $hash");
2935 my $mimetype = blob_mimetype($fd, $file_name);
2936 if ($mimetype !~ m/^text\//) {
2937 close $fd;
2938 return git_blob_plain($mimetype);
2940 git_header_html(undef, $expires);
2941 my $formats_nav = '';
2942 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2943 if (defined $file_name) {
2944 if ($have_blame) {
2945 $formats_nav .=
2946 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2947 hash=>$hash, file_name=>$file_name)},
2948 "blame") .
2949 " | ";
2951 $formats_nav .=
2952 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2953 hash=>$hash, file_name=>$file_name)},
2954 "history") .
2955 " | " .
2956 $cgi->a({-href => href(action=>"blob_plain",
2957 hash=>$hash, file_name=>$file_name)},
2958 "raw") .
2959 " | " .
2960 $cgi->a({-href => href(action=>"blob",
2961 hash_base=>"HEAD", file_name=>$file_name)},
2962 "HEAD");
2963 } else {
2964 $formats_nav .=
2965 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2967 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2968 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2969 } else {
2970 print "<div class=\"page_nav\">\n" .
2971 "<br/><br/></div>\n" .
2972 "<div class=\"title\">$hash</div>\n";
2974 git_print_page_path($file_name, "blob", $hash_base);
2975 print "<div class=\"page_body\">\n";
2976 my $nr;
2977 while (my $line = <$fd>) {
2978 chomp $line;
2979 $nr++;
2980 $line = untabify($line);
2981 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2982 $nr, $nr, $nr, esc_html($line);
2984 close $fd
2985 or print "Reading blob failed.\n";
2986 print "</div>";
2987 git_footer_html();
2990 sub git_tree {
2991 my $have_snapshot = gitweb_have_snapshot();
2993 if (!defined $hash_base) {
2994 $hash_base = "HEAD";
2996 if (!defined $hash) {
2997 if (defined $file_name) {
2998 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2999 } else {
3000 $hash = $hash_base;
3003 $/ = "\0";
3004 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3005 or die_error(undef, "Open git-ls-tree failed");
3006 my @entries = map { chomp; $_ } <$fd>;
3007 close $fd or die_error(undef, "Reading tree failed");
3008 $/ = "\n";
3010 my $refs = git_get_references();
3011 my $ref = format_ref_marker($refs, $hash_base);
3012 git_header_html();
3013 my $basedir = '';
3014 my ($have_blame) = gitweb_check_feature('blame');
3015 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3016 my @views_nav = ();
3017 if (defined $file_name) {
3018 push @views_nav,
3019 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3020 hash=>$hash, file_name=>$file_name)},
3021 "history"),
3022 $cgi->a({-href => href(action=>"tree",
3023 hash_base=>"HEAD", file_name=>$file_name)},
3024 "HEAD"),
3026 if ($have_snapshot) {
3027 # FIXME: Should be available when we have no hash base as well.
3028 push @views_nav,
3029 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3030 "snapshot");
3032 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3033 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3034 } else {
3035 undef $hash_base;
3036 print "<div class=\"page_nav\">\n";
3037 print "<br/><br/></div>\n";
3038 print "<div class=\"title\">$hash</div>\n";
3040 if (defined $file_name) {
3041 $basedir = $file_name;
3042 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3043 $basedir .= '/';
3046 git_print_page_path($file_name, 'tree', $hash_base);
3047 print "<div class=\"page_body\">\n";
3048 print "<table cellspacing=\"0\">\n";
3049 my $alternate = 1;
3050 # '..' (top directory) link if possible
3051 if (defined $hash_base &&
3052 defined $file_name && $file_name =~ m![^/]+$!) {
3053 if ($alternate) {
3054 print "<tr class=\"dark\">\n";
3055 } else {
3056 print "<tr class=\"light\">\n";
3058 $alternate ^= 1;
3060 my $up = $file_name;
3061 $up =~ s!/?[^/]+$!!;
3062 undef $up unless $up;
3063 # based on git_print_tree_entry
3064 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3065 print '<td class="list">';
3066 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3067 file_name=>$up)},
3068 "..");
3069 print "</td>\n";
3070 print "<td class=\"link\"></td>\n";
3072 print "</tr>\n";
3074 foreach my $line (@entries) {
3075 my %t = parse_ls_tree_line($line, -z => 1);
3077 if ($alternate) {
3078 print "<tr class=\"dark\">\n";
3079 } else {
3080 print "<tr class=\"light\">\n";
3082 $alternate ^= 1;
3084 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3086 print "</tr>\n";
3088 print "</table>\n" .
3089 "</div>";
3090 git_footer_html();
3093 sub git_snapshot {
3094 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3095 my $have_snapshot = (defined $ctype && defined $suffix);
3096 if (!$have_snapshot) {
3097 die_error('403 Permission denied', "Permission denied");
3100 if (!defined $hash) {
3101 $hash = git_get_head_hash($project);
3104 my $filename = basename($project) . "-$hash.tar.$suffix";
3106 print $cgi->header(
3107 -type => 'application/x-tar',
3108 -content_encoding => $ctype,
3109 -content_disposition => 'inline; filename="' . "$filename" . '"',
3110 -status => '200 OK');
3112 my $git = git_cmd_str();
3113 my $name = $project;
3114 $name =~ s/\047/\047\\\047\047/g;
3115 open my $fd, "-|",
3116 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3117 or die_error(undef, "Execute git-tar-tree failed.");
3118 binmode STDOUT, ':raw';
3119 print <$fd>;
3120 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3121 close $fd;
3125 sub git_log {
3126 my $head = git_get_head_hash($project);
3127 if (!defined $hash) {
3128 $hash = $head;
3130 if (!defined $page) {
3131 $page = 0;
3133 my $refs = git_get_references();
3135 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3136 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3137 or die_error(undef, "Open git-rev-list failed");
3138 my @revlist = map { chomp; $_ } <$fd>;
3139 close $fd;
3141 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
3143 git_header_html();
3144 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3146 if (!@revlist) {
3147 my %co = parse_commit($hash);
3149 git_print_header_div('summary', $project);
3150 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3152 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
3153 my $commit = $revlist[$i];
3154 my $ref = format_ref_marker($refs, $commit);
3155 my %co = parse_commit($commit);
3156 next if !%co;
3157 my %ad = parse_date($co{'author_epoch'});
3158 git_print_header_div('commit',
3159 "<span class=\"age\">$co{'age_string'}</span>" .
3160 esc_html($co{'title'}) . $ref,
3161 $commit);
3162 print "<div class=\"title_text\">\n" .
3163 "<div class=\"log_link\">\n" .
3164 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3165 " | " .
3166 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3167 " | " .
3168 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3169 "<br/>\n" .
3170 "</div>\n" .
3171 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3172 "</div>\n";
3174 print "<div class=\"log_body\">\n";
3175 git_print_log($co{'comment'}, -final_empty_line=> 1);
3176 print "</div>\n";
3178 git_footer_html();
3181 sub git_commit {
3182 my %co = parse_commit($hash);
3183 if (!%co) {
3184 die_error(undef, "Unknown commit object");
3186 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3187 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3189 my $parent = $co{'parent'};
3190 if (!defined $parent) {
3191 $parent = "--root";
3193 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
3194 @diff_opts, $parent, $hash
3195 or die_error(undef, "Open git-diff-tree failed");
3196 my @difftree = map { chomp; $_ } <$fd>;
3197 close $fd or die_error(undef, "Reading git-diff-tree failed");
3199 # non-textual hash id's can be cached
3200 my $expires;
3201 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3202 $expires = "+1d";
3204 my $refs = git_get_references();
3205 my $ref = format_ref_marker($refs, $co{'id'});
3207 my $have_snapshot = gitweb_have_snapshot();
3209 my @views_nav = ();
3210 if (defined $file_name && defined $co{'parent'}) {
3211 push @views_nav,
3212 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3213 "blame");
3215 git_header_html(undef, $expires);
3216 git_print_page_nav('commit', '',
3217 $hash, $co{'tree'}, $hash,
3218 join (' | ', @views_nav));
3220 if (defined $co{'parent'}) {
3221 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3222 } else {
3223 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3225 print "<div class=\"title_text\">\n" .
3226 "<table cellspacing=\"0\">\n";
3227 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3228 "<tr>" .
3229 "<td></td><td> $ad{'rfc2822'}";
3230 if ($ad{'hour_local'} < 6) {
3231 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3232 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3233 } else {
3234 printf(" (%02d:%02d %s)",
3235 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3237 print "</td>" .
3238 "</tr>\n";
3239 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3240 print "<tr><td></td><td> $cd{'rfc2822'}" .
3241 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3242 "</td></tr>\n";
3243 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3244 print "<tr>" .
3245 "<td>tree</td>" .
3246 "<td class=\"sha1\">" .
3247 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3248 class => "list"}, $co{'tree'}) .
3249 "</td>" .
3250 "<td class=\"link\">" .
3251 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3252 "tree");
3253 if ($have_snapshot) {
3254 print " | " .
3255 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3257 print "</td>" .
3258 "</tr>\n";
3259 my $parents = $co{'parents'};
3260 foreach my $par (@$parents) {
3261 print "<tr>" .
3262 "<td>parent</td>" .
3263 "<td class=\"sha1\">" .
3264 $cgi->a({-href => href(action=>"commit", hash=>$par),
3265 class => "list"}, $par) .
3266 "</td>" .
3267 "<td class=\"link\">" .
3268 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3269 " | " .
3270 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3271 "</td>" .
3272 "</tr>\n";
3274 print "</table>".
3275 "</div>\n";
3277 print "<div class=\"page_body\">\n";
3278 git_print_log($co{'comment'});
3279 print "</div>\n";
3281 git_difftree_body(\@difftree, $hash, $parent);
3283 git_footer_html();
3286 sub git_blobdiff {
3287 my $format = shift || 'html';
3289 my $fd;
3290 my @difftree;
3291 my %diffinfo;
3292 my $expires;
3294 # preparing $fd and %diffinfo for git_patchset_body
3295 # new style URI
3296 if (defined $hash_base && defined $hash_parent_base) {
3297 if (defined $file_name) {
3298 # read raw output
3299 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3300 "--", $file_name
3301 or die_error(undef, "Open git-diff-tree failed");
3302 @difftree = map { chomp; $_ } <$fd>;
3303 close $fd
3304 or die_error(undef, "Reading git-diff-tree failed");
3305 @difftree
3306 or die_error('404 Not Found', "Blob diff not found");
3308 } elsif (defined $hash &&
3309 $hash =~ /[0-9a-fA-F]{40}/) {
3310 # try to find filename from $hash
3312 # read filtered raw output
3313 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3314 or die_error(undef, "Open git-diff-tree failed");
3315 @difftree =
3316 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3317 # $hash == to_id
3318 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3319 map { chomp; $_ } <$fd>;
3320 close $fd
3321 or die_error(undef, "Reading git-diff-tree failed");
3322 @difftree
3323 or die_error('404 Not Found', "Blob diff not found");
3325 } else {
3326 die_error('404 Not Found', "Missing one of the blob diff parameters");
3329 if (@difftree > 1) {
3330 die_error('404 Not Found', "Ambiguous blob diff specification");
3333 %diffinfo = parse_difftree_raw_line($difftree[0]);
3334 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3335 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3337 $hash_parent ||= $diffinfo{'from_id'};
3338 $hash ||= $diffinfo{'to_id'};
3340 # non-textual hash id's can be cached
3341 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3342 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3343 $expires = '+1d';
3346 # open patch output
3347 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3348 '-p', $hash_parent_base, $hash_base,
3349 "--", $file_name
3350 or die_error(undef, "Open git-diff-tree failed");
3353 # old/legacy style URI
3354 if (!%diffinfo && # if new style URI failed
3355 defined $hash && defined $hash_parent) {
3356 # fake git-diff-tree raw output
3357 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3358 $diffinfo{'from_id'} = $hash_parent;
3359 $diffinfo{'to_id'} = $hash;
3360 if (defined $file_name) {
3361 if (defined $file_parent) {
3362 $diffinfo{'status'} = '2';
3363 $diffinfo{'from_file'} = $file_parent;
3364 $diffinfo{'to_file'} = $file_name;
3365 } else { # assume not renamed
3366 $diffinfo{'status'} = '1';
3367 $diffinfo{'from_file'} = $file_name;
3368 $diffinfo{'to_file'} = $file_name;
3370 } else { # no filename given
3371 $diffinfo{'status'} = '2';
3372 $diffinfo{'from_file'} = $hash_parent;
3373 $diffinfo{'to_file'} = $hash;
3376 # non-textual hash id's can be cached
3377 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3378 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3379 $expires = '+1d';
3382 # open patch output
3383 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3384 or die_error(undef, "Open git-diff failed");
3385 } else {
3386 die_error('404 Not Found', "Missing one of the blob diff parameters")
3387 unless %diffinfo;
3390 # header
3391 if ($format eq 'html') {
3392 my $formats_nav =
3393 $cgi->a({-href => href(action=>"blobdiff_plain",
3394 hash=>$hash, hash_parent=>$hash_parent,
3395 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3396 file_name=>$file_name, file_parent=>$file_parent)},
3397 "raw");
3398 git_header_html(undef, $expires);
3399 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3400 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3401 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3402 } else {
3403 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3404 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3406 if (defined $file_name) {
3407 git_print_page_path($file_name, "blob", $hash_base);
3408 } else {
3409 print "<div class=\"page_path\"></div>\n";
3412 } elsif ($format eq 'plain') {
3413 print $cgi->header(
3414 -type => 'text/plain',
3415 -charset => 'utf-8',
3416 -expires => $expires,
3417 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3419 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3421 } else {
3422 die_error(undef, "Unknown blobdiff format");
3425 # patch
3426 if ($format eq 'html') {
3427 print "<div class=\"page_body\">\n";
3429 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3430 close $fd;
3432 print "</div>\n"; # class="page_body"
3433 git_footer_html();
3435 } else {
3436 while (my $line = <$fd>) {
3437 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3438 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3440 print $line;
3442 last if $line =~ m!^\+\+\+!;
3444 local $/ = undef;
3445 print <$fd>;
3446 close $fd;
3450 sub git_blobdiff_plain {
3451 git_blobdiff('plain');
3454 sub git_commitdiff {
3455 my $format = shift || 'html';
3456 my %co = parse_commit($hash);
3457 if (!%co) {
3458 die_error(undef, "Unknown commit object");
3461 # we need to prepare $formats_nav before any parameter munging
3462 my $formats_nav;
3463 if ($format eq 'html') {
3464 $formats_nav =
3465 $cgi->a({-href => href(action=>"commitdiff_plain",
3466 hash=>$hash, hash_parent=>$hash_parent)},
3467 "raw");
3469 if (defined $hash_parent && $hash_parent !~ m/^-/) {
3470 # commitdiff with two commits given
3471 # second "commit" is not in fact diff option
3472 $formats_nav .=
3473 ' (' .
3474 $cgi->a({-href => href(action=>"commitdiff",
3475 hash=>$hash_parent)},
3476 'from') .
3477 ')';
3478 } elsif (!$co{'parent'}) {
3479 # --root commitdiff
3480 $formats_nav .= ' (initial)';
3481 } elsif (scalar @{$co{'parents'}} == 1) {
3482 # single parent commit
3483 $formats_nav .=
3484 ' (' .
3485 $cgi->a({-href => href(action=>"commitdiff",
3486 hash=>$co{'parent'})},
3487 'parent') .
3488 ')';
3489 } else {
3490 # merge commit
3491 $formats_nav .=
3492 ' (merge: ' .
3493 join(' ', map {
3494 $cgi->a({-href => href(action=>"commitdiff",
3495 hash=>$_)},
3496 'parent');
3497 } @{$co{'parents'}} ) .
3498 ')';
3502 if (!defined $hash_parent) {
3503 $hash_parent = $co{'parent'} || '--root';
3506 # read commitdiff
3507 my $fd;
3508 my @difftree;
3509 if ($format eq 'html') {
3510 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3511 "--no-commit-id",
3512 "--patch-with-raw", "--full-index", $hash_parent, $hash
3513 or die_error(undef, "Open git-diff-tree failed");
3515 while (chomp(my $line = <$fd>)) {
3516 # empty line ends raw part of diff-tree output
3517 last unless $line;
3518 push @difftree, $line;
3521 } elsif ($format eq 'plain') {
3522 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3523 '-p', $hash_parent, $hash
3524 or die_error(undef, "Open git-diff-tree failed");
3526 } else {
3527 die_error(undef, "Unknown commitdiff format");
3530 # non-textual hash id's can be cached
3531 my $expires;
3532 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3533 $expires = "+1d";
3536 # write commit message
3537 if ($format eq 'html') {
3538 my $refs = git_get_references();
3539 my $ref = format_ref_marker($refs, $co{'id'});
3541 git_header_html(undef, $expires);
3542 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3543 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3544 git_print_authorship(\%co);
3546 if (@{$co{'comment'}} > 1) {
3547 print "<div class=\"commitdiff_log\">\n";
3548 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
3549 print "</div>\n"; # class="commitdiff_log"
3552 print "<div class=\"page_body\">\n";
3554 } elsif ($format eq 'plain') {
3555 my $refs = git_get_references("tags");
3556 my $tagname = git_get_rev_name_tags($hash);
3557 my $filename = basename($project) . "-$hash.patch";
3559 print $cgi->header(
3560 -type => 'text/plain',
3561 -charset => 'utf-8',
3562 -expires => $expires,
3563 -content_disposition => 'inline; filename="' . "$filename" . '"');
3564 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3565 print <<TEXT;
3566 From: $co{'author'}
3567 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3568 Subject: $co{'title'}
3569 TEXT
3570 print "X-Git-Tag: $tagname\n" if $tagname;
3571 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3573 foreach my $line (@{$co{'comment'}}) {
3574 print "$line\n";
3576 print "---\n\n";
3579 # write patch
3580 if ($format eq 'html') {
3581 git_difftree_body(\@difftree, $hash, $hash_parent);
3582 print "<br/>\n";
3584 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3585 close $fd;
3586 print "</div>\n"; # class="page_body"
3587 git_footer_html();
3589 } elsif ($format eq 'plain') {
3590 local $/ = undef;
3591 print <$fd>;
3592 close $fd
3593 or print "Reading git-diff-tree failed\n";
3597 sub git_commitdiff_plain {
3598 git_commitdiff('plain');
3601 sub git_history {
3602 if (!defined $hash_base) {
3603 $hash_base = git_get_head_hash($project);
3605 if (!defined $page) {
3606 $page = 0;
3608 my $ftype;
3609 my %co = parse_commit($hash_base);
3610 if (!%co) {
3611 die_error(undef, "Unknown commit object");
3614 my $refs = git_get_references();
3615 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3617 if (!defined $hash && defined $file_name) {
3618 $hash = git_get_hash_by_path($hash_base, $file_name);
3620 if (defined $hash) {
3621 $ftype = git_get_type($hash);
3624 open my $fd, "-|",
3625 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3626 or die_error(undef, "Open git-rev-list-failed");
3627 my @revlist = map { chomp; $_ } <$fd>;
3628 close $fd
3629 or die_error(undef, "Reading git-rev-list failed");
3631 my $paging_nav = '';
3632 if ($page > 0) {
3633 $paging_nav .=
3634 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3635 file_name=>$file_name)},
3636 "first");
3637 $paging_nav .= " &sdot; " .
3638 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3639 file_name=>$file_name, page=>$page-1),
3640 -accesskey => "p", -title => "Alt-p"}, "prev");
3641 } else {
3642 $paging_nav .= "first";
3643 $paging_nav .= " &sdot; prev";
3645 if ($#revlist >= (100 * ($page+1)-1)) {
3646 $paging_nav .= " &sdot; " .
3647 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3648 file_name=>$file_name, page=>$page+1),
3649 -accesskey => "n", -title => "Alt-n"}, "next");
3650 } else {
3651 $paging_nav .= " &sdot; next";
3653 my $next_link = '';
3654 if ($#revlist >= (100 * ($page+1)-1)) {
3655 $next_link =
3656 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3657 file_name=>$file_name, page=>$page+1),
3658 -title => "Alt-n"}, "next");
3661 git_header_html();
3662 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3663 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3664 git_print_page_path($file_name, $ftype, $hash_base);
3666 git_history_body(\@revlist, ($page * 100), $#revlist,
3667 $refs, $hash_base, $ftype, $next_link);
3669 git_footer_html();
3672 sub git_search {
3673 if (!defined $searchtext) {
3674 die_error(undef, "Text field empty");
3676 if (!defined $hash) {
3677 $hash = git_get_head_hash($project);
3679 my %co = parse_commit($hash);
3680 if (!%co) {
3681 die_error(undef, "Unknown commit object");
3684 $searchtype ||= 'commit';
3685 if ($searchtype eq 'pickaxe') {
3686 # pickaxe may take all resources of your box and run for several minutes
3687 # with every query - so decide by yourself how public you make this feature
3688 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3689 if (!$have_pickaxe) {
3690 die_error('403 Permission denied', "Permission denied");
3694 git_header_html();
3695 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3696 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3698 print "<table cellspacing=\"0\">\n";
3699 my $alternate = 1;
3700 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
3701 $/ = "\0";
3702 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3703 while (my $commit_text = <$fd>) {
3704 if (!grep m/$searchtext/i, $commit_text) {
3705 next;
3707 if ($searchtype eq 'author' && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3708 next;
3710 if ($searchtype eq 'committer' && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3711 next;
3713 my @commit_lines = split "\n", $commit_text;
3714 my %co = parse_commit(undef, \@commit_lines);
3715 if (!%co) {
3716 next;
3718 if ($alternate) {
3719 print "<tr class=\"dark\">\n";
3720 } else {
3721 print "<tr class=\"light\">\n";
3723 $alternate ^= 1;
3724 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3725 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3726 "<td>" .
3727 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3728 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3729 my $comment = $co{'comment'};
3730 foreach my $line (@$comment) {
3731 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3732 my $lead = esc_html($1) || "";
3733 $lead = chop_str($lead, 30, 10);
3734 my $match = esc_html($2) || "";
3735 my $trail = esc_html($3) || "";
3736 $trail = chop_str($trail, 30, 10);
3737 my $text = "$lead<span class=\"match\">$match</span>$trail";
3738 print chop_str($text, 80, 5) . "<br/>\n";
3741 print "</td>\n" .
3742 "<td class=\"link\">" .
3743 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3744 " | " .
3745 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3746 print "</td>\n" .
3747 "</tr>\n";
3749 close $fd;
3752 if ($searchtype eq 'pickaxe') {
3753 $/ = "\n";
3754 my $git_command = git_cmd_str();
3755 open my $fd, "-|", "$git_command rev-list $hash | " .
3756 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3757 undef %co;
3758 my @files;
3759 while (my $line = <$fd>) {
3760 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3761 my %set;
3762 $set{'file'} = $6;
3763 $set{'from_id'} = $3;
3764 $set{'to_id'} = $4;
3765 $set{'id'} = $set{'to_id'};
3766 if ($set{'id'} =~ m/0{40}/) {
3767 $set{'id'} = $set{'from_id'};
3769 if ($set{'id'} =~ m/0{40}/) {
3770 next;
3772 push @files, \%set;
3773 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3774 if (%co) {
3775 if ($alternate) {
3776 print "<tr class=\"dark\">\n";
3777 } else {
3778 print "<tr class=\"light\">\n";
3780 $alternate ^= 1;
3781 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3782 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3783 "<td>" .
3784 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3785 -class => "list subject"},
3786 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3787 while (my $setref = shift @files) {
3788 my %set = %$setref;
3789 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3790 hash=>$set{'id'}, file_name=>$set{'file'}),
3791 -class => "list"},
3792 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3793 "<br/>\n";
3795 print "</td>\n" .
3796 "<td class=\"link\">" .
3797 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3798 " | " .
3799 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3800 print "</td>\n" .
3801 "</tr>\n";
3803 %co = parse_commit($1);
3806 close $fd;
3808 print "</table>\n";
3809 git_footer_html();
3812 sub git_search_help {
3813 git_header_html();
3814 git_print_page_nav('','', $hash,$hash,$hash);
3815 print <<EOT;
3816 <dl>
3817 <dt><b>commit</b></dt>
3818 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
3819 <dt><b>author</b></dt>
3820 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
3821 <dt><b>committer</b></dt>
3822 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
3824 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3825 if ($have_pickaxe) {
3826 print <<EOT;
3827 <dt><b>pickaxe</b></dt>
3828 <dd>All commits that caused the string to appear or disappear from any file (changes that
3829 added, removed or "modified" the string) will be listed. This search can take a while and
3830 takes a lot of strain on the server, so please use it wisely.</dd>
3833 print "</dl>\n";
3834 git_footer_html();
3837 sub git_shortlog {
3838 my $head = git_get_head_hash($project);
3839 if (!defined $hash) {
3840 $hash = $head;
3842 if (!defined $page) {
3843 $page = 0;
3845 my $refs = git_get_references();
3847 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3848 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3849 or die_error(undef, "Open git-rev-list failed");
3850 my @revlist = map { chomp; $_ } <$fd>;
3851 close $fd;
3853 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3854 my $next_link = '';
3855 if ($#revlist >= (100 * ($page+1)-1)) {
3856 $next_link =
3857 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3858 -title => "Alt-n"}, "next");
3862 git_header_html();
3863 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3864 git_print_header_div('summary', $project);
3866 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3868 git_footer_html();
3871 ## ......................................................................
3872 ## feeds (RSS, OPML)
3874 sub git_rss {
3875 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3876 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3877 or die_error(undef, "Open git-rev-list failed");
3878 my @revlist = map { chomp; $_ } <$fd>;
3879 close $fd or die_error(undef, "Reading git-rev-list failed");
3880 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3881 print <<XML;
3882 <?xml version="1.0" encoding="utf-8"?>
3883 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3884 <channel>
3885 <title>$project $my_uri $my_url</title>
3886 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3887 <description>$project log</description>
3888 <language>en</language>
3891 for (my $i = 0; $i <= $#revlist; $i++) {
3892 my $commit = $revlist[$i];
3893 my %co = parse_commit($commit);
3894 # we read 150, we always show 30 and the ones more recent than 48 hours
3895 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3896 last;
3898 my %cd = parse_date($co{'committer_epoch'});
3899 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3900 $co{'parent'}, $co{'id'}
3901 or next;
3902 my @difftree = map { chomp; $_ } <$fd>;
3903 close $fd
3904 or next;
3905 print "<item>\n" .
3906 "<title>" .
3907 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3908 "</title>\n" .
3909 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3910 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3911 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3912 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3913 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3914 "<content:encoded>" .
3915 "<![CDATA[\n";
3916 my $comment = $co{'comment'};
3917 foreach my $line (@$comment) {
3918 $line = to_utf8($line);
3919 print "$line<br/>\n";
3921 print "<br/>\n";
3922 foreach my $line (@difftree) {
3923 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3924 next;
3926 my $file = esc_html(unquote($7));
3927 $file = to_utf8($file);
3928 print "$file<br/>\n";
3930 print "]]>\n" .
3931 "</content:encoded>\n" .
3932 "</item>\n";
3934 print "</channel></rss>";
3937 sub git_opml {
3938 my @list = git_get_projects_list();
3940 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3941 print <<XML;
3942 <?xml version="1.0" encoding="utf-8"?>
3943 <opml version="1.0">
3944 <head>
3945 <title>$site_name OPML Export</title>
3946 </head>
3947 <body>
3948 <outline text="git RSS feeds">
3951 foreach my $pr (@list) {
3952 my %proj = %$pr;
3953 my $head = git_get_head_hash($proj{'path'});
3954 if (!defined $head) {
3955 next;
3957 $git_dir = "$projectroot/$proj{'path'}";
3958 my %co = parse_commit($head);
3959 if (!%co) {
3960 next;
3963 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3964 my $rss = "$my_url?p=$proj{'path'};a=rss";
3965 my $html = "$my_url?p=$proj{'path'};a=summary";
3966 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3968 print <<XML;
3969 </outline>
3970 </body>
3971 </opml>