blob: 3d6a7053881ec7646dc28a2eba991a15ff80c634 [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) {
57 if ($my_url =~ s,\Q$path_info\E$,, &&
58 $my_uri =~ s,\Q$path_info\E$,, &&
59 defined $ENV{'SCRIPT_NAME'}) {
60 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
61 }
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +010062 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +020063
64 # target of the home link on top of all pages
65 our $home_link = $my_uri || "/";
Giuseppe Bilottab65910f2008-09-29 15:07:42 +020066}
67
Alp Tokere130dda2006-07-12 23:55:10 +010068# core git executable to use
69# this can just be "git" if your webserver has a sensible PATH
Junio C Hamano06c084d2006-08-02 13:50:20 -070070our $GIT = "++GIT_BINDIR++/git";
Jakub Narebski3f7f27102006-06-21 09:48:04 +020071
Kay Sieversb87d78d2005-08-07 20:21:04 +020072# absolute fs-path which will be prepended to the project path
Dennis Stosberg4a87b432006-06-21 15:07:08 +020073#our $projectroot = "/pub/scm";
Junio C Hamano06c084d2006-08-02 13:50:20 -070074our $projectroot = "++GITWEB_PROJECTROOT++";
Kay Sieversb87d78d2005-08-07 20:21:04 +020075
Luke Luca5e9492007-10-16 20:45:25 -070076# fs traversing limit for getting project list
77# the number is relative to the projectroot
78our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
79
Yasushi SHOJI2de21fa2006-08-15 07:50:49 +090080# string of the home link on top of all pages
81our $home_link_str = "++GITWEB_HOME_LINK_STR++";
82
Alp Toker49da1da2006-07-11 21:10:26 +010083# name of your site or organization to appear in page titles
84# replace this with something more descriptive for clearer bookmarks
Petr Baudis8be28902006-10-24 05:18:39 +020085our $site_name = "++GITWEB_SITENAME++"
86 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
Alp Toker49da1da2006-07-11 21:10:26 +010087
Lénaïc Huardc1355b72011-10-21 09:09:29 +020088# html snippet to include in the <head> section of each page
89our $site_html_head_string = "++GITWEB_SITE_HTML_HEAD_STRING++";
Alan Chandlerb2d34762006-10-03 13:49:03 +010090# filename of html text to include at top of each page
91our $site_header = "++GITWEB_SITE_HEADER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020092# html text to include at home page
Junio C Hamano06c084d2006-08-02 13:50:20 -070093our $home_text = "++GITWEB_HOMETEXT++";
Alan Chandlerb2d34762006-10-03 13:49:03 +010094# filename of html text to include at bottom of each page
95our $site_footer = "++GITWEB_SITE_FOOTER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020096
Alan Chandlerb2d34762006-10-03 13:49:03 +010097# URI of stylesheets
98our @stylesheets = ("++GITWEB_CSS++");
Petr Baudis887a6122006-10-26 14:41:25 +020099# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
100our $stylesheet = undef;
Jakub Narebski9a7a62f2006-10-06 12:31:05 +0200101# URI of GIT logo (72x27 size)
Junio C Hamano06c084d2006-08-02 13:50:20 -0700102our $logo = "++GITWEB_LOGO++";
Jakub Narebski0b5deba2006-09-04 20:32:13 +0200103# URI of GIT favicon, assumed to be image/png type
104our $favicon = "++GITWEB_FAVICON++";
Jakub Narebski4af819d2009-09-01 13:39:17 +0200105# URI of gitweb.js (JavaScript code for gitweb)
106our $javascript = "++GITWEB_JS++";
Jakub Narebskiaedd9422006-06-17 11:23:56 +0200107
Jakub Narebski9a7a62f2006-10-06 12:31:05 +0200108# URI and label (title) of GIT logo link
109#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
110#our $logo_label = "git documentation";
Wincent Colaiuta69fb8282009-07-12 14:31:28 +0200111our $logo_url = "http://git-scm.com/";
Jakub Narebski9a7a62f2006-10-06 12:31:05 +0200112our $logo_label = "git homepage";
Junio C Hamano51a7c662006-09-23 12:36:01 -0700113
Kay Sievers09bd7892005-08-07 20:21:23 +0200114# source of projects list
Junio C Hamano06c084d2006-08-02 13:50:20 -0700115our $projects_list = "++GITWEB_LIST++";
Kay Sieversb87d78d2005-08-07 20:21:04 +0200116
Michael Hendricks55feb122007-07-04 18:36:48 -0600117# the width (in characters) of the projects list "Description" column
118our $projects_list_description_width = 25;
119
Sebastien Ceveyd940c902011-04-29 19:52:01 +0200120# group projects by category on the projects list
121# (enabled if this variable evaluates to true)
122our $projects_list_group_categories = 0;
123
124# default category if none specified
125# (leave the empty string for no category)
126our $project_list_default_category = "";
127
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +0200128# default order of projects list
129# valid values are none, project, descr, owner, and age
130our $default_projects_order = "project";
131
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200132# show repository only if this file exists
133# (only effective if this variable evaluates to true)
134our $export_ok = "++GITWEB_EXPORT_OK++";
135
Kacper Kornet5710be42012-04-24 19:39:15 +0200136# don't generate age column on the projects list page
137our $omit_age_column = 0;
138
Kacper Kornet0ebe7822012-04-26 18:45:44 +0200139# don't generate information about owners of repositories
140our $omit_owner=0;
141
Alexander Gavrilovdd7f5f12008-11-06 01:36:23 +0300142# show repository only if this subroutine returns true
143# when given the path to the project, for example:
144# sub { return -e "$_[0]/git-daemon-export-ok"; }
145our $export_auth_hook = undef;
146
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200147# only allow viewing of repositories also shown on the overview page
148our $strict_export = "++GITWEB_STRICT_EXPORT++";
149
Jakub Narebski19a87212006-08-15 23:03:17 +0200150# list of git base URLs used for URL to where fetch project from,
151# i.e. full URL is "$git_base_url/$project"
Jakub Narebskid6b7e0b2006-10-26 12:26:44 +0200152our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
Jakub Narebski19a87212006-08-15 23:03:17 +0200153
Jakub Narebskif5aa79d2006-06-17 13:32:15 +0200154# default blob_plain mimetype and default charset for text/plain blob
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200155our $default_blob_plain_mimetype = 'text/plain';
156our $default_text_plain_charset = undef;
Jakub Narebskif5aa79d2006-06-17 13:32:15 +0200157
Petr Baudis2d007372006-06-18 00:01:06 +0200158# file to use for guessing MIME types before trying /etc/mime.types
159# (relative to the current git repository)
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200160our $mimetypes_file = undef;
Petr Baudis2d007372006-06-18 00:01:06 +0200161
Martin Koegler00f429a2007-06-03 17:42:44 +0200162# assume this charset if line contains non-UTF-8 characters;
163# it should be valid encoding (see Encoding::Supported(3pm) for list),
164# for which encoding all byte sequences are valid, for example
165# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
166# could be even 'utf-8' for the old behavior)
167our $fallback_encoding = 'latin1';
168
Jakub Narebski69a9b412007-07-20 02:15:09 +0200169# rename detection options for git-diff and git-diff-tree
170# - default is '-M', with the cost proportional to
171# (number of removed files) * (number of new files).
172# - more costly is '-C' (which implies '-M'), with the cost proportional to
173# (number of changed files + number of removed files) * (number of new files)
174# - even more costly is '-C', '--find-copies-harder' with cost
175# (number of files in the original tree) * (number of new files)
176# - one might want to include '-B' option, e.g. '-B', '-M'
177our @diff_opts = ('-M'); # taken from git_commit
178
Matt McCutchen7e1100e2009-02-07 19:00:09 -0500179# Disables features that would allow repository owners to inject script into
180# the gitweb domain.
181our $prevent_xss = 0;
182
Christopher Wilson7ce896b2010-09-21 00:25:19 -0700183# Path to the highlight executable to use (must be the one from
184# http://www.andre-simon.de due to assumptions about parameters and output).
185# Useful if highlight is not installed on your webserver's PATH.
186# [Default: highlight]
187our $highlight_bin = "++HIGHLIGHT_BIN++";
188
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200189# information about snapshot formats that gitweb is capable of serving
190our %known_snapshot_formats = (
191 # name => {
192 # 'display' => display name,
193 # 'type' => mime type,
194 # 'suffix' => filename suffix,
195 # 'format' => --format for git-archive,
196 # 'compressor' => [compressor command and arguments]
Mark A Rada1bfd3632009-08-06 10:25:39 -0400197 # (array reference, optional)
198 # 'disabled' => boolean (optional)}
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200199 #
200 'tgz' => {
201 'display' => 'tar.gz',
202 'type' => 'application/x-gzip',
203 'suffix' => '.tar.gz',
204 'format' => 'tar',
Fraser Tweedale0c8c3852011-04-26 11:32:00 +1000205 'compressor' => ['gzip', '-n']},
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200206
207 'tbz2' => {
208 'display' => 'tar.bz2',
209 'type' => 'application/x-bzip2',
210 'suffix' => '.tar.bz2',
211 'format' => 'tar',
212 'compressor' => ['bzip2']},
213
Mark A Radacbdefb52009-08-06 10:28:25 -0400214 'txz' => {
215 'display' => 'tar.xz',
216 'type' => 'application/x-xz',
217 'suffix' => '.tar.xz',
218 'format' => 'tar',
219 'compressor' => ['xz'],
220 'disabled' => 1},
221
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200222 'zip' => {
223 'display' => 'zip',
224 'type' => 'application/x-zip',
225 'suffix' => '.zip',
226 'format' => 'zip'},
227);
228
229# Aliases so we understand old gitweb.snapshot values in repository
230# configuration.
231our %known_snapshot_format_aliases = (
232 'gzip' => 'tgz',
233 'bzip2' => 'tbz2',
Mark A Radacbdefb52009-08-06 10:28:25 -0400234 'xz' => 'txz',
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200235
236 # backward compatibility: legacy gitweb config support
237 'x-gzip' => undef, 'gz' => undef,
238 'x-bzip2' => undef, 'bz2' => undef,
239 'x-zip' => undef, '' => undef,
240);
241
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200242# Pixel sizes for icons and avatars. If the default font sizes or lineheights
243# are changed, it may be appropriate to change these values too via
244# $GITWEB_CONFIG.
245our %avatar_size = (
246 'default' => 16,
247 'double' => 32
248);
249
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +0100250# Used to set the maximum load that we will still respond to gitweb queries.
251# If server load exceed this value then return "503 server busy" error.
252# If gitweb cannot determined server load, it is taken to be 0.
253# Leave it undefined (or set to 'undef') to turn off load checking.
254our $maxload = 300;
255
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400256# configuration for 'highlight' (http://www.andre-simon.de/)
257# match by basename
258our %highlight_basename = (
259 #'Program' => 'py',
260 #'Library' => 'py',
261 'SConstruct' => 'py', # SCons equivalent of Makefile
262 'Makefile' => 'make',
263);
264# match by extension
265our %highlight_ext = (
266 # main extensions, defining name of syntax;
267 # see files in /usr/share/highlight/langDefs/ directory
268 map { $_ => $_ }
Sylvain Rabot3ce19eb2010-12-30 22:20:28 +0100269 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 -0400270 # alternate extensions, see /etc/highlight/filetypes.conf
271 'h' => 'c',
Sylvain Rabot3ce19eb2010-12-30 22:20:28 +0100272 map { $_ => 'sh' } qw(bash zsh ksh),
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400273 map { $_ => 'cpp' } qw(cxx c++ cc),
Sylvain Rabot3ce19eb2010-12-30 22:20:28 +0100274 map { $_ => 'php' } qw(php3 php4 php5 phps),
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400275 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
Sylvain Rabot3ce19eb2010-12-30 22:20:28 +0100276 map { $_ => 'make'} qw(mak mk),
Alejandro R. Sedeño61bf1262010-07-28 14:40:53 -0400277 map { $_ => 'xml' } qw(xhtml html htm),
278);
279
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530280# You define site-wide feature defaults here; override them with
281# $GITWEB_CONFIG as necessary.
Jakub Narebski952c65f2006-08-22 16:52:50 +0200282our %feature = (
Jakub Narebski17848fc2006-08-26 19:14:22 +0200283 # feature => {
284 # 'sub' => feature-sub (subroutine),
285 # 'override' => allow-override (boolean),
286 # 'default' => [ default options...] (array reference)}
287 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200288 # if feature is overridable (it means that allow-override has true value),
Jakub Narebski17848fc2006-08-26 19:14:22 +0200289 # then feature-sub will be called with default options as parameters;
290 # return value of feature-sub indicates if to enable specified feature
291 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200292 # if there is no 'sub' key (no feature-sub), then feature cannot be
Ralf Wildenhues22e5e582010-08-22 13:12:12 +0200293 # overridden
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200294 #
Giuseppe Bilottaff3c0ff2008-12-02 14:57:28 -0800295 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
296 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
297 # is enabled
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530298
Petr Baudis45a3b122006-10-07 15:17:47 +0200299 # Enable the 'blame' blob view, showing the last commit that modified
300 # each line in the file. This can be very CPU-intensive.
301
302 # To enable system wide have in $GITWEB_CONFIG
303 # $feature{'blame'}{'default'} = [1];
304 # To have project specific config enable override in $GITWEB_CONFIG
305 # $feature{'blame'}{'override'} = 1;
306 # and in project config gitweb.blame = 0|1;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200307 'blame' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800308 'sub' => sub { feature_bool('blame', @_) },
Jakub Narebski952c65f2006-08-22 16:52:50 +0200309 'override' => 0,
310 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530311
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200312 # Enable the 'snapshot' link, providing a compressed archive of any
Petr Baudis45a3b122006-10-07 15:17:47 +0200313 # tree. This can potentially generate high traffic if you have large
314 # project.
315
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200316 # Value is a list of formats defined in %known_snapshot_formats that
317 # you wish to offer.
Petr Baudis45a3b122006-10-07 15:17:47 +0200318 # To disable system wide have in $GITWEB_CONFIG
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200319 # $feature{'snapshot'}{'default'} = [];
Petr Baudis45a3b122006-10-07 15:17:47 +0200320 # To have project specific config enable override in $GITWEB_CONFIG
Uwe Zeisbergerbbee1d92006-12-08 12:44:31 +0100321 # $feature{'snapshot'}{'override'} = 1;
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200322 # and in project config, a comma-separated list of formats or "none"
323 # to disable. Example: gitweb.snapshot = tbz2,zip;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200324 'snapshot' => {
325 'sub' => \&feature_snapshot,
326 'override' => 0,
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200327 'default' => ['tgz']},
Jakub Narebski04f7a942006-09-11 00:29:27 +0200328
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000329 # Enable text search, which will list the commits which match author,
330 # committer or commit text to a given string. Enabled by default.
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200331 # Project specific override is not supported.
Jakub Narebskie0ca3642011-06-22 17:28:52 +0200332 #
333 # Note that this controls all search features, which means that if
334 # it is disabled, then 'grep' and 'pickaxe' search would also be
335 # disabled.
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000336 'search' => {
337 'override' => 0,
338 'default' => [1]},
339
Petr Baudise7738552007-05-17 04:31:12 +0200340 # Enable grep search, which will list the files in currently selected
341 # tree containing the given string. Enabled by default. This can be
342 # potentially CPU-intensive, of course.
Jakub Narebskia598ded2011-06-21 08:41:16 +0200343 # Note that you need to have 'search' feature enabled too.
Petr Baudise7738552007-05-17 04:31:12 +0200344
345 # To enable system wide have in $GITWEB_CONFIG
346 # $feature{'grep'}{'default'} = [1];
347 # To have project specific config enable override in $GITWEB_CONFIG
348 # $feature{'grep'}{'override'} = 1;
349 # and in project config gitweb.grep = 0|1;
350 'grep' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800351 'sub' => sub { feature_bool('grep', @_) },
Petr Baudise7738552007-05-17 04:31:12 +0200352 'override' => 0,
353 'default' => [1]},
354
Petr Baudis45a3b122006-10-07 15:17:47 +0200355 # Enable the pickaxe search, which will list the commits that modified
356 # a given string in a file. This can be practical and quite faster
357 # alternative to 'blame', but still potentially CPU-intensive.
Jakub Narebskia598ded2011-06-21 08:41:16 +0200358 # Note that you need to have 'search' feature enabled too.
Petr Baudis45a3b122006-10-07 15:17:47 +0200359
360 # To enable system wide have in $GITWEB_CONFIG
361 # $feature{'pickaxe'}{'default'} = [1];
362 # To have project specific config enable override in $GITWEB_CONFIG
363 # $feature{'pickaxe'}{'override'} = 1;
364 # and in project config gitweb.pickaxe = 0|1;
Jakub Narebski04f7a942006-09-11 00:29:27 +0200365 'pickaxe' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800366 'sub' => sub { feature_bool('pickaxe', @_) },
Jakub Narebski04f7a942006-09-11 00:29:27 +0200367 'override' => 0,
368 'default' => [1]},
Martin Waitz9e756902006-10-01 23:57:48 +0200369
Jakub Narebskie4b48ea2009-09-07 14:40:00 +0200370 # Enable showing size of blobs in a 'tree' view, in a separate
371 # column, similar to what 'ls -l' does. This cost a bit of IO.
372
373 # To disable system wide have in $GITWEB_CONFIG
374 # $feature{'show-sizes'}{'default'} = [0];
375 # To have project specific config enable override in $GITWEB_CONFIG
376 # $feature{'show-sizes'}{'override'} = 1;
377 # and in project config gitweb.showsizes = 0|1;
378 'show-sizes' => {
379 'sub' => sub { feature_bool('showsizes', @_) },
380 'override' => 0,
381 'default' => [1]},
382
Petr Baudis45a3b122006-10-07 15:17:47 +0200383 # Make gitweb use an alternative format of the URLs which can be
384 # more readable and natural-looking: project name is embedded
385 # directly in the path and the query string contains other
386 # auxiliary information. All gitweb installations recognize
387 # URL in either format; this configures in which formats gitweb
388 # generates links.
389
390 # To enable system wide have in $GITWEB_CONFIG
391 # $feature{'pathinfo'}{'default'} = [1];
392 # Project specific override is not supported.
393
394 # Note that you will need to change the default location of CSS,
395 # favicon, logo and possibly other files to an absolute URL. Also,
396 # if gitweb.cgi serves as your indexfile, you will need to force
397 # $my_uri to contain the script name in your $GITWEB_CONFIG.
Martin Waitz9e756902006-10-01 23:57:48 +0200398 'pathinfo' => {
399 'override' => 0,
400 'default' => [0]},
Petr Baudise30496d2006-10-24 05:33:17 +0200401
402 # Make gitweb consider projects in project root subdirectories
403 # to be forks of existing projects. Given project $projname.git,
404 # projects matching $projname/*.git will not be shown in the main
405 # projects list, instead a '+' mark will be added to $projname
406 # there and a 'forks' view will be enabled for the project, listing
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +0200407 # all the forks. If project list is taken from a file, forks have
408 # to be listed after the main project.
Petr Baudise30496d2006-10-24 05:33:17 +0200409
410 # To enable system wide have in $GITWEB_CONFIG
411 # $feature{'forks'}{'default'} = [1];
412 # Project specific override is not supported.
413 'forks' => {
414 'override' => 0,
415 'default' => [0]},
Petr Baudisd627f682008-10-02 16:36:52 +0200416
417 # Insert custom links to the action bar of all project pages.
418 # This enables you mainly to link to third-party scripts integrating
419 # into gitweb; e.g. git-browser for graphical history representation
420 # or custom web-based repository administration interface.
421
422 # The 'default' value consists of a list of triplets in the form
423 # (label, link, position) where position is the label after which
Jakub Narebski2b11e052008-10-12 00:02:32 +0200424 # to insert the link and link is a format string where %n expands
Petr Baudisd627f682008-10-02 16:36:52 +0200425 # to the project name, %f to the project path within the filesystem,
426 # %h to the current hash (h gitweb parameter) and %b to the current
Jakub Narebski2b11e052008-10-12 00:02:32 +0200427 # hash base (hb gitweb parameter); %% expands to %.
Petr Baudisd627f682008-10-02 16:36:52 +0200428
429 # To enable system wide have in $GITWEB_CONFIG e.g.
430 # $feature{'actions'}{'default'} = [('graphiclog',
431 # '/git-browser/by-commit.html?r=%n', 'summary')];
432 # Project specific override is not supported.
433 'actions' => {
434 'override' => 0,
435 'default' => []},
Shawn O. Pearce3e3d4ee2008-10-03 07:41:25 -0700436
Jakub Narebski0368c492011-04-29 19:51:57 +0200437 # Allow gitweb scan project content tags of project repository,
438 # and display the popular Web 2.0-ish "tag cloud" near the projects
439 # list. Note that this is something COMPLETELY different from the
440 # normal Git tags.
Petr Baudisaed93de2008-10-02 17:13:02 +0200441
442 # gitweb by itself can show existing tags, but it does not handle
Jakub Narebski0368c492011-04-29 19:51:57 +0200443 # tagging itself; you need to do it externally, outside gitweb.
444 # The format is described in git_get_project_ctags() subroutine.
Petr Baudisaed93de2008-10-02 17:13:02 +0200445 # You may want to install the HTML::TagCloud Perl module to get
446 # a pretty tag cloud instead of just a list of tags.
447
448 # To enable system wide have in $GITWEB_CONFIG
Jakub Narebski0368c492011-04-29 19:51:57 +0200449 # $feature{'ctags'}{'default'} = [1];
Petr Baudisaed93de2008-10-02 17:13:02 +0200450 # Project specific override is not supported.
Jakub Narebski0368c492011-04-29 19:51:57 +0200451
452 # In the future whether ctags editing is enabled might depend
453 # on the value, but using 1 should always mean no editing of ctags.
Petr Baudisaed93de2008-10-02 17:13:02 +0200454 'ctags' => {
455 'override' => 0,
456 'default' => [0]},
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100457
458 # The maximum number of patches in a patchset generated in patch
459 # view. Set this to 0 or undef to disable patch view, or to a
460 # negative number to remove any limit.
461
462 # To disable system wide have in $GITWEB_CONFIG
463 # $feature{'patches'}{'default'} = [0];
464 # To have project specific config enable override in $GITWEB_CONFIG
465 # $feature{'patches'}{'override'} = 1;
466 # and in project config gitweb.patches = 0|n;
467 # where n is the maximum number of patches allowed in a patchset.
468 'patches' => {
469 'sub' => \&feature_patches,
470 'override' => 0,
471 'default' => [16]},
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200472
473 # Avatar support. When this feature is enabled, views such as
474 # shortlog or commit will display an avatar associated with
475 # the email of the committer(s) and/or author(s).
476
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +0200477 # Currently available providers are gravatar and picon.
478 # If an unknown provider is specified, the feature is disabled.
479
480 # Gravatar depends on Digest::MD5.
481 # Picon currently relies on the indiana.edu database.
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200482
483 # To enable system wide have in $GITWEB_CONFIG
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +0200484 # $feature{'avatar'}{'default'} = ['<provider>'];
485 # where <provider> is either gravatar or picon.
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200486 # To have project specific config enable override in $GITWEB_CONFIG
487 # $feature{'avatar'}{'override'} = 1;
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +0200488 # and in project config gitweb.avatar = <provider>;
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200489 'avatar' => {
490 'sub' => \&feature_avatar,
491 'override' => 0,
492 'default' => ['']},
Jakub Narebskiaa7dd052009-09-01 13:39:16 +0200493
494 # Enable displaying how much time and how many git commands
495 # it took to generate and display page. Disabled by default.
496 # Project specific override is not supported.
497 'timed' => {
498 'override' => 0,
499 'default' => [0]},
Jakub Narebskie627e502009-11-26 21:12:15 +0100500
501 # Enable turning some links into links to actions which require
502 # JavaScript to run (like 'blame_incremental'). Not enabled by
503 # default. Project specific override is currently not supported.
504 'javascript-actions' => {
505 'override' => 0,
506 'default' => [0]},
Johannes Schindelinb331fe52010-04-27 21:34:44 +0200507
Jakub Narebski2e987f92011-04-28 21:04:11 +0200508 # Enable and configure ability to change common timezone for dates
509 # in gitweb output via JavaScript. Enabled by default.
510 # Project specific override is not supported.
511 'javascript-timezone' => {
512 'override' => 0,
513 'default' => [
514 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
515 # or undef to turn off this feature
516 'gitweb_tz', # name of cookie where to store selected timezone
517 'datetime', # CSS class used to mark up dates for manipulation
518 ]},
519
Johannes Schindelinb331fe52010-04-27 21:34:44 +0200520 # Syntax highlighting support. This is based on Daniel Svensson's
521 # and Sham Chukoury's work in gitweb-xmms2.git.
Jakub Narebski592ea412010-04-27 21:34:45 +0200522 # It requires the 'highlight' program present in $PATH,
523 # and therefore is disabled by default.
Johannes Schindelinb331fe52010-04-27 21:34:44 +0200524
525 # To enable system wide have in $GITWEB_CONFIG
526 # $feature{'highlight'}{'default'} = [1];
527
528 'highlight' => {
529 'sub' => sub { feature_bool('highlight', @_) },
530 'override' => 0,
531 'default' => [0]},
Giuseppe Bilotta60efa242010-11-11 13:26:09 +0100532
533 # Enable displaying of remote heads in the heads list
534
535 # To enable system wide have in $GITWEB_CONFIG
536 # $feature{'remote_heads'}{'default'} = [1];
537 # To have project specific config enable override in $GITWEB_CONFIG
538 # $feature{'remote_heads'}{'override'} = 1;
539 # and in project config gitweb.remote_heads = 0|1;
540 'remote_heads' => {
541 'sub' => sub { feature_bool('remote_heads', @_) },
542 'override' => 0,
543 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530544);
545
Junio C Hamanoa7c5a282008-11-29 13:02:08 -0800546sub gitweb_get_feature {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530547 my ($name) = @_;
Jakub Narebskidd1ad5f2006-09-26 01:56:17 +0200548 return unless exists $feature{$name};
Jakub Narebski952c65f2006-08-22 16:52:50 +0200549 my ($sub, $override, @defaults) = (
550 $feature{$name}{'sub'},
551 $feature{$name}{'override'},
552 @{$feature{$name}{'default'}});
Jakub Narebski9be36142010-03-01 22:51:34 +0100553 # project specific override is possible only if we have project
554 our $git_dir; # global variable, declared later
555 if (!$override || !defined $git_dir) {
556 return @defaults;
557 }
Martin Waitza9455912006-10-03 20:07:43 +0200558 if (!defined $sub) {
Nanako Shiraishi93197892009-08-28 12:18:49 +0900559 warn "feature $name is not overridable";
Martin Waitza9455912006-10-03 20:07:43 +0200560 return @defaults;
561 }
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530562 return $sub->(@defaults);
563}
564
Giuseppe Bilotta25b27902008-11-29 13:07:29 -0800565# A wrapper to check if a given feature is enabled.
566# With this, you can say
567#
568# my $bool_feat = gitweb_check_feature('bool_feat');
569# gitweb_check_feature('bool_feat') or somecode;
570#
571# instead of
572#
573# my ($bool_feat) = gitweb_get_feature('bool_feat');
574# (gitweb_get_feature('bool_feat'))[0] or somecode;
575#
576sub gitweb_check_feature {
577 return (gitweb_get_feature(@_))[0];
578}
579
580
Matt Kraaicdad8172008-12-15 22:16:19 -0800581sub feature_bool {
582 my $key = shift;
583 my ($val) = git_get_project_config($key, '--bool');
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530584
Marcel M. Carydf5d10a2009-02-18 14:09:41 +0100585 if (!defined $val) {
586 return ($_[0]);
587 } elsif ($val eq 'true') {
Matt Kraaicdad8172008-12-15 22:16:19 -0800588 return (1);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530589 } elsif ($val eq 'false') {
Matt Kraaicdad8172008-12-15 22:16:19 -0800590 return (0);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530591 }
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530592}
593
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530594sub feature_snapshot {
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200595 my (@fmts) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530596
597 my ($val) = git_get_project_config('snapshot');
598
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200599 if ($val) {
600 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530601 }
602
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200603 return @fmts;
Luben Tuikovde9272f2006-09-28 16:49:21 -0700604}
605
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100606sub feature_patches {
607 my @val = (git_get_project_config('patches', '--int'));
608
609 if (@val) {
610 return @val;
611 }
612
613 return ($_[0]);
614}
615
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +0200616sub feature_avatar {
617 my @val = (git_get_project_config('avatar'));
618
619 return @val ? @val : @_;
620}
621
Junio C Hamano2172ce42006-10-03 02:30:47 -0700622# checking HEAD file with -e is fragile if the repository was
623# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
624# and then pruned.
625sub check_head_link {
626 my ($dir) = @_;
627 my $headfile = "$dir/HEAD";
628 return ((-e $headfile) ||
629 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
630}
631
632sub check_export_ok {
633 my ($dir) = @_;
634 return (check_head_link($dir) &&
Alexander Gavrilovdd7f5f12008-11-06 01:36:23 +0300635 (!$export_ok || -e "$dir/$export_ok") &&
636 (!$export_auth_hook || $export_auth_hook->($dir)));
Junio C Hamano2172ce42006-10-03 02:30:47 -0700637}
638
Jakub Narebskia7817852007-07-22 23:41:20 +0200639# process alternate names for backward compatibility
640# filter out unsupported (unknown) snapshot formats
641sub filter_snapshot_fmts {
642 my @fmts = @_;
643
644 @fmts = map {
645 exists $known_snapshot_format_aliases{$_} ?
646 $known_snapshot_format_aliases{$_} : $_} @fmts;
Jakub Narebski68cedb12009-05-10 02:40:37 +0200647 @fmts = grep {
Mark A Rada1bfd3632009-08-06 10:25:39 -0400648 exists $known_snapshot_formats{$_} &&
649 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
Jakub Narebskia7817852007-07-22 23:41:20 +0200650}
651
Jakub Narebskida4b2432010-11-25 19:43:59 +0100652# If it is set to code reference, it is code that it is to be run once per
653# request, allowing updating configurations that change with each request,
654# while running other code in config file only once.
655#
656# Otherwise, if it is false then gitweb would process config file only once;
657# if it is true then gitweb config would be run for each request.
658our $per_request_config = 1;
659
Jakub Narebskif612a712011-05-25 18:35:26 +0200660# read and parse gitweb config file given by its parameter.
661# returns true on success, false on recoverable error, allowing
662# to chain this subroutine, using first file that exists.
663# dies on errors during parsing config file, as it is unrecoverable.
664sub read_config_file {
665 my $filename = shift;
666 return unless defined $filename;
667 # die if there are errors parsing config file
668 if (-e $filename) {
669 do $filename;
670 die $@ if $@;
671 return 1;
672 }
673 return;
674}
675
Jakub Narebski131d6af2011-07-25 00:29:18 +0200676our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200677sub evaluate_gitweb_config {
678 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
679 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
Jakub Narebski131d6af2011-07-25 00:29:18 +0200680 our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "++GITWEB_CONFIG_COMMON++";
Jakub Narebskif612a712011-05-25 18:35:26 +0200681
Jakub Narebski131d6af2011-07-25 00:29:18 +0200682 # Protect agains duplications of file names, to not read config twice.
683 # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
684 # there possibility of duplication of filename there doesn't matter.
685 $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
686 $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
687
688 # Common system-wide settings for convenience.
689 # Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
690 read_config_file($GITWEB_CONFIG_COMMON);
691
692 # Use first config file that exists. This means use the per-instance
693 # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
694 read_config_file($GITWEB_CONFIG) and return;
Jakub Narebskif612a712011-05-25 18:35:26 +0200695 read_config_file($GITWEB_CONFIG_SYSTEM);
Gerrit Pape17a8b252008-03-26 18:11:19 +0000696}
Jeff Kingc8d138a2006-08-02 15:23:34 -0400697
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +0100698# Get loadavg of system, to compare against $maxload.
699# Currently it requires '/proc/loadavg' present to get loadavg;
700# if it is not present it returns 0, which means no load checking.
701sub get_loadavg {
702 if( -e '/proc/loadavg' ){
703 open my $fd, '<', '/proc/loadavg'
704 or return 0;
705 my @load = split(/\s+/, scalar <$fd>);
706 close $fd;
707
708 # The first three columns measure CPU and IO utilization of the last one,
709 # five, and 10 minute periods. The fourth column shows the number of
710 # currently running processes and the total number of processes in the m/n
711 # format. The last column displays the last process ID used.
712 return $load[0] || 0;
713 }
714 # additional checks for load average should go here for things that don't export
715 # /proc/loadavg
716
717 return 0;
718}
719
Jeff Kingc8d138a2006-08-02 15:23:34 -0400720# version of the core git binary
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200721our $git_version;
722sub evaluate_git_version {
723 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
724 $number_of_git_cmds++;
725}
Jeff Kingc8d138a2006-08-02 15:23:34 -0400726
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200727sub check_loadavg {
728 if (defined $maxload && get_loadavg() > $maxload) {
729 die_error(503, "The load average on the server is too high");
730 }
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +0100731}
732
Jakub Narebski154b4d72006-08-05 12:55:20 +0200733# ======================================================================
Kay Sievers09bd7892005-08-07 20:21:23 +0200734# input validation and dispatch
Kay Sieversb87d78d2005-08-07 20:21:04 +0200735
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200736# input parameters can be collected from a variety of sources (presently, CGI
737# and PATH_INFO), so we define an %input_params hash that collects them all
738# together during validation: this allows subsequent uses (e.g. href()) to be
739# agnostic of the parameter origin
Kay Sievers6191f8e2005-08-07 20:19:56 +0200740
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300741our %input_params = ();
Martin Waitz5c95fab2006-08-17 00:28:38 +0200742
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200743# input parameters are stored with the long parameter name as key. This will
744# also be used in the href subroutine to convert parameters to their CGI
745# equivalent, and since the href() usage is the most frequent one, we store
746# the name -> CGI key mapping here, instead of the reverse.
747#
748# XXX: Warning: If you touch this, check the search form for updating,
749# too.
Jakub Narebski24d06932006-09-26 01:57:02 +0200750
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300751our @cgi_param_mapping = (
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200752 project => "p",
753 action => "a",
754 file_name => "f",
755 file_parent => "fp",
756 hash => "h",
757 hash_parent => "hp",
758 hash_base => "hb",
759 hash_parent_base => "hpb",
760 page => "pg",
761 order => "o",
762 searchtext => "s",
763 searchtype => "st",
764 snapshot_format => "sf",
765 extra_options => "opt",
766 search_use_regexp => "sr",
Jakub Narebski0368c492011-04-29 19:51:57 +0200767 ctag => "by_tag",
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +0100768 diff_style => "ds",
Bernhard R. Link19d2d232012-01-30 21:07:37 +0100769 project_filter => "pf",
Jakub Narebskic4ccf612009-09-01 13:39:19 +0200770 # this must be last entry (for manipulation from JavaScript)
771 javascript => "js"
Miklos Vajna868bc062007-07-12 20:39:27 +0200772);
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300773our %cgi_param_mapping = @cgi_param_mapping;
Miklos Vajna868bc062007-07-12 20:39:27 +0200774
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200775# we will also need to know the possible actions, for validation
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300776our %actions = (
Rafael Garcia-Suarez3a5b9192008-06-06 09:53:32 +0200777 "blame" => \&git_blame,
Jakub Narebski4af819d2009-09-01 13:39:17 +0200778 "blame_incremental" => \&git_blame_incremental,
779 "blame_data" => \&git_blame_data,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200780 "blobdiff" => \&git_blobdiff,
781 "blobdiff_plain" => \&git_blobdiff_plain,
782 "blob" => \&git_blob,
783 "blob_plain" => \&git_blob_plain,
784 "commitdiff" => \&git_commitdiff,
785 "commitdiff_plain" => \&git_commitdiff_plain,
786 "commit" => \&git_commit,
Petr Baudise30496d2006-10-24 05:33:17 +0200787 "forks" => \&git_forks,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200788 "heads" => \&git_heads,
789 "history" => \&git_history,
790 "log" => \&git_log,
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100791 "patch" => \&git_patch,
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +0100792 "patches" => \&git_patches,
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +0100793 "remotes" => \&git_remotes,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200794 "rss" => \&git_rss,
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +0100795 "atom" => \&git_atom,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200796 "search" => \&git_search,
Petr Baudis88ad7292006-10-24 05:15:46 +0200797 "search_help" => \&git_search_help,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200798 "shortlog" => \&git_shortlog,
799 "summary" => \&git_summary,
800 "tag" => \&git_tag,
801 "tags" => \&git_tags,
802 "tree" => \&git_tree,
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +0530803 "snapshot" => \&git_snapshot,
Jakub Narebskica946012006-12-10 13:25:47 +0100804 "object" => \&git_object,
Jakub Narebski77a153f2006-08-22 16:59:20 +0200805 # those below don't need $project
806 "opml" => \&git_opml,
807 "project_list" => \&git_project_list,
Jakub Narebskifc2b2be2006-09-15 04:56:03 +0200808 "project_index" => \&git_project_index,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200809);
810
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200811# finally, we have the hash of allowed extra_options for the commands that
812# allow them
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300813our %allowed_options = (
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200814 "--no-merges" => [ qw(rss atom log shortlog history) ],
815);
816
817# fill %input_params with the CGI parameters. All values except for 'opt'
818# should be single values, but opt can be an array. We should probably
819# build an array of parameters that can be multi-valued, but since for the time
820# being it's only this one, we just single it out
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200821sub evaluate_query_params {
822 our $cgi;
823
824 while (my ($name, $symbol) = each %cgi_param_mapping) {
825 if ($symbol eq 'opt') {
Jakub Narebski84d9e2d2012-02-03 13:44:54 +0100826 $input_params{$name} = [ map { decode_utf8($_) } $cgi->param($symbol) ];
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200827 } else {
Jakub Narebski84d9e2d2012-02-03 13:44:54 +0100828 $input_params{$name} = decode_utf8($cgi->param($symbol));
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200829 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200830 }
831}
832
833# now read PATH_INFO and update the parameter list for missing parameters
834sub evaluate_path_info {
835 return if defined $input_params{'project'};
836 return if !$path_info;
837 $path_info =~ s,^/+,,;
838 return if !$path_info;
839
840 # find which part of PATH_INFO is project
841 my $project = $path_info;
842 $project =~ s,/+$,,;
843 while ($project && !check_head_link("$projectroot/$project")) {
844 $project =~ s,/*[^/]*$,,;
845 }
846 return unless $project;
847 $input_params{'project'} = $project;
848
849 # do not change any parameters if an action is given using the query string
850 return if $input_params{'action'};
851 $path_info =~ s,^\Q$project\E/*,,;
852
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200853 # next, check if we have an action
854 my $action = $path_info;
855 $action =~ s,/.*$,,;
856 if (exists $actions{$action}) {
857 $path_info =~ s,^$action/*,,;
858 $input_params{'action'} = $action;
859 }
860
861 # list of actions that want hash_base instead of hash, but can have no
862 # pathname (f) parameter
863 my @wants_base = (
864 'tree',
865 'history',
866 );
867
Jakub Narebski7e00dc52010-10-13 13:33:48 +0200868 # we want to catch, among others
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200869 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
870 my ($parentrefname, $parentpathname, $refname, $pathname) =
Jakub Narebski7e00dc52010-10-13 13:33:48 +0200871 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200872
873 # first, analyze the 'current' part
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200874 if (defined $pathname) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200875 # we got "branch:filename" or "branch:dir/"
876 # we could use git_get_type(branch:pathname), but:
877 # - it needs $git_dir
878 # - it does a git() call
879 # - the convention of terminating directories with a slash
880 # makes it superfluous
881 # - embedding the action in the PATH_INFO would make it even
882 # more superfluous
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200883 $pathname =~ s,^/+,,;
884 if (!$pathname || substr($pathname, -1) eq "/") {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200885 $input_params{'action'} ||= "tree";
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200886 $pathname =~ s,/$,,;
887 } else {
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200888 # the default action depends on whether we had parent info
889 # or not
890 if ($parentrefname) {
891 $input_params{'action'} ||= "blobdiff_plain";
892 } else {
893 $input_params{'action'} ||= "blob_plain";
894 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200895 }
896 $input_params{'hash_base'} ||= $refname;
897 $input_params{'file_name'} ||= $pathname;
898 } elsif (defined $refname) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200899 # we got "branch". In this case we have to choose if we have to
900 # set hash or hash_base.
901 #
902 # Most of the actions without a pathname only want hash to be
903 # set, except for the ones specified in @wants_base that want
904 # hash_base instead. It should also be noted that hand-crafted
905 # links having 'history' as an action and no pathname or hash
906 # set will fail, but that happens regardless of PATH_INFO.
Jakub Narebskid0af3732010-10-13 13:35:20 +0200907 if (defined $parentrefname) {
908 # if there is parent let the default be 'shortlog' action
909 # (for http://git.example.com/repo.git/A..B links); if there
910 # is no parent, dispatch will detect type of object and set
911 # action appropriately if required (if action is not set)
912 $input_params{'action'} ||= "shortlog";
913 }
914 if ($input_params{'action'} &&
915 grep { $_ eq $input_params{'action'} } @wants_base) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200916 $input_params{'hash_base'} ||= $refname;
917 } else {
918 $input_params{'hash'} ||= $refname;
919 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200920 }
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200921
922 # next, handle the 'parent' part, if present
923 if (defined $parentrefname) {
924 # a missing pathspec defaults to the 'current' filename, allowing e.g.
925 # someproject/blobdiff/oldrev..newrev:/filename
926 if ($parentpathname) {
927 $parentpathname =~ s,^/+,,;
928 $parentpathname =~ s,/$,,;
929 $input_params{'file_parent'} ||= $parentpathname;
930 } else {
931 $input_params{'file_parent'} ||= $input_params{'file_name'};
932 }
933 # we assume that hash_parent_base is wanted if a path was specified,
934 # or if the action wants hash_base instead of hash
935 if (defined $input_params{'file_parent'} ||
936 grep { $_ eq $input_params{'action'} } @wants_base) {
937 $input_params{'hash_parent_base'} ||= $parentrefname;
938 } else {
939 $input_params{'hash_parent'} ||= $parentrefname;
940 }
941 }
Giuseppe Bilotta1ec2fb52008-11-02 10:21:38 +0100942
943 # for the snapshot action, we allow URLs in the form
944 # $project/snapshot/$hash.ext
945 # where .ext determines the snapshot and gets removed from the
946 # passed $refname to provide the $hash.
947 #
948 # To be able to tell that $refname includes the format extension, we
949 # require the following two conditions to be satisfied:
950 # - the hash input parameter MUST have been set from the $refname part
951 # of the URL (i.e. they must be equal)
952 # - the snapshot format MUST NOT have been defined already (e.g. from
953 # CGI parameter sf)
954 # It's also useless to try any matching unless $refname has a dot,
955 # so we check for that too
956 if (defined $input_params{'action'} &&
957 $input_params{'action'} eq 'snapshot' &&
958 defined $refname && index($refname, '.') != -1 &&
959 $refname eq $input_params{'hash'} &&
960 !defined $input_params{'snapshot_format'}) {
961 # We loop over the known snapshot formats, checking for
962 # extensions. Allowed extensions are both the defined suffix
963 # (which includes the initial dot already) and the snapshot
964 # format key itself, with a prepended dot
Holger Weißccb4b532009-03-31 18:16:36 +0200965 while (my ($fmt, $opt) = each %known_snapshot_formats) {
Giuseppe Bilotta1ec2fb52008-11-02 10:21:38 +0100966 my $hash = $refname;
Jakub Narebski095e9142009-05-11 19:42:47 +0200967 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
968 next;
969 }
970 my $sfx = $1;
Giuseppe Bilotta1ec2fb52008-11-02 10:21:38 +0100971 # a valid suffix was found, so set the snapshot format
972 # and reset the hash parameter
973 $input_params{'snapshot_format'} = $fmt;
974 $input_params{'hash'} = $hash;
975 # we also set the format suffix to the one requested
976 # in the URL: this way a request for e.g. .tgz returns
977 # a .tgz instead of a .tar.gz
978 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
979 last;
980 }
981 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200982}
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200983
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200984our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
985 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
Bernhard R. Link19d2d232012-01-30 21:07:37 +0100986 $searchtext, $search_regexp, $project_filter);
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200987sub evaluate_and_validate_params {
988 our $action = $input_params{'action'};
989 if (defined $action) {
990 if (!validate_action($action)) {
991 die_error(400, "Invalid action parameter");
992 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200993 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200994
Jakub Narebskic2394fe2010-05-07 14:54:04 +0200995 # parameters which are pathnames
996 our $project = $input_params{'project'};
997 if (defined $project) {
998 if (!validate_project($project)) {
999 undef $project;
1000 die_error(404, "No such project");
1001 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001002 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001003
Bernhard R. Link19d2d232012-01-30 21:07:37 +01001004 our $project_filter = $input_params{'project_filter'};
1005 if (defined $project_filter) {
1006 if (!validate_pathname($project_filter)) {
1007 die_error(404, "Invalid project_filter parameter");
1008 }
1009 }
1010
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001011 our $file_name = $input_params{'file_name'};
1012 if (defined $file_name) {
1013 if (!validate_pathname($file_name)) {
1014 die_error(400, "Invalid file parameter");
1015 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001016 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001017
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001018 our $file_parent = $input_params{'file_parent'};
1019 if (defined $file_parent) {
1020 if (!validate_pathname($file_parent)) {
1021 die_error(400, "Invalid file parent parameter");
1022 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001023 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001024
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001025 # parameters which are refnames
1026 our $hash = $input_params{'hash'};
1027 if (defined $hash) {
1028 if (!validate_refname($hash)) {
1029 die_error(400, "Invalid hash parameter");
1030 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001031 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001032
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001033 our $hash_parent = $input_params{'hash_parent'};
1034 if (defined $hash_parent) {
1035 if (!validate_refname($hash_parent)) {
1036 die_error(400, "Invalid hash parent parameter");
1037 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001038 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001039
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001040 our $hash_base = $input_params{'hash_base'};
1041 if (defined $hash_base) {
1042 if (!validate_refname($hash_base)) {
1043 die_error(400, "Invalid hash base parameter");
1044 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001045 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001046
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001047 our @extra_options = @{$input_params{'extra_options'}};
1048 # @extra_options is always defined, since it can only be (currently) set from
1049 # CGI, and $cgi->param() returns the empty array in array context if the param
1050 # is not set
1051 foreach my $opt (@extra_options) {
1052 if (not exists $allowed_options{$opt}) {
1053 die_error(400, "Invalid option parameter");
1054 }
1055 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1056 die_error(400, "Invalid option parameter for this action");
1057 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001058 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001059
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001060 our $hash_parent_base = $input_params{'hash_parent_base'};
1061 if (defined $hash_parent_base) {
1062 if (!validate_refname($hash_parent_base)) {
1063 die_error(400, "Invalid hash parent base parameter");
1064 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001065 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001066
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001067 # other parameters
1068 our $page = $input_params{'page'};
1069 if (defined $page) {
1070 if ($page =~ m/[^0-9]/) {
1071 die_error(400, "Invalid page parameter");
1072 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001073 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001074
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001075 our $searchtype = $input_params{'searchtype'};
1076 if (defined $searchtype) {
1077 if ($searchtype =~ m/[^a-z]/) {
1078 die_error(400, "Invalid searchtype parameter");
1079 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001080 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001081
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001082 our $search_use_regexp = $input_params{'search_use_regexp'};
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001083
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001084 our $searchtext = $input_params{'searchtext'};
1085 our $search_regexp;
1086 if (defined $searchtext) {
1087 if (length($searchtext) < 2) {
1088 die_error(403, "At least two characters are required for search parameter");
1089 }
Jakub Narebski36612e42012-02-28 19:41:47 +01001090 if ($search_use_regexp) {
1091 $search_regexp = $searchtext;
1092 if (!eval { qr/$search_regexp/; 1; }) {
1093 (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1094 die_error(400, "Invalid search regexp '$search_regexp'",
1095 esc_html($error));
1096 }
1097 } else {
1098 $search_regexp = quotemeta $searchtext;
1099 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001100 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001101}
1102
1103# path to the current git repository
1104our $git_dir;
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001105sub evaluate_git_dir {
1106 our $git_dir = "$projectroot/$project" if $project;
1107}
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001108
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001109our (@snapshot_fmts, $git_avatar);
1110sub configure_gitweb_features {
1111 # list of supported snapshot formats
1112 our @snapshot_fmts = gitweb_get_feature('snapshot');
1113 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01001114
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001115 # check that the avatar feature is set to a known provider name,
1116 # and for each provider check if the dependencies are satisfied.
1117 # if the provider name is invalid or the dependencies are not met,
1118 # reset $git_avatar to the empty string.
1119 our ($git_avatar) = gitweb_get_feature('avatar');
1120 if ($git_avatar eq 'gravatar') {
1121 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1122 } elsif ($git_avatar eq 'picon') {
1123 # no dependencies
1124 } else {
1125 $git_avatar = '';
1126 }
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02001127}
1128
Jakub Narebski7a597452010-04-24 16:00:04 +02001129# custom error handler: 'die <message>' is Internal Server Error
1130sub handle_errors_html {
1131 my $msg = shift; # it is already HTML escaped
1132
1133 # to avoid infinite loop where error occurs in die_error,
1134 # change handler to default handler, disabling handle_errors_html
1135 set_message("Error occured when inside die_error:\n$msg");
1136
1137 # you cannot jump out of die_error when called as error handler;
1138 # the subroutine set via CGI::Carp::set_message is called _after_
1139 # HTTP headers are already written, so it cannot write them itself
1140 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1141}
1142set_message(\&handle_errors_html);
1143
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001144# dispatch
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001145sub dispatch {
1146 if (!defined $action) {
1147 if (defined $hash) {
1148 $action = git_get_type($hash);
Jakub Narebski18ab83e2012-01-07 11:47:38 +01001149 $action or die_error(404, "Object does not exist");
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001150 } elsif (defined $hash_base && defined $file_name) {
1151 $action = git_get_type("$hash_base:$file_name");
Jakub Narebski18ab83e2012-01-07 11:47:38 +01001152 $action or die_error(404, "File or directory does not exist");
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001153 } elsif (defined $project) {
1154 $action = 'summary';
1155 } else {
1156 $action = 'project_list';
1157 }
Gerrit Pape7f9778b2007-05-10 07:32:07 +00001158 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001159 if (!defined($actions{$action})) {
1160 die_error(400, "Unknown action");
1161 }
1162 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1163 !$project) {
1164 die_error(400, "Project needed");
1165 }
1166 $actions{$action}->();
Jakub Narebski77a153f2006-08-22 16:59:20 +02001167}
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001168
Jakub Narebski869d5882010-07-05 20:52:43 +02001169sub reset_timer {
Jakub Narebski3962f1d72010-11-09 19:27:54 +01001170 our $t0 = [ gettimeofday() ]
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001171 if defined $t0;
Jakub Narebski869d5882010-07-05 20:52:43 +02001172 our $number_of_git_cmds = 0;
1173}
1174
Jakub Narebskida4b2432010-11-25 19:43:59 +01001175our $first_request = 1;
Jakub Narebski869d5882010-07-05 20:52:43 +02001176sub run_request {
1177 reset_timer();
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001178
1179 evaluate_uri();
Jakub Narebskida4b2432010-11-25 19:43:59 +01001180 if ($first_request) {
1181 evaluate_gitweb_config();
1182 evaluate_git_version();
1183 }
1184 if ($per_request_config) {
1185 if (ref($per_request_config) eq 'CODE') {
1186 $per_request_config->();
1187 } elsif (!$first_request) {
1188 evaluate_gitweb_config();
1189 }
1190 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001191 check_loadavg();
1192
Jonathan Nieder7f425db2010-07-30 22:01:59 -05001193 # $projectroot and $projects_list might be set in gitweb config file
1194 $projects_list ||= $projectroot;
1195
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001196 evaluate_query_params();
1197 evaluate_path_info();
1198 evaluate_and_validate_params();
1199 evaluate_git_dir();
1200
1201 configure_gitweb_features();
1202
1203 dispatch();
Kay Sievers09bd7892005-08-07 20:21:23 +02001204}
Sam Vilaina0446e72010-05-07 14:54:05 +02001205
1206our $is_last_request = sub { 1 };
1207our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1208our $CGI = 'CGI';
1209our $cgi;
Jakub Narebski45aa9892010-06-05 23:11:18 +02001210sub configure_as_fcgi {
1211 require CGI::Fast;
1212 our $CGI = 'CGI::Fast';
1213
1214 my $request_number = 0;
1215 # let each child service 100 requests
1216 our $is_last_request = sub { ++$request_number > 100 };
Jakub Narebskid04d3d42006-09-19 21:53:22 +02001217}
Sam Vilaina0446e72010-05-07 14:54:05 +02001218sub evaluate_argv {
Jakub Narebski45aa9892010-06-05 23:11:18 +02001219 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1220 configure_as_fcgi()
1221 if $script_name =~ /\.fcgi$/;
1222
Sam Vilaina0446e72010-05-07 14:54:05 +02001223 return unless (@ARGV);
1224
1225 require Getopt::Long;
1226 Getopt::Long::GetOptions(
Jakub Narebski45aa9892010-06-05 23:11:18 +02001227 'fastcgi|fcgi|f' => \&configure_as_fcgi,
Sam Vilaina0446e72010-05-07 14:54:05 +02001228 'nproc|n=i' => sub {
1229 my ($arg, $val) = @_;
1230 return unless eval { require FCGI::ProcManager; 1; };
1231 my $proc_manager = FCGI::ProcManager->new({
1232 n_processes => $val,
1233 });
1234 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1235 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1236 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1237 },
1238 );
1239}
1240
1241sub run {
1242 evaluate_argv();
Jakub Narebski869d5882010-07-05 20:52:43 +02001243
Jakub Narebskida4b2432010-11-25 19:43:59 +01001244 $first_request = 1;
Sam Vilaina0446e72010-05-07 14:54:05 +02001245 $pre_listen_hook->()
1246 if $pre_listen_hook;
1247
1248 REQUEST:
1249 while ($cgi = $CGI->new()) {
1250 $pre_dispatch_hook->()
1251 if $pre_dispatch_hook;
1252
1253 run_request();
1254
Jakub Narebski0b450102010-08-02 22:21:47 +02001255 $post_dispatch_hook->()
Sam Vilaina0446e72010-05-07 14:54:05 +02001256 if $post_dispatch_hook;
Jakub Narebskida4b2432010-11-25 19:43:59 +01001257 $first_request = 0;
Sam Vilaina0446e72010-05-07 14:54:05 +02001258
1259 last REQUEST if ($is_last_request->());
1260 }
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001261
1262 DONE_GITWEB:
1263 1;
Kay Sievers09bd7892005-08-07 20:21:23 +02001264}
Sam Vilaina0446e72010-05-07 14:54:05 +02001265
Jakub Narebskic2394fe2010-05-07 14:54:04 +02001266run();
Kay Sievers09bd7892005-08-07 20:21:23 +02001267
Jakub Narebski5ed2ec12010-06-13 12:09:32 +02001268if (defined caller) {
1269 # wrapped in a subroutine processing requests,
1270 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1271 return;
1272} else {
1273 # pure CGI script, serving single request
1274 exit;
1275}
Kay Sievers823d5dc2005-08-07 19:57:58 +02001276
Jakub Narebski717b8312006-07-31 21:22:15 +02001277## ======================================================================
Martin Waitz06a9d862006-08-16 00:23:50 +02001278## action links
1279
Jakub Narebski377bee32010-04-24 15:53:19 +02001280# possible values of extra options
1281# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1282# -replay => 1 - start from a current view (replay with modifications)
1283# -path_info => 0|1 - don't use/use path_info URL (if possible)
Kevin Cernekee5e96a842011-03-18 17:00:16 +01001284# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
Jakub Narebski74fd8722009-05-07 19:11:29 +02001285sub href {
Jakub Narebski498fe002006-08-22 19:05:25 +02001286 my %params = @_;
Jakub Narebskibd5d1e42006-11-19 15:05:21 +01001287 # default is to use -absolute url() i.e. $my_uri
1288 my $href = $params{-full} ? $my_url : $my_uri;
Jakub Narebski498fe002006-08-22 19:05:25 +02001289
Kevin Cernekee5e96a842011-03-18 17:00:16 +01001290 # implicit -replay, must be first of implicit params
1291 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1292
Jakub Narebskiafa9b622008-02-14 09:22:30 +01001293 $params{'project'} = $project unless exists $params{'project'};
1294
Jakub Narebski1cad2832007-11-01 13:06:27 +01001295 if ($params{-replay}) {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001296 while (my ($name, $symbol) = each %cgi_param_mapping) {
Jakub Narebski1cad2832007-11-01 13:06:27 +01001297 if (!exists $params{$name}) {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001298 $params{$name} = $input_params{$name};
Jakub Narebski1cad2832007-11-01 13:06:27 +01001299 }
1300 }
1301 }
1302
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08001303 my $use_pathinfo = gitweb_check_feature('pathinfo');
Jakub Narebski377bee32010-04-24 15:53:19 +02001304 if (defined $params{'project'} &&
1305 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001306 # try to put as many parameters as possible in PATH_INFO:
1307 # - project name
1308 # - action
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001309 # - hash_parent or hash_parent_base:/file_parent
Giuseppe Bilotta3550ea72008-10-21 21:34:52 +02001310 # - hash or hash_base:/filename
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +01001311 # - the snapshot_format as an appropriate suffix
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001312
1313 # When the script is the root DirectoryIndex for the domain,
1314 # $href here would be something like http://gitweb.example.com/
1315 # Thus, we strip any trailing / from $href, to spare us double
1316 # slashes in the final URL
1317 $href =~ s,/$,,;
1318
1319 # Then add the project name, if present
Jakub Narebski67976c62010-12-14 16:54:31 +01001320 $href .= "/".esc_path_info($params{'project'});
Martin Waitz9e756902006-10-01 23:57:48 +02001321 delete $params{'project'};
1322
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +01001323 # since we destructively absorb parameters, we keep this
1324 # boolean that remembers if we're handling a snapshot
1325 my $is_snapshot = $params{'action'} eq 'snapshot';
1326
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001327 # Summary just uses the project path URL, any other action is
1328 # added to the URL
1329 if (defined $params{'action'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001330 $href .= "/".esc_path_info($params{'action'})
1331 unless $params{'action'} eq 'summary';
Martin Waitz9e756902006-10-01 23:57:48 +02001332 delete $params{'action'};
1333 }
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001334
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001335 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1336 # stripping nonexistent or useless pieces
1337 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1338 || $params{'hash_parent'} || $params{'hash'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001339 if (defined $params{'hash_base'}) {
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001340 if (defined $params{'hash_parent_base'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001341 $href .= esc_path_info($params{'hash_parent_base'});
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001342 # skip the file_parent if it's the same as the file_name
Giuseppe Bilottab7da7212009-07-31 08:48:49 +02001343 if (defined $params{'file_parent'}) {
1344 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1345 delete $params{'file_parent'};
1346 } elsif ($params{'file_parent'} !~ /\.\./) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001347 $href .= ":/".esc_path_info($params{'file_parent'});
Giuseppe Bilottab7da7212009-07-31 08:48:49 +02001348 delete $params{'file_parent'};
1349 }
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001350 }
1351 $href .= "..";
1352 delete $params{'hash_parent'};
1353 delete $params{'hash_parent_base'};
1354 } elsif (defined $params{'hash_parent'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001355 $href .= esc_path_info($params{'hash_parent'}). "..";
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001356 delete $params{'hash_parent'};
1357 }
1358
Jakub Narebski67976c62010-12-14 16:54:31 +01001359 $href .= esc_path_info($params{'hash_base'});
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +02001360 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001361 $href .= ":/".esc_path_info($params{'file_name'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001362 delete $params{'file_name'};
1363 }
1364 delete $params{'hash'};
1365 delete $params{'hash_base'};
1366 } elsif (defined $params{'hash'}) {
Jakub Narebski67976c62010-12-14 16:54:31 +01001367 $href .= esc_path_info($params{'hash'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +02001368 delete $params{'hash'};
1369 }
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +01001370
1371 # If the action was a snapshot, we can absorb the
1372 # snapshot_format parameter too
1373 if ($is_snapshot) {
1374 my $fmt = $params{'snapshot_format'};
1375 # snapshot_format should always be defined when href()
1376 # is called, but just in case some code forgets, we
1377 # fall back to the default
1378 $fmt ||= $snapshot_fmts[0];
1379 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1380 delete $params{'snapshot_format'};
1381 }
Martin Waitz9e756902006-10-01 23:57:48 +02001382 }
1383
1384 # now encode the parameters explicitly
Jakub Narebski498fe002006-08-22 19:05:25 +02001385 my @result = ();
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001386 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1387 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
Jakub Narebski498fe002006-08-22 19:05:25 +02001388 if (defined $params{$name}) {
Jakub Narebskif22cca42007-07-29 01:04:09 +02001389 if (ref($params{$name}) eq "ARRAY") {
1390 foreach my $par (@{$params{$name}}) {
1391 push @result, $symbol . "=" . esc_param($par);
1392 }
1393 } else {
1394 push @result, $symbol . "=" . esc_param($params{$name});
1395 }
Jakub Narebski498fe002006-08-22 19:05:25 +02001396 }
1397 }
Martin Waitz9e756902006-10-01 23:57:48 +02001398 $href .= "?" . join(';', @result) if scalar @result;
1399
Jakub Narebski67976c62010-12-14 16:54:31 +01001400 # final transformation: trailing spaces must be escaped (URI-encoded)
1401 $href =~ s/(\s+)$/CGI::escape($1)/e;
1402
Kevin Cernekee5e96a842011-03-18 17:00:16 +01001403 if ($params{-anchor}) {
1404 $href .= "#".esc_param($params{-anchor});
1405 }
1406
Martin Waitz9e756902006-10-01 23:57:48 +02001407 return $href;
Martin Waitz06a9d862006-08-16 00:23:50 +02001408}
1409
1410
1411## ======================================================================
Jakub Narebski717b8312006-07-31 21:22:15 +02001412## validation, quoting/unquoting and escaping
1413
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001414sub validate_action {
1415 my $input = shift || return undef;
1416 return undef unless exists $actions{$input};
1417 return $input;
1418}
1419
1420sub validate_project {
1421 my $input = shift || return undef;
1422 if (!validate_pathname($input) ||
1423 !(-d "$projectroot/$input") ||
Alexander Gavrilovec26f092008-11-06 01:15:56 +03001424 !check_export_ok("$projectroot/$input") ||
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02001425 ($strict_export && !project_in_list($input))) {
1426 return undef;
1427 } else {
1428 return $input;
1429 }
1430}
1431
Jakub Narebski24d06932006-09-26 01:57:02 +02001432sub validate_pathname {
1433 my $input = shift || return undef;
Jakub Narebski717b8312006-07-31 21:22:15 +02001434
Jakub Narebski24d06932006-09-26 01:57:02 +02001435 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1436 # at the beginning, at the end, and between slashes.
1437 # also this catches doubled slashes
1438 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1439 return undef;
1440 }
1441 # no null characters
1442 if ($input =~ m!\0!) {
1443 return undef;
1444 }
1445 return $input;
1446}
1447
1448sub validate_refname {
1449 my $input = shift || return undef;
1450
1451 # textual hashes are O.K.
Jakub Narebski717b8312006-07-31 21:22:15 +02001452 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1453 return $input;
1454 }
Jakub Narebski24d06932006-09-26 01:57:02 +02001455 # it must be correct pathname
1456 $input = validate_pathname($input)
1457 or return undef;
1458 # restrictions on ref name according to git-check-ref-format
1459 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001460 return undef;
1461 }
1462 return $input;
1463}
1464
Martin Koegler00f429a2007-06-03 17:42:44 +02001465# decode sequences of octets in utf8 into Perl's internal form,
1466# which is utf-8 with utf8 flag set if needed. gitweb writes out
1467# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1468sub to_utf8 {
1469 my $str = shift;
Jakub Narebski1df48762010-02-07 21:52:25 +01001470 return undef unless defined $str;
Jakub Narebskib13e3ea2011-12-18 23:00:58 +01001471
1472 if (utf8::is_utf8($str) || utf8::decode($str)) {
İsmail Dönmeze5d3de52007-12-04 10:55:41 +02001473 return $str;
Martin Koegler00f429a2007-06-03 17:42:44 +02001474 } else {
1475 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1476 }
1477}
1478
Kay Sievers232ff552005-11-24 16:56:55 +01001479# quote unsafe chars, but keep the slash, even when it's not
1480# correct, but quoted slashes look too horrible in bookmarks
1481sub esc_param {
Kay Sievers353347b2005-11-14 05:47:18 +01001482 my $str = shift;
Jakub Narebski1df48762010-02-07 21:52:25 +01001483 return undef unless defined $str;
Giuseppe Bilotta452e2252009-10-13 21:51:36 +02001484 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
Kay Sieversa9e60b72005-11-14 15:15:12 +01001485 $str =~ s/ /\+/g;
Kay Sievers353347b2005-11-14 05:47:18 +01001486 return $str;
1487}
1488
Jakub Narebski67976c62010-12-14 16:54:31 +01001489# the quoting rules for path_info fragment are slightly different
1490sub esc_path_info {
1491 my $str = shift;
1492 return undef unless defined $str;
1493
1494 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1495 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1496
1497 return $str;
1498}
1499
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02001500# quote unsafe chars in whole URL, so some characters cannot be quoted
Jakub Narebskif93bff82006-09-26 01:58:41 +02001501sub esc_url {
1502 my $str = shift;
Jakub Narebski1df48762010-02-07 21:52:25 +01001503 return undef unless defined $str;
Pavan Kumar Sunkara109988f2010-07-15 12:59:01 +05301504 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
Jakub Narebskif93bff82006-09-26 01:58:41 +02001505 $str =~ s/ /\+/g;
1506 return $str;
1507}
1508
Jakub Narebski3017ed62010-12-15 00:34:01 +01001509# quote unsafe characters in HTML attributes
1510sub esc_attr {
1511
1512 # for XHTML conformance escaping '"' to '&quot;' is not enough
1513 return esc_html(@_);
1514}
1515
Kay Sievers232ff552005-11-24 16:56:55 +01001516# replace invalid utf8 character with SUBSTITUTION sequence
Jakub Narebski74fd8722009-05-07 19:11:29 +02001517sub esc_html {
Kay Sievers40c13812005-11-19 17:41:29 +01001518 my $str = shift;
Jakub Narebski6255ef02006-11-01 14:33:21 +01001519 my %opts = @_;
1520
Jakub Narebski1df48762010-02-07 21:52:25 +01001521 return undef unless defined $str;
1522
Martin Koegler00f429a2007-06-03 17:42:44 +02001523 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +08001524 $str = $cgi->escapeHTML($str);
Jakub Narebski6255ef02006-11-01 14:33:21 +01001525 if ($opts{'-nbsp'}) {
1526 $str =~ s/ /&nbsp;/g;
1527 }
Junio C Hamano25ffbb22006-11-08 15:11:10 -08001528 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
Kay Sievers40c13812005-11-19 17:41:29 +01001529 return $str;
1530}
1531
Jakub Narebski391862e2006-11-25 09:43:59 +01001532# quote control characters and escape filename to HTML
1533sub esc_path {
1534 my $str = shift;
1535 my %opts = @_;
1536
Jakub Narebski1df48762010-02-07 21:52:25 +01001537 return undef unless defined $str;
1538
Martin Koegler00f429a2007-06-03 17:42:44 +02001539 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +08001540 $str = $cgi->escapeHTML($str);
Jakub Narebski391862e2006-11-25 09:43:59 +01001541 if ($opts{'-nbsp'}) {
1542 $str =~ s/ /&nbsp;/g;
1543 }
1544 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1545 return $str;
1546}
1547
Jakub Narebski08667862011-09-16 14:41:57 +02001548# Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)
1549sub sanitize {
1550 my $str = shift;
1551
1552 return undef unless defined $str;
1553
1554 $str = to_utf8($str);
1555 $str =~ s|([[:cntrl:]])|($1 =~ /[\t\n\r]/ ? $1 : quot_cec($1))|eg;
1556 return $str;
1557}
1558
Jakub Narebski391862e2006-11-25 09:43:59 +01001559# Make control characters "printable", using character escape codes (CEC)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001560sub quot_cec {
1561 my $cntrl = shift;
Jakub Narebskic84c4832008-02-17 18:48:13 +01001562 my %opts = @_;
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001563 my %es = ( # character escape codes, aka escape sequences
Jakub Narebskic84c4832008-02-17 18:48:13 +01001564 "\t" => '\t', # tab (HT)
1565 "\n" => '\n', # line feed (LF)
1566 "\r" => '\r', # carrige return (CR)
1567 "\f" => '\f', # form feed (FF)
1568 "\b" => '\b', # backspace (BS)
1569 "\a" => '\a', # alarm (bell) (BEL)
1570 "\e" => '\e', # escape (ESC)
1571 "\013" => '\v', # vertical tab (VT)
1572 "\000" => '\0', # nul character (NUL)
1573 );
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001574 my $chr = ( (exists $es{$cntrl})
1575 ? $es{$cntrl}
Petr Baudis25dfd172008-10-01 22:11:54 +02001576 : sprintf('\%2x', ord($cntrl)) );
Jakub Narebskic84c4832008-02-17 18:48:13 +01001577 if ($opts{-nohtml}) {
1578 return $chr;
1579 } else {
1580 return "<span class=\"cntrl\">$chr</span>";
1581 }
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001582}
1583
Jakub Narebski391862e2006-11-25 09:43:59 +01001584# Alternatively use unicode control pictures codepoints,
1585# Unicode "printable representation" (PR)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001586sub quot_upr {
1587 my $cntrl = shift;
Jakub Narebskic84c4832008-02-17 18:48:13 +01001588 my %opts = @_;
1589
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001590 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
Jakub Narebskic84c4832008-02-17 18:48:13 +01001591 if ($opts{-nohtml}) {
1592 return $chr;
1593 } else {
1594 return "<span class=\"cntrl\">$chr</span>";
1595 }
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001596}
1597
Kay Sievers232ff552005-11-24 16:56:55 +01001598# git may return quoted and escaped filenames
1599sub unquote {
1600 my $str = shift;
Jakub Narebski403d0902006-11-08 11:48:56 +01001601
1602 sub unq {
1603 my $seq = shift;
1604 my %es = ( # character escape codes, aka escape sequences
1605 't' => "\t", # tab (HT, TAB)
1606 'n' => "\n", # newline (NL)
1607 'r' => "\r", # return (CR)
1608 'f' => "\f", # form feed (FF)
1609 'b' => "\b", # backspace (BS)
1610 'a' => "\a", # alarm (bell) (BEL)
1611 'e' => "\e", # escape (ESC)
1612 'v' => "\013", # vertical tab (VT)
1613 );
1614
1615 if ($seq =~ m/^[0-7]{1,3}$/) {
1616 # octal char sequence
1617 return chr(oct($seq));
1618 } elsif (exists $es{$seq}) {
1619 # C escape sequence, aka character escape code
Jakub Narebskic84c4832008-02-17 18:48:13 +01001620 return $es{$seq};
Jakub Narebski403d0902006-11-08 11:48:56 +01001621 }
1622 # quoted ordinary character
1623 return $seq;
1624 }
1625
Kay Sievers232ff552005-11-24 16:56:55 +01001626 if ($str =~ m/^"(.*)"$/) {
Jakub Narebski403d0902006-11-08 11:48:56 +01001627 # needs unquoting
Kay Sievers232ff552005-11-24 16:56:55 +01001628 $str = $1;
Jakub Narebski403d0902006-11-08 11:48:56 +01001629 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
Kay Sievers232ff552005-11-24 16:56:55 +01001630 }
1631 return $str;
1632}
1633
Jakub Narebskif16db172006-08-06 02:08:31 +02001634# escape tabs (convert tabs to spaces)
1635sub untabify {
1636 my $line = shift;
1637
1638 while ((my $pos = index($line, "\t")) != -1) {
1639 if (my $count = (8 - ($pos % 8))) {
1640 my $spaces = ' ' x $count;
1641 $line =~ s/\t/$spaces/;
1642 }
1643 }
1644
1645 return $line;
1646}
1647
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +02001648sub project_in_list {
1649 my $project = shift;
1650 my @list = git_get_projects_list();
1651 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1652}
1653
Jakub Narebski717b8312006-07-31 21:22:15 +02001654## ----------------------------------------------------------------------
1655## HTML aware string manipulation
1656
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001657# Try to chop given string on a word boundary between position
1658# $len and $len+$add_len. If there is no word boundary there,
1659# chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1660# (marking chopped part) would be longer than given string.
Jakub Narebski717b8312006-07-31 21:22:15 +02001661sub chop_str {
1662 my $str = shift;
1663 my $len = shift;
1664 my $add_len = shift || 10;
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001665 my $where = shift || 'right'; # 'left' | 'center' | 'right'
Jakub Narebski717b8312006-07-31 21:22:15 +02001666
Anders Waldenborgdee27752008-05-21 13:44:43 +02001667 # Make sure perl knows it is utf8 encoded so we don't
1668 # cut in the middle of a utf8 multibyte char.
1669 $str = to_utf8($str);
1670
Jakub Narebski717b8312006-07-31 21:22:15 +02001671 # allow only $len chars, but don't cut a word if it would fit in $add_len
1672 # 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 +01001673 # remove chopped character entities entirely
1674
1675 # when chopping in the middle, distribute $len into left and right part
1676 # return early if chopping wouldn't make string shorter
1677 if ($where eq 'center') {
1678 return $str if ($len + 5 >= length($str)); # filler is length 5
1679 $len = int($len/2);
1680 } else {
1681 return $str if ($len + 4 >= length($str)); # filler is length 4
Jakub Narebski717b8312006-07-31 21:22:15 +02001682 }
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001683
1684 # regexps: ending and beginning with word part up to $add_len
1685 my $endre = qr/.{$len}\w{0,$add_len}/;
1686 my $begre = qr/\w{0,$add_len}.{$len}/;
1687
1688 if ($where eq 'left') {
1689 $str =~ m/^(.*?)($begre)$/;
1690 my ($lead, $body) = ($1, $2);
1691 if (length($lead) > 4) {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001692 $lead = " ...";
1693 }
1694 return "$lead$body";
1695
1696 } elsif ($where eq 'center') {
1697 $str =~ m/^($endre)(.*)$/;
1698 my ($left, $str) = ($1, $2);
1699 $str =~ m/^(.*?)($begre)$/;
1700 my ($mid, $right) = ($1, $2);
1701 if (length($mid) > 5) {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001702 $mid = " ... ";
1703 }
1704 return "$left$mid$right";
1705
1706 } else {
1707 $str =~ m/^($endre)(.*)$/;
1708 my $body = $1;
1709 my $tail = $2;
1710 if (length($tail) > 4) {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001711 $tail = "... ";
1712 }
1713 return "$body$tail";
1714 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001715}
1716
David Symondsce58ec92007-10-23 11:31:22 +10001717# takes the same arguments as chop_str, but also wraps a <span> around the
1718# result with a title attribute if it does get chopped. Additionally, the
1719# string is HTML-escaped.
1720sub chop_and_escape_str {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001721 my ($str) = @_;
David Symondsce58ec92007-10-23 11:31:22 +10001722
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001723 my $chopped = chop_str(@_);
Jürgen Kreileder168c1e02011-12-17 10:22:21 +01001724 $str = to_utf8($str);
David Symondsce58ec92007-10-23 11:31:22 +10001725 if ($chopped eq $str) {
1726 return esc_html($chopped);
1727 } else {
Jakub Narebski14afe772009-05-22 17:35:46 +02001728 $str =~ s/[[:cntrl:]]/?/g;
Jakub Narebski850b90a2008-02-16 23:07:46 +01001729 return $cgi->span({-title=>$str}, esc_html($chopped));
David Symondsce58ec92007-10-23 11:31:22 +10001730 }
1731}
1732
Jakub Narebski337da8d2012-02-27 02:55:19 +01001733# Highlight selected fragments of string, using given CSS class,
1734# and escape HTML. It is assumed that fragments do not overlap.
1735# Regions are passed as list of pairs (array references).
1736#
1737# Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns
1738# '<span class="mark">foo</span>bar'
1739sub esc_html_hl_regions {
1740 my ($str, $css_class, @sel) = @_;
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001741 my %opts = grep { ref($_) ne 'ARRAY' } @sel;
1742 @sel = grep { ref($_) eq 'ARRAY' } @sel;
1743 return esc_html($str, %opts) unless @sel;
Jakub Narebski337da8d2012-02-27 02:55:19 +01001744
1745 my $out = '';
1746 my $pos = 0;
1747
1748 for my $s (@sel) {
Michał Kiedrowiczce61fb92012-04-11 23:18:37 +02001749 my ($begin, $end) = @$s;
Jakub Narebski337da8d2012-02-27 02:55:19 +01001750
Michał Kiedrowiczcbbea3d2012-04-11 23:18:38 +02001751 # Don't create empty <span> elements.
1752 next if $end <= $begin;
1753
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001754 my $escaped = esc_html(substr($str, $begin, $end - $begin),
1755 %opts);
Michał Kiedrowiczce61fb92012-04-11 23:18:37 +02001756
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001757 $out .= esc_html(substr($str, $pos, $begin - $pos), %opts)
Michał Kiedrowiczce61fb92012-04-11 23:18:37 +02001758 if ($begin - $pos > 0);
1759 $out .= $cgi->span({-class => $css_class}, $escaped);
1760
1761 $pos = $end;
Jakub Narebski337da8d2012-02-27 02:55:19 +01001762 }
Jakub Narębski9768a9d2012-04-11 23:18:39 +02001763 $out .= esc_html(substr($str, $pos), %opts)
Jakub Narebski337da8d2012-02-27 02:55:19 +01001764 if ($pos < length($str));
1765
1766 return $out;
1767}
1768
Jakub Narebskie607b792012-02-27 02:55:22 +01001769# return positions of beginning and end of each match
1770sub matchpos_list {
Jakub Narebski337da8d2012-02-27 02:55:19 +01001771 my ($str, $regexp) = @_;
Jakub Narebskie607b792012-02-27 02:55:22 +01001772 return unless (defined $str && defined $regexp);
Jakub Narebski337da8d2012-02-27 02:55:19 +01001773
1774 my @matches;
1775 while ($str =~ /$regexp/g) {
1776 push @matches, [$-[0], $+[0]];
1777 }
Jakub Narebskie607b792012-02-27 02:55:22 +01001778 return @matches;
1779}
1780
1781# highlight match (if any), and escape HTML
1782sub esc_html_match_hl {
1783 my ($str, $regexp) = @_;
1784 return esc_html($str) unless defined $regexp;
1785
1786 my @matches = matchpos_list($str, $regexp);
Jakub Narebski337da8d2012-02-27 02:55:19 +01001787 return esc_html($str) unless @matches;
1788
1789 return esc_html_hl_regions($str, 'match', @matches);
1790}
1791
Jakub Narebskie607b792012-02-27 02:55:22 +01001792
1793# highlight match (if any) of shortened string, and escape HTML
1794sub esc_html_match_hl_chopped {
1795 my ($str, $chopped, $regexp) = @_;
1796 return esc_html_match_hl($str, $regexp) unless defined $chopped;
1797
1798 my @matches = matchpos_list($str, $regexp);
1799 return esc_html($chopped) unless @matches;
1800
1801 # filter matches so that we mark chopped string
1802 my $tail = "... "; # see chop_str
1803 unless ($chopped =~ s/\Q$tail\E$//) {
1804 $tail = '';
1805 }
1806 my $chop_len = length($chopped);
1807 my $tail_len = length($tail);
1808 my @filtered;
1809
1810 for my $m (@matches) {
1811 if ($m->[0] > $chop_len) {
1812 push @filtered, [ $chop_len, $chop_len + $tail_len ] if ($tail_len > 0);
1813 last;
1814 } elsif ($m->[1] > $chop_len) {
1815 push @filtered, [ $m->[0], $chop_len + $tail_len ];
1816 last;
1817 }
1818 push @filtered, $m;
1819 }
1820
1821 return esc_html_hl_regions($chopped . $tail, 'match', @filtered);
1822}
1823
Jakub Narebski717b8312006-07-31 21:22:15 +02001824## ----------------------------------------------------------------------
1825## functions returning short strings
1826
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00001827# CSS class for given age value (in seconds)
1828sub age_class {
1829 my $age = shift;
1830
Jakub Narebski785cdea2007-05-13 12:39:22 +02001831 if (!defined $age) {
1832 return "noage";
1833 } elsif ($age < 60*60*2) {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00001834 return "age0";
1835 } elsif ($age < 60*60*24*2) {
1836 return "age1";
1837 } else {
1838 return "age2";
1839 }
1840}
1841
Jakub Narebski717b8312006-07-31 21:22:15 +02001842# convert age in seconds to "nn units ago" string
1843sub age_string {
1844 my $age = shift;
1845 my $age_str;
1846
1847 if ($age > 60*60*24*365*2) {
1848 $age_str = (int $age/60/60/24/365);
1849 $age_str .= " years ago";
1850 } elsif ($age > 60*60*24*(365/12)*2) {
1851 $age_str = int $age/60/60/24/(365/12);
1852 $age_str .= " months ago";
1853 } elsif ($age > 60*60*24*7*2) {
1854 $age_str = int $age/60/60/24/7;
1855 $age_str .= " weeks ago";
1856 } elsif ($age > 60*60*24*2) {
1857 $age_str = int $age/60/60/24;
1858 $age_str .= " days ago";
1859 } elsif ($age > 60*60*2) {
1860 $age_str = int $age/60/60;
1861 $age_str .= " hours ago";
1862 } elsif ($age > 60*2) {
1863 $age_str = int $age/60;
1864 $age_str .= " min ago";
1865 } elsif ($age > 2) {
1866 $age_str = int $age;
1867 $age_str .= " sec ago";
1868 } else {
1869 $age_str .= " right now";
1870 }
1871 return $age_str;
1872}
1873
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001874use constant {
1875 S_IFINVALID => 0030000,
1876 S_IFGITLINK => 0160000,
1877};
1878
1879# submodule/subproject, a commit object reference
Jakub Narebski74fd8722009-05-07 19:11:29 +02001880sub S_ISGITLINK {
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001881 my $mode = shift;
1882
1883 return (($mode & S_IFMT) == S_IFGITLINK)
1884}
1885
Jakub Narebski717b8312006-07-31 21:22:15 +02001886# convert file mode in octal to symbolic file mode string
1887sub mode_str {
1888 my $mode = oct shift;
1889
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001890 if (S_ISGITLINK($mode)) {
1891 return 'm---------';
1892 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001893 return 'drwxr-xr-x';
1894 } elsif (S_ISLNK($mode)) {
1895 return 'lrwxrwxrwx';
1896 } elsif (S_ISREG($mode)) {
1897 # git cares only about the executable bit
1898 if ($mode & S_IXUSR) {
1899 return '-rwxr-xr-x';
1900 } else {
1901 return '-rw-r--r--';
1902 };
1903 } else {
1904 return '----------';
1905 }
1906}
1907
1908# convert file mode in octal to file type string
1909sub file_type {
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02001910 my $mode = shift;
1911
1912 if ($mode !~ m/^[0-7]+$/) {
1913 return $mode;
1914 } else {
1915 $mode = oct $mode;
1916 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001917
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001918 if (S_ISGITLINK($mode)) {
1919 return "submodule";
1920 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001921 return "directory";
1922 } elsif (S_ISLNK($mode)) {
1923 return "symlink";
1924 } elsif (S_ISREG($mode)) {
1925 return "file";
1926 } else {
1927 return "unknown";
1928 }
1929}
1930
Jakub Narebski744d0ac2006-11-08 17:59:41 +01001931# convert file mode in octal to file type description string
1932sub file_type_long {
1933 my $mode = shift;
1934
1935 if ($mode !~ m/^[0-7]+$/) {
1936 return $mode;
1937 } else {
1938 $mode = oct $mode;
1939 }
1940
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001941 if (S_ISGITLINK($mode)) {
1942 return "submodule";
1943 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski744d0ac2006-11-08 17:59:41 +01001944 return "directory";
1945 } elsif (S_ISLNK($mode)) {
1946 return "symlink";
1947 } elsif (S_ISREG($mode)) {
1948 if ($mode & S_IXUSR) {
1949 return "executable";
1950 } else {
1951 return "file";
1952 };
1953 } else {
1954 return "unknown";
1955 }
1956}
1957
1958
Jakub Narebski717b8312006-07-31 21:22:15 +02001959## ----------------------------------------------------------------------
1960## functions returning short HTML fragments, or transforming HTML fragments
Pavel Roskin3dff5372007-02-03 23:49:16 -05001961## which don't belong to other sections
Jakub Narebski717b8312006-07-31 21:22:15 +02001962
Junio C Hamano225932e2006-11-09 00:57:13 -08001963# format line of commit message.
Jakub Narebski717b8312006-07-31 21:22:15 +02001964sub format_log_line_html {
1965 my $line = shift;
1966
Junio C Hamano225932e2006-11-09 00:57:13 -08001967 $line = esc_html($line, -nbsp=>1);
Marcel M. Cary7d233de2009-02-17 19:00:43 -08001968 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1969 $cgi->a({-href => href(action=>"object", hash=>$1),
1970 -class => "text"}, $1);
1971 }eg;
1972
Jakub Narebski717b8312006-07-31 21:22:15 +02001973 return $line;
1974}
1975
1976# format marker of refs pointing to given object
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001977
1978# the destination action is chosen based on object type and current context:
1979# - for annotated tags, we choose the tag view unless it's the current view
1980# already, in which case we go to shortlog view
1981# - for other refs, we keep the current view if we're in history, shortlog or
1982# log view, and select shortlog otherwise
Jakub Narebski847e01f2006-08-14 02:05:47 +02001983sub format_ref_marker {
Jakub Narebski717b8312006-07-31 21:22:15 +02001984 my ($refs, $id) = @_;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001985 my $markers = '';
Jakub Narebski717b8312006-07-31 21:22:15 +02001986
1987 if (defined $refs->{$id}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001988 foreach my $ref (@{$refs->{$id}}) {
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001989 # this code exploits the fact that non-lightweight tags are the
1990 # only indirect objects, and that they are the only objects for which
1991 # we want to use tag instead of shortlog as action
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001992 my ($type, $name) = qw();
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001993 my $indirect = ($ref =~ s/\^\{\}$//);
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001994 # e.g. tags/v2.6.11 or heads/next
1995 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1996 $type = $1;
1997 $name = $2;
1998 } else {
1999 $type = "ref";
2000 $name = $ref;
2001 }
2002
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002003 my $class = $type;
2004 $class .= " indirect" if $indirect;
2005
2006 my $dest_action = "shortlog";
2007
2008 if ($indirect) {
2009 $dest_action = "tag" unless $action eq "tag";
2010 } elsif ($action =~ /^(history|(short)?log)$/) {
2011 $dest_action = $action;
2012 }
2013
2014 my $dest = "";
2015 $dest .= "refs/" unless $ref =~ m!^refs/!;
2016 $dest .= $ref;
2017
2018 my $link = $cgi->a({
2019 -href => href(
2020 action=>$dest_action,
2021 hash=>$dest
2022 )}, $name);
2023
Jakub Narebski3017ed62010-12-15 00:34:01 +01002024 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002025 $link . "</span>";
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002026 }
2027 }
2028
2029 if ($markers) {
2030 return ' <span class="refs">'. $markers . '</span>';
Jakub Narebski717b8312006-07-31 21:22:15 +02002031 } else {
2032 return "";
2033 }
2034}
2035
Jakub Narebski17d07442006-08-14 02:08:27 +02002036# format, perhaps shortened and with markers, title line
2037sub format_subject_html {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002038 my ($long, $short, $href, $extra) = @_;
Jakub Narebski17d07442006-08-14 02:08:27 +02002039 $extra = '' unless defined($extra);
2040
2041 if (length($short) < length($long)) {
Jakub Narebski14afe772009-05-22 17:35:46 +02002042 $long =~ s/[[:cntrl:]]/?/g;
Jakub Narebski7c278012006-08-22 12:02:48 +02002043 return $cgi->a({-href => $href, -class => "list subject",
Martin Koegler00f429a2007-06-03 17:42:44 +02002044 -title => to_utf8($long)},
Giuseppe Bilotta01b89f02009-08-23 10:28:09 +02002045 esc_html($short)) . $extra;
Jakub Narebski17d07442006-08-14 02:08:27 +02002046 } else {
Jakub Narebski7c278012006-08-22 12:02:48 +02002047 return $cgi->a({-href => $href, -class => "list subject"},
Giuseppe Bilotta01b89f02009-08-23 10:28:09 +02002048 esc_html($long)) . $extra;
Jakub Narebski17d07442006-08-14 02:08:27 +02002049 }
2050}
2051
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002052# Rather than recomputing the url for an email multiple times, we cache it
2053# after the first hit. This gives a visible benefit in views where the avatar
2054# for the same email is used repeatedly (e.g. shortlog).
2055# The cache is shared by all avatar engines (currently gravatar only), which
2056# are free to use it as preferred. Since only one avatar engine is used for any
2057# given page, there's no risk for cache conflicts.
2058our %avatar_cache = ();
2059
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002060# Compute the picon url for a given email, by using the picon search service over at
2061# http://www.cs.indiana.edu/picons/search.html
2062sub picon_url {
2063 my $email = lc shift;
2064 if (!$avatar_cache{$email}) {
2065 my ($user, $domain) = split('@', $email);
2066 $avatar_cache{$email} =
2067 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
2068 "$domain/$user/" .
2069 "users+domains+unknown/up/single";
2070 }
2071 return $avatar_cache{$email};
2072}
2073
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002074# Compute the gravatar url for a given email, if it's not in the cache already.
2075# Gravatar stores only the part of the URL before the size, since that's the
2076# one computationally more expensive. This also allows reuse of the cache for
2077# different sizes (for this particular engine).
2078sub gravatar_url {
2079 my $email = lc shift;
2080 my $size = shift;
2081 $avatar_cache{$email} ||=
2082 "http://www.gravatar.com/avatar/" .
2083 Digest::MD5::md5_hex($email) . "?s=";
2084 return $avatar_cache{$email} . $size;
2085}
2086
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002087# Insert an avatar for the given $email at the given $size if the feature
2088# is enabled.
2089sub git_get_avatar {
2090 my ($email, %opts) = @_;
2091 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
2092 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
2093 $opts{-size} ||= 'default';
2094 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
2095 my $url = "";
2096 if ($git_avatar eq 'gravatar') {
Giuseppe Bilotta5a371b72009-06-30 00:00:52 +02002097 $url = gravatar_url($email, $size);
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002098 } elsif ($git_avatar eq 'picon') {
2099 $url = picon_url($email);
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002100 }
Giuseppe Bilotta679a1a12009-06-30 00:00:53 +02002101 # Other providers can be added by extending the if chain, defining $url
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002102 # as needed. If no variant puts something in $url, we assume avatars
2103 # are completely disabled/unavailable.
2104 if ($url) {
2105 return $pre_white .
2106 "<img width=\"$size\" " .
2107 "class=\"avatar\" " .
Jakub Narebski3017ed62010-12-15 00:34:01 +01002108 "src=\"".esc_url($url)."\" " .
Giuseppe Bilotta7d25ef42009-06-30 00:00:54 +02002109 "alt=\"\" " .
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002110 "/>" . $post_white;
2111 } else {
2112 return "";
2113 }
2114}
2115
Stephen Boyde133d652009-10-15 21:14:59 -07002116sub format_search_author {
2117 my ($author, $searchtype, $displaytext) = @_;
2118 my $have_search = gitweb_check_feature('search');
2119
2120 if ($have_search) {
2121 my $performed = "";
2122 if ($searchtype eq 'author') {
2123 $performed = "authored";
2124 } elsif ($searchtype eq 'committer') {
2125 $performed = "committed";
2126 }
2127
2128 return $cgi->a({-href => href(action=>"search", hash=>$hash,
2129 searchtext=>$author,
2130 searchtype=>$searchtype), class=>"list",
2131 title=>"Search for commits $performed by $author"},
2132 $displaytext);
2133
2134 } else {
2135 return $displaytext;
2136 }
2137}
2138
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02002139# format the author name of the given commit with the given tag
2140# the author name is chopped and escaped according to the other
2141# optional parameters (see chop_str).
2142sub format_author_html {
2143 my $tag = shift;
2144 my $co = shift;
2145 my $author = chop_and_escape_str($co->{'author_name'}, @_);
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02002146 return "<$tag class=\"author\">" .
Stephen Boyde133d652009-10-15 21:14:59 -07002147 format_search_author($co->{'author_name'}, "author",
2148 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2149 $author) .
2150 "</$tag>";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02002151}
2152
Jakub Narebski90921742007-06-08 13:27:42 +02002153# format git diff header line, i.e. "diff --(git|combined|cc) ..."
2154sub format_git_diff_header_line {
2155 my $line = shift;
2156 my $diffinfo = shift;
2157 my ($from, $to) = @_;
2158
2159 if ($diffinfo->{'nparents'}) {
2160 # combined diff
2161 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2162 if ($to->{'href'}) {
2163 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2164 esc_path($to->{'file'}));
2165 } else { # file was deleted (no href)
2166 $line .= esc_path($to->{'file'});
2167 }
2168 } else {
2169 # "ordinary" diff
2170 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2171 if ($from->{'href'}) {
2172 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2173 'a/' . esc_path($from->{'file'}));
2174 } else { # file was added (no href)
2175 $line .= 'a/' . esc_path($from->{'file'});
2176 }
2177 $line .= ' ';
2178 if ($to->{'href'}) {
2179 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2180 'b/' . esc_path($to->{'file'}));
2181 } else { # file was deleted
2182 $line .= 'b/' . esc_path($to->{'file'});
2183 }
2184 }
2185
2186 return "<div class=\"diff header\">$line</div>\n";
2187}
2188
2189# format extended diff header line, before patch itself
2190sub format_extended_diff_header_line {
2191 my $line = shift;
2192 my $diffinfo = shift;
2193 my ($from, $to) = @_;
2194
2195 # match <path>
2196 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2197 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2198 esc_path($from->{'file'}));
2199 }
2200 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2201 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2202 esc_path($to->{'file'}));
2203 }
2204 # match single <mode>
2205 if ($line =~ m/\s(\d{6})$/) {
2206 $line .= '<span class="info"> (' .
2207 file_type_long($1) .
2208 ')</span>';
2209 }
2210 # match <hash>
2211 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2212 # can match only for combined diff
2213 $line = 'index ';
2214 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2215 if ($from->{'href'}[$i]) {
2216 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2217 -class=>"hash"},
2218 substr($diffinfo->{'from_id'}[$i],0,7));
2219 } else {
2220 $line .= '0' x 7;
2221 }
2222 # separator
2223 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2224 }
2225 $line .= '..';
2226 if ($to->{'href'}) {
2227 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2228 substr($diffinfo->{'to_id'},0,7));
2229 } else {
2230 $line .= '0' x 7;
2231 }
2232
2233 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2234 # can match only for ordinary diff
2235 my ($from_link, $to_link);
2236 if ($from->{'href'}) {
2237 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2238 substr($diffinfo->{'from_id'},0,7));
2239 } else {
2240 $from_link = '0' x 7;
2241 }
2242 if ($to->{'href'}) {
2243 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2244 substr($diffinfo->{'to_id'},0,7));
2245 } else {
2246 $to_link = '0' x 7;
2247 }
2248 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2249 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2250 }
2251
2252 return $line . "<br/>\n";
2253}
2254
2255# format from-file/to-file diff header
2256sub format_diff_from_to_header {
Jakub Narebski91af4ce2007-06-08 13:32:44 +02002257 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
Jakub Narebski90921742007-06-08 13:27:42 +02002258 my $line;
2259 my $result = '';
2260
2261 $line = $from_line;
2262 #assert($line =~ m/^---/) if DEBUG;
Jakub Narebskideaa01a2007-06-08 13:29:49 +02002263 # no extra formatting for "^--- /dev/null"
2264 if (! $diffinfo->{'nparents'}) {
2265 # ordinary (single parent) diff
2266 if ($line =~ m!^--- "?a/!) {
2267 if ($from->{'href'}) {
2268 $line = '--- a/' .
2269 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2270 esc_path($from->{'file'}));
2271 } else {
2272 $line = '--- a/' .
2273 esc_path($from->{'file'});
2274 }
2275 }
2276 $result .= qq!<div class="diff from_file">$line</div>\n!;
2277
2278 } else {
2279 # combined diff (merge commit)
2280 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2281 if ($from->{'href'}[$i]) {
2282 $line = '--- ' .
Jakub Narebski91af4ce2007-06-08 13:32:44 +02002283 $cgi->a({-href=>href(action=>"blobdiff",
2284 hash_parent=>$diffinfo->{'from_id'}[$i],
2285 hash_parent_base=>$parents[$i],
2286 file_parent=>$from->{'file'}[$i],
2287 hash=>$diffinfo->{'to_id'},
2288 hash_base=>$hash,
2289 file_name=>$to->{'file'}),
2290 -class=>"path",
2291 -title=>"diff" . ($i+1)},
2292 $i+1) .
2293 '/' .
Jakub Narebskideaa01a2007-06-08 13:29:49 +02002294 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2295 esc_path($from->{'file'}[$i]));
2296 } else {
2297 $line = '--- /dev/null';
2298 }
2299 $result .= qq!<div class="diff from_file">$line</div>\n!;
Jakub Narebski90921742007-06-08 13:27:42 +02002300 }
2301 }
Jakub Narebski90921742007-06-08 13:27:42 +02002302
2303 $line = $to_line;
2304 #assert($line =~ m/^\+\+\+/) if DEBUG;
2305 # no extra formatting for "^+++ /dev/null"
2306 if ($line =~ m!^\+\+\+ "?b/!) {
2307 if ($to->{'href'}) {
2308 $line = '+++ b/' .
2309 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2310 esc_path($to->{'file'}));
2311 } else {
2312 $line = '+++ b/' .
2313 esc_path($to->{'file'});
2314 }
2315 }
2316 $result .= qq!<div class="diff to_file">$line</div>\n!;
2317
2318 return $result;
2319}
2320
Jakub Narebskicd030c32007-06-08 13:33:28 +02002321# create note for patch simplified by combined diff
2322sub format_diff_cc_simplified {
2323 my ($diffinfo, @parents) = @_;
2324 my $result = '';
2325
2326 $result .= "<div class=\"diff header\">" .
2327 "diff --cc ";
2328 if (!is_deleted($diffinfo)) {
2329 $result .= $cgi->a({-href => href(action=>"blob",
2330 hash_base=>$hash,
2331 hash=>$diffinfo->{'to_id'},
2332 file_name=>$diffinfo->{'to_file'}),
2333 -class => "path"},
2334 esc_path($diffinfo->{'to_file'}));
2335 } else {
2336 $result .= esc_path($diffinfo->{'to_file'});
2337 }
2338 $result .= "</div>\n" . # class="diff header"
2339 "<div class=\"diff nodifferences\">" .
2340 "Simple merge" .
2341 "</div>\n"; # class="diff nodifferences"
2342
2343 return $result;
2344}
2345
Jakub Narebski20a864c2011-10-31 00:36:20 +01002346sub diff_line_class {
2347 my ($line, $from, $to) = @_;
2348
2349 # ordinary diff
2350 my $num_sign = 1;
2351 # combined diff
2352 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2353 $num_sign = scalar @{$from->{'href'}};
2354 }
2355
2356 my @diff_line_classifier = (
2357 { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
2358 { regexp => qr/^\\/, class => "incomplete" },
2359 { regexp => qr/^ {$num_sign}/, class => "ctx" },
2360 # classifier for context must come before classifier add/rem,
2361 # or we would have to use more complicated regexp, for example
2362 # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
2363 { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
2364 { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
2365 );
2366 for my $clsfy (@diff_line_classifier) {
2367 return $clsfy->{'class'}
2368 if ($line =~ $clsfy->{'regexp'});
2369 }
2370
2371 # fallback
2372 return "";
2373}
2374
Jakub Narebskif1310cf2011-10-31 00:36:21 +01002375# assumes that $from and $to are defined and correctly filled,
2376# and that $line holds a line of chunk header for unified diff
2377sub format_unidiff_chunk_header {
2378 my ($line, $from, $to) = @_;
2379
2380 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2381 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2382
2383 $from_lines = 0 unless defined $from_lines;
2384 $to_lines = 0 unless defined $to_lines;
2385
2386 if ($from->{'href'}) {
2387 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2388 -class=>"list"}, $from_text);
2389 }
2390 if ($to->{'href'}) {
2391 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2392 -class=>"list"}, $to_text);
2393 }
2394 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2395 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2396 return $line;
2397}
2398
2399# assumes that $from and $to are defined and correctly filled,
2400# and that $line holds a line of chunk header for combined diff
2401sub format_cc_diff_chunk_header {
2402 my ($line, $from, $to) = @_;
2403
2404 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2405 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2406
2407 @from_text = split(' ', $ranges);
2408 for (my $i = 0; $i < @from_text; ++$i) {
2409 ($from_start[$i], $from_nlines[$i]) =
2410 (split(',', substr($from_text[$i], 1)), 0);
2411 }
2412
2413 $to_text = pop @from_text;
2414 $to_start = pop @from_start;
2415 $to_nlines = pop @from_nlines;
2416
2417 $line = "<span class=\"chunk_info\">$prefix ";
2418 for (my $i = 0; $i < @from_text; ++$i) {
2419 if ($from->{'href'}[$i]) {
2420 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2421 -class=>"list"}, $from_text[$i]);
2422 } else {
2423 $line .= $from_text[$i];
2424 }
2425 $line .= " ";
2426 }
2427 if ($to->{'href'}) {
2428 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2429 -class=>"list"}, $to_text);
2430 } else {
2431 $line .= $to_text;
2432 }
2433 $line .= " $prefix</span>" .
2434 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2435 return $line;
2436}
2437
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01002438# process patch (diff) line (not to be used for diff headers),
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002439# returning HTML-formatted (but not wrapped) line.
2440# If the line is passed as a reference, it is treated as HTML and not
2441# esc_html()'ed.
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002442sub format_diff_line {
2443 my ($line, $diff_class, $from, $to) = @_;
Jakub Narebski20a864c2011-10-31 00:36:20 +01002444
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002445 if (ref($line)) {
2446 $line = $$line;
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002447 } else {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002448 chomp $line;
2449 $line = untabify($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02002450
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02002451 if ($from && $to && $line =~ m/^\@{2} /) {
2452 $line = format_unidiff_chunk_header($line, $from, $to);
2453 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2454 $line = format_cc_diff_chunk_header($line, $from, $to);
2455 } else {
2456 $line = esc_html($line, -nbsp=>1);
2457 }
Jakub Narebski59e3b142006-11-18 23:35:40 +01002458 }
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02002459
2460 my $diff_classes = "diff";
2461 $diff_classes .= " $diff_class" if ($diff_class);
2462 $line = "<div class=\"$diff_classes\">$line</div>\n";
2463
2464 return $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02002465}
2466
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002467# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2468# linked. Pass the hash of the tree/commit to snapshot.
2469sub format_snapshot_links {
2470 my ($hash) = @_;
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002471 my $num_fmts = @snapshot_fmts;
2472 if ($num_fmts > 1) {
2473 # A parenthesized list of links bearing format names.
Jakub Narebskia7817852007-07-22 23:41:20 +02002474 # e.g. "snapshot (_tar.gz_ _zip_)"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002475 return "snapshot (" . join(' ', map
2476 $cgi->a({
2477 -href => href(
2478 action=>"snapshot",
2479 hash=>$hash,
2480 snapshot_format=>$_
2481 )
2482 }, $known_snapshot_formats{$_}{'display'})
2483 , @snapshot_fmts) . ")";
2484 } elsif ($num_fmts == 1) {
2485 # A single "snapshot" link whose tooltip bears the format name.
Jakub Narebskia7817852007-07-22 23:41:20 +02002486 # i.e. "_snapshot_"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002487 my ($fmt) = @snapshot_fmts;
Jakub Narebskia7817852007-07-22 23:41:20 +02002488 return
2489 $cgi->a({
Matt McCutchena3c8ab32007-07-22 01:30:27 +02002490 -href => href(
2491 action=>"snapshot",
2492 hash=>$hash,
2493 snapshot_format=>$fmt
2494 ),
2495 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2496 }, "snapshot");
2497 } else { # $num_fmts == 0
2498 return undef;
2499 }
2500}
2501
Jakub Narebski35621982008-04-20 22:09:48 +02002502## ......................................................................
2503## functions returning values to be passed, perhaps after some
2504## transformation, to other functions; e.g. returning arguments to href()
2505
2506# returns hash to be passed to href to generate gitweb URL
2507# in -title key it returns description of link
2508sub get_feed_info {
2509 my $format = shift || 'Atom';
2510 my %res = (action => lc($format));
2511
2512 # feed links are possible only for project views
2513 return unless (defined $project);
2514 # some views should link to OPML, or to generic project feed,
2515 # or don't have specific feed yet (so they should use generic)
Jakub Narebski18ab83e2012-01-07 11:47:38 +01002516 return if (!$action || $action =~ /^(?:tags|heads|forks|tag|search)$/x);
Jakub Narebski35621982008-04-20 22:09:48 +02002517
2518 my $branch;
2519 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2520 # from tag links; this also makes possible to detect branch links
2521 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2522 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2523 $branch = $1;
2524 }
2525 # find log type for feed description (title)
2526 my $type = 'log';
2527 if (defined $file_name) {
2528 $type = "history of $file_name";
2529 $type .= "/" if ($action eq 'tree');
2530 $type .= " on '$branch'" if (defined $branch);
2531 } else {
2532 $type = "log of $branch" if (defined $branch);
2533 }
2534
2535 $res{-title} = $type;
2536 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2537 $res{'file_name'} = $file_name;
2538
2539 return %res;
2540}
2541
Jakub Narebski717b8312006-07-31 21:22:15 +02002542## ----------------------------------------------------------------------
2543## git utility subroutines, invoking git commands
2544
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002545# returns path to the core git executable and the --git-dir parameter as list
2546sub git_cmd {
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02002547 $number_of_git_cmds++;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002548 return $GIT, '--git-dir='.$git_dir;
2549}
2550
Lea Wiemann516381d2008-06-17 23:46:35 +02002551# quote the given arguments for passing them to the shell
2552# quote_command("command", "arg 1", "arg with ' and ! characters")
2553# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2554# Try to avoid using this function wherever possible.
2555sub quote_command {
2556 return join(' ',
Jakub Narebski68cedb12009-05-10 02:40:37 +02002557 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002558}
2559
Jakub Narebski717b8312006-07-31 21:22:15 +02002560# get HEAD ref of given project as hash
Jakub Narebski847e01f2006-08-14 02:05:47 +02002561sub git_get_head_hash {
Mark Radab6292752009-11-07 16:13:29 +01002562 return git_get_full_hash(shift, 'HEAD');
2563}
2564
2565sub git_get_full_hash {
2566 return git_get_hash(@_);
2567}
2568
2569sub git_get_short_hash {
2570 return git_get_hash(@_, '--short=7');
2571}
2572
2573sub git_get_hash {
2574 my ($project, $hash, @options) = @_;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002575 my $o_git_dir = $git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02002576 my $retval = undef;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002577 $git_dir = "$projectroot/$project";
Mark Radab6292752009-11-07 16:13:29 +01002578 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2579 '--verify', '-q', @options, $hash) {
2580 $retval = <$fd>;
2581 chomp $retval if defined $retval;
Jakub Narebski717b8312006-07-31 21:22:15 +02002582 close $fd;
Jakub Narebski717b8312006-07-31 21:22:15 +02002583 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002584 if (defined $o_git_dir) {
2585 $git_dir = $o_git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02002586 }
2587 return $retval;
2588}
2589
2590# get type of given object
2591sub git_get_type {
2592 my $hash = shift;
2593
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002594 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02002595 my $type = <$fd>;
2596 close $fd or return;
2597 chomp $type;
2598 return $type;
2599}
2600
Jakub Narebskib2019272007-11-03 00:41:19 +01002601# repository configuration
2602our $config_file = '';
2603our %config;
2604
2605# store multiple values for single key as anonymous array reference
2606# single values stored directly in the hash, not as [ <value> ]
2607sub hash_set_multi {
2608 my ($hash, $key, $value) = @_;
2609
2610 if (!exists $hash->{$key}) {
2611 $hash->{$key} = $value;
2612 } elsif (!ref $hash->{$key}) {
2613 $hash->{$key} = [ $hash->{$key}, $value ];
2614 } else {
2615 push @{$hash->{$key}}, $value;
2616 }
2617}
2618
2619# return hash of git project configuration
2620# optionally limited to some section, e.g. 'gitweb'
2621sub git_parse_project_config {
2622 my $section_regexp = shift;
2623 my %config;
2624
2625 local $/ = "\0";
2626
2627 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2628 or return;
2629
2630 while (my $keyval = <$fh>) {
2631 chomp $keyval;
2632 my ($key, $value) = split(/\n/, $keyval, 2);
2633
2634 hash_set_multi(\%config, $key, $value)
2635 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2636 }
2637 close $fh;
2638
2639 return %config;
2640}
2641
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002642# convert config value to boolean: 'true' or 'false'
Jakub Narebskib2019272007-11-03 00:41:19 +01002643# no value, number > 0, 'true' and 'yes' values are true
2644# rest of values are treated as false (never as error)
2645sub config_to_bool {
2646 my $val = shift;
2647
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002648 return 1 if !defined $val; # section.key
2649
Jakub Narebskib2019272007-11-03 00:41:19 +01002650 # strip leading and trailing whitespace
2651 $val =~ s/^\s+//;
2652 $val =~ s/\s+$//;
2653
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002654 return (($val =~ /^\d+$/ && $val) || # section.key = 1
Jakub Narebskib2019272007-11-03 00:41:19 +01002655 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2656}
2657
2658# convert config value to simple decimal number
2659# an optional value suffix of 'k', 'm', or 'g' will cause the value
2660# to be multiplied by 1024, 1048576, or 1073741824
2661sub config_to_int {
2662 my $val = shift;
2663
2664 # strip leading and trailing whitespace
2665 $val =~ s/^\s+//;
2666 $val =~ s/\s+$//;
2667
2668 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2669 $unit = lc($unit);
2670 # unknown unit is treated as 1
2671 return $num * ($unit eq 'g' ? 1073741824 :
2672 $unit eq 'm' ? 1048576 :
2673 $unit eq 'k' ? 1024 : 1);
2674 }
2675 return $val;
2676}
2677
2678# convert config value to array reference, if needed
2679sub config_to_multi {
2680 my $val = shift;
2681
Jakub Narebskid76a5852007-12-20 10:48:09 +01002682 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
Jakub Narebskib2019272007-11-03 00:41:19 +01002683}
2684
Jakub Narebski717b8312006-07-31 21:22:15 +02002685sub git_get_project_config {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302686 my ($key, $type) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02002687
Jakub Narebski7a49c252010-03-27 20:26:59 +01002688 return unless defined $git_dir;
Jakub Narebski9be36142010-03-01 22:51:34 +01002689
Jakub Narebskib2019272007-11-03 00:41:19 +01002690 # key sanity check
Jakub Narebski717b8312006-07-31 21:22:15 +02002691 return unless ($key);
Jakub Narebski14569cd2011-07-28 23:38:03 +02002692 # only subsection, if exists, is case sensitive,
2693 # and not lowercased by 'git config -z -l'
2694 if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
2695 $key = join(".", lc($hi), $mi, lc($lo));
2696 } else {
2697 $key = lc($key);
2698 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002699 $key =~ s/^gitweb\.//;
2700 return if ($key =~ m/\W/);
2701
Jakub Narebskib2019272007-11-03 00:41:19 +01002702 # type sanity check
2703 if (defined $type) {
2704 $type =~ s/^--//;
2705 $type = undef
2706 unless ($type eq 'bool' || $type eq 'int');
2707 }
2708
2709 # get config
2710 if (!defined $config_file ||
2711 $config_file ne "$git_dir/config") {
2712 %config = git_parse_project_config('gitweb');
2713 $config_file = "$git_dir/config";
2714 }
2715
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01002716 # check if config variable (key) exists
2717 return unless exists $config{"gitweb.$key"};
2718
Jakub Narebskib2019272007-11-03 00:41:19 +01002719 # ensure given type
2720 if (!defined $type) {
2721 return $config{"gitweb.$key"};
2722 } elsif ($type eq 'bool') {
2723 # backward compatibility: 'git config --bool' returns true/false
2724 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2725 } elsif ($type eq 'int') {
2726 return config_to_int($config{"gitweb.$key"});
2727 }
2728 return $config{"gitweb.$key"};
Jakub Narebski717b8312006-07-31 21:22:15 +02002729}
2730
Jakub Narebski717b8312006-07-31 21:22:15 +02002731# get hash of given path at given ref
2732sub git_get_hash_by_path {
2733 my $base = shift;
2734 my $path = shift || return undef;
Jakub Narebski1d782b02006-09-21 18:09:12 +02002735 my $type = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02002736
Jakub Narebski4b02f482006-09-26 01:54:24 +02002737 $path =~ s,/+$,,;
Jakub Narebski717b8312006-07-31 21:22:15 +02002738
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002739 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
Lea Wiemann074afaa2008-06-19 22:03:21 +02002740 or die_error(500, "Open git-ls-tree failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02002741 my $line = <$fd>;
2742 close $fd or return undef;
2743
Jakub Narebski198a2a82007-05-12 21:16:34 +02002744 if (!defined $line) {
2745 # there is no tree or hash given by $path at $base
2746 return undef;
2747 }
2748
Jakub Narebski717b8312006-07-31 21:22:15 +02002749 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01002750 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
Jakub Narebski1d782b02006-09-21 18:09:12 +02002751 if (defined $type && $type ne $2) {
2752 # type doesn't match
2753 return undef;
2754 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002755 return $3;
2756}
2757
Jakub Narebskied224de2007-05-07 01:10:04 +02002758# get path of entry with given hash at given tree-ish (ref)
2759# used to get 'from' filename for combined diff (merge commit) for renames
2760sub git_get_path_by_hash {
2761 my $base = shift || return;
2762 my $hash = shift || return;
2763
2764 local $/ = "\0";
2765
2766 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2767 or return undef;
2768 while (my $line = <$fd>) {
2769 chomp $line;
2770
2771 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2772 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2773 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2774 close $fd;
2775 return $1;
2776 }
2777 }
2778 close $fd;
2779 return undef;
2780}
2781
Jakub Narebski717b8312006-07-31 21:22:15 +02002782## ......................................................................
2783## git utility functions, directly accessing git repository
2784
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002785# get the value of config variable either from file named as the variable
2786# itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2787# configuration variable in the repository config file.
2788sub git_get_file_or_project_config {
2789 my ($path, $name) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02002790
Jakub Narebski0e121a22007-11-03 00:41:20 +01002791 $git_dir = "$projectroot/$path";
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002792 open my $fd, '<', "$git_dir/$name"
2793 or return git_get_project_config($name);
2794 my $conf = <$fd>;
Jakub Narebski717b8312006-07-31 21:22:15 +02002795 close $fd;
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002796 if (defined $conf) {
2797 chomp $conf;
Junio C Hamano2eb54ef2007-05-16 21:04:16 -07002798 }
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002799 return $conf;
Jakub Narebski717b8312006-07-31 21:22:15 +02002800}
2801
Sebastien Ceveye4e3b322011-04-29 19:52:00 +02002802sub git_get_project_description {
2803 my $path = shift;
2804 return git_get_file_or_project_config($path, 'description');
Jakub Narebski717b8312006-07-31 21:22:15 +02002805}
2806
Sebastien Ceveyd940c902011-04-29 19:52:01 +02002807sub git_get_project_category {
2808 my $path = shift;
2809 return git_get_file_or_project_config($path, 'category');
2810}
2811
2812
Jakub Narebski0368c492011-04-29 19:51:57 +02002813# supported formats:
2814# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2815# - if its contents is a number, use it as tag weight,
2816# - otherwise add a tag with weight 1
2817# * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2818# the same value multiple times increases tag weight
2819# * `gitweb.ctag' multi-valued repo config variable
Petr Baudisaed93de2008-10-02 17:13:02 +02002820sub git_get_project_ctags {
Jakub Narebski0368c492011-04-29 19:51:57 +02002821 my $project = shift;
Petr Baudisaed93de2008-10-02 17:13:02 +02002822 my $ctags = {};
2823
Jakub Narebski0368c492011-04-29 19:51:57 +02002824 $git_dir = "$projectroot/$project";
2825 if (opendir my $dh, "$git_dir/ctags") {
2826 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2827 foreach my $tagfile (@files) {
2828 open my $ct, '<', $tagfile
2829 or next;
2830 my $val = <$ct>;
2831 chomp $val if $val;
2832 close $ct;
2833
2834 (my $ctag = $tagfile) =~ s#.*/##;
Jonathan Nieder2c162b52011-06-09 02:08:57 -05002835 if ($val =~ /^\d+$/) {
Jakub Narebski0368c492011-04-29 19:51:57 +02002836 $ctags->{$ctag} = $val;
2837 } else {
2838 $ctags->{$ctag} = 1;
2839 }
2840 }
2841 closedir $dh;
2842
2843 } elsif (open my $fh, '<', "$git_dir/ctags") {
2844 while (my $line = <$fh>) {
2845 chomp $line;
2846 $ctags->{$line}++ if $line;
2847 }
2848 close $fh;
2849
2850 } else {
2851 my $taglist = config_to_multi(git_get_project_config('ctag'));
2852 foreach my $tag (@$taglist) {
2853 $ctags->{$tag}++;
2854 }
Petr Baudisaed93de2008-10-02 17:13:02 +02002855 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002856
2857 return $ctags;
2858}
2859
2860# return hash, where keys are content tags ('ctags'),
2861# and values are sum of weights of given tag in every project
2862sub git_gather_all_ctags {
2863 my $projects = shift;
2864 my $ctags = {};
2865
2866 foreach my $p (@$projects) {
2867 foreach my $ct (keys %{$p->{'ctags'}}) {
2868 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2869 }
2870 }
2871
2872 return $ctags;
Petr Baudisaed93de2008-10-02 17:13:02 +02002873}
2874
2875sub git_populate_project_tagcloud {
2876 my $ctags = shift;
2877
2878 # First, merge different-cased tags; tags vote on casing
2879 my %ctags_lc;
2880 foreach (keys %$ctags) {
2881 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2882 if (not $ctags_lc{lc $_}->{topcount}
2883 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2884 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2885 $ctags_lc{lc $_}->{topname} = $_;
2886 }
2887 }
2888
2889 my $cloud;
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01002890 my $matched = $input_params{'ctag'};
Petr Baudisaed93de2008-10-02 17:13:02 +02002891 if (eval { require HTML::TagCloud; 1; }) {
2892 $cloud = HTML::TagCloud->new;
Jakub Narebski0368c492011-04-29 19:51:57 +02002893 foreach my $ctag (sort keys %ctags_lc) {
Petr Baudisaed93de2008-10-02 17:13:02 +02002894 # Pad the title with spaces so that the cloud looks
2895 # less crammed.
Jakub Narebski0368c492011-04-29 19:51:57 +02002896 my $title = esc_html($ctags_lc{$ctag}->{topname});
Petr Baudisaed93de2008-10-02 17:13:02 +02002897 $title =~ s/ /&nbsp;/g;
2898 $title =~ s/^/&nbsp;/g;
2899 $title =~ s/$/&nbsp;/g;
Jakub Narebski4b9447f2011-04-29 19:51:58 +02002900 if (defined $matched && $matched eq $ctag) {
2901 $title = qq(<span class="match">$title</span>);
2902 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002903 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2904 $ctags_lc{$ctag}->{count});
Petr Baudisaed93de2008-10-02 17:13:02 +02002905 }
2906 } else {
Jakub Narebski0368c492011-04-29 19:51:57 +02002907 $cloud = {};
2908 foreach my $ctag (keys %ctags_lc) {
Jakub Narebski4b9447f2011-04-29 19:51:58 +02002909 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
2910 if (defined $matched && $matched eq $ctag) {
2911 $title = qq(<span class="match">$title</span>);
2912 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002913 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
2914 $cloud->{$ctag}{ctag} =
Jakub Narebski4b9447f2011-04-29 19:51:58 +02002915 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
Jakub Narebski0368c492011-04-29 19:51:57 +02002916 }
Petr Baudisaed93de2008-10-02 17:13:02 +02002917 }
Jakub Narebski0368c492011-04-29 19:51:57 +02002918 return $cloud;
Petr Baudisaed93de2008-10-02 17:13:02 +02002919}
2920
2921sub git_show_project_tagcloud {
2922 my ($cloud, $count) = @_;
Petr Baudisaed93de2008-10-02 17:13:02 +02002923 if (ref $cloud eq 'HTML::TagCloud') {
2924 return $cloud->html_and_css($count);
2925 } else {
Jakub Narebski0368c492011-04-29 19:51:57 +02002926 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2927 return
2928 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2929 join (', ', map {
2930 $cloud->{$_}->{'ctag'}
2931 } splice(@tags, 0, $count)) .
2932 '</div>';
Petr Baudisaed93de2008-10-02 17:13:02 +02002933 }
2934}
2935
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002936sub git_get_project_url_list {
2937 my $path = shift;
2938
Jakub Narebski0e121a22007-11-03 00:41:20 +01002939 $git_dir = "$projectroot/$path";
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02002940 open my $fd, '<', "$git_dir/cloneurl"
Jakub Narebski0e121a22007-11-03 00:41:20 +01002941 or return wantarray ?
2942 @{ config_to_multi(git_get_project_config('url')) } :
2943 config_to_multi(git_get_project_config('url'));
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002944 my @git_project_url_list = map { chomp; $_ } <$fd>;
2945 close $fd;
2946
2947 return wantarray ? @git_project_url_list : \@git_project_url_list;
2948}
2949
Jakub Narebski847e01f2006-08-14 02:05:47 +02002950sub git_get_projects_list {
Jakub Narebski12b14432011-04-29 19:51:56 +02002951 my $filter = shift || '';
Bernhard R. Link348a6582012-01-30 21:06:38 +01002952 my $paranoid = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02002953 my @list;
2954
2955 if (-d $projects_list) {
2956 # search in directory
Jakub Narebski12b14432011-04-29 19:51:56 +02002957 my $dir = $projects_list;
Aneesh Kumar K.V6768d6b2006-11-03 10:41:45 +05302958 # remove the trailing "/"
2959 $dir =~ s!/+$!!;
Matthieu Moyac593b72012-01-04 11:07:45 +01002960 my $pfxlen = length("$dir");
2961 my $pfxdepth = ($dir =~ tr!/!!);
Jakub Narebski12b14432011-04-29 19:51:56 +02002962 # when filtering, search only given subdirectory
Bernhard R. Link348a6582012-01-30 21:06:38 +01002963 if ($filter && !$paranoid) {
Jakub Narebski12b14432011-04-29 19:51:56 +02002964 $dir .= "/$filter";
2965 $dir =~ s!/+$!!;
2966 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002967
2968 File::Find::find({
2969 follow_fast => 1, # follow symbolic links
Junio C Hamanod20602e2007-07-27 01:23:03 -07002970 follow_skip => 2, # ignore duplicates
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002971 dangling_symlinks => 0, # ignore dangling symlinks, silently
2972 wanted => sub {
Jakub Narebskiee1d8ee2010-04-30 18:30:31 +02002973 # global variables
2974 our $project_maxdepth;
2975 our $projectroot;
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002976 # skip project-list toplevel, if we get it.
2977 return if (m!^[/.]$!);
2978 # only directories can be git repositories
2979 return unless (-d $_);
Luke Luca5e9492007-10-16 20:45:25 -07002980 # don't traverse too deep (Find is super slow on os x)
Jakub Narebski12b14432011-04-29 19:51:56 +02002981 # $project_maxdepth excludes depth of $projectroot
Luke Luca5e9492007-10-16 20:45:25 -07002982 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2983 $File::Find::prune = 1;
2984 return;
2985 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002986
Jakub Narebski12b14432011-04-29 19:51:56 +02002987 my $path = substr($File::Find::name, $pfxlen + 1);
Bernhard R. Link348a6582012-01-30 21:06:38 +01002988 # paranoidly only filter here
2989 if ($paranoid && $filter && $path !~ m!^\Q$filter\E/!) {
2990 next;
2991 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002992 # we check related file in $projectroot
Devin Doucettefb3bb3d2008-12-27 02:39:31 -07002993 if (check_export_ok("$projectroot/$path")) {
2994 push @list, { path => $path };
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002995 $File::Find::prune = 1;
2996 }
2997 },
2998 }, "$dir");
2999
Jakub Narebski717b8312006-07-31 21:22:15 +02003000 } elsif (-f $projects_list) {
3001 # read from file(url-encoded):
3002 # 'git%2Fgit.git Linus+Torvalds'
3003 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3004 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003005 open my $fd, '<', $projects_list or return;
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02003006 PROJECT:
Jakub Narebski717b8312006-07-31 21:22:15 +02003007 while (my $line = <$fd>) {
3008 chomp $line;
3009 my ($path, $owner) = split ' ', $line;
3010 $path = unescape($path);
3011 $owner = unescape($owner);
3012 if (!defined $path) {
3013 next;
3014 }
Jakub Narebski12b14432011-04-29 19:51:56 +02003015 # if $filter is rpovided, check if $path begins with $filter
3016 if ($filter && $path !~ m!^\Q$filter\E/!) {
3017 next;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003018 }
Junio C Hamano2172ce42006-10-03 02:30:47 -07003019 if (check_export_ok("$projectroot/$path")) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003020 my $pr = {
Kacper Kornet75e0dff2012-04-24 19:50:05 +02003021 path => $path
Jakub Narebski717b8312006-07-31 21:22:15 +02003022 };
Kacper Kornet75e0dff2012-04-24 19:50:05 +02003023 if ($owner) {
3024 $pr->{'owner'} = to_utf8($owner);
3025 }
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02003026 push @list, $pr;
Jakub Narebski717b8312006-07-31 21:22:15 +02003027 }
3028 }
3029 close $fd;
3030 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003031 return @list;
3032}
3033
Jakub Narebski12b14432011-04-29 19:51:56 +02003034# written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
3035# as side effects it sets 'forks' field to list of forks for forked projects
3036sub filter_forks_from_projects_list {
3037 my $projects = shift;
3038
3039 my %trie; # prefix tree of directories (path components)
3040 # generate trie out of those directories that might contain forks
3041 foreach my $pr (@$projects) {
3042 my $path = $pr->{'path'};
3043 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
3044 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
3045 next unless ($path); # skip '.git' repository: tests, git-instaweb
Julien Muchembled53c632f2011-10-21 21:04:21 +02003046 next unless (-d "$projectroot/$path"); # containing directory exists
Jakub Narebski12b14432011-04-29 19:51:56 +02003047 $pr->{'forks'} = []; # there can be 0 or more forks of project
3048
3049 # add to trie
3050 my @dirs = split('/', $path);
3051 # walk the trie, until either runs out of components or out of trie
3052 my $ref = \%trie;
3053 while (scalar @dirs &&
3054 exists($ref->{$dirs[0]})) {
3055 $ref = $ref->{shift @dirs};
3056 }
3057 # create rest of trie structure from rest of components
3058 foreach my $dir (@dirs) {
3059 $ref = $ref->{$dir} = {};
3060 }
3061 # create end marker, store $pr as a data
3062 $ref->{''} = $pr if (!exists $ref->{''});
3063 }
3064
3065 # filter out forks, by finding shortest prefix match for paths
3066 my @filtered;
3067 PROJECT:
3068 foreach my $pr (@$projects) {
3069 # trie lookup
3070 my $ref = \%trie;
3071 DIR:
3072 foreach my $dir (split('/', $pr->{'path'})) {
3073 if (exists $ref->{''}) {
3074 # found [shortest] prefix, is a fork - skip it
3075 push @{$ref->{''}{'forks'}}, $pr;
3076 next PROJECT;
3077 }
3078 if (!exists $ref->{$dir}) {
3079 # not in trie, cannot have prefix, not a fork
3080 push @filtered, $pr;
3081 next PROJECT;
3082 }
3083 # If the dir is there, we just walk one step down the trie.
3084 $ref = $ref->{$dir};
3085 }
3086 # we ran out of trie
3087 # (shouldn't happen: it's either no match, or end marker)
3088 push @filtered, $pr;
3089 }
3090
3091 return @filtered;
3092}
3093
3094# note: fill_project_list_info must be run first,
3095# for 'descr_long' and 'ctags' to be filled
3096sub search_projects_list {
3097 my ($projlist, %opts) = @_;
3098 my $tagfilter = $opts{'tagfilter'};
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003099 my $search_re = $opts{'search_regexp'};
Jakub Narebski12b14432011-04-29 19:51:56 +02003100
3101 return @$projlist
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003102 unless ($tagfilter || $search_re);
Jakub Narebski12b14432011-04-29 19:51:56 +02003103
Jakub Narebski07b257f2012-02-23 16:54:48 +01003104 # searching projects require filling to be run before it;
3105 fill_project_list_info($projlist,
3106 $tagfilter ? 'ctags' : (),
Junio C Hamanoaa145bf2012-03-08 13:04:49 -08003107 $search_re ? ('path', 'descr') : ());
Jakub Narebski12b14432011-04-29 19:51:56 +02003108 my @projects;
3109 PROJECT:
3110 foreach my $pr (@$projlist) {
3111
3112 if ($tagfilter) {
3113 next unless ref($pr->{'ctags'}) eq 'HASH';
3114 next unless
3115 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
3116 }
3117
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003118 if ($search_re) {
Jakub Narebski12b14432011-04-29 19:51:56 +02003119 next unless
Jakub Narebskie65ceb62012-03-02 23:34:24 +01003120 $pr->{'path'} =~ /$search_re/ ||
3121 $pr->{'descr_long'} =~ /$search_re/;
Jakub Narebski12b14432011-04-29 19:51:56 +02003122 }
3123
3124 push @projects, $pr;
3125 }
3126
3127 return @projects;
3128}
3129
Junio C Hamano47852452007-07-03 22:10:42 -07003130our $gitweb_project_owner = undef;
3131sub git_get_project_list_from_file {
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003132
Junio C Hamano47852452007-07-03 22:10:42 -07003133 return if (defined $gitweb_project_owner);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003134
Junio C Hamano47852452007-07-03 22:10:42 -07003135 $gitweb_project_owner = {};
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003136 # read from file (url-encoded):
3137 # 'git%2Fgit.git Linus+Torvalds'
3138 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
3139 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
3140 if (-f $projects_list) {
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003141 open(my $fd, '<', $projects_list);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003142 while (my $line = <$fd>) {
3143 chomp $line;
3144 my ($pr, $ow) = split ' ', $line;
3145 $pr = unescape($pr);
3146 $ow = unescape($ow);
Junio C Hamano47852452007-07-03 22:10:42 -07003147 $gitweb_project_owner->{$pr} = to_utf8($ow);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003148 }
3149 close $fd;
3150 }
Junio C Hamano47852452007-07-03 22:10:42 -07003151}
3152
3153sub git_get_project_owner {
3154 my $project = shift;
3155 my $owner;
3156
3157 return undef unless $project;
Bruno Ribasb59012e2008-02-08 14:38:04 -02003158 $git_dir = "$projectroot/$project";
Junio C Hamano47852452007-07-03 22:10:42 -07003159
3160 if (!defined $gitweb_project_owner) {
3161 git_get_project_list_from_file();
3162 }
3163
3164 if (exists $gitweb_project_owner->{$project}) {
3165 $owner = $gitweb_project_owner->{$project};
3166 }
Bruno Ribasb59012e2008-02-08 14:38:04 -02003167 if (!defined $owner){
3168 $owner = git_get_project_config('owner');
3169 }
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003170 if (!defined $owner) {
Bruno Ribasb59012e2008-02-08 14:38:04 -02003171 $owner = get_file_owner("$git_dir");
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003172 }
3173
3174 return $owner;
3175}
3176
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003177sub git_get_last_activity {
3178 my ($path) = @_;
3179 my $fd;
3180
3181 $git_dir = "$projectroot/$path";
3182 open($fd, "-|", git_cmd(), 'for-each-ref',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00003183 '--format=%(committer)',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003184 '--sort=-committerdate',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00003185 '--count=1',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003186 'refs/heads') or return;
3187 my $most_recent = <$fd>;
3188 close $fd or return;
Jakub Narebski785cdea2007-05-13 12:39:22 +02003189 if (defined $most_recent &&
3190 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003191 my $timestamp = $1;
3192 my $age = time - $timestamp;
3193 return ($age, age_string($age));
3194 }
Matt McCutchenc9563952007-06-28 18:15:22 -04003195 return (undef, undef);
Jakub Narebskic60c56c2006-10-28 19:43:40 +02003196}
3197
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01003198# Implementation note: when a single remote is wanted, we cannot use 'git
3199# remote show -n' because that command always work (assuming it's a remote URL
3200# if it's not defined), and we cannot use 'git remote show' because that would
3201# try to make a network roundtrip. So the only way to find if that particular
3202# remote is defined is to walk the list provided by 'git remote -v' and stop if
3203# and when we find what we want.
3204sub git_get_remotes_list {
3205 my $wanted = shift;
3206 my %remotes = ();
3207
3208 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3209 return unless $fd;
3210 while (my $remote = <$fd>) {
3211 chomp $remote;
3212 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3213 next if $wanted and not $remote eq $wanted;
3214 my ($url, $key) = ($1, $2);
3215
3216 $remotes{$remote} ||= { 'heads' => () };
3217 $remotes{$remote}{$key} = $url;
3218 }
3219 close $fd or return;
3220 return wantarray ? %remotes : \%remotes;
3221}
3222
3223# Takes a hash of remotes as first parameter and fills it by adding the
3224# available remote heads for each of the indicated remotes.
3225sub fill_remote_heads {
3226 my $remotes = shift;
3227 my @heads = map { "remotes/$_" } keys %$remotes;
3228 my @remoteheads = git_get_heads_list(undef, @heads);
3229 foreach my $remote (keys %$remotes) {
3230 $remotes->{$remote}{'heads'} = [ grep {
3231 $_->{'name'} =~ s!^$remote/!!
3232 } @remoteheads ];
3233 }
3234}
3235
Jakub Narebski847e01f2006-08-14 02:05:47 +02003236sub git_get_references {
Jakub Narebski717b8312006-07-31 21:22:15 +02003237 my $type = shift || "";
3238 my %refs;
Jakub Narebski28b9d9f2006-11-25 11:32:08 +01003239 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3240 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3241 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3242 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
Jakub Narebski9704d752006-09-19 14:31:49 +02003243 or return;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003244
Jakub Narebski717b8312006-07-31 21:22:15 +02003245 while (my $line = <$fd>) {
3246 chomp $line;
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02003247 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003248 if (defined $refs{$1}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003249 push @{$refs{$1}}, $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02003250 } else {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02003251 $refs{$1} = [ $2 ];
Jakub Narebski717b8312006-07-31 21:22:15 +02003252 }
3253 }
3254 }
3255 close $fd or return;
3256 return \%refs;
3257}
3258
Jakub Narebski56a322f2006-08-24 19:41:23 +02003259sub git_get_rev_name_tags {
3260 my $hash = shift || return undef;
3261
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003262 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
Jakub Narebski56a322f2006-08-24 19:41:23 +02003263 or return;
3264 my $name_rev = <$fd>;
3265 close $fd;
3266
3267 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3268 return $1;
3269 } else {
3270 # catches also '$hash undefined' output
3271 return undef;
3272 }
3273}
3274
Jakub Narebski717b8312006-07-31 21:22:15 +02003275## ----------------------------------------------------------------------
3276## parse to hash functions
3277
Jakub Narebski847e01f2006-08-14 02:05:47 +02003278sub parse_date {
Jakub Narebski717b8312006-07-31 21:22:15 +02003279 my $epoch = shift;
3280 my $tz = shift || "-0000";
3281
3282 my %date;
3283 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3284 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3285 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3286 $date{'hour'} = $hour;
3287 $date{'minute'} = $min;
3288 $date{'mday'} = $mday;
3289 $date{'day'} = $days[$wday];
3290 $date{'month'} = $months[$mon];
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003291 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3292 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
Jakub Narebski952c65f2006-08-22 16:52:50 +02003293 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3294 $mday, $months[$mon], $hour ,$min;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003295 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
Vincent Zanottia62d6d82007-11-10 19:55:27 +01003296 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
Jakub Narebski717b8312006-07-31 21:22:15 +02003297
Jakub Narebski2b1e1722011-03-25 20:20:49 +01003298 my ($tz_sign, $tz_hour, $tz_min) =
3299 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3300 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3301 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
Jakub Narebski717b8312006-07-31 21:22:15 +02003302 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3303 $date{'hour_local'} = $hour;
3304 $date{'minute_local'} = $min;
3305 $date{'tz_local'} = $tz;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01003306 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3307 1900+$year, $mon+1, $mday,
3308 $hour, $min, $sec, $tz);
Jakub Narebski717b8312006-07-31 21:22:15 +02003309 return %date;
3310}
3311
Jakub Narebski847e01f2006-08-14 02:05:47 +02003312sub parse_tag {
Jakub Narebski717b8312006-07-31 21:22:15 +02003313 my $tag_id = shift;
3314 my %tag;
3315 my @comment;
3316
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003317 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02003318 $tag{'id'} = $tag_id;
3319 while (my $line = <$fd>) {
3320 chomp $line;
3321 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3322 $tag{'object'} = $1;
3323 } elsif ($line =~ m/^type (.+)$/) {
3324 $tag{'type'} = $1;
3325 } elsif ($line =~ m/^tag (.+)$/) {
3326 $tag{'name'} = $1;
3327 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3328 $tag{'author'} = $1;
Giuseppe Bilottaba924732009-06-30 00:00:50 +02003329 $tag{'author_epoch'} = $2;
3330 $tag{'author_tz'} = $3;
3331 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3332 $tag{'author_name'} = $1;
3333 $tag{'author_email'} = $2;
3334 } else {
3335 $tag{'author_name'} = $tag{'author'};
3336 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003337 } elsif ($line =~ m/--BEGIN/) {
3338 push @comment, $line;
3339 last;
3340 } elsif ($line eq "") {
3341 last;
3342 }
3343 }
3344 push @comment, <$fd>;
3345 $tag{'comment'} = \@comment;
3346 close $fd or return;
3347 if (!defined $tag{'name'}) {
3348 return
3349 };
3350 return %tag
3351}
3352
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003353sub parse_commit_text {
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003354 my ($commit_text, $withparents) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003355 my @commit_lines = split '\n', $commit_text;
Jakub Narebski717b8312006-07-31 21:22:15 +02003356 my %co;
3357
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003358 pop @commit_lines; # Remove '\0'
3359
Jakub Narebski198a2a82007-05-12 21:16:34 +02003360 if (! @commit_lines) {
3361 return;
3362 }
3363
Jakub Narebski717b8312006-07-31 21:22:15 +02003364 my $header = shift @commit_lines;
Jakub Narebski198a2a82007-05-12 21:16:34 +02003365 if ($header !~ m/^[0-9a-fA-F]{40}/) {
Jakub Narebski717b8312006-07-31 21:22:15 +02003366 return;
3367 }
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003368 ($co{'id'}, my @parents) = split ' ', $header;
Jakub Narebski717b8312006-07-31 21:22:15 +02003369 while (my $line = shift @commit_lines) {
3370 last if $line eq "\n";
3371 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3372 $co{'tree'} = $1;
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003373 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00003374 push @parents, $1;
Jakub Narebski717b8312006-07-31 21:22:15 +02003375 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
Zoltán Füzesi5ed5bbc2009-08-02 09:42:24 +02003376 $co{'author'} = to_utf8($1);
Jakub Narebski717b8312006-07-31 21:22:15 +02003377 $co{'author_epoch'} = $2;
3378 $co{'author_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01003379 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3380 $co{'author_name'} = $1;
3381 $co{'author_email'} = $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02003382 } else {
3383 $co{'author_name'} = $co{'author'};
3384 }
3385 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
Zoltán Füzesi5ed5bbc2009-08-02 09:42:24 +02003386 $co{'committer'} = to_utf8($1);
Jakub Narebski717b8312006-07-31 21:22:15 +02003387 $co{'committer_epoch'} = $2;
3388 $co{'committer_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01003389 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3390 $co{'committer_name'} = $1;
3391 $co{'committer_email'} = $2;
3392 } else {
3393 $co{'committer_name'} = $co{'committer'};
3394 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003395 }
3396 }
3397 if (!defined $co{'tree'}) {
3398 return;
3399 };
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00003400 $co{'parents'} = \@parents;
3401 $co{'parent'} = $parents[0];
Jakub Narebski717b8312006-07-31 21:22:15 +02003402
3403 foreach my $title (@commit_lines) {
3404 $title =~ s/^ //;
3405 if ($title ne "") {
3406 $co{'title'} = chop_str($title, 80, 5);
3407 # remove leading stuff of merges to make the interesting part visible
3408 if (length($title) > 50) {
3409 $title =~ s/^Automatic //;
3410 $title =~ s/^merge (of|with) /Merge ... /i;
3411 if (length($title) > 50) {
3412 $title =~ s/(http|rsync):\/\///;
3413 }
3414 if (length($title) > 50) {
3415 $title =~ s/(master|www|rsync)\.//;
3416 }
3417 if (length($title) > 50) {
3418 $title =~ s/kernel.org:?//;
3419 }
3420 if (length($title) > 50) {
3421 $title =~ s/\/pub\/scm//;
3422 }
3423 }
3424 $co{'title_short'} = chop_str($title, 50, 5);
3425 last;
3426 }
3427 }
Joey Hess53c39672008-09-05 14:26:29 -04003428 if (! defined $co{'title'} || $co{'title'} eq "") {
Petr Baudis7e0fe5c2006-10-06 18:55:04 +02003429 $co{'title'} = $co{'title_short'} = '(no commit message)';
3430 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003431 # remove added spaces
3432 foreach my $line (@commit_lines) {
3433 $line =~ s/^ //;
3434 }
3435 $co{'comment'} = \@commit_lines;
3436
3437 my $age = time - $co{'committer_epoch'};
3438 $co{'age'} = $age;
3439 $co{'age_string'} = age_string($age);
3440 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3441 if ($age > 60*60*24*7*2) {
3442 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3443 $co{'age_string_age'} = $co{'age_string'};
3444 } else {
3445 $co{'age_string_date'} = $co{'age_string'};
3446 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3447 }
3448 return %co;
3449}
3450
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003451sub parse_commit {
3452 my ($commit_id) = @_;
3453 my %co;
3454
3455 local $/ = "\0";
3456
3457 open my $fd, "-|", git_cmd(), "rev-list",
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003458 "--parents",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003459 "--header",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003460 "--max-count=1",
3461 $commit_id,
3462 "--",
Lea Wiemann074afaa2008-06-19 22:03:21 +02003463 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00003464 %co = parse_commit_text(<$fd>, 1);
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003465 close $fd;
3466
3467 return %co;
3468}
3469
3470sub parse_commits {
Jakub Narebski311e5522008-02-26 13:22:06 +01003471 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003472 my @cos;
3473
3474 $maxcount ||= 1;
3475 $skip ||= 0;
3476
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003477 local $/ = "\0";
3478
3479 open my $fd, "-|", git_cmd(), "rev-list",
3480 "--header",
Jakub Narebski311e5522008-02-26 13:22:06 +01003481 @args,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003482 ("--max-count=" . $maxcount),
Robert Fitzsimonsf47efbb2006-12-24 14:31:49 +00003483 ("--skip=" . $skip),
Miklos Vajna868bc062007-07-12 20:39:27 +02003484 @extra_options,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003485 $commit_id,
3486 "--",
3487 ($filename ? ($filename) : ())
Lea Wiemann074afaa2008-06-19 22:03:21 +02003488 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00003489 while (my $line = <$fd>) {
3490 my %co = parse_commit_text($line);
3491 push @cos, \%co;
3492 }
3493 close $fd;
3494
3495 return wantarray ? @cos : \@cos;
3496}
3497
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003498# parse line of git-diff-tree "raw" output
Jakub Narebski740e67f2006-08-21 23:07:00 +02003499sub parse_difftree_raw_line {
3500 my $line = shift;
3501 my %res;
3502
3503 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3504 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3505 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3506 $res{'from_mode'} = $1;
3507 $res{'to_mode'} = $2;
3508 $res{'from_id'} = $3;
3509 $res{'to_id'} = $4;
Jakub Narebski4ed4a342008-04-05 21:13:24 +01003510 $res{'status'} = $5;
Jakub Narebski740e67f2006-08-21 23:07:00 +02003511 $res{'similarity'} = $6;
3512 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003513 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02003514 } else {
Jakub Narebski9d301452007-11-01 12:38:08 +01003515 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02003516 }
3517 }
Jakub Narebski78bc4032007-05-07 01:10:03 +02003518 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3519 # combined diff (for merge commit)
3520 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3521 $res{'nparents'} = length($1);
3522 $res{'from_mode'} = [ split(' ', $2) ];
3523 $res{'to_mode'} = pop @{$res{'from_mode'}};
3524 $res{'from_id'} = [ split(' ', $3) ];
3525 $res{'to_id'} = pop @{$res{'from_id'}};
3526 $res{'status'} = [ split('', $4) ];
3527 $res{'to_file'} = unquote($5);
3528 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02003529 # 'c512b523472485aef4fff9e57b229d9d243c967f'
Jakub Narebski0edcb372006-08-31 00:36:04 +02003530 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3531 $res{'commit'} = $1;
3532 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02003533
3534 return wantarray ? %res : \%res;
3535}
3536
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003537# wrapper: return parsed line of git-diff-tree "raw" output
3538# (the argument might be raw line, or parsed info)
3539sub parsed_difftree_line {
3540 my $line_or_ref = shift;
3541
3542 if (ref($line_or_ref) eq "HASH") {
3543 # pre-parsed (or generated by hand)
3544 return $line_or_ref;
3545 } else {
3546 return parse_difftree_raw_line($line_or_ref);
3547 }
3548}
3549
Jakub Narebskicb849b42006-08-31 00:32:15 +02003550# parse line of git-ls-tree output
Jakub Narebski74fd8722009-05-07 19:11:29 +02003551sub parse_ls_tree_line {
Jakub Narebskicb849b42006-08-31 00:32:15 +02003552 my $line = shift;
3553 my %opts = @_;
3554 my %res;
3555
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003556 if ($opts{'-l'}) {
3557 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3558 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
Jakub Narebskicb849b42006-08-31 00:32:15 +02003559
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003560 $res{'mode'} = $1;
3561 $res{'type'} = $2;
3562 $res{'hash'} = $3;
3563 $res{'size'} = $4;
3564 if ($opts{'-z'}) {
3565 $res{'name'} = $5;
3566 } else {
3567 $res{'name'} = unquote($5);
3568 }
Jakub Narebskicb849b42006-08-31 00:32:15 +02003569 } else {
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02003570 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3571 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3572
3573 $res{'mode'} = $1;
3574 $res{'type'} = $2;
3575 $res{'hash'} = $3;
3576 if ($opts{'-z'}) {
3577 $res{'name'} = $4;
3578 } else {
3579 $res{'name'} = unquote($4);
3580 }
Jakub Narebskicb849b42006-08-31 00:32:15 +02003581 }
3582
3583 return wantarray ? %res : \%res;
3584}
3585
Jakub Narebski90921742007-06-08 13:27:42 +02003586# generates _two_ hashes, references to which are passed as 2 and 3 argument
3587sub parse_from_to_diffinfo {
3588 my ($diffinfo, $from, $to, @parents) = @_;
3589
3590 if ($diffinfo->{'nparents'}) {
3591 # combined diff
3592 $from->{'file'} = [];
3593 $from->{'href'} = [];
3594 fill_from_file_info($diffinfo, @parents)
3595 unless exists $diffinfo->{'from_file'};
3596 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
Jakub Narebski9d301452007-11-01 12:38:08 +01003597 $from->{'file'}[$i] =
3598 defined $diffinfo->{'from_file'}[$i] ?
3599 $diffinfo->{'from_file'}[$i] :
3600 $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003601 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3602 $from->{'href'}[$i] = href(action=>"blob",
3603 hash_base=>$parents[$i],
3604 hash=>$diffinfo->{'from_id'}[$i],
3605 file_name=>$from->{'file'}[$i]);
3606 } else {
3607 $from->{'href'}[$i] = undef;
3608 }
3609 }
3610 } else {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003611 # ordinary (not combined) diff
Jakub Narebski9d301452007-11-01 12:38:08 +01003612 $from->{'file'} = $diffinfo->{'from_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003613 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3614 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3615 hash=>$diffinfo->{'from_id'},
3616 file_name=>$from->{'file'});
3617 } else {
3618 delete $from->{'href'};
3619 }
3620 }
3621
Jakub Narebski9d301452007-11-01 12:38:08 +01003622 $to->{'file'} = $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003623 if (!is_deleted($diffinfo)) { # file exists in result
3624 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3625 hash=>$diffinfo->{'to_id'},
3626 file_name=>$to->{'file'});
3627 } else {
3628 delete $to->{'href'};
3629 }
3630}
3631
Jakub Narebski717b8312006-07-31 21:22:15 +02003632## ......................................................................
3633## parse to array of hashes functions
3634
Jakub Narebskicd146402006-11-02 20:23:11 +01003635sub git_get_heads_list {
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003636 my ($limit, @classes) = @_;
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01003637 @classes = ('heads') unless @classes;
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003638 my @patterns = map { "refs/$_" } @classes;
Jakub Narebskicd146402006-11-02 20:23:11 +01003639 my @headslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003640
Jakub Narebskicd146402006-11-02 20:23:11 +01003641 open my $fd, '-|', git_cmd(), 'for-each-ref',
3642 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3643 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
Giuseppe Bilotta9b3f3de2010-11-11 13:26:10 +01003644 @patterns
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003645 or return;
3646 while (my $line = <$fd>) {
Jakub Narebskicd146402006-11-02 20:23:11 +01003647 my %ref_item;
Jakub Narebski120ddde2006-09-19 14:33:22 +02003648
Jakub Narebskicd146402006-11-02 20:23:11 +01003649 chomp $line;
3650 my ($refinfo, $committerinfo) = split(/\0/, $line);
3651 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3652 my ($committer, $epoch, $tz) =
3653 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01003654 $ref_item{'fullname'} = $name;
Giuseppe Bilotta60efa242010-11-11 13:26:09 +01003655 $name =~ s!^refs/(?:head|remote)s/!!;
Jakub Narebskicd146402006-11-02 20:23:11 +01003656
3657 $ref_item{'name'} = $name;
3658 $ref_item{'id'} = $hash;
3659 $ref_item{'title'} = $title || '(no commit message)';
3660 $ref_item{'epoch'} = $epoch;
3661 if ($epoch) {
3662 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3663 } else {
3664 $ref_item{'age'} = "unknown";
Jakub Narebski717b8312006-07-31 21:22:15 +02003665 }
Jakub Narebskicd146402006-11-02 20:23:11 +01003666
3667 push @headslist, \%ref_item;
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003668 }
3669 close $fd;
Junio C Hamano7a13b992006-07-31 19:18:34 -07003670
Jakub Narebskicd146402006-11-02 20:23:11 +01003671 return wantarray ? @headslist : \@headslist;
3672}
Jakub Narebskic83a77e2006-09-15 03:43:28 +02003673
Jakub Narebskicd146402006-11-02 20:23:11 +01003674sub git_get_tags_list {
3675 my $limit = shift;
3676 my @tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003677
Jakub Narebskicd146402006-11-02 20:23:11 +01003678 open my $fd, '-|', git_cmd(), 'for-each-ref',
3679 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3680 '--format=%(objectname) %(objecttype) %(refname) '.
3681 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3682 'refs/tags'
3683 or return;
3684 while (my $line = <$fd>) {
3685 my %ref_item;
3686
3687 chomp $line;
3688 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3689 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3690 my ($creator, $epoch, $tz) =
3691 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01003692 $ref_item{'fullname'} = $name;
Jakub Narebskicd146402006-11-02 20:23:11 +01003693 $name =~ s!^refs/tags/!!;
3694
3695 $ref_item{'type'} = $type;
3696 $ref_item{'id'} = $id;
3697 $ref_item{'name'} = $name;
3698 if ($type eq "tag") {
3699 $ref_item{'subject'} = $title;
3700 $ref_item{'reftype'} = $reftype;
3701 $ref_item{'refid'} = $refid;
3702 } else {
3703 $ref_item{'reftype'} = $type;
3704 $ref_item{'refid'} = $id;
3705 }
3706
3707 if ($type eq "tag" || $type eq "commit") {
3708 $ref_item{'epoch'} = $epoch;
3709 if ($epoch) {
3710 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3711 } else {
3712 $ref_item{'age'} = "unknown";
3713 }
3714 }
3715
3716 push @tagslist, \%ref_item;
Jakub Narebski717b8312006-07-31 21:22:15 +02003717 }
Jakub Narebskicd146402006-11-02 20:23:11 +01003718 close $fd;
3719
3720 return wantarray ? @tagslist : \@tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02003721}
3722
3723## ----------------------------------------------------------------------
3724## filesystem-related functions
3725
3726sub get_file_owner {
3727 my $path = shift;
3728
3729 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3730 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3731 if (!defined $gcos) {
3732 return undef;
3733 }
3734 my $owner = $gcos;
3735 $owner =~ s/[,;].*$//;
Martin Koegler00f429a2007-06-03 17:42:44 +02003736 return to_utf8($owner);
Jakub Narebski717b8312006-07-31 21:22:15 +02003737}
3738
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01003739# assume that file exists
3740sub insert_file {
3741 my $filename = shift;
3742
3743 open my $fd, '<', $filename;
Jakub Narebski45868642008-12-08 14:13:21 +01003744 print map { to_utf8($_) } <$fd>;
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01003745 close $fd;
3746}
3747
Jakub Narebski717b8312006-07-31 21:22:15 +02003748## ......................................................................
3749## mimetype related functions
3750
3751sub mimetype_guess_file {
3752 my $filename = shift;
3753 my $mimemap = shift;
3754 -r $mimemap or return undef;
3755
3756 my %mimemap;
Jakub Narebskidff2b6d2009-05-10 02:38:34 +02003757 open(my $mh, '<', $mimemap) or return undef;
Jakub Narebskiad87e4f2009-05-11 03:21:06 +02003758 while (<$mh>) {
Jakub Narebski618918e2006-08-14 02:15:22 +02003759 next if m/^#/; # skip comments
Ludwig Nussel93a6ad12011-06-15 08:10:08 +02003760 my ($mimetype, @exts) = split(/\s+/);
3761 foreach my $ext (@exts) {
3762 $mimemap{$ext} = $mimetype;
Jakub Narebski717b8312006-07-31 21:22:15 +02003763 }
3764 }
Jakub Narebskiad87e4f2009-05-11 03:21:06 +02003765 close($mh);
Jakub Narebski717b8312006-07-31 21:22:15 +02003766
Jakub Narebski80593192006-09-19 13:57:03 +02003767 $filename =~ /\.([^.]*)$/;
Jakub Narebski717b8312006-07-31 21:22:15 +02003768 return $mimemap{$1};
3769}
3770
3771sub mimetype_guess {
3772 my $filename = shift;
3773 my $mime;
3774 $filename =~ /\./ or return undef;
3775
3776 if ($mimetypes_file) {
3777 my $file = $mimetypes_file;
Jakub Narebskid5aa50d2006-08-14 02:16:33 +02003778 if ($file !~ m!^/!) { # if it is relative path
3779 # it is relative to project
3780 $file = "$projectroot/$project/$file";
3781 }
Jakub Narebski717b8312006-07-31 21:22:15 +02003782 $mime = mimetype_guess_file($filename, $file);
3783 }
3784 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3785 return $mime;
3786}
3787
Jakub Narebski847e01f2006-08-14 02:05:47 +02003788sub blob_mimetype {
Jakub Narebski717b8312006-07-31 21:22:15 +02003789 my $fd = shift;
3790 my $filename = shift;
3791
3792 if ($filename) {
3793 my $mime = mimetype_guess($filename);
3794 $mime and return $mime;
3795 }
3796
3797 # just in case
3798 return $default_blob_plain_mimetype unless $fd;
3799
3800 if (-T $fd) {
Jakub Narebski7f718e82008-06-03 16:47:10 +02003801 return 'text/plain';
Jakub Narebski717b8312006-07-31 21:22:15 +02003802 } elsif (! $filename) {
3803 return 'application/octet-stream';
3804 } elsif ($filename =~ m/\.png$/i) {
3805 return 'image/png';
3806 } elsif ($filename =~ m/\.gif$/i) {
3807 return 'image/gif';
3808 } elsif ($filename =~ m/\.jpe?g$/i) {
3809 return 'image/jpeg';
3810 } else {
3811 return 'application/octet-stream';
3812 }
3813}
3814
Jakub Narebski7f718e82008-06-03 16:47:10 +02003815sub blob_contenttype {
3816 my ($fd, $file_name, $type) = @_;
3817
3818 $type ||= blob_mimetype($fd, $file_name);
3819 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3820 $type .= "; charset=$default_text_plain_charset";
3821 }
3822
3823 return $type;
3824}
3825
Jakub Narebski592ea412010-04-27 21:34:45 +02003826# guess file syntax for syntax highlighting; return undef if no highlighting
3827# the name of syntax can (in the future) depend on syntax highlighter used
3828sub guess_file_syntax {
3829 my ($highlight, $mimetype, $file_name) = @_;
3830 return undef unless ($highlight && defined $file_name);
Jakub Narebski592ea412010-04-27 21:34:45 +02003831 my $basename = basename($file_name, '.in');
3832 return $highlight_basename{$basename}
3833 if exists $highlight_basename{$basename};
3834
3835 $basename =~ /\.([^.]*)$/;
3836 my $ext = $1 or return undef;
3837 return $highlight_ext{$ext}
3838 if exists $highlight_ext{$ext};
3839
3840 return undef;
3841}
3842
3843# run highlighter and return FD of its output,
3844# or return original FD if no highlighting
3845sub run_highlighter {
3846 my ($fd, $highlight, $syntax) = @_;
3847 return $fd unless ($highlight && defined $syntax);
3848
Sylvain Rabot3ca73532010-12-30 22:20:29 +01003849 close $fd;
Jakub Narebski592ea412010-04-27 21:34:45 +02003850 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
Christopher Wilson7ce896b2010-09-21 00:25:19 -07003851 quote_command($highlight_bin).
Kevin Cernekee6affdbe2011-03-16 15:34:13 -07003852 " --replace-tabs=8 --fragment --syntax $syntax |"
Jakub Narebski592ea412010-04-27 21:34:45 +02003853 or die_error(500, "Couldn't open file or run syntax highlighter");
3854 return $fd;
3855}
3856
Jakub Narebski717b8312006-07-31 21:22:15 +02003857## ======================================================================
3858## functions printing HTML: header, footer, error page
3859
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02003860sub get_page_title {
3861 my $title = to_utf8($site_name);
3862
Bernhard R. Link19d2d232012-01-30 21:07:37 +01003863 unless (defined $project) {
3864 if (defined $project_filter) {
Jakub Narebskif4212082012-02-12 16:21:30 +01003865 $title .= " - projects in '" . esc_path($project_filter) . "'";
Bernhard R. Link19d2d232012-01-30 21:07:37 +01003866 }
3867 return $title;
3868 }
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02003869 $title .= " - " . to_utf8($project);
3870
3871 return $title unless (defined $action);
3872 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3873
3874 return $title unless (defined $file_name);
3875 $title .= " - " . esc_path($file_name);
3876 if ($action eq "tree" && $file_name !~ m|/$|) {
3877 $title .= "/";
3878 }
3879
3880 return $title;
3881}
3882
Jakub Narebski6ee90332011-06-22 13:50:46 +02003883sub get_content_type_html {
3884 # require explicit support from the UA if we are to send the page as
3885 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3886 # we have to do this because MSIE sometimes globs '*/*', pretending to
3887 # support xhtml+xml but choking when it gets what it asked for.
3888 if (defined $cgi->http('HTTP_ACCEPT') &&
3889 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3890 $cgi->Accept('application/xhtml+xml') != 0) {
3891 return 'application/xhtml+xml';
3892 } else {
3893 return 'text/html';
3894 }
3895}
3896
Jakub Narebski05bb5a22010-12-18 21:02:13 +01003897sub print_feed_meta {
3898 if (defined $project) {
3899 my %href_params = get_feed_info();
3900 if (!exists $href_params{'-title'}) {
3901 $href_params{'-title'} = 'log';
3902 }
3903
Ævar Arnfjörð Bjarmason0f54b7d2011-02-19 15:27:41 +00003904 foreach my $format (qw(RSS Atom)) {
Jakub Narebski05bb5a22010-12-18 21:02:13 +01003905 my $type = lc($format);
3906 my %link_attr = (
3907 '-rel' => 'alternate',
3908 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
3909 '-type' => "application/$type+xml"
3910 );
3911
Sebastian Pippingcc999a32012-04-04 14:25:44 +02003912 $href_params{'extra_options'} = undef;
Jakub Narebski05bb5a22010-12-18 21:02:13 +01003913 $href_params{'action'} = $type;
3914 $link_attr{'-href'} = href(%href_params);
3915 print "<link ".
3916 "rel=\"$link_attr{'-rel'}\" ".
3917 "title=\"$link_attr{'-title'}\" ".
3918 "href=\"$link_attr{'-href'}\" ".
3919 "type=\"$link_attr{'-type'}\" ".
3920 "/>\n";
3921
3922 $href_params{'extra_options'} = '--no-merges';
3923 $link_attr{'-href'} = href(%href_params);
3924 $link_attr{'-title'} .= ' (no merges)';
3925 print "<link ".
3926 "rel=\"$link_attr{'-rel'}\" ".
3927 "title=\"$link_attr{'-title'}\" ".
3928 "href=\"$link_attr{'-href'}\" ".
3929 "type=\"$link_attr{'-type'}\" ".
3930 "/>\n";
3931 }
3932
3933 } else {
3934 printf('<link rel="alternate" title="%s projects list" '.
3935 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3936 esc_attr($site_name), href(project=>undef, action=>"project_index"));
3937 printf('<link rel="alternate" title="%s projects feeds" '.
3938 'href="%s" type="text/x-opml" />'."\n",
3939 esc_attr($site_name), href(project=>undef, action=>"opml"));
3940 }
3941}
3942
Jakub Narebski6ee90332011-06-22 13:50:46 +02003943sub print_header_links {
3944 my $status = shift;
3945
3946 # print out each stylesheet that exist, providing backwards capability
3947 # for those people who defined $stylesheet in a config file
3948 if (defined $stylesheet) {
3949 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3950 } else {
3951 foreach my $stylesheet (@stylesheets) {
3952 next unless $stylesheet;
3953 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3954 }
3955 }
3956 print_feed_meta()
3957 if ($status eq '200 OK');
3958 if (defined $favicon) {
3959 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3960 }
3961}
3962
Bernhard R. Link40efa222012-01-30 21:09:43 +01003963sub print_nav_breadcrumbs_path {
3964 my $dirprefix = undef;
3965 while (my $part = shift) {
3966 $dirprefix .= "/" if defined $dirprefix;
3967 $dirprefix .= $part;
3968 print $cgi->a({-href => href(project => undef,
3969 project_filter => $dirprefix,
3970 action => "project_list")},
3971 esc_html($part)) . " / ";
3972 }
3973}
3974
Jakub Narebski6ee90332011-06-22 13:50:46 +02003975sub print_nav_breadcrumbs {
3976 my %opts = @_;
3977
3978 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3979 if (defined $project) {
Bernhard R. Link4426ba22012-01-30 21:10:23 +01003980 my @dirname = split '/', $project;
3981 my $projectbasename = pop @dirname;
3982 print_nav_breadcrumbs_path(@dirname);
3983 print $cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));
Jakub Narebski6ee90332011-06-22 13:50:46 +02003984 if (defined $action) {
3985 my $action_print = $action ;
3986 if (defined $opts{-action_extra}) {
3987 $action_print = $cgi->a({-href => href(action=>$action)},
3988 $action);
3989 }
3990 print " / $action_print";
3991 }
3992 if (defined $opts{-action_extra}) {
3993 print " / $opts{-action_extra}";
3994 }
3995 print "\n";
Bernhard R. Link40efa222012-01-30 21:09:43 +01003996 } elsif (defined $project_filter) {
3997 print_nav_breadcrumbs_path(split '/', $project_filter);
Jakub Narebski6ee90332011-06-22 13:50:46 +02003998 }
3999}
4000
4001sub print_search_form {
4002 if (!defined $searchtext) {
4003 $searchtext = "";
4004 }
4005 my $search_hash;
4006 if (defined $hash_base) {
4007 $search_hash = $hash_base;
4008 } elsif (defined $hash) {
4009 $search_hash = $hash;
4010 } else {
4011 $search_hash = "HEAD";
4012 }
4013 my $action = $my_uri;
4014 my $use_pathinfo = gitweb_check_feature('pathinfo');
4015 if ($use_pathinfo) {
4016 $action .= "/".esc_url($project);
4017 }
4018 print $cgi->startform(-method => "get", -action => $action) .
4019 "<div class=\"search\">\n" .
4020 (!$use_pathinfo &&
4021 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
4022 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
4023 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
4024 $cgi->popup_menu(-name => 'st', -default => 'commit',
4025 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
4026 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
4027 " search:\n",
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01004028 $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
Jakub Narebski6ee90332011-06-22 13:50:46 +02004029 "<span title=\"Extended regular expression\">" .
4030 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
4031 -checked => $search_use_regexp) .
4032 "</span>" .
4033 "</div>" .
4034 $cgi->end_form() . "\n";
4035}
4036
Kay Sievers12a88f22005-08-07 20:02:47 +02004037sub git_header_html {
Kay Sieversa59d4af2005-08-07 20:15:44 +02004038 my $status = shift || "200 OK";
Kay Sievers11044292005-10-19 03:18:45 +02004039 my $expires = shift;
Jakub Narebski7a597452010-04-24 16:00:04 +02004040 my %opts = @_;
Kay Sieversa59d4af2005-08-07 20:15:44 +02004041
Jakub Narebskiefb2d0c2010-04-24 16:01:10 +02004042 my $title = get_page_title();
Jakub Narebski6ee90332011-06-22 13:50:46 +02004043 my $content_type = get_content_type_html();
Jakub Narebski952c65f2006-08-22 16:52:50 +02004044 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
Jakub Narebski7a597452010-04-24 16:00:04 +02004045 -status=> $status, -expires => $expires)
Jakub Narebskiad709ea2010-06-13 00:35:59 +02004046 unless ($opts{'-no_http_header'});
Jakub Narebski45c9a752006-12-27 23:59:51 +01004047 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
Kay Sieversa59d4af2005-08-07 20:15:44 +02004048 print <<EOF;
Kay Sievers6191f8e2005-08-07 20:19:56 +02004049<?xml version="1.0" encoding="utf-8"?>
Kay Sievers161332a2005-08-07 19:49:46 +02004050<!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 +02004051<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02004052<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
Jakub Narebskiae20de52006-06-21 09:48:03 +02004053<!-- git core binaries version $git_version -->
Kay Sievers161332a2005-08-07 19:49:46 +02004054<head>
Alp Tokerf6801d62006-07-11 11:19:34 +01004055<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
Jakub Narebski45c9a752006-12-27 23:59:51 +01004056<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
Kay Sieversc994d622005-08-07 20:27:18 +02004057<meta name="robots" content="index, nofollow"/>
Kay Sieversb87d78d2005-08-07 20:21:04 +02004058<title>$title</title>
Kay Sievers161332a2005-08-07 19:49:46 +02004059EOF
Giuseppe Bilotta41a4d162009-01-31 02:31:52 +01004060 # the stylesheet, favicon etc urls won't work correctly with path_info
4061 # unless we set the appropriate base URL
Giuseppe Bilottac3254ae2009-01-31 02:31:50 +01004062 if ($ENV{'PATH_INFO'}) {
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +01004063 print "<base href=\"".esc_url($base_url)."\" />\n";
Giuseppe Bilottac3254ae2009-01-31 02:31:50 +01004064 }
Jakub Narebski6ee90332011-06-22 13:50:46 +02004065 print_header_links($status);
Lénaïc Huardc1355b72011-10-21 09:09:29 +02004066
4067 if (defined $site_html_head_string) {
4068 print to_utf8($site_html_head_string);
4069 }
4070
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02004071 print "</head>\n" .
Alan Chandlerb2d34762006-10-03 13:49:03 +01004072 "<body>\n";
4073
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01004074 if (defined $site_header && -f $site_header) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004075 insert_file($site_header);
Alan Chandlerb2d34762006-10-03 13:49:03 +01004076 }
4077
Jonathan Nieder68220522010-09-03 19:45:09 -05004078 print "<div class=\"page_header\">\n";
4079 if (defined $logo) {
4080 print $cgi->a({-href => esc_url($logo_url),
4081 -title => $logo_label},
4082 $cgi->img({-src => esc_url($logo),
4083 -width => 72, -height => 27,
4084 -alt => "git",
4085 -class => "logo"}));
4086 }
Jakub Narebski6ee90332011-06-22 13:50:46 +02004087 print_nav_breadcrumbs(%opts);
Petr Baudisd77b5672007-05-17 04:24:19 +02004088 print "</div>\n";
4089
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004090 my $have_search = gitweb_check_feature('search');
Jakub Narebskif70dda22008-06-02 11:54:41 +02004091 if (defined $project && $have_search) {
Jakub Narebski6ee90332011-06-22 13:50:46 +02004092 print_search_form();
Kay Sievers4c02e3c2005-08-07 19:52:52 +02004093 }
Kay Sievers161332a2005-08-07 19:49:46 +02004094}
4095
Kay Sievers12a88f22005-08-07 20:02:47 +02004096sub git_footer_html {
Jakub Narebski35621982008-04-20 22:09:48 +02004097 my $feed_class = 'rss_logo';
4098
Kay Sievers6191f8e2005-08-07 20:19:56 +02004099 print "<div class=\"page_footer\">\n";
Kay Sieversb87d78d2005-08-07 20:21:04 +02004100 if (defined $project) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004101 my $descr = git_get_project_description($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02004102 if (defined $descr) {
Kay Sievers40c13812005-11-19 17:41:29 +01004103 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
Kay Sievers6191f8e2005-08-07 20:19:56 +02004104 }
Jakub Narebski35621982008-04-20 22:09:48 +02004105
4106 my %href_params = get_feed_info();
4107 if (!%href_params) {
4108 $feed_class .= ' generic';
4109 }
4110 $href_params{'-title'} ||= 'log';
4111
Ævar Arnfjörð Bjarmason0f54b7d2011-02-19 15:27:41 +00004112 foreach my $format (qw(RSS Atom)) {
Jakub Narebski35621982008-04-20 22:09:48 +02004113 $href_params{'action'} = lc($format);
4114 print $cgi->a({-href => href(%href_params),
4115 -title => "$href_params{'-title'} $format feed",
4116 -class => $feed_class}, $format)."\n";
4117 }
4118
Kay Sieversc994d622005-08-07 20:27:18 +02004119 } else {
Bernhard R. Link56efd9d2012-01-30 21:09:00 +01004120 print $cgi->a({-href => href(project=>undef, action=>"opml",
4121 project_filter => $project_filter),
Jakub Narebski35621982008-04-20 22:09:48 +02004122 -class => $feed_class}, "OPML") . " ";
Bernhard R. Link56efd9d2012-01-30 21:09:00 +01004123 print $cgi->a({-href => href(project=>undef, action=>"project_index",
4124 project_filter => $project_filter),
Jakub Narebski35621982008-04-20 22:09:48 +02004125 -class => $feed_class}, "TXT") . "\n";
Kay Sieversff7669a2005-08-07 20:13:02 +02004126 }
Jakub Narebski35621982008-04-20 22:09:48 +02004127 print "</div>\n"; # class="page_footer"
Alan Chandlerb2d34762006-10-03 13:49:03 +01004128
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02004129 if (defined $t0 && gitweb_check_feature('timed')) {
4130 print "<div id=\"generating_info\">\n";
4131 print 'This page took '.
4132 '<span id="generating_time" class="time_span">'.
Jakub Narebski3962f1d72010-11-09 19:27:54 +01004133 tv_interval($t0, [ gettimeofday() ]).
Jakub Narebskiaa7dd052009-09-01 13:39:16 +02004134 ' seconds </span>'.
4135 ' and '.
4136 '<span id="generating_cmd">'.
4137 $number_of_git_cmds.
4138 '</span> git commands '.
4139 " to generate.\n";
4140 print "</div>\n"; # class="page_footer"
4141 }
4142
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01004143 if (defined $site_footer && -f $site_footer) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004144 insert_file($site_footer);
Alan Chandlerb2d34762006-10-03 13:49:03 +01004145 }
4146
Junio C Hamanoabf411e2010-12-15 11:32:57 -08004147 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004148 if (defined $action &&
4149 $action eq 'blame_incremental') {
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004150 print qq!<script type="text/javascript">\n!.
4151 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
4152 qq! "!. href() .qq!");\n!.
4153 qq!</script>\n!;
John 'Warthog9' Hawley291e52b2011-04-28 21:04:09 +02004154 } else {
Jakub Narebski2e987f92011-04-28 21:04:11 +02004155 my ($jstimezone, $tz_cookie, $datetime_class) =
4156 gitweb_get_feature('javascript-timezone');
4157
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004158 print qq!<script type="text/javascript">\n!.
Jakub Narebski2e987f92011-04-28 21:04:11 +02004159 qq!window.onload = function () {\n!;
4160 if (gitweb_check_feature('javascript-actions')) {
4161 print qq! fixLinks();\n!;
4162 }
4163 if ($jstimezone && $tz_cookie && $datetime_class) {
4164 print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
4165 qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
4166 }
4167 print qq!};\n!.
Jakub Narebskic4ccf612009-09-01 13:39:19 +02004168 qq!</script>\n!;
4169 }
4170
Alan Chandlerb2d34762006-10-03 13:49:03 +01004171 print "</body>\n" .
Kay Sievers9cd3d982005-08-07 20:17:42 +02004172 "</html>";
Kay Sievers161332a2005-08-07 19:49:46 +02004173}
4174
Jakub Narebski453541f2010-02-07 21:51:18 +01004175# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
Lea Wiemann074afaa2008-06-19 22:03:21 +02004176# Example: die_error(404, 'Hash not found')
4177# By convention, use the following status codes (as defined in RFC 2616):
4178# 400: Invalid or missing CGI parameters, or
4179# requested object exists but has wrong type.
4180# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
4181# this server or project.
4182# 404: Requested object/revision/project doesn't exist.
4183# 500: The server isn't configured properly, or
4184# an internal error occurred (e.g. failed assertions caused by bugs), or
4185# an unknown error occurred (e.g. the git binary died unexpectedly).
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004186# 503: The server is currently unavailable (because it is overloaded,
4187# or down for maintenance). Generally, this is a temporary state.
Kay Sievers061cc7c2005-08-07 20:15:57 +02004188sub die_error {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004189 my $status = shift || 500;
Jakub Narebski1df48762010-02-07 21:52:25 +01004190 my $error = esc_html(shift) || "Internal Server Error";
John 'Warthog9' Hawleyaa140132010-01-30 23:30:44 +01004191 my $extra = shift;
Jakub Narebski7a597452010-04-24 16:00:04 +02004192 my %opts = @_;
Kay Sievers664f4cc2005-08-07 20:17:19 +02004193
John 'Warthog9' Hawleyb62a1a92010-01-30 23:30:39 +01004194 my %http_responses = (
4195 400 => '400 Bad Request',
4196 403 => '403 Forbidden',
4197 404 => '404 Not Found',
4198 500 => '500 Internal Server Error',
4199 503 => '503 Service Unavailable',
4200 );
Jakub Narebski7a597452010-04-24 16:00:04 +02004201 git_header_html($http_responses{$status}, undef, %opts);
Jakub Narebski59b9f612006-08-22 23:42:53 +02004202 print <<EOF;
4203<div class="page_body">
4204<br /><br />
4205$status - $error
4206<br />
Jakub Narebski59b9f612006-08-22 23:42:53 +02004207EOF
John 'Warthog9' Hawleyaa140132010-01-30 23:30:44 +01004208 if (defined $extra) {
4209 print "<hr />\n" .
4210 "$extra\n";
4211 }
4212 print "</div>\n";
4213
Kay Sieversa59d4af2005-08-07 20:15:44 +02004214 git_footer_html();
Jakub Narebski7a597452010-04-24 16:00:04 +02004215 goto DONE_GITWEB
4216 unless ($opts{'-error_handler'});
Kay Sieversa59d4af2005-08-07 20:15:44 +02004217}
4218
Jakub Narebski717b8312006-07-31 21:22:15 +02004219## ----------------------------------------------------------------------
4220## functions printing or outputting HTML: navigation
4221
Jakub Narebski847e01f2006-08-14 02:05:47 +02004222sub git_print_page_nav {
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004223 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
4224 $extra = '' if !defined $extra; # pager or formats
4225
4226 my @navs = qw(summary shortlog log commit commitdiff tree);
4227 if ($suppress) {
4228 @navs = grep { $_ ne $suppress } @navs;
4229 }
4230
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004231 my %arg = map { $_ => {action=>$_} } @navs;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004232 if (defined $head) {
4233 for (qw(commit commitdiff)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02004234 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004235 }
4236 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4237 for (qw(shortlog log)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02004238 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004239 }
4240 }
4241 }
Petr Baudisd627f682008-10-02 16:36:52 +02004242
Jakub Narebski3be8e722007-04-01 22:22:21 +02004243 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4244 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004245
Junio C Hamanoa7c5a282008-11-29 13:02:08 -08004246 my @actions = gitweb_get_feature('actions');
Jakub Narebski2b11e052008-10-12 00:02:32 +02004247 my %repl = (
4248 '%' => '%',
4249 'n' => $project, # project name
4250 'f' => $git_dir, # project path within filesystem
4251 'h' => $treehead || '', # current hash ('h' parameter)
4252 'b' => $treebase || '', # hash base ('hb' parameter)
4253 );
Petr Baudisd627f682008-10-02 16:36:52 +02004254 while (@actions) {
Jakub Narebski2b11e052008-10-12 00:02:32 +02004255 my ($label, $link, $pos) = splice(@actions,0,3);
4256 # insert
Petr Baudisd627f682008-10-02 16:36:52 +02004257 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4258 # munch munch
Jakub Narebski2b11e052008-10-12 00:02:32 +02004259 $link =~ s/%([%nfhb])/$repl{$1}/g;
Petr Baudisd627f682008-10-02 16:36:52 +02004260 $arg{$label}{'_href'} = $link;
4261 }
4262
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004263 print "<div class=\"page_nav\">\n" .
4264 (join " | ",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004265 map { $_ eq $current ?
Petr Baudisd627f682008-10-02 16:36:52 +02004266 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004267 } @navs);
Jakub Narebski898a8932006-07-30 16:14:43 +02004268 print "<br/>\n$extra<br/>\n" .
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02004269 "</div>\n";
4270}
4271
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01004272# returns a submenu for the nagivation of the refs views (tags, heads,
4273# remotes) with the current view disabled and the remotes view only
4274# available if the feature is enabled
4275sub format_ref_views {
4276 my ($current) = @_;
4277 my @ref_views = qw{tags heads};
4278 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4279 return join " | ", map {
4280 $_ eq $current ? $_ :
4281 $cgi->a({-href => href(action=>$_)}, $_)
4282 } @ref_views
4283}
4284
Jakub Narebski847e01f2006-08-14 02:05:47 +02004285sub format_paging_nav {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004286 my ($action, $page, $has_next_link) = @_;
Jakub Narebski43ffc062006-07-30 17:49:00 +02004287 my $paging_nav;
4288
4289
Jakub Narebski43ffc062006-07-30 17:49:00 +02004290 if ($page > 0) {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004291 $paging_nav .=
4292 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4293 " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01004294 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebski26298b52006-08-10 12:38:47 +02004295 -accesskey => "p", -title => "Alt-p"}, "prev");
Jakub Narebski43ffc062006-07-30 17:49:00 +02004296 } else {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01004297 $paging_nav .= "first &sdot; prev";
Jakub Narebski43ffc062006-07-30 17:49:00 +02004298 }
4299
Lea Wiemann1f684dc2008-05-28 01:25:42 +02004300 if ($has_next_link) {
Jakub Narebski43ffc062006-07-30 17:49:00 +02004301 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01004302 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Jakub Narebski26298b52006-08-10 12:38:47 +02004303 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski43ffc062006-07-30 17:49:00 +02004304 } else {
4305 $paging_nav .= " &sdot; next";
4306 }
4307
4308 return $paging_nav;
4309}
4310
Jakub Narebski717b8312006-07-31 21:22:15 +02004311## ......................................................................
4312## functions printing or outputting HTML: div
Kay Sievers42f7eb92005-08-07 20:21:46 +02004313
Jakub Narebski847e01f2006-08-14 02:05:47 +02004314sub git_print_header_div {
Jakub Narebski717b8312006-07-31 21:22:15 +02004315 my ($action, $title, $hash, $hash_base) = @_;
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004316 my %args = ();
Jakub Narebski717b8312006-07-31 21:22:15 +02004317
Jakub Narebski3be8e722007-04-01 22:22:21 +02004318 $args{'action'} = $action;
4319 $args{'hash'} = $hash if $hash;
4320 $args{'hash_base'} = $hash_base if $hash_base;
Jakub Narebski717b8312006-07-31 21:22:15 +02004321
4322 print "<div class=\"header\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004323 $cgi->a({-href => href(%args), -class => "title"},
4324 $title ? $title : $action) .
4325 "\n</div>\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02004326}
4327
Giuseppe Bilotta0e656992010-11-11 13:26:15 +01004328sub format_repo_url {
4329 my ($name, $url) = @_;
4330 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4331}
4332
Giuseppe Bilottab891d522010-11-11 13:26:16 +01004333# Group output by placing it in a DIV element and adding a header.
4334# Options for start_div() can be provided by passing a hash reference as the
4335# first parameter to the function.
4336# Options to git_print_header_div() can be provided by passing an array
4337# reference. This must follow the options to start_div if they are present.
4338# The content can be a scalar, which is output as-is, a scalar reference, which
4339# is output after html escaping, an IO handle passed either as *handle or
4340# *handle{IO}, or a function reference. In the latter case all following
4341# parameters will be taken as argument to the content function call.
4342sub git_print_section {
4343 my ($div_args, $header_args, $content);
4344 my $arg = shift;
4345 if (ref($arg) eq 'HASH') {
4346 $div_args = $arg;
4347 $arg = shift;
4348 }
4349 if (ref($arg) eq 'ARRAY') {
4350 $header_args = $arg;
4351 $arg = shift;
4352 }
4353 $content = $arg;
4354
4355 print $cgi->start_div($div_args);
4356 git_print_header_div(@$header_args);
4357
4358 if (ref($content) eq 'CODE') {
4359 $content->(@_);
4360 } elsif (ref($content) eq 'SCALAR') {
4361 print esc_html($$content);
4362 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4363 print <$content>;
4364 } elsif (!ref($content) && defined($content)) {
4365 print $content;
4366 }
4367
4368 print $cgi->end_div;
4369}
4370
Jakub Narebski256b7b42011-04-28 21:04:07 +02004371sub format_timestamp_html {
Jakub Narebskice71b072011-04-28 21:04:08 +02004372 my $date = shift;
Jakub Narebski2e987f92011-04-28 21:04:11 +02004373 my $strtime = $date->{'rfc2822'};
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004374
Jakub Narebski2e987f92011-04-28 21:04:11 +02004375 my (undef, undef, $datetime_class) =
4376 gitweb_get_feature('javascript-timezone');
4377 if ($datetime_class) {
4378 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
Jakub Narebskia44465c2006-08-28 23:17:31 +02004379 }
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004380
Jakub Narebski256b7b42011-04-28 21:04:07 +02004381 my $localtime_format = '(%02d:%02d %s)';
4382 if ($date->{'hour_local'} < 6) {
4383 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
Jakub Narebskia44465c2006-08-28 23:17:31 +02004384 }
Jakub Narebski256b7b42011-04-28 21:04:07 +02004385 $strtime .= ' ' .
4386 sprintf($localtime_format,
4387 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
John 'Warthog9' Hawley0cf207f2010-01-30 23:30:42 +01004388
Jakub Narebski256b7b42011-04-28 21:04:07 +02004389 return $strtime;
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004390}
4391
4392# Outputs the author name and date in long form
4393sub git_print_authorship {
4394 my $co = shift;
4395 my %opts = @_;
4396 my $tag = $opts{-tag} || 'div';
Stephen Boyde133d652009-10-15 21:14:59 -07004397 my $author = $co->{'author_name'};
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004398
4399 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4400 print "<$tag class=\"author_date\">" .
Stephen Boyde133d652009-10-15 21:14:59 -07004401 format_search_author($author, "author", esc_html($author)) .
Jakub Narebskice71b072011-04-28 21:04:08 +02004402 " [".format_timestamp_html(\%ad)."]".
Jakub Narebski256b7b42011-04-28 21:04:07 +02004403 git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4404 "</$tag>\n";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004405}
4406
4407# Outputs table rows containing the full author or committer information,
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02004408# in the format expected for 'commit' view (& similar).
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004409# Parameters are a commit hash reference, followed by the list of people
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02004410# to output information for. If the list is empty it defaults to both
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004411# author and committer.
4412sub git_print_authorship_rows {
4413 my $co = shift;
4414 # too bad we can't use @people = @_ || ('author', 'committer')
4415 my @people = @_;
4416 @people = ('author', 'committer') unless @people;
4417 foreach my $who (@people) {
4418 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
Stephen Boyde133d652009-10-15 21:14:59 -07004419 print "<tr><td>$who</td><td>" .
4420 format_search_author($co->{"${who}_name"}, $who,
Jakub Narebski256b7b42011-04-28 21:04:07 +02004421 esc_html($co->{"${who}_name"})) . " " .
Stephen Boyde133d652009-10-15 21:14:59 -07004422 format_search_author($co->{"${who}_email"}, $who,
Jakub Narebski256b7b42011-04-28 21:04:07 +02004423 esc_html("<" . $co->{"${who}_email"} . ">")) .
Stephen Boyde133d652009-10-15 21:14:59 -07004424 "</td><td rowspan=\"2\">" .
Giuseppe Bilottae9fdd742009-06-30 00:00:51 +02004425 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4426 "</td></tr>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004427 "<tr>" .
Jakub Narebski256b7b42011-04-28 21:04:07 +02004428 "<td></td><td>" .
Jakub Narebskice71b072011-04-28 21:04:08 +02004429 format_timestamp_html(\%wd) .
Jakub Narebski256b7b42011-04-28 21:04:07 +02004430 "</td>" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02004431 "</tr>\n";
4432 }
Jakub Narebski6fd92a22006-08-28 14:48:12 +02004433}
4434
Jakub Narebski717b8312006-07-31 21:22:15 +02004435sub git_print_page_path {
4436 my $name = shift;
4437 my $type = shift;
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004438 my $hb = shift;
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01004439
Jakub Narebski4df118e2006-10-21 17:53:55 +02004440
4441 print "<div class=\"page_path\">";
4442 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
Martin Koegler00f429a2007-06-03 17:42:44 +02004443 -title => 'tree root'}, to_utf8("[$project]"));
Jakub Narebski4df118e2006-10-21 17:53:55 +02004444 print " / ";
4445 if (defined $name) {
Jakub Narebski762c7202006-09-04 18:17:58 +02004446 my @dirname = split '/', $name;
4447 my $basename = pop @dirname;
4448 my $fullname = '';
4449
Jakub Narebski762c7202006-09-04 18:17:58 +02004450 foreach my $dir (@dirname) {
Petr Baudis16fdb482006-09-21 02:05:50 +02004451 $fullname .= ($fullname ? '/' : '') . $dir;
Jakub Narebski762c7202006-09-04 18:17:58 +02004452 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4453 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004454 -title => $fullname}, esc_path($dir));
Petr Baudis26d0a972006-09-23 01:00:12 +02004455 print " / ";
Jakub Narebski762c7202006-09-04 18:17:58 +02004456 }
4457 if (defined $type && $type eq 'blob') {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004458 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
Jakub Narebski762c7202006-09-04 18:17:58 +02004459 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004460 -title => $name}, esc_path($basename));
Jakub Narebski762c7202006-09-04 18:17:58 +02004461 } elsif (defined $type && $type eq 'tree') {
4462 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4463 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01004464 -title => $name}, esc_path($basename));
Jakub Narebski4df118e2006-10-21 17:53:55 +02004465 print " / ";
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004466 } else {
Jakub Narebski403d0902006-11-08 11:48:56 +01004467 print esc_path($basename);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004468 }
Kay Sievers8ed23e12005-08-07 20:05:44 +02004469 }
Jakub Narebski4df118e2006-10-21 17:53:55 +02004470 print "<br/></div>\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02004471}
4472
Jakub Narebski74fd8722009-05-07 19:11:29 +02004473sub git_print_log {
Jakub Narebskid16d0932006-08-17 11:21:23 +02004474 my $log = shift;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004475 my %opts = @_;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004476
Jakub Narebskib7f92532006-08-28 14:48:10 +02004477 if ($opts{'-remove_title'}) {
4478 # remove title, i.e. first line of log
4479 shift @$log;
4480 }
Jakub Narebskid16d0932006-08-17 11:21:23 +02004481 # remove leading empty lines
4482 while (defined $log->[0] && $log->[0] eq "") {
4483 shift @$log;
4484 }
4485
4486 # print log
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004487 my $skip_blank_line = 0;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004488 foreach my $line (@$log) {
Namhyung Kim3d1110a2012-07-04 11:47:25 +09004489 if ($line =~ m/^\s*([A-Z][-A-Za-z]*-[Bb]y|C[Cc]): /) {
Jakub Narebskib7f92532006-08-28 14:48:10 +02004490 if (! $opts{'-remove_signoff'}) {
4491 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004492 $skip_blank_line = 1;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004493 }
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004494 next;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004495 }
4496
Namhyung Kim66c857e2012-07-04 11:47:26 +09004497 if ($line =~ m,\s*([a-z]*link): (https?://\S+),i) {
4498 if (! $opts{'-remove_signoff'}) {
4499 print "<span class=\"signoff\">" . esc_html($1) . ": " .
4500 "<a href=\"" . esc_html($2) . "\">" . esc_html($2) . "</a>" .
4501 "</span><br/>\n";
4502 $skip_blank_line = 1;
4503 }
4504 next;
Jakub Narebskib7f92532006-08-28 14:48:10 +02004505 }
4506
Jakub Narebskid16d0932006-08-17 11:21:23 +02004507 # print only one empty line
4508 # do not print empty line after signoff
4509 if ($line eq "") {
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004510 next if ($skip_blank_line);
4511 $skip_blank_line = 1;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004512 } else {
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004513 $skip_blank_line = 0;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004514 }
Jakub Narebskib7f92532006-08-28 14:48:10 +02004515
4516 print format_log_line_html($line) . "<br/>\n";
4517 }
4518
4519 if ($opts{'-final_empty_line'}) {
4520 # end with single empty line
Namhyung Kim5a45c0c2012-07-04 11:47:24 +09004521 print "<br/>\n" unless $skip_blank_line;
Jakub Narebskid16d0932006-08-17 11:21:23 +02004522 }
4523}
4524
Jakub Narebskie33fba42006-12-10 13:25:46 +01004525# return link target (what link points to)
4526sub git_get_link_target {
4527 my $hash = shift;
4528 my $link_target;
4529
4530 # read link
4531 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4532 or return;
4533 {
Jakub Narebski34122b52009-05-11 03:29:40 +02004534 local $/ = undef;
Jakub Narebskie33fba42006-12-10 13:25:46 +01004535 $link_target = <$fd>;
4536 }
4537 close $fd
4538 or return;
4539
4540 return $link_target;
4541}
4542
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004543# given link target, and the directory (basedir) the link is in,
4544# return target of link relative to top directory (top tree);
4545# return undef if it is not possible (including absolute links).
4546sub normalize_link_target {
Jakub Narebski15c54fe2009-05-11 19:45:11 +02004547 my ($link_target, $basedir) = @_;
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004548
4549 # absolute symlinks (beginning with '/') cannot be normalized
4550 return if (substr($link_target, 0, 1) eq '/');
4551
4552 # normalize link target to path from top (root) tree (dir)
4553 my $path;
4554 if ($basedir) {
4555 $path = $basedir . '/' . $link_target;
4556 } else {
4557 # we are in top (root) tree (dir)
4558 $path = $link_target;
4559 }
4560
4561 # remove //, /./, and /../
4562 my @path_parts;
4563 foreach my $part (split('/', $path)) {
4564 # discard '.' and ''
4565 next if (!$part || $part eq '.');
4566 # handle '..'
4567 if ($part eq '..') {
4568 if (@path_parts) {
4569 pop @path_parts;
4570 } else {
4571 # link leads outside repository (outside top dir)
4572 return;
4573 }
4574 } else {
4575 push @path_parts, $part;
4576 }
4577 }
4578 $path = join('/', @path_parts);
4579
4580 return $path;
4581}
Jakub Narebskie33fba42006-12-10 13:25:46 +01004582
Jakub Narebskifa702002006-08-31 00:35:07 +02004583# print tree entry (row of git_tree), but without encompassing <tr> element
4584sub git_print_tree_entry {
4585 my ($t, $basedir, $hash_base, $have_blame) = @_;
4586
4587 my %base_key = ();
Jakub Narebskie33fba42006-12-10 13:25:46 +01004588 $base_key{'hash_base'} = $hash_base if defined $hash_base;
Jakub Narebskifa702002006-08-31 00:35:07 +02004589
Luben Tuikov4de741b2006-09-25 22:38:16 -07004590 # The format of a table row is: mode list link. Where mode is
4591 # the mode of the entry, list is the name of the entry, an href,
4592 # and link is the action links of the entry.
4593
Jakub Narebskifa702002006-08-31 00:35:07 +02004594 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004595 if (exists $t->{'size'}) {
4596 print "<td class=\"size\">$t->{'size'}</td>\n";
4597 }
Jakub Narebskifa702002006-08-31 00:35:07 +02004598 if ($t->{'type'} eq "blob") {
4599 print "<td class=\"list\">" .
Luben Tuikov4de741b2006-09-25 22:38:16 -07004600 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004601 file_name=>"$basedir$t->{'name'}", %base_key),
Jakub Narebskie33fba42006-12-10 13:25:46 +01004602 -class => "list"}, esc_path($t->{'name'}));
4603 if (S_ISLNK(oct $t->{'mode'})) {
4604 my $link_target = git_get_link_target($t->{'hash'});
4605 if ($link_target) {
Jakub Narebski15c54fe2009-05-11 19:45:11 +02004606 my $norm_target = normalize_link_target($link_target, $basedir);
Jakub Narebski3bf9d572006-12-10 13:25:48 +01004607 if (defined $norm_target) {
4608 print " -> " .
4609 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4610 file_name=>$norm_target),
4611 -title => $norm_target}, esc_path($link_target));
4612 } else {
4613 print " -> " . esc_path($link_target);
4614 }
Jakub Narebskie33fba42006-12-10 13:25:46 +01004615 }
4616 }
4617 print "</td>\n";
Luben Tuikov4de741b2006-09-25 22:38:16 -07004618 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02004619 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004620 file_name=>"$basedir$t->{'name'}", %base_key)},
4621 "blob");
Jakub Narebskifa702002006-08-31 00:35:07 +02004622 if ($have_blame) {
Petr Baudis4777b012006-10-24 05:36:10 +02004623 print " | " .
4624 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004625 file_name=>"$basedir$t->{'name'}", %base_key)},
4626 "blame");
Jakub Narebskifa702002006-08-31 00:35:07 +02004627 }
4628 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02004629 print " | " .
4630 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02004631 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4632 "history");
4633 }
4634 print " | " .
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004635 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004636 file_name=>"$basedir$t->{'name'}")},
4637 "raw");
Luben Tuikov4de741b2006-09-25 22:38:16 -07004638 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02004639
4640 } elsif ($t->{'type'} eq "tree") {
Luben Tuikov0fa105e2006-09-26 12:45:37 -07004641 print "<td class=\"list\">";
4642 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004643 file_name=>"$basedir$t->{'name'}",
4644 %base_key)},
Jakub Narebski403d0902006-11-08 11:48:56 +01004645 esc_path($t->{'name'}));
Luben Tuikov0fa105e2006-09-26 12:45:37 -07004646 print "</td>\n";
4647 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02004648 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02004649 file_name=>"$basedir$t->{'name'}",
4650 %base_key)},
Jakub Narebskie33fba42006-12-10 13:25:46 +01004651 "tree");
Jakub Narebskifa702002006-08-31 00:35:07 +02004652 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02004653 print " | " .
4654 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02004655 file_name=>"$basedir$t->{'name'}")},
4656 "history");
4657 }
4658 print "</td>\n";
Jakub Narebski01ac1e32007-07-28 16:27:31 +02004659 } else {
4660 # unknown object: we can only present history for it
4661 # (this includes 'commit' object, i.e. submodule support)
4662 print "<td class=\"list\">" .
4663 esc_path($t->{'name'}) .
4664 "</td>\n";
4665 print "<td class=\"link\">";
4666 if (defined $hash_base) {
4667 print $cgi->a({-href => href(action=>"history",
4668 hash_base=>$hash_base,
4669 file_name=>"$basedir$t->{'name'}")},
4670 "history");
4671 }
4672 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02004673 }
4674}
4675
Jakub Narebski717b8312006-07-31 21:22:15 +02004676## ......................................................................
4677## functions printing large fragments of HTML
Kay Sieversede5e102005-08-07 20:23:12 +02004678
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004679# get pre-image filenames for merge (combined) diff
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004680sub fill_from_file_info {
4681 my ($diff, @parents) = @_;
4682
4683 $diff->{'from_file'} = [ ];
4684 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4685 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4686 if ($diff->{'status'}[$i] eq 'R' ||
4687 $diff->{'status'}[$i] eq 'C') {
4688 $diff->{'from_file'}[$i] =
4689 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4690 }
4691 }
4692
4693 return $diff;
4694}
4695
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004696# is current raw difftree line of file deletion
Jakub Narebski90921742007-06-08 13:27:42 +02004697sub is_deleted {
4698 my $diffinfo = shift;
4699
Jakub Narebski4ed4a342008-04-05 21:13:24 +01004700 return $diffinfo->{'to_id'} eq ('0' x 40);
Jakub Narebski90921742007-06-08 13:27:42 +02004701}
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004702
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004703# does patch correspond to [previous] difftree raw line
4704# $diffinfo - hashref of parsed raw diff format
4705# $patchinfo - hashref of parsed patch diff format
4706# (the same keys as in $diffinfo)
4707sub is_patch_split {
4708 my ($diffinfo, $patchinfo) = @_;
4709
4710 return defined $diffinfo && defined $patchinfo
Jakub Narebski9d301452007-11-01 12:38:08 +01004711 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004712}
4713
4714
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004715sub git_difftree_body {
Jakub Narebskied224de2007-05-07 01:10:04 +02004716 my ($difftree, $hash, @parents) = @_;
4717 my ($parent) = $parents[0];
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004718 my $have_blame = gitweb_check_feature('blame');
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004719 print "<div class=\"list_head\">\n";
4720 if ($#{$difftree} > 10) {
4721 print(($#{$difftree} + 1) . " files changed:\n");
4722 }
4723 print "</div>\n";
4724
Jakub Narebskied224de2007-05-07 01:10:04 +02004725 print "<table class=\"" .
4726 (@parents > 1 ? "combined " : "") .
4727 "diff_tree\">\n";
Jakub Narebski47598d72007-06-08 13:24:56 +02004728
4729 # header only for combined diff in 'commitdiff' view
Jakub Narebski3ef408a2007-09-08 21:54:28 +02004730 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
Jakub Narebski47598d72007-06-08 13:24:56 +02004731 if ($has_header) {
4732 # table header
4733 print "<thead><tr>\n" .
4734 "<th></th><th></th>\n"; # filename, patchN link
4735 for (my $i = 0; $i < @parents; $i++) {
4736 my $par = $parents[$i];
4737 print "<th>" .
4738 $cgi->a({-href => href(action=>"commitdiff",
4739 hash=>$hash, hash_parent=>$par),
4740 -title => 'commitdiff to parent number ' .
4741 ($i+1) . ': ' . substr($par,0,7)},
4742 $i+1) .
4743 "&nbsp;</th>\n";
4744 }
4745 print "</tr></thead>\n<tbody>\n";
4746 }
4747
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004748 my $alternate = 1;
Jakub Narebskib4657e72006-08-28 14:48:14 +02004749 my $patchno = 0;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004750 foreach my $line (@{$difftree}) {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01004751 my $diff = parsed_difftree_line($line);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004752
4753 if ($alternate) {
4754 print "<tr class=\"dark\">\n";
4755 } else {
4756 print "<tr class=\"light\">\n";
4757 }
4758 $alternate ^= 1;
4759
Jakub Narebski493e01d2007-05-07 01:10:06 +02004760 if (exists $diff->{'nparents'}) { # combined diff
Jakub Narebskied224de2007-05-07 01:10:04 +02004761
Jakub Narebski493e01d2007-05-07 01:10:06 +02004762 fill_from_file_info($diff, @parents)
4763 unless exists $diff->{'from_file'};
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02004764
Jakub Narebski90921742007-06-08 13:27:42 +02004765 if (!is_deleted($diff)) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004766 # file exists in the result (child) commit
4767 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02004768 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4769 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004770 hash_base=>$hash),
Jakub Narebski493e01d2007-05-07 01:10:06 +02004771 -class => "list"}, esc_path($diff->{'to_file'})) .
Jakub Narebskied224de2007-05-07 01:10:04 +02004772 "</td>\n";
4773 } else {
4774 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02004775 esc_path($diff->{'to_file'}) .
Jakub Narebskied224de2007-05-07 01:10:04 +02004776 "</td>\n";
4777 }
4778
4779 if ($action eq 'commitdiff') {
4780 # link to patch
4781 $patchno++;
4782 print "<td class=\"link\">" .
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004783 $cgi->a({-href => href(-anchor=>"patch$patchno")},
4784 "patch") .
Jakub Narebskied224de2007-05-07 01:10:04 +02004785 " | " .
4786 "</td>\n";
4787 }
4788
4789 my $has_history = 0;
4790 my $not_deleted = 0;
Jakub Narebski493e01d2007-05-07 01:10:06 +02004791 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004792 my $hash_parent = $parents[$i];
Jakub Narebski493e01d2007-05-07 01:10:06 +02004793 my $from_hash = $diff->{'from_id'}[$i];
4794 my $from_path = $diff->{'from_file'}[$i];
4795 my $status = $diff->{'status'}[$i];
Jakub Narebskied224de2007-05-07 01:10:04 +02004796
4797 $has_history ||= ($status ne 'A');
4798 $not_deleted ||= ($status ne 'D');
4799
Jakub Narebskied224de2007-05-07 01:10:04 +02004800 if ($status eq 'A') {
4801 print "<td class=\"link\" align=\"right\"> | </td>\n";
4802 } elsif ($status eq 'D') {
4803 print "<td class=\"link\">" .
4804 $cgi->a({-href => href(action=>"blob",
4805 hash_base=>$hash,
4806 hash=>$from_hash,
4807 file_name=>$from_path)},
4808 "blob" . ($i+1)) .
4809 " | </td>\n";
4810 } else {
Jakub Narebski493e01d2007-05-07 01:10:06 +02004811 if ($diff->{'to_id'} eq $from_hash) {
Jakub Narebskied224de2007-05-07 01:10:04 +02004812 print "<td class=\"link nochange\">";
4813 } else {
4814 print "<td class=\"link\">";
4815 }
4816 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004817 hash=>$diff->{'to_id'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004818 hash_parent=>$from_hash,
4819 hash_base=>$hash,
4820 hash_parent_base=>$hash_parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004821 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004822 file_parent=>$from_path)},
4823 "diff" . ($i+1)) .
4824 " | </td>\n";
4825 }
4826 }
4827
4828 print "<td class=\"link\">";
4829 if ($not_deleted) {
4830 print $cgi->a({-href => href(action=>"blob",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004831 hash=>$diff->{'to_id'},
4832 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004833 hash_base=>$hash)},
4834 "blob");
4835 print " | " if ($has_history);
4836 }
4837 if ($has_history) {
4838 print $cgi->a({-href => href(action=>"history",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004839 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02004840 hash_base=>$hash)},
4841 "history");
4842 }
4843 print "</td>\n";
4844
4845 print "</tr>\n";
4846 next; # instead of 'else' clause, to avoid extra indent
4847 }
4848 # else ordinary diff
4849
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004850 my ($to_mode_oct, $to_mode_str, $to_file_type);
4851 my ($from_mode_oct, $from_mode_str, $from_file_type);
Jakub Narebski493e01d2007-05-07 01:10:06 +02004852 if ($diff->{'to_mode'} ne ('0' x 6)) {
4853 $to_mode_oct = oct $diff->{'to_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004854 if (S_ISREG($to_mode_oct)) { # only for regular file
4855 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004856 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004857 $to_file_type = file_type($diff->{'to_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004858 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004859 if ($diff->{'from_mode'} ne ('0' x 6)) {
4860 $from_mode_oct = oct $diff->{'from_mode'};
Ævar Arnfjörð Bjarmason98885c22011-02-19 15:27:42 +00004861 if (S_ISREG($from_mode_oct)) { # only for regular file
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004862 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4863 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004864 $from_file_type = file_type($diff->{'from_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004865 }
4866
Jakub Narebski493e01d2007-05-07 01:10:06 +02004867 if ($diff->{'status'} eq "A") { # created
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004868 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4869 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4870 $mode_chng .= "]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07004871 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004872 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4873 hash_base=>$hash, file_name=>$diff->{'file'}),
4874 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07004875 print "</td>\n";
4876 print "<td>$mode_chng</td>\n";
4877 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02004878 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02004879 # link to patch
4880 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004881 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4882 "patch") .
4883 " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02004884 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004885 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4886 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski3faa5412007-01-08 02:10:42 +01004887 "blob");
Jakub Narebskib4657e72006-08-28 14:48:14 +02004888 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004889
Jakub Narebski493e01d2007-05-07 01:10:06 +02004890 } elsif ($diff->{'status'} eq "D") { # deleted
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004891 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07004892 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004893 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4894 hash_base=>$parent, file_name=>$diff->{'file'}),
4895 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07004896 print "</td>\n";
4897 print "<td>$mode_chng</td>\n";
4898 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02004899 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02004900 # link to patch
4901 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004902 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4903 "patch") .
4904 " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02004905 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004906 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4907 hash_base=>$parent, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01004908 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08004909 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01004910 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004911 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01004912 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08004913 }
Jakub Narebskib4657e72006-08-28 14:48:14 +02004914 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004915 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004916 "history");
Luben Tuikov499faed2006-09-27 17:23:25 -07004917 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004918
Jakub Narebski493e01d2007-05-07 01:10:06 +02004919 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004920 my $mode_chnge = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004921 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004922 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
Jakub Narebski6e72cf42007-01-03 20:47:24 +01004923 if ($from_file_type ne $to_file_type) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004924 $mode_chnge .= " from $from_file_type to $to_file_type";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004925 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004926 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4927 if ($from_mode_str && $to_mode_str) {
4928 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4929 } elsif ($to_mode_str) {
4930 $mode_chnge .= " mode: $to_mode_str";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004931 }
4932 }
4933 $mode_chnge .= "]</span>\n";
4934 }
4935 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004936 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4937 hash_base=>$hash, file_name=>$diff->{'file'}),
4938 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07004939 print "</td>\n";
4940 print "<td>$mode_chnge</td>\n";
4941 print "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01004942 if ($action eq 'commitdiff') {
4943 # link to patch
4944 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004945 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4946 "patch") .
Jakub Narebski241cc592006-10-31 17:36:27 +01004947 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004948 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01004949 # "commit" view and modified file (not onlu mode changed)
4950 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004951 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01004952 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004953 file_name=>$diff->{'file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01004954 "diff") .
4955 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004956 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02004957 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4958 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01004959 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08004960 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01004961 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004962 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01004963 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08004964 }
Luben Tuikoveb51ec92006-09-27 17:24:49 -07004965 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004966 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02004967 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004968 print "</td>\n";
4969
Jakub Narebski493e01d2007-05-07 01:10:06 +02004970 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004971 my %status_name = ('R' => 'moved', 'C' => 'copied');
Jakub Narebski493e01d2007-05-07 01:10:06 +02004972 my $nstatus = $status_name{$diff->{'status'}};
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004973 my $mode_chng = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004974 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004975 # mode also for directories, so we cannot use $to_mode_str
4976 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004977 }
4978 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004979 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004980 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4981 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02004982 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4983 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004984 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4985 -class => "list"}, esc_path($diff->{'from_file'})) .
4986 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
Luben Tuikov499faed2006-09-27 17:23:25 -07004987 "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01004988 if ($action eq 'commitdiff') {
4989 # link to patch
4990 $patchno++;
Kevin Cernekee5e96a842011-03-18 17:00:16 +01004991 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4992 "patch") .
Jakub Narebski241cc592006-10-31 17:36:27 +01004993 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02004994 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01004995 # "commit" view and modified file (not only pure rename or copy)
4996 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02004997 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01004998 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02004999 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01005000 "diff") .
5001 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005002 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02005003 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
5004 hash_base=>$parent, file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005005 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005006 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01005007 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005008 file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01005009 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08005010 }
Jakub Narebski897d1d22006-11-19 22:51:39 +01005011 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02005012 file_name=>$diff->{'to_file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02005013 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005014 print "</td>\n";
5015
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005016 } # we should not encounter Unmerged (U) or Unknown (X) status
5017 print "</tr>\n";
5018 }
Jakub Narebski47598d72007-06-08 13:24:56 +02005019 print "</tbody>" if $has_header;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005020 print "</table>\n";
5021}
5022
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005023# Print context lines and then rem/add lines in a side-by-side manner.
5024sub print_sidebyside_diff_lines {
5025 my ($ctx, $rem, $add) = @_;
5026
5027 # print context block before add/rem block
5028 if (@$ctx) {
5029 print join '',
5030 '<div class="chunk_block ctx">',
5031 '<div class="old">',
5032 @$ctx,
5033 '</div>',
5034 '<div class="new">',
5035 @$ctx,
5036 '</div>',
5037 '</div>';
5038 }
5039
5040 if (!@$add) {
5041 # pure removal
5042 print join '',
5043 '<div class="chunk_block rem">',
5044 '<div class="old">',
5045 @$rem,
5046 '</div>',
5047 '</div>';
5048 } elsif (!@$rem) {
5049 # pure addition
5050 print join '',
5051 '<div class="chunk_block add">',
5052 '<div class="new">',
5053 @$add,
5054 '</div>',
5055 '</div>';
5056 } else {
5057 print join '',
5058 '<div class="chunk_block chg">',
5059 '<div class="old">',
5060 @$rem,
5061 '</div>',
5062 '<div class="new">',
5063 @$add,
5064 '</div>',
5065 '</div>';
5066 }
5067}
5068
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005069# Print context lines and then rem/add lines in inline manner.
5070sub print_inline_diff_lines {
5071 my ($ctx, $rem, $add) = @_;
5072
5073 print @$ctx, @$rem, @$add;
5074}
5075
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005076# Format removed and added line, mark changed part and HTML-format them.
5077# Implementation is based on contrib/diff-highlight
5078sub format_rem_add_lines_pair {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005079 my ($rem, $add, $num_parents) = @_;
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005080
5081 # We need to untabify lines before split()'ing them;
5082 # otherwise offsets would be invalid.
5083 chomp $rem;
5084 chomp $add;
5085 $rem = untabify($rem);
5086 $add = untabify($add);
5087
5088 my @rem = split(//, $rem);
5089 my @add = split(//, $add);
5090 my ($esc_rem, $esc_add);
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005091 # Ignore leading +/- characters for each parent.
5092 my ($prefix_len, $suffix_len) = ($num_parents, 0);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005093 my ($prefix_has_nonspace, $suffix_has_nonspace);
5094
5095 my $shorter = (@rem < @add) ? @rem : @add;
5096 while ($prefix_len < $shorter) {
5097 last if ($rem[$prefix_len] ne $add[$prefix_len]);
5098
5099 $prefix_has_nonspace = 1 if ($rem[$prefix_len] !~ /\s/);
5100 $prefix_len++;
5101 }
5102
5103 while ($prefix_len + $suffix_len < $shorter) {
5104 last if ($rem[-1 - $suffix_len] ne $add[-1 - $suffix_len]);
5105
5106 $suffix_has_nonspace = 1 if ($rem[-1 - $suffix_len] !~ /\s/);
5107 $suffix_len++;
5108 }
5109
5110 # Mark lines that are different from each other, but have some common
5111 # part that isn't whitespace. If lines are completely different, don't
5112 # mark them because that would make output unreadable, especially if
5113 # diff consists of multiple lines.
5114 if ($prefix_has_nonspace || $suffix_has_nonspace) {
5115 $esc_rem = esc_html_hl_regions($rem, 'marked',
5116 [$prefix_len, @rem - $suffix_len], -nbsp=>1);
5117 $esc_add = esc_html_hl_regions($add, 'marked',
5118 [$prefix_len, @add - $suffix_len], -nbsp=>1);
5119 } else {
5120 $esc_rem = esc_html($rem, -nbsp=>1);
5121 $esc_add = esc_html($add, -nbsp=>1);
5122 }
5123
5124 return format_diff_line(\$esc_rem, 'rem'),
5125 format_diff_line(\$esc_add, 'add');
5126}
5127
5128# HTML-format diff context, removed and added lines.
5129sub format_ctx_rem_add_lines {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005130 my ($ctx, $rem, $add, $num_parents) = @_;
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005131 my (@new_ctx, @new_rem, @new_add);
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005132 my $can_highlight = 0;
5133 my $is_combined = ($num_parents > 1);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005134
5135 # Highlight if every removed line has a corresponding added line.
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005136 if (@$add > 0 && @$add == @$rem) {
5137 $can_highlight = 1;
5138
5139 # Highlight lines in combined diff only if the chunk contains
5140 # diff between the same version, e.g.
5141 #
5142 # - a
5143 # - b
5144 # + c
5145 # + d
5146 #
5147 # Otherwise the highlightling would be confusing.
5148 if ($is_combined) {
5149 for (my $i = 0; $i < @$add; $i++) {
5150 my $prefix_rem = substr($rem->[$i], 0, $num_parents);
5151 my $prefix_add = substr($add->[$i], 0, $num_parents);
5152
5153 $prefix_rem =~ s/-/+/g;
5154
5155 if ($prefix_rem ne $prefix_add) {
5156 $can_highlight = 0;
5157 last;
5158 }
5159 }
5160 }
5161 }
5162
5163 if ($can_highlight) {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005164 for (my $i = 0; $i < @$add; $i++) {
5165 my ($line_rem, $line_add) = format_rem_add_lines_pair(
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005166 $rem->[$i], $add->[$i], $num_parents);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005167 push @new_rem, $line_rem;
5168 push @new_add, $line_add;
5169 }
5170 } else {
5171 @new_rem = map { format_diff_line($_, 'rem') } @$rem;
5172 @new_add = map { format_diff_line($_, 'add') } @$add;
5173 }
5174
5175 @new_ctx = map { format_diff_line($_, 'ctx') } @$ctx;
5176
5177 return (\@new_ctx, \@new_rem, \@new_add);
5178}
5179
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005180# Print context lines and then rem/add lines.
5181sub print_diff_lines {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005182 my ($ctx, $rem, $add, $diff_style, $num_parents) = @_;
5183 my $is_combined = $num_parents > 1;
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005184
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005185 ($ctx, $rem, $add) = format_ctx_rem_add_lines($ctx, $rem, $add,
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005186 $num_parents);
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005187
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005188 if ($diff_style eq 'sidebyside' && !$is_combined) {
5189 print_sidebyside_diff_lines($ctx, $rem, $add);
5190 } else {
5191 # default 'inline' style and unknown styles
5192 print_inline_diff_lines($ctx, $rem, $add);
5193 }
5194}
5195
5196sub print_diff_chunk {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005197 my ($diff_style, $num_parents, $from, $to, @chunk) = @_;
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005198 my (@ctx, @rem, @add);
5199
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005200 # The class of the previous line.
5201 my $prev_class = '';
5202
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005203 return unless @chunk;
5204
5205 # incomplete last line might be among removed or added lines,
5206 # or both, or among context lines: find which
5207 for (my $i = 1; $i < @chunk; $i++) {
5208 if ($chunk[$i][0] eq 'incomplete') {
5209 $chunk[$i][0] = $chunk[$i-1][0];
5210 }
5211 }
5212
5213 # guardian
5214 push @chunk, ["", ""];
5215
5216 foreach my $line_info (@chunk) {
5217 my ($class, $line) = @$line_info;
5218
5219 # print chunk headers
5220 if ($class && $class eq 'chunk_header') {
Michał Kiedrowicz5fb6ddf2012-04-11 23:18:43 +02005221 print format_diff_line($line, $class, $from, $to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005222 next;
5223 }
5224
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005225 ## print from accumulator when have some add/rem lines or end
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005226 # of chunk (flush context lines), or when have add and rem
5227 # lines and new block is reached (otherwise add/rem lines could
5228 # be reordered)
5229 if (!$class || ((@rem || @add) && $class eq 'ctx') ||
5230 (@rem && @add && $class ne $prev_class)) {
5231 print_diff_lines(\@ctx, \@rem, \@add,
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005232 $diff_style, $num_parents);
Michał Kiedrowiczd21102c2012-04-11 23:18:40 +02005233 @ctx = @rem = @add = ();
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005234 }
5235
5236 ## adding lines to accumulator
5237 # guardian value
5238 last unless $line;
5239 # rem, add or change
5240 if ($class eq 'rem') {
5241 push @rem, $line;
5242 } elsif ($class eq 'add') {
5243 push @add, $line;
5244 }
5245 # context line
5246 if ($class eq 'ctx') {
5247 push @ctx, $line;
5248 }
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005249
5250 $prev_class = $class;
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005251 }
5252}
5253
Jakub Narebskieee08902006-08-24 00:15:14 +02005254sub git_patchset_body {
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005255 my ($fd, $diff_style, $difftree, $hash, @hash_parents) = @_;
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02005256 my ($hash_parent) = $hash_parents[0];
Jakub Narebskieee08902006-08-24 00:15:14 +02005257
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005258 my $is_combined = (@hash_parents > 1);
Jakub Narebskieee08902006-08-24 00:15:14 +02005259 my $patch_idx = 0;
Martin Koegler4280cde2007-04-22 22:49:25 -07005260 my $patch_number = 0;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005261 my $patch_line;
Jakub Narebskife875852006-08-25 20:59:39 +02005262 my $diffinfo;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005263 my $to_name;
Jakub Narebski744d0ac2006-11-08 17:59:41 +01005264 my (%from, %to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005265 my @chunk; # for side-by-side diff
Jakub Narebskieee08902006-08-24 00:15:14 +02005266
5267 print "<div class=\"patchset\">\n";
5268
Jakub Narebski6d55f052006-11-18 23:35:39 +01005269 # skip to first patch
5270 while ($patch_line = <$fd>) {
Jakub Narebski157e43b2006-08-24 19:34:36 +02005271 chomp $patch_line;
Jakub Narebskieee08902006-08-24 00:15:14 +02005272
Jakub Narebski6d55f052006-11-18 23:35:39 +01005273 last if ($patch_line =~ m/^diff /);
5274 }
5275
5276 PATCH:
5277 while ($patch_line) {
Jakub Narebski6d55f052006-11-18 23:35:39 +01005278
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005279 # parse "git diff" header line
5280 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
5281 # $1 is from_name, which we do not use
5282 $to_name = unquote($2);
5283 $to_name =~ s!^b/!!;
5284 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
5285 # $1 is 'cc' or 'combined', which we do not use
5286 $to_name = unquote($2);
5287 } else {
5288 $to_name = undef;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005289 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01005290
5291 # check if current patch belong to current raw line
5292 # and parse raw git-diff line if needed
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005293 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
Jakub Narebski22065372007-05-12 12:42:32 +02005294 # this is continuation of a split patch
Jakub Narebski6d55f052006-11-18 23:35:39 +01005295 print "<div class=\"patch cont\">\n";
5296 } else {
5297 # advance raw git-diff output if needed
5298 $patch_idx++ if defined $diffinfo;
Jakub Narebskieee08902006-08-24 00:15:14 +02005299
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005300 # read and prepare patch information
5301 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
5302
Jakub Narebskicd030c32007-06-08 13:33:28 +02005303 # compact combined diff output can have some patches skipped
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005304 # find which patch (using pathname of result) we are at now;
5305 if ($is_combined) {
5306 while ($to_name ne $diffinfo->{'to_file'}) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02005307 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5308 format_diff_cc_simplified($diffinfo, @hash_parents) .
5309 "</div>\n"; # class="patch"
5310
5311 $patch_idx++;
5312 $patch_number++;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005313
5314 last if $patch_idx > $#$difftree;
5315 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02005316 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005317 }
Jakub Narebski711fa742007-09-08 21:49:11 +02005318
Jakub Narebski90921742007-06-08 13:27:42 +02005319 # modifies %from, %to hashes
5320 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
Jakub Narebski5f855052007-05-17 22:54:28 +02005321
Jakub Narebski6d55f052006-11-18 23:35:39 +01005322 # this is first patch for raw difftree line with $patch_idx index
5323 # we index @$difftree array from 0, but number patches from 1
5324 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
Jakub Narebski744d0ac2006-11-08 17:59:41 +01005325 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005326
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005327 # git diff header
5328 #assert($patch_line =~ m/^diff /) if DEBUG;
5329 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
5330 $patch_number++;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005331 # print "git diff" header
Jakub Narebski90921742007-06-08 13:27:42 +02005332 print format_git_diff_header_line($patch_line, $diffinfo,
5333 \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02005334
Jakub Narebski6d55f052006-11-18 23:35:39 +01005335 # print extended diff header
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005336 print "<div class=\"diff extended_header\">\n";
Jakub Narebski6d55f052006-11-18 23:35:39 +01005337 EXTENDED_HEADER:
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005338 while ($patch_line = <$fd>) {
5339 chomp $patch_line;
5340
5341 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
5342
Jakub Narebski90921742007-06-08 13:27:42 +02005343 print format_extended_diff_header_line($patch_line, $diffinfo,
5344 \%from, \%to);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005345 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005346 print "</div>\n"; # class="diff extended_header"
Jakub Narebskie4e4f822006-08-25 21:04:13 +02005347
Jakub Narebski6d55f052006-11-18 23:35:39 +01005348 # from-file/to-file diff header
Jakub Narebski0bdb28c2007-01-10 00:07:43 +01005349 if (! $patch_line) {
5350 print "</div>\n"; # class="patch"
5351 last PATCH;
5352 }
Jakub Narebski66399ef2007-01-07 02:52:25 +01005353 next PATCH if ($patch_line =~ m/^diff /);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005354 #assert($patch_line =~ m/^---/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005355
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005356 my $last_patch_line = $patch_line;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005357 $patch_line = <$fd>;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005358 chomp $patch_line;
Jakub Narebski90921742007-06-08 13:27:42 +02005359 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01005360
Jakub Narebski90921742007-06-08 13:27:42 +02005361 print format_diff_from_to_header($last_patch_line, $patch_line,
Jakub Narebski91af4ce2007-06-08 13:32:44 +02005362 $diffinfo, \%from, \%to,
5363 @hash_parents);
Jakub Narebski6d55f052006-11-18 23:35:39 +01005364
5365 # the patch itself
5366 LINE:
5367 while ($patch_line = <$fd>) {
5368 chomp $patch_line;
5369
5370 next PATCH if ($patch_line =~ m/^diff /);
5371
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02005372 my $class = diff_line_class($patch_line, \%from, \%to);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005373
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005374 if ($class eq 'chunk_header') {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005375 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005376 @chunk = ();
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005377 }
Michał Kiedrowicz44185f92012-04-11 23:18:41 +02005378
Michał Kiedrowiczf4a81022012-04-11 23:18:42 +02005379 push @chunk, [ $class, $patch_line ];
Jakub Narebskieee08902006-08-24 00:15:14 +02005380 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005381
Jakub Narebski6d55f052006-11-18 23:35:39 +01005382 } continue {
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005383 if (@chunk) {
Michał Kiedrowicz51ef7a62012-04-11 23:18:44 +02005384 print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01005385 @chunk = ();
5386 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01005387 print "</div>\n"; # class="patch"
Jakub Narebskieee08902006-08-24 00:15:14 +02005388 }
Jakub Narebskid26c4262007-05-17 00:05:55 +02005389
Ralf Wildenhues22e5e582010-08-22 13:12:12 +02005390 # for compact combined (--cc) format, with chunk and patch simplification
5391 # the patchset might be empty, but there might be unprocessed raw lines
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005392 for (++$patch_idx if $patch_number > 0;
Jakub Narebskicd030c32007-06-08 13:33:28 +02005393 $patch_idx < @$difftree;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005394 ++$patch_idx) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02005395 # read and prepare patch information
Jakub Narebski0cec6db2007-10-30 01:35:05 +01005396 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02005397
5398 # generate anchor for "patch" links in difftree / whatchanged part
5399 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
5400 format_diff_cc_simplified($diffinfo, @hash_parents) .
5401 "</div>\n"; # class="patch"
5402
5403 $patch_number++;
5404 }
5405
Jakub Narebskid26c4262007-05-17 00:05:55 +02005406 if ($patch_number == 0) {
5407 if (@hash_parents > 1) {
5408 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
5409 } else {
5410 print "<div class=\"diff nodifferences\">No differences found</div>\n";
5411 }
5412 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005413
5414 print "</div>\n"; # class="patchset"
5415}
5416
5417# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5418
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005419sub git_project_search_form {
Jakub Narebskib22939a2012-03-02 23:50:01 +01005420 my ($searchtext, $search_use_regexp) = @_;
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005421
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005422 my $limit = '';
5423 if ($project_filter) {
5424 $limit = " in '$project_filter/'";
5425 }
5426
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005427 print "<div class=\"projsearch\">\n";
5428 print $cgi->startform(-method => 'get', -action => $my_uri) .
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005429 $cgi->hidden(-name => 'a', -value => 'project_list') . "\n";
5430 print $cgi->hidden(-name => 'pf', -value => $project_filter). "\n"
5431 if (defined $project_filter);
5432 print $cgi->textfield(-name => 's', -value => $searchtext,
5433 -title => "Search project by name and description$limit",
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005434 -size => 60) . "\n" .
5435 "<span title=\"Extended regular expression\">" .
5436 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
5437 -checked => $search_use_regexp) .
5438 "</span>\n" .
5439 $cgi->submit(-name => 'btnS', -value => 'Search') .
5440 $cgi->end_form() . "\n" .
Jakub Narebskiabc0c9d2012-01-31 01:20:55 +01005441 $cgi->a({-href => href(project => undef, searchtext => undef,
5442 project_filter => $project_filter)},
5443 esc_html("List all projects$limit")) . "<br />\n";
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01005444 print "</div>\n";
5445}
5446
Jakub Narebski14b289b2012-02-23 16:54:46 +01005447# entry for given @keys needs filling if at least one of keys in list
5448# is not present in %$project_info
5449sub project_info_needs_filling {
5450 my ($project_info, @keys) = @_;
5451
5452 # return List::MoreUtils::any { !exists $project_info->{$_} } @keys;
5453 foreach my $key (@keys) {
5454 if (!exists $project_info->{$key}) {
5455 return 1;
5456 }
5457 }
5458 return;
5459}
5460
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005461# fills project list info (age, description, owner, category, forks, etc.)
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005462# for each project in the list, removing invalid projects from
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005463# returned list, or fill only specified info.
5464#
5465# Invalid projects are removed from the returned list if and only if you
5466# ask 'age' or 'age_string' to be filled, because they are the only fields
5467# that run unconditionally git command that requires repository, and
5468# therefore do always check if project repository is invalid.
5469#
5470# USAGE:
5471# * fill_project_list_info(\@project_list, 'descr_long', 'ctags')
5472# ensures that 'descr_long' and 'ctags' fields are filled
5473# * @project_list = fill_project_list_info(\@project_list)
5474# ensures that all fields are filled (and invalid projects removed)
5475#
Jakub Narebski69913412008-06-10 19:21:01 +02005476# NOTE: modifies $projlist, but does not remove entries from it
5477sub fill_project_list_info {
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005478 my ($projlist, @wanted_keys) = @_;
Petr Baudise30496d2006-10-24 05:33:17 +02005479 my @projects;
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005480 my $filter_set = sub { return @_; };
5481 if (@wanted_keys) {
5482 my %wanted_keys = map { $_ => 1 } @wanted_keys;
5483 $filter_set = sub { return grep { $wanted_keys{$_} } @_; };
5484 }
Jakub Narebski69913412008-06-10 19:21:01 +02005485
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005486 my $show_ctags = gitweb_check_feature('ctags');
Jakub Narebski69913412008-06-10 19:21:01 +02005487 PROJECT:
Petr Baudise30496d2006-10-24 05:33:17 +02005488 foreach my $pr (@$projlist) {
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005489 if (project_info_needs_filling($pr, $filter_set->('age', 'age_string'))) {
Jakub Narebski14b289b2012-02-23 16:54:46 +01005490 my (@activity) = git_get_last_activity($pr->{'path'});
5491 unless (@activity) {
5492 next PROJECT;
5493 }
5494 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
Petr Baudise30496d2006-10-24 05:33:17 +02005495 }
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005496 if (project_info_needs_filling($pr, $filter_set->('descr', 'descr_long'))) {
Petr Baudise30496d2006-10-24 05:33:17 +02005497 my $descr = git_get_project_description($pr->{'path'}) || "";
Jakub Narebski69913412008-06-10 19:21:01 +02005498 $descr = to_utf8($descr);
5499 $pr->{'descr_long'} = $descr;
Michael Hendricks55feb122007-07-04 18:36:48 -06005500 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
Petr Baudise30496d2006-10-24 05:33:17 +02005501 }
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005502 if (project_info_needs_filling($pr, $filter_set->('owner'))) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02005503 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
Petr Baudise30496d2006-10-24 05:33:17 +02005504 }
Jakub Narebski14b289b2012-02-23 16:54:46 +01005505 if ($show_ctags &&
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005506 project_info_needs_filling($pr, $filter_set->('ctags'))) {
Jakub Narebski12b14432011-04-29 19:51:56 +02005507 $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
Petr Baudise30496d2006-10-24 05:33:17 +02005508 }
Jakub Narebski14b289b2012-02-23 16:54:46 +01005509 if ($projects_list_group_categories &&
Jakub Narebski2e3291a2012-02-23 16:54:47 +01005510 project_info_needs_filling($pr, $filter_set->('category'))) {
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005511 my $cat = git_get_project_category($pr->{'path'}) ||
5512 $project_list_default_category;
5513 $pr->{'category'} = to_utf8($cat);
5514 }
5515
Petr Baudise30496d2006-10-24 05:33:17 +02005516 push @projects, $pr;
5517 }
5518
Jakub Narebski69913412008-06-10 19:21:01 +02005519 return @projects;
5520}
5521
Jakub Narebski12b14432011-04-29 19:51:56 +02005522sub sort_projects_list {
5523 my ($projlist, $order) = @_;
5524 my @projects;
5525
5526 my %order_info = (
5527 project => { key => 'path', type => 'str' },
5528 descr => { key => 'descr_long', type => 'str' },
5529 owner => { key => 'owner', type => 'str' },
5530 age => { key => 'age', type => 'num' }
5531 );
5532 my $oi = $order_info{$order};
5533 return @$projlist unless defined $oi;
5534 if ($oi->{'type'} eq 'str') {
5535 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @$projlist;
5536 } else {
5537 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @$projlist;
5538 }
5539
5540 return @projects;
5541}
5542
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005543# returns a hash of categories, containing the list of project
5544# belonging to each category
5545sub build_projlist_by_category {
5546 my ($projlist, $from, $to) = @_;
5547 my %categories;
5548
5549 $from = 0 unless defined $from;
5550 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5551
5552 for (my $i = $from; $i <= $to; $i++) {
5553 my $pr = $projlist->[$i];
5554 push @{$categories{ $pr->{'category'} }}, $pr;
5555 }
5556
5557 return wantarray ? %categories : \%categories;
5558}
5559
Petr Baudis6b28da62008-09-25 18:48:37 +02005560# print 'sort by' <th> element, generating 'sort by $name' replay link
5561# if that order is not selected
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005562sub print_sort_th {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005563 print format_sort_th(@_);
5564}
5565
5566sub format_sort_th {
Petr Baudis6b28da62008-09-25 18:48:37 +02005567 my ($name, $order, $header) = @_;
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005568 my $sort_th = "";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005569 $header ||= ucfirst($name);
5570
5571 if ($order eq $name) {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005572 $sort_th .= "<th>$header</th>\n";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005573 } else {
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005574 $sort_th .= "<th>" .
5575 $cgi->a({-href => href(-replay=>1, order=>$name),
5576 -class => "header"}, $header) .
5577 "</th>\n";
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005578 }
John 'Warthog9' Hawley1ee4b4e2010-01-30 23:30:43 +01005579
5580 return $sort_th;
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005581}
5582
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005583sub git_project_list_rows {
5584 my ($projlist, $from, $to, $check_forks) = @_;
5585
5586 $from = 0 unless defined $from;
5587 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5588
5589 my $alternate = 1;
5590 for (my $i = $from; $i <= $to; $i++) {
5591 my $pr = $projlist->[$i];
5592
5593 if ($alternate) {
5594 print "<tr class=\"dark\">\n";
5595 } else {
5596 print "<tr class=\"light\">\n";
5597 }
5598 $alternate ^= 1;
5599
5600 if ($check_forks) {
5601 print "<td>";
5602 if ($pr->{'forks'}) {
5603 my $nforks = scalar @{$pr->{'forks'}};
5604 if ($nforks > 0) {
5605 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5606 -title => "$nforks forks"}, "+");
5607 } else {
5608 print $cgi->span({-title => "$nforks forks"}, "+");
5609 }
5610 }
5611 print "</td>\n";
5612 }
5613 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
Jakub Narebski07a40062012-02-27 02:55:20 +01005614 -class => "list"},
5615 esc_html_match_hl($pr->{'path'}, $search_regexp)) .
5616 "</td>\n" .
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005617 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005618 -class => "list",
Jakub Narebskie607b792012-02-27 02:55:22 +01005619 -title => $pr->{'descr_long'}},
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005620 $search_regexp
Jakub Narebskie607b792012-02-27 02:55:22 +01005621 ? esc_html_match_hl_chopped($pr->{'descr_long'},
5622 $pr->{'descr'}, $search_regexp)
Jakub Narebski5fb3cf22012-02-27 02:55:21 +01005623 : esc_html($pr->{'descr'})) .
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005624 "</td>\n";
5625 unless ($omit_owner) {
5626 print "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5627 }
Kacper Kornet5710be42012-04-24 19:39:15 +02005628 unless ($omit_age_column) {
5629 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5630 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n";
5631 }
5632 print"<td class=\"link\">" .
Sebastien Cevey0fa920c2011-04-29 19:51:59 +02005633 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
5634 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5635 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5636 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5637 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5638 "</td>\n" .
5639 "</tr>\n";
5640 }
5641}
5642
Jakub Narebski69913412008-06-10 19:21:01 +02005643sub git_project_list_body {
Petr Baudis42326112008-10-02 17:17:01 +02005644 # actually uses global variable $project
Jakub Narebski69913412008-06-10 19:21:01 +02005645 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
Jakub Narebski12b14432011-04-29 19:51:56 +02005646 my @projects = @$projlist;
Jakub Narebski69913412008-06-10 19:21:01 +02005647
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005648 my $check_forks = gitweb_check_feature('forks');
Jakub Narebski12b14432011-04-29 19:51:56 +02005649 my $show_ctags = gitweb_check_feature('ctags');
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01005650 my $tagfilter = $show_ctags ? $input_params{'ctag'} : undef;
Jakub Narebski12b14432011-04-29 19:51:56 +02005651 $check_forks = undef
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005652 if ($tagfilter || $search_regexp);
Jakub Narebski12b14432011-04-29 19:51:56 +02005653
5654 # filtering out forks before filling info allows to do less work
5655 @projects = filter_forks_from_projects_list(\@projects)
5656 if ($check_forks);
Jakub Narebski07b257f2012-02-23 16:54:48 +01005657 # search_projects_list pre-fills required info
Jakub Narebski12b14432011-04-29 19:51:56 +02005658 @projects = search_projects_list(\@projects,
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005659 'search_regexp' => $search_regexp,
Jakub Narebski12b14432011-04-29 19:51:56 +02005660 'tagfilter' => $tagfilter)
Jakub Narebskie65ceb62012-03-02 23:34:24 +01005661 if ($tagfilter || $search_regexp);
Jakub Narebski07b257f2012-02-23 16:54:48 +01005662 # fill the rest
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005663 my @all_fields = ('descr', 'descr_long', 'ctags', 'category');
5664 push @all_fields, ('age', 'age_string') unless($omit_age_column);
5665 push @all_fields, 'owner' unless($omit_owner);
Kacper Kornet5710be42012-04-24 19:39:15 +02005666 @projects = fill_project_list_info(\@projects, @all_fields);
Jakub Narebski69913412008-06-10 19:21:01 +02005667
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02005668 $order ||= $default_projects_order;
Petr Baudise30496d2006-10-24 05:33:17 +02005669 $from = 0 unless defined $from;
5670 $to = $#projects if (!defined $to || $#projects < $to);
5671
Jakub Narebski12b14432011-04-29 19:51:56 +02005672 # short circuit
5673 if ($from > $to) {
5674 print "<center>\n".
5675 "<b>No such projects found</b><br />\n".
5676 "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
5677 "</center>\n<br />\n";
5678 return;
Petr Baudis6b28da62008-09-25 18:48:37 +02005679 }
5680
Jakub Narebski12b14432011-04-29 19:51:56 +02005681 @projects = sort_projects_list(\@projects, $order);
5682
Petr Baudisaed93de2008-10-02 17:13:02 +02005683 if ($show_ctags) {
Jakub Narebski0368c492011-04-29 19:51:57 +02005684 my $ctags = git_gather_all_ctags(\@projects);
5685 my $cloud = git_populate_project_tagcloud($ctags);
Petr Baudisaed93de2008-10-02 17:13:02 +02005686 print git_show_project_tagcloud($cloud, 64);
5687 }
5688
Petr Baudise30496d2006-10-24 05:33:17 +02005689 print "<table class=\"project_list\">\n";
5690 unless ($no_header) {
5691 print "<tr>\n";
5692 if ($check_forks) {
5693 print "<th></th>\n";
5694 }
Petr Baudis6b28da62008-09-25 18:48:37 +02005695 print_sort_th('project', $order, 'Project');
5696 print_sort_th('descr', $order, 'Description');
Kacper Kornet0ebe7822012-04-26 18:45:44 +02005697 print_sort_th('owner', $order, 'Owner') unless $omit_owner;
Kacper Kornet5710be42012-04-24 19:39:15 +02005698 print_sort_th('age', $order, 'Last Change') unless $omit_age_column;
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02005699 print "<th></th>\n" . # for links
Petr Baudise30496d2006-10-24 05:33:17 +02005700 "</tr>\n";
5701 }
Petr Baudis42326112008-10-02 17:17:01 +02005702
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005703 if ($projects_list_group_categories) {
5704 # only display categories with projects in the $from-$to window
5705 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5706 my %categories = build_projlist_by_category(\@projects, $from, $to);
5707 foreach my $cat (sort keys %categories) {
5708 unless ($cat eq "") {
5709 print "<tr>\n";
5710 if ($check_forks) {
5711 print "<td></td>\n";
Jakub Narebski12b14432011-04-29 19:51:56 +02005712 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005713 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5714 print "</tr>\n";
Petr Baudise30496d2006-10-24 05:33:17 +02005715 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005716
5717 git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
Petr Baudise30496d2006-10-24 05:33:17 +02005718 }
Sebastien Ceveyd940c902011-04-29 19:52:01 +02005719 } else {
5720 git_project_list_rows(\@projects, $from, $to, $check_forks);
Petr Baudise30496d2006-10-24 05:33:17 +02005721 }
Jakub Narebski12b14432011-04-29 19:51:56 +02005722
Petr Baudise30496d2006-10-24 05:33:17 +02005723 if (defined $extra) {
5724 print "<tr>\n";
5725 if ($check_forks) {
5726 print "<td></td>\n";
5727 }
5728 print "<td colspan=\"5\">$extra</td>\n" .
5729 "</tr>\n";
5730 }
5731 print "</table>\n";
5732}
5733
Jakub Narebski42671ca2009-11-13 02:02:12 +01005734sub git_log_body {
5735 # uses global variable $project
5736 my ($commitlist, $from, $to, $refs, $extra) = @_;
5737
5738 $from = 0 unless defined $from;
5739 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5740
5741 for (my $i = 0; $i <= $to; $i++) {
5742 my %co = %{$commitlist->[$i]};
5743 next if !%co;
5744 my $commit = $co{'id'};
5745 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski42671ca2009-11-13 02:02:12 +01005746 git_print_header_div('commit',
5747 "<span class=\"age\">$co{'age_string'}</span>" .
5748 esc_html($co{'title'}) . $ref,
5749 $commit);
5750 print "<div class=\"title_text\">\n" .
5751 "<div class=\"log_link\">\n" .
5752 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5753 " | " .
5754 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5755 " | " .
5756 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5757 "<br/>\n" .
5758 "</div>\n";
5759 git_print_authorship(\%co, -tag => 'span');
5760 print "<br/>\n</div>\n";
5761
5762 print "<div class=\"log_body\">\n";
5763 git_print_log($co{'comment'}, -final_empty_line=> 1);
5764 print "</div>\n";
5765 }
5766 if ($extra) {
5767 print "<div class=\"page_nav\">\n";
5768 print "$extra\n";
5769 print "</div>\n";
5770 }
5771}
5772
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005773sub git_shortlog_body {
5774 # uses global variable $project
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005775 my ($commitlist, $from, $to, $refs, $extra) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05305776
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005777 $from = 0 unless defined $from;
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005778 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005779
Jakub Narebski591ebf62007-11-19 14:16:11 +01005780 print "<table class=\"shortlog\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005781 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005782 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005783 my %co = %{$commitlist->[$i]};
5784 my $commit = $co{'id'};
Jakub Narebski847e01f2006-08-14 02:05:47 +02005785 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005786 if ($alternate) {
5787 print "<tr class=\"dark\">\n";
5788 } else {
5789 print "<tr class=\"light\">\n";
5790 }
5791 $alternate ^= 1;
5792 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5793 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02005794 format_author_html('td', \%co, 10) . "<td>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02005795 print format_subject_html($co{'title'}, $co{'title_short'},
5796 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005797 print "</td>\n" .
5798 "<td class=\"link\">" .
Petr Baudis4777b012006-10-24 05:36:10 +02005799 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
Petr Baudis35749ae2006-09-22 03:19:44 +02005800 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
Petr Baudis55ff35c2006-10-06 15:57:52 +02005801 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02005802 my $snapshot_links = format_snapshot_links($commit);
5803 if (defined $snapshot_links) {
5804 print " | " . $snapshot_links;
Petr Baudis55ff35c2006-10-06 15:57:52 +02005805 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305806 print "</td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005807 "</tr>\n";
5808 }
5809 if (defined $extra) {
5810 print "<tr>\n" .
5811 "<td colspan=\"4\">$extra</td>\n" .
5812 "</tr>\n";
5813 }
5814 print "</table>\n";
5815}
5816
Jakub Narebski581860e2006-08-14 02:09:08 +02005817sub git_history_body {
5818 # Warning: assumes constant type (blob or tree) during history
Jakub Narebski69ca37d2009-11-13 02:02:14 +01005819 my ($commitlist, $from, $to, $refs, $extra,
5820 $file_name, $file_hash, $ftype) = @_;
Jakub Narebski8be68352006-09-11 00:36:04 +02005821
5822 $from = 0 unless defined $from;
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005823 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
Jakub Narebski581860e2006-08-14 02:09:08 +02005824
Jakub Narebski591ebf62007-11-19 14:16:11 +01005825 print "<table class=\"history\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005826 my $alternate = 1;
Jakub Narebski8be68352006-09-11 00:36:04 +02005827 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005828 my %co = %{$commitlist->[$i]};
Jakub Narebski581860e2006-08-14 02:09:08 +02005829 if (!%co) {
5830 next;
5831 }
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005832 my $commit = $co{'id'};
Jakub Narebski581860e2006-08-14 02:09:08 +02005833
5834 my $ref = format_ref_marker($refs, $commit);
5835
5836 if ($alternate) {
5837 print "<tr class=\"dark\">\n";
5838 } else {
5839 print "<tr class=\"light\">\n";
5840 }
5841 $alternate ^= 1;
5842 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02005843 # shortlog: format_author_html('td', \%co, 10)
5844 format_author_html('td', \%co, 15, 3) . "<td>";
Jakub Narebski581860e2006-08-14 02:09:08 +02005845 # originally git_history used chop_str($co{'title'}, 50)
Jakub Narebski952c65f2006-08-22 16:52:50 +02005846 print format_subject_html($co{'title'}, $co{'title_short'},
5847 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski581860e2006-08-14 02:09:08 +02005848 print "</td>\n" .
5849 "<td class=\"link\">" .
Luben Tuikov6d81c5a2006-09-28 17:21:07 -07005850 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
5851 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
Jakub Narebski581860e2006-08-14 02:09:08 +02005852
5853 if ($ftype eq 'blob') {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01005854 my $blob_current = $file_hash;
Jakub Narebski581860e2006-08-14 02:09:08 +02005855 my $blob_parent = git_get_hash_by_path($commit, $file_name);
5856 if (defined $blob_current && defined $blob_parent &&
5857 $blob_current ne $blob_parent) {
5858 print " | " .
Jakub Narebski420e92f2006-08-24 23:53:54 +02005859 $cgi->a({-href => href(action=>"blobdiff",
5860 hash=>$blob_current, hash_parent=>$blob_parent,
5861 hash_base=>$hash_base, hash_parent_base=>$commit,
5862 file_name=>$file_name)},
Jakub Narebski581860e2006-08-14 02:09:08 +02005863 "diff to current");
5864 }
5865 }
5866 print "</td>\n" .
5867 "</tr>\n";
5868 }
5869 if (defined $extra) {
5870 print "<tr>\n" .
5871 "<td colspan=\"4\">$extra</td>\n" .
5872 "</tr>\n";
5873 }
5874 print "</table>\n";
5875}
5876
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005877sub git_tags_body {
5878 # uses global variable $project
5879 my ($taglist, $from, $to, $extra) = @_;
5880 $from = 0 unless defined $from;
5881 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5882
Jakub Narebski591ebf62007-11-19 14:16:11 +01005883 print "<table class=\"tags\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005884 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005885 for (my $i = $from; $i <= $to; $i++) {
5886 my $entry = $taglist->[$i];
5887 my %tag = %$entry;
Jakub Narebskicd146402006-11-02 20:23:11 +01005888 my $comment = $tag{'subject'};
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005889 my $comment_short;
5890 if (defined $comment) {
5891 $comment_short = chop_str($comment, 30, 5);
5892 }
5893 if ($alternate) {
5894 print "<tr class=\"dark\">\n";
5895 } else {
5896 print "<tr class=\"light\">\n";
5897 }
5898 $alternate ^= 1;
Jakub Narebski27dd1a82007-01-03 22:54:29 +01005899 if (defined $tag{'age'}) {
5900 print "<td><i>$tag{'age'}</i></td>\n";
5901 } else {
5902 print "<td></td>\n";
5903 }
5904 print "<td>" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005905 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02005906 -class => "list name"}, esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005907 "</td>\n" .
5908 "<td>";
5909 if (defined $comment) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02005910 print format_subject_html($comment, $comment_short,
5911 href(action=>"tag", hash=>$tag{'id'}));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005912 }
5913 print "</td>\n" .
5914 "<td class=\"selflink\">";
5915 if ($tag{'type'} eq "tag") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005916 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005917 } else {
5918 print "&nbsp;";
5919 }
5920 print "</td>\n" .
5921 "<td class=\"link\">" . " | " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005922 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005923 if ($tag{'reftype'} eq "commit") {
Jakub Narebskibf901f82007-12-15 15:40:28 +01005924 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
5925 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005926 } elsif ($tag{'reftype'} eq "blob") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005927 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005928 }
5929 print "</td>\n" .
5930 "</tr>";
5931 }
5932 if (defined $extra) {
5933 print "<tr>\n" .
5934 "<td colspan=\"5\">$extra</td>\n" .
5935 "</tr>\n";
5936 }
5937 print "</table>\n";
5938}
5939
5940sub git_heads_body {
5941 # uses global variable $project
Jakub Narebskifd49e562012-02-15 16:36:41 +01005942 my ($headlist, $head_at, $from, $to, $extra) = @_;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005943 $from = 0 unless defined $from;
Jakub Narebski120ddde2006-09-19 14:33:22 +02005944 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005945
Jakub Narebski591ebf62007-11-19 14:16:11 +01005946 print "<table class=\"heads\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07005947 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005948 for (my $i = $from; $i <= $to; $i++) {
Jakub Narebski120ddde2006-09-19 14:33:22 +02005949 my $entry = $headlist->[$i];
Jakub Narebskicd146402006-11-02 20:23:11 +01005950 my %ref = %$entry;
Jakub Narebskifd49e562012-02-15 16:36:41 +01005951 my $curr = defined $head_at && $ref{'id'} eq $head_at;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005952 if ($alternate) {
5953 print "<tr class=\"dark\">\n";
5954 } else {
5955 print "<tr class=\"light\">\n";
5956 }
5957 $alternate ^= 1;
Jakub Narebskicd146402006-11-02 20:23:11 +01005958 print "<td><i>$ref{'age'}</i></td>\n" .
5959 ($curr ? "<td class=\"current_head\">" : "<td>") .
Jakub Narebskibf901f82007-12-15 15:40:28 +01005960 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
Jakub Narebskicd146402006-11-02 20:23:11 +01005961 -class => "list name"},esc_html($ref{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005962 "</td>\n" .
5963 "<td class=\"link\">" .
Jakub Narebskibf901f82007-12-15 15:40:28 +01005964 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
5965 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
Giuseppe Bilotta9e70e152010-11-11 13:26:08 +01005966 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005967 "</td>\n" .
5968 "</tr>";
5969 }
5970 if (defined $extra) {
5971 print "<tr>\n" .
5972 "<td colspan=\"3\">$extra</td>\n" .
5973 "</tr>\n";
5974 }
5975 print "</table>\n";
5976}
5977
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01005978# Display a single remote block
5979sub git_remote_block {
5980 my ($remote, $rdata, $limit, $head) = @_;
5981
5982 my $heads = $rdata->{'heads'};
5983 my $fetch = $rdata->{'fetch'};
5984 my $push = $rdata->{'push'};
5985
5986 my $urls_table = "<table class=\"projects_list\">\n" ;
5987
5988 if (defined $fetch) {
5989 if ($fetch eq $push) {
5990 $urls_table .= format_repo_url("URL", $fetch);
5991 } else {
5992 $urls_table .= format_repo_url("Fetch URL", $fetch);
5993 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
5994 }
5995 } elsif (defined $push) {
5996 $urls_table .= format_repo_url("Push URL", $push);
5997 } else {
5998 $urls_table .= format_repo_url("", "No remote URL");
5999 }
6000
6001 $urls_table .= "</table>\n";
6002
6003 my $dots;
6004 if (defined $limit && $limit < @$heads) {
6005 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
6006 }
6007
6008 print $urls_table;
6009 git_heads_body($heads, $head, 0, $limit, $dots);
6010}
6011
6012# Display a list of remote names with the respective fetch and push URLs
6013sub git_remotes_list {
6014 my ($remotedata, $limit) = @_;
6015 print "<table class=\"heads\">\n";
6016 my $alternate = 1;
6017 my @remotes = sort keys %$remotedata;
6018
6019 my $limited = $limit && $limit < @remotes;
6020
6021 $#remotes = $limit - 1 if $limited;
6022
6023 while (my $remote = shift @remotes) {
6024 my $rdata = $remotedata->{$remote};
6025 my $fetch = $rdata->{'fetch'};
6026 my $push = $rdata->{'push'};
6027 if ($alternate) {
6028 print "<tr class=\"dark\">\n";
6029 } else {
6030 print "<tr class=\"light\">\n";
6031 }
6032 $alternate ^= 1;
6033 print "<td>" .
6034 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
6035 -class=> "list name"},esc_html($remote)) .
6036 "</td>";
6037 print "<td class=\"link\">" .
6038 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
6039 " | " .
6040 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
6041 "</td>";
6042
6043 print "</tr>\n";
6044 }
6045
6046 if ($limited) {
6047 print "<tr>\n" .
6048 "<td colspan=\"3\">" .
6049 $cgi->a({-href => href(action=>"remotes")}, "...") .
6050 "</td>\n" . "</tr>\n";
6051 }
6052
6053 print "</table>";
6054}
6055
6056# Display remote heads grouped by remote, unless there are too many
6057# remotes, in which case we only display the remote names
6058sub git_remotes_body {
6059 my ($remotedata, $limit, $head) = @_;
6060 if ($limit and $limit < keys %$remotedata) {
6061 git_remotes_list($remotedata, $limit);
6062 } else {
6063 fill_remote_heads($remotedata);
6064 while (my ($remote, $rdata) = each %$remotedata) {
6065 git_print_section({-class=>"remote", -id=>$remote},
6066 ["remotes", $remote, $remote], sub {
6067 git_remote_block($remote, $rdata, $limit, $head);
6068 });
6069 }
6070 }
6071}
6072
Jakub Narebski16f20722011-06-22 17:28:53 +02006073sub git_search_message {
6074 my %co = @_;
6075
6076 my $greptype;
6077 if ($searchtype eq 'commit') {
6078 $greptype = "--grep=";
6079 } elsif ($searchtype eq 'author') {
6080 $greptype = "--author=";
6081 } elsif ($searchtype eq 'committer') {
6082 $greptype = "--committer=";
6083 }
6084 $greptype .= $searchtext;
6085 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6086 $greptype, '--regexp-ignore-case',
6087 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6088
6089 my $paging_nav = '';
6090 if ($page > 0) {
6091 $paging_nav .=
Jakub Narebski882541b2011-06-22 17:28:54 +02006092 $cgi->a({-href => href(-replay=>1, page=>undef)},
6093 "first") .
6094 " &sdot; " .
Jakub Narebski16f20722011-06-22 17:28:53 +02006095 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6096 -accesskey => "p", -title => "Alt-p"}, "prev");
6097 } else {
Jakub Narebski882541b2011-06-22 17:28:54 +02006098 $paging_nav .= "first &sdot; prev";
Jakub Narebski16f20722011-06-22 17:28:53 +02006099 }
6100 my $next_link = '';
6101 if ($#commitlist >= 100) {
6102 $next_link =
6103 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6104 -accesskey => "n", -title => "Alt-n"}, "next");
6105 $paging_nav .= " &sdot; $next_link";
6106 } else {
6107 $paging_nav .= " &sdot; next";
6108 }
6109
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006110 git_header_html();
6111
Jakub Narebski16f20722011-06-22 17:28:53 +02006112 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6113 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6114 if ($page == 0 && !@commitlist) {
6115 print "<p>No match.</p>\n";
6116 } else {
6117 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6118 }
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006119
6120 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006121}
6122
6123sub git_search_changes {
6124 my %co = @_;
6125
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006126 local $/ = "\n";
6127 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6128 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6129 ($search_use_regexp ? '--pickaxe-regex' : ())
6130 or die_error(500, "Open git-log failed");
6131
6132 git_header_html();
6133
Jakub Narebski16f20722011-06-22 17:28:53 +02006134 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6135 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6136
6137 print "<table class=\"pickaxe search\">\n";
6138 my $alternate = 1;
Jakub Narebski16f20722011-06-22 17:28:53 +02006139 undef %co;
6140 my @files;
6141 while (my $line = <$fd>) {
6142 chomp $line;
6143 next unless $line;
6144
6145 my %set = parse_difftree_raw_line($line);
6146 if (defined $set{'commit'}) {
6147 # finish previous commit
6148 if (%co) {
6149 print "</td>\n" .
6150 "<td class=\"link\">" .
Jakub Narebski882541b2011-06-22 17:28:54 +02006151 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6152 "commit") .
Jakub Narebski16f20722011-06-22 17:28:53 +02006153 " | " .
Jakub Narebski882541b2011-06-22 17:28:54 +02006154 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6155 hash_base=>$co{'id'})},
6156 "tree") .
6157 "</td>\n" .
Jakub Narebski16f20722011-06-22 17:28:53 +02006158 "</tr>\n";
6159 }
6160
6161 if ($alternate) {
6162 print "<tr class=\"dark\">\n";
6163 } else {
6164 print "<tr class=\"light\">\n";
6165 }
6166 $alternate ^= 1;
6167 %co = parse_commit($set{'commit'});
6168 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6169 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6170 "<td><i>$author</i></td>\n" .
6171 "<td>" .
6172 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6173 -class => "list subject"},
6174 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6175 } elsif (defined $set{'to_id'}) {
6176 next if ($set{'to_id'} =~ m/^0{40}$/);
6177
6178 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6179 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6180 -class => "list"},
6181 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6182 "<br/>\n";
6183 }
6184 }
6185 close $fd;
6186
6187 # finish last commit (warning: repetition!)
6188 if (%co) {
6189 print "</td>\n" .
6190 "<td class=\"link\">" .
Jakub Narebski882541b2011-06-22 17:28:54 +02006191 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
6192 "commit") .
Jakub Narebski16f20722011-06-22 17:28:53 +02006193 " | " .
Jakub Narebski882541b2011-06-22 17:28:54 +02006194 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
6195 hash_base=>$co{'id'})},
6196 "tree") .
6197 "</td>\n" .
Jakub Narebski16f20722011-06-22 17:28:53 +02006198 "</tr>\n";
6199 }
6200
6201 print "</table>\n";
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006202
6203 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006204}
6205
6206sub git_search_files {
6207 my %co = @_;
6208
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006209 local $/ = "\n";
Jakub Narebski8e09fd12012-01-05 21:32:56 +01006210 open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006211 $search_use_regexp ? ('-E', '-i') : '-F',
6212 $searchtext, $co{'tree'}
6213 or die_error(500, "Open git-grep failed");
6214
6215 git_header_html();
6216
Jakub Narebski16f20722011-06-22 17:28:53 +02006217 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6218 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6219
6220 print "<table class=\"grep_search\">\n";
6221 my $alternate = 1;
6222 my $matches = 0;
Jakub Narebski16f20722011-06-22 17:28:53 +02006223 my $lastfile = '';
Jakub Narebskifc8fcd22012-02-15 17:37:06 +01006224 my $file_href;
Jakub Narebski16f20722011-06-22 17:28:53 +02006225 while (my $line = <$fd>) {
6226 chomp $line;
Jakub Narebskifc8fcd22012-02-15 17:37:06 +01006227 my ($file, $lno, $ltext, $binary);
Jakub Narebski16f20722011-06-22 17:28:53 +02006228 last if ($matches++ > 1000);
6229 if ($line =~ /^Binary file (.+) matches$/) {
6230 $file = $1;
6231 $binary = 1;
6232 } else {
Jakub Narebski8e09fd12012-01-05 21:32:56 +01006233 ($file, $lno, $ltext) = split(/\0/, $line, 3);
6234 $file =~ s/^$co{'tree'}://;
Jakub Narebski16f20722011-06-22 17:28:53 +02006235 }
6236 if ($file ne $lastfile) {
6237 $lastfile and print "</td></tr>\n";
6238 if ($alternate++) {
6239 print "<tr class=\"dark\">\n";
6240 } else {
6241 print "<tr class=\"light\">\n";
6242 }
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006243 $file_href = href(action=>"blob", hash_base=>$co{'id'},
6244 file_name=>$file);
Jakub Narebski16f20722011-06-22 17:28:53 +02006245 print "<td class=\"list\">".
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006246 $cgi->a({-href => $file_href, -class => "list"}, esc_path($file));
Jakub Narebski16f20722011-06-22 17:28:53 +02006247 print "</td><td>\n";
6248 $lastfile = $file;
6249 }
6250 if ($binary) {
6251 print "<div class=\"binary\">Binary file</div>\n";
6252 } else {
6253 $ltext = untabify($ltext);
6254 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6255 $ltext = esc_html($1, -nbsp=>1);
6256 $ltext .= '<span class="match">';
6257 $ltext .= esc_html($2, -nbsp=>1);
6258 $ltext .= '</span>';
6259 $ltext .= esc_html($3, -nbsp=>1);
6260 } else {
6261 $ltext = esc_html($ltext, -nbsp=>1);
6262 }
6263 print "<div class=\"pre\">" .
Jakub Narebskiff7f2182012-01-05 21:26:48 +01006264 $cgi->a({-href => $file_href.'#l'.$lno,
6265 -class => "linenr"}, sprintf('%4i', $lno)) .
6266 ' ' . $ltext . "</div>\n";
Jakub Narebski16f20722011-06-22 17:28:53 +02006267 }
6268 }
6269 if ($lastfile) {
6270 print "</td></tr>\n";
6271 if ($matches > 1000) {
6272 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6273 }
6274 } else {
6275 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6276 }
6277 close $fd;
6278
6279 print "</table>\n";
Jakub Narebski1ae05be2011-06-22 17:28:55 +02006280
6281 git_footer_html();
Jakub Narebski16f20722011-06-22 17:28:53 +02006282}
6283
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006284sub git_search_grep_body {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006285 my ($commitlist, $from, $to, $extra) = @_;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006286 $from = 0 unless defined $from;
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006287 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006288
Jakub Narebski591ebf62007-11-19 14:16:11 +01006289 print "<table class=\"commit_search\">\n";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006290 my $alternate = 1;
6291 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006292 my %co = %{$commitlist->[$i]};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006293 if (!%co) {
6294 next;
6295 }
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00006296 my $commit = $co{'id'};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006297 if ($alternate) {
6298 print "<tr class=\"dark\">\n";
6299 } else {
6300 print "<tr class=\"light\">\n";
6301 }
6302 $alternate ^= 1;
6303 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02006304 format_author_html('td', \%co, 15, 5) .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006305 "<td>" .
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006306 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6307 -class => "list subject"},
6308 chop_and_escape_str($co{'title'}, 50) . "<br/>");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006309 my $comment = $co{'comment'};
6310 foreach my $line (@$comment) {
Jakub Narebski6dfbb302008-03-02 16:57:14 +01006311 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006312 my ($lead, $match, $trail) = ($1, $2, $3);
Jakub Narebskib8d97d02008-02-25 21:07:57 +01006313 $match = chop_str($match, 70, 5, 'center');
6314 my $contextlen = int((80 - length($match))/2);
6315 $contextlen = 30 if ($contextlen > 30);
6316 $lead = chop_str($lead, $contextlen, 10, 'left');
6317 $trail = chop_str($trail, $contextlen, 10, 'right');
Junio C Hamanobe8b9062008-02-22 17:33:47 +01006318
6319 $lead = esc_html($lead);
6320 $match = esc_html($match);
6321 $trail = esc_html($trail);
6322
6323 print "$lead<span class=\"match\">$match</span>$trail<br />";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006324 }
6325 }
6326 print "</td>\n" .
6327 "<td class=\"link\">" .
6328 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6329 " | " .
Denis Chengf1fe8f52007-11-26 20:42:06 +08006330 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
6331 " | " .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00006332 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6333 print "</td>\n" .
6334 "</tr>\n";
6335 }
6336 if (defined $extra) {
6337 print "<tr>\n" .
6338 "<td colspan=\"3\">$extra</td>\n" .
6339 "</tr>\n";
6340 }
6341 print "</table>\n";
6342}
6343
Jakub Narebski717b8312006-07-31 21:22:15 +02006344## ======================================================================
6345## ======================================================================
6346## actions
6347
Jakub Narebski717b8312006-07-31 21:22:15 +02006348sub git_project_list {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02006349 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02006350 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006351 die_error(400, "Unknown order parameter");
Jakub Narebski6326b602006-08-01 02:59:12 +02006352 }
6353
Bernhard R. Link19d2d232012-01-30 21:07:37 +01006354 my @list = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski717b8312006-07-31 21:22:15 +02006355 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006356 die_error(404, "No projects found");
Jakub Narebski717b8312006-07-31 21:22:15 +02006357 }
Jakub Narebski6326b602006-08-01 02:59:12 +02006358
Jakub Narebski717b8312006-07-31 21:22:15 +02006359 git_header_html();
John 'Warthog9' Hawley24d4afc2010-01-30 23:30:41 +01006360 if (defined $home_text && -f $home_text) {
Jakub Narebski717b8312006-07-31 21:22:15 +02006361 print "<div class=\"index_include\">\n";
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01006362 insert_file($home_text);
Jakub Narebski717b8312006-07-31 21:22:15 +02006363 print "</div>\n";
6364 }
Jakub Narebskia1e1b2d2012-01-31 01:20:54 +01006365
6366 git_project_search_form($searchtext, $search_use_regexp);
Petr Baudise30496d2006-10-24 05:33:17 +02006367 git_project_list_body(\@list, $order);
6368 git_footer_html();
6369}
6370
6371sub git_forks {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02006372 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02006373 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006374 die_error(400, "Unknown order parameter");
Jakub Narebski717b8312006-07-31 21:22:15 +02006375 }
Petr Baudise30496d2006-10-24 05:33:17 +02006376
Bernhard R. Link4c7cd172012-01-30 21:05:47 +01006377 my $filter = $project;
6378 $filter =~ s/\.git$//;
6379 my @list = git_get_projects_list($filter);
Petr Baudise30496d2006-10-24 05:33:17 +02006380 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006381 die_error(404, "No forks found");
Jakub Narebski717b8312006-07-31 21:22:15 +02006382 }
Petr Baudise30496d2006-10-24 05:33:17 +02006383
6384 git_header_html();
6385 git_print_page_nav('','');
6386 git_print_header_div('summary', "$project forks");
6387 git_project_list_body(\@list, $order);
Jakub Narebski717b8312006-07-31 21:22:15 +02006388 git_footer_html();
6389}
6390
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006391sub git_project_index {
Bernhard R. Link19d2d232012-01-30 21:07:37 +01006392 my @projects = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski12b14432011-04-29 19:51:56 +02006393 if (!@projects) {
6394 die_error(404, "No projects found");
6395 }
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006396
6397 print $cgi->header(
6398 -type => 'text/plain',
6399 -charset => 'utf-8',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02006400 -content_disposition => 'inline; filename="index.aux"');
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006401
6402 foreach my $pr (@projects) {
6403 if (!exists $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02006404 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02006405 }
6406
6407 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
6408 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
6409 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6410 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
6411 $path =~ s/ /\+/g;
6412 $owner =~ s/ /\+/g;
6413
6414 print "$path $owner\n";
6415 }
6416}
6417
Kay Sieversede5e102005-08-07 20:23:12 +02006418sub git_summary {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006419 my $descr = git_get_project_description($project) || "none";
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00006420 my %co = parse_commit("HEAD");
Jakub Narebski785cdea2007-05-13 12:39:22 +02006421 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00006422 my $head = $co{'id'};
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006423 my $remote_heads = gitweb_check_feature('remote_heads');
Kay Sieversede5e102005-08-07 20:23:12 +02006424
Jakub Narebski1e0cf032006-08-14 02:10:06 +02006425 my $owner = git_get_project_owner($project);
Kay Sieversede5e102005-08-07 20:23:12 +02006426
Jakub Narebskicd146402006-11-02 20:23:11 +01006427 my $refs = git_get_references();
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006428 # These get_*_list functions return one more to allow us to see if
6429 # there are more ...
6430 my @taglist = git_get_tags_list(16);
6431 my @headlist = git_get_heads_list(16);
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006432 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
Petr Baudise30496d2006-10-24 05:33:17 +02006433 my @forklist;
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006434 my $check_forks = gitweb_check_feature('forks');
Junio C Hamano5dd5ed02006-11-07 22:00:45 -08006435
6436 if ($check_forks) {
Jakub Narebski12b14432011-04-29 19:51:56 +02006437 # find forks of a project
Bernhard R. Link4c7cd172012-01-30 21:05:47 +01006438 my $filter = $project;
6439 $filter =~ s/\.git$//;
6440 @forklist = git_get_projects_list($filter);
Jakub Narebski12b14432011-04-29 19:51:56 +02006441 # filter out forks of forks
6442 @forklist = filter_forks_from_projects_list(\@forklist)
6443 if (@forklist);
Petr Baudise30496d2006-10-24 05:33:17 +02006444 }
Jakub Narebski120ddde2006-09-19 14:33:22 +02006445
Kay Sieversede5e102005-08-07 20:23:12 +02006446 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02006447 git_print_page_nav('summary','', $head);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006448
Kay Sievers19806692005-08-07 20:26:27 +02006449 print "<div class=\"title\">&nbsp;</div>\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01006450 print "<table class=\"projects_list\">\n" .
Kacper Kornet0ebe7822012-04-26 18:45:44 +02006451 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n";
6452 unless ($omit_owner) {
6453 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
6454 }
Jakub Narebski785cdea2007-05-13 12:39:22 +02006455 if (defined $cd{'rfc2822'}) {
Jakub Narebski256b7b42011-04-28 21:04:07 +02006456 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
6457 "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
Jakub Narebski785cdea2007-05-13 12:39:22 +02006458 }
6459
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02006460 # use per project git URL list in $projectroot/$project/cloneurl
6461 # or make project git URL from git base URL and project name
Jakub Narebski19a87212006-08-15 23:03:17 +02006462 my $url_tag = "URL";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02006463 my @url_list = git_get_project_url_list($project);
6464 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
6465 foreach my $git_url (@url_list) {
6466 next unless $git_url;
Giuseppe Bilotta0e656992010-11-11 13:26:15 +01006467 print format_repo_url($url_tag, $git_url);
Jakub Narebski19a87212006-08-15 23:03:17 +02006468 $url_tag = "";
6469 }
Petr Baudisaed93de2008-10-02 17:13:02 +02006470
6471 # Tag cloud
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006472 my $show_ctags = gitweb_check_feature('ctags');
Petr Baudisaed93de2008-10-02 17:13:02 +02006473 if ($show_ctags) {
6474 my $ctags = git_get_project_ctags($project);
Jakub Narebski0368c492011-04-29 19:51:57 +02006475 if (%$ctags) {
6476 # without ability to add tags, don't show if there are none
6477 my $cloud = git_populate_project_tagcloud($ctags);
6478 print "<tr id=\"metadata_ctags\">" .
6479 "<td>content tags</td>" .
6480 "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
6481 "</tr>\n";
6482 }
Petr Baudisaed93de2008-10-02 17:13:02 +02006483 }
6484
Jakub Narebski19a87212006-08-15 23:03:17 +02006485 print "</table>\n";
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006486
Matt McCutchen7e1100e2009-02-07 19:00:09 -05006487 # If XSS prevention is on, we don't include README.html.
6488 # TODO: Allow a readme in some safe format.
6489 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01006490 print "<div class=\"title\">readme</div>\n" .
6491 "<div class=\"readme\">\n";
6492 insert_file("$projectroot/$project/README.html");
6493 print "\n</div>\n"; # class="readme"
Petr Baudis447ef092006-10-24 05:23:46 +02006494 }
6495
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006496 # we need to request one more than 16 (0..15) to check if
6497 # those 16 are all
Jakub Narebski785cdea2007-05-13 12:39:22 +02006498 my @commitlist = $head ? parse_commits($head, 17) : ();
6499 if (@commitlist) {
6500 git_print_header_div('shortlog');
6501 git_shortlog_body(\@commitlist, 0, 15, $refs,
6502 $#commitlist <= 15 ? undef :
6503 $cgi->a({-href => href(action=>"shortlog")}, "..."));
6504 }
Kay Sieversede5e102005-08-07 20:23:12 +02006505
Jakub Narebski120ddde2006-09-19 14:33:22 +02006506 if (@taglist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006507 git_print_header_div('tags');
Jakub Narebski120ddde2006-09-19 14:33:22 +02006508 git_tags_body(\@taglist, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006509 $#taglist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006510 $cgi->a({-href => href(action=>"tags")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02006511 }
Kay Sievers0db37972005-08-07 20:24:35 +02006512
Jakub Narebski120ddde2006-09-19 14:33:22 +02006513 if (@headlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006514 git_print_header_div('heads');
Jakub Narebski120ddde2006-09-19 14:33:22 +02006515 git_heads_body(\@headlist, $head, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00006516 $#headlist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02006517 $cgi->a({-href => href(action=>"heads")}, "..."));
Kay Sievers0db37972005-08-07 20:24:35 +02006518 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006519
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006520 if (%remotedata) {
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006521 git_print_header_div('remotes');
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006522 git_remotes_body(\%remotedata, 15, $head);
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006523 }
6524
Petr Baudise30496d2006-10-24 05:33:17 +02006525 if (@forklist) {
6526 git_print_header_div('forks');
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02006527 git_project_list_body(\@forklist, 'age', 0, 15,
Robert Fitzsimonsaaca9672006-12-22 19:38:12 +00006528 $#forklist <= 15 ? undef :
Petr Baudise30496d2006-10-24 05:33:17 +02006529 $cgi->a({-href => href(action=>"forks")}, "..."),
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02006530 'no_header');
Petr Baudise30496d2006-10-24 05:33:17 +02006531 }
6532
Kay Sieversede5e102005-08-07 20:23:12 +02006533 git_footer_html();
6534}
6535
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006536sub git_tag {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006537 my %tag = parse_tag($hash);
Jakub Narebski198a2a82007-05-12 21:16:34 +02006538
6539 if (! %tag) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006540 die_error(404, "Unknown tag object");
Jakub Narebski198a2a82007-05-12 21:16:34 +02006541 }
6542
Anders Kaseorgd8a94802010-08-27 13:38:16 -04006543 my $head = git_get_head_hash($project);
6544 git_header_html();
6545 git_print_page_nav('','', $head,undef,$head);
Jakub Narebski847e01f2006-08-14 02:05:47 +02006546 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006547 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01006548 "<table class=\"object_header\">\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02006549 "<tr>\n" .
6550 "<td>object</td>\n" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02006551 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6552 $tag{'object'}) . "</td>\n" .
6553 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
6554 $tag{'type'}) . "</td>\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02006555 "</tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006556 if (defined($tag{'author'})) {
Giuseppe Bilottaba924732009-06-30 00:00:50 +02006557 git_print_authorship_rows(\%tag, 'author');
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006558 }
6559 print "</table>\n\n" .
6560 "</div>\n";
6561 print "<div class=\"page_body\">";
6562 my $comment = $tag{'comment'};
6563 foreach my $line (@$comment) {
Junio C Hamano70022432006-11-24 14:04:01 -08006564 chomp $line;
Jakub Narebski793c4002006-11-24 22:25:50 +01006565 print esc_html($line, -nbsp=>1) . "<br/>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02006566 }
6567 print "</div>\n";
6568 git_footer_html();
6569}
6570
Jakub Narebski4af819d2009-09-01 13:39:17 +02006571sub git_blame_common {
6572 my $format = shift || 'porcelain';
Jakub Narebski84d9e2d2012-02-03 13:44:54 +01006573 if ($format eq 'porcelain' && $input_params{'javascript'}) {
Jakub Narebskic4ccf612009-09-01 13:39:19 +02006574 $format = 'incremental';
6575 $action = 'blame_incremental'; # for page title etc
6576 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006577
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006578 # permissions
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006579 gitweb_check_feature('blame')
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006580 or die_error(403, "Blame view not allowed");
Lea Wiemann074afaa2008-06-19 22:03:21 +02006581
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006582 # error checking
Lea Wiemann074afaa2008-06-19 22:03:21 +02006583 die_error(400, "No file name given") unless $file_name;
Jakub Narebski847e01f2006-08-14 02:05:47 +02006584 $hash_base ||= git_get_head_hash($project);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006585 die_error(404, "Couldn't find base commit") unless $hash_base;
Jakub Narebski847e01f2006-08-14 02:05:47 +02006586 my %co = parse_commit($hash_base)
Lea Wiemann074afaa2008-06-19 22:03:21 +02006587 or die_error(404, "Commit not found");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006588 my $ftype = "blob";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006589 if (!defined $hash) {
6590 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02006591 or die_error(404, "Error looking up file");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006592 } else {
6593 $ftype = git_get_type($hash);
6594 if ($ftype !~ "blob") {
6595 die_error(400, "Object is not a blob");
6596 }
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006597 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006598
Jakub Narebski4af819d2009-09-01 13:39:17 +02006599 my $fd;
6600 if ($format eq 'incremental') {
6601 # get file contents (as base)
6602 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
6603 or die_error(500, "Open git-cat-file failed");
6604 } elsif ($format eq 'data') {
6605 # run git-blame --incremental
6606 open $fd, "-|", git_cmd(), "blame", "--incremental",
6607 $hash_base, "--", $file_name
6608 or die_error(500, "Open git-blame --incremental failed");
6609 } else {
6610 # run git-blame --porcelain
6611 open $fd, "-|", git_cmd(), "blame", '-p',
6612 $hash_base, '--', $file_name
6613 or die_error(500, "Open git-blame --porcelain failed");
6614 }
6615
6616 # incremental blame data returns early
6617 if ($format eq 'data') {
6618 print $cgi->header(
6619 -type=>"text/plain", -charset => "utf-8",
6620 -status=> "200 OK");
6621 local $| = 1; # output autoflush
Jürgen Kreileder57cf4ad2011-12-17 10:22:23 +01006622 while (my $line = <$fd>) {
6623 print to_utf8($line);
6624 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006625 close $fd
6626 or print "ERROR $!\n";
6627
6628 print 'END';
6629 if (defined $t0 && gitweb_check_feature('timed')) {
6630 print ' '.
Jakub Narebski3962f1d72010-11-09 19:27:54 +01006631 tv_interval($t0, [ gettimeofday() ]).
Jakub Narebski4af819d2009-09-01 13:39:17 +02006632 ' '.$number_of_git_cmds;
6633 }
6634 print "\n";
6635
6636 return;
6637 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006638
6639 # page header
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006640 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02006641 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01006642 $cgi->a({-href => href(action=>"blob", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02006643 "blob") .
Jakub Narebski87e573f2009-12-01 17:54:26 +01006644 " | ";
6645 if ($format eq 'incremental') {
6646 $formats_nav .=
6647 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
6648 "blame") . " (non-incremental)";
6649 } else {
6650 $formats_nav .=
6651 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
6652 "blame") . " (incremental)";
6653 }
6654 $formats_nav .=
Jakub Narebski952c65f2006-08-22 16:52:50 +02006655 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01006656 $cgi->a({-href => href(action=>"history", -replay=>1)},
6657 "history") .
Petr Baudiscae18622006-09-22 03:19:41 +02006658 " | " .
Jakub Narebski4af819d2009-09-01 13:39:17 +02006659 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02006660 "HEAD");
Jakub Narebski847e01f2006-08-14 02:05:47 +02006661 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6662 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07006663 git_print_page_path($file_name, $ftype, $hash_base);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006664
6665 # page body
Jakub Narebski4af819d2009-09-01 13:39:17 +02006666 if ($format eq 'incremental') {
6667 print "<noscript>\n<div class=\"error\"><center><b>\n".
6668 "This page requires JavaScript to run.\n Use ".
Jakub Narebskic4ccf612009-09-01 13:39:19 +02006669 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
Jakub Narebski4af819d2009-09-01 13:39:17 +02006670 'this page').
6671 " instead.\n".
6672 "</b></center></div>\n</noscript>\n";
6673
6674 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
6675 }
6676
6677 print qq!<div class="page_body">\n!;
6678 print qq!<div id="progress_info">... / ...</div>\n!
6679 if ($format eq 'incremental');
6680 print qq!<table id="blame_table" class="blame" width="100%">\n!.
6681 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
6682 qq!<thead>\n!.
6683 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
6684 qq!</thead>\n!.
6685 qq!<tbody>\n!;
6686
Jakub Narebskiaef37682009-07-25 00:44:06 +02006687 my @rev_color = qw(light dark);
Luben Tuikovcc1bf972006-07-23 13:37:53 -07006688 my $num_colors = scalar(@rev_color);
6689 my $current_color = 0;
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006690
Jakub Narebski4af819d2009-09-01 13:39:17 +02006691 if ($format eq 'incremental') {
6692 my $color_class = $rev_color[$current_color];
6693
6694 #contents of a file
6695 my $linenr = 0;
6696 LINE:
6697 while (my $line = <$fd>) {
6698 chomp $line;
6699 $linenr++;
6700
6701 print qq!<tr id="l$linenr" class="$color_class">!.
6702 qq!<td class="sha1"><a href=""> </a></td>!.
6703 qq!<td class="linenr">!.
6704 qq!<a class="linenr" href="">$linenr</a></td>!;
6705 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
6706 print qq!</tr>\n!;
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07006707 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006708
6709 } else { # porcelain, i.e. ordinary blame
6710 my %metainfo = (); # saves information about commits
6711
6712 # blame data
6713 LINE:
6714 while (my $line = <$fd>) {
6715 chomp $line;
6716 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
6717 # no <lines in group> for subsequent lines in group of lines
6718 my ($full_rev, $orig_lineno, $lineno, $group_size) =
6719 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
6720 if (!exists $metainfo{$full_rev}) {
6721 $metainfo{$full_rev} = { 'nprevious' => 0 };
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07006722 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006723 my $meta = $metainfo{$full_rev};
6724 my $data;
6725 while ($data = <$fd>) {
6726 chomp $data;
6727 last if ($data =~ s/^\t//); # contents of line
6728 if ($data =~ /^(\S+)(?: (.*))?$/) {
6729 $meta->{$1} = $2 unless exists $meta->{$1};
6730 }
6731 if ($data =~ /^previous /) {
6732 $meta->{'nprevious'}++;
Jakub Narebskia36817b2009-07-25 00:44:05 +02006733 }
6734 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006735 my $short_rev = substr($full_rev, 0, 8);
6736 my $author = $meta->{'author'};
6737 my %date =
6738 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
6739 my $date = $date{'iso-tz'};
6740 if ($group_size) {
6741 $current_color = ($current_color + 1) % $num_colors;
6742 }
6743 my $tr_class = $rev_color[$current_color];
6744 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
6745 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
6746 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
6747 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
6748 if ($group_size) {
6749 print "<td class=\"sha1\"";
6750 print " title=\"". esc_html($author) . ", $date\"";
6751 print " rowspan=\"$group_size\"" if ($group_size > 1);
6752 print ">";
6753 print $cgi->a({-href => href(action=>"commit",
6754 hash=>$full_rev,
6755 file_name=>$file_name)},
6756 esc_html($short_rev));
6757 if ($group_size >= 2) {
6758 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
6759 if (@author_initials) {
6760 print "<br />" .
6761 esc_html(join('', @author_initials));
6762 # or join('.', ...)
6763 }
6764 }
6765 print "</td>\n";
6766 }
6767 # 'previous' <sha1 of parent commit> <filename at commit>
6768 if (exists $meta->{'previous'} &&
6769 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6770 $meta->{'parent'} = $1;
6771 $meta->{'file_parent'} = unquote($2);
6772 }
6773 my $linenr_commit =
6774 exists($meta->{'parent'}) ?
6775 $meta->{'parent'} : $full_rev;
6776 my $linenr_filename =
6777 exists($meta->{'file_parent'}) ?
6778 $meta->{'file_parent'} : unquote($meta->{'filename'});
6779 my $blamed = href(action => 'blame',
6780 file_name => $linenr_filename,
6781 hash_base => $linenr_commit);
6782 print "<td class=\"linenr\">";
6783 print $cgi->a({ -href => "$blamed#l$orig_lineno",
6784 -class => "linenr" },
6785 esc_html($lineno));
6786 print "</td>";
6787 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
6788 print "</tr>\n";
6789 } # end while
6790
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006791 }
Jakub Narebski4af819d2009-09-01 13:39:17 +02006792
6793 # footer
6794 print "</tbody>\n".
6795 "</table>\n"; # class="blame"
6796 print "</div>\n"; # class="blame_body"
Jakub Narebski952c65f2006-08-22 16:52:50 +02006797 close $fd
6798 or print "Reading blob failed\n";
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01006799
Luben Tuikov1f2857e2006-07-23 13:34:55 -07006800 git_footer_html();
6801}
6802
Jakub Narebski4af819d2009-09-01 13:39:17 +02006803sub git_blame {
6804 git_blame_common();
6805}
6806
6807sub git_blame_incremental {
6808 git_blame_common('incremental');
6809}
6810
6811sub git_blame_data {
6812 git_blame_common('data');
6813}
6814
Kay Sieversede5e102005-08-07 20:23:12 +02006815sub git_tags {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006816 my $head = git_get_head_hash($project);
Kay Sieversede5e102005-08-07 20:23:12 +02006817 git_header_html();
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01006818 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
Jakub Narebski847e01f2006-08-14 02:05:47 +02006819 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02006820
Jakub Narebskicd146402006-11-02 20:23:11 +01006821 my @tagslist = git_get_tags_list();
6822 if (@tagslist) {
6823 git_tags_body(\@tagslist);
Kay Sieversede5e102005-08-07 20:23:12 +02006824 }
Kay Sieversede5e102005-08-07 20:23:12 +02006825 git_footer_html();
6826}
6827
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02006828sub git_heads {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006829 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02006830 git_header_html();
Giuseppe Bilotta11e7bec2010-11-11 13:26:12 +01006831 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
Jakub Narebski847e01f2006-08-14 02:05:47 +02006832 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02006833
Jakub Narebskicd146402006-11-02 20:23:11 +01006834 my @headslist = git_get_heads_list();
6835 if (@headslist) {
6836 git_heads_body(\@headslist, $head);
Kay Sievers0db37972005-08-07 20:24:35 +02006837 }
Kay Sievers0db37972005-08-07 20:24:35 +02006838 git_footer_html();
6839}
6840
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006841# used both for single remote view and for list of all the remotes
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006842sub git_remotes {
6843 gitweb_check_feature('remote_heads')
6844 or die_error(403, "Remote heads view is disabled");
6845
6846 my $head = git_get_head_hash($project);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006847 my $remote = $input_params{'hash'};
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006848
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006849 my $remotedata = git_get_remotes_list($remote);
6850 die_error(500, "Unable to get remote information") unless defined $remotedata;
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006851
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006852 unless (%$remotedata) {
6853 die_error(404, defined $remote ?
6854 "Remote $remote not found" :
6855 "No remotes found");
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006856 }
6857
6858 git_header_html(undef, undef, -action_extra => $remote);
6859 git_print_page_nav('', '', $head, undef, $head,
6860 format_ref_views($remote ? '' : 'remotes'));
6861
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006862 fill_remote_heads($remotedata);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006863 if (defined $remote) {
6864 git_print_header_div('remotes', "$remote remote for $project");
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006865 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006866 } else {
6867 git_print_header_div('summary', "$project remotes");
Giuseppe Bilotta9d0d42f2010-11-11 13:26:17 +01006868 git_remotes_body($remotedata, undef, $head);
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006869 }
Giuseppe Bilottabb607762010-11-11 13:26:14 +01006870
Giuseppe Bilotta00fa6fe2010-11-11 13:26:11 +01006871 git_footer_html();
6872}
6873
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006874sub git_blob_plain {
Jakub Narebski7f718e82008-06-03 16:47:10 +02006875 my $type = shift;
Jakub Narebskif2e73302006-08-26 19:14:25 +02006876 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02006877
Luben Tuikovcff07712006-07-23 13:28:55 -07006878 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006879 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006880 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006881 $hash = git_get_hash_by_path($base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02006882 or die_error(404, "Cannot find file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006883 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006884 die_error(400, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006885 }
Martin Waitz800764c2006-09-16 23:09:02 +02006886 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6887 # blobs defined by non-textual hash id's can be cached
6888 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006889 }
Martin Waitz800764c2006-09-16 23:09:02 +02006890
Dennis Stosberg25691fb2006-08-28 17:49:58 +02006891 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02006892 or die_error(500, "Open git-cat-file blob '$hash' failed");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006893
Jakub Narebski7f718e82008-06-03 16:47:10 +02006894 # content-type (can include charset)
6895 $type = blob_contenttype($fd, $file_name, $type);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006896
Jakub Narebski7f718e82008-06-03 16:47:10 +02006897 # "save as" filename, even when no $file_name is given
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006898 my $save_as = "$hash";
6899 if (defined $file_name) {
6900 $save_as = $file_name;
6901 } elsif ($type =~ m/^text\//) {
6902 $save_as .= '.txt';
6903 }
6904
Matt McCutchen7e1100e2009-02-07 19:00:09 -05006905 # With XSS prevention on, blobs of all types except a few known safe
6906 # ones are served with "Content-Disposition: attachment" to make sure
6907 # they don't run in our security domain. For certain image types,
6908 # blob view writes an <img> tag referring to blob_plain view, and we
6909 # want to be sure not to break that by serving the image as an
6910 # attachment (though Firefox 3 doesn't seem to care).
6911 my $sandbox = $prevent_xss &&
Jakub Narebski86afbd02011-06-30 11:39:20 +02006912 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
6913
6914 # serve text/* as text/plain
6915 if ($prevent_xss &&
Jakub Narebskie8c35312011-06-30 11:39:21 +02006916 ($type =~ m!^text/[a-z]+\b(.*)$! ||
6917 ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
Jakub Narebski86afbd02011-06-30 11:39:20 +02006918 my $rest = $1;
6919 $rest = defined $rest ? $rest : '';
6920 $type = "text/plain$rest";
6921 }
Matt McCutchen7e1100e2009-02-07 19:00:09 -05006922
Jakub Narebskif2e73302006-08-26 19:14:25 +02006923 print $cgi->header(
Jakub Narebski7f718e82008-06-03 16:47:10 +02006924 -type => $type,
6925 -expires => $expires,
Matt McCutchen7e1100e2009-02-07 19:00:09 -05006926 -content_disposition =>
6927 ($sandbox ? 'attachment' : 'inline')
6928 . '; filename="' . $save_as . '"');
Jakub Narebski34122b52009-05-11 03:29:40 +02006929 local $/ = undef;
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006930 binmode STDOUT, ':raw';
6931 print <$fd>;
6932 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006933 close $fd;
6934}
6935
Kay Sievers09bd7892005-08-07 20:21:23 +02006936sub git_blob {
Jakub Narebskif2e73302006-08-26 19:14:25 +02006937 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02006938
Luben Tuikovcff07712006-07-23 13:28:55 -07006939 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006940 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006941 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006942 $hash = git_get_hash_by_path($base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02006943 or die_error(404, "Cannot find file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006944 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006945 die_error(400, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006946 }
Martin Waitz800764c2006-09-16 23:09:02 +02006947 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6948 # blobs defined by non-textual hash id's can be cached
6949 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02006950 }
Martin Waitz800764c2006-09-16 23:09:02 +02006951
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006952 my $have_blame = gitweb_check_feature('blame');
Dennis Stosberg25691fb2006-08-28 17:49:58 +02006953 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02006954 or die_error(500, "Couldn't cat $file_name, $hash");
Jakub Narebski847e01f2006-08-14 02:05:47 +02006955 my $mimetype = blob_mimetype($fd, $file_name);
Johannes Schindelinb331fe52010-04-27 21:34:44 +02006956 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01006957 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
Luben Tuikov930cf7d2006-07-09 20:18:57 -07006958 close $fd;
6959 return git_blob_plain($mimetype);
6960 }
Jakub Narebski5a4cf332006-12-04 23:47:22 +01006961 # we can have blame only for text/* mimetype
6962 $have_blame &&= ($mimetype =~ m!^text/!);
6963
Jakub Narebski592ea412010-04-27 21:34:45 +02006964 my $highlight = gitweb_check_feature('highlight');
6965 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
6966 $fd = run_highlighter($fd, $highlight, $syntax)
6967 if $syntax;
Johannes Schindelinb331fe52010-04-27 21:34:44 +02006968
Jakub Narebskif2e73302006-08-26 19:14:25 +02006969 git_header_html(undef, $expires);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02006970 my $formats_nav = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02006971 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Kay Sievers93129442005-10-17 03:27:54 +02006972 if (defined $file_name) {
Florian Forster5996ca02006-06-12 10:31:57 +02006973 if ($have_blame) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02006974 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01006975 $cgi->a({-href => href(action=>"blame", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02006976 "blame") .
6977 " | ";
Florian Forster5996ca02006-06-12 10:31:57 +02006978 }
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02006979 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01006980 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02006981 "history") .
6982 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01006983 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02006984 "raw") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02006985 " | " .
6986 $cgi->a({-href => href(action=>"blob",
6987 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02006988 "HEAD");
Kay Sievers93129442005-10-17 03:27:54 +02006989 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02006990 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01006991 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6992 "raw");
Kay Sievers93129442005-10-17 03:27:54 +02006993 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02006994 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6995 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02006996 } else {
6997 print "<div class=\"page_nav\">\n" .
6998 "<br/><br/></div>\n" .
Jakub Narebski3017ed62010-12-15 00:34:01 +01006999 "<div class=\"title\">".esc_html($hash)."</div>\n";
Kay Sievers09bd7892005-08-07 20:21:23 +02007000 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07007001 git_print_page_path($file_name, "blob", $hash_base);
Kay Sieversc07ad4b2005-08-07 20:22:44 +02007002 print "<div class=\"page_body\">\n";
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007003 if ($mimetype =~ m!^image/!) {
Jakub Narebski3017ed62010-12-15 00:34:01 +01007004 print qq!<img type="!.esc_attr($mimetype).qq!"!;
Jakub Narebski5a4cf332006-12-04 23:47:22 +01007005 if ($file_name) {
Jakub Narebski3017ed62010-12-15 00:34:01 +01007006 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
Jakub Narebski5a4cf332006-12-04 23:47:22 +01007007 }
7008 print qq! src="! .
7009 href(action=>"blob_plain", hash=>$hash,
7010 hash_base=>$hash_base, file_name=>$file_name) .
7011 qq!" />\n!;
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007012 } else {
7013 my $nr;
7014 while (my $line = <$fd>) {
7015 chomp $line;
7016 $nr++;
7017 $line = untabify($line);
Jakub Narebski592ea412010-04-27 21:34:45 +02007018 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 +02007019 $nr, esc_attr(href(-replay => 1)), $nr, $nr,
7020 $syntax ? sanitize($line) : esc_html($line, -nbsp=>1);
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01007021 }
Kay Sievers161332a2005-08-07 19:49:46 +02007022 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02007023 close $fd
7024 or print "Reading blob failed.\n";
Kay Sieversfbb592a2005-08-07 20:12:11 +02007025 print "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02007026 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007027}
7028
7029sub git_tree {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07007030 if (!defined $hash_base) {
7031 $hash_base = "HEAD";
7032 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02007033 if (!defined $hash) {
Kay Sievers09bd7892005-08-07 20:21:23 +02007034 if (defined $file_name) {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07007035 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
7036 } else {
7037 $hash = $hash_base;
Kay Sievers10dba282005-08-07 20:25:27 +02007038 }
Kay Sieverse925f382005-08-07 20:23:35 +02007039 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02007040 die_error(404, "No such tree") unless defined($hash);
Jakub Narebski34122b52009-05-11 03:29:40 +02007041
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007042 my $show_sizes = gitweb_check_feature('show-sizes');
7043 my $have_blame = gitweb_check_feature('blame');
7044
Jakub Narebski34122b52009-05-11 03:29:40 +02007045 my @entries = ();
7046 {
7047 local $/ = "\0";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007048 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
7049 ($show_sizes ? '-l' : ()), @extra_options, $hash
Jakub Narebski34122b52009-05-11 03:29:40 +02007050 or die_error(500, "Open git-ls-tree failed");
7051 @entries = map { chomp; $_ } <$fd>;
7052 close $fd
7053 or die_error(404, "Reading tree failed");
7054 }
Kay Sieversd63577d2005-08-07 20:18:13 +02007055
Jakub Narebski847e01f2006-08-14 02:05:47 +02007056 my $refs = git_get_references();
7057 my $ref = format_ref_marker($refs, $hash_base);
Kay Sievers12a88f22005-08-07 20:02:47 +02007058 git_header_html();
Jakub Narebski300454f2006-10-21 17:53:09 +02007059 my $basedir = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02007060 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Petr Baudiscae18622006-09-22 03:19:41 +02007061 my @views_nav = ();
7062 if (defined $file_name) {
7063 push @views_nav,
Jakub Narebskia3823e52007-11-01 13:06:29 +01007064 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02007065 "history"),
7066 $cgi->a({-href => href(action=>"tree",
7067 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02007068 "HEAD"),
Petr Baudiscae18622006-09-22 03:19:41 +02007069 }
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007070 my $snapshot_links = format_snapshot_links($hash);
7071 if (defined $snapshot_links) {
Petr Baudiscae18622006-09-22 03:19:41 +02007072 # FIXME: Should be available when we have no hash base as well.
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007073 push @views_nav, $snapshot_links;
Petr Baudiscae18622006-09-22 03:19:41 +02007074 }
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007075 git_print_page_nav('tree','', $hash_base, undef, undef,
7076 join(' | ', @views_nav));
Jakub Narebski847e01f2006-08-14 02:05:47 +02007077 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
Kay Sieversd63577d2005-08-07 20:18:13 +02007078 } else {
Jakub Narebskifa702002006-08-31 00:35:07 +02007079 undef $hash_base;
Kay Sieversd63577d2005-08-07 20:18:13 +02007080 print "<div class=\"page_nav\">\n";
7081 print "<br/><br/></div>\n";
Jakub Narebski3017ed62010-12-15 00:34:01 +01007082 print "<div class=\"title\">".esc_html($hash)."</div>\n";
Kay Sieversd63577d2005-08-07 20:18:13 +02007083 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007084 if (defined $file_name) {
Jakub Narebski300454f2006-10-21 17:53:09 +02007085 $basedir = $file_name;
7086 if ($basedir ne '' && substr($basedir, -1) ne '/') {
7087 $basedir .= '/';
7088 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02007089 git_print_page_path($file_name, 'tree', $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02007090 }
Kay Sieversfbb592a2005-08-07 20:12:11 +02007091 print "<div class=\"page_body\">\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01007092 print "<table class=\"tree\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07007093 my $alternate = 1;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007094 # '..' (top directory) link if possible
7095 if (defined $hash_base &&
7096 defined $file_name && $file_name =~ m![^/]+$!) {
7097 if ($alternate) {
7098 print "<tr class=\"dark\">\n";
7099 } else {
7100 print "<tr class=\"light\">\n";
7101 }
7102 $alternate ^= 1;
7103
7104 my $up = $file_name;
7105 $up =~ s!/?[^/]+$!!;
7106 undef $up unless $up;
7107 # based on git_print_tree_entry
7108 print '<td class="mode">' . mode_str('040000') . "</td>\n";
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007109 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007110 print '<td class="list">';
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007111 print $cgi->a({-href => href(action=>"tree",
7112 hash_base=>$hash_base,
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02007113 file_name=>$up)},
7114 "..");
7115 print "</td>\n";
7116 print "<td class=\"link\"></td>\n";
7117
7118 print "</tr>\n";
7119 }
Kay Sievers161332a2005-08-07 19:49:46 +02007120 foreach my $line (@entries) {
Jakub Narebskie4b48ea2009-09-07 14:40:00 +02007121 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
Jakub Narebskicb849b42006-08-31 00:32:15 +02007122
Kay Sieversbddec012005-08-07 20:25:42 +02007123 if ($alternate) {
Kay Sieversc994d622005-08-07 20:27:18 +02007124 print "<tr class=\"dark\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007125 } else {
Kay Sieversc994d622005-08-07 20:27:18 +02007126 print "<tr class=\"light\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007127 }
7128 $alternate ^= 1;
Jakub Narebskicb849b42006-08-31 00:32:15 +02007129
Jakub Narebski300454f2006-10-21 17:53:09 +02007130 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
Jakub Narebskifa702002006-08-31 00:35:07 +02007131
Kay Sievers42f7eb92005-08-07 20:21:46 +02007132 print "</tr>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02007133 }
Kay Sievers42f7eb92005-08-07 20:21:46 +02007134 print "</table>\n" .
7135 "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02007136 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007137}
7138
Mark Radab6292752009-11-07 16:13:29 +01007139sub snapshot_name {
7140 my ($project, $hash) = @_;
7141
7142 # path/to/project.git -> project
7143 # path/to/project/.git -> project
7144 my $name = to_utf8($project);
7145 $name =~ s,([^/])/*\.git$,$1,;
7146 $name = basename($name);
7147 # sanitize name
7148 $name =~ s/[[:cntrl:]]/?/g;
7149
7150 my $ver = $hash;
7151 if ($hash =~ /^[0-9a-fA-F]+$/) {
7152 # shorten SHA-1 hash
7153 my $full_hash = git_get_full_hash($project, $hash);
7154 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
7155 $ver = git_get_short_hash($project, $hash);
7156 }
7157 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
7158 # tags don't need shortened SHA-1 hash
7159 $ver = $1;
7160 } else {
7161 # branches and other need shortened SHA-1 hash
7162 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
7163 $ver = $1;
7164 }
7165 $ver .= '-' . git_get_short_hash($project, $hash);
7166 }
7167 # in case of hierarchical branch names
7168 $ver =~ s!/!.!g;
7169
7170 # name = project-version_string
7171 $name = "$name-$ver";
7172
7173 return wantarray ? ($name, $name) : $name;
7174}
7175
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04007176sub exit_if_unmodified_since {
7177 my ($latest_epoch) = @_;
7178 our $cgi;
7179
7180 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7181 if (defined $if_modified) {
7182 my $since;
7183 if (eval { require HTTP::Date; 1; }) {
7184 $since = HTTP::Date::str2time($if_modified);
7185 } elsif (eval { require Time::ParseDate; 1; }) {
7186 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7187 }
7188 if (defined $since && $latest_epoch <= $since) {
7189 my %latest_date = parse_date($latest_epoch);
7190 print $cgi->header(
7191 -last_modified => $latest_date{'rfc2822'},
7192 -status => '304 Not Modified');
7193 goto DONE_GITWEB;
7194 }
7195 }
7196}
7197
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307198sub git_snapshot {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02007199 my $format = $input_params{'snapshot_format'};
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01007200 if (!@snapshot_fmts) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007201 die_error(403, "Snapshots not allowed");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007202 }
7203 # default to first supported snapshot format
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01007204 $format ||= $snapshot_fmts[0];
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007205 if ($format !~ m/^[a-z0-9]+$/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007206 die_error(400, "Invalid snapshot format parameter");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02007207 } elsif (!exists($known_snapshot_formats{$format})) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007208 die_error(400, "Unknown snapshot format");
Mark A Rada1bfd3632009-08-06 10:25:39 -04007209 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
7210 die_error(403, "Snapshot format not allowed");
Mark Rada34b31a82009-08-25 00:59:48 -04007211 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
7212 die_error(403, "Unsupported snapshot format");
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05307213 }
7214
Mark Radafdb0c362009-09-26 13:46:08 -04007215 my $type = git_get_type("$hash^{}");
7216 if (!$type) {
7217 die_error(404, 'Object does not exist');
7218 } elsif ($type eq 'blob') {
7219 die_error(400, 'Object is not a tree-ish');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307220 }
7221
Mark Radab6292752009-11-07 16:13:29 +01007222 my ($name, $prefix) = snapshot_name($project, $hash);
7223 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
W. Trevor King8745db62012-03-29 08:45:49 -04007224
7225 my %co = parse_commit($hash);
7226 exit_if_unmodified_since($co{'committer_epoch'}) if %co;
7227
Mark Radab6292752009-11-07 16:13:29 +01007228 my $cmd = quote_command(
Lea Wiemann516381d2008-06-17 23:46:35 +02007229 git_cmd(), 'archive',
7230 "--format=$known_snapshot_formats{$format}{'format'}",
Mark Radab6292752009-11-07 16:13:29 +01007231 "--prefix=$prefix/", $hash);
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007232 if (exists $known_snapshot_formats{$format}{'compressor'}) {
Lea Wiemann516381d2008-06-17 23:46:35 +02007233 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
Mark Levedahl072570e2007-05-20 11:46:46 -04007234 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307235
Mark Radab6292752009-11-07 16:13:29 +01007236 $filename =~ s/(["\\])/\\$1/g;
W. Trevor King8745db62012-03-29 08:45:49 -04007237 my %latest_date;
7238 if (%co) {
7239 %latest_date = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
7240 }
7241
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02007242 print $cgi->header(
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007243 -type => $known_snapshot_formats{$format}{'type'},
Mark Radab6292752009-11-07 16:13:29 +01007244 -content_disposition => 'inline; filename="' . $filename . '"',
W. Trevor King8745db62012-03-29 08:45:49 -04007245 %co ? (-last_modified => $latest_date{'rfc2822'}) : (),
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02007246 -status => '200 OK');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307247
Mark Levedahl072570e2007-05-20 11:46:46 -04007248 open my $fd, "-|", $cmd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007249 or die_error(500, "Execute git-archive failed");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307250 binmode STDOUT, ':raw';
7251 print <$fd>;
7252 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
7253 close $fd;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307254}
7255
Jakub Narebski15f0b112009-11-13 02:02:13 +01007256sub git_log_generic {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007257 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
Jakub Narebski15f0b112009-11-13 02:02:13 +01007258
Jakub Narebski847e01f2006-08-14 02:05:47 +02007259 my $head = git_get_head_hash($project);
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007260 if (!defined $base) {
7261 $base = $head;
Kay Sievers0db37972005-08-07 20:24:35 +02007262 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02007263 if (!defined $page) {
7264 $page = 0;
Kay Sieversb87d78d2005-08-07 20:21:04 +02007265 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007266 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02007267
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007268 my $commit_hash = $base;
7269 if (defined $parent) {
7270 $commit_hash = "$parent..$base";
Jakub Narebski15f0b112009-11-13 02:02:13 +01007271 }
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007272 my @commitlist =
7273 parse_commits($commit_hash, 101, (100 * $page),
7274 defined $file_name ? ($file_name, "--full-history") : ());
Kay Sieversea4a6df2005-08-07 20:26:49 +02007275
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007276 my $ftype;
7277 if (!defined $file_hash && defined $file_name) {
7278 # some commits could have deleted file in question,
7279 # and not have it in tree, but one of them has to have it
7280 for (my $i = 0; $i < @commitlist; $i++) {
7281 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
7282 last if defined $file_hash;
7283 }
7284 }
7285 if (defined $file_hash) {
7286 $ftype = git_get_type($file_hash);
7287 }
7288 if (defined $file_name && !defined $ftype) {
7289 die_error(500, "Unknown type of object");
7290 }
7291 my %co;
7292 if (defined $file_name) {
7293 %co = parse_commit($base)
7294 or die_error(404, "Unknown commit object");
7295 }
7296
7297
7298 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
Jakub Narebski15f0b112009-11-13 02:02:13 +01007299 my $next_link = '';
Jakub Narebski42671ca2009-11-13 02:02:12 +01007300 if ($#commitlist >= 100) {
7301 $next_link =
7302 $cgi->a({-href => href(-replay=>1, page=>$page+1),
7303 -accesskey => "n", -title => "Alt-n"}, "next");
7304 }
Jakub Narebski15f0b112009-11-13 02:02:13 +01007305 my $patch_max = gitweb_get_feature('patches');
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007306 if ($patch_max && !defined $file_name) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007307 if ($patch_max < 0 || @commitlist <= $patch_max) {
7308 $paging_nav .= " &sdot; " .
7309 $cgi->a({-href => href(action=>"patches", -replay=>1)},
7310 "patches");
7311 }
7312 }
7313
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007314 git_header_html();
Jakub Narebski15f0b112009-11-13 02:02:13 +01007315 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007316 if (defined $file_name) {
7317 git_print_header_div('commit', esc_html($co{'title'}), $base);
7318 } else {
7319 git_print_header_div('summary', $project)
7320 }
7321 git_print_page_path($file_name, $ftype, $hash_base)
7322 if (defined $file_name);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02007323
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007324 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
7325 $file_name, $file_hash, $ftype);
Jakub Narebski42671ca2009-11-13 02:02:12 +01007326
Kay Sievers034df392005-08-07 20:20:07 +02007327 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007328}
7329
Jakub Narebski15f0b112009-11-13 02:02:13 +01007330sub git_log {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007331 git_log_generic('log', \&git_log_body,
7332 $hash, $hash_parent);
Jakub Narebski15f0b112009-11-13 02:02:13 +01007333}
7334
Kay Sievers09bd7892005-08-07 20:21:23 +02007335sub git_commit {
Jakub Narebski9954f772006-11-18 23:35:41 +01007336 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02007337 my %co = parse_commit($hash)
7338 or die_error(404, "Unknown commit object");
Kay Sievers161332a2005-08-07 19:49:46 +02007339
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007340 my $parent = $co{'parent'};
7341 my $parents = $co{'parents'}; # listref
7342
7343 # we need to prepare $formats_nav before any parameter munging
7344 my $formats_nav;
7345 if (!defined $parent) {
7346 # --root commitdiff
7347 $formats_nav .= '(initial)';
7348 } elsif (@$parents == 1) {
7349 # single parent commit
7350 $formats_nav .=
7351 '(parent: ' .
7352 $cgi->a({-href => href(action=>"commit",
7353 hash=>$parent)},
7354 esc_html(substr($parent, 0, 7))) .
7355 ')';
7356 } else {
7357 # merge commit
7358 $formats_nav .=
7359 '(merge: ' .
7360 join(' ', map {
Jakub Narebskif9308a12007-03-23 21:04:31 +01007361 $cgi->a({-href => href(action=>"commit",
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007362 hash=>$_)},
7363 esc_html(substr($_, 0, 7)));
7364 } @$parents ) .
7365 ')';
7366 }
Jakub Narebski1655c982009-10-09 14:26:44 +02007367 if (gitweb_check_feature('patches') && @$parents <= 1) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007368 $formats_nav .= " | " .
7369 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7370 "patch");
7371 }
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007372
Kay Sieversd8a20ba2005-08-07 20:28:53 +02007373 if (!defined $parent) {
Jakub Narebskib9182982006-07-30 18:28:34 -07007374 $parent = "--root";
Kay Sievers6191f8e2005-08-07 20:19:56 +02007375 }
Jakub Narebski549ab4a2006-12-15 17:53:45 +01007376 my @difftree;
Jakub Narebski208ecb22007-05-07 01:10:08 +02007377 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
7378 @diff_opts,
7379 (@$parents <= 1 ? $parent : '-c'),
7380 $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007381 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski208ecb22007-05-07 01:10:08 +02007382 @difftree = map { chomp; $_ } <$fd>;
Lea Wiemann074afaa2008-06-19 22:03:21 +02007383 close $fd or die_error(404, "Reading git-diff-tree failed");
Kay Sievers11044292005-10-19 03:18:45 +02007384
7385 # non-textual hash id's can be cached
7386 my $expires;
7387 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7388 $expires = "+1d";
7389 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007390 my $refs = git_get_references();
7391 my $ref = format_ref_marker($refs, $co{'id'});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05307392
Jakub Narebski594e2122006-07-31 02:21:52 +02007393 git_header_html(undef, $expires);
Petr Baudisa1441542006-10-06 18:59:33 +02007394 git_print_page_nav('commit', '',
Jakub Narebski952c65f2006-08-22 16:52:50 +02007395 $hash, $co{'tree'}, $hash,
Jakub Narebskic9d193d2006-12-15 21:57:16 +01007396 $formats_nav);
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07007397
Kay Sieversb87d78d2005-08-07 20:21:04 +02007398 if (defined $co{'parent'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007399 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02007400 } else {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007401 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02007402 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02007403 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01007404 "<table class=\"object_header\">\n";
Giuseppe Bilotta1c49a4e2009-06-30 00:00:48 +02007405 git_print_authorship_rows(\%co);
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00007406 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02007407 print "<tr>" .
7408 "<td>tree</td>" .
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00007409 "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007410 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
7411 class => "list"}, $co{'tree'}) .
Kay Sievers19806692005-08-07 20:26:27 +02007412 "</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007413 "<td class=\"link\">" .
7414 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
7415 "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02007416 my $snapshot_links = format_snapshot_links($hash);
7417 if (defined $snapshot_links) {
7418 print " | " . $snapshot_links;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05307419 }
7420 print "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02007421 "</tr>\n";
Jakub Narebski549ab4a2006-12-15 17:53:45 +01007422
Kay Sievers3e029292005-08-07 20:05:15 +02007423 foreach my $par (@$parents) {
Kay Sieversbddec012005-08-07 20:25:42 +02007424 print "<tr>" .
7425 "<td>parent</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007426 "<td class=\"sha1\">" .
7427 $cgi->a({-href => href(action=>"commit", hash=>$par),
7428 class => "list"}, $par) .
7429 "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02007430 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02007431 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02007432 " | " .
Jakub Narebskif2e60942006-09-03 23:43:03 +02007433 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
Kay Sieversbddec012005-08-07 20:25:42 +02007434 "</td>" .
7435 "</tr>\n";
Kay Sievers3e029292005-08-07 20:05:15 +02007436 }
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02007437 print "</table>".
Kay Sieversb87d78d2005-08-07 20:21:04 +02007438 "</div>\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02007439
Kay Sieversfbb592a2005-08-07 20:12:11 +02007440 print "<div class=\"page_body\">\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02007441 git_print_log($co{'comment'});
Kay Sievers927dcec2005-08-07 20:18:44 +02007442 print "</div>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02007443
Jakub Narebski208ecb22007-05-07 01:10:08 +02007444 git_difftree_body(\@difftree, $hash, @$parents);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02007445
Kay Sievers12a88f22005-08-07 20:02:47 +02007446 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02007447}
7448
Jakub Narebskica946012006-12-10 13:25:47 +01007449sub git_object {
7450 # object is defined by:
7451 # - hash or hash_base alone
7452 # - hash_base and file_name
7453 my $type;
7454
7455 # - hash or hash_base alone
7456 if ($hash || ($hash_base && !defined $file_name)) {
7457 my $object_id = $hash || $hash_base;
7458
Lea Wiemann516381d2008-06-17 23:46:35 +02007459 open my $fd, "-|", quote_command(
7460 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
Lea Wiemann074afaa2008-06-19 22:03:21 +02007461 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007462 $type = <$fd>;
7463 chomp $type;
7464 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007465 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007466
7467 # - hash_base and file_name
7468 } elsif ($hash_base && defined $file_name) {
7469 $file_name =~ s,/+$,,;
7470
7471 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
Lea Wiemann074afaa2008-06-19 22:03:21 +02007472 or die_error(404, "Base object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007473
7474 # here errors should not hapen
7475 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007476 or die_error(500, "Open git-ls-tree failed");
Jakub Narebskica946012006-12-10 13:25:47 +01007477 my $line = <$fd>;
7478 close $fd;
7479
7480 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
7481 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007482 die_error(404, "File or directory for given base does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01007483 }
7484 $type = $2;
7485 $hash = $3;
7486 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007487 die_error(400, "Not enough information to find object");
Jakub Narebskica946012006-12-10 13:25:47 +01007488 }
7489
7490 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
7491 hash=>$hash, hash_base=>$hash_base,
7492 file_name=>$file_name),
7493 -status => '302 Found');
7494}
7495
Kay Sievers09bd7892005-08-07 20:21:23 +02007496sub git_blobdiff {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007497 my $format = shift || 'html';
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007498 my $diff_style = $input_params{'diff_style'} || 'inline';
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007499
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007500 my $fd;
7501 my @difftree;
7502 my %diffinfo;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007503 my $expires;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007504
7505 # preparing $fd and %diffinfo for git_patchset_body
7506 # new style URI
7507 if (defined $hash_base && defined $hash_parent_base) {
7508 if (defined $file_name) {
7509 # read raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007510 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7511 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02007512 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007513 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007514 @difftree = map { chomp; $_ } <$fd>;
7515 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007516 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007517 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02007518 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007519
Jakub Narebski0aea3372006-08-27 23:45:26 +02007520 } elsif (defined $hash &&
7521 $hash =~ /[0-9a-fA-F]{40}/) {
7522 # try to find filename from $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007523
7524 # read filtered raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007525 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7526 $hash_parent_base, $hash_base, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007527 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007528 @difftree =
7529 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
7530 # $hash == to_id
7531 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
7532 map { chomp; $_ } <$fd>;
7533 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02007534 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007535 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02007536 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007537
7538 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007539 die_error(400, "Missing one of the blob diff parameters");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007540 }
7541
7542 if (@difftree > 1) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007543 die_error(400, "Ambiguous blob diff specification");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007544 }
7545
7546 %diffinfo = parse_difftree_raw_line($difftree[0]);
Jakub Narebski9d301452007-11-01 12:38:08 +01007547 $file_parent ||= $diffinfo{'from_file'} || $file_name;
7548 $file_name ||= $diffinfo{'to_file'};
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007549
7550 $hash_parent ||= $diffinfo{'from_id'};
7551 $hash ||= $diffinfo{'to_id'};
7552
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007553 # non-textual hash id's can be cached
7554 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
7555 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
7556 $expires = '+1d';
7557 }
7558
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007559 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007560 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski957d6ea2007-04-05 13:45:41 +02007561 '-p', ($format eq 'html' ? "--full-index" : ()),
7562 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02007563 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02007564 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007565 }
7566
Junio C Hamanob54dc9f2008-12-16 19:42:02 -08007567 # old/legacy style URI -- not generated anymore since 1.4.3.
7568 if (!%diffinfo) {
7569 die_error('404 Not Found', "Missing one of the blob diff parameters")
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007570 }
7571
7572 # header
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007573 if ($format eq 'html') {
7574 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01007575 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02007576 "raw");
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007577 $formats_nav .= diff_style_nav($diff_style);
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007578 git_header_html(undef, $expires);
7579 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
7580 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
7581 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
7582 } else {
7583 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
Jakub Narebski3017ed62010-12-15 00:34:01 +01007584 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007585 }
7586 if (defined $file_name) {
7587 git_print_page_path($file_name, "blob", $hash_base);
7588 } else {
7589 print "<div class=\"page_path\"></div>\n";
7590 }
7591
7592 } elsif ($format eq 'plain') {
7593 print $cgi->header(
7594 -type => 'text/plain',
7595 -charset => 'utf-8',
7596 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07007597 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007598
7599 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7600
Kay Sievers09bd7892005-08-07 20:21:23 +02007601 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007602 die_error(400, "Unknown blobdiff format");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007603 }
7604
7605 # patch
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007606 if ($format eq 'html') {
7607 print "<div class=\"page_body\">\n";
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007608
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007609 git_patchset_body($fd, $diff_style,
7610 [ \%diffinfo ], $hash_base, $hash_parent_base);
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007611 close $fd;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02007612
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007613 print "</div>\n"; # class="page_body"
7614 git_footer_html();
7615
7616 } else {
7617 while (my $line = <$fd>) {
Jakub Narebski403d0902006-11-08 11:48:56 +01007618 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
7619 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007620
7621 print $line;
7622
7623 last if $line =~ m!^\+\+\+!;
7624 }
7625 local $/ = undef;
7626 print <$fd>;
7627 close $fd;
7628 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007629}
7630
Kay Sievers19806692005-08-07 20:26:27 +02007631sub git_blobdiff_plain {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02007632 git_blobdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02007633}
7634
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007635# assumes that it is added as later part of already existing navigation,
7636# so it returns "| foo | bar" rather than just "foo | bar"
7637sub diff_style_nav {
7638 my ($diff_style, $is_combined) = @_;
7639 $diff_style ||= 'inline';
7640
7641 return "" if ($is_combined);
7642
7643 my @styles = (inline => 'inline', 'sidebyside' => 'side by side');
7644 my %styles = @styles;
7645 @styles =
7646 @styles[ map { $_ * 2 } 0..$#styles/2 ];
7647
7648 return join '',
7649 map { " | ".$_ }
7650 map {
7651 $_ eq $diff_style ? $styles{$_} :
7652 $cgi->a({-href => href(-replay=>1, diff_style => $_)}, $styles{$_})
7653 } @styles;
7654}
7655
Kay Sievers09bd7892005-08-07 20:21:23 +02007656sub git_commitdiff {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01007657 my %params = @_;
7658 my $format = $params{-format} || 'html';
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007659 my $diff_style = $input_params{'diff_style'} || 'inline';
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007660
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007661 my ($patch_max) = gitweb_get_feature('patches');
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007662 if ($format eq 'patch') {
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007663 die_error(403, "Patch view not allowed") unless $patch_max;
7664 }
7665
Jakub Narebski9954f772006-11-18 23:35:41 +01007666 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02007667 my %co = parse_commit($hash)
7668 or die_error(404, "Unknown commit object");
Jakub Narebski151602d2006-10-23 00:37:56 +02007669
Jakub Narebskicd030c32007-06-08 13:33:28 +02007670 # choose format for commitdiff for merge
7671 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
7672 $hash_parent = '--cc';
7673 }
7674 # we need to prepare $formats_nav before almost any parameter munging
Jakub Narebski151602d2006-10-23 00:37:56 +02007675 my $formats_nav;
7676 if ($format eq 'html') {
7677 $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01007678 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007679 "raw");
Jakub Narebski1655c982009-10-09 14:26:44 +02007680 if ($patch_max && @{$co{'parents'}} <= 1) {
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01007681 $formats_nav .= " | " .
7682 $cgi->a({-href => href(action=>"patch", -replay=>1)},
7683 "patch");
7684 }
Kato Kazuyoshi6ae683c2011-10-31 00:36:27 +01007685 $formats_nav .= diff_style_nav($diff_style, @{$co{'parents'}} > 1);
Jakub Narebski151602d2006-10-23 00:37:56 +02007686
Jakub Narebskicd030c32007-06-08 13:33:28 +02007687 if (defined $hash_parent &&
7688 $hash_parent ne '-c' && $hash_parent ne '--cc') {
Jakub Narebski151602d2006-10-23 00:37:56 +02007689 # commitdiff with two commits given
7690 my $hash_parent_short = $hash_parent;
7691 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
7692 $hash_parent_short = substr($hash_parent, 0, 7);
7693 }
7694 $formats_nav .=
Jakub Narebskiada3e1f2007-06-08 13:26:31 +02007695 ' (from';
7696 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
7697 if ($co{'parents'}[$i] eq $hash_parent) {
7698 $formats_nav .= ' parent ' . ($i+1);
7699 last;
7700 }
7701 }
7702 $formats_nav .= ': ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007703 $cgi->a({-href => href(-replay=>1,
7704 hash=>$hash_parent, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007705 esc_html($hash_parent_short)) .
7706 ')';
7707 } elsif (!$co{'parent'}) {
7708 # --root commitdiff
7709 $formats_nav .= ' (initial)';
7710 } elsif (scalar @{$co{'parents'}} == 1) {
7711 # single parent commit
7712 $formats_nav .=
7713 ' (parent: ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007714 $cgi->a({-href => href(-replay=>1,
7715 hash=>$co{'parent'}, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007716 esc_html(substr($co{'parent'}, 0, 7))) .
7717 ')';
7718 } else {
7719 # merge commit
Jakub Narebskicd030c32007-06-08 13:33:28 +02007720 if ($hash_parent eq '--cc') {
7721 $formats_nav .= ' | ' .
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007722 $cgi->a({-href => href(-replay=>1,
Jakub Narebskicd030c32007-06-08 13:33:28 +02007723 hash=>$hash, hash_parent=>'-c')},
7724 'combined');
7725 } else { # $hash_parent eq '-c'
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=>'--cc')},
7729 'compact');
7730 }
Jakub Narebski151602d2006-10-23 00:37:56 +02007731 $formats_nav .=
7732 ' (merge: ' .
7733 join(' ', map {
Jakub Narebskid0e6e292011-10-31 00:36:26 +01007734 $cgi->a({-href => href(-replay=>1,
7735 hash=>$_, hash_base=>undef)},
Jakub Narebski151602d2006-10-23 00:37:56 +02007736 esc_html(substr($_, 0, 7)));
7737 } @{$co{'parents'}} ) .
7738 ')';
7739 }
7740 }
7741
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007742 my $hash_parent_param = $hash_parent;
Jakub Narebskicd030c32007-06-08 13:33:28 +02007743 if (!defined $hash_parent_param) {
7744 # --cc for multiple parents, --root for parentless
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007745 $hash_parent_param =
Jakub Narebskicd030c32007-06-08 13:33:28 +02007746 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
Kay Sieversbddec012005-08-07 20:25:42 +02007747 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007748
7749 # read commitdiff
7750 my $fd;
7751 my @difftree;
Jakub Narebskieee08902006-08-24 00:15:14 +02007752 if ($format eq 'html') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007753 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski45bd0c82006-10-30 22:29:06 +01007754 "--no-commit-id", "--patch-with-raw", "--full-index",
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007755 $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007756 or die_error(500, "Open git-diff-tree failed");
Jakub Narebskieee08902006-08-24 00:15:14 +02007757
Jakub Narebski04408c32006-11-18 23:35:38 +01007758 while (my $line = <$fd>) {
7759 chomp $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02007760 # empty line ends raw part of diff-tree output
7761 last unless $line;
Jakub Narebski493e01d2007-05-07 01:10:06 +02007762 push @difftree, scalar parse_difftree_raw_line($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02007763 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007764
Jakub Narebskieee08902006-08-24 00:15:14 +02007765 } elsif ($format eq 'plain') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02007766 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskifb1dde42007-05-07 01:10:07 +02007767 '-p', $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02007768 or die_error(500, "Open git-diff-tree failed");
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007769 } elsif ($format eq 'patch') {
7770 # For commit ranges, we limit the output to the number of
7771 # patches specified in the 'patches' feature.
7772 # For single commits, we limit the output to a single patch,
7773 # diverging from the git-format-patch default.
7774 my @commit_spec = ();
7775 if ($hash_parent) {
7776 if ($patch_max > 0) {
7777 push @commit_spec, "-$patch_max";
7778 }
7779 push @commit_spec, '-n', "$hash_parent..$hash";
7780 } else {
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +01007781 if ($params{-single}) {
7782 push @commit_spec, '-1';
7783 } else {
7784 if ($patch_max > 0) {
7785 push @commit_spec, "-$patch_max";
7786 }
7787 push @commit_spec, "-n";
7788 }
7789 push @commit_spec, '--root', $hash;
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007790 }
Pavan Kumar Sunkara04794fd2010-05-10 18:41:35 +02007791 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
7792 '--encoding=utf8', '--stdout', @commit_spec
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007793 or die_error(500, "Open git-format-patch failed");
Jakub Narebskieee08902006-08-24 00:15:14 +02007794 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007795 die_error(400, "Unknown commitdiff format");
Jakub Narebskieee08902006-08-24 00:15:14 +02007796 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02007797
Kay Sievers11044292005-10-19 03:18:45 +02007798 # non-textual hash id's can be cached
7799 my $expires;
7800 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
7801 $expires = "+1d";
7802 }
Kay Sievers09bd7892005-08-07 20:21:23 +02007803
Jakub Narebskieee08902006-08-24 00:15:14 +02007804 # write commit message
7805 if ($format eq 'html') {
7806 my $refs = git_get_references();
7807 my $ref = format_ref_marker($refs, $co{'id'});
Kay Sievers19806692005-08-07 20:26:27 +02007808
Jakub Narebskieee08902006-08-24 00:15:14 +02007809 git_header_html(undef, $expires);
7810 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
7811 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
Giuseppe Bilottaf88bafa2009-06-30 00:00:49 +02007812 print "<div class=\"title_text\">\n" .
7813 "<table class=\"object_header\">\n";
7814 git_print_authorship_rows(\%co);
7815 print "</table>".
7816 "</div>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02007817 print "<div class=\"page_body\">\n";
Jakub Narebski82560982006-10-24 13:55:33 +02007818 if (@{$co{'comment'}} > 1) {
7819 print "<div class=\"log\">\n";
7820 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
7821 print "</div>\n"; # class="log"
7822 }
Kay Sievers1b1cd422005-08-07 20:28:01 +02007823
Jakub Narebskieee08902006-08-24 00:15:14 +02007824 } elsif ($format eq 'plain') {
7825 my $refs = git_get_references("tags");
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007826 my $tagname = git_get_rev_name_tags($hash);
Jakub Narebskieee08902006-08-24 00:15:14 +02007827 my $filename = basename($project) . "-$hash.patch";
7828
7829 print $cgi->header(
7830 -type => 'text/plain',
7831 -charset => 'utf-8',
7832 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07007833 -content_disposition => 'inline; filename="' . "$filename" . '"');
Jakub Narebskieee08902006-08-24 00:15:14 +02007834 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
Yasushi SHOJI77202242008-01-29 21:16:02 +09007835 print "From: " . to_utf8($co{'author'}) . "\n";
7836 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7837 print "Subject: " . to_utf8($co{'title'}) . "\n";
7838
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007839 print "X-Git-Tag: $tagname\n" if $tagname;
Jakub Narebskieee08902006-08-24 00:15:14 +02007840 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
Jakub Narebskiedf735a2006-08-24 19:45:30 +02007841
Jakub Narebskieee08902006-08-24 00:15:14 +02007842 foreach my $line (@{$co{'comment'}}) {
Yasushi SHOJI77202242008-01-29 21:16:02 +09007843 print to_utf8($line) . "\n";
Kay Sievers19806692005-08-07 20:26:27 +02007844 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007845 print "---\n\n";
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007846 } elsif ($format eq 'patch') {
7847 my $filename = basename($project) . "-$hash.patch";
7848
7849 print $cgi->header(
7850 -type => 'text/plain',
7851 -charset => 'utf-8',
7852 -expires => $expires,
7853 -content_disposition => 'inline; filename="' . "$filename" . '"');
Kay Sievers19806692005-08-07 20:26:27 +02007854 }
Jakub Narebskieee08902006-08-24 00:15:14 +02007855
7856 # write patch
7857 if ($format eq 'html') {
Jakub Narebskicd030c32007-06-08 13:33:28 +02007858 my $use_parents = !defined $hash_parent ||
7859 $hash_parent eq '-c' || $hash_parent eq '--cc';
7860 git_difftree_body(\@difftree, $hash,
7861 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebskib4657e72006-08-28 14:48:14 +02007862 print "<br/>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02007863
Kato Kazuyoshi6ba1eb52011-10-31 00:36:22 +01007864 git_patchset_body($fd, $diff_style,
7865 \@difftree, $hash,
Jakub Narebskicd030c32007-06-08 13:33:28 +02007866 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebski157e43b2006-08-24 19:34:36 +02007867 close $fd;
Jakub Narebskieee08902006-08-24 00:15:14 +02007868 print "</div>\n"; # class="page_body"
7869 git_footer_html();
7870
7871 } elsif ($format eq 'plain') {
7872 local $/ = undef;
7873 print <$fd>;
7874 close $fd
7875 or print "Reading git-diff-tree failed\n";
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007876 } elsif ($format eq 'patch') {
7877 local $/ = undef;
7878 print <$fd>;
7879 close $fd
7880 or print "Reading git-format-patch failed\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02007881 }
7882}
7883
7884sub git_commitdiff_plain {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01007885 git_commitdiff(-format => 'plain');
Kay Sievers19806692005-08-07 20:26:27 +02007886}
7887
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01007888# format-patch-style patches
7889sub git_patch {
Jakub Narebski1655c982009-10-09 14:26:44 +02007890 git_commitdiff(-format => 'patch', -single => 1);
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +01007891}
7892
7893sub git_patches {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01007894 git_commitdiff(-format => 'patch');
Kay Sievers09bd7892005-08-07 20:21:23 +02007895}
7896
7897sub git_history {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007898 git_log_generic('history', \&git_history_body,
7899 $hash_base, $hash_parent_base,
7900 $file_name, $hash);
Kay Sievers161332a2005-08-07 19:49:46 +02007901}
Kay Sievers19806692005-08-07 20:26:27 +02007902
7903sub git_search {
Jakub Narebskie0ca3642011-06-22 17:28:52 +02007904 $searchtype ||= 'commit';
7905
7906 # check if appropriate features are enabled
7907 gitweb_check_feature('search')
7908 or die_error(403, "Search is disabled");
7909 if ($searchtype eq 'pickaxe') {
7910 # pickaxe may take all resources of your box and run for several minutes
7911 # with every query - so decide by yourself how public you make this feature
7912 gitweb_check_feature('pickaxe')
7913 or die_error(403, "Pickaxe search is disabled");
7914 }
7915 if ($searchtype eq 'grep') {
7916 # grep search might be potentially CPU-intensive, too
7917 gitweb_check_feature('grep')
7918 or die_error(403, "Grep search is disabled");
7919 }
7920
Kay Sievers19806692005-08-07 20:26:27 +02007921 if (!defined $searchtext) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007922 die_error(400, "Text field is empty");
Kay Sievers19806692005-08-07 20:26:27 +02007923 }
7924 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02007925 $hash = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02007926 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02007927 my %co = parse_commit($hash);
Kay Sievers19806692005-08-07 20:26:27 +02007928 if (!%co) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02007929 die_error(404, "Unknown commit object");
Kay Sievers19806692005-08-07 20:26:27 +02007930 }
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00007931 if (!defined $page) {
7932 $page = 0;
7933 }
Jakub Narebski04f7a942006-09-11 00:29:27 +02007934
Jakub Narebski16f20722011-06-22 17:28:53 +02007935 if ($searchtype eq 'commit' ||
7936 $searchtype eq 'author' ||
7937 $searchtype eq 'committer') {
7938 git_search_message(%co);
7939 } elsif ($searchtype eq 'pickaxe') {
7940 git_search_changes(%co);
7941 } elsif ($searchtype eq 'grep') {
7942 git_search_files(%co);
Jakub Narebski1ae05be2011-06-22 17:28:55 +02007943 } else {
7944 die_error(400, "Unknown search type");
Kay Sieversc994d622005-08-07 20:27:18 +02007945 }
Kay Sievers19806692005-08-07 20:26:27 +02007946}
7947
Petr Baudis88ad7292006-10-24 05:15:46 +02007948sub git_search_help {
7949 git_header_html();
7950 git_print_page_nav('','', $hash,$hash,$hash);
7951 print <<EOT;
Petr Baudis0e559912008-02-26 13:22:08 +01007952<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7953regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7954the pattern entered is recognized as the POSIX extended
7955<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7956insensitive).</p>
Petr Baudis88ad7292006-10-24 05:15:46 +02007957<dl>
7958<dt><b>commit</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01007959<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02007960EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08007961 my $have_grep = gitweb_check_feature('grep');
Petr Baudise7738552007-05-17 04:31:12 +02007962 if ($have_grep) {
7963 print <<EOT;
7964<dt><b>grep</b></dt>
7965<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
Petr Baudis0e559912008-02-26 13:22:08 +01007966 a different one) are searched for the given pattern. On large trees, this search can take
7967a while and put some strain on the server, so please use it with some consideration. Note that
7968due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7969case-sensitive.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02007970EOT
7971 }
7972 print <<EOT;
Petr Baudis88ad7292006-10-24 05:15:46 +02007973<dt><b>author</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01007974<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 +02007975<dt><b>committer</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01007976<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 +02007977EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08007978 my $have_pickaxe = gitweb_check_feature('pickaxe');
Petr Baudis88ad7292006-10-24 05:15:46 +02007979 if ($have_pickaxe) {
7980 print <<EOT;
7981<dt><b>pickaxe</b></dt>
7982<dd>All commits that caused the string to appear or disappear from any file (changes that
7983added, removed or "modified" the string) will be listed. This search can take a while and
Petr Baudis0e559912008-02-26 13:22:08 +01007984takes a lot of strain on the server, so please use it wisely. Note that since you may be
7985interested even in changes just changing the case as well, this search is case sensitive.</dd>
Petr Baudis88ad7292006-10-24 05:15:46 +02007986EOT
7987 }
7988 print "</dl>\n";
7989 git_footer_html();
7990}
7991
Kay Sievers19806692005-08-07 20:26:27 +02007992sub git_shortlog {
Jakub Narebski69ca37d2009-11-13 02:02:14 +01007993 git_log_generic('shortlog', \&git_shortlog_body,
7994 $hash, $hash_parent);
Kay Sievers19806692005-08-07 20:26:27 +02007995}
Jakub Narebski717b8312006-07-31 21:22:15 +02007996
7997## ......................................................................
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01007998## feeds (RSS, Atom; OPML)
Jakub Narebski717b8312006-07-31 21:22:15 +02007999
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008000sub git_feed {
8001 my $format = shift || 'atom';
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08008002 my $have_blame = gitweb_check_feature('blame');
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008003
8004 # Atom: http://www.atomenabled.org/developers/syndication/
8005 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
8006 if ($format ne 'rss' && $format ne 'atom') {
Lea Wiemann074afaa2008-06-19 22:03:21 +02008007 die_error(400, "Unknown web feed format");
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008008 }
8009
8010 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
8011 my $head = $hash || 'HEAD';
Jakub Narebski311e5522008-02-26 13:22:06 +01008012 my @commitlist = parse_commits($head, 150, 0, $file_name);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008013
8014 my %latest_commit;
8015 my %latest_date;
8016 my $content_type = "application/$format+xml";
8017 if (defined $cgi->http('HTTP_ACCEPT') &&
8018 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
8019 # browser (feed reader) prefers text/xml
8020 $content_type = 'text/xml';
8021 }
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008022 if (defined($commitlist[0])) {
8023 %latest_commit = %{$commitlist[0]};
Giuseppe Bilottacd956c72009-01-26 12:50:16 +01008024 my $latest_epoch = $latest_commit{'committer_epoch'};
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04008025 exit_if_unmodified_since($latest_epoch);
8026 %latest_date = parse_date($latest_epoch, $latest_commit{'comitter_tz'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008027 }
W. Trevor Kingb7d565e2012-03-29 08:45:48 -04008028 print $cgi->header(
8029 -type => $content_type,
8030 -charset => 'utf-8',
8031 %latest_date ? (-last_modified => $latest_date{'rfc2822'}) : (),
8032 -status => '200 OK');
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008033
8034 # Optimization: skip generating the body if client asks only
8035 # for Last-Modified date.
8036 return if ($cgi->request_method() eq 'HEAD');
8037
8038 # header variables
8039 my $title = "$site_name - $project/$action";
8040 my $feed_type = 'log';
8041 if (defined $hash) {
8042 $title .= " - '$hash'";
8043 $feed_type = 'branch log';
8044 if (defined $file_name) {
8045 $title .= " :: $file_name";
8046 $feed_type = 'history';
8047 }
8048 } elsif (defined $file_name) {
8049 $title .= " - $file_name";
8050 $feed_type = 'history';
8051 }
8052 $title .= " $feed_type";
8053 my $descr = git_get_project_description($project);
8054 if (defined $descr) {
8055 $descr = esc_html($descr);
8056 } else {
8057 $descr = "$project " .
8058 ($format eq 'rss' ? 'RSS' : 'Atom') .
8059 " feed";
8060 }
8061 my $owner = git_get_project_owner($project);
8062 $owner = esc_html($owner);
8063
8064 #header
8065 my $alt_url;
8066 if (defined $file_name) {
8067 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
8068 } elsif (defined $hash) {
8069 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
8070 } else {
8071 $alt_url = href(-full=>1, action=>"summary");
8072 }
8073 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
8074 if ($format eq 'rss') {
8075 print <<XML;
Jakub Narebski59b9f612006-08-22 23:42:53 +02008076<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
8077<channel>
Jakub Narebski59b9f612006-08-22 23:42:53 +02008078XML
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008079 print "<title>$title</title>\n" .
8080 "<link>$alt_url</link>\n" .
8081 "<description>$descr</description>\n" .
Giuseppe Bilotta3ac109a2009-01-26 12:50:13 +01008082 "<language>en</language>\n" .
8083 # project owner is responsible for 'editorial' content
8084 "<managingEditor>$owner</managingEditor>\n";
Giuseppe Bilotta1ba68ce2009-01-26 12:50:11 +01008085 if (defined $logo || defined $favicon) {
8086 # prefer the logo to the favicon, since RSS
8087 # doesn't allow both
8088 my $img = esc_url($logo || $favicon);
8089 print "<image>\n" .
8090 "<url>$img</url>\n" .
8091 "<title>$title</title>\n" .
8092 "<link>$alt_url</link>\n" .
8093 "</image>\n";
8094 }
Giuseppe Bilotta0cf31282009-01-26 12:50:14 +01008095 if (%latest_date) {
8096 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
8097 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
8098 }
Giuseppe Bilottaad59a7a2009-01-26 12:50:12 +01008099 print "<generator>gitweb v.$version/$git_version</generator>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008100 } elsif ($format eq 'atom') {
8101 print <<XML;
8102<feed xmlns="http://www.w3.org/2005/Atom">
8103XML
8104 print "<title>$title</title>\n" .
8105 "<subtitle>$descr</subtitle>\n" .
8106 '<link rel="alternate" type="text/html" href="' .
8107 $alt_url . '" />' . "\n" .
8108 '<link rel="self" type="' . $content_type . '" href="' .
8109 $cgi->self_url() . '" />' . "\n" .
8110 "<id>" . href(-full=>1) . "</id>\n" .
8111 # use project owner for feed author
8112 "<author><name>$owner</name></author>\n";
8113 if (defined $favicon) {
8114 print "<icon>" . esc_url($favicon) . "</icon>\n";
8115 }
Jonathan Nieder9d9f5e72010-09-03 19:44:39 -05008116 if (defined $logo) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008117 # not twice as wide as tall: 72 x 27 pixels
Jakub Narebskie1147262006-12-04 14:09:43 +01008118 print "<logo>" . esc_url($logo) . "</logo>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008119 }
8120 if (! %latest_date) {
8121 # dummy date to keep the feed valid until commits trickle in:
8122 print "<updated>1970-01-01T00:00:00Z</updated>\n";
8123 } else {
8124 print "<updated>$latest_date{'iso-8601'}</updated>\n";
8125 }
Giuseppe Bilottaad59a7a2009-01-26 12:50:12 +01008126 print "<generator version='$version/$git_version'>gitweb</generator>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008127 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008128
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008129 # contents
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008130 for (my $i = 0; $i <= $#commitlist; $i++) {
8131 my %co = %{$commitlist[$i]};
8132 my $commit = $co{'id'};
Jakub Narebski717b8312006-07-31 21:22:15 +02008133 # we read 150, we always show 30 and the ones more recent than 48 hours
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01008134 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02008135 last;
8136 }
Jakub Narebski6368d9f2011-03-19 23:53:55 +01008137 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008138
8139 # get list of changed files
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00008140 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskic906b182007-05-19 02:47:51 +02008141 $co{'parent'} || "--root",
8142 $co{'id'}, "--", (defined $file_name ? $file_name : ())
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02008143 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02008144 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02008145 close $fd
8146 or next;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008147
8148 # print element (entry, item)
Florian La Rochee62a6412008-02-03 12:38:46 +01008149 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008150 if ($format eq 'rss') {
8151 print "<item>\n" .
8152 "<title>" . esc_html($co{'title'}) . "</title>\n" .
8153 "<author>" . esc_html($co{'author'}) . "</author>\n" .
8154 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
8155 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
8156 "<link>$co_url</link>\n" .
8157 "<description>" . esc_html($co{'title'}) . "</description>\n" .
8158 "<content:encoded>" .
8159 "<![CDATA[\n";
8160 } elsif ($format eq 'atom') {
8161 print "<entry>\n" .
8162 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
8163 "<updated>$cd{'iso-8601'}</updated>\n" .
Jakub Narebskiab23c192006-11-25 15:54:33 +01008164 "<author>\n" .
8165 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
8166 if ($co{'author_email'}) {
8167 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
8168 }
8169 print "</author>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008170 # use committer for contributor
Jakub Narebskiab23c192006-11-25 15:54:33 +01008171 "<contributor>\n" .
8172 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
8173 if ($co{'committer_email'}) {
8174 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
8175 }
8176 print "</contributor>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008177 "<published>$cd{'iso-8601'}</published>\n" .
8178 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
8179 "<id>$co_url</id>\n" .
8180 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
8181 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
8182 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008183 my $comment = $co{'comment'};
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008184 print "<pre>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008185 foreach my $line (@$comment) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008186 $line = esc_html($line);
8187 print "$line\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008188 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008189 print "</pre><ul>\n";
8190 foreach my $difftree_line (@difftree) {
8191 my %difftree = parse_difftree_raw_line($difftree_line);
8192 next if !$difftree{'from_id'};
8193
8194 my $file = $difftree{'file'} || $difftree{'to_file'};
8195
8196 print "<li>" .
8197 "[" .
8198 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
8199 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
8200 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
8201 file_name=>$file, file_parent=>$difftree{'from_file'}),
8202 -title => "diff"}, 'D');
8203 if ($have_blame) {
8204 print $cgi->a({-href => href(-full=>1, action=>"blame",
8205 file_name=>$file, hash_base=>$commit),
8206 -title => "blame"}, 'B');
Jakub Narebski717b8312006-07-31 21:22:15 +02008207 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008208 # if this is not a feed of a file history
8209 if (!defined $file_name || $file_name ne $file) {
8210 print $cgi->a({-href => href(-full=>1, action=>"history",
8211 file_name=>$file, hash=>$commit),
8212 -title => "history"}, 'H');
8213 }
8214 $file = esc_path($file);
8215 print "] ".
8216 "$file</li>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02008217 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008218 if ($format eq 'rss') {
8219 print "</ul>]]>\n" .
8220 "</content:encoded>\n" .
8221 "</item>\n";
8222 } elsif ($format eq 'atom') {
8223 print "</ul>\n</div>\n" .
8224 "</content>\n" .
8225 "</entry>\n";
8226 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008227 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008228
8229 # end of feed
8230 if ($format eq 'rss') {
8231 print "</channel>\n</rss>\n";
Jakub Narebski3278fbc2009-05-11 19:37:28 +02008232 } elsif ($format eq 'atom') {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01008233 print "</feed>\n";
8234 }
8235}
8236
8237sub git_rss {
8238 git_feed('rss');
8239}
8240
8241sub git_atom {
8242 git_feed('atom');
Jakub Narebski717b8312006-07-31 21:22:15 +02008243}
8244
8245sub git_opml {
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008246 my @list = git_get_projects_list($project_filter, $strict_export);
Jakub Narebski12b14432011-04-29 19:51:56 +02008247 if (!@list) {
8248 die_error(404, "No projects found");
8249 }
Jakub Narebski717b8312006-07-31 21:22:15 +02008250
Giuseppe Bilottaae357852009-01-02 13:49:30 +01008251 print $cgi->header(
8252 -type => 'text/xml',
8253 -charset => 'utf-8',
8254 -content_disposition => 'inline; filename="opml.xml"');
8255
Jürgen Kreileder5d791052011-12-17 10:22:22 +01008256 my $title = esc_html($site_name);
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008257 my $filter = " within subdirectory ";
8258 if (defined $project_filter) {
8259 $filter .= esc_html($project_filter);
8260 } else {
8261 $filter = "";
8262 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02008263 print <<XML;
8264<?xml version="1.0" encoding="utf-8"?>
8265<opml version="1.0">
8266<head>
Bernhard R. Link19d2d232012-01-30 21:07:37 +01008267 <title>$title OPML Export$filter</title>
Jakub Narebski59b9f612006-08-22 23:42:53 +02008268</head>
8269<body>
8270<outline text="git RSS feeds">
8271XML
Jakub Narebski717b8312006-07-31 21:22:15 +02008272
8273 foreach my $pr (@list) {
8274 my %proj = %$pr;
Jakub Narebski847e01f2006-08-14 02:05:47 +02008275 my $head = git_get_head_hash($proj{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02008276 if (!defined $head) {
8277 next;
8278 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02008279 $git_dir = "$projectroot/$proj{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02008280 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02008281 if (!%co) {
8282 next;
8283 }
8284
8285 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
Giuseppe Bilottadf63fbb2009-01-02 13:15:28 +01008286 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
8287 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
Jakub Narebski717b8312006-07-31 21:22:15 +02008288 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
8289 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02008290 print <<XML;
8291</outline>
8292</body>
8293</opml>
8294XML
Jakub Narebski717b8312006-07-31 21:22:15 +02008295}