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