blob: 3d4a8ee27c96a8e1eb94f95771aeca7825f8a076 [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
Ævar Arnfjörð Bjarmason26547bf2016-10-06 09:11:33 +00001619# Sanitize for use in XHTML + application/xml+xhtml (valid XML 1.0)
Jakub Narebski08667862011-09-16 14:41:57 +02001620sub 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);
Ævar Arnfjörð Bjarmasoncf5c7252016-10-06 09:11:35 +00002039 $line =~ s{
2040 \b
2041 (
2042 # The output of "git describe", e.g. v2.10.0-297-gf6727b0
2043 # or hadoop-20160921-113441-20-g094fb7d
2044 (?<!-) # see strbuf_check_tag_ref(). Tags can't start with -
2045 [A-Za-z0-9.-]+
2046 (?!\.) # refs can't end with ".", see check_refname_format()
2047 -g[0-9a-fA-F]{7,40}
2048 |
2049 # Just a normal looking Git SHA1
2050 [0-9a-fA-F]{7,40}
2051 )
2052 \b
2053 }{
Marcel M. Cary7d233de2009-02-17 19:00:43 -08002054 $cgi->a({-href => href(action=>"object", hash=>$1),
2055 -class => "text"}, $1);
Ævar Arnfjörð Bjarmasoncf5c7252016-10-06 09:11:35 +00002056 }egx;
Marcel M. Cary7d233de2009-02-17 19:00:43 -08002057
Jakub Narebski717b8312006-07-31 21:22:15 +02002058 return $line;
2059}
2060
2061# format marker of refs pointing to given object
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002062
2063# the destination action is chosen based on object type and current context:
2064# - for annotated tags, we choose the tag view unless it's the current view
2065# already, in which case we go to shortlog view
2066# - for other refs, we keep the current view if we're in history, shortlog or
2067# log view, and select shortlog otherwise
Jakub Narebski847e01f2006-08-14 02:05:47 +02002068sub format_ref_marker {
Jakub Narebski717b8312006-07-31 21:22:15 +02002069 my ($refs, $id) = @_;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002070 my $markers = '';
Jakub Narebski717b8312006-07-31 21:22:15 +02002071
2072 if (defined $refs->{$id}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002073 foreach my $ref (@{$refs->{$id}}) {
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002074 # this code exploits the fact that non-lightweight tags are the
2075 # only indirect objects, and that they are the only objects for which
2076 # we want to use tag instead of shortlog as action
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002077 my ($type, $name) = qw();
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002078 my $indirect = ($ref =~ s/\^\{\}$//);
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002079 # e.g. tags/v2.6.11 or heads/next
2080 if ($ref =~ m!^(.*?)s?/(.*)$!) {
2081 $type = $1;
2082 $name = $2;
2083 } else {
2084 $type = "ref";
2085 $name = $ref;
2086 }
2087
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002088 my $class = $type;
2089 $class .= " indirect" if $indirect;
2090
2091 my $dest_action = "shortlog";
2092
2093 if ($indirect) {
2094 $dest_action = "tag" unless $action eq "tag";
2095 } elsif ($action =~ /^(history|(short)?log)$/) {
2096 $dest_action = $action;
2097 }
2098
2099 my $dest = "";
2100 $dest .= "refs/" unless $ref =~ m!^refs/!;
2101 $dest .= $ref;
2102
2103 my $link = $cgi->a({
2104 -href => href(
2105 action=>$dest_action,
2106 hash=>$dest
Andreas Brauchli77947bb2016-07-29 16:49:37 +02002107 )}, esc_html($name));
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002108
Jakub Narebski3017ed62010-12-15 00:34:01 +01002109 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002110 $link . "</span>";
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002111 }
2112 }
2113
2114 if ($markers) {
2115 return ' <span class="refs">'. $markers . '</span>';
Jakub Narebski717b8312006-07-31 21:22:15 +02002116 } else {
2117 return "";
2118 }
2119}
2120
Jakub Narebski17d07442006-08-14 02:08:27 +02002121# format, perhaps shortened and with markers, title line
2122sub format_subject_html {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002123 my ($long, $short, $href, $extra) = @_;
Jakub Narebski17d07442006-08-14 02:08:27 +02002124 $extra = '' unless defined($extra);
2125
2126 if (length($short) < length($long)) {
Jakub Narebski14afe772009-05-22 17:35:46 +02002127 $long =~ s/[[:cntrl:]]/?/g;
Jakub Narebski7c278012006-08-22 12:02:48 +02002128 return $cgi->a({-href => $href, -class => "list subject",
Martin Koegler00f429a2007-06-03 17:42:44 +02002129 -title => to_utf8($long)},
Giuseppe Bilotta01b89f02009-08-23 10:28:09 +02002130 esc_html($short)) . $extra;
Jakub Narebski17d07442006-08-14 02:08:27 +02002131 } else {
Jakub Narebski7c278012006-08-22 12:02:48 +02002132 return $cgi->a({-href => $href, -class => "list subject"},
Giuseppe Bilotta01b89f02009-08-23 10:28:09 +02002133 esc_html($long)) . $extra;
Jakub Narebski17d07442006-08-14 02:08:27 +02002134 }
2135}
2136
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002137# Rather than recomputing the url for an email multiple times, we cache it
2138# after the first hit. This gives a visible benefit in views where the avatar
2139# for the same email is used repeatedly (e.g. shortlog).
2140# The cache is shared by all avatar engines (currently gravatar only), which
2141# are free to use it as preferred. Since only one avatar engine is used for any
2142# given page, there's no risk for cache conflicts.
2143our %avatar_cache = ();
2144
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002145# Compute the picon url for a given email, by using the picon search service over at
2146# http://www.cs.indiana.edu/picons/search.html
2147sub picon_url {
2148 my $email = lc shift;
2149 if (!$avatar_cache{$email}) {
2150 my ($user, $domain) = split('@', $email);
2151 $avatar_cache{$email} =
Andrej E Baranov57485582013-01-29 00:41:32 +01002152 "//www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002153 "$domain/$user/" .
2154 "users+domains+unknown/up/single";
2155 }
2156 return $avatar_cache{$email};
2157}
2158
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002159# Compute the gravatar url for a given email, if it's not in the cache already.
2160# Gravatar stores only the part of the URL before the size, since that's the
2161# one computationally more expensive. This also allows reuse of the cache for
2162# different sizes (for this particular engine).
2163sub gravatar_url {
2164 my $email = lc shift;
2165 my $size = shift;
2166 $avatar_cache{$email} ||=
Andrej E Baranov57485582013-01-29 00:41:32 +01002167 "//www.gravatar.com/avatar/" .
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002168 Digest::MD5::md5_hex($email) . "?s=";
2169 return $avatar_cache{$email} . $size;
2170}
2171
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002172# Insert an avatar for the given $email at the given $size if the feature
2173# is enabled.
2174sub git_get_avatar {
2175 my ($email, %opts) = @_;
2176 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
2177 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
2178 $opts{-size} ||= 'default';
2179 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
2180 my $url = "";
2181 if ($git_avatar eq 'gravatar') {
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002182 $url = gravatar_url($email, $size);
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002183 } elsif ($git_avatar eq 'picon') {
2184 $url = picon_url($email);
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002185 }
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002186 # Other providers can be added by extending the if chain, defining $url
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002187 # as needed. If no variant puts something in $url, we assume avatars
2188 # are completely disabled/unavailable.
2189 if ($url) {
2190 return $pre_white .
2191 "<img width=\"$size\" " .
2192 "class=\"avatar\" " .
Jakub Narebski3017ed62010-12-15 00:34:01 +01002193 "src=\"".esc_url($url)."\" " .
Giuseppe Bilotta7d25ef42009-06-30 00:00:54 +02002194 "alt=\"\" " .
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002195 "/>" . $post_white;
2196 } else {
2197 return "";
2198 }
2199}
2200
Stephen Boyde133d652009-10-15 21:14:59 -07002201sub format_search_author {
2202 my ($author, $searchtype, $displaytext) = @_;
2203 my $have_search = gitweb_check_feature('search');
2204
2205 if ($have_search) {
2206 my $performed = "";
2207 if ($searchtype eq 'author') {
2208 $performed = "authored";
2209 } elsif ($searchtype eq 'committer') {
2210 $performed = "committed";
2211 }
2212
2213 return $cgi->a({-href => href(action=>"search", hash=>$hash,
2214 searchtext=>$author,
2215 searchtype=>$searchtype), class=>"list",
2216 title=>"Search for commits $performed by $author"},
2217 $displaytext);
2218
2219 } else {
2220 return $displaytext;
2221 }
2222}
2223
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02002224# format the author name of the given commit with the given tag
2225# the author name is chopped and escaped according to the other
2226# optional parameters (see chop_str).
2227sub format_author_html {
2228 my $tag = shift;
2229 my $co = shift;
2230 my $author = chop_and_escape_str($co->{'author_name'}, @_);
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002231 return "<$tag class=\"author\">" .
Stephen Boyde133d652009-10-15 21:14:59 -07002232 format_search_author($co->{'author_name'}, "author",
2233 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2234 $author) .
2235 "</$tag>";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02002236}
2237
Jakub Narebski90921742007-06-08 13:27:42 +02002238# format git diff header line, i.e. "diff --(git|combined|cc) ..."
2239sub format_git_diff_header_line {
2240 my $line = shift;
2241 my $diffinfo = shift;
2242 my ($from, $to) = @_;
2243
2244 if ($diffinfo->{'nparents'}) {
2245 # combined diff
2246 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2247 if ($to->{'href'}) {
2248 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2249 esc_path($to->{'file'}));
2250 } else { # file was deleted (no href)
2251 $line .= esc_path($to->{'file'});
2252 }
2253 } else {
2254 # "ordinary" diff
2255 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2256 if ($from->{'href'}) {
2257 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2258 'a/' . esc_path($from->{'file'}));
2259 } else { # file was added (no href)
2260 $line .= 'a/' . esc_path($from->{'file'});
2261 }
2262 $line .= ' ';
2263 if ($to->{'href'}) {
2264 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2265 'b/' . esc_path($to->{'file'}));
2266 } else { # file was deleted
2267 $line .= 'b/' . esc_path($to->{'file'});
2268 }
2269 }
2270
2271 return "<div class=\"diff header\">$line</div>\n";
2272}
2273
2274# format extended diff header line, before patch itself
2275sub format_extended_diff_header_line {
2276 my $line = shift;
2277 my $diffinfo = shift;
2278 my ($from, $to) = @_;
2279
2280 # match <path>
2281 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2282 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2283 esc_path($from->{'file'}));
2284 }
2285 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2286 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2287 esc_path($to->{'file'}));
2288 }
2289 # match single <mode>
2290 if ($line =~ m/\s(\d{6})$/) {
2291 $line .= '<span class="info"> (' .
2292 file_type_long($1) .
2293 ')</span>';
2294 }
2295 # match <hash>
2296 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2297 # can match only for combined diff
2298 $line = 'index ';
2299 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2300 if ($from->{'href'}[$i]) {
2301 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2302 -class=>"hash"},
2303 substr($diffinfo->{'from_id'}[$i],0,7));
2304 } else {
2305 $line .= '0' x 7;
2306 }
2307 # separator
2308 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2309 }
2310 $line .= '..';
2311 if ($to->{'href'}) {
2312 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2313 substr($diffinfo->{'to_id'},0,7));
2314 } else {
2315 $line .= '0' x 7;
2316 }
2317
2318 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2319 # can match only for ordinary diff
2320 my ($from_link, $to_link);
2321 if ($from->{'href'}) {
2322 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2323 substr($diffinfo->{'from_id'},0,7));
2324 } else {
2325 $from_link = '0' x 7;
2326 }
2327 if ($to->{'href'}) {
2328 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2329 substr($diffinfo->{'to_id'},0,7));
2330 } else {
2331 $to_link = '0' x 7;
2332 }
2333 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2334 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2335 }
2336
2337 return $line . "<br/>\n";
2338}
2339
2340# format from-file/to-file diff header
2341sub format_diff_from_to_header {
Jakub Narebski91af4ce2007-06-08 13:32:44 +02002342 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
Jakub Narebski90921742007-06-08 13:27:42 +02002343 my $line;
2344 my $result = '';
2345
2346 $line = $from_line;
2347 #assert($line =~ m/^---/) if DEBUG;
Jakub Narebskideaa01a2007-06-08 13:29:49 +02002348 # no extra formatting for "^--- /dev/null"
2349 if (! $diffinfo->{'nparents'}) {
2350 # ordinary (single parent) diff
2351 if ($line =~ m!^--- "?a/!) {
2352 if ($from->{'href'}) {
2353 $line = '--- a/' .
2354 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2355 esc_path($from->{'file'}));
2356 } else {
2357 $line = '--- a/' .
2358 esc_path($from->{'file'});
2359 }
2360 }
2361 $result .= qq!<div class="diff from_file">$line</div>\n!;
2362
2363 } else {
2364 # combined diff (merge commit)
2365 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2366 if ($from->{'href'}[$i]) {
2367 $line = '--- ' .
Jakub Narebski91af4ce2007-06-08 13:32:44 +02002368 $cgi->a({-href=>href(action=>"blobdiff",
2369 hash_parent=>$diffinfo->{'from_id'}[$i],
2370 hash_parent_base=>$parents[$i],
2371 file_parent=>$from->{'file'}[$i],
2372 hash=>$diffinfo->{'to_id'},
2373 hash_base=>$hash,
2374 file_name=>$to->{'file'}),
2375 -class=>"path",
2376 -title=>"diff" . ($i+1)},
2377 $i+1) .
2378 '/' .
Jakub Narebskideaa01a2007-06-08 13:29:49 +02002379 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2380 esc_path($from->{'file'}[$i]));
2381 } else {
2382 $line = '--- /dev/null';
2383 }
2384 $result .= qq!<div class="diff from_file">$line</div>\n!;
Jakub Narebski90921742007-06-08 13:27:42 +02002385 }
2386 }
Jakub Narebski90921742007-06-08 13:27:42 +02002387
2388 $line = $to_line;
2389 #assert($line =~ m/^\+\+\+/) if DEBUG;
2390 # no extra formatting for "^+++ /dev/null"
2391 if ($line =~ m!^\+\+\+ "?b/!) {
2392 if ($to->{'href'}) {
2393 $line = '+++ b/' .
2394 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2395 esc_path($to->{'file'}));
2396 } else {
2397 $line = '+++ b/' .
2398 esc_path($to->{'file'});
2399 }
2400 }
2401 $result .= qq!<div class="diff to_file">$line</div>\n!;
2402
2403 return $result;
2404}
2405
Jakub Narebskicd030c32007-06-08 13:33:28 +02002406# create note for patch simplified by combined diff
2407sub format_diff_cc_simplified {
2408 my ($diffinfo, @parents) = @_;
2409 my $result = '';
2410
2411 $result .= "<div class=\"diff header\">" .
2412 "diff --cc ";
2413 if (!is_deleted($diffinfo)) {
2414 $result .= $cgi->a({-href => href(action=>"blob",
2415 hash_base=>$hash,
2416 hash=>$diffinfo->{'to_id'},
2417 file_name=>$diffinfo->{'to_file'}),
2418 -class => "path"},
2419 esc_path($diffinfo->{'to_file'}));
2420 } else {
2421 $result .= esc_path($diffinfo->{'to_file'});
2422 }
2423 $result .= "</div>\n" . # class="diff header"
2424 "<div class=\"diff nodifferences\">" .
2425 "Simple merge" .
2426 "</div>\n"; # class="diff nodifferences"
2427
2428 return $result;
2429}
2430
Jakub Narebski20a864c2011-10-31 00:36:20 +01002431sub diff_line_class {
2432 my ($line, $from, $to) = @_;
2433
2434 # ordinary diff
2435 my $num_sign = 1;
2436 # combined diff
2437 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2438 $num_sign = scalar @{$from->{'href'}};
2439 }
2440
2441 my @diff_line_classifier = (
2442 { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
2443 { regexp => qr/^\\/, class => "incomplete" },
2444 { regexp => qr/^ {$num_sign}/, class => "ctx" },
2445 # classifier for context must come before classifier add/rem,
2446 # or we would have to use more complicated regexp, for example
2447 # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
2448 { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
2449 { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
2450 );
2451 for my $clsfy (@diff_line_classifier) {
2452 return $clsfy->{'class'}
2453 if ($line =~ $clsfy->{'regexp'});
2454 }
2455
2456 # fallback
2457 return "";
2458}
2459
Jakub Narebskif1310cf2011-10-31 00:36:21 +01002460# assumes that $from and $to are defined and correctly filled,
2461# and that $line holds a line of chunk header for unified diff
2462sub format_unidiff_chunk_header {
2463 my ($line, $from, $to) = @_;
2464
2465 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2466 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2467
2468 $from_lines = 0 unless defined $from_lines;
2469 $to_lines = 0 unless defined $to_lines;
2470
2471 if ($from->{'href'}) {
2472 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2473 -class=>"list"}, $from_text);
2474 }
2475 if ($to->{'href'}) {
2476 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2477 -class=>"list"}, $to_text);
2478 }
2479 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2480 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2481 return $line;
2482}
2483
2484# assumes that $from and $to are defined and correctly filled,
2485# and that $line holds a line of chunk header for combined diff
2486sub format_cc_diff_chunk_header {
2487 my ($line, $from, $to) = @_;
2488
2489 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2490 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2491
2492 @from_text = split(' ', $ranges);
2493 for (my $i = 0; $i < @from_text; ++$i) {
2494 ($from_start[$i], $from_nlines[$i]) =
2495 (split(',', substr($from_text[$i], 1)), 0);
2496 }
2497
2498 $to_text = pop @from_text;
2499 $to_start = pop @from_start;
2500 $to_nlines = pop @from_nlines;
2501
2502 $line = "<span class=\"chunk_info\">$prefix ";
2503 for (my $i = 0; $i < @from_text; ++$i) {
2504 if ($from->{'href'}[$i]) {
2505 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2506 -class=>"list"}, $from_text[$i]);
2507 } else {
2508 $line .= $from_text[$i];
2509 }
2510 $line .= " ";
2511 }
2512 if ($to->{'href'}) {
2513 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2514 -class=>"list"}, $to_text);
2515 } else {
2516 $line .= $to_text;
2517 }
2518 $line .= " $prefix</span>" .
2519 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2520 return $line;
2521}
2522
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01002523# process patch (diff) line (not to be used for diff headers),
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002524# returning HTML-formatted (but not wrapped) line.
2525# If the line is passed as a reference, it is treated as HTML and not
2526# esc_html()'ed.
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002527sub format_diff_line {
2528 my ($line, $diff_class, $from, $to) = @_;
Jakub Narebski20a864c2011-10-31 00:36:20 +01002529
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002530 if (ref($line)) {
2531 $line = $$line;
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002532 } else {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002533 chomp $line;
2534 $line = untabify($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02002535
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002536 if ($from && $to && $line =~ m/^\@{2} /) {
2537 $line = format_unidiff_chunk_header($line, $from, $to);
2538 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2539 $line = format_cc_diff_chunk_header($line, $from, $to);
2540 } else {
2541 $line = esc_html($line, -nbsp=>1);
2542 }
Jakub Narebski59e3b142006-11-18 23:35:40 +01002543 }
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002544
2545 my $diff_classes = "diff";
2546 $diff_classes .= " $diff_class" if ($diff_class);
2547 $line = "<div class=\"$diff_classes\">$line</div>\n";
2548
2549 return $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02002550}
2551
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002552# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2553# linked. Pass the hash of the tree/commit to snapshot.
2554sub format_snapshot_links {
2555 my ($hash) = @_;
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002556 my $num_fmts = @snapshot_fmts;
2557 if ($num_fmts > 1) {
2558 # A parenthesized list of links bearing format names.
Jakub Narebskia7817852007-07-22 23:41:20 +02002559 # e.g. "snapshot (_tar.gz_ _zip_)"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002560 return "snapshot (" . join(' ', map
2561 $cgi->a({
2562 -href => href(
2563 action=>"snapshot",
2564 hash=>$hash,
2565 snapshot_format=>$_
2566 )
2567 }, $known_snapshot_formats{$_}{'display'})
2568 , @snapshot_fmts) . ")";
2569 } elsif ($num_fmts == 1) {
2570 # A single "snapshot" link whose tooltip bears the format name.
Jakub Narebskia7817852007-07-22 23:41:20 +02002571 # i.e. "_snapshot_"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002572 my ($fmt) = @snapshot_fmts;
Jakub Narebskia7817852007-07-22 23:41:20 +02002573 return
2574 $cgi->a({
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002575 -href => href(
2576 action=>"snapshot",
2577 hash=>$hash,
2578 snapshot_format=>$fmt
2579 ),
2580 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2581 }, "snapshot");
2582 } else { # $num_fmts == 0
2583 return undef;
2584 }
2585}
2586
Jakub Narebski35621982008-04-20 22:09:48 +02002587## ......................................................................
2588## functions returning values to be passed, perhaps after some
2589## transformation, to other functions; e.g. returning arguments to href()
2590
2591# returns hash to be passed to href to generate gitweb URL
2592# in -title key it returns description of link
2593sub get_feed_info {
2594 my $format = shift || 'Atom';
2595 my %res = (action => lc($format));
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01002596 my $matched_ref = 0;
Jakub Narebski35621982008-04-20 22:09:48 +02002597
2598 # feed links are possible only for project views
2599 return unless (defined $project);
2600 # some views should link to OPML, or to generic project feed,
2601 # or don't have specific feed yet (so they should use generic)
Jakub Narebski18ab83e2012-01-07 11:47:38 +01002602 return if (!$action || $action =~ /^(?:tags|heads|forks|tag|search)$/x);
Jakub Narebski35621982008-04-20 22:09:48 +02002603
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01002604 my $branch = undef;
2605 # branches refs uses 'refs/' + $get_branch_refs()[x] + '/' prefix
2606 # (fullname) to differentiate from tag links; this also makes
2607 # possible to detect branch links
2608 for my $ref (get_branch_refs()) {
2609 if ((defined $hash_base && $hash_base =~ m!^refs/\Q$ref\E/(.*)$!) ||
2610 (defined $hash && $hash =~ m!^refs/\Q$ref\E/(.*)$!)) {
2611 $branch = $1;
2612 $matched_ref = $ref;
2613 last;
2614 }
Jakub Narebski35621982008-04-20 22:09:48 +02002615 }
2616 # find log type for feed description (title)
2617 my $type = 'log';
2618 if (defined $file_name) {
2619 $type = "history of $file_name";
2620 $type .= "/" if ($action eq 'tree');
2621 $type .= " on '$branch'" if (defined $branch);
2622 } else {
2623 $type = "log of $branch" if (defined $branch);
2624 }
2625
2626 $res{-title} = $type;
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01002627 $res{'hash'} = (defined $branch ? "refs/$matched_ref/$branch" : undef);
Jakub Narebski35621982008-04-20 22:09:48 +02002628 $res{'file_name'} = $file_name;
2629
2630 return %res;
2631}
2632
Jakub Narebski717b8312006-07-31 21:22:15 +02002633## ----------------------------------------------------------------------
2634## git utility subroutines, invoking git commands
2635
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002636# returns path to the core git executable and the --git-dir parameter as list
2637sub git_cmd {
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02002638 $number_of_git_cmds++;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002639 return $GIT, '--git-dir='.$git_dir;
2640}
2641
Lea Wiemann516381d2008-06-17 23:46:35 +02002642# quote the given arguments for passing them to the shell
2643# quote_command("command", "arg 1", "arg with ' and ! characters")
2644# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2645# Try to avoid using this function wherever possible.
2646sub quote_command {
2647 return join(' ',
Jakub Narebski68cedb12009-05-10 02:40:37 +02002648 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002649}
2650
Jakub Narebski717b8312006-07-31 21:22:15 +02002651# get HEAD ref of given project as hash
Jakub Narebski847e01f2006-08-14 02:05:47 +02002652sub git_get_head_hash {
Mark Radab6292752009-11-07 16:13:29 +01002653 return git_get_full_hash(shift, 'HEAD');
2654}
2655
2656sub git_get_full_hash {
2657 return git_get_hash(@_);
2658}
2659
2660sub git_get_short_hash {
2661 return git_get_hash(@_, '--short=7');
2662}
2663
2664sub git_get_hash {
2665 my ($project, $hash, @options) = @_;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002666 my $o_git_dir = $git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02002667 my $retval = undef;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002668 $git_dir = "$projectroot/$project";
Mark Radab6292752009-11-07 16:13:29 +01002669 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2670 '--verify', '-q', @options, $hash) {
2671 $retval = <$fd>;
2672 chomp $retval if defined $retval;
Jakub Narebski717b8312006-07-31 21:22:15 +02002673 close $fd;
Jakub Narebski717b8312006-07-31 21:22:15 +02002674 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002675 if (defined $o_git_dir) {
2676 $git_dir = $o_git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02002677 }
2678 return $retval;
2679}
2680
2681# get type of given object
2682sub git_get_type {
2683 my $hash = shift;
2684
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002685 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02002686 my $type = <$fd>;
2687 close $fd or return;
2688 chomp $type;
2689 return $type;
2690}
2691
Jakub Narebskib2019272007-11-03 00:41:19 +01002692# repository configuration
2693our $config_file = '';
2694our %config;
2695
2696# store multiple values for single key as anonymous array reference
2697# single values stored directly in the hash, not as [ <value> ]
2698sub hash_set_multi {
2699 my ($hash, $key, $value) = @_;
2700
2701 if (!exists $hash->{$key}) {
2702 $hash->{$key} = $value;
2703 } elsif (!ref $hash->{$key}) {
2704 $hash->{$key} = [ $hash->{$key}, $value ];
2705 } else {
2706 push @{$hash->{$key}}, $value;
2707 }
2708}
2709
2710# return hash of git project configuration
2711# optionally limited to some section, e.g. 'gitweb'
2712sub git_parse_project_config {
2713 my $section_regexp = shift;
2714 my %config;
2715
2716 local $/ = "\0";
2717
2718 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2719 or return;
2720
2721 while (my $keyval = <$fh>) {
2722 chomp $keyval;
2723 my ($key, $value) = split(/\n/, $keyval, 2);
2724
2725 hash_set_multi(\%config, $key, $value)
2726 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2727 }
2728 close $fh;
2729
2730 return %config;
2731}
2732
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002733# convert config value to boolean: 'true' or 'false'
Jakub Narebskib2019272007-11-03 00:41:19 +01002734# no value, number > 0, 'true' and 'yes' values are true
2735# rest of values are treated as false (never as error)
2736sub config_to_bool {
2737 my $val = shift;
2738
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002739 return 1 if !defined $val; # section.key
2740
Jakub Narebskib2019272007-11-03 00:41:19 +01002741 # strip leading and trailing whitespace
2742 $val =~ s/^\s+//;
2743 $val =~ s/\s+$//;
2744
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002745 return (($val =~ /^\d+$/ && $val) || # section.key = 1
Jakub Narebskib2019272007-11-03 00:41:19 +01002746 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2747}
2748
2749# convert config value to simple decimal number
2750# an optional value suffix of 'k', 'm', or 'g' will cause the value
2751# to be multiplied by 1024, 1048576, or 1073741824
2752sub config_to_int {
2753 my $val = shift;
2754
2755 # strip leading and trailing whitespace
2756 $val =~ s/^\s+//;
2757 $val =~ s/\s+$//;
2758
2759 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2760 $unit = lc($unit);
2761 # unknown unit is treated as 1
2762 return $num * ($unit eq 'g' ? 1073741824 :
2763 $unit eq 'm' ? 1048576 :
2764 $unit eq 'k' ? 1024 : 1);
2765 }
2766 return $val;
2767}
2768
2769# convert config value to array reference, if needed
2770sub config_to_multi {
2771 my $val = shift;
2772
Jakub Narebskid76a5852007-12-20 10:48:09 +01002773 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
Jakub Narebskib2019272007-11-03 00:41:19 +01002774}
2775
Jakub Narebski717b8312006-07-31 21:22:15 +02002776sub git_get_project_config {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302777 my ($key, $type) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02002778
Jakub Narebski7a49c252010-03-27 20:26:59 +01002779 return unless defined $git_dir;
Jakub Narebski9be36142010-03-01 22:51:34 +01002780
Jakub Narebskib2019272007-11-03 00:41:19 +01002781 # key sanity check
Jakub Narebski717b8312006-07-31 21:22:15 +02002782 return unless ($key);
Jakub Narebski14569cd2011-07-28 23:38:03 +02002783 # only subsection, if exists, is case sensitive,
2784 # and not lowercased by 'git config -z -l'
2785 if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
Phil Pennockaf507942012-11-05 18:50:47 -05002786 $lo =~ s/_//g;
Jakub Narebski14569cd2011-07-28 23:38:03 +02002787 $key = join(".", lc($hi), $mi, lc($lo));
Phil Pennockaf507942012-11-05 18:50:47 -05002788 return if ($lo =~ /\W/ || $hi =~ /\W/);
Jakub Narebski14569cd2011-07-28 23:38:03 +02002789 } else {
2790 $key = lc($key);
Phil Pennockaf507942012-11-05 18:50:47 -05002791 $key =~ s/_//g;
2792 return if ($key =~ /\W/);
Jakub Narebski14569cd2011-07-28 23:38:03 +02002793 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002794 $key =~ s/^gitweb\.//;
Jakub Narebski717b8312006-07-31 21:22:15 +02002795
Jakub Narebskib2019272007-11-03 00:41:19 +01002796 # type sanity check
2797 if (defined $type) {
2798 $type =~ s/^--//;
2799 $type = undef
2800 unless ($type eq 'bool' || $type eq 'int');
2801 }
2802
2803 # get config
2804 if (!defined $config_file ||
2805 $config_file ne "$git_dir/config") {
2806 %config = git_parse_project_config('gitweb');
2807 $config_file = "$git_dir/config";
2808 }
2809
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002810 # check if config variable (key) exists
2811 return unless exists $config{"gitweb.$key"};
2812
Jakub Narebskib2019272007-11-03 00:41:19 +01002813 # ensure given type
2814 if (!defined $type) {
2815 return $config{"gitweb.$key"};
2816 } elsif ($type eq 'bool') {
2817 # backward compatibility: 'git config --bool' returns true/false
2818 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2819 } elsif ($type eq 'int') {
2820 return config_to_int($config{"gitweb.$key"});
2821 }
2822 return $config{"gitweb.$key"};
Jakub Narebski717b8312006-07-31 21:22:15 +02002823}
2824
Jakub Narebski717b8312006-07-31 21:22:15 +02002825# get hash of given path at given ref
2826sub git_get_hash_by_path {
2827 my $base = shift;
2828 my $path = shift || return undef;
Jakub Narebski1d782b02006-09-21 18:09:12 +02002829 my $type = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02002830
Jakub Narebski4b02f482006-09-26 01:54:24 +02002831 $path =~ s,/+$,,;
Jakub Narebski717b8312006-07-31 21:22:15 +02002832
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002833 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
Lea Wiemann074afaa2008-06-19 22:03:21 +02002834 or die_error(500, "Open git-ls-tree failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02002835 my $line = <$fd>;
2836 close $fd or return undef;
2837
Jakub Narebski198a2a82007-05-12 21:16:34 +02002838 if (!defined $line) {
2839 # there is no tree or hash given by $path at $base
2840 return undef;
2841 }
2842
Jakub Narebski717b8312006-07-31 21:22:15 +02002843 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01002844 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
Jakub Narebski1d782b02006-09-21 18:09:12 +02002845 if (defined $type && $type ne $2) {
2846 # type doesn't match
2847 return undef;
2848 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002849 return $3;
2850}
2851
Jakub Narebskied224de2007-05-07 01:10:04 +02002852# get path of entry with given hash at given tree-ish (ref)
2853# used to get 'from' filename for combined diff (merge commit) for renames
2854sub git_get_path_by_hash {
2855 my $base = shift || return;
2856 my $hash = shift || return;
2857
2858 local $/ = "\0";
2859
2860 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2861 or return undef;
2862 while (my $line = <$fd>) {
2863 chomp $line;
2864
2865 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2866 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2867 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2868 close $fd;
2869 return $1;
2870 }
2871 }
2872 close $fd;
2873 return undef;
2874}
2875
Jakub Narebski717b8312006-07-31 21:22:15 +02002876## ......................................................................
2877## git utility functions, directly accessing git repository
2878
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002879# get the value of config variable either from file named as the variable
2880# itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2881# configuration variable in the repository config file.
2882sub git_get_file_or_project_config {
2883 my ($path, $name) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02002884
Jakub Narebski0e121a22007-11-03 00:41:20 +01002885 $git_dir = "$projectroot/$path";
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002886 open my $fd, '<', "$git_dir/$name"
2887 or return git_get_project_config($name);
2888 my $conf = <$fd>;
Jakub Narebski717b8312006-07-31 21:22:15 +02002889 close $fd;
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002890 if (defined $conf) {
2891 chomp $conf;
Junio C Hamano2eb54ef2007-05-16 21:04:16 -07002892 }
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002893 return $conf;
Jakub Narebski717b8312006-07-31 21:22:15 +02002894}
2895
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002896sub git_get_project_description {
2897 my $path = shift;
2898 return git_get_file_or_project_config($path, 'description');
Jakub Narebski717b8312006-07-31 21:22:15 +02002899}
2900
Sebastien Ceveyd940c902011-04-29 19:52:01 +02002901sub git_get_project_category {
2902 my $path = shift;
2903 return git_get_file_or_project_config($path, 'category');
2904}
2905
2906
Jakub Narebski0368c492011-04-29 19:51:57 +02002907# supported formats:
2908# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2909# - if its contents is a number, use it as tag weight,
2910# - otherwise add a tag with weight 1
2911# * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2912# the same value multiple times increases tag weight
2913# * `gitweb.ctag' multi-valued repo config variable
Petr Baudisaed93de2008-10-02 17:13:02 +02002914sub git_get_project_ctags {
Jakub Narebski0368c492011-04-29 19:51:57 +02002915 my $project = shift;
Petr Baudisaed93de2008-10-02 17:13:02 +02002916 my $ctags = {};
2917
Jakub Narebski0368c492011-04-29 19:51:57 +02002918 $git_dir = "$projectroot/$project";
2919 if (opendir my $dh, "$git_dir/ctags") {
2920 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2921 foreach my $tagfile (@files) {
2922 open my $ct, '<', $tagfile
2923 or next;
2924 my $val = <$ct>;
2925 chomp $val if $val;
2926 close $ct;
2927
2928 (my $ctag = $tagfile) =~ s#.*/##;
Jonathan Nieder2c162b52011-06-09 02:08:57 -05002929 if ($val =~ /^\d+$/) {
Jakub Narebski0368c492011-04-29 19:51:57 +02002930 $ctags->{$ctag} = $val;
2931 } else {
2932 $ctags->{$ctag} = 1;
2933 }
2934 }
2935 closedir $dh;
2936
2937 } elsif (open my $fh, '<', "$git_dir/ctags") {
2938 while (my $line = <$fh>) {
2939 chomp $line;
2940 $ctags->{$line}++ if $line;
2941 }
2942 close $fh;
2943
2944 } else {
2945 my $taglist = config_to_multi(git_get_project_config('ctag'));
2946 foreach my $tag (@$taglist) {
2947 $ctags->{$tag}++;
2948 }
Petr Baudisaed93de2008-10-02 17:13:02 +02002949 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002950
2951 return $ctags;
2952}
2953
2954# return hash, where keys are content tags ('ctags'),
2955# and values are sum of weights of given tag in every project
2956sub git_gather_all_ctags {
2957 my $projects = shift;
2958 my $ctags = {};
2959
2960 foreach my $p (@$projects) {
2961 foreach my $ct (keys %{$p->{'ctags'}}) {
2962 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2963 }
2964 }
2965
2966 return $ctags;
Petr Baudisaed93de2008-10-02 17:13:02 +02002967}
2968
2969sub git_populate_project_tagcloud {
2970 my $ctags = shift;
2971
2972 # First, merge different-cased tags; tags vote on casing
2973 my %ctags_lc;
2974 foreach (keys %$ctags) {
2975 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2976 if (not $ctags_lc{lc $_}->{topcount}
2977 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2978 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2979 $ctags_lc{lc $_}->{topname} = $_;
2980 }
2981 }
2982
2983 my $cloud;
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01002984 my $matched = $input_params{'ctag'};
Petr Baudisaed93de2008-10-02 17:13:02 +02002985 if (eval { require HTML::TagCloud; 1; }) {
2986 $cloud = HTML::TagCloud->new;
Jakub Narebski0368c492011-04-29 19:51:57 +02002987 foreach my $ctag (sort keys %ctags_lc) {
Petr Baudisaed93de2008-10-02 17:13:02 +02002988 # Pad the title with spaces so that the cloud looks
2989 # less crammed.
Jakub Narebski0368c492011-04-29 19:51:57 +02002990 my $title = esc_html($ctags_lc{$ctag}->{topname});
Petr Baudisaed93de2008-10-02 17:13:02 +02002991 $title =~ s/ /&nbsp;/g;
2992 $title =~ s/^/&nbsp;/g;
2993 $title =~ s/$/&nbsp;/g;
Jakub Narebski4b9447f2011-04-29 19:51:58 +02002994 if (defined $matched && $matched eq $ctag) {
2995 $title = qq(<span class="match">$title</span>);
2996 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002997 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2998 $ctags_lc{$ctag}->{count});
Petr Baudisaed93de2008-10-02 17:13:02 +02002999 }
3000 } else {
Jakub Narebski0368c492011-04-29 19:51:57 +02003001 $cloud = {};
3002 foreach my $ctag (keys %ctags_lc) {
Jakub Narebski4b9447f2011-04-29 19:51:58 +02003003 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
3004 if (defined $matched && $matched eq $ctag) {
3005 $title = qq(<span class="match">$title</span>);
3006 }
Jakub Narebski0368c492011-04-29 19:51:57 +02003007 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
3008 $cloud->{$ctag}{ctag} =
Jakub Narebski4b9447f2011-04-29 19:51:58 +02003009 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
Jakub Narebski0368c492011-04-29 19:51:57 +02003010 }
Petr Baudisaed93de2008-10-02 17:13:02 +02003011 }
Jakub Narebski0368c492011-04-29 19:51:57 +02003012 return $cloud;
Petr Baudisaed93de2008-10-02 17:13:02 +02003013}
3014
3015sub git_show_project_tagcloud {
3016 my ($cloud, $count) = @_;
Petr Baudisaed93de2008-10-02 17:13:02 +02003017 if (ref $cloud eq 'HTML::TagCloud') {
3018 return $cloud->html_and_css($count);
3019 } else {
Jakub Narebski0368c492011-04-29 19:51:57 +02003020 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
3021 return
3022 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
3023 join (', ', map {
3024 $cloud->{$_}->{'ctag'}
3025 } splice(@tags, 0, $count)) .
3026 '</div>';
Petr Baudisaed93de2008-10-02 17:13:02 +02003027 }
3028}
3029
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02003030sub git_get_project_url_list {
3031 my $path = shift;
3032
Jakub Narebski0e121a22007-11-03 00:41:20 +01003033 $git_dir = "$projectroot/$path";
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003034 open my $fd, '<', "$git_dir/cloneurl"
Jakub Narebski0e121a22007-11-03 00:41:20 +01003035 or return wantarray ?
3036 @{ config_to_multi(git_get_project_config('url')) } :
3037 config_to_multi(git_get_project_config('url'));
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02003038 my @git_project_url_list = map { chomp; $_ } <$fd>;
3039 close $fd;
3040
3041 return wantarray ? @git_project_url_list : \@git_project_url_list;
3042}
3043
Jakub Narebski847e01f2006-08-14 02:05:47 +02003044sub git_get_projects_list {
Jakub Narebski12b14432011-04-29 19:51:56 +02003045 my $filter = shift || '';
Bernhard R. Link348a6582012-01-30 21:06:38 +01003046 my $paranoid = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02003047 my @list;
3048
3049 if (-d $projects_list) {
3050 # search in directory
Jakub Narebski12b14432011-04-29 19:51:56 +02003051 my $dir = $projects_list;
Aneesh Kumar K.V6768d6b2006-11-03 10:41:45 +05303052 # remove the trailing "/"
3053 $dir =~ s!/+$!!;
Matthieu Moyac593b72012-01-04 11:07:45 +01003054 my $pfxlen = length("$dir");
3055 my $pfxdepth = ($dir =~ tr!/!!);
Jakub Narebski12b14432011-04-29 19:51:56 +02003056 # when filtering, search only given subdirectory
Bernhard R. Link348a6582012-01-30 21:06:38 +01003057 if ($filter && !$paranoid) {
Jakub Narebski12b14432011-04-29 19:51:56 +02003058 $dir .= "/$filter";
3059 $dir =~ s!/+$!!;
3060 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003061
3062 File::Find::find({
3063 follow_fast => 1, # follow symbolic links
Junio C Hamanod20602e2007-07-27 01:23:03 -07003064 follow_skip => 2, # ignore duplicates
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003065 dangling_symlinks => 0, # ignore dangling symlinks, silently
3066 wanted => sub {
Jakub Narebskiee1d8ee2010-04-30 18:30:31 +02003067 # global variables
3068 our $project_maxdepth;
3069 our $projectroot;
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003070 # skip project-list toplevel, if we get it.
3071 return if (m!^[/.]$!);
3072 # only directories can be git repositories
3073 return unless (-d $_);
Luke Luca5e9492007-10-16 20:45:25 -07003074 # don't traverse too deep (Find is super slow on os x)
Jakub Narebski12b14432011-04-29 19:51:56 +02003075 # $project_maxdepth excludes depth of $projectroot
Luke Luca5e9492007-10-16 20:45:25 -07003076 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
3077 $File::Find::prune = 1;
3078 return;
3079 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003080
Jakub Narebski12b14432011-04-29 19:51:56 +02003081 my $path = substr($File::Find::name, $pfxlen + 1);
Bernhard R. Link348a6582012-01-30 21:06:38 +01003082 # paranoidly only filter here
3083 if ($paranoid && $filter && $path !~ m!^\Q$filter\E/!) {
3084 next;
3085 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003086 # we check related file in $projectroot
Devin Doucettefb3bb3d2008-12-27 02:39:31 -07003087 if (check_export_ok("$projectroot/$path")) {
3088 push @list, { path => $path };
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003089 $File::Find::prune = 1;
3090 }
3091 },
3092 }, "$dir");
3093
Jakub Narebski717b8312006-07-31 21:22:15 +02003094 } elsif (-f $projects_list) {
3095 # read from file(url-encoded):
3096 # 'git%2Fgit.git Linus+Torvalds'
3097 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3098 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003099 open my $fd, '<', $projects_list or return;
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02003100 PROJECT:
Jakub Narebski717b8312006-07-31 21:22:15 +02003101 while (my $line = <$fd>) {
3102 chomp $line;
3103 my ($path, $owner) = split ' ', $line;
3104 $path = unescape($path);
3105 $owner = unescape($owner);
3106 if (!defined $path) {
3107 next;
3108 }
Jakub Narebski12b14432011-04-29 19:51:56 +02003109 # if $filter is rpovided, check if $path begins with $filter
3110 if ($filter && $path !~ m!^\Q$filter\E/!) {
3111 next;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003112 }
Junio C Hamano2172ce42006-10-03 02:30:47 -07003113 if (check_export_ok("$projectroot/$path")) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003114 my $pr = {
Kacper Kornet75e0dff2012-04-24 19:50:05 +02003115 path => $path
Jakub Narebski717b8312006-07-31 21:22:15 +02003116 };
Kacper Kornet75e0dff2012-04-24 19:50:05 +02003117 if ($owner) {
3118 $pr->{'owner'} = to_utf8($owner);
3119 }
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02003120 push @list, $pr;
Jakub Narebski717b8312006-07-31 21:22:15 +02003121 }
3122 }
3123 close $fd;
3124 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003125 return @list;
3126}
3127
Ville Skyttä64127572017-06-25 13:20:41 +03003128# written with help of Tree::Trie module (Perl Artistic License, GPL compatible)
Jakub Narebski12b14432011-04-29 19:51:56 +02003129# as side effects it sets 'forks' field to list of forks for forked projects
3130sub filter_forks_from_projects_list {
3131 my $projects = shift;
3132
3133 my %trie; # prefix tree of directories (path components)
3134 # generate trie out of those directories that might contain forks
3135 foreach my $pr (@$projects) {
3136 my $path = $pr->{'path'};
3137 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
3138 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
3139 next unless ($path); # skip '.git' repository: tests, git-instaweb
Julien Muchembled53c632f2011-10-21 21:04:21 +02003140 next unless (-d "$projectroot/$path"); # containing directory exists
Jakub Narebski12b14432011-04-29 19:51:56 +02003141 $pr->{'forks'} = []; # there can be 0 or more forks of project
3142
3143 # add to trie
3144 my @dirs = split('/', $path);
3145 # walk the trie, until either runs out of components or out of trie
3146 my $ref = \%trie;
3147 while (scalar @dirs &&
3148 exists($ref->{$dirs[0]})) {
3149 $ref = $ref->{shift @dirs};
3150 }
3151 # create rest of trie structure from rest of components
3152 foreach my $dir (@dirs) {
3153 $ref = $ref->{$dir} = {};
3154 }
3155 # create end marker, store $pr as a data
3156 $ref->{''} = $pr if (!exists $ref->{''});
3157 }
3158
3159 # filter out forks, by finding shortest prefix match for paths
3160 my @filtered;
3161 PROJECT:
3162 foreach my $pr (@$projects) {
3163 # trie lookup
3164 my $ref = \%trie;
3165 DIR:
3166 foreach my $dir (split('/', $pr->{'path'})) {
3167 if (exists $ref->{''}) {
3168 # found [shortest] prefix, is a fork - skip it
3169 push @{$ref->{''}{'forks'}}, $pr;
3170 next PROJECT;
3171 }
3172 if (!exists $ref->{$dir}) {
3173 # not in trie, cannot have prefix, not a fork
3174 push @filtered, $pr;
3175 next PROJECT;
3176 }
3177 # If the dir is there, we just walk one step down the trie.
3178 $ref = $ref->{$dir};
3179 }
3180 # we ran out of trie
3181 # (shouldn't happen: it's either no match, or end marker)
3182 push @filtered, $pr;
3183 }
3184
3185 return @filtered;
3186}
3187
3188# note: fill_project_list_info must be run first,
3189# for 'descr_long' and 'ctags' to be filled
3190sub search_projects_list {
3191 my ($projlist, %opts) = @_;
3192 my $tagfilter = $opts{'tagfilter'};
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003193 my $search_re = $opts{'search_regexp'};
Jakub Narebski12b14432011-04-29 19:51:56 +02003194
3195 return @$projlist
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003196 unless ($tagfilter || $search_re);
Jakub Narebski12b14432011-04-29 19:51:56 +02003197
Jakub Narebski07b257f2012-02-23 16:54:48 +01003198 # searching projects require filling to be run before it;
3199 fill_project_list_info($projlist,
3200 $tagfilter ? 'ctags' : (),
Junio C Hamanoaa145bf2012-03-08 13:04:49 -08003201 $search_re ? ('path', 'descr') : ());
Jakub Narebski12b14432011-04-29 19:51:56 +02003202 my @projects;
3203 PROJECT:
3204 foreach my $pr (@$projlist) {
3205
3206 if ($tagfilter) {
3207 next unless ref($pr->{'ctags'}) eq 'HASH';
3208 next unless
3209 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
3210 }
3211
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003212 if ($search_re) {
Jakub Narebski12b14432011-04-29 19:51:56 +02003213 next unless
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003214 $pr->{'path'} =~ /$search_re/ ||
3215 $pr->{'descr_long'} =~ /$search_re/;
Jakub Narebski12b14432011-04-29 19:51:56 +02003216 }
3217
3218 push @projects, $pr;
3219 }
3220
3221 return @projects;
3222}
3223
Junio C Hamano47852452007-07-03 22:10:42 -07003224our $gitweb_project_owner = undef;
3225sub git_get_project_list_from_file {
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003226
Junio C Hamano47852452007-07-03 22:10:42 -07003227 return if (defined $gitweb_project_owner);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003228
Junio C Hamano47852452007-07-03 22:10:42 -07003229 $gitweb_project_owner = {};
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003230 # read from file (url-encoded):
3231 # 'git%2Fgit.git Linus+Torvalds'
3232 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3233 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3234 if (-f $projects_list) {
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003235 open(my $fd, '<', $projects_list);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003236 while (my $line = <$fd>) {
3237 chomp $line;
3238 my ($pr, $ow) = split ' ', $line;
3239 $pr = unescape($pr);
3240 $ow = unescape($ow);
Junio C Hamano47852452007-07-03 22:10:42 -07003241 $gitweb_project_owner->{$pr} = to_utf8($ow);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003242 }
3243 close $fd;
3244 }
Junio C Hamano47852452007-07-03 22:10:42 -07003245}
3246
3247sub git_get_project_owner {
3248 my $project = shift;
3249 my $owner;
3250
3251 return undef unless $project;
Bruno Ribasb59012e2008-02-08 14:38:04 -02003252 $git_dir = "$projectroot/$project";
Junio C Hamano47852452007-07-03 22:10:42 -07003253
3254 if (!defined $gitweb_project_owner) {
3255 git_get_project_list_from_file();
3256 }
3257
3258 if (exists $gitweb_project_owner->{$project}) {
3259 $owner = $gitweb_project_owner->{$project};
3260 }
Bruno Ribasb59012e2008-02-08 14:38:04 -02003261 if (!defined $owner){
3262 $owner = git_get_project_config('owner');
3263 }
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003264 if (!defined $owner) {
Bruno Ribasb59012e2008-02-08 14:38:04 -02003265 $owner = get_file_owner("$git_dir");
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003266 }
3267
3268 return $owner;
3269}
3270
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003271sub git_get_last_activity {
3272 my ($path) = @_;
3273 my $fd;
3274
3275 $git_dir = "$projectroot/$path";
3276 open($fd, "-|", git_cmd(), 'for-each-ref',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00003277 '--format=%(committer)',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003278 '--sort=-committerdate',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00003279 '--count=1',
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01003280 map { "refs/$_" } get_branch_refs ()) or return;
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003281 my $most_recent = <$fd>;
3282 close $fd or return;
Jakub Narebski785cdea2007-05-13 12:39:22 +02003283 if (defined $most_recent &&
3284 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003285 my $timestamp = $1;
3286 my $age = time - $timestamp;
3287 return ($age, age_string($age));
3288 }
Matt McCutchenc9563952007-06-28 18:15:22 -04003289 return (undef, undef);
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003290}
3291
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01003292# Implementation note: when a single remote is wanted, we cannot use 'git
3293# remote show -n' because that command always work (assuming it's a remote URL
3294# if it's not defined), and we cannot use 'git remote show' because that would
3295# try to make a network roundtrip. So the only way to find if that particular
3296# remote is defined is to walk the list provided by 'git remote -v' and stop if
3297# and when we find what we want.
3298sub git_get_remotes_list {
3299 my $wanted = shift;
3300 my %remotes = ();
3301
3302 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3303 return unless $fd;
3304 while (my $remote = <$fd>) {
3305 chomp $remote;
3306 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3307 next if $wanted and not $remote eq $wanted;
3308 my ($url, $key) = ($1, $2);
3309
3310 $remotes{$remote} ||= { 'heads' => () };
3311 $remotes{$remote}{$key} = $url;
3312 }
3313 close $fd or return;
3314 return wantarray ? %remotes : \%remotes;
3315}
3316
3317# Takes a hash of remotes as first parameter and fills it by adding the
3318# available remote heads for each of the indicated remotes.
3319sub fill_remote_heads {
3320 my $remotes = shift;
3321 my @heads = map { "remotes/$_" } keys %$remotes;
3322 my @remoteheads = git_get_heads_list(undef, @heads);
3323 foreach my $remote (keys %$remotes) {
3324 $remotes->{$remote}{'heads'} = [ grep {
3325 $_->{'name'} =~ s!^$remote/!!
3326 } @remoteheads ];
3327 }
3328}
3329
Jakub Narebski847e01f2006-08-14 02:05:47 +02003330sub git_get_references {
Jakub Narebski717b8312006-07-31 21:22:15 +02003331 my $type = shift || "";
3332 my %refs;
Jakub Narebski28b9d9f2006-11-25 11:32:08 +01003333 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3334 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3335 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3336 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
Jakub Narebski9704d752006-09-19 14:31:49 +02003337 or return;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003338
Jakub Narebski717b8312006-07-31 21:22:15 +02003339 while (my $line = <$fd>) {
3340 chomp $line;
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02003341 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003342 if (defined $refs{$1}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003343 push @{$refs{$1}}, $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02003344 } else {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003345 $refs{$1} = [ $2 ];
Jakub Narebski717b8312006-07-31 21:22:15 +02003346 }
3347 }
3348 }
3349 close $fd or return;
3350 return \%refs;
3351}
3352
Jakub Narebski56a322f2006-08-24 19:41:23 +02003353sub git_get_rev_name_tags {
3354 my $hash = shift || return undef;
3355
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003356 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
Jakub Narebski56a322f2006-08-24 19:41:23 +02003357 or return;
3358 my $name_rev = <$fd>;
3359 close $fd;
3360
3361 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3362 return $1;
3363 } else {
3364 # catches also '$hash undefined' output
3365 return undef;
3366 }
3367}
3368
Jakub Narebski717b8312006-07-31 21:22:15 +02003369## ----------------------------------------------------------------------
3370## parse to hash functions
3371
Jakub Narebski847e01f2006-08-14 02:05:47 +02003372sub parse_date {
Jakub Narebski717b8312006-07-31 21:22:15 +02003373 my $epoch = shift;
3374 my $tz = shift || "-0000";
3375
3376 my %date;
3377 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3378 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3379 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3380 $date{'hour'} = $hour;
3381 $date{'minute'} = $min;
3382 $date{'mday'} = $mday;
3383 $date{'day'} = $days[$wday];
3384 $date{'month'} = $months[$mon];
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003385 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3386 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
Jakub Narebski952c65f2006-08-22 16:52:50 +02003387 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3388 $mday, $months[$mon], $hour ,$min;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003389 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
Vincent Zanottia62d6d82007-11-10 19:55:27 +01003390 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
Jakub Narebski717b8312006-07-31 21:22:15 +02003391
Jakub Narebski2b1e1722011-03-25 20:20:49 +01003392 my ($tz_sign, $tz_hour, $tz_min) =
3393 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3394 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3395 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
Jakub Narebski717b8312006-07-31 21:22:15 +02003396 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3397 $date{'hour_local'} = $hour;
3398 $date{'minute_local'} = $min;
3399 $date{'tz_local'} = $tz;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003400 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3401 1900+$year, $mon+1, $mday,
3402 $hour, $min, $sec, $tz);
Jakub Narebski717b8312006-07-31 21:22:15 +02003403 return %date;
3404}
3405
Jakub Narebski847e01f2006-08-14 02:05:47 +02003406sub parse_tag {
Jakub Narebski717b8312006-07-31 21:22:15 +02003407 my $tag_id = shift;
3408 my %tag;
3409 my @comment;
3410
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003411 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02003412 $tag{'id'} = $tag_id;
3413 while (my $line = <$fd>) {
3414 chomp $line;
3415 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3416 $tag{'object'} = $1;
3417 } elsif ($line =~ m/^type (.+)$/) {
3418 $tag{'type'} = $1;
3419 } elsif ($line =~ m/^tag (.+)$/) {
3420 $tag{'name'} = $1;
3421 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3422 $tag{'author'} = $1;
Giuseppe Bilottaba924732009-06-30 00:00:50 +02003423 $tag{'author_epoch'} = $2;
3424 $tag{'author_tz'} = $3;
3425 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3426 $tag{'author_name'} = $1;
3427 $tag{'author_email'} = $2;
3428 } else {
3429 $tag{'author_name'} = $tag{'author'};
3430 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003431 } elsif ($line =~ m/--BEGIN/) {
3432 push @comment, $line;
3433 last;
3434 } elsif ($line eq "") {
3435 last;
3436 }
3437 }
3438 push @comment, <$fd>;
3439 $tag{'comment'} = \@comment;
3440 close $fd or return;
3441 if (!defined $tag{'name'}) {
3442 return
3443 };
3444 return %tag
3445}
3446
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003447sub parse_commit_text {
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003448 my ($commit_text, $withparents) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003449 my @commit_lines = split '\n', $commit_text;
Jakub Narebski717b8312006-07-31 21:22:15 +02003450 my %co;
3451
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003452 pop @commit_lines; # Remove '\0'
3453
Jakub Narebski198a2a82007-05-12 21:16:34 +02003454 if (! @commit_lines) {
3455 return;
3456 }
3457
Jakub Narebski717b8312006-07-31 21:22:15 +02003458 my $header = shift @commit_lines;
Jakub Narebski198a2a82007-05-12 21:16:34 +02003459 if ($header !~ m/^[0-9a-fA-F]{40}/) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003460 return;
3461 }
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003462 ($co{'id'}, my @parents) = split ' ', $header;
Jakub Narebski717b8312006-07-31 21:22:15 +02003463 while (my $line = shift @commit_lines) {
3464 last if $line eq "\n";
3465 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3466 $co{'tree'} = $1;
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003467 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00003468 push @parents, $1;
Jakub Narebski717b8312006-07-31 21:22:15 +02003469 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
Zoltán Füzesi5ed5bbc2009-08-02 09:42:24 +02003470 $co{'author'} = to_utf8($1);
Jakub Narebski717b8312006-07-31 21:22:15 +02003471 $co{'author_epoch'} = $2;
3472 $co{'author_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01003473 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3474 $co{'author_name'} = $1;
3475 $co{'author_email'} = $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02003476 } else {
3477 $co{'author_name'} = $co{'author'};
3478 }
3479 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
Zoltán Füzesi5ed5bbc2009-08-02 09:42:24 +02003480 $co{'committer'} = to_utf8($1);
Jakub Narebski717b8312006-07-31 21:22:15 +02003481 $co{'committer_epoch'} = $2;
3482 $co{'committer_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01003483 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3484 $co{'committer_name'} = $1;
3485 $co{'committer_email'} = $2;
3486 } else {
3487 $co{'committer_name'} = $co{'committer'};
3488 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003489 }
3490 }
3491 if (!defined $co{'tree'}) {
3492 return;
3493 };
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00003494 $co{'parents'} = \@parents;
3495 $co{'parent'} = $parents[0];
Jakub Narebski717b8312006-07-31 21:22:15 +02003496
3497 foreach my $title (@commit_lines) {
3498 $title =~ s/^ //;
3499 if ($title ne "") {
3500 $co{'title'} = chop_str($title, 80, 5);
3501 # remove leading stuff of merges to make the interesting part visible
3502 if (length($title) > 50) {
3503 $title =~ s/^Automatic //;
3504 $title =~ s/^merge (of|with) /Merge ... /i;
3505 if (length($title) > 50) {
3506 $title =~ s/(http|rsync):\/\///;
3507 }
3508 if (length($title) > 50) {
3509 $title =~ s/(master|www|rsync)\.//;
3510 }
3511 if (length($title) > 50) {
3512 $title =~ s/kernel.org:?//;
3513 }
3514 if (length($title) > 50) {
3515 $title =~ s/\/pub\/scm//;
3516 }
3517 }
3518 $co{'title_short'} = chop_str($title, 50, 5);
3519 last;
3520 }
3521 }
Joey Hess53c39672008-09-05 14:26:29 -04003522 if (! defined $co{'title'} || $co{'title'} eq "") {
Petr Baudis7e0fe5c2006-10-06 18:55:04 +02003523 $co{'title'} = $co{'title_short'} = '(no commit message)';
3524 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003525 # remove added spaces
3526 foreach my $line (@commit_lines) {
3527 $line =~ s/^ //;
3528 }
3529 $co{'comment'} = \@commit_lines;
3530
3531 my $age = time - $co{'committer_epoch'};
3532 $co{'age'} = $age;
3533 $co{'age_string'} = age_string($age);
3534 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3535 if ($age > 60*60*24*7*2) {
3536 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3537 $co{'age_string_age'} = $co{'age_string'};
3538 } else {
3539 $co{'age_string_date'} = $co{'age_string'};
3540 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3541 }
3542 return %co;
3543}
3544
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003545sub parse_commit {
3546 my ($commit_id) = @_;
3547 my %co;
3548
3549 local $/ = "\0";
3550
3551 open my $fd, "-|", git_cmd(), "rev-list",
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003552 "--parents",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003553 "--header",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003554 "--max-count=1",
3555 $commit_id,
3556 "--",
Lea Wiemann074afaa2008-06-19 22:03:21 +02003557 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003558 %co = parse_commit_text(<$fd>, 1);
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003559 close $fd;
3560
3561 return %co;
3562}
3563
3564sub parse_commits {
Jakub Narebski311e5522008-02-26 13:22:06 +01003565 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003566 my @cos;
3567
3568 $maxcount ||= 1;
3569 $skip ||= 0;
3570
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003571 local $/ = "\0";
3572
3573 open my $fd, "-|", git_cmd(), "rev-list",
3574 "--header",
Jakub Narebski311e5522008-02-26 13:22:06 +01003575 @args,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003576 ("--max-count=" . $maxcount),
Robert Fitzsimonsf47efbb2006-12-24 14:31:49 +00003577 ("--skip=" . $skip),
Miklos Vajna868bc062007-07-12 20:39:27 +02003578 @extra_options,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003579 $commit_id,
3580 "--",
3581 ($filename ? ($filename) : ())
Lea Wiemann074afaa2008-06-19 22:03:21 +02003582 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003583 while (my $line = <$fd>) {
3584 my %co = parse_commit_text($line);
3585 push @cos, \%co;
3586 }
3587 close $fd;
3588
3589 return wantarray ? @cos : \@cos;
3590}
3591
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003592# parse line of git-diff-tree "raw" output
Jakub Narebski740e67f2006-08-21 23:07:00 +02003593sub parse_difftree_raw_line {
3594 my $line = shift;
3595 my %res;
3596
3597 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3598 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3599 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3600 $res{'from_mode'} = $1;
3601 $res{'to_mode'} = $2;
3602 $res{'from_id'} = $3;
3603 $res{'to_id'} = $4;
Jakub Narebski4ed4a342008-04-05 21:13:24 +01003604 $res{'status'} = $5;
Jakub Narebski740e67f2006-08-21 23:07:00 +02003605 $res{'similarity'} = $6;
3606 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003607 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02003608 } else {
Jakub Narebski9d301452007-11-01 12:38:08 +01003609 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02003610 }
3611 }
Jakub Narebski78bc4032007-05-07 01:10:03 +02003612 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3613 # combined diff (for merge commit)
3614 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3615 $res{'nparents'} = length($1);
3616 $res{'from_mode'} = [ split(' ', $2) ];
3617 $res{'to_mode'} = pop @{$res{'from_mode'}};
3618 $res{'from_id'} = [ split(' ', $3) ];
3619 $res{'to_id'} = pop @{$res{'from_id'}};
3620 $res{'status'} = [ split('', $4) ];
3621 $res{'to_file'} = unquote($5);
3622 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02003623 # 'c512b523472485aef4fff9e57b229d9d243c967f'
Jakub Narebski0edcb372006-08-31 00:36:04 +02003624 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3625 $res{'commit'} = $1;
3626 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02003627
3628 return wantarray ? %res : \%res;
3629}
3630
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003631# wrapper: return parsed line of git-diff-tree "raw" output
3632# (the argument might be raw line, or parsed info)
3633sub parsed_difftree_line {
3634 my $line_or_ref = shift;
3635
3636 if (ref($line_or_ref) eq "HASH") {
3637 # pre-parsed (or generated by hand)
3638 return $line_or_ref;
3639 } else {
3640 return parse_difftree_raw_line($line_or_ref);
3641 }
3642}
3643
Jakub Narebskicb849b42006-08-31 00:32:15 +02003644# parse line of git-ls-tree output
Jakub Narebski74fd8722009-05-07 19:11:29 +02003645sub parse_ls_tree_line {
Jakub Narebskicb849b42006-08-31 00:32:15 +02003646 my $line = shift;
3647 my %opts = @_;
3648 my %res;
3649
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003650 if ($opts{'-l'}) {
3651 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3652 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
Jakub Narebskicb849b42006-08-31 00:32:15 +02003653
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003654 $res{'mode'} = $1;
3655 $res{'type'} = $2;
3656 $res{'hash'} = $3;
3657 $res{'size'} = $4;
3658 if ($opts{'-z'}) {
3659 $res{'name'} = $5;
3660 } else {
3661 $res{'name'} = unquote($5);
3662 }
Jakub Narebskicb849b42006-08-31 00:32:15 +02003663 } else {
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003664 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3665 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3666
3667 $res{'mode'} = $1;
3668 $res{'type'} = $2;
3669 $res{'hash'} = $3;
3670 if ($opts{'-z'}) {
3671 $res{'name'} = $4;
3672 } else {
3673 $res{'name'} = unquote($4);
3674 }
Jakub Narebskicb849b42006-08-31 00:32:15 +02003675 }
3676
3677 return wantarray ? %res : \%res;
3678}
3679
Jakub Narebski90921742007-06-08 13:27:42 +02003680# generates _two_ hashes, references to which are passed as 2 and 3 argument
3681sub parse_from_to_diffinfo {
3682 my ($diffinfo, $from, $to, @parents) = @_;
3683
3684 if ($diffinfo->{'nparents'}) {
3685 # combined diff
3686 $from->{'file'} = [];
3687 $from->{'href'} = [];
3688 fill_from_file_info($diffinfo, @parents)
3689 unless exists $diffinfo->{'from_file'};
3690 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
Jakub Narebski9d301452007-11-01 12:38:08 +01003691 $from->{'file'}[$i] =
3692 defined $diffinfo->{'from_file'}[$i] ?
3693 $diffinfo->{'from_file'}[$i] :
3694 $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003695 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3696 $from->{'href'}[$i] = href(action=>"blob",
3697 hash_base=>$parents[$i],
3698 hash=>$diffinfo->{'from_id'}[$i],
3699 file_name=>$from->{'file'}[$i]);
3700 } else {
3701 $from->{'href'}[$i] = undef;
3702 }
3703 }
3704 } else {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003705 # ordinary (not combined) diff
Jakub Narebski9d301452007-11-01 12:38:08 +01003706 $from->{'file'} = $diffinfo->{'from_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003707 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3708 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3709 hash=>$diffinfo->{'from_id'},
3710 file_name=>$from->{'file'});
3711 } else {
3712 delete $from->{'href'};
3713 }
3714 }
3715
Jakub Narebski9d301452007-11-01 12:38:08 +01003716 $to->{'file'} = $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003717 if (!is_deleted($diffinfo)) { # file exists in result
3718 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3719 hash=>$diffinfo->{'to_id'},
3720 file_name=>$to->{'file'});
3721 } else {
3722 delete $to->{'href'};
3723 }
3724}
3725
Jakub Narebski717b8312006-07-31 21:22:15 +02003726## ......................................................................
3727## parse to array of hashes functions
3728
Jakub Narebskicd146402006-11-02 20:23:11 +01003729sub git_get_heads_list {
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003730 my ($limit, @classes) = @_;
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01003731 @classes = get_branch_refs() unless @classes;
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003732 my @patterns = map { "refs/$_" } @classes;
Jakub Narebskicd146402006-11-02 20:23:11 +01003733 my @headslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003734
Jakub Narebskicd146402006-11-02 20:23:11 +01003735 open my $fd, '-|', git_cmd(), 'for-each-ref',
3736 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3737 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003738 @patterns
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003739 or return;
3740 while (my $line = <$fd>) {
Jakub Narebskicd146402006-11-02 20:23:11 +01003741 my %ref_item;
Jakub Narebski120ddde2006-09-19 14:33:22 +02003742
Jakub Narebskicd146402006-11-02 20:23:11 +01003743 chomp $line;
3744 my ($refinfo, $committerinfo) = split(/\0/, $line);
3745 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3746 my ($committer, $epoch, $tz) =
3747 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01003748 $ref_item{'fullname'} = $name;
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01003749 my $strip_refs = join '|', map { quotemeta } get_branch_refs();
3750 $name =~ s!^refs/($strip_refs|remotes)/!!;
Krzesimir Nowake3747472013-12-11 12:54:44 +01003751 $ref_item{'name'} = $name;
3752 # for refs neither in 'heads' nor 'remotes' we want to
3753 # show their ref dir
3754 my $ref_dir = (defined $1) ? $1 : '';
3755 if ($ref_dir ne '' and $ref_dir ne 'heads' and $ref_dir ne 'remotes') {
3756 $ref_item{'name'} .= ' (' . $ref_dir . ')';
3757 }
Jakub Narebskicd146402006-11-02 20:23:11 +01003758
Jakub Narebskicd146402006-11-02 20:23:11 +01003759 $ref_item{'id'} = $hash;
3760 $ref_item{'title'} = $title || '(no commit message)';
3761 $ref_item{'epoch'} = $epoch;
3762 if ($epoch) {
3763 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3764 } else {
3765 $ref_item{'age'} = "unknown";
Jakub Narebski717b8312006-07-31 21:22:15 +02003766 }
Jakub Narebskicd146402006-11-02 20:23:11 +01003767
3768 push @headslist, \%ref_item;
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003769 }
3770 close $fd;
Junio C Hamano7a13b992006-07-31 19:18:34 -07003771
Jakub Narebskicd146402006-11-02 20:23:11 +01003772 return wantarray ? @headslist : \@headslist;
3773}
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003774
Jakub Narebskicd146402006-11-02 20:23:11 +01003775sub git_get_tags_list {
3776 my $limit = shift;
3777 my @tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003778
Jakub Narebskicd146402006-11-02 20:23:11 +01003779 open my $fd, '-|', git_cmd(), 'for-each-ref',
3780 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3781 '--format=%(objectname) %(objecttype) %(refname) '.
3782 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3783 'refs/tags'
3784 or return;
3785 while (my $line = <$fd>) {
3786 my %ref_item;
3787
3788 chomp $line;
3789 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3790 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3791 my ($creator, $epoch, $tz) =
3792 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01003793 $ref_item{'fullname'} = $name;
Jakub Narebskicd146402006-11-02 20:23:11 +01003794 $name =~ s!^refs/tags/!!;
3795
3796 $ref_item{'type'} = $type;
3797 $ref_item{'id'} = $id;
3798 $ref_item{'name'} = $name;
3799 if ($type eq "tag") {
3800 $ref_item{'subject'} = $title;
3801 $ref_item{'reftype'} = $reftype;
3802 $ref_item{'refid'} = $refid;
3803 } else {
3804 $ref_item{'reftype'} = $type;
3805 $ref_item{'refid'} = $id;
3806 }
3807
3808 if ($type eq "tag" || $type eq "commit") {
3809 $ref_item{'epoch'} = $epoch;
3810 if ($epoch) {
3811 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3812 } else {
3813 $ref_item{'age'} = "unknown";
3814 }
3815 }
3816
3817 push @tagslist, \%ref_item;
Jakub Narebski717b8312006-07-31 21:22:15 +02003818 }
Jakub Narebskicd146402006-11-02 20:23:11 +01003819 close $fd;
3820
3821 return wantarray ? @tagslist : \@tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003822}
3823
3824## ----------------------------------------------------------------------
3825## filesystem-related functions
3826
3827sub get_file_owner {
3828 my $path = shift;
3829
3830 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3831 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3832 if (!defined $gcos) {
3833 return undef;
3834 }
3835 my $owner = $gcos;
3836 $owner =~ s/[,;].*$//;
Martin Koegler00f429a2007-06-03 17:42:44 +02003837 return to_utf8($owner);
Jakub Narebski717b8312006-07-31 21:22:15 +02003838}
3839
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01003840# assume that file exists
3841sub insert_file {
3842 my $filename = shift;
3843
3844 open my $fd, '<', $filename;
Jakub Narebski45868642008-12-08 14:13:21 +01003845 print map { to_utf8($_) } <$fd>;
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01003846 close $fd;
3847}
3848
Jakub Narebski717b8312006-07-31 21:22:15 +02003849## ......................................................................
3850## mimetype related functions
3851
3852sub mimetype_guess_file {
3853 my $filename = shift;
3854 my $mimemap = shift;
3855 -r $mimemap or return undef;
3856
3857 my %mimemap;
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003858 open(my $mh, '<', $mimemap) or return undef;
Jakub Narebskiad87e4f2009-05-11 03:21:06 +02003859 while (<$mh>) {
Jakub Narebski618918e2006-08-14 02:15:22 +02003860 next if m/^#/; # skip comments
Ludwig Nussel93a6ad12011-06-15 08:10:08 +02003861 my ($mimetype, @exts) = split(/\s+/);
3862 foreach my $ext (@exts) {
3863 $mimemap{$ext} = $mimetype;
Jakub Narebski717b8312006-07-31 21:22:15 +02003864 }
3865 }
Jakub Narebskiad87e4f2009-05-11 03:21:06 +02003866 close($mh);
Jakub Narebski717b8312006-07-31 21:22:15 +02003867
Jakub Narebski80593192006-09-19 13:57:03 +02003868 $filename =~ /\.([^.]*)$/;
Jakub Narebski717b8312006-07-31 21:22:15 +02003869 return $mimemap{$1};
3870}
3871
3872sub mimetype_guess {
3873 my $filename = shift;
3874 my $mime;
3875 $filename =~ /\./ or return undef;
3876
3877 if ($mimetypes_file) {
3878 my $file = $mimetypes_file;
Jakub Narebskid5aa50d2006-08-14 02:16:33 +02003879 if ($file !~ m!^/!) { # if it is relative path
3880 # it is relative to project
3881 $file = "$projectroot/$project/$file";
3882 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003883 $mime = mimetype_guess_file($filename, $file);
3884 }
3885 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3886 return $mime;
3887}
3888
Jakub Narebski847e01f2006-08-14 02:05:47 +02003889sub blob_mimetype {
Jakub Narebski717b8312006-07-31 21:22:15 +02003890 my $fd = shift;
3891 my $filename = shift;
3892
3893 if ($filename) {
3894 my $mime = mimetype_guess($filename);
3895 $mime and return $mime;
3896 }
3897
3898 # just in case
3899 return $default_blob_plain_mimetype unless $fd;
3900
3901 if (-T $fd) {
Jakub Narebski7f718e82008-06-03 16:47:10 +02003902 return 'text/plain';
Jakub Narebski717b8312006-07-31 21:22:15 +02003903 } elsif (! $filename) {
3904 return 'application/octet-stream';
3905 } elsif ($filename =~ m/\.png$/i) {
3906 return 'image/png';
3907 } elsif ($filename =~ m/\.gif$/i) {
3908 return 'image/gif';
3909 } elsif ($filename =~ m/\.jpe?g$/i) {
3910 return 'image/jpeg';
3911 } else {
3912 return 'application/octet-stream';
3913 }
3914}
3915
Jakub Narebski7f718e82008-06-03 16:47:10 +02003916sub blob_contenttype {
3917 my ($fd, $file_name, $type) = @_;
3918
3919 $type ||= blob_mimetype($fd, $file_name);
3920 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3921 $type .= "; charset=$default_text_plain_charset";
3922 }
3923
3924 return $type;
3925}
3926
Jakub Narebski592ea412010-04-27 21:34:45 +02003927# guess file syntax for syntax highlighting; return undef if no highlighting
3928# the name of syntax can (in the future) depend on syntax highlighter used
3929sub guess_file_syntax {
Ian Kellingc151aa32016-09-24 15:32:57 -07003930 my ($highlight, $file_name) = @_;
Jakub Narebski592ea412010-04-27 21:34:45 +02003931 return undef unless ($highlight && defined $file_name);
Jakub Narebski592ea412010-04-27 21:34:45 +02003932 my $basename = basename($file_name, '.in');
3933 return $highlight_basename{$basename}
3934 if exists $highlight_basename{$basename};
3935
3936 $basename =~ /\.([^.]*)$/;
3937 my $ext = $1 or return undef;
3938 return $highlight_ext{$ext}
3939 if exists $highlight_ext{$ext};
3940
3941 return undef;
3942}
3943
3944# run highlighter and return FD of its output,
3945# or return original FD if no highlighting
3946sub run_highlighter {
3947 my ($fd, $highlight, $syntax) = @_;
Ian Kelling779a2062016-09-24 15:32:58 -07003948 return $fd unless ($highlight);
Jakub Narebski592ea412010-04-27 21:34:45 +02003949
Sylvain Rabot3ca73532010-12-30 22:20:29 +01003950 close $fd;
Ian Kelling779a2062016-09-24 15:32:58 -07003951 my $syntax_arg = (defined $syntax) ? "--syntax $syntax" : "--force";
Jakub Narebski592ea412010-04-27 21:34:45 +02003952 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
Shin Kojima029f3722016-05-03 22:00:51 +09003953 quote_command($^X, '-CO', '-MEncode=decode,FB_DEFAULT', '-pse',
3954 '$_ = decode($fe, $_, FB_DEFAULT) if !utf8::decode($_);',
3955 '--', "-fe=$fallback_encoding")." | ".
Christopher Wilson7ce896b2010-09-21 00:25:19 -07003956 quote_command($highlight_bin).
Ian Kelling779a2062016-09-24 15:32:58 -07003957 " --replace-tabs=8 --fragment $syntax_arg |"
Jakub Narebski592ea412010-04-27 21:34:45 +02003958 or die_error(500, "Couldn't open file or run syntax highlighter");
3959 return $fd;
3960}
3961
Jakub Narebski717b8312006-07-31 21:22:15 +02003962## ======================================================================
3963## functions printing HTML: header, footer, error page
3964
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02003965sub get_page_title {
3966 my $title = to_utf8($site_name);
3967
Bernhard R. Link19d2d232012-01-30 21:07:37 +01003968 unless (defined $project) {
3969 if (defined $project_filter) {
Jakub Narebskif4212082012-02-12 16:21:30 +01003970 $title .= " - projects in '" . esc_path($project_filter) . "'";
Bernhard R. Link19d2d232012-01-30 21:07:37 +01003971 }
3972 return $title;
3973 }
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02003974 $title .= " - " . to_utf8($project);
3975
3976 return $title unless (defined $action);
3977 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3978
3979 return $title unless (defined $file_name);
3980 $title .= " - " . esc_path($file_name);
3981 if ($action eq "tree" && $file_name !~ m|/$|) {
3982 $title .= "/";
3983 }
3984
3985 return $title;
3986}
3987
Jakub Narebski6ee90332011-06-22 13:50:46 +02003988sub get_content_type_html {
3989 # require explicit support from the UA if we are to send the page as
3990 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3991 # we have to do this because MSIE sometimes globs '*/*', pretending to
3992 # support xhtml+xml but choking when it gets what it asked for.
3993 if (defined $cgi->http('HTTP_ACCEPT') &&
3994 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3995 $cgi->Accept('application/xhtml+xml') != 0) {
3996 return 'application/xhtml+xml';
3997 } else {
3998 return 'text/html';
3999 }
4000}
4001
Jakub Narebski05bb5a22010-12-18 21:02:13 +01004002sub print_feed_meta {
4003 if (defined $project) {
4004 my %href_params = get_feed_info();
4005 if (!exists $href_params{'-title'}) {
4006 $href_params{'-title'} = 'log';
4007 }
4008
Ævar Arnfjörð Bjarmason0f54b7d2011-02-19 15:27:41 +00004009 foreach my $format (qw(RSS Atom)) {
Jakub Narebski05bb5a22010-12-18 21:02:13 +01004010 my $type = lc($format);
4011 my %link_attr = (
4012 '-rel' => 'alternate',
4013 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
4014 '-type' => "application/$type+xml"
4015 );
4016
Sebastian Pippingcc999a32012-04-04 14:25:44 +02004017 $href_params{'extra_options'} = undef;
Jakub Narebski05bb5a22010-12-18 21:02:13 +01004018 $href_params{'action'} = $type;
4019 $link_attr{'-href'} = href(%href_params);
4020 print "<link ".
4021 "rel=\"$link_attr{'-rel'}\" ".
4022 "title=\"$link_attr{'-title'}\" ".
4023 "href=\"$link_attr{'-href'}\" ".
4024 "type=\"$link_attr{'-type'}\" ".
4025 "/>\n";
4026
4027 $href_params{'extra_options'} = '--no-merges';
4028 $link_attr{'-href'} = href(%href_params);
4029 $link_attr{'-title'} .= ' (no merges)';
4030 print "<link ".
4031 "rel=\"$link_attr{'-rel'}\" ".
4032 "title=\"$link_attr{'-title'}\" ".
4033 "href=\"$link_attr{'-href'}\" ".
4034 "type=\"$link_attr{'-type'}\" ".
4035 "/>\n";
4036 }
4037
4038 } else {
4039 printf('<link rel="alternate" title="%s projects list" '.
4040 'href="%s" type="text/plain; charset=utf-8" />'."\n",
4041 esc_attr($site_name), href(project=>undef, action=>"project_index"));
4042 printf('<link rel="alternate" title="%s projects feeds" '.
4043 'href="%s" type="text/x-opml" />'."\n",
4044 esc_attr($site_name), href(project=>undef, action=>"opml"));
4045 }
4046}
4047
Jakub Narebski6ee90332011-06-22 13:50:46 +02004048sub print_header_links {
4049 my $status = shift;
4050
4051 # print out each stylesheet that exist, providing backwards capability
4052 # for those people who defined $stylesheet in a config file
4053 if (defined $stylesheet) {
4054 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
4055 } else {
4056 foreach my $stylesheet (@stylesheets) {
4057 next unless $stylesheet;
4058 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
4059 }
4060 }
4061 print_feed_meta()
4062 if ($status eq '200 OK');
4063 if (defined $favicon) {
4064 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
4065 }
4066}
4067
Bernhard R. Link40efa222012-01-30 21:09:43 +01004068sub print_nav_breadcrumbs_path {
4069 my $dirprefix = undef;
4070 while (my $part = shift) {
4071 $dirprefix .= "/" if defined $dirprefix;
4072 $dirprefix .= $part;
4073 print $cgi->a({-href => href(project => undef,
4074 project_filter => $dirprefix,
4075 action => "project_list")},
4076 esc_html($part)) . " / ";
4077 }
4078}
4079
Jakub Narebski6ee90332011-06-22 13:50:46 +02004080sub print_nav_breadcrumbs {
4081 my %opts = @_;
4082
Tony Finchad9c2e22013-07-04 18:02:12 +01004083 for my $crumb (@extra_breadcrumbs, [ $home_link_str => $home_link ]) {
4084 print $cgi->a({-href => esc_url($crumb->[1])}, $crumb->[0]) . " / ";
4085 }
Jakub Narebski6ee90332011-06-22 13:50:46 +02004086 if (defined $project) {
Bernhard R. Link4426ba22012-01-30 21:10:23 +01004087 my @dirname = split '/', $project;
4088 my $projectbasename = pop @dirname;
4089 print_nav_breadcrumbs_path(@dirname);
4090 print $cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));
Jakub Narebski6ee90332011-06-22 13:50:46 +02004091 if (defined $action) {
4092 my $action_print = $action ;
4093 if (defined $opts{-action_extra}) {
4094 $action_print = $cgi->a({-href => href(action=>$action)},
4095 $action);
4096 }
4097 print " / $action_print";
4098 }
4099 if (defined $opts{-action_extra}) {
4100 print " / $opts{-action_extra}";
4101 }
4102 print "\n";
Bernhard R. Link40efa222012-01-30 21:09:43 +01004103 } elsif (defined $project_filter) {
4104 print_nav_breadcrumbs_path(split '/', $project_filter);
Jakub Narebski6ee90332011-06-22 13:50:46 +02004105 }
4106}
4107
4108sub print_search_form {
4109 if (!defined $searchtext) {
4110 $searchtext = "";
4111 }
4112 my $search_hash;
4113 if (defined $hash_base) {
4114 $search_hash = $hash_base;
4115 } elsif (defined $hash) {
4116 $search_hash = $hash;
4117 } else {
4118 $search_hash = "HEAD";
4119 }
4120 my $action = $my_uri;
4121 my $use_pathinfo = gitweb_check_feature('pathinfo');
4122 if ($use_pathinfo) {
4123 $action .= "/".esc_url($project);
4124 }
Roland Mas4750f4b2014-10-16 08:54:47 +02004125 print $cgi->start_form(-method => "get", -action => $action) .
Jakub Narebski6ee90332011-06-22 13:50:46 +02004126 "<div class=\"search\">\n" .
4127 (!$use_pathinfo &&
4128 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
4129 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
4130 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
4131 $cgi->popup_menu(-name => 'st', -default => 'commit',
4132 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
Tony Finchaf52bd52013-08-20 17:59:54 +01004133 " " . $cgi->a({-href => href(action=>"search_help"),
4134 -title => "search help" }, "?") . " search:\n",
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01004135 $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
Jakub Narebski6ee90332011-06-22 13:50:46 +02004136 "<span title=\"Extended regular expression\">" .
4137 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
4138 -checked => $search_use_regexp) .
4139 "</span>" .
4140 "</div>" .
4141 $cgi->end_form() . "\n";
4142}
4143
Kay Sievers12a88f22005-08-07 20:02:47 +02004144sub git_header_html {
Kay Sieversa59d4af2005-08-07 20:15:44 +02004145 my $status = shift || "200 OK";
Kay Sievers11044292005-10-19 03:18:45 +02004146 my $expires = shift;
Jakub Narebski7a597452010-04-24 16:00:04 +02004147 my %opts = @_;
Kay Sieversa59d4af2005-08-07 20:15:44 +02004148
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02004149 my $title = get_page_title();
Jakub Narebski6ee90332011-06-22 13:50:46 +02004150 my $content_type = get_content_type_html();
Jakub Narebski952c65f2006-08-22 16:52:50 +02004151 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
Jakub Narebski7a597452010-04-24 16:00:04 +02004152 -status=> $status, -expires => $expires)
Jakub Narebskiad709ea2010-06-13 00:35:59 +02004153 unless ($opts{'-no_http_header'});
Jakub Narebski45c9a752006-12-27 23:59:51 +01004154 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
Kay Sieversa59d4af2005-08-07 20:15:44 +02004155 print <<EOF;
Kay Sievers6191f8e2005-08-07 20:19:56 +02004156<?xml version="1.0" encoding="utf-8"?>
Kay Sievers161332a2005-08-07 19:49:46 +02004157<!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 +02004158<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02004159<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
Jakub Narebskiae20de52006-06-21 09:48:03 +02004160<!-- git core binaries version $git_version -->
Kay Sievers161332a2005-08-07 19:49:46 +02004161<head>
Alp Tokerf6801d62006-07-11 11:19:34 +01004162<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
Jakub Narebski45c9a752006-12-27 23:59:51 +01004163<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
Kay Sieversc994d622005-08-07 20:27:18 +02004164<meta name="robots" content="index, nofollow"/>
Kay Sieversb87d78d2005-08-07 20:21:04 +02004165<title>$title</title>
Kay Sievers161332a2005-08-07 19:49:46 +02004166EOF
Giuseppe Bilotta41a4d162009-01-31 02:31:52 +01004167 # the stylesheet, favicon etc urls won't work correctly with path_info
4168 # unless we set the appropriate base URL
Giuseppe Bilottac3254ae2009-01-31 02:31:50 +01004169 if ($ENV{'PATH_INFO'}) {
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +01004170 print "<base href=\"".esc_url($base_url)."\" />\n";
Giuseppe Bilottac3254ae2009-01-31 02:31:50 +01004171 }
Jakub Narebski6ee90332011-06-22 13:50:46 +02004172 print_header_links($status);
Lénaïc Huardc1355b72011-10-21 09:09:29 +02004173
4174 if (defined $site_html_head_string) {
4175 print to_utf8($site_html_head_string);
4176 }
4177
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02004178 print "</head>\n" .
Alan Chandlerb2d34762006-10-03 13:49:03 +01004179 "<body>\n";
4180
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01004181 if (defined $site_header && -f $site_header) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004182 insert_file($site_header);
Alan Chandlerb2d34762006-10-03 13:49:03 +01004183 }
4184
Jonathan Nieder68220522010-09-03 19:45:09 -05004185 print "<div class=\"page_header\">\n";
4186 if (defined $logo) {
4187 print $cgi->a({-href => esc_url($logo_url),
4188 -title => $logo_label},
4189 $cgi->img({-src => esc_url($logo),
4190 -width => 72, -height => 27,
4191 -alt => "git",
4192 -class => "logo"}));
4193 }
Jakub Narebski6ee90332011-06-22 13:50:46 +02004194 print_nav_breadcrumbs(%opts);
Petr Baudisd77b5672007-05-17 04:24:19 +02004195 print "</div>\n";
4196
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004197 my $have_search = gitweb_check_feature('search');
Jakub Narebskif70dda22008-06-02 11:54:41 +02004198 if (defined $project && $have_search) {
Jakub Narebski6ee90332011-06-22 13:50:46 +02004199 print_search_form();
Kay Sievers4c02e3c2005-08-07 19:52:52 +02004200 }
Kay Sievers161332a2005-08-07 19:49:46 +02004201}
4202
Kay Sievers12a88f22005-08-07 20:02:47 +02004203sub git_footer_html {
Jakub Narebski35621982008-04-20 22:09:48 +02004204 my $feed_class = 'rss_logo';
4205
Kay Sievers6191f8e2005-08-07 20:19:56 +02004206 print "<div class=\"page_footer\">\n";
Kay Sieversb87d78d2005-08-07 20:21:04 +02004207 if (defined $project) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004208 my $descr = git_get_project_description($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02004209 if (defined $descr) {
Kay Sievers40c13812005-11-19 17:41:29 +01004210 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
Kay Sievers6191f8e2005-08-07 20:19:56 +02004211 }
Jakub Narebski35621982008-04-20 22:09:48 +02004212
4213 my %href_params = get_feed_info();
4214 if (!%href_params) {
4215 $feed_class .= ' generic';
4216 }
4217 $href_params{'-title'} ||= 'log';
4218
Ævar Arnfjörð Bjarmason0f54b7d2011-02-19 15:27:41 +00004219 foreach my $format (qw(RSS Atom)) {
Jakub Narebski35621982008-04-20 22:09:48 +02004220 $href_params{'action'} = lc($format);
4221 print $cgi->a({-href => href(%href_params),
4222 -title => "$href_params{'-title'} $format feed",
4223 -class => $feed_class}, $format)."\n";
4224 }
4225
Kay Sieversc994d622005-08-07 20:27:18 +02004226 } else {
Bernhard R. Link56efd9d2012-01-30 21:09:00 +01004227 print $cgi->a({-href => href(project=>undef, action=>"opml",
4228 project_filter => $project_filter),
Jakub Narebski35621982008-04-20 22:09:48 +02004229 -class => $feed_class}, "OPML") . " ";
Bernhard R. Link56efd9d2012-01-30 21:09:00 +01004230 print $cgi->a({-href => href(project=>undef, action=>"project_index",
4231 project_filter => $project_filter),
Jakub Narebski35621982008-04-20 22:09:48 +02004232 -class => $feed_class}, "TXT") . "\n";
Kay Sieversff7669a2005-08-07 20:13:02 +02004233 }
Jakub Narebski35621982008-04-20 22:09:48 +02004234 print "</div>\n"; # class="page_footer"
Alan Chandlerb2d34762006-10-03 13:49:03 +01004235
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02004236 if (defined $t0 && gitweb_check_feature('timed')) {
4237 print "<div id=\"generating_info\">\n";
4238 print 'This page took '.
4239 '<span id="generating_time" class="time_span">'.
Jakub Narebski3962f1d72010-11-09 19:27:54 +01004240 tv_interval($t0, [ gettimeofday() ]).
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02004241 ' seconds </span>'.
4242 ' and '.
4243 '<span id="generating_cmd">'.
4244 $number_of_git_cmds.
4245 '</span> git commands '.
4246 " to generate.\n";
4247 print "</div>\n"; # class="page_footer"
4248 }
4249
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01004250 if (defined $site_footer && -f $site_footer) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004251 insert_file($site_footer);
Alan Chandlerb2d34762006-10-03 13:49:03 +01004252 }
4253
Junio C Hamanoabf411e2010-12-15 11:32:57 -08004254 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004255 if (defined $action &&
4256 $action eq 'blame_incremental') {
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004257 print qq!<script type="text/javascript">\n!.
4258 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
4259 qq! "!. href() .qq!");\n!.
4260 qq!</script>\n!;
John 'Warthog9' Hawley291e52b2011-04-28 21:04:09 +02004261 } else {
Jakub Narebski2e987f92011-04-28 21:04:11 +02004262 my ($jstimezone, $tz_cookie, $datetime_class) =
4263 gitweb_get_feature('javascript-timezone');
4264
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004265 print qq!<script type="text/javascript">\n!.
Jakub Narebski2e987f92011-04-28 21:04:11 +02004266 qq!window.onload = function () {\n!;
4267 if (gitweb_check_feature('javascript-actions')) {
4268 print qq! fixLinks();\n!;
4269 }
4270 if ($jstimezone && $tz_cookie && $datetime_class) {
4271 print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
4272 qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
4273 }
4274 print qq!};\n!.
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004275 qq!</script>\n!;
4276 }
4277
Alan Chandlerb2d34762006-10-03 13:49:03 +01004278 print "</body>\n" .
Kay Sievers9cd3d982005-08-07 20:17:42 +02004279 "</html>";
Kay Sievers161332a2005-08-07 19:49:46 +02004280}
4281
Jakub Narebski453541f2010-02-07 21:51:18 +01004282# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
Lea Wiemann074afaa2008-06-19 22:03:21 +02004283# Example: die_error(404, 'Hash not found')
4284# By convention, use the following status codes (as defined in RFC 2616):
4285# 400: Invalid or missing CGI parameters, or
4286# requested object exists but has wrong type.
4287# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
4288# this server or project.
4289# 404: Requested object/revision/project doesn't exist.
4290# 500: The server isn't configured properly, or
4291# an internal error occurred (e.g. failed assertions caused by bugs), or
4292# an unknown error occurred (e.g. the git binary died unexpectedly).
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004293# 503: The server is currently unavailable (because it is overloaded,
4294# or down for maintenance). Generally, this is a temporary state.
Kay Sievers061cc7c2005-08-07 20:15:57 +02004295sub die_error {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004296 my $status = shift || 500;
Jakub Narebski1df48762010-02-07 21:52:25 +01004297 my $error = esc_html(shift) || "Internal Server Error";
John 'Warthog9' Hawleyaa140132010-01-30 23:30:44 +01004298 my $extra = shift;
Jakub Narebski7a597452010-04-24 16:00:04 +02004299 my %opts = @_;
Kay Sievers664f4cc2005-08-07 20:17:19 +02004300
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004301 my %http_responses = (
4302 400 => '400 Bad Request',
4303 403 => '403 Forbidden',
4304 404 => '404 Not Found',
4305 500 => '500 Internal Server Error',
4306 503 => '503 Service Unavailable',
4307 );
Jakub Narebski7a597452010-04-24 16:00:04 +02004308 git_header_html($http_responses{$status}, undef, %opts);
Jakub Narebski59b9f612006-08-22 23:42:53 +02004309 print <<EOF;
4310<div class="page_body">
4311<br /><br />
4312$status - $error
4313<br />
Jakub Narebski59b9f612006-08-22 23:42:53 +02004314EOF
John 'Warthog9' Hawleyaa140132010-01-30 23:30:44 +01004315 if (defined $extra) {
4316 print "<hr />\n" .
4317 "$extra\n";
4318 }
4319 print "</div>\n";
4320
Kay Sieversa59d4af2005-08-07 20:15:44 +02004321 git_footer_html();
Jakub Narebski7a597452010-04-24 16:00:04 +02004322 goto DONE_GITWEB
4323 unless ($opts{'-error_handler'});
Kay Sieversa59d4af2005-08-07 20:15:44 +02004324}
4325
Jakub Narebski717b8312006-07-31 21:22:15 +02004326## ----------------------------------------------------------------------
4327## functions printing or outputting HTML: navigation
4328
Jakub Narebski847e01f2006-08-14 02:05:47 +02004329sub git_print_page_nav {
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004330 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
4331 $extra = '' if !defined $extra; # pager or formats
4332
4333 my @navs = qw(summary shortlog log commit commitdiff tree);
4334 if ($suppress) {
4335 @navs = grep { $_ ne $suppress } @navs;
4336 }
4337
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004338 my %arg = map { $_ => {action=>$_} } @navs;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004339 if (defined $head) {
4340 for (qw(commit commitdiff)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02004341 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004342 }
4343 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4344 for (qw(shortlog log)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02004345 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004346 }
4347 }
4348 }
Petr Baudisd627f682008-10-02 16:36:52 +02004349
Jakub Narebski3be8e722007-04-01 22:22:21 +02004350 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4351 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004352
Junio C Hamanoa7c5a282008-11-29 13:02:08 -08004353 my @actions = gitweb_get_feature('actions');
Jakub Narebski2b11e052008-10-12 00:02:32 +02004354 my %repl = (
4355 '%' => '%',
4356 'n' => $project, # project name
4357 'f' => $git_dir, # project path within filesystem
4358 'h' => $treehead || '', # current hash ('h' parameter)
4359 'b' => $treebase || '', # hash base ('hb' parameter)
4360 );
Petr Baudisd627f682008-10-02 16:36:52 +02004361 while (@actions) {
Jakub Narebski2b11e052008-10-12 00:02:32 +02004362 my ($label, $link, $pos) = splice(@actions,0,3);
4363 # insert
Petr Baudisd627f682008-10-02 16:36:52 +02004364 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4365 # munch munch
Jakub Narebski2b11e052008-10-12 00:02:32 +02004366 $link =~ s/%([%nfhb])/$repl{$1}/g;
Petr Baudisd627f682008-10-02 16:36:52 +02004367 $arg{$label}{'_href'} = $link;
4368 }
4369
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004370 print "<div class=\"page_nav\">\n" .
4371 (join " | ",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004372 map { $_ eq $current ?
Petr Baudisd627f682008-10-02 16:36:52 +02004373 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004374 } @navs);
Jakub Narebski898a8932006-07-30 16:14:43 +02004375 print "<br/>\n$extra<br/>\n" .
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004376 "</div>\n";
4377}
4378
Ville Skyttä64127572017-06-25 13:20:41 +03004379# returns a submenu for the navigation of the refs views (tags, heads,
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01004380# remotes) with the current view disabled and the remotes view only
4381# available if the feature is enabled
4382sub format_ref_views {
4383 my ($current) = @_;
4384 my @ref_views = qw{tags heads};
4385 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4386 return join " | ", map {
4387 $_ eq $current ? $_ :
4388 $cgi->a({-href => href(action=>$_)}, $_)
4389 } @ref_views
4390}
4391
Jakub Narebski847e01f2006-08-14 02:05:47 +02004392sub format_paging_nav {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004393 my ($action, $page, $has_next_link) = @_;
Jakub Narebski43ffc062006-07-30 17:49:00 +02004394 my $paging_nav;
4395
4396
Jakub Narebski43ffc062006-07-30 17:49:00 +02004397 if ($page > 0) {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004398 $paging_nav .=
4399 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4400 " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01004401 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebski26298b52006-08-10 12:38:47 +02004402 -accesskey => "p", -title => "Alt-p"}, "prev");
Jakub Narebski43ffc062006-07-30 17:49:00 +02004403 } else {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004404 $paging_nav .= "first &sdot; prev";
Jakub Narebski43ffc062006-07-30 17:49:00 +02004405 }
4406
Lea Wiemann1f684dc2008-05-28 01:25:42 +02004407 if ($has_next_link) {
Jakub Narebski43ffc062006-07-30 17:49:00 +02004408 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01004409 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Jakub Narebski26298b52006-08-10 12:38:47 +02004410 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski43ffc062006-07-30 17:49:00 +02004411 } else {
4412 $paging_nav .= " &sdot; next";
4413 }
4414
4415 return $paging_nav;
4416}
4417
Jakub Narebski717b8312006-07-31 21:22:15 +02004418## ......................................................................
4419## functions printing or outputting HTML: div
Kay Sievers42f7eb92005-08-07 20:21:46 +02004420
Jakub Narebski847e01f2006-08-14 02:05:47 +02004421sub git_print_header_div {
Jakub Narebski717b8312006-07-31 21:22:15 +02004422 my ($action, $title, $hash, $hash_base) = @_;
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004423 my %args = ();
Jakub Narebski717b8312006-07-31 21:22:15 +02004424
Jakub Narebski3be8e722007-04-01 22:22:21 +02004425 $args{'action'} = $action;
4426 $args{'hash'} = $hash if $hash;
4427 $args{'hash_base'} = $hash_base if $hash_base;
Jakub Narebski717b8312006-07-31 21:22:15 +02004428
4429 print "<div class=\"header\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004430 $cgi->a({-href => href(%args), -class => "title"},
4431 $title ? $title : $action) .
4432 "\n</div>\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02004433}
4434
Giuseppe Bilotta0e656992010-11-11 13:26:15 +01004435sub format_repo_url {
4436 my ($name, $url) = @_;
4437 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4438}
4439
Giuseppe Bilottab891d522010-11-11 13:26:16 +01004440# Group output by placing it in a DIV element and adding a header.
4441# Options for start_div() can be provided by passing a hash reference as the
4442# first parameter to the function.
4443# Options to git_print_header_div() can be provided by passing an array
4444# reference. This must follow the options to start_div if they are present.
4445# The content can be a scalar, which is output as-is, a scalar reference, which
4446# is output after html escaping, an IO handle passed either as *handle or
4447# *handle{IO}, or a function reference. In the latter case all following
4448# parameters will be taken as argument to the content function call.
4449sub git_print_section {
4450 my ($div_args, $header_args, $content);
4451 my $arg = shift;
4452 if (ref($arg) eq 'HASH') {
4453 $div_args = $arg;
4454 $arg = shift;
4455 }
4456 if (ref($arg) eq 'ARRAY') {
4457 $header_args = $arg;
4458 $arg = shift;
4459 }
4460 $content = $arg;
4461
4462 print $cgi->start_div($div_args);
4463 git_print_header_div(@$header_args);
4464
4465 if (ref($content) eq 'CODE') {
4466 $content->(@_);
4467 } elsif (ref($content) eq 'SCALAR') {
4468 print esc_html($$content);
4469 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4470 print <$content>;
4471 } elsif (!ref($content) && defined($content)) {
4472 print $content;
4473 }
4474
4475 print $cgi->end_div;
4476}
4477
Jakub Narebski256b7b42011-04-28 21:04:07 +02004478sub format_timestamp_html {
Jakub Narebskice71b072011-04-28 21:04:08 +02004479 my $date = shift;
Jakub Narebski2e987f92011-04-28 21:04:11 +02004480 my $strtime = $date->{'rfc2822'};
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004481
Jakub Narebski2e987f92011-04-28 21:04:11 +02004482 my (undef, undef, $datetime_class) =
4483 gitweb_get_feature('javascript-timezone');
4484 if ($datetime_class) {
4485 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
Jakub Narebskia44465c2006-08-28 23:17:31 +02004486 }
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004487
Jakub Narebski256b7b42011-04-28 21:04:07 +02004488 my $localtime_format = '(%02d:%02d %s)';
4489 if ($date->{'hour_local'} < 6) {
4490 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
Jakub Narebskia44465c2006-08-28 23:17:31 +02004491 }
Jakub Narebski256b7b42011-04-28 21:04:07 +02004492 $strtime .= ' ' .
4493 sprintf($localtime_format,
4494 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004495
Jakub Narebski256b7b42011-04-28 21:04:07 +02004496 return $strtime;
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004497}
4498
4499# Outputs the author name and date in long form
4500sub git_print_authorship {
4501 my $co = shift;
4502 my %opts = @_;
4503 my $tag = $opts{-tag} || 'div';
Stephen Boyde133d652009-10-15 21:14:59 -07004504 my $author = $co->{'author_name'};
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004505
4506 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4507 print "<$tag class=\"author_date\">" .
Stephen Boyde133d652009-10-15 21:14:59 -07004508 format_search_author($author, "author", esc_html($author)) .
Jakub Narebskice71b072011-04-28 21:04:08 +02004509 " [".format_timestamp_html(\%ad)."]".
Jakub Narebski256b7b42011-04-28 21:04:07 +02004510 git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4511 "</$tag>\n";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004512}
4513
4514# Outputs table rows containing the full author or committer information,
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02004515# in the format expected for 'commit' view (& similar).
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004516# Parameters are a commit hash reference, followed by the list of people
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02004517# to output information for. If the list is empty it defaults to both
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004518# author and committer.
4519sub git_print_authorship_rows {
4520 my $co = shift;
4521 # too bad we can't use @people = @_ || ('author', 'committer')
4522 my @people = @_;
4523 @people = ('author', 'committer') unless @people;
4524 foreach my $who (@people) {
4525 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
Stephen Boyde133d652009-10-15 21:14:59 -07004526 print "<tr><td>$who</td><td>" .
4527 format_search_author($co->{"${who}_name"}, $who,
Jakub Narebski256b7b42011-04-28 21:04:07 +02004528 esc_html($co->{"${who}_name"})) . " " .
Stephen Boyde133d652009-10-15 21:14:59 -07004529 format_search_author($co->{"${who}_email"}, $who,
Jakub Narebski256b7b42011-04-28 21:04:07 +02004530 esc_html("<" . $co->{"${who}_email"} . ">")) .
Stephen Boyde133d652009-10-15 21:14:59 -07004531 "</td><td rowspan=\"2\">" .
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02004532 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4533 "</td></tr>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004534 "<tr>" .
Jakub Narebski256b7b42011-04-28 21:04:07 +02004535 "<td></td><td>" .
Jakub Narebskice71b072011-04-28 21:04:08 +02004536 format_timestamp_html(\%wd) .
Jakub Narebski256b7b42011-04-28 21:04:07 +02004537 "</td>" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004538 "</tr>\n";
4539 }
Jakub Narebski6fd92a22006-08-28 14:48:12 +02004540}
4541
Jakub Narebski717b8312006-07-31 21:22:15 +02004542sub git_print_page_path {
4543 my $name = shift;
4544 my $type = shift;
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004545 my $hb = shift;
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01004546
Jakub Narebski4df118e2006-10-21 17:53:55 +02004547
4548 print "<div class=\"page_path\">";
4549 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
Martin Koegler00f429a2007-06-03 17:42:44 +02004550 -title => 'tree root'}, to_utf8("[$project]"));
Jakub Narebski4df118e2006-10-21 17:53:55 +02004551 print " / ";
4552 if (defined $name) {
Jakub Narebski762c7202006-09-04 18:17:58 +02004553 my @dirname = split '/', $name;
4554 my $basename = pop @dirname;
4555 my $fullname = '';
4556
Jakub Narebski762c7202006-09-04 18:17:58 +02004557 foreach my $dir (@dirname) {
Petr Baudis16fdb482006-09-21 02:05:50 +02004558 $fullname .= ($fullname ? '/' : '') . $dir;
Jakub Narebski762c7202006-09-04 18:17:58 +02004559 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4560 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004561 -title => $fullname}, esc_path($dir));
Petr Baudis26d0a972006-09-23 01:00:12 +02004562 print " / ";
Jakub Narebski762c7202006-09-04 18:17:58 +02004563 }
4564 if (defined $type && $type eq 'blob') {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004565 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
Jakub Narebski762c7202006-09-04 18:17:58 +02004566 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004567 -title => $name}, esc_path($basename));
Jakub Narebski762c7202006-09-04 18:17:58 +02004568 } elsif (defined $type && $type eq 'tree') {
4569 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4570 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004571 -title => $name}, esc_path($basename));
Jakub Narebski4df118e2006-10-21 17:53:55 +02004572 print " / ";
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004573 } else {
Jakub Narebski403d0902006-11-08 11:48:56 +01004574 print esc_path($basename);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004575 }
Kay Sievers8ed23e12005-08-07 20:05:44 +02004576 }
Jakub Narebski4df118e2006-10-21 17:53:55 +02004577 print "<br/></div>\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02004578}
4579
Jakub Narebski74fd8722009-05-07 19:11:29 +02004580sub git_print_log {
Jakub Narebskid16d0932006-08-17 11:21:23 +02004581 my $log = shift;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004582 my %opts = @_;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004583
Jakub Narebskib7f92532006-08-28 14:48:10 +02004584 if ($opts{'-remove_title'}) {
4585 # remove title, i.e. first line of log
4586 shift @$log;
4587 }
Jakub Narebskid16d0932006-08-17 11:21:23 +02004588 # remove leading empty lines
4589 while (defined $log->[0] && $log->[0] eq "") {
4590 shift @$log;
4591 }
4592
4593 # print log
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004594 my $skip_blank_line = 0;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004595 foreach my $line (@$log) {
Namhyung Kim3d1110a2012-07-04 11:47:25 +09004596 if ($line =~ m/^\s*([A-Z][-A-Za-z]*-[Bb]y|C[Cc]): /) {
Jakub Narebskib7f92532006-08-28 14:48:10 +02004597 if (! $opts{'-remove_signoff'}) {
4598 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004599 $skip_blank_line = 1;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004600 }
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004601 next;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004602 }
4603
Namhyung Kim66c857e2012-07-04 11:47:26 +09004604 if ($line =~ m,\s*([a-z]*link): (https?://\S+),i) {
4605 if (! $opts{'-remove_signoff'}) {
4606 print "<span class=\"signoff\">" . esc_html($1) . ": " .
4607 "<a href=\"" . esc_html($2) . "\">" . esc_html($2) . "</a>" .
4608 "</span><br/>\n";
4609 $skip_blank_line = 1;
4610 }
4611 next;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004612 }
4613
4614 # print only one empty line
4615 # do not print empty line after signoff
4616 if ($line eq "") {
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004617 next if ($skip_blank_line);
4618 $skip_blank_line = 1;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004619 } else {
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004620 $skip_blank_line = 0;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004621 }
Jakub Narebskib7f92532006-08-28 14:48:10 +02004622
4623 print format_log_line_html($line) . "<br/>\n";
4624 }
4625
4626 if ($opts{'-final_empty_line'}) {
4627 # end with single empty line
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004628 print "<br/>\n" unless $skip_blank_line;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004629 }
4630}
4631
Jakub Narebskie33fba42006-12-10 13:25:46 +01004632# return link target (what link points to)
4633sub git_get_link_target {
4634 my $hash = shift;
4635 my $link_target;
4636
4637 # read link
4638 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4639 or return;
4640 {
Jakub Narebski34122b52009-05-11 03:29:40 +02004641 local $/ = undef;
Jakub Narebskie33fba42006-12-10 13:25:46 +01004642 $link_target = <$fd>;
4643 }
4644 close $fd
4645 or return;
4646
4647 return $link_target;
4648}
4649
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004650# given link target, and the directory (basedir) the link is in,
4651# return target of link relative to top directory (top tree);
4652# return undef if it is not possible (including absolute links).
4653sub normalize_link_target {
Jakub Narebski15c54fe2009-05-11 19:45:11 +02004654 my ($link_target, $basedir) = @_;
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004655
4656 # absolute symlinks (beginning with '/') cannot be normalized
4657 return if (substr($link_target, 0, 1) eq '/');
4658
4659 # normalize link target to path from top (root) tree (dir)
4660 my $path;
4661 if ($basedir) {
4662 $path = $basedir . '/' . $link_target;
4663 } else {
4664 # we are in top (root) tree (dir)
4665 $path = $link_target;
4666 }
4667
4668 # remove //, /./, and /../
4669 my @path_parts;
4670 foreach my $part (split('/', $path)) {
4671 # discard '.' and ''
4672 next if (!$part || $part eq '.');
4673 # handle '..'
4674 if ($part eq '..') {
4675 if (@path_parts) {
4676 pop @path_parts;
4677 } else {
4678 # link leads outside repository (outside top dir)
4679 return;
4680 }
4681 } else {
4682 push @path_parts, $part;
4683 }
4684 }
4685 $path = join('/', @path_parts);
4686
4687 return $path;
4688}
Jakub Narebskie33fba42006-12-10 13:25:46 +01004689
Jakub Narebskifa702002006-08-31 00:35:07 +02004690# print tree entry (row of git_tree), but without encompassing <tr> element
4691sub git_print_tree_entry {
4692 my ($t, $basedir, $hash_base, $have_blame) = @_;
4693
4694 my %base_key = ();
Jakub Narebskie33fba42006-12-10 13:25:46 +01004695 $base_key{'hash_base'} = $hash_base if defined $hash_base;
Jakub Narebskifa702002006-08-31 00:35:07 +02004696
Luben Tuikov4de741b2006-09-25 22:38:16 -07004697 # The format of a table row is: mode list link. Where mode is
4698 # the mode of the entry, list is the name of the entry, an href,
4699 # and link is the action links of the entry.
4700
Jakub Narebskifa702002006-08-31 00:35:07 +02004701 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004702 if (exists $t->{'size'}) {
4703 print "<td class=\"size\">$t->{'size'}</td>\n";
4704 }
Jakub Narebskifa702002006-08-31 00:35:07 +02004705 if ($t->{'type'} eq "blob") {
4706 print "<td class=\"list\">" .
Luben Tuikov4de741b2006-09-25 22:38:16 -07004707 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004708 file_name=>"$basedir$t->{'name'}", %base_key),
Jakub Narebskie33fba42006-12-10 13:25:46 +01004709 -class => "list"}, esc_path($t->{'name'}));
4710 if (S_ISLNK(oct $t->{'mode'})) {
4711 my $link_target = git_get_link_target($t->{'hash'});
4712 if ($link_target) {
Jakub Narebski15c54fe2009-05-11 19:45:11 +02004713 my $norm_target = normalize_link_target($link_target, $basedir);
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004714 if (defined $norm_target) {
4715 print " -> " .
4716 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4717 file_name=>$norm_target),
4718 -title => $norm_target}, esc_path($link_target));
4719 } else {
4720 print " -> " . esc_path($link_target);
4721 }
Jakub Narebskie33fba42006-12-10 13:25:46 +01004722 }
4723 }
4724 print "</td>\n";
Luben Tuikov4de741b2006-09-25 22:38:16 -07004725 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02004726 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004727 file_name=>"$basedir$t->{'name'}", %base_key)},
4728 "blob");
Jakub Narebskifa702002006-08-31 00:35:07 +02004729 if ($have_blame) {
Petr Baudis4777b012006-10-24 05:36:10 +02004730 print " | " .
4731 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004732 file_name=>"$basedir$t->{'name'}", %base_key)},
4733 "blame");
Jakub Narebskifa702002006-08-31 00:35:07 +02004734 }
4735 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02004736 print " | " .
4737 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02004738 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4739 "history");
4740 }
4741 print " | " .
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004742 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004743 file_name=>"$basedir$t->{'name'}")},
4744 "raw");
Luben Tuikov4de741b2006-09-25 22:38:16 -07004745 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02004746
4747 } elsif ($t->{'type'} eq "tree") {
Luben Tuikov0fa105e2006-09-26 12:45:37 -07004748 print "<td class=\"list\">";
4749 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004750 file_name=>"$basedir$t->{'name'}",
4751 %base_key)},
Jakub Narebski403d0902006-11-08 11:48:56 +01004752 esc_path($t->{'name'}));
Luben Tuikov0fa105e2006-09-26 12:45:37 -07004753 print "</td>\n";
4754 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02004755 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004756 file_name=>"$basedir$t->{'name'}",
4757 %base_key)},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004758 "tree");
Jakub Narebskifa702002006-08-31 00:35:07 +02004759 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02004760 print " | " .
4761 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02004762 file_name=>"$basedir$t->{'name'}")},
4763 "history");
4764 }
4765 print "</td>\n";
Jakub Narebski01ac1e32007-07-28 16:27:31 +02004766 } else {
4767 # unknown object: we can only present history for it
4768 # (this includes 'commit' object, i.e. submodule support)
4769 print "<td class=\"list\">" .
4770 esc_path($t->{'name'}) .
4771 "</td>\n";
4772 print "<td class=\"link\">";
4773 if (defined $hash_base) {
4774 print $cgi->a({-href => href(action=>"history",
4775 hash_base=>$hash_base,
4776 file_name=>"$basedir$t->{'name'}")},
4777 "history");
4778 }
4779 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02004780 }
4781}
4782
Jakub Narebski717b8312006-07-31 21:22:15 +02004783## ......................................................................
4784## functions printing large fragments of HTML
Kay Sieversede5e102005-08-07 20:23:12 +02004785
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004786# get pre-image filenames for merge (combined) diff
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004787sub fill_from_file_info {
4788 my ($diff, @parents) = @_;
4789
4790 $diff->{'from_file'} = [ ];
4791 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4792 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4793 if ($diff->{'status'}[$i] eq 'R' ||
4794 $diff->{'status'}[$i] eq 'C') {
4795 $diff->{'from_file'}[$i] =
4796 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4797 }
4798 }
4799
4800 return $diff;
4801}
4802
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004803# is current raw difftree line of file deletion
Jakub Narebski90921742007-06-08 13:27:42 +02004804sub is_deleted {
4805 my $diffinfo = shift;
4806
Jakub Narebski4ed4a342008-04-05 21:13:24 +01004807 return $diffinfo->{'to_id'} eq ('0' x 40);
Jakub Narebski90921742007-06-08 13:27:42 +02004808}
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004809
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004810# does patch correspond to [previous] difftree raw line
4811# $diffinfo - hashref of parsed raw diff format
4812# $patchinfo - hashref of parsed patch diff format
4813# (the same keys as in $diffinfo)
4814sub is_patch_split {
4815 my ($diffinfo, $patchinfo) = @_;
4816
4817 return defined $diffinfo && defined $patchinfo
Jakub Narebski9d301452007-11-01 12:38:08 +01004818 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004819}
4820
4821
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004822sub git_difftree_body {
Jakub Narebskied224de2007-05-07 01:10:04 +02004823 my ($difftree, $hash, @parents) = @_;
4824 my ($parent) = $parents[0];
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004825 my $have_blame = gitweb_check_feature('blame');
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004826 print "<div class=\"list_head\">\n";
4827 if ($#{$difftree} > 10) {
4828 print(($#{$difftree} + 1) . " files changed:\n");
4829 }
4830 print "</div>\n";
4831
Jakub Narebskied224de2007-05-07 01:10:04 +02004832 print "<table class=\"" .
4833 (@parents > 1 ? "combined " : "") .
4834 "diff_tree\">\n";
Jakub Narebski47598d72007-06-08 13:24:56 +02004835
4836 # header only for combined diff in 'commitdiff' view
Jakub Narebski3ef408a2007-09-08 21:54:28 +02004837 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
Jakub Narebski47598d72007-06-08 13:24:56 +02004838 if ($has_header) {
4839 # table header
4840 print "<thead><tr>\n" .
4841 "<th></th><th></th>\n"; # filename, patchN link
4842 for (my $i = 0; $i < @parents; $i++) {
4843 my $par = $parents[$i];
4844 print "<th>" .
4845 $cgi->a({-href => href(action=>"commitdiff",
4846 hash=>$hash, hash_parent=>$par),
4847 -title => 'commitdiff to parent number ' .
4848 ($i+1) . ': ' . substr($par,0,7)},
4849 $i+1) .
4850 "&nbsp;</th>\n";
4851 }
4852 print "</tr></thead>\n<tbody>\n";
4853 }
4854
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004855 my $alternate = 1;
Jakub Narebskib4657e72006-08-28 14:48:14 +02004856 my $patchno = 0;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004857 foreach my $line (@{$difftree}) {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004858 my $diff = parsed_difftree_line($line);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004859
4860 if ($alternate) {
4861 print "<tr class=\"dark\">\n";
4862 } else {
4863 print "<tr class=\"light\">\n";
4864 }
4865 $alternate ^= 1;
4866
Jakub Narebski493e01d2007-05-07 01:10:06 +02004867 if (exists $diff->{'nparents'}) { # combined diff
Jakub Narebskied224de2007-05-07 01:10:04 +02004868
Jakub Narebski493e01d2007-05-07 01:10:06 +02004869 fill_from_file_info($diff, @parents)
4870 unless exists $diff->{'from_file'};
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004871
Jakub Narebski90921742007-06-08 13:27:42 +02004872 if (!is_deleted($diff)) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004873 # file exists in the result (child) commit
4874 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02004875 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4876 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004877 hash_base=>$hash),
Jakub Narebski493e01d2007-05-07 01:10:06 +02004878 -class => "list"}, esc_path($diff->{'to_file'})) .
Jakub Narebskied224de2007-05-07 01:10:04 +02004879 "</td>\n";
4880 } else {
4881 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02004882 esc_path($diff->{'to_file'}) .
Jakub Narebskied224de2007-05-07 01:10:04 +02004883 "</td>\n";
4884 }
4885
4886 if ($action eq 'commitdiff') {
4887 # link to patch
4888 $patchno++;
4889 print "<td class=\"link\">" .
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004890 $cgi->a({-href => href(-anchor=>"patch$patchno")},
4891 "patch") .
Jakub Narebskied224de2007-05-07 01:10:04 +02004892 " | " .
4893 "</td>\n";
4894 }
4895
4896 my $has_history = 0;
4897 my $not_deleted = 0;
Jakub Narebski493e01d2007-05-07 01:10:06 +02004898 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004899 my $hash_parent = $parents[$i];
Jakub Narebski493e01d2007-05-07 01:10:06 +02004900 my $from_hash = $diff->{'from_id'}[$i];
4901 my $from_path = $diff->{'from_file'}[$i];
4902 my $status = $diff->{'status'}[$i];
Jakub Narebskied224de2007-05-07 01:10:04 +02004903
4904 $has_history ||= ($status ne 'A');
4905 $not_deleted ||= ($status ne 'D');
4906
Jakub Narebskied224de2007-05-07 01:10:04 +02004907 if ($status eq 'A') {
4908 print "<td class=\"link\" align=\"right\"> | </td>\n";
4909 } elsif ($status eq 'D') {
4910 print "<td class=\"link\">" .
4911 $cgi->a({-href => href(action=>"blob",
4912 hash_base=>$hash,
4913 hash=>$from_hash,
4914 file_name=>$from_path)},
4915 "blob" . ($i+1)) .
4916 " | </td>\n";
4917 } else {
Jakub Narebski493e01d2007-05-07 01:10:06 +02004918 if ($diff->{'to_id'} eq $from_hash) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004919 print "<td class=\"link nochange\">";
4920 } else {
4921 print "<td class=\"link\">";
4922 }
4923 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004924 hash=>$diff->{'to_id'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004925 hash_parent=>$from_hash,
4926 hash_base=>$hash,
4927 hash_parent_base=>$hash_parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004928 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004929 file_parent=>$from_path)},
4930 "diff" . ($i+1)) .
4931 " | </td>\n";
4932 }
4933 }
4934
4935 print "<td class=\"link\">";
4936 if ($not_deleted) {
4937 print $cgi->a({-href => href(action=>"blob",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004938 hash=>$diff->{'to_id'},
4939 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004940 hash_base=>$hash)},
4941 "blob");
4942 print " | " if ($has_history);
4943 }
4944 if ($has_history) {
4945 print $cgi->a({-href => href(action=>"history",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004946 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004947 hash_base=>$hash)},
4948 "history");
4949 }
4950 print "</td>\n";
4951
4952 print "</tr>\n";
4953 next; # instead of 'else' clause, to avoid extra indent
4954 }
4955 # else ordinary diff
4956
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004957 my ($to_mode_oct, $to_mode_str, $to_file_type);
4958 my ($from_mode_oct, $from_mode_str, $from_file_type);
Jakub Narebski493e01d2007-05-07 01:10:06 +02004959 if ($diff->{'to_mode'} ne ('0' x 6)) {
4960 $to_mode_oct = oct $diff->{'to_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004961 if (S_ISREG($to_mode_oct)) { # only for regular file
4962 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004963 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004964 $to_file_type = file_type($diff->{'to_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004965 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004966 if ($diff->{'from_mode'} ne ('0' x 6)) {
4967 $from_mode_oct = oct $diff->{'from_mode'};
Ævar Arnfjörð Bjarmason98885c22011-02-19 15:27:42 +00004968 if (S_ISREG($from_mode_oct)) { # only for regular file
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004969 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4970 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004971 $from_file_type = file_type($diff->{'from_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004972 }
4973
Jakub Narebski493e01d2007-05-07 01:10:06 +02004974 if ($diff->{'status'} eq "A") { # created
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004975 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4976 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4977 $mode_chng .= "]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07004978 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004979 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4980 hash_base=>$hash, file_name=>$diff->{'file'}),
4981 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07004982 print "</td>\n";
4983 print "<td>$mode_chng</td>\n";
4984 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02004985 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02004986 # link to patch
4987 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004988 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4989 "patch") .
4990 " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02004991 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004992 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4993 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski3faa5412007-01-08 02:10:42 +01004994 "blob");
Jakub Narebskib4657e72006-08-28 14:48:14 +02004995 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004996
Jakub Narebski493e01d2007-05-07 01:10:06 +02004997 } elsif ($diff->{'status'} eq "D") { # deleted
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004998 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07004999 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02005000 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
5001 hash_base=>$parent, file_name=>$diff->{'file'}),
5002 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07005003 print "</td>\n";
5004 print "<td>$mode_chng</td>\n";
5005 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02005006 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02005007 # link to patch
5008 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01005009 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
5010 "patch") .
5011 " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02005012 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02005013 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
5014 hash_base=>$parent, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005015 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005016 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01005017 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005018 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005019 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005020 }
Jakub Narebskib4657e72006-08-28 14:48:14 +02005021 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005022 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02005023 "history");
Luben Tuikov499faed2006-09-27 17:23:25 -07005024 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005025
Jakub Narebski493e01d2007-05-07 01:10:06 +02005026 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005027 my $mode_chnge = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02005028 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005029 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
Jakub Narebski6e72cf42007-01-03 20:47:24 +01005030 if ($from_file_type ne $to_file_type) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005031 $mode_chnge .= " from $from_file_type to $to_file_type";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005032 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005033 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
5034 if ($from_mode_str && $to_mode_str) {
5035 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
5036 } elsif ($to_mode_str) {
5037 $mode_chnge .= " mode: $to_mode_str";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005038 }
5039 }
5040 $mode_chnge .= "]</span>\n";
5041 }
5042 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02005043 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5044 hash_base=>$hash, file_name=>$diff->{'file'}),
5045 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07005046 print "</td>\n";
5047 print "<td>$mode_chnge</td>\n";
5048 print "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01005049 if ($action eq 'commitdiff') {
5050 # link to patch
5051 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01005052 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
5053 "patch") .
Jakub Narebski241cc592006-10-31 17:36:27 +01005054 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02005055 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01005056 # "commit" view and modified file (not onlu mode changed)
5057 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02005058 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01005059 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005060 file_name=>$diff->{'file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01005061 "diff") .
5062 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005063 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02005064 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5065 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005066 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005067 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01005068 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005069 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005070 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005071 }
Luben Tuikoveb51ec92006-09-27 17:24:49 -07005072 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005073 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02005074 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005075 print "</td>\n";
5076
Jakub Narebski493e01d2007-05-07 01:10:06 +02005077 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005078 my %status_name = ('R' => 'moved', 'C' => 'copied');
Jakub Narebski493e01d2007-05-07 01:10:06 +02005079 my $nstatus = $status_name{$diff->{'status'}};
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005080 my $mode_chng = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02005081 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005082 # mode also for directories, so we cannot use $to_mode_str
5083 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005084 }
5085 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005086 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005087 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
5088 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02005089 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
5090 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005091 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
5092 -class => "list"}, esc_path($diff->{'from_file'})) .
5093 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
Luben Tuikov499faed2006-09-27 17:23:25 -07005094 "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01005095 if ($action eq 'commitdiff') {
5096 # link to patch
5097 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01005098 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
5099 "patch") .
Jakub Narebski241cc592006-10-31 17:36:27 +01005100 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02005101 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01005102 # "commit" view and modified file (not only pure rename or copy)
5103 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02005104 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01005105 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005106 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01005107 "diff") .
5108 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005109 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02005110 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5111 hash_base=>$parent, file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005112 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005113 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01005114 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005115 file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005116 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005117 }
Jakub Narebski897d1d22006-11-19 22:51:39 +01005118 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005119 file_name=>$diff->{'to_file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02005120 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005121 print "</td>\n";
5122
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005123 } # we should not encounter Unmerged (U) or Unknown (X) status
5124 print "</tr>\n";
5125 }
Jakub Narebski47598d72007-06-08 13:24:56 +02005126 print "</tbody>" if $has_header;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005127 print "</table>\n";
5128}
5129
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005130# Print context lines and then rem/add lines in a side-by-side manner.
5131sub print_sidebyside_diff_lines {
5132 my ($ctx, $rem, $add) = @_;
5133
5134 # print context block before add/rem block
5135 if (@$ctx) {
5136 print join '',
5137 '<div class="chunk_block ctx">',
5138 '<div class="old">',
5139 @$ctx,
5140 '</div>',
5141 '<div class="new">',
5142 @$ctx,
5143 '</div>',
5144 '</div>';
5145 }
5146
5147 if (!@$add) {
5148 # pure removal
5149 print join '',
5150 '<div class="chunk_block rem">',
5151 '<div class="old">',
5152 @$rem,
5153 '</div>',
5154 '</div>';
5155 } elsif (!@$rem) {
5156 # pure addition
5157 print join '',
5158 '<div class="chunk_block add">',
5159 '<div class="new">',
5160 @$add,
5161 '</div>',
5162 '</div>';
5163 } else {
5164 print join '',
5165 '<div class="chunk_block chg">',
5166 '<div class="old">',
5167 @$rem,
5168 '</div>',
5169 '<div class="new">',
5170 @$add,
5171 '</div>',
5172 '</div>';
5173 }
5174}
5175
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005176# Print context lines and then rem/add lines in inline manner.
5177sub print_inline_diff_lines {
5178 my ($ctx, $rem, $add) = @_;
5179
5180 print @$ctx, @$rem, @$add;
5181}
5182
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005183# Format removed and added line, mark changed part and HTML-format them.
5184# Implementation is based on contrib/diff-highlight
5185sub format_rem_add_lines_pair {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005186 my ($rem, $add, $num_parents) = @_;
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005187
5188 # We need to untabify lines before split()'ing them;
5189 # otherwise offsets would be invalid.
5190 chomp $rem;
5191 chomp $add;
5192 $rem = untabify($rem);
5193 $add = untabify($add);
5194
5195 my @rem = split(//, $rem);
5196 my @add = split(//, $add);
5197 my ($esc_rem, $esc_add);
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005198 # Ignore leading +/- characters for each parent.
5199 my ($prefix_len, $suffix_len) = ($num_parents, 0);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005200 my ($prefix_has_nonspace, $suffix_has_nonspace);
5201
5202 my $shorter = (@rem < @add) ? @rem : @add;
5203 while ($prefix_len < $shorter) {
5204 last if ($rem[$prefix_len] ne $add[$prefix_len]);
5205
5206 $prefix_has_nonspace = 1 if ($rem[$prefix_len] !~ /\s/);
5207 $prefix_len++;
5208 }
5209
5210 while ($prefix_len + $suffix_len < $shorter) {
5211 last if ($rem[-1 - $suffix_len] ne $add[-1 - $suffix_len]);
5212
5213 $suffix_has_nonspace = 1 if ($rem[-1 - $suffix_len] !~ /\s/);
5214 $suffix_len++;
5215 }
5216
5217 # Mark lines that are different from each other, but have some common
5218 # part that isn't whitespace. If lines are completely different, don't
5219 # mark them because that would make output unreadable, especially if
5220 # diff consists of multiple lines.
5221 if ($prefix_has_nonspace || $suffix_has_nonspace) {
5222 $esc_rem = esc_html_hl_regions($rem, 'marked',
5223 [$prefix_len, @rem - $suffix_len], -nbsp=>1);
5224 $esc_add = esc_html_hl_regions($add, 'marked',
5225 [$prefix_len, @add - $suffix_len], -nbsp=>1);
5226 } else {
5227 $esc_rem = esc_html($rem, -nbsp=>1);
5228 $esc_add = esc_html($add, -nbsp=>1);
5229 }
5230
5231 return format_diff_line(\$esc_rem, 'rem'),
5232 format_diff_line(\$esc_add, 'add');
5233}
5234
5235# HTML-format diff context, removed and added lines.
5236sub format_ctx_rem_add_lines {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005237 my ($ctx, $rem, $add, $num_parents) = @_;
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005238 my (@new_ctx, @new_rem, @new_add);
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005239 my $can_highlight = 0;
5240 my $is_combined = ($num_parents > 1);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005241
5242 # Highlight if every removed line has a corresponding added line.
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005243 if (@$add > 0 && @$add == @$rem) {
5244 $can_highlight = 1;
5245
5246 # Highlight lines in combined diff only if the chunk contains
5247 # diff between the same version, e.g.
5248 #
5249 # - a
5250 # - b
5251 # + c
5252 # + d
5253 #
5254 # Otherwise the highlightling would be confusing.
5255 if ($is_combined) {
5256 for (my $i = 0; $i < @$add; $i++) {
5257 my $prefix_rem = substr($rem->[$i], 0, $num_parents);
5258 my $prefix_add = substr($add->[$i], 0, $num_parents);
5259
5260 $prefix_rem =~ s/-/+/g;
5261
5262 if ($prefix_rem ne $prefix_add) {
5263 $can_highlight = 0;
5264 last;
5265 }
5266 }
5267 }
5268 }
5269
5270 if ($can_highlight) {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005271 for (my $i = 0; $i < @$add; $i++) {
5272 my ($line_rem, $line_add) = format_rem_add_lines_pair(
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005273 $rem->[$i], $add->[$i], $num_parents);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005274 push @new_rem, $line_rem;
5275 push @new_add, $line_add;
5276 }
5277 } else {
5278 @new_rem = map { format_diff_line($_, 'rem') } @$rem;
5279 @new_add = map { format_diff_line($_, 'add') } @$add;
5280 }
5281
5282 @new_ctx = map { format_diff_line($_, 'ctx') } @$ctx;
5283
5284 return (\@new_ctx, \@new_rem, \@new_add);
5285}
5286
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005287# Print context lines and then rem/add lines.
5288sub print_diff_lines {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005289 my ($ctx, $rem, $add, $diff_style, $num_parents) = @_;
5290 my $is_combined = $num_parents > 1;
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005291
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005292 ($ctx, $rem, $add) = format_ctx_rem_add_lines($ctx, $rem, $add,
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005293 $num_parents);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005294
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005295 if ($diff_style eq 'sidebyside' && !$is_combined) {
5296 print_sidebyside_diff_lines($ctx, $rem, $add);
5297 } else {
5298 # default 'inline' style and unknown styles
5299 print_inline_diff_lines($ctx, $rem, $add);
5300 }
5301}
5302
5303sub print_diff_chunk {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005304 my ($diff_style, $num_parents, $from, $to, @chunk) = @_;
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005305 my (@ctx, @rem, @add);
5306
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005307 # The class of the previous line.
5308 my $prev_class = '';
5309
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005310 return unless @chunk;
5311
5312 # incomplete last line might be among removed or added lines,
5313 # or both, or among context lines: find which
5314 for (my $i = 1; $i < @chunk; $i++) {
5315 if ($chunk[$i][0] eq 'incomplete') {
5316 $chunk[$i][0] = $chunk[$i-1][0];
5317 }
5318 }
5319
5320 # guardian
5321 push @chunk, ["", ""];
5322
5323 foreach my $line_info (@chunk) {
5324 my ($class, $line) = @$line_info;
5325
5326 # print chunk headers
5327 if ($class && $class eq 'chunk_header') {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005328 print format_diff_line($line, $class, $from, $to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005329 next;
5330 }
5331
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005332 ## print from accumulator when have some add/rem lines or end
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005333 # of chunk (flush context lines), or when have add and rem
5334 # lines and new block is reached (otherwise add/rem lines could
5335 # be reordered)
5336 if (!$class || ((@rem || @add) && $class eq 'ctx') ||
5337 (@rem && @add && $class ne $prev_class)) {
5338 print_diff_lines(\@ctx, \@rem, \@add,
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005339 $diff_style, $num_parents);
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005340 @ctx = @rem = @add = ();
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005341 }
5342
5343 ## adding lines to accumulator
5344 # guardian value
5345 last unless $line;
5346 # rem, add or change
5347 if ($class eq 'rem') {
5348 push @rem, $line;
5349 } elsif ($class eq 'add') {
5350 push @add, $line;
5351 }
5352 # context line
5353 if ($class eq 'ctx') {
5354 push @ctx, $line;
5355 }
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005356
5357 $prev_class = $class;
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005358 }
5359}
5360
Jakub Narebskieee08902006-08-24 00:15:14 +02005361sub git_patchset_body {
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005362 my ($fd, $diff_style, $difftree, $hash, @hash_parents) = @_;
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02005363 my ($hash_parent) = $hash_parents[0];
Jakub Narebskieee08902006-08-24 00:15:14 +02005364
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005365 my $is_combined = (@hash_parents > 1);
Jakub Narebskieee08902006-08-24 00:15:14 +02005366 my $patch_idx = 0;
Martin Koegler4280cde2007-04-22 22:49:25 -07005367 my $patch_number = 0;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005368 my $patch_line;
Jakub Narebskife875852006-08-25 20:59:39 +02005369 my $diffinfo;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005370 my $to_name;
Jakub Narebski744d0ac2006-11-08 17:59:41 +01005371 my (%from, %to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005372 my @chunk; # for side-by-side diff
Jakub Narebskieee08902006-08-24 00:15:14 +02005373
5374 print "<div class=\"patchset\">\n";
5375
Jakub Narebski6d55f052006-11-18 23:35:39 +01005376 # skip to first patch
5377 while ($patch_line = <$fd>) {
Jakub Narebski157e43b2006-08-24 19:34:36 +02005378 chomp $patch_line;
Jakub Narebskieee08902006-08-24 00:15:14 +02005379
Jakub Narebski6d55f052006-11-18 23:35:39 +01005380 last if ($patch_line =~ m/^diff /);
5381 }
5382
5383 PATCH:
5384 while ($patch_line) {
Jakub Narebski6d55f052006-11-18 23:35:39 +01005385
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005386 # parse "git diff" header line
5387 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
5388 # $1 is from_name, which we do not use
5389 $to_name = unquote($2);
5390 $to_name =~ s!^b/!!;
5391 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
5392 # $1 is 'cc' or 'combined', which we do not use
5393 $to_name = unquote($2);
5394 } else {
5395 $to_name = undef;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005396 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01005397
5398 # check if current patch belong to current raw line
5399 # and parse raw git-diff line if needed
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005400 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
Jakub Narebski22065372007-05-12 12:42:32 +02005401 # this is continuation of a split patch
Jakub Narebski6d55f052006-11-18 23:35:39 +01005402 print "<div class=\"patch cont\">\n";
5403 } else {
5404 # advance raw git-diff output if needed
5405 $patch_idx++ if defined $diffinfo;
Jakub Narebskieee08902006-08-24 00:15:14 +02005406
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005407 # read and prepare patch information
5408 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5409
Jakub Narebskicd030c32007-06-08 13:33:28 +02005410 # compact combined diff output can have some patches skipped
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005411 # find which patch (using pathname of result) we are at now;
5412 if ($is_combined) {
5413 while ($to_name ne $diffinfo->{'to_file'}) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02005414 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5415 format_diff_cc_simplified($diffinfo, @hash_parents) .
5416 "</div>\n"; # class="patch"
5417
5418 $patch_idx++;
5419 $patch_number++;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005420
5421 last if $patch_idx > $#$difftree;
5422 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02005423 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005424 }
Jakub Narebski711fa742007-09-08 21:49:11 +02005425
Jakub Narebski90921742007-06-08 13:27:42 +02005426 # modifies %from, %to hashes
5427 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
Jakub Narebski5f855052007-05-17 22:54:28 +02005428
Jakub Narebski6d55f052006-11-18 23:35:39 +01005429 # this is first patch for raw difftree line with $patch_idx index
5430 # we index @$difftree array from 0, but number patches from 1
5431 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
Jakub Narebski744d0ac2006-11-08 17:59:41 +01005432 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005433
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005434 # git diff header
5435 #assert($patch_line =~ m/^diff /) if DEBUG;
5436 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
5437 $patch_number++;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005438 # print "git diff" header
Jakub Narebski90921742007-06-08 13:27:42 +02005439 print format_git_diff_header_line($patch_line, $diffinfo,
5440 \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02005441
Jakub Narebski6d55f052006-11-18 23:35:39 +01005442 # print extended diff header
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005443 print "<div class=\"diff extended_header\">\n";
Jakub Narebski6d55f052006-11-18 23:35:39 +01005444 EXTENDED_HEADER:
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005445 while ($patch_line = <$fd>) {
5446 chomp $patch_line;
5447
5448 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
5449
Jakub Narebski90921742007-06-08 13:27:42 +02005450 print format_extended_diff_header_line($patch_line, $diffinfo,
5451 \%from, \%to);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005452 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005453 print "</div>\n"; # class="diff extended_header"
Jakub Narebskie4e4f822006-08-25 21:04:13 +02005454
Jakub Narebski6d55f052006-11-18 23:35:39 +01005455 # from-file/to-file diff header
Jakub Narebski0bdb28c2007-01-10 00:07:43 +01005456 if (! $patch_line) {
5457 print "</div>\n"; # class="patch"
5458 last PATCH;
5459 }
Jakub Narebski66399ef2007-01-07 02:52:25 +01005460 next PATCH if ($patch_line =~ m/^diff /);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005461 #assert($patch_line =~ m/^---/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005462
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005463 my $last_patch_line = $patch_line;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005464 $patch_line = <$fd>;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005465 chomp $patch_line;
Jakub Narebski90921742007-06-08 13:27:42 +02005466 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005467
Jakub Narebski90921742007-06-08 13:27:42 +02005468 print format_diff_from_to_header($last_patch_line, $patch_line,
Jakub Narebski91af4ce2007-06-08 13:32:44 +02005469 $diffinfo, \%from, \%to,
5470 @hash_parents);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005471
5472 # the patch itself
5473 LINE:
5474 while ($patch_line = <$fd>) {
5475 chomp $patch_line;
5476
5477 next PATCH if ($patch_line =~ m/^diff /);
5478
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02005479 my $class = diff_line_class($patch_line, \%from, \%to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005480
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005481 if ($class eq 'chunk_header') {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005482 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005483 @chunk = ();
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005484 }
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005485
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02005486 push @chunk, [ $class, $patch_line ];
Jakub Narebskieee08902006-08-24 00:15:14 +02005487 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005488
Jakub Narebski6d55f052006-11-18 23:35:39 +01005489 } continue {
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005490 if (@chunk) {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005491 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005492 @chunk = ();
5493 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01005494 print "</div>\n"; # class="patch"
Jakub Narebskieee08902006-08-24 00:15:14 +02005495 }
Jakub Narebskid26c4262007-05-17 00:05:55 +02005496
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02005497 # for compact combined (--cc) format, with chunk and patch simplification
5498 # the patchset might be empty, but there might be unprocessed raw lines
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005499 for (++$patch_idx if $patch_number > 0;
Jakub Narebskicd030c32007-06-08 13:33:28 +02005500 $patch_idx < @$difftree;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005501 ++$patch_idx) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02005502 # read and prepare patch information
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005503 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02005504
5505 # generate anchor for "patch" links in difftree / whatchanged part
5506 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5507 format_diff_cc_simplified($diffinfo, @hash_parents) .
5508 "</div>\n"; # class="patch"
5509
5510 $patch_number++;
5511 }
5512
Jakub Narebskid26c4262007-05-17 00:05:55 +02005513 if ($patch_number == 0) {
5514 if (@hash_parents > 1) {
5515 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
5516 } else {
5517 print "<div class=\"diff nodifferences\">No differences found</div>\n";
5518 }
5519 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005520
5521 print "</div>\n"; # class="patchset"
5522}
5523
5524# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5525
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005526sub git_project_search_form {
Jakub Narebskib22939a2012-03-02 23:50:01 +01005527 my ($searchtext, $search_use_regexp) = @_;
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005528
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005529 my $limit = '';
5530 if ($project_filter) {
5531 $limit = " in '$project_filter/'";
5532 }
5533
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005534 print "<div class=\"projsearch\">\n";
Roland Mas4750f4b2014-10-16 08:54:47 +02005535 print $cgi->start_form(-method => 'get', -action => $my_uri) .
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005536 $cgi->hidden(-name => 'a', -value => 'project_list') . "\n";
5537 print $cgi->hidden(-name => 'pf', -value => $project_filter). "\n"
5538 if (defined $project_filter);
5539 print $cgi->textfield(-name => 's', -value => $searchtext,
5540 -title => "Search project by name and description$limit",
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005541 -size => 60) . "\n" .
5542 "<span title=\"Extended regular expression\">" .
5543 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
5544 -checked => $search_use_regexp) .
5545 "</span>\n" .
5546 $cgi->submit(-name => 'btnS', -value => 'Search') .
5547 $cgi->end_form() . "\n" .
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005548 $cgi->a({-href => href(project => undef, searchtext => undef,
5549 project_filter => $project_filter)},
5550 esc_html("List all projects$limit")) . "<br />\n";
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005551 print "</div>\n";
5552}
5553
Jakub Narebski14b289b2012-02-23 16:54:46 +01005554# entry for given @keys needs filling if at least one of keys in list
5555# is not present in %$project_info
5556sub project_info_needs_filling {
5557 my ($project_info, @keys) = @_;
5558
5559 # return List::MoreUtils::any { !exists $project_info->{$_} } @keys;
5560 foreach my $key (@keys) {
5561 if (!exists $project_info->{$key}) {
5562 return 1;
5563 }
5564 }
5565 return;
5566}
5567
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005568# fills project list info (age, description, owner, category, forks, etc.)
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005569# for each project in the list, removing invalid projects from
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005570# returned list, or fill only specified info.
5571#
5572# Invalid projects are removed from the returned list if and only if you
5573# ask 'age' or 'age_string' to be filled, because they are the only fields
5574# that run unconditionally git command that requires repository, and
5575# therefore do always check if project repository is invalid.
5576#
5577# USAGE:
5578# * fill_project_list_info(\@project_list, 'descr_long', 'ctags')
5579# ensures that 'descr_long' and 'ctags' fields are filled
5580# * @project_list = fill_project_list_info(\@project_list)
5581# ensures that all fields are filled (and invalid projects removed)
5582#
Jakub Narebski69913412008-06-10 19:21:01 +02005583# NOTE: modifies $projlist, but does not remove entries from it
5584sub fill_project_list_info {
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005585 my ($projlist, @wanted_keys) = @_;
Petr Baudise30496d2006-10-24 05:33:17 +02005586 my @projects;
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005587 my $filter_set = sub { return @_; };
5588 if (@wanted_keys) {
5589 my %wanted_keys = map { $_ => 1 } @wanted_keys;
5590 $filter_set = sub { return grep { $wanted_keys{$_} } @_; };
5591 }
Jakub Narebski69913412008-06-10 19:21:01 +02005592
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005593 my $show_ctags = gitweb_check_feature('ctags');
Jakub Narebski69913412008-06-10 19:21:01 +02005594 PROJECT:
Petr Baudise30496d2006-10-24 05:33:17 +02005595 foreach my $pr (@$projlist) {
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005596 if (project_info_needs_filling($pr, $filter_set->('age', 'age_string'))) {
Jakub Narebski14b289b2012-02-23 16:54:46 +01005597 my (@activity) = git_get_last_activity($pr->{'path'});
5598 unless (@activity) {
5599 next PROJECT;
5600 }
5601 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
Petr Baudise30496d2006-10-24 05:33:17 +02005602 }
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005603 if (project_info_needs_filling($pr, $filter_set->('descr', 'descr_long'))) {
Petr Baudise30496d2006-10-24 05:33:17 +02005604 my $descr = git_get_project_description($pr->{'path'}) || "";
Jakub Narebski69913412008-06-10 19:21:01 +02005605 $descr = to_utf8($descr);
5606 $pr->{'descr_long'} = $descr;
Michael Hendricks55feb122007-07-04 18:36:48 -06005607 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
Petr Baudise30496d2006-10-24 05:33:17 +02005608 }
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005609 if (project_info_needs_filling($pr, $filter_set->('owner'))) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02005610 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
Petr Baudise30496d2006-10-24 05:33:17 +02005611 }
Jakub Narebski14b289b2012-02-23 16:54:46 +01005612 if ($show_ctags &&
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005613 project_info_needs_filling($pr, $filter_set->('ctags'))) {
Jakub Narebski12b14432011-04-29 19:51:56 +02005614 $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
Petr Baudise30496d2006-10-24 05:33:17 +02005615 }
Jakub Narebski14b289b2012-02-23 16:54:46 +01005616 if ($projects_list_group_categories &&
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005617 project_info_needs_filling($pr, $filter_set->('category'))) {
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005618 my $cat = git_get_project_category($pr->{'path'}) ||
5619 $project_list_default_category;
5620 $pr->{'category'} = to_utf8($cat);
5621 }
5622
Petr Baudise30496d2006-10-24 05:33:17 +02005623 push @projects, $pr;
5624 }
5625
Jakub Narebski69913412008-06-10 19:21:01 +02005626 return @projects;
5627}
5628
Jakub Narebski12b14432011-04-29 19:51:56 +02005629sub sort_projects_list {
5630 my ($projlist, $order) = @_;
Jakub Narebski12b14432011-04-29 19:51:56 +02005631
Matthew Daley28dae182012-12-11 23:56:07 +13005632 sub order_str {
5633 my $key = shift;
5634 return sub { $a->{$key} cmp $b->{$key} };
Jakub Narebski12b14432011-04-29 19:51:56 +02005635 }
5636
Matthew Daley28dae182012-12-11 23:56:07 +13005637 sub order_num_then_undef {
5638 my $key = shift;
5639 return sub {
5640 defined $a->{$key} ?
5641 (defined $b->{$key} ? $a->{$key} <=> $b->{$key} : -1) :
5642 (defined $b->{$key} ? 1 : 0)
5643 };
5644 }
5645
5646 my %orderings = (
5647 project => order_str('path'),
5648 descr => order_str('descr_long'),
5649 owner => order_str('owner'),
5650 age => order_num_then_undef('age'),
5651 );
5652
5653 my $ordering = $orderings{$order};
5654 return defined $ordering ? sort $ordering @$projlist : @$projlist;
Jakub Narebski12b14432011-04-29 19:51:56 +02005655}
5656
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005657# returns a hash of categories, containing the list of project
5658# belonging to each category
5659sub build_projlist_by_category {
5660 my ($projlist, $from, $to) = @_;
5661 my %categories;
5662
5663 $from = 0 unless defined $from;
5664 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5665
5666 for (my $i = $from; $i <= $to; $i++) {
5667 my $pr = $projlist->[$i];
5668 push @{$categories{ $pr->{'category'} }}, $pr;
5669 }
5670
5671 return wantarray ? %categories : \%categories;
5672}
5673
Petr Baudis6b28da62008-09-25 18:48:37 +02005674# print 'sort by' <th> element, generating 'sort by $name' replay link
5675# if that order is not selected
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005676sub print_sort_th {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005677 print format_sort_th(@_);
5678}
5679
5680sub format_sort_th {
Petr Baudis6b28da62008-09-25 18:48:37 +02005681 my ($name, $order, $header) = @_;
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005682 my $sort_th = "";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005683 $header ||= ucfirst($name);
5684
5685 if ($order eq $name) {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005686 $sort_th .= "<th>$header</th>\n";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005687 } else {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005688 $sort_th .= "<th>" .
5689 $cgi->a({-href => href(-replay=>1, order=>$name),
5690 -class => "header"}, $header) .
5691 "</th>\n";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005692 }
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005693
5694 return $sort_th;
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005695}
5696
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005697sub git_project_list_rows {
5698 my ($projlist, $from, $to, $check_forks) = @_;
5699
5700 $from = 0 unless defined $from;
5701 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5702
5703 my $alternate = 1;
5704 for (my $i = $from; $i <= $to; $i++) {
5705 my $pr = $projlist->[$i];
5706
5707 if ($alternate) {
5708 print "<tr class=\"dark\">\n";
5709 } else {
5710 print "<tr class=\"light\">\n";
5711 }
5712 $alternate ^= 1;
5713
5714 if ($check_forks) {
5715 print "<td>";
5716 if ($pr->{'forks'}) {
5717 my $nforks = scalar @{$pr->{'forks'}};
5718 if ($nforks > 0) {
5719 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5720 -title => "$nforks forks"}, "+");
5721 } else {
5722 print $cgi->span({-title => "$nforks forks"}, "+");
5723 }
5724 }
5725 print "</td>\n";
5726 }
5727 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
Jakub Narebski07a40062012-02-27 02:55:20 +01005728 -class => "list"},
5729 esc_html_match_hl($pr->{'path'}, $search_regexp)) .
5730 "</td>\n" .
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005731 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005732 -class => "list",
Jakub Narebskie607b792012-02-27 02:55:22 +01005733 -title => $pr->{'descr_long'}},
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005734 $search_regexp
Jakub Narebskie607b792012-02-27 02:55:22 +01005735 ? esc_html_match_hl_chopped($pr->{'descr_long'},
5736 $pr->{'descr'}, $search_regexp)
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005737 : esc_html($pr->{'descr'})) .
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005738 "</td>\n";
5739 unless ($omit_owner) {
5740 print "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5741 }
Kacper Kornet5710be42012-04-24 19:39:15 +02005742 unless ($omit_age_column) {
5743 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5744 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n";
5745 }
5746 print"<td class=\"link\">" .
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005747 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
5748 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5749 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5750 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5751 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5752 "</td>\n" .
5753 "</tr>\n";
5754 }
5755}
5756
Jakub Narebski69913412008-06-10 19:21:01 +02005757sub git_project_list_body {
Petr Baudis42326112008-10-02 17:17:01 +02005758 # actually uses global variable $project
Jakub Narebski69913412008-06-10 19:21:01 +02005759 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
Jakub Narebski12b14432011-04-29 19:51:56 +02005760 my @projects = @$projlist;
Jakub Narebski69913412008-06-10 19:21:01 +02005761
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005762 my $check_forks = gitweb_check_feature('forks');
Jakub Narebski12b14432011-04-29 19:51:56 +02005763 my $show_ctags = gitweb_check_feature('ctags');
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01005764 my $tagfilter = $show_ctags ? $input_params{'ctag'} : undef;
Jakub Narebski12b14432011-04-29 19:51:56 +02005765 $check_forks = undef
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005766 if ($tagfilter || $search_regexp);
Jakub Narebski12b14432011-04-29 19:51:56 +02005767
5768 # filtering out forks before filling info allows to do less work
5769 @projects = filter_forks_from_projects_list(\@projects)
5770 if ($check_forks);
Jakub Narebski07b257f2012-02-23 16:54:48 +01005771 # search_projects_list pre-fills required info
Jakub Narebski12b14432011-04-29 19:51:56 +02005772 @projects = search_projects_list(\@projects,
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005773 'search_regexp' => $search_regexp,
Jakub Narebski12b14432011-04-29 19:51:56 +02005774 'tagfilter' => $tagfilter)
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005775 if ($tagfilter || $search_regexp);
Jakub Narebski07b257f2012-02-23 16:54:48 +01005776 # fill the rest
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005777 my @all_fields = ('descr', 'descr_long', 'ctags', 'category');
5778 push @all_fields, ('age', 'age_string') unless($omit_age_column);
5779 push @all_fields, 'owner' unless($omit_owner);
Kacper Kornet5710be42012-04-24 19:39:15 +02005780 @projects = fill_project_list_info(\@projects, @all_fields);
Jakub Narebski69913412008-06-10 19:21:01 +02005781
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02005782 $order ||= $default_projects_order;
Petr Baudise30496d2006-10-24 05:33:17 +02005783 $from = 0 unless defined $from;
5784 $to = $#projects if (!defined $to || $#projects < $to);
5785
Jakub Narebski12b14432011-04-29 19:51:56 +02005786 # short circuit
5787 if ($from > $to) {
5788 print "<center>\n".
5789 "<b>No such projects found</b><br />\n".
5790 "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
5791 "</center>\n<br />\n";
5792 return;
Petr Baudis6b28da62008-09-25 18:48:37 +02005793 }
5794
Jakub Narebski12b14432011-04-29 19:51:56 +02005795 @projects = sort_projects_list(\@projects, $order);
5796
Petr Baudisaed93de2008-10-02 17:13:02 +02005797 if ($show_ctags) {
Jakub Narebski0368c492011-04-29 19:51:57 +02005798 my $ctags = git_gather_all_ctags(\@projects);
5799 my $cloud = git_populate_project_tagcloud($ctags);
Petr Baudisaed93de2008-10-02 17:13:02 +02005800 print git_show_project_tagcloud($cloud, 64);
5801 }
5802
Petr Baudise30496d2006-10-24 05:33:17 +02005803 print "<table class=\"project_list\">\n";
5804 unless ($no_header) {
5805 print "<tr>\n";
5806 if ($check_forks) {
5807 print "<th></th>\n";
5808 }
Petr Baudis6b28da62008-09-25 18:48:37 +02005809 print_sort_th('project', $order, 'Project');
5810 print_sort_th('descr', $order, 'Description');
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005811 print_sort_th('owner', $order, 'Owner') unless $omit_owner;
Kacper Kornet5710be42012-04-24 19:39:15 +02005812 print_sort_th('age', $order, 'Last Change') unless $omit_age_column;
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005813 print "<th></th>\n" . # for links
Petr Baudise30496d2006-10-24 05:33:17 +02005814 "</tr>\n";
5815 }
Petr Baudis42326112008-10-02 17:17:01 +02005816
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005817 if ($projects_list_group_categories) {
5818 # only display categories with projects in the $from-$to window
5819 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5820 my %categories = build_projlist_by_category(\@projects, $from, $to);
5821 foreach my $cat (sort keys %categories) {
5822 unless ($cat eq "") {
5823 print "<tr>\n";
5824 if ($check_forks) {
5825 print "<td></td>\n";
Jakub Narebski12b14432011-04-29 19:51:56 +02005826 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005827 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5828 print "</tr>\n";
Petr Baudise30496d2006-10-24 05:33:17 +02005829 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005830
5831 git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
Petr Baudise30496d2006-10-24 05:33:17 +02005832 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005833 } else {
5834 git_project_list_rows(\@projects, $from, $to, $check_forks);
Petr Baudise30496d2006-10-24 05:33:17 +02005835 }
Jakub Narebski12b14432011-04-29 19:51:56 +02005836
Petr Baudise30496d2006-10-24 05:33:17 +02005837 if (defined $extra) {
5838 print "<tr>\n";
5839 if ($check_forks) {
5840 print "<td></td>\n";
5841 }
5842 print "<td colspan=\"5\">$extra</td>\n" .
5843 "</tr>\n";
5844 }
5845 print "</table>\n";
5846}
5847
Jakub Narebski42671ca2009-11-13 02:02:12 +01005848sub git_log_body {
5849 # uses global variable $project
5850 my ($commitlist, $from, $to, $refs, $extra) = @_;
5851
5852 $from = 0 unless defined $from;
5853 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5854
5855 for (my $i = 0; $i <= $to; $i++) {
5856 my %co = %{$commitlist->[$i]};
5857 next if !%co;
5858 my $commit = $co{'id'};
5859 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski42671ca2009-11-13 02:02:12 +01005860 git_print_header_div('commit',
5861 "<span class=\"age\">$co{'age_string'}</span>" .
5862 esc_html($co{'title'}) . $ref,
5863 $commit);
5864 print "<div class=\"title_text\">\n" .
5865 "<div class=\"log_link\">\n" .
5866 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5867 " | " .
5868 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5869 " | " .
5870 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5871 "<br/>\n" .
5872 "</div>\n";
5873 git_print_authorship(\%co, -tag => 'span');
5874 print "<br/>\n</div>\n";
5875
5876 print "<div class=\"log_body\">\n";
5877 git_print_log($co{'comment'}, -final_empty_line=> 1);
5878 print "</div>\n";
5879 }
5880 if ($extra) {
5881 print "<div class=\"page_nav\">\n";
5882 print "$extra\n";
5883 print "</div>\n";
5884 }
5885}
5886
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005887sub git_shortlog_body {
5888 # uses global variable $project
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005889 my ($commitlist, $from, $to, $refs, $extra) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05305890
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005891 $from = 0 unless defined $from;
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005892 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005893
Jakub Narebski591ebf62007-11-19 14:16:11 +01005894 print "<table class=\"shortlog\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005895 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005896 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005897 my %co = %{$commitlist->[$i]};
5898 my $commit = $co{'id'};
Jakub Narebski847e01f2006-08-14 02:05:47 +02005899 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005900 if ($alternate) {
5901 print "<tr class=\"dark\">\n";
5902 } else {
5903 print "<tr class=\"light\">\n";
5904 }
5905 $alternate ^= 1;
5906 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5907 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02005908 format_author_html('td', \%co, 10) . "<td>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02005909 print format_subject_html($co{'title'}, $co{'title_short'},
5910 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005911 print "</td>\n" .
5912 "<td class=\"link\">" .
Petr Baudis4777b012006-10-24 05:36:10 +02005913 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
Petr Baudis35749ae2006-09-22 03:19:44 +02005914 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
Petr Baudis55ff35c2006-10-06 15:57:52 +02005915 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02005916 my $snapshot_links = format_snapshot_links($commit);
5917 if (defined $snapshot_links) {
5918 print " | " . $snapshot_links;
Petr Baudis55ff35c2006-10-06 15:57:52 +02005919 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305920 print "</td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005921 "</tr>\n";
5922 }
5923 if (defined $extra) {
5924 print "<tr>\n" .
5925 "<td colspan=\"4\">$extra</td>\n" .
5926 "</tr>\n";
5927 }
5928 print "</table>\n";
5929}
5930
Jakub Narebski581860e2006-08-14 02:09:08 +02005931sub git_history_body {
5932 # Warning: assumes constant type (blob or tree) during history
Jakub Narebski69ca37d2009-11-13 02:02:14 +01005933 my ($commitlist, $from, $to, $refs, $extra,
5934 $file_name, $file_hash, $ftype) = @_;
Jakub Narebski8be68352006-09-11 00:36:04 +02005935
5936 $from = 0 unless defined $from;
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005937 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
Jakub Narebski581860e2006-08-14 02:09:08 +02005938
Jakub Narebski591ebf62007-11-19 14:16:11 +01005939 print "<table class=\"history\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005940 my $alternate = 1;
Jakub Narebski8be68352006-09-11 00:36:04 +02005941 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005942 my %co = %{$commitlist->[$i]};
Jakub Narebski581860e2006-08-14 02:09:08 +02005943 if (!%co) {
5944 next;
5945 }
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005946 my $commit = $co{'id'};
Jakub Narebski581860e2006-08-14 02:09:08 +02005947
5948 my $ref = format_ref_marker($refs, $commit);
5949
5950 if ($alternate) {
5951 print "<tr class=\"dark\">\n";
5952 } else {
5953 print "<tr class=\"light\">\n";
5954 }
5955 $alternate ^= 1;
5956 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02005957 # shortlog: format_author_html('td', \%co, 10)
5958 format_author_html('td', \%co, 15, 3) . "<td>";
Jakub Narebski581860e2006-08-14 02:09:08 +02005959 # originally git_history used chop_str($co{'title'}, 50)
Jakub Narebski952c65f2006-08-22 16:52:50 +02005960 print format_subject_html($co{'title'}, $co{'title_short'},
5961 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski581860e2006-08-14 02:09:08 +02005962 print "</td>\n" .
5963 "<td class=\"link\">" .
Luben Tuikov6d81c5a2006-09-28 17:21:07 -07005964 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
5965 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
Jakub Narebski581860e2006-08-14 02:09:08 +02005966
5967 if ($ftype eq 'blob') {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01005968 my $blob_current = $file_hash;
Jakub Narebski581860e2006-08-14 02:09:08 +02005969 my $blob_parent = git_get_hash_by_path($commit, $file_name);
5970 if (defined $blob_current && defined $blob_parent &&
5971 $blob_current ne $blob_parent) {
5972 print " | " .
Jakub Narebski420e92f2006-08-24 23:53:54 +02005973 $cgi->a({-href => href(action=>"blobdiff",
5974 hash=>$blob_current, hash_parent=>$blob_parent,
5975 hash_base=>$hash_base, hash_parent_base=>$commit,
5976 file_name=>$file_name)},
Jakub Narebski581860e2006-08-14 02:09:08 +02005977 "diff to current");
5978 }
5979 }
5980 print "</td>\n" .
5981 "</tr>\n";
5982 }
5983 if (defined $extra) {
5984 print "<tr>\n" .
5985 "<td colspan=\"4\">$extra</td>\n" .
5986 "</tr>\n";
5987 }
5988 print "</table>\n";
5989}
5990
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005991sub git_tags_body {
5992 # uses global variable $project
5993 my ($taglist, $from, $to, $extra) = @_;
5994 $from = 0 unless defined $from;
5995 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5996
Jakub Narebski591ebf62007-11-19 14:16:11 +01005997 print "<table class=\"tags\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005998 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005999 for (my $i = $from; $i <= $to; $i++) {
6000 my $entry = $taglist->[$i];
6001 my %tag = %$entry;
Jakub Narebskicd146402006-11-02 20:23:11 +01006002 my $comment = $tag{'subject'};
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006003 my $comment_short;
6004 if (defined $comment) {
6005 $comment_short = chop_str($comment, 30, 5);
6006 }
6007 if ($alternate) {
6008 print "<tr class=\"dark\">\n";
6009 } else {
6010 print "<tr class=\"light\">\n";
6011 }
6012 $alternate ^= 1;
Jakub Narebski27dd1a82007-01-03 22:54:29 +01006013 if (defined $tag{'age'}) {
6014 print "<td><i>$tag{'age'}</i></td>\n";
6015 } else {
6016 print "<td></td>\n";
6017 }
6018 print "<td>" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006019 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02006020 -class => "list name"}, esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006021 "</td>\n" .
6022 "<td>";
6023 if (defined $comment) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02006024 print format_subject_html($comment, $comment_short,
6025 href(action=>"tag", hash=>$tag{'id'}));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006026 }
6027 print "</td>\n" .
6028 "<td class=\"selflink\">";
6029 if ($tag{'type'} eq "tag") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006030 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006031 } else {
6032 print "&nbsp;";
6033 }
6034 print "</td>\n" .
6035 "<td class=\"link\">" . " | " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006036 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006037 if ($tag{'reftype'} eq "commit") {
Jakub Narebskibf901f82007-12-15 15:40:28 +01006038 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
6039 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006040 } elsif ($tag{'reftype'} eq "blob") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006041 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006042 }
6043 print "</td>\n" .
6044 "</tr>";
6045 }
6046 if (defined $extra) {
6047 print "<tr>\n" .
6048 "<td colspan=\"5\">$extra</td>\n" .
6049 "</tr>\n";
6050 }
6051 print "</table>\n";
6052}
6053
6054sub git_heads_body {
6055 # uses global variable $project
Jakub Narebskifd49e562012-02-15 16:36:41 +01006056 my ($headlist, $head_at, $from, $to, $extra) = @_;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006057 $from = 0 unless defined $from;
Jakub Narebski120ddde2006-09-19 14:33:22 +02006058 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006059
Jakub Narebski591ebf62007-11-19 14:16:11 +01006060 print "<table class=\"heads\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07006061 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006062 for (my $i = $from; $i <= $to; $i++) {
Jakub Narebski120ddde2006-09-19 14:33:22 +02006063 my $entry = $headlist->[$i];
Jakub Narebskicd146402006-11-02 20:23:11 +01006064 my %ref = %$entry;
Jakub Narebskifd49e562012-02-15 16:36:41 +01006065 my $curr = defined $head_at && $ref{'id'} eq $head_at;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006066 if ($alternate) {
6067 print "<tr class=\"dark\">\n";
6068 } else {
6069 print "<tr class=\"light\">\n";
6070 }
6071 $alternate ^= 1;
Jakub Narebskicd146402006-11-02 20:23:11 +01006072 print "<td><i>$ref{'age'}</i></td>\n" .
6073 ($curr ? "<td class=\"current_head\">" : "<td>") .
Jakub Narebskibf901f82007-12-15 15:40:28 +01006074 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
Jakub Narebskicd146402006-11-02 20:23:11 +01006075 -class => "list name"},esc_html($ref{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006076 "</td>\n" .
6077 "<td class=\"link\">" .
Jakub Narebskibf901f82007-12-15 15:40:28 +01006078 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
6079 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
Giuseppe Bilotta9e70e152010-11-11 13:26:08 +01006080 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006081 "</td>\n" .
6082 "</tr>";
6083 }
6084 if (defined $extra) {
6085 print "<tr>\n" .
6086 "<td colspan=\"3\">$extra</td>\n" .
6087 "</tr>\n";
6088 }
6089 print "</table>\n";
6090}
6091
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006092# Display a single remote block
6093sub git_remote_block {
6094 my ($remote, $rdata, $limit, $head) = @_;
6095
6096 my $heads = $rdata->{'heads'};
6097 my $fetch = $rdata->{'fetch'};
6098 my $push = $rdata->{'push'};
6099
6100 my $urls_table = "<table class=\"projects_list\">\n" ;
6101
6102 if (defined $fetch) {
6103 if ($fetch eq $push) {
6104 $urls_table .= format_repo_url("URL", $fetch);
6105 } else {
6106 $urls_table .= format_repo_url("Fetch URL", $fetch);
6107 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
6108 }
6109 } elsif (defined $push) {
6110 $urls_table .= format_repo_url("Push URL", $push);
6111 } else {
6112 $urls_table .= format_repo_url("", "No remote URL");
6113 }
6114
6115 $urls_table .= "</table>\n";
6116
6117 my $dots;
6118 if (defined $limit && $limit < @$heads) {
6119 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
6120 }
6121
6122 print $urls_table;
6123 git_heads_body($heads, $head, 0, $limit, $dots);
6124}
6125
6126# Display a list of remote names with the respective fetch and push URLs
6127sub git_remotes_list {
6128 my ($remotedata, $limit) = @_;
6129 print "<table class=\"heads\">\n";
6130 my $alternate = 1;
6131 my @remotes = sort keys %$remotedata;
6132
6133 my $limited = $limit && $limit < @remotes;
6134
6135 $#remotes = $limit - 1 if $limited;
6136
6137 while (my $remote = shift @remotes) {
6138 my $rdata = $remotedata->{$remote};
6139 my $fetch = $rdata->{'fetch'};
6140 my $push = $rdata->{'push'};
6141 if ($alternate) {
6142 print "<tr class=\"dark\">\n";
6143 } else {
6144 print "<tr class=\"light\">\n";
6145 }
6146 $alternate ^= 1;
6147 print "<td>" .
6148 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
6149 -class=> "list name"},esc_html($remote)) .
6150 "</td>";
6151 print "<td class=\"link\">" .
6152 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
6153 " | " .
6154 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
6155 "</td>";
6156
6157 print "</tr>\n";
6158 }
6159
6160 if ($limited) {
6161 print "<tr>\n" .
6162 "<td colspan=\"3\">" .
6163 $cgi->a({-href => href(action=>"remotes")}, "...") .
6164 "</td>\n" . "</tr>\n";
6165 }
6166
6167 print "</table>";
6168}
6169
6170# Display remote heads grouped by remote, unless there are too many
6171# remotes, in which case we only display the remote names
6172sub git_remotes_body {
6173 my ($remotedata, $limit, $head) = @_;
6174 if ($limit and $limit < keys %$remotedata) {
6175 git_remotes_list($remotedata, $limit);
6176 } else {
6177 fill_remote_heads($remotedata);
6178 while (my ($remote, $rdata) = each %$remotedata) {
6179 git_print_section({-class=>"remote", -id=>$remote},
6180 ["remotes", $remote, $remote], sub {
6181 git_remote_block($remote, $rdata, $limit, $head);
6182 });
6183 }
6184 }
6185}
6186
Jakub Narebski16f20722011-06-22 17:28:53 +02006187sub git_search_message {
6188 my %co = @_;
6189
6190 my $greptype;
6191 if ($searchtype eq 'commit') {
6192 $greptype = "--grep=";
6193 } elsif ($searchtype eq 'author') {
6194 $greptype = "--author=";
6195 } elsif ($searchtype eq 'committer') {
6196 $greptype = "--committer=";
6197 }
6198 $greptype .= $searchtext;
6199 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6200 $greptype, '--regexp-ignore-case',
6201 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6202
6203 my $paging_nav = '';
6204 if ($page > 0) {
6205 $paging_nav .=
Jakub Narebski882541b2011-06-22 17:28:54 +02006206 $cgi->a({-href => href(-replay=>1, page=>undef)},
6207 "first") .
6208 " &sdot; " .
Jakub Narebski16f20722011-06-22 17:28:53 +02006209 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6210 -accesskey => "p", -title => "Alt-p"}, "prev");
6211 } else {
Jakub Narebski882541b2011-06-22 17:28:54 +02006212 $paging_nav .= "first &sdot; prev";
Jakub Narebski16f20722011-06-22 17:28:53 +02006213 }
6214 my $next_link = '';
6215 if ($#commitlist >= 100) {
6216 $next_link =
6217 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6218 -accesskey => "n", -title => "Alt-n"}, "next");
6219 $paging_nav .= " &sdot; $next_link";
6220 } else {
6221 $paging_nav .= " &sdot; next";
6222 }
6223
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006224 git_header_html();
6225
Jakub Narebski16f20722011-06-22 17:28:53 +02006226 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6227 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6228 if ($page == 0 && !@commitlist) {
6229 print "<p>No match.</p>\n";
6230 } else {
6231 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6232 }
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006233
6234 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006235}
6236
6237sub git_search_changes {
6238 my %co = @_;
6239
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006240 local $/ = "\n";
6241 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6242 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6243 ($search_use_regexp ? '--pickaxe-regex' : ())
6244 or die_error(500, "Open git-log failed");
6245
6246 git_header_html();
6247
Jakub Narebski16f20722011-06-22 17:28:53 +02006248 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6249 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6250
6251 print "<table class=\"pickaxe search\">\n";
6252 my $alternate = 1;
Jakub Narebski16f20722011-06-22 17:28:53 +02006253 undef %co;
6254 my @files;
6255 while (my $line = <$fd>) {
6256 chomp $line;
6257 next unless $line;
6258
6259 my %set = parse_difftree_raw_line($line);
6260 if (defined $set{'commit'}) {
6261 # finish previous commit
6262 if (%co) {
6263 print "</td>\n" .
6264 "<td class=\"link\">" .
Jakub Narebski882541b2011-06-22 17:28:54 +02006265 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6266 "commit") .
Jakub Narebski16f20722011-06-22 17:28:53 +02006267 " | " .
Jakub Narebski882541b2011-06-22 17:28:54 +02006268 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6269 hash_base=>$co{'id'})},
6270 "tree") .
6271 "</td>\n" .
Jakub Narebski16f20722011-06-22 17:28:53 +02006272 "</tr>\n";
6273 }
6274
6275 if ($alternate) {
6276 print "<tr class=\"dark\">\n";
6277 } else {
6278 print "<tr class=\"light\">\n";
6279 }
6280 $alternate ^= 1;
6281 %co = parse_commit($set{'commit'});
6282 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6283 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6284 "<td><i>$author</i></td>\n" .
6285 "<td>" .
6286 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6287 -class => "list subject"},
6288 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6289 } elsif (defined $set{'to_id'}) {
6290 next if ($set{'to_id'} =~ m/^0{40}$/);
6291
6292 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6293 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6294 -class => "list"},
6295 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6296 "<br/>\n";
6297 }
6298 }
6299 close $fd;
6300
6301 # finish last commit (warning: repetition!)
6302 if (%co) {
6303 print "</td>\n" .
6304 "<td class=\"link\">" .
Jakub Narebski882541b2011-06-22 17:28:54 +02006305 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6306 "commit") .
Jakub Narebski16f20722011-06-22 17:28:53 +02006307 " | " .
Jakub Narebski882541b2011-06-22 17:28:54 +02006308 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6309 hash_base=>$co{'id'})},
6310 "tree") .
6311 "</td>\n" .
Jakub Narebski16f20722011-06-22 17:28:53 +02006312 "</tr>\n";
6313 }
6314
6315 print "</table>\n";
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006316
6317 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006318}
6319
6320sub git_search_files {
6321 my %co = @_;
6322
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006323 local $/ = "\n";
Jakub Narebski8e09fd12012-01-05 21:32:56 +01006324 open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006325 $search_use_regexp ? ('-E', '-i') : '-F',
6326 $searchtext, $co{'tree'}
6327 or die_error(500, "Open git-grep failed");
6328
6329 git_header_html();
6330
Jakub Narebski16f20722011-06-22 17:28:53 +02006331 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6332 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6333
6334 print "<table class=\"grep_search\">\n";
6335 my $alternate = 1;
6336 my $matches = 0;
Jakub Narebski16f20722011-06-22 17:28:53 +02006337 my $lastfile = '';
Jakub Narebskifc8fcd22012-02-15 17:37:06 +01006338 my $file_href;
Jakub Narebski16f20722011-06-22 17:28:53 +02006339 while (my $line = <$fd>) {
6340 chomp $line;
Jakub Narebskifc8fcd22012-02-15 17:37:06 +01006341 my ($file, $lno, $ltext, $binary);
Jakub Narebski16f20722011-06-22 17:28:53 +02006342 last if ($matches++ > 1000);
6343 if ($line =~ /^Binary file (.+) matches$/) {
6344 $file = $1;
6345 $binary = 1;
6346 } else {
Jakub Narebski8e09fd12012-01-05 21:32:56 +01006347 ($file, $lno, $ltext) = split(/\0/, $line, 3);
6348 $file =~ s/^$co{'tree'}://;
Jakub Narebski16f20722011-06-22 17:28:53 +02006349 }
6350 if ($file ne $lastfile) {
6351 $lastfile and print "</td></tr>\n";
6352 if ($alternate++) {
6353 print "<tr class=\"dark\">\n";
6354 } else {
6355 print "<tr class=\"light\">\n";
6356 }
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006357 $file_href = href(action=>"blob", hash_base=>$co{'id'},
6358 file_name=>$file);
Jakub Narebski16f20722011-06-22 17:28:53 +02006359 print "<td class=\"list\">".
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006360 $cgi->a({-href => $file_href, -class => "list"}, esc_path($file));
Jakub Narebski16f20722011-06-22 17:28:53 +02006361 print "</td><td>\n";
6362 $lastfile = $file;
6363 }
6364 if ($binary) {
6365 print "<div class=\"binary\">Binary file</div>\n";
6366 } else {
6367 $ltext = untabify($ltext);
6368 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6369 $ltext = esc_html($1, -nbsp=>1);
6370 $ltext .= '<span class="match">';
6371 $ltext .= esc_html($2, -nbsp=>1);
6372 $ltext .= '</span>';
6373 $ltext .= esc_html($3, -nbsp=>1);
6374 } else {
6375 $ltext = esc_html($ltext, -nbsp=>1);
6376 }
6377 print "<div class=\"pre\">" .
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006378 $cgi->a({-href => $file_href.'#l'.$lno,
6379 -class => "linenr"}, sprintf('%4i', $lno)) .
6380 ' ' . $ltext . "</div>\n";
Jakub Narebski16f20722011-06-22 17:28:53 +02006381 }
6382 }
6383 if ($lastfile) {
6384 print "</td></tr>\n";
6385 if ($matches > 1000) {
6386 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6387 }
6388 } else {
6389 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6390 }
6391 close $fd;
6392
6393 print "</table>\n";
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006394
6395 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006396}
6397
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006398sub git_search_grep_body {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006399 my ($commitlist, $from, $to, $extra) = @_;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006400 $from = 0 unless defined $from;
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006401 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006402
Jakub Narebski591ebf62007-11-19 14:16:11 +01006403 print "<table class=\"commit_search\">\n";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006404 my $alternate = 1;
6405 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006406 my %co = %{$commitlist->[$i]};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006407 if (!%co) {
6408 next;
6409 }
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006410 my $commit = $co{'id'};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006411 if ($alternate) {
6412 print "<tr class=\"dark\">\n";
6413 } else {
6414 print "<tr class=\"light\">\n";
6415 }
6416 $alternate ^= 1;
6417 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02006418 format_author_html('td', \%co, 15, 5) .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006419 "<td>" .
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006420 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6421 -class => "list subject"},
6422 chop_and_escape_str($co{'title'}, 50) . "<br/>");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006423 my $comment = $co{'comment'};
6424 foreach my $line (@$comment) {
Jakub Narebski6dfbb302008-03-02 16:57:14 +01006425 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006426 my ($lead, $match, $trail) = ($1, $2, $3);
Jakub Narebskib8d97d02008-02-25 21:07:57 +01006427 $match = chop_str($match, 70, 5, 'center');
6428 my $contextlen = int((80 - length($match))/2);
6429 $contextlen = 30 if ($contextlen > 30);
6430 $lead = chop_str($lead, $contextlen, 10, 'left');
6431 $trail = chop_str($trail, $contextlen, 10, 'right');
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006432
6433 $lead = esc_html($lead);
6434 $match = esc_html($match);
6435 $trail = esc_html($trail);
6436
6437 print "$lead<span class=\"match\">$match</span>$trail<br />";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006438 }
6439 }
6440 print "</td>\n" .
6441 "<td class=\"link\">" .
6442 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6443 " | " .
Denis Chengf1fe8f52007-11-26 20:42:06 +08006444 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
6445 " | " .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006446 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6447 print "</td>\n" .
6448 "</tr>\n";
6449 }
6450 if (defined $extra) {
6451 print "<tr>\n" .
6452 "<td colspan=\"3\">$extra</td>\n" .
6453 "</tr>\n";
6454 }
6455 print "</table>\n";
6456}
6457
Jakub Narebski717b8312006-07-31 21:22:15 +02006458## ======================================================================
6459## ======================================================================
6460## actions
6461
Jakub Narebski717b8312006-07-31 21:22:15 +02006462sub git_project_list {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02006463 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02006464 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006465 die_error(400, "Unknown order parameter");
Jakub Narebski6326b602006-08-01 02:59:12 +02006466 }
6467
Bernhard R. Link19d2d232012-01-30 21:07:37 +01006468 my @list = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski717b8312006-07-31 21:22:15 +02006469 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006470 die_error(404, "No projects found");
Jakub Narebski717b8312006-07-31 21:22:15 +02006471 }
Jakub Narebski6326b602006-08-01 02:59:12 +02006472
Jakub Narebski717b8312006-07-31 21:22:15 +02006473 git_header_html();
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01006474 if (defined $home_text && -f $home_text) {
Jakub Narebski717b8312006-07-31 21:22:15 +02006475 print "<div class=\"index_include\">\n";
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01006476 insert_file($home_text);
Jakub Narebski717b8312006-07-31 21:22:15 +02006477 print "</div>\n";
6478 }
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01006479
6480 git_project_search_form($searchtext, $search_use_regexp);
Petr Baudise30496d2006-10-24 05:33:17 +02006481 git_project_list_body(\@list, $order);
6482 git_footer_html();
6483}
6484
6485sub git_forks {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02006486 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02006487 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006488 die_error(400, "Unknown order parameter");
Jakub Narebski717b8312006-07-31 21:22:15 +02006489 }
Petr Baudise30496d2006-10-24 05:33:17 +02006490
Bernhard R. Link4c7cd172012-01-30 21:05:47 +01006491 my $filter = $project;
6492 $filter =~ s/\.git$//;
6493 my @list = git_get_projects_list($filter);
Petr Baudise30496d2006-10-24 05:33:17 +02006494 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006495 die_error(404, "No forks found");
Jakub Narebski717b8312006-07-31 21:22:15 +02006496 }
Petr Baudise30496d2006-10-24 05:33:17 +02006497
6498 git_header_html();
6499 git_print_page_nav('','');
6500 git_print_header_div('summary', "$project forks");
6501 git_project_list_body(\@list, $order);
Jakub Narebski717b8312006-07-31 21:22:15 +02006502 git_footer_html();
6503}
6504
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006505sub git_project_index {
Bernhard R. Link19d2d232012-01-30 21:07:37 +01006506 my @projects = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski12b14432011-04-29 19:51:56 +02006507 if (!@projects) {
6508 die_error(404, "No projects found");
6509 }
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006510
6511 print $cgi->header(
6512 -type => 'text/plain',
6513 -charset => 'utf-8',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02006514 -content_disposition => 'inline; filename="index.aux"');
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006515
6516 foreach my $pr (@projects) {
6517 if (!exists $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02006518 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006519 }
6520
6521 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
6522 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
6523 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6524 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6525 $path =~ s/ /\+/g;
6526 $owner =~ s/ /\+/g;
6527
6528 print "$path $owner\n";
6529 }
6530}
6531
Kay Sieversede5e102005-08-07 20:23:12 +02006532sub git_summary {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006533 my $descr = git_get_project_description($project) || "none";
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00006534 my %co = parse_commit("HEAD");
Jakub Narebski785cdea2007-05-13 12:39:22 +02006535 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00006536 my $head = $co{'id'};
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006537 my $remote_heads = gitweb_check_feature('remote_heads');
Kay Sieversede5e102005-08-07 20:23:12 +02006538
Jakub Narebski1e0cf032006-08-14 02:10:06 +02006539 my $owner = git_get_project_owner($project);
Kay Sieversede5e102005-08-07 20:23:12 +02006540
Jakub Narebskicd146402006-11-02 20:23:11 +01006541 my $refs = git_get_references();
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006542 # These get_*_list functions return one more to allow us to see if
6543 # there are more ...
6544 my @taglist = git_get_tags_list(16);
6545 my @headlist = git_get_heads_list(16);
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006546 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
Petr Baudise30496d2006-10-24 05:33:17 +02006547 my @forklist;
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006548 my $check_forks = gitweb_check_feature('forks');
Junio C Hamano5dd5ed02006-11-07 22:00:45 -08006549
6550 if ($check_forks) {
Jakub Narebski12b14432011-04-29 19:51:56 +02006551 # find forks of a project
Bernhard R. Link4c7cd172012-01-30 21:05:47 +01006552 my $filter = $project;
6553 $filter =~ s/\.git$//;
6554 @forklist = git_get_projects_list($filter);
Jakub Narebski12b14432011-04-29 19:51:56 +02006555 # filter out forks of forks
6556 @forklist = filter_forks_from_projects_list(\@forklist)
6557 if (@forklist);
Petr Baudise30496d2006-10-24 05:33:17 +02006558 }
Jakub Narebski120ddde2006-09-19 14:33:22 +02006559
Kay Sieversede5e102005-08-07 20:23:12 +02006560 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02006561 git_print_page_nav('summary','', $head);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006562
Kay Sievers19806692005-08-07 20:26:27 +02006563 print "<div class=\"title\">&nbsp;</div>\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01006564 print "<table class=\"projects_list\">\n" .
Kacper Kornet0ebe7822012-04-26 18:45:44 +02006565 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n";
Tony Finch860ccc62013-08-20 17:59:44 +01006566 if ($owner and not $omit_owner) {
Kacper Kornet0ebe7822012-04-26 18:45:44 +02006567 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
6568 }
Jakub Narebski785cdea2007-05-13 12:39:22 +02006569 if (defined $cd{'rfc2822'}) {
Jakub Narebski256b7b42011-04-28 21:04:07 +02006570 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
6571 "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
Jakub Narebski785cdea2007-05-13 12:39:22 +02006572 }
6573
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02006574 # use per project git URL list in $projectroot/$project/cloneurl
6575 # or make project git URL from git base URL and project name
Jakub Narebski19a87212006-08-15 23:03:17 +02006576 my $url_tag = "URL";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02006577 my @url_list = git_get_project_url_list($project);
6578 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
6579 foreach my $git_url (@url_list) {
6580 next unless $git_url;
Giuseppe Bilotta0e656992010-11-11 13:26:15 +01006581 print format_repo_url($url_tag, $git_url);
Jakub Narebski19a87212006-08-15 23:03:17 +02006582 $url_tag = "";
6583 }
Petr Baudisaed93de2008-10-02 17:13:02 +02006584
6585 # Tag cloud
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006586 my $show_ctags = gitweb_check_feature('ctags');
Petr Baudisaed93de2008-10-02 17:13:02 +02006587 if ($show_ctags) {
6588 my $ctags = git_get_project_ctags($project);
Jakub Narebski0368c492011-04-29 19:51:57 +02006589 if (%$ctags) {
6590 # without ability to add tags, don't show if there are none
6591 my $cloud = git_populate_project_tagcloud($ctags);
6592 print "<tr id=\"metadata_ctags\">" .
6593 "<td>content tags</td>" .
6594 "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
6595 "</tr>\n";
6596 }
Petr Baudisaed93de2008-10-02 17:13:02 +02006597 }
6598
Jakub Narebski19a87212006-08-15 23:03:17 +02006599 print "</table>\n";
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006600
Matt McCutchen7e1100e2009-02-07 19:00:09 -05006601 # If XSS prevention is on, we don't include README.html.
6602 # TODO: Allow a readme in some safe format.
6603 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01006604 print "<div class=\"title\">readme</div>\n" .
6605 "<div class=\"readme\">\n";
6606 insert_file("$projectroot/$project/README.html");
6607 print "\n</div>\n"; # class="readme"
Petr Baudis447ef092006-10-24 05:23:46 +02006608 }
6609
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006610 # we need to request one more than 16 (0..15) to check if
6611 # those 16 are all
Jakub Narebski785cdea2007-05-13 12:39:22 +02006612 my @commitlist = $head ? parse_commits($head, 17) : ();
6613 if (@commitlist) {
6614 git_print_header_div('shortlog');
6615 git_shortlog_body(\@commitlist, 0, 15, $refs,
6616 $#commitlist <= 15 ? undef :
6617 $cgi->a({-href => href(action=>"shortlog")}, "..."));
6618 }
Kay Sieversede5e102005-08-07 20:23:12 +02006619
Jakub Narebski120ddde2006-09-19 14:33:22 +02006620 if (@taglist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006621 git_print_header_div('tags');
Jakub Narebski120ddde2006-09-19 14:33:22 +02006622 git_tags_body(\@taglist, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006623 $#taglist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006624 $cgi->a({-href => href(action=>"tags")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02006625 }
Kay Sievers0db37972005-08-07 20:24:35 +02006626
Jakub Narebski120ddde2006-09-19 14:33:22 +02006627 if (@headlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006628 git_print_header_div('heads');
Jakub Narebski120ddde2006-09-19 14:33:22 +02006629 git_heads_body(\@headlist, $head, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006630 $#headlist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006631 $cgi->a({-href => href(action=>"heads")}, "..."));
Kay Sievers0db37972005-08-07 20:24:35 +02006632 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006633
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006634 if (%remotedata) {
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006635 git_print_header_div('remotes');
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006636 git_remotes_body(\%remotedata, 15, $head);
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006637 }
6638
Petr Baudise30496d2006-10-24 05:33:17 +02006639 if (@forklist) {
6640 git_print_header_div('forks');
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02006641 git_project_list_body(\@forklist, 'age', 0, 15,
Robert Fitzsimonsaaca9672006-12-22 19:38:12 +00006642 $#forklist <= 15 ? undef :
Petr Baudise30496d2006-10-24 05:33:17 +02006643 $cgi->a({-href => href(action=>"forks")}, "..."),
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02006644 'no_header');
Petr Baudise30496d2006-10-24 05:33:17 +02006645 }
6646
Kay Sieversede5e102005-08-07 20:23:12 +02006647 git_footer_html();
6648}
6649
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006650sub git_tag {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006651 my %tag = parse_tag($hash);
Jakub Narebski198a2a82007-05-12 21:16:34 +02006652
6653 if (! %tag) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006654 die_error(404, "Unknown tag object");
Jakub Narebski198a2a82007-05-12 21:16:34 +02006655 }
6656
Anders Kaseorgd8a94802010-08-27 13:38:16 -04006657 my $head = git_get_head_hash($project);
6658 git_header_html();
6659 git_print_page_nav('','', $head,undef,$head);
Jakub Narebski847e01f2006-08-14 02:05:47 +02006660 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006661 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01006662 "<table class=\"object_header\">\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02006663 "<tr>\n" .
6664 "<td>object</td>\n" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02006665 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6666 $tag{'object'}) . "</td>\n" .
6667 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6668 $tag{'type'}) . "</td>\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02006669 "</tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006670 if (defined($tag{'author'})) {
Giuseppe Bilottaba924732009-06-30 00:00:50 +02006671 git_print_authorship_rows(\%tag, 'author');
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006672 }
6673 print "</table>\n\n" .
6674 "</div>\n";
6675 print "<div class=\"page_body\">";
6676 my $comment = $tag{'comment'};
6677 foreach my $line (@$comment) {
Junio C Hamano70022432006-11-24 14:04:01 -08006678 chomp $line;
Jakub Narebski793c4002006-11-24 22:25:50 +01006679 print esc_html($line, -nbsp=>1) . "<br/>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006680 }
6681 print "</div>\n";
6682 git_footer_html();
6683}
6684
Jakub Narebski4af819d2009-09-01 13:39:17 +02006685sub git_blame_common {
6686 my $format = shift || 'porcelain';
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01006687 if ($format eq 'porcelain' && $input_params{'javascript'}) {
Jakub Narebskic4ccf612009-09-01 13:39:19 +02006688 $format = 'incremental';
6689 $action = 'blame_incremental'; # for page title etc
6690 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006691
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006692 # permissions
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006693 gitweb_check_feature('blame')
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006694 or die_error(403, "Blame view not allowed");
Lea Wiemann074afaa2008-06-19 22:03:21 +02006695
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006696 # error checking
Lea Wiemann074afaa2008-06-19 22:03:21 +02006697 die_error(400, "No file name given") unless $file_name;
Jakub Narebski847e01f2006-08-14 02:05:47 +02006698 $hash_base ||= git_get_head_hash($project);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006699 die_error(404, "Couldn't find base commit") unless $hash_base;
Jakub Narebski847e01f2006-08-14 02:05:47 +02006700 my %co = parse_commit($hash_base)
Lea Wiemann074afaa2008-06-19 22:03:21 +02006701 or die_error(404, "Commit not found");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006702 my $ftype = "blob";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006703 if (!defined $hash) {
6704 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02006705 or die_error(404, "Error looking up file");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006706 } else {
6707 $ftype = git_get_type($hash);
6708 if ($ftype !~ "blob") {
6709 die_error(400, "Object is not a blob");
6710 }
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006711 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006712
Jakub Narebski4af819d2009-09-01 13:39:17 +02006713 my $fd;
6714 if ($format eq 'incremental') {
6715 # get file contents (as base)
6716 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
6717 or die_error(500, "Open git-cat-file failed");
6718 } elsif ($format eq 'data') {
6719 # run git-blame --incremental
6720 open $fd, "-|", git_cmd(), "blame", "--incremental",
6721 $hash_base, "--", $file_name
6722 or die_error(500, "Open git-blame --incremental failed");
6723 } else {
6724 # run git-blame --porcelain
6725 open $fd, "-|", git_cmd(), "blame", '-p',
6726 $hash_base, '--', $file_name
6727 or die_error(500, "Open git-blame --porcelain failed");
6728 }
Ævar Arnfjörð Bjarmasonfd870042013-08-30 08:37:01 +00006729 binmode $fd, ':utf8';
Jakub Narebski4af819d2009-09-01 13:39:17 +02006730
6731 # incremental blame data returns early
6732 if ($format eq 'data') {
6733 print $cgi->header(
6734 -type=>"text/plain", -charset => "utf-8",
6735 -status=> "200 OK");
6736 local $| = 1; # output autoflush
Jürgen Kreileder57cf4ad2011-12-17 10:22:23 +01006737 while (my $line = <$fd>) {
6738 print to_utf8($line);
6739 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006740 close $fd
6741 or print "ERROR $!\n";
6742
6743 print 'END';
6744 if (defined $t0 && gitweb_check_feature('timed')) {
6745 print ' '.
Jakub Narebski3962f1d72010-11-09 19:27:54 +01006746 tv_interval($t0, [ gettimeofday() ]).
Jakub Narebski4af819d2009-09-01 13:39:17 +02006747 ' '.$number_of_git_cmds;
6748 }
6749 print "\n";
6750
6751 return;
6752 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006753
6754 # page header
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006755 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02006756 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01006757 $cgi->a({-href => href(action=>"blob", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02006758 "blob") .
Jakub Narebski87e573f2009-12-01 17:54:26 +01006759 " | ";
6760 if ($format eq 'incremental') {
6761 $formats_nav .=
6762 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
6763 "blame") . " (non-incremental)";
6764 } else {
6765 $formats_nav .=
6766 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
6767 "blame") . " (incremental)";
6768 }
6769 $formats_nav .=
Jakub Narebski952c65f2006-08-22 16:52:50 +02006770 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01006771 $cgi->a({-href => href(action=>"history", -replay=>1)},
6772 "history") .
Petr Baudiscae18622006-09-22 03:19:41 +02006773 " | " .
Jakub Narebski4af819d2009-09-01 13:39:17 +02006774 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02006775 "HEAD");
Jakub Narebski847e01f2006-08-14 02:05:47 +02006776 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6777 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07006778 git_print_page_path($file_name, $ftype, $hash_base);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006779
6780 # page body
Jakub Narebski4af819d2009-09-01 13:39:17 +02006781 if ($format eq 'incremental') {
6782 print "<noscript>\n<div class=\"error\"><center><b>\n".
6783 "This page requires JavaScript to run.\n Use ".
Jakub Narebskic4ccf612009-09-01 13:39:19 +02006784 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
Jakub Narebski4af819d2009-09-01 13:39:17 +02006785 'this page').
6786 " instead.\n".
6787 "</b></center></div>\n</noscript>\n";
6788
6789 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
6790 }
6791
6792 print qq!<div class="page_body">\n!;
6793 print qq!<div id="progress_info">... / ...</div>\n!
6794 if ($format eq 'incremental');
6795 print qq!<table id="blame_table" class="blame" width="100%">\n!.
6796 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6797 qq!<thead>\n!.
6798 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
6799 qq!</thead>\n!.
6800 qq!<tbody>\n!;
6801
Jakub Narebskiaef37682009-07-25 00:44:06 +02006802 my @rev_color = qw(light dark);
Luben Tuikovcc1bf972006-07-23 13:37:53 -07006803 my $num_colors = scalar(@rev_color);
6804 my $current_color = 0;
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006805
Jakub Narebski4af819d2009-09-01 13:39:17 +02006806 if ($format eq 'incremental') {
6807 my $color_class = $rev_color[$current_color];
6808
6809 #contents of a file
6810 my $linenr = 0;
6811 LINE:
6812 while (my $line = <$fd>) {
6813 chomp $line;
6814 $linenr++;
6815
6816 print qq!<tr id="l$linenr" class="$color_class">!.
6817 qq!<td class="sha1"><a href=""> </a></td>!.
6818 qq!<td class="linenr">!.
6819 qq!<a class="linenr" href="">$linenr</a></td>!;
6820 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
6821 print qq!</tr>\n!;
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07006822 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006823
6824 } else { # porcelain, i.e. ordinary blame
6825 my %metainfo = (); # saves information about commits
6826
6827 # blame data
6828 LINE:
6829 while (my $line = <$fd>) {
6830 chomp $line;
6831 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6832 # no <lines in group> for subsequent lines in group of lines
6833 my ($full_rev, $orig_lineno, $lineno, $group_size) =
6834 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6835 if (!exists $metainfo{$full_rev}) {
6836 $metainfo{$full_rev} = { 'nprevious' => 0 };
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07006837 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006838 my $meta = $metainfo{$full_rev};
6839 my $data;
6840 while ($data = <$fd>) {
6841 chomp $data;
6842 last if ($data =~ s/^\t//); # contents of line
6843 if ($data =~ /^(\S+)(?: (.*))?$/) {
6844 $meta->{$1} = $2 unless exists $meta->{$1};
6845 }
6846 if ($data =~ /^previous /) {
6847 $meta->{'nprevious'}++;
Jakub Narebskia36817b2009-07-25 00:44:05 +02006848 }
6849 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006850 my $short_rev = substr($full_rev, 0, 8);
6851 my $author = $meta->{'author'};
6852 my %date =
6853 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
6854 my $date = $date{'iso-tz'};
6855 if ($group_size) {
6856 $current_color = ($current_color + 1) % $num_colors;
6857 }
6858 my $tr_class = $rev_color[$current_color];
6859 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6860 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6861 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6862 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6863 if ($group_size) {
6864 print "<td class=\"sha1\"";
6865 print " title=\"". esc_html($author) . ", $date\"";
6866 print " rowspan=\"$group_size\"" if ($group_size > 1);
6867 print ">";
6868 print $cgi->a({-href => href(action=>"commit",
6869 hash=>$full_rev,
6870 file_name=>$file_name)},
6871 esc_html($short_rev));
6872 if ($group_size >= 2) {
6873 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6874 if (@author_initials) {
6875 print "<br />" .
6876 esc_html(join('', @author_initials));
6877 # or join('.', ...)
6878 }
6879 }
6880 print "</td>\n";
6881 }
6882 # 'previous' <sha1 of parent commit> <filename at commit>
6883 if (exists $meta->{'previous'} &&
6884 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6885 $meta->{'parent'} = $1;
6886 $meta->{'file_parent'} = unquote($2);
6887 }
6888 my $linenr_commit =
6889 exists($meta->{'parent'}) ?
6890 $meta->{'parent'} : $full_rev;
6891 my $linenr_filename =
6892 exists($meta->{'file_parent'}) ?
6893 $meta->{'file_parent'} : unquote($meta->{'filename'});
6894 my $blamed = href(action => 'blame',
6895 file_name => $linenr_filename,
6896 hash_base => $linenr_commit);
6897 print "<td class=\"linenr\">";
6898 print $cgi->a({ -href => "$blamed#l$orig_lineno",
6899 -class => "linenr" },
6900 esc_html($lineno));
6901 print "</td>";
6902 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
6903 print "</tr>\n";
6904 } # end while
6905
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006906 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006907
6908 # footer
6909 print "</tbody>\n".
6910 "</table>\n"; # class="blame"
6911 print "</div>\n"; # class="blame_body"
Jakub Narebski952c65f2006-08-22 16:52:50 +02006912 close $fd
6913 or print "Reading blob failed\n";
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006914
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006915 git_footer_html();
6916}
6917
Jakub Narebski4af819d2009-09-01 13:39:17 +02006918sub git_blame {
6919 git_blame_common();
6920}
6921
6922sub git_blame_incremental {
6923 git_blame_common('incremental');
6924}
6925
6926sub git_blame_data {
6927 git_blame_common('data');
6928}
6929
Kay Sieversede5e102005-08-07 20:23:12 +02006930sub git_tags {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006931 my $head = git_get_head_hash($project);
Kay Sieversede5e102005-08-07 20:23:12 +02006932 git_header_html();
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01006933 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
Jakub Narebski847e01f2006-08-14 02:05:47 +02006934 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02006935
Jakub Narebskicd146402006-11-02 20:23:11 +01006936 my @tagslist = git_get_tags_list();
6937 if (@tagslist) {
6938 git_tags_body(\@tagslist);
Kay Sieversede5e102005-08-07 20:23:12 +02006939 }
Kay Sieversede5e102005-08-07 20:23:12 +02006940 git_footer_html();
6941}
6942
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02006943sub git_heads {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006944 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02006945 git_header_html();
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01006946 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
Jakub Narebski847e01f2006-08-14 02:05:47 +02006947 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02006948
Jakub Narebskicd146402006-11-02 20:23:11 +01006949 my @headslist = git_get_heads_list();
6950 if (@headslist) {
6951 git_heads_body(\@headslist, $head);
Kay Sievers0db37972005-08-07 20:24:35 +02006952 }
Kay Sievers0db37972005-08-07 20:24:35 +02006953 git_footer_html();
6954}
6955
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006956# used both for single remote view and for list of all the remotes
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006957sub git_remotes {
6958 gitweb_check_feature('remote_heads')
6959 or die_error(403, "Remote heads view is disabled");
6960
6961 my $head = git_get_head_hash($project);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006962 my $remote = $input_params{'hash'};
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006963
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006964 my $remotedata = git_get_remotes_list($remote);
6965 die_error(500, "Unable to get remote information") unless defined $remotedata;
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006966
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006967 unless (%$remotedata) {
6968 die_error(404, defined $remote ?
6969 "Remote $remote not found" :
6970 "No remotes found");
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006971 }
6972
6973 git_header_html(undef, undef, -action_extra => $remote);
6974 git_print_page_nav('', '', $head, undef, $head,
6975 format_ref_views($remote ? '' : 'remotes'));
6976
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006977 fill_remote_heads($remotedata);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006978 if (defined $remote) {
6979 git_print_header_div('remotes', "$remote remote for $project");
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006980 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006981 } else {
6982 git_print_header_div('summary', "$project remotes");
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006983 git_remotes_body($remotedata, undef, $head);
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006984 }
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006985
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006986 git_footer_html();
6987}
6988
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006989sub git_blob_plain {
Jakub Narebski7f718e82008-06-03 16:47:10 +02006990 my $type = shift;
Jakub Narebskif2e73302006-08-26 19:14:25 +02006991 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02006992
Luben Tuikovcff07712006-07-23 13:28:55 -07006993 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006994 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006995 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006996 $hash = git_get_hash_by_path($base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02006997 or die_error(404, "Cannot find file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006998 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006999 die_error(400, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007000 }
Martin Waitz800764c2006-09-16 23:09:02 +02007001 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7002 # blobs defined by non-textual hash id's can be cached
7003 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007004 }
Martin Waitz800764c2006-09-16 23:09:02 +02007005
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007006 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02007007 or die_error(500, "Open git-cat-file blob '$hash' failed");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07007008
Jakub Narebski7f718e82008-06-03 16:47:10 +02007009 # content-type (can include charset)
7010 $type = blob_contenttype($fd, $file_name, $type);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07007011
Jakub Narebski7f718e82008-06-03 16:47:10 +02007012 # "save as" filename, even when no $file_name is given
Luben Tuikov930cf7d2006-07-09 20:18:57 -07007013 my $save_as = "$hash";
7014 if (defined $file_name) {
7015 $save_as = $file_name;
7016 } elsif ($type =~ m/^text\//) {
7017 $save_as .= '.txt';
7018 }
7019
Matt McCutchen7e1100e2009-02-07 19:00:09 -05007020 # With XSS prevention on, blobs of all types except a few known safe
7021 # ones are served with "Content-Disposition: attachment" to make sure
7022 # they don't run in our security domain. For certain image types,
7023 # blob view writes an <img> tag referring to blob_plain view, and we
7024 # want to be sure not to break that by serving the image as an
7025 # attachment (though Firefox 3 doesn't seem to care).
7026 my $sandbox = $prevent_xss &&
Jakub Narebski86afbd02011-06-30 11:39:20 +02007027 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
7028
7029 # serve text/* as text/plain
7030 if ($prevent_xss &&
Jakub Narebskie8c35312011-06-30 11:39:21 +02007031 ($type =~ m!^text/[a-z]+\b(.*)$! ||
7032 ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
Jakub Narebski86afbd02011-06-30 11:39:20 +02007033 my $rest = $1;
7034 $rest = defined $rest ? $rest : '';
7035 $type = "text/plain$rest";
7036 }
Matt McCutchen7e1100e2009-02-07 19:00:09 -05007037
Jakub Narebskif2e73302006-08-26 19:14:25 +02007038 print $cgi->header(
Jakub Narebski7f718e82008-06-03 16:47:10 +02007039 -type => $type,
7040 -expires => $expires,
Matt McCutchen7e1100e2009-02-07 19:00:09 -05007041 -content_disposition =>
7042 ($sandbox ? 'attachment' : 'inline')
7043 . '; filename="' . $save_as . '"');
Jakub Narebski34122b52009-05-11 03:29:40 +02007044 local $/ = undef;
Luben Tuikov930cf7d2006-07-09 20:18:57 -07007045 binmode STDOUT, ':raw';
7046 print <$fd>;
7047 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
Luben Tuikov930cf7d2006-07-09 20:18:57 -07007048 close $fd;
7049}
7050
Kay Sievers09bd7892005-08-07 20:21:23 +02007051sub git_blob {
Jakub Narebskif2e73302006-08-26 19:14:25 +02007052 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02007053
Luben Tuikovcff07712006-07-23 13:28:55 -07007054 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007055 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007056 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007057 $hash = git_get_hash_by_path($base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02007058 or die_error(404, "Cannot find file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007059 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007060 die_error(400, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007061 }
Martin Waitz800764c2006-09-16 23:09:02 +02007062 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7063 # blobs defined by non-textual hash id's can be cached
7064 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02007065 }
Martin Waitz800764c2006-09-16 23:09:02 +02007066
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08007067 my $have_blame = gitweb_check_feature('blame');
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007068 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02007069 or die_error(500, "Couldn't cat $file_name, $hash");
Jakub Narebski847e01f2006-08-14 02:05:47 +02007070 my $mimetype = blob_mimetype($fd, $file_name);
Johannes Schindelinb331fe52010-04-27 21:34:44 +02007071 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007072 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
Luben Tuikov930cf7d2006-07-09 20:18:57 -07007073 close $fd;
7074 return git_blob_plain($mimetype);
7075 }
Jakub Narebski5a4cf332006-12-04 23:47:22 +01007076 # we can have blame only for text/* mimetype
7077 $have_blame &&= ($mimetype =~ m!^text/!);
7078
Jakub Narebski592ea412010-04-27 21:34:45 +02007079 my $highlight = gitweb_check_feature('highlight');
Ian Kellingc151aa32016-09-24 15:32:57 -07007080 my $syntax = guess_file_syntax($highlight, $file_name);
Ian Kelling779a2062016-09-24 15:32:58 -07007081 $fd = run_highlighter($fd, $highlight, $syntax);
Johannes Schindelinb331fe52010-04-27 21:34:44 +02007082
Jakub Narebskif2e73302006-08-26 19:14:25 +02007083 git_header_html(undef, $expires);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007084 my $formats_nav = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02007085 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Kay Sievers93129442005-10-17 03:27:54 +02007086 if (defined $file_name) {
Florian Forster5996ca02006-06-12 10:31:57 +02007087 if ($have_blame) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02007088 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01007089 $cgi->a({-href => href(action=>"blame", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02007090 "blame") .
7091 " | ";
Florian Forster5996ca02006-06-12 10:31:57 +02007092 }
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007093 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01007094 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02007095 "history") .
7096 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01007097 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02007098 "raw") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007099 " | " .
7100 $cgi->a({-href => href(action=>"blob",
7101 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02007102 "HEAD");
Kay Sievers93129442005-10-17 03:27:54 +02007103 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02007104 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01007105 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
7106 "raw");
Kay Sievers93129442005-10-17 03:27:54 +02007107 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007108 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7109 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02007110 } else {
7111 print "<div class=\"page_nav\">\n" .
7112 "<br/><br/></div>\n" .
Jakub Narebski3017ed62010-12-15 00:34:01 +01007113 "<div class=\"title\">".esc_html($hash)."</div>\n";
Kay Sievers09bd7892005-08-07 20:21:23 +02007114 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07007115 git_print_page_path($file_name, "blob", $hash_base);
Kay Sieversc07ad4b2005-08-07 20:22:44 +02007116 print "<div class=\"page_body\">\n";
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007117 if ($mimetype =~ m!^image/!) {
Andrew Keller46a74712014-02-17 09:25:13 -05007118 print qq!<img class="blob" type="!.esc_attr($mimetype).qq!"!;
Jakub Narebski5a4cf332006-12-04 23:47:22 +01007119 if ($file_name) {
Jakub Narebski3017ed62010-12-15 00:34:01 +01007120 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
Jakub Narebski5a4cf332006-12-04 23:47:22 +01007121 }
7122 print qq! src="! .
7123 href(action=>"blob_plain", hash=>$hash,
7124 hash_base=>$hash_base, file_name=>$file_name) .
7125 qq!" />\n!;
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007126 } else {
7127 my $nr;
7128 while (my $line = <$fd>) {
7129 chomp $line;
7130 $nr++;
7131 $line = untabify($line);
Jakub Narebski592ea412010-04-27 21:34:45 +02007132 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 +02007133 $nr, esc_attr(href(-replay => 1)), $nr, $nr,
Ian Kelling779a2062016-09-24 15:32:58 -07007134 $highlight ? sanitize($line) : esc_html($line, -nbsp=>1);
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007135 }
Kay Sievers161332a2005-08-07 19:49:46 +02007136 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02007137 close $fd
7138 or print "Reading blob failed.\n";
Kay Sieversfbb592a2005-08-07 20:12:11 +02007139 print "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02007140 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007141}
7142
7143sub git_tree {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07007144 if (!defined $hash_base) {
7145 $hash_base = "HEAD";
7146 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02007147 if (!defined $hash) {
Kay Sievers09bd7892005-08-07 20:21:23 +02007148 if (defined $file_name) {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07007149 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
7150 } else {
7151 $hash = $hash_base;
Kay Sievers10dba282005-08-07 20:25:27 +02007152 }
Kay Sieverse925f382005-08-07 20:23:35 +02007153 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02007154 die_error(404, "No such tree") unless defined($hash);
Jakub Narebski34122b52009-05-11 03:29:40 +02007155
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007156 my $show_sizes = gitweb_check_feature('show-sizes');
7157 my $have_blame = gitweb_check_feature('blame');
7158
Jakub Narebski34122b52009-05-11 03:29:40 +02007159 my @entries = ();
7160 {
7161 local $/ = "\0";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007162 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
7163 ($show_sizes ? '-l' : ()), @extra_options, $hash
Jakub Narebski34122b52009-05-11 03:29:40 +02007164 or die_error(500, "Open git-ls-tree failed");
7165 @entries = map { chomp; $_ } <$fd>;
7166 close $fd
7167 or die_error(404, "Reading tree failed");
7168 }
Kay Sieversd63577d2005-08-07 20:18:13 +02007169
Jakub Narebski847e01f2006-08-14 02:05:47 +02007170 my $refs = git_get_references();
7171 my $ref = format_ref_marker($refs, $hash_base);
Kay Sievers12a88f22005-08-07 20:02:47 +02007172 git_header_html();
Jakub Narebski300454f2006-10-21 17:53:09 +02007173 my $basedir = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02007174 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Petr Baudiscae18622006-09-22 03:19:41 +02007175 my @views_nav = ();
7176 if (defined $file_name) {
7177 push @views_nav,
Jakub Narebskia3823e52007-11-01 13:06:29 +01007178 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02007179 "history"),
7180 $cgi->a({-href => href(action=>"tree",
7181 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02007182 "HEAD"),
Petr Baudiscae18622006-09-22 03:19:41 +02007183 }
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007184 my $snapshot_links = format_snapshot_links($hash);
7185 if (defined $snapshot_links) {
Petr Baudiscae18622006-09-22 03:19:41 +02007186 # FIXME: Should be available when we have no hash base as well.
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007187 push @views_nav, $snapshot_links;
Petr Baudiscae18622006-09-22 03:19:41 +02007188 }
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007189 git_print_page_nav('tree','', $hash_base, undef, undef,
7190 join(' | ', @views_nav));
Jakub Narebski847e01f2006-08-14 02:05:47 +02007191 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
Kay Sieversd63577d2005-08-07 20:18:13 +02007192 } else {
Jakub Narebskifa702002006-08-31 00:35:07 +02007193 undef $hash_base;
Kay Sieversd63577d2005-08-07 20:18:13 +02007194 print "<div class=\"page_nav\">\n";
7195 print "<br/><br/></div>\n";
Jakub Narebski3017ed62010-12-15 00:34:01 +01007196 print "<div class=\"title\">".esc_html($hash)."</div>\n";
Kay Sieversd63577d2005-08-07 20:18:13 +02007197 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007198 if (defined $file_name) {
Jakub Narebski300454f2006-10-21 17:53:09 +02007199 $basedir = $file_name;
7200 if ($basedir ne '' && substr($basedir, -1) ne '/') {
7201 $basedir .= '/';
7202 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02007203 git_print_page_path($file_name, 'tree', $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02007204 }
Kay Sieversfbb592a2005-08-07 20:12:11 +02007205 print "<div class=\"page_body\">\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01007206 print "<table class=\"tree\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07007207 my $alternate = 1;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007208 # '..' (top directory) link if possible
7209 if (defined $hash_base &&
7210 defined $file_name && $file_name =~ m![^/]+$!) {
7211 if ($alternate) {
7212 print "<tr class=\"dark\">\n";
7213 } else {
7214 print "<tr class=\"light\">\n";
7215 }
7216 $alternate ^= 1;
7217
7218 my $up = $file_name;
7219 $up =~ s!/?[^/]+$!!;
7220 undef $up unless $up;
7221 # based on git_print_tree_entry
7222 print '<td class="mode">' . mode_str('040000') . "</td>\n";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007223 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007224 print '<td class="list">';
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007225 print $cgi->a({-href => href(action=>"tree",
7226 hash_base=>$hash_base,
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007227 file_name=>$up)},
7228 "..");
7229 print "</td>\n";
7230 print "<td class=\"link\"></td>\n";
7231
7232 print "</tr>\n";
7233 }
Kay Sievers161332a2005-08-07 19:49:46 +02007234 foreach my $line (@entries) {
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007235 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
Jakub Narebskicb849b42006-08-31 00:32:15 +02007236
Kay Sieversbddec012005-08-07 20:25:42 +02007237 if ($alternate) {
Kay Sieversc994d622005-08-07 20:27:18 +02007238 print "<tr class=\"dark\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007239 } else {
Kay Sieversc994d622005-08-07 20:27:18 +02007240 print "<tr class=\"light\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007241 }
7242 $alternate ^= 1;
Jakub Narebskicb849b42006-08-31 00:32:15 +02007243
Jakub Narebski300454f2006-10-21 17:53:09 +02007244 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
Jakub Narebskifa702002006-08-31 00:35:07 +02007245
Kay Sievers42f7eb92005-08-07 20:21:46 +02007246 print "</tr>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02007247 }
Kay Sievers42f7eb92005-08-07 20:21:46 +02007248 print "</table>\n" .
7249 "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02007250 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007251}
7252
Krzesimir Nowake3747472013-12-11 12:54:44 +01007253sub sanitize_for_filename {
7254 my $name = shift;
7255
7256 $name =~ s!/!-!g;
7257 $name =~ s/[^[:alnum:]_.-]//g;
7258
7259 return $name;
7260}
7261
Mark Radab6292752009-11-07 16:13:29 +01007262sub snapshot_name {
7263 my ($project, $hash) = @_;
7264
7265 # path/to/project.git -> project
7266 # path/to/project/.git -> project
7267 my $name = to_utf8($project);
7268 $name =~ s,([^/])/*\.git$,$1,;
Krzesimir Nowake3747472013-12-11 12:54:44 +01007269 $name = sanitize_for_filename(basename($name));
Mark Radab6292752009-11-07 16:13:29 +01007270
7271 my $ver = $hash;
7272 if ($hash =~ /^[0-9a-fA-F]+$/) {
7273 # shorten SHA-1 hash
7274 my $full_hash = git_get_full_hash($project, $hash);
7275 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
7276 $ver = git_get_short_hash($project, $hash);
7277 }
7278 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
7279 # tags don't need shortened SHA-1 hash
7280 $ver = $1;
7281 } else {
7282 # branches and other need shortened SHA-1 hash
Krzesimir Nowak8d646a92013-12-11 12:54:43 +01007283 my $strip_refs = join '|', map { quotemeta } get_branch_refs();
7284 if ($hash =~ m!^refs/($strip_refs|remotes)/(.*)$!) {
Krzesimir Nowake3747472013-12-11 12:54:44 +01007285 my $ref_dir = (defined $1) ? $1 : '';
7286 $ver = $2;
7287
7288 $ref_dir = sanitize_for_filename($ref_dir);
7289 # for refs neither in heads nor remotes we want to
7290 # add a ref dir to archive name
7291 if ($ref_dir ne '' and $ref_dir ne 'heads' and $ref_dir ne 'remotes') {
7292 $ver = $ref_dir . '-' . $ver;
7293 }
Mark Radab6292752009-11-07 16:13:29 +01007294 }
7295 $ver .= '-' . git_get_short_hash($project, $hash);
7296 }
Krzesimir Nowake3747472013-12-11 12:54:44 +01007297 # special case of sanitization for filename - we change
7298 # slashes to dots instead of dashes
Mark Radab6292752009-11-07 16:13:29 +01007299 # in case of hierarchical branch names
7300 $ver =~ s!/!.!g;
Krzesimir Nowake3747472013-12-11 12:54:44 +01007301 $ver =~ s/[^[:alnum:]_.-]//g;
Mark Radab6292752009-11-07 16:13:29 +01007302
7303 # name = project-version_string
7304 $name = "$name-$ver";
7305
7306 return wantarray ? ($name, $name) : $name;
7307}
7308
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04007309sub exit_if_unmodified_since {
7310 my ($latest_epoch) = @_;
7311 our $cgi;
7312
7313 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7314 if (defined $if_modified) {
7315 my $since;
7316 if (eval { require HTTP::Date; 1; }) {
7317 $since = HTTP::Date::str2time($if_modified);
7318 } elsif (eval { require Time::ParseDate; 1; }) {
7319 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7320 }
7321 if (defined $since && $latest_epoch <= $since) {
7322 my %latest_date = parse_date($latest_epoch);
7323 print $cgi->header(
7324 -last_modified => $latest_date{'rfc2822'},
7325 -status => '304 Not Modified');
7326 goto DONE_GITWEB;
7327 }
7328 }
7329}
7330
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307331sub git_snapshot {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02007332 my $format = $input_params{'snapshot_format'};
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01007333 if (!@snapshot_fmts) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007334 die_error(403, "Snapshots not allowed");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007335 }
7336 # default to first supported snapshot format
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01007337 $format ||= $snapshot_fmts[0];
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007338 if ($format !~ m/^[a-z0-9]+$/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007339 die_error(400, "Invalid snapshot format parameter");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007340 } elsif (!exists($known_snapshot_formats{$format})) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007341 die_error(400, "Unknown snapshot format");
Mark A Rada1bfd3632009-08-06 10:25:39 -04007342 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
7343 die_error(403, "Snapshot format not allowed");
Mark Rada34b31a82009-08-25 00:59:48 -04007344 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
7345 die_error(403, "Unsupported snapshot format");
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05307346 }
7347
Mark Radafdb0c362009-09-26 13:46:08 -04007348 my $type = git_get_type("$hash^{}");
7349 if (!$type) {
7350 die_error(404, 'Object does not exist');
7351 } elsif ($type eq 'blob') {
7352 die_error(400, 'Object is not a tree-ish');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307353 }
7354
Mark Radab6292752009-11-07 16:13:29 +01007355 my ($name, $prefix) = snapshot_name($project, $hash);
7356 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
W. Trevor King8745db62012-03-29 08:45:49 -04007357
7358 my %co = parse_commit($hash);
7359 exit_if_unmodified_since($co{'committer_epoch'}) if %co;
7360
Mark Radab6292752009-11-07 16:13:29 +01007361 my $cmd = quote_command(
Lea Wiemann516381d2008-06-17 23:46:35 +02007362 git_cmd(), 'archive',
7363 "--format=$known_snapshot_formats{$format}{'format'}",
Mark Radab6292752009-11-07 16:13:29 +01007364 "--prefix=$prefix/", $hash);
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007365 if (exists $known_snapshot_formats{$format}{'compressor'}) {
Lea Wiemann516381d2008-06-17 23:46:35 +02007366 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
Mark Levedahl072570e2007-05-20 11:46:46 -04007367 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307368
Mark Radab6292752009-11-07 16:13:29 +01007369 $filename =~ s/(["\\])/\\$1/g;
W. Trevor King8745db62012-03-29 08:45:49 -04007370 my %latest_date;
7371 if (%co) {
7372 %latest_date = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
7373 }
7374
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02007375 print $cgi->header(
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007376 -type => $known_snapshot_formats{$format}{'type'},
Mark Radab6292752009-11-07 16:13:29 +01007377 -content_disposition => 'inline; filename="' . $filename . '"',
W. Trevor King8745db62012-03-29 08:45:49 -04007378 %co ? (-last_modified => $latest_date{'rfc2822'}) : (),
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02007379 -status => '200 OK');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307380
Mark Levedahl072570e2007-05-20 11:46:46 -04007381 open my $fd, "-|", $cmd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007382 or die_error(500, "Execute git-archive failed");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307383 binmode STDOUT, ':raw';
7384 print <$fd>;
7385 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
7386 close $fd;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307387}
7388
Jakub Narebski15f0b112009-11-13 02:02:13 +01007389sub git_log_generic {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007390 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
Jakub Narebski15f0b112009-11-13 02:02:13 +01007391
Jakub Narebski847e01f2006-08-14 02:05:47 +02007392 my $head = git_get_head_hash($project);
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007393 if (!defined $base) {
7394 $base = $head;
Kay Sievers0db37972005-08-07 20:24:35 +02007395 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02007396 if (!defined $page) {
7397 $page = 0;
Kay Sieversb87d78d2005-08-07 20:21:04 +02007398 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007399 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02007400
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007401 my $commit_hash = $base;
7402 if (defined $parent) {
7403 $commit_hash = "$parent..$base";
Jakub Narebski15f0b112009-11-13 02:02:13 +01007404 }
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007405 my @commitlist =
7406 parse_commits($commit_hash, 101, (100 * $page),
7407 defined $file_name ? ($file_name, "--full-history") : ());
Kay Sieversea4a6df2005-08-07 20:26:49 +02007408
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007409 my $ftype;
7410 if (!defined $file_hash && defined $file_name) {
7411 # some commits could have deleted file in question,
7412 # and not have it in tree, but one of them has to have it
7413 for (my $i = 0; $i < @commitlist; $i++) {
7414 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
7415 last if defined $file_hash;
7416 }
7417 }
7418 if (defined $file_hash) {
7419 $ftype = git_get_type($file_hash);
7420 }
7421 if (defined $file_name && !defined $ftype) {
7422 die_error(500, "Unknown type of object");
7423 }
7424 my %co;
7425 if (defined $file_name) {
7426 %co = parse_commit($base)
7427 or die_error(404, "Unknown commit object");
7428 }
7429
7430
7431 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
Jakub Narebski15f0b112009-11-13 02:02:13 +01007432 my $next_link = '';
Jakub Narebski42671ca2009-11-13 02:02:12 +01007433 if ($#commitlist >= 100) {
7434 $next_link =
7435 $cgi->a({-href => href(-replay=>1, page=>$page+1),
7436 -accesskey => "n", -title => "Alt-n"}, "next");
7437 }
Jakub Narebski15f0b112009-11-13 02:02:13 +01007438 my $patch_max = gitweb_get_feature('patches');
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007439 if ($patch_max && !defined $file_name) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007440 if ($patch_max < 0 || @commitlist <= $patch_max) {
7441 $paging_nav .= " &sdot; " .
7442 $cgi->a({-href => href(action=>"patches", -replay=>1)},
7443 "patches");
7444 }
7445 }
7446
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007447 git_header_html();
Jakub Narebski15f0b112009-11-13 02:02:13 +01007448 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007449 if (defined $file_name) {
7450 git_print_header_div('commit', esc_html($co{'title'}), $base);
7451 } else {
7452 git_print_header_div('summary', $project)
7453 }
7454 git_print_page_path($file_name, $ftype, $hash_base)
7455 if (defined $file_name);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007456
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007457 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
7458 $file_name, $file_hash, $ftype);
Jakub Narebski42671ca2009-11-13 02:02:12 +01007459
Kay Sievers034df392005-08-07 20:20:07 +02007460 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007461}
7462
Jakub Narebski15f0b112009-11-13 02:02:13 +01007463sub git_log {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007464 git_log_generic('log', \&git_log_body,
7465 $hash, $hash_parent);
Jakub Narebski15f0b112009-11-13 02:02:13 +01007466}
7467
Kay Sievers09bd7892005-08-07 20:21:23 +02007468sub git_commit {
Jakub Narebski9954f772006-11-18 23:35:41 +01007469 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02007470 my %co = parse_commit($hash)
7471 or die_error(404, "Unknown commit object");
Kay Sievers161332a2005-08-07 19:49:46 +02007472
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007473 my $parent = $co{'parent'};
7474 my $parents = $co{'parents'}; # listref
7475
7476 # we need to prepare $formats_nav before any parameter munging
7477 my $formats_nav;
7478 if (!defined $parent) {
7479 # --root commitdiff
7480 $formats_nav .= '(initial)';
7481 } elsif (@$parents == 1) {
7482 # single parent commit
7483 $formats_nav .=
7484 '(parent: ' .
7485 $cgi->a({-href => href(action=>"commit",
7486 hash=>$parent)},
7487 esc_html(substr($parent, 0, 7))) .
7488 ')';
7489 } else {
7490 # merge commit
7491 $formats_nav .=
7492 '(merge: ' .
7493 join(' ', map {
Jakub Narebskif9308a12007-03-23 21:04:31 +01007494 $cgi->a({-href => href(action=>"commit",
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007495 hash=>$_)},
7496 esc_html(substr($_, 0, 7)));
7497 } @$parents ) .
7498 ')';
7499 }
Jakub Narebski1655c982009-10-09 14:26:44 +02007500 if (gitweb_check_feature('patches') && @$parents <= 1) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007501 $formats_nav .= " | " .
7502 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7503 "patch");
7504 }
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007505
Kay Sieversd8a20ba2005-08-07 20:28:53 +02007506 if (!defined $parent) {
Jakub Narebskib9182982006-07-30 18:28:34 -07007507 $parent = "--root";
Kay Sievers6191f8e2005-08-07 20:19:56 +02007508 }
Jakub Narebski549ab4a2006-12-15 17:53:45 +01007509 my @difftree;
Jakub Narebski208ecb22007-05-07 01:10:08 +02007510 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
7511 @diff_opts,
7512 (@$parents <= 1 ? $parent : '-c'),
7513 $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007514 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski208ecb22007-05-07 01:10:08 +02007515 @difftree = map { chomp; $_ } <$fd>;
Lea Wiemann074afaa2008-06-19 22:03:21 +02007516 close $fd or die_error(404, "Reading git-diff-tree failed");
Kay Sievers11044292005-10-19 03:18:45 +02007517
7518 # non-textual hash id's can be cached
7519 my $expires;
7520 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7521 $expires = "+1d";
7522 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007523 my $refs = git_get_references();
7524 my $ref = format_ref_marker($refs, $co{'id'});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05307525
Jakub Narebski594e2122006-07-31 02:21:52 +02007526 git_header_html(undef, $expires);
Petr Baudisa1441542006-10-06 18:59:33 +02007527 git_print_page_nav('commit', '',
Jakub Narebski952c65f2006-08-22 16:52:50 +02007528 $hash, $co{'tree'}, $hash,
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007529 $formats_nav);
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07007530
Kay Sieversb87d78d2005-08-07 20:21:04 +02007531 if (defined $co{'parent'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007532 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02007533 } else {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007534 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02007535 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02007536 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01007537 "<table class=\"object_header\">\n";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02007538 git_print_authorship_rows(\%co);
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00007539 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007540 print "<tr>" .
7541 "<td>tree</td>" .
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00007542 "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007543 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
7544 class => "list"}, $co{'tree'}) .
Kay Sievers19806692005-08-07 20:26:27 +02007545 "</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007546 "<td class=\"link\">" .
7547 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
7548 "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007549 my $snapshot_links = format_snapshot_links($hash);
7550 if (defined $snapshot_links) {
7551 print " | " . $snapshot_links;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307552 }
7553 print "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02007554 "</tr>\n";
Jakub Narebski549ab4a2006-12-15 17:53:45 +01007555
Kay Sievers3e029292005-08-07 20:05:15 +02007556 foreach my $par (@$parents) {
Kay Sieversbddec012005-08-07 20:25:42 +02007557 print "<tr>" .
7558 "<td>parent</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007559 "<td class=\"sha1\">" .
7560 $cgi->a({-href => href(action=>"commit", hash=>$par),
7561 class => "list"}, $par) .
7562 "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02007563 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02007564 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007565 " | " .
Jakub Narebskif2e60942006-09-03 23:43:03 +02007566 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
Kay Sieversbddec012005-08-07 20:25:42 +02007567 "</td>" .
7568 "</tr>\n";
Kay Sievers3e029292005-08-07 20:05:15 +02007569 }
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02007570 print "</table>".
Kay Sieversb87d78d2005-08-07 20:21:04 +02007571 "</div>\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02007572
Kay Sieversfbb592a2005-08-07 20:12:11 +02007573 print "<div class=\"page_body\">\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02007574 git_print_log($co{'comment'});
Kay Sievers927dcec2005-08-07 20:18:44 +02007575 print "</div>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02007576
Jakub Narebski208ecb22007-05-07 01:10:08 +02007577 git_difftree_body(\@difftree, $hash, @$parents);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02007578
Kay Sievers12a88f22005-08-07 20:02:47 +02007579 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007580}
7581
Jakub Narebskica946012006-12-10 13:25:47 +01007582sub git_object {
7583 # object is defined by:
7584 # - hash or hash_base alone
7585 # - hash_base and file_name
7586 my $type;
7587
7588 # - hash or hash_base alone
7589 if ($hash || ($hash_base && !defined $file_name)) {
7590 my $object_id = $hash || $hash_base;
7591
Lea Wiemann516381d2008-06-17 23:46:35 +02007592 open my $fd, "-|", quote_command(
7593 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
Lea Wiemann074afaa2008-06-19 22:03:21 +02007594 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007595 $type = <$fd>;
Øyvind A. Holma9eb90a2016-01-12 04:31:56 +01007596 defined $type && chomp $type;
Jakub Narebskica946012006-12-10 13:25:47 +01007597 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007598 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007599
7600 # - hash_base and file_name
7601 } elsif ($hash_base && defined $file_name) {
7602 $file_name =~ s,/+$,,;
7603
7604 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
Lea Wiemann074afaa2008-06-19 22:03:21 +02007605 or die_error(404, "Base object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007606
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02007607 # here errors should not happen
Jakub Narebskica946012006-12-10 13:25:47 +01007608 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007609 or die_error(500, "Open git-ls-tree failed");
Jakub Narebskica946012006-12-10 13:25:47 +01007610 my $line = <$fd>;
7611 close $fd;
7612
7613 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
7614 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007615 die_error(404, "File or directory for given base does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007616 }
7617 $type = $2;
7618 $hash = $3;
7619 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007620 die_error(400, "Not enough information to find object");
Jakub Narebskica946012006-12-10 13:25:47 +01007621 }
7622
7623 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
7624 hash=>$hash, hash_base=>$hash_base,
7625 file_name=>$file_name),
7626 -status => '302 Found');
7627}
7628
Kay Sievers09bd7892005-08-07 20:21:23 +02007629sub git_blobdiff {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007630 my $format = shift || 'html';
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007631 my $diff_style = $input_params{'diff_style'} || 'inline';
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007632
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007633 my $fd;
7634 my @difftree;
7635 my %diffinfo;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007636 my $expires;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007637
7638 # preparing $fd and %diffinfo for git_patchset_body
7639 # new style URI
7640 if (defined $hash_base && defined $hash_parent_base) {
7641 if (defined $file_name) {
7642 # read raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007643 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7644 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02007645 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007646 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007647 @difftree = map { chomp; $_ } <$fd>;
7648 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007649 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007650 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02007651 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007652
Jakub Narebski0aea3372006-08-27 23:45:26 +02007653 } elsif (defined $hash &&
7654 $hash =~ /[0-9a-fA-F]{40}/) {
7655 # try to find filename from $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007656
7657 # read filtered raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007658 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7659 $hash_parent_base, $hash_base, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007660 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007661 @difftree =
7662 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
7663 # $hash == to_id
7664 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
7665 map { chomp; $_ } <$fd>;
7666 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007667 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007668 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02007669 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007670
7671 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007672 die_error(400, "Missing one of the blob diff parameters");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007673 }
7674
7675 if (@difftree > 1) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007676 die_error(400, "Ambiguous blob diff specification");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007677 }
7678
7679 %diffinfo = parse_difftree_raw_line($difftree[0]);
Jakub Narebski9d301452007-11-01 12:38:08 +01007680 $file_parent ||= $diffinfo{'from_file'} || $file_name;
7681 $file_name ||= $diffinfo{'to_file'};
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007682
7683 $hash_parent ||= $diffinfo{'from_id'};
7684 $hash ||= $diffinfo{'to_id'};
7685
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007686 # non-textual hash id's can be cached
7687 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
7688 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
7689 $expires = '+1d';
7690 }
7691
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007692 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007693 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski957d6ea2007-04-05 13:45:41 +02007694 '-p', ($format eq 'html' ? "--full-index" : ()),
7695 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02007696 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007697 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007698 }
7699
Junio C Hamanob54dc9f2008-12-16 19:42:02 -08007700 # old/legacy style URI -- not generated anymore since 1.4.3.
7701 if (!%diffinfo) {
7702 die_error('404 Not Found', "Missing one of the blob diff parameters")
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007703 }
7704
7705 # header
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007706 if ($format eq 'html') {
7707 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01007708 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02007709 "raw");
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007710 $formats_nav .= diff_style_nav($diff_style);
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007711 git_header_html(undef, $expires);
7712 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7713 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7714 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7715 } else {
7716 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
Jakub Narebski3017ed62010-12-15 00:34:01 +01007717 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007718 }
7719 if (defined $file_name) {
7720 git_print_page_path($file_name, "blob", $hash_base);
7721 } else {
7722 print "<div class=\"page_path\"></div>\n";
7723 }
7724
7725 } elsif ($format eq 'plain') {
7726 print $cgi->header(
7727 -type => 'text/plain',
7728 -charset => 'utf-8',
7729 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07007730 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007731
7732 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7733
Kay Sievers09bd7892005-08-07 20:21:23 +02007734 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007735 die_error(400, "Unknown blobdiff format");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007736 }
7737
7738 # patch
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007739 if ($format eq 'html') {
7740 print "<div class=\"page_body\">\n";
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007741
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007742 git_patchset_body($fd, $diff_style,
7743 [ \%diffinfo ], $hash_base, $hash_parent_base);
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007744 close $fd;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007745
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007746 print "</div>\n"; # class="page_body"
7747 git_footer_html();
7748
7749 } else {
7750 while (my $line = <$fd>) {
Jakub Narebski403d0902006-11-08 11:48:56 +01007751 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
7752 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007753
7754 print $line;
7755
7756 last if $line =~ m!^\+\+\+!;
7757 }
7758 local $/ = undef;
7759 print <$fd>;
7760 close $fd;
7761 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007762}
7763
Kay Sievers19806692005-08-07 20:26:27 +02007764sub git_blobdiff_plain {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007765 git_blobdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02007766}
7767
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007768# assumes that it is added as later part of already existing navigation,
7769# so it returns "| foo | bar" rather than just "foo | bar"
7770sub diff_style_nav {
7771 my ($diff_style, $is_combined) = @_;
7772 $diff_style ||= 'inline';
7773
7774 return "" if ($is_combined);
7775
7776 my @styles = (inline => 'inline', 'sidebyside' => 'side by side');
7777 my %styles = @styles;
7778 @styles =
7779 @styles[ map { $_ * 2 } 0..$#styles/2 ];
7780
7781 return join '',
7782 map { " | ".$_ }
7783 map {
7784 $_ eq $diff_style ? $styles{$_} :
7785 $cgi->a({-href => href(-replay=>1, diff_style => $_)}, $styles{$_})
7786 } @styles;
7787}
7788
Kay Sievers09bd7892005-08-07 20:21:23 +02007789sub git_commitdiff {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01007790 my %params = @_;
7791 my $format = $params{-format} || 'html';
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007792 my $diff_style = $input_params{'diff_style'} || 'inline';
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007793
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007794 my ($patch_max) = gitweb_get_feature('patches');
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007795 if ($format eq 'patch') {
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007796 die_error(403, "Patch view not allowed") unless $patch_max;
7797 }
7798
Jakub Narebski9954f772006-11-18 23:35:41 +01007799 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02007800 my %co = parse_commit($hash)
7801 or die_error(404, "Unknown commit object");
Jakub Narebski151602d2006-10-23 00:37:56 +02007802
Jakub Narebskicd030c32007-06-08 13:33:28 +02007803 # choose format for commitdiff for merge
7804 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7805 $hash_parent = '--cc';
7806 }
7807 # we need to prepare $formats_nav before almost any parameter munging
Jakub Narebski151602d2006-10-23 00:37:56 +02007808 my $formats_nav;
7809 if ($format eq 'html') {
7810 $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01007811 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007812 "raw");
Jakub Narebski1655c982009-10-09 14:26:44 +02007813 if ($patch_max && @{$co{'parents'}} <= 1) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007814 $formats_nav .= " | " .
7815 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7816 "patch");
7817 }
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007818 $formats_nav .= diff_style_nav($diff_style, @{$co{'parents'}} > 1);
Jakub Narebski151602d2006-10-23 00:37:56 +02007819
Jakub Narebskicd030c32007-06-08 13:33:28 +02007820 if (defined $hash_parent &&
7821 $hash_parent ne '-c' && $hash_parent ne '--cc') {
Jakub Narebski151602d2006-10-23 00:37:56 +02007822 # commitdiff with two commits given
7823 my $hash_parent_short = $hash_parent;
7824 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7825 $hash_parent_short = substr($hash_parent, 0, 7);
7826 }
7827 $formats_nav .=
Jakub Narebskiada3e1f2007-06-08 13:26:31 +02007828 ' (from';
7829 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7830 if ($co{'parents'}[$i] eq $hash_parent) {
7831 $formats_nav .= ' parent ' . ($i+1);
7832 last;
7833 }
7834 }
7835 $formats_nav .= ': ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007836 $cgi->a({-href => href(-replay=>1,
7837 hash=>$hash_parent, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007838 esc_html($hash_parent_short)) .
7839 ')';
7840 } elsif (!$co{'parent'}) {
7841 # --root commitdiff
7842 $formats_nav .= ' (initial)';
7843 } elsif (scalar @{$co{'parents'}} == 1) {
7844 # single parent commit
7845 $formats_nav .=
7846 ' (parent: ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007847 $cgi->a({-href => href(-replay=>1,
7848 hash=>$co{'parent'}, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007849 esc_html(substr($co{'parent'}, 0, 7))) .
7850 ')';
7851 } else {
7852 # merge commit
Jakub Narebskicd030c32007-06-08 13:33:28 +02007853 if ($hash_parent eq '--cc') {
7854 $formats_nav .= ' | ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007855 $cgi->a({-href => href(-replay=>1,
Jakub Narebskicd030c32007-06-08 13:33:28 +02007856 hash=>$hash, hash_parent=>'-c')},
7857 'combined');
7858 } else { # $hash_parent eq '-c'
7859 $formats_nav .= ' | ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007860 $cgi->a({-href => href(-replay=>1,
Jakub Narebskicd030c32007-06-08 13:33:28 +02007861 hash=>$hash, hash_parent=>'--cc')},
7862 'compact');
7863 }
Jakub Narebski151602d2006-10-23 00:37:56 +02007864 $formats_nav .=
7865 ' (merge: ' .
7866 join(' ', map {
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007867 $cgi->a({-href => href(-replay=>1,
7868 hash=>$_, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007869 esc_html(substr($_, 0, 7)));
7870 } @{$co{'parents'}} ) .
7871 ')';
7872 }
7873 }
7874
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007875 my $hash_parent_param = $hash_parent;
Jakub Narebskicd030c32007-06-08 13:33:28 +02007876 if (!defined $hash_parent_param) {
7877 # --cc for multiple parents, --root for parentless
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007878 $hash_parent_param =
Jakub Narebskicd030c32007-06-08 13:33:28 +02007879 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
Kay Sieversbddec012005-08-07 20:25:42 +02007880 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007881
7882 # read commitdiff
7883 my $fd;
7884 my @difftree;
Jakub Narebskieee08902006-08-24 00:15:14 +02007885 if ($format eq 'html') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007886 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007887 "--no-commit-id", "--patch-with-raw", "--full-index",
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007888 $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007889 or die_error(500, "Open git-diff-tree failed");
Jakub Narebskieee08902006-08-24 00:15:14 +02007890
Jakub Narebski04408c32006-11-18 23:35:38 +01007891 while (my $line = <$fd>) {
7892 chomp $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02007893 # empty line ends raw part of diff-tree output
7894 last unless $line;
Jakub Narebski493e01d2007-05-07 01:10:06 +02007895 push @difftree, scalar parse_difftree_raw_line($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02007896 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007897
Jakub Narebskieee08902006-08-24 00:15:14 +02007898 } elsif ($format eq 'plain') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007899 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007900 '-p', $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007901 or die_error(500, "Open git-diff-tree failed");
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007902 } elsif ($format eq 'patch') {
7903 # For commit ranges, we limit the output to the number of
7904 # patches specified in the 'patches' feature.
7905 # For single commits, we limit the output to a single patch,
7906 # diverging from the git-format-patch default.
7907 my @commit_spec = ();
7908 if ($hash_parent) {
7909 if ($patch_max > 0) {
7910 push @commit_spec, "-$patch_max";
7911 }
7912 push @commit_spec, '-n', "$hash_parent..$hash";
7913 } else {
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +01007914 if ($params{-single}) {
7915 push @commit_spec, '-1';
7916 } else {
7917 if ($patch_max > 0) {
7918 push @commit_spec, "-$patch_max";
7919 }
7920 push @commit_spec, "-n";
7921 }
7922 push @commit_spec, '--root', $hash;
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007923 }
Pavan Kumar Sunkara04794fd2010-05-10 18:41:35 +02007924 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
7925 '--encoding=utf8', '--stdout', @commit_spec
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007926 or die_error(500, "Open git-format-patch failed");
Jakub Narebskieee08902006-08-24 00:15:14 +02007927 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007928 die_error(400, "Unknown commitdiff format");
Jakub Narebskieee08902006-08-24 00:15:14 +02007929 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02007930
Kay Sievers11044292005-10-19 03:18:45 +02007931 # non-textual hash id's can be cached
7932 my $expires;
7933 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7934 $expires = "+1d";
7935 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007936
Jakub Narebskieee08902006-08-24 00:15:14 +02007937 # write commit message
7938 if ($format eq 'html') {
7939 my $refs = git_get_references();
7940 my $ref = format_ref_marker($refs, $co{'id'});
Kay Sievers19806692005-08-07 20:26:27 +02007941
Jakub Narebskieee08902006-08-24 00:15:14 +02007942 git_header_html(undef, $expires);
7943 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
7944 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
Giuseppe Bilottaf88bafa2009-06-30 00:00:49 +02007945 print "<div class=\"title_text\">\n" .
7946 "<table class=\"object_header\">\n";
7947 git_print_authorship_rows(\%co);
7948 print "</table>".
7949 "</div>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02007950 print "<div class=\"page_body\">\n";
Jakub Narebski82560982006-10-24 13:55:33 +02007951 if (@{$co{'comment'}} > 1) {
7952 print "<div class=\"log\">\n";
7953 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
7954 print "</div>\n"; # class="log"
7955 }
Kay Sievers1b1cd422005-08-07 20:28:01 +02007956
Jakub Narebskieee08902006-08-24 00:15:14 +02007957 } elsif ($format eq 'plain') {
7958 my $refs = git_get_references("tags");
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007959 my $tagname = git_get_rev_name_tags($hash);
Jakub Narebskieee08902006-08-24 00:15:14 +02007960 my $filename = basename($project) . "-$hash.patch";
7961
7962 print $cgi->header(
7963 -type => 'text/plain',
7964 -charset => 'utf-8',
7965 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07007966 -content_disposition => 'inline; filename="' . "$filename" . '"');
Jakub Narebskieee08902006-08-24 00:15:14 +02007967 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
Yasushi SHOJI77202242008-01-29 21:16:02 +09007968 print "From: " . to_utf8($co{'author'}) . "\n";
7969 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7970 print "Subject: " . to_utf8($co{'title'}) . "\n";
7971
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007972 print "X-Git-Tag: $tagname\n" if $tagname;
Jakub Narebskieee08902006-08-24 00:15:14 +02007973 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007974
Jakub Narebskieee08902006-08-24 00:15:14 +02007975 foreach my $line (@{$co{'comment'}}) {
Yasushi SHOJI77202242008-01-29 21:16:02 +09007976 print to_utf8($line) . "\n";
Kay Sievers19806692005-08-07 20:26:27 +02007977 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007978 print "---\n\n";
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007979 } elsif ($format eq 'patch') {
7980 my $filename = basename($project) . "-$hash.patch";
7981
7982 print $cgi->header(
7983 -type => 'text/plain',
7984 -charset => 'utf-8',
7985 -expires => $expires,
7986 -content_disposition => 'inline; filename="' . "$filename" . '"');
Kay Sievers19806692005-08-07 20:26:27 +02007987 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007988
7989 # write patch
7990 if ($format eq 'html') {
Jakub Narebskicd030c32007-06-08 13:33:28 +02007991 my $use_parents = !defined $hash_parent ||
7992 $hash_parent eq '-c' || $hash_parent eq '--cc';
7993 git_difftree_body(\@difftree, $hash,
7994 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebskib4657e72006-08-28 14:48:14 +02007995 print "<br/>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02007996
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007997 git_patchset_body($fd, $diff_style,
7998 \@difftree, $hash,
Jakub Narebskicd030c32007-06-08 13:33:28 +02007999 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebski157e43b2006-08-24 19:34:36 +02008000 close $fd;
Jakub Narebskieee08902006-08-24 00:15:14 +02008001 print "</div>\n"; # class="page_body"
8002 git_footer_html();
8003
8004 } elsif ($format eq 'plain') {
8005 local $/ = undef;
8006 print <$fd>;
8007 close $fd
8008 or print "Reading git-diff-tree failed\n";
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01008009 } elsif ($format eq 'patch') {
8010 local $/ = undef;
8011 print <$fd>;
8012 close $fd
8013 or print "Reading git-format-patch failed\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02008014 }
8015}
8016
8017sub git_commitdiff_plain {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01008018 git_commitdiff(-format => 'plain');
Kay Sievers19806692005-08-07 20:26:27 +02008019}
8020
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01008021# format-patch-style patches
8022sub git_patch {
Jakub Narebski1655c982009-10-09 14:26:44 +02008023 git_commitdiff(-format => 'patch', -single => 1);
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +01008024}
8025
8026sub git_patches {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01008027 git_commitdiff(-format => 'patch');
Kay Sievers09bd7892005-08-07 20:21:23 +02008028}
8029
8030sub git_history {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01008031 git_log_generic('history', \&git_history_body,
8032 $hash_base, $hash_parent_base,
8033 $file_name, $hash);
Kay Sievers161332a2005-08-07 19:49:46 +02008034}
Kay Sievers19806692005-08-07 20:26:27 +02008035
8036sub git_search {
Jakub Narebskie0ca3642011-06-22 17:28:52 +02008037 $searchtype ||= 'commit';
8038
8039 # check if appropriate features are enabled
8040 gitweb_check_feature('search')
8041 or die_error(403, "Search is disabled");
8042 if ($searchtype eq 'pickaxe') {
8043 # pickaxe may take all resources of your box and run for several minutes
8044 # with every query - so decide by yourself how public you make this feature
8045 gitweb_check_feature('pickaxe')
8046 or die_error(403, "Pickaxe search is disabled");
8047 }
8048 if ($searchtype eq 'grep') {
8049 # grep search might be potentially CPU-intensive, too
8050 gitweb_check_feature('grep')
8051 or die_error(403, "Grep search is disabled");
8052 }
8053
Kay Sievers19806692005-08-07 20:26:27 +02008054 if (!defined $searchtext) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02008055 die_error(400, "Text field is empty");
Kay Sievers19806692005-08-07 20:26:27 +02008056 }
8057 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02008058 $hash = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02008059 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02008060 my %co = parse_commit($hash);
Kay Sievers19806692005-08-07 20:26:27 +02008061 if (!%co) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02008062 die_error(404, "Unknown commit object");
Kay Sievers19806692005-08-07 20:26:27 +02008063 }
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00008064 if (!defined $page) {
8065 $page = 0;
8066 }
Jakub Narebski04f7a942006-09-11 00:29:27 +02008067
Jakub Narebski16f20722011-06-22 17:28:53 +02008068 if ($searchtype eq 'commit' ||
8069 $searchtype eq 'author' ||
8070 $searchtype eq 'committer') {
8071 git_search_message(%co);
8072 } elsif ($searchtype eq 'pickaxe') {
8073 git_search_changes(%co);
8074 } elsif ($searchtype eq 'grep') {
8075 git_search_files(%co);
Jakub Narebski1ae05be2011-06-22 17:28:55 +02008076 } else {
8077 die_error(400, "Unknown search type");
Kay Sieversc994d622005-08-07 20:27:18 +02008078 }
Kay Sievers19806692005-08-07 20:26:27 +02008079}
8080
Petr Baudis88ad7292006-10-24 05:15:46 +02008081sub git_search_help {
8082 git_header_html();
8083 git_print_page_nav('','', $hash,$hash,$hash);
8084 print <<EOT;
Petr Baudis0e559912008-02-26 13:22:08 +01008085<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
8086regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
8087the pattern entered is recognized as the POSIX extended
Sven Strickroth5e687292017-05-13 11:54:51 +02008088<a href="https://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
Petr Baudis0e559912008-02-26 13:22:08 +01008089insensitive).</p>
Petr Baudis88ad7292006-10-24 05:15:46 +02008090<dl>
8091<dt><b>commit</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01008092<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02008093EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08008094 my $have_grep = gitweb_check_feature('grep');
Petr Baudise7738552007-05-17 04:31:12 +02008095 if ($have_grep) {
8096 print <<EOT;
8097<dt><b>grep</b></dt>
8098<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
Petr Baudis0e559912008-02-26 13:22:08 +01008099 a different one) are searched for the given pattern. On large trees, this search can take
8100a while and put some strain on the server, so please use it with some consideration. Note that
8101due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
8102case-sensitive.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02008103EOT
8104 }
8105 print <<EOT;
Petr Baudis88ad7292006-10-24 05:15:46 +02008106<dt><b>author</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01008107<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 +02008108<dt><b>committer</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01008109<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 +02008110EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08008111 my $have_pickaxe = gitweb_check_feature('pickaxe');
Petr Baudis88ad7292006-10-24 05:15:46 +02008112 if ($have_pickaxe) {
8113 print <<EOT;
8114<dt><b>pickaxe</b></dt>
8115<dd>All commits that caused the string to appear or disappear from any file (changes that
8116added, removed or "modified" the string) will be listed. This search can take a while and
Petr Baudis0e559912008-02-26 13:22:08 +01008117takes a lot of strain on the server, so please use it wisely. Note that since you may be
8118interested even in changes just changing the case as well, this search is case sensitive.</dd>
Petr Baudis88ad7292006-10-24 05:15:46 +02008119EOT
8120 }
8121 print "</dl>\n";
8122 git_footer_html();
8123}
8124
Kay Sievers19806692005-08-07 20:26:27 +02008125sub git_shortlog {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01008126 git_log_generic('shortlog', \&git_shortlog_body,
8127 $hash, $hash_parent);
Kay Sievers19806692005-08-07 20:26:27 +02008128}
Jakub Narebski717b8312006-07-31 21:22:15 +02008129
8130## ......................................................................
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008131## feeds (RSS, Atom; OPML)
Jakub Narebski717b8312006-07-31 21:22:15 +02008132
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008133sub git_feed {
8134 my $format = shift || 'atom';
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08008135 my $have_blame = gitweb_check_feature('blame');
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008136
8137 # Atom: http://www.atomenabled.org/developers/syndication/
8138 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
8139 if ($format ne 'rss' && $format ne 'atom') {
Lea Wiemann074afaa2008-06-19 22:03:21 +02008140 die_error(400, "Unknown web feed format");
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008141 }
8142
8143 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
8144 my $head = $hash || 'HEAD';
Jakub Narebski311e5522008-02-26 13:22:06 +01008145 my @commitlist = parse_commits($head, 150, 0, $file_name);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008146
8147 my %latest_commit;
8148 my %latest_date;
8149 my $content_type = "application/$format+xml";
8150 if (defined $cgi->http('HTTP_ACCEPT') &&
8151 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
8152 # browser (feed reader) prefers text/xml
8153 $content_type = 'text/xml';
8154 }
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008155 if (defined($commitlist[0])) {
8156 %latest_commit = %{$commitlist[0]};
Giuseppe Bilottacd956c72009-01-26 12:50:16 +01008157 my $latest_epoch = $latest_commit{'committer_epoch'};
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04008158 exit_if_unmodified_since($latest_epoch);
Dylan Alex Simondebf29d2012-10-11 16:40:35 -04008159 %latest_date = parse_date($latest_epoch, $latest_commit{'committer_tz'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008160 }
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04008161 print $cgi->header(
8162 -type => $content_type,
8163 -charset => 'utf-8',
8164 %latest_date ? (-last_modified => $latest_date{'rfc2822'}) : (),
8165 -status => '200 OK');
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008166
8167 # Optimization: skip generating the body if client asks only
8168 # for Last-Modified date.
8169 return if ($cgi->request_method() eq 'HEAD');
8170
8171 # header variables
8172 my $title = "$site_name - $project/$action";
8173 my $feed_type = 'log';
8174 if (defined $hash) {
8175 $title .= " - '$hash'";
8176 $feed_type = 'branch log';
8177 if (defined $file_name) {
8178 $title .= " :: $file_name";
8179 $feed_type = 'history';
8180 }
8181 } elsif (defined $file_name) {
8182 $title .= " - $file_name";
8183 $feed_type = 'history';
8184 }
8185 $title .= " $feed_type";
Jeff King0f0ecf62012-11-12 16:34:28 -05008186 $title = esc_html($title);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008187 my $descr = git_get_project_description($project);
8188 if (defined $descr) {
8189 $descr = esc_html($descr);
8190 } else {
8191 $descr = "$project " .
8192 ($format eq 'rss' ? 'RSS' : 'Atom') .
8193 " feed";
8194 }
8195 my $owner = git_get_project_owner($project);
8196 $owner = esc_html($owner);
8197
8198 #header
8199 my $alt_url;
8200 if (defined $file_name) {
8201 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
8202 } elsif (defined $hash) {
8203 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
8204 } else {
8205 $alt_url = href(-full=>1, action=>"summary");
8206 }
8207 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
8208 if ($format eq 'rss') {
8209 print <<XML;
Jakub Narebski59b9f612006-08-22 23:42:53 +02008210<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
8211<channel>
Jakub Narebski59b9f612006-08-22 23:42:53 +02008212XML
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008213 print "<title>$title</title>\n" .
8214 "<link>$alt_url</link>\n" .
8215 "<description>$descr</description>\n" .
Giuseppe Bilotta3ac109a2009-01-26 12:50:13 +01008216 "<language>en</language>\n" .
8217 # project owner is responsible for 'editorial' content
8218 "<managingEditor>$owner</managingEditor>\n";
Giuseppe Bilotta1ba68ce2009-01-26 12:50:11 +01008219 if (defined $logo || defined $favicon) {
8220 # prefer the logo to the favicon, since RSS
8221 # doesn't allow both
8222 my $img = esc_url($logo || $favicon);
8223 print "<image>\n" .
8224 "<url>$img</url>\n" .
8225 "<title>$title</title>\n" .
8226 "<link>$alt_url</link>\n" .
8227 "</image>\n";
8228 }
Giuseppe Bilotta0cf31282009-01-26 12:50:14 +01008229 if (%latest_date) {
8230 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
8231 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
8232 }
Giuseppe Bilottaad59a7a2009-01-26 12:50:12 +01008233 print "<generator>gitweb v.$version/$git_version</generator>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008234 } elsif ($format eq 'atom') {
8235 print <<XML;
8236<feed xmlns="http://www.w3.org/2005/Atom">
8237XML
8238 print "<title>$title</title>\n" .
8239 "<subtitle>$descr</subtitle>\n" .
8240 '<link rel="alternate" type="text/html" href="' .
8241 $alt_url . '" />' . "\n" .
8242 '<link rel="self" type="' . $content_type . '" href="' .
8243 $cgi->self_url() . '" />' . "\n" .
8244 "<id>" . href(-full=>1) . "</id>\n" .
8245 # use project owner for feed author
8246 "<author><name>$owner</name></author>\n";
8247 if (defined $favicon) {
8248 print "<icon>" . esc_url($favicon) . "</icon>\n";
8249 }
Jonathan Nieder9d9f5e72010-09-03 19:44:39 -05008250 if (defined $logo) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008251 # not twice as wide as tall: 72 x 27 pixels
Jakub Narebskie1147262006-12-04 14:09:43 +01008252 print "<logo>" . esc_url($logo) . "</logo>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008253 }
8254 if (! %latest_date) {
8255 # dummy date to keep the feed valid until commits trickle in:
8256 print "<updated>1970-01-01T00:00:00Z</updated>\n";
8257 } else {
8258 print "<updated>$latest_date{'iso-8601'}</updated>\n";
8259 }
Giuseppe Bilottaad59a7a2009-01-26 12:50:12 +01008260 print "<generator version='$version/$git_version'>gitweb</generator>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008261 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008262
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008263 # contents
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008264 for (my $i = 0; $i <= $#commitlist; $i++) {
8265 my %co = %{$commitlist[$i]};
8266 my $commit = $co{'id'};
Jakub Narebski717b8312006-07-31 21:22:15 +02008267 # we read 150, we always show 30 and the ones more recent than 48 hours
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01008268 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02008269 last;
8270 }
Jakub Narebski6368d9f2011-03-19 23:53:55 +01008271 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008272
8273 # get list of changed files
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008274 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskic906b182007-05-19 02:47:51 +02008275 $co{'parent'} || "--root",
8276 $co{'id'}, "--", (defined $file_name ? $file_name : ())
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02008277 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02008278 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02008279 close $fd
8280 or next;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008281
8282 # print element (entry, item)
Florian La Rochee62a6412008-02-03 12:38:46 +01008283 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008284 if ($format eq 'rss') {
8285 print "<item>\n" .
8286 "<title>" . esc_html($co{'title'}) . "</title>\n" .
8287 "<author>" . esc_html($co{'author'}) . "</author>\n" .
8288 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
8289 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
8290 "<link>$co_url</link>\n" .
8291 "<description>" . esc_html($co{'title'}) . "</description>\n" .
8292 "<content:encoded>" .
8293 "<![CDATA[\n";
8294 } elsif ($format eq 'atom') {
8295 print "<entry>\n" .
8296 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
8297 "<updated>$cd{'iso-8601'}</updated>\n" .
Jakub Narebskiab23c192006-11-25 15:54:33 +01008298 "<author>\n" .
8299 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
8300 if ($co{'author_email'}) {
8301 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
8302 }
8303 print "</author>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008304 # use committer for contributor
Jakub Narebskiab23c192006-11-25 15:54:33 +01008305 "<contributor>\n" .
8306 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
8307 if ($co{'committer_email'}) {
8308 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
8309 }
8310 print "</contributor>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008311 "<published>$cd{'iso-8601'}</published>\n" .
8312 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
8313 "<id>$co_url</id>\n" .
8314 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
8315 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
8316 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008317 my $comment = $co{'comment'};
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008318 print "<pre>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008319 foreach my $line (@$comment) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008320 $line = esc_html($line);
8321 print "$line\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008322 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008323 print "</pre><ul>\n";
8324 foreach my $difftree_line (@difftree) {
8325 my %difftree = parse_difftree_raw_line($difftree_line);
8326 next if !$difftree{'from_id'};
8327
8328 my $file = $difftree{'file'} || $difftree{'to_file'};
8329
8330 print "<li>" .
8331 "[" .
8332 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
8333 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
8334 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
8335 file_name=>$file, file_parent=>$difftree{'from_file'}),
8336 -title => "diff"}, 'D');
8337 if ($have_blame) {
8338 print $cgi->a({-href => href(-full=>1, action=>"blame",
8339 file_name=>$file, hash_base=>$commit),
8340 -title => "blame"}, 'B');
Jakub Narebski717b8312006-07-31 21:22:15 +02008341 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008342 # if this is not a feed of a file history
8343 if (!defined $file_name || $file_name ne $file) {
8344 print $cgi->a({-href => href(-full=>1, action=>"history",
8345 file_name=>$file, hash=>$commit),
8346 -title => "history"}, 'H');
8347 }
8348 $file = esc_path($file);
8349 print "] ".
8350 "$file</li>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008351 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008352 if ($format eq 'rss') {
8353 print "</ul>]]>\n" .
8354 "</content:encoded>\n" .
8355 "</item>\n";
8356 } elsif ($format eq 'atom') {
8357 print "</ul>\n</div>\n" .
8358 "</content>\n" .
8359 "</entry>\n";
8360 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008361 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008362
8363 # end of feed
8364 if ($format eq 'rss') {
8365 print "</channel>\n</rss>\n";
Jakub Narebski3278fbc2009-05-11 19:37:28 +02008366 } elsif ($format eq 'atom') {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008367 print "</feed>\n";
8368 }
8369}
8370
8371sub git_rss {
8372 git_feed('rss');
8373}
8374
8375sub git_atom {
8376 git_feed('atom');
Jakub Narebski717b8312006-07-31 21:22:15 +02008377}
8378
8379sub git_opml {
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008380 my @list = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski12b14432011-04-29 19:51:56 +02008381 if (!@list) {
8382 die_error(404, "No projects found");
8383 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008384
Giuseppe Bilottaae357852009-01-02 13:49:30 +01008385 print $cgi->header(
8386 -type => 'text/xml',
8387 -charset => 'utf-8',
8388 -content_disposition => 'inline; filename="opml.xml"');
8389
Jürgen Kreileder5d791052011-12-17 10:22:22 +01008390 my $title = esc_html($site_name);
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008391 my $filter = " within subdirectory ";
8392 if (defined $project_filter) {
8393 $filter .= esc_html($project_filter);
8394 } else {
8395 $filter = "";
8396 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02008397 print <<XML;
8398<?xml version="1.0" encoding="utf-8"?>
8399<opml version="1.0">
8400<head>
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008401 <title>$title OPML Export$filter</title>
Jakub Narebski59b9f612006-08-22 23:42:53 +02008402</head>
8403<body>
8404<outline text="git RSS feeds">
8405XML
Jakub Narebski717b8312006-07-31 21:22:15 +02008406
8407 foreach my $pr (@list) {
8408 my %proj = %$pr;
Jakub Narebski847e01f2006-08-14 02:05:47 +02008409 my $head = git_get_head_hash($proj{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02008410 if (!defined $head) {
8411 next;
8412 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02008413 $git_dir = "$projectroot/$proj{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02008414 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02008415 if (!%co) {
8416 next;
8417 }
8418
8419 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
Giuseppe Bilottadf63fbb2009-01-02 13:15:28 +01008420 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
8421 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
Jakub Narebski717b8312006-07-31 21:22:15 +02008422 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
8423 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02008424 print <<XML;
8425</outline>
8426</body>
8427</opml>
8428XML
Jakub Narebski717b8312006-07-31 21:22:15 +02008429}