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