1#!/usr/bin/perl
2
3# gitweb - simple web interface to track changes in git repositories
4#
5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6# (C) 2005, Christian Gierke
7#
8# This program is licensed under the GPLv2
9
10use strict;
11use warnings;
12use CGI qw(:standard :escapeHTML -nosticky);
13use CGI::Util qw(unescape);
14use CGI::Carp qw(fatalsToBrowser);
15use Encode;
16use Fcntl ':mode';
17use File::Find qw();
18use File::Basename qw(basename);
19binmode STDOUT, ':utf8';
20
21BEGIN {
22 CGI->compile() if $ENV{'MOD_PERL'};
23}
24
25our $cgi = new CGI;
26our $version = "++GIT_VERSION++";
27our $my_url = $cgi->url();
28our $my_uri = $cgi->url(-absolute => 1);
29
30# core git executable to use
31# this can just be "git" if your webserver has a sensible PATH
32our $GIT = "++GIT_BINDIR++/git";
33
34# absolute fs-path which will be prepended to the project path
35#our $projectroot = "/pub/scm";
36our $projectroot = "++GITWEB_PROJECTROOT++";
37
38# target of the home link on top of all pages
39our $home_link = $my_uri || "/";
40
41# string of the home link on top of all pages
42our $home_link_str = "++GITWEB_HOME_LINK_STR++";
43
44# name of your site or organization to appear in page titles
45# replace this with something more descriptive for clearer bookmarks
46our $site_name = "++GITWEB_SITENAME++"
47 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
48
49# filename of html text to include at top of each page
50our $site_header = "++GITWEB_SITE_HEADER++";
51# html text to include at home page
52our $home_text = "++GITWEB_HOMETEXT++";
53# filename of html text to include at bottom of each page
54our $site_footer = "++GITWEB_SITE_FOOTER++";
55
56# URI of stylesheets
57our @stylesheets = ("++GITWEB_CSS++");
58# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
59our $stylesheet = undef;
60# URI of GIT logo (72x27 size)
61our $logo = "++GITWEB_LOGO++";
62# URI of GIT favicon, assumed to be image/png type
63our $favicon = "++GITWEB_FAVICON++";
64
65# URI and label (title) of GIT logo link
66#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
67#our $logo_label = "git documentation";
68our $logo_url = "http://git.or.cz/";
69our $logo_label = "git homepage";
70
71# source of projects list
72our $projects_list = "++GITWEB_LIST++";
73
74# default order of projects list
75# valid values are none, project, descr, owner, and age
76our $default_projects_order = "project";
77
78# show repository only if this file exists
79# (only effective if this variable evaluates to true)
80our $export_ok = "++GITWEB_EXPORT_OK++";
81
82# only allow viewing of repositories also shown on the overview page
83our $strict_export = "++GITWEB_STRICT_EXPORT++";
84
85# list of git base URLs used for URL to where fetch project from,
86# i.e. full URL is "$git_base_url/$project"
87our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
88
89# default blob_plain mimetype and default charset for text/plain blob
90our $default_blob_plain_mimetype = 'text/plain';
91our $default_text_plain_charset = undef;
92
93# file to use for guessing MIME types before trying /etc/mime.types
94# (relative to the current git repository)
95our $mimetypes_file = undef;
96
97# You define site-wide feature defaults here; override them with
98# $GITWEB_CONFIG as necessary.
99our %feature = (
100 # feature => {
101 # 'sub' => feature-sub (subroutine),
102 # 'override' => allow-override (boolean),
103 # 'default' => [ default options...] (array reference)}
104 #
105 # if feature is overridable (it means that allow-override has true value),
106 # then feature-sub will be called with default options as parameters;
107 # return value of feature-sub indicates if to enable specified feature
108 #
109 # if there is no 'sub' key (no feature-sub), then feature cannot be
110 # overriden
111 #
112 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
113
114 # Enable the 'blame' blob view, showing the last commit that modified
115 # each line in the file. This can be very CPU-intensive.
116
117 # To enable system wide have in $GITWEB_CONFIG
118 # $feature{'blame'}{'default'} = [1];
119 # To have project specific config enable override in $GITWEB_CONFIG
120 # $feature{'blame'}{'override'} = 1;
121 # and in project config gitweb.blame = 0|1;
122 'blame' => {
123 'sub' => \&feature_blame,
124 'override' => 0,
125 'default' => [0]},
126
127 # Enable the 'snapshot' link, providing a compressed tarball of any
128 # tree. This can potentially generate high traffic if you have large
129 # project.
130
131 # To disable system wide have in $GITWEB_CONFIG
132 # $feature{'snapshot'}{'default'} = [undef];
133 # To have project specific config enable override in $GITWEB_CONFIG
134 # $feature{'snapshot'}{'override'} = 1;
135 # and in project config gitweb.snapshot = none|gzip|bzip2;
136 'snapshot' => {
137 'sub' => \&feature_snapshot,
138 'override' => 0,
139 # => [content-encoding, suffix, program]
140 'default' => ['x-gzip', 'gz', 'gzip']},
141
142 # Enable text search, which will list the commits which match author,
143 # committer or commit text to a given string. Enabled by default.
144 # Project specific override is not supported.
145 'search' => {
146 'override' => 0,
147 'default' => [1]},
148
149 # Enable the pickaxe search, which will list the commits that modified
150 # a given string in a file. This can be practical and quite faster
151 # alternative to 'blame', but still potentially CPU-intensive.
152
153 # To enable system wide have in $GITWEB_CONFIG
154 # $feature{'pickaxe'}{'default'} = [1];
155 # To have project specific config enable override in $GITWEB_CONFIG
156 # $feature{'pickaxe'}{'override'} = 1;
157 # and in project config gitweb.pickaxe = 0|1;
158 'pickaxe' => {
159 'sub' => \&feature_pickaxe,
160 'override' => 0,
161 'default' => [1]},
162
163 # Make gitweb use an alternative format of the URLs which can be
164 # more readable and natural-looking: project name is embedded
165 # directly in the path and the query string contains other
166 # auxiliary information. All gitweb installations recognize
167 # URL in either format; this configures in which formats gitweb
168 # generates links.
169
170 # To enable system wide have in $GITWEB_CONFIG
171 # $feature{'pathinfo'}{'default'} = [1];
172 # Project specific override is not supported.
173
174 # Note that you will need to change the default location of CSS,
175 # favicon, logo and possibly other files to an absolute URL. Also,
176 # if gitweb.cgi serves as your indexfile, you will need to force
177 # $my_uri to contain the script name in your $GITWEB_CONFIG.
178 'pathinfo' => {
179 'override' => 0,
180 'default' => [0]},
181
182 # Make gitweb consider projects in project root subdirectories
183 # to be forks of existing projects. Given project $projname.git,
184 # projects matching $projname/*.git will not be shown in the main
185 # projects list, instead a '+' mark will be added to $projname
186 # there and a 'forks' view will be enabled for the project, listing
187 # all the forks. If project list is taken from a file, forks have
188 # to be listed after the main project.
189
190 # To enable system wide have in $GITWEB_CONFIG
191 # $feature{'forks'}{'default'} = [1];
192 # Project specific override is not supported.
193 'forks' => {
194 'override' => 0,
195 'default' => [0]},
196);
197
198sub gitweb_check_feature {
199 my ($name) = @_;
200 return unless exists $feature{$name};
201 my ($sub, $override, @defaults) = (
202 $feature{$name}{'sub'},
203 $feature{$name}{'override'},
204 @{$feature{$name}{'default'}});
205 if (!$override) { return @defaults; }
206 if (!defined $sub) {
207 warn "feature $name is not overrideable";
208 return @defaults;
209 }
210 return $sub->(@defaults);
211}
212
213sub feature_blame {
214 my ($val) = git_get_project_config('blame', '--bool');
215
216 if ($val eq 'true') {
217 return 1;
218 } elsif ($val eq 'false') {
219 return 0;
220 }
221
222 return $_[0];
223}
224
225sub feature_snapshot {
226 my ($ctype, $suffix, $command) = @_;
227
228 my ($val) = git_get_project_config('snapshot');
229
230 if ($val eq 'gzip') {
231 return ('x-gzip', 'gz', 'gzip');
232 } elsif ($val eq 'bzip2') {
233 return ('x-bzip2', 'bz2', 'bzip2');
234 } elsif ($val eq 'none') {
235 return ();
236 }
237
238 return ($ctype, $suffix, $command);
239}
240
241sub gitweb_have_snapshot {
242 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
243 my $have_snapshot = (defined $ctype && defined $suffix);
244
245 return $have_snapshot;
246}
247
248sub feature_pickaxe {
249 my ($val) = git_get_project_config('pickaxe', '--bool');
250
251 if ($val eq 'true') {
252 return (1);
253 } elsif ($val eq 'false') {
254 return (0);
255 }
256
257 return ($_[0]);
258}
259
260# checking HEAD file with -e is fragile if the repository was
261# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
262# and then pruned.
263sub check_head_link {
264 my ($dir) = @_;
265 my $headfile = "$dir/HEAD";
266 return ((-e $headfile) ||
267 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
268}
269
270sub check_export_ok {
271 my ($dir) = @_;
272 return (check_head_link($dir) &&
273 (!$export_ok || -e "$dir/$export_ok"));
274}
275
276# rename detection options for git-diff and git-diff-tree
277# - default is '-M', with the cost proportional to
278# (number of removed files) * (number of new files).
279# - more costly is '-C' (or '-C', '-M'), with the cost proportional to
280# (number of changed files + number of removed files) * (number of new files)
281# - even more costly is '-C', '--find-copies-harder' with cost
282# (number of files in the original tree) * (number of new files)
283# - one might want to include '-B' option, e.g. '-B', '-M'
284our @diff_opts = ('-M'); # taken from git_commit
285
286our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
287do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
288
289# version of the core git binary
290our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
291
292$projects_list ||= $projectroot;
293
294# ======================================================================
295# input validation and dispatch
296our $action = $cgi->param('a');
297if (defined $action) {
298 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
299 die_error(undef, "Invalid action parameter");
300 }
301}
302
303# parameters which are pathnames
304our $project = $cgi->param('p');
305if (defined $project) {
306 if (!validate_pathname($project) ||
307 !(-d "$projectroot/$project") ||
308 !check_head_link("$projectroot/$project") ||
309 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
310 ($strict_export && !project_in_list($project))) {
311 undef $project;
312 die_error(undef, "No such project");
313 }
314}
315
316our $file_name = $cgi->param('f');
317if (defined $file_name) {
318 if (!validate_pathname($file_name)) {
319 die_error(undef, "Invalid file parameter");
320 }
321}
322
323our $file_parent = $cgi->param('fp');
324if (defined $file_parent) {
325 if (!validate_pathname($file_parent)) {
326 die_error(undef, "Invalid file parent parameter");
327 }
328}
329
330# parameters which are refnames
331our $hash = $cgi->param('h');
332if (defined $hash) {
333 if (!validate_refname($hash)) {
334 die_error(undef, "Invalid hash parameter");
335 }
336}
337
338our $hash_parent = $cgi->param('hp');
339if (defined $hash_parent) {
340 if (!validate_refname($hash_parent)) {
341 die_error(undef, "Invalid hash parent parameter");
342 }
343}
344
345our $hash_base = $cgi->param('hb');
346if (defined $hash_base) {
347 if (!validate_refname($hash_base)) {
348 die_error(undef, "Invalid hash base parameter");
349 }
350}
351
352our $hash_parent_base = $cgi->param('hpb');
353if (defined $hash_parent_base) {
354 if (!validate_refname($hash_parent_base)) {
355 die_error(undef, "Invalid hash parent base parameter");
356 }
357}
358
359# other parameters
360our $page = $cgi->param('pg');
361if (defined $page) {
362 if ($page =~ m/[^0-9]/) {
363 die_error(undef, "Invalid page parameter");
364 }
365}
366
367our $searchtext = $cgi->param('s');
368our $search_regexp;
369if (defined $searchtext) {
370 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
371 die_error(undef, "Invalid search parameter");
372 }
373 if (length($searchtext) < 2) {
374 die_error(undef, "At least two characters are required for search parameter");
375 }
376 $search_regexp = quotemeta $searchtext;
377}
378
379our $searchtype = $cgi->param('st');
380if (defined $searchtype) {
381 if ($searchtype =~ m/[^a-z]/) {
382 die_error(undef, "Invalid searchtype parameter");
383 }
384}
385
386# now read PATH_INFO and use it as alternative to parameters
387sub evaluate_path_info {
388 return if defined $project;
389 my $path_info = $ENV{"PATH_INFO"};
390 return if !$path_info;
391 $path_info =~ s,^/+,,;
392 return if !$path_info;
393 # find which part of PATH_INFO is project
394 $project = $path_info;
395 $project =~ s,/+$,,;
396 while ($project && !check_head_link("$projectroot/$project")) {
397 $project =~ s,/*[^/]*$,,;
398 }
399 # validate project
400 $project = validate_pathname($project);
401 if (!$project ||
402 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
403 ($strict_export && !project_in_list($project))) {
404 undef $project;
405 return;
406 }
407 # do not change any parameters if an action is given using the query string
408 return if $action;
409 $path_info =~ s,^$project/*,,;
410 my ($refname, $pathname) = split(/:/, $path_info, 2);
411 if (defined $pathname) {
412 # we got "project.git/branch:filename" or "project.git/branch:dir/"
413 # we could use git_get_type(branch:pathname), but it needs $git_dir
414 $pathname =~ s,^/+,,;
415 if (!$pathname || substr($pathname, -1) eq "/") {
416 $action ||= "tree";
417 $pathname =~ s,/$,,;
418 } else {
419 $action ||= "blob_plain";
420 }
421 $hash_base ||= validate_refname($refname);
422 $file_name ||= validate_pathname($pathname);
423 } elsif (defined $refname) {
424 # we got "project.git/branch"
425 $action ||= "shortlog";
426 $hash ||= validate_refname($refname);
427 }
428}
429evaluate_path_info();
430
431# path to the current git repository
432our $git_dir;
433$git_dir = "$projectroot/$project" if $project;
434
435# dispatch
436my %actions = (
437 "blame" => \&git_blame2,
438 "blobdiff" => \&git_blobdiff,
439 "blobdiff_plain" => \&git_blobdiff_plain,
440 "blob" => \&git_blob,
441 "blob_plain" => \&git_blob_plain,
442 "commitdiff" => \&git_commitdiff,
443 "commitdiff_plain" => \&git_commitdiff_plain,
444 "commit" => \&git_commit,
445 "forks" => \&git_forks,
446 "heads" => \&git_heads,
447 "history" => \&git_history,
448 "log" => \&git_log,
449 "rss" => \&git_rss,
450 "atom" => \&git_atom,
451 "search" => \&git_search,
452 "search_help" => \&git_search_help,
453 "shortlog" => \&git_shortlog,
454 "summary" => \&git_summary,
455 "tag" => \&git_tag,
456 "tags" => \&git_tags,
457 "tree" => \&git_tree,
458 "snapshot" => \&git_snapshot,
459 "object" => \&git_object,
460 # those below don't need $project
461 "opml" => \&git_opml,
462 "project_list" => \&git_project_list,
463 "project_index" => \&git_project_index,
464);
465
466if (!defined $action) {
467 if (defined $hash) {
468 $action = git_get_type($hash);
469 } elsif (defined $hash_base && defined $file_name) {
470 $action = git_get_type("$hash_base:$file_name");
471 } elsif (defined $project) {
472 $action = 'summary';
473 } else {
474 $action = 'project_list';
475 }
476}
477if (!defined($actions{$action})) {
478 die_error(undef, "Unknown action");
479}
480if ($action !~ m/^(opml|project_list|project_index)$/ &&
481 !$project) {
482 die_error(undef, "Project needed");
483}
484$actions{$action}->();
485exit;
486
487## ======================================================================
488## action links
489
490sub href(%) {
491 my %params = @_;
492 # default is to use -absolute url() i.e. $my_uri
493 my $href = $params{-full} ? $my_url : $my_uri;
494
495 # XXX: Warning: If you touch this, check the search form for updating,
496 # too.
497
498 my @mapping = (
499 project => "p",
500 action => "a",
501 file_name => "f",
502 file_parent => "fp",
503 hash => "h",
504 hash_parent => "hp",
505 hash_base => "hb",
506 hash_parent_base => "hpb",
507 page => "pg",
508 order => "o",
509 searchtext => "s",
510 searchtype => "st",
511 );
512 my %mapping = @mapping;
513
514 $params{'project'} = $project unless exists $params{'project'};
515
516 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
517 if ($use_pathinfo) {
518 # use PATH_INFO for project name
519 $href .= "/$params{'project'}" if defined $params{'project'};
520 delete $params{'project'};
521
522 # Summary just uses the project path URL
523 if (defined $params{'action'} && $params{'action'} eq 'summary') {
524 delete $params{'action'};
525 }
526 }
527
528 # now encode the parameters explicitly
529 my @result = ();
530 for (my $i = 0; $i < @mapping; $i += 2) {
531 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
532 if (defined $params{$name}) {
533 push @result, $symbol . "=" . esc_param($params{$name});
534 }
535 }
536 $href .= "?" . join(';', @result) if scalar @result;
537
538 return $href;
539}
540
541
542## ======================================================================
543## validation, quoting/unquoting and escaping
544
545sub validate_pathname {
546 my $input = shift || return undef;
547
548 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
549 # at the beginning, at the end, and between slashes.
550 # also this catches doubled slashes
551 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
552 return undef;
553 }
554 # no null characters
555 if ($input =~ m!\0!) {
556 return undef;
557 }
558 return $input;
559}
560
561sub validate_refname {
562 my $input = shift || return undef;
563
564 # textual hashes are O.K.
565 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
566 return $input;
567 }
568 # it must be correct pathname
569 $input = validate_pathname($input)
570 or return undef;
571 # restrictions on ref name according to git-check-ref-format
572 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
573 return undef;
574 }
575 return $input;
576}
577
578# quote unsafe chars, but keep the slash, even when it's not
579# correct, but quoted slashes look too horrible in bookmarks
580sub esc_param {
581 my $str = shift;
582 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
583 $str =~ s/\+/%2B/g;
584 $str =~ s/ /\+/g;
585 return $str;
586}
587
588# quote unsafe chars in whole URL, so some charactrs cannot be quoted
589sub esc_url {
590 my $str = shift;
591 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
592 $str =~ s/\+/%2B/g;
593 $str =~ s/ /\+/g;
594 return $str;
595}
596
597# replace invalid utf8 character with SUBSTITUTION sequence
598sub esc_html ($;%) {
599 my $str = shift;
600 my %opts = @_;
601
602 $str = decode_utf8($str);
603 $str = $cgi->escapeHTML($str);
604 if ($opts{'-nbsp'}) {
605 $str =~ s/ / /g;
606 }
607 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
608 return $str;
609}
610
611# quote control characters and escape filename to HTML
612sub esc_path {
613 my $str = shift;
614 my %opts = @_;
615
616 $str = decode_utf8($str);
617 $str = $cgi->escapeHTML($str);
618 if ($opts{'-nbsp'}) {
619 $str =~ s/ / /g;
620 }
621 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
622 return $str;
623}
624
625# Make control characters "printable", using character escape codes (CEC)
626sub quot_cec {
627 my $cntrl = shift;
628 my %es = ( # character escape codes, aka escape sequences
629 "\t" => '\t', # tab (HT)
630 "\n" => '\n', # line feed (LF)
631 "\r" => '\r', # carrige return (CR)
632 "\f" => '\f', # form feed (FF)
633 "\b" => '\b', # backspace (BS)
634 "\a" => '\a', # alarm (bell) (BEL)
635 "\e" => '\e', # escape (ESC)
636 "\013" => '\v', # vertical tab (VT)
637 "\000" => '\0', # nul character (NUL)
638 );
639 my $chr = ( (exists $es{$cntrl})
640 ? $es{$cntrl}
641 : sprintf('\%03o', ord($cntrl)) );
642 return "<span class=\"cntrl\">$chr</span>";
643}
644
645# Alternatively use unicode control pictures codepoints,
646# Unicode "printable representation" (PR)
647sub quot_upr {
648 my $cntrl = shift;
649 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
650 return "<span class=\"cntrl\">$chr</span>";
651}
652
653# git may return quoted and escaped filenames
654sub unquote {
655 my $str = shift;
656
657 sub unq {
658 my $seq = shift;
659 my %es = ( # character escape codes, aka escape sequences
660 't' => "\t", # tab (HT, TAB)
661 'n' => "\n", # newline (NL)
662 'r' => "\r", # return (CR)
663 'f' => "\f", # form feed (FF)
664 'b' => "\b", # backspace (BS)
665 'a' => "\a", # alarm (bell) (BEL)
666 'e' => "\e", # escape (ESC)
667 'v' => "\013", # vertical tab (VT)
668 );
669
670 if ($seq =~ m/^[0-7]{1,3}$/) {
671 # octal char sequence
672 return chr(oct($seq));
673 } elsif (exists $es{$seq}) {
674 # C escape sequence, aka character escape code
675 return $es{$seq}
676 }
677 # quoted ordinary character
678 return $seq;
679 }
680
681 if ($str =~ m/^"(.*)"$/) {
682 # needs unquoting
683 $str = $1;
684 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
685 }
686 return $str;
687}
688
689# escape tabs (convert tabs to spaces)
690sub untabify {
691 my $line = shift;
692
693 while ((my $pos = index($line, "\t")) != -1) {
694 if (my $count = (8 - ($pos % 8))) {
695 my $spaces = ' ' x $count;
696 $line =~ s/\t/$spaces/;
697 }
698 }
699
700 return $line;
701}
702
703sub project_in_list {
704 my $project = shift;
705 my @list = git_get_projects_list();
706 return @list && scalar(grep { $_->{'path'} eq $project } @list);
707}
708
709## ----------------------------------------------------------------------
710## HTML aware string manipulation
711
712sub chop_str {
713 my $str = shift;
714 my $len = shift;
715 my $add_len = shift || 10;
716
717 # allow only $len chars, but don't cut a word if it would fit in $add_len
718 # if it doesn't fit, cut it if it's still longer than the dots we would add
719 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
720 my $body = $1;
721 my $tail = $2;
722 if (length($tail) > 4) {
723 $tail = " ...";
724 $body =~ s/&[^;]*$//; # remove chopped character entities
725 }
726 return "$body$tail";
727}
728
729## ----------------------------------------------------------------------
730## functions returning short strings
731
732# CSS class for given age value (in seconds)
733sub age_class {
734 my $age = shift;
735
736 if (!defined $age) {
737 return "noage";
738 } elsif ($age < 60*60*2) {
739 return "age0";
740 } elsif ($age < 60*60*24*2) {
741 return "age1";
742 } else {
743 return "age2";
744 }
745}
746
747# convert age in seconds to "nn units ago" string
748sub age_string {
749 my $age = shift;
750 my $age_str;
751
752 if ($age > 60*60*24*365*2) {
753 $age_str = (int $age/60/60/24/365);
754 $age_str .= " years ago";
755 } elsif ($age > 60*60*24*(365/12)*2) {
756 $age_str = int $age/60/60/24/(365/12);
757 $age_str .= " months ago";
758 } elsif ($age > 60*60*24*7*2) {
759 $age_str = int $age/60/60/24/7;
760 $age_str .= " weeks ago";
761 } elsif ($age > 60*60*24*2) {
762 $age_str = int $age/60/60/24;
763 $age_str .= " days ago";
764 } elsif ($age > 60*60*2) {
765 $age_str = int $age/60/60;
766 $age_str .= " hours ago";
767 } elsif ($age > 60*2) {
768 $age_str = int $age/60;
769 $age_str .= " min ago";
770 } elsif ($age > 2) {
771 $age_str = int $age;
772 $age_str .= " sec ago";
773 } else {
774 $age_str .= " right now";
775 }
776 return $age_str;
777}
778
779# convert file mode in octal to symbolic file mode string
780sub mode_str {
781 my $mode = oct shift;
782
783 if (S_ISDIR($mode & S_IFMT)) {
784 return 'drwxr-xr-x';
785 } elsif (S_ISLNK($mode)) {
786 return 'lrwxrwxrwx';
787 } elsif (S_ISREG($mode)) {
788 # git cares only about the executable bit
789 if ($mode & S_IXUSR) {
790 return '-rwxr-xr-x';
791 } else {
792 return '-rw-r--r--';
793 };
794 } else {
795 return '----------';
796 }
797}
798
799# convert file mode in octal to file type string
800sub file_type {
801 my $mode = shift;
802
803 if ($mode !~ m/^[0-7]+$/) {
804 return $mode;
805 } else {
806 $mode = oct $mode;
807 }
808
809 if (S_ISDIR($mode & S_IFMT)) {
810 return "directory";
811 } elsif (S_ISLNK($mode)) {
812 return "symlink";
813 } elsif (S_ISREG($mode)) {
814 return "file";
815 } else {
816 return "unknown";
817 }
818}
819
820# convert file mode in octal to file type description string
821sub file_type_long {
822 my $mode = shift;
823
824 if ($mode !~ m/^[0-7]+$/) {
825 return $mode;
826 } else {
827 $mode = oct $mode;
828 }
829
830 if (S_ISDIR($mode & S_IFMT)) {
831 return "directory";
832 } elsif (S_ISLNK($mode)) {
833 return "symlink";
834 } elsif (S_ISREG($mode)) {
835 if ($mode & S_IXUSR) {
836 return "executable";
837 } else {
838 return "file";
839 };
840 } else {
841 return "unknown";
842 }
843}
844
845
846## ----------------------------------------------------------------------
847## functions returning short HTML fragments, or transforming HTML fragments
848## which don't belong to other sections
849
850# format line of commit message.
851sub format_log_line_html {
852 my $line = shift;
853
854 $line = esc_html($line, -nbsp=>1);
855 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
856 my $hash_text = $1;
857 my $link =
858 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
859 -class => "text"}, $hash_text);
860 $line =~ s/$hash_text/$link/;
861 }
862 return $line;
863}
864
865# format marker of refs pointing to given object
866sub format_ref_marker {
867 my ($refs, $id) = @_;
868 my $markers = '';
869
870 if (defined $refs->{$id}) {
871 foreach my $ref (@{$refs->{$id}}) {
872 my ($type, $name) = qw();
873 # e.g. tags/v2.6.11 or heads/next
874 if ($ref =~ m!^(.*?)s?/(.*)$!) {
875 $type = $1;
876 $name = $2;
877 } else {
878 $type = "ref";
879 $name = $ref;
880 }
881
882 $markers .= " <span class=\"$type\" title=\"$ref\">" .
883 esc_html($name) . "</span>";
884 }
885 }
886
887 if ($markers) {
888 return ' <span class="refs">'. $markers . '</span>';
889 } else {
890 return "";
891 }
892}
893
894# format, perhaps shortened and with markers, title line
895sub format_subject_html {
896 my ($long, $short, $href, $extra) = @_;
897 $extra = '' unless defined($extra);
898
899 if (length($short) < length($long)) {
900 return $cgi->a({-href => $href, -class => "list subject",
901 -title => decode_utf8($long)},
902 esc_html($short) . $extra);
903 } else {
904 return $cgi->a({-href => $href, -class => "list subject"},
905 esc_html($long) . $extra);
906 }
907}
908
909# format patch (diff) line (rather not to be used for diff headers)
910sub format_diff_line {
911 my $line = shift;
912 my ($from, $to) = @_;
913 my $diff_class = "";
914
915 chomp $line;
916
917 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
918 # combined diff
919 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
920 if ($line =~ m/^\@{3}/) {
921 $diff_class = " chunk_header";
922 } elsif ($line =~ m/^\\/) {
923 $diff_class = " incomplete";
924 } elsif ($prefix =~ tr/+/+/) {
925 $diff_class = " add";
926 } elsif ($prefix =~ tr/-/-/) {
927 $diff_class = " rem";
928 }
929 } else {
930 # assume ordinary diff
931 my $char = substr($line, 0, 1);
932 if ($char eq '+') {
933 $diff_class = " add";
934 } elsif ($char eq '-') {
935 $diff_class = " rem";
936 } elsif ($char eq '@') {
937 $diff_class = " chunk_header";
938 } elsif ($char eq "\\") {
939 $diff_class = " incomplete";
940 }
941 }
942 $line = untabify($line);
943 if ($from && $to && $line =~ m/^\@{2} /) {
944 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
945 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
946
947 $from_lines = 0 unless defined $from_lines;
948 $to_lines = 0 unless defined $to_lines;
949
950 if ($from->{'href'}) {
951 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
952 -class=>"list"}, $from_text);
953 }
954 if ($to->{'href'}) {
955 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
956 -class=>"list"}, $to_text);
957 }
958 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
959 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
960 return "<div class=\"diff$diff_class\">$line</div>\n";
961 } elsif ($from && $to && $line =~ m/^\@{3}/) {
962 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
963 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
964
965 @from_text = split(' ', $ranges);
966 for (my $i = 0; $i < @from_text; ++$i) {
967 ($from_start[$i], $from_nlines[$i]) =
968 (split(',', substr($from_text[$i], 1)), 0);
969 }
970
971 $to_text = pop @from_text;
972 $to_start = pop @from_start;
973 $to_nlines = pop @from_nlines;
974
975 $line = "<span class=\"chunk_info\">$prefix ";
976 for (my $i = 0; $i < @from_text; ++$i) {
977 if ($from->{'href'}[$i]) {
978 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
979 -class=>"list"}, $from_text[$i]);
980 } else {
981 $line .= $from_text[$i];
982 }
983 $line .= " ";
984 }
985 if ($to->{'href'}) {
986 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
987 -class=>"list"}, $to_text);
988 } else {
989 $line .= $to_text;
990 }
991 $line .= " $prefix</span>" .
992 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
993 return "<div class=\"diff$diff_class\">$line</div>\n";
994 }
995 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
996}
997
998## ----------------------------------------------------------------------
999## git utility subroutines, invoking git commands
1000
1001# returns path to the core git executable and the --git-dir parameter as list
1002sub git_cmd {
1003 return $GIT, '--git-dir='.$git_dir;
1004}
1005
1006# returns path to the core git executable and the --git-dir parameter as string
1007sub git_cmd_str {
1008 return join(' ', git_cmd());
1009}
1010
1011# get HEAD ref of given project as hash
1012sub git_get_head_hash {
1013 my $project = shift;
1014 my $o_git_dir = $git_dir;
1015 my $retval = undef;
1016 $git_dir = "$projectroot/$project";
1017 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1018 my $head = <$fd>;
1019 close $fd;
1020 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1021 $retval = $1;
1022 }
1023 }
1024 if (defined $o_git_dir) {
1025 $git_dir = $o_git_dir;
1026 }
1027 return $retval;
1028}
1029
1030# get type of given object
1031sub git_get_type {
1032 my $hash = shift;
1033
1034 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1035 my $type = <$fd>;
1036 close $fd or return;
1037 chomp $type;
1038 return $type;
1039}
1040
1041sub git_get_project_config {
1042 my ($key, $type) = @_;
1043
1044 return unless ($key);
1045 $key =~ s/^gitweb\.//;
1046 return if ($key =~ m/\W/);
1047
1048 my @x = (git_cmd(), 'config');
1049 if (defined $type) { push @x, $type; }
1050 push @x, "--get";
1051 push @x, "gitweb.$key";
1052 my $val = qx(@x);
1053 chomp $val;
1054 return ($val);
1055}
1056
1057# get hash of given path at given ref
1058sub git_get_hash_by_path {
1059 my $base = shift;
1060 my $path = shift || return undef;
1061 my $type = shift;
1062
1063 $path =~ s,/+$,,;
1064
1065 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1066 or die_error(undef, "Open git-ls-tree failed");
1067 my $line = <$fd>;
1068 close $fd or return undef;
1069
1070 if (!defined $line) {
1071 # there is no tree or hash given by $path at $base
1072 return undef;
1073 }
1074
1075 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1076 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1077 if (defined $type && $type ne $2) {
1078 # type doesn't match
1079 return undef;
1080 }
1081 return $3;
1082}
1083
1084# get path of entry with given hash at given tree-ish (ref)
1085# used to get 'from' filename for combined diff (merge commit) for renames
1086sub git_get_path_by_hash {
1087 my $base = shift || return;
1088 my $hash = shift || return;
1089
1090 local $/ = "\0";
1091
1092 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1093 or return undef;
1094 while (my $line = <$fd>) {
1095 chomp $line;
1096
1097 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1098 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1099 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1100 close $fd;
1101 return $1;
1102 }
1103 }
1104 close $fd;
1105 return undef;
1106}
1107
1108## ......................................................................
1109## git utility functions, directly accessing git repository
1110
1111sub git_get_project_description {
1112 my $path = shift;
1113
1114 open my $fd, "$projectroot/$path/description" or return undef;
1115 my $descr = <$fd>;
1116 close $fd;
1117 if (defined $descr) {
1118 chomp $descr;
1119 }
1120 return $descr;
1121}
1122
1123sub git_get_project_url_list {
1124 my $path = shift;
1125
1126 open my $fd, "$projectroot/$path/cloneurl" or return;
1127 my @git_project_url_list = map { chomp; $_ } <$fd>;
1128 close $fd;
1129
1130 return wantarray ? @git_project_url_list : \@git_project_url_list;
1131}
1132
1133sub git_get_projects_list {
1134 my ($filter) = @_;
1135 my @list;
1136
1137 $filter ||= '';
1138 $filter =~ s/\.git$//;
1139
1140 my ($check_forks) = gitweb_check_feature('forks');
1141
1142 if (-d $projects_list) {
1143 # search in directory
1144 my $dir = $projects_list . ($filter ? "/$filter" : '');
1145 # remove the trailing "/"
1146 $dir =~ s!/+$!!;
1147 my $pfxlen = length("$dir");
1148
1149 File::Find::find({
1150 follow_fast => 1, # follow symbolic links
1151 dangling_symlinks => 0, # ignore dangling symlinks, silently
1152 wanted => sub {
1153 # skip project-list toplevel, if we get it.
1154 return if (m!^[/.]$!);
1155 # only directories can be git repositories
1156 return unless (-d $_);
1157
1158 my $subdir = substr($File::Find::name, $pfxlen + 1);
1159 # we check related file in $projectroot
1160 if ($check_forks and $subdir =~ m#/.#) {
1161 $File::Find::prune = 1;
1162 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1163 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1164 $File::Find::prune = 1;
1165 }
1166 },
1167 }, "$dir");
1168
1169 } elsif (-f $projects_list) {
1170 # read from file(url-encoded):
1171 # 'git%2Fgit.git Linus+Torvalds'
1172 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1173 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1174 my %paths;
1175 open my ($fd), $projects_list or return;
1176 PROJECT:
1177 while (my $line = <$fd>) {
1178 chomp $line;
1179 my ($path, $owner) = split ' ', $line;
1180 $path = unescape($path);
1181 $owner = unescape($owner);
1182 if (!defined $path) {
1183 next;
1184 }
1185 if ($filter ne '') {
1186 # looking for forks;
1187 my $pfx = substr($path, 0, length($filter));
1188 if ($pfx ne $filter) {
1189 next PROJECT;
1190 }
1191 my $sfx = substr($path, length($filter));
1192 if ($sfx !~ /^\/.*\.git$/) {
1193 next PROJECT;
1194 }
1195 } elsif ($check_forks) {
1196 PATH:
1197 foreach my $filter (keys %paths) {
1198 # looking for forks;
1199 my $pfx = substr($path, 0, length($filter));
1200 if ($pfx ne $filter) {
1201 next PATH;
1202 }
1203 my $sfx = substr($path, length($filter));
1204 if ($sfx !~ /^\/.*\.git$/) {
1205 next PATH;
1206 }
1207 # is a fork, don't include it in
1208 # the list
1209 next PROJECT;
1210 }
1211 }
1212 if (check_export_ok("$projectroot/$path")) {
1213 my $pr = {
1214 path => $path,
1215 owner => decode_utf8($owner),
1216 };
1217 push @list, $pr;
1218 (my $forks_path = $path) =~ s/\.git$//;
1219 $paths{$forks_path}++;
1220 }
1221 }
1222 close $fd;
1223 }
1224 return @list;
1225}
1226
1227sub git_get_project_owner {
1228 my $project = shift;
1229 my $owner;
1230
1231 return undef unless $project;
1232
1233 # read from file (url-encoded):
1234 # 'git%2Fgit.git Linus+Torvalds'
1235 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1236 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1237 if (-f $projects_list) {
1238 open (my $fd , $projects_list);
1239 while (my $line = <$fd>) {
1240 chomp $line;
1241 my ($pr, $ow) = split ' ', $line;
1242 $pr = unescape($pr);
1243 $ow = unescape($ow);
1244 if ($pr eq $project) {
1245 $owner = decode_utf8($ow);
1246 last;
1247 }
1248 }
1249 close $fd;
1250 }
1251 if (!defined $owner) {
1252 $owner = get_file_owner("$projectroot/$project");
1253 }
1254
1255 return $owner;
1256}
1257
1258sub git_get_last_activity {
1259 my ($path) = @_;
1260 my $fd;
1261
1262 $git_dir = "$projectroot/$path";
1263 open($fd, "-|", git_cmd(), 'for-each-ref',
1264 '--format=%(committer)',
1265 '--sort=-committerdate',
1266 '--count=1',
1267 'refs/heads') or return;
1268 my $most_recent = <$fd>;
1269 close $fd or return;
1270 if (defined $most_recent &&
1271 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1272 my $timestamp = $1;
1273 my $age = time - $timestamp;
1274 return ($age, age_string($age));
1275 }
1276}
1277
1278sub git_get_references {
1279 my $type = shift || "";
1280 my %refs;
1281 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1282 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1283 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1284 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1285 or return;
1286
1287 while (my $line = <$fd>) {
1288 chomp $line;
1289 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1290 if (defined $refs{$1}) {
1291 push @{$refs{$1}}, $2;
1292 } else {
1293 $refs{$1} = [ $2 ];
1294 }
1295 }
1296 }
1297 close $fd or return;
1298 return \%refs;
1299}
1300
1301sub git_get_rev_name_tags {
1302 my $hash = shift || return undef;
1303
1304 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1305 or return;
1306 my $name_rev = <$fd>;
1307 close $fd;
1308
1309 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1310 return $1;
1311 } else {
1312 # catches also '$hash undefined' output
1313 return undef;
1314 }
1315}
1316
1317## ----------------------------------------------------------------------
1318## parse to hash functions
1319
1320sub parse_date {
1321 my $epoch = shift;
1322 my $tz = shift || "-0000";
1323
1324 my %date;
1325 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1326 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1327 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1328 $date{'hour'} = $hour;
1329 $date{'minute'} = $min;
1330 $date{'mday'} = $mday;
1331 $date{'day'} = $days[$wday];
1332 $date{'month'} = $months[$mon];
1333 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1334 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1335 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1336 $mday, $months[$mon], $hour ,$min;
1337 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1338 1900+$year, $mon, $mday, $hour ,$min, $sec;
1339
1340 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1341 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1342 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1343 $date{'hour_local'} = $hour;
1344 $date{'minute_local'} = $min;
1345 $date{'tz_local'} = $tz;
1346 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1347 1900+$year, $mon+1, $mday,
1348 $hour, $min, $sec, $tz);
1349 return %date;
1350}
1351
1352sub parse_tag {
1353 my $tag_id = shift;
1354 my %tag;
1355 my @comment;
1356
1357 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1358 $tag{'id'} = $tag_id;
1359 while (my $line = <$fd>) {
1360 chomp $line;
1361 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1362 $tag{'object'} = $1;
1363 } elsif ($line =~ m/^type (.+)$/) {
1364 $tag{'type'} = $1;
1365 } elsif ($line =~ m/^tag (.+)$/) {
1366 $tag{'name'} = $1;
1367 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1368 $tag{'author'} = $1;
1369 $tag{'epoch'} = $2;
1370 $tag{'tz'} = $3;
1371 } elsif ($line =~ m/--BEGIN/) {
1372 push @comment, $line;
1373 last;
1374 } elsif ($line eq "") {
1375 last;
1376 }
1377 }
1378 push @comment, <$fd>;
1379 $tag{'comment'} = \@comment;
1380 close $fd or return;
1381 if (!defined $tag{'name'}) {
1382 return
1383 };
1384 return %tag
1385}
1386
1387sub parse_commit_text {
1388 my ($commit_text, $withparents) = @_;
1389 my @commit_lines = split '\n', $commit_text;
1390 my %co;
1391
1392 pop @commit_lines; # Remove '\0'
1393
1394 if (! @commit_lines) {
1395 return;
1396 }
1397
1398 my $header = shift @commit_lines;
1399 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1400 return;
1401 }
1402 ($co{'id'}, my @parents) = split ' ', $header;
1403 while (my $line = shift @commit_lines) {
1404 last if $line eq "\n";
1405 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1406 $co{'tree'} = $1;
1407 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1408 push @parents, $1;
1409 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1410 $co{'author'} = $1;
1411 $co{'author_epoch'} = $2;
1412 $co{'author_tz'} = $3;
1413 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1414 $co{'author_name'} = $1;
1415 $co{'author_email'} = $2;
1416 } else {
1417 $co{'author_name'} = $co{'author'};
1418 }
1419 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1420 $co{'committer'} = $1;
1421 $co{'committer_epoch'} = $2;
1422 $co{'committer_tz'} = $3;
1423 $co{'committer_name'} = $co{'committer'};
1424 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1425 $co{'committer_name'} = $1;
1426 $co{'committer_email'} = $2;
1427 } else {
1428 $co{'committer_name'} = $co{'committer'};
1429 }
1430 }
1431 }
1432 if (!defined $co{'tree'}) {
1433 return;
1434 };
1435 $co{'parents'} = \@parents;
1436 $co{'parent'} = $parents[0];
1437
1438 foreach my $title (@commit_lines) {
1439 $title =~ s/^ //;
1440 if ($title ne "") {
1441 $co{'title'} = chop_str($title, 80, 5);
1442 # remove leading stuff of merges to make the interesting part visible
1443 if (length($title) > 50) {
1444 $title =~ s/^Automatic //;
1445 $title =~ s/^merge (of|with) /Merge ... /i;
1446 if (length($title) > 50) {
1447 $title =~ s/(http|rsync):\/\///;
1448 }
1449 if (length($title) > 50) {
1450 $title =~ s/(master|www|rsync)\.//;
1451 }
1452 if (length($title) > 50) {
1453 $title =~ s/kernel.org:?//;
1454 }
1455 if (length($title) > 50) {
1456 $title =~ s/\/pub\/scm//;
1457 }
1458 }
1459 $co{'title_short'} = chop_str($title, 50, 5);
1460 last;
1461 }
1462 }
1463 if ($co{'title'} eq "") {
1464 $co{'title'} = $co{'title_short'} = '(no commit message)';
1465 }
1466 # remove added spaces
1467 foreach my $line (@commit_lines) {
1468 $line =~ s/^ //;
1469 }
1470 $co{'comment'} = \@commit_lines;
1471
1472 my $age = time - $co{'committer_epoch'};
1473 $co{'age'} = $age;
1474 $co{'age_string'} = age_string($age);
1475 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1476 if ($age > 60*60*24*7*2) {
1477 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1478 $co{'age_string_age'} = $co{'age_string'};
1479 } else {
1480 $co{'age_string_date'} = $co{'age_string'};
1481 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1482 }
1483 return %co;
1484}
1485
1486sub parse_commit {
1487 my ($commit_id) = @_;
1488 my %co;
1489
1490 local $/ = "\0";
1491
1492 open my $fd, "-|", git_cmd(), "rev-list",
1493 "--parents",
1494 "--header",
1495 "--max-count=1",
1496 $commit_id,
1497 "--",
1498 or die_error(undef, "Open git-rev-list failed");
1499 %co = parse_commit_text(<$fd>, 1);
1500 close $fd;
1501
1502 return %co;
1503}
1504
1505sub parse_commits {
1506 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1507 my @cos;
1508
1509 $maxcount ||= 1;
1510 $skip ||= 0;
1511
1512 local $/ = "\0";
1513
1514 open my $fd, "-|", git_cmd(), "rev-list",
1515 "--header",
1516 ($arg ? ($arg) : ()),
1517 ("--max-count=" . $maxcount),
1518 ("--skip=" . $skip),
1519 $commit_id,
1520 "--",
1521 ($filename ? ($filename) : ())
1522 or die_error(undef, "Open git-rev-list failed");
1523 while (my $line = <$fd>) {
1524 my %co = parse_commit_text($line);
1525 push @cos, \%co;
1526 }
1527 close $fd;
1528
1529 return wantarray ? @cos : \@cos;
1530}
1531
1532# parse ref from ref_file, given by ref_id, with given type
1533sub parse_ref {
1534 my $ref_file = shift;
1535 my $ref_id = shift;
1536 my $type = shift || git_get_type($ref_id);
1537 my %ref_item;
1538
1539 $ref_item{'type'} = $type;
1540 $ref_item{'id'} = $ref_id;
1541 $ref_item{'epoch'} = 0;
1542 $ref_item{'age'} = "unknown";
1543 if ($type eq "tag") {
1544 my %tag = parse_tag($ref_id);
1545 $ref_item{'comment'} = $tag{'comment'};
1546 if ($tag{'type'} eq "commit") {
1547 my %co = parse_commit($tag{'object'});
1548 $ref_item{'epoch'} = $co{'committer_epoch'};
1549 $ref_item{'age'} = $co{'age_string'};
1550 } elsif (defined($tag{'epoch'})) {
1551 my $age = time - $tag{'epoch'};
1552 $ref_item{'epoch'} = $tag{'epoch'};
1553 $ref_item{'age'} = age_string($age);
1554 }
1555 $ref_item{'reftype'} = $tag{'type'};
1556 $ref_item{'name'} = $tag{'name'};
1557 $ref_item{'refid'} = $tag{'object'};
1558 } elsif ($type eq "commit"){
1559 my %co = parse_commit($ref_id);
1560 $ref_item{'reftype'} = "commit";
1561 $ref_item{'name'} = $ref_file;
1562 $ref_item{'title'} = $co{'title'};
1563 $ref_item{'refid'} = $ref_id;
1564 $ref_item{'epoch'} = $co{'committer_epoch'};
1565 $ref_item{'age'} = $co{'age_string'};
1566 } else {
1567 $ref_item{'reftype'} = $type;
1568 $ref_item{'name'} = $ref_file;
1569 $ref_item{'refid'} = $ref_id;
1570 }
1571
1572 return %ref_item;
1573}
1574
1575# parse line of git-diff-tree "raw" output
1576sub parse_difftree_raw_line {
1577 my $line = shift;
1578 my %res;
1579
1580 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1581 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1582 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1583 $res{'from_mode'} = $1;
1584 $res{'to_mode'} = $2;
1585 $res{'from_id'} = $3;
1586 $res{'to_id'} = $4;
1587 $res{'status'} = $5;
1588 $res{'similarity'} = $6;
1589 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1590 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1591 } else {
1592 $res{'file'} = unquote($7);
1593 }
1594 }
1595 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1596 # combined diff (for merge commit)
1597 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1598 $res{'nparents'} = length($1);
1599 $res{'from_mode'} = [ split(' ', $2) ];
1600 $res{'to_mode'} = pop @{$res{'from_mode'}};
1601 $res{'from_id'} = [ split(' ', $3) ];
1602 $res{'to_id'} = pop @{$res{'from_id'}};
1603 $res{'status'} = [ split('', $4) ];
1604 $res{'to_file'} = unquote($5);
1605 }
1606 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1607 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1608 $res{'commit'} = $1;
1609 }
1610
1611 return wantarray ? %res : \%res;
1612}
1613
1614# parse line of git-ls-tree output
1615sub parse_ls_tree_line ($;%) {
1616 my $line = shift;
1617 my %opts = @_;
1618 my %res;
1619
1620 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1621 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1622
1623 $res{'mode'} = $1;
1624 $res{'type'} = $2;
1625 $res{'hash'} = $3;
1626 if ($opts{'-z'}) {
1627 $res{'name'} = $4;
1628 } else {
1629 $res{'name'} = unquote($4);
1630 }
1631
1632 return wantarray ? %res : \%res;
1633}
1634
1635## ......................................................................
1636## parse to array of hashes functions
1637
1638sub git_get_heads_list {
1639 my $limit = shift;
1640 my @headslist;
1641
1642 open my $fd, '-|', git_cmd(), 'for-each-ref',
1643 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1644 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1645 'refs/heads'
1646 or return;
1647 while (my $line = <$fd>) {
1648 my %ref_item;
1649
1650 chomp $line;
1651 my ($refinfo, $committerinfo) = split(/\0/, $line);
1652 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1653 my ($committer, $epoch, $tz) =
1654 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1655 $name =~ s!^refs/heads/!!;
1656
1657 $ref_item{'name'} = $name;
1658 $ref_item{'id'} = $hash;
1659 $ref_item{'title'} = $title || '(no commit message)';
1660 $ref_item{'epoch'} = $epoch;
1661 if ($epoch) {
1662 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1663 } else {
1664 $ref_item{'age'} = "unknown";
1665 }
1666
1667 push @headslist, \%ref_item;
1668 }
1669 close $fd;
1670
1671 return wantarray ? @headslist : \@headslist;
1672}
1673
1674sub git_get_tags_list {
1675 my $limit = shift;
1676 my @tagslist;
1677
1678 open my $fd, '-|', git_cmd(), 'for-each-ref',
1679 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1680 '--format=%(objectname) %(objecttype) %(refname) '.
1681 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1682 'refs/tags'
1683 or return;
1684 while (my $line = <$fd>) {
1685 my %ref_item;
1686
1687 chomp $line;
1688 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1689 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1690 my ($creator, $epoch, $tz) =
1691 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1692 $name =~ s!^refs/tags/!!;
1693
1694 $ref_item{'type'} = $type;
1695 $ref_item{'id'} = $id;
1696 $ref_item{'name'} = $name;
1697 if ($type eq "tag") {
1698 $ref_item{'subject'} = $title;
1699 $ref_item{'reftype'} = $reftype;
1700 $ref_item{'refid'} = $refid;
1701 } else {
1702 $ref_item{'reftype'} = $type;
1703 $ref_item{'refid'} = $id;
1704 }
1705
1706 if ($type eq "tag" || $type eq "commit") {
1707 $ref_item{'epoch'} = $epoch;
1708 if ($epoch) {
1709 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1710 } else {
1711 $ref_item{'age'} = "unknown";
1712 }
1713 }
1714
1715 push @tagslist, \%ref_item;
1716 }
1717 close $fd;
1718
1719 return wantarray ? @tagslist : \@tagslist;
1720}
1721
1722## ----------------------------------------------------------------------
1723## filesystem-related functions
1724
1725sub get_file_owner {
1726 my $path = shift;
1727
1728 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1729 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1730 if (!defined $gcos) {
1731 return undef;
1732 }
1733 my $owner = $gcos;
1734 $owner =~ s/[,;].*$//;
1735 return decode_utf8($owner);
1736}
1737
1738## ......................................................................
1739## mimetype related functions
1740
1741sub mimetype_guess_file {
1742 my $filename = shift;
1743 my $mimemap = shift;
1744 -r $mimemap or return undef;
1745
1746 my %mimemap;
1747 open(MIME, $mimemap) or return undef;
1748 while (<MIME>) {
1749 next if m/^#/; # skip comments
1750 my ($mime, $exts) = split(/\t+/);
1751 if (defined $exts) {
1752 my @exts = split(/\s+/, $exts);
1753 foreach my $ext (@exts) {
1754 $mimemap{$ext} = $mime;
1755 }
1756 }
1757 }
1758 close(MIME);
1759
1760 $filename =~ /\.([^.]*)$/;
1761 return $mimemap{$1};
1762}
1763
1764sub mimetype_guess {
1765 my $filename = shift;
1766 my $mime;
1767 $filename =~ /\./ or return undef;
1768
1769 if ($mimetypes_file) {
1770 my $file = $mimetypes_file;
1771 if ($file !~ m!^/!) { # if it is relative path
1772 # it is relative to project
1773 $file = "$projectroot/$project/$file";
1774 }
1775 $mime = mimetype_guess_file($filename, $file);
1776 }
1777 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1778 return $mime;
1779}
1780
1781sub blob_mimetype {
1782 my $fd = shift;
1783 my $filename = shift;
1784
1785 if ($filename) {
1786 my $mime = mimetype_guess($filename);
1787 $mime and return $mime;
1788 }
1789
1790 # just in case
1791 return $default_blob_plain_mimetype unless $fd;
1792
1793 if (-T $fd) {
1794 return 'text/plain' .
1795 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1796 } elsif (! $filename) {
1797 return 'application/octet-stream';
1798 } elsif ($filename =~ m/\.png$/i) {
1799 return 'image/png';
1800 } elsif ($filename =~ m/\.gif$/i) {
1801 return 'image/gif';
1802 } elsif ($filename =~ m/\.jpe?g$/i) {
1803 return 'image/jpeg';
1804 } else {
1805 return 'application/octet-stream';
1806 }
1807}
1808
1809## ======================================================================
1810## functions printing HTML: header, footer, error page
1811
1812sub git_header_html {
1813 my $status = shift || "200 OK";
1814 my $expires = shift;
1815
1816 my $title = "$site_name";
1817 if (defined $project) {
1818 $title .= " - " . decode_utf8($project);
1819 if (defined $action) {
1820 $title .= "/$action";
1821 if (defined $file_name) {
1822 $title .= " - " . esc_path($file_name);
1823 if ($action eq "tree" && $file_name !~ m|/$|) {
1824 $title .= "/";
1825 }
1826 }
1827 }
1828 }
1829 my $content_type;
1830 # require explicit support from the UA if we are to send the page as
1831 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1832 # we have to do this because MSIE sometimes globs '*/*', pretending to
1833 # support xhtml+xml but choking when it gets what it asked for.
1834 if (defined $cgi->http('HTTP_ACCEPT') &&
1835 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1836 $cgi->Accept('application/xhtml+xml') != 0) {
1837 $content_type = 'application/xhtml+xml';
1838 } else {
1839 $content_type = 'text/html';
1840 }
1841 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1842 -status=> $status, -expires => $expires);
1843 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
1844 print <<EOF;
1845<?xml version="1.0" encoding="utf-8"?>
1846<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1847<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1848<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1849<!-- git core binaries version $git_version -->
1850<head>
1851<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1852<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
1853<meta name="robots" content="index, nofollow"/>
1854<title>$title</title>
1855EOF
1856# print out each stylesheet that exist
1857 if (defined $stylesheet) {
1858#provides backwards capability for those people who define style sheet in a config file
1859 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1860 } else {
1861 foreach my $stylesheet (@stylesheets) {
1862 next unless $stylesheet;
1863 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1864 }
1865 }
1866 if (defined $project) {
1867 printf('<link rel="alternate" title="%s log RSS feed" '.
1868 'href="%s" type="application/rss+xml" />'."\n",
1869 esc_param($project), href(action=>"rss"));
1870 printf('<link rel="alternate" title="%s log Atom feed" '.
1871 'href="%s" type="application/atom+xml" />'."\n",
1872 esc_param($project), href(action=>"atom"));
1873 } else {
1874 printf('<link rel="alternate" title="%s projects list" '.
1875 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1876 $site_name, href(project=>undef, action=>"project_index"));
1877 printf('<link rel="alternate" title="%s projects feeds" '.
1878 'href="%s" type="text/x-opml"/>'."\n",
1879 $site_name, href(project=>undef, action=>"opml"));
1880 }
1881 if (defined $favicon) {
1882 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1883 }
1884
1885 print "</head>\n" .
1886 "<body>\n";
1887
1888 if (-f $site_header) {
1889 open (my $fd, $site_header);
1890 print <$fd>;
1891 close $fd;
1892 }
1893
1894 print "<div class=\"page_header\">\n" .
1895 $cgi->a({-href => esc_url($logo_url),
1896 -title => $logo_label},
1897 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1898 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1899 if (defined $project) {
1900 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1901 if (defined $action) {
1902 print " / $action";
1903 }
1904 print "\n";
1905 }
1906 print "</div>\n";
1907
1908 my ($have_search) = gitweb_check_feature('search');
1909 if ((defined $project) && ($have_search)) {
1910 if (!defined $searchtext) {
1911 $searchtext = "";
1912 }
1913 my $search_hash;
1914 if (defined $hash_base) {
1915 $search_hash = $hash_base;
1916 } elsif (defined $hash) {
1917 $search_hash = $hash;
1918 } else {
1919 $search_hash = "HEAD";
1920 }
1921 $cgi->param("a", "search");
1922 $cgi->param("h", $search_hash);
1923 $cgi->param("p", $project);
1924 print $cgi->startform(-method => "get", -action => $my_uri) .
1925 "<div class=\"search\">\n" .
1926 $cgi->hidden(-name => "p") . "\n" .
1927 $cgi->hidden(-name => "a") . "\n" .
1928 $cgi->hidden(-name => "h") . "\n" .
1929 $cgi->popup_menu(-name => 'st', -default => 'commit',
1930 -values => ['commit', 'author', 'committer', 'pickaxe']) .
1931 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1932 " search:\n",
1933 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1934 "</div>" .
1935 $cgi->end_form() . "\n";
1936 }
1937}
1938
1939sub git_footer_html {
1940 print "<div class=\"page_footer\">\n";
1941 if (defined $project) {
1942 my $descr = git_get_project_description($project);
1943 if (defined $descr) {
1944 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1945 }
1946 print $cgi->a({-href => href(action=>"rss"),
1947 -class => "rss_logo"}, "RSS") . " ";
1948 print $cgi->a({-href => href(action=>"atom"),
1949 -class => "rss_logo"}, "Atom") . "\n";
1950 } else {
1951 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1952 -class => "rss_logo"}, "OPML") . " ";
1953 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1954 -class => "rss_logo"}, "TXT") . "\n";
1955 }
1956 print "</div>\n" ;
1957
1958 if (-f $site_footer) {
1959 open (my $fd, $site_footer);
1960 print <$fd>;
1961 close $fd;
1962 }
1963
1964 print "</body>\n" .
1965 "</html>";
1966}
1967
1968sub die_error {
1969 my $status = shift || "403 Forbidden";
1970 my $error = shift || "Malformed query, file missing or permission denied";
1971
1972 git_header_html($status);
1973 print <<EOF;
1974<div class="page_body">
1975<br /><br />
1976$status - $error
1977<br />
1978</div>
1979EOF
1980 git_footer_html();
1981 exit;
1982}
1983
1984## ----------------------------------------------------------------------
1985## functions printing or outputting HTML: navigation
1986
1987sub git_print_page_nav {
1988 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1989 $extra = '' if !defined $extra; # pager or formats
1990
1991 my @navs = qw(summary shortlog log commit commitdiff tree);
1992 if ($suppress) {
1993 @navs = grep { $_ ne $suppress } @navs;
1994 }
1995
1996 my %arg = map { $_ => {action=>$_} } @navs;
1997 if (defined $head) {
1998 for (qw(commit commitdiff)) {
1999 $arg{$_}{'hash'} = $head;
2000 }
2001 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2002 for (qw(shortlog log)) {
2003 $arg{$_}{'hash'} = $head;
2004 }
2005 }
2006 }
2007 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2008 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2009
2010 print "<div class=\"page_nav\">\n" .
2011 (join " | ",
2012 map { $_ eq $current ?
2013 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2014 } @navs);
2015 print "<br/>\n$extra<br/>\n" .
2016 "</div>\n";
2017}
2018
2019sub format_paging_nav {
2020 my ($action, $hash, $head, $page, $nrevs) = @_;
2021 my $paging_nav;
2022
2023
2024 if ($hash ne $head || $page) {
2025 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2026 } else {
2027 $paging_nav .= "HEAD";
2028 }
2029
2030 if ($page > 0) {
2031 $paging_nav .= " ⋅ " .
2032 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2033 -accesskey => "p", -title => "Alt-p"}, "prev");
2034 } else {
2035 $paging_nav .= " ⋅ prev";
2036 }
2037
2038 if ($nrevs >= (100 * ($page+1)-1)) {
2039 $paging_nav .= " ⋅ " .
2040 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2041 -accesskey => "n", -title => "Alt-n"}, "next");
2042 } else {
2043 $paging_nav .= " ⋅ next";
2044 }
2045
2046 return $paging_nav;
2047}
2048
2049## ......................................................................
2050## functions printing or outputting HTML: div
2051
2052sub git_print_header_div {
2053 my ($action, $title, $hash, $hash_base) = @_;
2054 my %args = ();
2055
2056 $args{'action'} = $action;
2057 $args{'hash'} = $hash if $hash;
2058 $args{'hash_base'} = $hash_base if $hash_base;
2059
2060 print "<div class=\"header\">\n" .
2061 $cgi->a({-href => href(%args), -class => "title"},
2062 $title ? $title : $action) .
2063 "\n</div>\n";
2064}
2065
2066#sub git_print_authorship (\%) {
2067sub git_print_authorship {
2068 my $co = shift;
2069
2070 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2071 print "<div class=\"author_date\">" .
2072 esc_html($co->{'author_name'}) .
2073 " [$ad{'rfc2822'}";
2074 if ($ad{'hour_local'} < 6) {
2075 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2076 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2077 } else {
2078 printf(" (%02d:%02d %s)",
2079 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2080 }
2081 print "]</div>\n";
2082}
2083
2084sub git_print_page_path {
2085 my $name = shift;
2086 my $type = shift;
2087 my $hb = shift;
2088
2089
2090 print "<div class=\"page_path\">";
2091 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2092 -title => 'tree root'}, decode_utf8("[$project]"));
2093 print " / ";
2094 if (defined $name) {
2095 my @dirname = split '/', $name;
2096 my $basename = pop @dirname;
2097 my $fullname = '';
2098
2099 foreach my $dir (@dirname) {
2100 $fullname .= ($fullname ? '/' : '') . $dir;
2101 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2102 hash_base=>$hb),
2103 -title => $fullname}, esc_path($dir));
2104 print " / ";
2105 }
2106 if (defined $type && $type eq 'blob') {
2107 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2108 hash_base=>$hb),
2109 -title => $name}, esc_path($basename));
2110 } elsif (defined $type && $type eq 'tree') {
2111 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2112 hash_base=>$hb),
2113 -title => $name}, esc_path($basename));
2114 print " / ";
2115 } else {
2116 print esc_path($basename);
2117 }
2118 }
2119 print "<br/></div>\n";
2120}
2121
2122# sub git_print_log (\@;%) {
2123sub git_print_log ($;%) {
2124 my $log = shift;
2125 my %opts = @_;
2126
2127 if ($opts{'-remove_title'}) {
2128 # remove title, i.e. first line of log
2129 shift @$log;
2130 }
2131 # remove leading empty lines
2132 while (defined $log->[0] && $log->[0] eq "") {
2133 shift @$log;
2134 }
2135
2136 # print log
2137 my $signoff = 0;
2138 my $empty = 0;
2139 foreach my $line (@$log) {
2140 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2141 $signoff = 1;
2142 $empty = 0;
2143 if (! $opts{'-remove_signoff'}) {
2144 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2145 next;
2146 } else {
2147 # remove signoff lines
2148 next;
2149 }
2150 } else {
2151 $signoff = 0;
2152 }
2153
2154 # print only one empty line
2155 # do not print empty line after signoff
2156 if ($line eq "") {
2157 next if ($empty || $signoff);
2158 $empty = 1;
2159 } else {
2160 $empty = 0;
2161 }
2162
2163 print format_log_line_html($line) . "<br/>\n";
2164 }
2165
2166 if ($opts{'-final_empty_line'}) {
2167 # end with single empty line
2168 print "<br/>\n" unless $empty;
2169 }
2170}
2171
2172# return link target (what link points to)
2173sub git_get_link_target {
2174 my $hash = shift;
2175 my $link_target;
2176
2177 # read link
2178 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2179 or return;
2180 {
2181 local $/;
2182 $link_target = <$fd>;
2183 }
2184 close $fd
2185 or return;
2186
2187 return $link_target;
2188}
2189
2190# given link target, and the directory (basedir) the link is in,
2191# return target of link relative to top directory (top tree);
2192# return undef if it is not possible (including absolute links).
2193sub normalize_link_target {
2194 my ($link_target, $basedir, $hash_base) = @_;
2195
2196 # we can normalize symlink target only if $hash_base is provided
2197 return unless $hash_base;
2198
2199 # absolute symlinks (beginning with '/') cannot be normalized
2200 return if (substr($link_target, 0, 1) eq '/');
2201
2202 # normalize link target to path from top (root) tree (dir)
2203 my $path;
2204 if ($basedir) {
2205 $path = $basedir . '/' . $link_target;
2206 } else {
2207 # we are in top (root) tree (dir)
2208 $path = $link_target;
2209 }
2210
2211 # remove //, /./, and /../
2212 my @path_parts;
2213 foreach my $part (split('/', $path)) {
2214 # discard '.' and ''
2215 next if (!$part || $part eq '.');
2216 # handle '..'
2217 if ($part eq '..') {
2218 if (@path_parts) {
2219 pop @path_parts;
2220 } else {
2221 # link leads outside repository (outside top dir)
2222 return;
2223 }
2224 } else {
2225 push @path_parts, $part;
2226 }
2227 }
2228 $path = join('/', @path_parts);
2229
2230 return $path;
2231}
2232
2233# print tree entry (row of git_tree), but without encompassing <tr> element
2234sub git_print_tree_entry {
2235 my ($t, $basedir, $hash_base, $have_blame) = @_;
2236
2237 my %base_key = ();
2238 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2239
2240 # The format of a table row is: mode list link. Where mode is
2241 # the mode of the entry, list is the name of the entry, an href,
2242 # and link is the action links of the entry.
2243
2244 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2245 if ($t->{'type'} eq "blob") {
2246 print "<td class=\"list\">" .
2247 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2248 file_name=>"$basedir$t->{'name'}", %base_key),
2249 -class => "list"}, esc_path($t->{'name'}));
2250 if (S_ISLNK(oct $t->{'mode'})) {
2251 my $link_target = git_get_link_target($t->{'hash'});
2252 if ($link_target) {
2253 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2254 if (defined $norm_target) {
2255 print " -> " .
2256 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2257 file_name=>$norm_target),
2258 -title => $norm_target}, esc_path($link_target));
2259 } else {
2260 print " -> " . esc_path($link_target);
2261 }
2262 }
2263 }
2264 print "</td>\n";
2265 print "<td class=\"link\">";
2266 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2267 file_name=>"$basedir$t->{'name'}", %base_key)},
2268 "blob");
2269 if ($have_blame) {
2270 print " | " .
2271 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2272 file_name=>"$basedir$t->{'name'}", %base_key)},
2273 "blame");
2274 }
2275 if (defined $hash_base) {
2276 print " | " .
2277 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2278 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2279 "history");
2280 }
2281 print " | " .
2282 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2283 file_name=>"$basedir$t->{'name'}")},
2284 "raw");
2285 print "</td>\n";
2286
2287 } elsif ($t->{'type'} eq "tree") {
2288 print "<td class=\"list\">";
2289 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2290 file_name=>"$basedir$t->{'name'}", %base_key)},
2291 esc_path($t->{'name'}));
2292 print "</td>\n";
2293 print "<td class=\"link\">";
2294 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2295 file_name=>"$basedir$t->{'name'}", %base_key)},
2296 "tree");
2297 if (defined $hash_base) {
2298 print " | " .
2299 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2300 file_name=>"$basedir$t->{'name'}")},
2301 "history");
2302 }
2303 print "</td>\n";
2304 }
2305}
2306
2307## ......................................................................
2308## functions printing large fragments of HTML
2309
2310sub fill_from_file_info {
2311 my ($diff, @parents) = @_;
2312
2313 $diff->{'from_file'} = [ ];
2314 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2315 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2316 if ($diff->{'status'}[$i] eq 'R' ||
2317 $diff->{'status'}[$i] eq 'C') {
2318 $diff->{'from_file'}[$i] =
2319 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2320 }
2321 }
2322
2323 return $diff;
2324}
2325
2326# parameters can be strings, or references to arrays of strings
2327sub from_ids_eq {
2328 my ($a, $b) = @_;
2329
2330 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2331 for (my $i = 0; $i < @$a; ++$i) {
2332 return 0 unless ($a->[$i] eq $b->[$i]);
2333 }
2334 return 1;
2335 } elsif (!ref($a) && !ref($b)) {
2336 return $a eq $b;
2337 } else {
2338 return 0;
2339 }
2340}
2341
2342
2343sub git_difftree_body {
2344 my ($difftree, $hash, @parents) = @_;
2345 my ($parent) = $parents[0];
2346 my ($have_blame) = gitweb_check_feature('blame');
2347 print "<div class=\"list_head\">\n";
2348 if ($#{$difftree} > 10) {
2349 print(($#{$difftree} + 1) . " files changed:\n");
2350 }
2351 print "</div>\n";
2352
2353 print "<table class=\"" .
2354 (@parents > 1 ? "combined " : "") .
2355 "diff_tree\">\n";
2356 my $alternate = 1;
2357 my $patchno = 0;
2358 foreach my $line (@{$difftree}) {
2359 my $diff;
2360 if (ref($line) eq "HASH") {
2361 # pre-parsed (or generated by hand)
2362 $diff = $line;
2363 } else {
2364 $diff = parse_difftree_raw_line($line);
2365 }
2366
2367 if ($alternate) {
2368 print "<tr class=\"dark\">\n";
2369 } else {
2370 print "<tr class=\"light\">\n";
2371 }
2372 $alternate ^= 1;
2373
2374 if (exists $diff->{'nparents'}) { # combined diff
2375
2376 fill_from_file_info($diff, @parents)
2377 unless exists $diff->{'from_file'};
2378
2379 if ($diff->{'to_id'} ne ('0' x 40)) {
2380 # file exists in the result (child) commit
2381 print "<td>" .
2382 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2383 file_name=>$diff->{'to_file'},
2384 hash_base=>$hash),
2385 -class => "list"}, esc_path($diff->{'to_file'})) .
2386 "</td>\n";
2387 } else {
2388 print "<td>" .
2389 esc_path($diff->{'to_file'}) .
2390 "</td>\n";
2391 }
2392
2393 if ($action eq 'commitdiff') {
2394 # link to patch
2395 $patchno++;
2396 print "<td class=\"link\">" .
2397 $cgi->a({-href => "#patch$patchno"}, "patch") .
2398 " | " .
2399 "</td>\n";
2400 }
2401
2402 my $has_history = 0;
2403 my $not_deleted = 0;
2404 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2405 my $hash_parent = $parents[$i];
2406 my $from_hash = $diff->{'from_id'}[$i];
2407 my $from_path = $diff->{'from_file'}[$i];
2408 my $status = $diff->{'status'}[$i];
2409
2410 $has_history ||= ($status ne 'A');
2411 $not_deleted ||= ($status ne 'D');
2412
2413 if ($status eq 'A') {
2414 print "<td class=\"link\" align=\"right\"> | </td>\n";
2415 } elsif ($status eq 'D') {
2416 print "<td class=\"link\">" .
2417 $cgi->a({-href => href(action=>"blob",
2418 hash_base=>$hash,
2419 hash=>$from_hash,
2420 file_name=>$from_path)},
2421 "blob" . ($i+1)) .
2422 " | </td>\n";
2423 } else {
2424 if ($diff->{'to_id'} eq $from_hash) {
2425 print "<td class=\"link nochange\">";
2426 } else {
2427 print "<td class=\"link\">";
2428 }
2429 print $cgi->a({-href => href(action=>"blobdiff",
2430 hash=>$diff->{'to_id'},
2431 hash_parent=>$from_hash,
2432 hash_base=>$hash,
2433 hash_parent_base=>$hash_parent,
2434 file_name=>$diff->{'to_file'},
2435 file_parent=>$from_path)},
2436 "diff" . ($i+1)) .
2437 " | </td>\n";
2438 }
2439 }
2440
2441 print "<td class=\"link\">";
2442 if ($not_deleted) {
2443 print $cgi->a({-href => href(action=>"blob",
2444 hash=>$diff->{'to_id'},
2445 file_name=>$diff->{'to_file'},
2446 hash_base=>$hash)},
2447 "blob");
2448 print " | " if ($has_history);
2449 }
2450 if ($has_history) {
2451 print $cgi->a({-href => href(action=>"history",
2452 file_name=>$diff->{'to_file'},
2453 hash_base=>$hash)},
2454 "history");
2455 }
2456 print "</td>\n";
2457
2458 print "</tr>\n";
2459 next; # instead of 'else' clause, to avoid extra indent
2460 }
2461 # else ordinary diff
2462
2463 my ($to_mode_oct, $to_mode_str, $to_file_type);
2464 my ($from_mode_oct, $from_mode_str, $from_file_type);
2465 if ($diff->{'to_mode'} ne ('0' x 6)) {
2466 $to_mode_oct = oct $diff->{'to_mode'};
2467 if (S_ISREG($to_mode_oct)) { # only for regular file
2468 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2469 }
2470 $to_file_type = file_type($diff->{'to_mode'});
2471 }
2472 if ($diff->{'from_mode'} ne ('0' x 6)) {
2473 $from_mode_oct = oct $diff->{'from_mode'};
2474 if (S_ISREG($to_mode_oct)) { # only for regular file
2475 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2476 }
2477 $from_file_type = file_type($diff->{'from_mode'});
2478 }
2479
2480 if ($diff->{'status'} eq "A") { # created
2481 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2482 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2483 $mode_chng .= "]</span>";
2484 print "<td>";
2485 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2486 hash_base=>$hash, file_name=>$diff->{'file'}),
2487 -class => "list"}, esc_path($diff->{'file'}));
2488 print "</td>\n";
2489 print "<td>$mode_chng</td>\n";
2490 print "<td class=\"link\">";
2491 if ($action eq 'commitdiff') {
2492 # link to patch
2493 $patchno++;
2494 print $cgi->a({-href => "#patch$patchno"}, "patch");
2495 print " | ";
2496 }
2497 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2498 hash_base=>$hash, file_name=>$diff->{'file'})},
2499 "blob");
2500 print "</td>\n";
2501
2502 } elsif ($diff->{'status'} eq "D") { # deleted
2503 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2504 print "<td>";
2505 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2506 hash_base=>$parent, file_name=>$diff->{'file'}),
2507 -class => "list"}, esc_path($diff->{'file'}));
2508 print "</td>\n";
2509 print "<td>$mode_chng</td>\n";
2510 print "<td class=\"link\">";
2511 if ($action eq 'commitdiff') {
2512 # link to patch
2513 $patchno++;
2514 print $cgi->a({-href => "#patch$patchno"}, "patch");
2515 print " | ";
2516 }
2517 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2518 hash_base=>$parent, file_name=>$diff->{'file'})},
2519 "blob") . " | ";
2520 if ($have_blame) {
2521 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2522 file_name=>$diff->{'file'})},
2523 "blame") . " | ";
2524 }
2525 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2526 file_name=>$diff->{'file'})},
2527 "history");
2528 print "</td>\n";
2529
2530 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
2531 my $mode_chnge = "";
2532 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2533 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2534 if ($from_file_type ne $to_file_type) {
2535 $mode_chnge .= " from $from_file_type to $to_file_type";
2536 }
2537 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2538 if ($from_mode_str && $to_mode_str) {
2539 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2540 } elsif ($to_mode_str) {
2541 $mode_chnge .= " mode: $to_mode_str";
2542 }
2543 }
2544 $mode_chnge .= "]</span>\n";
2545 }
2546 print "<td>";
2547 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2548 hash_base=>$hash, file_name=>$diff->{'file'}),
2549 -class => "list"}, esc_path($diff->{'file'}));
2550 print "</td>\n";
2551 print "<td>$mode_chnge</td>\n";
2552 print "<td class=\"link\">";
2553 if ($action eq 'commitdiff') {
2554 # link to patch
2555 $patchno++;
2556 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2557 " | ";
2558 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2559 # "commit" view and modified file (not onlu mode changed)
2560 print $cgi->a({-href => href(action=>"blobdiff",
2561 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2562 hash_base=>$hash, hash_parent_base=>$parent,
2563 file_name=>$diff->{'file'})},
2564 "diff") .
2565 " | ";
2566 }
2567 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2568 hash_base=>$hash, file_name=>$diff->{'file'})},
2569 "blob") . " | ";
2570 if ($have_blame) {
2571 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2572 file_name=>$diff->{'file'})},
2573 "blame") . " | ";
2574 }
2575 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2576 file_name=>$diff->{'file'})},
2577 "history");
2578 print "</td>\n";
2579
2580 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
2581 my %status_name = ('R' => 'moved', 'C' => 'copied');
2582 my $nstatus = $status_name{$diff->{'status'}};
2583 my $mode_chng = "";
2584 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2585 # mode also for directories, so we cannot use $to_mode_str
2586 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2587 }
2588 print "<td>" .
2589 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2590 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
2591 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
2592 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2593 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2594 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
2595 -class => "list"}, esc_path($diff->{'from_file'})) .
2596 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2597 "<td class=\"link\">";
2598 if ($action eq 'commitdiff') {
2599 # link to patch
2600 $patchno++;
2601 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2602 " | ";
2603 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2604 # "commit" view and modified file (not only pure rename or copy)
2605 print $cgi->a({-href => href(action=>"blobdiff",
2606 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2607 hash_base=>$hash, hash_parent_base=>$parent,
2608 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
2609 "diff") .
2610 " | ";
2611 }
2612 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2613 hash_base=>$parent, file_name=>$diff->{'to_file'})},
2614 "blob") . " | ";
2615 if ($have_blame) {
2616 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2617 file_name=>$diff->{'to_file'})},
2618 "blame") . " | ";
2619 }
2620 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2621 file_name=>$diff->{'to_file'})},
2622 "history");
2623 print "</td>\n";
2624
2625 } # we should not encounter Unmerged (U) or Unknown (X) status
2626 print "</tr>\n";
2627 }
2628 print "</table>\n";
2629}
2630
2631sub git_patchset_body {
2632 my ($fd, $difftree, $hash, @hash_parents) = @_;
2633 my ($hash_parent) = $hash_parents[0];
2634
2635 my $patch_idx = 0;
2636 my $patch_number = 0;
2637 my $patch_line;
2638 my $diffinfo;
2639 my (%from, %to);
2640
2641 print "<div class=\"patchset\">\n";
2642
2643 # skip to first patch
2644 while ($patch_line = <$fd>) {
2645 chomp $patch_line;
2646
2647 last if ($patch_line =~ m/^diff /);
2648 }
2649
2650 PATCH:
2651 while ($patch_line) {
2652 my @diff_header;
2653 my ($from_id, $to_id);
2654
2655 # git diff header
2656 #assert($patch_line =~ m/^diff /) if DEBUG;
2657 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2658 $patch_number++;
2659 push @diff_header, $patch_line;
2660
2661 # extended diff header
2662 EXTENDED_HEADER:
2663 while ($patch_line = <$fd>) {
2664 chomp $patch_line;
2665
2666 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
2667
2668 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2669 $from_id = $1;
2670 $to_id = $2;
2671 } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2672 $from_id = [ split(',', $1) ];
2673 $to_id = $2;
2674 }
2675
2676 push @diff_header, $patch_line;
2677 }
2678 my $last_patch_line = $patch_line;
2679
2680 # check if current patch belong to current raw line
2681 # and parse raw git-diff line if needed
2682 if (defined $diffinfo &&
2683 defined $from_id && defined $to_id &&
2684 from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
2685 $diffinfo->{'to_id'} eq $to_id) {
2686 # this is continuation of a split patch
2687 print "<div class=\"patch cont\">\n";
2688 } else {
2689 # advance raw git-diff output if needed
2690 $patch_idx++ if defined $diffinfo;
2691
2692 # read and prepare patch information
2693 if (ref($difftree->[$patch_idx]) eq "HASH") {
2694 # pre-parsed (or generated by hand)
2695 $diffinfo = $difftree->[$patch_idx];
2696 } else {
2697 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2698 }
2699 if ($diffinfo->{'nparents'}) {
2700 # combined diff
2701 $from{'file'} = [];
2702 $from{'href'} = [];
2703 fill_from_file_info($diffinfo, @hash_parents)
2704 unless exists $diffinfo->{'from_file'};
2705 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2706 $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2707 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2708 $from{'href'}[$i] = href(action=>"blob",
2709 hash_base=>$hash_parents[$i],
2710 hash=>$diffinfo->{'from_id'}[$i],
2711 file_name=>$from{'file'}[$i]);
2712 } else {
2713 $from{'href'}[$i] = undef;
2714 }
2715 }
2716 } else {
2717 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2718 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2719 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2720 hash=>$diffinfo->{'from_id'},
2721 file_name=>$from{'file'});
2722 } else {
2723 delete $from{'href'};
2724 }
2725 }
2726
2727 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2728 if ($diffinfo->{'to_id'} ne ('0' x 40)) { # file exists in result
2729 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2730 hash=>$diffinfo->{'to_id'},
2731 file_name=>$to{'file'});
2732 } else {
2733 delete $to{'href'};
2734 }
2735 # this is first patch for raw difftree line with $patch_idx index
2736 # we index @$difftree array from 0, but number patches from 1
2737 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2738 }
2739
2740 # print "git diff" header
2741 $patch_line = shift @diff_header;
2742 if ($diffinfo->{'nparents'}) {
2743
2744 # combined diff
2745 $patch_line =~ s!^(diff (.*?) )"?.*$!$1!;
2746 if ($to{'href'}) {
2747 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2748 esc_path($to{'file'}));
2749 } else { # file was deleted
2750 $patch_line .= esc_path($to{'file'});
2751 }
2752
2753 } else {
2754
2755 $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2756 if ($from{'href'}) {
2757 $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2758 'a/' . esc_path($from{'file'}));
2759 } else { # file was added
2760 $patch_line .= 'a/' . esc_path($from{'file'});
2761 }
2762 $patch_line .= ' ';
2763 if ($to{'href'}) {
2764 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2765 'b/' . esc_path($to{'file'}));
2766 } else { # file was deleted
2767 $patch_line .= 'b/' . esc_path($to{'file'});
2768 }
2769
2770 }
2771 print "<div class=\"diff header\">$patch_line</div>\n";
2772
2773 # print extended diff header
2774 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
2775 EXTENDED_HEADER:
2776 foreach $patch_line (@diff_header) {
2777 # match <path>
2778 if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2779 $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2780 esc_path($from{'file'}));
2781 }
2782 if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2783 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"path"},
2784 esc_path($to{'file'}));
2785 }
2786 # match single <mode>
2787 if ($patch_line =~ m/\s(\d{6})$/) {
2788 $patch_line .= '<span class="info"> (' .
2789 file_type_long($1) .
2790 ')</span>';
2791 }
2792 # match <hash>
2793 if ($patch_line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2794 # can match only for combined diff
2795 $patch_line = 'index ';
2796 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2797 if ($from{'href'}[$i]) {
2798 $patch_line .= $cgi->a({-href=>$from{'href'}[$i],
2799 -class=>"hash"},
2800 substr($diffinfo->{'from_id'}[$i],0,7));
2801 } else {
2802 $patch_line .= '0' x 7;
2803 }
2804 # separator
2805 $patch_line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2806 }
2807 $patch_line .= '..';
2808 if ($to{'href'}) {
2809 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2810 substr($diffinfo->{'to_id'},0,7));
2811 } else {
2812 $patch_line .= '0' x 7;
2813 }
2814
2815 } elsif ($patch_line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2816 # can match only for ordinary diff
2817 my ($from_link, $to_link);
2818 if ($from{'href'}) {
2819 $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2820 substr($diffinfo->{'from_id'},0,7));
2821 } else {
2822 $from_link = '0' x 7;
2823 }
2824 if ($to{'href'}) {
2825 $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2826 substr($diffinfo->{'to_id'},0,7));
2827 } else {
2828 $to_link = '0' x 7;
2829 }
2830 #affirm {
2831 # my ($from_hash, $to_hash) =
2832 # ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/);
2833 # my ($from_id, $to_id) =
2834 # ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2835 # ($from_hash eq $from_id) && ($to_hash eq $to_id);
2836 #} if DEBUG;
2837 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2838 $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2839 }
2840 print $patch_line . "<br/>\n";
2841 }
2842 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
2843
2844 # from-file/to-file diff header
2845 $patch_line = $last_patch_line;
2846 if (! $patch_line) {
2847 print "</div>\n"; # class="patch"
2848 last PATCH;
2849 }
2850 next PATCH if ($patch_line =~ m/^diff /);
2851 #assert($patch_line =~ m/^---/) if DEBUG;
2852 if (!$diffinfo->{'nparents'} && # not from-file line for combined diff
2853 $from{'href'} && $patch_line =~ m!^--- "?a/!) {
2854 $patch_line = '--- a/' .
2855 $cgi->a({-href=>$from{'href'}, -class=>"path"},
2856 esc_path($from{'file'}));
2857 }
2858 print "<div class=\"diff from_file\">$patch_line</div>\n";
2859
2860 $patch_line = <$fd>;
2861 chomp $patch_line;
2862
2863 #assert($patch_line =~ m/^+++/) if DEBUG;
2864 if ($to{'href'} && $patch_line =~ m!^\+\+\+ "?b/!) {
2865 $patch_line = '+++ b/' .
2866 $cgi->a({-href=>$to{'href'}, -class=>"path"},
2867 esc_path($to{'file'}));
2868 }
2869 print "<div class=\"diff to_file\">$patch_line</div>\n";
2870
2871 # the patch itself
2872 LINE:
2873 while ($patch_line = <$fd>) {
2874 chomp $patch_line;
2875
2876 next PATCH if ($patch_line =~ m/^diff /);
2877
2878 print format_diff_line($patch_line, \%from, \%to);
2879 }
2880
2881 } continue {
2882 print "</div>\n"; # class="patch"
2883 }
2884
2885 if ($patch_number == 0) {
2886 if (@hash_parents > 1) {
2887 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
2888 } else {
2889 print "<div class=\"diff nodifferences\">No differences found</div>\n";
2890 }
2891 }
2892
2893 print "</div>\n"; # class="patchset"
2894}
2895
2896# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2897
2898sub git_project_list_body {
2899 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2900
2901 my ($check_forks) = gitweb_check_feature('forks');
2902
2903 my @projects;
2904 foreach my $pr (@$projlist) {
2905 my (@aa) = git_get_last_activity($pr->{'path'});
2906 unless (@aa) {
2907 next;
2908 }
2909 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2910 if (!defined $pr->{'descr'}) {
2911 my $descr = git_get_project_description($pr->{'path'}) || "";
2912 $pr->{'descr_long'} = decode_utf8($descr);
2913 $pr->{'descr'} = chop_str($descr, 25, 5);
2914 }
2915 if (!defined $pr->{'owner'}) {
2916 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2917 }
2918 if ($check_forks) {
2919 my $pname = $pr->{'path'};
2920 if (($pname =~ s/\.git$//) &&
2921 ($pname !~ /\/$/) &&
2922 (-d "$projectroot/$pname")) {
2923 $pr->{'forks'} = "-d $projectroot/$pname";
2924 }
2925 else {
2926 $pr->{'forks'} = 0;
2927 }
2928 }
2929 push @projects, $pr;
2930 }
2931
2932 $order ||= $default_projects_order;
2933 $from = 0 unless defined $from;
2934 $to = $#projects if (!defined $to || $#projects < $to);
2935
2936 print "<table class=\"project_list\">\n";
2937 unless ($no_header) {
2938 print "<tr>\n";
2939 if ($check_forks) {
2940 print "<th></th>\n";
2941 }
2942 if ($order eq "project") {
2943 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2944 print "<th>Project</th>\n";
2945 } else {
2946 print "<th>" .
2947 $cgi->a({-href => href(project=>undef, order=>'project'),
2948 -class => "header"}, "Project") .
2949 "</th>\n";
2950 }
2951 if ($order eq "descr") {
2952 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2953 print "<th>Description</th>\n";
2954 } else {
2955 print "<th>" .
2956 $cgi->a({-href => href(project=>undef, order=>'descr'),
2957 -class => "header"}, "Description") .
2958 "</th>\n";
2959 }
2960 if ($order eq "owner") {
2961 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2962 print "<th>Owner</th>\n";
2963 } else {
2964 print "<th>" .
2965 $cgi->a({-href => href(project=>undef, order=>'owner'),
2966 -class => "header"}, "Owner") .
2967 "</th>\n";
2968 }
2969 if ($order eq "age") {
2970 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2971 print "<th>Last Change</th>\n";
2972 } else {
2973 print "<th>" .
2974 $cgi->a({-href => href(project=>undef, order=>'age'),
2975 -class => "header"}, "Last Change") .
2976 "</th>\n";
2977 }
2978 print "<th></th>\n" .
2979 "</tr>\n";
2980 }
2981 my $alternate = 1;
2982 for (my $i = $from; $i <= $to; $i++) {
2983 my $pr = $projects[$i];
2984 if ($alternate) {
2985 print "<tr class=\"dark\">\n";
2986 } else {
2987 print "<tr class=\"light\">\n";
2988 }
2989 $alternate ^= 1;
2990 if ($check_forks) {
2991 print "<td>";
2992 if ($pr->{'forks'}) {
2993 print "<!-- $pr->{'forks'} -->\n";
2994 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2995 }
2996 print "</td>\n";
2997 }
2998 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2999 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3000 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3001 -class => "list", -title => $pr->{'descr_long'}},
3002 esc_html($pr->{'descr'})) . "</td>\n" .
3003 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
3004 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3005 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3006 "<td class=\"link\">" .
3007 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3008 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3009 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3010 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3011 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3012 "</td>\n" .
3013 "</tr>\n";
3014 }
3015 if (defined $extra) {
3016 print "<tr>\n";
3017 if ($check_forks) {
3018 print "<td></td>\n";
3019 }
3020 print "<td colspan=\"5\">$extra</td>\n" .
3021 "</tr>\n";
3022 }
3023 print "</table>\n";
3024}
3025
3026sub git_shortlog_body {
3027 # uses global variable $project
3028 my ($commitlist, $from, $to, $refs, $extra) = @_;
3029
3030 my $have_snapshot = gitweb_have_snapshot();
3031
3032 $from = 0 unless defined $from;
3033 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3034
3035 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3036 my $alternate = 1;
3037 for (my $i = $from; $i <= $to; $i++) {
3038 my %co = %{$commitlist->[$i]};
3039 my $commit = $co{'id'};
3040 my $ref = format_ref_marker($refs, $commit);
3041 if ($alternate) {
3042 print "<tr class=\"dark\">\n";
3043 } else {
3044 print "<tr class=\"light\">\n";
3045 }
3046 $alternate ^= 1;
3047 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3048 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3049 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3050 "<td>";
3051 print format_subject_html($co{'title'}, $co{'title_short'},
3052 href(action=>"commit", hash=>$commit), $ref);
3053 print "</td>\n" .
3054 "<td class=\"link\">" .
3055 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3056 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3057 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3058 if ($have_snapshot) {
3059 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
3060 }
3061 print "</td>\n" .
3062 "</tr>\n";
3063 }
3064 if (defined $extra) {
3065 print "<tr>\n" .
3066 "<td colspan=\"4\">$extra</td>\n" .
3067 "</tr>\n";
3068 }
3069 print "</table>\n";
3070}
3071
3072sub git_history_body {
3073 # Warning: assumes constant type (blob or tree) during history
3074 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3075
3076 $from = 0 unless defined $from;
3077 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3078
3079 print "<table class=\"history\" cellspacing=\"0\">\n";
3080 my $alternate = 1;
3081 for (my $i = $from; $i <= $to; $i++) {
3082 my %co = %{$commitlist->[$i]};
3083 if (!%co) {
3084 next;
3085 }
3086 my $commit = $co{'id'};
3087
3088 my $ref = format_ref_marker($refs, $commit);
3089
3090 if ($alternate) {
3091 print "<tr class=\"dark\">\n";
3092 } else {
3093 print "<tr class=\"light\">\n";
3094 }
3095 $alternate ^= 1;
3096 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3097 # shortlog uses chop_str($co{'author_name'}, 10)
3098 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3099 "<td>";
3100 # originally git_history used chop_str($co{'title'}, 50)
3101 print format_subject_html($co{'title'}, $co{'title_short'},
3102 href(action=>"commit", hash=>$commit), $ref);
3103 print "</td>\n" .
3104 "<td class=\"link\">" .
3105 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3106 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3107
3108 if ($ftype eq 'blob') {
3109 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3110 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3111 if (defined $blob_current && defined $blob_parent &&
3112 $blob_current ne $blob_parent) {
3113 print " | " .
3114 $cgi->a({-href => href(action=>"blobdiff",
3115 hash=>$blob_current, hash_parent=>$blob_parent,
3116 hash_base=>$hash_base, hash_parent_base=>$commit,
3117 file_name=>$file_name)},
3118 "diff to current");
3119 }
3120 }
3121 print "</td>\n" .
3122 "</tr>\n";
3123 }
3124 if (defined $extra) {
3125 print "<tr>\n" .
3126 "<td colspan=\"4\">$extra</td>\n" .
3127 "</tr>\n";
3128 }
3129 print "</table>\n";
3130}
3131
3132sub git_tags_body {
3133 # uses global variable $project
3134 my ($taglist, $from, $to, $extra) = @_;
3135 $from = 0 unless defined $from;
3136 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3137
3138 print "<table class=\"tags\" cellspacing=\"0\">\n";
3139 my $alternate = 1;
3140 for (my $i = $from; $i <= $to; $i++) {
3141 my $entry = $taglist->[$i];
3142 my %tag = %$entry;
3143 my $comment = $tag{'subject'};
3144 my $comment_short;
3145 if (defined $comment) {
3146 $comment_short = chop_str($comment, 30, 5);
3147 }
3148 if ($alternate) {
3149 print "<tr class=\"dark\">\n";
3150 } else {
3151 print "<tr class=\"light\">\n";
3152 }
3153 $alternate ^= 1;
3154 if (defined $tag{'age'}) {
3155 print "<td><i>$tag{'age'}</i></td>\n";
3156 } else {
3157 print "<td></td>\n";
3158 }
3159 print "<td>" .
3160 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3161 -class => "list name"}, esc_html($tag{'name'})) .
3162 "</td>\n" .
3163 "<td>";
3164 if (defined $comment) {
3165 print format_subject_html($comment, $comment_short,
3166 href(action=>"tag", hash=>$tag{'id'}));
3167 }
3168 print "</td>\n" .
3169 "<td class=\"selflink\">";
3170 if ($tag{'type'} eq "tag") {
3171 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3172 } else {
3173 print " ";
3174 }
3175 print "</td>\n" .
3176 "<td class=\"link\">" . " | " .
3177 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3178 if ($tag{'reftype'} eq "commit") {
3179 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3180 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3181 } elsif ($tag{'reftype'} eq "blob") {
3182 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3183 }
3184 print "</td>\n" .
3185 "</tr>";
3186 }
3187 if (defined $extra) {
3188 print "<tr>\n" .
3189 "<td colspan=\"5\">$extra</td>\n" .
3190 "</tr>\n";
3191 }
3192 print "</table>\n";
3193}
3194
3195sub git_heads_body {
3196 # uses global variable $project
3197 my ($headlist, $head, $from, $to, $extra) = @_;
3198 $from = 0 unless defined $from;
3199 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3200
3201 print "<table class=\"heads\" cellspacing=\"0\">\n";
3202 my $alternate = 1;
3203 for (my $i = $from; $i <= $to; $i++) {
3204 my $entry = $headlist->[$i];
3205 my %ref = %$entry;
3206 my $curr = $ref{'id'} eq $head;
3207 if ($alternate) {
3208 print "<tr class=\"dark\">\n";
3209 } else {
3210 print "<tr class=\"light\">\n";
3211 }
3212 $alternate ^= 1;
3213 print "<td><i>$ref{'age'}</i></td>\n" .
3214 ($curr ? "<td class=\"current_head\">" : "<td>") .
3215 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3216 -class => "list name"},esc_html($ref{'name'})) .
3217 "</td>\n" .
3218 "<td class=\"link\">" .
3219 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3220 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3221 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3222 "</td>\n" .
3223 "</tr>";
3224 }
3225 if (defined $extra) {
3226 print "<tr>\n" .
3227 "<td colspan=\"3\">$extra</td>\n" .
3228 "</tr>\n";
3229 }
3230 print "</table>\n";
3231}
3232
3233sub git_search_grep_body {
3234 my ($commitlist, $from, $to, $extra) = @_;
3235 $from = 0 unless defined $from;
3236 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3237
3238 print "<table class=\"grep\" cellspacing=\"0\">\n";
3239 my $alternate = 1;
3240 for (my $i = $from; $i <= $to; $i++) {
3241 my %co = %{$commitlist->[$i]};
3242 if (!%co) {
3243 next;
3244 }
3245 my $commit = $co{'id'};
3246 if ($alternate) {
3247 print "<tr class=\"dark\">\n";
3248 } else {
3249 print "<tr class=\"light\">\n";
3250 }
3251 $alternate ^= 1;
3252 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3253 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3254 "<td>" .
3255 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3256 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3257 my $comment = $co{'comment'};
3258 foreach my $line (@$comment) {
3259 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3260 my $lead = esc_html($1) || "";
3261 $lead = chop_str($lead, 30, 10);
3262 my $match = esc_html($2) || "";
3263 my $trail = esc_html($3) || "";
3264 $trail = chop_str($trail, 30, 10);
3265 my $text = "$lead<span class=\"match\">$match</span>$trail";
3266 print chop_str($text, 80, 5) . "<br/>\n";
3267 }
3268 }
3269 print "</td>\n" .
3270 "<td class=\"link\">" .
3271 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3272 " | " .
3273 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3274 print "</td>\n" .
3275 "</tr>\n";
3276 }
3277 if (defined $extra) {
3278 print "<tr>\n" .
3279 "<td colspan=\"3\">$extra</td>\n" .
3280 "</tr>\n";
3281 }
3282 print "</table>\n";
3283}
3284
3285## ======================================================================
3286## ======================================================================
3287## actions
3288
3289sub git_project_list {
3290 my $order = $cgi->param('o');
3291 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3292 die_error(undef, "Unknown order parameter");
3293 }
3294
3295 my @list = git_get_projects_list();
3296 if (!@list) {
3297 die_error(undef, "No projects found");
3298 }
3299
3300 git_header_html();
3301 if (-f $home_text) {
3302 print "<div class=\"index_include\">\n";
3303 open (my $fd, $home_text);
3304 print <$fd>;
3305 close $fd;
3306 print "</div>\n";
3307 }
3308 git_project_list_body(\@list, $order);
3309 git_footer_html();
3310}
3311
3312sub git_forks {
3313 my $order = $cgi->param('o');
3314 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3315 die_error(undef, "Unknown order parameter");
3316 }
3317
3318 my @list = git_get_projects_list($project);
3319 if (!@list) {
3320 die_error(undef, "No forks found");
3321 }
3322
3323 git_header_html();
3324 git_print_page_nav('','');
3325 git_print_header_div('summary', "$project forks");
3326 git_project_list_body(\@list, $order);
3327 git_footer_html();
3328}
3329
3330sub git_project_index {
3331 my @projects = git_get_projects_list($project);
3332
3333 print $cgi->header(
3334 -type => 'text/plain',
3335 -charset => 'utf-8',
3336 -content_disposition => 'inline; filename="index.aux"');
3337
3338 foreach my $pr (@projects) {
3339 if (!exists $pr->{'owner'}) {
3340 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}");
3341 }
3342
3343 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3344 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3345 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3346 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3347 $path =~ s/ /\+/g;
3348 $owner =~ s/ /\+/g;
3349
3350 print "$path $owner\n";
3351 }
3352}
3353
3354sub git_summary {
3355 my $descr = git_get_project_description($project) || "none";
3356 my %co = parse_commit("HEAD");
3357 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3358 my $head = $co{'id'};
3359
3360 my $owner = git_get_project_owner($project);
3361
3362 my $refs = git_get_references();
3363 # These get_*_list functions return one more to allow us to see if
3364 # there are more ...
3365 my @taglist = git_get_tags_list(16);
3366 my @headlist = git_get_heads_list(16);
3367 my @forklist;
3368 my ($check_forks) = gitweb_check_feature('forks');
3369
3370 if ($check_forks) {
3371 @forklist = git_get_projects_list($project);
3372 }
3373
3374 git_header_html();
3375 git_print_page_nav('summary','', $head);
3376
3377 print "<div class=\"title\"> </div>\n";
3378 print "<table cellspacing=\"0\">\n" .
3379 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3380 "<tr><td>owner</td><td>$owner</td></tr>\n";
3381 if (defined $cd{'rfc2822'}) {
3382 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3383 }
3384
3385 # use per project git URL list in $projectroot/$project/cloneurl
3386 # or make project git URL from git base URL and project name
3387 my $url_tag = "URL";
3388 my @url_list = git_get_project_url_list($project);
3389 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3390 foreach my $git_url (@url_list) {
3391 next unless $git_url;
3392 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3393 $url_tag = "";
3394 }
3395 print "</table>\n";
3396
3397 if (-s "$projectroot/$project/README.html") {
3398 if (open my $fd, "$projectroot/$project/README.html") {
3399 print "<div class=\"title\">readme</div>\n";
3400 print $_ while (<$fd>);
3401 close $fd;
3402 }
3403 }
3404
3405 # we need to request one more than 16 (0..15) to check if
3406 # those 16 are all
3407 my @commitlist = $head ? parse_commits($head, 17) : ();
3408 if (@commitlist) {
3409 git_print_header_div('shortlog');
3410 git_shortlog_body(\@commitlist, 0, 15, $refs,
3411 $#commitlist <= 15 ? undef :
3412 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3413 }
3414
3415 if (@taglist) {
3416 git_print_header_div('tags');
3417 git_tags_body(\@taglist, 0, 15,
3418 $#taglist <= 15 ? undef :
3419 $cgi->a({-href => href(action=>"tags")}, "..."));
3420 }
3421
3422 if (@headlist) {
3423 git_print_header_div('heads');
3424 git_heads_body(\@headlist, $head, 0, 15,
3425 $#headlist <= 15 ? undef :
3426 $cgi->a({-href => href(action=>"heads")}, "..."));
3427 }
3428
3429 if (@forklist) {
3430 git_print_header_div('forks');
3431 git_project_list_body(\@forklist, undef, 0, 15,
3432 $#forklist <= 15 ? undef :
3433 $cgi->a({-href => href(action=>"forks")}, "..."),
3434 'noheader');
3435 }
3436
3437 git_footer_html();
3438}
3439
3440sub git_tag {
3441 my $head = git_get_head_hash($project);
3442 git_header_html();
3443 git_print_page_nav('','', $head,undef,$head);
3444 my %tag = parse_tag($hash);
3445
3446 if (! %tag) {
3447 die_error(undef, "Unknown tag object");
3448 }
3449
3450 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3451 print "<div class=\"title_text\">\n" .
3452 "<table cellspacing=\"0\">\n" .
3453 "<tr>\n" .
3454 "<td>object</td>\n" .
3455 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3456 $tag{'object'}) . "</td>\n" .
3457 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3458 $tag{'type'}) . "</td>\n" .
3459 "</tr>\n";
3460 if (defined($tag{'author'})) {
3461 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3462 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3463 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3464 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3465 "</td></tr>\n";
3466 }
3467 print "</table>\n\n" .
3468 "</div>\n";
3469 print "<div class=\"page_body\">";
3470 my $comment = $tag{'comment'};
3471 foreach my $line (@$comment) {
3472 chomp $line;
3473 print esc_html($line, -nbsp=>1) . "<br/>\n";
3474 }
3475 print "</div>\n";
3476 git_footer_html();
3477}
3478
3479sub git_blame2 {
3480 my $fd;
3481 my $ftype;
3482
3483 my ($have_blame) = gitweb_check_feature('blame');
3484 if (!$have_blame) {
3485 die_error('403 Permission denied', "Permission denied");
3486 }
3487 die_error('404 Not Found', "File name not defined") if (!$file_name);
3488 $hash_base ||= git_get_head_hash($project);
3489 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3490 my %co = parse_commit($hash_base)
3491 or die_error(undef, "Reading commit failed");
3492 if (!defined $hash) {
3493 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3494 or die_error(undef, "Error looking up file");
3495 }
3496 $ftype = git_get_type($hash);
3497 if ($ftype !~ "blob") {
3498 die_error('400 Bad Request', "Object is not a blob");
3499 }
3500 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3501 $file_name, $hash_base)
3502 or die_error(undef, "Open git-blame failed");
3503 git_header_html();
3504 my $formats_nav =
3505 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3506 "blob") .
3507 " | " .
3508 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3509 "history") .
3510 " | " .
3511 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3512 "HEAD");
3513 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3514 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3515 git_print_page_path($file_name, $ftype, $hash_base);
3516 my @rev_color = (qw(light2 dark2));
3517 my $num_colors = scalar(@rev_color);
3518 my $current_color = 0;
3519 my $last_rev;
3520 print <<HTML;
3521<div class="page_body">
3522<table class="blame">
3523<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3524HTML
3525 my %metainfo = ();
3526 while (1) {
3527 $_ = <$fd>;
3528 last unless defined $_;
3529 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3530 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3531 if (!exists $metainfo{$full_rev}) {
3532 $metainfo{$full_rev} = {};
3533 }
3534 my $meta = $metainfo{$full_rev};
3535 while (<$fd>) {
3536 last if (s/^\t//);
3537 if (/^(\S+) (.*)$/) {
3538 $meta->{$1} = $2;
3539 }
3540 }
3541 my $data = $_;
3542 chomp $data;
3543 my $rev = substr($full_rev, 0, 8);
3544 my $author = $meta->{'author'};
3545 my %date = parse_date($meta->{'author-time'},
3546 $meta->{'author-tz'});
3547 my $date = $date{'iso-tz'};
3548 if ($group_size) {
3549 $current_color = ++$current_color % $num_colors;
3550 }
3551 print "<tr class=\"$rev_color[$current_color]\">\n";
3552 if ($group_size) {
3553 print "<td class=\"sha1\"";
3554 print " title=\"". esc_html($author) . ", $date\"";
3555 print " rowspan=\"$group_size\"" if ($group_size > 1);
3556 print ">";
3557 print $cgi->a({-href => href(action=>"commit",
3558 hash=>$full_rev,
3559 file_name=>$file_name)},
3560 esc_html($rev));
3561 print "</td>\n";
3562 }
3563 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3564 or die_error(undef, "Open git-rev-parse failed");
3565 my $parent_commit = <$dd>;
3566 close $dd;
3567 chomp($parent_commit);
3568 my $blamed = href(action => 'blame',
3569 file_name => $meta->{'filename'},
3570 hash_base => $parent_commit);
3571 print "<td class=\"linenr\">";
3572 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3573 -id => "l$lineno",
3574 -class => "linenr" },
3575 esc_html($lineno));
3576 print "</td>";
3577 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3578 print "</tr>\n";
3579 }
3580 print "</table>\n";
3581 print "</div>";
3582 close $fd
3583 or print "Reading blob failed\n";
3584 git_footer_html();
3585}
3586
3587sub git_blame {
3588 my $fd;
3589
3590 my ($have_blame) = gitweb_check_feature('blame');
3591 if (!$have_blame) {
3592 die_error('403 Permission denied', "Permission denied");
3593 }
3594 die_error('404 Not Found', "File name not defined") if (!$file_name);
3595 $hash_base ||= git_get_head_hash($project);
3596 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3597 my %co = parse_commit($hash_base)
3598 or die_error(undef, "Reading commit failed");
3599 if (!defined $hash) {
3600 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3601 or die_error(undef, "Error lookup file");
3602 }
3603 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3604 or die_error(undef, "Open git-annotate failed");
3605 git_header_html();
3606 my $formats_nav =
3607 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3608 "blob") .
3609 " | " .
3610 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3611 "history") .
3612 " | " .
3613 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3614 "HEAD");
3615 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3616 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3617 git_print_page_path($file_name, 'blob', $hash_base);
3618 print "<div class=\"page_body\">\n";
3619 print <<HTML;
3620<table class="blame">
3621 <tr>
3622 <th>Commit</th>
3623 <th>Age</th>
3624 <th>Author</th>
3625 <th>Line</th>
3626 <th>Data</th>
3627 </tr>
3628HTML
3629 my @line_class = (qw(light dark));
3630 my $line_class_len = scalar (@line_class);
3631 my $line_class_num = $#line_class;
3632 while (my $line = <$fd>) {
3633 my $long_rev;
3634 my $short_rev;
3635 my $author;
3636 my $time;
3637 my $lineno;
3638 my $data;
3639 my $age;
3640 my $age_str;
3641 my $age_class;
3642
3643 chomp $line;
3644 $line_class_num = ($line_class_num + 1) % $line_class_len;
3645
3646 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3647 $long_rev = $1;
3648 $author = $2;
3649 $time = $3;
3650 $lineno = $4;
3651 $data = $5;
3652 } else {
3653 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3654 next;
3655 }
3656 $short_rev = substr ($long_rev, 0, 8);
3657 $age = time () - $time;
3658 $age_str = age_string ($age);
3659 $age_str =~ s/ / /g;
3660 $age_class = age_class($age);
3661 $author = esc_html ($author);
3662 $author =~ s/ / /g;
3663
3664 $data = untabify($data);
3665 $data = esc_html ($data);
3666
3667 print <<HTML;
3668 <tr class="$line_class[$line_class_num]">
3669 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3670 <td class="$age_class">$age_str</td>
3671 <td>$author</td>
3672 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3673 <td class="pre">$data</td>
3674 </tr>
3675HTML
3676 } # while (my $line = <$fd>)
3677 print "</table>\n\n";
3678 close $fd
3679 or print "Reading blob failed.\n";
3680 print "</div>";
3681 git_footer_html();
3682}
3683
3684sub git_tags {
3685 my $head = git_get_head_hash($project);
3686 git_header_html();
3687 git_print_page_nav('','', $head,undef,$head);
3688 git_print_header_div('summary', $project);
3689
3690 my @tagslist = git_get_tags_list();
3691 if (@tagslist) {
3692 git_tags_body(\@tagslist);
3693 }
3694 git_footer_html();
3695}
3696
3697sub git_heads {
3698 my $head = git_get_head_hash($project);
3699 git_header_html();
3700 git_print_page_nav('','', $head,undef,$head);
3701 git_print_header_div('summary', $project);
3702
3703 my @headslist = git_get_heads_list();
3704 if (@headslist) {
3705 git_heads_body(\@headslist, $head);
3706 }
3707 git_footer_html();
3708}
3709
3710sub git_blob_plain {
3711 my $expires;
3712
3713 if (!defined $hash) {
3714 if (defined $file_name) {
3715 my $base = $hash_base || git_get_head_hash($project);
3716 $hash = git_get_hash_by_path($base, $file_name, "blob")
3717 or die_error(undef, "Error lookup file");
3718 } else {
3719 die_error(undef, "No file name defined");
3720 }
3721 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3722 # blobs defined by non-textual hash id's can be cached
3723 $expires = "+1d";
3724 }
3725
3726 my $type = shift;
3727 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3728 or die_error(undef, "Couldn't cat $file_name, $hash");
3729
3730 $type ||= blob_mimetype($fd, $file_name);
3731
3732 # save as filename, even when no $file_name is given
3733 my $save_as = "$hash";
3734 if (defined $file_name) {
3735 $save_as = $file_name;
3736 } elsif ($type =~ m/^text\//) {
3737 $save_as .= '.txt';
3738 }
3739
3740 print $cgi->header(
3741 -type => "$type",
3742 -expires=>$expires,
3743 -content_disposition => 'inline; filename="' . "$save_as" . '"');
3744 undef $/;
3745 binmode STDOUT, ':raw';
3746 print <$fd>;
3747 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3748 $/ = "\n";
3749 close $fd;
3750}
3751
3752sub git_blob {
3753 my $expires;
3754
3755 if (!defined $hash) {
3756 if (defined $file_name) {
3757 my $base = $hash_base || git_get_head_hash($project);
3758 $hash = git_get_hash_by_path($base, $file_name, "blob")
3759 or die_error(undef, "Error lookup file");
3760 } else {
3761 die_error(undef, "No file name defined");
3762 }
3763 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3764 # blobs defined by non-textual hash id's can be cached
3765 $expires = "+1d";
3766 }
3767
3768 my ($have_blame) = gitweb_check_feature('blame');
3769 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3770 or die_error(undef, "Couldn't cat $file_name, $hash");
3771 my $mimetype = blob_mimetype($fd, $file_name);
3772 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
3773 close $fd;
3774 return git_blob_plain($mimetype);
3775 }
3776 # we can have blame only for text/* mimetype
3777 $have_blame &&= ($mimetype =~ m!^text/!);
3778
3779 git_header_html(undef, $expires);
3780 my $formats_nav = '';
3781 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3782 if (defined $file_name) {
3783 if ($have_blame) {
3784 $formats_nav .=
3785 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3786 hash=>$hash, file_name=>$file_name)},
3787 "blame") .
3788 " | ";
3789 }
3790 $formats_nav .=
3791 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3792 hash=>$hash, file_name=>$file_name)},
3793 "history") .
3794 " | " .
3795 $cgi->a({-href => href(action=>"blob_plain",
3796 hash=>$hash, file_name=>$file_name)},
3797 "raw") .
3798 " | " .
3799 $cgi->a({-href => href(action=>"blob",
3800 hash_base=>"HEAD", file_name=>$file_name)},
3801 "HEAD");
3802 } else {
3803 $formats_nav .=
3804 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3805 }
3806 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3807 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3808 } else {
3809 print "<div class=\"page_nav\">\n" .
3810 "<br/><br/></div>\n" .
3811 "<div class=\"title\">$hash</div>\n";
3812 }
3813 git_print_page_path($file_name, "blob", $hash_base);
3814 print "<div class=\"page_body\">\n";
3815 if ($mimetype =~ m!^text/!) {
3816 my $nr;
3817 while (my $line = <$fd>) {
3818 chomp $line;
3819 $nr++;
3820 $line = untabify($line);
3821 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3822 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3823 }
3824 } elsif ($mimetype =~ m!^image/!) {
3825 print qq!<img type="$mimetype"!;
3826 if ($file_name) {
3827 print qq! alt="$file_name" title="$file_name"!;
3828 }
3829 print qq! src="! .
3830 href(action=>"blob_plain", hash=>$hash,
3831 hash_base=>$hash_base, file_name=>$file_name) .
3832 qq!" />\n!;
3833 }
3834 close $fd
3835 or print "Reading blob failed.\n";
3836 print "</div>";
3837 git_footer_html();
3838}
3839
3840sub git_tree {
3841 my $have_snapshot = gitweb_have_snapshot();
3842
3843 if (!defined $hash_base) {
3844 $hash_base = "HEAD";
3845 }
3846 if (!defined $hash) {
3847 if (defined $file_name) {
3848 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
3849 } else {
3850 $hash = $hash_base;
3851 }
3852 }
3853 $/ = "\0";
3854 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3855 or die_error(undef, "Open git-ls-tree failed");
3856 my @entries = map { chomp; $_ } <$fd>;
3857 close $fd or die_error(undef, "Reading tree failed");
3858 $/ = "\n";
3859
3860 my $refs = git_get_references();
3861 my $ref = format_ref_marker($refs, $hash_base);
3862 git_header_html();
3863 my $basedir = '';
3864 my ($have_blame) = gitweb_check_feature('blame');
3865 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3866 my @views_nav = ();
3867 if (defined $file_name) {
3868 push @views_nav,
3869 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3870 hash=>$hash, file_name=>$file_name)},
3871 "history"),
3872 $cgi->a({-href => href(action=>"tree",
3873 hash_base=>"HEAD", file_name=>$file_name)},
3874 "HEAD"),
3875 }
3876 if ($have_snapshot) {
3877 # FIXME: Should be available when we have no hash base as well.
3878 push @views_nav,
3879 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3880 "snapshot");
3881 }
3882 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3883 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3884 } else {
3885 undef $hash_base;
3886 print "<div class=\"page_nav\">\n";
3887 print "<br/><br/></div>\n";
3888 print "<div class=\"title\">$hash</div>\n";
3889 }
3890 if (defined $file_name) {
3891 $basedir = $file_name;
3892 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3893 $basedir .= '/';
3894 }
3895 }
3896 git_print_page_path($file_name, 'tree', $hash_base);
3897 print "<div class=\"page_body\">\n";
3898 print "<table cellspacing=\"0\">\n";
3899 my $alternate = 1;
3900 # '..' (top directory) link if possible
3901 if (defined $hash_base &&
3902 defined $file_name && $file_name =~ m![^/]+$!) {
3903 if ($alternate) {
3904 print "<tr class=\"dark\">\n";
3905 } else {
3906 print "<tr class=\"light\">\n";
3907 }
3908 $alternate ^= 1;
3909
3910 my $up = $file_name;
3911 $up =~ s!/?[^/]+$!!;
3912 undef $up unless $up;
3913 # based on git_print_tree_entry
3914 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3915 print '<td class="list">';
3916 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3917 file_name=>$up)},
3918 "..");
3919 print "</td>\n";
3920 print "<td class=\"link\"></td>\n";
3921
3922 print "</tr>\n";
3923 }
3924 foreach my $line (@entries) {
3925 my %t = parse_ls_tree_line($line, -z => 1);
3926
3927 if ($alternate) {
3928 print "<tr class=\"dark\">\n";
3929 } else {
3930 print "<tr class=\"light\">\n";
3931 }
3932 $alternate ^= 1;
3933
3934 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3935
3936 print "</tr>\n";
3937 }
3938 print "</table>\n" .
3939 "</div>";
3940 git_footer_html();
3941}
3942
3943sub git_snapshot {
3944 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3945 my $have_snapshot = (defined $ctype && defined $suffix);
3946 if (!$have_snapshot) {
3947 die_error('403 Permission denied', "Permission denied");
3948 }
3949
3950 if (!defined $hash) {
3951 $hash = git_get_head_hash($project);
3952 }
3953
3954 my $filename = decode_utf8(basename($project)) . "-$hash.tar.$suffix";
3955
3956 print $cgi->header(
3957 -type => "application/$ctype",
3958 -content_disposition => 'inline; filename="' . "$filename" . '"',
3959 -status => '200 OK');
3960
3961 my $git = git_cmd_str();
3962 my $name = $project;
3963 $name =~ s/\047/\047\\\047\047/g;
3964 open my $fd, "-|",
3965 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3966 or die_error(undef, "Execute git-tar-tree failed");
3967 binmode STDOUT, ':raw';
3968 print <$fd>;
3969 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3970 close $fd;
3971
3972}
3973
3974sub git_log {
3975 my $head = git_get_head_hash($project);
3976 if (!defined $hash) {
3977 $hash = $head;
3978 }
3979 if (!defined $page) {
3980 $page = 0;
3981 }
3982 my $refs = git_get_references();
3983
3984 my @commitlist = parse_commits($hash, 101, (100 * $page));
3985
3986 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
3987
3988 git_header_html();
3989 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3990
3991 if (!@commitlist) {
3992 my %co = parse_commit($hash);
3993
3994 git_print_header_div('summary', $project);
3995 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3996 }
3997 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
3998 for (my $i = 0; $i <= $to; $i++) {
3999 my %co = %{$commitlist[$i]};
4000 next if !%co;
4001 my $commit = $co{'id'};
4002 my $ref = format_ref_marker($refs, $commit);
4003 my %ad = parse_date($co{'author_epoch'});
4004 git_print_header_div('commit',
4005 "<span class=\"age\">$co{'age_string'}</span>" .
4006 esc_html($co{'title'}) . $ref,
4007 $commit);
4008 print "<div class=\"title_text\">\n" .
4009 "<div class=\"log_link\">\n" .
4010 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4011 " | " .
4012 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4013 " | " .
4014 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4015 "<br/>\n" .
4016 "</div>\n" .
4017 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4018 "</div>\n";
4019
4020 print "<div class=\"log_body\">\n";
4021 git_print_log($co{'comment'}, -final_empty_line=> 1);
4022 print "</div>\n";
4023 }
4024 if ($#commitlist >= 100) {
4025 print "<div class=\"page_nav\">\n";
4026 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4027 -accesskey => "n", -title => "Alt-n"}, "next");
4028 print "</div>\n";
4029 }
4030 git_footer_html();
4031}
4032
4033sub git_commit {
4034 $hash ||= $hash_base || "HEAD";
4035 my %co = parse_commit($hash);
4036 if (!%co) {
4037 die_error(undef, "Unknown commit object");
4038 }
4039 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4040 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4041
4042 my $parent = $co{'parent'};
4043 my $parents = $co{'parents'}; # listref
4044
4045 # we need to prepare $formats_nav before any parameter munging
4046 my $formats_nav;
4047 if (!defined $parent) {
4048 # --root commitdiff
4049 $formats_nav .= '(initial)';
4050 } elsif (@$parents == 1) {
4051 # single parent commit
4052 $formats_nav .=
4053 '(parent: ' .
4054 $cgi->a({-href => href(action=>"commit",
4055 hash=>$parent)},
4056 esc_html(substr($parent, 0, 7))) .
4057 ')';
4058 } else {
4059 # merge commit
4060 $formats_nav .=
4061 '(merge: ' .
4062 join(' ', map {
4063 $cgi->a({-href => href(action=>"commit",
4064 hash=>$_)},
4065 esc_html(substr($_, 0, 7)));
4066 } @$parents ) .
4067 ')';
4068 }
4069
4070 if (!defined $parent) {
4071 $parent = "--root";
4072 }
4073 my @difftree;
4074 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4075 @diff_opts,
4076 (@$parents <= 1 ? $parent : '-c'),
4077 $hash, "--"
4078 or die_error(undef, "Open git-diff-tree failed");
4079 @difftree = map { chomp; $_ } <$fd>;
4080 close $fd or die_error(undef, "Reading git-diff-tree failed");
4081
4082 # non-textual hash id's can be cached
4083 my $expires;
4084 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4085 $expires = "+1d";
4086 }
4087 my $refs = git_get_references();
4088 my $ref = format_ref_marker($refs, $co{'id'});
4089
4090 my $have_snapshot = gitweb_have_snapshot();
4091
4092 git_header_html(undef, $expires);
4093 git_print_page_nav('commit', '',
4094 $hash, $co{'tree'}, $hash,
4095 $formats_nav);
4096
4097 if (defined $co{'parent'}) {
4098 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4099 } else {
4100 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4101 }
4102 print "<div class=\"title_text\">\n" .
4103 "<table cellspacing=\"0\">\n";
4104 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4105 "<tr>" .
4106 "<td></td><td> $ad{'rfc2822'}";
4107 if ($ad{'hour_local'} < 6) {
4108 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4109 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4110 } else {
4111 printf(" (%02d:%02d %s)",
4112 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4113 }
4114 print "</td>" .
4115 "</tr>\n";
4116 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4117 print "<tr><td></td><td> $cd{'rfc2822'}" .
4118 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4119 "</td></tr>\n";
4120 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4121 print "<tr>" .
4122 "<td>tree</td>" .
4123 "<td class=\"sha1\">" .
4124 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4125 class => "list"}, $co{'tree'}) .
4126 "</td>" .
4127 "<td class=\"link\">" .
4128 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4129 "tree");
4130 if ($have_snapshot) {
4131 print " | " .
4132 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
4133 }
4134 print "</td>" .
4135 "</tr>\n";
4136
4137 foreach my $par (@$parents) {
4138 print "<tr>" .
4139 "<td>parent</td>" .
4140 "<td class=\"sha1\">" .
4141 $cgi->a({-href => href(action=>"commit", hash=>$par),
4142 class => "list"}, $par) .
4143 "</td>" .
4144 "<td class=\"link\">" .
4145 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4146 " | " .
4147 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4148 "</td>" .
4149 "</tr>\n";
4150 }
4151 print "</table>".
4152 "</div>\n";
4153
4154 print "<div class=\"page_body\">\n";
4155 git_print_log($co{'comment'});
4156 print "</div>\n";
4157
4158 git_difftree_body(\@difftree, $hash, @$parents);
4159
4160 git_footer_html();
4161}
4162
4163sub git_object {
4164 # object is defined by:
4165 # - hash or hash_base alone
4166 # - hash_base and file_name
4167 my $type;
4168
4169 # - hash or hash_base alone
4170 if ($hash || ($hash_base && !defined $file_name)) {
4171 my $object_id = $hash || $hash_base;
4172
4173 my $git_command = git_cmd_str();
4174 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4175 or die_error('404 Not Found', "Object does not exist");
4176 $type = <$fd>;
4177 chomp $type;
4178 close $fd
4179 or die_error('404 Not Found', "Object does not exist");
4180
4181 # - hash_base and file_name
4182 } elsif ($hash_base && defined $file_name) {
4183 $file_name =~ s,/+$,,;
4184
4185 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4186 or die_error('404 Not Found', "Base object does not exist");
4187
4188 # here errors should not hapen
4189 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4190 or die_error(undef, "Open git-ls-tree failed");
4191 my $line = <$fd>;
4192 close $fd;
4193
4194 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4195 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4196 die_error('404 Not Found', "File or directory for given base does not exist");
4197 }
4198 $type = $2;
4199 $hash = $3;
4200 } else {
4201 die_error('404 Not Found', "Not enough information to find object");
4202 }
4203
4204 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4205 hash=>$hash, hash_base=>$hash_base,
4206 file_name=>$file_name),
4207 -status => '302 Found');
4208}
4209
4210sub git_blobdiff {
4211 my $format = shift || 'html';
4212
4213 my $fd;
4214 my @difftree;
4215 my %diffinfo;
4216 my $expires;
4217
4218 # preparing $fd and %diffinfo for git_patchset_body
4219 # new style URI
4220 if (defined $hash_base && defined $hash_parent_base) {
4221 if (defined $file_name) {
4222 # read raw output
4223 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4224 $hash_parent_base, $hash_base,
4225 "--", (defined $file_parent ? $file_parent : ()), $file_name
4226 or die_error(undef, "Open git-diff-tree failed");
4227 @difftree = map { chomp; $_ } <$fd>;
4228 close $fd
4229 or die_error(undef, "Reading git-diff-tree failed");
4230 @difftree
4231 or die_error('404 Not Found', "Blob diff not found");
4232
4233 } elsif (defined $hash &&
4234 $hash =~ /[0-9a-fA-F]{40}/) {
4235 # try to find filename from $hash
4236
4237 # read filtered raw output
4238 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4239 $hash_parent_base, $hash_base, "--"
4240 or die_error(undef, "Open git-diff-tree failed");
4241 @difftree =
4242 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4243 # $hash == to_id
4244 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4245 map { chomp; $_ } <$fd>;
4246 close $fd
4247 or die_error(undef, "Reading git-diff-tree failed");
4248 @difftree
4249 or die_error('404 Not Found', "Blob diff not found");
4250
4251 } else {
4252 die_error('404 Not Found', "Missing one of the blob diff parameters");
4253 }
4254
4255 if (@difftree > 1) {
4256 die_error('404 Not Found', "Ambiguous blob diff specification");
4257 }
4258
4259 %diffinfo = parse_difftree_raw_line($difftree[0]);
4260 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4261 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4262
4263 $hash_parent ||= $diffinfo{'from_id'};
4264 $hash ||= $diffinfo{'to_id'};
4265
4266 # non-textual hash id's can be cached
4267 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4268 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4269 $expires = '+1d';
4270 }
4271
4272 # open patch output
4273 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4274 '-p', ($format eq 'html' ? "--full-index" : ()),
4275 $hash_parent_base, $hash_base,
4276 "--", (defined $file_parent ? $file_parent : ()), $file_name
4277 or die_error(undef, "Open git-diff-tree failed");
4278 }
4279
4280 # old/legacy style URI
4281 if (!%diffinfo && # if new style URI failed
4282 defined $hash && defined $hash_parent) {
4283 # fake git-diff-tree raw output
4284 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4285 $diffinfo{'from_id'} = $hash_parent;
4286 $diffinfo{'to_id'} = $hash;
4287 if (defined $file_name) {
4288 if (defined $file_parent) {
4289 $diffinfo{'status'} = '2';
4290 $diffinfo{'from_file'} = $file_parent;
4291 $diffinfo{'to_file'} = $file_name;
4292 } else { # assume not renamed
4293 $diffinfo{'status'} = '1';
4294 $diffinfo{'from_file'} = $file_name;
4295 $diffinfo{'to_file'} = $file_name;
4296 }
4297 } else { # no filename given
4298 $diffinfo{'status'} = '2';
4299 $diffinfo{'from_file'} = $hash_parent;
4300 $diffinfo{'to_file'} = $hash;
4301 }
4302
4303 # non-textual hash id's can be cached
4304 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4305 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4306 $expires = '+1d';
4307 }
4308
4309 # open patch output
4310 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4311 '-p', ($format eq 'html' ? "--full-index" : ()),
4312 $hash_parent, $hash, "--"
4313 or die_error(undef, "Open git-diff failed");
4314 } else {
4315 die_error('404 Not Found', "Missing one of the blob diff parameters")
4316 unless %diffinfo;
4317 }
4318
4319 # header
4320 if ($format eq 'html') {
4321 my $formats_nav =
4322 $cgi->a({-href => href(action=>"blobdiff_plain",
4323 hash=>$hash, hash_parent=>$hash_parent,
4324 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4325 file_name=>$file_name, file_parent=>$file_parent)},
4326 "raw");
4327 git_header_html(undef, $expires);
4328 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4329 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4330 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4331 } else {
4332 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4333 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4334 }
4335 if (defined $file_name) {
4336 git_print_page_path($file_name, "blob", $hash_base);
4337 } else {
4338 print "<div class=\"page_path\"></div>\n";
4339 }
4340
4341 } elsif ($format eq 'plain') {
4342 print $cgi->header(
4343 -type => 'text/plain',
4344 -charset => 'utf-8',
4345 -expires => $expires,
4346 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4347
4348 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4349
4350 } else {
4351 die_error(undef, "Unknown blobdiff format");
4352 }
4353
4354 # patch
4355 if ($format eq 'html') {
4356 print "<div class=\"page_body\">\n";
4357
4358 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4359 close $fd;
4360
4361 print "</div>\n"; # class="page_body"
4362 git_footer_html();
4363
4364 } else {
4365 while (my $line = <$fd>) {
4366 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4367 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4368
4369 print $line;
4370
4371 last if $line =~ m!^\+\+\+!;
4372 }
4373 local $/ = undef;
4374 print <$fd>;
4375 close $fd;
4376 }
4377}
4378
4379sub git_blobdiff_plain {
4380 git_blobdiff('plain');
4381}
4382
4383sub git_commitdiff {
4384 my $format = shift || 'html';
4385 $hash ||= $hash_base || "HEAD";
4386 my %co = parse_commit($hash);
4387 if (!%co) {
4388 die_error(undef, "Unknown commit object");
4389 }
4390
4391 # we need to prepare $formats_nav before any parameter munging
4392 my $formats_nav;
4393 if ($format eq 'html') {
4394 $formats_nav =
4395 $cgi->a({-href => href(action=>"commitdiff_plain",
4396 hash=>$hash, hash_parent=>$hash_parent)},
4397 "raw");
4398
4399 if (defined $hash_parent) {
4400 # commitdiff with two commits given
4401 my $hash_parent_short = $hash_parent;
4402 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4403 $hash_parent_short = substr($hash_parent, 0, 7);
4404 }
4405 $formats_nav .=
4406 ' (from: ' .
4407 $cgi->a({-href => href(action=>"commitdiff",
4408 hash=>$hash_parent)},
4409 esc_html($hash_parent_short)) .
4410 ')';
4411 } elsif (!$co{'parent'}) {
4412 # --root commitdiff
4413 $formats_nav .= ' (initial)';
4414 } elsif (scalar @{$co{'parents'}} == 1) {
4415 # single parent commit
4416 $formats_nav .=
4417 ' (parent: ' .
4418 $cgi->a({-href => href(action=>"commitdiff",
4419 hash=>$co{'parent'})},
4420 esc_html(substr($co{'parent'}, 0, 7))) .
4421 ')';
4422 } else {
4423 # merge commit
4424 $formats_nav .=
4425 ' (merge: ' .
4426 join(' ', map {
4427 $cgi->a({-href => href(action=>"commitdiff",
4428 hash=>$_)},
4429 esc_html(substr($_, 0, 7)));
4430 } @{$co{'parents'}} ) .
4431 ')';
4432 }
4433 }
4434
4435 my $hash_parent_param = $hash_parent;
4436 if (!defined $hash_parent) {
4437 $hash_parent_param =
4438 @{$co{'parents'}} > 1 ? '-c' : $co{'parent'} || '--root';
4439 }
4440
4441 # read commitdiff
4442 my $fd;
4443 my @difftree;
4444 if ($format eq 'html') {
4445 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4446 "--no-commit-id", "--patch-with-raw", "--full-index",
4447 $hash_parent_param, $hash, "--"
4448 or die_error(undef, "Open git-diff-tree failed");
4449
4450 while (my $line = <$fd>) {
4451 chomp $line;
4452 # empty line ends raw part of diff-tree output
4453 last unless $line;
4454 push @difftree, scalar parse_difftree_raw_line($line);
4455 }
4456
4457 } elsif ($format eq 'plain') {
4458 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4459 '-p', $hash_parent_param, $hash, "--"
4460 or die_error(undef, "Open git-diff-tree failed");
4461
4462 } else {
4463 die_error(undef, "Unknown commitdiff format");
4464 }
4465
4466 # non-textual hash id's can be cached
4467 my $expires;
4468 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4469 $expires = "+1d";
4470 }
4471
4472 # write commit message
4473 if ($format eq 'html') {
4474 my $refs = git_get_references();
4475 my $ref = format_ref_marker($refs, $co{'id'});
4476
4477 git_header_html(undef, $expires);
4478 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4479 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4480 git_print_authorship(\%co);
4481 print "<div class=\"page_body\">\n";
4482 if (@{$co{'comment'}} > 1) {
4483 print "<div class=\"log\">\n";
4484 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4485 print "</div>\n"; # class="log"
4486 }
4487
4488 } elsif ($format eq 'plain') {
4489 my $refs = git_get_references("tags");
4490 my $tagname = git_get_rev_name_tags($hash);
4491 my $filename = basename($project) . "-$hash.patch";
4492
4493 print $cgi->header(
4494 -type => 'text/plain',
4495 -charset => 'utf-8',
4496 -expires => $expires,
4497 -content_disposition => 'inline; filename="' . "$filename" . '"');
4498 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4499 print <<TEXT;
4500From: $co{'author'}
4501Date: $ad{'rfc2822'} ($ad{'tz_local'})
4502Subject: $co{'title'}
4503TEXT
4504 print "X-Git-Tag: $tagname\n" if $tagname;
4505 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4506
4507 foreach my $line (@{$co{'comment'}}) {
4508 print "$line\n";
4509 }
4510 print "---\n\n";
4511 }
4512
4513 # write patch
4514 if ($format eq 'html') {
4515 git_difftree_body(\@difftree, $hash, $hash_parent || @{$co{'parents'}});
4516 print "<br/>\n";
4517
4518 git_patchset_body($fd, \@difftree, $hash, $hash_parent || @{$co{'parents'}});
4519 close $fd;
4520 print "</div>\n"; # class="page_body"
4521 git_footer_html();
4522
4523 } elsif ($format eq 'plain') {
4524 local $/ = undef;
4525 print <$fd>;
4526 close $fd
4527 or print "Reading git-diff-tree failed\n";
4528 }
4529}
4530
4531sub git_commitdiff_plain {
4532 git_commitdiff('plain');
4533}
4534
4535sub git_history {
4536 if (!defined $hash_base) {
4537 $hash_base = git_get_head_hash($project);
4538 }
4539 if (!defined $page) {
4540 $page = 0;
4541 }
4542 my $ftype;
4543 my %co = parse_commit($hash_base);
4544 if (!%co) {
4545 die_error(undef, "Unknown commit object");
4546 }
4547
4548 my $refs = git_get_references();
4549 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4550
4551 if (!defined $hash && defined $file_name) {
4552 $hash = git_get_hash_by_path($hash_base, $file_name);
4553 }
4554 if (defined $hash) {
4555 $ftype = git_get_type($hash);
4556 }
4557
4558 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4559
4560 my $paging_nav = '';
4561 if ($page > 0) {
4562 $paging_nav .=
4563 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4564 file_name=>$file_name)},
4565 "first");
4566 $paging_nav .= " ⋅ " .
4567 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4568 file_name=>$file_name, page=>$page-1),
4569 -accesskey => "p", -title => "Alt-p"}, "prev");
4570 } else {
4571 $paging_nav .= "first";
4572 $paging_nav .= " ⋅ prev";
4573 }
4574 if ($#commitlist >= 100) {
4575 $paging_nav .= " ⋅ " .
4576 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4577 file_name=>$file_name, page=>$page+1),
4578 -accesskey => "n", -title => "Alt-n"}, "next");
4579 } else {
4580 $paging_nav .= " ⋅ next";
4581 }
4582 my $next_link = '';
4583 if ($#commitlist >= 100) {
4584 $next_link =
4585 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4586 file_name=>$file_name, page=>$page+1),
4587 -accesskey => "n", -title => "Alt-n"}, "next");
4588 }
4589
4590 git_header_html();
4591 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4592 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4593 git_print_page_path($file_name, $ftype, $hash_base);
4594
4595 git_history_body(\@commitlist, 0, 99,
4596 $refs, $hash_base, $ftype, $next_link);
4597
4598 git_footer_html();
4599}
4600
4601sub git_search {
4602 my ($have_search) = gitweb_check_feature('search');
4603 if (!$have_search) {
4604 die_error('403 Permission denied', "Permission denied");
4605 }
4606 if (!defined $searchtext) {
4607 die_error(undef, "Text field empty");
4608 }
4609 if (!defined $hash) {
4610 $hash = git_get_head_hash($project);
4611 }
4612 my %co = parse_commit($hash);
4613 if (!%co) {
4614 die_error(undef, "Unknown commit object");
4615 }
4616 if (!defined $page) {
4617 $page = 0;
4618 }
4619
4620 $searchtype ||= 'commit';
4621 if ($searchtype eq 'pickaxe') {
4622 # pickaxe may take all resources of your box and run for several minutes
4623 # with every query - so decide by yourself how public you make this feature
4624 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4625 if (!$have_pickaxe) {
4626 die_error('403 Permission denied', "Permission denied");
4627 }
4628 }
4629
4630 git_header_html();
4631
4632 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4633 my $greptype;
4634 if ($searchtype eq 'commit') {
4635 $greptype = "--grep=";
4636 } elsif ($searchtype eq 'author') {
4637 $greptype = "--author=";
4638 } elsif ($searchtype eq 'committer') {
4639 $greptype = "--committer=";
4640 }
4641 $greptype .= $search_regexp;
4642 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
4643
4644 my $paging_nav = '';
4645 if ($page > 0) {
4646 $paging_nav .=
4647 $cgi->a({-href => href(action=>"search", hash=>$hash,
4648 searchtext=>$searchtext, searchtype=>$searchtype)},
4649 "first");
4650 $paging_nav .= " ⋅ " .
4651 $cgi->a({-href => href(action=>"search", hash=>$hash,
4652 searchtext=>$searchtext, searchtype=>$searchtype,
4653 page=>$page-1),
4654 -accesskey => "p", -title => "Alt-p"}, "prev");
4655 } else {
4656 $paging_nav .= "first";
4657 $paging_nav .= " ⋅ prev";
4658 }
4659 if ($#commitlist >= 100) {
4660 $paging_nav .= " ⋅ " .
4661 $cgi->a({-href => href(action=>"search", hash=>$hash,
4662 searchtext=>$searchtext, searchtype=>$searchtype,
4663 page=>$page+1),
4664 -accesskey => "n", -title => "Alt-n"}, "next");
4665 } else {
4666 $paging_nav .= " ⋅ next";
4667 }
4668 my $next_link = '';
4669 if ($#commitlist >= 100) {
4670 $next_link =
4671 $cgi->a({-href => href(action=>"search", hash=>$hash,
4672 searchtext=>$searchtext, searchtype=>$searchtype,
4673 page=>$page+1),
4674 -accesskey => "n", -title => "Alt-n"}, "next");
4675 }
4676
4677 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
4678 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4679 git_search_grep_body(\@commitlist, 0, 99, $next_link);
4680 }
4681
4682 if ($searchtype eq 'pickaxe') {
4683 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4684 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4685
4686 print "<table cellspacing=\"0\">\n";
4687 my $alternate = 1;
4688 $/ = "\n";
4689 my $git_command = git_cmd_str();
4690 open my $fd, "-|", "$git_command rev-list $hash | " .
4691 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
4692 undef %co;
4693 my @files;
4694 while (my $line = <$fd>) {
4695 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
4696 my %set;
4697 $set{'file'} = $6;
4698 $set{'from_id'} = $3;
4699 $set{'to_id'} = $4;
4700 $set{'id'} = $set{'to_id'};
4701 if ($set{'id'} =~ m/0{40}/) {
4702 $set{'id'} = $set{'from_id'};
4703 }
4704 if ($set{'id'} =~ m/0{40}/) {
4705 next;
4706 }
4707 push @files, \%set;
4708 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
4709 if (%co) {
4710 if ($alternate) {
4711 print "<tr class=\"dark\">\n";
4712 } else {
4713 print "<tr class=\"light\">\n";
4714 }
4715 $alternate ^= 1;
4716 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4717 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4718 "<td>" .
4719 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4720 -class => "list subject"},
4721 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4722 while (my $setref = shift @files) {
4723 my %set = %$setref;
4724 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
4725 hash=>$set{'id'}, file_name=>$set{'file'}),
4726 -class => "list"},
4727 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
4728 "<br/>\n";
4729 }
4730 print "</td>\n" .
4731 "<td class=\"link\">" .
4732 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4733 " | " .
4734 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4735 print "</td>\n" .
4736 "</tr>\n";
4737 }
4738 %co = parse_commit($1);
4739 }
4740 }
4741 close $fd;
4742
4743 print "</table>\n";
4744 }
4745 git_footer_html();
4746}
4747
4748sub git_search_help {
4749 git_header_html();
4750 git_print_page_nav('','', $hash,$hash,$hash);
4751 print <<EOT;
4752<dl>
4753<dt><b>commit</b></dt>
4754<dd>The commit messages and authorship information will be scanned for the given string.</dd>
4755<dt><b>author</b></dt>
4756<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
4757<dt><b>committer</b></dt>
4758<dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
4759EOT
4760 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4761 if ($have_pickaxe) {
4762 print <<EOT;
4763<dt><b>pickaxe</b></dt>
4764<dd>All commits that caused the string to appear or disappear from any file (changes that
4765added, removed or "modified" the string) will be listed. This search can take a while and
4766takes a lot of strain on the server, so please use it wisely.</dd>
4767EOT
4768 }
4769 print "</dl>\n";
4770 git_footer_html();
4771}
4772
4773sub git_shortlog {
4774 my $head = git_get_head_hash($project);
4775 if (!defined $hash) {
4776 $hash = $head;
4777 }
4778 if (!defined $page) {
4779 $page = 0;
4780 }
4781 my $refs = git_get_references();
4782
4783 my @commitlist = parse_commits($hash, 101, (100 * $page));
4784
4785 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
4786 my $next_link = '';
4787 if ($#commitlist >= 100) {
4788 $next_link =
4789 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
4790 -accesskey => "n", -title => "Alt-n"}, "next");
4791 }
4792
4793 git_header_html();
4794 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
4795 git_print_header_div('summary', $project);
4796
4797 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
4798
4799 git_footer_html();
4800}
4801
4802## ......................................................................
4803## feeds (RSS, Atom; OPML)
4804
4805sub git_feed {
4806 my $format = shift || 'atom';
4807 my ($have_blame) = gitweb_check_feature('blame');
4808
4809 # Atom: http://www.atomenabled.org/developers/syndication/
4810 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
4811 if ($format ne 'rss' && $format ne 'atom') {
4812 die_error(undef, "Unknown web feed format");
4813 }
4814
4815 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
4816 my $head = $hash || 'HEAD';
4817 my @commitlist = parse_commits($head, 150);
4818
4819 my %latest_commit;
4820 my %latest_date;
4821 my $content_type = "application/$format+xml";
4822 if (defined $cgi->http('HTTP_ACCEPT') &&
4823 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
4824 # browser (feed reader) prefers text/xml
4825 $content_type = 'text/xml';
4826 }
4827 if (defined($commitlist[0])) {
4828 %latest_commit = %{$commitlist[0]};
4829 %latest_date = parse_date($latest_commit{'author_epoch'});
4830 print $cgi->header(
4831 -type => $content_type,
4832 -charset => 'utf-8',
4833 -last_modified => $latest_date{'rfc2822'});
4834 } else {
4835 print $cgi->header(
4836 -type => $content_type,
4837 -charset => 'utf-8');
4838 }
4839
4840 # Optimization: skip generating the body if client asks only
4841 # for Last-Modified date.
4842 return if ($cgi->request_method() eq 'HEAD');
4843
4844 # header variables
4845 my $title = "$site_name - $project/$action";
4846 my $feed_type = 'log';
4847 if (defined $hash) {
4848 $title .= " - '$hash'";
4849 $feed_type = 'branch log';
4850 if (defined $file_name) {
4851 $title .= " :: $file_name";
4852 $feed_type = 'history';
4853 }
4854 } elsif (defined $file_name) {
4855 $title .= " - $file_name";
4856 $feed_type = 'history';
4857 }
4858 $title .= " $feed_type";
4859 my $descr = git_get_project_description($project);
4860 if (defined $descr) {
4861 $descr = esc_html($descr);
4862 } else {
4863 $descr = "$project " .
4864 ($format eq 'rss' ? 'RSS' : 'Atom') .
4865 " feed";
4866 }
4867 my $owner = git_get_project_owner($project);
4868 $owner = esc_html($owner);
4869
4870 #header
4871 my $alt_url;
4872 if (defined $file_name) {
4873 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
4874 } elsif (defined $hash) {
4875 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
4876 } else {
4877 $alt_url = href(-full=>1, action=>"summary");
4878 }
4879 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
4880 if ($format eq 'rss') {
4881 print <<XML;
4882<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
4883<channel>
4884XML
4885 print "<title>$title</title>\n" .
4886 "<link>$alt_url</link>\n" .
4887 "<description>$descr</description>\n" .
4888 "<language>en</language>\n";
4889 } elsif ($format eq 'atom') {
4890 print <<XML;
4891<feed xmlns="http://www.w3.org/2005/Atom">
4892XML
4893 print "<title>$title</title>\n" .
4894 "<subtitle>$descr</subtitle>\n" .
4895 '<link rel="alternate" type="text/html" href="' .
4896 $alt_url . '" />' . "\n" .
4897 '<link rel="self" type="' . $content_type . '" href="' .
4898 $cgi->self_url() . '" />' . "\n" .
4899 "<id>" . href(-full=>1) . "</id>\n" .
4900 # use project owner for feed author
4901 "<author><name>$owner</name></author>\n";
4902 if (defined $favicon) {
4903 print "<icon>" . esc_url($favicon) . "</icon>\n";
4904 }
4905 if (defined $logo_url) {
4906 # not twice as wide as tall: 72 x 27 pixels
4907 print "<logo>" . esc_url($logo) . "</logo>\n";
4908 }
4909 if (! %latest_date) {
4910 # dummy date to keep the feed valid until commits trickle in:
4911 print "<updated>1970-01-01T00:00:00Z</updated>\n";
4912 } else {
4913 print "<updated>$latest_date{'iso-8601'}</updated>\n";
4914 }
4915 }
4916
4917 # contents
4918 for (my $i = 0; $i <= $#commitlist; $i++) {
4919 my %co = %{$commitlist[$i]};
4920 my $commit = $co{'id'};
4921 # we read 150, we always show 30 and the ones more recent than 48 hours
4922 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
4923 last;
4924 }
4925 my %cd = parse_date($co{'author_epoch'});
4926
4927 # get list of changed files
4928 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4929 $co{'parent'}, $co{'id'}, "--", (defined $file_name ? $file_name : ())
4930 or next;
4931 my @difftree = map { chomp; $_ } <$fd>;
4932 close $fd
4933 or next;
4934
4935 # print element (entry, item)
4936 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
4937 if ($format eq 'rss') {
4938 print "<item>\n" .
4939 "<title>" . esc_html($co{'title'}) . "</title>\n" .
4940 "<author>" . esc_html($co{'author'}) . "</author>\n" .
4941 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
4942 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
4943 "<link>$co_url</link>\n" .
4944 "<description>" . esc_html($co{'title'}) . "</description>\n" .
4945 "<content:encoded>" .
4946 "<![CDATA[\n";
4947 } elsif ($format eq 'atom') {
4948 print "<entry>\n" .
4949 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
4950 "<updated>$cd{'iso-8601'}</updated>\n" .
4951 "<author>\n" .
4952 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
4953 if ($co{'author_email'}) {
4954 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
4955 }
4956 print "</author>\n" .
4957 # use committer for contributor
4958 "<contributor>\n" .
4959 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
4960 if ($co{'committer_email'}) {
4961 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
4962 }
4963 print "</contributor>\n" .
4964 "<published>$cd{'iso-8601'}</published>\n" .
4965 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
4966 "<id>$co_url</id>\n" .
4967 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
4968 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
4969 }
4970 my $comment = $co{'comment'};
4971 print "<pre>\n";
4972 foreach my $line (@$comment) {
4973 $line = esc_html($line);
4974 print "$line\n";
4975 }
4976 print "</pre><ul>\n";
4977 foreach my $difftree_line (@difftree) {
4978 my %difftree = parse_difftree_raw_line($difftree_line);
4979 next if !$difftree{'from_id'};
4980
4981 my $file = $difftree{'file'} || $difftree{'to_file'};
4982
4983 print "<li>" .
4984 "[" .
4985 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
4986 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
4987 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
4988 file_name=>$file, file_parent=>$difftree{'from_file'}),
4989 -title => "diff"}, 'D');
4990 if ($have_blame) {
4991 print $cgi->a({-href => href(-full=>1, action=>"blame",
4992 file_name=>$file, hash_base=>$commit),
4993 -title => "blame"}, 'B');
4994 }
4995 # if this is not a feed of a file history
4996 if (!defined $file_name || $file_name ne $file) {
4997 print $cgi->a({-href => href(-full=>1, action=>"history",
4998 file_name=>$file, hash=>$commit),
4999 -title => "history"}, 'H');
5000 }
5001 $file = esc_path($file);
5002 print "] ".
5003 "$file</li>\n";
5004 }
5005 if ($format eq 'rss') {
5006 print "</ul>]]>\n" .
5007 "</content:encoded>\n" .
5008 "</item>\n";
5009 } elsif ($format eq 'atom') {
5010 print "</ul>\n</div>\n" .
5011 "</content>\n" .
5012 "</entry>\n";
5013 }
5014 }
5015
5016 # end of feed
5017 if ($format eq 'rss') {
5018 print "</channel>\n</rss>\n";
5019 } elsif ($format eq 'atom') {
5020 print "</feed>\n";
5021 }
5022}
5023
5024sub git_rss {
5025 git_feed('rss');
5026}
5027
5028sub git_atom {
5029 git_feed('atom');
5030}
5031
5032sub git_opml {
5033 my @list = git_get_projects_list();
5034
5035 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5036 print <<XML;
5037<?xml version="1.0" encoding="utf-8"?>
5038<opml version="1.0">
5039<head>
5040 <title>$site_name OPML Export</title>
5041</head>
5042<body>
5043<outline text="git RSS feeds">
5044XML
5045
5046 foreach my $pr (@list) {
5047 my %proj = %$pr;
5048 my $head = git_get_head_hash($proj{'path'});
5049 if (!defined $head) {
5050 next;
5051 }
5052 $git_dir = "$projectroot/$proj{'path'}";
5053 my %co = parse_commit($head);
5054 if (!%co) {
5055 next;
5056 }
5057
5058 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5059 my $rss = "$my_url?p=$proj{'path'};a=rss";
5060 my $html = "$my_url?p=$proj{'path'};a=summary";
5061 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5062 }
5063 print <<XML;
5064</outline>
5065</body>
5066</opml>
5067XML
5068}