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