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