blob: 7a5b23acf2155fb4e39dcb2f20944362cfbe0ac7 [file] [log] [blame]
Kay Sievers161332a2005-08-07 19:49:46 +02001#!/usr/bin/perl
2
Kay Sieversc994d622005-08-07 20:27:18 +02003# gitweb - simple web interface to track changes in git repositories
Kay Sievers22fafb92005-08-07 19:56:59 +02004#
Kay Sievers00cd0792006-05-22 14:30:47 +02005# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6# (C) 2005, Christian Gierke
Kay Sievers823d5dc2005-08-07 19:57:58 +02007#
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02008# This program is licensed under the GPLv2
Kay Sievers161332a2005-08-07 19:49:46 +02009
Ævar Arnfjörð Bjarmasond48b2842010-09-24 20:00:52 +000010use 5.008;
Kay Sievers161332a2005-08-07 19:49:46 +020011use strict;
12use warnings;
Kay Sievers19806692005-08-07 20:26:27 +020013use CGI qw(:standard :escapeHTML -nosticky);
Kay Sievers7403d502005-08-07 20:23:49 +020014use CGI::Util qw(unescape);
Jakub Narebski7a597452010-04-24 16:00:04 +020015use CGI::Carp qw(fatalsToBrowser set_message);
Kay Sievers40c13812005-11-19 17:41:29 +010016use Encode;
Kay Sieversb87d78d2005-08-07 20:21:04 +020017use Fcntl ':mode';
Junio C Hamano7a13b992006-07-31 19:18:34 -070018use File::Find qw();
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +053019use File::Basename qw(basename);
Jakub Narebski3962f1d72010-11-09 19:27:54 +010020use Time::HiRes qw(gettimeofday tv_interval);
Kay Sievers10bb9032005-11-23 04:26:40 +010021binmode STDOUT, ':utf8';
Kay Sievers161332a2005-08-07 19:49:46 +020022
Jeff King13dbf462014-11-18 12:10:22 -050023if (!defined($CGI::VERSION) || $CGI::VERSION < 4.08) {
24 eval 'sub CGI::multi_param { CGI::param(@_) }'
25}
26
Jakub Narebski3962f1d72010-11-09 19:27:54 +010027our $t0 = [ gettimeofday() ];
Jakub Narebskiaa7dd052009-09-01 13:39:16 +020028our $number_of_git_cmds = 0;
29
Jakub Narebskib1f5f642006-12-28 00:00:52 +010030BEGIN {
Jakub Narebski3be8e722007-04-01 22:22:21 +020031 CGI->compile() if $ENV{'MOD_PERL'};
Jakub Narebskib1f5f642006-12-28 00:00:52 +010032}
33
Junio C Hamano06c084d2006-08-02 13:50:20 -070034our $version = "++GIT_VERSION++";
Kay Sievers44ad2972005-08-07 19:59:24 +020035
Jakub Narebskic2394fe2010-05-07 14:54:04 +020036our ($my_url, $my_uri, $base_url, $path_info, $home_link);
37sub evaluate_uri {
38 our $cgi;
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +010039
Jakub Narebskic2394fe2010-05-07 14:54:04 +020040 our $my_url = $cgi->url();
41 our $my_uri = $cgi->url(-absolute => 1);
42
43 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
44 # needed and used only for URLs with nonempty PATH_INFO
45 our $base_url = $my_url;
46
47 # When the script is used as DirectoryIndex, the URL does not contain the name
48 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
49 # have to do it ourselves. We make $path_info global because it's also used
50 # later on.
51 #
52 # Another issue with the script being the DirectoryIndex is that the resulting
53 # $my_url data is not the full script URL: this is good, because we want
54 # generated links to keep implying the script name if it wasn't explicitly
55 # indicated in the URL we're handling, but it means that $my_url cannot be used
56 # as base URL.
57 # Therefore, if we needed to strip PATH_INFO, then we know that we have
58 # to build the base URL ourselves:
Jakub Narebski84d9e2d2012-02-03 13:44:54 +010059 our $path_info = decode_utf8($ENV{"PATH_INFO"});
Jakub Narebskic2394fe2010-05-07 14:54:04 +020060 if ($path_info) {
Jay Soffiancacfc092012-08-08 22:29:26 -040061 # $path_info has already been URL-decoded by the web server, but
62 # $my_url and $my_uri have not. URL-decode them so we can properly
63 # strip $path_info.
64 $my_url = unescape($my_url);
65 $my_uri = unescape($my_uri);
Jakub Narebskic2394fe2010-05-07 14:54:04 +020066 if ($my_url =~ s,\Q$path_info\E$,, &&
67 $my_uri =~ s,\Q$path_info\E$,, &&
68 defined $ENV{'SCRIPT_NAME'}) {
69 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
70 }
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +010071 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +020072
73 # target of the home link on top of all pages
74 our $home_link = $my_uri || "/";
Giuseppe Bilottab65910f2008-09-29 15:07:42 +020075}
76
Alp Tokere130dda2006-07-12 23:55:10 +010077# core git executable to use
78# this can just be "git" if your webserver has a sensible PATH
Junio C Hamano06c084d2006-08-02 13:50:20 -070079our $GIT = "++GIT_BINDIR++/git";
Jakub Narebski3f7f27102006-06-21 09:48:04 +020080
Kay Sieversb87d78d2005-08-07 20:21:04 +020081# absolute fs-path which will be prepended to the project path
Dennis Stosberg4a87b432006-06-21 15:07:08 +020082#our $projectroot = "/pub/scm";
Junio C Hamano06c084d2006-08-02 13:50:20 -070083our $projectroot = "++GITWEB_PROJECTROOT++";
Kay Sieversb87d78d2005-08-07 20:21:04 +020084
Luke Luca5e9492007-10-16 20:45:25 -070085# fs traversing limit for getting project list
86# the number is relative to the projectroot
87our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
88
Yasushi SHOJI2de21fa2006-08-15 07:50:49 +090089# string of the home link on top of all pages
90our $home_link_str = "++GITWEB_HOME_LINK_STR++";
91
Tony Finchad9c2e22013-07-04 18:02:12 +010092# extra breadcrumbs preceding the home link
93our @extra_breadcrumbs = ();
94
Alp Toker49da1da2006-07-11 21:10:26 +010095# name of your site or organization to appear in page titles
96# replace this with something more descriptive for clearer bookmarks
Petr Baudis8be28902006-10-24 05:18:39 +020097our $site_name = "++GITWEB_SITENAME++"
98 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
Alp Toker49da1da2006-07-11 21:10:26 +010099
Lénaïc Huardc1355b72011-10-21 09:09:29 +0200100# html snippet to include in the <head> section of each page
101our $site_html_head_string = "++GITWEB_SITE_HTML_HEAD_STRING++";
Alan Chandlerb2d34762006-10-03 13:49:03 +0100102# filename of html text to include at top of each page
103our $site_header = "++GITWEB_SITE_HEADER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +0200104# html text to include at home page
Junio C Hamano06c084d2006-08-02 13:50:20 -0700105our $home_text = "++GITWEB_HOMETEXT++";
Alan Chandlerb2d34762006-10-03 13:49:03 +0100106# filename of html text to include at bottom of each page
107our $site_footer = "++GITWEB_SITE_FOOTER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +0200108
Alan Chandlerb2d34762006-10-03 13:49:03 +0100109# URI of stylesheets
110our @stylesheets = ("++GITWEB_CSS++");
Petr Baudis887a6122006-10-26 14:41:25 +0200111# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
112our $stylesheet = undef;
Jakub Narebski9a7a62f2006-10-06 12:31:05 +0200113# URI of GIT logo (72x27 size)
Junio C Hamano06c084d2006-08-02 13:50:20 -0700114our $logo = "++GITWEB_LOGO++";
Jakub Narebski0b5deba2006-09-04 20:32:13 +0200115# URI of GIT favicon, assumed to be image/png type
116our $favicon = "++GITWEB_FAVICON++";
Jakub Narebski4af819d2009-09-01 13:39:17 +0200117# URI of gitweb.js (JavaScript code for gitweb)
118our $javascript = "++GITWEB_JS++";
Jakub Narebskiaedd9422006-06-17 11:23:56 +0200119
Jakub Narebski9a7a62f2006-10-06 12:31:05 +0200120# URI and label (title) of GIT logo link
121#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
122#our $logo_label = "git documentation";
Wincent Colaiuta69fb8282009-07-12 14:31:28 +0200123our $logo_url = "http://git-scm.com/";
Jakub Narebski9a7a62f2006-10-06 12:31:05 +0200124our $logo_label = "git homepage";
Junio C Hamano51a7c662006-09-23 12:36:01 -0700125
Kay Sievers09bd7892005-08-07 20:21:23 +0200126# source of projects list
Junio C Hamano06c084d2006-08-02 13:50:20 -0700127our $projects_list = "++GITWEB_LIST++";
Kay Sieversb87d78d2005-08-07 20:21:04 +0200128
Michael Hendricks55feb122007-07-04 18:36:48 -0600129# the width (in characters) of the projects list "Description" column
130our $projects_list_description_width = 25;
131
Sebastien Ceveyd940c902011-04-29 19:52:01 +0200132# group projects by category on the projects list
133# (enabled if this variable evaluates to true)
134our $projects_list_group_categories = 0;
135
136# default category if none specified
137# (leave the empty string for no category)
138our $project_list_default_category = "";
139
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +0200140# default order of projects list
141# valid values are none, project, descr, owner, and age
142our $default_projects_order = "project";
143
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200144# show repository only if this file exists
145# (only effective if this variable evaluates to true)
146our $export_ok = "++GITWEB_EXPORT_OK++";
147
Kacper Kornet5710be42012-04-24 19:39:15 +0200148# don't generate age column on the projects list page
149our $omit_age_column = 0;
150
Kacper Kornet0ebe7822012-04-26 18:45:44 +0200151# don't generate information about owners of repositories
152our $omit_owner=0;
153
Alexander Gavrilovdd7f5f12008-11-06 01:36:23 +0300154# show repository only if this subroutine returns true
155# when given the path to the project, for example:
156# sub { return -e "$_[0]/git-daemon-export-ok"; }
157our $export_auth_hook = undef;
158
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200159# only allow viewing of repositories also shown on the overview page
160our $strict_export = "++GITWEB_STRICT_EXPORT++";
161
Jakub Narebski19a87212006-08-15 23:03:17 +0200162# list of git base URLs used for URL to where fetch project from,
163# i.e. full URL is "$git_base_url/$project"
Jakub Narebskid6b7e0b2006-10-26 12:26:44 +0200164our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
Jakub Narebski19a87212006-08-15 23:03:17 +0200165
Jakub Narebskif5aa79d2006-06-17 13:32:15 +0200166# default blob_plain mimetype and default charset for text/plain blob
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200167our $default_blob_plain_mimetype = 'text/plain';
168our $default_text_plain_charset = undef;
Jakub Narebskif5aa79d2006-06-17 13:32:15 +0200169
Petr Baudis2d007372006-06-18 00:01:06 +0200170# file to use for guessing MIME types before trying /etc/mime.types
171# (relative to the current git repository)
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200172our $mimetypes_file = undef;
Petr Baudis2d007372006-06-18 00:01:06 +0200173
Martin Koegler00f429a2007-06-03 17:42:44 +0200174# assume this charset if line contains non-UTF-8 characters;
175# it should be valid encoding (see Encoding::Supported(3pm) for list),
176# for which encoding all byte sequences are valid, for example
177# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
178# could be even 'utf-8' for the old behavior)
179our $fallback_encoding = 'latin1';
180
Jakub Narebski69a9b412007-07-20 02:15:09 +0200181# rename detection options for git-diff and git-diff-tree
182# - default is '-M', with the cost proportional to
183# (number of removed files) * (number of new files).
184# - more costly is '-C' (which implies '-M'), with the cost proportional to
185# (number of changed files + number of removed files) * (number of new files)
186# - even more costly is '-C', '--find-copies-harder' with cost
187# (number of files in the original tree) * (number of new files)
188# - one might want to include '-B' option, e.g. '-B', '-M'
189our @diff_opts = ('-M'); # taken from git_commit
190
Matt McCutchen7e1100e2009-02-07 19:00:09 -0500191# Disables features that would allow repository owners to inject script into
192# the gitweb domain.
193our $prevent_xss = 0;
194
Christopher Wilson7ce896b2010-09-21 00:25:19 -0700195# Path to the highlight executable to use (must be the one from
196# http://www.andre-simon.de due to assumptions about parameters and output).
197# Useful if highlight is not installed on your webserver's PATH.
198# [Default: highlight]
199our $highlight_bin = "++HIGHLIGHT_BIN++";
200
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200201# information about snapshot formats that gitweb is capable of serving
202our %known_snapshot_formats = (
203 # name => {
204 # 'display' => display name,
205 # 'type' => mime type,
206 # 'suffix' => filename suffix,
207 # 'format' => --format for git-archive,
208 # 'compressor' => [compressor command and arguments]
Mark A Rada1bfd3632009-08-06 10:25:39 -0400209 # (array reference, optional)
210 # 'disabled' => boolean (optional)}
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200211 #
212 'tgz' => {
213 'display' => 'tar.gz',
214 'type' => 'application/x-gzip',
215 'suffix' => '.tar.gz',
216 'format' => 'tar',
Fraser Tweedale0c8c3852011-04-26 11:32:00 +1000217 'compressor' => ['gzip', '-n']},
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200218
219 'tbz2' => {
220 'display' => 'tar.bz2',
221 'type' => 'application/x-bzip2',
222 'suffix' => '.tar.bz2',
223 'format' => 'tar',
224 'compressor' => ['bzip2']},
225
Mark A Radacbdefb52009-08-06 10:28:25 -0400226 'txz' => {
227 'display' => 'tar.xz',
228 'type' => 'application/x-xz',
229 'suffix' => '.tar.xz',
230 'format' => 'tar',
231 'compressor' => ['xz'],
232 'disabled' => 1},
233
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200234 'zip' => {
235 'display' => 'zip',
236 'type' => 'application/x-zip',
237 'suffix' => '.zip',
238 'format' => 'zip'},
239);
240
241# Aliases so we understand old gitweb.snapshot values in repository
242# configuration.
243our %known_snapshot_format_aliases = (
244 'gzip' => 'tgz',
245 'bzip2' => 'tbz2',
Mark A Radacbdefb52009-08-06 10:28:25 -0400246 'xz' => 'txz',
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200247
248 # backward compatibility: legacy gitweb config support
249 'x-gzip' => undef, 'gz' => undef,
250 'x-bzip2' => undef, 'bz2' => undef,
251 'x-zip' => undef, '' => undef,
252);
253
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200254# Pixel sizes for icons and avatars. If the default font sizes or lineheights
255# are changed, it may be appropriate to change these values too via
256# $GITWEB_CONFIG.
257our %avatar_size = (
258 'default' => 16,
259 'double' => 32
260);
261
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +0100262# Used to set the maximum load that we will still respond to gitweb queries.
263# If server load exceed this value then return "503 server busy" error.
264# If gitweb cannot determined server load, it is taken to be 0.
265# Leave it undefined (or set to 'undef') to turn off load checking.
266our $maxload = 300;
267
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400268# configuration for 'highlight' (http://www.andre-simon.de/)
269# match by basename
270our %highlight_basename = (
271 #'Program' => 'py',
272 #'Library' => 'py',
273 'SConstruct' => 'py', # SCons equivalent of Makefile
274 'Makefile' => 'make',
275);
276# match by extension
277our %highlight_ext = (
278 # main extensions, defining name of syntax;
279 # see files in /usr/share/highlight/langDefs/ directory
Richard Hubbell048b3992012-11-04 09:45:55 -0800280 (map { $_ => $_ } qw(py rb java css js tex bib xml awk bat ini spec tcl sql)),
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400281 # alternate extensions, see /etc/highlight/filetypes.conf
Richard Hubbell048b3992012-11-04 09:45:55 -0800282 (map { $_ => 'c' } qw(c h)),
283 (map { $_ => 'sh' } qw(sh bash zsh ksh)),
284 (map { $_ => 'cpp' } qw(cpp cxx c++ cc)),
285 (map { $_ => 'php' } qw(php php3 php4 php5 phps)),
286 (map { $_ => 'pl' } qw(pl perl pm)), # perhaps also 'cgi'
287 (map { $_ => 'make'} qw(make mak mk)),
288 (map { $_ => 'xml' } qw(xml xhtml html htm)),
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400289);
290
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530291# You define site-wide feature defaults here; override them with
292# $GITWEB_CONFIG as necessary.
Jakub Narebski952c65f2006-08-22 16:52:50 +0200293our %feature = (
Jakub Narebski17848fc2006-08-26 19:14:22 +0200294 # feature => {
295 # 'sub' => feature-sub (subroutine),
296 # 'override' => allow-override (boolean),
297 # 'default' => [ default options...] (array reference)}
298 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200299 # if feature is overridable (it means that allow-override has true value),
Jakub Narebski17848fc2006-08-26 19:14:22 +0200300 # then feature-sub will be called with default options as parameters;
301 # return value of feature-sub indicates if to enable specified feature
302 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200303 # if there is no 'sub' key (no feature-sub), then feature cannot be
Ralf Wildenhues22e5e582010-08-22 13:12:12 +0200304 # overridden
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200305 #
Giuseppe Bilottaff3c0ff2008-12-02 14:57:28 -0800306 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
307 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
308 # is enabled
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530309
Petr Baudis45a3b122006-10-07 15:17:47 +0200310 # Enable the 'blame' blob view, showing the last commit that modified
311 # each line in the file. This can be very CPU-intensive.
312
313 # To enable system wide have in $GITWEB_CONFIG
314 # $feature{'blame'}{'default'} = [1];
315 # To have project specific config enable override in $GITWEB_CONFIG
316 # $feature{'blame'}{'override'} = 1;
317 # and in project config gitweb.blame = 0|1;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200318 'blame' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800319 'sub' => sub { feature_bool('blame', @_) },
Jakub Narebski952c65f2006-08-22 16:52:50 +0200320 'override' => 0,
321 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530322
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200323 # Enable the 'snapshot' link, providing a compressed archive of any
Petr Baudis45a3b122006-10-07 15:17:47 +0200324 # tree. This can potentially generate high traffic if you have large
325 # project.
326
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200327 # Value is a list of formats defined in %known_snapshot_formats that
328 # you wish to offer.
Petr Baudis45a3b122006-10-07 15:17:47 +0200329 # To disable system wide have in $GITWEB_CONFIG
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200330 # $feature{'snapshot'}{'default'} = [];
Petr Baudis45a3b122006-10-07 15:17:47 +0200331 # To have project specific config enable override in $GITWEB_CONFIG
Uwe Zeisbergerbbee1d92006-12-08 12:44:31 +0100332 # $feature{'snapshot'}{'override'} = 1;
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200333 # and in project config, a comma-separated list of formats or "none"
334 # to disable. Example: gitweb.snapshot = tbz2,zip;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200335 'snapshot' => {
336 'sub' => \&feature_snapshot,
337 'override' => 0,
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200338 'default' => ['tgz']},
Jakub Narebski04f7a942006-09-11 00:29:27 +0200339
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000340 # Enable text search, which will list the commits which match author,
341 # committer or commit text to a given string. Enabled by default.
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200342 # Project specific override is not supported.
Jakub Narebskie0ca3642011-06-22 17:28:52 +0200343 #
344 # Note that this controls all search features, which means that if
345 # it is disabled, then 'grep' and 'pickaxe' search would also be
346 # disabled.
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000347 'search' => {
348 'override' => 0,
349 'default' => [1]},
350
Petr Baudise7738552007-05-17 04:31:12 +0200351 # Enable grep search, which will list the files in currently selected
352 # tree containing the given string. Enabled by default. This can be
353 # potentially CPU-intensive, of course.
Jakub Narebskia598ded2011-06-21 08:41:16 +0200354 # Note that you need to have 'search' feature enabled too.
Petr Baudise7738552007-05-17 04:31:12 +0200355
356 # To enable system wide have in $GITWEB_CONFIG
357 # $feature{'grep'}{'default'} = [1];
358 # To have project specific config enable override in $GITWEB_CONFIG
359 # $feature{'grep'}{'override'} = 1;
360 # and in project config gitweb.grep = 0|1;
361 'grep' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800362 'sub' => sub { feature_bool('grep', @_) },
Petr Baudise7738552007-05-17 04:31:12 +0200363 'override' => 0,
364 'default' => [1]},
365
Petr Baudis45a3b122006-10-07 15:17:47 +0200366 # Enable the pickaxe search, which will list the commits that modified
367 # a given string in a file. This can be practical and quite faster
368 # alternative to 'blame', but still potentially CPU-intensive.
Jakub Narebskia598ded2011-06-21 08:41:16 +0200369 # Note that you need to have 'search' feature enabled too.
Petr Baudis45a3b122006-10-07 15:17:47 +0200370
371 # To enable system wide have in $GITWEB_CONFIG
372 # $feature{'pickaxe'}{'default'} = [1];
373 # To have project specific config enable override in $GITWEB_CONFIG
374 # $feature{'pickaxe'}{'override'} = 1;
375 # and in project config gitweb.pickaxe = 0|1;
Jakub Narebski04f7a942006-09-11 00:29:27 +0200376 'pickaxe' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800377 'sub' => sub { feature_bool('pickaxe', @_) },
Jakub Narebski04f7a942006-09-11 00:29:27 +0200378 'override' => 0,
379 'default' => [1]},
Martin Waitz9e756902006-10-01 23:57:48 +0200380
Jakub Narebskie4b48ea2009-09-07 14:40:00 +0200381 # Enable showing size of blobs in a 'tree' view, in a separate
382 # column, similar to what 'ls -l' does. This cost a bit of IO.
383
384 # To disable system wide have in $GITWEB_CONFIG
385 # $feature{'show-sizes'}{'default'} = [0];
386 # To have project specific config enable override in $GITWEB_CONFIG
387 # $feature{'show-sizes'}{'override'} = 1;
388 # and in project config gitweb.showsizes = 0|1;
389 'show-sizes' => {
390 'sub' => sub { feature_bool('showsizes', @_) },
391 'override' => 0,
392 'default' => [1]},
393
Petr Baudis45a3b122006-10-07 15:17:47 +0200394 # Make gitweb use an alternative format of the URLs which can be
395 # more readable and natural-looking: project name is embedded
396 # directly in the path and the query string contains other
397 # auxiliary information. All gitweb installations recognize
398 # URL in either format; this configures in which formats gitweb
399 # generates links.
400
401 # To enable system wide have in $GITWEB_CONFIG
402 # $feature{'pathinfo'}{'default'} = [1];
403 # Project specific override is not supported.
404
405 # Note that you will need to change the default location of CSS,
406 # favicon, logo and possibly other files to an absolute URL. Also,
407 # if gitweb.cgi serves as your indexfile, you will need to force
408 # $my_uri to contain the script name in your $GITWEB_CONFIG.
Martin Waitz9e756902006-10-01 23:57:48 +0200409 'pathinfo' => {
410 'override' => 0,
411 'default' => [0]},
Petr Baudise30496d2006-10-24 05:33:17 +0200412
413 # Make gitweb consider projects in project root subdirectories
414 # to be forks of existing projects. Given project $projname.git,
415 # projects matching $projname/*.git will not be shown in the main
416 # projects list, instead a '+' mark will be added to $projname
417 # there and a 'forks' view will be enabled for the project, listing
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +0200418 # all the forks. If project list is taken from a file, forks have
419 # to be listed after the main project.
Petr Baudise30496d2006-10-24 05:33:17 +0200420
421 # To enable system wide have in $GITWEB_CONFIG
422 # $feature{'forks'}{'default'} = [1];
423 # Project specific override is not supported.
424 'forks' => {
425 'override' => 0,
426 'default' => [0]},
Petr Baudisd627f682008-10-02 16:36:52 +0200427
428 # Insert custom links to the action bar of all project pages.
429 # This enables you mainly to link to third-party scripts integrating
430 # into gitweb; e.g. git-browser for graphical history representation
431 # or custom web-based repository administration interface.
432
433 # The 'default' value consists of a list of triplets in the form
434 # (label, link, position) where position is the label after which
Jakub Narebski2b11e052008-10-12 00:02:32 +0200435 # to insert the link and link is a format string where %n expands
Petr Baudisd627f682008-10-02 16:36:52 +0200436 # to the project name, %f to the project path within the filesystem,
437 # %h to the current hash (h gitweb parameter) and %b to the current
Jakub Narebski2b11e052008-10-12 00:02:32 +0200438 # hash base (hb gitweb parameter); %% expands to %.
Petr Baudisd627f682008-10-02 16:36:52 +0200439
440 # To enable system wide have in $GITWEB_CONFIG e.g.
441 # $feature{'actions'}{'default'} = [('graphiclog',
442 # '/git-browser/by-commit.html?r=%n', 'summary')];
443 # Project specific override is not supported.
444 'actions' => {
445 'override' => 0,
446 'default' => []},
Shawn O. Pearce3e3d4ee2008-10-03 07:41:25 -0700447
Jakub Narebski0368c492011-04-29 19:51:57 +0200448 # Allow gitweb scan project content tags of project repository,
449 # and display the popular Web 2.0-ish "tag cloud" near the projects
450 # list. Note that this is something COMPLETELY different from the
451 # normal Git tags.
Petr Baudisaed93de2008-10-02 17:13:02 +0200452
453 # gitweb by itself can show existing tags, but it does not handle
Jakub Narebski0368c492011-04-29 19:51:57 +0200454 # tagging itself; you need to do it externally, outside gitweb.
455 # The format is described in git_get_project_ctags() subroutine.
Petr Baudisaed93de2008-10-02 17:13:02 +0200456 # You may want to install the HTML::TagCloud Perl module to get
457 # a pretty tag cloud instead of just a list of tags.
458
459 # To enable system wide have in $GITWEB_CONFIG
Jakub Narebski0368c492011-04-29 19:51:57 +0200460 # $feature{'ctags'}{'default'} = [1];
Petr Baudisaed93de2008-10-02 17:13:02 +0200461 # Project specific override is not supported.
Jakub Narebski0368c492011-04-29 19:51:57 +0200462
463 # In the future whether ctags editing is enabled might depend
464 # on the value, but using 1 should always mean no editing of ctags.
Petr Baudisaed93de2008-10-02 17:13:02 +0200465 'ctags' => {
466 'override' => 0,
467 'default' => [0]},
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100468
469 # The maximum number of patches in a patchset generated in patch
470 # view. Set this to 0 or undef to disable patch view, or to a
471 # negative number to remove any limit.
472
473 # To disable system wide have in $GITWEB_CONFIG
474 # $feature{'patches'}{'default'} = [0];
475 # To have project specific config enable override in $GITWEB_CONFIG
476 # $feature{'patches'}{'override'} = 1;
477 # and in project config gitweb.patches = 0|n;
478 # where n is the maximum number of patches allowed in a patchset.
479 'patches' => {
480 'sub' => \&feature_patches,
481 'override' => 0,
482 'default' => [16]},
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200483
484 # Avatar support. When this feature is enabled, views such as
485 # shortlog or commit will display an avatar associated with
486 # the email of the committer(s) and/or author(s).
487
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +0200488 # Currently available providers are gravatar and picon.
489 # If an unknown provider is specified, the feature is disabled.
490
491 # Gravatar depends on Digest::MD5.
492 # Picon currently relies on the indiana.edu database.
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200493
494 # To enable system wide have in $GITWEB_CONFIG
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +0200495 # $feature{'avatar'}{'default'} = ['<provider>'];
496 # where <provider> is either gravatar or picon.
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200497 # To have project specific config enable override in $GITWEB_CONFIG
498 # $feature{'avatar'}{'override'} = 1;
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +0200499 # and in project config gitweb.avatar = <provider>;
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200500 'avatar' => {
501 'sub' => \&feature_avatar,
502 'override' => 0,
503 'default' => ['']},
Jakub Narebskiaa7dd052009-09-01 13:39:16 +0200504
505 # Enable displaying how much time and how many git commands
506 # it took to generate and display page. Disabled by default.
507 # Project specific override is not supported.
508 'timed' => {
509 'override' => 0,
510 'default' => [0]},
Jakub Narebskie627e502009-11-26 21:12:15 +0100511
512 # Enable turning some links into links to actions which require
513 # JavaScript to run (like 'blame_incremental'). Not enabled by
514 # default. Project specific override is currently not supported.
515 'javascript-actions' => {
516 'override' => 0,
517 'default' => [0]},
Johannes Schindelinb331fe52010-04-27 21:34:44 +0200518
Jakub Narebski2e987f92011-04-28 21:04:11 +0200519 # Enable and configure ability to change common timezone for dates
520 # in gitweb output via JavaScript. Enabled by default.
521 # Project specific override is not supported.
522 'javascript-timezone' => {
523 'override' => 0,
524 'default' => [
525 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
526 # or undef to turn off this feature
527 'gitweb_tz', # name of cookie where to store selected timezone
528 'datetime', # CSS class used to mark up dates for manipulation
529 ]},
530
Johannes Schindelinb331fe52010-04-27 21:34:44 +0200531 # Syntax highlighting support. This is based on Daniel Svensson's
532 # and Sham Chukoury's work in gitweb-xmms2.git.
Jakub Narebski592ea412010-04-27 21:34:45 +0200533 # It requires the 'highlight' program present in $PATH,
534 # and therefore is disabled by default.
Johannes Schindelinb331fe52010-04-27 21:34:44 +0200535
536 # To enable system wide have in $GITWEB_CONFIG
537 # $feature{'highlight'}{'default'} = [1];
538
539 'highlight' => {
540 'sub' => sub { feature_bool('highlight', @_) },
541 'override' => 0,
542 'default' => [0]},
Giuseppe Bilotta60efa242010-11-11 13:26:09 +0100543
544 # Enable displaying of remote heads in the heads list
545
546 # To enable system wide have in $GITWEB_CONFIG
547 # $feature{'remote_heads'}{'default'} = [1];
548 # To have project specific config enable override in $GITWEB_CONFIG
549 # $feature{'remote_heads'}{'override'} = 1;
Phil Pennockaf507942012-11-05 18:50:47 -0500550 # and in project config gitweb.remoteheads = 0|1;
Giuseppe Bilotta60efa242010-11-11 13:26:09 +0100551 'remote_heads' => {
552 'sub' => sub { feature_bool('remote_heads', @_) },
553 'override' => 0,
554 'default' => [0]},
Krzesimir Nowak8d646a92013-12-11 12:54:43 +0100555
556 # Enable showing branches under other refs in addition to heads
557
558 # To set system wide extra branch refs have in $GITWEB_CONFIG
559 # $feature{'extra-branch-refs'}{'default'} = ['dirs', 'of', 'choice'];
560 # To have project specific config enable override in $GITWEB_CONFIG
561 # $feature{'extra-branch-refs'}{'override'} = 1;
562 # and in project config gitweb.extrabranchrefs = dirs of choice
563 # Every directory is separated with whitespace.
564
565 'extra-branch-refs' => {
566 'sub' => \&feature_extra_branch_refs,
567 'override' => 0,
568 'default' => []},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530569);
570
Junio C Hamanoa7c5a282008-11-29 13:02:08 -0800571sub gitweb_get_feature {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530572 my ($name) = @_;
Jakub Narebskidd1ad5f2006-09-26 01:56:17 +0200573 return unless exists $feature{$name};
Jakub Narebski952c65f2006-08-22 16:52:50 +0200574 my ($sub, $override, @defaults) = (
575 $feature{$name}{'sub'},
576 $feature{$name}{'override'},
577 @{$feature{$name}{'default'}});
Jakub Narebski9be36142010-03-01 22:51:34 +0100578 # project specific override is possible only if we have project
579 our $git_dir; # global variable, declared later
580 if (!$override || !defined $git_dir) {
581 return @defaults;
582 }
Martin Waitza9455912006-10-03 20:07:43 +0200583 if (!defined $sub) {
Nanako Shiraishi93197892009-08-28 12:18:49 +0900584 warn "feature $name is not overridable";
Martin Waitza9455912006-10-03 20:07:43 +0200585 return @defaults;
586 }
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530587 return $sub->(@defaults);
588}
589
Giuseppe Bilotta25b27902008-11-29 13:07:29 -0800590# A wrapper to check if a given feature is enabled.
591# With this, you can say
592#
593# my $bool_feat = gitweb_check_feature('bool_feat');
594# gitweb_check_feature('bool_feat') or somecode;
595#
596# instead of
597#
598# my ($bool_feat) = gitweb_get_feature('bool_feat');
599# (gitweb_get_feature('bool_feat'))[0] or somecode;
600#
601sub gitweb_check_feature {
602 return (gitweb_get_feature(@_))[0];
603}
604
605
Matt Kraaicdad8172008-12-15 22:16:19 -0800606sub feature_bool {
607 my $key = shift;
608 my ($val) = git_get_project_config($key, '--bool');
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530609
Marcel M. Carydf5d10a2009-02-18 14:09:41 +0100610 if (!defined $val) {
611 return ($_[0]);
612 } elsif ($val eq 'true') {
Matt Kraaicdad8172008-12-15 22:16:19 -0800613 return (1);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530614 } elsif ($val eq 'false') {
Matt Kraaicdad8172008-12-15 22:16:19 -0800615 return (0);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530616 }
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530617}
618
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530619sub feature_snapshot {
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200620 my (@fmts) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530621
622 my ($val) = git_get_project_config('snapshot');
623
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200624 if ($val) {
625 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530626 }
627
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200628 return @fmts;
Luben Tuikovde9272f2006-09-28 16:49:21 -0700629}
630
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100631sub feature_patches {
632 my @val = (git_get_project_config('patches', '--int'));
633
634 if (@val) {
635 return @val;
636 }
637
638 return ($_[0]);
639}
640
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200641sub feature_avatar {
642 my @val = (git_get_project_config('avatar'));
643
644 return @val ? @val : @_;
645}
646
Krzesimir Nowak8d646a92013-12-11 12:54:43 +0100647sub feature_extra_branch_refs {
648 my (@branch_refs) = @_;
649 my $values = git_get_project_config('extrabranchrefs');
650
651 if ($values) {
652 $values = config_to_multi ($values);
653 @branch_refs = ();
654 foreach my $value (@{$values}) {
655 push @branch_refs, split /\s+/, $value;
656 }
657 }
658
659 return @branch_refs;
660}
661
Junio C Hamano2172ce42006-10-03 02:30:47 -0700662# checking HEAD file with -e is fragile if the repository was
663# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
664# and then pruned.
665sub check_head_link {
666 my ($dir) = @_;
667 my $headfile = "$dir/HEAD";
668 return ((-e $headfile) ||
669 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
670}
671
672sub check_export_ok {
673 my ($dir) = @_;
674 return (check_head_link($dir) &&
Alexander Gavrilovdd7f5f12008-11-06 01:36:23 +0300675 (!$export_ok || -e "$dir/$export_ok") &&
676 (!$export_auth_hook || $export_auth_hook->($dir)));
Junio C Hamano2172ce42006-10-03 02:30:47 -0700677}
678
Jakub Narebskia7817852007-07-22 23:41:20 +0200679# process alternate names for backward compatibility
680# filter out unsupported (unknown) snapshot formats
681sub filter_snapshot_fmts {
682 my @fmts = @_;
683
684 @fmts = map {
685 exists $known_snapshot_format_aliases{$_} ?
686 $known_snapshot_format_aliases{$_} : $_} @fmts;
Jakub Narebski68cedb12009-05-10 02:40:37 +0200687 @fmts = grep {
Mark A Rada1bfd3632009-08-06 10:25:39 -0400688 exists $known_snapshot_formats{$_} &&
689 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
Jakub Narebskia7817852007-07-22 23:41:20 +0200690}
691
Krzesimir Nowak8d646a92013-12-11 12:54:43 +0100692sub filter_and_validate_refs {
693 my @refs = @_;
694 my %unique_refs = ();
695
696 foreach my $ref (@refs) {
697 die_error(500, "Invalid ref '$ref' in 'extra-branch-refs' feature") unless (is_valid_ref_format($ref));
698 # 'heads' are added implicitly in get_branch_refs().
699 $unique_refs{$ref} = 1 if ($ref ne 'heads');
700 }
701 return sort keys %unique_refs;
702}
703
Jakub Narebskida4b2432010-11-25 19:43:59 +0100704# If it is set to code reference, it is code that it is to be run once per
705# request, allowing updating configurations that change with each request,
706# while running other code in config file only once.
707#
708# Otherwise, if it is false then gitweb would process config file only once;
709# if it is true then gitweb config would be run for each request.
710our $per_request_config = 1;
711
Jakub Narebskif612a712011-05-25 18:35:26 +0200712# read and parse gitweb config file given by its parameter.
713# returns true on success, false on recoverable error, allowing
714# to chain this subroutine, using first file that exists.
715# dies on errors during parsing config file, as it is unrecoverable.
716sub read_config_file {
717 my $filename = shift;
718 return unless defined $filename;
719 # die if there are errors parsing config file
720 if (-e $filename) {
721 do $filename;
722 die $@ if $@;
723 return 1;
724 }
725 return;
726}
727
Jakub Narebski131d6af2011-07-25 00:29:18 +0200728our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200729sub evaluate_gitweb_config {
730 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
731 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
Jakub Narebski131d6af2011-07-25 00:29:18 +0200732 our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "++GITWEB_CONFIG_COMMON++";
Jakub Narebskif612a712011-05-25 18:35:26 +0200733
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +0200734 # Protect against duplications of file names, to not read config twice.
Jakub Narebski131d6af2011-07-25 00:29:18 +0200735 # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
736 # there possibility of duplication of filename there doesn't matter.
737 $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
738 $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
739
740 # Common system-wide settings for convenience.
741 # Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
742 read_config_file($GITWEB_CONFIG_COMMON);
743
744 # Use first config file that exists. This means use the per-instance
745 # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
746 read_config_file($GITWEB_CONFIG) and return;
Jakub Narebskif612a712011-05-25 18:35:26 +0200747 read_config_file($GITWEB_CONFIG_SYSTEM);
Gerrit Pape17a8b252008-03-26 18:11:19 +0000748}
Jeff Kingc8d138a2006-08-02 15:23:34 -0400749
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +0100750# Get loadavg of system, to compare against $maxload.
751# Currently it requires '/proc/loadavg' present to get loadavg;
752# if it is not present it returns 0, which means no load checking.
753sub get_loadavg {
754 if( -e '/proc/loadavg' ){
755 open my $fd, '<', '/proc/loadavg'
756 or return 0;
757 my @load = split(/\s+/, scalar <$fd>);
758 close $fd;
759
760 # The first three columns measure CPU and IO utilization of the last one,
761 # five, and 10 minute periods. The fourth column shows the number of
762 # currently running processes and the total number of processes in the m/n
763 # format. The last column displays the last process ID used.
764 return $load[0] || 0;
765 }
766 # additional checks for load average should go here for things that don't export
767 # /proc/loadavg
768
769 return 0;
770}
771
Jeff Kingc8d138a2006-08-02 15:23:34 -0400772# version of the core git binary
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200773our $git_version;
774sub evaluate_git_version {
775 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
776 $number_of_git_cmds++;
777}
Jeff Kingc8d138a2006-08-02 15:23:34 -0400778
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200779sub check_loadavg {
780 if (defined $maxload && get_loadavg() > $maxload) {
781 die_error(503, "The load average on the server is too high");
782 }
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +0100783}
784
Jakub Narebski154b4d72006-08-05 12:55:20 +0200785# ======================================================================
Kay Sievers09bd7892005-08-07 20:21:23 +0200786# input validation and dispatch
Kay Sieversb87d78d2005-08-07 20:21:04 +0200787
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200788# input parameters can be collected from a variety of sources (presently, CGI
789# and PATH_INFO), so we define an %input_params hash that collects them all
790# together during validation: this allows subsequent uses (e.g. href()) to be
791# agnostic of the parameter origin
Kay Sievers6191f8e2005-08-07 20:19:56 +0200792
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300793our %input_params = ();
Martin Waitz5c95fab2006-08-17 00:28:38 +0200794
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200795# input parameters are stored with the long parameter name as key. This will
796# also be used in the href subroutine to convert parameters to their CGI
797# equivalent, and since the href() usage is the most frequent one, we store
798# the name -> CGI key mapping here, instead of the reverse.
799#
800# XXX: Warning: If you touch this, check the search form for updating,
801# too.
Jakub Narebski24d06932006-09-26 01:57:02 +0200802
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300803our @cgi_param_mapping = (
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200804 project => "p",
805 action => "a",
806 file_name => "f",
807 file_parent => "fp",
808 hash => "h",
809 hash_parent => "hp",
810 hash_base => "hb",
811 hash_parent_base => "hpb",
812 page => "pg",
813 order => "o",
814 searchtext => "s",
815 searchtype => "st",
816 snapshot_format => "sf",
817 extra_options => "opt",
818 search_use_regexp => "sr",
Jakub Narebski0368c492011-04-29 19:51:57 +0200819 ctag => "by_tag",
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +0100820 diff_style => "ds",
Bernhard R. Link19d2d232012-01-30 21:07:37 +0100821 project_filter => "pf",
Jakub Narebskic4ccf612009-09-01 13:39:19 +0200822 # this must be last entry (for manipulation from JavaScript)
823 javascript => "js"
Miklos Vajna868bc062007-07-12 20:39:27 +0200824);
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300825our %cgi_param_mapping = @cgi_param_mapping;
Miklos Vajna868bc062007-07-12 20:39:27 +0200826
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200827# we will also need to know the possible actions, for validation
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300828our %actions = (
Rafael Garcia-Suarez3a5b9192008-06-06 09:53:32 +0200829 "blame" => \&git_blame,
Jakub Narebski4af819d2009-09-01 13:39:17 +0200830 "blame_incremental" => \&git_blame_incremental,
831 "blame_data" => \&git_blame_data,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200832 "blobdiff" => \&git_blobdiff,
833 "blobdiff_plain" => \&git_blobdiff_plain,
834 "blob" => \&git_blob,
835 "blob_plain" => \&git_blob_plain,
836 "commitdiff" => \&git_commitdiff,
837 "commitdiff_plain" => \&git_commitdiff_plain,
838 "commit" => \&git_commit,
Petr Baudise30496d2006-10-24 05:33:17 +0200839 "forks" => \&git_forks,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200840 "heads" => \&git_heads,
841 "history" => \&git_history,
842 "log" => \&git_log,
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100843 "patch" => \&git_patch,
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +0100844 "patches" => \&git_patches,
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +0100845 "remotes" => \&git_remotes,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200846 "rss" => \&git_rss,
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +0100847 "atom" => \&git_atom,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200848 "search" => \&git_search,
Petr Baudis88ad7292006-10-24 05:15:46 +0200849 "search_help" => \&git_search_help,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200850 "shortlog" => \&git_shortlog,
851 "summary" => \&git_summary,
852 "tag" => \&git_tag,
853 "tags" => \&git_tags,
854 "tree" => \&git_tree,
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +0530855 "snapshot" => \&git_snapshot,
Jakub Narebskica946012006-12-10 13:25:47 +0100856 "object" => \&git_object,
Jakub Narebski77a153f2006-08-22 16:59:20 +0200857 # those below don't need $project
858 "opml" => \&git_opml,
859 "project_list" => \&git_project_list,
Jakub Narebskifc2b2be2006-09-15 04:56:03 +0200860 "project_index" => \&git_project_index,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200861);
862
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200863# finally, we have the hash of allowed extra_options for the commands that
864# allow them
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300865our %allowed_options = (
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200866 "--no-merges" => [ qw(rss atom log shortlog history) ],
867);
868
869# fill %input_params with the CGI parameters. All values except for 'opt'
870# should be single values, but opt can be an array. We should probably
871# build an array of parameters that can be multi-valued, but since for the time
872# being it's only this one, we just single it out
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200873sub evaluate_query_params {
874 our $cgi;
875
876 while (my ($name, $symbol) = each %cgi_param_mapping) {
877 if ($symbol eq 'opt') {
Jeff King13dbf462014-11-18 12:10:22 -0500878 $input_params{$name} = [ map { decode_utf8($_) } $cgi->multi_param($symbol) ];
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200879 } else {
Jakub Narebski84d9e2d2012-02-03 13:44:54 +0100880 $input_params{$name} = decode_utf8($cgi->param($symbol));
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200881 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200882 }
883}
884
885# now read PATH_INFO and update the parameter list for missing parameters
886sub evaluate_path_info {
887 return if defined $input_params{'project'};
888 return if !$path_info;
889 $path_info =~ s,^/+,,;
890 return if !$path_info;
891
892 # find which part of PATH_INFO is project
893 my $project = $path_info;
894 $project =~ s,/+$,,;
895 while ($project && !check_head_link("$projectroot/$project")) {
896 $project =~ s,/*[^/]*$,,;
897 }
898 return unless $project;
899 $input_params{'project'} = $project;
900
901 # do not change any parameters if an action is given using the query string
902 return if $input_params{'action'};
903 $path_info =~ s,^\Q$project\E/*,,;
904
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200905 # next, check if we have an action
906 my $action = $path_info;
907 $action =~ s,/.*$,,;
908 if (exists $actions{$action}) {
909 $path_info =~ s,^$action/*,,;
910 $input_params{'action'} = $action;
911 }
912
913 # list of actions that want hash_base instead of hash, but can have no
914 # pathname (f) parameter
915 my @wants_base = (
916 'tree',
917 'history',
918 );
919
Jakub Narebski7e00dc52010-10-13 13:33:48 +0200920 # we want to catch, among others
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200921 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
922 my ($parentrefname, $parentpathname, $refname, $pathname) =
Jakub Narebski7e00dc52010-10-13 13:33:48 +0200923 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200924
925 # first, analyze the 'current' part
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200926 if (defined $pathname) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200927 # we got "branch:filename" or "branch:dir/"
928 # we could use git_get_type(branch:pathname), but:
929 # - it needs $git_dir
930 # - it does a git() call
931 # - the convention of terminating directories with a slash
932 # makes it superfluous
933 # - embedding the action in the PATH_INFO would make it even
934 # more superfluous
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200935 $pathname =~ s,^/+,,;
936 if (!$pathname || substr($pathname, -1) eq "/") {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200937 $input_params{'action'} ||= "tree";
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200938 $pathname =~ s,/$,,;
939 } else {
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200940 # the default action depends on whether we had parent info
941 # or not
942 if ($parentrefname) {
943 $input_params{'action'} ||= "blobdiff_plain";
944 } else {
945 $input_params{'action'} ||= "blob_plain";
946 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200947 }
948 $input_params{'hash_base'} ||= $refname;
949 $input_params{'file_name'} ||= $pathname;
950 } elsif (defined $refname) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200951 # we got "branch". In this case we have to choose if we have to
952 # set hash or hash_base.
953 #
954 # Most of the actions without a pathname only want hash to be
955 # set, except for the ones specified in @wants_base that want
956 # hash_base instead. It should also be noted that hand-crafted
957 # links having 'history' as an action and no pathname or hash
958 # set will fail, but that happens regardless of PATH_INFO.
Jakub Narebskid0af3732010-10-13 13:35:20 +0200959 if (defined $parentrefname) {
960 # if there is parent let the default be 'shortlog' action
961 # (for http://git.example.com/repo.git/A..B links); if there
962 # is no parent, dispatch will detect type of object and set
963 # action appropriately if required (if action is not set)
964 $input_params{'action'} ||= "shortlog";
965 }
966 if ($input_params{'action'} &&
967 grep { $_ eq $input_params{'action'} } @wants_base) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200968 $input_params{'hash_base'} ||= $refname;
969 } else {
970 $input_params{'hash'} ||= $refname;
971 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200972 }
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200973
974 # next, handle the 'parent' part, if present
975 if (defined $parentrefname) {
976 # a missing pathspec defaults to the 'current' filename, allowing e.g.
977 # someproject/blobdiff/oldrev..newrev:/filename
978 if ($parentpathname) {
979 $parentpathname =~ s,^/+,,;
980 $parentpathname =~ s,/$,,;
981 $input_params{'file_parent'} ||= $parentpathname;
982 } else {
983 $input_params{'file_parent'} ||= $input_params{'file_name'};
984 }
985 # we assume that hash_parent_base is wanted if a path was specified,
986 # or if the action wants hash_base instead of hash
987 if (defined $input_params{'file_parent'} ||
988 grep { $_ eq $input_params{'action'} } @wants_base) {
989 $input_params{'hash_parent_base'} ||= $parentrefname;
990 } else {
991 $input_params{'hash_parent'} ||= $parentrefname;
992 }
993 }
Giuseppe Bilotta1ec2fb52008-11-02 10:21:38 +0100994
995 # for the snapshot action, we allow URLs in the form
996 # $project/snapshot/$hash.ext
997 # where .ext determines the snapshot and gets removed from the
998 # passed $refname to provide the $hash.
999 #
1000 # To be able to tell that $refname includes the format extension, we
1001 # require the following two conditions to be satisfied:
1002 # - the hash input parameter MUST have been set from the $refname part
1003 # of the URL (i.e. they must be equal)
1004 # - the snapshot format MUST NOT have been defined already (e.g. from
1005 # CGI parameter sf)
1006 # It's also useless to try any matching unless $refname has a dot,
1007 # so we check for that too
1008 if (defined $input_params{'action'} &&
1009 $input_params{'action'} eq 'snapshot' &&
1010 defined $refname && index($refname, '.') != -1 &&
1011 $refname eq $input_params{'hash'} &&
1012 !defined $input_params{'snapshot_format'}) {
1013 # We loop over the known snapshot formats, checking for
1014 # extensions. Allowed extensions are both the defined suffix
1015 # (which includes the initial dot already) and the snapshot
1016 # format key itself, with a prepended dot
Holger Weißccb4b532009-03-31 18:16:36 +02001017 while (my ($fmt, $opt) = each %known_snapshot_formats) {
Giuseppe Bilotta1ec2fb52008-11-02 10:21:38 +01001018 my $hash = $refname;
Jakub Narebski095e9142009-05-11 19:42:47 +02001019 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
1020 next;
1021 }
1022 my $sfx = $1;
Giuseppe Bilotta1ec2fb52008-11-02 10:21:38 +01001023 # a valid suffix was found, so set the snapshot format
1024 # and reset the hash parameter
1025 $input_params{'snapshot_format'} = $fmt;
1026 $input_params{'hash'} = $hash;
1027 # we also set the format suffix to the one requested
1028 # in the URL: this way a request for e.g. .tgz returns
1029 # a .tgz instead of a .tar.gz
1030 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
1031 last;
1032 }
1033 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001034}
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001035
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001036our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
1037 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
Bernhard R. Link19d2d232012-01-30 21:07:37 +01001038 $searchtext, $search_regexp, $project_filter);
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001039sub evaluate_and_validate_params {
1040 our $action = $input_params{'action'};
1041 if (defined $action) {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001042 if (!is_valid_action($action)) {
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001043 die_error(400, "Invalid action parameter");
1044 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001045 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001046
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001047 # parameters which are pathnames
1048 our $project = $input_params{'project'};
1049 if (defined $project) {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001050 if (!is_valid_project($project)) {
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001051 undef $project;
1052 die_error(404, "No such project");
1053 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001054 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001055
Bernhard R. Link19d2d232012-01-30 21:07:37 +01001056 our $project_filter = $input_params{'project_filter'};
1057 if (defined $project_filter) {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001058 if (!is_valid_pathname($project_filter)) {
Bernhard R. Link19d2d232012-01-30 21:07:37 +01001059 die_error(404, "Invalid project_filter parameter");
1060 }
1061 }
1062
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001063 our $file_name = $input_params{'file_name'};
1064 if (defined $file_name) {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001065 if (!is_valid_pathname($file_name)) {
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001066 die_error(400, "Invalid file parameter");
1067 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001068 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001069
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001070 our $file_parent = $input_params{'file_parent'};
1071 if (defined $file_parent) {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001072 if (!is_valid_pathname($file_parent)) {
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001073 die_error(400, "Invalid file parent parameter");
1074 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001075 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001076
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001077 # parameters which are refnames
1078 our $hash = $input_params{'hash'};
1079 if (defined $hash) {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001080 if (!is_valid_refname($hash)) {
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001081 die_error(400, "Invalid hash parameter");
1082 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001083 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001084
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001085 our $hash_parent = $input_params{'hash_parent'};
1086 if (defined $hash_parent) {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001087 if (!is_valid_refname($hash_parent)) {
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001088 die_error(400, "Invalid hash parent parameter");
1089 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001090 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001091
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001092 our $hash_base = $input_params{'hash_base'};
1093 if (defined $hash_base) {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001094 if (!is_valid_refname($hash_base)) {
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001095 die_error(400, "Invalid hash base parameter");
1096 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001097 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001098
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001099 our @extra_options = @{$input_params{'extra_options'}};
1100 # @extra_options is always defined, since it can only be (currently) set from
1101 # CGI, and $cgi->param() returns the empty array in array context if the param
1102 # is not set
1103 foreach my $opt (@extra_options) {
1104 if (not exists $allowed_options{$opt}) {
1105 die_error(400, "Invalid option parameter");
1106 }
1107 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1108 die_error(400, "Invalid option parameter for this action");
1109 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001110 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001111
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001112 our $hash_parent_base = $input_params{'hash_parent_base'};
1113 if (defined $hash_parent_base) {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001114 if (!is_valid_refname($hash_parent_base)) {
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001115 die_error(400, "Invalid hash parent base parameter");
1116 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001117 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001118
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001119 # other parameters
1120 our $page = $input_params{'page'};
1121 if (defined $page) {
1122 if ($page =~ m/[^0-9]/) {
1123 die_error(400, "Invalid page parameter");
1124 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001125 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001126
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001127 our $searchtype = $input_params{'searchtype'};
1128 if (defined $searchtype) {
1129 if ($searchtype =~ m/[^a-z]/) {
1130 die_error(400, "Invalid searchtype parameter");
1131 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001132 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001133
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001134 our $search_use_regexp = $input_params{'search_use_regexp'};
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001135
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001136 our $searchtext = $input_params{'searchtext'};
Charles McGarveyca7a5dc2013-06-04 22:44:28 -06001137 our $search_regexp = undef;
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001138 if (defined $searchtext) {
1139 if (length($searchtext) < 2) {
1140 die_error(403, "At least two characters are required for search parameter");
1141 }
Jakub Narebski36612e42012-02-28 19:41:47 +01001142 if ($search_use_regexp) {
1143 $search_regexp = $searchtext;
1144 if (!eval { qr/$search_regexp/; 1; }) {
1145 (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1146 die_error(400, "Invalid search regexp '$search_regexp'",
1147 esc_html($error));
1148 }
1149 } else {
1150 $search_regexp = quotemeta $searchtext;
1151 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001152 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001153}
1154
1155# path to the current git repository
1156our $git_dir;
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001157sub evaluate_git_dir {
1158 our $git_dir = "$projectroot/$project" if $project;
1159}
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001160
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01001161our (@snapshot_fmts, $git_avatar, @extra_branch_refs);
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001162sub configure_gitweb_features {
1163 # list of supported snapshot formats
1164 our @snapshot_fmts = gitweb_get_feature('snapshot');
1165 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01001166
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001167 # check that the avatar feature is set to a known provider name,
1168 # and for each provider check if the dependencies are satisfied.
1169 # if the provider name is invalid or the dependencies are not met,
1170 # reset $git_avatar to the empty string.
1171 our ($git_avatar) = gitweb_get_feature('avatar');
1172 if ($git_avatar eq 'gravatar') {
1173 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1174 } elsif ($git_avatar eq 'picon') {
1175 # no dependencies
1176 } else {
1177 $git_avatar = '';
1178 }
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01001179
1180 our @extra_branch_refs = gitweb_get_feature('extra-branch-refs');
1181 @extra_branch_refs = filter_and_validate_refs (@extra_branch_refs);
1182}
1183
1184sub get_branch_refs {
1185 return ('heads', @extra_branch_refs);
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02001186}
1187
Jakub Narebski7a597452010-04-24 16:00:04 +02001188# custom error handler: 'die <message>' is Internal Server Error
1189sub handle_errors_html {
1190 my $msg = shift; # it is already HTML escaped
1191
1192 # to avoid infinite loop where error occurs in die_error,
1193 # change handler to default handler, disabling handle_errors_html
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02001194 set_message("Error occurred when inside die_error:\n$msg");
Jakub Narebski7a597452010-04-24 16:00:04 +02001195
1196 # you cannot jump out of die_error when called as error handler;
1197 # the subroutine set via CGI::Carp::set_message is called _after_
1198 # HTTP headers are already written, so it cannot write them itself
1199 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1200}
1201set_message(\&handle_errors_html);
1202
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001203# dispatch
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001204sub dispatch {
1205 if (!defined $action) {
1206 if (defined $hash) {
1207 $action = git_get_type($hash);
Jakub Narebski18ab83e2012-01-07 11:47:38 +01001208 $action or die_error(404, "Object does not exist");
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001209 } elsif (defined $hash_base && defined $file_name) {
1210 $action = git_get_type("$hash_base:$file_name");
Jakub Narebski18ab83e2012-01-07 11:47:38 +01001211 $action or die_error(404, "File or directory does not exist");
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001212 } elsif (defined $project) {
1213 $action = 'summary';
1214 } else {
1215 $action = 'project_list';
1216 }
Gerrit Pape7f9778b2007-05-10 07:32:07 +00001217 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001218 if (!defined($actions{$action})) {
1219 die_error(400, "Unknown action");
1220 }
1221 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1222 !$project) {
1223 die_error(400, "Project needed");
1224 }
1225 $actions{$action}->();
Jakub Narebski77a153f2006-08-22 16:59:20 +02001226}
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001227
Jakub Narebski869d5882010-07-05 20:52:43 +02001228sub reset_timer {
Jakub Narebski3962f1d72010-11-09 19:27:54 +01001229 our $t0 = [ gettimeofday() ]
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001230 if defined $t0;
Jakub Narebski869d5882010-07-05 20:52:43 +02001231 our $number_of_git_cmds = 0;
1232}
1233
Jakub Narebskida4b2432010-11-25 19:43:59 +01001234our $first_request = 1;
Jakub Narebski869d5882010-07-05 20:52:43 +02001235sub run_request {
1236 reset_timer();
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001237
1238 evaluate_uri();
Jakub Narebskida4b2432010-11-25 19:43:59 +01001239 if ($first_request) {
1240 evaluate_gitweb_config();
1241 evaluate_git_version();
1242 }
1243 if ($per_request_config) {
1244 if (ref($per_request_config) eq 'CODE') {
1245 $per_request_config->();
1246 } elsif (!$first_request) {
1247 evaluate_gitweb_config();
1248 }
1249 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001250 check_loadavg();
1251
Jonathan Nieder7f425db2010-07-30 22:01:59 -05001252 # $projectroot and $projects_list might be set in gitweb config file
1253 $projects_list ||= $projectroot;
1254
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001255 evaluate_query_params();
1256 evaluate_path_info();
1257 evaluate_and_validate_params();
1258 evaluate_git_dir();
1259
1260 configure_gitweb_features();
1261
1262 dispatch();
Kay Sievers09bd7892005-08-07 20:21:23 +02001263}
Sam Vilaina0446e72010-05-07 14:54:05 +02001264
1265our $is_last_request = sub { 1 };
1266our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1267our $CGI = 'CGI';
1268our $cgi;
Jakub Narebski45aa9892010-06-05 23:11:18 +02001269sub configure_as_fcgi {
1270 require CGI::Fast;
1271 our $CGI = 'CGI::Fast';
1272
1273 my $request_number = 0;
1274 # let each child service 100 requests
1275 our $is_last_request = sub { ++$request_number > 100 };
Jakub Narebskid04d3d42006-09-19 21:53:22 +02001276}
Sam Vilaina0446e72010-05-07 14:54:05 +02001277sub evaluate_argv {
Jakub Narebski45aa9892010-06-05 23:11:18 +02001278 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1279 configure_as_fcgi()
1280 if $script_name =~ /\.fcgi$/;
1281
Sam Vilaina0446e72010-05-07 14:54:05 +02001282 return unless (@ARGV);
1283
1284 require Getopt::Long;
1285 Getopt::Long::GetOptions(
Jakub Narebski45aa9892010-06-05 23:11:18 +02001286 'fastcgi|fcgi|f' => \&configure_as_fcgi,
Sam Vilaina0446e72010-05-07 14:54:05 +02001287 'nproc|n=i' => sub {
1288 my ($arg, $val) = @_;
1289 return unless eval { require FCGI::ProcManager; 1; };
1290 my $proc_manager = FCGI::ProcManager->new({
1291 n_processes => $val,
1292 });
1293 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1294 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1295 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1296 },
1297 );
1298}
1299
1300sub run {
1301 evaluate_argv();
Jakub Narebski869d5882010-07-05 20:52:43 +02001302
Jakub Narebskida4b2432010-11-25 19:43:59 +01001303 $first_request = 1;
Sam Vilaina0446e72010-05-07 14:54:05 +02001304 $pre_listen_hook->()
1305 if $pre_listen_hook;
1306
1307 REQUEST:
1308 while ($cgi = $CGI->new()) {
1309 $pre_dispatch_hook->()
1310 if $pre_dispatch_hook;
1311
1312 run_request();
1313
Jakub Narebski0b450102010-08-02 22:21:47 +02001314 $post_dispatch_hook->()
Sam Vilaina0446e72010-05-07 14:54:05 +02001315 if $post_dispatch_hook;
Jakub Narebskida4b2432010-11-25 19:43:59 +01001316 $first_request = 0;
Sam Vilaina0446e72010-05-07 14:54:05 +02001317
1318 last REQUEST if ($is_last_request->());
1319 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001320
1321 DONE_GITWEB:
1322 1;
Kay Sievers09bd7892005-08-07 20:21:23 +02001323}
Sam Vilaina0446e72010-05-07 14:54:05 +02001324
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001325run();
Kay Sievers09bd7892005-08-07 20:21:23 +02001326
Jakub Narebski5ed2ec12010-06-13 12:09:32 +02001327if (defined caller) {
1328 # wrapped in a subroutine processing requests,
1329 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1330 return;
1331} else {
1332 # pure CGI script, serving single request
1333 exit;
1334}
Kay Sievers823d5dc2005-08-07 19:57:58 +02001335
Jakub Narebski717b8312006-07-31 21:22:15 +02001336## ======================================================================
Martin Waitz06a9d862006-08-16 00:23:50 +02001337## action links
1338
Jakub Narebski377bee32010-04-24 15:53:19 +02001339# possible values of extra options
1340# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1341# -replay => 1 - start from a current view (replay with modifications)
1342# -path_info => 0|1 - don't use/use path_info URL (if possible)
Kevin Cernekee5e96a842011-03-18 17:00:16 +01001343# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
Jakub Narebski74fd8722009-05-07 19:11:29 +02001344sub href {
Jakub Narebski498fe002006-08-22 19:05:25 +02001345 my %params = @_;
Jakub Narebskibd5d1e42006-11-19 15:05:21 +01001346 # default is to use -absolute url() i.e. $my_uri
1347 my $href = $params{-full} ? $my_url : $my_uri;
Jakub Narebski498fe002006-08-22 19:05:25 +02001348
Kevin Cernekee5e96a842011-03-18 17:00:16 +01001349 # implicit -replay, must be first of implicit params
1350 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1351
Jakub Narebskiafa9b622008-02-14 09:22:30 +01001352 $params{'project'} = $project unless exists $params{'project'};
1353
Jakub Narebski1cad2832007-11-01 13:06:27 +01001354 if ($params{-replay}) {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001355 while (my ($name, $symbol) = each %cgi_param_mapping) {
Jakub Narebski1cad2832007-11-01 13:06:27 +01001356 if (!exists $params{$name}) {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001357 $params{$name} = $input_params{$name};
Jakub Narebski1cad2832007-11-01 13:06:27 +01001358 }
1359 }
1360 }
1361
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08001362 my $use_pathinfo = gitweb_check_feature('pathinfo');
Jakub Narebski377bee32010-04-24 15:53:19 +02001363 if (defined $params{'project'} &&
1364 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001365 # try to put as many parameters as possible in PATH_INFO:
1366 # - project name
1367 # - action
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001368 # - hash_parent or hash_parent_base:/file_parent
Giuseppe Bilotta3550ea72008-10-21 21:34:52 +02001369 # - hash or hash_base:/filename
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +01001370 # - the snapshot_format as an appropriate suffix
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001371
1372 # When the script is the root DirectoryIndex for the domain,
1373 # $href here would be something like http://gitweb.example.com/
1374 # Thus, we strip any trailing / from $href, to spare us double
1375 # slashes in the final URL
1376 $href =~ s,/$,,;
1377
1378 # Then add the project name, if present
Jakub Narebski67976c62010-12-14 16:54:31 +01001379 $href .= "/".esc_path_info($params{'project'});
Martin Waitz9e756902006-10-01 23:57:48 +02001380 delete $params{'project'};
1381
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +01001382 # since we destructively absorb parameters, we keep this
1383 # boolean that remembers if we're handling a snapshot
1384 my $is_snapshot = $params{'action'} eq 'snapshot';
1385
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001386 # Summary just uses the project path URL, any other action is
1387 # added to the URL
1388 if (defined $params{'action'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001389 $href .= "/".esc_path_info($params{'action'})
1390 unless $params{'action'} eq 'summary';
Martin Waitz9e756902006-10-01 23:57:48 +02001391 delete $params{'action'};
1392 }
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001393
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001394 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1395 # stripping nonexistent or useless pieces
1396 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1397 || $params{'hash_parent'} || $params{'hash'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001398 if (defined $params{'hash_base'}) {
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001399 if (defined $params{'hash_parent_base'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001400 $href .= esc_path_info($params{'hash_parent_base'});
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001401 # skip the file_parent if it's the same as the file_name
Giuseppe Bilottab7da7212009-07-31 08:48:49 +02001402 if (defined $params{'file_parent'}) {
1403 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1404 delete $params{'file_parent'};
1405 } elsif ($params{'file_parent'} !~ /\.\./) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001406 $href .= ":/".esc_path_info($params{'file_parent'});
Giuseppe Bilottab7da7212009-07-31 08:48:49 +02001407 delete $params{'file_parent'};
1408 }
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001409 }
1410 $href .= "..";
1411 delete $params{'hash_parent'};
1412 delete $params{'hash_parent_base'};
1413 } elsif (defined $params{'hash_parent'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001414 $href .= esc_path_info($params{'hash_parent'}). "..";
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001415 delete $params{'hash_parent'};
1416 }
1417
Jakub Narebski67976c62010-12-14 16:54:31 +01001418 $href .= esc_path_info($params{'hash_base'});
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001419 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001420 $href .= ":/".esc_path_info($params{'file_name'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001421 delete $params{'file_name'};
1422 }
1423 delete $params{'hash'};
1424 delete $params{'hash_base'};
1425 } elsif (defined $params{'hash'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001426 $href .= esc_path_info($params{'hash'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001427 delete $params{'hash'};
1428 }
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +01001429
1430 # If the action was a snapshot, we can absorb the
1431 # snapshot_format parameter too
1432 if ($is_snapshot) {
1433 my $fmt = $params{'snapshot_format'};
1434 # snapshot_format should always be defined when href()
1435 # is called, but just in case some code forgets, we
1436 # fall back to the default
1437 $fmt ||= $snapshot_fmts[0];
1438 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1439 delete $params{'snapshot_format'};
1440 }
Martin Waitz9e756902006-10-01 23:57:48 +02001441 }
1442
1443 # now encode the parameters explicitly
Jakub Narebski498fe002006-08-22 19:05:25 +02001444 my @result = ();
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001445 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1446 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
Jakub Narebski498fe002006-08-22 19:05:25 +02001447 if (defined $params{$name}) {
Jakub Narebskif22cca42007-07-29 01:04:09 +02001448 if (ref($params{$name}) eq "ARRAY") {
1449 foreach my $par (@{$params{$name}}) {
1450 push @result, $symbol . "=" . esc_param($par);
1451 }
1452 } else {
1453 push @result, $symbol . "=" . esc_param($params{$name});
1454 }
Jakub Narebski498fe002006-08-22 19:05:25 +02001455 }
1456 }
Martin Waitz9e756902006-10-01 23:57:48 +02001457 $href .= "?" . join(';', @result) if scalar @result;
1458
Jakub Narebski67976c62010-12-14 16:54:31 +01001459 # final transformation: trailing spaces must be escaped (URI-encoded)
1460 $href =~ s/(\s+)$/CGI::escape($1)/e;
1461
Kevin Cernekee5e96a842011-03-18 17:00:16 +01001462 if ($params{-anchor}) {
1463 $href .= "#".esc_param($params{-anchor});
1464 }
1465
Martin Waitz9e756902006-10-01 23:57:48 +02001466 return $href;
Martin Waitz06a9d862006-08-16 00:23:50 +02001467}
1468
1469
1470## ======================================================================
Jakub Narebski717b8312006-07-31 21:22:15 +02001471## validation, quoting/unquoting and escaping
1472
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001473sub is_valid_action {
1474 my $input = shift;
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001475 return undef unless exists $actions{$input};
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001476 return 1;
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001477}
1478
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001479sub is_valid_project {
1480 my $input = shift;
1481
1482 return unless defined $input;
1483 if (!is_valid_pathname($input) ||
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001484 !(-d "$projectroot/$input") ||
Alexander Gavrilovec26f092008-11-06 01:15:56 +03001485 !check_export_ok("$projectroot/$input") ||
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001486 ($strict_export && !project_in_list($input))) {
1487 return undef;
1488 } else {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001489 return 1;
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001490 }
1491}
1492
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001493sub is_valid_pathname {
1494 my $input = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02001495
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001496 return undef unless defined $input;
Justin Lebar01689902014-03-31 15:11:46 -07001497 # no '.' or '..' as elements of path, i.e. no '.' or '..'
Jakub Narebski24d06932006-09-26 01:57:02 +02001498 # at the beginning, at the end, and between slashes.
1499 # also this catches doubled slashes
1500 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1501 return undef;
1502 }
1503 # no null characters
1504 if ($input =~ m!\0!) {
1505 return undef;
1506 }
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001507 return 1;
Jakub Narebski24d06932006-09-26 01:57:02 +02001508}
1509
Krzesimir Nowakc0bc2262013-12-11 12:54:41 +01001510sub is_valid_ref_format {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001511 my $input = shift;
Krzesimir Nowakc0bc2262013-12-11 12:54:41 +01001512
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001513 return undef unless defined $input;
Krzesimir Nowakc0bc2262013-12-11 12:54:41 +01001514 # restrictions on ref name according to git-check-ref-format
1515 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1516 return undef;
1517 }
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001518 return 1;
Krzesimir Nowakc0bc2262013-12-11 12:54:41 +01001519}
1520
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001521sub is_valid_refname {
1522 my $input = shift;
Jakub Narebski24d06932006-09-26 01:57:02 +02001523
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001524 return undef unless defined $input;
Jakub Narebski24d06932006-09-26 01:57:02 +02001525 # textual hashes are O.K.
Jakub Narebski717b8312006-07-31 21:22:15 +02001526 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001527 return 1;
Jakub Narebski717b8312006-07-31 21:22:15 +02001528 }
Jakub Narebski24d06932006-09-26 01:57:02 +02001529 # it must be correct pathname
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001530 is_valid_pathname($input) or return undef;
Krzesimir Nowakc0bc2262013-12-11 12:54:41 +01001531 # check git-check-ref-format restrictions
Krzesimir Nowak23faf542013-12-11 12:54:42 +01001532 is_valid_ref_format($input) or return undef;
1533 return 1;
Jakub Narebski717b8312006-07-31 21:22:15 +02001534}
1535
Martin Koegler00f429a2007-06-03 17:42:44 +02001536# decode sequences of octets in utf8 into Perl's internal form,
1537# which is utf-8 with utf8 flag set if needed. gitweb writes out
1538# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1539sub to_utf8 {
1540 my $str = shift;
Jakub Narebski1df48762010-02-07 21:52:25 +01001541 return undef unless defined $str;
Jakub Narebskib13e3ea2011-12-18 23:00:58 +01001542
1543 if (utf8::is_utf8($str) || utf8::decode($str)) {
İsmail Dönmeze5d3de52007-12-04 10:55:41 +02001544 return $str;
Martin Koegler00f429a2007-06-03 17:42:44 +02001545 } else {
1546 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1547 }
1548}
1549
Kay Sievers232ff552005-11-24 16:56:55 +01001550# quote unsafe chars, but keep the slash, even when it's not
1551# correct, but quoted slashes look too horrible in bookmarks
1552sub esc_param {
Kay Sievers353347b2005-11-14 05:47:18 +01001553 my $str = shift;
Jakub Narebski1df48762010-02-07 21:52:25 +01001554 return undef unless defined $str;
Giuseppe Bilotta452e2252009-10-13 21:51:36 +02001555 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
Kay Sieversa9e60b72005-11-14 15:15:12 +01001556 $str =~ s/ /\+/g;
Kay Sievers353347b2005-11-14 05:47:18 +01001557 return $str;
1558}
1559
Jakub Narebski67976c62010-12-14 16:54:31 +01001560# the quoting rules for path_info fragment are slightly different
1561sub esc_path_info {
1562 my $str = shift;
1563 return undef unless defined $str;
1564
1565 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1566 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1567
1568 return $str;
1569}
1570
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02001571# quote unsafe chars in whole URL, so some characters cannot be quoted
Jakub Narebskif93bff82006-09-26 01:58:41 +02001572sub esc_url {
1573 my $str = shift;
Jakub Narebski1df48762010-02-07 21:52:25 +01001574 return undef unless defined $str;
Pavan Kumar Sunkara109988f2010-07-15 12:59:01 +05301575 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
Jakub Narebskif93bff82006-09-26 01:58:41 +02001576 $str =~ s/ /\+/g;
1577 return $str;
1578}
1579
Jakub Narebski3017ed62010-12-15 00:34:01 +01001580# quote unsafe characters in HTML attributes
1581sub esc_attr {
1582
1583 # for XHTML conformance escaping '"' to '&quot;' is not enough
1584 return esc_html(@_);
1585}
1586
Kay Sievers232ff552005-11-24 16:56:55 +01001587# replace invalid utf8 character with SUBSTITUTION sequence
Jakub Narebski74fd8722009-05-07 19:11:29 +02001588sub esc_html {
Kay Sievers40c13812005-11-19 17:41:29 +01001589 my $str = shift;
Jakub Narebski6255ef02006-11-01 14:33:21 +01001590 my %opts = @_;
1591
Jakub Narebski1df48762010-02-07 21:52:25 +01001592 return undef unless defined $str;
1593
Martin Koegler00f429a2007-06-03 17:42:44 +02001594 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +08001595 $str = $cgi->escapeHTML($str);
Jakub Narebski6255ef02006-11-01 14:33:21 +01001596 if ($opts{'-nbsp'}) {
1597 $str =~ s/ /&nbsp;/g;
1598 }
Junio C Hamano25ffbb22006-11-08 15:11:10 -08001599 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
Kay Sievers40c13812005-11-19 17:41:29 +01001600 return $str;
1601}
1602
Jakub Narebski391862e2006-11-25 09:43:59 +01001603# quote control characters and escape filename to HTML
1604sub esc_path {
1605 my $str = shift;
1606 my %opts = @_;
1607
Jakub Narebski1df48762010-02-07 21:52:25 +01001608 return undef unless defined $str;
1609
Martin Koegler00f429a2007-06-03 17:42:44 +02001610 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +08001611 $str = $cgi->escapeHTML($str);
Jakub Narebski391862e2006-11-25 09:43:59 +01001612 if ($opts{'-nbsp'}) {
1613 $str =~ s/ /&nbsp;/g;
1614 }
1615 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1616 return $str;
1617}
1618
Jakub Narebski08667862011-09-16 14:41:57 +02001619# Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)
1620sub sanitize {
1621 my $str = shift;
1622
1623 return undef unless defined $str;
1624
1625 $str = to_utf8($str);
Orgad Shaneh0e901d22012-12-30 13:52:53 +02001626 $str =~ s|([[:cntrl:]])|(index("\t\n\r", $1) != -1 ? $1 : quot_cec($1))|eg;
Jakub Narebski08667862011-09-16 14:41:57 +02001627 return $str;
1628}
1629
Jakub Narebski391862e2006-11-25 09:43:59 +01001630# Make control characters "printable", using character escape codes (CEC)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001631sub quot_cec {
1632 my $cntrl = shift;
Jakub Narebskic84c4832008-02-17 18:48:13 +01001633 my %opts = @_;
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001634 my %es = ( # character escape codes, aka escape sequences
Jakub Narebskic84c4832008-02-17 18:48:13 +01001635 "\t" => '\t', # tab (HT)
1636 "\n" => '\n', # line feed (LF)
1637 "\r" => '\r', # carrige return (CR)
1638 "\f" => '\f', # form feed (FF)
1639 "\b" => '\b', # backspace (BS)
1640 "\a" => '\a', # alarm (bell) (BEL)
1641 "\e" => '\e', # escape (ESC)
1642 "\013" => '\v', # vertical tab (VT)
1643 "\000" => '\0', # nul character (NUL)
1644 );
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001645 my $chr = ( (exists $es{$cntrl})
1646 ? $es{$cntrl}
Petr Baudis25dfd172008-10-01 22:11:54 +02001647 : sprintf('\%2x', ord($cntrl)) );
Jakub Narebskic84c4832008-02-17 18:48:13 +01001648 if ($opts{-nohtml}) {
1649 return $chr;
1650 } else {
1651 return "<span class=\"cntrl\">$chr</span>";
1652 }
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001653}
1654
Jakub Narebski391862e2006-11-25 09:43:59 +01001655# Alternatively use unicode control pictures codepoints,
1656# Unicode "printable representation" (PR)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001657sub quot_upr {
1658 my $cntrl = shift;
Jakub Narebskic84c4832008-02-17 18:48:13 +01001659 my %opts = @_;
1660
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001661 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
Jakub Narebskic84c4832008-02-17 18:48:13 +01001662 if ($opts{-nohtml}) {
1663 return $chr;
1664 } else {
1665 return "<span class=\"cntrl\">$chr</span>";
1666 }
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001667}
1668
Kay Sievers232ff552005-11-24 16:56:55 +01001669# git may return quoted and escaped filenames
1670sub unquote {
1671 my $str = shift;
Jakub Narebski403d0902006-11-08 11:48:56 +01001672
1673 sub unq {
1674 my $seq = shift;
1675 my %es = ( # character escape codes, aka escape sequences
1676 't' => "\t", # tab (HT, TAB)
1677 'n' => "\n", # newline (NL)
1678 'r' => "\r", # return (CR)
1679 'f' => "\f", # form feed (FF)
1680 'b' => "\b", # backspace (BS)
1681 'a' => "\a", # alarm (bell) (BEL)
1682 'e' => "\e", # escape (ESC)
1683 'v' => "\013", # vertical tab (VT)
1684 );
1685
1686 if ($seq =~ m/^[0-7]{1,3}$/) {
1687 # octal char sequence
1688 return chr(oct($seq));
1689 } elsif (exists $es{$seq}) {
1690 # C escape sequence, aka character escape code
Jakub Narebskic84c4832008-02-17 18:48:13 +01001691 return $es{$seq};
Jakub Narebski403d0902006-11-08 11:48:56 +01001692 }
1693 # quoted ordinary character
1694 return $seq;
1695 }
1696
Kay Sievers232ff552005-11-24 16:56:55 +01001697 if ($str =~ m/^"(.*)"$/) {
Jakub Narebski403d0902006-11-08 11:48:56 +01001698 # needs unquoting
Kay Sievers232ff552005-11-24 16:56:55 +01001699 $str = $1;
Jakub Narebski403d0902006-11-08 11:48:56 +01001700 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
Kay Sievers232ff552005-11-24 16:56:55 +01001701 }
1702 return $str;
1703}
1704
Jakub Narebskif16db172006-08-06 02:08:31 +02001705# escape tabs (convert tabs to spaces)
1706sub untabify {
1707 my $line = shift;
1708
1709 while ((my $pos = index($line, "\t")) != -1) {
1710 if (my $count = (8 - ($pos % 8))) {
1711 my $spaces = ' ' x $count;
1712 $line =~ s/\t/$spaces/;
1713 }
1714 }
1715
1716 return $line;
1717}
1718
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +02001719sub project_in_list {
1720 my $project = shift;
1721 my @list = git_get_projects_list();
1722 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1723}
1724
Jakub Narebski717b8312006-07-31 21:22:15 +02001725## ----------------------------------------------------------------------
1726## HTML aware string manipulation
1727
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001728# Try to chop given string on a word boundary between position
1729# $len and $len+$add_len. If there is no word boundary there,
1730# chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1731# (marking chopped part) would be longer than given string.
Jakub Narebski717b8312006-07-31 21:22:15 +02001732sub chop_str {
1733 my $str = shift;
1734 my $len = shift;
1735 my $add_len = shift || 10;
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001736 my $where = shift || 'right'; # 'left' | 'center' | 'right'
Jakub Narebski717b8312006-07-31 21:22:15 +02001737
Anders Waldenborgdee27752008-05-21 13:44:43 +02001738 # Make sure perl knows it is utf8 encoded so we don't
1739 # cut in the middle of a utf8 multibyte char.
1740 $str = to_utf8($str);
1741
Jakub Narebski717b8312006-07-31 21:22:15 +02001742 # allow only $len chars, but don't cut a word if it would fit in $add_len
1743 # if it doesn't fit, cut it if it's still longer than the dots we would add
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001744 # remove chopped character entities entirely
1745
1746 # when chopping in the middle, distribute $len into left and right part
1747 # return early if chopping wouldn't make string shorter
1748 if ($where eq 'center') {
1749 return $str if ($len + 5 >= length($str)); # filler is length 5
1750 $len = int($len/2);
1751 } else {
1752 return $str if ($len + 4 >= length($str)); # filler is length 4
Jakub Narebski717b8312006-07-31 21:22:15 +02001753 }
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001754
1755 # regexps: ending and beginning with word part up to $add_len
1756 my $endre = qr/.{$len}\w{0,$add_len}/;
1757 my $begre = qr/\w{0,$add_len}.{$len}/;
1758
1759 if ($where eq 'left') {
1760 $str =~ m/^(.*?)($begre)$/;
1761 my ($lead, $body) = ($1, $2);
1762 if (length($lead) > 4) {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001763 $lead = " ...";
1764 }
1765 return "$lead$body";
1766
1767 } elsif ($where eq 'center') {
1768 $str =~ m/^($endre)(.*)$/;
1769 my ($left, $str) = ($1, $2);
1770 $str =~ m/^(.*?)($begre)$/;
1771 my ($mid, $right) = ($1, $2);
1772 if (length($mid) > 5) {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001773 $mid = " ... ";
1774 }
1775 return "$left$mid$right";
1776
1777 } else {
1778 $str =~ m/^($endre)(.*)$/;
1779 my $body = $1;
1780 my $tail = $2;
1781 if (length($tail) > 4) {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001782 $tail = "... ";
1783 }
1784 return "$body$tail";
1785 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001786}
1787
David Symondsce58ec92007-10-23 11:31:22 +10001788# takes the same arguments as chop_str, but also wraps a <span> around the
1789# result with a title attribute if it does get chopped. Additionally, the
1790# string is HTML-escaped.
1791sub chop_and_escape_str {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001792 my ($str) = @_;
David Symondsce58ec92007-10-23 11:31:22 +10001793
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001794 my $chopped = chop_str(@_);
Jürgen Kreileder168c1e02011-12-17 10:22:21 +01001795 $str = to_utf8($str);
David Symondsce58ec92007-10-23 11:31:22 +10001796 if ($chopped eq $str) {
1797 return esc_html($chopped);
1798 } else {
Jakub Narebski14afe772009-05-22 17:35:46 +02001799 $str =~ s/[[:cntrl:]]/?/g;
Jakub Narebski850b90a2008-02-16 23:07:46 +01001800 return $cgi->span({-title=>$str}, esc_html($chopped));
David Symondsce58ec92007-10-23 11:31:22 +10001801 }
1802}
1803
Jakub Narebski337da8d2012-02-27 02:55:19 +01001804# Highlight selected fragments of string, using given CSS class,
1805# and escape HTML. It is assumed that fragments do not overlap.
1806# Regions are passed as list of pairs (array references).
1807#
1808# Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns
1809# '<span class="mark">foo</span>bar'
1810sub esc_html_hl_regions {
1811 my ($str, $css_class, @sel) = @_;
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001812 my %opts = grep { ref($_) ne 'ARRAY' } @sel;
1813 @sel = grep { ref($_) eq 'ARRAY' } @sel;
1814 return esc_html($str, %opts) unless @sel;
Jakub Narebski337da8d2012-02-27 02:55:19 +01001815
1816 my $out = '';
1817 my $pos = 0;
1818
1819 for my $s (@sel) {
Michał Kiedrowiczce61fb92012-04-11 23:18:37 +02001820 my ($begin, $end) = @$s;
Jakub Narebski337da8d2012-02-27 02:55:19 +01001821
Michał Kiedrowiczcbbea3d2012-04-11 23:18:38 +02001822 # Don't create empty <span> elements.
1823 next if $end <= $begin;
1824
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001825 my $escaped = esc_html(substr($str, $begin, $end - $begin),
1826 %opts);
Michał Kiedrowiczce61fb92012-04-11 23:18:37 +02001827
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001828 $out .= esc_html(substr($str, $pos, $begin - $pos), %opts)
Michał Kiedrowiczce61fb92012-04-11 23:18:37 +02001829 if ($begin - $pos > 0);
1830 $out .= $cgi->span({-class => $css_class}, $escaped);
1831
1832 $pos = $end;
Jakub Narebski337da8d2012-02-27 02:55:19 +01001833 }
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001834 $out .= esc_html(substr($str, $pos), %opts)
Jakub Narebski337da8d2012-02-27 02:55:19 +01001835 if ($pos < length($str));
1836
1837 return $out;
1838}
1839
Jakub Narebskie607b792012-02-27 02:55:22 +01001840# return positions of beginning and end of each match
1841sub matchpos_list {
Jakub Narebski337da8d2012-02-27 02:55:19 +01001842 my ($str, $regexp) = @_;
Jakub Narebskie607b792012-02-27 02:55:22 +01001843 return unless (defined $str && defined $regexp);
Jakub Narebski337da8d2012-02-27 02:55:19 +01001844
1845 my @matches;
1846 while ($str =~ /$regexp/g) {
1847 push @matches, [$-[0], $+[0]];
1848 }
Jakub Narebskie607b792012-02-27 02:55:22 +01001849 return @matches;
1850}
1851
1852# highlight match (if any), and escape HTML
1853sub esc_html_match_hl {
1854 my ($str, $regexp) = @_;
1855 return esc_html($str) unless defined $regexp;
1856
1857 my @matches = matchpos_list($str, $regexp);
Jakub Narebski337da8d2012-02-27 02:55:19 +01001858 return esc_html($str) unless @matches;
1859
1860 return esc_html_hl_regions($str, 'match', @matches);
1861}
1862
Jakub Narebskie607b792012-02-27 02:55:22 +01001863
1864# highlight match (if any) of shortened string, and escape HTML
1865sub esc_html_match_hl_chopped {
1866 my ($str, $chopped, $regexp) = @_;
1867 return esc_html_match_hl($str, $regexp) unless defined $chopped;
1868
1869 my @matches = matchpos_list($str, $regexp);
1870 return esc_html($chopped) unless @matches;
1871
1872 # filter matches so that we mark chopped string
1873 my $tail = "... "; # see chop_str
1874 unless ($chopped =~ s/\Q$tail\E$//) {
1875 $tail = '';
1876 }
1877 my $chop_len = length($chopped);
1878 my $tail_len = length($tail);
1879 my @filtered;
1880
1881 for my $m (@matches) {
1882 if ($m->[0] > $chop_len) {
1883 push @filtered, [ $chop_len, $chop_len + $tail_len ] if ($tail_len > 0);
1884 last;
1885 } elsif ($m->[1] > $chop_len) {
1886 push @filtered, [ $m->[0], $chop_len + $tail_len ];
1887 last;
1888 }
1889 push @filtered, $m;
1890 }
1891
1892 return esc_html_hl_regions($chopped . $tail, 'match', @filtered);
1893}
1894
Jakub Narebski717b8312006-07-31 21:22:15 +02001895## ----------------------------------------------------------------------
1896## functions returning short strings
1897
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00001898# CSS class for given age value (in seconds)
1899sub age_class {
1900 my $age = shift;
1901
Jakub Narebski785cdea2007-05-13 12:39:22 +02001902 if (!defined $age) {
1903 return "noage";
1904 } elsif ($age < 60*60*2) {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00001905 return "age0";
1906 } elsif ($age < 60*60*24*2) {
1907 return "age1";
1908 } else {
1909 return "age2";
1910 }
1911}
1912
Jakub Narebski717b8312006-07-31 21:22:15 +02001913# convert age in seconds to "nn units ago" string
1914sub age_string {
1915 my $age = shift;
1916 my $age_str;
1917
1918 if ($age > 60*60*24*365*2) {
1919 $age_str = (int $age/60/60/24/365);
1920 $age_str .= " years ago";
1921 } elsif ($age > 60*60*24*(365/12)*2) {
1922 $age_str = int $age/60/60/24/(365/12);
1923 $age_str .= " months ago";
1924 } elsif ($age > 60*60*24*7*2) {
1925 $age_str = int $age/60/60/24/7;
1926 $age_str .= " weeks ago";
1927 } elsif ($age > 60*60*24*2) {
1928 $age_str = int $age/60/60/24;
1929 $age_str .= " days ago";
1930 } elsif ($age > 60*60*2) {
1931 $age_str = int $age/60/60;
1932 $age_str .= " hours ago";
1933 } elsif ($age > 60*2) {
1934 $age_str = int $age/60;
1935 $age_str .= " min ago";
1936 } elsif ($age > 2) {
1937 $age_str = int $age;
1938 $age_str .= " sec ago";
1939 } else {
1940 $age_str .= " right now";
1941 }
1942 return $age_str;
1943}
1944
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001945use constant {
1946 S_IFINVALID => 0030000,
1947 S_IFGITLINK => 0160000,
1948};
1949
1950# submodule/subproject, a commit object reference
Jakub Narebski74fd8722009-05-07 19:11:29 +02001951sub S_ISGITLINK {
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001952 my $mode = shift;
1953
1954 return (($mode & S_IFMT) == S_IFGITLINK)
1955}
1956
Jakub Narebski717b8312006-07-31 21:22:15 +02001957# convert file mode in octal to symbolic file mode string
1958sub mode_str {
1959 my $mode = oct shift;
1960
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001961 if (S_ISGITLINK($mode)) {
1962 return 'm---------';
1963 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001964 return 'drwxr-xr-x';
1965 } elsif (S_ISLNK($mode)) {
1966 return 'lrwxrwxrwx';
1967 } elsif (S_ISREG($mode)) {
1968 # git cares only about the executable bit
1969 if ($mode & S_IXUSR) {
1970 return '-rwxr-xr-x';
1971 } else {
1972 return '-rw-r--r--';
1973 };
1974 } else {
1975 return '----------';
1976 }
1977}
1978
1979# convert file mode in octal to file type string
1980sub file_type {
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02001981 my $mode = shift;
1982
1983 if ($mode !~ m/^[0-7]+$/) {
1984 return $mode;
1985 } else {
1986 $mode = oct $mode;
1987 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001988
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001989 if (S_ISGITLINK($mode)) {
1990 return "submodule";
1991 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001992 return "directory";
1993 } elsif (S_ISLNK($mode)) {
1994 return "symlink";
1995 } elsif (S_ISREG($mode)) {
1996 return "file";
1997 } else {
1998 return "unknown";
1999 }
2000}
2001
Jakub Narebski744d0ac2006-11-08 17:59:41 +01002002# convert file mode in octal to file type description string
2003sub file_type_long {
2004 my $mode = shift;
2005
2006 if ($mode !~ m/^[0-7]+$/) {
2007 return $mode;
2008 } else {
2009 $mode = oct $mode;
2010 }
2011
Jakub Narebski01ac1e32007-07-28 16:27:31 +02002012 if (S_ISGITLINK($mode)) {
2013 return "submodule";
2014 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski744d0ac2006-11-08 17:59:41 +01002015 return "directory";
2016 } elsif (S_ISLNK($mode)) {
2017 return "symlink";
2018 } elsif (S_ISREG($mode)) {
2019 if ($mode & S_IXUSR) {
2020 return "executable";
2021 } else {
2022 return "file";
2023 };
2024 } else {
2025 return "unknown";
2026 }
2027}
2028
2029
Jakub Narebski717b8312006-07-31 21:22:15 +02002030## ----------------------------------------------------------------------
2031## functions returning short HTML fragments, or transforming HTML fragments
Pavel Roskin3dff5372007-02-03 23:49:16 -05002032## which don't belong to other sections
Jakub Narebski717b8312006-07-31 21:22:15 +02002033
Junio C Hamano225932e2006-11-09 00:57:13 -08002034# format line of commit message.
Jakub Narebski717b8312006-07-31 21:22:15 +02002035sub format_log_line_html {
2036 my $line = shift;
2037
Junio C Hamano225932e2006-11-09 00:57:13 -08002038 $line = esc_html($line, -nbsp=>1);
Marcel M. Cary7d233de2009-02-17 19:00:43 -08002039 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
2040 $cgi->a({-href => href(action=>"object", hash=>$1),
2041 -class => "text"}, $1);
2042 }eg;
2043
Jakub Narebski717b8312006-07-31 21:22:15 +02002044 return $line;
2045}
2046
2047# format marker of refs pointing to given object
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002048
2049# the destination action is chosen based on object type and current context:
2050# - for annotated tags, we choose the tag view unless it's the current view
2051# already, in which case we go to shortlog view
2052# - for other refs, we keep the current view if we're in history, shortlog or
2053# log view, and select shortlog otherwise
Jakub Narebski847e01f2006-08-14 02:05:47 +02002054sub format_ref_marker {
Jakub Narebski717b8312006-07-31 21:22:15 +02002055 my ($refs, $id) = @_;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002056 my $markers = '';
Jakub Narebski717b8312006-07-31 21:22:15 +02002057
2058 if (defined $refs->{$id}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002059 foreach my $ref (@{$refs->{$id}}) {
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002060 # this code exploits the fact that non-lightweight tags are the
2061 # only indirect objects, and that they are the only objects for which
2062 # we want to use tag instead of shortlog as action
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002063 my ($type, $name) = qw();
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002064 my $indirect = ($ref =~ s/\^\{\}$//);
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002065 # e.g. tags/v2.6.11 or heads/next
2066 if ($ref =~ m!^(.*?)s?/(.*)$!) {
2067 $type = $1;
2068 $name = $2;
2069 } else {
2070 $type = "ref";
2071 $name = $ref;
2072 }
2073
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002074 my $class = $type;
2075 $class .= " indirect" if $indirect;
2076
2077 my $dest_action = "shortlog";
2078
2079 if ($indirect) {
2080 $dest_action = "tag" unless $action eq "tag";
2081 } elsif ($action =~ /^(history|(short)?log)$/) {
2082 $dest_action = $action;
2083 }
2084
2085 my $dest = "";
2086 $dest .= "refs/" unless $ref =~ m!^refs/!;
2087 $dest .= $ref;
2088
2089 my $link = $cgi->a({
2090 -href => href(
2091 action=>$dest_action,
2092 hash=>$dest
2093 )}, $name);
2094
Jakub Narebski3017ed62010-12-15 00:34:01 +01002095 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002096 $link . "</span>";
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002097 }
2098 }
2099
2100 if ($markers) {
2101 return ' <span class="refs">'. $markers . '</span>';
Jakub Narebski717b8312006-07-31 21:22:15 +02002102 } else {
2103 return "";
2104 }
2105}
2106
Jakub Narebski17d07442006-08-14 02:08:27 +02002107# format, perhaps shortened and with markers, title line
2108sub format_subject_html {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002109 my ($long, $short, $href, $extra) = @_;
Jakub Narebski17d07442006-08-14 02:08:27 +02002110 $extra = '' unless defined($extra);
2111
2112 if (length($short) < length($long)) {
Jakub Narebski14afe772009-05-22 17:35:46 +02002113 $long =~ s/[[:cntrl:]]/?/g;
Jakub Narebski7c278012006-08-22 12:02:48 +02002114 return $cgi->a({-href => $href, -class => "list subject",
Martin Koegler00f429a2007-06-03 17:42:44 +02002115 -title => to_utf8($long)},
Giuseppe Bilotta01b89f02009-08-23 10:28:09 +02002116 esc_html($short)) . $extra;
Jakub Narebski17d07442006-08-14 02:08:27 +02002117 } else {
Jakub Narebski7c278012006-08-22 12:02:48 +02002118 return $cgi->a({-href => $href, -class => "list subject"},
Giuseppe Bilotta01b89f02009-08-23 10:28:09 +02002119 esc_html($long)) . $extra;
Jakub Narebski17d07442006-08-14 02:08:27 +02002120 }
2121}
2122
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002123# Rather than recomputing the url for an email multiple times, we cache it
2124# after the first hit. This gives a visible benefit in views where the avatar
2125# for the same email is used repeatedly (e.g. shortlog).
2126# The cache is shared by all avatar engines (currently gravatar only), which
2127# are free to use it as preferred. Since only one avatar engine is used for any
2128# given page, there's no risk for cache conflicts.
2129our %avatar_cache = ();
2130
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002131# Compute the picon url for a given email, by using the picon search service over at
2132# http://www.cs.indiana.edu/picons/search.html
2133sub picon_url {
2134 my $email = lc shift;
2135 if (!$avatar_cache{$email}) {
2136 my ($user, $domain) = split('@', $email);
2137 $avatar_cache{$email} =
Andrej E Baranov57485582013-01-29 00:41:32 +01002138 "//www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002139 "$domain/$user/" .
2140 "users+domains+unknown/up/single";
2141 }
2142 return $avatar_cache{$email};
2143}
2144
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002145# Compute the gravatar url for a given email, if it's not in the cache already.
2146# Gravatar stores only the part of the URL before the size, since that's the
2147# one computationally more expensive. This also allows reuse of the cache for
2148# different sizes (for this particular engine).
2149sub gravatar_url {
2150 my $email = lc shift;
2151 my $size = shift;
2152 $avatar_cache{$email} ||=
Andrej E Baranov57485582013-01-29 00:41:32 +01002153 "//www.gravatar.com/avatar/" .
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002154 Digest::MD5::md5_hex($email) . "?s=";
2155 return $avatar_cache{$email} . $size;
2156}
2157
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002158# Insert an avatar for the given $email at the given $size if the feature
2159# is enabled.
2160sub git_get_avatar {
2161 my ($email, %opts) = @_;
2162 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
2163 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
2164 $opts{-size} ||= 'default';
2165 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
2166 my $url = "";
2167 if ($git_avatar eq 'gravatar') {
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002168 $url = gravatar_url($email, $size);
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002169 } elsif ($git_avatar eq 'picon') {
2170 $url = picon_url($email);
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002171 }
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002172 # Other providers can be added by extending the if chain, defining $url
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002173 # as needed. If no variant puts something in $url, we assume avatars
2174 # are completely disabled/unavailable.
2175 if ($url) {
2176 return $pre_white .
2177 "<img width=\"$size\" " .
2178 "class=\"avatar\" " .
Jakub Narebski3017ed62010-12-15 00:34:01 +01002179 "src=\"".esc_url($url)."\" " .
Giuseppe Bilotta7d25ef42009-06-30 00:00:54 +02002180 "alt=\"\" " .
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002181 "/>" . $post_white;
2182 } else {
2183 return "";
2184 }
2185}
2186
Stephen Boyde133d652009-10-15 21:14:59 -07002187sub format_search_author {
2188 my ($author, $searchtype, $displaytext) = @_;
2189 my $have_search = gitweb_check_feature('search');
2190
2191 if ($have_search) {
2192 my $performed = "";
2193 if ($searchtype eq 'author') {
2194 $performed = "authored";
2195 } elsif ($searchtype eq 'committer') {
2196 $performed = "committed";
2197 }
2198
2199 return $cgi->a({-href => href(action=>"search", hash=>$hash,
2200 searchtext=>$author,
2201 searchtype=>$searchtype), class=>"list",
2202 title=>"Search for commits $performed by $author"},
2203 $displaytext);
2204
2205 } else {
2206 return $displaytext;
2207 }
2208}
2209
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02002210# format the author name of the given commit with the given tag
2211# the author name is chopped and escaped according to the other
2212# optional parameters (see chop_str).
2213sub format_author_html {
2214 my $tag = shift;
2215 my $co = shift;
2216 my $author = chop_and_escape_str($co->{'author_name'}, @_);
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002217 return "<$tag class=\"author\">" .
Stephen Boyde133d652009-10-15 21:14:59 -07002218 format_search_author($co->{'author_name'}, "author",
2219 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2220 $author) .
2221 "</$tag>";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02002222}
2223
Jakub Narebski90921742007-06-08 13:27:42 +02002224# format git diff header line, i.e. "diff --(git|combined|cc) ..."
2225sub format_git_diff_header_line {
2226 my $line = shift;
2227 my $diffinfo = shift;
2228 my ($from, $to) = @_;
2229
2230 if ($diffinfo->{'nparents'}) {
2231 # combined diff
2232 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2233 if ($to->{'href'}) {
2234 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2235 esc_path($to->{'file'}));
2236 } else { # file was deleted (no href)
2237 $line .= esc_path($to->{'file'});
2238 }
2239 } else {
2240 # "ordinary" diff
2241 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2242 if ($from->{'href'}) {
2243 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2244 'a/' . esc_path($from->{'file'}));
2245 } else { # file was added (no href)
2246 $line .= 'a/' . esc_path($from->{'file'});
2247 }
2248 $line .= ' ';
2249 if ($to->{'href'}) {
2250 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2251 'b/' . esc_path($to->{'file'}));
2252 } else { # file was deleted
2253 $line .= 'b/' . esc_path($to->{'file'});
2254 }
2255 }
2256
2257 return "<div class=\"diff header\">$line</div>\n";
2258}
2259
2260# format extended diff header line, before patch itself
2261sub format_extended_diff_header_line {
2262 my $line = shift;
2263 my $diffinfo = shift;
2264 my ($from, $to) = @_;
2265
2266 # match <path>
2267 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2268 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2269 esc_path($from->{'file'}));
2270 }
2271 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2272 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2273 esc_path($to->{'file'}));
2274 }
2275 # match single <mode>
2276 if ($line =~ m/\s(\d{6})$/) {
2277 $line .= '<span class="info"> (' .
2278 file_type_long($1) .
2279 ')</span>';
2280 }
2281 # match <hash>
2282 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2283 # can match only for combined diff
2284 $line = 'index ';
2285 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2286 if ($from->{'href'}[$i]) {
2287 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2288 -class=>"hash"},
2289 substr($diffinfo->{'from_id'}[$i],0,7));
2290 } else {
2291 $line .= '0' x 7;
2292 }
2293 # separator
2294 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2295 }
2296 $line .= '..';
2297 if ($to->{'href'}) {
2298 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2299 substr($diffinfo->{'to_id'},0,7));
2300 } else {
2301 $line .= '0' x 7;
2302 }
2303
2304 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2305 # can match only for ordinary diff
2306 my ($from_link, $to_link);
2307 if ($from->{'href'}) {
2308 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2309 substr($diffinfo->{'from_id'},0,7));
2310 } else {
2311 $from_link = '0' x 7;
2312 }
2313 if ($to->{'href'}) {
2314 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2315 substr($diffinfo->{'to_id'},0,7));
2316 } else {
2317 $to_link = '0' x 7;
2318 }
2319 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2320 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2321 }
2322
2323 return $line . "<br/>\n";
2324}
2325
2326# format from-file/to-file diff header
2327sub format_diff_from_to_header {
Jakub Narebski91af4ce2007-06-08 13:32:44 +02002328 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
Jakub Narebski90921742007-06-08 13:27:42 +02002329 my $line;
2330 my $result = '';
2331
2332 $line = $from_line;
2333 #assert($line =~ m/^---/) if DEBUG;
Jakub Narebskideaa01a2007-06-08 13:29:49 +02002334 # no extra formatting for "^--- /dev/null"
2335 if (! $diffinfo->{'nparents'}) {
2336 # ordinary (single parent) diff
2337 if ($line =~ m!^--- "?a/!) {
2338 if ($from->{'href'}) {
2339 $line = '--- a/' .
2340 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2341 esc_path($from->{'file'}));
2342 } else {
2343 $line = '--- a/' .
2344 esc_path($from->{'file'});
2345 }
2346 }
2347 $result .= qq!<div class="diff from_file">$line</div>\n!;
2348
2349 } else {
2350 # combined diff (merge commit)
2351 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2352 if ($from->{'href'}[$i]) {
2353 $line = '--- ' .
Jakub Narebski91af4ce2007-06-08 13:32:44 +02002354 $cgi->a({-href=>href(action=>"blobdiff",
2355 hash_parent=>$diffinfo->{'from_id'}[$i],
2356 hash_parent_base=>$parents[$i],
2357 file_parent=>$from->{'file'}[$i],
2358 hash=>$diffinfo->{'to_id'},
2359 hash_base=>$hash,
2360 file_name=>$to->{'file'}),
2361 -class=>"path",
2362 -title=>"diff" . ($i+1)},
2363 $i+1) .
2364 '/' .
Jakub Narebskideaa01a2007-06-08 13:29:49 +02002365 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2366 esc_path($from->{'file'}[$i]));
2367 } else {
2368 $line = '--- /dev/null';
2369 }
2370 $result .= qq!<div class="diff from_file">$line</div>\n!;
Jakub Narebski90921742007-06-08 13:27:42 +02002371 }
2372 }
Jakub Narebski90921742007-06-08 13:27:42 +02002373
2374 $line = $to_line;
2375 #assert($line =~ m/^\+\+\+/) if DEBUG;
2376 # no extra formatting for "^+++ /dev/null"
2377 if ($line =~ m!^\+\+\+ "?b/!) {
2378 if ($to->{'href'}) {
2379 $line = '+++ b/' .
2380 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2381 esc_path($to->{'file'}));
2382 } else {
2383 $line = '+++ b/' .
2384 esc_path($to->{'file'});
2385 }
2386 }
2387 $result .= qq!<div class="diff to_file">$line</div>\n!;
2388
2389 return $result;
2390}
2391
Jakub Narebskicd030c32007-06-08 13:33:28 +02002392# create note for patch simplified by combined diff
2393sub format_diff_cc_simplified {
2394 my ($diffinfo, @parents) = @_;
2395 my $result = '';
2396
2397 $result .= "<div class=\"diff header\">" .
2398 "diff --cc ";
2399 if (!is_deleted($diffinfo)) {
2400 $result .= $cgi->a({-href => href(action=>"blob",
2401 hash_base=>$hash,
2402 hash=>$diffinfo->{'to_id'},
2403 file_name=>$diffinfo->{'to_file'}),
2404 -class => "path"},
2405 esc_path($diffinfo->{'to_file'}));
2406 } else {
2407 $result .= esc_path($diffinfo->{'to_file'});
2408 }
2409 $result .= "</div>\n" . # class="diff header"
2410 "<div class=\"diff nodifferences\">" .
2411 "Simple merge" .
2412 "</div>\n"; # class="diff nodifferences"
2413
2414 return $result;
2415}
2416
Jakub Narebski20a864c2011-10-31 00:36:20 +01002417sub diff_line_class {
2418 my ($line, $from, $to) = @_;
2419
2420 # ordinary diff
2421 my $num_sign = 1;
2422 # combined diff
2423 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2424 $num_sign = scalar @{$from->{'href'}};
2425 }
2426
2427 my @diff_line_classifier = (
2428 { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
2429 { regexp => qr/^\\/, class => "incomplete" },
2430 { regexp => qr/^ {$num_sign}/, class => "ctx" },
2431 # classifier for context must come before classifier add/rem,
2432 # or we would have to use more complicated regexp, for example
2433 # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
2434 { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
2435 { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
2436 );
2437 for my $clsfy (@diff_line_classifier) {
2438 return $clsfy->{'class'}
2439 if ($line =~ $clsfy->{'regexp'});
2440 }
2441
2442 # fallback
2443 return "";
2444}
2445
Jakub Narebskif1310cf2011-10-31 00:36:21 +01002446# assumes that $from and $to are defined and correctly filled,
2447# and that $line holds a line of chunk header for unified diff
2448sub format_unidiff_chunk_header {
2449 my ($line, $from, $to) = @_;
2450
2451 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2452 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2453
2454 $from_lines = 0 unless defined $from_lines;
2455 $to_lines = 0 unless defined $to_lines;
2456
2457 if ($from->{'href'}) {
2458 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2459 -class=>"list"}, $from_text);
2460 }
2461 if ($to->{'href'}) {
2462 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2463 -class=>"list"}, $to_text);
2464 }
2465 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2466 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2467 return $line;
2468}
2469
2470# assumes that $from and $to are defined and correctly filled,
2471# and that $line holds a line of chunk header for combined diff
2472sub format_cc_diff_chunk_header {
2473 my ($line, $from, $to) = @_;
2474
2475 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2476 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2477
2478 @from_text = split(' ', $ranges);
2479 for (my $i = 0; $i < @from_text; ++$i) {
2480 ($from_start[$i], $from_nlines[$i]) =
2481 (split(',', substr($from_text[$i], 1)), 0);
2482 }
2483
2484 $to_text = pop @from_text;
2485 $to_start = pop @from_start;
2486 $to_nlines = pop @from_nlines;
2487
2488 $line = "<span class=\"chunk_info\">$prefix ";
2489 for (my $i = 0; $i < @from_text; ++$i) {
2490 if ($from->{'href'}[$i]) {
2491 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2492 -class=>"list"}, $from_text[$i]);
2493 } else {
2494 $line .= $from_text[$i];
2495 }
2496 $line .= " ";
2497 }
2498 if ($to->{'href'}) {
2499 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2500 -class=>"list"}, $to_text);
2501 } else {
2502 $line .= $to_text;
2503 }
2504 $line .= " $prefix</span>" .
2505 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2506 return $line;
2507}
2508
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01002509# process patch (diff) line (not to be used for diff headers),
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002510# returning HTML-formatted (but not wrapped) line.
2511# If the line is passed as a reference, it is treated as HTML and not
2512# esc_html()'ed.
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002513sub format_diff_line {
2514 my ($line, $diff_class, $from, $to) = @_;
Jakub Narebski20a864c2011-10-31 00:36:20 +01002515
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002516 if (ref($line)) {
2517 $line = $$line;
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002518 } else {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002519 chomp $line;
2520 $line = untabify($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02002521
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002522 if ($from && $to && $line =~ m/^\@{2} /) {
2523 $line = format_unidiff_chunk_header($line, $from, $to);
2524 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2525 $line = format_cc_diff_chunk_header($line, $from, $to);
2526 } else {
2527 $line = esc_html($line, -nbsp=>1);
2528 }
Jakub Narebski59e3b142006-11-18 23:35:40 +01002529 }
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002530
2531 my $diff_classes = "diff";
2532 $diff_classes .= " $diff_class" if ($diff_class);
2533 $line = "<div class=\"$diff_classes\">$line</div>\n";
2534
2535 return $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02002536}
2537
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002538# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2539# linked. Pass the hash of the tree/commit to snapshot.
2540sub format_snapshot_links {
2541 my ($hash) = @_;
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002542 my $num_fmts = @snapshot_fmts;
2543 if ($num_fmts > 1) {
2544 # A parenthesized list of links bearing format names.
Jakub Narebskia7817852007-07-22 23:41:20 +02002545 # e.g. "snapshot (_tar.gz_ _zip_)"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002546 return "snapshot (" . join(' ', map
2547 $cgi->a({
2548 -href => href(
2549 action=>"snapshot",
2550 hash=>$hash,
2551 snapshot_format=>$_
2552 )
2553 }, $known_snapshot_formats{$_}{'display'})
2554 , @snapshot_fmts) . ")";
2555 } elsif ($num_fmts == 1) {
2556 # A single "snapshot" link whose tooltip bears the format name.
Jakub Narebskia7817852007-07-22 23:41:20 +02002557 # i.e. "_snapshot_"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002558 my ($fmt) = @snapshot_fmts;
Jakub Narebskia7817852007-07-22 23:41:20 +02002559 return
2560 $cgi->a({
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002561 -href => href(
2562 action=>"snapshot",
2563 hash=>$hash,
2564 snapshot_format=>$fmt
2565 ),
2566 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2567 }, "snapshot");
2568 } else { # $num_fmts == 0
2569 return undef;
2570 }
2571}
2572
Jakub Narebski35621982008-04-20 22:09:48 +02002573## ......................................................................
2574## functions returning values to be passed, perhaps after some
2575## transformation, to other functions; e.g. returning arguments to href()
2576
2577# returns hash to be passed to href to generate gitweb URL
2578# in -title key it returns description of link
2579sub get_feed_info {
2580 my $format = shift || 'Atom';
2581 my %res = (action => lc($format));
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01002582 my $matched_ref = 0;
Jakub Narebski35621982008-04-20 22:09:48 +02002583
2584 # feed links are possible only for project views
2585 return unless (defined $project);
2586 # some views should link to OPML, or to generic project feed,
2587 # or don't have specific feed yet (so they should use generic)
Jakub Narebski18ab83e2012-01-07 11:47:38 +01002588 return if (!$action || $action =~ /^(?:tags|heads|forks|tag|search)$/x);
Jakub Narebski35621982008-04-20 22:09:48 +02002589
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01002590 my $branch = undef;
2591 # branches refs uses 'refs/' + $get_branch_refs()[x] + '/' prefix
2592 # (fullname) to differentiate from tag links; this also makes
2593 # possible to detect branch links
2594 for my $ref (get_branch_refs()) {
2595 if ((defined $hash_base && $hash_base =~ m!^refs/\Q$ref\E/(.*)$!) ||
2596 (defined $hash && $hash =~ m!^refs/\Q$ref\E/(.*)$!)) {
2597 $branch = $1;
2598 $matched_ref = $ref;
2599 last;
2600 }
Jakub Narebski35621982008-04-20 22:09:48 +02002601 }
2602 # find log type for feed description (title)
2603 my $type = 'log';
2604 if (defined $file_name) {
2605 $type = "history of $file_name";
2606 $type .= "/" if ($action eq 'tree');
2607 $type .= " on '$branch'" if (defined $branch);
2608 } else {
2609 $type = "log of $branch" if (defined $branch);
2610 }
2611
2612 $res{-title} = $type;
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01002613 $res{'hash'} = (defined $branch ? "refs/$matched_ref/$branch" : undef);
Jakub Narebski35621982008-04-20 22:09:48 +02002614 $res{'file_name'} = $file_name;
2615
2616 return %res;
2617}
2618
Jakub Narebski717b8312006-07-31 21:22:15 +02002619## ----------------------------------------------------------------------
2620## git utility subroutines, invoking git commands
2621
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002622# returns path to the core git executable and the --git-dir parameter as list
2623sub git_cmd {
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02002624 $number_of_git_cmds++;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002625 return $GIT, '--git-dir='.$git_dir;
2626}
2627
Lea Wiemann516381d2008-06-17 23:46:35 +02002628# quote the given arguments for passing them to the shell
2629# quote_command("command", "arg 1", "arg with ' and ! characters")
2630# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2631# Try to avoid using this function wherever possible.
2632sub quote_command {
2633 return join(' ',
Jakub Narebski68cedb12009-05-10 02:40:37 +02002634 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002635}
2636
Jakub Narebski717b8312006-07-31 21:22:15 +02002637# get HEAD ref of given project as hash
Jakub Narebski847e01f2006-08-14 02:05:47 +02002638sub git_get_head_hash {
Mark Radab6292752009-11-07 16:13:29 +01002639 return git_get_full_hash(shift, 'HEAD');
2640}
2641
2642sub git_get_full_hash {
2643 return git_get_hash(@_);
2644}
2645
2646sub git_get_short_hash {
2647 return git_get_hash(@_, '--short=7');
2648}
2649
2650sub git_get_hash {
2651 my ($project, $hash, @options) = @_;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002652 my $o_git_dir = $git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02002653 my $retval = undef;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002654 $git_dir = "$projectroot/$project";
Mark Radab6292752009-11-07 16:13:29 +01002655 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2656 '--verify', '-q', @options, $hash) {
2657 $retval = <$fd>;
2658 chomp $retval if defined $retval;
Jakub Narebski717b8312006-07-31 21:22:15 +02002659 close $fd;
Jakub Narebski717b8312006-07-31 21:22:15 +02002660 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002661 if (defined $o_git_dir) {
2662 $git_dir = $o_git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02002663 }
2664 return $retval;
2665}
2666
2667# get type of given object
2668sub git_get_type {
2669 my $hash = shift;
2670
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002671 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02002672 my $type = <$fd>;
2673 close $fd or return;
2674 chomp $type;
2675 return $type;
2676}
2677
Jakub Narebskib2019272007-11-03 00:41:19 +01002678# repository configuration
2679our $config_file = '';
2680our %config;
2681
2682# store multiple values for single key as anonymous array reference
2683# single values stored directly in the hash, not as [ <value> ]
2684sub hash_set_multi {
2685 my ($hash, $key, $value) = @_;
2686
2687 if (!exists $hash->{$key}) {
2688 $hash->{$key} = $value;
2689 } elsif (!ref $hash->{$key}) {
2690 $hash->{$key} = [ $hash->{$key}, $value ];
2691 } else {
2692 push @{$hash->{$key}}, $value;
2693 }
2694}
2695
2696# return hash of git project configuration
2697# optionally limited to some section, e.g. 'gitweb'
2698sub git_parse_project_config {
2699 my $section_regexp = shift;
2700 my %config;
2701
2702 local $/ = "\0";
2703
2704 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2705 or return;
2706
2707 while (my $keyval = <$fh>) {
2708 chomp $keyval;
2709 my ($key, $value) = split(/\n/, $keyval, 2);
2710
2711 hash_set_multi(\%config, $key, $value)
2712 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2713 }
2714 close $fh;
2715
2716 return %config;
2717}
2718
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002719# convert config value to boolean: 'true' or 'false'
Jakub Narebskib2019272007-11-03 00:41:19 +01002720# no value, number > 0, 'true' and 'yes' values are true
2721# rest of values are treated as false (never as error)
2722sub config_to_bool {
2723 my $val = shift;
2724
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002725 return 1 if !defined $val; # section.key
2726
Jakub Narebskib2019272007-11-03 00:41:19 +01002727 # strip leading and trailing whitespace
2728 $val =~ s/^\s+//;
2729 $val =~ s/\s+$//;
2730
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002731 return (($val =~ /^\d+$/ && $val) || # section.key = 1
Jakub Narebskib2019272007-11-03 00:41:19 +01002732 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2733}
2734
2735# convert config value to simple decimal number
2736# an optional value suffix of 'k', 'm', or 'g' will cause the value
2737# to be multiplied by 1024, 1048576, or 1073741824
2738sub config_to_int {
2739 my $val = shift;
2740
2741 # strip leading and trailing whitespace
2742 $val =~ s/^\s+//;
2743 $val =~ s/\s+$//;
2744
2745 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2746 $unit = lc($unit);
2747 # unknown unit is treated as 1
2748 return $num * ($unit eq 'g' ? 1073741824 :
2749 $unit eq 'm' ? 1048576 :
2750 $unit eq 'k' ? 1024 : 1);
2751 }
2752 return $val;
2753}
2754
2755# convert config value to array reference, if needed
2756sub config_to_multi {
2757 my $val = shift;
2758
Jakub Narebskid76a5852007-12-20 10:48:09 +01002759 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
Jakub Narebskib2019272007-11-03 00:41:19 +01002760}
2761
Jakub Narebski717b8312006-07-31 21:22:15 +02002762sub git_get_project_config {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302763 my ($key, $type) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02002764
Jakub Narebski7a49c252010-03-27 20:26:59 +01002765 return unless defined $git_dir;
Jakub Narebski9be36142010-03-01 22:51:34 +01002766
Jakub Narebskib2019272007-11-03 00:41:19 +01002767 # key sanity check
Jakub Narebski717b8312006-07-31 21:22:15 +02002768 return unless ($key);
Jakub Narebski14569cd2011-07-28 23:38:03 +02002769 # only subsection, if exists, is case sensitive,
2770 # and not lowercased by 'git config -z -l'
2771 if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
Phil Pennockaf507942012-11-05 18:50:47 -05002772 $lo =~ s/_//g;
Jakub Narebski14569cd2011-07-28 23:38:03 +02002773 $key = join(".", lc($hi), $mi, lc($lo));
Phil Pennockaf507942012-11-05 18:50:47 -05002774 return if ($lo =~ /\W/ || $hi =~ /\W/);
Jakub Narebski14569cd2011-07-28 23:38:03 +02002775 } else {
2776 $key = lc($key);
Phil Pennockaf507942012-11-05 18:50:47 -05002777 $key =~ s/_//g;
2778 return if ($key =~ /\W/);
Jakub Narebski14569cd2011-07-28 23:38:03 +02002779 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002780 $key =~ s/^gitweb\.//;
Jakub Narebski717b8312006-07-31 21:22:15 +02002781
Jakub Narebskib2019272007-11-03 00:41:19 +01002782 # type sanity check
2783 if (defined $type) {
2784 $type =~ s/^--//;
2785 $type = undef
2786 unless ($type eq 'bool' || $type eq 'int');
2787 }
2788
2789 # get config
2790 if (!defined $config_file ||
2791 $config_file ne "$git_dir/config") {
2792 %config = git_parse_project_config('gitweb');
2793 $config_file = "$git_dir/config";
2794 }
2795
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002796 # check if config variable (key) exists
2797 return unless exists $config{"gitweb.$key"};
2798
Jakub Narebskib2019272007-11-03 00:41:19 +01002799 # ensure given type
2800 if (!defined $type) {
2801 return $config{"gitweb.$key"};
2802 } elsif ($type eq 'bool') {
2803 # backward compatibility: 'git config --bool' returns true/false
2804 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2805 } elsif ($type eq 'int') {
2806 return config_to_int($config{"gitweb.$key"});
2807 }
2808 return $config{"gitweb.$key"};
Jakub Narebski717b8312006-07-31 21:22:15 +02002809}
2810
Jakub Narebski717b8312006-07-31 21:22:15 +02002811# get hash of given path at given ref
2812sub git_get_hash_by_path {
2813 my $base = shift;
2814 my $path = shift || return undef;
Jakub Narebski1d782b02006-09-21 18:09:12 +02002815 my $type = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02002816
Jakub Narebski4b02f482006-09-26 01:54:24 +02002817 $path =~ s,/+$,,;
Jakub Narebski717b8312006-07-31 21:22:15 +02002818
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002819 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
Lea Wiemann074afaa2008-06-19 22:03:21 +02002820 or die_error(500, "Open git-ls-tree failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02002821 my $line = <$fd>;
2822 close $fd or return undef;
2823
Jakub Narebski198a2a82007-05-12 21:16:34 +02002824 if (!defined $line) {
2825 # there is no tree or hash given by $path at $base
2826 return undef;
2827 }
2828
Jakub Narebski717b8312006-07-31 21:22:15 +02002829 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01002830 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
Jakub Narebski1d782b02006-09-21 18:09:12 +02002831 if (defined $type && $type ne $2) {
2832 # type doesn't match
2833 return undef;
2834 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002835 return $3;
2836}
2837
Jakub Narebskied224de2007-05-07 01:10:04 +02002838# get path of entry with given hash at given tree-ish (ref)
2839# used to get 'from' filename for combined diff (merge commit) for renames
2840sub git_get_path_by_hash {
2841 my $base = shift || return;
2842 my $hash = shift || return;
2843
2844 local $/ = "\0";
2845
2846 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2847 or return undef;
2848 while (my $line = <$fd>) {
2849 chomp $line;
2850
2851 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2852 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2853 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2854 close $fd;
2855 return $1;
2856 }
2857 }
2858 close $fd;
2859 return undef;
2860}
2861
Jakub Narebski717b8312006-07-31 21:22:15 +02002862## ......................................................................
2863## git utility functions, directly accessing git repository
2864
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002865# get the value of config variable either from file named as the variable
2866# itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2867# configuration variable in the repository config file.
2868sub git_get_file_or_project_config {
2869 my ($path, $name) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02002870
Jakub Narebski0e121a22007-11-03 00:41:20 +01002871 $git_dir = "$projectroot/$path";
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002872 open my $fd, '<', "$git_dir/$name"
2873 or return git_get_project_config($name);
2874 my $conf = <$fd>;
Jakub Narebski717b8312006-07-31 21:22:15 +02002875 close $fd;
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002876 if (defined $conf) {
2877 chomp $conf;
Junio C Hamano2eb54ef2007-05-16 21:04:16 -07002878 }
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002879 return $conf;
Jakub Narebski717b8312006-07-31 21:22:15 +02002880}
2881
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002882sub git_get_project_description {
2883 my $path = shift;
2884 return git_get_file_or_project_config($path, 'description');
Jakub Narebski717b8312006-07-31 21:22:15 +02002885}
2886
Sebastien Ceveyd940c902011-04-29 19:52:01 +02002887sub git_get_project_category {
2888 my $path = shift;
2889 return git_get_file_or_project_config($path, 'category');
2890}
2891
2892
Jakub Narebski0368c492011-04-29 19:51:57 +02002893# supported formats:
2894# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2895# - if its contents is a number, use it as tag weight,
2896# - otherwise add a tag with weight 1
2897# * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2898# the same value multiple times increases tag weight
2899# * `gitweb.ctag' multi-valued repo config variable
Petr Baudisaed93de2008-10-02 17:13:02 +02002900sub git_get_project_ctags {
Jakub Narebski0368c492011-04-29 19:51:57 +02002901 my $project = shift;
Petr Baudisaed93de2008-10-02 17:13:02 +02002902 my $ctags = {};
2903
Jakub Narebski0368c492011-04-29 19:51:57 +02002904 $git_dir = "$projectroot/$project";
2905 if (opendir my $dh, "$git_dir/ctags") {
2906 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2907 foreach my $tagfile (@files) {
2908 open my $ct, '<', $tagfile
2909 or next;
2910 my $val = <$ct>;
2911 chomp $val if $val;
2912 close $ct;
2913
2914 (my $ctag = $tagfile) =~ s#.*/##;
Jonathan Nieder2c162b52011-06-09 02:08:57 -05002915 if ($val =~ /^\d+$/) {
Jakub Narebski0368c492011-04-29 19:51:57 +02002916 $ctags->{$ctag} = $val;
2917 } else {
2918 $ctags->{$ctag} = 1;
2919 }
2920 }
2921 closedir $dh;
2922
2923 } elsif (open my $fh, '<', "$git_dir/ctags") {
2924 while (my $line = <$fh>) {
2925 chomp $line;
2926 $ctags->{$line}++ if $line;
2927 }
2928 close $fh;
2929
2930 } else {
2931 my $taglist = config_to_multi(git_get_project_config('ctag'));
2932 foreach my $tag (@$taglist) {
2933 $ctags->{$tag}++;
2934 }
Petr Baudisaed93de2008-10-02 17:13:02 +02002935 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002936
2937 return $ctags;
2938}
2939
2940# return hash, where keys are content tags ('ctags'),
2941# and values are sum of weights of given tag in every project
2942sub git_gather_all_ctags {
2943 my $projects = shift;
2944 my $ctags = {};
2945
2946 foreach my $p (@$projects) {
2947 foreach my $ct (keys %{$p->{'ctags'}}) {
2948 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2949 }
2950 }
2951
2952 return $ctags;
Petr Baudisaed93de2008-10-02 17:13:02 +02002953}
2954
2955sub git_populate_project_tagcloud {
2956 my $ctags = shift;
2957
2958 # First, merge different-cased tags; tags vote on casing
2959 my %ctags_lc;
2960 foreach (keys %$ctags) {
2961 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2962 if (not $ctags_lc{lc $_}->{topcount}
2963 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2964 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2965 $ctags_lc{lc $_}->{topname} = $_;
2966 }
2967 }
2968
2969 my $cloud;
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01002970 my $matched = $input_params{'ctag'};
Petr Baudisaed93de2008-10-02 17:13:02 +02002971 if (eval { require HTML::TagCloud; 1; }) {
2972 $cloud = HTML::TagCloud->new;
Jakub Narebski0368c492011-04-29 19:51:57 +02002973 foreach my $ctag (sort keys %ctags_lc) {
Petr Baudisaed93de2008-10-02 17:13:02 +02002974 # Pad the title with spaces so that the cloud looks
2975 # less crammed.
Jakub Narebski0368c492011-04-29 19:51:57 +02002976 my $title = esc_html($ctags_lc{$ctag}->{topname});
Petr Baudisaed93de2008-10-02 17:13:02 +02002977 $title =~ s/ /&nbsp;/g;
2978 $title =~ s/^/&nbsp;/g;
2979 $title =~ s/$/&nbsp;/g;
Jakub Narebski4b9447f2011-04-29 19:51:58 +02002980 if (defined $matched && $matched eq $ctag) {
2981 $title = qq(<span class="match">$title</span>);
2982 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002983 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2984 $ctags_lc{$ctag}->{count});
Petr Baudisaed93de2008-10-02 17:13:02 +02002985 }
2986 } else {
Jakub Narebski0368c492011-04-29 19:51:57 +02002987 $cloud = {};
2988 foreach my $ctag (keys %ctags_lc) {
Jakub Narebski4b9447f2011-04-29 19:51:58 +02002989 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
2990 if (defined $matched && $matched eq $ctag) {
2991 $title = qq(<span class="match">$title</span>);
2992 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002993 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
2994 $cloud->{$ctag}{ctag} =
Jakub Narebski4b9447f2011-04-29 19:51:58 +02002995 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
Jakub Narebski0368c492011-04-29 19:51:57 +02002996 }
Petr Baudisaed93de2008-10-02 17:13:02 +02002997 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002998 return $cloud;
Petr Baudisaed93de2008-10-02 17:13:02 +02002999}
3000
3001sub git_show_project_tagcloud {
3002 my ($cloud, $count) = @_;
Petr Baudisaed93de2008-10-02 17:13:02 +02003003 if (ref $cloud eq 'HTML::TagCloud') {
3004 return $cloud->html_and_css($count);
3005 } else {
Jakub Narebski0368c492011-04-29 19:51:57 +02003006 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
3007 return
3008 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
3009 join (', ', map {
3010 $cloud->{$_}->{'ctag'}
3011 } splice(@tags, 0, $count)) .
3012 '</div>';
Petr Baudisaed93de2008-10-02 17:13:02 +02003013 }
3014}
3015
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02003016sub git_get_project_url_list {
3017 my $path = shift;
3018
Jakub Narebski0e121a22007-11-03 00:41:20 +01003019 $git_dir = "$projectroot/$path";
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003020 open my $fd, '<', "$git_dir/cloneurl"
Jakub Narebski0e121a22007-11-03 00:41:20 +01003021 or return wantarray ?
3022 @{ config_to_multi(git_get_project_config('url')) } :
3023 config_to_multi(git_get_project_config('url'));
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02003024 my @git_project_url_list = map { chomp; $_ } <$fd>;
3025 close $fd;
3026
3027 return wantarray ? @git_project_url_list : \@git_project_url_list;
3028}
3029
Jakub Narebski847e01f2006-08-14 02:05:47 +02003030sub git_get_projects_list {
Jakub Narebski12b14432011-04-29 19:51:56 +02003031 my $filter = shift || '';
Bernhard R. Link348a6582012-01-30 21:06:38 +01003032 my $paranoid = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02003033 my @list;
3034
3035 if (-d $projects_list) {
3036 # search in directory
Jakub Narebski12b14432011-04-29 19:51:56 +02003037 my $dir = $projects_list;
Aneesh Kumar K.V6768d6b2006-11-03 10:41:45 +05303038 # remove the trailing "/"
3039 $dir =~ s!/+$!!;
Matthieu Moyac593b72012-01-04 11:07:45 +01003040 my $pfxlen = length("$dir");
3041 my $pfxdepth = ($dir =~ tr!/!!);
Jakub Narebski12b14432011-04-29 19:51:56 +02003042 # when filtering, search only given subdirectory
Bernhard R. Link348a6582012-01-30 21:06:38 +01003043 if ($filter && !$paranoid) {
Jakub Narebski12b14432011-04-29 19:51:56 +02003044 $dir .= "/$filter";
3045 $dir =~ s!/+$!!;
3046 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003047
3048 File::Find::find({
3049 follow_fast => 1, # follow symbolic links
Junio C Hamanod20602e2007-07-27 01:23:03 -07003050 follow_skip => 2, # ignore duplicates
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003051 dangling_symlinks => 0, # ignore dangling symlinks, silently
3052 wanted => sub {
Jakub Narebskiee1d8ee2010-04-30 18:30:31 +02003053 # global variables
3054 our $project_maxdepth;
3055 our $projectroot;
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003056 # skip project-list toplevel, if we get it.
3057 return if (m!^[/.]$!);
3058 # only directories can be git repositories
3059 return unless (-d $_);
Luke Luca5e9492007-10-16 20:45:25 -07003060 # don't traverse too deep (Find is super slow on os x)
Jakub Narebski12b14432011-04-29 19:51:56 +02003061 # $project_maxdepth excludes depth of $projectroot
Luke Luca5e9492007-10-16 20:45:25 -07003062 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
3063 $File::Find::prune = 1;
3064 return;
3065 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003066
Jakub Narebski12b14432011-04-29 19:51:56 +02003067 my $path = substr($File::Find::name, $pfxlen + 1);
Bernhard R. Link348a6582012-01-30 21:06:38 +01003068 # paranoidly only filter here
3069 if ($paranoid && $filter && $path !~ m!^\Q$filter\E/!) {
3070 next;
3071 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003072 # we check related file in $projectroot
Devin Doucettefb3bb3d2008-12-27 02:39:31 -07003073 if (check_export_ok("$projectroot/$path")) {
3074 push @list, { path => $path };
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003075 $File::Find::prune = 1;
3076 }
3077 },
3078 }, "$dir");
3079
Jakub Narebski717b8312006-07-31 21:22:15 +02003080 } elsif (-f $projects_list) {
3081 # read from file(url-encoded):
3082 # 'git%2Fgit.git Linus+Torvalds'
3083 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3084 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003085 open my $fd, '<', $projects_list or return;
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02003086 PROJECT:
Jakub Narebski717b8312006-07-31 21:22:15 +02003087 while (my $line = <$fd>) {
3088 chomp $line;
3089 my ($path, $owner) = split ' ', $line;
3090 $path = unescape($path);
3091 $owner = unescape($owner);
3092 if (!defined $path) {
3093 next;
3094 }
Jakub Narebski12b14432011-04-29 19:51:56 +02003095 # if $filter is rpovided, check if $path begins with $filter
3096 if ($filter && $path !~ m!^\Q$filter\E/!) {
3097 next;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003098 }
Junio C Hamano2172ce42006-10-03 02:30:47 -07003099 if (check_export_ok("$projectroot/$path")) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003100 my $pr = {
Kacper Kornet75e0dff2012-04-24 19:50:05 +02003101 path => $path
Jakub Narebski717b8312006-07-31 21:22:15 +02003102 };
Kacper Kornet75e0dff2012-04-24 19:50:05 +02003103 if ($owner) {
3104 $pr->{'owner'} = to_utf8($owner);
3105 }
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02003106 push @list, $pr;
Jakub Narebski717b8312006-07-31 21:22:15 +02003107 }
3108 }
3109 close $fd;
3110 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003111 return @list;
3112}
3113
Jakub Narebski12b14432011-04-29 19:51:56 +02003114# written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
3115# as side effects it sets 'forks' field to list of forks for forked projects
3116sub filter_forks_from_projects_list {
3117 my $projects = shift;
3118
3119 my %trie; # prefix tree of directories (path components)
3120 # generate trie out of those directories that might contain forks
3121 foreach my $pr (@$projects) {
3122 my $path = $pr->{'path'};
3123 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
3124 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
3125 next unless ($path); # skip '.git' repository: tests, git-instaweb
Julien Muchembled53c632f2011-10-21 21:04:21 +02003126 next unless (-d "$projectroot/$path"); # containing directory exists
Jakub Narebski12b14432011-04-29 19:51:56 +02003127 $pr->{'forks'} = []; # there can be 0 or more forks of project
3128
3129 # add to trie
3130 my @dirs = split('/', $path);
3131 # walk the trie, until either runs out of components or out of trie
3132 my $ref = \%trie;
3133 while (scalar @dirs &&
3134 exists($ref->{$dirs[0]})) {
3135 $ref = $ref->{shift @dirs};
3136 }
3137 # create rest of trie structure from rest of components
3138 foreach my $dir (@dirs) {
3139 $ref = $ref->{$dir} = {};
3140 }
3141 # create end marker, store $pr as a data
3142 $ref->{''} = $pr if (!exists $ref->{''});
3143 }
3144
3145 # filter out forks, by finding shortest prefix match for paths
3146 my @filtered;
3147 PROJECT:
3148 foreach my $pr (@$projects) {
3149 # trie lookup
3150 my $ref = \%trie;
3151 DIR:
3152 foreach my $dir (split('/', $pr->{'path'})) {
3153 if (exists $ref->{''}) {
3154 # found [shortest] prefix, is a fork - skip it
3155 push @{$ref->{''}{'forks'}}, $pr;
3156 next PROJECT;
3157 }
3158 if (!exists $ref->{$dir}) {
3159 # not in trie, cannot have prefix, not a fork
3160 push @filtered, $pr;
3161 next PROJECT;
3162 }
3163 # If the dir is there, we just walk one step down the trie.
3164 $ref = $ref->{$dir};
3165 }
3166 # we ran out of trie
3167 # (shouldn't happen: it's either no match, or end marker)
3168 push @filtered, $pr;
3169 }
3170
3171 return @filtered;
3172}
3173
3174# note: fill_project_list_info must be run first,
3175# for 'descr_long' and 'ctags' to be filled
3176sub search_projects_list {
3177 my ($projlist, %opts) = @_;
3178 my $tagfilter = $opts{'tagfilter'};
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003179 my $search_re = $opts{'search_regexp'};
Jakub Narebski12b14432011-04-29 19:51:56 +02003180
3181 return @$projlist
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003182 unless ($tagfilter || $search_re);
Jakub Narebski12b14432011-04-29 19:51:56 +02003183
Jakub Narebski07b257f2012-02-23 16:54:48 +01003184 # searching projects require filling to be run before it;
3185 fill_project_list_info($projlist,
3186 $tagfilter ? 'ctags' : (),
Junio C Hamanoaa145bf2012-03-08 13:04:49 -08003187 $search_re ? ('path', 'descr') : ());
Jakub Narebski12b14432011-04-29 19:51:56 +02003188 my @projects;
3189 PROJECT:
3190 foreach my $pr (@$projlist) {
3191
3192 if ($tagfilter) {
3193 next unless ref($pr->{'ctags'}) eq 'HASH';
3194 next unless
3195 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
3196 }
3197
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003198 if ($search_re) {
Jakub Narebski12b14432011-04-29 19:51:56 +02003199 next unless
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003200 $pr->{'path'} =~ /$search_re/ ||
3201 $pr->{'descr_long'} =~ /$search_re/;
Jakub Narebski12b14432011-04-29 19:51:56 +02003202 }
3203
3204 push @projects, $pr;
3205 }
3206
3207 return @projects;
3208}
3209
Junio C Hamano47852452007-07-03 22:10:42 -07003210our $gitweb_project_owner = undef;
3211sub git_get_project_list_from_file {
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003212
Junio C Hamano47852452007-07-03 22:10:42 -07003213 return if (defined $gitweb_project_owner);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003214
Junio C Hamano47852452007-07-03 22:10:42 -07003215 $gitweb_project_owner = {};
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003216 # read from file (url-encoded):
3217 # 'git%2Fgit.git Linus+Torvalds'
3218 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3219 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3220 if (-f $projects_list) {
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003221 open(my $fd, '<', $projects_list);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003222 while (my $line = <$fd>) {
3223 chomp $line;
3224 my ($pr, $ow) = split ' ', $line;
3225 $pr = unescape($pr);
3226 $ow = unescape($ow);
Junio C Hamano47852452007-07-03 22:10:42 -07003227 $gitweb_project_owner->{$pr} = to_utf8($ow);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003228 }
3229 close $fd;
3230 }
Junio C Hamano47852452007-07-03 22:10:42 -07003231}
3232
3233sub git_get_project_owner {
3234 my $project = shift;
3235 my $owner;
3236
3237 return undef unless $project;
Bruno Ribasb59012e2008-02-08 14:38:04 -02003238 $git_dir = "$projectroot/$project";
Junio C Hamano47852452007-07-03 22:10:42 -07003239
3240 if (!defined $gitweb_project_owner) {
3241 git_get_project_list_from_file();
3242 }
3243
3244 if (exists $gitweb_project_owner->{$project}) {
3245 $owner = $gitweb_project_owner->{$project};
3246 }
Bruno Ribasb59012e2008-02-08 14:38:04 -02003247 if (!defined $owner){
3248 $owner = git_get_project_config('owner');
3249 }
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003250 if (!defined $owner) {
Bruno Ribasb59012e2008-02-08 14:38:04 -02003251 $owner = get_file_owner("$git_dir");
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003252 }
3253
3254 return $owner;
3255}
3256
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003257sub git_get_last_activity {
3258 my ($path) = @_;
3259 my $fd;
3260
3261 $git_dir = "$projectroot/$path";
3262 open($fd, "-|", git_cmd(), 'for-each-ref',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00003263 '--format=%(committer)',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003264 '--sort=-committerdate',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00003265 '--count=1',
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01003266 map { "refs/$_" } get_branch_refs ()) or return;
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003267 my $most_recent = <$fd>;
3268 close $fd or return;
Jakub Narebski785cdea2007-05-13 12:39:22 +02003269 if (defined $most_recent &&
3270 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003271 my $timestamp = $1;
3272 my $age = time - $timestamp;
3273 return ($age, age_string($age));
3274 }
Matt McCutchenc9563952007-06-28 18:15:22 -04003275 return (undef, undef);
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003276}
3277
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01003278# Implementation note: when a single remote is wanted, we cannot use 'git
3279# remote show -n' because that command always work (assuming it's a remote URL
3280# if it's not defined), and we cannot use 'git remote show' because that would
3281# try to make a network roundtrip. So the only way to find if that particular
3282# remote is defined is to walk the list provided by 'git remote -v' and stop if
3283# and when we find what we want.
3284sub git_get_remotes_list {
3285 my $wanted = shift;
3286 my %remotes = ();
3287
3288 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3289 return unless $fd;
3290 while (my $remote = <$fd>) {
3291 chomp $remote;
3292 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3293 next if $wanted and not $remote eq $wanted;
3294 my ($url, $key) = ($1, $2);
3295
3296 $remotes{$remote} ||= { 'heads' => () };
3297 $remotes{$remote}{$key} = $url;
3298 }
3299 close $fd or return;
3300 return wantarray ? %remotes : \%remotes;
3301}
3302
3303# Takes a hash of remotes as first parameter and fills it by adding the
3304# available remote heads for each of the indicated remotes.
3305sub fill_remote_heads {
3306 my $remotes = shift;
3307 my @heads = map { "remotes/$_" } keys %$remotes;
3308 my @remoteheads = git_get_heads_list(undef, @heads);
3309 foreach my $remote (keys %$remotes) {
3310 $remotes->{$remote}{'heads'} = [ grep {
3311 $_->{'name'} =~ s!^$remote/!!
3312 } @remoteheads ];
3313 }
3314}
3315
Jakub Narebski847e01f2006-08-14 02:05:47 +02003316sub git_get_references {
Jakub Narebski717b8312006-07-31 21:22:15 +02003317 my $type = shift || "";
3318 my %refs;
Jakub Narebski28b9d9f2006-11-25 11:32:08 +01003319 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3320 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3321 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3322 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
Jakub Narebski9704d752006-09-19 14:31:49 +02003323 or return;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003324
Jakub Narebski717b8312006-07-31 21:22:15 +02003325 while (my $line = <$fd>) {
3326 chomp $line;
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02003327 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003328 if (defined $refs{$1}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003329 push @{$refs{$1}}, $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02003330 } else {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003331 $refs{$1} = [ $2 ];
Jakub Narebski717b8312006-07-31 21:22:15 +02003332 }
3333 }
3334 }
3335 close $fd or return;
3336 return \%refs;
3337}
3338
Jakub Narebski56a322f2006-08-24 19:41:23 +02003339sub git_get_rev_name_tags {
3340 my $hash = shift || return undef;
3341
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003342 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
Jakub Narebski56a322f2006-08-24 19:41:23 +02003343 or return;
3344 my $name_rev = <$fd>;
3345 close $fd;
3346
3347 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3348 return $1;
3349 } else {
3350 # catches also '$hash undefined' output
3351 return undef;
3352 }
3353}
3354
Jakub Narebski717b8312006-07-31 21:22:15 +02003355## ----------------------------------------------------------------------
3356## parse to hash functions
3357
Jakub Narebski847e01f2006-08-14 02:05:47 +02003358sub parse_date {
Jakub Narebski717b8312006-07-31 21:22:15 +02003359 my $epoch = shift;
3360 my $tz = shift || "-0000";
3361
3362 my %date;
3363 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3364 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3365 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3366 $date{'hour'} = $hour;
3367 $date{'minute'} = $min;
3368 $date{'mday'} = $mday;
3369 $date{'day'} = $days[$wday];
3370 $date{'month'} = $months[$mon];
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003371 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3372 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
Jakub Narebski952c65f2006-08-22 16:52:50 +02003373 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3374 $mday, $months[$mon], $hour ,$min;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003375 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
Vincent Zanottia62d6d82007-11-10 19:55:27 +01003376 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
Jakub Narebski717b8312006-07-31 21:22:15 +02003377
Jakub Narebski2b1e1722011-03-25 20:20:49 +01003378 my ($tz_sign, $tz_hour, $tz_min) =
3379 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3380 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3381 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
Jakub Narebski717b8312006-07-31 21:22:15 +02003382 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3383 $date{'hour_local'} = $hour;
3384 $date{'minute_local'} = $min;
3385 $date{'tz_local'} = $tz;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003386 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3387 1900+$year, $mon+1, $mday,
3388 $hour, $min, $sec, $tz);
Jakub Narebski717b8312006-07-31 21:22:15 +02003389 return %date;
3390}
3391
Jakub Narebski847e01f2006-08-14 02:05:47 +02003392sub parse_tag {
Jakub Narebski717b8312006-07-31 21:22:15 +02003393 my $tag_id = shift;
3394 my %tag;
3395 my @comment;
3396
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003397 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02003398 $tag{'id'} = $tag_id;
3399 while (my $line = <$fd>) {
3400 chomp $line;
3401 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3402 $tag{'object'} = $1;
3403 } elsif ($line =~ m/^type (.+)$/) {
3404 $tag{'type'} = $1;
3405 } elsif ($line =~ m/^tag (.+)$/) {
3406 $tag{'name'} = $1;
3407 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3408 $tag{'author'} = $1;
Giuseppe Bilottaba924732009-06-30 00:00:50 +02003409 $tag{'author_epoch'} = $2;
3410 $tag{'author_tz'} = $3;
3411 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3412 $tag{'author_name'} = $1;
3413 $tag{'author_email'} = $2;
3414 } else {
3415 $tag{'author_name'} = $tag{'author'};
3416 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003417 } elsif ($line =~ m/--BEGIN/) {
3418 push @comment, $line;
3419 last;
3420 } elsif ($line eq "") {
3421 last;
3422 }
3423 }
3424 push @comment, <$fd>;
3425 $tag{'comment'} = \@comment;
3426 close $fd or return;
3427 if (!defined $tag{'name'}) {
3428 return
3429 };
3430 return %tag
3431}
3432
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003433sub parse_commit_text {
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003434 my ($commit_text, $withparents) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003435 my @commit_lines = split '\n', $commit_text;
Jakub Narebski717b8312006-07-31 21:22:15 +02003436 my %co;
3437
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003438 pop @commit_lines; # Remove '\0'
3439
Jakub Narebski198a2a82007-05-12 21:16:34 +02003440 if (! @commit_lines) {
3441 return;
3442 }
3443
Jakub Narebski717b8312006-07-31 21:22:15 +02003444 my $header = shift @commit_lines;
Jakub Narebski198a2a82007-05-12 21:16:34 +02003445 if ($header !~ m/^[0-9a-fA-F]{40}/) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003446 return;
3447 }
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003448 ($co{'id'}, my @parents) = split ' ', $header;
Jakub Narebski717b8312006-07-31 21:22:15 +02003449 while (my $line = shift @commit_lines) {
3450 last if $line eq "\n";
3451 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3452 $co{'tree'} = $1;
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003453 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00003454 push @parents, $1;
Jakub Narebski717b8312006-07-31 21:22:15 +02003455 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
Zoltán Füzesi5ed5bbc2009-08-02 09:42:24 +02003456 $co{'author'} = to_utf8($1);
Jakub Narebski717b8312006-07-31 21:22:15 +02003457 $co{'author_epoch'} = $2;
3458 $co{'author_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01003459 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3460 $co{'author_name'} = $1;
3461 $co{'author_email'} = $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02003462 } else {
3463 $co{'author_name'} = $co{'author'};
3464 }
3465 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
Zoltán Füzesi5ed5bbc2009-08-02 09:42:24 +02003466 $co{'committer'} = to_utf8($1);
Jakub Narebski717b8312006-07-31 21:22:15 +02003467 $co{'committer_epoch'} = $2;
3468 $co{'committer_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01003469 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3470 $co{'committer_name'} = $1;
3471 $co{'committer_email'} = $2;
3472 } else {
3473 $co{'committer_name'} = $co{'committer'};
3474 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003475 }
3476 }
3477 if (!defined $co{'tree'}) {
3478 return;
3479 };
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00003480 $co{'parents'} = \@parents;
3481 $co{'parent'} = $parents[0];
Jakub Narebski717b8312006-07-31 21:22:15 +02003482
3483 foreach my $title (@commit_lines) {
3484 $title =~ s/^ //;
3485 if ($title ne "") {
3486 $co{'title'} = chop_str($title, 80, 5);
3487 # remove leading stuff of merges to make the interesting part visible
3488 if (length($title) > 50) {
3489 $title =~ s/^Automatic //;
3490 $title =~ s/^merge (of|with) /Merge ... /i;
3491 if (length($title) > 50) {
3492 $title =~ s/(http|rsync):\/\///;
3493 }
3494 if (length($title) > 50) {
3495 $title =~ s/(master|www|rsync)\.//;
3496 }
3497 if (length($title) > 50) {
3498 $title =~ s/kernel.org:?//;
3499 }
3500 if (length($title) > 50) {
3501 $title =~ s/\/pub\/scm//;
3502 }
3503 }
3504 $co{'title_short'} = chop_str($title, 50, 5);
3505 last;
3506 }
3507 }
Joey Hess53c39672008-09-05 14:26:29 -04003508 if (! defined $co{'title'} || $co{'title'} eq "") {
Petr Baudis7e0fe5c2006-10-06 18:55:04 +02003509 $co{'title'} = $co{'title_short'} = '(no commit message)';
3510 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003511 # remove added spaces
3512 foreach my $line (@commit_lines) {
3513 $line =~ s/^ //;
3514 }
3515 $co{'comment'} = \@commit_lines;
3516
3517 my $age = time - $co{'committer_epoch'};
3518 $co{'age'} = $age;
3519 $co{'age_string'} = age_string($age);
3520 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3521 if ($age > 60*60*24*7*2) {
3522 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3523 $co{'age_string_age'} = $co{'age_string'};
3524 } else {
3525 $co{'age_string_date'} = $co{'age_string'};
3526 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3527 }
3528 return %co;
3529}
3530
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003531sub parse_commit {
3532 my ($commit_id) = @_;
3533 my %co;
3534
3535 local $/ = "\0";
3536
3537 open my $fd, "-|", git_cmd(), "rev-list",
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003538 "--parents",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003539 "--header",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003540 "--max-count=1",
3541 $commit_id,
3542 "--",
Lea Wiemann074afaa2008-06-19 22:03:21 +02003543 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003544 %co = parse_commit_text(<$fd>, 1);
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003545 close $fd;
3546
3547 return %co;
3548}
3549
3550sub parse_commits {
Jakub Narebski311e5522008-02-26 13:22:06 +01003551 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003552 my @cos;
3553
3554 $maxcount ||= 1;
3555 $skip ||= 0;
3556
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003557 local $/ = "\0";
3558
3559 open my $fd, "-|", git_cmd(), "rev-list",
3560 "--header",
Jakub Narebski311e5522008-02-26 13:22:06 +01003561 @args,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003562 ("--max-count=" . $maxcount),
Robert Fitzsimonsf47efbb2006-12-24 14:31:49 +00003563 ("--skip=" . $skip),
Miklos Vajna868bc062007-07-12 20:39:27 +02003564 @extra_options,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003565 $commit_id,
3566 "--",
3567 ($filename ? ($filename) : ())
Lea Wiemann074afaa2008-06-19 22:03:21 +02003568 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003569 while (my $line = <$fd>) {
3570 my %co = parse_commit_text($line);
3571 push @cos, \%co;
3572 }
3573 close $fd;
3574
3575 return wantarray ? @cos : \@cos;
3576}
3577
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003578# parse line of git-diff-tree "raw" output
Jakub Narebski740e67f2006-08-21 23:07:00 +02003579sub parse_difftree_raw_line {
3580 my $line = shift;
3581 my %res;
3582
3583 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3584 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3585 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3586 $res{'from_mode'} = $1;
3587 $res{'to_mode'} = $2;
3588 $res{'from_id'} = $3;
3589 $res{'to_id'} = $4;
Jakub Narebski4ed4a342008-04-05 21:13:24 +01003590 $res{'status'} = $5;
Jakub Narebski740e67f2006-08-21 23:07:00 +02003591 $res{'similarity'} = $6;
3592 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003593 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02003594 } else {
Jakub Narebski9d301452007-11-01 12:38:08 +01003595 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02003596 }
3597 }
Jakub Narebski78bc4032007-05-07 01:10:03 +02003598 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3599 # combined diff (for merge commit)
3600 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3601 $res{'nparents'} = length($1);
3602 $res{'from_mode'} = [ split(' ', $2) ];
3603 $res{'to_mode'} = pop @{$res{'from_mode'}};
3604 $res{'from_id'} = [ split(' ', $3) ];
3605 $res{'to_id'} = pop @{$res{'from_id'}};
3606 $res{'status'} = [ split('', $4) ];
3607 $res{'to_file'} = unquote($5);
3608 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02003609 # 'c512b523472485aef4fff9e57b229d9d243c967f'
Jakub Narebski0edcb372006-08-31 00:36:04 +02003610 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3611 $res{'commit'} = $1;
3612 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02003613
3614 return wantarray ? %res : \%res;
3615}
3616
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003617# wrapper: return parsed line of git-diff-tree "raw" output
3618# (the argument might be raw line, or parsed info)
3619sub parsed_difftree_line {
3620 my $line_or_ref = shift;
3621
3622 if (ref($line_or_ref) eq "HASH") {
3623 # pre-parsed (or generated by hand)
3624 return $line_or_ref;
3625 } else {
3626 return parse_difftree_raw_line($line_or_ref);
3627 }
3628}
3629
Jakub Narebskicb849b42006-08-31 00:32:15 +02003630# parse line of git-ls-tree output
Jakub Narebski74fd8722009-05-07 19:11:29 +02003631sub parse_ls_tree_line {
Jakub Narebskicb849b42006-08-31 00:32:15 +02003632 my $line = shift;
3633 my %opts = @_;
3634 my %res;
3635
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003636 if ($opts{'-l'}) {
3637 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3638 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
Jakub Narebskicb849b42006-08-31 00:32:15 +02003639
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003640 $res{'mode'} = $1;
3641 $res{'type'} = $2;
3642 $res{'hash'} = $3;
3643 $res{'size'} = $4;
3644 if ($opts{'-z'}) {
3645 $res{'name'} = $5;
3646 } else {
3647 $res{'name'} = unquote($5);
3648 }
Jakub Narebskicb849b42006-08-31 00:32:15 +02003649 } else {
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003650 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3651 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3652
3653 $res{'mode'} = $1;
3654 $res{'type'} = $2;
3655 $res{'hash'} = $3;
3656 if ($opts{'-z'}) {
3657 $res{'name'} = $4;
3658 } else {
3659 $res{'name'} = unquote($4);
3660 }
Jakub Narebskicb849b42006-08-31 00:32:15 +02003661 }
3662
3663 return wantarray ? %res : \%res;
3664}
3665
Jakub Narebski90921742007-06-08 13:27:42 +02003666# generates _two_ hashes, references to which are passed as 2 and 3 argument
3667sub parse_from_to_diffinfo {
3668 my ($diffinfo, $from, $to, @parents) = @_;
3669
3670 if ($diffinfo->{'nparents'}) {
3671 # combined diff
3672 $from->{'file'} = [];
3673 $from->{'href'} = [];
3674 fill_from_file_info($diffinfo, @parents)
3675 unless exists $diffinfo->{'from_file'};
3676 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
Jakub Narebski9d301452007-11-01 12:38:08 +01003677 $from->{'file'}[$i] =
3678 defined $diffinfo->{'from_file'}[$i] ?
3679 $diffinfo->{'from_file'}[$i] :
3680 $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003681 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3682 $from->{'href'}[$i] = href(action=>"blob",
3683 hash_base=>$parents[$i],
3684 hash=>$diffinfo->{'from_id'}[$i],
3685 file_name=>$from->{'file'}[$i]);
3686 } else {
3687 $from->{'href'}[$i] = undef;
3688 }
3689 }
3690 } else {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003691 # ordinary (not combined) diff
Jakub Narebski9d301452007-11-01 12:38:08 +01003692 $from->{'file'} = $diffinfo->{'from_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003693 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3694 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3695 hash=>$diffinfo->{'from_id'},
3696 file_name=>$from->{'file'});
3697 } else {
3698 delete $from->{'href'};
3699 }
3700 }
3701
Jakub Narebski9d301452007-11-01 12:38:08 +01003702 $to->{'file'} = $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003703 if (!is_deleted($diffinfo)) { # file exists in result
3704 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3705 hash=>$diffinfo->{'to_id'},
3706 file_name=>$to->{'file'});
3707 } else {
3708 delete $to->{'href'};
3709 }
3710}
3711
Jakub Narebski717b8312006-07-31 21:22:15 +02003712## ......................................................................
3713## parse to array of hashes functions
3714
Jakub Narebskicd146402006-11-02 20:23:11 +01003715sub git_get_heads_list {
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003716 my ($limit, @classes) = @_;
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01003717 @classes = get_branch_refs() unless @classes;
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003718 my @patterns = map { "refs/$_" } @classes;
Jakub Narebskicd146402006-11-02 20:23:11 +01003719 my @headslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003720
Jakub Narebskicd146402006-11-02 20:23:11 +01003721 open my $fd, '-|', git_cmd(), 'for-each-ref',
3722 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3723 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003724 @patterns
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003725 or return;
3726 while (my $line = <$fd>) {
Jakub Narebskicd146402006-11-02 20:23:11 +01003727 my %ref_item;
Jakub Narebski120ddde2006-09-19 14:33:22 +02003728
Jakub Narebskicd146402006-11-02 20:23:11 +01003729 chomp $line;
3730 my ($refinfo, $committerinfo) = split(/\0/, $line);
3731 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3732 my ($committer, $epoch, $tz) =
3733 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01003734 $ref_item{'fullname'} = $name;
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01003735 my $strip_refs = join '|', map { quotemeta } get_branch_refs();
3736 $name =~ s!^refs/($strip_refs|remotes)/!!;
Krzesimir Nowake3747472013-12-11 12:54:44 +01003737 $ref_item{'name'} = $name;
3738 # for refs neither in 'heads' nor 'remotes' we want to
3739 # show their ref dir
3740 my $ref_dir = (defined $1) ? $1 : '';
3741 if ($ref_dir ne '' and $ref_dir ne 'heads' and $ref_dir ne 'remotes') {
3742 $ref_item{'name'} .= ' (' . $ref_dir . ')';
3743 }
Jakub Narebskicd146402006-11-02 20:23:11 +01003744
Jakub Narebskicd146402006-11-02 20:23:11 +01003745 $ref_item{'id'} = $hash;
3746 $ref_item{'title'} = $title || '(no commit message)';
3747 $ref_item{'epoch'} = $epoch;
3748 if ($epoch) {
3749 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3750 } else {
3751 $ref_item{'age'} = "unknown";
Jakub Narebski717b8312006-07-31 21:22:15 +02003752 }
Jakub Narebskicd146402006-11-02 20:23:11 +01003753
3754 push @headslist, \%ref_item;
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003755 }
3756 close $fd;
Junio C Hamano7a13b992006-07-31 19:18:34 -07003757
Jakub Narebskicd146402006-11-02 20:23:11 +01003758 return wantarray ? @headslist : \@headslist;
3759}
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003760
Jakub Narebskicd146402006-11-02 20:23:11 +01003761sub git_get_tags_list {
3762 my $limit = shift;
3763 my @tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003764
Jakub Narebskicd146402006-11-02 20:23:11 +01003765 open my $fd, '-|', git_cmd(), 'for-each-ref',
3766 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3767 '--format=%(objectname) %(objecttype) %(refname) '.
3768 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3769 'refs/tags'
3770 or return;
3771 while (my $line = <$fd>) {
3772 my %ref_item;
3773
3774 chomp $line;
3775 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3776 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3777 my ($creator, $epoch, $tz) =
3778 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01003779 $ref_item{'fullname'} = $name;
Jakub Narebskicd146402006-11-02 20:23:11 +01003780 $name =~ s!^refs/tags/!!;
3781
3782 $ref_item{'type'} = $type;
3783 $ref_item{'id'} = $id;
3784 $ref_item{'name'} = $name;
3785 if ($type eq "tag") {
3786 $ref_item{'subject'} = $title;
3787 $ref_item{'reftype'} = $reftype;
3788 $ref_item{'refid'} = $refid;
3789 } else {
3790 $ref_item{'reftype'} = $type;
3791 $ref_item{'refid'} = $id;
3792 }
3793
3794 if ($type eq "tag" || $type eq "commit") {
3795 $ref_item{'epoch'} = $epoch;
3796 if ($epoch) {
3797 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3798 } else {
3799 $ref_item{'age'} = "unknown";
3800 }
3801 }
3802
3803 push @tagslist, \%ref_item;
Jakub Narebski717b8312006-07-31 21:22:15 +02003804 }
Jakub Narebskicd146402006-11-02 20:23:11 +01003805 close $fd;
3806
3807 return wantarray ? @tagslist : \@tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003808}
3809
3810## ----------------------------------------------------------------------
3811## filesystem-related functions
3812
3813sub get_file_owner {
3814 my $path = shift;
3815
3816 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3817 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3818 if (!defined $gcos) {
3819 return undef;
3820 }
3821 my $owner = $gcos;
3822 $owner =~ s/[,;].*$//;
Martin Koegler00f429a2007-06-03 17:42:44 +02003823 return to_utf8($owner);
Jakub Narebski717b8312006-07-31 21:22:15 +02003824}
3825
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01003826# assume that file exists
3827sub insert_file {
3828 my $filename = shift;
3829
3830 open my $fd, '<', $filename;
Jakub Narebski45868642008-12-08 14:13:21 +01003831 print map { to_utf8($_) } <$fd>;
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01003832 close $fd;
3833}
3834
Jakub Narebski717b8312006-07-31 21:22:15 +02003835## ......................................................................
3836## mimetype related functions
3837
3838sub mimetype_guess_file {
3839 my $filename = shift;
3840 my $mimemap = shift;
3841 -r $mimemap or return undef;
3842
3843 my %mimemap;
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003844 open(my $mh, '<', $mimemap) or return undef;
Jakub Narebskiad87e4f2009-05-11 03:21:06 +02003845 while (<$mh>) {
Jakub Narebski618918e2006-08-14 02:15:22 +02003846 next if m/^#/; # skip comments
Ludwig Nussel93a6ad12011-06-15 08:10:08 +02003847 my ($mimetype, @exts) = split(/\s+/);
3848 foreach my $ext (@exts) {
3849 $mimemap{$ext} = $mimetype;
Jakub Narebski717b8312006-07-31 21:22:15 +02003850 }
3851 }
Jakub Narebskiad87e4f2009-05-11 03:21:06 +02003852 close($mh);
Jakub Narebski717b8312006-07-31 21:22:15 +02003853
Jakub Narebski80593192006-09-19 13:57:03 +02003854 $filename =~ /\.([^.]*)$/;
Jakub Narebski717b8312006-07-31 21:22:15 +02003855 return $mimemap{$1};
3856}
3857
3858sub mimetype_guess {
3859 my $filename = shift;
3860 my $mime;
3861 $filename =~ /\./ or return undef;
3862
3863 if ($mimetypes_file) {
3864 my $file = $mimetypes_file;
Jakub Narebskid5aa50d2006-08-14 02:16:33 +02003865 if ($file !~ m!^/!) { # if it is relative path
3866 # it is relative to project
3867 $file = "$projectroot/$project/$file";
3868 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003869 $mime = mimetype_guess_file($filename, $file);
3870 }
3871 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3872 return $mime;
3873}
3874
Jakub Narebski847e01f2006-08-14 02:05:47 +02003875sub blob_mimetype {
Jakub Narebski717b8312006-07-31 21:22:15 +02003876 my $fd = shift;
3877 my $filename = shift;
3878
3879 if ($filename) {
3880 my $mime = mimetype_guess($filename);
3881 $mime and return $mime;
3882 }
3883
3884 # just in case
3885 return $default_blob_plain_mimetype unless $fd;
3886
3887 if (-T $fd) {
Jakub Narebski7f718e82008-06-03 16:47:10 +02003888 return 'text/plain';
Jakub Narebski717b8312006-07-31 21:22:15 +02003889 } elsif (! $filename) {
3890 return 'application/octet-stream';
3891 } elsif ($filename =~ m/\.png$/i) {
3892 return 'image/png';
3893 } elsif ($filename =~ m/\.gif$/i) {
3894 return 'image/gif';
3895 } elsif ($filename =~ m/\.jpe?g$/i) {
3896 return 'image/jpeg';
3897 } else {
3898 return 'application/octet-stream';
3899 }
3900}
3901
Jakub Narebski7f718e82008-06-03 16:47:10 +02003902sub blob_contenttype {
3903 my ($fd, $file_name, $type) = @_;
3904
3905 $type ||= blob_mimetype($fd, $file_name);
3906 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3907 $type .= "; charset=$default_text_plain_charset";
3908 }
3909
3910 return $type;
3911}
3912
Jakub Narebski592ea412010-04-27 21:34:45 +02003913# guess file syntax for syntax highlighting; return undef if no highlighting
3914# the name of syntax can (in the future) depend on syntax highlighter used
3915sub guess_file_syntax {
3916 my ($highlight, $mimetype, $file_name) = @_;
3917 return undef unless ($highlight && defined $file_name);
Jakub Narebski592ea412010-04-27 21:34:45 +02003918 my $basename = basename($file_name, '.in');
3919 return $highlight_basename{$basename}
3920 if exists $highlight_basename{$basename};
3921
3922 $basename =~ /\.([^.]*)$/;
3923 my $ext = $1 or return undef;
3924 return $highlight_ext{$ext}
3925 if exists $highlight_ext{$ext};
3926
3927 return undef;
3928}
3929
3930# run highlighter and return FD of its output,
3931# or return original FD if no highlighting
3932sub run_highlighter {
3933 my ($fd, $highlight, $syntax) = @_;
3934 return $fd unless ($highlight && defined $syntax);
3935
Sylvain Rabot3ca73532010-12-30 22:20:29 +01003936 close $fd;
Jakub Narebski592ea412010-04-27 21:34:45 +02003937 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
Christopher Wilson7ce896b2010-09-21 00:25:19 -07003938 quote_command($highlight_bin).
Kevin Cernekee6affdbe2011-03-16 15:34:13 -07003939 " --replace-tabs=8 --fragment --syntax $syntax |"
Jakub Narebski592ea412010-04-27 21:34:45 +02003940 or die_error(500, "Couldn't open file or run syntax highlighter");
3941 return $fd;
3942}
3943
Jakub Narebski717b8312006-07-31 21:22:15 +02003944## ======================================================================
3945## functions printing HTML: header, footer, error page
3946
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02003947sub get_page_title {
3948 my $title = to_utf8($site_name);
3949
Bernhard R. Link19d2d232012-01-30 21:07:37 +01003950 unless (defined $project) {
3951 if (defined $project_filter) {
Jakub Narebskif4212082012-02-12 16:21:30 +01003952 $title .= " - projects in '" . esc_path($project_filter) . "'";
Bernhard R. Link19d2d232012-01-30 21:07:37 +01003953 }
3954 return $title;
3955 }
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02003956 $title .= " - " . to_utf8($project);
3957
3958 return $title unless (defined $action);
3959 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3960
3961 return $title unless (defined $file_name);
3962 $title .= " - " . esc_path($file_name);
3963 if ($action eq "tree" && $file_name !~ m|/$|) {
3964 $title .= "/";
3965 }
3966
3967 return $title;
3968}
3969
Jakub Narebski6ee90332011-06-22 13:50:46 +02003970sub get_content_type_html {
3971 # require explicit support from the UA if we are to send the page as
3972 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3973 # we have to do this because MSIE sometimes globs '*/*', pretending to
3974 # support xhtml+xml but choking when it gets what it asked for.
3975 if (defined $cgi->http('HTTP_ACCEPT') &&
3976 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3977 $cgi->Accept('application/xhtml+xml') != 0) {
3978 return 'application/xhtml+xml';
3979 } else {
3980 return 'text/html';
3981 }
3982}
3983
Jakub Narebski05bb5a22010-12-18 21:02:13 +01003984sub print_feed_meta {
3985 if (defined $project) {
3986 my %href_params = get_feed_info();
3987 if (!exists $href_params{'-title'}) {
3988 $href_params{'-title'} = 'log';
3989 }
3990
Ævar Arnfjörð Bjarmason0f54b7d2011-02-19 15:27:41 +00003991 foreach my $format (qw(RSS Atom)) {
Jakub Narebski05bb5a22010-12-18 21:02:13 +01003992 my $type = lc($format);
3993 my %link_attr = (
3994 '-rel' => 'alternate',
3995 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
3996 '-type' => "application/$type+xml"
3997 );
3998
Sebastian Pippingcc999a32012-04-04 14:25:44 +02003999 $href_params{'extra_options'} = undef;
Jakub Narebski05bb5a22010-12-18 21:02:13 +01004000 $href_params{'action'} = $type;
4001 $link_attr{'-href'} = href(%href_params);
4002 print "<link ".
4003 "rel=\"$link_attr{'-rel'}\" ".
4004 "title=\"$link_attr{'-title'}\" ".
4005 "href=\"$link_attr{'-href'}\" ".
4006 "type=\"$link_attr{'-type'}\" ".
4007 "/>\n";
4008
4009 $href_params{'extra_options'} = '--no-merges';
4010 $link_attr{'-href'} = href(%href_params);
4011 $link_attr{'-title'} .= ' (no merges)';
4012 print "<link ".
4013 "rel=\"$link_attr{'-rel'}\" ".
4014 "title=\"$link_attr{'-title'}\" ".
4015 "href=\"$link_attr{'-href'}\" ".
4016 "type=\"$link_attr{'-type'}\" ".
4017 "/>\n";
4018 }
4019
4020 } else {
4021 printf('<link rel="alternate" title="%s projects list" '.
4022 'href="%s" type="text/plain; charset=utf-8" />'."\n",
4023 esc_attr($site_name), href(project=>undef, action=>"project_index"));
4024 printf('<link rel="alternate" title="%s projects feeds" '.
4025 'href="%s" type="text/x-opml" />'."\n",
4026 esc_attr($site_name), href(project=>undef, action=>"opml"));
4027 }
4028}
4029
Jakub Narebski6ee90332011-06-22 13:50:46 +02004030sub print_header_links {
4031 my $status = shift;
4032
4033 # print out each stylesheet that exist, providing backwards capability
4034 # for those people who defined $stylesheet in a config file
4035 if (defined $stylesheet) {
4036 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
4037 } else {
4038 foreach my $stylesheet (@stylesheets) {
4039 next unless $stylesheet;
4040 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
4041 }
4042 }
4043 print_feed_meta()
4044 if ($status eq '200 OK');
4045 if (defined $favicon) {
4046 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
4047 }
4048}
4049
Bernhard R. Link40efa222012-01-30 21:09:43 +01004050sub print_nav_breadcrumbs_path {
4051 my $dirprefix = undef;
4052 while (my $part = shift) {
4053 $dirprefix .= "/" if defined $dirprefix;
4054 $dirprefix .= $part;
4055 print $cgi->a({-href => href(project => undef,
4056 project_filter => $dirprefix,
4057 action => "project_list")},
4058 esc_html($part)) . " / ";
4059 }
4060}
4061
Jakub Narebski6ee90332011-06-22 13:50:46 +02004062sub print_nav_breadcrumbs {
4063 my %opts = @_;
4064
Tony Finchad9c2e22013-07-04 18:02:12 +01004065 for my $crumb (@extra_breadcrumbs, [ $home_link_str => $home_link ]) {
4066 print $cgi->a({-href => esc_url($crumb->[1])}, $crumb->[0]) . " / ";
4067 }
Jakub Narebski6ee90332011-06-22 13:50:46 +02004068 if (defined $project) {
Bernhard R. Link4426ba22012-01-30 21:10:23 +01004069 my @dirname = split '/', $project;
4070 my $projectbasename = pop @dirname;
4071 print_nav_breadcrumbs_path(@dirname);
4072 print $cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));
Jakub Narebski6ee90332011-06-22 13:50:46 +02004073 if (defined $action) {
4074 my $action_print = $action ;
4075 if (defined $opts{-action_extra}) {
4076 $action_print = $cgi->a({-href => href(action=>$action)},
4077 $action);
4078 }
4079 print " / $action_print";
4080 }
4081 if (defined $opts{-action_extra}) {
4082 print " / $opts{-action_extra}";
4083 }
4084 print "\n";
Bernhard R. Link40efa222012-01-30 21:09:43 +01004085 } elsif (defined $project_filter) {
4086 print_nav_breadcrumbs_path(split '/', $project_filter);
Jakub Narebski6ee90332011-06-22 13:50:46 +02004087 }
4088}
4089
4090sub print_search_form {
4091 if (!defined $searchtext) {
4092 $searchtext = "";
4093 }
4094 my $search_hash;
4095 if (defined $hash_base) {
4096 $search_hash = $hash_base;
4097 } elsif (defined $hash) {
4098 $search_hash = $hash;
4099 } else {
4100 $search_hash = "HEAD";
4101 }
4102 my $action = $my_uri;
4103 my $use_pathinfo = gitweb_check_feature('pathinfo');
4104 if ($use_pathinfo) {
4105 $action .= "/".esc_url($project);
4106 }
Roland Mas4750f4b2014-10-16 08:54:47 +02004107 print $cgi->start_form(-method => "get", -action => $action) .
Jakub Narebski6ee90332011-06-22 13:50:46 +02004108 "<div class=\"search\">\n" .
4109 (!$use_pathinfo &&
4110 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
4111 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
4112 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
4113 $cgi->popup_menu(-name => 'st', -default => 'commit',
4114 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
Tony Finchaf52bd52013-08-20 17:59:54 +01004115 " " . $cgi->a({-href => href(action=>"search_help"),
4116 -title => "search help" }, "?") . " search:\n",
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01004117 $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
Jakub Narebski6ee90332011-06-22 13:50:46 +02004118 "<span title=\"Extended regular expression\">" .
4119 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
4120 -checked => $search_use_regexp) .
4121 "</span>" .
4122 "</div>" .
4123 $cgi->end_form() . "\n";
4124}
4125
Kay Sievers12a88f22005-08-07 20:02:47 +02004126sub git_header_html {
Kay Sieversa59d4af2005-08-07 20:15:44 +02004127 my $status = shift || "200 OK";
Kay Sievers11044292005-10-19 03:18:45 +02004128 my $expires = shift;
Jakub Narebski7a597452010-04-24 16:00:04 +02004129 my %opts = @_;
Kay Sieversa59d4af2005-08-07 20:15:44 +02004130
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02004131 my $title = get_page_title();
Jakub Narebski6ee90332011-06-22 13:50:46 +02004132 my $content_type = get_content_type_html();
Jakub Narebski952c65f2006-08-22 16:52:50 +02004133 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
Jakub Narebski7a597452010-04-24 16:00:04 +02004134 -status=> $status, -expires => $expires)
Jakub Narebskiad709ea2010-06-13 00:35:59 +02004135 unless ($opts{'-no_http_header'});
Jakub Narebski45c9a752006-12-27 23:59:51 +01004136 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
Kay Sieversa59d4af2005-08-07 20:15:44 +02004137 print <<EOF;
Kay Sievers6191f8e2005-08-07 20:19:56 +02004138<?xml version="1.0" encoding="utf-8"?>
Kay Sievers161332a2005-08-07 19:49:46 +02004139<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Kay Sievers034df392005-08-07 20:20:07 +02004140<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02004141<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
Jakub Narebskiae20de52006-06-21 09:48:03 +02004142<!-- git core binaries version $git_version -->
Kay Sievers161332a2005-08-07 19:49:46 +02004143<head>
Alp Tokerf6801d62006-07-11 11:19:34 +01004144<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
Jakub Narebski45c9a752006-12-27 23:59:51 +01004145<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
Kay Sieversc994d622005-08-07 20:27:18 +02004146<meta name="robots" content="index, nofollow"/>
Kay Sieversb87d78d2005-08-07 20:21:04 +02004147<title>$title</title>
Kay Sievers161332a2005-08-07 19:49:46 +02004148EOF
Giuseppe Bilotta41a4d162009-01-31 02:31:52 +01004149 # the stylesheet, favicon etc urls won't work correctly with path_info
4150 # unless we set the appropriate base URL
Giuseppe Bilottac3254ae2009-01-31 02:31:50 +01004151 if ($ENV{'PATH_INFO'}) {
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +01004152 print "<base href=\"".esc_url($base_url)."\" />\n";
Giuseppe Bilottac3254ae2009-01-31 02:31:50 +01004153 }
Jakub Narebski6ee90332011-06-22 13:50:46 +02004154 print_header_links($status);
Lénaïc Huardc1355b72011-10-21 09:09:29 +02004155
4156 if (defined $site_html_head_string) {
4157 print to_utf8($site_html_head_string);
4158 }
4159
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02004160 print "</head>\n" .
Alan Chandlerb2d34762006-10-03 13:49:03 +01004161 "<body>\n";
4162
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01004163 if (defined $site_header && -f $site_header) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004164 insert_file($site_header);
Alan Chandlerb2d34762006-10-03 13:49:03 +01004165 }
4166
Jonathan Nieder68220522010-09-03 19:45:09 -05004167 print "<div class=\"page_header\">\n";
4168 if (defined $logo) {
4169 print $cgi->a({-href => esc_url($logo_url),
4170 -title => $logo_label},
4171 $cgi->img({-src => esc_url($logo),
4172 -width => 72, -height => 27,
4173 -alt => "git",
4174 -class => "logo"}));
4175 }
Jakub Narebski6ee90332011-06-22 13:50:46 +02004176 print_nav_breadcrumbs(%opts);
Petr Baudisd77b5672007-05-17 04:24:19 +02004177 print "</div>\n";
4178
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004179 my $have_search = gitweb_check_feature('search');
Jakub Narebskif70dda22008-06-02 11:54:41 +02004180 if (defined $project && $have_search) {
Jakub Narebski6ee90332011-06-22 13:50:46 +02004181 print_search_form();
Kay Sievers4c02e3c2005-08-07 19:52:52 +02004182 }
Kay Sievers161332a2005-08-07 19:49:46 +02004183}
4184
Kay Sievers12a88f22005-08-07 20:02:47 +02004185sub git_footer_html {
Jakub Narebski35621982008-04-20 22:09:48 +02004186 my $feed_class = 'rss_logo';
4187
Kay Sievers6191f8e2005-08-07 20:19:56 +02004188 print "<div class=\"page_footer\">\n";
Kay Sieversb87d78d2005-08-07 20:21:04 +02004189 if (defined $project) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004190 my $descr = git_get_project_description($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02004191 if (defined $descr) {
Kay Sievers40c13812005-11-19 17:41:29 +01004192 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
Kay Sievers6191f8e2005-08-07 20:19:56 +02004193 }
Jakub Narebski35621982008-04-20 22:09:48 +02004194
4195 my %href_params = get_feed_info();
4196 if (!%href_params) {
4197 $feed_class .= ' generic';
4198 }
4199 $href_params{'-title'} ||= 'log';
4200
Ævar Arnfjörð Bjarmason0f54b7d2011-02-19 15:27:41 +00004201 foreach my $format (qw(RSS Atom)) {
Jakub Narebski35621982008-04-20 22:09:48 +02004202 $href_params{'action'} = lc($format);
4203 print $cgi->a({-href => href(%href_params),
4204 -title => "$href_params{'-title'} $format feed",
4205 -class => $feed_class}, $format)."\n";
4206 }
4207
Kay Sieversc994d622005-08-07 20:27:18 +02004208 } else {
Bernhard R. Link56efd9d2012-01-30 21:09:00 +01004209 print $cgi->a({-href => href(project=>undef, action=>"opml",
4210 project_filter => $project_filter),
Jakub Narebski35621982008-04-20 22:09:48 +02004211 -class => $feed_class}, "OPML") . " ";
Bernhard R. Link56efd9d2012-01-30 21:09:00 +01004212 print $cgi->a({-href => href(project=>undef, action=>"project_index",
4213 project_filter => $project_filter),
Jakub Narebski35621982008-04-20 22:09:48 +02004214 -class => $feed_class}, "TXT") . "\n";
Kay Sieversff7669a2005-08-07 20:13:02 +02004215 }
Jakub Narebski35621982008-04-20 22:09:48 +02004216 print "</div>\n"; # class="page_footer"
Alan Chandlerb2d34762006-10-03 13:49:03 +01004217
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02004218 if (defined $t0 && gitweb_check_feature('timed')) {
4219 print "<div id=\"generating_info\">\n";
4220 print 'This page took '.
4221 '<span id="generating_time" class="time_span">'.
Jakub Narebski3962f1d72010-11-09 19:27:54 +01004222 tv_interval($t0, [ gettimeofday() ]).
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02004223 ' seconds </span>'.
4224 ' and '.
4225 '<span id="generating_cmd">'.
4226 $number_of_git_cmds.
4227 '</span> git commands '.
4228 " to generate.\n";
4229 print "</div>\n"; # class="page_footer"
4230 }
4231
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01004232 if (defined $site_footer && -f $site_footer) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004233 insert_file($site_footer);
Alan Chandlerb2d34762006-10-03 13:49:03 +01004234 }
4235
Junio C Hamanoabf411e2010-12-15 11:32:57 -08004236 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004237 if (defined $action &&
4238 $action eq 'blame_incremental') {
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004239 print qq!<script type="text/javascript">\n!.
4240 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
4241 qq! "!. href() .qq!");\n!.
4242 qq!</script>\n!;
John 'Warthog9' Hawley291e52b2011-04-28 21:04:09 +02004243 } else {
Jakub Narebski2e987f92011-04-28 21:04:11 +02004244 my ($jstimezone, $tz_cookie, $datetime_class) =
4245 gitweb_get_feature('javascript-timezone');
4246
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004247 print qq!<script type="text/javascript">\n!.
Jakub Narebski2e987f92011-04-28 21:04:11 +02004248 qq!window.onload = function () {\n!;
4249 if (gitweb_check_feature('javascript-actions')) {
4250 print qq! fixLinks();\n!;
4251 }
4252 if ($jstimezone && $tz_cookie && $datetime_class) {
4253 print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
4254 qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
4255 }
4256 print qq!};\n!.
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004257 qq!</script>\n!;
4258 }
4259
Alan Chandlerb2d34762006-10-03 13:49:03 +01004260 print "</body>\n" .
Kay Sievers9cd3d982005-08-07 20:17:42 +02004261 "</html>";
Kay Sievers161332a2005-08-07 19:49:46 +02004262}
4263
Jakub Narebski453541f2010-02-07 21:51:18 +01004264# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
Lea Wiemann074afaa2008-06-19 22:03:21 +02004265# Example: die_error(404, 'Hash not found')
4266# By convention, use the following status codes (as defined in RFC 2616):
4267# 400: Invalid or missing CGI parameters, or
4268# requested object exists but has wrong type.
4269# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
4270# this server or project.
4271# 404: Requested object/revision/project doesn't exist.
4272# 500: The server isn't configured properly, or
4273# an internal error occurred (e.g. failed assertions caused by bugs), or
4274# an unknown error occurred (e.g. the git binary died unexpectedly).
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004275# 503: The server is currently unavailable (because it is overloaded,
4276# or down for maintenance). Generally, this is a temporary state.
Kay Sievers061cc7c2005-08-07 20:15:57 +02004277sub die_error {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004278 my $status = shift || 500;
Jakub Narebski1df48762010-02-07 21:52:25 +01004279 my $error = esc_html(shift) || "Internal Server Error";
John 'Warthog9' Hawleyaa140132010-01-30 23:30:44 +01004280 my $extra = shift;
Jakub Narebski7a597452010-04-24 16:00:04 +02004281 my %opts = @_;
Kay Sievers664f4cc2005-08-07 20:17:19 +02004282
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004283 my %http_responses = (
4284 400 => '400 Bad Request',
4285 403 => '403 Forbidden',
4286 404 => '404 Not Found',
4287 500 => '500 Internal Server Error',
4288 503 => '503 Service Unavailable',
4289 );
Jakub Narebski7a597452010-04-24 16:00:04 +02004290 git_header_html($http_responses{$status}, undef, %opts);
Jakub Narebski59b9f612006-08-22 23:42:53 +02004291 print <<EOF;
4292<div class="page_body">
4293<br /><br />
4294$status - $error
4295<br />
Jakub Narebski59b9f612006-08-22 23:42:53 +02004296EOF
John 'Warthog9' Hawleyaa140132010-01-30 23:30:44 +01004297 if (defined $extra) {
4298 print "<hr />\n" .
4299 "$extra\n";
4300 }
4301 print "</div>\n";
4302
Kay Sieversa59d4af2005-08-07 20:15:44 +02004303 git_footer_html();
Jakub Narebski7a597452010-04-24 16:00:04 +02004304 goto DONE_GITWEB
4305 unless ($opts{'-error_handler'});
Kay Sieversa59d4af2005-08-07 20:15:44 +02004306}
4307
Jakub Narebski717b8312006-07-31 21:22:15 +02004308## ----------------------------------------------------------------------
4309## functions printing or outputting HTML: navigation
4310
Jakub Narebski847e01f2006-08-14 02:05:47 +02004311sub git_print_page_nav {
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004312 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
4313 $extra = '' if !defined $extra; # pager or formats
4314
4315 my @navs = qw(summary shortlog log commit commitdiff tree);
4316 if ($suppress) {
4317 @navs = grep { $_ ne $suppress } @navs;
4318 }
4319
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004320 my %arg = map { $_ => {action=>$_} } @navs;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004321 if (defined $head) {
4322 for (qw(commit commitdiff)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02004323 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004324 }
4325 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4326 for (qw(shortlog log)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02004327 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004328 }
4329 }
4330 }
Petr Baudisd627f682008-10-02 16:36:52 +02004331
Jakub Narebski3be8e722007-04-01 22:22:21 +02004332 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4333 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004334
Junio C Hamanoa7c5a282008-11-29 13:02:08 -08004335 my @actions = gitweb_get_feature('actions');
Jakub Narebski2b11e052008-10-12 00:02:32 +02004336 my %repl = (
4337 '%' => '%',
4338 'n' => $project, # project name
4339 'f' => $git_dir, # project path within filesystem
4340 'h' => $treehead || '', # current hash ('h' parameter)
4341 'b' => $treebase || '', # hash base ('hb' parameter)
4342 );
Petr Baudisd627f682008-10-02 16:36:52 +02004343 while (@actions) {
Jakub Narebski2b11e052008-10-12 00:02:32 +02004344 my ($label, $link, $pos) = splice(@actions,0,3);
4345 # insert
Petr Baudisd627f682008-10-02 16:36:52 +02004346 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4347 # munch munch
Jakub Narebski2b11e052008-10-12 00:02:32 +02004348 $link =~ s/%([%nfhb])/$repl{$1}/g;
Petr Baudisd627f682008-10-02 16:36:52 +02004349 $arg{$label}{'_href'} = $link;
4350 }
4351
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004352 print "<div class=\"page_nav\">\n" .
4353 (join " | ",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004354 map { $_ eq $current ?
Petr Baudisd627f682008-10-02 16:36:52 +02004355 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004356 } @navs);
Jakub Narebski898a8932006-07-30 16:14:43 +02004357 print "<br/>\n$extra<br/>\n" .
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004358 "</div>\n";
4359}
4360
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01004361# returns a submenu for the nagivation of the refs views (tags, heads,
4362# remotes) with the current view disabled and the remotes view only
4363# available if the feature is enabled
4364sub format_ref_views {
4365 my ($current) = @_;
4366 my @ref_views = qw{tags heads};
4367 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4368 return join " | ", map {
4369 $_ eq $current ? $_ :
4370 $cgi->a({-href => href(action=>$_)}, $_)
4371 } @ref_views
4372}
4373
Jakub Narebski847e01f2006-08-14 02:05:47 +02004374sub format_paging_nav {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004375 my ($action, $page, $has_next_link) = @_;
Jakub Narebski43ffc062006-07-30 17:49:00 +02004376 my $paging_nav;
4377
4378
Jakub Narebski43ffc062006-07-30 17:49:00 +02004379 if ($page > 0) {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004380 $paging_nav .=
4381 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4382 " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01004383 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebski26298b52006-08-10 12:38:47 +02004384 -accesskey => "p", -title => "Alt-p"}, "prev");
Jakub Narebski43ffc062006-07-30 17:49:00 +02004385 } else {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004386 $paging_nav .= "first &sdot; prev";
Jakub Narebski43ffc062006-07-30 17:49:00 +02004387 }
4388
Lea Wiemann1f684dc2008-05-28 01:25:42 +02004389 if ($has_next_link) {
Jakub Narebski43ffc062006-07-30 17:49:00 +02004390 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01004391 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Jakub Narebski26298b52006-08-10 12:38:47 +02004392 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski43ffc062006-07-30 17:49:00 +02004393 } else {
4394 $paging_nav .= " &sdot; next";
4395 }
4396
4397 return $paging_nav;
4398}
4399
Jakub Narebski717b8312006-07-31 21:22:15 +02004400## ......................................................................
4401## functions printing or outputting HTML: div
Kay Sievers42f7eb92005-08-07 20:21:46 +02004402
Jakub Narebski847e01f2006-08-14 02:05:47 +02004403sub git_print_header_div {
Jakub Narebski717b8312006-07-31 21:22:15 +02004404 my ($action, $title, $hash, $hash_base) = @_;
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004405 my %args = ();
Jakub Narebski717b8312006-07-31 21:22:15 +02004406
Jakub Narebski3be8e722007-04-01 22:22:21 +02004407 $args{'action'} = $action;
4408 $args{'hash'} = $hash if $hash;
4409 $args{'hash_base'} = $hash_base if $hash_base;
Jakub Narebski717b8312006-07-31 21:22:15 +02004410
4411 print "<div class=\"header\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004412 $cgi->a({-href => href(%args), -class => "title"},
4413 $title ? $title : $action) .
4414 "\n</div>\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02004415}
4416
Giuseppe Bilotta0e656992010-11-11 13:26:15 +01004417sub format_repo_url {
4418 my ($name, $url) = @_;
4419 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4420}
4421
Giuseppe Bilottab891d522010-11-11 13:26:16 +01004422# Group output by placing it in a DIV element and adding a header.
4423# Options for start_div() can be provided by passing a hash reference as the
4424# first parameter to the function.
4425# Options to git_print_header_div() can be provided by passing an array
4426# reference. This must follow the options to start_div if they are present.
4427# The content can be a scalar, which is output as-is, a scalar reference, which
4428# is output after html escaping, an IO handle passed either as *handle or
4429# *handle{IO}, or a function reference. In the latter case all following
4430# parameters will be taken as argument to the content function call.
4431sub git_print_section {
4432 my ($div_args, $header_args, $content);
4433 my $arg = shift;
4434 if (ref($arg) eq 'HASH') {
4435 $div_args = $arg;
4436 $arg = shift;
4437 }
4438 if (ref($arg) eq 'ARRAY') {
4439 $header_args = $arg;
4440 $arg = shift;
4441 }
4442 $content = $arg;
4443
4444 print $cgi->start_div($div_args);
4445 git_print_header_div(@$header_args);
4446
4447 if (ref($content) eq 'CODE') {
4448 $content->(@_);
4449 } elsif (ref($content) eq 'SCALAR') {
4450 print esc_html($$content);
4451 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4452 print <$content>;
4453 } elsif (!ref($content) && defined($content)) {
4454 print $content;
4455 }
4456
4457 print $cgi->end_div;
4458}
4459
Jakub Narebski256b7b42011-04-28 21:04:07 +02004460sub format_timestamp_html {
Jakub Narebskice71b072011-04-28 21:04:08 +02004461 my $date = shift;
Jakub Narebski2e987f92011-04-28 21:04:11 +02004462 my $strtime = $date->{'rfc2822'};
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004463
Jakub Narebski2e987f92011-04-28 21:04:11 +02004464 my (undef, undef, $datetime_class) =
4465 gitweb_get_feature('javascript-timezone');
4466 if ($datetime_class) {
4467 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
Jakub Narebskia44465c2006-08-28 23:17:31 +02004468 }
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004469
Jakub Narebski256b7b42011-04-28 21:04:07 +02004470 my $localtime_format = '(%02d:%02d %s)';
4471 if ($date->{'hour_local'} < 6) {
4472 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
Jakub Narebskia44465c2006-08-28 23:17:31 +02004473 }
Jakub Narebski256b7b42011-04-28 21:04:07 +02004474 $strtime .= ' ' .
4475 sprintf($localtime_format,
4476 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004477
Jakub Narebski256b7b42011-04-28 21:04:07 +02004478 return $strtime;
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004479}
4480
4481# Outputs the author name and date in long form
4482sub git_print_authorship {
4483 my $co = shift;
4484 my %opts = @_;
4485 my $tag = $opts{-tag} || 'div';
Stephen Boyde133d652009-10-15 21:14:59 -07004486 my $author = $co->{'author_name'};
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004487
4488 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4489 print "<$tag class=\"author_date\">" .
Stephen Boyde133d652009-10-15 21:14:59 -07004490 format_search_author($author, "author", esc_html($author)) .
Jakub Narebskice71b072011-04-28 21:04:08 +02004491 " [".format_timestamp_html(\%ad)."]".
Jakub Narebski256b7b42011-04-28 21:04:07 +02004492 git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4493 "</$tag>\n";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004494}
4495
4496# Outputs table rows containing the full author or committer information,
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02004497# in the format expected for 'commit' view (& similar).
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004498# Parameters are a commit hash reference, followed by the list of people
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02004499# to output information for. If the list is empty it defaults to both
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004500# author and committer.
4501sub git_print_authorship_rows {
4502 my $co = shift;
4503 # too bad we can't use @people = @_ || ('author', 'committer')
4504 my @people = @_;
4505 @people = ('author', 'committer') unless @people;
4506 foreach my $who (@people) {
4507 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
Stephen Boyde133d652009-10-15 21:14:59 -07004508 print "<tr><td>$who</td><td>" .
4509 format_search_author($co->{"${who}_name"}, $who,
Jakub Narebski256b7b42011-04-28 21:04:07 +02004510 esc_html($co->{"${who}_name"})) . " " .
Stephen Boyde133d652009-10-15 21:14:59 -07004511 format_search_author($co->{"${who}_email"}, $who,
Jakub Narebski256b7b42011-04-28 21:04:07 +02004512 esc_html("<" . $co->{"${who}_email"} . ">")) .
Stephen Boyde133d652009-10-15 21:14:59 -07004513 "</td><td rowspan=\"2\">" .
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02004514 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4515 "</td></tr>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004516 "<tr>" .
Jakub Narebski256b7b42011-04-28 21:04:07 +02004517 "<td></td><td>" .
Jakub Narebskice71b072011-04-28 21:04:08 +02004518 format_timestamp_html(\%wd) .
Jakub Narebski256b7b42011-04-28 21:04:07 +02004519 "</td>" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004520 "</tr>\n";
4521 }
Jakub Narebski6fd92a22006-08-28 14:48:12 +02004522}
4523
Jakub Narebski717b8312006-07-31 21:22:15 +02004524sub git_print_page_path {
4525 my $name = shift;
4526 my $type = shift;
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004527 my $hb = shift;
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01004528
Jakub Narebski4df118e2006-10-21 17:53:55 +02004529
4530 print "<div class=\"page_path\">";
4531 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
Martin Koegler00f429a2007-06-03 17:42:44 +02004532 -title => 'tree root'}, to_utf8("[$project]"));
Jakub Narebski4df118e2006-10-21 17:53:55 +02004533 print " / ";
4534 if (defined $name) {
Jakub Narebski762c7202006-09-04 18:17:58 +02004535 my @dirname = split '/', $name;
4536 my $basename = pop @dirname;
4537 my $fullname = '';
4538
Jakub Narebski762c7202006-09-04 18:17:58 +02004539 foreach my $dir (@dirname) {
Petr Baudis16fdb482006-09-21 02:05:50 +02004540 $fullname .= ($fullname ? '/' : '') . $dir;
Jakub Narebski762c7202006-09-04 18:17:58 +02004541 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4542 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004543 -title => $fullname}, esc_path($dir));
Petr Baudis26d0a972006-09-23 01:00:12 +02004544 print " / ";
Jakub Narebski762c7202006-09-04 18:17:58 +02004545 }
4546 if (defined $type && $type eq 'blob') {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004547 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
Jakub Narebski762c7202006-09-04 18:17:58 +02004548 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004549 -title => $name}, esc_path($basename));
Jakub Narebski762c7202006-09-04 18:17:58 +02004550 } elsif (defined $type && $type eq 'tree') {
4551 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4552 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004553 -title => $name}, esc_path($basename));
Jakub Narebski4df118e2006-10-21 17:53:55 +02004554 print " / ";
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004555 } else {
Jakub Narebski403d0902006-11-08 11:48:56 +01004556 print esc_path($basename);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004557 }
Kay Sievers8ed23e12005-08-07 20:05:44 +02004558 }
Jakub Narebski4df118e2006-10-21 17:53:55 +02004559 print "<br/></div>\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02004560}
4561
Jakub Narebski74fd8722009-05-07 19:11:29 +02004562sub git_print_log {
Jakub Narebskid16d0932006-08-17 11:21:23 +02004563 my $log = shift;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004564 my %opts = @_;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004565
Jakub Narebskib7f92532006-08-28 14:48:10 +02004566 if ($opts{'-remove_title'}) {
4567 # remove title, i.e. first line of log
4568 shift @$log;
4569 }
Jakub Narebskid16d0932006-08-17 11:21:23 +02004570 # remove leading empty lines
4571 while (defined $log->[0] && $log->[0] eq "") {
4572 shift @$log;
4573 }
4574
4575 # print log
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004576 my $skip_blank_line = 0;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004577 foreach my $line (@$log) {
Namhyung Kim3d1110a2012-07-04 11:47:25 +09004578 if ($line =~ m/^\s*([A-Z][-A-Za-z]*-[Bb]y|C[Cc]): /) {
Jakub Narebskib7f92532006-08-28 14:48:10 +02004579 if (! $opts{'-remove_signoff'}) {
4580 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004581 $skip_blank_line = 1;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004582 }
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004583 next;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004584 }
4585
Namhyung Kim66c857e2012-07-04 11:47:26 +09004586 if ($line =~ m,\s*([a-z]*link): (https?://\S+),i) {
4587 if (! $opts{'-remove_signoff'}) {
4588 print "<span class=\"signoff\">" . esc_html($1) . ": " .
4589 "<a href=\"" . esc_html($2) . "\">" . esc_html($2) . "</a>" .
4590 "</span><br/>\n";
4591 $skip_blank_line = 1;
4592 }
4593 next;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004594 }
4595
4596 # print only one empty line
4597 # do not print empty line after signoff
4598 if ($line eq "") {
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004599 next if ($skip_blank_line);
4600 $skip_blank_line = 1;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004601 } else {
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004602 $skip_blank_line = 0;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004603 }
Jakub Narebskib7f92532006-08-28 14:48:10 +02004604
4605 print format_log_line_html($line) . "<br/>\n";
4606 }
4607
4608 if ($opts{'-final_empty_line'}) {
4609 # end with single empty line
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004610 print "<br/>\n" unless $skip_blank_line;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004611 }
4612}
4613
Jakub Narebskie33fba42006-12-10 13:25:46 +01004614# return link target (what link points to)
4615sub git_get_link_target {
4616 my $hash = shift;
4617 my $link_target;
4618
4619 # read link
4620 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4621 or return;
4622 {
Jakub Narebski34122b52009-05-11 03:29:40 +02004623 local $/ = undef;
Jakub Narebskie33fba42006-12-10 13:25:46 +01004624 $link_target = <$fd>;
4625 }
4626 close $fd
4627 or return;
4628
4629 return $link_target;
4630}
4631
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004632# given link target, and the directory (basedir) the link is in,
4633# return target of link relative to top directory (top tree);
4634# return undef if it is not possible (including absolute links).
4635sub normalize_link_target {
Jakub Narebski15c54fe2009-05-11 19:45:11 +02004636 my ($link_target, $basedir) = @_;
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004637
4638 # absolute symlinks (beginning with '/') cannot be normalized
4639 return if (substr($link_target, 0, 1) eq '/');
4640
4641 # normalize link target to path from top (root) tree (dir)
4642 my $path;
4643 if ($basedir) {
4644 $path = $basedir . '/' . $link_target;
4645 } else {
4646 # we are in top (root) tree (dir)
4647 $path = $link_target;
4648 }
4649
4650 # remove //, /./, and /../
4651 my @path_parts;
4652 foreach my $part (split('/', $path)) {
4653 # discard '.' and ''
4654 next if (!$part || $part eq '.');
4655 # handle '..'
4656 if ($part eq '..') {
4657 if (@path_parts) {
4658 pop @path_parts;
4659 } else {
4660 # link leads outside repository (outside top dir)
4661 return;
4662 }
4663 } else {
4664 push @path_parts, $part;
4665 }
4666 }
4667 $path = join('/', @path_parts);
4668
4669 return $path;
4670}
Jakub Narebskie33fba42006-12-10 13:25:46 +01004671
Jakub Narebskifa702002006-08-31 00:35:07 +02004672# print tree entry (row of git_tree), but without encompassing <tr> element
4673sub git_print_tree_entry {
4674 my ($t, $basedir, $hash_base, $have_blame) = @_;
4675
4676 my %base_key = ();
Jakub Narebskie33fba42006-12-10 13:25:46 +01004677 $base_key{'hash_base'} = $hash_base if defined $hash_base;
Jakub Narebskifa702002006-08-31 00:35:07 +02004678
Luben Tuikov4de741b2006-09-25 22:38:16 -07004679 # The format of a table row is: mode list link. Where mode is
4680 # the mode of the entry, list is the name of the entry, an href,
4681 # and link is the action links of the entry.
4682
Jakub Narebskifa702002006-08-31 00:35:07 +02004683 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004684 if (exists $t->{'size'}) {
4685 print "<td class=\"size\">$t->{'size'}</td>\n";
4686 }
Jakub Narebskifa702002006-08-31 00:35:07 +02004687 if ($t->{'type'} eq "blob") {
4688 print "<td class=\"list\">" .
Luben Tuikov4de741b2006-09-25 22:38:16 -07004689 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004690 file_name=>"$basedir$t->{'name'}", %base_key),
Jakub Narebskie33fba42006-12-10 13:25:46 +01004691 -class => "list"}, esc_path($t->{'name'}));
4692 if (S_ISLNK(oct $t->{'mode'})) {
4693 my $link_target = git_get_link_target($t->{'hash'});
4694 if ($link_target) {
Jakub Narebski15c54fe2009-05-11 19:45:11 +02004695 my $norm_target = normalize_link_target($link_target, $basedir);
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004696 if (defined $norm_target) {
4697 print " -> " .
4698 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4699 file_name=>$norm_target),
4700 -title => $norm_target}, esc_path($link_target));
4701 } else {
4702 print " -> " . esc_path($link_target);
4703 }
Jakub Narebskie33fba42006-12-10 13:25:46 +01004704 }
4705 }
4706 print "</td>\n";
Luben Tuikov4de741b2006-09-25 22:38:16 -07004707 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02004708 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004709 file_name=>"$basedir$t->{'name'}", %base_key)},
4710 "blob");
Jakub Narebskifa702002006-08-31 00:35:07 +02004711 if ($have_blame) {
Petr Baudis4777b012006-10-24 05:36:10 +02004712 print " | " .
4713 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004714 file_name=>"$basedir$t->{'name'}", %base_key)},
4715 "blame");
Jakub Narebskifa702002006-08-31 00:35:07 +02004716 }
4717 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02004718 print " | " .
4719 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02004720 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4721 "history");
4722 }
4723 print " | " .
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004724 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004725 file_name=>"$basedir$t->{'name'}")},
4726 "raw");
Luben Tuikov4de741b2006-09-25 22:38:16 -07004727 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02004728
4729 } elsif ($t->{'type'} eq "tree") {
Luben Tuikov0fa105e2006-09-26 12:45:37 -07004730 print "<td class=\"list\">";
4731 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004732 file_name=>"$basedir$t->{'name'}",
4733 %base_key)},
Jakub Narebski403d0902006-11-08 11:48:56 +01004734 esc_path($t->{'name'}));
Luben Tuikov0fa105e2006-09-26 12:45:37 -07004735 print "</td>\n";
4736 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02004737 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004738 file_name=>"$basedir$t->{'name'}",
4739 %base_key)},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004740 "tree");
Jakub Narebskifa702002006-08-31 00:35:07 +02004741 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02004742 print " | " .
4743 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02004744 file_name=>"$basedir$t->{'name'}")},
4745 "history");
4746 }
4747 print "</td>\n";
Jakub Narebski01ac1e32007-07-28 16:27:31 +02004748 } else {
4749 # unknown object: we can only present history for it
4750 # (this includes 'commit' object, i.e. submodule support)
4751 print "<td class=\"list\">" .
4752 esc_path($t->{'name'}) .
4753 "</td>\n";
4754 print "<td class=\"link\">";
4755 if (defined $hash_base) {
4756 print $cgi->a({-href => href(action=>"history",
4757 hash_base=>$hash_base,
4758 file_name=>"$basedir$t->{'name'}")},
4759 "history");
4760 }
4761 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02004762 }
4763}
4764
Jakub Narebski717b8312006-07-31 21:22:15 +02004765## ......................................................................
4766## functions printing large fragments of HTML
Kay Sieversede5e102005-08-07 20:23:12 +02004767
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004768# get pre-image filenames for merge (combined) diff
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004769sub fill_from_file_info {
4770 my ($diff, @parents) = @_;
4771
4772 $diff->{'from_file'} = [ ];
4773 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4774 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4775 if ($diff->{'status'}[$i] eq 'R' ||
4776 $diff->{'status'}[$i] eq 'C') {
4777 $diff->{'from_file'}[$i] =
4778 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4779 }
4780 }
4781
4782 return $diff;
4783}
4784
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004785# is current raw difftree line of file deletion
Jakub Narebski90921742007-06-08 13:27:42 +02004786sub is_deleted {
4787 my $diffinfo = shift;
4788
Jakub Narebski4ed4a342008-04-05 21:13:24 +01004789 return $diffinfo->{'to_id'} eq ('0' x 40);
Jakub Narebski90921742007-06-08 13:27:42 +02004790}
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004791
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004792# does patch correspond to [previous] difftree raw line
4793# $diffinfo - hashref of parsed raw diff format
4794# $patchinfo - hashref of parsed patch diff format
4795# (the same keys as in $diffinfo)
4796sub is_patch_split {
4797 my ($diffinfo, $patchinfo) = @_;
4798
4799 return defined $diffinfo && defined $patchinfo
Jakub Narebski9d301452007-11-01 12:38:08 +01004800 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004801}
4802
4803
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004804sub git_difftree_body {
Jakub Narebskied224de2007-05-07 01:10:04 +02004805 my ($difftree, $hash, @parents) = @_;
4806 my ($parent) = $parents[0];
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004807 my $have_blame = gitweb_check_feature('blame');
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004808 print "<div class=\"list_head\">\n";
4809 if ($#{$difftree} > 10) {
4810 print(($#{$difftree} + 1) . " files changed:\n");
4811 }
4812 print "</div>\n";
4813
Jakub Narebskied224de2007-05-07 01:10:04 +02004814 print "<table class=\"" .
4815 (@parents > 1 ? "combined " : "") .
4816 "diff_tree\">\n";
Jakub Narebski47598d72007-06-08 13:24:56 +02004817
4818 # header only for combined diff in 'commitdiff' view
Jakub Narebski3ef408a2007-09-08 21:54:28 +02004819 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
Jakub Narebski47598d72007-06-08 13:24:56 +02004820 if ($has_header) {
4821 # table header
4822 print "<thead><tr>\n" .
4823 "<th></th><th></th>\n"; # filename, patchN link
4824 for (my $i = 0; $i < @parents; $i++) {
4825 my $par = $parents[$i];
4826 print "<th>" .
4827 $cgi->a({-href => href(action=>"commitdiff",
4828 hash=>$hash, hash_parent=>$par),
4829 -title => 'commitdiff to parent number ' .
4830 ($i+1) . ': ' . substr($par,0,7)},
4831 $i+1) .
4832 "&nbsp;</th>\n";
4833 }
4834 print "</tr></thead>\n<tbody>\n";
4835 }
4836
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004837 my $alternate = 1;
Jakub Narebskib4657e72006-08-28 14:48:14 +02004838 my $patchno = 0;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004839 foreach my $line (@{$difftree}) {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004840 my $diff = parsed_difftree_line($line);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004841
4842 if ($alternate) {
4843 print "<tr class=\"dark\">\n";
4844 } else {
4845 print "<tr class=\"light\">\n";
4846 }
4847 $alternate ^= 1;
4848
Jakub Narebski493e01d2007-05-07 01:10:06 +02004849 if (exists $diff->{'nparents'}) { # combined diff
Jakub Narebskied224de2007-05-07 01:10:04 +02004850
Jakub Narebski493e01d2007-05-07 01:10:06 +02004851 fill_from_file_info($diff, @parents)
4852 unless exists $diff->{'from_file'};
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004853
Jakub Narebski90921742007-06-08 13:27:42 +02004854 if (!is_deleted($diff)) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004855 # file exists in the result (child) commit
4856 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02004857 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4858 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004859 hash_base=>$hash),
Jakub Narebski493e01d2007-05-07 01:10:06 +02004860 -class => "list"}, esc_path($diff->{'to_file'})) .
Jakub Narebskied224de2007-05-07 01:10:04 +02004861 "</td>\n";
4862 } else {
4863 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02004864 esc_path($diff->{'to_file'}) .
Jakub Narebskied224de2007-05-07 01:10:04 +02004865 "</td>\n";
4866 }
4867
4868 if ($action eq 'commitdiff') {
4869 # link to patch
4870 $patchno++;
4871 print "<td class=\"link\">" .
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004872 $cgi->a({-href => href(-anchor=>"patch$patchno")},
4873 "patch") .
Jakub Narebskied224de2007-05-07 01:10:04 +02004874 " | " .
4875 "</td>\n";
4876 }
4877
4878 my $has_history = 0;
4879 my $not_deleted = 0;
Jakub Narebski493e01d2007-05-07 01:10:06 +02004880 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004881 my $hash_parent = $parents[$i];
Jakub Narebski493e01d2007-05-07 01:10:06 +02004882 my $from_hash = $diff->{'from_id'}[$i];
4883 my $from_path = $diff->{'from_file'}[$i];
4884 my $status = $diff->{'status'}[$i];
Jakub Narebskied224de2007-05-07 01:10:04 +02004885
4886 $has_history ||= ($status ne 'A');
4887 $not_deleted ||= ($status ne 'D');
4888
Jakub Narebskied224de2007-05-07 01:10:04 +02004889 if ($status eq 'A') {
4890 print "<td class=\"link\" align=\"right\"> | </td>\n";
4891 } elsif ($status eq 'D') {
4892 print "<td class=\"link\">" .
4893 $cgi->a({-href => href(action=>"blob",
4894 hash_base=>$hash,
4895 hash=>$from_hash,
4896 file_name=>$from_path)},
4897 "blob" . ($i+1)) .
4898 " | </td>\n";
4899 } else {
Jakub Narebski493e01d2007-05-07 01:10:06 +02004900 if ($diff->{'to_id'} eq $from_hash) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004901 print "<td class=\"link nochange\">";
4902 } else {
4903 print "<td class=\"link\">";
4904 }
4905 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004906 hash=>$diff->{'to_id'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004907 hash_parent=>$from_hash,
4908 hash_base=>$hash,
4909 hash_parent_base=>$hash_parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004910 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004911 file_parent=>$from_path)},
4912 "diff" . ($i+1)) .
4913 " | </td>\n";
4914 }
4915 }
4916
4917 print "<td class=\"link\">";
4918 if ($not_deleted) {
4919 print $cgi->a({-href => href(action=>"blob",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004920 hash=>$diff->{'to_id'},
4921 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004922 hash_base=>$hash)},
4923 "blob");
4924 print " | " if ($has_history);
4925 }
4926 if ($has_history) {
4927 print $cgi->a({-href => href(action=>"history",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004928 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004929 hash_base=>$hash)},
4930 "history");
4931 }
4932 print "</td>\n";
4933
4934 print "</tr>\n";
4935 next; # instead of 'else' clause, to avoid extra indent
4936 }
4937 # else ordinary diff
4938
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004939 my ($to_mode_oct, $to_mode_str, $to_file_type);
4940 my ($from_mode_oct, $from_mode_str, $from_file_type);
Jakub Narebski493e01d2007-05-07 01:10:06 +02004941 if ($diff->{'to_mode'} ne ('0' x 6)) {
4942 $to_mode_oct = oct $diff->{'to_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004943 if (S_ISREG($to_mode_oct)) { # only for regular file
4944 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004945 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004946 $to_file_type = file_type($diff->{'to_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004947 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004948 if ($diff->{'from_mode'} ne ('0' x 6)) {
4949 $from_mode_oct = oct $diff->{'from_mode'};
Ævar Arnfjörð Bjarmason98885c22011-02-19 15:27:42 +00004950 if (S_ISREG($from_mode_oct)) { # only for regular file
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004951 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4952 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004953 $from_file_type = file_type($diff->{'from_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004954 }
4955
Jakub Narebski493e01d2007-05-07 01:10:06 +02004956 if ($diff->{'status'} eq "A") { # created
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004957 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4958 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4959 $mode_chng .= "]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07004960 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004961 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4962 hash_base=>$hash, file_name=>$diff->{'file'}),
4963 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07004964 print "</td>\n";
4965 print "<td>$mode_chng</td>\n";
4966 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02004967 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02004968 # link to patch
4969 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004970 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4971 "patch") .
4972 " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02004973 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004974 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4975 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski3faa5412007-01-08 02:10:42 +01004976 "blob");
Jakub Narebskib4657e72006-08-28 14:48:14 +02004977 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004978
Jakub Narebski493e01d2007-05-07 01:10:06 +02004979 } elsif ($diff->{'status'} eq "D") { # deleted
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004980 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07004981 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004982 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4983 hash_base=>$parent, file_name=>$diff->{'file'}),
4984 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07004985 print "</td>\n";
4986 print "<td>$mode_chng</td>\n";
4987 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02004988 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02004989 # link to patch
4990 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004991 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4992 "patch") .
4993 " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02004994 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004995 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4996 hash_base=>$parent, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01004997 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08004998 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01004999 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005000 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005001 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005002 }
Jakub Narebskib4657e72006-08-28 14:48:14 +02005003 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005004 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02005005 "history");
Luben Tuikov499faed2006-09-27 17:23:25 -07005006 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005007
Jakub Narebski493e01d2007-05-07 01:10:06 +02005008 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005009 my $mode_chnge = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02005010 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005011 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
Jakub Narebski6e72cf42007-01-03 20:47:24 +01005012 if ($from_file_type ne $to_file_type) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005013 $mode_chnge .= " from $from_file_type to $to_file_type";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005014 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005015 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
5016 if ($from_mode_str && $to_mode_str) {
5017 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
5018 } elsif ($to_mode_str) {
5019 $mode_chnge .= " mode: $to_mode_str";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005020 }
5021 }
5022 $mode_chnge .= "]</span>\n";
5023 }
5024 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02005025 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5026 hash_base=>$hash, file_name=>$diff->{'file'}),
5027 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07005028 print "</td>\n";
5029 print "<td>$mode_chnge</td>\n";
5030 print "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01005031 if ($action eq 'commitdiff') {
5032 # link to patch
5033 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01005034 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
5035 "patch") .
Jakub Narebski241cc592006-10-31 17:36:27 +01005036 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02005037 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01005038 # "commit" view and modified file (not onlu mode changed)
5039 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02005040 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01005041 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005042 file_name=>$diff->{'file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01005043 "diff") .
5044 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005045 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02005046 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5047 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005048 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005049 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01005050 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005051 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005052 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005053 }
Luben Tuikoveb51ec92006-09-27 17:24:49 -07005054 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005055 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02005056 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005057 print "</td>\n";
5058
Jakub Narebski493e01d2007-05-07 01:10:06 +02005059 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005060 my %status_name = ('R' => 'moved', 'C' => 'copied');
Jakub Narebski493e01d2007-05-07 01:10:06 +02005061 my $nstatus = $status_name{$diff->{'status'}};
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005062 my $mode_chng = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02005063 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005064 # mode also for directories, so we cannot use $to_mode_str
5065 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005066 }
5067 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005068 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005069 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
5070 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005071 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
5072 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005073 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
5074 -class => "list"}, esc_path($diff->{'from_file'})) .
5075 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
Luben Tuikov499faed2006-09-27 17:23:25 -07005076 "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01005077 if ($action eq 'commitdiff') {
5078 # link to patch
5079 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01005080 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
5081 "patch") .
Jakub Narebski241cc592006-10-31 17:36:27 +01005082 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02005083 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01005084 # "commit" view and modified file (not only pure rename or copy)
5085 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02005086 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01005087 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005088 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01005089 "diff") .
5090 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005091 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02005092 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5093 hash_base=>$parent, file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005094 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005095 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01005096 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005097 file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005098 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005099 }
Jakub Narebski897d1d22006-11-19 22:51:39 +01005100 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005101 file_name=>$diff->{'to_file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02005102 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005103 print "</td>\n";
5104
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005105 } # we should not encounter Unmerged (U) or Unknown (X) status
5106 print "</tr>\n";
5107 }
Jakub Narebski47598d72007-06-08 13:24:56 +02005108 print "</tbody>" if $has_header;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005109 print "</table>\n";
5110}
5111
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005112# Print context lines and then rem/add lines in a side-by-side manner.
5113sub print_sidebyside_diff_lines {
5114 my ($ctx, $rem, $add) = @_;
5115
5116 # print context block before add/rem block
5117 if (@$ctx) {
5118 print join '',
5119 '<div class="chunk_block ctx">',
5120 '<div class="old">',
5121 @$ctx,
5122 '</div>',
5123 '<div class="new">',
5124 @$ctx,
5125 '</div>',
5126 '</div>';
5127 }
5128
5129 if (!@$add) {
5130 # pure removal
5131 print join '',
5132 '<div class="chunk_block rem">',
5133 '<div class="old">',
5134 @$rem,
5135 '</div>',
5136 '</div>';
5137 } elsif (!@$rem) {
5138 # pure addition
5139 print join '',
5140 '<div class="chunk_block add">',
5141 '<div class="new">',
5142 @$add,
5143 '</div>',
5144 '</div>';
5145 } else {
5146 print join '',
5147 '<div class="chunk_block chg">',
5148 '<div class="old">',
5149 @$rem,
5150 '</div>',
5151 '<div class="new">',
5152 @$add,
5153 '</div>',
5154 '</div>';
5155 }
5156}
5157
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005158# Print context lines and then rem/add lines in inline manner.
5159sub print_inline_diff_lines {
5160 my ($ctx, $rem, $add) = @_;
5161
5162 print @$ctx, @$rem, @$add;
5163}
5164
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005165# Format removed and added line, mark changed part and HTML-format them.
5166# Implementation is based on contrib/diff-highlight
5167sub format_rem_add_lines_pair {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005168 my ($rem, $add, $num_parents) = @_;
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005169
5170 # We need to untabify lines before split()'ing them;
5171 # otherwise offsets would be invalid.
5172 chomp $rem;
5173 chomp $add;
5174 $rem = untabify($rem);
5175 $add = untabify($add);
5176
5177 my @rem = split(//, $rem);
5178 my @add = split(//, $add);
5179 my ($esc_rem, $esc_add);
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005180 # Ignore leading +/- characters for each parent.
5181 my ($prefix_len, $suffix_len) = ($num_parents, 0);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005182 my ($prefix_has_nonspace, $suffix_has_nonspace);
5183
5184 my $shorter = (@rem < @add) ? @rem : @add;
5185 while ($prefix_len < $shorter) {
5186 last if ($rem[$prefix_len] ne $add[$prefix_len]);
5187
5188 $prefix_has_nonspace = 1 if ($rem[$prefix_len] !~ /\s/);
5189 $prefix_len++;
5190 }
5191
5192 while ($prefix_len + $suffix_len < $shorter) {
5193 last if ($rem[-1 - $suffix_len] ne $add[-1 - $suffix_len]);
5194
5195 $suffix_has_nonspace = 1 if ($rem[-1 - $suffix_len] !~ /\s/);
5196 $suffix_len++;
5197 }
5198
5199 # Mark lines that are different from each other, but have some common
5200 # part that isn't whitespace. If lines are completely different, don't
5201 # mark them because that would make output unreadable, especially if
5202 # diff consists of multiple lines.
5203 if ($prefix_has_nonspace || $suffix_has_nonspace) {
5204 $esc_rem = esc_html_hl_regions($rem, 'marked',
5205 [$prefix_len, @rem - $suffix_len], -nbsp=>1);
5206 $esc_add = esc_html_hl_regions($add, 'marked',
5207 [$prefix_len, @add - $suffix_len], -nbsp=>1);
5208 } else {
5209 $esc_rem = esc_html($rem, -nbsp=>1);
5210 $esc_add = esc_html($add, -nbsp=>1);
5211 }
5212
5213 return format_diff_line(\$esc_rem, 'rem'),
5214 format_diff_line(\$esc_add, 'add');
5215}
5216
5217# HTML-format diff context, removed and added lines.
5218sub format_ctx_rem_add_lines {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005219 my ($ctx, $rem, $add, $num_parents) = @_;
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005220 my (@new_ctx, @new_rem, @new_add);
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005221 my $can_highlight = 0;
5222 my $is_combined = ($num_parents > 1);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005223
5224 # Highlight if every removed line has a corresponding added line.
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005225 if (@$add > 0 && @$add == @$rem) {
5226 $can_highlight = 1;
5227
5228 # Highlight lines in combined diff only if the chunk contains
5229 # diff between the same version, e.g.
5230 #
5231 # - a
5232 # - b
5233 # + c
5234 # + d
5235 #
5236 # Otherwise the highlightling would be confusing.
5237 if ($is_combined) {
5238 for (my $i = 0; $i < @$add; $i++) {
5239 my $prefix_rem = substr($rem->[$i], 0, $num_parents);
5240 my $prefix_add = substr($add->[$i], 0, $num_parents);
5241
5242 $prefix_rem =~ s/-/+/g;
5243
5244 if ($prefix_rem ne $prefix_add) {
5245 $can_highlight = 0;
5246 last;
5247 }
5248 }
5249 }
5250 }
5251
5252 if ($can_highlight) {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005253 for (my $i = 0; $i < @$add; $i++) {
5254 my ($line_rem, $line_add) = format_rem_add_lines_pair(
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005255 $rem->[$i], $add->[$i], $num_parents);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005256 push @new_rem, $line_rem;
5257 push @new_add, $line_add;
5258 }
5259 } else {
5260 @new_rem = map { format_diff_line($_, 'rem') } @$rem;
5261 @new_add = map { format_diff_line($_, 'add') } @$add;
5262 }
5263
5264 @new_ctx = map { format_diff_line($_, 'ctx') } @$ctx;
5265
5266 return (\@new_ctx, \@new_rem, \@new_add);
5267}
5268
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005269# Print context lines and then rem/add lines.
5270sub print_diff_lines {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005271 my ($ctx, $rem, $add, $diff_style, $num_parents) = @_;
5272 my $is_combined = $num_parents > 1;
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005273
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005274 ($ctx, $rem, $add) = format_ctx_rem_add_lines($ctx, $rem, $add,
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005275 $num_parents);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005276
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005277 if ($diff_style eq 'sidebyside' && !$is_combined) {
5278 print_sidebyside_diff_lines($ctx, $rem, $add);
5279 } else {
5280 # default 'inline' style and unknown styles
5281 print_inline_diff_lines($ctx, $rem, $add);
5282 }
5283}
5284
5285sub print_diff_chunk {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005286 my ($diff_style, $num_parents, $from, $to, @chunk) = @_;
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005287 my (@ctx, @rem, @add);
5288
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005289 # The class of the previous line.
5290 my $prev_class = '';
5291
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005292 return unless @chunk;
5293
5294 # incomplete last line might be among removed or added lines,
5295 # or both, or among context lines: find which
5296 for (my $i = 1; $i < @chunk; $i++) {
5297 if ($chunk[$i][0] eq 'incomplete') {
5298 $chunk[$i][0] = $chunk[$i-1][0];
5299 }
5300 }
5301
5302 # guardian
5303 push @chunk, ["", ""];
5304
5305 foreach my $line_info (@chunk) {
5306 my ($class, $line) = @$line_info;
5307
5308 # print chunk headers
5309 if ($class && $class eq 'chunk_header') {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005310 print format_diff_line($line, $class, $from, $to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005311 next;
5312 }
5313
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005314 ## print from accumulator when have some add/rem lines or end
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005315 # of chunk (flush context lines), or when have add and rem
5316 # lines and new block is reached (otherwise add/rem lines could
5317 # be reordered)
5318 if (!$class || ((@rem || @add) && $class eq 'ctx') ||
5319 (@rem && @add && $class ne $prev_class)) {
5320 print_diff_lines(\@ctx, \@rem, \@add,
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005321 $diff_style, $num_parents);
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005322 @ctx = @rem = @add = ();
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005323 }
5324
5325 ## adding lines to accumulator
5326 # guardian value
5327 last unless $line;
5328 # rem, add or change
5329 if ($class eq 'rem') {
5330 push @rem, $line;
5331 } elsif ($class eq 'add') {
5332 push @add, $line;
5333 }
5334 # context line
5335 if ($class eq 'ctx') {
5336 push @ctx, $line;
5337 }
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005338
5339 $prev_class = $class;
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005340 }
5341}
5342
Jakub Narebskieee08902006-08-24 00:15:14 +02005343sub git_patchset_body {
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005344 my ($fd, $diff_style, $difftree, $hash, @hash_parents) = @_;
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02005345 my ($hash_parent) = $hash_parents[0];
Jakub Narebskieee08902006-08-24 00:15:14 +02005346
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005347 my $is_combined = (@hash_parents > 1);
Jakub Narebskieee08902006-08-24 00:15:14 +02005348 my $patch_idx = 0;
Martin Koegler4280cde2007-04-22 22:49:25 -07005349 my $patch_number = 0;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005350 my $patch_line;
Jakub Narebskife875852006-08-25 20:59:39 +02005351 my $diffinfo;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005352 my $to_name;
Jakub Narebski744d0ac2006-11-08 17:59:41 +01005353 my (%from, %to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005354 my @chunk; # for side-by-side diff
Jakub Narebskieee08902006-08-24 00:15:14 +02005355
5356 print "<div class=\"patchset\">\n";
5357
Jakub Narebski6d55f052006-11-18 23:35:39 +01005358 # skip to first patch
5359 while ($patch_line = <$fd>) {
Jakub Narebski157e43b2006-08-24 19:34:36 +02005360 chomp $patch_line;
Jakub Narebskieee08902006-08-24 00:15:14 +02005361
Jakub Narebski6d55f052006-11-18 23:35:39 +01005362 last if ($patch_line =~ m/^diff /);
5363 }
5364
5365 PATCH:
5366 while ($patch_line) {
Jakub Narebski6d55f052006-11-18 23:35:39 +01005367
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005368 # parse "git diff" header line
5369 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
5370 # $1 is from_name, which we do not use
5371 $to_name = unquote($2);
5372 $to_name =~ s!^b/!!;
5373 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
5374 # $1 is 'cc' or 'combined', which we do not use
5375 $to_name = unquote($2);
5376 } else {
5377 $to_name = undef;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005378 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01005379
5380 # check if current patch belong to current raw line
5381 # and parse raw git-diff line if needed
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005382 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
Jakub Narebski22065372007-05-12 12:42:32 +02005383 # this is continuation of a split patch
Jakub Narebski6d55f052006-11-18 23:35:39 +01005384 print "<div class=\"patch cont\">\n";
5385 } else {
5386 # advance raw git-diff output if needed
5387 $patch_idx++ if defined $diffinfo;
Jakub Narebskieee08902006-08-24 00:15:14 +02005388
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005389 # read and prepare patch information
5390 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5391
Jakub Narebskicd030c32007-06-08 13:33:28 +02005392 # compact combined diff output can have some patches skipped
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005393 # find which patch (using pathname of result) we are at now;
5394 if ($is_combined) {
5395 while ($to_name ne $diffinfo->{'to_file'}) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02005396 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5397 format_diff_cc_simplified($diffinfo, @hash_parents) .
5398 "</div>\n"; # class="patch"
5399
5400 $patch_idx++;
5401 $patch_number++;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005402
5403 last if $patch_idx > $#$difftree;
5404 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02005405 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005406 }
Jakub Narebski711fa742007-09-08 21:49:11 +02005407
Jakub Narebski90921742007-06-08 13:27:42 +02005408 # modifies %from, %to hashes
5409 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
Jakub Narebski5f855052007-05-17 22:54:28 +02005410
Jakub Narebski6d55f052006-11-18 23:35:39 +01005411 # this is first patch for raw difftree line with $patch_idx index
5412 # we index @$difftree array from 0, but number patches from 1
5413 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
Jakub Narebski744d0ac2006-11-08 17:59:41 +01005414 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005415
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005416 # git diff header
5417 #assert($patch_line =~ m/^diff /) if DEBUG;
5418 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
5419 $patch_number++;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005420 # print "git diff" header
Jakub Narebski90921742007-06-08 13:27:42 +02005421 print format_git_diff_header_line($patch_line, $diffinfo,
5422 \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02005423
Jakub Narebski6d55f052006-11-18 23:35:39 +01005424 # print extended diff header
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005425 print "<div class=\"diff extended_header\">\n";
Jakub Narebski6d55f052006-11-18 23:35:39 +01005426 EXTENDED_HEADER:
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005427 while ($patch_line = <$fd>) {
5428 chomp $patch_line;
5429
5430 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
5431
Jakub Narebski90921742007-06-08 13:27:42 +02005432 print format_extended_diff_header_line($patch_line, $diffinfo,
5433 \%from, \%to);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005434 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005435 print "</div>\n"; # class="diff extended_header"
Jakub Narebskie4e4f822006-08-25 21:04:13 +02005436
Jakub Narebski6d55f052006-11-18 23:35:39 +01005437 # from-file/to-file diff header
Jakub Narebski0bdb28c2007-01-10 00:07:43 +01005438 if (! $patch_line) {
5439 print "</div>\n"; # class="patch"
5440 last PATCH;
5441 }
Jakub Narebski66399ef2007-01-07 02:52:25 +01005442 next PATCH if ($patch_line =~ m/^diff /);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005443 #assert($patch_line =~ m/^---/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005444
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005445 my $last_patch_line = $patch_line;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005446 $patch_line = <$fd>;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005447 chomp $patch_line;
Jakub Narebski90921742007-06-08 13:27:42 +02005448 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005449
Jakub Narebski90921742007-06-08 13:27:42 +02005450 print format_diff_from_to_header($last_patch_line, $patch_line,
Jakub Narebski91af4ce2007-06-08 13:32:44 +02005451 $diffinfo, \%from, \%to,
5452 @hash_parents);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005453
5454 # the patch itself
5455 LINE:
5456 while ($patch_line = <$fd>) {
5457 chomp $patch_line;
5458
5459 next PATCH if ($patch_line =~ m/^diff /);
5460
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02005461 my $class = diff_line_class($patch_line, \%from, \%to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005462
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005463 if ($class eq 'chunk_header') {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005464 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005465 @chunk = ();
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005466 }
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005467
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02005468 push @chunk, [ $class, $patch_line ];
Jakub Narebskieee08902006-08-24 00:15:14 +02005469 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005470
Jakub Narebski6d55f052006-11-18 23:35:39 +01005471 } continue {
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005472 if (@chunk) {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005473 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005474 @chunk = ();
5475 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01005476 print "</div>\n"; # class="patch"
Jakub Narebskieee08902006-08-24 00:15:14 +02005477 }
Jakub Narebskid26c4262007-05-17 00:05:55 +02005478
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02005479 # for compact combined (--cc) format, with chunk and patch simplification
5480 # the patchset might be empty, but there might be unprocessed raw lines
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005481 for (++$patch_idx if $patch_number > 0;
Jakub Narebskicd030c32007-06-08 13:33:28 +02005482 $patch_idx < @$difftree;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005483 ++$patch_idx) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02005484 # read and prepare patch information
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005485 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02005486
5487 # generate anchor for "patch" links in difftree / whatchanged part
5488 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5489 format_diff_cc_simplified($diffinfo, @hash_parents) .
5490 "</div>\n"; # class="patch"
5491
5492 $patch_number++;
5493 }
5494
Jakub Narebskid26c4262007-05-17 00:05:55 +02005495 if ($patch_number == 0) {
5496 if (@hash_parents > 1) {
5497 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
5498 } else {
5499 print "<div class=\"diff nodifferences\">No differences found</div>\n";
5500 }
5501 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005502
5503 print "</div>\n"; # class="patchset"
5504}
5505
5506# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5507
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005508sub git_project_search_form {
Jakub Narebskib22939a2012-03-02 23:50:01 +01005509 my ($searchtext, $search_use_regexp) = @_;
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005510
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005511 my $limit = '';
5512 if ($project_filter) {
5513 $limit = " in '$project_filter/'";
5514 }
5515
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005516 print "<div class=\"projsearch\">\n";
Roland Mas4750f4b2014-10-16 08:54:47 +02005517 print $cgi->start_form(-method => 'get', -action => $my_uri) .
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005518 $cgi->hidden(-name => 'a', -value => 'project_list') . "\n";
5519 print $cgi->hidden(-name => 'pf', -value => $project_filter). "\n"
5520 if (defined $project_filter);
5521 print $cgi->textfield(-name => 's', -value => $searchtext,
5522 -title => "Search project by name and description$limit",
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005523 -size => 60) . "\n" .
5524 "<span title=\"Extended regular expression\">" .
5525 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
5526 -checked => $search_use_regexp) .
5527 "</span>\n" .
5528 $cgi->submit(-name => 'btnS', -value => 'Search') .
5529 $cgi->end_form() . "\n" .
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005530 $cgi->a({-href => href(project => undef, searchtext => undef,
5531 project_filter => $project_filter)},
5532 esc_html("List all projects$limit")) . "<br />\n";
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005533 print "</div>\n";
5534}
5535
Jakub Narebski14b289b2012-02-23 16:54:46 +01005536# entry for given @keys needs filling if at least one of keys in list
5537# is not present in %$project_info
5538sub project_info_needs_filling {
5539 my ($project_info, @keys) = @_;
5540
5541 # return List::MoreUtils::any { !exists $project_info->{$_} } @keys;
5542 foreach my $key (@keys) {
5543 if (!exists $project_info->{$key}) {
5544 return 1;
5545 }
5546 }
5547 return;
5548}
5549
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005550# fills project list info (age, description, owner, category, forks, etc.)
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005551# for each project in the list, removing invalid projects from
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005552# returned list, or fill only specified info.
5553#
5554# Invalid projects are removed from the returned list if and only if you
5555# ask 'age' or 'age_string' to be filled, because they are the only fields
5556# that run unconditionally git command that requires repository, and
5557# therefore do always check if project repository is invalid.
5558#
5559# USAGE:
5560# * fill_project_list_info(\@project_list, 'descr_long', 'ctags')
5561# ensures that 'descr_long' and 'ctags' fields are filled
5562# * @project_list = fill_project_list_info(\@project_list)
5563# ensures that all fields are filled (and invalid projects removed)
5564#
Jakub Narebski69913412008-06-10 19:21:01 +02005565# NOTE: modifies $projlist, but does not remove entries from it
5566sub fill_project_list_info {
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005567 my ($projlist, @wanted_keys) = @_;
Petr Baudise30496d2006-10-24 05:33:17 +02005568 my @projects;
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005569 my $filter_set = sub { return @_; };
5570 if (@wanted_keys) {
5571 my %wanted_keys = map { $_ => 1 } @wanted_keys;
5572 $filter_set = sub { return grep { $wanted_keys{$_} } @_; };
5573 }
Jakub Narebski69913412008-06-10 19:21:01 +02005574
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005575 my $show_ctags = gitweb_check_feature('ctags');
Jakub Narebski69913412008-06-10 19:21:01 +02005576 PROJECT:
Petr Baudise30496d2006-10-24 05:33:17 +02005577 foreach my $pr (@$projlist) {
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005578 if (project_info_needs_filling($pr, $filter_set->('age', 'age_string'))) {
Jakub Narebski14b289b2012-02-23 16:54:46 +01005579 my (@activity) = git_get_last_activity($pr->{'path'});
5580 unless (@activity) {
5581 next PROJECT;
5582 }
5583 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
Petr Baudise30496d2006-10-24 05:33:17 +02005584 }
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005585 if (project_info_needs_filling($pr, $filter_set->('descr', 'descr_long'))) {
Petr Baudise30496d2006-10-24 05:33:17 +02005586 my $descr = git_get_project_description($pr->{'path'}) || "";
Jakub Narebski69913412008-06-10 19:21:01 +02005587 $descr = to_utf8($descr);
5588 $pr->{'descr_long'} = $descr;
Michael Hendricks55feb122007-07-04 18:36:48 -06005589 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
Petr Baudise30496d2006-10-24 05:33:17 +02005590 }
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005591 if (project_info_needs_filling($pr, $filter_set->('owner'))) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02005592 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
Petr Baudise30496d2006-10-24 05:33:17 +02005593 }
Jakub Narebski14b289b2012-02-23 16:54:46 +01005594 if ($show_ctags &&
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005595 project_info_needs_filling($pr, $filter_set->('ctags'))) {
Jakub Narebski12b14432011-04-29 19:51:56 +02005596 $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
Petr Baudise30496d2006-10-24 05:33:17 +02005597 }
Jakub Narebski14b289b2012-02-23 16:54:46 +01005598 if ($projects_list_group_categories &&
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005599 project_info_needs_filling($pr, $filter_set->('category'))) {
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005600 my $cat = git_get_project_category($pr->{'path'}) ||
5601 $project_list_default_category;
5602 $pr->{'category'} = to_utf8($cat);
5603 }
5604
Petr Baudise30496d2006-10-24 05:33:17 +02005605 push @projects, $pr;
5606 }
5607
Jakub Narebski69913412008-06-10 19:21:01 +02005608 return @projects;
5609}
5610
Jakub Narebski12b14432011-04-29 19:51:56 +02005611sub sort_projects_list {
5612 my ($projlist, $order) = @_;
Jakub Narebski12b14432011-04-29 19:51:56 +02005613
Matthew Daley28dae182012-12-11 23:56:07 +13005614 sub order_str {
5615 my $key = shift;
5616 return sub { $a->{$key} cmp $b->{$key} };
Jakub Narebski12b14432011-04-29 19:51:56 +02005617 }
5618
Matthew Daley28dae182012-12-11 23:56:07 +13005619 sub order_num_then_undef {
5620 my $key = shift;
5621 return sub {
5622 defined $a->{$key} ?
5623 (defined $b->{$key} ? $a->{$key} <=> $b->{$key} : -1) :
5624 (defined $b->{$key} ? 1 : 0)
5625 };
5626 }
5627
5628 my %orderings = (
5629 project => order_str('path'),
5630 descr => order_str('descr_long'),
5631 owner => order_str('owner'),
5632 age => order_num_then_undef('age'),
5633 );
5634
5635 my $ordering = $orderings{$order};
5636 return defined $ordering ? sort $ordering @$projlist : @$projlist;
Jakub Narebski12b14432011-04-29 19:51:56 +02005637}
5638
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005639# returns a hash of categories, containing the list of project
5640# belonging to each category
5641sub build_projlist_by_category {
5642 my ($projlist, $from, $to) = @_;
5643 my %categories;
5644
5645 $from = 0 unless defined $from;
5646 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5647
5648 for (my $i = $from; $i <= $to; $i++) {
5649 my $pr = $projlist->[$i];
5650 push @{$categories{ $pr->{'category'} }}, $pr;
5651 }
5652
5653 return wantarray ? %categories : \%categories;
5654}
5655
Petr Baudis6b28da62008-09-25 18:48:37 +02005656# print 'sort by' <th> element, generating 'sort by $name' replay link
5657# if that order is not selected
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005658sub print_sort_th {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005659 print format_sort_th(@_);
5660}
5661
5662sub format_sort_th {
Petr Baudis6b28da62008-09-25 18:48:37 +02005663 my ($name, $order, $header) = @_;
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005664 my $sort_th = "";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005665 $header ||= ucfirst($name);
5666
5667 if ($order eq $name) {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005668 $sort_th .= "<th>$header</th>\n";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005669 } else {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005670 $sort_th .= "<th>" .
5671 $cgi->a({-href => href(-replay=>1, order=>$name),
5672 -class => "header"}, $header) .
5673 "</th>\n";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005674 }
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005675
5676 return $sort_th;
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005677}
5678
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005679sub git_project_list_rows {
5680 my ($projlist, $from, $to, $check_forks) = @_;
5681
5682 $from = 0 unless defined $from;
5683 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5684
5685 my $alternate = 1;
5686 for (my $i = $from; $i <= $to; $i++) {
5687 my $pr = $projlist->[$i];
5688
5689 if ($alternate) {
5690 print "<tr class=\"dark\">\n";
5691 } else {
5692 print "<tr class=\"light\">\n";
5693 }
5694 $alternate ^= 1;
5695
5696 if ($check_forks) {
5697 print "<td>";
5698 if ($pr->{'forks'}) {
5699 my $nforks = scalar @{$pr->{'forks'}};
5700 if ($nforks > 0) {
5701 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5702 -title => "$nforks forks"}, "+");
5703 } else {
5704 print $cgi->span({-title => "$nforks forks"}, "+");
5705 }
5706 }
5707 print "</td>\n";
5708 }
5709 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
Jakub Narebski07a40062012-02-27 02:55:20 +01005710 -class => "list"},
5711 esc_html_match_hl($pr->{'path'}, $search_regexp)) .
5712 "</td>\n" .
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005713 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005714 -class => "list",
Jakub Narebskie607b792012-02-27 02:55:22 +01005715 -title => $pr->{'descr_long'}},
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005716 $search_regexp
Jakub Narebskie607b792012-02-27 02:55:22 +01005717 ? esc_html_match_hl_chopped($pr->{'descr_long'},
5718 $pr->{'descr'}, $search_regexp)
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005719 : esc_html($pr->{'descr'})) .
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005720 "</td>\n";
5721 unless ($omit_owner) {
5722 print "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5723 }
Kacper Kornet5710be42012-04-24 19:39:15 +02005724 unless ($omit_age_column) {
5725 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5726 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n";
5727 }
5728 print"<td class=\"link\">" .
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005729 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
5730 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5731 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5732 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5733 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5734 "</td>\n" .
5735 "</tr>\n";
5736 }
5737}
5738
Jakub Narebski69913412008-06-10 19:21:01 +02005739sub git_project_list_body {
Petr Baudis42326112008-10-02 17:17:01 +02005740 # actually uses global variable $project
Jakub Narebski69913412008-06-10 19:21:01 +02005741 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
Jakub Narebski12b14432011-04-29 19:51:56 +02005742 my @projects = @$projlist;
Jakub Narebski69913412008-06-10 19:21:01 +02005743
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005744 my $check_forks = gitweb_check_feature('forks');
Jakub Narebski12b14432011-04-29 19:51:56 +02005745 my $show_ctags = gitweb_check_feature('ctags');
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01005746 my $tagfilter = $show_ctags ? $input_params{'ctag'} : undef;
Jakub Narebski12b14432011-04-29 19:51:56 +02005747 $check_forks = undef
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005748 if ($tagfilter || $search_regexp);
Jakub Narebski12b14432011-04-29 19:51:56 +02005749
5750 # filtering out forks before filling info allows to do less work
5751 @projects = filter_forks_from_projects_list(\@projects)
5752 if ($check_forks);
Jakub Narebski07b257f2012-02-23 16:54:48 +01005753 # search_projects_list pre-fills required info
Jakub Narebski12b14432011-04-29 19:51:56 +02005754 @projects = search_projects_list(\@projects,
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005755 'search_regexp' => $search_regexp,
Jakub Narebski12b14432011-04-29 19:51:56 +02005756 'tagfilter' => $tagfilter)
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005757 if ($tagfilter || $search_regexp);
Jakub Narebski07b257f2012-02-23 16:54:48 +01005758 # fill the rest
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005759 my @all_fields = ('descr', 'descr_long', 'ctags', 'category');
5760 push @all_fields, ('age', 'age_string') unless($omit_age_column);
5761 push @all_fields, 'owner' unless($omit_owner);
Kacper Kornet5710be42012-04-24 19:39:15 +02005762 @projects = fill_project_list_info(\@projects, @all_fields);
Jakub Narebski69913412008-06-10 19:21:01 +02005763
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02005764 $order ||= $default_projects_order;
Petr Baudise30496d2006-10-24 05:33:17 +02005765 $from = 0 unless defined $from;
5766 $to = $#projects if (!defined $to || $#projects < $to);
5767
Jakub Narebski12b14432011-04-29 19:51:56 +02005768 # short circuit
5769 if ($from > $to) {
5770 print "<center>\n".
5771 "<b>No such projects found</b><br />\n".
5772 "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
5773 "</center>\n<br />\n";
5774 return;
Petr Baudis6b28da62008-09-25 18:48:37 +02005775 }
5776
Jakub Narebski12b14432011-04-29 19:51:56 +02005777 @projects = sort_projects_list(\@projects, $order);
5778
Petr Baudisaed93de2008-10-02 17:13:02 +02005779 if ($show_ctags) {
Jakub Narebski0368c492011-04-29 19:51:57 +02005780 my $ctags = git_gather_all_ctags(\@projects);
5781 my $cloud = git_populate_project_tagcloud($ctags);
Petr Baudisaed93de2008-10-02 17:13:02 +02005782 print git_show_project_tagcloud($cloud, 64);
5783 }
5784
Petr Baudise30496d2006-10-24 05:33:17 +02005785 print "<table class=\"project_list\">\n";
5786 unless ($no_header) {
5787 print "<tr>\n";
5788 if ($check_forks) {
5789 print "<th></th>\n";
5790 }
Petr Baudis6b28da62008-09-25 18:48:37 +02005791 print_sort_th('project', $order, 'Project');
5792 print_sort_th('descr', $order, 'Description');
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005793 print_sort_th('owner', $order, 'Owner') unless $omit_owner;
Kacper Kornet5710be42012-04-24 19:39:15 +02005794 print_sort_th('age', $order, 'Last Change') unless $omit_age_column;
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005795 print "<th></th>\n" . # for links
Petr Baudise30496d2006-10-24 05:33:17 +02005796 "</tr>\n";
5797 }
Petr Baudis42326112008-10-02 17:17:01 +02005798
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005799 if ($projects_list_group_categories) {
5800 # only display categories with projects in the $from-$to window
5801 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5802 my %categories = build_projlist_by_category(\@projects, $from, $to);
5803 foreach my $cat (sort keys %categories) {
5804 unless ($cat eq "") {
5805 print "<tr>\n";
5806 if ($check_forks) {
5807 print "<td></td>\n";
Jakub Narebski12b14432011-04-29 19:51:56 +02005808 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005809 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5810 print "</tr>\n";
Petr Baudise30496d2006-10-24 05:33:17 +02005811 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005812
5813 git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
Petr Baudise30496d2006-10-24 05:33:17 +02005814 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005815 } else {
5816 git_project_list_rows(\@projects, $from, $to, $check_forks);
Petr Baudise30496d2006-10-24 05:33:17 +02005817 }
Jakub Narebski12b14432011-04-29 19:51:56 +02005818
Petr Baudise30496d2006-10-24 05:33:17 +02005819 if (defined $extra) {
5820 print "<tr>\n";
5821 if ($check_forks) {
5822 print "<td></td>\n";
5823 }
5824 print "<td colspan=\"5\">$extra</td>\n" .
5825 "</tr>\n";
5826 }
5827 print "</table>\n";
5828}
5829
Jakub Narebski42671ca2009-11-13 02:02:12 +01005830sub git_log_body {
5831 # uses global variable $project
5832 my ($commitlist, $from, $to, $refs, $extra) = @_;
5833
5834 $from = 0 unless defined $from;
5835 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5836
5837 for (my $i = 0; $i <= $to; $i++) {
5838 my %co = %{$commitlist->[$i]};
5839 next if !%co;
5840 my $commit = $co{'id'};
5841 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski42671ca2009-11-13 02:02:12 +01005842 git_print_header_div('commit',
5843 "<span class=\"age\">$co{'age_string'}</span>" .
5844 esc_html($co{'title'}) . $ref,
5845 $commit);
5846 print "<div class=\"title_text\">\n" .
5847 "<div class=\"log_link\">\n" .
5848 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5849 " | " .
5850 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5851 " | " .
5852 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5853 "<br/>\n" .
5854 "</div>\n";
5855 git_print_authorship(\%co, -tag => 'span');
5856 print "<br/>\n</div>\n";
5857
5858 print "<div class=\"log_body\">\n";
5859 git_print_log($co{'comment'}, -final_empty_line=> 1);
5860 print "</div>\n";
5861 }
5862 if ($extra) {
5863 print "<div class=\"page_nav\">\n";
5864 print "$extra\n";
5865 print "</div>\n";
5866 }
5867}
5868
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005869sub git_shortlog_body {
5870 # uses global variable $project
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005871 my ($commitlist, $from, $to, $refs, $extra) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05305872
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005873 $from = 0 unless defined $from;
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005874 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005875
Jakub Narebski591ebf62007-11-19 14:16:11 +01005876 print "<table class=\"shortlog\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005877 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005878 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005879 my %co = %{$commitlist->[$i]};
5880 my $commit = $co{'id'};
Jakub Narebski847e01f2006-08-14 02:05:47 +02005881 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005882 if ($alternate) {
5883 print "<tr class=\"dark\">\n";
5884 } else {
5885 print "<tr class=\"light\">\n";
5886 }
5887 $alternate ^= 1;
5888 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5889 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02005890 format_author_html('td', \%co, 10) . "<td>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02005891 print format_subject_html($co{'title'}, $co{'title_short'},
5892 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005893 print "</td>\n" .
5894 "<td class=\"link\">" .
Petr Baudis4777b012006-10-24 05:36:10 +02005895 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
Petr Baudis35749ae2006-09-22 03:19:44 +02005896 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
Petr Baudis55ff35c2006-10-06 15:57:52 +02005897 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02005898 my $snapshot_links = format_snapshot_links($commit);
5899 if (defined $snapshot_links) {
5900 print " | " . $snapshot_links;
Petr Baudis55ff35c2006-10-06 15:57:52 +02005901 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305902 print "</td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005903 "</tr>\n";
5904 }
5905 if (defined $extra) {
5906 print "<tr>\n" .
5907 "<td colspan=\"4\">$extra</td>\n" .
5908 "</tr>\n";
5909 }
5910 print "</table>\n";
5911}
5912
Jakub Narebski581860e2006-08-14 02:09:08 +02005913sub git_history_body {
5914 # Warning: assumes constant type (blob or tree) during history
Jakub Narebski69ca37d2009-11-13 02:02:14 +01005915 my ($commitlist, $from, $to, $refs, $extra,
5916 $file_name, $file_hash, $ftype) = @_;
Jakub Narebski8be68352006-09-11 00:36:04 +02005917
5918 $from = 0 unless defined $from;
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005919 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
Jakub Narebski581860e2006-08-14 02:09:08 +02005920
Jakub Narebski591ebf62007-11-19 14:16:11 +01005921 print "<table class=\"history\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005922 my $alternate = 1;
Jakub Narebski8be68352006-09-11 00:36:04 +02005923 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005924 my %co = %{$commitlist->[$i]};
Jakub Narebski581860e2006-08-14 02:09:08 +02005925 if (!%co) {
5926 next;
5927 }
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005928 my $commit = $co{'id'};
Jakub Narebski581860e2006-08-14 02:09:08 +02005929
5930 my $ref = format_ref_marker($refs, $commit);
5931
5932 if ($alternate) {
5933 print "<tr class=\"dark\">\n";
5934 } else {
5935 print "<tr class=\"light\">\n";
5936 }
5937 $alternate ^= 1;
5938 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02005939 # shortlog: format_author_html('td', \%co, 10)
5940 format_author_html('td', \%co, 15, 3) . "<td>";
Jakub Narebski581860e2006-08-14 02:09:08 +02005941 # originally git_history used chop_str($co{'title'}, 50)
Jakub Narebski952c65f2006-08-22 16:52:50 +02005942 print format_subject_html($co{'title'}, $co{'title_short'},
5943 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski581860e2006-08-14 02:09:08 +02005944 print "</td>\n" .
5945 "<td class=\"link\">" .
Luben Tuikov6d81c5a2006-09-28 17:21:07 -07005946 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
5947 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
Jakub Narebski581860e2006-08-14 02:09:08 +02005948
5949 if ($ftype eq 'blob') {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01005950 my $blob_current = $file_hash;
Jakub Narebski581860e2006-08-14 02:09:08 +02005951 my $blob_parent = git_get_hash_by_path($commit, $file_name);
5952 if (defined $blob_current && defined $blob_parent &&
5953 $blob_current ne $blob_parent) {
5954 print " | " .
Jakub Narebski420e92f2006-08-24 23:53:54 +02005955 $cgi->a({-href => href(action=>"blobdiff",
5956 hash=>$blob_current, hash_parent=>$blob_parent,
5957 hash_base=>$hash_base, hash_parent_base=>$commit,
5958 file_name=>$file_name)},
Jakub Narebski581860e2006-08-14 02:09:08 +02005959 "diff to current");
5960 }
5961 }
5962 print "</td>\n" .
5963 "</tr>\n";
5964 }
5965 if (defined $extra) {
5966 print "<tr>\n" .
5967 "<td colspan=\"4\">$extra</td>\n" .
5968 "</tr>\n";
5969 }
5970 print "</table>\n";
5971}
5972
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005973sub git_tags_body {
5974 # uses global variable $project
5975 my ($taglist, $from, $to, $extra) = @_;
5976 $from = 0 unless defined $from;
5977 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5978
Jakub Narebski591ebf62007-11-19 14:16:11 +01005979 print "<table class=\"tags\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005980 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005981 for (my $i = $from; $i <= $to; $i++) {
5982 my $entry = $taglist->[$i];
5983 my %tag = %$entry;
Jakub Narebskicd146402006-11-02 20:23:11 +01005984 my $comment = $tag{'subject'};
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005985 my $comment_short;
5986 if (defined $comment) {
5987 $comment_short = chop_str($comment, 30, 5);
5988 }
5989 if ($alternate) {
5990 print "<tr class=\"dark\">\n";
5991 } else {
5992 print "<tr class=\"light\">\n";
5993 }
5994 $alternate ^= 1;
Jakub Narebski27dd1a82007-01-03 22:54:29 +01005995 if (defined $tag{'age'}) {
5996 print "<td><i>$tag{'age'}</i></td>\n";
5997 } else {
5998 print "<td></td>\n";
5999 }
6000 print "<td>" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006001 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02006002 -class => "list name"}, esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006003 "</td>\n" .
6004 "<td>";
6005 if (defined $comment) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02006006 print format_subject_html($comment, $comment_short,
6007 href(action=>"tag", hash=>$tag{'id'}));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006008 }
6009 print "</td>\n" .
6010 "<td class=\"selflink\">";
6011 if ($tag{'type'} eq "tag") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006012 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006013 } else {
6014 print "&nbsp;";
6015 }
6016 print "</td>\n" .
6017 "<td class=\"link\">" . " | " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006018 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006019 if ($tag{'reftype'} eq "commit") {
Jakub Narebskibf901f82007-12-15 15:40:28 +01006020 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
6021 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006022 } elsif ($tag{'reftype'} eq "blob") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006023 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006024 }
6025 print "</td>\n" .
6026 "</tr>";
6027 }
6028 if (defined $extra) {
6029 print "<tr>\n" .
6030 "<td colspan=\"5\">$extra</td>\n" .
6031 "</tr>\n";
6032 }
6033 print "</table>\n";
6034}
6035
6036sub git_heads_body {
6037 # uses global variable $project
Jakub Narebskifd49e562012-02-15 16:36:41 +01006038 my ($headlist, $head_at, $from, $to, $extra) = @_;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006039 $from = 0 unless defined $from;
Jakub Narebski120ddde2006-09-19 14:33:22 +02006040 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006041
Jakub Narebski591ebf62007-11-19 14:16:11 +01006042 print "<table class=\"heads\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07006043 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006044 for (my $i = $from; $i <= $to; $i++) {
Jakub Narebski120ddde2006-09-19 14:33:22 +02006045 my $entry = $headlist->[$i];
Jakub Narebskicd146402006-11-02 20:23:11 +01006046 my %ref = %$entry;
Jakub Narebskifd49e562012-02-15 16:36:41 +01006047 my $curr = defined $head_at && $ref{'id'} eq $head_at;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006048 if ($alternate) {
6049 print "<tr class=\"dark\">\n";
6050 } else {
6051 print "<tr class=\"light\">\n";
6052 }
6053 $alternate ^= 1;
Jakub Narebskicd146402006-11-02 20:23:11 +01006054 print "<td><i>$ref{'age'}</i></td>\n" .
6055 ($curr ? "<td class=\"current_head\">" : "<td>") .
Jakub Narebskibf901f82007-12-15 15:40:28 +01006056 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
Jakub Narebskicd146402006-11-02 20:23:11 +01006057 -class => "list name"},esc_html($ref{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006058 "</td>\n" .
6059 "<td class=\"link\">" .
Jakub Narebskibf901f82007-12-15 15:40:28 +01006060 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
6061 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
Giuseppe Bilotta9e70e152010-11-11 13:26:08 +01006062 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006063 "</td>\n" .
6064 "</tr>";
6065 }
6066 if (defined $extra) {
6067 print "<tr>\n" .
6068 "<td colspan=\"3\">$extra</td>\n" .
6069 "</tr>\n";
6070 }
6071 print "</table>\n";
6072}
6073
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006074# Display a single remote block
6075sub git_remote_block {
6076 my ($remote, $rdata, $limit, $head) = @_;
6077
6078 my $heads = $rdata->{'heads'};
6079 my $fetch = $rdata->{'fetch'};
6080 my $push = $rdata->{'push'};
6081
6082 my $urls_table = "<table class=\"projects_list\">\n" ;
6083
6084 if (defined $fetch) {
6085 if ($fetch eq $push) {
6086 $urls_table .= format_repo_url("URL", $fetch);
6087 } else {
6088 $urls_table .= format_repo_url("Fetch URL", $fetch);
6089 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
6090 }
6091 } elsif (defined $push) {
6092 $urls_table .= format_repo_url("Push URL", $push);
6093 } else {
6094 $urls_table .= format_repo_url("", "No remote URL");
6095 }
6096
6097 $urls_table .= "</table>\n";
6098
6099 my $dots;
6100 if (defined $limit && $limit < @$heads) {
6101 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
6102 }
6103
6104 print $urls_table;
6105 git_heads_body($heads, $head, 0, $limit, $dots);
6106}
6107
6108# Display a list of remote names with the respective fetch and push URLs
6109sub git_remotes_list {
6110 my ($remotedata, $limit) = @_;
6111 print "<table class=\"heads\">\n";
6112 my $alternate = 1;
6113 my @remotes = sort keys %$remotedata;
6114
6115 my $limited = $limit && $limit < @remotes;
6116
6117 $#remotes = $limit - 1 if $limited;
6118
6119 while (my $remote = shift @remotes) {
6120 my $rdata = $remotedata->{$remote};
6121 my $fetch = $rdata->{'fetch'};
6122 my $push = $rdata->{'push'};
6123 if ($alternate) {
6124 print "<tr class=\"dark\">\n";
6125 } else {
6126 print "<tr class=\"light\">\n";
6127 }
6128 $alternate ^= 1;
6129 print "<td>" .
6130 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
6131 -class=> "list name"},esc_html($remote)) .
6132 "</td>";
6133 print "<td class=\"link\">" .
6134 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
6135 " | " .
6136 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
6137 "</td>";
6138
6139 print "</tr>\n";
6140 }
6141
6142 if ($limited) {
6143 print "<tr>\n" .
6144 "<td colspan=\"3\">" .
6145 $cgi->a({-href => href(action=>"remotes")}, "...") .
6146 "</td>\n" . "</tr>\n";
6147 }
6148
6149 print "</table>";
6150}
6151
6152# Display remote heads grouped by remote, unless there are too many
6153# remotes, in which case we only display the remote names
6154sub git_remotes_body {
6155 my ($remotedata, $limit, $head) = @_;
6156 if ($limit and $limit < keys %$remotedata) {
6157 git_remotes_list($remotedata, $limit);
6158 } else {
6159 fill_remote_heads($remotedata);
6160 while (my ($remote, $rdata) = each %$remotedata) {
6161 git_print_section({-class=>"remote", -id=>$remote},
6162 ["remotes", $remote, $remote], sub {
6163 git_remote_block($remote, $rdata, $limit, $head);
6164 });
6165 }
6166 }
6167}
6168
Jakub Narebski16f20722011-06-22 17:28:53 +02006169sub git_search_message {
6170 my %co = @_;
6171
6172 my $greptype;
6173 if ($searchtype eq 'commit') {
6174 $greptype = "--grep=";
6175 } elsif ($searchtype eq 'author') {
6176 $greptype = "--author=";
6177 } elsif ($searchtype eq 'committer') {
6178 $greptype = "--committer=";
6179 }
6180 $greptype .= $searchtext;
6181 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6182 $greptype, '--regexp-ignore-case',
6183 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6184
6185 my $paging_nav = '';
6186 if ($page > 0) {
6187 $paging_nav .=
Jakub Narebski882541b2011-06-22 17:28:54 +02006188 $cgi->a({-href => href(-replay=>1, page=>undef)},
6189 "first") .
6190 " &sdot; " .
Jakub Narebski16f20722011-06-22 17:28:53 +02006191 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6192 -accesskey => "p", -title => "Alt-p"}, "prev");
6193 } else {
Jakub Narebski882541b2011-06-22 17:28:54 +02006194 $paging_nav .= "first &sdot; prev";
Jakub Narebski16f20722011-06-22 17:28:53 +02006195 }
6196 my $next_link = '';
6197 if ($#commitlist >= 100) {
6198 $next_link =
6199 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6200 -accesskey => "n", -title => "Alt-n"}, "next");
6201 $paging_nav .= " &sdot; $next_link";
6202 } else {
6203 $paging_nav .= " &sdot; next";
6204 }
6205
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006206 git_header_html();
6207
Jakub Narebski16f20722011-06-22 17:28:53 +02006208 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6209 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6210 if ($page == 0 && !@commitlist) {
6211 print "<p>No match.</p>\n";
6212 } else {
6213 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6214 }
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006215
6216 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006217}
6218
6219sub git_search_changes {
6220 my %co = @_;
6221
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006222 local $/ = "\n";
6223 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6224 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6225 ($search_use_regexp ? '--pickaxe-regex' : ())
6226 or die_error(500, "Open git-log failed");
6227
6228 git_header_html();
6229
Jakub Narebski16f20722011-06-22 17:28:53 +02006230 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6231 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6232
6233 print "<table class=\"pickaxe search\">\n";
6234 my $alternate = 1;
Jakub Narebski16f20722011-06-22 17:28:53 +02006235 undef %co;
6236 my @files;
6237 while (my $line = <$fd>) {
6238 chomp $line;
6239 next unless $line;
6240
6241 my %set = parse_difftree_raw_line($line);
6242 if (defined $set{'commit'}) {
6243 # finish previous commit
6244 if (%co) {
6245 print "</td>\n" .
6246 "<td class=\"link\">" .
Jakub Narebski882541b2011-06-22 17:28:54 +02006247 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6248 "commit") .
Jakub Narebski16f20722011-06-22 17:28:53 +02006249 " | " .
Jakub Narebski882541b2011-06-22 17:28:54 +02006250 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6251 hash_base=>$co{'id'})},
6252 "tree") .
6253 "</td>\n" .
Jakub Narebski16f20722011-06-22 17:28:53 +02006254 "</tr>\n";
6255 }
6256
6257 if ($alternate) {
6258 print "<tr class=\"dark\">\n";
6259 } else {
6260 print "<tr class=\"light\">\n";
6261 }
6262 $alternate ^= 1;
6263 %co = parse_commit($set{'commit'});
6264 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6265 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6266 "<td><i>$author</i></td>\n" .
6267 "<td>" .
6268 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6269 -class => "list subject"},
6270 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6271 } elsif (defined $set{'to_id'}) {
6272 next if ($set{'to_id'} =~ m/^0{40}$/);
6273
6274 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6275 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6276 -class => "list"},
6277 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6278 "<br/>\n";
6279 }
6280 }
6281 close $fd;
6282
6283 # finish last commit (warning: repetition!)
6284 if (%co) {
6285 print "</td>\n" .
6286 "<td class=\"link\">" .
Jakub Narebski882541b2011-06-22 17:28:54 +02006287 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6288 "commit") .
Jakub Narebski16f20722011-06-22 17:28:53 +02006289 " | " .
Jakub Narebski882541b2011-06-22 17:28:54 +02006290 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6291 hash_base=>$co{'id'})},
6292 "tree") .
6293 "</td>\n" .
Jakub Narebski16f20722011-06-22 17:28:53 +02006294 "</tr>\n";
6295 }
6296
6297 print "</table>\n";
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006298
6299 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006300}
6301
6302sub git_search_files {
6303 my %co = @_;
6304
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006305 local $/ = "\n";
Jakub Narebski8e09fd12012-01-05 21:32:56 +01006306 open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006307 $search_use_regexp ? ('-E', '-i') : '-F',
6308 $searchtext, $co{'tree'}
6309 or die_error(500, "Open git-grep failed");
6310
6311 git_header_html();
6312
Jakub Narebski16f20722011-06-22 17:28:53 +02006313 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6314 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6315
6316 print "<table class=\"grep_search\">\n";
6317 my $alternate = 1;
6318 my $matches = 0;
Jakub Narebski16f20722011-06-22 17:28:53 +02006319 my $lastfile = '';
Jakub Narebskifc8fcd22012-02-15 17:37:06 +01006320 my $file_href;
Jakub Narebski16f20722011-06-22 17:28:53 +02006321 while (my $line = <$fd>) {
6322 chomp $line;
Jakub Narebskifc8fcd22012-02-15 17:37:06 +01006323 my ($file, $lno, $ltext, $binary);
Jakub Narebski16f20722011-06-22 17:28:53 +02006324 last if ($matches++ > 1000);
6325 if ($line =~ /^Binary file (.+) matches$/) {
6326 $file = $1;
6327 $binary = 1;
6328 } else {
Jakub Narebski8e09fd12012-01-05 21:32:56 +01006329 ($file, $lno, $ltext) = split(/\0/, $line, 3);
6330 $file =~ s/^$co{'tree'}://;
Jakub Narebski16f20722011-06-22 17:28:53 +02006331 }
6332 if ($file ne $lastfile) {
6333 $lastfile and print "</td></tr>\n";
6334 if ($alternate++) {
6335 print "<tr class=\"dark\">\n";
6336 } else {
6337 print "<tr class=\"light\">\n";
6338 }
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006339 $file_href = href(action=>"blob", hash_base=>$co{'id'},
6340 file_name=>$file);
Jakub Narebski16f20722011-06-22 17:28:53 +02006341 print "<td class=\"list\">".
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006342 $cgi->a({-href => $file_href, -class => "list"}, esc_path($file));
Jakub Narebski16f20722011-06-22 17:28:53 +02006343 print "</td><td>\n";
6344 $lastfile = $file;
6345 }
6346 if ($binary) {
6347 print "<div class=\"binary\">Binary file</div>\n";
6348 } else {
6349 $ltext = untabify($ltext);
6350 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6351 $ltext = esc_html($1, -nbsp=>1);
6352 $ltext .= '<span class="match">';
6353 $ltext .= esc_html($2, -nbsp=>1);
6354 $ltext .= '</span>';
6355 $ltext .= esc_html($3, -nbsp=>1);
6356 } else {
6357 $ltext = esc_html($ltext, -nbsp=>1);
6358 }
6359 print "<div class=\"pre\">" .
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006360 $cgi->a({-href => $file_href.'#l'.$lno,
6361 -class => "linenr"}, sprintf('%4i', $lno)) .
6362 ' ' . $ltext . "</div>\n";
Jakub Narebski16f20722011-06-22 17:28:53 +02006363 }
6364 }
6365 if ($lastfile) {
6366 print "</td></tr>\n";
6367 if ($matches > 1000) {
6368 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6369 }
6370 } else {
6371 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6372 }
6373 close $fd;
6374
6375 print "</table>\n";
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006376
6377 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006378}
6379
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006380sub git_search_grep_body {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006381 my ($commitlist, $from, $to, $extra) = @_;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006382 $from = 0 unless defined $from;
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006383 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006384
Jakub Narebski591ebf62007-11-19 14:16:11 +01006385 print "<table class=\"commit_search\">\n";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006386 my $alternate = 1;
6387 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006388 my %co = %{$commitlist->[$i]};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006389 if (!%co) {
6390 next;
6391 }
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006392 my $commit = $co{'id'};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006393 if ($alternate) {
6394 print "<tr class=\"dark\">\n";
6395 } else {
6396 print "<tr class=\"light\">\n";
6397 }
6398 $alternate ^= 1;
6399 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02006400 format_author_html('td', \%co, 15, 5) .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006401 "<td>" .
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006402 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6403 -class => "list subject"},
6404 chop_and_escape_str($co{'title'}, 50) . "<br/>");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006405 my $comment = $co{'comment'};
6406 foreach my $line (@$comment) {
Jakub Narebski6dfbb302008-03-02 16:57:14 +01006407 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006408 my ($lead, $match, $trail) = ($1, $2, $3);
Jakub Narebskib8d97d02008-02-25 21:07:57 +01006409 $match = chop_str($match, 70, 5, 'center');
6410 my $contextlen = int((80 - length($match))/2);
6411 $contextlen = 30 if ($contextlen > 30);
6412 $lead = chop_str($lead, $contextlen, 10, 'left');
6413 $trail = chop_str($trail, $contextlen, 10, 'right');
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006414
6415 $lead = esc_html($lead);
6416 $match = esc_html($match);
6417 $trail = esc_html($trail);
6418
6419 print "$lead<span class=\"match\">$match</span>$trail<br />";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006420 }
6421 }
6422 print "</td>\n" .
6423 "<td class=\"link\">" .
6424 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6425 " | " .
Denis Chengf1fe8f52007-11-26 20:42:06 +08006426 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
6427 " | " .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006428 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6429 print "</td>\n" .
6430 "</tr>\n";
6431 }
6432 if (defined $extra) {
6433 print "<tr>\n" .
6434 "<td colspan=\"3\">$extra</td>\n" .
6435 "</tr>\n";
6436 }
6437 print "</table>\n";
6438}
6439
Jakub Narebski717b8312006-07-31 21:22:15 +02006440## ======================================================================
6441## ======================================================================
6442## actions
6443
Jakub Narebski717b8312006-07-31 21:22:15 +02006444sub git_project_list {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02006445 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02006446 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006447 die_error(400, "Unknown order parameter");
Jakub Narebski6326b602006-08-01 02:59:12 +02006448 }
6449
Bernhard R. Link19d2d232012-01-30 21:07:37 +01006450 my @list = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski717b8312006-07-31 21:22:15 +02006451 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006452 die_error(404, "No projects found");
Jakub Narebski717b8312006-07-31 21:22:15 +02006453 }
Jakub Narebski6326b602006-08-01 02:59:12 +02006454
Jakub Narebski717b8312006-07-31 21:22:15 +02006455 git_header_html();
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01006456 if (defined $home_text && -f $home_text) {
Jakub Narebski717b8312006-07-31 21:22:15 +02006457 print "<div class=\"index_include\">\n";
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01006458 insert_file($home_text);
Jakub Narebski717b8312006-07-31 21:22:15 +02006459 print "</div>\n";
6460 }
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01006461
6462 git_project_search_form($searchtext, $search_use_regexp);
Petr Baudise30496d2006-10-24 05:33:17 +02006463 git_project_list_body(\@list, $order);
6464 git_footer_html();
6465}
6466
6467sub git_forks {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02006468 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02006469 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006470 die_error(400, "Unknown order parameter");
Jakub Narebski717b8312006-07-31 21:22:15 +02006471 }
Petr Baudise30496d2006-10-24 05:33:17 +02006472
Bernhard R. Link4c7cd172012-01-30 21:05:47 +01006473 my $filter = $project;
6474 $filter =~ s/\.git$//;
6475 my @list = git_get_projects_list($filter);
Petr Baudise30496d2006-10-24 05:33:17 +02006476 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006477 die_error(404, "No forks found");
Jakub Narebski717b8312006-07-31 21:22:15 +02006478 }
Petr Baudise30496d2006-10-24 05:33:17 +02006479
6480 git_header_html();
6481 git_print_page_nav('','');
6482 git_print_header_div('summary', "$project forks");
6483 git_project_list_body(\@list, $order);
Jakub Narebski717b8312006-07-31 21:22:15 +02006484 git_footer_html();
6485}
6486
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006487sub git_project_index {
Bernhard R. Link19d2d232012-01-30 21:07:37 +01006488 my @projects = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski12b14432011-04-29 19:51:56 +02006489 if (!@projects) {
6490 die_error(404, "No projects found");
6491 }
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006492
6493 print $cgi->header(
6494 -type => 'text/plain',
6495 -charset => 'utf-8',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02006496 -content_disposition => 'inline; filename="index.aux"');
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006497
6498 foreach my $pr (@projects) {
6499 if (!exists $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02006500 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006501 }
6502
6503 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
6504 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
6505 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6506 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6507 $path =~ s/ /\+/g;
6508 $owner =~ s/ /\+/g;
6509
6510 print "$path $owner\n";
6511 }
6512}
6513
Kay Sieversede5e102005-08-07 20:23:12 +02006514sub git_summary {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006515 my $descr = git_get_project_description($project) || "none";
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00006516 my %co = parse_commit("HEAD");
Jakub Narebski785cdea2007-05-13 12:39:22 +02006517 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00006518 my $head = $co{'id'};
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006519 my $remote_heads = gitweb_check_feature('remote_heads');
Kay Sieversede5e102005-08-07 20:23:12 +02006520
Jakub Narebski1e0cf032006-08-14 02:10:06 +02006521 my $owner = git_get_project_owner($project);
Kay Sieversede5e102005-08-07 20:23:12 +02006522
Jakub Narebskicd146402006-11-02 20:23:11 +01006523 my $refs = git_get_references();
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006524 # These get_*_list functions return one more to allow us to see if
6525 # there are more ...
6526 my @taglist = git_get_tags_list(16);
6527 my @headlist = git_get_heads_list(16);
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006528 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
Petr Baudise30496d2006-10-24 05:33:17 +02006529 my @forklist;
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006530 my $check_forks = gitweb_check_feature('forks');
Junio C Hamano5dd5ed02006-11-07 22:00:45 -08006531
6532 if ($check_forks) {
Jakub Narebski12b14432011-04-29 19:51:56 +02006533 # find forks of a project
Bernhard R. Link4c7cd172012-01-30 21:05:47 +01006534 my $filter = $project;
6535 $filter =~ s/\.git$//;
6536 @forklist = git_get_projects_list($filter);
Jakub Narebski12b14432011-04-29 19:51:56 +02006537 # filter out forks of forks
6538 @forklist = filter_forks_from_projects_list(\@forklist)
6539 if (@forklist);
Petr Baudise30496d2006-10-24 05:33:17 +02006540 }
Jakub Narebski120ddde2006-09-19 14:33:22 +02006541
Kay Sieversede5e102005-08-07 20:23:12 +02006542 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02006543 git_print_page_nav('summary','', $head);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006544
Kay Sievers19806692005-08-07 20:26:27 +02006545 print "<div class=\"title\">&nbsp;</div>\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01006546 print "<table class=\"projects_list\">\n" .
Kacper Kornet0ebe7822012-04-26 18:45:44 +02006547 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n";
Tony Finch860ccc62013-08-20 17:59:44 +01006548 if ($owner and not $omit_owner) {
Kacper Kornet0ebe7822012-04-26 18:45:44 +02006549 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
6550 }
Jakub Narebski785cdea2007-05-13 12:39:22 +02006551 if (defined $cd{'rfc2822'}) {
Jakub Narebski256b7b42011-04-28 21:04:07 +02006552 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
6553 "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
Jakub Narebski785cdea2007-05-13 12:39:22 +02006554 }
6555
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02006556 # use per project git URL list in $projectroot/$project/cloneurl
6557 # or make project git URL from git base URL and project name
Jakub Narebski19a87212006-08-15 23:03:17 +02006558 my $url_tag = "URL";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02006559 my @url_list = git_get_project_url_list($project);
6560 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
6561 foreach my $git_url (@url_list) {
6562 next unless $git_url;
Giuseppe Bilotta0e656992010-11-11 13:26:15 +01006563 print format_repo_url($url_tag, $git_url);
Jakub Narebski19a87212006-08-15 23:03:17 +02006564 $url_tag = "";
6565 }
Petr Baudisaed93de2008-10-02 17:13:02 +02006566
6567 # Tag cloud
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006568 my $show_ctags = gitweb_check_feature('ctags');
Petr Baudisaed93de2008-10-02 17:13:02 +02006569 if ($show_ctags) {
6570 my $ctags = git_get_project_ctags($project);
Jakub Narebski0368c492011-04-29 19:51:57 +02006571 if (%$ctags) {
6572 # without ability to add tags, don't show if there are none
6573 my $cloud = git_populate_project_tagcloud($ctags);
6574 print "<tr id=\"metadata_ctags\">" .
6575 "<td>content tags</td>" .
6576 "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
6577 "</tr>\n";
6578 }
Petr Baudisaed93de2008-10-02 17:13:02 +02006579 }
6580
Jakub Narebski19a87212006-08-15 23:03:17 +02006581 print "</table>\n";
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006582
Matt McCutchen7e1100e2009-02-07 19:00:09 -05006583 # If XSS prevention is on, we don't include README.html.
6584 # TODO: Allow a readme in some safe format.
6585 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01006586 print "<div class=\"title\">readme</div>\n" .
6587 "<div class=\"readme\">\n";
6588 insert_file("$projectroot/$project/README.html");
6589 print "\n</div>\n"; # class="readme"
Petr Baudis447ef092006-10-24 05:23:46 +02006590 }
6591
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006592 # we need to request one more than 16 (0..15) to check if
6593 # those 16 are all
Jakub Narebski785cdea2007-05-13 12:39:22 +02006594 my @commitlist = $head ? parse_commits($head, 17) : ();
6595 if (@commitlist) {
6596 git_print_header_div('shortlog');
6597 git_shortlog_body(\@commitlist, 0, 15, $refs,
6598 $#commitlist <= 15 ? undef :
6599 $cgi->a({-href => href(action=>"shortlog")}, "..."));
6600 }
Kay Sieversede5e102005-08-07 20:23:12 +02006601
Jakub Narebski120ddde2006-09-19 14:33:22 +02006602 if (@taglist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006603 git_print_header_div('tags');
Jakub Narebski120ddde2006-09-19 14:33:22 +02006604 git_tags_body(\@taglist, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006605 $#taglist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006606 $cgi->a({-href => href(action=>"tags")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02006607 }
Kay Sievers0db37972005-08-07 20:24:35 +02006608
Jakub Narebski120ddde2006-09-19 14:33:22 +02006609 if (@headlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006610 git_print_header_div('heads');
Jakub Narebski120ddde2006-09-19 14:33:22 +02006611 git_heads_body(\@headlist, $head, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006612 $#headlist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006613 $cgi->a({-href => href(action=>"heads")}, "..."));
Kay Sievers0db37972005-08-07 20:24:35 +02006614 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006615
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006616 if (%remotedata) {
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006617 git_print_header_div('remotes');
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006618 git_remotes_body(\%remotedata, 15, $head);
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006619 }
6620
Petr Baudise30496d2006-10-24 05:33:17 +02006621 if (@forklist) {
6622 git_print_header_div('forks');
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02006623 git_project_list_body(\@forklist, 'age', 0, 15,
Robert Fitzsimonsaaca9672006-12-22 19:38:12 +00006624 $#forklist <= 15 ? undef :
Petr Baudise30496d2006-10-24 05:33:17 +02006625 $cgi->a({-href => href(action=>"forks")}, "..."),
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02006626 'no_header');
Petr Baudise30496d2006-10-24 05:33:17 +02006627 }
6628
Kay Sieversede5e102005-08-07 20:23:12 +02006629 git_footer_html();
6630}
6631
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006632sub git_tag {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006633 my %tag = parse_tag($hash);
Jakub Narebski198a2a82007-05-12 21:16:34 +02006634
6635 if (! %tag) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006636 die_error(404, "Unknown tag object");
Jakub Narebski198a2a82007-05-12 21:16:34 +02006637 }
6638
Anders Kaseorgd8a94802010-08-27 13:38:16 -04006639 my $head = git_get_head_hash($project);
6640 git_header_html();
6641 git_print_page_nav('','', $head,undef,$head);
Jakub Narebski847e01f2006-08-14 02:05:47 +02006642 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006643 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01006644 "<table class=\"object_header\">\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02006645 "<tr>\n" .
6646 "<td>object</td>\n" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02006647 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6648 $tag{'object'}) . "</td>\n" .
6649 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6650 $tag{'type'}) . "</td>\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02006651 "</tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006652 if (defined($tag{'author'})) {
Giuseppe Bilottaba924732009-06-30 00:00:50 +02006653 git_print_authorship_rows(\%tag, 'author');
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006654 }
6655 print "</table>\n\n" .
6656 "</div>\n";
6657 print "<div class=\"page_body\">";
6658 my $comment = $tag{'comment'};
6659 foreach my $line (@$comment) {
Junio C Hamano70022432006-11-24 14:04:01 -08006660 chomp $line;
Jakub Narebski793c4002006-11-24 22:25:50 +01006661 print esc_html($line, -nbsp=>1) . "<br/>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006662 }
6663 print "</div>\n";
6664 git_footer_html();
6665}
6666
Jakub Narebski4af819d2009-09-01 13:39:17 +02006667sub git_blame_common {
6668 my $format = shift || 'porcelain';
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01006669 if ($format eq 'porcelain' && $input_params{'javascript'}) {
Jakub Narebskic4ccf612009-09-01 13:39:19 +02006670 $format = 'incremental';
6671 $action = 'blame_incremental'; # for page title etc
6672 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006673
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006674 # permissions
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006675 gitweb_check_feature('blame')
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006676 or die_error(403, "Blame view not allowed");
Lea Wiemann074afaa2008-06-19 22:03:21 +02006677
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006678 # error checking
Lea Wiemann074afaa2008-06-19 22:03:21 +02006679 die_error(400, "No file name given") unless $file_name;
Jakub Narebski847e01f2006-08-14 02:05:47 +02006680 $hash_base ||= git_get_head_hash($project);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006681 die_error(404, "Couldn't find base commit") unless $hash_base;
Jakub Narebski847e01f2006-08-14 02:05:47 +02006682 my %co = parse_commit($hash_base)
Lea Wiemann074afaa2008-06-19 22:03:21 +02006683 or die_error(404, "Commit not found");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006684 my $ftype = "blob";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006685 if (!defined $hash) {
6686 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02006687 or die_error(404, "Error looking up file");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006688 } else {
6689 $ftype = git_get_type($hash);
6690 if ($ftype !~ "blob") {
6691 die_error(400, "Object is not a blob");
6692 }
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006693 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006694
Jakub Narebski4af819d2009-09-01 13:39:17 +02006695 my $fd;
6696 if ($format eq 'incremental') {
6697 # get file contents (as base)
6698 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
6699 or die_error(500, "Open git-cat-file failed");
6700 } elsif ($format eq 'data') {
6701 # run git-blame --incremental
6702 open $fd, "-|", git_cmd(), "blame", "--incremental",
6703 $hash_base, "--", $file_name
6704 or die_error(500, "Open git-blame --incremental failed");
6705 } else {
6706 # run git-blame --porcelain
6707 open $fd, "-|", git_cmd(), "blame", '-p',
6708 $hash_base, '--', $file_name
6709 or die_error(500, "Open git-blame --porcelain failed");
6710 }
Ævar Arnfjörð Bjarmasonfd870042013-08-30 08:37:01 +00006711 binmode $fd, ':utf8';
Jakub Narebski4af819d2009-09-01 13:39:17 +02006712
6713 # incremental blame data returns early
6714 if ($format eq 'data') {
6715 print $cgi->header(
6716 -type=>"text/plain", -charset => "utf-8",
6717 -status=> "200 OK");
6718 local $| = 1; # output autoflush
Jürgen Kreileder57cf4ad2011-12-17 10:22:23 +01006719 while (my $line = <$fd>) {
6720 print to_utf8($line);
6721 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006722 close $fd
6723 or print "ERROR $!\n";
6724
6725 print 'END';
6726 if (defined $t0 && gitweb_check_feature('timed')) {
6727 print ' '.
Jakub Narebski3962f1d72010-11-09 19:27:54 +01006728 tv_interval($t0, [ gettimeofday() ]).
Jakub Narebski4af819d2009-09-01 13:39:17 +02006729 ' '.$number_of_git_cmds;
6730 }
6731 print "\n";
6732
6733 return;
6734 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006735
6736 # page header
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006737 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02006738 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01006739 $cgi->a({-href => href(action=>"blob", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02006740 "blob") .
Jakub Narebski87e573f2009-12-01 17:54:26 +01006741 " | ";
6742 if ($format eq 'incremental') {
6743 $formats_nav .=
6744 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
6745 "blame") . " (non-incremental)";
6746 } else {
6747 $formats_nav .=
6748 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
6749 "blame") . " (incremental)";
6750 }
6751 $formats_nav .=
Jakub Narebski952c65f2006-08-22 16:52:50 +02006752 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01006753 $cgi->a({-href => href(action=>"history", -replay=>1)},
6754 "history") .
Petr Baudiscae18622006-09-22 03:19:41 +02006755 " | " .
Jakub Narebski4af819d2009-09-01 13:39:17 +02006756 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02006757 "HEAD");
Jakub Narebski847e01f2006-08-14 02:05:47 +02006758 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6759 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07006760 git_print_page_path($file_name, $ftype, $hash_base);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006761
6762 # page body
Jakub Narebski4af819d2009-09-01 13:39:17 +02006763 if ($format eq 'incremental') {
6764 print "<noscript>\n<div class=\"error\"><center><b>\n".
6765 "This page requires JavaScript to run.\n Use ".
Jakub Narebskic4ccf612009-09-01 13:39:19 +02006766 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
Jakub Narebski4af819d2009-09-01 13:39:17 +02006767 'this page').
6768 " instead.\n".
6769 "</b></center></div>\n</noscript>\n";
6770
6771 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
6772 }
6773
6774 print qq!<div class="page_body">\n!;
6775 print qq!<div id="progress_info">... / ...</div>\n!
6776 if ($format eq 'incremental');
6777 print qq!<table id="blame_table" class="blame" width="100%">\n!.
6778 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6779 qq!<thead>\n!.
6780 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
6781 qq!</thead>\n!.
6782 qq!<tbody>\n!;
6783
Jakub Narebskiaef37682009-07-25 00:44:06 +02006784 my @rev_color = qw(light dark);
Luben Tuikovcc1bf972006-07-23 13:37:53 -07006785 my $num_colors = scalar(@rev_color);
6786 my $current_color = 0;
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006787
Jakub Narebski4af819d2009-09-01 13:39:17 +02006788 if ($format eq 'incremental') {
6789 my $color_class = $rev_color[$current_color];
6790
6791 #contents of a file
6792 my $linenr = 0;
6793 LINE:
6794 while (my $line = <$fd>) {
6795 chomp $line;
6796 $linenr++;
6797
6798 print qq!<tr id="l$linenr" class="$color_class">!.
6799 qq!<td class="sha1"><a href=""> </a></td>!.
6800 qq!<td class="linenr">!.
6801 qq!<a class="linenr" href="">$linenr</a></td>!;
6802 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
6803 print qq!</tr>\n!;
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07006804 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006805
6806 } else { # porcelain, i.e. ordinary blame
6807 my %metainfo = (); # saves information about commits
6808
6809 # blame data
6810 LINE:
6811 while (my $line = <$fd>) {
6812 chomp $line;
6813 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6814 # no <lines in group> for subsequent lines in group of lines
6815 my ($full_rev, $orig_lineno, $lineno, $group_size) =
6816 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6817 if (!exists $metainfo{$full_rev}) {
6818 $metainfo{$full_rev} = { 'nprevious' => 0 };
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07006819 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006820 my $meta = $metainfo{$full_rev};
6821 my $data;
6822 while ($data = <$fd>) {
6823 chomp $data;
6824 last if ($data =~ s/^\t//); # contents of line
6825 if ($data =~ /^(\S+)(?: (.*))?$/) {
6826 $meta->{$1} = $2 unless exists $meta->{$1};
6827 }
6828 if ($data =~ /^previous /) {
6829 $meta->{'nprevious'}++;
Jakub Narebskia36817b2009-07-25 00:44:05 +02006830 }
6831 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006832 my $short_rev = substr($full_rev, 0, 8);
6833 my $author = $meta->{'author'};
6834 my %date =
6835 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
6836 my $date = $date{'iso-tz'};
6837 if ($group_size) {
6838 $current_color = ($current_color + 1) % $num_colors;
6839 }
6840 my $tr_class = $rev_color[$current_color];
6841 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6842 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6843 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6844 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6845 if ($group_size) {
6846 print "<td class=\"sha1\"";
6847 print " title=\"". esc_html($author) . ", $date\"";
6848 print " rowspan=\"$group_size\"" if ($group_size > 1);
6849 print ">";
6850 print $cgi->a({-href => href(action=>"commit",
6851 hash=>$full_rev,
6852 file_name=>$file_name)},
6853 esc_html($short_rev));
6854 if ($group_size >= 2) {
6855 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6856 if (@author_initials) {
6857 print "<br />" .
6858 esc_html(join('', @author_initials));
6859 # or join('.', ...)
6860 }
6861 }
6862 print "</td>\n";
6863 }
6864 # 'previous' <sha1 of parent commit> <filename at commit>
6865 if (exists $meta->{'previous'} &&
6866 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6867 $meta->{'parent'} = $1;
6868 $meta->{'file_parent'} = unquote($2);
6869 }
6870 my $linenr_commit =
6871 exists($meta->{'parent'}) ?
6872 $meta->{'parent'} : $full_rev;
6873 my $linenr_filename =
6874 exists($meta->{'file_parent'}) ?
6875 $meta->{'file_parent'} : unquote($meta->{'filename'});
6876 my $blamed = href(action => 'blame',
6877 file_name => $linenr_filename,
6878 hash_base => $linenr_commit);
6879 print "<td class=\"linenr\">";
6880 print $cgi->a({ -href => "$blamed#l$orig_lineno",
6881 -class => "linenr" },
6882 esc_html($lineno));
6883 print "</td>";
6884 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
6885 print "</tr>\n";
6886 } # end while
6887
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006888 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006889
6890 # footer
6891 print "</tbody>\n".
6892 "</table>\n"; # class="blame"
6893 print "</div>\n"; # class="blame_body"
Jakub Narebski952c65f2006-08-22 16:52:50 +02006894 close $fd
6895 or print "Reading blob failed\n";
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006896
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006897 git_footer_html();
6898}
6899
Jakub Narebski4af819d2009-09-01 13:39:17 +02006900sub git_blame {
6901 git_blame_common();
6902}
6903
6904sub git_blame_incremental {
6905 git_blame_common('incremental');
6906}
6907
6908sub git_blame_data {
6909 git_blame_common('data');
6910}
6911
Kay Sieversede5e102005-08-07 20:23:12 +02006912sub git_tags {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006913 my $head = git_get_head_hash($project);
Kay Sieversede5e102005-08-07 20:23:12 +02006914 git_header_html();
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01006915 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
Jakub Narebski847e01f2006-08-14 02:05:47 +02006916 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02006917
Jakub Narebskicd146402006-11-02 20:23:11 +01006918 my @tagslist = git_get_tags_list();
6919 if (@tagslist) {
6920 git_tags_body(\@tagslist);
Kay Sieversede5e102005-08-07 20:23:12 +02006921 }
Kay Sieversede5e102005-08-07 20:23:12 +02006922 git_footer_html();
6923}
6924
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02006925sub git_heads {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006926 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02006927 git_header_html();
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01006928 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
Jakub Narebski847e01f2006-08-14 02:05:47 +02006929 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02006930
Jakub Narebskicd146402006-11-02 20:23:11 +01006931 my @headslist = git_get_heads_list();
6932 if (@headslist) {
6933 git_heads_body(\@headslist, $head);
Kay Sievers0db37972005-08-07 20:24:35 +02006934 }
Kay Sievers0db37972005-08-07 20:24:35 +02006935 git_footer_html();
6936}
6937
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006938# used both for single remote view and for list of all the remotes
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006939sub git_remotes {
6940 gitweb_check_feature('remote_heads')
6941 or die_error(403, "Remote heads view is disabled");
6942
6943 my $head = git_get_head_hash($project);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006944 my $remote = $input_params{'hash'};
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006945
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006946 my $remotedata = git_get_remotes_list($remote);
6947 die_error(500, "Unable to get remote information") unless defined $remotedata;
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006948
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006949 unless (%$remotedata) {
6950 die_error(404, defined $remote ?
6951 "Remote $remote not found" :
6952 "No remotes found");
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006953 }
6954
6955 git_header_html(undef, undef, -action_extra => $remote);
6956 git_print_page_nav('', '', $head, undef, $head,
6957 format_ref_views($remote ? '' : 'remotes'));
6958
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006959 fill_remote_heads($remotedata);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006960 if (defined $remote) {
6961 git_print_header_div('remotes', "$remote remote for $project");
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006962 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006963 } else {
6964 git_print_header_div('summary', "$project remotes");
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006965 git_remotes_body($remotedata, undef, $head);
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006966 }
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006967
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006968 git_footer_html();
6969}
6970
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006971sub git_blob_plain {
Jakub Narebski7f718e82008-06-03 16:47:10 +02006972 my $type = shift;
Jakub Narebskif2e73302006-08-26 19:14:25 +02006973 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02006974
Luben Tuikovcff07712006-07-23 13:28:55 -07006975 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006976 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006977 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006978 $hash = git_get_hash_by_path($base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02006979 or die_error(404, "Cannot find file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006980 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006981 die_error(400, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006982 }
Martin Waitz800764c2006-09-16 23:09:02 +02006983 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6984 # blobs defined by non-textual hash id's can be cached
6985 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006986 }
Martin Waitz800764c2006-09-16 23:09:02 +02006987
Dennis Stosberg25691fb2006-08-28 17:49:58 +02006988 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02006989 or die_error(500, "Open git-cat-file blob '$hash' failed");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006990
Jakub Narebski7f718e82008-06-03 16:47:10 +02006991 # content-type (can include charset)
6992 $type = blob_contenttype($fd, $file_name, $type);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006993
Jakub Narebski7f718e82008-06-03 16:47:10 +02006994 # "save as" filename, even when no $file_name is given
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006995 my $save_as = "$hash";
6996 if (defined $file_name) {
6997 $save_as = $file_name;
6998 } elsif ($type =~ m/^text\//) {
6999 $save_as .= '.txt';
7000 }
7001
Matt McCutchen7e1100e2009-02-07 19:00:09 -05007002 # With XSS prevention on, blobs of all types except a few known safe
7003 # ones are served with "Content-Disposition: attachment" to make sure
7004 # they don't run in our security domain. For certain image types,
7005 # blob view writes an <img> tag referring to blob_plain view, and we
7006 # want to be sure not to break that by serving the image as an
7007 # attachment (though Firefox 3 doesn't seem to care).
7008 my $sandbox = $prevent_xss &&
Jakub Narebski86afbd02011-06-30 11:39:20 +02007009 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
7010
7011 # serve text/* as text/plain
7012 if ($prevent_xss &&
Jakub Narebskie8c35312011-06-30 11:39:21 +02007013 ($type =~ m!^text/[a-z]+\b(.*)$! ||
7014 ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
Jakub Narebski86afbd02011-06-30 11:39:20 +02007015 my $rest = $1;
7016 $rest = defined $rest ? $rest : '';
7017 $type = "text/plain$rest";
7018 }
Matt McCutchen7e1100e2009-02-07 19:00:09 -05007019
Jakub Narebskif2e73302006-08-26 19:14:25 +02007020 print $cgi->header(
Jakub Narebski7f718e82008-06-03 16:47:10 +02007021 -type => $type,
7022 -expires => $expires,
Matt McCutchen7e1100e2009-02-07 19:00:09 -05007023 -content_disposition =>
7024 ($sandbox ? 'attachment' : 'inline')
7025 . '; filename="' . $save_as . '"');
Jakub Narebski34122b52009-05-11 03:29:40 +02007026 local $/ = undef;
Luben Tuikov930cf7d2006-07-09 20:18:57 -07007027 binmode STDOUT, ':raw';
7028 print <$fd>;
7029 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
Luben Tuikov930cf7d2006-07-09 20:18:57 -07007030 close $fd;
7031}
7032
Kay Sievers09bd7892005-08-07 20:21:23 +02007033sub git_blob {
Jakub Narebskif2e73302006-08-26 19:14:25 +02007034 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02007035
Luben Tuikovcff07712006-07-23 13:28:55 -07007036 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007037 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007038 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007039 $hash = git_get_hash_by_path($base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02007040 or die_error(404, "Cannot find file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007041 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007042 die_error(400, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007043 }
Martin Waitz800764c2006-09-16 23:09:02 +02007044 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7045 # blobs defined by non-textual hash id's can be cached
7046 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007047 }
Martin Waitz800764c2006-09-16 23:09:02 +02007048
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08007049 my $have_blame = gitweb_check_feature('blame');
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007050 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02007051 or die_error(500, "Couldn't cat $file_name, $hash");
Jakub Narebski847e01f2006-08-14 02:05:47 +02007052 my $mimetype = blob_mimetype($fd, $file_name);
Johannes Schindelinb331fe52010-04-27 21:34:44 +02007053 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007054 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
Luben Tuikov930cf7d2006-07-09 20:18:57 -07007055 close $fd;
7056 return git_blob_plain($mimetype);
7057 }
Jakub Narebski5a4cf332006-12-04 23:47:22 +01007058 # we can have blame only for text/* mimetype
7059 $have_blame &&= ($mimetype =~ m!^text/!);
7060
Jakub Narebski592ea412010-04-27 21:34:45 +02007061 my $highlight = gitweb_check_feature('highlight');
7062 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
7063 $fd = run_highlighter($fd, $highlight, $syntax)
7064 if $syntax;
Johannes Schindelinb331fe52010-04-27 21:34:44 +02007065
Jakub Narebskif2e73302006-08-26 19:14:25 +02007066 git_header_html(undef, $expires);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007067 my $formats_nav = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02007068 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Kay Sievers93129442005-10-17 03:27:54 +02007069 if (defined $file_name) {
Florian Forster5996ca02006-06-12 10:31:57 +02007070 if ($have_blame) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02007071 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01007072 $cgi->a({-href => href(action=>"blame", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02007073 "blame") .
7074 " | ";
Florian Forster5996ca02006-06-12 10:31:57 +02007075 }
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007076 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01007077 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02007078 "history") .
7079 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01007080 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02007081 "raw") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007082 " | " .
7083 $cgi->a({-href => href(action=>"blob",
7084 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02007085 "HEAD");
Kay Sievers93129442005-10-17 03:27:54 +02007086 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02007087 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01007088 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
7089 "raw");
Kay Sievers93129442005-10-17 03:27:54 +02007090 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007091 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7092 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02007093 } else {
7094 print "<div class=\"page_nav\">\n" .
7095 "<br/><br/></div>\n" .
Jakub Narebski3017ed62010-12-15 00:34:01 +01007096 "<div class=\"title\">".esc_html($hash)."</div>\n";
Kay Sievers09bd7892005-08-07 20:21:23 +02007097 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07007098 git_print_page_path($file_name, "blob", $hash_base);
Kay Sieversc07ad4b2005-08-07 20:22:44 +02007099 print "<div class=\"page_body\">\n";
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007100 if ($mimetype =~ m!^image/!) {
Andrew Keller46a74712014-02-17 09:25:13 -05007101 print qq!<img class="blob" type="!.esc_attr($mimetype).qq!"!;
Jakub Narebski5a4cf332006-12-04 23:47:22 +01007102 if ($file_name) {
Jakub Narebski3017ed62010-12-15 00:34:01 +01007103 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
Jakub Narebski5a4cf332006-12-04 23:47:22 +01007104 }
7105 print qq! src="! .
7106 href(action=>"blob_plain", hash=>$hash,
7107 hash_base=>$hash_base, file_name=>$file_name) .
7108 qq!" />\n!;
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007109 } else {
7110 my $nr;
7111 while (my $line = <$fd>) {
7112 chomp $line;
7113 $nr++;
7114 $line = untabify($line);
Jakub Narebski592ea412010-04-27 21:34:45 +02007115 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
Jakub Narebski08667862011-09-16 14:41:57 +02007116 $nr, esc_attr(href(-replay => 1)), $nr, $nr,
7117 $syntax ? sanitize($line) : esc_html($line, -nbsp=>1);
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007118 }
Kay Sievers161332a2005-08-07 19:49:46 +02007119 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02007120 close $fd
7121 or print "Reading blob failed.\n";
Kay Sieversfbb592a2005-08-07 20:12:11 +02007122 print "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02007123 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007124}
7125
7126sub git_tree {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07007127 if (!defined $hash_base) {
7128 $hash_base = "HEAD";
7129 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02007130 if (!defined $hash) {
Kay Sievers09bd7892005-08-07 20:21:23 +02007131 if (defined $file_name) {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07007132 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
7133 } else {
7134 $hash = $hash_base;
Kay Sievers10dba282005-08-07 20:25:27 +02007135 }
Kay Sieverse925f382005-08-07 20:23:35 +02007136 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02007137 die_error(404, "No such tree") unless defined($hash);
Jakub Narebski34122b52009-05-11 03:29:40 +02007138
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007139 my $show_sizes = gitweb_check_feature('show-sizes');
7140 my $have_blame = gitweb_check_feature('blame');
7141
Jakub Narebski34122b52009-05-11 03:29:40 +02007142 my @entries = ();
7143 {
7144 local $/ = "\0";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007145 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
7146 ($show_sizes ? '-l' : ()), @extra_options, $hash
Jakub Narebski34122b52009-05-11 03:29:40 +02007147 or die_error(500, "Open git-ls-tree failed");
7148 @entries = map { chomp; $_ } <$fd>;
7149 close $fd
7150 or die_error(404, "Reading tree failed");
7151 }
Kay Sieversd63577d2005-08-07 20:18:13 +02007152
Jakub Narebski847e01f2006-08-14 02:05:47 +02007153 my $refs = git_get_references();
7154 my $ref = format_ref_marker($refs, $hash_base);
Kay Sievers12a88f22005-08-07 20:02:47 +02007155 git_header_html();
Jakub Narebski300454f2006-10-21 17:53:09 +02007156 my $basedir = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02007157 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Petr Baudiscae18622006-09-22 03:19:41 +02007158 my @views_nav = ();
7159 if (defined $file_name) {
7160 push @views_nav,
Jakub Narebskia3823e52007-11-01 13:06:29 +01007161 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02007162 "history"),
7163 $cgi->a({-href => href(action=>"tree",
7164 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02007165 "HEAD"),
Petr Baudiscae18622006-09-22 03:19:41 +02007166 }
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007167 my $snapshot_links = format_snapshot_links($hash);
7168 if (defined $snapshot_links) {
Petr Baudiscae18622006-09-22 03:19:41 +02007169 # FIXME: Should be available when we have no hash base as well.
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007170 push @views_nav, $snapshot_links;
Petr Baudiscae18622006-09-22 03:19:41 +02007171 }
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007172 git_print_page_nav('tree','', $hash_base, undef, undef,
7173 join(' | ', @views_nav));
Jakub Narebski847e01f2006-08-14 02:05:47 +02007174 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
Kay Sieversd63577d2005-08-07 20:18:13 +02007175 } else {
Jakub Narebskifa702002006-08-31 00:35:07 +02007176 undef $hash_base;
Kay Sieversd63577d2005-08-07 20:18:13 +02007177 print "<div class=\"page_nav\">\n";
7178 print "<br/><br/></div>\n";
Jakub Narebski3017ed62010-12-15 00:34:01 +01007179 print "<div class=\"title\">".esc_html($hash)."</div>\n";
Kay Sieversd63577d2005-08-07 20:18:13 +02007180 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007181 if (defined $file_name) {
Jakub Narebski300454f2006-10-21 17:53:09 +02007182 $basedir = $file_name;
7183 if ($basedir ne '' && substr($basedir, -1) ne '/') {
7184 $basedir .= '/';
7185 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02007186 git_print_page_path($file_name, 'tree', $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02007187 }
Kay Sieversfbb592a2005-08-07 20:12:11 +02007188 print "<div class=\"page_body\">\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01007189 print "<table class=\"tree\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07007190 my $alternate = 1;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007191 # '..' (top directory) link if possible
7192 if (defined $hash_base &&
7193 defined $file_name && $file_name =~ m![^/]+$!) {
7194 if ($alternate) {
7195 print "<tr class=\"dark\">\n";
7196 } else {
7197 print "<tr class=\"light\">\n";
7198 }
7199 $alternate ^= 1;
7200
7201 my $up = $file_name;
7202 $up =~ s!/?[^/]+$!!;
7203 undef $up unless $up;
7204 # based on git_print_tree_entry
7205 print '<td class="mode">' . mode_str('040000') . "</td>\n";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007206 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007207 print '<td class="list">';
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007208 print $cgi->a({-href => href(action=>"tree",
7209 hash_base=>$hash_base,
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007210 file_name=>$up)},
7211 "..");
7212 print "</td>\n";
7213 print "<td class=\"link\"></td>\n";
7214
7215 print "</tr>\n";
7216 }
Kay Sievers161332a2005-08-07 19:49:46 +02007217 foreach my $line (@entries) {
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007218 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
Jakub Narebskicb849b42006-08-31 00:32:15 +02007219
Kay Sieversbddec012005-08-07 20:25:42 +02007220 if ($alternate) {
Kay Sieversc994d622005-08-07 20:27:18 +02007221 print "<tr class=\"dark\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007222 } else {
Kay Sieversc994d622005-08-07 20:27:18 +02007223 print "<tr class=\"light\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007224 }
7225 $alternate ^= 1;
Jakub Narebskicb849b42006-08-31 00:32:15 +02007226
Jakub Narebski300454f2006-10-21 17:53:09 +02007227 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
Jakub Narebskifa702002006-08-31 00:35:07 +02007228
Kay Sievers42f7eb92005-08-07 20:21:46 +02007229 print "</tr>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02007230 }
Kay Sievers42f7eb92005-08-07 20:21:46 +02007231 print "</table>\n" .
7232 "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02007233 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007234}
7235
Krzesimir Nowake3747472013-12-11 12:54:44 +01007236sub sanitize_for_filename {
7237 my $name = shift;
7238
7239 $name =~ s!/!-!g;
7240 $name =~ s/[^[:alnum:]_.-]//g;
7241
7242 return $name;
7243}
7244
Mark Radab6292752009-11-07 16:13:29 +01007245sub snapshot_name {
7246 my ($project, $hash) = @_;
7247
7248 # path/to/project.git -> project
7249 # path/to/project/.git -> project
7250 my $name = to_utf8($project);
7251 $name =~ s,([^/])/*\.git$,$1,;
Krzesimir Nowake3747472013-12-11 12:54:44 +01007252 $name = sanitize_for_filename(basename($name));
Mark Radab6292752009-11-07 16:13:29 +01007253
7254 my $ver = $hash;
7255 if ($hash =~ /^[0-9a-fA-F]+$/) {
7256 # shorten SHA-1 hash
7257 my $full_hash = git_get_full_hash($project, $hash);
7258 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
7259 $ver = git_get_short_hash($project, $hash);
7260 }
7261 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
7262 # tags don't need shortened SHA-1 hash
7263 $ver = $1;
7264 } else {
7265 # branches and other need shortened SHA-1 hash
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01007266 my $strip_refs = join '|', map { quotemeta } get_branch_refs();
7267 if ($hash =~ m!^refs/($strip_refs|remotes)/(.*)$!) {
Krzesimir Nowake3747472013-12-11 12:54:44 +01007268 my $ref_dir = (defined $1) ? $1 : '';
7269 $ver = $2;
7270
7271 $ref_dir = sanitize_for_filename($ref_dir);
7272 # for refs neither in heads nor remotes we want to
7273 # add a ref dir to archive name
7274 if ($ref_dir ne '' and $ref_dir ne 'heads' and $ref_dir ne 'remotes') {
7275 $ver = $ref_dir . '-' . $ver;
7276 }
Mark Radab6292752009-11-07 16:13:29 +01007277 }
7278 $ver .= '-' . git_get_short_hash($project, $hash);
7279 }
Krzesimir Nowake3747472013-12-11 12:54:44 +01007280 # special case of sanitization for filename - we change
7281 # slashes to dots instead of dashes
Mark Radab6292752009-11-07 16:13:29 +01007282 # in case of hierarchical branch names
7283 $ver =~ s!/!.!g;
Krzesimir Nowake3747472013-12-11 12:54:44 +01007284 $ver =~ s/[^[:alnum:]_.-]//g;
Mark Radab6292752009-11-07 16:13:29 +01007285
7286 # name = project-version_string
7287 $name = "$name-$ver";
7288
7289 return wantarray ? ($name, $name) : $name;
7290}
7291
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04007292sub exit_if_unmodified_since {
7293 my ($latest_epoch) = @_;
7294 our $cgi;
7295
7296 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7297 if (defined $if_modified) {
7298 my $since;
7299 if (eval { require HTTP::Date; 1; }) {
7300 $since = HTTP::Date::str2time($if_modified);
7301 } elsif (eval { require Time::ParseDate; 1; }) {
7302 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7303 }
7304 if (defined $since && $latest_epoch <= $since) {
7305 my %latest_date = parse_date($latest_epoch);
7306 print $cgi->header(
7307 -last_modified => $latest_date{'rfc2822'},
7308 -status => '304 Not Modified');
7309 goto DONE_GITWEB;
7310 }
7311 }
7312}
7313
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307314sub git_snapshot {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02007315 my $format = $input_params{'snapshot_format'};
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01007316 if (!@snapshot_fmts) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007317 die_error(403, "Snapshots not allowed");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007318 }
7319 # default to first supported snapshot format
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01007320 $format ||= $snapshot_fmts[0];
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007321 if ($format !~ m/^[a-z0-9]+$/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007322 die_error(400, "Invalid snapshot format parameter");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007323 } elsif (!exists($known_snapshot_formats{$format})) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007324 die_error(400, "Unknown snapshot format");
Mark A Rada1bfd3632009-08-06 10:25:39 -04007325 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
7326 die_error(403, "Snapshot format not allowed");
Mark Rada34b31a82009-08-25 00:59:48 -04007327 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
7328 die_error(403, "Unsupported snapshot format");
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05307329 }
7330
Mark Radafdb0c362009-09-26 13:46:08 -04007331 my $type = git_get_type("$hash^{}");
7332 if (!$type) {
7333 die_error(404, 'Object does not exist');
7334 } elsif ($type eq 'blob') {
7335 die_error(400, 'Object is not a tree-ish');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307336 }
7337
Mark Radab6292752009-11-07 16:13:29 +01007338 my ($name, $prefix) = snapshot_name($project, $hash);
7339 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
W. Trevor King8745db62012-03-29 08:45:49 -04007340
7341 my %co = parse_commit($hash);
7342 exit_if_unmodified_since($co{'committer_epoch'}) if %co;
7343
Mark Radab6292752009-11-07 16:13:29 +01007344 my $cmd = quote_command(
Lea Wiemann516381d2008-06-17 23:46:35 +02007345 git_cmd(), 'archive',
7346 "--format=$known_snapshot_formats{$format}{'format'}",
Mark Radab6292752009-11-07 16:13:29 +01007347 "--prefix=$prefix/", $hash);
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007348 if (exists $known_snapshot_formats{$format}{'compressor'}) {
Lea Wiemann516381d2008-06-17 23:46:35 +02007349 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
Mark Levedahl072570e2007-05-20 11:46:46 -04007350 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307351
Mark Radab6292752009-11-07 16:13:29 +01007352 $filename =~ s/(["\\])/\\$1/g;
W. Trevor King8745db62012-03-29 08:45:49 -04007353 my %latest_date;
7354 if (%co) {
7355 %latest_date = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
7356 }
7357
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02007358 print $cgi->header(
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007359 -type => $known_snapshot_formats{$format}{'type'},
Mark Radab6292752009-11-07 16:13:29 +01007360 -content_disposition => 'inline; filename="' . $filename . '"',
W. Trevor King8745db62012-03-29 08:45:49 -04007361 %co ? (-last_modified => $latest_date{'rfc2822'}) : (),
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02007362 -status => '200 OK');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307363
Mark Levedahl072570e2007-05-20 11:46:46 -04007364 open my $fd, "-|", $cmd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007365 or die_error(500, "Execute git-archive failed");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307366 binmode STDOUT, ':raw';
7367 print <$fd>;
7368 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
7369 close $fd;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307370}
7371
Jakub Narebski15f0b112009-11-13 02:02:13 +01007372sub git_log_generic {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007373 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
Jakub Narebski15f0b112009-11-13 02:02:13 +01007374
Jakub Narebski847e01f2006-08-14 02:05:47 +02007375 my $head = git_get_head_hash($project);
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007376 if (!defined $base) {
7377 $base = $head;
Kay Sievers0db37972005-08-07 20:24:35 +02007378 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02007379 if (!defined $page) {
7380 $page = 0;
Kay Sieversb87d78d2005-08-07 20:21:04 +02007381 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007382 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02007383
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007384 my $commit_hash = $base;
7385 if (defined $parent) {
7386 $commit_hash = "$parent..$base";
Jakub Narebski15f0b112009-11-13 02:02:13 +01007387 }
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007388 my @commitlist =
7389 parse_commits($commit_hash, 101, (100 * $page),
7390 defined $file_name ? ($file_name, "--full-history") : ());
Kay Sieversea4a6df2005-08-07 20:26:49 +02007391
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007392 my $ftype;
7393 if (!defined $file_hash && defined $file_name) {
7394 # some commits could have deleted file in question,
7395 # and not have it in tree, but one of them has to have it
7396 for (my $i = 0; $i < @commitlist; $i++) {
7397 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
7398 last if defined $file_hash;
7399 }
7400 }
7401 if (defined $file_hash) {
7402 $ftype = git_get_type($file_hash);
7403 }
7404 if (defined $file_name && !defined $ftype) {
7405 die_error(500, "Unknown type of object");
7406 }
7407 my %co;
7408 if (defined $file_name) {
7409 %co = parse_commit($base)
7410 or die_error(404, "Unknown commit object");
7411 }
7412
7413
7414 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
Jakub Narebski15f0b112009-11-13 02:02:13 +01007415 my $next_link = '';
Jakub Narebski42671ca2009-11-13 02:02:12 +01007416 if ($#commitlist >= 100) {
7417 $next_link =
7418 $cgi->a({-href => href(-replay=>1, page=>$page+1),
7419 -accesskey => "n", -title => "Alt-n"}, "next");
7420 }
Jakub Narebski15f0b112009-11-13 02:02:13 +01007421 my $patch_max = gitweb_get_feature('patches');
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007422 if ($patch_max && !defined $file_name) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007423 if ($patch_max < 0 || @commitlist <= $patch_max) {
7424 $paging_nav .= " &sdot; " .
7425 $cgi->a({-href => href(action=>"patches", -replay=>1)},
7426 "patches");
7427 }
7428 }
7429
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007430 git_header_html();
Jakub Narebski15f0b112009-11-13 02:02:13 +01007431 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007432 if (defined $file_name) {
7433 git_print_header_div('commit', esc_html($co{'title'}), $base);
7434 } else {
7435 git_print_header_div('summary', $project)
7436 }
7437 git_print_page_path($file_name, $ftype, $hash_base)
7438 if (defined $file_name);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007439
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007440 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
7441 $file_name, $file_hash, $ftype);
Jakub Narebski42671ca2009-11-13 02:02:12 +01007442
Kay Sievers034df392005-08-07 20:20:07 +02007443 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007444}
7445
Jakub Narebski15f0b112009-11-13 02:02:13 +01007446sub git_log {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007447 git_log_generic('log', \&git_log_body,
7448 $hash, $hash_parent);
Jakub Narebski15f0b112009-11-13 02:02:13 +01007449}
7450
Kay Sievers09bd7892005-08-07 20:21:23 +02007451sub git_commit {
Jakub Narebski9954f772006-11-18 23:35:41 +01007452 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02007453 my %co = parse_commit($hash)
7454 or die_error(404, "Unknown commit object");
Kay Sievers161332a2005-08-07 19:49:46 +02007455
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007456 my $parent = $co{'parent'};
7457 my $parents = $co{'parents'}; # listref
7458
7459 # we need to prepare $formats_nav before any parameter munging
7460 my $formats_nav;
7461 if (!defined $parent) {
7462 # --root commitdiff
7463 $formats_nav .= '(initial)';
7464 } elsif (@$parents == 1) {
7465 # single parent commit
7466 $formats_nav .=
7467 '(parent: ' .
7468 $cgi->a({-href => href(action=>"commit",
7469 hash=>$parent)},
7470 esc_html(substr($parent, 0, 7))) .
7471 ')';
7472 } else {
7473 # merge commit
7474 $formats_nav .=
7475 '(merge: ' .
7476 join(' ', map {
Jakub Narebskif9308a12007-03-23 21:04:31 +01007477 $cgi->a({-href => href(action=>"commit",
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007478 hash=>$_)},
7479 esc_html(substr($_, 0, 7)));
7480 } @$parents ) .
7481 ')';
7482 }
Jakub Narebski1655c982009-10-09 14:26:44 +02007483 if (gitweb_check_feature('patches') && @$parents <= 1) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007484 $formats_nav .= " | " .
7485 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7486 "patch");
7487 }
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007488
Kay Sieversd8a20ba2005-08-07 20:28:53 +02007489 if (!defined $parent) {
Jakub Narebskib9182982006-07-30 18:28:34 -07007490 $parent = "--root";
Kay Sievers6191f8e2005-08-07 20:19:56 +02007491 }
Jakub Narebski549ab4a2006-12-15 17:53:45 +01007492 my @difftree;
Jakub Narebski208ecb22007-05-07 01:10:08 +02007493 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
7494 @diff_opts,
7495 (@$parents <= 1 ? $parent : '-c'),
7496 $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007497 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski208ecb22007-05-07 01:10:08 +02007498 @difftree = map { chomp; $_ } <$fd>;
Lea Wiemann074afaa2008-06-19 22:03:21 +02007499 close $fd or die_error(404, "Reading git-diff-tree failed");
Kay Sievers11044292005-10-19 03:18:45 +02007500
7501 # non-textual hash id's can be cached
7502 my $expires;
7503 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7504 $expires = "+1d";
7505 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007506 my $refs = git_get_references();
7507 my $ref = format_ref_marker($refs, $co{'id'});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05307508
Jakub Narebski594e2122006-07-31 02:21:52 +02007509 git_header_html(undef, $expires);
Petr Baudisa1441542006-10-06 18:59:33 +02007510 git_print_page_nav('commit', '',
Jakub Narebski952c65f2006-08-22 16:52:50 +02007511 $hash, $co{'tree'}, $hash,
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007512 $formats_nav);
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07007513
Kay Sieversb87d78d2005-08-07 20:21:04 +02007514 if (defined $co{'parent'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007515 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02007516 } else {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007517 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02007518 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02007519 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01007520 "<table class=\"object_header\">\n";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02007521 git_print_authorship_rows(\%co);
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00007522 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007523 print "<tr>" .
7524 "<td>tree</td>" .
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00007525 "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007526 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
7527 class => "list"}, $co{'tree'}) .
Kay Sievers19806692005-08-07 20:26:27 +02007528 "</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007529 "<td class=\"link\">" .
7530 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
7531 "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007532 my $snapshot_links = format_snapshot_links($hash);
7533 if (defined $snapshot_links) {
7534 print " | " . $snapshot_links;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307535 }
7536 print "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02007537 "</tr>\n";
Jakub Narebski549ab4a2006-12-15 17:53:45 +01007538
Kay Sievers3e029292005-08-07 20:05:15 +02007539 foreach my $par (@$parents) {
Kay Sieversbddec012005-08-07 20:25:42 +02007540 print "<tr>" .
7541 "<td>parent</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007542 "<td class=\"sha1\">" .
7543 $cgi->a({-href => href(action=>"commit", hash=>$par),
7544 class => "list"}, $par) .
7545 "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02007546 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02007547 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007548 " | " .
Jakub Narebskif2e60942006-09-03 23:43:03 +02007549 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
Kay Sieversbddec012005-08-07 20:25:42 +02007550 "</td>" .
7551 "</tr>\n";
Kay Sievers3e029292005-08-07 20:05:15 +02007552 }
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02007553 print "</table>".
Kay Sieversb87d78d2005-08-07 20:21:04 +02007554 "</div>\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02007555
Kay Sieversfbb592a2005-08-07 20:12:11 +02007556 print "<div class=\"page_body\">\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02007557 git_print_log($co{'comment'});
Kay Sievers927dcec2005-08-07 20:18:44 +02007558 print "</div>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02007559
Jakub Narebski208ecb22007-05-07 01:10:08 +02007560 git_difftree_body(\@difftree, $hash, @$parents);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02007561
Kay Sievers12a88f22005-08-07 20:02:47 +02007562 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007563}
7564
Jakub Narebskica946012006-12-10 13:25:47 +01007565sub git_object {
7566 # object is defined by:
7567 # - hash or hash_base alone
7568 # - hash_base and file_name
7569 my $type;
7570
7571 # - hash or hash_base alone
7572 if ($hash || ($hash_base && !defined $file_name)) {
7573 my $object_id = $hash || $hash_base;
7574
Lea Wiemann516381d2008-06-17 23:46:35 +02007575 open my $fd, "-|", quote_command(
7576 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
Lea Wiemann074afaa2008-06-19 22:03:21 +02007577 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007578 $type = <$fd>;
7579 chomp $type;
7580 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007581 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007582
7583 # - hash_base and file_name
7584 } elsif ($hash_base && defined $file_name) {
7585 $file_name =~ s,/+$,,;
7586
7587 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
Lea Wiemann074afaa2008-06-19 22:03:21 +02007588 or die_error(404, "Base object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007589
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02007590 # here errors should not happen
Jakub Narebskica946012006-12-10 13:25:47 +01007591 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007592 or die_error(500, "Open git-ls-tree failed");
Jakub Narebskica946012006-12-10 13:25:47 +01007593 my $line = <$fd>;
7594 close $fd;
7595
7596 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
7597 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007598 die_error(404, "File or directory for given base does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007599 }
7600 $type = $2;
7601 $hash = $3;
7602 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007603 die_error(400, "Not enough information to find object");
Jakub Narebskica946012006-12-10 13:25:47 +01007604 }
7605
7606 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
7607 hash=>$hash, hash_base=>$hash_base,
7608 file_name=>$file_name),
7609 -status => '302 Found');
7610}
7611
Kay Sievers09bd7892005-08-07 20:21:23 +02007612sub git_blobdiff {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007613 my $format = shift || 'html';
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007614 my $diff_style = $input_params{'diff_style'} || 'inline';
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007615
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007616 my $fd;
7617 my @difftree;
7618 my %diffinfo;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007619 my $expires;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007620
7621 # preparing $fd and %diffinfo for git_patchset_body
7622 # new style URI
7623 if (defined $hash_base && defined $hash_parent_base) {
7624 if (defined $file_name) {
7625 # read raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007626 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7627 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02007628 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007629 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007630 @difftree = map { chomp; $_ } <$fd>;
7631 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007632 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007633 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02007634 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007635
Jakub Narebski0aea3372006-08-27 23:45:26 +02007636 } elsif (defined $hash &&
7637 $hash =~ /[0-9a-fA-F]{40}/) {
7638 # try to find filename from $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007639
7640 # read filtered raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007641 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7642 $hash_parent_base, $hash_base, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007643 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007644 @difftree =
7645 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
7646 # $hash == to_id
7647 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
7648 map { chomp; $_ } <$fd>;
7649 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007650 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007651 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02007652 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007653
7654 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007655 die_error(400, "Missing one of the blob diff parameters");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007656 }
7657
7658 if (@difftree > 1) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007659 die_error(400, "Ambiguous blob diff specification");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007660 }
7661
7662 %diffinfo = parse_difftree_raw_line($difftree[0]);
Jakub Narebski9d301452007-11-01 12:38:08 +01007663 $file_parent ||= $diffinfo{'from_file'} || $file_name;
7664 $file_name ||= $diffinfo{'to_file'};
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007665
7666 $hash_parent ||= $diffinfo{'from_id'};
7667 $hash ||= $diffinfo{'to_id'};
7668
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007669 # non-textual hash id's can be cached
7670 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
7671 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
7672 $expires = '+1d';
7673 }
7674
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007675 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007676 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski957d6ea2007-04-05 13:45:41 +02007677 '-p', ($format eq 'html' ? "--full-index" : ()),
7678 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02007679 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007680 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007681 }
7682
Junio C Hamanob54dc9f2008-12-16 19:42:02 -08007683 # old/legacy style URI -- not generated anymore since 1.4.3.
7684 if (!%diffinfo) {
7685 die_error('404 Not Found', "Missing one of the blob diff parameters")
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007686 }
7687
7688 # header
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007689 if ($format eq 'html') {
7690 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01007691 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02007692 "raw");
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007693 $formats_nav .= diff_style_nav($diff_style);
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007694 git_header_html(undef, $expires);
7695 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7696 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7697 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7698 } else {
7699 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
Jakub Narebski3017ed62010-12-15 00:34:01 +01007700 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007701 }
7702 if (defined $file_name) {
7703 git_print_page_path($file_name, "blob", $hash_base);
7704 } else {
7705 print "<div class=\"page_path\"></div>\n";
7706 }
7707
7708 } elsif ($format eq 'plain') {
7709 print $cgi->header(
7710 -type => 'text/plain',
7711 -charset => 'utf-8',
7712 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07007713 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007714
7715 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7716
Kay Sievers09bd7892005-08-07 20:21:23 +02007717 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007718 die_error(400, "Unknown blobdiff format");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007719 }
7720
7721 # patch
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007722 if ($format eq 'html') {
7723 print "<div class=\"page_body\">\n";
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007724
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007725 git_patchset_body($fd, $diff_style,
7726 [ \%diffinfo ], $hash_base, $hash_parent_base);
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007727 close $fd;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007728
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007729 print "</div>\n"; # class="page_body"
7730 git_footer_html();
7731
7732 } else {
7733 while (my $line = <$fd>) {
Jakub Narebski403d0902006-11-08 11:48:56 +01007734 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
7735 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007736
7737 print $line;
7738
7739 last if $line =~ m!^\+\+\+!;
7740 }
7741 local $/ = undef;
7742 print <$fd>;
7743 close $fd;
7744 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007745}
7746
Kay Sievers19806692005-08-07 20:26:27 +02007747sub git_blobdiff_plain {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007748 git_blobdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02007749}
7750
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007751# assumes that it is added as later part of already existing navigation,
7752# so it returns "| foo | bar" rather than just "foo | bar"
7753sub diff_style_nav {
7754 my ($diff_style, $is_combined) = @_;
7755 $diff_style ||= 'inline';
7756
7757 return "" if ($is_combined);
7758
7759 my @styles = (inline => 'inline', 'sidebyside' => 'side by side');
7760 my %styles = @styles;
7761 @styles =
7762 @styles[ map { $_ * 2 } 0..$#styles/2 ];
7763
7764 return join '',
7765 map { " | ".$_ }
7766 map {
7767 $_ eq $diff_style ? $styles{$_} :
7768 $cgi->a({-href => href(-replay=>1, diff_style => $_)}, $styles{$_})
7769 } @styles;
7770}
7771
Kay Sievers09bd7892005-08-07 20:21:23 +02007772sub git_commitdiff {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01007773 my %params = @_;
7774 my $format = $params{-format} || 'html';
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007775 my $diff_style = $input_params{'diff_style'} || 'inline';
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007776
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007777 my ($patch_max) = gitweb_get_feature('patches');
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007778 if ($format eq 'patch') {
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007779 die_error(403, "Patch view not allowed") unless $patch_max;
7780 }
7781
Jakub Narebski9954f772006-11-18 23:35:41 +01007782 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02007783 my %co = parse_commit($hash)
7784 or die_error(404, "Unknown commit object");
Jakub Narebski151602d2006-10-23 00:37:56 +02007785
Jakub Narebskicd030c32007-06-08 13:33:28 +02007786 # choose format for commitdiff for merge
7787 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7788 $hash_parent = '--cc';
7789 }
7790 # we need to prepare $formats_nav before almost any parameter munging
Jakub Narebski151602d2006-10-23 00:37:56 +02007791 my $formats_nav;
7792 if ($format eq 'html') {
7793 $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01007794 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007795 "raw");
Jakub Narebski1655c982009-10-09 14:26:44 +02007796 if ($patch_max && @{$co{'parents'}} <= 1) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007797 $formats_nav .= " | " .
7798 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7799 "patch");
7800 }
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007801 $formats_nav .= diff_style_nav($diff_style, @{$co{'parents'}} > 1);
Jakub Narebski151602d2006-10-23 00:37:56 +02007802
Jakub Narebskicd030c32007-06-08 13:33:28 +02007803 if (defined $hash_parent &&
7804 $hash_parent ne '-c' && $hash_parent ne '--cc') {
Jakub Narebski151602d2006-10-23 00:37:56 +02007805 # commitdiff with two commits given
7806 my $hash_parent_short = $hash_parent;
7807 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7808 $hash_parent_short = substr($hash_parent, 0, 7);
7809 }
7810 $formats_nav .=
Jakub Narebskiada3e1f2007-06-08 13:26:31 +02007811 ' (from';
7812 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7813 if ($co{'parents'}[$i] eq $hash_parent) {
7814 $formats_nav .= ' parent ' . ($i+1);
7815 last;
7816 }
7817 }
7818 $formats_nav .= ': ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007819 $cgi->a({-href => href(-replay=>1,
7820 hash=>$hash_parent, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007821 esc_html($hash_parent_short)) .
7822 ')';
7823 } elsif (!$co{'parent'}) {
7824 # --root commitdiff
7825 $formats_nav .= ' (initial)';
7826 } elsif (scalar @{$co{'parents'}} == 1) {
7827 # single parent commit
7828 $formats_nav .=
7829 ' (parent: ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007830 $cgi->a({-href => href(-replay=>1,
7831 hash=>$co{'parent'}, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007832 esc_html(substr($co{'parent'}, 0, 7))) .
7833 ')';
7834 } else {
7835 # merge commit
Jakub Narebskicd030c32007-06-08 13:33:28 +02007836 if ($hash_parent eq '--cc') {
7837 $formats_nav .= ' | ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007838 $cgi->a({-href => href(-replay=>1,
Jakub Narebskicd030c32007-06-08 13:33:28 +02007839 hash=>$hash, hash_parent=>'-c')},
7840 'combined');
7841 } else { # $hash_parent eq '-c'
7842 $formats_nav .= ' | ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007843 $cgi->a({-href => href(-replay=>1,
Jakub Narebskicd030c32007-06-08 13:33:28 +02007844 hash=>$hash, hash_parent=>'--cc')},
7845 'compact');
7846 }
Jakub Narebski151602d2006-10-23 00:37:56 +02007847 $formats_nav .=
7848 ' (merge: ' .
7849 join(' ', map {
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007850 $cgi->a({-href => href(-replay=>1,
7851 hash=>$_, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007852 esc_html(substr($_, 0, 7)));
7853 } @{$co{'parents'}} ) .
7854 ')';
7855 }
7856 }
7857
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007858 my $hash_parent_param = $hash_parent;
Jakub Narebskicd030c32007-06-08 13:33:28 +02007859 if (!defined $hash_parent_param) {
7860 # --cc for multiple parents, --root for parentless
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007861 $hash_parent_param =
Jakub Narebskicd030c32007-06-08 13:33:28 +02007862 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
Kay Sieversbddec012005-08-07 20:25:42 +02007863 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007864
7865 # read commitdiff
7866 my $fd;
7867 my @difftree;
Jakub Narebskieee08902006-08-24 00:15:14 +02007868 if ($format eq 'html') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007869 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007870 "--no-commit-id", "--patch-with-raw", "--full-index",
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007871 $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007872 or die_error(500, "Open git-diff-tree failed");
Jakub Narebskieee08902006-08-24 00:15:14 +02007873
Jakub Narebski04408c32006-11-18 23:35:38 +01007874 while (my $line = <$fd>) {
7875 chomp $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02007876 # empty line ends raw part of diff-tree output
7877 last unless $line;
Jakub Narebski493e01d2007-05-07 01:10:06 +02007878 push @difftree, scalar parse_difftree_raw_line($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02007879 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007880
Jakub Narebskieee08902006-08-24 00:15:14 +02007881 } elsif ($format eq 'plain') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007882 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007883 '-p', $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007884 or die_error(500, "Open git-diff-tree failed");
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007885 } elsif ($format eq 'patch') {
7886 # For commit ranges, we limit the output to the number of
7887 # patches specified in the 'patches' feature.
7888 # For single commits, we limit the output to a single patch,
7889 # diverging from the git-format-patch default.
7890 my @commit_spec = ();
7891 if ($hash_parent) {
7892 if ($patch_max > 0) {
7893 push @commit_spec, "-$patch_max";
7894 }
7895 push @commit_spec, '-n', "$hash_parent..$hash";
7896 } else {
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +01007897 if ($params{-single}) {
7898 push @commit_spec, '-1';
7899 } else {
7900 if ($patch_max > 0) {
7901 push @commit_spec, "-$patch_max";
7902 }
7903 push @commit_spec, "-n";
7904 }
7905 push @commit_spec, '--root', $hash;
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007906 }
Pavan Kumar Sunkara04794fd2010-05-10 18:41:35 +02007907 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
7908 '--encoding=utf8', '--stdout', @commit_spec
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007909 or die_error(500, "Open git-format-patch failed");
Jakub Narebskieee08902006-08-24 00:15:14 +02007910 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007911 die_error(400, "Unknown commitdiff format");
Jakub Narebskieee08902006-08-24 00:15:14 +02007912 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02007913
Kay Sievers11044292005-10-19 03:18:45 +02007914 # non-textual hash id's can be cached
7915 my $expires;
7916 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7917 $expires = "+1d";
7918 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007919
Jakub Narebskieee08902006-08-24 00:15:14 +02007920 # write commit message
7921 if ($format eq 'html') {
7922 my $refs = git_get_references();
7923 my $ref = format_ref_marker($refs, $co{'id'});
Kay Sievers19806692005-08-07 20:26:27 +02007924
Jakub Narebskieee08902006-08-24 00:15:14 +02007925 git_header_html(undef, $expires);
7926 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
7927 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
Giuseppe Bilottaf88bafa2009-06-30 00:00:49 +02007928 print "<div class=\"title_text\">\n" .
7929 "<table class=\"object_header\">\n";
7930 git_print_authorship_rows(\%co);
7931 print "</table>".
7932 "</div>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02007933 print "<div class=\"page_body\">\n";
Jakub Narebski82560982006-10-24 13:55:33 +02007934 if (@{$co{'comment'}} > 1) {
7935 print "<div class=\"log\">\n";
7936 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
7937 print "</div>\n"; # class="log"
7938 }
Kay Sievers1b1cd422005-08-07 20:28:01 +02007939
Jakub Narebskieee08902006-08-24 00:15:14 +02007940 } elsif ($format eq 'plain') {
7941 my $refs = git_get_references("tags");
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007942 my $tagname = git_get_rev_name_tags($hash);
Jakub Narebskieee08902006-08-24 00:15:14 +02007943 my $filename = basename($project) . "-$hash.patch";
7944
7945 print $cgi->header(
7946 -type => 'text/plain',
7947 -charset => 'utf-8',
7948 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07007949 -content_disposition => 'inline; filename="' . "$filename" . '"');
Jakub Narebskieee08902006-08-24 00:15:14 +02007950 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
Yasushi SHOJI77202242008-01-29 21:16:02 +09007951 print "From: " . to_utf8($co{'author'}) . "\n";
7952 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7953 print "Subject: " . to_utf8($co{'title'}) . "\n";
7954
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007955 print "X-Git-Tag: $tagname\n" if $tagname;
Jakub Narebskieee08902006-08-24 00:15:14 +02007956 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007957
Jakub Narebskieee08902006-08-24 00:15:14 +02007958 foreach my $line (@{$co{'comment'}}) {
Yasushi SHOJI77202242008-01-29 21:16:02 +09007959 print to_utf8($line) . "\n";
Kay Sievers19806692005-08-07 20:26:27 +02007960 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007961 print "---\n\n";
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007962 } elsif ($format eq 'patch') {
7963 my $filename = basename($project) . "-$hash.patch";
7964
7965 print $cgi->header(
7966 -type => 'text/plain',
7967 -charset => 'utf-8',
7968 -expires => $expires,
7969 -content_disposition => 'inline; filename="' . "$filename" . '"');
Kay Sievers19806692005-08-07 20:26:27 +02007970 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007971
7972 # write patch
7973 if ($format eq 'html') {
Jakub Narebskicd030c32007-06-08 13:33:28 +02007974 my $use_parents = !defined $hash_parent ||
7975 $hash_parent eq '-c' || $hash_parent eq '--cc';
7976 git_difftree_body(\@difftree, $hash,
7977 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebskib4657e72006-08-28 14:48:14 +02007978 print "<br/>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02007979
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007980 git_patchset_body($fd, $diff_style,
7981 \@difftree, $hash,
Jakub Narebskicd030c32007-06-08 13:33:28 +02007982 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebski157e43b2006-08-24 19:34:36 +02007983 close $fd;
Jakub Narebskieee08902006-08-24 00:15:14 +02007984 print "</div>\n"; # class="page_body"
7985 git_footer_html();
7986
7987 } elsif ($format eq 'plain') {
7988 local $/ = undef;
7989 print <$fd>;
7990 close $fd
7991 or print "Reading git-diff-tree failed\n";
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007992 } elsif ($format eq 'patch') {
7993 local $/ = undef;
7994 print <$fd>;
7995 close $fd
7996 or print "Reading git-format-patch failed\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02007997 }
7998}
7999
8000sub git_commitdiff_plain {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01008001 git_commitdiff(-format => 'plain');
Kay Sievers19806692005-08-07 20:26:27 +02008002}
8003
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01008004# format-patch-style patches
8005sub git_patch {
Jakub Narebski1655c982009-10-09 14:26:44 +02008006 git_commitdiff(-format => 'patch', -single => 1);
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +01008007}
8008
8009sub git_patches {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01008010 git_commitdiff(-format => 'patch');
Kay Sievers09bd7892005-08-07 20:21:23 +02008011}
8012
8013sub git_history {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01008014 git_log_generic('history', \&git_history_body,
8015 $hash_base, $hash_parent_base,
8016 $file_name, $hash);
Kay Sievers161332a2005-08-07 19:49:46 +02008017}
Kay Sievers19806692005-08-07 20:26:27 +02008018
8019sub git_search {
Jakub Narebskie0ca3642011-06-22 17:28:52 +02008020 $searchtype ||= 'commit';
8021
8022 # check if appropriate features are enabled
8023 gitweb_check_feature('search')
8024 or die_error(403, "Search is disabled");
8025 if ($searchtype eq 'pickaxe') {
8026 # pickaxe may take all resources of your box and run for several minutes
8027 # with every query - so decide by yourself how public you make this feature
8028 gitweb_check_feature('pickaxe')
8029 or die_error(403, "Pickaxe search is disabled");
8030 }
8031 if ($searchtype eq 'grep') {
8032 # grep search might be potentially CPU-intensive, too
8033 gitweb_check_feature('grep')
8034 or die_error(403, "Grep search is disabled");
8035 }
8036
Kay Sievers19806692005-08-07 20:26:27 +02008037 if (!defined $searchtext) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02008038 die_error(400, "Text field is empty");
Kay Sievers19806692005-08-07 20:26:27 +02008039 }
8040 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02008041 $hash = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02008042 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02008043 my %co = parse_commit($hash);
Kay Sievers19806692005-08-07 20:26:27 +02008044 if (!%co) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02008045 die_error(404, "Unknown commit object");
Kay Sievers19806692005-08-07 20:26:27 +02008046 }
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00008047 if (!defined $page) {
8048 $page = 0;
8049 }
Jakub Narebski04f7a942006-09-11 00:29:27 +02008050
Jakub Narebski16f20722011-06-22 17:28:53 +02008051 if ($searchtype eq 'commit' ||
8052 $searchtype eq 'author' ||
8053 $searchtype eq 'committer') {
8054 git_search_message(%co);
8055 } elsif ($searchtype eq 'pickaxe') {
8056 git_search_changes(%co);
8057 } elsif ($searchtype eq 'grep') {
8058 git_search_files(%co);
Jakub Narebski1ae05be2011-06-22 17:28:55 +02008059 } else {
8060 die_error(400, "Unknown search type");
Kay Sieversc994d622005-08-07 20:27:18 +02008061 }
Kay Sievers19806692005-08-07 20:26:27 +02008062}
8063
Petr Baudis88ad7292006-10-24 05:15:46 +02008064sub git_search_help {
8065 git_header_html();
8066 git_print_page_nav('','', $hash,$hash,$hash);
8067 print <<EOT;
Petr Baudis0e559912008-02-26 13:22:08 +01008068<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
8069regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
8070the pattern entered is recognized as the POSIX extended
8071<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
8072insensitive).</p>
Petr Baudis88ad7292006-10-24 05:15:46 +02008073<dl>
8074<dt><b>commit</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01008075<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02008076EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08008077 my $have_grep = gitweb_check_feature('grep');
Petr Baudise7738552007-05-17 04:31:12 +02008078 if ($have_grep) {
8079 print <<EOT;
8080<dt><b>grep</b></dt>
8081<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
Petr Baudis0e559912008-02-26 13:22:08 +01008082 a different one) are searched for the given pattern. On large trees, this search can take
8083a while and put some strain on the server, so please use it with some consideration. Note that
8084due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
8085case-sensitive.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02008086EOT
8087 }
8088 print <<EOT;
Petr Baudis88ad7292006-10-24 05:15:46 +02008089<dt><b>author</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01008090<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
Petr Baudis88ad7292006-10-24 05:15:46 +02008091<dt><b>committer</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01008092<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
Petr Baudis88ad7292006-10-24 05:15:46 +02008093EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08008094 my $have_pickaxe = gitweb_check_feature('pickaxe');
Petr Baudis88ad7292006-10-24 05:15:46 +02008095 if ($have_pickaxe) {
8096 print <<EOT;
8097<dt><b>pickaxe</b></dt>
8098<dd>All commits that caused the string to appear or disappear from any file (changes that
8099added, removed or "modified" the string) will be listed. This search can take a while and
Petr Baudis0e559912008-02-26 13:22:08 +01008100takes a lot of strain on the server, so please use it wisely. Note that since you may be
8101interested even in changes just changing the case as well, this search is case sensitive.</dd>
Petr Baudis88ad7292006-10-24 05:15:46 +02008102EOT
8103 }
8104 print "</dl>\n";
8105 git_footer_html();
8106}
8107
Kay Sievers19806692005-08-07 20:26:27 +02008108sub git_shortlog {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01008109 git_log_generic('shortlog', \&git_shortlog_body,
8110 $hash, $hash_parent);
Kay Sievers19806692005-08-07 20:26:27 +02008111}
Jakub Narebski717b8312006-07-31 21:22:15 +02008112
8113## ......................................................................
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008114## feeds (RSS, Atom; OPML)
Jakub Narebski717b8312006-07-31 21:22:15 +02008115
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008116sub git_feed {
8117 my $format = shift || 'atom';
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08008118 my $have_blame = gitweb_check_feature('blame');
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008119
8120 # Atom: http://www.atomenabled.org/developers/syndication/
8121 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
8122 if ($format ne 'rss' && $format ne 'atom') {
Lea Wiemann074afaa2008-06-19 22:03:21 +02008123 die_error(400, "Unknown web feed format");
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008124 }
8125
8126 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
8127 my $head = $hash || 'HEAD';
Jakub Narebski311e5522008-02-26 13:22:06 +01008128 my @commitlist = parse_commits($head, 150, 0, $file_name);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008129
8130 my %latest_commit;
8131 my %latest_date;
8132 my $content_type = "application/$format+xml";
8133 if (defined $cgi->http('HTTP_ACCEPT') &&
8134 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
8135 # browser (feed reader) prefers text/xml
8136 $content_type = 'text/xml';
8137 }
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008138 if (defined($commitlist[0])) {
8139 %latest_commit = %{$commitlist[0]};
Giuseppe Bilottacd956c72009-01-26 12:50:16 +01008140 my $latest_epoch = $latest_commit{'committer_epoch'};
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04008141 exit_if_unmodified_since($latest_epoch);
Dylan Alex Simondebf29d2012-10-11 16:40:35 -04008142 %latest_date = parse_date($latest_epoch, $latest_commit{'committer_tz'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008143 }
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04008144 print $cgi->header(
8145 -type => $content_type,
8146 -charset => 'utf-8',
8147 %latest_date ? (-last_modified => $latest_date{'rfc2822'}) : (),
8148 -status => '200 OK');
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008149
8150 # Optimization: skip generating the body if client asks only
8151 # for Last-Modified date.
8152 return if ($cgi->request_method() eq 'HEAD');
8153
8154 # header variables
8155 my $title = "$site_name - $project/$action";
8156 my $feed_type = 'log';
8157 if (defined $hash) {
8158 $title .= " - '$hash'";
8159 $feed_type = 'branch log';
8160 if (defined $file_name) {
8161 $title .= " :: $file_name";
8162 $feed_type = 'history';
8163 }
8164 } elsif (defined $file_name) {
8165 $title .= " - $file_name";
8166 $feed_type = 'history';
8167 }
8168 $title .= " $feed_type";
Jeff King0f0ecf62012-11-12 16:34:28 -05008169 $title = esc_html($title);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008170 my $descr = git_get_project_description($project);
8171 if (defined $descr) {
8172 $descr = esc_html($descr);
8173 } else {
8174 $descr = "$project " .
8175 ($format eq 'rss' ? 'RSS' : 'Atom') .
8176 " feed";
8177 }
8178 my $owner = git_get_project_owner($project);
8179 $owner = esc_html($owner);
8180
8181 #header
8182 my $alt_url;
8183 if (defined $file_name) {
8184 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
8185 } elsif (defined $hash) {
8186 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
8187 } else {
8188 $alt_url = href(-full=>1, action=>"summary");
8189 }
8190 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
8191 if ($format eq 'rss') {
8192 print <<XML;
Jakub Narebski59b9f612006-08-22 23:42:53 +02008193<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
8194<channel>
Jakub Narebski59b9f612006-08-22 23:42:53 +02008195XML
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008196 print "<title>$title</title>\n" .
8197 "<link>$alt_url</link>\n" .
8198 "<description>$descr</description>\n" .
Giuseppe Bilotta3ac109a2009-01-26 12:50:13 +01008199 "<language>en</language>\n" .
8200 # project owner is responsible for 'editorial' content
8201 "<managingEditor>$owner</managingEditor>\n";
Giuseppe Bilotta1ba68ce2009-01-26 12:50:11 +01008202 if (defined $logo || defined $favicon) {
8203 # prefer the logo to the favicon, since RSS
8204 # doesn't allow both
8205 my $img = esc_url($logo || $favicon);
8206 print "<image>\n" .
8207 "<url>$img</url>\n" .
8208 "<title>$title</title>\n" .
8209 "<link>$alt_url</link>\n" .
8210 "</image>\n";
8211 }
Giuseppe Bilotta0cf31282009-01-26 12:50:14 +01008212 if (%latest_date) {
8213 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
8214 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
8215 }
Giuseppe Bilottaad59a7a2009-01-26 12:50:12 +01008216 print "<generator>gitweb v.$version/$git_version</generator>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008217 } elsif ($format eq 'atom') {
8218 print <<XML;
8219<feed xmlns="http://www.w3.org/2005/Atom">
8220XML
8221 print "<title>$title</title>\n" .
8222 "<subtitle>$descr</subtitle>\n" .
8223 '<link rel="alternate" type="text/html" href="' .
8224 $alt_url . '" />' . "\n" .
8225 '<link rel="self" type="' . $content_type . '" href="' .
8226 $cgi->self_url() . '" />' . "\n" .
8227 "<id>" . href(-full=>1) . "</id>\n" .
8228 # use project owner for feed author
8229 "<author><name>$owner</name></author>\n";
8230 if (defined $favicon) {
8231 print "<icon>" . esc_url($favicon) . "</icon>\n";
8232 }
Jonathan Nieder9d9f5e72010-09-03 19:44:39 -05008233 if (defined $logo) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008234 # not twice as wide as tall: 72 x 27 pixels
Jakub Narebskie1147262006-12-04 14:09:43 +01008235 print "<logo>" . esc_url($logo) . "</logo>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008236 }
8237 if (! %latest_date) {
8238 # dummy date to keep the feed valid until commits trickle in:
8239 print "<updated>1970-01-01T00:00:00Z</updated>\n";
8240 } else {
8241 print "<updated>$latest_date{'iso-8601'}</updated>\n";
8242 }
Giuseppe Bilottaad59a7a2009-01-26 12:50:12 +01008243 print "<generator version='$version/$git_version'>gitweb</generator>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008244 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008245
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008246 # contents
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008247 for (my $i = 0; $i <= $#commitlist; $i++) {
8248 my %co = %{$commitlist[$i]};
8249 my $commit = $co{'id'};
Jakub Narebski717b8312006-07-31 21:22:15 +02008250 # we read 150, we always show 30 and the ones more recent than 48 hours
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01008251 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02008252 last;
8253 }
Jakub Narebski6368d9f2011-03-19 23:53:55 +01008254 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008255
8256 # get list of changed files
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008257 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskic906b182007-05-19 02:47:51 +02008258 $co{'parent'} || "--root",
8259 $co{'id'}, "--", (defined $file_name ? $file_name : ())
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02008260 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02008261 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02008262 close $fd
8263 or next;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008264
8265 # print element (entry, item)
Florian La Rochee62a6412008-02-03 12:38:46 +01008266 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008267 if ($format eq 'rss') {
8268 print "<item>\n" .
8269 "<title>" . esc_html($co{'title'}) . "</title>\n" .
8270 "<author>" . esc_html($co{'author'}) . "</author>\n" .
8271 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
8272 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
8273 "<link>$co_url</link>\n" .
8274 "<description>" . esc_html($co{'title'}) . "</description>\n" .
8275 "<content:encoded>" .
8276 "<![CDATA[\n";
8277 } elsif ($format eq 'atom') {
8278 print "<entry>\n" .
8279 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
8280 "<updated>$cd{'iso-8601'}</updated>\n" .
Jakub Narebskiab23c192006-11-25 15:54:33 +01008281 "<author>\n" .
8282 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
8283 if ($co{'author_email'}) {
8284 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
8285 }
8286 print "</author>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008287 # use committer for contributor
Jakub Narebskiab23c192006-11-25 15:54:33 +01008288 "<contributor>\n" .
8289 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
8290 if ($co{'committer_email'}) {
8291 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
8292 }
8293 print "</contributor>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008294 "<published>$cd{'iso-8601'}</published>\n" .
8295 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
8296 "<id>$co_url</id>\n" .
8297 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
8298 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
8299 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008300 my $comment = $co{'comment'};
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008301 print "<pre>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008302 foreach my $line (@$comment) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008303 $line = esc_html($line);
8304 print "$line\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008305 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008306 print "</pre><ul>\n";
8307 foreach my $difftree_line (@difftree) {
8308 my %difftree = parse_difftree_raw_line($difftree_line);
8309 next if !$difftree{'from_id'};
8310
8311 my $file = $difftree{'file'} || $difftree{'to_file'};
8312
8313 print "<li>" .
8314 "[" .
8315 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
8316 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
8317 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
8318 file_name=>$file, file_parent=>$difftree{'from_file'}),
8319 -title => "diff"}, 'D');
8320 if ($have_blame) {
8321 print $cgi->a({-href => href(-full=>1, action=>"blame",
8322 file_name=>$file, hash_base=>$commit),
8323 -title => "blame"}, 'B');
Jakub Narebski717b8312006-07-31 21:22:15 +02008324 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008325 # if this is not a feed of a file history
8326 if (!defined $file_name || $file_name ne $file) {
8327 print $cgi->a({-href => href(-full=>1, action=>"history",
8328 file_name=>$file, hash=>$commit),
8329 -title => "history"}, 'H');
8330 }
8331 $file = esc_path($file);
8332 print "] ".
8333 "$file</li>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008334 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008335 if ($format eq 'rss') {
8336 print "</ul>]]>\n" .
8337 "</content:encoded>\n" .
8338 "</item>\n";
8339 } elsif ($format eq 'atom') {
8340 print "</ul>\n</div>\n" .
8341 "</content>\n" .
8342 "</entry>\n";
8343 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008344 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008345
8346 # end of feed
8347 if ($format eq 'rss') {
8348 print "</channel>\n</rss>\n";
Jakub Narebski3278fbc2009-05-11 19:37:28 +02008349 } elsif ($format eq 'atom') {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008350 print "</feed>\n";
8351 }
8352}
8353
8354sub git_rss {
8355 git_feed('rss');
8356}
8357
8358sub git_atom {
8359 git_feed('atom');
Jakub Narebski717b8312006-07-31 21:22:15 +02008360}
8361
8362sub git_opml {
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008363 my @list = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski12b14432011-04-29 19:51:56 +02008364 if (!@list) {
8365 die_error(404, "No projects found");
8366 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008367
Giuseppe Bilottaae357852009-01-02 13:49:30 +01008368 print $cgi->header(
8369 -type => 'text/xml',
8370 -charset => 'utf-8',
8371 -content_disposition => 'inline; filename="opml.xml"');
8372
Jürgen Kreileder5d791052011-12-17 10:22:22 +01008373 my $title = esc_html($site_name);
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008374 my $filter = " within subdirectory ";
8375 if (defined $project_filter) {
8376 $filter .= esc_html($project_filter);
8377 } else {
8378 $filter = "";
8379 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02008380 print <<XML;
8381<?xml version="1.0" encoding="utf-8"?>
8382<opml version="1.0">
8383<head>
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008384 <title>$title OPML Export$filter</title>
Jakub Narebski59b9f612006-08-22 23:42:53 +02008385</head>
8386<body>
8387<outline text="git RSS feeds">
8388XML
Jakub Narebski717b8312006-07-31 21:22:15 +02008389
8390 foreach my $pr (@list) {
8391 my %proj = %$pr;
Jakub Narebski847e01f2006-08-14 02:05:47 +02008392 my $head = git_get_head_hash($proj{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02008393 if (!defined $head) {
8394 next;
8395 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02008396 $git_dir = "$projectroot/$proj{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02008397 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02008398 if (!%co) {
8399 next;
8400 }
8401
8402 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
Giuseppe Bilottadf63fbb2009-01-02 13:15:28 +01008403 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
8404 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
Jakub Narebski717b8312006-07-31 21:22:15 +02008405 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
8406 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02008407 print <<XML;
8408</outline>
8409</body>
8410</opml>
8411XML
Jakub Narebski717b8312006-07-31 21:22:15 +02008412}