]> git.ipfire.org Git - thirdparty/git.git/blame - gitweb/gitweb.perl
Merge branch 'gp/maint-cvsserver'
[thirdparty/git.git] / gitweb / gitweb.perl
CommitLineData
161332a5
KS
1#!/usr/bin/perl
2
c994d620 3# gitweb - simple web interface to track changes in git repositories
22fafb99 4#
00cd0794
KS
5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6# (C) 2005, Christian Gierke
823d5dc8 7#
d8f1c5c2 8# This program is licensed under the GPLv2
161332a5
KS
9
10use strict;
11use warnings;
19806691 12use CGI qw(:standard :escapeHTML -nosticky);
7403d50b 13use CGI::Util qw(unescape);
161332a5 14use CGI::Carp qw(fatalsToBrowser);
40c13813 15use Encode;
b87d78d6 16use Fcntl ':mode';
7a13b999 17use File::Find qw();
cb9c6e5b 18use File::Basename qw(basename);
10bb9036 19binmode STDOUT, ':utf8';
161332a5 20
aa7dd05e
JN
21our $t0;
22if (eval { require Time::HiRes; 1; }) {
23 $t0 = [Time::HiRes::gettimeofday()];
24}
25our $number_of_git_cmds = 0;
26
b1f5f64f 27BEGIN {
3be8e720 28 CGI->compile() if $ENV{'MOD_PERL'};
b1f5f64f
JN
29}
30
4a87b43e 31our $cgi = new CGI;
06c084d2 32our $version = "++GIT_VERSION++";
4a87b43e
DS
33our $my_url = $cgi->url();
34our $my_uri = $cgi->url(-absolute => 1);
3e029299 35
81d3fe9f
GB
36# Base URL for relative URLs in gitweb ($logo, $favicon, ...),
37# needed and used only for URLs with nonempty PATH_INFO
38our $base_url = $my_url;
39
40# When the script is used as DirectoryIndex, the URL does not contain the name
41# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
42# have to do it ourselves. We make $path_info global because it's also used
43# later on.
44#
45# Another issue with the script being the DirectoryIndex is that the resulting
46# $my_url data is not the full script URL: this is good, because we want
47# generated links to keep implying the script name if it wasn't explicitly
48# indicated in the URL we're handling, but it means that $my_url cannot be used
49# as base URL.
50# Therefore, if we needed to strip PATH_INFO, then we know that we have
51# to build the base URL ourselves:
dde80d9c 52our $path_info = $ENV{"PATH_INFO"};
1b2d297e 53if ($path_info) {
81d3fe9f
GB
54 if ($my_url =~ s,\Q$path_info\E$,, &&
55 $my_uri =~ s,\Q$path_info\E$,, &&
56 defined $ENV{'SCRIPT_NAME'}) {
57 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
58 }
b65910fe
GB
59}
60
e130ddaa
AT
61# core git executable to use
62# this can just be "git" if your webserver has a sensible PATH
06c084d2 63our $GIT = "++GIT_BINDIR++/git";
3f7f2710 64
b87d78d6 65# absolute fs-path which will be prepended to the project path
4a87b43e 66#our $projectroot = "/pub/scm";
06c084d2 67our $projectroot = "++GITWEB_PROJECTROOT++";
b87d78d6 68
ca5e9495
LL
69# fs traversing limit for getting project list
70# the number is relative to the projectroot
71our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
72
b87d78d6 73# target of the home link on top of all pages
6132b7e4 74our $home_link = $my_uri || "/";
b87d78d6 75
2de21fac
YS
76# string of the home link on top of all pages
77our $home_link_str = "++GITWEB_HOME_LINK_STR++";
78
49da1daf
AT
79# name of your site or organization to appear in page titles
80# replace this with something more descriptive for clearer bookmarks
8be2890c
PB
81our $site_name = "++GITWEB_SITENAME++"
82 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
49da1daf 83
b2d3476e
AC
84# filename of html text to include at top of each page
85our $site_header = "++GITWEB_SITE_HEADER++";
8ab1da2c 86# html text to include at home page
06c084d2 87our $home_text = "++GITWEB_HOMETEXT++";
b2d3476e
AC
88# filename of html text to include at bottom of each page
89our $site_footer = "++GITWEB_SITE_FOOTER++";
90
91# URI of stylesheets
92our @stylesheets = ("++GITWEB_CSS++");
887a612f
PB
93# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
94our $stylesheet = undef;
9a7a62ff 95# URI of GIT logo (72x27 size)
06c084d2 96our $logo = "++GITWEB_LOGO++";
0b5deba1
JN
97# URI of GIT favicon, assumed to be image/png type
98our $favicon = "++GITWEB_FAVICON++";
4af819d4
JN
99# URI of gitweb.js (JavaScript code for gitweb)
100our $javascript = "++GITWEB_JS++";
aedd9425 101
9a7a62ff
JN
102# URI and label (title) of GIT logo link
103#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
104#our $logo_label = "git documentation";
69fb8283 105our $logo_url = "http://git-scm.com/";
9a7a62ff 106our $logo_label = "git homepage";
51a7c66a 107
09bd7898 108# source of projects list
06c084d2 109our $projects_list = "++GITWEB_LIST++";
b87d78d6 110
55feb120
MH
111# the width (in characters) of the projects list "Description" column
112our $projects_list_description_width = 25;
113
b06dcf8c
FL
114# default order of projects list
115# valid values are none, project, descr, owner, and age
116our $default_projects_order = "project";
117
32f4aacc
ML
118# show repository only if this file exists
119# (only effective if this variable evaluates to true)
120our $export_ok = "++GITWEB_EXPORT_OK++";
121
dd7f5f10
AG
122# show repository only if this subroutine returns true
123# when given the path to the project, for example:
124# sub { return -e "$_[0]/git-daemon-export-ok"; }
125our $export_auth_hook = undef;
126
32f4aacc
ML
127# only allow viewing of repositories also shown on the overview page
128our $strict_export = "++GITWEB_STRICT_EXPORT++";
129
19a8721e
JN
130# list of git base URLs used for URL to where fetch project from,
131# i.e. full URL is "$git_base_url/$project"
d6b7e0b9 132our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
19a8721e 133
f5aa79d9 134# default blob_plain mimetype and default charset for text/plain blob
4a87b43e
DS
135our $default_blob_plain_mimetype = 'text/plain';
136our $default_text_plain_charset = undef;
f5aa79d9 137
2d007374
PB
138# file to use for guessing MIME types before trying /etc/mime.types
139# (relative to the current git repository)
4a87b43e 140our $mimetypes_file = undef;
2d007374 141
00f429af
MK
142# assume this charset if line contains non-UTF-8 characters;
143# it should be valid encoding (see Encoding::Supported(3pm) for list),
144# for which encoding all byte sequences are valid, for example
145# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
146# could be even 'utf-8' for the old behavior)
147our $fallback_encoding = 'latin1';
148
69a9b41c
JN
149# rename detection options for git-diff and git-diff-tree
150# - default is '-M', with the cost proportional to
151# (number of removed files) * (number of new files).
152# - more costly is '-C' (which implies '-M'), with the cost proportional to
153# (number of changed files + number of removed files) * (number of new files)
154# - even more costly is '-C', '--find-copies-harder' with cost
155# (number of files in the original tree) * (number of new files)
156# - one might want to include '-B' option, e.g. '-B', '-M'
157our @diff_opts = ('-M'); # taken from git_commit
158
7e1100e9
MM
159# Disables features that would allow repository owners to inject script into
160# the gitweb domain.
161our $prevent_xss = 0;
162
a3c8ab30
MM
163# information about snapshot formats that gitweb is capable of serving
164our %known_snapshot_formats = (
165 # name => {
166 # 'display' => display name,
167 # 'type' => mime type,
168 # 'suffix' => filename suffix,
169 # 'format' => --format for git-archive,
170 # 'compressor' => [compressor command and arguments]
1bfd3631
MR
171 # (array reference, optional)
172 # 'disabled' => boolean (optional)}
a3c8ab30
MM
173 #
174 'tgz' => {
175 'display' => 'tar.gz',
176 'type' => 'application/x-gzip',
177 'suffix' => '.tar.gz',
178 'format' => 'tar',
179 'compressor' => ['gzip']},
180
181 'tbz2' => {
182 'display' => 'tar.bz2',
183 'type' => 'application/x-bzip2',
184 'suffix' => '.tar.bz2',
185 'format' => 'tar',
186 'compressor' => ['bzip2']},
187
cbdefb5a
MR
188 'txz' => {
189 'display' => 'tar.xz',
190 'type' => 'application/x-xz',
191 'suffix' => '.tar.xz',
192 'format' => 'tar',
193 'compressor' => ['xz'],
194 'disabled' => 1},
195
a3c8ab30
MM
196 'zip' => {
197 'display' => 'zip',
198 'type' => 'application/x-zip',
199 'suffix' => '.zip',
200 'format' => 'zip'},
201);
202
203# Aliases so we understand old gitweb.snapshot values in repository
204# configuration.
205our %known_snapshot_format_aliases = (
206 'gzip' => 'tgz',
207 'bzip2' => 'tbz2',
cbdefb5a 208 'xz' => 'txz',
a3c8ab30
MM
209
210 # backward compatibility: legacy gitweb config support
211 'x-gzip' => undef, 'gz' => undef,
212 'x-bzip2' => undef, 'bz2' => undef,
213 'x-zip' => undef, '' => undef,
214);
215
e9fdd74e
GB
216# Pixel sizes for icons and avatars. If the default font sizes or lineheights
217# are changed, it may be appropriate to change these values too via
218# $GITWEB_CONFIG.
219our %avatar_size = (
220 'default' => 16,
221 'double' => 32
222);
223
ddb8d900
AK
224# You define site-wide feature defaults here; override them with
225# $GITWEB_CONFIG as necessary.
952c65fc 226our %feature = (
17848fc6
JN
227 # feature => {
228 # 'sub' => feature-sub (subroutine),
229 # 'override' => allow-override (boolean),
230 # 'default' => [ default options...] (array reference)}
231 #
b4b20b21 232 # if feature is overridable (it means that allow-override has true value),
17848fc6
JN
233 # then feature-sub will be called with default options as parameters;
234 # return value of feature-sub indicates if to enable specified feature
235 #
b4b20b21
JN
236 # if there is no 'sub' key (no feature-sub), then feature cannot be
237 # overriden
238 #
ff3c0ff2
GB
239 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
240 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
241 # is enabled
952c65fc 242
45a3b12c
PB
243 # Enable the 'blame' blob view, showing the last commit that modified
244 # each line in the file. This can be very CPU-intensive.
245
246 # To enable system wide have in $GITWEB_CONFIG
247 # $feature{'blame'}{'default'} = [1];
248 # To have project specific config enable override in $GITWEB_CONFIG
249 # $feature{'blame'}{'override'} = 1;
250 # and in project config gitweb.blame = 0|1;
952c65fc 251 'blame' => {
cdad8170 252 'sub' => sub { feature_bool('blame', @_) },
952c65fc
JN
253 'override' => 0,
254 'default' => [0]},
255
a3c8ab30 256 # Enable the 'snapshot' link, providing a compressed archive of any
45a3b12c
PB
257 # tree. This can potentially generate high traffic if you have large
258 # project.
259
a3c8ab30
MM
260 # Value is a list of formats defined in %known_snapshot_formats that
261 # you wish to offer.
45a3b12c 262 # To disable system wide have in $GITWEB_CONFIG
a3c8ab30 263 # $feature{'snapshot'}{'default'} = [];
45a3b12c 264 # To have project specific config enable override in $GITWEB_CONFIG
bbee1d97 265 # $feature{'snapshot'}{'override'} = 1;
a3c8ab30
MM
266 # and in project config, a comma-separated list of formats or "none"
267 # to disable. Example: gitweb.snapshot = tbz2,zip;
952c65fc
JN
268 'snapshot' => {
269 'sub' => \&feature_snapshot,
270 'override' => 0,
a3c8ab30 271 'default' => ['tgz']},
04f7a94f 272
6be93511
RF
273 # Enable text search, which will list the commits which match author,
274 # committer or commit text to a given string. Enabled by default.
b4b20b21 275 # Project specific override is not supported.
6be93511
RF
276 'search' => {
277 'override' => 0,
278 'default' => [1]},
279
e7738553
PB
280 # Enable grep search, which will list the files in currently selected
281 # tree containing the given string. Enabled by default. This can be
282 # potentially CPU-intensive, of course.
283
284 # To enable system wide have in $GITWEB_CONFIG
285 # $feature{'grep'}{'default'} = [1];
286 # To have project specific config enable override in $GITWEB_CONFIG
287 # $feature{'grep'}{'override'} = 1;
288 # and in project config gitweb.grep = 0|1;
289 'grep' => {
cdad8170 290 'sub' => sub { feature_bool('grep', @_) },
e7738553
PB
291 'override' => 0,
292 'default' => [1]},
293
45a3b12c
PB
294 # Enable the pickaxe search, which will list the commits that modified
295 # a given string in a file. This can be practical and quite faster
296 # alternative to 'blame', but still potentially CPU-intensive.
297
298 # To enable system wide have in $GITWEB_CONFIG
299 # $feature{'pickaxe'}{'default'} = [1];
300 # To have project specific config enable override in $GITWEB_CONFIG
301 # $feature{'pickaxe'}{'override'} = 1;
302 # and in project config gitweb.pickaxe = 0|1;
04f7a94f 303 'pickaxe' => {
cdad8170 304 'sub' => sub { feature_bool('pickaxe', @_) },
04f7a94f
JN
305 'override' => 0,
306 'default' => [1]},
9e756904 307
e4b48eaa
JN
308 # Enable showing size of blobs in a 'tree' view, in a separate
309 # column, similar to what 'ls -l' does. This cost a bit of IO.
310
311 # To disable system wide have in $GITWEB_CONFIG
312 # $feature{'show-sizes'}{'default'} = [0];
313 # To have project specific config enable override in $GITWEB_CONFIG
314 # $feature{'show-sizes'}{'override'} = 1;
315 # and in project config gitweb.showsizes = 0|1;
316 'show-sizes' => {
317 'sub' => sub { feature_bool('showsizes', @_) },
318 'override' => 0,
319 'default' => [1]},
320
45a3b12c
PB
321 # Make gitweb use an alternative format of the URLs which can be
322 # more readable and natural-looking: project name is embedded
323 # directly in the path and the query string contains other
324 # auxiliary information. All gitweb installations recognize
325 # URL in either format; this configures in which formats gitweb
326 # generates links.
327
328 # To enable system wide have in $GITWEB_CONFIG
329 # $feature{'pathinfo'}{'default'} = [1];
330 # Project specific override is not supported.
331
332 # Note that you will need to change the default location of CSS,
333 # favicon, logo and possibly other files to an absolute URL. Also,
334 # if gitweb.cgi serves as your indexfile, you will need to force
335 # $my_uri to contain the script name in your $GITWEB_CONFIG.
9e756904
MW
336 'pathinfo' => {
337 'override' => 0,
338 'default' => [0]},
e30496df
PB
339
340 # Make gitweb consider projects in project root subdirectories
341 # to be forks of existing projects. Given project $projname.git,
342 # projects matching $projname/*.git will not be shown in the main
343 # projects list, instead a '+' mark will be added to $projname
344 # there and a 'forks' view will be enabled for the project, listing
c2b8b134
FL
345 # all the forks. If project list is taken from a file, forks have
346 # to be listed after the main project.
e30496df
PB
347
348 # To enable system wide have in $GITWEB_CONFIG
349 # $feature{'forks'}{'default'} = [1];
350 # Project specific override is not supported.
351 'forks' => {
352 'override' => 0,
353 'default' => [0]},
d627f68f
PB
354
355 # Insert custom links to the action bar of all project pages.
356 # This enables you mainly to link to third-party scripts integrating
357 # into gitweb; e.g. git-browser for graphical history representation
358 # or custom web-based repository administration interface.
359
360 # The 'default' value consists of a list of triplets in the form
361 # (label, link, position) where position is the label after which
2b11e059 362 # to insert the link and link is a format string where %n expands
d627f68f
PB
363 # to the project name, %f to the project path within the filesystem,
364 # %h to the current hash (h gitweb parameter) and %b to the current
2b11e059 365 # hash base (hb gitweb parameter); %% expands to %.
d627f68f
PB
366
367 # To enable system wide have in $GITWEB_CONFIG e.g.
368 # $feature{'actions'}{'default'} = [('graphiclog',
369 # '/git-browser/by-commit.html?r=%n', 'summary')];
370 # Project specific override is not supported.
371 'actions' => {
372 'override' => 0,
373 'default' => []},
3e3d4ee7 374
aed93de4
PB
375 # Allow gitweb scan project content tags described in ctags/
376 # of project repository, and display the popular Web 2.0-ish
377 # "tag cloud" near the project list. Note that this is something
378 # COMPLETELY different from the normal Git tags.
379
380 # gitweb by itself can show existing tags, but it does not handle
381 # tagging itself; you need an external application for that.
382 # For an example script, check Girocco's cgi/tagproj.cgi.
383 # You may want to install the HTML::TagCloud Perl module to get
384 # a pretty tag cloud instead of just a list of tags.
385
386 # To enable system wide have in $GITWEB_CONFIG
387 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
388 # Project specific override is not supported.
389 'ctags' => {
390 'override' => 0,
391 'default' => [0]},
9872cd6f
GB
392
393 # The maximum number of patches in a patchset generated in patch
394 # view. Set this to 0 or undef to disable patch view, or to a
395 # negative number to remove any limit.
396
397 # To disable system wide have in $GITWEB_CONFIG
398 # $feature{'patches'}{'default'} = [0];
399 # To have project specific config enable override in $GITWEB_CONFIG
400 # $feature{'patches'}{'override'} = 1;
401 # and in project config gitweb.patches = 0|n;
402 # where n is the maximum number of patches allowed in a patchset.
403 'patches' => {
404 'sub' => \&feature_patches,
405 'override' => 0,
406 'default' => [16]},
e9fdd74e
GB
407
408 # Avatar support. When this feature is enabled, views such as
409 # shortlog or commit will display an avatar associated with
410 # the email of the committer(s) and/or author(s).
411
679a1a1d
GB
412 # Currently available providers are gravatar and picon.
413 # If an unknown provider is specified, the feature is disabled.
414
415 # Gravatar depends on Digest::MD5.
416 # Picon currently relies on the indiana.edu database.
e9fdd74e
GB
417
418 # To enable system wide have in $GITWEB_CONFIG
679a1a1d
GB
419 # $feature{'avatar'}{'default'} = ['<provider>'];
420 # where <provider> is either gravatar or picon.
e9fdd74e
GB
421 # To have project specific config enable override in $GITWEB_CONFIG
422 # $feature{'avatar'}{'override'} = 1;
679a1a1d 423 # and in project config gitweb.avatar = <provider>;
e9fdd74e
GB
424 'avatar' => {
425 'sub' => \&feature_avatar,
426 'override' => 0,
427 'default' => ['']},
aa7dd05e
JN
428
429 # Enable displaying how much time and how many git commands
430 # it took to generate and display page. Disabled by default.
431 # Project specific override is not supported.
432 'timed' => {
433 'override' => 0,
434 'default' => [0]},
e627e50a
JN
435
436 # Enable turning some links into links to actions which require
437 # JavaScript to run (like 'blame_incremental'). Not enabled by
438 # default. Project specific override is currently not supported.
439 'javascript-actions' => {
440 'override' => 0,
441 'default' => [0]},
ddb8d900
AK
442);
443
a7c5a283 444sub gitweb_get_feature {
ddb8d900 445 my ($name) = @_;
dd1ad5f1 446 return unless exists $feature{$name};
952c65fc
JN
447 my ($sub, $override, @defaults) = (
448 $feature{$name}{'sub'},
449 $feature{$name}{'override'},
450 @{$feature{$name}{'default'}});
ddb8d900 451 if (!$override) { return @defaults; }
a9455919 452 if (!defined $sub) {
93197898 453 warn "feature $name is not overridable";
a9455919
MW
454 return @defaults;
455 }
ddb8d900
AK
456 return $sub->(@defaults);
457}
458
25b2790f
GB
459# A wrapper to check if a given feature is enabled.
460# With this, you can say
461#
462# my $bool_feat = gitweb_check_feature('bool_feat');
463# gitweb_check_feature('bool_feat') or somecode;
464#
465# instead of
466#
467# my ($bool_feat) = gitweb_get_feature('bool_feat');
468# (gitweb_get_feature('bool_feat'))[0] or somecode;
469#
470sub gitweb_check_feature {
471 return (gitweb_get_feature(@_))[0];
472}
473
474
cdad8170
MK
475sub feature_bool {
476 my $key = shift;
477 my ($val) = git_get_project_config($key, '--bool');
ddb8d900 478
df5d10a3
MC
479 if (!defined $val) {
480 return ($_[0]);
481 } elsif ($val eq 'true') {
cdad8170 482 return (1);
ddb8d900 483 } elsif ($val eq 'false') {
cdad8170 484 return (0);
ddb8d900 485 }
ddb8d900
AK
486}
487
ddb8d900 488sub feature_snapshot {
a3c8ab30 489 my (@fmts) = @_;
ddb8d900
AK
490
491 my ($val) = git_get_project_config('snapshot');
492
a3c8ab30
MM
493 if ($val) {
494 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
ddb8d900
AK
495 }
496
a3c8ab30 497 return @fmts;
de9272f4
LT
498}
499
9872cd6f
GB
500sub feature_patches {
501 my @val = (git_get_project_config('patches', '--int'));
502
503 if (@val) {
504 return @val;
505 }
506
507 return ($_[0]);
508}
509
e9fdd74e
GB
510sub feature_avatar {
511 my @val = (git_get_project_config('avatar'));
512
513 return @val ? @val : @_;
514}
515
2172ce4b
JH
516# checking HEAD file with -e is fragile if the repository was
517# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
518# and then pruned.
519sub check_head_link {
520 my ($dir) = @_;
521 my $headfile = "$dir/HEAD";
522 return ((-e $headfile) ||
523 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
524}
525
526sub check_export_ok {
527 my ($dir) = @_;
528 return (check_head_link($dir) &&
dd7f5f10
AG
529 (!$export_ok || -e "$dir/$export_ok") &&
530 (!$export_auth_hook || $export_auth_hook->($dir)));
2172ce4b
JH
531}
532
a781785d
JN
533# process alternate names for backward compatibility
534# filter out unsupported (unknown) snapshot formats
535sub filter_snapshot_fmts {
536 my @fmts = @_;
537
538 @fmts = map {
539 exists $known_snapshot_format_aliases{$_} ?
540 $known_snapshot_format_aliases{$_} : $_} @fmts;
68cedb1f 541 @fmts = grep {
1bfd3631
MR
542 exists $known_snapshot_formats{$_} &&
543 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
a781785d
JN
544}
545
06c084d2 546our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
17a8b250
GP
547if (-e $GITWEB_CONFIG) {
548 do $GITWEB_CONFIG;
549} else {
550 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
551 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
552}
c8d138a8
JK
553
554# version of the core git binary
66115d36 555our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
aa7dd05e 556$number_of_git_cmds++;
c8d138a8
JK
557
558$projects_list ||= $projectroot;
c8d138a8 559
154b4d78 560# ======================================================================
09bd7898 561# input validation and dispatch
1b2d297e
GB
562
563# input parameters can be collected from a variety of sources (presently, CGI
564# and PATH_INFO), so we define an %input_params hash that collects them all
565# together during validation: this allows subsequent uses (e.g. href()) to be
566# agnostic of the parameter origin
567
dde80d9c 568our %input_params = ();
1b2d297e
GB
569
570# input parameters are stored with the long parameter name as key. This will
571# also be used in the href subroutine to convert parameters to their CGI
572# equivalent, and since the href() usage is the most frequent one, we store
573# the name -> CGI key mapping here, instead of the reverse.
574#
575# XXX: Warning: If you touch this, check the search form for updating,
576# too.
577
dde80d9c 578our @cgi_param_mapping = (
1b2d297e
GB
579 project => "p",
580 action => "a",
581 file_name => "f",
582 file_parent => "fp",
583 hash => "h",
584 hash_parent => "hp",
585 hash_base => "hb",
586 hash_parent_base => "hpb",
587 page => "pg",
588 order => "o",
589 searchtext => "s",
590 searchtype => "st",
591 snapshot_format => "sf",
592 extra_options => "opt",
593 search_use_regexp => "sr",
c4ccf61f
JN
594 # this must be last entry (for manipulation from JavaScript)
595 javascript => "js"
1b2d297e 596);
dde80d9c 597our %cgi_param_mapping = @cgi_param_mapping;
1b2d297e
GB
598
599# we will also need to know the possible actions, for validation
dde80d9c 600our %actions = (
1b2d297e 601 "blame" => \&git_blame,
4af819d4
JN
602 "blame_incremental" => \&git_blame_incremental,
603 "blame_data" => \&git_blame_data,
1b2d297e
GB
604 "blobdiff" => \&git_blobdiff,
605 "blobdiff_plain" => \&git_blobdiff_plain,
606 "blob" => \&git_blob,
607 "blob_plain" => \&git_blob_plain,
608 "commitdiff" => \&git_commitdiff,
609 "commitdiff_plain" => \&git_commitdiff_plain,
610 "commit" => \&git_commit,
611 "forks" => \&git_forks,
612 "heads" => \&git_heads,
613 "history" => \&git_history,
614 "log" => \&git_log,
9872cd6f 615 "patch" => \&git_patch,
a3411f8a 616 "patches" => \&git_patches,
1b2d297e
GB
617 "rss" => \&git_rss,
618 "atom" => \&git_atom,
619 "search" => \&git_search,
620 "search_help" => \&git_search_help,
621 "shortlog" => \&git_shortlog,
622 "summary" => \&git_summary,
623 "tag" => \&git_tag,
624 "tags" => \&git_tags,
625 "tree" => \&git_tree,
626 "snapshot" => \&git_snapshot,
627 "object" => \&git_object,
628 # those below don't need $project
629 "opml" => \&git_opml,
630 "project_list" => \&git_project_list,
631 "project_index" => \&git_project_index,
632);
633
634# finally, we have the hash of allowed extra_options for the commands that
635# allow them
dde80d9c 636our %allowed_options = (
1b2d297e
GB
637 "--no-merges" => [ qw(rss atom log shortlog history) ],
638);
639
640# fill %input_params with the CGI parameters. All values except for 'opt'
641# should be single values, but opt can be an array. We should probably
642# build an array of parameters that can be multi-valued, but since for the time
643# being it's only this one, we just single it out
644while (my ($name, $symbol) = each %cgi_param_mapping) {
645 if ($symbol eq 'opt') {
646 $input_params{$name} = [ $cgi->param($symbol) ];
647 } else {
648 $input_params{$name} = $cgi->param($symbol);
649 }
650}
651
652# now read PATH_INFO and update the parameter list for missing parameters
653sub evaluate_path_info {
654 return if defined $input_params{'project'};
655 return if !$path_info;
656 $path_info =~ s,^/+,,;
657 return if !$path_info;
658
659 # find which part of PATH_INFO is project
660 my $project = $path_info;
661 $project =~ s,/+$,,;
662 while ($project && !check_head_link("$projectroot/$project")) {
663 $project =~ s,/*[^/]*$,,;
664 }
665 return unless $project;
666 $input_params{'project'} = $project;
667
668 # do not change any parameters if an action is given using the query string
669 return if $input_params{'action'};
670 $path_info =~ s,^\Q$project\E/*,,;
671
d8c28822
GB
672 # next, check if we have an action
673 my $action = $path_info;
674 $action =~ s,/.*$,,;
675 if (exists $actions{$action}) {
676 $path_info =~ s,^$action/*,,;
677 $input_params{'action'} = $action;
678 }
679
680 # list of actions that want hash_base instead of hash, but can have no
681 # pathname (f) parameter
682 my @wants_base = (
683 'tree',
684 'history',
685 );
686
b0be3838
GB
687 # we want to catch
688 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
689 my ($parentrefname, $parentpathname, $refname, $pathname) =
690 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
691
692 # first, analyze the 'current' part
1b2d297e 693 if (defined $pathname) {
d8c28822
GB
694 # we got "branch:filename" or "branch:dir/"
695 # we could use git_get_type(branch:pathname), but:
696 # - it needs $git_dir
697 # - it does a git() call
698 # - the convention of terminating directories with a slash
699 # makes it superfluous
700 # - embedding the action in the PATH_INFO would make it even
701 # more superfluous
1b2d297e
GB
702 $pathname =~ s,^/+,,;
703 if (!$pathname || substr($pathname, -1) eq "/") {
d8c28822 704 $input_params{'action'} ||= "tree";
1b2d297e
GB
705 $pathname =~ s,/$,,;
706 } else {
b0be3838
GB
707 # the default action depends on whether we had parent info
708 # or not
709 if ($parentrefname) {
710 $input_params{'action'} ||= "blobdiff_plain";
711 } else {
712 $input_params{'action'} ||= "blob_plain";
713 }
1b2d297e
GB
714 }
715 $input_params{'hash_base'} ||= $refname;
716 $input_params{'file_name'} ||= $pathname;
717 } elsif (defined $refname) {
d8c28822
GB
718 # we got "branch". In this case we have to choose if we have to
719 # set hash or hash_base.
720 #
721 # Most of the actions without a pathname only want hash to be
722 # set, except for the ones specified in @wants_base that want
723 # hash_base instead. It should also be noted that hand-crafted
724 # links having 'history' as an action and no pathname or hash
725 # set will fail, but that happens regardless of PATH_INFO.
726 $input_params{'action'} ||= "shortlog";
727 if (grep { $_ eq $input_params{'action'} } @wants_base) {
728 $input_params{'hash_base'} ||= $refname;
729 } else {
730 $input_params{'hash'} ||= $refname;
731 }
1b2d297e 732 }
b0be3838
GB
733
734 # next, handle the 'parent' part, if present
735 if (defined $parentrefname) {
736 # a missing pathspec defaults to the 'current' filename, allowing e.g.
737 # someproject/blobdiff/oldrev..newrev:/filename
738 if ($parentpathname) {
739 $parentpathname =~ s,^/+,,;
740 $parentpathname =~ s,/$,,;
741 $input_params{'file_parent'} ||= $parentpathname;
742 } else {
743 $input_params{'file_parent'} ||= $input_params{'file_name'};
744 }
745 # we assume that hash_parent_base is wanted if a path was specified,
746 # or if the action wants hash_base instead of hash
747 if (defined $input_params{'file_parent'} ||
748 grep { $_ eq $input_params{'action'} } @wants_base) {
749 $input_params{'hash_parent_base'} ||= $parentrefname;
750 } else {
751 $input_params{'hash_parent'} ||= $parentrefname;
752 }
753 }
1ec2fb5f
GB
754
755 # for the snapshot action, we allow URLs in the form
756 # $project/snapshot/$hash.ext
757 # where .ext determines the snapshot and gets removed from the
758 # passed $refname to provide the $hash.
759 #
760 # To be able to tell that $refname includes the format extension, we
761 # require the following two conditions to be satisfied:
762 # - the hash input parameter MUST have been set from the $refname part
763 # of the URL (i.e. they must be equal)
764 # - the snapshot format MUST NOT have been defined already (e.g. from
765 # CGI parameter sf)
766 # It's also useless to try any matching unless $refname has a dot,
767 # so we check for that too
768 if (defined $input_params{'action'} &&
769 $input_params{'action'} eq 'snapshot' &&
770 defined $refname && index($refname, '.') != -1 &&
771 $refname eq $input_params{'hash'} &&
772 !defined $input_params{'snapshot_format'}) {
773 # We loop over the known snapshot formats, checking for
774 # extensions. Allowed extensions are both the defined suffix
775 # (which includes the initial dot already) and the snapshot
776 # format key itself, with a prepended dot
ccb4b539 777 while (my ($fmt, $opt) = each %known_snapshot_formats) {
1ec2fb5f 778 my $hash = $refname;
095e9142
JN
779 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
780 next;
781 }
782 my $sfx = $1;
1ec2fb5f
GB
783 # a valid suffix was found, so set the snapshot format
784 # and reset the hash parameter
785 $input_params{'snapshot_format'} = $fmt;
786 $input_params{'hash'} = $hash;
787 # we also set the format suffix to the one requested
788 # in the URL: this way a request for e.g. .tgz returns
789 # a .tgz instead of a .tar.gz
790 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
791 last;
792 }
793 }
1b2d297e
GB
794}
795evaluate_path_info();
796
797our $action = $input_params{'action'};
09bd7898 798if (defined $action) {
1b2d297e 799 if (!validate_action($action)) {
074afaa0 800 die_error(400, "Invalid action parameter");
b87d78d6 801 }
b87d78d6 802}
44ad2978 803
24d0693a 804# parameters which are pathnames
1b2d297e 805our $project = $input_params{'project'};
13d02165 806if (defined $project) {
1b2d297e 807 if (!validate_project($project)) {
7939fe44 808 undef $project;
074afaa0 809 die_error(404, "No such project");
9cd3d988 810 }
a59d4afd 811}
6191f8e1 812
1b2d297e 813our $file_name = $input_params{'file_name'};
24d0693a
JN
814if (defined $file_name) {
815 if (!validate_pathname($file_name)) {
074afaa0 816 die_error(400, "Invalid file parameter");
24d0693a
JN
817 }
818}
819
1b2d297e 820our $file_parent = $input_params{'file_parent'};
24d0693a
JN
821if (defined $file_parent) {
822 if (!validate_pathname($file_parent)) {
074afaa0 823 die_error(400, "Invalid file parent parameter");
24d0693a
JN
824 }
825}
5c95fab0 826
24d0693a 827# parameters which are refnames
1b2d297e 828our $hash = $input_params{'hash'};
4fac5294 829if (defined $hash) {
24d0693a 830 if (!validate_refname($hash)) {
074afaa0 831 die_error(400, "Invalid hash parameter");
4fac5294 832 }
a59d4afd 833}
6191f8e1 834
1b2d297e 835our $hash_parent = $input_params{'hash_parent'};
c91da262 836if (defined $hash_parent) {
24d0693a 837 if (!validate_refname($hash_parent)) {
074afaa0 838 die_error(400, "Invalid hash parent parameter");
c91da262 839 }
09bd7898
KS
840}
841
1b2d297e 842our $hash_base = $input_params{'hash_base'};
c91da262 843if (defined $hash_base) {
24d0693a 844 if (!validate_refname($hash_base)) {
074afaa0 845 die_error(400, "Invalid hash base parameter");
c91da262 846 }
a59d4afd 847}
6191f8e1 848
1b2d297e
GB
849our @extra_options = @{$input_params{'extra_options'}};
850# @extra_options is always defined, since it can only be (currently) set from
851# CGI, and $cgi->param() returns the empty array in array context if the param
852# is not set
853foreach my $opt (@extra_options) {
854 if (not exists $allowed_options{$opt}) {
855 die_error(400, "Invalid option parameter");
856 }
857 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
858 die_error(400, "Invalid option parameter for this action");
868bc068
MV
859 }
860}
861
1b2d297e 862our $hash_parent_base = $input_params{'hash_parent_base'};
420e92f2 863if (defined $hash_parent_base) {
24d0693a 864 if (!validate_refname($hash_parent_base)) {
074afaa0 865 die_error(400, "Invalid hash parent base parameter");
420e92f2
JN
866 }
867}
868
24d0693a 869# other parameters
1b2d297e 870our $page = $input_params{'page'};
ea4a6df4 871if (defined $page) {
ac8e3f2b 872 if ($page =~ m/[^0-9]/) {
074afaa0 873 die_error(400, "Invalid page parameter");
b87d78d6 874 }
2ad9331e 875}
823d5dc8 876
1b2d297e 877our $searchtype = $input_params{'searchtype'};
e7738553
PB
878if (defined $searchtype) {
879 if ($searchtype =~ m/[^a-z]/) {
074afaa0 880 die_error(400, "Invalid searchtype parameter");
e7738553
PB
881 }
882}
883
1b2d297e 884our $search_use_regexp = $input_params{'search_use_regexp'};
0e559919 885
1b2d297e 886our $searchtext = $input_params{'searchtext'};
7e431ef9 887our $search_regexp;
19806691 888if (defined $searchtext) {
9d032c72 889 if (length($searchtext) < 2) {
074afaa0 890 die_error(403, "At least two characters are required for search parameter");
9d032c72 891 }
0e559919 892 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
19806691
KS
893}
894
645927ce
ML
895# path to the current git repository
896our $git_dir;
897$git_dir = "$projectroot/$project" if $project;
dd70235f 898
5e166843 899# list of supported snapshot formats
a7c5a283 900our @snapshot_fmts = gitweb_get_feature('snapshot');
5e166843
GB
901@snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
902
e9fdd74e
GB
903# check that the avatar feature is set to a known provider name,
904# and for each provider check if the dependencies are satisfied.
905# if the provider name is invalid or the dependencies are not met,
906# reset $git_avatar to the empty string.
907our ($git_avatar) = gitweb_get_feature('avatar');
908if ($git_avatar eq 'gravatar') {
909 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
679a1a1d
GB
910} elsif ($git_avatar eq 'picon') {
911 # no dependencies
e9fdd74e
GB
912} else {
913 $git_avatar = '';
914}
915
717b8311 916# dispatch
7f9778b1
GP
917if (!defined $action) {
918 if (defined $hash) {
919 $action = git_get_type($hash);
920 } elsif (defined $hash_base && defined $file_name) {
921 $action = git_get_type("$hash_base:$file_name");
922 } elsif (defined $project) {
923 $action = 'summary';
924 } else {
925 $action = 'project_list';
926 }
77a153fd 927}
8e85cdc4 928if (!defined($actions{$action})) {
074afaa0 929 die_error(400, "Unknown action");
09bd7898 930}
e8bb4b38 931if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
d04d3d42 932 !$project) {
074afaa0 933 die_error(400, "Project needed");
d04d3d42 934}
8e85cdc4
ML
935$actions{$action}->();
936exit;
09bd7898 937
06a9d86b
MW
938## ======================================================================
939## action links
940
74fd8728 941sub href {
498fe002 942 my %params = @_;
bd5d1e42
JN
943 # default is to use -absolute url() i.e. $my_uri
944 my $href = $params{-full} ? $my_url : $my_uri;
498fe002 945
afa9b620
JN
946 $params{'project'} = $project unless exists $params{'project'};
947
1cad283a 948 if ($params{-replay}) {
1b2d297e 949 while (my ($name, $symbol) = each %cgi_param_mapping) {
1cad283a 950 if (!exists $params{$name}) {
1b2d297e 951 $params{$name} = $input_params{$name};
1cad283a
JN
952 }
953 }
954 }
955
25b2790f 956 my $use_pathinfo = gitweb_check_feature('pathinfo');
fb098a94 957 if ($use_pathinfo and defined $params{'project'}) {
b02bd7a6
GB
958 # try to put as many parameters as possible in PATH_INFO:
959 # - project name
960 # - action
8db49a7f 961 # - hash_parent or hash_parent_base:/file_parent
3550ea71 962 # - hash or hash_base:/filename
c752a0e0 963 # - the snapshot_format as an appropriate suffix
b02bd7a6
GB
964
965 # When the script is the root DirectoryIndex for the domain,
966 # $href here would be something like http://gitweb.example.com/
967 # Thus, we strip any trailing / from $href, to spare us double
968 # slashes in the final URL
969 $href =~ s,/$,,;
970
971 # Then add the project name, if present
fb098a94 972 $href .= "/".esc_url($params{'project'});
9e756904
MW
973 delete $params{'project'};
974
c752a0e0
GB
975 # since we destructively absorb parameters, we keep this
976 # boolean that remembers if we're handling a snapshot
977 my $is_snapshot = $params{'action'} eq 'snapshot';
978
b02bd7a6
GB
979 # Summary just uses the project path URL, any other action is
980 # added to the URL
981 if (defined $params{'action'}) {
982 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
9e756904
MW
983 delete $params{'action'};
984 }
b02bd7a6 985
8db49a7f
GB
986 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
987 # stripping nonexistent or useless pieces
988 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
989 || $params{'hash_parent'} || $params{'hash'});
b02bd7a6 990 if (defined $params{'hash_base'}) {
8db49a7f
GB
991 if (defined $params{'hash_parent_base'}) {
992 $href .= esc_url($params{'hash_parent_base'});
993 # skip the file_parent if it's the same as the file_name
b7da721f
GB
994 if (defined $params{'file_parent'}) {
995 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
996 delete $params{'file_parent'};
997 } elsif ($params{'file_parent'} !~ /\.\./) {
998 $href .= ":/".esc_url($params{'file_parent'});
999 delete $params{'file_parent'};
1000 }
8db49a7f
GB
1001 }
1002 $href .= "..";
1003 delete $params{'hash_parent'};
1004 delete $params{'hash_parent_base'};
1005 } elsif (defined $params{'hash_parent'}) {
1006 $href .= esc_url($params{'hash_parent'}). "..";
1007 delete $params{'hash_parent'};
1008 }
1009
1010 $href .= esc_url($params{'hash_base'});
1011 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
3550ea71 1012 $href .= ":/".esc_url($params{'file_name'});
b02bd7a6
GB
1013 delete $params{'file_name'};
1014 }
1015 delete $params{'hash'};
1016 delete $params{'hash_base'};
1017 } elsif (defined $params{'hash'}) {
8db49a7f 1018 $href .= esc_url($params{'hash'});
b02bd7a6
GB
1019 delete $params{'hash'};
1020 }
c752a0e0
GB
1021
1022 # If the action was a snapshot, we can absorb the
1023 # snapshot_format parameter too
1024 if ($is_snapshot) {
1025 my $fmt = $params{'snapshot_format'};
1026 # snapshot_format should always be defined when href()
1027 # is called, but just in case some code forgets, we
1028 # fall back to the default
1029 $fmt ||= $snapshot_fmts[0];
1030 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1031 delete $params{'snapshot_format'};
1032 }
9e756904
MW
1033 }
1034
1035 # now encode the parameters explicitly
498fe002 1036 my @result = ();
1b2d297e
GB
1037 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1038 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
498fe002 1039 if (defined $params{$name}) {
f22cca44
JN
1040 if (ref($params{$name}) eq "ARRAY") {
1041 foreach my $par (@{$params{$name}}) {
1042 push @result, $symbol . "=" . esc_param($par);
1043 }
1044 } else {
1045 push @result, $symbol . "=" . esc_param($params{$name});
1046 }
498fe002
JN
1047 }
1048 }
9e756904
MW
1049 $href .= "?" . join(';', @result) if scalar @result;
1050
1051 return $href;
06a9d86b
MW
1052}
1053
1054
717b8311
JN
1055## ======================================================================
1056## validation, quoting/unquoting and escaping
1057
1b2d297e
GB
1058sub validate_action {
1059 my $input = shift || return undef;
1060 return undef unless exists $actions{$input};
1061 return $input;
1062}
1063
1064sub validate_project {
1065 my $input = shift || return undef;
1066 if (!validate_pathname($input) ||
1067 !(-d "$projectroot/$input") ||
ec26f098 1068 !check_export_ok("$projectroot/$input") ||
1b2d297e
GB
1069 ($strict_export && !project_in_list($input))) {
1070 return undef;
1071 } else {
1072 return $input;
1073 }
1074}
1075
24d0693a
JN
1076sub validate_pathname {
1077 my $input = shift || return undef;
717b8311 1078
24d0693a
JN
1079 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1080 # at the beginning, at the end, and between slashes.
1081 # also this catches doubled slashes
1082 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1083 return undef;
717b8311 1084 }
24d0693a
JN
1085 # no null characters
1086 if ($input =~ m!\0!) {
717b8311
JN
1087 return undef;
1088 }
24d0693a
JN
1089 return $input;
1090}
1091
1092sub validate_refname {
1093 my $input = shift || return undef;
1094
1095 # textual hashes are O.K.
1096 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1097 return $input;
1098 }
1099 # it must be correct pathname
1100 $input = validate_pathname($input)
1101 or return undef;
1102 # restrictions on ref name according to git-check-ref-format
1103 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
717b8311
JN
1104 return undef;
1105 }
1106 return $input;
1107}
1108
00f429af
MK
1109# decode sequences of octets in utf8 into Perl's internal form,
1110# which is utf-8 with utf8 flag set if needed. gitweb writes out
1111# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1112sub to_utf8 {
1113 my $str = shift;
e5d3de5c
İD
1114 if (utf8::valid($str)) {
1115 utf8::decode($str);
1116 return $str;
00f429af
MK
1117 } else {
1118 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1119 }
1120}
1121
232ff553
KS
1122# quote unsafe chars, but keep the slash, even when it's not
1123# correct, but quoted slashes look too horrible in bookmarks
1124sub esc_param {
353347b0 1125 my $str = shift;
452e2256 1126 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
a9e60b7d 1127 $str =~ s/ /\+/g;
353347b0
KS
1128 return $str;
1129}
1130
f93bff8d
JN
1131# quote unsafe chars in whole URL, so some charactrs cannot be quoted
1132sub esc_url {
1133 my $str = shift;
1134 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1135 $str =~ s/\+/%2B/g;
1136 $str =~ s/ /\+/g;
1137 return $str;
1138}
1139
232ff553 1140# replace invalid utf8 character with SUBSTITUTION sequence
74fd8728 1141sub esc_html {
40c13813 1142 my $str = shift;
6255ef08
JN
1143 my %opts = @_;
1144
00f429af 1145 $str = to_utf8($str);
c390ae97 1146 $str = $cgi->escapeHTML($str);
6255ef08
JN
1147 if ($opts{'-nbsp'}) {
1148 $str =~ s/ /&nbsp;/g;
1149 }
25ffbb27 1150 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
40c13813
KS
1151 return $str;
1152}
1153
391862e3
JN
1154# quote control characters and escape filename to HTML
1155sub esc_path {
1156 my $str = shift;
1157 my %opts = @_;
1158
00f429af 1159 $str = to_utf8($str);
c390ae97 1160 $str = $cgi->escapeHTML($str);
391862e3
JN
1161 if ($opts{'-nbsp'}) {
1162 $str =~ s/ /&nbsp;/g;
1163 }
1164 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1165 return $str;
1166}
1167
1168# Make control characters "printable", using character escape codes (CEC)
1d3bc0cc
JN
1169sub quot_cec {
1170 my $cntrl = shift;
c84c483f 1171 my %opts = @_;
1d3bc0cc 1172 my %es = ( # character escape codes, aka escape sequences
c84c483f
JN
1173 "\t" => '\t', # tab (HT)
1174 "\n" => '\n', # line feed (LF)
1175 "\r" => '\r', # carrige return (CR)
1176 "\f" => '\f', # form feed (FF)
1177 "\b" => '\b', # backspace (BS)
1178 "\a" => '\a', # alarm (bell) (BEL)
1179 "\e" => '\e', # escape (ESC)
1180 "\013" => '\v', # vertical tab (VT)
1181 "\000" => '\0', # nul character (NUL)
1182 );
1d3bc0cc
JN
1183 my $chr = ( (exists $es{$cntrl})
1184 ? $es{$cntrl}
25dfd171 1185 : sprintf('\%2x', ord($cntrl)) );
c84c483f
JN
1186 if ($opts{-nohtml}) {
1187 return $chr;
1188 } else {
1189 return "<span class=\"cntrl\">$chr</span>";
1190 }
1d3bc0cc
JN
1191}
1192
391862e3
JN
1193# Alternatively use unicode control pictures codepoints,
1194# Unicode "printable representation" (PR)
1d3bc0cc
JN
1195sub quot_upr {
1196 my $cntrl = shift;
c84c483f
JN
1197 my %opts = @_;
1198
1d3bc0cc 1199 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
c84c483f
JN
1200 if ($opts{-nohtml}) {
1201 return $chr;
1202 } else {
1203 return "<span class=\"cntrl\">$chr</span>";
1204 }
1d3bc0cc
JN
1205}
1206
232ff553
KS
1207# git may return quoted and escaped filenames
1208sub unquote {
1209 my $str = shift;
403d0906
JN
1210
1211 sub unq {
1212 my $seq = shift;
1213 my %es = ( # character escape codes, aka escape sequences
1214 't' => "\t", # tab (HT, TAB)
1215 'n' => "\n", # newline (NL)
1216 'r' => "\r", # return (CR)
1217 'f' => "\f", # form feed (FF)
1218 'b' => "\b", # backspace (BS)
1219 'a' => "\a", # alarm (bell) (BEL)
1220 'e' => "\e", # escape (ESC)
1221 'v' => "\013", # vertical tab (VT)
1222 );
1223
1224 if ($seq =~ m/^[0-7]{1,3}$/) {
1225 # octal char sequence
1226 return chr(oct($seq));
1227 } elsif (exists $es{$seq}) {
1228 # C escape sequence, aka character escape code
c84c483f 1229 return $es{$seq};
403d0906
JN
1230 }
1231 # quoted ordinary character
1232 return $seq;
1233 }
1234
232ff553 1235 if ($str =~ m/^"(.*)"$/) {
403d0906 1236 # needs unquoting
232ff553 1237 $str = $1;
403d0906 1238 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
232ff553
KS
1239 }
1240 return $str;
1241}
1242
f16db173
JN
1243# escape tabs (convert tabs to spaces)
1244sub untabify {
1245 my $line = shift;
1246
1247 while ((my $pos = index($line, "\t")) != -1) {
1248 if (my $count = (8 - ($pos % 8))) {
1249 my $spaces = ' ' x $count;
1250 $line =~ s/\t/$spaces/;
1251 }
1252 }
1253
1254 return $line;
1255}
1256
32f4aacc
ML
1257sub project_in_list {
1258 my $project = shift;
1259 my @list = git_get_projects_list();
1260 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1261}
1262
717b8311
JN
1263## ----------------------------------------------------------------------
1264## HTML aware string manipulation
1265
b8d97d07
JN
1266# Try to chop given string on a word boundary between position
1267# $len and $len+$add_len. If there is no word boundary there,
1268# chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1269# (marking chopped part) would be longer than given string.
717b8311
JN
1270sub chop_str {
1271 my $str = shift;
1272 my $len = shift;
1273 my $add_len = shift || 10;
b8d97d07 1274 my $where = shift || 'right'; # 'left' | 'center' | 'right'
717b8311 1275
dee2775a
AW
1276 # Make sure perl knows it is utf8 encoded so we don't
1277 # cut in the middle of a utf8 multibyte char.
1278 $str = to_utf8($str);
1279
717b8311
JN
1280 # allow only $len chars, but don't cut a word if it would fit in $add_len
1281 # if it doesn't fit, cut it if it's still longer than the dots we would add
b8d97d07
JN
1282 # remove chopped character entities entirely
1283
1284 # when chopping in the middle, distribute $len into left and right part
1285 # return early if chopping wouldn't make string shorter
1286 if ($where eq 'center') {
1287 return $str if ($len + 5 >= length($str)); # filler is length 5
1288 $len = int($len/2);
1289 } else {
1290 return $str if ($len + 4 >= length($str)); # filler is length 4
1291 }
1292
1293 # regexps: ending and beginning with word part up to $add_len
1294 my $endre = qr/.{$len}\w{0,$add_len}/;
1295 my $begre = qr/\w{0,$add_len}.{$len}/;
1296
1297 if ($where eq 'left') {
1298 $str =~ m/^(.*?)($begre)$/;
1299 my ($lead, $body) = ($1, $2);
1300 if (length($lead) > 4) {
1301 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
1302 $lead = " ...";
1303 }
1304 return "$lead$body";
1305
1306 } elsif ($where eq 'center') {
1307 $str =~ m/^($endre)(.*)$/;
1308 my ($left, $str) = ($1, $2);
1309 $str =~ m/^(.*?)($begre)$/;
1310 my ($mid, $right) = ($1, $2);
1311 if (length($mid) > 5) {
1312 $left =~ s/&[^;]*$//;
1313 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
1314 $mid = " ... ";
1315 }
1316 return "$left$mid$right";
1317
1318 } else {
1319 $str =~ m/^($endre)(.*)$/;
1320 my $body = $1;
1321 my $tail = $2;
1322 if (length($tail) > 4) {
1323 $body =~ s/&[^;]*$//;
1324 $tail = "... ";
1325 }
1326 return "$body$tail";
717b8311 1327 }
717b8311
JN
1328}
1329
ce58ec91
DS
1330# takes the same arguments as chop_str, but also wraps a <span> around the
1331# result with a title attribute if it does get chopped. Additionally, the
1332# string is HTML-escaped.
1333sub chop_and_escape_str {
b8d97d07 1334 my ($str) = @_;
ce58ec91 1335
b8d97d07 1336 my $chopped = chop_str(@_);
ce58ec91
DS
1337 if ($chopped eq $str) {
1338 return esc_html($chopped);
1339 } else {
14afe774 1340 $str =~ s/[[:cntrl:]]/?/g;
850b90a5 1341 return $cgi->span({-title=>$str}, esc_html($chopped));
ce58ec91
DS
1342 }
1343}
1344
717b8311
JN
1345## ----------------------------------------------------------------------
1346## functions returning short strings
1347
1f1ab5f0
JN
1348# CSS class for given age value (in seconds)
1349sub age_class {
1350 my $age = shift;
1351
785cdea9
JN
1352 if (!defined $age) {
1353 return "noage";
1354 } elsif ($age < 60*60*2) {
1f1ab5f0
JN
1355 return "age0";
1356 } elsif ($age < 60*60*24*2) {
1357 return "age1";
1358 } else {
1359 return "age2";
1360 }
1361}
1362
717b8311
JN
1363# convert age in seconds to "nn units ago" string
1364sub age_string {
1365 my $age = shift;
1366 my $age_str;
a59d4afd 1367
717b8311
JN
1368 if ($age > 60*60*24*365*2) {
1369 $age_str = (int $age/60/60/24/365);
1370 $age_str .= " years ago";
1371 } elsif ($age > 60*60*24*(365/12)*2) {
1372 $age_str = int $age/60/60/24/(365/12);
1373 $age_str .= " months ago";
1374 } elsif ($age > 60*60*24*7*2) {
1375 $age_str = int $age/60/60/24/7;
1376 $age_str .= " weeks ago";
1377 } elsif ($age > 60*60*24*2) {
1378 $age_str = int $age/60/60/24;
1379 $age_str .= " days ago";
1380 } elsif ($age > 60*60*2) {
1381 $age_str = int $age/60/60;
1382 $age_str .= " hours ago";
1383 } elsif ($age > 60*2) {
1384 $age_str = int $age/60;
1385 $age_str .= " min ago";
1386 } elsif ($age > 2) {
1387 $age_str = int $age;
1388 $age_str .= " sec ago";
f6801d66 1389 } else {
717b8311 1390 $age_str .= " right now";
4c02e3c5 1391 }
717b8311 1392 return $age_str;
161332a5
KS
1393}
1394
01ac1e38
JN
1395use constant {
1396 S_IFINVALID => 0030000,
1397 S_IFGITLINK => 0160000,
1398};
1399
1400# submodule/subproject, a commit object reference
74fd8728 1401sub S_ISGITLINK {
01ac1e38
JN
1402 my $mode = shift;
1403
1404 return (($mode & S_IFMT) == S_IFGITLINK)
1405}
1406
717b8311
JN
1407# convert file mode in octal to symbolic file mode string
1408sub mode_str {
1409 my $mode = oct shift;
1410
01ac1e38
JN
1411 if (S_ISGITLINK($mode)) {
1412 return 'm---------';
1413 } elsif (S_ISDIR($mode & S_IFMT)) {
717b8311
JN
1414 return 'drwxr-xr-x';
1415 } elsif (S_ISLNK($mode)) {
1416 return 'lrwxrwxrwx';
1417 } elsif (S_ISREG($mode)) {
1418 # git cares only about the executable bit
1419 if ($mode & S_IXUSR) {
1420 return '-rwxr-xr-x';
1421 } else {
1422 return '-rw-r--r--';
1423 };
c994d620 1424 } else {
717b8311 1425 return '----------';
ff7669a5 1426 }
161332a5
KS
1427}
1428
717b8311
JN
1429# convert file mode in octal to file type string
1430sub file_type {
7c5e2ebb
JN
1431 my $mode = shift;
1432
1433 if ($mode !~ m/^[0-7]+$/) {
1434 return $mode;
1435 } else {
1436 $mode = oct $mode;
1437 }
664f4cc5 1438
01ac1e38
JN
1439 if (S_ISGITLINK($mode)) {
1440 return "submodule";
1441 } elsif (S_ISDIR($mode & S_IFMT)) {
717b8311
JN
1442 return "directory";
1443 } elsif (S_ISLNK($mode)) {
1444 return "symlink";
1445 } elsif (S_ISREG($mode)) {
1446 return "file";
1447 } else {
1448 return "unknown";
1449 }
a59d4afd
KS
1450}
1451
744d0ac3
JN
1452# convert file mode in octal to file type description string
1453sub file_type_long {
1454 my $mode = shift;
1455
1456 if ($mode !~ m/^[0-7]+$/) {
1457 return $mode;
1458 } else {
1459 $mode = oct $mode;
1460 }
1461
01ac1e38
JN
1462 if (S_ISGITLINK($mode)) {
1463 return "submodule";
1464 } elsif (S_ISDIR($mode & S_IFMT)) {
744d0ac3
JN
1465 return "directory";
1466 } elsif (S_ISLNK($mode)) {
1467 return "symlink";
1468 } elsif (S_ISREG($mode)) {
1469 if ($mode & S_IXUSR) {
1470 return "executable";
1471 } else {
1472 return "file";
1473 };
1474 } else {
1475 return "unknown";
1476 }
1477}
1478
1479
717b8311
JN
1480## ----------------------------------------------------------------------
1481## functions returning short HTML fragments, or transforming HTML fragments
3dff5379 1482## which don't belong to other sections
b18f9bf4 1483
225932ed 1484# format line of commit message.
717b8311
JN
1485sub format_log_line_html {
1486 my $line = shift;
b18f9bf4 1487
225932ed 1488 $line = esc_html($line, -nbsp=>1);
7d233dea
MC
1489 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1490 $cgi->a({-href => href(action=>"object", hash=>$1),
1491 -class => "text"}, $1);
1492 }eg;
1493
717b8311 1494 return $line;
b18f9bf4
JN
1495}
1496
717b8311 1497# format marker of refs pointing to given object
4afbaeff
GB
1498
1499# the destination action is chosen based on object type and current context:
1500# - for annotated tags, we choose the tag view unless it's the current view
1501# already, in which case we go to shortlog view
1502# - for other refs, we keep the current view if we're in history, shortlog or
1503# log view, and select shortlog otherwise
847e01fb 1504sub format_ref_marker {
717b8311 1505 my ($refs, $id) = @_;
d294e1ca 1506 my $markers = '';
27fb8c40 1507
717b8311 1508 if (defined $refs->{$id}) {
d294e1ca 1509 foreach my $ref (@{$refs->{$id}}) {
4afbaeff
GB
1510 # this code exploits the fact that non-lightweight tags are the
1511 # only indirect objects, and that they are the only objects for which
1512 # we want to use tag instead of shortlog as action
d294e1ca 1513 my ($type, $name) = qw();
4afbaeff 1514 my $indirect = ($ref =~ s/\^\{\}$//);
d294e1ca
JN
1515 # e.g. tags/v2.6.11 or heads/next
1516 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1517 $type = $1;
1518 $name = $2;
1519 } else {
1520 $type = "ref";
1521 $name = $ref;
1522 }
1523
4afbaeff
GB
1524 my $class = $type;
1525 $class .= " indirect" if $indirect;
1526
1527 my $dest_action = "shortlog";
1528
1529 if ($indirect) {
1530 $dest_action = "tag" unless $action eq "tag";
1531 } elsif ($action =~ /^(history|(short)?log)$/) {
1532 $dest_action = $action;
1533 }
1534
1535 my $dest = "";
1536 $dest .= "refs/" unless $ref =~ m!^refs/!;
1537 $dest .= $ref;
1538
1539 my $link = $cgi->a({
1540 -href => href(
1541 action=>$dest_action,
1542 hash=>$dest
1543 )}, $name);
1544
1545 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1546 $link . "</span>";
d294e1ca
JN
1547 }
1548 }
1549
1550 if ($markers) {
1551 return ' <span class="refs">'. $markers . '</span>';
717b8311
JN
1552 } else {
1553 return "";
1554 }
27fb8c40
JN
1555}
1556
17d07443
JN
1557# format, perhaps shortened and with markers, title line
1558sub format_subject_html {
1c2a4f5a 1559 my ($long, $short, $href, $extra) = @_;
17d07443
JN
1560 $extra = '' unless defined($extra);
1561
1562 if (length($short) < length($long)) {
14afe774 1563 $long =~ s/[[:cntrl:]]/?/g;
7c278014 1564 return $cgi->a({-href => $href, -class => "list subject",
00f429af 1565 -title => to_utf8($long)},
01b89f0c 1566 esc_html($short)) . $extra;
17d07443 1567 } else {
7c278014 1568 return $cgi->a({-href => $href, -class => "list subject"},
01b89f0c 1569 esc_html($long)) . $extra;
17d07443
JN
1570 }
1571}
1572
5a371b7b
GB
1573# Rather than recomputing the url for an email multiple times, we cache it
1574# after the first hit. This gives a visible benefit in views where the avatar
1575# for the same email is used repeatedly (e.g. shortlog).
1576# The cache is shared by all avatar engines (currently gravatar only), which
1577# are free to use it as preferred. Since only one avatar engine is used for any
1578# given page, there's no risk for cache conflicts.
1579our %avatar_cache = ();
1580
679a1a1d
GB
1581# Compute the picon url for a given email, by using the picon search service over at
1582# http://www.cs.indiana.edu/picons/search.html
1583sub picon_url {
1584 my $email = lc shift;
1585 if (!$avatar_cache{$email}) {
1586 my ($user, $domain) = split('@', $email);
1587 $avatar_cache{$email} =
1588 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1589 "$domain/$user/" .
1590 "users+domains+unknown/up/single";
1591 }
1592 return $avatar_cache{$email};
1593}
1594
5a371b7b
GB
1595# Compute the gravatar url for a given email, if it's not in the cache already.
1596# Gravatar stores only the part of the URL before the size, since that's the
1597# one computationally more expensive. This also allows reuse of the cache for
1598# different sizes (for this particular engine).
1599sub gravatar_url {
1600 my $email = lc shift;
1601 my $size = shift;
1602 $avatar_cache{$email} ||=
1603 "http://www.gravatar.com/avatar/" .
1604 Digest::MD5::md5_hex($email) . "?s=";
1605 return $avatar_cache{$email} . $size;
1606}
1607
e9fdd74e
GB
1608# Insert an avatar for the given $email at the given $size if the feature
1609# is enabled.
1610sub git_get_avatar {
1611 my ($email, %opts) = @_;
1612 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
1613 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
1614 $opts{-size} ||= 'default';
1615 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1616 my $url = "";
1617 if ($git_avatar eq 'gravatar') {
5a371b7b 1618 $url = gravatar_url($email, $size);
679a1a1d
GB
1619 } elsif ($git_avatar eq 'picon') {
1620 $url = picon_url($email);
e9fdd74e 1621 }
679a1a1d 1622 # Other providers can be added by extending the if chain, defining $url
e9fdd74e
GB
1623 # as needed. If no variant puts something in $url, we assume avatars
1624 # are completely disabled/unavailable.
1625 if ($url) {
1626 return $pre_white .
1627 "<img width=\"$size\" " .
1628 "class=\"avatar\" " .
1629 "src=\"$url\" " .
7d25ef41 1630 "alt=\"\" " .
e9fdd74e
GB
1631 "/>" . $post_white;
1632 } else {
1633 return "";
1634 }
1635}
1636
e133d65c
SB
1637sub format_search_author {
1638 my ($author, $searchtype, $displaytext) = @_;
1639 my $have_search = gitweb_check_feature('search');
1640
1641 if ($have_search) {
1642 my $performed = "";
1643 if ($searchtype eq 'author') {
1644 $performed = "authored";
1645 } elsif ($searchtype eq 'committer') {
1646 $performed = "committed";
1647 }
1648
1649 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1650 searchtext=>$author,
1651 searchtype=>$searchtype), class=>"list",
1652 title=>"Search for commits $performed by $author"},
1653 $displaytext);
1654
1655 } else {
1656 return $displaytext;
1657 }
1658}
1659
1c49a4e1
GB
1660# format the author name of the given commit with the given tag
1661# the author name is chopped and escaped according to the other
1662# optional parameters (see chop_str).
1663sub format_author_html {
1664 my $tag = shift;
1665 my $co = shift;
1666 my $author = chop_and_escape_str($co->{'author_name'}, @_);
e9fdd74e 1667 return "<$tag class=\"author\">" .
e133d65c
SB
1668 format_search_author($co->{'author_name'}, "author",
1669 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1670 $author) .
1671 "</$tag>";
1c49a4e1
GB
1672}
1673
90921740
JN
1674# format git diff header line, i.e. "diff --(git|combined|cc) ..."
1675sub format_git_diff_header_line {
1676 my $line = shift;
1677 my $diffinfo = shift;
1678 my ($from, $to) = @_;
1679
1680 if ($diffinfo->{'nparents'}) {
1681 # combined diff
1682 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1683 if ($to->{'href'}) {
1684 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1685 esc_path($to->{'file'}));
1686 } else { # file was deleted (no href)
1687 $line .= esc_path($to->{'file'});
1688 }
1689 } else {
1690 # "ordinary" diff
1691 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1692 if ($from->{'href'}) {
1693 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1694 'a/' . esc_path($from->{'file'}));
1695 } else { # file was added (no href)
1696 $line .= 'a/' . esc_path($from->{'file'});
1697 }
1698 $line .= ' ';
1699 if ($to->{'href'}) {
1700 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1701 'b/' . esc_path($to->{'file'}));
1702 } else { # file was deleted
1703 $line .= 'b/' . esc_path($to->{'file'});
1704 }
1705 }
1706
1707 return "<div class=\"diff header\">$line</div>\n";
1708}
1709
1710# format extended diff header line, before patch itself
1711sub format_extended_diff_header_line {
1712 my $line = shift;
1713 my $diffinfo = shift;
1714 my ($from, $to) = @_;
1715
1716 # match <path>
1717 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1718 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1719 esc_path($from->{'file'}));
1720 }
1721 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1722 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1723 esc_path($to->{'file'}));
1724 }
1725 # match single <mode>
1726 if ($line =~ m/\s(\d{6})$/) {
1727 $line .= '<span class="info"> (' .
1728 file_type_long($1) .
1729 ')</span>';
1730 }
1731 # match <hash>
1732 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1733 # can match only for combined diff
1734 $line = 'index ';
1735 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1736 if ($from->{'href'}[$i]) {
1737 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1738 -class=>"hash"},
1739 substr($diffinfo->{'from_id'}[$i],0,7));
1740 } else {
1741 $line .= '0' x 7;
1742 }
1743 # separator
1744 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1745 }
1746 $line .= '..';
1747 if ($to->{'href'}) {
1748 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1749 substr($diffinfo->{'to_id'},0,7));
1750 } else {
1751 $line .= '0' x 7;
1752 }
1753
1754 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1755 # can match only for ordinary diff
1756 my ($from_link, $to_link);
1757 if ($from->{'href'}) {
1758 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1759 substr($diffinfo->{'from_id'},0,7));
1760 } else {
1761 $from_link = '0' x 7;
1762 }
1763 if ($to->{'href'}) {
1764 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1765 substr($diffinfo->{'to_id'},0,7));
1766 } else {
1767 $to_link = '0' x 7;
1768 }
1769 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1770 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1771 }
1772
1773 return $line . "<br/>\n";
1774}
1775
1776# format from-file/to-file diff header
1777sub format_diff_from_to_header {
91af4ce4 1778 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
90921740
JN
1779 my $line;
1780 my $result = '';
1781
1782 $line = $from_line;
1783 #assert($line =~ m/^---/) if DEBUG;
deaa01a9
JN
1784 # no extra formatting for "^--- /dev/null"
1785 if (! $diffinfo->{'nparents'}) {
1786 # ordinary (single parent) diff
1787 if ($line =~ m!^--- "?a/!) {
1788 if ($from->{'href'}) {
1789 $line = '--- a/' .
1790 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1791 esc_path($from->{'file'}));
1792 } else {
1793 $line = '--- a/' .
1794 esc_path($from->{'file'});
1795 }
1796 }
1797 $result .= qq!<div class="diff from_file">$line</div>\n!;
1798
1799 } else {
1800 # combined diff (merge commit)
1801 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1802 if ($from->{'href'}[$i]) {
1803 $line = '--- ' .
91af4ce4
JN
1804 $cgi->a({-href=>href(action=>"blobdiff",
1805 hash_parent=>$diffinfo->{'from_id'}[$i],
1806 hash_parent_base=>$parents[$i],
1807 file_parent=>$from->{'file'}[$i],
1808 hash=>$diffinfo->{'to_id'},
1809 hash_base=>$hash,
1810 file_name=>$to->{'file'}),
1811 -class=>"path",
1812 -title=>"diff" . ($i+1)},
1813 $i+1) .
1814 '/' .
deaa01a9
JN
1815 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1816 esc_path($from->{'file'}[$i]));
1817 } else {
1818 $line = '--- /dev/null';
1819 }
1820 $result .= qq!<div class="diff from_file">$line</div>\n!;
90921740
JN
1821 }
1822 }
90921740
JN
1823
1824 $line = $to_line;
1825 #assert($line =~ m/^\+\+\+/) if DEBUG;
1826 # no extra formatting for "^+++ /dev/null"
1827 if ($line =~ m!^\+\+\+ "?b/!) {
1828 if ($to->{'href'}) {
1829 $line = '+++ b/' .
1830 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1831 esc_path($to->{'file'}));
1832 } else {
1833 $line = '+++ b/' .
1834 esc_path($to->{'file'});
1835 }
1836 }
1837 $result .= qq!<div class="diff to_file">$line</div>\n!;
1838
1839 return $result;
1840}
1841
cd030c3a
JN
1842# create note for patch simplified by combined diff
1843sub format_diff_cc_simplified {
1844 my ($diffinfo, @parents) = @_;
1845 my $result = '';
1846
1847 $result .= "<div class=\"diff header\">" .
1848 "diff --cc ";
1849 if (!is_deleted($diffinfo)) {
1850 $result .= $cgi->a({-href => href(action=>"blob",
1851 hash_base=>$hash,
1852 hash=>$diffinfo->{'to_id'},
1853 file_name=>$diffinfo->{'to_file'}),
1854 -class => "path"},
1855 esc_path($diffinfo->{'to_file'}));
1856 } else {
1857 $result .= esc_path($diffinfo->{'to_file'});
1858 }
1859 $result .= "</div>\n" . # class="diff header"
1860 "<div class=\"diff nodifferences\">" .
1861 "Simple merge" .
1862 "</div>\n"; # class="diff nodifferences"
1863
1864 return $result;
1865}
1866
90921740 1867# format patch (diff) line (not to be used for diff headers)
eee08903
JN
1868sub format_diff_line {
1869 my $line = shift;
59e3b14e 1870 my ($from, $to) = @_;
eee08903
JN
1871 my $diff_class = "";
1872
1873 chomp $line;
1874
e72c0eaf
JN
1875 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1876 # combined diff
1877 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1878 if ($line =~ m/^\@{3}/) {
1879 $diff_class = " chunk_header";
1880 } elsif ($line =~ m/^\\/) {
1881 $diff_class = " incomplete";
1882 } elsif ($prefix =~ tr/+/+/) {
1883 $diff_class = " add";
1884 } elsif ($prefix =~ tr/-/-/) {
1885 $diff_class = " rem";
1886 }
1887 } else {
1888 # assume ordinary diff
1889 my $char = substr($line, 0, 1);
1890 if ($char eq '+') {
1891 $diff_class = " add";
1892 } elsif ($char eq '-') {
1893 $diff_class = " rem";
1894 } elsif ($char eq '@') {
1895 $diff_class = " chunk_header";
1896 } elsif ($char eq "\\") {
1897 $diff_class = " incomplete";
1898 }
eee08903
JN
1899 }
1900 $line = untabify($line);
59e3b14e
JN
1901 if ($from && $to && $line =~ m/^\@{2} /) {
1902 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1903 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1904
1905 $from_lines = 0 unless defined $from_lines;
1906 $to_lines = 0 unless defined $to_lines;
1907
1908 if ($from->{'href'}) {
1909 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1910 -class=>"list"}, $from_text);
1911 }
1912 if ($to->{'href'}) {
1913 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1914 -class=>"list"}, $to_text);
1915 }
1916 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1917 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1918 return "<div class=\"diff$diff_class\">$line</div>\n";
e72c0eaf
JN
1919 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1920 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1921 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1922
1923 @from_text = split(' ', $ranges);
1924 for (my $i = 0; $i < @from_text; ++$i) {
1925 ($from_start[$i], $from_nlines[$i]) =
1926 (split(',', substr($from_text[$i], 1)), 0);
1927 }
1928
1929 $to_text = pop @from_text;
1930 $to_start = pop @from_start;
1931 $to_nlines = pop @from_nlines;
1932
1933 $line = "<span class=\"chunk_info\">$prefix ";
1934 for (my $i = 0; $i < @from_text; ++$i) {
1935 if ($from->{'href'}[$i]) {
1936 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1937 -class=>"list"}, $from_text[$i]);
1938 } else {
1939 $line .= $from_text[$i];
1940 }
1941 $line .= " ";
1942 }
1943 if ($to->{'href'}) {
1944 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1945 -class=>"list"}, $to_text);
1946 } else {
1947 $line .= $to_text;
1948 }
1949 $line .= " $prefix</span>" .
1950 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1951 return "<div class=\"diff$diff_class\">$line</div>\n";
59e3b14e 1952 }
6255ef08 1953 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
eee08903
JN
1954}
1955
a3c8ab30
MM
1956# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1957# linked. Pass the hash of the tree/commit to snapshot.
1958sub format_snapshot_links {
1959 my ($hash) = @_;
a3c8ab30
MM
1960 my $num_fmts = @snapshot_fmts;
1961 if ($num_fmts > 1) {
1962 # A parenthesized list of links bearing format names.
a781785d 1963 # e.g. "snapshot (_tar.gz_ _zip_)"
a3c8ab30
MM
1964 return "snapshot (" . join(' ', map
1965 $cgi->a({
1966 -href => href(
1967 action=>"snapshot",
1968 hash=>$hash,
1969 snapshot_format=>$_
1970 )
1971 }, $known_snapshot_formats{$_}{'display'})
1972 , @snapshot_fmts) . ")";
1973 } elsif ($num_fmts == 1) {
1974 # A single "snapshot" link whose tooltip bears the format name.
a781785d 1975 # i.e. "_snapshot_"
a3c8ab30 1976 my ($fmt) = @snapshot_fmts;
a781785d
JN
1977 return
1978 $cgi->a({
a3c8ab30
MM
1979 -href => href(
1980 action=>"snapshot",
1981 hash=>$hash,
1982 snapshot_format=>$fmt
1983 ),
1984 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1985 }, "snapshot");
1986 } else { # $num_fmts == 0
1987 return undef;
1988 }
1989}
1990
3562198b
JN
1991## ......................................................................
1992## functions returning values to be passed, perhaps after some
1993## transformation, to other functions; e.g. returning arguments to href()
1994
1995# returns hash to be passed to href to generate gitweb URL
1996# in -title key it returns description of link
1997sub get_feed_info {
1998 my $format = shift || 'Atom';
1999 my %res = (action => lc($format));
2000
2001 # feed links are possible only for project views
2002 return unless (defined $project);
2003 # some views should link to OPML, or to generic project feed,
2004 # or don't have specific feed yet (so they should use generic)
2005 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2006
2007 my $branch;
2008 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2009 # from tag links; this also makes possible to detect branch links
2010 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2011 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2012 $branch = $1;
2013 }
2014 # find log type for feed description (title)
2015 my $type = 'log';
2016 if (defined $file_name) {
2017 $type = "history of $file_name";
2018 $type .= "/" if ($action eq 'tree');
2019 $type .= " on '$branch'" if (defined $branch);
2020 } else {
2021 $type = "log of $branch" if (defined $branch);
2022 }
2023
2024 $res{-title} = $type;
2025 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2026 $res{'file_name'} = $file_name;
2027
2028 return %res;
2029}
2030
717b8311
JN
2031## ----------------------------------------------------------------------
2032## git utility subroutines, invoking git commands
42f7eb94 2033
25691fbe
DS
2034# returns path to the core git executable and the --git-dir parameter as list
2035sub git_cmd {
aa7dd05e 2036 $number_of_git_cmds++;
25691fbe
DS
2037 return $GIT, '--git-dir='.$git_dir;
2038}
2039
516381d5
LW
2040# quote the given arguments for passing them to the shell
2041# quote_command("command", "arg 1", "arg with ' and ! characters")
2042# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2043# Try to avoid using this function wherever possible.
2044sub quote_command {
2045 return join(' ',
68cedb1f 2046 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
25691fbe
DS
2047}
2048
717b8311 2049# get HEAD ref of given project as hash
847e01fb 2050sub git_get_head_hash {
b629275f
MR
2051 return git_get_full_hash(shift, 'HEAD');
2052}
2053
2054sub git_get_full_hash {
2055 return git_get_hash(@_);
2056}
2057
2058sub git_get_short_hash {
2059 return git_get_hash(@_, '--short=7');
2060}
2061
2062sub git_get_hash {
2063 my ($project, $hash, @options) = @_;
25691fbe 2064 my $o_git_dir = $git_dir;
df2c37a5 2065 my $retval = undef;
25691fbe 2066 $git_dir = "$projectroot/$project";
b629275f
MR
2067 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2068 '--verify', '-q', @options, $hash) {
2069 $retval = <$fd>;
2070 chomp $retval if defined $retval;
df2c37a5 2071 close $fd;
df2c37a5 2072 }
25691fbe
DS
2073 if (defined $o_git_dir) {
2074 $git_dir = $o_git_dir;
2c5c008b 2075 }
df2c37a5
JH
2076 return $retval;
2077}
2078
717b8311
JN
2079# get type of given object
2080sub git_get_type {
2081 my $hash = shift;
2082
25691fbe 2083 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
717b8311
JN
2084 my $type = <$fd>;
2085 close $fd or return;
2086 chomp $type;
2087 return $type;
2088}
2089
b201927a
JN
2090# repository configuration
2091our $config_file = '';
2092our %config;
2093
2094# store multiple values for single key as anonymous array reference
2095# single values stored directly in the hash, not as [ <value> ]
2096sub hash_set_multi {
2097 my ($hash, $key, $value) = @_;
2098
2099 if (!exists $hash->{$key}) {
2100 $hash->{$key} = $value;
2101 } elsif (!ref $hash->{$key}) {
2102 $hash->{$key} = [ $hash->{$key}, $value ];
2103 } else {
2104 push @{$hash->{$key}}, $value;
2105 }
2106}
2107
2108# return hash of git project configuration
2109# optionally limited to some section, e.g. 'gitweb'
2110sub git_parse_project_config {
2111 my $section_regexp = shift;
2112 my %config;
2113
2114 local $/ = "\0";
2115
2116 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2117 or return;
2118
2119 while (my $keyval = <$fh>) {
2120 chomp $keyval;
2121 my ($key, $value) = split(/\n/, $keyval, 2);
2122
2123 hash_set_multi(\%config, $key, $value)
2124 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2125 }
2126 close $fh;
2127
2128 return %config;
2129}
2130
df5d10a3 2131# convert config value to boolean: 'true' or 'false'
b201927a
JN
2132# no value, number > 0, 'true' and 'yes' values are true
2133# rest of values are treated as false (never as error)
2134sub config_to_bool {
2135 my $val = shift;
2136
df5d10a3
MC
2137 return 1 if !defined $val; # section.key
2138
b201927a
JN
2139 # strip leading and trailing whitespace
2140 $val =~ s/^\s+//;
2141 $val =~ s/\s+$//;
2142
df5d10a3 2143 return (($val =~ /^\d+$/ && $val) || # section.key = 1
b201927a
JN
2144 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2145}
2146
2147# convert config value to simple decimal number
2148# an optional value suffix of 'k', 'm', or 'g' will cause the value
2149# to be multiplied by 1024, 1048576, or 1073741824
2150sub config_to_int {
2151 my $val = shift;
2152
2153 # strip leading and trailing whitespace
2154 $val =~ s/^\s+//;
2155 $val =~ s/\s+$//;
2156
2157 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2158 $unit = lc($unit);
2159 # unknown unit is treated as 1
2160 return $num * ($unit eq 'g' ? 1073741824 :
2161 $unit eq 'm' ? 1048576 :
2162 $unit eq 'k' ? 1024 : 1);
2163 }
2164 return $val;
2165}
2166
2167# convert config value to array reference, if needed
2168sub config_to_multi {
2169 my $val = shift;
2170
d76a585d 2171 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
b201927a
JN
2172}
2173
717b8311 2174sub git_get_project_config {
ddb8d900 2175 my ($key, $type) = @_;
717b8311 2176
b201927a 2177 # key sanity check
717b8311
JN
2178 return unless ($key);
2179 $key =~ s/^gitweb\.//;
2180 return if ($key =~ m/\W/);
2181
b201927a
JN
2182 # type sanity check
2183 if (defined $type) {
2184 $type =~ s/^--//;
2185 $type = undef
2186 unless ($type eq 'bool' || $type eq 'int');
2187 }
2188
2189 # get config
2190 if (!defined $config_file ||
2191 $config_file ne "$git_dir/config") {
2192 %config = git_parse_project_config('gitweb');
2193 $config_file = "$git_dir/config";
2194 }
2195
df5d10a3
MC
2196 # check if config variable (key) exists
2197 return unless exists $config{"gitweb.$key"};
2198
b201927a
JN
2199 # ensure given type
2200 if (!defined $type) {
2201 return $config{"gitweb.$key"};
2202 } elsif ($type eq 'bool') {
2203 # backward compatibility: 'git config --bool' returns true/false
2204 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2205 } elsif ($type eq 'int') {
2206 return config_to_int($config{"gitweb.$key"});
2207 }
2208 return $config{"gitweb.$key"};
717b8311
JN
2209}
2210
717b8311
JN
2211# get hash of given path at given ref
2212sub git_get_hash_by_path {
2213 my $base = shift;
2214 my $path = shift || return undef;
1d782b03 2215 my $type = shift;
717b8311 2216
4b02f483 2217 $path =~ s,/+$,,;
717b8311 2218
25691fbe 2219 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
074afaa0 2220 or die_error(500, "Open git-ls-tree failed");
717b8311
JN
2221 my $line = <$fd>;
2222 close $fd or return undef;
2223
198a2a8a
JN
2224 if (!defined $line) {
2225 # there is no tree or hash given by $path at $base
2226 return undef;
2227 }
2228
717b8311 2229 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
8b4b94cc 2230 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1d782b03
JN
2231 if (defined $type && $type ne $2) {
2232 # type doesn't match
2233 return undef;
2234 }
717b8311
JN
2235 return $3;
2236}
2237
ed224dea
JN
2238# get path of entry with given hash at given tree-ish (ref)
2239# used to get 'from' filename for combined diff (merge commit) for renames
2240sub git_get_path_by_hash {
2241 my $base = shift || return;
2242 my $hash = shift || return;
2243
2244 local $/ = "\0";
2245
2246 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2247 or return undef;
2248 while (my $line = <$fd>) {
2249 chomp $line;
2250
2251 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2252 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2253 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2254 close $fd;
2255 return $1;
2256 }
2257 }
2258 close $fd;
2259 return undef;
2260}
2261
717b8311
JN
2262## ......................................................................
2263## git utility functions, directly accessing git repository
2264
847e01fb 2265sub git_get_project_description {
b87d78d6 2266 my $path = shift;
09bd7898 2267
0e121a2c 2268 $git_dir = "$projectroot/$path";
dff2b6d4 2269 open my $fd, '<', "$git_dir/description"
0e121a2c 2270 or return git_get_project_config('description');
b87d78d6
KS
2271 my $descr = <$fd>;
2272 close $fd;
2eb54efc
JH
2273 if (defined $descr) {
2274 chomp $descr;
2275 }
b87d78d6 2276 return $descr;
12a88f2f
KS
2277}
2278
aed93de4
PB
2279sub git_get_project_ctags {
2280 my $path = shift;
2281 my $ctags = {};
2282
2283 $git_dir = "$projectroot/$path";
ad87e4f6
JN
2284 opendir my $dh, "$git_dir/ctags"
2285 or return $ctags;
2286 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
dff2b6d4 2287 open my $ct, '<', $_ or next;
ad87e4f6 2288 my $val = <$ct>;
aed93de4 2289 chomp $val;
ad87e4f6 2290 close $ct;
aed93de4
PB
2291 my $ctag = $_; $ctag =~ s#.*/##;
2292 $ctags->{$ctag} = $val;
2293 }
ad87e4f6 2294 closedir $dh;
aed93de4
PB
2295 $ctags;
2296}
2297
2298sub git_populate_project_tagcloud {
2299 my $ctags = shift;
2300
2301 # First, merge different-cased tags; tags vote on casing
2302 my %ctags_lc;
2303 foreach (keys %$ctags) {
2304 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2305 if (not $ctags_lc{lc $_}->{topcount}
2306 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2307 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2308 $ctags_lc{lc $_}->{topname} = $_;
2309 }
2310 }
2311
2312 my $cloud;
2313 if (eval { require HTML::TagCloud; 1; }) {
2314 $cloud = HTML::TagCloud->new;
2315 foreach (sort keys %ctags_lc) {
2316 # Pad the title with spaces so that the cloud looks
2317 # less crammed.
2318 my $title = $ctags_lc{$_}->{topname};
2319 $title =~ s/ /&nbsp;/g;
2320 $title =~ s/^/&nbsp;/g;
2321 $title =~ s/$/&nbsp;/g;
2322 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2323 }
2324 } else {
2325 $cloud = \%ctags_lc;
2326 }
2327 $cloud;
2328}
2329
2330sub git_show_project_tagcloud {
2331 my ($cloud, $count) = @_;
2332 print STDERR ref($cloud)."..\n";
2333 if (ref $cloud eq 'HTML::TagCloud') {
2334 return $cloud->html_and_css($count);
2335 } else {
2336 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2337 return '<p align="center">' . join (', ', map {
2338 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2339 } splice(@tags, 0, $count)) . '</p>';
2340 }
2341}
2342
e79ca7cc
JN
2343sub git_get_project_url_list {
2344 my $path = shift;
2345
0e121a2c 2346 $git_dir = "$projectroot/$path";
dff2b6d4 2347 open my $fd, '<', "$git_dir/cloneurl"
0e121a2c
JN
2348 or return wantarray ?
2349 @{ config_to_multi(git_get_project_config('url')) } :
2350 config_to_multi(git_get_project_config('url'));
e79ca7cc
JN
2351 my @git_project_url_list = map { chomp; $_ } <$fd>;
2352 close $fd;
2353
2354 return wantarray ? @git_project_url_list : \@git_project_url_list;
2355}
2356
847e01fb 2357sub git_get_projects_list {
e30496df 2358 my ($filter) = @_;
717b8311
JN
2359 my @list;
2360
e30496df
PB
2361 $filter ||= '';
2362 $filter =~ s/\.git$//;
2363
25b2790f 2364 my $check_forks = gitweb_check_feature('forks');
c2b8b134 2365
717b8311
JN
2366 if (-d $projects_list) {
2367 # search in directory
e30496df 2368 my $dir = $projects_list . ($filter ? "/$filter" : '');
6768d6b8
AK
2369 # remove the trailing "/"
2370 $dir =~ s!/+$!!;
c0011ff8 2371 my $pfxlen = length("$dir");
ca5e9495 2372 my $pfxdepth = ($dir =~ tr!/!!);
c0011ff8
JN
2373
2374 File::Find::find({
2375 follow_fast => 1, # follow symbolic links
d20602ee 2376 follow_skip => 2, # ignore duplicates
c0011ff8
JN
2377 dangling_symlinks => 0, # ignore dangling symlinks, silently
2378 wanted => sub {
2379 # skip project-list toplevel, if we get it.
2380 return if (m!^[/.]$!);
2381 # only directories can be git repositories
2382 return unless (-d $_);
ca5e9495
LL
2383 # don't traverse too deep (Find is super slow on os x)
2384 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2385 $File::Find::prune = 1;
2386 return;
2387 }
c0011ff8
JN
2388
2389 my $subdir = substr($File::Find::name, $pfxlen + 1);
2390 # we check related file in $projectroot
fb3bb3d1
DD
2391 my $path = ($filter ? "$filter/" : '') . $subdir;
2392 if (check_export_ok("$projectroot/$path")) {
2393 push @list, { path => $path };
c0011ff8
JN
2394 $File::Find::prune = 1;
2395 }
2396 },
2397 }, "$dir");
2398
717b8311
JN
2399 } elsif (-f $projects_list) {
2400 # read from file(url-encoded):
2401 # 'git%2Fgit.git Linus+Torvalds'
2402 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2403 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
c2b8b134 2404 my %paths;
dff2b6d4 2405 open my $fd, '<', $projects_list or return;
c2b8b134 2406 PROJECT:
717b8311
JN
2407 while (my $line = <$fd>) {
2408 chomp $line;
2409 my ($path, $owner) = split ' ', $line;
2410 $path = unescape($path);
2411 $owner = unescape($owner);
2412 if (!defined $path) {
2413 next;
2414 }
83ee94c1
JH
2415 if ($filter ne '') {
2416 # looking for forks;
2417 my $pfx = substr($path, 0, length($filter));
2418 if ($pfx ne $filter) {
c2b8b134 2419 next PROJECT;
83ee94c1
JH
2420 }
2421 my $sfx = substr($path, length($filter));
2422 if ($sfx !~ /^\/.*\.git$/) {
c2b8b134
FL
2423 next PROJECT;
2424 }
2425 } elsif ($check_forks) {
2426 PATH:
2427 foreach my $filter (keys %paths) {
2428 # looking for forks;
2429 my $pfx = substr($path, 0, length($filter));
2430 if ($pfx ne $filter) {
2431 next PATH;
2432 }
2433 my $sfx = substr($path, length($filter));
2434 if ($sfx !~ /^\/.*\.git$/) {
2435 next PATH;
2436 }
2437 # is a fork, don't include it in
2438 # the list
2439 next PROJECT;
83ee94c1
JH
2440 }
2441 }
2172ce4b 2442 if (check_export_ok("$projectroot/$path")) {
717b8311
JN
2443 my $pr = {
2444 path => $path,
00f429af 2445 owner => to_utf8($owner),
717b8311 2446 };
c2b8b134
FL
2447 push @list, $pr;
2448 (my $forks_path = $path) =~ s/\.git$//;
2449 $paths{$forks_path}++;
717b8311
JN
2450 }
2451 }
2452 close $fd;
2453 }
717b8311
JN
2454 return @list;
2455}
2456
47852450
JH
2457our $gitweb_project_owner = undef;
2458sub git_get_project_list_from_file {
1e0cf030 2459
47852450 2460 return if (defined $gitweb_project_owner);
1e0cf030 2461
47852450 2462 $gitweb_project_owner = {};
1e0cf030
JN
2463 # read from file (url-encoded):
2464 # 'git%2Fgit.git Linus+Torvalds'
2465 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2466 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2467 if (-f $projects_list) {
dff2b6d4 2468 open(my $fd, '<', $projects_list);
1e0cf030
JN
2469 while (my $line = <$fd>) {
2470 chomp $line;
2471 my ($pr, $ow) = split ' ', $line;
2472 $pr = unescape($pr);
2473 $ow = unescape($ow);
47852450 2474 $gitweb_project_owner->{$pr} = to_utf8($ow);
1e0cf030
JN
2475 }
2476 close $fd;
2477 }
47852450
JH
2478}
2479
2480sub git_get_project_owner {
2481 my $project = shift;
2482 my $owner;
2483
2484 return undef unless $project;
b59012ef 2485 $git_dir = "$projectroot/$project";
47852450
JH
2486
2487 if (!defined $gitweb_project_owner) {
2488 git_get_project_list_from_file();
2489 }
2490
2491 if (exists $gitweb_project_owner->{$project}) {
2492 $owner = $gitweb_project_owner->{$project};
2493 }
b59012ef
BR
2494 if (!defined $owner){
2495 $owner = git_get_project_config('owner');
2496 }
1e0cf030 2497 if (!defined $owner) {
b59012ef 2498 $owner = get_file_owner("$git_dir");
1e0cf030
JN
2499 }
2500
2501 return $owner;
2502}
2503
c60c56cc
JN
2504sub git_get_last_activity {
2505 my ($path) = @_;
2506 my $fd;
2507
2508 $git_dir = "$projectroot/$path";
2509 open($fd, "-|", git_cmd(), 'for-each-ref',
0ff5ec70 2510 '--format=%(committer)',
c60c56cc 2511 '--sort=-committerdate',
0ff5ec70 2512 '--count=1',
c60c56cc
JN
2513 'refs/heads') or return;
2514 my $most_recent = <$fd>;
2515 close $fd or return;
785cdea9
JN
2516 if (defined $most_recent &&
2517 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
c60c56cc
JN
2518 my $timestamp = $1;
2519 my $age = time - $timestamp;
2520 return ($age, age_string($age));
2521 }
c956395e 2522 return (undef, undef);
c60c56cc
JN
2523}
2524
847e01fb 2525sub git_get_references {
717b8311
JN
2526 my $type = shift || "";
2527 my %refs;
28b9d9f7
JN
2528 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2529 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2530 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2531 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
9704d75d 2532 or return;
d294e1ca 2533
717b8311
JN
2534 while (my $line = <$fd>) {
2535 chomp $line;
4afbaeff 2536 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
717b8311 2537 if (defined $refs{$1}) {
d294e1ca 2538 push @{$refs{$1}}, $2;
717b8311 2539 } else {
d294e1ca 2540 $refs{$1} = [ $2 ];
717b8311
JN
2541 }
2542 }
2543 }
2544 close $fd or return;
2545 return \%refs;
2546}
2547
56a322f1
JN
2548sub git_get_rev_name_tags {
2549 my $hash = shift || return undef;
2550
25691fbe 2551 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
56a322f1
JN
2552 or return;
2553 my $name_rev = <$fd>;
2554 close $fd;
2555
2556 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2557 return $1;
2558 } else {
2559 # catches also '$hash undefined' output
2560 return undef;
2561 }
2562}
2563
717b8311
JN
2564## ----------------------------------------------------------------------
2565## parse to hash functions
2566
847e01fb 2567sub parse_date {
717b8311
JN
2568 my $epoch = shift;
2569 my $tz = shift || "-0000";
2570
2571 my %date;
2572 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2573 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2574 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2575 $date{'hour'} = $hour;
2576 $date{'minute'} = $min;
2577 $date{'mday'} = $mday;
2578 $date{'day'} = $days[$wday];
2579 $date{'month'} = $months[$mon];
af6feeb2
JN
2580 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2581 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
952c65fc
JN
2582 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2583 $mday, $months[$mon], $hour ,$min;
af6feeb2 2584 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
a62d6d84 2585 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
717b8311
JN
2586
2587 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2588 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2589 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2590 $date{'hour_local'} = $hour;
2591 $date{'minute_local'} = $min;
2592 $date{'tz_local'} = $tz;
af6feeb2
JN
2593 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2594 1900+$year, $mon+1, $mday,
2595 $hour, $min, $sec, $tz);
717b8311
JN
2596 return %date;
2597}
2598
847e01fb 2599sub parse_tag {
ede5e100
KS
2600 my $tag_id = shift;
2601 my %tag;
d8a20ba9 2602 my @comment;
ede5e100 2603
25691fbe 2604 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
d8a20ba9 2605 $tag{'id'} = $tag_id;
ede5e100
KS
2606 while (my $line = <$fd>) {
2607 chomp $line;
2608 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2609 $tag{'object'} = $1;
7ab0d2b6 2610 } elsif ($line =~ m/^type (.+)$/) {
ede5e100 2611 $tag{'type'} = $1;
7ab0d2b6 2612 } elsif ($line =~ m/^tag (.+)$/) {
ede5e100 2613 $tag{'name'} = $1;
d8a20ba9
KS
2614 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2615 $tag{'author'} = $1;
ba924733
GB
2616 $tag{'author_epoch'} = $2;
2617 $tag{'author_tz'} = $3;
2618 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2619 $tag{'author_name'} = $1;
2620 $tag{'author_email'} = $2;
2621 } else {
2622 $tag{'author_name'} = $tag{'author'};
2623 }
d8a20ba9
KS
2624 } elsif ($line =~ m/--BEGIN/) {
2625 push @comment, $line;
2626 last;
2627 } elsif ($line eq "") {
2628 last;
ede5e100
KS
2629 }
2630 }
d8a20ba9
KS
2631 push @comment, <$fd>;
2632 $tag{'comment'} = \@comment;
19806691 2633 close $fd or return;
ede5e100
KS
2634 if (!defined $tag{'name'}) {
2635 return
2636 };
2637 return %tag
2638}
2639
756bbf54 2640sub parse_commit_text {
ccdfdea0 2641 my ($commit_text, $withparents) = @_;
756bbf54 2642 my @commit_lines = split '\n', $commit_text;
703ac710 2643 my %co;
703ac710 2644
756bbf54
RF
2645 pop @commit_lines; # Remove '\0'
2646
198a2a8a
JN
2647 if (! @commit_lines) {
2648 return;
2649 }
2650
25f422fb 2651 my $header = shift @commit_lines;
198a2a8a 2652 if ($header !~ m/^[0-9a-fA-F]{40}/) {
25f422fb
KS
2653 return;
2654 }
ccdfdea0 2655 ($co{'id'}, my @parents) = split ' ', $header;
19806691 2656 while (my $line = shift @commit_lines) {
b87d78d6 2657 last if $line eq "\n";
7ab0d2b6 2658 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
703ac710 2659 $co{'tree'} = $1;
ccdfdea0 2660 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
208b2dff 2661 push @parents, $1;
022be3d0 2662 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
5ed5bbc7 2663 $co{'author'} = to_utf8($1);
185f09e5
KS
2664 $co{'author_epoch'} = $2;
2665 $co{'author_tz'} = $3;
ba00b8c1
JN
2666 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2667 $co{'author_name'} = $1;
2668 $co{'author_email'} = $2;
2bf7a52c
KS
2669 } else {
2670 $co{'author_name'} = $co{'author'};
2671 }
86eed32d 2672 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
5ed5bbc7 2673 $co{'committer'} = to_utf8($1);
185f09e5
KS
2674 $co{'committer_epoch'} = $2;
2675 $co{'committer_tz'} = $3;
ba00b8c1
JN
2676 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2677 $co{'committer_name'} = $1;
2678 $co{'committer_email'} = $2;
2679 } else {
2680 $co{'committer_name'} = $co{'committer'};
2681 }
703ac710
KS
2682 }
2683 }
ede5e100 2684 if (!defined $co{'tree'}) {
25f422fb 2685 return;
ede5e100 2686 };
208b2dff
RF
2687 $co{'parents'} = \@parents;
2688 $co{'parent'} = $parents[0];
25f422fb 2689
19806691 2690 foreach my $title (@commit_lines) {
c2488d06 2691 $title =~ s/^ //;
19806691 2692 if ($title ne "") {
48c771f4 2693 $co{'title'} = chop_str($title, 80, 5);
19806691
KS
2694 # remove leading stuff of merges to make the interesting part visible
2695 if (length($title) > 50) {
2696 $title =~ s/^Automatic //;
2697 $title =~ s/^merge (of|with) /Merge ... /i;
2698 if (length($title) > 50) {
2699 $title =~ s/(http|rsync):\/\///;
2700 }
2701 if (length($title) > 50) {
2702 $title =~ s/(master|www|rsync)\.//;
2703 }
2704 if (length($title) > 50) {
2705 $title =~ s/kernel.org:?//;
2706 }
2707 if (length($title) > 50) {
2708 $title =~ s/\/pub\/scm//;
2709 }
2710 }
48c771f4 2711 $co{'title_short'} = chop_str($title, 50, 5);
19806691
KS
2712 last;
2713 }
2714 }
53c39676 2715 if (! defined $co{'title'} || $co{'title'} eq "") {
7e0fe5c9
PB
2716 $co{'title'} = $co{'title_short'} = '(no commit message)';
2717 }
25f422fb
KS
2718 # remove added spaces
2719 foreach my $line (@commit_lines) {
2720 $line =~ s/^ //;
2721 }
2722 $co{'comment'} = \@commit_lines;
2ae100df
KS
2723
2724 my $age = time - $co{'committer_epoch'};
2725 $co{'age'} = $age;
d263a6bd 2726 $co{'age_string'} = age_string($age);
71be1e79
KS
2727 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2728 if ($age > 60*60*24*7*2) {
1b1cd421 2729 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
71be1e79
KS
2730 $co{'age_string_age'} = $co{'age_string'};
2731 } else {
2732 $co{'age_string_date'} = $co{'age_string'};
1b1cd421 2733 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
71be1e79 2734 }
703ac710
KS
2735 return %co;
2736}
2737
756bbf54
RF
2738sub parse_commit {
2739 my ($commit_id) = @_;
2740 my %co;
2741
2742 local $/ = "\0";
2743
2744 open my $fd, "-|", git_cmd(), "rev-list",
ccdfdea0 2745 "--parents",
756bbf54 2746 "--header",
756bbf54
RF
2747 "--max-count=1",
2748 $commit_id,
2749 "--",
074afaa0 2750 or die_error(500, "Open git-rev-list failed");
ccdfdea0 2751 %co = parse_commit_text(<$fd>, 1);
756bbf54
RF
2752 close $fd;
2753
2754 return %co;
2755}
2756
2757sub parse_commits {
311e552e 2758 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
756bbf54
RF
2759 my @cos;
2760
2761 $maxcount ||= 1;
2762 $skip ||= 0;
2763
756bbf54
RF
2764 local $/ = "\0";
2765
2766 open my $fd, "-|", git_cmd(), "rev-list",
2767 "--header",
311e552e 2768 @args,
756bbf54 2769 ("--max-count=" . $maxcount),
f47efbb7 2770 ("--skip=" . $skip),
868bc068 2771 @extra_options,
756bbf54
RF
2772 $commit_id,
2773 "--",
2774 ($filename ? ($filename) : ())
074afaa0 2775 or die_error(500, "Open git-rev-list failed");
756bbf54
RF
2776 while (my $line = <$fd>) {
2777 my %co = parse_commit_text($line);
2778 push @cos, \%co;
2779 }
2780 close $fd;
2781
2782 return wantarray ? @cos : \@cos;
2783}
2784
e8e41a93 2785# parse line of git-diff-tree "raw" output
740e67f9
JN
2786sub parse_difftree_raw_line {
2787 my $line = shift;
2788 my %res;
2789
2790 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2791 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2792 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2793 $res{'from_mode'} = $1;
2794 $res{'to_mode'} = $2;
2795 $res{'from_id'} = $3;
2796 $res{'to_id'} = $4;
4ed4a347 2797 $res{'status'} = $5;
740e67f9
JN
2798 $res{'similarity'} = $6;
2799 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
e8e41a93 2800 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
740e67f9 2801 } else {
9d301456 2802 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
740e67f9
JN
2803 }
2804 }
78bc403a
JN
2805 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2806 # combined diff (for merge commit)
2807 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2808 $res{'nparents'} = length($1);
2809 $res{'from_mode'} = [ split(' ', $2) ];
2810 $res{'to_mode'} = pop @{$res{'from_mode'}};
2811 $res{'from_id'} = [ split(' ', $3) ];
2812 $res{'to_id'} = pop @{$res{'from_id'}};
2813 $res{'status'} = [ split('', $4) ];
2814 $res{'to_file'} = unquote($5);
2815 }
740e67f9 2816 # 'c512b523472485aef4fff9e57b229d9d243c967f'
0edcb37d
JN
2817 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2818 $res{'commit'} = $1;
2819 }
740e67f9
JN
2820
2821 return wantarray ? %res : \%res;
2822}
2823
0cec6db5
JN
2824# wrapper: return parsed line of git-diff-tree "raw" output
2825# (the argument might be raw line, or parsed info)
2826sub parsed_difftree_line {
2827 my $line_or_ref = shift;
2828
2829 if (ref($line_or_ref) eq "HASH") {
2830 # pre-parsed (or generated by hand)
2831 return $line_or_ref;
2832 } else {
2833 return parse_difftree_raw_line($line_or_ref);
2834 }
2835}
2836
cb849b46 2837# parse line of git-ls-tree output
74fd8728 2838sub parse_ls_tree_line {
cb849b46
JN
2839 my $line = shift;
2840 my %opts = @_;
2841 my %res;
2842
e4b48eaa
JN
2843 if ($opts{'-l'}) {
2844 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
2845 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
cb849b46 2846
e4b48eaa
JN
2847 $res{'mode'} = $1;
2848 $res{'type'} = $2;
2849 $res{'hash'} = $3;
2850 $res{'size'} = $4;
2851 if ($opts{'-z'}) {
2852 $res{'name'} = $5;
2853 } else {
2854 $res{'name'} = unquote($5);
2855 }
cb849b46 2856 } else {
e4b48eaa
JN
2857 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2858 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2859
2860 $res{'mode'} = $1;
2861 $res{'type'} = $2;
2862 $res{'hash'} = $3;
2863 if ($opts{'-z'}) {
2864 $res{'name'} = $4;
2865 } else {
2866 $res{'name'} = unquote($4);
2867 }
cb849b46
JN
2868 }
2869
2870 return wantarray ? %res : \%res;
2871}
2872
90921740
JN
2873# generates _two_ hashes, references to which are passed as 2 and 3 argument
2874sub parse_from_to_diffinfo {
2875 my ($diffinfo, $from, $to, @parents) = @_;
2876
2877 if ($diffinfo->{'nparents'}) {
2878 # combined diff
2879 $from->{'file'} = [];
2880 $from->{'href'} = [];
2881 fill_from_file_info($diffinfo, @parents)
2882 unless exists $diffinfo->{'from_file'};
2883 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
9d301456
JN
2884 $from->{'file'}[$i] =
2885 defined $diffinfo->{'from_file'}[$i] ?
2886 $diffinfo->{'from_file'}[$i] :
2887 $diffinfo->{'to_file'};
90921740
JN
2888 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2889 $from->{'href'}[$i] = href(action=>"blob",
2890 hash_base=>$parents[$i],
2891 hash=>$diffinfo->{'from_id'}[$i],
2892 file_name=>$from->{'file'}[$i]);
2893 } else {
2894 $from->{'href'}[$i] = undef;
2895 }
2896 }
2897 } else {
0cec6db5 2898 # ordinary (not combined) diff
9d301456 2899 $from->{'file'} = $diffinfo->{'from_file'};
90921740
JN
2900 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2901 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2902 hash=>$diffinfo->{'from_id'},
2903 file_name=>$from->{'file'});
2904 } else {
2905 delete $from->{'href'};
2906 }
2907 }
2908
9d301456 2909 $to->{'file'} = $diffinfo->{'to_file'};
90921740
JN
2910 if (!is_deleted($diffinfo)) { # file exists in result
2911 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2912 hash=>$diffinfo->{'to_id'},
2913 file_name=>$to->{'file'});
2914 } else {
2915 delete $to->{'href'};
2916 }
2917}
2918
717b8311
JN
2919## ......................................................................
2920## parse to array of hashes functions
4c02e3c5 2921
cd146408
JN
2922sub git_get_heads_list {
2923 my $limit = shift;
2924 my @headslist;
2925
2926 open my $fd, '-|', git_cmd(), 'for-each-ref',
2927 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2928 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2929 'refs/heads'
c83a77e4
JN
2930 or return;
2931 while (my $line = <$fd>) {
cd146408 2932 my %ref_item;
120ddde2 2933
cd146408
JN
2934 chomp $line;
2935 my ($refinfo, $committerinfo) = split(/\0/, $line);
2936 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2937 my ($committer, $epoch, $tz) =
2938 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
bf901f8e 2939 $ref_item{'fullname'} = $name;
cd146408
JN
2940 $name =~ s!^refs/heads/!!;
2941
2942 $ref_item{'name'} = $name;
2943 $ref_item{'id'} = $hash;
2944 $ref_item{'title'} = $title || '(no commit message)';
2945 $ref_item{'epoch'} = $epoch;
2946 if ($epoch) {
2947 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2948 } else {
2949 $ref_item{'age'} = "unknown";
717b8311 2950 }
cd146408
JN
2951
2952 push @headslist, \%ref_item;
c83a77e4
JN
2953 }
2954 close $fd;
2955
cd146408
JN
2956 return wantarray ? @headslist : \@headslist;
2957}
2958
2959sub git_get_tags_list {
2960 my $limit = shift;
2961 my @tagslist;
2962
2963 open my $fd, '-|', git_cmd(), 'for-each-ref',
2964 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2965 '--format=%(objectname) %(objecttype) %(refname) '.
2966 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2967 'refs/tags'
2968 or return;
2969 while (my $line = <$fd>) {
2970 my %ref_item;
7a13b999 2971
cd146408
JN
2972 chomp $line;
2973 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2974 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2975 my ($creator, $epoch, $tz) =
2976 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
bf901f8e 2977 $ref_item{'fullname'} = $name;
cd146408
JN
2978 $name =~ s!^refs/tags/!!;
2979
2980 $ref_item{'type'} = $type;
2981 $ref_item{'id'} = $id;
2982 $ref_item{'name'} = $name;
2983 if ($type eq "tag") {
2984 $ref_item{'subject'} = $title;
2985 $ref_item{'reftype'} = $reftype;
2986 $ref_item{'refid'} = $refid;
2987 } else {
2988 $ref_item{'reftype'} = $type;
2989 $ref_item{'refid'} = $id;
2990 }
2991
2992 if ($type eq "tag" || $type eq "commit") {
2993 $ref_item{'epoch'} = $epoch;
2994 if ($epoch) {
2995 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2996 } else {
2997 $ref_item{'age'} = "unknown";
2998 }
2999 }
991910a9 3000
cd146408 3001 push @tagslist, \%ref_item;
717b8311 3002 }
cd146408
JN
3003 close $fd;
3004
3005 return wantarray ? @tagslist : \@tagslist;
86eed32d
KS
3006}
3007
717b8311
JN
3008## ----------------------------------------------------------------------
3009## filesystem-related functions
022be3d0 3010
c07ad4b9
KS
3011sub get_file_owner {
3012 my $path = shift;
3013
3014 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3015 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3016 if (!defined $gcos) {
3017 return undef;
3018 }
3019 my $owner = $gcos;
3020 $owner =~ s/[,;].*$//;
00f429af 3021 return to_utf8($owner);
c07ad4b9
KS
3022}
3023
2dcb5e1a
JN
3024# assume that file exists
3025sub insert_file {
3026 my $filename = shift;
3027
3028 open my $fd, '<', $filename;
4586864a 3029 print map { to_utf8($_) } <$fd>;
2dcb5e1a
JN
3030 close $fd;
3031}
3032
717b8311
JN
3033## ......................................................................
3034## mimetype related functions
09bd7898 3035
717b8311
JN
3036sub mimetype_guess_file {
3037 my $filename = shift;
3038 my $mimemap = shift;
3039 -r $mimemap or return undef;
3040
3041 my %mimemap;
dff2b6d4 3042 open(my $mh, '<', $mimemap) or return undef;
ad87e4f6 3043 while (<$mh>) {
618918e5 3044 next if m/^#/; # skip comments
ad87e4f6 3045 my ($mimetype, $exts) = split(/\t+/);
46b059d7
JH
3046 if (defined $exts) {
3047 my @exts = split(/\s+/, $exts);
3048 foreach my $ext (@exts) {
ad87e4f6 3049 $mimemap{$ext} = $mimetype;
46b059d7 3050 }
09bd7898 3051 }
09bd7898 3052 }
ad87e4f6 3053 close($mh);
09bd7898 3054
8059319a 3055 $filename =~ /\.([^.]*)$/;
717b8311
JN
3056 return $mimemap{$1};
3057}
5996ca08 3058
717b8311
JN
3059sub mimetype_guess {
3060 my $filename = shift;
3061 my $mime;
3062 $filename =~ /\./ or return undef;
5996ca08 3063
717b8311
JN
3064 if ($mimetypes_file) {
3065 my $file = $mimetypes_file;
d5aa50de
JN
3066 if ($file !~ m!^/!) { # if it is relative path
3067 # it is relative to project
3068 $file = "$projectroot/$project/$file";
3069 }
717b8311
JN
3070 $mime = mimetype_guess_file($filename, $file);
3071 }
3072 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3073 return $mime;
5996ca08
FF
3074}
3075
847e01fb 3076sub blob_mimetype {
717b8311
JN
3077 my $fd = shift;
3078 my $filename = shift;
5996ca08 3079
717b8311
JN
3080 if ($filename) {
3081 my $mime = mimetype_guess($filename);
3082 $mime and return $mime;
d8d17b5d 3083 }
717b8311
JN
3084
3085 # just in case
3086 return $default_blob_plain_mimetype unless $fd;
3087
3088 if (-T $fd) {
7f718e8b 3089 return 'text/plain';
717b8311
JN
3090 } elsif (! $filename) {
3091 return 'application/octet-stream';
3092 } elsif ($filename =~ m/\.png$/i) {
3093 return 'image/png';
3094 } elsif ($filename =~ m/\.gif$/i) {
3095 return 'image/gif';
3096 } elsif ($filename =~ m/\.jpe?g$/i) {
3097 return 'image/jpeg';
d8d17b5d 3098 } else {
717b8311 3099 return 'application/octet-stream';
f7ab660c 3100 }
717b8311
JN
3101}
3102
7f718e8b
JN
3103sub blob_contenttype {
3104 my ($fd, $file_name, $type) = @_;
3105
3106 $type ||= blob_mimetype($fd, $file_name);
3107 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3108 $type .= "; charset=$default_text_plain_charset";
3109 }
3110
3111 return $type;
3112}
3113
717b8311
JN
3114## ======================================================================
3115## functions printing HTML: header, footer, error page
3116
3117sub git_header_html {
3118 my $status = shift || "200 OK";
3119 my $expires = shift;
3120
8be2890c 3121 my $title = "$site_name";
717b8311 3122 if (defined $project) {
00f429af 3123 $title .= " - " . to_utf8($project);
717b8311
JN
3124 if (defined $action) {
3125 $title .= "/$action";
3126 if (defined $file_name) {
403d0906 3127 $title .= " - " . esc_path($file_name);
717b8311
JN
3128 if ($action eq "tree" && $file_name !~ m|/$|) {
3129 $title .= "/";
3130 }
3131 }
3132 }
f7ab660c 3133 }
717b8311
JN
3134 my $content_type;
3135 # require explicit support from the UA if we are to send the page as
3136 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3137 # we have to do this because MSIE sometimes globs '*/*', pretending to
3138 # support xhtml+xml but choking when it gets what it asked for.
952c65fc
JN
3139 if (defined $cgi->http('HTTP_ACCEPT') &&
3140 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3141 $cgi->Accept('application/xhtml+xml') != 0) {
717b8311 3142 $content_type = 'application/xhtml+xml';
f7ab660c 3143 } else {
717b8311 3144 $content_type = 'text/html';
f7ab660c 3145 }
952c65fc
JN
3146 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3147 -status=> $status, -expires => $expires);
45c9a758 3148 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
717b8311
JN
3149 print <<EOF;
3150<?xml version="1.0" encoding="utf-8"?>
3151<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3152<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
d4baf9ea 3153<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
717b8311
JN
3154<!-- git core binaries version $git_version -->
3155<head>
3156<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
45c9a758 3157<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
717b8311
JN
3158<meta name="robots" content="index, nofollow"/>
3159<title>$title</title>
717b8311 3160EOF
41a4d16e
GB
3161 # the stylesheet, favicon etc urls won't work correctly with path_info
3162 # unless we set the appropriate base URL
c3254aee 3163 if ($ENV{'PATH_INFO'}) {
81d3fe9f 3164 print "<base href=\"".esc_url($base_url)."\" />\n";
c3254aee 3165 }
41a4d16e
GB
3166 # print out each stylesheet that exist, providing backwards capability
3167 # for those people who defined $stylesheet in a config file
b2d3476e 3168 if (defined $stylesheet) {
b2d3476e
AC
3169 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3170 } else {
3171 foreach my $stylesheet (@stylesheets) {
3172 next unless $stylesheet;
3173 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3174 }
3175 }
dd04c428 3176 if (defined $project) {
3562198b
JN
3177 my %href_params = get_feed_info();
3178 if (!exists $href_params{'-title'}) {
3179 $href_params{'-title'} = 'log';
3180 }
3181
3182 foreach my $format qw(RSS Atom) {
3183 my $type = lc($format);
3184 my %link_attr = (
3185 '-rel' => 'alternate',
3186 '-title' => "$project - $href_params{'-title'} - $format feed",
3187 '-type' => "application/$type+xml"
3188 );
3189
3190 $href_params{'action'} = $type;
3191 $link_attr{'-href'} = href(%href_params);
3192 print "<link ".
3193 "rel=\"$link_attr{'-rel'}\" ".
3194 "title=\"$link_attr{'-title'}\" ".
3195 "href=\"$link_attr{'-href'}\" ".
3196 "type=\"$link_attr{'-type'}\" ".
3197 "/>\n";
3198
3199 $href_params{'extra_options'} = '--no-merges';
3200 $link_attr{'-href'} = href(%href_params);
3201 $link_attr{'-title'} .= ' (no merges)';
3202 print "<link ".
3203 "rel=\"$link_attr{'-rel'}\" ".
3204 "title=\"$link_attr{'-title'}\" ".
3205 "href=\"$link_attr{'-href'}\" ".
3206 "type=\"$link_attr{'-type'}\" ".
3207 "/>\n";
3208 }
3209
9d0734ae
JN
3210 } else {
3211 printf('<link rel="alternate" title="%s projects list" '.
3562198b 3212 'href="%s" type="text/plain; charset=utf-8" />'."\n",
9d0734ae 3213 $site_name, href(project=>undef, action=>"project_index"));
af6feeb2 3214 printf('<link rel="alternate" title="%s projects feeds" '.
3562198b 3215 'href="%s" type="text/x-opml" />'."\n",
9d0734ae 3216 $site_name, href(project=>undef, action=>"opml"));
dd04c428 3217 }
0b5deba1 3218 if (defined $favicon) {
3562198b 3219 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
0b5deba1 3220 }
10161355 3221
dd04c428 3222 print "</head>\n" .
b2d3476e
AC
3223 "<body>\n";
3224
3225 if (-f $site_header) {
2dcb5e1a 3226 insert_file($site_header);
b2d3476e
AC
3227 }
3228
3229 print "<div class=\"page_header\">\n" .
9a7a62ff
JN
3230 $cgi->a({-href => esc_url($logo_url),
3231 -title => $logo_label},
3232 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
f93bff8d 3233 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
717b8311 3234 if (defined $project) {
1c2a4f5a 3235 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
717b8311
JN
3236 if (defined $action) {
3237 print " / $action";
3238 }
3239 print "\n";
6be93511 3240 }
d77b5673
PB
3241 print "</div>\n";
3242
25b2790f 3243 my $have_search = gitweb_check_feature('search');
f70dda25 3244 if (defined $project && $have_search) {
717b8311
JN
3245 if (!defined $searchtext) {
3246 $searchtext = "";
3247 }
3248 my $search_hash;
3249 if (defined $hash_base) {
3250 $search_hash = $hash_base;
3251 } elsif (defined $hash) {
3252 $search_hash = $hash;
bddec01d 3253 } else {
717b8311 3254 $search_hash = "HEAD";
bddec01d 3255 }
40375a83 3256 my $action = $my_uri;
25b2790f 3257 my $use_pathinfo = gitweb_check_feature('pathinfo');
40375a83 3258 if ($use_pathinfo) {
85d17a12 3259 $action .= "/".esc_url($project);
40375a83 3260 }
40375a83 3261 print $cgi->startform(-method => "get", -action => $action) .
717b8311 3262 "<div class=\"search\">\n" .
f70dda25
JN
3263 (!$use_pathinfo &&
3264 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3265 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3266 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
88ad729b 3267 $cgi->popup_menu(-name => 'st', -default => 'commit',
e7738553 3268 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
88ad729b
PB
3269 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3270 " search:\n",
717b8311 3271 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
0e559919
PB
3272 "<span title=\"Extended regular expression\">" .
3273 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3274 -checked => $search_use_regexp) .
3275 "</span>" .
717b8311
JN
3276 "</div>" .
3277 $cgi->end_form() . "\n";
b87d78d6 3278 }
717b8311
JN
3279}
3280
3281sub git_footer_html {
3562198b
JN
3282 my $feed_class = 'rss_logo';
3283
717b8311
JN
3284 print "<div class=\"page_footer\">\n";
3285 if (defined $project) {
847e01fb 3286 my $descr = git_get_project_description($project);
717b8311
JN
3287 if (defined $descr) {
3288 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3289 }
3562198b
JN
3290
3291 my %href_params = get_feed_info();
3292 if (!%href_params) {
3293 $feed_class .= ' generic';
3294 }
3295 $href_params{'-title'} ||= 'log';
3296
3297 foreach my $format qw(RSS Atom) {
3298 $href_params{'action'} = lc($format);
3299 print $cgi->a({-href => href(%href_params),
3300 -title => "$href_params{'-title'} $format feed",
3301 -class => $feed_class}, $format)."\n";
3302 }
3303
717b8311 3304 } else {
a1565c44 3305 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3562198b 3306 -class => $feed_class}, "OPML") . " ";
9d0734ae 3307 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3562198b 3308 -class => $feed_class}, "TXT") . "\n";
717b8311 3309 }
3562198b 3310 print "</div>\n"; # class="page_footer"
b2d3476e 3311
aa7dd05e
JN
3312 if (defined $t0 && gitweb_check_feature('timed')) {
3313 print "<div id=\"generating_info\">\n";
3314 print 'This page took '.
3315 '<span id="generating_time" class="time_span">'.
3316 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
3317 ' seconds </span>'.
3318 ' and '.
3319 '<span id="generating_cmd">'.
3320 $number_of_git_cmds.
3321 '</span> git commands '.
3322 " to generate.\n";
3323 print "</div>\n"; # class="page_footer"
3324 }
3325
b2d3476e 3326 if (-f $site_footer) {
2dcb5e1a 3327 insert_file($site_footer);
b2d3476e
AC
3328 }
3329
c4ccf61f
JN
3330 print qq!<script type="text/javascript" src="$javascript"></script>\n!;
3331 if ($action eq 'blame_incremental') {
3332 print qq!<script type="text/javascript">\n!.
3333 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
3334 qq! "!. href() .qq!");\n!.
3335 qq!</script>\n!;
e627e50a 3336 } elsif (gitweb_check_feature('javascript-actions')) {
c4ccf61f
JN
3337 print qq!<script type="text/javascript">\n!.
3338 qq!window.onload = fixLinks;\n!.
3339 qq!</script>\n!;
3340 }
3341
b2d3476e 3342 print "</body>\n" .
717b8311
JN
3343 "</html>";
3344}
3345
074afaa0
LW
3346# die_error(<http_status_code>, <error_message>)
3347# Example: die_error(404, 'Hash not found')
3348# By convention, use the following status codes (as defined in RFC 2616):
3349# 400: Invalid or missing CGI parameters, or
3350# requested object exists but has wrong type.
3351# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3352# this server or project.
3353# 404: Requested object/revision/project doesn't exist.
3354# 500: The server isn't configured properly, or
3355# an internal error occurred (e.g. failed assertions caused by bugs), or
3356# an unknown error occurred (e.g. the git binary died unexpectedly).
717b8311 3357sub die_error {
074afaa0
LW
3358 my $status = shift || 500;
3359 my $error = shift || "Internal server error";
3360
3361 my %http_responses = (400 => '400 Bad Request',
3362 403 => '403 Forbidden',
3363 404 => '404 Not Found',
3364 500 => '500 Internal Server Error');
3365 git_header_html($http_responses{$status});
59b9f61a
JN
3366 print <<EOF;
3367<div class="page_body">
3368<br /><br />
3369$status - $error
3370<br />
3371</div>
3372EOF
b87d78d6 3373 git_footer_html();
717b8311 3374 exit;
161332a5
KS
3375}
3376
717b8311
JN
3377## ----------------------------------------------------------------------
3378## functions printing or outputting HTML: navigation
3379
847e01fb 3380sub git_print_page_nav {
717b8311
JN
3381 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3382 $extra = '' if !defined $extra; # pager or formats
3383
3384 my @navs = qw(summary shortlog log commit commitdiff tree);
3385 if ($suppress) {
3386 @navs = grep { $_ ne $suppress } @navs;
3387 }
3388
1c2a4f5a 3389 my %arg = map { $_ => {action=>$_} } @navs;
717b8311
JN
3390 if (defined $head) {
3391 for (qw(commit commitdiff)) {
3be8e720 3392 $arg{$_}{'hash'} = $head;
717b8311
JN
3393 }
3394 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3395 for (qw(shortlog log)) {
3be8e720 3396 $arg{$_}{'hash'} = $head;
045e531a 3397 }
6a928415
KS
3398 }
3399 }
d627f68f 3400
3be8e720
JN
3401 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3402 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
717b8311 3403
a7c5a283 3404 my @actions = gitweb_get_feature('actions');
2b11e059
JN
3405 my %repl = (
3406 '%' => '%',
3407 'n' => $project, # project name
3408 'f' => $git_dir, # project path within filesystem
3409 'h' => $treehead || '', # current hash ('h' parameter)
3410 'b' => $treebase || '', # hash base ('hb' parameter)
3411 );
d627f68f 3412 while (@actions) {
2b11e059
JN
3413 my ($label, $link, $pos) = splice(@actions,0,3);
3414 # insert
d627f68f
PB
3415 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3416 # munch munch
2b11e059 3417 $link =~ s/%([%nfhb])/$repl{$1}/g;
d627f68f
PB
3418 $arg{$label}{'_href'} = $link;
3419 }
3420
717b8311
JN
3421 print "<div class=\"page_nav\">\n" .
3422 (join " | ",
1c2a4f5a 3423 map { $_ eq $current ?
d627f68f 3424 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
1c2a4f5a 3425 } @navs);
717b8311
JN
3426 print "<br/>\n$extra<br/>\n" .
3427 "</div>\n";
6a928415
KS
3428}
3429
847e01fb 3430sub format_paging_nav {
69ca37d2 3431 my ($action, $page, $has_next_link) = @_;
717b8311 3432 my $paging_nav;
594e212b 3433
717b8311 3434
717b8311 3435 if ($page > 0) {
69ca37d2
JN
3436 $paging_nav .=
3437 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
3438 " &sdot; " .
7afd77bf 3439 $cgi->a({-href => href(-replay=>1, page=>$page-1),
26298b5f 3440 -accesskey => "p", -title => "Alt-p"}, "prev");
717b8311 3441 } else {
69ca37d2 3442 $paging_nav .= "first &sdot; prev";
717b8311
JN
3443 }
3444
1f684dc0 3445 if ($has_next_link) {
717b8311 3446 $paging_nav .= " &sdot; " .
7afd77bf 3447 $cgi->a({-href => href(-replay=>1, page=>$page+1),
26298b5f 3448 -accesskey => "n", -title => "Alt-n"}, "next");
717b8311
JN
3449 } else {
3450 $paging_nav .= " &sdot; next";
594e212b 3451 }
717b8311
JN
3452
3453 return $paging_nav;
594e212b
JN
3454}
3455
717b8311
JN
3456## ......................................................................
3457## functions printing or outputting HTML: div
3458
847e01fb 3459sub git_print_header_div {
717b8311 3460 my ($action, $title, $hash, $hash_base) = @_;
1c2a4f5a 3461 my %args = ();
717b8311 3462
3be8e720
JN
3463 $args{'action'} = $action;
3464 $args{'hash'} = $hash if $hash;
3465 $args{'hash_base'} = $hash_base if $hash_base;
717b8311
JN
3466
3467 print "<div class=\"header\">\n" .
1c2a4f5a
MW
3468 $cgi->a({-href => href(%args), -class => "title"},
3469 $title ? $title : $action) .
3470 "\n</div>\n";
717b8311 3471}
ede5e100 3472
1c49a4e1
GB
3473sub print_local_time {
3474 my %date = @_;
3475 if ($date{'hour_local'} < 6) {
3476 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3477 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3478 } else {
3479 printf(" (%02d:%02d %s)",
3480 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3481 }
3482}
3483
3484# Outputs the author name and date in long form
6fd92a28
JN
3485sub git_print_authorship {
3486 my $co = shift;
1c49a4e1
GB
3487 my %opts = @_;
3488 my $tag = $opts{-tag} || 'div';
e133d65c 3489 my $author = $co->{'author_name'};
6fd92a28 3490
a44465cc 3491 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1c49a4e1 3492 print "<$tag class=\"author_date\">" .
e133d65c 3493 format_search_author($author, "author", esc_html($author)) .
a44465cc 3494 " [$ad{'rfc2822'}";
1c49a4e1 3495 print_local_time(%ad) if ($opts{-localtime});
e9fdd74e
GB
3496 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3497 . "</$tag>\n";
1c49a4e1
GB
3498}
3499
3500# Outputs table rows containing the full author or committer information,
3501# in the format expected for 'commit' view (& similia).
3502# Parameters are a commit hash reference, followed by the list of people
3503# to output information for. If the list is empty it defalts to both
3504# author and committer.
3505sub git_print_authorship_rows {
3506 my $co = shift;
3507 # too bad we can't use @people = @_ || ('author', 'committer')
3508 my @people = @_;
3509 @people = ('author', 'committer') unless @people;
3510 foreach my $who (@people) {
3511 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
e133d65c
SB
3512 print "<tr><td>$who</td><td>" .
3513 format_search_author($co->{"${who}_name"}, $who,
3514 esc_html($co->{"${who}_name"})) . " " .
3515 format_search_author($co->{"${who}_email"}, $who,
3516 esc_html("<" . $co->{"${who}_email"} . ">")) .
3517 "</td><td rowspan=\"2\">" .
e9fdd74e
GB
3518 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3519 "</td></tr>\n" .
1c49a4e1
GB
3520 "<tr>" .
3521 "<td></td><td> $wd{'rfc2822'}";
3522 print_local_time(%wd);
3523 print "</td>" .
3524 "</tr>\n";
a44465cc 3525 }
6fd92a28
JN
3526}
3527
717b8311
JN
3528sub git_print_page_path {
3529 my $name = shift;
3530 my $type = shift;
59fb1c94 3531 my $hb = shift;
ede5e100 3532
4df118ed
JN
3533
3534 print "<div class=\"page_path\">";
3535 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
00f429af 3536 -title => 'tree root'}, to_utf8("[$project]"));
4df118ed
JN
3537 print " / ";
3538 if (defined $name) {
762c7205
JN
3539 my @dirname = split '/', $name;
3540 my $basename = pop @dirname;
3541 my $fullname = '';
3542
762c7205 3543 foreach my $dir (@dirname) {
16fdb488 3544 $fullname .= ($fullname ? '/' : '') . $dir;
762c7205
JN
3545 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3546 hash_base=>$hb),
edc04e90 3547 -title => $fullname}, esc_path($dir));
26d0a976 3548 print " / ";
762c7205
JN
3549 }
3550 if (defined $type && $type eq 'blob') {
952c65fc 3551 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
762c7205 3552 hash_base=>$hb),
edc04e90 3553 -title => $name}, esc_path($basename));
762c7205
JN
3554 } elsif (defined $type && $type eq 'tree') {
3555 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3556 hash_base=>$hb),
edc04e90 3557 -title => $name}, esc_path($basename));
4df118ed 3558 print " / ";
59fb1c94 3559 } else {
403d0906 3560 print esc_path($basename);
59fb1c94 3561 }
ede5e100 3562 }
4df118ed 3563 print "<br/></div>\n";
ede5e100
KS
3564}
3565
74fd8728 3566sub git_print_log {
d16d093c 3567 my $log = shift;
b7f9253d 3568 my %opts = @_;
d16d093c 3569
b7f9253d
JN
3570 if ($opts{'-remove_title'}) {
3571 # remove title, i.e. first line of log
3572 shift @$log;
3573 }
d16d093c
JN
3574 # remove leading empty lines
3575 while (defined $log->[0] && $log->[0] eq "") {
3576 shift @$log;
3577 }
3578
3579 # print log
3580 my $signoff = 0;
3581 my $empty = 0;
3582 foreach my $line (@$log) {
b7f9253d
JN
3583 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3584 $signoff = 1;
fba20b42 3585 $empty = 0;
b7f9253d
JN
3586 if (! $opts{'-remove_signoff'}) {
3587 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3588 next;
3589 } else {
3590 # remove signoff lines
3591 next;
3592 }
3593 } else {
3594 $signoff = 0;
3595 }
3596
d16d093c
JN
3597 # print only one empty line
3598 # do not print empty line after signoff
3599 if ($line eq "") {
3600 next if ($empty || $signoff);
3601 $empty = 1;
3602 } else {
3603 $empty = 0;
3604 }
b7f9253d
JN
3605
3606 print format_log_line_html($line) . "<br/>\n";
3607 }
3608
3609 if ($opts{'-final_empty_line'}) {
3610 # end with single empty line
3611 print "<br/>\n" unless $empty;
d16d093c
JN
3612 }
3613}
3614
e33fba4c
JN
3615# return link target (what link points to)
3616sub git_get_link_target {
3617 my $hash = shift;
3618 my $link_target;
3619
3620 # read link
3621 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3622 or return;
3623 {
34122b57 3624 local $/ = undef;
e33fba4c
JN
3625 $link_target = <$fd>;
3626 }
3627 close $fd
3628 or return;
3629
3630 return $link_target;
3631}
3632
3bf9d570
JN
3633# given link target, and the directory (basedir) the link is in,
3634# return target of link relative to top directory (top tree);
3635# return undef if it is not possible (including absolute links).
3636sub normalize_link_target {
15c54fe7 3637 my ($link_target, $basedir) = @_;
3bf9d570
JN
3638
3639 # absolute symlinks (beginning with '/') cannot be normalized
3640 return if (substr($link_target, 0, 1) eq '/');
3641
3642 # normalize link target to path from top (root) tree (dir)
3643 my $path;
3644 if ($basedir) {
3645 $path = $basedir . '/' . $link_target;
3646 } else {
3647 # we are in top (root) tree (dir)
3648 $path = $link_target;
3649 }
3650
3651 # remove //, /./, and /../
3652 my @path_parts;
3653 foreach my $part (split('/', $path)) {
3654 # discard '.' and ''
3655 next if (!$part || $part eq '.');
3656 # handle '..'
3657 if ($part eq '..') {
3658 if (@path_parts) {
3659 pop @path_parts;
3660 } else {
3661 # link leads outside repository (outside top dir)
3662 return;
3663 }
3664 } else {
3665 push @path_parts, $part;
3666 }
3667 }
3668 $path = join('/', @path_parts);
3669
3670 return $path;
3671}
e33fba4c 3672
fa702003
JN
3673# print tree entry (row of git_tree), but without encompassing <tr> element
3674sub git_print_tree_entry {
3675 my ($t, $basedir, $hash_base, $have_blame) = @_;
3676
3677 my %base_key = ();
e33fba4c 3678 $base_key{'hash_base'} = $hash_base if defined $hash_base;
fa702003 3679
4de741b3
LT
3680 # The format of a table row is: mode list link. Where mode is
3681 # the mode of the entry, list is the name of the entry, an href,
3682 # and link is the action links of the entry.
3683
fa702003 3684 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
e4b48eaa
JN
3685 if (exists $t->{'size'}) {
3686 print "<td class=\"size\">$t->{'size'}</td>\n";
3687 }
fa702003
JN
3688 if ($t->{'type'} eq "blob") {
3689 print "<td class=\"list\">" .
4de741b3 3690 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
e7fb022a 3691 file_name=>"$basedir$t->{'name'}", %base_key),
e33fba4c
JN
3692 -class => "list"}, esc_path($t->{'name'}));
3693 if (S_ISLNK(oct $t->{'mode'})) {
3694 my $link_target = git_get_link_target($t->{'hash'});
3695 if ($link_target) {
15c54fe7 3696 my $norm_target = normalize_link_target($link_target, $basedir);
3bf9d570
JN
3697 if (defined $norm_target) {
3698 print " -> " .
3699 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3700 file_name=>$norm_target),
3701 -title => $norm_target}, esc_path($link_target));
3702 } else {
3703 print " -> " . esc_path($link_target);
3704 }
e33fba4c
JN
3705 }
3706 }
3707 print "</td>\n";
4de741b3 3708 print "<td class=\"link\">";
4777b014 3709 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
e33fba4c
JN
3710 file_name=>"$basedir$t->{'name'}", %base_key)},
3711 "blob");
fa702003 3712 if ($have_blame) {
4777b014
PB
3713 print " | " .
3714 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
e33fba4c
JN
3715 file_name=>"$basedir$t->{'name'}", %base_key)},
3716 "blame");
fa702003
JN
3717 }
3718 if (defined $hash_base) {
4777b014
PB
3719 print " | " .
3720 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
fa702003
JN
3721 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3722 "history");
3723 }
3724 print " | " .
6f7ea5fb 3725 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
e7fb022a
JN
3726 file_name=>"$basedir$t->{'name'}")},
3727 "raw");
4de741b3 3728 print "</td>\n";
fa702003
JN
3729
3730 } elsif ($t->{'type'} eq "tree") {
0fa105e7
LT
3731 print "<td class=\"list\">";
3732 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
e4b48eaa
JN
3733 file_name=>"$basedir$t->{'name'}",
3734 %base_key)},
403d0906 3735 esc_path($t->{'name'}));
0fa105e7
LT
3736 print "</td>\n";
3737 print "<td class=\"link\">";
4777b014 3738 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
e4b48eaa
JN
3739 file_name=>"$basedir$t->{'name'}",
3740 %base_key)},
e33fba4c 3741 "tree");
fa702003 3742 if (defined $hash_base) {
4777b014
PB
3743 print " | " .
3744 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
fa702003 3745 file_name=>"$basedir$t->{'name'}")},
01ac1e38
JN
3746 "history");
3747 }
3748 print "</td>\n";
3749 } else {
3750 # unknown object: we can only present history for it
3751 # (this includes 'commit' object, i.e. submodule support)
3752 print "<td class=\"list\">" .
3753 esc_path($t->{'name'}) .
3754 "</td>\n";
3755 print "<td class=\"link\">";
3756 if (defined $hash_base) {
3757 print $cgi->a({-href => href(action=>"history",
3758 hash_base=>$hash_base,
3759 file_name=>"$basedir$t->{'name'}")},
fa702003
JN
3760 "history");
3761 }
3762 print "</td>\n";
3763 }
3764}
3765
717b8311
JN
3766## ......................................................................
3767## functions printing large fragments of HTML
3768
0cec6db5 3769# get pre-image filenames for merge (combined) diff
e72c0eaf
JN
3770sub fill_from_file_info {
3771 my ($diff, @parents) = @_;
3772
3773 $diff->{'from_file'} = [ ];
3774 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3775 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3776 if ($diff->{'status'}[$i] eq 'R' ||
3777 $diff->{'status'}[$i] eq 'C') {
3778 $diff->{'from_file'}[$i] =
3779 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3780 }
3781 }
3782
3783 return $diff;
3784}
3785
0cec6db5 3786# is current raw difftree line of file deletion
90921740
JN
3787sub is_deleted {
3788 my $diffinfo = shift;
3789
4ed4a347 3790 return $diffinfo->{'to_id'} eq ('0' x 40);
90921740 3791}
e72c0eaf 3792
0cec6db5
JN
3793# does patch correspond to [previous] difftree raw line
3794# $diffinfo - hashref of parsed raw diff format
3795# $patchinfo - hashref of parsed patch diff format
3796# (the same keys as in $diffinfo)
3797sub is_patch_split {
3798 my ($diffinfo, $patchinfo) = @_;
3799
3800 return defined $diffinfo && defined $patchinfo
9d301456 3801 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
0cec6db5
JN
3802}
3803
3804
4a4a1a53 3805sub git_difftree_body {
ed224dea
JN
3806 my ($difftree, $hash, @parents) = @_;
3807 my ($parent) = $parents[0];
25b2790f 3808 my $have_blame = gitweb_check_feature('blame');
4a4a1a53
JN
3809 print "<div class=\"list_head\">\n";
3810 if ($#{$difftree} > 10) {
3811 print(($#{$difftree} + 1) . " files changed:\n");
3812 }
3813 print "</div>\n";
3814
ed224dea
JN
3815 print "<table class=\"" .
3816 (@parents > 1 ? "combined " : "") .
3817 "diff_tree\">\n";
47598d7a
JN
3818
3819 # header only for combined diff in 'commitdiff' view
3ef408ae 3820 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
47598d7a
JN
3821 if ($has_header) {
3822 # table header
3823 print "<thead><tr>\n" .
3824 "<th></th><th></th>\n"; # filename, patchN link
3825 for (my $i = 0; $i < @parents; $i++) {
3826 my $par = $parents[$i];
3827 print "<th>" .
3828 $cgi->a({-href => href(action=>"commitdiff",
3829 hash=>$hash, hash_parent=>$par),
3830 -title => 'commitdiff to parent number ' .
3831 ($i+1) . ': ' . substr($par,0,7)},
3832 $i+1) .
3833 "&nbsp;</th>\n";
3834 }
3835 print "</tr></thead>\n<tbody>\n";
3836 }
3837
6dd36acd 3838 my $alternate = 1;
b4657e77 3839 my $patchno = 0;
4a4a1a53 3840 foreach my $line (@{$difftree}) {
0cec6db5 3841 my $diff = parsed_difftree_line($line);
4a4a1a53
JN
3842
3843 if ($alternate) {
3844 print "<tr class=\"dark\">\n";
3845 } else {
3846 print "<tr class=\"light\">\n";
3847 }
3848 $alternate ^= 1;
3849
493e01db 3850 if (exists $diff->{'nparents'}) { # combined diff
ed224dea 3851
493e01db
JN
3852 fill_from_file_info($diff, @parents)
3853 unless exists $diff->{'from_file'};
e72c0eaf 3854
90921740 3855 if (!is_deleted($diff)) {
ed224dea
JN
3856 # file exists in the result (child) commit
3857 print "<td>" .
493e01db
JN
3858 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3859 file_name=>$diff->{'to_file'},
ed224dea 3860 hash_base=>$hash),
493e01db 3861 -class => "list"}, esc_path($diff->{'to_file'})) .
ed224dea
JN
3862 "</td>\n";
3863 } else {
3864 print "<td>" .
493e01db 3865 esc_path($diff->{'to_file'}) .
ed224dea
JN
3866 "</td>\n";
3867 }
3868
3869 if ($action eq 'commitdiff') {
3870 # link to patch
3871 $patchno++;
3872 print "<td class=\"link\">" .
3873 $cgi->a({-href => "#patch$patchno"}, "patch") .
3874 " | " .
3875 "</td>\n";
3876 }
3877
3878 my $has_history = 0;
3879 my $not_deleted = 0;
493e01db 3880 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
ed224dea 3881 my $hash_parent = $parents[$i];
493e01db
JN
3882 my $from_hash = $diff->{'from_id'}[$i];
3883 my $from_path = $diff->{'from_file'}[$i];
3884 my $status = $diff->{'status'}[$i];
ed224dea
JN
3885
3886 $has_history ||= ($status ne 'A');
3887 $not_deleted ||= ($status ne 'D');
3888
ed224dea
JN
3889 if ($status eq 'A') {
3890 print "<td class=\"link\" align=\"right\"> | </td>\n";
3891 } elsif ($status eq 'D') {
3892 print "<td class=\"link\">" .
3893 $cgi->a({-href => href(action=>"blob",
3894 hash_base=>$hash,
3895 hash=>$from_hash,
3896 file_name=>$from_path)},
3897 "blob" . ($i+1)) .
3898 " | </td>\n";
3899 } else {
493e01db 3900 if ($diff->{'to_id'} eq $from_hash) {
ed224dea
JN
3901 print "<td class=\"link nochange\">";
3902 } else {
3903 print "<td class=\"link\">";
3904 }
3905 print $cgi->a({-href => href(action=>"blobdiff",
493e01db 3906 hash=>$diff->{'to_id'},
ed224dea
JN
3907 hash_parent=>$from_hash,
3908 hash_base=>$hash,
3909 hash_parent_base=>$hash_parent,
493e01db 3910 file_name=>$diff->{'to_file'},
ed224dea
JN
3911 file_parent=>$from_path)},
3912 "diff" . ($i+1)) .
3913 " | </td>\n";
3914 }
3915 }
3916
3917 print "<td class=\"link\">";
3918 if ($not_deleted) {
3919 print $cgi->a({-href => href(action=>"blob",
493e01db
JN
3920 hash=>$diff->{'to_id'},
3921 file_name=>$diff->{'to_file'},
ed224dea
JN
3922 hash_base=>$hash)},
3923 "blob");
3924 print " | " if ($has_history);
3925 }
3926 if ($has_history) {
3927 print $cgi->a({-href => href(action=>"history",
493e01db 3928 file_name=>$diff->{'to_file'},
ed224dea
JN
3929 hash_base=>$hash)},
3930 "history");
3931 }
3932 print "</td>\n";
3933
3934 print "</tr>\n";
3935 next; # instead of 'else' clause, to avoid extra indent
3936 }
3937 # else ordinary diff
3938
e8e41a93
JN
3939 my ($to_mode_oct, $to_mode_str, $to_file_type);
3940 my ($from_mode_oct, $from_mode_str, $from_file_type);
493e01db
JN
3941 if ($diff->{'to_mode'} ne ('0' x 6)) {
3942 $to_mode_oct = oct $diff->{'to_mode'};
e8e41a93
JN
3943 if (S_ISREG($to_mode_oct)) { # only for regular file
3944 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3945 }
493e01db 3946 $to_file_type = file_type($diff->{'to_mode'});
e8e41a93 3947 }
493e01db
JN
3948 if ($diff->{'from_mode'} ne ('0' x 6)) {
3949 $from_mode_oct = oct $diff->{'from_mode'};
e8e41a93
JN
3950 if (S_ISREG($to_mode_oct)) { # only for regular file
3951 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4a4a1a53 3952 }
493e01db 3953 $from_file_type = file_type($diff->{'from_mode'});
e8e41a93
JN
3954 }
3955
493e01db 3956 if ($diff->{'status'} eq "A") { # created
e8e41a93
JN
3957 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3958 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3959 $mode_chng .= "]</span>";
499faeda 3960 print "<td>";
493e01db
JN
3961 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3962 hash_base=>$hash, file_name=>$diff->{'file'}),
3963 -class => "list"}, esc_path($diff->{'file'}));
499faeda
LT
3964 print "</td>\n";
3965 print "<td>$mode_chng</td>\n";
3966 print "<td class=\"link\">";
72dbafa1 3967 if ($action eq 'commitdiff') {
b4657e77
JN
3968 # link to patch
3969 $patchno++;
499faeda 3970 print $cgi->a({-href => "#patch$patchno"}, "patch");
897d1d2e 3971 print " | ";
b4657e77 3972 }
493e01db
JN
3973 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3974 hash_base=>$hash, file_name=>$diff->{'file'})},
3faa541f 3975 "blob");
b4657e77 3976 print "</td>\n";
4a4a1a53 3977
493e01db 3978 } elsif ($diff->{'status'} eq "D") { # deleted
e8e41a93 3979 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
499faeda 3980 print "<td>";
493e01db
JN
3981 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3982 hash_base=>$parent, file_name=>$diff->{'file'}),
3983 -class => "list"}, esc_path($diff->{'file'}));
499faeda
LT
3984 print "</td>\n";
3985 print "<td>$mode_chng</td>\n";
3986 print "<td class=\"link\">";
72dbafa1 3987 if ($action eq 'commitdiff') {
b4657e77
JN
3988 # link to patch
3989 $patchno++;
499faeda
LT
3990 print $cgi->a({-href => "#patch$patchno"}, "patch");
3991 print " | ";
b4657e77 3992 }
493e01db
JN
3993 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3994 hash_base=>$parent, file_name=>$diff->{'file'})},
897d1d2e 3995 "blob") . " | ";
2b2a8c78 3996 if ($have_blame) {
897d1d2e 3997 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
493e01db 3998 file_name=>$diff->{'file'})},
897d1d2e 3999 "blame") . " | ";
2b2a8c78 4000 }
b4657e77 4001 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
493e01db 4002 file_name=>$diff->{'file'})},
e7fb022a 4003 "history");
499faeda 4004 print "</td>\n";
4a4a1a53 4005
493e01db 4006 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4a4a1a53 4007 my $mode_chnge = "";
493e01db 4008 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
e8e41a93 4009 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
6e72cf43 4010 if ($from_file_type ne $to_file_type) {
e8e41a93 4011 $mode_chnge .= " from $from_file_type to $to_file_type";
4a4a1a53 4012 }
e8e41a93
JN
4013 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4014 if ($from_mode_str && $to_mode_str) {
4015 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4016 } elsif ($to_mode_str) {
4017 $mode_chnge .= " mode: $to_mode_str";
4a4a1a53
JN
4018 }
4019 }
4020 $mode_chnge .= "]</span>\n";
4021 }
4022 print "<td>";
493e01db
JN
4023 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4024 hash_base=>$hash, file_name=>$diff->{'file'}),
4025 -class => "list"}, esc_path($diff->{'file'}));
499faeda
LT
4026 print "</td>\n";
4027 print "<td>$mode_chnge</td>\n";
4028 print "<td class=\"link\">";
241cc599
JN
4029 if ($action eq 'commitdiff') {
4030 # link to patch
4031 $patchno++;
4032 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4033 " | ";
493e01db 4034 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
241cc599
JN
4035 # "commit" view and modified file (not onlu mode changed)
4036 print $cgi->a({-href => href(action=>"blobdiff",
493e01db 4037 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
241cc599 4038 hash_base=>$hash, hash_parent_base=>$parent,
493e01db 4039 file_name=>$diff->{'file'})},
241cc599
JN
4040 "diff") .
4041 " | ";
4a4a1a53 4042 }
493e01db
JN
4043 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4044 hash_base=>$hash, file_name=>$diff->{'file'})},
897d1d2e 4045 "blob") . " | ";
2b2a8c78 4046 if ($have_blame) {
897d1d2e 4047 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
493e01db 4048 file_name=>$diff->{'file'})},
897d1d2e 4049 "blame") . " | ";
2b2a8c78 4050 }
eb51ec9c 4051 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
493e01db 4052 file_name=>$diff->{'file'})},
e7fb022a 4053 "history");
4a4a1a53
JN
4054 print "</td>\n";
4055
493e01db 4056 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
e8e41a93 4057 my %status_name = ('R' => 'moved', 'C' => 'copied');
493e01db 4058 my $nstatus = $status_name{$diff->{'status'}};
4a4a1a53 4059 my $mode_chng = "";
493e01db 4060 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
e8e41a93
JN
4061 # mode also for directories, so we cannot use $to_mode_str
4062 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4a4a1a53
JN
4063 }
4064 print "<td>" .
e8e41a93 4065 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
493e01db
JN
4066 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4067 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
e8e41a93
JN
4068 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4069 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
493e01db
JN
4070 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4071 -class => "list"}, esc_path($diff->{'from_file'})) .
4072 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
499faeda 4073 "<td class=\"link\">";
241cc599
JN
4074 if ($action eq 'commitdiff') {
4075 # link to patch
4076 $patchno++;
4077 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4078 " | ";
493e01db 4079 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
241cc599
JN
4080 # "commit" view and modified file (not only pure rename or copy)
4081 print $cgi->a({-href => href(action=>"blobdiff",
493e01db 4082 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
241cc599 4083 hash_base=>$hash, hash_parent_base=>$parent,
493e01db 4084 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
241cc599
JN
4085 "diff") .
4086 " | ";
4a4a1a53 4087 }
493e01db
JN
4088 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4089 hash_base=>$parent, file_name=>$diff->{'to_file'})},
897d1d2e 4090 "blob") . " | ";
2b2a8c78 4091 if ($have_blame) {
897d1d2e 4092 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
493e01db 4093 file_name=>$diff->{'to_file'})},
897d1d2e 4094 "blame") . " | ";
2b2a8c78 4095 }
897d1d2e 4096 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
493e01db 4097 file_name=>$diff->{'to_file'})},
e7fb022a 4098 "history");
4a4a1a53 4099 print "</td>\n";
e8e41a93 4100
4a4a1a53
JN
4101 } # we should not encounter Unmerged (U) or Unknown (X) status
4102 print "</tr>\n";
4103 }
47598d7a 4104 print "</tbody>" if $has_header;
4a4a1a53
JN
4105 print "</table>\n";
4106}
4107
eee08903 4108sub git_patchset_body {
e72c0eaf
JN
4109 my ($fd, $difftree, $hash, @hash_parents) = @_;
4110 my ($hash_parent) = $hash_parents[0];
eee08903 4111
0cec6db5 4112 my $is_combined = (@hash_parents > 1);
eee08903 4113 my $patch_idx = 0;
4280cde9 4114 my $patch_number = 0;
6d55f055 4115 my $patch_line;
fe87585e 4116 my $diffinfo;
0cec6db5 4117 my $to_name;
744d0ac3 4118 my (%from, %to);
eee08903
JN
4119
4120 print "<div class=\"patchset\">\n";
4121
6d55f055
JN
4122 # skip to first patch
4123 while ($patch_line = <$fd>) {
157e43b4 4124 chomp $patch_line;
eee08903 4125
6d55f055
JN
4126 last if ($patch_line =~ m/^diff /);
4127 }
4128
4129 PATCH:
4130 while ($patch_line) {
6d55f055 4131
0cec6db5
JN
4132 # parse "git diff" header line
4133 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4134 # $1 is from_name, which we do not use
4135 $to_name = unquote($2);
4136 $to_name =~ s!^b/!!;
4137 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4138 # $1 is 'cc' or 'combined', which we do not use
4139 $to_name = unquote($2);
4140 } else {
4141 $to_name = undef;
6d55f055 4142 }
6d55f055
JN
4143
4144 # check if current patch belong to current raw line
4145 # and parse raw git-diff line if needed
0cec6db5 4146 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
2206537c 4147 # this is continuation of a split patch
6d55f055
JN
4148 print "<div class=\"patch cont\">\n";
4149 } else {
4150 # advance raw git-diff output if needed
4151 $patch_idx++ if defined $diffinfo;
eee08903 4152
0cec6db5
JN
4153 # read and prepare patch information
4154 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
cd030c3a 4155
0cec6db5
JN
4156 # compact combined diff output can have some patches skipped
4157 # find which patch (using pathname of result) we are at now;
4158 if ($is_combined) {
4159 while ($to_name ne $diffinfo->{'to_file'}) {
cd030c3a
JN
4160 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4161 format_diff_cc_simplified($diffinfo, @hash_parents) .
4162 "</div>\n"; # class="patch"
4163
4164 $patch_idx++;
4165 $patch_number++;
0cec6db5
JN
4166
4167 last if $patch_idx > $#$difftree;
4168 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
cd030c3a 4169 }
0cec6db5 4170 }
711fa742 4171
90921740
JN
4172 # modifies %from, %to hashes
4173 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
5f855052 4174
6d55f055
JN
4175 # this is first patch for raw difftree line with $patch_idx index
4176 # we index @$difftree array from 0, but number patches from 1
4177 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
744d0ac3 4178 }
eee08903 4179
0cec6db5
JN
4180 # git diff header
4181 #assert($patch_line =~ m/^diff /) if DEBUG;
4182 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4183 $patch_number++;
6d55f055 4184 # print "git diff" header
90921740
JN
4185 print format_git_diff_header_line($patch_line, $diffinfo,
4186 \%from, \%to);
6d55f055
JN
4187
4188 # print extended diff header
0cec6db5 4189 print "<div class=\"diff extended_header\">\n";
6d55f055 4190 EXTENDED_HEADER:
0cec6db5
JN
4191 while ($patch_line = <$fd>) {
4192 chomp $patch_line;
4193
4194 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4195
90921740
JN
4196 print format_extended_diff_header_line($patch_line, $diffinfo,
4197 \%from, \%to);
6d55f055 4198 }
0cec6db5 4199 print "</div>\n"; # class="diff extended_header"
6d55f055
JN
4200
4201 # from-file/to-file diff header
0bdb28c9
JN
4202 if (! $patch_line) {
4203 print "</div>\n"; # class="patch"
4204 last PATCH;
4205 }
66399eff 4206 next PATCH if ($patch_line =~ m/^diff /);
6d55f055 4207 #assert($patch_line =~ m/^---/) if DEBUG;
744d0ac3 4208
0cec6db5 4209 my $last_patch_line = $patch_line;
6d55f055 4210 $patch_line = <$fd>;
6d55f055 4211 chomp $patch_line;
90921740 4212 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
e4e4f825 4213
90921740 4214 print format_diff_from_to_header($last_patch_line, $patch_line,
91af4ce4
JN
4215 $diffinfo, \%from, \%to,
4216 @hash_parents);
e4e4f825 4217
6d55f055
JN
4218 # the patch itself
4219 LINE:
4220 while ($patch_line = <$fd>) {
4221 chomp $patch_line;
e4e4f825 4222
6d55f055 4223 next PATCH if ($patch_line =~ m/^diff /);
e4e4f825 4224
59e3b14e 4225 print format_diff_line($patch_line, \%from, \%to);
eee08903 4226 }
eee08903 4227
6d55f055
JN
4228 } continue {
4229 print "</div>\n"; # class="patch"
eee08903 4230 }
d26c4264 4231
cd030c3a
JN
4232 # for compact combined (--cc) format, with chunk and patch simpliciaction
4233 # patchset might be empty, but there might be unprocessed raw lines
0cec6db5 4234 for (++$patch_idx if $patch_number > 0;
cd030c3a 4235 $patch_idx < @$difftree;
0cec6db5 4236 ++$patch_idx) {
cd030c3a 4237 # read and prepare patch information
0cec6db5 4238 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
cd030c3a
JN
4239
4240 # generate anchor for "patch" links in difftree / whatchanged part
4241 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4242 format_diff_cc_simplified($diffinfo, @hash_parents) .
4243 "</div>\n"; # class="patch"
4244
4245 $patch_number++;
4246 }
4247
d26c4264
JN
4248 if ($patch_number == 0) {
4249 if (@hash_parents > 1) {
4250 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4251 } else {
4252 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4253 }
4254 }
eee08903
JN
4255
4256 print "</div>\n"; # class="patchset"
4257}
4258
4259# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4260
69913415
JN
4261# fills project list info (age, description, owner, forks) for each
4262# project in the list, removing invalid projects from returned list
4263# NOTE: modifies $projlist, but does not remove entries from it
4264sub fill_project_list_info {
4265 my ($projlist, $check_forks) = @_;
e30496df 4266 my @projects;
69913415 4267
25b2790f 4268 my $show_ctags = gitweb_check_feature('ctags');
69913415 4269 PROJECT:
e30496df 4270 foreach my $pr (@$projlist) {
69913415
JN
4271 my (@activity) = git_get_last_activity($pr->{'path'});
4272 unless (@activity) {
4273 next PROJECT;
e30496df 4274 }
69913415 4275 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
e30496df
PB
4276 if (!defined $pr->{'descr'}) {
4277 my $descr = git_get_project_description($pr->{'path'}) || "";
69913415
JN
4278 $descr = to_utf8($descr);
4279 $pr->{'descr_long'} = $descr;
55feb120 4280 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
e30496df
PB
4281 }
4282 if (!defined $pr->{'owner'}) {
76e4f5d0 4283 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
e30496df
PB
4284 }
4285 if ($check_forks) {
4286 my $pname = $pr->{'path'};
83ee94c1
JH
4287 if (($pname =~ s/\.git$//) &&
4288 ($pname !~ /\/$/) &&
4289 (-d "$projectroot/$pname")) {
4290 $pr->{'forks'} = "-d $projectroot/$pname";
3278fbc5 4291 } else {
83ee94c1
JH
4292 $pr->{'forks'} = 0;
4293 }
e30496df 4294 }
aed93de4 4295 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
e30496df
PB
4296 push @projects, $pr;
4297 }
4298
69913415
JN
4299 return @projects;
4300}
4301
6b28da67
PB
4302# print 'sort by' <th> element, generating 'sort by $name' replay link
4303# if that order is not selected
7da0f3a4 4304sub print_sort_th {
6b28da67 4305 my ($name, $order, $header) = @_;
7da0f3a4
JN
4306 $header ||= ucfirst($name);
4307
4308 if ($order eq $name) {
7da0f3a4
JN
4309 print "<th>$header</th>\n";
4310 } else {
4311 print "<th>" .
4312 $cgi->a({-href => href(-replay=>1, order=>$name),
4313 -class => "header"}, $header) .
4314 "</th>\n";
4315 }
4316}
4317
69913415 4318sub git_project_list_body {
42326110 4319 # actually uses global variable $project
69913415
JN
4320 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4321
25b2790f 4322 my $check_forks = gitweb_check_feature('forks');
69913415
JN
4323 my @projects = fill_project_list_info($projlist, $check_forks);
4324
b06dcf8c 4325 $order ||= $default_projects_order;
e30496df
PB
4326 $from = 0 unless defined $from;
4327 $to = $#projects if (!defined $to || $#projects < $to);
4328
6b28da67
PB
4329 my %order_info = (
4330 project => { key => 'path', type => 'str' },
4331 descr => { key => 'descr_long', type => 'str' },
4332 owner => { key => 'owner', type => 'str' },
4333 age => { key => 'age', type => 'num' }
4334 );
4335 my $oi = $order_info{$order};
4336 if ($oi->{'type'} eq 'str') {
4337 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4338 } else {
4339 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4340 }
4341
25b2790f 4342 my $show_ctags = gitweb_check_feature('ctags');
aed93de4
PB
4343 if ($show_ctags) {
4344 my %ctags;
4345 foreach my $p (@projects) {
4346 foreach my $ct (keys %{$p->{'ctags'}}) {
4347 $ctags{$ct} += $p->{'ctags'}->{$ct};
4348 }
4349 }
4350 my $cloud = git_populate_project_tagcloud(\%ctags);
4351 print git_show_project_tagcloud($cloud, 64);
4352 }
4353
e30496df
PB
4354 print "<table class=\"project_list\">\n";
4355 unless ($no_header) {
4356 print "<tr>\n";
4357 if ($check_forks) {
4358 print "<th></th>\n";
4359 }
6b28da67
PB
4360 print_sort_th('project', $order, 'Project');
4361 print_sort_th('descr', $order, 'Description');
4362 print_sort_th('owner', $order, 'Owner');
4363 print_sort_th('age', $order, 'Last Change');
7da0f3a4 4364 print "<th></th>\n" . # for links
e30496df
PB
4365 "</tr>\n";
4366 }
4367 my $alternate = 1;
aed93de4 4368 my $tagfilter = $cgi->param('by_tag');
e30496df
PB
4369 for (my $i = $from; $i <= $to; $i++) {
4370 my $pr = $projects[$i];
42326110 4371
aed93de4 4372 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
0d1d154d
PB
4373 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4374 and not $pr->{'descr_long'} =~ /$searchtext/;
4375 # Weed out forks or non-matching entries of search
42326110
PB
4376 if ($check_forks) {
4377 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4378 $forkbase="^$forkbase" if $forkbase;
0d1d154d
PB
4379 next if not $searchtext and not $tagfilter and $show_ctags
4380 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
42326110
PB
4381 }
4382
e30496df
PB
4383 if ($alternate) {
4384 print "<tr class=\"dark\">\n";
4385 } else {
4386 print "<tr class=\"light\">\n";
4387 }
4388 $alternate ^= 1;
4389 if ($check_forks) {
4390 print "<td>";
4391 if ($pr->{'forks'}) {
83ee94c1 4392 print "<!-- $pr->{'forks'} -->\n";
e30496df
PB
4393 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4394 }
4395 print "</td>\n";
4396 }
4397 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4398 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
e88ce8a4
JN
4399 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4400 -class => "list", -title => $pr->{'descr_long'}},
4401 esc_html($pr->{'descr'})) . "</td>\n" .
d3cd2495 4402 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
e30496df 4403 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
785cdea9 4404 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
e30496df
PB
4405 "<td class=\"link\">" .
4406 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
faa1bbfd 4407 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
e30496df
PB
4408 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4409 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4410 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4411 "</td>\n" .
4412 "</tr>\n";
4413 }
4414 if (defined $extra) {
4415 print "<tr>\n";
4416 if ($check_forks) {
4417 print "<td></td>\n";
4418 }
4419 print "<td colspan=\"5\">$extra</td>\n" .
4420 "</tr>\n";
4421 }
4422 print "</table>\n";
4423}
4424
42671caa
JN
4425sub git_log_body {
4426 # uses global variable $project
4427 my ($commitlist, $from, $to, $refs, $extra) = @_;
4428
4429 $from = 0 unless defined $from;
4430 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4431
4432 for (my $i = 0; $i <= $to; $i++) {
4433 my %co = %{$commitlist->[$i]};
4434 next if !%co;
4435 my $commit = $co{'id'};
4436 my $ref = format_ref_marker($refs, $commit);
4437 my %ad = parse_date($co{'author_epoch'});
4438 git_print_header_div('commit',
4439 "<span class=\"age\">$co{'age_string'}</span>" .
4440 esc_html($co{'title'}) . $ref,
4441 $commit);
4442 print "<div class=\"title_text\">\n" .
4443 "<div class=\"log_link\">\n" .
4444 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4445 " | " .
4446 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4447 " | " .
4448 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4449 "<br/>\n" .
4450 "</div>\n";
4451 git_print_authorship(\%co, -tag => 'span');
4452 print "<br/>\n</div>\n";
4453
4454 print "<div class=\"log_body\">\n";
4455 git_print_log($co{'comment'}, -final_empty_line=> 1);
4456 print "</div>\n";
4457 }
4458 if ($extra) {
4459 print "<div class=\"page_nav\">\n";
4460 print "$extra\n";
4461 print "</div>\n";
4462 }
4463}
4464
9f5dcb81
JN
4465sub git_shortlog_body {
4466 # uses global variable $project
190d7fdc 4467 my ($commitlist, $from, $to, $refs, $extra) = @_;
ddb8d900 4468
9f5dcb81 4469 $from = 0 unless defined $from;
190d7fdc 4470 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
9f5dcb81 4471
591ebf65 4472 print "<table class=\"shortlog\">\n";
6dd36acd 4473 my $alternate = 1;
9f5dcb81 4474 for (my $i = $from; $i <= $to; $i++) {
190d7fdc
RF
4475 my %co = %{$commitlist->[$i]};
4476 my $commit = $co{'id'};
847e01fb 4477 my $ref = format_ref_marker($refs, $commit);
9f5dcb81
JN
4478 if ($alternate) {
4479 print "<tr class=\"dark\">\n";
4480 } else {
4481 print "<tr class=\"light\">\n";
4482 }
4483 $alternate ^= 1;
4484 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4485 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1c49a4e1 4486 format_author_html('td', \%co, 10) . "<td>";
952c65fc
JN
4487 print format_subject_html($co{'title'}, $co{'title_short'},
4488 href(action=>"commit", hash=>$commit), $ref);
9f5dcb81
JN
4489 print "</td>\n" .
4490 "<td class=\"link\">" .
4777b014 4491 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
35749ae5 4492 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
55ff35cb 4493 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
a3c8ab30
MM
4494 my $snapshot_links = format_snapshot_links($commit);
4495 if (defined $snapshot_links) {
4496 print " | " . $snapshot_links;
55ff35cb 4497 }
cb9c6e5b 4498 print "</td>\n" .
9f5dcb81
JN
4499 "</tr>\n";
4500 }
4501 if (defined $extra) {
4502 print "<tr>\n" .
4503 "<td colspan=\"4\">$extra</td>\n" .
4504 "</tr>\n";
4505 }
4506 print "</table>\n";
4507}
4508
581860e1
JN
4509sub git_history_body {
4510 # Warning: assumes constant type (blob or tree) during history
69ca37d2
JN
4511 my ($commitlist, $from, $to, $refs, $extra,
4512 $file_name, $file_hash, $ftype) = @_;
8be68352
JN
4513
4514 $from = 0 unless defined $from;
a8b983bf 4515 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
581860e1 4516
591ebf65 4517 print "<table class=\"history\">\n";
6dd36acd 4518 my $alternate = 1;
8be68352 4519 for (my $i = $from; $i <= $to; $i++) {
a8b983bf 4520 my %co = %{$commitlist->[$i]};
581860e1
JN
4521 if (!%co) {
4522 next;
4523 }
a8b983bf 4524 my $commit = $co{'id'};
581860e1
JN
4525
4526 my $ref = format_ref_marker($refs, $commit);
4527
4528 if ($alternate) {
4529 print "<tr class=\"dark\">\n";
4530 } else {
4531 print "<tr class=\"light\">\n";
4532 }
4533 $alternate ^= 1;
4534 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1c49a4e1
GB
4535 # shortlog: format_author_html('td', \%co, 10)
4536 format_author_html('td', \%co, 15, 3) . "<td>";
581860e1 4537 # originally git_history used chop_str($co{'title'}, 50)
952c65fc
JN
4538 print format_subject_html($co{'title'}, $co{'title_short'},
4539 href(action=>"commit", hash=>$commit), $ref);
581860e1
JN
4540 print "</td>\n" .
4541 "<td class=\"link\">" .
6d81c5a2
LT
4542 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4543 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
581860e1
JN
4544
4545 if ($ftype eq 'blob') {
69ca37d2 4546 my $blob_current = $file_hash;
581860e1
JN
4547 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4548 if (defined $blob_current && defined $blob_parent &&
4549 $blob_current ne $blob_parent) {
4550 print " | " .
420e92f2
JN
4551 $cgi->a({-href => href(action=>"blobdiff",
4552 hash=>$blob_current, hash_parent=>$blob_parent,
4553 hash_base=>$hash_base, hash_parent_base=>$commit,
4554 file_name=>$file_name)},
581860e1
JN
4555 "diff to current");
4556 }
4557 }
4558 print "</td>\n" .
4559 "</tr>\n";
4560 }
4561 if (defined $extra) {
4562 print "<tr>\n" .
4563 "<td colspan=\"4\">$extra</td>\n" .
4564 "</tr>\n";
4565 }
4566 print "</table>\n";
4567}
4568
717b8311
JN
4569sub git_tags_body {
4570 # uses global variable $project
4571 my ($taglist, $from, $to, $extra) = @_;
4572 $from = 0 unless defined $from;
4573 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4574
591ebf65 4575 print "<table class=\"tags\">\n";
6dd36acd 4576 my $alternate = 1;
717b8311
JN
4577 for (my $i = $from; $i <= $to; $i++) {
4578 my $entry = $taglist->[$i];
4579 my %tag = %$entry;
cd146408 4580 my $comment = $tag{'subject'};
717b8311
JN
4581 my $comment_short;
4582 if (defined $comment) {
4583 $comment_short = chop_str($comment, 30, 5);
4584 }
4585 if ($alternate) {
4586 print "<tr class=\"dark\">\n";
4587 } else {
4588 print "<tr class=\"light\">\n";
4589 }
4590 $alternate ^= 1;
27dd1a83
JN
4591 if (defined $tag{'age'}) {
4592 print "<td><i>$tag{'age'}</i></td>\n";
4593 } else {
4594 print "<td></td>\n";
4595 }
4596 print "<td>" .
1c2a4f5a 4597 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
63e4220b 4598 -class => "list name"}, esc_html($tag{'name'})) .
717b8311
JN
4599 "</td>\n" .
4600 "<td>";
4601 if (defined $comment) {
952c65fc
JN
4602 print format_subject_html($comment, $comment_short,
4603 href(action=>"tag", hash=>$tag{'id'}));
717b8311
JN
4604 }
4605 print "</td>\n" .
4606 "<td class=\"selflink\">";
4607 if ($tag{'type'} eq "tag") {
1c2a4f5a 4608 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
717b8311
JN
4609 } else {
4610 print "&nbsp;";
4611 }
4612 print "</td>\n" .
4613 "<td class=\"link\">" . " | " .
1c2a4f5a 4614 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
717b8311 4615 if ($tag{'reftype'} eq "commit") {
bf901f8e
JN
4616 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
4617 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
717b8311 4618 } elsif ($tag{'reftype'} eq "blob") {
1c2a4f5a 4619 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
717b8311
JN
4620 }
4621 print "</td>\n" .
4622 "</tr>";
4623 }
4624 if (defined $extra) {
4625 print "<tr>\n" .
4626 "<td colspan=\"5\">$extra</td>\n" .
4627 "</tr>\n";
4628 }
4629 print "</table>\n";
4630}
4631
4632sub git_heads_body {
4633 # uses global variable $project
120ddde2 4634 my ($headlist, $head, $from, $to, $extra) = @_;
717b8311 4635 $from = 0 unless defined $from;
120ddde2 4636 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
717b8311 4637
591ebf65 4638 print "<table class=\"heads\">\n";
6dd36acd 4639 my $alternate = 1;
717b8311 4640 for (my $i = $from; $i <= $to; $i++) {
120ddde2 4641 my $entry = $headlist->[$i];
cd146408
JN
4642 my %ref = %$entry;
4643 my $curr = $ref{'id'} eq $head;
717b8311
JN
4644 if ($alternate) {
4645 print "<tr class=\"dark\">\n";
4646 } else {
4647 print "<tr class=\"light\">\n";
4648 }
4649 $alternate ^= 1;
cd146408
JN
4650 print "<td><i>$ref{'age'}</i></td>\n" .
4651 ($curr ? "<td class=\"current_head\">" : "<td>") .
bf901f8e 4652 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
cd146408 4653 -class => "list name"},esc_html($ref{'name'})) .
717b8311
JN
4654 "</td>\n" .
4655 "<td class=\"link\">" .
bf901f8e
JN
4656 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4657 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4658 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
717b8311
JN
4659 "</td>\n" .
4660 "</tr>";
4661 }
4662 if (defined $extra) {
4663 print "<tr>\n" .
4664 "<td colspan=\"3\">$extra</td>\n" .
4665 "</tr>\n";
4666 }
4667 print "</table>\n";
4668}
4669
8dbc0fce 4670sub git_search_grep_body {
5ad66088 4671 my ($commitlist, $from, $to, $extra) = @_;
8dbc0fce 4672 $from = 0 unless defined $from;
5ad66088 4673 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
8dbc0fce 4674
591ebf65 4675 print "<table class=\"commit_search\">\n";
8dbc0fce
RF
4676 my $alternate = 1;
4677 for (my $i = $from; $i <= $to; $i++) {
5ad66088 4678 my %co = %{$commitlist->[$i]};
8dbc0fce
RF
4679 if (!%co) {
4680 next;
4681 }
5ad66088 4682 my $commit = $co{'id'};
8dbc0fce
RF
4683 if ($alternate) {
4684 print "<tr class=\"dark\">\n";
4685 } else {
4686 print "<tr class=\"light\">\n";
4687 }
4688 $alternate ^= 1;
4689 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1c49a4e1 4690 format_author_html('td', \%co, 15, 5) .
8dbc0fce 4691 "<td>" .
be8b9063
JH
4692 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4693 -class => "list subject"},
4694 chop_and_escape_str($co{'title'}, 50) . "<br/>");
8dbc0fce
RF
4695 my $comment = $co{'comment'};
4696 foreach my $line (@$comment) {
6dfbb304 4697 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
be8b9063 4698 my ($lead, $match, $trail) = ($1, $2, $3);
b8d97d07
JN
4699 $match = chop_str($match, 70, 5, 'center');
4700 my $contextlen = int((80 - length($match))/2);
4701 $contextlen = 30 if ($contextlen > 30);
4702 $lead = chop_str($lead, $contextlen, 10, 'left');
4703 $trail = chop_str($trail, $contextlen, 10, 'right');
be8b9063
JH
4704
4705 $lead = esc_html($lead);
4706 $match = esc_html($match);
4707 $trail = esc_html($trail);
4708
4709 print "$lead<span class=\"match\">$match</span>$trail<br />";
8dbc0fce
RF
4710 }
4711 }
4712 print "</td>\n" .
4713 "<td class=\"link\">" .
4714 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4715 " | " .
f1fe8f5c
CR
4716 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4717 " | " .
8dbc0fce
RF
4718 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4719 print "</td>\n" .
4720 "</tr>\n";
4721 }
4722 if (defined $extra) {
4723 print "<tr>\n" .
4724 "<td colspan=\"3\">$extra</td>\n" .
4725 "</tr>\n";
4726 }
4727 print "</table>\n";
4728}
4729
717b8311
JN
4730## ======================================================================
4731## ======================================================================
4732## actions
4733
717b8311 4734sub git_project_list {
1b2d297e 4735 my $order = $input_params{'order'};
b06dcf8c 4736 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
074afaa0 4737 die_error(400, "Unknown order parameter");
6326b60c
JN
4738 }
4739
847e01fb 4740 my @list = git_get_projects_list();
717b8311 4741 if (!@list) {
074afaa0 4742 die_error(404, "No projects found");
717b8311 4743 }
6326b60c 4744
717b8311
JN
4745 git_header_html();
4746 if (-f $home_text) {
4747 print "<div class=\"index_include\">\n";
2dcb5e1a 4748 insert_file($home_text);
717b8311 4749 print "</div>\n";
9f5dcb81 4750 }
0d1d154d
PB
4751 print $cgi->startform(-method => "get") .
4752 "<p class=\"projsearch\">Search:\n" .
4753 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4754 "</p>" .
4755 $cgi->end_form() . "\n";
e30496df
PB
4756 git_project_list_body(\@list, $order);
4757 git_footer_html();
4758}
4759
4760sub git_forks {
1b2d297e 4761 my $order = $input_params{'order'};
b06dcf8c 4762 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
074afaa0 4763 die_error(400, "Unknown order parameter");
717b8311 4764 }
e30496df
PB
4765
4766 my @list = git_get_projects_list($project);
4767 if (!@list) {
074afaa0 4768 die_error(404, "No forks found");
9f5dcb81 4769 }
e30496df
PB
4770
4771 git_header_html();
4772 git_print_page_nav('','');
4773 git_print_header_div('summary', "$project forks");
4774 git_project_list_body(\@list, $order);
717b8311 4775 git_footer_html();
9f5dcb81
JN
4776}
4777
fc2b2be0 4778sub git_project_index {
e30496df 4779 my @projects = git_get_projects_list($project);
fc2b2be0
JN
4780
4781 print $cgi->header(
4782 -type => 'text/plain',
4783 -charset => 'utf-8',
ab41dfbf 4784 -content_disposition => 'inline; filename="index.aux"');
fc2b2be0
JN
4785
4786 foreach my $pr (@projects) {
4787 if (!exists $pr->{'owner'}) {
76e4f5d0 4788 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
fc2b2be0
JN
4789 }
4790
4791 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4792 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4793 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4794 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4795 $path =~ s/ /\+/g;
4796 $owner =~ s/ /\+/g;
4797
4798 print "$path $owner\n";
4799 }
4800}
4801
ede5e100 4802sub git_summary {
847e01fb 4803 my $descr = git_get_project_description($project) || "none";
a979d128 4804 my %co = parse_commit("HEAD");
785cdea9 4805 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
a979d128 4806 my $head = $co{'id'};
ede5e100 4807
1e0cf030 4808 my $owner = git_get_project_owner($project);
ede5e100 4809
cd146408 4810 my $refs = git_get_references();
313ce8ce
RF
4811 # These get_*_list functions return one more to allow us to see if
4812 # there are more ...
4813 my @taglist = git_get_tags_list(16);
4814 my @headlist = git_get_heads_list(16);
e30496df 4815 my @forklist;
25b2790f 4816 my $check_forks = gitweb_check_feature('forks');
5dd5ed09
JH
4817
4818 if ($check_forks) {
e30496df
PB
4819 @forklist = git_get_projects_list($project);
4820 }
120ddde2 4821
ede5e100 4822 git_header_html();
847e01fb 4823 git_print_page_nav('summary','', $head);
9f5dcb81 4824
19806691 4825 print "<div class=\"title\">&nbsp;</div>\n";
591ebf65 4826 print "<table class=\"projects_list\">\n" .
a476142f
PB
4827 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4828 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
785cdea9 4829 if (defined $cd{'rfc2822'}) {
a476142f 4830 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
785cdea9
JN
4831 }
4832
e79ca7cc
JN
4833 # use per project git URL list in $projectroot/$project/cloneurl
4834 # or make project git URL from git base URL and project name
19a8721e 4835 my $url_tag = "URL";
e79ca7cc
JN
4836 my @url_list = git_get_project_url_list($project);
4837 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4838 foreach my $git_url (@url_list) {
4839 next unless $git_url;
a476142f 4840 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
19a8721e
JN
4841 $url_tag = "";
4842 }
aed93de4
PB
4843
4844 # Tag cloud
25b2790f 4845 my $show_ctags = gitweb_check_feature('ctags');
aed93de4
PB
4846 if ($show_ctags) {
4847 my $ctags = git_get_project_ctags($project);
4848 my $cloud = git_populate_project_tagcloud($ctags);
4849 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4850 print "</td>\n<td>" unless %$ctags;
4851 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4852 print "</td>\n<td>" if %$ctags;
4853 print git_show_project_tagcloud($cloud, 48);
4854 print "</td></tr>";
4855 }
4856
19a8721e 4857 print "</table>\n";
9f5dcb81 4858
7e1100e9
MM
4859 # If XSS prevention is on, we don't include README.html.
4860 # TODO: Allow a readme in some safe format.
4861 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
2dcb5e1a
JN
4862 print "<div class=\"title\">readme</div>\n" .
4863 "<div class=\"readme\">\n";
4864 insert_file("$projectroot/$project/README.html");
4865 print "\n</div>\n"; # class="readme"
447ef09a
PB
4866 }
4867
313ce8ce
RF
4868 # we need to request one more than 16 (0..15) to check if
4869 # those 16 are all
785cdea9
JN
4870 my @commitlist = $head ? parse_commits($head, 17) : ();
4871 if (@commitlist) {
4872 git_print_header_div('shortlog');
4873 git_shortlog_body(\@commitlist, 0, 15, $refs,
4874 $#commitlist <= 15 ? undef :
4875 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4876 }
ede5e100 4877
120ddde2 4878 if (@taglist) {
847e01fb 4879 git_print_header_div('tags');
120ddde2 4880 git_tags_body(\@taglist, 0, 15,
313ce8ce 4881 $#taglist <= 15 ? undef :
1c2a4f5a 4882 $cgi->a({-href => href(action=>"tags")}, "..."));
ede5e100 4883 }
0db37973 4884
120ddde2 4885 if (@headlist) {
847e01fb 4886 git_print_header_div('heads');
120ddde2 4887 git_heads_body(\@headlist, $head, 0, 15,
313ce8ce 4888 $#headlist <= 15 ? undef :
1c2a4f5a 4889 $cgi->a({-href => href(action=>"heads")}, "..."));
0db37973 4890 }
9f5dcb81 4891
e30496df
PB
4892 if (@forklist) {
4893 git_print_header_div('forks');
f04f27e8 4894 git_project_list_body(\@forklist, 'age', 0, 15,
aaca9675 4895 $#forklist <= 15 ? undef :
e30496df 4896 $cgi->a({-href => href(action=>"forks")}, "..."),
f04f27e8 4897 'no_header');
e30496df
PB
4898 }
4899
ede5e100
KS
4900 git_footer_html();
4901}
4902
d8a20ba9 4903sub git_tag {
847e01fb 4904 my $head = git_get_head_hash($project);
d8a20ba9 4905 git_header_html();
847e01fb
JN
4906 git_print_page_nav('','', $head,undef,$head);
4907 my %tag = parse_tag($hash);
198a2a8a
JN
4908
4909 if (! %tag) {
074afaa0 4910 die_error(404, "Unknown tag object");
198a2a8a
JN
4911 }
4912
847e01fb 4913 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
d8a20ba9 4914 print "<div class=\"title_text\">\n" .
591ebf65 4915 "<table class=\"object_header\">\n" .
e4669df9
KS
4916 "<tr>\n" .
4917 "<td>object</td>\n" .
952c65fc
JN
4918 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4919 $tag{'object'}) . "</td>\n" .
4920 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4921 $tag{'type'}) . "</td>\n" .
e4669df9 4922 "</tr>\n";
d8a20ba9 4923 if (defined($tag{'author'})) {
ba924733 4924 git_print_authorship_rows(\%tag, 'author');
d8a20ba9
KS
4925 }
4926 print "</table>\n\n" .
4927 "</div>\n";
4928 print "<div class=\"page_body\">";
4929 my $comment = $tag{'comment'};
4930 foreach my $line (@$comment) {
7002243f 4931 chomp $line;
793c400c 4932 print esc_html($line, -nbsp=>1) . "<br/>\n";
d8a20ba9
KS
4933 }
4934 print "</div>\n";
4935 git_footer_html();
4936}
4937
4af819d4
JN
4938sub git_blame_common {
4939 my $format = shift || 'porcelain';
c4ccf61f
JN
4940 if ($format eq 'porcelain' && $cgi->param('js')) {
4941 $format = 'incremental';
4942 $action = 'blame_incremental'; # for page title etc
4943 }
4af819d4 4944
d2ce10d7 4945 # permissions
25b2790f 4946 gitweb_check_feature('blame')
d2ce10d7 4947 or die_error(403, "Blame view not allowed");
074afaa0 4948
d2ce10d7 4949 # error checking
074afaa0 4950 die_error(400, "No file name given") unless $file_name;
847e01fb 4951 $hash_base ||= git_get_head_hash($project);
d2ce10d7 4952 die_error(404, "Couldn't find base commit") unless $hash_base;
847e01fb 4953 my %co = parse_commit($hash_base)
074afaa0 4954 or die_error(404, "Commit not found");
d2ce10d7 4955 my $ftype = "blob";
1f2857ea
LT
4956 if (!defined $hash) {
4957 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
074afaa0 4958 or die_error(404, "Error looking up file");
d2ce10d7
JN
4959 } else {
4960 $ftype = git_get_type($hash);
4961 if ($ftype !~ "blob") {
4962 die_error(400, "Object is not a blob");
4963 }
1f2857ea 4964 }
d2ce10d7 4965
4af819d4
JN
4966 my $fd;
4967 if ($format eq 'incremental') {
4968 # get file contents (as base)
4969 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
4970 or die_error(500, "Open git-cat-file failed");
4971 } elsif ($format eq 'data') {
4972 # run git-blame --incremental
4973 open $fd, "-|", git_cmd(), "blame", "--incremental",
4974 $hash_base, "--", $file_name
4975 or die_error(500, "Open git-blame --incremental failed");
4976 } else {
4977 # run git-blame --porcelain
4978 open $fd, "-|", git_cmd(), "blame", '-p',
4979 $hash_base, '--', $file_name
4980 or die_error(500, "Open git-blame --porcelain failed");
4981 }
4982
4983 # incremental blame data returns early
4984 if ($format eq 'data') {
4985 print $cgi->header(
4986 -type=>"text/plain", -charset => "utf-8",
4987 -status=> "200 OK");
4988 local $| = 1; # output autoflush
4989 print while <$fd>;
4990 close $fd
4991 or print "ERROR $!\n";
4992
4993 print 'END';
4994 if (defined $t0 && gitweb_check_feature('timed')) {
4995 print ' '.
4996 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
4997 ' '.$number_of_git_cmds;
4998 }
4999 print "\n";
5000
5001 return;
5002 }
d2ce10d7
JN
5003
5004 # page header
1f2857ea 5005 git_header_html();
0d83ddc4 5006 my $formats_nav =
a3823e5a 5007 $cgi->a({-href => href(action=>"blob", -replay=>1)},
952c65fc 5008 "blob") .
87e573f6
JN
5009 " | ";
5010 if ($format eq 'incremental') {
5011 $formats_nav .=
5012 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
5013 "blame") . " (non-incremental)";
5014 } else {
5015 $formats_nav .=
5016 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
5017 "blame") . " (incremental)";
5018 }
5019 $formats_nav .=
952c65fc 5020 " | " .
a3823e5a
JN
5021 $cgi->a({-href => href(action=>"history", -replay=>1)},
5022 "history") .
cae1862a 5023 " | " .
4af819d4 5024 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
f35274da 5025 "HEAD");
847e01fb
JN
5026 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5027 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
59fb1c94 5028 git_print_page_path($file_name, $ftype, $hash_base);
d2ce10d7
JN
5029
5030 # page body
4af819d4
JN
5031 if ($format eq 'incremental') {
5032 print "<noscript>\n<div class=\"error\"><center><b>\n".
5033 "This page requires JavaScript to run.\n Use ".
c4ccf61f 5034 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
4af819d4
JN
5035 'this page').
5036 " instead.\n".
5037 "</b></center></div>\n</noscript>\n";
5038
5039 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
5040 }
5041
5042 print qq!<div class="page_body">\n!;
5043 print qq!<div id="progress_info">... / ...</div>\n!
5044 if ($format eq 'incremental');
5045 print qq!<table id="blame_table" class="blame" width="100%">\n!.
5046 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5047 qq!<thead>\n!.
5048 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
5049 qq!</thead>\n!.
5050 qq!<tbody>\n!;
5051
aef37684 5052 my @rev_color = qw(light dark);
cc1bf97e
LT
5053 my $num_colors = scalar(@rev_color);
5054 my $current_color = 0;
d2ce10d7 5055
4af819d4
JN
5056 if ($format eq 'incremental') {
5057 my $color_class = $rev_color[$current_color];
5058
5059 #contents of a file
5060 my $linenr = 0;
5061 LINE:
5062 while (my $line = <$fd>) {
5063 chomp $line;
5064 $linenr++;
5065
5066 print qq!<tr id="l$linenr" class="$color_class">!.
5067 qq!<td class="sha1"><a href=""> </a></td>!.
5068 qq!<td class="linenr">!.
5069 qq!<a class="linenr" href="">$linenr</a></td>!;
5070 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
5071 print qq!</tr>\n!;
eeef88cd 5072 }
4af819d4
JN
5073
5074 } else { # porcelain, i.e. ordinary blame
5075 my %metainfo = (); # saves information about commits
5076
5077 # blame data
5078 LINE:
5079 while (my $line = <$fd>) {
5080 chomp $line;
5081 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5082 # no <lines in group> for subsequent lines in group of lines
5083 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5084 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5085 if (!exists $metainfo{$full_rev}) {
5086 $metainfo{$full_rev} = { 'nprevious' => 0 };
eeef88cd 5087 }
4af819d4
JN
5088 my $meta = $metainfo{$full_rev};
5089 my $data;
5090 while ($data = <$fd>) {
5091 chomp $data;
5092 last if ($data =~ s/^\t//); # contents of line
5093 if ($data =~ /^(\S+)(?: (.*))?$/) {
5094 $meta->{$1} = $2 unless exists $meta->{$1};
5095 }
5096 if ($data =~ /^previous /) {
5097 $meta->{'nprevious'}++;
5098 }
eeef88cd 5099 }
4af819d4
JN
5100 my $short_rev = substr($full_rev, 0, 8);
5101 my $author = $meta->{'author'};
5102 my %date =
5103 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5104 my $date = $date{'iso-tz'};
5105 if ($group_size) {
5106 $current_color = ($current_color + 1) % $num_colors;
3665e7e7 5107 }
4af819d4
JN
5108 my $tr_class = $rev_color[$current_color];
5109 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5110 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5111 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5112 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5113 if ($group_size) {
5114 print "<td class=\"sha1\"";
5115 print " title=\"". esc_html($author) . ", $date\"";
5116 print " rowspan=\"$group_size\"" if ($group_size > 1);
5117 print ">";
5118 print $cgi->a({-href => href(action=>"commit",
5119 hash=>$full_rev,
5120 file_name=>$file_name)},
5121 esc_html($short_rev));
5122 if ($group_size >= 2) {
5123 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5124 if (@author_initials) {
5125 print "<br />" .
5126 esc_html(join('', @author_initials));
5127 # or join('.', ...)
5128 }
a36817b6 5129 }
4af819d4 5130 print "</td>\n";
a36817b6 5131 }
4af819d4
JN
5132 # 'previous' <sha1 of parent commit> <filename at commit>
5133 if (exists $meta->{'previous'} &&
5134 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5135 $meta->{'parent'} = $1;
5136 $meta->{'file_parent'} = unquote($2);
5137 }
5138 my $linenr_commit =
5139 exists($meta->{'parent'}) ?
5140 $meta->{'parent'} : $full_rev;
5141 my $linenr_filename =
5142 exists($meta->{'file_parent'}) ?
5143 $meta->{'file_parent'} : unquote($meta->{'filename'});
5144 my $blamed = href(action => 'blame',
5145 file_name => $linenr_filename,
5146 hash_base => $linenr_commit);
5147 print "<td class=\"linenr\">";
5148 print $cgi->a({ -href => "$blamed#l$orig_lineno",
5149 -class => "linenr" },
5150 esc_html($lineno));
5151 print "</td>";
5152 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
5153 print "</tr>\n";
5154 } # end while
5155
1f2857ea 5156 }
4af819d4
JN
5157
5158 # footer
5159 print "</tbody>\n".
5160 "</table>\n"; # class="blame"
5161 print "</div>\n"; # class="blame_body"
952c65fc
JN
5162 close $fd
5163 or print "Reading blob failed\n";
d2ce10d7 5164
1f2857ea
LT
5165 git_footer_html();
5166}
5167
4af819d4
JN
5168sub git_blame {
5169 git_blame_common();
5170}
5171
5172sub git_blame_incremental {
5173 git_blame_common('incremental');
5174}
5175
5176sub git_blame_data {
5177 git_blame_common('data');
5178}
5179
717b8311 5180sub git_tags {
847e01fb 5181 my $head = git_get_head_hash($project);
717b8311 5182 git_header_html();
847e01fb
JN
5183 git_print_page_nav('','', $head,undef,$head);
5184 git_print_header_div('summary', $project);
2d007374 5185
cd146408
JN
5186 my @tagslist = git_get_tags_list();
5187 if (@tagslist) {
5188 git_tags_body(\@tagslist);
2d007374 5189 }
717b8311 5190 git_footer_html();
2d007374
PB
5191}
5192
717b8311 5193sub git_heads {
847e01fb 5194 my $head = git_get_head_hash($project);
717b8311 5195 git_header_html();
847e01fb
JN
5196 git_print_page_nav('','', $head,undef,$head);
5197 git_print_header_div('summary', $project);
930cf7dd 5198
cd146408
JN
5199 my @headslist = git_get_heads_list();
5200 if (@headslist) {
5201 git_heads_body(\@headslist, $head);
f5aa79d9 5202 }
717b8311 5203 git_footer_html();
f5aa79d9
JN
5204}
5205
19806691 5206sub git_blob_plain {
7f718e8b 5207 my $type = shift;
f2e73302 5208 my $expires;
f2e73302 5209
cff0771b 5210 if (!defined $hash) {
5be01bc8 5211 if (defined $file_name) {
847e01fb 5212 my $base = $hash_base || git_get_head_hash($project);
5be01bc8 5213 $hash = git_get_hash_by_path($base, $file_name, "blob")
074afaa0 5214 or die_error(404, "Cannot find file");
5be01bc8 5215 } else {
074afaa0 5216 die_error(400, "No file name defined");
5be01bc8 5217 }
800764cf
MW
5218 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5219 # blobs defined by non-textual hash id's can be cached
5220 $expires = "+1d";
5be01bc8 5221 }
800764cf 5222
25691fbe 5223 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
074afaa0 5224 or die_error(500, "Open git-cat-file blob '$hash' failed");
930cf7dd 5225
7f718e8b
JN
5226 # content-type (can include charset)
5227 $type = blob_contenttype($fd, $file_name, $type);
f5aa79d9 5228
7f718e8b 5229 # "save as" filename, even when no $file_name is given
f5aa79d9 5230 my $save_as = "$hash";
9312944d
KS
5231 if (defined $file_name) {
5232 $save_as = $file_name;
f5aa79d9
JN
5233 } elsif ($type =~ m/^text\//) {
5234 $save_as .= '.txt';
9312944d 5235 }
f5aa79d9 5236
7e1100e9
MM
5237 # With XSS prevention on, blobs of all types except a few known safe
5238 # ones are served with "Content-Disposition: attachment" to make sure
5239 # they don't run in our security domain. For certain image types,
5240 # blob view writes an <img> tag referring to blob_plain view, and we
5241 # want to be sure not to break that by serving the image as an
5242 # attachment (though Firefox 3 doesn't seem to care).
5243 my $sandbox = $prevent_xss &&
5244 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5245
f2e73302 5246 print $cgi->header(
7f718e8b
JN
5247 -type => $type,
5248 -expires => $expires,
7e1100e9
MM
5249 -content_disposition =>
5250 ($sandbox ? 'attachment' : 'inline')
5251 . '; filename="' . $save_as . '"');
34122b57 5252 local $/ = undef;
ad14e931 5253 binmode STDOUT, ':raw';
19806691 5254 print <$fd>;
ad14e931 5255 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
19806691
KS
5256 close $fd;
5257}
5258
930cf7dd 5259sub git_blob {
f2e73302 5260 my $expires;
f2e73302 5261
cff0771b 5262 if (!defined $hash) {
5be01bc8 5263 if (defined $file_name) {
847e01fb 5264 my $base = $hash_base || git_get_head_hash($project);
5be01bc8 5265 $hash = git_get_hash_by_path($base, $file_name, "blob")
074afaa0 5266 or die_error(404, "Cannot find file");
5be01bc8 5267 } else {
074afaa0 5268 die_error(400, "No file name defined");
5be01bc8 5269 }
800764cf
MW
5270 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5271 # blobs defined by non-textual hash id's can be cached
5272 $expires = "+1d";
5be01bc8 5273 }
800764cf 5274
25b2790f 5275 my $have_blame = gitweb_check_feature('blame');
25691fbe 5276 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
074afaa0 5277 or die_error(500, "Couldn't cat $file_name, $hash");
847e01fb 5278 my $mimetype = blob_mimetype($fd, $file_name);
dfa7c7d2 5279 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
930cf7dd
LT
5280 close $fd;
5281 return git_blob_plain($mimetype);
5282 }
5a4cf334
JN
5283 # we can have blame only for text/* mimetype
5284 $have_blame &&= ($mimetype =~ m!^text/!);
5285
f2e73302 5286 git_header_html(undef, $expires);
0d83ddc4 5287 my $formats_nav = '';
847e01fb 5288 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
930cf7dd
LT
5289 if (defined $file_name) {
5290 if ($have_blame) {
952c65fc 5291 $formats_nav .=
a3823e5a 5292 $cgi->a({-href => href(action=>"blame", -replay=>1)},
952c65fc
JN
5293 "blame") .
5294 " | ";
930cf7dd 5295 }
0d83ddc4 5296 $formats_nav .=
a3823e5a 5297 $cgi->a({-href => href(action=>"history", -replay=>1)},
cae1862a
PB
5298 "history") .
5299 " | " .
a3823e5a 5300 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
35329cc1 5301 "raw") .
952c65fc
JN
5302 " | " .
5303 $cgi->a({-href => href(action=>"blob",
5304 hash_base=>"HEAD", file_name=>$file_name)},
f35274da 5305 "HEAD");
930cf7dd 5306 } else {
952c65fc 5307 $formats_nav .=
a3823e5a
JN
5308 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5309 "raw");
930cf7dd 5310 }
847e01fb
JN
5311 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5312 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
930cf7dd
LT
5313 } else {
5314 print "<div class=\"page_nav\">\n" .
5315 "<br/><br/></div>\n" .
5316 "<div class=\"title\">$hash</div>\n";
5317 }
59fb1c94 5318 git_print_page_path($file_name, "blob", $hash_base);
930cf7dd 5319 print "<div class=\"page_body\">\n";
dfa7c7d2 5320 if ($mimetype =~ m!^image/!) {
5a4cf334
JN
5321 print qq!<img type="$mimetype"!;
5322 if ($file_name) {
5323 print qq! alt="$file_name" title="$file_name"!;
5324 }
5325 print qq! src="! .
5326 href(action=>"blob_plain", hash=>$hash,
5327 hash_base=>$hash_base, file_name=>$file_name) .
5328 qq!" />\n!;
dfa7c7d2
JN
5329 } else {
5330 my $nr;
5331 while (my $line = <$fd>) {
5332 chomp $line;
5333 $nr++;
5334 $line = untabify($line);
b9759f07
PB
5335 printf "<div class=\"pre\"><a id=\"l%i\" href=\"" . href(-replay => 1)
5336 . "#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
dfa7c7d2
JN
5337 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
5338 }
930cf7dd 5339 }
952c65fc
JN
5340 close $fd
5341 or print "Reading blob failed.\n";
930cf7dd
LT
5342 print "</div>";
5343 git_footer_html();
5344}
5345
09bd7898 5346sub git_tree {
6f7ea5fb
LT
5347 if (!defined $hash_base) {
5348 $hash_base = "HEAD";
5349 }
b87d78d6 5350 if (!defined $hash) {
09bd7898 5351 if (defined $file_name) {
6f7ea5fb
LT
5352 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5353 } else {
5354 $hash = $hash_base;
10dba28d 5355 }
e925f38c 5356 }
2d7a3532 5357 die_error(404, "No such tree") unless defined($hash);
34122b57 5358
e4b48eaa
JN
5359 my $show_sizes = gitweb_check_feature('show-sizes');
5360 my $have_blame = gitweb_check_feature('blame');
5361
34122b57
JN
5362 my @entries = ();
5363 {
5364 local $/ = "\0";
e4b48eaa
JN
5365 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
5366 ($show_sizes ? '-l' : ()), @extra_options, $hash
34122b57
JN
5367 or die_error(500, "Open git-ls-tree failed");
5368 @entries = map { chomp; $_ } <$fd>;
5369 close $fd
5370 or die_error(404, "Reading tree failed");
5371 }
d63577da 5372
847e01fb
JN
5373 my $refs = git_get_references();
5374 my $ref = format_ref_marker($refs, $hash_base);
12a88f2f 5375 git_header_html();
300454fe 5376 my $basedir = '';
847e01fb 5377 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
cae1862a
PB
5378 my @views_nav = ();
5379 if (defined $file_name) {
5380 push @views_nav,
a3823e5a 5381 $cgi->a({-href => href(action=>"history", -replay=>1)},
cae1862a
PB
5382 "history"),
5383 $cgi->a({-href => href(action=>"tree",
5384 hash_base=>"HEAD", file_name=>$file_name)},
f35274da 5385 "HEAD"),
cae1862a 5386 }
a3c8ab30
MM
5387 my $snapshot_links = format_snapshot_links($hash);
5388 if (defined $snapshot_links) {
cae1862a 5389 # FIXME: Should be available when we have no hash base as well.
a3c8ab30 5390 push @views_nav, $snapshot_links;
cae1862a 5391 }
e4b48eaa
JN
5392 git_print_page_nav('tree','', $hash_base, undef, undef,
5393 join(' | ', @views_nav));
847e01fb 5394 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
d63577da 5395 } else {
fa702003 5396 undef $hash_base;
d63577da
KS
5397 print "<div class=\"page_nav\">\n";
5398 print "<br/><br/></div>\n";
5399 print "<div class=\"title\">$hash</div>\n";
5400 }
09bd7898 5401 if (defined $file_name) {
300454fe
JN
5402 $basedir = $file_name;
5403 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5404 $basedir .= '/';
5405 }
2d7a3532 5406 git_print_page_path($file_name, 'tree', $hash_base);
09bd7898 5407 }
fbb592a9 5408 print "<div class=\"page_body\">\n";
591ebf65 5409 print "<table class=\"tree\">\n";
6dd36acd 5410 my $alternate = 1;
b6b7fc72
JN
5411 # '..' (top directory) link if possible
5412 if (defined $hash_base &&
5413 defined $file_name && $file_name =~ m![^/]+$!) {
5414 if ($alternate) {
5415 print "<tr class=\"dark\">\n";
5416 } else {
5417 print "<tr class=\"light\">\n";
5418 }
5419 $alternate ^= 1;
5420
5421 my $up = $file_name;
5422 $up =~ s!/?[^/]+$!!;
5423 undef $up unless $up;
5424 # based on git_print_tree_entry
5425 print '<td class="mode">' . mode_str('040000') . "</td>\n";
e4b48eaa 5426 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
b6b7fc72 5427 print '<td class="list">';
e4b48eaa
JN
5428 print $cgi->a({-href => href(action=>"tree",
5429 hash_base=>$hash_base,
b6b7fc72
JN
5430 file_name=>$up)},
5431 "..");
5432 print "</td>\n";
5433 print "<td class=\"link\"></td>\n";
5434
5435 print "</tr>\n";
5436 }
161332a5 5437 foreach my $line (@entries) {
e4b48eaa 5438 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
cb849b46 5439
bddec01d 5440 if ($alternate) {
c994d620 5441 print "<tr class=\"dark\">\n";
bddec01d 5442 } else {
c994d620 5443 print "<tr class=\"light\">\n";
bddec01d
KS
5444 }
5445 $alternate ^= 1;
cb849b46 5446
300454fe 5447 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
fa702003 5448
42f7eb94 5449 print "</tr>\n";
161332a5 5450 }
42f7eb94
KS
5451 print "</table>\n" .
5452 "</div>";
12a88f2f 5453 git_footer_html();
09bd7898
KS
5454}
5455
b629275f
MR
5456sub snapshot_name {
5457 my ($project, $hash) = @_;
5458
5459 # path/to/project.git -> project
5460 # path/to/project/.git -> project
5461 my $name = to_utf8($project);
5462 $name =~ s,([^/])/*\.git$,$1,;
5463 $name = basename($name);
5464 # sanitize name
5465 $name =~ s/[[:cntrl:]]/?/g;
5466
5467 my $ver = $hash;
5468 if ($hash =~ /^[0-9a-fA-F]+$/) {
5469 # shorten SHA-1 hash
5470 my $full_hash = git_get_full_hash($project, $hash);
5471 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
5472 $ver = git_get_short_hash($project, $hash);
5473 }
5474 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
5475 # tags don't need shortened SHA-1 hash
5476 $ver = $1;
5477 } else {
5478 # branches and other need shortened SHA-1 hash
5479 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
5480 $ver = $1;
5481 }
5482 $ver .= '-' . git_get_short_hash($project, $hash);
5483 }
5484 # in case of hierarchical branch names
5485 $ver =~ s!/!.!g;
5486
5487 # name = project-version_string
5488 $name = "$name-$ver";
5489
5490 return wantarray ? ($name, $name) : $name;
5491}
5492
cb9c6e5b 5493sub git_snapshot {
1b2d297e 5494 my $format = $input_params{'snapshot_format'};
5e166843 5495 if (!@snapshot_fmts) {
074afaa0 5496 die_error(403, "Snapshots not allowed");
3473e7df
JN
5497 }
5498 # default to first supported snapshot format
5e166843 5499 $format ||= $snapshot_fmts[0];
3473e7df 5500 if ($format !~ m/^[a-z0-9]+$/) {
074afaa0 5501 die_error(400, "Invalid snapshot format parameter");
3473e7df 5502 } elsif (!exists($known_snapshot_formats{$format})) {
074afaa0 5503 die_error(400, "Unknown snapshot format");
1bfd3631
MR
5504 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
5505 die_error(403, "Snapshot format not allowed");
34b31a8d
MR
5506 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
5507 die_error(403, "Unsupported snapshot format");
ddb8d900
AK
5508 }
5509
fdb0c36e
MR
5510 my $type = git_get_type("$hash^{}");
5511 if (!$type) {
5512 die_error(404, 'Object does not exist');
5513 } elsif ($type eq 'blob') {
5514 die_error(400, 'Object is not a tree-ish');
cb9c6e5b
AK
5515 }
5516
b629275f
MR
5517 my ($name, $prefix) = snapshot_name($project, $hash);
5518 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
5519 my $cmd = quote_command(
516381d5
LW
5520 git_cmd(), 'archive',
5521 "--format=$known_snapshot_formats{$format}{'format'}",
b629275f 5522 "--prefix=$prefix/", $hash);
a3c8ab30 5523 if (exists $known_snapshot_formats{$format}{'compressor'}) {
516381d5 5524 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
072570ee 5525 }
cb9c6e5b 5526
b629275f 5527 $filename =~ s/(["\\])/\\$1/g;
ab41dfbf 5528 print $cgi->header(
a3c8ab30 5529 -type => $known_snapshot_formats{$format}{'type'},
b629275f 5530 -content_disposition => 'inline; filename="' . $filename . '"',
ab41dfbf 5531 -status => '200 OK');
cb9c6e5b 5532
072570ee 5533 open my $fd, "-|", $cmd
074afaa0 5534 or die_error(500, "Execute git-archive failed");
cb9c6e5b
AK
5535 binmode STDOUT, ':raw';
5536 print <$fd>;
5537 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5538 close $fd;
cb9c6e5b
AK
5539}
5540
15f0b112 5541sub git_log_generic {
69ca37d2 5542 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
15f0b112 5543
847e01fb 5544 my $head = git_get_head_hash($project);
69ca37d2
JN
5545 if (!defined $base) {
5546 $base = $head;
0db37973 5547 }
ea4a6df4
KS
5548 if (!defined $page) {
5549 $page = 0;
b87d78d6 5550 }
847e01fb 5551 my $refs = git_get_references();
ea4a6df4 5552
69ca37d2
JN
5553 my $commit_hash = $base;
5554 if (defined $parent) {
5555 $commit_hash = "$parent..$base";
5556 }
5557 my @commitlist =
5558 parse_commits($commit_hash, 101, (100 * $page),
5559 defined $file_name ? ($file_name, "--full-history") : ());
5560
5561 my $ftype;
5562 if (!defined $file_hash && defined $file_name) {
5563 # some commits could have deleted file in question,
5564 # and not have it in tree, but one of them has to have it
5565 for (my $i = 0; $i < @commitlist; $i++) {
5566 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5567 last if defined $file_hash;
5568 }
5569 }
5570 if (defined $file_hash) {
5571 $ftype = git_get_type($file_hash);
5572 }
5573 if (defined $file_name && !defined $ftype) {
5574 die_error(500, "Unknown type of object");
5575 }
5576 my %co;
5577 if (defined $file_name) {
5578 %co = parse_commit($base)
5579 or die_error(404, "Unknown commit object");
15f0b112 5580 }
ea4a6df4 5581
69ca37d2
JN
5582
5583 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
15f0b112 5584 my $next_link = '';
42671caa
JN
5585 if ($#commitlist >= 100) {
5586 $next_link =
5587 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5588 -accesskey => "n", -title => "Alt-n"}, "next");
5589 }
15f0b112 5590 my $patch_max = gitweb_get_feature('patches');
69ca37d2 5591 if ($patch_max && !defined $file_name) {
75bf2cb2
GB
5592 if ($patch_max < 0 || @commitlist <= $patch_max) {
5593 $paging_nav .= " &sdot; " .
5594 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5595 "patches");
5596 }
5597 }
5598
0d83ddc4 5599 git_header_html();
15f0b112 5600 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
69ca37d2
JN
5601 if (defined $file_name) {
5602 git_print_header_div('commit', esc_html($co{'title'}), $base);
5603 } else {
5604 git_print_header_div('summary', $project)
5605 }
5606 git_print_page_path($file_name, $ftype, $hash_base)
5607 if (defined $file_name);
d16d093c 5608
69ca37d2
JN
5609 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
5610 $file_name, $file_hash, $ftype);
42671caa 5611
034df39e 5612 git_footer_html();
09bd7898
KS
5613}
5614
15f0b112 5615sub git_log {
69ca37d2
JN
5616 git_log_generic('log', \&git_log_body,
5617 $hash, $hash_parent);
15f0b112
JN
5618}
5619
09bd7898 5620sub git_commit {
9954f772 5621 $hash ||= $hash_base || "HEAD";
074afaa0
LW
5622 my %co = parse_commit($hash)
5623 or die_error(404, "Unknown commit object");
161332a5 5624
c9d193df
JN
5625 my $parent = $co{'parent'};
5626 my $parents = $co{'parents'}; # listref
5627
5628 # we need to prepare $formats_nav before any parameter munging
5629 my $formats_nav;
5630 if (!defined $parent) {
5631 # --root commitdiff
5632 $formats_nav .= '(initial)';
5633 } elsif (@$parents == 1) {
5634 # single parent commit
5635 $formats_nav .=
5636 '(parent: ' .
5637 $cgi->a({-href => href(action=>"commit",
5638 hash=>$parent)},
5639 esc_html(substr($parent, 0, 7))) .
5640 ')';
5641 } else {
5642 # merge commit
5643 $formats_nav .=
5644 '(merge: ' .
5645 join(' ', map {
f9308a18 5646 $cgi->a({-href => href(action=>"commit",
c9d193df
JN
5647 hash=>$_)},
5648 esc_html(substr($_, 0, 7)));
5649 } @$parents ) .
5650 ')';
5651 }
1655c987 5652 if (gitweb_check_feature('patches') && @$parents <= 1) {
75bf2cb2
GB
5653 $formats_nav .= " | " .
5654 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5655 "patch");
5656 }
c9d193df 5657
d8a20ba9 5658 if (!defined $parent) {
b9182987 5659 $parent = "--root";
6191f8e1 5660 }
549ab4a3 5661 my @difftree;
208ecb2e
JN
5662 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5663 @diff_opts,
5664 (@$parents <= 1 ? $parent : '-c'),
5665 $hash, "--"
074afaa0 5666 or die_error(500, "Open git-diff-tree failed");
208ecb2e 5667 @difftree = map { chomp; $_ } <$fd>;
074afaa0 5668 close $fd or die_error(404, "Reading git-diff-tree failed");
11044297
KS
5669
5670 # non-textual hash id's can be cached
5671 my $expires;
5672 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5673 $expires = "+1d";
5674 }
847e01fb
JN
5675 my $refs = git_get_references();
5676 my $ref = format_ref_marker($refs, $co{'id'});
ddb8d900 5677
594e212b 5678 git_header_html(undef, $expires);
a144154f 5679 git_print_page_nav('commit', '',
952c65fc 5680 $hash, $co{'tree'}, $hash,
c9d193df 5681 $formats_nav);
4f7b34c9 5682
b87d78d6 5683 if (defined $co{'parent'}) {
847e01fb 5684 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
b87d78d6 5685 } else {
847e01fb 5686 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
b87d78d6 5687 }
6191f8e1 5688 print "<div class=\"title_text\">\n" .
591ebf65 5689 "<table class=\"object_header\">\n";
1c49a4e1 5690 git_print_authorship_rows(\%co);
1f1ab5f0 5691 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
bddec01d
KS
5692 print "<tr>" .
5693 "<td>tree</td>" .
1f1ab5f0 5694 "<td class=\"sha1\">" .
952c65fc
JN
5695 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5696 class => "list"}, $co{'tree'}) .
19806691 5697 "</td>" .
952c65fc
JN
5698 "<td class=\"link\">" .
5699 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5700 "tree");
a3c8ab30
MM
5701 my $snapshot_links = format_snapshot_links($hash);
5702 if (defined $snapshot_links) {
5703 print " | " . $snapshot_links;
cb9c6e5b
AK
5704 }
5705 print "</td>" .
bddec01d 5706 "</tr>\n";
549ab4a3 5707
3e029299 5708 foreach my $par (@$parents) {
bddec01d
KS
5709 print "<tr>" .
5710 "<td>parent</td>" .
952c65fc
JN
5711 "<td class=\"sha1\">" .
5712 $cgi->a({-href => href(action=>"commit", hash=>$par),
5713 class => "list"}, $par) .
5714 "</td>" .
bddec01d 5715 "<td class=\"link\">" .
1c2a4f5a 5716 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
952c65fc 5717 " | " .
f2e60947 5718 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
bddec01d
KS
5719 "</td>" .
5720 "</tr>\n";
3e029299 5721 }
7a9b4c5f 5722 print "</table>".
b87d78d6 5723 "</div>\n";
d16d093c 5724
fbb592a9 5725 print "<div class=\"page_body\">\n";
d16d093c 5726 git_print_log($co{'comment'});
927dcec4 5727 print "</div>\n";
4a4a1a53 5728
208ecb2e 5729 git_difftree_body(\@difftree, $hash, @$parents);
4a4a1a53 5730
12a88f2f 5731 git_footer_html();
09bd7898
KS
5732}
5733
ca94601c
JN
5734sub git_object {
5735 # object is defined by:
5736 # - hash or hash_base alone
5737 # - hash_base and file_name
5738 my $type;
5739
5740 # - hash or hash_base alone
5741 if ($hash || ($hash_base && !defined $file_name)) {
5742 my $object_id = $hash || $hash_base;
5743
516381d5
LW
5744 open my $fd, "-|", quote_command(
5745 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
074afaa0 5746 or die_error(404, "Object does not exist");
ca94601c
JN
5747 $type = <$fd>;
5748 chomp $type;
5749 close $fd
074afaa0 5750 or die_error(404, "Object does not exist");
ca94601c
JN
5751
5752 # - hash_base and file_name
5753 } elsif ($hash_base && defined $file_name) {
5754 $file_name =~ s,/+$,,;
5755
5756 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
074afaa0 5757 or die_error(404, "Base object does not exist");
ca94601c
JN
5758
5759 # here errors should not hapen
5760 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
074afaa0 5761 or die_error(500, "Open git-ls-tree failed");
ca94601c
JN
5762 my $line = <$fd>;
5763 close $fd;
5764
5765 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5766 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
074afaa0 5767 die_error(404, "File or directory for given base does not exist");
ca94601c
JN
5768 }
5769 $type = $2;
5770 $hash = $3;
5771 } else {
074afaa0 5772 die_error(400, "Not enough information to find object");
ca94601c
JN
5773 }
5774
5775 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5776 hash=>$hash, hash_base=>$hash_base,
5777 file_name=>$file_name),
5778 -status => '302 Found');
5779}
5780
09bd7898 5781sub git_blobdiff {
9b71b1f6
JN
5782 my $format = shift || 'html';
5783
7c5e2ebb
JN
5784 my $fd;
5785 my @difftree;
5786 my %diffinfo;
9b71b1f6 5787 my $expires;
7c5e2ebb
JN
5788
5789 # preparing $fd and %diffinfo for git_patchset_body
5790 # new style URI
5791 if (defined $hash_base && defined $hash_parent_base) {
5792 if (defined $file_name) {
5793 # read raw output
45bd0c80
JN
5794 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5795 $hash_parent_base, $hash_base,
5ae917ac 5796 "--", (defined $file_parent ? $file_parent : ()), $file_name
074afaa0 5797 or die_error(500, "Open git-diff-tree failed");
7c5e2ebb
JN
5798 @difftree = map { chomp; $_ } <$fd>;
5799 close $fd
074afaa0 5800 or die_error(404, "Reading git-diff-tree failed");
7c5e2ebb 5801 @difftree
074afaa0 5802 or die_error(404, "Blob diff not found");
7c5e2ebb 5803
0aea3376
JN
5804 } elsif (defined $hash &&
5805 $hash =~ /[0-9a-fA-F]{40}/) {
5806 # try to find filename from $hash
7c5e2ebb
JN
5807
5808 # read filtered raw output
45bd0c80
JN
5809 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5810 $hash_parent_base, $hash_base, "--"
074afaa0 5811 or die_error(500, "Open git-diff-tree failed");
7c5e2ebb
JN
5812 @difftree =
5813 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
5814 # $hash == to_id
5815 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
5816 map { chomp; $_ } <$fd>;
5817 close $fd
074afaa0 5818 or die_error(404, "Reading git-diff-tree failed");
7c5e2ebb 5819 @difftree
074afaa0 5820 or die_error(404, "Blob diff not found");
7c5e2ebb
JN
5821
5822 } else {
074afaa0 5823 die_error(400, "Missing one of the blob diff parameters");
7c5e2ebb
JN
5824 }
5825
5826 if (@difftree > 1) {
074afaa0 5827 die_error(400, "Ambiguous blob diff specification");
7c5e2ebb
JN
5828 }
5829
5830 %diffinfo = parse_difftree_raw_line($difftree[0]);
9d301456
JN
5831 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5832 $file_name ||= $diffinfo{'to_file'};
7c5e2ebb
JN
5833
5834 $hash_parent ||= $diffinfo{'from_id'};
5835 $hash ||= $diffinfo{'to_id'};
5836
9b71b1f6
JN
5837 # non-textual hash id's can be cached
5838 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5839 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5840 $expires = '+1d';
5841 }
5842
7c5e2ebb 5843 # open patch output
25691fbe 5844 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
957d6ea7
JN
5845 '-p', ($format eq 'html' ? "--full-index" : ()),
5846 $hash_parent_base, $hash_base,
5ae917ac 5847 "--", (defined $file_parent ? $file_parent : ()), $file_name
074afaa0 5848 or die_error(500, "Open git-diff-tree failed");
7c5e2ebb
JN
5849 }
5850
b54dc9fd
JH
5851 # old/legacy style URI -- not generated anymore since 1.4.3.
5852 if (!%diffinfo) {
5853 die_error('404 Not Found', "Missing one of the blob diff parameters")
7c5e2ebb
JN
5854 }
5855
5856 # header
9b71b1f6
JN
5857 if ($format eq 'html') {
5858 my $formats_nav =
a3823e5a 5859 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
35329cc1 5860 "raw");
9b71b1f6
JN
5861 git_header_html(undef, $expires);
5862 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5863 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5864 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5865 } else {
5866 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5867 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5868 }
5869 if (defined $file_name) {
5870 git_print_page_path($file_name, "blob", $hash_base);
5871 } else {
5872 print "<div class=\"page_path\"></div>\n";
5873 }
5874
5875 } elsif ($format eq 'plain') {
5876 print $cgi->header(
5877 -type => 'text/plain',
5878 -charset => 'utf-8',
5879 -expires => $expires,
a2a3bf7b 5880 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
9b71b1f6
JN
5881
5882 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5883
7c5e2ebb 5884 } else {
074afaa0 5885 die_error(400, "Unknown blobdiff format");
7c5e2ebb
JN
5886 }
5887
5888 # patch
9b71b1f6
JN
5889 if ($format eq 'html') {
5890 print "<div class=\"page_body\">\n";
7c5e2ebb 5891
9b71b1f6
JN
5892 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5893 close $fd;
7c5e2ebb 5894
9b71b1f6
JN
5895 print "</div>\n"; # class="page_body"
5896 git_footer_html();
5897
5898 } else {
5899 while (my $line = <$fd>) {
403d0906
JN
5900 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5901 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
9b71b1f6
JN
5902
5903 print $line;
5904
5905 last if $line =~ m!^\+\+\+!;
5906 }
5907 local $/ = undef;
5908 print <$fd>;
5909 close $fd;
5910 }
09bd7898
KS
5911}
5912
19806691 5913sub git_blobdiff_plain {
9b71b1f6 5914 git_blobdiff('plain');
19806691
KS
5915}
5916
09bd7898 5917sub git_commitdiff {
20209854
GB
5918 my %params = @_;
5919 my $format = $params{-format} || 'html';
9872cd6f 5920
75bf2cb2 5921 my ($patch_max) = gitweb_get_feature('patches');
9872cd6f 5922 if ($format eq 'patch') {
9872cd6f
GB
5923 die_error(403, "Patch view not allowed") unless $patch_max;
5924 }
5925
9954f772 5926 $hash ||= $hash_base || "HEAD";
074afaa0
LW
5927 my %co = parse_commit($hash)
5928 or die_error(404, "Unknown commit object");
151602df 5929
cd030c3a
JN
5930 # choose format for commitdiff for merge
5931 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5932 $hash_parent = '--cc';
5933 }
5934 # we need to prepare $formats_nav before almost any parameter munging
151602df
JN
5935 my $formats_nav;
5936 if ($format eq 'html') {
5937 $formats_nav =
a3823e5a 5938 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
151602df 5939 "raw");
1655c987 5940 if ($patch_max && @{$co{'parents'}} <= 1) {
75bf2cb2
GB
5941 $formats_nav .= " | " .
5942 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5943 "patch");
5944 }
151602df 5945
cd030c3a
JN
5946 if (defined $hash_parent &&
5947 $hash_parent ne '-c' && $hash_parent ne '--cc') {
151602df
JN
5948 # commitdiff with two commits given
5949 my $hash_parent_short = $hash_parent;
5950 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5951 $hash_parent_short = substr($hash_parent, 0, 7);
5952 }
5953 $formats_nav .=
ada3e1f7
JN
5954 ' (from';
5955 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5956 if ($co{'parents'}[$i] eq $hash_parent) {
5957 $formats_nav .= ' parent ' . ($i+1);
5958 last;
5959 }
5960 }
5961 $formats_nav .= ': ' .
151602df
JN
5962 $cgi->a({-href => href(action=>"commitdiff",
5963 hash=>$hash_parent)},
5964 esc_html($hash_parent_short)) .
5965 ')';
5966 } elsif (!$co{'parent'}) {
5967 # --root commitdiff
5968 $formats_nav .= ' (initial)';
5969 } elsif (scalar @{$co{'parents'}} == 1) {
5970 # single parent commit
5971 $formats_nav .=
5972 ' (parent: ' .
5973 $cgi->a({-href => href(action=>"commitdiff",
5974 hash=>$co{'parent'})},
5975 esc_html(substr($co{'parent'}, 0, 7))) .
5976 ')';
5977 } else {
5978 # merge commit
cd030c3a
JN
5979 if ($hash_parent eq '--cc') {
5980 $formats_nav .= ' | ' .
5981 $cgi->a({-href => href(action=>"commitdiff",
5982 hash=>$hash, hash_parent=>'-c')},
5983 'combined');
5984 } else { # $hash_parent eq '-c'
5985 $formats_nav .= ' | ' .
5986 $cgi->a({-href => href(action=>"commitdiff",
5987 hash=>$hash, hash_parent=>'--cc')},
5988 'compact');
5989 }
151602df
JN
5990 $formats_nav .=
5991 ' (merge: ' .
5992 join(' ', map {
5993 $cgi->a({-href => href(action=>"commitdiff",
5994 hash=>$_)},
5995 esc_html(substr($_, 0, 7)));
5996 } @{$co{'parents'}} ) .
5997 ')';
5998 }
5999 }
6000
fb1dde4a 6001 my $hash_parent_param = $hash_parent;
cd030c3a
JN
6002 if (!defined $hash_parent_param) {
6003 # --cc for multiple parents, --root for parentless
fb1dde4a 6004 $hash_parent_param =
cd030c3a 6005 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
bddec01d 6006 }
eee08903
JN
6007
6008 # read commitdiff
6009 my $fd;
6010 my @difftree;
eee08903 6011 if ($format eq 'html') {
25691fbe 6012 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
45bd0c80 6013 "--no-commit-id", "--patch-with-raw", "--full-index",
fb1dde4a 6014 $hash_parent_param, $hash, "--"
074afaa0 6015 or die_error(500, "Open git-diff-tree failed");
eee08903 6016
04408c35
JN
6017 while (my $line = <$fd>) {
6018 chomp $line;
eee08903
JN
6019 # empty line ends raw part of diff-tree output
6020 last unless $line;
493e01db 6021 push @difftree, scalar parse_difftree_raw_line($line);
eee08903 6022 }
eee08903 6023
eee08903 6024 } elsif ($format eq 'plain') {
25691fbe 6025 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
fb1dde4a 6026 '-p', $hash_parent_param, $hash, "--"
074afaa0 6027 or die_error(500, "Open git-diff-tree failed");
9872cd6f
GB
6028 } elsif ($format eq 'patch') {
6029 # For commit ranges, we limit the output to the number of
6030 # patches specified in the 'patches' feature.
6031 # For single commits, we limit the output to a single patch,
6032 # diverging from the git-format-patch default.
6033 my @commit_spec = ();
6034 if ($hash_parent) {
6035 if ($patch_max > 0) {
6036 push @commit_spec, "-$patch_max";
6037 }
6038 push @commit_spec, '-n', "$hash_parent..$hash";
6039 } else {
a3411f8a
GB
6040 if ($params{-single}) {
6041 push @commit_spec, '-1';
6042 } else {
6043 if ($patch_max > 0) {
6044 push @commit_spec, "-$patch_max";
6045 }
6046 push @commit_spec, "-n";
6047 }
6048 push @commit_spec, '--root', $hash;
9872cd6f
GB
6049 }
6050 open $fd, "-|", git_cmd(), "format-patch", '--encoding=utf8',
6051 '--stdout', @commit_spec
6052 or die_error(500, "Open git-format-patch failed");
eee08903 6053 } else {
074afaa0 6054 die_error(400, "Unknown commitdiff format");
eee08903 6055 }
161332a5 6056
11044297
KS
6057 # non-textual hash id's can be cached
6058 my $expires;
6059 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6060 $expires = "+1d";
6061 }
09bd7898 6062
eee08903
JN
6063 # write commit message
6064 if ($format eq 'html') {
6065 my $refs = git_get_references();
6066 my $ref = format_ref_marker($refs, $co{'id'});
1b1cd421 6067
eee08903
JN
6068 git_header_html(undef, $expires);
6069 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6070 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
f88bafad
GB
6071 print "<div class=\"title_text\">\n" .
6072 "<table class=\"object_header\">\n";
6073 git_print_authorship_rows(\%co);
6074 print "</table>".
6075 "</div>\n";
eee08903 6076 print "<div class=\"page_body\">\n";
82560983
JN
6077 if (@{$co{'comment'}} > 1) {
6078 print "<div class=\"log\">\n";
6079 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6080 print "</div>\n"; # class="log"
6081 }
eee08903
JN
6082
6083 } elsif ($format eq 'plain') {
6084 my $refs = git_get_references("tags");
edf735ab 6085 my $tagname = git_get_rev_name_tags($hash);
eee08903
JN
6086 my $filename = basename($project) . "-$hash.patch";
6087
6088 print $cgi->header(
6089 -type => 'text/plain',
6090 -charset => 'utf-8',
6091 -expires => $expires,
a2a3bf7b 6092 -content_disposition => 'inline; filename="' . "$filename" . '"');
eee08903 6093 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
7720224c
YS
6094 print "From: " . to_utf8($co{'author'}) . "\n";
6095 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6096 print "Subject: " . to_utf8($co{'title'}) . "\n";
6097
edf735ab 6098 print "X-Git-Tag: $tagname\n" if $tagname;
eee08903 6099 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
edf735ab 6100
eee08903 6101 foreach my $line (@{$co{'comment'}}) {
7720224c 6102 print to_utf8($line) . "\n";
eee08903
JN
6103 }
6104 print "---\n\n";
9872cd6f
GB
6105 } elsif ($format eq 'patch') {
6106 my $filename = basename($project) . "-$hash.patch";
6107
6108 print $cgi->header(
6109 -type => 'text/plain',
6110 -charset => 'utf-8',
6111 -expires => $expires,
6112 -content_disposition => 'inline; filename="' . "$filename" . '"');
1b1cd421 6113 }
1b1cd421 6114
eee08903
JN
6115 # write patch
6116 if ($format eq 'html') {
cd030c3a
JN
6117 my $use_parents = !defined $hash_parent ||
6118 $hash_parent eq '-c' || $hash_parent eq '--cc';
6119 git_difftree_body(\@difftree, $hash,
6120 $use_parents ? @{$co{'parents'}} : $hash_parent);
b4657e77 6121 print "<br/>\n";
1b1cd421 6122
cd030c3a
JN
6123 git_patchset_body($fd, \@difftree, $hash,
6124 $use_parents ? @{$co{'parents'}} : $hash_parent);
157e43b4 6125 close $fd;
eee08903
JN
6126 print "</div>\n"; # class="page_body"
6127 git_footer_html();
6128
6129 } elsif ($format eq 'plain') {
6130 local $/ = undef;
6131 print <$fd>;
6132 close $fd
6133 or print "Reading git-diff-tree failed\n";
9872cd6f
GB
6134 } elsif ($format eq 'patch') {
6135 local $/ = undef;
6136 print <$fd>;
6137 close $fd
6138 or print "Reading git-format-patch failed\n";
19806691
KS
6139 }
6140}
6141
eee08903 6142sub git_commitdiff_plain {
20209854 6143 git_commitdiff(-format => 'plain');
eee08903
JN
6144}
6145
9872cd6f
GB
6146# format-patch-style patches
6147sub git_patch {
1655c987 6148 git_commitdiff(-format => 'patch', -single => 1);
a3411f8a
GB
6149}
6150
6151sub git_patches {
20209854 6152 git_commitdiff(-format => 'patch');
eee08903
JN
6153}
6154
09bd7898 6155sub git_history {
69ca37d2
JN
6156 git_log_generic('history', \&git_history_body,
6157 $hash_base, $hash_parent_base,
6158 $file_name, $hash);
161332a5 6159}
19806691
KS
6160
6161sub git_search {
25b2790f 6162 gitweb_check_feature('search') or die_error(403, "Search is disabled");
19806691 6163 if (!defined $searchtext) {
074afaa0 6164 die_error(400, "Text field is empty");
19806691
KS
6165 }
6166 if (!defined $hash) {
847e01fb 6167 $hash = git_get_head_hash($project);
19806691 6168 }
847e01fb 6169 my %co = parse_commit($hash);
19806691 6170 if (!%co) {
074afaa0 6171 die_error(404, "Unknown commit object");
19806691 6172 }
8dbc0fce
RF
6173 if (!defined $page) {
6174 $page = 0;
6175 }
04f7a94f 6176
88ad729b
PB
6177 $searchtype ||= 'commit';
6178 if ($searchtype eq 'pickaxe') {
04f7a94f
JN
6179 # pickaxe may take all resources of your box and run for several minutes
6180 # with every query - so decide by yourself how public you make this feature
25b2790f 6181 gitweb_check_feature('pickaxe')
074afaa0 6182 or die_error(403, "Pickaxe is disabled");
c994d620 6183 }
e7738553 6184 if ($searchtype eq 'grep') {
25b2790f 6185 gitweb_check_feature('grep')
074afaa0 6186 or die_error(403, "Grep is disabled");
e7738553 6187 }
88ad729b 6188
19806691 6189 git_header_html();
19806691 6190
88ad729b 6191 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
8e574fb5
RF
6192 my $greptype;
6193 if ($searchtype eq 'commit') {
6194 $greptype = "--grep=";
6195 } elsif ($searchtype eq 'author') {
6196 $greptype = "--author=";
6197 } elsif ($searchtype eq 'committer') {
6198 $greptype = "--committer=";
6199 }
0270cd0e
JN
6200 $greptype .= $searchtext;
6201 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
0e559919
PB
6202 $greptype, '--regexp-ignore-case',
6203 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
8dbc0fce
RF
6204
6205 my $paging_nav = '';
6206 if ($page > 0) {
6207 $paging_nav .=
6208 $cgi->a({-href => href(action=>"search", hash=>$hash,
0270cd0e
JN
6209 searchtext=>$searchtext,
6210 searchtype=>$searchtype)},
a23f0a73 6211 "first");
8dbc0fce 6212 $paging_nav .= " &sdot; " .
7afd77bf 6213 $cgi->a({-href => href(-replay=>1, page=>$page-1),
a23f0a73 6214 -accesskey => "p", -title => "Alt-p"}, "prev");
8dbc0fce
RF
6215 } else {
6216 $paging_nav .= "first";
6217 $paging_nav .= " &sdot; prev";
6218 }
7afd77bf 6219 my $next_link = '';
5ad66088 6220 if ($#commitlist >= 100) {
7afd77bf
JN
6221 $next_link =
6222 $cgi->a({-href => href(-replay=>1, page=>$page+1),
a23f0a73 6223 -accesskey => "n", -title => "Alt-n"}, "next");
7afd77bf 6224 $paging_nav .= " &sdot; $next_link";
8dbc0fce
RF
6225 } else {
6226 $paging_nav .= " &sdot; next";
6227 }
7afd77bf 6228
5ad66088 6229 if ($#commitlist >= 100) {
8dbc0fce
RF
6230 }
6231
6232 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6233 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5ad66088 6234 git_search_grep_body(\@commitlist, 0, 99, $next_link);
c994d620
KS
6235 }
6236
88ad729b 6237 if ($searchtype eq 'pickaxe') {
8dbc0fce
RF
6238 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6239 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6240
591ebf65 6241 print "<table class=\"pickaxe search\">\n";
8dbc0fce 6242 my $alternate = 1;
34122b57 6243 local $/ = "\n";
c582abae
JN
6244 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6245 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6246 ($search_use_regexp ? '--pickaxe-regex' : ());
c994d620
KS
6247 undef %co;
6248 my @files;
6249 while (my $line = <$fd>) {
c582abae
JN
6250 chomp $line;
6251 next unless $line;
6252
6253 my %set = parse_difftree_raw_line($line);
6254 if (defined $set{'commit'}) {
6255 # finish previous commit
c994d620 6256 if (%co) {
c994d620
KS
6257 print "</td>\n" .
6258 "<td class=\"link\">" .
756d2f06 6259 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
952c65fc
JN
6260 " | " .
6261 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
c994d620
KS
6262 print "</td>\n" .
6263 "</tr>\n";
6264 }
c582abae
JN
6265
6266 if ($alternate) {
6267 print "<tr class=\"dark\">\n";
6268 } else {
6269 print "<tr class=\"light\">\n";
6270 }
6271 $alternate ^= 1;
6272 %co = parse_commit($set{'commit'});
6273 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6274 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6275 "<td><i>$author</i></td>\n" .
6276 "<td>" .
6277 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6278 -class => "list subject"},
6279 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6280 } elsif (defined $set{'to_id'}) {
6281 next if ($set{'to_id'} =~ m/^0{40}$/);
6282
6283 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6284 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6285 -class => "list"},
6286 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6287 "<br/>\n";
19806691 6288 }
19806691 6289 }
c994d620 6290 close $fd;
8dbc0fce 6291
c582abae
JN
6292 # finish last commit (warning: repetition!)
6293 if (%co) {
6294 print "</td>\n" .
6295 "<td class=\"link\">" .
6296 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6297 " | " .
6298 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6299 print "</td>\n" .
6300 "</tr>\n";
6301 }
6302
8dbc0fce 6303 print "</table>\n";
19806691 6304 }
e7738553
PB
6305
6306 if ($searchtype eq 'grep') {
6307 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6308 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6309
591ebf65 6310 print "<table class=\"grep_search\">\n";
e7738553
PB
6311 my $alternate = 1;
6312 my $matches = 0;
34122b57 6313 local $/ = "\n";
0e559919
PB
6314 open my $fd, "-|", git_cmd(), 'grep', '-n',
6315 $search_use_regexp ? ('-E', '-i') : '-F',
6316 $searchtext, $co{'tree'};
e7738553
PB
6317 my $lastfile = '';
6318 while (my $line = <$fd>) {
6319 chomp $line;
6320 my ($file, $lno, $ltext, $binary);
6321 last if ($matches++ > 1000);
6322 if ($line =~ /^Binary file (.+) matches$/) {
6323 $file = $1;
6324 $binary = 1;
6325 } else {
6326 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6327 }
6328 if ($file ne $lastfile) {
6329 $lastfile and print "</td></tr>\n";
6330 if ($alternate++) {
6331 print "<tr class=\"dark\">\n";
6332 } else {
6333 print "<tr class=\"light\">\n";
6334 }
6335 print "<td class=\"list\">".
6336 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6337 file_name=>"$file"),
6338 -class => "list"}, esc_path($file));
6339 print "</td><td>\n";
6340 $lastfile = $file;
6341 }
6342 if ($binary) {
6343 print "<div class=\"binary\">Binary file</div>\n";
6344 } else {
6345 $ltext = untabify($ltext);
0e559919 6346 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
e7738553
PB
6347 $ltext = esc_html($1, -nbsp=>1);
6348 $ltext .= '<span class="match">';
6349 $ltext .= esc_html($2, -nbsp=>1);
6350 $ltext .= '</span>';
6351 $ltext .= esc_html($3, -nbsp=>1);
6352 } else {
6353 $ltext = esc_html($ltext, -nbsp=>1);
6354 }
6355 print "<div class=\"pre\">" .
6356 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6357 file_name=>"$file").'#l'.$lno,
6358 -class => "linenr"}, sprintf('%4i', $lno))
6359 . ' ' . $ltext . "</div>\n";
6360 }
6361 }
6362 if ($lastfile) {
6363 print "</td></tr>\n";
6364 if ($matches > 1000) {
6365 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6366 }
6367 } else {
6368 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6369 }
6370 close $fd;
6371
6372 print "</table>\n";
6373 }
19806691
KS
6374 git_footer_html();
6375}
6376
88ad729b
PB
6377sub git_search_help {
6378 git_header_html();
6379 git_print_page_nav('','', $hash,$hash,$hash);
6380 print <<EOT;
0e559919
PB
6381<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6382regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6383the pattern entered is recognized as the POSIX extended
6384<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6385insensitive).</p>
88ad729b
PB
6386<dl>
6387<dt><b>commit</b></dt>
0e559919 6388<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
e7738553 6389EOT
25b2790f 6390 my $have_grep = gitweb_check_feature('grep');
e7738553
PB
6391 if ($have_grep) {
6392 print <<EOT;
6393<dt><b>grep</b></dt>
6394<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
0e559919
PB
6395 a different one) are searched for the given pattern. On large trees, this search can take
6396a while and put some strain on the server, so please use it with some consideration. Note that
6397due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6398case-sensitive.</dd>
e7738553
PB
6399EOT
6400 }
6401 print <<EOT;
88ad729b 6402<dt><b>author</b></dt>
0e559919 6403<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
88ad729b 6404<dt><b>committer</b></dt>
0e559919 6405<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
88ad729b 6406EOT
25b2790f 6407 my $have_pickaxe = gitweb_check_feature('pickaxe');
88ad729b
PB
6408 if ($have_pickaxe) {
6409 print <<EOT;
6410<dt><b>pickaxe</b></dt>
6411<dd>All commits that caused the string to appear or disappear from any file (changes that
6412added, removed or "modified" the string) will be listed. This search can take a while and
0e559919
PB
6413takes a lot of strain on the server, so please use it wisely. Note that since you may be
6414interested even in changes just changing the case as well, this search is case sensitive.</dd>
88ad729b
PB
6415EOT
6416 }
6417 print "</dl>\n";
6418 git_footer_html();
6419}
6420
19806691 6421sub git_shortlog {
69ca37d2
JN
6422 git_log_generic('shortlog', \&git_shortlog_body,
6423 $hash, $hash_parent);
19806691 6424}
717b8311
JN
6425
6426## ......................................................................
af6feeb2 6427## feeds (RSS, Atom; OPML)
717b8311 6428
af6feeb2
JN
6429sub git_feed {
6430 my $format = shift || 'atom';
25b2790f 6431 my $have_blame = gitweb_check_feature('blame');
af6feeb2
JN
6432
6433 # Atom: http://www.atomenabled.org/developers/syndication/
6434 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6435 if ($format ne 'rss' && $format ne 'atom') {
074afaa0 6436 die_error(400, "Unknown web feed format");
af6feeb2
JN
6437 }
6438
6439 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6440 my $head = $hash || 'HEAD';
311e552e 6441 my @commitlist = parse_commits($head, 150, 0, $file_name);
af6feeb2
JN
6442
6443 my %latest_commit;
6444 my %latest_date;
6445 my $content_type = "application/$format+xml";
6446 if (defined $cgi->http('HTTP_ACCEPT') &&
6447 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6448 # browser (feed reader) prefers text/xml
6449 $content_type = 'text/xml';
6450 }
b6093a5c
RF
6451 if (defined($commitlist[0])) {
6452 %latest_commit = %{$commitlist[0]};
cd956c73
GB
6453 my $latest_epoch = $latest_commit{'committer_epoch'};
6454 %latest_date = parse_date($latest_epoch);
6455 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6456 if (defined $if_modified) {
6457 my $since;
6458 if (eval { require HTTP::Date; 1; }) {
6459 $since = HTTP::Date::str2time($if_modified);
6460 } elsif (eval { require Time::ParseDate; 1; }) {
6461 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6462 }
6463 if (defined $since && $latest_epoch <= $since) {
6464 print $cgi->header(
6465 -type => $content_type,
6466 -charset => 'utf-8',
6467 -last_modified => $latest_date{'rfc2822'},
6468 -status => '304 Not Modified');
6469 return;
6470 }
6471 }
af6feeb2
JN
6472 print $cgi->header(
6473 -type => $content_type,
6474 -charset => 'utf-8',
6475 -last_modified => $latest_date{'rfc2822'});
6476 } else {
6477 print $cgi->header(
6478 -type => $content_type,
6479 -charset => 'utf-8');
6480 }
6481
6482 # Optimization: skip generating the body if client asks only
6483 # for Last-Modified date.
6484 return if ($cgi->request_method() eq 'HEAD');
6485
6486 # header variables
6487 my $title = "$site_name - $project/$action";
6488 my $feed_type = 'log';
6489 if (defined $hash) {
6490 $title .= " - '$hash'";
6491 $feed_type = 'branch log';
6492 if (defined $file_name) {
6493 $title .= " :: $file_name";
6494 $feed_type = 'history';
6495 }
6496 } elsif (defined $file_name) {
6497 $title .= " - $file_name";
6498 $feed_type = 'history';
6499 }
6500 $title .= " $feed_type";
6501 my $descr = git_get_project_description($project);
6502 if (defined $descr) {
6503 $descr = esc_html($descr);
6504 } else {
6505 $descr = "$project " .
6506 ($format eq 'rss' ? 'RSS' : 'Atom') .
6507 " feed";
6508 }
6509 my $owner = git_get_project_owner($project);
6510 $owner = esc_html($owner);
6511
6512 #header
6513 my $alt_url;
6514 if (defined $file_name) {
6515 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6516 } elsif (defined $hash) {
6517 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6518 } else {
6519 $alt_url = href(-full=>1, action=>"summary");
6520 }
6521 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6522 if ($format eq 'rss') {
6523 print <<XML;
59b9f61a
JN
6524<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6525<channel>
59b9f61a 6526XML
af6feeb2
JN
6527 print "<title>$title</title>\n" .
6528 "<link>$alt_url</link>\n" .
6529 "<description>$descr</description>\n" .
3ac109ae
GB
6530 "<language>en</language>\n" .
6531 # project owner is responsible for 'editorial' content
6532 "<managingEditor>$owner</managingEditor>\n";
1ba68ce2
GB
6533 if (defined $logo || defined $favicon) {
6534 # prefer the logo to the favicon, since RSS
6535 # doesn't allow both
6536 my $img = esc_url($logo || $favicon);
6537 print "<image>\n" .
6538 "<url>$img</url>\n" .
6539 "<title>$title</title>\n" .
6540 "<link>$alt_url</link>\n" .
6541 "</image>\n";
6542 }
0cf31285
GB
6543 if (%latest_date) {
6544 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6545 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6546 }
ad59a7a3 6547 print "<generator>gitweb v.$version/$git_version</generator>\n";
af6feeb2
JN
6548 } elsif ($format eq 'atom') {
6549 print <<XML;
6550<feed xmlns="http://www.w3.org/2005/Atom">
6551XML
6552 print "<title>$title</title>\n" .
6553 "<subtitle>$descr</subtitle>\n" .
6554 '<link rel="alternate" type="text/html" href="' .
6555 $alt_url . '" />' . "\n" .
6556 '<link rel="self" type="' . $content_type . '" href="' .
6557 $cgi->self_url() . '" />' . "\n" .
6558 "<id>" . href(-full=>1) . "</id>\n" .
6559 # use project owner for feed author
6560 "<author><name>$owner</name></author>\n";
6561 if (defined $favicon) {
6562 print "<icon>" . esc_url($favicon) . "</icon>\n";
6563 }
6564 if (defined $logo_url) {
6565 # not twice as wide as tall: 72 x 27 pixels
e1147267 6566 print "<logo>" . esc_url($logo) . "</logo>\n";
af6feeb2
JN
6567 }
6568 if (! %latest_date) {
6569 # dummy date to keep the feed valid until commits trickle in:
6570 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6571 } else {
6572 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6573 }
ad59a7a3 6574 print "<generator version='$version/$git_version'>gitweb</generator>\n";
af6feeb2 6575 }
717b8311 6576
af6feeb2 6577 # contents
b6093a5c
RF
6578 for (my $i = 0; $i <= $#commitlist; $i++) {
6579 my %co = %{$commitlist[$i]};
6580 my $commit = $co{'id'};
717b8311 6581 # we read 150, we always show 30 and the ones more recent than 48 hours
91fd2bf3 6582 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
717b8311
JN
6583 last;
6584 }
91fd2bf3 6585 my %cd = parse_date($co{'author_epoch'});
af6feeb2
JN
6586
6587 # get list of changed files
b6093a5c 6588 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
c906b181
JN
6589 $co{'parent'} || "--root",
6590 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6bcf4b46 6591 or next;
717b8311 6592 my @difftree = map { chomp; $_ } <$fd>;
6bcf4b46
JN
6593 close $fd
6594 or next;
af6feeb2
JN
6595
6596 # print element (entry, item)
e62a641d 6597 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
af6feeb2
JN
6598 if ($format eq 'rss') {
6599 print "<item>\n" .
6600 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6601 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6602 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6603 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6604 "<link>$co_url</link>\n" .
6605 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6606 "<content:encoded>" .
6607 "<![CDATA[\n";
6608 } elsif ($format eq 'atom') {
6609 print "<entry>\n" .
6610 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6611 "<updated>$cd{'iso-8601'}</updated>\n" .
ab23c19d
JN
6612 "<author>\n" .
6613 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6614 if ($co{'author_email'}) {
6615 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6616 }
6617 print "</author>\n" .
af6feeb2 6618 # use committer for contributor
ab23c19d
JN
6619 "<contributor>\n" .
6620 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6621 if ($co{'committer_email'}) {
6622 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6623 }
6624 print "</contributor>\n" .
af6feeb2
JN
6625 "<published>$cd{'iso-8601'}</published>\n" .
6626 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6627 "<id>$co_url</id>\n" .
6628 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6629 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6630 }
717b8311 6631 my $comment = $co{'comment'};
af6feeb2 6632 print "<pre>\n";
717b8311 6633 foreach my $line (@$comment) {
af6feeb2
JN
6634 $line = esc_html($line);
6635 print "$line\n";
717b8311 6636 }
af6feeb2
JN
6637 print "</pre><ul>\n";
6638 foreach my $difftree_line (@difftree) {
6639 my %difftree = parse_difftree_raw_line($difftree_line);
6640 next if !$difftree{'from_id'};
6641
6642 my $file = $difftree{'file'} || $difftree{'to_file'};
6643
6644 print "<li>" .
6645 "[" .
6646 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6647 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6648 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6649 file_name=>$file, file_parent=>$difftree{'from_file'}),
6650 -title => "diff"}, 'D');
6651 if ($have_blame) {
6652 print $cgi->a({-href => href(-full=>1, action=>"blame",
6653 file_name=>$file, hash_base=>$commit),
6654 -title => "blame"}, 'B');
717b8311 6655 }
af6feeb2
JN
6656 # if this is not a feed of a file history
6657 if (!defined $file_name || $file_name ne $file) {
6658 print $cgi->a({-href => href(-full=>1, action=>"history",
6659 file_name=>$file, hash=>$commit),
6660 -title => "history"}, 'H');
6661 }
6662 $file = esc_path($file);
6663 print "] ".
6664 "$file</li>\n";
6665 }
6666 if ($format eq 'rss') {
6667 print "</ul>]]>\n" .
6668 "</content:encoded>\n" .
6669 "</item>\n";
6670 } elsif ($format eq 'atom') {
6671 print "</ul>\n</div>\n" .
6672 "</content>\n" .
6673 "</entry>\n";
717b8311 6674 }
717b8311 6675 }
af6feeb2
JN
6676
6677 # end of feed
6678 if ($format eq 'rss') {
6679 print "</channel>\n</rss>\n";
3278fbc5 6680 } elsif ($format eq 'atom') {
af6feeb2
JN
6681 print "</feed>\n";
6682 }
6683}
6684
6685sub git_rss {
6686 git_feed('rss');
6687}
6688
6689sub git_atom {
6690 git_feed('atom');
717b8311
JN
6691}
6692
6693sub git_opml {
847e01fb 6694 my @list = git_get_projects_list();
717b8311 6695
ae35785e
GB
6696 print $cgi->header(
6697 -type => 'text/xml',
6698 -charset => 'utf-8',
6699 -content_disposition => 'inline; filename="opml.xml"');
6700
59b9f61a
JN
6701 print <<XML;
6702<?xml version="1.0" encoding="utf-8"?>
6703<opml version="1.0">
6704<head>
8be2890c 6705 <title>$site_name OPML Export</title>
59b9f61a
JN
6706</head>
6707<body>
6708<outline text="git RSS feeds">
6709XML
717b8311
JN
6710
6711 foreach my $pr (@list) {
6712 my %proj = %$pr;
847e01fb 6713 my $head = git_get_head_hash($proj{'path'});
717b8311
JN
6714 if (!defined $head) {
6715 next;
6716 }
25691fbe 6717 $git_dir = "$projectroot/$proj{'path'}";
847e01fb 6718 my %co = parse_commit($head);
717b8311
JN
6719 if (!%co) {
6720 next;
6721 }
6722
6723 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
df63fbbf
GB
6724 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
6725 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
717b8311
JN
6726 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
6727 }
59b9f61a
JN
6728 print <<XML;
6729</outline>
6730</body>
6731</opml>
6732XML
717b8311 6733}