549e0270b6d8a277b20546756d64a639fce9ae99
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 chomp $descr;
1118 return $descr;
1119}
1120
1121sub git_get_project_url_list {
1122 my $path = shift;
1123
1124 open my $fd, "$projectroot/$path/cloneurl" or return;
1125 my @git_project_url_list = map { chomp; $_ } <$fd>;
1126 close $fd;
1127
1128 return wantarray ? @git_project_url_list : \@git_project_url_list;
1129}
1130
1131sub git_get_projects_list {
1132 my ($filter) = @_;
1133 my @list;
1134
1135 $filter ||= '';
1136 $filter =~ s/\.git$//;
1137
1138 my ($check_forks) = gitweb_check_feature('forks');
1139
1140 if (-d $projects_list) {
1141 # search in directory
1142 my $dir = $projects_list . ($filter ? "/$filter" : '');
1143 # remove the trailing "/"
1144 $dir =~ s!/+$!!;
1145 my $pfxlen = length("$dir");
1146
1147 File::Find::find({
1148 follow_fast => 1, # follow symbolic links
1149 dangling_symlinks => 0, # ignore dangling symlinks, silently
1150 wanted => sub {
1151 # skip project-list toplevel, if we get it.
1152 return if (m!^[/.]$!);
1153 # only directories can be git repositories
1154 return unless (-d $_);
1155
1156 my $subdir = substr($File::Find::name, $pfxlen + 1);
1157 # we check related file in $projectroot
1158 if ($check_forks and $subdir =~ m#/.#) {
1159 $File::Find::prune = 1;
1160 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1161 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1162 $File::Find::prune = 1;
1163 }
1164 },
1165 }, "$dir");
1166
1167 } elsif (-f $projects_list) {
1168 # read from file(url-encoded):
1169 # 'git%2Fgit.git Linus+Torvalds'
1170 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1171 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1172 my %paths;
1173 open my ($fd), $projects_list or return;
1174 PROJECT:
1175 while (my $line = <$fd>) {
1176 chomp $line;
1177 my ($path, $owner) = split ' ', $line;
1178 $path = unescape($path);
1179 $owner = unescape($owner);
1180 if (!defined $path) {
1181 next;
1182 }
1183 if ($filter ne '') {
1184 # looking for forks;
1185 my $pfx = substr($path, 0, length($filter));
1186 if ($pfx ne $filter) {
1187 next PROJECT;
1188 }
1189 my $sfx = substr($path, length($filter));
1190 if ($sfx !~ /^\/.*\.git$/) {
1191 next PROJECT;
1192 }
1193 } elsif ($check_forks) {
1194 PATH:
1195 foreach my $filter (keys %paths) {
1196 # looking for forks;
1197 my $pfx = substr($path, 0, length($filter));
1198 if ($pfx ne $filter) {
1199 next PATH;
1200 }
1201 my $sfx = substr($path, length($filter));
1202 if ($sfx !~ /^\/.*\.git$/) {
1203 next PATH;
1204 }
1205 # is a fork, don't include it in
1206 # the list
1207 next PROJECT;
1208 }
1209 }
1210 if (check_export_ok("$projectroot/$path")) {
1211 my $pr = {
1212 path => $path,
1213 owner => decode_utf8($owner),
1214 };
1215 push @list, $pr;
1216 (my $forks_path = $path) =~ s/\.git$//;
1217 $paths{$forks_path}++;
1218 }
1219 }
1220 close $fd;
1221 }
1222 return @list;
1223}
1224
1225sub git_get_project_owner {
1226 my $project = shift;
1227 my $owner;
1228
1229 return undef unless $project;
1230
1231 # read from file (url-encoded):
1232 # 'git%2Fgit.git Linus+Torvalds'
1233 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1234 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1235 if (-f $projects_list) {
1236 open (my $fd , $projects_list);
1237 while (my $line = <$fd>) {
1238 chomp $line;
1239 my ($pr, $ow) = split ' ', $line;
1240 $pr = unescape($pr);
1241 $ow = unescape($ow);
1242 if ($pr eq $project) {
1243 $owner = decode_utf8($ow);
1244 last;
1245 }
1246 }
1247 close $fd;
1248 }
1249 if (!defined $owner) {
1250 $owner = get_file_owner("$projectroot/$project");
1251 }
1252
1253 return $owner;
1254}
1255
1256sub git_get_last_activity {
1257 my ($path) = @_;
1258 my $fd;
1259
1260 $git_dir = "$projectroot/$path";
1261 open($fd, "-|", git_cmd(), 'for-each-ref',
1262 '--format=%(committer)',
1263 '--sort=-committerdate',
1264 '--count=1',
1265 'refs/heads') or return;
1266 my $most_recent = <$fd>;
1267 close $fd or return;
1268 if (defined $most_recent &&
1269 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1270 my $timestamp = $1;
1271 my $age = time - $timestamp;
1272 return ($age, age_string($age));
1273 }
1274}
1275
1276sub git_get_references {
1277 my $type = shift || "";
1278 my %refs;
1279 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1280 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1281 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1282 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1283 or return;
1284
1285 while (my $line = <$fd>) {
1286 chomp $line;
1287 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1288 if (defined $refs{$1}) {
1289 push @{$refs{$1}}, $2;
1290 } else {
1291 $refs{$1} = [ $2 ];
1292 }
1293 }
1294 }
1295 close $fd or return;
1296 return \%refs;
1297}
1298
1299sub git_get_rev_name_tags {
1300 my $hash = shift || return undef;
1301
1302 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1303 or return;
1304 my $name_rev = <$fd>;
1305 close $fd;
1306
1307 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1308 return $1;
1309 } else {
1310 # catches also '$hash undefined' output
1311 return undef;
1312 }
1313}
1314
1315## ----------------------------------------------------------------------
1316## parse to hash functions
1317
1318sub parse_date {
1319 my $epoch = shift;
1320 my $tz = shift || "-0000";
1321
1322 my %date;
1323 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1324 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1325 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1326 $date{'hour'} = $hour;
1327 $date{'minute'} = $min;
1328 $date{'mday'} = $mday;
1329 $date{'day'} = $days[$wday];
1330 $date{'month'} = $months[$mon];
1331 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1332 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1333 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1334 $mday, $months[$mon], $hour ,$min;
1335 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1336 1900+$year, $mon, $mday, $hour ,$min, $sec;
1337
1338 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1339 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1340 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1341 $date{'hour_local'} = $hour;
1342 $date{'minute_local'} = $min;
1343 $date{'tz_local'} = $tz;
1344 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1345 1900+$year, $mon+1, $mday,
1346 $hour, $min, $sec, $tz);
1347 return %date;
1348}
1349
1350sub parse_tag {
1351 my $tag_id = shift;
1352 my %tag;
1353 my @comment;
1354
1355 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1356 $tag{'id'} = $tag_id;
1357 while (my $line = <$fd>) {
1358 chomp $line;
1359 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1360 $tag{'object'} = $1;
1361 } elsif ($line =~ m/^type (.+)$/) {
1362 $tag{'type'} = $1;
1363 } elsif ($line =~ m/^tag (.+)$/) {
1364 $tag{'name'} = $1;
1365 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1366 $tag{'author'} = $1;
1367 $tag{'epoch'} = $2;
1368 $tag{'tz'} = $3;
1369 } elsif ($line =~ m/--BEGIN/) {
1370 push @comment, $line;
1371 last;
1372 } elsif ($line eq "") {
1373 last;
1374 }
1375 }
1376 push @comment, <$fd>;
1377 $tag{'comment'} = \@comment;
1378 close $fd or return;
1379 if (!defined $tag{'name'}) {
1380 return
1381 };
1382 return %tag
1383}
1384
1385sub parse_commit_text {
1386 my ($commit_text, $withparents) = @_;
1387 my @commit_lines = split '\n', $commit_text;
1388 my %co;
1389
1390 pop @commit_lines; # Remove '\0'
1391
1392 if (! @commit_lines) {
1393 return;
1394 }
1395
1396 my $header = shift @commit_lines;
1397 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1398 return;
1399 }
1400 ($co{'id'}, my @parents) = split ' ', $header;
1401 while (my $line = shift @commit_lines) {
1402 last if $line eq "\n";
1403 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1404 $co{'tree'} = $1;
1405 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1406 push @parents, $1;
1407 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1408 $co{'author'} = $1;
1409 $co{'author_epoch'} = $2;
1410 $co{'author_tz'} = $3;
1411 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1412 $co{'author_name'} = $1;
1413 $co{'author_email'} = $2;
1414 } else {
1415 $co{'author_name'} = $co{'author'};
1416 }
1417 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1418 $co{'committer'} = $1;
1419 $co{'committer_epoch'} = $2;
1420 $co{'committer_tz'} = $3;
1421 $co{'committer_name'} = $co{'committer'};
1422 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1423 $co{'committer_name'} = $1;
1424 $co{'committer_email'} = $2;
1425 } else {
1426 $co{'committer_name'} = $co{'committer'};
1427 }
1428 }
1429 }
1430 if (!defined $co{'tree'}) {
1431 return;
1432 };
1433 $co{'parents'} = \@parents;
1434 $co{'parent'} = $parents[0];
1435
1436 foreach my $title (@commit_lines) {
1437 $title =~ s/^ //;
1438 if ($title ne "") {
1439 $co{'title'} = chop_str($title, 80, 5);
1440 # remove leading stuff of merges to make the interesting part visible
1441 if (length($title) > 50) {
1442 $title =~ s/^Automatic //;
1443 $title =~ s/^merge (of|with) /Merge ... /i;
1444 if (length($title) > 50) {
1445 $title =~ s/(http|rsync):\/\///;
1446 }
1447 if (length($title) > 50) {
1448 $title =~ s/(master|www|rsync)\.//;
1449 }
1450 if (length($title) > 50) {
1451 $title =~ s/kernel.org:?//;
1452 }
1453 if (length($title) > 50) {
1454 $title =~ s/\/pub\/scm//;
1455 }
1456 }
1457 $co{'title_short'} = chop_str($title, 50, 5);
1458 last;
1459 }
1460 }
1461 if ($co{'title'} eq "") {
1462 $co{'title'} = $co{'title_short'} = '(no commit message)';
1463 }
1464 # remove added spaces
1465 foreach my $line (@commit_lines) {
1466 $line =~ s/^ //;
1467 }
1468 $co{'comment'} = \@commit_lines;
1469
1470 my $age = time - $co{'committer_epoch'};
1471 $co{'age'} = $age;
1472 $co{'age_string'} = age_string($age);
1473 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1474 if ($age > 60*60*24*7*2) {
1475 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1476 $co{'age_string_age'} = $co{'age_string'};
1477 } else {
1478 $co{'age_string_date'} = $co{'age_string'};
1479 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1480 }
1481 return %co;
1482}
1483
1484sub parse_commit {
1485 my ($commit_id) = @_;
1486 my %co;
1487
1488 local $/ = "\0";
1489
1490 open my $fd, "-|", git_cmd(), "rev-list",
1491 "--parents",
1492 "--header",
1493 "--max-count=1",
1494 $commit_id,
1495 "--",
1496 or die_error(undef, "Open git-rev-list failed");
1497 %co = parse_commit_text(<$fd>, 1);
1498 close $fd;
1499
1500 return %co;
1501}
1502
1503sub parse_commits {
1504 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1505 my @cos;
1506
1507 $maxcount ||= 1;
1508 $skip ||= 0;
1509
1510 local $/ = "\0";
1511
1512 open my $fd, "-|", git_cmd(), "rev-list",
1513 "--header",
1514 ($arg ? ($arg) : ()),
1515 ("--max-count=" . $maxcount),
1516 ("--skip=" . $skip),
1517 $commit_id,
1518 "--",
1519 ($filename ? ($filename) : ())
1520 or die_error(undef, "Open git-rev-list failed");
1521 while (my $line = <$fd>) {
1522 my %co = parse_commit_text($line);
1523 push @cos, \%co;
1524 }
1525 close $fd;
1526
1527 return wantarray ? @cos : \@cos;
1528}
1529
1530# parse ref from ref_file, given by ref_id, with given type
1531sub parse_ref {
1532 my $ref_file = shift;
1533 my $ref_id = shift;
1534 my $type = shift || git_get_type($ref_id);
1535 my %ref_item;
1536
1537 $ref_item{'type'} = $type;
1538 $ref_item{'id'} = $ref_id;
1539 $ref_item{'epoch'} = 0;
1540 $ref_item{'age'} = "unknown";
1541 if ($type eq "tag") {
1542 my %tag = parse_tag($ref_id);
1543 $ref_item{'comment'} = $tag{'comment'};
1544 if ($tag{'type'} eq "commit") {
1545 my %co = parse_commit($tag{'object'});
1546 $ref_item{'epoch'} = $co{'committer_epoch'};
1547 $ref_item{'age'} = $co{'age_string'};
1548 } elsif (defined($tag{'epoch'})) {
1549 my $age = time - $tag{'epoch'};
1550 $ref_item{'epoch'} = $tag{'epoch'};
1551 $ref_item{'age'} = age_string($age);
1552 }
1553 $ref_item{'reftype'} = $tag{'type'};
1554 $ref_item{'name'} = $tag{'name'};
1555 $ref_item{'refid'} = $tag{'object'};
1556 } elsif ($type eq "commit"){
1557 my %co = parse_commit($ref_id);
1558 $ref_item{'reftype'} = "commit";
1559 $ref_item{'name'} = $ref_file;
1560 $ref_item{'title'} = $co{'title'};
1561 $ref_item{'refid'} = $ref_id;
1562 $ref_item{'epoch'} = $co{'committer_epoch'};
1563 $ref_item{'age'} = $co{'age_string'};
1564 } else {
1565 $ref_item{'reftype'} = $type;
1566 $ref_item{'name'} = $ref_file;
1567 $ref_item{'refid'} = $ref_id;
1568 }
1569
1570 return %ref_item;
1571}
1572
1573# parse line of git-diff-tree "raw" output
1574sub parse_difftree_raw_line {
1575 my $line = shift;
1576 my %res;
1577
1578 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1579 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1580 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1581 $res{'from_mode'} = $1;
1582 $res{'to_mode'} = $2;
1583 $res{'from_id'} = $3;
1584 $res{'to_id'} = $4;
1585 $res{'status'} = $5;
1586 $res{'similarity'} = $6;
1587 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1588 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1589 } else {
1590 $res{'file'} = unquote($7);
1591 }
1592 }
1593 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1594 # combined diff (for merge commit)
1595 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1596 $res{'nparents'} = length($1);
1597 $res{'from_mode'} = [ split(' ', $2) ];
1598 $res{'to_mode'} = pop @{$res{'from_mode'}};
1599 $res{'from_id'} = [ split(' ', $3) ];
1600 $res{'to_id'} = pop @{$res{'from_id'}};
1601 $res{'status'} = [ split('', $4) ];
1602 $res{'to_file'} = unquote($5);
1603 }
1604 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1605 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1606 $res{'commit'} = $1;
1607 }
1608
1609 return wantarray ? %res : \%res;
1610}
1611
1612# parse line of git-ls-tree output
1613sub parse_ls_tree_line ($;%) {
1614 my $line = shift;
1615 my %opts = @_;
1616 my %res;
1617
1618 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1619 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1620
1621 $res{'mode'} = $1;
1622 $res{'type'} = $2;
1623 $res{'hash'} = $3;
1624 if ($opts{'-z'}) {
1625 $res{'name'} = $4;
1626 } else {
1627 $res{'name'} = unquote($4);
1628 }
1629
1630 return wantarray ? %res : \%res;
1631}
1632
1633## ......................................................................
1634## parse to array of hashes functions
1635
1636sub git_get_heads_list {
1637 my $limit = shift;
1638 my @headslist;
1639
1640 open my $fd, '-|', git_cmd(), 'for-each-ref',
1641 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1642 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1643 'refs/heads'
1644 or return;
1645 while (my $line = <$fd>) {
1646 my %ref_item;
1647
1648 chomp $line;
1649 my ($refinfo, $committerinfo) = split(/\0/, $line);
1650 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1651 my ($committer, $epoch, $tz) =
1652 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1653 $name =~ s!^refs/heads/!!;
1654
1655 $ref_item{'name'} = $name;
1656 $ref_item{'id'} = $hash;
1657 $ref_item{'title'} = $title || '(no commit message)';
1658 $ref_item{'epoch'} = $epoch;
1659 if ($epoch) {
1660 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1661 } else {
1662 $ref_item{'age'} = "unknown";
1663 }
1664
1665 push @headslist, \%ref_item;
1666 }
1667 close $fd;
1668
1669 return wantarray ? @headslist : \@headslist;
1670}
1671
1672sub git_get_tags_list {
1673 my $limit = shift;
1674 my @tagslist;
1675
1676 open my $fd, '-|', git_cmd(), 'for-each-ref',
1677 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1678 '--format=%(objectname) %(objecttype) %(refname) '.
1679 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1680 'refs/tags'
1681 or return;
1682 while (my $line = <$fd>) {
1683 my %ref_item;
1684
1685 chomp $line;
1686 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1687 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1688 my ($creator, $epoch, $tz) =
1689 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1690 $name =~ s!^refs/tags/!!;
1691
1692 $ref_item{'type'} = $type;
1693 $ref_item{'id'} = $id;
1694 $ref_item{'name'} = $name;
1695 if ($type eq "tag") {
1696 $ref_item{'subject'} = $title;
1697 $ref_item{'reftype'} = $reftype;
1698 $ref_item{'refid'} = $refid;
1699 } else {
1700 $ref_item{'reftype'} = $type;
1701 $ref_item{'refid'} = $id;
1702 }
1703
1704 if ($type eq "tag" || $type eq "commit") {
1705 $ref_item{'epoch'} = $epoch;
1706 if ($epoch) {
1707 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1708 } else {
1709 $ref_item{'age'} = "unknown";
1710 }
1711 }
1712
1713 push @tagslist, \%ref_item;
1714 }
1715 close $fd;
1716
1717 return wantarray ? @tagslist : \@tagslist;
1718}
1719
1720## ----------------------------------------------------------------------
1721## filesystem-related functions
1722
1723sub get_file_owner {
1724 my $path = shift;
1725
1726 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1727 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1728 if (!defined $gcos) {
1729 return undef;
1730 }
1731 my $owner = $gcos;
1732 $owner =~ s/[,;].*$//;
1733 return decode_utf8($owner);
1734}
1735
1736## ......................................................................
1737## mimetype related functions
1738
1739sub mimetype_guess_file {
1740 my $filename = shift;
1741 my $mimemap = shift;
1742 -r $mimemap or return undef;
1743
1744 my %mimemap;
1745 open(MIME, $mimemap) or return undef;
1746 while (<MIME>) {
1747 next if m/^#/; # skip comments
1748 my ($mime, $exts) = split(/\t+/);
1749 if (defined $exts) {
1750 my @exts = split(/\s+/, $exts);
1751 foreach my $ext (@exts) {
1752 $mimemap{$ext} = $mime;
1753 }
1754 }
1755 }
1756 close(MIME);
1757
1758 $filename =~ /\.([^.]*)$/;
1759 return $mimemap{$1};
1760}
1761
1762sub mimetype_guess {
1763 my $filename = shift;
1764 my $mime;
1765 $filename =~ /\./ or return undef;
1766
1767 if ($mimetypes_file) {
1768 my $file = $mimetypes_file;
1769 if ($file !~ m!^/!) { # if it is relative path
1770 # it is relative to project
1771 $file = "$projectroot/$project/$file";
1772 }
1773 $mime = mimetype_guess_file($filename, $file);
1774 }
1775 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1776 return $mime;
1777}
1778
1779sub blob_mimetype {
1780 my $fd = shift;
1781 my $filename = shift;
1782
1783 if ($filename) {
1784 my $mime = mimetype_guess($filename);
1785 $mime and return $mime;
1786 }
1787
1788 # just in case
1789 return $default_blob_plain_mimetype unless $fd;
1790
1791 if (-T $fd) {
1792 return 'text/plain' .
1793 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1794 } elsif (! $filename) {
1795 return 'application/octet-stream';
1796 } elsif ($filename =~ m/\.png$/i) {
1797 return 'image/png';
1798 } elsif ($filename =~ m/\.gif$/i) {
1799 return 'image/gif';
1800 } elsif ($filename =~ m/\.jpe?g$/i) {
1801 return 'image/jpeg';
1802 } else {
1803 return 'application/octet-stream';
1804 }
1805}
1806
1807## ======================================================================
1808## functions printing HTML: header, footer, error page
1809
1810sub git_header_html {
1811 my $status = shift || "200 OK";
1812 my $expires = shift;
1813
1814 my $title = "$site_name";
1815 if (defined $project) {
1816 $title .= " - " . decode_utf8($project);
1817 if (defined $action) {
1818 $title .= "/$action";
1819 if (defined $file_name) {
1820 $title .= " - " . esc_path($file_name);
1821 if ($action eq "tree" && $file_name !~ m|/$|) {
1822 $title .= "/";
1823 }
1824 }
1825 }
1826 }
1827 my $content_type;
1828 # require explicit support from the UA if we are to send the page as
1829 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1830 # we have to do this because MSIE sometimes globs '*/*', pretending to
1831 # support xhtml+xml but choking when it gets what it asked for.
1832 if (defined $cgi->http('HTTP_ACCEPT') &&
1833 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1834 $cgi->Accept('application/xhtml+xml') != 0) {
1835 $content_type = 'application/xhtml+xml';
1836 } else {
1837 $content_type = 'text/html';
1838 }
1839 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1840 -status=> $status, -expires => $expires);
1841 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
1842 print <<EOF;
1843<?xml version="1.0" encoding="utf-8"?>
1844<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1845<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1846<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1847<!-- git core binaries version $git_version -->
1848<head>
1849<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1850<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
1851<meta name="robots" content="index, nofollow"/>
1852<title>$title</title>
1853EOF
1854# print out each stylesheet that exist
1855 if (defined $stylesheet) {
1856#provides backwards capability for those people who define style sheet in a config file
1857 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1858 } else {
1859 foreach my $stylesheet (@stylesheets) {
1860 next unless $stylesheet;
1861 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1862 }
1863 }
1864 if (defined $project) {
1865 printf('<link rel="alternate" title="%s log RSS feed" '.
1866 'href="%s" type="application/rss+xml" />'."\n",
1867 esc_param($project), href(action=>"rss"));
1868 printf('<link rel="alternate" title="%s log Atom feed" '.
1869 'href="%s" type="application/atom+xml" />'."\n",
1870 esc_param($project), href(action=>"atom"));
1871 } else {
1872 printf('<link rel="alternate" title="%s projects list" '.
1873 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1874 $site_name, href(project=>undef, action=>"project_index"));
1875 printf('<link rel="alternate" title="%s projects feeds" '.
1876 'href="%s" type="text/x-opml"/>'."\n",
1877 $site_name, href(project=>undef, action=>"opml"));
1878 }
1879 if (defined $favicon) {
1880 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1881 }
1882
1883 print "</head>\n" .
1884 "<body>\n";
1885
1886 if (-f $site_header) {
1887 open (my $fd, $site_header);
1888 print <$fd>;
1889 close $fd;
1890 }
1891
1892 print "<div class=\"page_header\">\n" .
1893 $cgi->a({-href => esc_url($logo_url),
1894 -title => $logo_label},
1895 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1896 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1897 if (defined $project) {
1898 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1899 if (defined $action) {
1900 print " / $action";
1901 }
1902 print "\n";
1903 }
1904 my ($have_search) = gitweb_check_feature('search');
1905 if ((defined $project) && ($have_search)) {
1906 if (!defined $searchtext) {
1907 $searchtext = "";
1908 }
1909 my $search_hash;
1910 if (defined $hash_base) {
1911 $search_hash = $hash_base;
1912 } elsif (defined $hash) {
1913 $search_hash = $hash;
1914 } else {
1915 $search_hash = "HEAD";
1916 }
1917 $cgi->param("a", "search");
1918 $cgi->param("h", $search_hash);
1919 $cgi->param("p", $project);
1920 print $cgi->startform(-method => "get", -action => $my_uri) .
1921 "<div class=\"search\">\n" .
1922 $cgi->hidden(-name => "p") . "\n" .
1923 $cgi->hidden(-name => "a") . "\n" .
1924 $cgi->hidden(-name => "h") . "\n" .
1925 $cgi->popup_menu(-name => 'st', -default => 'commit',
1926 -values => ['commit', 'author', 'committer', 'pickaxe']) .
1927 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1928 " search:\n",
1929 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1930 "</div>" .
1931 $cgi->end_form() . "\n";
1932 }
1933 print "</div>\n";
1934}
1935
1936sub git_footer_html {
1937 print "<div class=\"page_footer\">\n";
1938 if (defined $project) {
1939 my $descr = git_get_project_description($project);
1940 if (defined $descr) {
1941 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1942 }
1943 print $cgi->a({-href => href(action=>"rss"),
1944 -class => "rss_logo"}, "RSS") . " ";
1945 print $cgi->a({-href => href(action=>"atom"),
1946 -class => "rss_logo"}, "Atom") . "\n";
1947 } else {
1948 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1949 -class => "rss_logo"}, "OPML") . " ";
1950 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1951 -class => "rss_logo"}, "TXT") . "\n";
1952 }
1953 print "</div>\n" ;
1954
1955 if (-f $site_footer) {
1956 open (my $fd, $site_footer);
1957 print <$fd>;
1958 close $fd;
1959 }
1960
1961 print "</body>\n" .
1962 "</html>";
1963}
1964
1965sub die_error {
1966 my $status = shift || "403 Forbidden";
1967 my $error = shift || "Malformed query, file missing or permission denied";
1968
1969 git_header_html($status);
1970 print <<EOF;
1971<div class="page_body">
1972<br /><br />
1973$status - $error
1974<br />
1975</div>
1976EOF
1977 git_footer_html();
1978 exit;
1979}
1980
1981## ----------------------------------------------------------------------
1982## functions printing or outputting HTML: navigation
1983
1984sub git_print_page_nav {
1985 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1986 $extra = '' if !defined $extra; # pager or formats
1987
1988 my @navs = qw(summary shortlog log commit commitdiff tree);
1989 if ($suppress) {
1990 @navs = grep { $_ ne $suppress } @navs;
1991 }
1992
1993 my %arg = map { $_ => {action=>$_} } @navs;
1994 if (defined $head) {
1995 for (qw(commit commitdiff)) {
1996 $arg{$_}{'hash'} = $head;
1997 }
1998 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1999 for (qw(shortlog log)) {
2000 $arg{$_}{'hash'} = $head;
2001 }
2002 }
2003 }
2004 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2005 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2006
2007 print "<div class=\"page_nav\">\n" .
2008 (join " | ",
2009 map { $_ eq $current ?
2010 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2011 } @navs);
2012 print "<br/>\n$extra<br/>\n" .
2013 "</div>\n";
2014}
2015
2016sub format_paging_nav {
2017 my ($action, $hash, $head, $page, $nrevs) = @_;
2018 my $paging_nav;
2019
2020
2021 if ($hash ne $head || $page) {
2022 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2023 } else {
2024 $paging_nav .= "HEAD";
2025 }
2026
2027 if ($page > 0) {
2028 $paging_nav .= " ⋅ " .
2029 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2030 -accesskey => "p", -title => "Alt-p"}, "prev");
2031 } else {
2032 $paging_nav .= " ⋅ prev";
2033 }
2034
2035 if ($nrevs >= (100 * ($page+1)-1)) {
2036 $paging_nav .= " ⋅ " .
2037 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2038 -accesskey => "n", -title => "Alt-n"}, "next");
2039 } else {
2040 $paging_nav .= " ⋅ next";
2041 }
2042
2043 return $paging_nav;
2044}
2045
2046## ......................................................................
2047## functions printing or outputting HTML: div
2048
2049sub git_print_header_div {
2050 my ($action, $title, $hash, $hash_base) = @_;
2051 my %args = ();
2052
2053 $args{'action'} = $action;
2054 $args{'hash'} = $hash if $hash;
2055 $args{'hash_base'} = $hash_base if $hash_base;
2056
2057 print "<div class=\"header\">\n" .
2058 $cgi->a({-href => href(%args), -class => "title"},
2059 $title ? $title : $action) .
2060 "\n</div>\n";
2061}
2062
2063#sub git_print_authorship (\%) {
2064sub git_print_authorship {
2065 my $co = shift;
2066
2067 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2068 print "<div class=\"author_date\">" .
2069 esc_html($co->{'author_name'}) .
2070 " [$ad{'rfc2822'}";
2071 if ($ad{'hour_local'} < 6) {
2072 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2073 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2074 } else {
2075 printf(" (%02d:%02d %s)",
2076 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2077 }
2078 print "]</div>\n";
2079}
2080
2081sub git_print_page_path {
2082 my $name = shift;
2083 my $type = shift;
2084 my $hb = shift;
2085
2086
2087 print "<div class=\"page_path\">";
2088 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2089 -title => 'tree root'}, decode_utf8("[$project]"));
2090 print " / ";
2091 if (defined $name) {
2092 my @dirname = split '/', $name;
2093 my $basename = pop @dirname;
2094 my $fullname = '';
2095
2096 foreach my $dir (@dirname) {
2097 $fullname .= ($fullname ? '/' : '') . $dir;
2098 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2099 hash_base=>$hb),
2100 -title => $fullname}, esc_path($dir));
2101 print " / ";
2102 }
2103 if (defined $type && $type eq 'blob') {
2104 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2105 hash_base=>$hb),
2106 -title => $name}, esc_path($basename));
2107 } elsif (defined $type && $type eq 'tree') {
2108 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2109 hash_base=>$hb),
2110 -title => $name}, esc_path($basename));
2111 print " / ";
2112 } else {
2113 print esc_path($basename);
2114 }
2115 }
2116 print "<br/></div>\n";
2117}
2118
2119# sub git_print_log (\@;%) {
2120sub git_print_log ($;%) {
2121 my $log = shift;
2122 my %opts = @_;
2123
2124 if ($opts{'-remove_title'}) {
2125 # remove title, i.e. first line of log
2126 shift @$log;
2127 }
2128 # remove leading empty lines
2129 while (defined $log->[0] && $log->[0] eq "") {
2130 shift @$log;
2131 }
2132
2133 # print log
2134 my $signoff = 0;
2135 my $empty = 0;
2136 foreach my $line (@$log) {
2137 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2138 $signoff = 1;
2139 $empty = 0;
2140 if (! $opts{'-remove_signoff'}) {
2141 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2142 next;
2143 } else {
2144 # remove signoff lines
2145 next;
2146 }
2147 } else {
2148 $signoff = 0;
2149 }
2150
2151 # print only one empty line
2152 # do not print empty line after signoff
2153 if ($line eq "") {
2154 next if ($empty || $signoff);
2155 $empty = 1;
2156 } else {
2157 $empty = 0;
2158 }
2159
2160 print format_log_line_html($line) . "<br/>\n";
2161 }
2162
2163 if ($opts{'-final_empty_line'}) {
2164 # end with single empty line
2165 print "<br/>\n" unless $empty;
2166 }
2167}
2168
2169# return link target (what link points to)
2170sub git_get_link_target {
2171 my $hash = shift;
2172 my $link_target;
2173
2174 # read link
2175 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2176 or return;
2177 {
2178 local $/;
2179 $link_target = <$fd>;
2180 }
2181 close $fd
2182 or return;
2183
2184 return $link_target;
2185}
2186
2187# given link target, and the directory (basedir) the link is in,
2188# return target of link relative to top directory (top tree);
2189# return undef if it is not possible (including absolute links).
2190sub normalize_link_target {
2191 my ($link_target, $basedir, $hash_base) = @_;
2192
2193 # we can normalize symlink target only if $hash_base is provided
2194 return unless $hash_base;
2195
2196 # absolute symlinks (beginning with '/') cannot be normalized
2197 return if (substr($link_target, 0, 1) eq '/');
2198
2199 # normalize link target to path from top (root) tree (dir)
2200 my $path;
2201 if ($basedir) {
2202 $path = $basedir . '/' . $link_target;
2203 } else {
2204 # we are in top (root) tree (dir)
2205 $path = $link_target;
2206 }
2207
2208 # remove //, /./, and /../
2209 my @path_parts;
2210 foreach my $part (split('/', $path)) {
2211 # discard '.' and ''
2212 next if (!$part || $part eq '.');
2213 # handle '..'
2214 if ($part eq '..') {
2215 if (@path_parts) {
2216 pop @path_parts;
2217 } else {
2218 # link leads outside repository (outside top dir)
2219 return;
2220 }
2221 } else {
2222 push @path_parts, $part;
2223 }
2224 }
2225 $path = join('/', @path_parts);
2226
2227 return $path;
2228}
2229
2230# print tree entry (row of git_tree), but without encompassing <tr> element
2231sub git_print_tree_entry {
2232 my ($t, $basedir, $hash_base, $have_blame) = @_;
2233
2234 my %base_key = ();
2235 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2236
2237 # The format of a table row is: mode list link. Where mode is
2238 # the mode of the entry, list is the name of the entry, an href,
2239 # and link is the action links of the entry.
2240
2241 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2242 if ($t->{'type'} eq "blob") {
2243 print "<td class=\"list\">" .
2244 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2245 file_name=>"$basedir$t->{'name'}", %base_key),
2246 -class => "list"}, esc_path($t->{'name'}));
2247 if (S_ISLNK(oct $t->{'mode'})) {
2248 my $link_target = git_get_link_target($t->{'hash'});
2249 if ($link_target) {
2250 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2251 if (defined $norm_target) {
2252 print " -> " .
2253 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2254 file_name=>$norm_target),
2255 -title => $norm_target}, esc_path($link_target));
2256 } else {
2257 print " -> " . esc_path($link_target);
2258 }
2259 }
2260 }
2261 print "</td>\n";
2262 print "<td class=\"link\">";
2263 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2264 file_name=>"$basedir$t->{'name'}", %base_key)},
2265 "blob");
2266 if ($have_blame) {
2267 print " | " .
2268 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2269 file_name=>"$basedir$t->{'name'}", %base_key)},
2270 "blame");
2271 }
2272 if (defined $hash_base) {
2273 print " | " .
2274 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2275 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2276 "history");
2277 }
2278 print " | " .
2279 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2280 file_name=>"$basedir$t->{'name'}")},
2281 "raw");
2282 print "</td>\n";
2283
2284 } elsif ($t->{'type'} eq "tree") {
2285 print "<td class=\"list\">";
2286 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2287 file_name=>"$basedir$t->{'name'}", %base_key)},
2288 esc_path($t->{'name'}));
2289 print "</td>\n";
2290 print "<td class=\"link\">";
2291 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2292 file_name=>"$basedir$t->{'name'}", %base_key)},
2293 "tree");
2294 if (defined $hash_base) {
2295 print " | " .
2296 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2297 file_name=>"$basedir$t->{'name'}")},
2298 "history");
2299 }
2300 print "</td>\n";
2301 }
2302}
2303
2304## ......................................................................
2305## functions printing large fragments of HTML
2306
2307sub fill_from_file_info {
2308 my ($diff, @parents) = @_;
2309
2310 $diff->{'from_file'} = [ ];
2311 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2312 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2313 if ($diff->{'status'}[$i] eq 'R' ||
2314 $diff->{'status'}[$i] eq 'C') {
2315 $diff->{'from_file'}[$i] =
2316 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2317 }
2318 }
2319
2320 return $diff;
2321}
2322
2323# parameters can be strings, or references to arrays of strings
2324sub from_ids_eq {
2325 my ($a, $b) = @_;
2326
2327 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2328 for (my $i = 0; $i < @$a; ++$i) {
2329 return 0 unless ($a->[$i] eq $b->[$i]);
2330 }
2331 return 1;
2332 } elsif (!ref($a) && !ref($b)) {
2333 return $a eq $b;
2334 } else {
2335 return 0;
2336 }
2337}
2338
2339
2340sub git_difftree_body {
2341 my ($difftree, $hash, @parents) = @_;
2342 my ($parent) = $parents[0];
2343 my ($have_blame) = gitweb_check_feature('blame');
2344 print "<div class=\"list_head\">\n";
2345 if ($#{$difftree} > 10) {
2346 print(($#{$difftree} + 1) . " files changed:\n");
2347 }
2348 print "</div>\n";
2349
2350 print "<table class=\"" .
2351 (@parents > 1 ? "combined " : "") .
2352 "diff_tree\">\n";
2353 my $alternate = 1;
2354 my $patchno = 0;
2355 foreach my $line (@{$difftree}) {
2356 my $diff;
2357 if (ref($line) eq "HASH") {
2358 # pre-parsed (or generated by hand)
2359 $diff = $line;
2360 } else {
2361 $diff = parse_difftree_raw_line($line);
2362 }
2363
2364 if ($alternate) {
2365 print "<tr class=\"dark\">\n";
2366 } else {
2367 print "<tr class=\"light\">\n";
2368 }
2369 $alternate ^= 1;
2370
2371 if (exists $diff->{'nparents'}) { # combined diff
2372
2373 fill_from_file_info($diff, @parents)
2374 unless exists $diff->{'from_file'};
2375
2376 if ($diff->{'to_id'} ne ('0' x 40)) {
2377 # file exists in the result (child) commit
2378 print "<td>" .
2379 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2380 file_name=>$diff->{'to_file'},
2381 hash_base=>$hash),
2382 -class => "list"}, esc_path($diff->{'to_file'})) .
2383 "</td>\n";
2384 } else {
2385 print "<td>" .
2386 esc_path($diff->{'to_file'}) .
2387 "</td>\n";
2388 }
2389
2390 if ($action eq 'commitdiff') {
2391 # link to patch
2392 $patchno++;
2393 print "<td class=\"link\">" .
2394 $cgi->a({-href => "#patch$patchno"}, "patch") .
2395 " | " .
2396 "</td>\n";
2397 }
2398
2399 my $has_history = 0;
2400 my $not_deleted = 0;
2401 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2402 my $hash_parent = $parents[$i];
2403 my $from_hash = $diff->{'from_id'}[$i];
2404 my $from_path = $diff->{'from_file'}[$i];
2405 my $status = $diff->{'status'}[$i];
2406
2407 $has_history ||= ($status ne 'A');
2408 $not_deleted ||= ($status ne 'D');
2409
2410 if ($status eq 'A') {
2411 print "<td class=\"link\" align=\"right\"> | </td>\n";
2412 } elsif ($status eq 'D') {
2413 print "<td class=\"link\">" .
2414 $cgi->a({-href => href(action=>"blob",
2415 hash_base=>$hash,
2416 hash=>$from_hash,
2417 file_name=>$from_path)},
2418 "blob" . ($i+1)) .
2419 " | </td>\n";
2420 } else {
2421 if ($diff->{'to_id'} eq $from_hash) {
2422 print "<td class=\"link nochange\">";
2423 } else {
2424 print "<td class=\"link\">";
2425 }
2426 print $cgi->a({-href => href(action=>"blobdiff",
2427 hash=>$diff->{'to_id'},
2428 hash_parent=>$from_hash,
2429 hash_base=>$hash,
2430 hash_parent_base=>$hash_parent,
2431 file_name=>$diff->{'to_file'},
2432 file_parent=>$from_path)},
2433 "diff" . ($i+1)) .
2434 " | </td>\n";
2435 }
2436 }
2437
2438 print "<td class=\"link\">";
2439 if ($not_deleted) {
2440 print $cgi->a({-href => href(action=>"blob",
2441 hash=>$diff->{'to_id'},
2442 file_name=>$diff->{'to_file'},
2443 hash_base=>$hash)},
2444 "blob");
2445 print " | " if ($has_history);
2446 }
2447 if ($has_history) {
2448 print $cgi->a({-href => href(action=>"history",
2449 file_name=>$diff->{'to_file'},
2450 hash_base=>$hash)},
2451 "history");
2452 }
2453 print "</td>\n";
2454
2455 print "</tr>\n";
2456 next; # instead of 'else' clause, to avoid extra indent
2457 }
2458 # else ordinary diff
2459
2460 my ($to_mode_oct, $to_mode_str, $to_file_type);
2461 my ($from_mode_oct, $from_mode_str, $from_file_type);
2462 if ($diff->{'to_mode'} ne ('0' x 6)) {
2463 $to_mode_oct = oct $diff->{'to_mode'};
2464 if (S_ISREG($to_mode_oct)) { # only for regular file
2465 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2466 }
2467 $to_file_type = file_type($diff->{'to_mode'});
2468 }
2469 if ($diff->{'from_mode'} ne ('0' x 6)) {
2470 $from_mode_oct = oct $diff->{'from_mode'};
2471 if (S_ISREG($to_mode_oct)) { # only for regular file
2472 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2473 }
2474 $from_file_type = file_type($diff->{'from_mode'});
2475 }
2476
2477 if ($diff->{'status'} eq "A") { # created
2478 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2479 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2480 $mode_chng .= "]</span>";
2481 print "<td>";
2482 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2483 hash_base=>$hash, file_name=>$diff->{'file'}),
2484 -class => "list"}, esc_path($diff->{'file'}));
2485 print "</td>\n";
2486 print "<td>$mode_chng</td>\n";
2487 print "<td class=\"link\">";
2488 if ($action eq 'commitdiff') {
2489 # link to patch
2490 $patchno++;
2491 print $cgi->a({-href => "#patch$patchno"}, "patch");
2492 print " | ";
2493 }
2494 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2495 hash_base=>$hash, file_name=>$diff->{'file'})},
2496 "blob");
2497 print "</td>\n";
2498
2499 } elsif ($diff->{'status'} eq "D") { # deleted
2500 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2501 print "<td>";
2502 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2503 hash_base=>$parent, file_name=>$diff->{'file'}),
2504 -class => "list"}, esc_path($diff->{'file'}));
2505 print "</td>\n";
2506 print "<td>$mode_chng</td>\n";
2507 print "<td class=\"link\">";
2508 if ($action eq 'commitdiff') {
2509 # link to patch
2510 $patchno++;
2511 print $cgi->a({-href => "#patch$patchno"}, "patch");
2512 print " | ";
2513 }
2514 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2515 hash_base=>$parent, file_name=>$diff->{'file'})},
2516 "blob") . " | ";
2517 if ($have_blame) {
2518 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2519 file_name=>$diff->{'file'})},
2520 "blame") . " | ";
2521 }
2522 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2523 file_name=>$diff->{'file'})},
2524 "history");
2525 print "</td>\n";
2526
2527 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
2528 my $mode_chnge = "";
2529 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2530 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2531 if ($from_file_type ne $to_file_type) {
2532 $mode_chnge .= " from $from_file_type to $to_file_type";
2533 }
2534 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2535 if ($from_mode_str && $to_mode_str) {
2536 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2537 } elsif ($to_mode_str) {
2538 $mode_chnge .= " mode: $to_mode_str";
2539 }
2540 }
2541 $mode_chnge .= "]</span>\n";
2542 }
2543 print "<td>";
2544 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2545 hash_base=>$hash, file_name=>$diff->{'file'}),
2546 -class => "list"}, esc_path($diff->{'file'}));
2547 print "</td>\n";
2548 print "<td>$mode_chnge</td>\n";
2549 print "<td class=\"link\">";
2550 if ($action eq 'commitdiff') {
2551 # link to patch
2552 $patchno++;
2553 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2554 " | ";
2555 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2556 # "commit" view and modified file (not onlu mode changed)
2557 print $cgi->a({-href => href(action=>"blobdiff",
2558 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2559 hash_base=>$hash, hash_parent_base=>$parent,
2560 file_name=>$diff->{'file'})},
2561 "diff") .
2562 " | ";
2563 }
2564 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2565 hash_base=>$hash, file_name=>$diff->{'file'})},
2566 "blob") . " | ";
2567 if ($have_blame) {
2568 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2569 file_name=>$diff->{'file'})},
2570 "blame") . " | ";
2571 }
2572 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2573 file_name=>$diff->{'file'})},
2574 "history");
2575 print "</td>\n";
2576
2577 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
2578 my %status_name = ('R' => 'moved', 'C' => 'copied');
2579 my $nstatus = $status_name{$diff->{'status'}};
2580 my $mode_chng = "";
2581 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2582 # mode also for directories, so we cannot use $to_mode_str
2583 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2584 }
2585 print "<td>" .
2586 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2587 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
2588 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
2589 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2590 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2591 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
2592 -class => "list"}, esc_path($diff->{'from_file'})) .
2593 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2594 "<td class=\"link\">";
2595 if ($action eq 'commitdiff') {
2596 # link to patch
2597 $patchno++;
2598 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2599 " | ";
2600 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2601 # "commit" view and modified file (not only pure rename or copy)
2602 print $cgi->a({-href => href(action=>"blobdiff",
2603 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2604 hash_base=>$hash, hash_parent_base=>$parent,
2605 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
2606 "diff") .
2607 " | ";
2608 }
2609 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2610 hash_base=>$parent, file_name=>$diff->{'to_file'})},
2611 "blob") . " | ";
2612 if ($have_blame) {
2613 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2614 file_name=>$diff->{'to_file'})},
2615 "blame") . " | ";
2616 }
2617 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2618 file_name=>$diff->{'to_file'})},
2619 "history");
2620 print "</td>\n";
2621
2622 } # we should not encounter Unmerged (U) or Unknown (X) status
2623 print "</tr>\n";
2624 }
2625 print "</table>\n";
2626}
2627
2628sub git_patchset_body {
2629 my ($fd, $difftree, $hash, @hash_parents) = @_;
2630 my ($hash_parent) = $hash_parents[0];
2631
2632 my $patch_idx = 0;
2633 my $patch_number = 0;
2634 my $patch_line;
2635 my $diffinfo;
2636 my (%from, %to);
2637
2638 print "<div class=\"patchset\">\n";
2639
2640 # skip to first patch
2641 while ($patch_line = <$fd>) {
2642 chomp $patch_line;
2643
2644 last if ($patch_line =~ m/^diff /);
2645 }
2646
2647 PATCH:
2648 while ($patch_line) {
2649 my @diff_header;
2650 my ($from_id, $to_id);
2651
2652 # git diff header
2653 #assert($patch_line =~ m/^diff /) if DEBUG;
2654 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2655 $patch_number++;
2656 push @diff_header, $patch_line;
2657
2658 # extended diff header
2659 EXTENDED_HEADER:
2660 while ($patch_line = <$fd>) {
2661 chomp $patch_line;
2662
2663 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
2664
2665 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2666 $from_id = $1;
2667 $to_id = $2;
2668 } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2669 $from_id = [ split(',', $1) ];
2670 $to_id = $2;
2671 }
2672
2673 push @diff_header, $patch_line;
2674 }
2675 my $last_patch_line = $patch_line;
2676
2677 # check if current patch belong to current raw line
2678 # and parse raw git-diff line if needed
2679 if (defined $diffinfo &&
2680 defined $from_id && defined $to_id &&
2681 from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
2682 $diffinfo->{'to_id'} eq $to_id) {
2683 # this is continuation of a split patch
2684 print "<div class=\"patch cont\">\n";
2685 } else {
2686 # advance raw git-diff output if needed
2687 $patch_idx++ if defined $diffinfo;
2688
2689 # read and prepare patch information
2690 if (ref($difftree->[$patch_idx]) eq "HASH") {
2691 # pre-parsed (or generated by hand)
2692 $diffinfo = $difftree->[$patch_idx];
2693 } else {
2694 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2695 }
2696 if ($diffinfo->{'nparents'}) {
2697 # combined diff
2698 $from{'file'} = [];
2699 $from{'href'} = [];
2700 fill_from_file_info($diffinfo, @hash_parents)
2701 unless exists $diffinfo->{'from_file'};
2702 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2703 $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2704 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2705 $from{'href'}[$i] = href(action=>"blob",
2706 hash_base=>$hash_parents[$i],
2707 hash=>$diffinfo->{'from_id'}[$i],
2708 file_name=>$from{'file'}[$i]);
2709 } else {
2710 $from{'href'}[$i] = undef;
2711 }
2712 }
2713 } else {
2714 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2715 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2716 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2717 hash=>$diffinfo->{'from_id'},
2718 file_name=>$from{'file'});
2719 } else {
2720 delete $from{'href'};
2721 }
2722 }
2723 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2724 if ($diffinfo->{'status'} ne "D") { # not deleted file
2725 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2726 hash=>$diffinfo->{'to_id'},
2727 file_name=>$to{'file'});
2728 } else {
2729 delete $to{'href'};
2730 }
2731 # this is first patch for raw difftree line with $patch_idx index
2732 # we index @$difftree array from 0, but number patches from 1
2733 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2734 }
2735
2736 # print "git diff" header
2737 $patch_line = shift @diff_header;
2738 if ($diffinfo->{'nparents'}) {
2739
2740 # combined diff
2741 $patch_line =~ s!^(diff (.*?) )"?.*$!$1!;
2742 if ($to{'href'}) {
2743 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2744 esc_path($to{'file'}));
2745 } else { # file was deleted
2746 $patch_line .= esc_path($to{'file'});
2747 }
2748
2749 } else {
2750
2751 $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2752 if ($from{'href'}) {
2753 $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2754 'a/' . esc_path($from{'file'}));
2755 } else { # file was added
2756 $patch_line .= 'a/' . esc_path($from{'file'});
2757 }
2758 $patch_line .= ' ';
2759 if ($to{'href'}) {
2760 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2761 'b/' . esc_path($to{'file'}));
2762 } else { # file was deleted
2763 $patch_line .= 'b/' . esc_path($to{'file'});
2764 }
2765
2766 }
2767 print "<div class=\"diff header\">$patch_line</div>\n";
2768
2769 # print extended diff header
2770 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
2771 EXTENDED_HEADER:
2772 foreach $patch_line (@diff_header) {
2773 # match <path>
2774 if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2775 $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2776 esc_path($from{'file'}));
2777 }
2778 if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2779 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"path"},
2780 esc_path($to{'file'}));
2781 }
2782 # match single <mode>
2783 if ($patch_line =~ m/\s(\d{6})$/) {
2784 $patch_line .= '<span class="info"> (' .
2785 file_type_long($1) .
2786 ')</span>';
2787 }
2788 # match <hash>
2789 if ($patch_line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2790 # can match only for combined diff
2791 $patch_line = 'index ';
2792 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2793 if ($from{'href'}[$i]) {
2794 $patch_line .= $cgi->a({-href=>$from{'href'}[$i],
2795 -class=>"hash"},
2796 substr($diffinfo->{'from_id'}[$i],0,7));
2797 } else {
2798 $patch_line .= '0' x 7;
2799 }
2800 # separator
2801 $patch_line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2802 }
2803 $patch_line .= '..';
2804 if ($to{'href'}) {
2805 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2806 substr($diffinfo->{'to_id'},0,7));
2807 } else {
2808 $patch_line .= '0' x 7;
2809 }
2810
2811 } elsif ($patch_line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2812 # can match only for ordinary diff
2813 my ($from_link, $to_link);
2814 if ($from{'href'}) {
2815 $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2816 substr($diffinfo->{'from_id'},0,7));
2817 } else {
2818 $from_link = '0' x 7;
2819 }
2820 if ($to{'href'}) {
2821 $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2822 substr($diffinfo->{'to_id'},0,7));
2823 } else {
2824 $to_link = '0' x 7;
2825 }
2826 #affirm {
2827 # my ($from_hash, $to_hash) =
2828 # ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/);
2829 # my ($from_id, $to_id) =
2830 # ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2831 # ($from_hash eq $from_id) && ($to_hash eq $to_id);
2832 #} if DEBUG;
2833 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2834 $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2835 }
2836 print $patch_line . "<br/>\n";
2837 }
2838 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
2839
2840 # from-file/to-file diff header
2841 $patch_line = $last_patch_line;
2842 if (! $patch_line) {
2843 print "</div>\n"; # class="patch"
2844 last PATCH;
2845 }
2846 next PATCH if ($patch_line =~ m/^diff /);
2847 #assert($patch_line =~ m/^---/) if DEBUG;
2848 if (!$diffinfo->{'nparents'} && # not from-file line for combined diff
2849 $from{'href'} && $patch_line =~ m!^--- "?a/!) {
2850 $patch_line = '--- a/' .
2851 $cgi->a({-href=>$from{'href'}, -class=>"path"},
2852 esc_path($from{'file'}));
2853 }
2854 print "<div class=\"diff from_file\">$patch_line</div>\n";
2855
2856 $patch_line = <$fd>;
2857 chomp $patch_line;
2858
2859 #assert($patch_line =~ m/^+++/) if DEBUG;
2860 if ($to{'href'} && $patch_line =~ m!^\+\+\+ "?b/!) {
2861 $patch_line = '+++ b/' .
2862 $cgi->a({-href=>$to{'href'}, -class=>"path"},
2863 esc_path($to{'file'}));
2864 }
2865 print "<div class=\"diff to_file\">$patch_line</div>\n";
2866
2867 # the patch itself
2868 LINE:
2869 while ($patch_line = <$fd>) {
2870 chomp $patch_line;
2871
2872 next PATCH if ($patch_line =~ m/^diff /);
2873
2874 print format_diff_line($patch_line, \%from, \%to);
2875 }
2876
2877 } continue {
2878 print "</div>\n"; # class="patch"
2879 }
2880 print "<div class=\"diff nodifferences\">No differences found</div>\n" if (!$patch_number);
2881
2882 print "</div>\n"; # class="patchset"
2883}
2884
2885# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2886
2887sub git_project_list_body {
2888 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2889
2890 my ($check_forks) = gitweb_check_feature('forks');
2891
2892 my @projects;
2893 foreach my $pr (@$projlist) {
2894 my (@aa) = git_get_last_activity($pr->{'path'});
2895 unless (@aa) {
2896 next;
2897 }
2898 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2899 if (!defined $pr->{'descr'}) {
2900 my $descr = git_get_project_description($pr->{'path'}) || "";
2901 $pr->{'descr_long'} = decode_utf8($descr);
2902 $pr->{'descr'} = chop_str($descr, 25, 5);
2903 }
2904 if (!defined $pr->{'owner'}) {
2905 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2906 }
2907 if ($check_forks) {
2908 my $pname = $pr->{'path'};
2909 if (($pname =~ s/\.git$//) &&
2910 ($pname !~ /\/$/) &&
2911 (-d "$projectroot/$pname")) {
2912 $pr->{'forks'} = "-d $projectroot/$pname";
2913 }
2914 else {
2915 $pr->{'forks'} = 0;
2916 }
2917 }
2918 push @projects, $pr;
2919 }
2920
2921 $order ||= $default_projects_order;
2922 $from = 0 unless defined $from;
2923 $to = $#projects if (!defined $to || $#projects < $to);
2924
2925 print "<table class=\"project_list\">\n";
2926 unless ($no_header) {
2927 print "<tr>\n";
2928 if ($check_forks) {
2929 print "<th></th>\n";
2930 }
2931 if ($order eq "project") {
2932 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2933 print "<th>Project</th>\n";
2934 } else {
2935 print "<th>" .
2936 $cgi->a({-href => href(project=>undef, order=>'project'),
2937 -class => "header"}, "Project") .
2938 "</th>\n";
2939 }
2940 if ($order eq "descr") {
2941 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2942 print "<th>Description</th>\n";
2943 } else {
2944 print "<th>" .
2945 $cgi->a({-href => href(project=>undef, order=>'descr'),
2946 -class => "header"}, "Description") .
2947 "</th>\n";
2948 }
2949 if ($order eq "owner") {
2950 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2951 print "<th>Owner</th>\n";
2952 } else {
2953 print "<th>" .
2954 $cgi->a({-href => href(project=>undef, order=>'owner'),
2955 -class => "header"}, "Owner") .
2956 "</th>\n";
2957 }
2958 if ($order eq "age") {
2959 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2960 print "<th>Last Change</th>\n";
2961 } else {
2962 print "<th>" .
2963 $cgi->a({-href => href(project=>undef, order=>'age'),
2964 -class => "header"}, "Last Change") .
2965 "</th>\n";
2966 }
2967 print "<th></th>\n" .
2968 "</tr>\n";
2969 }
2970 my $alternate = 1;
2971 for (my $i = $from; $i <= $to; $i++) {
2972 my $pr = $projects[$i];
2973 if ($alternate) {
2974 print "<tr class=\"dark\">\n";
2975 } else {
2976 print "<tr class=\"light\">\n";
2977 }
2978 $alternate ^= 1;
2979 if ($check_forks) {
2980 print "<td>";
2981 if ($pr->{'forks'}) {
2982 print "<!-- $pr->{'forks'} -->\n";
2983 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2984 }
2985 print "</td>\n";
2986 }
2987 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2988 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2989 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2990 -class => "list", -title => $pr->{'descr_long'}},
2991 esc_html($pr->{'descr'})) . "</td>\n" .
2992 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2993 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2994 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
2995 "<td class=\"link\">" .
2996 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2997 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2998 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2999 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3000 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3001 "</td>\n" .
3002 "</tr>\n";
3003 }
3004 if (defined $extra) {
3005 print "<tr>\n";
3006 if ($check_forks) {
3007 print "<td></td>\n";
3008 }
3009 print "<td colspan=\"5\">$extra</td>\n" .
3010 "</tr>\n";
3011 }
3012 print "</table>\n";
3013}
3014
3015sub git_shortlog_body {
3016 # uses global variable $project
3017 my ($commitlist, $from, $to, $refs, $extra) = @_;
3018
3019 my $have_snapshot = gitweb_have_snapshot();
3020
3021 $from = 0 unless defined $from;
3022 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3023
3024 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3025 my $alternate = 1;
3026 for (my $i = $from; $i <= $to; $i++) {
3027 my %co = %{$commitlist->[$i]};
3028 my $commit = $co{'id'};
3029 my $ref = format_ref_marker($refs, $commit);
3030 if ($alternate) {
3031 print "<tr class=\"dark\">\n";
3032 } else {
3033 print "<tr class=\"light\">\n";
3034 }
3035 $alternate ^= 1;
3036 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3037 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3038 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3039 "<td>";
3040 print format_subject_html($co{'title'}, $co{'title_short'},
3041 href(action=>"commit", hash=>$commit), $ref);
3042 print "</td>\n" .
3043 "<td class=\"link\">" .
3044 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3045 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3046 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3047 if ($have_snapshot) {
3048 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
3049 }
3050 print "</td>\n" .
3051 "</tr>\n";
3052 }
3053 if (defined $extra) {
3054 print "<tr>\n" .
3055 "<td colspan=\"4\">$extra</td>\n" .
3056 "</tr>\n";
3057 }
3058 print "</table>\n";
3059}
3060
3061sub git_history_body {
3062 # Warning: assumes constant type (blob or tree) during history
3063 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3064
3065 $from = 0 unless defined $from;
3066 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3067
3068 print "<table class=\"history\" cellspacing=\"0\">\n";
3069 my $alternate = 1;
3070 for (my $i = $from; $i <= $to; $i++) {
3071 my %co = %{$commitlist->[$i]};
3072 if (!%co) {
3073 next;
3074 }
3075 my $commit = $co{'id'};
3076
3077 my $ref = format_ref_marker($refs, $commit);
3078
3079 if ($alternate) {
3080 print "<tr class=\"dark\">\n";
3081 } else {
3082 print "<tr class=\"light\">\n";
3083 }
3084 $alternate ^= 1;
3085 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3086 # shortlog uses chop_str($co{'author_name'}, 10)
3087 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3088 "<td>";
3089 # originally git_history used chop_str($co{'title'}, 50)
3090 print format_subject_html($co{'title'}, $co{'title_short'},
3091 href(action=>"commit", hash=>$commit), $ref);
3092 print "</td>\n" .
3093 "<td class=\"link\">" .
3094 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3095 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3096
3097 if ($ftype eq 'blob') {
3098 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3099 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3100 if (defined $blob_current && defined $blob_parent &&
3101 $blob_current ne $blob_parent) {
3102 print " | " .
3103 $cgi->a({-href => href(action=>"blobdiff",
3104 hash=>$blob_current, hash_parent=>$blob_parent,
3105 hash_base=>$hash_base, hash_parent_base=>$commit,
3106 file_name=>$file_name)},
3107 "diff to current");
3108 }
3109 }
3110 print "</td>\n" .
3111 "</tr>\n";
3112 }
3113 if (defined $extra) {
3114 print "<tr>\n" .
3115 "<td colspan=\"4\">$extra</td>\n" .
3116 "</tr>\n";
3117 }
3118 print "</table>\n";
3119}
3120
3121sub git_tags_body {
3122 # uses global variable $project
3123 my ($taglist, $from, $to, $extra) = @_;
3124 $from = 0 unless defined $from;
3125 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3126
3127 print "<table class=\"tags\" cellspacing=\"0\">\n";
3128 my $alternate = 1;
3129 for (my $i = $from; $i <= $to; $i++) {
3130 my $entry = $taglist->[$i];
3131 my %tag = %$entry;
3132 my $comment = $tag{'subject'};
3133 my $comment_short;
3134 if (defined $comment) {
3135 $comment_short = chop_str($comment, 30, 5);
3136 }
3137 if ($alternate) {
3138 print "<tr class=\"dark\">\n";
3139 } else {
3140 print "<tr class=\"light\">\n";
3141 }
3142 $alternate ^= 1;
3143 if (defined $tag{'age'}) {
3144 print "<td><i>$tag{'age'}</i></td>\n";
3145 } else {
3146 print "<td></td>\n";
3147 }
3148 print "<td>" .
3149 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3150 -class => "list name"}, esc_html($tag{'name'})) .
3151 "</td>\n" .
3152 "<td>";
3153 if (defined $comment) {
3154 print format_subject_html($comment, $comment_short,
3155 href(action=>"tag", hash=>$tag{'id'}));
3156 }
3157 print "</td>\n" .
3158 "<td class=\"selflink\">";
3159 if ($tag{'type'} eq "tag") {
3160 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3161 } else {
3162 print " ";
3163 }
3164 print "</td>\n" .
3165 "<td class=\"link\">" . " | " .
3166 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3167 if ($tag{'reftype'} eq "commit") {
3168 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3169 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3170 } elsif ($tag{'reftype'} eq "blob") {
3171 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3172 }
3173 print "</td>\n" .
3174 "</tr>";
3175 }
3176 if (defined $extra) {
3177 print "<tr>\n" .
3178 "<td colspan=\"5\">$extra</td>\n" .
3179 "</tr>\n";
3180 }
3181 print "</table>\n";
3182}
3183
3184sub git_heads_body {
3185 # uses global variable $project
3186 my ($headlist, $head, $from, $to, $extra) = @_;
3187 $from = 0 unless defined $from;
3188 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3189
3190 print "<table class=\"heads\" cellspacing=\"0\">\n";
3191 my $alternate = 1;
3192 for (my $i = $from; $i <= $to; $i++) {
3193 my $entry = $headlist->[$i];
3194 my %ref = %$entry;
3195 my $curr = $ref{'id'} eq $head;
3196 if ($alternate) {
3197 print "<tr class=\"dark\">\n";
3198 } else {
3199 print "<tr class=\"light\">\n";
3200 }
3201 $alternate ^= 1;
3202 print "<td><i>$ref{'age'}</i></td>\n" .
3203 ($curr ? "<td class=\"current_head\">" : "<td>") .
3204 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3205 -class => "list name"},esc_html($ref{'name'})) .
3206 "</td>\n" .
3207 "<td class=\"link\">" .
3208 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3209 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3210 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3211 "</td>\n" .
3212 "</tr>";
3213 }
3214 if (defined $extra) {
3215 print "<tr>\n" .
3216 "<td colspan=\"3\">$extra</td>\n" .
3217 "</tr>\n";
3218 }
3219 print "</table>\n";
3220}
3221
3222sub git_search_grep_body {
3223 my ($commitlist, $from, $to, $extra) = @_;
3224 $from = 0 unless defined $from;
3225 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3226
3227 print "<table class=\"grep\" cellspacing=\"0\">\n";
3228 my $alternate = 1;
3229 for (my $i = $from; $i <= $to; $i++) {
3230 my %co = %{$commitlist->[$i]};
3231 if (!%co) {
3232 next;
3233 }
3234 my $commit = $co{'id'};
3235 if ($alternate) {
3236 print "<tr class=\"dark\">\n";
3237 } else {
3238 print "<tr class=\"light\">\n";
3239 }
3240 $alternate ^= 1;
3241 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3242 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3243 "<td>" .
3244 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3245 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3246 my $comment = $co{'comment'};
3247 foreach my $line (@$comment) {
3248 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3249 my $lead = esc_html($1) || "";
3250 $lead = chop_str($lead, 30, 10);
3251 my $match = esc_html($2) || "";
3252 my $trail = esc_html($3) || "";
3253 $trail = chop_str($trail, 30, 10);
3254 my $text = "$lead<span class=\"match\">$match</span>$trail";
3255 print chop_str($text, 80, 5) . "<br/>\n";
3256 }
3257 }
3258 print "</td>\n" .
3259 "<td class=\"link\">" .
3260 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3261 " | " .
3262 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3263 print "</td>\n" .
3264 "</tr>\n";
3265 }
3266 if (defined $extra) {
3267 print "<tr>\n" .
3268 "<td colspan=\"3\">$extra</td>\n" .
3269 "</tr>\n";
3270 }
3271 print "</table>\n";
3272}
3273
3274## ======================================================================
3275## ======================================================================
3276## actions
3277
3278sub git_project_list {
3279 my $order = $cgi->param('o');
3280 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3281 die_error(undef, "Unknown order parameter");
3282 }
3283
3284 my @list = git_get_projects_list();
3285 if (!@list) {
3286 die_error(undef, "No projects found");
3287 }
3288
3289 git_header_html();
3290 if (-f $home_text) {
3291 print "<div class=\"index_include\">\n";
3292 open (my $fd, $home_text);
3293 print <$fd>;
3294 close $fd;
3295 print "</div>\n";
3296 }
3297 git_project_list_body(\@list, $order);
3298 git_footer_html();
3299}
3300
3301sub git_forks {
3302 my $order = $cgi->param('o');
3303 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3304 die_error(undef, "Unknown order parameter");
3305 }
3306
3307 my @list = git_get_projects_list($project);
3308 if (!@list) {
3309 die_error(undef, "No forks found");
3310 }
3311
3312 git_header_html();
3313 git_print_page_nav('','');
3314 git_print_header_div('summary', "$project forks");
3315 git_project_list_body(\@list, $order);
3316 git_footer_html();
3317}
3318
3319sub git_project_index {
3320 my @projects = git_get_projects_list($project);
3321
3322 print $cgi->header(
3323 -type => 'text/plain',
3324 -charset => 'utf-8',
3325 -content_disposition => 'inline; filename="index.aux"');
3326
3327 foreach my $pr (@projects) {
3328 if (!exists $pr->{'owner'}) {
3329 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}");
3330 }
3331
3332 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3333 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3334 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3335 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3336 $path =~ s/ /\+/g;
3337 $owner =~ s/ /\+/g;
3338
3339 print "$path $owner\n";
3340 }
3341}
3342
3343sub git_summary {
3344 my $descr = git_get_project_description($project) || "none";
3345 my %co = parse_commit("HEAD");
3346 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3347 my $head = $co{'id'};
3348
3349 my $owner = git_get_project_owner($project);
3350
3351 my $refs = git_get_references();
3352 # These get_*_list functions return one more to allow us to see if
3353 # there are more ...
3354 my @taglist = git_get_tags_list(16);
3355 my @headlist = git_get_heads_list(16);
3356 my @forklist;
3357 my ($check_forks) = gitweb_check_feature('forks');
3358
3359 if ($check_forks) {
3360 @forklist = git_get_projects_list($project);
3361 }
3362
3363 git_header_html();
3364 git_print_page_nav('summary','', $head);
3365
3366 print "<div class=\"title\"> </div>\n";
3367 print "<table cellspacing=\"0\">\n" .
3368 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3369 "<tr><td>owner</td><td>$owner</td></tr>\n";
3370 if (defined $cd{'rfc2822'}) {
3371 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3372 }
3373
3374 # use per project git URL list in $projectroot/$project/cloneurl
3375 # or make project git URL from git base URL and project name
3376 my $url_tag = "URL";
3377 my @url_list = git_get_project_url_list($project);
3378 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3379 foreach my $git_url (@url_list) {
3380 next unless $git_url;
3381 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3382 $url_tag = "";
3383 }
3384 print "</table>\n";
3385
3386 if (-s "$projectroot/$project/README.html") {
3387 if (open my $fd, "$projectroot/$project/README.html") {
3388 print "<div class=\"title\">readme</div>\n";
3389 print $_ while (<$fd>);
3390 close $fd;
3391 }
3392 }
3393
3394 # we need to request one more than 16 (0..15) to check if
3395 # those 16 are all
3396 my @commitlist = $head ? parse_commits($head, 17) : ();
3397 if (@commitlist) {
3398 git_print_header_div('shortlog');
3399 git_shortlog_body(\@commitlist, 0, 15, $refs,
3400 $#commitlist <= 15 ? undef :
3401 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3402 }
3403
3404 if (@taglist) {
3405 git_print_header_div('tags');
3406 git_tags_body(\@taglist, 0, 15,
3407 $#taglist <= 15 ? undef :
3408 $cgi->a({-href => href(action=>"tags")}, "..."));
3409 }
3410
3411 if (@headlist) {
3412 git_print_header_div('heads');
3413 git_heads_body(\@headlist, $head, 0, 15,
3414 $#headlist <= 15 ? undef :
3415 $cgi->a({-href => href(action=>"heads")}, "..."));
3416 }
3417
3418 if (@forklist) {
3419 git_print_header_div('forks');
3420 git_project_list_body(\@forklist, undef, 0, 15,
3421 $#forklist <= 15 ? undef :
3422 $cgi->a({-href => href(action=>"forks")}, "..."),
3423 'noheader');
3424 }
3425
3426 git_footer_html();
3427}
3428
3429sub git_tag {
3430 my $head = git_get_head_hash($project);
3431 git_header_html();
3432 git_print_page_nav('','', $head,undef,$head);
3433 my %tag = parse_tag($hash);
3434
3435 if (! %tag) {
3436 die_error(undef, "Unknown tag object");
3437 }
3438
3439 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3440 print "<div class=\"title_text\">\n" .
3441 "<table cellspacing=\"0\">\n" .
3442 "<tr>\n" .
3443 "<td>object</td>\n" .
3444 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3445 $tag{'object'}) . "</td>\n" .
3446 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3447 $tag{'type'}) . "</td>\n" .
3448 "</tr>\n";
3449 if (defined($tag{'author'})) {
3450 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3451 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3452 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3453 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3454 "</td></tr>\n";
3455 }
3456 print "</table>\n\n" .
3457 "</div>\n";
3458 print "<div class=\"page_body\">";
3459 my $comment = $tag{'comment'};
3460 foreach my $line (@$comment) {
3461 chomp $line;
3462 print esc_html($line, -nbsp=>1) . "<br/>\n";
3463 }
3464 print "</div>\n";
3465 git_footer_html();
3466}
3467
3468sub git_blame2 {
3469 my $fd;
3470 my $ftype;
3471
3472 my ($have_blame) = gitweb_check_feature('blame');
3473 if (!$have_blame) {
3474 die_error('403 Permission denied', "Permission denied");
3475 }
3476 die_error('404 Not Found', "File name not defined") if (!$file_name);
3477 $hash_base ||= git_get_head_hash($project);
3478 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3479 my %co = parse_commit($hash_base)
3480 or die_error(undef, "Reading commit failed");
3481 if (!defined $hash) {
3482 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3483 or die_error(undef, "Error looking up file");
3484 }
3485 $ftype = git_get_type($hash);
3486 if ($ftype !~ "blob") {
3487 die_error('400 Bad Request', "Object is not a blob");
3488 }
3489 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3490 $file_name, $hash_base)
3491 or die_error(undef, "Open git-blame failed");
3492 git_header_html();
3493 my $formats_nav =
3494 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3495 "blob") .
3496 " | " .
3497 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3498 "history") .
3499 " | " .
3500 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3501 "HEAD");
3502 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3503 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3504 git_print_page_path($file_name, $ftype, $hash_base);
3505 my @rev_color = (qw(light2 dark2));
3506 my $num_colors = scalar(@rev_color);
3507 my $current_color = 0;
3508 my $last_rev;
3509 print <<HTML;
3510<div class="page_body">
3511<table class="blame">
3512<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3513HTML
3514 my %metainfo = ();
3515 while (1) {
3516 $_ = <$fd>;
3517 last unless defined $_;
3518 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3519 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3520 if (!exists $metainfo{$full_rev}) {
3521 $metainfo{$full_rev} = {};
3522 }
3523 my $meta = $metainfo{$full_rev};
3524 while (<$fd>) {
3525 last if (s/^\t//);
3526 if (/^(\S+) (.*)$/) {
3527 $meta->{$1} = $2;
3528 }
3529 }
3530 my $data = $_;
3531 chomp $data;
3532 my $rev = substr($full_rev, 0, 8);
3533 my $author = $meta->{'author'};
3534 my %date = parse_date($meta->{'author-time'},
3535 $meta->{'author-tz'});
3536 my $date = $date{'iso-tz'};
3537 if ($group_size) {
3538 $current_color = ++$current_color % $num_colors;
3539 }
3540 print "<tr class=\"$rev_color[$current_color]\">\n";
3541 if ($group_size) {
3542 print "<td class=\"sha1\"";
3543 print " title=\"". esc_html($author) . ", $date\"";
3544 print " rowspan=\"$group_size\"" if ($group_size > 1);
3545 print ">";
3546 print $cgi->a({-href => href(action=>"commit",
3547 hash=>$full_rev,
3548 file_name=>$file_name)},
3549 esc_html($rev));
3550 print "</td>\n";
3551 }
3552 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3553 or die_error(undef, "Open git-rev-parse failed");
3554 my $parent_commit = <$dd>;
3555 close $dd;
3556 chomp($parent_commit);
3557 my $blamed = href(action => 'blame',
3558 file_name => $meta->{'filename'},
3559 hash_base => $parent_commit);
3560 print "<td class=\"linenr\">";
3561 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3562 -id => "l$lineno",
3563 -class => "linenr" },
3564 esc_html($lineno));
3565 print "</td>";
3566 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3567 print "</tr>\n";
3568 }
3569 print "</table>\n";
3570 print "</div>";
3571 close $fd
3572 or print "Reading blob failed\n";
3573 git_footer_html();
3574}
3575
3576sub git_blame {
3577 my $fd;
3578
3579 my ($have_blame) = gitweb_check_feature('blame');
3580 if (!$have_blame) {
3581 die_error('403 Permission denied', "Permission denied");
3582 }
3583 die_error('404 Not Found', "File name not defined") if (!$file_name);
3584 $hash_base ||= git_get_head_hash($project);
3585 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3586 my %co = parse_commit($hash_base)
3587 or die_error(undef, "Reading commit failed");
3588 if (!defined $hash) {
3589 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3590 or die_error(undef, "Error lookup file");
3591 }
3592 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3593 or die_error(undef, "Open git-annotate failed");
3594 git_header_html();
3595 my $formats_nav =
3596 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3597 "blob") .
3598 " | " .
3599 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3600 "history") .
3601 " | " .
3602 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3603 "HEAD");
3604 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3605 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3606 git_print_page_path($file_name, 'blob', $hash_base);
3607 print "<div class=\"page_body\">\n";
3608 print <<HTML;
3609<table class="blame">
3610 <tr>
3611 <th>Commit</th>
3612 <th>Age</th>
3613 <th>Author</th>
3614 <th>Line</th>
3615 <th>Data</th>
3616 </tr>
3617HTML
3618 my @line_class = (qw(light dark));
3619 my $line_class_len = scalar (@line_class);
3620 my $line_class_num = $#line_class;
3621 while (my $line = <$fd>) {
3622 my $long_rev;
3623 my $short_rev;
3624 my $author;
3625 my $time;
3626 my $lineno;
3627 my $data;
3628 my $age;
3629 my $age_str;
3630 my $age_class;
3631
3632 chomp $line;
3633 $line_class_num = ($line_class_num + 1) % $line_class_len;
3634
3635 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3636 $long_rev = $1;
3637 $author = $2;
3638 $time = $3;
3639 $lineno = $4;
3640 $data = $5;
3641 } else {
3642 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3643 next;
3644 }
3645 $short_rev = substr ($long_rev, 0, 8);
3646 $age = time () - $time;
3647 $age_str = age_string ($age);
3648 $age_str =~ s/ / /g;
3649 $age_class = age_class($age);
3650 $author = esc_html ($author);
3651 $author =~ s/ / /g;
3652
3653 $data = untabify($data);
3654 $data = esc_html ($data);
3655
3656 print <<HTML;
3657 <tr class="$line_class[$line_class_num]">
3658 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3659 <td class="$age_class">$age_str</td>
3660 <td>$author</td>
3661 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3662 <td class="pre">$data</td>
3663 </tr>
3664HTML
3665 } # while (my $line = <$fd>)
3666 print "</table>\n\n";
3667 close $fd
3668 or print "Reading blob failed.\n";
3669 print "</div>";
3670 git_footer_html();
3671}
3672
3673sub git_tags {
3674 my $head = git_get_head_hash($project);
3675 git_header_html();
3676 git_print_page_nav('','', $head,undef,$head);
3677 git_print_header_div('summary', $project);
3678
3679 my @tagslist = git_get_tags_list();
3680 if (@tagslist) {
3681 git_tags_body(\@tagslist);
3682 }
3683 git_footer_html();
3684}
3685
3686sub git_heads {
3687 my $head = git_get_head_hash($project);
3688 git_header_html();
3689 git_print_page_nav('','', $head,undef,$head);
3690 git_print_header_div('summary', $project);
3691
3692 my @headslist = git_get_heads_list();
3693 if (@headslist) {
3694 git_heads_body(\@headslist, $head);
3695 }
3696 git_footer_html();
3697}
3698
3699sub git_blob_plain {
3700 my $expires;
3701
3702 if (!defined $hash) {
3703 if (defined $file_name) {
3704 my $base = $hash_base || git_get_head_hash($project);
3705 $hash = git_get_hash_by_path($base, $file_name, "blob")
3706 or die_error(undef, "Error lookup file");
3707 } else {
3708 die_error(undef, "No file name defined");
3709 }
3710 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3711 # blobs defined by non-textual hash id's can be cached
3712 $expires = "+1d";
3713 }
3714
3715 my $type = shift;
3716 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3717 or die_error(undef, "Couldn't cat $file_name, $hash");
3718
3719 $type ||= blob_mimetype($fd, $file_name);
3720
3721 # save as filename, even when no $file_name is given
3722 my $save_as = "$hash";
3723 if (defined $file_name) {
3724 $save_as = $file_name;
3725 } elsif ($type =~ m/^text\//) {
3726 $save_as .= '.txt';
3727 }
3728
3729 print $cgi->header(
3730 -type => "$type",
3731 -expires=>$expires,
3732 -content_disposition => 'inline; filename="' . "$save_as" . '"');
3733 undef $/;
3734 binmode STDOUT, ':raw';
3735 print <$fd>;
3736 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3737 $/ = "\n";
3738 close $fd;
3739}
3740
3741sub git_blob {
3742 my $expires;
3743
3744 if (!defined $hash) {
3745 if (defined $file_name) {
3746 my $base = $hash_base || git_get_head_hash($project);
3747 $hash = git_get_hash_by_path($base, $file_name, "blob")
3748 or die_error(undef, "Error lookup file");
3749 } else {
3750 die_error(undef, "No file name defined");
3751 }
3752 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3753 # blobs defined by non-textual hash id's can be cached
3754 $expires = "+1d";
3755 }
3756
3757 my ($have_blame) = gitweb_check_feature('blame');
3758 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3759 or die_error(undef, "Couldn't cat $file_name, $hash");
3760 my $mimetype = blob_mimetype($fd, $file_name);
3761 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
3762 close $fd;
3763 return git_blob_plain($mimetype);
3764 }
3765 # we can have blame only for text/* mimetype
3766 $have_blame &&= ($mimetype =~ m!^text/!);
3767
3768 git_header_html(undef, $expires);
3769 my $formats_nav = '';
3770 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3771 if (defined $file_name) {
3772 if ($have_blame) {
3773 $formats_nav .=
3774 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3775 hash=>$hash, file_name=>$file_name)},
3776 "blame") .
3777 " | ";
3778 }
3779 $formats_nav .=
3780 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3781 hash=>$hash, file_name=>$file_name)},
3782 "history") .
3783 " | " .
3784 $cgi->a({-href => href(action=>"blob_plain",
3785 hash=>$hash, file_name=>$file_name)},
3786 "raw") .
3787 " | " .
3788 $cgi->a({-href => href(action=>"blob",
3789 hash_base=>"HEAD", file_name=>$file_name)},
3790 "HEAD");
3791 } else {
3792 $formats_nav .=
3793 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3794 }
3795 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3796 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3797 } else {
3798 print "<div class=\"page_nav\">\n" .
3799 "<br/><br/></div>\n" .
3800 "<div class=\"title\">$hash</div>\n";
3801 }
3802 git_print_page_path($file_name, "blob", $hash_base);
3803 print "<div class=\"page_body\">\n";
3804 if ($mimetype =~ m!^text/!) {
3805 my $nr;
3806 while (my $line = <$fd>) {
3807 chomp $line;
3808 $nr++;
3809 $line = untabify($line);
3810 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3811 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3812 }
3813 } elsif ($mimetype =~ m!^image/!) {
3814 print qq!<img type="$mimetype"!;
3815 if ($file_name) {
3816 print qq! alt="$file_name" title="$file_name"!;
3817 }
3818 print qq! src="! .
3819 href(action=>"blob_plain", hash=>$hash,
3820 hash_base=>$hash_base, file_name=>$file_name) .
3821 qq!" />\n!;
3822 }
3823 close $fd
3824 or print "Reading blob failed.\n";
3825 print "</div>";
3826 git_footer_html();
3827}
3828
3829sub git_tree {
3830 my $have_snapshot = gitweb_have_snapshot();
3831
3832 if (!defined $hash_base) {
3833 $hash_base = "HEAD";
3834 }
3835 if (!defined $hash) {
3836 if (defined $file_name) {
3837 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
3838 } else {
3839 $hash = $hash_base;
3840 }
3841 }
3842 $/ = "\0";
3843 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3844 or die_error(undef, "Open git-ls-tree failed");
3845 my @entries = map { chomp; $_ } <$fd>;
3846 close $fd or die_error(undef, "Reading tree failed");
3847 $/ = "\n";
3848
3849 my $refs = git_get_references();
3850 my $ref = format_ref_marker($refs, $hash_base);
3851 git_header_html();
3852 my $basedir = '';
3853 my ($have_blame) = gitweb_check_feature('blame');
3854 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3855 my @views_nav = ();
3856 if (defined $file_name) {
3857 push @views_nav,
3858 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3859 hash=>$hash, file_name=>$file_name)},
3860 "history"),
3861 $cgi->a({-href => href(action=>"tree",
3862 hash_base=>"HEAD", file_name=>$file_name)},
3863 "HEAD"),
3864 }
3865 if ($have_snapshot) {
3866 # FIXME: Should be available when we have no hash base as well.
3867 push @views_nav,
3868 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3869 "snapshot");
3870 }
3871 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3872 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3873 } else {
3874 undef $hash_base;
3875 print "<div class=\"page_nav\">\n";
3876 print "<br/><br/></div>\n";
3877 print "<div class=\"title\">$hash</div>\n";
3878 }
3879 if (defined $file_name) {
3880 $basedir = $file_name;
3881 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3882 $basedir .= '/';
3883 }
3884 }
3885 git_print_page_path($file_name, 'tree', $hash_base);
3886 print "<div class=\"page_body\">\n";
3887 print "<table cellspacing=\"0\">\n";
3888 my $alternate = 1;
3889 # '..' (top directory) link if possible
3890 if (defined $hash_base &&
3891 defined $file_name && $file_name =~ m![^/]+$!) {
3892 if ($alternate) {
3893 print "<tr class=\"dark\">\n";
3894 } else {
3895 print "<tr class=\"light\">\n";
3896 }
3897 $alternate ^= 1;
3898
3899 my $up = $file_name;
3900 $up =~ s!/?[^/]+$!!;
3901 undef $up unless $up;
3902 # based on git_print_tree_entry
3903 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3904 print '<td class="list">';
3905 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3906 file_name=>$up)},
3907 "..");
3908 print "</td>\n";
3909 print "<td class=\"link\"></td>\n";
3910
3911 print "</tr>\n";
3912 }
3913 foreach my $line (@entries) {
3914 my %t = parse_ls_tree_line($line, -z => 1);
3915
3916 if ($alternate) {
3917 print "<tr class=\"dark\">\n";
3918 } else {
3919 print "<tr class=\"light\">\n";
3920 }
3921 $alternate ^= 1;
3922
3923 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3924
3925 print "</tr>\n";
3926 }
3927 print "</table>\n" .
3928 "</div>";
3929 git_footer_html();
3930}
3931
3932sub git_snapshot {
3933 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3934 my $have_snapshot = (defined $ctype && defined $suffix);
3935 if (!$have_snapshot) {
3936 die_error('403 Permission denied', "Permission denied");
3937 }
3938
3939 if (!defined $hash) {
3940 $hash = git_get_head_hash($project);
3941 }
3942
3943 my $filename = decode_utf8(basename($project)) . "-$hash.tar.$suffix";
3944
3945 print $cgi->header(
3946 -type => "application/$ctype",
3947 -content_disposition => 'inline; filename="' . "$filename" . '"',
3948 -status => '200 OK');
3949
3950 my $git = git_cmd_str();
3951 my $name = $project;
3952 $name =~ s/\047/\047\\\047\047/g;
3953 open my $fd, "-|",
3954 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3955 or die_error(undef, "Execute git-tar-tree failed");
3956 binmode STDOUT, ':raw';
3957 print <$fd>;
3958 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3959 close $fd;
3960
3961}
3962
3963sub git_log {
3964 my $head = git_get_head_hash($project);
3965 if (!defined $hash) {
3966 $hash = $head;
3967 }
3968 if (!defined $page) {
3969 $page = 0;
3970 }
3971 my $refs = git_get_references();
3972
3973 my @commitlist = parse_commits($hash, 101, (100 * $page));
3974
3975 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
3976
3977 git_header_html();
3978 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3979
3980 if (!@commitlist) {
3981 my %co = parse_commit($hash);
3982
3983 git_print_header_div('summary', $project);
3984 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3985 }
3986 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
3987 for (my $i = 0; $i <= $to; $i++) {
3988 my %co = %{$commitlist[$i]};
3989 next if !%co;
3990 my $commit = $co{'id'};
3991 my $ref = format_ref_marker($refs, $commit);
3992 my %ad = parse_date($co{'author_epoch'});
3993 git_print_header_div('commit',
3994 "<span class=\"age\">$co{'age_string'}</span>" .
3995 esc_html($co{'title'}) . $ref,
3996 $commit);
3997 print "<div class=\"title_text\">\n" .
3998 "<div class=\"log_link\">\n" .
3999 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4000 " | " .
4001 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4002 " | " .
4003 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4004 "<br/>\n" .
4005 "</div>\n" .
4006 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4007 "</div>\n";
4008
4009 print "<div class=\"log_body\">\n";
4010 git_print_log($co{'comment'}, -final_empty_line=> 1);
4011 print "</div>\n";
4012 }
4013 if ($#commitlist >= 100) {
4014 print "<div class=\"page_nav\">\n";
4015 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4016 -accesskey => "n", -title => "Alt-n"}, "next");
4017 print "</div>\n";
4018 }
4019 git_footer_html();
4020}
4021
4022sub git_commit {
4023 $hash ||= $hash_base || "HEAD";
4024 my %co = parse_commit($hash);
4025 if (!%co) {
4026 die_error(undef, "Unknown commit object");
4027 }
4028 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4029 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4030
4031 my $parent = $co{'parent'};
4032 my $parents = $co{'parents'}; # listref
4033
4034 # we need to prepare $formats_nav before any parameter munging
4035 my $formats_nav;
4036 if (!defined $parent) {
4037 # --root commitdiff
4038 $formats_nav .= '(initial)';
4039 } elsif (@$parents == 1) {
4040 # single parent commit
4041 $formats_nav .=
4042 '(parent: ' .
4043 $cgi->a({-href => href(action=>"commit",
4044 hash=>$parent)},
4045 esc_html(substr($parent, 0, 7))) .
4046 ')';
4047 } else {
4048 # merge commit
4049 $formats_nav .=
4050 '(merge: ' .
4051 join(' ', map {
4052 $cgi->a({-href => href(action=>"commit",
4053 hash=>$_)},
4054 esc_html(substr($_, 0, 7)));
4055 } @$parents ) .
4056 ')';
4057 }
4058
4059 if (!defined $parent) {
4060 $parent = "--root";
4061 }
4062 my @difftree;
4063 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4064 @diff_opts,
4065 (@$parents <= 1 ? $parent : '-c'),
4066 $hash, "--"
4067 or die_error(undef, "Open git-diff-tree failed");
4068 @difftree = map { chomp; $_ } <$fd>;
4069 close $fd or die_error(undef, "Reading git-diff-tree failed");
4070
4071 # non-textual hash id's can be cached
4072 my $expires;
4073 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4074 $expires = "+1d";
4075 }
4076 my $refs = git_get_references();
4077 my $ref = format_ref_marker($refs, $co{'id'});
4078
4079 my $have_snapshot = gitweb_have_snapshot();
4080
4081 git_header_html(undef, $expires);
4082 git_print_page_nav('commit', '',
4083 $hash, $co{'tree'}, $hash,
4084 $formats_nav);
4085
4086 if (defined $co{'parent'}) {
4087 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4088 } else {
4089 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4090 }
4091 print "<div class=\"title_text\">\n" .
4092 "<table cellspacing=\"0\">\n";
4093 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4094 "<tr>" .
4095 "<td></td><td> $ad{'rfc2822'}";
4096 if ($ad{'hour_local'} < 6) {
4097 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4098 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4099 } else {
4100 printf(" (%02d:%02d %s)",
4101 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4102 }
4103 print "</td>" .
4104 "</tr>\n";
4105 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4106 print "<tr><td></td><td> $cd{'rfc2822'}" .
4107 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4108 "</td></tr>\n";
4109 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4110 print "<tr>" .
4111 "<td>tree</td>" .
4112 "<td class=\"sha1\">" .
4113 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4114 class => "list"}, $co{'tree'}) .
4115 "</td>" .
4116 "<td class=\"link\">" .
4117 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4118 "tree");
4119 if ($have_snapshot) {
4120 print " | " .
4121 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
4122 }
4123 print "</td>" .
4124 "</tr>\n";
4125
4126 foreach my $par (@$parents) {
4127 print "<tr>" .
4128 "<td>parent</td>" .
4129 "<td class=\"sha1\">" .
4130 $cgi->a({-href => href(action=>"commit", hash=>$par),
4131 class => "list"}, $par) .
4132 "</td>" .
4133 "<td class=\"link\">" .
4134 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4135 " | " .
4136 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4137 "</td>" .
4138 "</tr>\n";
4139 }
4140 print "</table>".
4141 "</div>\n";
4142
4143 print "<div class=\"page_body\">\n";
4144 git_print_log($co{'comment'});
4145 print "</div>\n";
4146
4147 git_difftree_body(\@difftree, $hash, @$parents);
4148
4149 git_footer_html();
4150}
4151
4152sub git_object {
4153 # object is defined by:
4154 # - hash or hash_base alone
4155 # - hash_base and file_name
4156 my $type;
4157
4158 # - hash or hash_base alone
4159 if ($hash || ($hash_base && !defined $file_name)) {
4160 my $object_id = $hash || $hash_base;
4161
4162 my $git_command = git_cmd_str();
4163 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4164 or die_error('404 Not Found', "Object does not exist");
4165 $type = <$fd>;
4166 chomp $type;
4167 close $fd
4168 or die_error('404 Not Found', "Object does not exist");
4169
4170 # - hash_base and file_name
4171 } elsif ($hash_base && defined $file_name) {
4172 $file_name =~ s,/+$,,;
4173
4174 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4175 or die_error('404 Not Found', "Base object does not exist");
4176
4177 # here errors should not hapen
4178 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4179 or die_error(undef, "Open git-ls-tree failed");
4180 my $line = <$fd>;
4181 close $fd;
4182
4183 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4184 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4185 die_error('404 Not Found', "File or directory for given base does not exist");
4186 }
4187 $type = $2;
4188 $hash = $3;
4189 } else {
4190 die_error('404 Not Found', "Not enough information to find object");
4191 }
4192
4193 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4194 hash=>$hash, hash_base=>$hash_base,
4195 file_name=>$file_name),
4196 -status => '302 Found');
4197}
4198
4199sub git_blobdiff {
4200 my $format = shift || 'html';
4201
4202 my $fd;
4203 my @difftree;
4204 my %diffinfo;
4205 my $expires;
4206
4207 # preparing $fd and %diffinfo for git_patchset_body
4208 # new style URI
4209 if (defined $hash_base && defined $hash_parent_base) {
4210 if (defined $file_name) {
4211 # read raw output
4212 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4213 $hash_parent_base, $hash_base,
4214 "--", (defined $file_parent ? $file_parent : ()), $file_name
4215 or die_error(undef, "Open git-diff-tree failed");
4216 @difftree = map { chomp; $_ } <$fd>;
4217 close $fd
4218 or die_error(undef, "Reading git-diff-tree failed");
4219 @difftree
4220 or die_error('404 Not Found', "Blob diff not found");
4221
4222 } elsif (defined $hash &&
4223 $hash =~ /[0-9a-fA-F]{40}/) {
4224 # try to find filename from $hash
4225
4226 # read filtered raw output
4227 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4228 $hash_parent_base, $hash_base, "--"
4229 or die_error(undef, "Open git-diff-tree failed");
4230 @difftree =
4231 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4232 # $hash == to_id
4233 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4234 map { chomp; $_ } <$fd>;
4235 close $fd
4236 or die_error(undef, "Reading git-diff-tree failed");
4237 @difftree
4238 or die_error('404 Not Found', "Blob diff not found");
4239
4240 } else {
4241 die_error('404 Not Found', "Missing one of the blob diff parameters");
4242 }
4243
4244 if (@difftree > 1) {
4245 die_error('404 Not Found', "Ambiguous blob diff specification");
4246 }
4247
4248 %diffinfo = parse_difftree_raw_line($difftree[0]);
4249 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4250 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4251
4252 $hash_parent ||= $diffinfo{'from_id'};
4253 $hash ||= $diffinfo{'to_id'};
4254
4255 # non-textual hash id's can be cached
4256 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4257 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4258 $expires = '+1d';
4259 }
4260
4261 # open patch output
4262 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4263 '-p', ($format eq 'html' ? "--full-index" : ()),
4264 $hash_parent_base, $hash_base,
4265 "--", (defined $file_parent ? $file_parent : ()), $file_name
4266 or die_error(undef, "Open git-diff-tree failed");
4267 }
4268
4269 # old/legacy style URI
4270 if (!%diffinfo && # if new style URI failed
4271 defined $hash && defined $hash_parent) {
4272 # fake git-diff-tree raw output
4273 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4274 $diffinfo{'from_id'} = $hash_parent;
4275 $diffinfo{'to_id'} = $hash;
4276 if (defined $file_name) {
4277 if (defined $file_parent) {
4278 $diffinfo{'status'} = '2';
4279 $diffinfo{'from_file'} = $file_parent;
4280 $diffinfo{'to_file'} = $file_name;
4281 } else { # assume not renamed
4282 $diffinfo{'status'} = '1';
4283 $diffinfo{'from_file'} = $file_name;
4284 $diffinfo{'to_file'} = $file_name;
4285 }
4286 } else { # no filename given
4287 $diffinfo{'status'} = '2';
4288 $diffinfo{'from_file'} = $hash_parent;
4289 $diffinfo{'to_file'} = $hash;
4290 }
4291
4292 # non-textual hash id's can be cached
4293 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4294 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4295 $expires = '+1d';
4296 }
4297
4298 # open patch output
4299 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4300 '-p', ($format eq 'html' ? "--full-index" : ()),
4301 $hash_parent, $hash, "--"
4302 or die_error(undef, "Open git-diff failed");
4303 } else {
4304 die_error('404 Not Found', "Missing one of the blob diff parameters")
4305 unless %diffinfo;
4306 }
4307
4308 # header
4309 if ($format eq 'html') {
4310 my $formats_nav =
4311 $cgi->a({-href => href(action=>"blobdiff_plain",
4312 hash=>$hash, hash_parent=>$hash_parent,
4313 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4314 file_name=>$file_name, file_parent=>$file_parent)},
4315 "raw");
4316 git_header_html(undef, $expires);
4317 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4318 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4319 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4320 } else {
4321 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4322 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4323 }
4324 if (defined $file_name) {
4325 git_print_page_path($file_name, "blob", $hash_base);
4326 } else {
4327 print "<div class=\"page_path\"></div>\n";
4328 }
4329
4330 } elsif ($format eq 'plain') {
4331 print $cgi->header(
4332 -type => 'text/plain',
4333 -charset => 'utf-8',
4334 -expires => $expires,
4335 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4336
4337 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4338
4339 } else {
4340 die_error(undef, "Unknown blobdiff format");
4341 }
4342
4343 # patch
4344 if ($format eq 'html') {
4345 print "<div class=\"page_body\">\n";
4346
4347 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4348 close $fd;
4349
4350 print "</div>\n"; # class="page_body"
4351 git_footer_html();
4352
4353 } else {
4354 while (my $line = <$fd>) {
4355 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4356 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4357
4358 print $line;
4359
4360 last if $line =~ m!^\+\+\+!;
4361 }
4362 local $/ = undef;
4363 print <$fd>;
4364 close $fd;
4365 }
4366}
4367
4368sub git_blobdiff_plain {
4369 git_blobdiff('plain');
4370}
4371
4372sub git_commitdiff {
4373 my $format = shift || 'html';
4374 $hash ||= $hash_base || "HEAD";
4375 my %co = parse_commit($hash);
4376 if (!%co) {
4377 die_error(undef, "Unknown commit object");
4378 }
4379
4380 # we need to prepare $formats_nav before any parameter munging
4381 my $formats_nav;
4382 if ($format eq 'html') {
4383 $formats_nav =
4384 $cgi->a({-href => href(action=>"commitdiff_plain",
4385 hash=>$hash, hash_parent=>$hash_parent)},
4386 "raw");
4387
4388 if (defined $hash_parent) {
4389 # commitdiff with two commits given
4390 my $hash_parent_short = $hash_parent;
4391 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4392 $hash_parent_short = substr($hash_parent, 0, 7);
4393 }
4394 $formats_nav .=
4395 ' (from: ' .
4396 $cgi->a({-href => href(action=>"commitdiff",
4397 hash=>$hash_parent)},
4398 esc_html($hash_parent_short)) .
4399 ')';
4400 } elsif (!$co{'parent'}) {
4401 # --root commitdiff
4402 $formats_nav .= ' (initial)';
4403 } elsif (scalar @{$co{'parents'}} == 1) {
4404 # single parent commit
4405 $formats_nav .=
4406 ' (parent: ' .
4407 $cgi->a({-href => href(action=>"commitdiff",
4408 hash=>$co{'parent'})},
4409 esc_html(substr($co{'parent'}, 0, 7))) .
4410 ')';
4411 } else {
4412 # merge commit
4413 $formats_nav .=
4414 ' (merge: ' .
4415 join(' ', map {
4416 $cgi->a({-href => href(action=>"commitdiff",
4417 hash=>$_)},
4418 esc_html(substr($_, 0, 7)));
4419 } @{$co{'parents'}} ) .
4420 ')';
4421 }
4422 }
4423
4424 my $hash_parent_param = $hash_parent;
4425 if (!defined $hash_parent) {
4426 $hash_parent_param =
4427 @{$co{'parents'}} > 1 ? '-c' : $co{'parent'} || '--root';
4428 }
4429
4430 # read commitdiff
4431 my $fd;
4432 my @difftree;
4433 if ($format eq 'html') {
4434 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4435 "--no-commit-id", "--patch-with-raw", "--full-index",
4436 $hash_parent_param, $hash, "--"
4437 or die_error(undef, "Open git-diff-tree failed");
4438
4439 while (my $line = <$fd>) {
4440 chomp $line;
4441 # empty line ends raw part of diff-tree output
4442 last unless $line;
4443 push @difftree, scalar parse_difftree_raw_line($line);
4444 }
4445
4446 } elsif ($format eq 'plain') {
4447 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4448 '-p', $hash_parent_param, $hash, "--"
4449 or die_error(undef, "Open git-diff-tree failed");
4450
4451 } else {
4452 die_error(undef, "Unknown commitdiff format");
4453 }
4454
4455 # non-textual hash id's can be cached
4456 my $expires;
4457 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4458 $expires = "+1d";
4459 }
4460
4461 # write commit message
4462 if ($format eq 'html') {
4463 my $refs = git_get_references();
4464 my $ref = format_ref_marker($refs, $co{'id'});
4465
4466 git_header_html(undef, $expires);
4467 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4468 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4469 git_print_authorship(\%co);
4470 print "<div class=\"page_body\">\n";
4471 if (@{$co{'comment'}} > 1) {
4472 print "<div class=\"log\">\n";
4473 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4474 print "</div>\n"; # class="log"
4475 }
4476
4477 } elsif ($format eq 'plain') {
4478 my $refs = git_get_references("tags");
4479 my $tagname = git_get_rev_name_tags($hash);
4480 my $filename = basename($project) . "-$hash.patch";
4481
4482 print $cgi->header(
4483 -type => 'text/plain',
4484 -charset => 'utf-8',
4485 -expires => $expires,
4486 -content_disposition => 'inline; filename="' . "$filename" . '"');
4487 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4488 print <<TEXT;
4489From: $co{'author'}
4490Date: $ad{'rfc2822'} ($ad{'tz_local'})
4491Subject: $co{'title'}
4492TEXT
4493 print "X-Git-Tag: $tagname\n" if $tagname;
4494 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4495
4496 foreach my $line (@{$co{'comment'}}) {
4497 print "$line\n";
4498 }
4499 print "---\n\n";
4500 }
4501
4502 # write patch
4503 if ($format eq 'html') {
4504 git_difftree_body(\@difftree, $hash, $hash_parent || @{$co{'parents'}});
4505 print "<br/>\n";
4506
4507 git_patchset_body($fd, \@difftree, $hash, $hash_parent || @{$co{'parents'}});
4508 close $fd;
4509 print "</div>\n"; # class="page_body"
4510 git_footer_html();
4511
4512 } elsif ($format eq 'plain') {
4513 local $/ = undef;
4514 print <$fd>;
4515 close $fd
4516 or print "Reading git-diff-tree failed\n";
4517 }
4518}
4519
4520sub git_commitdiff_plain {
4521 git_commitdiff('plain');
4522}
4523
4524sub git_history {
4525 if (!defined $hash_base) {
4526 $hash_base = git_get_head_hash($project);
4527 }
4528 if (!defined $page) {
4529 $page = 0;
4530 }
4531 my $ftype;
4532 my %co = parse_commit($hash_base);
4533 if (!%co) {
4534 die_error(undef, "Unknown commit object");
4535 }
4536
4537 my $refs = git_get_references();
4538 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4539
4540 if (!defined $hash && defined $file_name) {
4541 $hash = git_get_hash_by_path($hash_base, $file_name);
4542 }
4543 if (defined $hash) {
4544 $ftype = git_get_type($hash);
4545 }
4546
4547 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4548
4549 my $paging_nav = '';
4550 if ($page > 0) {
4551 $paging_nav .=
4552 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4553 file_name=>$file_name)},
4554 "first");
4555 $paging_nav .= " ⋅ " .
4556 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4557 file_name=>$file_name, page=>$page-1),
4558 -accesskey => "p", -title => "Alt-p"}, "prev");
4559 } else {
4560 $paging_nav .= "first";
4561 $paging_nav .= " ⋅ prev";
4562 }
4563 if ($#commitlist >= 100) {
4564 $paging_nav .= " ⋅ " .
4565 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4566 file_name=>$file_name, page=>$page+1),
4567 -accesskey => "n", -title => "Alt-n"}, "next");
4568 } else {
4569 $paging_nav .= " ⋅ next";
4570 }
4571 my $next_link = '';
4572 if ($#commitlist >= 100) {
4573 $next_link =
4574 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4575 file_name=>$file_name, page=>$page+1),
4576 -accesskey => "n", -title => "Alt-n"}, "next");
4577 }
4578
4579 git_header_html();
4580 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4581 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4582 git_print_page_path($file_name, $ftype, $hash_base);
4583
4584 git_history_body(\@commitlist, 0, 99,
4585 $refs, $hash_base, $ftype, $next_link);
4586
4587 git_footer_html();
4588}
4589
4590sub git_search {
4591 my ($have_search) = gitweb_check_feature('search');
4592 if (!$have_search) {
4593 die_error('403 Permission denied', "Permission denied");
4594 }
4595 if (!defined $searchtext) {
4596 die_error(undef, "Text field empty");
4597 }
4598 if (!defined $hash) {
4599 $hash = git_get_head_hash($project);
4600 }
4601 my %co = parse_commit($hash);
4602 if (!%co) {
4603 die_error(undef, "Unknown commit object");
4604 }
4605 if (!defined $page) {
4606 $page = 0;
4607 }
4608
4609 $searchtype ||= 'commit';
4610 if ($searchtype eq 'pickaxe') {
4611 # pickaxe may take all resources of your box and run for several minutes
4612 # with every query - so decide by yourself how public you make this feature
4613 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4614 if (!$have_pickaxe) {
4615 die_error('403 Permission denied', "Permission denied");
4616 }
4617 }
4618
4619 git_header_html();
4620
4621 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4622 my $greptype;
4623 if ($searchtype eq 'commit') {
4624 $greptype = "--grep=";
4625 } elsif ($searchtype eq 'author') {
4626 $greptype = "--author=";
4627 } elsif ($searchtype eq 'committer') {
4628 $greptype = "--committer=";
4629 }
4630 $greptype .= $search_regexp;
4631 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
4632
4633 my $paging_nav = '';
4634 if ($page > 0) {
4635 $paging_nav .=
4636 $cgi->a({-href => href(action=>"search", hash=>$hash,
4637 searchtext=>$searchtext, searchtype=>$searchtype)},
4638 "first");
4639 $paging_nav .= " ⋅ " .
4640 $cgi->a({-href => href(action=>"search", hash=>$hash,
4641 searchtext=>$searchtext, searchtype=>$searchtype,
4642 page=>$page-1),
4643 -accesskey => "p", -title => "Alt-p"}, "prev");
4644 } else {
4645 $paging_nav .= "first";
4646 $paging_nav .= " ⋅ prev";
4647 }
4648 if ($#commitlist >= 100) {
4649 $paging_nav .= " ⋅ " .
4650 $cgi->a({-href => href(action=>"search", hash=>$hash,
4651 searchtext=>$searchtext, searchtype=>$searchtype,
4652 page=>$page+1),
4653 -accesskey => "n", -title => "Alt-n"}, "next");
4654 } else {
4655 $paging_nav .= " ⋅ next";
4656 }
4657 my $next_link = '';
4658 if ($#commitlist >= 100) {
4659 $next_link =
4660 $cgi->a({-href => href(action=>"search", hash=>$hash,
4661 searchtext=>$searchtext, searchtype=>$searchtype,
4662 page=>$page+1),
4663 -accesskey => "n", -title => "Alt-n"}, "next");
4664 }
4665
4666 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
4667 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4668 git_search_grep_body(\@commitlist, 0, 99, $next_link);
4669 }
4670
4671 if ($searchtype eq 'pickaxe') {
4672 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4673 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4674
4675 print "<table cellspacing=\"0\">\n";
4676 my $alternate = 1;
4677 $/ = "\n";
4678 my $git_command = git_cmd_str();
4679 open my $fd, "-|", "$git_command rev-list $hash | " .
4680 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
4681 undef %co;
4682 my @files;
4683 while (my $line = <$fd>) {
4684 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
4685 my %set;
4686 $set{'file'} = $6;
4687 $set{'from_id'} = $3;
4688 $set{'to_id'} = $4;
4689 $set{'id'} = $set{'to_id'};
4690 if ($set{'id'} =~ m/0{40}/) {
4691 $set{'id'} = $set{'from_id'};
4692 }
4693 if ($set{'id'} =~ m/0{40}/) {
4694 next;
4695 }
4696 push @files, \%set;
4697 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
4698 if (%co) {
4699 if ($alternate) {
4700 print "<tr class=\"dark\">\n";
4701 } else {
4702 print "<tr class=\"light\">\n";
4703 }
4704 $alternate ^= 1;
4705 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4706 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4707 "<td>" .
4708 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4709 -class => "list subject"},
4710 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4711 while (my $setref = shift @files) {
4712 my %set = %$setref;
4713 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
4714 hash=>$set{'id'}, file_name=>$set{'file'}),
4715 -class => "list"},
4716 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
4717 "<br/>\n";
4718 }
4719 print "</td>\n" .
4720 "<td class=\"link\">" .
4721 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4722 " | " .
4723 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4724 print "</td>\n" .
4725 "</tr>\n";
4726 }
4727 %co = parse_commit($1);
4728 }
4729 }
4730 close $fd;
4731
4732 print "</table>\n";
4733 }
4734 git_footer_html();
4735}
4736
4737sub git_search_help {
4738 git_header_html();
4739 git_print_page_nav('','', $hash,$hash,$hash);
4740 print <<EOT;
4741<dl>
4742<dt><b>commit</b></dt>
4743<dd>The commit messages and authorship information will be scanned for the given string.</dd>
4744<dt><b>author</b></dt>
4745<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
4746<dt><b>committer</b></dt>
4747<dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
4748EOT
4749 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4750 if ($have_pickaxe) {
4751 print <<EOT;
4752<dt><b>pickaxe</b></dt>
4753<dd>All commits that caused the string to appear or disappear from any file (changes that
4754added, removed or "modified" the string) will be listed. This search can take a while and
4755takes a lot of strain on the server, so please use it wisely.</dd>
4756EOT
4757 }
4758 print "</dl>\n";
4759 git_footer_html();
4760}
4761
4762sub git_shortlog {
4763 my $head = git_get_head_hash($project);
4764 if (!defined $hash) {
4765 $hash = $head;
4766 }
4767 if (!defined $page) {
4768 $page = 0;
4769 }
4770 my $refs = git_get_references();
4771
4772 my @commitlist = parse_commits($hash, 101, (100 * $page));
4773
4774 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
4775 my $next_link = '';
4776 if ($#commitlist >= 100) {
4777 $next_link =
4778 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
4779 -accesskey => "n", -title => "Alt-n"}, "next");
4780 }
4781
4782 git_header_html();
4783 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
4784 git_print_header_div('summary', $project);
4785
4786 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
4787
4788 git_footer_html();
4789}
4790
4791## ......................................................................
4792## feeds (RSS, Atom; OPML)
4793
4794sub git_feed {
4795 my $format = shift || 'atom';
4796 my ($have_blame) = gitweb_check_feature('blame');
4797
4798 # Atom: http://www.atomenabled.org/developers/syndication/
4799 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
4800 if ($format ne 'rss' && $format ne 'atom') {
4801 die_error(undef, "Unknown web feed format");
4802 }
4803
4804 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
4805 my $head = $hash || 'HEAD';
4806 my @commitlist = parse_commits($head, 150);
4807
4808 my %latest_commit;
4809 my %latest_date;
4810 my $content_type = "application/$format+xml";
4811 if (defined $cgi->http('HTTP_ACCEPT') &&
4812 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
4813 # browser (feed reader) prefers text/xml
4814 $content_type = 'text/xml';
4815 }
4816 if (defined($commitlist[0])) {
4817 %latest_commit = %{$commitlist[0]};
4818 %latest_date = parse_date($latest_commit{'author_epoch'});
4819 print $cgi->header(
4820 -type => $content_type,
4821 -charset => 'utf-8',
4822 -last_modified => $latest_date{'rfc2822'});
4823 } else {
4824 print $cgi->header(
4825 -type => $content_type,
4826 -charset => 'utf-8');
4827 }
4828
4829 # Optimization: skip generating the body if client asks only
4830 # for Last-Modified date.
4831 return if ($cgi->request_method() eq 'HEAD');
4832
4833 # header variables
4834 my $title = "$site_name - $project/$action";
4835 my $feed_type = 'log';
4836 if (defined $hash) {
4837 $title .= " - '$hash'";
4838 $feed_type = 'branch log';
4839 if (defined $file_name) {
4840 $title .= " :: $file_name";
4841 $feed_type = 'history';
4842 }
4843 } elsif (defined $file_name) {
4844 $title .= " - $file_name";
4845 $feed_type = 'history';
4846 }
4847 $title .= " $feed_type";
4848 my $descr = git_get_project_description($project);
4849 if (defined $descr) {
4850 $descr = esc_html($descr);
4851 } else {
4852 $descr = "$project " .
4853 ($format eq 'rss' ? 'RSS' : 'Atom') .
4854 " feed";
4855 }
4856 my $owner = git_get_project_owner($project);
4857 $owner = esc_html($owner);
4858
4859 #header
4860 my $alt_url;
4861 if (defined $file_name) {
4862 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
4863 } elsif (defined $hash) {
4864 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
4865 } else {
4866 $alt_url = href(-full=>1, action=>"summary");
4867 }
4868 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
4869 if ($format eq 'rss') {
4870 print <<XML;
4871<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
4872<channel>
4873XML
4874 print "<title>$title</title>\n" .
4875 "<link>$alt_url</link>\n" .
4876 "<description>$descr</description>\n" .
4877 "<language>en</language>\n";
4878 } elsif ($format eq 'atom') {
4879 print <<XML;
4880<feed xmlns="http://www.w3.org/2005/Atom">
4881XML
4882 print "<title>$title</title>\n" .
4883 "<subtitle>$descr</subtitle>\n" .
4884 '<link rel="alternate" type="text/html" href="' .
4885 $alt_url . '" />' . "\n" .
4886 '<link rel="self" type="' . $content_type . '" href="' .
4887 $cgi->self_url() . '" />' . "\n" .
4888 "<id>" . href(-full=>1) . "</id>\n" .
4889 # use project owner for feed author
4890 "<author><name>$owner</name></author>\n";
4891 if (defined $favicon) {
4892 print "<icon>" . esc_url($favicon) . "</icon>\n";
4893 }
4894 if (defined $logo_url) {
4895 # not twice as wide as tall: 72 x 27 pixels
4896 print "<logo>" . esc_url($logo) . "</logo>\n";
4897 }
4898 if (! %latest_date) {
4899 # dummy date to keep the feed valid until commits trickle in:
4900 print "<updated>1970-01-01T00:00:00Z</updated>\n";
4901 } else {
4902 print "<updated>$latest_date{'iso-8601'}</updated>\n";
4903 }
4904 }
4905
4906 # contents
4907 for (my $i = 0; $i <= $#commitlist; $i++) {
4908 my %co = %{$commitlist[$i]};
4909 my $commit = $co{'id'};
4910 # we read 150, we always show 30 and the ones more recent than 48 hours
4911 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
4912 last;
4913 }
4914 my %cd = parse_date($co{'author_epoch'});
4915
4916 # get list of changed files
4917 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4918 $co{'parent'}, $co{'id'}, "--", (defined $file_name ? $file_name : ())
4919 or next;
4920 my @difftree = map { chomp; $_ } <$fd>;
4921 close $fd
4922 or next;
4923
4924 # print element (entry, item)
4925 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
4926 if ($format eq 'rss') {
4927 print "<item>\n" .
4928 "<title>" . esc_html($co{'title'}) . "</title>\n" .
4929 "<author>" . esc_html($co{'author'}) . "</author>\n" .
4930 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
4931 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
4932 "<link>$co_url</link>\n" .
4933 "<description>" . esc_html($co{'title'}) . "</description>\n" .
4934 "<content:encoded>" .
4935 "<![CDATA[\n";
4936 } elsif ($format eq 'atom') {
4937 print "<entry>\n" .
4938 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
4939 "<updated>$cd{'iso-8601'}</updated>\n" .
4940 "<author>\n" .
4941 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
4942 if ($co{'author_email'}) {
4943 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
4944 }
4945 print "</author>\n" .
4946 # use committer for contributor
4947 "<contributor>\n" .
4948 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
4949 if ($co{'committer_email'}) {
4950 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
4951 }
4952 print "</contributor>\n" .
4953 "<published>$cd{'iso-8601'}</published>\n" .
4954 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
4955 "<id>$co_url</id>\n" .
4956 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
4957 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
4958 }
4959 my $comment = $co{'comment'};
4960 print "<pre>\n";
4961 foreach my $line (@$comment) {
4962 $line = esc_html($line);
4963 print "$line\n";
4964 }
4965 print "</pre><ul>\n";
4966 foreach my $difftree_line (@difftree) {
4967 my %difftree = parse_difftree_raw_line($difftree_line);
4968 next if !$difftree{'from_id'};
4969
4970 my $file = $difftree{'file'} || $difftree{'to_file'};
4971
4972 print "<li>" .
4973 "[" .
4974 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
4975 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
4976 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
4977 file_name=>$file, file_parent=>$difftree{'from_file'}),
4978 -title => "diff"}, 'D');
4979 if ($have_blame) {
4980 print $cgi->a({-href => href(-full=>1, action=>"blame",
4981 file_name=>$file, hash_base=>$commit),
4982 -title => "blame"}, 'B');
4983 }
4984 # if this is not a feed of a file history
4985 if (!defined $file_name || $file_name ne $file) {
4986 print $cgi->a({-href => href(-full=>1, action=>"history",
4987 file_name=>$file, hash=>$commit),
4988 -title => "history"}, 'H');
4989 }
4990 $file = esc_path($file);
4991 print "] ".
4992 "$file</li>\n";
4993 }
4994 if ($format eq 'rss') {
4995 print "</ul>]]>\n" .
4996 "</content:encoded>\n" .
4997 "</item>\n";
4998 } elsif ($format eq 'atom') {
4999 print "</ul>\n</div>\n" .
5000 "</content>\n" .
5001 "</entry>\n";
5002 }
5003 }
5004
5005 # end of feed
5006 if ($format eq 'rss') {
5007 print "</channel>\n</rss>\n";
5008 } elsif ($format eq 'atom') {
5009 print "</feed>\n";
5010 }
5011}
5012
5013sub git_rss {
5014 git_feed('rss');
5015}
5016
5017sub git_atom {
5018 git_feed('atom');
5019}
5020
5021sub git_opml {
5022 my @list = git_get_projects_list();
5023
5024 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5025 print <<XML;
5026<?xml version="1.0" encoding="utf-8"?>
5027<opml version="1.0">
5028<head>
5029 <title>$site_name OPML Export</title>
5030</head>
5031<body>
5032<outline text="git RSS feeds">
5033XML
5034
5035 foreach my $pr (@list) {
5036 my %proj = %$pr;
5037 my $head = git_get_head_hash($proj{'path'});
5038 if (!defined $head) {
5039 next;
5040 }
5041 $git_dir = "$projectroot/$proj{'path'}";
5042 my %co = parse_commit($head);
5043 if (!%co) {
5044 next;
5045 }
5046
5047 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5048 my $rss = "$my_url?p=$proj{'path'};a=rss";
5049 my $html = "$my_url?p=$proj{'path'};a=summary";
5050 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5051 }
5052 print <<XML;
5053</outline>
5054</body>
5055</opml>
5056XML
5057}