gitweb: Restore object-named links in item lists
[git/gitweb.git] / gitweb / gitweb.perl
blob5aed66185049339125fcbecee62d9d42d5a8b5f3
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++" || $ENV{'SERVER_NAME'} || "Untitled";
44 # filename of html text to include at top of each page
45 our $site_header = "++GITWEB_SITE_HEADER++";
46 # html text to include at home page
47 our $home_text = "++GITWEB_HOMETEXT++";
48 # filename of html text to include at bottom of each page
49 our $site_footer = "++GITWEB_SITE_FOOTER++";
51 # URI of stylesheets
52 our @stylesheets = ("++GITWEB_CSS++");
53 our $stylesheet;
54 # default is not to define style sheet, but it can be overwritten later
55 undef $stylesheet;
57 # URI of default stylesheet
58 our $stylesheet = "++GITWEB_CSS++";
59 # URI of GIT logo (72x27 size)
60 our $logo = "++GITWEB_LOGO++";
61 # URI of GIT favicon, assumed to be image/png type
62 our $favicon = "++GITWEB_FAVICON++";
64 # URI and label (title) of GIT logo link
65 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
66 #our $logo_label = "git documentation";
67 our $logo_url = "http://git.or.cz/";
68 our $logo_label = "git homepage";
70 # source of projects list
71 our $projects_list = "++GITWEB_LIST++";
73 # show repository only if this file exists
74 # (only effective if this variable evaluates to true)
75 our $export_ok = "++GITWEB_EXPORT_OK++";
77 # only allow viewing of repositories also shown on the overview page
78 our $strict_export = "++GITWEB_STRICT_EXPORT++";
80 # list of git base URLs used for URL to where fetch project from,
81 # i.e. full URL is "$git_base_url/$project"
82 our @git_base_url_list = ("++GITWEB_BASE_URL++");
84 # default blob_plain mimetype and default charset for text/plain blob
85 our $default_blob_plain_mimetype = 'text/plain';
86 our $default_text_plain_charset = undef;
88 # file to use for guessing MIME types before trying /etc/mime.types
89 # (relative to the current git repository)
90 our $mimetypes_file = undef;
92 # You define site-wide feature defaults here; override them with
93 # $GITWEB_CONFIG as necessary.
94 our %feature = (
95 # feature => {
96 # 'sub' => feature-sub (subroutine),
97 # 'override' => allow-override (boolean),
98 # 'default' => [ default options...] (array reference)}
100 # if feature is overridable (it means that allow-override has true value,
101 # then feature-sub will be called with default options as parameters;
102 # return value of feature-sub indicates if to enable specified feature
104 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
106 # Enable the 'blame' blob view, showing the last commit that modified
107 # each line in the file. This can be very CPU-intensive.
109 # To enable system wide have in $GITWEB_CONFIG
110 # $feature{'blame'}{'default'} = [1];
111 # To have project specific config enable override in $GITWEB_CONFIG
112 # $feature{'blame'}{'override'} = 1;
113 # and in project config gitweb.blame = 0|1;
114 'blame' => {
115 'sub' => \&feature_blame,
116 'override' => 0,
117 'default' => [0]},
119 # Enable the 'snapshot' link, providing a compressed tarball of any
120 # tree. This can potentially generate high traffic if you have large
121 # project.
123 # To disable system wide have in $GITWEB_CONFIG
124 # $feature{'snapshot'}{'default'} = [undef];
125 # To have project specific config enable override in $GITWEB_CONFIG
126 # $feature{'blame'}{'override'} = 1;
127 # and in project config gitweb.snapshot = none|gzip|bzip2;
128 'snapshot' => {
129 'sub' => \&feature_snapshot,
130 'override' => 0,
131 # => [content-encoding, suffix, program]
132 'default' => ['x-gzip', 'gz', 'gzip']},
134 # Enable the pickaxe search, which will list the commits that modified
135 # a given string in a file. This can be practical and quite faster
136 # alternative to 'blame', but still potentially CPU-intensive.
138 # To enable system wide have in $GITWEB_CONFIG
139 # $feature{'pickaxe'}{'default'} = [1];
140 # To have project specific config enable override in $GITWEB_CONFIG
141 # $feature{'pickaxe'}{'override'} = 1;
142 # and in project config gitweb.pickaxe = 0|1;
143 'pickaxe' => {
144 'sub' => \&feature_pickaxe,
145 'override' => 0,
146 'default' => [1]},
148 # Make gitweb use an alternative format of the URLs which can be
149 # more readable and natural-looking: project name is embedded
150 # directly in the path and the query string contains other
151 # auxiliary information. All gitweb installations recognize
152 # URL in either format; this configures in which formats gitweb
153 # generates links.
155 # To enable system wide have in $GITWEB_CONFIG
156 # $feature{'pathinfo'}{'default'} = [1];
157 # Project specific override is not supported.
159 # Note that you will need to change the default location of CSS,
160 # favicon, logo and possibly other files to an absolute URL. Also,
161 # if gitweb.cgi serves as your indexfile, you will need to force
162 # $my_uri to contain the script name in your $GITWEB_CONFIG.
163 'pathinfo' => {
164 'override' => 0,
165 'default' => [0]},
168 sub gitweb_check_feature {
169 my ($name) = @_;
170 return unless exists $feature{$name};
171 my ($sub, $override, @defaults) = (
172 $feature{$name}{'sub'},
173 $feature{$name}{'override'},
174 @{$feature{$name}{'default'}});
175 if (!$override) { return @defaults; }
176 if (!defined $sub) {
177 warn "feature $name is not overrideable";
178 return @defaults;
180 return $sub->(@defaults);
183 sub feature_blame {
184 my ($val) = git_get_project_config('blame', '--bool');
186 if ($val eq 'true') {
187 return 1;
188 } elsif ($val eq 'false') {
189 return 0;
192 return $_[0];
195 sub feature_snapshot {
196 my ($ctype, $suffix, $command) = @_;
198 my ($val) = git_get_project_config('snapshot');
200 if ($val eq 'gzip') {
201 return ('x-gzip', 'gz', 'gzip');
202 } elsif ($val eq 'bzip2') {
203 return ('x-bzip2', 'bz2', 'bzip2');
204 } elsif ($val eq 'none') {
205 return ();
208 return ($ctype, $suffix, $command);
211 sub gitweb_have_snapshot {
212 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
213 my $have_snapshot = (defined $ctype && defined $suffix);
215 return $have_snapshot;
218 sub feature_pickaxe {
219 my ($val) = git_get_project_config('pickaxe', '--bool');
221 if ($val eq 'true') {
222 return (1);
223 } elsif ($val eq 'false') {
224 return (0);
227 return ($_[0]);
230 # checking HEAD file with -e is fragile if the repository was
231 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
232 # and then pruned.
233 sub check_head_link {
234 my ($dir) = @_;
235 my $headfile = "$dir/HEAD";
236 return ((-e $headfile) ||
237 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
240 sub check_export_ok {
241 my ($dir) = @_;
242 return (check_head_link($dir) &&
243 (!$export_ok || -e "$dir/$export_ok"));
246 # rename detection options for git-diff and git-diff-tree
247 # - default is '-M', with the cost proportional to
248 # (number of removed files) * (number of new files).
249 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
250 # (number of changed files + number of removed files) * (number of new files)
251 # - even more costly is '-C', '--find-copies-harder' with cost
252 # (number of files in the original tree) * (number of new files)
253 # - one might want to include '-B' option, e.g. '-B', '-M'
254 our @diff_opts = ('-M'); # taken from git_commit
256 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
257 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
259 # version of the core git binary
260 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
262 $projects_list ||= $projectroot;
264 # ======================================================================
265 # input validation and dispatch
266 our $action = $cgi->param('a');
267 if (defined $action) {
268 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
269 die_error(undef, "Invalid action parameter");
273 # parameters which are pathnames
274 our $project = $cgi->param('p');
275 if (defined $project) {
276 if (!validate_pathname($project) ||
277 !(-d "$projectroot/$project") ||
278 !check_head_link("$projectroot/$project") ||
279 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
280 ($strict_export && !project_in_list($project))) {
281 undef $project;
282 die_error(undef, "No such project");
286 our $file_name = $cgi->param('f');
287 if (defined $file_name) {
288 if (!validate_pathname($file_name)) {
289 die_error(undef, "Invalid file parameter");
293 our $file_parent = $cgi->param('fp');
294 if (defined $file_parent) {
295 if (!validate_pathname($file_parent)) {
296 die_error(undef, "Invalid file parent parameter");
300 # parameters which are refnames
301 our $hash = $cgi->param('h');
302 if (defined $hash) {
303 if (!validate_refname($hash)) {
304 die_error(undef, "Invalid hash parameter");
308 our $hash_parent = $cgi->param('hp');
309 if (defined $hash_parent) {
310 if (!validate_refname($hash_parent)) {
311 die_error(undef, "Invalid hash parent parameter");
315 our $hash_base = $cgi->param('hb');
316 if (defined $hash_base) {
317 if (!validate_refname($hash_base)) {
318 die_error(undef, "Invalid hash base parameter");
322 our $hash_parent_base = $cgi->param('hpb');
323 if (defined $hash_parent_base) {
324 if (!validate_refname($hash_parent_base)) {
325 die_error(undef, "Invalid hash parent base parameter");
329 # other parameters
330 our $page = $cgi->param('pg');
331 if (defined $page) {
332 if ($page =~ m/[^0-9]/) {
333 die_error(undef, "Invalid page parameter");
337 our $searchtext = $cgi->param('s');
338 if (defined $searchtext) {
339 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
340 die_error(undef, "Invalid search parameter");
342 $searchtext = quotemeta $searchtext;
345 # now read PATH_INFO and use it as alternative to parameters
346 sub evaluate_path_info {
347 return if defined $project;
348 my $path_info = $ENV{"PATH_INFO"};
349 return if !$path_info;
350 $path_info =~ s,^/+,,;
351 return if !$path_info;
352 # find which part of PATH_INFO is project
353 $project = $path_info;
354 $project =~ s,/+$,,;
355 while ($project && !check_head_link("$projectroot/$project")) {
356 $project =~ s,/*[^/]*$,,;
358 # validate project
359 $project = validate_pathname($project);
360 if (!$project ||
361 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
362 ($strict_export && !project_in_list($project))) {
363 undef $project;
364 return;
366 # do not change any parameters if an action is given using the query string
367 return if $action;
368 $path_info =~ s,^$project/*,,;
369 my ($refname, $pathname) = split(/:/, $path_info, 2);
370 if (defined $pathname) {
371 # we got "project.git/branch:filename" or "project.git/branch:dir/"
372 # we could use git_get_type(branch:pathname), but it needs $git_dir
373 $pathname =~ s,^/+,,;
374 if (!$pathname || substr($pathname, -1) eq "/") {
375 $action ||= "tree";
376 $pathname =~ s,/$,,;
377 } else {
378 $action ||= "blob_plain";
380 $hash_base ||= validate_refname($refname);
381 $file_name ||= validate_pathname($pathname);
382 } elsif (defined $refname) {
383 # we got "project.git/branch"
384 $action ||= "shortlog";
385 $hash ||= validate_refname($refname);
388 evaluate_path_info();
390 # path to the current git repository
391 our $git_dir;
392 $git_dir = "$projectroot/$project" if $project;
394 # dispatch
395 my %actions = (
396 "blame" => \&git_blame2,
397 "blobdiff" => \&git_blobdiff,
398 "blobdiff_plain" => \&git_blobdiff_plain,
399 "blob" => \&git_blob,
400 "blob_plain" => \&git_blob_plain,
401 "commitdiff" => \&git_commitdiff,
402 "commitdiff_plain" => \&git_commitdiff_plain,
403 "commit" => \&git_commit,
404 "heads" => \&git_heads,
405 "history" => \&git_history,
406 "log" => \&git_log,
407 "rss" => \&git_rss,
408 "search" => \&git_search,
409 "shortlog" => \&git_shortlog,
410 "summary" => \&git_summary,
411 "tag" => \&git_tag,
412 "tags" => \&git_tags,
413 "tree" => \&git_tree,
414 "snapshot" => \&git_snapshot,
415 # those below don't need $project
416 "opml" => \&git_opml,
417 "project_list" => \&git_project_list,
418 "project_index" => \&git_project_index,
421 if (defined $project) {
422 $action ||= 'summary';
423 } else {
424 $action ||= 'project_list';
426 if (!defined($actions{$action})) {
427 die_error(undef, "Unknown action");
429 if ($action !~ m/^(opml|project_list|project_index)$/ &&
430 !$project) {
431 die_error(undef, "Project needed");
433 $actions{$action}->();
434 exit;
436 ## ======================================================================
437 ## action links
439 sub href(%) {
440 my %params = @_;
441 my $href = $my_uri;
443 # XXX: Warning: If you touch this, check the search form for updating,
444 # too.
446 my @mapping = (
447 project => "p",
448 action => "a",
449 file_name => "f",
450 file_parent => "fp",
451 hash => "h",
452 hash_parent => "hp",
453 hash_base => "hb",
454 hash_parent_base => "hpb",
455 page => "pg",
456 order => "o",
457 searchtext => "s",
459 my %mapping = @mapping;
461 $params{'project'} = $project unless exists $params{'project'};
463 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
464 if ($use_pathinfo) {
465 # use PATH_INFO for project name
466 $href .= "/$params{'project'}" if defined $params{'project'};
467 delete $params{'project'};
469 # Summary just uses the project path URL
470 if (defined $params{'action'} && $params{'action'} eq 'summary') {
471 delete $params{'action'};
475 # now encode the parameters explicitly
476 my @result = ();
477 for (my $i = 0; $i < @mapping; $i += 2) {
478 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
479 if (defined $params{$name}) {
480 push @result, $symbol . "=" . esc_param($params{$name});
483 $href .= "?" . join(';', @result) if scalar @result;
485 return $href;
489 ## ======================================================================
490 ## validation, quoting/unquoting and escaping
492 sub validate_pathname {
493 my $input = shift || return undef;
495 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
496 # at the beginning, at the end, and between slashes.
497 # also this catches doubled slashes
498 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
499 return undef;
501 # no null characters
502 if ($input =~ m!\0!) {
503 return undef;
505 return $input;
508 sub validate_refname {
509 my $input = shift || return undef;
511 # textual hashes are O.K.
512 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
513 return $input;
515 # it must be correct pathname
516 $input = validate_pathname($input)
517 or return undef;
518 # restrictions on ref name according to git-check-ref-format
519 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
520 return undef;
522 return $input;
525 # very thin wrapper for decode("utf8", $str, Encode::FB_DEFAULT);
526 sub to_utf8 {
527 my $str = shift;
528 return decode("utf8", $str, Encode::FB_DEFAULT);
531 # quote unsafe chars, but keep the slash, even when it's not
532 # correct, but quoted slashes look too horrible in bookmarks
533 sub esc_param {
534 my $str = shift;
535 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
536 $str =~ s/\+/%2B/g;
537 $str =~ s/ /\+/g;
538 return $str;
541 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
542 sub esc_url {
543 my $str = shift;
544 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
545 $str =~ s/\+/%2B/g;
546 $str =~ s/ /\+/g;
547 return $str;
550 # replace invalid utf8 character with SUBSTITUTION sequence
551 sub esc_html {
552 my $str = shift;
553 $str = to_utf8($str);
554 $str = escapeHTML($str);
555 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
556 $str =~ s/\033/^[/g; # "escape" ESCAPE (\e) character (e.g. commit 20a3847d8a5032ce41f90dcc68abfb36e6fee9b1)
557 return $str;
560 # git may return quoted and escaped filenames
561 sub unquote {
562 my $str = shift;
563 if ($str =~ m/^"(.*)"$/) {
564 $str = $1;
565 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
567 return $str;
570 # escape tabs (convert tabs to spaces)
571 sub untabify {
572 my $line = shift;
574 while ((my $pos = index($line, "\t")) != -1) {
575 if (my $count = (8 - ($pos % 8))) {
576 my $spaces = ' ' x $count;
577 $line =~ s/\t/$spaces/;
581 return $line;
584 sub project_in_list {
585 my $project = shift;
586 my @list = git_get_projects_list();
587 return @list && scalar(grep { $_->{'path'} eq $project } @list);
590 ## ----------------------------------------------------------------------
591 ## HTML aware string manipulation
593 sub chop_str {
594 my $str = shift;
595 my $len = shift;
596 my $add_len = shift || 10;
598 # allow only $len chars, but don't cut a word if it would fit in $add_len
599 # if it doesn't fit, cut it if it's still longer than the dots we would add
600 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
601 my $body = $1;
602 my $tail = $2;
603 if (length($tail) > 4) {
604 $tail = " ...";
605 $body =~ s/&[^;]*$//; # remove chopped character entities
607 return "$body$tail";
610 ## ----------------------------------------------------------------------
611 ## functions returning short strings
613 # CSS class for given age value (in seconds)
614 sub age_class {
615 my $age = shift;
617 if ($age < 60*60*2) {
618 return "age0";
619 } elsif ($age < 60*60*24*2) {
620 return "age1";
621 } else {
622 return "age2";
626 # convert age in seconds to "nn units ago" string
627 sub age_string {
628 my $age = shift;
629 my $age_str;
631 if ($age > 60*60*24*365*2) {
632 $age_str = (int $age/60/60/24/365);
633 $age_str .= " years ago";
634 } elsif ($age > 60*60*24*(365/12)*2) {
635 $age_str = int $age/60/60/24/(365/12);
636 $age_str .= " months ago";
637 } elsif ($age > 60*60*24*7*2) {
638 $age_str = int $age/60/60/24/7;
639 $age_str .= " weeks ago";
640 } elsif ($age > 60*60*24*2) {
641 $age_str = int $age/60/60/24;
642 $age_str .= " days ago";
643 } elsif ($age > 60*60*2) {
644 $age_str = int $age/60/60;
645 $age_str .= " hours ago";
646 } elsif ($age > 60*2) {
647 $age_str = int $age/60;
648 $age_str .= " min ago";
649 } elsif ($age > 2) {
650 $age_str = int $age;
651 $age_str .= " sec ago";
652 } else {
653 $age_str .= " right now";
655 return $age_str;
658 # convert file mode in octal to symbolic file mode string
659 sub mode_str {
660 my $mode = oct shift;
662 if (S_ISDIR($mode & S_IFMT)) {
663 return 'drwxr-xr-x';
664 } elsif (S_ISLNK($mode)) {
665 return 'lrwxrwxrwx';
666 } elsif (S_ISREG($mode)) {
667 # git cares only about the executable bit
668 if ($mode & S_IXUSR) {
669 return '-rwxr-xr-x';
670 } else {
671 return '-rw-r--r--';
673 } else {
674 return '----------';
678 # convert file mode in octal to file type string
679 sub file_type {
680 my $mode = shift;
682 if ($mode !~ m/^[0-7]+$/) {
683 return $mode;
684 } else {
685 $mode = oct $mode;
688 if (S_ISDIR($mode & S_IFMT)) {
689 return "directory";
690 } elsif (S_ISLNK($mode)) {
691 return "symlink";
692 } elsif (S_ISREG($mode)) {
693 return "file";
694 } else {
695 return "unknown";
699 ## ----------------------------------------------------------------------
700 ## functions returning short HTML fragments, or transforming HTML fragments
701 ## which don't beling to other sections
703 # format line of commit message or tag comment
704 sub format_log_line_html {
705 my $line = shift;
707 $line = esc_html($line);
708 $line =~ s/ /&nbsp;/g;
709 if ($line =~ m/([0-9a-fA-F]{40})/) {
710 my $hash_text = $1;
711 if (git_get_type($hash_text) eq "commit") {
712 my $link =
713 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
714 -class => "text"}, $hash_text);
715 $line =~ s/$hash_text/$link/;
718 return $line;
721 # format marker of refs pointing to given object
722 sub format_ref_marker {
723 my ($refs, $id) = @_;
724 my $markers = '';
726 if (defined $refs->{$id}) {
727 foreach my $ref (@{$refs->{$id}}) {
728 my ($type, $name) = qw();
729 # e.g. tags/v2.6.11 or heads/next
730 if ($ref =~ m!^(.*?)s?/(.*)$!) {
731 $type = $1;
732 $name = $2;
733 } else {
734 $type = "ref";
735 $name = $ref;
738 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
742 if ($markers) {
743 return ' <span class="refs">'. $markers . '</span>';
744 } else {
745 return "";
749 # format, perhaps shortened and with markers, title line
750 sub format_subject_html {
751 my ($long, $short, $href, $extra) = @_;
752 $extra = '' unless defined($extra);
754 if (length($short) < length($long)) {
755 return $cgi->a({-href => $href, -class => "list subject",
756 -title => to_utf8($long)},
757 esc_html($short) . $extra);
758 } else {
759 return $cgi->a({-href => $href, -class => "list subject"},
760 esc_html($long) . $extra);
764 sub format_diff_line {
765 my $line = shift;
766 my $char = substr($line, 0, 1);
767 my $diff_class = "";
769 chomp $line;
771 if ($char eq '+') {
772 $diff_class = " add";
773 } elsif ($char eq "-") {
774 $diff_class = " rem";
775 } elsif ($char eq "@") {
776 $diff_class = " chunk_header";
777 } elsif ($char eq "\\") {
778 $diff_class = " incomplete";
780 $line = untabify($line);
781 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
784 ## ----------------------------------------------------------------------
785 ## git utility subroutines, invoking git commands
787 # returns path to the core git executable and the --git-dir parameter as list
788 sub git_cmd {
789 return $GIT, '--git-dir='.$git_dir;
792 # returns path to the core git executable and the --git-dir parameter as string
793 sub git_cmd_str {
794 return join(' ', git_cmd());
797 # get HEAD ref of given project as hash
798 sub git_get_head_hash {
799 my $project = shift;
800 my $o_git_dir = $git_dir;
801 my $retval = undef;
802 $git_dir = "$projectroot/$project";
803 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
804 my $head = <$fd>;
805 close $fd;
806 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
807 $retval = $1;
810 if (defined $o_git_dir) {
811 $git_dir = $o_git_dir;
813 return $retval;
816 # get type of given object
817 sub git_get_type {
818 my $hash = shift;
820 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
821 my $type = <$fd>;
822 close $fd or return;
823 chomp $type;
824 return $type;
827 sub git_get_project_config {
828 my ($key, $type) = @_;
830 return unless ($key);
831 $key =~ s/^gitweb\.//;
832 return if ($key =~ m/\W/);
834 my @x = (git_cmd(), 'repo-config');
835 if (defined $type) { push @x, $type; }
836 push @x, "--get";
837 push @x, "gitweb.$key";
838 my $val = qx(@x);
839 chomp $val;
840 return ($val);
843 # get hash of given path at given ref
844 sub git_get_hash_by_path {
845 my $base = shift;
846 my $path = shift || return undef;
847 my $type = shift;
849 $path =~ s,/+$,,;
851 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
852 or die_error(undef, "Open git-ls-tree failed");
853 my $line = <$fd>;
854 close $fd or return undef;
856 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
857 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
858 if (defined $type && $type ne $2) {
859 # type doesn't match
860 return undef;
862 return $3;
865 ## ......................................................................
866 ## git utility functions, directly accessing git repository
868 sub git_get_project_description {
869 my $path = shift;
871 open my $fd, "$projectroot/$path/description" or return undef;
872 my $descr = <$fd>;
873 close $fd;
874 chomp $descr;
875 return $descr;
878 sub git_get_project_url_list {
879 my $path = shift;
881 open my $fd, "$projectroot/$path/cloneurl" or return;
882 my @git_project_url_list = map { chomp; $_ } <$fd>;
883 close $fd;
885 return wantarray ? @git_project_url_list : \@git_project_url_list;
888 sub git_get_projects_list {
889 my @list;
891 if (-d $projects_list) {
892 # search in directory
893 my $dir = $projects_list;
894 my $pfxlen = length("$dir");
896 File::Find::find({
897 follow_fast => 1, # follow symbolic links
898 dangling_symlinks => 0, # ignore dangling symlinks, silently
899 wanted => sub {
900 # skip project-list toplevel, if we get it.
901 return if (m!^[/.]$!);
902 # only directories can be git repositories
903 return unless (-d $_);
905 my $subdir = substr($File::Find::name, $pfxlen + 1);
906 # we check related file in $projectroot
907 if (check_export_ok("$projectroot/$subdir")) {
908 push @list, { path => $subdir };
909 $File::Find::prune = 1;
912 }, "$dir");
914 } elsif (-f $projects_list) {
915 # read from file(url-encoded):
916 # 'git%2Fgit.git Linus+Torvalds'
917 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
918 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
919 open my ($fd), $projects_list or return;
920 while (my $line = <$fd>) {
921 chomp $line;
922 my ($path, $owner) = split ' ', $line;
923 $path = unescape($path);
924 $owner = unescape($owner);
925 if (!defined $path) {
926 next;
928 if (check_export_ok("$projectroot/$path")) {
929 my $pr = {
930 path => $path,
931 owner => to_utf8($owner),
933 push @list, $pr
936 close $fd;
938 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
939 return @list;
942 sub git_get_project_owner {
943 my $project = shift;
944 my $owner;
946 return undef unless $project;
948 # read from file (url-encoded):
949 # 'git%2Fgit.git Linus+Torvalds'
950 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
951 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
952 if (-f $projects_list) {
953 open (my $fd , $projects_list);
954 while (my $line = <$fd>) {
955 chomp $line;
956 my ($pr, $ow) = split ' ', $line;
957 $pr = unescape($pr);
958 $ow = unescape($ow);
959 if ($pr eq $project) {
960 $owner = to_utf8($ow);
961 last;
964 close $fd;
966 if (!defined $owner) {
967 $owner = get_file_owner("$projectroot/$project");
970 return $owner;
973 sub git_get_references {
974 my $type = shift || "";
975 my %refs;
976 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
977 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
978 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
979 or return;
981 while (my $line = <$fd>) {
982 chomp $line;
983 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
984 if (defined $refs{$1}) {
985 push @{$refs{$1}}, $2;
986 } else {
987 $refs{$1} = [ $2 ];
991 close $fd or return;
992 return \%refs;
995 sub git_get_rev_name_tags {
996 my $hash = shift || return undef;
998 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
999 or return;
1000 my $name_rev = <$fd>;
1001 close $fd;
1003 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1004 return $1;
1005 } else {
1006 # catches also '$hash undefined' output
1007 return undef;
1011 ## ----------------------------------------------------------------------
1012 ## parse to hash functions
1014 sub parse_date {
1015 my $epoch = shift;
1016 my $tz = shift || "-0000";
1018 my %date;
1019 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1020 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1021 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1022 $date{'hour'} = $hour;
1023 $date{'minute'} = $min;
1024 $date{'mday'} = $mday;
1025 $date{'day'} = $days[$wday];
1026 $date{'month'} = $months[$mon];
1027 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1028 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1029 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1030 $mday, $months[$mon], $hour ,$min;
1032 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1033 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1034 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1035 $date{'hour_local'} = $hour;
1036 $date{'minute_local'} = $min;
1037 $date{'tz_local'} = $tz;
1038 return %date;
1041 sub parse_tag {
1042 my $tag_id = shift;
1043 my %tag;
1044 my @comment;
1046 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1047 $tag{'id'} = $tag_id;
1048 while (my $line = <$fd>) {
1049 chomp $line;
1050 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1051 $tag{'object'} = $1;
1052 } elsif ($line =~ m/^type (.+)$/) {
1053 $tag{'type'} = $1;
1054 } elsif ($line =~ m/^tag (.+)$/) {
1055 $tag{'name'} = $1;
1056 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1057 $tag{'author'} = $1;
1058 $tag{'epoch'} = $2;
1059 $tag{'tz'} = $3;
1060 } elsif ($line =~ m/--BEGIN/) {
1061 push @comment, $line;
1062 last;
1063 } elsif ($line eq "") {
1064 last;
1067 push @comment, <$fd>;
1068 $tag{'comment'} = \@comment;
1069 close $fd or return;
1070 if (!defined $tag{'name'}) {
1071 return
1073 return %tag
1076 sub git_get_last_activity {
1077 my ($path) = @_;
1078 my $fd;
1080 $git_dir = "$projectroot/$path";
1081 open($fd, "-|", git_cmd(), 'for-each-ref',
1082 '--format=%(refname) %(committer)',
1083 '--sort=-committerdate',
1084 'refs/heads') or return;
1085 my $most_recent = <$fd>;
1086 close $fd or return;
1087 if ($most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1088 my $timestamp = $1;
1089 my $age = time - $timestamp;
1090 return ($age, age_string($age));
1094 sub parse_commit {
1095 my $commit_id = shift;
1096 my $commit_text = shift;
1098 my @commit_lines;
1099 my %co;
1101 if (defined $commit_text) {
1102 @commit_lines = @$commit_text;
1103 } else {
1104 local $/ = "\0";
1105 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
1106 or return;
1107 @commit_lines = split '\n', <$fd>;
1108 close $fd or return;
1109 pop @commit_lines;
1111 my $header = shift @commit_lines;
1112 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1113 return;
1115 ($co{'id'}, my @parents) = split ' ', $header;
1116 $co{'parents'} = \@parents;
1117 $co{'parent'} = $parents[0];
1118 while (my $line = shift @commit_lines) {
1119 last if $line eq "\n";
1120 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1121 $co{'tree'} = $1;
1122 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1123 $co{'author'} = $1;
1124 $co{'author_epoch'} = $2;
1125 $co{'author_tz'} = $3;
1126 if ($co{'author'} =~ m/^([^<]+) </) {
1127 $co{'author_name'} = $1;
1128 } else {
1129 $co{'author_name'} = $co{'author'};
1131 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1132 $co{'committer'} = $1;
1133 $co{'committer_epoch'} = $2;
1134 $co{'committer_tz'} = $3;
1135 $co{'committer_name'} = $co{'committer'};
1136 $co{'committer_name'} =~ s/ <.*//;
1139 if (!defined $co{'tree'}) {
1140 return;
1143 foreach my $title (@commit_lines) {
1144 $title =~ s/^ //;
1145 if ($title ne "") {
1146 $co{'title'} = chop_str($title, 80, 5);
1147 # remove leading stuff of merges to make the interesting part visible
1148 if (length($title) > 50) {
1149 $title =~ s/^Automatic //;
1150 $title =~ s/^merge (of|with) /Merge ... /i;
1151 if (length($title) > 50) {
1152 $title =~ s/(http|rsync):\/\///;
1154 if (length($title) > 50) {
1155 $title =~ s/(master|www|rsync)\.//;
1157 if (length($title) > 50) {
1158 $title =~ s/kernel.org:?//;
1160 if (length($title) > 50) {
1161 $title =~ s/\/pub\/scm//;
1164 $co{'title_short'} = chop_str($title, 50, 5);
1165 last;
1168 if ($co{'title'} eq "") {
1169 $co{'title'} = $co{'title_short'} = '(no commit message)';
1171 # remove added spaces
1172 foreach my $line (@commit_lines) {
1173 $line =~ s/^ //;
1175 $co{'comment'} = \@commit_lines;
1177 my $age = time - $co{'committer_epoch'};
1178 $co{'age'} = $age;
1179 $co{'age_string'} = age_string($age);
1180 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1181 if ($age > 60*60*24*7*2) {
1182 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1183 $co{'age_string_age'} = $co{'age_string'};
1184 } else {
1185 $co{'age_string_date'} = $co{'age_string'};
1186 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1188 return %co;
1191 # parse ref from ref_file, given by ref_id, with given type
1192 sub parse_ref {
1193 my $ref_file = shift;
1194 my $ref_id = shift;
1195 my $type = shift || git_get_type($ref_id);
1196 my %ref_item;
1198 $ref_item{'type'} = $type;
1199 $ref_item{'id'} = $ref_id;
1200 $ref_item{'epoch'} = 0;
1201 $ref_item{'age'} = "unknown";
1202 if ($type eq "tag") {
1203 my %tag = parse_tag($ref_id);
1204 $ref_item{'comment'} = $tag{'comment'};
1205 if ($tag{'type'} eq "commit") {
1206 my %co = parse_commit($tag{'object'});
1207 $ref_item{'epoch'} = $co{'committer_epoch'};
1208 $ref_item{'age'} = $co{'age_string'};
1209 } elsif (defined($tag{'epoch'})) {
1210 my $age = time - $tag{'epoch'};
1211 $ref_item{'epoch'} = $tag{'epoch'};
1212 $ref_item{'age'} = age_string($age);
1214 $ref_item{'reftype'} = $tag{'type'};
1215 $ref_item{'name'} = $tag{'name'};
1216 $ref_item{'refid'} = $tag{'object'};
1217 } elsif ($type eq "commit"){
1218 my %co = parse_commit($ref_id);
1219 $ref_item{'reftype'} = "commit";
1220 $ref_item{'name'} = $ref_file;
1221 $ref_item{'title'} = $co{'title'};
1222 $ref_item{'refid'} = $ref_id;
1223 $ref_item{'epoch'} = $co{'committer_epoch'};
1224 $ref_item{'age'} = $co{'age_string'};
1225 } else {
1226 $ref_item{'reftype'} = $type;
1227 $ref_item{'name'} = $ref_file;
1228 $ref_item{'refid'} = $ref_id;
1231 return %ref_item;
1234 # parse line of git-diff-tree "raw" output
1235 sub parse_difftree_raw_line {
1236 my $line = shift;
1237 my %res;
1239 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1240 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1241 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1242 $res{'from_mode'} = $1;
1243 $res{'to_mode'} = $2;
1244 $res{'from_id'} = $3;
1245 $res{'to_id'} = $4;
1246 $res{'status'} = $5;
1247 $res{'similarity'} = $6;
1248 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1249 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1250 } else {
1251 $res{'file'} = unquote($7);
1254 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1255 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1256 $res{'commit'} = $1;
1259 return wantarray ? %res : \%res;
1262 # parse line of git-ls-tree output
1263 sub parse_ls_tree_line ($;%) {
1264 my $line = shift;
1265 my %opts = @_;
1266 my %res;
1268 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1269 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1271 $res{'mode'} = $1;
1272 $res{'type'} = $2;
1273 $res{'hash'} = $3;
1274 if ($opts{'-z'}) {
1275 $res{'name'} = $4;
1276 } else {
1277 $res{'name'} = unquote($4);
1280 return wantarray ? %res : \%res;
1283 ## ......................................................................
1284 ## parse to array of hashes functions
1286 sub git_get_refs_list {
1287 my $type = shift || "";
1288 my %refs;
1289 my @reflist;
1291 my @refs;
1292 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1293 or return;
1294 while (my $line = <$fd>) {
1295 chomp $line;
1296 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1297 if (defined $refs{$1}) {
1298 push @{$refs{$1}}, $2;
1299 } else {
1300 $refs{$1} = [ $2 ];
1303 if (! $4) { # unpeeled, direct reference
1304 push @refs, { hash => $1, name => $3 }; # without type
1305 } elsif ($3 eq $refs[-1]{'name'}) {
1306 # most likely a tag is followed by its peeled
1307 # (deref) one, and when that happens we know the
1308 # previous one was of type 'tag'.
1309 $refs[-1]{'type'} = "tag";
1313 close $fd;
1315 foreach my $ref (@refs) {
1316 my $ref_file = $ref->{'name'};
1317 my $ref_id = $ref->{'hash'};
1319 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1320 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1322 push @reflist, \%ref_item;
1324 # sort refs by age
1325 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1326 return (\@reflist, \%refs);
1329 ## ----------------------------------------------------------------------
1330 ## filesystem-related functions
1332 sub get_file_owner {
1333 my $path = shift;
1335 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1336 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1337 if (!defined $gcos) {
1338 return undef;
1340 my $owner = $gcos;
1341 $owner =~ s/[,;].*$//;
1342 return to_utf8($owner);
1345 ## ......................................................................
1346 ## mimetype related functions
1348 sub mimetype_guess_file {
1349 my $filename = shift;
1350 my $mimemap = shift;
1351 -r $mimemap or return undef;
1353 my %mimemap;
1354 open(MIME, $mimemap) or return undef;
1355 while (<MIME>) {
1356 next if m/^#/; # skip comments
1357 my ($mime, $exts) = split(/\t+/);
1358 if (defined $exts) {
1359 my @exts = split(/\s+/, $exts);
1360 foreach my $ext (@exts) {
1361 $mimemap{$ext} = $mime;
1365 close(MIME);
1367 $filename =~ /\.([^.]*)$/;
1368 return $mimemap{$1};
1371 sub mimetype_guess {
1372 my $filename = shift;
1373 my $mime;
1374 $filename =~ /\./ or return undef;
1376 if ($mimetypes_file) {
1377 my $file = $mimetypes_file;
1378 if ($file !~ m!^/!) { # if it is relative path
1379 # it is relative to project
1380 $file = "$projectroot/$project/$file";
1382 $mime = mimetype_guess_file($filename, $file);
1384 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1385 return $mime;
1388 sub blob_mimetype {
1389 my $fd = shift;
1390 my $filename = shift;
1392 if ($filename) {
1393 my $mime = mimetype_guess($filename);
1394 $mime and return $mime;
1397 # just in case
1398 return $default_blob_plain_mimetype unless $fd;
1400 if (-T $fd) {
1401 return 'text/plain' .
1402 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1403 } elsif (! $filename) {
1404 return 'application/octet-stream';
1405 } elsif ($filename =~ m/\.png$/i) {
1406 return 'image/png';
1407 } elsif ($filename =~ m/\.gif$/i) {
1408 return 'image/gif';
1409 } elsif ($filename =~ m/\.jpe?g$/i) {
1410 return 'image/jpeg';
1411 } else {
1412 return 'application/octet-stream';
1416 ## ======================================================================
1417 ## functions printing HTML: header, footer, error page
1419 sub git_header_html {
1420 my $status = shift || "200 OK";
1421 my $expires = shift;
1423 my $title = "$site_name git";
1424 if (defined $project) {
1425 $title .= " - $project";
1426 if (defined $action) {
1427 $title .= "/$action";
1428 if (defined $file_name) {
1429 $title .= " - " . esc_html($file_name);
1430 if ($action eq "tree" && $file_name !~ m|/$|) {
1431 $title .= "/";
1436 my $content_type;
1437 # require explicit support from the UA if we are to send the page as
1438 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1439 # we have to do this because MSIE sometimes globs '*/*', pretending to
1440 # support xhtml+xml but choking when it gets what it asked for.
1441 if (defined $cgi->http('HTTP_ACCEPT') &&
1442 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1443 $cgi->Accept('application/xhtml+xml') != 0) {
1444 $content_type = 'application/xhtml+xml';
1445 } else {
1446 $content_type = 'text/html';
1448 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1449 -status=> $status, -expires => $expires);
1450 print <<EOF;
1451 <?xml version="1.0" encoding="utf-8"?>
1452 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1453 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1454 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1455 <!-- git core binaries version $git_version -->
1456 <head>
1457 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1458 <meta name="generator" content="gitweb/$version git/$git_version"/>
1459 <meta name="robots" content="index, nofollow"/>
1460 <title>$title</title>
1462 # print out each stylesheet that exist
1463 if (defined $stylesheet) {
1464 #provides backwards capability for those people who define style sheet in a config file
1465 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1466 } else {
1467 foreach my $stylesheet (@stylesheets) {
1468 next unless $stylesheet;
1469 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1472 if (defined $project) {
1473 printf('<link rel="alternate" title="%s log" '.
1474 'href="%s" type="application/rss+xml"/>'."\n",
1475 esc_param($project), href(action=>"rss"));
1476 } else {
1477 printf('<link rel="alternate" title="%s projects list" '.
1478 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1479 $site_name, href(project=>undef, action=>"project_index"));
1480 printf('<link rel="alternate" title="%s projects logs" '.
1481 'href="%s" type="text/x-opml"/>'."\n",
1482 $site_name, href(project=>undef, action=>"opml"));
1484 if (defined $favicon) {
1485 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1488 print "</head>\n" .
1489 "<body>\n";
1491 if (-f $site_header) {
1492 open (my $fd, $site_header);
1493 print <$fd>;
1494 close $fd;
1497 print "<div class=\"page_header\">\n" .
1498 $cgi->a({-href => esc_url($logo_url),
1499 -title => $logo_label},
1500 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1501 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1502 if (defined $project) {
1503 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1504 if (defined $action) {
1505 print " / $action";
1507 print "\n";
1508 if (!defined $searchtext) {
1509 $searchtext = "";
1511 my $search_hash;
1512 if (defined $hash_base) {
1513 $search_hash = $hash_base;
1514 } elsif (defined $hash) {
1515 $search_hash = $hash;
1516 } else {
1517 $search_hash = "HEAD";
1519 $cgi->param("a", "search");
1520 $cgi->param("h", $search_hash);
1521 $cgi->param("p", $project);
1522 print $cgi->startform(-method => "get", -action => $my_uri) .
1523 "<div class=\"search\">\n" .
1524 $cgi->hidden(-name => "p") . "\n" .
1525 $cgi->hidden(-name => "a") . "\n" .
1526 $cgi->hidden(-name => "h") . "\n" .
1527 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1528 "</div>" .
1529 $cgi->end_form() . "\n";
1531 print "</div>\n";
1534 sub git_footer_html {
1535 print "<div class=\"page_footer\">\n";
1536 if (defined $project) {
1537 my $descr = git_get_project_description($project);
1538 if (defined $descr) {
1539 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1541 print $cgi->a({-href => href(action=>"rss"),
1542 -class => "rss_logo"}, "RSS") . "\n";
1543 } else {
1544 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1545 -class => "rss_logo"}, "OPML") . " ";
1546 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1547 -class => "rss_logo"}, "TXT") . "\n";
1549 print "</div>\n" ;
1551 if (-f $site_footer) {
1552 open (my $fd, $site_footer);
1553 print <$fd>;
1554 close $fd;
1557 print "</body>\n" .
1558 "</html>";
1561 sub die_error {
1562 my $status = shift || "403 Forbidden";
1563 my $error = shift || "Malformed query, file missing or permission denied";
1565 git_header_html($status);
1566 print <<EOF;
1567 <div class="page_body">
1568 <br /><br />
1569 $status - $error
1570 <br />
1571 </div>
1573 git_footer_html();
1574 exit;
1577 ## ----------------------------------------------------------------------
1578 ## functions printing or outputting HTML: navigation
1580 sub git_print_page_nav {
1581 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1582 $extra = '' if !defined $extra; # pager or formats
1584 my @navs = qw(summary shortlog log commit commitdiff tree);
1585 if ($suppress) {
1586 @navs = grep { $_ ne $suppress } @navs;
1589 my %arg = map { $_ => {action=>$_} } @navs;
1590 if (defined $head) {
1591 for (qw(commit commitdiff)) {
1592 $arg{$_}{hash} = $head;
1594 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1595 for (qw(shortlog log)) {
1596 $arg{$_}{hash} = $head;
1600 $arg{tree}{hash} = $treehead if defined $treehead;
1601 $arg{tree}{hash_base} = $treebase if defined $treebase;
1603 print "<div class=\"page_nav\">\n" .
1604 (join " | ",
1605 map { $_ eq $current ?
1606 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1607 } @navs);
1608 print "<br/>\n$extra<br/>\n" .
1609 "</div>\n";
1612 sub format_paging_nav {
1613 my ($action, $hash, $head, $page, $nrevs) = @_;
1614 my $paging_nav;
1617 if ($hash ne $head || $page) {
1618 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1619 } else {
1620 $paging_nav .= "HEAD";
1623 if ($page > 0) {
1624 $paging_nav .= " &sdot; " .
1625 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1626 -accesskey => "p", -title => "Alt-p"}, "prev");
1627 } else {
1628 $paging_nav .= " &sdot; prev";
1631 if ($nrevs >= (100 * ($page+1)-1)) {
1632 $paging_nav .= " &sdot; " .
1633 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1634 -accesskey => "n", -title => "Alt-n"}, "next");
1635 } else {
1636 $paging_nav .= " &sdot; next";
1639 return $paging_nav;
1642 ## ......................................................................
1643 ## functions printing or outputting HTML: div
1645 sub git_print_header_div {
1646 my ($action, $title, $hash, $hash_base) = @_;
1647 my %args = ();
1649 $args{action} = $action;
1650 $args{hash} = $hash if $hash;
1651 $args{hash_base} = $hash_base if $hash_base;
1653 print "<div class=\"header\">\n" .
1654 $cgi->a({-href => href(%args), -class => "title"},
1655 $title ? $title : $action) .
1656 "\n</div>\n";
1659 #sub git_print_authorship (\%) {
1660 sub git_print_authorship {
1661 my $co = shift;
1663 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1664 print "<div class=\"author_date\">" .
1665 esc_html($co->{'author_name'}) .
1666 " [$ad{'rfc2822'}";
1667 if ($ad{'hour_local'} < 6) {
1668 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1669 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1670 } else {
1671 printf(" (%02d:%02d %s)",
1672 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1674 print "]</div>\n";
1677 sub git_print_page_path {
1678 my $name = shift;
1679 my $type = shift;
1680 my $hb = shift;
1683 print "<div class=\"page_path\">";
1684 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1685 -title => 'tree root'}, "[$project]");
1686 print " / ";
1687 if (defined $name) {
1688 my @dirname = split '/', $name;
1689 my $basename = pop @dirname;
1690 my $fullname = '';
1692 foreach my $dir (@dirname) {
1693 $fullname .= ($fullname ? '/' : '') . $dir;
1694 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1695 hash_base=>$hb),
1696 -title => $fullname}, esc_html($dir));
1697 print " / ";
1699 if (defined $type && $type eq 'blob') {
1700 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1701 hash_base=>$hb),
1702 -title => $name}, esc_html($basename));
1703 } elsif (defined $type && $type eq 'tree') {
1704 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1705 hash_base=>$hb),
1706 -title => $name}, esc_html($basename));
1707 print " / ";
1708 } else {
1709 print esc_html($basename);
1712 print "<br/></div>\n";
1715 # sub git_print_log (\@;%) {
1716 sub git_print_log ($;%) {
1717 my $log = shift;
1718 my %opts = @_;
1720 if ($opts{'-remove_title'}) {
1721 # remove title, i.e. first line of log
1722 shift @$log;
1724 # remove leading empty lines
1725 while (defined $log->[0] && $log->[0] eq "") {
1726 shift @$log;
1729 # print log
1730 my $signoff = 0;
1731 my $empty = 0;
1732 foreach my $line (@$log) {
1733 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1734 $signoff = 1;
1735 $empty = 0;
1736 if (! $opts{'-remove_signoff'}) {
1737 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1738 next;
1739 } else {
1740 # remove signoff lines
1741 next;
1743 } else {
1744 $signoff = 0;
1747 # print only one empty line
1748 # do not print empty line after signoff
1749 if ($line eq "") {
1750 next if ($empty || $signoff);
1751 $empty = 1;
1752 } else {
1753 $empty = 0;
1756 print format_log_line_html($line) . "<br/>\n";
1759 if ($opts{'-final_empty_line'}) {
1760 # end with single empty line
1761 print "<br/>\n" unless $empty;
1765 sub git_print_simplified_log {
1766 my $log = shift;
1767 my $remove_title = shift;
1769 git_print_log($log,
1770 -final_empty_line=> 1,
1771 -remove_title => $remove_title);
1774 # print tree entry (row of git_tree), but without encompassing <tr> element
1775 sub git_print_tree_entry {
1776 my ($t, $basedir, $hash_base, $have_blame) = @_;
1778 my %base_key = ();
1779 $base_key{hash_base} = $hash_base if defined $hash_base;
1781 # The format of a table row is: mode list link. Where mode is
1782 # the mode of the entry, list is the name of the entry, an href,
1783 # and link is the action links of the entry.
1785 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1786 if ($t->{'type'} eq "blob") {
1787 print "<td class=\"list\">" .
1788 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1789 file_name=>"$basedir$t->{'name'}", %base_key),
1790 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1791 print "<td class=\"link\">";
1792 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1793 file_name=>"$basedir$t->{'name'}", %base_key)},
1794 "blob");
1795 if ($have_blame) {
1796 print " | " .
1797 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1798 file_name=>"$basedir$t->{'name'}", %base_key)},
1799 "blame");
1801 if (defined $hash_base) {
1802 print " | " .
1803 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1804 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1805 "history");
1807 print " | " .
1808 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1809 file_name=>"$basedir$t->{'name'}")},
1810 "raw");
1811 print "</td>\n";
1813 } elsif ($t->{'type'} eq "tree") {
1814 print "<td class=\"list\">";
1815 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1816 file_name=>"$basedir$t->{'name'}", %base_key)},
1817 esc_html($t->{'name'}));
1818 print "</td>\n";
1819 print "<td class=\"link\">";
1820 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1821 file_name=>"$basedir$t->{'name'}", %base_key)},
1822 "tree");
1823 if (defined $hash_base) {
1824 print " | " .
1825 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1826 file_name=>"$basedir$t->{'name'}")},
1827 "history");
1829 print "</td>\n";
1833 ## ......................................................................
1834 ## functions printing large fragments of HTML
1836 sub git_difftree_body {
1837 my ($difftree, $hash, $parent) = @_;
1839 print "<div class=\"list_head\">\n";
1840 if ($#{$difftree} > 10) {
1841 print(($#{$difftree} + 1) . " files changed:\n");
1843 print "</div>\n";
1845 print "<table class=\"diff_tree\">\n";
1846 my $alternate = 1;
1847 my $patchno = 0;
1848 foreach my $line (@{$difftree}) {
1849 my %diff = parse_difftree_raw_line($line);
1851 if ($alternate) {
1852 print "<tr class=\"dark\">\n";
1853 } else {
1854 print "<tr class=\"light\">\n";
1856 $alternate ^= 1;
1858 my ($to_mode_oct, $to_mode_str, $to_file_type);
1859 my ($from_mode_oct, $from_mode_str, $from_file_type);
1860 if ($diff{'to_mode'} ne ('0' x 6)) {
1861 $to_mode_oct = oct $diff{'to_mode'};
1862 if (S_ISREG($to_mode_oct)) { # only for regular file
1863 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1865 $to_file_type = file_type($diff{'to_mode'});
1867 if ($diff{'from_mode'} ne ('0' x 6)) {
1868 $from_mode_oct = oct $diff{'from_mode'};
1869 if (S_ISREG($to_mode_oct)) { # only for regular file
1870 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1872 $from_file_type = file_type($diff{'from_mode'});
1875 if ($diff{'status'} eq "A") { # created
1876 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1877 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1878 $mode_chng .= "]</span>";
1879 print "<td>";
1880 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1881 hash_base=>$hash, file_name=>$diff{'file'}),
1882 -class => "list"}, esc_html($diff{'file'}));
1883 print "</td>\n";
1884 print "<td>$mode_chng</td>\n";
1885 print "<td class=\"link\">";
1886 if ($action eq 'commitdiff') {
1887 # link to patch
1888 $patchno++;
1889 print $cgi->a({-href => "#patch$patchno"}, "patch");
1891 print "</td>\n";
1893 } elsif ($diff{'status'} eq "D") { # deleted
1894 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1895 print "<td>";
1896 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1897 hash_base=>$parent, file_name=>$diff{'file'}),
1898 -class => "list"}, esc_html($diff{'file'}));
1899 print "</td>\n";
1900 print "<td>$mode_chng</td>\n";
1901 print "<td class=\"link\">";
1902 if ($action eq 'commitdiff') {
1903 # link to patch
1904 $patchno++;
1905 print $cgi->a({-href => "#patch$patchno"}, "patch");
1906 print " | ";
1908 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1909 hash_base=>$parent, file_name=>$diff{'file'})},
1910 "blob") . " | ";
1911 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1912 file_name=>$diff{'file'})},
1913 "blame") . " | ";
1914 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1915 file_name=>$diff{'file'})},
1916 "history");
1917 print "</td>\n";
1919 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1920 my $mode_chnge = "";
1921 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1922 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1923 if ($from_file_type != $to_file_type) {
1924 $mode_chnge .= " from $from_file_type to $to_file_type";
1926 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1927 if ($from_mode_str && $to_mode_str) {
1928 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1929 } elsif ($to_mode_str) {
1930 $mode_chnge .= " mode: $to_mode_str";
1933 $mode_chnge .= "]</span>\n";
1935 print "<td>";
1936 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1937 hash_base=>$hash, file_name=>$diff{'file'}),
1938 -class => "list"}, esc_html($diff{'file'}));
1939 print "</td>\n";
1940 print "<td>$mode_chnge</td>\n";
1941 print "<td class=\"link\">";
1942 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1943 if ($action eq 'commitdiff') {
1944 # link to patch
1945 $patchno++;
1946 print $cgi->a({-href => "#patch$patchno"}, "patch");
1947 } else {
1948 print $cgi->a({-href => href(action=>"blobdiff",
1949 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1950 hash_base=>$hash, hash_parent_base=>$parent,
1951 file_name=>$diff{'file'})},
1952 "diff");
1954 print " | ";
1956 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1957 hash_base=>$hash, file_name=>$diff{'file'})},
1958 "blob") . " | ";
1959 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1960 file_name=>$diff{'file'})},
1961 "blame") . " | ";
1962 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1963 file_name=>$diff{'file'})},
1964 "history");
1965 print "</td>\n";
1967 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1968 my %status_name = ('R' => 'moved', 'C' => 'copied');
1969 my $nstatus = $status_name{$diff{'status'}};
1970 my $mode_chng = "";
1971 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1972 # mode also for directories, so we cannot use $to_mode_str
1973 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1975 print "<td>" .
1976 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1977 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1978 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1979 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1980 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1981 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1982 -class => "list"}, esc_html($diff{'from_file'})) .
1983 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1984 "<td class=\"link\">";
1985 if ($diff{'to_id'} ne $diff{'from_id'}) {
1986 if ($action eq 'commitdiff') {
1987 # link to patch
1988 $patchno++;
1989 print $cgi->a({-href => "#patch$patchno"}, "patch");
1990 } else {
1991 print $cgi->a({-href => href(action=>"blobdiff",
1992 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1993 hash_base=>$hash, hash_parent_base=>$parent,
1994 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1995 "diff");
1997 print " | ";
1999 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
2000 hash_base=>$parent, file_name=>$diff{'from_file'})},
2001 "blob") . " | ";
2002 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2003 file_name=>$diff{'from_file'})},
2004 "blame") . " | ";
2005 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2006 file_name=>$diff{'from_file'})},
2007 "history");
2008 print "</td>\n";
2010 } # we should not encounter Unmerged (U) or Unknown (X) status
2011 print "</tr>\n";
2013 print "</table>\n";
2016 sub git_patchset_body {
2017 my ($fd, $difftree, $hash, $hash_parent) = @_;
2019 my $patch_idx = 0;
2020 my $in_header = 0;
2021 my $patch_found = 0;
2022 my $diffinfo;
2024 print "<div class=\"patchset\">\n";
2026 LINE:
2027 while (my $patch_line = <$fd>) {
2028 chomp $patch_line;
2030 if ($patch_line =~ m/^diff /) { # "git diff" header
2031 # beginning of patch (in patchset)
2032 if ($patch_found) {
2033 # close previous patch
2034 print "</div>\n"; # class="patch"
2035 } else {
2036 # first patch in patchset
2037 $patch_found = 1;
2039 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2041 if (ref($difftree->[$patch_idx]) eq "HASH") {
2042 $diffinfo = $difftree->[$patch_idx];
2043 } else {
2044 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2046 $patch_idx++;
2048 # for now, no extended header, hence we skip empty patches
2049 # companion to next LINE if $in_header;
2050 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
2051 $in_header = 1;
2052 next LINE;
2055 if ($diffinfo->{'status'} eq "A") { # added
2056 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
2057 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2058 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2059 $diffinfo->{'to_id'}) . " (new)" .
2060 "</div>\n"; # class="diff_info"
2062 } elsif ($diffinfo->{'status'} eq "D") { # deleted
2063 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
2064 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2065 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2066 $diffinfo->{'from_id'}) . " (deleted)" .
2067 "</div>\n"; # class="diff_info"
2069 } elsif ($diffinfo->{'status'} eq "R" || # renamed
2070 $diffinfo->{'status'} eq "C" || # copied
2071 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
2072 print "<div class=\"diff_info\">" .
2073 file_type($diffinfo->{'from_mode'}) . ":" .
2074 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2075 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
2076 $diffinfo->{'from_id'}) .
2077 " -> " .
2078 file_type($diffinfo->{'to_mode'}) . ":" .
2079 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2080 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
2081 $diffinfo->{'to_id'});
2082 print "</div>\n"; # class="diff_info"
2084 } else { # modified, mode changed, ...
2085 print "<div class=\"diff_info\">" .
2086 file_type($diffinfo->{'from_mode'}) . ":" .
2087 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2088 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2089 $diffinfo->{'from_id'}) .
2090 " -> " .
2091 file_type($diffinfo->{'to_mode'}) . ":" .
2092 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2093 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2094 $diffinfo->{'to_id'});
2095 print "</div>\n"; # class="diff_info"
2098 #print "<div class=\"diff extended_header\">\n";
2099 $in_header = 1;
2100 next LINE;
2101 } # start of patch in patchset
2104 if ($in_header && $patch_line =~ m/^---/) {
2105 #print "</div>\n"; # class="diff extended_header"
2106 $in_header = 0;
2108 my $file = $diffinfo->{'from_file'};
2109 $file ||= $diffinfo->{'file'};
2110 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2111 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2112 -class => "list"}, esc_html($file));
2113 $patch_line =~ s|a/.*$|a/$file|g;
2114 print "<div class=\"diff from_file\">$patch_line</div>\n";
2116 $patch_line = <$fd>;
2117 chomp $patch_line;
2119 #$patch_line =~ m/^+++/;
2120 $file = $diffinfo->{'to_file'};
2121 $file ||= $diffinfo->{'file'};
2122 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2123 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2124 -class => "list"}, esc_html($file));
2125 $patch_line =~ s|b/.*|b/$file|g;
2126 print "<div class=\"diff to_file\">$patch_line</div>\n";
2128 next LINE;
2130 next LINE if $in_header;
2132 print format_diff_line($patch_line);
2134 print "</div>\n" if $patch_found; # class="patch"
2136 print "</div>\n"; # class="patchset"
2139 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2141 sub git_shortlog_body {
2142 # uses global variable $project
2143 my ($revlist, $from, $to, $refs, $extra) = @_;
2145 $from = 0 unless defined $from;
2146 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2148 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2149 my $alternate = 1;
2150 for (my $i = $from; $i <= $to; $i++) {
2151 my $commit = $revlist->[$i];
2152 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2153 my $ref = format_ref_marker($refs, $commit);
2154 my %co = parse_commit($commit);
2155 if ($alternate) {
2156 print "<tr class=\"dark\">\n";
2157 } else {
2158 print "<tr class=\"light\">\n";
2160 $alternate ^= 1;
2161 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2162 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2163 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2164 "<td>";
2165 print format_subject_html($co{'title'}, $co{'title_short'},
2166 href(action=>"commit", hash=>$commit), $ref);
2167 print "</td>\n" .
2168 "<td class=\"link\">" .
2169 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2170 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2171 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2172 if (gitweb_have_snapshot()) {
2173 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2175 print "</td>\n" .
2176 "</tr>\n";
2178 if (defined $extra) {
2179 print "<tr>\n" .
2180 "<td colspan=\"4\">$extra</td>\n" .
2181 "</tr>\n";
2183 print "</table>\n";
2186 sub git_history_body {
2187 # Warning: assumes constant type (blob or tree) during history
2188 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2190 $from = 0 unless defined $from;
2191 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2193 print "<table class=\"history\" cellspacing=\"0\">\n";
2194 my $alternate = 1;
2195 for (my $i = $from; $i <= $to; $i++) {
2196 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2197 next;
2200 my $commit = $1;
2201 my %co = parse_commit($commit);
2202 if (!%co) {
2203 next;
2206 my $ref = format_ref_marker($refs, $commit);
2208 if ($alternate) {
2209 print "<tr class=\"dark\">\n";
2210 } else {
2211 print "<tr class=\"light\">\n";
2213 $alternate ^= 1;
2214 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2215 # shortlog uses chop_str($co{'author_name'}, 10)
2216 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2217 "<td>";
2218 # originally git_history used chop_str($co{'title'}, 50)
2219 print format_subject_html($co{'title'}, $co{'title_short'},
2220 href(action=>"commit", hash=>$commit), $ref);
2221 print "</td>\n" .
2222 "<td class=\"link\">" .
2223 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2224 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2226 if ($ftype eq 'blob') {
2227 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2228 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2229 if (defined $blob_current && defined $blob_parent &&
2230 $blob_current ne $blob_parent) {
2231 print " | " .
2232 $cgi->a({-href => href(action=>"blobdiff",
2233 hash=>$blob_current, hash_parent=>$blob_parent,
2234 hash_base=>$hash_base, hash_parent_base=>$commit,
2235 file_name=>$file_name)},
2236 "diff to current");
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_tags_body {
2251 # uses global variable $project
2252 my ($taglist, $from, $to, $extra) = @_;
2253 $from = 0 unless defined $from;
2254 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2256 print "<table class=\"tags\" cellspacing=\"0\">\n";
2257 my $alternate = 1;
2258 for (my $i = $from; $i <= $to; $i++) {
2259 my $entry = $taglist->[$i];
2260 my %tag = %$entry;
2261 my $comment_lines = $tag{'comment'};
2262 my $comment = shift @$comment_lines;
2263 my $comment_short;
2264 if (defined $comment) {
2265 $comment_short = chop_str($comment, 30, 5);
2267 if ($alternate) {
2268 print "<tr class=\"dark\">\n";
2269 } else {
2270 print "<tr class=\"light\">\n";
2272 $alternate ^= 1;
2273 print "<td><i>$tag{'age'}</i></td>\n" .
2274 "<td>" .
2275 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2276 -class => "list name"}, esc_html($tag{'name'})) .
2277 "</td>\n" .
2278 "<td>";
2279 if (defined $comment) {
2280 print format_subject_html($comment, $comment_short,
2281 href(action=>"tag", hash=>$tag{'id'}));
2283 print "</td>\n" .
2284 "<td class=\"selflink\">";
2285 if ($tag{'type'} eq "tag") {
2286 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2287 } else {
2288 print "&nbsp;";
2290 print "</td>\n" .
2291 "<td class=\"link\">" . " | " .
2292 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2293 if ($tag{'reftype'} eq "commit") {
2294 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2295 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2296 } elsif ($tag{'reftype'} eq "blob") {
2297 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2299 print "</td>\n" .
2300 "</tr>";
2302 if (defined $extra) {
2303 print "<tr>\n" .
2304 "<td colspan=\"5\">$extra</td>\n" .
2305 "</tr>\n";
2307 print "</table>\n";
2310 sub git_heads_body {
2311 # uses global variable $project
2312 my ($headlist, $head, $from, $to, $extra) = @_;
2313 $from = 0 unless defined $from;
2314 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2316 print "<table class=\"heads\" cellspacing=\"0\">\n";
2317 my $alternate = 1;
2318 for (my $i = $from; $i <= $to; $i++) {
2319 my $entry = $headlist->[$i];
2320 my %tag = %$entry;
2321 my $curr = $tag{'id'} eq $head;
2322 if ($alternate) {
2323 print "<tr class=\"dark\">\n";
2324 } else {
2325 print "<tr class=\"light\">\n";
2327 $alternate ^= 1;
2328 print "<td><i>$tag{'age'}</i></td>\n" .
2329 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2330 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2331 -class => "list name"},esc_html($tag{'name'})) .
2332 "</td>\n" .
2333 "<td class=\"link\">" .
2334 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2335 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2336 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2337 "</td>\n" .
2338 "</tr>";
2340 if (defined $extra) {
2341 print "<tr>\n" .
2342 "<td colspan=\"3\">$extra</td>\n" .
2343 "</tr>\n";
2345 print "</table>\n";
2348 ## ======================================================================
2349 ## ======================================================================
2350 ## actions
2352 sub git_project_list {
2353 my $order = $cgi->param('o');
2354 if (defined $order && $order !~ m/project|descr|owner|age/) {
2355 die_error(undef, "Unknown order parameter");
2358 my @list = git_get_projects_list();
2359 my @projects;
2360 if (!@list) {
2361 die_error(undef, "No projects found");
2363 foreach my $pr (@list) {
2364 my (@aa) = git_get_last_activity($pr->{'path'});
2365 unless (@aa) {
2366 next;
2368 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2369 if (!defined $pr->{'descr'}) {
2370 my $descr = git_get_project_description($pr->{'path'}) || "";
2371 $pr->{'descr'} = chop_str($descr, 25, 5);
2373 if (!defined $pr->{'owner'}) {
2374 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2376 push @projects, $pr;
2379 git_header_html();
2380 if (-f $home_text) {
2381 print "<div class=\"index_include\">\n";
2382 open (my $fd, $home_text);
2383 print <$fd>;
2384 close $fd;
2385 print "</div>\n";
2387 print "<table class=\"project_list\">\n" .
2388 "<tr>\n";
2389 $order ||= "project";
2390 if ($order eq "project") {
2391 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2392 print "<th>Project</th>\n";
2393 } else {
2394 print "<th>" .
2395 $cgi->a({-href => href(project=>undef, order=>'project'),
2396 -class => "header"}, "Project") .
2397 "</th>\n";
2399 if ($order eq "descr") {
2400 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2401 print "<th>Description</th>\n";
2402 } else {
2403 print "<th>" .
2404 $cgi->a({-href => href(project=>undef, order=>'descr'),
2405 -class => "header"}, "Description") .
2406 "</th>\n";
2408 if ($order eq "owner") {
2409 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2410 print "<th>Owner</th>\n";
2411 } else {
2412 print "<th>" .
2413 $cgi->a({-href => href(project=>undef, order=>'owner'),
2414 -class => "header"}, "Owner") .
2415 "</th>\n";
2417 if ($order eq "age") {
2418 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2419 print "<th>Last Change</th>\n";
2420 } else {
2421 print "<th>" .
2422 $cgi->a({-href => href(project=>undef, order=>'age'),
2423 -class => "header"}, "Last Change") .
2424 "</th>\n";
2426 print "<th></th>\n" .
2427 "</tr>\n";
2428 my $alternate = 1;
2429 foreach my $pr (@projects) {
2430 if ($alternate) {
2431 print "<tr class=\"dark\">\n";
2432 } else {
2433 print "<tr class=\"light\">\n";
2435 $alternate ^= 1;
2436 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2437 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2438 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2439 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2440 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2441 $pr->{'age_string'} . "</td>\n" .
2442 "<td class=\"link\">" .
2443 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2444 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2445 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2446 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2447 "</td>\n" .
2448 "</tr>\n";
2450 print "</table>\n";
2451 git_footer_html();
2454 sub git_project_index {
2455 my @projects = git_get_projects_list();
2457 print $cgi->header(
2458 -type => 'text/plain',
2459 -charset => 'utf-8',
2460 -content_disposition => 'inline; filename="index.aux"');
2462 foreach my $pr (@projects) {
2463 if (!exists $pr->{'owner'}) {
2464 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2467 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2468 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2469 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2470 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2471 $path =~ s/ /\+/g;
2472 $owner =~ s/ /\+/g;
2474 print "$path $owner\n";
2478 sub git_summary {
2479 my $descr = git_get_project_description($project) || "none";
2480 my $head = git_get_head_hash($project);
2481 my %co = parse_commit($head);
2482 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2484 my $owner = git_get_project_owner($project);
2486 my ($reflist, $refs) = git_get_refs_list();
2488 my @taglist;
2489 my @headlist;
2490 foreach my $ref (@$reflist) {
2491 if ($ref->{'name'} =~ s!^heads/!!) {
2492 push @headlist, $ref;
2493 } else {
2494 $ref->{'name'} =~ s!^tags/!!;
2495 push @taglist, $ref;
2499 git_header_html();
2500 git_print_page_nav('summary','', $head);
2502 print "<div class=\"title\">&nbsp;</div>\n";
2503 print "<table cellspacing=\"0\">\n" .
2504 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2505 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2506 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2507 # use per project git URL list in $projectroot/$project/cloneurl
2508 # or make project git URL from git base URL and project name
2509 my $url_tag = "URL";
2510 my @url_list = git_get_project_url_list($project);
2511 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2512 foreach my $git_url (@url_list) {
2513 next unless $git_url;
2514 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2515 $url_tag = "";
2517 print "</table>\n";
2519 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2520 git_get_head_hash($project)
2521 or die_error(undef, "Open git-rev-list failed");
2522 my @revlist = map { chomp; $_ } <$fd>;
2523 close $fd;
2524 git_print_header_div('shortlog');
2525 git_shortlog_body(\@revlist, 0, 15, $refs,
2526 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2528 if (@taglist) {
2529 git_print_header_div('tags');
2530 git_tags_body(\@taglist, 0, 15,
2531 $cgi->a({-href => href(action=>"tags")}, "..."));
2534 if (@headlist) {
2535 git_print_header_div('heads');
2536 git_heads_body(\@headlist, $head, 0, 15,
2537 $cgi->a({-href => href(action=>"heads")}, "..."));
2540 git_footer_html();
2543 sub git_tag {
2544 my $head = git_get_head_hash($project);
2545 git_header_html();
2546 git_print_page_nav('','', $head,undef,$head);
2547 my %tag = parse_tag($hash);
2548 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2549 print "<div class=\"title_text\">\n" .
2550 "<table cellspacing=\"0\">\n" .
2551 "<tr>\n" .
2552 "<td>object</td>\n" .
2553 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2554 $tag{'object'}) . "</td>\n" .
2555 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2556 $tag{'type'}) . "</td>\n" .
2557 "</tr>\n";
2558 if (defined($tag{'author'})) {
2559 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2560 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2561 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2562 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2563 "</td></tr>\n";
2565 print "</table>\n\n" .
2566 "</div>\n";
2567 print "<div class=\"page_body\">";
2568 my $comment = $tag{'comment'};
2569 foreach my $line (@$comment) {
2570 print esc_html($line) . "<br/>\n";
2572 print "</div>\n";
2573 git_footer_html();
2576 sub git_blame2 {
2577 my $fd;
2578 my $ftype;
2580 my ($have_blame) = gitweb_check_feature('blame');
2581 if (!$have_blame) {
2582 die_error('403 Permission denied', "Permission denied");
2584 die_error('404 Not Found', "File name not defined") if (!$file_name);
2585 $hash_base ||= git_get_head_hash($project);
2586 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2587 my %co = parse_commit($hash_base)
2588 or die_error(undef, "Reading commit failed");
2589 if (!defined $hash) {
2590 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2591 or die_error(undef, "Error looking up file");
2593 $ftype = git_get_type($hash);
2594 if ($ftype !~ "blob") {
2595 die_error("400 Bad Request", "Object is not a blob");
2597 open ($fd, "-|", git_cmd(), "blame", '-l', '--', $file_name, $hash_base)
2598 or die_error(undef, "Open git-blame failed");
2599 git_header_html();
2600 my $formats_nav =
2601 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2602 "blob") .
2603 " | " .
2604 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2605 "history") .
2606 " | " .
2607 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2608 "HEAD");
2609 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2610 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2611 git_print_page_path($file_name, $ftype, $hash_base);
2612 my @rev_color = (qw(light2 dark2));
2613 my $num_colors = scalar(@rev_color);
2614 my $current_color = 0;
2615 my $last_rev;
2616 print <<HTML;
2617 <div class="page_body">
2618 <table class="blame">
2619 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2620 HTML
2621 while (<$fd>) {
2622 my ($full_rev, $author, $date, $lineno, $data) =
2623 /^([0-9a-f]{40}).*?\s\((.*?)\s+([-\d]+ [:\d]+ [-+\d]+)\s+(\d+)\)\s(.*)/;
2624 my $rev = substr($full_rev, 0, 8);
2625 my $print_c8 = 0;
2627 if (!defined $last_rev) {
2628 $last_rev = $full_rev;
2629 $print_c8 = 1;
2630 } elsif ($last_rev ne $full_rev) {
2631 $last_rev = $full_rev;
2632 $current_color = ++$current_color % $num_colors;
2633 $print_c8 = 1;
2635 print "<tr class=\"$rev_color[$current_color]\">\n";
2636 print "<td class=\"sha1\"";
2637 if ($print_c8 == 1) {
2638 print " title=\"$author, $date\"";
2640 print ">";
2641 if ($print_c8 == 1) {
2642 print $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2643 esc_html($rev));
2645 print "</td>\n";
2646 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2647 esc_html($lineno) . "</a></td>\n";
2648 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2649 print "</tr>\n";
2651 print "</table>\n";
2652 print "</div>";
2653 close $fd
2654 or print "Reading blob failed\n";
2655 git_footer_html();
2658 sub git_blame {
2659 my $fd;
2661 my ($have_blame) = gitweb_check_feature('blame');
2662 if (!$have_blame) {
2663 die_error('403 Permission denied', "Permission denied");
2665 die_error('404 Not Found', "File name not defined") if (!$file_name);
2666 $hash_base ||= git_get_head_hash($project);
2667 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2668 my %co = parse_commit($hash_base)
2669 or die_error(undef, "Reading commit failed");
2670 if (!defined $hash) {
2671 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2672 or die_error(undef, "Error lookup file");
2674 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2675 or die_error(undef, "Open git-annotate failed");
2676 git_header_html();
2677 my $formats_nav =
2678 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2679 "blob") .
2680 " | " .
2681 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2682 "history") .
2683 " | " .
2684 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2685 "HEAD");
2686 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2687 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2688 git_print_page_path($file_name, 'blob', $hash_base);
2689 print "<div class=\"page_body\">\n";
2690 print <<HTML;
2691 <table class="blame">
2692 <tr>
2693 <th>Commit</th>
2694 <th>Age</th>
2695 <th>Author</th>
2696 <th>Line</th>
2697 <th>Data</th>
2698 </tr>
2699 HTML
2700 my @line_class = (qw(light dark));
2701 my $line_class_len = scalar (@line_class);
2702 my $line_class_num = $#line_class;
2703 while (my $line = <$fd>) {
2704 my $long_rev;
2705 my $short_rev;
2706 my $author;
2707 my $time;
2708 my $lineno;
2709 my $data;
2710 my $age;
2711 my $age_str;
2712 my $age_class;
2714 chomp $line;
2715 $line_class_num = ($line_class_num + 1) % $line_class_len;
2717 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2718 $long_rev = $1;
2719 $author = $2;
2720 $time = $3;
2721 $lineno = $4;
2722 $data = $5;
2723 } else {
2724 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2725 next;
2727 $short_rev = substr ($long_rev, 0, 8);
2728 $age = time () - $time;
2729 $age_str = age_string ($age);
2730 $age_str =~ s/ /&nbsp;/g;
2731 $age_class = age_class($age);
2732 $author = esc_html ($author);
2733 $author =~ s/ /&nbsp;/g;
2735 $data = untabify($data);
2736 $data = esc_html ($data);
2738 print <<HTML;
2739 <tr class="$line_class[$line_class_num]">
2740 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2741 <td class="$age_class">$age_str</td>
2742 <td>$author</td>
2743 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2744 <td class="pre">$data</td>
2745 </tr>
2746 HTML
2747 } # while (my $line = <$fd>)
2748 print "</table>\n\n";
2749 close $fd
2750 or print "Reading blob failed.\n";
2751 print "</div>";
2752 git_footer_html();
2755 sub git_tags {
2756 my $head = git_get_head_hash($project);
2757 git_header_html();
2758 git_print_page_nav('','', $head,undef,$head);
2759 git_print_header_div('summary', $project);
2761 my ($taglist) = git_get_refs_list("tags");
2762 if (@$taglist) {
2763 git_tags_body($taglist);
2765 git_footer_html();
2768 sub git_heads {
2769 my $head = git_get_head_hash($project);
2770 git_header_html();
2771 git_print_page_nav('','', $head,undef,$head);
2772 git_print_header_div('summary', $project);
2774 my ($headlist) = git_get_refs_list("heads");
2775 if (@$headlist) {
2776 git_heads_body($headlist, $head);
2778 git_footer_html();
2781 sub git_blob_plain {
2782 my $expires;
2784 if (!defined $hash) {
2785 if (defined $file_name) {
2786 my $base = $hash_base || git_get_head_hash($project);
2787 $hash = git_get_hash_by_path($base, $file_name, "blob")
2788 or die_error(undef, "Error lookup file");
2789 } else {
2790 die_error(undef, "No file name defined");
2792 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2793 # blobs defined by non-textual hash id's can be cached
2794 $expires = "+1d";
2797 my $type = shift;
2798 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2799 or die_error(undef, "Couldn't cat $file_name, $hash");
2801 $type ||= blob_mimetype($fd, $file_name);
2803 # save as filename, even when no $file_name is given
2804 my $save_as = "$hash";
2805 if (defined $file_name) {
2806 $save_as = $file_name;
2807 } elsif ($type =~ m/^text\//) {
2808 $save_as .= '.txt';
2811 print $cgi->header(
2812 -type => "$type",
2813 -expires=>$expires,
2814 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2815 undef $/;
2816 binmode STDOUT, ':raw';
2817 print <$fd>;
2818 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2819 $/ = "\n";
2820 close $fd;
2823 sub git_blob {
2824 my $expires;
2826 if (!defined $hash) {
2827 if (defined $file_name) {
2828 my $base = $hash_base || git_get_head_hash($project);
2829 $hash = git_get_hash_by_path($base, $file_name, "blob")
2830 or die_error(undef, "Error lookup file");
2831 } else {
2832 die_error(undef, "No file name defined");
2834 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2835 # blobs defined by non-textual hash id's can be cached
2836 $expires = "+1d";
2839 my ($have_blame) = gitweb_check_feature('blame');
2840 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2841 or die_error(undef, "Couldn't cat $file_name, $hash");
2842 my $mimetype = blob_mimetype($fd, $file_name);
2843 if ($mimetype !~ m/^text\//) {
2844 close $fd;
2845 return git_blob_plain($mimetype);
2847 git_header_html(undef, $expires);
2848 my $formats_nav = '';
2849 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2850 if (defined $file_name) {
2851 if ($have_blame) {
2852 $formats_nav .=
2853 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2854 hash=>$hash, file_name=>$file_name)},
2855 "blame") .
2856 " | ";
2858 $formats_nav .=
2859 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2860 hash=>$hash, file_name=>$file_name)},
2861 "history") .
2862 " | " .
2863 $cgi->a({-href => href(action=>"blob_plain",
2864 hash=>$hash, file_name=>$file_name)},
2865 "raw") .
2866 " | " .
2867 $cgi->a({-href => href(action=>"blob",
2868 hash_base=>"HEAD", file_name=>$file_name)},
2869 "HEAD");
2870 } else {
2871 $formats_nav .=
2872 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2874 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2875 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2876 } else {
2877 print "<div class=\"page_nav\">\n" .
2878 "<br/><br/></div>\n" .
2879 "<div class=\"title\">$hash</div>\n";
2881 git_print_page_path($file_name, "blob", $hash_base);
2882 print "<div class=\"page_body\">\n";
2883 my $nr;
2884 while (my $line = <$fd>) {
2885 chomp $line;
2886 $nr++;
2887 $line = untabify($line);
2888 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2889 $nr, $nr, $nr, esc_html($line);
2891 close $fd
2892 or print "Reading blob failed.\n";
2893 print "</div>";
2894 git_footer_html();
2897 sub git_tree {
2898 my $have_snapshot = gitweb_have_snapshot();
2900 if (!defined $hash_base) {
2901 $hash_base = "HEAD";
2903 if (!defined $hash) {
2904 if (defined $file_name) {
2905 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2906 } else {
2907 $hash = $hash_base;
2910 $/ = "\0";
2911 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2912 or die_error(undef, "Open git-ls-tree failed");
2913 my @entries = map { chomp; $_ } <$fd>;
2914 close $fd or die_error(undef, "Reading tree failed");
2915 $/ = "\n";
2917 my $refs = git_get_references();
2918 my $ref = format_ref_marker($refs, $hash_base);
2919 git_header_html();
2920 my $basedir = '';
2921 my ($have_blame) = gitweb_check_feature('blame');
2922 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2923 my @views_nav = ();
2924 if (defined $file_name) {
2925 push @views_nav,
2926 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2927 hash=>$hash, file_name=>$file_name)},
2928 "history"),
2929 $cgi->a({-href => href(action=>"tree",
2930 hash_base=>"HEAD", file_name=>$file_name)},
2931 "HEAD"),
2933 if ($have_snapshot) {
2934 # FIXME: Should be available when we have no hash base as well.
2935 push @views_nav,
2936 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2937 "snapshot");
2939 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2940 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2941 } else {
2942 undef $hash_base;
2943 print "<div class=\"page_nav\">\n";
2944 print "<br/><br/></div>\n";
2945 print "<div class=\"title\">$hash</div>\n";
2947 if (defined $file_name) {
2948 $basedir = $file_name;
2949 if ($basedir ne '' && substr($basedir, -1) ne '/') {
2950 $basedir .= '/';
2953 git_print_page_path($file_name, 'tree', $hash_base);
2954 print "<div class=\"page_body\">\n";
2955 print "<table cellspacing=\"0\">\n";
2956 my $alternate = 1;
2957 # '..' (top directory) link if possible
2958 if (defined $hash_base &&
2959 defined $file_name && $file_name =~ m![^/]+$!) {
2960 if ($alternate) {
2961 print "<tr class=\"dark\">\n";
2962 } else {
2963 print "<tr class=\"light\">\n";
2965 $alternate ^= 1;
2967 my $up = $file_name;
2968 $up =~ s!/?[^/]+$!!;
2969 undef $up unless $up;
2970 # based on git_print_tree_entry
2971 print '<td class="mode">' . mode_str('040000') . "</td>\n";
2972 print '<td class="list">';
2973 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
2974 file_name=>$up)},
2975 "..");
2976 print "</td>\n";
2977 print "<td class=\"link\"></td>\n";
2979 print "</tr>\n";
2981 foreach my $line (@entries) {
2982 my %t = parse_ls_tree_line($line, -z => 1);
2984 if ($alternate) {
2985 print "<tr class=\"dark\">\n";
2986 } else {
2987 print "<tr class=\"light\">\n";
2989 $alternate ^= 1;
2991 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
2993 print "</tr>\n";
2995 print "</table>\n" .
2996 "</div>";
2997 git_footer_html();
3000 sub git_snapshot {
3001 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3002 my $have_snapshot = (defined $ctype && defined $suffix);
3003 if (!$have_snapshot) {
3004 die_error('403 Permission denied', "Permission denied");
3007 if (!defined $hash) {
3008 $hash = git_get_head_hash($project);
3011 my $filename = basename($project) . "-$hash.tar.$suffix";
3013 print $cgi->header(
3014 -type => 'application/x-tar',
3015 -content_encoding => $ctype,
3016 -content_disposition => 'inline; filename="' . "$filename" . '"',
3017 -status => '200 OK');
3019 my $git = git_cmd_str();
3020 my $name = $project;
3021 $name =~ s/\047/\047\\\047\047/g;
3022 open my $fd, "-|",
3023 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3024 or die_error(undef, "Execute git-tar-tree failed.");
3025 binmode STDOUT, ':raw';
3026 print <$fd>;
3027 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3028 close $fd;
3032 sub git_log {
3033 my $head = git_get_head_hash($project);
3034 if (!defined $hash) {
3035 $hash = $head;
3037 if (!defined $page) {
3038 $page = 0;
3040 my $refs = git_get_references();
3042 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3043 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3044 or die_error(undef, "Open git-rev-list failed");
3045 my @revlist = map { chomp; $_ } <$fd>;
3046 close $fd;
3048 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
3050 git_header_html();
3051 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3053 if (!@revlist) {
3054 my %co = parse_commit($hash);
3056 git_print_header_div('summary', $project);
3057 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3059 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
3060 my $commit = $revlist[$i];
3061 my $ref = format_ref_marker($refs, $commit);
3062 my %co = parse_commit($commit);
3063 next if !%co;
3064 my %ad = parse_date($co{'author_epoch'});
3065 git_print_header_div('commit',
3066 "<span class=\"age\">$co{'age_string'}</span>" .
3067 esc_html($co{'title'}) . $ref,
3068 $commit);
3069 print "<div class=\"title_text\">\n" .
3070 "<div class=\"log_link\">\n" .
3071 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3072 " | " .
3073 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3074 " | " .
3075 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3076 "<br/>\n" .
3077 "</div>\n" .
3078 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3079 "</div>\n";
3081 print "<div class=\"log_body\">\n";
3082 git_print_simplified_log($co{'comment'});
3083 print "</div>\n";
3085 git_footer_html();
3088 sub git_commit {
3089 my %co = parse_commit($hash);
3090 if (!%co) {
3091 die_error(undef, "Unknown commit object");
3093 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3094 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3096 my $parent = $co{'parent'};
3097 if (!defined $parent) {
3098 $parent = "--root";
3100 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
3101 or die_error(undef, "Open git-diff-tree failed");
3102 my @difftree = map { chomp; $_ } <$fd>;
3103 close $fd or die_error(undef, "Reading git-diff-tree failed");
3105 # non-textual hash id's can be cached
3106 my $expires;
3107 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3108 $expires = "+1d";
3110 my $refs = git_get_references();
3111 my $ref = format_ref_marker($refs, $co{'id'});
3113 my $have_snapshot = gitweb_have_snapshot();
3115 my @views_nav = ();
3116 if (defined $file_name && defined $co{'parent'}) {
3117 push @views_nav,
3118 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3119 "blame");
3121 git_header_html(undef, $expires);
3122 git_print_page_nav('commit', '',
3123 $hash, $co{'tree'}, $hash,
3124 join (' | ', @views_nav));
3126 if (defined $co{'parent'}) {
3127 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3128 } else {
3129 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3131 print "<div class=\"title_text\">\n" .
3132 "<table cellspacing=\"0\">\n";
3133 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3134 "<tr>" .
3135 "<td></td><td> $ad{'rfc2822'}";
3136 if ($ad{'hour_local'} < 6) {
3137 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3138 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3139 } else {
3140 printf(" (%02d:%02d %s)",
3141 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3143 print "</td>" .
3144 "</tr>\n";
3145 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3146 print "<tr><td></td><td> $cd{'rfc2822'}" .
3147 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3148 "</td></tr>\n";
3149 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3150 print "<tr>" .
3151 "<td>tree</td>" .
3152 "<td class=\"sha1\">" .
3153 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3154 class => "list"}, $co{'tree'}) .
3155 "</td>" .
3156 "<td class=\"link\">" .
3157 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3158 "tree");
3159 if ($have_snapshot) {
3160 print " | " .
3161 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3163 print "</td>" .
3164 "</tr>\n";
3165 my $parents = $co{'parents'};
3166 foreach my $par (@$parents) {
3167 print "<tr>" .
3168 "<td>parent</td>" .
3169 "<td class=\"sha1\">" .
3170 $cgi->a({-href => href(action=>"commit", hash=>$par),
3171 class => "list"}, $par) .
3172 "</td>" .
3173 "<td class=\"link\">" .
3174 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3175 " | " .
3176 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3177 "</td>" .
3178 "</tr>\n";
3180 print "</table>".
3181 "</div>\n";
3183 print "<div class=\"page_body\">\n";
3184 git_print_log($co{'comment'});
3185 print "</div>\n";
3187 git_difftree_body(\@difftree, $hash, $parent);
3189 git_footer_html();
3192 sub git_blobdiff {
3193 my $format = shift || 'html';
3195 my $fd;
3196 my @difftree;
3197 my %diffinfo;
3198 my $expires;
3200 # preparing $fd and %diffinfo for git_patchset_body
3201 # new style URI
3202 if (defined $hash_base && defined $hash_parent_base) {
3203 if (defined $file_name) {
3204 # read raw output
3205 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3206 "--", $file_name
3207 or die_error(undef, "Open git-diff-tree failed");
3208 @difftree = map { chomp; $_ } <$fd>;
3209 close $fd
3210 or die_error(undef, "Reading git-diff-tree failed");
3211 @difftree
3212 or die_error('404 Not Found', "Blob diff not found");
3214 } elsif (defined $hash &&
3215 $hash =~ /[0-9a-fA-F]{40}/) {
3216 # try to find filename from $hash
3218 # read filtered raw output
3219 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3220 or die_error(undef, "Open git-diff-tree failed");
3221 @difftree =
3222 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3223 # $hash == to_id
3224 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3225 map { chomp; $_ } <$fd>;
3226 close $fd
3227 or die_error(undef, "Reading git-diff-tree failed");
3228 @difftree
3229 or die_error('404 Not Found', "Blob diff not found");
3231 } else {
3232 die_error('404 Not Found', "Missing one of the blob diff parameters");
3235 if (@difftree > 1) {
3236 die_error('404 Not Found', "Ambiguous blob diff specification");
3239 %diffinfo = parse_difftree_raw_line($difftree[0]);
3240 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3241 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3243 $hash_parent ||= $diffinfo{'from_id'};
3244 $hash ||= $diffinfo{'to_id'};
3246 # non-textual hash id's can be cached
3247 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3248 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3249 $expires = '+1d';
3252 # open patch output
3253 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3254 '-p', $hash_parent_base, $hash_base,
3255 "--", $file_name
3256 or die_error(undef, "Open git-diff-tree failed");
3259 # old/legacy style URI
3260 if (!%diffinfo && # if new style URI failed
3261 defined $hash && defined $hash_parent) {
3262 # fake git-diff-tree raw output
3263 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3264 $diffinfo{'from_id'} = $hash_parent;
3265 $diffinfo{'to_id'} = $hash;
3266 if (defined $file_name) {
3267 if (defined $file_parent) {
3268 $diffinfo{'status'} = '2';
3269 $diffinfo{'from_file'} = $file_parent;
3270 $diffinfo{'to_file'} = $file_name;
3271 } else { # assume not renamed
3272 $diffinfo{'status'} = '1';
3273 $diffinfo{'from_file'} = $file_name;
3274 $diffinfo{'to_file'} = $file_name;
3276 } else { # no filename given
3277 $diffinfo{'status'} = '2';
3278 $diffinfo{'from_file'} = $hash_parent;
3279 $diffinfo{'to_file'} = $hash;
3282 # non-textual hash id's can be cached
3283 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3284 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3285 $expires = '+1d';
3288 # open patch output
3289 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3290 or die_error(undef, "Open git-diff failed");
3291 } else {
3292 die_error('404 Not Found', "Missing one of the blob diff parameters")
3293 unless %diffinfo;
3296 # header
3297 if ($format eq 'html') {
3298 my $formats_nav =
3299 $cgi->a({-href => href(action=>"blobdiff_plain",
3300 hash=>$hash, hash_parent=>$hash_parent,
3301 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3302 file_name=>$file_name, file_parent=>$file_parent)},
3303 "raw");
3304 git_header_html(undef, $expires);
3305 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3306 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3307 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3308 } else {
3309 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3310 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3312 if (defined $file_name) {
3313 git_print_page_path($file_name, "blob", $hash_base);
3314 } else {
3315 print "<div class=\"page_path\"></div>\n";
3318 } elsif ($format eq 'plain') {
3319 print $cgi->header(
3320 -type => 'text/plain',
3321 -charset => 'utf-8',
3322 -expires => $expires,
3323 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3325 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3327 } else {
3328 die_error(undef, "Unknown blobdiff format");
3331 # patch
3332 if ($format eq 'html') {
3333 print "<div class=\"page_body\">\n";
3335 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3336 close $fd;
3338 print "</div>\n"; # class="page_body"
3339 git_footer_html();
3341 } else {
3342 while (my $line = <$fd>) {
3343 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3344 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3346 print $line;
3348 last if $line =~ m!^\+\+\+!;
3350 local $/ = undef;
3351 print <$fd>;
3352 close $fd;
3356 sub git_blobdiff_plain {
3357 git_blobdiff('plain');
3360 sub git_commitdiff {
3361 my $format = shift || 'html';
3362 my %co = parse_commit($hash);
3363 if (!%co) {
3364 die_error(undef, "Unknown commit object");
3366 if (!defined $hash_parent) {
3367 $hash_parent = $co{'parent'} || '--root';
3370 # read commitdiff
3371 my $fd;
3372 my @difftree;
3373 if ($format eq 'html') {
3374 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3375 "--patch-with-raw", "--full-index", $hash_parent, $hash
3376 or die_error(undef, "Open git-diff-tree failed");
3378 while (chomp(my $line = <$fd>)) {
3379 # empty line ends raw part of diff-tree output
3380 last unless $line;
3381 push @difftree, $line;
3384 } elsif ($format eq 'plain') {
3385 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3386 '-p', $hash_parent, $hash
3387 or die_error(undef, "Open git-diff-tree failed");
3389 } else {
3390 die_error(undef, "Unknown commitdiff format");
3393 # non-textual hash id's can be cached
3394 my $expires;
3395 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3396 $expires = "+1d";
3399 # write commit message
3400 if ($format eq 'html') {
3401 my $refs = git_get_references();
3402 my $ref = format_ref_marker($refs, $co{'id'});
3403 my $formats_nav =
3404 $cgi->a({-href => href(action=>"commitdiff_plain",
3405 hash=>$hash, hash_parent=>$hash_parent)},
3406 "raw");
3408 git_header_html(undef, $expires);
3409 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3410 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3411 git_print_authorship(\%co);
3412 print "<div class=\"page_body\">\n";
3413 print "<div class=\"log\">\n";
3414 git_print_simplified_log($co{'comment'}, 1); # skip title
3415 print "</div>\n"; # class="log"
3417 } elsif ($format eq 'plain') {
3418 my $refs = git_get_references("tags");
3419 my $tagname = git_get_rev_name_tags($hash);
3420 my $filename = basename($project) . "-$hash.patch";
3422 print $cgi->header(
3423 -type => 'text/plain',
3424 -charset => 'utf-8',
3425 -expires => $expires,
3426 -content_disposition => 'inline; filename="' . "$filename" . '"');
3427 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3428 print <<TEXT;
3429 From: $co{'author'}
3430 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3431 Subject: $co{'title'}
3432 TEXT
3433 print "X-Git-Tag: $tagname\n" if $tagname;
3434 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3436 foreach my $line (@{$co{'comment'}}) {
3437 print "$line\n";
3439 print "---\n\n";
3442 # write patch
3443 if ($format eq 'html') {
3444 git_difftree_body(\@difftree, $hash, $hash_parent);
3445 print "<br/>\n";
3447 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3448 close $fd;
3449 print "</div>\n"; # class="page_body"
3450 git_footer_html();
3452 } elsif ($format eq 'plain') {
3453 local $/ = undef;
3454 print <$fd>;
3455 close $fd
3456 or print "Reading git-diff-tree failed\n";
3460 sub git_commitdiff_plain {
3461 git_commitdiff('plain');
3464 sub git_history {
3465 if (!defined $hash_base) {
3466 $hash_base = git_get_head_hash($project);
3468 if (!defined $page) {
3469 $page = 0;
3471 my $ftype;
3472 my %co = parse_commit($hash_base);
3473 if (!%co) {
3474 die_error(undef, "Unknown commit object");
3477 my $refs = git_get_references();
3478 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3480 if (!defined $hash && defined $file_name) {
3481 $hash = git_get_hash_by_path($hash_base, $file_name);
3483 if (defined $hash) {
3484 $ftype = git_get_type($hash);
3487 open my $fd, "-|",
3488 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3489 or die_error(undef, "Open git-rev-list-failed");
3490 my @revlist = map { chomp; $_ } <$fd>;
3491 close $fd
3492 or die_error(undef, "Reading git-rev-list failed");
3494 my $paging_nav = '';
3495 if ($page > 0) {
3496 $paging_nav .=
3497 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3498 file_name=>$file_name)},
3499 "first");
3500 $paging_nav .= " &sdot; " .
3501 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3502 file_name=>$file_name, page=>$page-1),
3503 -accesskey => "p", -title => "Alt-p"}, "prev");
3504 } else {
3505 $paging_nav .= "first";
3506 $paging_nav .= " &sdot; prev";
3508 if ($#revlist >= (100 * ($page+1)-1)) {
3509 $paging_nav .= " &sdot; " .
3510 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3511 file_name=>$file_name, page=>$page+1),
3512 -accesskey => "n", -title => "Alt-n"}, "next");
3513 } else {
3514 $paging_nav .= " &sdot; next";
3516 my $next_link = '';
3517 if ($#revlist >= (100 * ($page+1)-1)) {
3518 $next_link =
3519 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3520 file_name=>$file_name, page=>$page+1),
3521 -title => "Alt-n"}, "next");
3524 git_header_html();
3525 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3526 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3527 git_print_page_path($file_name, $ftype, $hash_base);
3529 git_history_body(\@revlist, ($page * 100), $#revlist,
3530 $refs, $hash_base, $ftype, $next_link);
3532 git_footer_html();
3535 sub git_search {
3536 if (!defined $searchtext) {
3537 die_error(undef, "Text field empty");
3539 if (!defined $hash) {
3540 $hash = git_get_head_hash($project);
3542 my %co = parse_commit($hash);
3543 if (!%co) {
3544 die_error(undef, "Unknown commit object");
3547 my $commit_search = 1;
3548 my $author_search = 0;
3549 my $committer_search = 0;
3550 my $pickaxe_search = 0;
3551 if ($searchtext =~ s/^author\\://i) {
3552 $author_search = 1;
3553 } elsif ($searchtext =~ s/^committer\\://i) {
3554 $committer_search = 1;
3555 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3556 $commit_search = 0;
3557 $pickaxe_search = 1;
3559 # pickaxe may take all resources of your box and run for several minutes
3560 # with every query - so decide by yourself how public you make this feature
3561 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3562 if (!$have_pickaxe) {
3563 die_error('403 Permission denied', "Permission denied");
3566 git_header_html();
3567 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3568 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3570 print "<table cellspacing=\"0\">\n";
3571 my $alternate = 1;
3572 if ($commit_search) {
3573 $/ = "\0";
3574 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3575 while (my $commit_text = <$fd>) {
3576 if (!grep m/$searchtext/i, $commit_text) {
3577 next;
3579 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3580 next;
3582 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3583 next;
3585 my @commit_lines = split "\n", $commit_text;
3586 my %co = parse_commit(undef, \@commit_lines);
3587 if (!%co) {
3588 next;
3590 if ($alternate) {
3591 print "<tr class=\"dark\">\n";
3592 } else {
3593 print "<tr class=\"light\">\n";
3595 $alternate ^= 1;
3596 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3597 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3598 "<td>" .
3599 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3600 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3601 my $comment = $co{'comment'};
3602 foreach my $line (@$comment) {
3603 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3604 my $lead = esc_html($1) || "";
3605 $lead = chop_str($lead, 30, 10);
3606 my $match = esc_html($2) || "";
3607 my $trail = esc_html($3) || "";
3608 $trail = chop_str($trail, 30, 10);
3609 my $text = "$lead<span class=\"match\">$match</span>$trail";
3610 print chop_str($text, 80, 5) . "<br/>\n";
3613 print "</td>\n" .
3614 "<td class=\"link\">" .
3615 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3616 " | " .
3617 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3618 print "</td>\n" .
3619 "</tr>\n";
3621 close $fd;
3624 if ($pickaxe_search) {
3625 $/ = "\n";
3626 my $git_command = git_cmd_str();
3627 open my $fd, "-|", "$git_command rev-list $hash | " .
3628 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3629 undef %co;
3630 my @files;
3631 while (my $line = <$fd>) {
3632 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3633 my %set;
3634 $set{'file'} = $6;
3635 $set{'from_id'} = $3;
3636 $set{'to_id'} = $4;
3637 $set{'id'} = $set{'to_id'};
3638 if ($set{'id'} =~ m/0{40}/) {
3639 $set{'id'} = $set{'from_id'};
3641 if ($set{'id'} =~ m/0{40}/) {
3642 next;
3644 push @files, \%set;
3645 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3646 if (%co) {
3647 if ($alternate) {
3648 print "<tr class=\"dark\">\n";
3649 } else {
3650 print "<tr class=\"light\">\n";
3652 $alternate ^= 1;
3653 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3654 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3655 "<td>" .
3656 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3657 -class => "list subject"},
3658 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3659 while (my $setref = shift @files) {
3660 my %set = %$setref;
3661 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3662 hash=>$set{'id'}, file_name=>$set{'file'}),
3663 -class => "list"},
3664 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3665 "<br/>\n";
3667 print "</td>\n" .
3668 "<td class=\"link\">" .
3669 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3670 " | " .
3671 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3672 print "</td>\n" .
3673 "</tr>\n";
3675 %co = parse_commit($1);
3678 close $fd;
3680 print "</table>\n";
3681 git_footer_html();
3684 sub git_shortlog {
3685 my $head = git_get_head_hash($project);
3686 if (!defined $hash) {
3687 $hash = $head;
3689 if (!defined $page) {
3690 $page = 0;
3692 my $refs = git_get_references();
3694 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3695 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3696 or die_error(undef, "Open git-rev-list failed");
3697 my @revlist = map { chomp; $_ } <$fd>;
3698 close $fd;
3700 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3701 my $next_link = '';
3702 if ($#revlist >= (100 * ($page+1)-1)) {
3703 $next_link =
3704 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3705 -title => "Alt-n"}, "next");
3709 git_header_html();
3710 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3711 git_print_header_div('summary', $project);
3713 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3715 git_footer_html();
3718 ## ......................................................................
3719 ## feeds (RSS, OPML)
3721 sub git_rss {
3722 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3723 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3724 or die_error(undef, "Open git-rev-list failed");
3725 my @revlist = map { chomp; $_ } <$fd>;
3726 close $fd or die_error(undef, "Reading git-rev-list failed");
3727 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3728 print <<XML;
3729 <?xml version="1.0" encoding="utf-8"?>
3730 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3731 <channel>
3732 <title>$project $my_uri $my_url</title>
3733 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3734 <description>$project log</description>
3735 <language>en</language>
3738 for (my $i = 0; $i <= $#revlist; $i++) {
3739 my $commit = $revlist[$i];
3740 my %co = parse_commit($commit);
3741 # we read 150, we always show 30 and the ones more recent than 48 hours
3742 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3743 last;
3745 my %cd = parse_date($co{'committer_epoch'});
3746 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3747 $co{'parent'}, $co{'id'}
3748 or next;
3749 my @difftree = map { chomp; $_ } <$fd>;
3750 close $fd
3751 or next;
3752 print "<item>\n" .
3753 "<title>" .
3754 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3755 "</title>\n" .
3756 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3757 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3758 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3759 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3760 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3761 "<content:encoded>" .
3762 "<![CDATA[\n";
3763 my $comment = $co{'comment'};
3764 foreach my $line (@$comment) {
3765 $line = to_utf8($line);
3766 print "$line<br/>\n";
3768 print "<br/>\n";
3769 foreach my $line (@difftree) {
3770 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3771 next;
3773 my $file = esc_html(unquote($7));
3774 $file = to_utf8($file);
3775 print "$file<br/>\n";
3777 print "]]>\n" .
3778 "</content:encoded>\n" .
3779 "</item>\n";
3781 print "</channel></rss>";
3784 sub git_opml {
3785 my @list = git_get_projects_list();
3787 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3788 print <<XML;
3789 <?xml version="1.0" encoding="utf-8"?>
3790 <opml version="1.0">
3791 <head>
3792 <title>$site_name Git OPML Export</title>
3793 </head>
3794 <body>
3795 <outline text="git RSS feeds">
3798 foreach my $pr (@list) {
3799 my %proj = %$pr;
3800 my $head = git_get_head_hash($proj{'path'});
3801 if (!defined $head) {
3802 next;
3804 $git_dir = "$projectroot/$proj{'path'}";
3805 my %co = parse_commit($head);
3806 if (!%co) {
3807 next;
3810 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3811 my $rss = "$my_url?p=$proj{'path'};a=rss";
3812 my $html = "$my_url?p=$proj{'path'};a=summary";
3813 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3815 print <<XML;
3816 </outline>
3817 </body>
3818 </opml>