blob: 7f8c1878d407e07c3ac4ac16d840557a18176d29 [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
Jakub Narebski3962f1d72010-11-09 19:27:54 +010023our $t0 = [ gettimeofday() ];
Jakub Narebskiaa7dd052009-09-01 13:39:16 +020024our $number_of_git_cmds = 0;
25
Jakub Narebskib1f5f642006-12-28 00:00:52 +010026BEGIN {
Jakub Narebski3be8e722007-04-01 22:22:21 +020027 CGI->compile() if $ENV{'MOD_PERL'};
Jakub Narebskib1f5f642006-12-28 00:00:52 +010028}
29
Junio C Hamano06c084d2006-08-02 13:50:20 -070030our $version = "++GIT_VERSION++";
Kay Sievers44ad2972005-08-07 19:59:24 +020031
Jakub Narebskic2394fe2010-05-07 14:54:04 +020032our ($my_url, $my_uri, $base_url, $path_info, $home_link);
33sub evaluate_uri {
34 our $cgi;
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +010035
Jakub Narebskic2394fe2010-05-07 14:54:04 +020036 our $my_url = $cgi->url();
37 our $my_uri = $cgi->url(-absolute => 1);
38
39 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
40 # needed and used only for URLs with nonempty PATH_INFO
41 our $base_url = $my_url;
42
43 # When the script is used as DirectoryIndex, the URL does not contain the name
44 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
45 # have to do it ourselves. We make $path_info global because it's also used
46 # later on.
47 #
48 # Another issue with the script being the DirectoryIndex is that the resulting
49 # $my_url data is not the full script URL: this is good, because we want
50 # generated links to keep implying the script name if it wasn't explicitly
51 # indicated in the URL we're handling, but it means that $my_url cannot be used
52 # as base URL.
53 # Therefore, if we needed to strip PATH_INFO, then we know that we have
54 # to build the base URL ourselves:
Jakub Narebski84d9e2d2012-02-03 13:44:54 +010055 our $path_info = decode_utf8($ENV{"PATH_INFO"});
Jakub Narebskic2394fe2010-05-07 14:54:04 +020056 if ($path_info) {
Jay Soffiancacfc092012-08-08 22:29:26 -040057 # $path_info has already been URL-decoded by the web server, but
58 # $my_url and $my_uri have not. URL-decode them so we can properly
59 # strip $path_info.
60 $my_url = unescape($my_url);
61 $my_uri = unescape($my_uri);
Jakub Narebskic2394fe2010-05-07 14:54:04 +020062 if ($my_url =~ s,\Q$path_info\E$,, &&
63 $my_uri =~ s,\Q$path_info\E$,, &&
64 defined $ENV{'SCRIPT_NAME'}) {
65 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
66 }
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +010067 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +020068
69 # target of the home link on top of all pages
70 our $home_link = $my_uri || "/";
Giuseppe Bilottab65910f2008-09-29 15:07:42 +020071}
72
Alp Tokere130dda2006-07-12 23:55:10 +010073# core git executable to use
74# this can just be "git" if your webserver has a sensible PATH
Junio C Hamano06c084d2006-08-02 13:50:20 -070075our $GIT = "++GIT_BINDIR++/git";
Jakub Narebski3f7f27102006-06-21 09:48:04 +020076
Kay Sieversb87d78d2005-08-07 20:21:04 +020077# absolute fs-path which will be prepended to the project path
Dennis Stosberg4a87b432006-06-21 15:07:08 +020078#our $projectroot = "/pub/scm";
Junio C Hamano06c084d2006-08-02 13:50:20 -070079our $projectroot = "++GITWEB_PROJECTROOT++";
Kay Sieversb87d78d2005-08-07 20:21:04 +020080
Luke Luca5e9492007-10-16 20:45:25 -070081# fs traversing limit for getting project list
82# the number is relative to the projectroot
83our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
84
Yasushi SHOJI2de21fa2006-08-15 07:50:49 +090085# string of the home link on top of all pages
86our $home_link_str = "++GITWEB_HOME_LINK_STR++";
87
Alp Toker49da1da2006-07-11 21:10:26 +010088# name of your site or organization to appear in page titles
89# replace this with something more descriptive for clearer bookmarks
Petr Baudis8be28902006-10-24 05:18:39 +020090our $site_name = "++GITWEB_SITENAME++"
91 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
Alp Toker49da1da2006-07-11 21:10:26 +010092
Lénaïc Huardc1355b72011-10-21 09:09:29 +020093# html snippet to include in the <head> section of each page
94our $site_html_head_string = "++GITWEB_SITE_HTML_HEAD_STRING++";
Alan Chandlerb2d34762006-10-03 13:49:03 +010095# filename of html text to include at top of each page
96our $site_header = "++GITWEB_SITE_HEADER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020097# html text to include at home page
Junio C Hamano06c084d2006-08-02 13:50:20 -070098our $home_text = "++GITWEB_HOMETEXT++";
Alan Chandlerb2d34762006-10-03 13:49:03 +010099# filename of html text to include at bottom of each page
100our $site_footer = "++GITWEB_SITE_FOOTER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +0200101
Alan Chandlerb2d34762006-10-03 13:49:03 +0100102# URI of stylesheets
103our @stylesheets = ("++GITWEB_CSS++");
Petr Baudis887a6122006-10-26 14:41:25 +0200104# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
105our $stylesheet = undef;
Jakub Narebski9a7a62f2006-10-06 12:31:05 +0200106# URI of GIT logo (72x27 size)
Junio C Hamano06c084d2006-08-02 13:50:20 -0700107our $logo = "++GITWEB_LOGO++";
Jakub Narebski0b5deba2006-09-04 20:32:13 +0200108# URI of GIT favicon, assumed to be image/png type
109our $favicon = "++GITWEB_FAVICON++";
Jakub Narebski4af819d2009-09-01 13:39:17 +0200110# URI of gitweb.js (JavaScript code for gitweb)
111our $javascript = "++GITWEB_JS++";
Jakub Narebskiaedd9422006-06-17 11:23:56 +0200112
Jakub Narebski9a7a62f2006-10-06 12:31:05 +0200113# URI and label (title) of GIT logo link
114#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
115#our $logo_label = "git documentation";
Wincent Colaiuta69fb8282009-07-12 14:31:28 +0200116our $logo_url = "http://git-scm.com/";
Jakub Narebski9a7a62f2006-10-06 12:31:05 +0200117our $logo_label = "git homepage";
Junio C Hamano51a7c662006-09-23 12:36:01 -0700118
Kay Sievers09bd7892005-08-07 20:21:23 +0200119# source of projects list
Junio C Hamano06c084d2006-08-02 13:50:20 -0700120our $projects_list = "++GITWEB_LIST++";
Kay Sieversb87d78d2005-08-07 20:21:04 +0200121
Michael Hendricks55feb122007-07-04 18:36:48 -0600122# the width (in characters) of the projects list "Description" column
123our $projects_list_description_width = 25;
124
Sebastien Ceveyd940c902011-04-29 19:52:01 +0200125# group projects by category on the projects list
126# (enabled if this variable evaluates to true)
127our $projects_list_group_categories = 0;
128
129# default category if none specified
130# (leave the empty string for no category)
131our $project_list_default_category = "";
132
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +0200133# default order of projects list
134# valid values are none, project, descr, owner, and age
135our $default_projects_order = "project";
136
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200137# show repository only if this file exists
138# (only effective if this variable evaluates to true)
139our $export_ok = "++GITWEB_EXPORT_OK++";
140
Kacper Kornet5710be42012-04-24 19:39:15 +0200141# don't generate age column on the projects list page
142our $omit_age_column = 0;
143
Kacper Kornet0ebe7822012-04-26 18:45:44 +0200144# don't generate information about owners of repositories
145our $omit_owner=0;
146
Alexander Gavrilovdd7f5f12008-11-06 01:36:23 +0300147# show repository only if this subroutine returns true
148# when given the path to the project, for example:
149# sub { return -e "$_[0]/git-daemon-export-ok"; }
150our $export_auth_hook = undef;
151
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200152# only allow viewing of repositories also shown on the overview page
153our $strict_export = "++GITWEB_STRICT_EXPORT++";
154
Jakub Narebski19a87212006-08-15 23:03:17 +0200155# list of git base URLs used for URL to where fetch project from,
156# i.e. full URL is "$git_base_url/$project"
Jakub Narebskid6b7e0b2006-10-26 12:26:44 +0200157our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
Jakub Narebski19a87212006-08-15 23:03:17 +0200158
Jakub Narebskif5aa79d2006-06-17 13:32:15 +0200159# default blob_plain mimetype and default charset for text/plain blob
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200160our $default_blob_plain_mimetype = 'text/plain';
161our $default_text_plain_charset = undef;
Jakub Narebskif5aa79d2006-06-17 13:32:15 +0200162
Petr Baudis2d007372006-06-18 00:01:06 +0200163# file to use for guessing MIME types before trying /etc/mime.types
164# (relative to the current git repository)
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200165our $mimetypes_file = undef;
Petr Baudis2d007372006-06-18 00:01:06 +0200166
Martin Koegler00f429a2007-06-03 17:42:44 +0200167# assume this charset if line contains non-UTF-8 characters;
168# it should be valid encoding (see Encoding::Supported(3pm) for list),
169# for which encoding all byte sequences are valid, for example
170# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
171# could be even 'utf-8' for the old behavior)
172our $fallback_encoding = 'latin1';
173
Jakub Narebski69a9b412007-07-20 02:15:09 +0200174# rename detection options for git-diff and git-diff-tree
175# - default is '-M', with the cost proportional to
176# (number of removed files) * (number of new files).
177# - more costly is '-C' (which implies '-M'), with the cost proportional to
178# (number of changed files + number of removed files) * (number of new files)
179# - even more costly is '-C', '--find-copies-harder' with cost
180# (number of files in the original tree) * (number of new files)
181# - one might want to include '-B' option, e.g. '-B', '-M'
182our @diff_opts = ('-M'); # taken from git_commit
183
Matt McCutchen7e1100e2009-02-07 19:00:09 -0500184# Disables features that would allow repository owners to inject script into
185# the gitweb domain.
186our $prevent_xss = 0;
187
Christopher Wilson7ce896b2010-09-21 00:25:19 -0700188# Path to the highlight executable to use (must be the one from
189# http://www.andre-simon.de due to assumptions about parameters and output).
190# Useful if highlight is not installed on your webserver's PATH.
191# [Default: highlight]
192our $highlight_bin = "++HIGHLIGHT_BIN++";
193
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200194# information about snapshot formats that gitweb is capable of serving
195our %known_snapshot_formats = (
196 # name => {
197 # 'display' => display name,
198 # 'type' => mime type,
199 # 'suffix' => filename suffix,
200 # 'format' => --format for git-archive,
201 # 'compressor' => [compressor command and arguments]
Mark A Rada1bfd3632009-08-06 10:25:39 -0400202 # (array reference, optional)
203 # 'disabled' => boolean (optional)}
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200204 #
205 'tgz' => {
206 'display' => 'tar.gz',
207 'type' => 'application/x-gzip',
208 'suffix' => '.tar.gz',
209 'format' => 'tar',
Fraser Tweedale0c8c3852011-04-26 11:32:00 +1000210 'compressor' => ['gzip', '-n']},
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200211
212 'tbz2' => {
213 'display' => 'tar.bz2',
214 'type' => 'application/x-bzip2',
215 'suffix' => '.tar.bz2',
216 'format' => 'tar',
217 'compressor' => ['bzip2']},
218
Mark A Radacbdefb52009-08-06 10:28:25 -0400219 'txz' => {
220 'display' => 'tar.xz',
221 'type' => 'application/x-xz',
222 'suffix' => '.tar.xz',
223 'format' => 'tar',
224 'compressor' => ['xz'],
225 'disabled' => 1},
226
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200227 'zip' => {
228 'display' => 'zip',
229 'type' => 'application/x-zip',
230 'suffix' => '.zip',
231 'format' => 'zip'},
232);
233
234# Aliases so we understand old gitweb.snapshot values in repository
235# configuration.
236our %known_snapshot_format_aliases = (
237 'gzip' => 'tgz',
238 'bzip2' => 'tbz2',
Mark A Radacbdefb52009-08-06 10:28:25 -0400239 'xz' => 'txz',
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200240
241 # backward compatibility: legacy gitweb config support
242 'x-gzip' => undef, 'gz' => undef,
243 'x-bzip2' => undef, 'bz2' => undef,
244 'x-zip' => undef, '' => undef,
245);
246
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200247# Pixel sizes for icons and avatars. If the default font sizes or lineheights
248# are changed, it may be appropriate to change these values too via
249# $GITWEB_CONFIG.
250our %avatar_size = (
251 'default' => 16,
252 'double' => 32
253);
254
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +0100255# Used to set the maximum load that we will still respond to gitweb queries.
256# If server load exceed this value then return "503 server busy" error.
257# If gitweb cannot determined server load, it is taken to be 0.
258# Leave it undefined (or set to 'undef') to turn off load checking.
259our $maxload = 300;
260
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400261# configuration for 'highlight' (http://www.andre-simon.de/)
262# match by basename
263our %highlight_basename = (
264 #'Program' => 'py',
265 #'Library' => 'py',
266 'SConstruct' => 'py', # SCons equivalent of Makefile
267 'Makefile' => 'make',
268);
269# match by extension
270our %highlight_ext = (
271 # main extensions, defining name of syntax;
272 # see files in /usr/share/highlight/langDefs/ directory
273 map { $_ => $_ }
Sylvain Rabot3ce19eb2010-12-30 22:20:28 +0100274 qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make),
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400275 # alternate extensions, see /etc/highlight/filetypes.conf
276 'h' => 'c',
Sylvain Rabot3ce19eb2010-12-30 22:20:28 +0100277 map { $_ => 'sh' } qw(bash zsh ksh),
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400278 map { $_ => 'cpp' } qw(cxx c++ cc),
Sylvain Rabot3ce19eb2010-12-30 22:20:28 +0100279 map { $_ => 'php' } qw(php3 php4 php5 phps),
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400280 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
Sylvain Rabot3ce19eb2010-12-30 22:20:28 +0100281 map { $_ => 'make'} qw(mak mk),
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400282 map { $_ => 'xml' } qw(xhtml html htm),
283);
284
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530285# You define site-wide feature defaults here; override them with
286# $GITWEB_CONFIG as necessary.
Jakub Narebski952c65f2006-08-22 16:52:50 +0200287our %feature = (
Jakub Narebski17848fc2006-08-26 19:14:22 +0200288 # feature => {
289 # 'sub' => feature-sub (subroutine),
290 # 'override' => allow-override (boolean),
291 # 'default' => [ default options...] (array reference)}
292 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200293 # if feature is overridable (it means that allow-override has true value),
Jakub Narebski17848fc2006-08-26 19:14:22 +0200294 # then feature-sub will be called with default options as parameters;
295 # return value of feature-sub indicates if to enable specified feature
296 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200297 # if there is no 'sub' key (no feature-sub), then feature cannot be
Ralf Wildenhues22e5e582010-08-22 13:12:12 +0200298 # overridden
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200299 #
Giuseppe Bilottaff3c0ff2008-12-02 14:57:28 -0800300 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
301 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
302 # is enabled
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530303
Petr Baudis45a3b122006-10-07 15:17:47 +0200304 # Enable the 'blame' blob view, showing the last commit that modified
305 # each line in the file. This can be very CPU-intensive.
306
307 # To enable system wide have in $GITWEB_CONFIG
308 # $feature{'blame'}{'default'} = [1];
309 # To have project specific config enable override in $GITWEB_CONFIG
310 # $feature{'blame'}{'override'} = 1;
311 # and in project config gitweb.blame = 0|1;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200312 'blame' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800313 'sub' => sub { feature_bool('blame', @_) },
Jakub Narebski952c65f2006-08-22 16:52:50 +0200314 'override' => 0,
315 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530316
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200317 # Enable the 'snapshot' link, providing a compressed archive of any
Petr Baudis45a3b122006-10-07 15:17:47 +0200318 # tree. This can potentially generate high traffic if you have large
319 # project.
320
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200321 # Value is a list of formats defined in %known_snapshot_formats that
322 # you wish to offer.
Petr Baudis45a3b122006-10-07 15:17:47 +0200323 # To disable system wide have in $GITWEB_CONFIG
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200324 # $feature{'snapshot'}{'default'} = [];
Petr Baudis45a3b122006-10-07 15:17:47 +0200325 # To have project specific config enable override in $GITWEB_CONFIG
Uwe Zeisbergerbbee1d92006-12-08 12:44:31 +0100326 # $feature{'snapshot'}{'override'} = 1;
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200327 # and in project config, a comma-separated list of formats or "none"
328 # to disable. Example: gitweb.snapshot = tbz2,zip;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200329 'snapshot' => {
330 'sub' => \&feature_snapshot,
331 'override' => 0,
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200332 'default' => ['tgz']},
Jakub Narebski04f7a942006-09-11 00:29:27 +0200333
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000334 # Enable text search, which will list the commits which match author,
335 # committer or commit text to a given string. Enabled by default.
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200336 # Project specific override is not supported.
Jakub Narebskie0ca3642011-06-22 17:28:52 +0200337 #
338 # Note that this controls all search features, which means that if
339 # it is disabled, then 'grep' and 'pickaxe' search would also be
340 # disabled.
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000341 'search' => {
342 'override' => 0,
343 'default' => [1]},
344
Petr Baudise7738552007-05-17 04:31:12 +0200345 # Enable grep search, which will list the files in currently selected
346 # tree containing the given string. Enabled by default. This can be
347 # potentially CPU-intensive, of course.
Jakub Narebskia598ded2011-06-21 08:41:16 +0200348 # Note that you need to have 'search' feature enabled too.
Petr Baudise7738552007-05-17 04:31:12 +0200349
350 # To enable system wide have in $GITWEB_CONFIG
351 # $feature{'grep'}{'default'} = [1];
352 # To have project specific config enable override in $GITWEB_CONFIG
353 # $feature{'grep'}{'override'} = 1;
354 # and in project config gitweb.grep = 0|1;
355 'grep' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800356 'sub' => sub { feature_bool('grep', @_) },
Petr Baudise7738552007-05-17 04:31:12 +0200357 'override' => 0,
358 'default' => [1]},
359
Petr Baudis45a3b122006-10-07 15:17:47 +0200360 # Enable the pickaxe search, which will list the commits that modified
361 # a given string in a file. This can be practical and quite faster
362 # alternative to 'blame', but still potentially CPU-intensive.
Jakub Narebskia598ded2011-06-21 08:41:16 +0200363 # Note that you need to have 'search' feature enabled too.
Petr Baudis45a3b122006-10-07 15:17:47 +0200364
365 # To enable system wide have in $GITWEB_CONFIG
366 # $feature{'pickaxe'}{'default'} = [1];
367 # To have project specific config enable override in $GITWEB_CONFIG
368 # $feature{'pickaxe'}{'override'} = 1;
369 # and in project config gitweb.pickaxe = 0|1;
Jakub Narebski04f7a942006-09-11 00:29:27 +0200370 'pickaxe' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800371 'sub' => sub { feature_bool('pickaxe', @_) },
Jakub Narebski04f7a942006-09-11 00:29:27 +0200372 'override' => 0,
373 'default' => [1]},
Martin Waitz9e756902006-10-01 23:57:48 +0200374
Jakub Narebskie4b48ea2009-09-07 14:40:00 +0200375 # Enable showing size of blobs in a 'tree' view, in a separate
376 # column, similar to what 'ls -l' does. This cost a bit of IO.
377
378 # To disable system wide have in $GITWEB_CONFIG
379 # $feature{'show-sizes'}{'default'} = [0];
380 # To have project specific config enable override in $GITWEB_CONFIG
381 # $feature{'show-sizes'}{'override'} = 1;
382 # and in project config gitweb.showsizes = 0|1;
383 'show-sizes' => {
384 'sub' => sub { feature_bool('showsizes', @_) },
385 'override' => 0,
386 'default' => [1]},
387
Petr Baudis45a3b122006-10-07 15:17:47 +0200388 # Make gitweb use an alternative format of the URLs which can be
389 # more readable and natural-looking: project name is embedded
390 # directly in the path and the query string contains other
391 # auxiliary information. All gitweb installations recognize
392 # URL in either format; this configures in which formats gitweb
393 # generates links.
394
395 # To enable system wide have in $GITWEB_CONFIG
396 # $feature{'pathinfo'}{'default'} = [1];
397 # Project specific override is not supported.
398
399 # Note that you will need to change the default location of CSS,
400 # favicon, logo and possibly other files to an absolute URL. Also,
401 # if gitweb.cgi serves as your indexfile, you will need to force
402 # $my_uri to contain the script name in your $GITWEB_CONFIG.
Martin Waitz9e756902006-10-01 23:57:48 +0200403 'pathinfo' => {
404 'override' => 0,
405 'default' => [0]},
Petr Baudise30496d2006-10-24 05:33:17 +0200406
407 # Make gitweb consider projects in project root subdirectories
408 # to be forks of existing projects. Given project $projname.git,
409 # projects matching $projname/*.git will not be shown in the main
410 # projects list, instead a '+' mark will be added to $projname
411 # there and a 'forks' view will be enabled for the project, listing
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +0200412 # all the forks. If project list is taken from a file, forks have
413 # to be listed after the main project.
Petr Baudise30496d2006-10-24 05:33:17 +0200414
415 # To enable system wide have in $GITWEB_CONFIG
416 # $feature{'forks'}{'default'} = [1];
417 # Project specific override is not supported.
418 'forks' => {
419 'override' => 0,
420 'default' => [0]},
Petr Baudisd627f682008-10-02 16:36:52 +0200421
422 # Insert custom links to the action bar of all project pages.
423 # This enables you mainly to link to third-party scripts integrating
424 # into gitweb; e.g. git-browser for graphical history representation
425 # or custom web-based repository administration interface.
426
427 # The 'default' value consists of a list of triplets in the form
428 # (label, link, position) where position is the label after which
Jakub Narebski2b11e052008-10-12 00:02:32 +0200429 # to insert the link and link is a format string where %n expands
Petr Baudisd627f682008-10-02 16:36:52 +0200430 # to the project name, %f to the project path within the filesystem,
431 # %h to the current hash (h gitweb parameter) and %b to the current
Jakub Narebski2b11e052008-10-12 00:02:32 +0200432 # hash base (hb gitweb parameter); %% expands to %.
Petr Baudisd627f682008-10-02 16:36:52 +0200433
434 # To enable system wide have in $GITWEB_CONFIG e.g.
435 # $feature{'actions'}{'default'} = [('graphiclog',
436 # '/git-browser/by-commit.html?r=%n', 'summary')];
437 # Project specific override is not supported.
438 'actions' => {
439 'override' => 0,
440 'default' => []},
Shawn O. Pearce3e3d4ee2008-10-03 07:41:25 -0700441
Jakub Narebski0368c492011-04-29 19:51:57 +0200442 # Allow gitweb scan project content tags of project repository,
443 # and display the popular Web 2.0-ish "tag cloud" near the projects
444 # list. Note that this is something COMPLETELY different from the
445 # normal Git tags.
Petr Baudisaed93de2008-10-02 17:13:02 +0200446
447 # gitweb by itself can show existing tags, but it does not handle
Jakub Narebski0368c492011-04-29 19:51:57 +0200448 # tagging itself; you need to do it externally, outside gitweb.
449 # The format is described in git_get_project_ctags() subroutine.
Petr Baudisaed93de2008-10-02 17:13:02 +0200450 # You may want to install the HTML::TagCloud Perl module to get
451 # a pretty tag cloud instead of just a list of tags.
452
453 # To enable system wide have in $GITWEB_CONFIG
Jakub Narebski0368c492011-04-29 19:51:57 +0200454 # $feature{'ctags'}{'default'} = [1];
Petr Baudisaed93de2008-10-02 17:13:02 +0200455 # Project specific override is not supported.
Jakub Narebski0368c492011-04-29 19:51:57 +0200456
457 # In the future whether ctags editing is enabled might depend
458 # on the value, but using 1 should always mean no editing of ctags.
Petr Baudisaed93de2008-10-02 17:13:02 +0200459 'ctags' => {
460 'override' => 0,
461 'default' => [0]},
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100462
463 # The maximum number of patches in a patchset generated in patch
464 # view. Set this to 0 or undef to disable patch view, or to a
465 # negative number to remove any limit.
466
467 # To disable system wide have in $GITWEB_CONFIG
468 # $feature{'patches'}{'default'} = [0];
469 # To have project specific config enable override in $GITWEB_CONFIG
470 # $feature{'patches'}{'override'} = 1;
471 # and in project config gitweb.patches = 0|n;
472 # where n is the maximum number of patches allowed in a patchset.
473 'patches' => {
474 'sub' => \&feature_patches,
475 'override' => 0,
476 'default' => [16]},
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200477
478 # Avatar support. When this feature is enabled, views such as
479 # shortlog or commit will display an avatar associated with
480 # the email of the committer(s) and/or author(s).
481
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +0200482 # Currently available providers are gravatar and picon.
483 # If an unknown provider is specified, the feature is disabled.
484
485 # Gravatar depends on Digest::MD5.
486 # Picon currently relies on the indiana.edu database.
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200487
488 # To enable system wide have in $GITWEB_CONFIG
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +0200489 # $feature{'avatar'}{'default'} = ['<provider>'];
490 # where <provider> is either gravatar or picon.
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200491 # To have project specific config enable override in $GITWEB_CONFIG
492 # $feature{'avatar'}{'override'} = 1;
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +0200493 # and in project config gitweb.avatar = <provider>;
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200494 'avatar' => {
495 'sub' => \&feature_avatar,
496 'override' => 0,
497 'default' => ['']},
Jakub Narebskiaa7dd052009-09-01 13:39:16 +0200498
499 # Enable displaying how much time and how many git commands
500 # it took to generate and display page. Disabled by default.
501 # Project specific override is not supported.
502 'timed' => {
503 'override' => 0,
504 'default' => [0]},
Jakub Narebskie627e502009-11-26 21:12:15 +0100505
506 # Enable turning some links into links to actions which require
507 # JavaScript to run (like 'blame_incremental'). Not enabled by
508 # default. Project specific override is currently not supported.
509 'javascript-actions' => {
510 'override' => 0,
511 'default' => [0]},
Johannes Schindelinb331fe52010-04-27 21:34:44 +0200512
Jakub Narebski2e987f92011-04-28 21:04:11 +0200513 # Enable and configure ability to change common timezone for dates
514 # in gitweb output via JavaScript. Enabled by default.
515 # Project specific override is not supported.
516 'javascript-timezone' => {
517 'override' => 0,
518 'default' => [
519 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
520 # or undef to turn off this feature
521 'gitweb_tz', # name of cookie where to store selected timezone
522 'datetime', # CSS class used to mark up dates for manipulation
523 ]},
524
Johannes Schindelinb331fe52010-04-27 21:34:44 +0200525 # Syntax highlighting support. This is based on Daniel Svensson's
526 # and Sham Chukoury's work in gitweb-xmms2.git.
Jakub Narebski592ea412010-04-27 21:34:45 +0200527 # It requires the 'highlight' program present in $PATH,
528 # and therefore is disabled by default.
Johannes Schindelinb331fe52010-04-27 21:34:44 +0200529
530 # To enable system wide have in $GITWEB_CONFIG
531 # $feature{'highlight'}{'default'} = [1];
532
533 'highlight' => {
534 'sub' => sub { feature_bool('highlight', @_) },
535 'override' => 0,
536 'default' => [0]},
Giuseppe Bilotta60efa242010-11-11 13:26:09 +0100537
538 # Enable displaying of remote heads in the heads list
539
540 # To enable system wide have in $GITWEB_CONFIG
541 # $feature{'remote_heads'}{'default'} = [1];
542 # To have project specific config enable override in $GITWEB_CONFIG
543 # $feature{'remote_heads'}{'override'} = 1;
544 # and in project config gitweb.remote_heads = 0|1;
545 'remote_heads' => {
546 'sub' => sub { feature_bool('remote_heads', @_) },
547 'override' => 0,
548 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530549);
550
Junio C Hamanoa7c5a282008-11-29 13:02:08 -0800551sub gitweb_get_feature {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530552 my ($name) = @_;
Jakub Narebskidd1ad5f2006-09-26 01:56:17 +0200553 return unless exists $feature{$name};
Jakub Narebski952c65f2006-08-22 16:52:50 +0200554 my ($sub, $override, @defaults) = (
555 $feature{$name}{'sub'},
556 $feature{$name}{'override'},
557 @{$feature{$name}{'default'}});
Jakub Narebski9be36142010-03-01 22:51:34 +0100558 # project specific override is possible only if we have project
559 our $git_dir; # global variable, declared later
560 if (!$override || !defined $git_dir) {
561 return @defaults;
562 }
Martin Waitza9455912006-10-03 20:07:43 +0200563 if (!defined $sub) {
Nanako Shiraishi93197892009-08-28 12:18:49 +0900564 warn "feature $name is not overridable";
Martin Waitza9455912006-10-03 20:07:43 +0200565 return @defaults;
566 }
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530567 return $sub->(@defaults);
568}
569
Giuseppe Bilotta25b27902008-11-29 13:07:29 -0800570# A wrapper to check if a given feature is enabled.
571# With this, you can say
572#
573# my $bool_feat = gitweb_check_feature('bool_feat');
574# gitweb_check_feature('bool_feat') or somecode;
575#
576# instead of
577#
578# my ($bool_feat) = gitweb_get_feature('bool_feat');
579# (gitweb_get_feature('bool_feat'))[0] or somecode;
580#
581sub gitweb_check_feature {
582 return (gitweb_get_feature(@_))[0];
583}
584
585
Matt Kraaicdad8172008-12-15 22:16:19 -0800586sub feature_bool {
587 my $key = shift;
588 my ($val) = git_get_project_config($key, '--bool');
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530589
Marcel M. Carydf5d10a2009-02-18 14:09:41 +0100590 if (!defined $val) {
591 return ($_[0]);
592 } elsif ($val eq 'true') {
Matt Kraaicdad8172008-12-15 22:16:19 -0800593 return (1);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530594 } elsif ($val eq 'false') {
Matt Kraaicdad8172008-12-15 22:16:19 -0800595 return (0);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530596 }
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530597}
598
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530599sub feature_snapshot {
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200600 my (@fmts) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530601
602 my ($val) = git_get_project_config('snapshot');
603
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200604 if ($val) {
605 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530606 }
607
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200608 return @fmts;
Luben Tuikovde9272f2006-09-28 16:49:21 -0700609}
610
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100611sub feature_patches {
612 my @val = (git_get_project_config('patches', '--int'));
613
614 if (@val) {
615 return @val;
616 }
617
618 return ($_[0]);
619}
620
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200621sub feature_avatar {
622 my @val = (git_get_project_config('avatar'));
623
624 return @val ? @val : @_;
625}
626
Junio C Hamano2172ce42006-10-03 02:30:47 -0700627# checking HEAD file with -e is fragile if the repository was
628# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
629# and then pruned.
630sub check_head_link {
631 my ($dir) = @_;
632 my $headfile = "$dir/HEAD";
633 return ((-e $headfile) ||
634 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
635}
636
637sub check_export_ok {
638 my ($dir) = @_;
639 return (check_head_link($dir) &&
Alexander Gavrilovdd7f5f12008-11-06 01:36:23 +0300640 (!$export_ok || -e "$dir/$export_ok") &&
641 (!$export_auth_hook || $export_auth_hook->($dir)));
Junio C Hamano2172ce42006-10-03 02:30:47 -0700642}
643
Jakub Narebskia7817852007-07-22 23:41:20 +0200644# process alternate names for backward compatibility
645# filter out unsupported (unknown) snapshot formats
646sub filter_snapshot_fmts {
647 my @fmts = @_;
648
649 @fmts = map {
650 exists $known_snapshot_format_aliases{$_} ?
651 $known_snapshot_format_aliases{$_} : $_} @fmts;
Jakub Narebski68cedb12009-05-10 02:40:37 +0200652 @fmts = grep {
Mark A Rada1bfd3632009-08-06 10:25:39 -0400653 exists $known_snapshot_formats{$_} &&
654 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
Jakub Narebskia7817852007-07-22 23:41:20 +0200655}
656
Jakub Narebskida4b2432010-11-25 19:43:59 +0100657# If it is set to code reference, it is code that it is to be run once per
658# request, allowing updating configurations that change with each request,
659# while running other code in config file only once.
660#
661# Otherwise, if it is false then gitweb would process config file only once;
662# if it is true then gitweb config would be run for each request.
663our $per_request_config = 1;
664
Jakub Narebskif612a712011-05-25 18:35:26 +0200665# read and parse gitweb config file given by its parameter.
666# returns true on success, false on recoverable error, allowing
667# to chain this subroutine, using first file that exists.
668# dies on errors during parsing config file, as it is unrecoverable.
669sub read_config_file {
670 my $filename = shift;
671 return unless defined $filename;
672 # die if there are errors parsing config file
673 if (-e $filename) {
674 do $filename;
675 die $@ if $@;
676 return 1;
677 }
678 return;
679}
680
Jakub Narebski131d6af2011-07-25 00:29:18 +0200681our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200682sub evaluate_gitweb_config {
683 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
684 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
Jakub Narebski131d6af2011-07-25 00:29:18 +0200685 our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "++GITWEB_CONFIG_COMMON++";
Jakub Narebskif612a712011-05-25 18:35:26 +0200686
Jakub Narebski131d6af2011-07-25 00:29:18 +0200687 # Protect agains duplications of file names, to not read config twice.
688 # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
689 # there possibility of duplication of filename there doesn't matter.
690 $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
691 $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
692
693 # Common system-wide settings for convenience.
694 # Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
695 read_config_file($GITWEB_CONFIG_COMMON);
696
697 # Use first config file that exists. This means use the per-instance
698 # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
699 read_config_file($GITWEB_CONFIG) and return;
Jakub Narebskif612a712011-05-25 18:35:26 +0200700 read_config_file($GITWEB_CONFIG_SYSTEM);
Gerrit Pape17a8b252008-03-26 18:11:19 +0000701}
Jeff Kingc8d138a2006-08-02 15:23:34 -0400702
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +0100703# Get loadavg of system, to compare against $maxload.
704# Currently it requires '/proc/loadavg' present to get loadavg;
705# if it is not present it returns 0, which means no load checking.
706sub get_loadavg {
707 if( -e '/proc/loadavg' ){
708 open my $fd, '<', '/proc/loadavg'
709 or return 0;
710 my @load = split(/\s+/, scalar <$fd>);
711 close $fd;
712
713 # The first three columns measure CPU and IO utilization of the last one,
714 # five, and 10 minute periods. The fourth column shows the number of
715 # currently running processes and the total number of processes in the m/n
716 # format. The last column displays the last process ID used.
717 return $load[0] || 0;
718 }
719 # additional checks for load average should go here for things that don't export
720 # /proc/loadavg
721
722 return 0;
723}
724
Jeff Kingc8d138a2006-08-02 15:23:34 -0400725# version of the core git binary
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200726our $git_version;
727sub evaluate_git_version {
728 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
729 $number_of_git_cmds++;
730}
Jeff Kingc8d138a2006-08-02 15:23:34 -0400731
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200732sub check_loadavg {
733 if (defined $maxload && get_loadavg() > $maxload) {
734 die_error(503, "The load average on the server is too high");
735 }
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +0100736}
737
Jakub Narebski154b4d72006-08-05 12:55:20 +0200738# ======================================================================
Kay Sievers09bd7892005-08-07 20:21:23 +0200739# input validation and dispatch
Kay Sieversb87d78d2005-08-07 20:21:04 +0200740
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200741# input parameters can be collected from a variety of sources (presently, CGI
742# and PATH_INFO), so we define an %input_params hash that collects them all
743# together during validation: this allows subsequent uses (e.g. href()) to be
744# agnostic of the parameter origin
Kay Sievers6191f8e2005-08-07 20:19:56 +0200745
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300746our %input_params = ();
Martin Waitz5c95fab2006-08-17 00:28:38 +0200747
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200748# input parameters are stored with the long parameter name as key. This will
749# also be used in the href subroutine to convert parameters to their CGI
750# equivalent, and since the href() usage is the most frequent one, we store
751# the name -> CGI key mapping here, instead of the reverse.
752#
753# XXX: Warning: If you touch this, check the search form for updating,
754# too.
Jakub Narebski24d06932006-09-26 01:57:02 +0200755
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300756our @cgi_param_mapping = (
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200757 project => "p",
758 action => "a",
759 file_name => "f",
760 file_parent => "fp",
761 hash => "h",
762 hash_parent => "hp",
763 hash_base => "hb",
764 hash_parent_base => "hpb",
765 page => "pg",
766 order => "o",
767 searchtext => "s",
768 searchtype => "st",
769 snapshot_format => "sf",
770 extra_options => "opt",
771 search_use_regexp => "sr",
Jakub Narebski0368c492011-04-29 19:51:57 +0200772 ctag => "by_tag",
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +0100773 diff_style => "ds",
Bernhard R. Link19d2d232012-01-30 21:07:37 +0100774 project_filter => "pf",
Jakub Narebskic4ccf612009-09-01 13:39:19 +0200775 # this must be last entry (for manipulation from JavaScript)
776 javascript => "js"
Miklos Vajna868bc062007-07-12 20:39:27 +0200777);
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300778our %cgi_param_mapping = @cgi_param_mapping;
Miklos Vajna868bc062007-07-12 20:39:27 +0200779
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200780# we will also need to know the possible actions, for validation
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300781our %actions = (
Rafael Garcia-Suarez3a5b9192008-06-06 09:53:32 +0200782 "blame" => \&git_blame,
Jakub Narebski4af819d2009-09-01 13:39:17 +0200783 "blame_incremental" => \&git_blame_incremental,
784 "blame_data" => \&git_blame_data,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200785 "blobdiff" => \&git_blobdiff,
786 "blobdiff_plain" => \&git_blobdiff_plain,
787 "blob" => \&git_blob,
788 "blob_plain" => \&git_blob_plain,
789 "commitdiff" => \&git_commitdiff,
790 "commitdiff_plain" => \&git_commitdiff_plain,
791 "commit" => \&git_commit,
Petr Baudise30496d2006-10-24 05:33:17 +0200792 "forks" => \&git_forks,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200793 "heads" => \&git_heads,
794 "history" => \&git_history,
795 "log" => \&git_log,
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100796 "patch" => \&git_patch,
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +0100797 "patches" => \&git_patches,
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +0100798 "remotes" => \&git_remotes,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200799 "rss" => \&git_rss,
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +0100800 "atom" => \&git_atom,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200801 "search" => \&git_search,
Petr Baudis88ad7292006-10-24 05:15:46 +0200802 "search_help" => \&git_search_help,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200803 "shortlog" => \&git_shortlog,
804 "summary" => \&git_summary,
805 "tag" => \&git_tag,
806 "tags" => \&git_tags,
807 "tree" => \&git_tree,
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +0530808 "snapshot" => \&git_snapshot,
Jakub Narebskica946012006-12-10 13:25:47 +0100809 "object" => \&git_object,
Jakub Narebski77a153f2006-08-22 16:59:20 +0200810 # those below don't need $project
811 "opml" => \&git_opml,
812 "project_list" => \&git_project_list,
Jakub Narebskifc2b2be2006-09-15 04:56:03 +0200813 "project_index" => \&git_project_index,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200814);
815
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200816# finally, we have the hash of allowed extra_options for the commands that
817# allow them
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300818our %allowed_options = (
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200819 "--no-merges" => [ qw(rss atom log shortlog history) ],
820);
821
822# fill %input_params with the CGI parameters. All values except for 'opt'
823# should be single values, but opt can be an array. We should probably
824# build an array of parameters that can be multi-valued, but since for the time
825# being it's only this one, we just single it out
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200826sub evaluate_query_params {
827 our $cgi;
828
829 while (my ($name, $symbol) = each %cgi_param_mapping) {
830 if ($symbol eq 'opt') {
Jakub Narebski84d9e2d2012-02-03 13:44:54 +0100831 $input_params{$name} = [ map { decode_utf8($_) } $cgi->param($symbol) ];
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200832 } else {
Jakub Narebski84d9e2d2012-02-03 13:44:54 +0100833 $input_params{$name} = decode_utf8($cgi->param($symbol));
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200834 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200835 }
836}
837
838# now read PATH_INFO and update the parameter list for missing parameters
839sub evaluate_path_info {
840 return if defined $input_params{'project'};
841 return if !$path_info;
842 $path_info =~ s,^/+,,;
843 return if !$path_info;
844
845 # find which part of PATH_INFO is project
846 my $project = $path_info;
847 $project =~ s,/+$,,;
848 while ($project && !check_head_link("$projectroot/$project")) {
849 $project =~ s,/*[^/]*$,,;
850 }
851 return unless $project;
852 $input_params{'project'} = $project;
853
854 # do not change any parameters if an action is given using the query string
855 return if $input_params{'action'};
856 $path_info =~ s,^\Q$project\E/*,,;
857
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200858 # next, check if we have an action
859 my $action = $path_info;
860 $action =~ s,/.*$,,;
861 if (exists $actions{$action}) {
862 $path_info =~ s,^$action/*,,;
863 $input_params{'action'} = $action;
864 }
865
866 # list of actions that want hash_base instead of hash, but can have no
867 # pathname (f) parameter
868 my @wants_base = (
869 'tree',
870 'history',
871 );
872
Jakub Narebski7e00dc52010-10-13 13:33:48 +0200873 # we want to catch, among others
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200874 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
875 my ($parentrefname, $parentpathname, $refname, $pathname) =
Jakub Narebski7e00dc52010-10-13 13:33:48 +0200876 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200877
878 # first, analyze the 'current' part
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200879 if (defined $pathname) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200880 # we got "branch:filename" or "branch:dir/"
881 # we could use git_get_type(branch:pathname), but:
882 # - it needs $git_dir
883 # - it does a git() call
884 # - the convention of terminating directories with a slash
885 # makes it superfluous
886 # - embedding the action in the PATH_INFO would make it even
887 # more superfluous
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200888 $pathname =~ s,^/+,,;
889 if (!$pathname || substr($pathname, -1) eq "/") {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200890 $input_params{'action'} ||= "tree";
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200891 $pathname =~ s,/$,,;
892 } else {
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200893 # the default action depends on whether we had parent info
894 # or not
895 if ($parentrefname) {
896 $input_params{'action'} ||= "blobdiff_plain";
897 } else {
898 $input_params{'action'} ||= "blob_plain";
899 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200900 }
901 $input_params{'hash_base'} ||= $refname;
902 $input_params{'file_name'} ||= $pathname;
903 } elsif (defined $refname) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200904 # we got "branch". In this case we have to choose if we have to
905 # set hash or hash_base.
906 #
907 # Most of the actions without a pathname only want hash to be
908 # set, except for the ones specified in @wants_base that want
909 # hash_base instead. It should also be noted that hand-crafted
910 # links having 'history' as an action and no pathname or hash
911 # set will fail, but that happens regardless of PATH_INFO.
Jakub Narebskid0af3732010-10-13 13:35:20 +0200912 if (defined $parentrefname) {
913 # if there is parent let the default be 'shortlog' action
914 # (for http://git.example.com/repo.git/A..B links); if there
915 # is no parent, dispatch will detect type of object and set
916 # action appropriately if required (if action is not set)
917 $input_params{'action'} ||= "shortlog";
918 }
919 if ($input_params{'action'} &&
920 grep { $_ eq $input_params{'action'} } @wants_base) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200921 $input_params{'hash_base'} ||= $refname;
922 } else {
923 $input_params{'hash'} ||= $refname;
924 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200925 }
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200926
927 # next, handle the 'parent' part, if present
928 if (defined $parentrefname) {
929 # a missing pathspec defaults to the 'current' filename, allowing e.g.
930 # someproject/blobdiff/oldrev..newrev:/filename
931 if ($parentpathname) {
932 $parentpathname =~ s,^/+,,;
933 $parentpathname =~ s,/$,,;
934 $input_params{'file_parent'} ||= $parentpathname;
935 } else {
936 $input_params{'file_parent'} ||= $input_params{'file_name'};
937 }
938 # we assume that hash_parent_base is wanted if a path was specified,
939 # or if the action wants hash_base instead of hash
940 if (defined $input_params{'file_parent'} ||
941 grep { $_ eq $input_params{'action'} } @wants_base) {
942 $input_params{'hash_parent_base'} ||= $parentrefname;
943 } else {
944 $input_params{'hash_parent'} ||= $parentrefname;
945 }
946 }
Giuseppe Bilotta1ec2fb52008-11-02 10:21:38 +0100947
948 # for the snapshot action, we allow URLs in the form
949 # $project/snapshot/$hash.ext
950 # where .ext determines the snapshot and gets removed from the
951 # passed $refname to provide the $hash.
952 #
953 # To be able to tell that $refname includes the format extension, we
954 # require the following two conditions to be satisfied:
955 # - the hash input parameter MUST have been set from the $refname part
956 # of the URL (i.e. they must be equal)
957 # - the snapshot format MUST NOT have been defined already (e.g. from
958 # CGI parameter sf)
959 # It's also useless to try any matching unless $refname has a dot,
960 # so we check for that too
961 if (defined $input_params{'action'} &&
962 $input_params{'action'} eq 'snapshot' &&
963 defined $refname && index($refname, '.') != -1 &&
964 $refname eq $input_params{'hash'} &&
965 !defined $input_params{'snapshot_format'}) {
966 # We loop over the known snapshot formats, checking for
967 # extensions. Allowed extensions are both the defined suffix
968 # (which includes the initial dot already) and the snapshot
969 # format key itself, with a prepended dot
Holger Weißccb4b532009-03-31 18:16:36 +0200970 while (my ($fmt, $opt) = each %known_snapshot_formats) {
Giuseppe Bilotta1ec2fb52008-11-02 10:21:38 +0100971 my $hash = $refname;
Jakub Narebski095e9142009-05-11 19:42:47 +0200972 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
973 next;
974 }
975 my $sfx = $1;
Giuseppe Bilotta1ec2fb52008-11-02 10:21:38 +0100976 # a valid suffix was found, so set the snapshot format
977 # and reset the hash parameter
978 $input_params{'snapshot_format'} = $fmt;
979 $input_params{'hash'} = $hash;
980 # we also set the format suffix to the one requested
981 # in the URL: this way a request for e.g. .tgz returns
982 # a .tgz instead of a .tar.gz
983 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
984 last;
985 }
986 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200987}
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200988
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200989our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
990 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
Bernhard R. Link19d2d232012-01-30 21:07:37 +0100991 $searchtext, $search_regexp, $project_filter);
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200992sub evaluate_and_validate_params {
993 our $action = $input_params{'action'};
994 if (defined $action) {
995 if (!validate_action($action)) {
996 die_error(400, "Invalid action parameter");
997 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200998 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200999
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001000 # parameters which are pathnames
1001 our $project = $input_params{'project'};
1002 if (defined $project) {
1003 if (!validate_project($project)) {
1004 undef $project;
1005 die_error(404, "No such project");
1006 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001007 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001008
Bernhard R. Link19d2d232012-01-30 21:07:37 +01001009 our $project_filter = $input_params{'project_filter'};
1010 if (defined $project_filter) {
1011 if (!validate_pathname($project_filter)) {
1012 die_error(404, "Invalid project_filter parameter");
1013 }
1014 }
1015
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001016 our $file_name = $input_params{'file_name'};
1017 if (defined $file_name) {
1018 if (!validate_pathname($file_name)) {
1019 die_error(400, "Invalid file parameter");
1020 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001021 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001022
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001023 our $file_parent = $input_params{'file_parent'};
1024 if (defined $file_parent) {
1025 if (!validate_pathname($file_parent)) {
1026 die_error(400, "Invalid file parent parameter");
1027 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001028 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001029
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001030 # parameters which are refnames
1031 our $hash = $input_params{'hash'};
1032 if (defined $hash) {
1033 if (!validate_refname($hash)) {
1034 die_error(400, "Invalid hash parameter");
1035 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001036 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001037
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001038 our $hash_parent = $input_params{'hash_parent'};
1039 if (defined $hash_parent) {
1040 if (!validate_refname($hash_parent)) {
1041 die_error(400, "Invalid hash parent parameter");
1042 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001043 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001044
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001045 our $hash_base = $input_params{'hash_base'};
1046 if (defined $hash_base) {
1047 if (!validate_refname($hash_base)) {
1048 die_error(400, "Invalid hash base parameter");
1049 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001050 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001051
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001052 our @extra_options = @{$input_params{'extra_options'}};
1053 # @extra_options is always defined, since it can only be (currently) set from
1054 # CGI, and $cgi->param() returns the empty array in array context if the param
1055 # is not set
1056 foreach my $opt (@extra_options) {
1057 if (not exists $allowed_options{$opt}) {
1058 die_error(400, "Invalid option parameter");
1059 }
1060 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1061 die_error(400, "Invalid option parameter for this action");
1062 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001063 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001064
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001065 our $hash_parent_base = $input_params{'hash_parent_base'};
1066 if (defined $hash_parent_base) {
1067 if (!validate_refname($hash_parent_base)) {
1068 die_error(400, "Invalid hash parent base parameter");
1069 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001070 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001071
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001072 # other parameters
1073 our $page = $input_params{'page'};
1074 if (defined $page) {
1075 if ($page =~ m/[^0-9]/) {
1076 die_error(400, "Invalid page parameter");
1077 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001078 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001079
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001080 our $searchtype = $input_params{'searchtype'};
1081 if (defined $searchtype) {
1082 if ($searchtype =~ m/[^a-z]/) {
1083 die_error(400, "Invalid searchtype parameter");
1084 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001085 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001086
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001087 our $search_use_regexp = $input_params{'search_use_regexp'};
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001088
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001089 our $searchtext = $input_params{'searchtext'};
1090 our $search_regexp;
1091 if (defined $searchtext) {
1092 if (length($searchtext) < 2) {
1093 die_error(403, "At least two characters are required for search parameter");
1094 }
Jakub Narebski36612e42012-02-28 19:41:47 +01001095 if ($search_use_regexp) {
1096 $search_regexp = $searchtext;
1097 if (!eval { qr/$search_regexp/; 1; }) {
1098 (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1099 die_error(400, "Invalid search regexp '$search_regexp'",
1100 esc_html($error));
1101 }
1102 } else {
1103 $search_regexp = quotemeta $searchtext;
1104 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001105 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001106}
1107
1108# path to the current git repository
1109our $git_dir;
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001110sub evaluate_git_dir {
1111 our $git_dir = "$projectroot/$project" if $project;
1112}
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001113
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001114our (@snapshot_fmts, $git_avatar);
1115sub configure_gitweb_features {
1116 # list of supported snapshot formats
1117 our @snapshot_fmts = gitweb_get_feature('snapshot');
1118 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01001119
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001120 # check that the avatar feature is set to a known provider name,
1121 # and for each provider check if the dependencies are satisfied.
1122 # if the provider name is invalid or the dependencies are not met,
1123 # reset $git_avatar to the empty string.
1124 our ($git_avatar) = gitweb_get_feature('avatar');
1125 if ($git_avatar eq 'gravatar') {
1126 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1127 } elsif ($git_avatar eq 'picon') {
1128 # no dependencies
1129 } else {
1130 $git_avatar = '';
1131 }
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02001132}
1133
Jakub Narebski7a597452010-04-24 16:00:04 +02001134# custom error handler: 'die <message>' is Internal Server Error
1135sub handle_errors_html {
1136 my $msg = shift; # it is already HTML escaped
1137
1138 # to avoid infinite loop where error occurs in die_error,
1139 # change handler to default handler, disabling handle_errors_html
1140 set_message("Error occured when inside die_error:\n$msg");
1141
1142 # you cannot jump out of die_error when called as error handler;
1143 # the subroutine set via CGI::Carp::set_message is called _after_
1144 # HTTP headers are already written, so it cannot write them itself
1145 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1146}
1147set_message(\&handle_errors_html);
1148
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001149# dispatch
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001150sub dispatch {
1151 if (!defined $action) {
1152 if (defined $hash) {
1153 $action = git_get_type($hash);
Jakub Narebski18ab83e2012-01-07 11:47:38 +01001154 $action or die_error(404, "Object does not exist");
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001155 } elsif (defined $hash_base && defined $file_name) {
1156 $action = git_get_type("$hash_base:$file_name");
Jakub Narebski18ab83e2012-01-07 11:47:38 +01001157 $action or die_error(404, "File or directory does not exist");
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001158 } elsif (defined $project) {
1159 $action = 'summary';
1160 } else {
1161 $action = 'project_list';
1162 }
Gerrit Pape7f9778b2007-05-10 07:32:07 +00001163 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001164 if (!defined($actions{$action})) {
1165 die_error(400, "Unknown action");
1166 }
1167 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1168 !$project) {
1169 die_error(400, "Project needed");
1170 }
1171 $actions{$action}->();
Jakub Narebski77a153f2006-08-22 16:59:20 +02001172}
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001173
Jakub Narebski869d5882010-07-05 20:52:43 +02001174sub reset_timer {
Jakub Narebski3962f1d72010-11-09 19:27:54 +01001175 our $t0 = [ gettimeofday() ]
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001176 if defined $t0;
Jakub Narebski869d5882010-07-05 20:52:43 +02001177 our $number_of_git_cmds = 0;
1178}
1179
Jakub Narebskida4b2432010-11-25 19:43:59 +01001180our $first_request = 1;
Jakub Narebski869d5882010-07-05 20:52:43 +02001181sub run_request {
1182 reset_timer();
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001183
1184 evaluate_uri();
Jakub Narebskida4b2432010-11-25 19:43:59 +01001185 if ($first_request) {
1186 evaluate_gitweb_config();
1187 evaluate_git_version();
1188 }
1189 if ($per_request_config) {
1190 if (ref($per_request_config) eq 'CODE') {
1191 $per_request_config->();
1192 } elsif (!$first_request) {
1193 evaluate_gitweb_config();
1194 }
1195 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001196 check_loadavg();
1197
Jonathan Nieder7f425db2010-07-30 22:01:59 -05001198 # $projectroot and $projects_list might be set in gitweb config file
1199 $projects_list ||= $projectroot;
1200
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001201 evaluate_query_params();
1202 evaluate_path_info();
1203 evaluate_and_validate_params();
1204 evaluate_git_dir();
1205
1206 configure_gitweb_features();
1207
1208 dispatch();
Kay Sievers09bd7892005-08-07 20:21:23 +02001209}
Sam Vilaina0446e72010-05-07 14:54:05 +02001210
1211our $is_last_request = sub { 1 };
1212our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1213our $CGI = 'CGI';
1214our $cgi;
Jakub Narebski45aa9892010-06-05 23:11:18 +02001215sub configure_as_fcgi {
1216 require CGI::Fast;
1217 our $CGI = 'CGI::Fast';
1218
1219 my $request_number = 0;
1220 # let each child service 100 requests
1221 our $is_last_request = sub { ++$request_number > 100 };
Jakub Narebskid04d3d42006-09-19 21:53:22 +02001222}
Sam Vilaina0446e72010-05-07 14:54:05 +02001223sub evaluate_argv {
Jakub Narebski45aa9892010-06-05 23:11:18 +02001224 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1225 configure_as_fcgi()
1226 if $script_name =~ /\.fcgi$/;
1227
Sam Vilaina0446e72010-05-07 14:54:05 +02001228 return unless (@ARGV);
1229
1230 require Getopt::Long;
1231 Getopt::Long::GetOptions(
Jakub Narebski45aa9892010-06-05 23:11:18 +02001232 'fastcgi|fcgi|f' => \&configure_as_fcgi,
Sam Vilaina0446e72010-05-07 14:54:05 +02001233 'nproc|n=i' => sub {
1234 my ($arg, $val) = @_;
1235 return unless eval { require FCGI::ProcManager; 1; };
1236 my $proc_manager = FCGI::ProcManager->new({
1237 n_processes => $val,
1238 });
1239 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1240 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1241 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1242 },
1243 );
1244}
1245
1246sub run {
1247 evaluate_argv();
Jakub Narebski869d5882010-07-05 20:52:43 +02001248
Jakub Narebskida4b2432010-11-25 19:43:59 +01001249 $first_request = 1;
Sam Vilaina0446e72010-05-07 14:54:05 +02001250 $pre_listen_hook->()
1251 if $pre_listen_hook;
1252
1253 REQUEST:
1254 while ($cgi = $CGI->new()) {
1255 $pre_dispatch_hook->()
1256 if $pre_dispatch_hook;
1257
1258 run_request();
1259
Jakub Narebski0b450102010-08-02 22:21:47 +02001260 $post_dispatch_hook->()
Sam Vilaina0446e72010-05-07 14:54:05 +02001261 if $post_dispatch_hook;
Jakub Narebskida4b2432010-11-25 19:43:59 +01001262 $first_request = 0;
Sam Vilaina0446e72010-05-07 14:54:05 +02001263
1264 last REQUEST if ($is_last_request->());
1265 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001266
1267 DONE_GITWEB:
1268 1;
Kay Sievers09bd7892005-08-07 20:21:23 +02001269}
Sam Vilaina0446e72010-05-07 14:54:05 +02001270
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001271run();
Kay Sievers09bd7892005-08-07 20:21:23 +02001272
Jakub Narebski5ed2ec12010-06-13 12:09:32 +02001273if (defined caller) {
1274 # wrapped in a subroutine processing requests,
1275 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1276 return;
1277} else {
1278 # pure CGI script, serving single request
1279 exit;
1280}
Kay Sievers823d5dc2005-08-07 19:57:58 +02001281
Jakub Narebski717b8312006-07-31 21:22:15 +02001282## ======================================================================
Martin Waitz06a9d862006-08-16 00:23:50 +02001283## action links
1284
Jakub Narebski377bee32010-04-24 15:53:19 +02001285# possible values of extra options
1286# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1287# -replay => 1 - start from a current view (replay with modifications)
1288# -path_info => 0|1 - don't use/use path_info URL (if possible)
Kevin Cernekee5e96a842011-03-18 17:00:16 +01001289# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
Jakub Narebski74fd8722009-05-07 19:11:29 +02001290sub href {
Jakub Narebski498fe002006-08-22 19:05:25 +02001291 my %params = @_;
Jakub Narebskibd5d1e42006-11-19 15:05:21 +01001292 # default is to use -absolute url() i.e. $my_uri
1293 my $href = $params{-full} ? $my_url : $my_uri;
Jakub Narebski498fe002006-08-22 19:05:25 +02001294
Kevin Cernekee5e96a842011-03-18 17:00:16 +01001295 # implicit -replay, must be first of implicit params
1296 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1297
Jakub Narebskiafa9b622008-02-14 09:22:30 +01001298 $params{'project'} = $project unless exists $params{'project'};
1299
Jakub Narebski1cad2832007-11-01 13:06:27 +01001300 if ($params{-replay}) {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001301 while (my ($name, $symbol) = each %cgi_param_mapping) {
Jakub Narebski1cad2832007-11-01 13:06:27 +01001302 if (!exists $params{$name}) {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001303 $params{$name} = $input_params{$name};
Jakub Narebski1cad2832007-11-01 13:06:27 +01001304 }
1305 }
1306 }
1307
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08001308 my $use_pathinfo = gitweb_check_feature('pathinfo');
Jakub Narebski377bee32010-04-24 15:53:19 +02001309 if (defined $params{'project'} &&
1310 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001311 # try to put as many parameters as possible in PATH_INFO:
1312 # - project name
1313 # - action
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001314 # - hash_parent or hash_parent_base:/file_parent
Giuseppe Bilotta3550ea72008-10-21 21:34:52 +02001315 # - hash or hash_base:/filename
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +01001316 # - the snapshot_format as an appropriate suffix
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001317
1318 # When the script is the root DirectoryIndex for the domain,
1319 # $href here would be something like http://gitweb.example.com/
1320 # Thus, we strip any trailing / from $href, to spare us double
1321 # slashes in the final URL
1322 $href =~ s,/$,,;
1323
1324 # Then add the project name, if present
Jakub Narebski67976c62010-12-14 16:54:31 +01001325 $href .= "/".esc_path_info($params{'project'});
Martin Waitz9e756902006-10-01 23:57:48 +02001326 delete $params{'project'};
1327
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +01001328 # since we destructively absorb parameters, we keep this
1329 # boolean that remembers if we're handling a snapshot
1330 my $is_snapshot = $params{'action'} eq 'snapshot';
1331
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001332 # Summary just uses the project path URL, any other action is
1333 # added to the URL
1334 if (defined $params{'action'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001335 $href .= "/".esc_path_info($params{'action'})
1336 unless $params{'action'} eq 'summary';
Martin Waitz9e756902006-10-01 23:57:48 +02001337 delete $params{'action'};
1338 }
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001339
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001340 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1341 # stripping nonexistent or useless pieces
1342 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1343 || $params{'hash_parent'} || $params{'hash'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001344 if (defined $params{'hash_base'}) {
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001345 if (defined $params{'hash_parent_base'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001346 $href .= esc_path_info($params{'hash_parent_base'});
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001347 # skip the file_parent if it's the same as the file_name
Giuseppe Bilottab7da7212009-07-31 08:48:49 +02001348 if (defined $params{'file_parent'}) {
1349 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1350 delete $params{'file_parent'};
1351 } elsif ($params{'file_parent'} !~ /\.\./) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001352 $href .= ":/".esc_path_info($params{'file_parent'});
Giuseppe Bilottab7da7212009-07-31 08:48:49 +02001353 delete $params{'file_parent'};
1354 }
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001355 }
1356 $href .= "..";
1357 delete $params{'hash_parent'};
1358 delete $params{'hash_parent_base'};
1359 } elsif (defined $params{'hash_parent'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001360 $href .= esc_path_info($params{'hash_parent'}). "..";
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001361 delete $params{'hash_parent'};
1362 }
1363
Jakub Narebski67976c62010-12-14 16:54:31 +01001364 $href .= esc_path_info($params{'hash_base'});
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001365 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001366 $href .= ":/".esc_path_info($params{'file_name'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001367 delete $params{'file_name'};
1368 }
1369 delete $params{'hash'};
1370 delete $params{'hash_base'};
1371 } elsif (defined $params{'hash'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001372 $href .= esc_path_info($params{'hash'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001373 delete $params{'hash'};
1374 }
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +01001375
1376 # If the action was a snapshot, we can absorb the
1377 # snapshot_format parameter too
1378 if ($is_snapshot) {
1379 my $fmt = $params{'snapshot_format'};
1380 # snapshot_format should always be defined when href()
1381 # is called, but just in case some code forgets, we
1382 # fall back to the default
1383 $fmt ||= $snapshot_fmts[0];
1384 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1385 delete $params{'snapshot_format'};
1386 }
Martin Waitz9e756902006-10-01 23:57:48 +02001387 }
1388
1389 # now encode the parameters explicitly
Jakub Narebski498fe002006-08-22 19:05:25 +02001390 my @result = ();
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001391 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1392 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
Jakub Narebski498fe002006-08-22 19:05:25 +02001393 if (defined $params{$name}) {
Jakub Narebskif22cca42007-07-29 01:04:09 +02001394 if (ref($params{$name}) eq "ARRAY") {
1395 foreach my $par (@{$params{$name}}) {
1396 push @result, $symbol . "=" . esc_param($par);
1397 }
1398 } else {
1399 push @result, $symbol . "=" . esc_param($params{$name});
1400 }
Jakub Narebski498fe002006-08-22 19:05:25 +02001401 }
1402 }
Martin Waitz9e756902006-10-01 23:57:48 +02001403 $href .= "?" . join(';', @result) if scalar @result;
1404
Jakub Narebski67976c62010-12-14 16:54:31 +01001405 # final transformation: trailing spaces must be escaped (URI-encoded)
1406 $href =~ s/(\s+)$/CGI::escape($1)/e;
1407
Kevin Cernekee5e96a842011-03-18 17:00:16 +01001408 if ($params{-anchor}) {
1409 $href .= "#".esc_param($params{-anchor});
1410 }
1411
Martin Waitz9e756902006-10-01 23:57:48 +02001412 return $href;
Martin Waitz06a9d862006-08-16 00:23:50 +02001413}
1414
1415
1416## ======================================================================
Jakub Narebski717b8312006-07-31 21:22:15 +02001417## validation, quoting/unquoting and escaping
1418
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001419sub validate_action {
1420 my $input = shift || return undef;
1421 return undef unless exists $actions{$input};
1422 return $input;
1423}
1424
1425sub validate_project {
1426 my $input = shift || return undef;
1427 if (!validate_pathname($input) ||
1428 !(-d "$projectroot/$input") ||
Alexander Gavrilovec26f092008-11-06 01:15:56 +03001429 !check_export_ok("$projectroot/$input") ||
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001430 ($strict_export && !project_in_list($input))) {
1431 return undef;
1432 } else {
1433 return $input;
1434 }
1435}
1436
Jakub Narebski24d06932006-09-26 01:57:02 +02001437sub validate_pathname {
1438 my $input = shift || return undef;
Jakub Narebski717b8312006-07-31 21:22:15 +02001439
Jakub Narebski24d06932006-09-26 01:57:02 +02001440 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1441 # at the beginning, at the end, and between slashes.
1442 # also this catches doubled slashes
1443 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1444 return undef;
1445 }
1446 # no null characters
1447 if ($input =~ m!\0!) {
1448 return undef;
1449 }
1450 return $input;
1451}
1452
1453sub validate_refname {
1454 my $input = shift || return undef;
1455
1456 # textual hashes are O.K.
Jakub Narebski717b8312006-07-31 21:22:15 +02001457 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1458 return $input;
1459 }
Jakub Narebski24d06932006-09-26 01:57:02 +02001460 # it must be correct pathname
1461 $input = validate_pathname($input)
1462 or return undef;
1463 # restrictions on ref name according to git-check-ref-format
1464 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001465 return undef;
1466 }
1467 return $input;
1468}
1469
Martin Koegler00f429a2007-06-03 17:42:44 +02001470# decode sequences of octets in utf8 into Perl's internal form,
1471# which is utf-8 with utf8 flag set if needed. gitweb writes out
1472# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1473sub to_utf8 {
1474 my $str = shift;
Jakub Narebski1df48762010-02-07 21:52:25 +01001475 return undef unless defined $str;
Jakub Narebskib13e3ea2011-12-18 23:00:58 +01001476
1477 if (utf8::is_utf8($str) || utf8::decode($str)) {
İsmail Dönmeze5d3de52007-12-04 10:55:41 +02001478 return $str;
Martin Koegler00f429a2007-06-03 17:42:44 +02001479 } else {
1480 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1481 }
1482}
1483
Kay Sievers232ff552005-11-24 16:56:55 +01001484# quote unsafe chars, but keep the slash, even when it's not
1485# correct, but quoted slashes look too horrible in bookmarks
1486sub esc_param {
Kay Sievers353347b2005-11-14 05:47:18 +01001487 my $str = shift;
Jakub Narebski1df48762010-02-07 21:52:25 +01001488 return undef unless defined $str;
Giuseppe Bilotta452e2252009-10-13 21:51:36 +02001489 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
Kay Sieversa9e60b72005-11-14 15:15:12 +01001490 $str =~ s/ /\+/g;
Kay Sievers353347b2005-11-14 05:47:18 +01001491 return $str;
1492}
1493
Jakub Narebski67976c62010-12-14 16:54:31 +01001494# the quoting rules for path_info fragment are slightly different
1495sub esc_path_info {
1496 my $str = shift;
1497 return undef unless defined $str;
1498
1499 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1500 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1501
1502 return $str;
1503}
1504
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02001505# quote unsafe chars in whole URL, so some characters cannot be quoted
Jakub Narebskif93bff82006-09-26 01:58:41 +02001506sub esc_url {
1507 my $str = shift;
Jakub Narebski1df48762010-02-07 21:52:25 +01001508 return undef unless defined $str;
Pavan Kumar Sunkara109988f2010-07-15 12:59:01 +05301509 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
Jakub Narebskif93bff82006-09-26 01:58:41 +02001510 $str =~ s/ /\+/g;
1511 return $str;
1512}
1513
Jakub Narebski3017ed62010-12-15 00:34:01 +01001514# quote unsafe characters in HTML attributes
1515sub esc_attr {
1516
1517 # for XHTML conformance escaping '"' to '&quot;' is not enough
1518 return esc_html(@_);
1519}
1520
Kay Sievers232ff552005-11-24 16:56:55 +01001521# replace invalid utf8 character with SUBSTITUTION sequence
Jakub Narebski74fd8722009-05-07 19:11:29 +02001522sub esc_html {
Kay Sievers40c13812005-11-19 17:41:29 +01001523 my $str = shift;
Jakub Narebski6255ef02006-11-01 14:33:21 +01001524 my %opts = @_;
1525
Jakub Narebski1df48762010-02-07 21:52:25 +01001526 return undef unless defined $str;
1527
Martin Koegler00f429a2007-06-03 17:42:44 +02001528 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +08001529 $str = $cgi->escapeHTML($str);
Jakub Narebski6255ef02006-11-01 14:33:21 +01001530 if ($opts{'-nbsp'}) {
1531 $str =~ s/ /&nbsp;/g;
1532 }
Junio C Hamano25ffbb22006-11-08 15:11:10 -08001533 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
Kay Sievers40c13812005-11-19 17:41:29 +01001534 return $str;
1535}
1536
Jakub Narebski391862e2006-11-25 09:43:59 +01001537# quote control characters and escape filename to HTML
1538sub esc_path {
1539 my $str = shift;
1540 my %opts = @_;
1541
Jakub Narebski1df48762010-02-07 21:52:25 +01001542 return undef unless defined $str;
1543
Martin Koegler00f429a2007-06-03 17:42:44 +02001544 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +08001545 $str = $cgi->escapeHTML($str);
Jakub Narebski391862e2006-11-25 09:43:59 +01001546 if ($opts{'-nbsp'}) {
1547 $str =~ s/ /&nbsp;/g;
1548 }
1549 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1550 return $str;
1551}
1552
Jakub Narebski08667862011-09-16 14:41:57 +02001553# Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)
1554sub sanitize {
1555 my $str = shift;
1556
1557 return undef unless defined $str;
1558
1559 $str = to_utf8($str);
1560 $str =~ s|([[:cntrl:]])|($1 =~ /[\t\n\r]/ ? $1 : quot_cec($1))|eg;
1561 return $str;
1562}
1563
Jakub Narebski391862e2006-11-25 09:43:59 +01001564# Make control characters "printable", using character escape codes (CEC)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001565sub quot_cec {
1566 my $cntrl = shift;
Jakub Narebskic84c4832008-02-17 18:48:13 +01001567 my %opts = @_;
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001568 my %es = ( # character escape codes, aka escape sequences
Jakub Narebskic84c4832008-02-17 18:48:13 +01001569 "\t" => '\t', # tab (HT)
1570 "\n" => '\n', # line feed (LF)
1571 "\r" => '\r', # carrige return (CR)
1572 "\f" => '\f', # form feed (FF)
1573 "\b" => '\b', # backspace (BS)
1574 "\a" => '\a', # alarm (bell) (BEL)
1575 "\e" => '\e', # escape (ESC)
1576 "\013" => '\v', # vertical tab (VT)
1577 "\000" => '\0', # nul character (NUL)
1578 );
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001579 my $chr = ( (exists $es{$cntrl})
1580 ? $es{$cntrl}
Petr Baudis25dfd172008-10-01 22:11:54 +02001581 : sprintf('\%2x', ord($cntrl)) );
Jakub Narebskic84c4832008-02-17 18:48:13 +01001582 if ($opts{-nohtml}) {
1583 return $chr;
1584 } else {
1585 return "<span class=\"cntrl\">$chr</span>";
1586 }
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001587}
1588
Jakub Narebski391862e2006-11-25 09:43:59 +01001589# Alternatively use unicode control pictures codepoints,
1590# Unicode "printable representation" (PR)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001591sub quot_upr {
1592 my $cntrl = shift;
Jakub Narebskic84c4832008-02-17 18:48:13 +01001593 my %opts = @_;
1594
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001595 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
Jakub Narebskic84c4832008-02-17 18:48:13 +01001596 if ($opts{-nohtml}) {
1597 return $chr;
1598 } else {
1599 return "<span class=\"cntrl\">$chr</span>";
1600 }
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001601}
1602
Kay Sievers232ff552005-11-24 16:56:55 +01001603# git may return quoted and escaped filenames
1604sub unquote {
1605 my $str = shift;
Jakub Narebski403d0902006-11-08 11:48:56 +01001606
1607 sub unq {
1608 my $seq = shift;
1609 my %es = ( # character escape codes, aka escape sequences
1610 't' => "\t", # tab (HT, TAB)
1611 'n' => "\n", # newline (NL)
1612 'r' => "\r", # return (CR)
1613 'f' => "\f", # form feed (FF)
1614 'b' => "\b", # backspace (BS)
1615 'a' => "\a", # alarm (bell) (BEL)
1616 'e' => "\e", # escape (ESC)
1617 'v' => "\013", # vertical tab (VT)
1618 );
1619
1620 if ($seq =~ m/^[0-7]{1,3}$/) {
1621 # octal char sequence
1622 return chr(oct($seq));
1623 } elsif (exists $es{$seq}) {
1624 # C escape sequence, aka character escape code
Jakub Narebskic84c4832008-02-17 18:48:13 +01001625 return $es{$seq};
Jakub Narebski403d0902006-11-08 11:48:56 +01001626 }
1627 # quoted ordinary character
1628 return $seq;
1629 }
1630
Kay Sievers232ff552005-11-24 16:56:55 +01001631 if ($str =~ m/^"(.*)"$/) {
Jakub Narebski403d0902006-11-08 11:48:56 +01001632 # needs unquoting
Kay Sievers232ff552005-11-24 16:56:55 +01001633 $str = $1;
Jakub Narebski403d0902006-11-08 11:48:56 +01001634 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
Kay Sievers232ff552005-11-24 16:56:55 +01001635 }
1636 return $str;
1637}
1638
Jakub Narebskif16db172006-08-06 02:08:31 +02001639# escape tabs (convert tabs to spaces)
1640sub untabify {
1641 my $line = shift;
1642
1643 while ((my $pos = index($line, "\t")) != -1) {
1644 if (my $count = (8 - ($pos % 8))) {
1645 my $spaces = ' ' x $count;
1646 $line =~ s/\t/$spaces/;
1647 }
1648 }
1649
1650 return $line;
1651}
1652
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +02001653sub project_in_list {
1654 my $project = shift;
1655 my @list = git_get_projects_list();
1656 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1657}
1658
Jakub Narebski717b8312006-07-31 21:22:15 +02001659## ----------------------------------------------------------------------
1660## HTML aware string manipulation
1661
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001662# Try to chop given string on a word boundary between position
1663# $len and $len+$add_len. If there is no word boundary there,
1664# chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1665# (marking chopped part) would be longer than given string.
Jakub Narebski717b8312006-07-31 21:22:15 +02001666sub chop_str {
1667 my $str = shift;
1668 my $len = shift;
1669 my $add_len = shift || 10;
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001670 my $where = shift || 'right'; # 'left' | 'center' | 'right'
Jakub Narebski717b8312006-07-31 21:22:15 +02001671
Anders Waldenborgdee27752008-05-21 13:44:43 +02001672 # Make sure perl knows it is utf8 encoded so we don't
1673 # cut in the middle of a utf8 multibyte char.
1674 $str = to_utf8($str);
1675
Jakub Narebski717b8312006-07-31 21:22:15 +02001676 # allow only $len chars, but don't cut a word if it would fit in $add_len
1677 # 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 +01001678 # remove chopped character entities entirely
1679
1680 # when chopping in the middle, distribute $len into left and right part
1681 # return early if chopping wouldn't make string shorter
1682 if ($where eq 'center') {
1683 return $str if ($len + 5 >= length($str)); # filler is length 5
1684 $len = int($len/2);
1685 } else {
1686 return $str if ($len + 4 >= length($str)); # filler is length 4
Jakub Narebski717b8312006-07-31 21:22:15 +02001687 }
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001688
1689 # regexps: ending and beginning with word part up to $add_len
1690 my $endre = qr/.{$len}\w{0,$add_len}/;
1691 my $begre = qr/\w{0,$add_len}.{$len}/;
1692
1693 if ($where eq 'left') {
1694 $str =~ m/^(.*?)($begre)$/;
1695 my ($lead, $body) = ($1, $2);
1696 if (length($lead) > 4) {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001697 $lead = " ...";
1698 }
1699 return "$lead$body";
1700
1701 } elsif ($where eq 'center') {
1702 $str =~ m/^($endre)(.*)$/;
1703 my ($left, $str) = ($1, $2);
1704 $str =~ m/^(.*?)($begre)$/;
1705 my ($mid, $right) = ($1, $2);
1706 if (length($mid) > 5) {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001707 $mid = " ... ";
1708 }
1709 return "$left$mid$right";
1710
1711 } else {
1712 $str =~ m/^($endre)(.*)$/;
1713 my $body = $1;
1714 my $tail = $2;
1715 if (length($tail) > 4) {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001716 $tail = "... ";
1717 }
1718 return "$body$tail";
1719 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001720}
1721
David Symondsce58ec92007-10-23 11:31:22 +10001722# takes the same arguments as chop_str, but also wraps a <span> around the
1723# result with a title attribute if it does get chopped. Additionally, the
1724# string is HTML-escaped.
1725sub chop_and_escape_str {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001726 my ($str) = @_;
David Symondsce58ec92007-10-23 11:31:22 +10001727
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001728 my $chopped = chop_str(@_);
Jürgen Kreileder168c1e02011-12-17 10:22:21 +01001729 $str = to_utf8($str);
David Symondsce58ec92007-10-23 11:31:22 +10001730 if ($chopped eq $str) {
1731 return esc_html($chopped);
1732 } else {
Jakub Narebski14afe772009-05-22 17:35:46 +02001733 $str =~ s/[[:cntrl:]]/?/g;
Jakub Narebski850b90a2008-02-16 23:07:46 +01001734 return $cgi->span({-title=>$str}, esc_html($chopped));
David Symondsce58ec92007-10-23 11:31:22 +10001735 }
1736}
1737
Jakub Narebski337da8d2012-02-27 02:55:19 +01001738# Highlight selected fragments of string, using given CSS class,
1739# and escape HTML. It is assumed that fragments do not overlap.
1740# Regions are passed as list of pairs (array references).
1741#
1742# Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns
1743# '<span class="mark">foo</span>bar'
1744sub esc_html_hl_regions {
1745 my ($str, $css_class, @sel) = @_;
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001746 my %opts = grep { ref($_) ne 'ARRAY' } @sel;
1747 @sel = grep { ref($_) eq 'ARRAY' } @sel;
1748 return esc_html($str, %opts) unless @sel;
Jakub Narebski337da8d2012-02-27 02:55:19 +01001749
1750 my $out = '';
1751 my $pos = 0;
1752
1753 for my $s (@sel) {
Michał Kiedrowiczce61fb92012-04-11 23:18:37 +02001754 my ($begin, $end) = @$s;
Jakub Narebski337da8d2012-02-27 02:55:19 +01001755
Michał Kiedrowiczcbbea3d2012-04-11 23:18:38 +02001756 # Don't create empty <span> elements.
1757 next if $end <= $begin;
1758
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001759 my $escaped = esc_html(substr($str, $begin, $end - $begin),
1760 %opts);
Michał Kiedrowiczce61fb92012-04-11 23:18:37 +02001761
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001762 $out .= esc_html(substr($str, $pos, $begin - $pos), %opts)
Michał Kiedrowiczce61fb92012-04-11 23:18:37 +02001763 if ($begin - $pos > 0);
1764 $out .= $cgi->span({-class => $css_class}, $escaped);
1765
1766 $pos = $end;
Jakub Narebski337da8d2012-02-27 02:55:19 +01001767 }
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001768 $out .= esc_html(substr($str, $pos), %opts)
Jakub Narebski337da8d2012-02-27 02:55:19 +01001769 if ($pos < length($str));
1770
1771 return $out;
1772}
1773
Jakub Narebskie607b792012-02-27 02:55:22 +01001774# return positions of beginning and end of each match
1775sub matchpos_list {
Jakub Narebski337da8d2012-02-27 02:55:19 +01001776 my ($str, $regexp) = @_;
Jakub Narebskie607b792012-02-27 02:55:22 +01001777 return unless (defined $str && defined $regexp);
Jakub Narebski337da8d2012-02-27 02:55:19 +01001778
1779 my @matches;
1780 while ($str =~ /$regexp/g) {
1781 push @matches, [$-[0], $+[0]];
1782 }
Jakub Narebskie607b792012-02-27 02:55:22 +01001783 return @matches;
1784}
1785
1786# highlight match (if any), and escape HTML
1787sub esc_html_match_hl {
1788 my ($str, $regexp) = @_;
1789 return esc_html($str) unless defined $regexp;
1790
1791 my @matches = matchpos_list($str, $regexp);
Jakub Narebski337da8d2012-02-27 02:55:19 +01001792 return esc_html($str) unless @matches;
1793
1794 return esc_html_hl_regions($str, 'match', @matches);
1795}
1796
Jakub Narebskie607b792012-02-27 02:55:22 +01001797
1798# highlight match (if any) of shortened string, and escape HTML
1799sub esc_html_match_hl_chopped {
1800 my ($str, $chopped, $regexp) = @_;
1801 return esc_html_match_hl($str, $regexp) unless defined $chopped;
1802
1803 my @matches = matchpos_list($str, $regexp);
1804 return esc_html($chopped) unless @matches;
1805
1806 # filter matches so that we mark chopped string
1807 my $tail = "... "; # see chop_str
1808 unless ($chopped =~ s/\Q$tail\E$//) {
1809 $tail = '';
1810 }
1811 my $chop_len = length($chopped);
1812 my $tail_len = length($tail);
1813 my @filtered;
1814
1815 for my $m (@matches) {
1816 if ($m->[0] > $chop_len) {
1817 push @filtered, [ $chop_len, $chop_len + $tail_len ] if ($tail_len > 0);
1818 last;
1819 } elsif ($m->[1] > $chop_len) {
1820 push @filtered, [ $m->[0], $chop_len + $tail_len ];
1821 last;
1822 }
1823 push @filtered, $m;
1824 }
1825
1826 return esc_html_hl_regions($chopped . $tail, 'match', @filtered);
1827}
1828
Jakub Narebski717b8312006-07-31 21:22:15 +02001829## ----------------------------------------------------------------------
1830## functions returning short strings
1831
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00001832# CSS class for given age value (in seconds)
1833sub age_class {
1834 my $age = shift;
1835
Jakub Narebski785cdea2007-05-13 12:39:22 +02001836 if (!defined $age) {
1837 return "noage";
1838 } elsif ($age < 60*60*2) {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00001839 return "age0";
1840 } elsif ($age < 60*60*24*2) {
1841 return "age1";
1842 } else {
1843 return "age2";
1844 }
1845}
1846
Jakub Narebski717b8312006-07-31 21:22:15 +02001847# convert age in seconds to "nn units ago" string
1848sub age_string {
1849 my $age = shift;
1850 my $age_str;
1851
1852 if ($age > 60*60*24*365*2) {
1853 $age_str = (int $age/60/60/24/365);
1854 $age_str .= " years ago";
1855 } elsif ($age > 60*60*24*(365/12)*2) {
1856 $age_str = int $age/60/60/24/(365/12);
1857 $age_str .= " months ago";
1858 } elsif ($age > 60*60*24*7*2) {
1859 $age_str = int $age/60/60/24/7;
1860 $age_str .= " weeks ago";
1861 } elsif ($age > 60*60*24*2) {
1862 $age_str = int $age/60/60/24;
1863 $age_str .= " days ago";
1864 } elsif ($age > 60*60*2) {
1865 $age_str = int $age/60/60;
1866 $age_str .= " hours ago";
1867 } elsif ($age > 60*2) {
1868 $age_str = int $age/60;
1869 $age_str .= " min ago";
1870 } elsif ($age > 2) {
1871 $age_str = int $age;
1872 $age_str .= " sec ago";
1873 } else {
1874 $age_str .= " right now";
1875 }
1876 return $age_str;
1877}
1878
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001879use constant {
1880 S_IFINVALID => 0030000,
1881 S_IFGITLINK => 0160000,
1882};
1883
1884# submodule/subproject, a commit object reference
Jakub Narebski74fd8722009-05-07 19:11:29 +02001885sub S_ISGITLINK {
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001886 my $mode = shift;
1887
1888 return (($mode & S_IFMT) == S_IFGITLINK)
1889}
1890
Jakub Narebski717b8312006-07-31 21:22:15 +02001891# convert file mode in octal to symbolic file mode string
1892sub mode_str {
1893 my $mode = oct shift;
1894
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001895 if (S_ISGITLINK($mode)) {
1896 return 'm---------';
1897 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001898 return 'drwxr-xr-x';
1899 } elsif (S_ISLNK($mode)) {
1900 return 'lrwxrwxrwx';
1901 } elsif (S_ISREG($mode)) {
1902 # git cares only about the executable bit
1903 if ($mode & S_IXUSR) {
1904 return '-rwxr-xr-x';
1905 } else {
1906 return '-rw-r--r--';
1907 };
1908 } else {
1909 return '----------';
1910 }
1911}
1912
1913# convert file mode in octal to file type string
1914sub file_type {
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02001915 my $mode = shift;
1916
1917 if ($mode !~ m/^[0-7]+$/) {
1918 return $mode;
1919 } else {
1920 $mode = oct $mode;
1921 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001922
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001923 if (S_ISGITLINK($mode)) {
1924 return "submodule";
1925 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001926 return "directory";
1927 } elsif (S_ISLNK($mode)) {
1928 return "symlink";
1929 } elsif (S_ISREG($mode)) {
1930 return "file";
1931 } else {
1932 return "unknown";
1933 }
1934}
1935
Jakub Narebski744d0ac2006-11-08 17:59:41 +01001936# convert file mode in octal to file type description string
1937sub file_type_long {
1938 my $mode = shift;
1939
1940 if ($mode !~ m/^[0-7]+$/) {
1941 return $mode;
1942 } else {
1943 $mode = oct $mode;
1944 }
1945
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001946 if (S_ISGITLINK($mode)) {
1947 return "submodule";
1948 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski744d0ac2006-11-08 17:59:41 +01001949 return "directory";
1950 } elsif (S_ISLNK($mode)) {
1951 return "symlink";
1952 } elsif (S_ISREG($mode)) {
1953 if ($mode & S_IXUSR) {
1954 return "executable";
1955 } else {
1956 return "file";
1957 };
1958 } else {
1959 return "unknown";
1960 }
1961}
1962
1963
Jakub Narebski717b8312006-07-31 21:22:15 +02001964## ----------------------------------------------------------------------
1965## functions returning short HTML fragments, or transforming HTML fragments
Pavel Roskin3dff5372007-02-03 23:49:16 -05001966## which don't belong to other sections
Jakub Narebski717b8312006-07-31 21:22:15 +02001967
Junio C Hamano225932e2006-11-09 00:57:13 -08001968# format line of commit message.
Jakub Narebski717b8312006-07-31 21:22:15 +02001969sub format_log_line_html {
1970 my $line = shift;
1971
Junio C Hamano225932e2006-11-09 00:57:13 -08001972 $line = esc_html($line, -nbsp=>1);
Marcel M. Cary7d233de2009-02-17 19:00:43 -08001973 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1974 $cgi->a({-href => href(action=>"object", hash=>$1),
1975 -class => "text"}, $1);
1976 }eg;
1977
Jakub Narebski717b8312006-07-31 21:22:15 +02001978 return $line;
1979}
1980
1981# format marker of refs pointing to given object
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001982
1983# the destination action is chosen based on object type and current context:
1984# - for annotated tags, we choose the tag view unless it's the current view
1985# already, in which case we go to shortlog view
1986# - for other refs, we keep the current view if we're in history, shortlog or
1987# log view, and select shortlog otherwise
Jakub Narebski847e01f2006-08-14 02:05:47 +02001988sub format_ref_marker {
Jakub Narebski717b8312006-07-31 21:22:15 +02001989 my ($refs, $id) = @_;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001990 my $markers = '';
Jakub Narebski717b8312006-07-31 21:22:15 +02001991
1992 if (defined $refs->{$id}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001993 foreach my $ref (@{$refs->{$id}}) {
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001994 # this code exploits the fact that non-lightweight tags are the
1995 # only indirect objects, and that they are the only objects for which
1996 # we want to use tag instead of shortlog as action
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001997 my ($type, $name) = qw();
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001998 my $indirect = ($ref =~ s/\^\{\}$//);
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001999 # e.g. tags/v2.6.11 or heads/next
2000 if ($ref =~ m!^(.*?)s?/(.*)$!) {
2001 $type = $1;
2002 $name = $2;
2003 } else {
2004 $type = "ref";
2005 $name = $ref;
2006 }
2007
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002008 my $class = $type;
2009 $class .= " indirect" if $indirect;
2010
2011 my $dest_action = "shortlog";
2012
2013 if ($indirect) {
2014 $dest_action = "tag" unless $action eq "tag";
2015 } elsif ($action =~ /^(history|(short)?log)$/) {
2016 $dest_action = $action;
2017 }
2018
2019 my $dest = "";
2020 $dest .= "refs/" unless $ref =~ m!^refs/!;
2021 $dest .= $ref;
2022
2023 my $link = $cgi->a({
2024 -href => href(
2025 action=>$dest_action,
2026 hash=>$dest
2027 )}, $name);
2028
Jakub Narebski3017ed62010-12-15 00:34:01 +01002029 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002030 $link . "</span>";
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002031 }
2032 }
2033
2034 if ($markers) {
2035 return ' <span class="refs">'. $markers . '</span>';
Jakub Narebski717b8312006-07-31 21:22:15 +02002036 } else {
2037 return "";
2038 }
2039}
2040
Jakub Narebski17d07442006-08-14 02:08:27 +02002041# format, perhaps shortened and with markers, title line
2042sub format_subject_html {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002043 my ($long, $short, $href, $extra) = @_;
Jakub Narebski17d07442006-08-14 02:08:27 +02002044 $extra = '' unless defined($extra);
2045
2046 if (length($short) < length($long)) {
Jakub Narebski14afe772009-05-22 17:35:46 +02002047 $long =~ s/[[:cntrl:]]/?/g;
Jakub Narebski7c278012006-08-22 12:02:48 +02002048 return $cgi->a({-href => $href, -class => "list subject",
Martin Koegler00f429a2007-06-03 17:42:44 +02002049 -title => to_utf8($long)},
Giuseppe Bilotta01b89f02009-08-23 10:28:09 +02002050 esc_html($short)) . $extra;
Jakub Narebski17d07442006-08-14 02:08:27 +02002051 } else {
Jakub Narebski7c278012006-08-22 12:02:48 +02002052 return $cgi->a({-href => $href, -class => "list subject"},
Giuseppe Bilotta01b89f02009-08-23 10:28:09 +02002053 esc_html($long)) . $extra;
Jakub Narebski17d07442006-08-14 02:08:27 +02002054 }
2055}
2056
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002057# Rather than recomputing the url for an email multiple times, we cache it
2058# after the first hit. This gives a visible benefit in views where the avatar
2059# for the same email is used repeatedly (e.g. shortlog).
2060# The cache is shared by all avatar engines (currently gravatar only), which
2061# are free to use it as preferred. Since only one avatar engine is used for any
2062# given page, there's no risk for cache conflicts.
2063our %avatar_cache = ();
2064
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002065# Compute the picon url for a given email, by using the picon search service over at
2066# http://www.cs.indiana.edu/picons/search.html
2067sub picon_url {
2068 my $email = lc shift;
2069 if (!$avatar_cache{$email}) {
2070 my ($user, $domain) = split('@', $email);
2071 $avatar_cache{$email} =
2072 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
2073 "$domain/$user/" .
2074 "users+domains+unknown/up/single";
2075 }
2076 return $avatar_cache{$email};
2077}
2078
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002079# Compute the gravatar url for a given email, if it's not in the cache already.
2080# Gravatar stores only the part of the URL before the size, since that's the
2081# one computationally more expensive. This also allows reuse of the cache for
2082# different sizes (for this particular engine).
2083sub gravatar_url {
2084 my $email = lc shift;
2085 my $size = shift;
2086 $avatar_cache{$email} ||=
2087 "http://www.gravatar.com/avatar/" .
2088 Digest::MD5::md5_hex($email) . "?s=";
2089 return $avatar_cache{$email} . $size;
2090}
2091
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002092# Insert an avatar for the given $email at the given $size if the feature
2093# is enabled.
2094sub git_get_avatar {
2095 my ($email, %opts) = @_;
2096 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
2097 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
2098 $opts{-size} ||= 'default';
2099 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
2100 my $url = "";
2101 if ($git_avatar eq 'gravatar') {
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002102 $url = gravatar_url($email, $size);
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002103 } elsif ($git_avatar eq 'picon') {
2104 $url = picon_url($email);
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002105 }
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002106 # Other providers can be added by extending the if chain, defining $url
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002107 # as needed. If no variant puts something in $url, we assume avatars
2108 # are completely disabled/unavailable.
2109 if ($url) {
2110 return $pre_white .
2111 "<img width=\"$size\" " .
2112 "class=\"avatar\" " .
Jakub Narebski3017ed62010-12-15 00:34:01 +01002113 "src=\"".esc_url($url)."\" " .
Giuseppe Bilotta7d25ef42009-06-30 00:00:54 +02002114 "alt=\"\" " .
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002115 "/>" . $post_white;
2116 } else {
2117 return "";
2118 }
2119}
2120
Stephen Boyde133d652009-10-15 21:14:59 -07002121sub format_search_author {
2122 my ($author, $searchtype, $displaytext) = @_;
2123 my $have_search = gitweb_check_feature('search');
2124
2125 if ($have_search) {
2126 my $performed = "";
2127 if ($searchtype eq 'author') {
2128 $performed = "authored";
2129 } elsif ($searchtype eq 'committer') {
2130 $performed = "committed";
2131 }
2132
2133 return $cgi->a({-href => href(action=>"search", hash=>$hash,
2134 searchtext=>$author,
2135 searchtype=>$searchtype), class=>"list",
2136 title=>"Search for commits $performed by $author"},
2137 $displaytext);
2138
2139 } else {
2140 return $displaytext;
2141 }
2142}
2143
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02002144# format the author name of the given commit with the given tag
2145# the author name is chopped and escaped according to the other
2146# optional parameters (see chop_str).
2147sub format_author_html {
2148 my $tag = shift;
2149 my $co = shift;
2150 my $author = chop_and_escape_str($co->{'author_name'}, @_);
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002151 return "<$tag class=\"author\">" .
Stephen Boyde133d652009-10-15 21:14:59 -07002152 format_search_author($co->{'author_name'}, "author",
2153 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2154 $author) .
2155 "</$tag>";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02002156}
2157
Jakub Narebski90921742007-06-08 13:27:42 +02002158# format git diff header line, i.e. "diff --(git|combined|cc) ..."
2159sub format_git_diff_header_line {
2160 my $line = shift;
2161 my $diffinfo = shift;
2162 my ($from, $to) = @_;
2163
2164 if ($diffinfo->{'nparents'}) {
2165 # combined diff
2166 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2167 if ($to->{'href'}) {
2168 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2169 esc_path($to->{'file'}));
2170 } else { # file was deleted (no href)
2171 $line .= esc_path($to->{'file'});
2172 }
2173 } else {
2174 # "ordinary" diff
2175 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2176 if ($from->{'href'}) {
2177 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2178 'a/' . esc_path($from->{'file'}));
2179 } else { # file was added (no href)
2180 $line .= 'a/' . esc_path($from->{'file'});
2181 }
2182 $line .= ' ';
2183 if ($to->{'href'}) {
2184 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2185 'b/' . esc_path($to->{'file'}));
2186 } else { # file was deleted
2187 $line .= 'b/' . esc_path($to->{'file'});
2188 }
2189 }
2190
2191 return "<div class=\"diff header\">$line</div>\n";
2192}
2193
2194# format extended diff header line, before patch itself
2195sub format_extended_diff_header_line {
2196 my $line = shift;
2197 my $diffinfo = shift;
2198 my ($from, $to) = @_;
2199
2200 # match <path>
2201 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2202 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2203 esc_path($from->{'file'}));
2204 }
2205 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2206 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2207 esc_path($to->{'file'}));
2208 }
2209 # match single <mode>
2210 if ($line =~ m/\s(\d{6})$/) {
2211 $line .= '<span class="info"> (' .
2212 file_type_long($1) .
2213 ')</span>';
2214 }
2215 # match <hash>
2216 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2217 # can match only for combined diff
2218 $line = 'index ';
2219 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2220 if ($from->{'href'}[$i]) {
2221 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2222 -class=>"hash"},
2223 substr($diffinfo->{'from_id'}[$i],0,7));
2224 } else {
2225 $line .= '0' x 7;
2226 }
2227 # separator
2228 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2229 }
2230 $line .= '..';
2231 if ($to->{'href'}) {
2232 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2233 substr($diffinfo->{'to_id'},0,7));
2234 } else {
2235 $line .= '0' x 7;
2236 }
2237
2238 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2239 # can match only for ordinary diff
2240 my ($from_link, $to_link);
2241 if ($from->{'href'}) {
2242 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2243 substr($diffinfo->{'from_id'},0,7));
2244 } else {
2245 $from_link = '0' x 7;
2246 }
2247 if ($to->{'href'}) {
2248 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2249 substr($diffinfo->{'to_id'},0,7));
2250 } else {
2251 $to_link = '0' x 7;
2252 }
2253 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2254 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2255 }
2256
2257 return $line . "<br/>\n";
2258}
2259
2260# format from-file/to-file diff header
2261sub format_diff_from_to_header {
Jakub Narebski91af4ce2007-06-08 13:32:44 +02002262 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
Jakub Narebski90921742007-06-08 13:27:42 +02002263 my $line;
2264 my $result = '';
2265
2266 $line = $from_line;
2267 #assert($line =~ m/^---/) if DEBUG;
Jakub Narebskideaa01a2007-06-08 13:29:49 +02002268 # no extra formatting for "^--- /dev/null"
2269 if (! $diffinfo->{'nparents'}) {
2270 # ordinary (single parent) diff
2271 if ($line =~ m!^--- "?a/!) {
2272 if ($from->{'href'}) {
2273 $line = '--- a/' .
2274 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2275 esc_path($from->{'file'}));
2276 } else {
2277 $line = '--- a/' .
2278 esc_path($from->{'file'});
2279 }
2280 }
2281 $result .= qq!<div class="diff from_file">$line</div>\n!;
2282
2283 } else {
2284 # combined diff (merge commit)
2285 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2286 if ($from->{'href'}[$i]) {
2287 $line = '--- ' .
Jakub Narebski91af4ce2007-06-08 13:32:44 +02002288 $cgi->a({-href=>href(action=>"blobdiff",
2289 hash_parent=>$diffinfo->{'from_id'}[$i],
2290 hash_parent_base=>$parents[$i],
2291 file_parent=>$from->{'file'}[$i],
2292 hash=>$diffinfo->{'to_id'},
2293 hash_base=>$hash,
2294 file_name=>$to->{'file'}),
2295 -class=>"path",
2296 -title=>"diff" . ($i+1)},
2297 $i+1) .
2298 '/' .
Jakub Narebskideaa01a2007-06-08 13:29:49 +02002299 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2300 esc_path($from->{'file'}[$i]));
2301 } else {
2302 $line = '--- /dev/null';
2303 }
2304 $result .= qq!<div class="diff from_file">$line</div>\n!;
Jakub Narebski90921742007-06-08 13:27:42 +02002305 }
2306 }
Jakub Narebski90921742007-06-08 13:27:42 +02002307
2308 $line = $to_line;
2309 #assert($line =~ m/^\+\+\+/) if DEBUG;
2310 # no extra formatting for "^+++ /dev/null"
2311 if ($line =~ m!^\+\+\+ "?b/!) {
2312 if ($to->{'href'}) {
2313 $line = '+++ b/' .
2314 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2315 esc_path($to->{'file'}));
2316 } else {
2317 $line = '+++ b/' .
2318 esc_path($to->{'file'});
2319 }
2320 }
2321 $result .= qq!<div class="diff to_file">$line</div>\n!;
2322
2323 return $result;
2324}
2325
Jakub Narebskicd030c32007-06-08 13:33:28 +02002326# create note for patch simplified by combined diff
2327sub format_diff_cc_simplified {
2328 my ($diffinfo, @parents) = @_;
2329 my $result = '';
2330
2331 $result .= "<div class=\"diff header\">" .
2332 "diff --cc ";
2333 if (!is_deleted($diffinfo)) {
2334 $result .= $cgi->a({-href => href(action=>"blob",
2335 hash_base=>$hash,
2336 hash=>$diffinfo->{'to_id'},
2337 file_name=>$diffinfo->{'to_file'}),
2338 -class => "path"},
2339 esc_path($diffinfo->{'to_file'}));
2340 } else {
2341 $result .= esc_path($diffinfo->{'to_file'});
2342 }
2343 $result .= "</div>\n" . # class="diff header"
2344 "<div class=\"diff nodifferences\">" .
2345 "Simple merge" .
2346 "</div>\n"; # class="diff nodifferences"
2347
2348 return $result;
2349}
2350
Jakub Narebski20a864c2011-10-31 00:36:20 +01002351sub diff_line_class {
2352 my ($line, $from, $to) = @_;
2353
2354 # ordinary diff
2355 my $num_sign = 1;
2356 # combined diff
2357 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2358 $num_sign = scalar @{$from->{'href'}};
2359 }
2360
2361 my @diff_line_classifier = (
2362 { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
2363 { regexp => qr/^\\/, class => "incomplete" },
2364 { regexp => qr/^ {$num_sign}/, class => "ctx" },
2365 # classifier for context must come before classifier add/rem,
2366 # or we would have to use more complicated regexp, for example
2367 # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
2368 { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
2369 { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
2370 );
2371 for my $clsfy (@diff_line_classifier) {
2372 return $clsfy->{'class'}
2373 if ($line =~ $clsfy->{'regexp'});
2374 }
2375
2376 # fallback
2377 return "";
2378}
2379
Jakub Narebskif1310cf2011-10-31 00:36:21 +01002380# assumes that $from and $to are defined and correctly filled,
2381# and that $line holds a line of chunk header for unified diff
2382sub format_unidiff_chunk_header {
2383 my ($line, $from, $to) = @_;
2384
2385 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2386 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2387
2388 $from_lines = 0 unless defined $from_lines;
2389 $to_lines = 0 unless defined $to_lines;
2390
2391 if ($from->{'href'}) {
2392 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2393 -class=>"list"}, $from_text);
2394 }
2395 if ($to->{'href'}) {
2396 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2397 -class=>"list"}, $to_text);
2398 }
2399 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2400 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2401 return $line;
2402}
2403
2404# assumes that $from and $to are defined and correctly filled,
2405# and that $line holds a line of chunk header for combined diff
2406sub format_cc_diff_chunk_header {
2407 my ($line, $from, $to) = @_;
2408
2409 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2410 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2411
2412 @from_text = split(' ', $ranges);
2413 for (my $i = 0; $i < @from_text; ++$i) {
2414 ($from_start[$i], $from_nlines[$i]) =
2415 (split(',', substr($from_text[$i], 1)), 0);
2416 }
2417
2418 $to_text = pop @from_text;
2419 $to_start = pop @from_start;
2420 $to_nlines = pop @from_nlines;
2421
2422 $line = "<span class=\"chunk_info\">$prefix ";
2423 for (my $i = 0; $i < @from_text; ++$i) {
2424 if ($from->{'href'}[$i]) {
2425 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2426 -class=>"list"}, $from_text[$i]);
2427 } else {
2428 $line .= $from_text[$i];
2429 }
2430 $line .= " ";
2431 }
2432 if ($to->{'href'}) {
2433 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2434 -class=>"list"}, $to_text);
2435 } else {
2436 $line .= $to_text;
2437 }
2438 $line .= " $prefix</span>" .
2439 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2440 return $line;
2441}
2442
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01002443# process patch (diff) line (not to be used for diff headers),
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002444# returning HTML-formatted (but not wrapped) line.
2445# If the line is passed as a reference, it is treated as HTML and not
2446# esc_html()'ed.
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002447sub format_diff_line {
2448 my ($line, $diff_class, $from, $to) = @_;
Jakub Narebski20a864c2011-10-31 00:36:20 +01002449
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002450 if (ref($line)) {
2451 $line = $$line;
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002452 } else {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002453 chomp $line;
2454 $line = untabify($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02002455
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002456 if ($from && $to && $line =~ m/^\@{2} /) {
2457 $line = format_unidiff_chunk_header($line, $from, $to);
2458 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2459 $line = format_cc_diff_chunk_header($line, $from, $to);
2460 } else {
2461 $line = esc_html($line, -nbsp=>1);
2462 }
Jakub Narebski59e3b142006-11-18 23:35:40 +01002463 }
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002464
2465 my $diff_classes = "diff";
2466 $diff_classes .= " $diff_class" if ($diff_class);
2467 $line = "<div class=\"$diff_classes\">$line</div>\n";
2468
2469 return $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02002470}
2471
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002472# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2473# linked. Pass the hash of the tree/commit to snapshot.
2474sub format_snapshot_links {
2475 my ($hash) = @_;
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002476 my $num_fmts = @snapshot_fmts;
2477 if ($num_fmts > 1) {
2478 # A parenthesized list of links bearing format names.
Jakub Narebskia7817852007-07-22 23:41:20 +02002479 # e.g. "snapshot (_tar.gz_ _zip_)"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002480 return "snapshot (" . join(' ', map
2481 $cgi->a({
2482 -href => href(
2483 action=>"snapshot",
2484 hash=>$hash,
2485 snapshot_format=>$_
2486 )
2487 }, $known_snapshot_formats{$_}{'display'})
2488 , @snapshot_fmts) . ")";
2489 } elsif ($num_fmts == 1) {
2490 # A single "snapshot" link whose tooltip bears the format name.
Jakub Narebskia7817852007-07-22 23:41:20 +02002491 # i.e. "_snapshot_"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002492 my ($fmt) = @snapshot_fmts;
Jakub Narebskia7817852007-07-22 23:41:20 +02002493 return
2494 $cgi->a({
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002495 -href => href(
2496 action=>"snapshot",
2497 hash=>$hash,
2498 snapshot_format=>$fmt
2499 ),
2500 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2501 }, "snapshot");
2502 } else { # $num_fmts == 0
2503 return undef;
2504 }
2505}
2506
Jakub Narebski35621982008-04-20 22:09:48 +02002507## ......................................................................
2508## functions returning values to be passed, perhaps after some
2509## transformation, to other functions; e.g. returning arguments to href()
2510
2511# returns hash to be passed to href to generate gitweb URL
2512# in -title key it returns description of link
2513sub get_feed_info {
2514 my $format = shift || 'Atom';
2515 my %res = (action => lc($format));
2516
2517 # feed links are possible only for project views
2518 return unless (defined $project);
2519 # some views should link to OPML, or to generic project feed,
2520 # or don't have specific feed yet (so they should use generic)
Jakub Narebski18ab83e2012-01-07 11:47:38 +01002521 return if (!$action || $action =~ /^(?:tags|heads|forks|tag|search)$/x);
Jakub Narebski35621982008-04-20 22:09:48 +02002522
2523 my $branch;
2524 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2525 # from tag links; this also makes possible to detect branch links
2526 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2527 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2528 $branch = $1;
2529 }
2530 # find log type for feed description (title)
2531 my $type = 'log';
2532 if (defined $file_name) {
2533 $type = "history of $file_name";
2534 $type .= "/" if ($action eq 'tree');
2535 $type .= " on '$branch'" if (defined $branch);
2536 } else {
2537 $type = "log of $branch" if (defined $branch);
2538 }
2539
2540 $res{-title} = $type;
2541 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2542 $res{'file_name'} = $file_name;
2543
2544 return %res;
2545}
2546
Jakub Narebski717b8312006-07-31 21:22:15 +02002547## ----------------------------------------------------------------------
2548## git utility subroutines, invoking git commands
2549
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002550# returns path to the core git executable and the --git-dir parameter as list
2551sub git_cmd {
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02002552 $number_of_git_cmds++;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002553 return $GIT, '--git-dir='.$git_dir;
2554}
2555
Lea Wiemann516381d2008-06-17 23:46:35 +02002556# quote the given arguments for passing them to the shell
2557# quote_command("command", "arg 1", "arg with ' and ! characters")
2558# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2559# Try to avoid using this function wherever possible.
2560sub quote_command {
2561 return join(' ',
Jakub Narebski68cedb12009-05-10 02:40:37 +02002562 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002563}
2564
Jakub Narebski717b8312006-07-31 21:22:15 +02002565# get HEAD ref of given project as hash
Jakub Narebski847e01f2006-08-14 02:05:47 +02002566sub git_get_head_hash {
Mark Radab6292752009-11-07 16:13:29 +01002567 return git_get_full_hash(shift, 'HEAD');
2568}
2569
2570sub git_get_full_hash {
2571 return git_get_hash(@_);
2572}
2573
2574sub git_get_short_hash {
2575 return git_get_hash(@_, '--short=7');
2576}
2577
2578sub git_get_hash {
2579 my ($project, $hash, @options) = @_;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002580 my $o_git_dir = $git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02002581 my $retval = undef;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002582 $git_dir = "$projectroot/$project";
Mark Radab6292752009-11-07 16:13:29 +01002583 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2584 '--verify', '-q', @options, $hash) {
2585 $retval = <$fd>;
2586 chomp $retval if defined $retval;
Jakub Narebski717b8312006-07-31 21:22:15 +02002587 close $fd;
Jakub Narebski717b8312006-07-31 21:22:15 +02002588 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002589 if (defined $o_git_dir) {
2590 $git_dir = $o_git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02002591 }
2592 return $retval;
2593}
2594
2595# get type of given object
2596sub git_get_type {
2597 my $hash = shift;
2598
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002599 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02002600 my $type = <$fd>;
2601 close $fd or return;
2602 chomp $type;
2603 return $type;
2604}
2605
Jakub Narebskib2019272007-11-03 00:41:19 +01002606# repository configuration
2607our $config_file = '';
2608our %config;
2609
2610# store multiple values for single key as anonymous array reference
2611# single values stored directly in the hash, not as [ <value> ]
2612sub hash_set_multi {
2613 my ($hash, $key, $value) = @_;
2614
2615 if (!exists $hash->{$key}) {
2616 $hash->{$key} = $value;
2617 } elsif (!ref $hash->{$key}) {
2618 $hash->{$key} = [ $hash->{$key}, $value ];
2619 } else {
2620 push @{$hash->{$key}}, $value;
2621 }
2622}
2623
2624# return hash of git project configuration
2625# optionally limited to some section, e.g. 'gitweb'
2626sub git_parse_project_config {
2627 my $section_regexp = shift;
2628 my %config;
2629
2630 local $/ = "\0";
2631
2632 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2633 or return;
2634
2635 while (my $keyval = <$fh>) {
2636 chomp $keyval;
2637 my ($key, $value) = split(/\n/, $keyval, 2);
2638
2639 hash_set_multi(\%config, $key, $value)
2640 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2641 }
2642 close $fh;
2643
2644 return %config;
2645}
2646
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002647# convert config value to boolean: 'true' or 'false'
Jakub Narebskib2019272007-11-03 00:41:19 +01002648# no value, number > 0, 'true' and 'yes' values are true
2649# rest of values are treated as false (never as error)
2650sub config_to_bool {
2651 my $val = shift;
2652
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002653 return 1 if !defined $val; # section.key
2654
Jakub Narebskib2019272007-11-03 00:41:19 +01002655 # strip leading and trailing whitespace
2656 $val =~ s/^\s+//;
2657 $val =~ s/\s+$//;
2658
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002659 return (($val =~ /^\d+$/ && $val) || # section.key = 1
Jakub Narebskib2019272007-11-03 00:41:19 +01002660 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2661}
2662
2663# convert config value to simple decimal number
2664# an optional value suffix of 'k', 'm', or 'g' will cause the value
2665# to be multiplied by 1024, 1048576, or 1073741824
2666sub config_to_int {
2667 my $val = shift;
2668
2669 # strip leading and trailing whitespace
2670 $val =~ s/^\s+//;
2671 $val =~ s/\s+$//;
2672
2673 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2674 $unit = lc($unit);
2675 # unknown unit is treated as 1
2676 return $num * ($unit eq 'g' ? 1073741824 :
2677 $unit eq 'm' ? 1048576 :
2678 $unit eq 'k' ? 1024 : 1);
2679 }
2680 return $val;
2681}
2682
2683# convert config value to array reference, if needed
2684sub config_to_multi {
2685 my $val = shift;
2686
Jakub Narebskid76a5852007-12-20 10:48:09 +01002687 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
Jakub Narebskib2019272007-11-03 00:41:19 +01002688}
2689
Jakub Narebski717b8312006-07-31 21:22:15 +02002690sub git_get_project_config {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302691 my ($key, $type) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02002692
Jakub Narebski7a49c252010-03-27 20:26:59 +01002693 return unless defined $git_dir;
Jakub Narebski9be36142010-03-01 22:51:34 +01002694
Jakub Narebskib2019272007-11-03 00:41:19 +01002695 # key sanity check
Jakub Narebski717b8312006-07-31 21:22:15 +02002696 return unless ($key);
Jakub Narebski14569cd2011-07-28 23:38:03 +02002697 # only subsection, if exists, is case sensitive,
2698 # and not lowercased by 'git config -z -l'
2699 if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
2700 $key = join(".", lc($hi), $mi, lc($lo));
2701 } else {
2702 $key = lc($key);
2703 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002704 $key =~ s/^gitweb\.//;
2705 return if ($key =~ m/\W/);
2706
Jakub Narebskib2019272007-11-03 00:41:19 +01002707 # type sanity check
2708 if (defined $type) {
2709 $type =~ s/^--//;
2710 $type = undef
2711 unless ($type eq 'bool' || $type eq 'int');
2712 }
2713
2714 # get config
2715 if (!defined $config_file ||
2716 $config_file ne "$git_dir/config") {
2717 %config = git_parse_project_config('gitweb');
2718 $config_file = "$git_dir/config";
2719 }
2720
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002721 # check if config variable (key) exists
2722 return unless exists $config{"gitweb.$key"};
2723
Jakub Narebskib2019272007-11-03 00:41:19 +01002724 # ensure given type
2725 if (!defined $type) {
2726 return $config{"gitweb.$key"};
2727 } elsif ($type eq 'bool') {
2728 # backward compatibility: 'git config --bool' returns true/false
2729 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2730 } elsif ($type eq 'int') {
2731 return config_to_int($config{"gitweb.$key"});
2732 }
2733 return $config{"gitweb.$key"};
Jakub Narebski717b8312006-07-31 21:22:15 +02002734}
2735
Jakub Narebski717b8312006-07-31 21:22:15 +02002736# get hash of given path at given ref
2737sub git_get_hash_by_path {
2738 my $base = shift;
2739 my $path = shift || return undef;
Jakub Narebski1d782b02006-09-21 18:09:12 +02002740 my $type = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02002741
Jakub Narebski4b02f482006-09-26 01:54:24 +02002742 $path =~ s,/+$,,;
Jakub Narebski717b8312006-07-31 21:22:15 +02002743
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002744 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
Lea Wiemann074afaa2008-06-19 22:03:21 +02002745 or die_error(500, "Open git-ls-tree failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02002746 my $line = <$fd>;
2747 close $fd or return undef;
2748
Jakub Narebski198a2a82007-05-12 21:16:34 +02002749 if (!defined $line) {
2750 # there is no tree or hash given by $path at $base
2751 return undef;
2752 }
2753
Jakub Narebski717b8312006-07-31 21:22:15 +02002754 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01002755 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
Jakub Narebski1d782b02006-09-21 18:09:12 +02002756 if (defined $type && $type ne $2) {
2757 # type doesn't match
2758 return undef;
2759 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002760 return $3;
2761}
2762
Jakub Narebskied224de2007-05-07 01:10:04 +02002763# get path of entry with given hash at given tree-ish (ref)
2764# used to get 'from' filename for combined diff (merge commit) for renames
2765sub git_get_path_by_hash {
2766 my $base = shift || return;
2767 my $hash = shift || return;
2768
2769 local $/ = "\0";
2770
2771 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2772 or return undef;
2773 while (my $line = <$fd>) {
2774 chomp $line;
2775
2776 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2777 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2778 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2779 close $fd;
2780 return $1;
2781 }
2782 }
2783 close $fd;
2784 return undef;
2785}
2786
Jakub Narebski717b8312006-07-31 21:22:15 +02002787## ......................................................................
2788## git utility functions, directly accessing git repository
2789
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002790# get the value of config variable either from file named as the variable
2791# itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2792# configuration variable in the repository config file.
2793sub git_get_file_or_project_config {
2794 my ($path, $name) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02002795
Jakub Narebski0e121a22007-11-03 00:41:20 +01002796 $git_dir = "$projectroot/$path";
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002797 open my $fd, '<', "$git_dir/$name"
2798 or return git_get_project_config($name);
2799 my $conf = <$fd>;
Jakub Narebski717b8312006-07-31 21:22:15 +02002800 close $fd;
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002801 if (defined $conf) {
2802 chomp $conf;
Junio C Hamano2eb54ef2007-05-16 21:04:16 -07002803 }
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002804 return $conf;
Jakub Narebski717b8312006-07-31 21:22:15 +02002805}
2806
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002807sub git_get_project_description {
2808 my $path = shift;
2809 return git_get_file_or_project_config($path, 'description');
Jakub Narebski717b8312006-07-31 21:22:15 +02002810}
2811
Sebastien Ceveyd940c902011-04-29 19:52:01 +02002812sub git_get_project_category {
2813 my $path = shift;
2814 return git_get_file_or_project_config($path, 'category');
2815}
2816
2817
Jakub Narebski0368c492011-04-29 19:51:57 +02002818# supported formats:
2819# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2820# - if its contents is a number, use it as tag weight,
2821# - otherwise add a tag with weight 1
2822# * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2823# the same value multiple times increases tag weight
2824# * `gitweb.ctag' multi-valued repo config variable
Petr Baudisaed93de2008-10-02 17:13:02 +02002825sub git_get_project_ctags {
Jakub Narebski0368c492011-04-29 19:51:57 +02002826 my $project = shift;
Petr Baudisaed93de2008-10-02 17:13:02 +02002827 my $ctags = {};
2828
Jakub Narebski0368c492011-04-29 19:51:57 +02002829 $git_dir = "$projectroot/$project";
2830 if (opendir my $dh, "$git_dir/ctags") {
2831 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2832 foreach my $tagfile (@files) {
2833 open my $ct, '<', $tagfile
2834 or next;
2835 my $val = <$ct>;
2836 chomp $val if $val;
2837 close $ct;
2838
2839 (my $ctag = $tagfile) =~ s#.*/##;
Jonathan Nieder2c162b52011-06-09 02:08:57 -05002840 if ($val =~ /^\d+$/) {
Jakub Narebski0368c492011-04-29 19:51:57 +02002841 $ctags->{$ctag} = $val;
2842 } else {
2843 $ctags->{$ctag} = 1;
2844 }
2845 }
2846 closedir $dh;
2847
2848 } elsif (open my $fh, '<', "$git_dir/ctags") {
2849 while (my $line = <$fh>) {
2850 chomp $line;
2851 $ctags->{$line}++ if $line;
2852 }
2853 close $fh;
2854
2855 } else {
2856 my $taglist = config_to_multi(git_get_project_config('ctag'));
2857 foreach my $tag (@$taglist) {
2858 $ctags->{$tag}++;
2859 }
Petr Baudisaed93de2008-10-02 17:13:02 +02002860 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002861
2862 return $ctags;
2863}
2864
2865# return hash, where keys are content tags ('ctags'),
2866# and values are sum of weights of given tag in every project
2867sub git_gather_all_ctags {
2868 my $projects = shift;
2869 my $ctags = {};
2870
2871 foreach my $p (@$projects) {
2872 foreach my $ct (keys %{$p->{'ctags'}}) {
2873 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2874 }
2875 }
2876
2877 return $ctags;
Petr Baudisaed93de2008-10-02 17:13:02 +02002878}
2879
2880sub git_populate_project_tagcloud {
2881 my $ctags = shift;
2882
2883 # First, merge different-cased tags; tags vote on casing
2884 my %ctags_lc;
2885 foreach (keys %$ctags) {
2886 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2887 if (not $ctags_lc{lc $_}->{topcount}
2888 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2889 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2890 $ctags_lc{lc $_}->{topname} = $_;
2891 }
2892 }
2893
2894 my $cloud;
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01002895 my $matched = $input_params{'ctag'};
Petr Baudisaed93de2008-10-02 17:13:02 +02002896 if (eval { require HTML::TagCloud; 1; }) {
2897 $cloud = HTML::TagCloud->new;
Jakub Narebski0368c492011-04-29 19:51:57 +02002898 foreach my $ctag (sort keys %ctags_lc) {
Petr Baudisaed93de2008-10-02 17:13:02 +02002899 # Pad the title with spaces so that the cloud looks
2900 # less crammed.
Jakub Narebski0368c492011-04-29 19:51:57 +02002901 my $title = esc_html($ctags_lc{$ctag}->{topname});
Petr Baudisaed93de2008-10-02 17:13:02 +02002902 $title =~ s/ /&nbsp;/g;
2903 $title =~ s/^/&nbsp;/g;
2904 $title =~ s/$/&nbsp;/g;
Jakub Narebski4b9447f2011-04-29 19:51:58 +02002905 if (defined $matched && $matched eq $ctag) {
2906 $title = qq(<span class="match">$title</span>);
2907 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002908 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2909 $ctags_lc{$ctag}->{count});
Petr Baudisaed93de2008-10-02 17:13:02 +02002910 }
2911 } else {
Jakub Narebski0368c492011-04-29 19:51:57 +02002912 $cloud = {};
2913 foreach my $ctag (keys %ctags_lc) {
Jakub Narebski4b9447f2011-04-29 19:51:58 +02002914 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
2915 if (defined $matched && $matched eq $ctag) {
2916 $title = qq(<span class="match">$title</span>);
2917 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002918 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
2919 $cloud->{$ctag}{ctag} =
Jakub Narebski4b9447f2011-04-29 19:51:58 +02002920 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
Jakub Narebski0368c492011-04-29 19:51:57 +02002921 }
Petr Baudisaed93de2008-10-02 17:13:02 +02002922 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002923 return $cloud;
Petr Baudisaed93de2008-10-02 17:13:02 +02002924}
2925
2926sub git_show_project_tagcloud {
2927 my ($cloud, $count) = @_;
Petr Baudisaed93de2008-10-02 17:13:02 +02002928 if (ref $cloud eq 'HTML::TagCloud') {
2929 return $cloud->html_and_css($count);
2930 } else {
Jakub Narebski0368c492011-04-29 19:51:57 +02002931 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2932 return
2933 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2934 join (', ', map {
2935 $cloud->{$_}->{'ctag'}
2936 } splice(@tags, 0, $count)) .
2937 '</div>';
Petr Baudisaed93de2008-10-02 17:13:02 +02002938 }
2939}
2940
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002941sub git_get_project_url_list {
2942 my $path = shift;
2943
Jakub Narebski0e121a22007-11-03 00:41:20 +01002944 $git_dir = "$projectroot/$path";
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02002945 open my $fd, '<', "$git_dir/cloneurl"
Jakub Narebski0e121a22007-11-03 00:41:20 +01002946 or return wantarray ?
2947 @{ config_to_multi(git_get_project_config('url')) } :
2948 config_to_multi(git_get_project_config('url'));
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002949 my @git_project_url_list = map { chomp; $_ } <$fd>;
2950 close $fd;
2951
2952 return wantarray ? @git_project_url_list : \@git_project_url_list;
2953}
2954
Jakub Narebski847e01f2006-08-14 02:05:47 +02002955sub git_get_projects_list {
Jakub Narebski12b14432011-04-29 19:51:56 +02002956 my $filter = shift || '';
Bernhard R. Link348a6582012-01-30 21:06:38 +01002957 my $paranoid = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02002958 my @list;
2959
2960 if (-d $projects_list) {
2961 # search in directory
Jakub Narebski12b14432011-04-29 19:51:56 +02002962 my $dir = $projects_list;
Aneesh Kumar K.V6768d6b2006-11-03 10:41:45 +05302963 # remove the trailing "/"
2964 $dir =~ s!/+$!!;
Matthieu Moyac593b72012-01-04 11:07:45 +01002965 my $pfxlen = length("$dir");
2966 my $pfxdepth = ($dir =~ tr!/!!);
Jakub Narebski12b14432011-04-29 19:51:56 +02002967 # when filtering, search only given subdirectory
Bernhard R. Link348a6582012-01-30 21:06:38 +01002968 if ($filter && !$paranoid) {
Jakub Narebski12b14432011-04-29 19:51:56 +02002969 $dir .= "/$filter";
2970 $dir =~ s!/+$!!;
2971 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002972
2973 File::Find::find({
2974 follow_fast => 1, # follow symbolic links
Junio C Hamanod20602e2007-07-27 01:23:03 -07002975 follow_skip => 2, # ignore duplicates
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002976 dangling_symlinks => 0, # ignore dangling symlinks, silently
2977 wanted => sub {
Jakub Narebskiee1d8ee2010-04-30 18:30:31 +02002978 # global variables
2979 our $project_maxdepth;
2980 our $projectroot;
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002981 # skip project-list toplevel, if we get it.
2982 return if (m!^[/.]$!);
2983 # only directories can be git repositories
2984 return unless (-d $_);
Luke Luca5e9492007-10-16 20:45:25 -07002985 # don't traverse too deep (Find is super slow on os x)
Jakub Narebski12b14432011-04-29 19:51:56 +02002986 # $project_maxdepth excludes depth of $projectroot
Luke Luca5e9492007-10-16 20:45:25 -07002987 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2988 $File::Find::prune = 1;
2989 return;
2990 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002991
Jakub Narebski12b14432011-04-29 19:51:56 +02002992 my $path = substr($File::Find::name, $pfxlen + 1);
Bernhard R. Link348a6582012-01-30 21:06:38 +01002993 # paranoidly only filter here
2994 if ($paranoid && $filter && $path !~ m!^\Q$filter\E/!) {
2995 next;
2996 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002997 # we check related file in $projectroot
Devin Doucettefb3bb3d2008-12-27 02:39:31 -07002998 if (check_export_ok("$projectroot/$path")) {
2999 push @list, { path => $path };
Jakub Narebskic0011ff2006-09-14 22:18:59 +02003000 $File::Find::prune = 1;
3001 }
3002 },
3003 }, "$dir");
3004
Jakub Narebski717b8312006-07-31 21:22:15 +02003005 } elsif (-f $projects_list) {
3006 # read from file(url-encoded):
3007 # 'git%2Fgit.git Linus+Torvalds'
3008 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3009 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003010 open my $fd, '<', $projects_list or return;
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02003011 PROJECT:
Jakub Narebski717b8312006-07-31 21:22:15 +02003012 while (my $line = <$fd>) {
3013 chomp $line;
3014 my ($path, $owner) = split ' ', $line;
3015 $path = unescape($path);
3016 $owner = unescape($owner);
3017 if (!defined $path) {
3018 next;
3019 }
Jakub Narebski12b14432011-04-29 19:51:56 +02003020 # if $filter is rpovided, check if $path begins with $filter
3021 if ($filter && $path !~ m!^\Q$filter\E/!) {
3022 next;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003023 }
Junio C Hamano2172ce42006-10-03 02:30:47 -07003024 if (check_export_ok("$projectroot/$path")) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003025 my $pr = {
Kacper Kornet75e0dff2012-04-24 19:50:05 +02003026 path => $path
Jakub Narebski717b8312006-07-31 21:22:15 +02003027 };
Kacper Kornet75e0dff2012-04-24 19:50:05 +02003028 if ($owner) {
3029 $pr->{'owner'} = to_utf8($owner);
3030 }
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02003031 push @list, $pr;
Jakub Narebski717b8312006-07-31 21:22:15 +02003032 }
3033 }
3034 close $fd;
3035 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003036 return @list;
3037}
3038
Jakub Narebski12b14432011-04-29 19:51:56 +02003039# written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
3040# as side effects it sets 'forks' field to list of forks for forked projects
3041sub filter_forks_from_projects_list {
3042 my $projects = shift;
3043
3044 my %trie; # prefix tree of directories (path components)
3045 # generate trie out of those directories that might contain forks
3046 foreach my $pr (@$projects) {
3047 my $path = $pr->{'path'};
3048 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
3049 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
3050 next unless ($path); # skip '.git' repository: tests, git-instaweb
Julien Muchembled53c632f2011-10-21 21:04:21 +02003051 next unless (-d "$projectroot/$path"); # containing directory exists
Jakub Narebski12b14432011-04-29 19:51:56 +02003052 $pr->{'forks'} = []; # there can be 0 or more forks of project
3053
3054 # add to trie
3055 my @dirs = split('/', $path);
3056 # walk the trie, until either runs out of components or out of trie
3057 my $ref = \%trie;
3058 while (scalar @dirs &&
3059 exists($ref->{$dirs[0]})) {
3060 $ref = $ref->{shift @dirs};
3061 }
3062 # create rest of trie structure from rest of components
3063 foreach my $dir (@dirs) {
3064 $ref = $ref->{$dir} = {};
3065 }
3066 # create end marker, store $pr as a data
3067 $ref->{''} = $pr if (!exists $ref->{''});
3068 }
3069
3070 # filter out forks, by finding shortest prefix match for paths
3071 my @filtered;
3072 PROJECT:
3073 foreach my $pr (@$projects) {
3074 # trie lookup
3075 my $ref = \%trie;
3076 DIR:
3077 foreach my $dir (split('/', $pr->{'path'})) {
3078 if (exists $ref->{''}) {
3079 # found [shortest] prefix, is a fork - skip it
3080 push @{$ref->{''}{'forks'}}, $pr;
3081 next PROJECT;
3082 }
3083 if (!exists $ref->{$dir}) {
3084 # not in trie, cannot have prefix, not a fork
3085 push @filtered, $pr;
3086 next PROJECT;
3087 }
3088 # If the dir is there, we just walk one step down the trie.
3089 $ref = $ref->{$dir};
3090 }
3091 # we ran out of trie
3092 # (shouldn't happen: it's either no match, or end marker)
3093 push @filtered, $pr;
3094 }
3095
3096 return @filtered;
3097}
3098
3099# note: fill_project_list_info must be run first,
3100# for 'descr_long' and 'ctags' to be filled
3101sub search_projects_list {
3102 my ($projlist, %opts) = @_;
3103 my $tagfilter = $opts{'tagfilter'};
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003104 my $search_re = $opts{'search_regexp'};
Jakub Narebski12b14432011-04-29 19:51:56 +02003105
3106 return @$projlist
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003107 unless ($tagfilter || $search_re);
Jakub Narebski12b14432011-04-29 19:51:56 +02003108
Jakub Narebski07b257f2012-02-23 16:54:48 +01003109 # searching projects require filling to be run before it;
3110 fill_project_list_info($projlist,
3111 $tagfilter ? 'ctags' : (),
Junio C Hamanoaa145bf2012-03-08 13:04:49 -08003112 $search_re ? ('path', 'descr') : ());
Jakub Narebski12b14432011-04-29 19:51:56 +02003113 my @projects;
3114 PROJECT:
3115 foreach my $pr (@$projlist) {
3116
3117 if ($tagfilter) {
3118 next unless ref($pr->{'ctags'}) eq 'HASH';
3119 next unless
3120 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
3121 }
3122
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003123 if ($search_re) {
Jakub Narebski12b14432011-04-29 19:51:56 +02003124 next unless
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003125 $pr->{'path'} =~ /$search_re/ ||
3126 $pr->{'descr_long'} =~ /$search_re/;
Jakub Narebski12b14432011-04-29 19:51:56 +02003127 }
3128
3129 push @projects, $pr;
3130 }
3131
3132 return @projects;
3133}
3134
Junio C Hamano47852452007-07-03 22:10:42 -07003135our $gitweb_project_owner = undef;
3136sub git_get_project_list_from_file {
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003137
Junio C Hamano47852452007-07-03 22:10:42 -07003138 return if (defined $gitweb_project_owner);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003139
Junio C Hamano47852452007-07-03 22:10:42 -07003140 $gitweb_project_owner = {};
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003141 # read from file (url-encoded):
3142 # 'git%2Fgit.git Linus+Torvalds'
3143 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3144 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3145 if (-f $projects_list) {
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003146 open(my $fd, '<', $projects_list);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003147 while (my $line = <$fd>) {
3148 chomp $line;
3149 my ($pr, $ow) = split ' ', $line;
3150 $pr = unescape($pr);
3151 $ow = unescape($ow);
Junio C Hamano47852452007-07-03 22:10:42 -07003152 $gitweb_project_owner->{$pr} = to_utf8($ow);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003153 }
3154 close $fd;
3155 }
Junio C Hamano47852452007-07-03 22:10:42 -07003156}
3157
3158sub git_get_project_owner {
3159 my $project = shift;
3160 my $owner;
3161
3162 return undef unless $project;
Bruno Ribasb59012e2008-02-08 14:38:04 -02003163 $git_dir = "$projectroot/$project";
Junio C Hamano47852452007-07-03 22:10:42 -07003164
3165 if (!defined $gitweb_project_owner) {
3166 git_get_project_list_from_file();
3167 }
3168
3169 if (exists $gitweb_project_owner->{$project}) {
3170 $owner = $gitweb_project_owner->{$project};
3171 }
Bruno Ribasb59012e2008-02-08 14:38:04 -02003172 if (!defined $owner){
3173 $owner = git_get_project_config('owner');
3174 }
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003175 if (!defined $owner) {
Bruno Ribasb59012e2008-02-08 14:38:04 -02003176 $owner = get_file_owner("$git_dir");
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003177 }
3178
3179 return $owner;
3180}
3181
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003182sub git_get_last_activity {
3183 my ($path) = @_;
3184 my $fd;
3185
3186 $git_dir = "$projectroot/$path";
3187 open($fd, "-|", git_cmd(), 'for-each-ref',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00003188 '--format=%(committer)',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003189 '--sort=-committerdate',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00003190 '--count=1',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003191 'refs/heads') or return;
3192 my $most_recent = <$fd>;
3193 close $fd or return;
Jakub Narebski785cdea2007-05-13 12:39:22 +02003194 if (defined $most_recent &&
3195 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003196 my $timestamp = $1;
3197 my $age = time - $timestamp;
3198 return ($age, age_string($age));
3199 }
Matt McCutchenc9563952007-06-28 18:15:22 -04003200 return (undef, undef);
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003201}
3202
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01003203# Implementation note: when a single remote is wanted, we cannot use 'git
3204# remote show -n' because that command always work (assuming it's a remote URL
3205# if it's not defined), and we cannot use 'git remote show' because that would
3206# try to make a network roundtrip. So the only way to find if that particular
3207# remote is defined is to walk the list provided by 'git remote -v' and stop if
3208# and when we find what we want.
3209sub git_get_remotes_list {
3210 my $wanted = shift;
3211 my %remotes = ();
3212
3213 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3214 return unless $fd;
3215 while (my $remote = <$fd>) {
3216 chomp $remote;
3217 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3218 next if $wanted and not $remote eq $wanted;
3219 my ($url, $key) = ($1, $2);
3220
3221 $remotes{$remote} ||= { 'heads' => () };
3222 $remotes{$remote}{$key} = $url;
3223 }
3224 close $fd or return;
3225 return wantarray ? %remotes : \%remotes;
3226}
3227
3228# Takes a hash of remotes as first parameter and fills it by adding the
3229# available remote heads for each of the indicated remotes.
3230sub fill_remote_heads {
3231 my $remotes = shift;
3232 my @heads = map { "remotes/$_" } keys %$remotes;
3233 my @remoteheads = git_get_heads_list(undef, @heads);
3234 foreach my $remote (keys %$remotes) {
3235 $remotes->{$remote}{'heads'} = [ grep {
3236 $_->{'name'} =~ s!^$remote/!!
3237 } @remoteheads ];
3238 }
3239}
3240
Jakub Narebski847e01f2006-08-14 02:05:47 +02003241sub git_get_references {
Jakub Narebski717b8312006-07-31 21:22:15 +02003242 my $type = shift || "";
3243 my %refs;
Jakub Narebski28b9d9f2006-11-25 11:32:08 +01003244 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3245 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3246 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3247 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
Jakub Narebski9704d752006-09-19 14:31:49 +02003248 or return;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003249
Jakub Narebski717b8312006-07-31 21:22:15 +02003250 while (my $line = <$fd>) {
3251 chomp $line;
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02003252 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003253 if (defined $refs{$1}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003254 push @{$refs{$1}}, $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02003255 } else {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003256 $refs{$1} = [ $2 ];
Jakub Narebski717b8312006-07-31 21:22:15 +02003257 }
3258 }
3259 }
3260 close $fd or return;
3261 return \%refs;
3262}
3263
Jakub Narebski56a322f2006-08-24 19:41:23 +02003264sub git_get_rev_name_tags {
3265 my $hash = shift || return undef;
3266
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003267 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
Jakub Narebski56a322f2006-08-24 19:41:23 +02003268 or return;
3269 my $name_rev = <$fd>;
3270 close $fd;
3271
3272 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3273 return $1;
3274 } else {
3275 # catches also '$hash undefined' output
3276 return undef;
3277 }
3278}
3279
Jakub Narebski717b8312006-07-31 21:22:15 +02003280## ----------------------------------------------------------------------
3281## parse to hash functions
3282
Jakub Narebski847e01f2006-08-14 02:05:47 +02003283sub parse_date {
Jakub Narebski717b8312006-07-31 21:22:15 +02003284 my $epoch = shift;
3285 my $tz = shift || "-0000";
3286
3287 my %date;
3288 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3289 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3290 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3291 $date{'hour'} = $hour;
3292 $date{'minute'} = $min;
3293 $date{'mday'} = $mday;
3294 $date{'day'} = $days[$wday];
3295 $date{'month'} = $months[$mon];
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003296 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3297 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
Jakub Narebski952c65f2006-08-22 16:52:50 +02003298 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3299 $mday, $months[$mon], $hour ,$min;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003300 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
Vincent Zanottia62d6d82007-11-10 19:55:27 +01003301 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
Jakub Narebski717b8312006-07-31 21:22:15 +02003302
Jakub Narebski2b1e1722011-03-25 20:20:49 +01003303 my ($tz_sign, $tz_hour, $tz_min) =
3304 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3305 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3306 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
Jakub Narebski717b8312006-07-31 21:22:15 +02003307 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3308 $date{'hour_local'} = $hour;
3309 $date{'minute_local'} = $min;
3310 $date{'tz_local'} = $tz;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003311 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3312 1900+$year, $mon+1, $mday,
3313 $hour, $min, $sec, $tz);
Jakub Narebski717b8312006-07-31 21:22:15 +02003314 return %date;
3315}
3316
Jakub Narebski847e01f2006-08-14 02:05:47 +02003317sub parse_tag {
Jakub Narebski717b8312006-07-31 21:22:15 +02003318 my $tag_id = shift;
3319 my %tag;
3320 my @comment;
3321
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003322 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02003323 $tag{'id'} = $tag_id;
3324 while (my $line = <$fd>) {
3325 chomp $line;
3326 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3327 $tag{'object'} = $1;
3328 } elsif ($line =~ m/^type (.+)$/) {
3329 $tag{'type'} = $1;
3330 } elsif ($line =~ m/^tag (.+)$/) {
3331 $tag{'name'} = $1;
3332 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3333 $tag{'author'} = $1;
Giuseppe Bilottaba924732009-06-30 00:00:50 +02003334 $tag{'author_epoch'} = $2;
3335 $tag{'author_tz'} = $3;
3336 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3337 $tag{'author_name'} = $1;
3338 $tag{'author_email'} = $2;
3339 } else {
3340 $tag{'author_name'} = $tag{'author'};
3341 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003342 } elsif ($line =~ m/--BEGIN/) {
3343 push @comment, $line;
3344 last;
3345 } elsif ($line eq "") {
3346 last;
3347 }
3348 }
3349 push @comment, <$fd>;
3350 $tag{'comment'} = \@comment;
3351 close $fd or return;
3352 if (!defined $tag{'name'}) {
3353 return
3354 };
3355 return %tag
3356}
3357
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003358sub parse_commit_text {
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003359 my ($commit_text, $withparents) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003360 my @commit_lines = split '\n', $commit_text;
Jakub Narebski717b8312006-07-31 21:22:15 +02003361 my %co;
3362
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003363 pop @commit_lines; # Remove '\0'
3364
Jakub Narebski198a2a82007-05-12 21:16:34 +02003365 if (! @commit_lines) {
3366 return;
3367 }
3368
Jakub Narebski717b8312006-07-31 21:22:15 +02003369 my $header = shift @commit_lines;
Jakub Narebski198a2a82007-05-12 21:16:34 +02003370 if ($header !~ m/^[0-9a-fA-F]{40}/) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003371 return;
3372 }
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003373 ($co{'id'}, my @parents) = split ' ', $header;
Jakub Narebski717b8312006-07-31 21:22:15 +02003374 while (my $line = shift @commit_lines) {
3375 last if $line eq "\n";
3376 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3377 $co{'tree'} = $1;
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003378 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00003379 push @parents, $1;
Jakub Narebski717b8312006-07-31 21:22:15 +02003380 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
Zoltán Füzesi5ed5bbc2009-08-02 09:42:24 +02003381 $co{'author'} = to_utf8($1);
Jakub Narebski717b8312006-07-31 21:22:15 +02003382 $co{'author_epoch'} = $2;
3383 $co{'author_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01003384 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3385 $co{'author_name'} = $1;
3386 $co{'author_email'} = $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02003387 } else {
3388 $co{'author_name'} = $co{'author'};
3389 }
3390 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
Zoltán Füzesi5ed5bbc2009-08-02 09:42:24 +02003391 $co{'committer'} = to_utf8($1);
Jakub Narebski717b8312006-07-31 21:22:15 +02003392 $co{'committer_epoch'} = $2;
3393 $co{'committer_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01003394 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3395 $co{'committer_name'} = $1;
3396 $co{'committer_email'} = $2;
3397 } else {
3398 $co{'committer_name'} = $co{'committer'};
3399 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003400 }
3401 }
3402 if (!defined $co{'tree'}) {
3403 return;
3404 };
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00003405 $co{'parents'} = \@parents;
3406 $co{'parent'} = $parents[0];
Jakub Narebski717b8312006-07-31 21:22:15 +02003407
3408 foreach my $title (@commit_lines) {
3409 $title =~ s/^ //;
3410 if ($title ne "") {
3411 $co{'title'} = chop_str($title, 80, 5);
3412 # remove leading stuff of merges to make the interesting part visible
3413 if (length($title) > 50) {
3414 $title =~ s/^Automatic //;
3415 $title =~ s/^merge (of|with) /Merge ... /i;
3416 if (length($title) > 50) {
3417 $title =~ s/(http|rsync):\/\///;
3418 }
3419 if (length($title) > 50) {
3420 $title =~ s/(master|www|rsync)\.//;
3421 }
3422 if (length($title) > 50) {
3423 $title =~ s/kernel.org:?//;
3424 }
3425 if (length($title) > 50) {
3426 $title =~ s/\/pub\/scm//;
3427 }
3428 }
3429 $co{'title_short'} = chop_str($title, 50, 5);
3430 last;
3431 }
3432 }
Joey Hess53c39672008-09-05 14:26:29 -04003433 if (! defined $co{'title'} || $co{'title'} eq "") {
Petr Baudis7e0fe5c2006-10-06 18:55:04 +02003434 $co{'title'} = $co{'title_short'} = '(no commit message)';
3435 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003436 # remove added spaces
3437 foreach my $line (@commit_lines) {
3438 $line =~ s/^ //;
3439 }
3440 $co{'comment'} = \@commit_lines;
3441
3442 my $age = time - $co{'committer_epoch'};
3443 $co{'age'} = $age;
3444 $co{'age_string'} = age_string($age);
3445 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3446 if ($age > 60*60*24*7*2) {
3447 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3448 $co{'age_string_age'} = $co{'age_string'};
3449 } else {
3450 $co{'age_string_date'} = $co{'age_string'};
3451 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3452 }
3453 return %co;
3454}
3455
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003456sub parse_commit {
3457 my ($commit_id) = @_;
3458 my %co;
3459
3460 local $/ = "\0";
3461
3462 open my $fd, "-|", git_cmd(), "rev-list",
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003463 "--parents",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003464 "--header",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003465 "--max-count=1",
3466 $commit_id,
3467 "--",
Lea Wiemann074afaa2008-06-19 22:03:21 +02003468 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003469 %co = parse_commit_text(<$fd>, 1);
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003470 close $fd;
3471
3472 return %co;
3473}
3474
3475sub parse_commits {
Jakub Narebski311e5522008-02-26 13:22:06 +01003476 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003477 my @cos;
3478
3479 $maxcount ||= 1;
3480 $skip ||= 0;
3481
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003482 local $/ = "\0";
3483
3484 open my $fd, "-|", git_cmd(), "rev-list",
3485 "--header",
Jakub Narebski311e5522008-02-26 13:22:06 +01003486 @args,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003487 ("--max-count=" . $maxcount),
Robert Fitzsimonsf47efbb2006-12-24 14:31:49 +00003488 ("--skip=" . $skip),
Miklos Vajna868bc062007-07-12 20:39:27 +02003489 @extra_options,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003490 $commit_id,
3491 "--",
3492 ($filename ? ($filename) : ())
Lea Wiemann074afaa2008-06-19 22:03:21 +02003493 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003494 while (my $line = <$fd>) {
3495 my %co = parse_commit_text($line);
3496 push @cos, \%co;
3497 }
3498 close $fd;
3499
3500 return wantarray ? @cos : \@cos;
3501}
3502
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003503# parse line of git-diff-tree "raw" output
Jakub Narebski740e67f2006-08-21 23:07:00 +02003504sub parse_difftree_raw_line {
3505 my $line = shift;
3506 my %res;
3507
3508 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3509 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3510 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3511 $res{'from_mode'} = $1;
3512 $res{'to_mode'} = $2;
3513 $res{'from_id'} = $3;
3514 $res{'to_id'} = $4;
Jakub Narebski4ed4a342008-04-05 21:13:24 +01003515 $res{'status'} = $5;
Jakub Narebski740e67f2006-08-21 23:07:00 +02003516 $res{'similarity'} = $6;
3517 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003518 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02003519 } else {
Jakub Narebski9d301452007-11-01 12:38:08 +01003520 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02003521 }
3522 }
Jakub Narebski78bc4032007-05-07 01:10:03 +02003523 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3524 # combined diff (for merge commit)
3525 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3526 $res{'nparents'} = length($1);
3527 $res{'from_mode'} = [ split(' ', $2) ];
3528 $res{'to_mode'} = pop @{$res{'from_mode'}};
3529 $res{'from_id'} = [ split(' ', $3) ];
3530 $res{'to_id'} = pop @{$res{'from_id'}};
3531 $res{'status'} = [ split('', $4) ];
3532 $res{'to_file'} = unquote($5);
3533 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02003534 # 'c512b523472485aef4fff9e57b229d9d243c967f'
Jakub Narebski0edcb372006-08-31 00:36:04 +02003535 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3536 $res{'commit'} = $1;
3537 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02003538
3539 return wantarray ? %res : \%res;
3540}
3541
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003542# wrapper: return parsed line of git-diff-tree "raw" output
3543# (the argument might be raw line, or parsed info)
3544sub parsed_difftree_line {
3545 my $line_or_ref = shift;
3546
3547 if (ref($line_or_ref) eq "HASH") {
3548 # pre-parsed (or generated by hand)
3549 return $line_or_ref;
3550 } else {
3551 return parse_difftree_raw_line($line_or_ref);
3552 }
3553}
3554
Jakub Narebskicb849b42006-08-31 00:32:15 +02003555# parse line of git-ls-tree output
Jakub Narebski74fd8722009-05-07 19:11:29 +02003556sub parse_ls_tree_line {
Jakub Narebskicb849b42006-08-31 00:32:15 +02003557 my $line = shift;
3558 my %opts = @_;
3559 my %res;
3560
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003561 if ($opts{'-l'}) {
3562 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3563 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
Jakub Narebskicb849b42006-08-31 00:32:15 +02003564
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003565 $res{'mode'} = $1;
3566 $res{'type'} = $2;
3567 $res{'hash'} = $3;
3568 $res{'size'} = $4;
3569 if ($opts{'-z'}) {
3570 $res{'name'} = $5;
3571 } else {
3572 $res{'name'} = unquote($5);
3573 }
Jakub Narebskicb849b42006-08-31 00:32:15 +02003574 } else {
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003575 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3576 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3577
3578 $res{'mode'} = $1;
3579 $res{'type'} = $2;
3580 $res{'hash'} = $3;
3581 if ($opts{'-z'}) {
3582 $res{'name'} = $4;
3583 } else {
3584 $res{'name'} = unquote($4);
3585 }
Jakub Narebskicb849b42006-08-31 00:32:15 +02003586 }
3587
3588 return wantarray ? %res : \%res;
3589}
3590
Jakub Narebski90921742007-06-08 13:27:42 +02003591# generates _two_ hashes, references to which are passed as 2 and 3 argument
3592sub parse_from_to_diffinfo {
3593 my ($diffinfo, $from, $to, @parents) = @_;
3594
3595 if ($diffinfo->{'nparents'}) {
3596 # combined diff
3597 $from->{'file'} = [];
3598 $from->{'href'} = [];
3599 fill_from_file_info($diffinfo, @parents)
3600 unless exists $diffinfo->{'from_file'};
3601 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
Jakub Narebski9d301452007-11-01 12:38:08 +01003602 $from->{'file'}[$i] =
3603 defined $diffinfo->{'from_file'}[$i] ?
3604 $diffinfo->{'from_file'}[$i] :
3605 $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003606 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3607 $from->{'href'}[$i] = href(action=>"blob",
3608 hash_base=>$parents[$i],
3609 hash=>$diffinfo->{'from_id'}[$i],
3610 file_name=>$from->{'file'}[$i]);
3611 } else {
3612 $from->{'href'}[$i] = undef;
3613 }
3614 }
3615 } else {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003616 # ordinary (not combined) diff
Jakub Narebski9d301452007-11-01 12:38:08 +01003617 $from->{'file'} = $diffinfo->{'from_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003618 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3619 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3620 hash=>$diffinfo->{'from_id'},
3621 file_name=>$from->{'file'});
3622 } else {
3623 delete $from->{'href'};
3624 }
3625 }
3626
Jakub Narebski9d301452007-11-01 12:38:08 +01003627 $to->{'file'} = $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003628 if (!is_deleted($diffinfo)) { # file exists in result
3629 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3630 hash=>$diffinfo->{'to_id'},
3631 file_name=>$to->{'file'});
3632 } else {
3633 delete $to->{'href'};
3634 }
3635}
3636
Jakub Narebski717b8312006-07-31 21:22:15 +02003637## ......................................................................
3638## parse to array of hashes functions
3639
Jakub Narebskicd146402006-11-02 20:23:11 +01003640sub git_get_heads_list {
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003641 my ($limit, @classes) = @_;
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01003642 @classes = ('heads') unless @classes;
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003643 my @patterns = map { "refs/$_" } @classes;
Jakub Narebskicd146402006-11-02 20:23:11 +01003644 my @headslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003645
Jakub Narebskicd146402006-11-02 20:23:11 +01003646 open my $fd, '-|', git_cmd(), 'for-each-ref',
3647 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3648 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003649 @patterns
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003650 or return;
3651 while (my $line = <$fd>) {
Jakub Narebskicd146402006-11-02 20:23:11 +01003652 my %ref_item;
Jakub Narebski120ddde2006-09-19 14:33:22 +02003653
Jakub Narebskicd146402006-11-02 20:23:11 +01003654 chomp $line;
3655 my ($refinfo, $committerinfo) = split(/\0/, $line);
3656 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3657 my ($committer, $epoch, $tz) =
3658 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01003659 $ref_item{'fullname'} = $name;
Giuseppe Bilotta60efa242010-11-11 13:26:09 +01003660 $name =~ s!^refs/(?:head|remote)s/!!;
Jakub Narebskicd146402006-11-02 20:23:11 +01003661
3662 $ref_item{'name'} = $name;
3663 $ref_item{'id'} = $hash;
3664 $ref_item{'title'} = $title || '(no commit message)';
3665 $ref_item{'epoch'} = $epoch;
3666 if ($epoch) {
3667 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3668 } else {
3669 $ref_item{'age'} = "unknown";
Jakub Narebski717b8312006-07-31 21:22:15 +02003670 }
Jakub Narebskicd146402006-11-02 20:23:11 +01003671
3672 push @headslist, \%ref_item;
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003673 }
3674 close $fd;
Junio C Hamano7a13b992006-07-31 19:18:34 -07003675
Jakub Narebskicd146402006-11-02 20:23:11 +01003676 return wantarray ? @headslist : \@headslist;
3677}
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003678
Jakub Narebskicd146402006-11-02 20:23:11 +01003679sub git_get_tags_list {
3680 my $limit = shift;
3681 my @tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003682
Jakub Narebskicd146402006-11-02 20:23:11 +01003683 open my $fd, '-|', git_cmd(), 'for-each-ref',
3684 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3685 '--format=%(objectname) %(objecttype) %(refname) '.
3686 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3687 'refs/tags'
3688 or return;
3689 while (my $line = <$fd>) {
3690 my %ref_item;
3691
3692 chomp $line;
3693 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3694 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3695 my ($creator, $epoch, $tz) =
3696 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01003697 $ref_item{'fullname'} = $name;
Jakub Narebskicd146402006-11-02 20:23:11 +01003698 $name =~ s!^refs/tags/!!;
3699
3700 $ref_item{'type'} = $type;
3701 $ref_item{'id'} = $id;
3702 $ref_item{'name'} = $name;
3703 if ($type eq "tag") {
3704 $ref_item{'subject'} = $title;
3705 $ref_item{'reftype'} = $reftype;
3706 $ref_item{'refid'} = $refid;
3707 } else {
3708 $ref_item{'reftype'} = $type;
3709 $ref_item{'refid'} = $id;
3710 }
3711
3712 if ($type eq "tag" || $type eq "commit") {
3713 $ref_item{'epoch'} = $epoch;
3714 if ($epoch) {
3715 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3716 } else {
3717 $ref_item{'age'} = "unknown";
3718 }
3719 }
3720
3721 push @tagslist, \%ref_item;
Jakub Narebski717b8312006-07-31 21:22:15 +02003722 }
Jakub Narebskicd146402006-11-02 20:23:11 +01003723 close $fd;
3724
3725 return wantarray ? @tagslist : \@tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003726}
3727
3728## ----------------------------------------------------------------------
3729## filesystem-related functions
3730
3731sub get_file_owner {
3732 my $path = shift;
3733
3734 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3735 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3736 if (!defined $gcos) {
3737 return undef;
3738 }
3739 my $owner = $gcos;
3740 $owner =~ s/[,;].*$//;
Martin Koegler00f429a2007-06-03 17:42:44 +02003741 return to_utf8($owner);
Jakub Narebski717b8312006-07-31 21:22:15 +02003742}
3743
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01003744# assume that file exists
3745sub insert_file {
3746 my $filename = shift;
3747
3748 open my $fd, '<', $filename;
Jakub Narebski45868642008-12-08 14:13:21 +01003749 print map { to_utf8($_) } <$fd>;
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01003750 close $fd;
3751}
3752
Jakub Narebski717b8312006-07-31 21:22:15 +02003753## ......................................................................
3754## mimetype related functions
3755
3756sub mimetype_guess_file {
3757 my $filename = shift;
3758 my $mimemap = shift;
3759 -r $mimemap or return undef;
3760
3761 my %mimemap;
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003762 open(my $mh, '<', $mimemap) or return undef;
Jakub Narebskiad87e4f2009-05-11 03:21:06 +02003763 while (<$mh>) {
Jakub Narebski618918e2006-08-14 02:15:22 +02003764 next if m/^#/; # skip comments
Ludwig Nussel93a6ad12011-06-15 08:10:08 +02003765 my ($mimetype, @exts) = split(/\s+/);
3766 foreach my $ext (@exts) {
3767 $mimemap{$ext} = $mimetype;
Jakub Narebski717b8312006-07-31 21:22:15 +02003768 }
3769 }
Jakub Narebskiad87e4f2009-05-11 03:21:06 +02003770 close($mh);
Jakub Narebski717b8312006-07-31 21:22:15 +02003771
Jakub Narebski80593192006-09-19 13:57:03 +02003772 $filename =~ /\.([^.]*)$/;
Jakub Narebski717b8312006-07-31 21:22:15 +02003773 return $mimemap{$1};
3774}
3775
3776sub mimetype_guess {
3777 my $filename = shift;
3778 my $mime;
3779 $filename =~ /\./ or return undef;
3780
3781 if ($mimetypes_file) {
3782 my $file = $mimetypes_file;
Jakub Narebskid5aa50d2006-08-14 02:16:33 +02003783 if ($file !~ m!^/!) { # if it is relative path
3784 # it is relative to project
3785 $file = "$projectroot/$project/$file";
3786 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003787 $mime = mimetype_guess_file($filename, $file);
3788 }
3789 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3790 return $mime;
3791}
3792
Jakub Narebski847e01f2006-08-14 02:05:47 +02003793sub blob_mimetype {
Jakub Narebski717b8312006-07-31 21:22:15 +02003794 my $fd = shift;
3795 my $filename = shift;
3796
3797 if ($filename) {
3798 my $mime = mimetype_guess($filename);
3799 $mime and return $mime;
3800 }
3801
3802 # just in case
3803 return $default_blob_plain_mimetype unless $fd;
3804
3805 if (-T $fd) {
Jakub Narebski7f718e82008-06-03 16:47:10 +02003806 return 'text/plain';
Jakub Narebski717b8312006-07-31 21:22:15 +02003807 } elsif (! $filename) {
3808 return 'application/octet-stream';
3809 } elsif ($filename =~ m/\.png$/i) {
3810 return 'image/png';
3811 } elsif ($filename =~ m/\.gif$/i) {
3812 return 'image/gif';
3813 } elsif ($filename =~ m/\.jpe?g$/i) {
3814 return 'image/jpeg';
3815 } else {
3816 return 'application/octet-stream';
3817 }
3818}
3819
Jakub Narebski7f718e82008-06-03 16:47:10 +02003820sub blob_contenttype {
3821 my ($fd, $file_name, $type) = @_;
3822
3823 $type ||= blob_mimetype($fd, $file_name);
3824 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3825 $type .= "; charset=$default_text_plain_charset";
3826 }
3827
3828 return $type;
3829}
3830
Jakub Narebski592ea412010-04-27 21:34:45 +02003831# guess file syntax for syntax highlighting; return undef if no highlighting
3832# the name of syntax can (in the future) depend on syntax highlighter used
3833sub guess_file_syntax {
3834 my ($highlight, $mimetype, $file_name) = @_;
3835 return undef unless ($highlight && defined $file_name);
Jakub Narebski592ea412010-04-27 21:34:45 +02003836 my $basename = basename($file_name, '.in');
3837 return $highlight_basename{$basename}
3838 if exists $highlight_basename{$basename};
3839
3840 $basename =~ /\.([^.]*)$/;
3841 my $ext = $1 or return undef;
3842 return $highlight_ext{$ext}
3843 if exists $highlight_ext{$ext};
3844
3845 return undef;
3846}
3847
3848# run highlighter and return FD of its output,
3849# or return original FD if no highlighting
3850sub run_highlighter {
3851 my ($fd, $highlight, $syntax) = @_;
3852 return $fd unless ($highlight && defined $syntax);
3853
Sylvain Rabot3ca73532010-12-30 22:20:29 +01003854 close $fd;
Jakub Narebski592ea412010-04-27 21:34:45 +02003855 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
Christopher Wilson7ce896b2010-09-21 00:25:19 -07003856 quote_command($highlight_bin).
Kevin Cernekee6affdbe2011-03-16 15:34:13 -07003857 " --replace-tabs=8 --fragment --syntax $syntax |"
Jakub Narebski592ea412010-04-27 21:34:45 +02003858 or die_error(500, "Couldn't open file or run syntax highlighter");
3859 return $fd;
3860}
3861
Jakub Narebski717b8312006-07-31 21:22:15 +02003862## ======================================================================
3863## functions printing HTML: header, footer, error page
3864
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02003865sub get_page_title {
3866 my $title = to_utf8($site_name);
3867
Bernhard R. Link19d2d232012-01-30 21:07:37 +01003868 unless (defined $project) {
3869 if (defined $project_filter) {
Jakub Narebskif4212082012-02-12 16:21:30 +01003870 $title .= " - projects in '" . esc_path($project_filter) . "'";
Bernhard R. Link19d2d232012-01-30 21:07:37 +01003871 }
3872 return $title;
3873 }
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02003874 $title .= " - " . to_utf8($project);
3875
3876 return $title unless (defined $action);
3877 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3878
3879 return $title unless (defined $file_name);
3880 $title .= " - " . esc_path($file_name);
3881 if ($action eq "tree" && $file_name !~ m|/$|) {
3882 $title .= "/";
3883 }
3884
3885 return $title;
3886}
3887
Jakub Narebski6ee90332011-06-22 13:50:46 +02003888sub get_content_type_html {
3889 # require explicit support from the UA if we are to send the page as
3890 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3891 # we have to do this because MSIE sometimes globs '*/*', pretending to
3892 # support xhtml+xml but choking when it gets what it asked for.
3893 if (defined $cgi->http('HTTP_ACCEPT') &&
3894 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3895 $cgi->Accept('application/xhtml+xml') != 0) {
3896 return 'application/xhtml+xml';
3897 } else {
3898 return 'text/html';
3899 }
3900}
3901
Jakub Narebski05bb5a22010-12-18 21:02:13 +01003902sub print_feed_meta {
3903 if (defined $project) {
3904 my %href_params = get_feed_info();
3905 if (!exists $href_params{'-title'}) {
3906 $href_params{'-title'} = 'log';
3907 }
3908
Ævar Arnfjörð Bjarmason0f54b7d2011-02-19 15:27:41 +00003909 foreach my $format (qw(RSS Atom)) {
Jakub Narebski05bb5a22010-12-18 21:02:13 +01003910 my $type = lc($format);
3911 my %link_attr = (
3912 '-rel' => 'alternate',
3913 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
3914 '-type' => "application/$type+xml"
3915 );
3916
Sebastian Pippingcc999a32012-04-04 14:25:44 +02003917 $href_params{'extra_options'} = undef;
Jakub Narebski05bb5a22010-12-18 21:02:13 +01003918 $href_params{'action'} = $type;
3919 $link_attr{'-href'} = href(%href_params);
3920 print "<link ".
3921 "rel=\"$link_attr{'-rel'}\" ".
3922 "title=\"$link_attr{'-title'}\" ".
3923 "href=\"$link_attr{'-href'}\" ".
3924 "type=\"$link_attr{'-type'}\" ".
3925 "/>\n";
3926
3927 $href_params{'extra_options'} = '--no-merges';
3928 $link_attr{'-href'} = href(%href_params);
3929 $link_attr{'-title'} .= ' (no merges)';
3930 print "<link ".
3931 "rel=\"$link_attr{'-rel'}\" ".
3932 "title=\"$link_attr{'-title'}\" ".
3933 "href=\"$link_attr{'-href'}\" ".
3934 "type=\"$link_attr{'-type'}\" ".
3935 "/>\n";
3936 }
3937
3938 } else {
3939 printf('<link rel="alternate" title="%s projects list" '.
3940 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3941 esc_attr($site_name), href(project=>undef, action=>"project_index"));
3942 printf('<link rel="alternate" title="%s projects feeds" '.
3943 'href="%s" type="text/x-opml" />'."\n",
3944 esc_attr($site_name), href(project=>undef, action=>"opml"));
3945 }
3946}
3947
Jakub Narebski6ee90332011-06-22 13:50:46 +02003948sub print_header_links {
3949 my $status = shift;
3950
3951 # print out each stylesheet that exist, providing backwards capability
3952 # for those people who defined $stylesheet in a config file
3953 if (defined $stylesheet) {
3954 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3955 } else {
3956 foreach my $stylesheet (@stylesheets) {
3957 next unless $stylesheet;
3958 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3959 }
3960 }
3961 print_feed_meta()
3962 if ($status eq '200 OK');
3963 if (defined $favicon) {
3964 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3965 }
3966}
3967
Bernhard R. Link40efa222012-01-30 21:09:43 +01003968sub print_nav_breadcrumbs_path {
3969 my $dirprefix = undef;
3970 while (my $part = shift) {
3971 $dirprefix .= "/" if defined $dirprefix;
3972 $dirprefix .= $part;
3973 print $cgi->a({-href => href(project => undef,
3974 project_filter => $dirprefix,
3975 action => "project_list")},
3976 esc_html($part)) . " / ";
3977 }
3978}
3979
Jakub Narebski6ee90332011-06-22 13:50:46 +02003980sub print_nav_breadcrumbs {
3981 my %opts = @_;
3982
3983 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3984 if (defined $project) {
Bernhard R. Link4426ba22012-01-30 21:10:23 +01003985 my @dirname = split '/', $project;
3986 my $projectbasename = pop @dirname;
3987 print_nav_breadcrumbs_path(@dirname);
3988 print $cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));
Jakub Narebski6ee90332011-06-22 13:50:46 +02003989 if (defined $action) {
3990 my $action_print = $action ;
3991 if (defined $opts{-action_extra}) {
3992 $action_print = $cgi->a({-href => href(action=>$action)},
3993 $action);
3994 }
3995 print " / $action_print";
3996 }
3997 if (defined $opts{-action_extra}) {
3998 print " / $opts{-action_extra}";
3999 }
4000 print "\n";
Bernhard R. Link40efa222012-01-30 21:09:43 +01004001 } elsif (defined $project_filter) {
4002 print_nav_breadcrumbs_path(split '/', $project_filter);
Jakub Narebski6ee90332011-06-22 13:50:46 +02004003 }
4004}
4005
4006sub print_search_form {
4007 if (!defined $searchtext) {
4008 $searchtext = "";
4009 }
4010 my $search_hash;
4011 if (defined $hash_base) {
4012 $search_hash = $hash_base;
4013 } elsif (defined $hash) {
4014 $search_hash = $hash;
4015 } else {
4016 $search_hash = "HEAD";
4017 }
4018 my $action = $my_uri;
4019 my $use_pathinfo = gitweb_check_feature('pathinfo');
4020 if ($use_pathinfo) {
4021 $action .= "/".esc_url($project);
4022 }
4023 print $cgi->startform(-method => "get", -action => $action) .
4024 "<div class=\"search\">\n" .
4025 (!$use_pathinfo &&
4026 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
4027 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
4028 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
4029 $cgi->popup_menu(-name => 'st', -default => 'commit',
4030 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
4031 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
4032 " search:\n",
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01004033 $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
Jakub Narebski6ee90332011-06-22 13:50:46 +02004034 "<span title=\"Extended regular expression\">" .
4035 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
4036 -checked => $search_use_regexp) .
4037 "</span>" .
4038 "</div>" .
4039 $cgi->end_form() . "\n";
4040}
4041
Kay Sievers12a88f22005-08-07 20:02:47 +02004042sub git_header_html {
Kay Sieversa59d4af2005-08-07 20:15:44 +02004043 my $status = shift || "200 OK";
Kay Sievers11044292005-10-19 03:18:45 +02004044 my $expires = shift;
Jakub Narebski7a597452010-04-24 16:00:04 +02004045 my %opts = @_;
Kay Sieversa59d4af2005-08-07 20:15:44 +02004046
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02004047 my $title = get_page_title();
Jakub Narebski6ee90332011-06-22 13:50:46 +02004048 my $content_type = get_content_type_html();
Jakub Narebski952c65f2006-08-22 16:52:50 +02004049 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
Jakub Narebski7a597452010-04-24 16:00:04 +02004050 -status=> $status, -expires => $expires)
Jakub Narebskiad709ea2010-06-13 00:35:59 +02004051 unless ($opts{'-no_http_header'});
Jakub Narebski45c9a752006-12-27 23:59:51 +01004052 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
Kay Sieversa59d4af2005-08-07 20:15:44 +02004053 print <<EOF;
Kay Sievers6191f8e2005-08-07 20:19:56 +02004054<?xml version="1.0" encoding="utf-8"?>
Kay Sievers161332a2005-08-07 19:49:46 +02004055<!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 +02004056<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02004057<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
Jakub Narebskiae20de52006-06-21 09:48:03 +02004058<!-- git core binaries version $git_version -->
Kay Sievers161332a2005-08-07 19:49:46 +02004059<head>
Alp Tokerf6801d62006-07-11 11:19:34 +01004060<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
Jakub Narebski45c9a752006-12-27 23:59:51 +01004061<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
Kay Sieversc994d622005-08-07 20:27:18 +02004062<meta name="robots" content="index, nofollow"/>
Kay Sieversb87d78d2005-08-07 20:21:04 +02004063<title>$title</title>
Kay Sievers161332a2005-08-07 19:49:46 +02004064EOF
Giuseppe Bilotta41a4d162009-01-31 02:31:52 +01004065 # the stylesheet, favicon etc urls won't work correctly with path_info
4066 # unless we set the appropriate base URL
Giuseppe Bilottac3254ae2009-01-31 02:31:50 +01004067 if ($ENV{'PATH_INFO'}) {
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +01004068 print "<base href=\"".esc_url($base_url)."\" />\n";
Giuseppe Bilottac3254ae2009-01-31 02:31:50 +01004069 }
Jakub Narebski6ee90332011-06-22 13:50:46 +02004070 print_header_links($status);
Lénaïc Huardc1355b72011-10-21 09:09:29 +02004071
4072 if (defined $site_html_head_string) {
4073 print to_utf8($site_html_head_string);
4074 }
4075
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02004076 print "</head>\n" .
Alan Chandlerb2d34762006-10-03 13:49:03 +01004077 "<body>\n";
4078
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01004079 if (defined $site_header && -f $site_header) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004080 insert_file($site_header);
Alan Chandlerb2d34762006-10-03 13:49:03 +01004081 }
4082
Jonathan Nieder68220522010-09-03 19:45:09 -05004083 print "<div class=\"page_header\">\n";
4084 if (defined $logo) {
4085 print $cgi->a({-href => esc_url($logo_url),
4086 -title => $logo_label},
4087 $cgi->img({-src => esc_url($logo),
4088 -width => 72, -height => 27,
4089 -alt => "git",
4090 -class => "logo"}));
4091 }
Jakub Narebski6ee90332011-06-22 13:50:46 +02004092 print_nav_breadcrumbs(%opts);
Petr Baudisd77b5672007-05-17 04:24:19 +02004093 print "</div>\n";
4094
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004095 my $have_search = gitweb_check_feature('search');
Jakub Narebskif70dda22008-06-02 11:54:41 +02004096 if (defined $project && $have_search) {
Jakub Narebski6ee90332011-06-22 13:50:46 +02004097 print_search_form();
Kay Sievers4c02e3c2005-08-07 19:52:52 +02004098 }
Kay Sievers161332a2005-08-07 19:49:46 +02004099}
4100
Kay Sievers12a88f22005-08-07 20:02:47 +02004101sub git_footer_html {
Jakub Narebski35621982008-04-20 22:09:48 +02004102 my $feed_class = 'rss_logo';
4103
Kay Sievers6191f8e2005-08-07 20:19:56 +02004104 print "<div class=\"page_footer\">\n";
Kay Sieversb87d78d2005-08-07 20:21:04 +02004105 if (defined $project) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004106 my $descr = git_get_project_description($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02004107 if (defined $descr) {
Kay Sievers40c13812005-11-19 17:41:29 +01004108 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
Kay Sievers6191f8e2005-08-07 20:19:56 +02004109 }
Jakub Narebski35621982008-04-20 22:09:48 +02004110
4111 my %href_params = get_feed_info();
4112 if (!%href_params) {
4113 $feed_class .= ' generic';
4114 }
4115 $href_params{'-title'} ||= 'log';
4116
Ævar Arnfjörð Bjarmason0f54b7d2011-02-19 15:27:41 +00004117 foreach my $format (qw(RSS Atom)) {
Jakub Narebski35621982008-04-20 22:09:48 +02004118 $href_params{'action'} = lc($format);
4119 print $cgi->a({-href => href(%href_params),
4120 -title => "$href_params{'-title'} $format feed",
4121 -class => $feed_class}, $format)."\n";
4122 }
4123
Kay Sieversc994d622005-08-07 20:27:18 +02004124 } else {
Bernhard R. Link56efd9d2012-01-30 21:09:00 +01004125 print $cgi->a({-href => href(project=>undef, action=>"opml",
4126 project_filter => $project_filter),
Jakub Narebski35621982008-04-20 22:09:48 +02004127 -class => $feed_class}, "OPML") . " ";
Bernhard R. Link56efd9d2012-01-30 21:09:00 +01004128 print $cgi->a({-href => href(project=>undef, action=>"project_index",
4129 project_filter => $project_filter),
Jakub Narebski35621982008-04-20 22:09:48 +02004130 -class => $feed_class}, "TXT") . "\n";
Kay Sieversff7669a2005-08-07 20:13:02 +02004131 }
Jakub Narebski35621982008-04-20 22:09:48 +02004132 print "</div>\n"; # class="page_footer"
Alan Chandlerb2d34762006-10-03 13:49:03 +01004133
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02004134 if (defined $t0 && gitweb_check_feature('timed')) {
4135 print "<div id=\"generating_info\">\n";
4136 print 'This page took '.
4137 '<span id="generating_time" class="time_span">'.
Jakub Narebski3962f1d72010-11-09 19:27:54 +01004138 tv_interval($t0, [ gettimeofday() ]).
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02004139 ' seconds </span>'.
4140 ' and '.
4141 '<span id="generating_cmd">'.
4142 $number_of_git_cmds.
4143 '</span> git commands '.
4144 " to generate.\n";
4145 print "</div>\n"; # class="page_footer"
4146 }
4147
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01004148 if (defined $site_footer && -f $site_footer) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004149 insert_file($site_footer);
Alan Chandlerb2d34762006-10-03 13:49:03 +01004150 }
4151
Junio C Hamanoabf411e2010-12-15 11:32:57 -08004152 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004153 if (defined $action &&
4154 $action eq 'blame_incremental') {
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004155 print qq!<script type="text/javascript">\n!.
4156 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
4157 qq! "!. href() .qq!");\n!.
4158 qq!</script>\n!;
John 'Warthog9' Hawley291e52b2011-04-28 21:04:09 +02004159 } else {
Jakub Narebski2e987f92011-04-28 21:04:11 +02004160 my ($jstimezone, $tz_cookie, $datetime_class) =
4161 gitweb_get_feature('javascript-timezone');
4162
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004163 print qq!<script type="text/javascript">\n!.
Jakub Narebski2e987f92011-04-28 21:04:11 +02004164 qq!window.onload = function () {\n!;
4165 if (gitweb_check_feature('javascript-actions')) {
4166 print qq! fixLinks();\n!;
4167 }
4168 if ($jstimezone && $tz_cookie && $datetime_class) {
4169 print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
4170 qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
4171 }
4172 print qq!};\n!.
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004173 qq!</script>\n!;
4174 }
4175
Alan Chandlerb2d34762006-10-03 13:49:03 +01004176 print "</body>\n" .
Kay Sievers9cd3d982005-08-07 20:17:42 +02004177 "</html>";
Kay Sievers161332a2005-08-07 19:49:46 +02004178}
4179
Jakub Narebski453541f2010-02-07 21:51:18 +01004180# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
Lea Wiemann074afaa2008-06-19 22:03:21 +02004181# Example: die_error(404, 'Hash not found')
4182# By convention, use the following status codes (as defined in RFC 2616):
4183# 400: Invalid or missing CGI parameters, or
4184# requested object exists but has wrong type.
4185# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
4186# this server or project.
4187# 404: Requested object/revision/project doesn't exist.
4188# 500: The server isn't configured properly, or
4189# an internal error occurred (e.g. failed assertions caused by bugs), or
4190# an unknown error occurred (e.g. the git binary died unexpectedly).
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004191# 503: The server is currently unavailable (because it is overloaded,
4192# or down for maintenance). Generally, this is a temporary state.
Kay Sievers061cc7c2005-08-07 20:15:57 +02004193sub die_error {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004194 my $status = shift || 500;
Jakub Narebski1df48762010-02-07 21:52:25 +01004195 my $error = esc_html(shift) || "Internal Server Error";
John 'Warthog9' Hawleyaa140132010-01-30 23:30:44 +01004196 my $extra = shift;
Jakub Narebski7a597452010-04-24 16:00:04 +02004197 my %opts = @_;
Kay Sievers664f4cc2005-08-07 20:17:19 +02004198
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004199 my %http_responses = (
4200 400 => '400 Bad Request',
4201 403 => '403 Forbidden',
4202 404 => '404 Not Found',
4203 500 => '500 Internal Server Error',
4204 503 => '503 Service Unavailable',
4205 );
Jakub Narebski7a597452010-04-24 16:00:04 +02004206 git_header_html($http_responses{$status}, undef, %opts);
Jakub Narebski59b9f612006-08-22 23:42:53 +02004207 print <<EOF;
4208<div class="page_body">
4209<br /><br />
4210$status - $error
4211<br />
Jakub Narebski59b9f612006-08-22 23:42:53 +02004212EOF
John 'Warthog9' Hawleyaa140132010-01-30 23:30:44 +01004213 if (defined $extra) {
4214 print "<hr />\n" .
4215 "$extra\n";
4216 }
4217 print "</div>\n";
4218
Kay Sieversa59d4af2005-08-07 20:15:44 +02004219 git_footer_html();
Jakub Narebski7a597452010-04-24 16:00:04 +02004220 goto DONE_GITWEB
4221 unless ($opts{'-error_handler'});
Kay Sieversa59d4af2005-08-07 20:15:44 +02004222}
4223
Jakub Narebski717b8312006-07-31 21:22:15 +02004224## ----------------------------------------------------------------------
4225## functions printing or outputting HTML: navigation
4226
Jakub Narebski847e01f2006-08-14 02:05:47 +02004227sub git_print_page_nav {
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004228 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
4229 $extra = '' if !defined $extra; # pager or formats
4230
4231 my @navs = qw(summary shortlog log commit commitdiff tree);
4232 if ($suppress) {
4233 @navs = grep { $_ ne $suppress } @navs;
4234 }
4235
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004236 my %arg = map { $_ => {action=>$_} } @navs;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004237 if (defined $head) {
4238 for (qw(commit commitdiff)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02004239 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004240 }
4241 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4242 for (qw(shortlog log)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02004243 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004244 }
4245 }
4246 }
Petr Baudisd627f682008-10-02 16:36:52 +02004247
Jakub Narebski3be8e722007-04-01 22:22:21 +02004248 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4249 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004250
Junio C Hamanoa7c5a282008-11-29 13:02:08 -08004251 my @actions = gitweb_get_feature('actions');
Jakub Narebski2b11e052008-10-12 00:02:32 +02004252 my %repl = (
4253 '%' => '%',
4254 'n' => $project, # project name
4255 'f' => $git_dir, # project path within filesystem
4256 'h' => $treehead || '', # current hash ('h' parameter)
4257 'b' => $treebase || '', # hash base ('hb' parameter)
4258 );
Petr Baudisd627f682008-10-02 16:36:52 +02004259 while (@actions) {
Jakub Narebski2b11e052008-10-12 00:02:32 +02004260 my ($label, $link, $pos) = splice(@actions,0,3);
4261 # insert
Petr Baudisd627f682008-10-02 16:36:52 +02004262 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4263 # munch munch
Jakub Narebski2b11e052008-10-12 00:02:32 +02004264 $link =~ s/%([%nfhb])/$repl{$1}/g;
Petr Baudisd627f682008-10-02 16:36:52 +02004265 $arg{$label}{'_href'} = $link;
4266 }
4267
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004268 print "<div class=\"page_nav\">\n" .
4269 (join " | ",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004270 map { $_ eq $current ?
Petr Baudisd627f682008-10-02 16:36:52 +02004271 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004272 } @navs);
Jakub Narebski898a8932006-07-30 16:14:43 +02004273 print "<br/>\n$extra<br/>\n" .
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004274 "</div>\n";
4275}
4276
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01004277# returns a submenu for the nagivation of the refs views (tags, heads,
4278# remotes) with the current view disabled and the remotes view only
4279# available if the feature is enabled
4280sub format_ref_views {
4281 my ($current) = @_;
4282 my @ref_views = qw{tags heads};
4283 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4284 return join " | ", map {
4285 $_ eq $current ? $_ :
4286 $cgi->a({-href => href(action=>$_)}, $_)
4287 } @ref_views
4288}
4289
Jakub Narebski847e01f2006-08-14 02:05:47 +02004290sub format_paging_nav {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004291 my ($action, $page, $has_next_link) = @_;
Jakub Narebski43ffc062006-07-30 17:49:00 +02004292 my $paging_nav;
4293
4294
Jakub Narebski43ffc062006-07-30 17:49:00 +02004295 if ($page > 0) {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004296 $paging_nav .=
4297 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4298 " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01004299 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebski26298b52006-08-10 12:38:47 +02004300 -accesskey => "p", -title => "Alt-p"}, "prev");
Jakub Narebski43ffc062006-07-30 17:49:00 +02004301 } else {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004302 $paging_nav .= "first &sdot; prev";
Jakub Narebski43ffc062006-07-30 17:49:00 +02004303 }
4304
Lea Wiemann1f684dc2008-05-28 01:25:42 +02004305 if ($has_next_link) {
Jakub Narebski43ffc062006-07-30 17:49:00 +02004306 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01004307 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Jakub Narebski26298b52006-08-10 12:38:47 +02004308 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski43ffc062006-07-30 17:49:00 +02004309 } else {
4310 $paging_nav .= " &sdot; next";
4311 }
4312
4313 return $paging_nav;
4314}
4315
Jakub Narebski717b8312006-07-31 21:22:15 +02004316## ......................................................................
4317## functions printing or outputting HTML: div
Kay Sievers42f7eb92005-08-07 20:21:46 +02004318
Jakub Narebski847e01f2006-08-14 02:05:47 +02004319sub git_print_header_div {
Jakub Narebski717b8312006-07-31 21:22:15 +02004320 my ($action, $title, $hash, $hash_base) = @_;
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004321 my %args = ();
Jakub Narebski717b8312006-07-31 21:22:15 +02004322
Jakub Narebski3be8e722007-04-01 22:22:21 +02004323 $args{'action'} = $action;
4324 $args{'hash'} = $hash if $hash;
4325 $args{'hash_base'} = $hash_base if $hash_base;
Jakub Narebski717b8312006-07-31 21:22:15 +02004326
4327 print "<div class=\"header\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004328 $cgi->a({-href => href(%args), -class => "title"},
4329 $title ? $title : $action) .
4330 "\n</div>\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02004331}
4332
Giuseppe Bilotta0e656992010-11-11 13:26:15 +01004333sub format_repo_url {
4334 my ($name, $url) = @_;
4335 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4336}
4337
Giuseppe Bilottab891d522010-11-11 13:26:16 +01004338# Group output by placing it in a DIV element and adding a header.
4339# Options for start_div() can be provided by passing a hash reference as the
4340# first parameter to the function.
4341# Options to git_print_header_div() can be provided by passing an array
4342# reference. This must follow the options to start_div if they are present.
4343# The content can be a scalar, which is output as-is, a scalar reference, which
4344# is output after html escaping, an IO handle passed either as *handle or
4345# *handle{IO}, or a function reference. In the latter case all following
4346# parameters will be taken as argument to the content function call.
4347sub git_print_section {
4348 my ($div_args, $header_args, $content);
4349 my $arg = shift;
4350 if (ref($arg) eq 'HASH') {
4351 $div_args = $arg;
4352 $arg = shift;
4353 }
4354 if (ref($arg) eq 'ARRAY') {
4355 $header_args = $arg;
4356 $arg = shift;
4357 }
4358 $content = $arg;
4359
4360 print $cgi->start_div($div_args);
4361 git_print_header_div(@$header_args);
4362
4363 if (ref($content) eq 'CODE') {
4364 $content->(@_);
4365 } elsif (ref($content) eq 'SCALAR') {
4366 print esc_html($$content);
4367 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4368 print <$content>;
4369 } elsif (!ref($content) && defined($content)) {
4370 print $content;
4371 }
4372
4373 print $cgi->end_div;
4374}
4375
Jakub Narebski256b7b42011-04-28 21:04:07 +02004376sub format_timestamp_html {
Jakub Narebskice71b072011-04-28 21:04:08 +02004377 my $date = shift;
Jakub Narebski2e987f92011-04-28 21:04:11 +02004378 my $strtime = $date->{'rfc2822'};
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004379
Jakub Narebski2e987f92011-04-28 21:04:11 +02004380 my (undef, undef, $datetime_class) =
4381 gitweb_get_feature('javascript-timezone');
4382 if ($datetime_class) {
4383 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
Jakub Narebskia44465c2006-08-28 23:17:31 +02004384 }
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004385
Jakub Narebski256b7b42011-04-28 21:04:07 +02004386 my $localtime_format = '(%02d:%02d %s)';
4387 if ($date->{'hour_local'} < 6) {
4388 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
Jakub Narebskia44465c2006-08-28 23:17:31 +02004389 }
Jakub Narebski256b7b42011-04-28 21:04:07 +02004390 $strtime .= ' ' .
4391 sprintf($localtime_format,
4392 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004393
Jakub Narebski256b7b42011-04-28 21:04:07 +02004394 return $strtime;
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004395}
4396
4397# Outputs the author name and date in long form
4398sub git_print_authorship {
4399 my $co = shift;
4400 my %opts = @_;
4401 my $tag = $opts{-tag} || 'div';
Stephen Boyde133d652009-10-15 21:14:59 -07004402 my $author = $co->{'author_name'};
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004403
4404 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4405 print "<$tag class=\"author_date\">" .
Stephen Boyde133d652009-10-15 21:14:59 -07004406 format_search_author($author, "author", esc_html($author)) .
Jakub Narebskice71b072011-04-28 21:04:08 +02004407 " [".format_timestamp_html(\%ad)."]".
Jakub Narebski256b7b42011-04-28 21:04:07 +02004408 git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4409 "</$tag>\n";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004410}
4411
4412# Outputs table rows containing the full author or committer information,
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02004413# in the format expected for 'commit' view (& similar).
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004414# Parameters are a commit hash reference, followed by the list of people
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02004415# to output information for. If the list is empty it defaults to both
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004416# author and committer.
4417sub git_print_authorship_rows {
4418 my $co = shift;
4419 # too bad we can't use @people = @_ || ('author', 'committer')
4420 my @people = @_;
4421 @people = ('author', 'committer') unless @people;
4422 foreach my $who (@people) {
4423 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
Stephen Boyde133d652009-10-15 21:14:59 -07004424 print "<tr><td>$who</td><td>" .
4425 format_search_author($co->{"${who}_name"}, $who,
Jakub Narebski256b7b42011-04-28 21:04:07 +02004426 esc_html($co->{"${who}_name"})) . " " .
Stephen Boyde133d652009-10-15 21:14:59 -07004427 format_search_author($co->{"${who}_email"}, $who,
Jakub Narebski256b7b42011-04-28 21:04:07 +02004428 esc_html("<" . $co->{"${who}_email"} . ">")) .
Stephen Boyde133d652009-10-15 21:14:59 -07004429 "</td><td rowspan=\"2\">" .
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02004430 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4431 "</td></tr>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004432 "<tr>" .
Jakub Narebski256b7b42011-04-28 21:04:07 +02004433 "<td></td><td>" .
Jakub Narebskice71b072011-04-28 21:04:08 +02004434 format_timestamp_html(\%wd) .
Jakub Narebski256b7b42011-04-28 21:04:07 +02004435 "</td>" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004436 "</tr>\n";
4437 }
Jakub Narebski6fd92a22006-08-28 14:48:12 +02004438}
4439
Jakub Narebski717b8312006-07-31 21:22:15 +02004440sub git_print_page_path {
4441 my $name = shift;
4442 my $type = shift;
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004443 my $hb = shift;
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01004444
Jakub Narebski4df118e2006-10-21 17:53:55 +02004445
4446 print "<div class=\"page_path\">";
4447 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
Martin Koegler00f429a2007-06-03 17:42:44 +02004448 -title => 'tree root'}, to_utf8("[$project]"));
Jakub Narebski4df118e2006-10-21 17:53:55 +02004449 print " / ";
4450 if (defined $name) {
Jakub Narebski762c7202006-09-04 18:17:58 +02004451 my @dirname = split '/', $name;
4452 my $basename = pop @dirname;
4453 my $fullname = '';
4454
Jakub Narebski762c7202006-09-04 18:17:58 +02004455 foreach my $dir (@dirname) {
Petr Baudis16fdb482006-09-21 02:05:50 +02004456 $fullname .= ($fullname ? '/' : '') . $dir;
Jakub Narebski762c7202006-09-04 18:17:58 +02004457 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4458 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004459 -title => $fullname}, esc_path($dir));
Petr Baudis26d0a972006-09-23 01:00:12 +02004460 print " / ";
Jakub Narebski762c7202006-09-04 18:17:58 +02004461 }
4462 if (defined $type && $type eq 'blob') {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004463 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
Jakub Narebski762c7202006-09-04 18:17:58 +02004464 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004465 -title => $name}, esc_path($basename));
Jakub Narebski762c7202006-09-04 18:17:58 +02004466 } elsif (defined $type && $type eq 'tree') {
4467 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4468 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004469 -title => $name}, esc_path($basename));
Jakub Narebski4df118e2006-10-21 17:53:55 +02004470 print " / ";
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004471 } else {
Jakub Narebski403d0902006-11-08 11:48:56 +01004472 print esc_path($basename);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004473 }
Kay Sievers8ed23e12005-08-07 20:05:44 +02004474 }
Jakub Narebski4df118e2006-10-21 17:53:55 +02004475 print "<br/></div>\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02004476}
4477
Jakub Narebski74fd8722009-05-07 19:11:29 +02004478sub git_print_log {
Jakub Narebskid16d0932006-08-17 11:21:23 +02004479 my $log = shift;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004480 my %opts = @_;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004481
Jakub Narebskib7f92532006-08-28 14:48:10 +02004482 if ($opts{'-remove_title'}) {
4483 # remove title, i.e. first line of log
4484 shift @$log;
4485 }
Jakub Narebskid16d0932006-08-17 11:21:23 +02004486 # remove leading empty lines
4487 while (defined $log->[0] && $log->[0] eq "") {
4488 shift @$log;
4489 }
4490
4491 # print log
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004492 my $skip_blank_line = 0;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004493 foreach my $line (@$log) {
Namhyung Kim3d1110a2012-07-04 11:47:25 +09004494 if ($line =~ m/^\s*([A-Z][-A-Za-z]*-[Bb]y|C[Cc]): /) {
Jakub Narebskib7f92532006-08-28 14:48:10 +02004495 if (! $opts{'-remove_signoff'}) {
4496 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004497 $skip_blank_line = 1;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004498 }
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004499 next;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004500 }
4501
Namhyung Kim66c857e2012-07-04 11:47:26 +09004502 if ($line =~ m,\s*([a-z]*link): (https?://\S+),i) {
4503 if (! $opts{'-remove_signoff'}) {
4504 print "<span class=\"signoff\">" . esc_html($1) . ": " .
4505 "<a href=\"" . esc_html($2) . "\">" . esc_html($2) . "</a>" .
4506 "</span><br/>\n";
4507 $skip_blank_line = 1;
4508 }
4509 next;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004510 }
4511
4512 # print only one empty line
4513 # do not print empty line after signoff
4514 if ($line eq "") {
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004515 next if ($skip_blank_line);
4516 $skip_blank_line = 1;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004517 } else {
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004518 $skip_blank_line = 0;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004519 }
Jakub Narebskib7f92532006-08-28 14:48:10 +02004520
4521 print format_log_line_html($line) . "<br/>\n";
4522 }
4523
4524 if ($opts{'-final_empty_line'}) {
4525 # end with single empty line
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004526 print "<br/>\n" unless $skip_blank_line;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004527 }
4528}
4529
Jakub Narebskie33fba42006-12-10 13:25:46 +01004530# return link target (what link points to)
4531sub git_get_link_target {
4532 my $hash = shift;
4533 my $link_target;
4534
4535 # read link
4536 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4537 or return;
4538 {
Jakub Narebski34122b52009-05-11 03:29:40 +02004539 local $/ = undef;
Jakub Narebskie33fba42006-12-10 13:25:46 +01004540 $link_target = <$fd>;
4541 }
4542 close $fd
4543 or return;
4544
4545 return $link_target;
4546}
4547
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004548# given link target, and the directory (basedir) the link is in,
4549# return target of link relative to top directory (top tree);
4550# return undef if it is not possible (including absolute links).
4551sub normalize_link_target {
Jakub Narebski15c54fe2009-05-11 19:45:11 +02004552 my ($link_target, $basedir) = @_;
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004553
4554 # absolute symlinks (beginning with '/') cannot be normalized
4555 return if (substr($link_target, 0, 1) eq '/');
4556
4557 # normalize link target to path from top (root) tree (dir)
4558 my $path;
4559 if ($basedir) {
4560 $path = $basedir . '/' . $link_target;
4561 } else {
4562 # we are in top (root) tree (dir)
4563 $path = $link_target;
4564 }
4565
4566 # remove //, /./, and /../
4567 my @path_parts;
4568 foreach my $part (split('/', $path)) {
4569 # discard '.' and ''
4570 next if (!$part || $part eq '.');
4571 # handle '..'
4572 if ($part eq '..') {
4573 if (@path_parts) {
4574 pop @path_parts;
4575 } else {
4576 # link leads outside repository (outside top dir)
4577 return;
4578 }
4579 } else {
4580 push @path_parts, $part;
4581 }
4582 }
4583 $path = join('/', @path_parts);
4584
4585 return $path;
4586}
Jakub Narebskie33fba42006-12-10 13:25:46 +01004587
Jakub Narebskifa702002006-08-31 00:35:07 +02004588# print tree entry (row of git_tree), but without encompassing <tr> element
4589sub git_print_tree_entry {
4590 my ($t, $basedir, $hash_base, $have_blame) = @_;
4591
4592 my %base_key = ();
Jakub Narebskie33fba42006-12-10 13:25:46 +01004593 $base_key{'hash_base'} = $hash_base if defined $hash_base;
Jakub Narebskifa702002006-08-31 00:35:07 +02004594
Luben Tuikov4de741b2006-09-25 22:38:16 -07004595 # The format of a table row is: mode list link. Where mode is
4596 # the mode of the entry, list is the name of the entry, an href,
4597 # and link is the action links of the entry.
4598
Jakub Narebskifa702002006-08-31 00:35:07 +02004599 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004600 if (exists $t->{'size'}) {
4601 print "<td class=\"size\">$t->{'size'}</td>\n";
4602 }
Jakub Narebskifa702002006-08-31 00:35:07 +02004603 if ($t->{'type'} eq "blob") {
4604 print "<td class=\"list\">" .
Luben Tuikov4de741b2006-09-25 22:38:16 -07004605 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004606 file_name=>"$basedir$t->{'name'}", %base_key),
Jakub Narebskie33fba42006-12-10 13:25:46 +01004607 -class => "list"}, esc_path($t->{'name'}));
4608 if (S_ISLNK(oct $t->{'mode'})) {
4609 my $link_target = git_get_link_target($t->{'hash'});
4610 if ($link_target) {
Jakub Narebski15c54fe2009-05-11 19:45:11 +02004611 my $norm_target = normalize_link_target($link_target, $basedir);
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004612 if (defined $norm_target) {
4613 print " -> " .
4614 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4615 file_name=>$norm_target),
4616 -title => $norm_target}, esc_path($link_target));
4617 } else {
4618 print " -> " . esc_path($link_target);
4619 }
Jakub Narebskie33fba42006-12-10 13:25:46 +01004620 }
4621 }
4622 print "</td>\n";
Luben Tuikov4de741b2006-09-25 22:38:16 -07004623 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02004624 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004625 file_name=>"$basedir$t->{'name'}", %base_key)},
4626 "blob");
Jakub Narebskifa702002006-08-31 00:35:07 +02004627 if ($have_blame) {
Petr Baudis4777b012006-10-24 05:36:10 +02004628 print " | " .
4629 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004630 file_name=>"$basedir$t->{'name'}", %base_key)},
4631 "blame");
Jakub Narebskifa702002006-08-31 00:35:07 +02004632 }
4633 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02004634 print " | " .
4635 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02004636 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4637 "history");
4638 }
4639 print " | " .
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004640 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004641 file_name=>"$basedir$t->{'name'}")},
4642 "raw");
Luben Tuikov4de741b2006-09-25 22:38:16 -07004643 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02004644
4645 } elsif ($t->{'type'} eq "tree") {
Luben Tuikov0fa105e2006-09-26 12:45:37 -07004646 print "<td class=\"list\">";
4647 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004648 file_name=>"$basedir$t->{'name'}",
4649 %base_key)},
Jakub Narebski403d0902006-11-08 11:48:56 +01004650 esc_path($t->{'name'}));
Luben Tuikov0fa105e2006-09-26 12:45:37 -07004651 print "</td>\n";
4652 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02004653 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004654 file_name=>"$basedir$t->{'name'}",
4655 %base_key)},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004656 "tree");
Jakub Narebskifa702002006-08-31 00:35:07 +02004657 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02004658 print " | " .
4659 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02004660 file_name=>"$basedir$t->{'name'}")},
4661 "history");
4662 }
4663 print "</td>\n";
Jakub Narebski01ac1e32007-07-28 16:27:31 +02004664 } else {
4665 # unknown object: we can only present history for it
4666 # (this includes 'commit' object, i.e. submodule support)
4667 print "<td class=\"list\">" .
4668 esc_path($t->{'name'}) .
4669 "</td>\n";
4670 print "<td class=\"link\">";
4671 if (defined $hash_base) {
4672 print $cgi->a({-href => href(action=>"history",
4673 hash_base=>$hash_base,
4674 file_name=>"$basedir$t->{'name'}")},
4675 "history");
4676 }
4677 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02004678 }
4679}
4680
Jakub Narebski717b8312006-07-31 21:22:15 +02004681## ......................................................................
4682## functions printing large fragments of HTML
Kay Sieversede5e102005-08-07 20:23:12 +02004683
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004684# get pre-image filenames for merge (combined) diff
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004685sub fill_from_file_info {
4686 my ($diff, @parents) = @_;
4687
4688 $diff->{'from_file'} = [ ];
4689 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4690 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4691 if ($diff->{'status'}[$i] eq 'R' ||
4692 $diff->{'status'}[$i] eq 'C') {
4693 $diff->{'from_file'}[$i] =
4694 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4695 }
4696 }
4697
4698 return $diff;
4699}
4700
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004701# is current raw difftree line of file deletion
Jakub Narebski90921742007-06-08 13:27:42 +02004702sub is_deleted {
4703 my $diffinfo = shift;
4704
Jakub Narebski4ed4a342008-04-05 21:13:24 +01004705 return $diffinfo->{'to_id'} eq ('0' x 40);
Jakub Narebski90921742007-06-08 13:27:42 +02004706}
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004707
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004708# does patch correspond to [previous] difftree raw line
4709# $diffinfo - hashref of parsed raw diff format
4710# $patchinfo - hashref of parsed patch diff format
4711# (the same keys as in $diffinfo)
4712sub is_patch_split {
4713 my ($diffinfo, $patchinfo) = @_;
4714
4715 return defined $diffinfo && defined $patchinfo
Jakub Narebski9d301452007-11-01 12:38:08 +01004716 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004717}
4718
4719
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004720sub git_difftree_body {
Jakub Narebskied224de2007-05-07 01:10:04 +02004721 my ($difftree, $hash, @parents) = @_;
4722 my ($parent) = $parents[0];
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004723 my $have_blame = gitweb_check_feature('blame');
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004724 print "<div class=\"list_head\">\n";
4725 if ($#{$difftree} > 10) {
4726 print(($#{$difftree} + 1) . " files changed:\n");
4727 }
4728 print "</div>\n";
4729
Jakub Narebskied224de2007-05-07 01:10:04 +02004730 print "<table class=\"" .
4731 (@parents > 1 ? "combined " : "") .
4732 "diff_tree\">\n";
Jakub Narebski47598d72007-06-08 13:24:56 +02004733
4734 # header only for combined diff in 'commitdiff' view
Jakub Narebski3ef408a2007-09-08 21:54:28 +02004735 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
Jakub Narebski47598d72007-06-08 13:24:56 +02004736 if ($has_header) {
4737 # table header
4738 print "<thead><tr>\n" .
4739 "<th></th><th></th>\n"; # filename, patchN link
4740 for (my $i = 0; $i < @parents; $i++) {
4741 my $par = $parents[$i];
4742 print "<th>" .
4743 $cgi->a({-href => href(action=>"commitdiff",
4744 hash=>$hash, hash_parent=>$par),
4745 -title => 'commitdiff to parent number ' .
4746 ($i+1) . ': ' . substr($par,0,7)},
4747 $i+1) .
4748 "&nbsp;</th>\n";
4749 }
4750 print "</tr></thead>\n<tbody>\n";
4751 }
4752
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004753 my $alternate = 1;
Jakub Narebskib4657e72006-08-28 14:48:14 +02004754 my $patchno = 0;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004755 foreach my $line (@{$difftree}) {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004756 my $diff = parsed_difftree_line($line);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004757
4758 if ($alternate) {
4759 print "<tr class=\"dark\">\n";
4760 } else {
4761 print "<tr class=\"light\">\n";
4762 }
4763 $alternate ^= 1;
4764
Jakub Narebski493e01d2007-05-07 01:10:06 +02004765 if (exists $diff->{'nparents'}) { # combined diff
Jakub Narebskied224de2007-05-07 01:10:04 +02004766
Jakub Narebski493e01d2007-05-07 01:10:06 +02004767 fill_from_file_info($diff, @parents)
4768 unless exists $diff->{'from_file'};
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004769
Jakub Narebski90921742007-06-08 13:27:42 +02004770 if (!is_deleted($diff)) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004771 # file exists in the result (child) commit
4772 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02004773 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4774 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004775 hash_base=>$hash),
Jakub Narebski493e01d2007-05-07 01:10:06 +02004776 -class => "list"}, esc_path($diff->{'to_file'})) .
Jakub Narebskied224de2007-05-07 01:10:04 +02004777 "</td>\n";
4778 } else {
4779 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02004780 esc_path($diff->{'to_file'}) .
Jakub Narebskied224de2007-05-07 01:10:04 +02004781 "</td>\n";
4782 }
4783
4784 if ($action eq 'commitdiff') {
4785 # link to patch
4786 $patchno++;
4787 print "<td class=\"link\">" .
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004788 $cgi->a({-href => href(-anchor=>"patch$patchno")},
4789 "patch") .
Jakub Narebskied224de2007-05-07 01:10:04 +02004790 " | " .
4791 "</td>\n";
4792 }
4793
4794 my $has_history = 0;
4795 my $not_deleted = 0;
Jakub Narebski493e01d2007-05-07 01:10:06 +02004796 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004797 my $hash_parent = $parents[$i];
Jakub Narebski493e01d2007-05-07 01:10:06 +02004798 my $from_hash = $diff->{'from_id'}[$i];
4799 my $from_path = $diff->{'from_file'}[$i];
4800 my $status = $diff->{'status'}[$i];
Jakub Narebskied224de2007-05-07 01:10:04 +02004801
4802 $has_history ||= ($status ne 'A');
4803 $not_deleted ||= ($status ne 'D');
4804
Jakub Narebskied224de2007-05-07 01:10:04 +02004805 if ($status eq 'A') {
4806 print "<td class=\"link\" align=\"right\"> | </td>\n";
4807 } elsif ($status eq 'D') {
4808 print "<td class=\"link\">" .
4809 $cgi->a({-href => href(action=>"blob",
4810 hash_base=>$hash,
4811 hash=>$from_hash,
4812 file_name=>$from_path)},
4813 "blob" . ($i+1)) .
4814 " | </td>\n";
4815 } else {
Jakub Narebski493e01d2007-05-07 01:10:06 +02004816 if ($diff->{'to_id'} eq $from_hash) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004817 print "<td class=\"link nochange\">";
4818 } else {
4819 print "<td class=\"link\">";
4820 }
4821 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004822 hash=>$diff->{'to_id'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004823 hash_parent=>$from_hash,
4824 hash_base=>$hash,
4825 hash_parent_base=>$hash_parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004826 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004827 file_parent=>$from_path)},
4828 "diff" . ($i+1)) .
4829 " | </td>\n";
4830 }
4831 }
4832
4833 print "<td class=\"link\">";
4834 if ($not_deleted) {
4835 print $cgi->a({-href => href(action=>"blob",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004836 hash=>$diff->{'to_id'},
4837 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004838 hash_base=>$hash)},
4839 "blob");
4840 print " | " if ($has_history);
4841 }
4842 if ($has_history) {
4843 print $cgi->a({-href => href(action=>"history",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004844 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004845 hash_base=>$hash)},
4846 "history");
4847 }
4848 print "</td>\n";
4849
4850 print "</tr>\n";
4851 next; # instead of 'else' clause, to avoid extra indent
4852 }
4853 # else ordinary diff
4854
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004855 my ($to_mode_oct, $to_mode_str, $to_file_type);
4856 my ($from_mode_oct, $from_mode_str, $from_file_type);
Jakub Narebski493e01d2007-05-07 01:10:06 +02004857 if ($diff->{'to_mode'} ne ('0' x 6)) {
4858 $to_mode_oct = oct $diff->{'to_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004859 if (S_ISREG($to_mode_oct)) { # only for regular file
4860 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004861 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004862 $to_file_type = file_type($diff->{'to_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004863 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004864 if ($diff->{'from_mode'} ne ('0' x 6)) {
4865 $from_mode_oct = oct $diff->{'from_mode'};
Ævar Arnfjörð Bjarmason98885c22011-02-19 15:27:42 +00004866 if (S_ISREG($from_mode_oct)) { # only for regular file
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004867 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4868 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004869 $from_file_type = file_type($diff->{'from_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004870 }
4871
Jakub Narebski493e01d2007-05-07 01:10:06 +02004872 if ($diff->{'status'} eq "A") { # created
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004873 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4874 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4875 $mode_chng .= "]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07004876 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004877 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4878 hash_base=>$hash, file_name=>$diff->{'file'}),
4879 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07004880 print "</td>\n";
4881 print "<td>$mode_chng</td>\n";
4882 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02004883 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02004884 # link to patch
4885 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004886 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4887 "patch") .
4888 " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02004889 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004890 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4891 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski3faa5412007-01-08 02:10:42 +01004892 "blob");
Jakub Narebskib4657e72006-08-28 14:48:14 +02004893 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004894
Jakub Narebski493e01d2007-05-07 01:10:06 +02004895 } elsif ($diff->{'status'} eq "D") { # deleted
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004896 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07004897 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004898 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4899 hash_base=>$parent, file_name=>$diff->{'file'}),
4900 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07004901 print "</td>\n";
4902 print "<td>$mode_chng</td>\n";
4903 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02004904 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02004905 # link to patch
4906 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004907 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4908 "patch") .
4909 " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02004910 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004911 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4912 hash_base=>$parent, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01004913 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08004914 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01004915 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004916 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01004917 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08004918 }
Jakub Narebskib4657e72006-08-28 14:48:14 +02004919 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004920 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004921 "history");
Luben Tuikov499faed2006-09-27 17:23:25 -07004922 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004923
Jakub Narebski493e01d2007-05-07 01:10:06 +02004924 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004925 my $mode_chnge = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004926 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004927 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
Jakub Narebski6e72cf42007-01-03 20:47:24 +01004928 if ($from_file_type ne $to_file_type) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004929 $mode_chnge .= " from $from_file_type to $to_file_type";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004930 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004931 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4932 if ($from_mode_str && $to_mode_str) {
4933 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4934 } elsif ($to_mode_str) {
4935 $mode_chnge .= " mode: $to_mode_str";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004936 }
4937 }
4938 $mode_chnge .= "]</span>\n";
4939 }
4940 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004941 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4942 hash_base=>$hash, file_name=>$diff->{'file'}),
4943 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07004944 print "</td>\n";
4945 print "<td>$mode_chnge</td>\n";
4946 print "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01004947 if ($action eq 'commitdiff') {
4948 # link to patch
4949 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004950 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4951 "patch") .
Jakub Narebski241cc592006-10-31 17:36:27 +01004952 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004953 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01004954 # "commit" view and modified file (not onlu mode changed)
4955 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004956 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01004957 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004958 file_name=>$diff->{'file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01004959 "diff") .
4960 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004961 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004962 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4963 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01004964 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08004965 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01004966 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004967 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01004968 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08004969 }
Luben Tuikoveb51ec92006-09-27 17:24:49 -07004970 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004971 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004972 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004973 print "</td>\n";
4974
Jakub Narebski493e01d2007-05-07 01:10:06 +02004975 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004976 my %status_name = ('R' => 'moved', 'C' => 'copied');
Jakub Narebski493e01d2007-05-07 01:10:06 +02004977 my $nstatus = $status_name{$diff->{'status'}};
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004978 my $mode_chng = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004979 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004980 # mode also for directories, so we cannot use $to_mode_str
4981 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004982 }
4983 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004984 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004985 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4986 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004987 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4988 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004989 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4990 -class => "list"}, esc_path($diff->{'from_file'})) .
4991 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
Luben Tuikov499faed2006-09-27 17:23:25 -07004992 "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01004993 if ($action eq 'commitdiff') {
4994 # link to patch
4995 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004996 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4997 "patch") .
Jakub Narebski241cc592006-10-31 17:36:27 +01004998 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004999 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01005000 # "commit" view and modified file (not only pure rename or copy)
5001 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02005002 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01005003 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005004 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01005005 "diff") .
5006 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005007 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02005008 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5009 hash_base=>$parent, file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005010 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005011 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01005012 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005013 file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005014 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005015 }
Jakub Narebski897d1d22006-11-19 22:51:39 +01005016 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005017 file_name=>$diff->{'to_file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02005018 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005019 print "</td>\n";
5020
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005021 } # we should not encounter Unmerged (U) or Unknown (X) status
5022 print "</tr>\n";
5023 }
Jakub Narebski47598d72007-06-08 13:24:56 +02005024 print "</tbody>" if $has_header;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005025 print "</table>\n";
5026}
5027
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005028# Print context lines and then rem/add lines in a side-by-side manner.
5029sub print_sidebyside_diff_lines {
5030 my ($ctx, $rem, $add) = @_;
5031
5032 # print context block before add/rem block
5033 if (@$ctx) {
5034 print join '',
5035 '<div class="chunk_block ctx">',
5036 '<div class="old">',
5037 @$ctx,
5038 '</div>',
5039 '<div class="new">',
5040 @$ctx,
5041 '</div>',
5042 '</div>';
5043 }
5044
5045 if (!@$add) {
5046 # pure removal
5047 print join '',
5048 '<div class="chunk_block rem">',
5049 '<div class="old">',
5050 @$rem,
5051 '</div>',
5052 '</div>';
5053 } elsif (!@$rem) {
5054 # pure addition
5055 print join '',
5056 '<div class="chunk_block add">',
5057 '<div class="new">',
5058 @$add,
5059 '</div>',
5060 '</div>';
5061 } else {
5062 print join '',
5063 '<div class="chunk_block chg">',
5064 '<div class="old">',
5065 @$rem,
5066 '</div>',
5067 '<div class="new">',
5068 @$add,
5069 '</div>',
5070 '</div>';
5071 }
5072}
5073
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005074# Print context lines and then rem/add lines in inline manner.
5075sub print_inline_diff_lines {
5076 my ($ctx, $rem, $add) = @_;
5077
5078 print @$ctx, @$rem, @$add;
5079}
5080
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005081# Format removed and added line, mark changed part and HTML-format them.
5082# Implementation is based on contrib/diff-highlight
5083sub format_rem_add_lines_pair {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005084 my ($rem, $add, $num_parents) = @_;
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005085
5086 # We need to untabify lines before split()'ing them;
5087 # otherwise offsets would be invalid.
5088 chomp $rem;
5089 chomp $add;
5090 $rem = untabify($rem);
5091 $add = untabify($add);
5092
5093 my @rem = split(//, $rem);
5094 my @add = split(//, $add);
5095 my ($esc_rem, $esc_add);
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005096 # Ignore leading +/- characters for each parent.
5097 my ($prefix_len, $suffix_len) = ($num_parents, 0);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005098 my ($prefix_has_nonspace, $suffix_has_nonspace);
5099
5100 my $shorter = (@rem < @add) ? @rem : @add;
5101 while ($prefix_len < $shorter) {
5102 last if ($rem[$prefix_len] ne $add[$prefix_len]);
5103
5104 $prefix_has_nonspace = 1 if ($rem[$prefix_len] !~ /\s/);
5105 $prefix_len++;
5106 }
5107
5108 while ($prefix_len + $suffix_len < $shorter) {
5109 last if ($rem[-1 - $suffix_len] ne $add[-1 - $suffix_len]);
5110
5111 $suffix_has_nonspace = 1 if ($rem[-1 - $suffix_len] !~ /\s/);
5112 $suffix_len++;
5113 }
5114
5115 # Mark lines that are different from each other, but have some common
5116 # part that isn't whitespace. If lines are completely different, don't
5117 # mark them because that would make output unreadable, especially if
5118 # diff consists of multiple lines.
5119 if ($prefix_has_nonspace || $suffix_has_nonspace) {
5120 $esc_rem = esc_html_hl_regions($rem, 'marked',
5121 [$prefix_len, @rem - $suffix_len], -nbsp=>1);
5122 $esc_add = esc_html_hl_regions($add, 'marked',
5123 [$prefix_len, @add - $suffix_len], -nbsp=>1);
5124 } else {
5125 $esc_rem = esc_html($rem, -nbsp=>1);
5126 $esc_add = esc_html($add, -nbsp=>1);
5127 }
5128
5129 return format_diff_line(\$esc_rem, 'rem'),
5130 format_diff_line(\$esc_add, 'add');
5131}
5132
5133# HTML-format diff context, removed and added lines.
5134sub format_ctx_rem_add_lines {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005135 my ($ctx, $rem, $add, $num_parents) = @_;
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005136 my (@new_ctx, @new_rem, @new_add);
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005137 my $can_highlight = 0;
5138 my $is_combined = ($num_parents > 1);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005139
5140 # Highlight if every removed line has a corresponding added line.
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005141 if (@$add > 0 && @$add == @$rem) {
5142 $can_highlight = 1;
5143
5144 # Highlight lines in combined diff only if the chunk contains
5145 # diff between the same version, e.g.
5146 #
5147 # - a
5148 # - b
5149 # + c
5150 # + d
5151 #
5152 # Otherwise the highlightling would be confusing.
5153 if ($is_combined) {
5154 for (my $i = 0; $i < @$add; $i++) {
5155 my $prefix_rem = substr($rem->[$i], 0, $num_parents);
5156 my $prefix_add = substr($add->[$i], 0, $num_parents);
5157
5158 $prefix_rem =~ s/-/+/g;
5159
5160 if ($prefix_rem ne $prefix_add) {
5161 $can_highlight = 0;
5162 last;
5163 }
5164 }
5165 }
5166 }
5167
5168 if ($can_highlight) {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005169 for (my $i = 0; $i < @$add; $i++) {
5170 my ($line_rem, $line_add) = format_rem_add_lines_pair(
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005171 $rem->[$i], $add->[$i], $num_parents);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005172 push @new_rem, $line_rem;
5173 push @new_add, $line_add;
5174 }
5175 } else {
5176 @new_rem = map { format_diff_line($_, 'rem') } @$rem;
5177 @new_add = map { format_diff_line($_, 'add') } @$add;
5178 }
5179
5180 @new_ctx = map { format_diff_line($_, 'ctx') } @$ctx;
5181
5182 return (\@new_ctx, \@new_rem, \@new_add);
5183}
5184
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005185# Print context lines and then rem/add lines.
5186sub print_diff_lines {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005187 my ($ctx, $rem, $add, $diff_style, $num_parents) = @_;
5188 my $is_combined = $num_parents > 1;
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005189
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005190 ($ctx, $rem, $add) = format_ctx_rem_add_lines($ctx, $rem, $add,
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005191 $num_parents);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005192
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005193 if ($diff_style eq 'sidebyside' && !$is_combined) {
5194 print_sidebyside_diff_lines($ctx, $rem, $add);
5195 } else {
5196 # default 'inline' style and unknown styles
5197 print_inline_diff_lines($ctx, $rem, $add);
5198 }
5199}
5200
5201sub print_diff_chunk {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005202 my ($diff_style, $num_parents, $from, $to, @chunk) = @_;
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005203 my (@ctx, @rem, @add);
5204
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005205 # The class of the previous line.
5206 my $prev_class = '';
5207
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005208 return unless @chunk;
5209
5210 # incomplete last line might be among removed or added lines,
5211 # or both, or among context lines: find which
5212 for (my $i = 1; $i < @chunk; $i++) {
5213 if ($chunk[$i][0] eq 'incomplete') {
5214 $chunk[$i][0] = $chunk[$i-1][0];
5215 }
5216 }
5217
5218 # guardian
5219 push @chunk, ["", ""];
5220
5221 foreach my $line_info (@chunk) {
5222 my ($class, $line) = @$line_info;
5223
5224 # print chunk headers
5225 if ($class && $class eq 'chunk_header') {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005226 print format_diff_line($line, $class, $from, $to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005227 next;
5228 }
5229
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005230 ## print from accumulator when have some add/rem lines or end
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005231 # of chunk (flush context lines), or when have add and rem
5232 # lines and new block is reached (otherwise add/rem lines could
5233 # be reordered)
5234 if (!$class || ((@rem || @add) && $class eq 'ctx') ||
5235 (@rem && @add && $class ne $prev_class)) {
5236 print_diff_lines(\@ctx, \@rem, \@add,
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005237 $diff_style, $num_parents);
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005238 @ctx = @rem = @add = ();
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005239 }
5240
5241 ## adding lines to accumulator
5242 # guardian value
5243 last unless $line;
5244 # rem, add or change
5245 if ($class eq 'rem') {
5246 push @rem, $line;
5247 } elsif ($class eq 'add') {
5248 push @add, $line;
5249 }
5250 # context line
5251 if ($class eq 'ctx') {
5252 push @ctx, $line;
5253 }
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005254
5255 $prev_class = $class;
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005256 }
5257}
5258
Jakub Narebskieee08902006-08-24 00:15:14 +02005259sub git_patchset_body {
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005260 my ($fd, $diff_style, $difftree, $hash, @hash_parents) = @_;
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02005261 my ($hash_parent) = $hash_parents[0];
Jakub Narebskieee08902006-08-24 00:15:14 +02005262
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005263 my $is_combined = (@hash_parents > 1);
Jakub Narebskieee08902006-08-24 00:15:14 +02005264 my $patch_idx = 0;
Martin Koegler4280cde2007-04-22 22:49:25 -07005265 my $patch_number = 0;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005266 my $patch_line;
Jakub Narebskife875852006-08-25 20:59:39 +02005267 my $diffinfo;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005268 my $to_name;
Jakub Narebski744d0ac2006-11-08 17:59:41 +01005269 my (%from, %to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005270 my @chunk; # for side-by-side diff
Jakub Narebskieee08902006-08-24 00:15:14 +02005271
5272 print "<div class=\"patchset\">\n";
5273
Jakub Narebski6d55f052006-11-18 23:35:39 +01005274 # skip to first patch
5275 while ($patch_line = <$fd>) {
Jakub Narebski157e43b2006-08-24 19:34:36 +02005276 chomp $patch_line;
Jakub Narebskieee08902006-08-24 00:15:14 +02005277
Jakub Narebski6d55f052006-11-18 23:35:39 +01005278 last if ($patch_line =~ m/^diff /);
5279 }
5280
5281 PATCH:
5282 while ($patch_line) {
Jakub Narebski6d55f052006-11-18 23:35:39 +01005283
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005284 # parse "git diff" header line
5285 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
5286 # $1 is from_name, which we do not use
5287 $to_name = unquote($2);
5288 $to_name =~ s!^b/!!;
5289 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
5290 # $1 is 'cc' or 'combined', which we do not use
5291 $to_name = unquote($2);
5292 } else {
5293 $to_name = undef;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005294 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01005295
5296 # check if current patch belong to current raw line
5297 # and parse raw git-diff line if needed
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005298 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
Jakub Narebski22065372007-05-12 12:42:32 +02005299 # this is continuation of a split patch
Jakub Narebski6d55f052006-11-18 23:35:39 +01005300 print "<div class=\"patch cont\">\n";
5301 } else {
5302 # advance raw git-diff output if needed
5303 $patch_idx++ if defined $diffinfo;
Jakub Narebskieee08902006-08-24 00:15:14 +02005304
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005305 # read and prepare patch information
5306 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5307
Jakub Narebskicd030c32007-06-08 13:33:28 +02005308 # compact combined diff output can have some patches skipped
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005309 # find which patch (using pathname of result) we are at now;
5310 if ($is_combined) {
5311 while ($to_name ne $diffinfo->{'to_file'}) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02005312 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5313 format_diff_cc_simplified($diffinfo, @hash_parents) .
5314 "</div>\n"; # class="patch"
5315
5316 $patch_idx++;
5317 $patch_number++;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005318
5319 last if $patch_idx > $#$difftree;
5320 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02005321 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005322 }
Jakub Narebski711fa742007-09-08 21:49:11 +02005323
Jakub Narebski90921742007-06-08 13:27:42 +02005324 # modifies %from, %to hashes
5325 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
Jakub Narebski5f855052007-05-17 22:54:28 +02005326
Jakub Narebski6d55f052006-11-18 23:35:39 +01005327 # this is first patch for raw difftree line with $patch_idx index
5328 # we index @$difftree array from 0, but number patches from 1
5329 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
Jakub Narebski744d0ac2006-11-08 17:59:41 +01005330 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005331
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005332 # git diff header
5333 #assert($patch_line =~ m/^diff /) if DEBUG;
5334 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
5335 $patch_number++;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005336 # print "git diff" header
Jakub Narebski90921742007-06-08 13:27:42 +02005337 print format_git_diff_header_line($patch_line, $diffinfo,
5338 \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02005339
Jakub Narebski6d55f052006-11-18 23:35:39 +01005340 # print extended diff header
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005341 print "<div class=\"diff extended_header\">\n";
Jakub Narebski6d55f052006-11-18 23:35:39 +01005342 EXTENDED_HEADER:
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005343 while ($patch_line = <$fd>) {
5344 chomp $patch_line;
5345
5346 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
5347
Jakub Narebski90921742007-06-08 13:27:42 +02005348 print format_extended_diff_header_line($patch_line, $diffinfo,
5349 \%from, \%to);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005350 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005351 print "</div>\n"; # class="diff extended_header"
Jakub Narebskie4e4f822006-08-25 21:04:13 +02005352
Jakub Narebski6d55f052006-11-18 23:35:39 +01005353 # from-file/to-file diff header
Jakub Narebski0bdb28c2007-01-10 00:07:43 +01005354 if (! $patch_line) {
5355 print "</div>\n"; # class="patch"
5356 last PATCH;
5357 }
Jakub Narebski66399ef2007-01-07 02:52:25 +01005358 next PATCH if ($patch_line =~ m/^diff /);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005359 #assert($patch_line =~ m/^---/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005360
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005361 my $last_patch_line = $patch_line;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005362 $patch_line = <$fd>;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005363 chomp $patch_line;
Jakub Narebski90921742007-06-08 13:27:42 +02005364 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005365
Jakub Narebski90921742007-06-08 13:27:42 +02005366 print format_diff_from_to_header($last_patch_line, $patch_line,
Jakub Narebski91af4ce2007-06-08 13:32:44 +02005367 $diffinfo, \%from, \%to,
5368 @hash_parents);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005369
5370 # the patch itself
5371 LINE:
5372 while ($patch_line = <$fd>) {
5373 chomp $patch_line;
5374
5375 next PATCH if ($patch_line =~ m/^diff /);
5376
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02005377 my $class = diff_line_class($patch_line, \%from, \%to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005378
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005379 if ($class eq 'chunk_header') {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005380 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005381 @chunk = ();
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005382 }
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005383
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02005384 push @chunk, [ $class, $patch_line ];
Jakub Narebskieee08902006-08-24 00:15:14 +02005385 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005386
Jakub Narebski6d55f052006-11-18 23:35:39 +01005387 } continue {
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005388 if (@chunk) {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005389 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005390 @chunk = ();
5391 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01005392 print "</div>\n"; # class="patch"
Jakub Narebskieee08902006-08-24 00:15:14 +02005393 }
Jakub Narebskid26c4262007-05-17 00:05:55 +02005394
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02005395 # for compact combined (--cc) format, with chunk and patch simplification
5396 # the patchset might be empty, but there might be unprocessed raw lines
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005397 for (++$patch_idx if $patch_number > 0;
Jakub Narebskicd030c32007-06-08 13:33:28 +02005398 $patch_idx < @$difftree;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005399 ++$patch_idx) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02005400 # read and prepare patch information
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005401 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02005402
5403 # generate anchor for "patch" links in difftree / whatchanged part
5404 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5405 format_diff_cc_simplified($diffinfo, @hash_parents) .
5406 "</div>\n"; # class="patch"
5407
5408 $patch_number++;
5409 }
5410
Jakub Narebskid26c4262007-05-17 00:05:55 +02005411 if ($patch_number == 0) {
5412 if (@hash_parents > 1) {
5413 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
5414 } else {
5415 print "<div class=\"diff nodifferences\">No differences found</div>\n";
5416 }
5417 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005418
5419 print "</div>\n"; # class="patchset"
5420}
5421
5422# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5423
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005424sub git_project_search_form {
Jakub Narebskib22939a2012-03-02 23:50:01 +01005425 my ($searchtext, $search_use_regexp) = @_;
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005426
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005427 my $limit = '';
5428 if ($project_filter) {
5429 $limit = " in '$project_filter/'";
5430 }
5431
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005432 print "<div class=\"projsearch\">\n";
5433 print $cgi->startform(-method => 'get', -action => $my_uri) .
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005434 $cgi->hidden(-name => 'a', -value => 'project_list') . "\n";
5435 print $cgi->hidden(-name => 'pf', -value => $project_filter). "\n"
5436 if (defined $project_filter);
5437 print $cgi->textfield(-name => 's', -value => $searchtext,
5438 -title => "Search project by name and description$limit",
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005439 -size => 60) . "\n" .
5440 "<span title=\"Extended regular expression\">" .
5441 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
5442 -checked => $search_use_regexp) .
5443 "</span>\n" .
5444 $cgi->submit(-name => 'btnS', -value => 'Search') .
5445 $cgi->end_form() . "\n" .
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005446 $cgi->a({-href => href(project => undef, searchtext => undef,
5447 project_filter => $project_filter)},
5448 esc_html("List all projects$limit")) . "<br />\n";
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005449 print "</div>\n";
5450}
5451
Jakub Narebski14b289b2012-02-23 16:54:46 +01005452# entry for given @keys needs filling if at least one of keys in list
5453# is not present in %$project_info
5454sub project_info_needs_filling {
5455 my ($project_info, @keys) = @_;
5456
5457 # return List::MoreUtils::any { !exists $project_info->{$_} } @keys;
5458 foreach my $key (@keys) {
5459 if (!exists $project_info->{$key}) {
5460 return 1;
5461 }
5462 }
5463 return;
5464}
5465
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005466# fills project list info (age, description, owner, category, forks, etc.)
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005467# for each project in the list, removing invalid projects from
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005468# returned list, or fill only specified info.
5469#
5470# Invalid projects are removed from the returned list if and only if you
5471# ask 'age' or 'age_string' to be filled, because they are the only fields
5472# that run unconditionally git command that requires repository, and
5473# therefore do always check if project repository is invalid.
5474#
5475# USAGE:
5476# * fill_project_list_info(\@project_list, 'descr_long', 'ctags')
5477# ensures that 'descr_long' and 'ctags' fields are filled
5478# * @project_list = fill_project_list_info(\@project_list)
5479# ensures that all fields are filled (and invalid projects removed)
5480#
Jakub Narebski69913412008-06-10 19:21:01 +02005481# NOTE: modifies $projlist, but does not remove entries from it
5482sub fill_project_list_info {
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005483 my ($projlist, @wanted_keys) = @_;
Petr Baudise30496d2006-10-24 05:33:17 +02005484 my @projects;
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005485 my $filter_set = sub { return @_; };
5486 if (@wanted_keys) {
5487 my %wanted_keys = map { $_ => 1 } @wanted_keys;
5488 $filter_set = sub { return grep { $wanted_keys{$_} } @_; };
5489 }
Jakub Narebski69913412008-06-10 19:21:01 +02005490
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005491 my $show_ctags = gitweb_check_feature('ctags');
Jakub Narebski69913412008-06-10 19:21:01 +02005492 PROJECT:
Petr Baudise30496d2006-10-24 05:33:17 +02005493 foreach my $pr (@$projlist) {
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005494 if (project_info_needs_filling($pr, $filter_set->('age', 'age_string'))) {
Jakub Narebski14b289b2012-02-23 16:54:46 +01005495 my (@activity) = git_get_last_activity($pr->{'path'});
5496 unless (@activity) {
5497 next PROJECT;
5498 }
5499 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
Petr Baudise30496d2006-10-24 05:33:17 +02005500 }
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005501 if (project_info_needs_filling($pr, $filter_set->('descr', 'descr_long'))) {
Petr Baudise30496d2006-10-24 05:33:17 +02005502 my $descr = git_get_project_description($pr->{'path'}) || "";
Jakub Narebski69913412008-06-10 19:21:01 +02005503 $descr = to_utf8($descr);
5504 $pr->{'descr_long'} = $descr;
Michael Hendricks55feb122007-07-04 18:36:48 -06005505 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
Petr Baudise30496d2006-10-24 05:33:17 +02005506 }
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005507 if (project_info_needs_filling($pr, $filter_set->('owner'))) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02005508 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
Petr Baudise30496d2006-10-24 05:33:17 +02005509 }
Jakub Narebski14b289b2012-02-23 16:54:46 +01005510 if ($show_ctags &&
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005511 project_info_needs_filling($pr, $filter_set->('ctags'))) {
Jakub Narebski12b14432011-04-29 19:51:56 +02005512 $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
Petr Baudise30496d2006-10-24 05:33:17 +02005513 }
Jakub Narebski14b289b2012-02-23 16:54:46 +01005514 if ($projects_list_group_categories &&
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005515 project_info_needs_filling($pr, $filter_set->('category'))) {
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005516 my $cat = git_get_project_category($pr->{'path'}) ||
5517 $project_list_default_category;
5518 $pr->{'category'} = to_utf8($cat);
5519 }
5520
Petr Baudise30496d2006-10-24 05:33:17 +02005521 push @projects, $pr;
5522 }
5523
Jakub Narebski69913412008-06-10 19:21:01 +02005524 return @projects;
5525}
5526
Jakub Narebski12b14432011-04-29 19:51:56 +02005527sub sort_projects_list {
5528 my ($projlist, $order) = @_;
5529 my @projects;
5530
5531 my %order_info = (
5532 project => { key => 'path', type => 'str' },
5533 descr => { key => 'descr_long', type => 'str' },
5534 owner => { key => 'owner', type => 'str' },
5535 age => { key => 'age', type => 'num' }
5536 );
5537 my $oi = $order_info{$order};
5538 return @$projlist unless defined $oi;
5539 if ($oi->{'type'} eq 'str') {
5540 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @$projlist;
5541 } else {
5542 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @$projlist;
5543 }
5544
5545 return @projects;
5546}
5547
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005548# returns a hash of categories, containing the list of project
5549# belonging to each category
5550sub build_projlist_by_category {
5551 my ($projlist, $from, $to) = @_;
5552 my %categories;
5553
5554 $from = 0 unless defined $from;
5555 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5556
5557 for (my $i = $from; $i <= $to; $i++) {
5558 my $pr = $projlist->[$i];
5559 push @{$categories{ $pr->{'category'} }}, $pr;
5560 }
5561
5562 return wantarray ? %categories : \%categories;
5563}
5564
Petr Baudis6b28da62008-09-25 18:48:37 +02005565# print 'sort by' <th> element, generating 'sort by $name' replay link
5566# if that order is not selected
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005567sub print_sort_th {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005568 print format_sort_th(@_);
5569}
5570
5571sub format_sort_th {
Petr Baudis6b28da62008-09-25 18:48:37 +02005572 my ($name, $order, $header) = @_;
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005573 my $sort_th = "";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005574 $header ||= ucfirst($name);
5575
5576 if ($order eq $name) {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005577 $sort_th .= "<th>$header</th>\n";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005578 } else {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005579 $sort_th .= "<th>" .
5580 $cgi->a({-href => href(-replay=>1, order=>$name),
5581 -class => "header"}, $header) .
5582 "</th>\n";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005583 }
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005584
5585 return $sort_th;
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005586}
5587
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005588sub git_project_list_rows {
5589 my ($projlist, $from, $to, $check_forks) = @_;
5590
5591 $from = 0 unless defined $from;
5592 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5593
5594 my $alternate = 1;
5595 for (my $i = $from; $i <= $to; $i++) {
5596 my $pr = $projlist->[$i];
5597
5598 if ($alternate) {
5599 print "<tr class=\"dark\">\n";
5600 } else {
5601 print "<tr class=\"light\">\n";
5602 }
5603 $alternate ^= 1;
5604
5605 if ($check_forks) {
5606 print "<td>";
5607 if ($pr->{'forks'}) {
5608 my $nforks = scalar @{$pr->{'forks'}};
5609 if ($nforks > 0) {
5610 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5611 -title => "$nforks forks"}, "+");
5612 } else {
5613 print $cgi->span({-title => "$nforks forks"}, "+");
5614 }
5615 }
5616 print "</td>\n";
5617 }
5618 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
Jakub Narebski07a40062012-02-27 02:55:20 +01005619 -class => "list"},
5620 esc_html_match_hl($pr->{'path'}, $search_regexp)) .
5621 "</td>\n" .
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005622 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005623 -class => "list",
Jakub Narebskie607b792012-02-27 02:55:22 +01005624 -title => $pr->{'descr_long'}},
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005625 $search_regexp
Jakub Narebskie607b792012-02-27 02:55:22 +01005626 ? esc_html_match_hl_chopped($pr->{'descr_long'},
5627 $pr->{'descr'}, $search_regexp)
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005628 : esc_html($pr->{'descr'})) .
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005629 "</td>\n";
5630 unless ($omit_owner) {
5631 print "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5632 }
Kacper Kornet5710be42012-04-24 19:39:15 +02005633 unless ($omit_age_column) {
5634 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5635 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n";
5636 }
5637 print"<td class=\"link\">" .
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005638 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
5639 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5640 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5641 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5642 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5643 "</td>\n" .
5644 "</tr>\n";
5645 }
5646}
5647
Jakub Narebski69913412008-06-10 19:21:01 +02005648sub git_project_list_body {
Petr Baudis42326112008-10-02 17:17:01 +02005649 # actually uses global variable $project
Jakub Narebski69913412008-06-10 19:21:01 +02005650 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
Jakub Narebski12b14432011-04-29 19:51:56 +02005651 my @projects = @$projlist;
Jakub Narebski69913412008-06-10 19:21:01 +02005652
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005653 my $check_forks = gitweb_check_feature('forks');
Jakub Narebski12b14432011-04-29 19:51:56 +02005654 my $show_ctags = gitweb_check_feature('ctags');
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01005655 my $tagfilter = $show_ctags ? $input_params{'ctag'} : undef;
Jakub Narebski12b14432011-04-29 19:51:56 +02005656 $check_forks = undef
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005657 if ($tagfilter || $search_regexp);
Jakub Narebski12b14432011-04-29 19:51:56 +02005658
5659 # filtering out forks before filling info allows to do less work
5660 @projects = filter_forks_from_projects_list(\@projects)
5661 if ($check_forks);
Jakub Narebski07b257f2012-02-23 16:54:48 +01005662 # search_projects_list pre-fills required info
Jakub Narebski12b14432011-04-29 19:51:56 +02005663 @projects = search_projects_list(\@projects,
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005664 'search_regexp' => $search_regexp,
Jakub Narebski12b14432011-04-29 19:51:56 +02005665 'tagfilter' => $tagfilter)
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005666 if ($tagfilter || $search_regexp);
Jakub Narebski07b257f2012-02-23 16:54:48 +01005667 # fill the rest
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005668 my @all_fields = ('descr', 'descr_long', 'ctags', 'category');
5669 push @all_fields, ('age', 'age_string') unless($omit_age_column);
5670 push @all_fields, 'owner' unless($omit_owner);
Kacper Kornet5710be42012-04-24 19:39:15 +02005671 @projects = fill_project_list_info(\@projects, @all_fields);
Jakub Narebski69913412008-06-10 19:21:01 +02005672
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02005673 $order ||= $default_projects_order;
Petr Baudise30496d2006-10-24 05:33:17 +02005674 $from = 0 unless defined $from;
5675 $to = $#projects if (!defined $to || $#projects < $to);
5676
Jakub Narebski12b14432011-04-29 19:51:56 +02005677 # short circuit
5678 if ($from > $to) {
5679 print "<center>\n".
5680 "<b>No such projects found</b><br />\n".
5681 "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
5682 "</center>\n<br />\n";
5683 return;
Petr Baudis6b28da62008-09-25 18:48:37 +02005684 }
5685
Jakub Narebski12b14432011-04-29 19:51:56 +02005686 @projects = sort_projects_list(\@projects, $order);
5687
Petr Baudisaed93de2008-10-02 17:13:02 +02005688 if ($show_ctags) {
Jakub Narebski0368c492011-04-29 19:51:57 +02005689 my $ctags = git_gather_all_ctags(\@projects);
5690 my $cloud = git_populate_project_tagcloud($ctags);
Petr Baudisaed93de2008-10-02 17:13:02 +02005691 print git_show_project_tagcloud($cloud, 64);
5692 }
5693
Petr Baudise30496d2006-10-24 05:33:17 +02005694 print "<table class=\"project_list\">\n";
5695 unless ($no_header) {
5696 print "<tr>\n";
5697 if ($check_forks) {
5698 print "<th></th>\n";
5699 }
Petr Baudis6b28da62008-09-25 18:48:37 +02005700 print_sort_th('project', $order, 'Project');
5701 print_sort_th('descr', $order, 'Description');
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005702 print_sort_th('owner', $order, 'Owner') unless $omit_owner;
Kacper Kornet5710be42012-04-24 19:39:15 +02005703 print_sort_th('age', $order, 'Last Change') unless $omit_age_column;
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005704 print "<th></th>\n" . # for links
Petr Baudise30496d2006-10-24 05:33:17 +02005705 "</tr>\n";
5706 }
Petr Baudis42326112008-10-02 17:17:01 +02005707
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005708 if ($projects_list_group_categories) {
5709 # only display categories with projects in the $from-$to window
5710 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5711 my %categories = build_projlist_by_category(\@projects, $from, $to);
5712 foreach my $cat (sort keys %categories) {
5713 unless ($cat eq "") {
5714 print "<tr>\n";
5715 if ($check_forks) {
5716 print "<td></td>\n";
Jakub Narebski12b14432011-04-29 19:51:56 +02005717 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005718 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5719 print "</tr>\n";
Petr Baudise30496d2006-10-24 05:33:17 +02005720 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005721
5722 git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
Petr Baudise30496d2006-10-24 05:33:17 +02005723 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005724 } else {
5725 git_project_list_rows(\@projects, $from, $to, $check_forks);
Petr Baudise30496d2006-10-24 05:33:17 +02005726 }
Jakub Narebski12b14432011-04-29 19:51:56 +02005727
Petr Baudise30496d2006-10-24 05:33:17 +02005728 if (defined $extra) {
5729 print "<tr>\n";
5730 if ($check_forks) {
5731 print "<td></td>\n";
5732 }
5733 print "<td colspan=\"5\">$extra</td>\n" .
5734 "</tr>\n";
5735 }
5736 print "</table>\n";
5737}
5738
Jakub Narebski42671ca2009-11-13 02:02:12 +01005739sub git_log_body {
5740 # uses global variable $project
5741 my ($commitlist, $from, $to, $refs, $extra) = @_;
5742
5743 $from = 0 unless defined $from;
5744 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5745
5746 for (my $i = 0; $i <= $to; $i++) {
5747 my %co = %{$commitlist->[$i]};
5748 next if !%co;
5749 my $commit = $co{'id'};
5750 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski42671ca2009-11-13 02:02:12 +01005751 git_print_header_div('commit',
5752 "<span class=\"age\">$co{'age_string'}</span>" .
5753 esc_html($co{'title'}) . $ref,
5754 $commit);
5755 print "<div class=\"title_text\">\n" .
5756 "<div class=\"log_link\">\n" .
5757 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5758 " | " .
5759 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5760 " | " .
5761 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5762 "<br/>\n" .
5763 "</div>\n";
5764 git_print_authorship(\%co, -tag => 'span');
5765 print "<br/>\n</div>\n";
5766
5767 print "<div class=\"log_body\">\n";
5768 git_print_log($co{'comment'}, -final_empty_line=> 1);
5769 print "</div>\n";
5770 }
5771 if ($extra) {
5772 print "<div class=\"page_nav\">\n";
5773 print "$extra\n";
5774 print "</div>\n";
5775 }
5776}
5777
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005778sub git_shortlog_body {
5779 # uses global variable $project
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005780 my ($commitlist, $from, $to, $refs, $extra) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05305781
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005782 $from = 0 unless defined $from;
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005783 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005784
Jakub Narebski591ebf62007-11-19 14:16:11 +01005785 print "<table class=\"shortlog\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005786 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005787 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005788 my %co = %{$commitlist->[$i]};
5789 my $commit = $co{'id'};
Jakub Narebski847e01f2006-08-14 02:05:47 +02005790 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005791 if ($alternate) {
5792 print "<tr class=\"dark\">\n";
5793 } else {
5794 print "<tr class=\"light\">\n";
5795 }
5796 $alternate ^= 1;
5797 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5798 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02005799 format_author_html('td', \%co, 10) . "<td>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02005800 print format_subject_html($co{'title'}, $co{'title_short'},
5801 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005802 print "</td>\n" .
5803 "<td class=\"link\">" .
Petr Baudis4777b012006-10-24 05:36:10 +02005804 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
Petr Baudis35749ae2006-09-22 03:19:44 +02005805 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
Petr Baudis55ff35c2006-10-06 15:57:52 +02005806 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02005807 my $snapshot_links = format_snapshot_links($commit);
5808 if (defined $snapshot_links) {
5809 print " | " . $snapshot_links;
Petr Baudis55ff35c2006-10-06 15:57:52 +02005810 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305811 print "</td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005812 "</tr>\n";
5813 }
5814 if (defined $extra) {
5815 print "<tr>\n" .
5816 "<td colspan=\"4\">$extra</td>\n" .
5817 "</tr>\n";
5818 }
5819 print "</table>\n";
5820}
5821
Jakub Narebski581860e2006-08-14 02:09:08 +02005822sub git_history_body {
5823 # Warning: assumes constant type (blob or tree) during history
Jakub Narebski69ca37d2009-11-13 02:02:14 +01005824 my ($commitlist, $from, $to, $refs, $extra,
5825 $file_name, $file_hash, $ftype) = @_;
Jakub Narebski8be68352006-09-11 00:36:04 +02005826
5827 $from = 0 unless defined $from;
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005828 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
Jakub Narebski581860e2006-08-14 02:09:08 +02005829
Jakub Narebski591ebf62007-11-19 14:16:11 +01005830 print "<table class=\"history\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005831 my $alternate = 1;
Jakub Narebski8be68352006-09-11 00:36:04 +02005832 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005833 my %co = %{$commitlist->[$i]};
Jakub Narebski581860e2006-08-14 02:09:08 +02005834 if (!%co) {
5835 next;
5836 }
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005837 my $commit = $co{'id'};
Jakub Narebski581860e2006-08-14 02:09:08 +02005838
5839 my $ref = format_ref_marker($refs, $commit);
5840
5841 if ($alternate) {
5842 print "<tr class=\"dark\">\n";
5843 } else {
5844 print "<tr class=\"light\">\n";
5845 }
5846 $alternate ^= 1;
5847 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02005848 # shortlog: format_author_html('td', \%co, 10)
5849 format_author_html('td', \%co, 15, 3) . "<td>";
Jakub Narebski581860e2006-08-14 02:09:08 +02005850 # originally git_history used chop_str($co{'title'}, 50)
Jakub Narebski952c65f2006-08-22 16:52:50 +02005851 print format_subject_html($co{'title'}, $co{'title_short'},
5852 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski581860e2006-08-14 02:09:08 +02005853 print "</td>\n" .
5854 "<td class=\"link\">" .
Luben Tuikov6d81c5a2006-09-28 17:21:07 -07005855 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
5856 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
Jakub Narebski581860e2006-08-14 02:09:08 +02005857
5858 if ($ftype eq 'blob') {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01005859 my $blob_current = $file_hash;
Jakub Narebski581860e2006-08-14 02:09:08 +02005860 my $blob_parent = git_get_hash_by_path($commit, $file_name);
5861 if (defined $blob_current && defined $blob_parent &&
5862 $blob_current ne $blob_parent) {
5863 print " | " .
Jakub Narebski420e92f2006-08-24 23:53:54 +02005864 $cgi->a({-href => href(action=>"blobdiff",
5865 hash=>$blob_current, hash_parent=>$blob_parent,
5866 hash_base=>$hash_base, hash_parent_base=>$commit,
5867 file_name=>$file_name)},
Jakub Narebski581860e2006-08-14 02:09:08 +02005868 "diff to current");
5869 }
5870 }
5871 print "</td>\n" .
5872 "</tr>\n";
5873 }
5874 if (defined $extra) {
5875 print "<tr>\n" .
5876 "<td colspan=\"4\">$extra</td>\n" .
5877 "</tr>\n";
5878 }
5879 print "</table>\n";
5880}
5881
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005882sub git_tags_body {
5883 # uses global variable $project
5884 my ($taglist, $from, $to, $extra) = @_;
5885 $from = 0 unless defined $from;
5886 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5887
Jakub Narebski591ebf62007-11-19 14:16:11 +01005888 print "<table class=\"tags\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005889 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005890 for (my $i = $from; $i <= $to; $i++) {
5891 my $entry = $taglist->[$i];
5892 my %tag = %$entry;
Jakub Narebskicd146402006-11-02 20:23:11 +01005893 my $comment = $tag{'subject'};
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005894 my $comment_short;
5895 if (defined $comment) {
5896 $comment_short = chop_str($comment, 30, 5);
5897 }
5898 if ($alternate) {
5899 print "<tr class=\"dark\">\n";
5900 } else {
5901 print "<tr class=\"light\">\n";
5902 }
5903 $alternate ^= 1;
Jakub Narebski27dd1a82007-01-03 22:54:29 +01005904 if (defined $tag{'age'}) {
5905 print "<td><i>$tag{'age'}</i></td>\n";
5906 } else {
5907 print "<td></td>\n";
5908 }
5909 print "<td>" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005910 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02005911 -class => "list name"}, esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005912 "</td>\n" .
5913 "<td>";
5914 if (defined $comment) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02005915 print format_subject_html($comment, $comment_short,
5916 href(action=>"tag", hash=>$tag{'id'}));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005917 }
5918 print "</td>\n" .
5919 "<td class=\"selflink\">";
5920 if ($tag{'type'} eq "tag") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005921 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005922 } else {
5923 print "&nbsp;";
5924 }
5925 print "</td>\n" .
5926 "<td class=\"link\">" . " | " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005927 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005928 if ($tag{'reftype'} eq "commit") {
Jakub Narebskibf901f82007-12-15 15:40:28 +01005929 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
5930 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005931 } elsif ($tag{'reftype'} eq "blob") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005932 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005933 }
5934 print "</td>\n" .
5935 "</tr>";
5936 }
5937 if (defined $extra) {
5938 print "<tr>\n" .
5939 "<td colspan=\"5\">$extra</td>\n" .
5940 "</tr>\n";
5941 }
5942 print "</table>\n";
5943}
5944
5945sub git_heads_body {
5946 # uses global variable $project
Jakub Narebskifd49e562012-02-15 16:36:41 +01005947 my ($headlist, $head_at, $from, $to, $extra) = @_;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005948 $from = 0 unless defined $from;
Jakub Narebski120ddde2006-09-19 14:33:22 +02005949 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005950
Jakub Narebski591ebf62007-11-19 14:16:11 +01005951 print "<table class=\"heads\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005952 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005953 for (my $i = $from; $i <= $to; $i++) {
Jakub Narebski120ddde2006-09-19 14:33:22 +02005954 my $entry = $headlist->[$i];
Jakub Narebskicd146402006-11-02 20:23:11 +01005955 my %ref = %$entry;
Jakub Narebskifd49e562012-02-15 16:36:41 +01005956 my $curr = defined $head_at && $ref{'id'} eq $head_at;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005957 if ($alternate) {
5958 print "<tr class=\"dark\">\n";
5959 } else {
5960 print "<tr class=\"light\">\n";
5961 }
5962 $alternate ^= 1;
Jakub Narebskicd146402006-11-02 20:23:11 +01005963 print "<td><i>$ref{'age'}</i></td>\n" .
5964 ($curr ? "<td class=\"current_head\">" : "<td>") .
Jakub Narebskibf901f82007-12-15 15:40:28 +01005965 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
Jakub Narebskicd146402006-11-02 20:23:11 +01005966 -class => "list name"},esc_html($ref{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005967 "</td>\n" .
5968 "<td class=\"link\">" .
Jakub Narebskibf901f82007-12-15 15:40:28 +01005969 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
5970 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
Giuseppe Bilotta9e70e152010-11-11 13:26:08 +01005971 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005972 "</td>\n" .
5973 "</tr>";
5974 }
5975 if (defined $extra) {
5976 print "<tr>\n" .
5977 "<td colspan=\"3\">$extra</td>\n" .
5978 "</tr>\n";
5979 }
5980 print "</table>\n";
5981}
5982
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01005983# Display a single remote block
5984sub git_remote_block {
5985 my ($remote, $rdata, $limit, $head) = @_;
5986
5987 my $heads = $rdata->{'heads'};
5988 my $fetch = $rdata->{'fetch'};
5989 my $push = $rdata->{'push'};
5990
5991 my $urls_table = "<table class=\"projects_list\">\n" ;
5992
5993 if (defined $fetch) {
5994 if ($fetch eq $push) {
5995 $urls_table .= format_repo_url("URL", $fetch);
5996 } else {
5997 $urls_table .= format_repo_url("Fetch URL", $fetch);
5998 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
5999 }
6000 } elsif (defined $push) {
6001 $urls_table .= format_repo_url("Push URL", $push);
6002 } else {
6003 $urls_table .= format_repo_url("", "No remote URL");
6004 }
6005
6006 $urls_table .= "</table>\n";
6007
6008 my $dots;
6009 if (defined $limit && $limit < @$heads) {
6010 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
6011 }
6012
6013 print $urls_table;
6014 git_heads_body($heads, $head, 0, $limit, $dots);
6015}
6016
6017# Display a list of remote names with the respective fetch and push URLs
6018sub git_remotes_list {
6019 my ($remotedata, $limit) = @_;
6020 print "<table class=\"heads\">\n";
6021 my $alternate = 1;
6022 my @remotes = sort keys %$remotedata;
6023
6024 my $limited = $limit && $limit < @remotes;
6025
6026 $#remotes = $limit - 1 if $limited;
6027
6028 while (my $remote = shift @remotes) {
6029 my $rdata = $remotedata->{$remote};
6030 my $fetch = $rdata->{'fetch'};
6031 my $push = $rdata->{'push'};
6032 if ($alternate) {
6033 print "<tr class=\"dark\">\n";
6034 } else {
6035 print "<tr class=\"light\">\n";
6036 }
6037 $alternate ^= 1;
6038 print "<td>" .
6039 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
6040 -class=> "list name"},esc_html($remote)) .
6041 "</td>";
6042 print "<td class=\"link\">" .
6043 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
6044 " | " .
6045 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
6046 "</td>";
6047
6048 print "</tr>\n";
6049 }
6050
6051 if ($limited) {
6052 print "<tr>\n" .
6053 "<td colspan=\"3\">" .
6054 $cgi->a({-href => href(action=>"remotes")}, "...") .
6055 "</td>\n" . "</tr>\n";
6056 }
6057
6058 print "</table>";
6059}
6060
6061# Display remote heads grouped by remote, unless there are too many
6062# remotes, in which case we only display the remote names
6063sub git_remotes_body {
6064 my ($remotedata, $limit, $head) = @_;
6065 if ($limit and $limit < keys %$remotedata) {
6066 git_remotes_list($remotedata, $limit);
6067 } else {
6068 fill_remote_heads($remotedata);
6069 while (my ($remote, $rdata) = each %$remotedata) {
6070 git_print_section({-class=>"remote", -id=>$remote},
6071 ["remotes", $remote, $remote], sub {
6072 git_remote_block($remote, $rdata, $limit, $head);
6073 });
6074 }
6075 }
6076}
6077
Jakub Narebski16f20722011-06-22 17:28:53 +02006078sub git_search_message {
6079 my %co = @_;
6080
6081 my $greptype;
6082 if ($searchtype eq 'commit') {
6083 $greptype = "--grep=";
6084 } elsif ($searchtype eq 'author') {
6085 $greptype = "--author=";
6086 } elsif ($searchtype eq 'committer') {
6087 $greptype = "--committer=";
6088 }
6089 $greptype .= $searchtext;
6090 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6091 $greptype, '--regexp-ignore-case',
6092 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6093
6094 my $paging_nav = '';
6095 if ($page > 0) {
6096 $paging_nav .=
Jakub Narebski882541b2011-06-22 17:28:54 +02006097 $cgi->a({-href => href(-replay=>1, page=>undef)},
6098 "first") .
6099 " &sdot; " .
Jakub Narebski16f20722011-06-22 17:28:53 +02006100 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6101 -accesskey => "p", -title => "Alt-p"}, "prev");
6102 } else {
Jakub Narebski882541b2011-06-22 17:28:54 +02006103 $paging_nav .= "first &sdot; prev";
Jakub Narebski16f20722011-06-22 17:28:53 +02006104 }
6105 my $next_link = '';
6106 if ($#commitlist >= 100) {
6107 $next_link =
6108 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6109 -accesskey => "n", -title => "Alt-n"}, "next");
6110 $paging_nav .= " &sdot; $next_link";
6111 } else {
6112 $paging_nav .= " &sdot; next";
6113 }
6114
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006115 git_header_html();
6116
Jakub Narebski16f20722011-06-22 17:28:53 +02006117 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6118 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6119 if ($page == 0 && !@commitlist) {
6120 print "<p>No match.</p>\n";
6121 } else {
6122 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6123 }
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006124
6125 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006126}
6127
6128sub git_search_changes {
6129 my %co = @_;
6130
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006131 local $/ = "\n";
6132 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6133 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6134 ($search_use_regexp ? '--pickaxe-regex' : ())
6135 or die_error(500, "Open git-log failed");
6136
6137 git_header_html();
6138
Jakub Narebski16f20722011-06-22 17:28:53 +02006139 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6140 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6141
6142 print "<table class=\"pickaxe search\">\n";
6143 my $alternate = 1;
Jakub Narebski16f20722011-06-22 17:28:53 +02006144 undef %co;
6145 my @files;
6146 while (my $line = <$fd>) {
6147 chomp $line;
6148 next unless $line;
6149
6150 my %set = parse_difftree_raw_line($line);
6151 if (defined $set{'commit'}) {
6152 # finish previous commit
6153 if (%co) {
6154 print "</td>\n" .
6155 "<td class=\"link\">" .
Jakub Narebski882541b2011-06-22 17:28:54 +02006156 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6157 "commit") .
Jakub Narebski16f20722011-06-22 17:28:53 +02006158 " | " .
Jakub Narebski882541b2011-06-22 17:28:54 +02006159 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6160 hash_base=>$co{'id'})},
6161 "tree") .
6162 "</td>\n" .
Jakub Narebski16f20722011-06-22 17:28:53 +02006163 "</tr>\n";
6164 }
6165
6166 if ($alternate) {
6167 print "<tr class=\"dark\">\n";
6168 } else {
6169 print "<tr class=\"light\">\n";
6170 }
6171 $alternate ^= 1;
6172 %co = parse_commit($set{'commit'});
6173 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6174 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6175 "<td><i>$author</i></td>\n" .
6176 "<td>" .
6177 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6178 -class => "list subject"},
6179 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6180 } elsif (defined $set{'to_id'}) {
6181 next if ($set{'to_id'} =~ m/^0{40}$/);
6182
6183 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6184 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6185 -class => "list"},
6186 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6187 "<br/>\n";
6188 }
6189 }
6190 close $fd;
6191
6192 # finish last commit (warning: repetition!)
6193 if (%co) {
6194 print "</td>\n" .
6195 "<td class=\"link\">" .
Jakub Narebski882541b2011-06-22 17:28:54 +02006196 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6197 "commit") .
Jakub Narebski16f20722011-06-22 17:28:53 +02006198 " | " .
Jakub Narebski882541b2011-06-22 17:28:54 +02006199 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6200 hash_base=>$co{'id'})},
6201 "tree") .
6202 "</td>\n" .
Jakub Narebski16f20722011-06-22 17:28:53 +02006203 "</tr>\n";
6204 }
6205
6206 print "</table>\n";
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006207
6208 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006209}
6210
6211sub git_search_files {
6212 my %co = @_;
6213
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006214 local $/ = "\n";
Jakub Narebski8e09fd12012-01-05 21:32:56 +01006215 open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006216 $search_use_regexp ? ('-E', '-i') : '-F',
6217 $searchtext, $co{'tree'}
6218 or die_error(500, "Open git-grep failed");
6219
6220 git_header_html();
6221
Jakub Narebski16f20722011-06-22 17:28:53 +02006222 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6223 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6224
6225 print "<table class=\"grep_search\">\n";
6226 my $alternate = 1;
6227 my $matches = 0;
Jakub Narebski16f20722011-06-22 17:28:53 +02006228 my $lastfile = '';
Jakub Narebskifc8fcd22012-02-15 17:37:06 +01006229 my $file_href;
Jakub Narebski16f20722011-06-22 17:28:53 +02006230 while (my $line = <$fd>) {
6231 chomp $line;
Jakub Narebskifc8fcd22012-02-15 17:37:06 +01006232 my ($file, $lno, $ltext, $binary);
Jakub Narebski16f20722011-06-22 17:28:53 +02006233 last if ($matches++ > 1000);
6234 if ($line =~ /^Binary file (.+) matches$/) {
6235 $file = $1;
6236 $binary = 1;
6237 } else {
Jakub Narebski8e09fd12012-01-05 21:32:56 +01006238 ($file, $lno, $ltext) = split(/\0/, $line, 3);
6239 $file =~ s/^$co{'tree'}://;
Jakub Narebski16f20722011-06-22 17:28:53 +02006240 }
6241 if ($file ne $lastfile) {
6242 $lastfile and print "</td></tr>\n";
6243 if ($alternate++) {
6244 print "<tr class=\"dark\">\n";
6245 } else {
6246 print "<tr class=\"light\">\n";
6247 }
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006248 $file_href = href(action=>"blob", hash_base=>$co{'id'},
6249 file_name=>$file);
Jakub Narebski16f20722011-06-22 17:28:53 +02006250 print "<td class=\"list\">".
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006251 $cgi->a({-href => $file_href, -class => "list"}, esc_path($file));
Jakub Narebski16f20722011-06-22 17:28:53 +02006252 print "</td><td>\n";
6253 $lastfile = $file;
6254 }
6255 if ($binary) {
6256 print "<div class=\"binary\">Binary file</div>\n";
6257 } else {
6258 $ltext = untabify($ltext);
6259 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6260 $ltext = esc_html($1, -nbsp=>1);
6261 $ltext .= '<span class="match">';
6262 $ltext .= esc_html($2, -nbsp=>1);
6263 $ltext .= '</span>';
6264 $ltext .= esc_html($3, -nbsp=>1);
6265 } else {
6266 $ltext = esc_html($ltext, -nbsp=>1);
6267 }
6268 print "<div class=\"pre\">" .
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006269 $cgi->a({-href => $file_href.'#l'.$lno,
6270 -class => "linenr"}, sprintf('%4i', $lno)) .
6271 ' ' . $ltext . "</div>\n";
Jakub Narebski16f20722011-06-22 17:28:53 +02006272 }
6273 }
6274 if ($lastfile) {
6275 print "</td></tr>\n";
6276 if ($matches > 1000) {
6277 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6278 }
6279 } else {
6280 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6281 }
6282 close $fd;
6283
6284 print "</table>\n";
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006285
6286 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006287}
6288
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006289sub git_search_grep_body {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006290 my ($commitlist, $from, $to, $extra) = @_;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006291 $from = 0 unless defined $from;
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006292 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006293
Jakub Narebski591ebf62007-11-19 14:16:11 +01006294 print "<table class=\"commit_search\">\n";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006295 my $alternate = 1;
6296 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006297 my %co = %{$commitlist->[$i]};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006298 if (!%co) {
6299 next;
6300 }
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006301 my $commit = $co{'id'};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006302 if ($alternate) {
6303 print "<tr class=\"dark\">\n";
6304 } else {
6305 print "<tr class=\"light\">\n";
6306 }
6307 $alternate ^= 1;
6308 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02006309 format_author_html('td', \%co, 15, 5) .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006310 "<td>" .
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006311 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6312 -class => "list subject"},
6313 chop_and_escape_str($co{'title'}, 50) . "<br/>");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006314 my $comment = $co{'comment'};
6315 foreach my $line (@$comment) {
Jakub Narebski6dfbb302008-03-02 16:57:14 +01006316 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006317 my ($lead, $match, $trail) = ($1, $2, $3);
Jakub Narebskib8d97d02008-02-25 21:07:57 +01006318 $match = chop_str($match, 70, 5, 'center');
6319 my $contextlen = int((80 - length($match))/2);
6320 $contextlen = 30 if ($contextlen > 30);
6321 $lead = chop_str($lead, $contextlen, 10, 'left');
6322 $trail = chop_str($trail, $contextlen, 10, 'right');
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006323
6324 $lead = esc_html($lead);
6325 $match = esc_html($match);
6326 $trail = esc_html($trail);
6327
6328 print "$lead<span class=\"match\">$match</span>$trail<br />";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006329 }
6330 }
6331 print "</td>\n" .
6332 "<td class=\"link\">" .
6333 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6334 " | " .
Denis Chengf1fe8f52007-11-26 20:42:06 +08006335 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
6336 " | " .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006337 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6338 print "</td>\n" .
6339 "</tr>\n";
6340 }
6341 if (defined $extra) {
6342 print "<tr>\n" .
6343 "<td colspan=\"3\">$extra</td>\n" .
6344 "</tr>\n";
6345 }
6346 print "</table>\n";
6347}
6348
Jakub Narebski717b8312006-07-31 21:22:15 +02006349## ======================================================================
6350## ======================================================================
6351## actions
6352
Jakub Narebski717b8312006-07-31 21:22:15 +02006353sub git_project_list {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02006354 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02006355 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006356 die_error(400, "Unknown order parameter");
Jakub Narebski6326b602006-08-01 02:59:12 +02006357 }
6358
Bernhard R. Link19d2d232012-01-30 21:07:37 +01006359 my @list = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski717b8312006-07-31 21:22:15 +02006360 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006361 die_error(404, "No projects found");
Jakub Narebski717b8312006-07-31 21:22:15 +02006362 }
Jakub Narebski6326b602006-08-01 02:59:12 +02006363
Jakub Narebski717b8312006-07-31 21:22:15 +02006364 git_header_html();
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01006365 if (defined $home_text && -f $home_text) {
Jakub Narebski717b8312006-07-31 21:22:15 +02006366 print "<div class=\"index_include\">\n";
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01006367 insert_file($home_text);
Jakub Narebski717b8312006-07-31 21:22:15 +02006368 print "</div>\n";
6369 }
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01006370
6371 git_project_search_form($searchtext, $search_use_regexp);
Petr Baudise30496d2006-10-24 05:33:17 +02006372 git_project_list_body(\@list, $order);
6373 git_footer_html();
6374}
6375
6376sub git_forks {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02006377 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02006378 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006379 die_error(400, "Unknown order parameter");
Jakub Narebski717b8312006-07-31 21:22:15 +02006380 }
Petr Baudise30496d2006-10-24 05:33:17 +02006381
Bernhard R. Link4c7cd172012-01-30 21:05:47 +01006382 my $filter = $project;
6383 $filter =~ s/\.git$//;
6384 my @list = git_get_projects_list($filter);
Petr Baudise30496d2006-10-24 05:33:17 +02006385 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006386 die_error(404, "No forks found");
Jakub Narebski717b8312006-07-31 21:22:15 +02006387 }
Petr Baudise30496d2006-10-24 05:33:17 +02006388
6389 git_header_html();
6390 git_print_page_nav('','');
6391 git_print_header_div('summary', "$project forks");
6392 git_project_list_body(\@list, $order);
Jakub Narebski717b8312006-07-31 21:22:15 +02006393 git_footer_html();
6394}
6395
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006396sub git_project_index {
Bernhard R. Link19d2d232012-01-30 21:07:37 +01006397 my @projects = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski12b14432011-04-29 19:51:56 +02006398 if (!@projects) {
6399 die_error(404, "No projects found");
6400 }
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006401
6402 print $cgi->header(
6403 -type => 'text/plain',
6404 -charset => 'utf-8',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02006405 -content_disposition => 'inline; filename="index.aux"');
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006406
6407 foreach my $pr (@projects) {
6408 if (!exists $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02006409 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006410 }
6411
6412 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
6413 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
6414 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6415 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6416 $path =~ s/ /\+/g;
6417 $owner =~ s/ /\+/g;
6418
6419 print "$path $owner\n";
6420 }
6421}
6422
Kay Sieversede5e102005-08-07 20:23:12 +02006423sub git_summary {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006424 my $descr = git_get_project_description($project) || "none";
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00006425 my %co = parse_commit("HEAD");
Jakub Narebski785cdea2007-05-13 12:39:22 +02006426 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00006427 my $head = $co{'id'};
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006428 my $remote_heads = gitweb_check_feature('remote_heads');
Kay Sieversede5e102005-08-07 20:23:12 +02006429
Jakub Narebski1e0cf032006-08-14 02:10:06 +02006430 my $owner = git_get_project_owner($project);
Kay Sieversede5e102005-08-07 20:23:12 +02006431
Jakub Narebskicd146402006-11-02 20:23:11 +01006432 my $refs = git_get_references();
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006433 # These get_*_list functions return one more to allow us to see if
6434 # there are more ...
6435 my @taglist = git_get_tags_list(16);
6436 my @headlist = git_get_heads_list(16);
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006437 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
Petr Baudise30496d2006-10-24 05:33:17 +02006438 my @forklist;
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006439 my $check_forks = gitweb_check_feature('forks');
Junio C Hamano5dd5ed02006-11-07 22:00:45 -08006440
6441 if ($check_forks) {
Jakub Narebski12b14432011-04-29 19:51:56 +02006442 # find forks of a project
Bernhard R. Link4c7cd172012-01-30 21:05:47 +01006443 my $filter = $project;
6444 $filter =~ s/\.git$//;
6445 @forklist = git_get_projects_list($filter);
Jakub Narebski12b14432011-04-29 19:51:56 +02006446 # filter out forks of forks
6447 @forklist = filter_forks_from_projects_list(\@forklist)
6448 if (@forklist);
Petr Baudise30496d2006-10-24 05:33:17 +02006449 }
Jakub Narebski120ddde2006-09-19 14:33:22 +02006450
Kay Sieversede5e102005-08-07 20:23:12 +02006451 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02006452 git_print_page_nav('summary','', $head);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006453
Kay Sievers19806692005-08-07 20:26:27 +02006454 print "<div class=\"title\">&nbsp;</div>\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01006455 print "<table class=\"projects_list\">\n" .
Kacper Kornet0ebe7822012-04-26 18:45:44 +02006456 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n";
6457 unless ($omit_owner) {
6458 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
6459 }
Jakub Narebski785cdea2007-05-13 12:39:22 +02006460 if (defined $cd{'rfc2822'}) {
Jakub Narebski256b7b42011-04-28 21:04:07 +02006461 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
6462 "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
Jakub Narebski785cdea2007-05-13 12:39:22 +02006463 }
6464
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02006465 # use per project git URL list in $projectroot/$project/cloneurl
6466 # or make project git URL from git base URL and project name
Jakub Narebski19a87212006-08-15 23:03:17 +02006467 my $url_tag = "URL";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02006468 my @url_list = git_get_project_url_list($project);
6469 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
6470 foreach my $git_url (@url_list) {
6471 next unless $git_url;
Giuseppe Bilotta0e656992010-11-11 13:26:15 +01006472 print format_repo_url($url_tag, $git_url);
Jakub Narebski19a87212006-08-15 23:03:17 +02006473 $url_tag = "";
6474 }
Petr Baudisaed93de2008-10-02 17:13:02 +02006475
6476 # Tag cloud
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006477 my $show_ctags = gitweb_check_feature('ctags');
Petr Baudisaed93de2008-10-02 17:13:02 +02006478 if ($show_ctags) {
6479 my $ctags = git_get_project_ctags($project);
Jakub Narebski0368c492011-04-29 19:51:57 +02006480 if (%$ctags) {
6481 # without ability to add tags, don't show if there are none
6482 my $cloud = git_populate_project_tagcloud($ctags);
6483 print "<tr id=\"metadata_ctags\">" .
6484 "<td>content tags</td>" .
6485 "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
6486 "</tr>\n";
6487 }
Petr Baudisaed93de2008-10-02 17:13:02 +02006488 }
6489
Jakub Narebski19a87212006-08-15 23:03:17 +02006490 print "</table>\n";
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006491
Matt McCutchen7e1100e2009-02-07 19:00:09 -05006492 # If XSS prevention is on, we don't include README.html.
6493 # TODO: Allow a readme in some safe format.
6494 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01006495 print "<div class=\"title\">readme</div>\n" .
6496 "<div class=\"readme\">\n";
6497 insert_file("$projectroot/$project/README.html");
6498 print "\n</div>\n"; # class="readme"
Petr Baudis447ef092006-10-24 05:23:46 +02006499 }
6500
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006501 # we need to request one more than 16 (0..15) to check if
6502 # those 16 are all
Jakub Narebski785cdea2007-05-13 12:39:22 +02006503 my @commitlist = $head ? parse_commits($head, 17) : ();
6504 if (@commitlist) {
6505 git_print_header_div('shortlog');
6506 git_shortlog_body(\@commitlist, 0, 15, $refs,
6507 $#commitlist <= 15 ? undef :
6508 $cgi->a({-href => href(action=>"shortlog")}, "..."));
6509 }
Kay Sieversede5e102005-08-07 20:23:12 +02006510
Jakub Narebski120ddde2006-09-19 14:33:22 +02006511 if (@taglist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006512 git_print_header_div('tags');
Jakub Narebski120ddde2006-09-19 14:33:22 +02006513 git_tags_body(\@taglist, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006514 $#taglist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006515 $cgi->a({-href => href(action=>"tags")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02006516 }
Kay Sievers0db37972005-08-07 20:24:35 +02006517
Jakub Narebski120ddde2006-09-19 14:33:22 +02006518 if (@headlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006519 git_print_header_div('heads');
Jakub Narebski120ddde2006-09-19 14:33:22 +02006520 git_heads_body(\@headlist, $head, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006521 $#headlist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006522 $cgi->a({-href => href(action=>"heads")}, "..."));
Kay Sievers0db37972005-08-07 20:24:35 +02006523 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006524
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006525 if (%remotedata) {
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006526 git_print_header_div('remotes');
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006527 git_remotes_body(\%remotedata, 15, $head);
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006528 }
6529
Petr Baudise30496d2006-10-24 05:33:17 +02006530 if (@forklist) {
6531 git_print_header_div('forks');
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02006532 git_project_list_body(\@forklist, 'age', 0, 15,
Robert Fitzsimonsaaca9672006-12-22 19:38:12 +00006533 $#forklist <= 15 ? undef :
Petr Baudise30496d2006-10-24 05:33:17 +02006534 $cgi->a({-href => href(action=>"forks")}, "..."),
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02006535 'no_header');
Petr Baudise30496d2006-10-24 05:33:17 +02006536 }
6537
Kay Sieversede5e102005-08-07 20:23:12 +02006538 git_footer_html();
6539}
6540
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006541sub git_tag {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006542 my %tag = parse_tag($hash);
Jakub Narebski198a2a82007-05-12 21:16:34 +02006543
6544 if (! %tag) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006545 die_error(404, "Unknown tag object");
Jakub Narebski198a2a82007-05-12 21:16:34 +02006546 }
6547
Anders Kaseorgd8a94802010-08-27 13:38:16 -04006548 my $head = git_get_head_hash($project);
6549 git_header_html();
6550 git_print_page_nav('','', $head,undef,$head);
Jakub Narebski847e01f2006-08-14 02:05:47 +02006551 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006552 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01006553 "<table class=\"object_header\">\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02006554 "<tr>\n" .
6555 "<td>object</td>\n" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02006556 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6557 $tag{'object'}) . "</td>\n" .
6558 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6559 $tag{'type'}) . "</td>\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02006560 "</tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006561 if (defined($tag{'author'})) {
Giuseppe Bilottaba924732009-06-30 00:00:50 +02006562 git_print_authorship_rows(\%tag, 'author');
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006563 }
6564 print "</table>\n\n" .
6565 "</div>\n";
6566 print "<div class=\"page_body\">";
6567 my $comment = $tag{'comment'};
6568 foreach my $line (@$comment) {
Junio C Hamano70022432006-11-24 14:04:01 -08006569 chomp $line;
Jakub Narebski793c4002006-11-24 22:25:50 +01006570 print esc_html($line, -nbsp=>1) . "<br/>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006571 }
6572 print "</div>\n";
6573 git_footer_html();
6574}
6575
Jakub Narebski4af819d2009-09-01 13:39:17 +02006576sub git_blame_common {
6577 my $format = shift || 'porcelain';
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01006578 if ($format eq 'porcelain' && $input_params{'javascript'}) {
Jakub Narebskic4ccf612009-09-01 13:39:19 +02006579 $format = 'incremental';
6580 $action = 'blame_incremental'; # for page title etc
6581 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006582
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006583 # permissions
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006584 gitweb_check_feature('blame')
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006585 or die_error(403, "Blame view not allowed");
Lea Wiemann074afaa2008-06-19 22:03:21 +02006586
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006587 # error checking
Lea Wiemann074afaa2008-06-19 22:03:21 +02006588 die_error(400, "No file name given") unless $file_name;
Jakub Narebski847e01f2006-08-14 02:05:47 +02006589 $hash_base ||= git_get_head_hash($project);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006590 die_error(404, "Couldn't find base commit") unless $hash_base;
Jakub Narebski847e01f2006-08-14 02:05:47 +02006591 my %co = parse_commit($hash_base)
Lea Wiemann074afaa2008-06-19 22:03:21 +02006592 or die_error(404, "Commit not found");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006593 my $ftype = "blob";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006594 if (!defined $hash) {
6595 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02006596 or die_error(404, "Error looking up file");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006597 } else {
6598 $ftype = git_get_type($hash);
6599 if ($ftype !~ "blob") {
6600 die_error(400, "Object is not a blob");
6601 }
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006602 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006603
Jakub Narebski4af819d2009-09-01 13:39:17 +02006604 my $fd;
6605 if ($format eq 'incremental') {
6606 # get file contents (as base)
6607 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
6608 or die_error(500, "Open git-cat-file failed");
6609 } elsif ($format eq 'data') {
6610 # run git-blame --incremental
6611 open $fd, "-|", git_cmd(), "blame", "--incremental",
6612 $hash_base, "--", $file_name
6613 or die_error(500, "Open git-blame --incremental failed");
6614 } else {
6615 # run git-blame --porcelain
6616 open $fd, "-|", git_cmd(), "blame", '-p',
6617 $hash_base, '--', $file_name
6618 or die_error(500, "Open git-blame --porcelain failed");
6619 }
6620
6621 # incremental blame data returns early
6622 if ($format eq 'data') {
6623 print $cgi->header(
6624 -type=>"text/plain", -charset => "utf-8",
6625 -status=> "200 OK");
6626 local $| = 1; # output autoflush
Jürgen Kreileder57cf4ad2011-12-17 10:22:23 +01006627 while (my $line = <$fd>) {
6628 print to_utf8($line);
6629 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006630 close $fd
6631 or print "ERROR $!\n";
6632
6633 print 'END';
6634 if (defined $t0 && gitweb_check_feature('timed')) {
6635 print ' '.
Jakub Narebski3962f1d72010-11-09 19:27:54 +01006636 tv_interval($t0, [ gettimeofday() ]).
Jakub Narebski4af819d2009-09-01 13:39:17 +02006637 ' '.$number_of_git_cmds;
6638 }
6639 print "\n";
6640
6641 return;
6642 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006643
6644 # page header
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006645 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02006646 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01006647 $cgi->a({-href => href(action=>"blob", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02006648 "blob") .
Jakub Narebski87e573f2009-12-01 17:54:26 +01006649 " | ";
6650 if ($format eq 'incremental') {
6651 $formats_nav .=
6652 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
6653 "blame") . " (non-incremental)";
6654 } else {
6655 $formats_nav .=
6656 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
6657 "blame") . " (incremental)";
6658 }
6659 $formats_nav .=
Jakub Narebski952c65f2006-08-22 16:52:50 +02006660 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01006661 $cgi->a({-href => href(action=>"history", -replay=>1)},
6662 "history") .
Petr Baudiscae18622006-09-22 03:19:41 +02006663 " | " .
Jakub Narebski4af819d2009-09-01 13:39:17 +02006664 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02006665 "HEAD");
Jakub Narebski847e01f2006-08-14 02:05:47 +02006666 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6667 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07006668 git_print_page_path($file_name, $ftype, $hash_base);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006669
6670 # page body
Jakub Narebski4af819d2009-09-01 13:39:17 +02006671 if ($format eq 'incremental') {
6672 print "<noscript>\n<div class=\"error\"><center><b>\n".
6673 "This page requires JavaScript to run.\n Use ".
Jakub Narebskic4ccf612009-09-01 13:39:19 +02006674 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
Jakub Narebski4af819d2009-09-01 13:39:17 +02006675 'this page').
6676 " instead.\n".
6677 "</b></center></div>\n</noscript>\n";
6678
6679 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
6680 }
6681
6682 print qq!<div class="page_body">\n!;
6683 print qq!<div id="progress_info">... / ...</div>\n!
6684 if ($format eq 'incremental');
6685 print qq!<table id="blame_table" class="blame" width="100%">\n!.
6686 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6687 qq!<thead>\n!.
6688 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
6689 qq!</thead>\n!.
6690 qq!<tbody>\n!;
6691
Jakub Narebskiaef37682009-07-25 00:44:06 +02006692 my @rev_color = qw(light dark);
Luben Tuikovcc1bf972006-07-23 13:37:53 -07006693 my $num_colors = scalar(@rev_color);
6694 my $current_color = 0;
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006695
Jakub Narebski4af819d2009-09-01 13:39:17 +02006696 if ($format eq 'incremental') {
6697 my $color_class = $rev_color[$current_color];
6698
6699 #contents of a file
6700 my $linenr = 0;
6701 LINE:
6702 while (my $line = <$fd>) {
6703 chomp $line;
6704 $linenr++;
6705
6706 print qq!<tr id="l$linenr" class="$color_class">!.
6707 qq!<td class="sha1"><a href=""> </a></td>!.
6708 qq!<td class="linenr">!.
6709 qq!<a class="linenr" href="">$linenr</a></td>!;
6710 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
6711 print qq!</tr>\n!;
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07006712 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006713
6714 } else { # porcelain, i.e. ordinary blame
6715 my %metainfo = (); # saves information about commits
6716
6717 # blame data
6718 LINE:
6719 while (my $line = <$fd>) {
6720 chomp $line;
6721 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6722 # no <lines in group> for subsequent lines in group of lines
6723 my ($full_rev, $orig_lineno, $lineno, $group_size) =
6724 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6725 if (!exists $metainfo{$full_rev}) {
6726 $metainfo{$full_rev} = { 'nprevious' => 0 };
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07006727 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006728 my $meta = $metainfo{$full_rev};
6729 my $data;
6730 while ($data = <$fd>) {
6731 chomp $data;
6732 last if ($data =~ s/^\t//); # contents of line
6733 if ($data =~ /^(\S+)(?: (.*))?$/) {
6734 $meta->{$1} = $2 unless exists $meta->{$1};
6735 }
6736 if ($data =~ /^previous /) {
6737 $meta->{'nprevious'}++;
Jakub Narebskia36817b2009-07-25 00:44:05 +02006738 }
6739 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006740 my $short_rev = substr($full_rev, 0, 8);
6741 my $author = $meta->{'author'};
6742 my %date =
6743 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
6744 my $date = $date{'iso-tz'};
6745 if ($group_size) {
6746 $current_color = ($current_color + 1) % $num_colors;
6747 }
6748 my $tr_class = $rev_color[$current_color];
6749 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6750 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6751 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6752 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6753 if ($group_size) {
6754 print "<td class=\"sha1\"";
6755 print " title=\"". esc_html($author) . ", $date\"";
6756 print " rowspan=\"$group_size\"" if ($group_size > 1);
6757 print ">";
6758 print $cgi->a({-href => href(action=>"commit",
6759 hash=>$full_rev,
6760 file_name=>$file_name)},
6761 esc_html($short_rev));
6762 if ($group_size >= 2) {
6763 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6764 if (@author_initials) {
6765 print "<br />" .
6766 esc_html(join('', @author_initials));
6767 # or join('.', ...)
6768 }
6769 }
6770 print "</td>\n";
6771 }
6772 # 'previous' <sha1 of parent commit> <filename at commit>
6773 if (exists $meta->{'previous'} &&
6774 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6775 $meta->{'parent'} = $1;
6776 $meta->{'file_parent'} = unquote($2);
6777 }
6778 my $linenr_commit =
6779 exists($meta->{'parent'}) ?
6780 $meta->{'parent'} : $full_rev;
6781 my $linenr_filename =
6782 exists($meta->{'file_parent'}) ?
6783 $meta->{'file_parent'} : unquote($meta->{'filename'});
6784 my $blamed = href(action => 'blame',
6785 file_name => $linenr_filename,
6786 hash_base => $linenr_commit);
6787 print "<td class=\"linenr\">";
6788 print $cgi->a({ -href => "$blamed#l$orig_lineno",
6789 -class => "linenr" },
6790 esc_html($lineno));
6791 print "</td>";
6792 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
6793 print "</tr>\n";
6794 } # end while
6795
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006796 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006797
6798 # footer
6799 print "</tbody>\n".
6800 "</table>\n"; # class="blame"
6801 print "</div>\n"; # class="blame_body"
Jakub Narebski952c65f2006-08-22 16:52:50 +02006802 close $fd
6803 or print "Reading blob failed\n";
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006804
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006805 git_footer_html();
6806}
6807
Jakub Narebski4af819d2009-09-01 13:39:17 +02006808sub git_blame {
6809 git_blame_common();
6810}
6811
6812sub git_blame_incremental {
6813 git_blame_common('incremental');
6814}
6815
6816sub git_blame_data {
6817 git_blame_common('data');
6818}
6819
Kay Sieversede5e102005-08-07 20:23:12 +02006820sub git_tags {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006821 my $head = git_get_head_hash($project);
Kay Sieversede5e102005-08-07 20:23:12 +02006822 git_header_html();
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01006823 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
Jakub Narebski847e01f2006-08-14 02:05:47 +02006824 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02006825
Jakub Narebskicd146402006-11-02 20:23:11 +01006826 my @tagslist = git_get_tags_list();
6827 if (@tagslist) {
6828 git_tags_body(\@tagslist);
Kay Sieversede5e102005-08-07 20:23:12 +02006829 }
Kay Sieversede5e102005-08-07 20:23:12 +02006830 git_footer_html();
6831}
6832
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02006833sub git_heads {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006834 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02006835 git_header_html();
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01006836 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
Jakub Narebski847e01f2006-08-14 02:05:47 +02006837 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02006838
Jakub Narebskicd146402006-11-02 20:23:11 +01006839 my @headslist = git_get_heads_list();
6840 if (@headslist) {
6841 git_heads_body(\@headslist, $head);
Kay Sievers0db37972005-08-07 20:24:35 +02006842 }
Kay Sievers0db37972005-08-07 20:24:35 +02006843 git_footer_html();
6844}
6845
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006846# used both for single remote view and for list of all the remotes
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006847sub git_remotes {
6848 gitweb_check_feature('remote_heads')
6849 or die_error(403, "Remote heads view is disabled");
6850
6851 my $head = git_get_head_hash($project);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006852 my $remote = $input_params{'hash'};
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006853
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006854 my $remotedata = git_get_remotes_list($remote);
6855 die_error(500, "Unable to get remote information") unless defined $remotedata;
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006856
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006857 unless (%$remotedata) {
6858 die_error(404, defined $remote ?
6859 "Remote $remote not found" :
6860 "No remotes found");
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006861 }
6862
6863 git_header_html(undef, undef, -action_extra => $remote);
6864 git_print_page_nav('', '', $head, undef, $head,
6865 format_ref_views($remote ? '' : 'remotes'));
6866
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006867 fill_remote_heads($remotedata);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006868 if (defined $remote) {
6869 git_print_header_div('remotes', "$remote remote for $project");
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006870 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006871 } else {
6872 git_print_header_div('summary', "$project remotes");
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006873 git_remotes_body($remotedata, undef, $head);
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006874 }
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006875
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006876 git_footer_html();
6877}
6878
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006879sub git_blob_plain {
Jakub Narebski7f718e82008-06-03 16:47:10 +02006880 my $type = shift;
Jakub Narebskif2e73302006-08-26 19:14:25 +02006881 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02006882
Luben Tuikovcff07712006-07-23 13:28:55 -07006883 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006884 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006885 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006886 $hash = git_get_hash_by_path($base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02006887 or die_error(404, "Cannot find file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006888 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006889 die_error(400, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006890 }
Martin Waitz800764c2006-09-16 23:09:02 +02006891 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6892 # blobs defined by non-textual hash id's can be cached
6893 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006894 }
Martin Waitz800764c2006-09-16 23:09:02 +02006895
Dennis Stosberg25691fb2006-08-28 17:49:58 +02006896 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02006897 or die_error(500, "Open git-cat-file blob '$hash' failed");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006898
Jakub Narebski7f718e82008-06-03 16:47:10 +02006899 # content-type (can include charset)
6900 $type = blob_contenttype($fd, $file_name, $type);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006901
Jakub Narebski7f718e82008-06-03 16:47:10 +02006902 # "save as" filename, even when no $file_name is given
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006903 my $save_as = "$hash";
6904 if (defined $file_name) {
6905 $save_as = $file_name;
6906 } elsif ($type =~ m/^text\//) {
6907 $save_as .= '.txt';
6908 }
6909
Matt McCutchen7e1100e2009-02-07 19:00:09 -05006910 # With XSS prevention on, blobs of all types except a few known safe
6911 # ones are served with "Content-Disposition: attachment" to make sure
6912 # they don't run in our security domain. For certain image types,
6913 # blob view writes an <img> tag referring to blob_plain view, and we
6914 # want to be sure not to break that by serving the image as an
6915 # attachment (though Firefox 3 doesn't seem to care).
6916 my $sandbox = $prevent_xss &&
Jakub Narebski86afbd02011-06-30 11:39:20 +02006917 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
6918
6919 # serve text/* as text/plain
6920 if ($prevent_xss &&
Jakub Narebskie8c35312011-06-30 11:39:21 +02006921 ($type =~ m!^text/[a-z]+\b(.*)$! ||
6922 ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
Jakub Narebski86afbd02011-06-30 11:39:20 +02006923 my $rest = $1;
6924 $rest = defined $rest ? $rest : '';
6925 $type = "text/plain$rest";
6926 }
Matt McCutchen7e1100e2009-02-07 19:00:09 -05006927
Jakub Narebskif2e73302006-08-26 19:14:25 +02006928 print $cgi->header(
Jakub Narebski7f718e82008-06-03 16:47:10 +02006929 -type => $type,
6930 -expires => $expires,
Matt McCutchen7e1100e2009-02-07 19:00:09 -05006931 -content_disposition =>
6932 ($sandbox ? 'attachment' : 'inline')
6933 . '; filename="' . $save_as . '"');
Jakub Narebski34122b52009-05-11 03:29:40 +02006934 local $/ = undef;
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006935 binmode STDOUT, ':raw';
6936 print <$fd>;
6937 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006938 close $fd;
6939}
6940
Kay Sievers09bd7892005-08-07 20:21:23 +02006941sub git_blob {
Jakub Narebskif2e73302006-08-26 19:14:25 +02006942 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02006943
Luben Tuikovcff07712006-07-23 13:28:55 -07006944 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006945 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006946 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006947 $hash = git_get_hash_by_path($base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02006948 or die_error(404, "Cannot find file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006949 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006950 die_error(400, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006951 }
Martin Waitz800764c2006-09-16 23:09:02 +02006952 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6953 # blobs defined by non-textual hash id's can be cached
6954 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006955 }
Martin Waitz800764c2006-09-16 23:09:02 +02006956
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006957 my $have_blame = gitweb_check_feature('blame');
Dennis Stosberg25691fb2006-08-28 17:49:58 +02006958 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02006959 or die_error(500, "Couldn't cat $file_name, $hash");
Jakub Narebski847e01f2006-08-14 02:05:47 +02006960 my $mimetype = blob_mimetype($fd, $file_name);
Johannes Schindelinb331fe52010-04-27 21:34:44 +02006961 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01006962 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006963 close $fd;
6964 return git_blob_plain($mimetype);
6965 }
Jakub Narebski5a4cf332006-12-04 23:47:22 +01006966 # we can have blame only for text/* mimetype
6967 $have_blame &&= ($mimetype =~ m!^text/!);
6968
Jakub Narebski592ea412010-04-27 21:34:45 +02006969 my $highlight = gitweb_check_feature('highlight');
6970 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
6971 $fd = run_highlighter($fd, $highlight, $syntax)
6972 if $syntax;
Johannes Schindelinb331fe52010-04-27 21:34:44 +02006973
Jakub Narebskif2e73302006-08-26 19:14:25 +02006974 git_header_html(undef, $expires);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02006975 my $formats_nav = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02006976 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Kay Sievers93129442005-10-17 03:27:54 +02006977 if (defined $file_name) {
Florian Forster5996ca02006-06-12 10:31:57 +02006978 if ($have_blame) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02006979 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01006980 $cgi->a({-href => href(action=>"blame", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02006981 "blame") .
6982 " | ";
Florian Forster5996ca02006-06-12 10:31:57 +02006983 }
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02006984 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01006985 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02006986 "history") .
6987 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01006988 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02006989 "raw") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02006990 " | " .
6991 $cgi->a({-href => href(action=>"blob",
6992 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02006993 "HEAD");
Kay Sievers93129442005-10-17 03:27:54 +02006994 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02006995 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01006996 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6997 "raw");
Kay Sievers93129442005-10-17 03:27:54 +02006998 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02006999 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7000 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02007001 } else {
7002 print "<div class=\"page_nav\">\n" .
7003 "<br/><br/></div>\n" .
Jakub Narebski3017ed62010-12-15 00:34:01 +01007004 "<div class=\"title\">".esc_html($hash)."</div>\n";
Kay Sievers09bd7892005-08-07 20:21:23 +02007005 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07007006 git_print_page_path($file_name, "blob", $hash_base);
Kay Sieversc07ad4b2005-08-07 20:22:44 +02007007 print "<div class=\"page_body\">\n";
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007008 if ($mimetype =~ m!^image/!) {
Jakub Narebski3017ed62010-12-15 00:34:01 +01007009 print qq!<img type="!.esc_attr($mimetype).qq!"!;
Jakub Narebski5a4cf332006-12-04 23:47:22 +01007010 if ($file_name) {
Jakub Narebski3017ed62010-12-15 00:34:01 +01007011 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
Jakub Narebski5a4cf332006-12-04 23:47:22 +01007012 }
7013 print qq! src="! .
7014 href(action=>"blob_plain", hash=>$hash,
7015 hash_base=>$hash_base, file_name=>$file_name) .
7016 qq!" />\n!;
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007017 } else {
7018 my $nr;
7019 while (my $line = <$fd>) {
7020 chomp $line;
7021 $nr++;
7022 $line = untabify($line);
Jakub Narebski592ea412010-04-27 21:34:45 +02007023 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 +02007024 $nr, esc_attr(href(-replay => 1)), $nr, $nr,
7025 $syntax ? sanitize($line) : esc_html($line, -nbsp=>1);
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007026 }
Kay Sievers161332a2005-08-07 19:49:46 +02007027 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02007028 close $fd
7029 or print "Reading blob failed.\n";
Kay Sieversfbb592a2005-08-07 20:12:11 +02007030 print "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02007031 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007032}
7033
7034sub git_tree {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07007035 if (!defined $hash_base) {
7036 $hash_base = "HEAD";
7037 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02007038 if (!defined $hash) {
Kay Sievers09bd7892005-08-07 20:21:23 +02007039 if (defined $file_name) {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07007040 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
7041 } else {
7042 $hash = $hash_base;
Kay Sievers10dba282005-08-07 20:25:27 +02007043 }
Kay Sieverse925f382005-08-07 20:23:35 +02007044 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02007045 die_error(404, "No such tree") unless defined($hash);
Jakub Narebski34122b52009-05-11 03:29:40 +02007046
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007047 my $show_sizes = gitweb_check_feature('show-sizes');
7048 my $have_blame = gitweb_check_feature('blame');
7049
Jakub Narebski34122b52009-05-11 03:29:40 +02007050 my @entries = ();
7051 {
7052 local $/ = "\0";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007053 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
7054 ($show_sizes ? '-l' : ()), @extra_options, $hash
Jakub Narebski34122b52009-05-11 03:29:40 +02007055 or die_error(500, "Open git-ls-tree failed");
7056 @entries = map { chomp; $_ } <$fd>;
7057 close $fd
7058 or die_error(404, "Reading tree failed");
7059 }
Kay Sieversd63577d2005-08-07 20:18:13 +02007060
Jakub Narebski847e01f2006-08-14 02:05:47 +02007061 my $refs = git_get_references();
7062 my $ref = format_ref_marker($refs, $hash_base);
Kay Sievers12a88f22005-08-07 20:02:47 +02007063 git_header_html();
Jakub Narebski300454f2006-10-21 17:53:09 +02007064 my $basedir = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02007065 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Petr Baudiscae18622006-09-22 03:19:41 +02007066 my @views_nav = ();
7067 if (defined $file_name) {
7068 push @views_nav,
Jakub Narebskia3823e52007-11-01 13:06:29 +01007069 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02007070 "history"),
7071 $cgi->a({-href => href(action=>"tree",
7072 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02007073 "HEAD"),
Petr Baudiscae18622006-09-22 03:19:41 +02007074 }
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007075 my $snapshot_links = format_snapshot_links($hash);
7076 if (defined $snapshot_links) {
Petr Baudiscae18622006-09-22 03:19:41 +02007077 # FIXME: Should be available when we have no hash base as well.
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007078 push @views_nav, $snapshot_links;
Petr Baudiscae18622006-09-22 03:19:41 +02007079 }
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007080 git_print_page_nav('tree','', $hash_base, undef, undef,
7081 join(' | ', @views_nav));
Jakub Narebski847e01f2006-08-14 02:05:47 +02007082 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
Kay Sieversd63577d2005-08-07 20:18:13 +02007083 } else {
Jakub Narebskifa702002006-08-31 00:35:07 +02007084 undef $hash_base;
Kay Sieversd63577d2005-08-07 20:18:13 +02007085 print "<div class=\"page_nav\">\n";
7086 print "<br/><br/></div>\n";
Jakub Narebski3017ed62010-12-15 00:34:01 +01007087 print "<div class=\"title\">".esc_html($hash)."</div>\n";
Kay Sieversd63577d2005-08-07 20:18:13 +02007088 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007089 if (defined $file_name) {
Jakub Narebski300454f2006-10-21 17:53:09 +02007090 $basedir = $file_name;
7091 if ($basedir ne '' && substr($basedir, -1) ne '/') {
7092 $basedir .= '/';
7093 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02007094 git_print_page_path($file_name, 'tree', $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02007095 }
Kay Sieversfbb592a2005-08-07 20:12:11 +02007096 print "<div class=\"page_body\">\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01007097 print "<table class=\"tree\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07007098 my $alternate = 1;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007099 # '..' (top directory) link if possible
7100 if (defined $hash_base &&
7101 defined $file_name && $file_name =~ m![^/]+$!) {
7102 if ($alternate) {
7103 print "<tr class=\"dark\">\n";
7104 } else {
7105 print "<tr class=\"light\">\n";
7106 }
7107 $alternate ^= 1;
7108
7109 my $up = $file_name;
7110 $up =~ s!/?[^/]+$!!;
7111 undef $up unless $up;
7112 # based on git_print_tree_entry
7113 print '<td class="mode">' . mode_str('040000') . "</td>\n";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007114 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007115 print '<td class="list">';
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007116 print $cgi->a({-href => href(action=>"tree",
7117 hash_base=>$hash_base,
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007118 file_name=>$up)},
7119 "..");
7120 print "</td>\n";
7121 print "<td class=\"link\"></td>\n";
7122
7123 print "</tr>\n";
7124 }
Kay Sievers161332a2005-08-07 19:49:46 +02007125 foreach my $line (@entries) {
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007126 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
Jakub Narebskicb849b42006-08-31 00:32:15 +02007127
Kay Sieversbddec012005-08-07 20:25:42 +02007128 if ($alternate) {
Kay Sieversc994d622005-08-07 20:27:18 +02007129 print "<tr class=\"dark\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007130 } else {
Kay Sieversc994d622005-08-07 20:27:18 +02007131 print "<tr class=\"light\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007132 }
7133 $alternate ^= 1;
Jakub Narebskicb849b42006-08-31 00:32:15 +02007134
Jakub Narebski300454f2006-10-21 17:53:09 +02007135 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
Jakub Narebskifa702002006-08-31 00:35:07 +02007136
Kay Sievers42f7eb92005-08-07 20:21:46 +02007137 print "</tr>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02007138 }
Kay Sievers42f7eb92005-08-07 20:21:46 +02007139 print "</table>\n" .
7140 "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02007141 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007142}
7143
Mark Radab6292752009-11-07 16:13:29 +01007144sub snapshot_name {
7145 my ($project, $hash) = @_;
7146
7147 # path/to/project.git -> project
7148 # path/to/project/.git -> project
7149 my $name = to_utf8($project);
7150 $name =~ s,([^/])/*\.git$,$1,;
7151 $name = basename($name);
7152 # sanitize name
7153 $name =~ s/[[:cntrl:]]/?/g;
7154
7155 my $ver = $hash;
7156 if ($hash =~ /^[0-9a-fA-F]+$/) {
7157 # shorten SHA-1 hash
7158 my $full_hash = git_get_full_hash($project, $hash);
7159 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
7160 $ver = git_get_short_hash($project, $hash);
7161 }
7162 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
7163 # tags don't need shortened SHA-1 hash
7164 $ver = $1;
7165 } else {
7166 # branches and other need shortened SHA-1 hash
7167 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
7168 $ver = $1;
7169 }
7170 $ver .= '-' . git_get_short_hash($project, $hash);
7171 }
7172 # in case of hierarchical branch names
7173 $ver =~ s!/!.!g;
7174
7175 # name = project-version_string
7176 $name = "$name-$ver";
7177
7178 return wantarray ? ($name, $name) : $name;
7179}
7180
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04007181sub exit_if_unmodified_since {
7182 my ($latest_epoch) = @_;
7183 our $cgi;
7184
7185 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7186 if (defined $if_modified) {
7187 my $since;
7188 if (eval { require HTTP::Date; 1; }) {
7189 $since = HTTP::Date::str2time($if_modified);
7190 } elsif (eval { require Time::ParseDate; 1; }) {
7191 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7192 }
7193 if (defined $since && $latest_epoch <= $since) {
7194 my %latest_date = parse_date($latest_epoch);
7195 print $cgi->header(
7196 -last_modified => $latest_date{'rfc2822'},
7197 -status => '304 Not Modified');
7198 goto DONE_GITWEB;
7199 }
7200 }
7201}
7202
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307203sub git_snapshot {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02007204 my $format = $input_params{'snapshot_format'};
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01007205 if (!@snapshot_fmts) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007206 die_error(403, "Snapshots not allowed");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007207 }
7208 # default to first supported snapshot format
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01007209 $format ||= $snapshot_fmts[0];
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007210 if ($format !~ m/^[a-z0-9]+$/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007211 die_error(400, "Invalid snapshot format parameter");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007212 } elsif (!exists($known_snapshot_formats{$format})) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007213 die_error(400, "Unknown snapshot format");
Mark A Rada1bfd3632009-08-06 10:25:39 -04007214 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
7215 die_error(403, "Snapshot format not allowed");
Mark Rada34b31a82009-08-25 00:59:48 -04007216 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
7217 die_error(403, "Unsupported snapshot format");
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05307218 }
7219
Mark Radafdb0c362009-09-26 13:46:08 -04007220 my $type = git_get_type("$hash^{}");
7221 if (!$type) {
7222 die_error(404, 'Object does not exist');
7223 } elsif ($type eq 'blob') {
7224 die_error(400, 'Object is not a tree-ish');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307225 }
7226
Mark Radab6292752009-11-07 16:13:29 +01007227 my ($name, $prefix) = snapshot_name($project, $hash);
7228 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
W. Trevor King8745db62012-03-29 08:45:49 -04007229
7230 my %co = parse_commit($hash);
7231 exit_if_unmodified_since($co{'committer_epoch'}) if %co;
7232
Mark Radab6292752009-11-07 16:13:29 +01007233 my $cmd = quote_command(
Lea Wiemann516381d2008-06-17 23:46:35 +02007234 git_cmd(), 'archive',
7235 "--format=$known_snapshot_formats{$format}{'format'}",
Mark Radab6292752009-11-07 16:13:29 +01007236 "--prefix=$prefix/", $hash);
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007237 if (exists $known_snapshot_formats{$format}{'compressor'}) {
Lea Wiemann516381d2008-06-17 23:46:35 +02007238 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
Mark Levedahl072570e2007-05-20 11:46:46 -04007239 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307240
Mark Radab6292752009-11-07 16:13:29 +01007241 $filename =~ s/(["\\])/\\$1/g;
W. Trevor King8745db62012-03-29 08:45:49 -04007242 my %latest_date;
7243 if (%co) {
7244 %latest_date = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
7245 }
7246
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02007247 print $cgi->header(
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007248 -type => $known_snapshot_formats{$format}{'type'},
Mark Radab6292752009-11-07 16:13:29 +01007249 -content_disposition => 'inline; filename="' . $filename . '"',
W. Trevor King8745db62012-03-29 08:45:49 -04007250 %co ? (-last_modified => $latest_date{'rfc2822'}) : (),
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02007251 -status => '200 OK');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307252
Mark Levedahl072570e2007-05-20 11:46:46 -04007253 open my $fd, "-|", $cmd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007254 or die_error(500, "Execute git-archive failed");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307255 binmode STDOUT, ':raw';
7256 print <$fd>;
7257 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
7258 close $fd;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307259}
7260
Jakub Narebski15f0b112009-11-13 02:02:13 +01007261sub git_log_generic {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007262 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
Jakub Narebski15f0b112009-11-13 02:02:13 +01007263
Jakub Narebski847e01f2006-08-14 02:05:47 +02007264 my $head = git_get_head_hash($project);
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007265 if (!defined $base) {
7266 $base = $head;
Kay Sievers0db37972005-08-07 20:24:35 +02007267 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02007268 if (!defined $page) {
7269 $page = 0;
Kay Sieversb87d78d2005-08-07 20:21:04 +02007270 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007271 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02007272
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007273 my $commit_hash = $base;
7274 if (defined $parent) {
7275 $commit_hash = "$parent..$base";
Jakub Narebski15f0b112009-11-13 02:02:13 +01007276 }
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007277 my @commitlist =
7278 parse_commits($commit_hash, 101, (100 * $page),
7279 defined $file_name ? ($file_name, "--full-history") : ());
Kay Sieversea4a6df2005-08-07 20:26:49 +02007280
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007281 my $ftype;
7282 if (!defined $file_hash && defined $file_name) {
7283 # some commits could have deleted file in question,
7284 # and not have it in tree, but one of them has to have it
7285 for (my $i = 0; $i < @commitlist; $i++) {
7286 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
7287 last if defined $file_hash;
7288 }
7289 }
7290 if (defined $file_hash) {
7291 $ftype = git_get_type($file_hash);
7292 }
7293 if (defined $file_name && !defined $ftype) {
7294 die_error(500, "Unknown type of object");
7295 }
7296 my %co;
7297 if (defined $file_name) {
7298 %co = parse_commit($base)
7299 or die_error(404, "Unknown commit object");
7300 }
7301
7302
7303 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
Jakub Narebski15f0b112009-11-13 02:02:13 +01007304 my $next_link = '';
Jakub Narebski42671ca2009-11-13 02:02:12 +01007305 if ($#commitlist >= 100) {
7306 $next_link =
7307 $cgi->a({-href => href(-replay=>1, page=>$page+1),
7308 -accesskey => "n", -title => "Alt-n"}, "next");
7309 }
Jakub Narebski15f0b112009-11-13 02:02:13 +01007310 my $patch_max = gitweb_get_feature('patches');
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007311 if ($patch_max && !defined $file_name) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007312 if ($patch_max < 0 || @commitlist <= $patch_max) {
7313 $paging_nav .= " &sdot; " .
7314 $cgi->a({-href => href(action=>"patches", -replay=>1)},
7315 "patches");
7316 }
7317 }
7318
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007319 git_header_html();
Jakub Narebski15f0b112009-11-13 02:02:13 +01007320 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007321 if (defined $file_name) {
7322 git_print_header_div('commit', esc_html($co{'title'}), $base);
7323 } else {
7324 git_print_header_div('summary', $project)
7325 }
7326 git_print_page_path($file_name, $ftype, $hash_base)
7327 if (defined $file_name);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007328
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007329 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
7330 $file_name, $file_hash, $ftype);
Jakub Narebski42671ca2009-11-13 02:02:12 +01007331
Kay Sievers034df392005-08-07 20:20:07 +02007332 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007333}
7334
Jakub Narebski15f0b112009-11-13 02:02:13 +01007335sub git_log {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007336 git_log_generic('log', \&git_log_body,
7337 $hash, $hash_parent);
Jakub Narebski15f0b112009-11-13 02:02:13 +01007338}
7339
Kay Sievers09bd7892005-08-07 20:21:23 +02007340sub git_commit {
Jakub Narebski9954f772006-11-18 23:35:41 +01007341 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02007342 my %co = parse_commit($hash)
7343 or die_error(404, "Unknown commit object");
Kay Sievers161332a2005-08-07 19:49:46 +02007344
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007345 my $parent = $co{'parent'};
7346 my $parents = $co{'parents'}; # listref
7347
7348 # we need to prepare $formats_nav before any parameter munging
7349 my $formats_nav;
7350 if (!defined $parent) {
7351 # --root commitdiff
7352 $formats_nav .= '(initial)';
7353 } elsif (@$parents == 1) {
7354 # single parent commit
7355 $formats_nav .=
7356 '(parent: ' .
7357 $cgi->a({-href => href(action=>"commit",
7358 hash=>$parent)},
7359 esc_html(substr($parent, 0, 7))) .
7360 ')';
7361 } else {
7362 # merge commit
7363 $formats_nav .=
7364 '(merge: ' .
7365 join(' ', map {
Jakub Narebskif9308a12007-03-23 21:04:31 +01007366 $cgi->a({-href => href(action=>"commit",
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007367 hash=>$_)},
7368 esc_html(substr($_, 0, 7)));
7369 } @$parents ) .
7370 ')';
7371 }
Jakub Narebski1655c982009-10-09 14:26:44 +02007372 if (gitweb_check_feature('patches') && @$parents <= 1) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007373 $formats_nav .= " | " .
7374 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7375 "patch");
7376 }
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007377
Kay Sieversd8a20ba2005-08-07 20:28:53 +02007378 if (!defined $parent) {
Jakub Narebskib9182982006-07-30 18:28:34 -07007379 $parent = "--root";
Kay Sievers6191f8e2005-08-07 20:19:56 +02007380 }
Jakub Narebski549ab4a2006-12-15 17:53:45 +01007381 my @difftree;
Jakub Narebski208ecb22007-05-07 01:10:08 +02007382 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
7383 @diff_opts,
7384 (@$parents <= 1 ? $parent : '-c'),
7385 $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007386 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski208ecb22007-05-07 01:10:08 +02007387 @difftree = map { chomp; $_ } <$fd>;
Lea Wiemann074afaa2008-06-19 22:03:21 +02007388 close $fd or die_error(404, "Reading git-diff-tree failed");
Kay Sievers11044292005-10-19 03:18:45 +02007389
7390 # non-textual hash id's can be cached
7391 my $expires;
7392 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7393 $expires = "+1d";
7394 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007395 my $refs = git_get_references();
7396 my $ref = format_ref_marker($refs, $co{'id'});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05307397
Jakub Narebski594e2122006-07-31 02:21:52 +02007398 git_header_html(undef, $expires);
Petr Baudisa1441542006-10-06 18:59:33 +02007399 git_print_page_nav('commit', '',
Jakub Narebski952c65f2006-08-22 16:52:50 +02007400 $hash, $co{'tree'}, $hash,
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007401 $formats_nav);
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07007402
Kay Sieversb87d78d2005-08-07 20:21:04 +02007403 if (defined $co{'parent'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007404 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02007405 } else {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007406 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02007407 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02007408 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01007409 "<table class=\"object_header\">\n";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02007410 git_print_authorship_rows(\%co);
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00007411 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007412 print "<tr>" .
7413 "<td>tree</td>" .
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00007414 "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007415 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
7416 class => "list"}, $co{'tree'}) .
Kay Sievers19806692005-08-07 20:26:27 +02007417 "</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007418 "<td class=\"link\">" .
7419 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
7420 "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007421 my $snapshot_links = format_snapshot_links($hash);
7422 if (defined $snapshot_links) {
7423 print " | " . $snapshot_links;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307424 }
7425 print "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02007426 "</tr>\n";
Jakub Narebski549ab4a2006-12-15 17:53:45 +01007427
Kay Sievers3e029292005-08-07 20:05:15 +02007428 foreach my $par (@$parents) {
Kay Sieversbddec012005-08-07 20:25:42 +02007429 print "<tr>" .
7430 "<td>parent</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007431 "<td class=\"sha1\">" .
7432 $cgi->a({-href => href(action=>"commit", hash=>$par),
7433 class => "list"}, $par) .
7434 "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02007435 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02007436 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007437 " | " .
Jakub Narebskif2e60942006-09-03 23:43:03 +02007438 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
Kay Sieversbddec012005-08-07 20:25:42 +02007439 "</td>" .
7440 "</tr>\n";
Kay Sievers3e029292005-08-07 20:05:15 +02007441 }
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02007442 print "</table>".
Kay Sieversb87d78d2005-08-07 20:21:04 +02007443 "</div>\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02007444
Kay Sieversfbb592a2005-08-07 20:12:11 +02007445 print "<div class=\"page_body\">\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02007446 git_print_log($co{'comment'});
Kay Sievers927dcec2005-08-07 20:18:44 +02007447 print "</div>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02007448
Jakub Narebski208ecb22007-05-07 01:10:08 +02007449 git_difftree_body(\@difftree, $hash, @$parents);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02007450
Kay Sievers12a88f22005-08-07 20:02:47 +02007451 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007452}
7453
Jakub Narebskica946012006-12-10 13:25:47 +01007454sub git_object {
7455 # object is defined by:
7456 # - hash or hash_base alone
7457 # - hash_base and file_name
7458 my $type;
7459
7460 # - hash or hash_base alone
7461 if ($hash || ($hash_base && !defined $file_name)) {
7462 my $object_id = $hash || $hash_base;
7463
Lea Wiemann516381d2008-06-17 23:46:35 +02007464 open my $fd, "-|", quote_command(
7465 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
Lea Wiemann074afaa2008-06-19 22:03:21 +02007466 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007467 $type = <$fd>;
7468 chomp $type;
7469 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007470 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007471
7472 # - hash_base and file_name
7473 } elsif ($hash_base && defined $file_name) {
7474 $file_name =~ s,/+$,,;
7475
7476 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
Lea Wiemann074afaa2008-06-19 22:03:21 +02007477 or die_error(404, "Base object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007478
7479 # here errors should not hapen
7480 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007481 or die_error(500, "Open git-ls-tree failed");
Jakub Narebskica946012006-12-10 13:25:47 +01007482 my $line = <$fd>;
7483 close $fd;
7484
7485 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
7486 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007487 die_error(404, "File or directory for given base does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007488 }
7489 $type = $2;
7490 $hash = $3;
7491 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007492 die_error(400, "Not enough information to find object");
Jakub Narebskica946012006-12-10 13:25:47 +01007493 }
7494
7495 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
7496 hash=>$hash, hash_base=>$hash_base,
7497 file_name=>$file_name),
7498 -status => '302 Found');
7499}
7500
Kay Sievers09bd7892005-08-07 20:21:23 +02007501sub git_blobdiff {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007502 my $format = shift || 'html';
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007503 my $diff_style = $input_params{'diff_style'} || 'inline';
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007504
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007505 my $fd;
7506 my @difftree;
7507 my %diffinfo;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007508 my $expires;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007509
7510 # preparing $fd and %diffinfo for git_patchset_body
7511 # new style URI
7512 if (defined $hash_base && defined $hash_parent_base) {
7513 if (defined $file_name) {
7514 # read raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007515 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7516 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02007517 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007518 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007519 @difftree = map { chomp; $_ } <$fd>;
7520 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007521 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007522 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02007523 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007524
Jakub Narebski0aea3372006-08-27 23:45:26 +02007525 } elsif (defined $hash &&
7526 $hash =~ /[0-9a-fA-F]{40}/) {
7527 # try to find filename from $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007528
7529 # read filtered raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007530 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7531 $hash_parent_base, $hash_base, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007532 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007533 @difftree =
7534 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
7535 # $hash == to_id
7536 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
7537 map { chomp; $_ } <$fd>;
7538 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007539 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007540 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02007541 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007542
7543 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007544 die_error(400, "Missing one of the blob diff parameters");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007545 }
7546
7547 if (@difftree > 1) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007548 die_error(400, "Ambiguous blob diff specification");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007549 }
7550
7551 %diffinfo = parse_difftree_raw_line($difftree[0]);
Jakub Narebski9d301452007-11-01 12:38:08 +01007552 $file_parent ||= $diffinfo{'from_file'} || $file_name;
7553 $file_name ||= $diffinfo{'to_file'};
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007554
7555 $hash_parent ||= $diffinfo{'from_id'};
7556 $hash ||= $diffinfo{'to_id'};
7557
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007558 # non-textual hash id's can be cached
7559 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
7560 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
7561 $expires = '+1d';
7562 }
7563
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007564 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007565 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski957d6ea2007-04-05 13:45:41 +02007566 '-p', ($format eq 'html' ? "--full-index" : ()),
7567 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02007568 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007569 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007570 }
7571
Junio C Hamanob54dc9f2008-12-16 19:42:02 -08007572 # old/legacy style URI -- not generated anymore since 1.4.3.
7573 if (!%diffinfo) {
7574 die_error('404 Not Found', "Missing one of the blob diff parameters")
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007575 }
7576
7577 # header
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007578 if ($format eq 'html') {
7579 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01007580 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02007581 "raw");
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007582 $formats_nav .= diff_style_nav($diff_style);
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007583 git_header_html(undef, $expires);
7584 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7585 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7586 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7587 } else {
7588 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
Jakub Narebski3017ed62010-12-15 00:34:01 +01007589 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007590 }
7591 if (defined $file_name) {
7592 git_print_page_path($file_name, "blob", $hash_base);
7593 } else {
7594 print "<div class=\"page_path\"></div>\n";
7595 }
7596
7597 } elsif ($format eq 'plain') {
7598 print $cgi->header(
7599 -type => 'text/plain',
7600 -charset => 'utf-8',
7601 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07007602 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007603
7604 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7605
Kay Sievers09bd7892005-08-07 20:21:23 +02007606 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007607 die_error(400, "Unknown blobdiff format");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007608 }
7609
7610 # patch
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007611 if ($format eq 'html') {
7612 print "<div class=\"page_body\">\n";
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007613
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007614 git_patchset_body($fd, $diff_style,
7615 [ \%diffinfo ], $hash_base, $hash_parent_base);
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007616 close $fd;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007617
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007618 print "</div>\n"; # class="page_body"
7619 git_footer_html();
7620
7621 } else {
7622 while (my $line = <$fd>) {
Jakub Narebski403d0902006-11-08 11:48:56 +01007623 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
7624 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007625
7626 print $line;
7627
7628 last if $line =~ m!^\+\+\+!;
7629 }
7630 local $/ = undef;
7631 print <$fd>;
7632 close $fd;
7633 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007634}
7635
Kay Sievers19806692005-08-07 20:26:27 +02007636sub git_blobdiff_plain {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007637 git_blobdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02007638}
7639
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007640# assumes that it is added as later part of already existing navigation,
7641# so it returns "| foo | bar" rather than just "foo | bar"
7642sub diff_style_nav {
7643 my ($diff_style, $is_combined) = @_;
7644 $diff_style ||= 'inline';
7645
7646 return "" if ($is_combined);
7647
7648 my @styles = (inline => 'inline', 'sidebyside' => 'side by side');
7649 my %styles = @styles;
7650 @styles =
7651 @styles[ map { $_ * 2 } 0..$#styles/2 ];
7652
7653 return join '',
7654 map { " | ".$_ }
7655 map {
7656 $_ eq $diff_style ? $styles{$_} :
7657 $cgi->a({-href => href(-replay=>1, diff_style => $_)}, $styles{$_})
7658 } @styles;
7659}
7660
Kay Sievers09bd7892005-08-07 20:21:23 +02007661sub git_commitdiff {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01007662 my %params = @_;
7663 my $format = $params{-format} || 'html';
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007664 my $diff_style = $input_params{'diff_style'} || 'inline';
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007665
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007666 my ($patch_max) = gitweb_get_feature('patches');
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007667 if ($format eq 'patch') {
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007668 die_error(403, "Patch view not allowed") unless $patch_max;
7669 }
7670
Jakub Narebski9954f772006-11-18 23:35:41 +01007671 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02007672 my %co = parse_commit($hash)
7673 or die_error(404, "Unknown commit object");
Jakub Narebski151602d2006-10-23 00:37:56 +02007674
Jakub Narebskicd030c32007-06-08 13:33:28 +02007675 # choose format for commitdiff for merge
7676 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7677 $hash_parent = '--cc';
7678 }
7679 # we need to prepare $formats_nav before almost any parameter munging
Jakub Narebski151602d2006-10-23 00:37:56 +02007680 my $formats_nav;
7681 if ($format eq 'html') {
7682 $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01007683 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007684 "raw");
Jakub Narebski1655c982009-10-09 14:26:44 +02007685 if ($patch_max && @{$co{'parents'}} <= 1) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007686 $formats_nav .= " | " .
7687 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7688 "patch");
7689 }
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007690 $formats_nav .= diff_style_nav($diff_style, @{$co{'parents'}} > 1);
Jakub Narebski151602d2006-10-23 00:37:56 +02007691
Jakub Narebskicd030c32007-06-08 13:33:28 +02007692 if (defined $hash_parent &&
7693 $hash_parent ne '-c' && $hash_parent ne '--cc') {
Jakub Narebski151602d2006-10-23 00:37:56 +02007694 # commitdiff with two commits given
7695 my $hash_parent_short = $hash_parent;
7696 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7697 $hash_parent_short = substr($hash_parent, 0, 7);
7698 }
7699 $formats_nav .=
Jakub Narebskiada3e1f2007-06-08 13:26:31 +02007700 ' (from';
7701 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7702 if ($co{'parents'}[$i] eq $hash_parent) {
7703 $formats_nav .= ' parent ' . ($i+1);
7704 last;
7705 }
7706 }
7707 $formats_nav .= ': ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007708 $cgi->a({-href => href(-replay=>1,
7709 hash=>$hash_parent, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007710 esc_html($hash_parent_short)) .
7711 ')';
7712 } elsif (!$co{'parent'}) {
7713 # --root commitdiff
7714 $formats_nav .= ' (initial)';
7715 } elsif (scalar @{$co{'parents'}} == 1) {
7716 # single parent commit
7717 $formats_nav .=
7718 ' (parent: ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007719 $cgi->a({-href => href(-replay=>1,
7720 hash=>$co{'parent'}, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007721 esc_html(substr($co{'parent'}, 0, 7))) .
7722 ')';
7723 } else {
7724 # merge commit
Jakub Narebskicd030c32007-06-08 13:33:28 +02007725 if ($hash_parent eq '--cc') {
7726 $formats_nav .= ' | ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007727 $cgi->a({-href => href(-replay=>1,
Jakub Narebskicd030c32007-06-08 13:33:28 +02007728 hash=>$hash, hash_parent=>'-c')},
7729 'combined');
7730 } else { # $hash_parent eq '-c'
7731 $formats_nav .= ' | ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007732 $cgi->a({-href => href(-replay=>1,
Jakub Narebskicd030c32007-06-08 13:33:28 +02007733 hash=>$hash, hash_parent=>'--cc')},
7734 'compact');
7735 }
Jakub Narebski151602d2006-10-23 00:37:56 +02007736 $formats_nav .=
7737 ' (merge: ' .
7738 join(' ', map {
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007739 $cgi->a({-href => href(-replay=>1,
7740 hash=>$_, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007741 esc_html(substr($_, 0, 7)));
7742 } @{$co{'parents'}} ) .
7743 ')';
7744 }
7745 }
7746
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007747 my $hash_parent_param = $hash_parent;
Jakub Narebskicd030c32007-06-08 13:33:28 +02007748 if (!defined $hash_parent_param) {
7749 # --cc for multiple parents, --root for parentless
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007750 $hash_parent_param =
Jakub Narebskicd030c32007-06-08 13:33:28 +02007751 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
Kay Sieversbddec012005-08-07 20:25:42 +02007752 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007753
7754 # read commitdiff
7755 my $fd;
7756 my @difftree;
Jakub Narebskieee08902006-08-24 00:15:14 +02007757 if ($format eq 'html') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007758 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007759 "--no-commit-id", "--patch-with-raw", "--full-index",
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007760 $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007761 or die_error(500, "Open git-diff-tree failed");
Jakub Narebskieee08902006-08-24 00:15:14 +02007762
Jakub Narebski04408c32006-11-18 23:35:38 +01007763 while (my $line = <$fd>) {
7764 chomp $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02007765 # empty line ends raw part of diff-tree output
7766 last unless $line;
Jakub Narebski493e01d2007-05-07 01:10:06 +02007767 push @difftree, scalar parse_difftree_raw_line($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02007768 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007769
Jakub Narebskieee08902006-08-24 00:15:14 +02007770 } elsif ($format eq 'plain') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007771 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007772 '-p', $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007773 or die_error(500, "Open git-diff-tree failed");
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007774 } elsif ($format eq 'patch') {
7775 # For commit ranges, we limit the output to the number of
7776 # patches specified in the 'patches' feature.
7777 # For single commits, we limit the output to a single patch,
7778 # diverging from the git-format-patch default.
7779 my @commit_spec = ();
7780 if ($hash_parent) {
7781 if ($patch_max > 0) {
7782 push @commit_spec, "-$patch_max";
7783 }
7784 push @commit_spec, '-n', "$hash_parent..$hash";
7785 } else {
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +01007786 if ($params{-single}) {
7787 push @commit_spec, '-1';
7788 } else {
7789 if ($patch_max > 0) {
7790 push @commit_spec, "-$patch_max";
7791 }
7792 push @commit_spec, "-n";
7793 }
7794 push @commit_spec, '--root', $hash;
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007795 }
Pavan Kumar Sunkara04794fd2010-05-10 18:41:35 +02007796 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
7797 '--encoding=utf8', '--stdout', @commit_spec
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007798 or die_error(500, "Open git-format-patch failed");
Jakub Narebskieee08902006-08-24 00:15:14 +02007799 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007800 die_error(400, "Unknown commitdiff format");
Jakub Narebskieee08902006-08-24 00:15:14 +02007801 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02007802
Kay Sievers11044292005-10-19 03:18:45 +02007803 # non-textual hash id's can be cached
7804 my $expires;
7805 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7806 $expires = "+1d";
7807 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007808
Jakub Narebskieee08902006-08-24 00:15:14 +02007809 # write commit message
7810 if ($format eq 'html') {
7811 my $refs = git_get_references();
7812 my $ref = format_ref_marker($refs, $co{'id'});
Kay Sievers19806692005-08-07 20:26:27 +02007813
Jakub Narebskieee08902006-08-24 00:15:14 +02007814 git_header_html(undef, $expires);
7815 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
7816 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
Giuseppe Bilottaf88bafa2009-06-30 00:00:49 +02007817 print "<div class=\"title_text\">\n" .
7818 "<table class=\"object_header\">\n";
7819 git_print_authorship_rows(\%co);
7820 print "</table>".
7821 "</div>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02007822 print "<div class=\"page_body\">\n";
Jakub Narebski82560982006-10-24 13:55:33 +02007823 if (@{$co{'comment'}} > 1) {
7824 print "<div class=\"log\">\n";
7825 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
7826 print "</div>\n"; # class="log"
7827 }
Kay Sievers1b1cd422005-08-07 20:28:01 +02007828
Jakub Narebskieee08902006-08-24 00:15:14 +02007829 } elsif ($format eq 'plain') {
7830 my $refs = git_get_references("tags");
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007831 my $tagname = git_get_rev_name_tags($hash);
Jakub Narebskieee08902006-08-24 00:15:14 +02007832 my $filename = basename($project) . "-$hash.patch";
7833
7834 print $cgi->header(
7835 -type => 'text/plain',
7836 -charset => 'utf-8',
7837 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07007838 -content_disposition => 'inline; filename="' . "$filename" . '"');
Jakub Narebskieee08902006-08-24 00:15:14 +02007839 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
Yasushi SHOJI77202242008-01-29 21:16:02 +09007840 print "From: " . to_utf8($co{'author'}) . "\n";
7841 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7842 print "Subject: " . to_utf8($co{'title'}) . "\n";
7843
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007844 print "X-Git-Tag: $tagname\n" if $tagname;
Jakub Narebskieee08902006-08-24 00:15:14 +02007845 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007846
Jakub Narebskieee08902006-08-24 00:15:14 +02007847 foreach my $line (@{$co{'comment'}}) {
Yasushi SHOJI77202242008-01-29 21:16:02 +09007848 print to_utf8($line) . "\n";
Kay Sievers19806692005-08-07 20:26:27 +02007849 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007850 print "---\n\n";
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007851 } elsif ($format eq 'patch') {
7852 my $filename = basename($project) . "-$hash.patch";
7853
7854 print $cgi->header(
7855 -type => 'text/plain',
7856 -charset => 'utf-8',
7857 -expires => $expires,
7858 -content_disposition => 'inline; filename="' . "$filename" . '"');
Kay Sievers19806692005-08-07 20:26:27 +02007859 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007860
7861 # write patch
7862 if ($format eq 'html') {
Jakub Narebskicd030c32007-06-08 13:33:28 +02007863 my $use_parents = !defined $hash_parent ||
7864 $hash_parent eq '-c' || $hash_parent eq '--cc';
7865 git_difftree_body(\@difftree, $hash,
7866 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebskib4657e72006-08-28 14:48:14 +02007867 print "<br/>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02007868
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007869 git_patchset_body($fd, $diff_style,
7870 \@difftree, $hash,
Jakub Narebskicd030c32007-06-08 13:33:28 +02007871 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebski157e43b2006-08-24 19:34:36 +02007872 close $fd;
Jakub Narebskieee08902006-08-24 00:15:14 +02007873 print "</div>\n"; # class="page_body"
7874 git_footer_html();
7875
7876 } elsif ($format eq 'plain') {
7877 local $/ = undef;
7878 print <$fd>;
7879 close $fd
7880 or print "Reading git-diff-tree failed\n";
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007881 } elsif ($format eq 'patch') {
7882 local $/ = undef;
7883 print <$fd>;
7884 close $fd
7885 or print "Reading git-format-patch failed\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02007886 }
7887}
7888
7889sub git_commitdiff_plain {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01007890 git_commitdiff(-format => 'plain');
Kay Sievers19806692005-08-07 20:26:27 +02007891}
7892
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007893# format-patch-style patches
7894sub git_patch {
Jakub Narebski1655c982009-10-09 14:26:44 +02007895 git_commitdiff(-format => 'patch', -single => 1);
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +01007896}
7897
7898sub git_patches {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01007899 git_commitdiff(-format => 'patch');
Kay Sievers09bd7892005-08-07 20:21:23 +02007900}
7901
7902sub git_history {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007903 git_log_generic('history', \&git_history_body,
7904 $hash_base, $hash_parent_base,
7905 $file_name, $hash);
Kay Sievers161332a2005-08-07 19:49:46 +02007906}
Kay Sievers19806692005-08-07 20:26:27 +02007907
7908sub git_search {
Jakub Narebskie0ca3642011-06-22 17:28:52 +02007909 $searchtype ||= 'commit';
7910
7911 # check if appropriate features are enabled
7912 gitweb_check_feature('search')
7913 or die_error(403, "Search is disabled");
7914 if ($searchtype eq 'pickaxe') {
7915 # pickaxe may take all resources of your box and run for several minutes
7916 # with every query - so decide by yourself how public you make this feature
7917 gitweb_check_feature('pickaxe')
7918 or die_error(403, "Pickaxe search is disabled");
7919 }
7920 if ($searchtype eq 'grep') {
7921 # grep search might be potentially CPU-intensive, too
7922 gitweb_check_feature('grep')
7923 or die_error(403, "Grep search is disabled");
7924 }
7925
Kay Sievers19806692005-08-07 20:26:27 +02007926 if (!defined $searchtext) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007927 die_error(400, "Text field is empty");
Kay Sievers19806692005-08-07 20:26:27 +02007928 }
7929 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007930 $hash = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02007931 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007932 my %co = parse_commit($hash);
Kay Sievers19806692005-08-07 20:26:27 +02007933 if (!%co) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007934 die_error(404, "Unknown commit object");
Kay Sievers19806692005-08-07 20:26:27 +02007935 }
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00007936 if (!defined $page) {
7937 $page = 0;
7938 }
Jakub Narebski04f7a942006-09-11 00:29:27 +02007939
Jakub Narebski16f20722011-06-22 17:28:53 +02007940 if ($searchtype eq 'commit' ||
7941 $searchtype eq 'author' ||
7942 $searchtype eq 'committer') {
7943 git_search_message(%co);
7944 } elsif ($searchtype eq 'pickaxe') {
7945 git_search_changes(%co);
7946 } elsif ($searchtype eq 'grep') {
7947 git_search_files(%co);
Jakub Narebski1ae05be2011-06-22 17:28:55 +02007948 } else {
7949 die_error(400, "Unknown search type");
Kay Sieversc994d622005-08-07 20:27:18 +02007950 }
Kay Sievers19806692005-08-07 20:26:27 +02007951}
7952
Petr Baudis88ad7292006-10-24 05:15:46 +02007953sub git_search_help {
7954 git_header_html();
7955 git_print_page_nav('','', $hash,$hash,$hash);
7956 print <<EOT;
Petr Baudis0e559912008-02-26 13:22:08 +01007957<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7958regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7959the pattern entered is recognized as the POSIX extended
7960<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7961insensitive).</p>
Petr Baudis88ad7292006-10-24 05:15:46 +02007962<dl>
7963<dt><b>commit</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01007964<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02007965EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08007966 my $have_grep = gitweb_check_feature('grep');
Petr Baudise7738552007-05-17 04:31:12 +02007967 if ($have_grep) {
7968 print <<EOT;
7969<dt><b>grep</b></dt>
7970<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
Petr Baudis0e559912008-02-26 13:22:08 +01007971 a different one) are searched for the given pattern. On large trees, this search can take
7972a while and put some strain on the server, so please use it with some consideration. Note that
7973due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7974case-sensitive.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02007975EOT
7976 }
7977 print <<EOT;
Petr Baudis88ad7292006-10-24 05:15:46 +02007978<dt><b>author</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01007979<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 +02007980<dt><b>committer</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01007981<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 +02007982EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08007983 my $have_pickaxe = gitweb_check_feature('pickaxe');
Petr Baudis88ad7292006-10-24 05:15:46 +02007984 if ($have_pickaxe) {
7985 print <<EOT;
7986<dt><b>pickaxe</b></dt>
7987<dd>All commits that caused the string to appear or disappear from any file (changes that
7988added, removed or "modified" the string) will be listed. This search can take a while and
Petr Baudis0e559912008-02-26 13:22:08 +01007989takes a lot of strain on the server, so please use it wisely. Note that since you may be
7990interested even in changes just changing the case as well, this search is case sensitive.</dd>
Petr Baudis88ad7292006-10-24 05:15:46 +02007991EOT
7992 }
7993 print "</dl>\n";
7994 git_footer_html();
7995}
7996
Kay Sievers19806692005-08-07 20:26:27 +02007997sub git_shortlog {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007998 git_log_generic('shortlog', \&git_shortlog_body,
7999 $hash, $hash_parent);
Kay Sievers19806692005-08-07 20:26:27 +02008000}
Jakub Narebski717b8312006-07-31 21:22:15 +02008001
8002## ......................................................................
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008003## feeds (RSS, Atom; OPML)
Jakub Narebski717b8312006-07-31 21:22:15 +02008004
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008005sub git_feed {
8006 my $format = shift || 'atom';
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08008007 my $have_blame = gitweb_check_feature('blame');
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008008
8009 # Atom: http://www.atomenabled.org/developers/syndication/
8010 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
8011 if ($format ne 'rss' && $format ne 'atom') {
Lea Wiemann074afaa2008-06-19 22:03:21 +02008012 die_error(400, "Unknown web feed format");
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008013 }
8014
8015 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
8016 my $head = $hash || 'HEAD';
Jakub Narebski311e5522008-02-26 13:22:06 +01008017 my @commitlist = parse_commits($head, 150, 0, $file_name);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008018
8019 my %latest_commit;
8020 my %latest_date;
8021 my $content_type = "application/$format+xml";
8022 if (defined $cgi->http('HTTP_ACCEPT') &&
8023 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
8024 # browser (feed reader) prefers text/xml
8025 $content_type = 'text/xml';
8026 }
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008027 if (defined($commitlist[0])) {
8028 %latest_commit = %{$commitlist[0]};
Giuseppe Bilottacd956c72009-01-26 12:50:16 +01008029 my $latest_epoch = $latest_commit{'committer_epoch'};
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04008030 exit_if_unmodified_since($latest_epoch);
8031 %latest_date = parse_date($latest_epoch, $latest_commit{'comitter_tz'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008032 }
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04008033 print $cgi->header(
8034 -type => $content_type,
8035 -charset => 'utf-8',
8036 %latest_date ? (-last_modified => $latest_date{'rfc2822'}) : (),
8037 -status => '200 OK');
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008038
8039 # Optimization: skip generating the body if client asks only
8040 # for Last-Modified date.
8041 return if ($cgi->request_method() eq 'HEAD');
8042
8043 # header variables
8044 my $title = "$site_name - $project/$action";
8045 my $feed_type = 'log';
8046 if (defined $hash) {
8047 $title .= " - '$hash'";
8048 $feed_type = 'branch log';
8049 if (defined $file_name) {
8050 $title .= " :: $file_name";
8051 $feed_type = 'history';
8052 }
8053 } elsif (defined $file_name) {
8054 $title .= " - $file_name";
8055 $feed_type = 'history';
8056 }
8057 $title .= " $feed_type";
8058 my $descr = git_get_project_description($project);
8059 if (defined $descr) {
8060 $descr = esc_html($descr);
8061 } else {
8062 $descr = "$project " .
8063 ($format eq 'rss' ? 'RSS' : 'Atom') .
8064 " feed";
8065 }
8066 my $owner = git_get_project_owner($project);
8067 $owner = esc_html($owner);
8068
8069 #header
8070 my $alt_url;
8071 if (defined $file_name) {
8072 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
8073 } elsif (defined $hash) {
8074 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
8075 } else {
8076 $alt_url = href(-full=>1, action=>"summary");
8077 }
8078 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
8079 if ($format eq 'rss') {
8080 print <<XML;
Jakub Narebski59b9f612006-08-22 23:42:53 +02008081<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
8082<channel>
Jakub Narebski59b9f612006-08-22 23:42:53 +02008083XML
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008084 print "<title>$title</title>\n" .
8085 "<link>$alt_url</link>\n" .
8086 "<description>$descr</description>\n" .
Giuseppe Bilotta3ac109a2009-01-26 12:50:13 +01008087 "<language>en</language>\n" .
8088 # project owner is responsible for 'editorial' content
8089 "<managingEditor>$owner</managingEditor>\n";
Giuseppe Bilotta1ba68ce2009-01-26 12:50:11 +01008090 if (defined $logo || defined $favicon) {
8091 # prefer the logo to the favicon, since RSS
8092 # doesn't allow both
8093 my $img = esc_url($logo || $favicon);
8094 print "<image>\n" .
8095 "<url>$img</url>\n" .
8096 "<title>$title</title>\n" .
8097 "<link>$alt_url</link>\n" .
8098 "</image>\n";
8099 }
Giuseppe Bilotta0cf31282009-01-26 12:50:14 +01008100 if (%latest_date) {
8101 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
8102 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
8103 }
Giuseppe Bilottaad59a7a2009-01-26 12:50:12 +01008104 print "<generator>gitweb v.$version/$git_version</generator>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008105 } elsif ($format eq 'atom') {
8106 print <<XML;
8107<feed xmlns="http://www.w3.org/2005/Atom">
8108XML
8109 print "<title>$title</title>\n" .
8110 "<subtitle>$descr</subtitle>\n" .
8111 '<link rel="alternate" type="text/html" href="' .
8112 $alt_url . '" />' . "\n" .
8113 '<link rel="self" type="' . $content_type . '" href="' .
8114 $cgi->self_url() . '" />' . "\n" .
8115 "<id>" . href(-full=>1) . "</id>\n" .
8116 # use project owner for feed author
8117 "<author><name>$owner</name></author>\n";
8118 if (defined $favicon) {
8119 print "<icon>" . esc_url($favicon) . "</icon>\n";
8120 }
Jonathan Nieder9d9f5e72010-09-03 19:44:39 -05008121 if (defined $logo) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008122 # not twice as wide as tall: 72 x 27 pixels
Jakub Narebskie1147262006-12-04 14:09:43 +01008123 print "<logo>" . esc_url($logo) . "</logo>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008124 }
8125 if (! %latest_date) {
8126 # dummy date to keep the feed valid until commits trickle in:
8127 print "<updated>1970-01-01T00:00:00Z</updated>\n";
8128 } else {
8129 print "<updated>$latest_date{'iso-8601'}</updated>\n";
8130 }
Giuseppe Bilottaad59a7a2009-01-26 12:50:12 +01008131 print "<generator version='$version/$git_version'>gitweb</generator>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008132 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008133
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008134 # contents
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008135 for (my $i = 0; $i <= $#commitlist; $i++) {
8136 my %co = %{$commitlist[$i]};
8137 my $commit = $co{'id'};
Jakub Narebski717b8312006-07-31 21:22:15 +02008138 # we read 150, we always show 30 and the ones more recent than 48 hours
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01008139 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02008140 last;
8141 }
Jakub Narebski6368d9f2011-03-19 23:53:55 +01008142 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008143
8144 # get list of changed files
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008145 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskic906b182007-05-19 02:47:51 +02008146 $co{'parent'} || "--root",
8147 $co{'id'}, "--", (defined $file_name ? $file_name : ())
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02008148 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02008149 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02008150 close $fd
8151 or next;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008152
8153 # print element (entry, item)
Florian La Rochee62a6412008-02-03 12:38:46 +01008154 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008155 if ($format eq 'rss') {
8156 print "<item>\n" .
8157 "<title>" . esc_html($co{'title'}) . "</title>\n" .
8158 "<author>" . esc_html($co{'author'}) . "</author>\n" .
8159 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
8160 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
8161 "<link>$co_url</link>\n" .
8162 "<description>" . esc_html($co{'title'}) . "</description>\n" .
8163 "<content:encoded>" .
8164 "<![CDATA[\n";
8165 } elsif ($format eq 'atom') {
8166 print "<entry>\n" .
8167 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
8168 "<updated>$cd{'iso-8601'}</updated>\n" .
Jakub Narebskiab23c192006-11-25 15:54:33 +01008169 "<author>\n" .
8170 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
8171 if ($co{'author_email'}) {
8172 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
8173 }
8174 print "</author>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008175 # use committer for contributor
Jakub Narebskiab23c192006-11-25 15:54:33 +01008176 "<contributor>\n" .
8177 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
8178 if ($co{'committer_email'}) {
8179 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
8180 }
8181 print "</contributor>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008182 "<published>$cd{'iso-8601'}</published>\n" .
8183 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
8184 "<id>$co_url</id>\n" .
8185 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
8186 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
8187 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008188 my $comment = $co{'comment'};
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008189 print "<pre>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008190 foreach my $line (@$comment) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008191 $line = esc_html($line);
8192 print "$line\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008193 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008194 print "</pre><ul>\n";
8195 foreach my $difftree_line (@difftree) {
8196 my %difftree = parse_difftree_raw_line($difftree_line);
8197 next if !$difftree{'from_id'};
8198
8199 my $file = $difftree{'file'} || $difftree{'to_file'};
8200
8201 print "<li>" .
8202 "[" .
8203 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
8204 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
8205 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
8206 file_name=>$file, file_parent=>$difftree{'from_file'}),
8207 -title => "diff"}, 'D');
8208 if ($have_blame) {
8209 print $cgi->a({-href => href(-full=>1, action=>"blame",
8210 file_name=>$file, hash_base=>$commit),
8211 -title => "blame"}, 'B');
Jakub Narebski717b8312006-07-31 21:22:15 +02008212 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008213 # if this is not a feed of a file history
8214 if (!defined $file_name || $file_name ne $file) {
8215 print $cgi->a({-href => href(-full=>1, action=>"history",
8216 file_name=>$file, hash=>$commit),
8217 -title => "history"}, 'H');
8218 }
8219 $file = esc_path($file);
8220 print "] ".
8221 "$file</li>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008222 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008223 if ($format eq 'rss') {
8224 print "</ul>]]>\n" .
8225 "</content:encoded>\n" .
8226 "</item>\n";
8227 } elsif ($format eq 'atom') {
8228 print "</ul>\n</div>\n" .
8229 "</content>\n" .
8230 "</entry>\n";
8231 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008232 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008233
8234 # end of feed
8235 if ($format eq 'rss') {
8236 print "</channel>\n</rss>\n";
Jakub Narebski3278fbc2009-05-11 19:37:28 +02008237 } elsif ($format eq 'atom') {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008238 print "</feed>\n";
8239 }
8240}
8241
8242sub git_rss {
8243 git_feed('rss');
8244}
8245
8246sub git_atom {
8247 git_feed('atom');
Jakub Narebski717b8312006-07-31 21:22:15 +02008248}
8249
8250sub git_opml {
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008251 my @list = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski12b14432011-04-29 19:51:56 +02008252 if (!@list) {
8253 die_error(404, "No projects found");
8254 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008255
Giuseppe Bilottaae357852009-01-02 13:49:30 +01008256 print $cgi->header(
8257 -type => 'text/xml',
8258 -charset => 'utf-8',
8259 -content_disposition => 'inline; filename="opml.xml"');
8260
Jürgen Kreileder5d791052011-12-17 10:22:22 +01008261 my $title = esc_html($site_name);
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008262 my $filter = " within subdirectory ";
8263 if (defined $project_filter) {
8264 $filter .= esc_html($project_filter);
8265 } else {
8266 $filter = "";
8267 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02008268 print <<XML;
8269<?xml version="1.0" encoding="utf-8"?>
8270<opml version="1.0">
8271<head>
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008272 <title>$title OPML Export$filter</title>
Jakub Narebski59b9f612006-08-22 23:42:53 +02008273</head>
8274<body>
8275<outline text="git RSS feeds">
8276XML
Jakub Narebski717b8312006-07-31 21:22:15 +02008277
8278 foreach my $pr (@list) {
8279 my %proj = %$pr;
Jakub Narebski847e01f2006-08-14 02:05:47 +02008280 my $head = git_get_head_hash($proj{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02008281 if (!defined $head) {
8282 next;
8283 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02008284 $git_dir = "$projectroot/$proj{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02008285 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02008286 if (!%co) {
8287 next;
8288 }
8289
8290 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
Giuseppe Bilottadf63fbb2009-01-02 13:15:28 +01008291 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
8292 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
Jakub Narebski717b8312006-07-31 21:22:15 +02008293 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
8294 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02008295 print <<XML;
8296</outline>
8297</body>
8298</opml>
8299XML
Jakub Narebski717b8312006-07-31 21:22:15 +02008300}